From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ECAB53D1705 for ; Fri, 8 May 2026 15:01:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778252473; cv=none; b=k8Yj0gKPdEfCdr6XFXi6gJuL9auKWO3XW0KmLuN8EHmIaFuKJQRQW9C4PPeYKoL+4amiTL8Xu9Md/Ho5yAGjAW7QEb8c2FX2tZ7o2RnBNgVdkLzGLVvIUcr7DZ25kbcCFnYofuJAbMJneM+bfcsSuovrbHGs9NXEIFptnDc2WS4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778252473; c=relaxed/simple; bh=tBSLp4TFWnzxz3eHgUik+or3IqdAn5knLKmt0L5xo5M=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Bng+/1nmqms1K2x6NYSIyzPDmBcaeW9GIl1sfYuMlS01f1zXBPVed8FIPmlekjGiwhrlHWuy7SZYe9zvDvwDLQZlXuza0gGS4rb16onxec16pI+0sf3+0pBdmUL3+qW1WOLFYqB4opDJgDX+91B4WIOaT3RFVNi8GWMAr+ZVdFg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=bDraT/pH; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="bDraT/pH" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D2FB83595 for ; Fri, 8 May 2026 08:01:04 -0700 (PDT) Received: from [192.168.0.1] (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id E8D293F763 for ; Fri, 8 May 2026 08:01:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1778252470; bh=tBSLp4TFWnzxz3eHgUik+or3IqdAn5knLKmt0L5xo5M=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=bDraT/pHkcbZ0bQVqGw+Hjq3KZWAsaUEojhtX2G17fhz7e6+9mAma/JC0Ld0LRNrY xxtEyBsSreyFqr6YjZgXrSfLbCEAmtqfdVPKebQfTo6fnHCfrMYqp9LTO7pXECXFjv 1spBYz6HnJmzGgYidmu5mcvxGGYfSQ4nD9UknUe0= Date: Fri, 8 May 2026 16:00:51 +0100 From: Liviu Dudau To: Boris Brezillon Cc: Steven Price , Dmitry Osipenko , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Akash Goel , Chia-I Wu , Rob Clark , Dmitry Baryshkov , Abhinav Kumar , Jessica Zhang , Sean Paul , Marijn Suijten , linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 3/4] drm/gem: Stop exposing the racy/unsafe drm_gem_lru_remove() helper Message-ID: References: <20260508-panthor-shrinker-fixes-v2-0-39cdb7d577c9@collabora.com> <20260508-panthor-shrinker-fixes-v2-3-39cdb7d577c9@collabora.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20260508-panthor-shrinker-fixes-v2-3-39cdb7d577c9@collabora.com> On Fri, May 08, 2026 at 12:40:49PM +0200, Boris Brezillon wrote: > The only place where it's safe to call drm_gem_lru_remove() is when > we know the drm_gem_object::lru field can't be concurrently updated, > which we know is the case when the drm_gem_object is destroyed. > > Rather than trying to make that safe, let's kill the function and inline > its content in drm_gem_object_release(). > > Signed-off-by: Boris Brezillon Reviewed-by: Liviu Dudau Best regards, Liviu > --- > drivers/gpu/drm/drm_gem.c | 90 ++++++++++++++++++++--------------------------- > include/drm/drm_gem.h | 1 - > 2 files changed, 39 insertions(+), 52 deletions(-) > > diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c > index 0e087c770883..c85a39b8b163 100644 > --- a/drivers/gpu/drm/drm_gem.c > +++ b/drivers/gpu/drm/drm_gem.c > @@ -1108,6 +1108,15 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private) > idr_destroy(&file_private->object_idr); > } > > +static void > +drm_gem_lru_remove_locked(struct drm_gem_object *obj) > +{ > + obj->lru->count -= obj->size >> PAGE_SHIFT; > + WARN_ON(obj->lru->count < 0); > + list_del(&obj->lru_node); > + obj->lru = NULL; > +} > + > /** > * drm_gem_object_release - release GEM buffer object resources > * @obj: GEM buffer object > @@ -1118,13 +1127,42 @@ drm_gem_release(struct drm_device *dev, struct drm_file *file_private) > void > drm_gem_object_release(struct drm_gem_object *obj) > { > + struct drm_gem_lru *lru; > + > if (obj->filp) > fput(obj->filp); > > drm_gem_private_object_fini(obj); > > drm_gem_free_mmap_offset(obj); > - drm_gem_lru_remove(obj); > + > + /* > + * We do the lru != NULL check without the lru->lock held, which > + * means we might end up with a stale lru value by the time the > + * lock is acquired. > + * > + * This is deemed safe because: > + * 1. the LRU is assumed to outlive any GEM object it was attached > + * (LRUs are usually bound to a drm_device). So even if obj->lru > + * has become NULL, it still point to a valid object that can > + * safely be dereferenced to get the lock. > + * > + * 2. all LRUs a GEM object might be attached to must share the same > + * lock (lock that's usually part of the driver-specific device > + * object), so taking the lock on the 'old' LRU is equivalent > + * to taking it on the new one (if any) > + */ > + lru = obj->lru; > + if (lru) { > + guard(mutex)(lru->lock); > + > + /* Check a second time with the lock held to make sure we're > + * not racing with the drm_gem_lru_remove_locked() call in > + * drm_gem_lru_scan(). > + */ > + if (obj->lru) > + drm_gem_lru_remove_locked(obj); > + } > } > EXPORT_SYMBOL(drm_gem_object_release); > > @@ -1552,56 +1590,6 @@ drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock) > } > EXPORT_SYMBOL(drm_gem_lru_init); > > -static void > -drm_gem_lru_remove_locked(struct drm_gem_object *obj) > -{ > - obj->lru->count -= obj->size >> PAGE_SHIFT; > - WARN_ON(obj->lru->count < 0); > - list_del(&obj->lru_node); > - obj->lru = NULL; > -} > - > -/** > - * drm_gem_lru_remove - remove object from whatever LRU it is in > - * > - * If the object is currently in any LRU, remove it. > - * > - * @obj: The GEM object to remove from current LRU > - */ > -void > -drm_gem_lru_remove(struct drm_gem_object *obj) > -{ > - struct drm_gem_lru *lru = obj->lru; > - > - /* > - * We do the lru != NULL check without the lru->lock held, which > - * means we might end up with a stale lru value by the time the > - * lock is acquired. > - * > - * This is deemed safe because: > - * 1. the LRU is assumed to outlive any GEM object it was attached > - * (LRUs are usually bound to a drm_device). So even if obj->lru > - * has become NULL, it still point to a valid object that can > - * safely be dereferenced to get the lock. > - * > - * 2. all LRUs a GEM object might be attached to must share the same > - * lock (lock that's usually part of the driver-specific device > - * object), so taking the lock on the 'old' LRU is equivalent > - * to taking it on the new one (if any) > - */ > - if (!lru) > - return; > - > - mutex_lock(lru->lock); > - /* Check a second time with the lock held to make sure we're not racing > - * with another drm_gem_lru_remove[_locked]() call. > - */ > - if (obj->lru) > - drm_gem_lru_remove_locked(obj); > - mutex_unlock(lru->lock); > -} > -EXPORT_SYMBOL(drm_gem_lru_remove); > - > /** > * drm_gem_lru_move_tail_locked - move the object to the tail of the LRU > * > diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h > index 86f5846154f7..d527df98d142 100644 > --- a/include/drm/drm_gem.h > +++ b/include/drm/drm_gem.h > @@ -611,7 +611,6 @@ int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, > u32 handle, u64 *offset); > > void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock); > -void drm_gem_lru_remove(struct drm_gem_object *obj); > void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj); > void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj); > unsigned long > > -- > 2.54.0 > -- ==================== | I would like to | | fix the world, | | but they're not | | giving me the | \ source code! / --------------- ¯\_(ツ)_/¯