Repository navigation
ASoC: apple: macaudio: Keep the speaker volumes wherever the sleep lock is taken - #2880
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. |
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:
|
|
Thanks both. Pushed 72f15f4 -> 004231b. The fix and the test commits are unchanged; 004231b is a third commit on top of them.
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Verdict: HOLD at 004231bfab03
Blockers:
sound/soc/apple/macaudio.c:779-784(merge conflict): Branch isCONFLICTING(DIRTY) againstmaininmacaudio_vlimit_update()where004231bfab03insertsquiesced = READ_ONCE(ma->pm_quiesced);at the same sitemain(3fa618e415f6, merged in065764c614c8/ #1862) addedif (macaudio_spk_lease_only(ma->cfg)) return;. Rebase ontomainkeepingmacaudio_spk_lease_only()before theREAD_ONCE(ma->pm_quiesced)snapshot.- §4 PR body (AI attribution): Remove the trailing
Assisted-by:line from the PR description (hard rule: no AI attribution anywhere in PR bodies, commits, or comments). - §5 PR body structure: Add the required visible header fields (
What:,Tested:,Risk:,Not yet:) with the two-way door / affected Macs summary and folded<details>for evidence. - §5 Hardware evidence at head: Post an exact-head run on J604 (after rebasing onto
main, sincemacaudio.chas a conflict) covering both theSpeaker Volume Unlocks2idle stream-playing test and the queuedSpeaker Protection(speakerguardd v2) lease s2idle check, plus regression counts (0 failed units, 0 new kernel errors, and SELinux status). A non-J604 row on one other Mac sharingmacaudio.c(e.g., lab SN012776 or TAS5770) is also still needed for shared code.
Checked: Interdiff 72f15f423932..004231bfab03 (READ_ONCE(ma->pm_quiesced) fix is clean and resolves the prior §2 data-race blocker), 3-way merge against main (a9cd131e8207), §1–§6 pre-PR checklist, commit hygiene (all 3 commits clean), and CI (host-tests: SUCCESS).
Evidence
- Interdiff (
72f15f423932..004231bfab03): Single commit004231bfab03(ASoC: apple: macaudio: Read pm_quiesced once per volume-lock update) touching onlysound/soc/apple/macaudio.c. Snapshotsquiesced = READ_ONCE(ma->pm_quiesced)once inmacaudio_vlimit_update()and reuses it across all three branches (lock reason,speaker_volume_was_locked, andmacaudio_pm_volumes_save()/pm_lock_taken), and switchesmacaudio_slk_put()toREAD_ONCE(ma->pm_quiesced). No plain reads ofpm_quiescedremain inmacaudio.c. - Conflict check against
main(a9cd131e8207):sound/soc/apple/macaudio.c: CONFLICT at lines 779–784 (macaudio_vlimit_update()vs3fa618e415f6).sound/soc/apple/macaudio-pm-volumes-test.h: merges cleanly.sound/soc/apple/macaudio-iface-test.h: merges cleanly.
- §1 No model gating: Pass in code (amp-agnostic across SN012776, TAS5770, SSM3515, MAX98360A/J700). Multi-Mac test row still pending.
- §2 Production-ready: Pass in code (default cmdline, no sleeps/retries, prior
pm_quiescedtorn-read blocker fixed). Blocked only bymainmerge conflict. - §3 Safety: Pass in code.
macaudio_pm_volumes_save()runs undercontrols_rwsem(read) +volume_lock_mutexbefore clipping; save failure leaves volumes at ceiling and forces-ETIMEDOUTlost-lock recovery;macaudio_pm_volumes_drop()clearspm_lock_takenon lease transitions, new owner, and teardown;macaudio_spk_leased()short-circuits forspeakerguardd v2; synchronous cancels oflock_timeout_workrun withoutcontrols_rwsemorvolume_lock_mutex. Preservemacaudio_spk_lease_only()early return when resolving the conflict. Follow-up ramped/lower-of restore tracked in #2982. - §4 Clean-room & attribution: Commits
a5e04e4e25ad,72f15f423932, and004231bfab03all have human author/committer (Dylan Ironside) andSigned-off-by:with zero AI trailers. Fails on PR body due to the trailingAssisted-by:line. - §5 Evidence & CI:
- CI:
host-testsSUCCESS on004231bfab03(run45201300632, completed 2026-11-23T15:13:07Z). - PR body lacks
What:,Tested:,Risk:,Not yet:lines. - 8-boot J604 table in PR body predates
004231bfab03(4d45cc5a) and lacks regression counts; J604Speaker Protectionlease s2idle run and non-J604 amp-family row are still pending.
- CI:
- §6 Naming: Pass.
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
004231b to
d5d2cff
Compare
|
Pushed 004231b -> d5d2cff. Going through the review:
The lower-of / ramped set-back idea from point 5 of the earlier review is tracked in #2982 and stays out of this PR. |
satchlj
left a comment
There was a problem hiding this comment.
Gemini 4 Neon on @satchlj's behalf
Verdict at d5d2cffebae2
APPROVE — Clean fix for the suspend-prepare volume-lock race (macaudio_vlimit_update() saves speaker volumes on any path taking the system-sleep lock, reports lost-lock -ETIMEDOUT if saving failed, snapshots pm_quiesced once with READ_ONCE(), and acquires controls_rwsem before volume_lock_mutex in macaudio_vlimit_timeout_work()). Rebase onto main cleanly preserves macaudio_spk_lease_only() first; all active CI checks are green.
Blockers
- None on code, locking, KUnit tests, or CI. (See
Untested scoperegarding exact-head J604 / second-Mac hardware boot rows noted in the PR'sNot yet:section.)
Checked
- Full diff audit (3 changed files,
+161/-6):sound/soc/apple/macaudio.c:macaudio_vlimit_update()preserves themacaudio_spk_lease_only(ma->cfg)early return before readingquiesced = READ_ONCE(ma->pm_quiesced)once and reusing it consistently across lock reason,speaker_volume_was_locked, andmacaudio_pm_volumes_save()/ma->pm_lock_taken.- On unlock with
ma->pm_lock_taken && !ma->pm_volumes, logs warning and setsma->speaker_volume_was_locked = trueso the daemon re-applies speaker volumes on its next ping;macaudio_pm_volumes_restore()always callsmacaudio_pm_volumes_drop()(clearingma->pm_lock_taken = false). macaudio_vlimit_timeout_work()acquiresdown_read(&ma->card.snd_card->controls_rwsem)beforemutex_lock(&ma->volume_lock_mutex)(matchingmacaudio_vlimit_update_work()andmacaudio_vlimit_pm()lock ordering).macaudio_slk_put()usesREAD_ONCE(ma->pm_quiesced).
sound/soc/apple/macaudio-pm-volumes-test.h&sound/soc/apple/macaudio-iface-test.h: adds 3 focused KUnit tests (macaudio_pm_volumes_lock_taken_before_pm,macaudio_pm_volumes_save_fails,macaudio_iface_lease_after_sleep).
- CI at
d5d2cffebae2:host-testsPASS,readiness / readinessPASS,pr-text / pr-textPASS (all active checks green). - Untested scope: Static diff and CI review only; no kernel build or live Mac s2idle boot was run in this userspace-only review (as noted in the PR's
Not yet:section, exact-headd5d2cffebae2J604 boot, speaker-lease suspend test, and a second Mac model row remain outside this review).
Evidence
- PR head:
d5d2cffebae2(base=main,MERGEABLE, 3 files+161/-6) - CI:
host-tests: SUCCESS,readiness / readiness: SUCCESS,pr-text / pr-text: SUCCESS
|
Reproducing the "stuck at 1 of 201" state on a J313 (MacBook Air M1, 2020) with Machine
SymptomAfter a lid-close s2idle cycle, the internal speakers are silent for the rest of
But the hardware amp volume is pinned at the floor:
Kernel logThree s2idle cycles this boot, identical each time: No Restart fixes it→ both amp volumes read Caveat on the trigger: I did not capture whether a stream was running at Two questions
|
|
Thanks @IronsideXXVI. main's 11-28 speaker rework (the legacy volume lock is now opt-in via |
|
@RyanTheTide Thanks for doing the merge — I fast-forwarded Verified
I also updated the PR body to reflect @atmosuwiryo Thanks for the J313 report! Answering both questions from
|
…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>
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>
4bfd12e to
a0ad259
Compare
|
Rebased the three commits cleanly onto Verified
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
Re-review at a0ad259c9d82: the code is right, and the points from my earlier review are fixed.
- The conflict is resolved the right way: the lease-only return comes first, then the single
READ_ONCE(ma->pm_quiesced)snapshot, then main'sspk_legacy_selectedcheck. - The volumes are now kept wherever the sleep lock is taken (
macaudio_vlimit_update(), only on the unlocked-to-locked transition withquiesced), and an unlock that finds nothing kept reports a lost lock once so the daemon sets them again.pm_lock_takenis cleared inmacaudio_pm_volumes_drop(), so the lease switch, rebind and restore paths all reset it. - The timeout work now takes
controls_rwsemfor reading beforevolume_lock_mutex, the same order as the update work,macaudio_vlimit_pm()and control writes. Nothing cancels it synchronously while holdingcontrols_rwsem(macaudio_card_remove()already does the same forlock_update_work). - The PR text is fixed and has no attribution line.
Still needed before merge (hardware, not code): the J604 row at this head, since the 8-boot table ran on 004231bf before the rebase onto the spk_legacy_selected rework, and one row on a second Mac that uses macaudio (for example the J313/TAS2770 from this thread). Each row: music playing, s2idle, then all amp volumes read back, plus 0 failed units and no new kernel errors.
|
Hardware verification PASS on J414s (MacBook Pro 14" M2 Pro, T6020, 27.2 firmware
|
|
Checked before merging. The hosted Apple build and KUnit jobs were skipped at this head, and main has moved 459 commits past the PR's base (
Hardware: the J414s exact-head row above (all 6 amps 201/201 before and after s2idle) and the author's J604 row. Approved at this head. Merging. |
|
A row from a second Mac model, on main with this merged: MacBook Pro 14" M1 Pro (J314s), main 6162827 plus one local commit that only gives the GPU driver the name my installed Mesa looks for.
Setup, the same for both: the installed userspace does not select the volume lock on main, so the test wrote "Speaker Volume Unlock" to "Speaker Protection Interface" and started speakersafetyd, which then held the lock. Before each sleep a tone pattern was played to the speakers with the sink at volume 0; each sleep was 25 s of s2idle ended by an RTC alarm; the six Not covered: deep sleep (main has none for this model), the speaker lease path, and whether the race window was actually hit in those 20 sleeps: only the outcome was counted. On plain main it took 8 sleeps to see it once. |
What: Keep the speaker volumes in macaudio_vlimit_update() when it takes the system-sleep lock, not only in macaudio_vlimit_pm(). Until now, a speaker stream still running at PM_SUSPEND_PREPARE could take the lock first, so nothing was kept and the six amps stayed at 1 of 201 after resume. If the unlock still finds nothing kept, the lock is reported as lost so speakersafetyd sets the volumes again. A third commit reads pm_quiesced once per update. Follow-up to 911d47c (#820). Rebased cleanly onto main (19ee1b5, including #2893 and the opt-in spk_legacy_selected rework).
Tested: On a0ad259 (this head, rebased onto main 19ee1b5): KUnit
apple-speaker-*on arm64 QEMU 83/83 pass (includingmacaudio_pm_volumes_lock_taken_before_pm,macaudio_pm_volumes_save_fails,macaudio_iface_lease_after_sleepwith explicit legacy selection, and the #2893 cases);LLVM=1 W=1build ofsound/soc/apple/with 0 warnings;checkpatch --strictclean on all three commits (0 errors, 0 warnings, 0 checks). J604 (M4 MacBook Pro 14"), on the earlier head 004231b: 8 fresh boots each way, music playing, one s2idle cycle, then all six amp volumes read back. Without the patch, 4 of 8 boots had volumes stuck at 1 of 201. With the patch, 0 of 8 did, and all six read 201.Risk: two-way door · Macs whose speakers go through macaudio with the system-sleep volume lock (M1–M4). Only the J604 (M4) was run with the patch; the unpatched bug was also reproduced on a J313 (M1 Air, TAS2770) in this thread.
Not yet: No second Mac model has run this. The J604 boots were on 004231b, before the rebase onto 19ee1b5 (and before the opt-in
spk_legacy_selected/ #2893 reworks); a0ad259 has not been booted on hardware yet, and the speaker-lease path from main has not been run through a sleep. The "new case fails on the base" check was not run again: the test needs the fix's pm_lock_taken field and does not build without it. In 1 of the 8 patched boots a deep sleep attempt fell back to s2idle. The volumes still came back, and the patch adds no path that can veto a sleep.Full description
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 reads the speaker volume 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.
The third commit reads pm_quiesced once per macaudio_vlimit_update() with READ_ONCE(). macaudio_pm_notify() changes the flag before it takes volume_lock_mutex, and the function read it three times (lock reason, lost-lease flag, keeping), so a change between the reads could lock the volumes for suspend without keeping them.
Rebase onto main 19ee1b5 (a0ad259): preserves
macaudio_spk_lease_only()first, then theREAD_ONCE(ma->pm_quiesced)snapshot, then main'sspk_legacy_selectedcheck and #2893'sma->spk_volsiteration.macaudio_iface_lease_after_sleepselects the legacy volume lock first viamacaudio_test_select_legacy(), matching main's other interface cases, and both new pm-volumes cases sit alongside #2893'smacaudio_pm_volumes_taken_again.Tested on my J604 (M4 MacBook Pro 14") at 004231b. 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.
At a0ad259: KUnit
apple-speaker-*on arm64 QEMU 83/83 pass (apple-speaker-protection,apple-speaker-hpf,apple-speaker-pm-volumes,apple-speaker-volume-unlock,apple-speaker-interface,apple-speaker-amp-level,apple-speaker-admission),LLVM=1 W=1build ofsound/soc/apple/with 0 warnings,checkpatch --strictclean on all three commits. At 004231b the apple-speaker suites passed 59 of 59 and the new bug case failed on the base without the fix.