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>
This commit is contained in:
Steven Rostedt 2025-06-04 15:16:25 -04:00 committed by Steven Rostedt (Google)
parent c369299895
commit 4d9b262031

View File

@ -180,29 +180,25 @@ static int eventfs_set_attr(struct mnt_idmap *idmap, struct dentry *dentry,
const char *name; const char *name;
int ret; int ret;
mutex_lock(&eventfs_mutex); guard(mutex)(&eventfs_mutex);
ei = dentry->d_fsdata; ei = dentry->d_fsdata;
if (ei->is_freed) { /* Do not allow changes if the event is about to be removed. */
/* Do not allow changes if the event is about to be removed. */ if (ei->is_freed)
mutex_unlock(&eventfs_mutex);
return -ENODEV; return -ENODEV;
}
/* Preallocate the children mode array if necessary */ /* Preallocate the children mode array if necessary */
if (!(dentry->d_inode->i_mode & S_IFDIR)) { if (!(dentry->d_inode->i_mode & S_IFDIR)) {
if (!ei->entry_attrs) { if (!ei->entry_attrs) {
ei->entry_attrs = kzalloc_objs(*ei->entry_attrs, ei->entry_attrs = kzalloc_objs(*ei->entry_attrs,
ei->nr_entries, GFP_NOFS); ei->nr_entries, GFP_NOFS);
if (!ei->entry_attrs) { if (!ei->entry_attrs)
ret = -ENOMEM; return -ENOMEM;
goto out;
}
} }
} }
ret = simple_setattr(idmap, dentry, iattr); ret = simple_setattr(idmap, dentry, iattr);
if (ret < 0) if (ret < 0)
goto out; return ret;
/* /*
* If this is a dir, then update the ei cache, only the file * 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,
} }
} }
} }
out:
mutex_unlock(&eventfs_mutex);
return ret; return ret;
} }
@ -528,26 +522,24 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,
struct tracefs_inode *ti; struct tracefs_inode *ti;
struct eventfs_inode *ei; struct eventfs_inode *ei;
const char *name = dentry->d_name.name; const char *name = dentry->d_name.name;
struct dentry *result = NULL;
ti = get_tracefs(dir); ti = get_tracefs(dir);
if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE))) if (WARN_ON_ONCE(!(ti->flags & TRACEFS_EVENT_INODE)))
return ERR_PTR(-EIO); return ERR_PTR(-EIO);
mutex_lock(&eventfs_mutex); guard(mutex)(&eventfs_mutex);
ei = ti->private; ei = ti->private;
if (!ei || ei->is_freed) if (!ei || ei->is_freed)
goto out; return NULL;
list_for_each_entry(ei_child, &ei->children, list) { list_for_each_entry(ei_child, &ei->children, list) {
if (strcmp(ei_child->name, name) != 0) if (strcmp(ei_child->name, name) != 0)
continue; continue;
/* A child is freed and removed from the list at the same time */ /* A child is freed and removed from the list at the same time */
if (WARN_ON_ONCE(ei_child->is_freed)) if (WARN_ON_ONCE(ei_child->is_freed))
goto out; return NULL;
result = lookup_dir_entry(dentry, ei, ei_child); return lookup_dir_entry(dentry, ei, ei_child);
goto out;
} }
for (int i = 0; i < ei->nr_entries; i++) { for (int i = 0; i < ei->nr_entries; i++) {
@ -561,14 +553,12 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,
data = ei->data; data = ei->data;
if (entry->callback(name, &mode, &data, &fops) <= 0) if (entry->callback(name, &mode, &data, &fops) <= 0)
goto out; return NULL;
return lookup_file_dentry(dentry, ei, i, mode, data, fops);
result = lookup_file_dentry(dentry, ei, i, mode, data, fops);
goto out;
} }
out: return NULL;
mutex_unlock(&eventfs_mutex);
return result;
} }
/* /*
@ -584,7 +574,6 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
struct eventfs_inode *ei; struct eventfs_inode *ei;
const char *name; const char *name;
umode_t mode; umode_t mode;
int idx;
int ret = -EINVAL; int ret = -EINVAL;
int ino; int ino;
int i, r, c; int i, r, c;
@ -598,16 +587,13 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
c = ctx->pos - 2; c = ctx->pos - 2;
idx = srcu_read_lock(&eventfs_srcu); guard(srcu)(&eventfs_srcu);
mutex_lock(&eventfs_mutex); scoped_guard(mutex, &eventfs_mutex) {
ei = READ_ONCE(ti->private); ei = READ_ONCE(ti->private);
if (ei && ei->is_freed) if (!ei || ei->is_freed)
ei = NULL; return -EINVAL;
mutex_unlock(&eventfs_mutex); }
if (!ei)
goto out;
/* /*
* Need to create the dentries and inodes to have a consistent * 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)
entry = &ei->entries[i]; entry = &ei->entries[i];
name = entry->name; name = entry->name;
mutex_lock(&eventfs_mutex);
/* If ei->is_freed then just bail here, nothing more to do */ /* If ei->is_freed then just bail here, nothing more to do */
if (ei->is_freed) { scoped_guard(mutex, &eventfs_mutex) {
mutex_unlock(&eventfs_mutex); if (ei->is_freed)
goto out; return -EINVAL;
r = entry->callback(name, &mode, &cdata, &fops);
} }
r = entry->callback(name, &mode, &cdata, &fops);
mutex_unlock(&eventfs_mutex);
if (r <= 0) if (r <= 0)
continue; continue;
ino = EVENTFS_FILE_INODE_INO; ino = EVENTFS_FILE_INODE_INO;
if (!dir_emit(ctx, name, strlen(name), ino, DT_REG)) if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
goto out; return -EINVAL;
} }
/* Subtract the skipped entries above */ /* Subtract the skipped entries above */
@ -659,19 +643,13 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
ino = eventfs_dir_ino(ei_child); ino = eventfs_dir_ino(ei_child);
if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) {
goto out_dec; /* Incremented ctx->pos without adding something, reset it */
ctx->pos--;
return -EINVAL;
}
} }
ret = 1; return 1;
out:
srcu_read_unlock(&eventfs_srcu, idx);
return ret;
out_dec:
/* Incremented ctx->pos without adding something, reset it */
ctx->pos--;
goto out;
} }
/** /**
@ -728,11 +706,10 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode
INIT_LIST_HEAD(&ei->children); INIT_LIST_HEAD(&ei->children);
INIT_LIST_HEAD(&ei->list); INIT_LIST_HEAD(&ei->list);
mutex_lock(&eventfs_mutex); scoped_guard(mutex, &eventfs_mutex) {
if (!parent->is_freed) if (!parent->is_freed)
list_add_tail(&ei->list, &parent->children); list_add_tail(&ei->list, &parent->children);
mutex_unlock(&eventfs_mutex); }
/* Was the parent freed? */ /* Was the parent freed? */
if (list_empty(&ei->list)) { if (list_empty(&ei->list)) {
cleanup_ei(ei); cleanup_ei(ei);
@ -878,9 +855,8 @@ void eventfs_remove_dir(struct eventfs_inode *ei)
if (!ei) if (!ei)
return; return;
mutex_lock(&eventfs_mutex); guard(mutex)(&eventfs_mutex);
eventfs_remove_rec(ei, 0); eventfs_remove_rec(ei, 0);
mutex_unlock(&eventfs_mutex);
} }
/** /**