Skip to contentSkip to conversation

tracing: Add new file system tracefs

PATCH Superseded Steven Rostedt 2 participants 40 messages Jan 26, 2015 – Jan 27, 2015

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v3 1/16 debugfs_{mkdir,create,link}(): get rid of redundant argument

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
fs/debugfs/inode.c +9 -8
index 05f2960ed7c3..adaaa04448b3 100644
@@ -89,8 +89,9 @@ static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
 	return error;
 }
 
-static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
+static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
 {
+	struct inode *dir = dentry->d_parent->d_inode;
 	int res;
 
 	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
@@ -102,16 +103,18 @@ static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
 	return res;
 }
 
-static int debugfs_link(struct inode *dir, struct dentry *dentry, umode_t mode,
+static int debugfs_link(struct dentry *dentry, umode_t mode,
 			void *data)
 {
+	struct inode *dir = dentry->d_parent->d_inode;
 	mode = (mode & S_IALLUGO) | S_IFLNK;
 	return debugfs_mknod(dir, dentry, mode, 0, data, NULL);
 }
 
-static int debugfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
+static int debugfs_create(struct dentry *dentry, umode_t mode,
 			  void *data, const struct file_operations *fops)
 {
+	struct inode *dir = dentry->d_parent->d_inode;
 	int res;
 
 	mode = (mode & S_IALLUGO) | S_IFREG;
@@ -329,16 +332,14 @@ static struct dentry *__create_file(const char *name, umode_t mode,
 	if (!IS_ERR(dentry)) {
 		switch (mode & S_IFMT) {
 		case S_IFDIR:
-			error = debugfs_mkdir(parent->d_inode, dentry, mode);
+			error = debugfs_mkdir(dentry, mode);
 
 			break;
 		case S_IFLNK:
-			error = debugfs_link(parent->d_inode, dentry, mode,
-					     data);
+			error = debugfs_link(dentry, mode, data);
 			break;
 		default:
-			error = debugfs_create(parent->d_inode, dentry, mode,
-					       data, fops);
+			error = debugfs_create(dentry, mode, data, fops);
 			break;
 		}
 		dput(dentry);
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH v3 2/16 debugfs: split the beginning and the end of __create_file() off

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 61 ++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 22 deletions(-)
fs/debugfs/inode.c +39 -22
index adaaa04448b3..100186c2bf0a 100644
@@ -305,11 +305,9 @@ static struct file_system_type debug_fs_type = {
 };
 MODULE_ALIAS_FS("debugfs");
 
-static struct dentry *__create_file(const char *name, umode_t mode,
-				    struct dentry *parent, void *data,
-				    const struct file_operations *fops)
+static struct dentry *start_creating(const char *name, struct dentry *parent)
 {
-	struct dentry *dentry = NULL;
+	struct dentry *dentry;
 	int error;
 
 	pr_debug("debugfs: creating file '%s'\n",name);
@@ -317,7 +315,7 @@ static struct dentry *__create_file(const char *name, umode_t mode,
 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
 			      &debugfs_mount_count);
 	if (error)
-		goto exit;
+		return ERR_PTR(error);
 
 	/* If the parent is not specified, we create it in the root.
 	 * We need the root dentry to do this, which is in the super
@@ -329,32 +327,51 @@ static struct dentry *__create_file(const char *name, umode_t mode,
 
 	mutex_lock(&parent->d_inode->i_mutex);
 	dentry = lookup_one_len(name, parent, strlen(name));
-	if (!IS_ERR(dentry)) {
-		switch (mode & S_IFMT) {
-		case S_IFDIR:
-			error = debugfs_mkdir(dentry, mode);
-
-			break;
-		case S_IFLNK:
-			error = debugfs_link(dentry, mode, data);
-			break;
-		default:
-			error = debugfs_create(dentry, mode, data, fops);
-			break;
-		}
+	if (!IS_ERR(dentry) && dentry->d_inode) {
 		dput(dentry);
-	} else
-		error = PTR_ERR(dentry);
-	mutex_unlock(&parent->d_inode->i_mutex);
+		dentry = ERR_PTR(-EEXIST);
+	}
+	if (IS_ERR(dentry))
+		mutex_unlock(&parent->d_inode->i_mutex);
+	return dentry;
+}
+
+static struct dentry *end_creating(struct dentry *dentry, int error)
+{
+	mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
+	dput(dentry);
 
 	if (error) {
 		dentry = NULL;
 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 	}
-exit:
 	return dentry;
 }
 
+static struct dentry *__create_file(const char *name, umode_t mode,
+				    struct dentry *parent, void *data,
+				    const struct file_operations *fops)
+{
+	struct dentry *dentry = start_creating(name, parent);
+	int error;
+
+	if (IS_ERR(dentry))
+		return NULL;
+
+	switch (mode & S_IFMT) {
+	case S_IFDIR:
+		error = debugfs_mkdir(dentry, mode);
+		break;
+	case S_IFLNK:
+		error = debugfs_link(dentry, mode, data);
+		break;
+	default:
+		error = debugfs_create(dentry, mode, data, fops);
+		break;
+	}
+	return end_creating(dentry, error);
+}
+
 /**
  * debugfs_create_file - create a file in the debugfs filesystem
  * @name: a pointer to a string containing the name of the file to create.
Signature · 1 line
2.1.4
author maintainer
#5 lore raw

PATCH v3 5/16 debugfs_mknod(): get rid useless arguments

In reply to Steven Rostedt

From: Al Viro <[email protected]>

dev is always zero, dir was only used to get its ->i_sb, which is equal to ->d_sb of dentry...

---
 fs/debugfs/inode.c | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)
fs/debugfs/inode.c +11 -17
index c69e00d69ff1..bc02e2096977 100644
@@ -70,23 +70,18 @@ static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev
 }
 
 /* SMP-safe */
-static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
-			 umode_t mode, dev_t dev, void *data,
+static int debugfs_mknod(struct dentry *dentry,
+			 umode_t mode, void *data,
 			 const struct file_operations *fops)
 {
 	struct inode *inode;
-	int error = -EPERM;
 
-	if (dentry->d_inode)
-		return -EEXIST;
-
-	inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
-	if (inode) {
-		d_instantiate(dentry, inode);
-		dget(dentry);
-		error = 0;
-	}
-	return error;
+	inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops);
+	if (unlikely(!inode))
+		return -EPERM;
+	d_instantiate(dentry, inode);
+	dget(dentry);
+	return 0;
 }
 
 static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
@@ -95,7 +90,7 @@ static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
 	int res;
 
 	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
-	res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL);
+	res = debugfs_mknod(dentry, mode, NULL, NULL);
 	if (!res) {
 		inc_nlink(dir);
 		fsnotify_mkdir(dir, dentry);
@@ -110,7 +105,7 @@ static int debugfs_create(struct dentry *dentry, umode_t mode,
 	int res;
 
 	mode = (mode & S_IALLUGO) | S_IFREG;
-	res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
+	res = debugfs_mknod(dentry, mode, data, fops);
 	if (!res)
 		fsnotify_create(dir, dentry);
 	return res;
@@ -458,8 +453,7 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 		return NULL;
 	}
 
-	error = debugfs_mknod(dentry->d_parent->d_inode, dentry,
-			      S_IFLNK | S_IRWXUGO, 0, link, NULL);
+	error = debugfs_mknod(dentry, S_IFLNK | S_IRWXUGO, link, NULL);
 	if (error)
 		kfree(link);
 
Signature · 1 line
2.1.4
author maintainer
#4 lore raw

PATCH v3 4/16 fold debugfs_link() into caller

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
fs/debugfs/inode.c +2 -9
index 18564b08884c..c69e00d69ff1 100644
@@ -103,14 +103,6 @@ static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
 	return res;
 }
 
-static int debugfs_link(struct dentry *dentry, umode_t mode,
-			void *data)
-{
-	struct inode *dir = dentry->d_parent->d_inode;
-	mode = (mode & S_IALLUGO) | S_IFLNK;
-	return debugfs_mknod(dir, dentry, mode, 0, data, NULL);
-}
-
 static int debugfs_create(struct dentry *dentry, umode_t mode,
 			  void *data, const struct file_operations *fops)
 {
@@ -466,7 +458,8 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 		return NULL;
 	}
 
-	error = debugfs_link(dentry, S_IFLNK | S_IRWXUGO, link);
+	error = debugfs_mknod(dentry->d_parent->d_inode, dentry,
+			      S_IFLNK | S_IRWXUGO, 0, link, NULL);
 	if (error)
 		kfree(link);
 
Signature · 1 line
2.1.4
author maintainer
#13 lore raw

PATCH v3 13/16 tracing: Convert the tracing facility over to use tracefs

In reply to Steven Rostedt

From: "Steven Rostedt (Red Hat)" <[email protected]>

debugfs was fine for the tracing facility as a quick way to get an interface. Now that tracing has matured, it should separate itself from debugfs such that it can be mounted separately without needing to mount all of debugfs with it. That is, users resist using tracing because it requires mounting debugfs. Having tracing have its own file system lets users get the features of tracing without needing to bring in the rest of the kernel's debug infrastructure.

Another reason for tracefs is that debubfs does not support mkdir. Currently, to create instances, one does a mkdir in the tracing/instance directory. This is implemented via a hack that forces debugfs to do something it is not intended on doing. By converting over to tracefs, this hack can be removed and mkdir can be properly implemented. This patch does not address this yet, but it lays the ground work for that to be done.

---
 kernel/trace/ftrace.c                | 22 ++++++-------
 kernel/trace/trace.c                 | 61 ++++++++++++++++++++++--------------
 kernel/trace/trace.h                 |  2 +-
 kernel/trace/trace_events.c          | 32 +++++++++----------
 kernel/trace/trace_functions_graph.c |  7 ++---
 kernel/trace/trace_kprobe.c          | 10 +++---
 kernel/trace/trace_probe.h           |  2 +-
 kernel/trace/trace_stat.c            | 10 +++---
 8 files changed, 79 insertions(+), 67 deletions(-)
kernel/trace/ftrace.c +11 -11
index 80c9d34540dd..e3596de88fc1 100644
@@ -18,7 +18,7 @@
 #include <linux/kallsyms.h>
 #include <linux/seq_file.h>
 #include <linux/suspend.h>
-#include <linux/debugfs.h>
+#include <linux/tracefs.h>
 #include <linux/hardirq.h>
 #include <linux/kthread.h>
 #include <linux/uaccess.h>
@@ -1008,7 +1008,7 @@ static struct tracer_stat function_stats __initdata = {
 	.stat_show	= function_stat_show
 };
 
-static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
+static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
 {
 	struct ftrace_profile_stat *stat;
 	struct dentry *entry;
@@ -1044,15 +1044,15 @@ static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
 		}
 	}
 
-	entry = debugfs_create_file("function_profile_enabled", 0644,
+	entry = tracefs_create_file("function_profile_enabled", 0644,
 				    d_tracer, NULL, &ftrace_profile_fops);
 	if (!entry)
-		pr_warning("Could not create debugfs "
+		pr_warning("Could not create tracefs "
 			   "'function_profile_enabled' entry\n");
 }
 
 #else /* CONFIG_FUNCTION_PROFILER */
-static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
+static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
 {
 }
 #endif /* CONFIG_FUNCTION_PROFILER */
@@ -4653,7 +4653,7 @@ void ftrace_destroy_filter_files(struct ftrace_ops *ops)
 	mutex_unlock(&ftrace_lock);
 }
 
-static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
+static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
 {
 
 	trace_create_file("available_filter_functions", 0444,
@@ -4961,7 +4961,7 @@ static int __init ftrace_nodyn_init(void)
 }
 core_initcall(ftrace_nodyn_init);
 
-static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
+static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
 static inline void ftrace_startup_enable(int command) { }
 static inline void ftrace_startup_all(int command) { }
 /* Keep as macros so we do not need to define the commands */
@@ -5414,7 +5414,7 @@ static const struct file_operations ftrace_pid_fops = {
 	.release	= ftrace_pid_release,
 };
 
-static __init int ftrace_init_debugfs(void)
+static __init int ftrace_init_tracefs(void)
 {
 	struct dentry *d_tracer;
 
@@ -5422,16 +5422,16 @@ static __init int ftrace_init_debugfs(void)
 	if (IS_ERR(d_tracer))
 		return 0;
 
-	ftrace_init_dyn_debugfs(d_tracer);
+	ftrace_init_dyn_tracefs(d_tracer);
 
 	trace_create_file("set_ftrace_pid", 0644, d_tracer,
 			    NULL, &ftrace_pid_fops);
 
-	ftrace_profile_debugfs(d_tracer);
+	ftrace_profile_tracefs(d_tracer);
 
 	return 0;
 }
-fs_initcall(ftrace_init_debugfs);
+fs_initcall(ftrace_init_tracefs);
 
 /**
  * ftrace_kill - kill ftrace
kernel/trace/trace.c +37 -24

188 lines not on this page · raw message

kernel/trace/trace.h +1 -1
index 0eddfeb05fee..ba1170cb4880 100644
@@ -334,7 +334,7 @@ struct tracer_flags {
 
 
 /**
- * struct tracer - a specific tracer and its callbacks to interact with debugfs
+ * struct tracer - a specific tracer and its callbacks to interact with tracefs
  * @name: the name chosen to select it on the available_tracers file
  * @init: called when one switches to this tracer (echo name > current_tracer)
  * @reset: called when one switches to another tracer
kernel/trace/trace_events.c +16 -16
index 4ff8c1394017..e3b7782f904f 100644
@@ -13,7 +13,7 @@
 #include <linux/workqueue.h>
 #include <linux/spinlock.h>
 #include <linux/kthread.h>
-#include <linux/debugfs.h>
+#include <linux/tracefs.h>
 #include <linux/uaccess.h>
 #include <linux/module.h>
 #include <linux/ctype.h>
@@ -480,7 +480,7 @@ static void remove_subsystem(struct ftrace_subsystem_dir *dir)
 		return;
 
 	if (!--dir->nr_events) {
-		debugfs_remove_recursive(dir->entry);
+		tracefs_remove_recursive(dir->entry);
 		list_del(&dir->list);
 		__put_system_dir(dir);
 	}
@@ -499,7 +499,7 @@ static void remove_event_file_dir(struct ftrace_event_file *file)
 		}
 		spin_unlock(&dir->d_lock);
 
-		debugfs_remove_recursive(dir);
+		tracefs_remove_recursive(dir);
 	}
 
 	list_del(&file->list);
@@ -1526,7 +1526,7 @@ event_subsystem_dir(struct trace_array *tr, const char *name,
 	} else
 		__get_system(system);
 
-	dir->entry = debugfs_create_dir(name, parent);
+	dir->entry = tracefs_create_dir(name, parent);
 	if (!dir->entry) {
 		pr_warn("Failed to create system directory %s\n", name);
 		__put_system(system);
@@ -1539,12 +1539,12 @@ event_subsystem_dir(struct trace_array *tr, const char *name,
 	dir->subsystem = system;
 	file->system = dir;
 
-	entry = debugfs_create_file("filter", 0644, dir->entry, dir,
+	entry = tracefs_create_file("filter", 0644, dir->entry, dir,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry) {
 		kfree(system->filter);
 		system->filter = NULL;
-		pr_warn("Could not create debugfs '%s/filter' entry\n", name);
+		pr_warn("Could not create tracefs '%s/filter' entry\n", name);
 	}
 
 	trace_create_file("enable", 0644, dir->entry, dir,
@@ -1585,9 +1585,9 @@ event_create_dir(struct dentry *parent, struct ftrace_event_file *file)
 		d_events = parent;
 
 	name = ftrace_event_name(call);
-	file->dir = debugfs_create_dir(name, d_events);
+	file->dir = tracefs_create_dir(name, d_events);
 	if (!file->dir) {
-		pr_warn("Could not create debugfs '%s' directory\n", name);
+		pr_warn("Could not create tracefs '%s' directory\n", name);
 		return -1;
 	}
 
@@ -2228,7 +2228,7 @@ static inline int register_event_cmds(void) { return 0; }
 /*
  * The top level array has already had its ftrace_event_file
  * descriptors created in order to allow for early events to
- * be recorded. This function is called after the debugfs has been
+ * be recorded. This function is called after the tracefs has been
  * initialized, and we now have to create the files associated
  * to the events.
  */
@@ -2311,16 +2311,16 @@ create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
 	struct dentry *d_events;
 	struct dentry *entry;
 
-	entry = debugfs_create_file("set_event", 0644, parent,
+	entry = tracefs_create_file("set_event", 0644, parent,
 				    tr, &ftrace_set_event_fops);
 	if (!entry) {
-		pr_warn("Could not create debugfs 'set_event' entry\n");
+		pr_warn("Could not create tracefs 'set_event' entry\n");
 		return -ENOMEM;
 	}
 
-	d_events = debugfs_create_dir("events", parent);
+	d_events = tracefs_create_dir("events", parent);
 	if (!d_events) {
-		pr_warn("Could not create debugfs 'events' directory\n");
+		pr_warn("Could not create tracefs 'events' directory\n");
 		return -ENOMEM;
 	}
 
@@ -2412,7 +2412,7 @@ int event_trace_del_tracer(struct trace_array *tr)
 
 	down_write(&trace_event_sem);
 	__trace_remove_event_dirs(tr);
-	debugfs_remove_recursive(tr->event_dir);
+	tracefs_remove_recursive(tr->event_dir);
 	up_write(&trace_event_sem);
 
 	tr->event_dir = NULL;
@@ -2493,10 +2493,10 @@ static __init int event_trace_init(void)
 	if (IS_ERR(d_tracer))
 		return 0;
 
-	entry = debugfs_create_file("available_events", 0444, d_tracer,
+	entry = tracefs_create_file("available_events", 0444, d_tracer,
 				    tr, &ftrace_avail_fops);
 	if (!entry)
-		pr_warn("Could not create debugfs 'available_events' entry\n");
+		pr_warn("Could not create tracefs 'available_events' entry\n");
 
 	if (trace_define_common_fields())
 		pr_warn("tracing: Failed to allocate common fields");
kernel/trace/trace_functions_graph.c +3 -4
index 2d25ad1526bb..9cfea4c6d314 100644
@@ -6,7 +6,6 @@
  * is Copyright (c) Steven Rostedt <[email protected]>
  *
  */
-#include <linux/debugfs.h>
 #include <linux/uaccess.h>
 #include <linux/ftrace.h>
 #include <linux/slab.h>
@@ -151,7 +150,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
 	 * The curr_ret_stack is initialized to -1 and get increased
 	 * in this function.  So it can be less than -1 only if it was
 	 * filtered out via ftrace_graph_notrace_addr() which can be
-	 * set from set_graph_notrace file in debugfs by user.
+	 * set from set_graph_notrace file in tracefs by user.
 	 */
 	if (current->curr_ret_stack < -1)
 		return -EBUSY;
@@ -1432,7 +1431,7 @@ static const struct file_operations graph_depth_fops = {
 	.llseek		= generic_file_llseek,
 };
 
-static __init int init_graph_debugfs(void)
+static __init int init_graph_tracefs(void)
 {
 	struct dentry *d_tracer;
 
@@ -1445,7 +1444,7 @@ static __init int init_graph_debugfs(void)
 
 	return 0;
 }
-fs_initcall(init_graph_debugfs);
+fs_initcall(init_graph_tracefs);
 
 static __init int init_graph_trace(void)
 {
kernel/trace/trace_kprobe.c +5 -5
index b4a00def88f5..c1c6655847c8 100644
@@ -1310,7 +1310,7 @@ static int unregister_kprobe_event(struct trace_kprobe *tk)
 	return ret;
 }
 
-/* Make a debugfs interface for controlling probe points */
+/* Make a tracefs interface for controlling probe points */
 static __init int init_kprobe_trace(void)
 {
 	struct dentry *d_tracer;
@@ -1323,20 +1323,20 @@ static __init int init_kprobe_trace(void)
 	if (IS_ERR(d_tracer))
 		return 0;
 
-	entry = debugfs_create_file("kprobe_events", 0644, d_tracer,
+	entry = tracefs_create_file("kprobe_events", 0644, d_tracer,
 				    NULL, &kprobe_events_ops);
 
 	/* Event list interface */
 	if (!entry)
-		pr_warning("Could not create debugfs "
+		pr_warning("Could not create tracefs "
 			   "'kprobe_events' entry\n");
 
 	/* Profile interface */
-	entry = debugfs_create_file("kprobe_profile", 0444, d_tracer,
+	entry = tracefs_create_file("kprobe_profile", 0444, d_tracer,
 				    NULL, &kprobe_profile_ops);
 
 	if (!entry)
-		pr_warning("Could not create debugfs "
+		pr_warning("Could not create tracefs "
 			   "'kprobe_profile' entry\n");
 	return 0;
 }
kernel/trace/trace_probe.h +1 -1
index 4f815fbce16d..19aff635841a 100644
@@ -25,7 +25,7 @@
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 #include <linux/smp.h>
-#include <linux/debugfs.h>
+#include <linux/tracefs.h>
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/ctype.h>
kernel/trace/trace_stat.c +5 -5
index 75e19e86c954..6cf935316769 100644
@@ -12,7 +12,7 @@
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/rbtree.h>
-#include <linux/debugfs.h>
+#include <linux/tracefs.h>
 #include "trace_stat.h"
 #include "trace.h"
 
@@ -65,7 +65,7 @@ static void reset_stat_session(struct stat_session *session)
 
 static void destroy_session(struct stat_session *session)
 {
-	debugfs_remove(session->file);
+	tracefs_remove(session->file);
 	__reset_stat_session(session);
 	mutex_destroy(&session->stat_mutex);
 	kfree(session);
@@ -279,9 +279,9 @@ static int tracing_stat_init(void)
 	if (IS_ERR(d_tracing))
 		return 0;
 
-	stat_dir = debugfs_create_dir("trace_stat", d_tracing);
+	stat_dir = tracefs_create_dir("trace_stat", d_tracing);
 	if (!stat_dir)
-		pr_warning("Could not create debugfs "
+		pr_warning("Could not create tracefs "
 			   "'trace_stat' entry\n");
 	return 0;
 }
@@ -291,7 +291,7 @@ static int init_stat_file(struct stat_session *session)
 	if (!stat_dir && tracing_stat_init())
 		return -ENODEV;
 
-	session->file = debugfs_create_file(session->ts->name, 0644,
+	session->file = tracefs_create_file(session->ts->name, 0644,
 					    stat_dir,
 					    session, &tracing_stat_fops);
 	if (!session->file)
Signature · 1 line
2.1.4
author maintainer
#16 lore raw

PATCH v3 16/16 tracing: Have mkdir and rmdir be part of tracefs

In reply to Steven Rostedt

From: "Steven Rostedt (Red Hat)" <[email protected]>

The tracing "instances" directory can create sub tracing buffers with mkdir, and remove them with rmdir. As a mkdir will also create all the files and directories that control the sub buffer the inode mutexes need to be released before this is done, to avoid deadlocks. It is better to let the tracing system unlock the inode mutexes before calling the functions that create the files within the new directory (or deletes the files from the one being destroyed).

Now that tracing has been converted over to tracefs, the tracefs file system can be modified to accommodate this feature. It still releases the locks, but the filesystem itself can take care of the ugly business and let the user just do what it needs.

The tracing system now attaches a descriptor to the directory dentry that can have userspace create or remove sub directories. If this descriptor does not exist for a dentry, then that dentry can not be used to create other directories. This descriptor holds a mkdir and rmdir method that only takes a character string as an argument.

The tracefs file system will first make a copy of the dentry name before releasing the locks. Then it will pass the copied name to the methods. It is up to the tracing system that supplied the methods to handle races with duplicate names and such as all the inode mutexes would be released when the functions are called.

---
 fs/tracefs/inode.c      | 93 ++++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/tracefs.h |  7 ++++
 kernel/trace/trace.c    | 68 +++---------------------------------
 3 files changed, 103 insertions(+), 65 deletions(-)
fs/tracefs/inode.c +92 -1
index cdbaa42b44a1..a005b951fd85 100644
@@ -50,6 +50,87 @@ static const struct file_operations tracefs_file_operations = {
 	.llseek =	noop_llseek,
 };
 
+static char *get_dname(struct dentry *dentry)
+{
+	const char *dname;
+	char *name;
+	int len = dentry->d_name.len;
+
+	dname = dentry->d_name.name;
+	name = kmalloc(len + 1, GFP_KERNEL);
+	if (!name)
+		return NULL;
+	memcpy(name, dname, len);
+	name[len] = 0;
+	return name;
+}
+
+static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
+{
+	const struct tracefs_dir_ops *ops = inode ? inode->i_private : NULL;
+	char *name;
+	int ret;
+
+	if (!ops)
+		return -EPERM;
+
+	name = get_dname(dentry);
+	if (!name)
+		return -ENOMEM;
+
+	/*
+	 * The mkdir call can call the generic functions that create
+	 * the files within the tracefs system. It is up to the individual
+	 * mkdir routine to handle races.
+	 */
+	mutex_unlock(&inode->i_mutex);
+	ret = ops->mkdir(name);
+	mutex_lock(&inode->i_mutex);
+
+	kfree(name);
+
+	return ret;
+}
+
+static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
+{
+	const struct tracefs_dir_ops *ops = inode->i_private;
+	char *name;
+	int ret;
+
+	if (!ops)
+		return -EPERM;
+
+	name = get_dname(dentry);
+	if (!name)
+		return -ENOMEM;
+
+	/*
+	 * The rmdir call can call the generic functions that create
+	 * the files within the tracefs system. It is up to the individual
+	 * rmdir routine to handle races.
+	 * This time we need to unlock not only the parent (inode) but
+	 * also the directory that is being deleted.
+	 */
+	mutex_unlock(&inode->i_mutex);
+	mutex_unlock(&dentry->d_inode->i_mutex);
+
+	ret = ops->rmdir(name);
+
+	mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
+	mutex_lock(&dentry->d_inode->i_mutex);
+
+	kfree(name);
+
+	return ret;
+}
+
+const struct inode_operations tracefs_dir_inode_operations = {
+	.lookup		= simple_lookup,
+	.mkdir		= tracefs_syscall_mkdir,
+	.rmdir		= tracefs_syscall_rmdir,
+};
+
 static struct inode *tracefs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
 				      void *data, const struct file_operations *fops)
 
@@ -69,7 +150,7 @@ static struct inode *tracefs_get_inode(struct super_block *sb, umode_t mode, dev
 			inode->i_private = data;
 			break;
 		case S_IFDIR:
-			inode->i_op = &simple_dir_inode_operations;
+			inode->i_op = &tracefs_dir_inode_operations;
 			inode->i_fop = &simple_dir_operations;
 
 			/* directory inodes start off with i_nlink == 2
@@ -125,6 +206,16 @@ static int tracefs_create(struct inode *dir, struct dentry *dentry, umode_t mode
 	return res;
 }
 
+void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops)
+{
+	struct inode *inode = dentry->d_inode;
+
+	if (!inode)
+		return;
+
+	inode->i_private = (void *)ops;
+}
+
 struct tracefs_mount_opts {
 	kuid_t uid;
 	kgid_t gid;
include/linux/tracefs.h +7 -0
index 23e04ce21749..d142b1f9d453 100644
@@ -34,6 +34,13 @@ struct dentry *tracefs_create_dir(const char *name, struct dentry *parent);
 void tracefs_remove(struct dentry *dentry);
 void tracefs_remove_recursive(struct dentry *dentry);
 
+struct tracefs_dir_ops {
+	int (*mkdir)(const char *name);
+	int (*rmdir)(const char *name);
+};
+
+void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops);
+
 bool tracefs_initialized(void);
 
 #endif /* CONFIG_TRACING */
kernel/trace/trace.c +4 -64

92 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#14 lore raw

PATCH v3 14/16 tracing: Automatically mount tracefs on debugfs/tracing

In reply to Steven Rostedt

From: "Steven Rostedt (Red Hat)" <[email protected]>

As tools currently rely on the tracing directory in debugfs, we can not just created a tracefs infrastructure and expect sysadmins to mount the new tracefs to have their old tools work.

Instead, the debugfs tracing directory is still created and the tracefs file system is mounted there when the debugfs filesystem is mounted.

No longer does the tracing infrastructure update the debugfs file system, but instead interacts with the tracefs file system. But now, it still appears to the user like nothing changed, except you also have the feature of mounting just the tracing system without needing all of debugfs!

---
 kernel/trace/trace.c | 36 +++++++++++++++++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)
kernel/trace/trace.c +35 -1
index fb577a2a60ea..623791e15966 100644
@@ -32,6 +32,7 @@
 #include <linux/splice.h>
 #include <linux/kdebug.h>
 #include <linux/string.h>
+#include <linux/mount.h>
 #include <linux/rwsem.h>
 #include <linux/slab.h>
 #include <linux/ctype.h>
@@ -5815,10 +5816,34 @@ static __init int register_snapshot_cmd(void)
 static inline __init int register_snapshot_cmd(void) { return 0; }
 #endif /* defined(CONFIG_TRACER_SNAPSHOT) && defined(CONFIG_DYNAMIC_FTRACE) */
 
+static struct vfsmount *trace_automount(void *ingore)
+{
+	struct vfsmount *mnt;
+	struct file_system_type *type;
+
+	/*
+	 * To maintain backward compatibility for tools that mount
+	 * debugfs to get to the tracing facility, tracefs is automatically
+	 * mounted to the debugfs/tracing directory.
+	 */
+	type = get_fs_type("tracefs");
+	if (!type)
+		return NULL;
+	mnt = vfs_kern_mount(type, 0, "tracefs", NULL);
+	put_filesystem(type);
+	if (IS_ERR(mnt))
+		return NULL;
+	mntget(mnt);
+
+	return mnt;
+}
+
 #define TRACE_TOP_DIR_ENTRY		((struct dentry *)1)
 
 struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
 {
+	struct dentry *traced;
+
 	/* Top entry does not have a descriptor */
 	if (tr->dir == TRACE_TOP_DIR_ENTRY)
 		return NULL;
@@ -5831,7 +5856,16 @@ struct dentry *tracing_init_dentry_tr(struct trace_array *tr)
 		return ERR_PTR(-ENODEV);
 
 	if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
-		tr->dir = debugfs_create_dir("tracing", NULL);
+		/*
+		 * As there may still be users that expect the tracing
+		 * files to exist in debugfs/tracing, we must automount
+		 * the tracefs file system there, so older tools still
+		 * work with the newer kerenl.
+		 */
+		traced = debugfs_create_automount("tracing", NULL,
+						  trace_automount, NULL);
+		if (!traced)
+			return ERR_PTR(-ENOMEM);
 		tr->dir = TRACE_TOP_DIR_ENTRY;
 		return NULL;
 	}
Signature · 1 line
2.1.4
author maintainer
#15 lore raw

PATCH v3 15/16 tracefs: Add directory /sys/kernel/tracing

In reply to Steven Rostedt

From: "Steven Rostedt (Red Hat)" <[email protected]>

When tracefs is configured, have the directory /sys/kernel/tracing appear just like /sys/kernel/debug appears when debugfs is configured.

This will give a consistent place for system admins to mount tracefs.

---
 fs/tracefs/inode.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/tracefs/inode.c +7 -0
index c31997a303c7..cdbaa42b44a1 100644
@@ -16,6 +16,7 @@
 #include <linux/module.h>
 #include <linux/fs.h>
 #include <linux/mount.h>
+#include <linux/kobject.h>
 #include <linux/namei.h>
 #include <linux/tracefs.h>
 #include <linux/fsnotify.h>
@@ -547,10 +548,16 @@ bool tracefs_initialized(void)
 	return tracefs_registered;
 }
 
+static struct kobject *trace_kobj;
+
 static int __init tracefs_init(void)
 {
 	int retval;
 
+	trace_kobj = kobject_create_and_add("tracing", kernel_kobj);
+	if (!trace_kobj)
+		return -EINVAL;
+
 	retval = register_filesystem(&trace_fs_type);
 	if (!retval)
 		tracefs_registered = true;
Signature · 1 line
2.1.4
author maintainer
#12 lore raw

PATCH v3 12/16 tracefs: Add new tracefs file system

In reply to Steven Rostedt

From: "Steven Rostedt (Red Hat)" <[email protected]>

Add a separate file system to handle the tracing directory. Currently it is part of debugfs, but that is starting to show its limits.

One thing is that in order to access the tracing infrastructure, you need to mount debugfs. As that includes debugging from all sorts of sub systems in the kernel, it is not considered advisable to mount such an all encompassing debugging system.

Having the tracing system in its own file systems gives access to the tracing sub system without needing to include all other systems.

Another problem with tracing using the debugfs system is that the instances use mkdir to create sub buffers. debugfs does not support mkdir from userspace so to implement it, special hacks were used. By controlling the file system that the tracing infrastructure uses, this can be properly done without hacks.

---
 fs/Makefile                |   1 +
 fs/tracefs/Makefile        |   4 +
 fs/tracefs/inode.c         | 560 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/tracefs.h    |  41 ++++
 include/uapi/linux/magic.h |   2 +
 5 files changed, 608 insertions(+)
 create mode 100644 fs/tracefs/Makefile
 create mode 100644 fs/tracefs/inode.c
 create mode 100644 include/linux/tracefs.h
fs/Makefile +1 -0
index bedff48e8fdc..d244b8d973ac 100644
@@ -118,6 +118,7 @@ obj-$(CONFIG_HOSTFS)		+= hostfs/
 obj-$(CONFIG_HPPFS)		+= hppfs/
 obj-$(CONFIG_CACHEFILES)	+= cachefiles/
 obj-$(CONFIG_DEBUG_FS)		+= debugfs/
+obj-$(CONFIG_TRACING)		+= tracefs/
 obj-$(CONFIG_OCFS2_FS)		+= ocfs2/
 obj-$(CONFIG_BTRFS_FS)		+= btrfs/
 obj-$(CONFIG_GFS2_FS)           += gfs2/
fs/tracefs/Makefile new file +4 -0
new file mode 100644
index 000000000000..82fa35b656c4
@@ -0,0 +1,4 @@
+tracefs-objs	:= inode.o
+
+obj-$(CONFIG_TRACING)	+= tracefs.o
+
fs/tracefs/inode.c new file +560 -0

560 lines not on this page · raw message

include/linux/tracefs.h new file +41 -0

41 lines not on this page · raw message

include/uapi/linux/magic.h +2 -0
index 7d664ea85ebd..7b1425a6b370 100644
@@ -58,6 +58,8 @@
 
 #define STACK_END_MAGIC		0x57AC6E9D
 
+#define TRACEFS_MAGIC          0x74726163
+
 #define V9FS_MAGIC		0x01021997
 
 #define BDEVFS_MAGIC            0x62646576
Signature · 1 line
2.1.4
author maintainer
#11 lore raw

PATCH v3 11/16 new primitive: debugfs_create_automount()

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c      | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/debugfs.h |  5 +++++
 2 files changed, 53 insertions(+)
fs/debugfs/inode.c +48 -0
index 1219dff8e18f..957c40ce09f6 100644
@@ -175,6 +175,18 @@ static const struct super_operations debugfs_super_operations = {
 	.show_options	= debugfs_show_options,
 };
 
+static struct vfsmount *debugfs_automount(struct path *path)
+{
+	struct vfsmount *(*f)(void *);
+	f = (struct vfsmount *(*)(void *))path->dentry->d_fsdata;
+	return f(path->dentry->d_inode->i_private);
+}
+
+static const struct dentry_operations debugfs_dops = {
+	.d_delete = always_delete_dentry,
+	.d_automount = debugfs_automount,
+};
+
 static int debug_fill_super(struct super_block *sb, void *data, int silent)
 {
 	static struct tree_descr debug_files[] = {{""}};
@@ -199,6 +211,7 @@ static int debug_fill_super(struct super_block *sb, void *data, int silent)
 		goto fail;
 
 	sb->s_op = &debugfs_super_operations;
+	sb->s_d_op = &debugfs_dops;
 
 	debugfs_apply_options(sb);
 
@@ -368,6 +381,41 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 EXPORT_SYMBOL_GPL(debugfs_create_dir);
 
 /**
+ * debugfs_create_automount - create automount point in the debugfs filesystem
+ * @name: a pointer to a string containing the name of the file to create.
+ * @parent: a pointer to the parent dentry for this file.  This should be a
+ *          directory dentry if set.  If this parameter is NULL, then the
+ *          file will be created in the root of the debugfs filesystem.
+ * @f: function to be called when pathname resolution steps on that one.
+ * @data: opaque argument to pass to f().
+ *
+ * @f should return what ->d_automount() would.
+ */
+struct dentry *debugfs_create_automount(const char *name,
+					struct dentry *parent,
+					struct vfsmount *(*f)(void *),
+					void *data)
+{
+	struct dentry *dentry = start_creating(name, parent);
+	struct inode *inode;
+
+	if (IS_ERR(dentry))
+		return NULL;
+
+	inode = debugfs_get_inode(dentry->d_sb);
+	if (unlikely(!inode))
+		return failed_creating(dentry);
+
+	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
+	inode->i_flags |= S_AUTOMOUNT;
+	inode->i_private = data;
+	dentry->d_fsdata = (void *)f;
+	d_instantiate(dentry, inode);
+	return end_creating(dentry);
+}
+EXPORT_SYMBOL(debugfs_create_automount);
+
+/**
  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  * @name: a pointer to a string containing the name of the symbolic link to
  *        create.
include/linux/debugfs.h +5 -0
index da4c4983adbe..ea149a24a1f2 100644
@@ -56,6 +56,11 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 				      const char *dest);
 
+struct dentry *debugfs_create_automount(const char *name,
+					struct dentry *parent,
+					struct vfsmount *(*f)(void *),
+					void *data);
+
 void debugfs_remove(struct dentry *dentry);
 void debugfs_remove_recursive(struct dentry *dentry);
 
Signature · 1 line
2.1.4
author maintainer
#9 lore raw

PATCH v3 9/16 debugfs: take mode-dependent parts of debugfs_get_inode() into callers

In reply to Steven Rostedt

From: Al Viro <[email protected]>

... and trim the arguments list

---
 fs/debugfs/inode.c | 48 ++++++++++++++++--------------------------------
 1 file changed, 16 insertions(+), 32 deletions(-)
fs/debugfs/inode.c +16 -32
index b765c04eba20..61e9a6815a19 100644
@@ -34,37 +34,12 @@ static struct vfsmount *debugfs_mount;
 static int debugfs_mount_count;
 static bool debugfs_registered;
 
-static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
-				       void *data, const struct file_operations *fops)
-
+static struct inode *debugfs_get_inode(struct super_block *sb)
 {
 	struct inode *inode = new_inode(sb);
-
 	if (inode) {
 		inode->i_ino = get_next_ino();
-		inode->i_mode = mode;
 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
-		switch (mode & S_IFMT) {
-		default:
-			init_special_inode(inode, mode, dev);
-			break;
-		case S_IFREG:
-			inode->i_fop = fops ? fops : &debugfs_file_operations;
-			inode->i_private = data;
-			break;
-		case S_IFLNK:
-			inode->i_op = &debugfs_link_operations;
-			inode->i_private = data;
-			break;
-		case S_IFDIR:
-			inode->i_op = &simple_dir_inode_operations;
-			inode->i_fop = &simple_dir_operations;
-
-			/* directory inodes start off with i_nlink == 2
-			 * (for "." entry) */
-			inc_nlink(inode);
-			break;
-		}
 	}
 	return inode;
 }
@@ -334,10 +309,13 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 	if (IS_ERR(dentry))
 		return NULL;
 
-	inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops);
+	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode))
 		return end_creating(dentry, -ENOMEM);
 
+	inode->i_mode = mode;
+	inode->i_fop = fops ? fops : &debugfs_file_operations;
+	inode->i_private = data;
 	d_instantiate(dentry, inode);
 	dget(dentry);
 	fsnotify_create(dentry->d_parent->d_inode, dentry);
@@ -371,12 +349,16 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 	if (IS_ERR(dentry))
 		return NULL;
 
-	inode = debugfs_get_inode(dentry->d_sb,
-				  S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
-				  0, NULL, NULL);
+	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode))
 		return end_creating(dentry, -ENOMEM);
 
+	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
+	inode->i_op = &simple_dir_inode_operations;
+	inode->i_fop = &simple_dir_operations;
+
+	/* directory inodes start off with i_nlink == 2 (for "." entry) */
+	inc_nlink(inode);
 	d_instantiate(dentry, inode);
 	dget(dentry);
 	inc_nlink(dentry->d_parent->d_inode);
@@ -423,12 +405,14 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 		return NULL;
 	}
 
-	inode = debugfs_get_inode(dentry->d_sb, S_IFLNK | S_IRWXUGO, 0,
-				  link, NULL);
+	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode)) {
 		kfree(link);
 		return end_creating(dentry, -ENOMEM);
 	}
+	inode->i_mode = S_IFLNK | S_IRWXUGO;
+	inode->i_op = &debugfs_link_operations;
+	inode->i_private = link;
 	d_instantiate(dentry, inode);
 	dget(dentry);
 	return end_creating(dentry, 0);
Signature · 1 line
2.1.4
author maintainer
#10 lore raw

PATCH v3 10/16 debugfs: split end_creating() into success and failure cases

In reply to Steven Rostedt

From: Al Viro <[email protected]>

... and don't bother with dput(dentry) in the former and with dget(dentry) preceding all its calls.

---
 fs/debugfs/inode.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
fs/debugfs/inode.c +13 -14
index 61e9a6815a19..1219dff8e18f 100644
@@ -256,15 +256,17 @@ static struct dentry *start_creating(const char *name, struct dentry *parent)
 	return dentry;
 }
 
-static struct dentry *end_creating(struct dentry *dentry, int error)
+static struct dentry *failed_creating(struct dentry *dentry)
 {
 	mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
 	dput(dentry);
+	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
+	return NULL;
+}
 
-	if (error) {
-		dentry = NULL;
-		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
-	}
+static struct dentry *end_creating(struct dentry *dentry)
+{
+	mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
 	return dentry;
 }
 
@@ -311,15 +313,14 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 
 	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode))
-		return end_creating(dentry, -ENOMEM);
+		return failed_creating(dentry);
 
 	inode->i_mode = mode;
 	inode->i_fop = fops ? fops : &debugfs_file_operations;
 	inode->i_private = data;
 	d_instantiate(dentry, inode);
-	dget(dentry);
 	fsnotify_create(dentry->d_parent->d_inode, dentry);
-	return end_creating(dentry, 0);
+	return end_creating(dentry);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_file);
 
@@ -351,7 +352,7 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 
 	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode))
-		return end_creating(dentry, -ENOMEM);
+		return failed_creating(dentry);
 
 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
 	inode->i_op = &simple_dir_inode_operations;
@@ -360,10 +361,9 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
 	inc_nlink(inode);
 	d_instantiate(dentry, inode);
-	dget(dentry);
 	inc_nlink(dentry->d_parent->d_inode);
 	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
-	return end_creating(dentry, 0);
+	return end_creating(dentry);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_dir);
 
@@ -408,14 +408,13 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 	inode = debugfs_get_inode(dentry->d_sb);
 	if (unlikely(!inode)) {
 		kfree(link);
-		return end_creating(dentry, -ENOMEM);
+		return failed_creating(dentry);
 	}
 	inode->i_mode = S_IFLNK | S_IRWXUGO;
 	inode->i_op = &debugfs_link_operations;
 	inode->i_private = link;
 	d_instantiate(dentry, inode);
-	dget(dentry);
-	return end_creating(dentry, 0);
+	return end_creating(dentry);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
 
Signature · 1 line
2.1.4
author maintainer
#3 lore raw

PATCH v3 3/16 debugfs: kill __create_file()

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 71 +++++++++++++++++++++++++-----------------------------
 1 file changed, 33 insertions(+), 38 deletions(-)
fs/debugfs/inode.c +33 -38
index 100186c2bf0a..18564b08884c 100644
@@ -348,30 +348,6 @@ static struct dentry *end_creating(struct dentry *dentry, int error)
 	return dentry;
 }
 
-static struct dentry *__create_file(const char *name, umode_t mode,
-				    struct dentry *parent, void *data,
-				    const struct file_operations *fops)
-{
-	struct dentry *dentry = start_creating(name, parent);
-	int error;
-
-	if (IS_ERR(dentry))
-		return NULL;
-
-	switch (mode & S_IFMT) {
-	case S_IFDIR:
-		error = debugfs_mkdir(dentry, mode);
-		break;
-	case S_IFLNK:
-		error = debugfs_link(dentry, mode, data);
-		break;
-	default:
-		error = debugfs_create(dentry, mode, data, fops);
-		break;
-	}
-	return end_creating(dentry, error);
-}
-
 /**
  * debugfs_create_file - create a file in the debugfs filesystem
  * @name: a pointer to a string containing the name of the file to create.
@@ -402,15 +378,19 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 				   struct dentry *parent, void *data,
 				   const struct file_operations *fops)
 {
-	switch (mode & S_IFMT) {
-	case S_IFREG:
-	case 0:
-		break;
-	default:
-		BUG();
-	}
+	struct dentry *dentry;
+	int error;
+
+	if (!(mode & S_IFMT))
+		mode |= S_IFREG;
+	BUG_ON(!S_ISREG(mode));
+	dentry = start_creating(name, parent);
+
+	if (IS_ERR(dentry))
+		return NULL;
 
-	return __create_file(name, mode, parent, data, fops);
+	error = debugfs_create(dentry, mode, data, fops);
+	return end_creating(dentry, error);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_file);
 
@@ -434,8 +414,14 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);
  */
 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 {
-	return __create_file(name, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
-				   parent, NULL, NULL);
+	struct dentry *dentry = start_creating(name, parent);
+	int error;
+
+	if (IS_ERR(dentry))
+		return NULL;
+
+	error = debugfs_mkdir(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
+	return end_creating(dentry, error);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_dir);
 
@@ -465,17 +451,26 @@ EXPORT_SYMBOL_GPL(debugfs_create_dir);
 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 				      const char *target)
 {
-	struct dentry *result;
+	struct dentry *dentry;
 	char *link;
+	int error;
 
 	link = kstrdup(target, GFP_KERNEL);
 	if (!link)
 		return NULL;
 
-	result = __create_file(name, S_IFLNK | S_IRWXUGO, parent, link, NULL);
-	if (!result)
+	dentry = start_creating(name, parent);
+
+	if (IS_ERR(dentry)) {
 		kfree(link);
-	return result;
+		return NULL;
+	}
+
+	error = debugfs_link(dentry, S_IFLNK | S_IRWXUGO, link);
+	if (error)
+		kfree(link);
+
+	return end_creating(dentry, error);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
 
Signature · 1 line
2.1.4
author maintainer
#8 lore raw

PATCH v3 8/16 fold debugfs_mknod() into callers

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 68 +++++++++++++++++++++++++-----------------------------
 1 file changed, 31 insertions(+), 37 deletions(-)
fs/debugfs/inode.c +31 -37
index 778c0e32eb51..b765c04eba20 100644
@@ -69,21 +69,6 @@ static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev
 	return inode;
 }
 
-/* SMP-safe */
-static int debugfs_mknod(struct dentry *dentry,
-			 umode_t mode, void *data,
-			 const struct file_operations *fops)
-{
-	struct inode *inode;
-
-	inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops);
-	if (unlikely(!inode))
-		return -EPERM;
-	d_instantiate(dentry, inode);
-	dget(dentry);
-	return 0;
-}
-
 static inline int debugfs_positive(struct dentry *dentry)
 {
 	return dentry->d_inode && !d_unhashed(dentry);
@@ -339,7 +324,7 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 				   const struct file_operations *fops)
 {
 	struct dentry *dentry;
-	int error;
+	struct inode *inode;
 
 	if (!(mode & S_IFMT))
 		mode |= S_IFREG;
@@ -349,10 +334,14 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 	if (IS_ERR(dentry))
 		return NULL;
 
-	error = debugfs_mknod(dentry, mode, data, fops);
-	if (!error)
-		fsnotify_create(dentry->d_parent->d_inode, dentry);
-	return end_creating(dentry, error);
+	inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops);
+	if (unlikely(!inode))
+		return end_creating(dentry, -ENOMEM);
+
+	d_instantiate(dentry, inode);
+	dget(dentry);
+	fsnotify_create(dentry->d_parent->d_inode, dentry);
+	return end_creating(dentry, 0);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_file);
 
@@ -377,18 +366,22 @@ EXPORT_SYMBOL_GPL(debugfs_create_file);
 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 {
 	struct dentry *dentry = start_creating(name, parent);
-	int error;
+	struct inode *inode;
 
 	if (IS_ERR(dentry))
 		return NULL;
 
-	error = debugfs_mknod(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
-			      NULL, NULL);
-	if (!error) {
-		inc_nlink(dentry->d_parent->d_inode);
-		fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
-	}
-	return end_creating(dentry, error);
+	inode = debugfs_get_inode(dentry->d_sb,
+				  S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
+				  0, NULL, NULL);
+	if (unlikely(!inode))
+		return end_creating(dentry, -ENOMEM);
+
+	d_instantiate(dentry, inode);
+	dget(dentry);
+	inc_nlink(dentry->d_parent->d_inode);
+	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
+	return end_creating(dentry, 0);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_dir);
 
@@ -419,25 +412,26 @@ struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
 				      const char *target)
 {
 	struct dentry *dentry;
-	char *link;
-	int error;
-
-	link = kstrdup(target, GFP_KERNEL);
+	struct inode *inode;
+	char *link = kstrdup(target, GFP_KERNEL);
 	if (!link)
 		return NULL;
 
 	dentry = start_creating(name, parent);
-
 	if (IS_ERR(dentry)) {
 		kfree(link);
 		return NULL;
 	}
 
-	error = debugfs_mknod(dentry, S_IFLNK | S_IRWXUGO, link, NULL);
-	if (error)
+	inode = debugfs_get_inode(dentry->d_sb, S_IFLNK | S_IRWXUGO, 0,
+				  link, NULL);
+	if (unlikely(!inode)) {
 		kfree(link);
-
-	return end_creating(dentry, error);
+		return end_creating(dentry, -ENOMEM);
+	}
+	d_instantiate(dentry, inode);
+	dget(dentry);
+	return end_creating(dentry, 0);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
 
Signature · 1 line
2.1.4
author maintainer
#7 lore raw

PATCH v3 7/16 fold debugfs_create() into caller

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
fs/debugfs/inode.c +3 -14
index 8d12935c87f1..778c0e32eb51 100644
@@ -84,19 +84,6 @@ static int debugfs_mknod(struct dentry *dentry,
 	return 0;
 }
 
-static int debugfs_create(struct dentry *dentry, umode_t mode,
-			  void *data, const struct file_operations *fops)
-{
-	struct inode *dir = dentry->d_parent->d_inode;
-	int res;
-
-	mode = (mode & S_IALLUGO) | S_IFREG;
-	res = debugfs_mknod(dentry, mode, data, fops);
-	if (!res)
-		fsnotify_create(dir, dentry);
-	return res;
-}
-
 static inline int debugfs_positive(struct dentry *dentry)
 {
 	return dentry->d_inode && !d_unhashed(dentry);
@@ -362,7 +349,9 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 	if (IS_ERR(dentry))
 		return NULL;
 
-	error = debugfs_create(dentry, mode, data, fops);
+	error = debugfs_mknod(dentry, mode, data, fops);
+	if (!error)
+		fsnotify_create(dentry->d_parent->d_inode, dentry);
 	return end_creating(dentry, error);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_file);
Signature · 1 line
2.1.4
author maintainer
#6 lore raw

PATCH v3 6/16 fold debugfs_mkdir() into caller

In reply to Steven Rostedt

From: Al Viro <[email protected]>

---
 fs/debugfs/inode.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)
fs/debugfs/inode.c +6 -15
index bc02e2096977..8d12935c87f1 100644
@@ -84,20 +84,6 @@ static int debugfs_mknod(struct dentry *dentry,
 	return 0;
 }
 
-static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
-{
-	struct inode *dir = dentry->d_parent->d_inode;
-	int res;
-
-	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
-	res = debugfs_mknod(dentry, mode, NULL, NULL);
-	if (!res) {
-		inc_nlink(dir);
-		fsnotify_mkdir(dir, dentry);
-	}
-	return res;
-}
-
 static int debugfs_create(struct dentry *dentry, umode_t mode,
 			  void *data, const struct file_operations *fops)
 {
@@ -407,7 +393,12 @@ struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
 	if (IS_ERR(dentry))
 		return NULL;
 
-	error = debugfs_mkdir(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
+	error = debugfs_mknod(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
+			      NULL, NULL);
+	if (!error) {
+		inc_nlink(dentry->d_parent->d_inode);
+		fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
+	}
 	return end_creating(dentry, error);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_dir);
Signature · 1 line
2.1.4

Steven Rostedt

author maintainer
#0 lore raw

v3 cover tracing: Add new file system tracefs

There has been complaints that tracing is tied too much to debugfs, as there are systems that would like to perform tracing, but do not mount debugfs for security reasons. That is because any subsystem may use debugfs for debugging, and these interfaces are not always tested for security.

Creating a new tracefs that the tracing directory will now be attached to allows system admins the ability to access the tracing directory without the need to mount debugfs.

Another advantage is that debugfs does not support the system calls for mkdir and rmdir. Tracing uses these system calls to create new instances for sub buffers. This was done by a hack that hijacked the dentry ops from the "instances" debugfs dentry, and replacing it with one that could work.

Instead of using this hack, tracefs can provide a proper interface to allow the tracing system to have a mkdir and rmdir feature.

To maintain backward compatibility with older tools that expect that the tracing directory is mounted with debugfs, the tracing directory is still created under debugfs and tracefs is automatically mounted there.

Finally, a new directory is created when tracefs is enabled called /sys/kernel/tracing. This will be the new location that system admins may mount tracefs if they are not using debugfs.

Changes from v2:

o Al Viro wrote a series of patches to create "debugfs_create_automount()" that gives us a safe way to modify the debugfs dentry to add automounting. tracefs now uses this function to automount itself on the debugfs/tracing directory.

Changes from v1:

o Fixed all the posting problems (included files that were missing and removed changes that were not suppose to be there).

o Changed the mkdir/rmdir logic. Instead of trying to keep the inode mutexes locked, which caused locking issues with other locks that were taken, the locks are still released. But this time, they are released by the tracefs system which has a bit more control. As the mkdir/rmdir methods for the tracing facility only need the new name of the instance, the tracefs mkdir/rmdir copies the name from the dentry, releases the locks, and passes in the copy to the tracing methods.

Al Viro (11):
debugfs_{mkdir,create,link}(): get rid of redundant argument
debugfs: split the beginning and the end of __create_file() off
debugfs: kill __create_file()
fold debugfs_link() into caller
debugfs_mknod(): get rid useless arguments
fold debugfs_mkdir() into caller
fold debugfs_create() into caller
fold debugfs_mknod() into callers
debugfs: take mode-dependent parts of debugfs_get_inode() into callers
debugfs: split end_creating() into success and failure cases new primitive: debugfs_create_automount()

Steven Rostedt (Red Hat) (5):
tracefs: Add new tracefs file system
tracing: Convert the tracing facility over to use tracefs
tracing: Automatically mount tracefs on debugfs/tracing
tracefs: Add directory /sys/kernel/tracing
tracing: Have mkdir and rmdir be part of tracefs

----

 fs/Makefile                          |   1 +
 fs/debugfs/inode.c                   | 251 ++++++-------
 fs/tracefs/Makefile                  |   4 +
 fs/tracefs/inode.c                   | 658 +++++++++++++++++++++++++++++++++++
 include/linux/debugfs.h              |   5 +
 include/linux/tracefs.h              |  48 +++
 include/uapi/linux/magic.h           |   2 +
 kernel/trace/ftrace.c                |  22 +-
 kernel/trace/trace.c                 | 161 ++++-----
 kernel/trace/trace.h                 |   2 +-
 kernel/trace/trace_events.c          |  32 +-
 kernel/trace/trace_functions_graph.c |   7 +-
 kernel/trace/trace_kprobe.c          |  10 +-
 kernel/trace/trace_probe.h           |   2 +-
 kernel/trace/trace_stat.c            |  10 +-
 15 files changed, 962 insertions(+), 253 deletions(-)
maintainer
#17 lore raw
Quoted 19 lines · Steven Rostedt

There has been complaints that tracing is tied too much to debugfs, as there are systems that would like to perform tracing, but do not mount debugfs for security reasons. That is because any subsystem may use debugfs for debugging, and these interfaces are not always tested for security.

Creating a new tracefs that the tracing directory will now be attached to allows system admins the ability to access the tracing directory without the need to mount debugfs.

Another advantage is that debugfs does not support the system calls for mkdir and rmdir. Tracing uses these system calls to create new instances for sub buffers. This was done by a hack that hijacked the dentry ops from the "instances" debugfs dentry, and replacing it with one that could work.

Instead of using this hack, tracefs can provide a proper interface to allow the tracing system to have a mkdir and rmdir feature.

Quoted 8 lines

To maintain backward compatibility with older tools that expect that the tracing directory is mounted with debugfs, the tracing directory is still created under debugfs and tracefs is automatically mounted there.

Finally, a new directory is created when tracefs is enabled called /sys/kernel/tracing. This will be the new location that system admins may mount tracefs if they are not using debugfs.

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

author maintainer
#18 lore raw
Quoted 9 lines · On Mon, 26 Jan 2015 19:30:49 +0000 Al Viro <[email protected]> wrote:

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

The vfs layer grabs the i_mutex, which needs to be dropped. I was hoping to keep the dropping of the i_mutex within the tracefs code and not have the tracing code worry about it. I still will need to add i_private to hold this info.

But that said, I could clean it up a little be having a tracefs_create_instance_dir(), which does all the work within the tracefs code. The caller would pass in the functions to be called by that.

Like this patch applied on top of my current series:

-- Steve

fs/tracefs/inode.c +50 -32
index c7eb4c58579e..13e17bf677f4 100644
@@ -50,6 +50,11 @@ static const struct file_operations tracefs_file_operations = {
 	.llseek =	noop_llseek,
 };
 
+struct tracefs_dir_ops {
+	int (*mkdir)(const char *name);
+	int (*rmdir)(const char *name);
+};
+
 static char *get_dname(struct dentry *dentry)
 {
 	const char *dname;
@@ -67,13 +72,10 @@ static char *get_dname(struct dentry *dentry)
 
 static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
 {
-	const struct tracefs_dir_ops *ops = inode ? inode->i_private : NULL;
+	const struct tracefs_dir_ops *ops = inode->i_private;
 	char *name;
 	int ret;
 
-	if (!ops)
-		return -EPERM;
-
 	name = get_dname(dentry);
 	if (!name)
 		return -ENOMEM;
@@ -98,9 +100,6 @@ static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
 	char *name;
 	int ret;
 
-	if (!ops)
-		return -EPERM;
-
 	name = get_dname(dentry);
 	if (!name)
 		return -ENOMEM;
@@ -317,16 +316,6 @@ static struct file_system_type trace_fs_type = {
 };
 MODULE_ALIAS_FS("tracefs");
 
-void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops)
-{
-	struct inode *inode = dentry->d_inode;
-
-	if (!inode)
-		return;
-
-	inode->i_private = (void *)ops;
-}
-
 static struct dentry *start_creating(const char *name, struct dentry *parent)
 {
 	struct dentry *dentry;
@@ -425,6 +414,31 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,
 	return end_creating(dentry);
 }
 
+static struct dentry *__create_dir(const char *name, struct dentry *parent,
+				   const struct inode_operations *ops)
+{
+	struct dentry *dentry = start_creating(name, parent);
+	struct inode *inode;
+
+	if (IS_ERR(dentry))
+		return NULL;
+
+	inode = tracefs_get_inode(dentry->d_sb);
+	if (unlikely(!inode))
+		return failed_creating(dentry);
+
+	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
+	inode->i_op = ops;
+	inode->i_fop = &simple_dir_operations;
+
+	/* directory inodes start off with i_nlink == 2 (for "." entry) */
+	inc_nlink(inode);
+	d_instantiate(dentry, inode);
+	inc_nlink(dentry->d_parent->d_inode);
+	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
+	return end_creating(dentry);
+}
+
 /**
  * tracefs_create_dir - create a directory in the tracefs filesystem
  * @name: a pointer to a string containing the name of the directory to
@@ -444,26 +458,30 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,
  */
 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
 {
-	struct dentry *dentry = start_creating(name, parent);
-	struct inode *inode;
+	return __create_dir(name, parent, &simple_dir_inode_operations);
+}
 
-	if (IS_ERR(dentry))
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					  int (*mkdir)(const char *name),
+					  int (*rmdir)(const char *name))
+{
+	struct tracefs_dir_ops *dir_ops;
+	struct dentry *dentry;
+
+	dir_ops = kmalloc(sizeof(*dir_ops), GFP_KERNEL);
+	if (!dir_ops)
 		return NULL;
 
-	inode = tracefs_get_inode(dentry->d_sb);
-	if (unlikely(!inode))
-		return failed_creating(dentry);
+	dir_ops->mkdir = mkdir;
+	dir_ops->rmdir = mkdir;
 
-	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
-	inode->i_op = &tracefs_dir_inode_operations;
-	inode->i_fop = &simple_dir_operations;
+	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
+	if (!dentry)
+		return NULL;
 
-	/* directory inodes start off with i_nlink == 2 (for "." entry) */
-	inc_nlink(inode);
-	d_instantiate(dentry, inode);
-	inc_nlink(dentry->d_parent->d_inode);
-	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
-	return end_creating(dentry);
+	dentry->d_inode->i_private = (void *)dir_ops;
+
+	return dentry;
 }
 
 static inline int tracefs_positive(struct dentry *dentry)
include/linux/tracefs.h +3 -6
index d142b1f9d453..5b727a17beee 100644
@@ -34,12 +34,9 @@ struct dentry *tracefs_create_dir(const char *name, struct dentry *parent);
 void tracefs_remove(struct dentry *dentry);
 void tracefs_remove_recursive(struct dentry *dentry);
 
-struct tracefs_dir_ops {
-	int (*mkdir)(const char *name);
-	int (*rmdir)(const char *name);
-};
-
-void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops);
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					   int (*mkdir)(const char *name),
+					   int (*rmdir)(const char *name));
 
 bool tracefs_initialized(void);
 
kernel/trace/trace.c +3 -8
index c555027c8aaf..2047501dcd16 100644
@@ -6445,18 +6445,13 @@ static int instance_rmdir(const char *name)
 	return ret;
 }
 
-static const struct tracefs_dir_ops instance_dir_ops = {
-	.mkdir		= instance_mkdir,
-	.rmdir		= instance_rmdir,
-};
-
 static __init void create_trace_instances(struct dentry *d_tracer)
 {
-	trace_instance_dir = tracefs_create_dir("instances", d_tracer);
+	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
+							 instance_mkdir,
+							 instance_rmdir);
 	if (WARN_ON(!trace_instance_dir))
 		return;
-
-	tracefs_add_dir_ops(trace_instance_dir, &instance_dir_ops);
 }
 
 static void
author maintainer
#19 lore raw

fs/tracefs/inode.c struct dentry *tracefs_create_file(const char *name, umode_t mode, · line 466

Quoted 4 lines · On Mon, 26 Jan 2015 15:42:59 -0500 Steven Rostedt <[email protected]> wrote:
fs/tracefs/inode.c +3 -1
-	if (IS_ERR(dentry))
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					  int (*mkdir)(const char *name),
+					  int (*rmdir)(const char *name))

Now what I could also do is to only allow this to be called once. And in that case, we can hard code what mkdir and rmdir are. This could work as currently there only exists a single instance of the instances directory.

I might just do that too.

-- Steve

author maintainer
#20 lore raw
Quoted 6 lines · On Mon, 26 Jan 2015 15:44:19 -0500 Steven Rostedt <[email protected]> wrote:

Now what I could also do is to only allow this to be called once. And in that case, we can hard code what mkdir and rmdir are. This could work as currently there only exists a single instance of the instances directory.

I might just do that too.

And just did:

fs/tracefs/inode.c +50 -32
index c7eb4c58579e..13e17bf677f4 100644
@@ -50,6 +50,11 @@ static const struct file_operations tracefs_file_operations = {
 	.llseek =	noop_llseek,
 };
 
+struct tracefs_dir_ops {
+	int (*mkdir)(const char *name);
+	int (*rmdir)(const char *name);
+};
+
 static char *get_dname(struct dentry *dentry)
 {
 	const char *dname;
@@ -67,13 +72,10 @@ static char *get_dname(struct dentry *dentry)
 
 static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
 {
-	const struct tracefs_dir_ops *ops = inode ? inode->i_private : NULL;
+	const struct tracefs_dir_ops *ops = inode->i_private;
 	char *name;
 	int ret;
 
-	if (!ops)
-		return -EPERM;
-
 	name = get_dname(dentry);
 	if (!name)
 		return -ENOMEM;
@@ -98,9 +100,6 @@ static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
 	char *name;
 	int ret;
 
-	if (!ops)
-		return -EPERM;
-
 	name = get_dname(dentry);
 	if (!name)
 		return -ENOMEM;
@@ -317,16 +316,6 @@ static struct file_system_type trace_fs_type = {
 };
 MODULE_ALIAS_FS("tracefs");
 
-void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops)
-{
-	struct inode *inode = dentry->d_inode;
-
-	if (!inode)
-		return;
-
-	inode->i_private = (void *)ops;
-}
-
 static struct dentry *start_creating(const char *name, struct dentry *parent)
 {
 	struct dentry *dentry;
@@ -425,6 +414,31 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,
 	return end_creating(dentry);
 }
 
+static struct dentry *__create_dir(const char *name, struct dentry *parent,
+				   const struct inode_operations *ops)
+{
+	struct dentry *dentry = start_creating(name, parent);
+	struct inode *inode;
+
+	if (IS_ERR(dentry))
+		return NULL;
+
+	inode = tracefs_get_inode(dentry->d_sb);
+	if (unlikely(!inode))
+		return failed_creating(dentry);
+
+	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
+	inode->i_op = ops;
+	inode->i_fop = &simple_dir_operations;
+
+	/* directory inodes start off with i_nlink == 2 (for "." entry) */
+	inc_nlink(inode);
+	d_instantiate(dentry, inode);
+	inc_nlink(dentry->d_parent->d_inode);
+	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
+	return end_creating(dentry);
+}
+
 /**
  * tracefs_create_dir - create a directory in the tracefs filesystem
  * @name: a pointer to a string containing the name of the directory to
@@ -444,26 +458,30 @@ struct dentry *tracefs_create_file(const char *name, umode_t mode,
  */
 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
 {
-	struct dentry *dentry = start_creating(name, parent);
-	struct inode *inode;
+	return __create_dir(name, parent, &simple_dir_inode_operations);
+}
 
-	if (IS_ERR(dentry))
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					  int (*mkdir)(const char *name),
+					  int (*rmdir)(const char *name))
+{
+	struct tracefs_dir_ops *dir_ops;
+	struct dentry *dentry;
+
+	dir_ops = kmalloc(sizeof(*dir_ops), GFP_KERNEL);
+	if (!dir_ops)
 		return NULL;
 
-	inode = tracefs_get_inode(dentry->d_sb);
-	if (unlikely(!inode))
-		return failed_creating(dentry);
+	dir_ops->mkdir = mkdir;
+	dir_ops->rmdir = mkdir;
 
-	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
-	inode->i_op = &tracefs_dir_inode_operations;
-	inode->i_fop = &simple_dir_operations;
+	dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
+	if (!dentry)
+		return NULL;
 
-	/* directory inodes start off with i_nlink == 2 (for "." entry) */
-	inc_nlink(inode);
-	d_instantiate(dentry, inode);
-	inc_nlink(dentry->d_parent->d_inode);
-	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
-	return end_creating(dentry);
+	dentry->d_inode->i_private = (void *)dir_ops;
+
+	return dentry;
 }
 
 static inline int tracefs_positive(struct dentry *dentry)
include/linux/tracefs.h +3 -6
index d142b1f9d453..5b727a17beee 100644
@@ -34,12 +34,9 @@ struct dentry *tracefs_create_dir(const char *name, struct dentry *parent);
 void tracefs_remove(struct dentry *dentry);
 void tracefs_remove_recursive(struct dentry *dentry);
 
-struct tracefs_dir_ops {
-	int (*mkdir)(const char *name);
-	int (*rmdir)(const char *name);
-};
-
-void tracefs_add_dir_ops(struct dentry *dentry, const struct tracefs_dir_ops *ops);
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					   int (*mkdir)(const char *name),
+					   int (*rmdir)(const char *name));
 
 bool tracefs_initialized(void);
 
kernel/trace/trace.c +3 -8
index c555027c8aaf..2047501dcd16 100644
@@ -6445,18 +6445,13 @@ static int instance_rmdir(const char *name)
 	return ret;
 }
 
-static const struct tracefs_dir_ops instance_dir_ops = {
-	.mkdir		= instance_mkdir,
-	.rmdir		= instance_rmdir,
-};
-
 static __init void create_trace_instances(struct dentry *d_tracer)
 {
-	trace_instance_dir = tracefs_create_dir("instances", d_tracer);
+	trace_instance_dir = tracefs_create_instance_dir("instances", d_tracer,
+							 instance_mkdir,
+							 instance_rmdir);
 	if (WARN_ON(!trace_instance_dir))
 		return;
-
-	tracefs_add_dir_ops(trace_instance_dir, &instance_dir_ops);
 }
 
 static void
maintainer
#36 lore raw
In reply to Al Viro
Quoted 29 lines · Al Viro
Quoted 19 lines · Steven Rostedt

There has been complaints that tracing is tied too much to debugfs, as there are systems that would like to perform tracing, but do not mount debugfs for security reasons. That is because any subsystem may use debugfs for debugging, and these interfaces are not always tested for security.

Creating a new tracefs that the tracing directory will now be attached to allows system admins the ability to access the tracing directory without the need to mount debugfs.

Another advantage is that debugfs does not support the system calls for mkdir and rmdir. Tracing uses these system calls to create new instances for sub buffers. This was done by a hack that hijacked the dentry ops from the "instances" debugfs dentry, and replacing it with one that could work.

Instead of using this hack, tracefs can provide a proper interface to allow the tracing system to have a mkdir and rmdir feature.

Quoted 8 lines

To maintain backward compatibility with older tools that expect that the tracing directory is mounted with debugfs, the tracing directory is still created under debugfs and tracefs is automatically mounted there.

Finally, a new directory is created when tracefs is enabled called /sys/kernel/tracing. This will be the new location that system admins may mount tracefs if they are not using debugfs.

Quoted 9 lines

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

Is the following correct?
* only one directory in the entire tree (/instances) allows mkdir/rmdir.
* ftrace_trace_arrays always starts with global_trace and the rest
is in 1-to-1 correspondence with subdirectories of /instances.
* tr->name is NULL for global_trace and non-NULL for everything
else.
* all modifications of that list are happening from mkdir/rmdir
* in the end of ->mkdir tr->dir set to the dentry of our subdirectory,
and it's non-NULL (or trace_array creation simply fails)
* global_array.dir is set to magical value ((struct dentry *)1) upon
the first call of tracing_init_dentry(). Prior to that it's NULL. BTW, may I politely inquire what the fuck are those contortions in tracing_init_dentry_tr() about? Looks like a stunningly convoluted way to trigger that automount point creation early in tracer_init_tracefs(). Why not do that right there explicitly?

What are mount options doing? I mean, sure, you set the mode/uid/gid of root. Which is not modifiable anyway... And AFAICS that's all those options are affecting.

AFAICS, nothing ever removes files in debugfs root. Right? If so, you don't need the games with simple_pin_fs() - they are pointless in such situation anyway. Just do tracefs_mount = kern_mount(&trace_fs_type); in tracefs_init() and be done with that; lose the tracefs_mount_count and all calls of simple_{pin,release}_fs().

While we are at it, what the hell is tracefs_file_operations about? Looks like some bastard offspring of /dev/null, but I don't see anything that would use it...

maintainer
#21 lore raw
In reply to Steven Rostedt
Quoted 17 lines · Steven Rostedt
Quoted 6 lines · On Mon, 26 Jan 2015 15:44:19 -0500 Steven Rostedt <[email protected]> wrote:

Now what I could also do is to only allow this to be called once. And in that case, we can hard code what mkdir and rmdir are. This could work as currently there only exists a single instance of the instances directory.

I might just do that too.

And just did:

fs/tracefs/inode.c +0 -0
index c7eb4c58579e..13e17bf677f4 100644

fs/tracefs/inode.c static const struct file_operations tracefs_file_operations = { · line 56

Quoted 7 lines
fs/tracefs/inode.c +4 -0
@@ -50,6 +50,11 @@ static const struct file_operations tracefs_file_operations = {
 	.llseek =	noop_llseek,
 };
 
+struct tracefs_dir_ops {
+	int (*mkdir)(const char *name);
+	int (*rmdir)(const char *name);
+};

Why go through that indirection at all...

fs/tracefs/inode.c static const struct file_operations tracefs_file_operations = { · line 59

Quoted 3 lines
fs/tracefs/inode.c +3 -0
+struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
+					   int (*mkdir)(const char *name),
+					   int (*rmdir)(const char *name));

... when there's exactly one caller of that thing, and the values of mkdir and rmdir callbacks are the same on each call?

maintainer
#22 lore raw
In reply to Steven Rostedt
Quoted 13 lines · Steven Rostedt
Quoted 9 lines · On Mon, 26 Jan 2015 19:30:49 +0000 Al Viro <[email protected]> wrote:

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

Quoted 1 line

The vfs layer grabs the i_mutex, which needs to be dropped.

What for? Just keep it through your instance_mkdir/instance_rmdir and be done with that. Sure, it means that you need variants of file/directory creation/removal primitives that would assume that parent is already locked by caller (and would leave unlocking to the caller as well). So add them...

maintainer
#23 lore raw
Quoted 14 lines · Al Viro
Quoted 13 lines · Steven Rostedt
Quoted 9 lines · On Mon, 26 Jan 2015 19:30:49 +0000 Al Viro <[email protected]> wrote:

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

Quoted 6 lines
Quoted 1 line

The vfs layer grabs the i_mutex, which needs to be dropped.

What for? Just keep it through your instance_mkdir/instance_rmdir and be done with that. Sure, it means that you need variants of file/directory creation/removal primitives that would assume that parent is already locked by caller (and would leave unlocking to the caller as well). So add them...

BTW, could you merge vfs.git#debugfs_automount, rather than copy the commits into your branch? Bloody inconvenient as it is...

author maintainer
#26 lore raw
In reply to Steven Rostedt
Quoted 4 lines · On Mon, 26 Jan 2015 18:43:14 -0500 Steven Rostedt <[email protected]> wrote:

That is, we can not hold i_mutex and take trace_types_lock.

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

Although, I can not remember how this happened, but lockdep blew up on me with this. I'll look again.

author maintainer
#24 lore raw
In reply to Al Viro
Quoted 2 lines · On Mon, 26 Jan 2015 23:02:03 +0000 Al Viro <[email protected]> wrote:

BTW, could you merge vfs.git#debugfs_automount, rather than copy the commits into your branch? Bloody inconvenient as it is...

Ah, you're pushing it. Sorry, I thought this was more of a temp branch for me to play with.

OK, I can merge it.

-- Steve

author maintainer
#25 lore raw
In reply to Al Viro
Quoted 14 lines · On Mon, 26 Jan 2015 21:46:58 +0000 Al Viro <[email protected]> wrote:
Quoted 13 lines · Steven Rostedt
Quoted 9 lines · On Mon, 26 Jan 2015 19:30:49 +0000 Al Viro <[email protected]> wrote:

You are still fighting an inconvenient API, but now it's not debugfs one - it's your copy thereof. Why not give your instances/ an inode_operations of its own? One with ->mkdir() and ->rmdir(), leaving all other directories as-is. That way you don't need the secondary methods at all. And sure, debugfs_create_dir() grabs ->i_mutex on parent, making you drop that in your ->mkdir() if you want to call it. But now you are not talking to it - just to your own code, where you are free to change the calling conventions, making it caller's responsibility to get that ->i_mutex. The same goes for the rmdir side...

Quoted 6 lines
Quoted 1 line

The vfs layer grabs the i_mutex, which needs to be dropped.

What for? Just keep it through your instance_mkdir/instance_rmdir and be done with that. Sure, it means that you need variants of file/directory creation/removal primitives that would assume that parent is already locked by caller (and would leave unlocking to the caller as well). So add them...

I originally had this, but the issue isn't with i_mutex only. The instance_mkdir and instance_rmdir need to grab trace_types_lock. This is also held when calling into tracefs.

That is, we can not hold i_mutex and take trace_types_lock.

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

-- Steve

author maintainer
#37 lore raw
In reply to Al Viro
Quoted 2 lines · On Mon, 26 Jan 2015 21:40:20 +0000 Al Viro <[email protected]> wrote:

Is the following correct?
* only one directory in the entire tree (/instances) allows mkdir/rmdir.

yes

Quoted 2 lines

* ftrace_trace_arrays always starts with global_trace and the rest
is in 1-to-1 correspondence with subdirectories of /instances.

pretty much.

Quoted 2 lines

* tr->name is NULL for global_trace and non-NULL for everything
else.

yes.

Quoted 1 line

* all modifications of that list are happening from mkdir/rmdir

yes.

Quoted 2 lines

* in the end of ->mkdir tr->dir set to the dentry of our subdirectory,
and it's non-NULL (or trace_array creation simply fails)

yes.

Quoted 1 line

* global_array.dir is set to magical value ((struct dentry *)1) upon

This is new for this patch series. Currently, global_array.dir points to the dentry of /sys/kernel/debug/tracing.

Quoted 5 lines

the first call of tracing_init_dentry(). Prior to that it's NULL. BTW, may I politely inquire what the fuck are those contortions in tracing_init_dentry_tr() about? Looks like a stunningly convoluted way to trigger that automount point creation early in tracer_init_tracefs(). Why not do that right there explicitly?

Yeah, that could be cleaned up. Before the tracefs code, it made much more sense to keep that as a single function. Now that global_array.dir is treated differently as the subdirs, it does make sense to have global_arry.dir initialized in a separate function.

I'll update my patch series to do this.

Quoted 4 lines

What are mount options doing? I mean, sure, you set the mode/uid/gid of root. Which is not modifiable anyway... And AFAICS that's all those options are affecting.

You mean in tracefs.c/inode.c? I have no idea, they are there from debugfs cut and paste ;-)

Quoted 2 lines

AFAICS, nothing ever removes files in debugfs root. Right? If so, you don't

I'm not sure about that. Well, not from tracing or from userspace. But I believe things can be removed from debugfs root with module unload. But are you talking about tracefs root?

Quoted 4 lines

need the games with simple_pin_fs() - they are pointless in such situation anyway. Just do tracefs_mount = kern_mount(&trace_fs_type); in tracefs_init() and be done with that; lose the tracefs_mount_count and all calls of simple_{pin,release}_fs().

OK, cool. Because I had no idea what hey were used for anyway. ;-)

Quoted 4 lines

While we are at it, what the hell is tracefs_file_operations about? Looks like some bastard offspring of /dev/null, but I don't see anything that would use it...

Hehe, again, this is what debugfs did and I just brought it over thinking I needed it.

Thanks,

-- Steve

maintainer
#30 lore raw
In reply to Steven Rostedt
Quoted 8 lines · Steven Rostedt

I originally had this, but the issue isn't with i_mutex only. The instance_mkdir and instance_rmdir need to grab trace_types_lock. This is also held when calling into tracefs.

That is, we can not hold i_mutex and take trace_types_lock.

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

Yecchhh...

Looks like something that fell out of BKL removal - AFAICS, you are using it to protect all kinds of things ;-/

author maintainer
#27 lore raw
In reply to Steven Rostedt
Quoted 10 lines · On Mon, 26 Jan 2015 18:49:16 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Mon, 26 Jan 2015 18:43:14 -0500 Steven Rostedt <[email protected]> wrote:

That is, we can not hold i_mutex and take trace_types_lock.

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

Although, I can not remember how this happened, but lockdep blew up on me with this. I'll look again.

OK, found it. When events are created (module is loaded), the trace_types_lock is taken, and the event directories are created (these are in /sys/kernel/debug/tracing/events). We need to grab the i_mutex in order to create these files.

This means that we can not take the trace_types_lock within the mkdir or rmdir calls.

The directories are not static even outside the mkdir and rmdir calls. As events can be created from several sources, which will create new files and directories.

-- Steve

author maintainer
#31 lore raw
In reply to Al Viro
Quoted 8 lines · On Tue, 27 Jan 2015 00:37:42 +0000 Al Viro <[email protected]> wrote:
Quoted 6 lines · Steven Rostedt

I originally had this, but the issue isn't with i_mutex only. The instance_mkdir and instance_rmdir need to grab trace_types_lock. This is also held when calling into tracefs.

That is, we can not hold i_mutex and take trace_types_lock.

Quoted 7 lines
Quoted 2 lines

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

Yecchhh...

Looks like something that fell out of BKL removal - AFAICS, you are using it to protect all kinds of things ;-/

It's only used to protect slow paths. Yes, it grew more than what it should protect. I had patches to add a few more mutexes, to narrow things down. But that still did not solve this issue. The broken out mutex still needed to be held when events are created.

The main issue is that mkdir() and rmdir() will create events that require holding a mutex to make sure access to the events are serialized.

But creating events requires grabbing the i_mutex. We can create events either with mkdir() or when a module is loaded that has its own events.

Same goes with the mutex that protects adding and removing of trace_arrays. When a module is loaded, we need to add events to each trace array that exists (requiring that we grab the mutex that protects that list). And hold it while we add our events.

Now you see why I found just dropping the parent mutex easier.

-- Steve

author maintainer
#32 lore raw
Quoted 1 line · On Mon, 26 Jan 2015 20:02:18 -0500 Steven Rostedt <[email protected]> wrote:

Now you see why I found just dropping the parent mutex easier.

And this is probably why kernfs does things the way it does. I can imagine it having the same locking issues.

-- Steve

author maintainer
#38 lore raw
In reply to Steven Rostedt
Quoted 6 lines · On Mon, 26 Jan 2015 18:37:39 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines

the first call of tracing_init_dentry(). Prior to that it's NULL. BTW, may I politely inquire what the fuck are those contortions in tracing_init_dentry_tr() about? Looks like a stunningly convoluted way to trigger that automount point creation early in tracer_init_tracefs(). Why not do that right there explicitly?

Quoted 6 lines

Yeah, that could be cleaned up. Before the tracefs code, it made much more sense to keep that as a single function. Now that global_array.dir is treated differently as the subdirs, it does make sense to have global_arry.dir initialized in a separate function.

I'll update my patch series to do this.

Now I remember why I did this (as I changed the code and everything blew up). The files in the tracing directory can be created by several users (do a grep for fs_initcall() in kernel/trace/*.c). The first caller to add a file initializes the tracing directory.

I guess I can have all the other callers use fs_initcall_sync(). I'm assuming those come after fs_initcall().

-- Steve

maintainer
#28 lore raw
In reply to Steven Rostedt
Quoted 19 lines · Steven Rostedt
Quoted 10 lines · On Mon, 26 Jan 2015 18:49:16 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Mon, 26 Jan 2015 18:43:14 -0500 Steven Rostedt <[email protected]> wrote:

That is, we can not hold i_mutex and take trace_types_lock.

trace_types_lock needs to be held with the creation or destruction of events, which is what mkdir an rmdir do.

Although, I can not remember how this happened, but lockdep blew up on me with this. I'll look again.

OK, found it. When events are created (module is loaded), the trace_types_lock is taken, and the event directories are created (these are in /sys/kernel/debug/tracing/events). We need to grab the i_mutex in order to create these files.

Quoted 6 lines

This means that we can not take the trace_types_lock within the mkdir or rmdir calls.

The directories are not static even outside the mkdir and rmdir calls. As events can be created from several sources, which will create new files and directories.

Frankly, the first impression is that you have trace_types_lock way too high in the hierarchy - you are taking it well outside of the level where you start walking through ftrace_trace_arrays...

What else is it used for? You seem to use it protect trace_array refcounts, but you use them in a very odd way... What happens if lookup for a file in that tree loses CPU just as it enters trace_array_get() (e.g. on the same trace_types_lock), at which point rm .../instances/<whatever> comes, sees that tr->ref is still zero, kicks the sucker out of the list and frees it, immediately followed by mkdir creating a new instace? If the allocation of the new trace_array happens to reuse the just-freed one, your trace_array_get() will actually succeed, won't it?

It's not fatal (after all, everything will work as if you opened the similar file in new instance in the first place), but it looks rather bogus...

maintainer
#33 lore raw
In reply to Steven Rostedt
Quoted 8 lines · Steven Rostedt
Quoted 1 line · On Mon, 26 Jan 2015 20:02:18 -0500 Steven Rostedt <[email protected]> wrote:

Now you see why I found just dropping the parent mutex easier.

And this is probably why kernfs does things the way it does. I can imagine it having the same locking issues.

The least said about kernfs locking, the better...

As for the use of trace_types_lock to serialize rmdir vs. event addition/removal, I wonder what's wrong with actually using the ->i_mutex of /instances - you have a reference to its dentry, after all...

maintainer
#39 lore raw
In reply to Steven Rostedt
Quoted 17 lines · Steven Rostedt
Quoted 6 lines · On Mon, 26 Jan 2015 18:37:39 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines

the first call of tracing_init_dentry(). Prior to that it's NULL. BTW, may I politely inquire what the fuck are those contortions in tracing_init_dentry_tr() about? Looks like a stunningly convoluted way to trigger that automount point creation early in tracer_init_tracefs(). Why not do that right there explicitly?

Quoted 6 lines

Yeah, that could be cleaned up. Before the tracefs code, it made much more sense to keep that as a single function. Now that global_array.dir is treated differently as the subdirs, it does make sense to have global_arry.dir initialized in a separate function.

I'll update my patch series to do this.

Quoted 7 lines

Now I remember why I did this (as I changed the code and everything blew up). The files in the tracing directory can be created by several users (do a grep for fs_initcall() in kernel/trace/*.c). The first caller to add a file initializes the tracing directory.

I guess I can have all the other callers use fs_initcall_sync(). I'm assuming those come after fs_initcall().

Ah... That explains some of the oddities of that kind (not all of them, though - your trace_options_init_dentry() is called by create_trace_option_dirs() and create_trace_option_core_file(), which is called only by create_trace_option_dirs(), and that - quite a few times, each time calling trace_options_init_dentry()).

Strange style, IMO. Frankly, I'd rather have the ordering as explicit as it gets...

author maintainer
#29 lore raw
In reply to Al Viro
Quoted 3 lines · On Tue, 27 Jan 2015 04:34:21 +0000 Al Viro <[email protected]> wrote:

Frankly, the first impression is that you have trace_types_lock way too high in the hierarchy - you are taking it well outside of the level where you start walking through ftrace_trace_arrays...

Actually, it is originally used for modifying of tr->current_trace

This was before having multiple buffers. I just used the same lock to protect the trace arrays. Note, I have patches to make a separate mutex for that, but it just seems to make things a little more complex, so I haven't pushed it too much. Yeah, that trace_types_lock is basically tracing's BKL.

Quoted 10 lines

What else is it used for? You seem to use it protect trace_array refcounts, but you use them in a very odd way... What happens if lookup for a file in that tree loses CPU just as it enters trace_array_get() (e.g. on the same trace_types_lock), at which point rm .../instances/<whatever> comes, sees that tr->ref is still zero, kicks the sucker out of the list and frees it, immediately followed by mkdir creating a new instace? If the allocation of the new trace_array happens to reuse the just-freed one, your trace_array_get() will actually succeed, won't it?

Quoted 2 lines

It's not fatal (after all, everything will work as if you opened the similar file in new instance in the first place), but it looks rather bogus...

I'm actually aware of this possibility. But as you said, it's not fatal, and very unlikely to happen.

-- Steve

author maintainer
#34 lore raw
In reply to Al Viro
Quoted 10 lines · On Tue, 27 Jan 2015 04:38:39 +0000 Al Viro <[email protected]> wrote:
Quoted 8 lines · Steven Rostedt
Quoted 1 line · On Mon, 26 Jan 2015 20:02:18 -0500 Steven Rostedt <[email protected]> wrote:

Now you see why I found just dropping the parent mutex easier.

And this is probably why kernfs does things the way it does. I can imagine it having the same locking issues.

Quoted 6 lines

The least said about kernfs locking, the better...

As for the use of trace_types_lock to serialize rmdir vs. event addition/removal, I wonder what's wrong with actually using the ->i_mutex of /instances - you have a reference to its dentry, after all...

You mean, instead of grabbing trace_types_lock for modifying of events and trace arrays, we should grab the dentry->d_inode->i_mutex?

BTW, what exactly can go wrong with the current method I have that releases the i_mutex, calls the mkdir() method, and then regrabs the i_mutex?

-- Steve

author maintainer
#35 lore raw
Quoted 4 lines · On Tue, 27 Jan 2015 10:19:53 -0500 Steven Rostedt <[email protected]> wrote:

You mean, instead of grabbing trace_types_lock for modifying of events and trace arrays, we should grab the dentry->d_inode->i_mutex?

OK, trace_arrays_lock protects against the tr->current_trace and the list of arrays, and accessing it.

The creation of events is protected by the event_mutex. Which would suffer the same issue, as it is grabbed when creating and removing event directories.

-- Steve