Skip to contentSkip to conversation

tracing/events: make the filter files writable

PATCH In review Frederic Weisbecker 4 participants 22 messages Mar 22, 2009 – Mar 23, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/5 tracing/events: make the filter files writable

We need the filter files to be writable, the current filter file permissions make are only set readable.

---
 kernel/trace/trace_events.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events.c +2 -2
index 594d78a..19f61dd 100644
@@ -706,7 +706,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
 	system->preds = NULL;
 
-	entry = debugfs_create_file("filter", 0444, system->entry, system,
+	entry = debugfs_create_file("filter", 0644, system->entry, system,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
@@ -769,7 +769,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
 		}
 	}
 
-	entry = debugfs_create_file("filter", 0444, call->dir, call,
+	entry = debugfs_create_file("filter", 0644, call->dir, call,
 				    &ftrace_event_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
Signature · 1 line
1.6.1
#1 lore raw

PATCH 2/5 debugfs: function to know if debugfs is initialized

With ftrace, some tracers are registered in early initcalls and attempt to create files on the debugfs filesystem. Depending on when they are activated, they can try to create their file at any time. Some checks can be done on the tracing area but providing a helper to know if debugfs is registered make it really more easy.

---
 fs/debugfs/inode.c      |   16 ++++++++++++++++
 include/linux/debugfs.h |    8 ++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
fs/debugfs/inode.c +16 -0
index 81ae9ea..0662ba6 100644
@@ -30,6 +30,7 @@
 
 static struct vfsmount *debugfs_mount;
 static int debugfs_mount_count;
+static bool debugfs_registered;
 
 static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
 {
@@ -496,6 +497,16 @@ exit:
 }
 EXPORT_SYMBOL_GPL(debugfs_rename);
 
+/**
+ * debugfs_initialized - Tells whether debugfs has been registered
+ */
+bool debugfs_initialized(void)
+{
+	return debugfs_registered;
+}
+EXPORT_SYMBOL_GPL(debugfs_initialized);
+
+
 static struct kobject *debug_kobj;
 
 static int __init debugfs_init(void)
@@ -509,11 +520,16 @@ static int __init debugfs_init(void)
 	retval = register_filesystem(&debug_fs_type);
 	if (retval)
 		kobject_put(debug_kobj);
+	else
+		debugfs_registered = true;
+
 	return retval;
 }
 
 static void __exit debugfs_exit(void)
 {
+	debugfs_registered = false;
+
 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 	unregister_filesystem(&debug_fs_type);
 	kobject_put(debug_kobj);
include/linux/debugfs.h +8 -0
index af0e01d..eb5c2ba 100644
@@ -71,6 +71,9 @@ struct dentry *debugfs_create_bool(const char *name, mode_t mode,
 struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 				  struct dentry *parent,
 				  struct debugfs_blob_wrapper *blob);
+
+bool debugfs_initialized(void);
+
 #else
 
 #include <linux/err.h>
@@ -183,6 +186,11 @@ static inline struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 	return ERR_PTR(-ENODEV);
 }
 
+static inline bool debugfs_initialized(void)
+{
+	return false;
+}
+
 #endif
 
 #endif
Signature · 1 line
1.6.1
#6 lore raw

PATCH 3/5 tracing/ftrace: check if debugfs is registered before creating files

In reply to Frederic Weisbecker

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/trace/trace.c +3 -0
index ace685c..f0e1337 100644
@@ -3513,6 +3513,9 @@ struct dentry *tracing_init_dentry(void)
 	if (d_tracer)
 		return d_tracer;
 
+	if (!debugfs_initialized())
+		return NULL;
+
 	d_tracer = debugfs_create_dir("tracing", NULL);
 
 	if (!d_tracer && !once) {
Signature · 1 line
1.6.1
#12 lore raw

PATCH 4/5 tracing/events: don't use wake up for events

In reply to Frederic Weisbecker

Impact: fix hard lockup with sched switch events

Some ftrace events, such as sched wakeup, can be traced while the runqueue lock is hold. Since they are using trace_current_buffer_unlock_commit(), they call wake_up() which can try to grab the runqueue lock too, resulting in a deadlock.

Now for all event, we call a new helper:
trace_nowake_buffer_unlock_commit() which do pretty the same than trace_current_buffer_unlock_commit() except than it doesn't call trace_wake_up().

---
 kernel/trace/trace.c                |   26 +++++++++++++++++++++-----
 kernel/trace/trace.h                |    2 ++
 kernel/trace/trace_events_stage_3.h |    2 +-
 3 files changed, 24 insertions(+), 6 deletions(-)
kernel/trace/trace.c +21 -5
index f0e1337..8dddb42 100644
@@ -860,15 +860,25 @@ static void ftrace_trace_stack(struct trace_array *tr,
 static void ftrace_trace_userstack(struct trace_array *tr,
 				   unsigned long flags, int pc);
 
-void trace_buffer_unlock_commit(struct trace_array *tr,
-				struct ring_buffer_event *event,
-				unsigned long flags, int pc)
+static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
+					struct ring_buffer_event *event,
+					unsigned long flags, int pc,
+					int wake)
 {
 	ring_buffer_unlock_commit(tr->buffer, event);
 
 	ftrace_trace_stack(tr, flags, 6, pc);
 	ftrace_trace_userstack(tr, flags, pc);
-	trace_wake_up();
+
+	if (wake)
+		trace_wake_up();
+}
+
+void trace_buffer_unlock_commit(struct trace_array *tr,
+					struct ring_buffer_event *event,
+					unsigned long flags, int pc)
+{
+	__trace_buffer_unlock_commit(tr, event, flags, pc, 1);
 }
 
 struct ring_buffer_event *
@@ -882,7 +892,13 @@ trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
 					unsigned long flags, int pc)
 {
-	return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
+	return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
+}
+
+void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
+					unsigned long flags, int pc)
+{
+	return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
 }
 
 void
kernel/trace/trace.h +2 -0
index f267723..54fd9bc 100644
@@ -483,6 +483,8 @@ trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
 				  unsigned long flags, int pc);
 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
 					unsigned long flags, int pc);
+void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
+					unsigned long flags, int pc);
 
 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
 						struct trace_array_cpu *data);
kernel/trace/trace_events_stage_3.h +1 -1
index ebf215e..9a3bd49 100644
@@ -222,7 +222,7 @@ static void ftrace_raw_event_##call(proto)				\
 									\
 	assign;								\
 									\
-	trace_current_buffer_unlock_commit(event, irq_flags, pc);	\
+	trace_nowake_buffer_unlock_commit(event, irq_flags, pc);	\
 									\
 	if (call->preds && !filter_match_preds(call, entry))		\
 		ring_buffer_event_discard(event);			\
Signature · 1 line
1.6.1
#16 lore raw

PATCH 5/5 tracing/ftrace: make nop using polling wait for events on pipe

In reply to Frederic Weisbecker

Impact: display events when they arrive

Now that the events don't use wake_up() anymore, we need the nop tracer to poll waiting for events on the pipe. Especially because nop is useful to look at orphans traces types (traces types that don't rely on specific tracers) because it doesn't produce traces itself.

And unlike other tracers that trigger specific traces periodically, nop triggers no traces by itself that can wake him.

---
 kernel/trace/trace_nop.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/trace/trace_nop.c +1 -0
index 9aa84bd..394f944 100644
@@ -91,6 +91,7 @@ struct tracer nop_trace __read_mostly =
 	.name		= "nop",
 	.init		= nop_trace_init,
 	.reset		= nop_trace_reset,
+	.wait_pipe	= poll_wait_pipe,
 #ifdef CONFIG_FTRACE_SELFTEST
 	.selftest	= trace_selftest_startup_nop,
 #endif
Signature · 1 line
1.6.1
#18 lore raw
In reply to Frederic Weisbecker
Quoted 33 lines · Frederic Weisbecker

We need the filter files to be writable, the current filter file permissions make are only set readable.

---
 kernel/trace/trace_events.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events.c +2 -2
index 594d78a..19f61dd 100644
@@ -706,7 +706,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
 	system->preds = NULL;
 
-	entry = debugfs_create_file("filter", 0444, system->entry, system,
+	entry = debugfs_create_file("filter", 0644, system->entry, system,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
@@ -769,7 +769,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
 		}
 	}
 
-	entry = debugfs_create_file("filter", 0444, call->dir, call,
+	entry = debugfs_create_file("filter", 0644, call->dir, call,
 				    &ftrace_event_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
Signature · 1 line
1.6.1

Adding Tom Zanussi in Cc.

#2 lore raw

Re: [PATCH 2/5] debugfs: function to know if debugfs is initialized

In reply to Frederic Weisbecker
Quoted 7 lines · Frederic Weisbecker

With ftrace, some tracers are registered in early initcalls and attempt to create files on the debugfs filesystem. Depending on when they are activated, they can try to create their file at any time. Some checks can be done on the tracing area but providing a helper to know if debugfs is registered make it really more easy.

Quoted 6 lines
---
 fs/debugfs/inode.c      |   16 ++++++++++++++++
 include/linux/debugfs.h |    8 ++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

Greg, is this patch is fine with you?

If yes, then would you mind if we picked this up into the tracing tree - as a subsequent fix-patch relies on it. There's no debugfs patches pending that i can see that would conflict with this.

Ingo

#19 lore raw
In reply to Frederic Weisbecker
Quoted 22 lines · Frederic Weisbecker

We need the filter files to be writable, the current filter file permissions make are only set readable.

---
 kernel/trace/trace_events.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events.c +1 -1
index 594d78a..19f61dd 100644
@@ -706,7 +706,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
 	system->preds = NULL;
 
-	entry = debugfs_create_file("filter", 0444, system->entry, system,
+	entry = debugfs_create_file("filter", 0644, system->entry, system,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "

kernel/trace/trace_events.c event_create_dir(struct ftrace_event_call *call, struct dentry *d_events) · line 772

Quoted 5 lines
kernel/trace/trace_events.c +1 -1
@@ -769,7 +769,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
 		}
 	}
 
-	entry = debugfs_create_file("filter", 0444, call->dir, call,
+	entry = debugfs_create_file("filter", 0644, call->dir, call,

Interesting. Yesterday i tried the filter files and they worked - i was able to create a 'comm == bash' filter for example. Does debugfs ignore permissions perhaps?

Ingo

#7 lore raw

Re: [PATCH 3/5] tracing/ftrace: check if debugfs is registered before creating files

In reply to Frederic Weisbecker
Quoted 14 lines · Frederic Weisbecker

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

Quoted 5 lines
---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

Note - i'll delay this patch pending Greg's Ack.

Ingo

#21 lore raw
In reply to Frederic Weisbecker
Commit-ID:  9bd7d099ab3f10dd666da399c064999bae427cd9
Gitweb:     http://git.kernel.org/tip/9bd7d099ab3f10dd666da399c064999bae427cd9
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Sun, 22 Mar 2009 23:10:43 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Mar 2009 09:22:14 +0100

tracing/events: make the filter files writable

We need the filter files to be writable, the current filter file permissions are only set readable.

Cc Steven Rostedt <[email protected]>
Cc Tom Zanussi <[email protected]>

LKML-Reference: <[email protected]>

---
 kernel/trace/trace_events.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events.c +2 -2
index 594d78a..19f61dd 100644
@@ -706,7 +706,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
 	system->preds = NULL;
 
-	entry = debugfs_create_file("filter", 0444, system->entry, system,
+	entry = debugfs_create_file("filter", 0644, system->entry, system,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
@@ -769,7 +769,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
 		}
 	}
 
-	entry = debugfs_create_file("filter", 0444, call->dir, call,
+	entry = debugfs_create_file("filter", 0644, call->dir, call,
 				    &ftrace_event_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
#13 lore raw

[tip:tracing/filters] tracing/events: don't use wake up for events

In reply to Frederic Weisbecker
Commit-ID:  07edf7121374609709ef1b0889f6e7b8d6a62ec1
Gitweb:     http://git.kernel.org/tip/07edf7121374609709ef1b0889f6e7b8d6a62ec1
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Sun, 22 Mar 2009 23:10:46 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Mar 2009 09:22:14 +0100

tracing/events: don't use wake up for events

Impact: fix hard-lockup with sched switch events

Some ftrace events, such as sched wakeup, can be traced while the runqueue lock is hold. Since they are using trace_current_buffer_unlock_commit(), they call wake_up() which can try to grab the runqueue lock too, resulting in a deadlock.

Now for all event, we call a new helper:
trace_nowake_buffer_unlock_commit() which do pretty the same than trace_current_buffer_unlock_commit() except than it doesn't call trace_wake_up().

LKML-Reference: <[email protected]>

---
 kernel/trace/trace.c                |   26 +++++++++++++++++++++-----
 kernel/trace/trace.h                |    2 ++
 kernel/trace/trace_events_stage_3.h |    2 +-
 3 files changed, 24 insertions(+), 6 deletions(-)
kernel/trace/trace.c +21 -5
index e6fac0f..6bad128 100644
@@ -860,15 +860,25 @@ static void ftrace_trace_stack(struct trace_array *tr,
 static void ftrace_trace_userstack(struct trace_array *tr,
 				   unsigned long flags, int pc);
 
-void trace_buffer_unlock_commit(struct trace_array *tr,
-				struct ring_buffer_event *event,
-				unsigned long flags, int pc)
+static inline void __trace_buffer_unlock_commit(struct trace_array *tr,
+					struct ring_buffer_event *event,
+					unsigned long flags, int pc,
+					int wake)
 {
 	ring_buffer_unlock_commit(tr->buffer, event);
 
 	ftrace_trace_stack(tr, flags, 6, pc);
 	ftrace_trace_userstack(tr, flags, pc);
-	trace_wake_up();
+
+	if (wake)
+		trace_wake_up();
+}
+
+void trace_buffer_unlock_commit(struct trace_array *tr,
+					struct ring_buffer_event *event,
+					unsigned long flags, int pc)
+{
+	__trace_buffer_unlock_commit(tr, event, flags, pc, 1);
 }
 
 struct ring_buffer_event *
@@ -882,7 +892,13 @@ trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
 					unsigned long flags, int pc)
 {
-	return trace_buffer_unlock_commit(&global_trace, event, flags, pc);
+	return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 1);
+}
+
+void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
+					unsigned long flags, int pc)
+{
+	return __trace_buffer_unlock_commit(&global_trace, event, flags, pc, 0);
 }
 
 void
kernel/trace/trace.h +2 -0
index f267723..54fd9bc 100644
@@ -483,6 +483,8 @@ trace_current_buffer_lock_reserve(unsigned char type, unsigned long len,
 				  unsigned long flags, int pc);
 void trace_current_buffer_unlock_commit(struct ring_buffer_event *event,
 					unsigned long flags, int pc);
+void trace_nowake_buffer_unlock_commit(struct ring_buffer_event *event,
+					unsigned long flags, int pc);
 
 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr,
 						struct trace_array_cpu *data);
kernel/trace/trace_events_stage_3.h +1 -1
index ebf215e..9a3bd49 100644
@@ -222,7 +222,7 @@ static void ftrace_raw_event_##call(proto)				\
 									\
 	assign;								\
 									\
-	trace_current_buffer_unlock_commit(event, irq_flags, pc);	\
+	trace_nowake_buffer_unlock_commit(event, irq_flags, pc);	\
 									\
 	if (call->preds && !filter_match_preds(call, entry))		\
 		ring_buffer_event_discard(event);			\
#17 lore raw

[tip:tracing/filters] tracing/ftrace: make nop-tracer use polling wait for events on pipe

In reply to Frederic Weisbecker
Commit-ID:  7e6ea92df3fd7cbe74e7985c6f3e40255c44b201
Gitweb:     http://git.kernel.org/tip/7e6ea92df3fd7cbe74e7985c6f3e40255c44b201
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Sun, 22 Mar 2009 23:10:47 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Mar 2009 09:22:15 +0100

tracing/ftrace: make nop-tracer use polling wait for events on pipe

Impact: display events when they arrive

Now that the events don't use wake_up() anymore, we need the nop tracer to poll waiting for events on the pipe. Especially because nop is useful to look at orphan traces types (traces types that don't rely on specific tracers) because it doesn't produce traces itself.

And unlike other tracers that trigger specific traces periodically, nop triggers no traces by itself that can wake him.

LKML-Reference: <[email protected]>

---
 kernel/trace/trace_nop.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
kernel/trace/trace_nop.c +1 -0
index 9aa84bd..394f944 100644
@@ -91,6 +91,7 @@ struct tracer nop_trace __read_mostly =
 	.name		= "nop",
 	.init		= nop_trace_init,
 	.reset		= nop_trace_reset,
+	.wait_pipe	= poll_wait_pipe,
 #ifdef CONFIG_FTRACE_SELFTEST
 	.selftest	= trace_selftest_startup_nop,
 #endif
#20 lore raw
In reply to Ingo Molnar
Quoted 28 lines · Ingo Molnar
Quoted 25 lines · Frederic Weisbecker

We need the filter files to be writable, the current filter file permissions make are only set readable.

---
 kernel/trace/trace_events.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_events.c +1 -1
index 594d78a..19f61dd 100644
@@ -706,7 +706,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
 
 	system->preds = NULL;
 
-	entry = debugfs_create_file("filter", 0444, system->entry, system,
+	entry = debugfs_create_file("filter", 0644, system->entry, system,
 				    &ftrace_subsystem_filter_fops);
 	if (!entry)
 		pr_warning("Could not create debugfs "
@@ -769,7 +769,7 @@ event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
 		}
 	}
 
Quoted 8 lines

kernel/trace/trace_events.c event_create_dir(struct ftrace_event_call *call, struct dentry *d_events) · line 772

Quoted 2 lines
kernel/trace/trace_events.c +1 -1
-	entry = debugfs_create_file("filter", 0444, call->dir, call,
+	entry = debugfs_create_file("filter", 0644, call->dir, call,

Interesting. Yesterday i tried the filter files and they worked - i was able to create a 'comm == bash' filter for example. Does debugfs ignore permissions perhaps?

Ingo

That's the second patch I see this week wich proposes a 444 file supposed to be writable.
Usually it shouldn't even have been testable, so yes I think there is a little problem.

Acked-by
#3 lore raw

Re: [PATCH 2/5] debugfs: function to know if debugfs is initialized

In reply to Ingo Molnar
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Frederic Weisbecker

With ftrace, some tracers are registered in early initcalls and attempt to create files on the debugfs filesystem. Depending on when they are activated, they can try to create their file at any time. Some checks can be done on the tracing area but providing a helper to know if debugfs is registered make it really more easy.

Quoted 8 lines
Quoted 6 lines
---
 fs/debugfs/inode.c      |   16 ++++++++++++++++
 include/linux/debugfs.h |    8 ++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

Greg, is this patch is fine with you?

This patch is fine with me, feel free to add:

to it.

Quoted 3 lines

If yes, then would you mind if we picked this up into the tracing tree - as a subsequent fix-patch relies on it. There's no debugfs patches pending that i can see that would conflict with this.

Yes, that is fine, please take it through the tracing tree.

greg k-h

#8 lore raw

[tip:tracing/ftrace] tracing/ftrace: check if debugfs is registered before creating files

In reply to Frederic Weisbecker
Commit-ID:  3e1f60b80cafcb5d7e8d3665b35962fbb8fb9efa
Gitweb:     http://git.kernel.org/tip/3e1f60b80cafcb5d7e8d3665b35962fbb8fb9efa
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Sun, 22 Mar 2009 23:10:45 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Mar 2009 16:25:47 +0100

tracing/ftrace: check if debugfs is registered before creating files

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

Cc Greg Kroah-Hartman <[email protected]>
Cc Steven Rostedt <[email protected]>

LKML-Reference: <[email protected]>

---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/trace/trace.c +3 -0
index ace685c..f0e1337 100644
@@ -3513,6 +3513,9 @@ struct dentry *tracing_init_dentry(void)
 	if (d_tracer)
 		return d_tracer;
 
+	if (!debugfs_initialized())
+		return NULL;
+
 	d_tracer = debugfs_create_dir("tracing", NULL);
 
 	if (!d_tracer && !once) {
#5 lore raw

[tip:tracing/ftrace] debugfs: function to know if debugfs is initialized

In reply to Frederic Weisbecker
Commit-ID:  c0f92ba99bdeaf35f9c580291b4e1a657c67fbd4
Gitweb:     http://git.kernel.org/tip/c0f92ba99bdeaf35f9c580291b4e1a657c67fbd4
Author:     Frederic Weisbecker <[email protected]>
AuthorDate: Sun, 22 Mar 2009 23:10:44 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 23 Mar 2009 16:25:46 +0100

debugfs: function to know if debugfs is initialized

Impact: add new debugfs API

With ftrace, some tracers are registered in early initcalls and attempt to create files on the debugfs filesystem. Depending on when they are activated, they can try to create their file at any time. Some checks can be done on the tracing area but providing a helper to know if debugfs is registered make it really more easy.

LKML-Reference: <[email protected]>

---
 fs/debugfs/inode.c      |   16 ++++++++++++++++
 include/linux/debugfs.h |    8 ++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
fs/debugfs/inode.c +16 -0
index 81ae9ea..0662ba6 100644
@@ -30,6 +30,7 @@
 
 static struct vfsmount *debugfs_mount;
 static int debugfs_mount_count;
+static bool debugfs_registered;
 
 static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
 {
@@ -496,6 +497,16 @@ exit:
 }
 EXPORT_SYMBOL_GPL(debugfs_rename);
 
+/**
+ * debugfs_initialized - Tells whether debugfs has been registered
+ */
+bool debugfs_initialized(void)
+{
+	return debugfs_registered;
+}
+EXPORT_SYMBOL_GPL(debugfs_initialized);
+
+
 static struct kobject *debug_kobj;
 
 static int __init debugfs_init(void)
@@ -509,11 +520,16 @@ static int __init debugfs_init(void)
 	retval = register_filesystem(&debug_fs_type);
 	if (retval)
 		kobject_put(debug_kobj);
+	else
+		debugfs_registered = true;
+
 	return retval;
 }
 
 static void __exit debugfs_exit(void)
 {
+	debugfs_registered = false;
+
 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
 	unregister_filesystem(&debug_fs_type);
 	kobject_put(debug_kobj);
include/linux/debugfs.h +8 -0
index af0e01d..eb5c2ba 100644
@@ -71,6 +71,9 @@ struct dentry *debugfs_create_bool(const char *name, mode_t mode,
 struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 				  struct dentry *parent,
 				  struct debugfs_blob_wrapper *blob);
+
+bool debugfs_initialized(void);
+
 #else
 
 #include <linux/err.h>
@@ -183,6 +186,11 @@ static inline struct dentry *debugfs_create_blob(const char *name, mode_t mode,
 	return ERR_PTR(-ENODEV);
 }
 
+static inline bool debugfs_initialized(void)
+{
+	return false;
+}
+
 #endif
 
 #endif
#4 lore raw

Re: [PATCH 2/5] debugfs: function to know if debugfs is initialized

In reply to Greg KH
Quoted 21 lines · Greg KH
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Frederic Weisbecker

With ftrace, some tracers are registered in early initcalls and attempt to create files on the debugfs filesystem. Depending on when they are activated, they can try to create their file at any time. Some checks can be done on the tracing area but providing a helper to know if debugfs is registered make it really more easy.

Quoted 8 lines
Quoted 6 lines
---
 fs/debugfs/inode.c      |   16 ++++++++++++++++
 include/linux/debugfs.h |    8 ++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

Greg, is this patch is fine with you?

This patch is fine with me, feel free to add:

Quoted 8 lines

to it.

Quoted 3 lines

If yes, then would you mind if we picked this up into the tracing tree - as a subsequent fix-patch relies on it. There's no debugfs patches pending that i can see that would conflict with this.

Yes, that is fine, please take it through the tracing tree.

Thanks Greg!

Ingo

maintainer
#9 lore raw

Re: [PATCH 3/5] tracing/ftrace: check if debugfs is registered before creating files

In reply to Frederic Weisbecker
Quoted 25 lines · Frederic Weisbecker

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/trace/trace.c +0 -0
index ace685c..f0e1337 100644

kernel/trace/trace.c struct dentry *tracing_init_dentry(void) · line 3520

Quoted 8 lines
kernel/trace/trace.c +3 -0
@@ -3513,6 +3513,9 @@ struct dentry *tracing_init_dentry(void)
 	if (d_tracer)
 		return d_tracer;
 
+	if (!debugfs_initialized())
+		return NULL;
+
 	d_tracer = debugfs_create_dir("tracing", NULL);
 

Hmm, those tracers should separate out the debugfs code, and put it into a fs_initcall(), where debugfs should already initialized.

-- Steve

maintainer
#14 lore raw

Re: [tip:tracing/filters] tracing/events: don't use wake up for events

In reply to Frederic Weisbecker
Quoted 4 lines · Frederic Weisbecker

tracing/events: don't use wake up for events

Impact: fix hard-lockup with sched switch events

Quoted 8 lines

Some ftrace events, such as sched wakeup, can be traced while the runqueue lock is hold. Since they are using trace_current_buffer_unlock_commit(), they call wake_up() which can try to grab the runqueue lock too, resulting in a deadlock.

Now for all event, we call a new helper:
trace_nowake_buffer_unlock_commit() which do pretty the same than

Ug, that's an ugly name. It should at least be:

trace_buffer_unlock_commit_nowake

to be more in line with kernel conventions. That is where the derivitive has the same name as the original, like preempt_enable_noresched.

-- Steve

Quoted 10 lines

trace_current_buffer_unlock_commit() except than it doesn't call trace_wake_up().

LKML-Reference: <[email protected]>

#10 lore raw

Re: [PATCH 3/5] tracing/ftrace: check if debugfs is registered before creating files

In reply to Steven Rostedt
Quoted 31 lines · Steven Rostedt
Quoted 28 lines · Frederic Weisbecker

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/trace/trace.c +0 -0
index ace685c..f0e1337 100644
@@ -3513,6 +3513,9 @@ struct dentry *tracing_init_dentry(void)
 	if (d_tracer)
 		return d_tracer;
 
Quoted 8 lines

kernel/trace/trace.c struct dentry *tracing_init_dentry(void) · line 3520

Quoted 5 lines
kernel/trace/trace.c +3 -0
+	if (!debugfs_initialized())
+		return NULL;
+
 	d_tracer = debugfs_create_dir("tracing", NULL);
 

Hmm, those tracers should separate out the debugfs code, and put it into a fs_initcall(), where debugfs should already initialized.

indeed, this seems much cleaner and we'll have the debugfs structures initialized as well.

Ingo

#15 lore raw

Re: [tip:tracing/filters] tracing/events: don't use wake up for events

In reply to Steven Rostedt
Quoted 15 lines · Steven Rostedt
Quoted 4 lines · Frederic Weisbecker

tracing/events: don't use wake up for events

Impact: fix hard-lockup with sched switch events

Quoted 8 lines

Some ftrace events, such as sched wakeup, can be traced while the runqueue lock is hold. Since they are using trace_current_buffer_unlock_commit(), they call wake_up() which can try to grab the runqueue lock too, resulting in a deadlock.

Now for all event, we call a new helper:
trace_nowake_buffer_unlock_commit() which do pretty the same than

Quoted 8 lines

Ug, that's an ugly name. It should at least be:

trace_buffer_unlock_commit_nowake

to be more in line with kernel conventions. That is where the derivitive has the same name as the original, like preempt_enable_noresched.

-- Steve

Indeed.

I scratched my head the evening long to find a name that could mean:
trace_current_buffer_unlock_commit_nowake() ;-)

I think my headache brought me to this silly trace_nowake....

I will fix the name.
Thanks.

Quoted 11 lines
Quoted 11 lines

trace_current_buffer_unlock_commit() except than it doesn't call trace_wake_up().

LKML-Reference: <[email protected]>

#11 lore raw

Re: [PATCH 3/5] tracing/ftrace: check if debugfs is registered before creating files

In reply to Steven Rostedt
Quoted 40 lines · Steven Rostedt
Quoted 25 lines · Frederic Weisbecker

Impact: fix a crash with ftrace={nop,boot} parameter

If the nop or initcall tracers are launched as boot tracers, they will attempt to create their option directory and files. But these tracers are registered very early and then assigned as "boot tracers" very early if asked to.

Since they do this before debugfs has been registered (core initcall), a crash is triggered.

Another early tracers could also come later. So we fix it by checking if debugfs is initialized before creating the root tracing directory.

---
 kernel/trace/trace.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/trace/trace.c +0 -0
index ace685c..f0e1337 100644

kernel/trace/trace.c struct dentry *tracing_init_dentry(void) · line 3520

Quoted 8 lines
kernel/trace/trace.c +3 -0
@@ -3513,6 +3513,9 @@ struct dentry *tracing_init_dentry(void)
 	if (d_tracer)
 		return d_tracer;
 
+	if (!debugfs_initialized())
+		return NULL;
+
 	d_tracer = debugfs_create_dir("tracing", NULL);
 

Hmm, those tracers should separate out the debugfs code, and put it into a fs_initcall(), where debugfs should already initialized.

Quoted 2 lines

-- Steve

Indeed, I lacked a bit of imagination here :-)

Thanks!