All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences
@ 2026-12-02  2:35 Hanxiang Xu
  2026-12-02  3:18 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02  2:35 UTC (permalink / raw)
  To: Johannes Berg, Takashi Iwai; +Cc: linuxppc-dev, linux-sound, linux-kernel

i2sbus_detach_codec() manipulates dev->codec_list without any lock
and ends in an unlocked check-then-free:
"if (list_empty(&dev->codec_list) && dev->pcm)
snd_device_free(dev->pcm->card, dev->pcm)".  The callers hold device
locks that do not serialize against each other: codec drivers detach
from i2c driver context (each holding its own i2c client lock) while
bus removal runs from soundbus/macio context, and a bus can host two
codecs (MAX_CODECS_PER_BUS).  Two concurrent codec detaches therefore
both pass the dev->pcm check before either clears it and both run
snd_device_free() on the same pcm, double-freeing it (the second call
also walks card->devices while the first is freeing the snd_device
wrapper).

Serialize attach/detach with a mutex and claim dev->pcm before
calling snd_device_free() so a concurrent detach cannot re-enter the
free.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 sound/aoa/soundbus/i2sbus/pcm.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/sound/aoa/soundbus/i2sbus/pcm.c b/sound/aoa/soundbus/i2sbus/pcm.c
index 63004ece94f9333eaacdeb9ed2fb7298743d2e45..3fbc34a247988d7a0de7edb7dcd16b266880653d 100644
@@ -862,6 +862,8 @@
 	struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
 	struct codec_info_item *cii;
 
+	guard(mutex)(&i2sbus_pcm_mutex);
+
 	if (!dev->pcmname || dev->pcmid == -1) {
 		printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
 		return -EINVAL;
@@ -1018,10 +1020,18 @@ static void i2sbus_private_free(struct snd_pcm *pcm)
 	return err;
 }
 
+/*
+ * codec attach/detach (i2c driver context) and bus removal
+ * (soundbus/macio context) hold different device locks; serialize the
+ * codec_list and dev->pcm sequences between them.
+ */
+static DEFINE_MUTEX(i2sbus_pcm_mutex);
+
 void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
 {
 	struct codec_info_item *cii = NULL, *i;
 
+	guard(mutex)(&i2sbus_pcm_mutex);
 	list_for_each_entry(i, &dev->codec_list, list) {
 		if (i->codec_data == data) {
 			cii = i;
@@ -1035,7 +1045,13 @@ void i2sbus_detach_codec(struct soundbus_dev *dev, void *data)
 	}
 	/* no more codecs, but still a pcm? */
 	if (list_empty(&dev->codec_list) && dev->pcm) {
+		struct snd_pcm *pcm = dev->pcm;
+		struct snd_card *card = pcm->card;
+
+		/* claim before freeing: a concurrent detach_codec() must not
+		 * re-enter snd_device_free() on the same pcm */
+		dev->pcm = NULL;
 		/* the actual cleanup is done by the callback above! */
-		snd_device_free(dev->pcm->card, dev->pcm);
-	}
-}
+		snd_device_free(card, pcm);
+	}
+}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences
  2026-12-02  2:35 [PATCH] ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences Hanxiang Xu
@ 2026-12-02  3:18 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  3:18 UTC (permalink / raw)
  To: Hanxiang Xu; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ALSA: aoa: i2sbus: undeclared i2sbus_pcm_mutex in
  i2sbus_attach_codec()
- [Low] ALSA: aoa: i2sbus: missing Fixes trailer on bug-fix commit
--

commit 0568c140cfaf58b5209a2f736b6288cd639d9e34
Author: Hanxiang Xu <xuhx@hust.edu.cn>

> ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences
>
> i2sbus_detach_codec() manipulates dev->codec_list without any lock
> and ends in an unlocked check-then-free:
> "if (list_empty(&dev->codec_list) && dev->pcm)
> snd_device_free(dev->pcm->card, dev->pcm)".  The callers hold device
> locks that do not serialize against each other: codec drivers detach
> from i2c driver context (each holding its own i2c client lock) while
> bus removal runs from soundbus/macio context, and a bus can host two
> codecs (MAX_CODECS_PER_BUS).  Two concurrent codec detaches therefore
> both pass the dev->pcm check before either clears it and both run
> snd_device_free() on the same pcm, double-freeing it (the second call
> also walks card->devices while the first is freeing the snd_device
> wrapper).
>
> Serialize attach/detach with a mutex and claim dev->pcm before
> calling snd_device_free() so a concurrent detach cannot re-enter the
> free.
>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>

[Severity: Low]
Should this bug fix include a Fixes: tag?

  Fixes: f3d9478b2ce4 ("[ALSA] snd-aoa: add snd-aoa")

> diff --git a/sound/aoa/soundbus/i2sbus/pcm.c b/sound/aoa/soundbus/i2sbus/pcm.c
> index 63004ece94f93..3fbc34a247988 100644
> --- a/sound/aoa/soundbus/i2sbus/pcm.c
> +++ b/sound/aoa/soundbus/i2sbus/pcm.c
> @@ -862,6 +862,8 @@ i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
>  	struct i2sbus_dev *i2sdev = soundbus_dev_to_i2sbus_dev(dev);
>  	struct codec_info_item *cii;
>  
> +	guard(mutex)(&i2sbus_pcm_mutex);
> +
>  	if (!dev->pcmname || dev->pcmid == -1) {
>  		printk(KERN_ERR "i2sbus: pcm name and id must be set!\n");
>  		return -EINVAL;
> @@ -1018,10 +1020,18 @@ i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card,
>  	return err;
>  }
>  
> +/*
> + * codec attach/detach (i2c driver context) and bus removal
> + * (soundbus/macio context) hold different device locks; serialize the
> + * codec_list and dev->pcm sequences between them.
> + */
> +static DEFINE_MUTEX(i2sbus_pcm_mutex);

[Severity: High]
Does this fail to build because i2sbus_pcm_mutex is defined below
i2sbus_attach_codec(), where it is already used without a prior declaration?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202103500.6-1-xuhx@hust.edu.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02  3:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  2:35 [PATCH] ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences Hanxiang Xu
2026-12-02  3:18 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.