Skip to content

Commit b7f8496

Browse files
committed
Merge tag 'tracefs-v7.1-v2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull tracefs updates from Steven Rostedt: - Simplify error handling with guards() Use guards() to simplify the handling of releasing locks in exit paths. - Use dentry name snapshots instead of allocation Instead of allocating a temp buffer to store the dentry name to use in mkdir() and rmdir() use take_dentry_name_snapshot(). - Fix default permissions not being applied at boot The default permissions for tracefs was 0700 to only allow root having access. But after a change to fix other mount options the update to permissions ignored the defined default and used the system default of 0755. This is a regression and is fixed. * tag 'tracefs-v7.1-v2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: tracefs: Removed unused 'ret' variable in eventfs_iterate() tracefs: Fix default permissions not being applied on initial mount tracefs: Use dentry name snapshots instead of heap allocation eventfs: Simplify code using guard()s
2 parents e4bf304 + 43cec30 commit b7f8496

2 files changed

Lines changed: 45 additions & 93 deletions

File tree

fs/tracefs/event_inode.c

Lines changed: 36 additions & 62 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,8 +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;
588-
int ret = -EINVAL;
589577
int ino;
590578
int i, r, c;
591579

@@ -598,22 +586,18 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
598586

599587
c = ctx->pos - 2;
600588

601-
idx = srcu_read_lock(&eventfs_srcu);
589+
guard(srcu)(&eventfs_srcu);
602590

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;
591+
scoped_guard(mutex, &eventfs_mutex) {
592+
ei = READ_ONCE(ti->private);
593+
if (!ei || ei->is_freed)
594+
return -EINVAL;
595+
}
611596

612597
/*
613598
* Need to create the dentries and inodes to have a consistent
614599
* inode number.
615600
*/
616-
ret = 0;
617601

618602
/* Start at 'c' to jump over already read entries */
619603
for (i = c; i < ei->nr_entries; i++, ctx->pos++) {
@@ -622,21 +606,19 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
622606
entry = &ei->entries[i];
623607
name = entry->name;
624608

625-
mutex_lock(&eventfs_mutex);
626609
/* 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;
610+
scoped_guard(mutex, &eventfs_mutex) {
611+
if (ei->is_freed)
612+
return -EINVAL;
613+
r = entry->callback(name, &mode, &cdata, &fops);
630614
}
631-
r = entry->callback(name, &mode, &cdata, &fops);
632-
mutex_unlock(&eventfs_mutex);
633615
if (r <= 0)
634616
continue;
635617

636618
ino = EVENTFS_FILE_INODE_INO;
637619

638620
if (!dir_emit(ctx, name, strlen(name), ino, DT_REG))
639-
goto out;
621+
return -EINVAL;
640622
}
641623

642624
/* Subtract the skipped entries above */
@@ -659,19 +641,13 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)
659641

660642
ino = eventfs_dir_ino(ei_child);
661643

662-
if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR))
663-
goto out_dec;
644+
if (!dir_emit(ctx, name, strlen(name), ino, DT_DIR)) {
645+
/* Incremented ctx->pos without adding something, reset it */
646+
ctx->pos--;
647+
return -EINVAL;
648+
}
664649
}
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;
650+
return 1;
675651
}
676652

677653
/**
@@ -728,11 +704,10 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inode
728704
INIT_LIST_HEAD(&ei->children);
729705
INIT_LIST_HEAD(&ei->list);
730706

731-
mutex_lock(&eventfs_mutex);
732-
if (!parent->is_freed)
733-
list_add_tail(&ei->list, &parent->children);
734-
mutex_unlock(&eventfs_mutex);
735-
707+
scoped_guard(mutex, &eventfs_mutex) {
708+
if (!parent->is_freed)
709+
list_add_tail(&ei->list, &parent->children);
710+
}
736711
/* Was the parent freed? */
737712
if (list_empty(&ei->list)) {
738713
cleanup_ei(ei);
@@ -878,9 +853,8 @@ void eventfs_remove_dir(struct eventfs_inode *ei)
878853
if (!ei)
879854
return;
880855

881-
mutex_lock(&eventfs_mutex);
856+
guard(mutex)(&eventfs_mutex);
882857
eventfs_remove_rec(ei, 0);
883-
mutex_unlock(&eventfs_mutex);
884858
}
885859

886860
/**

fs/tracefs/inode.c

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -94,33 +94,14 @@ static struct tracefs_dir_ops {
9494
int (*rmdir)(const char *name);
9595
} tracefs_ops __ro_after_init;
9696

97-
static char *get_dname(struct dentry *dentry)
98-
{
99-
const char *dname;
100-
char *name;
101-
int len = dentry->d_name.len;
102-
103-
dname = dentry->d_name.name;
104-
name = kmalloc(len + 1, GFP_KERNEL);
105-
if (!name)
106-
return NULL;
107-
memcpy(name, dname, len);
108-
name[len] = 0;
109-
return name;
110-
}
111-
11297
static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,
11398
struct inode *inode, struct dentry *dentry,
11499
umode_t mode)
115100
{
116101
struct tracefs_inode *ti;
117-
char *name;
102+
struct name_snapshot name;
118103
int ret;
119104

120-
name = get_dname(dentry);
121-
if (!name)
122-
return ERR_PTR(-ENOMEM);
123-
124105
/*
125106
* This is a new directory that does not take the default of
126107
* the rootfs. It becomes the default permissions for all the
@@ -135,40 +116,36 @@ static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,
135116
* the files within the tracefs system. It is up to the individual
136117
* mkdir routine to handle races.
137118
*/
119+
take_dentry_name_snapshot(&name, dentry);
138120
inode_unlock(inode);
139-
ret = tracefs_ops.mkdir(name);
121+
ret = tracefs_ops.mkdir(name.name.name);
140122
inode_lock(inode);
141-
142-
kfree(name);
123+
release_dentry_name_snapshot(&name);
143124

144125
return ERR_PTR(ret);
145126
}
146127

147128
static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
148129
{
149-
char *name;
130+
struct name_snapshot name;
150131
int ret;
151132

152-
name = get_dname(dentry);
153-
if (!name)
154-
return -ENOMEM;
155-
156133
/*
157134
* The rmdir call can call the generic functions that create
158135
* the files within the tracefs system. It is up to the individual
159136
* rmdir routine to handle races.
160137
* This time we need to unlock not only the parent (inode) but
161138
* also the directory that is being deleted.
162139
*/
140+
take_dentry_name_snapshot(&name, dentry);
163141
inode_unlock(inode);
164142
inode_unlock(d_inode(dentry));
165143

166-
ret = tracefs_ops.rmdir(name);
144+
ret = tracefs_ops.rmdir(name.name.name);
167145

168146
inode_lock_nested(inode, I_MUTEX_PARENT);
169147
inode_lock(d_inode(dentry));
170-
171-
kfree(name);
148+
release_dentry_name_snapshot(&name);
172149

173150
return ret;
174151
}
@@ -491,6 +468,7 @@ static int tracefs_fill_super(struct super_block *sb, struct fs_context *fc)
491468
return err;
492469

493470
sb->s_op = &tracefs_super_operations;
471+
tracefs_apply_options(sb, false);
494472
set_default_d_op(sb, &tracefs_dentry_operations);
495473

496474
return 0;

0 commit comments

Comments
 (0)