Skip to content

Commit 963ec27

Browse files
liu-song-6Alexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: fix raw_tp test run in preempt kernel
In preempt kernel, BPF_PROG_TEST_RUN on raw_tp triggers: [ 35.874974] BUG: using smp_processor_id() in preemptible [00000000] code: new_name/87 [ 35.893983] caller is bpf_prog_test_run_raw_tp+0xd4/0x1b0 [ 35.900124] CPU: 1 PID: 87 Comm: new_name Not tainted 5.9.0-rc6-g615bd02bf #1 [ 35.907358] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014 [ 35.916941] Call Trace: [ 35.919660] dump_stack+0x77/0x9b [ 35.923273] check_preemption_disabled+0xb4/0xc0 [ 35.928376] bpf_prog_test_run_raw_tp+0xd4/0x1b0 [ 35.933872] ? selinux_bpf+0xd/0x70 [ 35.937532] __do_sys_bpf+0x6bb/0x21e0 [ 35.941570] ? find_held_lock+0x2d/0x90 [ 35.945687] ? vfs_write+0x150/0x220 [ 35.949586] do_syscall_64+0x2d/0x40 [ 35.953443] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Fix this by calling migrate_disable() before smp_processor_id(). Fixes: 1b4d60e ("bpf: Enable BPF_PROG_TEST_RUN for raw_tracepoint") Reported-by: Alexei Starovoitov <[email protected]> Signed-off-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b0efc21 commit 963ec27

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

net/bpf/test_run.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,7 @@ __bpf_prog_test_run_raw_tp(void *data)
252252
struct bpf_raw_tp_test_run_info *info = data;
253253

254254
rcu_read_lock();
255-
migrate_disable();
256255
info->retval = BPF_PROG_RUN(info->prog, info->ctx);
257-
migrate_enable();
258256
rcu_read_unlock();
259257
}
260258

@@ -266,6 +264,7 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
266264
__u32 ctx_size_in = kattr->test.ctx_size_in;
267265
struct bpf_raw_tp_test_run_info info;
268266
int cpu = kattr->test.cpu, err = 0;
267+
int current_cpu;
269268

270269
/* doesn't support data_in/out, ctx_out, duration, or repeat */
271270
if (kattr->test.data_in || kattr->test.data_out ||
@@ -293,27 +292,25 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
293292

294293
info.prog = prog;
295294

295+
current_cpu = get_cpu();
296296
if ((kattr->test.flags & BPF_F_TEST_RUN_ON_CPU) == 0 ||
297-
cpu == smp_processor_id()) {
297+
cpu == current_cpu) {
298298
__bpf_prog_test_run_raw_tp(&info);
299-
} else {
299+
} else if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
300300
/* smp_call_function_single() also checks cpu_online()
301301
* after csd_lock(). However, since cpu is from user
302302
* space, let's do an extra quick check to filter out
303303
* invalid value before smp_call_function_single().
304304
*/
305-
if (cpu >= nr_cpu_ids || !cpu_online(cpu)) {
306-
err = -ENXIO;
307-
goto out;
308-
}
309-
305+
err = -ENXIO;
306+
} else {
310307
err = smp_call_function_single(cpu, __bpf_prog_test_run_raw_tp,
311308
&info, 1);
312-
if (err)
313-
goto out;
314309
}
310+
put_cpu();
315311

316-
if (copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
312+
if (!err &&
313+
copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
317314
err = -EFAULT;
318315

319316
out:

0 commit comments

Comments
 (0)