Repository navigation
ASoC: apple: macaudio: Keep the speaker volumes wherever the sleep lock is taken - #2880
IronsideXXVI wants to merge 3 commits into
Conversation
|
Review at 4d45cc5. Thanks, the root cause is right: with a stream running, lock_update_work can take the sleep lock before macaudio_vlimit_pm() and the save is skipped. Moving the save to the lock transition fixes that, and the restore still only runs on the owner's first fresh ping after resume (not quiesced, not leased), so nothing comes back without the daemon holding the lock. A few points:
Testing: J604 is the only machine tested. The change reaches every speaker card on the Speaker Volume Unlock interface (SN012776, TAS5770, SSM3515 and the J700 software gain). It would help to have, at this head, an s2idle run with a stream playing and a volume readback before and after resume on one other SN012776 laptop (J713 or J615), and on one TAS5770 machine (J293/J313). Also one run with the daemon on the Speaker Protection lease, to show that nothing is kept or set on that path. |
…ck is taken The system-sleep lock clips the speaker volumes to the lock's ceiling, and the unlock after the resume sets back the volumes kept for it. They were kept only in macaudio_vlimit_pm(), after macaudio_spk_close_gate() in the same PM_SUSPEND_PREPARE. With a speaker stream still running, the stop that the gate queues triggers the back-ends, lock_update_work reaches macaudio_vlimit_update() with pm_quiesced already set and takes the lock first. macaudio_vlimit_pm() then finds the volumes locked and keeps nothing, and the unlock has nothing to set back. speakersafetyd writes a volume only when its gain changes, so the six amps stay at 1 of 201 (DVC 0xc8) for the rest of the boot. Keep the volumes in macaudio_vlimit_update() itself, at the transition to the system-sleep lock, so it does not matter which path takes it. If the unlock still finds nothing kept (an allocation or a read failed), report the lock as lost so that the daemon sets the volumes again, as it does after a lease timeout. The flag that marks the sleep lock is cleared wherever the kept volumes go, in macaudio_pm_volumes_drop(). Log at debug level how many volumes were kept and set back. Keeping them walks the card's controls, so every path into macaudio_vlimit_update() must hold controls_rwsem. All do but the lease timeout work, which can expire the lease between pm_quiesced being set and macaudio_vlimit_pm(): take the rwsem for reading there too, before volume_lock_mutex, in the order the other holders use. The lockdep_assert_held() checks in macaudio_pm_volumes_save() were reached only from macaudio_vlimit_pm(); they are now reachable from every caller of macaudio_vlimit_update(): the lock control put, lock and unlock callbacks, the Speaker Protection enter and leave paths, lock_update_work and the lease timeout work. Each holds controls_rwsem and volume_lock_mutex there. Fixes: 911d47c ("ASoC: apple: macaudio: Set the speaker volumes again after a system sleep") Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
Add KUnit cases for the sleep lock taken through lock_update_work before macaudio_vlimit_pm() (the volumes must come back at the first ping after resume), for a failed keeping (the unlock sets nothing back and the next ping reports the lost lock once), and for a switch to Speaker Protection and back between the resume and the unlock (the way back reports the lost lock, and the unlock after it no second time). Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
4d45cc5 to
72f15f4
Compare
|
Thanks Ryan. Pushed 4d45cc5 -> 72f15f4. It is two commits now: the fix (a5e04e4), then the tests (72f15f4).
Testing at 72f15f4: KUnit apple-speaker* on arm64 qemu passes 59/59. macaudio.o builds with W=1 and no warnings, and checkpatch --strict is clean on both commits. The J604 is the only Mac I have, so I can't do the J713/J615 or J293/J313 runs. If someone with one of those can, that would help. The Speaker Protection lease run on the J604 (s2idle with the daemon on the lease, nothing kept or set) needs a kernel at this head. It's queued, and I'll post the numbers here when it has run. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
Formal §1–§6 pre-PR checklist review at 72f15f423932c9d95456f4d55dac278ffbdebb4b (host-tests: still QUEUED / UNSTABLE).
The root-cause fix and follow-up updates at 72f15f42 (clearing pm_lock_taken in macaudio_pm_volumes_drop(), adding down_read(&ma->card.snd_card->controls_rwsem) in macaudio_vlimit_timeout_work(), verified locking order across all callers including sound/core/control.c lock/unlock, dev_dbg for routine save/restore logs, and the three new KUnit tests) are well-structured and fail-safe on save failure.
Blockers
- CI not green:
host-testsfor72f15f423932c9d95456f4d55dac278ffbdebb4bis stillQUEUED(mergeStateStatus: UNSTABLE); cannot approve until green. - §5 Hardware evidence stale / incomplete for current head
72f15f42and touched paths:- The 8-boot J604 table in the PR body was run on the previous head (
4d45cc5a), not on72f15f42, and omits the required §5 regression check (0 SELinux denials, 0 failed units, 0 new kernel errors). - The J604 Speaker Protection lease (
speakerguardd v2) s2idle run at72f15f42is still pending (noted as queued in your comment). macaudio.caffects all speaker amp families on Speaker Volume Unlock (SN012776, TAS5770, SSM3515, MAX98360A/J700); a non-J604 hardware row (e.g. TAS5770 or another SN012776 Mac) is still needed.
- The 8-boot J604 table in the PR body was run on the previous head (
- §2 Data race / torn read of
ma->pm_quiescedinmacaudio_vlimit_update()(sound/soc/apple/macaudio.c:797,822,834):
macaudio_pm_notify()updatesma->pm_quiescedusingWRITE_ONCE()without holdingvolume_lock_mutex(lines 1615, 1633), and other readers useREAD_ONCE(ma->pm_quiesced).macaudio_vlimit_update()readsma->pm_quiescedthree separate times withoutREAD_ONCE()or a local snapshot. Snapshotbool quiesced = READ_ONCE(ma->pm_quiesced);once at the top ofmacaudio_vlimit_update()(and useREAD_ONCE()inmacaudio_slk_put()line 3894) so all three checks in a single call observe a consistent state and stay KCSAN-clean.
Checklist summary
- §1 No model gating: Pass in code (amp-agnostic); multi-device test coverage pending.
- §2 Production-ready: Fix
READ_ONCE(ma->pm_quiesced)snapshot inmacaudio_vlimit_update(). - §3 Safety: Pass (fail-closed ceiling preserved; failed save falls back to
-ETIMEDOUTlost-lock notification so the daemon re-applies safe model volumes; verify with the queued Speaker Protection lease /speakerguardd v2run). - §4 Clean-room & attribution: Pass (human author/committer + human
Signed-off-by:only, zero AI trailers in commits,Assisted-by:in PR body). - §5 Evidence: Blocked on current-head (
72f15f42) J604 + regression counts + Speaker Protection lease row + non-J604 amp family row. - §6 Naming: Pass.
Assisted-by: LLM (Gemini 4 Neon)
|
Review at 72f15f4. The PR is now two commits: the fix a5e04e4 plus the tests 72f15f4, on the same base e0a9448. Status of our points:
No new issues in the delta. Heads-up for whoever merges: this PR and linux#2893 both touch Verdict: APPROVE on code. Hardware still owed at this head, all s2idle with a stream playing plus speaker volume readback before and after resume:
|
macaudio_pm_notify() sets and clears pm_quiesced with WRITE_ONCE() before it takes volume_lock_mutex, so macaudio_vlimit_update() can run while the flag changes. It read the flag three times: for the lock reason, for the lost-lease flag and for keeping the volumes. A change between those reads could lock the volumes for system suspend without keeping them, or mark a lost lease for the kernel's own sleep lock. Read the flag once with READ_ONCE() into a local and use it at all three places. Use READ_ONCE() in macaudio_slk_put() too, as the other readers outside the notifier already do. Signed-off-by: Dylan Ironside <dylan@dylanironside.io>
|
Thanks both. Pushed 72f15f4 -> 004231b. The fix and the test commits are unchanged; 004231b is a third commit on top of them.
|
Follow-up to 911d47c (from #820). That commit keeps the speaker volumes when the system-sleep lock clips them, and sets them back at the unlock after resume. It only keeps them in macaudio_vlimit_pm(), though.
If a speaker stream is still running at PM_SUSPEND_PREPARE, the stop that macaudio_spk_close_gate() queues reaches macaudio_vlimit_update() through lock_update_work first, and takes the lock there. macaudio_vlimit_pm() then finds the volumes already locked and keeps nothing, so the unlock has nothing to set back. speakersafetyd only writes a volume when its gain changes, so on my J604 the six amps stayed at 1 of 201 for the rest of the boot after a suspend with music playing.
This keeps the volumes in macaudio_vlimit_update() at the transition to the sleep lock, whichever path gets there first. If the unlock still finds nothing kept (an allocation or a read failed), it reports the lock as lost, so the daemon sets the volumes again the way it does after a lease timeout. Keeping the volumes walks the card's controls, so the lease timeout work now takes controls_rwsem for reading like the other callers, before volume_lock_mutex. No new switch: it is just on.
Tested on my J604 (M4 MacBook Pro 14"). Each run is a fresh boot, music playing through the speakers, one s2idle cycle, resume, then all six amp volumes read back:
With the patch, all 8 boots logged "Kept 6 speaker volumes for the system sleep" and "Set 6 speaker volumes again after the system sleep", and all six volumes read 201 after resume. In one of the 8 boots a deep sleep attempt also failed and fell back to s2idle. The volumes came back there too, and this patch adds no path that can veto a sleep, so I don't think that one comes from here.
The change builds with W=1 and no warnings, checkpatch --strict is clean, and the apple-speaker KUnit suites pass on arm64 qemu (59 of 59 at 004231b, pm-volumes 5 of 5). The new bug case fails on the base without the fix.
Assisted-by: LLM (Claude:Opus 5.5, Claude:Fable 5.2)