[Intel-gfx] [PATCH v9 17/70] drm/i915: Flatten obj->mm.lock

Daniel Vetter daniel at ffwll.ch
Wed Mar 24 11:13:49 UTC 2021


On Tue, Mar 23, 2021 at 04:50:06PM +0100, Maarten Lankhorst wrote:
> With userptr fixed, there is no need for all separate lockdep classes
> now, and we can remove all lockdep tricks used. A trylock in the
> shrinker is all we need now to flatten the locking hierarchy.
> 
> Signed-off-by: Maarten Lankhorst <maarten.lankhorst at linux.intel.com>
> Reviewed-by: Thomas Hellström <thomas.hellstrom at linux.intel.com>

Minor conflict with this one here when applying, due to lack of

https://lore.kernel.org/intel-gfx/20210202154318.19246-1-chris@chris-wilson.co.

It's not a bad idea, but if we do this we really should move this helper
into the right headers, and ideally get linux-mm/-fs folks to ack this as
a good idea. Especially Dave Chinner very much wants to be in the loop for
anything that touches fs_reclaim lockdep annotations.
-Daniel


> ---
>  drivers/gpu/drm/i915/gem/i915_gem_object.c   |  5 +--
>  drivers/gpu/drm/i915/gem/i915_gem_object.h   | 20 ++----------
>  drivers/gpu/drm/i915/gem/i915_gem_pages.c    | 34 ++++++++++----------
>  drivers/gpu/drm/i915/gem/i915_gem_phys.c     |  2 +-
>  drivers/gpu/drm/i915/gem/i915_gem_shrinker.c | 10 +++---
>  drivers/gpu/drm/i915/gem/i915_gem_userptr.c  |  2 +-
>  6 files changed, 27 insertions(+), 46 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.c b/drivers/gpu/drm/i915/gem/i915_gem_object.c
> index 6083b9c14be6..821cb40f8d73 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_object.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.c
> @@ -62,7 +62,7 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
>  			  const struct drm_i915_gem_object_ops *ops,
>  			  struct lock_class_key *key, unsigned flags)
>  {
> -	__mutex_init(&obj->mm.lock, ops->name ?: "obj->mm.lock", key);
> +	mutex_init(&obj->mm.lock);
>  
>  	spin_lock_init(&obj->vma.lock);
>  	INIT_LIST_HEAD(&obj->vma.list);
> @@ -86,9 +86,6 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
>  	mutex_init(&obj->mm.get_page.lock);
>  	INIT_RADIX_TREE(&obj->mm.get_dma_page.radix, GFP_KERNEL | __GFP_NOWARN);
>  	mutex_init(&obj->mm.get_dma_page.lock);
> -
> -	if (IS_ENABLED(CONFIG_LOCKDEP) && i915_gem_object_is_shrinkable(obj))
> -		fs_reclaim_taints_mutex(&obj->mm.lock);
>  }
>  
>  /**
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_object.h b/drivers/gpu/drm/i915/gem/i915_gem_object.h
> index b5af9c440ac5..a0e1c4ff0de4 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_object.h
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_object.h
> @@ -372,27 +372,10 @@ void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
>  int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
>  int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
>  
> -enum i915_mm_subclass { /* lockdep subclass for obj->mm.lock/struct_mutex */
> -	I915_MM_NORMAL = 0,
> -	/*
> -	 * Only used by struct_mutex, when called "recursively" from
> -	 * direct-reclaim-esque. Safe because there is only every one
> -	 * struct_mutex in the entire system.
> -	 */
> -	I915_MM_SHRINKER = 1,
> -	/*
> -	 * Used for obj->mm.lock when allocating pages. Safe because the object
> -	 * isn't yet on any LRU, and therefore the shrinker can't deadlock on
> -	 * it. As soon as the object has pages, obj->mm.lock nests within
> -	 * fs_reclaim.
> -	 */
> -	I915_MM_GET_PAGES = 1,
> -};
> -
>  static inline int __must_check
>  i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
>  {
> -	might_lock_nested(&obj->mm.lock, I915_MM_GET_PAGES);
> +	might_lock(&obj->mm.lock);
>  
>  	if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
>  		return 0;
> @@ -436,6 +419,7 @@ i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
>  }
>  
>  int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj);
> +int __i915_gem_object_put_pages_locked(struct drm_i915_gem_object *obj);
>  void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
>  void i915_gem_object_writeback(struct drm_i915_gem_object *obj);
>  
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_pages.c b/drivers/gpu/drm/i915/gem/i915_gem_pages.c
> index e7d7650072c5..e947d4c0da1f 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_pages.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_pages.c
> @@ -114,7 +114,7 @@ int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
>  {
>  	int err;
>  
> -	err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES);
> +	err = mutex_lock_interruptible(&obj->mm.lock);
>  	if (err)
>  		return err;
>  
> @@ -196,21 +196,13 @@ __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
>  	return pages;
>  }
>  
> -int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
> +int __i915_gem_object_put_pages_locked(struct drm_i915_gem_object *obj)
>  {
>  	struct sg_table *pages;
> -	int err;
>  
>  	if (i915_gem_object_has_pinned_pages(obj))
>  		return -EBUSY;
>  
> -	/* May be called by shrinker from within get_pages() (on another bo) */
> -	mutex_lock(&obj->mm.lock);
> -	if (unlikely(atomic_read(&obj->mm.pages_pin_count))) {
> -		err = -EBUSY;
> -		goto unlock;
> -	}
> -
>  	i915_gem_object_release_mmap_offset(obj);
>  
>  	/*
> @@ -226,14 +218,22 @@ int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
>  	 * get_pages backends we should be better able to handle the
>  	 * cancellation of the async task in a more uniform manner.
>  	 */
> -	if (!pages)
> -		pages = ERR_PTR(-EINVAL);
> -
> -	if (!IS_ERR(pages))
> +	if (!IS_ERR_OR_NULL(pages))
>  		obj->ops->put_pages(obj, pages);
>  
> -	err = 0;
> -unlock:
> +	return 0;
> +}
> +
> +int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
> +{
> +	int err;
> +
> +	if (i915_gem_object_has_pinned_pages(obj))
> +		return -EBUSY;
> +
> +	/* May be called by shrinker from within get_pages() (on another bo) */
> +	mutex_lock(&obj->mm.lock);
> +	err = __i915_gem_object_put_pages_locked(obj);
>  	mutex_unlock(&obj->mm.lock);
>  
>  	return err;
> @@ -341,7 +341,7 @@ void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
>  	    !i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_IOMEM))
>  		return ERR_PTR(-ENXIO);
>  
> -	err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES);
> +	err = mutex_lock_interruptible(&obj->mm.lock);
>  	if (err)
>  		return ERR_PTR(err);
>  
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_phys.c b/drivers/gpu/drm/i915/gem/i915_gem_phys.c
> index 06c481ff79d8..44329c435cf1 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_phys.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_phys.c
> @@ -236,7 +236,7 @@ int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
>  	if (err)
>  		return err;
>  
> -	err = mutex_lock_interruptible_nested(&obj->mm.lock, I915_MM_GET_PAGES);
> +	err = mutex_lock_interruptible(&obj->mm.lock);
>  	if (err)
>  		goto err_unlock;
>  
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shrinker.c b/drivers/gpu/drm/i915/gem/i915_gem_shrinker.c
> index b64a0788381f..3052ef5ad89d 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_shrinker.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_shrinker.c
> @@ -49,9 +49,9 @@ static bool unsafe_drop_pages(struct drm_i915_gem_object *obj,
>  		flags = I915_GEM_OBJECT_UNBIND_TEST;
>  
>  	if (i915_gem_object_unbind(obj, flags) == 0)
> -		__i915_gem_object_put_pages(obj);
> +		return true;
>  
> -	return !i915_gem_object_has_pages(obj);
> +	return false;
>  }
>  
>  static void try_to_writeback(struct drm_i915_gem_object *obj,
> @@ -200,10 +200,10 @@ i915_gem_shrink(struct drm_i915_private *i915,
>  
>  			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
>  
> -			if (unsafe_drop_pages(obj, shrink)) {
> +			if (unsafe_drop_pages(obj, shrink) &&
> +			    mutex_trylock(&obj->mm.lock)) {
>  				/* May arrive from get_pages on another bo */
> -				mutex_lock(&obj->mm.lock);
> -				if (!i915_gem_object_has_pages(obj)) {
> +				if (!__i915_gem_object_put_pages_locked(obj)) {
>  					try_to_writeback(obj, shrink);
>  					count += obj->base.size >> PAGE_SHIFT;
>  				}
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_userptr.c b/drivers/gpu/drm/i915/gem/i915_gem_userptr.c
> index 1e42fbc68697..503325e74eff 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_userptr.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_userptr.c
> @@ -253,7 +253,7 @@ static int i915_gem_object_userptr_unbind(struct drm_i915_gem_object *obj, bool
>  	if (GEM_WARN_ON(i915_gem_object_has_pinned_pages(obj)))
>  		return -EBUSY;
>  
> -	mutex_lock_nested(&obj->mm.lock, I915_MM_GET_PAGES);
> +	mutex_lock(&obj->mm.lock);
>  
>  	pages = __i915_gem_object_unset_pages(obj);
>  	if (!IS_ERR_OR_NULL(pages))
> -- 
> 2.31.0
> 
> _______________________________________________
> Intel-gfx mailing list
> Intel-gfx at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/intel-gfx

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch


More information about the Intel-gfx mailing list