Skip to content

Commit 4d9b262

Browse files
committed
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>
1 parent c369299 commit 4d9b262

1 file changed

Lines changed: 36 additions & 60 deletions

File tree

fs/tracefs/event_inode.c

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -180,29 +180,25 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
180180
const char *name;
181181
int ret;
182182

183-
mutex_lock(&eventfs_mutex);
183+
guard(mutex)(&eventfs_mutex);
184184
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)
188187
return -ENODEV;
189-
}
190188

191189
/* Preallocate the children mode array if necessary */
192190
if (!(dentry->d_inode->i_mode & S_IFDIR)) {
193191
if (!ei->entry_attrs) {
194192
ei->entry_attrs = kzalloc_objs(*ei->entry_attrs,
195193
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;
200196
}
201197
}
202198

203199
ret = simple_setattr(idmap, dentry, iattr);
204200
if (ret < 0)
205-
goto out;
201+
return ret;
206202

207203
/*
208204
* If this is a dir, then update the ei cache, only the file
@@ -225,8 +221,6 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
225221
}
226222
}
227223
}
228-
out:
229-
mutex_unlock(&eventfs_mutex);
230224
return ret;
231225
}
232226

@@ -528,26 +522,24 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,
528522
struct tracefs_inode *ti;
529523
struct eventfs_inode *ei;
530524
const char *name = dentry->d_name.name;
531-
struct dentry *result = NULL;
532525

533526
ti = get_tracefs(dir);
534527
if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
535528
return ERR_PTR(-EIO);
536529

537-
mutex_lock(&eventfs_mutex);
530+
guard(mutex)(&eventfs_mutex);
538531

539532
ei = ti->private;
540533
if (!ei || ei->is_freed)
541-
goto out;
534+
return NULL;
542535

543536
list_for_each_entry(ei_child, &ei->children, list) {
544537
if (strcmp(ei_child->name, name) != 0)
545538
continue;
546539
/* A child is freed and removed from the list at the same time */
547540
if (WARN_ON_ONCE(ei_child->is_freed))
548-
goto out;
549-
result = lookup_dir_entry(dentry, ei, ei_child);
550-
goto out;
541+
return NULL;
542+
return lookup_dir_entry(dentry, ei, ei_child);
551543
}
552544

553545
for (int i = 0; i < ei->nr_entries; i++) {
@@ -561,14 +553,12 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,
561553

562554
data = ei->data;
563555
if (entry->callback(name, &mode, &data, &fops) <= 0)
564-
goto out;
556+
return NULL;
557+
558+
return lookup_file_dentry(dentry, ei, i, mode, data, fops);
565559

566-
result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
567-
goto out;
568560
}
569-
out:
570-
mutex_unlock(&eventfs_mutex);
571-
return result;
561+
return NULL;
572562
}
573563

574564
/*
@@ -584,7 +574,6 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
584574
struct eventfs_inode *ei;
585575
const char *name;
586576
umode_t mode;
587-
int idx;
588577
int ret = -EINVAL;
589578
int ino;
590579
int i, r, c;
@@ -598,16 +587,13 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
598587

599588
c = ctx->pos - 2;
600589

601-
idx = srcu_read_lock(&eventfs_srcu);
590+
guard(srcu)(&eventfs_srcu);
602591

603-
mutex_lock(&eventfs_mutex);
604-
ei = READ_ONCE(ti->private);
605-
if (ei && ei->is_freed)
606-
ei = NULL;
607-
mutex_unlock(&eventfs_mutex);
608-
609-
if (!ei)
610-
goto out;
592+
scoped_guard(mutex, &eventfs_mutex) {
593+
ei = READ_ONCE(ti->private);
594+
if (!ei || ei->is_freed)
595+
return -EINVAL;
596+
}
611597

612598
/*
613599
* Need to create the dentries and inodes to have a consistent
@@ -622,21 +608,19 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
622608
entry = &ei->entries[i];
623609
name = entry->name;
624610

625-
mutex_lock(&eventfs_mutex);
626611
/* If ei->is_freed then just bail here, nothing more to do */
627-
if (ei->is_freed) {
628-
mutex_unlock(&eventfs_mutex);
629-
goto out;
612+
scoped_guard(mutex, &eventfs_mutex) {
613+
if (ei->is_freed)
614+
return -EINVAL;
615+
r = entry->callback(name, &mode, &cdata, &fops);
630616
}
631-
r = entry->callback(name, &mode, &cdata, &fops);
632-
mutex_unlock(&eventfs_mutex);
633617
if (r <= 0)
634618
continue;
635619

636620
ino = EVENTFS_FILE_INODE_INO;
637621

638622
if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
639-
goto out;
623+
return -EINVAL;
640624
}
641625

642626
/* Subtract the skipped entries above */
@@ -659,19 +643,13 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
659643

660644
ino = eventfs_dir_ino(ei_child);
661645

662-
if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
663-
goto out_dec;
646+
if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) {
647+
/* Incremented ctx->pos without adding something, reset it */
648+
ctx->pos--;
649+
return -EINVAL;
650+
}
664651
}
665-
ret = 1;
666-
out:
667-
srcu_read_unlock(&eventfs_srcu, idx);
668-
669-
return ret;
670-
671-
out_dec:
672-
/* Incremented ctx->pos without adding something, reset it */
673-
ctx->pos--;
674-
goto out;
652+
return 1;
675653
}
676654

677655
/**
@@ -728,11 +706,10 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode
728706
INIT_LIST_HEAD(&ei->children);
729707
INIT_LIST_HEAD(&ei->list);
730708

731-
mutex_lock(&eventfs_mutex);
732-
if (!parent->is_freed)
733-
list_add_tail(&ei->list, &parent->children);
734-
mutex_unlock(&eventfs_mutex);
735-
709+
scoped_guard(mutex, &eventfs_mutex) {
710+
if (!parent->is_freed)
711+
list_add_tail(&ei->list, &parent->children);
712+
}
736713
/* Was the parent freed? */
737714
if (list_empty(&ei->list)) {
738715
cleanup_ei(ei);
@@ -878,9 +855,8 @@ void eventfs_remove_dir(struct eventfs_inode *ei)
878855
if (!ei)
879856
return;
880857

881-
mutex_lock(&eventfs_mutex);
858+
guard(mutex)(&eventfs_mutex);
882859
eventfs_remove_rec(ei, 0);
883-
mutex_unlock(&eventfs_mutex);
884860
}
885861

886862
/**

0 commit comments

Comments
 (0)