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