Skip to content

Commit 247ab8e

Browse files
axboeSasha Levin
authored and
Sasha Levin
committed
blktrace: fix unlocked registration of tracepoints
[ Upstream commit a6da002 ] We need to ensure that tracepoints are registered and unregistered with the users of them. The existing atomic count isn't enough for that. Add a lock around the tracepoints, so we serialize access to them. This fixes cases where we have multiple users setting up and tearing down tracepoints, like this: CPU: 0 PID: 2995 Comm: syzkaller857118 Not tainted 4.14.0-rc5-next-20171018+ #36 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Call Trace: __dump_stack lib/dump_stack.c:16 [inline] dump_stack+0x194/0x257 lib/dump_stack.c:52 panic+0x1e4/0x41c kernel/panic.c:183 __warn+0x1c4/0x1e0 kernel/panic.c:546 report_bug+0x211/0x2d0 lib/bug.c:183 fixup_bug+0x40/0x90 arch/x86/kernel/traps.c:177 do_trap_no_signal arch/x86/kernel/traps.c:211 [inline] do_trap+0x260/0x390 arch/x86/kernel/traps.c:260 do_error_trap+0x120/0x390 arch/x86/kernel/traps.c:297 do_invalid_op+0x1b/0x20 arch/x86/kernel/traps.c:310 invalid_op+0x18/0x20 arch/x86/entry/entry_64.S:905 RIP: 0010:tracepoint_add_func kernel/tracepoint.c:210 [inline] RIP: 0010:tracepoint_probe_register_prio+0x397/0x9a0 kernel/tracepoint.c:283 RSP: 0018:ffff8801d1d1f6c0 EFLAGS: 00010293 RAX: ffff8801d22e8540 RBX: 00000000ffffffef RCX: ffffffff81710f07 RDX: 0000000000000000 RSI: ffffffff85b679c0 RDI: ffff8801d5f19818 RBP: ffff8801d1d1f7c8 R08: ffffffff81710c10 R09: 0000000000000004 R10: ffff8801d1d1f6b0 R11: 0000000000000003 R12: ffffffff817597f0 R13: 0000000000000000 R14: 00000000ffffffff R15: ffff8801d1d1f7a0 tracepoint_probe_register+0x2a/0x40 kernel/tracepoint.c:304 register_trace_block_rq_insert include/trace/events/block.h:191 [inline] blk_register_tracepoints+0x1e/0x2f0 kernel/trace/blktrace.c:1043 do_blk_trace_setup+0xa10/0xcf0 kernel/trace/blktrace.c:542 blk_trace_setup+0xbd/0x180 kernel/trace/blktrace.c:564 sg_ioctl+0xc71/0x2d90 drivers/scsi/sg.c:1089 vfs_ioctl fs/ioctl.c:45 [inline] do_vfs_ioctl+0x1b1/0x1520 fs/ioctl.c:685 SYSC_ioctl fs/ioctl.c:700 [inline] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:691 entry_SYSCALL_64_fastpath+0x1f/0xbe RIP: 0033:0x444339 RSP: 002b:00007ffe05bb5b18 EFLAGS: 00000206 ORIG_RAX: 0000000000000010 RAX: ffffffffffffffda RBX: 00000000006d66c0 RCX: 0000000000444339 RDX: 000000002084cf90 RSI: 00000000c0481273 RDI: 0000000000000009 RBP: 0000000000000082 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000206 R12: ffffffffffffffff R13: 00000000c0481273 R14: 0000000000000000 R15: 0000000000000000 since we can now run these in parallel. Ensure that the exported helpers for doing this are grabbing the queue trace mutex. Reported-by: Steven Rostedt <[email protected]> Tested-by: Dmitry Vyukov <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 3f065a1 commit 247ab8e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

kernel/trace/blktrace.c

+22-10
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ static struct tracer_flags blk_tracer_flags = {
5757
};
5858

5959
/* Global reference count of probes */
60-
static atomic_t blk_probes_ref = ATOMIC_INIT(0);
60+
static DEFINE_MUTEX(blk_probe_mutex);
61+
static int blk_probes_ref;
6162

6263
static void blk_register_tracepoints(void);
6364
static void blk_unregister_tracepoints(void);
@@ -300,11 +301,26 @@ static void blk_trace_free(struct blk_trace *bt)
300301
kfree(bt);
301302
}
302303

304+
static void get_probe_ref(void)
305+
{
306+
mutex_lock(&blk_probe_mutex);
307+
if (++blk_probes_ref == 1)
308+
blk_register_tracepoints();
309+
mutex_unlock(&blk_probe_mutex);
310+
}
311+
312+
static void put_probe_ref(void)
313+
{
314+
mutex_lock(&blk_probe_mutex);
315+
if (!--blk_probes_ref)
316+
blk_unregister_tracepoints();
317+
mutex_unlock(&blk_probe_mutex);
318+
}
319+
303320
static void blk_trace_cleanup(struct blk_trace *bt)
304321
{
305322
blk_trace_free(bt);
306-
if (atomic_dec_and_test(&blk_probes_ref))
307-
blk_unregister_tracepoints();
323+
put_probe_ref();
308324
}
309325

310326
int blk_trace_remove(struct request_queue *q)
@@ -527,8 +543,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
527543
goto err;
528544
}
529545

530-
if (atomic_inc_return(&blk_probes_ref) == 1)
531-
blk_register_tracepoints();
546+
get_probe_ref();
532547

533548
return 0;
534549
err:
@@ -1474,9 +1489,7 @@ static int blk_trace_remove_queue(struct request_queue *q)
14741489
if (bt == NULL)
14751490
return -EINVAL;
14761491

1477-
if (atomic_dec_and_test(&blk_probes_ref))
1478-
blk_unregister_tracepoints();
1479-
1492+
put_probe_ref();
14801493
blk_trace_free(bt);
14811494
return 0;
14821495
}
@@ -1510,8 +1523,7 @@ static int blk_trace_setup_queue(struct request_queue *q,
15101523
goto free_bt;
15111524
}
15121525

1513-
if (atomic_inc_return(&blk_probes_ref) == 1)
1514-
blk_register_tracepoints();
1526+
get_probe_ref();
15151527
return 0;
15161528

15171529
free_bt:

0 commit comments

Comments
 (0)