Skip to content

ASoC: apple: macaudio: Keep the speaker volumes wherever the sleep lock is taken - #2880

Open
IronsideXXVI wants to merge 3 commits into
aurora-silicon:integration/aurorafrom
IronsideXXVI:j604-macaudio-vlimit-keep
Open

IronsideXXVI wants to merge 3 commits into
aurora-silicon:integration/aurorafrom
IronsideXXVI:j604-macaudio-vlimit-keep

Conversation

@IronsideXXVI

@IronsideXXVI IronsideXXVI commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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:

kernel boots volumes stuck at 1 of 201 after resume
without this patch 8 4
with this patch 8 0

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)

@RyanTheTide

Copy link
Copy Markdown
Contributor

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:

  1. macaudio.c:834/837: pm_lock_taken is only cleared at the unlock and in macaudio_slk_lock() (3943). macaudio_spk_enter_lease() (1770), macaudio_spk_leave_lease() (1844) and macaudio_spk_fini() (3604) drop the kept volumes but leave the flag set. So: sleep on the Unlock interface, resume, switch to the lease, switch back. leave_lease already reports a lost lock; the daemon's next ping clears that and unlocks, then 837 sees the flag with no volumes and reports a second lost lock (plus the "No speaker volumes kept" warning). It fails safe, but it is wrong. Clearing the flag in macaudio_pm_volumes_drop() covers every one of these paths (save calls drop before 834 sets the flag, restore drops after 837 checks it), and the extra line in slk_lock goes away.

  2. macaudio-pm-volumes-test.h: none of the cases covers the bug being fixed. Please add one where the lock is taken with pm_quiesced already set before macaudio_vlimit_pm() runs (set pm_quiesced, call macaudio_vlimit_disable_timeout() or the timeout work under controls_rwsem, then PM_SUSPEND_PREPARE), and check that the volumes come back at the first ping after PM_POST_SUSPEND. A second case for the fallback (save fails, the ping after resume returns -ETIMEDOUT) would pin down 837-846.

  3. macaudio.c:951: taking controls_rwsem for reading before volume_lock_mutex in the timeout work matches macaudio_vlimit_update_work() and the control write paths, and the only synchronous cancels (3602, 4635) run without the rwsem held, so I see no new deadlock. Worth saying in the commit message that the lockdep_assert_held() in macaudio_pm_volumes_save() is now reachable from every caller of macaudio_vlimit_update().

  4. Nit, 716/753: with the existing "Speaker volumes locked/unlocked" lines that makes four info lines per sleep. dev_dbg for the two success lines, keeping the dev_warn, would be quieter.

  5. Older behaviour from 911d47c, but this PR makes it run on every sleep with a stream open, so it's worth fixing here or right after: the set-back goes in one step from the lock ceiling to the kept pre-sleep value, with no ramp, and it overwrites a lower volume the daemon wrote while still locked after resume (that write is clipped to the ceiling, then replaced). Setting back to the lower of the kept and current values, through the same ramped path the floor uses, would keep the "start quiet and ramp" behaviour across resume.

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>
@IronsideXXVI
IronsideXXVI force-pushed the j604-macaudio-vlimit-keep branch from 4d45cc5 to 72f15f4 Compare November 22, 2026 23:31
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Thanks Ryan. Pushed 4d45cc5 -> 72f15f4. It is two commits now: the fix (a5e04e4), then the tests (72f15f4).

  1. Done. macaudio_pm_volumes_drop() clears pm_lock_taken, and the extra line in macaudio_slk_lock() is gone. The restore now always ends in the drop, including its early return when nothing was kept. macaudio_iface_lease_after_sleep runs your sequence: sleep on the Unlock interface, resume, switch to the lease, switch back. The pings then give -ETIMEDOUT (from leave_lease), 0, 0. At 4d45cc5 with the same test, pm_lock_taken is still set after the lease switch and the third ping returns -ETIMEDOUT, with the "No speaker volumes kept" warning.
  2. Added macaudio_pm_volumes_lock_taken_before_pm. A stream runs, pm_quiesced is set, and the stop goes through macaudio_vlimit_update_work() (macaudio_vlimit_disable_timeout() under controls_rwsem). After that come PM_SUSPEND_PREPARE and PM_POST_SUSPEND, and the volumes are back at the first ping. The same case fails on e0a9448: nothing is kept and the volumes stay at 1. Also added macaudio_pm_volumes_save_fails: one control read fails during the keep. A small correction on the order: the ping that unlocks returns 0 and leaves the volumes at the ceiling, the ping after it returns -ETIMEDOUT, and the next one returns 0. That's because the unlock happens inside the first ping.
  3. Added to the fix commit's message, with the list of callers. Each one holds controls_rwsem and volume_lock_mutex there.
  4. Done. Both success lines are dev_dbg now, and the dev_warn stays.
  5. I'd rather do this right after than in this PR. While locked, every speaker volume is clipped to the ceiling (1 on SN012776/TAS5770), so min(kept, current) is always the current value and would never set anything back. A real rule has to track a write the daemon made while locked, and a write of exactly the ceiling stays ambiguous. There's also no ramp up today: macaudio_spk_ramp_down() only goes down, needs spk_mutex and D0, and covers SN012776/TAS5770 only, while the set-back runs inside the ping. That needs its own device check, so I'll send it as a follow-up.

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 DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. CI not green: host-tests for 72f15f423932c9d95456f4d55dac278ffbdebb4b is still QUEUED (mergeStateStatus: UNSTABLE); cannot approve until green.
  2. §5 Hardware evidence stale / incomplete for current head 72f15f42 and touched paths:
    • The 8-boot J604 table in the PR body was run on the previous head (4d45cc5a), not on 72f15f42, 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 at 72f15f42 is still pending (noted as queued in your comment).
    • macaudio.c affects 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.
  3. §2 Data race / torn read of ma->pm_quiesced in macaudio_vlimit_update() (sound/soc/apple/macaudio.c:797,822,834):
    macaudio_pm_notify() updates ma->pm_quiesced using WRITE_ONCE() without holding volume_lock_mutex (lines 1615, 1633), and other readers use READ_ONCE(ma->pm_quiesced). macaudio_vlimit_update() reads ma->pm_quiesced three separate times without READ_ONCE() or a local snapshot. Snapshot bool quiesced = READ_ONCE(ma->pm_quiesced); once at the top of macaudio_vlimit_update() (and use READ_ONCE() in macaudio_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 in macaudio_vlimit_update().
  • §3 Safety: Pass (fail-closed ceiling preserved; failed save falls back to -ETIMEDOUT lost-lock notification so the daemon re-applies safe model volumes; verify with the queued Speaker Protection lease / speakerguardd v2 run).
  • §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)

@RyanTheTide

Copy link
Copy Markdown
Contributor

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:

  1. pm_lock_taken stale after the lease paths: fixed. macaudio_pm_volumes_drop() now clears the flag, the extra clear in macaudio_slk_lock() is gone, and restore always ends in drop, including the nothing-kept return. The order still holds: save drops first and the flag is set after it, and restore checks the flag before it drops. macaudio_iface_lease_after_sleep covers the sequence I described.
  2. No test for the bug: fixed. macaudio_pm_volumes_lock_taken_before_pm takes the lock through lock_update_work with pm_quiesced set, and it fails on the base. macaudio_pm_volumes_save_fails pins down the fallback. Thanks for correcting the order of the pings.
  3. The lock-order note is in the commit message, with the list of callers.
  4. The success lines are now dev_dbg. Fixed.
  5. Ramped, lower-of set-back: deferred to a follow-up, and the reason is good. While locked, the current value is always the ceiling, so min() would never set anything back. A real rule needs to track the daemon's writes made while locked. Please open an issue for it so it is not lost.

No new issues in the delta. Heads-up for whoever merges: this PR and linux#2893 both touch macaudio-pm-volumes-test.h. git merge-tree of the two heads conflicts in that test header only (macaudio.c merges cleanly), so the second one to land needs a small rebase. There are no gates and no new parameters. The commits carry only the author's Signed-off-by.

Verdict: APPROVE on code.

Hardware still owed at this head, all s2idle with a stream playing plus speaker volume readback before and after resume:

  • IronsideXXVI (queued): the J604 Speaker Protection lease run, showing nothing is kept or set.
  • Lab (speaker rows are permitted under v2 gating): one other SN012776 laptop (J713 or J615).
  • An owner with a TAS5770 machine (J293/J313); the lab cannot cover this.

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>
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Thanks both. Pushed 72f15f4 -> 004231b. The fix and the test commits are unchanged; 004231b is a third commit on top of them.

  1. pm_quiesced race: fixed in 004231b. macaudio_vlimit_update() now reads it once with READ_ONCE() into a local after the lease check, and all three checks use that local. macaudio_slk_put() uses READ_ONCE() too. No plain reads are left. KUnit apple-speaker* passes 59 of 59 at 004231b, W=1 on macaudio.o gives no warnings, and checkpatch --strict is clean.
  2. CI: host-tests passed at 72f15f4. It runs again for 004231b.
  3. Hardware at the head: the J604 Speaker Protection lease run (s2idle with a stream playing, volume readback before and after resume) is queued. It will run on a kernel built at this head, and I'll post it with the regression counts from the same boots: failed units and new kernel errors. SELinux is not enabled on this Arch install, so I'll show that instead of a denial count.
  4. The J604 is the only Mac I have, so I can't add the other SN012776 row or the TAS5770 row myself. Those need the lab and an owner, as Ryan listed.
  5. Point 5 follow-up: macaudio: lower-of, ramped set-back of the speaker volumes kept across the sleep lock #2982.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants