Skip to contentSkip to conversation

drm/msm: Fix submit error-path leaks

PATCH Superseded Rob Clark 2 participants 2 messages May 1, 2023 – May 9, 2023

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH v2 1/1 drm/msm: Fix submit error-path leaks

From: Rob Clark <[email protected]>

For errors after msm_submitqueue_get(), we need to drop the submitqueue reference. Additionally after get_unused_fd() we need to drop the fd. The ordering for dropping the queue lock and put_unused_fd() is not important, so just move this all into out_post_unlock.

v2: Only drop queue ref if submit doesn't take it

Reported-by [email protected]
Fixes f0de40a131d9 drm/msm: ("Reorder lock vs submit alloc")
---
 drivers/gpu/drm/msm/msm_gem_submit.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/msm/msm_gem_submit.c +16 -6
index 6c6aefaa72be..77d73a81d10e 100644
@@ -767,27 +767,29 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
 	queue = msm_submitqueue_get(ctx, args->queueid);
 	if (!queue)
 		return -ENOENT;
 
 	ring = gpu->rb[queue->ring_nr];
 
 	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
 		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);
 		if (out_fence_fd < 0) {
 			ret = out_fence_fd;
-			return ret;
+			goto out_post_unlock;
 		}
 	}
 
 	submit = submit_create(dev, gpu, queue, args->nr_bos, args->nr_cmds);
-	if (IS_ERR(submit))
-		return PTR_ERR(submit);
+	if (IS_ERR(submit)) {
+		ret = PTR_ERR(submit);
+		goto out_post_unlock;
+	}
 
 	trace_msm_gpu_submit(pid_nr(submit->pid), ring->id, submit->ident,
 		args->nr_bos, args->nr_cmds);
 
 	ret = mutex_lock_interruptible(&queue->lock);
 	if (ret)
 		goto out_post_unlock;
 
 	if (args->flags & MSM_SUBMIT_SUDO)
 		submit->in_rb = true;
@@ -962,25 +964,33 @@ int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
 	msm_reset_syncobjs(syncobjs_to_reset, args->nr_in_syncobjs);
 	msm_process_post_deps(post_deps, args->nr_out_syncobjs,
 	                      submit->user_fence);
 
 
 out:
 	submit_cleanup(submit, !!ret);
 	if (has_ww_ticket)
 		ww_acquire_fini(&submit->ticket);
 out_unlock:
-	if (ret && (out_fence_fd >= 0))
-		put_unused_fd(out_fence_fd);
 	mutex_unlock(&queue->lock);
 out_post_unlock:
-	msm_gem_submit_put(submit);
+	if (ret && (out_fence_fd >= 0))
+		put_unused_fd(out_fence_fd);
+	if (submit) {
+		msm_gem_submit_put(submit);
+	} else {
+		/*
+		 * If the submit hasn't yet taken ownership of the queue
+		 * then we need to drop the reference ourself:
+		 */
+		msm_submitqueue_put(queue);
+	}
 	if (!IS_ERR_OR_NULL(post_deps)) {
 		for (i = 0; i < args->nr_out_syncobjs; ++i) {
 			kfree(post_deps[i].chain);
 			drm_syncobj_put(post_deps[i].syncobj);
 		}
 		kfree(post_deps);
 	}
 
 	if (!IS_ERR_OR_NULL(syncobjs_to_reset)) {
 		for (i = 0; i < args->nr_in_syncobjs; ++i) {
Signature · 1 line
2.39.2
#1 lore raw
kernel test robot noticed the following build warnings:

Hi Rob,

kernel test robot noticed the following build warnings:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on drm/drm-next drm-exynos/exynos-drm-next drm-intel/for-linux-next drm-intel/for-linux-next-fixes drm-tip/drm-tip linus/master v6.4-rc1 next-20230508] [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/Rob-Clark/drm-msm-Fix-submit-error-path-leaks/20230502-021521
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20230501181327.1618596-1-robdclark%40gmail.com
patch subject: [PATCH v2] drm/msm: Fix submit error-path leaks
config: arm-randconfig-r002-20230507 (https://download.01.org/0day-ci/archive/20230509/[email protected]/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project b0fb98227c90adf2536c9ad644a74d5e92961111)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/intel-lab-lkp/linux/commit/895a58ff05de3cebb081d61f8e001864f4ef41c2
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Rob-Clark/drm-msm-Fix-submit-error-path-leaks/20230502-021521
git checkout 895a58ff05de3cebb081d61f8e001864f4ef41c2
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash drivers/gpu/drm/msm/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/msm/msm_gem_submit.c:770:7: warning: variable 'submit' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
                   if (out_fence_fd < 0) {
                       ^~~~~~~~~~~~~~~~
   drivers/gpu/drm/msm/msm_gem_submit.c:971:6: note: uninitialized use occurs here
           if (submit) {
               ^~~~~~
   drivers/gpu/drm/msm/msm_gem_submit.c:770:3: note: remove the 'if' if its condition is always false
                   if (out_fence_fd < 0) {
                   ^~~~~~~~~~~~~~~~~~~~~~~
   drivers/gpu/drm/msm/msm_gem_submit.c:725:31: note: initialize the variable 'submit' to silence this warning
           struct msm_gem_submit *submit;
                                        ^
                                         = NULL

1 warning generated.

vim +770 drivers/gpu/drm/msm/msm_gem_submit.c

ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  718  
7198e6b03155f6 Rob Clark         2013-07-19  719  int msm_ioctl_gem_submit(struct drm_device *dev, void *data,
7198e6b03155f6 Rob Clark         2013-07-19  720  		struct drm_file *file)
7198e6b03155f6 Rob Clark         2013-07-19  721  {
7198e6b03155f6 Rob Clark         2013-07-19  722  	struct msm_drm_private *priv = dev->dev_private;
7198e6b03155f6 Rob Clark         2013-07-19  723  	struct drm_msm_gem_submit *args = data;
7198e6b03155f6 Rob Clark         2013-07-19  724  	struct msm_file_private *ctx = file->driver_priv;
ccc40d42bd59ac Aashish Sharma    2022-10-11  725  	struct msm_gem_submit *submit;
c01a958eca6e7f Rob Clark         2016-02-03  726  	struct msm_gpu *gpu = priv->gpu;
f7de15450e906e Jordan Crouse     2017-10-20  727  	struct msm_gpu_submitqueue *queue;
f97decac5f4c2d Jordan Crouse     2017-10-20  728  	struct msm_ringbuffer *ring;
ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  729  	struct msm_submit_post_dep *post_deps = NULL;
ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  730  	struct drm_syncobj **syncobjs_to_reset = NULL;
4cd0945901a6dd Rob Clark         2016-06-16  731  	int out_fence_fd = -1;
92ec0767710797 Daniel Vetter     2019-11-20  732  	bool has_ww_ticket = false;
7198e6b03155f6 Rob Clark         2013-07-19  733  	unsigned i;
fc1dd04c3841c4 Rob Clark         2022-08-02  734  	int ret;
375f9a63a66bae Rob Clark         2021-07-27  735  
c01a958eca6e7f Rob Clark         2016-02-03  736  	if (!gpu)
c01a958eca6e7f Rob Clark         2016-02-03  737  		return -ENXIO;
c01a958eca6e7f Rob Clark         2016-02-03  738  
ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  739  	if (args->pad)
ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  740  		return -EINVAL;
ab723b7a992a19 Bas Nieuwenhuizen 2020-01-24  741  
4a257ca694015f Rob Clark         2022-05-02  742  	if (unlikely(!ctx->aspace) && !capable(CAP_SYS_RAWIO)) {
4a257ca694015f Rob Clark         2022-05-02  743  		DRM_ERROR_RATELIMITED("IOMMU support or CAP_SYS_RAWIO required!\n");
4a257ca694015f Rob Clark         2022-05-02  744  		return -EPERM;
4a257ca694015f Rob Clark         2022-05-02  745  	}
4a257ca694015f Rob Clark         2022-05-02  746  
7198e6b03155f6 Rob Clark         2013-07-19  747  	/* for now, we just have 3d pipe.. eventually this would need to
7198e6b03155f6 Rob Clark         2013-07-19  748  	 * be more clever to dispatch to appropriate gpu module:
7198e6b03155f6 Rob Clark         2013-07-19  749  	 */
d9c181e22a0599 Rob Clark         2016-04-23  750  	if (MSM_PIPE_ID(args->flags) != MSM_PIPE_3D0)
d9c181e22a0599 Rob Clark         2016-04-23  751  		return -EINVAL;
d9c181e22a0599 Rob Clark         2016-04-23  752  
d9c181e22a0599 Rob Clark         2016-04-23  753  	if (MSM_PIPE_FLAGS(args->flags) & ~MSM_SUBMIT_FLAGS)
7198e6b03155f6 Rob Clark         2013-07-19  754  		return -EINVAL;
7198e6b03155f6 Rob Clark         2013-07-19  755  
6a8bd08d0465b2 Rob Clark         2017-12-13  756  	if (args->flags & MSM_SUBMIT_SUDO) {
6a8bd08d0465b2 Rob Clark         2017-12-13  757  		if (!IS_ENABLED(CONFIG_DRM_MSM_GPU_SUDO) ||
6a8bd08d0465b2 Rob Clark         2017-12-13  758  		    !capable(CAP_SYS_RAWIO))
6a8bd08d0465b2 Rob Clark         2017-12-13  759  			return -EINVAL;
6a8bd08d0465b2 Rob Clark         2017-12-13  760  	}
6a8bd08d0465b2 Rob Clark         2017-12-13  761  
f7de15450e906e Jordan Crouse     2017-10-20  762  	queue = msm_submitqueue_get(ctx, args->queueid);
f7de15450e906e Jordan Crouse     2017-10-20  763  	if (!queue)
f7de15450e906e Jordan Crouse     2017-10-20  764  		return -ENOENT;
f7de15450e906e Jordan Crouse     2017-10-20  765  
fc40e5e10c3bcc Rob Clark         2021-07-27  766  	ring = gpu->rb[queue->ring_nr];
f97decac5f4c2d Jordan Crouse     2017-10-20  767  
1d8a5ca436ee4a Rob Clark         2021-07-27  768  	if (args->flags & MSM_SUBMIT_FENCE_FD_OUT) {
1d8a5ca436ee4a Rob Clark         2021-07-27  769  		out_fence_fd = get_unused_fd_flags(O_CLOEXEC);

1d8a5ca436ee4a Rob Clark 2021-07-27 @770 if (out_fence_fd < 0) {

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests