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

ALSA: hda/core: Use guard() for spinlocks

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/20250827072916.31933-24-tiwai@suse.de

+33 -50
+23 -35
sound/hda/core/controller.c
··· 44 44 { 45 45 WARN_ON_ONCE(!bus->rb.area); 46 46 47 - spin_lock_irq(&bus->reg_lock); 47 + guard(spinlock_irq)(&bus->reg_lock); 48 48 /* CORB set up */ 49 49 bus->corb.addr = bus->rb.addr; 50 50 bus->corb.buf = (__le32 *)bus->rb.area; ··· 86 86 snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN); 87 87 /* Accept unsolicited responses */ 88 88 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL); 89 - spin_unlock_irq(&bus->reg_lock); 90 89 } 91 90 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io); 92 91 ··· 111 112 */ 112 113 void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus) 113 114 { 114 - spin_lock_irq(&bus->reg_lock); 115 - /* disable ringbuffer DMAs */ 116 - snd_hdac_chip_writeb(bus, RIRBCTL, 0); 117 - snd_hdac_chip_writeb(bus, CORBCTL, 0); 118 - spin_unlock_irq(&bus->reg_lock); 115 + scoped_guard(spinlock_irq, &bus->reg_lock) { 116 + /* disable ringbuffer DMAs */ 117 + snd_hdac_chip_writeb(bus, RIRBCTL, 0); 118 + snd_hdac_chip_writeb(bus, CORBCTL, 0); 119 + } 119 120 120 121 hdac_wait_for_cmd_dmas(bus); 121 122 122 - spin_lock_irq(&bus->reg_lock); 123 + guard(spinlock_irq)(&bus->reg_lock); 123 124 /* disable unsolicited responses */ 124 125 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, 0); 125 - spin_unlock_irq(&bus->reg_lock); 126 126 } 127 127 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io); 128 128 ··· 169 171 { 170 172 unsigned int addr = azx_command_addr(val); 171 173 int timeout = 50; 172 - int ret = -EIO; 173 174 174 - spin_lock_irq(&bus->reg_lock); 175 + guard(spinlock_irq)(&bus->reg_lock); 175 176 176 177 while (timeout--) { 177 178 /* check ICB bit */ ··· 181 184 /* Set ICB bit */ 182 185 snd_hdac_chip_updatew(bus, IRS, AZX_IRS_BUSY, AZX_IRS_BUSY); 183 186 184 - ret = snd_hdac_bus_wait_for_pio_response(bus, addr); 185 - goto out; 187 + return snd_hdac_bus_wait_for_pio_response(bus, addr); 186 188 } 187 189 udelay(1); 188 190 } ··· 189 193 dev_dbg_ratelimited(bus->dev, "send_cmd_pio timeout: IRS=%#x, val=%#x\n", 190 194 snd_hdac_chip_readw(bus, IRS), val); 191 195 192 - out: 193 - spin_unlock_irq(&bus->reg_lock); 194 - 195 - return ret; 196 + return -EIO; 196 197 } 197 198 198 199 /** ··· 221 228 unsigned int addr = azx_command_addr(val); 222 229 unsigned int wp, rp; 223 230 224 - spin_lock_irq(&bus->reg_lock); 231 + guard(spinlock_irq)(&bus->reg_lock); 225 232 226 233 bus->last_cmd[azx_command_addr(val)] = val; 227 234 ··· 229 236 wp = snd_hdac_chip_readw(bus, CORBWP); 230 237 if (wp == 0xffff) { 231 238 /* something wrong, controller likely turned to D3 */ 232 - spin_unlock_irq(&bus->reg_lock); 233 239 return -EIO; 234 240 } 235 241 wp++; ··· 237 245 rp = snd_hdac_chip_readw(bus, CORBRP); 238 246 if (wp == rp) { 239 247 /* oops, it's full */ 240 - spin_unlock_irq(&bus->reg_lock); 241 248 return -EAGAIN; 242 249 } 243 250 244 251 bus->rirb.cmds[addr]++; 245 252 bus->corb.buf[wp] = cpu_to_le32(val); 246 253 snd_hdac_chip_writew(bus, CORBWP, wp); 247 - 248 - spin_unlock_irq(&bus->reg_lock); 249 254 250 255 return 0; 251 256 } ··· 322 333 timeout = jiffies + msecs_to_jiffies(1000); 323 334 324 335 for (loopcounter = 0;; loopcounter++) { 325 - spin_lock_irq(&bus->reg_lock); 326 - if (!bus->polling_mode) 327 - prepare_to_wait(&bus->rirb_wq, &wait, 328 - TASK_UNINTERRUPTIBLE); 329 - if (bus->polling_mode) 330 - snd_hdac_bus_update_rirb(bus); 331 - if (!bus->rirb.cmds[addr]) { 332 - if (res) 333 - *res = bus->rirb.res[addr]; /* the last value */ 336 + scoped_guard(spinlock_irq, &bus->reg_lock) { 334 337 if (!bus->polling_mode) 335 - finish_wait(&bus->rirb_wq, &wait); 336 - spin_unlock_irq(&bus->reg_lock); 337 - return 0; 338 + prepare_to_wait(&bus->rirb_wq, &wait, 339 + TASK_UNINTERRUPTIBLE); 340 + if (bus->polling_mode) 341 + snd_hdac_bus_update_rirb(bus); 342 + if (!bus->rirb.cmds[addr]) { 343 + if (res) 344 + *res = bus->rirb.res[addr]; /* the last value */ 345 + if (!bus->polling_mode) 346 + finish_wait(&bus->rirb_wq, &wait); 347 + return 0; 348 + } 338 349 } 339 - spin_unlock_irq(&bus->reg_lock); 340 350 if (time_after(jiffies, timeout)) 341 351 break; 342 352 #define LOOP_COUNT_MAX 3000
+10 -15
sound/hda/core/stream.c
··· 370 370 if (substream->pcm) 371 371 key |= (substream->pcm->device << 16); 372 372 373 - spin_lock_irq(&bus->reg_lock); 373 + guard(spinlock_irq)(&bus->reg_lock); 374 374 list_for_each_entry(azx_dev, &bus->stream_list, list) { 375 375 if (azx_dev->direction != substream->stream) 376 376 continue; ··· 389 389 res->assigned_key = key; 390 390 res->substream = substream; 391 391 } 392 - spin_unlock_irq(&bus->reg_lock); 393 392 return res; 394 393 } 395 394 EXPORT_SYMBOL_GPL(snd_hdac_stream_assign); ··· 418 419 { 419 420 struct hdac_bus *bus = azx_dev->bus; 420 421 421 - spin_lock_irq(&bus->reg_lock); 422 + guard(spinlock_irq)(&bus->reg_lock); 422 423 snd_hdac_stream_release_locked(azx_dev); 423 - spin_unlock_irq(&bus->reg_lock); 424 424 } 425 425 EXPORT_SYMBOL_GPL(snd_hdac_stream_release); 426 426 ··· 921 923 int err; 922 924 923 925 guard(snd_hdac_dsp_lock)(azx_dev); 924 - spin_lock_irq(&bus->reg_lock); 925 - if (azx_dev->running || azx_dev->locked) { 926 - spin_unlock_irq(&bus->reg_lock); 927 - return -EBUSY; 926 + scoped_guard(spinlock_irq, &bus->reg_lock) { 927 + if (azx_dev->running || azx_dev->locked) 928 + return -EBUSY; 929 + azx_dev->locked = true; 928 930 } 929 - azx_dev->locked = true; 930 - spin_unlock_irq(&bus->reg_lock); 931 931 932 932 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, bus->dev, 933 933 byte_size, bufp); ··· 951 955 error: 952 956 snd_dma_free_pages(bufp); 953 957 err_alloc: 954 - spin_lock_irq(&bus->reg_lock); 955 - azx_dev->locked = false; 956 - spin_unlock_irq(&bus->reg_lock); 958 + scoped_guard(spinlock_irq, &bus->reg_lock) { 959 + azx_dev->locked = false; 960 + } 957 961 return err; 958 962 } 959 963 EXPORT_SYMBOL_GPL(snd_hdac_dsp_prepare); ··· 997 1001 snd_dma_free_pages(dmab); 998 1002 dmab->area = NULL; 999 1003 1000 - spin_lock_irq(&bus->reg_lock); 1004 + guard(spinlock_irq)(&bus->reg_lock); 1001 1005 azx_dev->locked = false; 1002 - spin_unlock_irq(&bus->reg_lock); 1003 1006 } 1004 1007 EXPORT_SYMBOL_GPL(snd_hdac_dsp_cleanup); 1005 1008 #endif /* CONFIG_SND_HDA_DSP_LOADER */