Skip to content

Commit 1c7a387

Browse files
tursulinChristianKoenigAMD
authored andcommitted
drm: Update file owner during use
With the typical model where the display server opens the file descriptor and then hands it over to the client(*), we were showing stale data in debugfs. Fix it by updating the drm_file->pid on ioctl access from a different process. The field is also made RCU protected to allow for lockless readers. Update side is protected with dev->filelist_mutex. Before: $ cat /sys/kernel/debug/dri/0/clients command pid dev master a uid magic Xorg 2344 0 y y 0 0 Xorg 2344 0 n y 0 2 Xorg 2344 0 n y 0 3 Xorg 2344 0 n y 0 4 After: $ cat /sys/kernel/debug/dri/0/clients command tgid dev master a uid magic Xorg 830 0 y y 0 0 xfce4-session 880 0 n y 0 1 xfwm4 943 0 n y 0 2 neverball 1095 0 n y 0 3 *) More detailed and historically accurate description of various handover implementation kindly provided by Emil Velikov: """ The traditional model, the server was the orchestrator managing the primary device node. From the fd, to the master status and authentication. But looking at the fd alone, this has varied across the years. IIRC in the DRI1 days, Xorg (libdrm really) would have a list of open fd(s) and reuse those whenever needed, DRI2 the client was responsible for open() themselves and with DRI3 the fd was passed to the client. Around the inception of DRI3 and systemd-logind, the latter became another possible orchestrator. Whereby Xorg and Wayland compositors could ask it for the fd. For various reasons (hysterical and genuine ones) Xorg has a fallback path going the open(), whereas Wayland compositors are moving to solely relying on logind... some never had fallback even. Over the past few years, more projects have emerged which provide functionality similar (be that on API level, Dbus, or otherwise) to systemd-logind. """ v2: * Fixed typo in commit text and added a fine historical explanation from Emil. Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Cc: "Christian König" <christian.koenig@amd.com> Cc: Daniel Vetter <daniel@ffwll.ch> Acked-by: Christian König <christian.koenig@amd.com> Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com> Reviewed-by: Rob Clark <robdclark@gmail.com> Tested-by: Rob Clark <robdclark@gmail.com> Link: https://patchwork.freedesktop.org/patch/msgid/20230621094824.2348732-1-tvrtko.ursulin@linux.intel.com Signed-off-by: Christian König <christian.koenig@amd.com>
1 parent 9fc75c4 commit 1c7a387

File tree

8 files changed

+71
-15
lines changed

8 files changed

+71
-15
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
958958
list_for_each_entry(file, &dev->filelist, lhead) {
959959
struct task_struct *task;
960960
struct drm_gem_object *gobj;
961+
struct pid *pid;
961962
int id;
962963

963964
/*
@@ -967,8 +968,9 @@ static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
967968
* Therefore, we need to protect this ->comm access using RCU.
968969
*/
969970
rcu_read_lock();
970-
task = pid_task(file->pid, PIDTYPE_TGID);
971-
seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
971+
pid = rcu_dereference(file->pid);
972+
task = pid_task(pid, PIDTYPE_TGID);
973+
seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
972974
task ? task->comm : "<unknown>");
973975
rcu_read_unlock();
974976

drivers/gpu/drm/drm_auth.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ static int drm_new_set_master(struct drm_device *dev, struct drm_file *fpriv)
235235
static int
236236
drm_master_check_perm(struct drm_device *dev, struct drm_file *file_priv)
237237
{
238-
if (file_priv->pid == task_pid(current) && file_priv->was_master)
238+
if (file_priv->was_master &&
239+
rcu_access_pointer(file_priv->pid) == task_pid(current))
239240
return 0;
240241

241242
if (!capable(CAP_SYS_ADMIN))

drivers/gpu/drm/drm_debugfs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ static int drm_clients_info(struct seq_file *m, void *data)
9292
*/
9393
mutex_lock(&dev->filelist_mutex);
9494
list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
95-
struct task_struct *task;
9695
bool is_current_master = drm_is_current_master(priv);
96+
struct task_struct *task;
97+
struct pid *pid;
9798

98-
rcu_read_lock(); /* locks pid_task()->comm */
99-
task = pid_task(priv->pid, PIDTYPE_TGID);
99+
rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
100+
pid = rcu_dereference(priv->pid);
101+
task = pid_task(pid, PIDTYPE_TGID);
100102
uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
101103
seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n",
102104
task ? task->comm : "<unknown>",
103-
pid_vnr(priv->pid),
105+
pid_vnr(pid),
104106
priv->minor->index,
105107
is_current_master ? 'y' : 'n',
106108
priv->authenticated ? 'y' : 'n',

drivers/gpu/drm/drm_file.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
160160

161161
/* Get a unique identifier for fdinfo: */
162162
file->client_id = atomic64_inc_return(&ident);
163-
file->pid = get_pid(task_tgid(current));
163+
rcu_assign_pointer(file->pid, get_pid(task_tgid(current)));
164164
file->minor = minor;
165165

166166
/* for compatibility root is always authenticated */
@@ -200,7 +200,7 @@ struct drm_file *drm_file_alloc(struct drm_minor *minor)
200200
drm_syncobj_release(file);
201201
if (drm_core_check_feature(dev, DRIVER_GEM))
202202
drm_gem_release(dev, file);
203-
put_pid(file->pid);
203+
put_pid(rcu_access_pointer(file->pid));
204204
kfree(file);
205205

206206
return ERR_PTR(ret);
@@ -291,7 +291,7 @@ void drm_file_free(struct drm_file *file)
291291

292292
WARN_ON(!list_empty(&file->event_list));
293293

294-
put_pid(file->pid);
294+
put_pid(rcu_access_pointer(file->pid));
295295
kfree(file);
296296
}
297297

@@ -505,6 +505,40 @@ int drm_release(struct inode *inode, struct file *filp)
505505
}
506506
EXPORT_SYMBOL(drm_release);
507507

508+
void drm_file_update_pid(struct drm_file *filp)
509+
{
510+
struct drm_device *dev;
511+
struct pid *pid, *old;
512+
513+
/*
514+
* Master nodes need to keep the original ownership in order for
515+
* drm_master_check_perm to keep working correctly. (See comment in
516+
* drm_auth.c.)
517+
*/
518+
if (filp->was_master)
519+
return;
520+
521+
pid = task_tgid(current);
522+
523+
/*
524+
* Quick unlocked check since the model is a single handover followed by
525+
* exclusive repeated use.
526+
*/
527+
if (pid == rcu_access_pointer(filp->pid))
528+
return;
529+
530+
dev = filp->minor->dev;
531+
mutex_lock(&dev->filelist_mutex);
532+
old = rcu_replace_pointer(filp->pid, pid, 1);
533+
mutex_unlock(&dev->filelist_mutex);
534+
535+
if (pid != old) {
536+
get_pid(pid);
537+
synchronize_rcu();
538+
put_pid(old);
539+
}
540+
}
541+
508542
/**
509543
* drm_release_noglobal - release method for DRM file
510544
* @inode: device inode

drivers/gpu/drm/drm_ioctl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ long drm_ioctl_kernel(struct file *file, drm_ioctl_t *func, void *kdata,
776776
struct drm_device *dev = file_priv->minor->dev;
777777
int retcode;
778778

779+
/* Update drm_file owner if fd was passed along. */
780+
drm_file_update_pid(file_priv);
781+
779782
if (drm_dev_is_unplugged(dev))
780783
return -ENODEV;
781784

drivers/gpu/drm/nouveau/nouveau_drm.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,10 @@ nouveau_drm_open(struct drm_device *dev, struct drm_file *fpriv)
11331133
}
11341134

11351135
get_task_comm(tmpname, current);
1136-
snprintf(name, sizeof(name), "%s[%d]", tmpname, pid_nr(fpriv->pid));
1136+
rcu_read_lock();
1137+
snprintf(name, sizeof(name), "%s[%d]",
1138+
tmpname, pid_nr(rcu_dereference(fpriv->pid)));
1139+
rcu_read_unlock();
11371140

11381141
if (!(cli = kzalloc(sizeof(*cli), GFP_KERNEL))) {
11391142
ret = -ENOMEM;

drivers/gpu/drm/vmwgfx/vmwgfx_gem.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
232232
list_for_each_entry(file, &dev->filelist, lhead) {
233233
struct task_struct *task;
234234
struct drm_gem_object *gobj;
235+
struct pid *pid;
235236
int id;
236237

237238
/*
@@ -241,8 +242,9 @@ static int vmw_debugfs_gem_info_show(struct seq_file *m, void *unused)
241242
* Therefore, we need to protect this ->comm access using RCU.
242243
*/
243244
rcu_read_lock();
244-
task = pid_task(file->pid, PIDTYPE_TGID);
245-
seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
245+
pid = rcu_dereference(file->pid);
246+
task = pid_task(pid, PIDTYPE_TGID);
247+
seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
246248
task ? task->comm : "<unknown>");
247249
rcu_read_unlock();
248250

include/drm/drm_file.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,15 @@ struct drm_file {
254254
/** @master_lookup_lock: Serializes @master. */
255255
spinlock_t master_lookup_lock;
256256

257-
/** @pid: Process that opened this file. */
258-
struct pid *pid;
257+
/**
258+
* @pid: Process that is using this file.
259+
*
260+
* Must only be dereferenced under a rcu_read_lock or equivalent.
261+
*
262+
* Updates are guarded with dev->filelist_mutex and reference must be
263+
* dropped after a RCU grace period to accommodate lockless readers.
264+
*/
265+
struct pid __rcu *pid;
259266

260267
/** @client_id: A unique id for fdinfo */
261268
u64 client_id;
@@ -418,6 +425,8 @@ static inline bool drm_is_accel_client(const struct drm_file *file_priv)
418425
return file_priv->minor->type == DRM_MINOR_ACCEL;
419426
}
420427

428+
void drm_file_update_pid(struct drm_file *);
429+
421430
int drm_open(struct inode *inode, struct file *filp);
422431
int drm_open_helper(struct file *filp, struct drm_minor *minor);
423432
ssize_t drm_read(struct file *filp, char __user *buffer,

0 commit comments

Comments
 (0)