dma-buf: add DMA_BUF_SET_NAME ioctls
authorGreg Hackmann <ghackmann@google.com>
Thu, 13 Jun 2019 22:34:07 +0000 (15:34 -0700)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Fri, 7 Jun 2024 15:40:56 +0000 (17:40 +0200)
This patch adds complimentary DMA_BUF_SET_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>
Signed-off-by: Sumit Semwal <sumit.semwal@linaro.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20190613223408.139221-3-fengc@google.com
[backport of the commit bb2bb903042517b8fb17b2bc21e00512f2dcac01 from mainline]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
drivers/dma-buf/dma-buf.c
include/linux/dma-buf.h
include/uapi/linux/dma-buf.h

index 216dec5cad640e840d8a0113489654f809120216..6460c93bfc35ddf3523ce850c24f86c656c960b1 100644 (file)
@@ -47,8 +47,24 @@ struct dma_buf_list {
 
 static struct dma_buf_list db_list;
 
+static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
+{
+       struct dma_buf *dmabuf;
+       char name[DMA_BUF_NAME_LEN];
+       size_t ret = 0;
+
+       dmabuf = dentry->d_fsdata;
+       mutex_lock(&dmabuf->lock);
+       if (dmabuf->name)
+               ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
+       mutex_unlock(&dmabuf->lock);
+
+       return dynamic_dname(dentry, buffer, buflen, "/%s:%s",
+                            dentry->d_name.name, ret > 0 ? name : "");
+}
+
 static const struct dentry_operations dma_buf_dentry_ops = {
-       .d_dname = simple_dname,
+       .d_dname = dmabuffs_dname,
 };
 
 static struct vfsmount *dma_buf_mnt;
@@ -274,6 +290,43 @@ out:
        return events;
 }
 
+/**
+ * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
+ * The name of the dma-buf buffer can only be set when the dma-buf is not
+ * attached to any devices. It could theoritically support changing the
+ * name of the dma-buf if the same piece of memory is used for multiple
+ * purpose between different devices.
+ *
+ * @dmabuf [in]     dmabuf buffer that will be renamed.
+ * @buf:   [in]     A piece of userspace memory that contains the name of
+ *                  the dma-buf.
+ *
+ * Returns 0 on success. If the dma-buf buffer is already attached to
+ * devices, return -EBUSY.
+ *
+ */
+static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
+{
+       char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
+       long ret = 0;
+
+       if (IS_ERR(name))
+               return PTR_ERR(name);
+
+       mutex_lock(&dmabuf->lock);
+       if (!list_empty(&dmabuf->attachments)) {
+               ret = -EBUSY;
+               kfree(name);
+               goto out_unlock;
+       }
+       kfree(dmabuf->name);
+       dmabuf->name = name;
+
+out_unlock:
+       mutex_unlock(&dmabuf->lock);
+       return ret;
+}
+
 static long dma_buf_ioctl(struct file *file,
                          unsigned int cmd, unsigned long arg)
 {
@@ -311,6 +364,10 @@ static long dma_buf_ioctl(struct file *file,
                        dma_buf_begin_cpu_access(dmabuf, direction);
 
                return 0;
+
+       case DMA_BUF_SET_NAME:
+               return dma_buf_set_name(dmabuf, (const char __user *)arg);
+
        default:
                return -ENOTTY;
        }
@@ -349,6 +406,7 @@ static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags)
                goto err_alloc_file;
        file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
        file->private_data = dmabuf;
+       file->f_path.dentry->d_fsdata = dmabuf;
 
        return file;
 
@@ -888,12 +946,13 @@ static int dma_buf_describe(struct seq_file *s)
                        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 ?: "");
 
                seq_puts(s, "\tAttached Devices:\n");
                attach_count = 0;
index 532108ea0c1c046dbc264af046137efc3a0d7b81..cde81e468f294e3d69232ef100034c05a0f1d018 100644 (file)
@@ -112,7 +112,10 @@ 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, and accesses to name
  * @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.
@@ -129,6 +132,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;
index fb0dedb7c1217e862e5d30e135b9deaa43f64e0b..16069357a20be28ff163da13ce5cc49c44c516e3 100644 (file)
@@ -34,7 +34,10 @@ 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 *)
 
 #endif