mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Maíra Canal" <mcanal@igalia.com>
To: Xiaolei Wang <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 v2] drm/v3d: Fix double free of query arrays on parse failure
Date: Fri, 20 Nov 2026 13:49:19 -0300	[thread overview]
Message-ID: <3f029973-1a52-4118-acd5-4a433fb49710@igalia.com> (raw)
In-Reply-To: <20261120022828.2612091-1-wangxiaolei01@kaihong.com>

Hi Xiaolei,

On 19/11/26 23:28, Xiaolei Wang wrote:
> v3d_get_cpu_timestamp_query_params(), v3d_get_cpu_reset_timestamp_params(),
> v3d_get_cpu_copy_query_results_params() and v3d_copy_query_info() -- the
> latter shared by the reset and copy performance parsers -- free the query
> array on their error paths through v3d_timestamp_query_info_free() and
> v3d_performance_query_info_free().
> 
> 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 was only
> written after the parse loop completed -- so the syncobj loop ran zero
> times and no syncobj was put twice, but the "if (query_info->queries)" test
> still succeeded and the dangling pointer was handed to kvfree() a second
> time.
> 

====
> Every failure inside the parse loop takes this path: -ENOENT from
> drm_syncobj_find(), -EFAULT from get_user(), or -ENOMEM from
> kvmalloc_array() in v3d_copy_query_info().
====

Needless paragraph.

> 
> Fix the ownership instead of the symptom: the query array, and the per
> query kperfmon_ids, belong to the job and are freed exactly once, by
> v3d_cpu_job_free().  The parsers no longer free anything; they only fill
> the array in and record how far they got:
> 
>    - query_info->count is set before the parse loop, so the helpers know how
>      many entries were touched when parsing fails halfway;
> 
>    - the arrays are allocated with kvzalloc_objs(), so entries that were
>      never reached carry a NULL syncobj and a NULL kperfmon_ids;
> 
>    - the helpers walk the full count but test every entry, so they put
>      exactly the syncobj references that were taken, keeping
>      put-count == find-count on all failure paths;
> 
>    - the inline kvfree() of kperfmon_ids in v3d_copy_query_info() is dropped
>      for the same reason;
> 
>    - the count argument of v3d_timestamp_query_info_free() and
>      v3d_performance_query_info_free() goes away, as the count now lives in
>      query_info itself.

You don't need to describe every single detail of the code in the commit
message.

Please, make your commit message for human-readable.

[...]

> diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c
> index 1924e3440dc9..6ee64fbacae9 100644
> --- a/drivers/gpu/drm/v3d/v3d_sched.c
> +++ b/drivers/gpu/drm/v3d/v3d_sched.c
> @@ -95,34 +95,44 @@ v3d_sched_job_free(struct drm_sched_job *sched_job)
>   	v3d_job_cleanup(job);
>   }
>   
> +/*
> + * The query arrays are owned by the job: they are allocated by the extension
> + * parsers in v3d_submit.c (which also publish ->count before they start
> + * filling the entries) and they are released only here, from
> + * v3d_cpu_job_free().  The arrays are zero allocated, so on a parsing failure
> + * the entries past the last one set up are all zero and get skipped.
> + */

This comment is superfluous. Please, delete it.

>   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);
> +	if (!query_info->queries)
> +		return;
>   
> -		kvfree(query_info->queries);
> +	for (i = 0; i < query_info->count; i++) {

Stop the loop at the first NULL syncobj.

if (!query_info->queries[i].syncobj)
     break;

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

[...]

> @@ -915,7 +885,8 @@ v3d_copy_query_info(struct v3d_performance_query_info *query_info,
>   		    struct drm_file *file_priv)
>   {
>   	unsigned int i, j;
> -	int err;
> +
> +	query_info->count = count;

Leave this in the functions that call v3d_copy_query_info() and remove
count from v3d_copy_query_info()'s parameters.

Best regards,
- Maíra



  reply	other threads:[~2026-11-20 16:49 UTC|newest]

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

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=3f029973-1a52-4118-acd5-4a433fb49710@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®