Linux Sound subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pavel Artemev" <a@pavel.bz>
Cc: linux-sound@vger.kernel.org
Subject: Re: [RFC PATCH v4 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
Date: Tue, 17 Nov 2026 13:13:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-218847@kernel.org> (raw)
In-Reply-To: <20261117124242.12237-3-a@pavel.bz>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] ALSA: usb-audio: stale pending master state in
  mixer_ctl_feature_put()
- [Medium] ALSA: usb-audio: derived master not restored in
  snd_usb_mixer_flush_pending()
- [Medium] ALSA: usb-audio: missing disconnected check in
  mixer_ctl_feature_put()
--

commit f4eb0b4119dee3a007d6e2f3e25d6abfcb9bb149
Author: Pavel Artemev <a@pavel.bz>

ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag

This commit adds the QUIRK_FLAG_MIXER_STATE_REPUSH quirk flag to defer
feature unit volume and mute writes into coalesced bursts and re-push
cached mixer state on PCM stream open and resume.

Link: https://bugzilla.kernel.org/show_bug.cgi?id=221401
Link: https://lore.kernel.org/linux-sound/20261117092532.408981-3-a@pavel.bz

> diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> index bf1d9fbf08aae..e9f256df7aede 100644
> --- a/sound/usb/mixer.c
> +++ b/sound/usb/mixer.c
[ ... ]
> @@ -1608,18 +1618,342 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,
[ ... ]
> +		/* master first (the Windows burst order) */
> +		if (master && !master_pending && !master->master_explicit &&
> +		    have_ch &&
> +		    (touched || level != FLUSH_TOUCHED) &&
> +		    (!test_bit(0, master->cached) ||
> +		     *master->cache_val != max_val)) {
> +			int err = snd_usb_set_cur_mix_value(master, 0, 0,
> +							    max_val);
> +			if (err < 0)
> +				usb_audio_dbg(mixer->chip,
> +					      "derive ctl %d ch 0 failed (%d)\n",
> +					      master->control, err);
> +		}
> +		if (master && level != FLUSH_TOUCHED && !master_pending &&
> +		    (master->master_explicit || !have_ch))
> +			/* user-owned master, or a unit without per-channel
> +			 * data: re-assert the cached master value
> +			 */
> +			repush_ctl_cached(master);
> +		for_each_mixer_elem(list, mixer, id) {
> +			cval = mixer_elem_list_to_info(list);
> +			if (!is_feature_volume_mute_ctl(cval))
> +				continue;
> +			if (cval->control == UAC_FU_VOLUME && cval->cmask &&
> +			    level != FLUSH_TOUCHED) {
> +				bitmap_zero(cval->pending, MAX_CHANNELS + 1);
> +				repush_ctl_cached(cval);
> +				continue;
> +			}

[Severity: Medium]
Does snd_usb_mixer_flush_pending() skip restoring a derived master volume on
FLUSH_RESUME_STATE and FLUSH_FULL_STATE when *master->cache_val already
matches max_val?

Once a prior flush has derived and written the master volume, bit 0 of
master->cached is set and *master->cache_val equals max_val.  Because
restore_mixer_value() skips all controls matching
is_feature_volume_mute_ctl() and the loop above only calls
repush_ctl_cached() for volume controls with cval->cmask != 0 (or when
master->master_explicit || !have_ch), a derived master is never written back
to the device on resume if no channel volume changed while suspended, and a
derived master write dropped during the post-enumeration window is not
re-sent on PCM stream open.

> @@ -1632,14 +1966,50 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
>  				return -EINVAL;
>  			val = get_abs_value(cval, val);
>  			if (oval != val) {
> -				err = snd_usb_set_cur_mix_value(cval, c + 1,
> -								cnt, val);
> -				if (err < 0)
> -					return filter_error(cval, err);
> +				if (deferred) {
> +					/* record only; one flush writes the
> +					 * whole unit in a single burst.
> +					 * Read-only channels keep the
> +					 * direct-path behaviour: no write,
> +					 * no cache update.
> +					 */
> +					if (!mixer_ch_readonly(cval, c + 1)) {
> +						set_bit(c + 1, cval->cached);
> +						cval->cache_val[cnt] = val;
> +						set_bit(c + 1, cval->pending);
> +						mod_delayed_work(system_wq,
> +								 &cval->head.mixer->flush_work,
> +								 MIXER_FLUSH_DELAY);
> +						ch_put = true;
> +					}
> +				} else {
> +					err = snd_usb_set_cur_mix_value(cval, c + 1,
> +									cnt, val);
> +					if (err < 0)
> +						return filter_error(cval, err);
> +				}
>  				changed = 1;
>  			}
>  			cnt++;
>  		}
> +		/* The last writer owns the master: a channel write (the
> +		 * way audio servers drive this unit) re-claims master
> +		 * derivation, so a master saved and restored by e.g.
> +		 * alsactl does not pin the volume forever.
> +		 */
> +		if (ch_put) {
> +			struct usb_mixer_elem_list *ml;
> +
> +			for_each_mixer_elem(ml, cval->head.mixer,
> +					    cval->head.id) {
> +				struct usb_mixer_elem_info *m =
> +					mixer_elem_list_to_info(ml);
> +
> +				if (is_feature_volume_mute_ctl(m) &&
> +				    m->control == UAC_FU_VOLUME && !m->cmask)
> +					m->master_explicit = false;
> +			}
> +		}

[Severity: Medium]
When a per-channel volume write reclaims master derivation after an unflushed
explicit master write, does leaving bit 0 of m->pending and m->cached set
cause snd_usb_mixer_flush_pending() to write the superseded master value?

If an explicit master put and a per-channel put happen within the same
MIXER_FLUSH_DELAY window (such as during alsactl restore) or while
suspended, the explicit master put sets bit 0 of m->pending and updates
m->cache_val[0].  When the channel put runs, clearing m->master_explicit
leaves bit 0 of m->pending set, so snd_usb_mixer_flush_pending() sees
master_pending == true, skips master derivation, and calls
flush_ctl_pending(master) with the stale explicit value; even if m->pending
were cleared, m->cache_val[0] already holds the unwritten value and can match
max_val, skipping the derived hardware write.

Also, ch_put is set for any deferred per-channel control, so toggling a
per-channel UAC_FU_MUTE control clears m->master_explicit on the unit's
master volume control as well.

[ ... ]
> @@ -3259,6 +3646,8 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer)
>  	/* kill pending URBs */
>  	snd_usb_mixer_disconnect(mixer);
>  
> +	cancel_delayed_work_sync(&mixer->flush_work);
> +
>  	/* Unregister controls first, snd_ctl_remove() frees the element */
>  	if (mixer->id_elems) {
[ ... ]
> @@ -3976,13 +4378,51 @@ void snd_usb_mixer_disconnect(struct usb_mixer_interface *mixer)
>  {
>  	if (mixer->disconnected)
>  		return;
> +	/* Every mod_delayed_work() call site either holds this lock and
> +	 * re-checks ->disconnected inside it, or runs from a PM callback
> +	 * (which completes before the USB core calls driver->disconnect),
> +	 * or is the flush worker itself before it returns.  Marking the
> +	 * mixer disconnected under the lock before cancelling therefore
> +	 * guarantees no flush can be armed after the cancel returns.
> +	 */
> +	scoped_guard(mutex, &mixer->lock) {
> +		mixer->disconnected = true;
> +	}
> +	cancel_delayed_work_sync(&mixer->flush_work);

[Severity: Medium]
Can mixer_ctl_feature_put() re-arm mixer->flush_work after
snd_usb_mixer_disconnect() and snd_usb_mixer_free() have cancelled it?

The comment in snd_usb_mixer_disconnect() notes that every locked
mod_delayed_work() caller checks mixer->disconnected under mixer->lock, but
neither deferred branch in mixer_ctl_feature_put() checks
cval->head.mixer->disconnected before calling mod_delayed_work(), and unlike
the direct write path, the deferred path never calls
snd_usb_lock_shutdown().  In snd_usb_mixer_free(),
cancel_delayed_work_sync(&mixer->flush_work) also runs before
snd_ctl_remove() unregisters the controls:

CPU0                                  CPU1
snd_usb_mixer_free()
  snd_usb_mixer_disconnect()
    mutex_lock(&mixer->lock)
    mixer->disconnected = true
    mutex_unlock(&mixer->lock)
    cancel_delayed_work_sync()
  cancel_delayed_work_sync()
                                      mixer_ctl_feature_put()
                                        mutex_lock(&mixer->lock)
                                        mod_delayed_work(..., &mixer->flush_work, ...)
                                        mutex_unlock(&mixer->lock)
  snd_ctl_remove()
  kfree(mixer)
                                      snd_usb_mixer_flush_work()
                                        mutex_lock(&mixer->lock)  <-- use-after-free

Checking cval->head.mixer->disconnected under mixer->lock in
mixer_ctl_feature_put() (or before arming flush_work) would uphold the
teardown invariant.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117124242.12237-1-a@pavel.bz?part=2

      reply	other threads:[~2026-11-17 13:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 12:42 [RFC PATCH v4 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 12:42 ` [RFC PATCH v4 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it Pavel Artemev
2026-11-17 12:42 ` [RFC PATCH v4 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 13:13   ` 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-218847@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a@pavel.bz \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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