* [PATCH 1/9] ALSA: ua101: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 2/9] ALSA: ua101: Use guard() for spin locks Takashi Iwai
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/misc/ua101.c | 85 +++++++++++++++++-------------------------
1 file changed, 34 insertions(+), 51 deletions(-)
diff --git a/sound/usb/misc/ua101.c b/sound/usb/misc/ua101.c
index e200e4af799c..1c4a266018bb 100644
--- a/sound/usb/misc/ua101.c
+++ b/sound/usb/misc/ua101.c
@@ -658,11 +658,10 @@ static int capture_pcm_open(struct snd_pcm_substream *substream)
DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second);
substream->runtime->delay = substream->runtime->hw.fifo_size;
- mutex_lock(&ua->mutex);
+ guard(mutex)(&ua->mutex);
err = start_usb_capture(ua);
if (err >= 0)
set_bit(ALSA_CAPTURE_OPEN, &ua->states);
- mutex_unlock(&ua->mutex);
return err;
}
@@ -679,31 +678,28 @@ static int playback_pcm_open(struct snd_pcm_substream *substream)
DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length,
ua->packets_per_second);
- mutex_lock(&ua->mutex);
+ guard(mutex)(&ua->mutex);
err = start_usb_capture(ua);
if (err < 0)
- goto error;
+ return err;
err = start_usb_playback(ua);
if (err < 0) {
if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
stop_usb_capture(ua);
- goto error;
+ return err;
}
set_bit(ALSA_PLAYBACK_OPEN, &ua->states);
-error:
- mutex_unlock(&ua->mutex);
- return err;
+ return 0;
}
static int capture_pcm_close(struct snd_pcm_substream *substream)
{
struct ua101 *ua = substream->private_data;
- mutex_lock(&ua->mutex);
+ guard(mutex)(&ua->mutex);
clear_bit(ALSA_CAPTURE_OPEN, &ua->states);
if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states))
stop_usb_capture(ua);
- mutex_unlock(&ua->mutex);
return 0;
}
@@ -711,12 +707,11 @@ static int playback_pcm_close(struct snd_pcm_substream *substream)
{
struct ua101 *ua = substream->private_data;
- mutex_lock(&ua->mutex);
+ guard(mutex)(&ua->mutex);
stop_usb_playback(ua);
clear_bit(ALSA_PLAYBACK_OPEN, &ua->states);
if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states))
stop_usb_capture(ua);
- mutex_unlock(&ua->mutex);
return 0;
}
@@ -724,12 +719,9 @@ static int capture_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
{
struct ua101 *ua = substream->private_data;
- int err;
- mutex_lock(&ua->mutex);
- err = start_usb_capture(ua);
- mutex_unlock(&ua->mutex);
- return err;
+ guard(mutex)(&ua->mutex);
+ return start_usb_capture(ua);
}
static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
@@ -738,11 +730,10 @@ static int playback_pcm_hw_params(struct snd_pcm_substream *substream,
struct ua101 *ua = substream->private_data;
int err;
- mutex_lock(&ua->mutex);
+ guard(mutex)(&ua->mutex);
err = start_usb_capture(ua);
if (err >= 0)
err = start_usb_playback(ua);
- mutex_unlock(&ua->mutex);
return err;
}
@@ -751,9 +742,9 @@ static int capture_pcm_prepare(struct snd_pcm_substream *substream)
struct ua101 *ua = substream->private_data;
int err;
- mutex_lock(&ua->mutex);
- err = start_usb_capture(ua);
- mutex_unlock(&ua->mutex);
+ scoped_guard(mutex, &ua->mutex) {
+ err = start_usb_capture(ua);
+ }
if (err < 0)
return err;
@@ -781,11 +772,11 @@ static int playback_pcm_prepare(struct snd_pcm_substream *substream)
struct ua101 *ua = substream->private_data;
int err;
- mutex_lock(&ua->mutex);
- err = start_usb_capture(ua);
- if (err >= 0)
- err = start_usb_playback(ua);
- mutex_unlock(&ua->mutex);
+ scoped_guard(mutex, &ua->mutex) {
+ err = start_usb_capture(ua);
+ if (err >= 0)
+ err = start_usb_playback(ua);
+ }
if (err < 0)
return err;
@@ -1127,18 +1118,18 @@ static void free_usb_related_resources(struct ua101 *ua,
unsigned int i;
struct usb_interface *intf;
- mutex_lock(&ua->mutex);
- free_stream_urbs(&ua->capture);
- free_stream_urbs(&ua->playback);
- mutex_unlock(&ua->mutex);
+ scoped_guard(mutex, &ua->mutex) {
+ free_stream_urbs(&ua->capture);
+ free_stream_urbs(&ua->playback);
+ }
free_stream_buffers(ua, &ua->capture);
free_stream_buffers(ua, &ua->playback);
for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) {
- mutex_lock(&ua->mutex);
- intf = ua->intf[i];
- ua->intf[i] = NULL;
- mutex_unlock(&ua->mutex);
+ scoped_guard(mutex, &ua->mutex) {
+ intf = ua->intf[i];
+ ua->intf[i] = NULL;
+ }
if (intf) {
usb_set_intfdata(intf, NULL);
if (intf != interface)
@@ -1192,22 +1183,18 @@ static int ua101_probe(struct usb_interface *interface,
intf_numbers[is_ua1000][0])
return -ENODEV;
- mutex_lock(&devices_mutex);
+ guard(mutex)(&devices_mutex);
for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
if (enable[card_index] && !(devices_used & (1 << card_index)))
break;
- if (card_index >= SNDRV_CARDS) {
- mutex_unlock(&devices_mutex);
+ if (card_index >= SNDRV_CARDS)
return -ENOENT;
- }
err = snd_card_new(&interface->dev,
index[card_index], id[card_index], THIS_MODULE,
sizeof(*ua), &card);
- if (err < 0) {
- mutex_unlock(&devices_mutex);
+ if (err < 0)
return err;
- }
card->private_free = ua101_card_free;
ua = card->private_data;
ua->dev = interface_to_usbdev(interface);
@@ -1290,13 +1277,11 @@ static int ua101_probe(struct usb_interface *interface,
usb_set_intfdata(interface, ua);
devices_used |= 1 << card_index;
- mutex_unlock(&devices_mutex);
return 0;
probe_error:
free_usb_related_resources(ua, interface);
snd_card_free(card);
- mutex_unlock(&devices_mutex);
return err;
}
@@ -1308,7 +1293,7 @@ static void ua101_disconnect(struct usb_interface *interface)
if (!ua)
return;
- mutex_lock(&devices_mutex);
+ guard(mutex)(&devices_mutex);
set_bit(DISCONNECTED, &ua->states);
wake_up(&ua->rate_feedback_wait);
@@ -1321,18 +1306,16 @@ static void ua101_disconnect(struct usb_interface *interface)
snd_usbmidi_disconnect(midi);
abort_alsa_playback(ua);
abort_alsa_capture(ua);
- mutex_lock(&ua->mutex);
- stop_usb_playback(ua);
- stop_usb_capture(ua);
- mutex_unlock(&ua->mutex);
+ scoped_guard(mutex, &ua->mutex) {
+ stop_usb_playback(ua);
+ stop_usb_capture(ua);
+ }
free_usb_related_resources(ua, interface);
devices_used &= ~(1 << ua->card_index);
snd_card_free_when_closed(ua->card);
-
- mutex_unlock(&devices_mutex);
}
static const struct usb_device_id ua101_ids[] = {
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 2/9] ALSA: ua101: Use guard() for spin locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
2025-08-29 15:07 ` [PATCH 1/9] ALSA: ua101: Use guard() for mutex locks Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 3/9] ALSA: usx2y: Use guard() for mutex locks Takashi Iwai
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Clean up the code using guard() for spin locks.
Merely code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/misc/ua101.c | 165 +++++++++++++++++++----------------------
1 file changed, 77 insertions(+), 88 deletions(-)
diff --git a/sound/usb/misc/ua101.c b/sound/usb/misc/ua101.c
index 1c4a266018bb..76b6eb55dcc2 100644
--- a/sound/usb/misc/ua101.c
+++ b/sound/usb/misc/ua101.c
@@ -171,7 +171,6 @@ static void playback_urb_complete(struct urb *usb_urb)
{
struct ua101_urb *urb = (struct ua101_urb *)usb_urb;
struct ua101 *ua = urb->urb.context;
- unsigned long flags;
if (unlikely(urb->urb.status == -ENOENT || /* unlinked */
urb->urb.status == -ENODEV || /* device removed */
@@ -184,14 +183,13 @@ static void playback_urb_complete(struct urb *usb_urb)
if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) {
/* append URB to FIFO */
- spin_lock_irqsave(&ua->lock, flags);
+ guard(spinlock_irqsave)(&ua->lock);
list_add_tail(&urb->ready_list, &ua->ready_playback_urbs);
if (ua->rate_feedback_count > 0)
queue_work(system_highpri_wq, &ua->playback_work);
ua->playback.substream->runtime->delay -=
urb->urb.iso_frame_desc[0].length /
ua->playback.frame_bytes;
- spin_unlock_irqrestore(&ua->lock, flags);
}
}
@@ -249,7 +247,6 @@ static inline void add_with_wraparound(struct ua101 *ua,
static void playback_work(struct work_struct *work)
{
struct ua101 *ua = container_of(work, struct ua101, playback_work);
- unsigned long flags;
unsigned int frames;
struct ua101_urb *urb;
bool do_period_elapsed = false;
@@ -269,43 +266,43 @@ static void playback_work(struct work_struct *work)
* submitting playback URBs is possible as long as both FIFOs are
* nonempty.
*/
- spin_lock_irqsave(&ua->lock, flags);
- while (ua->rate_feedback_count > 0 &&
- !list_empty(&ua->ready_playback_urbs)) {
- /* take packet size out of FIFO */
- frames = ua->rate_feedback[ua->rate_feedback_start];
- add_with_wraparound(ua, &ua->rate_feedback_start, 1);
- ua->rate_feedback_count--;
+ scoped_guard(spinlock_irqsave, &ua->lock) {
+ while (ua->rate_feedback_count > 0 &&
+ !list_empty(&ua->ready_playback_urbs)) {
+ /* take packet size out of FIFO */
+ frames = ua->rate_feedback[ua->rate_feedback_start];
+ add_with_wraparound(ua, &ua->rate_feedback_start, 1);
+ ua->rate_feedback_count--;
- /* take URB out of FIFO */
- urb = list_first_entry(&ua->ready_playback_urbs,
- struct ua101_urb, ready_list);
- list_del(&urb->ready_list);
+ /* take URB out of FIFO */
+ urb = list_first_entry(&ua->ready_playback_urbs,
+ struct ua101_urb, ready_list);
+ list_del(&urb->ready_list);
- /* fill packet with data or silence */
- urb->urb.iso_frame_desc[0].length =
- frames * ua->playback.frame_bytes;
- if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
- do_period_elapsed |= copy_playback_data(&ua->playback,
- &urb->urb,
- frames);
- else
- memset(urb->urb.transfer_buffer, 0,
- urb->urb.iso_frame_desc[0].length);
+ /* fill packet with data or silence */
+ urb->urb.iso_frame_desc[0].length =
+ frames * ua->playback.frame_bytes;
+ if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states))
+ do_period_elapsed |= copy_playback_data(&ua->playback,
+ &urb->urb,
+ frames);
+ else
+ memset(urb->urb.transfer_buffer, 0,
+ urb->urb.iso_frame_desc[0].length);
- /* and off you go ... */
- err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
- if (unlikely(err < 0)) {
- spin_unlock_irqrestore(&ua->lock, flags);
- abort_usb_playback(ua);
- abort_alsa_playback(ua);
- dev_err(&ua->dev->dev, "USB request error %d: %s\n",
- err, usb_error_string(err));
- return;
+ /* and off you go ... */
+ err = usb_submit_urb(&urb->urb, GFP_ATOMIC);
+ if (unlikely(err < 0)) {
+ abort_usb_playback(ua);
+ abort_alsa_playback(ua);
+ dev_err(&ua->dev->dev, "USB request error %d: %s\n",
+ err, usb_error_string(err));
+ return;
+ }
+ ua->playback.substream->runtime->delay += frames;
}
- ua->playback.substream->runtime->delay += frames;
}
- spin_unlock_irqrestore(&ua->lock, flags);
+
if (do_period_elapsed)
snd_pcm_period_elapsed(ua->playback.substream);
}
@@ -347,7 +344,6 @@ static void capture_urb_complete(struct urb *urb)
{
struct ua101 *ua = urb->context;
struct ua101_stream *stream = &ua->capture;
- unsigned long flags;
unsigned int frames, write_ptr;
bool do_period_elapsed;
int err;
@@ -364,47 +360,45 @@ static void capture_urb_complete(struct urb *urb)
else
frames = 0;
- spin_lock_irqsave(&ua->lock, flags);
+ scoped_guard(spinlock_irqsave, &ua->lock) {
- if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
- do_period_elapsed = copy_capture_data(stream, urb, frames);
- else
- do_period_elapsed = false;
+ if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states))
+ do_period_elapsed = copy_capture_data(stream, urb, frames);
+ else
+ do_period_elapsed = false;
- if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
- err = usb_submit_urb(urb, GFP_ATOMIC);
- if (unlikely(err < 0)) {
- spin_unlock_irqrestore(&ua->lock, flags);
- dev_err(&ua->dev->dev, "USB request error %d: %s\n",
- err, usb_error_string(err));
- goto stream_stopped;
+ if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) {
+ err = usb_submit_urb(urb, GFP_ATOMIC);
+ if (unlikely(err < 0)) {
+ dev_err(&ua->dev->dev, "USB request error %d: %s\n",
+ err, usb_error_string(err));
+ goto stream_stopped;
+ }
+
+ /* append packet size to FIFO */
+ write_ptr = ua->rate_feedback_start;
+ add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
+ ua->rate_feedback[write_ptr] = frames;
+ if (ua->rate_feedback_count < ua->playback.queue_length) {
+ ua->rate_feedback_count++;
+ if (ua->rate_feedback_count ==
+ ua->playback.queue_length)
+ wake_up(&ua->rate_feedback_wait);
+ } else {
+ /*
+ * Ring buffer overflow; this happens when the playback
+ * stream is not running. Throw away the oldest entry,
+ * so that the playback stream, when it starts, sees
+ * the most recent packet sizes.
+ */
+ add_with_wraparound(ua, &ua->rate_feedback_start, 1);
+ }
+ if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
+ !list_empty(&ua->ready_playback_urbs))
+ queue_work(system_highpri_wq, &ua->playback_work);
}
-
- /* append packet size to FIFO */
- write_ptr = ua->rate_feedback_start;
- add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count);
- ua->rate_feedback[write_ptr] = frames;
- if (ua->rate_feedback_count < ua->playback.queue_length) {
- ua->rate_feedback_count++;
- if (ua->rate_feedback_count ==
- ua->playback.queue_length)
- wake_up(&ua->rate_feedback_wait);
- } else {
- /*
- * Ring buffer overflow; this happens when the playback
- * stream is not running. Throw away the oldest entry,
- * so that the playback stream, when it starts, sees
- * the most recent packet sizes.
- */
- add_with_wraparound(ua, &ua->rate_feedback_start, 1);
- }
- if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) &&
- !list_empty(&ua->ready_playback_urbs))
- queue_work(system_highpri_wq, &ua->playback_work);
}
- spin_unlock_irqrestore(&ua->lock, flags);
-
if (do_period_elapsed)
snd_pcm_period_elapsed(stream->substream);
@@ -558,9 +552,9 @@ static int start_usb_playback(struct ua101 *ua)
clear_bit(PLAYBACK_URB_COMPLETED, &ua->states);
ua->playback.urbs[0]->urb.complete =
first_playback_urb_complete;
- spin_lock_irq(&ua->lock);
- INIT_LIST_HEAD(&ua->ready_playback_urbs);
- spin_unlock_irq(&ua->lock);
+ scoped_guard(spinlock_irq, &ua->lock) {
+ INIT_LIST_HEAD(&ua->ready_playback_urbs);
+ }
/*
* We submit the initial URBs all at once, so we have to wait for the
@@ -581,11 +575,11 @@ static int start_usb_playback(struct ua101 *ua)
for (i = 0; i < ua->playback.queue_length; ++i) {
/* all initial URBs contain silence */
- spin_lock_irq(&ua->lock);
- frames = ua->rate_feedback[ua->rate_feedback_start];
- add_with_wraparound(ua, &ua->rate_feedback_start, 1);
- ua->rate_feedback_count--;
- spin_unlock_irq(&ua->lock);
+ scoped_guard(spinlock_irq, &ua->lock) {
+ frames = ua->rate_feedback[ua->rate_feedback_start];
+ add_with_wraparound(ua, &ua->rate_feedback_start, 1);
+ ua->rate_feedback_count--;
+ }
urb = &ua->playback.urbs[i]->urb;
urb->iso_frame_desc[0].length =
frames * ua->playback.frame_bytes;
@@ -834,13 +828,8 @@ static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua,
struct ua101_stream *stream)
{
- unsigned long flags;
- unsigned int pos;
-
- spin_lock_irqsave(&ua->lock, flags);
- pos = stream->buffer_pos;
- spin_unlock_irqrestore(&ua->lock, flags);
- return pos;
+ guard(spinlock_irqsave)(&ua->lock);
+ return stream->buffer_pos;
}
static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs)
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 3/9] ALSA: usx2y: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
2025-08-29 15:07 ` [PATCH 1/9] ALSA: ua101: Use guard() for mutex locks Takashi Iwai
2025-08-29 15:07 ` [PATCH 2/9] ALSA: ua101: Use guard() for spin locks Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 4/9] ALSA: hiface: " Takashi Iwai
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/usx2y/us122l.c | 44 +++++++++++----------------------
sound/usb/usx2y/usbusx2yaudio.c | 23 ++++++-----------
sound/usb/usx2y/usx2yhwdeppcm.c | 33 +++++++++----------------
3 files changed, 35 insertions(+), 65 deletions(-)
diff --git a/sound/usb/usx2y/us122l.c b/sound/usb/usx2y/us122l.c
index 8dbbefe3e730..011ea96e9779 100644
--- a/sound/usb/usx2y/us122l.c
+++ b/sound/usb/usx2y/us122l.c
@@ -97,10 +97,10 @@ static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
struct us122l *us122l = vmf->vma->vm_private_data;
struct usb_stream *s;
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
s = us122l->sk.s;
if (!s)
- goto unlock;
+ return VM_FAULT_SIGBUS;
offset = vmf->pgoff << PAGE_SHIFT;
if (offset < PAGE_ALIGN(s->read_size)) {
@@ -108,21 +108,17 @@ static vm_fault_t usb_stream_hwdep_vm_fault(struct vm_fault *vmf)
} else {
offset -= PAGE_ALIGN(s->read_size);
if (offset >= PAGE_ALIGN(s->write_size))
- goto unlock;
+ return VM_FAULT_SIGBUS;
vaddr = us122l->sk.write_page + offset;
}
page = virt_to_page(vaddr);
get_page(page);
- mutex_unlock(&us122l->mutex);
vmf->page = page;
return 0;
-unlock:
- mutex_unlock(&us122l->mutex);
- return VM_FAULT_SIGBUS;
}
@@ -163,12 +159,11 @@ static int usb_stream_hwdep_release(struct snd_hwdep *hw, struct file *file)
usb_autopm_put_interface(iface);
if (us122l->first == file)
us122l->first = NULL;
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
if (us122l->master == file)
us122l->master = us122l->slave;
us122l->slave = NULL;
- mutex_unlock(&us122l->mutex);
return 0;
}
@@ -179,23 +174,19 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
struct us122l *us122l = hw->private_data;
unsigned long offset;
struct usb_stream *s;
- int err = 0;
bool read;
offset = area->vm_pgoff << PAGE_SHIFT;
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
s = us122l->sk.s;
read = offset < s->read_size;
- if (read && area->vm_flags & VM_WRITE) {
- err = -EPERM;
- goto out;
- }
+ if (read && area->vm_flags & VM_WRITE)
+ return -EPERM;
/* if userspace tries to mmap beyond end of our buffer, fail */
if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
dev_warn(hw->card->dev, "%s: size %lu > %u\n", __func__,
size, read ? s->read_size : s->write_size);
- err = -EINVAL;
- goto out;
+ return -EINVAL;
}
area->vm_ops = &usb_stream_hwdep_vm_ops;
@@ -203,9 +194,7 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
if (!read)
vm_flags_set(area, VM_DONTEXPAND);
area->vm_private_data = us122l;
-out:
- mutex_unlock(&us122l->mutex);
- return err;
+ return 0;
}
static __poll_t usb_stream_hwdep_poll(struct snd_hwdep *hw,
@@ -361,7 +350,7 @@ static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
snd_power_wait(hw->card);
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
s = us122l->sk.s;
if (!us122l->master) {
us122l->master = file;
@@ -381,7 +370,6 @@ static int usb_stream_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
err = 1;
}
unlock:
- mutex_unlock(&us122l->mutex);
wake_up_all(&us122l->sk.sleep);
return err;
}
@@ -577,9 +565,9 @@ static void snd_us122l_disconnect(struct usb_interface *intf)
snd_card_disconnect(card);
us122l = US122L(card);
- mutex_lock(&us122l->mutex);
- us122l_stop(us122l);
- mutex_unlock(&us122l->mutex);
+ scoped_guard(mutex, &us122l->mutex) {
+ us122l_stop(us122l);
+ }
/* release the midi resources */
list_for_each(p, &us122l->midi_list) {
@@ -611,9 +599,8 @@ static int snd_us122l_suspend(struct usb_interface *intf, pm_message_t message)
list_for_each(p, &us122l->midi_list)
snd_usbmidi_input_stop(p);
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
usb_stream_stop(&us122l->sk);
- mutex_unlock(&us122l->mutex);
return 0;
}
@@ -633,7 +620,7 @@ static int snd_us122l_resume(struct usb_interface *intf)
if (!us122l)
return 0;
- mutex_lock(&us122l->mutex);
+ guard(mutex)(&us122l->mutex);
/* needed, doesn't restart without: */
if (us122l->is_us144) {
err = usb_set_interface(us122l->dev, 0, 1);
@@ -664,7 +651,6 @@ static int snd_us122l_resume(struct usb_interface *intf)
list_for_each(p, &us122l->midi_list)
snd_usbmidi_input_start(p);
unlock:
- mutex_unlock(&us122l->mutex);
snd_power_change_state(card, SNDRV_CTL_POWER_D0);
return err;
}
diff --git a/sound/usb/usx2y/usbusx2yaudio.c b/sound/usb/usx2y/usbusx2yaudio.c
index acca8bead82e..c7c7ec9c228b 100644
--- a/sound/usb/usx2y/usbusx2yaudio.c
+++ b/sound/usb/usx2y/usbusx2yaudio.c
@@ -751,7 +751,6 @@ static int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format)
static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
{
- int err = 0;
unsigned int rate = params_rate(hw_params);
snd_pcm_format_t format = params_format(hw_params);
struct snd_card *card = substream->pstr->pcm->card;
@@ -760,7 +759,7 @@ static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_substream *test_substream;
int i;
- mutex_lock(&usx2y(card)->pcm_mutex);
+ guard(mutex)(&usx2y(card)->pcm_mutex);
dev_dbg(&dev->dev->dev, "%s(%p, %p)\n", __func__, substream, hw_params);
/* all pcm substreams off one usx2y have to operate at the same
* rate & format
@@ -777,14 +776,11 @@ static int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
test_substream->runtime->format != format) ||
(test_substream->runtime->rate &&
test_substream->runtime->rate != rate)) {
- err = -EINVAL;
- goto error;
+ return -EINVAL;
}
}
- error:
- mutex_unlock(&usx2y(card)->pcm_mutex);
- return err;
+ return 0;
}
/*
@@ -796,7 +792,7 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
struct snd_usx2y_substream *subs = runtime->private_data;
struct snd_usx2y_substream *cap_subs, *playback_subs;
- mutex_lock(&subs->usx2y->pcm_mutex);
+ guard(mutex)(&subs->usx2y->pcm_mutex);
dev_dbg(&subs->usx2y->dev->dev, "%s(%p)\n", __func__, substream);
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
@@ -816,7 +812,6 @@ static int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
usx2y_urbs_release(subs);
}
}
- mutex_unlock(&subs->usx2y->pcm_mutex);
return 0;
}
@@ -835,7 +830,7 @@ static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
dev_dbg(&usx2y->dev->dev, "%s(%p)\n", __func__, substream);
- mutex_lock(&usx2y->pcm_mutex);
+ guard(mutex)(&usx2y->pcm_mutex);
usx2y_subs_prepare(subs);
// Start hardware streams
// SyncStream first....
@@ -843,25 +838,23 @@ static int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
if (usx2y->format != runtime->format) {
err = usx2y_format_set(usx2y, runtime->format);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
if (usx2y->rate != runtime->rate) {
err = usx2y_rate_set(usx2y, runtime->rate);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
dev_dbg(&usx2y->dev->dev, "%s: starting capture pipe for %s\n",
__func__, subs == capsubs ? "self" : "playpipe");
err = usx2y_urbs_start(capsubs);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED)
err = usx2y_urbs_start(subs);
- up_prepare_mutex:
- mutex_unlock(&usx2y->pcm_mutex);
return err;
}
diff --git a/sound/usb/usx2y/usx2yhwdeppcm.c b/sound/usb/usx2y/usx2yhwdeppcm.c
index 1b1496adb47e..7c90214485d9 100644
--- a/sound/usb/usx2y/usx2yhwdeppcm.c
+++ b/sound/usb/usx2y/usx2yhwdeppcm.c
@@ -365,7 +365,7 @@ static int snd_usx2y_usbpcm_hw_free(struct snd_pcm_substream *substream)
struct snd_usx2y_substream *playback_subs;
struct snd_usx2y_substream *cap_subs2;
- mutex_lock(&subs->usx2y->pcm_mutex);
+ guard(mutex)(&subs->usx2y->pcm_mutex);
dev_dbg(&subs->usx2y->dev->dev, "%s(%p)\n", __func__, substream);
cap_subs2 = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE + 2];
@@ -394,7 +394,6 @@ static int snd_usx2y_usbpcm_hw_free(struct snd_pcm_substream *substream)
usx2y_usbpcm_urbs_release(cap_subs2);
}
}
- mutex_unlock(&subs->usx2y->pcm_mutex);
return 0;
}
@@ -504,15 +503,13 @@ static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
dev_dbg(&usx2y->dev->dev, "snd_usx2y_pcm_prepare(%p)\n", substream);
- mutex_lock(&usx2y->pcm_mutex);
+ guard(mutex)(&usx2y->pcm_mutex);
if (!usx2y->hwdep_pcm_shm) {
usx2y->hwdep_pcm_shm = alloc_pages_exact(USX2Y_HWDEP_PCM_PAGES,
GFP_KERNEL);
- if (!usx2y->hwdep_pcm_shm) {
- err = -ENOMEM;
- goto up_prepare_mutex;
- }
+ if (!usx2y->hwdep_pcm_shm)
+ return -ENOMEM;
memset(usx2y->hwdep_pcm_shm, 0, USX2Y_HWDEP_PCM_PAGES);
}
@@ -523,19 +520,19 @@ static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
if (usx2y->format != runtime->format) {
err = usx2y_format_set(usx2y, runtime->format);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
if (usx2y->rate != runtime->rate) {
err = usx2y_rate_set(usx2y, runtime->rate);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
dev_dbg(&usx2y->dev->dev,
"starting capture pipe for %s\n", subs == capsubs ?
"self" : "playpipe");
err = usx2y_usbpcm_urbs_start(capsubs);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
if (subs != capsubs) {
@@ -547,14 +544,12 @@ static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
"Wait: iso_frames_per_buffer=%i,captured_iso_frames=%i\n",
usx2y_iso_frames_per_buffer(runtime, usx2y),
usx2y->hwdep_pcm_shm->captured_iso_frames);
- if (msleep_interruptible(10)) {
- err = -ERESTARTSYS;
- goto up_prepare_mutex;
- }
+ if (msleep_interruptible(10))
+ return -ERESTARTSYS;
}
err = usx2y_usbpcm_urbs_start(subs);
if (err < 0)
- goto up_prepare_mutex;
+ return err;
}
dev_dbg(&usx2y->dev->dev,
"Ready: iso_frames_per_buffer=%i,captured_iso_frames=%i\n",
@@ -564,8 +559,6 @@ static int snd_usx2y_usbpcm_prepare(struct snd_pcm_substream *substream)
usx2y->hwdep_pcm_shm->capture_iso_start = -1;
}
- up_prepare_mutex:
- mutex_unlock(&usx2y->pcm_mutex);
return err;
}
@@ -646,11 +639,10 @@ static int snd_usx2y_hwdep_pcm_open(struct snd_hwdep *hw, struct file *file)
struct snd_card *card = hw->card;
int err;
- mutex_lock(&usx2y(card)->pcm_mutex);
+ guard(mutex)(&usx2y(card)->pcm_mutex);
err = usx2y_pcms_busy_check(card);
if (!err)
usx2y(card)->chip_status |= USX2Y_STAT_CHIP_MMAP_PCM_URBS;
- mutex_unlock(&usx2y(card)->pcm_mutex);
return err;
}
@@ -659,11 +651,10 @@ static int snd_usx2y_hwdep_pcm_release(struct snd_hwdep *hw, struct file *file)
struct snd_card *card = hw->card;
int err;
- mutex_lock(&usx2y(card)->pcm_mutex);
+ guard(mutex)(&usx2y(card)->pcm_mutex);
err = usx2y_pcms_busy_check(card);
if (!err)
usx2y(hw->card)->chip_status &= ~USX2Y_STAT_CHIP_MMAP_PCM_URBS;
- mutex_unlock(&usx2y(card)->pcm_mutex);
return err;
}
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 4/9] ALSA: hiface: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (2 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 3/9] ALSA: usx2y: Use guard() for mutex locks Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 5/9] ALSA: hiface: Use guard() for spin locks Takashi Iwai
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/hiface/chip.c | 11 +++--------
sound/usb/hiface/pcm.c | 25 +++++++------------------
2 files changed, 10 insertions(+), 26 deletions(-)
diff --git a/sound/usb/hiface/chip.c b/sound/usb/hiface/chip.c
index 95385e90882c..bce28f683666 100644
--- a/sound/usb/hiface/chip.c
+++ b/sound/usb/hiface/chip.c
@@ -101,7 +101,7 @@ static int hiface_chip_probe(struct usb_interface *intf,
/* check whether the card is already registered */
chip = NULL;
- mutex_lock(®ister_mutex);
+ guard(mutex)(®ister_mutex);
for (i = 0; i < SNDRV_CARDS; i++)
if (enable[i])
@@ -109,13 +109,12 @@ static int hiface_chip_probe(struct usb_interface *intf,
if (i >= SNDRV_CARDS) {
dev_err(&device->dev, "no available " CARD_NAME " audio device\n");
- ret = -ENODEV;
- goto err;
+ return -ENODEV;
}
ret = hiface_chip_create(intf, device, i, quirk, &chip);
if (ret < 0)
- goto err;
+ return ret;
ret = hiface_pcm_init(chip, quirk ? quirk->extra_freq : 0);
if (ret < 0)
@@ -127,15 +126,11 @@ static int hiface_chip_probe(struct usb_interface *intf,
goto err_chip_destroy;
}
- mutex_unlock(®ister_mutex);
-
usb_set_intfdata(intf, chip);
return 0;
err_chip_destroy:
snd_card_free(chip->card);
-err:
- mutex_unlock(®ister_mutex);
return ret;
}
diff --git a/sound/usb/hiface/pcm.c b/sound/usb/hiface/pcm.c
index cf650fab54d7..f992e94feb64 100644
--- a/sound/usb/hiface/pcm.c
+++ b/sound/usb/hiface/pcm.c
@@ -356,7 +356,7 @@ static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
if (rt->panic)
return -EPIPE;
- mutex_lock(&rt->stream_mutex);
+ guard(mutex)(&rt->stream_mutex);
alsa_rt->hw = pcm_hw;
if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
@@ -364,7 +364,6 @@ static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
if (!sub) {
struct device *device = &rt->chip->dev->dev;
- mutex_unlock(&rt->stream_mutex);
dev_err(device, "Invalid stream type\n");
return -EINVAL;
}
@@ -377,15 +376,12 @@ static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
SNDRV_PCM_HW_PARAM_RATE,
&constraints_extra_rates);
- if (ret < 0) {
- mutex_unlock(&rt->stream_mutex);
+ if (ret < 0)
return ret;
- }
}
sub->instance = alsa_sub;
sub->active = false;
- mutex_unlock(&rt->stream_mutex);
return 0;
}
@@ -398,7 +394,7 @@ static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
if (rt->panic)
return 0;
- mutex_lock(&rt->stream_mutex);
+ guard(mutex)(&rt->stream_mutex);
if (sub) {
hiface_pcm_stream_stop(rt);
@@ -409,7 +405,6 @@ static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
spin_unlock_irqrestore(&sub->lock, flags);
}
- mutex_unlock(&rt->stream_mutex);
return 0;
}
@@ -425,7 +420,7 @@ static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
if (!sub)
return -ENODEV;
- mutex_lock(&rt->stream_mutex);
+ guard(mutex)(&rt->stream_mutex);
hiface_pcm_stream_stop(rt);
@@ -435,17 +430,12 @@ static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
if (rt->stream_state == STREAM_DISABLED) {
ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
- if (ret) {
- mutex_unlock(&rt->stream_mutex);
+ if (ret)
return ret;
- }
ret = hiface_pcm_stream_start(rt);
- if (ret) {
- mutex_unlock(&rt->stream_mutex);
+ if (ret)
return ret;
- }
}
- mutex_unlock(&rt->stream_mutex);
return 0;
}
@@ -532,9 +522,8 @@ void hiface_pcm_abort(struct hiface_chip *chip)
if (rt) {
rt->panic = true;
- mutex_lock(&rt->stream_mutex);
+ guard(mutex)(&rt->stream_mutex);
hiface_pcm_stream_stop(rt);
- mutex_unlock(&rt->stream_mutex);
}
}
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 5/9] ALSA: hiface: Use guard() for spin locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (3 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 4/9] ALSA: hiface: " Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 6/9] ALSA: line6: Use guard() for mutex locks Takashi Iwai
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Clean up the code using guard() for spin locks.
Merely code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/hiface/pcm.c | 35 ++++++++++++++---------------------
1 file changed, 14 insertions(+), 21 deletions(-)
diff --git a/sound/usb/hiface/pcm.c b/sound/usb/hiface/pcm.c
index f992e94feb64..27cd427fbaa5 100644
--- a/sound/usb/hiface/pcm.c
+++ b/sound/usb/hiface/pcm.c
@@ -305,7 +305,6 @@ static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
struct pcm_runtime *rt = out_urb->chip->pcm;
struct pcm_substream *sub;
bool do_period_elapsed = false;
- unsigned long flags;
int ret;
if (rt->panic || rt->stream_state == STREAM_STOPPING)
@@ -325,13 +324,12 @@ static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
/* now send our playback data (if a free out urb was found) */
sub = &rt->playback;
- spin_lock_irqsave(&sub->lock, flags);
- if (sub->active)
- do_period_elapsed = hiface_pcm_playback(sub, out_urb);
- else
- memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
-
- spin_unlock_irqrestore(&sub->lock, flags);
+ scoped_guard(spinlock_irqsave, &sub->lock) {
+ if (sub->active)
+ do_period_elapsed = hiface_pcm_playback(sub, out_urb);
+ else
+ memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
+ }
if (do_period_elapsed)
snd_pcm_period_elapsed(sub->instance);
@@ -389,7 +387,6 @@ static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
{
struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
- unsigned long flags;
if (rt->panic)
return 0;
@@ -399,11 +396,9 @@ static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
hiface_pcm_stream_stop(rt);
/* deactivate substream */
- spin_lock_irqsave(&sub->lock, flags);
+ guard(spinlock_irqsave)(&sub->lock);
sub->instance = NULL;
sub->active = false;
- spin_unlock_irqrestore(&sub->lock, flags);
-
}
return 0;
}
@@ -452,16 +447,16 @@ static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
- spin_lock_irq(&sub->lock);
- sub->active = true;
- spin_unlock_irq(&sub->lock);
+ scoped_guard(spinlock_irq, &sub->lock) {
+ sub->active = true;
+ }
return 0;
case SNDRV_PCM_TRIGGER_STOP:
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
- spin_lock_irq(&sub->lock);
- sub->active = false;
- spin_unlock_irq(&sub->lock);
+ scoped_guard(spinlock_irq, &sub->lock) {
+ sub->active = false;
+ }
return 0;
default:
@@ -473,15 +468,13 @@ static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
{
struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
- unsigned long flags;
snd_pcm_uframes_t dma_offset;
if (rt->panic || !sub)
return SNDRV_PCM_POS_XRUN;
- spin_lock_irqsave(&sub->lock, flags);
+ guard(spinlock_irqsave)(&sub->lock);
dma_offset = sub->dma_off;
- spin_unlock_irqrestore(&sub->lock, flags);
return bytes_to_frames(alsa_sub->runtime, dma_offset);
}
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 6/9] ALSA: line6: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (4 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 5/9] ALSA: hiface: Use guard() for spin locks Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 7/9] ALSA: usb: qcom: " Takashi Iwai
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification. The core code of line6_pcm_release() is factored out,
so that it can be covered by guard() nicely, too.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/line6/driver.c | 8 ++----
sound/usb/line6/pcm.c | 58 ++++++++++++++++++++--------------------
2 files changed, 31 insertions(+), 35 deletions(-)
diff --git a/sound/usb/line6/driver.c b/sound/usb/line6/driver.c
index f2f9261489a2..c505c1cba162 100644
--- a/sound/usb/line6/driver.c
+++ b/sound/usb/line6/driver.c
@@ -628,16 +628,12 @@ line6_hwdep_write(struct snd_hwdep *hwdep, const char __user *data, long count,
static __poll_t
line6_hwdep_poll(struct snd_hwdep *hwdep, struct file *file, poll_table *wait)
{
- __poll_t rv;
struct usb_line6 *line6 = hwdep->private_data;
poll_wait(file, &line6->messages.wait_queue, wait);
- mutex_lock(&line6->messages.read_lock);
- rv = kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
- mutex_unlock(&line6->messages.read_lock);
-
- return rv;
+ guard(mutex)(&line6->messages.read_lock);
+ return kfifo_len(&line6->messages.fifo) == 0 ? 0 : EPOLLIN | EPOLLRDNORM;
}
static const struct snd_hwdep_ops hwdep_ops = {
diff --git a/sound/usb/line6/pcm.c b/sound/usb/line6/pcm.c
index c1e2a8ab66fa..81e6d5e05135 100644
--- a/sound/usb/line6/pcm.c
+++ b/sound/usb/line6/pcm.c
@@ -295,6 +295,28 @@ snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
return pstr->pos_done;
}
+/* Stop and release duplex streams */
+static void __line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
+{
+ struct line6_pcm_stream *pstr;
+ int dir;
+
+ for (dir = 0; dir < 2; dir++)
+ line6_stream_stop(line6pcm, dir, type);
+ for (dir = 0; dir < 2; dir++) {
+ pstr = get_stream(line6pcm, dir);
+ line6_buffer_release(line6pcm, pstr, type);
+ }
+}
+
+/* Stop and release duplex streams */
+void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
+{
+ guard(mutex)(&line6pcm->state_mutex);
+ __line6_pcm_release(line6pcm, type);
+}
+EXPORT_SYMBOL_GPL(line6_pcm_release);
+
/* Acquire and optionally start duplex streams:
* type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
*/
@@ -304,7 +326,7 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
int ret = 0, dir;
/* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
- mutex_lock(&line6pcm->state_mutex);
+ guard(mutex)(&line6pcm->state_mutex);
for (dir = 0; dir < 2; dir++) {
pstr = get_stream(line6pcm, dir);
ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
@@ -321,30 +343,12 @@ int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
}
}
error:
- mutex_unlock(&line6pcm->state_mutex);
if (ret < 0)
- line6_pcm_release(line6pcm, type);
+ __line6_pcm_release(line6pcm, type);
return ret;
}
EXPORT_SYMBOL_GPL(line6_pcm_acquire);
-/* Stop and release duplex streams */
-void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
-{
- struct line6_pcm_stream *pstr;
- int dir;
-
- mutex_lock(&line6pcm->state_mutex);
- for (dir = 0; dir < 2; dir++)
- line6_stream_stop(line6pcm, dir, type);
- for (dir = 0; dir < 2; dir++) {
- pstr = get_stream(line6pcm, dir);
- line6_buffer_release(line6pcm, pstr, type);
- }
- mutex_unlock(&line6pcm->state_mutex);
-}
-EXPORT_SYMBOL_GPL(line6_pcm_release);
-
/* common PCM hw_params callback */
int snd_line6_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *hw_params)
@@ -353,16 +357,14 @@ int snd_line6_hw_params(struct snd_pcm_substream *substream,
struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
- mutex_lock(&line6pcm->state_mutex);
+ guard(mutex)(&line6pcm->state_mutex);
ret = line6_buffer_acquire(line6pcm, pstr, substream->stream,
LINE6_STREAM_PCM);
if (ret < 0)
- goto error;
+ return ret;
pstr->period = params_period_bytes(hw_params);
- error:
- mutex_unlock(&line6pcm->state_mutex);
- return ret;
+ return 0;
}
/* common PCM hw_free callback */
@@ -371,9 +373,8 @@ int snd_line6_hw_free(struct snd_pcm_substream *substream)
struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
- mutex_lock(&line6pcm->state_mutex);
+ guard(mutex)(&line6pcm->state_mutex);
line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
- mutex_unlock(&line6pcm->state_mutex);
return 0;
}
@@ -588,7 +589,7 @@ int snd_line6_prepare(struct snd_pcm_substream *substream)
struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
- mutex_lock(&line6pcm->state_mutex);
+ guard(mutex)(&line6pcm->state_mutex);
if (!pstr->running)
line6_wait_clear_audio_urbs(line6pcm, pstr);
@@ -602,6 +603,5 @@ int snd_line6_prepare(struct snd_pcm_substream *substream)
line6pcm->in.bytes = 0;
}
- mutex_unlock(&line6pcm->state_mutex);
return 0;
}
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 7/9] ALSA: usb: qcom: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (5 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 6/9] ALSA: line6: Use guard() for mutex locks Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 8/9] ALSA: bcd2000: " Takashi Iwai
2025-08-29 15:07 ` [PATCH 9/9] ALSA: caiaq: Use guard() for spin locks Takashi Iwai
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.
The manual mutex lock/unlock are still left in
handle_uaudio_stream_req() and its callee as they have a bit complex
locking patterns.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/qcom/qc_audio_offload.c | 69 ++++++++++---------------------
1 file changed, 22 insertions(+), 47 deletions(-)
diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c
index 965ddc792acc..a17fb63f605e 100644
--- a/sound/usb/qcom/qc_audio_offload.c
+++ b/sound/usb/qcom/qc_audio_offload.c
@@ -744,7 +744,7 @@ static void qmi_stop_session(void)
int if_idx;
int idx;
- mutex_lock(&qdev_mutex);
+ guard(mutex)(&qdev_mutex);
/* find all active intf for set alt 0 and cleanup usb audio dev */
for (idx = 0; idx < SNDRV_CARDS; idx++) {
if (!atomic_read(&uadev[idx].in_use))
@@ -780,11 +780,9 @@ static void qmi_stop_session(void)
disable_audio_stream(subs);
}
atomic_set(&uadev[idx].in_use, 0);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&chip->mutex);
uaudio_dev_cleanup(&uadev[idx]);
- mutex_unlock(&chip->mutex);
}
- mutex_unlock(&qdev_mutex);
}
/**
@@ -810,8 +808,8 @@ static int uaudio_sideband_notifier(struct usb_interface *intf,
chip = usb_get_intfdata(intf);
- mutex_lock(&qdev_mutex);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&qdev_mutex);
+ guard(mutex)(&chip->mutex);
dev = &uadev[chip->card->number];
@@ -825,9 +823,6 @@ static int uaudio_sideband_notifier(struct usb_interface *intf,
}
}
- mutex_unlock(&chip->mutex);
- mutex_unlock(&qdev_mutex);
-
return 0;
}
@@ -1577,17 +1572,15 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
goto response;
}
- mutex_lock(&chip->mutex);
- if (req_msg->enable) {
- if (info_idx < 0 || chip->system_suspend || subs->opened) {
- ret = -EBUSY;
- mutex_unlock(&chip->mutex);
-
- goto response;
+ scoped_guard(mutex, &chip->mutex) {
+ if (req_msg->enable) {
+ if (info_idx < 0 || chip->system_suspend || subs->opened) {
+ ret = -EBUSY;
+ goto response;
+ }
+ subs->opened = 1;
}
- subs->opened = 1;
}
- mutex_unlock(&chip->mutex);
if (req_msg->service_interval_valid) {
ret = get_data_interval_from_si(subs,
@@ -1610,9 +1603,8 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
ret = prepare_qmi_response(subs, req_msg, &resp,
info_idx);
if (ret < 0) {
- mutex_lock(&chip->mutex);
+ guard(mutex)(&chip->mutex);
subs->opened = 0;
- mutex_unlock(&chip->mutex);
}
} else {
info = &uadev[pcm_card_num].info[info_idx];
@@ -1643,14 +1635,13 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
}
disable_audio_stream(subs);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&chip->mutex);
subs->opened = 0;
- mutex_unlock(&chip->mutex);
}
response:
if (!req_msg->enable && ret != -EINVAL && ret != -ENODEV) {
- mutex_lock(&chip->mutex);
+ guard(mutex)(&chip->mutex);
if (info_idx >= 0) {
info = &uadev[pcm_card_num].info[info_idx];
uaudio_dev_intf_cleanup(uadev[pcm_card_num].udev,
@@ -1659,7 +1650,6 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle,
if (atomic_read(&uadev[pcm_card_num].in_use))
kref_put(&uadev[pcm_card_num].kref,
uaudio_dev_release);
- mutex_unlock(&chip->mutex);
}
mutex_unlock(&qdev_mutex);
@@ -1762,12 +1752,12 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
!usb_qmi_get_pcm_num(chip, 0))
return;
- mutex_lock(&qdev_mutex);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&qdev_mutex);
+ guard(mutex)(&chip->mutex);
if (!uadev[chip->card->number].chip) {
sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
if (!sdev)
- goto exit;
+ return;
sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR,
uaudio_sideband_notifier);
@@ -1806,9 +1796,6 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
snd_soc_usb_connect(uaudio_qdev->auxdev->dev.parent, sdev);
}
- mutex_unlock(&chip->mutex);
- mutex_unlock(&qdev_mutex);
-
return;
unreg_xhci:
@@ -1818,9 +1805,6 @@ static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip)
kfree(sdev);
uadev[chip->card->number].sdev = NULL;
uadev[chip->card->number].chip = NULL;
-exit:
- mutex_unlock(&chip->mutex);
- mutex_unlock(&qdev_mutex);
}
/**
@@ -1856,16 +1840,13 @@ static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip)
if (card_num >= SNDRV_CARDS)
return;
- mutex_lock(&qdev_mutex);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&qdev_mutex);
+ guard(mutex)(&chip->mutex);
dev = &uadev[card_num];
/* Device has already been cleaned up, or never populated */
- if (!dev->chip) {
- mutex_unlock(&chip->mutex);
- mutex_unlock(&qdev_mutex);
+ if (!dev->chip)
return;
- }
/* cleaned up already */
if (!dev->udev)
@@ -1886,9 +1867,6 @@ static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip)
kfree(dev->sdev);
dev->sdev = NULL;
}
- mutex_unlock(&chip->mutex);
-
- mutex_unlock(&qdev_mutex);
}
/**
@@ -1913,13 +1891,10 @@ static void qc_usb_audio_offload_suspend(struct usb_interface *intf,
if (card_num >= SNDRV_CARDS)
return;
- mutex_lock(&qdev_mutex);
- mutex_lock(&chip->mutex);
+ guard(mutex)(&qdev_mutex);
+ guard(mutex)(&chip->mutex);
uaudio_send_disconnect_ind(chip);
-
- mutex_unlock(&chip->mutex);
- mutex_unlock(&qdev_mutex);
}
static struct snd_usb_platform_ops offload_ops = {
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 8/9] ALSA: bcd2000: Use guard() for mutex locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (6 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 7/9] ALSA: usb: qcom: " Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
2025-08-29 15:07 ` [PATCH 9/9] ALSA: caiaq: Use guard() for spin locks Takashi Iwai
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Replace the manual mutex lock/unlock pairs with guard() for code
simplification.
Only code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/bcd2000/bcd2000.c | 16 ++++------------
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/sound/usb/bcd2000/bcd2000.c b/sound/usb/bcd2000/bcd2000.c
index 392b4d8e9e76..bebb48cb9abc 100644
--- a/sound/usb/bcd2000/bcd2000.c
+++ b/sound/usb/bcd2000/bcd2000.c
@@ -369,23 +369,19 @@ static int bcd2000_probe(struct usb_interface *interface,
char usb_path[32];
int err;
- mutex_lock(&devices_mutex);
+ guard(mutex)(&devices_mutex);
for (card_index = 0; card_index < SNDRV_CARDS; ++card_index)
if (!test_bit(card_index, devices_used))
break;
- if (card_index >= SNDRV_CARDS) {
- mutex_unlock(&devices_mutex);
+ if (card_index >= SNDRV_CARDS)
return -ENOENT;
- }
err = snd_card_new(&interface->dev, index[card_index], id[card_index],
THIS_MODULE, sizeof(*bcd2k), &card);
- if (err < 0) {
- mutex_unlock(&devices_mutex);
+ if (err < 0)
return err;
- }
bcd2k = card->private_data;
bcd2k->dev = interface_to_usbdev(interface);
@@ -413,14 +409,12 @@ static int bcd2000_probe(struct usb_interface *interface,
usb_set_intfdata(interface, bcd2k);
set_bit(card_index, devices_used);
- mutex_unlock(&devices_mutex);
return 0;
probe_error:
dev_info(&bcd2k->dev->dev, PREFIX "error during probing");
bcd2000_free_usb_related_resources(bcd2k, interface);
snd_card_free(card);
- mutex_unlock(&devices_mutex);
return err;
}
@@ -431,7 +425,7 @@ static void bcd2000_disconnect(struct usb_interface *interface)
if (!bcd2k)
return;
- mutex_lock(&devices_mutex);
+ guard(mutex)(&devices_mutex);
/* make sure that userspace cannot create new requests */
snd_card_disconnect(bcd2k->card);
@@ -441,8 +435,6 @@ static void bcd2000_disconnect(struct usb_interface *interface)
clear_bit(bcd2k->card_index, devices_used);
snd_card_free_when_closed(bcd2k->card);
-
- mutex_unlock(&devices_mutex);
}
static struct usb_driver bcd2000_driver = {
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH 9/9] ALSA: caiaq: Use guard() for spin locks
2025-08-29 15:07 [PATCH 0/9] ALSA: Use auto-cleanup macros for USB drivers Takashi Iwai
` (7 preceding siblings ...)
2025-08-29 15:07 ` [PATCH 8/9] ALSA: bcd2000: " Takashi Iwai
@ 2025-08-29 15:07 ` Takashi Iwai
8 siblings, 0 replies; 10+ messages in thread
From: Takashi Iwai @ 2025-08-29 15:07 UTC (permalink / raw)
To: linux-sound
Clean up the code using guard() for spin locks.
Merely code refactoring, and no behavior change.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
---
sound/usb/caiaq/audio.c | 39 +++++++++++++--------------------------
1 file changed, 13 insertions(+), 26 deletions(-)
diff --git a/sound/usb/caiaq/audio.c b/sound/usb/caiaq/audio.c
index 05f964347ed6..95d425dd9d70 100644
--- a/sound/usb/caiaq/audio.c
+++ b/sound/usb/caiaq/audio.c
@@ -51,29 +51,24 @@ static void
activate_substream(struct snd_usb_caiaqdev *cdev,
struct snd_pcm_substream *sub)
{
- spin_lock(&cdev->spinlock);
+ guard(spinlock)(&cdev->spinlock);
if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
cdev->sub_playback[sub->number] = sub;
else
cdev->sub_capture[sub->number] = sub;
-
- spin_unlock(&cdev->spinlock);
}
static void
deactivate_substream(struct snd_usb_caiaqdev *cdev,
struct snd_pcm_substream *sub)
{
- unsigned long flags;
- spin_lock_irqsave(&cdev->spinlock, flags);
+ guard(spinlock_irqsave)(&cdev->spinlock);
if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
cdev->sub_playback[sub->number] = NULL;
else
cdev->sub_capture[sub->number] = NULL;
-
- spin_unlock_irqrestore(&cdev->spinlock, flags);
}
static int
@@ -285,25 +280,18 @@ snd_usb_caiaq_pcm_pointer(struct snd_pcm_substream *sub)
{
int index = sub->number;
struct snd_usb_caiaqdev *cdev = snd_pcm_substream_chip(sub);
- snd_pcm_uframes_t ptr;
- spin_lock(&cdev->spinlock);
+ guard(spinlock)(&cdev->spinlock);
- if (cdev->input_panic || cdev->output_panic) {
- ptr = SNDRV_PCM_POS_XRUN;
- goto unlock;
- }
+ if (cdev->input_panic || cdev->output_panic)
+ return SNDRV_PCM_POS_XRUN;
if (sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
- ptr = bytes_to_frames(sub->runtime,
- cdev->audio_out_buf_pos[index]);
+ return bytes_to_frames(sub->runtime,
+ cdev->audio_out_buf_pos[index]);
else
- ptr = bytes_to_frames(sub->runtime,
- cdev->audio_in_buf_pos[index]);
-
-unlock:
- spin_unlock(&cdev->spinlock);
- return ptr;
+ return bytes_to_frames(sub->runtime,
+ cdev->audio_in_buf_pos[index]);
}
/* operators for both playback and capture */
@@ -601,7 +589,6 @@ static void read_completed(struct urb *urb)
struct device *dev;
struct urb *out = NULL;
int i, frame, len, send_it = 0, outframe = 0;
- unsigned long flags;
size_t offset = 0;
if (urb->status || !info)
@@ -638,10 +625,10 @@ static void read_completed(struct urb *urb)
offset += len;
if (len > 0) {
- spin_lock_irqsave(&cdev->spinlock, flags);
- fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
- read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
- spin_unlock_irqrestore(&cdev->spinlock, flags);
+ scoped_guard(spinlock_irqsave, &cdev->spinlock) {
+ fill_out_urb(cdev, out, &out->iso_frame_desc[outframe]);
+ read_in_urb(cdev, urb, &urb->iso_frame_desc[frame]);
+ }
check_for_elapsed_periods(cdev, cdev->sub_playback);
check_for_elapsed_periods(cdev, cdev->sub_capture);
send_it = 1;
--
2.50.1
^ permalink raw reply related [flat|nested] 10+ messages in thread