Linux Sound subsystem development
 help / color / mirror / Atom feed
* [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

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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox