Repository navigation
ASoC: apple: macaudio: volume lock only after an explicit root selection - #3479
Conversation
…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>
|
@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. |
There was a problem hiding this comment.
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-EPERMforELEM_LOCKon "Speaker Volume Unlock", andmacaudio_slk_put()returns-EPERMforSPEAKER_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 forcesunlock = unlock && macaudio_spk_leased(ma)before choosing the ceiling for every amp type, so even a strayunlock=truecall 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'ssnd_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>
|
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. |
There was a problem hiding this comment.
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_selectedstarts false at every bind (macaudio_late_probe); without it,ELEM_LOCKon "Speaker Volume Unlock" and the magic ping return-EPERM(one-time log line), andmacaudio_vlimit_update()forcesunlock = false. In that functionunlockstarts true and every later branch only sets it false, so nothing can undo the refusal. - Who can select: only
macaudio_spk_iface_put(), which checkscapable(CAP_SYS_ADMIN)first. The control isINACTIVE, soalsactl restoreat 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 underspk_mutexpluscontrols_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.
|
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. |
There was a problem hiding this comment.
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).
|
Merge readiness at
Areas: Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels. |
satchlj
left a comment
There was a problem hiding this comment.
Code review PASS at ea865dc5e5.
Verified:
- Requiring an explicit root selection of the legacy volume-lock interface before a daemon can acquire/ping
slkprevents 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_selectedimmediately even if the lease transition is refused (e.g. during sleep) and relocks previously lifted volumes. - KUnit coverage in
macaudio-iface-test.hexercises all state transitions.Signed-off-bypresent.
|
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
Log: |
|
Second-family row at ea865dc: J700 (A18 Pro), unchanged 7.3.0-aurora-fleet89, the same module (sha256
Log: Tested: J413 (T8112) and J700 (A18 Pro) at ea865dc. |
|
Merging at |
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.
Problem: On every Mac, "Speaker Volume Unlock" is the speaker interface at every bind. Any process that took
ELEM_LOCKon that control and on every speaker volume, then pinged it withSPEAKER_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_LOCKon "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:
While leased the ping still gets
-EBUSY. The lease path (v2) is unchanged. The net diff against main is +191/-2 insound/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'sExecStartPre. Without that write, speakersafetyd gets-EPERMatELEM_LOCKand 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, filterapple-*): 78/78 passed. New cases: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 withW=1and no warnings in the fleet release config (fedora 7.0.13 +asahi.config, clang 22, rustc 1.97.1).checkpatch.plon 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
spk_legacy_selected, written underspk_mutexandvolume_lock_mutexbymacaudio_slk_select()frommacaudio_spk_iface_put()only, and cleared inmacaudio_late_probe().macaudio_slk_lock()(before taking ownership),macaudio_slk_put()(after the existing owner-EPERMand lease-EBUSYchecks), andmacaudio_vlimit_update()(no unlock without it).aop_t8140.rs: the full-scale register has no control.please_blow_up_my_speakersdoes nothing.__queue_workWARN inmacaudio_spk_refused_starts_are_counted. It is also on main and this PR doesn't touch it: the protection-test fixture never initialiseslock_update_work, whichmacaudio_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).