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: [email protected]
Cc: Maíra Canal <[email protected]>
Cc: Iago Toral Quiroga <[email protected]>
Cc: Tvrtko Ursulin <[email protected]>
Signed-off-by: Wang Xiaolei <[email protected]>
---
  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;
        }
  }

Reply via email to