From 4636680b13ddbf3d153a4f9727001976036a0343 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sun, 22 Nov 2026 08:52:38 +0000 Subject: [PATCH 1/3] ASoC: apple: macaudio: Keep the speaker volumes wherever the sleep lock 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: 911d47c3d1a5 ("ASoC: apple: macaudio: Set the speaker volumes again after a system sleep") Signed-off-by: Dylan Ironside --- sound/soc/apple/macaudio.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index ae4c96a816fe4..a3c982c35a04d 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -382,6 +382,8 @@ struct macaudio_snd_data { */ struct macaudio_pm_volume *pm_volumes; unsigned int pm_nr_volumes; + /* The system-sleep lock clipped the volumes and its unlock has not come */ + bool pm_lock_taken; /* Allocated with them, so that setting them again cannot fail */ struct snd_ctl_elem_value *pm_uval; @@ -673,6 +675,7 @@ static void macaudio_pm_volumes_drop(struct macaudio_snd_data *ma) ma->pm_volumes = NULL; ma->pm_nr_volumes = 0; ma->pm_uval = NULL; + ma->pm_lock_taken = false; } /* @@ -734,6 +737,7 @@ static void macaudio_pm_volumes_save(struct macaudio_snd_data *ma) ma->pm_volumes = vols; ma->pm_nr_volumes = i; ma->pm_uval = uval; + dev_dbg(ma->card.dev, "Kept %u speaker volumes for the system sleep\n", i); return; out: kfree(uval); @@ -753,7 +757,7 @@ static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) int ret; if (!ma->pm_volumes) - return; + goto out; for (i = 0; i < ma->pm_nr_volumes; i++) { struct snd_kcontrol *kctl = ma->pm_volumes[i].kctl; @@ -770,6 +774,9 @@ static void macaudio_pm_volumes_restore(struct macaudio_snd_data *ma) snd_ctl_notify(ma->card.snd_card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id); } + dev_dbg(ma->card.dev, "Set %u speaker volumes again after the system sleep\n", + ma->pm_nr_volumes); +out: macaudio_pm_volumes_drop(ma); } @@ -843,7 +850,29 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) ma->speaker_volume_was_locked = true; } + /* + * The system-sleep lock keeps the volumes wherever it is taken. + * With a speaker stream running at suspend-prepare, the stop + * that macaudio_spk_close_gate() queues reaches here through + * lock_update_work before macaudio_vlimit_pm() does; keeping + * them only there left nothing to set again at the unlock, and + * the volumes stayed at the lock's ceiling for the whole boot. + */ + if (!unlock && ma->pm_quiesced) { + macaudio_pm_volumes_save(ma); + ma->pm_lock_taken = true; + } macaudio_vlimit_unlock(ma, unlock); + if (unlock && ma->pm_lock_taken && !ma->pm_volumes) { + /* + * Nothing kept: the daemon does not set volumes it + * believes unchanged, so tell it the lock was lost; it + * sets them again from its model. + */ + dev_warn(ma->card.dev, + "No speaker volumes kept for the system sleep: the daemon sets them again\n"); + ma->speaker_volume_was_locked = true; + } if (unlock) macaudio_pm_volumes_restore(ma); if (!unlock && ma->cfg->amp == AMP_MAX98360A) { @@ -878,7 +907,6 @@ static void macaudio_vlimit_pm(struct macaudio_snd_data *ma, bool quiesce) if (quiesce) { cancel_delayed_work(&ma->lock_timeout_work); ma->speaker_lock_remain = 0; - macaudio_pm_volumes_save(ma); } macaudio_vlimit_update(ma); mutex_unlock(&ma->volume_lock_mutex); @@ -943,6 +971,11 @@ static void macaudio_vlimit_timeout_work(struct work_struct *wrk) struct macaudio_snd_data, lock_timeout_work); ktime_t now, left; + /* + * An expiry with pm_quiesced set keeps the volumes, which walks the + * card's controls: hold them as macaudio_vlimit_update_work() does. + */ + down_read(&ma->card.snd_card->controls_rwsem); mutex_lock(&ma->volume_lock_mutex); /* @@ -967,6 +1000,7 @@ static void macaudio_vlimit_timeout_work(struct work_struct *wrk) out: mutex_unlock(&ma->volume_lock_mutex); + up_read(&ma->card.snd_card->controls_rwsem); } static void macaudio_vlimit_update_work(struct work_struct *wrk) From 84248fc7ac68e226e163c9387a9a79cac9f5a591 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sun, 22 Nov 2026 23:24:23 +0000 Subject: [PATCH 2/3] ASoC: apple: macaudio: Test the speaker volumes kept for the sleep lock 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 --- sound/soc/apple/macaudio-iface-test.h | 32 +++++++++ sound/soc/apple/macaudio-pm-volumes-test.h | 83 +++++++++++++++++++++- 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/sound/soc/apple/macaudio-iface-test.h b/sound/soc/apple/macaudio-iface-test.h index 6d54f87fcd5fb..93a4323219ccc 100644 --- a/sound/soc/apple/macaudio-iface-test.h +++ b/sound/soc/apple/macaudio-iface-test.h @@ -299,10 +299,42 @@ static void macaudio_iface_control_is_inactive(struct kunit *test) KUNIT_EXPECT_TRUE(test, iface.access & SNDRV_CTL_ELEM_ACCESS_VOLATILE); } +/* + * A sleep on the volume lock, then the lease and back before the daemon's + * first ping: both switches drop the kept volumes, the way back reports the + * lost lock once, and the unlock after it reports nothing more. + */ +static void macaudio_iface_lease_after_sleep(struct kunit *test) +{ + struct macaudio_test_iface f; + struct macaudio_snd_data *ma; + + macaudio_test_iface_card(test, &f); + ma = f.t.ma; + macaudio_test_daemon_take(test, &f); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + macaudio_pm_notify(&ma->pm_nb, PM_SUSPEND_PREPARE, NULL); + macaudio_pm_notify(&ma->pm_nb, PM_POST_SUSPEND, NULL); + KUNIT_EXPECT_NOT_NULL(test, ma->pm_volumes); + KUNIT_EXPECT_TRUE(test, ma->pm_lock_taken); + + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_LEASE), 1); + KUNIT_EXPECT_NULL(test, ma->pm_volumes); + KUNIT_EXPECT_FALSE(test, ma->pm_lock_taken); + KUNIT_EXPECT_EQ(test, macaudio_test_iface_write(test, &f, MACAUDIO_SPK_IFACE_UNLOCK), 1); + macaudio_test_settle(ma); + + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); + KUNIT_EXPECT_TRUE(test, ma->speaker_volume_unlocked); + KUNIT_EXPECT_EQ(test, macaudio_test_daemon_ping(test, &f), 0); +} + static struct kunit_case macaudio_iface_cases[] = { KUNIT_CASE(macaudio_iface_volume_lock_from_binding), KUNIT_CASE(macaudio_iface_switch_goes_through_the_floor), KUNIT_CASE(macaudio_iface_revert_starts_at_the_floor), + KUNIT_CASE(macaudio_iface_lease_after_sleep), KUNIT_CASE(macaudio_iface_unowned_lease_reverts), KUNIT_CASE(macaudio_iface_needs_cap_sys_admin), KUNIT_CASE(macaudio_iface_control_is_inactive), diff --git a/sound/soc/apple/macaudio-pm-volumes-test.h b/sound/soc/apple/macaudio-pm-volumes-test.h index dab9d69bb0faa..99e2889435b13 100644 --- a/sound/soc/apple/macaudio-pm-volumes-test.h +++ b/sound/soc/apple/macaudio-pm-volumes-test.h @@ -8,6 +8,8 @@ struct macaudio_pmt_vol { struct soc_mixer_control mc; long value; + /* The next read fails once, as the keeping before the sleep can meet */ + int get_err; }; static struct macaudio_pmt_vol *macaudio_pmt_vol(struct snd_kcontrol *kctl) @@ -26,7 +28,14 @@ static int macaudio_pmt_vol_info(struct snd_kcontrol *kctl, struct snd_ctl_elem_ static int macaudio_pmt_vol_get(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *uval) { - uval->value.integer.value[0] = macaudio_pmt_vol(kctl)->value; + struct macaudio_pmt_vol *vol = macaudio_pmt_vol(kctl); + int err = vol->get_err; + + /* Once only: the clip after the keeping reads the control too */ + vol->get_err = 0; + if (err) + return err; + uval->value.integer.value[0] = vol->value; return 0; } @@ -254,10 +263,82 @@ static void macaudio_pm_volumes_new_owner(struct kunit *test) macaudio_pmt_expect_vols(test, &t, true); } +/* The stream's stop, through lock_update_work, as the back-ends trigger it */ +static void macaudio_pmt_bes(struct macaudio_pmt_pm *t, bool active) +{ + t->ma->bes_active = active; + macaudio_vlimit_update_work(&t->ma->lock_update_work); +} + +/* + * A speaker stream runs at suspend-prepare: the stop that the gate queues + * takes the lock with pm_quiesced already set, before macaudio_vlimit_pm(). + * The volumes are kept there, and come back at the first ping after resume. + */ +static void macaudio_pm_volumes_lock_taken_before_pm(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + macaudio_pmt_bes(&t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + + WRITE_ONCE(t.ma->pm_quiesced, true); + macaudio_pmt_bes(&t, false); + KUNIT_EXPECT_FALSE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_NOT_NULL(test, t.ma->pm_volumes); + macaudio_pmt_expect_vols(test, &t, true); + + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + macaudio_pmt_expect_vols(test, &t, true); + + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + macaudio_pmt_expect_vols(test, &t, false); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + KUNIT_EXPECT_FALSE(test, t.ma->pm_lock_taken); + /* Set back, not lost: no lost-lock report follows */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); +} + +/* + * Keeping the volumes fails: the unlock after resume has nothing to set + * back, so the daemon learns that the lock was lost (its next ping fails + * with -ETIMEDOUT) and sets the volumes again itself. Once only. + */ +static void macaudio_pm_volumes_save_fails(struct kunit *test) +{ + struct macaudio_pmt_pm t; + + macaudio_pmt_pm_card(test, &t); + macaudio_pmt_take(test, &t); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + + macaudio_pmt_vol(t.vols[1])->get_err = -EIO; + macaudio_pmt_notify(&t, PM_SUSPEND_PREPARE); + KUNIT_EXPECT_NULL(test, t.ma->pm_volumes); + KUNIT_EXPECT_TRUE(test, t.ma->pm_lock_taken); + macaudio_pmt_expect_vols(test, &t, true); + macaudio_pmt_notify(&t, PM_POST_SUSPEND); + + /* The unlock itself: the volumes stay at the ceiling for the daemon */ + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); + KUNIT_EXPECT_TRUE(test, t.ma->speaker_volume_unlocked); + KUNIT_EXPECT_FALSE(test, t.ma->pm_lock_taken); + macaudio_pmt_expect_vols(test, &t, true); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, macaudio_pmt_ping(test, &t), 0); +} + static struct kunit_case macaudio_pm_volumes_cases[] = { KUNIT_CASE(macaudio_pm_volumes_restored_after_sleep), KUNIT_CASE(macaudio_pm_volumes_second_sleep_before_ping), KUNIT_CASE(macaudio_pm_volumes_new_owner), + KUNIT_CASE(macaudio_pm_volumes_lock_taken_before_pm), + KUNIT_CASE(macaudio_pm_volumes_save_fails), {} }; From d5d2cffebae29a2ae076fb0e881ae1c87ed52125 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Mon, 23 Nov 2026 15:04:32 +0000 Subject: [PATCH 3/3] ASoC: apple: macaudio: Read pm_quiesced once per volume-lock update 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 --- sound/soc/apple/macaudio.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sound/soc/apple/macaudio.c b/sound/soc/apple/macaudio.c index a3c982c35a04d..7a72d4d0a5784 100644 --- a/sound/soc/apple/macaudio.c +++ b/sound/soc/apple/macaudio.c @@ -784,6 +784,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) { int i; bool unlock = true; + bool quiesced; struct snd_kcontrol *kctl; const char *reason; @@ -802,6 +803,13 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) if (macaudio_spk_lease_only(ma->cfg)) return; + /* + * macaudio_pm_notify() writes pm_quiesced without volume_lock_mutex: + * read it once, so the lock reason, the lost-lease flag and the kept + * volumes below all see the same value. + */ + quiesced = READ_ONCE(ma->pm_quiesced); + /* Check that someone is holding the main lock */ if (!ma->speaker_lock_owner) { reason = "Main control not locked"; @@ -821,7 +829,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) * Lock the volumes before the freeze instead of when the frozen daemon's * lease runs out. */ - if (ma->pm_quiesced) { + if (quiesced) { reason = "System suspend"; unlock = false; } @@ -846,7 +854,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) } else { dev_info(ma->card.dev, "Speaker volumes locked: %s\n", reason); /* The system-sleep lock is the kernel's own, not a lease the daemon lost */ - if (!ma->pm_quiesced) + if (!quiesced) ma->speaker_volume_was_locked = true; } @@ -858,7 +866,7 @@ static void macaudio_vlimit_update(struct macaudio_snd_data *ma) * them only there left nothing to set again at the unlock, and * the volumes stayed at the lock's ceiling for the whole boot. */ - if (!unlock && ma->pm_quiesced) { + if (!unlock && quiesced) { macaudio_pm_volumes_save(ma); ma->pm_lock_taken = true; } @@ -3935,7 +3943,7 @@ static int macaudio_slk_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_v } /* Between suspend-prepare and the freeze: the lease starts again after the resume */ - if (ma->pm_quiesced) { + if (READ_ONCE(ma->pm_quiesced)) { mutex_unlock(&ma->volume_lock_mutex); return 0; }