Skip to contentSkip to conversation

drm/gem: Use vmemdup_array_user in drm_gem_objects_lookup

PATCH Superseded Tvrtko Ursulin 2 participants 2 messages Nov 6, 2025 – Nov 8, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/gem: Use vmemdup_array_user in drm_gem_objects_lookup

Using a helper shrinks the code and separating the user and kernel slabs is more secure.

---
 drivers/gpu/drm/drm_gem.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_gem.c +3 -9
index a1a9c828938b..df138375e864 100644
@@ -798,15 +798,9 @@ int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
 
 	*objs_out = objs;
 
-	handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
-	if (!handles) {
-		ret = -ENOMEM;
-		goto out;
-	}
-
-	if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
-		ret = -EFAULT;
-		drm_dbg_core(dev, "Failed to copy in GEM handles\n");
+	handles = vmemdup_array_user(bo_handles, count, sizeof(u32));
+	if (IS_ERR(handles)) {
+		ret = PTR_ERR(handles);
 		goto out;
 	}
 
Signature · 1 line
2.48.0
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Tvrtko,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on drm-tip/drm-tip next-20251107]
[cannot apply to drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-intel/for-linux-next-fixes linus/master v6.18-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Tvrtko-Ursulin/drm-gem-Use-vmemdup_array_user-in-drm_gem_objects_lookup/20251106-212028
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20251106131926.28096-1-tvrtko.ursulin%40igalia.com
patch subject: [PATCH] drm/gem: Use vmemdup_array_user in drm_gem_objects_lookup
config: loongarch-randconfig-002-20251108 (https://download.01.org/0day-ci/archive/20251108/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 12.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251108/[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 <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/gpu/drm/drm_gem.c: In function 'drm_gem_objects_lookup':

>> drivers/gpu/drm/drm_gem.c:786:28: warning: unused variable 'dev' [-Wunused-variable]
     786 |         struct drm_device *dev = filp->minor->dev;
         |                            ^~~

vim +/dev +786 drivers/gpu/drm/drm_gem.c

c117aa4d8701a7 Rob Herring         2019-03-08  764  
c117aa4d8701a7 Rob Herring         2019-03-08  765  /**
c117aa4d8701a7 Rob Herring         2019-03-08  766   * drm_gem_objects_lookup - look up GEM objects from an array of handles
c117aa4d8701a7 Rob Herring         2019-03-08  767   * @filp: DRM file private date
c117aa4d8701a7 Rob Herring         2019-03-08  768   * @bo_handles: user pointer to array of userspace handle
c117aa4d8701a7 Rob Herring         2019-03-08  769   * @count: size of handle array
c117aa4d8701a7 Rob Herring         2019-03-08  770   * @objs_out: returned pointer to array of drm_gem_object pointers
c117aa4d8701a7 Rob Herring         2019-03-08  771   *
c117aa4d8701a7 Rob Herring         2019-03-08  772   * Takes an array of userspace handles and returns a newly allocated array of
c117aa4d8701a7 Rob Herring         2019-03-08  773   * GEM objects.
c117aa4d8701a7 Rob Herring         2019-03-08  774   *
c117aa4d8701a7 Rob Herring         2019-03-08  775   * For a single handle lookup, use drm_gem_object_lookup().
c117aa4d8701a7 Rob Herring         2019-03-08  776   *
c117aa4d8701a7 Rob Herring         2019-03-08  777   * Returns:
c117aa4d8701a7 Rob Herring         2019-03-08  778   * @objs filled in with GEM object pointers. Returned GEM objects need to be
be6ee102341bc4 Emil Velikov        2020-05-15  779   * released with drm_gem_object_put(). -ENOENT is returned on a lookup
c117aa4d8701a7 Rob Herring         2019-03-08  780   * failure. 0 is returned on success.
c117aa4d8701a7 Rob Herring         2019-03-08  781   *
c117aa4d8701a7 Rob Herring         2019-03-08  782   */
c117aa4d8701a7 Rob Herring         2019-03-08  783  int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
c117aa4d8701a7 Rob Herring         2019-03-08  784  			   int count, struct drm_gem_object ***objs_out)
c117aa4d8701a7 Rob Herring         2019-03-08  785  {
e5e0350d5d1ab0 Athul Raj Kollareth 2025-09-02 @786  	struct drm_device *dev = filp->minor->dev;
c117aa4d8701a7 Rob Herring         2019-03-08  787  	struct drm_gem_object **objs;
e5e0350d5d1ab0 Athul Raj Kollareth 2025-09-02  788  	u32 *handles;
e5e0350d5d1ab0 Athul Raj Kollareth 2025-09-02  789  	int ret;
c117aa4d8701a7 Rob Herring         2019-03-08  790  
c117aa4d8701a7 Rob Herring         2019-03-08  791  	if (!count)
c117aa4d8701a7 Rob Herring         2019-03-08  792  		return 0;
c117aa4d8701a7 Rob Herring         2019-03-08  793  
c117aa4d8701a7 Rob Herring         2019-03-08  794  	objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
c117aa4d8701a7 Rob Herring         2019-03-08  795  			     GFP_KERNEL | __GFP_ZERO);
c117aa4d8701a7 Rob Herring         2019-03-08  796  	if (!objs)
c117aa4d8701a7 Rob Herring         2019-03-08  797  		return -ENOMEM;
c117aa4d8701a7 Rob Herring         2019-03-08  798  
ec0bb482de0ad5 Dan Carpenter       2020-03-20  799  	*objs_out = objs;
ec0bb482de0ad5 Dan Carpenter       2020-03-20  800  
50c1d127a2aa88 Tvrtko Ursulin      2025-11-06  801  	handles = vmemdup_array_user(bo_handles, count, sizeof(u32));
50c1d127a2aa88 Tvrtko Ursulin      2025-11-06  802  	if (IS_ERR(handles)) {
50c1d127a2aa88 Tvrtko Ursulin      2025-11-06  803  		ret = PTR_ERR(handles);
c117aa4d8701a7 Rob Herring         2019-03-08  804  		goto out;
c117aa4d8701a7 Rob Herring         2019-03-08  805  	}
c117aa4d8701a7 Rob Herring         2019-03-08  806  
c117aa4d8701a7 Rob Herring         2019-03-08  807  	ret = objects_lookup(filp, handles, count, objs);
c117aa4d8701a7 Rob Herring         2019-03-08  808  out:
c117aa4d8701a7 Rob Herring         2019-03-08  809  	kvfree(handles);
c117aa4d8701a7 Rob Herring         2019-03-08  810  	return ret;
c117aa4d8701a7 Rob Herring         2019-03-08  811  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki