Skip to contentSkip to conversation

drm: prime: Allow exported dma-bufs to be mapped

PATCH RESEND Applied Daniel Thompson 3 participants 7 messages Jun 19, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover drm: prime: Allow exported dma-bufs to be mapped

This patch set started out as a single patch with a trivial bit of boilerplate to add dmabuf mmap support to the msm driver. However Rob Clark pointed out that, rather than keep one of the tricks I had used, it would be better to change the helpers resulting in this series.

I've tested this both with a rather hacked about Android userspace and with a fairly small test case run from debian. Both bits of code currently use dumb buffers.

Thanks to Benjamin Gaignard for his help in finding this bit of code and to Damien Hobson-Garcia for pointing out that I'd forgotten (since 3.18) to RESEND these patches.

Dave: I guess its probably too late in the dev. cycle to take this code
but don't worry, I will try really hard to remember to RESEND it for 4.2. ;-)

v2:

* Modified DRM_PRIME_HANDLE_TO_FD to honour the O_RDRW from the user and removed code to workaround this from the sti driver (Rob Clark).

* Added a patch to (rather spartanly) document gem_prime_mmap. Only tacked into this series 'cos I spotted it was missing when I was checking whether I needed to describe DRM_RDRW anywhere.

Daniel Thompson (2):
drm: prime: Honour O_RDWR during prime-handle-to-fd
drm: prime: Document gem_prime_mmap

 drivers/gpu/drm/drm_prime.c | 13 ++++++-------
 include/uapi/drm/drm.h      |  1 +
 2 files changed, 7 insertions(+), 7 deletions(-)

--
2.4.3

#1 lore raw

PATCH RESEND v2 1/2 drm: prime: Honour O_RDWR during prime-handle-to-fd

Currently DRM_IOCTL_PRIME_HANDLE_TO_FD rejects all flags except (DRM|O)_CLOEXEC making it difficult (maybe impossible) for userspace to mmap() the resulting dma-buf even when this is supported by the DRM driver.

It is trivial to relax the restriction and permit read/write access. This is safe because the flags are seldom touched by drm; mostly they are passed verbatim to dma_buf calls.

---
 drivers/gpu/drm/drm_prime.c | 9 +++------
 include/uapi/drm/drm.h      | 1 +
 2 files changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_prime.c +3 -6
index 7fec191b45f7..6d2cf4fb4038 100644
@@ -331,7 +331,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  * drm_gem_prime_export - helper library implementation of the export callback
  * @dev: drm_device to export from
  * @obj: GEM object to export
- * @flags: flags like DRM_CLOEXEC
+ * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  *
  * This is the implementation of the gem_prime_export functions for GEM drivers
  * using the PRIME helpers.
@@ -639,14 +639,11 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
 		return -ENOSYS;
 
 	/* check flags are valid */
-	if (args->flags & ~DRM_CLOEXEC)
+	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
 		return -EINVAL;
 
-	/* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
-	flags = args->flags & DRM_CLOEXEC;
-
 	return dev->driver->prime_handle_to_fd(dev, file_priv,
-			args->handle, flags, &args->fd);
+			args->handle, args->flags, &args->fd);
 }
 
 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
include/uapi/drm/drm.h +1 -0
index ff6ef62d084b..092fe3fa8ec0 100644
@@ -668,6 +668,7 @@ struct drm_set_client_cap {
 	__u64 value;
 };
 
+#define DRM_RDWR O_RDWR
 #define DRM_CLOEXEC O_CLOEXEC
 struct drm_prime_handle {
 	__u32 handle;
Signature · 1 line
2.4.3
#2 lore raw

Re: [RESEND PATCH v2 v4.1-rc8 1/2] drm: prime: Honour O_RDWR during prime-handle-to-fd

Quoted 29 lines · Daniel Thompson

Currently DRM_IOCTL_PRIME_HANDLE_TO_FD rejects all flags except (DRM|O)_CLOEXEC making it difficult (maybe impossible) for userspace to mmap() the resulting dma-buf even when this is supported by the DRM driver.

It is trivial to relax the restriction and permit read/write access. This is safe because the flags are seldom touched by drm; mostly they are passed verbatim to dma_buf calls.

---
 drivers/gpu/drm/drm_prime.c | 9 +++------
 include/uapi/drm/drm.h      | 1 +
 2 files changed, 4 insertions(+), 6 deletions(-)
drivers/gpu/drm/drm_prime.c +1 -1
index 7fec191b45f7..6d2cf4fb4038 100644
@@ -331,7 +331,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  * drm_gem_prime_export - helper library implementation of the export callback
  * @dev: drm_device to export from
  * @obj: GEM object to export
- * @flags: flags like DRM_CLOEXEC
+ * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  *
  * This is the implementation of the gem_prime_export functions for GEM drivers
  * using the PRIME helpers.

drivers/gpu/drm/drm_prime.c int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, · line 643

Quoted 6 lines
drivers/gpu/drm/drm_prime.c +1 -1
@@ -639,14 +639,11 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
 		return -ENOSYS;
 
 	/* check flags are valid */
-	if (args->flags & ~DRM_CLOEXEC)
+	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
 		return -EINVAL;

I think we should reject DRM_RDWR if there's no mmap implementation in the underlying dma-buf vfunc table. Or in the gem version of those. Otherwise looks ok to me, if we first resolve the dma-buf userspace mmap coherency issue.
-Daniel

Quoted 29 lines
drivers/gpu/drm/drm_prime.c +1 -4
-	/* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
-	flags = args->flags & DRM_CLOEXEC;
-
 	return dev->driver->prime_handle_to_fd(dev, file_priv,
-			args->handle, flags, &args->fd);
+			args->handle, args->flags, &args->fd);
 }
 
 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
include/uapi/drm/drm.h +1 -0
index ff6ef62d084b..092fe3fa8ec0 100644
@@ -668,6 +668,7 @@ struct drm_set_client_cap {
 	__u64 value;
 };
 
+#define DRM_RDWR O_RDWR
 #define DRM_CLOEXEC O_CLOEXEC
 struct drm_prime_handle {
 	__u32 handle;
Signature · 6 lines
2.4.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

PATCH RESEND v2 2/2 drm: prime: Document gem_prime_mmap

In reply to Daniel Thompson

gem_prime_map is not currently described in the DRM manual, lets document it.

---
 drivers/gpu/drm/drm_prime.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +3 -1
index 6d2cf4fb4038..3b40a415f45d 100644
@@ -309,7 +309,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  * simpler APIs by using the helper functions @drm_gem_prime_export and
  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
- * five lower-level driver callbacks:
+ * six lower-level driver callbacks:
  *
  * Export callbacks:
  *
@@ -321,6 +321,8 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  *
  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
  *
+ *  - @gem_prime_mmap (optional): mmap a buffer exported by your driver
+ *
  * Import callback:
  *
  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
Signature · 1 line
2.4.3
#4 lore raw

Re: [RESEND PATCH v2 v4.1-rc8 2/2] drm: prime: Document gem_prime_mmap

Quoted 4 lines · Daniel Thompson

gem_prime_map is not currently described in the DRM manual, lets document it.

Applied to drm-misc, thanks.
-Daniel

Quoted 33 lines
---
 drivers/gpu/drm/drm_prime.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/drm_prime.c +3 -1
index 6d2cf4fb4038..3b40a415f45d 100644
@@ -309,7 +309,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  * simpler APIs by using the helper functions @drm_gem_prime_export and
  * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
- * five lower-level driver callbacks:
+ * six lower-level driver callbacks:
  *
  * Export callbacks:
  *
@@ -321,6 +321,8 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
  *
  *  - @gem_prime_vunmap: vunmap a buffer exported by your driver
  *
+ *  - @gem_prime_mmap (optional): mmap a buffer exported by your driver
+ *
  * Import callback:
  *
  *  - @gem_prime_import_sg_table (import): produce a GEM object from another
Signature · 6 lines
2.4.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw
In reply to Daniel Thompson
Quoted 17 lines · Daniel Thompson

This patch set started out as a single patch with a trivial bit of boilerplate to add dmabuf mmap support to the msm driver. However Rob Clark pointed out that, rather than keep one of the tricks I had used, it would be better to change the helpers resulting in this series.

I've tested this both with a rather hacked about Android userspace and with a fairly small test case run from debian. Both bits of code currently use dumb buffers.

Thanks to Benjamin Gaignard for his help in finding this bit of code and to Damien Hobson-Garcia for pointing out that I'd forgotten (since 3.18) to RESEND these patches.

Dave: I guess its probably too late in the dev. cycle to take this code
but don't worry, I will try really hard to remember to RESEND it for 4.2. ;-)

Quoted 8 lines

v2:

* Modified DRM_PRIME_HANDLE_TO_FD to honour the O_RDRW from the user and removed code to workaround this from the sti driver (Rob Clark).

* Added a patch to (rather spartanly) document gem_prime_mmap. Only tacked into this series 'cos I spotted it was missing when I was checking whether I needed to describe DRM_RDRW anywhere.

Oh hornets nest since I just screamed around again against drm prime mmap support ;-) Imo before we expose this for real we really need to somehow figure out what to do about cache coherency. Some intel folks are looking into adding suitable ioctls to the dma-buf fd to make this possible. I think we should wait with enabling drm prime mmaping before that's resolved somehow. I'll point them at your patches though to make sure they don't reinvent this wheel here.
-Daniel

Quoted 17 lines

Daniel Thompson (2):
drm: prime: Honour O_RDWR during prime-handle-to-fd
drm: prime: Document gem_prime_mmap

 drivers/gpu/drm/drm_prime.c | 13 ++++++-------
 include/uapi/drm/drm.h      |  1 +
 2 files changed, 7 insertions(+), 7 deletions(-)

--
2.4.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw

I'm far of being an expert in cache coherency but, from past experiences, I have notice that ION cache management give good performances and is simple to understand.

ION mark as "dirty" the mapped pages in wm_fault function and check this flag while mapping the buffer in kernel space.
If the flag is set it call dma_sync_sg_for_device() with the good direction.

ION allow with an iotcl to do explicit or implicit cache management but, for me, it add complexity to the code when implicit mode should be the preferred one.

Benjamin

2015-06-19 17:48 GMT+02:00 Daniel Vetter <[email protected]>:

Quoted 58 lines · Daniel Thompson
Quoted 17 lines

This patch set started out as a single patch with a trivial bit of boilerplate to add dmabuf mmap support to the msm driver. However Rob Clark pointed out that, rather than keep one of the tricks I had used, it would be better to change the helpers resulting in this series.

I've tested this both with a rather hacked about Android userspace and with a fairly small test case run from debian. Both bits of code currently use dumb buffers.

Thanks to Benjamin Gaignard for his help in finding this bit of code and to Damien Hobson-Garcia for pointing out that I'd forgotten (since 3.18) to RESEND these patches.

Dave: I guess its probably too late in the dev. cycle to take this code
but don't worry, I will try really hard to remember to RESEND it for 4.2. ;-)

Quoted 8 lines

v2:

* Modified DRM_PRIME_HANDLE_TO_FD to honour the O_RDRW from the user and removed code to workaround this from the sti driver (Rob Clark).

* Added a patch to (rather spartanly) document gem_prime_mmap. Only tacked into this series 'cos I spotted it was missing when I was checking whether I needed to describe DRM_RDRW anywhere.

Oh hornets nest since I just screamed around again against drm prime mmap support ;-) Imo before we expose this for real we really need to somehow figure out what to do about cache coherency. Some intel folks are looking into adding suitable ioctls to the dma-buf fd to make this possible. I think we should wait with enabling drm prime mmaping before that's resolved somehow. I'll point them at your patches though to make sure they don't reinvent this wheel here.
-Daniel

Quoted 10 lines

Daniel Thompson (2):
drm: prime: Honour O_RDWR during prime-handle-to-fd
drm: prime: Document gem_prime_mmap

 drivers/gpu/drm/drm_prime.c | 13 ++++++-------
 include/uapi/drm/drm.h      |  1 +
 2 files changed, 7 insertions(+), 7 deletions(-)
Quoted 7 lines

--
2.4.3

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 11 lines
Benjamin Gaignard

Graphic Working Group

Linaro.org │ Open source software for ARM SoCs

Follow Linaro: Facebook | Twitter | Blog
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel