drm/v3d: Add gpu_gem_info node via debugfs
authorSeung-Woo Kim <sw0312.kim@samsung.com>
Mon, 19 Sep 2022 10:35:14 +0000 (19:35 +0900)
committerJaehoon Chung <jh80.chung@samsung.com>
Tue, 12 Nov 2024 04:03:14 +0000 (13:03 +0900)
Add gpu_gem_info node via debugfs of dri debug sysfs for each
process with pid/tgid and its gem objects. Same gem object can be
opened from multiple processes, so from the gpu_gem_info node,
it can print same gem in multiple lines.

Ported from the commit dea4ace78ec7 ("drm/vc4: Add gem_info node
via debugfs") to v3d.

NOTE: From Raspberry Pi 4 board, v3d gpu is card0 and also render node
in dri card128, so accessing can be from both nodes. For render fixed
node, it is /sys/kernel/debug/dri/128/gpu_gem_info.
Also, Raspberry Pi 4 board gives entire gem count and total size with
/sys/kernel/debug/dri/128/bo_stats.

Signed-off-by: Seung-Woo Kim <sw0312.kim@samsung.com>
[mszyprow: ported and adapted to v6.6 kernel release]
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: I0b9cc241d551904a95cafb7531c50ba6e1dd9082

drivers/gpu/drm/v3d/v3d_bo.c
drivers/gpu/drm/v3d/v3d_debugfs.c
drivers/gpu/drm/v3d/v3d_drv.c
drivers/gpu/drm/v3d/v3d_drv.h

index 99aac01e2bbb96d38c2cb984affa228a8922d905..0b1f4c322f38e77646ff5176c4be6049b9b44a56 100644 (file)
@@ -167,6 +167,46 @@ v3d_prime_import_sg_table(struct drm_device *dev,
        return obj;
 }
 
+static void v3d_gem_register_pid(struct drm_file *file)
+{
+       struct v3d_file_priv *v3d_priv = file->driver_priv;
+       struct drm_v3d_file_private *pid_priv;
+
+       if (!v3d_priv)
+               return;
+
+       pid_priv = &v3d_priv->priv;
+
+       if (!pid_priv->pid && !pid_priv->tgid) {
+               pid_priv->pid = task_pid_nr(current);
+               pid_priv->tgid = task_tgid_nr(current);
+       } else {
+               if (pid_priv->pid != task_pid_nr(current))
+                       DRM_DEBUG_KMS("wrong pid: %ld, %ld\n",
+                                       (unsigned long)pid_priv->pid,
+                                       (unsigned long)task_pid_nr(current));
+               if (pid_priv->tgid != task_tgid_nr(current))
+                       DRM_DEBUG_KMS("wrong tgid: %ld, %ld\n",
+                                       (unsigned long)pid_priv->tgid,
+                                       (unsigned long)task_tgid_nr(current));
+       }
+}
+
+int v3d_drm_gem_prime_fd_to_handle(struct drm_device *dev,
+                                  struct drm_file *file_priv, int prime_fd,
+                                  uint32_t *handle)
+{
+       int ret;
+
+       ret = drm_gem_prime_fd_to_handle(dev, file_priv, prime_fd, handle);
+       if (ret < 0)
+               return ret;
+
+       v3d_gem_register_pid(file_priv);
+
+       return ret;
+}
+
 int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
                        struct drm_file *file_priv)
 {
@@ -186,6 +226,9 @@ int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
        args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT;
 
        ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
+       if (!ret)
+               v3d_gem_register_pid(file_priv);
+
        drm_gem_object_put(&bo->base.base);
 
        return ret;
index b338dec2b68cd3eed1bf46cfde36e9dd4a84cef9..e22b19b44fe8f097900d614e7287384033935ff0 100644 (file)
@@ -338,12 +338,81 @@ static int v3d_measure_clock(struct seq_file *m, void *unused)
        return 0;
 }
 
+struct v3d_gem_info_data {
+       struct drm_file *filp;
+       struct seq_file *m;
+};
+
+static int v3d_gem_one_info(int id, void *ptr, void *data)
+{
+       struct drm_gem_object *obj = (struct drm_gem_object *)ptr;
+       struct v3d_gem_info_data *gem_info_data = data;
+       struct v3d_file_priv *v3d_priv = gem_info_data->filp->driver_priv;
+       struct drm_v3d_file_private *pid_priv = &v3d_priv->priv;
+
+       if (!obj) {
+               DRM_ERROR("failed to get drm_gem_object\n");
+               return -EFAULT;
+       }
+
+       drm_gem_object_get(obj);
+
+       seq_printf(gem_info_data->m,
+                       "%5d\t%5d\t%4d\t%4d\t\t%4d\t0x%08zx\t0x%x\t%4d\t%4d\t\t"
+                       "%4d\t\t0x%p\t%6d\n",
+                       pid_priv->pid,
+                       pid_priv->tgid,
+                       id,
+                       kref_read(&obj->refcount) - 1,
+                       obj->handle_count,
+                       obj->size,
+                       0,
+                       0,
+                       obj->dma_buf ? 1 : 0,
+                       obj->import_attach ? 1 : 0,
+                       obj,
+                       obj->name);
+
+       drm_gem_object_put(obj);
+
+       return 0;
+}
+
+int v3d_debugfs_gem_info(struct seq_file *m, void *data)
+{
+       struct drm_debugfs_entry *entry = m->private;
+       struct drm_device *drm_dev = entry->dev;
+       struct v3d_gem_info_data gem_info_data;
+       struct drm_file *filp;
+
+       gem_info_data.m = m;
+
+       seq_puts(gem_info_data.m,
+                       "pid\ttgid\thandle\trefcount\thcount\tsize\t\tflags\t"
+                       "pfnmap\texport_to_fd\timport_from_fd\tobj_addr\t\t"
+                       "name\n");
+
+       mutex_lock(&drm_dev->struct_mutex);
+       list_for_each_entry(filp, &drm_dev->filelist, lhead) {
+               gem_info_data.filp = filp;
+
+               spin_lock(&filp->table_lock);
+               idr_for_each(&filp->object_idr, v3d_gem_one_info,
+                               &gem_info_data);
+               spin_unlock(&filp->table_lock);
+       }
+       mutex_unlock(&drm_dev->struct_mutex);
+
+       return 0;
+}
+
 static const struct drm_debugfs_info v3d_debugfs_list[] = {
        {"v3d_ident", v3d_v3d_debugfs_ident, 0},
        {"v3d_regs", v3d_v3d_debugfs_regs, 0},
        {"measure_clock", v3d_measure_clock, 0},
        {"bo_stats", v3d_debugfs_bo_stats, 0},
        {"gpu_usage", v3d_debugfs_gpu_usage, 0},
+       {"gpu_gem_info", v3d_debugfs_gem_info, 0},
        {"gpu_pid_usage", v3d_debugfs_gpu_pid_usage, 0},
 };
 
index 1258a47cce344af19e90981a9fd8badb5bb0b0b4..77e634e76267d897646b4511b6393a9bd92e7453 100644 (file)
@@ -174,6 +174,7 @@ static const struct drm_driver v3d_drm_driver = {
 #endif
 
        .gem_create_object = v3d_create_object,
+       .prime_fd_to_handle = v3d_drm_gem_prime_fd_to_handle,
        .gem_prime_import_sg_table = v3d_prime_import_sg_table,
 
        .ioctls = v3d_drm_ioctls,
index c59bc83bcfc0a370b0034524ce78490c8a978062..82de10a9e75ba3aec56226ca303fddca990df049 100644 (file)
@@ -218,6 +218,11 @@ v3d_has_csd(struct v3d_dev *v3d)
 
 #define v3d_to_pdev(v3d) to_platform_device((v3d)->drm.dev)
 
+struct drm_v3d_file_private {
+       pid_t pid;
+       pid_t tgid;
+};
+
 /* The per-fd struct, which tracks the MMU mappings. */
 struct v3d_file_priv {
        struct v3d_dev *v3d;
@@ -228,6 +233,7 @@ struct v3d_file_priv {
        } perfmon;
 
        struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
+       struct drm_v3d_file_private priv;
 };
 
 struct v3d_bo {
@@ -427,6 +433,9 @@ int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
 struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
                                                 struct dma_buf_attachment *attach,
                                                 struct sg_table *sgt);
+int v3d_drm_gem_prime_fd_to_handle(struct drm_device *dev,
+                                  struct drm_file *file_priv, int prime_fd,
+                                  uint32_t *handle);
 
 /* v3d_debugfs.c */
 void v3d_debugfs_init(struct drm_minor *minor);