drm/i915: Introduce i915_address_space.mutex
authorChris Wilson <chris@chris-wilson.co.uk>
Wed, 11 Jul 2018 07:36:02 +0000 (08:36 +0100)
committerChris Wilson <chris@chris-wilson.co.uk>
Wed, 11 Jul 2018 12:56:16 +0000 (13:56 +0100)
Add a mutex into struct i915_address_space to be used while operating on
the vma and their lists for a particular vm. As this may be called from
the shrinker, we taint the mutex with fs_reclaim so that from the start
lockdep warns us if we are caught holding the mutex across an
allocation. (With such small steps we will eventually rid ourselves of
struct_mutex recursion!)

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: https://patchwork.freedesktop.org/patch/msgid/20180711073608.20286-2-chris@chris-wilson.co.uk
drivers/gpu/drm/i915/i915_drv.h
drivers/gpu/drm/i915/i915_gem_gtt.c
drivers/gpu/drm/i915/i915_gem_gtt.h
drivers/gpu/drm/i915/i915_gem_shrinker.c

index a90a37c..48b3501 100644 (file)
@@ -3307,7 +3307,7 @@ unsigned long i915_gem_shrink(struct drm_i915_private *i915,
 unsigned long i915_gem_shrink_all(struct drm_i915_private *i915);
 void i915_gem_shrinker_register(struct drm_i915_private *i915);
 void i915_gem_shrinker_unregister(struct drm_i915_private *i915);
-
+void i915_gem_shrinker_taints_mutex(struct mutex *mutex);
 
 /* i915_gem_tiling.c */
 static inline bool i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
index 210baf3..502353b 100644 (file)
@@ -531,6 +531,14 @@ static void vm_free_page(struct i915_address_space *vm, struct page *page)
 static void i915_address_space_init(struct i915_address_space *vm,
                                    struct drm_i915_private *dev_priv)
 {
+       /*
+        * The vm->mutex must be reclaim safe (for use in the shrinker).
+        * Do a dummy acquire now under fs_reclaim so that any allocation
+        * attempt holding the lock is immediately reported by lockdep.
+        */
+       mutex_init(&vm->mutex);
+       i915_gem_shrinker_taints_mutex(&vm->mutex);
+
        GEM_BUG_ON(!vm->total);
        drm_mm_init(&vm->mm, 0, vm->total);
        vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
@@ -551,6 +559,8 @@ static void i915_address_space_fini(struct i915_address_space *vm)
        spin_unlock(&vm->free_pages.lock);
 
        drm_mm_takedown(&vm->mm);
+
+       mutex_destroy(&vm->mutex);
 }
 
 static int __setup_page_dma(struct i915_address_space *vm,
index feda45d..14e6265 100644 (file)
@@ -293,6 +293,8 @@ struct i915_address_space {
 
        bool closed;
 
+       struct mutex mutex; /* protects vma and our lists */
+
        struct i915_page_dma scratch_page;
        struct i915_page_table *scratch_pt;
        struct i915_page_directory *scratch_pd;
index c61f5b8..ea90d3a 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 #include <linux/oom.h>
+#include <linux/sched/mm.h>
 #include <linux/shmem_fs.h>
 #include <linux/slab.h>
 #include <linux/swap.h>
@@ -531,3 +532,14 @@ void i915_gem_shrinker_unregister(struct drm_i915_private *i915)
        WARN_ON(unregister_oom_notifier(&i915->mm.oom_notifier));
        unregister_shrinker(&i915->mm.shrinker);
 }
+
+void i915_gem_shrinker_taints_mutex(struct mutex *mutex)
+{
+       if (!IS_ENABLED(CONFIG_LOCKDEP))
+               return;
+
+       fs_reclaim_acquire(GFP_KERNEL);
+       mutex_lock(mutex);
+       mutex_unlock(mutex);
+       fs_reclaim_release(GFP_KERNEL);
+}