[PATCH v4 0/2] cpufreq/amd-pstate: Prevent scheduling when atomic on PREEMPT_RT
From: K Prateek Nayak
Date: Mon Mar 16 2026 - 04:19:43 EST
Bert reported hitting "BUG: scheduling while atomic" when running
amd-pstate-ut on a PREEMPT_RT kernel [1].
Since reader-writer locks turn sleepable on PREEMPT_RT, they are not
suitable to be used in the scheduler hot-path under rq_lock to grab the
cpufreq policy object.
Unfortunately, the amd-pstate driver has a tight coupling between the
cpufreq_policy object and the cpudata stored in it as the driver_data.
Trying to grab a read reference on PREEMPT_RT can cause "scheduling
while atomic" if a concurrent writer is active, and trying to grab a
nested reference in presence of a writer can cause a deadlock (manifests
as lockup) since the reader fast-path is disabled on PREEMPT_RT to
prevent write-side starvation.
The two patches included removes cases of grabbing a nested read
reference to the cpufreq policy in amd-pstate, and modifies the
cpufreq_driver->adjust_perf() callback to take the raw policy reference
cached by the schedutil governor respectively.
The policy object outlives the governor and the driver making it safe to
use this cached reference from the sugov data. Any changes to the policy
will end up calling cpufreq_driver->set_policy() or
governor->set_limits() once the policy is modified which should ensure
eventual consistency despite not holding the read-side.
Series has been tested with amd-pstate-ut on PREEMPT_RT kernel which
successfully passes without any splats on LOCKDEP + DEBUG_ATOMIC_SLEEP
config. Additionally, the driver switch test from Gautham [2] was run
for 10min on the same config without observing any splats.
[1] https://lore.kernel.org/all/20250731092316.3191-1-spasswolf@xxxxxx/
[2] https://lore.kernel.org/all/aJRN2wMLAnhDFykv@xxxxxxxxxxxxxxxxxxxx/
Since the generic cpufreq changes also affect the rust bindings, Patch2
includes the necessary rust/ binding fixes to preserve bisection.
Patches are based on:
git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git bleeding-edge
at commit 18ca40891b83 ("Merge branch 'pm-cpuidle-fixes' into
bleeding-edge") (14-03-2026).
Everyone has been Cc'd on the cover letter. Respective Maintainers,
Reviewers, and the folks from get_maintainer.pl have been Cc'd on the
respective patches. The amd-pstate maintainers, PREEMPT_RT maintainers,
and the lists get the entire series.
Rafael, since this touches the generic cpufreq layer, it would be good
to route this through linux-pm. Upcoming amd-pstate changes from Gautham
[3] apply cleanly on top of this and doesn't have any dependencies.
[3] https://lore.kernel.org/lkml/20260311140116.19604-1-gautham.shenoy@xxxxxxx/
If you would like to see anything changed, please let me know and I'll
address it in the next version.
---
Chnagelog v3..v4:
o Rebased and tested on the latest linux-pm:bleeding-edge.
o Added a "Fixes:" tag to Patch 2. (Gautham)
v3: https://lore.kernel.org/lkml/20260116063234.24084-1-kprateek.nayak@xxxxxxx/
Changelog v2..v3:
o Fixed the rust bindings for adjust_perf_callaback in Patch 2 (kernel
test robot).
o Tested PREEMPT_RT + CONFIG_RUST builds using amd-pstate-ut.
o Viresh's ack on Patch 2 was retained since the incremental diff for
Rust bindings was discussed on v2. (Viresh let me know in case you
have additional comments and if I should drop the tag.)
o Adding the Rust and sched folks to Cc for awareness on rust bindings
and the schedutil bits respectively.
v2: https://lore.kernel.org/lkml/20260114085113.21378-1-kprateek.nayak@xxxxxxx/
Changelog rfc v1..v2:
o Updated the kdoc comment above cpufreq_driver_adjust_perf() in Patch 2
to reflect that cpufreq_policy is passed as an argument now instead of
the target CPU. (kernel test robot)
o Added "Reported-by:" and "Closes:" tags to Patch 2. (Mario)
o Collected tags from v1. (Thank you Mario, Viresh)
o Dropped the RFC tag.
v1: https://lore.kernel.org/lkml/20260106073608.278644-1-kprateek.nayak@xxxxxxx/
---
K Prateek Nayak (2):
cpufreq/amd-pstate: Pass the policy to amd_pstate_update()
cpufreq: Pass the policy to cpufreq_driver->adjust_perf()
drivers/cpufreq/amd-pstate.c | 14 +++++---------
drivers/cpufreq/cpufreq.c | 6 +++---
drivers/cpufreq/intel_pstate.c | 4 ++--
include/linux/cpufreq.h | 4 ++--
kernel/sched/cpufreq_schedutil.c | 5 +++--
rust/kernel/cpufreq.rs | 13 ++++++-------
6 files changed, 21 insertions(+), 25 deletions(-)
base-commit: 18ca40891b83f01b35537f4cc93c9d95528eaf42
--
2.34.1