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_event *event;
u32 length;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags);
guard(spinlock_irqsave)(&queue->lock);
do {
virtqueue_disable_cb(vqueue);
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);
}
} 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_event *event;
u32 length;
unsigned long flags;
if (queue->vqueue) {
spin_lock_irqsave(&queue->lock, flags);
guard(spinlock_irqsave)(&queue->lock);
virtqueue_disable_cb(queue->vqueue);
while ((event = virtqueue_get_buf(queue->vqueue, &length)))
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;
struct scatterlist *psgs[4];
bool notify = false;
unsigned long flags;
int rc;
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)
psgs[nouts + nins++] = in_sgs;
spin_lock_irqsave(&queue->lock, flags);
rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
GFP_ATOMIC);
if (!rc) {
notify = virtqueue_kick_prepare(queue->vqueue);
scoped_guard(spinlock_irqsave, &queue->lock) {
rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
GFP_ATOMIC);
if (!rc) {
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) {
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)
{
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)) {
struct virtio_snd_msg *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);
}
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_msg *msg;
u32 length;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags);
guard(spinlock_irqsave)(&queue->lock);
do {
virtqueue_disable_cb(vqueue);
while ((msg = virtqueue_get_buf(vqueue, &length)))
virtsnd_ctl_msg_complete(msg);
} 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 */
break;
case VIRTIO_SND_EVT_PCM_XRUN:
spin_lock(&vss->lock);
if (vss->xfer_enabled)
vss->xfer_xrun = true;
spin_unlock(&vss->lock);
scoped_guard(spinlock, &vss->lock) {
if (vss->xfer_enabled)
vss->xfer_xrun = true;
}
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 num;
unsigned long flags;
spin_lock_irqsave(&vss->lock, flags);
num = vss->msg_count;
spin_unlock_irqrestore(&vss->lock, flags);
return num;
guard(spinlock_irqsave)(&vss->lock);
return vss->msg_count;
}
/**
@@ -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,
* 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
* 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) {
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;
u32 written_bytes;
unsigned long flags;
spin_lock_irqsave(&queue->lock, flags);
guard(spinlock_irqsave)(&queue->lock);
do {
virtqueue_disable_cb(queue->vqueue);
while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
virtsnd_pcm_msg_complete(msg, written_bytes);
} 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_queue *queue;
struct virtio_snd_msg *msg;
unsigned long flags;
int rc = 0;
switch (command) {
@@ -335,23 +334,20 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
queue = virtsnd_pcm_queue(vss);
spin_lock_irqsave(&queue->lock, flags);
spin_lock(&vss->lock);
if (vss->direction == SNDRV_PCM_STREAM_CAPTURE)
rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes);
if (!rc)
scoped_guard(spinlock_irqsave, &queue->lock) {
guard(spinlock)(&vss->lock);
if (vss->direction == SNDRV_PCM_STREAM_CAPTURE)
rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes);
if (rc)
return rc;
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,
GFP_KERNEL);
if (!msg) {
spin_lock_irqsave(&vss->lock, flags);
guard(spinlock_irqsave)(&vss->lock);
vss->xfer_enabled = false;
spin_unlock_irqrestore(&vss->lock, flags);
return -ENOMEM;
}
@@ -364,9 +360,9 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
vss->stopped = true;
fallthrough;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
spin_lock_irqsave(&vss->lock, flags);
vss->xfer_enabled = false;
spin_unlock_irqrestore(&vss->lock, flags);
scoped_guard(spinlock_irqsave, &vss->lock) {
vss->xfer_enabled = false;
}
msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
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_snd_queue *queue = virtsnd_pcm_queue(vss);
unsigned long flags;
int rc;
spin_lock_irqsave(&queue->lock, flags);
spin_lock(&vss->lock);
guard(spinlock_irqsave)(&queue->lock);
guard(spinlock)(&vss->lock);
rc = snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy);
spin_unlock(&vss->lock);
spin_unlock_irqrestore(&queue->lock, flags);
return rc;
return snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy);
}
static int virtsnd_pcm_cp_ack(struct snd_pcm_substream *substream)
{
struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss);
unsigned long flags;
int rc;
spin_lock_irqsave(&queue->lock, flags);
spin_lock(&vss->lock);
guard(spinlock_irqsave)(&queue->lock);
guard(spinlock)(&vss->lock);
rc = snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy);
spin_unlock(&vss->lock);
spin_unlock_irqrestore(&queue->lock, flags);
return rc;
return snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect,
virtsnd_pcm_trans_copy);
}
/* PCM substream operators map. */