[RFC,4/8] drm/i915: Update client name on context create
diff mbox series

Message ID 20191219180019.25562-5-tvrtko.ursulin@linux.intel.com
State New
Headers show
Series
  • Per client engine busyness
Related show

Commit Message

Tvrtko Ursulin Dec. 19, 2019, 6 p.m. UTC
From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Some clients have the DRM fd passed to them over a socket by the X server.

Grab the real client and pid when they create their first context and
update the exposed data for more useful enumeration.

v2:
 * Do not leak the pid reference and borrow context idr_lock. (Chris)

v3:
 * More avoiding leaks. (Chris)

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 drivers/gpu/drm/i915/gem/i915_gem_context.c | 36 ++++++++++++++++++---
 drivers/gpu/drm/i915/i915_drv.h             |  6 ++++
 drivers/gpu/drm/i915/i915_gem.c             |  4 +--
 3 files changed, 39 insertions(+), 7 deletions(-)

Patch
diff mbox series

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_context.c b/drivers/gpu/drm/i915/gem/i915_gem_context.c
index b482b2e5f31f..dc3a7856ae22 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_context.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_context.c
@@ -2170,7 +2170,10 @@  int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
 {
 	struct drm_i915_private *i915 = to_i915(dev);
 	struct drm_i915_gem_context_create_ext *args = data;
+	struct drm_i915_file_private *file_priv = file->driver_priv;
+	struct i915_drm_client *client = &file_priv->client;
 	struct create_ext ext_data;
+	struct pid *pid;
 	int ret;
 	u32 id;
 
@@ -2184,16 +2187,36 @@  int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
 	if (ret)
 		return ret;
 
-	ext_data.fpriv = file->driver_priv;
+	pid = get_task_pid(current, PIDTYPE_PID);
+
+	ext_data.fpriv = file_priv;
 	if (client_is_banned(ext_data.fpriv)) {
 		DRM_DEBUG("client %s[%d] banned from creating ctx\n",
-			  current->comm, task_pid_nr(current));
-		return -EIO;
+			  current->comm, pid_nr(pid));
+		ret = -EIO;
+		goto err_pid;
+	}
+
+	/*
+	 * Borrow struct_mutex to protect the client remove-add cycle.
+	 */
+	ret = mutex_lock_interruptible(&dev->struct_mutex);
+	if (ret)
+		goto err_pid;
+	if (client->pid != pid) {
+		__i915_gem_unregister_client(client);
+		ret = __i915_gem_register_client(&i915->clients, client,
+						 current);
 	}
+	mutex_unlock(&dev->struct_mutex);
+	if (ret)
+		goto err_pid;
 
 	ext_data.ctx = i915_gem_create_context(i915, args->flags);
-	if (IS_ERR(ext_data.ctx))
-		return PTR_ERR(ext_data.ctx);
+	if (IS_ERR(ext_data.ctx)) {
+		ret = PTR_ERR(ext_data.ctx);
+		goto err_pid;
+	}
 
 	if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
 		ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
@@ -2215,6 +2238,9 @@  int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
 
 err_ctx:
 	context_close(ext_data.ctx);
+err_pid:
+	put_pid(pid);
+
 	return ret;
 }
 
diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index e1d8361aafd7..514d7d630fce 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -1900,6 +1900,12 @@  void i915_gem_suspend(struct drm_i915_private *dev_priv);
 void i915_gem_suspend_late(struct drm_i915_private *dev_priv);
 void i915_gem_resume(struct drm_i915_private *dev_priv);
 
+int
+__i915_gem_register_client(struct i915_drm_clients *clients,
+			   struct i915_drm_client *client,
+			   struct task_struct *task);
+void __i915_gem_unregister_client(struct i915_drm_client *client);
+
 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file);
 void i915_gem_release(struct drm_device *dev, struct drm_file *file);
 
diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c
index f2d285388b8c..f953d4e20e33 100644
--- a/drivers/gpu/drm/i915/i915_gem.c
+++ b/drivers/gpu/drm/i915/i915_gem.c
@@ -1546,7 +1546,7 @@  show_client_pid(struct device *kdev, struct device_attribute *attr, char *buf)
 		return snprintf(buf, PAGE_SIZE, "-");
 }
 
-static int
+int
 __i915_gem_register_client(struct i915_drm_clients *clients,
 			   struct i915_drm_client *client,
 			   struct task_struct *task)
@@ -1599,7 +1599,7 @@  __i915_gem_register_client(struct i915_drm_clients *clients,
 	return ret;
 }
 
-static void __i915_gem_unregister_client(struct i915_drm_client *client)
+void __i915_gem_unregister_client(struct i915_drm_client *client)
 {
 	struct drm_i915_file_private *fpriv =
 		container_of(client, typeof(*fpriv), client);