linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC dma-buf 0/3] Improve the dma-buf tracking
@ 2019-02-27  3:54 Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 1/3] dma-buf: give each buffer a full-fledged inode Chenbo Feng
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Chenbo Feng @ 2019-02-27  3:54 UTC (permalink / raw)
  To: linux-kernel, dri-devel, linux-media
  Cc: Sumit Semwal, erickreyes, Chenbo Feng

Currently, all dma-bufs share the same anonymous inode. While we can count
how many dma-buf fds or mappings a process has, we can't get the size of
the backing buffers or tell if two entries point to the same dma-buf. And
in debugfs, we can get a per-buffer breakdown of size and reference count,
but can't tell which processes are actually holding the references to each
buffer.

To resolve the issue above and provide better method for userspace to track
the dma-buf usage across different processes, the following changes are
proposed in dma-buf kernel side. First of all, replace the singleton inode
inside the dma-buf subsystem with a mini-filesystem, and assign each
dma-buf a unique inode out of this filesystem.  With this change, calling
stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's
size (st_size), and even the number of pages assigned to each dma-buffer.
Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo
so in the case where a buffer is mmap()ed into a process’s address space
but all remaining fds have been closed, we can still get the dma-buf
information and try to accociate it with the process by searching the
proc/pid/maps and looking for the corresponding inode number exposed in
dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs
which lets userspace assign short names (e.g., "CAMERA") to buffers. This
information can be extremely helpful for tracking and accounting shared
buffers based on their usage and original purpose. Last but not least, add
dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler
to dma_buf_file_operations. The handler will print the file_count and name
of each buffer.

Greg Hackmann (3):
  dma-buf: give each buffer a full-fledged inode
  dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
  dma-buf: add show_fdinfo handler

 drivers/dma-buf/dma-buf.c    | 121 ++++++++++++++++++++++++++++++++---
 include/linux/dma-buf.h      |   5 +-
 include/uapi/linux/dma-buf.h |   4 ++
 include/uapi/linux/magic.h   |   1 +
 4 files changed, 122 insertions(+), 9 deletions(-)

-- 
2.21.0.rc2.261.ga7da99ff1b-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC dma-buf 1/3] dma-buf: give each buffer a full-fledged inode
  2019-02-27  3:54 [RFC dma-buf 0/3] Improve the dma-buf tracking Chenbo Feng
@ 2019-02-27  3:54 ` Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 2/3] dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls Chenbo Feng
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Chenbo Feng @ 2019-02-27  3:54 UTC (permalink / raw)
  To: linux-kernel, dri-devel, linux-media
  Cc: Sumit Semwal, erickreyes, Greg Hackmann, Chenbo Feng

From: Greg Hackmann <ghackmann@google.com>

By traversing /proc/*/fd and /proc/*/map_files, processes with CAP_ADMIN
can get a lot of fine-grained data about how shmem buffers are shared
among processes.  stat(2) on each entry gives the caller a unique
ID (st_ino), the buffer's size (st_size), and even the number of pages
currently charged to the buffer (st_blocks / 512).

In contrast, all dma-bufs share the same anonymous inode.  So while we
can count how many dma-buf fds or mappings a process has, we can't get
the size of the backing buffers or tell if two entries point to the same
dma-buf.  On systems with debugfs, we can get a per-buffer breakdown of
size and reference count, but can't tell which processes are actually
holding the references to each buffer.

Replace the singleton inode with full-fledged inodes allocated by
alloc_anon_inode().  This involves creating and mounting a
mini-pseudo-filesystem for dma-buf, following the example in fs/aio.c.

Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Chenbo Feng <fengc@google.com>
---
 drivers/dma-buf/dma-buf.c  | 68 +++++++++++++++++++++++++++++++++-----
 include/uapi/linux/magic.h |  1 +
 2 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 02f7f9a89979..d72352356ac5 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -34,8 +34,10 @@
 #include <linux/poll.h>
 #include <linux/reservation.h>
 #include <linux/mm.h>
+#include <linux/mount.h>
 
 #include <uapi/linux/dma-buf.h>
+#include <uapi/linux/magic.h>
 
 static inline int is_dma_buf_file(struct file *);
 
@@ -46,6 +48,25 @@ struct dma_buf_list {
 
 static struct dma_buf_list db_list;
 
+static const struct dentry_operations dma_buf_dentry_ops = {
+	.d_dname = simple_dname,
+};
+
+static struct vfsmount *dma_buf_mnt;
+
+static struct dentry *dma_buf_fs_mount(struct file_system_type *fs_type,
+		int flags, const char *name, void *data)
+{
+	return mount_pseudo(fs_type, "dmabuf:", NULL, &dma_buf_dentry_ops,
+			DMA_BUF_MAGIC);
+}
+
+static struct file_system_type dma_buf_fs_type = {
+	.name = "dmabuf",
+	.mount = dma_buf_fs_mount,
+	.kill_sb = kill_anon_super,
+};
+
 static int dma_buf_release(struct inode *inode, struct file *file)
 {
 	struct dma_buf *dmabuf;
@@ -338,6 +359,34 @@ static inline int is_dma_buf_file(struct file *file)
 	return file->f_op == &dma_buf_fops;
 }
 
+static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags)
+{
+	static const struct qstr this = QSTR_INIT("dmabuf", 6);
+	struct path path;
+	struct file *file;
+	struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
+
+	if (IS_ERR(inode))
+		return ERR_CAST(inode);
+
+	inode->i_size = dmabuf->size;
+	inode_set_bytes(inode, dmabuf->size);
+
+	file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
+				 OPEN_FMODE(flags) | FMODE_LSEEK,
+				 &dma_buf_fops);
+	if (IS_ERR(file))
+		goto err_alloc_file;
+	file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
+	file->private_data = dmabuf;
+
+	return file;
+
+err_alloc_file:
+	iput(inode);
+	return file;
+}
+
 /**
  * DOC: dma buf device access
  *
@@ -433,14 +482,11 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 	}
 	dmabuf->resv = resv;
 
-	file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf,
-					exp_info->flags);
+	file = dma_buf_getfile(dmabuf, exp_info->flags);
 	if (IS_ERR(file)) {
 		ret = PTR_ERR(file);
 		goto err_dmabuf;
 	}
-
-	file->f_mode |= FMODE_LSEEK;
 	dmabuf->file = file;
 
 	mutex_init(&dmabuf->lock);
@@ -1025,8 +1071,8 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		return ret;
 
 	seq_puts(s, "\nDma-buf Objects:\n");
-	seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\n",
-		   "size", "flags", "mode", "count");
+	seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n",
+		   "size", "flags", "mode", "count", "ino");
 
 	list_for_each_entry(buf_obj, &db_list.head, list_node) {
 		ret = mutex_lock_interruptible(&buf_obj->lock);
@@ -1037,11 +1083,12 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 			continue;
 		}
 
-		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
+		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\n",
 				buf_obj->size,
 				buf_obj->file->f_flags, buf_obj->file->f_mode,
 				file_count(buf_obj->file),
-				buf_obj->exp_name);
+				buf_obj->exp_name,
+				file_inode(buf_obj->file)->i_ino);
 
 		robj = buf_obj->resv;
 		while (true) {
@@ -1146,6 +1193,10 @@ static inline void dma_buf_uninit_debugfs(void)
 
 static int __init dma_buf_init(void)
 {
+	dma_buf_mnt = kern_mount(&dma_buf_fs_type);
+	if (IS_ERR(dma_buf_mnt))
+		return PTR_ERR(dma_buf_mnt);
+
 	mutex_init(&db_list.lock);
 	INIT_LIST_HEAD(&db_list.head);
 	dma_buf_init_debugfs();
@@ -1156,5 +1207,6 @@ subsys_initcall(dma_buf_init);
 static void __exit dma_buf_deinit(void)
 {
 	dma_buf_uninit_debugfs();
+	kern_unmount(dma_buf_mnt);
 }
 __exitcall(dma_buf_deinit);
diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h
index f8c00045d537..665e18627f78 100644
--- a/include/uapi/linux/magic.h
+++ b/include/uapi/linux/magic.h
@@ -91,5 +91,6 @@
 #define UDF_SUPER_MAGIC		0x15013346
 #define BALLOON_KVM_MAGIC	0x13661366
 #define ZSMALLOC_MAGIC		0x58295829
+#define DMA_BUF_MAGIC		0x444d4142	/* "DMAB" */
 
 #endif /* __LINUX_MAGIC_H__ */
-- 
2.21.0.rc2.261.ga7da99ff1b-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [RFC dma-buf 2/3] dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
  2019-02-27  3:54 [RFC dma-buf 0/3] Improve the dma-buf tracking Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 1/3] dma-buf: give each buffer a full-fledged inode Chenbo Feng
@ 2019-02-27  3:54 ` Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 3/3] dma-buf: add show_fdinfo handler Chenbo Feng
  2019-03-14 17:49 ` [RFC dma-buf 0/3] Improve the dma-buf tracking Sumit Semwal
  3 siblings, 0 replies; 8+ messages in thread
From: Chenbo Feng @ 2019-02-27  3:54 UTC (permalink / raw)
  To: linux-kernel, dri-devel, linux-media
  Cc: Sumit Semwal, erickreyes, Greg Hackmann, Chenbo Feng

From: Greg Hackmann <ghackmann@google.com>

This patch adds complimentary DMA_BUF_SET_NAME and DMA_BUF_GET_NAME
ioctls, which lets userspace processes attach a free-form name to each
buffer.

This information can be extremely helpful for tracking and accounting
shared buffers.  For example, on Android, we know what each buffer will
be used for at allocation time: GL, multimedia, camera, etc.  The
userspace allocator can use DMA_BUF_SET_NAME to associate that
information with the buffer, so we can later give developers a
breakdown of how much memory they're allocating for graphics, camera,
etc.

Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Chenbo Feng <fengc@google.com>
---
 drivers/dma-buf/dma-buf.c    | 42 ++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h      |  5 ++++-
 include/uapi/linux/dma-buf.h |  4 ++++
 3 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index d72352356ac5..e0d9cdd3520b 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -297,6 +297,36 @@ static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
 	return events;
 }
 
+static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
+{
+	char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
+
+	if (IS_ERR(name))
+		return PTR_ERR(name);
+
+	mutex_lock(&dmabuf->lock);
+	kfree(dmabuf->name);
+	dmabuf->name = name;
+	mutex_unlock(&dmabuf->lock);
+
+	return 0;
+}
+
+static long dma_buf_get_name(struct dma_buf *dmabuf, char __user *buf)
+{
+	const char *name = "";
+	long ret = 0;
+
+	mutex_lock(&dmabuf->lock);
+	if (dmabuf->name)
+		name = dmabuf->name;
+	if (copy_to_user(buf, name, strlen(name) + 1))
+		ret = -EFAULT;
+	mutex_unlock(&dmabuf->lock);
+
+	return ret;
+}
+
 static long dma_buf_ioctl(struct file *file,
 			  unsigned int cmd, unsigned long arg)
 {
@@ -335,6 +365,13 @@ static long dma_buf_ioctl(struct file *file,
 			ret = dma_buf_begin_cpu_access(dmabuf, direction);
 
 		return ret;
+
+	case DMA_BUF_SET_NAME:
+		return dma_buf_set_name(dmabuf, (const char __user *)arg);
+
+	case DMA_BUF_GET_NAME:
+		return dma_buf_get_name(dmabuf, (char __user *)arg);
+
 	default:
 		return -ENOTTY;
 	}
@@ -1083,12 +1120,13 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 			continue;
 		}
 
-		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\n",
+		seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
 				buf_obj->size,
 				buf_obj->file->f_flags, buf_obj->file->f_mode,
 				file_count(buf_obj->file),
 				buf_obj->exp_name,
-				file_inode(buf_obj->file)->i_ino);
+				file_inode(buf_obj->file)->i_ino,
+				buf_obj->name ?: "");
 
 		robj = buf_obj->resv;
 		while (true) {
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 58725f890b5b..582998e19df6 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -255,10 +255,12 @@ struct dma_buf_ops {
  * @file: file pointer used for sharing buffers across, and for refcounting.
  * @attachments: list of dma_buf_attachment that denotes all devices attached.
  * @ops: dma_buf_ops associated with this buffer object.
- * @lock: used internally to serialize list manipulation, attach/detach and vmap/unmap
+ * @lock: used internally to serialize list manipulation, attach/detach and
+ *        vmap/unmap, and accesses to name
  * @vmapping_counter: used internally to refcnt the vmaps
  * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
  * @exp_name: name of the exporter; useful for debugging.
+ * @name: userspace-provided name; useful for accounting and debugging.
  * @owner: pointer to exporter module; used for refcounting when exporter is a
  *         kernel module.
  * @list_node: node for dma_buf accounting and debugging.
@@ -286,6 +288,7 @@ struct dma_buf {
 	unsigned vmapping_counter;
 	void *vmap_ptr;
 	const char *exp_name;
+	const char *name;
 	struct module *owner;
 	struct list_head list_node;
 	void *priv;
diff --git a/include/uapi/linux/dma-buf.h b/include/uapi/linux/dma-buf.h
index d75df5210a4a..4e9c5fe7aecd 100644
--- a/include/uapi/linux/dma-buf.h
+++ b/include/uapi/linux/dma-buf.h
@@ -35,7 +35,11 @@ struct dma_buf_sync {
 #define DMA_BUF_SYNC_VALID_FLAGS_MASK \
 	(DMA_BUF_SYNC_RW | DMA_BUF_SYNC_END)
 
+#define DMA_BUF_NAME_LEN	32
+
 #define DMA_BUF_BASE		'b'
 #define DMA_BUF_IOCTL_SYNC	_IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)
+#define DMA_BUF_SET_NAME	_IOW(DMA_BUF_BASE, 1, const char *)
+#define DMA_BUF_GET_NAME	_IOR(DMA_BUF_BASE, 2, char *)
 
 #endif
-- 
2.21.0.rc2.261.ga7da99ff1b-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [RFC dma-buf 3/3] dma-buf: add show_fdinfo handler
  2019-02-27  3:54 [RFC dma-buf 0/3] Improve the dma-buf tracking Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 1/3] dma-buf: give each buffer a full-fledged inode Chenbo Feng
  2019-02-27  3:54 ` [RFC dma-buf 2/3] dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls Chenbo Feng
@ 2019-02-27  3:54 ` Chenbo Feng
  2019-03-14 17:49 ` [RFC dma-buf 0/3] Improve the dma-buf tracking Sumit Semwal
  3 siblings, 0 replies; 8+ messages in thread
From: Chenbo Feng @ 2019-02-27  3:54 UTC (permalink / raw)
  To: linux-kernel, dri-devel, linux-media
  Cc: Sumit Semwal, erickreyes, Greg Hackmann, Chenbo Feng

From: Greg Hackmann <ghackmann@google.com>

The show_fdinfo handler exports the same information available through
debugfs on a per-buffer basis.

Signed-off-by: Greg Hackmann <ghackmann@google.com>
Signed-off-by: Chenbo Feng <fengc@google.com>
---
 drivers/dma-buf/dma-buf.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index e0d9cdd3520b..2da3e2653f92 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -377,6 +377,20 @@ static long dma_buf_ioctl(struct file *file,
 	}
 }
 
+static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
+{
+	struct dma_buf *dmabuf = file->private_data;
+
+	seq_printf(m, "size:\t%zu\n", dmabuf->size);
+	/* Don't count the temporary reference taken inside procfs seq_show */
+	seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
+	seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
+	mutex_lock(&dmabuf->lock);
+	if (dmabuf->name)
+		seq_printf(m, "name:\t%s\n", dmabuf->name);
+	mutex_unlock(&dmabuf->lock);
+}
+
 static const struct file_operations dma_buf_fops = {
 	.release	= dma_buf_release,
 	.mmap		= dma_buf_mmap_internal,
@@ -386,6 +400,7 @@ static const struct file_operations dma_buf_fops = {
 #ifdef CONFIG_COMPAT
 	.compat_ioctl	= dma_buf_ioctl,
 #endif
+	.show_fdinfo	= dma_buf_show_fdinfo,
 };
 
 /*
-- 
2.21.0.rc2.261.ga7da99ff1b-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [RFC dma-buf 0/3] Improve the dma-buf tracking
  2019-02-27  3:54 [RFC dma-buf 0/3] Improve the dma-buf tracking Chenbo Feng
                   ` (2 preceding siblings ...)
  2019-02-27  3:54 ` [RFC dma-buf 3/3] dma-buf: add show_fdinfo handler Chenbo Feng
@ 2019-03-14 17:49 ` Sumit Semwal
  2019-03-15  1:50   ` Chenbo Feng
  2019-03-15 11:06   ` Daniel Vetter
  3 siblings, 2 replies; 8+ messages in thread
From: Sumit Semwal @ 2019-03-14 17:49 UTC (permalink / raw)
  To: Chenbo Feng; +Cc: LKML, DRI mailing list, linux-media, erickreyes

Hello Chenbo,Thank you for your RFC series.

On Wed, 27 Feb 2019 at 09:24, Chenbo Feng <fengc@google.com> wrote:
>
> Currently, all dma-bufs share the same anonymous inode. While we can count
> how many dma-buf fds or mappings a process has, we can't get the size of
> the backing buffers or tell if two entries point to the same dma-buf. And
> in debugfs, we can get a per-buffer breakdown of size and reference count,
> but can't tell which processes are actually holding the references to each
> buffer.
>
> To resolve the issue above and provide better method for userspace to track
> the dma-buf usage across different processes, the following changes are
> proposed in dma-buf kernel side. First of all, replace the singleton inode
> inside the dma-buf subsystem with a mini-filesystem, and assign each
> dma-buf a unique inode out of this filesystem.  With this change, calling
> stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's
> size (st_size), and even the number of pages assigned to each dma-buffer.
> Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo
> so in the case where a buffer is mmap()ed into a process’s address space
> but all remaining fds have been closed, we can still get the dma-buf
> information and try to accociate it with the process by searching the
> proc/pid/maps and looking for the corresponding inode number exposed in
> dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs
> which lets userspace assign short names (e.g., "CAMERA") to buffers. This
> information can be extremely helpful for tracking and accounting shared
> buffers based on their usage and original purpose. Last but not least, add
> dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler
> to dma_buf_file_operations. The handler will print the file_count and name
> of each buffer.

In general, I think I like the idea as it contributes to a much more
relevant usage analysis of dma-buf backed buffers.
I will get to doing a more detailed review soon, but immediately, we
might want to think a bit about the get/set_name IOCTLS - do we need
to think of disallowing multiple renaming of buffers once they start
getting used? It could otherwise make the whole metrics a lot
confused?

>
> Greg Hackmann (3):
>   dma-buf: give each buffer a full-fledged inode
>   dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
>   dma-buf: add show_fdinfo handler
>
>  drivers/dma-buf/dma-buf.c    | 121 ++++++++++++++++++++++++++++++++---
>  include/linux/dma-buf.h      |   5 +-
>  include/uapi/linux/dma-buf.h |   4 ++
>  include/uapi/linux/magic.h   |   1 +
>  4 files changed, 122 insertions(+), 9 deletions(-)
>
> --
> 2.21.0.rc2.261.ga7da99ff1b-goog
>

Best,
Sumit.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC dma-buf 0/3] Improve the dma-buf tracking
  2019-03-14 17:49 ` [RFC dma-buf 0/3] Improve the dma-buf tracking Sumit Semwal
@ 2019-03-15  1:50   ` Chenbo Feng
  2019-03-15 11:06   ` Daniel Vetter
  1 sibling, 0 replies; 8+ messages in thread
From: Chenbo Feng @ 2019-03-15  1:50 UTC (permalink / raw)
  To: Sumit Semwal; +Cc: LKML, DRI mailing list, linux-media, Erick Reyes

On Thu, Mar 14, 2019 at 10:49 AM Sumit Semwal <sumit.semwal@linaro.org> wrote:
>
> Hello Chenbo,Thank you for your RFC series.
>
> On Wed, 27 Feb 2019 at 09:24, Chenbo Feng <fengc@google.com> wrote:
> >
> > Currently, all dma-bufs share the same anonymous inode. While we can count
> > how many dma-buf fds or mappings a process has, we can't get the size of
> > the backing buffers or tell if two entries point to the same dma-buf. And
> > in debugfs, we can get a per-buffer breakdown of size and reference count,
> > but can't tell which processes are actually holding the references to each
> > buffer.
> >
> > To resolve the issue above and provide better method for userspace to track
> > the dma-buf usage across different processes, the following changes are
> > proposed in dma-buf kernel side. First of all, replace the singleton inode
> > inside the dma-buf subsystem with a mini-filesystem, and assign each
> > dma-buf a unique inode out of this filesystem.  With this change, calling
> > stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's
> > size (st_size), and even the number of pages assigned to each dma-buffer.
> > Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo
> > so in the case where a buffer is mmap()ed into a process’s address space
> > but all remaining fds have been closed, we can still get the dma-buf
> > information and try to accociate it with the process by searching the
> > proc/pid/maps and looking for the corresponding inode number exposed in
> > dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs
> > which lets userspace assign short names (e.g., "CAMERA") to buffers. This
> > information can be extremely helpful for tracking and accounting shared
> > buffers based on their usage and original purpose. Last but not least, add
> > dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler
> > to dma_buf_file_operations. The handler will print the file_count and name
> > of each buffer.
>
> In general, I think I like the idea as it contributes to a much more
> relevant usage analysis of dma-buf backed buffers.
> I will get to doing a more detailed review soon, but immediately, we
> might want to think a bit about the get/set_name IOCTLS - do we need
> to think of disallowing multiple renaming of buffers once they start
> getting used? It could otherwise make the whole metrics a lot
> confused?
Yes, I agree we need to control the dma-buf naming to make sure it
doesn't confuse the user. Ideally the process asked for dma-buf need
to make sure they only set the name once when they are using the
buffer for the same purpose. But I guess we can add check in kernel
side to make sure the name only get initialized once and can never
change after it. Or do we have better way to disallow multiple
renaming of buffers once they start getting used? Can we find a way to
set the name information in dma_buf_export so no one can change it
after that?
>
> >
> > Greg Hackmann (3):
> >   dma-buf: give each buffer a full-fledged inode
> >   dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
> >   dma-buf: add show_fdinfo handler
> >
> >  drivers/dma-buf/dma-buf.c    | 121 ++++++++++++++++++++++++++++++++---
> >  include/linux/dma-buf.h      |   5 +-
> >  include/uapi/linux/dma-buf.h |   4 ++
> >  include/uapi/linux/magic.h   |   1 +
> >  4 files changed, 122 insertions(+), 9 deletions(-)
> >
> > --
> > 2.21.0.rc2.261.ga7da99ff1b-goog
> >
>
> Best,
> Sumit.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC dma-buf 0/3] Improve the dma-buf tracking
  2019-03-14 17:49 ` [RFC dma-buf 0/3] Improve the dma-buf tracking Sumit Semwal
  2019-03-15  1:50   ` Chenbo Feng
@ 2019-03-15 11:06   ` Daniel Vetter
  2019-03-18 16:17     ` Sumit Semwal
  1 sibling, 1 reply; 8+ messages in thread
From: Daniel Vetter @ 2019-03-15 11:06 UTC (permalink / raw)
  To: Sumit Semwal
  Cc: Chenbo Feng, erickreyes, LKML, DRI mailing list,
	open list:DMA BUFFER SHARING FRAMEWORK

On Thu, Mar 14, 2019 at 6:49 PM Sumit Semwal <sumit.semwal@linaro.org> wrote:
>
> Hello Chenbo,Thank you for your RFC series.
>
> On Wed, 27 Feb 2019 at 09:24, Chenbo Feng <fengc@google.com> wrote:
> >
> > Currently, all dma-bufs share the same anonymous inode. While we can count
> > how many dma-buf fds or mappings a process has, we can't get the size of
> > the backing buffers or tell if two entries point to the same dma-buf. And
> > in debugfs, we can get a per-buffer breakdown of size and reference count,
> > but can't tell which processes are actually holding the references to each
> > buffer.
> >
> > To resolve the issue above and provide better method for userspace to track
> > the dma-buf usage across different processes, the following changes are
> > proposed in dma-buf kernel side. First of all, replace the singleton inode
> > inside the dma-buf subsystem with a mini-filesystem, and assign each
> > dma-buf a unique inode out of this filesystem.  With this change, calling
> > stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's
> > size (st_size), and even the number of pages assigned to each dma-buffer.
> > Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo
> > so in the case where a buffer is mmap()ed into a process’s address space
> > but all remaining fds have been closed, we can still get the dma-buf
> > information and try to accociate it with the process by searching the
> > proc/pid/maps and looking for the corresponding inode number exposed in
> > dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs
> > which lets userspace assign short names (e.g., "CAMERA") to buffers. This
> > information can be extremely helpful for tracking and accounting shared
> > buffers based on their usage and original purpose. Last but not least, add
> > dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler
> > to dma_buf_file_operations. The handler will print the file_count and name
> > of each buffer.
>
> In general, I think I like the idea as it contributes to a much more
> relevant usage analysis of dma-buf backed buffers.
> I will get to doing a more detailed review soon, but immediately, we
> might want to think a bit about the get/set_name IOCTLS - do we need
> to think of disallowing multiple renaming of buffers once they start
> getting used? It could otherwise make the whole metrics a lot
> confused?
>
> >
> > Greg Hackmann (3):
> >   dma-buf: give each buffer a full-fledged inode
> >   dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
> >   dma-buf: add show_fdinfo handler

I'm not seeing the patches, so just quick comment here: Some drivers
(at least vc4) already have the concept of buffer names. Would be neat
to integrate this between dma-buf and drm_gem in prime.

Aside from that sounds like a good idea overall, but I can't see any details.
-Daniel

> >
> >  drivers/dma-buf/dma-buf.c    | 121 ++++++++++++++++++++++++++++++++---
> >  include/linux/dma-buf.h      |   5 +-
> >  include/uapi/linux/dma-buf.h |   4 ++
> >  include/uapi/linux/magic.h   |   1 +
> >  4 files changed, 122 insertions(+), 9 deletions(-)
> >
> > --
> > 2.21.0.rc2.261.ga7da99ff1b-goog
> >
>
> Best,
> Sumit.
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel



-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [RFC dma-buf 0/3] Improve the dma-buf tracking
  2019-03-15 11:06   ` Daniel Vetter
@ 2019-03-18 16:17     ` Sumit Semwal
  0 siblings, 0 replies; 8+ messages in thread
From: Sumit Semwal @ 2019-03-18 16:17 UTC (permalink / raw)
  To: Daniel Vetter
  Cc: Chenbo Feng, erickreyes, LKML, DRI mailing list,
	open list:DMA BUFFER SHARING FRAMEWORK

Hi Daniel,

On Fri, 15 Mar 2019 at 16:36, Daniel Vetter <daniel@ffwll.ch> wrote:
>
> On Thu, Mar 14, 2019 at 6:49 PM Sumit Semwal <sumit.semwal@linaro.org> wrote:
> >
> > Hello Chenbo,Thank you for your RFC series.
> >
> > On Wed, 27 Feb 2019 at 09:24, Chenbo Feng <fengc@google.com> wrote:
> > >
> > > Currently, all dma-bufs share the same anonymous inode. While we can count
> > > how many dma-buf fds or mappings a process has, we can't get the size of
> > > the backing buffers or tell if two entries point to the same dma-buf. And
> > > in debugfs, we can get a per-buffer breakdown of size and reference count,
> > > but can't tell which processes are actually holding the references to each
> > > buffer.
> > >
> > > To resolve the issue above and provide better method for userspace to track
> > > the dma-buf usage across different processes, the following changes are
> > > proposed in dma-buf kernel side. First of all, replace the singleton inode
> > > inside the dma-buf subsystem with a mini-filesystem, and assign each
> > > dma-buf a unique inode out of this filesystem.  With this change, calling
> > > stat(2) on each entry gives the caller a unique ID (st_ino), the buffer's
> > > size (st_size), and even the number of pages assigned to each dma-buffer.
> > > Secoundly, add the inode information to /sys/kernel/debug/dma_buf/bufinfo
> > > so in the case where a buffer is mmap()ed into a process’s address space
> > > but all remaining fds have been closed, we can still get the dma-buf
> > > information and try to accociate it with the process by searching the
> > > proc/pid/maps and looking for the corresponding inode number exposed in
> > > dma-buf debug fs. Thirdly, created an ioctl to assign names to dma-bufs
> > > which lets userspace assign short names (e.g., "CAMERA") to buffers. This
> > > information can be extremely helpful for tracking and accounting shared
> > > buffers based on their usage and original purpose. Last but not least, add
> > > dma-buf information to /proc/pid/fdinfo by adding a show_fdinfo() handler
> > > to dma_buf_file_operations. The handler will print the file_count and name
> > > of each buffer.
> >
> > In general, I think I like the idea as it contributes to a much more
> > relevant usage analysis of dma-buf backed buffers.
> > I will get to doing a more detailed review soon, but immediately, we
> > might want to think a bit about the get/set_name IOCTLS - do we need
> > to think of disallowing multiple renaming of buffers once they start
> > getting used? It could otherwise make the whole metrics a lot
> > confused?
> >
> > >
> > > Greg Hackmann (3):
> > >   dma-buf: give each buffer a full-fledged inode
> > >   dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls
> > >   dma-buf: add show_fdinfo handler
>
> I'm not seeing the patches, so just quick comment here: Some drivers
> (at least vc4) already have the concept of buffer names. Would be neat
> to integrate this between dma-buf and drm_gem in prime.
>
FYI, here's the patch series - https://patchwork.freedesktop.org/series/57282/

Best,
Sumit.

> Aside from that sounds like a good idea overall, but I can't see any details.
> -Daniel
>
> > >
> > >  drivers/dma-buf/dma-buf.c    | 121 ++++++++++++++++++++++++++++++++---
> > >  include/linux/dma-buf.h      |   5 +-
> > >  include/uapi/linux/dma-buf.h |   4 ++
> > >  include/uapi/linux/magic.h   |   1 +
> > >  4 files changed, 122 insertions(+), 9 deletions(-)
> > >
> > > --
> > > 2.21.0.rc2.261.ga7da99ff1b-goog
> > >
> >
> > Best,
> > Sumit.
> > _______________________________________________
> > dri-devel mailing list
> > dri-devel@lists.freedesktop.org
> > https://lists.freedesktop.org/mailman/listinfo/dri-devel
>
>
>
> --
> Daniel Vetter
> Software Engineer, Intel Corporation
> +41 (0) 79 365 57 48 - http://blog.ffwll.ch



-- 
Thanks and regards,

Sumit Semwal
Linaro Consumer Group - Kernel Team Lead
Linaro.org │ Open source software for ARM SoCs

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-03-18 16:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-27  3:54 [RFC dma-buf 0/3] Improve the dma-buf tracking Chenbo Feng
2019-02-27  3:54 ` [RFC dma-buf 1/3] dma-buf: give each buffer a full-fledged inode Chenbo Feng
2019-02-27  3:54 ` [RFC dma-buf 2/3] dma-buf: add DMA_BUF_{GET,SET}_NAME ioctls Chenbo Feng
2019-02-27  3:54 ` [RFC dma-buf 3/3] dma-buf: add show_fdinfo handler Chenbo Feng
2019-03-14 17:49 ` [RFC dma-buf 0/3] Improve the dma-buf tracking Sumit Semwal
2019-03-15  1:50   ` Chenbo Feng
2019-03-15 11:06   ` Daniel Vetter
2019-03-18 16:17     ` Sumit Semwal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).