function_graph: Allow multiple users for function graph tracing
Steven Rostedt
maintainercover function_graph: Allow multiple users for function graph tracing
[
Resend for some of you as I missed a comma in the Cc and quilt died sending this out.
]
This is a continuation of the function graph multi user code. I wrote a proof of concept back in 2019 of this code[1] and Masami started cleaning it up. I started from Masami's work v10 that can be found here:
https://lore.kernel.org/linux-trace-kernel/171509088006.162236.7227326999861366050.stgit@devnote2/
This is *only* the code that allows multiple users of function graph tracing. This is not the fprobe work that Masami is working to add on top of it. As Masami took my proof of concept, there was still several things I disliked about that code. Instead of having Masami clean it up even more, I decided to take over on just my code and change it up a bit.
The biggest changes from where Masami left off is mostly renaming more variables, macros, and function names. I fixed up the current comments and added more to make the code a bit more understandable.
At the end of the series, I added two patches to optimize the entry and exit. On entry, there was a loop that iterated the 16 elements of the fgraph_array[] looking for any that may have a gops registered to it. It's quite a waste to do that loop if there's only one registered user. To fix that, I added a fgraph_array_bitmask that has its bits set that correspond to the elements of the array. Then a simple for_each_set_bit() is used for the iteration. I do the same thing at the exit callback of the function where it iterates over the bits of the bitmap saved on the ret_stack.
I also noticed that Masami added code to handle tail calls in the unwinder and had that in one of my patches. I took that code out and made it a separate patch with Masami as the author.
The diff between this and Masami's last update is at the end of this email.
Based on Linus commit: 0eb03c7e8e2a4cc3653eb5eeb2d2001182071215
[1] https://lore.kernel.org/all/[email protected]/
Masami Hiramatsu (Google) (3):
function_graph: Handle tail calls for stack unwinding
function_graph: Use a simple LRU for fgraph_array index number
ftrace: Add multiple fgraph storage selftest
Steven Rostedt (Google) (2):
function_graph: Use for_each_set_bit() in __ftrace_return_to_handler()
function_graph: Use bitmask to loop on fgraph entry
Steven Rostedt (VMware) (15):
function_graph: Convert ret_stack to a series of longs
fgraph: Use BUILD_BUG_ON() to make sure we have structures divisible by long
function_graph: Add an array structure that will allow multiple callbacks
function_graph: Allow multiple users to attach to function graph
function_graph: Remove logic around ftrace_graph_entry and return ftrace/function_graph: Pass fgraph_ops to function graph callbacks
ftrace: Allow function_graph tracer to be enabled in instances
ftrace: Allow ftrace startup flags to exist without dynamic ftrace
function_graph: Have the instances use their own ftrace_ops for filtering
function_graph: Add "task variables" per task for fgraph_ops
function_graph: Move set_graph_function tests to shadow stack global var
function_graph: Move graph depth stored data to shadow stack global var
function_graph: Move graph notrace bit to shadow stack global var
function_graph: Implement fgraph_reserve_data() and fgraph_retrieve_data()
function_graph: Add selftest for passing local variables
----
arch/arm64/kernel/ftrace.c | 21 +- arch/loongarch/kernel/ftrace_dyn.c | 15 +- arch/powerpc/kernel/trace/ftrace.c | 3 +- arch/riscv/kernel/ftrace.c | 15 +- arch/x86/kernel/ftrace.c | 19 +- include/linux/ftrace.h | 42 +- include/linux/sched.h | 2 +- include/linux/trace_recursion.h | 39 -- kernel/trace/fgraph.c | 994 ++++++++++++++++++++++++++++------- kernel/trace/ftrace.c | 11 +- kernel/trace/ftrace_internal.h | 2 - kernel/trace/trace.h | 94 +++- kernel/trace/trace_functions.c | 8 + kernel/trace/trace_functions_graph.c | 96 ++-- kernel/trace/trace_irqsoff.c | 10 +- kernel/trace/trace_sched_wakeup.c | 10 +- kernel/trace/trace_selftest.c | 259 ++++++++- 17 files changed, 1330 insertions(+), 310 deletions(-)
kernel/trace/fgraph.c +125 -85
@@ -18,29 +18,36 @@#include "ftrace_internal.h" #include "trace.h" -#define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) -#define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long))/* -* On entry to a function (via function_graph_enter()), a new ftrace_ret_stack -* is allocated on the task's ret_stack with bitmap entry, then each -* fgraph_ops on the fgraph_array[]'s entryfunc is called and if that returns -* non-zero, the index into the fgraph_array[] for that fgraph_ops is recorded -* on the bitmap entry as a bit flag. -* -* The top of the ret_stack (when not empty) will always have a reference -* to the last ftrace_ret_stack saved. All references to the -* ftrace_ret_stack has the format of: +* FGRAPH_FRAME_SIZE: Size in bytes of the meta data on the shadow stack +* FGRAPH_FRAME_OFFSET: Size in long words of the meta data frame +*/ +#define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) +#define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long)) + +/* +* On entry to a function (via function_graph_enter()), a new fgraph frame +* (ftrace_ret_stack) is pushed onto the stack as well as a word that +* holds a bitmask and a type (called "bitmap"). The bitmap is defined as: * * bits: 0 - 9 offset in words from the previous ftrace_ret_stack -* (bitmap type should have FGRAPH_FRAME_OFFSET always) +* * bits: 10 - 11 Type of storage * 0 - reserved * 1 - bitmap of fgraph_array index * 2 - reserved data * -* For bitmap of fgraph_array index +* For type with "bitmap of fgraph_array index" (FGRAPH_TYPE_BITMAP): * bits: 12 - 27 The bitmap of fgraph_ops fgraph_array index +* That is, it's a bitmask of 0-15 (16 bits) +* where if a corresponding ops in the fgraph_array[] +* expects a callback from the return of the function +* it's corresponding bit will be set. +* +* +* The top of the ret_stack (when not empty) will always have a reference +* word that points to the last fgraph frame that was saved. * * For reserved data: * bits: 12 - 17 The size in words that is stored
@@ -52,7 +59,7 @@* stored two words of data, this is what will be on the task's shadow * ret_stack: (the stack grows upward) * -* ret_stack[SHADOW_STACK_IN_WORD] +* ret_stack[SHADOW_STACK_OFFSET] * | SHADOW_STACK_TASK_VARS(ret_stack)[15] | * ... * | SHADOW_STACK_TASK_VARS(ret_stack)[0] |
@@ -60,6 +67,8 @@* ... * | | <- task->curr_ret_stack * +--------------------------------------------+ +* | (3 << 12) | (3 << 10) | FGRAPH_FRAME_OFFSET| +* | *or put another way* | * | (3 << FGRAPH_DATA_INDEX_SHIFT)| \ | This is for fgraph_ops[3]. * | ((2 - 1) << FGRAPH_DATA_SHIFT)| \ | The data size is 2 words. * | (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT)| \ |
@@ -67,7 +76,9 @@* +--------------------------------------------+ ( It is 4 words from the ret_stack) * | STORED DATA WORD 2 | * | STORED DATA WORD 1 | -* +------i-------------------------------------+ +* +--------------------------------------------+ +* | (9 << 12) | (1 << 10) | FGRAPH_FRAME_OFFSET| +* | *or put another way* | * | (BIT(3)|BIT(0)) << FGRAPH_INDEX_SHIFT | \ | * | FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT| \ | * | (offset1:FGRAPH_FRAME_OFFSET) | <- the offset1 is from here
@@ -82,17 +93,24 @@* * If a backtrace is required, and the real return pointer needs to be * fetched, then it looks at the task's curr_ret_stack offset, if it -* is greater than zero (reserved, or right before poped), it would mask +* is greater than zero (reserved, or right before popped), it would mask * the value by FGRAPH_FRAME_OFFSET_MASK to get the offset of the * ftrace_ret_stack structure stored on the shadow stack. */ -#define FGRAPH_FRAME_OFFSET_SIZE 10 -#define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_SIZE - 1, 0) +/* +* The following is for the top word on the stack: +* +* FGRAPH_FRAME_OFFSET (0-9) holds the offset delta to the fgraph frame +* FGRAPH_TYPE (10-11) holds the type of word this is. +* (RESERVED or BITMAP) +*/ +#define FGRAPH_FRAME_OFFSET_BITS 10 +#define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_BITS - 1, 0) -#define FGRAPH_TYPE_SIZE 2 -#define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_SIZE - 1, 0) -#define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_SIZE +#define FGRAPH_TYPE_BITS 2 +#define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_BITS - 1, 0) +#define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_BITSenum {FGRAPH_TYPE_RESERVED =0 ,
@@ -100,31 +118,48 @@ enum { FGRAPH_TYPE_DATA = 2, }; -#define FGRAPH_INDEX_SIZE 16 -#define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_SIZE - 1, 0) -#define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_SIZE) +/* +* For BITMAP type: +* FGRAPH_INDEX (12-27) bits holding the gops index wanting return callback called +*/ +#define FGRAPH_INDEX_BITS 16 +#define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_BITS - 1, 0) +#define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) -/* The data size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words. */ -#define FGRAPH_DATA_SIZE 5 -#define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_SIZE - 1, 0) -#define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_SIZE) -#define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_SIZE)) +/* +* For DATA type: +* FGRAPH_DATA (12-17) bits hold the size of data (in words) +* FGRAPH_INDEX (18-23) bits hold the index for which gops->idx the data is for +* +* Note: +* data_size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words. +*/ +#define FGRAPH_DATA_BITS 5 +#define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_BITS - 1, 0) +#define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) +#define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_BITS)) -#define FGRAPH_DATA_INDEX_SIZE 4 -#define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_SIZE - 1, 0) -#define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_SIZE) +#define FGRAPH_DATA_INDEX_BITS 4 +#define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_BITS - 1, 0) +#define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_BITS)#define FGRAPH_MAX_INDEX \ - ((FGRAPH_INDEX_SIZE << FGRAPH_DATA_SIZE) + FGRAPH_RET_INDEX) + ((FGRAPH_INDEX_SIZE << FGRAPH_DATA_BITS) + FGRAPH_RET_INDEX) -#define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_SIZE +#define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_BITS -#define SHADOW_STACK_SIZE (PAGE_SIZE) -#define SHADOW_STACK_IN_WORD (SHADOW_STACK_SIZE / sizeof(long)) +/* +* SHADOW_STACK_SIZE: The size in bytes of the entire shadow stack +* SHADOW_STACK_OFFSET: The size in long words of the shadow stack +* SHADOW_STACK_MAX_OFFSET: The max offset of the stack for a new frame to be added +*/ +#define SHADOW_STACK_SIZE (PAGE_SIZE) +#define SHADOW_STACK_OFFSET (SHADOW_STACK_SIZE / sizeof(long))/* Leave on a buffer at the end */ #define SHADOW_STACK_MAX_OFFSET \ - (SHADOW_STACK_IN_WORD - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) + (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) +/* RET_STACK(): Return the frame from a given @offset from task @t */ #define RET_STACK (t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset]))/*
@@ -132,12 +167,13 @@ enum {* ret_stack to store task specific state. */ #define SHADOW_STACK_TASK_VARS (ret_stack) \ - ((unsigned long *)(&(ret_stack)[SHADOW_STACK_IN_WORD - FGRAPH_ARRAY_SIZE])) + ((unsigned long *)(&(ret_stack)[SHADOW_STACK_OFFSET - FGRAPH_ARRAY_SIZE]))DEFINE_STATIC_KEY_FALSE (kill_ftrace_graph);int ftrace_graph_active;static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +static unsigned long fgraph_array_bitmask;/* LRU index table for fgraph_array */ static int fgraph_lru_table[FGRAPH_ARRAY_SIZE ];
@@ -162,6 +198,8 @@ static int fgraph_lru_release_index(int idx) fgraph_lru_table[fgraph_lru_last] = idx; fgraph_lru_last = (fgraph_lru_last +1 ) %FGRAPH_ARRAY_SIZE ; + +clear_bit (idx, &fgraph_array_bitmask);return 0 ; }
@@ -176,70 +214,77 @@ static int fgraph_lru_alloc_index(void) fgraph_lru_table[fgraph_lru_next] =-1 ; fgraph_lru_next = (fgraph_lru_next +1 ) %FGRAPH_ARRAY_SIZE ; + +set_bit (idx, &fgraph_array_bitmask);return idx; } +/* Get the offset to the fgraph frame from a ret_stack value */ static inline int __get_offset (unsigned long val) {return val &FGRAPH_FRAME_OFFSET_MASK ; } +/* Get the type of word from a ret_stack value */ static inline int __get_type (unsigned long val) {return (val >>FGRAPH_TYPE_SHIFT ) &FGRAPH_TYPE_MASK ; } +/* Get the data_index for a DATA type ret_stack word */ static inline int __get_data_index (unsigned long val) {return (val >>FGRAPH_DATA_INDEX_SHIFT ) &FGRAPH_DATA_INDEX_MASK ; } +/* Get the data_size for a DATA type ret_stack word */ static inline int __get_data_size (unsigned long val) {return ((val >>FGRAPH_DATA_SHIFT ) &FGRAPH_DATA_MASK ) +1 ; } +/* Get the word from the ret_stack at @offset */ static inline unsigned long get_fgraph_entry (struct task_struct *t,int offset) {return t->ret_stack[offset]; } +/* Get the FRAME_OFFSET from the word from the @offset on ret_stack */ static inline int get_frame_offset (struct task_struct *t,int offset) {return __get_offset (t->ret_stack[offset]); } +/* Get FGRAPH_TYPE from the word from the @offset at ret_stack */ static inline int get_fgraph_type (struct task_struct *t,int offset) {return __get_type (t->ret_stack[offset]); } +/* For BITMAP type: get the bitmask from the @offset at ret_stack */ static inline unsigned long -get_fgraph_index_bitmap (struct task_struct *t,int offset) +get_bitmap_bits (struct task_struct *t,int offset) {return (t->ret_stack[offset] >>FGRAPH_INDEX_SHIFT ) &FGRAPH_INDEX_MASK ; } +/* For BITMAP type: set the bits in the bitmap bitmask at @offset on ret_stack */ static inline void -set_fgraph_index_bitmap (struct task_struct *t,int offset,unsigned long bitmap) -{ - t->ret_stack[offset] = (bitmap <<FGRAPH_INDEX_SHIFT ) | - (FGRAPH_TYPE_BITMAP <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; -} - -static inline bool is_fgraph_index_set (struct task_struct *t,int offset,int idx) +set_bitmap_bits (struct task_struct *t,int offset,unsigned long bitmap) { -return !!(get_fgraph_index_bitmap (t, offset) &BIT (idx)); + t->ret_stack[offset] |= (bitmap <<FGRAPH_INDEX_SHIFT ); } +/* Write the bitmap to the ret_stack at @offset (does index, offset and bitmask) */ static inline void -add_fgraph_index_bitmap (struct task_struct *t,int offset,unsigned long bitmap) +set_bitmap (struct task_struct *t,int offset,unsigned long bitmap) { - t->ret_stack[offset] |= (bitmap <<FGRAPH_INDEX_SHIFT ); + t->ret_stack[offset] = (bitmap <<FGRAPH_INDEX_SHIFT ) | + (FGRAPH_TYPE_BITMAP <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; } -/* Note: @offset is the offset of FGRAPH_TYPE_DATA entry. */ -static inline void *get_fgraph_data (struct task_struct *t,int offset) +/* For DATA type: get the data saved under the ret_stack word at @offset */ +static inline void *get_data_type_data (struct task_struct *t,int offset) {unsigned long val = t->ret_stack[offset];
@@ -249,7 +294,8 @@ static inline void *get_fgraph_data(struct task_struct *t, int offset)return (void *)&t->ret_stack[offset]; } -static inline unsigned long make_fgraph_data (int idx,int size,int offset) +/* Create the ret_stack word for a DATA type */ +static inline unsigned long make_data_type_val (int idx,int size,int offset) {return (idx <<FGRAPH_DATA_INDEX_SHIFT ) | ((size - 1) << FGRAPH_DATA_SHIFT) |
@@ -326,7 +372,7 @@ void *fgraph_reserve_data(int idx, int size_bytes)if (unlikely (curr_ret_stack >=SHADOW_STACK_MAX_OFFSET ))return NULL ; - val =make_fgraph_data (idx, data_size,__get_offset (val) + data_size +1 ); + val =make_data_type_val (idx, data_size,__get_offset (val) + data_size +1 );/* Set the last word to be reserved */ current->ret_stack[curr_ret_stack -1 ] = val;
@@ -371,7 +417,7 @@ void *fgraph_retrieve_data(int idx, int *size_bytes) found:if (size_bytes) *size_bytes =__get_data_size (val) *sizeof (long ); -return get_fgraph_data (current, offset); +return get_data_type_data (current, offset); }/**
@@ -394,6 +440,9 @@ unsigned long *fgraph_get_task_var(struct fgraph_ops *gops)* @offset: The offset into @t->ret_stack to find the ret_stack entry * @frame_offset: Where to place the offset into @t->ret_stack of that entry * +* Returns a pointer to the previous ret_stack below @offset or NULL +* when it reaches the bottom of the stack. +* * Calling this with: * * offset = task->curr_ret_stack;
@@ -493,30 +542,21 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,if (!current->ret_stack)return -EBUSY ; -/* -* At first, check whether the previous fgraph callback is pushed by -* the fgraph on the same function entry. -* But if @func is the self tail-call function, we also need to ensure -* the ret_stack is not for the previous call by checking whether the -* bit of @fgraph_idx is set or not. -*/ - ret_stack =get_ret_stack (current, current->curr_ret_stack, &offset); -if (ret_stack && ret_stack->func == func && -get_fgraph_type (current, offset +FGRAPH_FRAME_OFFSET ) ==FGRAPH_TYPE_BITMAP && - !is_fgraph_index_set (current, offset +FGRAPH_FRAME_OFFSET , fgraph_idx)) -return offset +FGRAPH_FRAME_OFFSET ; +BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); +/* Set val to "reserved" with the delta to the new fgraph frame */ val = (FGRAPH_TYPE_RESERVED <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; -BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); -/* * We must make sure the ret_stack is tested before we read * anything else. */ smp_rmb (); -/* The return trace stack is full */ +/* +* Check if there's room on the shadow stack to fit a fraph frame +* and a bitmap word. +*/ if (current->curr_ret_stack +FGRAPH_FRAME_OFFSET +1 >=SHADOW_STACK_MAX_OFFSET ) {atomic_inc (¤t->trace_overrun);return -EBUSY ;
@@ -545,7 +585,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,* at least a correct offset! */ barrier (); - current->curr_ret_stack = offset +1 ; +WRITE_ONCE (current->curr_ret_stack, offset +1 );/* * This next barrier is to ensure that an interrupt coming in * will not corrupt what we are about to write.
@@ -597,7 +637,8 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (offset <0 ) goto out; -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +for_each_set_bit (i, &fgraph_array_bitmask, +sizeof (fgraph_array_bitmask) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i];int save_curr_ret_stack;
@@ -620,7 +661,7 @@ int function_graph_enter(unsigned long ret, unsigned long func,* Since this function uses fgraph_idx = 0 as a tail-call checking * flag, set that bit always. */ -set_fgraph_index_bitmap (current, offset, bitmap |BIT (0 )); +set_bitmap (current, offset, bitmap |BIT (0 ));return 0 ; out_ret:
@@ -659,9 +700,9 @@ int function_graph_enter_ops(unsigned long ret, unsigned long func, save_curr_ret_stack = current->curr_ret_stack;if (gops->entryfunc (&trace, gops)) {if (type ==FGRAPH_TYPE_RESERVED ) -set_fgraph_index_bitmap (current, offset,BIT (gops->idx)); +set_bitmap (current, offset,BIT (gops->idx));else -add_fgraph_index_bitmap (current, offset,BIT (gops->idx)); +set_bitmap_bits (current, offset,BIT (gops->idx));return 0 ; }else current->curr_ret_stack = save_curr_ret_stack;
@@ -791,12 +832,11 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs trace.retval =fgraph_ret_regs_return_value (ret_regs);#endif - bitmap =get_fgraph_index_bitmap (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + bitmap =get_bitmap_bits (current, offset); + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)continue ;
@@ -879,9 +919,10 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int i = task->curr_ret_stack; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;while (i >0 ) {
@@ -891,14 +932,13 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,/* * For the tail-call, there would be 2 or more ftrace_ret_stacks on * the ret_stack, which records "return_to_handler" as the return -* address excpt for the last one. +* address except for the last one. * But on the real stack, there should be 1 entry because tail-call * reuses the return address on the stack and jump to the next function. * Thus we will continue to find real return address. */ if (ret_stack->retp == retp && - ret_stack->ret != - (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) + ret_stack->ret != return_handler)return ret_stack->ret; }
@@ -909,10 +949,11 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int offset = task->curr_ret_stack;int i; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;if (!idx)
@@ -921,8 +962,7 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, i = *idx;do { ret_stack =get_ret_stack (task, offset, &offset); -if (ret_stack && ret_stack->ret == - (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret_stack && ret_stack->ret == return_handler)continue ; i--; }while (i >=0 && ret_stack);
Steven Rostedt
maintainerPATCH 1/20 function_graph: Convert ret_stack to a series of longs
From: "Steven Rostedt (VMware)" <[email protected]>
In order to make it possible to have multiple callbacks registered with the function_graph tracer, the retstack needs to be converted from an array of ftrace_ret_stack structures to an array of longs. This will allow to store the list of callbacks on the stack for the return side of the functions.
--- include/linux/sched.h | 2 +- kernel/trace/fgraph.c | 136 +++++++++++++++++++++++++----------------- 2 files changed, 83 insertions(+), 55 deletions(-)
include/linux/sched.h +1 -1
@@ -1402,7 +1402,7 @@ struct task_struct {int curr_ret_depth;/* Stack of return addresses for return function tracing: */ -struct ftrace_ret_stack *ret_stack; +unsigned long *ret_stack;/* Timestamp for last schedule: */ unsigned long long ftrace_timestamp;
kernel/trace/fgraph.c +82 -54
319 lines not on this page · raw message
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 2/20 fgraph: Use BUILD_BUG_ON() to make sure we have structures divisible by long
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Instead of using "ALIGN()", use BUILD_BUG_ON() as the structures should always be divisible by sizeof(long).
Co-developed with Masami Hiramatsu:
--- kernel/trace/fgraph.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/trace/fgraph.c +5 -1
@@ -33,7 +33,7 @@* SHADOW_STACK_MAX_OFFSET: The max offset of the stack for a new frame to be added */ #define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) -#define FGRAPH_FRAME_OFFSET (ALIGN(FGRAPH_FRAME_SIZE, sizeof(long)) / sizeof(long)) +#define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long))#define SHADOW_STACK_SIZE (PAGE_SIZE)#define SHADOW_STACK_OFFSET \ (ALIGN(SHADOW_STACK_SIZE, sizeof(long)) / sizeof(long))
@@ -103,6 +103,8 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,if (!current->ret_stack)return -EBUSY ; +BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); +/* * We must make sure the ret_stack is tested before we read * anything else.
@@ -326,6 +328,8 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int idx) {int index = task->curr_ret_stack; +BUILD_BUG_ON (FGRAPH_FRAME_SIZE %sizeof (long )); + index -=FGRAPH_FRAME_OFFSET * (idx +1 );if (index <0 )return NULL ;
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 3/20 function_graph: Add an array structure that will allow multiple callbacks
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add an array structure that will eventually allow the function graph tracer to have up to 16 simultaneous callbacks attached. It's an array of 16 fgraph_ops pointers, that is assigned when one is registered. On entry of a function the entry of the first item in the array is called, and if it returns zero, then the callback returns non zero if it wants the return callback to be called on exit of the function.
The array will simplify the process of having more than one callback attached to the same function, as its index into the array can be stored on the shadow stack. We need to only save the index, because this will allow the fgraph_ops to be freed before the function returns (which may happen if the function call schedule for a long time).
Co-developed with Masami Hiramatsu:
--- kernel/trace/fgraph.c | 114 ++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 33 deletions(-)
kernel/trace/fgraph.c +81 -33
@@ -52,6 +52,11 @@DEFINE_STATIC_KEY_FALSE (kill_ftrace_graph);int ftrace_graph_active; +static int fgraph_array_cnt; +#define FGRAPH_ARRAY_SIZE 16 + +static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +/* Both enabled by default (can be cleared by function_graph tracer flags */ static bool fgraph_sleep_time = true;
@@ -75,6 +80,20 @@ int __weak ftrace_disable_ftrace_graph_caller(void) }#endif +int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace) +{ +return 0 ; +} + +static void ftrace_graph_ret_stub (struct ftrace_graph_ret *trace) +{ +} + +static struct fgraph_ops fgraph_stub = { + .entryfunc = ftrace_graph_entry_stub, + .retfunc = ftrace_graph_ret_stub, +}; +/** * ftrace_graph_stop - set to permanently disable function graph tracing *
@@ -161,7 +180,7 @@ int function_graph_enter(unsigned long ret, unsigned long func, goto out;/* Only trace if the calling function expects to */ -if (!ftrace_graph_entry (&trace)) +if (!fgraph_array[0 ]->entryfunc (&trace)) goto out_ret;return 0 ;
@@ -276,7 +295,7 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs trace.retval =fgraph_ret_regs_return_value (ret_regs);#endif trace.rettime =trace_clock_local (); -ftrace_graph_return (&trace); + fgraph_array[0 ]->retfunc (&trace);/* * The ftrace_graph_return() may still access the current * ret_stack structure, we need to make sure the update of
@@ -412,11 +431,6 @@ void ftrace_graph_sleep_time_control(bool enable) fgraph_sleep_time = enable; } -int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace) -{ -return 0 ; -} -/* * Simply points to ftrace_stub, but with the proper protocol. * Defined by the linker script in linux/vmlinux.lds.h
@@ -654,37 +668,54 @@ static int start_graph_tracing(void)int register_ftrace_graph (struct fgraph_ops *gops) {int ret =0 ; +int i;mutex_lock (&ftrace_lock); -/* we currently allow only one tracer registered at a time */ -if (ftrace_graph_active) { +if (!fgraph_array[0 ]) { +/* The array must always have real data on it */ +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) + fgraph_array[i] = &fgraph_stub; + } + +/* Look for an available spot */ +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +if (fgraph_array[i] == &fgraph_stub) +break ; + } +if (i >=FGRAPH_ARRAY_SIZE ) { ret = -EBUSY ; goto out; } -register_pm_notifier (&ftrace_suspend_notifier); + fgraph_array[i] = gops; +if (i +1 > fgraph_array_cnt) + fgraph_array_cnt = i +1 ; ftrace_graph_active++; - ret =start_graph_tracing (); -if (ret) { - ftrace_graph_active--; - goto out; - } - ftrace_graph_return = gops->retfunc; +if (ftrace_graph_active ==1 ) { +register_pm_notifier (&ftrace_suspend_notifier); + ret =start_graph_tracing (); +if (ret) { + ftrace_graph_active--; + goto out; + } + + ftrace_graph_return = gops->retfunc; -/* -* Update the indirect function to the entryfunc, and the -* function that gets called to the entry_test first. Then -* call the update fgraph entry function to determine if -* the entryfunc should be called directly or not. -*/ - __ftrace_graph_entry = gops->entryfunc; - ftrace_graph_entry = ftrace_graph_entry_test; -update_function_graph_func (); +/* +* Update the indirect function to the entryfunc, and the +* function that gets called to the entry_test first. Then +* call the update fgraph entry function to determine if +* the entryfunc should be called directly or not. +*/ + __ftrace_graph_entry = gops->entryfunc; + ftrace_graph_entry = ftrace_graph_entry_test; +update_function_graph_func (); - ret =ftrace_startup (&graph_ops,FTRACE_START_FUNC_RET ); + ret =ftrace_startup (&graph_ops,FTRACE_START_FUNC_RET ); + } out:mutex_unlock (&ftrace_lock);return ret;
@@ -692,19 +723,36 @@ int register_ftrace_graph(struct fgraph_ops *gops)void unregister_ftrace_graph (struct fgraph_ops *gops) { +int i; +mutex_lock (&ftrace_lock);if (unlikely (!ftrace_graph_active)) goto out; - ftrace_graph_active--; - ftrace_graph_return = ftrace_stub_graph; - ftrace_graph_entry = ftrace_graph_entry_stub; - __ftrace_graph_entry = ftrace_graph_entry_stub; -ftrace_shutdown (&graph_ops,FTRACE_STOP_FUNC_RET ); -unregister_pm_notifier (&ftrace_suspend_notifier); -unregister_trace_sched_switch (ftrace_graph_probe_sched_switch,NULL ); +for (i =0 ; i < fgraph_array_cnt; i++) +if (gops == fgraph_array[i]) +break ; +if (i >= fgraph_array_cnt) + goto out; + fgraph_array[i] = &fgraph_stub; +if (i +1 == fgraph_array_cnt) { +for (; i >=0 ; i--) +if (fgraph_array[i] != &fgraph_stub) +break ; + fgraph_array_cnt = i +1 ; + } + + ftrace_graph_active--; +if (!ftrace_graph_active) { + ftrace_graph_return = ftrace_stub_graph; + ftrace_graph_entry = ftrace_graph_entry_stub; + __ftrace_graph_entry = ftrace_graph_entry_stub; +ftrace_shutdown (&graph_ops,FTRACE_STOP_FUNC_RET ); +unregister_pm_notifier (&ftrace_suspend_notifier); +unregister_trace_sched_switch (ftrace_graph_probe_sched_switch,NULL ); + } out:mutex_unlock (&ftrace_lock); }
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 4/20 function_graph: Allow multiple users to attach to function graph
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Allow for multiple users to attach to function graph tracer at the same time. Only 16 simultaneous users can attach to the tracer. This is because there's an array that stores the pointers to the attached fgraph_ops. When a function being traced is entered, each of the ftrace_ops entryfunc is called and if it returns non zero, its index into the array will be added to the shadow stack.
On exit of the function being traced, the shadow stack will contain the indexes of the ftrace_ops on the array that want their retfunc to be called.
Because a function may sleep for a long time (if a task sleeps itself), the return of the function may be literally days later. If the ftrace_ops is removed, its place on the array is replaced with a ftrace_ops that contains the stub functions and that will be called when the function finally returns.
If another ftrace_ops is added that happens to get the same index into the array, its return function may be called. But that's actually the way things current work with the old function graph tracer. If one tracer is removed and another is added, the new one will get the return calls of the function traced by the previous one, thus this is not a regression. This can be fixed by adding a counter to each time the array item is updated and save that on the shadow stack as well, such that it won't be called if the index saved does not match the index on the array.
Note, being able to filter functions when both are called is not completely handled yet, but that shouldn't be too hard to manage.
Co-developed with Masami Hiramatsu:
--- include/linux/ftrace.h | 3 +- kernel/trace/fgraph.c | 379 ++++++++++++++++++++++++++++++++--------- 2 files changed, 304 insertions(+), 78 deletions(-)
include/linux/ftrace.h +2 -1
@@ -1037,6 +1037,7 @@ extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);struct fgraph_ops {trace_func_graph_ent_t entryfunc;trace_func_graph_ret_t retfunc; +int idx; };/*
@@ -1071,7 +1072,7 @@ function_graph_enter(unsigned long ret, unsigned long func, unsigned long frame_pointer, unsigned long *retp);struct ftrace_ret_stack * -ftrace_graph_get_ret_stack (struct task_struct *task,int idx); +ftrace_graph_get_ret_stack (struct task_struct *task,int skip);unsigned long ftrace_graph_ret_addr (struct task_struct *task,int *idx,unsigned long ret,unsigned long *retp);
kernel/trace/fgraph.c +302 -77
578 lines not on this page · raw message
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 5/20 function_graph: Handle tail calls for stack unwinding
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
For the tail-call, there would be 2 or more ftrace_ret_stacks on the ret_stack, which records "return_to_handler" as the return address except for the last one. But on the real stack, there should be 1 entry because tail-call reuses the return address on the stack and jump to the next function.
In ftrace_graph_ret_addr() that is used for stack unwinding, skip tail calls as a real stack unwinder would do.
--- kernel/trace/fgraph.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
kernel/trace/fgraph.c +16 -3
@@ -594,16 +594,26 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int i = task->curr_ret_stack; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;while (i >0 ) { ret_stack =get_ret_stack (current, i, &i);if (!ret_stack)break ; -if (ret_stack->retp == retp) +/* +* For the tail-call, there would be 2 or more ftrace_ret_stacks on +* the ret_stack, which records "return_to_handler" as the return +* address except for the last one. +* But on the real stack, there should be 1 entry because tail-call +* reuses the return address on the stack and jump to the next function. +* Thus we will continue to find real return address. +*/ +if (ret_stack->retp == retp && + ret_stack->ret != return_handler)return ret_stack->ret; }
@@ -614,10 +624,11 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int offset = task->curr_ret_stack;int i; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;if (!idx)
@@ -626,6 +637,8 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, i = *idx;do { ret_stack =get_ret_stack (task, offset, &offset); +if (ret_stack && ret_stack->ret == return_handler) +continue ; i--; }while (i >=0 && ret_stack);
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 6/20 function_graph: Remove logic around ftrace_graph_entry and return
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The function pointers ftrace_graph_entry and ftrace_graph_return are no longer called via the function_graph tracer. Instead, an array structure is now used that will allow for multiple users of the function_graph infrastructure. The variables are still used by the architecture code for non dynamic ftrace configs, where a test is made against them to see if they point to the default stub function or not. This is how the static function tracing knows to call into the function graph tracer infrastructure or not.
Two new stub functions are made. entry_run() and return_run(). The ftrace_graph_entry and ftrace_graph_return are set to them respectively when the function graph tracer is enabled, and this will trigger the architecture specific function graph code to be executed.
This also requires checking the global_ops hash for all calls into the function_graph tracer.
Co-developed with Masami Hiramatsu:
--- kernel/trace/fgraph.c | 67 ++++++++-------------------------- kernel/trace/ftrace.c | 2 - kernel/trace/ftrace_internal.h | 2 - 3 files changed, 15 insertions(+), 56 deletions(-)
kernel/trace/fgraph.c +15 -52
@@ -163,6 +163,17 @@ set_bitmap(struct task_struct *t, int offset, unsigned long bitmap) (FGRAPH_TYPE_BITMAP <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; } +/* ftrace_graph_entry set to this to tell some archs to run function graph */ +static int entry_run (struct ftrace_graph_ent *trace) +{ +return 0 ; +} + +/* ftrace_graph_return set to this to tell some archs to run function graph */ +static void return_run (struct ftrace_graph_ret *trace) +{ +} +/* * @offset: The offset into @t->ret_stack to find the ret_stack entry * @frame_offset: Where to place the offset into @t->ret_stack of that entry
@@ -675,7 +686,6 @@ extern void ftrace_stub_graph(struct ftrace_graph_ret *);/* The callbacks that hook a function */ trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; -static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ static int alloc_retstack_tasklist (unsigned long **ret_stack_list)
@@ -758,46 +768,6 @@ ftrace_graph_probe_sched_switch(void *ignore, bool preempt, } } -static int ftrace_graph_entry_test (struct ftrace_graph_ent *trace) -{ -if (!ftrace_ops_test (&global_ops, trace->func,NULL )) -return 0 ; -return __ftrace_graph_entry (trace); -} - -/* -* The function graph tracer should only trace the functions defined -* by set_ftrace_filter and set_ftrace_notrace. If another function -* tracer ops is registered, the graph tracer requires testing the -* function against the global ops, and not just trace any function -* that any ftrace_ops registered. -*/ -void update_function_graph_func (void ) -{ -struct ftrace_ops *op; -bool do_test = false; - -/* -* The graph and global ops share the same set of functions -* to test. If any other ops is on the list, then -* the graph tracing needs to test if its the function -* it should call. -*/ -do_for_each_ftrace_op (op, ftrace_ops_list) { -if (op != &global_ops && op != &graph_ops && - op != &ftrace_list_end) { - do_test = true; -/* in double loop, break out with goto */ - goto out; - } - }while_for_each_ftrace_op (op); - out: -if (do_test) - ftrace_graph_entry = ftrace_graph_entry_test; -else - ftrace_graph_entry = __ftrace_graph_entry; -} - static DEFINE_PER_CPU(unsigned long *, idle_ret_stack); static void
@@ -939,18 +909,12 @@ int register_ftrace_graph(struct fgraph_ops *gops) ftrace_graph_active--; goto out; } - - ftrace_graph_return = gops->retfunc; -/* -* Update the indirect function to the entryfunc, and the -* function that gets called to the entry_test first. Then -* call the update fgraph entry function to determine if -* the entryfunc should be called directly or not. +* Some archs just test to see if these are not +* the default function */ - __ftrace_graph_entry = gops->entryfunc; - ftrace_graph_entry = ftrace_graph_entry_test; -update_function_graph_func (); + ftrace_graph_return = return_run; + ftrace_graph_entry = entry_run; ret =ftrace_startup (&graph_ops,FTRACE_START_FUNC_RET ); }
@@ -986,7 +950,6 @@ void unregister_ftrace_graph(struct fgraph_ops *gops)if (!ftrace_graph_active) { ftrace_graph_return = ftrace_stub_graph; ftrace_graph_entry = ftrace_graph_entry_stub; - __ftrace_graph_entry = ftrace_graph_entry_stub;ftrace_shutdown (&graph_ops,FTRACE_STOP_FUNC_RET );unregister_pm_notifier (&ftrace_suspend_notifier);unregister_trace_sched_switch (ftrace_graph_probe_sched_switch,NULL );
kernel/trace/ftrace.c +0 -2
@@ -235,8 +235,6 @@ static void update_ftrace_function(void) func = ftrace_ops_list_func; } -update_function_graph_func (); -/* If there's no change, then do nothing more here */ if (ftrace_trace_function == func)return ;
kernel/trace/ftrace_internal.h +0 -2
@@ -42,10 +42,8 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)#ifdef CONFIG_FUNCTION_GRAPH_TRACER extern int ftrace_graph_active; -void update_function_graph_func (void );#else /* !CONFIG_FUNCTION_GRAPH_TRACER */ # define ftrace_graph_active 0 -static inline void update_function_graph_func (void ) { }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ #else /* !CONFIG_FUNCTION_TRACER */
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 7/20 ftrace/function_graph: Pass fgraph_ops to function graph callbacks
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Pass the fgraph_ops structure to the function graph callbacks. This will allow callbacks to add a descriptor to a fgraph_ops private field that wil be added in the future and use it for the callbacks. This will be useful when more than one callback can be registered to the function graph tracer.
Co-developed with Masami Hiramatsu:
--- include/linux/ftrace.h | 10 +++++++--- kernel/trace/fgraph.c | 16 +++++++++------- kernel/trace/ftrace.c | 6 ++++-- kernel/trace/trace.h | 4 ++-- kernel/trace/trace_functions_graph.c | 11 +++++++---- kernel/trace/trace_irqsoff.c | 6 ++++-- kernel/trace/trace_sched_wakeup.c | 6 ++++-- kernel/trace/trace_selftest.c | 5 +++-- 8 files changed, 40 insertions(+), 24 deletions(-)
include/linux/ftrace.h +7 -3
@@ -1026,11 +1026,15 @@ struct ftrace_graph_ret {unsigned long long rettime; } __packed; +struct fgraph_ops ; +/* Type of the callback handlers for tracing function graph*/ -typedef void (*trace_func_graph_ret_t )(struct ftrace_graph_ret *);/* return */ -typedef int (*trace_func_graph_ent_t )(struct ftrace_graph_ent *);/* entry */ +typedef void (*trace_func_graph_ret_t )(struct ftrace_graph_ret *, +struct fgraph_ops *);/* return */ +typedef int (*trace_func_graph_ent_t )(struct ftrace_graph_ent *, +struct fgraph_ops *);/* entry */ -extern int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace); +extern int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace,struct fgraph_ops *gops);#ifdef CONFIG_FUNCTION_GRAPH_TRACER
kernel/trace/fgraph.c +9 -7
@@ -164,13 +164,13 @@ set_bitmap(struct task_struct *t, int offset, unsigned long bitmap) }/* ftrace_graph_entry set to this to tell some archs to run function graph */ -static int entry_run (struct ftrace_graph_ent *trace) +static int entry_run (struct ftrace_graph_ent *trace,struct fgraph_ops *ops) {return 0 ; }/* ftrace_graph_return set to this to tell some archs to run function graph */ -static void return_run (struct ftrace_graph_ret *trace) +static void return_run (struct ftrace_graph_ret *trace,struct fgraph_ops *ops) { }
@@ -234,12 +234,14 @@ int __weak ftrace_disable_ftrace_graph_caller(void) }#endif -int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace) +int ftrace_graph_entry_stub (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {return 0 ; } -static void ftrace_graph_ret_stub (struct ftrace_graph_ret *trace) +static void ftrace_graph_ret_stub (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) { }
@@ -379,7 +381,7 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (gops == &fgraph_stub)continue ; -if (gops->entryfunc (&trace)) +if (gops->entryfunc (&trace, gops)) bitmap |=BIT (i); }
@@ -527,7 +529,7 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regsif (gops == &fgraph_stub)continue ; - gops->retfunc (&trace); + gops->retfunc (&trace, gops); }/*
@@ -681,7 +683,7 @@ void ftrace_graph_sleep_time_control(bool enable)* Simply points to ftrace_stub, but with the proper protocol. * Defined by the linker script in linux/vmlinux.lds.h */ -extern void ftrace_stub_graph (struct ftrace_graph_ret *); +void ftrace_stub_graph (struct ftrace_graph_ret *trace,struct fgraph_ops *gops);/* The callbacks that hook a function */ trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph;
kernel/trace/ftrace.c +4 -2
@@ -815,7 +815,8 @@ void ftrace_graph_graph_time_control(bool enable) fgraph_graph_time = enable; } -static int profile_graph_entry (struct ftrace_graph_ent *trace) +static int profile_graph_entry (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {struct ftrace_ret_stack *ret_stack;
@@ -832,7 +833,8 @@ static int profile_graph_entry(struct ftrace_graph_ent *trace)return 1 ; } -static void profile_graph_return (struct ftrace_graph_ret *trace) +static void profile_graph_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) {struct ftrace_ret_stack *ret_stack;struct ftrace_profile_stat *stat;
kernel/trace/trace.h +2 -2
@@ -679,8 +679,8 @@ void trace_latency_header(struct seq_file *m);void trace_default_header (struct seq_file *m);void print_trace_header (struct seq_file *m,struct trace_iterator *iter); -void trace_graph_return (struct ftrace_graph_ret *trace); -int trace_graph_entry (struct ftrace_graph_ent *trace); +void trace_graph_return (struct ftrace_graph_ret *trace,struct fgraph_ops *gops); +int trace_graph_entry (struct ftrace_graph_ent *trace,struct fgraph_ops *gops);void set_graph_array (struct trace_array *tr);void tracing_start_cmdline_record (void );
kernel/trace/trace_functions_graph.c +7 -4
@@ -129,7 +129,8 @@ static inline int ftrace_graph_ignore_irqs(void)return in_hardirq (); } -int trace_graph_entry (struct ftrace_graph_ent *trace) +int trace_graph_entry (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {struct trace_array *tr = graph_array;struct trace_array_cpu *data;
@@ -238,7 +239,8 @@ void __trace_graph_return(struct trace_array *tr,trace_buffer_unlock_commit_nostack (buffer, event); } -void trace_graph_return (struct ftrace_graph_ret *trace) +void trace_graph_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) {struct trace_array *tr = graph_array;struct trace_array_cpu *data;
@@ -275,7 +277,8 @@ void set_graph_array(struct trace_array *tr)smp_mb (); } -static void trace_graph_thresh_return (struct ftrace_graph_ret *trace) +static void trace_graph_thresh_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) {ftrace_graph_addr_finish (trace);
@@ -288,7 +291,7 @@ static void trace_graph_thresh_return(struct ftrace_graph_ret *trace) (trace->rettime - trace->calltime < tracing_thresh)) return; else -trace_graph_return (trace ); +trace_graph_return (trace ,gops ); }static struct fgraph_ops funcgraph_thresh_ops = {
kernel/trace/trace_irqsoff.c +4 -2
@@ -175,7 +175,8 @@ static int irqsoff_display_graph(struct trace_array *tr, int set)return start_irqsoff_tracer (irqsoff_trace, set); } -static int irqsoff_graph_entry (struct ftrace_graph_ent *trace) +static int irqsoff_graph_entry (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {struct trace_array *tr = irqsoff_trace;struct trace_array_cpu *data;
@@ -205,7 +206,8 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)return ret; } -static void irqsoff_graph_return (struct ftrace_graph_ret *trace) +static void irqsoff_graph_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) {struct trace_array *tr = irqsoff_trace;struct trace_array_cpu *data;
kernel/trace/trace_sched_wakeup.c +4 -2
@@ -112,7 +112,8 @@ static int wakeup_display_graph(struct trace_array *tr, int set)return start_func_tracer (tr, set); } -static int wakeup_graph_entry (struct ftrace_graph_ent *trace) +static int wakeup_graph_entry (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {struct trace_array *tr = wakeup_trace;struct trace_array_cpu *data;
@@ -141,7 +142,8 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *trace)return ret; } -static void wakeup_graph_return (struct ftrace_graph_ret *trace) +static void wakeup_graph_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) {struct trace_array *tr = wakeup_trace;struct trace_array_cpu *data;
kernel/trace/trace_selftest.c +3 -2
@@ -762,7 +762,8 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)static unsigned int graph_hang_thresh;/* Wrap the real function entry probe to avoid possible hanging */ -static int trace_graph_entry_watchdog (struct ftrace_graph_ent *trace) +static int trace_graph_entry_watchdog (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) {/* This is harmlessly racy, we want to approximately detect a hang */ if (unlikely (++graph_hang_thresh >GRAPH_MAX_FUNC_TEST )) {
@@ -776,7 +777,7 @@ static int trace_graph_entry_watchdog(struct ftrace_graph_ent *trace)return 0 ; } -return trace_graph_entry (trace); +return trace_graph_entry (trace, gops); }static struct fgraph_ops fgraph_ops__initdata = {
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 8/20 ftrace: Allow function_graph tracer to be enabled in instances
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Now that function graph tracing can handle more than one user, allow it to be enabled in the ftrace instances. Note, the filtering of the functions is still joined by the top level set_ftrace_filter and friends, as well as the graph and nograph files.
Co-developed with Masami Hiramatsu:
--- include/linux/ftrace.h | 1 + kernel/trace/ftrace.c | 1 + kernel/trace/trace.h | 13 +++++- kernel/trace/trace_functions.c | 8 ++++ kernel/trace/trace_functions_graph.c | 65 +++++++++++++++++----------- kernel/trace/trace_selftest.c | 4 +- 6 files changed, 64 insertions(+), 28 deletions(-)
include/linux/ftrace.h +1 -0
@@ -1041,6 +1041,7 @@ extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace, struct fgraphstruct fgraph_ops {trace_func_graph_ent_t entryfunc;trace_func_graph_ret_t retfunc; +void *private;int idx; };
kernel/trace/ftrace.c +1 -0
@@ -7327,6 +7327,7 @@ __init void ftrace_init_global_array_ops(struct trace_array *tr) tr->ops = &global_ops; tr->ops->private = tr;ftrace_init_trace_array (tr); +init_array_fgraph_ops (tr); }void ftrace_init_array_ops (struct trace_array *tr,ftrace_func_t func)
kernel/trace/trace.h +12 -1
@@ -397,6 +397,9 @@ struct trace_array {struct ftrace_ops *ops;struct trace_pid_list __rcu *function_pids;struct trace_pid_list __rcu *function_no_pids; +#ifdef CONFIG_FUNCTION_GRAPH_TRACER +struct fgraph_ops *gops; +#endif #ifdef CONFIG_DYNAMIC_FTRACE /* All of these are protected by the ftrace_lock */ struct list_head func_probes;
@@ -681,7 +684,6 @@ void print_trace_header(struct seq_file *m, struct trace_iterator *iter);void trace_graph_return (struct ftrace_graph_ret *trace,struct fgraph_ops *gops);int trace_graph_entry (struct ftrace_graph_ent *trace,struct fgraph_ops *gops); -void set_graph_array (struct trace_array *tr);void tracing_start_cmdline_record (void );void tracing_stop_cmdline_record (void );
@@ -892,6 +894,9 @@ extern int __trace_graph_entry(struct trace_array *tr,extern void __trace_graph_return (struct trace_array *tr,struct ftrace_graph_ret *trace,unsigned int trace_ctx); +extern void init_array_fgraph_ops (struct trace_array *tr); +extern int allocate_fgraph_ops (struct trace_array *tr); +extern void free_fgraph_ops (struct trace_array *tr);#ifdef CONFIG_DYNAMIC_FTRACE extern struct ftrace_hash __rcu *ftrace_graph_hash;
@@ -1004,6 +1009,12 @@ print_graph_function_flags(struct trace_iterator *iter, u32 flags) {return TRACE_TYPE_UNHANDLED ; } +static inline void init_array_fgraph_ops (struct trace_array *tr) { } +static inline int allocate_fgraph_ops (struct trace_array *tr) +{ +return 0 ; +} +static inline void free_fgraph_ops (struct trace_array *tr) { }#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ extern struct list_head ftrace_pids;
kernel/trace/trace_functions.c +8 -0
@@ -80,6 +80,7 @@ void ftrace_free_ftrace_ops(struct trace_array *tr)int ftrace_create_function_files (struct trace_array *tr,struct dentry *parent) { +int ret;/* * The top level array uses the "global_ops", and the files are * created on boot up.
@@ -90,6 +91,12 @@ int ftrace_create_function_files(struct trace_array *tr,if (!tr->ops)return -EINVAL ; + ret =allocate_fgraph_ops (tr); +if (ret) { +kfree (tr->ops); +return ret; + } +ftrace_create_filter_files (tr->ops, parent);return 0 ;
@@ -99,6 +106,7 @@ void ftrace_destroy_function_files(struct trace_array *tr) {ftrace_destroy_filter_files (tr->ops);ftrace_free_ftrace_ops (tr); +free_fgraph_ops (tr); }static ftrace_func_t select_trace_function (u32 flags_val)
kernel/trace/trace_functions_graph.c +40 -25
118 lines not on this page · raw message
kernel/trace/trace_selftest.c +2 -2
@@ -813,7 +813,7 @@ trace_selftest_startup_function_graph(struct tracer *trace,* to detect and recover from possible hangs */ tracing_reset_online_cpus (&tr->array_buffer); -set_graph_array (tr); + fgraph_ops.private = tr; ret =register_ftrace_graph (&fgraph_ops);if (ret) {warn_failed_init_tracer (trace, ret);
@@ -856,7 +856,7 @@ trace_selftest_startup_function_graph(struct tracer *trace,cond_resched ();tracing_reset_online_cpus (&tr->array_buffer); -set_graph_array (tr); + fgraph_ops.private = tr;/* * Some archs *cough*PowerPC*cough* add characters to the
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 9/20 ftrace: Allow ftrace startup flags to exist without dynamic ftrace
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Some of the flags for ftrace_startup() may be exposed even when CONFIG_DYNAMIC_FTRACE is not configured in. This is fine as the difference between dynamic ftrace and static ftrace is done within the internals of ftrace itself. No need to have use cases fail to compile because dynamic ftrace is disabled.
This change is needed to move some of the logic of what is passed to ftrace_startup() out of the parameters of ftrace_startup().
--- include/linux/ftrace.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
include/linux/ftrace.h +9 -9
@@ -509,6 +509,15 @@ static inline void stack_tracer_disable(void) { }static inline void stack_tracer_enable (void ) { }#endif +enum { +FTRACE_UPDATE_CALLS = (1 <<0 ), +FTRACE_DISABLE_CALLS = (1 <<1 ), +FTRACE_UPDATE_TRACE_FUNC = (1 <<2 ), +FTRACE_START_FUNC_RET = (1 <<3 ), +FTRACE_STOP_FUNC_RET = (1 <<4 ), +FTRACE_MAY_SLEEP = (1 <<5 ), +}; +#ifdef CONFIG_DYNAMIC_FTRACE void ftrace_arch_code_modify_prepare (void );
@@ -603,15 +612,6 @@ void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);void ftrace_free_filter (struct ftrace_ops *ops);void ftrace_ops_set_global_filter (struct ftrace_ops *ops); -enum { -FTRACE_UPDATE_CALLS = (1 <<0 ), -FTRACE_DISABLE_CALLS = (1 <<1 ), -FTRACE_UPDATE_TRACE_FUNC = (1 <<2 ), -FTRACE_START_FUNC_RET = (1 <<3 ), -FTRACE_STOP_FUNC_RET = (1 <<4 ), -FTRACE_MAY_SLEEP = (1 <<5 ), -}; -/* * The FTRACE_UPDATE_* enum is used to pass information back * from the ftrace_update_record() and ftrace_test_record()
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 10/20 function_graph: Have the instances use their own ftrace_ops for filtering
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Allow for instances to have their own ftrace_ops part of the fgraph_ops that makes the funtion_graph tracer filter on the set_ftrace_filter file of the instance and not the top instance.
Note that this also requires to update ftrace_graph_func() to call new function_graph_enter_ops() instead of function_graph_enter() so that it avoid pushing on shadow stack multiple times on the same function.
Co-developed with Masami Hiramatsu:
--- arch/arm64/kernel/ftrace.c | 21 ++++- arch/loongarch/kernel/ftrace_dyn.c | 15 ++- arch/powerpc/kernel/trace/ftrace.c | 3 +- arch/riscv/kernel/ftrace.c | 15 ++- arch/x86/kernel/ftrace.c | 19 +++- include/linux/ftrace.h | 6 ++ kernel/trace/fgraph.c | 131 ++++++++++++++++++++------- kernel/trace/ftrace.c | 4 +- kernel/trace/trace.h | 16 ++-- kernel/trace/trace_functions.c | 2 +- kernel/trace/trace_functions_graph.c | 8 +- 11 files changed, 190 insertions(+), 50 deletions(-)
arch/arm64/kernel/ftrace.c +20 -1
@@ -481,7 +481,26 @@ void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,void ftrace_graph_func (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs) { -prepare_ftrace_return (ip, &fregs->lr, fregs->fp); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +unsigned long frame_pointer = fregs->fp; +unsigned long *parent = &fregs->lr; +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ; + + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip, frame_pointer, + (void *)frame_pointer, gops)) + *parent = (unsigned long )&return_to_handler; + +ftrace_test_recursion_unlock (bit); }#else /*
arch/loongarch/kernel/ftrace_dyn.c +13 -2
@@ -241,10 +241,21 @@ void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent)void ftrace_graph_func (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs) { + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +unsigned long return_hooker = (unsigned long )&return_to_handler;struct pt_regs *regs = &fregs->regs; -unsigned long *parent = (unsigned long *)®s->regs[1 ]; +unsigned long *parent; +unsigned long old; + + parent = (unsigned long *)®s->regs[1 ]; -prepare_ftrace_return (ip, (unsigned long *)parent); +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ; + + old = *parent; + +if (!function_graph_enter_ops (old, ip,0 , parent, gops)) + *parent = return_hooker; }#else static int ftrace_modify_graph_caller (bool enable)
arch/powerpc/kernel/trace/ftrace.c +2 -1
@@ -421,6 +421,7 @@ int __init ftrace_dyn_arch_init(void)void ftrace_graph_func (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs) { + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops);unsigned long sp = fregs->regs.gpr[1 ];int bit;
@@ -434,7 +435,7 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,if (bit <0 ) goto out; -if (!function_graph_enter (parent_ip, ip,0 , (unsigned long *)sp)) +if (!function_graph_enter_ops (parent_ip, ip,0 , (unsigned long *)sp, gops)) parent_ip =ppc_function_entry (return_to_handler);ftrace_test_recursion_unlock (bit);
arch/riscv/kernel/ftrace.c +14 -1
@@ -218,10 +218,23 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,void ftrace_graph_func (unsigned long ip,unsigned long parent_ip,struct ftrace_ops *op,struct ftrace_regs *fregs) { + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +unsigned long return_hooker = (unsigned long )&return_to_handler;struct pt_regs *regs =arch_ftrace_get_regs (fregs);unsigned long *parent = (unsigned long *)®s->ra; +unsigned long old; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ; + +/* +* We don't suffer access faults, so no extra fault-recovery assembly +* is needed here. +*/ + old = *parent; -prepare_ftrace_return (parent, ip,frame_pointer (regs)); +if (!function_graph_enter_ops (old, ip,frame_pointer (regs), parent, gops)) + *parent = return_hooker; }#else /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */ extern void ftrace_graph_call (void );
arch/x86/kernel/ftrace.c +17 -2
@@ -648,9 +648,24 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { struct pt_regs *regs = &fregs->regs; -unsigned long *stack = (unsigned long *)kernel_stack_pointer (regs); +unsigned long *parent = (unsigned long *)kernel_stack_pointer (regs); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ; -prepare_ftrace_return (ip, (unsigned long *)stack,0 ); + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip,0 , parent, gops)) + *parent = (unsigned long )&return_to_handler; + +ftrace_test_recursion_unlock (bit); }#endif
include/linux/ftrace.h +6 -0
@@ -1041,6 +1041,7 @@ extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace, struct fgraphstruct fgraph_ops {trace_func_graph_ent_t entryfunc;trace_func_graph_ret_t retfunc; +struct ftrace_ops ops;/* for the hash lists */ void *private;int idx; };
@@ -1076,6 +1077,11 @@ extern int function_graph_enter(unsigned long ret, unsigned long func, unsigned long frame_pointer, unsigned long *retp); +extern int +function_graph_enter_ops (unsigned long ret,unsigned long func, +unsigned long frame_pointer,unsigned long *retp, +struct fgraph_ops *gops); +struct ftrace_ret_stack *ftrace_graph_get_ret_stack (struct task_struct *task,int skip);
kernel/trace/fgraph.c +99 -32
215 lines not on this page · raw message
kernel/trace/ftrace.c +3 -1
@@ -3016,6 +3016,8 @@ int ftrace_startup(struct ftrace_ops *ops, int command)if (unlikely (ftrace_disabled))return -ENODEV ; +ftrace_ops_init (ops); + ret =__register_ftrace_function (ops);if (ret)return ret;
@@ -7327,7 +7329,7 @@ __init void ftrace_init_global_array_ops(struct trace_array *tr) tr->ops = &global_ops; tr->ops->private = tr;ftrace_init_trace_array (tr); -init_array_fgraph_ops (tr); +init_array_fgraph_ops (tr, tr->ops); }void ftrace_init_array_ops (struct trace_array *tr,ftrace_func_t func)
kernel/trace/trace.h +9 -7
@@ -894,8 +894,8 @@ extern int __trace_graph_entry(struct trace_array *tr,extern void __trace_graph_return (struct trace_array *tr,struct ftrace_graph_ret *trace,unsigned int trace_ctx); -extern void init_array_fgraph_ops (struct trace_array *tr); -extern int allocate_fgraph_ops (struct trace_array *tr); +extern void init_array_fgraph_ops (struct trace_array *tr,struct ftrace_ops *ops); +extern int allocate_fgraph_ops (struct trace_array *tr,struct ftrace_ops *ops);extern void free_fgraph_ops (struct trace_array *tr);#ifdef CONFIG_DYNAMIC_FTRACE
@@ -978,6 +978,7 @@ static inline int ftrace_graph_notrace_addr(unsigned long addr)preempt_enable_notrace (); return ret; } +#else static inline int ftrace_graph_addr (struct ftrace_graph_ent *trace) {
@@ -1003,18 +1004,19 @@ static inline bool ftrace_graph_ignore_func(struct ftrace_graph_ent *trace) (fgraph_max_depth && trace->depth >= fgraph_max_depth); } +void fgraph_init_ops (struct ftrace_ops *dst_ops, +struct ftrace_ops *src_ops); +#else /* CONFIG_FUNCTION_GRAPH_TRACER */ static inline enum print_line_t print_graph_function_flags (struct trace_iterator *iter,u32 flags) {return TRACE_TYPE_UNHANDLED ; } -static inline void init_array_fgraph_ops (struct trace_array *tr) { } -static inline int allocate_fgraph_ops (struct trace_array *tr) -{ -return 0 ; -}static inline void free_fgraph_ops (struct trace_array *tr) { } +/* ftrace_ops may not be defined */ +#define init_array_fgraph_ops (tr, ops) do { } while (0) +#define allocate_fgraph_ops (tr, ops) ({ 0; })#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ extern struct list_head ftrace_pids;
kernel/trace/trace_functions.c +1 -1
@@ -91,7 +91,7 @@ int ftrace_create_function_files(struct trace_array *tr,if (!tr->ops)return -EINVAL ; - ret =allocate_fgraph_ops (tr); + ret =allocate_fgraph_ops (tr, tr->ops);if (ret) {kfree (tr->ops);return ret;
kernel/trace/trace_functions_graph.c +6 -2
@@ -288,7 +288,7 @@ static struct fgraph_ops funcgraph_ops = { .retfunc = &trace_graph_return, }; -int allocate_fgraph_ops (struct trace_array *tr) +int allocate_fgraph_ops (struct trace_array *tr,struct ftrace_ops *ops) {struct fgraph_ops *gops;
@@ -301,6 +301,9 @@ int allocate_fgraph_ops(struct trace_array *tr) tr->gops = gops; gops->private = tr; + +fgraph_init_ops (&gops->ops, ops); +return 0 ; }
@@ -309,10 +312,11 @@ void free_fgraph_ops(struct trace_array *tr)kfree (tr->gops); } -__init void init_array_fgraph_ops (struct trace_array *tr) +__init void init_array_fgraph_ops (struct trace_array *tr,struct ftrace_ops *ops) { tr->gops = &funcgraph_ops; funcgraph_ops.private = tr; +fgraph_init_ops (&tr->gops->ops, ops); }static int graph_trace_init (struct trace_array *tr)
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 11/20 function_graph: Use a simple LRU for fgraph_array index number
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Since the fgraph_array index is used for the bitmap on the shadow stack, it may leave some entries after a function_graph instance is removed. Thus if another instance reuses the fgraph_array index soon after releasing it, the fgraph may confuse to call the newer callback for the entries which are pushed by the older instance.
To avoid reusing the fgraph_array index soon after releasing, introduce a simple LRU table for managing the index number. This will reduce the possibility of this confusion.
--- kernel/trace/fgraph.c | 71 ++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 21 deletions(-)
kernel/trace/fgraph.c +50 -21
@@ -125,10 +125,48 @@ enum {DEFINE_STATIC_KEY_FALSE (kill_ftrace_graph);int ftrace_graph_active; -static int fgraph_array_cnt; -static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +/* LRU index table for fgraph_array */ +static int fgraph_lru_table[FGRAPH_ARRAY_SIZE ]; +static int fgraph_lru_next; +static int fgraph_lru_last; + +/* Initialize fgraph_lru_table with unused index */ +static void fgraph_lru_init (void ) +{ +int i; + +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) + fgraph_lru_table[i] = i; +} + +/* Release the used index to the LRU table */ +static int fgraph_lru_release_index (int idx) +{ +if (idx <0 || idx >=FGRAPH_ARRAY_SIZE || +WARN_ON_ONCE (fgraph_lru_table[fgraph_lru_last] !=-1 )) +return -1 ; + + fgraph_lru_table[fgraph_lru_last] = idx; + fgraph_lru_last = (fgraph_lru_last +1 ) %FGRAPH_ARRAY_SIZE ; +return 0 ; +} + +/* Allocate a new index from LRU table */ +static int fgraph_lru_alloc_index (void ) +{ +int idx = fgraph_lru_table[fgraph_lru_next]; + +/* No id is available */ +if (idx ==-1 ) +return -1 ; + + fgraph_lru_table[fgraph_lru_next] =-1 ; + fgraph_lru_next = (fgraph_lru_next +1 ) %FGRAPH_ARRAY_SIZE ; +return idx; +} +/* Get the FRAME_OFFSET from the word from the @offset on ret_stack */ static inline int get_frame_offset (struct task_struct *t,int offset) {
@@ -375,7 +413,7 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (offset <0 ) goto out; -for (i =0 ; i < fgraph_array_cnt; i++) { +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) {struct fgraph_ops *gops = fgraph_array[i];if (gops == &fgraph_stub)
@@ -927,7 +965,7 @@ int register_ftrace_graph(struct fgraph_ops *gops) {int command =0 ;int ret =0 ; -int i; +int i =-1 ;mutex_lock (&ftrace_lock);
@@ -943,21 +981,16 @@ int register_ftrace_graph(struct fgraph_ops *gops)/* The array must always have real data on it */ for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) fgraph_array[i] = &fgraph_stub; +fgraph_lru_init (); } -/* Look for an available spot */ -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { -if (fgraph_array[i] == &fgraph_stub) -break ; - } -if (i >=FGRAPH_ARRAY_SIZE ) { + i =fgraph_lru_alloc_index (); +if (i <0 ||WARN_ON_ONCE (fgraph_array[i] != &fgraph_stub)) { ret = -ENOSPC ; goto out; } fgraph_array[i] = gops; -if (i +1 > fgraph_array_cnt) - fgraph_array_cnt = i +1 ; gops->idx = i; ftrace_graph_active++;
@@ -981,6 +1014,7 @@ int register_ftrace_graph(struct fgraph_ops *gops)if (ret) { fgraph_array[i] = &fgraph_stub; ftrace_graph_active--; +fgraph_lru_release_index (i); } out:mutex_unlock (&ftrace_lock);
@@ -990,25 +1024,20 @@ int register_ftrace_graph(struct fgraph_ops *gops)void unregister_ftrace_graph (struct fgraph_ops *gops) {int command =0 ; -int i;mutex_lock (&ftrace_lock);if (unlikely (!ftrace_graph_active)) goto out; -if (unlikely (gops->idx <0 || gops->idx >= fgraph_array_cnt)) +if (unlikely (gops->idx <0 || gops->idx >=FGRAPH_ARRAY_SIZE || + fgraph_array[gops->idx] != gops)) goto out; -WARN_ON_ONCE (fgraph_array[gops->idx] != gops); +if (fgraph_lru_release_index (gops->idx) <0 ) + goto out; fgraph_array[gops->idx] = &fgraph_stub; -if (gops->idx +1 == fgraph_array_cnt) { - i = gops->idx; -while (i >=0 && fgraph_array[i] == &fgraph_stub) - i--; - fgraph_array_cnt = i +1 ; - } ftrace_graph_active--;
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 12/20 function_graph: Add "task variables" per task for fgraph_ops
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add a "task variables" array on the tasks shadow ret_stack that is the size of longs for each possible registered fgraph_ops. That's a total of 16, taking up 8 * 16 = 128 bytes (out of a page size 4k).
This will allow for fgraph_ops to do specific features on a per task basis having a way to maintain state for each task.
Co-developed with Masami Hiramatsu:
--- include/linux/ftrace.h | 1 + kernel/trace/fgraph.c | 74 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-)
include/linux/ftrace.h +1 -0
@@ -1087,6 +1087,7 @@ ftrace_graph_get_ret_stack(struct task_struct *task, int skip);unsigned long ftrace_graph_ret_addr (struct task_struct *task,int *idx,unsigned long ret,unsigned long *retp); +unsigned long *fgraph_get_task_var (struct fgraph_ops *gops);/* * Sometimes we don't want to trace a function with the function
kernel/trace/fgraph.c +73 -1
120 lines not on this page · raw message
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 13/20 function_graph: Move set_graph_function tests to shadow stack global var
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The use of the task->trace_recursion for the logic used for the set_graph_funnction was a bit of an abuse of that variable. Now that there exists global vars that are per stack for registered graph traces, use that instead.
--- include/linux/trace_recursion.h | 5 +---- kernel/trace/trace.h | 32 ++++++++++++++++++---------- kernel/trace/trace_functions_graph.c | 6 +++--- kernel/trace/trace_irqsoff.c | 4 ++-- kernel/trace/trace_sched_wakeup.c | 4 ++-- 5 files changed, 29 insertions(+), 22 deletions(-)
include/linux/trace_recursion.h +1 -4
@@ -44,9 +44,6 @@ enum {*/ TRACE_IRQ_BIT, -/* Set if the function is in the set_graph_function file */ - TRACE_GRAPH_BIT, -/* * In the very unlikely case that an interrupt came in * at a start of graph tracing, and we want to trace
@@ -60,7 +57,7 @@ enum {* that preempted a softirq start of a function that * preempted normal context!!!! Luckily, it can't be * greater than 3, so the next two bits are a mask -* of what the depth is when we set TRACE_GRAPH_BIT +* of what the depth is when we set TRACE_GRAPH_FL */ TRACE_GRAPH_DEPTH_START_BIT,
kernel/trace/trace.h +21 -11
@@ -898,11 +898,16 @@ extern void init_array_fgraph_ops(struct trace_array *tr, struct ftrace_ops *opsextern int allocate_fgraph_ops (struct trace_array *tr,struct ftrace_ops *ops);extern void free_fgraph_ops (struct trace_array *tr); +enum { +TRACE_GRAPH_FL =1 , +}; +#ifdef CONFIG_DYNAMIC_FTRACE extern struct ftrace_hash __rcu *ftrace_graph_hash;extern struct ftrace_hash __rcu *ftrace_graph_notrace_hash; -static inline int ftrace_graph_addr (struct ftrace_graph_ent *trace) +static inline int +ftrace_graph_addr (unsigned long *task_var,struct ftrace_graph_ent *trace) {unsigned long addr = trace->func;int ret =0 ;
@@ -924,12 +929,11 @@ static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace) }if (ftrace_lookup_ip (hash, addr)) { -/* * This needs to be cleared on the return functions * when the depth is zero. */ -trace_recursion_set (TRACE_GRAPH_BIT ); + *task_var |=TRACE_GRAPH_FL ;trace_recursion_set_depth (trace->depth);/*
@@ -949,11 +953,14 @@ static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)return ret; } -static inline void ftrace_graph_addr_finish (struct ftrace_graph_ret *trace) +static inline void +ftrace_graph_addr_finish (struct fgraph_ops *gops,struct ftrace_graph_ret *trace) { -if (trace_recursion_test (TRACE_GRAPH_BIT ) && +unsigned long *task_var =fgraph_get_task_var (gops); + +if ((*task_var &TRACE_GRAPH_FL ) && trace->depth ==trace_recursion_depth ()) -trace_recursion_clear (TRACE_GRAPH_BIT ); + *task_var &= ~TRACE_GRAPH_FL ; }static inline int ftrace_graph_notrace_addr (unsigned long addr)
@@ -980,7 +987,7 @@ static inline int ftrace_graph_notrace_addr(unsigned long addr) }#else -static inline int ftrace_graph_addr (struct ftrace_graph_ent *trace) +static inline int ftrace_graph_addr (unsigned long *task_var,struct ftrace_graph_ent *trace) {return 1 ; }
@@ -989,17 +996,20 @@ static inline int ftrace_graph_notrace_addr(unsigned long addr) {return 0 ; } -static inline void ftrace_graph_addr_finish (struct ftrace_graph_ret *trace) +static inline void ftrace_graph_addr_finish (struct fgraph_ops *gops,struct ftrace_graph_ret *trace) { }#endif /* CONFIG_DYNAMIC_FTRACE */ extern unsigned int fgraph_max_depth; -static inline bool ftrace_graph_ignore_func (struct ftrace_graph_ent *trace) +static inline bool +ftrace_graph_ignore_func (struct fgraph_ops *gops,struct ftrace_graph_ent *trace) { +unsigned long *task_var =fgraph_get_task_var (gops); +/* trace it when it is-nested-in or is a function enabled. */ -return !(trace_recursion_test (TRACE_GRAPH_BIT ) || -ftrace_graph_addr (trace)) || +return !((*task_var &TRACE_GRAPH_FL ) || +ftrace_graph_addr (task_var, trace)) || (trace->depth <0 ) || (fgraph_max_depth && trace->depth >= fgraph_max_depth); }
kernel/trace/trace_functions_graph.c +3 -3
@@ -160,7 +160,7 @@ int trace_graph_entry(struct ftrace_graph_ent *trace,if (!ftrace_trace_task (tr))return 0 ; -if (ftrace_graph_ignore_func (trace)) +if (ftrace_graph_ignore_func (gops, trace))return 0 ;if (ftrace_graph_ignore_irqs ())
@@ -247,7 +247,7 @@ void trace_graph_return(struct ftrace_graph_ret *trace,long disabled;int cpu; -ftrace_graph_addr_finish (trace); +ftrace_graph_addr_finish (gops, trace);if (trace_recursion_test (TRACE_GRAPH_NOTRACE_BIT )) {trace_recursion_clear (TRACE_GRAPH_NOTRACE_BIT );
@@ -269,7 +269,7 @@ void trace_graph_return(struct ftrace_graph_ret *trace,static void trace_graph_thresh_return (struct ftrace_graph_ret *trace,struct fgraph_ops *gops) { -ftrace_graph_addr_finish (trace); +ftrace_graph_addr_finish (gops, trace);if (trace_recursion_test (TRACE_GRAPH_NOTRACE_BIT )) {trace_recursion_clear (TRACE_GRAPH_NOTRACE_BIT );
kernel/trace/trace_irqsoff.c +2 -2
@@ -184,7 +184,7 @@ static int irqsoff_graph_entry(struct ftrace_graph_ent *trace,unsigned int trace_ctx;int ret; -if (ftrace_graph_ignore_func (trace)) +if (ftrace_graph_ignore_func (gops, trace))return 0 ;/* * Do not trace a function if it's filtered by set_graph_notrace.
@@ -214,7 +214,7 @@ static void irqsoff_graph_return(struct ftrace_graph_ret *trace,unsigned long flags;unsigned int trace_ctx; -ftrace_graph_addr_finish (trace); +ftrace_graph_addr_finish (gops, trace);if (!func_prolog_dec (tr, &data, &flags))return ;
kernel/trace/trace_sched_wakeup.c +2 -2
@@ -120,7 +120,7 @@ static int wakeup_graph_entry(struct ftrace_graph_ent *trace,unsigned int trace_ctx;int ret =0 ; -if (ftrace_graph_ignore_func (trace)) +if (ftrace_graph_ignore_func (gops, trace))return 0 ;/* * Do not trace a function if it's filtered by set_graph_notrace.
@@ -149,7 +149,7 @@ static void wakeup_graph_return(struct ftrace_graph_ret *trace,struct trace_array_cpu *data;unsigned int trace_ctx; -ftrace_graph_addr_finish (trace); +ftrace_graph_addr_finish (gops, trace);if (!func_prolog_preempt_disable (tr, &data, &trace_ctx))return ;
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 14/20 function_graph: Move graph depth stored data to shadow stack global var
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The use of the task->trace_recursion for the logic used for the function graph depth was a bit of an abuse of that variable. Now that there exists global vars that are per stack for registered graph traces, use that instead.
--- include/linux/trace_recursion.h | 29 ---------------------------- kernel/trace/trace.h | 34 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 31 deletions(-)
include/linux/trace_recursion.h +0 -29
@@ -44,25 +44,6 @@ enum {*/ TRACE_IRQ_BIT, -/* -* In the very unlikely case that an interrupt came in -* at a start of graph tracing, and we want to trace -* the function in that interrupt, the depth can be greater -* than zero, because of the preempted start of a previous -* trace. In an even more unlikely case, depth could be 2 -* if a softirq interrupted the start of graph tracing, -* followed by an interrupt preempting a start of graph -* tracing in the softirq, and depth can even be 3 -* if an NMI came in at the start of an interrupt function -* that preempted a softirq start of a function that -* preempted normal context!!!! Luckily, it can't be -* greater than 3, so the next two bits are a mask -* of what the depth is when we set TRACE_GRAPH_FL -*/ - -TRACE_GRAPH_DEPTH_START_BIT , -TRACE_GRAPH_DEPTH_END_BIT , -/* * To implement set_graph_notrace, if this bit is set, we ignore * function graph tracing of called functions, until the return
@@ -78,16 +59,6 @@ enum {#define trace_recursion_clear (bit) do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)#define trace_recursion_test (bit) ((current)->trace_recursion & (1<<(bit))) -#define trace_recursion_depth () \ - (((current)->trace_recursion >> TRACE_GRAPH_DEPTH_START_BIT) & 3) -#define trace_recursion_set_depth (depth) \ - do { \ - current->trace_recursion &= \ - ~(3 << TRACE_GRAPH_DEPTH_START_BIT); \ - current->trace_recursion |= \ - ((depth) & 3) << TRACE_GRAPH_DEPTH_START_BIT; \ - } while (0) -#define TRACE_CONTEXT_BITS 4#define TRACE_FTRACE_START TRACE_FTRACE_BIT
kernel/trace/trace.h +32 -2
@@ -900,8 +900,38 @@ extern void free_fgraph_ops(struct trace_array *tr);enum {TRACE_GRAPH_FL =1 , + +/* +* In the very unlikely case that an interrupt came in +* at a start of graph tracing, and we want to trace +* the function in that interrupt, the depth can be greater +* than zero, because of the preempted start of a previous +* trace. In an even more unlikely case, depth could be 2 +* if a softirq interrupted the start of graph tracing, +* followed by an interrupt preempting a start of graph +* tracing in the softirq, and depth can even be 3 +* if an NMI came in at the start of an interrupt function +* that preempted a softirq start of a function that +* preempted normal context!!!! Luckily, it can't be +* greater than 3, so the next two bits are a mask +* of what the depth is when we set TRACE_GRAPH_FL +*/ + +TRACE_GRAPH_DEPTH_START_BIT , +TRACE_GRAPH_DEPTH_END_BIT , }; +static inline unsigned long ftrace_graph_depth (unsigned long *task_var) +{ +return (*task_var >>TRACE_GRAPH_DEPTH_START_BIT ) &3 ; +} + +static inline void ftrace_graph_set_depth (unsigned long *task_var,int depth) +{ + *task_var &= ~(3 <<TRACE_GRAPH_DEPTH_START_BIT ); + *task_var |= (depth &3 ) <<TRACE_GRAPH_DEPTH_START_BIT ; +} +#ifdef CONFIG_DYNAMIC_FTRACE extern struct ftrace_hash __rcu *ftrace_graph_hash;extern struct ftrace_hash __rcu *ftrace_graph_notrace_hash;
@@ -934,7 +964,7 @@ ftrace_graph_addr(unsigned long *task_var, struct ftrace_graph_ent *trace)* when the depth is zero. */ *task_var |=TRACE_GRAPH_FL ; -trace_recursion_set_depth (trace->depth); +ftrace_graph_set_depth (task_var, trace->depth);/* * If no irqs are to be traced, but a set_graph_function
@@ -959,7 +989,7 @@ ftrace_graph_addr_finish(struct fgraph_ops *gops, struct ftrace_graph_ret *traceunsigned long *task_var =fgraph_get_task_var (gops);if ((*task_var &TRACE_GRAPH_FL ) && - trace->depth ==trace_recursion_depth ()) + trace->depth ==ftrace_graph_depth (task_var)) *task_var &= ~TRACE_GRAPH_FL ; }
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 15/20 function_graph: Move graph notrace bit to shadow stack global var
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
The use of the task->trace_recursion for the logic used for the function graph no-trace was a bit of an abuse of that variable. Now that there exists global vars that are per stack for registered graph traces, use that instead.
--- include/linux/trace_recursion.h | 7 ------- kernel/trace/trace.h | 9 +++++++++ kernel/trace/trace_functions_graph.c | 10 ++++++---- 3 files changed, 15 insertions(+), 11 deletions(-)
include/linux/trace_recursion.h +0 -7
@@ -44,13 +44,6 @@ enum {*/ TRACE_IRQ_BIT , -/* -* To implement set_graph_notrace, if this bit is set, we ignore -* function graph tracing of called functions, until the return -* function is called to clear it. -*/ -TRACE_GRAPH_NOTRACE_BIT , -/* Used to prevent recursion recording from recursing. */ TRACE_RECORD_RECURSION_BIT, };
kernel/trace/trace.h +9 -0
@@ -919,8 +919,17 @@ enum {TRACE_GRAPH_DEPTH_START_BIT ,TRACE_GRAPH_DEPTH_END_BIT , + +/* +* To implement set_graph_notrace, if this bit is set, we ignore +* function graph tracing of called functions, until the return +* function is called to clear it. +*/ + TRACE_GRAPH_NOTRACE_BIT, }; +#define TRACE_GRAPH_NOTRACE (1 << TRACE_GRAPH_NOTRACE_BIT) +static inline unsigned long ftrace_graph_depth (unsigned long *task_var) {return (*task_var >>TRACE_GRAPH_DEPTH_START_BIT ) &3 ;
kernel/trace/trace_functions_graph.c +6 -4
@@ -130,6 +130,7 @@ static inline int ftrace_graph_ignore_irqs(void)int trace_graph_entry (struct ftrace_graph_ent *trace,struct fgraph_ops *gops) { +unsigned long *task_var =fgraph_get_task_var (gops);struct trace_array *tr = gops->private;struct trace_array_cpu *data;unsigned long flags;
@@ -138,7 +139,7 @@ int trace_graph_entry(struct ftrace_graph_ent *trace,int ret;int cpu; -if (trace_recursion_test (TRACE_GRAPH_NOTRACE_BIT )) +if (*task_var &TRACE_GRAPH_NOTRACE )return 0 ;/*
@@ -149,7 +150,7 @@ int trace_graph_entry(struct ftrace_graph_ent *trace,* returning from the function. */ if (ftrace_graph_notrace_addr (trace->func)) { -trace_recursion_set (TRACE_GRAPH_NOTRACE_BIT ); + *task_var |=TRACE_GRAPH_NOTRACE_BIT ;/* * Need to return 1 to have the return called * that will clear the NOTRACE bit.
@@ -240,6 +241,7 @@ void __trace_graph_return(struct trace_array *tr,void trace_graph_return (struct ftrace_graph_ret *trace,struct fgraph_ops *gops) { +unsigned long *task_var =fgraph_get_task_var (gops);struct trace_array *tr = gops->private;struct trace_array_cpu *data;unsigned long flags;
@@ -249,8 +251,8 @@ void trace_graph_return(struct ftrace_graph_ret *trace,ftrace_graph_addr_finish (gops, trace); -if (trace_recursion_test (TRACE_GRAPH_NOTRACE_BIT )) { -trace_recursion_clear (TRACE_GRAPH_NOTRACE_BIT ); +if (*task_var &TRACE_GRAPH_NOTRACE ) { + *task_var &= ~TRACE_GRAPH_NOTRACE ;return ; }
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 16/20 function_graph: Implement fgraph_reserve_data() and fgraph_retrieve_data()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Added functions that can be called by a fgraph_ops entryfunc and retfunc to store state between the entry of the function being traced to the exit of the same function. The fgraph_ops entryfunc() may call fgraph_reserve_data() to store up to 32 words onto the task's shadow ret_stack and this then can be retrieved by fgraph_retrieve_data() called by the corresponding retfunc().
Co-developed with Masami Hiramatsu:
--- include/linux/ftrace.h | 3 + kernel/trace/fgraph.c | 196 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 190 insertions(+), 9 deletions(-)
include/linux/ftrace.h +3 -0
@@ -1046,6 +1046,9 @@ struct fgraph_ops {int idx; }; +void *fgraph_reserve_data (int idx,int size_bytes); +void *fgraph_retrieve_data (int idx,int *size_bytes); +/* * Stack of return addresses for functions * of a thread.
kernel/trace/fgraph.c +187 -9
299 lines not on this page · raw message
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 17/20 function_graph: Add selftest for passing local variables
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add boot up selftest that passes variables from a function entry to a function exit, and make sure that they do get passed around.
Co-developed with Masami Hiramatsu:
--- kernel/trace/trace_selftest.c | 169 ++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+)
kernel/trace/trace_selftest.c +169 -0
@@ -756,6 +756,173 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)#ifdef CONFIG_FUNCTION_GRAPH_TRACER +#ifdef CONFIG_DYNAMIC_FTRACE + +#define BYTE_NUMBER 123 +#define SHORT_NUMBER 12345 +#define WORD_NUMBER 1234567890 +#define LONG_NUMBER 1234567890123456789LL + +static int fgraph_store_size__initdata ; +static const char *fgraph_store_type_name__initdata ; +static char *fgraph_error_str__initdata ; +static char fgraph_error_str_buf[128 ]__initdata ; + +static __init int store_entry (struct ftrace_graph_ent *trace, +struct fgraph_ops *gops) +{ +const char *type = fgraph_store_type_name; +int size = fgraph_store_size; +void *p; + + p =fgraph_reserve_data (gops->idx, size); +if (!p) { +snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +"Failed to reserve %s\n" , type); + fgraph_error_str = fgraph_error_str_buf; +return 0 ; + } + +switch (fgraph_store_size) { +case 1 : + *(char *)p =BYTE_NUMBER ; +break ; +case 2 : + *(short *)p =SHORT_NUMBER ; +break ; +case 4 : + *(int *)p =WORD_NUMBER ; +break ; +case 8 : + *(long long *)p =LONG_NUMBER ; +break ; + } + +return 1 ; +} + +static __init void store_return (struct ftrace_graph_ret *trace, +struct fgraph_ops *gops) +{ +const char *type = fgraph_store_type_name; +long long expect =0 ; +long long found =-1 ; +int size; +char *p; + + p =fgraph_retrieve_data (gops->idx, &size); +if (!p) { +snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +"Failed to retrieve %s\n" , type); + fgraph_error_str = fgraph_error_str_buf; +return ; + } +if (fgraph_store_size > size) { +snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +"Retrieved size %d is smaller than expected %d\n" , + size, (int )fgraph_store_size); + fgraph_error_str = fgraph_error_str_buf; +return ; + } + +switch (fgraph_store_size) { +case 1 : + expect =BYTE_NUMBER ; + found = *(char *)p; +break ; +case 2 : + expect =SHORT_NUMBER ; + found = *(short *)p; +break ; +case 4 : + expect =WORD_NUMBER ; + found = *(int *)p; +break ; +case 8 : + expect =LONG_NUMBER ; + found = *(long long *)p; +break ; + } + +if (found != expect) { +snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +"%s returned not %lld but %lld\n" , type, expect, found); + fgraph_error_str = fgraph_error_str_buf; +return ; + } + fgraph_error_str =NULL ; +} + +static struct fgraph_ops store_bytes__initdata = { + .entryfunc = store_entry, + .retfunc = store_return, +}; + +static int __init test_graph_storage_type (const char *name,int size) +{ +char *func_name; +int len; +int ret; + + fgraph_store_type_name = name; + fgraph_store_size = size; + +snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +"Failed to execute storage %s\n" , name); + fgraph_error_str = fgraph_error_str_buf; + +pr_cont ("PASSED\n" ); +pr_info ("Testing fgraph storage of %d byte%s: " , size, size >1 ?"s" :"" ); + + func_name ="*" __stringify(DYN_FTRACE_TEST_NAME ); + len =strlen (func_name); + + ret =ftrace_set_filter (&store_bytes.ops, func_name, len,1 ); +if (ret && ret != -ENODEV ) { +pr_cont ("*Could not set filter* " ); +return -1 ; + } + + ret =register_ftrace_graph (&store_bytes); +if (ret) { +pr_warn ("Failed to init store_bytes fgraph tracing\n" ); +return -1 ; + } + +DYN_FTRACE_TEST_NAME (); + +unregister_ftrace_graph (&store_bytes); + +if (fgraph_error_str) { +pr_cont ("*** %s ***" , fgraph_error_str); +return -1 ; + } + +return 0 ; +} +/* Test the storage passed across function_graph entry and return */ +static __init int test_graph_storage (void ) +{ +int ret; + + ret =test_graph_storage_type ("byte" ,1 ); +if (ret) +return ret; + ret =test_graph_storage_type ("short" ,2 ); +if (ret) +return ret; + ret =test_graph_storage_type ("word" ,4 ); +if (ret) +return ret; + ret =test_graph_storage_type ("long long" ,8 ); +if (ret) +return ret; +return 0 ; +} +#else +static inline int test_graph_storage (void ) {return 0 ; } +#endif /* CONFIG_DYNAMIC_FTRACE */ +/* Maximum number of functions to trace before diagnosing a hang */ #define GRAPH_MAX_FUNC_TEST 100000000
@@ -913,6 +1080,8 @@ trace_selftest_startup_function_graph(struct tracer *trace,ftrace_set_global_filter (NULL ,0 ,1 );#endif + ret =test_graph_storage (); +/* Don't test dynamic tracing, the function tracer already did */ out:/* Stop it if we failed */
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 18/20 ftrace: Add multiple fgraph storage selftest
In reply to Steven RostedtFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add a selftest for multiple function graph tracer with storage on a same function. In this case, the shadow stack entry will be shared among those fgraph with different data storage. So this will ensure the fgraph will not mixed those storage data.
--- kernel/trace/trace_selftest.c | 171 +++++++++++++++++++++++++--------- 1 file changed, 126 insertions(+), 45 deletions(-)
kernel/trace/trace_selftest.c +126 -45
@@ -762,28 +762,32 @@ trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)#define SHORT_NUMBER 12345#define WORD_NUMBER 1234567890#define LONG_NUMBER 1234567890123456789LL - -static int fgraph_store_size__initdata ; -static const char *fgraph_store_type_name__initdata ; -static char *fgraph_error_str__initdata ; -static char fgraph_error_str_buf[128 ]__initdata ; +#define ERRSTR_BUFLEN 128 + +struct fgraph_fixture { +struct fgraph_ops gops; +int store_size; +const char *store_type_name; +char error_str_buf[ERRSTR_BUFLEN ]; +char *error_str; +};static __init int store_entry (struct ftrace_graph_ent *trace,struct fgraph_ops *gops) { -const char *type = fgraph_store_type_name; -int size = fgraph_store_size; + struct fgraph_fixture *fixture = container_of(gops, struct fgraph_fixture, gops); +const char *type = fixture->store_type_name; +int size = fixture->store_size;void *p; p =fgraph_reserve_data (gops->idx, size);if (!p) { -snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +snprintf (fixture->error_str_buf,ERRSTR_BUFLEN ,"Failed to reserve %s\n" , type); - fgraph_error_str = fgraph_error_str_buf;return 0 ; } -switch (fgraph_store_size) { +switch (size) {case 1 : *(char *)p =BYTE_NUMBER ;break ;
@@ -804,7 +808,8 @@ static __init int store_entry(struct ftrace_graph_ent *trace,static __init void store_return (struct ftrace_graph_ret *trace,struct fgraph_ops *gops) { -const char *type = fgraph_store_type_name; + struct fgraph_fixture *fixture = container_of(gops, struct fgraph_fixture, gops); +const char *type = fixture->store_type_name;long long expect =0 ;long long found =-1 ;int size;
@@ -812,20 +817,18 @@ static __init void store_return(struct ftrace_graph_ret *trace, p =fgraph_retrieve_data (gops->idx, &size);if (!p) { -snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +snprintf (fixture->error_str_buf,ERRSTR_BUFLEN ,"Failed to retrieve %s\n" , type); - fgraph_error_str = fgraph_error_str_buf;return ; } -if (fgraph_store_size > size) { -snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +if (fixture->store_size > size) { +snprintf (fixture->error_str_buf,ERRSTR_BUFLEN ,"Retrieved size %d is smaller than expected %d\n" , - size, (int )fgraph_store_size); - fgraph_error_str = fgraph_error_str_buf; + size, (int )fixture->store_size);return ; } -switch (fgraph_store_size) { +switch (fixture->store_size) {case 1 : expect =BYTE_NUMBER ; found = *(char *)p;
@@ -845,45 +848,44 @@ static __init void store_return(struct ftrace_graph_ret *trace, }if (found != expect) { -snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), +snprintf (fixture->error_str_buf,ERRSTR_BUFLEN ,"%s returned not %lld but %lld\n" , type, expect, found); - fgraph_error_str = fgraph_error_str_buf;return ; } - fgraph_error_str =NULL ; + fixture->error_str =NULL ; } -static struct fgraph_ops store_bytes__initdata = { - .entryfunc = store_entry, - .retfunc = store_return, -}; - -static int __init test_graph_storage_type (const char *name,int size) +static int __init init_fgraph_fixture (struct fgraph_fixture *fixture) {char *func_name;int len; -int ret; - fgraph_store_type_name = name; - fgraph_store_size = size; +snprintf (fixture->error_str_buf,ERRSTR_BUFLEN , +"Failed to execute storage %s\n" , fixture->store_type_name); + fixture->error_str = fixture->error_str_buf; -snprintf (fgraph_error_str_buf,sizeof (fgraph_error_str_buf), -"Failed to execute storage %s\n" , name); - fgraph_error_str = fgraph_error_str_buf; + func_name ="*" __stringify(DYN_FTRACE_TEST_NAME ); + len =strlen (func_name); + +return ftrace_set_filter (&fixture->gops.ops, func_name, len,1 ); +} + +/* Test fgraph storage for each size */ +static int __init test_graph_storage_single (struct fgraph_fixture *fixture) +{ +int size = fixture->store_size; +int ret;pr_cont ("PASSED\n" );pr_info ("Testing fgraph storage of %d byte%s: " , size, size >1 ?"s" :"" ); - func_name ="*" __stringify(DYN_FTRACE_TEST_NAME ); - len =strlen (func_name); - - ret =ftrace_set_filter (&store_bytes.ops, func_name, len,1 ); + ret =init_fgraph_fixture (fixture);if (ret && ret != -ENODEV ) {pr_cont ("*Could not set filter* " );return -1 ; } - ret =register_ftrace_graph (&store_bytes); + ret =register_ftrace_graph (&fixture->gops);if (ret) {pr_warn ("Failed to init store_bytes fgraph tracing\n" );return -1 ;
@@ -891,30 +893,109 @@ static int __init test_graph_storage_type(const char *name, int size)DYN_FTRACE_TEST_NAME (); -unregister_ftrace_graph (&store_bytes); +unregister_ftrace_graph (&fixture->gops); -if (fgraph_error_str) { -pr_cont ("*** %s ***" , fgraph_error_str); +if (fixture->error_str) { +pr_cont ("*** %s ***" , fixture->error_str);return -1 ; }return 0 ; } + +static struct fgraph_fixture store_bytes[4 ]__initdata = { + [0 ] = { + .gops = { + .entryfunc = store_entry, + .retfunc = store_return, + }, + .store_size =1 , + .store_type_name ="byte" , + }, + [1 ] = { + .gops = { + .entryfunc = store_entry, + .retfunc = store_return, + }, + .store_size =2 , + .store_type_name ="short" , + }, + [2 ] = { + .gops = { + .entryfunc = store_entry, + .retfunc = store_return, + }, + .store_size =4 , + .store_type_name ="word" , + }, + [3 ] = { + .gops = { + .entryfunc = store_entry, + .retfunc = store_return, + }, + .store_size =8 , + .store_type_name ="long long" , + }, +}; + +static __init int test_graph_storage_multi (void ) +{ +struct fgraph_fixture *fixture; +bool printed = false; +int i, ret; + +pr_cont ("PASSED\n" ); +pr_info ("Testing multiple fgraph storage on a function: " ); + +for (i =0 ; i <ARRAY_SIZE (store_bytes); i++) { + fixture = &store_bytes[i]; + ret =init_fgraph_fixture (fixture); +if (ret && ret != -ENODEV ) { +pr_cont ("*Could not set filter* " ); + printed = true; + goto out; + } + + ret =register_ftrace_graph (&fixture->gops); +if (ret) { +pr_warn ("Failed to init store_bytes fgraph tracing\n" ); + printed = true; + goto out; + } + } + +DYN_FTRACE_TEST_NAME (); +out: +while (--i >=0 ) { + fixture = &store_bytes[i]; +unregister_ftrace_graph (&fixture->gops); + +if (fixture->error_str && !printed) { +pr_cont ("*** %s ***" , fixture->error_str); + printed = true; + } + } +return printed ?-1 :0 ; +} +/* Test the storage passed across function_graph entry and return */ static __init int test_graph_storage (void ) {int ret; - ret =test_graph_storage_type ("byte" ,1 ); + ret =test_graph_storage_single (&store_bytes[0 ]); +if (ret) +return ret; + ret =test_graph_storage_single (&store_bytes[1 ]);if (ret)return ret; - ret =test_graph_storage_type ("short" ,2 ); + ret =test_graph_storage_single (&store_bytes[2 ]);if (ret)return ret; - ret =test_graph_storage_type ("word" ,4 ); + ret =test_graph_storage_single (&store_bytes[3 ]);if (ret)return ret; - ret =test_graph_storage_type ("long long" ,8 ); + ret =test_graph_storage_multi ();if (ret)return ret;return 0 ;
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 19/20 function_graph: Use for_each_set_bit() in __ftrace_return_to_handler()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Instead of iterating through the entire fgraph_array[] and seeing if one of the bitmap bits are set to know to call the array's retfunc() function, use for_each_set_bit() on the bitmap itself. This will only iterate for the number of set bits.
--- kernel/trace/fgraph.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
kernel/trace/fgraph.c +2 -3
@@ -827,11 +827,10 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs#endif bitmap =get_bitmap_bits (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)continue ;
Signature · 1 line
2.43.0
Steven Rostedt
maintainerPATCH 20/20 function_graph: Use bitmask to loop on fgraph entry
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Instead of looping through all the elements of fgraph_array[] to see if there's an gops attached to one and then calling its gops->func(). Create a fgraph_array_bitmask that sets bits when an index in the array is reserved (via the simple lru algorithm). Then only the bits set in this bitmask needs to be looked at where only elements in the array that have ops registered need to be looked at.
Note, we do not care about races. If a bit is set before the gops is assigned, it only wastes time looking at the element and ignoring it (as it did before this bitmask is added).
--- kernel/trace/fgraph.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/trace/fgraph.c +7 -1
@@ -173,6 +173,7 @@ DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);int ftrace_graph_active;static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +static unsigned long fgraph_array_bitmask;/* LRU index table for fgraph_array */ static int fgraph_lru_table[FGRAPH_ARRAY_SIZE ];
@@ -197,6 +198,8 @@ static int fgraph_lru_release_index(int idx) fgraph_lru_table[fgraph_lru_last] = idx; fgraph_lru_last = (fgraph_lru_last +1 ) %FGRAPH_ARRAY_SIZE ; + +clear_bit (idx, &fgraph_array_bitmask);return 0 ; }
@@ -211,6 +214,8 @@ static int fgraph_lru_alloc_index(void) fgraph_lru_table[fgraph_lru_next] =-1 ; fgraph_lru_next = (fgraph_lru_next +1 ) %FGRAPH_ARRAY_SIZE ; + +set_bit (idx, &fgraph_array_bitmask);return idx; }
@@ -632,7 +637,8 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (offset <0 ) goto out; -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +for_each_set_bit (i, &fgraph_array_bitmask, +sizeof (fgraph_array_bitmask) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i];int save_curr_ret_stack;
Signature · 1 line
2.43.0
Re: [PATCH 19/20] function_graph: Use for_each_set_bit() in __ftrace_return_to_handler()
In reply to Steven RostedtQuoted 7 lines · On Fri, 24 May 2024 22:37:11 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (Google)" <[email protected]>
Instead of iterating through the entire fgraph_array[] and seeing if one of the bitmap bits are set to know to call the array's
retfunc()function, usefor_each_set_bit()on the bitmap itself. This will only iterate for the number of set bits.
Looks good to me.
Thanks,
Quoted 27 lines
Signed-off-by Steven Rostedt (Google) <[email protected]>--- kernel/trace/fgraph.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)kernel/trace/fgraph.c +2
-3@@ -827,11 +827,10 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs#endif bitmap =get_bitmap_bits (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)continue ;Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerRe: [PATCH 19/20] function_graph: Use for_each_set_bit() in __ftrace_return_to_handler()
In reply to Steven RostedtQuoted 18 lines · On Fri, 24 May 2024 22:37:11 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (Google)" <[email protected]>
Instead of iterating through the entire fgraph_array[] and seeing if one of the bitmap bits are set to know to call the array's
retfunc()function, usefor_each_set_bit()on the bitmap itself. This will only iterate for the number of set bits.Signed-off-by Steven Rostedt (Google) <[email protected]>--- kernel/trace/fgraph.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)kernel/trace/fgraph.c +0
-0@@ -827,11 +827,10 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs#endif
kernel/trace/fgraph.c static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs · line 834
Quoted 10 lines
kernel/trace/fgraph.c +2
-3bitmap =get_bitmap_bits (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)
Ah, nit: maybe this is unlikely()?
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH 20/20] function_graph: Use bitmask to loop on fgraph entry
In reply to Steven RostedtQuoted 9 lines · On Fri, 24 May 2024 22:37:12 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (Google)" <[email protected]>
Instead of looping through all the elements of fgraph_array[] to see if there's an gops attached to one and then calling its
gops->func(). Create afgraph_array_bitmaskthat sets bits when an index in the array is reserved (via the simple lru algorithm). Then only the bits set in this bitmask needs to be looked at where only elements in the array that have ops registered need to be looked at.
Quoted 3 lines
Note, we do not care about races. If a bit is set before the gops is assigned, it only wastes time looking at the element and ignoring it (as it did before this bitmask is added).
This is OK because anyway we check gops == &fgraph_stub. By the way, shouldn't we also make "if (gops == &fgraph_stub)" check unlikely()?
This change looks good to me.
Thank you,
Quoted 50 lines
Signed-off-by Steven Rostedt (Google) <[email protected]>--- kernel/trace/fgraph.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/trace/fgraph.c +7
-1@@ -173,6 +173,7 @@ DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph);int ftrace_graph_active;static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +static unsigned long fgraph_array_bitmask;/* LRU index table for fgraph_array */ static int fgraph_lru_table[FGRAPH_ARRAY_SIZE ];@@ -197,6 +198,8 @@ static int fgraph_lru_release_index(int idx) fgraph_lru_table[fgraph_lru_last] = idx; fgraph_lru_last = (fgraph_lru_last +1 ) %FGRAPH_ARRAY_SIZE ; + +clear_bit (idx, &fgraph_array_bitmask);return 0 ; }@@ -211,6 +214,8 @@ static int fgraph_lru_alloc_index(void) fgraph_lru_table[fgraph_lru_next] =-1 ; fgraph_lru_next = (fgraph_lru_next +1 ) %FGRAPH_ARRAY_SIZE ; + +set_bit (idx, &fgraph_array_bitmask);return idx; }@@ -632,7 +637,8 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (offset <0 ) goto out; -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +for_each_set_bit (i, &fgraph_array_bitmask, +sizeof (fgraph_array_bitmask) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i];int save_curr_ret_stack;Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRe: [PATCH 19/20] function_graph: Use for_each_set_bit() in __ftrace_return_to_handler()
In reply to Masami Hiramatsu (Google)Quoted 11 lines · On Mon, 27 May 2024 09:04:34 +0900 Masami Hiramatsu (Google)…
kernel/trace/fgraph.c static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs · line 834
Quoted 9 lines
kernel/trace/fgraph.c +2
-3bitmap =get_bitmap_bits (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)Ah, nit: maybe this is
unlikely()?
Ah probably. I'll update it.
Thanks,
-- Steve
Steven Rostedt
maintainerRe: [PATCH 20/20] function_graph: Use bitmask to loop on fgraph entry
In reply to Masami Hiramatsu (Google)Quoted 7 lines · On Mon, 27 May 2024 09:09:49 +0900 Masami Hiramatsu (Google)…
Quoted 3 lines
Note, we do not care about races. If a bit is set before the gops is assigned, it only wastes time looking at the element and ignoring it (as it did before this bitmask is added).
This is OK because anyway we check gops ==
&fgraph_stub. By the way, shouldn't we also make "if (gops ==&fgraph_stub)" checkunlikely()?
Yeah, I'll add the unlikely() here too.
Quoted 4 lines
This change looks good to me.
Reviewed-by Masami Hiramatsu (Google) <[email protected]>
Thanks for the review Masami!
-- Steve
Masami Hiramatsu (Google)
maintainerRe: [PATCH 04/20] function_graph: Allow multiple users to attach to function graph
In reply to Steven RostedtQuoted 28 lines · On Fri, 24 May 2024 22:36:56 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow for multiple users to attach to function graph tracer at the same time. Only 16 simultaneous users can attach to the tracer. This is because there's an array that stores the pointers to the attached
fgraph_ops. When a function being traced is entered, each of theftrace_opsentryfunc is called and if it returns non zero, its index into the array will be added to the shadow stack.On exit of the function being traced, the shadow stack will contain the indexes of the
ftrace_opson the array that want their retfunc to be called.Because a function may sleep for a long time (if a task sleeps itself), the return of the function may be literally days later. If the
ftrace_opsis removed, its place on the array is replaced with aftrace_opsthat contains the stub functions and that will be called when the function finally returns.If another
ftrace_opsis added that happens to get the same index into the array, its return function may be called. But that's actually the way things current work with the old function graph tracer. If one tracer is removed and another is added, the new one will get the return calls of the function traced by the previous one, thus this is not a regression. This can be fixed by adding a counter to each time the array item is updated and save that on the shadow stack as well, such that it won't be called if the index saved does not match the index on the array.
Quoted 6 lines
Note, being able to filter functions when both are called is not completely handled yet, but that shouldn't be too hard to manage.
Co-developed with Masami Hiramatsu:
Thanks for update this. I have some comments below.
Quoted 4 lines
Signed-off-by Steven Rostedt (VMware) <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>---
[...]
kernel/trace/fgraph.c void ftrace_graph_stop(void) · line 257
Quoted 7 lines
kernel/trace/fgraph.c +2
-1@@ -110,11 +253,13 @@ void ftrace_graph_stop(void)/* Add a function return address to the trace stack on thread info.*/ static int ftrace_push_return_trace (unsigned long ret,unsigned long func, -unsigned long frame_pointer,unsigned long *retp) +unsigned long frame_pointer,unsigned long *retp, +int fgraph_idx)
We do not need this fgraph_idx parameter anymore because this removed reuse-frame check.
Quoted 111 lines
kernel/trace/fgraph.c +54
-10{struct ftrace_ret_stack *ret_stack;unsigned long long calltime; -int index; +unsigned long val; +int offset;if (unlikely (ftrace_graph_is_dead ()))return -EBUSY ;@@ -124,24 +269,57 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); +/* Set val to "reserved" with the delta to the new fgraph frame */ + val = (FGRAPH_TYPE_RESERVED <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; +/* * We must make sure the ret_stack is tested before we read * anything else. */ smp_rmb (); -/* The return trace stack is full */ -if (current->curr_ret_stack >=SHADOW_STACK_MAX_INDEX ) { +/* +* Check if there's room on the shadow stack to fit a fraph frame +* and a bitmap word. +*/ +if (current->curr_ret_stack +FGRAPH_FRAME_OFFSET +1 >=SHADOW_STACK_MAX_OFFSET ) {atomic_inc (¤t->trace_overrun);return -EBUSY ; } calltime =trace_clock_local (); - index = current->curr_ret_stack; -RET_STACK_INC (current->curr_ret_stack); - ret_stack =RET_STACK (current, index); + offset =READ_ONCE (current->curr_ret_stack); + ret_stack =RET_STACK (current, offset); + offset +=FGRAPH_FRAME_OFFSET ; + +/* ret offset = FGRAPH_FRAME_OFFSET ; type = reserved */ + current->ret_stack[offset] = val; + ret_stack->ret = ret; +/* +* The unwinders expect curr_ret_stack to point to either zero +* or an offset where to find the next ret_stack. Even though the +* ret stack might be bogus, we want to write the ret and the +* offset to find the ret_stack before we increment the stack point. +* If an interrupt comes in now before we increment the curr_ret_stack +* it may blow away what we wrote. But that's fine, because the +* offset will still be correct (even though the 'ret' won't be). +* What we worry about is the offset being correct after we increment +* the curr_ret_stack and before we update that offset, as if an +* interrupt comes in and does an unwind stack dump, it will need +* at least a correct offset! +*/ barrier (); +WRITE_ONCE (current->curr_ret_stack, offset +1 ); +/* +* This next barrier is to ensure that an interrupt coming in +* will not corrupt what we are about to write. +*/ +barrier (); + +/* Still keep it reserved even if an interrupt came in */ + current->ret_stack[offset] = val; + ret_stack->ret = ret; ret_stack->func = func; ret_stack->calltime = calltime;@@ -151,7 +329,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR ret_stack->retp = retp;#endif -return 0 ; +return offset; }/* @@ -168,49 +346,67 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,# define MCOUNT_INSN_SIZE 0#endif +/* If the caller does not use ftrace, call this function. */ int function_graph_enter (unsigned long ret,unsigned long func,unsigned long frame_pointer,unsigned long *retp) {struct ftrace_graph_ent trace; +unsigned long bitmap =0 ; +int offset; +int i; trace.func = func; trace.depth = ++current->curr_ret_depth; -if (ftrace_push_return_trace (ret, func, frame_pointer, retp)) + offset =ftrace_push_return_trace (ret, func, frame_pointer, retp,0 ); +if (offset <0 ) goto out; -/* Only trace if the calling function expects to */ -if (!fgraph_array[0 ]->entryfunc (&trace)) +for (i =0 ; i < fgraph_array_cnt; i++) { +struct fgraph_ops *gops = fgraph_array[i]; + +if (gops == &fgraph_stub) +continue ; + +if (gops->entryfunc (&trace))
kernel/trace/fgraph.c ftrace_push_return_trace(unsigned long ret, unsigned long func, · line 381
Quoted 10 lines
kernel/trace/fgraph.c +8
-0+ bitmap |=BIT (i); + } + +if (!bitmap) goto out_ret; +/* +* Since this function uses fgraph_idx = 0 as a tail-call checking +* flag, set that bit always. +*/
This comment is also out-of-date.
kernel/trace/fgraph.c ftrace_push_return_trace(unsigned long ret, unsigned long func, · line 382
Quoted 1 line
kernel/trace/fgraph.c +1
-0+set_bitmap (current, offset, bitmap |BIT (0 ));
And we do not need to set BIT(0) anymore.
Quoted 98 lines
kernel/trace/fgraph.c +29
-14+return 0 ; out_ret: -RET_STACK_DEC (current->curr_ret_stack); + current->curr_ret_stack -=FGRAPH_FRAME_OFFSET +1 ; out: current->curr_ret_depth--;return -EBUSY ; }/* Retrieve a function return address to the trace stack on thread info.*/ -static void +static struct ftrace_ret_stack *ftrace_pop_return_trace (struct ftrace_graph_ret *trace,unsigned long *ret, -unsigned long frame_pointer) +unsigned long frame_pointer,int *offset) {struct ftrace_ret_stack *ret_stack; -int index; - index = current->curr_ret_stack; -RET_STACK_DEC (index); + ret_stack =get_ret_stack (current, current->curr_ret_stack, offset); -if (unlikely (index <0 || index >SHADOW_STACK_MAX_INDEX )) { +if (unlikely (!ret_stack)) {ftrace_graph_stop (); -WARN_ON (1 ); +WARN (1 ,"Bad function graph ret_stack pointer: %d" , + current->curr_ret_stack);/* Might as well panic, otherwise we have no where to go */ *ret = (unsigned long )panic; -return ; +return NULL ; } - ret_stack =RET_STACK (current, index);#ifdef HAVE_FUNCTION_GRAPH_FP_TEST /* * The arch may choose to record the frame pointer used @@ -230,26 +426,29 @@ ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,ftrace_graph_stop ();WARN (1 ,"Bad frame pointer: expected %lx, received %lx\n" " from func %ps return to %lx\n" , - current->ret_stack[index].fp, + ret_stack->fp, frame_pointer, (void *)ret_stack->func, ret_stack->ret); *ret = (unsigned long )panic; - return; + return NULL; }#endif + *offset +=FGRAPH_FRAME_OFFSET ; *ret = ret_stack->ret; trace->func = ret_stack->func; trace->calltime = ret_stack->calltime; trace->overrun =atomic_read (¤t->trace_overrun); - trace->depth = current->curr_ret_depth--; + trace->depth = current->curr_ret_depth;/* * We still want to trace interrupts coming in if * max_depth is set to 1. Make sure the decrement is * seen before ftrace_graph_return. */ barrier (); + +return ret_stack; }/* @@ -287,30 +486,47 @@ struct fgraph_ret_regs;static unsigned long __ftrace_return_to_handler (struct fgraph_ret_regs *ret_regs,unsigned long frame_pointer) { +struct ftrace_ret_stack *ret_stack;struct ftrace_graph_ret trace; +unsigned long bitmap;unsigned long ret; +int offset; +int i; + + ret_stack =ftrace_pop_return_trace (&trace, &ret, frame_pointer, &offset); + +if (unlikely (!ret_stack)) { +ftrace_graph_stop (); +WARN_ON (1 ); +/* Might as well panic. What else to do? */ +return (unsigned long )panic; + } -ftrace_pop_return_trace (&trace, &ret, frame_pointer); + trace.rettime =trace_clock_local ();#ifdef CONFIG_FUNCTION_GRAPH_RETVAL trace.retval =fgraph_ret_regs_return_value (ret_regs);#endif
kernel/trace/fgraph.c struct fgraph_ret_regs; · line 516
Quoted 10 lines
kernel/trace/fgraph.c +8
-2- trace.rettime =trace_clock_local (); - fgraph_array[0 ]->retfunc (&trace); + + bitmap =get_bitmap_bits (current, offset); +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +struct fgraph_ops *gops = fgraph_array[i]; + +if (!(bitmap &BIT (i))) +continue ; +if (gops == &fgraph_stub)
nit: here, we can make this check unlikely() because the above bitmap check already filtered. (Some sleepable functions leave the return frame on shadow stack after gops is unregistered. But it also rare compared with living time.)
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Masami Hiramatsu (Google)
maintainerQuoted 33 lines · On Fri, 24 May 2024 22:36:52 -0400 Steven Rostedt <[email protected]> wrote:
[
Resend for some of you as I missed a comma in the Cc and quilt died sending this out.
]This is a continuation of the function graph multi user code. I wrote a proof of concept back in 2019 of this code[1] and Masami started cleaning it up. I started from Masami's work v10 that can be found here:
https://lore.kernel.org/linux-trace-kernel/171509088006.162236.7227326999861366050.stgit@devnote2/
This is *only* the code that allows multiple users of function graph tracing. This is not the fprobe work that Masami is working to add on top of it. As Masami took my proof of concept, there was still several things I disliked about that code. Instead of having Masami clean it up even more, I decided to take over on just my code and change it up a bit.
The biggest changes from where Masami left off is mostly renaming more variables, macros, and function names. I fixed up the current comments and added more to make the code a bit more understandable.
At the end of the series, I added two patches to optimize the entry and exit. On entry, there was a loop that iterated the 16 elements of the fgraph_array[] looking for any that may have a gops registered to it. It's quite a waste to do that loop if there's only one registered user. To fix that, I added a
fgraph_array_bitmaskthat has its bits set that correspond to the elements of the array. Then a simplefor_each_set_bit()is used for the iteration. I do the same thing at the exit callback of the function where it iterates over the bits of the bitmap saved on theret_stack.
Quoted 5 lines
I also noticed that Masami added code to handle tail calls in the unwinder and had that in one of my patches. I took that code out and made it a separate patch with Masami as the author.
The diff between this and Masami's last update is at the end of this email.
Thanks for update the patches!
I think your changes are good. I just have some comments and replied. So, the plan is to push this series in the tracing/for-next? I will port my fprobe part on it and run some tests.
Thank you,
Quoted 529 lines
Based on Linus commit:
0eb03c7e8e2a4cc3653eb5eeb2d2001182071215[1] https://lore.kernel.org/all/[email protected]/
Masami Hiramatsu (Google) (3):
function_graph: Handle tail calls for stack unwinding
function_graph: Use a simple LRU forfgraph_arrayindex number
ftrace: Add multiple fgraph storage selftestSteven Rostedt (Google) (2):
function_graph: Usefor_each_set_bit()in__ftrace_return_to_handler()
function_graph: Use bitmask to loop on fgraph entrySteven Rostedt (VMware) (15):
function_graph: Convertret_stackto a series of longs
fgraph: UseBUILD_BUG_ON()to make sure we have structures divisible by long
function_graph: Add an array structure that will allow multiple callbacks
function_graph: Allow multiple users to attach to function graph
function_graph: Remove logic aroundftrace_graph_entryand return ftrace/function_graph: Passfgraph_opsto function graph callbacks
ftrace: Allowfunction_graphtracer to be enabled in instances
ftrace: Allow ftrace startup flags to exist without dynamic ftrace
function_graph: Have the instances use their ownftrace_opsfor filtering
function_graph: Add "task variables" per task forfgraph_ops
function_graph: Moveset_graph_functiontests to shadow stack global var
function_graph: Move graph depth stored data to shadow stack global var
function_graph: Move graph notrace bit to shadow stack global var
function_graph: Implementfgraph_reserve_data()andfgraph_retrieve_data()
function_graph: Add selftest for passing local variables----
arch/arm64/kernel/ftrace.c | 21 +- arch/loongarch/kernel/ftrace_dyn.c | 15 +- arch/powerpc/kernel/trace/ftrace.c | 3 +- arch/riscv/kernel/ftrace.c | 15 +- arch/x86/kernel/ftrace.c | 19 +- include/linux/ftrace.h | 42 +- include/linux/sched.h | 2 +- include/linux/trace_recursion.h | 39 -- kernel/trace/fgraph.c | 994 ++++++++++++++++++++++++++++------- kernel/trace/ftrace.c | 11 +- kernel/trace/ftrace_internal.h | 2 - kernel/trace/trace.h | 94 +++- kernel/trace/trace_functions.c | 8 + kernel/trace/trace_functions_graph.c | 96 ++-- kernel/trace/trace_irqsoff.c | 10 +- kernel/trace/trace_sched_wakeup.c | 10 +- kernel/trace/trace_selftest.c | 259 ++++++++- 17 files changed, 1330 insertions(+), 310 deletions(-)kernel/trace/fgraph.c +125
-85@@ -18,29 +18,36 @@#include "ftrace_internal.h" #include "trace.h" -#define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) -#define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long))/* -* On entry to a function (via function_graph_enter()), a new ftrace_ret_stack -* is allocated on the task's ret_stack with bitmap entry, then each -* fgraph_ops on the fgraph_array[]'s entryfunc is called and if that returns -* non-zero, the index into the fgraph_array[] for that fgraph_ops is recorded -* on the bitmap entry as a bit flag. -* -* The top of the ret_stack (when not empty) will always have a reference -* to the last ftrace_ret_stack saved. All references to the -* ftrace_ret_stack has the format of: +* FGRAPH_FRAME_SIZE: Size in bytes of the meta data on the shadow stack +* FGRAPH_FRAME_OFFSET: Size in long words of the meta data frame +*/ +#define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) +#define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long)) + +/* +* On entry to a function (via function_graph_enter()), a new fgraph frame +* (ftrace_ret_stack) is pushed onto the stack as well as a word that +* holds a bitmask and a type (called "bitmap"). The bitmap is defined as: * * bits: 0 - 9 offset in words from the previous ftrace_ret_stack -* (bitmap type should have FGRAPH_FRAME_OFFSET always) +* * bits: 10 - 11 Type of storage * 0 - reserved * 1 - bitmap of fgraph_array index * 2 - reserved data * -* For bitmap of fgraph_array index +* For type with "bitmap of fgraph_array index" (FGRAPH_TYPE_BITMAP): * bits: 12 - 27 The bitmap of fgraph_ops fgraph_array index +* That is, it's a bitmask of 0-15 (16 bits) +* where if a corresponding ops in the fgraph_array[] +* expects a callback from the return of the function +* it's corresponding bit will be set. +* +* +* The top of the ret_stack (when not empty) will always have a reference +* word that points to the last fgraph frame that was saved. * * For reserved data: * bits: 12 - 17 The size in words that is stored @@ -52,7 +59,7 @@* stored two words of data, this is what will be on the task's shadow * ret_stack: (the stack grows upward) * -* ret_stack[SHADOW_STACK_IN_WORD] +* ret_stack[SHADOW_STACK_OFFSET] * | SHADOW_STACK_TASK_VARS(ret_stack)[15] | * ... * | SHADOW_STACK_TASK_VARS(ret_stack)[0] | @@ -60,6 +67,8 @@* ... * | | <- task->curr_ret_stack * +--------------------------------------------+ +* | (3 << 12) | (3 << 10) | FGRAPH_FRAME_OFFSET| +* | *or put another way* | * | (3 << FGRAPH_DATA_INDEX_SHIFT)| \ | This is for fgraph_ops[3]. * | ((2 - 1) << FGRAPH_DATA_SHIFT)| \ | The data size is 2 words. * | (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT)| \ | @@ -67,7 +76,9 @@* +--------------------------------------------+ ( It is 4 words from the ret_stack) * | STORED DATA WORD 2 | * | STORED DATA WORD 1 | -* +------i-------------------------------------+ +* +--------------------------------------------+ +* | (9 << 12) | (1 << 10) | FGRAPH_FRAME_OFFSET| +* | *or put another way* | * | (BIT(3)|BIT(0)) << FGRAPH_INDEX_SHIFT | \ | * | FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT| \ | * | (offset1:FGRAPH_FRAME_OFFSET) | <- the offset1 is from here @@ -82,17 +93,24 @@* * If a backtrace is required, and the real return pointer needs to be * fetched, then it looks at the task's curr_ret_stack offset, if it -* is greater than zero (reserved, or right before poped), it would mask +* is greater than zero (reserved, or right before popped), it would mask * the value by FGRAPH_FRAME_OFFSET_MASK to get the offset of the * ftrace_ret_stack structure stored on the shadow stack. */ -#define FGRAPH_FRAME_OFFSET_SIZE 10 -#define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_SIZE - 1, 0) +/* +* The following is for the top word on the stack: +* +* FGRAPH_FRAME_OFFSET (0-9) holds the offset delta to the fgraph frame +* FGRAPH_TYPE (10-11) holds the type of word this is. +* (RESERVED or BITMAP) +*/ +#define FGRAPH_FRAME_OFFSET_BITS 10 +#define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_BITS - 1, 0) -#define FGRAPH_TYPE_SIZE 2 -#define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_SIZE - 1, 0) -#define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_SIZE +#define FGRAPH_TYPE_BITS 2 +#define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_BITS - 1, 0) +#define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_BITSenum {FGRAPH_TYPE_RESERVED =0 ,@@ -100,31 +118,48 @@ enum { FGRAPH_TYPE_DATA = 2, }; -#define FGRAPH_INDEX_SIZE 16 -#define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_SIZE - 1, 0) -#define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_SIZE) +/* +* For BITMAP type: +* FGRAPH_INDEX (12-27) bits holding the gops index wanting return callback called +*/ +#define FGRAPH_INDEX_BITS 16 +#define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_BITS - 1, 0) +#define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) -/* The data size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words. */ -#define FGRAPH_DATA_SIZE 5 -#define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_SIZE - 1, 0) -#define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_SIZE) -#define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_SIZE)) +/* +* For DATA type: +* FGRAPH_DATA (12-17) bits hold the size of data (in words) +* FGRAPH_INDEX (18-23) bits hold the index for which gops->idx the data is for +* +* Note: +* data_size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words. +*/ +#define FGRAPH_DATA_BITS 5 +#define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_BITS - 1, 0) +#define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) +#define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_BITS)) -#define FGRAPH_DATA_INDEX_SIZE 4 -#define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_SIZE - 1, 0) -#define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_SIZE) +#define FGRAPH_DATA_INDEX_BITS 4 +#define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_BITS - 1, 0) +#define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_BITS)#define FGRAPH_MAX_INDEX \ - ((FGRAPH_INDEX_SIZE << FGRAPH_DATA_SIZE) + FGRAPH_RET_INDEX) + ((FGRAPH_INDEX_SIZE << FGRAPH_DATA_BITS) + FGRAPH_RET_INDEX) -#define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_SIZE +#define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_BITS -#define SHADOW_STACK_SIZE (PAGE_SIZE) -#define SHADOW_STACK_IN_WORD (SHADOW_STACK_SIZE / sizeof(long)) +/* +* SHADOW_STACK_SIZE: The size in bytes of the entire shadow stack +* SHADOW_STACK_OFFSET: The size in long words of the shadow stack +* SHADOW_STACK_MAX_OFFSET: The max offset of the stack for a new frame to be added +*/ +#define SHADOW_STACK_SIZE (PAGE_SIZE) +#define SHADOW_STACK_OFFSET (SHADOW_STACK_SIZE / sizeof(long))/* Leave on a buffer at the end */ #define SHADOW_STACK_MAX_OFFSET \ - (SHADOW_STACK_IN_WORD - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) + (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) +/* RET_STACK(): Return the frame from a given @offset from task @t */ #define RET_STACK (t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset]))/* @@ -132,12 +167,13 @@ enum {* ret_stack to store task specific state. */ #define SHADOW_STACK_TASK_VARS (ret_stack) \ - ((unsigned long *)(&(ret_stack)[SHADOW_STACK_IN_WORD - FGRAPH_ARRAY_SIZE])) + ((unsigned long *)(&(ret_stack)[SHADOW_STACK_OFFSET - FGRAPH_ARRAY_SIZE]))DEFINE_STATIC_KEY_FALSE (kill_ftrace_graph);int ftrace_graph_active;static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE ]; +static unsigned long fgraph_array_bitmask;/* LRU index table for fgraph_array */ static int fgraph_lru_table[FGRAPH_ARRAY_SIZE ];@@ -162,6 +198,8 @@ static int fgraph_lru_release_index(int idx) fgraph_lru_table[fgraph_lru_last] = idx; fgraph_lru_last = (fgraph_lru_last +1 ) %FGRAPH_ARRAY_SIZE ; + +clear_bit (idx, &fgraph_array_bitmask);return 0 ; }@@ -176,70 +214,77 @@ static int fgraph_lru_alloc_index(void) fgraph_lru_table[fgraph_lru_next] =-1 ; fgraph_lru_next = (fgraph_lru_next +1 ) %FGRAPH_ARRAY_SIZE ; + +set_bit (idx, &fgraph_array_bitmask);return idx; } +/* Get the offset to the fgraph frame from a ret_stack value */ static inline int __get_offset (unsigned long val) {return val &FGRAPH_FRAME_OFFSET_MASK ; } +/* Get the type of word from a ret_stack value */ static inline int __get_type (unsigned long val) {return (val >>FGRAPH_TYPE_SHIFT ) &FGRAPH_TYPE_MASK ; } +/* Get the data_index for a DATA type ret_stack word */ static inline int __get_data_index (unsigned long val) {return (val >>FGRAPH_DATA_INDEX_SHIFT ) &FGRAPH_DATA_INDEX_MASK ; } +/* Get the data_size for a DATA type ret_stack word */ static inline int __get_data_size (unsigned long val) {return ((val >>FGRAPH_DATA_SHIFT ) &FGRAPH_DATA_MASK ) +1 ; } +/* Get the word from the ret_stack at @offset */ static inline unsigned long get_fgraph_entry (struct task_struct *t,int offset) {return t->ret_stack[offset]; } +/* Get the FRAME_OFFSET from the word from the @offset on ret_stack */ static inline int get_frame_offset (struct task_struct *t,int offset) {return __get_offset (t->ret_stack[offset]); } +/* Get FGRAPH_TYPE from the word from the @offset at ret_stack */ static inline int get_fgraph_type (struct task_struct *t,int offset) {return __get_type (t->ret_stack[offset]); } +/* For BITMAP type: get the bitmask from the @offset at ret_stack */ static inline unsigned long -get_fgraph_index_bitmap (struct task_struct *t,int offset) +get_bitmap_bits (struct task_struct *t,int offset) {return (t->ret_stack[offset] >>FGRAPH_INDEX_SHIFT ) &FGRAPH_INDEX_MASK ; } +/* For BITMAP type: set the bits in the bitmap bitmask at @offset on ret_stack */ static inline void -set_fgraph_index_bitmap (struct task_struct *t,int offset,unsigned long bitmap) -{ - t->ret_stack[offset] = (bitmap <<FGRAPH_INDEX_SHIFT ) | - (FGRAPH_TYPE_BITMAP <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; -} - -static inline bool is_fgraph_index_set (struct task_struct *t,int offset,int idx) +set_bitmap_bits (struct task_struct *t,int offset,unsigned long bitmap) { -return !!(get_fgraph_index_bitmap (t, offset) &BIT (idx)); + t->ret_stack[offset] |= (bitmap <<FGRAPH_INDEX_SHIFT ); } +/* Write the bitmap to the ret_stack at @offset (does index, offset and bitmask) */ static inline void -add_fgraph_index_bitmap (struct task_struct *t,int offset,unsigned long bitmap) +set_bitmap (struct task_struct *t,int offset,unsigned long bitmap) { - t->ret_stack[offset] |= (bitmap <<FGRAPH_INDEX_SHIFT ); + t->ret_stack[offset] = (bitmap <<FGRAPH_INDEX_SHIFT ) | + (FGRAPH_TYPE_BITMAP <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; } -/* Note: @offset is the offset of FGRAPH_TYPE_DATA entry. */ -static inline void *get_fgraph_data (struct task_struct *t,int offset) +/* For DATA type: get the data saved under the ret_stack word at @offset */ +static inline void *get_data_type_data (struct task_struct *t,int offset) {unsigned long val = t->ret_stack[offset];@@ -249,7 +294,8 @@ static inline void *get_fgraph_data(struct task_struct *t, int offset)return (void *)&t->ret_stack[offset]; } -static inline unsigned long make_fgraph_data (int idx,int size,int offset) +/* Create the ret_stack word for a DATA type */ +static inline unsigned long make_data_type_val (int idx,int size,int offset) {return (idx <<FGRAPH_DATA_INDEX_SHIFT ) | ((size - 1) << FGRAPH_DATA_SHIFT) |@@ -326,7 +372,7 @@ void *fgraph_reserve_data(int idx, int size_bytes)if (unlikely (curr_ret_stack >=SHADOW_STACK_MAX_OFFSET ))return NULL ; - val =make_fgraph_data (idx, data_size,__get_offset (val) + data_size +1 ); + val =make_data_type_val (idx, data_size,__get_offset (val) + data_size +1 );/* Set the last word to be reserved */ current->ret_stack[curr_ret_stack -1 ] = val;@@ -371,7 +417,7 @@ void *fgraph_retrieve_data(int idx, int *size_bytes) found:if (size_bytes) *size_bytes =__get_data_size (val) *sizeof (long ); -return get_fgraph_data (current, offset); +return get_data_type_data (current, offset); }/** @@ -394,6 +440,9 @@ unsigned long *fgraph_get_task_var(struct fgraph_ops *gops)* @offset: The offset into @t->ret_stack to find the ret_stack entry * @frame_offset: Where to place the offset into @t->ret_stack of that entry * +* Returns a pointer to the previous ret_stack below @offset or NULL +* when it reaches the bottom of the stack. +* * Calling this with: * * offset = task->curr_ret_stack; @@ -493,30 +542,21 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,if (!current->ret_stack)return -EBUSY ; -/* -* At first, check whether the previous fgraph callback is pushed by -* the fgraph on the same function entry. -* But if @func is the self tail-call function, we also need to ensure -* the ret_stack is not for the previous call by checking whether the -* bit of @fgraph_idx is set or not. -*/ - ret_stack =get_ret_stack (current, current->curr_ret_stack, &offset); -if (ret_stack && ret_stack->func == func && -get_fgraph_type (current, offset +FGRAPH_FRAME_OFFSET ) ==FGRAPH_TYPE_BITMAP && - !is_fgraph_index_set (current, offset +FGRAPH_FRAME_OFFSET , fgraph_idx)) -return offset +FGRAPH_FRAME_OFFSET ; +BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); +/* Set val to "reserved" with the delta to the new fgraph frame */ val = (FGRAPH_TYPE_RESERVED <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; -BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); -/* * We must make sure the ret_stack is tested before we read * anything else. */ smp_rmb (); -/* The return trace stack is full */ +/* +* Check if there's room on the shadow stack to fit a fraph frame +* and a bitmap word. +*/ if (current->curr_ret_stack +FGRAPH_FRAME_OFFSET +1 >=SHADOW_STACK_MAX_OFFSET ) {atomic_inc (¤t->trace_overrun);return -EBUSY ;@@ -545,7 +585,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,* at least a correct offset! */ barrier (); - current->curr_ret_stack = offset +1 ; +WRITE_ONCE (current->curr_ret_stack, offset +1 );/* * This next barrier is to ensure that an interrupt coming in * will not corrupt what we are about to write. @@ -597,7 +637,8 @@ int function_graph_enter(unsigned long ret, unsigned long func,if (offset <0 ) goto out; -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +for_each_set_bit (i, &fgraph_array_bitmask, +sizeof (fgraph_array_bitmask) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i];int save_curr_ret_stack;@@ -620,7 +661,7 @@ int function_graph_enter(unsigned long ret, unsigned long func,* Since this function uses fgraph_idx = 0 as a tail-call checking * flag, set that bit always. */ -set_fgraph_index_bitmap (current, offset, bitmap |BIT (0 )); +set_bitmap (current, offset, bitmap |BIT (0 ));return 0 ; out_ret:@@ -659,9 +700,9 @@ int function_graph_enter_ops(unsigned long ret, unsigned long func, save_curr_ret_stack = current->curr_ret_stack;if (gops->entryfunc (&trace, gops)) {if (type ==FGRAPH_TYPE_RESERVED ) -set_fgraph_index_bitmap (current, offset,BIT (gops->idx)); +set_bitmap (current, offset,BIT (gops->idx));else -add_fgraph_index_bitmap (current, offset,BIT (gops->idx)); +set_bitmap_bits (current, offset,BIT (gops->idx));return 0 ; }else current->curr_ret_stack = save_curr_ret_stack;@@ -791,12 +832,11 @@ static unsigned long __ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs trace.retval =fgraph_ret_regs_return_value (ret_regs);#endif - bitmap =get_fgraph_index_bitmap (current, offset); -for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { + bitmap =get_bitmap_bits (current, offset); + +for_each_set_bit (i, &bitmap,sizeof (bitmap) *BITS_PER_BYTE ) {struct fgraph_ops *gops = fgraph_array[i]; -if (!(bitmap &BIT (i))) -continue ;if (gops == &fgraph_stub)continue ;@@ -879,9 +919,10 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int i = task->curr_ret_stack; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;while (i >0 ) {@@ -891,14 +932,13 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,/* * For the tail-call, there would be 2 or more ftrace_ret_stacks on * the ret_stack, which records "return_to_handler" as the return -* address excpt for the last one. +* address except for the last one. * But on the real stack, there should be 1 entry because tail-call * reuses the return address on the stack and jump to the next function. * Thus we will continue to find real return address. */ if (ret_stack->retp == retp && - ret_stack->ret != - (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) + ret_stack->ret != return_handler)return ret_stack->ret; }@@ -909,10 +949,11 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret, unsigned long *retp) { struct ftrace_ret_stack *ret_stack; +unsigned long return_handler = (unsigned long )dereference_kernel_function_descriptor (return_to_handler);int offset = task->curr_ret_stack;int i; -if (ret != (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret != return_handler)return ret;if (!idx)@@ -921,8 +962,7 @@ unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, i = *idx;do { ret_stack =get_ret_stack (task, offset, &offset); -if (ret_stack && ret_stack->ret == - (unsigned long )dereference_kernel_function_descriptor (return_to_handler)) +if (ret_stack && ret_stack->ret == return_handler)continue ; i--; }while (i >=0 && ret_stack);
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRe: [PATCH 04/20] function_graph: Allow multiple users to attach to function graph
In reply to Masami Hiramatsu (Google)Quoted 10 lines · On Mon, 27 May 2024 09:34:36 +0900 Masami Hiramatsu (Google)…
kernel/trace/fgraph.c void ftrace_graph_stop(void) · line 257
Quoted 7 lines
kernel/trace/fgraph.c +2
-1@@ -110,11 +253,13 @@ void ftrace_graph_stop(void)/* Add a function return address to the trace stack on thread info.*/ static int ftrace_push_return_trace (unsigned long ret,unsigned long func, -unsigned long frame_pointer,unsigned long *retp) +unsigned long frame_pointer,unsigned long *retp, +int fgraph_idx)We do not need this
fgraph_idxparameter anymore because this removed reuse-frame check.
Agreed. Will remove.
Quoted 125 lines
Quoted 112 lines
kernel/trace/fgraph.c +54
-10{struct ftrace_ret_stack *ret_stack;unsigned long long calltime; -int index; +unsigned long val; +int offset;if (unlikely (ftrace_graph_is_dead ()))return -EBUSY ;@@ -124,24 +269,57 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,BUILD_BUG_ON (SHADOW_STACK_SIZE %sizeof (long )); +/* Set val to "reserved" with the delta to the new fgraph frame */ + val = (FGRAPH_TYPE_RESERVED <<FGRAPH_TYPE_SHIFT ) |FGRAPH_FRAME_OFFSET ; +/* * We must make sure the ret_stack is tested before we read * anything else. */ smp_rmb (); -/* The return trace stack is full */ -if (current->curr_ret_stack >=SHADOW_STACK_MAX_INDEX ) { +/* +* Check if there's room on the shadow stack to fit a fraph frame +* and a bitmap word. +*/ +if (current->curr_ret_stack +FGRAPH_FRAME_OFFSET +1 >=SHADOW_STACK_MAX_OFFSET ) {atomic_inc (¤t->trace_overrun);return -EBUSY ; } calltime =trace_clock_local (); - index = current->curr_ret_stack; -RET_STACK_INC (current->curr_ret_stack); - ret_stack =RET_STACK (current, index); + offset =READ_ONCE (current->curr_ret_stack); + ret_stack =RET_STACK (current, offset); + offset +=FGRAPH_FRAME_OFFSET ; + +/* ret offset = FGRAPH_FRAME_OFFSET ; type = reserved */ + current->ret_stack[offset] = val; + ret_stack->ret = ret; +/* +* The unwinders expect curr_ret_stack to point to either zero +* or an offset where to find the next ret_stack. Even though the +* ret stack might be bogus, we want to write the ret and the +* offset to find the ret_stack before we increment the stack point. +* If an interrupt comes in now before we increment the curr_ret_stack +* it may blow away what we wrote. But that's fine, because the +* offset will still be correct (even though the 'ret' won't be). +* What we worry about is the offset being correct after we increment +* the curr_ret_stack and before we update that offset, as if an +* interrupt comes in and does an unwind stack dump, it will need +* at least a correct offset! +*/ barrier (); +WRITE_ONCE (current->curr_ret_stack, offset +1 ); +/* +* This next barrier is to ensure that an interrupt coming in +* will not corrupt what we are about to write. +*/ +barrier (); + +/* Still keep it reserved even if an interrupt came in */ + current->ret_stack[offset] = val; + ret_stack->ret = ret; ret_stack->func = func; ret_stack->calltime = calltime;@@ -151,7 +329,7 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR ret_stack->retp = retp;#endif -return 0 ; +return offset; }/* @@ -168,49 +346,67 @@ ftrace_push_return_trace(unsigned long ret, unsigned long func,# define MCOUNT_INSN_SIZE 0#endif +/* If the caller does not use ftrace, call this function. */ int function_graph_enter (unsigned long ret,unsigned long func,unsigned long frame_pointer,unsigned long *retp) {struct ftrace_graph_ent trace; +unsigned long bitmap =0 ; +int offset; +int i; trace.func = func; trace.depth = ++current->curr_ret_depth; -if (ftrace_push_return_trace (ret, func, frame_pointer, retp)) + offset =ftrace_push_return_trace (ret, func, frame_pointer, retp,0 ); +if (offset <0 ) goto out; -/* Only trace if the calling function expects to */ -if (!fgraph_array[0 ]->entryfunc (&trace)) +for (i =0 ; i < fgraph_array_cnt; i++) { +struct fgraph_ops *gops = fgraph_array[i]; + +if (gops == &fgraph_stub) +continue ; + +if (gops->entryfunc (&trace))kernel/trace/fgraph.c ftrace_push_return_trace(unsigned long ret, unsigned long func, · line 381
Quoted 10 lines
kernel/trace/fgraph.c +8
-0+ bitmap |=BIT (i); + } + +if (!bitmap) goto out_ret; +/* +* Since this function uses fgraph_idx = 0 as a tail-call checking +* flag, set that bit always. +*/ This comment is also out-of-date.
Quoted 3 lines
kernel/trace/fgraph.c ftrace_push_return_trace(unsigned long ret, unsigned long func, · line 382
Quoted 1 line
kernel/trace/fgraph.c +1
-0+set_bitmap (current, offset, bitmap |BIT (0 ));And we do not need to set BIT(0) anymore.
Right. When looking at your first comment, I did a search for fgraph_idx and noticed this code, and realized it should be removed too. And of course, you noticed it too ;-)
Quoted 106 lines
Quoted 106 lines
kernel/trace/fgraph.c +34
-16+return 0 ; out_ret: -RET_STACK_DEC (current->curr_ret_stack); + current->curr_ret_stack -=FGRAPH_FRAME_OFFSET +1 ; out: current->curr_ret_depth--;return -EBUSY ; }/* Retrieve a function return address to the trace stack on thread info.*/ -static void +static struct ftrace_ret_stack *ftrace_pop_return_trace (struct ftrace_graph_ret *trace,unsigned long *ret, -unsigned long frame_pointer) +unsigned long frame_pointer,int *offset) {struct ftrace_ret_stack *ret_stack; -int index; - index = current->curr_ret_stack; -RET_STACK_DEC (index); + ret_stack =get_ret_stack (current, current->curr_ret_stack, offset); -if (unlikely (index <0 || index >SHADOW_STACK_MAX_INDEX )) { +if (unlikely (!ret_stack)) {ftrace_graph_stop (); -WARN_ON (1 ); +WARN (1 ,"Bad function graph ret_stack pointer: %d" , + current->curr_ret_stack);/* Might as well panic, otherwise we have no where to go */ *ret = (unsigned long )panic; -return ; +return NULL ; } - ret_stack =RET_STACK (current, index);#ifdef HAVE_FUNCTION_GRAPH_FP_TEST /* * The arch may choose to record the frame pointer used @@ -230,26 +426,29 @@ ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,ftrace_graph_stop ();WARN (1 ,"Bad frame pointer: expected %lx, received %lx\n" " from func %ps return to %lx\n" , - current->ret_stack[index].fp, + ret_stack->fp, frame_pointer, (void *)ret_stack->func, ret_stack->ret); *ret = (unsigned long )panic; - return; + return NULL; }#endif + *offset +=FGRAPH_FRAME_OFFSET ; *ret = ret_stack->ret; trace->func = ret_stack->func; trace->calltime = ret_stack->calltime; trace->overrun =atomic_read (¤t->trace_overrun); - trace->depth = current->curr_ret_depth--; + trace->depth = current->curr_ret_depth;/* * We still want to trace interrupts coming in if * max_depth is set to 1. Make sure the decrement is * seen before ftrace_graph_return. */ barrier (); + +return ret_stack; }/* @@ -287,30 +486,47 @@ struct fgraph_ret_regs;static unsigned long __ftrace_return_to_handler (struct fgraph_ret_regs *ret_regs,unsigned long frame_pointer) { +struct ftrace_ret_stack *ret_stack;struct ftrace_graph_ret trace; +unsigned long bitmap;unsigned long ret; +int offset; +int i; + + ret_stack =ftrace_pop_return_trace (&trace, &ret, frame_pointer, &offset); + +if (unlikely (!ret_stack)) { +ftrace_graph_stop (); +WARN_ON (1 ); +/* Might as well panic. What else to do? */ +return (unsigned long )panic; + } -ftrace_pop_return_trace (&trace, &ret, frame_pointer); + trace.rettime =trace_clock_local ();#ifdef CONFIG_FUNCTION_GRAPH_RETVAL trace.retval =fgraph_ret_regs_return_value (ret_regs);#endif - trace.rettime =trace_clock_local (); - fgraph_array[0 ]->retfunc (&trace); + + bitmap =get_bitmap_bits (current, offset); +for (i =0 ; i <FGRAPH_ARRAY_SIZE ; i++) { +struct fgraph_ops *gops = fgraph_array[i]; +
Quoted 8 lines
kernel/trace/fgraph.c struct fgraph_ret_regs; · line 516
Quoted 3 lines
kernel/trace/fgraph.c +3
-0+if (!(bitmap &BIT (i))) +continue ; +if (gops == &fgraph_stub)nit: here, we can make this check
unlikely()because the above bitmap check already filtered. (Some sleepable functions leave the return frame on shadow stack after gops is unregistered. But it also rare compared with living time.)
Sure.
Thanks for the review.
-- Steve
Steven Rostedt
maintainerQuoted 6 lines · On Mon, 27 May 2024 09:37:47 +0900 Masami Hiramatsu (Google)…
Quoted 1 line
The diff between this and Masami's last update is at the end of this email.
Thanks for update the patches!
I think your changes are good. I just have some comments and replied. So, the plan is to push this series in the tracing/for-next? I will port my fprobe part on it and run some tests.
Yeah. I'll probably push it after -rc2 comes out and base it on top of that. I usually wait till rc2 as it tends to be more stable than rc1.
I'll send an updated series later this week that addresses your comments.
-- Steve
Steven Rostedt
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
In reply to Steven RostedtQuoted 9 lines · On Fri, 24 May 2024 22:37:02 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow for instances to have their own
ftrace_opspart of thefgraph_opsthat makes thefuntion_graphtracer filter on theset_ftrace_filterfile of the instance and not the top instance.Note that this also requires to update
ftrace_graph_func()to call newfunction_graph_enter_ops()instead offunction_graph_enter()so that it avoid pushing on shadow stack multiple times on the same function.
So I found a major design flaw in this patch.
Quoted 24 lines
Co-developed with Masami Hiramatsu:
Signed-off-by Steven Rostedt (VMware) <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>---
arch/x86/kernel/ftrace.c +6
-1@@ -648,9 +648,24 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { struct pt_regs *regs = &fregs->regs; -unsigned long *stack = (unsigned long *)kernel_stack_pointer (regs); +unsigned long *parent = (unsigned long *)kernel_stack_pointer (regs); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ;
arch/x86/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 665
Quoted 10 lines
arch/x86/kernel/ftrace.c +8
-1+ +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ; -prepare_ftrace_return (ip, (unsigned long *)stack,0 ); + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip,0 , parent, gops))
So each registered graph ops has its own ftrace_ops which gets registered with ftrace, so this function does get called in a loop (by the ftrace iterator function). This means that we would need that code to detect the function_graph_enter_ops() getting called multiple times for the same function. This means each fgraph_ops gits its own retstack on the shadow stack.
I find this a waste of shadow stack resources, and also complicates the code with having to deal with tail calls and all that.
BUT! There's good news! I also thought about another way of handling this. I have something working, but requires a bit of rewriting the code. I should have something out in a day or two.
-- Steve
Quoted 6 lines
arch/x86/kernel/ftrace.c +3
-0+ *parent = (unsigned long )&return_to_handler; + + ftrace_test_recursion_unlock(bit); }#endif
Masami Hiramatsu (Google)
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
Quoted 43 lines · On Thu, 30 May 2024 22:30:57 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Fri, 24 May 2024 22:37:02 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow for instances to have their own
ftrace_opspart of thefgraph_opsthat makes thefuntion_graphtracer filter on theset_ftrace_filterfile of the instance and not the top instance.Note that this also requires to update
ftrace_graph_func()to call newfunction_graph_enter_ops()instead offunction_graph_enter()so that it avoid pushing on shadow stack multiple times on the same function.So I found a major design flaw in this patch.
Quoted 28 lines
Co-developed with Masami Hiramatsu:
Signed-off-by Steven Rostedt (VMware) <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>---
arch/x86/kernel/ftrace.c +9
-1@@ -648,9 +648,24 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { struct pt_regs *regs = &fregs->regs; -unsigned long *stack = (unsigned long *)kernel_stack_pointer (regs); +unsigned long *parent = (unsigned long *)kernel_stack_pointer (regs); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ;
Quoted 13 lines
arch/x86/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 665
Quoted 6 lines
arch/x86/kernel/ftrace.c +5
-1-prepare_ftrace_return (ip, (unsigned long *)stack,0 ); + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip,0 , parent, gops))So each registered graph ops has its own
ftrace_opswhich gets registered with ftrace, so this function does get called in a loop (by the ftrace iterator function). This means that we would need that code to detect thefunction_graph_enter_ops()getting called multiple times for the same function. This means eachfgraph_opsgits its own retstack on the shadow stack.
Ah, that is my concern and the reason why I added bitmap and stack reuse code in the ftrace_push_return_trace().
Quoted 7 lines
I find this a waste of shadow stack resources, and also complicates the code with having to deal with tail calls and all that.
BUT! There's good news! I also thought about another way of handling this. I have something working, but requires a bit of rewriting the code. I should have something out in a day or two.
Hmm, I just wonder why you don't reocver my bitmap check and stack reusing code. Are there any problem on it? (Too complicated?)
Thanks,
Quoted 11 lines
-- Steve
Quoted 6 lines
arch/x86/kernel/ftrace.c +3
-0+ *parent = (unsigned long )&return_to_handler; + + ftrace_test_recursion_unlock(bit); }#endif
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
Quoted 67 lines · On Fri, 31 May 2024 12:12:41 +0900 Masami Hiramatsu (Google)…
Quoted 43 lines · On Thu, 30 May 2024 22:30:57 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Fri, 24 May 2024 22:37:02 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow for instances to have their own
ftrace_opspart of thefgraph_opsthat makes thefuntion_graphtracer filter on theset_ftrace_filterfile of the instance and not the top instance.Note that this also requires to update
ftrace_graph_func()to call newfunction_graph_enter_ops()instead offunction_graph_enter()so that it avoid pushing on shadow stack multiple times on the same function.So I found a major design flaw in this patch.
Quoted 28 lines
Co-developed with Masami Hiramatsu:
Signed-off-by Steven Rostedt (VMware) <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>---
arch/x86/kernel/ftrace.c +9
-1@@ -648,9 +648,24 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { struct pt_regs *regs = &fregs->regs; -unsigned long *stack = (unsigned long *)kernel_stack_pointer (regs); +unsigned long *parent = (unsigned long *)kernel_stack_pointer (regs); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ;Quoted 13 lines
arch/x86/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 665
Quoted 6 lines
arch/x86/kernel/ftrace.c +5
-1-prepare_ftrace_return (ip, (unsigned long *)stack,0 ); + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip,0 , parent, gops))So each registered graph ops has its own
ftrace_opswhich gets registered with ftrace, so this function does get called in a loop (by the ftrace iterator function). This means that we would need that code to detect thefunction_graph_enter_ops()getting called multiple times for the same function. This means eachfgraph_opsgits its own retstack on the shadow stack.Ah, that is my concern and the reason why I added bitmap and stack reuse code in the
ftrace_push_return_trace().Quoted 4 lines
I find this a waste of shadow stack resources, and also complicates the code with having to deal with tail calls and all that.
Quoted 7 lines
Quoted 3 lines
BUT! There's good news! I also thought about another way of handling this. I have something working, but requires a bit of rewriting the code. I should have something out in a day or two.
Hmm, I just wonder why you don't reocver my bitmap check and stack reusing code. Are there any problem on it? (Too complicated?)
I actually dislike the use of ftrace itself to do the loop. I rather have fgraph be in control of it.
I've come up with a new "subops" assignment, where you can have one ftrace_ops represent multiple sub ftrace_ops. Basically, each fgraph ops can register its own ftrace_ops under a single graph_ops ftrace_ops. The graph_ops will be used to decide what functions call the callback, and then the callback does the multiplexing.
This removes the need to touch the architecture code. It can also be used by fprobes to handle the attachments to functions for several different sets of callbacks.
I'll send out patches soon.
-- Steve
Masami Hiramatsu (Google)
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
Quoted 74 lines · On Fri, 31 May 2024 02:03:46 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 71 lines · On Fri, 31 May 2024 12:12:41 +0900 Masami Hiramatsu (Google)…
Quoted 43 lines · On Thu, 30 May 2024 22:30:57 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Fri, 24 May 2024 22:37:02 -0400 Steven Rostedt <[email protected]> wrote:
From: "Steven Rostedt (VMware)" <[email protected]>
Allow for instances to have their own
ftrace_opspart of thefgraph_opsthat makes thefuntion_graphtracer filter on theset_ftrace_filterfile of the instance and not the top instance.Note that this also requires to update
ftrace_graph_func()to call newfunction_graph_enter_ops()instead offunction_graph_enter()so that it avoid pushing on shadow stack multiple times on the same function.So I found a major design flaw in this patch.
Quoted 28 lines
Co-developed with Masami Hiramatsu:
Signed-off-by Steven Rostedt (VMware) <[email protected]>Signed-off-by Masami Hiramatsu (Google) <[email protected]>Signed-off-by Steven Rostedt (Google) <[email protected]>---
arch/x86/kernel/ftrace.c +9
-1@@ -648,9 +648,24 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op, struct ftrace_regs *fregs) { struct pt_regs *regs = &fregs->regs; -unsigned long *stack = (unsigned long *)kernel_stack_pointer (regs); +unsigned long *parent = (unsigned long *)kernel_stack_pointer (regs); + struct fgraph_ops *gops = container_of(op, struct fgraph_ops, ops); +int bit; + +if (unlikely (ftrace_graph_is_dead ())) +return ; + +if (unlikely (atomic_read (¤t->tracing_graph_pause))) +return ;Quoted 13 lines
arch/x86/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 665
Quoted 6 lines
arch/x86/kernel/ftrace.c +5
-1-prepare_ftrace_return (ip, (unsigned long *)stack,0 ); + bit =ftrace_test_recursion_trylock (ip, *parent); +if (bit <0 ) +return ; + +if (!function_graph_enter_ops (*parent, ip,0 , parent, gops))So each registered graph ops has its own
ftrace_opswhich gets registered with ftrace, so this function does get called in a loop (by the ftrace iterator function). This means that we would need that code to detect thefunction_graph_enter_ops()getting called multiple times for the same function. This means eachfgraph_opsgits its own retstack on the shadow stack.Ah, that is my concern and the reason why I added bitmap and stack reuse code in the
ftrace_push_return_trace().Quoted 7 lines
I find this a waste of shadow stack resources, and also complicates the code with having to deal with tail calls and all that.
BUT! There's good news! I also thought about another way of handling this. I have something working, but requires a bit of rewriting the code. I should have something out in a day or two.
Quoted 6 lines
Quoted 3 lines
Hmm, I just wonder why you don't reocver my bitmap check and stack reusing code. Are there any problem on it? (Too complicated?)
I actually dislike the use of ftrace itself to do the loop. I rather have fgraph be in control of it.
(actually, I agreed with you, looping in ftrace may cause trouble)
Quoted 6 lines
I've come up with a new "subops" assignment, where you can have one
ftrace_opsrepresent multiple subftrace_ops. Basically, each fgraph ops can register its ownftrace_opsunder a singlegraph_opsftrace_ops. Thegraph_opswill be used to decide what functions call the callback, and then the callback does the multiplexing.
So is it similar to the fprobe/kprobe, use shared signle ftrace_ops, but keep each fgraph has own hash table?
Quoted 5 lines
This removes the need to touch the architecture code. It can also be used by fprobes to handle the attachments to functions for several different sets of callbacks.
I'll send out patches soon.
OK, I'll wait for that.
Thank you!
Quoted 3 lines
-- Steve
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Steven Rostedt
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
Quoted 2 lines · On Fri, 31 May 2024 23:50:23 +0900 Masami Hiramatsu (Google)…
So is it similar to the fprobe/kprobe, use shared signle
ftrace_ops, but keep each fgraph has own hash table?
Sort of.
I created helper functions in ftrace that lets you have a "manager ftrace_ops" that will be used to assign to ftrace (with the function that will demultiplex), and then you can have "subops" that can be assigned that is per user. Here's a glimpse of the code:
/**
* ftrace_startup_subops - enable tracing for subops of an ops
* @ops: Manager ops (used to pick all the functions of its subops)
* @subops: A new ops to add to @ops
* @command: Extra commands to use to enable tracing
*
* The @ops is a manager @ops that has the filter that includes all the functions
* that its list of subops are tracing. Adding a new @subops will add the
* functions of @subops to @ops.
*/
int ftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command)
{
struct ftrace_hash *filter_hash;
struct ftrace_hash *notrace_hash;
struct ftrace_hash *save_filter_hash;
struct ftrace_hash *save_notrace_hash;
int size_bits;
int ret;
if (unlikely(ftrace_disabled))
return -ENODEV;
ftrace_ops_init(ops);
ftrace_ops_init(subops);
/* Make everything canonical (Just in case!) */
if (!ops->func_hash->filter_hash)
ops->func_hash->filter_hash = EMPTY_HASH;
if (!ops->func_hash->notrace_hash)
ops->func_hash->notrace_hash = EMPTY_HASH;
if (!subops->func_hash->filter_hash)
subops->func_hash->filter_hash = EMPTY_HASH;
if (!subops->func_hash->notrace_hash)
subops->func_hash->notrace_hash = EMPTY_HASH;
/* For the first subops to ops just enable it normally */
if (list_empty(&ops->subop_list)) {
/* Just use the subops hashes */
filter_hash = copy_hash(subops->func_hash->filter_hash);
notrace_hash = copy_hash(subops->func_hash->notrace_hash);
if (!filter_hash || !notrace_hash) {
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
return -ENOMEM;
}
save_filter_hash = ops->func_hash->filter_hash;
save_notrace_hash = ops->func_hash->notrace_hash;
ops->func_hash->filter_hash = filter_hash;
ops->func_hash->notrace_hash = notrace_hash;
list_add(&subops->list, &ops->subop_list);
ret = ftrace_startup(ops, command);
if (ret < 0) {
list_del(&subops->list);
ops->func_hash->filter_hash = save_filter_hash;
ops->func_hash->notrace_hash = save_notrace_hash;
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
} else {
free_ftrace_hash(save_filter_hash);
free_ftrace_hash(save_notrace_hash);
subops->flags |= FTRACE_OPS_FL_ENABLED;
}
return ret;
}
/*
* Here there's already something attached. Here are the rules:
* o If either filter_hash is empty then the final stays empty
* o Otherwise, the final is a superset of both hashes
* o If either notrace_hash is empty then the final stays empty
* o Otherwise, the final is an intersection between the hashes
*/
if (ops->func_hash->filter_hash == EMPTY_HASH ||
subops->func_hash->filter_hash == EMPTY_HASH) {
filter_hash = EMPTY_HASH;
} else {
size_bits = max(ops->func_hash->filter_hash->size_bits,
subops->func_hash->filter_hash->size_bits);
filter_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->filter_hash);
if (!filter_hash)
return -ENOMEM;
ret = append_hash(&filter_hash, subops->func_hash->filter_hash);
if (ret < 0) {
free_ftrace_hash(filter_hash);
return ret;
}
}
if (ops->func_hash->notrace_hash == EMPTY_HASH ||
subops->func_hash->notrace_hash == EMPTY_HASH) {
notrace_hash = EMPTY_HASH;
} else {
size_bits = max(ops->func_hash->filter_hash->size_bits,
subops->func_hash->filter_hash->size_bits);
notrace_hash = alloc_ftrace_hash(size_bits);
if (!notrace_hash) {
free_ftrace_hash(filter_hash);
return -ENOMEM;
}
ret = intersect_hash(¬race_hash, ops->func_hash->filter_hash,
subops->func_hash->filter_hash);
if (ret < 0) {
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
return ret;
}
}
list_add(&subops->list, &ops->subop_list);
ret = ftrace_update_ops(ops, filter_hash, notrace_hash);
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
if (ret < 0)
list_del(&subops->list);
return ret;
}/**
* ftrace_shutdown_subops - Remove a subops from a manager ops
* @ops: A manager ops to remove @subops from
* @subops: The subops to remove from @ops
* @command: Any extra command flags to add to modifying the text
*
* Removes the functions being traced by the @subops from @ops. Note, it
* will not affect functions that are being traced by other subops that
* still exist in @ops.
*
* If the last subops is removed from @ops, then @ops is shutdown normally. */
int ftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command)
{
struct ftrace_hash *filter_hash;
struct ftrace_hash *notrace_hash;
int ret;
if (unlikely(ftrace_disabled))
return -ENODEV;
list_del(&subops->list);
if (list_empty(&ops->subop_list)) {
/* Last one, just disable the current ops */
ret = ftrace_shutdown(ops, command);
if (ret < 0) {
list_add(&subops->list, &ops->subop_list);
return ret;
}
subops->flags &= ~FTRACE_OPS_FL_ENABLED;
free_ftrace_hash(ops->func_hash->filter_hash);
free_ftrace_hash(ops->func_hash->notrace_hash);
ops->func_hash->filter_hash = EMPTY_HASH;
ops->func_hash->notrace_hash = EMPTY_HASH;
return 0;
}
/* Rebuild the hashes without subops */
filter_hash = append_hashes(ops);
notrace_hash = intersect_hashes(ops);
if (!filter_hash || !notrace_hash) {
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
list_add(&subops->list, &ops->subop_list);
return -ENOMEM;
}
ret = ftrace_update_ops(ops, filter_hash, notrace_hash);
if (ret < 0)
list_add(&subops->list, &ops->subop_list);
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
return ret;
}Quoted 8 lines
Quoted 6 lines
This removes the need to touch the architecture code. It can also be used by fprobes to handle the attachments to functions for several different sets of callbacks.
I'll send out patches soon.
OK, I'll wait for that.
I'm just cleaning it up. I'll post it tomorrow (your today).
-- Steve
Steven Rostedt
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
Quoted 1 line · On Fri, 31 May 2024 18:49:10 -0400 Steven Rostedt <[email protected]> wrote:
I'm just cleaning it up. I'll post it tomorrow (your today).
It's going to take a little longer. I found that the rewrite broke updating the filters while tracing is enabled. Working on fixing that now.
-- Steve
Masami Hiramatsu (Google)
maintainerRe: [PATCH 10/20] function_graph: Have the instances use their own ftrace_ops for filtering
In reply to Steven RostedtQuoted 42 lines · On Fri, 31 May 2024 18:49:10 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 2 lines · On Fri, 31 May 2024 23:50:23 +0900 Masami Hiramatsu (Google)…
So is it similar to the fprobe/kprobe, use shared signle
ftrace_ops, but keep each fgraph has own hash table?Sort of.
I created helper functions in ftrace that lets you have a "manager
ftrace_ops" that will be used to assign to ftrace (with the function that will demultiplex), and then you can have "subops" that can be assigned that is per user. Here's a glimpse of the code:/**
*ftrace_startup_subops- enable tracing for subops of an ops
* @ops: Manager ops (used to pick all the functions of its subops)
* @subops: A new ops to add to @ops
* @command: Extra commands to use to enable tracing
*
* The @ops is a manager @ops that has the filter that includes all the functions
* that its list of subops are tracing. Adding a new @subops will add the
* functions of @subops to @ops.
*/
intftrace_startup_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command){ struct ftrace_hash *filter_hash; struct ftrace_hash *notrace_hash; struct ftrace_hash *save_filter_hash; struct ftrace_hash *save_notrace_hash; int size_bits; int ret; if (unlikely(ftrace_disabled)) return -ENODEV; ftrace_ops_init(ops); ftrace_ops_init(subops); /* Make everything canonical (Just in case!) */ if (!ops->func_hash->filter_hash) ops->func_hash->filter_hash = EMPTY_HASH; if (!ops->func_hash->notrace_hash)
Quoted 8 lines
ops->func_hash->notrace_hash = EMPTY_HASH;
if (!subops->func_hash->filter_hash)
subops->func_hash->filter_hash = EMPTY_HASH;
if (!subops->func_hash->notrace_hash)
subops->func_hash->notrace_hash = EMPTY_HASH;
/* For the first subops to ops just enable it normally */
if (list_empty(&ops->subop_list)) {May above ftrace_ops_init() clear this list up always?
Quoted 28 lines
/* Just use the subops hashes */
filter_hash = copy_hash(subops->func_hash->filter_hash);
notrace_hash = copy_hash(subops->func_hash->notrace_hash);
if (!filter_hash || !notrace_hash) {
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
return -ENOMEM;
}
save_filter_hash = ops->func_hash->filter_hash;
save_notrace_hash = ops->func_hash->notrace_hash;
ops->func_hash->filter_hash = filter_hash;
ops->func_hash->notrace_hash = notrace_hash;
list_add(&subops->list, &ops->subop_list);
ret = ftrace_startup(ops, command);
if (ret < 0) {
list_del(&subops->list);
ops->func_hash->filter_hash = save_filter_hash;
ops->func_hash->notrace_hash = save_notrace_hash;
free_ftrace_hash(filter_hash);
free_ftrace_hash(notrace_hash);
} else {
free_ftrace_hash(save_filter_hash);
free_ftrace_hash(save_notrace_hash);
subops->flags |= FTRACE_OPS_FL_ENABLED;
}
return ret;Quoted 8 lines
} /* * Here there's already something attached. Here are the rules: * o If either filter_hash is empty then the final stays empty * o Otherwise, the final is a superset of both hashes * o If either notrace_hash is empty then the final stays empty * o Otherwise, the final is an intersection between the hashes
Yeah, filter_hash |= subops_filter_hash and notrace_hash &= subops_notrace_hash. The complicated point is filter's EMPTY_HASH means FULLSET_HASH. :)
Quoted 7 lines
*/
if (ops->func_hash->filter_hash == EMPTY_HASH ||
subops->func_hash->filter_hash == EMPTY_HASH) {
filter_hash = EMPTY_HASH;
} else {
size_bits = max(ops->func_hash->filter_hash->size_bits,
subops->func_hash->filter_hash->size_bits);Don't we need to expand the size_bits? In the worst case, both hash does not share any entry, then it should be expanded.
Quoted 93 lines
filter_hash = alloc_and_copy_ftrace_hash(size_bits, ops->func_hash->filter_hash); if (!filter_hash) return -ENOMEM; ret = append_hash(&filter_hash, subops->func_hash->filter_hash); if (ret < 0) { free_ftrace_hash(filter_hash); return ret; } } if (ops->func_hash->notrace_hash == EMPTY_HASH || subops->func_hash->notrace_hash == EMPTY_HASH) { notrace_hash = EMPTY_HASH; } else { size_bits = max(ops->func_hash->filter_hash->size_bits, subops->func_hash->filter_hash->size_bits); notrace_hash = alloc_ftrace_hash(size_bits); if (!notrace_hash) { free_ftrace_hash(filter_hash); return -ENOMEM; } ret = intersect_hash(¬race_hash, ops->func_hash->filter_hash, subops->func_hash->filter_hash); if (ret < 0) { free_ftrace_hash(filter_hash); free_ftrace_hash(notrace_hash); return ret; } } list_add(&subops->list, &ops->subop_list); ret = ftrace_update_ops(ops, filter_hash, notrace_hash); free_ftrace_hash(filter_hash); free_ftrace_hash(notrace_hash); if (ret < 0) list_del(&subops->list); return ret; }/**
*ftrace_shutdown_subops- Remove a subops from a manager ops
* @ops: A manager ops to remove @subops from
* @subops: The subops to remove from @ops
* @command: Any extra command flags to add to modifying the text
*
* Removes the functions being traced by the @subops from @ops. Note, it
* will not affect functions that are being traced by other subops that
* still exist in @ops.
*
* If the last subops is removed from @ops, then @ops is shutdown normally. */
intftrace_shutdown_subops(struct ftrace_ops *ops, struct ftrace_ops *subops, int command){ struct ftrace_hash *filter_hash; struct ftrace_hash *notrace_hash; int ret; if (unlikely(ftrace_disabled)) return -ENODEV; list_del(&subops->list); if (list_empty(&ops->subop_list)) { /* Last one, just disable the current ops */ ret = ftrace_shutdown(ops, command); if (ret < 0) { list_add(&subops->list, &ops->subop_list); return ret; } subops->flags &= ~FTRACE_OPS_FL_ENABLED; free_ftrace_hash(ops->func_hash->filter_hash); free_ftrace_hash(ops->func_hash->notrace_hash); ops->func_hash->filter_hash = EMPTY_HASH; ops->func_hash->notrace_hash = EMPTY_HASH; return 0; } /* Rebuild the hashes without subops */ filter_hash = append_hashes(ops); notrace_hash = intersect_hashes(ops); if (!filter_hash || !notrace_hash) { free_ftrace_hash(filter_hash); free_ftrace_hash(notrace_hash); list_add(&subops->list, &ops->subop_list); return -ENOMEM; }
Quoted 7 lines
ret = ftrace_update_ops(ops, filter_hash, notrace_hash); if (ret < 0) list_add(&subops->list, &ops->subop_list); free_ftrace_hash(filter_hash); free_ftrace_hash(notrace_hash); return ret; }
OK, so if the list_is_singlar(ops->subop_list), ftrace_graph_enter_ops() is called and if not, ftrace_graph_enter() is called, right?
Thank you,
Quoted 14 lines
Quoted 10 lines
Quoted 8 lines
This removes the need to touch the architecture code. It can also be used by fprobes to handle the attachments to functions for several different sets of callbacks.
I'll send out patches soon.
OK, I'll wait for that.
I'm just cleaning it up. I'll post it tomorrow (your today).
-- Steve
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>