Skip to content

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

Merged
DjDeveloperr merged 3 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604-macaudio-vlimit-keep
Dec 2, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-silicon:mainfrom
IronsideXXVI:j604-macaudio-vlimit-keep

Conversation

@IronsideXXVI

@IronsideXXVI IronsideXXVI commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 (including macaudio_pm_volumes_lock_taken_before_pm, macaudio_pm_volumes_save_fails, macaudio_iface_lease_after_sleep with explicit legacy selection, and the #2893 cases); LLVM=1 W=1 build of sound/soc/apple/ with 0 warnings; checkpatch --strict clean 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 the READ_ONCE(ma->pm_quiesced) snapshot, then main's spk_legacy_selected check and #2893's ma->spk_vols iteration. macaudio_iface_lease_after_sleep selects the legacy volume lock first via macaudio_test_select_legacy(), matching main's other interface cases, and both new pm-volumes cases sit alongside #2893's macaudio_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:

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.

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=1 build of sound/soc/apple/ with 0 warnings, checkpatch --strict clean 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.

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

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

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

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

Verdict: HOLD at 004231bfab03

Blockers:

  • sound/soc/apple/macaudio.c:779-784 (merge conflict): Branch is CONFLICTING (DIRTY) against main in macaudio_vlimit_update() where 004231bfab03 inserts quiesced = READ_ONCE(ma->pm_quiesced); at the same site main (3fa618e415f6, merged in 065764c614c8 / #1862) added if (macaudio_spk_lease_only(ma->cfg)) return;. Rebase onto main keeping macaudio_spk_lease_only() before the READ_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, since macaudio.c has a conflict) covering both the Speaker Volume Unlock s2idle stream-playing test and the queued Speaker 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 sharing macaudio.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 commit 004231bfab03 (ASoC: apple: macaudio: Read pm_quiesced once per volume-lock update) touching only sound/soc/apple/macaudio.c. Snapshots quiesced = READ_ONCE(ma->pm_quiesced) once in macaudio_vlimit_update() and reuses it across all three branches (lock reason, speaker_volume_was_locked, and macaudio_pm_volumes_save() / pm_lock_taken), and switches macaudio_slk_put() to READ_ONCE(ma->pm_quiesced). No plain reads of pm_quiesced remain in macaudio.c.
  • Conflict check against main (a9cd131e8207):
    • sound/soc/apple/macaudio.c: CONFLICT at lines 779–784 (macaudio_vlimit_update() vs 3fa618e415f6).
    • 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_quiesced torn-read blocker fixed). Blocked only by main merge conflict.
  • §3 Safety: Pass in code. macaudio_pm_volumes_save() runs under controls_rwsem (read) + volume_lock_mutex before clipping; save failure leaves volumes at ceiling and forces -ETIMEDOUT lost-lock recovery; macaudio_pm_volumes_drop() clears pm_lock_taken on lease transitions, new owner, and teardown; macaudio_spk_leased() short-circuits for speakerguardd v2; synchronous cancels of lock_timeout_work run without controls_rwsem or volume_lock_mutex. Preserve macaudio_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, and 004231bfab03 all have human author/committer (Dylan Ironside) and Signed-off-by: with zero AI trailers. Fails on PR body due to the trailing Assisted-by: line.
  • §5 Evidence & CI:
    • CI: host-tests SUCCESS on 004231bfab03 (run 45201300632, 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; J604 Speaker Protection lease s2idle run and non-J604 amp-family row are still pending.
  • §6 Naming: Pass.

@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) merge-conflict Conflicts with the base branch (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) and removed merge-conflict Conflicts with the base branch (bot) needs-hw-row No hardware row at the PR head (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at a0ad259c: ready to merge

Check Detail
CI at head ok 4 checks passed
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at a0ad259c
Conflicts ok none
PR text ok Risk: two-way · 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.

Areas: area:audio
Merge: gh pr merge 2880 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@IronsideXXVI
IronsideXXVI force-pushed the j604-macaudio-vlimit-keep branch from 004231b to d5d2cff Compare November 24, 2026 23:42
@IronsideXXVI
IronsideXXVI requested review from a team as code owners November 24, 2026 23:42
@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Nov 24, 2026
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Pushed 004231b -> d5d2cff. Going through the review:

  1. Conflict with main: rebased onto f91bc94. The macaudio_spk_lease_only() early return now sits before the READ_ONCE(ma->pm_quiesced) snapshot in macaudio_vlimit_update(), so a lease-only config never reads it.
  2. The trailer line you flagged: removed from the description.
  3. Description: now What / Tested / Risk / Not yet with the logs in <details>.
  4. J604 run at d5d2cff: not done yet. The kernel on the J604 right now does not carry these three commits, so I am building one that does and will post the Speaker Volume Unlock s2idle stream test, the Speaker Protection lease through s2idle and the regression counts (failed units, new kernel errors, SELinux N/A on Arch) from that exact head.
  5. Second Mac (SN012776 / TAS5770): I do not have one. If someone with one can run the same s2idle stream test that would close it; otherwise it stays under Not yet.

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
satchlj previously approved these changes Nov 25, 2026

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 scope regarding exact-head J604 / second-Mac hardware boot rows noted in the PR's Not yet: section.)

Checked

  • Full diff audit (3 changed files, +161/-6):
    • sound/soc/apple/macaudio.c:
      • macaudio_vlimit_update() preserves the macaudio_spk_lease_only(ma->cfg) early return before reading quiesced = READ_ONCE(ma->pm_quiesced) once and reusing it consistently across lock reason, speaker_volume_was_locked, and macaudio_pm_volumes_save()/ma->pm_lock_taken.
      • On unlock with ma->pm_lock_taken && !ma->pm_volumes, logs warning and sets ma->speaker_volume_was_locked = true so the daemon re-applies speaker volumes on its next ping; macaudio_pm_volumes_restore() always calls macaudio_pm_volumes_drop() (clearing ma->pm_lock_taken = false).
      • macaudio_vlimit_timeout_work() acquires down_read(&ma->card.snd_card->controls_rwsem) before mutex_lock(&ma->volume_lock_mutex) (matching macaudio_vlimit_update_work() and macaudio_vlimit_pm() lock ordering).
      • macaudio_slk_put() uses READ_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-tests PASS, readiness / readiness PASS, pr-text / pr-text PASS (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-head d5d2cffebae2 J604 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

@atmosuwiryo

Copy link
Copy Markdown

Reproducing the "stuck at 1 of 201" state on a J313 (MacBook Air M1, 2020) with
TAS2770 amps — a machine and amp family not in your test matrix (J604 /
SN012776 alone so far).

Machine

  • MacBook Air (M1, 2020), apple,j313 / apple,t8103
  • snd_soc_macaudio, card 1 MacBook Air J313
  • Amps: snd_soc_tas2770 at i2c 1-0031 and 2-0034; codec cs42l83
  • speakersafetyd 2.0.1-2, PipeWire 1.6.11, WirePlumber 0.5.19
  • linux-aurora 7.3.0.aurora1-1 (built 2026-11-23)

Symptom

After a lid-close s2idle cycle, the internal speakers are silent for the rest of
the boot. Nothing above the amp is at fault:

  • PipeWire sink RUNNING, Mute: no, volume 80%, card profile HiFi
  • Speaker Playback Switch = on — not an ALSA mute
  • A 440 Hz tone played to the default sink reaches the speaker DSP node's monitor
    at 45.0 % FS peak (recorded with parec on audio_effect.j313-convolver.monitor)

But the hardware amp volume is pinned at the floor:

numid=3,iface=MIXER,name='Left Speaker Playback Volume'
  ; type=INTEGER,access=rw--lR--,values=1,min=0,max=201,step=0
  : values=1
  | dBscale-min=-100.50dB,step=0.50dB
numid=5,name='Right Speaker Playback Volume'  : values=1

amixer renders that as Playback 1 [0%] [-100.00dB]. Writing them from
userspace is refused: element write error: Operation not permitted.

Kernel log

Three s2idle cycles this boot, identical each time:

[  293.874928] snd-soc-macaudio sound: Speaker volumes locked: System suspend
[  295.933965] snd-soc-macaudio sound: Speaker volumes unlocked
[14703.321271] snd-soc-macaudio sound: Speaker volumes locked: System suspend
[14706.344918] snd-soc-macaudio sound: Speaker volumes unlocked
[21408.764958] snd-soc-macaudio sound: Speaker volumes locked: System suspend
[21411.829078] snd-soc-macaudio sound: Speaker volumes unlocked      <-- amp stays at 1

No Kept N speaker volumes / Set N speaker volumes again lines appear anywhere
in this boot. I have not verified whether 911d47c3d1a5 is in Omarchy's
7.3.0.aurora1 build, so I can't yet say whether this is your macaudio_vlimit_update()
path or the older macaudio_vlimit_pm()-only state. The log-line absence is
consistent with the latter but is not proof.

Restart fixes it

systemctl restart speakersafetyd.service

→ both amp volumes read 201 (0 dB), stayed there across 6 s of continuous tone
(read back each second), no new warnings. Same "restart needed: yes" signature as
your before-column.

Caveat on the trigger: I did not capture whether a stream was running at
PM_SUSPEND_PREPARE on this machine, so I can't yet say this is your macaudio_vlimit_update()
path rather than the pre-#820 state.

Two questions

  1. Is the TAS2770 path (macaudio_spk_close_gate() on J313's 2-amp config) known to
    reach macaudio_vlimit_update() at suspend with a stream running, or is that
    SN012776-specific? If it does, this is more evidence for the ASoC: apple: macaudio: Keep the speaker volumes wherever the sleep lock is taken #2880 fix.
  2. Would a J313 hardware row be useful? I can run the s2idle-with-stream / s2idle-idle
    pair and report amp readback, but I'd want to know which head to build first.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks @IronsideXXVI. main's 11-28 speaker rework (the legacy volume lock is now opt-in via spk_legacy_selected) conflicted again in macaudio_vlimit_update(). I merged main keeping both: your quiesced snapshot after the lease-only return, then main's selection check; your new lease-after-sleep KUnit case now selects the volume lock first, like main's other interface cases. No protection behaviour changed.
I could not push to your branch from here, so the merge (4bfd12e, on top of your unchanged d5d2cff) is carried in #3597. If you would rather keep it here, fast-forward your branch to that commit and we close #3597.
Tests: macaudio KUnit apple-speaker-* on arm64 QEMU, 77/77 passed on the merged tree.
Still needed before merge: Dj's review of the merged result, and silent rows (J615 with the legacy lock selected + speakersafetyd, music, s2idle, amp volumes read back after resume over several boots). A boot of the merged head on your J604 would cover a second model.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 30, 2026
@IronsideXXVI
IronsideXXVI requested a review from a team as a code owner December 1, 2026 08:29
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

@RyanTheTide Thanks for doing the merge — I fast-forwarded j604-macaudio-vlimit-keep to 4bfd12e65568 (no force push), so we can keep the thread here and close #3597.

Verified 4bfd12e65568 locally:

  • arm64 QEMU KUnit apple-speaker-*: 77/77 pass
  • make ARCH=arm64 LLVM=1 W=1 sound/soc/apple/: 0 warnings
  • checkpatch --strict on the diff against d2532e903: 0 errors, 0 warnings, 0 checks

I also updated the PR body to reflect 4bfd12e65568. An exact-head J604 boot with spk_legacy_selected + speakersafetyd is queued next.

@atmosuwiryo Thanks for the J313 report! Answering both questions from sound/soc/apple/macaudio.c:

  1. Yes — the macaudio_pm_notify() -> macaudio_spk_close_gate() -> macaudio_spk_stop_work() -> macaudio_be_trigger() -> lock_update_work -> macaudio_vlimit_update() path is amp-agnostic (macaudio_be_trigger() has no amp-family branch, and macaudio_j313_cfg uses AMP_TAS5770 with "* Speaker Playback Volume", which matches J313's Left/Right Speaker Playback Volume controls). So J313 hits the exact same macaudio_vlimit_update() race when a stream is active at PM_SUSPEND_PREPARE on the legacy speakersafetyd volume-lock path. (Also note this PR changes the Kept N / Set N messages to dev_dbg, and kernels before #820 didn't save/restore at all.)
  2. Yes, a J313 (TAS2770) hardware row at 4bfd12e65568 would be very helpful as a second Mac and amp family. Note that with main's 11-28 rework (ea865dc5e5ee), using speakersafetyd now requires selecting "Speaker Volume Unlock" on "Speaker Protection Interface" as root (CAP_SYS_ADMIN) before starting the daemon. Running s2idle both with a speaker stream playing and idle, with amp volume readback before/after resume plus failed units and new kernel error lines, would cover J313 cleanly.

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Dec 1, 2026
…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>
@IronsideXXVI
IronsideXXVI force-pushed the j604-macaudio-vlimit-keep branch from 4bfd12e to a0ad259 Compare December 1, 2026 10:03
@IronsideXXVI

Copy link
Copy Markdown
Contributor Author

Rebased the three commits cleanly onto main (19ee1b5bc, head a0ad259c9d82) after #2893 (6773a6112) merged on main and conflicted with 4bfd12e65568 (this also clears the stale pr-text / host-tests check runs inherited from #3597's SHA).

Verified a0ad259c9d82 locally:

@github-actions github-actions Bot added blast:m1 Macs affected, from Merge risk (bot) and removed ci-failing A check failed at the PR head (bot) merge-conflict Conflicts with the base branch (bot) labels Dec 1, 2026

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

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's spk_legacy_selected check.
  • The volumes are now kept wherever the sleep lock is taken (macaudio_vlimit_update(), only on the unlocked-to-locked transition with quiesced), and an unlock that finds nothing kept reports a lost lock once so the daemon sets them again. pm_lock_taken is cleared in macaudio_pm_volumes_drop(), so the lease switch, rebind and restore paths all reset it.
  • The timeout work now takes controls_rwsem for reading before volume_lock_mutex, the same order as the update work, macaudio_vlimit_pm() and control writes. Nothing cancels it synchronously while holding controls_rwsem (macaudio_card_remove() already does the same for lock_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.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 2, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Hardware verification PASS on J414s (MacBook Pro 14" M2 Pro, T6020, 27.2 firmware mBoot-20457.41.3) at exact head a0ad259c9d82:

  • Kernel: 7.3.0+ #4 with snd-soc-macaudio.ko built from a0ad259c9d82
  • Boot ID: daef456b-5549-4028-98bf-762f4662a677
  • Audio path: speakers at 5% (-78.07 dB) strictly through speakerguardd-v2 (sgv2.voicing.j414), quiet tone playing through speakers across s2idle (pm_test=platform, suspend 0.820 s, resume 0.121 s, Speaker Protection Sleeps: 1 -> 2).
  • Amp volumes before s2idle (playing): all 6 amps (Left/Right Tweeter, Left/Right Woofer 1, Left/Right Woofer 2) Speaker Volume = 201, Amp Gain Volume = 15, Amp Fault Status = 0,0,0.
  • Amp volumes after s2idle resume (playing): all 6 amps Speaker Volume = 201 (daemon-set values preserved, not floored to 1/201), Amp Gain Volume = 15, Amp Fault Status = 0,0,0.
  • Amp volumes after playback stop: all 6 amps Speaker Volume = 0, Amp Fault Status = 0,0,0.
  • System health: 0 failed systemd units, 0 new kernel errors (Speaker protection withdrawn: system sleep -> Speaker protection granted (1 speaker starts refused before)).

@DjDeveloperr

Copy link
Copy Markdown
Contributor

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 (19ee1b5bc9d5). It has also changed macaudio.c (J873 speaker enable, has_sense from dma-names) and macaudio-admission-test.h. So these checks ran on the merge with current main, not on the head alone.

  • Merge of a0ad259c into main 5a870d0f: no conflicts, tree 3c9dc7bca28f. Main's changes are in macaudio_parse_of() and the J873 config, away from the vlimit/pm_quiesced/lease-only paths changed here. macaudio_vlimit_update() and macaudio_vlimit_pm() are the same in the merge as at the head.
  • Hosted Apple build and KUnit on that merge tree (temporary ci/ ref, now deleted), suites as the PR plan picks them (macaudio): https://github.com/aurora-silicon/linux/actions/runs/46850232253
    • apple-build: pass
    • KUnit macaudio (arm64 QEMU, apple-speaker-*): 7 suites, 83/83 pass, including macaudio_iface_lease_after_sleep, macaudio_pm_volumes_lock_taken_before_pm, macaudio_pm_volumes_save_fails and the admission suite with main's J873 change.
  • W=1, arm64, Mac config (defconfig + asahi.config, LLVM=1): make W=1 sound/soc/apple/ on main, then on the merge. 0 warnings both times (mca.o, macaudio.o).
  • Same suites under UML on the merge tree (COMPILE_TEST instead of ARCH_APPLE): 83/83 pass. With the test headers compiled in, macaudio.o at W=1 also has 0 warnings.
  • Host tests: only tools/asahi/hardware-defaults-tests.py reads sound/soc/apple/, and only its Kconfig, which this PR doesn't touch. It passes 6/6 on the merge tree. Main's host-tests at 5a870d0f pass.

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.

@DjDeveloperr
DjDeveloperr merged commit 6162827 into aurora-silicon:main Dec 2, 2026
5 of 7 checks passed
@Ante042

Ante042 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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.

Kernel Sleeps Wakes with all six speaker volumes at 201
main 2225082 (before this) 8 (stopped at the first failure) 7
main 6162827 (with this) 20 20

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 Speaker Volume controls were read 6 s after each wake. With this in: 0 WARNING, BUG or Oops lines, no amp fault lines, no failed units at the end.

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.

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

Labels

area:audio Area from CODEOWNERS (bot) blast:m1 Macs affected, from Merge risk (bot) blast:m4 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants