ALSA: Fix some bugs reported by Sashiko
Subsystems (2)
v3 cover ALSA: Fix some bugs reported by Sashiko
Hi,
here is the revised patch series of the previous fix series. Now Sashiko reports the existing bugs, I took a look, grabbed a few critical ones, and here are the fix patches. About a half are about core stuff and a few about USB-audio mixers.
Takashi
v1: https://lore.kernel.org/[email protected]
v2: https://lore.kernel.org/[email protected]
v1->v2: fix mutex() scope in USB mixer mutex in patch 5.v2->v3: covering selector unit with mutex, destroy the mixer mutex in patch 5
fix MAX_CHANNELS check in patch 7
===
Takashi Iwai (8):
ALSA: seq: Drop the bogus RCU guard from clientptr()
ALSA: pcm: Fix TOCTOU state overwrite in snd_pcm_drop()
ALSA: hda: Fix potential UAF for gating jack
ALSA: usb-audio: Fix invalid UAC2/3 mixer unit matrix evaluation
ALSA: usb-audio: Fix data race at mixer_ctl_feature_info()
ALSA: pcmtest: Fix a bogus pointer read in snd_pcmtst_pcm_pointer()
ALSA: usb-audio: Fix mixer bitmap cache over 32 channels
ALSA: core: Add missing barriers for power_ref vs card->shutdown
sound/core/init.c | 4 + sound/core/pcm_native.c | 2 +- sound/core/seq/seq_clientmgr.c | 21 ++--- sound/drivers/pcmtest.c | 3 +- sound/hda/common/jack.c | 9 +- sound/usb/mixer.c | 146 +++++++++++++++++++++++---------- sound/usb/mixer.h | 3 +- sound/usb/mixer_scarlett.c | 18 ++-- sound/usb/mixer_us16x08.c | 20 ++--- 9 files changed, 143 insertions(+), 83 deletions(-)
Signature · 1 line
2.55.0
PATCH v3 1/8 ALSA: seq: Drop the bogus RCU guard from clientptr()
In the recent refactoring with RCU, clientptr() takes guard(rcu)() around the client table lookup, but the RCU read-side section ends as soon as the function returns, so the returned pointer isn't protected at all. This gives a false impression as if that the callers were safe, and confuse reviewers including Sashiko.
Actually, the callers (snd_seq_delete_kernel_client(), snd_seq_kernel_client_ctl() and snd_seq_kernel_client_write_poll()) never relied on any lock; the caller is the owner of the client (a kernel client passing its own id, or a user client via its opened file), hence the client can't be released concurrently by others.
So just drop the confusing and useless RCU guard, read the table via rcu_dereference_protected(), and document the lifetime rule. Along with it, fold __clientptr() into its only user client_use_ptr(); the id range is already checked there, and it's never called with clients_lock held, so a plain rcu_dereference() suffices.
--- sound/core/seq/seq_clientmgr.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-)
sound/core/seq/seq_clientmgr.c +8 -13
@@ -95,23 +95,18 @@ static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)return snd_seq_total_cells (client->pool) >0 ; } -/* return pointer to client structure for specified id; call under RCU read-lock */ -static struct snd_seq_client *__clientptr (int clientid) +/* return pointer to client structure for specified id; +* the caller must guarantee the client's lifetime by itself, as neither RCU +* nor a use_lock reference is taken here. +*/ +static struct snd_seq_client *clientptr (int clientid) {if (clientid <0 || clientid >=SNDRV_SEQ_MAX_CLIENTS ) {pr_debug ("ALSA: seq: oops. Trying to get pointer to client %d\n" , clientid);return NULL ; } -return rcu_dereference_check (clienttab[clientid], -lockdep_is_held (&clients_lock)); -} - -/* return pointer to client structure for specified id */ -static struct snd_seq_client *clientptr (int clientid) -{ -guard (rcu)(); -return __clientptr (clientid); +return rcu_dereference_protected (clienttab[clientid], true); }static struct snd_seq_client *client_use_ptr (int clientid,bool load_module)
@@ -124,7 +119,7 @@ static struct snd_seq_client *client_use_ptr(int clientid, bool load_module)return NULL ; }scoped_guard (rcu) { - client =__clientptr (clientid); + client =rcu_dereference (clienttab[clientid]);if (client)return snd_seq_client_ref (client);if (clienttablock[clientid])
@@ -159,7 +154,7 @@ static struct snd_seq_client *client_use_ptr(int clientid, bool load_module) } }scoped_guard (rcu) { - client =__clientptr (clientid); + client =rcu_dereference (clienttab[clientid]);if (client)return snd_seq_client_ref (client); }
Signature · 1 line
2.55.0
PATCH v3 2/8 ALSA: pcm: Fix TOCTOU state overwrite in snd_pcm_drop()
In reply to Takashi Iwaisnd_pcm_drop() checks the current state at the beginning, and bails out if it's in an invalid state (OPEN or DISCONNECTED). However, since the check is done before the PCM stream lock, this can lead to a Time-of-Check to Time-of-Use (TOCTOU) race against the other forcible state change like the device disconnection like below:
CPU 0 CPU 1
----- -----
snd_pcm_drop()
runtime->state check
snd_pcm_dev_disconnect()
guard(pcm_stream_lock_irq)
runtime->state = SNDRV_PCM_STATE_DISCONNECTED
guard(pcm_stream_lock_irq)
snd_pcm_stop(SNDRV_PCM_STATE_SETUP) <== inconsistent stateFor avoiding the inconsistent state change, this patch moves the runtime state check inside the stream lock guard.
--- sound/core/pcm_native.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sound/core/pcm_native.c +1 -1
@@ -2289,11 +2289,11 @@ static int snd_pcm_drop(struct snd_pcm_substream *substream)return -ENXIO ; runtime = substream->runtime; +guard (pcm_stream_lock_irq)(substream);if (runtime->state ==SNDRV_PCM_STATE_OPEN || runtime->state ==SNDRV_PCM_STATE_DISCONNECTED )return -EBADFD ; -guard (pcm_stream_lock_irq)(substream);/* resume pause */ if (runtime->state ==SNDRV_PCM_STATE_PAUSED )snd_pcm_pause (substream, false);
Signature · 1 line
2.55.0
PATCH v3 3/8 ALSA: hda: Fix potential UAF for gating jack
In reply to Takashi Iwaisnd_hda_jack_set_gating_jack() assigns gated and gating variables from the succeeding calls of snd_hda_jack_tbl_get(). This works normally, but the second call might kick off the reallocation of the jack table, hence the first pointer might become invalid, which leads to a UAF.
For avoiding that pitfall, this patch changes the code to two phases:
the first to only create two entries, and the second phase to get the pointers from the table, so that no relocation can happen any longer.
--- sound/hda/common/jack.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
sound/hda/common/jack.c +6 -3
@@ -389,12 +389,15 @@ EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);int snd_hda_jack_set_gating_jack (struct hda_codec *codec,hda_nid_t gated_nid,hda_nid_t gating_nid) { -struct hda_jack_tbl *gated =snd_hda_jack_tbl_new (codec, gated_nid,0 ); -struct hda_jack_tbl *gating = -snd_hda_jack_tbl_new (codec, gating_nid,0 ); +struct hda_jack_tbl *gated, *gating;WARN_ON (codec->dp_mst); +if (!snd_hda_jack_tbl_new (codec, gated_nid,0 ) || + !snd_hda_jack_tbl_new (codec, gating_nid,0 )) +return -ENOMEM ; + gated =snd_hda_jack_tbl_get_mst (codec, gated_nid,0 ); + gating =snd_hda_jack_tbl_get_mst (codec, gating_nid,0 );if (!gated || !gating)return -EINVAL ;
Signature · 1 line
2.55.0
PATCH v3 4/8 ALSA: usb-audio: Fix invalid UAC2/3 mixer unit matrix evaluation
In reply to Takashi IwaiThe bitmap matrix in the mixer unit descriptor for UAC2 and UAC3 has rather the size of input-pins x output-pins, while the current USB-audio driver code wrongly assumes the UAC1 bitmap matrix size, which is input-channels x output-pins. That is, when input pins have multiple channels, the column size differs and it leads to the accesses at a wrong position.
This patch corrects the access of the bitmap matrix for UAC2/UAC3. For making the code cleaner, split the parser to UAC1 and UAC2/3, too.
--- sound/usb/mixer.c | 112 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 34 deletions(-)
sound/usb/mixer.c +78 -34
@@ -2377,7 +2377,7 @@ static bool mixer_bitmap_overflow(struct uac_mixer_unit_descriptor *desc,*/ static void build_mixer_unit_ctl (struct mixer_build *state,struct uac_mixer_unit_descriptor *desc, -int in_pin,int in_ch,int num_outs, +int in_ch,int num_outs,int unitid,struct usb_audio_term *iterm) {struct usb_mixer_elem_info *cval;
@@ -2472,34 +2472,13 @@ static int parse_audio_input_terminal(struct mixer_build *state, int unitid,/* * parse a mixer unit */ -static int parse_audio_mixer_unit (struct mixer_build *state,int unitid, -void *raw_desc) +/* UAC1 mixer unit */ +static int parse_audio_mixer_unit_v1 (struct mixer_build *state,int unitid, +int input_pins,int num_outs, +struct uac_mixer_unit_descriptor *desc) { -struct uac_mixer_unit_descriptor *desc = raw_desc;struct usb_audio_term iterm; -int input_pins, num_ins, num_outs; -int pin, ich, err; - - err =uac_mixer_unit_get_channels (state, desc); -if (err <0 ) { -usb_audio_err (state->chip, -"invalid MIXER UNIT descriptor %d\n" , - unitid); -return err; - } - - num_outs = err; - input_pins = desc->bNrInPins; - -if (state->mixer->protocol ==UAC_VERSION_2 || - state->mixer->protocol ==UAC_VERSION_3 ) { -if (input_pins * num_outs >256 ) { -usb_audio_err (state->chip, -"invalid channels for MIXER UNIT %d: input=%d, output=%d\n" , - unitid, input_pins, num_outs); -return -EINVAL ; - } - } +int num_ins, pin, ich, och, err; num_ins =0 ; ich =0 ;
@@ -2518,25 +2497,90 @@ static int parse_audio_mixer_unit(struct mixer_build *state, int unitid, num_ins, num_outs))break ;for (; ich < num_ins; ich++) { -int och, ich_has_controls =0 ; -for (och =0 ; och < num_outs; och++) {__u8 *c =uac_mixer_unit_bmControls (desc, state->mixer->protocol); -if (check_matrix_bitmap (c, ich, och, num_outs)) { - ich_has_controls =1 ; +if (check_matrix_bitmap (c, ich, och, num_outs))break ; - } } -if (ich_has_controls) -build_mixer_unit_ctl (state, desc, pin, ich, num_outs, +if (och < num_outs) +build_mixer_unit_ctl (state, desc, ich, num_outs, unitid, &iterm); } }return 0 ; } +/* UAC2/UAC3 mixer unit */ +static int parse_audio_mixer_unit_v2 (struct mixer_build *state,int unitid, +int input_pins,int num_outs, +struct uac_mixer_unit_descriptor *desc) +{ +struct usb_audio_term iterm; +int pin, och, err; + +if (input_pins * num_outs >256 || +mixer_bitmap_overflow (desc, state->mixer->protocol, + input_pins, num_outs)) { +usb_audio_err (state->chip, +"invalid channels for MIXER UNIT %d: input=%d, output=%d\n" , + unitid, input_pins, num_outs); +return -EINVAL ; + } + +for (pin =0 ; pin < input_pins; pin++) { + err =parse_audio_unit (state, desc->baSourceID[pin]); +if (err <0 ) +continue ; +if (!num_outs) +continue ; + err =check_input_term (state, desc->baSourceID[pin], &iterm); +if (err <0 ) +return err; + +for (och =0 ; och < num_outs; och++) { +__u8 *c =uac_mixer_unit_bmControls (desc, + state->mixer->protocol); + +if (check_matrix_bitmap (c, pin, och, num_outs)) +break ; + } +if (och < num_outs) +build_mixer_unit_ctl (state, desc, pin, num_outs, + unitid, &iterm); + } +return 0 ; +} + +static int parse_audio_mixer_unit (struct mixer_build *state,int unitid, +void *raw_desc) +{ +struct uac_mixer_unit_descriptor *desc = raw_desc; +int num_outs; + + num_outs =uac_mixer_unit_get_channels (state, desc); +if (num_outs <0 ) { +usb_audio_err (state->chip, +"invalid MIXER UNIT descriptor %d\n" , + unitid); +return num_outs; + } + +switch (state->mixer->protocol) { +case UAC_VERSION_1 : +default : +return parse_audio_mixer_unit_v1 (state, unitid, + desc->bNrInPins, num_outs, + desc); +case UAC_VERSION_2 : +case UAC_VERSION_3 : +return parse_audio_mixer_unit_v2 (state, unitid, + desc->bNrInPins, num_outs, + desc); + } +} +/* * Processing Unit / Extension Unit */
Signature · 1 line
2.55.0
PATCH v3 5/8 ALSA: usb-audio: Fix data race at mixer_ctl_feature_info()
In reply to Takashi IwaiThe info callback for USB-audio mixer controls for feature unit has a dynamic initialization of the contents with the check of cval->initialized flag. But, since the info callback may be concurrently called, this may lead to a data race, giving back an inconsistent state. Similarly, get and put callbacks may have concurrent accesses and can get bogus states.
For avoiding the data race, introduce a mutex locking for the controls and protect against concurrent info callback calls.
--- sound/usb/mixer.c | 14 +++++++++++++- sound/usb/mixer.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-)
sound/usb/mixer.c +13 -1
@@ -1531,6 +1531,7 @@ static int mixer_ctl_feature_info(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int ret; +guard (mutex)(&cval->head.mixer->lock);if (cval->val_type ==USB_MIXER_BOOLEAN || cval->val_type ==USB_MIXER_INV_BOOLEAN ) uinfo->type =SNDRV_CTL_ELEM_TYPE_BOOLEAN ;
@@ -1565,6 +1566,7 @@ static int mixer_ctl_feature_get(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int c, cnt, val, err; +guard (mutex)(&cval->head.mixer->lock); ucontrol->value.integer.value[0 ] = cval->min;if (cval->cmask) { cnt =0 ;
@@ -1595,10 +1597,12 @@ 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); -int max_val =get_max_exposed (cval); +int max_val;int c, cnt, val, oval, err;int changed =0 ; +guard (mutex)(&cval->head.mixer->lock); + max_val =get_max_exposed (cval);if (cval->cmask) { cnt =0 ;for (c =0 ; c <MAX_CHANNELS ; c++) {
@@ -1646,6 +1650,7 @@ static int mixer_ctl_master_bool_get(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int val, err; +guard (mutex)(&cval->head.mixer->lock); err =snd_usb_get_cur_mix_value (cval,0 ,0 , &val);if (err <0 )return filter_error (cval, err);
@@ -2592,6 +2597,7 @@ static int mixer_ctl_procunit_get(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int err, val; +guard (mutex)(&cval->head.mixer->lock); err =get_cur_ctl_value (cval, cval->control <<8 , &val);if (err <0 ) { ucontrol->value.integer.value[0 ] = cval->min;
@@ -2609,6 +2615,7 @@ static int mixer_ctl_procunit_put(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int val, oval, err; +guard (mutex)(&cval->head.mixer->lock); err =get_cur_ctl_value (cval, cval->control <<8 , &oval);if (err <0 )return filter_error (cval, err);
@@ -2960,6 +2967,7 @@ static int mixer_ctl_selector_get(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int val, err; +guard (mutex)(&cval->head.mixer->lock); err =get_cur_ctl_value (cval, cval->control <<8 , &val);if (err <0 ) { ucontrol->value.enumerated.item[0 ] =0 ;
@@ -2977,6 +2985,7 @@ static int mixer_ctl_selector_put(struct snd_kcontrol *kcontrol,struct usb_mixer_elem_info *cval =snd_kcontrol_chip (kcontrol);int val, oval, err; +guard (mutex)(&cval->head.mixer->lock); err =get_cur_ctl_value (cval, cval->control <<8 , &oval);if (err <0 )return filter_error (cval, err);
@@ -3249,6 +3258,7 @@ static void snd_usb_mixer_free(struct usb_mixer_interface *mixer) }usb_free_urb (mixer->rc_urb);kfree (mixer->rc_setup_packet); +mutex_destroy (&mixer->lock);kfree (mixer); }
@@ -3893,6 +3903,8 @@ int snd_usb_create_mixer(struct snd_usb_audio *chip, int ctrlif)return -ENOMEM ; } +mutex_init (&mixer->lock); + mixer->hostif = &usb_ifnum_to_if (chip->dev, ctrlif)->altsetting[0 ];switch (get_iface_desc (mixer->hostif)->bInterfaceProtocol) {case UAC_VERSION_1 :
sound/usb/mixer.h +1 -0
@@ -18,6 +18,7 @@ struct usb_mixer_interface {struct usb_host_interface *hostif;struct list_head list;unsigned int ignore_ctl_error; +struct mutex lock;/* lock for feature unit callbacks */ /* UAC2 status interrupt endpoint; owned by mixer.c */ struct urb *urb;/* array[MAX_ID_ELEMS], indexed by unit id */
Signature · 1 line
2.55.0
PATCH v3 6/8 ALSA: pcmtest: Fix a bogus pointer read in snd_pcmtst_pcm_pointer()
In reply to Takashi IwaiSashiko reported a potential bogus value for a pcmtest driver when a concurrent call to PCM pointer is invoked while the pcmtest's timer callback is running: since the position is updated in the timer callback without locking, the following wrapping in inc_buf_pos() might be screwed up:
if (v_iter->buf_pos >= bytes) v_iter->buf_pos %= bytes;
Although it was reported as an OOB, the actual return is corrected inside buffer_size, so no corruption is expected in this scenario, but an error message could show a bogus value.
Also, the whole state is read and modified locklessly in the timer callback, which can be racy against the pause operation, too.
For avoiding those races, simply put the PCM stream lock in the timer callback (while the snd_pcm_period_elapsed() must be changed to its *_under_stream_lock() variant for avoiding the deadlock).
--- sound/drivers/pcmtest.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
sound/drivers/pcmtest.c +2 -1
@@ -345,6 +345,7 @@ static void timer_timeout(struct timer_list *data) v_iter =timer_container_of (v_iter, data, timer_instance); substream = v_iter->substream; +guard (pcm_stream_lock_irqsave)(substream);if (v_iter->suspend)return ;
@@ -358,7 +359,7 @@ static void timer_timeout(struct timer_list *data) v_iter->period_pos += v_iter->b_rw;if (v_iter->period_pos >= v_iter->period_bytes) { v_iter->period_pos %= v_iter->period_bytes; -snd_pcm_period_elapsed (substream); +snd_pcm_period_elapsed_under_stream_lock (substream); }if (!v_iter->suspend)
Signature · 1 line
2.55.0
PATCH v3 7/8 ALSA: usb-audio: Fix mixer bitmap cache over 32 channels
In reply to Takashi IwaiUSB-audio driver keeps the bitmap for the cached mixer channels, but since a 32bit integer is used, it's currently broken for over 32 channels. As the driver is supposed to support up to 64 channels, this patch extends the bitmap properly -- now to be more flexible, use the standard bitmap instead of the manual bit shifts.
Some checks for master channels are replaced in a slightly different manner (checking the channel index 0) instead of the full cval->cached check, so that it fits better in the bitmap helper usage.
--- sound/usb/mixer.c | 20 ++++++++++---------- sound/usb/mixer.h | 2 +- sound/usb/mixer_scarlett.c | 18 +++++++++--------- sound/usb/mixer_us16x08.c | 20 ++++++++++---------- 4 files changed, 30 insertions(+), 30 deletions(-)
sound/usb/mixer.c +10 -10
@@ -423,7 +423,7 @@ int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval, {int err; -if (cval->cached &BIT (channel)) { +if (test_bit (channel, cval->cached)) { *value = cval->cache_val[index];return 0 ; }
@@ -440,7 +440,7 @@ int snd_usb_get_cur_mix_value(struct usb_mixer_elem_info *cval, cval->control, channel, err);return err; } - cval->cached |=BIT (channel); +set_bit (channel, cval->cached); cval->cache_val[index] = *value;return 0 ; }
@@ -604,7 +604,7 @@ int snd_usb_set_cur_mix_value(struct usb_mixer_elem_info *cval, int channel, value);if (err <0 )return err; - cval->cached |=BIT (channel); +set_bit (channel, cval->cached); cval->cache_val[index] = value;return 0 ; }
@@ -1449,7 +1449,7 @@ static int get_min_max_with_quirks(struct usb_mixer_elem_info *cval,* properly. */ if (ret) - cval->cached =0 ; +bitmap_zero (cval->cached,MAX_CHANNELS +1 ); cval->initialized =1 ; }
@@ -3669,7 +3669,7 @@ void snd_usb_mixer_notify_id(struct usb_mixer_interface *mixer, int unitid) info =mixer_elem_list_to_info (list);/* invalidate cache, so the value is read from the device */ if (!info->get_cur_broken) - info->cached =0 ; +bitmap_zero (info->cached,MAX_CHANNELS +1 );snd_ctl_notify (mixer->chip->card,SNDRV_CTL_EVENT_MASK_VALUE , &list->kctl->id); }
@@ -3736,7 +3736,7 @@ static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,__u8 channel = value &0xff ;unsigned int count =0 ; -if (channel >=MAX_CHANNELS ) { +if (channel >MAX_CHANNELS ) {usb_audio_dbg (mixer->chip,"%s(): bogus channel number %d\n" , __func__, channel);
@@ -3768,9 +3768,9 @@ static void snd_usb_mixer_interrupt_v2(struct usb_mixer_interface *mixer,/* invalidate cache, so the value is read from the device */ if (!info->get_cur_broken) {if (channel) - info->cached &= ~BIT (channel); +clear_bit (channel, info->cached);else /* master channel */ - info->cached =0 ; +bitmap_zero (info->cached,MAX_CHANNELS +1 ); } snd_ctl_notify(mixer->chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
@@ -4008,7 +4008,7 @@ static int restore_mixer_value(struct usb_mixer_elem_list *list)for (c =0 ; c <MAX_CHANNELS ; c++) {if (!(cval->cmask &BIT (c)))continue ; -if (cval->cached &BIT (c +1 )) { +if (test_bit (c +1 , cval->cached)) { err =snd_usb_set_cur_mix_value (cval, c +1 , idx, cval->cache_val[idx]);if (err <0 )
@@ -4018,7 +4018,7 @@ static int restore_mixer_value(struct usb_mixer_elem_list *list) } }else {/* master */ -if (cval->cached) +if (test_bit (0 , cval->cached))snd_usb_set_cur_mix_value (cval,0 ,0 , *cval->cache_val); }
sound/usb/mixer.h +1 -1
@@ -93,7 +93,7 @@ struct usb_mixer_elem_info {int min, max, res;int max_exposed;/* control API exposes the value in 0..max_exposed */ int dBmin, dBmax; -int cached; +DECLARE_BITMAP (cached,MAX_CHANNELS +1 );int cache_val[MAX_CHANNELS ];u8 initialized;u8 min_mute;
sound/usb/mixer_scarlett.c +9 -9
@@ -292,7 +292,7 @@ static int forte_get_ctl_value(struct usb_mixer_elem_info *elem, int *value)/* Device may not support reading input controls. * Return cached value or default to avoid blocking module load. */ -if (elem->cached) +if (test_bit (0 , elem->cached)) *value = elem->cache_val[0 ];else *value =0 ;/* Default: first option */
@@ -353,7 +353,7 @@ static int forte_input_gain_put(struct snd_kcontrol *kctl, err =forte_set_ctl_value (elem, val);if (err <0 )return err; - elem->cached |=1 ; +set_bit (0 , elem->cached); elem->cache_val[0 ] = val;return 1 ; }
@@ -364,7 +364,7 @@ static int forte_input_gain_resume(struct usb_mixer_elem_list *list) {struct usb_mixer_elem_info *elem =mixer_elem_list_to_info (list); -if (elem->cached) +if (test_bit (0 , elem->cached))forte_set_ctl_value (elem, *elem->cache_val);return 0 ; }
@@ -405,7 +405,7 @@ static int forte_ctl_enum_put(struct snd_kcontrol *kctl, err =forte_set_ctl_value (elem, val);if (err <0 )return err; - elem->cached |=1 ; +set_bit (0 , elem->cached); elem->cache_val[0 ] = val;return 1 ; }
@@ -416,7 +416,7 @@ static int forte_ctl_enum_resume(struct usb_mixer_elem_list *list) {struct usb_mixer_elem_info *elem =mixer_elem_list_to_info (list); -if (elem->cached) +if (test_bit (0 , elem->cached))forte_set_ctl_value (elem, *elem->cache_val);return 0 ; }
@@ -454,7 +454,7 @@ static int forte_ctl_switch_put(struct snd_kcontrol *kctl, err =forte_set_ctl_value (elem, val);if (err <0 )return err; - elem->cached |=1 ; +set_bit (0 , elem->cached); elem->cache_val[0 ] = val;return 1 ; }
@@ -465,7 +465,7 @@ static int forte_ctl_switch_resume(struct usb_mixer_elem_list *list) {struct usb_mixer_elem_info *elem =mixer_elem_list_to_info (list); -if (elem->cached) +if (test_bit (0 , elem->cached))forte_set_ctl_value (elem, *elem->cache_val);return 0 ; }
@@ -532,7 +532,7 @@ static int scarlett_ctl_resume(struct usb_mixer_elem_list *list)int i;for (i =0 ; i < elem->channels; i++) -if (elem->cached & (1 << i)) +if (test_bit (i, elem->cached))snd_usb_set_cur_mix_value (elem, i, i, elem->cache_val[i]);return 0 ;
@@ -692,7 +692,7 @@ static int scarlett_ctl_enum_resume(struct usb_mixer_elem_list *list) {struct usb_mixer_elem_info *elem =mixer_elem_list_to_info (list); -if (elem->cached) +if (test_bit (0 , elem->cached))snd_usb_set_cur_mix_value (elem,0 ,0 , *elem->cache_val);return 0 ; }
sound/usb/mixer_us16x08.c +10 -10
@@ -236,7 +236,7 @@ static int snd_us16x08_route_put(struct snd_kcontrol *kcontrol,return err; } - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -295,7 +295,7 @@ static int snd_us16x08_master_put(struct snd_kcontrol *kcontrol,return err; } - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -336,7 +336,7 @@ static int snd_us16x08_bus_put(struct snd_kcontrol *kcontrol,return err; } - elem->cached |=1 ; +set_bit (0 , elem->cached); elem->cache_val[0 ] = val;return 1 ; }
@@ -404,7 +404,7 @@ static int snd_us16x08_channel_put(struct snd_kcontrol *kcontrol,return err; } - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -508,7 +508,7 @@ static int snd_us16x08_comp_put(struct snd_kcontrol *kcontrol, } store->val[val_idx][index] = val; - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -567,7 +567,7 @@ static int snd_us16x08_eqswitch_put(struct snd_kcontrol *kcontrol,return err; } - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -631,7 +631,7 @@ static int snd_us16x08_eq_put(struct snd_kcontrol *kcontrol, store->val[b_idx][p_idx][index] = val;/* store new value in EQ band cache */ - elem->cached |=1 << index; +set_bit (index, elem->cached); elem->cache_val[index] = val;return 1 ; }
@@ -1352,7 +1352,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer) }for (i =0 ; i <8 ; i++) elem->cache_val[i] = i <2 ? i : i +2 ; - elem->cached =0xff ; +bitmap_set (elem->cached,0 ,8 );/* create compressor mixer elements */ comp_store =snd_us16x08_create_comp_store ();
@@ -1374,7 +1374,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer)if (err <0 )return err; elem->cache_val[0 ] = master_controls[i].default_val; - elem->cached =1 ; +set_bit (0 , elem->cached); }/* add channel controls */
@@ -1394,7 +1394,7 @@ int snd_us16x08_controls_create(struct usb_mixer_interface *mixer) elem->cache_val[j] = channel_controls[i].default_val; } - elem->cached =0xffff ; +bitmap_set (elem->cached,0 ,SND_US16X08_MAX_CHANNELS ); }/* create eq store */
Signature · 1 line
2.55.0
PATCH v3 8/8 ALSA: core: Add missing barriers for power_ref vs card->shutdown
In reply to Takashi Iwaisnd_power_ref_and_wait() increments card->power_ref and then reads card->shutdown (and the power state) locklessly, while snd_card_disconnect() sets card->shutdown and then waits for power_ref to drop to zero via snd_power_sync_ref(). Due to the missing barrier on both sides, the caller proceeds with shutdown still seen as false, while the disconnect side sees a zero refcount and continues tearing down incorrectly.
For addressing the potential race, add smp_mb__after_atomic() after the refcount increment and smp_mb() before snd_power_sync_ref() in snd_card_disconnect().
--- sound/core/init.c | 4 ++++ 1 file changed, 4 insertions(+)
sound/core/init.c +4 -0
@@ -535,6 +535,8 @@ void snd_card_disconnect(struct snd_card *card)clear_bit (card->number, snd_cards_lock); } +/* order card->shutdown store against power_ref read */ +smp_mb ();snd_power_sync_ref (card); }EXPORT_SYMBOL (snd_card_disconnect);
@@ -1152,6 +1154,8 @@ EXPORT_SYMBOL(snd_card_file_remove);int snd_power_ref_and_wait (struct snd_card *card) {snd_power_ref (card); +/* order power_ref increment against card->shutdown read */ +smp_mb__after_atomic ();if (snd_power_get_state (card) !=SNDRV_CTL_POWER_D0 ) { wait_event_cmd(card->power_sleep, card->shutdown ||
Signature · 1 line
2.55.0