FAILED: patch "[PATCH] ALSA: core: Check shutdown flag at D0 power-state, too" failed to apply to 5.10-stable tree
Subsystems (2)
Sasha Levin
maintainerThe patch below does not apply to the 5.10-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.10.y
git checkout FETCH_HEAD
git cherry-pick -x ad63c5b62843895e8c3cd7291ed07cc659df0355
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 5.10.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From ad63c5b62843895e8c3cd7291ed07cc659df0355 Mon Sep 17 00:00:00 2001
From: Takashi Iwai <[email protected]>
Date: Tue, 29 Sep 2026 14:53:41 +0200
Subject: [PATCH] ALSA: core: Check shutdown flag at D0 power-state, too
The snd_power_ref_and_wait() skips the card->shutdown check when the card is already in D0 state and immediately returns as successful, by assuming the all-green in D0. This assumption makes the code after this sync point behaving as if all power is up and ready, even though actually it might have been already at the disconnected state.
Add the missing check of shutdown flag there for the more consistent behavior.
--- sound/core/init.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
sound/core/init.c +6 -6
@@ -1174,12 +1174,12 @@ EXPORT_SYMBOL(snd_card_file_remove);int snd_power_ref_and_wait (struct snd_card *card) {snd_power_ref (card); -if (snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 ) -return 0 ; -wait_event_cmd (card->power_sleep, - card->shutdown || -snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , -snd_power_unref (card),snd_power_ref (card)); +if (snd_power_get_state (card) !=SNDRV_CTL_POWER_D0 ) { +wait_event_cmd (card->power_sleep, + card->shutdown || +snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , +snd_power_unref (card),snd_power_ref (card)); + }if (card->shutdown) {snd_power_unref (card);return -ENODEV ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/5 ALSA: control - introduce snd_ctl_notify_one() helper
From: Jaroslav Kysela <[email protected]>
[ Upstream commit 1fa4445f9adf19a3028ce0e8f375bac75214fc10 ]
This helper is required for the following generic LED mute patch. The helper also simplifies some other functions.
Stable-dep-of: ad63c5b62843 ("ALSA: core: Check shutdown flag at D0 power-state, too")
--- include/sound/control.h | 4 +-- sound/core/control.c | 68 +++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 25 deletions(-)
include/sound/control.h +2 -2
@@ -115,6 +115,7 @@ typedef int (*snd_kctl_ioctl_func_t) (struct snd_card * card, unsigned int cmd, unsigned long arg);void snd_ctl_notify (struct snd_card * card,unsigned int mask,struct snd_ctl_elem_id * id); +void snd_ctl_notify_one (struct snd_card * card,unsigned int mask,struct snd_kcontrol * kctl,unsigned int ioff);struct snd_kcontrol *snd_ctl_new1 (const struct snd_kcontrol_new * kcontrolnew,void * private_data);void snd_ctl_free_one (struct snd_kcontrol * kcontrol);
@@ -123,8 +124,7 @@ int snd_ctl_remove(struct snd_card * card, struct snd_kcontrol * kcontrol);int snd_ctl_replace (struct snd_card *card,struct snd_kcontrol *kcontrol,bool add_on_replace);int snd_ctl_remove_id (struct snd_card * card,struct snd_ctl_elem_id *id);int snd_ctl_rename_id (struct snd_card * card,struct snd_ctl_elem_id *src_id,struct snd_ctl_elem_id *dst_id); -int snd_ctl_activate_id (struct snd_card *card,struct snd_ctl_elem_id *id, -int active); +int snd_ctl_activate_id (struct snd_card *card,struct snd_ctl_elem_id *id,int active);struct snd_kcontrol *snd_ctl_find_numid (struct snd_card * card,unsigned int numid);struct snd_kcontrol *snd_ctl_find_id (struct snd_card * card,struct snd_ctl_elem_id *id);
sound/core/control.c +45 -23
146 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/5 ALSA: control: Track in-flight control read/write/tlv accesses
From: Takashi Iwai <[email protected]>
[ Upstream commit e94fdbd7b25d87e64688bb109e2c550217a4c879 ]
Although the power state check is performed in various places (e.g. at the entrance of quite a few ioctls), there can be still some pending tasks that already went into the ioctl handler or other ops, and those may access the hardware even after the power state check. For example, kcontrol access ioctl paths that call info/get/put callbacks may update the hardware registers. If a system wants to assure the free from such hw access (like the case of PCI rescan feature we're going to implement in future), this situation must be avoided, and we have to sync such in-flight tasks finishing beforehand.
For that purpose, this patch introduces a few new things in core code:
- A refcount, power_ref, and a wait queue, power_ref_sleep, to the card object
- A few new helpers, snd_power_ref(), snd_power_unref(), snd_power_ref_and_wait(), and snd_power_sync_ref()
In the code paths that call kctl info/read/write/tlv ops, we check the power state with the newly introduced snd_power_ref_and_wait(). This function also takes the card.power_ref refcount for tracking this in-flight task. Once after the access finishes, snd_power_unref() is called to released the refcount in return. So the driver can sync via snd_power_sync_ref() assuring that all in-flight tasks have been finished.
As of this patch, snd_power_sync_ref() is called only at snd_card_disconnect(), but it'll be used in other places in future.
Note that atomic_t is used for power_ref intentionally instead of refcount_t. It's because of the design of refcount_t type; refcount_t cannot be zero-based, and it cannot do dec_and_test() call for multiple times, hence it's not suitable for our purpose.
Also, this patch changes snd_power_wait() to accept only SNDRV_CTL_POWER_D0, which is the only value that makes sense. In later patch, the snd_power_wait() calls will be cleaned up.
Stable-dep-of: ad63c5b62843 ("ALSA: core: Check shutdown flag at D0 power-state, too")
--- include/sound/core.h | 42 +++++++++++++++++++++++++++++++ sound/core/control.c | 23 +++++++++++++---- sound/core/control_compat.c | 5 +++- sound/core/init.c | 50 +++++++++++++++++++++++++++++++------ 4 files changed, 106 insertions(+), 14 deletions(-)
include/sound/core.h +42 -0
@@ -125,7 +125,9 @@ struct snd_card {#ifdef CONFIG_PM unsigned int power_state;/* power state */ +atomic_t power_ref;wait_queue_head_t power_sleep; +wait_queue_head_t power_ref_sleep;#endif #if IS_ENABLED (CONFIG_SND_MIXER_OSS )
@@ -148,12 +150,52 @@ static inline void snd_power_change_state(struct snd_card *card, unsigned int st wake_up(&card->power_sleep); } +/** +* snd_power_ref - Take the reference count for power control +* @card: sound card object +* +* The power_ref reference of the card is used for managing to block +* the snd_power_sync_ref() operation. This function increments the reference. +* The counterpart snd_power_unref() has to be called appropriately later. +*/ +static inline void snd_power_ref (struct snd_card *card) +{ +atomic_inc (&card->power_ref); +} + +/** +* snd_power_unref - Release the reference count for power control +* @card: sound card object +*/ +static inline void snd_power_unref (struct snd_card *card) +{ +if (atomic_dec_and_test (&card->power_ref)) +wake_up (&card->power_ref_sleep); +} + +/** +* snd_power_sync_ref - wait until the card power_ref is freed +* @card: sound card object +* +* This function is used to synchronize with the pending power_ref being +* released. +*/ +static inline void snd_power_sync_ref (struct snd_card *card) +{ +wait_event (card->power_ref_sleep, !atomic_read (&card->power_ref)); +} +/* init.c */ int snd_power_wait (struct snd_card *card,unsigned int power_state); +int snd_power_ref_and_wait (struct snd_card *card);#else /* ! CONFIG_PM */ static inline int snd_power_wait (struct snd_card *card,unsigned int state) {return 0 ; } +static inline void snd_power_ref (struct snd_card *card) {} +static inline void snd_power_unref (struct snd_card *card) {} +static inline int snd_power_ref_and_wait (struct snd_card *card) {return 0 ; } +static inline void snd_power_sync_ref (struct snd_card *card) {}#define snd_power_get_state (card) ({ (void)(card); SNDRV_CTL_POWER_D0; })#define snd_power_change_state (card, state) do { (void)(card); } while (0)
sound/core/control.c +18 -5
@@ -987,7 +987,10 @@ static int __snd_ctl_elem_info(struct snd_card *card,#ifdef CONFIG_SND_DEBUG info->access =0 ;#endif - result = kctl->info (kctl, info); + result =snd_power_ref_and_wait (card); +if (!result) + result = kctl->info (kctl, info); +snd_power_unref (card);if (result >=0 ) {snd_BUG_ON (info->access); index_offset =snd_ctl_get_ioff (kctl, &info->id);
@@ -1079,7 +1082,10 @@ static int snd_ctl_elem_read(struct snd_card *card,if (!snd_ctl_skip_validation (&info))fill_remaining_elem_value (control, &info, pattern); - ret = kctl->get (kctl, control); + ret =snd_power_ref_and_wait (card); +if (!ret) + ret = kctl->get (kctl, control); +snd_power_unref (card);if (ret <0 )return ret; if (!snd_ctl_skip_validation(&info) &&
@@ -1145,7 +1151,10 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file, }snd_ctl_build_ioff (&control->id, kctl, index_offset); - result = kctl->put (kctl, control); + result =snd_power_ref_and_wait (card); +if (!result) + result = kctl->put (kctl, control); +snd_power_unref (card);if (result <0 ) {up_write (&card->controls_rwsem);return result;
@@ -1633,7 +1642,7 @@ static int call_tlv_handler(struct snd_ctl_file *file, int op_flag, {SNDRV_CTL_TLV_OP_CMD, SNDRV_CTL_ELEM_ACCESS_TLV_COMMAND}, };struct snd_kcontrol_volatile *vd = &kctl->vd[snd_ctl_get_ioff (kctl, id)]; -int i; +int i, ret;/* Check support of the request for this element. */ for (i =0 ; i <ARRAY_SIZE (pairs); ++i) {
@@ -1651,7 +1660,11 @@ static int call_tlv_handler(struct snd_ctl_file *file, int op_flag, vd->owner != NULL && vd->owner != file)return -EPERM ; -return kctl->tlv.c (kctl, op_flag, size, buf); + ret =snd_power_ref_and_wait (file->card); +if (!ret) + ret = kctl->tlv.c (kctl, op_flag, size, buf); +snd_power_unref (file->card); +return ret; } static int read_tlv_buf(struct snd_kcontrol *kctl, struct snd_ctl_elem_id *id,
sound/core/control_compat.c +4 -1
@@ -187,7 +187,10 @@ static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,return -ENOMEM ; } info->id = *id; - err = kctl->info (kctl, info); + err =snd_power_ref_and_wait (card); +if (!err) + err = kctl->info (kctl, info); +snd_power_unref (card);up_read (&card->controls_rwsem);if (err >=0 ) { err = info->type;
sound/core/init.c +42 -8
88 lines not on this page · raw message
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/5 ALSA: core: Simplify snd_power_ref_and_wait() with the standard macro
In reply to Sasha LevinFrom: Takashi Iwai <[email protected]>
[ Upstream commit 8c0ae778e2874f3742bd619000b791d178c187e2 ]
Use wait_event_cmd() macro and simplify snd_power_ref_wait() implementation. This may also cover possible races in the current open code, too.
Stable-dep-of: ad63c5b62843 ("ALSA: core: Check shutdown flag at D0 power-state, too")
--- sound/core/init.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-)
sound/core/init.c +5 -20
@@ -1014,29 +1014,14 @@ EXPORT_SYMBOL(snd_card_file_remove);*/ int snd_power_ref_and_wait (struct snd_card *card) { -wait_queue_entry_t wait; -int result =0 ; -snd_power_ref (card); -/* fastpath */ if (snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 )return 0 ; -init_waitqueue_entry (&wait, current); -add_wait_queue (&card->power_sleep, &wait); -while (1 ) { -if (card->shutdown) { - result = -ENODEV ; -break ; - } -if (snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 ) -break ; -snd_power_unref (card); -set_current_state (TASK_UNINTERRUPTIBLE ); -schedule_timeout (30 *HZ ); -snd_power_ref (card); - } -remove_wait_queue (&card->power_sleep, &wait); -return result; +wait_event_cmd (card->power_sleep, + card->shutdown || +snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , +snd_power_unref (card),snd_power_ref (card)); +return card->shutdown ? -ENODEV :0 ; }EXPORT_SYMBOL_GPL (snd_power_ref_and_wait);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/5 ALSA: core: Fix unintuitive behavior of snd_power_ref_and_wait()
In reply to Sasha LevinFrom: Takashi Iwai <[email protected]>
[ Upstream commit b113a891252c3fa4fab11ec8c2894a22ecaf278c ]
snd_power_ref_and_wait() takes the power refcount and doesn't leave it no matter whether it returns an error or not. However, the majority of callers don't expect but just returns without unreferencing in the caller side upon errors.
For addressing the potential refcount unbalance, rather correct the behavior of snd_power_ref_wait() to unreference upon returning an error.
Note that the problem above is likely negligible; the function returns an error only when the sound card is being shutdown, hence it doesn't matter about the power refcount any longer at such a state.
Stable-dep-of: ad63c5b62843 ("ALSA: core: Check shutdown flag at D0 power-state, too")
--- sound/core/init.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
sound/core/init.c +8 -3
@@ -1008,7 +1008,7 @@ EXPORT_SYMBOL(snd_card_file_remove);* typically around calling control ops. * * The caller needs to pull down the refcount via snd_power_unref() later -* no matter whether the error is returned from this function or not. +* when this function returns 0. * * Return: Zero if successful, or a negative error code. */
@@ -1021,7 +1021,11 @@ int snd_power_ref_and_wait(struct snd_card *card) card->shutdown ||snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , snd_power_unref(card), snd_power_ref(card)); -return card->shutdown ? -ENODEV :0 ; +if (card->shutdown) { +snd_power_unref (card); +return -ENODEV ; + } +return 0 ; }EXPORT_SYMBOL_GPL (snd_power_ref_and_wait);
@@ -1042,7 +1046,8 @@ int snd_power_wait(struct snd_card *card, unsigned int power_state)if (WARN_ON (power_state !=SNDRV_CTL_POWER_D0 ))return 0 ; ret =snd_power_ref_and_wait (card); -snd_power_unref (card); +if (!ret) +snd_power_unref (card);return ret; }EXPORT_SYMBOL (snd_power_wait);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/5 ALSA: core: Check shutdown flag at D0 power-state, too
In reply to Sasha LevinFrom: Takashi Iwai <[email protected]>
[ Upstream commit ad63c5b62843895e8c3cd7291ed07cc659df0355 ]
The snd_power_ref_and_wait() skips the card->shutdown check when the card is already in D0 state and immediately returns as successful, by assuming the all-green in D0. This assumption makes the code after this sync point behaving as if all power is up and ready, even though actually it might have been already at the disconnected state.
Add the missing check of shutdown flag there for the more consistent behavior.
--- sound/core/init.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
sound/core/init.c +6 -6
@@ -1015,12 +1015,12 @@ EXPORT_SYMBOL(snd_card_file_remove);int snd_power_ref_and_wait (struct snd_card *card) {snd_power_ref (card); -if (snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 ) -return 0 ; -wait_event_cmd (card->power_sleep, - card->shutdown || -snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , -snd_power_unref (card),snd_power_ref (card)); +if (snd_power_get_state (card) !=SNDRV_CTL_POWER_D0 ) { +wait_event_cmd (card->power_sleep, + card->shutdown || +snd_power_get_state (card) ==SNDRV_CTL_POWER_D0 , +snd_power_unref (card),snd_power_ref (card)); + }if (card->shutdown) {snd_power_unref (card);return -ENODEV ;
Signature · 1 line
2.53.0