Skip to content

Commit c996fd0

Browse files
committed
drm: Protect the master management with a drm_device::master_mutex v3
The master management was previously protected by the drm_device::struct_mutex. In order to avoid locking order violations in a reworked dropped master security check in the vmwgfx driver, break it out into a separate master_mutex. Locking order is master_mutex -> struct_mutex. Also remove drm_master::blocked since it's not used. v2: Add an inline comment about what drm_device::master_mutex is protecting. v3: Remove unneeded struct_mutex locks. Fix error returns in drm_setmaster_ioctl(). Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com> Reviewed-by: Brian Paul <brianp@vmware.com> Reviewed-by: David Herrmann <dh.herrmann@gmail.com> Acked-by: Daniel Vetter <daniel@ffwll.ch>
1 parent a12cd00 commit c996fd0

3 files changed

Lines changed: 64 additions & 47 deletions

File tree

‎drivers/gpu/drm/drm_fops.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,50 +231,43 @@ static int drm_open_helper(struct inode *inode, struct file *filp,
231231

232232
/* if there is no current master make this fd it, but do not create
233233
* any master object for render clients */
234-
mutex_lock(&dev->struct_mutex);
234+
mutex_lock(&dev->master_mutex);
235235
if (drm_is_primary_client(priv) && !priv->minor->master) {
236236
/* create a new master */
237237
priv->minor->master = drm_master_create(priv->minor);
238238
if (!priv->minor->master) {
239-
mutex_unlock(&dev->struct_mutex);
240239
ret = -ENOMEM;
241240
goto out_close;
242241
}
243242

244243
priv->is_master = 1;
245244
/* take another reference for the copy in the local file priv */
246245
priv->master = drm_master_get(priv->minor->master);
247-
248246
priv->authenticated = 1;
249247

250-
mutex_unlock(&dev->struct_mutex);
251248
if (dev->driver->master_create) {
252249
ret = dev->driver->master_create(dev, priv->master);
253250
if (ret) {
254-
mutex_lock(&dev->struct_mutex);
255251
/* drop both references if this fails */
256252
drm_master_put(&priv->minor->master);
257253
drm_master_put(&priv->master);
258-
mutex_unlock(&dev->struct_mutex);
259254
goto out_close;
260255
}
261256
}
262-
mutex_lock(&dev->struct_mutex);
263257
if (dev->driver->master_set) {
264258
ret = dev->driver->master_set(dev, priv, true);
265259
if (ret) {
266260
/* drop both references if this fails */
267261
drm_master_put(&priv->minor->master);
268262
drm_master_put(&priv->master);
269-
mutex_unlock(&dev->struct_mutex);
270263
goto out_close;
271264
}
272265
}
273266
} else if (drm_is_primary_client(priv)) {
274267
/* get a reference to the master */
275268
priv->master = drm_master_get(priv->minor->master);
276269
}
277-
mutex_unlock(&dev->struct_mutex);
270+
mutex_unlock(&dev->master_mutex);
278271

279272
mutex_lock(&dev->struct_mutex);
280273
list_add(&priv->lhead, &dev->filelist);
@@ -302,6 +295,7 @@ static int drm_open_helper(struct inode *inode, struct file *filp,
302295
return 0;
303296

304297
out_close:
298+
mutex_unlock(&dev->master_mutex);
305299
if (dev->driver->postclose)
306300
dev->driver->postclose(dev, priv);
307301
out_prime_destroy:
@@ -489,11 +483,13 @@ int drm_release(struct inode *inode, struct file *filp)
489483
}
490484
mutex_unlock(&dev->ctxlist_mutex);
491485

492-
mutex_lock(&dev->struct_mutex);
486+
mutex_lock(&dev->master_mutex);
493487

494488
if (file_priv->is_master) {
495489
struct drm_master *master = file_priv->master;
496490
struct drm_file *temp;
491+
492+
mutex_lock(&dev->struct_mutex);
497493
list_for_each_entry(temp, &dev->filelist, lhead) {
498494
if ((temp->master == file_priv->master) &&
499495
(temp != file_priv))
@@ -512,6 +508,7 @@ int drm_release(struct inode *inode, struct file *filp)
512508
master->lock.file_priv = NULL;
513509
wake_up_interruptible_all(&master->lock.lock_queue);
514510
}
511+
mutex_unlock(&dev->struct_mutex);
515512

516513
if (file_priv->minor->master == file_priv->master) {
517514
/* drop the reference held my the minor */
@@ -521,10 +518,13 @@ int drm_release(struct inode *inode, struct file *filp)
521518
}
522519
}
523520

524-
/* drop the reference held my the file priv */
521+
/* drop the master reference held by the file priv */
525522
if (file_priv->master)
526523
drm_master_put(&file_priv->master);
527524
file_priv->is_master = 0;
525+
mutex_unlock(&dev->master_mutex);
526+
527+
mutex_lock(&dev->struct_mutex);
528528
list_del(&file_priv->lhead);
529529
mutex_unlock(&dev->struct_mutex);
530530

‎drivers/gpu/drm/drm_stub.c‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static void drm_master_destroy(struct kref *kref)
144144
struct drm_device *dev = master->minor->dev;
145145
struct drm_map_list *r_list, *list_temp;
146146

147+
mutex_lock(&dev->struct_mutex);
147148
if (dev->driver->master_destroy)
148149
dev->driver->master_destroy(dev, master);
149150

@@ -171,6 +172,7 @@ static void drm_master_destroy(struct kref *kref)
171172

172173
drm_ht_remove(&master->magiclist);
173174

175+
mutex_unlock(&dev->struct_mutex);
174176
kfree(master);
175177
}
176178

@@ -186,19 +188,20 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data,
186188
{
187189
int ret = 0;
188190

191+
mutex_lock(&dev->master_mutex);
189192
if (file_priv->is_master)
190-
return 0;
193+
goto out_unlock;
191194

192-
if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
193-
return -EINVAL;
194-
195-
if (!file_priv->master)
196-
return -EINVAL;
195+
if (file_priv->minor->master) {
196+
ret = -EINVAL;
197+
goto out_unlock;
198+
}
197199

198-
if (file_priv->minor->master)
199-
return -EINVAL;
200+
if (!file_priv->master) {
201+
ret = -EINVAL;
202+
goto out_unlock;
203+
}
200204

201-
mutex_lock(&dev->struct_mutex);
202205
file_priv->minor->master = drm_master_get(file_priv->master);
203206
file_priv->is_master = 1;
204207
if (dev->driver->master_set) {
@@ -208,27 +211,33 @@ int drm_setmaster_ioctl(struct drm_device *dev, void *data,
208211
drm_master_put(&file_priv->minor->master);
209212
}
210213
}
211-
mutex_unlock(&dev->struct_mutex);
212214

215+
out_unlock:
216+
mutex_unlock(&dev->master_mutex);
213217
return ret;
214218
}
215219

216220
int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
217221
struct drm_file *file_priv)
218222
{
223+
int ret = -EINVAL;
224+
225+
mutex_lock(&dev->master_mutex);
219226
if (!file_priv->is_master)
220-
return -EINVAL;
227+
goto out_unlock;
221228

222229
if (!file_priv->minor->master)
223-
return -EINVAL;
230+
goto out_unlock;
224231

225-
mutex_lock(&dev->struct_mutex);
232+
ret = 0;
226233
if (dev->driver->master_drop)
227234
dev->driver->master_drop(dev, file_priv, false);
228235
drm_master_put(&file_priv->minor->master);
229236
file_priv->is_master = 0;
230-
mutex_unlock(&dev->struct_mutex);
231-
return 0;
237+
238+
out_unlock:
239+
mutex_unlock(&dev->master_mutex);
240+
return ret;
232241
}
233242

234243
/*
@@ -559,6 +568,7 @@ struct drm_device *drm_dev_alloc(struct drm_driver *driver,
559568
spin_lock_init(&dev->event_lock);
560569
mutex_init(&dev->struct_mutex);
561570
mutex_init(&dev->ctxlist_mutex);
571+
mutex_init(&dev->master_mutex);
562572

563573
dev->anon_inode = drm_fs_inode_new();
564574
if (IS_ERR(dev->anon_inode)) {
@@ -612,6 +622,7 @@ struct drm_device *drm_dev_alloc(struct drm_driver *driver,
612622
drm_minor_free(dev, DRM_MINOR_CONTROL);
613623
drm_fs_inode_free(dev->anon_inode);
614624
err_free:
625+
mutex_destroy(&dev->master_mutex);
615626
kfree(dev);
616627
return NULL;
617628
}
@@ -633,6 +644,8 @@ static void drm_dev_release(struct kref *ref)
633644
drm_minor_free(dev, DRM_MINOR_CONTROL);
634645

635646
kfree(dev->devname);
647+
648+
mutex_destroy(&dev->master_mutex);
636649
kfree(dev);
637650
}
638651

‎include/drm/drmP.h‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ struct drm_prime_file_private {
405405
struct drm_file {
406406
unsigned always_authenticated :1;
407407
unsigned authenticated :1;
408-
unsigned is_master :1; /* this file private is a master for a minor */
408+
/* Whether we're master for a minor. Protected by master_mutex */
409+
unsigned is_master :1;
409410
/* true when the client has asked us to expose stereo 3D mode flags */
410411
unsigned stereo_allowed :1;
411412

@@ -684,28 +685,29 @@ struct drm_gem_object {
684685

685686
#include <drm/drm_crtc.h>
686687

687-
/* per-master structure */
688+
/**
689+
* struct drm_master - drm master structure
690+
*
691+
* @refcount: Refcount for this master object.
692+
* @minor: Link back to minor char device we are master for. Immutable.
693+
* @unique: Unique identifier: e.g. busid. Protected by drm_global_mutex.
694+
* @unique_len: Length of unique field. Protected by drm_global_mutex.
695+
* @unique_size: Amount allocated. Protected by drm_global_mutex.
696+
* @magiclist: Hash of used authentication tokens. Protected by struct_mutex.
697+
* @magicfree: List of used authentication tokens. Protected by struct_mutex.
698+
* @lock: DRI lock information.
699+
* @driver_priv: Pointer to driver-private information.
700+
*/
688701
struct drm_master {
689-
690-
struct kref refcount; /* refcount for this master */
691-
692-
struct drm_minor *minor; /**< link back to minor we are a master for */
693-
694-
char *unique; /**< Unique identifier: e.g., busid */
695-
int unique_len; /**< Length of unique field */
696-
int unique_size; /**< amount allocated */
697-
698-
int blocked; /**< Blocked due to VC switch? */
699-
700-
/** \name Authentication */
701-
/*@{ */
702+
struct kref refcount;
703+
struct drm_minor *minor;
704+
char *unique;
705+
int unique_len;
706+
int unique_size;
702707
struct drm_open_hash magiclist;
703708
struct list_head magicfree;
704-
/*@} */
705-
706-
struct drm_lock_data lock; /**< Information on hardware lock */
707-
708-
void *driver_priv; /**< Private structure for driver to use */
709+
struct drm_lock_data lock;
710+
void *driver_priv;
709711
};
710712

711713
/* Size of ringbuffer for vblank timestamps. Just double-buffer
@@ -1020,7 +1022,8 @@ struct drm_minor {
10201022
struct list_head debugfs_list;
10211023
struct mutex debugfs_lock; /* Protects debugfs_list. */
10221024

1023-
struct drm_master *master; /* currently active master for this node */
1025+
/* currently active master for this node. Protected by master_mutex */
1026+
struct drm_master *master;
10241027
struct drm_mode_group mode_group;
10251028
};
10261029

@@ -1070,6 +1073,7 @@ struct drm_device {
10701073
/*@{ */
10711074
spinlock_t count_lock; /**< For inuse, drm_device::open_count, drm_device::buf_use */
10721075
struct mutex struct_mutex; /**< For others */
1076+
struct mutex master_mutex; /**< For drm_minor::master and drm_file::is_master */
10731077
/*@} */
10741078

10751079
/** \name Usage Counters */

0 commit comments

Comments
 (0)