Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hanxiang Xu" <xuhx@hust.edu.cn>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: opl3: serialize patch_table access with opl3->voice_lock
Date: Wed, 02 Dec 2026 03:16:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-244749@kernel.org> (raw)
In-Reply-To: <20261202100501.1-1-xuhx@hust.edu.cn>

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

      reply	other threads:[~2026-12-02  3:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-244749@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xuhx@hust.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox