Commit 6e94a780 authored by Steven Rostedt's avatar Steven Rostedt

tracing: Failed to create system directory

Running the following:

 # cd /sys/kernel/debug/tracing
 # echo p:i do_sys_open > kprobe_events
 # echo p:j schedule >> kprobe_events
 # cat kprobe_events
p:kprobes/i do_sys_open
p:kprobes/j schedule
 # echo p:i do_sys_open >> kprobe_events
 # cat kprobe_events
p:kprobes/j schedule
p:kprobes/i do_sys_open
 # ls /sys/kernel/debug/tracing/events/kprobes/
enable  filter  j

Notice that the 'i' is missing from the kprobes directory.

The console produces:

"Failed to create system directory kprobes"

This is because kprobes passes in a allocated name for the system
and the ftrace event subsystem saves off that name instead of creating
a duplicate for it. But the kprobes may free the system name making
the pointer to it invalid.

This bug was introduced by 92edca07 "tracing: Use direct field, type
and system names" which switched from using kstrdup() on the system name
in favor of just keeping apointer to it, as the internal ftrace event
system names are static and exist for the life of the computer being booted.

Instead of reverting back to duplicating system names again, we can use
core_kernel_data() to determine if the passed in name was allocated or
static. Then use the MSB of the ref_count to be a flag to keep track if
the name was allocated or not. Then we can still save from having to duplicate
strings that will always exist, but still copy the ones that may be freed.

Cc: stable@vger.kernel.org # 3.10
Reported-by: default avatar"zhangwei(Jovi)" <jovi.zhangwei@huawei.com>
Reported-by: default avatarMasami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Tested-by: default avatarMasami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Signed-off-by: default avatarSteven Rostedt <rostedt@goodmis.org>
parent 52d85d76
...@@ -41,6 +41,23 @@ static LIST_HEAD(ftrace_common_fields); ...@@ -41,6 +41,23 @@ static LIST_HEAD(ftrace_common_fields);
static struct kmem_cache *field_cachep; static struct kmem_cache *field_cachep;
static struct kmem_cache *file_cachep; static struct kmem_cache *file_cachep;
#define SYSTEM_FL_FREE_NAME (1 << 31)
static inline int system_refcount(struct event_subsystem *system)
{
return system->ref_count & ~SYSTEM_FL_FREE_NAME;
}
static int system_refcount_inc(struct event_subsystem *system)
{
return (system->ref_count++) & ~SYSTEM_FL_FREE_NAME;
}
static int system_refcount_dec(struct event_subsystem *system)
{
return (--system->ref_count) & ~SYSTEM_FL_FREE_NAME;
}
/* Double loops, do not use break, only goto's work */ /* Double loops, do not use break, only goto's work */
#define do_for_each_event_file(tr, file) \ #define do_for_each_event_file(tr, file) \
list_for_each_entry(tr, &ftrace_trace_arrays, list) { \ list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
...@@ -344,8 +361,8 @@ static void __put_system(struct event_subsystem *system) ...@@ -344,8 +361,8 @@ static void __put_system(struct event_subsystem *system)
{ {
struct event_filter *filter = system->filter; struct event_filter *filter = system->filter;
WARN_ON_ONCE(system->ref_count == 0); WARN_ON_ONCE(system_refcount(system) == 0);
if (--system->ref_count) if (system_refcount_dec(system))
return; return;
list_del(&system->list); list_del(&system->list);
...@@ -354,13 +371,15 @@ static void __put_system(struct event_subsystem *system) ...@@ -354,13 +371,15 @@ static void __put_system(struct event_subsystem *system)
kfree(filter->filter_string); kfree(filter->filter_string);
kfree(filter); kfree(filter);
} }
if (system->ref_count & SYSTEM_FL_FREE_NAME)
kfree(system->name);
kfree(system); kfree(system);
} }
static void __get_system(struct event_subsystem *system) static void __get_system(struct event_subsystem *system)
{ {
WARN_ON_ONCE(system->ref_count == 0); WARN_ON_ONCE(system_refcount(system) == 0);
system->ref_count++; system_refcount_inc(system);
} }
static void __get_system_dir(struct ftrace_subsystem_dir *dir) static void __get_system_dir(struct ftrace_subsystem_dir *dir)
...@@ -374,7 +393,7 @@ static void __put_system_dir(struct ftrace_subsystem_dir *dir) ...@@ -374,7 +393,7 @@ static void __put_system_dir(struct ftrace_subsystem_dir *dir)
{ {
WARN_ON_ONCE(dir->ref_count == 0); WARN_ON_ONCE(dir->ref_count == 0);
/* If the subsystem is about to be freed, the dir must be too */ /* If the subsystem is about to be freed, the dir must be too */
WARN_ON_ONCE(dir->subsystem->ref_count == 1 && dir->ref_count != 1); WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
__put_system(dir->subsystem); __put_system(dir->subsystem);
if (!--dir->ref_count) if (!--dir->ref_count)
...@@ -1274,6 +1293,14 @@ create_new_subsystem(const char *name) ...@@ -1274,6 +1293,14 @@ create_new_subsystem(const char *name)
return NULL; return NULL;
system->ref_count = 1; system->ref_count = 1;
/* Only allocate if dynamic (kprobes and modules) */
if (!core_kernel_data((unsigned long)name)) {
system->ref_count |= SYSTEM_FL_FREE_NAME;
system->name = kstrdup(name, GFP_KERNEL);
if (!system->name)
goto out_free;
} else
system->name = name; system->name = name;
system->filter = NULL; system->filter = NULL;
...@@ -1287,6 +1314,8 @@ create_new_subsystem(const char *name) ...@@ -1287,6 +1314,8 @@ create_new_subsystem(const char *name)
return system; return system;
out_free: out_free:
if (system->ref_count & SYSTEM_FL_FREE_NAME)
kfree(system->name);
kfree(system); kfree(system);
return NULL; return NULL;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment