* [PATCH] ALSA: opl3: serialize patch_table access with opl3->voice_lock
@ 2026-12-02 2:05 Hanxiang Xu
2026-12-02 3:16 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hanxiang Xu @ 2026-12-02 2:05 UTC (permalink / raw)
To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel
The OPL3 patch table is manipulated with no serialization:
snd_opl3_clear_patches() kfrees every struct fm_patch and only clears
the bucket heads afterwards, and snd_opl3_load_patch() inserts into and
fills the same objects via snd_opl3_find_patch(create=1), while
snd_opl3_note_on() walks them under opl3->voice_lock. Two
SNDRV_DM_FM_IOCTL_CLEAR_PATCHES ioctls sharing one hwdep fd double-free
the same nodes, and a concurrent write() writes through freed memory
(patch->type/inst/name), leaving the hash chain corrupted for later
lookups.
Hold opl3->voice_lock across the lookup and the fill in
snd_opl3_load_patch(), and unlink the whole table under the lock in
snd_opl3_clear_patches(), freeing the detached chain after the unlock.
The sequencer reader already holds the same lock, so the three contexts
are now pairwise serialized.
Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
sound/drivers/opl3/opl3_synth.c | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/sound/drivers/opl3/opl3_synth.c b/sound/drivers/opl3/opl3_synth.c
index 47a2486..311c638 100644
--- a/sound/drivers/opl3/opl3_synth.c
+++ b/sound/drivers/opl3/opl3_synth.c
@@ -244,10 +244,14 @@ int snd_opl3_load_patch(struct snd_opl3 *opl3,
{
struct fm_patch *patch;
int i;
+ unsigned long flags;
+ spin_lock_irqsave(&opl3->voice_lock, flags);
patch = snd_opl3_find_patch(opl3, prog, bank, 1);
- if (!patch)
+ if (!patch) {
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
return -ENOMEM;
+ }
patch->type = type;
@@ -290,6 +294,7 @@ int snd_opl3_load_patch(struct snd_opl3 *opl3,
if (name)
strscpy(patch->name, name, sizeof(patch->name));
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
return 0;
}
EXPORT_SYMBOL(snd_opl3_load_patch);
@@ -330,14 +335,24 @@ EXPORT_SYMBOL(snd_opl3_find_patch);
void snd_opl3_clear_patches(struct snd_opl3 *opl3)
{
int i;
+ struct fm_patch *patch, *next, *free_list = NULL;
+ unsigned long flags;
+
+ spin_lock_irqsave(&opl3->voice_lock, flags);
for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
- struct fm_patch *patch, *next;
for (patch = opl3->patch_table[i]; patch; patch = next) {
next = patch->next;
- kfree(patch);
+ patch->next = free_list;
+ free_list = patch;
}
}
memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+
+ for (patch = free_list; patch; patch = next) {
+ next = patch->next;
+ kfree(patch);
+ }
}
#endif /* OPL3_SUPPORT_SYNTH */
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] ALSA: opl3: serialize patch_table access with opl3->voice_lock
2026-12-02 2:05 [PATCH] ALSA: opl3: serialize patch_table access with opl3->voice_lock Hanxiang Xu
@ 2026-12-02 3:16 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 3:16 UTC (permalink / raw)
To: Hanxiang Xu; +Cc: linux-sound
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] ALSA: opl3: GFP_KERNEL allocation under voice_lock in
snd_opl3_load_patch()
- [High] ALSA: opl3: uninitialized or reinitialized voice_lock in patch
table paths
- [Low] ALSA: opl3: missing Fixes trailer for patch_table serialization
fix
--
commit d1b6b14cc692f61065a86fe44ac2106bb9a023aa
Author: Hanxiang Xu <xuhx@hust.edu.cn>
> ALSA: opl3: serialize patch_table access with opl3->voice_lock
>
> The OPL3 patch table is manipulated with no serialization:
> snd_opl3_clear_patches() kfrees every struct fm_patch and only clears
> the bucket heads afterwards, and snd_opl3_load_patch() inserts into and
> fills the same objects via snd_opl3_find_patch(create=1), while
> snd_opl3_note_on() walks them under opl3->voice_lock. Two
> SNDRV_DM_FM_IOCTL_CLEAR_PATCHES ioctls sharing one hwdep fd double-free
> the same nodes, and a concurrent write() writes through freed memory
> (patch->type/inst/name), leaving the hash chain corrupted for later
> lookups.
>
> Hold opl3->voice_lock across the lookup and the fill in
> snd_opl3_load_patch(), and unlink the whole table under the lock in
> snd_opl3_clear_patches(), freeing the detached chain after the unlock.
> The sequencer reader already holds the same lock, so the three contexts
> are now pairwise serialized.
>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
[Severity: Low]
Should this bug fix include a Fixes tag for the commit that introduced the
unlocked patch_table helpers?
Fixes: 224a033252bb ("[ALSA] opl3 - Use hwdep for patch loading")
> diff --git a/sound/drivers/opl3/opl3_synth.c b/sound/drivers/opl3/opl3_synth.c
> index 47a2486be46d5..311c63863d6e3 100644
> --- a/sound/drivers/opl3/opl3_synth.c
> +++ b/sound/drivers/opl3/opl3_synth.c
> @@ -244,10 +244,14 @@ int snd_opl3_load_patch(struct snd_opl3 *opl3,
> {
> struct fm_patch *patch;
> int i;
> + unsigned long flags;
>
> + spin_lock_irqsave(&opl3->voice_lock, flags);
> patch = snd_opl3_find_patch(opl3, prog, bank, 1);
[Severity: High]
Does calling snd_opl3_find_patch(..., 1) under opl3->voice_lock sleep in
atomic context when a new (prog, bank) entry is loaded?
When create_patch is set and no entry exists, snd_opl3_find_patch()
allocates the patch with kzalloc_obj(*patch), which defaults to GFP_KERNEL
and can sleep while interrupts are disabled and opl3->voice_lock is held.
[ ... ]
> @@ -330,14 +335,24 @@ EXPORT_SYMBOL(snd_opl3_find_patch);
> void snd_opl3_clear_patches(struct snd_opl3 *opl3)
> {
> int i;
> + struct fm_patch *patch, *next, *free_list = NULL;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&opl3->voice_lock, flags);
[Severity: High]
Is opl3->voice_lock guaranteed to be initialized when
snd_opl3_clear_patches() or snd_opl3_load_patch() acquires it?
snd_opl3_new() initializes opl3->reg_lock and opl3->timer_lock, while
spin_lock_init(&opl3->voice_lock) only runs in snd_opl3_seq_probe() in the
separate snd-opl3-synth module:
sound/drivers/opl3/opl3_lib.c:snd_opl3_new() {
...
spin_lock_init(&opl3->reg_lock);
spin_lock_init(&opl3->timer_lock);
err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops);
if (err < 0) {
snd_opl3_free(opl3);
return err;
}
...
}
If snd_opl3_free() -> snd_opl3_clear_patches() runs on probe failure, on
teardown without snd-opl3-synth bound, or if userspace accesses the hwdep
device before snd_opl3_seq_probe() runs, spin_lock_irqsave() operates on an
uninitialized spinlock_t, and a later snd_opl3_seq_probe() reinitializes the
lock while hwdep callers may already be using it. Initializing
opl3->voice_lock in snd_opl3_new() instead of snd_opl3_seq_probe() would
avoid both issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100501.1-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:16 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:05 [PATCH] ALSA: opl3: serialize patch_table access with opl3->voice_lock Hanxiang Xu
2026-12-02 3:16 ` 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.