drm: Taint mm->mmap_sem with dev->struct_mutex for lockdep

Submitted by Chris Wilson on March 3, 2016, 12:43 p.m.

Details

Message ID 1457009001-15833-1-git-send-email-chris@chris-wilson.co.uk
State New
Headers show
Series "drm: Taint mm->mmap_sem with dev->struct_mutex for lockdep" ( rev: 1 ) in DRI devel

Not browsing as part of any series.

Commit Message

Chris Wilson March 3, 2016, 12:43 p.m.
The DRM core depends on mm->mmap_sem being the outer lock in order to
setup/teardown and utilize fault handlers. If we taint the mm->mmap_sem
as soon as we open the device for a process, we can define this proper
ordering for lockdep and so it will report any violations early.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
---
 drivers/gpu/drm/drm_fops.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Patch hide | download patch | download mbox

diff --git a/drivers/gpu/drm/drm_fops.c b/drivers/gpu/drm/drm_fops.c
index aeef58ed359b..a95d588c24c8 100644
--- a/drivers/gpu/drm/drm_fops.c
+++ b/drivers/gpu/drm/drm_fops.c
@@ -246,6 +246,19 @@  static int drm_open_helper(struct file *filp, struct drm_minor *minor)
 
 	DRM_DEBUG("pid = %d, minor = %d\n", task_pid_nr(current), minor->index);
 
+	if (IS_ENABLED(CONFIG_LOCKDEP)) {
+		/* We depend upon the strict order of mmap_sem outside of
+		 * struct_mutex in order for the fault handlers and their
+		 * setup/teardown. Taint mmap_sem as early as possible in
+		 * order to define the proper order and flag ABBA errors
+		 * should the order ever be inverted.
+		 */
+		down_write(&current->mm->mmap_sem);
+		mutex_lock(&dev->struct_mutex);
+		mutex_unlock(&dev->struct_mutex);
+		up_write(&current->mm->mmap_sem);
+	}
+
 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;