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: [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag
Date: Tue, 17 Nov 2026 15:04:37 +0000	[thread overview]
Message-ID: <sashiko-outbox-219014@kernel.org> (raw)
In-Reply-To: <20261117143314.41853-3-a@pavel.bz>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ALSA: usb-audio: stale master cache_val in
  mixer_ctl_feature_put()
- [Medium] ALSA: usb-audio: master_explicit stays pinned in
  mixer_ctl_feature_put()
--

commit 41831f8b3a2ed7b3056781d9884cd8cfd89e4001
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 coalesce
feature unit volume and mute writes into deferred per-unit 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/20261117124242.12237-3-a@pavel.bz

> diff --git a/sound/usb/mixer.c b/sound/usb/mixer.c
> index bf1d9fbf08aae..03d6fa3ab1d25 100644
> --- a/sound/usb/mixer.c
> +++ b/sound/usb/mixer.c

[ ... ]

> @@ -1608,18 +1618,369 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,

[ ... ]

> +static void snd_usb_mixer_flush_pending(struct usb_mixer_interface *mixer,
> +					int level)
> +{

[ ... ]

> +		/* master first (the Windows burst order).  Re-asserted
> +		 * even unchanged, unless the value is the control's raw
> +		 * maximum: that re-send would only trigger the in-headset
> +		 * limit notification, and the device sits at raw max
> +		 * after every reset anyway.
> +		 */
> +		if (master && !master_pending && !master->master_explicit &&
> +		    have_ch &&
> +		    (touched || level != FLUSH_TOUCHED) &&
> +		    (!test_bit(0, master->cached) ||
> +		     *master->cache_val != max_val ||
> +		     max_val != master->max)) {
> +			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);
> +		}

[ ... ]

> +static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
> +				 struct snd_ctl_elem_value *ucontrol)
> +{
>  	struct usb_mixer_elem_info *cval = snd_kcontrol_chip(kcontrol);
> +	bool deferred;
>  	int max_val;
>  	int c, cnt, val, oval, err;
>  	int changed = 0;
>  
> +	deferred = (cval->head.mixer->chip->quirk_flags &
> +		    QUIRK_FLAG_MIXER_STATE_REPUSH) &&
> +		   is_feature_volume_mute_ctl(cval);
> +
>  	guard(mutex)(&cval->head.mixer->lock);
>  	max_val = get_max_exposed(cval);
>  	if (cval->cmask) {
> +		bool ch_put = false;
> +
>  		cnt = 0;
>  		for (c = 0; c < MAX_CHANNELS; c++) {
>  			if (!(cval->cmask & BIT(c)))
> @@ -1632,14 +1993,54 @@ 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);
> +						mixer_arm_flush(cval->head.mixer);
> +						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 volume 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.  An unflushed explicit master value is
> +		 * dropped along with the ownership: the pending bit is
> +		 * cleared so the flush re-derives the master instead of
> +		 * writing the superseded value.
> +		 */
> +		if (ch_put && cval->control == UAC_FU_VOLUME) {
> +			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;
> +					clear_bit(0, m->pending);
> +				}
> +			}
> +		}

[Severity: Medium]
Can m->master_explicit stay pinned after alsactl restore?

parse_audio_feature_unit() builds the per-channel control before the
master control:

sound/usb/mixer.c:parse_audio_feature_unit() {
    ...
			if (ch_bits & 1)
				build_feature_ctl(state, _ftr, ch_bits, control,
						  &iterm, unitid, 0);
			if (master_bits & BIT(i))
				build_feature_ctl(state, _ftr, 0, control,
						  &iterm, unitid, 0);
    ...
}

Because the per-channel control gets index 0 (lower numid) and the master
control gets index 1 (higher numid), alsactl restore writes the channels
first and the master second, leaving m->master_explicit set.  If an audio
server then writes a per-channel volume that already matches
cval->cache_val (for example the value alsactl just restored, or
cval->max after QUIRK_FLAG_MIXER_INIT_VOLUME_MAX), oval == val on every
channel so ch_put remains false, m->master_explicit is never cleared, and
no flush is armed to re-derive the master volume.

>  	} else {
>  		/* master channel */
>  		err = snd_usb_get_cur_mix_value(cval, 0, 0, &oval);
> @@ -1650,9 +2051,24 @@ static int mixer_ctl_feature_put(struct snd_kcontrol *kcontrol,
>  			return -EINVAL;
>  		val = get_abs_value(cval, val);
>  		if (val != oval) {
> -			err = snd_usb_set_cur_mix_value(cval, 0, 0, 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
> +				 * masters keep the direct-path behaviour:
> +				 * no write, no cache update.
> +				 */
> +				if (!mixer_ch_readonly(cval, 0)) {
> +					set_bit(0, cval->cached);
> +					cval->cache_val[0] = val;
> +					set_bit(0, cval->pending);
> +					cval->master_explicit = true;
> +					mixer_arm_flush(cval->head.mixer);
> +				}
> +			} else {
> +				err = snd_usb_set_cur_mix_value(cval, 0, 0, val);
> +				if (err < 0)
> +					return filter_error(cval, err);
> +			}
>  			changed = 1;
>  		}
>  	}

[Severity: Medium]
When a per-channel write supersedes an unflushed master write by clearing
bit 0 of m->pending, does leaving bit 0 of m->cached set with the
unflushed m->cache_val[0] corrupt the master derivation check in
snd_usb_mixer_flush_pending()?

If the hardware master is below master->max, and user space writes
master->max to the master control followed by master->max to a channel
control before the flush runs, m->cache_val[0] is already updated to
master->max while m->pending is cleared.  snd_usb_mixer_flush_pending()
then sees test_bit(0, master->cached) and *master->cache_val == max_val ==
master->max, so it skips the master write on that flush and on all later
stream-open and resume re-pushes, leaving the hardware master volume stuck
at its old value.  Clearing bit 0 of m->cached whenever bit 0 of
m->pending is cleared on ownership handoff would force the next flush to
write the derived master value.

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

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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 14:33 [RFC PATCH v5 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 14:33 ` [PATCH 1/2] ALSA: usb-audio: drive XiiSound playback volume instead of skipping it Pavel Artemev
2026-11-17 14:33 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 15:04   ` sashiko-bot [this message]
  -- strict thread matches above, loose matches on Subject: below --
2026-11-17 18:07 [RFC PATCH v6 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 18:07 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 18:40   ` sashiko-bot
2026-11-17 19:46 [RFC PATCH v7 0/2] ALSA: usb-audio: working hardware volume for XiiSound headsets (040b:0897) Pavel Artemev
2026-11-17 19:46 ` [PATCH 2/2] ALSA: usb-audio: add MIXER_STATE_REPUSH quirk flag Pavel Artemev
2026-11-17 20:13   ` sashiko-bot

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-219014@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