1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Infrastructure to took into function calls and returns. 4 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com> 5 * Mostly borrowed from function tracer which 6 * is Copyright (c) Steven Rostedt <srostedt@redhat.com> 7 * 8 * Highly modified by Steven Rostedt (VMware). 9 */ 10 #include <linux/bits.h> 11 #include <linux/jump_label.h> 12 #include <linux/suspend.h> 13 #include <linux/ftrace.h> 14 #include <linux/static_call.h> 15 #include <linux/slab.h> 16 17 #include <trace/events/sched.h> 18 19 #include "ftrace_internal.h" 20 #include "trace.h" 21 22 /* 23 * FGRAPH_FRAME_SIZE: Size in bytes of the meta data on the shadow stack 24 * FGRAPH_FRAME_OFFSET: Size in long words of the meta data frame 25 */ 26 #define FGRAPH_FRAME_SIZE sizeof(struct ftrace_ret_stack) 27 #define FGRAPH_FRAME_OFFSET DIV_ROUND_UP(FGRAPH_FRAME_SIZE, sizeof(long)) 28 29 /* 30 * On entry to a function (via function_graph_enter()), a new fgraph frame 31 * (ftrace_ret_stack) is pushed onto the stack as well as a word that 32 * holds a bitmask and a type (called "bitmap"). The bitmap is defined as: 33 * 34 * bits: 0 - 9 offset in words from the previous ftrace_ret_stack 35 * 36 * bits: 10 - 11 Type of storage 37 * 0 - reserved 38 * 1 - bitmap of fgraph_array index 39 * 2 - reserved data 40 * 41 * For type with "bitmap of fgraph_array index" (FGRAPH_TYPE_BITMAP): 42 * bits: 12 - 27 The bitmap of fgraph_ops fgraph_array index 43 * That is, it's a bitmask of 0-15 (16 bits) 44 * where if a corresponding ops in the fgraph_array[] 45 * expects a callback from the return of the function 46 * it's corresponding bit will be set. 47 * 48 * 49 * The top of the ret_stack (when not empty) will always have a reference 50 * word that points to the last fgraph frame that was saved. 51 * 52 * For reserved data: 53 * bits: 12 - 17 The size in words that is stored 54 * bits: 18 - 23 The index of fgraph_array, which shows who is stored 55 * 56 * That is, at the end of function_graph_enter, if the first and forth 57 * fgraph_ops on the fgraph_array[] (index 0 and 3) needs their retfunc called 58 * on the return of the function being traced, and the forth fgraph_ops 59 * stored two words of data, this is what will be on the task's shadow 60 * ret_stack: (the stack grows upward) 61 * 62 * ret_stack[SHADOW_STACK_OFFSET] 63 * | SHADOW_STACK_TASK_VARS(ret_stack)[15] | 64 * ... 65 * | SHADOW_STACK_TASK_VARS(ret_stack)[0] | 66 * ret_stack[SHADOW_STACK_MAX_OFFSET] 67 * ... 68 * | | <- task->curr_ret_stack 69 * +--------------------------------------------+ 70 * | (3 << 12) | (3 << 10) | FGRAPH_FRAME_OFFSET| 71 * | *or put another way* | 72 * | (3 << FGRAPH_DATA_INDEX_SHIFT)| \ | This is for fgraph_ops[3]. 73 * | ((2 - 1) << FGRAPH_DATA_SHIFT)| \ | The data size is 2 words. 74 * | (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT)| \ | 75 * | (offset2:FGRAPH_FRAME_OFFSET+3) | <- the offset2 is from here 76 * +--------------------------------------------+ ( It is 4 words from the ret_stack) 77 * | STORED DATA WORD 2 | 78 * | STORED DATA WORD 1 | 79 * +--------------------------------------------+ 80 * | (9 << 12) | (1 << 10) | FGRAPH_FRAME_OFFSET| 81 * | *or put another way* | 82 * | (BIT(3)|BIT(0)) << FGRAPH_INDEX_SHIFT | \ | 83 * | FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT| \ | 84 * | (offset1:FGRAPH_FRAME_OFFSET) | <- the offset1 is from here 85 * +--------------------------------------------+ 86 * | struct ftrace_ret_stack | 87 * | (stores the saved ret pointer) | <- the offset points here 88 * +--------------------------------------------+ 89 * | (X) | (N) | ( N words away from 90 * | | previous ret_stack) 91 * ... 92 * ret_stack[0] 93 * 94 * If a backtrace is required, and the real return pointer needs to be 95 * fetched, then it looks at the task's curr_ret_stack offset, if it 96 * is greater than zero (reserved, or right before popped), it would mask 97 * the value by FGRAPH_FRAME_OFFSET_MASK to get the offset of the 98 * ftrace_ret_stack structure stored on the shadow stack. 99 */ 100 101 /* 102 * The following is for the top word on the stack: 103 * 104 * FGRAPH_FRAME_OFFSET (0-9) holds the offset delta to the fgraph frame 105 * FGRAPH_TYPE (10-11) holds the type of word this is. 106 * (RESERVED or BITMAP) 107 */ 108 #define FGRAPH_FRAME_OFFSET_BITS 10 109 #define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_BITS - 1, 0) 110 111 #define FGRAPH_TYPE_BITS 2 112 #define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_BITS - 1, 0) 113 #define FGRAPH_TYPE_SHIFT FGRAPH_FRAME_OFFSET_BITS 114 115 enum { 116 FGRAPH_TYPE_RESERVED = 0, 117 FGRAPH_TYPE_BITMAP = 1, 118 FGRAPH_TYPE_DATA = 2, 119 }; 120 121 /* 122 * For BITMAP type: 123 * FGRAPH_INDEX (12-27) bits holding the gops index wanting return callback called 124 */ 125 #define FGRAPH_INDEX_BITS 16 126 #define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_BITS - 1, 0) 127 #define FGRAPH_INDEX_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) 128 129 /* 130 * For DATA type: 131 * FGRAPH_DATA (12-17) bits hold the size of data (in words) 132 * FGRAPH_INDEX (18-23) bits hold the index for which gops->idx the data is for 133 * 134 * Note: 135 * data_size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words. 136 */ 137 #define FGRAPH_DATA_BITS 5 138 #define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_BITS - 1, 0) 139 #define FGRAPH_DATA_SHIFT (FGRAPH_TYPE_SHIFT + FGRAPH_TYPE_BITS) 140 #define FGRAPH_MAX_DATA_SIZE (sizeof(long) * (1 << FGRAPH_DATA_BITS)) 141 142 #define FGRAPH_DATA_INDEX_BITS 4 143 #define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_BITS - 1, 0) 144 #define FGRAPH_DATA_INDEX_SHIFT (FGRAPH_DATA_SHIFT + FGRAPH_DATA_BITS) 145 146 #define FGRAPH_ARRAY_SIZE FGRAPH_INDEX_BITS 147 148 /* 149 * SHADOW_STACK_SIZE: The size in bytes of the entire shadow stack 150 * SHADOW_STACK_OFFSET: The size in long words of the shadow stack 151 * SHADOW_STACK_MAX_OFFSET: The max offset of the stack for a new frame to be added 152 */ 153 #define SHADOW_STACK_SIZE (4096) 154 #define SHADOW_STACK_OFFSET (SHADOW_STACK_SIZE / sizeof(long)) 155 /* Leave on a buffer at the end */ 156 #define SHADOW_STACK_MAX_OFFSET \ 157 (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE)) 158 159 /* RET_STACK(): Return the frame from a given @offset from task @t */ 160 #define RET_STACK(t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset])) 161 162 /* 163 * Each fgraph_ops has a reserved unsigned long at the end (top) of the 164 * ret_stack to store task specific state. 165 */ 166 #define SHADOW_STACK_TASK_VARS(ret_stack) \ 167 ((unsigned long *)(&(ret_stack)[SHADOW_STACK_OFFSET - FGRAPH_ARRAY_SIZE])) 168 169 DEFINE_STATIC_KEY_FALSE(kill_ftrace_graph); 170 int ftrace_graph_active; 171 172 static struct kmem_cache *fgraph_stack_cachep; 173 174 static struct fgraph_ops *fgraph_array[FGRAPH_ARRAY_SIZE]; 175 static unsigned long fgraph_array_bitmask; 176 177 /* LRU index table for fgraph_array */ 178 static int fgraph_lru_table[FGRAPH_ARRAY_SIZE]; 179 static int fgraph_lru_next; 180 static int fgraph_lru_last; 181 182 /* Initialize fgraph_lru_table with unused index */ 183 static void fgraph_lru_init(void) 184 { 185 int i; 186 187 for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) 188 fgraph_lru_table[i] = i; 189 } 190 191 /* Release the used index to the LRU table */ 192 static int fgraph_lru_release_index(int idx) 193 { 194 if (idx < 0 || idx >= FGRAPH_ARRAY_SIZE || 195 WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1)) 196 return -1; 197 198 fgraph_lru_table[fgraph_lru_last] = idx; 199 fgraph_lru_last = (fgraph_lru_last + 1) % FGRAPH_ARRAY_SIZE; 200 201 clear_bit(idx, &fgraph_array_bitmask); 202 return 0; 203 } 204 205 /* Allocate a new index from LRU table */ 206 static int fgraph_lru_alloc_index(void) 207 { 208 int idx = fgraph_lru_table[fgraph_lru_next]; 209 210 /* No id is available */ 211 if (idx == -1) 212 return -1; 213 214 fgraph_lru_table[fgraph_lru_next] = -1; 215 fgraph_lru_next = (fgraph_lru_next + 1) % FGRAPH_ARRAY_SIZE; 216 217 set_bit(idx, &fgraph_array_bitmask); 218 return idx; 219 } 220 221 /* Get the offset to the fgraph frame from a ret_stack value */ 222 static inline int __get_offset(unsigned long val) 223 { 224 return val & FGRAPH_FRAME_OFFSET_MASK; 225 } 226 227 /* Get the type of word from a ret_stack value */ 228 static inline int __get_type(unsigned long val) 229 { 230 return (val >> FGRAPH_TYPE_SHIFT) & FGRAPH_TYPE_MASK; 231 } 232 233 /* Get the data_index for a DATA type ret_stack word */ 234 static inline int __get_data_index(unsigned long val) 235 { 236 return (val >> FGRAPH_DATA_INDEX_SHIFT) & FGRAPH_DATA_INDEX_MASK; 237 } 238 239 /* Get the data_size for a DATA type ret_stack word */ 240 static inline int __get_data_size(unsigned long val) 241 { 242 return ((val >> FGRAPH_DATA_SHIFT) & FGRAPH_DATA_MASK) + 1; 243 } 244 245 /* Get the word from the ret_stack at @offset */ 246 static inline unsigned long get_fgraph_entry(struct task_struct *t, int offset) 247 { 248 return t->ret_stack[offset]; 249 } 250 251 /* Get the FRAME_OFFSET from the word from the @offset on ret_stack */ 252 static inline int get_frame_offset(struct task_struct *t, int offset) 253 { 254 return __get_offset(t->ret_stack[offset]); 255 } 256 257 /* For BITMAP type: get the bitmask from the @offset at ret_stack */ 258 static inline unsigned long 259 get_bitmap_bits(struct task_struct *t, int offset) 260 { 261 return (t->ret_stack[offset] >> FGRAPH_INDEX_SHIFT) & FGRAPH_INDEX_MASK; 262 } 263 264 /* Write the bitmap to the ret_stack at @offset (does index, offset and bitmask) */ 265 static inline void 266 set_bitmap(struct task_struct *t, int offset, unsigned long bitmap) 267 { 268 t->ret_stack[offset] = (bitmap << FGRAPH_INDEX_SHIFT) | 269 (FGRAPH_TYPE_BITMAP << FGRAPH_TYPE_SHIFT) | FGRAPH_FRAME_OFFSET; 270 } 271 272 /* For DATA type: get the data saved under the ret_stack word at @offset */ 273 static inline void *get_data_type_data(struct task_struct *t, int offset) 274 { 275 unsigned long val = t->ret_stack[offset]; 276 277 if (__get_type(val) != FGRAPH_TYPE_DATA) 278 return NULL; 279 offset -= __get_data_size(val); 280 return (void *)&t->ret_stack[offset]; 281 } 282 283 /* Create the ret_stack word for a DATA type */ 284 static inline unsigned long make_data_type_val(int idx, int size, int offset) 285 { 286 return (idx << FGRAPH_DATA_INDEX_SHIFT) | 287 ((size - 1) << FGRAPH_DATA_SHIFT) | 288 (FGRAPH_TYPE_DATA << FGRAPH_TYPE_SHIFT) | offset; 289 } 290 291 /* ftrace_graph_entry set to this to tell some archs to run function graph */ 292 static int entry_run(struct ftrace_graph_ent *trace, struct fgraph_ops *ops, 293 struct ftrace_regs *fregs) 294 { 295 return 0; 296 } 297 298 /* ftrace_graph_return set to this to tell some archs to run function graph */ 299 static void return_run(struct ftrace_graph_ret *trace, struct fgraph_ops *ops, 300 struct ftrace_regs *fregs) 301 { 302 } 303 304 static void ret_stack_set_task_var(struct task_struct *t, int idx, long val) 305 { 306 unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); 307 308 gvals[idx] = val; 309 } 310 311 static unsigned long * 312 ret_stack_get_task_var(struct task_struct *t, int idx) 313 { 314 unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); 315 316 return &gvals[idx]; 317 } 318 319 static void ret_stack_init_task_vars(unsigned long *ret_stack) 320 { 321 unsigned long *gvals = SHADOW_STACK_TASK_VARS(ret_stack); 322 323 memset(gvals, 0, sizeof(*gvals) * FGRAPH_ARRAY_SIZE); 324 } 325 326 /** 327 * fgraph_reserve_data - Reserve storage on the task's ret_stack 328 * @idx: The index of fgraph_array 329 * @size_bytes: The size in bytes to reserve 330 * 331 * Reserves space of up to FGRAPH_MAX_DATA_SIZE bytes on the 332 * task's ret_stack shadow stack, for a given fgraph_ops during 333 * the entryfunc() call. If entryfunc() returns zero, the storage 334 * is discarded. An entryfunc() can only call this once per iteration. 335 * The fgraph_ops retfunc() can retrieve this stored data with 336 * fgraph_retrieve_data(). 337 * 338 * Returns: On success, a pointer to the data on the stack. 339 * Otherwise, NULL if there's not enough space left on the 340 * ret_stack for the data, or if fgraph_reserve_data() was called 341 * more than once for a single entryfunc() call. 342 */ 343 void *fgraph_reserve_data(int idx, int size_bytes) 344 { 345 unsigned long val; 346 void *data; 347 int curr_ret_stack = current->curr_ret_stack; 348 int data_size; 349 350 if (size_bytes > FGRAPH_MAX_DATA_SIZE) 351 return NULL; 352 353 /* Convert the data size to number of longs. */ 354 data_size = (size_bytes + sizeof(long) - 1) >> (sizeof(long) == 4 ? 2 : 3); 355 356 val = get_fgraph_entry(current, curr_ret_stack - 1); 357 data = ¤t->ret_stack[curr_ret_stack]; 358 359 curr_ret_stack += data_size + 1; 360 if (unlikely(curr_ret_stack >= SHADOW_STACK_MAX_OFFSET)) 361 return NULL; 362 363 val = make_data_type_val(idx, data_size, __get_offset(val) + data_size + 1); 364 365 /* Set the last word to be reserved */ 366 current->ret_stack[curr_ret_stack - 1] = val; 367 368 /* Make sure interrupts see this */ 369 barrier(); 370 current->curr_ret_stack = curr_ret_stack; 371 /* Again sync with interrupts, and reset reserve */ 372 current->ret_stack[curr_ret_stack - 1] = val; 373 374 return data; 375 } 376 377 /** 378 * fgraph_retrieve_data - Retrieve stored data from fgraph_reserve_data() 379 * @idx: the index of fgraph_array (fgraph_ops::idx) 380 * @size_bytes: pointer to retrieved data size. 381 * 382 * This is to be called by a fgraph_ops retfunc(), to retrieve data that 383 * was stored by the fgraph_ops entryfunc() on the function entry. 384 * That is, this will retrieve the data that was reserved on the 385 * entry of the function that corresponds to the exit of the function 386 * that the fgraph_ops retfunc() is called on. 387 * 388 * Returns: The stored data from fgraph_reserve_data() called by the 389 * matching entryfunc() for the retfunc() this is called from. 390 * Or NULL if there was nothing stored. 391 */ 392 void *fgraph_retrieve_data(int idx, int *size_bytes) 393 { 394 return fgraph_retrieve_parent_data(idx, size_bytes, 0); 395 } 396 397 /** 398 * fgraph_get_task_var - retrieve a task specific state variable 399 * @gops: The ftrace_ops that owns the task specific variable 400 * 401 * Every registered fgraph_ops has a task state variable 402 * reserved on the task's ret_stack. This function returns the 403 * address to that variable. 404 * 405 * Returns the address to the fgraph_ops @gops tasks specific 406 * unsigned long variable. 407 */ 408 unsigned long *fgraph_get_task_var(struct fgraph_ops *gops) 409 { 410 return ret_stack_get_task_var(current, gops->idx); 411 } 412 413 /* 414 * @offset: The offset into @t->ret_stack to find the ret_stack entry 415 * @frame_offset: Where to place the offset into @t->ret_stack of that entry 416 * 417 * Returns a pointer to the previous ret_stack below @offset or NULL 418 * when it reaches the bottom of the stack. 419 * 420 * Calling this with: 421 * 422 * offset = task->curr_ret_stack; 423 * do { 424 * ret_stack = get_ret_stack(task, offset, &offset); 425 * } while (ret_stack); 426 * 427 * Will iterate through all the ret_stack entries from curr_ret_stack 428 * down to the first one. 429 */ 430 static inline struct ftrace_ret_stack * 431 get_ret_stack(struct task_struct *t, int offset, int *frame_offset) 432 { 433 int offs; 434 435 BUILD_BUG_ON(FGRAPH_FRAME_SIZE % sizeof(long)); 436 437 if (unlikely(offset <= 0)) 438 return NULL; 439 440 offs = get_frame_offset(t, --offset); 441 if (WARN_ON_ONCE(offs <= 0 || offs > offset)) 442 return NULL; 443 444 offset -= offs; 445 446 *frame_offset = offset; 447 return RET_STACK(t, offset); 448 } 449 450 /** 451 * fgraph_retrieve_parent_data - get data from a parent function 452 * @idx: The index into the fgraph_array (fgraph_ops::idx) 453 * @size_bytes: A pointer to retrieved data size 454 * @depth: The depth to find the parent (0 is the current function) 455 * 456 * This is similar to fgraph_retrieve_data() but can be used to retrieve 457 * data from a parent caller function. 458 * 459 * Return: a pointer to the specified parent data or NULL if not found 460 */ 461 void *fgraph_retrieve_parent_data(int idx, int *size_bytes, int depth) 462 { 463 struct ftrace_ret_stack *ret_stack = NULL; 464 int offset = current->curr_ret_stack; 465 unsigned long val; 466 467 if (offset <= 0) 468 return NULL; 469 470 for (;;) { 471 int next_offset; 472 473 ret_stack = get_ret_stack(current, offset, &next_offset); 474 if (!ret_stack || --depth < 0) 475 break; 476 offset = next_offset; 477 } 478 479 if (!ret_stack) 480 return NULL; 481 482 offset--; 483 484 val = get_fgraph_entry(current, offset); 485 while (__get_type(val) == FGRAPH_TYPE_DATA) { 486 if (__get_data_index(val) == idx) 487 goto found; 488 offset -= __get_data_size(val) + 1; 489 val = get_fgraph_entry(current, offset); 490 } 491 return NULL; 492 found: 493 if (size_bytes) 494 *size_bytes = __get_data_size(val) * sizeof(long); 495 return get_data_type_data(current, offset); 496 } 497 498 #ifdef CONFIG_DYNAMIC_FTRACE 499 /* 500 * archs can override this function if they must do something 501 * to enable hook for graph tracer. 502 */ 503 int __weak ftrace_enable_ftrace_graph_caller(void) 504 { 505 return 0; 506 } 507 508 /* 509 * archs can override this function if they must do something 510 * to disable hook for graph tracer. 511 */ 512 int __weak ftrace_disable_ftrace_graph_caller(void) 513 { 514 return 0; 515 } 516 #endif 517 518 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace, 519 struct fgraph_ops *gops, 520 struct ftrace_regs *fregs) 521 { 522 return 0; 523 } 524 525 static void ftrace_graph_ret_stub(struct ftrace_graph_ret *trace, 526 struct fgraph_ops *gops, 527 struct ftrace_regs *fregs) 528 { 529 } 530 531 static struct fgraph_ops fgraph_stub = { 532 .entryfunc = ftrace_graph_entry_stub, 533 .retfunc = ftrace_graph_ret_stub, 534 }; 535 536 static struct fgraph_ops *fgraph_direct_gops = &fgraph_stub; 537 DEFINE_STATIC_CALL(fgraph_func, ftrace_graph_entry_stub); 538 DEFINE_STATIC_CALL(fgraph_retfunc, ftrace_graph_ret_stub); 539 #if FGRAPH_NO_DIRECT 540 static DEFINE_STATIC_KEY_FALSE(fgraph_do_direct); 541 #else 542 static DEFINE_STATIC_KEY_TRUE(fgraph_do_direct); 543 #endif 544 545 /** 546 * ftrace_graph_stop - set to permanently disable function graph tracing 547 * 548 * In case of an error int function graph tracing, this is called 549 * to try to keep function graph tracing from causing any more harm. 550 * Usually this is pretty severe and this is called to try to at least 551 * get a warning out to the user. 552 */ 553 void ftrace_graph_stop(void) 554 { 555 static_branch_enable(&kill_ftrace_graph); 556 } 557 558 /* Add a function return address to the trace stack on thread info.*/ 559 static int 560 ftrace_push_return_trace(unsigned long ret, unsigned long func, 561 unsigned long frame_pointer, unsigned long *retp, 562 int fgraph_idx) 563 { 564 struct ftrace_ret_stack *ret_stack; 565 unsigned long val; 566 int offset; 567 568 if (unlikely(ftrace_graph_is_dead())) 569 return -EBUSY; 570 571 /* 572 * We must make sure the ret_stack is tested before we read 573 * anything else (See alloc_retstack_tasklist()). 574 */ 575 if (!smp_load_acquire(¤t->ret_stack)) 576 return -EBUSY; 577 578 BUILD_BUG_ON(SHADOW_STACK_SIZE % sizeof(long)); 579 580 /* Set val to "reserved" with the delta to the new fgraph frame */ 581 val = (FGRAPH_TYPE_RESERVED << FGRAPH_TYPE_SHIFT) | FGRAPH_FRAME_OFFSET; 582 583 /* 584 * Check if there's room on the shadow stack to fit a fraph frame 585 * and a bitmap word. 586 */ 587 if (current->curr_ret_stack + FGRAPH_FRAME_OFFSET + 1 >= SHADOW_STACK_MAX_OFFSET) { 588 atomic_inc(¤t->trace_overrun); 589 return -EBUSY; 590 } 591 592 offset = READ_ONCE(current->curr_ret_stack); 593 ret_stack = RET_STACK(current, offset); 594 offset += FGRAPH_FRAME_OFFSET; 595 596 /* ret offset = FGRAPH_FRAME_OFFSET ; type = reserved */ 597 current->ret_stack[offset] = val; 598 ret_stack->ret = ret; 599 /* 600 * The unwinders expect curr_ret_stack to point to either zero 601 * or an offset where to find the next ret_stack. Even though the 602 * ret stack might be bogus, we want to write the ret and the 603 * offset to find the ret_stack before we increment the stack point. 604 * If an interrupt comes in now before we increment the curr_ret_stack 605 * it may blow away what we wrote. But that's fine, because the 606 * offset will still be correct (even though the 'ret' won't be). 607 * What we worry about is the offset being correct after we increment 608 * the curr_ret_stack and before we update that offset, as if an 609 * interrupt comes in and does an unwind stack dump, it will need 610 * at least a correct offset! 611 */ 612 barrier(); 613 WRITE_ONCE(current->curr_ret_stack, offset + 1); 614 /* 615 * This next barrier is to ensure that an interrupt coming in 616 * will not corrupt what we are about to write. 617 */ 618 barrier(); 619 620 /* Still keep it reserved even if an interrupt came in */ 621 current->ret_stack[offset] = val; 622 623 ret_stack->ret = ret; 624 ret_stack->func = func; 625 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST 626 ret_stack->fp = frame_pointer; 627 #endif 628 ret_stack->retp = retp; 629 return offset; 630 } 631 632 /* 633 * Not all archs define MCOUNT_INSN_SIZE which is used to look for direct 634 * functions. But those archs currently don't support direct functions 635 * anyway, and ftrace_find_rec_direct() is just a stub for them. 636 * Define MCOUNT_INSN_SIZE to keep those archs compiling. 637 */ 638 #ifndef MCOUNT_INSN_SIZE 639 /* Make sure this only works without direct calls */ 640 # ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 641 # error MCOUNT_INSN_SIZE not defined with direct calls enabled 642 # endif 643 # define MCOUNT_INSN_SIZE 0 644 #endif 645 646 /* If the caller does not use ftrace, call this function. */ 647 int function_graph_enter_regs(unsigned long ret, unsigned long func, 648 unsigned long frame_pointer, unsigned long *retp, 649 struct ftrace_regs *fregs) 650 { 651 struct ftrace_graph_ent trace; 652 unsigned long bitmap = 0; 653 int offset; 654 int bit; 655 int i; 656 657 bit = ftrace_test_recursion_trylock(func, ret); 658 if (bit < 0) 659 return -EBUSY; 660 661 trace.func = func; 662 trace.depth = ++current->curr_ret_depth; 663 664 offset = ftrace_push_return_trace(ret, func, frame_pointer, retp, 0); 665 if (offset < 0) 666 goto out; 667 668 #ifdef CONFIG_HAVE_STATIC_CALL 669 if (static_branch_likely(&fgraph_do_direct)) { 670 int save_curr_ret_stack = current->curr_ret_stack; 671 672 if (static_call(fgraph_func)(&trace, fgraph_direct_gops, fregs)) 673 bitmap |= BIT(fgraph_direct_gops->idx); 674 else 675 /* Clear out any saved storage */ 676 current->curr_ret_stack = save_curr_ret_stack; 677 } else 678 #endif 679 { 680 for_each_set_bit(i, &fgraph_array_bitmask, 681 sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) { 682 struct fgraph_ops *gops = READ_ONCE(fgraph_array[i]); 683 int save_curr_ret_stack; 684 685 if (gops == &fgraph_stub) 686 continue; 687 688 save_curr_ret_stack = current->curr_ret_stack; 689 if (ftrace_ops_test(&gops->ops, func, NULL) && 690 gops->entryfunc(&trace, gops, fregs)) 691 bitmap |= BIT(i); 692 else 693 /* Clear out any saved storage */ 694 current->curr_ret_stack = save_curr_ret_stack; 695 } 696 } 697 698 if (!bitmap) 699 goto out_ret; 700 701 set_bitmap(current, offset, bitmap); 702 ftrace_test_recursion_unlock(bit); 703 return 0; 704 out_ret: 705 current->curr_ret_stack -= FGRAPH_FRAME_OFFSET + 1; 706 out: 707 current->curr_ret_depth--; 708 ftrace_test_recursion_unlock(bit); 709 return -EBUSY; 710 } 711 712 /* Retrieve a function return address to the trace stack on thread info.*/ 713 static struct ftrace_ret_stack * 714 ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret, 715 unsigned long frame_pointer, int *offset) 716 { 717 struct ftrace_ret_stack *ret_stack; 718 719 ret_stack = get_ret_stack(current, current->curr_ret_stack, offset); 720 721 if (unlikely(!ret_stack)) { 722 ftrace_graph_stop(); 723 WARN(1, "Bad function graph ret_stack pointer: %d", 724 current->curr_ret_stack); 725 /* Might as well panic, otherwise we have no where to go */ 726 *ret = (unsigned long)panic; 727 return NULL; 728 } 729 730 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST 731 /* 732 * The arch may choose to record the frame pointer used 733 * and check it here to make sure that it is what we expect it 734 * to be. If gcc does not set the place holder of the return 735 * address in the frame pointer, and does a copy instead, then 736 * the function graph trace will fail. This test detects this 737 * case. 738 * 739 * Currently, x86_32 with optimize for size (-Os) makes the latest 740 * gcc do the above. 741 * 742 * Note, -mfentry does not use frame pointers, and this test 743 * is not needed if CC_USING_FENTRY is set. 744 */ 745 if (unlikely(ret_stack->fp != frame_pointer)) { 746 ftrace_graph_stop(); 747 WARN(1, "Bad frame pointer: expected %lx, received %lx\n" 748 " from func %ps return to %lx\n", 749 ret_stack->fp, 750 frame_pointer, 751 (void *)ret_stack->func, 752 ret_stack->ret); 753 *ret = (unsigned long)panic; 754 return NULL; 755 } 756 #endif 757 758 *offset += FGRAPH_FRAME_OFFSET; 759 *ret = ret_stack->ret; 760 trace->func = ret_stack->func; 761 trace->overrun = atomic_read(¤t->trace_overrun); 762 trace->depth = current->curr_ret_depth; 763 /* 764 * We still want to trace interrupts coming in if 765 * max_depth is set to 1. Make sure the decrement is 766 * seen before ftrace_graph_return. 767 */ 768 barrier(); 769 770 return ret_stack; 771 } 772 773 /* 774 * Hibernation protection. 775 * The state of the current task is too much unstable during 776 * suspend/restore to disk. We want to protect against that. 777 */ 778 static int 779 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, 780 void *unused) 781 { 782 switch (state) { 783 case PM_HIBERNATION_PREPARE: 784 pause_graph_tracing(); 785 break; 786 787 case PM_POST_HIBERNATION: 788 unpause_graph_tracing(); 789 break; 790 } 791 return NOTIFY_DONE; 792 } 793 794 static struct notifier_block ftrace_suspend_notifier = { 795 .notifier_call = ftrace_suspend_notifier_call, 796 }; 797 798 /* 799 * Send the trace to the ring-buffer. 800 * @return the original return address. 801 */ 802 static inline unsigned long 803 __ftrace_return_to_handler(struct ftrace_regs *fregs, unsigned long frame_pointer) 804 { 805 struct ftrace_ret_stack *ret_stack; 806 struct ftrace_graph_ret trace; 807 unsigned long bitmap; 808 unsigned long ret; 809 int offset; 810 int bit; 811 int i; 812 813 ret_stack = ftrace_pop_return_trace(&trace, &ret, frame_pointer, &offset); 814 815 if (unlikely(!ret_stack)) { 816 ftrace_graph_stop(); 817 WARN_ON(1); 818 /* Might as well panic. What else to do? */ 819 return (unsigned long)panic; 820 } 821 822 if (fregs) 823 ftrace_regs_set_instruction_pointer(fregs, ret); 824 825 bit = ftrace_test_recursion_trylock(trace.func, ret); 826 /* 827 * This can fail because ftrace_test_recursion_trylock() allows one nest 828 * call. If we are already in a nested call, then we don't probe this and 829 * just return the original return address. 830 */ 831 if (unlikely(bit < 0)) 832 goto out; 833 834 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL 835 trace.retval = ftrace_regs_get_return_value(fregs); 836 #endif 837 838 bitmap = get_bitmap_bits(current, offset); 839 840 #ifdef CONFIG_HAVE_STATIC_CALL 841 if (!FGRAPH_NO_DIRECT && static_branch_likely(&fgraph_do_direct)) { 842 if (test_bit(fgraph_direct_gops->idx, &bitmap)) 843 static_call(fgraph_retfunc)(&trace, fgraph_direct_gops, fregs); 844 } else 845 #endif 846 { 847 for_each_set_bit(i, &bitmap, sizeof(bitmap) * BITS_PER_BYTE) { 848 struct fgraph_ops *gops = READ_ONCE(fgraph_array[i]); 849 850 if (gops == &fgraph_stub) 851 continue; 852 853 gops->retfunc(&trace, gops, fregs); 854 } 855 } 856 857 ftrace_test_recursion_unlock(bit); 858 out: 859 /* 860 * The ftrace_graph_return() may still access the current 861 * ret_stack structure, we need to make sure the update of 862 * curr_ret_stack is after that. 863 */ 864 barrier(); 865 current->curr_ret_stack = offset - FGRAPH_FRAME_OFFSET; 866 867 current->curr_ret_depth--; 868 return ret; 869 } 870 871 /* 872 * After all architectures have selected HAVE_FUNCTION_GRAPH_FREGS, we can 873 * leave only ftrace_return_to_handler(fregs). 874 */ 875 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FREGS 876 unsigned long ftrace_return_to_handler(struct ftrace_regs *fregs) 877 { 878 return __ftrace_return_to_handler(fregs, 879 ftrace_regs_get_frame_pointer(fregs)); 880 } 881 #else 882 unsigned long ftrace_return_to_handler(unsigned long frame_pointer) 883 { 884 return __ftrace_return_to_handler(NULL, frame_pointer); 885 } 886 #endif 887 888 /** 889 * ftrace_graph_get_ret_stack - return the entry of the shadow stack 890 * @task: The task to read the shadow stack from. 891 * @idx: Index down the shadow stack 892 * 893 * Return the ret_struct on the shadow stack of the @task at the 894 * call graph at @idx starting with zero. If @idx is zero, it 895 * will return the last saved ret_stack entry. If it is greater than 896 * zero, it will return the corresponding ret_stack for the depth 897 * of saved return addresses. 898 */ 899 struct ftrace_ret_stack * 900 ftrace_graph_get_ret_stack(struct task_struct *task, int idx) 901 { 902 struct ftrace_ret_stack *ret_stack = NULL; 903 int offset = task->curr_ret_stack; 904 905 if (offset < 0) 906 return NULL; 907 908 do { 909 ret_stack = get_ret_stack(task, offset, &offset); 910 } while (ret_stack && --idx >= 0); 911 912 return ret_stack; 913 } 914 915 /** 916 * ftrace_graph_top_ret_addr - return the top return address in the shadow stack 917 * @task: The task to read the shadow stack from. 918 * 919 * Return the first return address on the shadow stack of the @task, which is 920 * not the fgraph's return_to_handler. 921 */ 922 unsigned long ftrace_graph_top_ret_addr(struct task_struct *task) 923 { 924 unsigned long return_handler = (unsigned long)dereference_kernel_function_descriptor(return_to_handler); 925 struct ftrace_ret_stack *ret_stack = NULL; 926 int offset = task->curr_ret_stack; 927 928 if (offset < 0) 929 return 0; 930 931 do { 932 ret_stack = get_ret_stack(task, offset, &offset); 933 } while (ret_stack && ret_stack->ret == return_handler); 934 935 return ret_stack ? ret_stack->ret : 0; 936 } 937 938 /** 939 * ftrace_graph_ret_addr - return the original value of the return address 940 * @task: The task the unwinder is being executed on 941 * @idx: An initialized pointer to the next stack index to use 942 * @ret: The current return address (likely pointing to return_handler) 943 * @retp: The address on the stack of the current return location 944 * 945 * This function can be called by stack unwinding code to convert a found stack 946 * return address (@ret) to its original value, in case the function graph 947 * tracer has modified it to be 'return_to_handler'. If the address hasn't 948 * been modified, the unchanged value of @ret is returned. 949 * 950 * @idx holds the last index used to know where to start from. It should be 951 * initialized to zero for the first iteration as that will mean to start 952 * at the top of the shadow stack. If the location is found, this pointer 953 * will be assigned that location so that if called again, it will continue 954 * where it left off. 955 * 956 * @retp is a pointer to the return address on the stack. 957 */ 958 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx, 959 unsigned long ret, unsigned long *retp) 960 { 961 struct ftrace_ret_stack *ret_stack; 962 unsigned long return_handler = (unsigned long)dereference_kernel_function_descriptor(return_to_handler); 963 int i; 964 965 if (ret != return_handler) 966 return ret; 967 968 if (!idx) 969 return ret; 970 971 i = *idx ? : task->curr_ret_stack; 972 while (i > 0) { 973 ret_stack = get_ret_stack(task, i, &i); 974 if (!ret_stack) 975 break; 976 /* 977 * For the tail-call, there would be 2 or more ftrace_ret_stacks on 978 * the ret_stack, which records "return_to_handler" as the return 979 * address except for the last one. 980 * But on the real stack, there should be 1 entry because tail-call 981 * reuses the return address on the stack and jump to the next function. 982 * Thus we will continue to find real return address. 983 */ 984 if (ret_stack->retp == retp && 985 ret_stack->ret != return_handler) { 986 *idx = i; 987 return ret_stack->ret; 988 } 989 } 990 991 return ret; 992 } 993 994 static struct ftrace_ops graph_ops = { 995 .func = ftrace_graph_func, 996 .flags = FTRACE_OPS_GRAPH_STUB, 997 #ifdef FTRACE_GRAPH_TRAMP_ADDR 998 .trampoline = FTRACE_GRAPH_TRAMP_ADDR, 999 /* trampoline_size is only needed for dynamically allocated tramps */ 1000 #endif 1001 }; 1002 1003 void fgraph_init_ops(struct ftrace_ops *dst_ops, 1004 struct ftrace_ops *src_ops) 1005 { 1006 dst_ops->flags = FTRACE_OPS_FL_PID | FTRACE_OPS_GRAPH_STUB; 1007 1008 #ifdef CONFIG_DYNAMIC_FTRACE 1009 if (src_ops) { 1010 dst_ops->func_hash = &src_ops->local_hash; 1011 mutex_init(&dst_ops->local_hash.regex_lock); 1012 INIT_LIST_HEAD(&dst_ops->subop_list); 1013 dst_ops->flags |= FTRACE_OPS_FL_INITIALIZED; 1014 dst_ops->private = src_ops->private; 1015 } 1016 #endif 1017 } 1018 1019 /* 1020 * Simply points to ftrace_stub, but with the proper protocol. 1021 * Defined by the linker script in linux/vmlinux.lds.h 1022 */ 1023 void ftrace_stub_graph(struct ftrace_graph_ret *trace, struct fgraph_ops *gops, 1024 struct ftrace_regs *fregs); 1025 1026 /* The callbacks that hook a function */ 1027 trace_func_graph_ret_t ftrace_graph_return = ftrace_stub_graph; 1028 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; 1029 1030 /* 1031 * Loop thru all the tasks in the system and assign ret_stack. 1032 * The @ret_stack_list is a small pre-allocated fallback, in case 1033 * the GFP_NOWAIT allocations here fail. 1034 */ 1035 static int alloc_retstack_tasklist(unsigned long **ret_stack_list) 1036 { 1037 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; 1038 struct task_struct *g, *t; 1039 int i, ret = 0; 1040 1041 if (WARN_ON_ONCE(!fgraph_stack_cachep)) 1042 return -ENOMEM; 1043 1044 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { 1045 ret_stack_list[i] = kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); 1046 if (!ret_stack_list[i]) { 1047 start = 0; 1048 end = i; 1049 ret = -ENOMEM; 1050 goto free; 1051 } 1052 } 1053 1054 scoped_guard (rcu) { 1055 for_each_process_thread(g, t) { 1056 unsigned long *rs; 1057 1058 if (t->ret_stack) 1059 continue; 1060 1061 rs = kmem_cache_alloc(fgraph_stack_cachep, GFP_NOWAIT); 1062 if (!rs) { 1063 if (start == end) { 1064 ret = -EAGAIN; 1065 goto free; 1066 } 1067 rs = ret_stack_list[start++]; 1068 } 1069 1070 atomic_set(&t->trace_overrun, 0); 1071 ret_stack_init_task_vars(rs); 1072 t->curr_ret_stack = 0; 1073 t->curr_ret_depth = -1; 1074 /* Make sure the tasks see the 0 first: */ 1075 smp_store_release(&t->ret_stack, rs); 1076 } 1077 } 1078 1079 free: 1080 for (i = start; i < end; i++) 1081 kmem_cache_free(fgraph_stack_cachep, ret_stack_list[i]); 1082 return ret; 1083 } 1084 1085 static void 1086 ftrace_graph_probe_sched_switch(void *ignore, bool preempt, 1087 struct task_struct *prev, 1088 struct task_struct *next, 1089 unsigned int prev_state) 1090 { 1091 unsigned long long timestamp; 1092 1093 /* 1094 * Does the user want to count the time a function was asleep. 1095 * If so, do not update the time stamps. 1096 */ 1097 if (!fgraph_no_sleep_time) 1098 return; 1099 1100 timestamp = trace_clock_local(); 1101 1102 prev->ftrace_timestamp = timestamp; 1103 1104 /* only process tasks that we timestamped */ 1105 if (!next->ftrace_timestamp) 1106 return; 1107 1108 next->ftrace_sleeptime += timestamp - next->ftrace_timestamp; 1109 } 1110 1111 static DEFINE_PER_CPU(unsigned long *, idle_ret_stack); 1112 1113 static void 1114 graph_init_task(struct task_struct *t, unsigned long *ret_stack) 1115 { 1116 atomic_set(&t->trace_overrun, 0); 1117 ret_stack_init_task_vars(ret_stack); 1118 t->ftrace_timestamp = 0; 1119 t->curr_ret_stack = 0; 1120 t->curr_ret_depth = -1; 1121 /* make curr_ret_stack visible before we add the ret_stack */ 1122 smp_store_release(&t->ret_stack, ret_stack); 1123 } 1124 1125 /* 1126 * Allocate a return stack for the idle task. May be the first 1127 * time through, or it may be done by CPU hotplug online. 1128 */ 1129 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) 1130 { 1131 t->curr_ret_stack = 0; 1132 t->curr_ret_depth = -1; 1133 /* 1134 * The idle task has no parent, it either has its own 1135 * stack or no stack at all. 1136 */ 1137 if (t->ret_stack) 1138 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); 1139 1140 if (ftrace_graph_active) { 1141 unsigned long *ret_stack; 1142 1143 if (WARN_ON_ONCE(!fgraph_stack_cachep)) 1144 return; 1145 1146 ret_stack = per_cpu(idle_ret_stack, cpu); 1147 if (!ret_stack) { 1148 ret_stack = kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); 1149 if (!ret_stack) 1150 return; 1151 per_cpu(idle_ret_stack, cpu) = ret_stack; 1152 } 1153 graph_init_task(t, ret_stack); 1154 } 1155 } 1156 1157 /* Allocate a return stack for newly created task */ 1158 void ftrace_graph_init_task(struct task_struct *t) 1159 { 1160 /* Make sure we do not use the parent ret_stack */ 1161 t->ret_stack = NULL; 1162 t->curr_ret_stack = 0; 1163 t->curr_ret_depth = -1; 1164 1165 if (ftrace_graph_active) { 1166 unsigned long *ret_stack; 1167 1168 if (WARN_ON_ONCE(!fgraph_stack_cachep)) 1169 return; 1170 1171 ret_stack = kmem_cache_alloc(fgraph_stack_cachep, GFP_KERNEL); 1172 if (!ret_stack) 1173 return; 1174 graph_init_task(t, ret_stack); 1175 } 1176 } 1177 1178 void ftrace_graph_exit_task(struct task_struct *t) 1179 { 1180 unsigned long *ret_stack = t->ret_stack; 1181 1182 t->ret_stack = NULL; 1183 /* NULL must become visible to IRQs before we free it: */ 1184 barrier(); 1185 1186 if (ret_stack) { 1187 if (WARN_ON_ONCE(!fgraph_stack_cachep)) 1188 return; 1189 kmem_cache_free(fgraph_stack_cachep, ret_stack); 1190 } 1191 } 1192 1193 #ifdef CONFIG_DYNAMIC_FTRACE 1194 static int fgraph_pid_func(struct ftrace_graph_ent *trace, 1195 struct fgraph_ops *gops, 1196 struct ftrace_regs *fregs) 1197 { 1198 struct trace_array *tr = gops->ops.private; 1199 int pid; 1200 1201 if (tr) { 1202 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid); 1203 if (pid == FTRACE_PID_IGNORE) 1204 return 0; 1205 if (pid != FTRACE_PID_TRACE && 1206 pid != current->pid) 1207 return 0; 1208 } 1209 1210 return gops->saved_func(trace, gops, fregs); 1211 } 1212 1213 void fgraph_update_pid_func(void) 1214 { 1215 struct fgraph_ops *gops; 1216 struct ftrace_ops *op; 1217 1218 if (!(graph_ops.flags & FTRACE_OPS_FL_INITIALIZED)) 1219 return; 1220 1221 list_for_each_entry(op, &graph_ops.subop_list, list) { 1222 if (op->flags & FTRACE_OPS_FL_PID) { 1223 gops = container_of(op, struct fgraph_ops, ops); 1224 gops->entryfunc = ftrace_pids_enabled(op) ? 1225 fgraph_pid_func : gops->saved_func; 1226 if (ftrace_graph_active == 1) 1227 static_call_update(fgraph_func, gops->entryfunc); 1228 } 1229 } 1230 } 1231 #endif 1232 1233 /* Allocate a return stack for each task */ 1234 static int start_graph_tracing(void) 1235 { 1236 unsigned long **ret_stack_list; 1237 int ret, cpu; 1238 1239 ret_stack_list = kzalloc_objs(*ret_stack_list, 1240 FTRACE_RETSTACK_ALLOC_SIZE); 1241 1242 if (!ret_stack_list) 1243 return -ENOMEM; 1244 1245 /* The cpu_boot init_task->ret_stack will never be freed */ 1246 for_each_online_cpu(cpu) { 1247 if (!idle_task(cpu)->ret_stack) 1248 ftrace_graph_init_idle_task(idle_task(cpu), cpu); 1249 } 1250 1251 do { 1252 ret = alloc_retstack_tasklist(ret_stack_list); 1253 } while (ret == -EAGAIN); 1254 1255 if (!ret) { 1256 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 1257 if (ret) 1258 pr_info("ftrace_graph: Couldn't activate tracepoint" 1259 " probe to kernel_sched_switch\n"); 1260 } 1261 1262 kfree(ret_stack_list); 1263 return ret; 1264 } 1265 1266 static void init_task_vars(int idx) 1267 { 1268 struct task_struct *g, *t; 1269 int cpu; 1270 1271 for_each_online_cpu(cpu) { 1272 if (idle_task(cpu)->ret_stack) 1273 ret_stack_set_task_var(idle_task(cpu), idx, 0); 1274 } 1275 1276 read_lock(&tasklist_lock); 1277 for_each_process_thread(g, t) { 1278 if (t->ret_stack) 1279 ret_stack_set_task_var(t, idx, 0); 1280 } 1281 read_unlock(&tasklist_lock); 1282 } 1283 1284 static void ftrace_graph_enable_direct(bool enable_branch, struct fgraph_ops *gops) 1285 { 1286 trace_func_graph_ent_t func = NULL; 1287 trace_func_graph_ret_t retfunc = NULL; 1288 int i; 1289 1290 if (FGRAPH_NO_DIRECT) 1291 return; 1292 1293 if (gops) { 1294 func = gops->entryfunc; 1295 retfunc = gops->retfunc; 1296 fgraph_direct_gops = gops; 1297 } else { 1298 for_each_set_bit(i, &fgraph_array_bitmask, 1299 sizeof(fgraph_array_bitmask) * BITS_PER_BYTE) { 1300 func = fgraph_array[i]->entryfunc; 1301 retfunc = fgraph_array[i]->retfunc; 1302 fgraph_direct_gops = fgraph_array[i]; 1303 } 1304 } 1305 if (WARN_ON_ONCE(!func)) 1306 return; 1307 1308 static_call_update(fgraph_func, func); 1309 static_call_update(fgraph_retfunc, retfunc); 1310 if (enable_branch) 1311 static_branch_enable(&fgraph_do_direct); 1312 } 1313 1314 static void ftrace_graph_disable_direct(bool disable_branch) 1315 { 1316 if (FGRAPH_NO_DIRECT) 1317 return; 1318 1319 if (disable_branch) 1320 static_branch_disable(&fgraph_do_direct); 1321 static_call_update(fgraph_func, ftrace_graph_entry_stub); 1322 static_call_update(fgraph_retfunc, ftrace_graph_ret_stub); 1323 fgraph_direct_gops = &fgraph_stub; 1324 } 1325 1326 /* The cpu_boot init_task->ret_stack will never be freed */ 1327 static int fgraph_cpu_init(unsigned int cpu) 1328 { 1329 if (!idle_task(cpu)->ret_stack) 1330 ftrace_graph_init_idle_task(idle_task(cpu), cpu); 1331 return 0; 1332 } 1333 1334 int register_ftrace_graph(struct fgraph_ops *gops) 1335 { 1336 static bool fgraph_initialized; 1337 int command = 0; 1338 int ret = 0; 1339 int i = -1; 1340 1341 if (WARN_ONCE(gops->ops.flags & FTRACE_OPS_FL_GRAPH, 1342 "function graph ops registered again")) 1343 return -EBUSY; 1344 1345 guard(mutex)(&ftrace_lock); 1346 1347 if (!fgraph_stack_cachep) { 1348 fgraph_stack_cachep = kmem_cache_create("fgraph_stack", 1349 SHADOW_STACK_SIZE, 1350 SHADOW_STACK_SIZE, 0, NULL); 1351 if (!fgraph_stack_cachep) 1352 return -ENOMEM; 1353 } 1354 1355 if (!fgraph_initialized) { 1356 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "fgraph:online", 1357 fgraph_cpu_init, NULL); 1358 if (ret < 0) { 1359 pr_warn("fgraph: Error to init cpu hotplug support\n"); 1360 return ret; 1361 } 1362 fgraph_initialized = true; 1363 ret = 0; 1364 } 1365 1366 if (!fgraph_array[0]) { 1367 /* The array must always have real data on it */ 1368 for (i = 0; i < FGRAPH_ARRAY_SIZE; i++) 1369 fgraph_array[i] = &fgraph_stub; 1370 fgraph_lru_init(); 1371 } 1372 1373 i = fgraph_lru_alloc_index(); 1374 if (i < 0 || WARN_ON_ONCE(fgraph_array[i] != &fgraph_stub)) 1375 return -ENOSPC; 1376 gops->idx = i; 1377 1378 ftrace_graph_active++; 1379 1380 /* Always save the function, and reset at unregistering */ 1381 gops->saved_func = gops->entryfunc; 1382 #ifdef CONFIG_DYNAMIC_FTRACE 1383 if (ftrace_pids_enabled(&gops->ops)) 1384 gops->entryfunc = fgraph_pid_func; 1385 #endif 1386 1387 if (ftrace_graph_active == 2) 1388 ftrace_graph_disable_direct(true); 1389 1390 if (ftrace_graph_active == 1) { 1391 ftrace_graph_enable_direct(false, gops); 1392 register_pm_notifier(&ftrace_suspend_notifier); 1393 ret = start_graph_tracing(); 1394 if (ret) 1395 goto error; 1396 /* 1397 * Some archs just test to see if these are not 1398 * the default function 1399 */ 1400 ftrace_graph_return = return_run; 1401 ftrace_graph_entry = entry_run; 1402 command = FTRACE_START_FUNC_RET; 1403 } else { 1404 init_task_vars(gops->idx); 1405 } 1406 1407 gops->ops.flags |= FTRACE_OPS_FL_GRAPH; 1408 1409 ret = ftrace_startup_subops(&graph_ops, &gops->ops, command); 1410 if (!ret) 1411 fgraph_array[i] = gops; 1412 1413 error: 1414 if (ret) { 1415 ftrace_graph_active--; 1416 gops->saved_func = NULL; 1417 fgraph_lru_release_index(i); 1418 if (!ftrace_graph_active) 1419 unregister_pm_notifier(&ftrace_suspend_notifier); 1420 } 1421 return ret; 1422 } 1423 1424 void unregister_ftrace_graph(struct fgraph_ops *gops) 1425 { 1426 int command = 0; 1427 1428 if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH), 1429 "function graph ops unregistered without registering")) 1430 return; 1431 1432 guard(mutex)(&ftrace_lock); 1433 1434 if (unlikely(!ftrace_graph_active)) 1435 goto out; 1436 1437 if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE || 1438 fgraph_array[gops->idx] != gops)) 1439 goto out; 1440 1441 if (fgraph_lru_release_index(gops->idx) < 0) 1442 goto out; 1443 1444 fgraph_array[gops->idx] = &fgraph_stub; 1445 1446 ftrace_graph_active--; 1447 1448 if (!ftrace_graph_active) 1449 command = FTRACE_STOP_FUNC_RET; 1450 1451 ftrace_shutdown_subops(&graph_ops, &gops->ops, command); 1452 1453 if (ftrace_graph_active == 1) 1454 ftrace_graph_enable_direct(true, NULL); 1455 else if (!ftrace_graph_active) 1456 ftrace_graph_disable_direct(false); 1457 1458 if (!ftrace_graph_active) { 1459 ftrace_graph_return = ftrace_stub_graph; 1460 ftrace_graph_entry = ftrace_graph_entry_stub; 1461 unregister_pm_notifier(&ftrace_suspend_notifier); 1462 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); 1463 } 1464 gops->saved_func = NULL; 1465 out: 1466 gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH; 1467 } 1468