Skip to content

ASoC: apple: macaudio: volume lock only after an explicit root selection - #3479

Merged
RyanTheTide merged 2 commits into
mainfrom
fix/speaker-interlock-lease-only
Nov 28, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
fix/speaker-interlock-lease-only

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem: On every Mac, "Speaker Volume Unlock" is the speaker interface at every bind. Any process that took ELEM_LOCK on that control and on every speaker volume, then pinged it with SPEAKER_MAGIC_VALUE, could lift the speakers to the amps' 0 dB. That includes speakersafetyd or speakerguardd 0.1 started by a unit or preset without anyone choosing it. SGV2 (speakerguardd v2, the "Speaker Protection" lease) should be the only thing that takes the interlock by default. A user's explicit one-command opt-out to speakersafetyd still has to work.

Change: The volume lock stays exactly as on main, behind one gate. A daemon may take it only after a CAP_SYS_ADMIN process has written "Speaker Volume Unlock" to "Speaker Protection Interface" during this card bind. The write counts even though that is already the interface from the bind. Until then:

  • ELEM_LOCK on "Speaker Volume Unlock" fails with -EPERM, and so does the magic ping. A one-time log line says "Speaker Volume Unlock refused: not selected on Speaker Protection Interface".
  • macaudio_vlimit_update() never unlocks.

The selection:

  • starts cleared at every bind;
  • is not set by the kernel's own return to the volume lock (the 30 s revert when the lease has no owner);
  • is cleared by a write of "Speaker Protection", which also relocks anything a daemon had lifted, even if the lease itself is refused.

While leased the ping still gets -EBUSY. The lease path (v2) is unchanged. The net diff against main is +191/-2 in sound/soc/apple/. The first commit removed the legacy path outright; the second puts it back and adds the gate.

For the opt-out tool: before starting speakersafetyd, it must write the interface control as root, e.g. amixer -c <card> cset name='Speaker Protection Interface' 'Speaker Volume Unlock' in the unit's ExecStartPre. Without that write, speakersafetyd gets -EPERM at ELEM_LOCK and exits. speakerguardd v2 writes "Speaker Protection" at boot as before, which also clears any earlier selection.

Testing:

  • KUnit at ea865dc5e5ee (kunit.py run --arch=arm64 LLVM=1, qemu, PROVE_LOCKING, filter apple-*): 78/78 passed. New cases:

    • lock and ping refused without the selection (root included);
    • an owner that loses the selection is relocked and refused;
    • the kernel's unowned revert selects nothing;
    • an unprivileged write selects nothing;
    • a "Speaker Protection" write deselects and relocks, even when the lease is refused during sleep.

    The main volume-lock suites (ping deadline, every-volume rule, pm-volume restore, -ETIMEDOUT after a revert) pass unchanged once selected. The lease suites (protection 34, hpf 11, amp-level 5) are unchanged.

  • sound/soc/apple/ builds with W=1 and no warnings in the fleet release config (fedora 7.0.13 + asahi.config, clang 22, rustc 1.97.1).

  • checkpatch.pl on the net diff against main: 0 errors, 0 warnings. Run on the second commit alone, it flags only main's own lines that the commit restores.

Tested: J413 M2 Air (T8112) and J700 (A18 Pro) at ea865dc (fleet89 + this macaudio.c, runtime module swap, silent): default and non-root refused, root selection locks, deselect relocks, v2 lease granted, selfcheck PASS, 0 failed units. Rows in the PR comments. KUnit apple-* 78/78 in arm64 QEMU.

Risk: two-way door · every Mac with speakers (macaudio, no SoC or board gating). A Mac that runs speakersafetyd or speakerguardd 0.1 without the selection write keeps its speakers at the safe ceiling, and the daemon fails at ELEM_LOCK. That is intended, and the distro opt-out tool has to do the write. v2 behaviour is unchanged.

Not yet: the magic ping after the opt-out selection was not exercised on hardware (the row stops at ELEM_LOCK/ELEM_UNLOCK, KUnit covers the ping). Lab Macs never run speakersafetyd.

Details
  • New field spk_legacy_selected, written under spk_mutex and volume_lock_mutex by macaudio_slk_select() from macaudio_spk_iface_put() only, and cleared in macaudio_late_probe().
  • Gates: macaudio_slk_lock() (before taking ownership), macaudio_slk_put() (after the existing owner -EPERM and lease -EBUSY checks), and macaudio_vlimit_update() (no unlock without it).
  • Other paths checked, none lifts the speakers outside these two interfaces:
    • aop_t8140.rs: the full-scale register has no control.
    • The AOP Rust drivers have no gain controls.
    • tas2764/tas2770 "Amp Gain Volume" is capped at the boot-loader amp level.
    • SSM3515 speakers are never admitted.
    • please_blow_up_my_speakers does nothing.
  • KUnit raw log: one __queue_work WARN in macaudio_spk_refused_starts_are_counted. It is also on main and this PR doesn't touch it: the protection-test fixture never initialises lock_update_work, which macaudio_be_trigger() schedules.
Evidence

Logs on the lab store: traces/audio/20261128-sgv2-interlock-lease-only/ (kunit-apple-v2.log, kunit-apple-v2-raw.log, fleet-config-sound-soc-apple-W1-v2.log, checkpatch-net-vs-main.log, net-vs-main.diff, README).

[15:56:20] ======== apple-speaker-volume-unlock (11 subtests) =========
[15:56:22] =========== apple-speaker-interface (8 subtests) ===========
[15:56:23] Testing complete. Ran 78 tests: passed: 78

…eakers

The speakers had two owners. "Speaker Protection", the lease of a
protection engine in the signal path (speakerguardd v2), and "Speaker
Volume Unlock", the volume lock of the older daemons (speakersafetyd,
speakerguardd 0.1): a process that held ELEM_LOCK on that control and on
every speaker volume, and pinged it with SPEAKER_MAGIC_VALUE, lifted the
volumes to the amps' 0 dB on any machine whose interface was not switched
to the lease, which is every machine at every bind.

Make the lease the only way up, on every machine:

 - ELEM_LOCK on "Speaker Volume Unlock" fails with -EPERM, and so does
   the magic ping, with a one-time log line saying why, so that an older
   daemon fails at once instead of believing it guards the speakers.
   0 and 1 stay accepted no-ops so that a saved mixer state still
   restores, and the control reads 0.
 - macaudio_vlimit_unlock() lifts the ceiling only while leased, so
   nothing outside macaudio_spk_enter_lease() can lift it.
 - The volume lock's lease machinery is gone with its only user: the
   ping deadline and its works, the back-end trigger bookkeeping that
   drove them, and the speaker volumes the system-sleep lock kept for the
   daemon's next ping.

Unchanged: "Speaker Volume Unlock" is still the interface at every bind,
with the speaker volumes at its ceiling; a CAP_SYS_ADMIN write of
"Speaker Protection" to "Speaker Protection Interface" hands the speakers
to the lease through the floor, which grants as before; the 30 s unowned
revert and an administrator's revert put them back at the floor under
the ceiling; system sleep withdraws the lease as before.

The KUnit suites follow: apple-speaker-volume-unlock and
apple-speaker-pm-volumes now prove that the lock and the ping are
refused and lift nothing, across a system sleep too, and that the ceiling
cannot be lifted outside the lease; apple-speaker-interface keeps the
switch, grant, revert and unowned cases with the older daemon refused
throughout.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide requested review from a team as code owners November 28, 2026 04:44
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

@DjDeveloperr could you review this when you get a chance? It makes the Speaker Protection lease the only thing that can lift the speakers in macaudio.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at 8788ddba886e, once CI is green at this head.

Blockers: none. This only removes ways to lift the speakers.

Checked (tried to find any path left that lifts a speaker limit outside the lease):

  • The old owner is refused at both entry points: macaudio_slk_lock() returns -EPERM for ELEM_LOCK on "Speaker Volume Unlock", and macaudio_slk_put() returns -EPERM for SPEAKER_MAGIC_VALUE (with a one-time log line). 0 and 1 stay no-ops so a saved mixer state restores; anything else is -EINVAL.
  • macaudio_vlimit_unlock() now forces unlock = unlock && macaudio_spk_leased(ma) before choosing the ceiling for every amp type, so even a stray unlock=true call can't lift it without the lease.
  • Every remaining lifting call site: macaudio_vlimit_unlock(ma, true) only in lease entry, after the floor and amp gains are set, with the volumes left at the safe level; macaudio_spk_set_gain(ma, true) only in the lease grant (CAP_SYS_ADMIN, powered card, tweeter HPF first); the probe's snd_soc_limit_volume() caps each SN012776 at its ADT level and lifts nothing.
  • The removed machinery (ping deadline works, BE trigger bookkeeping, system-sleep volume save/restore) had the old lock as its only user; the lease's own sleep withdrawal and 30 s unowned revert are unchanged.
  • KUnit (arm64 QEMU, macaudio + OF_OVERLAY built in): 69/69, including apple-speaker-volume-unlock (4), apple-speaker-pm-volumes (2), apple-speaker-interface (7) and apple-speaker-protection (34).
  • No attribution.

Deployment effect, intended: a Mac still running speakersafetyd or speakerguardd 0.1 keeps its speakers at the floor (silent) under this kernel until speakerguardd v2 owns them, which is the safe direction and matches ORCH5's 15:26 order (legacy removed fleet-wide, SGV2-FLEET deploys).

The previous commit refused "Speaker Volume Unlock" outright. That also
takes away a user's deliberate choice of the older daemon
(speakersafetyd), which the distributions keep as a one-command opt-out.
Bring the volume lock back as it is on main and gate it instead.

A daemon may take the volume lock only once a CAP_SYS_ADMIN process has
written "Speaker Volume Unlock" to "Speaker Protection Interface" during
this card bind, also where that is the interface already, as it is from
every bind. Until then ELEM_LOCK on "Speaker Volume Unlock" and the
SPEAKER_MAGIC_VALUE ping fail with -EPERM, with a one-time log line
saying why, and macaudio_vlimit_update() never unlocks. The selection
starts cleared at every bind, the kernel's own return to the volume lock
(no owner of the lease for 30 s) does not set it, and a write of
"Speaker Protection" clears it at once and relocks whatever a daemon had
lifted, even where the lease itself is refused. While leased the ping
still fails with -EBUSY.

So a daemon started by a unit or preset alone gets -EPERM and fails
loudly, and only an explicit root selection (the opt-out tool, before it
starts the daemon) or the lease can lift the speakers. The lease path is
unchanged.

The volume lock's KUnit suites run selected as before, with new cases
for the refusal without a selection, the owner that loses the
selection, the kernel's revert that selects nothing, the unprivileged
write that selects nothing, and the write of "Speaker Protection" that
deselects.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide RyanTheTide changed the title ASoC: apple: macaudio: only the Speaker Protection lease lifts the speakers ASoC: apple: macaudio: volume lock only after an explicit root selection Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Decision on the open question (lane owner): both requirements hold. speakersafetyd can't take the interlock by default, and an explicit opt-out still works. The legacy volume lock is back as on main, but a daemon can lock or ping it only after a root write of 'Speaker Volume Unlock' to 'Speaker Protection Interface' during this boot. The kernel's own revert doesn't count, and a 'Speaker Protection' write clears it. The opt-out tool has to make that write before it starts speakersafetyd. Pushed as ea865dc (new commit, no force push); body updated.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: HOLD at ea865dc5e5ee for a policy decision, not a code fault. My approval at 8788ddba was for removing the volume lock outright; this head keeps it as an administrator opt-in, so that approval no longer describes what would land.

The decision needed (ORCH5 / SGV2-FLEET): the new commit brings back the "Speaker Volume Unlock" path so distributions can keep speakersafetyd as a one-command opt-out. ORCH5's 15:26 order (Ryan) was: speakersafetyd and the Asahi audio stack removed fleet-wide, SGV2 end to end, never restore legacy. Whether a kernel opt-in for the old daemon fits that order is theirs to decide. If it doesn't, the previous head (8788ddba) is the one to merge. If it does, the code below is safe and I'll approve it.

Checked (the gate, adversarially):

  • Default refused: spk_legacy_selected starts false at every bind (macaudio_late_probe); without it, ELEM_LOCK on "Speaker Volume Unlock" and the magic ping return -EPERM (one-time log line), and macaudio_vlimit_update() forces unlock = false. In that function unlock starts true and every later branch only sets it false, so nothing can undo the refusal.
  • Who can select: only macaudio_spk_iface_put(), which checks capable(CAP_SYS_ADMIN) first. The control is INACTIVE, so alsactl restore at boot can't select it from a saved state. The kernel's own 30 s return to the volume lock selects nothing.
  • Deselect: a write of "Speaker Protection" clears the selection first and relocks whatever a daemon had lifted, even if the lease itself is then refused; a selected daemon's next ping gets -EPERM. The lease path is unchanged, and while leased the ping still gets -EBUSY.
  • Locking: the flag is read and written under volume_lock_mutex, and selection changes happen under spk_mutex plus controls_rwsem.
  • Against main this is still a tightening: today any daemon can take the lock at every bind; with this, it needs an explicit root selection first.
  • KUnit (arm64 QEMU): 78/78, including apple-speaker-volume-unlock 11/11, apple-speaker-pm-volumes 3/3 and apple-speaker-interface 8/8.
  • No attribution.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Decision on the hold: keep the explicit root opt-in (this head, ea865dc). The rollout order has two parts: every user moves to speakerguardd v2 by default and the old daemon never takes the speakers on its own, and a user who chooses to can switch back with one command that installs and selects speakersafetyd. With this head the default is refused at every bind, and only a root write of the interface control (the opt-out tool's job, never done by default or on our lab Macs) lets the old daemon lock. 8788ddb would make that opt-out impossible. Please review ea865dc on that basis.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: MERGE at ea865dc5e5ee, once CI is green at this head. The policy hold is resolved: ORCH5's 15:28 addendum from Ryan asks for exactly this (everyone moves to speakerguardd v2 by default, plus a one-command user opt-out that installs and selects speakersafetyd, never used in the lab), and SGV2-FLEET's 16:13 decision names this head.

Blockers: none.

Checked: as in my review at this head: the volume lock is refused at every bind until a CAP_SYS_ADMIN write of "Speaker Volume Unlock" to the INACTIVE interface control (so alsactl restore can't select it, and neither can the kernel's own 30 s revert); macaudio_vlimit_update() only ever clears unlock after its initial true; a write of "Speaker Protection" deselects and relocks at once; the lease path is unchanged; flag access is under volume_lock_mutex. KUnit 78/78 (arm64 QEMU), including the new refusal, deselection, revert and unprivileged-write cases. Against main it's a tightening (no daemon lifts the speakers without an explicit root selection). No attribution.

For SGV2-FLEET's rollout: the opt-out tool's unit has to write the selection before starting speakersafetyd, at every boot, since the selection resets at every bind (including after a module reload or driver rebind).

@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (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) labels Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Merge readiness at ea865dc5: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text, pr-text / pr-text, pr-text / pr-text
Review ok approved by satchlj
Requested changes ok none
Hardware row at head ok in the PR body at ea865dc5
Conflicts ok not computed yet
PR text ok Risk: two-way · every Mac with speakers (macaudio, no SoC or board gating). A Mac that runs speakersafetyd or speakerguardd 0.1 without the selection write keeps its speakers at the safe ceiling, and the daemon fails at ELEMLOCK. That is intended, and the distro opt-out tool has to do the write. v2 behaviour is unchanged.

Areas: area:audio

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

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

Code review PASS at ea865dc5e5.

Verified:

  • Requiring an explicit root selection of the legacy volume-lock interface before a daemon can acquire/ping slk prevents an unselected daemon from lifting speaker protection on its own, while reverting from an unowned lease does not re-select the legacy interface.
  • Writing "Speaker Protection" deselects spk_legacy_selected immediately even if the lease transition is refused (e.g. during sleep) and relocks previously lifted volumes.
  • KUnit coverage in macaudio-iface-test.h exercises all state transitions. Signed-off-by present.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Hardware row at ea865dc (J413 M2 Air, unchanged 7.3.0-aurora-fleet89 kernel, runtime module swap, silent: no playback, no magic ping).

Module: fleet89 source 645f7cd plus this PR's macaudio.c diff only (ff59991..ea865dc), built against fleet89's config, vermagic 7.3.0-aurora-fleet89, BTF stripped, unsigned (the Mac has sig_enforce=N, so the kernel took the E taint). The probe is our own SNDRV_CTL_IOCTL_ELEM_LOCK on "Speaker Volume Unlock", the first step a legacy daemon takes. No legacy daemon was run.

step result
A: fresh bind, nothing selected, root probe ELEM_LOCK -> EPERM; log "Speaker Volume Unlock refused: not selected on Speaker Protection Interface"
A2: alsactl store then restore of the fresh-bind state as root (the boot restore path writes 'Speaker Volume Unlock') restore rc 0, probe still EPERM (restore does not select)
B: non-root amixer cset 'Speaker Protection Interface' 'Speaker Volume Unlock' Operation not permitted; root probe after it still EPERM
C: root writes the selection (the opt-out tool's step) log "selected by the administrator"; ELEM_LOCK OK, ELEM_UNLOCK OK
D: root writes "Speaker Protection" log "deselected by the administrator"; probe EPERM again
E: alsa-state restore, PipeWire, speakerguardd v2 0.63.0 on the test module probe EPERM after the alsa-state restore; v2 active, "Speaker protection granted"; selfcheck verify: first run right after the PipeWire restart refused on 1 graph xrun over 5 silent pilot starts, then 2/2 PASS (hardware, voicing AID13, graph)
reboot back to the stock module v2 active, 0 failed units, taint back to 4

Log: traces/audio/20261128-sgv2-fleet/m2/row3479-1925.log on the lab store (our own measurements, not tainted).

@github-actions github-actions Bot removed needs-hw-row No hardware row at the PR head (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 28, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Second-family row at ea865dc: J700 (A18 Pro), unchanged 7.3.0-aurora-fleet89, the same module (sha256 5a8b0915…) and the same probe as the J413 row. Silent: no playback, no magic ping, no legacy daemon run.

step result
A: fresh bind, root probe ELEM_LOCK -> EPERM, log "Speaker Volume Unlock refused: not selected on Speaker Protection Interface"
A2: alsactl store/restore of 'Speaker Volume Unlock' as root rc 0, still EPERM
B: non-root selection write Operation not permitted; root probe after it EPERM
C: root selection "selected by the administrator"; lock and unlock OK
D: root writes "Speaker Protection" "deselected by the administrator"; EPERM
E: alsa-state, PipeWire, speakerguardd v2 0.63.0 EPERM after the alsa-state restore; v2 active, "Speaker protection granted"; selfcheck verify 3/3 PASS (hardware, voicing AID44 +0.012/-0.024 dB, graph)
reboot to stock v2 active, 0 failed units, taint back to 4

Log: traces/audio/20261128-sgv2-fleet/neo-dev/row3479-2026.log on the lab store.

Tested: J413 (T8112) and J700 (A18 Pro) at ea865dc.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Merging at ea865dc5. satchlj (code owner) and the review App approved at this head. Hosted host-tests and KUnit are green. pr-text is stuck in the runner backlog after a body edit; the canonical pr-text.sh passes locally on this body. Rows at this head: J413 (T8112) and J700 (A18 Pro), both silent. linux#3484 (also macaudio.c) landed just before: git merge-tree against main 85cde3c is clean, and it adds no control, so #3484's BUILD_BUG_ON count still holds. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit 68253d5 into main Nov 28, 2026
3 of 14 checks passed
@RyanTheTide
RyanTheTide deleted the fix/speaker-interlock-lease-only branch November 28, 2026 09:37
allanclempe added a commit to allanclempe/omarchy-mac-pkgs that referenced this pull request Nov 30, 2026
Since aurora-silicon/linux#3479 the kernel lets a daemon lock the speaker volume only after a CAP_SYS_ADMIN process writes "Speaker Volume Unlock" to "Speaker Protection Interface" on the card's current bind. speakersafetyd runs as its own user with CAP_SYS_NICE only, so on such a kernel it panics at ELEM_LOCK with EPERM on every start and the speakers stay off. A speakersafetyd drop-in now runs select-speaker-volume-unlock as root (ExecStartPre=-+) before each start, after the wait for the sound card. It writes only to cards that have the control: a kernel without it sees no write, and on a kernel before #3479 that has it the write changes nothing, since "Speaker Volume Unlock" is already its interface. A refused write is logged without stopping speakersafetyd. The helper honours OMARCHY_ASOUND_ROOT only when not run as root, as wait-for-macaudio does.
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:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants