[PATCH] ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences
From: Hanxiang Xu
Date: Tue Dec 01 2026 - 22:09:13 EST
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@xxxxxxxxxxxxxxx>
Signed-off-by: Hanxiang Xu <xuhx@xxxxxxxxxxx>
---
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