Lines Matching +full:shadow +full:- +full:interrupts

1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
23 * FGRAPH_FRAME_SIZE: Size in bytes of the meta data on the shadow stack
34 * bits: 0 - 9 offset in words from the previous ftrace_ret_stack
36 * bits: 10 - 11 Type of storage
37 * 0 - reserved
38 * 1 - bitmap of fgraph_array index
39 * 2 - reserved data
42 * bits: 12 - 27 The bitmap of fgraph_ops fgraph_array index
43 * That is, it's a bitmask of 0-15 (16 bits)
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
59 * stored two words of data, this is what will be on the task's shadow
68 * | | <- task->curr_ret_stack
69 * +--------------------------------------------+
73 * | ((2 - 1) << FGRAPH_DATA_SHIFT)| \ | The data size is 2 words.
75 * | (offset2:FGRAPH_FRAME_OFFSET+3) | <- the offset2 is from here
76 * +--------------------------------------------+ ( It is 4 words from the ret_stack)
79 * +--------------------------------------------+
84 * | (offset1:FGRAPH_FRAME_OFFSET) | <- the offset1 is from here
85 * +--------------------------------------------+
87 * | (stores the saved ret pointer) | <- the offset points here
88 * +--------------------------------------------+
98 * ftrace_ret_stack structure stored on the shadow stack.
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.
109 #define FGRAPH_FRAME_OFFSET_MASK GENMASK(FGRAPH_FRAME_OFFSET_BITS - 1, 0)
112 #define FGRAPH_TYPE_MASK GENMASK(FGRAPH_TYPE_BITS - 1, 0)
123 * FGRAPH_INDEX (12-27) bits holding the gops index wanting return callback called
126 #define FGRAPH_INDEX_MASK GENMASK(FGRAPH_INDEX_BITS - 1, 0)
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
135 * data_size == 0 means 1 word, and 31 (=2^5 - 1) means 32 words.
138 #define FGRAPH_DATA_MASK GENMASK(FGRAPH_DATA_BITS - 1, 0)
143 #define FGRAPH_DATA_INDEX_MASK GENMASK(FGRAPH_DATA_INDEX_BITS - 1, 0)
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
160 (SHADOW_STACK_OFFSET - (FGRAPH_FRAME_OFFSET + 1 + FGRAPH_ARRAY_SIZE))
163 #define RET_STACK(t, offset) ((struct ftrace_ret_stack *)(&(t)->ret_stack[offset]))
170 ((unsigned long *)(&(ret_stack)[SHADOW_STACK_OFFSET - FGRAPH_ARRAY_SIZE]))
198 WARN_ON_ONCE(fgraph_lru_table[fgraph_lru_last] != -1)) in fgraph_lru_release_index()
199 return -1; in fgraph_lru_release_index()
214 if (idx == -1) in fgraph_lru_alloc_index()
215 return -1; in fgraph_lru_alloc_index()
217 fgraph_lru_table[fgraph_lru_next] = -1; in fgraph_lru_alloc_index()
251 return t->ret_stack[offset]; in get_fgraph_entry()
257 return __get_offset(t->ret_stack[offset]); in get_frame_offset()
264 return (t->ret_stack[offset] >> FGRAPH_INDEX_SHIFT) & FGRAPH_INDEX_MASK; in get_bitmap_bits()
271 t->ret_stack[offset] = (bitmap << FGRAPH_INDEX_SHIFT) | in set_bitmap()
278 unsigned long val = t->ret_stack[offset]; in get_data_type_data()
282 offset -= __get_data_size(val); in get_data_type_data()
283 return (void *)&t->ret_stack[offset]; in get_data_type_data()
290 ((size - 1) << FGRAPH_DATA_SHIFT) | in make_data_type_val()
309 unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); in ret_stack_set_task_var()
317 unsigned long *gvals = SHADOW_STACK_TASK_VARS(t->ret_stack); in ret_stack_get_task_var()
330 * fgraph_reserve_data - Reserve storage on the task's ret_stack
335 * task's ret_stack shadow stack, for a given fgraph_ops during
350 int curr_ret_stack = current->curr_ret_stack; in fgraph_reserve_data()
357 data_size = (size_bytes + sizeof(long) - 1) >> (sizeof(long) == 4 ? 2 : 3); in fgraph_reserve_data()
359 val = get_fgraph_entry(current, curr_ret_stack - 1); in fgraph_reserve_data()
360 data = &current->ret_stack[curr_ret_stack]; in fgraph_reserve_data()
369 current->ret_stack[curr_ret_stack - 1] = val; in fgraph_reserve_data()
371 /* Make sure interrupts see this */ in fgraph_reserve_data()
373 current->curr_ret_stack = curr_ret_stack; in fgraph_reserve_data()
374 /* Again sync with interrupts, and reset reserve */ in fgraph_reserve_data()
375 current->ret_stack[curr_ret_stack - 1] = val; in fgraph_reserve_data()
381 * fgraph_retrieve_data - Retrieve stored data from fgraph_reserve_data()
401 * fgraph_get_task_var - retrieve a task specific state variable
413 return ret_stack_get_task_var(current, gops->idx); in fgraph_get_task_var()
417 * @offset: The offset into @t->ret_stack to find the ret_stack entry
418 * @frame_offset: Where to place the offset into @t->ret_stack of that entry
425 * offset = task->curr_ret_stack;
443 offs = get_frame_offset(t, --offset); in get_ret_stack()
447 offset -= offs; in get_ret_stack()
454 * fgraph_retrieve_parent_data - get data from a parent function
467 int offset = current->curr_ret_stack; in fgraph_retrieve_parent_data()
477 if (!ret_stack || --depth < 0) in fgraph_retrieve_parent_data()
485 offset--; in fgraph_retrieve_parent_data()
491 offset -= __get_data_size(val) + 1; in fgraph_retrieve_parent_data()
548 * ftrace_graph_stop - set to permanently disable function graph tracing
571 return -EBUSY; in ftrace_push_return_trace()
573 if (!current->ret_stack) in ftrace_push_return_trace()
574 return -EBUSY; in ftrace_push_return_trace()
588 * Check if there's room on the shadow stack to fit a fraph frame in ftrace_push_return_trace()
591 if (current->curr_ret_stack + FGRAPH_FRAME_OFFSET + 1 >= SHADOW_STACK_MAX_OFFSET) { in ftrace_push_return_trace()
592 atomic_inc(&current->trace_overrun); in ftrace_push_return_trace()
593 return -EBUSY; in ftrace_push_return_trace()
596 offset = READ_ONCE(current->curr_ret_stack); in ftrace_push_return_trace()
601 current->ret_stack[offset] = val; in ftrace_push_return_trace()
602 ret_stack->ret = ret; in ftrace_push_return_trace()
617 WRITE_ONCE(current->curr_ret_stack, offset + 1); in ftrace_push_return_trace()
625 current->ret_stack[offset] = val; in ftrace_push_return_trace()
627 ret_stack->ret = ret; in ftrace_push_return_trace()
628 ret_stack->func = func; in ftrace_push_return_trace()
630 ret_stack->fp = frame_pointer; in ftrace_push_return_trace()
632 ret_stack->retp = retp; in ftrace_push_return_trace()
663 return -EBUSY; in function_graph_enter_regs()
666 trace.depth = ++current->curr_ret_depth; in function_graph_enter_regs()
674 int save_curr_ret_stack = current->curr_ret_stack; in function_graph_enter_regs()
677 bitmap |= BIT(fgraph_direct_gops->idx); in function_graph_enter_regs()
680 current->curr_ret_stack = save_curr_ret_stack; in function_graph_enter_regs()
692 save_curr_ret_stack = current->curr_ret_stack; in function_graph_enter_regs()
693 if (ftrace_ops_test(&gops->ops, func, NULL) && in function_graph_enter_regs()
694 gops->entryfunc(&trace, gops, fregs)) in function_graph_enter_regs()
698 current->curr_ret_stack = save_curr_ret_stack; in function_graph_enter_regs()
706 * Since this function uses fgraph_idx = 0 as a tail-call checking in function_graph_enter_regs()
713 current->curr_ret_stack -= FGRAPH_FRAME_OFFSET + 1; in function_graph_enter_regs()
715 current->curr_ret_depth--; in function_graph_enter_regs()
717 return -EBUSY; in function_graph_enter_regs()
727 ret_stack = get_ret_stack(current, current->curr_ret_stack, offset); in ftrace_pop_return_trace()
732 current->curr_ret_stack); in ftrace_pop_return_trace()
747 * Currently, x86_32 with optimize for size (-Os) makes the latest in ftrace_pop_return_trace()
750 * Note, -mfentry does not use frame pointers, and this test in ftrace_pop_return_trace()
753 if (unlikely(ret_stack->fp != frame_pointer)) { in ftrace_pop_return_trace()
757 ret_stack->fp, in ftrace_pop_return_trace()
759 (void *)ret_stack->func, in ftrace_pop_return_trace()
760 ret_stack->ret); in ftrace_pop_return_trace()
767 *ret = ret_stack->ret; in ftrace_pop_return_trace()
768 trace->func = ret_stack->func; in ftrace_pop_return_trace()
769 trace->overrun = atomic_read(&current->trace_overrun); in ftrace_pop_return_trace()
770 trace->depth = current->curr_ret_depth; in ftrace_pop_return_trace()
772 * We still want to trace interrupts coming in if in ftrace_pop_return_trace()
807 * Send the trace to the ring-buffer.
850 if (test_bit(fgraph_direct_gops->idx, &bitmap)) in __ftrace_return_to_handler()
861 gops->retfunc(&trace, gops, fregs); in __ftrace_return_to_handler()
873 current->curr_ret_stack = offset - FGRAPH_FRAME_OFFSET; in __ftrace_return_to_handler()
875 current->curr_ret_depth--; in __ftrace_return_to_handler()
897 * ftrace_graph_get_ret_stack - return the entry of the shadow stack
898 * @task: The task to read the shadow stack from.
899 * @idx: Index down the shadow stack
901 * Return the ret_struct on the shadow stack of the @task at the
911 int offset = task->curr_ret_stack; in ftrace_graph_get_ret_stack()
918 } while (ret_stack && --idx >= 0); in ftrace_graph_get_ret_stack()
924 * ftrace_graph_top_ret_addr - return the top return address in the shadow stack
925 * @task: The task to read the shadow stack from.
927 * Return the first return address on the shadow stack of the @task, which is
934 int offset = task->curr_ret_stack; in ftrace_graph_top_ret_addr()
941 } while (ret_stack && ret_stack->ret == return_handler); in ftrace_graph_top_ret_addr()
943 return ret_stack ? ret_stack->ret : 0; in ftrace_graph_top_ret_addr()
947 * ftrace_graph_ret_addr - return the original value of the return address
960 * at the top of the shadow stack. If the location is found, this pointer
979 i = *idx ? : task->curr_ret_stack; in ftrace_graph_ret_addr()
985 * For the tail-call, there would be 2 or more ftrace_ret_stacks on in ftrace_graph_ret_addr()
988 * But on the real stack, there should be 1 entry because tail-call in ftrace_graph_ret_addr()
992 if (ret_stack->retp == retp && in ftrace_graph_ret_addr()
993 ret_stack->ret != return_handler) { in ftrace_graph_ret_addr()
995 return ret_stack->ret; in ftrace_graph_ret_addr()
1014 dst_ops->flags = FTRACE_OPS_FL_PID | FTRACE_OPS_GRAPH_STUB; in fgraph_init_ops()
1018 dst_ops->func_hash = &src_ops->local_hash; in fgraph_init_ops()
1019 mutex_init(&dst_ops->local_hash.regex_lock); in fgraph_init_ops()
1020 INIT_LIST_HEAD(&dst_ops->subop_list); in fgraph_init_ops()
1021 dst_ops->flags |= FTRACE_OPS_FL_INITIALIZED; in fgraph_init_ops()
1051 return -ENOMEM; in alloc_retstack_tasklist()
1058 ret = -ENOMEM; in alloc_retstack_tasklist()
1066 ret = -EAGAIN; in alloc_retstack_tasklist()
1070 if (t->ret_stack == NULL) { in alloc_retstack_tasklist()
1071 atomic_set(&t->trace_overrun, 0); in alloc_retstack_tasklist()
1073 t->curr_ret_stack = 0; in alloc_retstack_tasklist()
1074 t->curr_ret_depth = -1; in alloc_retstack_tasklist()
1077 t->ret_stack = ret_stack_list[start++]; in alloc_retstack_tasklist()
1106 prev->ftrace_timestamp = timestamp; in ftrace_graph_probe_sched_switch()
1109 if (!next->ftrace_timestamp) in ftrace_graph_probe_sched_switch()
1112 next->ftrace_sleeptime += timestamp - next->ftrace_timestamp; in ftrace_graph_probe_sched_switch()
1120 atomic_set(&t->trace_overrun, 0); in graph_init_task()
1122 t->ftrace_timestamp = 0; in graph_init_task()
1123 t->curr_ret_stack = 0; in graph_init_task()
1124 t->curr_ret_depth = -1; in graph_init_task()
1127 t->ret_stack = ret_stack; in graph_init_task()
1136 t->curr_ret_stack = 0; in ftrace_graph_init_idle_task()
1137 t->curr_ret_depth = -1; in ftrace_graph_init_idle_task()
1142 if (t->ret_stack) in ftrace_graph_init_idle_task()
1143 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); in ftrace_graph_init_idle_task()
1166 t->ret_stack = NULL; in ftrace_graph_init_task()
1167 t->curr_ret_stack = 0; in ftrace_graph_init_task()
1168 t->curr_ret_depth = -1; in ftrace_graph_init_task()
1185 unsigned long *ret_stack = t->ret_stack; in ftrace_graph_exit_task()
1187 t->ret_stack = NULL; in ftrace_graph_exit_task()
1203 struct trace_array *tr = gops->ops.private; in fgraph_pid_func()
1207 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid); in fgraph_pid_func()
1211 pid != current->pid) in fgraph_pid_func()
1215 return gops->saved_func(trace, gops, fregs); in fgraph_pid_func()
1227 if (op->flags & FTRACE_OPS_FL_PID) { in fgraph_update_pid_func()
1229 gops->entryfunc = ftrace_pids_enabled(op) ? in fgraph_update_pid_func()
1230 fgraph_pid_func : gops->saved_func; in fgraph_update_pid_func()
1232 static_call_update(fgraph_func, gops->entryfunc); in fgraph_update_pid_func()
1248 return -ENOMEM; in start_graph_tracing()
1250 /* The cpu_boot init_task->ret_stack will never be freed */ in start_graph_tracing()
1252 if (!idle_task(cpu)->ret_stack) in start_graph_tracing()
1258 } while (ret == -EAGAIN); in start_graph_tracing()
1277 if (idle_task(cpu)->ret_stack) in init_task_vars()
1283 if (t->ret_stack) in init_task_vars()
1296 func = gops->entryfunc; in ftrace_graph_enable_direct()
1297 retfunc = gops->retfunc; in ftrace_graph_enable_direct()
1302 func = fgraph_array[i]->entryfunc; in ftrace_graph_enable_direct()
1303 retfunc = fgraph_array[i]->retfunc; in ftrace_graph_enable_direct()
1325 /* The cpu_boot init_task->ret_stack will never be freed */
1328 if (!idle_task(cpu)->ret_stack) in fgraph_cpu_init()
1338 int i = -1; in register_ftrace_graph()
1340 if (WARN_ONCE(gops->ops.flags & FTRACE_OPS_FL_GRAPH, in register_ftrace_graph()
1342 return -EBUSY; in register_ftrace_graph()
1351 return -ENOMEM; in register_ftrace_graph()
1374 return -ENOSPC; in register_ftrace_graph()
1375 gops->idx = i; in register_ftrace_graph()
1396 init_task_vars(gops->idx); in register_ftrace_graph()
1399 gops->saved_func = gops->entryfunc; in register_ftrace_graph()
1401 gops->ops.flags |= FTRACE_OPS_FL_GRAPH; in register_ftrace_graph()
1403 ret = ftrace_startup_subops(&graph_ops, &gops->ops, command); in register_ftrace_graph()
1409 ftrace_graph_active--; in register_ftrace_graph()
1410 gops->saved_func = NULL; in register_ftrace_graph()
1422 if (WARN_ONCE(!(gops->ops.flags & FTRACE_OPS_FL_GRAPH), in unregister_ftrace_graph()
1431 if (unlikely(gops->idx < 0 || gops->idx >= FGRAPH_ARRAY_SIZE || in unregister_ftrace_graph()
1432 fgraph_array[gops->idx] != gops)) in unregister_ftrace_graph()
1435 if (fgraph_lru_release_index(gops->idx) < 0) in unregister_ftrace_graph()
1438 fgraph_array[gops->idx] = &fgraph_stub; in unregister_ftrace_graph()
1440 ftrace_graph_active--; in unregister_ftrace_graph()
1445 ftrace_shutdown_subops(&graph_ops, &gops->ops, command); in unregister_ftrace_graph()
1458 gops->saved_func = NULL; in unregister_ftrace_graph()
1460 gops->ops.flags &= ~FTRACE_OPS_FL_GRAPH; in unregister_ftrace_graph()