]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
sched/psi: Shut down rtpoll_timer in psi_cgroup_free()
authorTejun Heo <tj@kernel.org>
Sun, 12 Jul 2026 17:23:55 +0000 (07:23 -1000)
committerTejun Heo <tj@kernel.org>
Wed, 15 Jul 2026 15:00:57 +0000 (05:00 -1000)
psi_schedule_rtpoll_work() is called locklessly from the scheduler hotpath
and can race psi_trigger_destroy() taking down the last rtpoll trigger under
rtpoll_trigger_lock:

  psi_schedule_rtpoll_work()        psi_trigger_destroy()

  rcu_read_lock();
  task = rcu_dereference(rtpoll_task);
                                    rcu_assign_pointer(rtpoll_task, NULL);
                                    timer_delete(&rtpoll_timer);
  mod_timer(&rtpoll_timer, ...);
  rcu_read_unlock();
                                    synchronize_rcu();
                                    kthread_stop(task_to_destroy);

The group can then be freed with the re-armed timer still pending, and
poll_timer_fn() runs on freed memory.

461daba06bdc ("psi: eliminate kthread_worker from psi trigger scheduling
mechanism") deleted the timer synchronously after the synchronize_rcu(),
which prevented this but raced trigger creation instead: the deletion could
cancel the timer that a new trigger set armed during the grace period and,
as creation also reinitialized the timer at the time, corrupt it.
8f91efd870ea ("psi: Fix race between psi_trigger_create/destroy") moved the
initialization into group_init() and the deletion into the locked section,
trading the creation races for the window above.

Neither placement in the destruction path works. A pending timer firing
while the group is alive is harmless though. poll_timer_fn() just wakes the
rtpoll waitqueue and doesn't re-arm itself. Bind the timer to the group's
lifetime instead and shut it down in psi_cgroup_free(). Nothing can arm it
by then. timer_shutdown_sync() because the timer is never armed again.

Fixes: 8f91efd870ea ("psi: Fix race between psi_trigger_create/destroy")
Cc: stable@vger.kernel.org # v5.10+
Reported-by: Sashiko AI <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/20260711000434.36C4A1F000E9@smtp.kernel.org/
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: Johannes Weiner <hannes@cmpxchg.org>
Tested-by: Matt Fleming <mfleming@cloudflare.com>
Acked-by: Suren Baghdasaryan <surenb@google.com>
kernel/sched/psi.c

index 565ec7b807432b7262d203bb66a926b1b1332b97..e2e825dcd088fcc4563be45fc5ba4bca8e73e4a0 100644 (file)
@@ -1134,6 +1134,12 @@ void psi_cgroup_free(struct cgroup *cgroup)
                return;
 
        cancel_delayed_work_sync(&cgroup->psi->avgs_work);
+       /*
+        * A psi_schedule_rtpoll_work() call racing the last trigger's
+        * destruction may have re-armed the timer after psi_trigger_destroy()
+        * deleted it. Spurious firing while the group is alive is harmless.
+        */
+       timer_shutdown_sync(&cgroup->psi->rtpoll_timer);
        free_percpu(cgroup->psi->pcpu);
        /* All triggers must be removed by now */
        WARN_ONCE(cgroup->psi->rtpoll_states, "psi: trigger leak\n");