Skip to content

Commit 06526da

Browse files
committed
Merge tag 'ftrace-v6.12-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull ftrace fixes from Steven Rostedt: "A couple of fixes to function graph infrastructure: - Fix allocation of idle shadow stack allocation during hotplug If function graph tracing is started when a CPU is offline, if it were come online during the trace then the idle task that represents the CPU will not get a shadow stack allocated for it. This means all function graph hooks that happen while that idle task is running (including in interrupt mode) will have all its events dropped. Switch over to the CPU hotplug mechanism that will have any newly brought on line CPU get a callback that can allocate the shadow stack for its idle task. - Fix allocation size of the ret_stack_list array When function graph tracing converted over to allowing more than one user at a time, it had to convert its shadow stack from an array of ret_stack structures to an array of unsigned longs. The shadow stacks are allocated in batches of 32 at a time and assigned to every running task. The batch is held by the ret_stack_list array. But when the conversion happened, instead of allocating an array of 32 pointers, it was allocated as a ret_stack itself (PAGE_SIZE). This ret_stack_list gets passed to a function that iterates over what it believes is its size defined by the FTRACE_RETSTACK_ALLOC_SIZE macro (which is 32). Luckily (PAGE_SIZE) is greater than 32 * sizeof(long), otherwise this would have been an array overflow. This still should be fixed and the ret_stack_list should be allocated to the size it is expected to be as someday it may end up being bigger than SHADOW_STACK_SIZE" * tag 'ftrace-v6.12-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: fgraph: Allocate ret_stack_list with proper size fgraph: Use CPU hotplug mechanism to initialize idle shadow stacks
2 parents 8203ca3 + fae4078 commit 06526da

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

kernel/trace/fgraph.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,19 +1160,14 @@ void fgraph_update_pid_func(void)
11601160
static int start_graph_tracing(void)
11611161
{
11621162
unsigned long **ret_stack_list;
1163-
int ret, cpu;
1163+
int ret;
11641164

1165-
ret_stack_list = kmalloc(SHADOW_STACK_SIZE, GFP_KERNEL);
1165+
ret_stack_list = kcalloc(FTRACE_RETSTACK_ALLOC_SIZE,
1166+
sizeof(*ret_stack_list), GFP_KERNEL);
11661167

11671168
if (!ret_stack_list)
11681169
return -ENOMEM;
11691170

1170-
/* The cpu_boot init_task->ret_stack will never be freed */
1171-
for_each_online_cpu(cpu) {
1172-
if (!idle_task(cpu)->ret_stack)
1173-
ftrace_graph_init_idle_task(idle_task(cpu), cpu);
1174-
}
1175-
11761171
do {
11771172
ret = alloc_retstack_tasklist(ret_stack_list);
11781173
} while (ret == -EAGAIN);
@@ -1242,14 +1237,34 @@ static void ftrace_graph_disable_direct(bool disable_branch)
12421237
fgraph_direct_gops = &fgraph_stub;
12431238
}
12441239

1240+
/* The cpu_boot init_task->ret_stack will never be freed */
1241+
static int fgraph_cpu_init(unsigned int cpu)
1242+
{
1243+
if (!idle_task(cpu)->ret_stack)
1244+
ftrace_graph_init_idle_task(idle_task(cpu), cpu);
1245+
return 0;
1246+
}
1247+
12451248
int register_ftrace_graph(struct fgraph_ops *gops)
12461249
{
1250+
static bool fgraph_initialized;
12471251
int command = 0;
12481252
int ret = 0;
12491253
int i = -1;
12501254

12511255
mutex_lock(&ftrace_lock);
12521256

1257+
if (!fgraph_initialized) {
1258+
ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "fgraph_idle_init",
1259+
fgraph_cpu_init, NULL);
1260+
if (ret < 0) {
1261+
pr_warn("fgraph: Error to init cpu hotplug support\n");
1262+
return ret;
1263+
}
1264+
fgraph_initialized = true;
1265+
ret = 0;
1266+
}
1267+
12531268
if (!fgraph_array[0]) {
12541269
/* The array must always have real data on it */
12551270
for (i = 0; i < FGRAPH_ARRAY_SIZE; i++)

0 commit comments

Comments
 (0)