scsi: ufs: core: update clkscale is_enabled under clk_scaling.lock
Subsystems (2)
PATCH 1/1 scsi: ufs: core: update clkscale is_enabled under clk_scaling.lock
ufshcd_clk_scaling_start_busy() reads clk_scaling.is_enabled and queues clk_scaling.resume_work under clk_scaling.lock. The clkscale_enable store writes is_enabled without that lock and, when it disables scaling, then calls ufshcd_suspend_clkscaling(), which cancels resume_work and marks clock scaling suspended.
A request that starts while clock scaling is being disabled can read the old value and queue resume_work after it was cancelled:
clkscale_enable_store(0) ufshcd_clk_scaling_start_busy()
------------------------ -------------------------------
lock clk_scaling.lock is_enabled is still true
is_enabled = false
ufshcd_suspend_clkscaling()
cancel_work_sync(resume_work)
queue_work(resume_work) unlock clk_scaling.lock
is_suspended = true
devfreq_suspend_device()
ufshcd_clk_scaling_resume_work()
is_suspended = false devfreq_resume_device()
devfreq is then active again although clkscale_enable reads 0, and ufshcd_devfreq_target() only checks is_suspended, so devfreq can change the clock frequency again (for example scale it down when the load drops). The window is a few instructions inside the locked section of start_busy().
Write is_enabled under clk_scaling.lock. ufshcd_clk_scaling_start_busy() then either sees the new value, or it queued resume_work before the store and ufshcd_suspend_clkscaling() cancels or waits for that work.
Reproduced on v7.4-rc4 in QEMU with two debug-only changes, not part of this patch: clock scaling enabled on the emulated UFS controller with a fake clock entry (no clock or gear is actually changed), and a delay in ufshcd_clk_scaling_start_busy() after the is_enabled check. Writing 0 to clkscale_enable during the first request after idle left devfreq resumed although scaling was disabled in 5 of 5 runs; with this patch, in none of 5 runs. Without the delay (2 control runs) we did not see it. We have no UFS hardware with clock scaling.
The problem was found with a Lean-based formal model of the code and AI-assisted analysis. The patch and this description were written with AI assistance; a human checked them against the source.
---ufshcd_clkscale_enable_store() writes clk_scaling.is_enabled without clk_scaling.lock (drivers/ufs/core/ufshcd.c:1872 in v7.4-rc4), while ufshcd_clk_scaling_start_busy() reads it and queues resume_work under that lock (2303-2312). When scaling is disabled as the first request after idle starts, start_busy() can queue resume_work after ufshcd_suspend_clkscaling() cancelled it (1806); resume_work then clears is_suspended and resumes devfreq although clkscale_enable reads
0. The window is a few instructions with interrupts off; on non-RT kernels the CPU must stall exactly there (NMI, preempted vCPU).
Affected: v7.4-rc4, next-20261123, 7.3.y, 6.18.y (clk_scaling.lock exists since be769e5cf53b, v6.14). 6.12.y, 6.6.y, 6.1.y and 5.15.y use host_lock in start_busy() and need a backport using it; 5.10.y differs (is_allowed, cancel before the write).
Who can trigger it: root (the attribute is 0644). Functional bug.
Reproduced on v7.4-rc4 in QEMU (x86_64 TCG, 4 vCPUs). QEMU's UFS device has no clock scaling, so a debug-only test helper enables UFSHCD_CAP_CLK_SCALING with a fake clock entry (clk == NULL: no clock or gear change, only the scaling bookkeeping and devfreq run). A debug-only one-shot mdelay() sits in start_busy() after the is_enabled check. Writing 0 to clkscale_enable during the first read after idle:
v7.4-rc4 + delay: scaling left resumed after the write,
5 of 5 runs
+ this patch: suspended correctly, 5 of 5 runs
no delay (2 control runs): suspended correctly
Not tested on UFS hardware; how long devfreq stays active was not measured. Helper and debug patches on request.
Found by a formal code verification tool under development (Lean-based), with AI-assisted analysis; verified by the reproduction above and human review. The patch and this text were written with AI assistance and reviewed by a human. Point of contact: Zhenwen Liang <[email protected]>.
A search of public list archives, git history and syzbot found no earlier report; we could not check the review thread of be769e5cf53b.
drivers/ufs/core/ufshcd.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +7 -1
@@ -1869,7 +1869,13 @@ static ssize_t ufshcd_clkscale_enable_store(struct device *dev, goto out;ufshcd_hold (hba); - hba->clk_scaling.is_enabled = value; +/* +* ufshcd_clk_scaling_start_busy() reads is_enabled and queues +* resume_work under clk_scaling.lock; update it under the same lock +* so that no resume_work is queued after it is cancelled below. +*/ +scoped_guard (spinlock_irqsave, &hba->clk_scaling.lock) + hba->clk_scaling.is_enabled = value;if (value) {ufshcd_resume_clkscaling (hba);
Signature · 1 line
2.56.1.809.ga0b61035b5-goog