]> www.infradead.org Git - users/hch/misc.git/commitdiff
ALSA: virtio: Use guard() for spin locks
authorTakashi Iwai <tiwai@suse.de>
Fri, 29 Aug 2025 15:13:32 +0000 (17:13 +0200)
committerTakashi Iwai <tiwai@suse.de>
Mon, 1 Sep 2025 11:54:28 +0000 (13:54 +0200)
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
sound/virtio/virtio_card.c
sound/virtio/virtio_ctl_msg.c
sound/virtio/virtio_pcm.c
sound/virtio/virtio_pcm_msg.c
sound/virtio/virtio_pcm_ops.c

index 965209e1d872822158d53377df91d7921f51bff5..52c5757585c6d34376341daf3c842fef4908be8d 100644 (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);
        }
 }
 
index 9dabea01277f845726ee2a908b9288c9f0e5e918..6433c870f271a7a222dd08dc184dab6e8795d754 100644 (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);
 }
index 2f7c5e709f0755f2381727616268b41a2b5aaf1b..3602b6690fcd902b5bdbb57ed3c91a063aa098dd 100644 (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;
        }
 }
index 8c32efaf4c5294e6aba0adcfb8a40a22d3a0d261..9778020a7ba89953bfdd09209db843d487e23968 100644 (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);
 }
 
 /**
index ad12aae52fc32e80a04e9effba0424688fe0257a..6297a9c61e70b06a1b9689a79aeceddd2df5a917 100644 (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);
-
-       rc = snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect,
-                                              virtsnd_pcm_trans_copy);
 
-       spin_unlock(&vss->lock);
-       spin_unlock_irqrestore(&queue->lock, flags);
+       guard(spinlock_irqsave)(&queue->lock);
+       guard(spinlock)(&vss->lock);
 
-       return rc;
+       return snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect,
+                                                virtsnd_pcm_trans_copy);
 }
 
 /* PCM substream operators map. */