mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Maíra Canal" <mcanal@igalia.com>
To: Wang Xiaolei <wangxiaolei01@kaihong.com>,
	mwen@igalia.com, maarten.lankhorst@linux.intel.com,
	mripard@kernel.org, tzimmermann@suse.de, airlied@gmail.com,
	simona@ffwll.ch, tvrtko.ursulin@igalia.com
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, Iago Toral Quiroga <itoral@igalia.com>
Subject: Re: [PATCH] drm/v3d: Fix double free of query arrays when extension parsing fails
Date: Wed, 18 Nov 2026 10:21:43 -0300	[thread overview]
Message-ID: <c8e13d27-7d16-481d-82bf-6f7cca80a5e4@igalia.com> (raw)
In-Reply-To: <20261110061341.2075931-1-wangxiaolei01@kaihong.com>

Hi Wang,

Please, be patient with kernel maintainers. Kernel etiquette recommends
waiting at least a week before pinging maintainers [1].

On 10/11/26 03:13, Wang Xiaolei wrote:
> v3d_get_cpu_timestamp_query_params() and v3d_copy_query_info() - the
> latter used by both performance query parsers - free the query array on
> their error paths through v3d_timestamp_query_info_free() /
> v3d_performance_query_info_free(), but leave query_info->queries
> pointing at the freed buffer.
> 
> The same job is then released by v3d_submit_cpu_ioctl()'s failure path
> (v3d_submit_cleanup_jobs() -> v3d_job_cleanup() -> v3d_job_put() ->
> v3d_cpu_job_free()), which calls the same helpers on the same
> query_info. On these error paths query_info->count was never assigned -
> it is only written after the parsing loop completes - so the syncobj
> loop runs zero times and no syncobj is put twice, but the
> "if (query_info->queries)" test still succeeds and the dangling pointer
> is handed to kvfree() a second time.
> 
> Every failure inside the parsing loop takes this path: -ENOENT from
> drm_syncobj_find(), -EFAULT from get_user(), or -ENOMEM from
> kvmalloc_array() in v3d_copy_query_info(). The helpers are shared
> between that error path and the job release path.
> 
> Clear the pointer once the buffer has been freed.

This is indeed a bug, but the solution doesn't fix the ownership problem
here. We should actually leave the cleaning process to
v3d_cpu_job_free() instead of cleaning it in the parser. I wrote this
draft of what I was thinking on:

diff --git a/drivers/gpu/drm/v3d/v3d_sched.c 
b/drivers/gpu/drm/v3d/v3d_sched.c
index f29d79b15be3..15bec1e35e39 100644
--- a/drivers/gpu/drm/v3d/v3d_sched.c
+++ b/drivers/gpu/drm/v3d/v3d_sched.c
@@ -96,17 +96,18 @@ v3d_sched_job_free(struct drm_sched_job *sched_job)
  }

  void
-v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
-                             unsigned int count)
+v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info)
  {
-       if (query_info->queries) {
-               unsigned int i;
+       unsigned int i;

-               for (i = 0; i < count; i++)
-                       drm_syncobj_put(query_info->queries[i].syncobj);
+       for (i = 0; i < query_info->count; i++) {
+               if (!query_info->queries[i].syncobj)
+                       break;

-               kvfree(query_info->queries);
+               drm_syncobj_put(query_info->queries[i].syncobj);
         }
+
+       kvfree(query_info->queries);
  }

  void
diff --git a/drivers/gpu/drm/v3d/v3d_submit.c 
b/drivers/gpu/drm/v3d/v3d_submit.c
index eada30a31701..a3e92e097423 100644
--- a/drivers/gpu/drm/v3d/v3d_submit.c
+++ b/drivers/gpu/drm/v3d/v3d_submit.c
@@ -734,7 +734,6 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,
         struct drm_v3d_timestamp_query timestamp;
         struct v3d_timestamp_query_info *query_info = 
&job->timestamp_query;
         unsigned int i;
-       int err;

         if (!v3d_validate_cpu_job(file_priv, job))
                 return -EINVAL;
@@ -747,7 +746,7 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,

         job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;

-       query_info->queries = kvmalloc_objs(struct v3d_timestamp_query,
+       query_info->queries = kvzalloc_objs(struct v3d_timestamp_query,
                                             timestamp.count);
         if (!query_info->queries)
                 return -ENOMEM;
@@ -758,32 +757,22 @@ v3d_get_cpu_timestamp_query_params(struct drm_file 
*file_priv,
         for (i = 0; i < timestamp.count; i++) {
                 u32 offset, sync;

-               if (get_user(offset, offsets++)) {
-                       err = -EFAULT;
-                       goto error;
-               }
+               if (get_user(offset, offsets++))
+                       return -EFAULT;

                 query_info->queries[i].offset = offset;

-               if (get_user(sync, syncs++)) {
-                       err = -EFAULT;
-                       goto error;
-               }
+               if (get_user(sync, syncs++))
+                       return -EFAULT;

                 query_info->queries[i].syncobj = 
drm_syncobj_find(file_priv,
                                                                   sync);
-               if (!query_info->queries[i].syncobj) {
-                       err = -ENOENT;
-                       goto error;
-               }
+               if (!query_info->queries[i].syncobj)
+                       return -ENOENT;
         }
         query_info->count = timestamp.count;

         return 0;
-
-error:
-       v3d_timestamp_query_info_free(&job->timestamp_query, i);
-       return err;
  }

It doesn't even compile, it's just a draft to give you a more concrete
example of what I'm thinking on.

[1] 
https://docs.kernel.org/process/submitting-patches.html#don-t-get-discouraged-or-impatient

Best regards,
- Maíra

> 
> Fixes: 0e50fcc20bd8 ("drm/v3d: Fix potential memory leak in the timestamp extension")
> Fixes: 32df4abc44f2 ("drm/v3d: Fix potential memory leak in the performance extension")
> Cc: stable@vger.kernel.org
> Cc: Maíra Canal <mcanal@igalia.com>
> Cc: Iago Toral Quiroga <itoral@igalia.com>
> Cc: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
> Signed-off-by: Wang Xiaolei <wangxiaolei01@kaihong.com>
> ---
>   drivers/gpu/drm/v3d/v3d_sched.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
> index 1924e3440dc9..a96023b5dce0 100644
> --- a/drivers/gpu/drm/v3d/v3d_sched.c
> +++ b/drivers/gpu/drm/v3d/v3d_sched.c
> @@ -106,6 +106,7 @@ v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
>   			drm_syncobj_put(query_info->queries[i].syncobj);
>   
>   		kvfree(query_info->queries);
> +		query_info->queries = NULL;
>   	}
>   }
>   
> @@ -122,6 +123,7 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
>   		}
>   
>   		kvfree(query_info->queries);
> +		query_info->queries = NULL;
>   	}
>   }
>   


      parent reply	other threads:[~2026-11-18 13:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  6:13 Wang Xiaolei
2026-11-16  6:28 ` 王孝雷
2026-11-18 13:21 ` Maíra Canal [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c8e13d27-7d16-481d-82bf-6f7cca80a5e4@igalia.com \
    --to=mcanal@igalia.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=itoral@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=mwen@igalia.com \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    --cc=tvrtko.ursulin@igalia.com \
    --cc=tzimmermann@suse.de \
    --cc=wangxiaolei01@kaihong.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®