Skip to contentSkip to conversation

mmap on the dma-buf directly

PATCH Superseded Tiago Vignatti 2 participants 6 messages Aug 5, 2015 – Aug 6, 2015

Subsystems (4)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover mmap on the dma-buf directly

Hi,

I've tested these patches (in drm-intel-nightly, but also in CrOS kernel v3.14) and they seem just enough for what we want to do: the idea is to create a GEM bo in one process and pass the prime handle of the it to another process, which in turn uses the handle only to map and write. This could be useful for Chrome OS architecture, where the Web content ("unpriviledged process") maps and CPU-draws a buffer, which was previously allocated in the GPU process ("priviledged process").

In v2, I've added a patch that Daniel kindly drafted to allow the unpriviledged process flush through a prime fd. To validate it I've built a test on top of igt's kms_pwrite_crc (which I'll be sending next for review).

Let me know your concerns.

Tiago

Daniel Thompson (1):
drm: prime: Honour O_RDWR during prime-handle-to-fd

Daniel Vetter (1):
dma-buf: Add ioctls to allow userspace to flush

Tiago Vignatti (1):
drm/i915: Use CPU mapping for userspace dma-buf mmap()

 drivers/dma-buf/dma-buf.c              | 47 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/drm_prime.c            | 10 +++-----
 drivers/gpu/drm/i915/i915_gem_dmabuf.c | 18 ++++++++++++-
 include/uapi/drm/drm.h                 |  1 +
 include/uapi/linux/dma-buf.h           | 39 ++++++++++++++++++++++++++++
 5 files changed, 107 insertions(+), 8 deletions(-)
 create mode 100644 include/uapi/linux/dma-buf.h
Signature · 6 lines
2.1.0

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

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

From: Daniel Thompson <[email protected]>

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.

v3 (Tiago): removed unused flags variable from drm_prime_handle_to_fd_ioctl.

---
 drivers/gpu/drm/drm_prime.c | 10 +++-------
 include/uapi/drm/drm.h      |  1 +
 2 files changed, 4 insertions(+), 7 deletions(-)
drivers/gpu/drm/drm_prime.c +3 -7
index 27aa718..df6cdc7 100644
@@ -329,7 +329,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.
@@ -628,7 +628,6 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
 				 struct drm_file *file_priv)
 {
 	struct drm_prime_handle *args = data;
-	uint32_t flags;
 
 	if (!drm_core_check_feature(dev, DRIVER_PRIME))
 		return -EINVAL;
@@ -637,14 +636,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 3801584..ad8223e 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.1.0

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

PATCH v2 2/3 dma-buf: Add ioctls to allow userspace to flush

In reply to Tiago Vignatti

From: Daniel Vetter <[email protected]>

FIXME: Update kerneldoc for begin/end to make it clear that those are for mmap too.

Open: Do we need a special indication that the begin/end is from userspace mmap and not from kernel mmap?

There's also the question already about kernel internal users - vmap and kmap interfaces are much different ... We might need to add a mapping enum to the begin/end dma-buf functions.

v2 (Tiago): Fix header file type names (u64 -> __u64)

---
 drivers/dma-buf/dma-buf.c    | 47 ++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/dma-buf.h | 39 ++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
 create mode 100644 include/uapi/linux/dma-buf.h
drivers/dma-buf/dma-buf.c +47 -0
index 155c146..4820d61 100644
@@ -34,6 +34,8 @@
 #include <linux/poll.h>
 #include <linux/reservation.h>
 
+#include <uapi/linux/dma-buf.h>
+
 static inline int is_dma_buf_file(struct file *);
 
 struct dma_buf_list {
@@ -251,11 +253,56 @@ out:
 	return events;
 }
 
+static long dma_buf_ioctl(struct file *file,
+			  unsigned int cmd, unsigned long arg)
+{
+	struct dma_buf *dmabuf;
+	struct dma_buf_sync sync;
+	enum dma_data_direction direction;
+
+	dmabuf = file->private_data;
+
+	if (!is_dma_buf_file(file))
+		return -EINVAL;
+
+	switch (cmd) {
+	case DMA_BUF_IOCTL_SYNC:
+		if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
+			return -EFAULT;
+
+		if (sync.flags & DMA_BUF_SYNC_RW)
+			direction = DMA_BIDIRECTIONAL;
+		else if (sync.flags & DMA_BUF_SYNC_READ)
+			direction = DMA_FROM_DEVICE;
+		else if (sync.flags & DMA_BUF_SYNC_WRITE)
+			direction = DMA_TO_DEVICE;
+		else
+			return -EINVAL;
+
+		if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
+			return -EINVAL;
+
+		/* FIXME: Check for overflows in start/length. */
+
+		if (sync.flags & DMA_BUF_SYNC_END)
+			dma_buf_end_cpu_access(dmabuf, sync.start,
+					       sync.length, direction);
+		else
+			dma_buf_begin_cpu_access(dmabuf, sync.start,
+						 sync.length, direction);
+
+		return 0;
+	default:
+		return -ENOTTY;
+	}
+}
+
 static const struct file_operations dma_buf_fops = {
 	.release	= dma_buf_release,
 	.mmap		= dma_buf_mmap_internal,
 	.llseek		= dma_buf_llseek,
 	.poll		= dma_buf_poll,
+	.unlocked_ioctl	= dma_buf_ioctl,
 };
 
 /*
include/uapi/linux/dma-buf.h new file +39 -0
new file mode 100644
index 0000000..e5327df
@@ -0,0 +1,39 @@
+/*
+ * Framework for buffer objects that can be shared across devices/subsystems.
+ *
+ * Copyright(C) 2015 Intel Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _DMA_BUF_UAPI_H_
+#define _DMA_BUF_UAPI_H_
+
+struct dma_buf_sync {
+	__u64 flags;
+	__u64 start;
+	__u64 length;
+};
+
+#define DMA_BUF_SYNC_READ	(1 << 0)
+#define DMA_BUF_SYNC_WRITE	(2 << 0)
+#define DMA_BUF_SYNC_RW		(3 << 0)
+#define DMA_BUF_SYNC_START	(0 << 2)
+#define DMA_BUF_SYNC_END	(1 << 2)
+#define DMA_BUF_SYNC_VALID_FLAGS_MASK \
+	(DMA_BUF_SYNC_RW | DMA_BUF_SYNC_END)
+
+#define DMA_BUF_BASE		'b'
+#define DMA_BUF_IOCTL_SYNC	_IOWR(DMA_BUF_BASE, 0, struct dma_buf_sync)
+
+#endif
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH v2 3/3 drm/i915: Use CPU mapping for userspace dma-buf mmap()

In reply to Tiago Vignatti

Userspace is the one in charge of flush CPU by wrapping mmap with begin{,end}_cpu_access.

v2: Remove LLC check cause we have dma-buf sync providers now. Also, fix return before transferring ownership when mmap fails.

---
 drivers/gpu/drm/i915/i915_gem_dmabuf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_gem_dmabuf.c +17 -1
index e9c2bfd..b608f67 100644
@@ -193,7 +193,23 @@ static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_n
 
 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
 {
-	return -EINVAL;
+	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
+	int ret;
+
+	if (obj->base.size < vma->vm_end - vma->vm_start)
+		return -EINVAL;
+
+	if (WARN_ON(!obj->base.filp))
+		return -EINVAL;
+
+	ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
+	if (IS_ERR_VALUE(ret))
+		return -EINVAL;
+
+	fput(vma->vm_file);
+	vma->vm_file = get_file(obj->base.filp);
+
+	return ret;
 }
 
 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
Signature · 6 lines
2.1.0

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

Re: [PATCH 3/3] drm/i915: Use CPU mapping for userspace dma-buf mmap()

Quoted 5 lines · Tiago Vignatti

Userspace is the one in charge of flush CPU by wrapping mmap with begin{,end}_cpu_access.

v2: Remove LLC check cause we have dma-buf sync providers now. Also, fix return before transferring ownership when mmap fails.

i915 doesn't implement end_cpu_access() as required for your sync ioctl.

Quoted 12 lines
---
 drivers/gpu/drm/i915/i915_gem_dmabuf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/i915/i915_gem_dmabuf.c +0 -0
index e9c2bfd..b608f67 100644
@@ -193,7 +193,23 @@ static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_n
 
 static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)

drivers/gpu/drm/i915/i915_gem_dmabuf.c static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_n · line 203

Quoted 10 lines
drivers/gpu/drm/i915/i915_gem_dmabuf.c +8 -1
 {
-	return -EINVAL;
+	struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
+	int ret;
+
+	if (obj->base.size < vma->vm_end - vma->vm_start)
+		return -EINVAL;
+
+	if (WARN_ON(!obj->base.filp))
+		return -EINVAL;

So long as Linus is not watching, ENODEV - since we could support it, but we just have not implemented it yet.

drivers/gpu/drm/i915/i915_gem_dmabuf.c static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_n · line 207

Quoted 3 lines
drivers/gpu/drm/i915/i915_gem_dmabuf.c +3 -0
+	ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
+	if (IS_ERR_VALUE(ret))
+		return -EINVAL;

The return is 0 on success or error otherwise. Always propagate the original error unless you have good reason not to (in which case you document it). So

if (ret)
return ret;

drivers/gpu/drm/i915/i915_gem_dmabuf.c static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_n · line 212

Quoted 5 lines
drivers/gpu/drm/i915/i915_gem_dmabuf.c +5 -0
+
+	fput(vma->vm_file);
+	vma->vm_file = get_file(obj->base.filp);
+
+	return ret;

return 0;

Quoted 6 lines
drivers/gpu/drm/i915/i915_gem_dmabuf.c +0 -0
 }
 
 static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
Signature · 1 line
2.1.0
Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

Re: [PATCH 3/3] drm/i915: Use CPU mapping for userspace dma-buf mmap()

Quoted 8 lines · Chris Wilson
Quoted 5 lines · Tiago Vignatti

Userspace is the one in charge of flush CPU by wrapping mmap with begin{,end}_cpu_access.

v2: Remove LLC check cause we have dma-buf sync providers now. Also, fix return before transferring ownership when mmap fails.

i915 doesn't implement end_cpu_access() as required for your sync ioctl.

An important point here is that performance on !llc (and for certain objects with llc) will be absymal, utterly absymal. So what's the rationale for this compared to doing the opposite and mapping the client memory (a normal mmap) into the GPU with llc/snooping. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx