Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/atomic: race on obj->lock after unlock in
  drm_atomic_private_obj_fini()
--

commit 3a87086f7fda1b1abd7935fa8244d82c32bc24a2
Author: Luca Ceresoli <[email protected]>

drm/atomic: drm_atomic_private_obj_fini: protect private_obj removal from list

This commit wraps the list_del_init(&obj->head) call in
drm_atomic_private_obj_fini() with DRM_MODESET_LOCK_ALL_BEGIN() and
DRM_MODESET_LOCK_ALL_END() to serialize private object removal against
concurrent modeset locking.

> diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c
> index 1c3fe07b327ec..096f85419b369 100644
> --- a/drivers/gpu/drm/drm_atomic.c
> +++ b/drivers/gpu/drm/drm_atomic.c
> @@ -1106,6 +1106,9 @@ EXPORT_SYMBOL(drm_atomic_private_obj_init);
>  void
>  drm_atomic_private_obj_fini(struct drm_private_obj *obj)
>  {
> +     struct drm_modeset_acquire_ctx ctx;
> +     int ret = 0;
> +
>       /*
>        * Some callers ignore drm_atomic_private_obj_init() failures
>        * and unconditionally register a teardown action that calls
> @@ -1117,7 +1120,10 @@ drm_atomic_private_obj_fini(struct drm_private_obj 
> *obj)
>       if (list_empty(&obj->head))
>               return;
>  
> +     DRM_MODESET_LOCK_ALL_BEGIN(obj->dev, ctx, 0, ret);
>       list_del_init(&obj->head);
> +     DRM_MODESET_LOCK_ALL_END(obj->dev, ctx, ret);
> +
>       obj->funcs->atomic_destroy_state(obj, obj->state);
>       drm_modeset_lock_fini(&obj->lock);
>  }

[Severity: High]
Can a thread that already observed obj before list_del_init(&obj->head)
acquire obj->lock as soon as DRM_MODESET_LOCK_ALL_END() drops it, racing
with atomic_destroy_state() and drm_modeset_lock_fini()?

For example, if drm_modeset_lock_all_ctx() hits -EDEADLK on obj->lock
(because another atomic operation holds obj->lock), modeset_lock() saves
&obj->lock in ctx->contended and drm_modeset_backoff() drops all held
locks, including connection_mutex, before slow-locking contended:

CPU0                                  CPU1
drm_mode_obj_get_properties_ioctl()
  DRM_MODESET_LOCK_ALL_BEGIN()
    drm_modeset_lock_all_ctx()
      drm_modeset_lock(&obj->lock)
        ctx->contended = &obj->lock
        returns -EDEADLK
  DRM_MODESET_LOCK_ALL_END()
    drm_modeset_backoff()
      contended = ctx->contended
      drm_modeset_drop_locks()
                                      drm_bridge_detach()
                                        drm_atomic_private_obj_fini()
                                          DRM_MODESET_LOCK_ALL_BEGIN()
                                          list_del_init(&obj->head)
                                          DRM_MODESET_LOCK_ALL_END()
      modeset_lock(contended, ...)
        list_add(&obj->lock.head,
                 &ctx->locked)
                                          atomic_destroy_state()
                                          drm_modeset_lock_fini(&obj->lock)
                                            WARN_ON(!list_empty(&lock->head))
                                        ...
                                        drm_bridge_put() -> kfree(bridge)
    drm_modeset_drop_locks()
      /* touches freed obj->lock */  <-- use-after-free

The same race occurs if another thread blocks directly on obj->lock
without holding connection_mutex, such as drm_atomic_get_bridge_state()
while bridge->chain_node is still on encoder->bridge_chain (since
drm_bridge_detach() unlinks chain_node after drm_atomic_private_obj_fini()),
or __drm_state_dump().  When DRM_MODESET_LOCK_ALL_END() unlocks obj->lock,
the waiter wakes up and duplicates or reads obj->state while
atomic_destroy_state() frees it.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=15

Reply via email to