Skip to content

Commit a404d55

Browse files
jankaraaxboe
authored andcommitted
blktrace: Send BLK_TN_PROCESS events to all running traces
Currently each task sends BLK_TN_PROCESS event to the first traced device it interacts with after a new trace is started. When there are several traced devices and the task accesses more devices, this logic can result in BLK_TN_PROCESS being sent several times to some devices while it is never sent to other devices. Thus blkparse doesn't display command name when parsing some blktrace files. Fix the problem by sending BLK_TN_PROCESS event to all traced devices when a task interacts with any of them. Signed-off-by: Jan Kara <jack@suse.cz> Review-by: Jeff Moyer <jmoyer@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 5e01dc7 commit a404d55

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎include/linux/blktrace_api.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/relay.h>
66
#include <linux/compat.h>
77
#include <uapi/linux/blktrace_api.h>
8+
#include <linux/list.h>
89

910
#if defined(CONFIG_BLK_DEV_IO_TRACE)
1011

@@ -23,6 +24,7 @@ struct blk_trace {
2324
struct dentry *dir;
2425
struct dentry *dropped_file;
2526
struct dentry *msg_file;
27+
struct list_head running_list;
2628
atomic_t dropped;
2729
};
2830

‎kernel/trace/blktrace.c‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/export.h>
2727
#include <linux/time.h>
2828
#include <linux/uaccess.h>
29+
#include <linux/list.h>
2930

3031
#include <trace/events/block.h>
3132

@@ -38,6 +39,9 @@ static unsigned int blktrace_seq __read_mostly = 1;
3839
static struct trace_array *blk_tr;
3940
static bool blk_tracer_enabled __read_mostly;
4041

42+
static LIST_HEAD(running_trace_list);
43+
static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
44+
4145
/* Select an alternative, minimalistic output than the original one */
4246
#define TRACE_BLK_OPT_CLASSIC 0x1
4347

@@ -107,10 +111,18 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
107111
* Send out a notify for this process, if we haven't done so since a trace
108112
* started
109113
*/
110-
static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
114+
static void trace_note_tsk(struct task_struct *tsk)
111115
{
116+
unsigned long flags;
117+
struct blk_trace *bt;
118+
112119
tsk->btrace_seq = blktrace_seq;
113-
trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
120+
spin_lock_irqsave(&running_trace_lock, flags);
121+
list_for_each_entry(bt, &running_trace_list, running_list) {
122+
trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
123+
sizeof(tsk->comm));
124+
}
125+
spin_unlock_irqrestore(&running_trace_lock, flags);
114126
}
115127

116128
static void trace_note_time(struct blk_trace *bt)
@@ -229,16 +241,15 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
229241
goto record_it;
230242
}
231243

244+
if (unlikely(tsk->btrace_seq != blktrace_seq))
245+
trace_note_tsk(tsk);
246+
232247
/*
233248
* A word about the locking here - we disable interrupts to reserve
234249
* some space in the relay per-cpu buffer, to prevent an irq
235250
* from coming in and stepping on our toes.
236251
*/
237252
local_irq_save(flags);
238-
239-
if (unlikely(tsk->btrace_seq != blktrace_seq))
240-
trace_note_tsk(bt, tsk);
241-
242253
t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
243254
if (t) {
244255
sequence = per_cpu_ptr(bt->sequence, cpu);
@@ -477,6 +488,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
477488
bt->dir = dir;
478489
bt->dev = dev;
479490
atomic_set(&bt->dropped, 0);
491+
INIT_LIST_HEAD(&bt->running_list);
480492

481493
ret = -EIO;
482494
bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
@@ -601,13 +613,19 @@ int blk_trace_startstop(struct request_queue *q, int start)
601613
blktrace_seq++;
602614
smp_mb();
603615
bt->trace_state = Blktrace_running;
616+
spin_lock_irq(&running_trace_lock);
617+
list_add(&bt->running_list, &running_trace_list);
618+
spin_unlock_irq(&running_trace_lock);
604619

605620
trace_note_time(bt);
606621
ret = 0;
607622
}
608623
} else {
609624
if (bt->trace_state == Blktrace_running) {
610625
bt->trace_state = Blktrace_stopped;
626+
spin_lock_irq(&running_trace_lock);
627+
list_del_init(&bt->running_list);
628+
spin_unlock_irq(&running_trace_lock);
611629
relay_flush(bt->rchan);
612630
ret = 0;
613631
}
@@ -1472,6 +1490,9 @@ static int blk_trace_remove_queue(struct request_queue *q)
14721490
if (atomic_dec_and_test(&blk_probes_ref))
14731491
blk_unregister_tracepoints();
14741492

1493+
spin_lock_irq(&running_trace_lock);
1494+
list_del(&bt->running_list);
1495+
spin_unlock_irq(&running_trace_lock);
14751496
blk_trace_free(bt);
14761497
return 0;
14771498
}

0 commit comments

Comments
 (0)