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

Configure Feed

Select the types of activity you want to include in your feed.

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

+38 -74
+2 -6
sound/virtio/virtio_card.c
··· 85 85 struct virtio_snd_queue *queue = virtsnd_event_queue(snd); 86 86 struct virtio_snd_event *event; 87 87 u32 length; 88 - unsigned long flags; 89 88 90 - spin_lock_irqsave(&queue->lock, flags); 89 + guard(spinlock_irqsave)(&queue->lock); 91 90 do { 92 91 virtqueue_disable_cb(vqueue); 93 92 while ((event = virtqueue_get_buf(vqueue, &length))) { ··· 94 95 virtsnd_event_send(vqueue, event, true, GFP_ATOMIC); 95 96 } 96 97 } while (!virtqueue_enable_cb(vqueue)); 97 - spin_unlock_irqrestore(&queue->lock, flags); 98 98 } 99 99 100 100 /** ··· 174 176 struct virtio_snd_queue *queue = virtsnd_event_queue(snd); 175 177 struct virtio_snd_event *event; 176 178 u32 length; 177 - unsigned long flags; 178 179 179 180 if (queue->vqueue) { 180 - spin_lock_irqsave(&queue->lock, flags); 181 + guard(spinlock_irqsave)(&queue->lock); 181 182 virtqueue_disable_cb(queue->vqueue); 182 183 while ((event = virtqueue_get_buf(queue->vqueue, &length))) 183 184 virtsnd_event_dispatch(snd, event); 184 - spin_unlock_irqrestore(&queue->lock, flags); 185 185 } 186 186 } 187 187
+9 -14
sound/virtio/virtio_ctl_msg.c
··· 131 131 unsigned int nins = 0; 132 132 struct scatterlist *psgs[4]; 133 133 bool notify = false; 134 - unsigned long flags; 135 134 int rc; 136 135 137 136 virtsnd_ctl_msg_ref(msg); ··· 146 147 if (in_sgs) 147 148 psgs[nouts + nins++] = in_sgs; 148 149 149 - spin_lock_irqsave(&queue->lock, flags); 150 - rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg, 151 - GFP_ATOMIC); 152 - if (!rc) { 153 - notify = virtqueue_kick_prepare(queue->vqueue); 150 + scoped_guard(spinlock_irqsave, &queue->lock) { 151 + rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg, 152 + GFP_ATOMIC); 153 + if (!rc) { 154 + notify = virtqueue_kick_prepare(queue->vqueue); 154 155 155 - list_add_tail(&msg->list, &snd->ctl_msgs); 156 + list_add_tail(&msg->list, &snd->ctl_msgs); 157 + } 156 158 } 157 - spin_unlock_irqrestore(&queue->lock, flags); 158 159 159 160 if (rc) { 160 161 dev_err(&vdev->dev, "failed to send control message (0x%08x)\n", ··· 232 233 void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd) 233 234 { 234 235 struct virtio_snd_queue *queue = virtsnd_control_queue(snd); 235 - unsigned long flags; 236 236 237 - spin_lock_irqsave(&queue->lock, flags); 237 + guard(spinlock_irqsave)(&queue->lock); 238 238 while (!list_empty(&snd->ctl_msgs)) { 239 239 struct virtio_snd_msg *msg = 240 240 list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg, ··· 241 243 242 244 virtsnd_ctl_msg_complete(msg); 243 245 } 244 - spin_unlock_irqrestore(&queue->lock, flags); 245 246 } 246 247 247 248 /** ··· 293 296 struct virtio_snd_queue *queue = virtsnd_control_queue(snd); 294 297 struct virtio_snd_msg *msg; 295 298 u32 length; 296 - unsigned long flags; 297 299 298 - spin_lock_irqsave(&queue->lock, flags); 300 + guard(spinlock_irqsave)(&queue->lock); 299 301 do { 300 302 virtqueue_disable_cb(vqueue); 301 303 while ((msg = virtqueue_get_buf(vqueue, &length))) 302 304 virtsnd_ctl_msg_complete(msg); 303 305 } while (!virtqueue_enable_cb(vqueue)); 304 - spin_unlock_irqrestore(&queue->lock, flags); 305 306 }
+4 -4
sound/virtio/virtio_pcm.c
··· 515 515 /* TODO: deal with shmem elapsed period */ 516 516 break; 517 517 case VIRTIO_SND_EVT_PCM_XRUN: 518 - spin_lock(&vss->lock); 519 - if (vss->xfer_enabled) 520 - vss->xfer_xrun = true; 521 - spin_unlock(&vss->lock); 518 + scoped_guard(spinlock, &vss->lock) { 519 + if (vss->xfer_enabled) 520 + vss->xfer_xrun = true; 521 + } 522 522 break; 523 523 } 524 524 }
+4 -13
sound/virtio/virtio_pcm_msg.c
··· 272 272 */ 273 273 unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss) 274 274 { 275 - unsigned int num; 276 - unsigned long flags; 277 - 278 - spin_lock_irqsave(&vss->lock, flags); 279 - num = vss->msg_count; 280 - spin_unlock_irqrestore(&vss->lock, flags); 281 - 282 - return num; 275 + guard(spinlock_irqsave)(&vss->lock); 276 + return vss->msg_count; 283 277 } 284 278 285 279 /** ··· 302 308 * in the virtqueue. Therefore, on each completion of an I/O message, 303 309 * the hw_ptr value is unconditionally advanced. 304 310 */ 305 - spin_lock(&vss->lock); 311 + guard(spinlock)(&vss->lock); 306 312 /* 307 313 * If the capture substream returned an incorrect status, then just 308 314 * increase the hw_ptr by the message size. ··· 332 338 } else if (!vss->msg_count) { 333 339 wake_up_all(&vss->msg_empty); 334 340 } 335 - spin_unlock(&vss->lock); 336 341 } 337 342 338 343 /** ··· 344 351 { 345 352 struct virtio_pcm_msg *msg; 346 353 u32 written_bytes; 347 - unsigned long flags; 348 354 349 - spin_lock_irqsave(&queue->lock, flags); 355 + guard(spinlock_irqsave)(&queue->lock); 350 356 do { 351 357 virtqueue_disable_cb(queue->vqueue); 352 358 while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes))) 353 359 virtsnd_pcm_msg_complete(msg, written_bytes); 354 360 } while (!virtqueue_enable_cb(queue->vqueue)); 355 - spin_unlock_irqrestore(&queue->lock, flags); 356 361 } 357 362 358 363 /**
+19 -37
sound/virtio/virtio_pcm_ops.c
··· 327 327 struct virtio_snd *snd = vss->snd; 328 328 struct virtio_snd_queue *queue; 329 329 struct virtio_snd_msg *msg; 330 - unsigned long flags; 331 330 int rc = 0; 332 331 333 332 switch (command) { ··· 334 335 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 335 336 queue = virtsnd_pcm_queue(vss); 336 337 337 - spin_lock_irqsave(&queue->lock, flags); 338 - spin_lock(&vss->lock); 339 - if (vss->direction == SNDRV_PCM_STREAM_CAPTURE) 340 - rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes); 341 - if (!rc) 338 + scoped_guard(spinlock_irqsave, &queue->lock) { 339 + guard(spinlock)(&vss->lock); 340 + if (vss->direction == SNDRV_PCM_STREAM_CAPTURE) 341 + rc = virtsnd_pcm_msg_send(vss, 0, vss->buffer_bytes); 342 + if (rc) 343 + return rc; 342 344 vss->xfer_enabled = true; 343 - spin_unlock(&vss->lock); 344 - spin_unlock_irqrestore(&queue->lock, flags); 345 - if (rc) 346 - return rc; 345 + } 347 346 348 347 msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START, 349 348 GFP_KERNEL); 350 349 if (!msg) { 351 - spin_lock_irqsave(&vss->lock, flags); 350 + guard(spinlock_irqsave)(&vss->lock); 352 351 vss->xfer_enabled = false; 353 - spin_unlock_irqrestore(&vss->lock, flags); 354 352 355 353 return -ENOMEM; 356 354 } ··· 360 364 vss->stopped = true; 361 365 fallthrough; 362 366 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 363 - spin_lock_irqsave(&vss->lock, flags); 364 - vss->xfer_enabled = false; 365 - spin_unlock_irqrestore(&vss->lock, flags); 367 + scoped_guard(spinlock_irqsave, &vss->lock) { 368 + vss->xfer_enabled = false; 369 + } 366 370 367 371 msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP, 368 372 GFP_KERNEL); ··· 476 480 { 477 481 struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); 478 482 struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); 479 - unsigned long flags; 480 - int rc; 481 483 482 - spin_lock_irqsave(&queue->lock, flags); 483 - spin_lock(&vss->lock); 484 + guard(spinlock_irqsave)(&queue->lock); 485 + guard(spinlock)(&vss->lock); 484 486 485 - rc = snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect, 486 - virtsnd_pcm_trans_copy); 487 - 488 - spin_unlock(&vss->lock); 489 - spin_unlock_irqrestore(&queue->lock, flags); 490 - 491 - return rc; 487 + return snd_pcm_indirect_playback_transfer(substream, &vss->pcm_indirect, 488 + virtsnd_pcm_trans_copy); 492 489 } 493 490 494 491 static int virtsnd_pcm_cp_ack(struct snd_pcm_substream *substream) 495 492 { 496 493 struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream); 497 494 struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss); 498 - unsigned long flags; 499 - int rc; 500 495 501 - spin_lock_irqsave(&queue->lock, flags); 502 - spin_lock(&vss->lock); 496 + guard(spinlock_irqsave)(&queue->lock); 497 + guard(spinlock)(&vss->lock); 503 498 504 - rc = snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect, 505 - virtsnd_pcm_trans_copy); 506 - 507 - spin_unlock(&vss->lock); 508 - spin_unlock_irqrestore(&queue->lock, flags); 509 - 510 - return rc; 499 + return snd_pcm_indirect_capture_transfer(substream, &vss->pcm_indirect, 500 + virtsnd_pcm_trans_copy); 511 501 } 512 502 513 503 /* PCM substream operators map. */