Re: [PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device

2026-05-13 Thread kernel test robot
Hi Boris,

kernel test robot noticed the following build warnings:

[auto build test WARNING on b2ed01e7ad3de80333e9b962a44024b094bc0b2b]

url:
https://github.com/intel-lab-lkp/linux/commits/Boris-Brezillon/drm-gem-Make-the-GEM-LRU-lock-part-of-drm_device/20260514-051749
base:   b2ed01e7ad3de80333e9b962a44024b094bc0b2b
patch link:
https://lore.kernel.org/r/20260512-panthor-shrinker-fixes-v3-1-3bf066259471%40collabora.com
patch subject: [PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device
config: arm-randconfig-002-20260514 
(https://download.01.org/0day-ci/archive/20260514/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 
5bac06718f502014fade905512f1d26d578a18f3)
reproduce (this is a W=1 build): 
(https://download.01.org/0day-ci/archive/20260514/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot 
| Closes: 
https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/msm/msm_gem.c:827:26: warning: unused variable 'priv' 
>> [-Wunused-variable]
 827 | struct msm_drm_private *priv = obj->dev->dev_private;
 | ^~~~
   1 warning generated.


vim +/priv +827 drivers/gpu/drm/msm/msm_gem.c

4cd33c48ea25ba1 Rob Clark 2016-05-17  823  
599089c6af68300 Rob Clark 2020-10-23  824  void 
msm_gem_purge(struct drm_gem_object *obj)
68209390f116034 Rob Clark 2016-05-17  825  {
68209390f116034 Rob Clark 2016-05-17  826   struct drm_device *dev 
= obj->dev;
6c7c8fb863f7c31 Rob Clark 2023-03-20 @827   struct msm_drm_private 
*priv = obj->dev->dev_private;
68209390f116034 Rob Clark 2016-05-17  828   struct msm_gem_object 
*msm_obj = to_msm_bo(obj);
68209390f116034 Rob Clark 2016-05-17  829  
d4d7d3630d703ff Rob Clark 2022-08-02  830   
msm_gem_assert_locked(obj);
90643a24a7bfbe9 Rob Clark 2021-04-05  831   
GEM_WARN_ON(!is_purgeable(msm_obj));
68209390f116034 Rob Clark 2016-05-17  832  
20d0ae2f8c72e36 Rob Clark 2021-04-05  833   /* Get rid of any iommu 
mapping(s): */
0b4339c55ef59ff Rob Clark 2025-06-29  834   put_iova_spaces(obj, 
NULL, false, "purge");
0e08270a1f01bce Sushmita Susheelendra 2017-06-13  835  
599089c6af68300 Rob Clark 2020-10-23  836   msm_gem_vunmap(obj);
68209390f116034 Rob Clark 2016-05-17  837  
81d4d597d4faadb Rob Clark 2021-04-05  838   
drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
81d4d597d4faadb Rob Clark 2021-04-05  839  
68209390f116034 Rob Clark 2016-05-17  840   put_pages(obj);
68209390f116034 Rob Clark 2016-05-17  841  
c20ad1ee88c074c Boris Brezillon   2026-05-12  842   
mutex_lock(&dev->gem_lru_mutex);
6c7c8fb863f7c31 Rob Clark 2023-03-20  843   /* A one-way 
transition: */
68209390f116034 Rob Clark 2016-05-17  844   msm_obj->madv = 
__MSM_MADV_PURGED;
c20ad1ee88c074c Boris Brezillon   2026-05-12  845   
mutex_unlock(&dev->gem_lru_mutex);
68209390f116034 Rob Clark 2016-05-17  846  
68209390f116034 Rob Clark 2016-05-17  847   
drm_gem_free_mmap_offset(obj);
68209390f116034 Rob Clark 2016-05-17  848  
68209390f116034 Rob Clark 2016-05-17  849   /* Our goal here is to 
return as much of the memory as
68209390f116034 Rob Clark 2016-05-17  850* is possible back to 
the system as we are called from OOM.
68209390f116034 Rob Clark 2016-05-17  851* To do this we must 
instruct the shmfs to drop all of its
68209390f116034 Rob Clark 2016-05-17  852* backing pages, *now*.
68209390f116034 Rob Clark 2016-05-17  853*/
68209390f116034 Rob Clark 2016-05-17  854   
shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
68209390f116034 Rob Clark 2016-05-17  855  
68209390f116034 Rob Clark 2016-05-17  856   
invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
68209390f116034 Rob Clark 2016-05-17  857   0, 
(loff_t)-1);
68209390f116034 Rob Clark 2016-05-17  858  }
68209390f116034 Rob Clark 2016-05-17  859  

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


Re: [PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device

2026-05-13 Thread kernel test robot
Hi Boris,

kernel test robot noticed the following build warnings:

[auto build test WARNING on b2ed01e7ad3de80333e9b962a44024b094bc0b2b]

url:
https://github.com/intel-lab-lkp/linux/commits/Boris-Brezillon/drm-gem-Make-the-GEM-LRU-lock-part-of-drm_device/20260514-051749
base:   b2ed01e7ad3de80333e9b962a44024b094bc0b2b
patch link:
https://lore.kernel.org/r/20260512-panthor-shrinker-fixes-v3-1-3bf066259471%40collabora.com
patch subject: [PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device
config: csky-randconfig-r071-20260514 
(https://download.01.org/0day-ci/archive/20260514/[email protected]/config)
compiler: csky-linux-gcc (GCC) 15.2.0
smatch: v0.5.0-9185-gbcc58b9c
reproduce (this is a W=1 build): 
(https://download.01.org/0day-ci/archive/20260514/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot 
| Closes: 
https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: drivers/gpu/drm/drm_gem.c:1643 function parameter 'dev' not 
>> described in 'drm_gem_lru_scan'
>> Warning: drivers/gpu/drm/drm_gem.c:1643 function parameter 'dev' not 
>> described in 'drm_gem_lru_scan'

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


Re: [PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device

2026-05-12 Thread Chia-I Wu
On Mon, May 11, 2026 at 11:59 PM Boris Brezillon
 wrote:
>
> Recently, a few races have been discovered in the GEM LRU logic, all
> of them caused by the fact the LRU lock is accessed through
> gem->lru->lock, and that very same lock also protects changes to
> gem->lru, leading to situations where gem->lru needs to first be
> accessed without the lock held, to then get the lru to access the lock
> through and finally take the lock and do the expected operation.
>
> Currently, the only driver making use of this API (MSM) declares a
> device-wide lock, and the user we're about to add (panthor) will
> do the same. There's no evidence that we will ever have a driver
> that wants different pools of LRUs protected by different locks under
> the same drm_device. So we're better off moving this lock to drm_device
> and always locking it through obj->dev->gem_lru_mutex, or directly
> through dev->gem_lru_mutex.
>
> If anyone ever needs more fine-grained locking, this can be revisited
> to pass some drm_gem_lru_pool object representing the pool of LRUs
> under a specific lock, but for now, the per-device lock seems to be
> enough.
>
> Fixes: e7c2af13f811 ("drm/gem: Add LRU/shrinker helper")
> Reported-by: Chia-I Wu 
> Closes: https://gitlab.freedesktop.org/panfrost/linux/-/work_items/86
> Reviewed-by: Rob Clark 
> Reviewed-by: Liviu Dudau 
> Signed-off-by: Boris Brezillon 
I cannot quite reproduce with or without the fix, but this is

Reviewed-by: Chia-I Wu 

> ---
> As reported by Chia-I [1], a race exists between drm_gem_lru_remove()
> and drm_gem_lru_scan(), causing a UAF on a stack-allocated object.
>
> This new version only keeps the last patch of the series that
> addresses the problem more generically by moving the LRU lock to the
> drm_device object.
>
> [1]https://gitlab.freedesktop.org/panfrost/linux/-/work_items/86
> ---
> Changes in v3:
> - Only keep patch 4 and flag it for backport
> - Rebase on drm-misc-fixes
> - Link to v2: 
> https://lore.kernel.org/r/[email protected]
>
> Changes in v2:
> - Collect R-b
> - Drop a useless obj->lru != NULL check in drm_gem_lru_scan()
> - Fix another race introduced in patch 2
> - Document why the lru != NULL check done without the lru lock held
>   in drm_gem_lru_remove() is safe
> - Add a patch to sanitize the GEM LRU locking: lock is now part of
>   drm_device, meaning we don't have this chicken/egg problem where
>   the lock that needs to acquired is under gem->lru->lock, and
>   gem->lru is also supposed to be accessed with the lru->lock held
> - Fix typos in commit messages and comments
> - Link to v1: 
> https://lore.kernel.org/r/[email protected]
> ---
>  drivers/gpu/drm/drm_drv.c  |  2 ++
>  drivers/gpu/drm/drm_gem.c  | 34 
> +++---
>  drivers/gpu/drm/msm/msm_drv.c  | 11 +--
>  drivers/gpu/drm/msm/msm_drv.h  |  7 ---
>  drivers/gpu/drm/msm/msm_gem.c  | 32 
>  drivers/gpu/drm/msm/msm_gem_shrinker.c |  4 ++--
>  drivers/gpu/drm/msm/msm_gem_submit.c   |  6 +++---
>  drivers/gpu/drm/msm/msm_gem_vma.c  | 12 ++--
>  drivers/gpu/drm/msm/msm_ringbuffer.c   |  6 +++---
>  include/drm/drm_device.h   |  7 +++
>  include/drm/drm_gem.h  | 20 +---
>  11 files changed, 68 insertions(+), 73 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
> index 985c283cf59f..675675480da4 100644
> --- a/drivers/gpu/drm/drm_drv.c
> +++ b/drivers/gpu/drm/drm_drv.c
> @@ -697,6 +697,7 @@ static void drm_dev_init_release(struct drm_device *dev, 
> void *res)
> mutex_destroy(&dev->master_mutex);
> mutex_destroy(&dev->clientlist_mutex);
> mutex_destroy(&dev->filelist_mutex);
> +   mutex_destroy(&dev->gem_lru_mutex);
>  }
>
>  static int drm_dev_init(struct drm_device *dev,
> @@ -738,6 +739,7 @@ static int drm_dev_init(struct drm_device *dev,
> INIT_LIST_HEAD(&dev->vblank_event_list);
>
> spin_lock_init(&dev->event_lock);
> +   mutex_init(&dev->gem_lru_mutex);
> mutex_init(&dev->filelist_mutex);
> mutex_init(&dev->clientlist_mutex);
> mutex_init(&dev->master_mutex);
> diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
> index d6424267260b..2b1ac2b02b14 100644
> --- a/drivers/gpu/drm/drm_gem.c
> +++ b/drivers/gpu/drm/drm_gem.c
> @@ -1544,9 +1544,8 @@ EXPORT_SYMBOL(drm_gem_unlock_reservations);
>   * @lock: The lock protecting the LRU
>   */
>  void
> -drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock)
> +drm_gem_lru_init(struct drm_gem_lru *lru)
>  {
> -   lru->lock = lock;
> lru->count = 0;
> INIT_LIST_HEAD(&lru->list);
>  }
> @@ -1571,14 +1570,10 @@ drm_gem_lru_remove_locked(struct drm_gem_object *obj)
>  void
>  drm_gem_lru_remove(struct drm_gem_object *obj)
>  {
> -   

[PATCH v3] drm/gem: Make the GEM LRU lock part of drm_device

2026-05-11 Thread Boris Brezillon
Recently, a few races have been discovered in the GEM LRU logic, all
of them caused by the fact the LRU lock is accessed through
gem->lru->lock, and that very same lock also protects changes to
gem->lru, leading to situations where gem->lru needs to first be
accessed without the lock held, to then get the lru to access the lock
through and finally take the lock and do the expected operation.

Currently, the only driver making use of this API (MSM) declares a
device-wide lock, and the user we're about to add (panthor) will
do the same. There's no evidence that we will ever have a driver
that wants different pools of LRUs protected by different locks under
the same drm_device. So we're better off moving this lock to drm_device
and always locking it through obj->dev->gem_lru_mutex, or directly
through dev->gem_lru_mutex.

If anyone ever needs more fine-grained locking, this can be revisited
to pass some drm_gem_lru_pool object representing the pool of LRUs
under a specific lock, but for now, the per-device lock seems to be
enough.

Fixes: e7c2af13f811 ("drm/gem: Add LRU/shrinker helper")
Reported-by: Chia-I Wu 
Closes: https://gitlab.freedesktop.org/panfrost/linux/-/work_items/86
Reviewed-by: Rob Clark 
Reviewed-by: Liviu Dudau 
Signed-off-by: Boris Brezillon 
---
As reported by Chia-I [1], a race exists between drm_gem_lru_remove()
and drm_gem_lru_scan(), causing a UAF on a stack-allocated object.

This new version only keeps the last patch of the series that
addresses the problem more generically by moving the LRU lock to the
drm_device object.

[1]https://gitlab.freedesktop.org/panfrost/linux/-/work_items/86
---
Changes in v3:
- Only keep patch 4 and flag it for backport
- Rebase on drm-misc-fixes
- Link to v2: 
https://lore.kernel.org/r/[email protected]

Changes in v2:
- Collect R-b
- Drop a useless obj->lru != NULL check in drm_gem_lru_scan()
- Fix another race introduced in patch 2
- Document why the lru != NULL check done without the lru lock held
  in drm_gem_lru_remove() is safe
- Add a patch to sanitize the GEM LRU locking: lock is now part of
  drm_device, meaning we don't have this chicken/egg problem where
  the lock that needs to acquired is under gem->lru->lock, and
  gem->lru is also supposed to be accessed with the lru->lock held
- Fix typos in commit messages and comments
- Link to v1: 
https://lore.kernel.org/r/[email protected]
---
 drivers/gpu/drm/drm_drv.c  |  2 ++
 drivers/gpu/drm/drm_gem.c  | 34 +++---
 drivers/gpu/drm/msm/msm_drv.c  | 11 +--
 drivers/gpu/drm/msm/msm_drv.h  |  7 ---
 drivers/gpu/drm/msm/msm_gem.c  | 32 
 drivers/gpu/drm/msm/msm_gem_shrinker.c |  4 ++--
 drivers/gpu/drm/msm/msm_gem_submit.c   |  6 +++---
 drivers/gpu/drm/msm/msm_gem_vma.c  | 12 ++--
 drivers/gpu/drm/msm/msm_ringbuffer.c   |  6 +++---
 include/drm/drm_device.h   |  7 +++
 include/drm/drm_gem.h  | 20 +---
 11 files changed, 68 insertions(+), 73 deletions(-)

diff --git a/drivers/gpu/drm/drm_drv.c b/drivers/gpu/drm/drm_drv.c
index 985c283cf59f..675675480da4 100644
--- a/drivers/gpu/drm/drm_drv.c
+++ b/drivers/gpu/drm/drm_drv.c
@@ -697,6 +697,7 @@ static void drm_dev_init_release(struct drm_device *dev, 
void *res)
mutex_destroy(&dev->master_mutex);
mutex_destroy(&dev->clientlist_mutex);
mutex_destroy(&dev->filelist_mutex);
+   mutex_destroy(&dev->gem_lru_mutex);
 }
 
 static int drm_dev_init(struct drm_device *dev,
@@ -738,6 +739,7 @@ static int drm_dev_init(struct drm_device *dev,
INIT_LIST_HEAD(&dev->vblank_event_list);
 
spin_lock_init(&dev->event_lock);
+   mutex_init(&dev->gem_lru_mutex);
mutex_init(&dev->filelist_mutex);
mutex_init(&dev->clientlist_mutex);
mutex_init(&dev->master_mutex);
diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index d6424267260b..2b1ac2b02b14 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -1544,9 +1544,8 @@ EXPORT_SYMBOL(drm_gem_unlock_reservations);
  * @lock: The lock protecting the LRU
  */
 void
-drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock)
+drm_gem_lru_init(struct drm_gem_lru *lru)
 {
-   lru->lock = lock;
lru->count = 0;
INIT_LIST_HEAD(&lru->list);
 }
@@ -1571,14 +1570,10 @@ drm_gem_lru_remove_locked(struct drm_gem_object *obj)
 void
 drm_gem_lru_remove(struct drm_gem_object *obj)
 {
-   struct drm_gem_lru *lru = obj->lru;
-
-   if (!lru)
-   return;
-
-   mutex_lock(lru->lock);
-   drm_gem_lru_remove_locked(obj);
-   mutex_unlock(lru->lock);
+   mutex_lock(&obj->dev->gem_lru_mutex);
+   if (obj->lru)
+   drm_gem_lru_remove_locked(obj);
+   mutex_unlock(&obj->dev->gem_lru_mutex);
 }