diff options
author | Daniel Vetter <daniel.vetter@ffwll.ch> | 2012-04-24 05:08:52 -0400 |
---|---|---|
committer | Sumit Semwal <sumit.semwal@ti.com> | 2012-05-25 03:05:24 -0400 |
commit | 4c78513e457f72d5554a0f6e2eabfad7b98e4f19 (patch) | |
tree | 2cdb5c552580f159ff66632aef781e8739c85c42 /drivers/base | |
parent | 76e10d158efb6d4516018846f60c2ab5501900bc (diff) |
dma-buf: mmap support
Compared to Rob Clark's RFC I've ditched the prepare/finish hooks
and corresponding ioctls on the dma_buf file. The major reason for
that is that many people seem to be under the impression that this is
also for synchronization with outstanding asynchronous processsing.
I'm pretty massively opposed to this because:
- It boils down reinventing a new rather general-purpose userspace
synchronization interface. If we look at things like futexes, this
is hard to get right.
- Furthermore a lot of kernel code has to interact with this
synchronization primitive. This smells a look like the dri1 hw_lock,
a horror show I prefer not to reinvent.
- Even more fun is that multiple different subsystems would interact
here, so we have plenty of opportunities to create funny deadlock
scenarios.
I think synchronization is a wholesale different problem from data
sharing and should be tackled as an orthogonal problem.
Now we could demand that prepare/finish may only ensure cache
coherency (as Rob intended), but that runs up into the next problem:
We not only need mmap support to facilitate sw-only processing nodes
in a pipeline (without jumping through hoops by importing the dma_buf
into some sw-access only importer), which allows for a nicer
ION->dma-buf upgrade path for existing Android userspace. We also need
mmap support for existing importing subsystems to support existing
userspace libraries. And a loot of these subsystems are expected to
export coherent userspace mappings.
So prepare/finish can only ever be optional and the exporter /needs/
to support coherent mappings. Given that mmap access is always
somewhat fallback-y in nature I've decided to drop this optimization,
instead of just making it optional. If we demonstrate a clear need for
this, supported by benchmark results, we can always add it in again
later as an optional extension.
Other differences compared to Rob's RFC is the above mentioned support
for mapping a dma-buf through facilities provided by the importer.
Which results in mmap support no longer being optional.
Note that this dma-buf mmap patch does _not_ support every possible
insanity an existing subsystem could pull of with mmap: Because it
does not allow to intercept pagefaults and shoot down ptes importing
subsystems can't add some magic of their own at these points (e.g. to
automatically synchronize with outstanding rendering or set up some
special resources). I've done a cursory read through a few mmap
implementions of various subsytems and I'm hopeful that we can avoid
this (and the complexity it'd bring with it).
Additonally I've extended the documentation a bit to explain the hows
and whys of this mmap extension.
In case we ever want to add support for explicitly cache maneged
userspace mmap with a prepare/finish ioctl pair, we could specify that
userspace needs to mmap a different part of the dma_buf, e.g. the
range starting at dma_buf->size up to dma_buf->size*2. This works
because the size of a dma_buf is invariant over it's lifetime. The
exporter would obviously need to fall back to coherent mappings for
both ranges if a legacy clients maps the coherent range and the
architecture cannot suppor conflicting caching policies. Also, this
would obviously be optional and userspace needs to be able to fall
back to coherent mappings.
v2:
- Spelling fixes from Rob Clark.
- Compile fix for !DMA_BUF from Rob Clark.
- Extend commit message to explain how explicitly cache managed mmap
support could be added later.
- Extend the documentation with implementations notes for exporters
that need to manually fake coherency.
v3:
- dma_buf pointer initialization goof-up noticed by Rebecca Schultz
Zavin.
Cc: Rob Clark <rob.clark@linaro.org>
Cc: Rebecca Schultz Zavin <rebecca@android.com>
Acked-by: Rob Clark <rob.clark@linaro.org>
Signed-Off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Diffstat (limited to 'drivers/base')
-rw-r--r-- | drivers/base/dma-buf.c | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/drivers/base/dma-buf.c b/drivers/base/dma-buf.c index 07cbbc6fddb4..7cfb405b1ad5 100644 --- a/drivers/base/dma-buf.c +++ b/drivers/base/dma-buf.c | |||
@@ -44,8 +44,26 @@ static int dma_buf_release(struct inode *inode, struct file *file) | |||
44 | return 0; | 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 | static const struct file_operations dma_buf_fops = { | 64 | static const struct file_operations dma_buf_fops = { |
48 | .release = dma_buf_release, | 65 | .release = dma_buf_release, |
66 | .mmap = dma_buf_mmap_internal, | ||
49 | }; | 67 | }; |
50 | 68 | ||
51 | /* | 69 | /* |
@@ -82,7 +100,8 @@ struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops, | |||
82 | || !ops->unmap_dma_buf | 100 | || !ops->unmap_dma_buf |
83 | || !ops->release | 101 | || !ops->release |
84 | || !ops->kmap_atomic | 102 | || !ops->kmap_atomic |
85 | || !ops->kmap)) { | 103 | || !ops->kmap |
104 | || !ops->mmap)) { | ||
86 | return ERR_PTR(-EINVAL); | 105 | return ERR_PTR(-EINVAL); |
87 | } | 106 | } |
88 | 107 | ||
@@ -406,3 +425,46 @@ void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num, | |||
406 | dmabuf->ops->kunmap(dmabuf, page_num, vaddr); | 425 | dmabuf->ops->kunmap(dmabuf, page_num, vaddr); |
407 | } | 426 | } |
408 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); | 427 | EXPORT_SYMBOL_GPL(dma_buf_kunmap); |
428 | |||
429 | |||
430 | /** | ||
431 | * dma_buf_mmap - Setup up a userspace mmap with the given vma | ||
432 | * @dma_buf: [in] buffer that should back the vma | ||
433 | * @vma: [in] vma for the mmap | ||
434 | * @pgoff: [in] offset in pages where this mmap should start within the | ||
435 | * dma-buf buffer. | ||
436 | * | ||
437 | * This function adjusts the passed in vma so that it points at the file of the | ||
438 | * dma_buf operation. It alsog adjusts the starting pgoff and does bounds | ||
439 | * checking on the size of the vma. Then it calls the exporters mmap function to | ||
440 | * set up the mapping. | ||
441 | * | ||
442 | * Can return negative error values, returns 0 on success. | ||
443 | */ | ||
444 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, | ||
445 | unsigned long pgoff) | ||
446 | { | ||
447 | if (WARN_ON(!dmabuf || !vma)) | ||
448 | return -EINVAL; | ||
449 | |||
450 | /* check for offset overflow */ | ||
451 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff) | ||
452 | return -EOVERFLOW; | ||
453 | |||
454 | /* check for overflowing the buffer's size */ | ||
455 | if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) > | ||
456 | dmabuf->size >> PAGE_SHIFT) | ||
457 | return -EINVAL; | ||
458 | |||
459 | /* readjust the vma */ | ||
460 | if (vma->vm_file) | ||
461 | fput(vma->vm_file); | ||
462 | |||
463 | vma->vm_file = dmabuf->file; | ||
464 | get_file(vma->vm_file); | ||
465 | |||
466 | vma->vm_pgoff = pgoff; | ||
467 | |||
468 | return dmabuf->ops->mmap(dmabuf, vma); | ||
469 | } | ||
470 | EXPORT_SYMBOL_GPL(dma_buf_mmap); | ||