ALSA: virtio: Use guard() for spin locks

Replace the manual spin lock/unlock pairs with guard() for code
simplification.

Only code refactoring, and no behavior change.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250829151335.7342-19-tiwai@suse.de
This commit is contained in:
Takashi Iwai
2025-08-29 17:13:32 +02:00
parent c07824a14d
commit 69f374931f
5 changed files with 38 additions and 74 deletions

View File

@@ -85,9 +85,8 @@ static void virtsnd_event_notify_cb(struct virtqueue *vqueue)
struct virtio_snd_queue *queue = virtsnd_event_queue(snd); struct virtio_snd_queue *queue = virtsnd_event_queue(snd);
struct virtio_snd_event *event; struct virtio_snd_event *event;
u32 length; u32 length;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
do { do {
virtqueue_disable_cb(vqueue); virtqueue_disable_cb(vqueue);
while ((event = virtqueue_get_buf(vqueue, &length))) { while ((event = virtqueue_get_buf(vqueue, &length))) {
@@ -95,7 +94,6 @@ static void virtsnd_event_notify_cb(struct virtqueue *vqueue)
virtsnd_event_send(vqueue, event, true, GFP_ATOMIC); virtsnd_event_send(vqueue, event, true, GFP_ATOMIC);
} }
} while (!virtqueue_enable_cb(vqueue)); } while (!virtqueue_enable_cb(vqueue));
spin_unlock_irqrestore(&queue->lock, flags);
} }
/** /**
@@ -176,14 +174,12 @@ static void virtsnd_disable_event_vq(struct virtio_snd *snd)
struct virtio_snd_queue *queue = virtsnd_event_queue(snd); struct virtio_snd_queue *queue = virtsnd_event_queue(snd);
struct virtio_snd_event *event; struct virtio_snd_event *event;
u32 length; u32 length;
unsigned long flags;
if (queue->vqueue) { if (queue->vqueue) {
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
virtqueue_disable_cb(queue->vqueue); virtqueue_disable_cb(queue->vqueue);
while ((event = virtqueue_get_buf(queue->vqueue, &length))) while ((event = virtqueue_get_buf(queue->vqueue, &length)))
virtsnd_event_dispatch(snd, event); virtsnd_event_dispatch(snd, event);
spin_unlock_irqrestore(&queue->lock, flags);
} }
} }

View File

@@ -131,7 +131,6 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
unsigned int nins = 0; unsigned int nins = 0;
struct scatterlist *psgs[4]; struct scatterlist *psgs[4];
bool notify = false; bool notify = false;
unsigned long flags;
int rc; int rc;
virtsnd_ctl_msg_ref(msg); virtsnd_ctl_msg_ref(msg);
@@ -147,15 +146,15 @@ int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
if (in_sgs) if (in_sgs)
psgs[nouts + nins++] = in_sgs; psgs[nouts + nins++] = in_sgs;
spin_lock_irqsave(&queue->lock, flags); scoped_guard(spinlock_irqsave, &queue->lock) {
rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg, rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
GFP_ATOMIC); GFP_ATOMIC);
if (!rc) { if (!rc) {
notify = virtqueue_kick_prepare(queue->vqueue); notify = virtqueue_kick_prepare(queue->vqueue);
list_add_tail(&msg->list, &snd->ctl_msgs); list_add_tail(&msg->list, &snd->ctl_msgs);
}
} }
spin_unlock_irqrestore(&queue->lock, flags);
if (rc) { if (rc) {
dev_err(&vdev->dev, "failed to send control message (0x%08x)\n", dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
@@ -233,9 +232,8 @@ void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd) void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
{ {
struct virtio_snd_queue *queue = virtsnd_control_queue(snd); struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
while (!list_empty(&snd->ctl_msgs)) { while (!list_empty(&snd->ctl_msgs)) {
struct virtio_snd_msg *msg = struct virtio_snd_msg *msg =
list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg, list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
@@ -243,7 +241,6 @@ void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
virtsnd_ctl_msg_complete(msg); virtsnd_ctl_msg_complete(msg);
} }
spin_unlock_irqrestore(&queue->lock, flags);
} }
/** /**
@@ -296,13 +293,11 @@ void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
struct virtio_snd_queue *queue = virtsnd_control_queue(snd); struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
struct virtio_snd_msg *msg; struct virtio_snd_msg *msg;
u32 length; u32 length;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
do { do {
virtqueue_disable_cb(vqueue); virtqueue_disable_cb(vqueue);
while ((msg = virtqueue_get_buf(vqueue, &length))) while ((msg = virtqueue_get_buf(vqueue, &length)))
virtsnd_ctl_msg_complete(msg); virtsnd_ctl_msg_complete(msg);
} while (!virtqueue_enable_cb(vqueue)); } while (!virtqueue_enable_cb(vqueue));
spin_unlock_irqrestore(&queue->lock, flags);
} }

View File

@@ -515,10 +515,10 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
/* TODO: deal with shmem elapsed period */ /* TODO: deal with shmem elapsed period */
break; break;
case VIRTIO_SND_EVT_PCM_XRUN: case VIRTIO_SND_EVT_PCM_XRUN:
spin_lock(&vss->lock); scoped_guard(spinlock, &vss->lock) {
if (vss->xfer_enabled) if (vss->xfer_enabled)
vss->xfer_xrun = true; vss->xfer_xrun = true;
spin_unlock(&vss->lock); }
break; break;
} }
} }

View File

@@ -272,14 +272,8 @@ int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss, unsigned long offset,
*/ */
unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss) unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
{ {
unsigned int num; guard(spinlock_irqsave)(&vss->lock);
unsigned long flags; return vss->msg_count;
spin_lock_irqsave(&vss->lock, flags);
num = vss->msg_count;
spin_unlock_irqrestore(&vss->lock, flags);
return num;
} }
/** /**
@@ -308,7 +302,7 @@ static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
* in the virtqueue. Therefore, on each completion of an I/O message, * in the virtqueue. Therefore, on each completion of an I/O message,
* the hw_ptr value is unconditionally advanced. * the hw_ptr value is unconditionally advanced.
*/ */
spin_lock(&vss->lock); guard(spinlock)(&vss->lock);
/* /*
* If the capture substream returned an incorrect status, then just * If the capture substream returned an incorrect status, then just
* increase the hw_ptr by the message size. * increase the hw_ptr by the message size.
@@ -338,7 +332,6 @@ static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
} else if (!vss->msg_count) { } else if (!vss->msg_count) {
wake_up_all(&vss->msg_empty); wake_up_all(&vss->msg_empty);
} }
spin_unlock(&vss->lock);
} }
/** /**
@@ -351,15 +344,13 @@ static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
{ {
struct virtio_pcm_msg *msg; struct virtio_pcm_msg *msg;
u32 written_bytes; u32 written_bytes;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
do { do {
virtqueue_disable_cb(queue->vqueue); virtqueue_disable_cb(queue->vqueue);
while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes))) while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
virtsnd_pcm_msg_complete(msg, written_bytes); virtsnd_pcm_msg_complete(msg, written_bytes);
} while (!virtqueue_enable_cb(queue->vqueue)); } while (!virtqueue_enable_cb(queue->vqueue));
spin_unlock_irqrestore(&queue->lock, flags);
} }
/** /**

View File

@@ -327,7 +327,6 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
struct virtio_snd *snd = vss->snd; struct virtio_snd *snd = vss->snd;
struct virtio_snd_queue *queue; struct virtio_snd_queue *queue;
struct virtio_snd_msg *msg; struct virtio_snd_msg *msg;
unsigned long flags;
int rc = 0; int rc = 0;
switch (command) { switch (command) {
@@ -335,23 +334,20 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
queue = virtsnd_pcm_queue(vss); queue = virtsnd_pcm_queue(vss);
spin_lock_irqsave(&queue->lock, flags); scoped_guard(spinlock_irqsave, &queue->lock) {
spin_lock(&vss->lock); guard(spinlock)(&vss->lock);
if (vss->direction == SNDRV_PCM_STREAM_CAPTURE) if (vss->direction == SNDRV_PCM_STREAM_CAPTURE)
rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes); rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes);
if (!rc) if (rc)
return rc;
vss->xfer_enabled = true; vss->xfer_enabled = true;
spin_unlock(&vss->lock); }
spin_unlock_irqrestore(&queue->lock, flags);
if (rc)
return rc;
msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START,
GFP_KERNEL); GFP_KERNEL);
if (!msg) { if (!msg) {
spin_lock_irqsave(&vss->lock, flags); guard(spinlock_irqsave)(&vss->lock);
vss->xfer_enabled = false; vss->xfer_enabled = false;
spin_unlock_irqrestore(&vss->lock, flags);
return -ENOMEM; return -ENOMEM;
} }
@@ -364,9 +360,9 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
vss->stopped = true; vss->stopped = true;
fallthrough; fallthrough;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
spin_lock_irqsave(&vss->lock, flags); scoped_guard(spinlock_irqsave, &vss->lock) {
vss->xfer_enabled = false; vss->xfer_enabled = false;
spin_unlock_irqrestore(&vss->lock, flags); }
msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP, msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
GFP_KERNEL); GFP_KERNEL);
@@ -480,38 +476,24 @@ static int virtsnd_pcm_pb_ack(struct snd_pcm_substream *substream)
{ {
struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss);
unsigned long flags;
int rc;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
spin_lock(&vss->lock); guard(spinlock)(&vss->lock);
rc = snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect, return snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy); virtsnd_pcm_trans_copy);
spin_unlock(&vss->lock);
spin_unlock_irqrestore(&queue->lock, flags);
return rc;
} }
static int virtsnd_pcm_cp_ack(struct snd_pcm_substream *substream) static int virtsnd_pcm_cp_ack(struct snd_pcm_substream *substream)
{ {
struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss);
unsigned long flags;
int rc;
spin_lock_irqsave(&queue->lock, flags); guard(spinlock_irqsave)(&queue->lock);
spin_lock(&vss->lock); guard(spinlock)(&vss->lock);
rc = snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect, return snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy); virtsnd_pcm_trans_copy);
spin_unlock(&vss->lock);
spin_unlock_irqrestore(&queue->lock, flags);
return rc;
} }
/* PCM substream operators map. */ /* PCM substream operators map. */