diff mbox

drm/i915: Handle recursive shrinker for vma->last_active allocation

Message ID 20180719072206.16015-1-chris@chris-wilson.co.uk (mailing list archive)
State New, archived
Headers show

Commit Message

Chris Wilson July 19, 2018, 7:22 a.m. UTC
If we call into the shrinker for direct relcaim inside kmalloc, it will
retire the requests. If we retire the vma->last_active while a new
i915_vma_move_to_active() we can upset the delicate bookkeeping required
for the cache. After the possible invocation of the shrinker, we need to
double check the vma->last_active is still valid.

Fixes: 8b293eb53a7d ("drm/i915: Track the last-active inside the i915_vma")
Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 drivers/gpu/drm/i915/i915_vma.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Comments

Tvrtko Ursulin July 19, 2018, 11:18 a.m. UTC | #1
On 19/07/2018 08:22, Chris Wilson wrote:
> If we call into the shrinker for direct relcaim inside kmalloc, it will
> retire the requests. If we retire the vma->last_active while a new
> i915_vma_move_to_active() we can upset the delicate bookkeeping required
> for the cache. After the possible invocation of the shrinker, we need to
> double check the vma->last_active is still valid.
> 
> Fixes: 8b293eb53a7d ("drm/i915: Track the last-active inside the i915_vma")
> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> ---
>   drivers/gpu/drm/i915/i915_vma.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
> index ed4e0fb558f7..11d834f94220 100644
> --- a/drivers/gpu/drm/i915/i915_vma.c
> +++ b/drivers/gpu/drm/i915/i915_vma.c
> @@ -942,6 +942,14 @@ static struct i915_gem_active *active_instance(struct i915_vma *vma, u64 idx)
>   	}
>   
>   	active = kmalloc(sizeof(*active), GFP_KERNEL);
> +
> +	/* kmalloc may retire the vma->last_active request (thanks shrinker)! */
> +	if (unlikely(!i915_gem_active_raw(&vma->last_active,
> +					  &vma->vm->i915->drm.struct_mutex))) {
> +		kfree(active);
> +		goto out;
> +	}
> +
>   	if (unlikely(!active))
>   		return ERR_PTR(-ENOMEM);
>   
> 

Fun!

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Regards,

Tvrtko
Chris Wilson July 19, 2018, 11:36 a.m. UTC | #2
Quoting Tvrtko Ursulin (2018-07-19 12:18:26)
> 
> On 19/07/2018 08:22, Chris Wilson wrote:
> > If we call into the shrinker for direct relcaim inside kmalloc, it will
> > retire the requests. If we retire the vma->last_active while a new
> > i915_vma_move_to_active() we can upset the delicate bookkeeping required
> > for the cache. After the possible invocation of the shrinker, we need to
> > double check the vma->last_active is still valid.
> > 
> > Fixes: 8b293eb53a7d ("drm/i915: Track the last-active inside the i915_vma")
> > Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
> > Cc: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
> > ---
> >   drivers/gpu/drm/i915/i915_vma.c | 8 ++++++++
> >   1 file changed, 8 insertions(+)
> > 
> > diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
> > index ed4e0fb558f7..11d834f94220 100644
> > --- a/drivers/gpu/drm/i915/i915_vma.c
> > +++ b/drivers/gpu/drm/i915/i915_vma.c
> > @@ -942,6 +942,14 @@ static struct i915_gem_active *active_instance(struct i915_vma *vma, u64 idx)
> >       }
> >   
> >       active = kmalloc(sizeof(*active), GFP_KERNEL);
> > +
> > +     /* kmalloc may retire the vma->last_active request (thanks shrinker)! */
> > +     if (unlikely(!i915_gem_active_raw(&vma->last_active,
> > +                                       &vma->vm->i915->drm.struct_mutex))) {
> > +             kfree(active);
> > +             goto out;
> > +     }
> > +
> >       if (unlikely(!active))
> >               return ERR_PTR(-ENOMEM);
> >   
> > 
> 
> Fun!
> 
> Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Pushed to keep CI ticking over.
-Chris
diff mbox

Patch

diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
index ed4e0fb558f7..11d834f94220 100644
--- a/drivers/gpu/drm/i915/i915_vma.c
+++ b/drivers/gpu/drm/i915/i915_vma.c
@@ -942,6 +942,14 @@  static struct i915_gem_active *active_instance(struct i915_vma *vma, u64 idx)
 	}
 
 	active = kmalloc(sizeof(*active), GFP_KERNEL);
+
+	/* kmalloc may retire the vma->last_active request (thanks shrinker)! */
+	if (unlikely(!i915_gem_active_raw(&vma->last_active,
+					  &vma->vm->i915->drm.struct_mutex))) {
+		kfree(active);
+		goto out;
+	}
+
 	if (unlikely(!active))
 		return ERR_PTR(-ENOMEM);