Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/syncobj: unconverted handles allocation in
  drm_syncobj_array_find()
--

commit 9cfbb7c668583619e7699957c17e9063e116a22e
Author: Tvrtko Ursulin <[email protected]>

drm/syncobj: Add temporary array allocation fast paths

This commit adds stack-backed fast-path macros for temporary syncobj array
allocations up to four elements and converts the syncobj wait, reset,
signal, and query paths to use them.

> diff --git a/drivers/gpu/drm/drm_syncobj.c b/drivers/gpu/drm/drm_syncobj.c
> index 6b4df322b12a8..cde7835744177 100644
> --- a/drivers/gpu/drm/drm_syncobj.c
> +++ b/drivers/gpu/drm/drm_syncobj.c
> @@ -214,6 +214,53 @@
>  
>  #include "drm_internal.h"
>  
> +/*
> + * Empirically vast majority of ioctls pass in a single syncobj (96%) and 
> never
> + * more than three points. Therefore implement a fast path with a small stack
> + * array to avoid going into the allocator sometimes several times per
> + * userspace rendered frame.
> + */
> +#define DRM_SYNCOBJ_FAST_PATH_ENTRIES 4
> +
> +#define DRM_SYNCOBJ_DECLARE_FASTPATH_ARRAY(_type, _name)     \
> +     _type _name##_stack[DRM_SYNCOBJ_FAST_PATH_ENTRIES];     \
> +     _type * _name##_heap __free(kvfree) = NULL;             \
> +     _type * _name
> +
> +#define DRM_SYNCOBJ_INIT_FASTPATH_ARRAY(_name, _src, _count) ({              
> \
> +     int __ret = 0;                                                  \
> +                                                                     \
> +     if ((_count) > ARRAY_SIZE(_name##_stack)) {                     \
> +             if (_src) {                                             \
> +                     _name##_heap = vmemdup_array_user((_src),       \
> +                                                       (_count),     \
> +                                                       sizeof(*(_name))); \
> +                     if (IS_ERR(_name##_heap)) {                     \
> +                             __ret = PTR_ERR(no_free_ptr(_name##_heap)); \
> +                     } else {                                        \
> +                             _name = _name##_heap;                   \
> +                     }                                               \
> +             } else {                                                \
> +                     _name##_heap = kvzalloc_objs(*(_name), (_count)); \
> +                     if (!_name##_heap)                              \
> +                             __ret = -ENOMEM;                        \
> +                     else                                            \
> +                             _name = _name##_heap;                   \
> +             }                                                       \
> +     } else {                                                        \
> +             size_t __size = (_count) * sizeof(*(_name));            \
> +                                                                     \
> +             _name = _name##_stack;                                  \
> +             if (_src) {                                             \
> +                     if (copy_from_user(_name, (_src), __size))      \
> +                             __ret = -EFAULT;                        \
> +             } else {                                                \
> +                     memset(_name, 0, __size);                       \
> +             }                                                       \
> +     }                                                               \
> +     __ret;                                                          \
> +})

[Severity: Medium]
Was drm_syncobj_array_find() meant to be converted to use the fast-path
array macros for its temporary handles array as well?

The commit message notes that drm_syncobj_array_find() is called with one
to three objects in almost all calls, and commit "drm/syncobj: Use scoped
cleanup in drm_syncobj_array_find and callers" converted handles to scoped
cleanup, but drm_syncobj_array_find() still unconditionally allocates and
frees handles on the heap on every syncobj array ioctl:

drivers/gpu/drm/drm_syncobj.c:drm_syncobj_array_find() {
        u32 *handles __free(kvfree) = NULL;
        unsigned int i;

        handles = vmemdup_array_user(user_handles, count, sizeof(*handles));
        if (IS_ERR(handles))
                return PTR_ERR(no_free_ptr(handles));
    ...
}

Note that if drm_syncobj_array_find() is converted to
DRM_SYNCOBJ_INIT_FASTPATH_ARRAY(), a runtime NULL user_handles pointer
(args->handles == 0) must still return -EFAULT rather than taking the
!_src zero-initialization branch and looking up handle 0 (-ENOENT).

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

Reply via email to