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.

eventfs: Simplify code using guard()s

Use guard(mutex), scoped_guard(mutex) and guard(src) to simplify the code
and remove a lot of the jumps to "out:" labels.

Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Link: https://lore.kernel.org/20250604151625.250d13e1@gandalf.local.home
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

+36 -60
+36 -60
fs/tracefs/event_inode.c
··· 180 180 const char *name; 181 181 int ret; 182 182 183 - mutex_lock(&eventfs_mutex); 183 + guard(mutex)(&eventfs_mutex); 184 184 ei = dentry->d_fsdata; 185 - if (ei->is_freed) { 186 - /* Do not allow changes if the event is about to be removed. */ 187 - mutex_unlock(&eventfs_mutex); 185 + /* Do not allow changes if the event is about to be removed. */ 186 + if (ei->is_freed) 188 187 return -ENODEV; 189 - } 190 188 191 189 /* Preallocate the children mode array if necessary */ 192 190 if (!(dentry->d_inode->i_mode & S_IFDIR)) { 193 191 if (!ei->entry_attrs) { 194 192 ei->entry_attrs = kzalloc_objs(*ei->entry_attrs, 195 193 ei->nr_entries, GFP_NOFS); 196 - if (!ei->entry_attrs) { 197 - ret = -ENOMEM; 198 - goto out; 199 - } 194 + if (!ei->entry_attrs) 195 + return -ENOMEM; 200 196 } 201 197 } 202 198 203 199 ret = simple_setattr(idmap, dentry, iattr); 204 200 if (ret < 0) 205 - goto out; 201 + return ret; 206 202 207 203 /* 208 204 * If this is a dir, then update the ei cache, only the file ··· 221 225 } 222 226 } 223 227 } 224 - out: 225 - mutex_unlock(&eventfs_mutex); 226 228 return ret; 227 229 } 228 230 ··· 522 528 struct tracefs_inode *ti; 523 529 struct eventfs_inode *ei; 524 530 const char *name = dentry->d_name.name; 525 - struct dentry *result = NULL; 526 531 527 532 ti = get_tracefs(dir); 528 533 if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE))) 529 534 return ERR_PTR(-EIO); 530 535 531 - mutex_lock(&eventfs_mutex); 536 + guard(mutex)(&eventfs_mutex); 532 537 533 538 ei = ti->private; 534 539 if (!ei || ei->is_freed) 535 - goto out; 540 + return NULL; 536 541 537 542 list_for_each_entry(ei_child, &ei->children, list) { 538 543 if (strcmp(ei_child->name, name) != 0) 539 544 continue; 540 545 /* A child is freed and removed from the list at the same time */ 541 546 if (WARN_ON_ONCE(ei_child->is_freed)) 542 - goto out; 543 - result = lookup_dir_entry(dentry, ei, ei_child); 544 - goto out; 547 + return NULL; 548 + return lookup_dir_entry(dentry, ei, ei_child); 545 549 } 546 550 547 551 for (int i = 0; i < ei->nr_entries; i++) { ··· 553 561 554 562 data = ei->data; 555 563 if (entry->callback(name, &mode, &data, &fops) <= 0) 556 - goto out; 564 + return NULL; 557 565 558 - result = lookup_file_dentry(dentry, ei, i, mode, data, fops); 559 - goto out; 566 + return lookup_file_dentry(dentry, ei, i, mode, data, fops); 567 + 560 568 } 561 - out: 562 - mutex_unlock(&eventfs_mutex); 563 - return result; 569 + return NULL; 564 570 } 565 571 566 572 /* ··· 574 584 struct eventfs_inode *ei; 575 585 const char *name; 576 586 umode_t mode; 577 - int idx; 578 587 int ret = -EINVAL; 579 588 int ino; 580 589 int i, r, c; ··· 587 598 588 599 c = ctx->pos - 2; 589 600 590 - idx = srcu_read_lock(&eventfs_srcu); 601 + guard(srcu)(&eventfs_srcu); 591 602 592 - mutex_lock(&eventfs_mutex); 593 - ei = READ_ONCE(ti->private); 594 - if (ei && ei->is_freed) 595 - ei = NULL; 596 - mutex_unlock(&eventfs_mutex); 597 - 598 - if (!ei) 599 - goto out; 603 + scoped_guard(mutex, &eventfs_mutex) { 604 + ei = READ_ONCE(ti->private); 605 + if (!ei || ei->is_freed) 606 + return -EINVAL; 607 + } 600 608 601 609 /* 602 610 * Need to create the dentries and inodes to have a consistent ··· 608 622 entry = &ei->entries[i]; 609 623 name = entry->name; 610 624 611 - mutex_lock(&eventfs_mutex); 612 625 /* If ei->is_freed then just bail here, nothing more to do */ 613 - if (ei->is_freed) { 614 - mutex_unlock(&eventfs_mutex); 615 - goto out; 626 + scoped_guard(mutex, &eventfs_mutex) { 627 + if (ei->is_freed) 628 + return -EINVAL; 629 + r = entry->callback(name, &mode, &cdata, &fops); 616 630 } 617 - r = entry->callback(name, &mode, &cdata, &fops); 618 - mutex_unlock(&eventfs_mutex); 619 631 if (r <= 0) 620 632 continue; 621 633 622 634 ino = EVENTFS_FILE_INODE_INO; 623 635 624 636 if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) 625 - goto out; 637 + return -EINVAL; 626 638 } 627 639 628 640 /* Subtract the skipped entries above */ ··· 643 659 644 660 ino = eventfs_dir_ino(ei_child); 645 661 646 - if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) 647 - goto out_dec; 662 + if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) { 663 + /* Incremented ctx->pos without adding something, reset it */ 664 + ctx->pos--; 665 + return -EINVAL; 666 + } 648 667 } 649 - ret = 1; 650 - out: 651 - srcu_read_unlock(&eventfs_srcu, idx); 652 - 653 - return ret; 654 - 655 - out_dec: 656 - /* Incremented ctx->pos without adding something, reset it */ 657 - ctx->pos--; 658 - goto out; 668 + return 1; 659 669 } 660 670 661 671 /** ··· 706 728 INIT_LIST_HEAD(&ei->children); 707 729 INIT_LIST_HEAD(&ei->list); 708 730 709 - mutex_lock(&eventfs_mutex); 710 - if (!parent->is_freed) 711 - list_add_tail(&ei->list, &parent->children); 712 - mutex_unlock(&eventfs_mutex); 713 - 731 + scoped_guard(mutex, &eventfs_mutex) { 732 + if (!parent->is_freed) 733 + list_add_tail(&ei->list, &parent->children); 734 + } 714 735 /* Was the parent freed? */ 715 736 if (list_empty(&ei->list)) { 716 737 cleanup_ei(ei); ··· 855 878 if (!ei) 856 879 return; 857 880 858 - mutex_lock(&eventfs_mutex); 881 + guard(mutex)(&eventfs_mutex); 859 882 eventfs_remove_rec(ei, 0); 860 - mutex_unlock(&eventfs_mutex); 861 883 } 862 884 863 885 /**