Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/tests: race on inflight counter in
  drm_test_flood_commit_work()
- [Medium] drm/tests: premature inflight dec in
  drm_test_flood_commit_work() causes UAF
--

commit 6c86373dc620a9330ac1f7518a644a35a7fe6819
Author: Ville Syrjälä <[email protected]>

drm/tests: Test limiting the run-ahead of fake commit only commits

This commit adds a KUnit test that floods nonblocking commits touching only
an unbound plane with swap_state(stall=false) to verify that in-flight
commits stay bounded and do not time out waiting for hw_done.

> diff --git a/drivers/gpu/drm/tests/drm_atomic_commit_test.c 
> b/drivers/gpu/drm/tests/drm_atomic_commit_test.c
> index 593e1842fada2..247b5a916b585 100644
> --- a/drivers/gpu/drm/tests/drm_atomic_commit_test.c
> +++ b/drivers/gpu/drm/tests/drm_atomic_commit_test.c
[ ... ]
> @@ -495,6 +496,88 @@ static void drm_test_fake_commit_not_busy(struct kunit 
> *test)
>       drm_modeset_acquire_fini(&ctx);
>  }
>  
> +static atomic_t drm_test_flood_inflight, drm_test_flood_max, 
> drm_test_flood_timeouts;
> +
> +static void drm_test_flood_commit_work(struct work_struct *work)
> +{
> +     struct drm_atomic_commit *state =
> +             container_of(work, struct drm_atomic_commit, commit_work);
> +     struct drm_plane *plane;
> +     struct drm_plane_state *old_plane_state;
> +     int i;
> +
> +     for_each_old_plane_in_state(state, plane, old_plane_state, i)
> +             if (drm_crtc_commit_wait(old_plane_state->commit))
> +                     atomic_inc(&drm_test_flood_timeouts);
> +
> +     /* Pretend to do some work */
> +     msleep(2);
> +
> +     drm_atomic_helper_commit_hw_done(state);
> +     drm_atomic_helper_commit_cleanup_done(state);
> +     atomic_dec(&drm_test_flood_inflight);
> +     drm_atomic_commit_put(state);
> +}

[Severity: Medium]
Can decrementing drm_test_flood_inflight before drm_atomic_commit_put(state)
cause a use-after-free on dev->driver during test teardown?

CPU0 (kunit)                      CPU1 (kworker)
drm_test_fake_commit_flood()      drm_test_flood_commit_work()
  /* sees inflight == 0 */          atomic_dec(&drm_test_flood_inflight)
kunit_cleanup()
  ...
    devres_release_all(dev)
      /* frees dev->driver */
                                    drm_atomic_commit_put(state)
                                      __drm_atomic_commit_free()
                                        drm_dev_put(dev)
                                          drm_dev_release()
                                            dev->driver->release
                                              <-- use-after-free

__drm_kunit_helper_alloc_drm_device() allocates struct drm_driver with
devm_kzalloc_obj(dev, *driver), while each drm_atomic_commit holds a
drm_device reference via drm_dev_get() until __drm_atomic_commit_free().
Because drm_test_fake_commit_flood() only waits for drm_test_flood_inflight
to reach 0 without flushing the work items, KUnit cleanup can unregister the
mock device and free dev->driver before the final worker finishes
drm_atomic_commit_put(state).

> +
> +/*
> + * Test that a flood of nonblocking commits that only touch an unbound plane
> + * can not queue up an unbounded number of commits, even if the driver does 
> not
> + * ask swap_state() to stall. Otherwise the work items would all wait for the
> + * hw_done of their predecessor, and the waits would add up.
> + */
> +static void drm_test_fake_commit_flood(struct kunit *test)
> +{
[ ... ]
> +             INIT_WORK(&state->commit_work, drm_test_flood_commit_work);
> +             queue_work(system_dfl_wq, &state->commit_work);
> +
> +             drm_modeset_drop_locks(&ctx);
> +             drm_modeset_acquire_fini(&ctx);
> +     }
> +
> +     while (atomic_read(&drm_test_flood_inflight))
> +             msleep(10);
> +
> +     /*
> +      * The previous commit may still be doing its cleanup when the next
> +      * one is submitted, hence 2 rather than 1.
> +      */
> +     KUNIT_EXPECT_LE(test, atomic_read(&drm_test_flood_max), 2);

[Severity: Medium]
Can drm_test_flood_max exceed 2 when workers on system_dfl_wq are preempted
between drm_atomic_helper_commit_hw_done() and
atomic_dec(&drm_test_flood_inflight)?

drm_atomic_helper_commit_hw_done(state) completes state->fake_commit->hw_done,
which immediately unblocks stall_for_fake_commit() in
drm_atomic_helper_setup_commit() for the next commit on the test thread.
Neither stall_for_fake_commit() nor drm_crtc_commit_wait() waits for
cleanup_done or work completion, and system_dfl_wq is an unbound workqueue
that can run multiple workers concurrently.  If more than one worker is
preempted after signaling hw_done before reaching
atomic_dec(&drm_test_flood_inflight), drm_test_flood_max will be greater than
2 and fail the expectation.  In addition, exiting the wait loop as soon as
drm_test_flood_inflight reaches 0 without flushing the work items allows test
teardown to race with the final drm_atomic_commit_put(state).

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

Reply via email to