drm/vc4: Hold a GEM reference while purging userspace BOs
Subsystems (4)
PATCH 1/1 drm/vc4: Hold a GEM reference while purging userspace BOs
vc4_bo_userspace_cache_purge() removes a BO from the purgeable list and then drops the list lock before taking the BO madvise lock. A concurrent final GEM put can therefore release the BO while the purge path still holds only a raw pointer.
Acquire a non-zero GEM reference while the purgeable list lock still protects the lookup. If final release has already started, drop the list lock so the release path can remove the entry and retry. Put the temporary reference after dropping the madvise lock to avoid recursing into final release while that lock is held.
--- drivers/gpu/drm/vc4/vc4_bo.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
drivers/gpu/drm/vc4/vc4_bo.c +14 -0
@@ -319,6 +319,19 @@ static void vc4_bo_userspace_cache_purge(struct drm_device *dev)struct drm_gem_object *obj = &bo->base.base;size_t purged_size =0 ; +/* +* The last GEM reference can be dropped concurrently. Its free +* callback removes the BO from this list before releasing it, so +* the list lock makes an unless-zero get safe. Never carry only a +* raw list pointer across the lock drop below. +*/ +if (!kref_get_unless_zero (&obj->refcount)) { +mutex_unlock (&vc4->purgeable.lock); +cond_resched (); +mutex_lock (&vc4->purgeable.lock); +continue ; + } +vc4_bo_remove_from_purgeable_pool_locked (bo);/* Release the purgeable lock while we're purging the BO so
@@ -345,6 +358,7 @@ static void vc4_bo_userspace_cache_purge(struct drm_device *dev)vc4_bo_purge (obj); }mutex_unlock (&bo->madv_lock); +drm_gem_object_put (obj);mutex_lock (&vc4->purgeable.lock);if (purged_size) {
Maíra Canal
reviewerHi Kazuki,
Quoted 28 lines · Kazuki Hanai
vc4_bo_userspace_cache_purge()removes a BO from the purgeable list and then drops the list lock before taking the BO madvise lock. A concurrent final GEM put can therefore release the BO while the purge path still holds only a raw pointer.Acquire a non-zero GEM reference while the purgeable list lock still protects the lookup. If final release has already started, drop the list lock so the release path can remove the entry and retry. Put the temporary reference after dropping the madvise lock to avoid recursing into final release while that lock is held.
Fixes b9f19259b84d ("drm/vc4: Add the DRM_IOCTL_VC4_GEM_MADVISE ioctl")Signed-off-by Kazuki Hanai <[email protected]>--- drivers/gpu/drm/vc4/vc4_bo.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)drivers/gpu/drm/vc4/vc4_bo.c +2
-0@@ -319,6 +319,19 @@ static void vc4_bo_userspace_cache_purge(struct drm_device *dev)struct drm_gem_object *obj = &bo->base.base;size_t purged_size =0 ; +/* +* The last GEM reference can be dropped concurrently. Its free
drivers/gpu/drm/vc4/vc4_bo.c static void vc4_bo_userspace_cache_purge(struct drm_device *dev) · line 333
Quoted 10 lines
drivers/gpu/drm/vc4/vc4_bo.c +10
-0+* callback removes the BO from this list before releasing it, so +* the list lock makes an unless-zero get safe. Never carry only a +* raw list pointer across the lock drop below. +*/ +if (!kref_get_unless_zero (&obj->refcount)) { +mutex_unlock (&vc4->purgeable.lock); +cond_resched (); +mutex_lock (&vc4->purgeable.lock); +continue ; + }
Hum, your fix doesn't look correct to me. First, I don't understand why you are using something like cond_resched() here. Next, with this approach, the BO with refcount=0 will never be removed from the list.
drivers/gpu/drm/vc4/vc4_bo.c static void vc4_bo_userspace_cache_purge(struct drm_device *dev) · line 335
Quoted 2 lines
drivers/gpu/drm/vc4/vc4_bo.c +1
-0+vc4_bo_remove_from_purgeable_pool_locked (bo);
What you could do instead is calling:
if (!kref_get_unless_zero(&obj->refcount))
continue;
here.
Best regards,
- Maíra
Quoted 10 lines
drivers/gpu/drm/vc4/vc4_bo.c +1
-0/* Release the purgeable lock while we're purging the BO so @@ -345,6 +358,7 @@ static void vc4_bo_userspace_cache_purge(struct drm_device *dev)vc4_bo_purge (obj); }mutex_unlock (&bo->madv_lock); +drm_gem_object_put (obj);mutex_lock (&vc4->purgeable.lock);if (purged_size) {