ALSA: aoa: i2sbus: serialize codec_list and pcm check-and-free sequences
Subsystems (2)
PATCH 1/1 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.
--- sound/aoa/soundbus/i2sbus/pcm.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
sound/aoa/soundbus/i2sbus/pcm.c +19 -3
@@ -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); + } +}
Signature · 1 line
2.43.0
[email protected]
AI reviewThank 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…
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 <[email protected]>
Quoted 15 lines
ALSA: aoa: i2sbus: serialize
codec_listand pcm check-and-free sequences
i2sbus_detach_codec()manipulatesdev->codec_listwithout 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 thedev->pcmcheck before either clears it and both runsnd_device_free()on the same pcm, double-freeing it (the second call also walkscard->deviceswhile the first is freeing thesnd_devicewrapper).
Quoted 6 lines
Serialize attach/detach with a mutex and claim
dev->pcmbefore callingsnd_device_free()so a concurrent detach cannot re-enter the free.Cc <[email protected]>Signed-off-by Hanxiang Xu <[email protected]>
[Severity: Low]
Should this bug fix include a Fixes: tag?
Quoted 14 lines
sound/aoa/soundbus/i2sbus/pcm.c +2
-0@@ -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 ;
sound/aoa/soundbus/i2sbus/pcm.c i2sbus_attach_codec(struct soundbus_dev *dev, struct snd_card *card, · line 1028
Quoted 9 lines
sound/aoa/soundbus/i2sbus/pcm.c +6
-0@@ -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?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1