Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: hda/generic: Use guard() for mutex locks

Replace the manual mutex lock/unlock pairs with guard().

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250827072916.31933-17-tiwai@suse.de

+30 -39
+30 -39
sound/hda/codecs/generic.c
··· 1118 1118 unsigned long pval; 1119 1119 int err; 1120 1120 1121 - mutex_lock(&codec->control_mutex); 1121 + guard(mutex)(&codec->control_mutex); 1122 1122 pval = kcontrol->private_value; 1123 1123 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ 1124 1124 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); 1125 1125 kcontrol->private_value = pval; 1126 - mutex_unlock(&codec->control_mutex); 1127 1126 return err; 1128 1127 } 1129 1128 ··· 1135 1136 1136 1137 sync_auto_mute_bits(kcontrol, ucontrol); 1137 1138 1138 - mutex_lock(&codec->control_mutex); 1139 + guard(mutex)(&codec->control_mutex); 1139 1140 pval = kcontrol->private_value; 1140 1141 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; 1141 1142 for (i = 0; i < indices; i++) { ··· 1147 1148 change |= err; 1148 1149 } 1149 1150 kcontrol->private_value = pval; 1150 - mutex_unlock(&codec->control_mutex); 1151 1151 return err < 0 ? err : change; 1152 1152 } 1153 1153 ··· 2247 2249 unsigned int select = ucontrol->value.enumerated.item[0]; 2248 2250 int ret = 0; 2249 2251 2250 - mutex_lock(&spec->pcm_mutex); 2251 - if (spec->active_streams) { 2252 - ret = -EBUSY; 2253 - goto unlock; 2254 - } 2252 + guard(mutex)(&spec->pcm_mutex); 2253 + if (spec->active_streams) 2254 + return -EBUSY; 2255 2255 2256 2256 if (spec->indep_hp_enabled != select) { 2257 2257 hda_nid_t *dacp; ··· 2281 2285 call_hp_automute(codec, NULL); 2282 2286 ret = 1; 2283 2287 } 2284 - unlock: 2285 - mutex_unlock(&spec->pcm_mutex); 2286 2288 return ret; 2287 2289 } 2288 2290 ··· 3469 3475 3470 3476 imux = &spec->input_mux; 3471 3477 adc_idx = kcontrol->id.index; 3472 - mutex_lock(&codec->control_mutex); 3473 - for (i = 0; i < imux->num_items; i++) { 3474 - path = get_input_path(codec, adc_idx, i); 3475 - if (!path || !path->ctls[type]) 3476 - continue; 3477 - kcontrol->private_value = path->ctls[type]; 3478 - ret = func(kcontrol, ucontrol); 3479 - if (ret < 0) { 3480 - err = ret; 3481 - break; 3478 + scoped_guard(mutex, &codec->control_mutex) { 3479 + for (i = 0; i < imux->num_items; i++) { 3480 + path = get_input_path(codec, adc_idx, i); 3481 + if (!path || !path->ctls[type]) 3482 + continue; 3483 + kcontrol->private_value = path->ctls[type]; 3484 + ret = func(kcontrol, ucontrol); 3485 + if (ret < 0) 3486 + return ret; 3487 + if (ret > 0) 3488 + err = 1; 3482 3489 } 3483 - if (ret > 0) 3484 - err = 1; 3485 3490 } 3486 - mutex_unlock(&codec->control_mutex); 3487 - if (err >= 0 && spec->cap_sync_hook) 3491 + if (spec->cap_sync_hook) 3488 3492 spec->cap_sync_hook(codec, kcontrol, ucontrol); 3489 3493 return err; 3490 3494 } ··· 5324 5332 struct hda_gen_spec *spec = codec->spec; 5325 5333 int err; 5326 5334 5327 - mutex_lock(&spec->pcm_mutex); 5335 + guard(mutex)(&spec->pcm_mutex); 5328 5336 err = snd_hda_multi_out_analog_open(codec, 5329 5337 &spec->multiout, substream, 5330 5338 hinfo); 5331 - if (!err) { 5332 - spec->active_streams |= 1 << STREAM_MULTI_OUT; 5333 - call_pcm_playback_hook(hinfo, codec, substream, 5334 - HDA_GEN_PCM_ACT_OPEN); 5335 - } 5336 - mutex_unlock(&spec->pcm_mutex); 5337 - return err; 5339 + if (err < 0) 5340 + return err; 5341 + 5342 + spec->active_streams |= 1 << STREAM_MULTI_OUT; 5343 + call_pcm_playback_hook(hinfo, codec, substream, 5344 + HDA_GEN_PCM_ACT_OPEN); 5345 + return 0; 5338 5346 } 5339 5347 5340 5348 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo, ··· 5373 5381 struct snd_pcm_substream *substream) 5374 5382 { 5375 5383 struct hda_gen_spec *spec = codec->spec; 5376 - mutex_lock(&spec->pcm_mutex); 5384 + 5385 + guard(mutex)(&spec->pcm_mutex); 5377 5386 spec->active_streams &= ~(1 << STREAM_MULTI_OUT); 5378 5387 call_pcm_playback_hook(hinfo, codec, substream, 5379 5388 HDA_GEN_PCM_ACT_CLOSE); 5380 - mutex_unlock(&spec->pcm_mutex); 5381 5389 return 0; 5382 5390 } 5383 5391 ··· 5426 5434 struct hda_gen_spec *spec = codec->spec; 5427 5435 int err = 0; 5428 5436 5429 - mutex_lock(&spec->pcm_mutex); 5437 + guard(mutex)(&spec->pcm_mutex); 5430 5438 if (spec->indep_hp && !spec->indep_hp_enabled) 5431 5439 err = -EBUSY; 5432 5440 else 5433 5441 spec->active_streams |= 1 << STREAM_INDEP_HP; 5434 5442 call_pcm_playback_hook(hinfo, codec, substream, 5435 5443 HDA_GEN_PCM_ACT_OPEN); 5436 - mutex_unlock(&spec->pcm_mutex); 5437 5444 return err; 5438 5445 } 5439 5446 ··· 5441 5450 struct snd_pcm_substream *substream) 5442 5451 { 5443 5452 struct hda_gen_spec *spec = codec->spec; 5444 - mutex_lock(&spec->pcm_mutex); 5453 + 5454 + guard(mutex)(&spec->pcm_mutex); 5445 5455 spec->active_streams &= ~(1 << STREAM_INDEP_HP); 5446 5456 call_pcm_playback_hook(hinfo, codec, substream, 5447 5457 HDA_GEN_PCM_ACT_CLOSE); 5448 - mutex_unlock(&spec->pcm_mutex); 5449 5458 return 0; 5450 5459 } 5451 5460