With this add a new attribute that can be passsed to dma-mapping IOMMU apis
to differentiate between kernel and user allcoations.
diff --git a/include/linux/dma-attrs.h b/include/linux/dma-attrs.h
index 547ab56..861df09 100644
--- a/include/linux/dma-attrs.h
+++ b/include/linux/dma-attrs.h
@@ -15,6 +15,7 @@ enum dma_attr {
DMA_ATTR_WEAK_ORDERING,
DMA_ATTR_WRITE_COMBINE,
DMA_ATTR_NON_CONSISTENT,
+ DMA_ATTR_USER_SPACE,
DMA_ATTR_MAX,
};
Hello,
This is a request for comments on dma-mapping patches for ARM. I
did some additions for issue related to kernel virtual memory allocations
in the iommu ops defined in dma-mapping framework.
The patches are based on:
git://git.linaro.org/people/mszyprowski/linux-dma-mapping.git3.4-rc3-arm-dm…
The code has been tested on Samsung Exynos5 SMDK5250.
These patches do the following:
1. Define a new dma attribute to identify user space allocation.
2. Add new wrapper functions to pass the dma attribute defined in (1)
above, as in the current framework there is no way to pass the new
attribute which can be used to differentiate between kernel and user
allocations.
3. Extend the existing arm_dma_ops for iommu enabled devices to
differentiate between kernel and user space allocations.
Patch summary:
[PATCH 1/3]:
Common: add DMA_ATTR_USER_SPACE to dma-attr. This can be passed to
arm_dma_ops to identify the type of allocation which can be either from
kernel or from user.
[PATCH 2/3]:
ARM: add "struct page_infodma" to hold information for allocated pages.
This can be attached to any of the devices which is making use of
dma-mapping APIs. Any interested device should allocate this structure and
store all the relevant information about the allocated pages to be able to
do a look up for all future references.
ARM: add dma_alloc_writecombine_user() function to pass DMA_ATTR_USER_SPACE
attribute
ARM: add dma_free_writecombine_user() function to pass DMA_ATTR_USER_SPACE
attribute
ARM: add dma_mmap_writecombine_user() function to pass DMA_ATTR_USER_SPACE
attribute
[PATCH 3/3]:
ARM: add check for allocation type in __dma_alloc_remap() function
ARM: add check for allocation type in arm_iommu_alloc_attrs() function
ARM: add check for allocation type in arm_iommu_mmap_attrs() function
ARM: re-used dma_addr as a flag to check for memory allocation type. It was
an unused argument and the prototype does not pass dma-attrs, so used this
as a means to pass the flag.
ARM: add check for allocation type in arm_iommu_free_attrs() function
arch/arm/include/asm/dma-mapping.h | 31 +++++++
arch/arm/mm/dma-mapping.c | 168
++++++++++++++++++++++++++----------
include/linux/dma-attrs.h | 1 +
3 files changed, 155 insertions(+), 45 deletions(-)
Hello everyone,
This patchset adds support for DMABUF [2] importing to V4L2 stack.
The support for DMABUF exporting was moved to separate patchset
due to dependency on patches for DMA mapping redesign by
Marek Szyprowski [4].
v5:
- removed change of importer/exporter behaviour
- fixes vb2_dc_pages_to_sgt basing on Laurent's hints
- changed pin/unpin words to lock/unlock in Doc
v4:
- rebased on mainline 3.4-rc2
- included missing importing support for s5p-fimc and s5p-tv
- added patch for changing map/unmap for importers
- fixes to Documentation part
- coding style fixes
- pairing {map/unmap}_dmabuf in vb2-core
- fixing variable types and semantic of arguments in videobufb2-dma-contig.c
v3:
- rebased on mainline 3.4-rc1
- split 'code refactor' patch to multiple smaller patches
- squashed fixes to Sumit's patches
- patchset is no longer dependant on 'DMA mapping redesign'
- separated path for handling IO and non-IO mappings
- add documentation for DMABUF importing to V4L
- removed all DMABUF exporter related code
- removed usage of dma_get_pages extension
v2:
- extended VIDIOC_EXPBUF argument from integer memoffset to struct
v4l2_exportbuffer
- added patch that breaks DMABUF spec on (un)map_atachment callcacks but allows
to work with existing implementation of DMABUF prime in DRM
- all dma-contig code refactoring patches were squashed
- bugfixes
v1: List of changes since [1].
- support for DMA api extension dma_get_pages, the function is used to retrieve
pages used to create DMA mapping.
- small fixes/code cleanup to videobuf2
- added prepare and finish callbacks to vb2 allocators, it is used keep
consistency between dma-cpu acess to the memory (by Marek Szyprowski)
- support for exporting of DMABUF buffer in V4L2 and Videobuf2, originated from
[3].
- support for dma-buf exporting in vb2-dma-contig allocator
- support for DMABUF for s5p-tv and s5p-fimc (capture interface) drivers,
originated from [3]
- changed handling for userptr buffers (by Marek Szyprowski, Andrzej
Pietrasiewicz)
- let mmap method to use dma_mmap_writecombine call (by Marek Szyprowski)
[1] http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/4296…
[2] https://lkml.org/lkml/2011/12/26/29
[3] http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/3635…
[4] http://thread.gmane.org/gmane.linux.kernel.cross-arch/12819
Andrzej Pietrasiewicz (1):
v4l: vb2-dma-contig: add support for scatterlist in userptr mode
Laurent Pinchart (2):
v4l: vb2-dma-contig: Shorten vb2_dma_contig prefix to vb2_dc
v4l: vb2-dma-contig: Reorder functions
Marek Szyprowski (2):
v4l: vb2: add prepare/finish callbacks to allocators
v4l: vb2-dma-contig: add prepare/finish to dma-contig allocator
Sumit Semwal (4):
v4l: Add DMABUF as a memory type
v4l: vb2: add support for shared buffer (dma_buf)
v4l: vb: remove warnings about MEMORY_DMABUF
v4l: vb2-dma-contig: add support for dma_buf importing
Tomasz Stanislawski (4):
Documentation: media: description of DMABUF importing in V4L2
v4l: vb2-dma-contig: Remove unneeded allocation context structure
v4l: s5p-tv: mixer: support for dmabuf importing
v4l: s5p-fimc: support for dmabuf importing
Documentation/DocBook/media/v4l/compat.xml | 4 +
Documentation/DocBook/media/v4l/io.xml | 179 +++++++
.../DocBook/media/v4l/vidioc-create-bufs.xml | 1 +
Documentation/DocBook/media/v4l/vidioc-qbuf.xml | 15 +
Documentation/DocBook/media/v4l/vidioc-reqbufs.xml | 47 +-
drivers/media/video/Kconfig | 1 +
drivers/media/video/s5p-fimc/fimc-capture.c | 2 +-
drivers/media/video/s5p-tv/Kconfig | 1 +
drivers/media/video/s5p-tv/mixer_video.c | 2 +-
drivers/media/video/videobuf-core.c | 4 +
drivers/media/video/videobuf2-core.c | 207 ++++++++-
drivers/media/video/videobuf2-dma-contig.c | 529 +++++++++++++++++---
include/linux/videodev2.h | 7 +
include/media/videobuf2-core.h | 34 ++
14 files changed, 932 insertions(+), 101 deletions(-)
--
1.7.5.4
Hey there,
We ran a call today on the topics of V4L2/dmabuf integration and the
DMA mapping changes; I've included minutes and actions below.
In summary:
- We're not going to see the V4L2 patches in 3.4; they haven't had
enough review or testing and Mauro is very busy this merge window.
- We're going to put in effort to validate the work on Exynos4 and
OMAP4 using test applications that Rob and Tomasz are maintaining.
- Sumit's maintaining an up-to-date branch containing all the
in-flight DRM/V4L2/dmabuf-dependent work that we can carry
meanwhile:
http://git.linaro.org/gitweb?p=people/sumitsemwal/linux-3.x.git;a=shortlog;…
Laurent, I missed one thing that you mentioned in the call and it's
included in an XXX: below; if you could note what it was it would be
great.
Please feel free to correct my possibly wild misunderstandings in the
minutes and summary above. Thanks!
Attendees are all on the To: line.
Tomasz: At the moment, have PoC support for import/export dma-buf
for V4L2
Modified patches by Sumit
Supporting importer of dma-buf
Exynos DRM and drm-prime
Test application worked fine for V4L capture and DRM output
Test application between two V4L devices
Laurent has sent in review comments
Tomasz: proposed extension to DMA Mapping -- dma_get_pages
Currently difficult to change the camera address into list of pages
DMA framework has the knowledge of this list and could do this
Depends on dma_get_pages
Needs to be merged first
Test application posted to dri-devel with dependencies to run demo
Many dependencies
Topic: dmabuf importer from V4L2
For sharing with DRM do not need exporter
Need some patches added to DRM
prime
prime for Exynos
drm-prime
ACTION: Tomasz to check with Inki Dae and David A. on
status
Will send request to Mauro?
Doesn't think so, won't have enough time for testing
RC is already open -- not enough time for that
API details needs consideration
Depends on how much time current project
2-3 weeks after the merge window closes
1 month to actually review
Would like to see Exynos working
Mauro has a Samsung V310/Exynos4 dev board
Also wants to see tested with V4L driver
virtual drivers also supporting dma-buf
wants both importer and exporter?
ACTION: Sumit to look into VV driver used for v4l2 testing
needs importer and exporter
Laurent has work that he can push forward in parallel
API change ioctl addition that could be reviewed
Rob: the demo had V4L importer and DRM exporter (?)
There are some changes for prime changes
Laurent: needs to implement importer/exporter camera piece
With drm-prime-dmabuf patches drivers can be simplified
Sumit: we have a new 3.3-WIP branch
Rob could use that as a base and put DRM updates on it
ACTION: Rob to pull in changes and update demonstration
Rob: what camera does Mauro have?
On the Samsung SDK, M5MOLS
Mauro does have a Pandaboard with ov5650 camera but
needs setting up and potential hardware mod
ACTION: Mauro to take picture of setup for Sumit
As a backup Rob could add a Tested-By for the changes
Mauro essentially wants a test on
With CPU access in dmabuf can use vmalloc
Without dependency on dma mapping at all
Without Russell's acceptance can't go forward with ARM-related
pieces
ACTION: Kiko to check with Nicolas on this piece, see if he
can review or help Marek with the ARM-related pieces
Tomasz could use the 3.3-WIP branch
Sumit: rebasing not a good idea, but could pull in for-next
Suggests Tomasz bases work on Linus' mainline
Had problems with Tomasz' branch that is based on -next
His branch includes Tomasz RFCv2 patches as well
Laurent: agree with Sumit
ACTION: Sumit to email Tomasz and CC: Inki Dae
ACTION: send around latest drm-prime branch
Tomasz: so no XXX: exporter support for V4L?
Laurent: doesn't have time to update XXX at the moment
Needs porting to videobuf2
Rob: Looks like Daniel V. has also replied to the V4L patches so
it's going to be hard for 3.4
Mauro's lack of time makes 3.4 not possible anyway
mmap support also likely to miss 3.4
EGLImage extension needs sorting out
Can be carried in our WIP branch meanwhile
Mauro: what are the test applications being used for dmabuf?
Rob: using Panda with camera adapter board (omap-drm)
Would like others to have similar setups
Requires YUV sensor, but there are patches that support the
RAW YUV conversion which allows other sensors to be used
Mauro: but what software app are you using?
Rob: has test code in github:
https://github.com/robclark/omapdrmtest
Tomasz:
Have posted one test application on dri-devel (March 6th)
http://www.spinics.net/lists/dri-devel/msg19634.html
Second application posted to linux-media
As a reply to RFCv1 dma-buf V4L2 patches (Jan 26th)
http://www.mail-archive.com/linux-media@vger.kernel.org/msg42522.html
Mauro: include both applications together with patches when
posting
Sumit: could ask Inki to provide similar exynos-drm test application
in parallel to Rob's omap-drm; same interface will simplify
testing
Marek:
device-to-device coordinate will be needed
Will start next week ideally
For a device-to-device operation, need this to avoid touching
CPU cache
Not sure about ARM stuff for 3.4; need review
If they don't go in, will keep on dma-mapping-next
Konrad's comments are addressed and Reviewed-bys added
Take care,
--
Christian Robottom Reis, Engineering VP
Brazil (GMT-3) | [+55] 16 9112 6430 | [+1] 612 216 4935
Linaro.org: Open Source Software for ARM SoCs
Hi Nishanth,
On Sun, 29 Apr 2012 21:56:26 +0200
Nishanth Peethambaran <nishanth.peethu(a)gmail.com> wrote:
> Hi Rebecca, Doyu,
>
> I am sending this mail related to the mailchain seen in the link:
> http://lists.linaro.org/pipermail/linaro-mm-sig/2012-January/001009.html
> I am not sure where to post the query. If I am supposed to post in a
> mail-list, let me know and
> I shall post it there.
I think that "linaro-mm-sig(a)lists.linaro.org" would be the best for
further discussion, putting linaro-mm-sig on Cc:.
> I am also trying to use ION for IOMMU and have some comments on ION in
> general and specific to IOMMU usage.
> 1. Isn't 2 heap types sufficient - physically contiguous and non-contiguous?
> We could treat physical carveout and sytem_contig(kmalloc) as two
> implementations of physcially contiguous type
> and add the heaps with separate ids. Order of using them would be
> as ION currently does - order of registration of heap.
> IOMMU heap type also won't be needed as that is just a different
> implementation of system heap (no kernel map at allocation
> time). Please correct me if I am missing something.
>
> 2. The user space interface does not support exposing a dma_addr_t to
> user space. We could add this using custom ioctl.
> But, shouldn't this be a standard interface. For example, to
> protect/hide the graphics core IP details, platfrom provider would
> prefer to generate graphics core program from user space (license
> requirements) and only pass the final buffer containing
> the program to the driver. If an IOMMU is used, the dma/iommu
> address mapping would be needed in user space to embed
> few pointers in the graphics program.
>
> 3. With the current ION implementation, how do we keep user space
> code/drivers independent of platform supporting IOMMU or not?
> For example, CMA is supported by the platform but want to restrict
> buffers allocated from CMA only if necessary (to avoid migration
> overheads). How does a client decide whether to request for
> physically contiguous buffers (CMA) ot IOMMU buffers (buddy allocator)
> In one platform, the buffer could be shared with an IP with IOMMU
> support (eg: camera and codec both supporting IOMMU) - CMA not needed.
> In another platform the buffer may have to be shared with an IP
> which works on physical address space. buffer has to be
> shared with an IP which does not support IOMMU(to avoid migrations)
> - we need to use physically contiguous heap(CMA).
> I see dma buf sharing API has a callback to exporter at attachment
> time so that exporter can pass of fail the sharing based on the
> receiver.
> Don't we need something similar and more?
>
> ION version referred:
> commit 3fe24366a40147d7c776e1f291193fd3b61f217d
> Author: Iliyan Malchev <malchev(a)google.com>
> Date: Tue Aug 9 14:42:08 2011 -0700
>
> - Nishanth Peethambaran
Hello everyone,
This patchset adds support for DMABUF [2] importing to V4L2 stack.
The support for DMABUF exporting was moved to separate patchset
due to dependency on patches for DMA mapping redesign by
Marek Szyprowski [4].
v5:
- removed change of importer/exporter behaviour
- fixes vb2_dc_pages_to_sgt basing on Laurent's hints
- changed pin/unpin words to lock/unlock in Doc
v4:
- rebased on mainline 3.4-rc2
- included missing importing support for s5p-fimc and s5p-tv
- added patch for changing map/unmap for importers
- fixes to Documentation part
- coding style fixes
- pairing {map/unmap}_dmabuf in vb2-core
- fixing variable types and semantic of arguments in videobufb2-dma-contig.c
v3:
- rebased on mainline 3.4-rc1
- split 'code refactor' patch to multiple smaller patches
- squashed fixes to Sumit's patches
- patchset is no longer dependant on 'DMA mapping redesign'
- separated path for handling IO and non-IO mappings
- add documentation for DMABUF importing to V4L
- removed all DMABUF exporter related code
- removed usage of dma_get_pages extension
v2:
- extended VIDIOC_EXPBUF argument from integer memoffset to struct
v4l2_exportbuffer
- added patch that breaks DMABUF spec on (un)map_atachment callcacks but allows
to work with existing implementation of DMABUF prime in DRM
- all dma-contig code refactoring patches were squashed
- bugfixes
v1: List of changes since [1].
- support for DMA api extension dma_get_pages, the function is used to retrieve
pages used to create DMA mapping.
- small fixes/code cleanup to videobuf2
- added prepare and finish callbacks to vb2 allocators, it is used keep
consistency between dma-cpu acess to the memory (by Marek Szyprowski)
- support for exporting of DMABUF buffer in V4L2 and Videobuf2, originated from
[3].
- support for dma-buf exporting in vb2-dma-contig allocator
- support for DMABUF for s5p-tv and s5p-fimc (capture interface) drivers,
originated from [3]
- changed handling for userptr buffers (by Marek Szyprowski, Andrzej
Pietrasiewicz)
- let mmap method to use dma_mmap_writecombine call (by Marek Szyprowski)
[1] http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/4296…
[2] https://lkml.org/lkml/2011/12/26/29
[3] http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/3635…
[4] http://thread.gmane.org/gmane.linux.kernel.cross-arch/12819
Andrzej Pietrasiewicz (1):
v4l: vb2-dma-contig: add support for scatterlist in userptr mode
Laurent Pinchart (2):
v4l: vb2-dma-contig: Shorten vb2_dma_contig prefix to vb2_dc
v4l: vb2-dma-contig: Reorder functions
Marek Szyprowski (2):
v4l: vb2: add prepare/finish callbacks to allocators
v4l: vb2-dma-contig: add prepare/finish to dma-contig allocator
Sumit Semwal (4):
v4l: Add DMABUF as a memory type
v4l: vb2: add support for shared buffer (dma_buf)
v4l: vb: remove warnings about MEMORY_DMABUF
v4l: vb2-dma-contig: add support for dma_buf importing
Tomasz Stanislawski (4):
Documentation: media: description of DMABUF importing in V4L2
v4l: vb2-dma-contig: Remove unneeded allocation context structure
v4l: s5p-tv: mixer: support for dmabuf importing
v4l: s5p-fimc: support for dmabuf importing
Documentation/DocBook/media/v4l/compat.xml | 4 +
Documentation/DocBook/media/v4l/io.xml | 179 +++++++
.../DocBook/media/v4l/vidioc-create-bufs.xml | 1 +
Documentation/DocBook/media/v4l/vidioc-qbuf.xml | 15 +
Documentation/DocBook/media/v4l/vidioc-reqbufs.xml | 47 +-
drivers/media/video/Kconfig | 1 +
drivers/media/video/s5p-fimc/fimc-capture.c | 2 +-
drivers/media/video/s5p-tv/Kconfig | 1 +
drivers/media/video/s5p-tv/mixer_video.c | 2 +-
drivers/media/video/videobuf-core.c | 4 +
drivers/media/video/videobuf2-core.c | 207 ++++++++-
drivers/media/video/videobuf2-dma-contig.c | 529 +++++++++++++++++---
include/linux/videodev2.h | 7 +
include/media/videobuf2-core.h | 34 ++
14 files changed, 932 insertions(+), 101 deletions(-)
--
1.7.5.4
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.
Cc: Rob Clark <rob.clark(a)linaro.org>
Cc: Rebecca Schultz Zavin <rebecca(a)android.com>
Signed-Off-by: Daniel Vetter <daniel.vetter(a)ffwll.ch>
---
Documentation/dma-buf-sharing.txt | 98 ++++++++++++++++++++++++++++++++++---
drivers/base/dma-buf.c | 64 +++++++++++++++++++++++-
include/linux/dma-buf.h | 16 ++++++
3 files changed, 170 insertions(+), 8 deletions(-)
diff --git a/Documentation/dma-buf-sharing.txt b/Documentation/dma-buf-sharing.txt
index 3bbd5c5..5ff4d2b 100644
--- a/Documentation/dma-buf-sharing.txt
+++ b/Documentation/dma-buf-sharing.txt
@@ -29,13 +29,6 @@ The buffer-user
in memory, mapped into its own address space, so it can access the same area
of memory.
-*IMPORTANT*: [see https://lkml.org/lkml/2011/12/20/211 for more details]
-For this first version, A buffer shared using the dma_buf sharing API:
-- *may* be exported to user space using "mmap" *ONLY* by exporter, outside of
- this framework.
-- with this new iteration of the dma-buf api cpu access from the kernel has been
- enable, see below for the details.
-
dma-buf operations for device dma only
--------------------------------------
@@ -313,6 +306,83 @@ Access to a dma_buf from the kernel context involves three steps:
enum dma_data_direction dir);
+Direct Userspace Access/mmap Support
+------------------------------------
+
+Being able to mmap an export dma-buf buffer object has 2 main use-cases:
+- CPU fallback processing in a pipeline and
+- supporting existing mmap interfaces in importers.
+
+1. CPU fallback processing in a pipeline
+
+ In many processing pipelines it is sometimes required that the cpu can access
+ the data in a dma-buf (e.g. for thumbnail creation, snapshots, ...). To avoid
+ the need to handle this specially in userspace frameworks for buffer sharing
+ it's ideal if the dma_buf fd itself can be used to access the backing storage
+ from userspace using mmap.
+
+ Furthermore Android's ION framework already supports this (and is otherwise
+ rather similar to dma-buf from a userspace consumer side with using fds as
+ handles, too). So it's beneficial to support this in a similar fashion on
+ dma-buf to have a good transition path for existing Android userspace.
+
+ No special interfaces, userspace simply calls mmap on the dma-buf fd.
+
+2. Supporting existing mmap interfaces in exporters
+
+ Similar to the motivation for kernel cpu access it is again important that
+ the userspace code of a given importing subsystem can use the same interfaces
+ with a imported dma-buf buffer object as with a native buffer object. This is
+ especially important for drm where the userspace part of contemporary OpenGL,
+ X, and other drivers is huge, and reworking them to use a different way to
+ mmap a buffer rather invasive.
+
+ The assumption in the current dma-buf interfaces is that redirecting the
+ initial mmap is all that's needed. A survey of some of the existing
+ subsystems shows that no driver seems to do any nefarious thing like syncing
+ up with outstanding asynchronous processing on the device or allocating
+ special resources at fault time. So hopefully this is good enough, since
+ adding interfaces to intercept pagefaults and allow pte shootdowns would
+ increase the complexity quite a bit.
+
+ Interface:
+ int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
+ unsigned long);
+
+ If the importing subsystem simply provides a special-purpose mmap call to set
+ up a mapping in userspace, calling do_mmap with dma_buf->file will equally
+ achieve that for a dma-buf object.
+
+3. Implementation notes for exporters
+
+ Because dma-buf buffers have invariant size over their lifetime, the dma-buf
+ core checks whether a vma is too large and rejects such mappings. The
+ exporter hence does not need to duplicate this check.
+
+ Because existing importing subsystems might presume coherent mappings for
+ userspace, the exporter needs to set up a coherent mapping. If that's not
+ possible, it needs to fake coherency by manually shooting down ptes when
+ leaving the cpu domain and flushing caches at fault time. Note that all the
+ dma_buf files share the same anon inode, hence the exporter needs to replace
+ the dma_buf file stored in vma->vm_file with it's own if pte shootdown is
+ requred. This is because the kernel uses the underlying inode's address_space
+ for vma tracking (and hence pte tracking at shootdown time with
+ unmap_mapping_range).
+
+ If the above shootdown dance turns out to be too expensive in certain
+ scenarios, we can extend dma-buf with a more explicit cache tracking scheme
+ for userspace mappings. But the current assumption is that using mmap is
+ always a slower path, so some inefficiencies should be acceptable.
+
+ Exporters that shoot down mappings (for any reasons) shall not do any
+ synchronization at fault time with outstanding device operations.
+ Synchronization is an orthogonal issue to sharing the backing storage of a
+ buffer and hence should not be handled by dma-buf itself. This is explictly
+ mentioned here because many people seem to want something like this, but if
+ different exporters handle this differently, buffer sharing can fail in
+ interesting ways depending upong the exporter (if userspace starts depending
+ upon this implicit synchronization).
+
Miscellaneous notes
-------------------
@@ -336,6 +406,20 @@ Miscellaneous notes
the exporting driver to create a dmabuf fd must provide a way to let
userspace control setting of O_CLOEXEC flag passed in to dma_buf_fd().
+- If an exporter needs to manually flush caches and hence needs to fake
+ coherency for mmap support, it needs to be able to zap all the ptes pointing
+ at the backing storage. Now linux mm needs a struct address_space associated
+ with the struct file stored in vma->vm_file to do that with the function
+ unmap_mapping_range. But the dma_buf framework only backs every dma_buf fd
+ with the anon_file struct file, i.e. all dma_bufs share the same file.
+
+ Hence exporters need to setup their own file (and address_space) association
+ by setting vma->vm_file and adjusting vma->vm_pgoff in the dma_buf mmap
+ callback. In the specific case of a gem driver the exporter could use the
+ shmem file already provided by gem (and set vm_pgoff = 0). Exporters can then
+ zap ptes by unmapping the corresponding range of the struct address_space
+ associated with their own file.
+
References:
[1] struct dma_buf_ops in include/linux/dma-buf.h
[2] All interfaces mentioned above defined in include/linux/dma-buf.h
diff --git a/drivers/base/dma-buf.c b/drivers/base/dma-buf.c
index 07cbbc6..8a573b9 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)
return 0;
}
+static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
+{
+ struct dma_buf *dmabuf;
+
+ if (!is_dma_buf_file(file))
+ return -EINVAL;
+
+ /* check for overflowing the buffer's size */
+ if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
+ dmabuf->size >> PAGE_SHIFT)
+ return -EINVAL;
+
+ dmabuf = file->private_data;
+
+ return dmabuf->ops->mmap(dmabuf, vma);
+}
+
static const struct file_operations dma_buf_fops = {
.release = dma_buf_release,
+ .mmap = dma_buf_mmap_internal,
};
/*
@@ -82,7 +100,8 @@ struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
|| !ops->unmap_dma_buf
|| !ops->release
|| !ops->kmap_atomic
- || !ops->kmap)) {
+ || !ops->kmap
+ || !ops->mmap)) {
return ERR_PTR(-EINVAL);
}
@@ -406,3 +425,46 @@ void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
}
EXPORT_SYMBOL_GPL(dma_buf_kunmap);
+
+
+/**
+ * dma_buf_mmap - Setup up a userspace mmap with the given vma
+ * @dma_buf: [in] buffer that should back the vma
+ * @vma: [in] vma for the mmap
+ * @pgoff: [in] offset in pages where this mmap should start within the
+ * dma-buf buffer.
+ *
+ * This function adjusts the passed in vma so that it points at the file of the
+ * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
+ * checking on the size of the vma. Then it calls the exporters mmap function to
+ * set up the mapping.
+ *
+ * Can return negative error values, returns 0 on success.
+ */
+int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
+ unsigned long pgoff)
+{
+ if (WARN_ON(!dmabuf || !vma))
+ return -EINVAL;
+
+ /* check for offset overflow */
+ if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
+ return -EOVERFLOW;
+
+ /* check for overflowing the buffer's size */
+ if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
+ dmabuf->size >> PAGE_SHIFT)
+ return -EINVAL;
+
+ /* readjust the vma */
+ if (vma->vm_file)
+ fput(vma->vm_file);
+
+ vma->vm_file = dmabuf->file;
+ get_file(vma->vm_file);
+
+ vma->vm_pgoff = pgoff;
+
+ return dmabuf->ops->mmap(dmabuf, vma);
+}
+EXPORT_SYMBOL_GPL(dma_buf_mmap);
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 3efbfc2..1f78d15 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -61,6 +61,10 @@ struct dma_buf_attachment;
* This Callback must not sleep.
* @kmap: maps a page from the buffer into kernel address space.
* @kunmap: [optional] unmaps a page from the buffer.
+ * @mmap: used to expose the backing storage to userspace. Note that the
+ * mapping needs to be coherent - if the exporter doesn't directly
+ * support this, it needs to fake coherency by shooting down any ptes
+ * when transitioning away from the cpu domain.
*/
struct dma_buf_ops {
int (*attach)(struct dma_buf *, struct device *,
@@ -92,6 +96,8 @@ struct dma_buf_ops {
void (*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
void *(*kmap)(struct dma_buf *, unsigned long);
void (*kunmap)(struct dma_buf *, unsigned long, void *);
+
+ int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
};
/**
@@ -167,6 +173,9 @@ void *dma_buf_kmap_atomic(struct dma_buf *, unsigned long);
void dma_buf_kunmap_atomic(struct dma_buf *, unsigned long, void *);
void *dma_buf_kmap(struct dma_buf *, unsigned long);
void dma_buf_kunmap(struct dma_buf *, unsigned long, void *);
+
+int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
+ unsigned long);
#else
static inline struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
@@ -248,6 +257,13 @@ static inline void dma_buf_kunmap(struct dma_buf *dmabuf,
unsigned long pnum, void *vaddr)
{
}
+
+static inline int dma_buf_mmap(struct dma_buf *dmabuf,
+ struct vm_area_struct *vma,
+ unsigned long pgoff)
+{
+ return -ENODEV;
+}
#endif /* CONFIG_DMA_SHARED_BUFFER */
#endif /* __DMA_BUF_H__ */
--
1.7.10
Hi!
Recent changes to ioremap and unification of vmalloc regions on ARM
significantly reduces the possible size of the consistent dma region and
limited allowed dma coherent/writecombine allocations.
This experimental patch series replaces custom consistent dma regions
usage in dma-mapping framework in favour of generic vmalloc areas
created on demand for each coherent and writecombine allocations.
This patch is based on vanilla v3.4-rc2 release.
Best regards
Marek Szyprowski
Samsung Poland R&D Center
Patch summary:
Marek Szyprowski (4):
mm: vmalloc: use const void * for caller argument
mm: vmalloc: export find_vm_area() function
mm: vmalloc: add VM_DMA flag to indicate areas used by dma-mapping
framework
ARM: remove consistent dma region and use common vmalloc range for
dma allocations
arch/arm/include/asm/dma-mapping.h | 2 +-
arch/arm/mm/dma-mapping.c | 220 +++++++-----------------------------
include/linux/vmalloc.h | 10 +-
mm/vmalloc.c | 31 ++++--
4 files changed, 67 insertions(+), 196 deletions(-)
--
1.7.1.569.g6f426