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

ALSA: firewire: lib: Use guard() for mutex locks

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

Only code refactoring, and no behavior change.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250828132802.9032-11-tiwai@suse.de

+38 -77
+8 -20
sound/firewire/amdtp-stream.c
··· 1688 1688 struct pkt_desc *descs; 1689 1689 int i, type, tag, err; 1690 1690 1691 - mutex_lock(&s->mutex); 1691 + guard(mutex)(&s->mutex); 1692 1692 1693 1693 if (WARN_ON(amdtp_stream_running(s) || 1694 - (s->data_block_quadlets < 1))) { 1695 - err = -EBADFD; 1696 - goto err_unlock; 1697 - } 1694 + (s->data_block_quadlets < 1))) 1695 + return -EBADFD; 1698 1696 1699 1697 if (s->direction == AMDTP_IN_STREAM) { 1700 1698 // NOTE: IT context should be used for constant IRQ. 1701 - if (is_irq_target) { 1702 - err = -EINVAL; 1703 - goto err_unlock; 1704 - } 1699 + if (is_irq_target) 1700 + return -EINVAL; 1705 1701 1706 1702 s->data_block_counter = UINT_MAX; 1707 1703 } else { ··· 1721 1725 1722 1726 err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir); 1723 1727 if (err < 0) 1724 - goto err_unlock; 1728 + return err; 1725 1729 s->queue_size = queue_size; 1726 1730 1727 1731 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card, ··· 1842 1846 if (err < 0) 1843 1847 goto err_pkt_descs; 1844 1848 1845 - mutex_unlock(&s->mutex); 1846 - 1847 1849 return 0; 1848 1850 err_pkt_descs: 1849 1851 kfree(s->packet_descs); ··· 1857 1863 s->context = ERR_PTR(-1); 1858 1864 err_buffer: 1859 1865 iso_packets_buffer_destroy(&s->buffer, s->unit); 1860 - err_unlock: 1861 - mutex_unlock(&s->mutex); 1862 1866 1863 1867 return err; 1864 1868 } ··· 1926 1934 */ 1927 1935 static void amdtp_stream_stop(struct amdtp_stream *s) 1928 1936 { 1929 - mutex_lock(&s->mutex); 1937 + guard(mutex)(&s->mutex); 1930 1938 1931 - if (!amdtp_stream_running(s)) { 1932 - mutex_unlock(&s->mutex); 1939 + if (!amdtp_stream_running(s)) 1933 1940 return; 1934 - } 1935 1941 1936 1942 cancel_work_sync(&s->period_work); 1937 1943 fw_iso_context_stop(s->context); ··· 1945 1955 if (s->domain->replay.enable) 1946 1956 kfree(s->ctx_data.tx.cache.descs); 1947 1957 } 1948 - 1949 - mutex_unlock(&s->mutex); 1950 1958 } 1951 1959 1952 1960 /**
+10 -27
sound/firewire/cmp.c
··· 188 188 int cmp_connection_reserve(struct cmp_connection *c, 189 189 unsigned int max_payload_bytes) 190 190 { 191 - int err; 191 + guard(mutex)(&c->mutex); 192 192 193 - mutex_lock(&c->mutex); 194 - 195 - if (WARN_ON(c->resources.allocated)) { 196 - err = -EBUSY; 197 - goto end; 198 - } 193 + if (WARN_ON(c->resources.allocated)) 194 + return -EBUSY; 199 195 200 196 c->speed = min(c->max_speed, 201 197 fw_parent_device(c->resources.unit)->max_speed); 202 198 203 - err = fw_iso_resources_allocate(&c->resources, max_payload_bytes, 204 - c->speed); 205 - end: 206 - mutex_unlock(&c->mutex); 207 - 208 - return err; 199 + return fw_iso_resources_allocate(&c->resources, max_payload_bytes, 200 + c->speed); 209 201 } 210 202 EXPORT_SYMBOL(cmp_connection_reserve); 211 203 212 204 void cmp_connection_release(struct cmp_connection *c) 213 205 { 214 - mutex_lock(&c->mutex); 206 + guard(mutex)(&c->mutex); 215 207 fw_iso_resources_free(&c->resources); 216 - mutex_unlock(&c->mutex); 217 208 } 218 209 EXPORT_SYMBOL(cmp_connection_release); 219 210 ··· 295 304 { 296 305 int err; 297 306 298 - mutex_lock(&c->mutex); 307 + guard(mutex)(&c->mutex); 299 308 300 - if (WARN_ON(c->connected)) { 301 - mutex_unlock(&c->mutex); 309 + if (WARN_ON(c->connected)) 302 310 return -EISCONN; 303 - } 304 311 305 312 retry_after_bus_reset: 306 313 if (c->direction == CMP_OUTPUT) ··· 315 326 } 316 327 if (err >= 0) 317 328 c->connected = true; 318 - 319 - mutex_unlock(&c->mutex); 320 329 321 330 return err; 322 331 } ··· 337 350 { 338 351 int err; 339 352 340 - mutex_lock(&c->mutex); 353 + guard(mutex)(&c->mutex); 341 354 342 - if (!c->connected) { 343 - mutex_unlock(&c->mutex); 355 + if (!c->connected) 344 356 return; 345 - } 346 357 347 358 err = pcr_modify(c, pcr_break_modify, NULL, SUCCEED_ON_BUS_RESET); 348 359 if (err < 0) 349 360 cmp_error(c, "plug is still connected\n"); 350 361 351 362 c->connected = false; 352 - 353 - mutex_unlock(&c->mutex); 354 363 } 355 364 EXPORT_SYMBOL(cmp_connection_break);
+20 -30
sound/firewire/iso-resources.c
··· 123 123 if (err < 0) 124 124 return err; 125 125 126 - mutex_lock(&r->mutex); 127 - 128 - bandwidth = r->bandwidth + r->bandwidth_overhead; 129 - fw_iso_resource_manage(card, r->generation, r->channels_mask, 130 - &channel, &bandwidth, true); 131 - if (channel == -EAGAIN) { 132 - mutex_unlock(&r->mutex); 133 - goto retry_after_bus_reset; 126 + scoped_guard(mutex, &r->mutex) { 127 + bandwidth = r->bandwidth + r->bandwidth_overhead; 128 + fw_iso_resource_manage(card, r->generation, r->channels_mask, 129 + &channel, &bandwidth, true); 130 + if (channel == -EAGAIN) 131 + goto retry_after_bus_reset; 132 + if (channel >= 0) { 133 + r->channel = channel; 134 + r->allocated = true; 135 + } else { 136 + if (channel == -EBUSY) 137 + dev_err(&r->unit->device, 138 + "isochronous resources exhausted\n"); 139 + else 140 + dev_err(&r->unit->device, 141 + "isochronous resource allocation failed\n"); 142 + } 134 143 } 135 - if (channel >= 0) { 136 - r->channel = channel; 137 - r->allocated = true; 138 - } else { 139 - if (channel == -EBUSY) 140 - dev_err(&r->unit->device, 141 - "isochronous resources exhausted\n"); 142 - else 143 - dev_err(&r->unit->device, 144 - "isochronous resource allocation failed\n"); 145 - } 146 - 147 - mutex_unlock(&r->mutex); 148 144 149 145 return channel; 150 146 } ··· 162 166 struct fw_card *card = fw_parent_device(r->unit)->card; 163 167 int bandwidth, channel; 164 168 165 - mutex_lock(&r->mutex); 169 + guard(mutex)(&r->mutex); 166 170 167 - if (!r->allocated) { 168 - mutex_unlock(&r->mutex); 171 + if (!r->allocated) 169 172 return 0; 170 - } 171 173 172 174 spin_lock_irq(&card->lock); 173 175 r->generation = card->generation; ··· 190 196 "isochronous resource allocation failed\n"); 191 197 } 192 198 193 - mutex_unlock(&r->mutex); 194 - 195 199 return channel; 196 200 } 197 201 EXPORT_SYMBOL(fw_iso_resources_update); ··· 210 218 return; 211 219 card = fw_parent_device(r->unit)->card; 212 220 213 - mutex_lock(&r->mutex); 221 + guard(mutex)(&r->mutex); 214 222 215 223 if (r->allocated) { 216 224 bandwidth = r->bandwidth + r->bandwidth_overhead; ··· 222 230 223 231 r->allocated = false; 224 232 } 225 - 226 - mutex_unlock(&r->mutex); 227 233 } 228 234 EXPORT_SYMBOL(fw_iso_resources_free);