Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'tag-for-linus-3.5' of git://git.linaro.org/people/sumitsemwal/linux-dma-buf

Pull dma-buf updates from Sumit Semwal:
"Here's the first signed-tag pull request for dma-buf framework. It
includes the following key items:
- mmap support
- vmap support
- related documentation updates

These are needed by various drivers to allow mmap/vmap of dma-buf
shared buffers. Dave Airlie has some prime patches dependent on the
vmap pull as well."

* tag 'tag-for-linus-3.5' of git://git.linaro.org/people/sumitsemwal/linux-dma-buf:
dma-buf: add initial vmap documentation
dma-buf: minor documentation fixes.
dma-buf: add vmap interface
dma-buf: mmap support

+233 -8
+102 -7
Documentation/dma-buf-sharing.txt
··· 29 29 in memory, mapped into its own address space, so it can access the same area 30 30 of memory. 31 31 32 - *IMPORTANT*: [see https://lkml.org/lkml/2011/12/20/211 for more details] 33 - For this first version, A buffer shared using the dma_buf sharing API: 34 - - *may* be exported to user space using "mmap" *ONLY* by exporter, outside of 35 - this framework. 36 - - with this new iteration of the dma-buf api cpu access from the kernel has been 37 - enable, see below for the details. 38 - 39 32 dma-buf operations for device dma only 40 33 -------------------------------------- 41 34 ··· 293 300 Note that these calls need to always succeed. The exporter needs to complete 294 301 any preparations that might fail in begin_cpu_access. 295 302 303 + For some cases the overhead of kmap can be too high, a vmap interface 304 + is introduced. This interface should be used very carefully, as vmalloc 305 + space is a limited resources on many architectures. 306 + 307 + Interfaces: 308 + void *dma_buf_vmap(struct dma_buf *dmabuf) 309 + void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) 310 + 311 + The vmap call can fail if there is no vmap support in the exporter, or if it 312 + runs out of vmalloc space. Fallback to kmap should be implemented. 313 + 296 314 3. Finish access 297 315 298 316 When the importer is done accessing the range specified in begin_cpu_access, ··· 316 312 size_t start, size_t len, 317 313 enum dma_data_direction dir); 318 314 315 + 316 + Direct Userspace Access/mmap Support 317 + ------------------------------------ 318 + 319 + Being able to mmap an export dma-buf buffer object has 2 main use-cases: 320 + - CPU fallback processing in a pipeline and 321 + - supporting existing mmap interfaces in importers. 322 + 323 + 1. CPU fallback processing in a pipeline 324 + 325 + In many processing pipelines it is sometimes required that the cpu can access 326 + the data in a dma-buf (e.g. for thumbnail creation, snapshots, ...). To avoid 327 + the need to handle this specially in userspace frameworks for buffer sharing 328 + it's ideal if the dma_buf fd itself can be used to access the backing storage 329 + from userspace using mmap. 330 + 331 + Furthermore Android's ION framework already supports this (and is otherwise 332 + rather similar to dma-buf from a userspace consumer side with using fds as 333 + handles, too). So it's beneficial to support this in a similar fashion on 334 + dma-buf to have a good transition path for existing Android userspace. 335 + 336 + No special interfaces, userspace simply calls mmap on the dma-buf fd. 337 + 338 + 2. Supporting existing mmap interfaces in exporters 339 + 340 + Similar to the motivation for kernel cpu access it is again important that 341 + the userspace code of a given importing subsystem can use the same interfaces 342 + with a imported dma-buf buffer object as with a native buffer object. This is 343 + especially important for drm where the userspace part of contemporary OpenGL, 344 + X, and other drivers is huge, and reworking them to use a different way to 345 + mmap a buffer rather invasive. 346 + 347 + The assumption in the current dma-buf interfaces is that redirecting the 348 + initial mmap is all that's needed. A survey of some of the existing 349 + subsystems shows that no driver seems to do any nefarious thing like syncing 350 + up with outstanding asynchronous processing on the device or allocating 351 + special resources at fault time. So hopefully this is good enough, since 352 + adding interfaces to intercept pagefaults and allow pte shootdowns would 353 + increase the complexity quite a bit. 354 + 355 + Interface: 356 + int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, 357 + unsigned long); 358 + 359 + If the importing subsystem simply provides a special-purpose mmap call to set 360 + up a mapping in userspace, calling do_mmap with dma_buf->file will equally 361 + achieve that for a dma-buf object. 362 + 363 + 3. Implementation notes for exporters 364 + 365 + Because dma-buf buffers have invariant size over their lifetime, the dma-buf 366 + core checks whether a vma is too large and rejects such mappings. The 367 + exporter hence does not need to duplicate this check. 368 + 369 + Because existing importing subsystems might presume coherent mappings for 370 + userspace, the exporter needs to set up a coherent mapping. If that's not 371 + possible, it needs to fake coherency by manually shooting down ptes when 372 + leaving the cpu domain and flushing caches at fault time. Note that all the 373 + dma_buf files share the same anon inode, hence the exporter needs to replace 374 + the dma_buf file stored in vma->vm_file with it's own if pte shootdown is 375 + requred. This is because the kernel uses the underlying inode's address_space 376 + for vma tracking (and hence pte tracking at shootdown time with 377 + unmap_mapping_range). 378 + 379 + If the above shootdown dance turns out to be too expensive in certain 380 + scenarios, we can extend dma-buf with a more explicit cache tracking scheme 381 + for userspace mappings. But the current assumption is that using mmap is 382 + always a slower path, so some inefficiencies should be acceptable. 383 + 384 + Exporters that shoot down mappings (for any reasons) shall not do any 385 + synchronization at fault time with outstanding device operations. 386 + Synchronization is an orthogonal issue to sharing the backing storage of a 387 + buffer and hence should not be handled by dma-buf itself. This is explictly 388 + mentioned here because many people seem to want something like this, but if 389 + different exporters handle this differently, buffer sharing can fail in 390 + interesting ways depending upong the exporter (if userspace starts depending 391 + upon this implicit synchronization). 319 392 320 393 Miscellaneous notes 321 394 ------------------- ··· 416 335 flag be set when the dma-buf fd is created. So any API provided by 417 336 the exporting driver to create a dmabuf fd must provide a way to let 418 337 userspace control setting of O_CLOEXEC flag passed in to dma_buf_fd(). 338 + 339 + - If an exporter needs to manually flush caches and hence needs to fake 340 + coherency for mmap support, it needs to be able to zap all the ptes pointing 341 + at the backing storage. Now linux mm needs a struct address_space associated 342 + with the struct file stored in vma->vm_file to do that with the function 343 + unmap_mapping_range. But the dma_buf framework only backs every dma_buf fd 344 + with the anon_file struct file, i.e. all dma_bufs share the same file. 345 + 346 + Hence exporters need to setup their own file (and address_space) association 347 + by setting vma->vm_file and adjusting vma->vm_pgoff in the dma_buf mmap 348 + callback. In the specific case of a gem driver the exporter could use the 349 + shmem file already provided by gem (and set vm_pgoff = 0). Exporters can then 350 + zap ptes by unmapping the corresponding range of the struct address_space 351 + associated with their own file. 419 352 420 353 References: 421 354 [1] struct dma_buf_ops in include/linux/dma-buf.h
+98 -1
drivers/base/dma-buf.c
··· 44 44 return 0; 45 45 } 46 46 47 + static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) 48 + { 49 + struct dma_buf *dmabuf; 50 + 51 + if (!is_dma_buf_file(file)) 52 + return -EINVAL; 53 + 54 + dmabuf = file->private_data; 55 + 56 + /* check for overflowing the buffer's size */ 57 + if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > 58 + dmabuf->size >> PAGE_SHIFT) 59 + return -EINVAL; 60 + 61 + return dmabuf->ops->mmap(dmabuf, vma); 62 + } 63 + 47 64 static const struct file_operations dma_buf_fops = { 48 65 .release = dma_buf_release, 66 + .mmap = dma_buf_mmap_internal, 49 67 }; 50 68 51 69 /* ··· 100 82 || !ops->unmap_dma_buf 101 83 || !ops->release 102 84 || !ops->kmap_atomic 103 - || !ops->kmap)) { 85 + || !ops->kmap 86 + || !ops->mmap)) { 104 87 return ERR_PTR(-EINVAL); 105 88 } 106 89 ··· 425 406 dmabuf->ops->kunmap(dmabuf, page_num, vaddr); 426 407 } 427 408 EXPORT_SYMBOL_GPL(dma_buf_kunmap); 409 + 410 + 411 + /** 412 + * dma_buf_mmap - Setup up a userspace mmap with the given vma 413 + * @dmabuf: [in] buffer that should back the vma 414 + * @vma: [in] vma for the mmap 415 + * @pgoff: [in] offset in pages where this mmap should start within the 416 + * dma-buf buffer. 417 + * 418 + * This function adjusts the passed in vma so that it points at the file of the 419 + * dma_buf operation. It alsog adjusts the starting pgoff and does bounds 420 + * checking on the size of the vma. Then it calls the exporters mmap function to 421 + * set up the mapping. 422 + * 423 + * Can return negative error values, returns 0 on success. 424 + */ 425 + int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, 426 + unsigned long pgoff) 427 + { 428 + if (WARN_ON(!dmabuf || !vma)) 429 + return -EINVAL; 430 + 431 + /* check for offset overflow */ 432 + if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff) 433 + return -EOVERFLOW; 434 + 435 + /* check for overflowing the buffer's size */ 436 + if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > 437 + dmabuf->size >> PAGE_SHIFT) 438 + return -EINVAL; 439 + 440 + /* readjust the vma */ 441 + if (vma->vm_file) 442 + fput(vma->vm_file); 443 + 444 + vma->vm_file = dmabuf->file; 445 + get_file(vma->vm_file); 446 + 447 + vma->vm_pgoff = pgoff; 448 + 449 + return dmabuf->ops->mmap(dmabuf, vma); 450 + } 451 + EXPORT_SYMBOL_GPL(dma_buf_mmap); 452 + 453 + /** 454 + * dma_buf_vmap - Create virtual mapping for the buffer object into kernel 455 + * address space. Same restrictions as for vmap and friends apply. 456 + * @dmabuf: [in] buffer to vmap 457 + * 458 + * This call may fail due to lack of virtual mapping address space. 459 + * These calls are optional in drivers. The intended use for them 460 + * is for mapping objects linear in kernel space for high use objects. 461 + * Please attempt to use kmap/kunmap before thinking about these interfaces. 462 + */ 463 + void *dma_buf_vmap(struct dma_buf *dmabuf) 464 + { 465 + if (WARN_ON(!dmabuf)) 466 + return NULL; 467 + 468 + if (dmabuf->ops->vmap) 469 + return dmabuf->ops->vmap(dmabuf); 470 + return NULL; 471 + } 472 + EXPORT_SYMBOL_GPL(dma_buf_vmap); 473 + 474 + /** 475 + * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. 476 + * @dmabuf: [in] buffer to vunmap 477 + */ 478 + void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) 479 + { 480 + if (WARN_ON(!dmabuf)) 481 + return; 482 + 483 + if (dmabuf->ops->vunmap) 484 + dmabuf->ops->vunmap(dmabuf, vaddr); 485 + } 486 + EXPORT_SYMBOL_GPL(dma_buf_vunmap);
+33
include/linux/dma-buf.h
··· 61 61 * This Callback must not sleep. 62 62 * @kmap: maps a page from the buffer into kernel address space. 63 63 * @kunmap: [optional] unmaps a page from the buffer. 64 + * @mmap: used to expose the backing storage to userspace. Note that the 65 + * mapping needs to be coherent - if the exporter doesn't directly 66 + * support this, it needs to fake coherency by shooting down any ptes 67 + * when transitioning away from the cpu domain. 68 + * @vmap: [optional] creates a virtual mapping for the buffer into kernel 69 + * address space. Same restrictions as for vmap and friends apply. 70 + * @vunmap: [optional] unmaps a vmap from the buffer 64 71 */ 65 72 struct dma_buf_ops { 66 73 int (*attach)(struct dma_buf *, struct device *, ··· 99 92 void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *); 100 93 void *(*kmap)(struct dma_buf *, unsigned long); 101 94 void (*kunmap)(struct dma_buf *, unsigned long, void *); 95 + 96 + int (*mmap)(struct dma_buf *, struct vm_area_struct *vma); 97 + 98 + void *(*vmap)(struct dma_buf *); 99 + void (*vunmap)(struct dma_buf *, void *vaddr); 102 100 }; 103 101 104 102 /** ··· 179 167 void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *); 180 168 void *dma_buf_kmap(struct dma_buf *, unsigned long); 181 169 void dma_buf_kunmap(struct dma_buf *, unsigned long, void *); 170 + 171 + int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, 172 + unsigned long); 173 + void *dma_buf_vmap(struct dma_buf *); 174 + void dma_buf_vunmap(struct dma_buf *, void *vaddr); 182 175 #else 183 176 184 177 static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, ··· 263 246 264 247 static inline void dma_buf_kunmap(struct dma_buf *dmabuf, 265 248 unsigned long pnum, void *vaddr) 249 + { 250 + } 251 + 252 + static inline int dma_buf_mmap(struct dma_buf *dmabuf, 253 + struct vm_area_struct *vma, 254 + unsigned long pgoff) 255 + { 256 + return -ENODEV; 257 + } 258 + 259 + static inline void *dma_buf_vmap(struct dma_buf *dmabuf) 260 + { 261 + return NULL; 262 + } 263 + 264 + static inline void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr) 266 265 { 267 266 } 268 267 #endif /* CONFIG_DMA_SHARED_BUFFER */