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

ALSA: info: Use guard() for locking

We can simplify the code gracefully with new guard() macro and co for
automatic cleanup of locks.

Only the code refactoring, and no functional changes.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://lore.kernel.org/r/20240227085306.9764-7-tiwai@suse.de

+33 -70
+30 -63
sound/core/info.c
··· 105 105 { 106 106 struct snd_info_private_data *data; 107 107 struct snd_info_entry *entry; 108 - loff_t ret = -EINVAL, size; 108 + loff_t size; 109 109 110 110 data = file->private_data; 111 111 entry = data->entry; 112 - mutex_lock(&entry->access); 113 - if (entry->c.ops->llseek) { 114 - ret = entry->c.ops->llseek(entry, 115 - data->file_private_data, 116 - file, offset, orig); 117 - goto out; 118 - } 112 + guard(mutex)(&entry->access); 113 + if (entry->c.ops->llseek) 114 + return entry->c.ops->llseek(entry, 115 + data->file_private_data, 116 + file, offset, orig); 119 117 120 118 size = entry->size; 121 119 switch (orig) { ··· 124 126 break; 125 127 case SEEK_END: 126 128 if (!size) 127 - goto out; 129 + return -EINVAL; 128 130 offset += size; 129 131 break; 130 132 default: 131 - goto out; 133 + return -EINVAL; 132 134 } 133 135 if (offset < 0) 134 - goto out; 136 + return -EINVAL; 135 137 if (size && offset > size) 136 138 offset = size; 137 139 file->f_pos = offset; 138 - ret = offset; 139 - out: 140 - mutex_unlock(&entry->access); 141 - return ret; 140 + return offset; 142 141 } 143 142 144 143 static ssize_t snd_info_entry_read(struct file *file, char __user *buffer, ··· 233 238 struct snd_info_private_data *data; 234 239 int mode, err; 235 240 236 - mutex_lock(&info_mutex); 241 + guard(mutex)(&info_mutex); 237 242 err = alloc_info_private(entry, &data); 238 243 if (err < 0) 239 - goto unlock; 244 + return err; 240 245 241 246 mode = file->f_flags & O_ACCMODE; 242 247 if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) || ··· 252 257 } 253 258 254 259 file->private_data = data; 255 - mutex_unlock(&info_mutex); 256 260 return 0; 257 261 258 262 error: 259 263 kfree(data); 260 264 module_put(entry->module); 261 - unlock: 262 - mutex_unlock(&info_mutex); 263 265 return err; 264 266 } 265 267 ··· 298 306 struct snd_info_buffer *buf; 299 307 loff_t pos; 300 308 size_t next; 301 - int err = 0; 302 309 303 310 if (!entry->c.text.write) 304 311 return -EIO; ··· 308 317 /* don't handle too large text inputs */ 309 318 if (next > 16 * 1024) 310 319 return -EIO; 311 - mutex_lock(&entry->access); 320 + guard(mutex)(&entry->access); 312 321 buf = data->wbuffer; 313 322 if (!buf) { 314 323 data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL); 315 - if (!buf) { 316 - err = -ENOMEM; 317 - goto error; 318 - } 324 + if (!buf) 325 + return -ENOMEM; 319 326 } 320 327 if (next > buf->len) { 321 328 char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL); 322 - if (!nbuf) { 323 - err = -ENOMEM; 324 - goto error; 325 - } 329 + if (!nbuf) 330 + return -ENOMEM; 326 331 kvfree(buf->buffer); 327 332 buf->buffer = nbuf; 328 333 buf->len = PAGE_ALIGN(next); 329 334 } 330 - if (copy_from_user(buf->buffer + pos, buffer, count)) { 331 - err = -EFAULT; 332 - goto error; 333 - } 335 + if (copy_from_user(buf->buffer + pos, buffer, count)) 336 + return -EFAULT; 334 337 buf->size = next; 335 - error: 336 - mutex_unlock(&entry->access); 337 - if (err < 0) 338 - return err; 339 338 *offset = next; 340 339 return count; 341 340 } ··· 350 369 struct snd_info_private_data *data; 351 370 int err; 352 371 353 - mutex_lock(&info_mutex); 372 + guard(mutex)(&info_mutex); 354 373 err = alloc_info_private(entry, &data); 355 374 if (err < 0) 356 - goto unlock; 375 + return err; 357 376 358 377 data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL); 359 378 if (!data->rbuffer) { ··· 367 386 err = single_open(file, snd_info_seq_show, data); 368 387 if (err < 0) 369 388 goto error; 370 - mutex_unlock(&info_mutex); 371 389 return 0; 372 390 373 391 error: 374 392 kfree(data->rbuffer); 375 393 kfree(data); 376 394 module_put(entry->module); 377 - unlock: 378 - mutex_unlock(&info_mutex); 379 395 return err; 380 396 } 381 397 ··· 527 549 */ 528 550 void snd_info_card_id_change(struct snd_card *card) 529 551 { 530 - mutex_lock(&info_mutex); 552 + guard(mutex)(&info_mutex); 531 553 if (card->proc_root_link) { 532 554 proc_remove(card->proc_root_link); 533 555 card->proc_root_link = NULL; ··· 536 558 card->proc_root_link = proc_symlink(card->id, 537 559 snd_proc_root->p, 538 560 card->proc_root->name); 539 - mutex_unlock(&info_mutex); 540 561 } 541 562 542 563 /* ··· 551 574 if (card->proc_root) 552 575 proc_remove(card->proc_root->p); 553 576 554 - mutex_lock(&info_mutex); 577 + guard(mutex)(&info_mutex); 555 578 if (card->proc_root) 556 579 snd_info_clear_entries(card->proc_root); 557 580 card->proc_root_link = NULL; 558 581 card->proc_root = NULL; 559 - mutex_unlock(&info_mutex); 560 582 } 561 583 562 584 /* ··· 679 703 entry->parent = parent; 680 704 entry->module = module; 681 705 if (parent) { 682 - mutex_lock(&parent->access); 706 + guard(mutex)(&parent->access); 683 707 list_add_tail(&entry->list, &parent->children); 684 - mutex_unlock(&parent->access); 685 708 } 686 709 return entry; 687 710 } ··· 750 775 return; 751 776 if (entry->p) { 752 777 proc_remove(entry->p); 753 - mutex_lock(&info_mutex); 778 + guard(mutex)(&info_mutex); 754 779 snd_info_clear_entries(entry); 755 - mutex_unlock(&info_mutex); 756 780 } 757 781 758 782 /* free all children at first */ ··· 760 786 761 787 p = entry->parent; 762 788 if (p) { 763 - mutex_lock(&p->access); 789 + guard(mutex)(&p->access); 764 790 list_del(&entry->list); 765 - mutex_unlock(&p->access); 766 791 } 767 792 kfree(entry->name); 768 793 if (entry->private_free) ··· 777 804 if (snd_BUG_ON(!entry)) 778 805 return -ENXIO; 779 806 root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p; 780 - mutex_lock(&info_mutex); 807 + guard(mutex)(&info_mutex); 781 808 if (entry->p || !root) 782 - goto unlock; 809 + return 0; 783 810 if (S_ISDIR(entry->mode)) { 784 811 p = proc_mkdir_mode(entry->name, entry->mode, root); 785 - if (!p) { 786 - mutex_unlock(&info_mutex); 812 + if (!p) 787 813 return -ENOMEM; 788 - } 789 814 } else { 790 815 const struct proc_ops *ops; 791 816 if (entry->content == SNDRV_INFO_CONTENT_DATA) ··· 792 821 ops = &snd_info_text_entry_ops; 793 822 p = proc_create_data(entry->name, entry->mode, root, 794 823 ops, entry); 795 - if (!p) { 796 - mutex_unlock(&info_mutex); 824 + if (!p) 797 825 return -ENOMEM; 798 - } 799 826 proc_set_size(p, entry->size); 800 827 } 801 828 entry->p = p; 802 - unlock: 803 - mutex_unlock(&info_mutex); 804 829 return 0; 805 830 } 806 831
+3 -7
sound/core/info_oss.c
··· 29 29 return -ENXIO; 30 30 if (snd_BUG_ON(num < 0 || num >= SNDRV_CARDS)) 31 31 return -ENXIO; 32 - mutex_lock(&strings); 32 + guard(mutex)(&strings); 33 33 if (string == NULL) { 34 34 x = snd_sndstat_strings[num][dev]; 35 35 kfree(x); 36 36 x = NULL; 37 37 } else { 38 38 x = kstrdup(string, GFP_KERNEL); 39 - if (x == NULL) { 40 - mutex_unlock(&strings); 39 + if (x == NULL) 41 40 return -ENOMEM; 42 - } 43 41 } 44 42 snd_sndstat_strings[num][dev] = x; 45 - mutex_unlock(&strings); 46 43 return 0; 47 44 } 48 45 EXPORT_SYMBOL(snd_oss_info_register); ··· 50 53 char *str; 51 54 52 55 snd_iprintf(buf, "\n%s:", id); 53 - mutex_lock(&strings); 56 + guard(mutex)(&strings); 54 57 for (idx = 0; idx < SNDRV_CARDS; idx++) { 55 58 str = snd_sndstat_strings[idx][dev]; 56 59 if (str) { ··· 61 64 snd_iprintf(buf, "%i: %s\n", idx, str); 62 65 } 63 66 } 64 - mutex_unlock(&strings); 65 67 if (ok < 0) 66 68 snd_iprintf(buf, " NOT ENABLED IN CONFIG\n"); 67 69 return ok;