Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/v3d: unbounded user in_sync_count passed to kcalloc() in
  v3d_job_add_syncobj_dep()
- [Medium] drm/v3d: duplicate unlocked syncobj lookup and TOCTOU race in
  v3d_job_add_syncobj_dep()
--

commit 306a08e6c3987e679232c1d45332551d2561d4dd
Author: Tarel Ethridge <[email protected]>

drm/v3d: Propagate a failed job's error to dependent jobs

This commit records a job's syncobj dependency fences and the previous
job's done_fence on struct v3d_job so that run_job() callbacks can
propagate any error from those fences to the job's finished fence.

> diff --git a/drivers/gpu/drm/v3d/v3d_submit.c 
> b/drivers/gpu/drm/v3d/v3d_submit.c
> index eada30a317015..3f464128cf4b5 100644
> --- a/drivers/gpu/drm/v3d/v3d_submit.c
> +++ b/drivers/gpu/drm/v3d_submit.c

[ ... ]

> @@ -203,6 +207,34 @@ void v3d_job_put(struct v3d_job *job)
>       kref_put(&job->refcount, job->free);
>  }
>  
> +/* Remember the fence a syncobj dependency waits on, so that run_job() can
> + * tell whether the work this job depends on failed.
> + */
> +static int
> +v3d_job_add_syncobj_dep(struct v3d_job *job, struct drm_file *file_priv,
> +                     u32 handle, u32 max_deps)
> +{
> +     struct dma_fence *fence = NULL;
> +     int ret;
> +
> +     ret = drm_sched_job_add_syncobj_dependency(&job->base, file_priv, 
> handle, 0);
> +     if (ret)
> +             return ret;
> +
> +     if (!job->dep_fences) {
> +             job->dep_fences = kcalloc(max_deps, sizeof(*job->dep_fences),
> +                                       GFP_KERNEL);
> +             if (!job->dep_fences)
> +                     return -ENOMEM;
> +     }
> +
> +     if (job->dep_count < max_deps &&
> +         !drm_syncobj_find_fence(file_priv, handle, 0, 0, &fence) && fence)
> +             job->dep_fences[job->dep_count++] = fence;

[Severity: Medium]
Can the second drm_syncobj_find_fence() lookup observe a different fence
than the one drm_sched_job_add_syncobj_dependency() just added?

drm_sched_job_add_syncobj_dependency() resolves handle via
drm_syncobj_find_fence() and releases the syncobj locks before
v3d_job_add_syncobj_dep() looks up handle a second time.  If another
thread or an in-flight CPU query job replaces or resets the syncobj fence
in between, job->dep_fences records a different fence (or none at all)
than job->base.dependencies, so v3d_job_failed() either misses the error
from the fence the scheduler waited on or checks an unrelated newer fence.

Looking up the fence once with drm_syncobj_find_fence(), saving an extra
reference with dma_fence_get() in job->dep_fences, and passing the fence
to drm_sched_job_add_dependency() would avoid both the race and the
duplicate lookup.

> +
> +     return 0;
> +}
> +
>  static int
>  v3d_job_add_syncobjs(struct v3d_job *job, struct drm_file *file_priv,
>                    u32 in_sync, struct v3d_submit_ext *se)
> @@ -214,8 +246,7 @@ v3d_job_add_syncobjs(struct v3d_job *job, struct drm_file 
> *file_priv,
>       if (!has_multisync) {
>               /* Ignore syncobj if its handle is zero */
>               if (in_sync)
> -                     ret = drm_sched_job_add_syncobj_dependency(&job->base, 
> file_priv,
> -                                                                in_sync, 0);
> +                     ret = v3d_job_add_syncobj_dep(job, file_priv, in_sync, 
> 1);
>               return ret;
>       }
>  
> @@ -232,8 +263,8 @@ v3d_job_add_syncobjs(struct v3d_job *job, struct drm_file 
> *file_priv,
>  
>                       /* Ignore syncobj if its handle is zero */
>                       if (in.handle) {
> -                             ret = 
> drm_sched_job_add_syncobj_dependency(&job->base,
> -                                                                        
> file_priv, in.handle, 0);
> +                             ret = v3d_job_add_syncobj_dep(job, file_priv, 
> in.handle,
> +                                                           
> se->in_sync_count);

[Severity: Medium]
Can passing the unvalidated se->in_sync_count as max_deps to kcalloc()
trigger a page allocator warning or high-order allocation stall from
userspace?

v3d_get_multisync_submit_deps() copies multisync.in_sync_count directly
from userspace without bounds checking.  On the first valid non-zero
in.handle, v3d_job_add_syncobj_dep() allocates job->dep_fences for the
entire se->in_sync_count via kcalloc(..., GFP_KERNEL) before the rest of
the user array is copied, so a single valid handle with a large
in_sync_count triggers WARN_ON_ONCE_GFP(size > KMALLOC_MAX_SIZE, flags)
in the slab allocator.

-- 
Sashiko AI review · 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to