tracing: Various clean ups and fixes
Subsystems (3)
Steven Rostedt
maintainercover tracing: Various clean ups and fixes
This is some clean ups and fixes for this merge window.
Dr. David Alan Gilbert (2):
ftrace: Remove unused list 'ftrace_direct_funcs'
ftrace: Remove unused global 'ftrace_direct_func_count'
Hao Ge (1):
eventfs: Fix a possible null pointer dereference in eventfs_find_events()
Paul E. McKenney (1):
ftrace: Use asynchronous grace period for register_ftrace_direct()
Thorsten Blum (1):
tracing: Improve benchmark test performance by using do_div()
Yuran Pereira (1):
ftrace: Replaces simple_strtoul in ftrace
Zheng Yejian (1):
ftrace: Fix possible use-after-free issue in ftrace_location()
----
fs/tracefs/event_inode.c | 7 ++--- include/linux/ftrace.h | 3 -- kernel/trace/fgraph.c | 11 ------- kernel/trace/ftrace.c | 68 ++++++++++++++++++++++-------------------- kernel/trace/trace_benchmark.c | 2 +- 5 files changed, 39 insertions(+), 52 deletions(-)
Steven Rostedt
maintainerPATCH 1/7 ftrace: Replaces simple_strtoul in ftrace
From: Yuran Pereira <[email protected]>
The function simple_strtoul performs no error checking in scenarios where the input value overflows the intended output variable. This results in this function successfully returning, even when the output does not match the input string (aka the function returns successfully even when the result is wrong).
Or as it was mentioned [1], "...simple_strtol(), simple_strtoll(), simple_strtoul(), and simple_strtoull() functions explicitly ignore overflows, which may lead to unexpected results in callers." Hence, the use of those functions is discouraged.
This patch replaces all uses of the simple_strtoul with the safer alternatives kstrtoul and kstruint.
Callers affected:
- add_rec_by_index
- set_graph_max_depth_function
Side effects of this patch:
- Since fgraph_max_depth is an unsigned int, this patch uses kstrtouint instead of kstrtoul to avoid any compiler warnings that could originate from calling the latter.
- This patch ensures that the callers of kstrtou* return accordingly when kstrtoul and kstruint fail for some reason.
In this case, both callers this patch is addressing return 0 on error.
--- kernel/trace/ftrace.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/trace/ftrace.c +3 -4
@@ -4202,12 +4202,12 @@ static int add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g, int clear_filter) { - long index = simple_strtoul(func_g->search, NULL, 0); + long index;struct ftrace_page *pg;struct dyn_ftrace *rec;/* The index starts at 1 */ -if (--index <0 ) +if (kstrtoul (func_g->search,0 , &index) || --index <0 )return 0 ;do_for_each_ftrace_rec (pg, rec) {
@@ -5817,9 +5817,8 @@ __setup("ftrace_graph_notrace=", set_graph_notrace_function);static int __init set_graph_max_depth_function (char *str) { -if (!str) +if (!str ||kstrtouint (str,0 , &fgraph_max_depth))return 0 ; - fgraph_max_depth =simple_strtoul (str,NULL ,0 );return 1 ; }__setup ("ftrace_graph_max_depth=" , set_graph_max_depth_function);
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 2/7 ftrace: Use asynchronous grace period for register_ftrace_direct()
In reply to Steven RostedtFrom: "Paul E. McKenney" <[email protected]>
When running heavy test workloads with KASAN enabled, RCU Tasks grace periods can extend for many tens of seconds, significantly slowing trace registration. Therefore, make the registration-side RCU Tasks grace period be asynchronous via call_rcu_tasks().
--- kernel/trace/ftrace.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/trace/ftrace.c +9 -4
@@ -5366,6 +5366,13 @@ static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long } } +static void register_ftrace_direct_cb (struct rcu_head *rhp) +{ + struct ftrace_hash *fhp = container_of(rhp, struct ftrace_hash, rcu); + +free_ftrace_hash (fhp); +} +/** * register_ftrace_direct - Call a custom trampoline directly * for multiple functions registered in @ops
@@ -5464,10 +5471,8 @@ int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr) out_unlock:mutex_unlock (&direct_mutex); -if (free_hash && free_hash !=EMPTY_HASH ) { -synchronize_rcu_tasks (); -free_ftrace_hash (free_hash); - } +if (free_hash && free_hash !=EMPTY_HASH ) +call_rcu_tasks (&free_hash->rcu, register_ftrace_direct_cb);if (new_hash)free_ftrace_hash (new_hash);
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 3/7 tracing: Improve benchmark test performance by using do_div()
In reply to Steven RostedtFrom: Thorsten Blum <[email protected]>
Partially revert commit d6cb38e10810 ("tracing: Use div64_u64() instead of do_div()") and use do_div() again to utilize its faster 64-by-32 division compared to the 64-by-64 division done by div64_u64().
Explicitly cast the divisor bm_cnt to u32 to prevent a Coccinelle warning reported by do_div.cocci. The warning was removed with commit d6cb38e10810 ("tracing: Use div64_u64() instead of do_div()").
Using the faster 64-by-32 division and casting bm_cnt to u32 is safe because we return early from trace_do_benchmark() if bm_cnt > UINT_MAX. This approach is already used twice in trace_do_benchmark() when calculating the standard deviation:
do_div(stddev, (u32)bm_cnt); do_div(stddev, (u32)bm_cnt - 1);
--- kernel/trace/trace_benchmark.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/trace/trace_benchmark.c +1 -1
@@ -104,7 +104,7 @@ static void trace_do_benchmark(void) stddev =0 ; delta = bm_total; - delta =div64_u64 (delta, bm_cnt); +do_div (delta, (u32 )bm_cnt); avg = delta;if (stddev >0 ) {
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 4/7 ftrace: Remove unused list ftrace_direct_funcs
In reply to Steven RostedtFrom: "Dr. David Alan Gilbert" <[email protected]>
Commit 8788ca164eb4b ("ftrace: Remove the legacy _ftrace_direct API") stopped using 'ftrace_direct_funcs' (and the associated struct ftrace_direct_func). Remove them.
Build tested only (on x86-64 with FTRACE and DYNAMIC_FTRACE enabled)
--- include/linux/ftrace.h | 1 - kernel/trace/ftrace.c | 8 -------- 2 files changed, 9 deletions(-)
include/linux/ftrace.h +0 -1
@@ -83,7 +83,6 @@ static inline void early_trace_init(void) { }struct module ;struct ftrace_hash ; -struct ftrace_direct_func ;#if defined(CONFIG_FUNCTION_TRACER ) && defined(CONFIG_MODULES ) && \ defined(CONFIG_DYNAMIC_FTRACE )
kernel/trace/ftrace.c +0 -8
@@ -5318,14 +5318,6 @@ ftrace_set_addr(struct ftrace_ops *ops, unsigned long *ips, unsigned int cnt,#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS -struct ftrace_direct_func { -struct list_head next; -unsigned long addr; -int count; -}; - -static LIST_HEAD (ftrace_direct_funcs); -static int register_ftrace_function_nolock (struct ftrace_ops *ops);/*
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 5/7 ftrace: Remove unused global ftrace_direct_func_count
In reply to Steven RostedtFrom: "Dr. David Alan Gilbert" <[email protected]>
Commit 8788ca164eb4b ("ftrace: Remove the legacy _ftrace_direct API") stopped setting the 'ftrace_direct_func_count' variable, but left it around. Clean it up.
--- include/linux/ftrace.h | 2 -- kernel/trace/fgraph.c | 11 ----------- kernel/trace/ftrace.c | 1 - 3 files changed, 14 deletions(-)
include/linux/ftrace.h +0 -2
@@ -413,7 +413,6 @@ struct ftrace_func_entry { };#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS -extern int ftrace_direct_func_count;unsigned long ftrace_find_rec_direct (unsigned long ip);int register_ftrace_direct (struct ftrace_ops *ops,unsigned long addr); int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
@@ -425,7 +424,6 @@ void ftrace_stub_direct_tramp(void);#else struct ftrace_ops ; -# define ftrace_direct_func_count 0static inline unsigned long ftrace_find_rec_direct (unsigned long ip) {return 0 ;
kernel/trace/fgraph.c +0 -11
@@ -125,17 +125,6 @@ int function_graph_enter(unsigned long ret, unsigned long func, {struct ftrace_graph_ent trace; -#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS -/* -* Skip graph tracing if the return location is served by direct trampoline, -* since call sequence and return addresses are unpredictable anyway. -* Ex: BPF trampoline may call original function and may skip frame -* depending on type of BPF programs attached. -*/ -if (ftrace_direct_func_count && -ftrace_find_rec_direct (ret -MCOUNT_INSN_SIZE )) -return -EBUSY ; -#endif trace.func = func; trace.depth = ++current->curr_ret_depth;
kernel/trace/ftrace.c +0 -1
@@ -2538,7 +2538,6 @@ ftrace_find_unique_ops(struct dyn_ftrace *rec)/* Protected by rcu_tasks for reading, and direct_mutex for writing */ static struct ftrace_hash __rcu *direct_functions =EMPTY_HASH ;static DEFINE_MUTEX (direct_mutex); -int ftrace_direct_func_count;/* * Search the direct_functions hash to see if the given instruction pointer
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 6/7 ftrace: Fix possible use-after-free issue in ftrace_location()
In reply to Steven RostedtFrom: Zheng Yejian <[email protected]>
KASAN reports a bug:
BUG: KASAN: use-after-free in ftrace_location+0x90/0x120 Read of size 8 at addr ffff888141d40010 by task insmod/424 CPU: 8 PID: 424 Comm: insmod Tainted: G W 6.9.0-rc2+ [...] Call Trace: <TASK> dump_stack_lvl+0x68/0xa0 print_report+0xcf/0x610 kasan_report+0xb5/0xe0 ftrace_location+0x90/0x120 register_kprobe+0x14b/0xa40 kprobe_init+0x2d/0xff0 [kprobe_example] do_one_initcall+0x8f/0x2d0 do_init_module+0x13a/0x3c0 load_module+0x3082/0x33d0 init_module_from_file+0xd2/0x130 __x64_sys_finit_module+0x306/0x440 do_syscall_64+0x68/0x140 entry_SYSCALL_64_after_hwframe+0x71/0x79
The root cause is that, in lookup_rec(), ftrace record of some address is being searched in ftrace pages of some module, but those ftrace pages at the same time is being freed in ftrace_release_mod() as the corresponding module is being deleted:
CPU1 | CPU2
register_kprobes() { | delete_module() {
check_kprobe_address_safe() { |
arch_check_ftrace_location() { |
ftrace_location() { |
lookup_rec() // USE! | ftrace_release_mod() // Free!To fix this issue:
1. Hold rcu lock as accessing ftrace pages in ftrace_location_range();
2. Use ftrace_location_range() instead of lookup_rec() in
ftrace_location();
3. Call synchronize_rcu() before freeing any ftrace pages both in ftrace_process_locs()/ftrace_release_mod()/ftrace_free_mem().
--- kernel/trace/ftrace.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-)
kernel/trace/ftrace.c +23 -16
@@ -1595,12 +1595,15 @@ static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)unsigned long ftrace_location_range (unsigned long start,unsigned long end) {struct dyn_ftrace *rec; +unsigned long ip =0 ; +rcu_read_lock (); rec =lookup_rec (start, end);if (rec) -return rec->ip; + ip = rec->ip; +rcu_read_unlock (); -return 0 ; +return ip; }/**
@@ -1614,25 +1617,22 @@ unsigned long ftrace_location_range(unsigned long start, unsigned long end)*/ unsigned long ftrace_location (unsigned long ip) { -struct dyn_ftrace *rec; +unsigned long loc;unsigned long offset;unsigned long size; - rec =lookup_rec (ip, ip); -if (!rec) { + loc =ftrace_location_range (ip, ip); +if (!loc) {if (!kallsyms_lookup_size_offset (ip, &size, &offset)) goto out;/* map sym+0 to __fentry__ */ if (!offset) - rec =lookup_rec (ip, ip + size -1 ); + loc =ftrace_location_range (ip, ip + size -1 ); } -if (rec) -return rec->ip; - out: -return 0 ; +return loc; }/**
@@ -6591,6 +6591,8 @@ static int ftrace_process_locs(struct module *mod,/* We should have used all pages unless we skipped some */ if (pg_unuse) {WARN_ON (!skipped); +/* Need to synchronize with ftrace_location_range() */ +synchronize_rcu ();ftrace_free_pages (pg_unuse); }return ret;
@@ -6804,6 +6806,9 @@ void ftrace_release_mod(struct module *mod) out_unlock:mutex_unlock (&ftrace_lock); +/* Need to synchronize with ftrace_location_range() */ +if (tmp_page) +synchronize_rcu ();for (pg = tmp_page; pg; pg = tmp_page) {/* Needs to be called outside of ftrace_lock */
@@ -7137,6 +7142,7 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)unsigned long start = (unsigned long )(start_ptr);unsigned long end = (unsigned long )(end_ptr);struct ftrace_page **last_pg = &ftrace_pages_start; +struct ftrace_page *tmp_page =NULL ;struct ftrace_page *pg;struct dyn_ftrace *rec;struct dyn_ftrace key;
@@ -7178,12 +7184,8 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr) ftrace_update_tot_cnt--;if (!pg->index) { *last_pg = pg->next; -if (pg->records) { -free_pages ((unsigned long )pg->records, pg->order); - ftrace_number_of_pages -=1 << pg->order; - } - ftrace_number_of_groups--; -kfree (pg); + pg->next = tmp_page; + tmp_page = pg; pg = container_of(last_pg, struct ftrace_page, next);if (!(*last_pg)) ftrace_pages = pg;
@@ -7200,6 +7202,11 @@ void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)clear_func_from_hashes (func);kfree (func); } +/* Need to synchronize with ftrace_location_range() */ +if (tmp_page) { +synchronize_rcu (); +ftrace_free_pages (tmp_page); + } }void __init ftrace_free_init_mem (void )
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 7/7 eventfs: Fix a possible null pointer dereference in eventfs_find_events()
In reply to Steven RostedtFrom: Hao Ge <[email protected]>
In function eventfs_find_events,there is a potential null pointer that may be caused by calling update_events_attr which will perform some operations on the members of the ei struct when ei is NULL.
Hence,When ei->is_freed is set,return NULL directly.
--- fs/tracefs/event_inode.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
fs/tracefs/event_inode.c +3 -4
@@ -345,10 +345,9 @@ static struct eventfs_inode *eventfs_find_events(struct dentry *dentry)* If the ei is being freed, the ownership of the children * doesn't matter. */ -if (ei->is_freed) { - ei =NULL ; -break ; - } +if (ei->is_freed) +return NULL ; +// Walk upwards until you find the events inode }while (!ei->is_events);
Signature · 1 line
2.43.0