Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/panthor: 64-bit smp_load_acquire() in session_read_extract_idx() - [High] drm/panthor: missing output handle field in struct drm_panthor_perf_session_create - [High] drm/panthor: panthor_perf_bo_get() discards iosys_map is_iomem flag - [Medium] drm/panthor: unreachable stopping check in session_try_sample() - [Low] drm/panthor: format type mismatch for size_t in panthor_perf_bo_get() - [Low] drm/panthor: split declaration of __free() vars in panthor_perf.c --
commit e36bb7b1917c06a9ff609cb883067fc62562c67b Author: Lukas Zapolskas <[email protected]> drm/panthor: Add perf setup and session management This commit adds performance counter subsystem initialization and unplug handling, along with user session lifecycle management and the associated UAPI structures. > diff --git a/drivers/gpu/drm/panthor/panthor_perf.c > b/drivers/gpu/drm/panthor/panthor_perf.c > new file mode 100644 > index 0000000000000..a4167bbc33adc > --- /dev/null > +++ b/drivers/gpu/drm/panthor/panthor_perf.c [ ... ] > +static int > +panthor_perf_bo_get(struct drm_file *file, u32 handle, size_t min_size, > + struct panthor_perf_bo *bo) > +{ > + struct drm_device *ddev = file->minor->dev; > + struct drm_gem_object *obj; > + struct iosys_map map; > + int ret; > + > + if (WARN_ON_ONCE(!bo)) > + return -EINVAL; > + > + obj = drm_gem_object_lookup(file, handle); > + if (!obj) { > + drm_err(ddev, "Could not find handle %d!\n", handle); > + return -EINVAL; > + } > + > + if (obj->size < min_size) { > + drm_err(ddev, > + "Perf BO expected min size %lu, passed BO with size > %lu\n", > + min_size, obj->size); > + ret = -ENOMEM; > + goto err_put; > + } [Severity: Low] Both min_size and obj->size have type size_t, so formatting them with %lu causes a -Wformat warning on 32-bit architectures. Should this use %zu instead? > + > + ret = drm_gem_vmap(obj, &map); > + if (ret) > + goto err_put; > + > + bo->obj = obj; > + bo->vaddr = map.vaddr; > + > + return 0; > + > +err_put: > + drm_gem_object_put(obj); > + return ret; > +} > + > +static void panthor_perf_bo_put(struct panthor_perf_bo *bo) > +{ > + if (!bo->obj) > + return; > + > + if (bo->vaddr) { > + struct iosys_map map = IOSYS_MAP_INIT_VADDR(bo->vaddr); > + > + drm_gem_vunmap(bo->obj, &map); > + } [Severity: High] What happens if userspace passes a PRIME-imported GEM handle backed by IOMEM to panthor_perf_bo_get()? For imported objects, drm_gem_vmap() delegates to dma_buf_vmap(), which can return an IOMEM mapping with map.is_iomem set to true. Because panthor_perf_bo_get() discards map.is_iomem and stores only map.vaddr, the driver dereferences session->control and session->samples directly as regular system memory, and panthor_perf_bo_put() passes an iosys_map with is_iomem = false to drm_gem_vunmap(), triggering BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map)) in dma_buf_vunmap(). [ ... ] > +#define DEFINE_SESSION_STATE_CHECK(name, _state) \ > + static bool session_is_## name(struct panthor_perf_session *session) \ > + { \ > + lockdep_assert_held(&session->state_lock); \ > + \ > + return session->state == (_state); \ > + } > + > +DEFINE_SESSION_STATE_CHECK(active, PANTHOR_PERF_SESSION_ACTIVE); > +DEFINE_SESSION_STATE_CHECK(starting, PANTHOR_PERF_SESSION_STARTING); > +DEFINE_SESSION_STATE_CHECK(stopping, PANTHOR_PERF_SESSION_STOPPING); > +DEFINE_SESSION_STATE_CHECK(destroy, PANTHOR_PERF_SESSION_DESTROY); > + > +static bool session_has_overflow(struct panthor_perf_session *session) > +{ > + lockdep_assert_held(&session->state_lock); > + > + return session->overflow; > +} > + > +static void session_set_state(struct panthor_perf_session *session, > + enum panthor_perf_session_state state) > +{ > + lockdep_assert_held(&session->state_lock); > + > + session->state = state; > +} > + > +static u64 session_read_extract_idx(struct panthor_perf_session *session) > +{ > + /* Userspace will update their own extract index to indicate that a > sample is consumed > + * from the ringbuffer, and we must ensure we read the latest value. > + */ > + return smp_load_acquire(&session->control->extract_idx); > +} [Severity: High] Does smp_load_acquire(&session->control->extract_idx) compile on 32-bit SMP architectures? session->control->extract_idx is a __u64, and on 32-bit SMP targets smp_load_acquire() triggers compiletime_assert_atomic_type() because sizeof(__u64) exceeds sizeof(long). [ ... ] > +static int session_try_sample(struct panthor_perf_session *session, u64 > user_data) > +{ > + guard(spinlock_irqsave)(&session->state_lock); > + > + if (!session_is_active(session)) > + return -EINVAL; > + > + if (session_is_stopping(session)) > + return -EBUSY; [Severity: Medium] Can session_is_stopping(session) ever be reached here? session->state holds a single enum panthor_perf_session_state value, so when the state is PANTHOR_PERF_SESSION_STOPPING, !session_is_active() is already true and session_try_sample() returns -EINVAL instead of -EBUSY. [ ... ] > +int panthor_perf_session_destroy(struct panthor_file *pfile, u32 handle) > +{ > + struct panthor_perf_session *session __free(session_put) = NULL; > + struct panthor_perf *perf = pfile->ptdev->perf; > + struct panthor_perf_pool *pool = pfile->sessions; > + int ret = -EINVAL; > + > + if (!perf) > + return -EOPNOTSUPP; > + > + session = session_from_handle(pool, handle); [Severity: Low] This isn't a bug, but cleanup.h guidelines prefer defining and initializing __free() variables in a single statement rather than initializing them to NULL at the top of the function and assigning them later. The same pattern appears in panthor_perf_session_start(), panthor_perf_session_stop(), and panthor_perf_session_sample(). > diff --git a/include/uapi/drm/panthor_drm.h b/include/uapi/drm/panthor_drm.h > index a2ff0f4ec6915..dfa852aa3690d 100644 > --- a/include/uapi/drm/panthor_drm.h > +++ b/include/uapi/drm/panthor_drm.h [ ... ] > +struct drm_panthor_perf_session_create { > + /** > + * @block_set: Set of performance counter blocks, member of > + * enum drm_panthor_perf_counter_set. > + * > + * This is a global configuration and only one set can be active at a > time. If > + * another client has already requested a counter set, any further > requests > + * for a different counter set will fail and return an -EBUSY. > + * > + * If the requested set does not exist, the request will fail and > return an -EINVAL. > + * > + * Some sets have additional requirements to be enabled, and the setup > request will > + * fail with an -EACCES if these requirements are not satisfied. > + */ > + __u32 block_set; > + > + /** @fd: eventfd for signalling the availability of a new sample. */ > + __u32 fd; > + > + /** @ringbuf_handle: Handle to the BO to write perf counter sample to. > */ > + __u32 ringbuf_handle; > + > + /** > + * @control_handle: Handle to the BO containing a contiguous 16 byte > range, used for the > + * insert and extract indices for the ringbuffer. > + */ > + __u32 control_handle; > + > + /** > + * @sample_slots: The number of slots available in the > userspace-provided BO. Must be > + * a power of 2 and at least 2, to leave space for a sample and the > final STOP sample. > + * > + * If sample_slots * sample_size is smaller than the ringbuf BO, the > setup will fail. > + */ > + __u32 sample_slots; > + > + /** @pad: MBZ. */ > + __u32 pad; [Severity: High] Should struct drm_panthor_perf_session_create have an output field for the allocated session handle instead of returning the handle via *sid as the ioctl return value? drm_ioctl() expects driver ioctl handlers to return 0 on success and a negative errno on failure, and logs an error whenever the return code is non-zero. Returning a positive session ID from the ioctl breaks standard DRM userspace wrappers that check for a zero return value on success, and differs from all other object-creation ioctls in panthor (VM_CREATE, BO_CREATE, GROUP_CREATE, TILER_HEAP_CREATE). -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=1
