Re: [PATCH v2] drm/v3d: Fix double free of query arrays on parse failure

From: Maíra Canal

Date: Fri Nov 20 2026 - 11:49:40 EST


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