drm/i915: fix reference counting in i915_gem_create
Subsystems (2)
PATCH 1/2 drm/i915: fix reference counting in i915_gem_create
This function is called without the dev->struct_mutex held, hence we need to use the _unlocked unreference variants.
As soon as the object is registered userspace can sneak in here with a gem_close ioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.
To fix this we simply need to revert
Chris Wilson <[email protected]>
v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.
v3: Just revert the original patch as suggested by Chris Wilson.
--- drivers/gpu/drm/i915/i915_gem.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +6 -9
@@ -219,18 +219,13 @@ i915_gem_create(struct drm_file *file,return -ENOMEM ; ret =drm_gem_handle_create (file, &obj->base, &handle); -if (ret) { -drm_gem_object_release (&obj->base); -i915_gem_info_remove_obj (dev->dev_private, obj->base.size); -i915_gem_object_free (obj); -return ret; - } -/* drop reference from allocate - handle holds it now */ -drm_gem_object_unreference (&obj->base); -trace_i915_gem_object_create (obj); +drm_gem_object_unreference_unlocked (&obj->base); +if (ret) +return ret; *handle_p = handle; +return 0 ; }
@@ -3935,6 +3930,8 @@ struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, }else obj->cache_level =I915_CACHE_NONE ; +trace_i915_gem_object_create (obj); +return obj; }
Signature · 1 line
1.8.1.4
PATCH 2/2 drm/i915: fix the racy object accounting
Use an atomic_t for the count (since that is useful for leak detection in tests) and just rip out the object memory check.
v2: Rebase onto the new object create refcount fix patch.
--- drivers/gpu/drm/i915/i915_debugfs.c | 5 ++--- drivers/gpu/drm/i915/i915_drv.h | 5 ++--- drivers/gpu/drm/i915/i915_gem.c | 21 ++++----------------- 3 files changed, 8 insertions(+), 23 deletions(-)
drivers/gpu/drm/i915/i915_debugfs.c +2 -3
@@ -228,9 +228,8 @@ static int i915_gem_object_info(struct seq_file *m, void *data)if (ret)return ret; -seq_printf (m,"%u objects, %zu bytes\n" , - dev_priv->mm.object_count, - dev_priv->mm.object_memory); +seq_printf (m,"%u objects\n" , +atomic_read (&dev_priv->mm.object_count)); size = count = mappable_size = mappable_count =0 ;count_objects (&dev_priv->mm.bound_list, global_list);
drivers/gpu/drm/i915/i915_drv.h +2 -3
@@ -925,9 +925,8 @@ struct i915_gem_mm {/* storage for physical objects */ struct drm_i915_gem_phys_object *phys_objs[I915_MAX_PHYS_OBJECT ]; -/* accounting, useful for userland debugging */ -size_t object_memory; -u32 object_count; +/* accounting, useful for leak detection in tests */ +atomic_t object_count; };struct drm_i915_error_state_buf {
drivers/gpu/drm/i915/i915_gem.c +4 -17
@@ -71,21 +71,6 @@ static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj) obj->fence_reg =I915_FENCE_REG_NONE ; } -/* some bookkeeping */ -static void i915_gem_info_add_obj (struct drm_i915_private *dev_priv, -size_t size) -{ - dev_priv->mm.object_count++; - dev_priv->mm.object_memory += size; -} - -static void i915_gem_info_remove_obj (struct drm_i915_private *dev_priv, -size_t size) -{ - dev_priv->mm.object_count--; - dev_priv->mm.object_memory -= size; -} -static int i915_gem_wait_for_error (struct i915_gpu_error *error) {
@@ -3861,6 +3846,8 @@ unlock:void i915_gem_object_init (struct drm_i915_gem_object *obj,const struct drm_i915_gem_object_ops *ops) { +struct drm_i915_private *dev_priv = obj->base.dev->dev_private; +INIT_LIST_HEAD (&obj->mm_list);INIT_LIST_HEAD (&obj->global_list);INIT_LIST_HEAD (&obj->ring_list);
@@ -3874,7 +3861,7 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,/* Avoid an unnecessary call to unbind on the first bind. */ obj->map_and_fenceable = true; -i915_gem_info_add_obj (obj->base.dev->dev_private, obj->base.size); +atomic_inc (&dev_priv->mm.object_count); }static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
@@ -3982,7 +3969,7 @@ void i915_gem_free_object(struct drm_gem_object *gem_obj)drm_prime_gem_destroy (&obj->base,NULL );drm_gem_object_release (&obj->base); -i915_gem_info_remove_obj (dev_priv, obj->base.size); +atomic_dec (&dev_priv->mm.object_count);kfree (obj->bit_17);i915_gem_object_free (obj);
Signature · 1 line
1.8.1.4
Re: [PATCH 2/2] drm/i915: fix the racy object accounting
Quoted 7 lines · Daniel Vetter
Use an
atomic_tfor the count (since that is useful for leak detection in tests) and just rip out the object memory check.v2: Rebase onto the new object create refcount fix patch.
Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>
I will miss that summary of total allocation bytes, and would actually like to have the information provided by the patch you reverted earlier. -Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Re: [PATCH 2/2] drm/i915: fix the racy object accounting
Quoted 11 lines · Chris Wilson
Quoted 7 lines · Daniel Vetter
Use an
atomic_tfor the count (since that is useful for leak detection in tests) and just rip out the object memory check.v2: Rebase onto the new object create refcount fix patch.
Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>I will miss that summary of total allocation bytes, and would actually like to have the information provided by the patch you reverted earlier.
We already dump both the bound and unbound list, and imo adding those up and claiming is everything is too misleading. I guess I could do a spinlock around the object_memory instead, but since I only cared about the object count I didn't bother.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
PATCH 1/1 drm/i915: fix the racy object accounting
In reply to Chris WilsonJust use a spinlock to protect them.
v2: Rebase onto the new object create refcount fix patch.
v3: Don't kill dev_priv->mm.object_memory as requested by Chris and hence just use a spinlock instead of atomic_t.
--- drivers/gpu/drm/i915/i915_dma.c | 1 + drivers/gpu/drm/i915/i915_drv.h | 1 + drivers/gpu/drm/i915/i915_gem.c | 4 ++++ 3 files changed, 6 insertions(+)
drivers/gpu/drm/i915/i915_dma.c +1 -0
@@ -1493,6 +1493,7 @@ int i915_driver_load(struct drm_device *dev, unsigned long flags)spin_lock_init (&dev_priv->irq_lock);spin_lock_init (&dev_priv->gpu_error.lock);spin_lock_init (&dev_priv->backlight.lock); +spin_lock_init (&dev_priv->mm.object_stat_lock);mutex_init (&dev_priv->dpio_lock);mutex_init (&dev_priv->rps.hw_lock);mutex_init (&dev_priv->modeset_restore_lock);
drivers/gpu/drm/i915/i915_drv.h +1 -0
@@ -926,6 +926,7 @@ struct i915_gem_mm {struct drm_i915_gem_phys_object *phys_objs[I915_MAX_PHYS_OBJECT ];/* accounting, useful for userland debugging */ +spinlock_t object_stat_lock;size_t object_memory;u32 object_count; };
drivers/gpu/drm/i915/i915_gem.c +4 -0
@@ -75,15 +75,19 @@ static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)static void i915_gem_info_add_obj (struct drm_i915_private *dev_priv,size_t size) { +spin_lock (&dev_priv->mm.object_stat_lock); dev_priv->mm.object_count++; dev_priv->mm.object_memory += size; +spin_unlock (&dev_priv->mm.object_stat_lock); }static void i915_gem_info_remove_obj (struct drm_i915_private *dev_priv,size_t size) { +spin_lock (&dev_priv->mm.object_stat_lock); dev_priv->mm.object_count--; dev_priv->mm.object_memory -= size; +spin_unlock (&dev_priv->mm.object_stat_lock); } static int
Signature · 1 line
1.8.1.4
Re: [PATCH] drm/i915: fix the racy object accounting
Quoted 9 lines · Daniel Vetter
Just use a spinlock to protect them.
v2: Rebase onto the new object create refcount fix patch.
v3: Don't kill
dev_priv->mm.object_memoryas requested by Chris and hence just use a spinlock instead ofatomic_t.Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>
Sadly, I have no better answer to my desire have my cake and eat it.
Now, I guess to pester Ben to make sure there is adequate^Wsuperficial per-vma, per-ctx, per-file accounting.
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Daniel Vetter
AppliedRe: [PATCH] drm/i915: fix the racy object accounting
Quoted 5 lines · Chris Wilson
Quoted 4 lines · Daniel Vetter
Just use a spinlock to protect them.
v2: Rebase onto the new object create refcount fix patch.
Quoted 8 lines
Quoted 5 lines
v3: Don't kill
dev_priv->mm.object_memoryas requested by Chris and hence just use a spinlock instead ofatomic_t.Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>Sadly, I have no better answer to my desire have my cake and eat it.
Reviewed-by Chris Wilson <[email protected]>
Queued for -next, thanks for the review.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch
Quoted 12 lines · Daniel Vetter
This function is called without the
dev->struct_mutexheld, hence we need to use the_unlockedunreference variants.As soon as the object is registered userspace can sneak in here with a
gem_closeioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.To fix this we simply need to revert
Chris Wilson <[email protected]>
Quoted 7 lines
v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.
v3: Just revert the original patch as suggested by Chris Wilson.
Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>
I'm sad to see this go, but we can fix any potential mutex contention here by generically fixing _unlocked() - which is the better solution.
Quoted 19 lines
--- drivers/gpu/drm/i915/i915_gem.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)drivers/gpu/drm/i915/i915_gem.c +0
-7@@ -219,18 +219,13 @@ i915_gem_create(struct drm_file *file,return -ENOMEM ; ret =drm_gem_handle_create (file, &obj->base, &handle); -if (ret) { -drm_gem_object_release (&obj->base); -i915_gem_info_remove_obj (dev->dev_private, obj->base.size); -i915_gem_object_free (obj); -return ret; - } -
drivers/gpu/drm/i915/i915_gem.c i915_gem_create(struct drm_file *file, · line 229
Quoted 10 lines
drivers/gpu/drm/i915/i915_gem.c +4
-2/* drop reference from allocate - handle holds it now */ -drm_gem_object_unreference (&obj->base); -trace_i915_gem_object_create (obj); +drm_gem_object_unreference_unlocked (&obj->base); +if (ret) +return ret; *handle_p = handle; +return 0 ;
However, I do like my return parameters clustered - any chance this extra whitespace can be erradicated?
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Quoted 52 lines · Chris Wilson
Quoted 12 lines · Daniel Vetter
This function is called without the
dev->struct_mutexheld, hence we need to use the_unlockedunreference variants.As soon as the object is registered userspace can sneak in here with a
gem_closeioctl call, so the object can (and with my new evil tests actually does) get the final unreference in this place. The lack of locking then results in hilarity and some good leakage.To fix this we simply need to revert
Chris Wilson <[email protected]>
Quoted 7 lines
v2: We need to make the trace call _before_ we drop our ref - the object might very well be gone by then already.
v3: Just revert the original patch as suggested by Chris Wilson.
Cc Chris Wilson <[email protected]>Signed-off-by Daniel Vetter <[email protected]>I'm sad to see this go, but we can fix any potential mutex contention here by generically fixing
_unlocked()- which is the better solution.Reviewed-by Chris Wilson <[email protected]>Quoted 26 lines
--- drivers/gpu/drm/i915/i915_gem.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)drivers/gpu/drm/i915/i915_gem.c +3
-9@@ -219,18 +219,13 @@ i915_gem_create(struct drm_file *file,return -ENOMEM ; ret =drm_gem_handle_create (file, &obj->base, &handle); -if (ret) { -drm_gem_object_release (&obj->base); -i915_gem_info_remove_obj (dev->dev_private, obj->base.size); -i915_gem_object_free (obj); -return ret; - } -/* drop reference from allocate - handle holds it now */ -drm_gem_object_unreference (&obj->base); -trace_i915_gem_object_create (obj); +drm_gem_object_unreference_unlocked (&obj->base); +if (ret) +return ret;
Quoted 6 lines
drivers/gpu/drm/i915/i915_gem.c i915_gem_create(struct drm_file *file, · line 229
Quoted 3 lines
drivers/gpu/drm/i915/i915_gem.c +1
-0*handle_p = handle; +return 0 ;However, I do like my return parameters clustered - any chance this extra whitespace can be erradicated?
Done and queued for -next, thanks for the review.
-Daniel
Signature · 3 lines
Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch