]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/blktrace.c
tracing: Let tracepoints have data passed to tracepoint callbacks
[net-next-2.6.git] / kernel / trace / blktrace.c
CommitLineData
2056a782 1/*
0fe23479 2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
2056a782
JA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 */
2056a782
JA
18#include <linux/kernel.h>
19#include <linux/blkdev.h>
20#include <linux/blktrace_api.h>
21#include <linux/percpu.h>
22#include <linux/init.h>
23#include <linux/mutex.h>
5a0e3ad6 24#include <linux/slab.h>
2056a782 25#include <linux/debugfs.h>
405f5571 26#include <linux/smp_lock.h>
be1c6341 27#include <linux/time.h>
939b3669 28#include <linux/uaccess.h>
55782138
LZ
29
30#include <trace/events/block.h>
31
2db270a8 32#include "trace_output.h"
2056a782 33
55782138
LZ
34#ifdef CONFIG_BLK_DEV_IO_TRACE
35
2056a782
JA
36static unsigned int blktrace_seq __read_mostly = 1;
37
c71a8961 38static struct trace_array *blk_tr;
5006ea73 39static bool blk_tracer_enabled __read_mostly;
c71a8961
ACM
40
41/* Select an alternative, minimalistic output than the original one */
ef18012b 42#define TRACE_BLK_OPT_CLASSIC 0x1
c71a8961
ACM
43
44static struct tracer_opt blk_tracer_opts[] = {
45 /* Default disable the minimalistic output */
157f9c00 46 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
c71a8961
ACM
47 { }
48};
49
50static struct tracer_flags blk_tracer_flags = {
51 .val = 0,
52 .opts = blk_tracer_opts,
53};
54
5f3ea37c 55/* Global reference count of probes */
5f3ea37c
ACM
56static atomic_t blk_probes_ref = ATOMIC_INIT(0);
57
3c289ba7 58static void blk_register_tracepoints(void);
5f3ea37c
ACM
59static void blk_unregister_tracepoints(void);
60
be1c6341
OK
61/*
62 * Send out a notify message.
63 */
a863055b
JA
64static void trace_note(struct blk_trace *bt, pid_t pid, int action,
65 const void *data, size_t len)
be1c6341
OK
66{
67 struct blk_io_trace *t;
18cea459 68 struct ring_buffer_event *event = NULL;
e77405ad 69 struct ring_buffer *buffer = NULL;
18cea459
LZ
70 int pc = 0;
71 int cpu = smp_processor_id();
72 bool blk_tracer = blk_tracer_enabled;
73
74 if (blk_tracer) {
e77405ad 75 buffer = blk_tr->buffer;
18cea459 76 pc = preempt_count();
e77405ad 77 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
18cea459
LZ
78 sizeof(*t) + len,
79 0, pc);
80 if (!event)
81 return;
82 t = ring_buffer_event_data(event);
83 goto record_it;
84 }
be1c6341 85
c71a8961
ACM
86 if (!bt->rchan)
87 return;
88
be1c6341 89 t = relay_reserve(bt->rchan, sizeof(*t) + len);
d3d9d2a5 90 if (t) {
d3d9d2a5 91 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
2997c8c4 92 t->time = ktime_to_ns(ktime_get());
18cea459 93record_it:
d3d9d2a5
JA
94 t->device = bt->dev;
95 t->action = action;
96 t->pid = pid;
97 t->cpu = cpu;
98 t->pdu_len = len;
99 memcpy((void *) t + sizeof(*t), data, len);
18cea459
LZ
100
101 if (blk_tracer)
e77405ad 102 trace_buffer_unlock_commit(buffer, event, 0, pc);
d3d9d2a5 103 }
be1c6341
OK
104}
105
2056a782
JA
106/*
107 * Send out a notify for this process, if we haven't done so since a trace
108 * started
109 */
110static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
111{
a863055b
JA
112 tsk->btrace_seq = blktrace_seq;
113 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
be1c6341 114}
2056a782 115
be1c6341
OK
116static void trace_note_time(struct blk_trace *bt)
117{
118 struct timespec now;
119 unsigned long flags;
120 u32 words[2];
121
122 getnstimeofday(&now);
123 words[0] = now.tv_sec;
124 words[1] = now.tv_nsec;
125
126 local_irq_save(flags);
127 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
128 local_irq_restore(flags);
2056a782
JA
129}
130
9d5f09a4
AB
131void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
132{
133 int n;
134 va_list args;
14a73f54 135 unsigned long flags;
64565911 136 char *buf;
9d5f09a4 137
18cea459
LZ
138 if (unlikely(bt->trace_state != Blktrace_running &&
139 !blk_tracer_enabled))
c71a8961
ACM
140 return;
141
14a73f54 142 local_irq_save(flags);
64565911 143 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
9d5f09a4 144 va_start(args, fmt);
64565911 145 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
9d5f09a4
AB
146 va_end(args);
147
64565911 148 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
14a73f54 149 local_irq_restore(flags);
9d5f09a4
AB
150}
151EXPORT_SYMBOL_GPL(__trace_note_message);
152
2056a782
JA
153static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
154 pid_t pid)
155{
156 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
157 return 1;
d0deef5b 158 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
2056a782
JA
159 return 1;
160 if (bt->pid && pid != bt->pid)
161 return 1;
162
163 return 0;
164}
165
166/*
167 * Data direction bit lookup
168 */
e4955c99
LZ
169static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
170 BLK_TC_ACT(BLK_TC_WRITE) };
2056a782 171
35ba8f70 172/* The ilog2() calls fall out because they're constant */
939b3669
ACM
173#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
174 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
2056a782
JA
175
176/*
177 * The worker for the various blk_add_trace*() types. Fills out a
178 * blk_io_trace structure and places it in a per-cpu subbuffer.
179 */
5f3ea37c 180static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
2056a782
JA
181 int rw, u32 what, int error, int pdu_len, void *pdu_data)
182{
183 struct task_struct *tsk = current;
c71a8961 184 struct ring_buffer_event *event = NULL;
e77405ad 185 struct ring_buffer *buffer = NULL;
2056a782 186 struct blk_io_trace *t;
0a987751 187 unsigned long flags = 0;
2056a782
JA
188 unsigned long *sequence;
189 pid_t pid;
c71a8961 190 int cpu, pc = 0;
18cea459 191 bool blk_tracer = blk_tracer_enabled;
2056a782 192
18cea459 193 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
2056a782
JA
194 return;
195
196 what |= ddir_act[rw & WRITE];
35ba8f70 197 what |= MASK_TC_BIT(rw, BARRIER);
93dbb393 198 what |= MASK_TC_BIT(rw, SYNCIO);
35ba8f70
DW
199 what |= MASK_TC_BIT(rw, AHEAD);
200 what |= MASK_TC_BIT(rw, META);
201 what |= MASK_TC_BIT(rw, DISCARD);
2056a782
JA
202
203 pid = tsk->pid;
d0deef5b 204 if (act_log_check(bt, what, sector, pid))
2056a782 205 return;
c71a8961
ACM
206 cpu = raw_smp_processor_id();
207
18cea459 208 if (blk_tracer) {
c71a8961
ACM
209 tracing_record_cmdline(current);
210
e77405ad 211 buffer = blk_tr->buffer;
51a763dd 212 pc = preempt_count();
e77405ad 213 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
51a763dd
ACM
214 sizeof(*t) + pdu_len,
215 0, pc);
c71a8961
ACM
216 if (!event)
217 return;
51a763dd 218 t = ring_buffer_event_data(event);
c71a8961
ACM
219 goto record_it;
220 }
2056a782
JA
221
222 /*
223 * A word about the locking here - we disable interrupts to reserve
224 * some space in the relay per-cpu buffer, to prevent an irq
14a73f54 225 * from coming in and stepping on our toes.
2056a782
JA
226 */
227 local_irq_save(flags);
228
229 if (unlikely(tsk->btrace_seq != blktrace_seq))
230 trace_note_tsk(bt, tsk);
231
232 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
233 if (t) {
2056a782
JA
234 sequence = per_cpu_ptr(bt->sequence, cpu);
235
236 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
237 t->sequence = ++(*sequence);
2997c8c4 238 t->time = ktime_to_ns(ktime_get());
c71a8961 239record_it:
08a06b83 240 /*
939b3669
ACM
241 * These two are not needed in ftrace as they are in the
242 * generic trace_entry, filled by tracing_generic_entry_update,
243 * but for the trace_event->bin() synthesizer benefit we do it
244 * here too.
245 */
246 t->cpu = cpu;
247 t->pid = pid;
08a06b83 248
2056a782
JA
249 t->sector = sector;
250 t->bytes = bytes;
251 t->action = what;
2056a782 252 t->device = bt->dev;
2056a782
JA
253 t->error = error;
254 t->pdu_len = pdu_len;
255
256 if (pdu_len)
257 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
c71a8961 258
18cea459 259 if (blk_tracer) {
e77405ad 260 trace_buffer_unlock_commit(buffer, event, 0, pc);
c71a8961
ACM
261 return;
262 }
2056a782
JA
263 }
264
265 local_irq_restore(flags);
266}
267
2056a782 268static struct dentry *blk_tree_root;
11a57153 269static DEFINE_MUTEX(blk_tree_mutex);
2056a782 270
ad5dd549 271static void blk_trace_free(struct blk_trace *bt)
2056a782 272{
02c62304 273 debugfs_remove(bt->msg_file);
2056a782 274 debugfs_remove(bt->dropped_file);
f48fc4d3 275 relay_close(bt->rchan);
39cbb602 276 debugfs_remove(bt->dir);
2056a782 277 free_percpu(bt->sequence);
64565911 278 free_percpu(bt->msg_data);
2056a782 279 kfree(bt);
ad5dd549
LZ
280}
281
282static void blk_trace_cleanup(struct blk_trace *bt)
283{
284 blk_trace_free(bt);
5f3ea37c
ACM
285 if (atomic_dec_and_test(&blk_probes_ref))
286 blk_unregister_tracepoints();
2056a782
JA
287}
288
6da127ad 289int blk_trace_remove(struct request_queue *q)
2056a782
JA
290{
291 struct blk_trace *bt;
292
293 bt = xchg(&q->blk_trace, NULL);
294 if (!bt)
295 return -EINVAL;
296
55547204 297 if (bt->trace_state != Blktrace_running)
2056a782
JA
298 blk_trace_cleanup(bt);
299
300 return 0;
301}
6da127ad 302EXPORT_SYMBOL_GPL(blk_trace_remove);
2056a782
JA
303
304static int blk_dropped_open(struct inode *inode, struct file *filp)
305{
8e18e294 306 filp->private_data = inode->i_private;
2056a782
JA
307
308 return 0;
309}
310
311static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
312 size_t count, loff_t *ppos)
313{
314 struct blk_trace *bt = filp->private_data;
315 char buf[16];
316
317 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
318
319 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
320}
321
2b8693c0 322static const struct file_operations blk_dropped_fops = {
2056a782
JA
323 .owner = THIS_MODULE,
324 .open = blk_dropped_open,
325 .read = blk_dropped_read,
326};
327
02c62304
AB
328static int blk_msg_open(struct inode *inode, struct file *filp)
329{
330 filp->private_data = inode->i_private;
331
332 return 0;
333}
334
335static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
336 size_t count, loff_t *ppos)
337{
338 char *msg;
339 struct blk_trace *bt;
340
7635b03a 341 if (count >= BLK_TN_MAX_MSG)
02c62304
AB
342 return -EINVAL;
343
a4b3ada8 344 msg = kmalloc(count + 1, GFP_KERNEL);
02c62304
AB
345 if (msg == NULL)
346 return -ENOMEM;
347
348 if (copy_from_user(msg, buffer, count)) {
349 kfree(msg);
350 return -EFAULT;
351 }
352
a4b3ada8 353 msg[count] = '\0';
02c62304
AB
354 bt = filp->private_data;
355 __trace_note_message(bt, "%s", msg);
356 kfree(msg);
357
358 return count;
359}
360
361static const struct file_operations blk_msg_fops = {
362 .owner = THIS_MODULE,
363 .open = blk_msg_open,
364 .write = blk_msg_write,
365};
366
2056a782
JA
367/*
368 * Keep track of how many times we encountered a full subbuffer, to aid
369 * the user space app in telling how many lost events there were.
370 */
371static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
372 void *prev_subbuf, size_t prev_padding)
373{
374 struct blk_trace *bt;
375
376 if (!relay_buf_full(buf))
377 return 1;
378
379 bt = buf->chan->private_data;
380 atomic_inc(&bt->dropped);
381 return 0;
382}
383
384static int blk_remove_buf_file_callback(struct dentry *dentry)
385{
386 debugfs_remove(dentry);
f48fc4d3 387
2056a782
JA
388 return 0;
389}
390
391static struct dentry *blk_create_buf_file_callback(const char *filename,
392 struct dentry *parent,
393 int mode,
394 struct rchan_buf *buf,
395 int *is_global)
396{
397 return debugfs_create_file(filename, mode, parent, buf,
398 &relay_file_operations);
399}
400
401static struct rchan_callbacks blk_relay_callbacks = {
402 .subbuf_start = blk_subbuf_start_callback,
403 .create_buf_file = blk_create_buf_file_callback,
404 .remove_buf_file = blk_remove_buf_file_callback,
405};
406
9908c309
LZ
407static void blk_trace_setup_lba(struct blk_trace *bt,
408 struct block_device *bdev)
409{
410 struct hd_struct *part = NULL;
411
412 if (bdev)
413 part = bdev->bd_part;
414
415 if (part) {
416 bt->start_lba = part->start_sect;
417 bt->end_lba = part->start_sect + part->nr_sects;
418 } else {
419 bt->start_lba = 0;
420 bt->end_lba = -1ULL;
421 }
422}
423
2056a782
JA
424/*
425 * Setup everything required to start tracing
426 */
6da127ad 427int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
d0deef5b
SD
428 struct block_device *bdev,
429 struct blk_user_trace_setup *buts)
2056a782 430{
2056a782
JA
431 struct blk_trace *old_bt, *bt = NULL;
432 struct dentry *dir = NULL;
2056a782
JA
433 int ret, i;
434
171044d4 435 if (!buts->buf_size || !buts->buf_nr)
2056a782
JA
436 return -EINVAL;
437
0497b345
JA
438 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
439 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
2056a782
JA
440
441 /*
442 * some device names have larger paths - convert the slashes
443 * to underscores for this to work as expected
444 */
171044d4
AB
445 for (i = 0; i < strlen(buts->name); i++)
446 if (buts->name[i] == '/')
447 buts->name[i] = '_';
2056a782 448
2056a782
JA
449 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
450 if (!bt)
ad5dd549 451 return -ENOMEM;
2056a782 452
ad5dd549 453 ret = -ENOMEM;
2056a782
JA
454 bt->sequence = alloc_percpu(unsigned long);
455 if (!bt->sequence)
456 goto err;
457
313e458f 458 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
64565911
JA
459 if (!bt->msg_data)
460 goto err;
461
2056a782 462 ret = -ENOENT;
f48fc4d3 463
b5230b56 464 mutex_lock(&blk_tree_mutex);
f48fc4d3
JA
465 if (!blk_tree_root) {
466 blk_tree_root = debugfs_create_dir("block", NULL);
b5230b56
LZ
467 if (!blk_tree_root) {
468 mutex_unlock(&blk_tree_mutex);
1a17662e 469 goto err;
b5230b56 470 }
f48fc4d3 471 }
b5230b56 472 mutex_unlock(&blk_tree_mutex);
f48fc4d3
JA
473
474 dir = debugfs_create_dir(buts->name, blk_tree_root);
475
2056a782
JA
476 if (!dir)
477 goto err;
478
479 bt->dir = dir;
6da127ad 480 bt->dev = dev;
2056a782
JA
481 atomic_set(&bt->dropped, 0);
482
483 ret = -EIO;
939b3669
ACM
484 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
485 &blk_dropped_fops);
2056a782
JA
486 if (!bt->dropped_file)
487 goto err;
488
02c62304
AB
489 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
490 if (!bt->msg_file)
491 goto err;
492
171044d4
AB
493 bt->rchan = relay_open("trace", dir, buts->buf_size,
494 buts->buf_nr, &blk_relay_callbacks, bt);
2056a782
JA
495 if (!bt->rchan)
496 goto err;
2056a782 497
171044d4 498 bt->act_mask = buts->act_mask;
2056a782
JA
499 if (!bt->act_mask)
500 bt->act_mask = (u16) -1;
501
9908c309 502 blk_trace_setup_lba(bt, bdev);
2056a782 503
d0deef5b
SD
504 /* overwrite with user settings */
505 if (buts->start_lba)
506 bt->start_lba = buts->start_lba;
507 if (buts->end_lba)
508 bt->end_lba = buts->end_lba;
509
171044d4 510 bt->pid = buts->pid;
2056a782
JA
511 bt->trace_state = Blktrace_setup;
512
513 ret = -EBUSY;
514 old_bt = xchg(&q->blk_trace, bt);
515 if (old_bt) {
516 (void) xchg(&q->blk_trace, old_bt);
517 goto err;
518 }
519
17ba97e3 520 if (atomic_inc_return(&blk_probes_ref) == 1)
cbe28296
LZ
521 blk_register_tracepoints();
522
2056a782
JA
523 return 0;
524err:
ad5dd549 525 blk_trace_free(bt);
2056a782
JA
526 return ret;
527}
171044d4 528
6da127ad 529int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
d0deef5b 530 struct block_device *bdev,
6da127ad 531 char __user *arg)
171044d4
AB
532{
533 struct blk_user_trace_setup buts;
534 int ret;
535
536 ret = copy_from_user(&buts, arg, sizeof(buts));
537 if (ret)
538 return -EFAULT;
539
d0deef5b 540 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
171044d4
AB
541 if (ret)
542 return ret;
543
9a8c28c8
DM
544 if (copy_to_user(arg, &buts, sizeof(buts))) {
545 blk_trace_remove(q);
171044d4 546 return -EFAULT;
9a8c28c8 547 }
171044d4
AB
548 return 0;
549}
6da127ad 550EXPORT_SYMBOL_GPL(blk_trace_setup);
2056a782 551
6da127ad 552int blk_trace_startstop(struct request_queue *q, int start)
2056a782 553{
2056a782 554 int ret;
939b3669 555 struct blk_trace *bt = q->blk_trace;
2056a782 556
939b3669 557 if (bt == NULL)
2056a782
JA
558 return -EINVAL;
559
560 /*
561 * For starting a trace, we can transition from a setup or stopped
562 * trace. For stopping a trace, the state must be running
563 */
564 ret = -EINVAL;
565 if (start) {
566 if (bt->trace_state == Blktrace_setup ||
567 bt->trace_state == Blktrace_stopped) {
568 blktrace_seq++;
569 smp_mb();
570 bt->trace_state = Blktrace_running;
be1c6341
OK
571
572 trace_note_time(bt);
2056a782
JA
573 ret = 0;
574 }
575 } else {
576 if (bt->trace_state == Blktrace_running) {
577 bt->trace_state = Blktrace_stopped;
578 relay_flush(bt->rchan);
579 ret = 0;
580 }
581 }
582
583 return ret;
584}
6da127ad 585EXPORT_SYMBOL_GPL(blk_trace_startstop);
2056a782
JA
586
587/**
588 * blk_trace_ioctl: - handle the ioctls associated with tracing
589 * @bdev: the block device
ef18012b 590 * @cmd: the ioctl cmd
2056a782
JA
591 * @arg: the argument data, if any
592 *
593 **/
594int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
595{
165125e1 596 struct request_queue *q;
2056a782 597 int ret, start = 0;
6da127ad 598 char b[BDEVNAME_SIZE];
2056a782
JA
599
600 q = bdev_get_queue(bdev);
601 if (!q)
602 return -ENXIO;
603
604 mutex_lock(&bdev->bd_mutex);
605
606 switch (cmd) {
607 case BLKTRACESETUP:
f36f21ec 608 bdevname(bdev, b);
d0deef5b 609 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
2056a782
JA
610 break;
611 case BLKTRACESTART:
612 start = 1;
613 case BLKTRACESTOP:
614 ret = blk_trace_startstop(q, start);
615 break;
616 case BLKTRACETEARDOWN:
617 ret = blk_trace_remove(q);
618 break;
619 default:
620 ret = -ENOTTY;
621 break;
622 }
623
624 mutex_unlock(&bdev->bd_mutex);
625 return ret;
626}
627
628/**
629 * blk_trace_shutdown: - stop and cleanup trace structures
630 * @q: the request queue associated with the device
631 *
632 **/
165125e1 633void blk_trace_shutdown(struct request_queue *q)
2056a782 634{
6c5c9341
AD
635 if (q->blk_trace) {
636 blk_trace_startstop(q, 0);
637 blk_trace_remove(q);
638 }
2056a782 639}
5f3ea37c
ACM
640
641/*
642 * blktrace probes
643 */
644
645/**
646 * blk_add_trace_rq - Add a trace for a request oriented action
647 * @q: queue the io is for
648 * @rq: the source request
649 * @what: the action
650 *
651 * Description:
652 * Records an action against a request. Will log the bio offset + size.
653 *
654 **/
655static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
656 u32 what)
657{
658 struct blk_trace *bt = q->blk_trace;
659 int rw = rq->cmd_flags & 0x03;
660
661 if (likely(!bt))
662 return;
663
664 if (blk_discard_rq(rq))
665 rw |= (1 << BIO_RW_DISCARD);
666
667 if (blk_pc_request(rq)) {
668 what |= BLK_TC_ACT(BLK_TC_PC);
2e46e8b2
TH
669 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
670 what, rq->errors, rq->cmd_len, rq->cmd);
5f3ea37c
ACM
671 } else {
672 what |= BLK_TC_ACT(BLK_TC_FS);
2e46e8b2
TH
673 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
674 what, rq->errors, 0, NULL);
5f3ea37c
ACM
675 }
676}
677
38516ab5
SR
678static void blk_add_trace_rq_abort(void *ignore,
679 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
680{
681 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
682}
683
38516ab5
SR
684static void blk_add_trace_rq_insert(void *ignore,
685 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
686{
687 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
688}
689
38516ab5
SR
690static void blk_add_trace_rq_issue(void *ignore,
691 struct request_queue *q, struct request *rq)
5f3ea37c
ACM
692{
693 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
694}
695
38516ab5
SR
696static void blk_add_trace_rq_requeue(void *ignore,
697 struct request_queue *q,
939b3669 698 struct request *rq)
5f3ea37c
ACM
699{
700 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
701}
702
38516ab5
SR
703static void blk_add_trace_rq_complete(void *ignore,
704 struct request_queue *q,
939b3669 705 struct request *rq)
5f3ea37c
ACM
706{
707 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
708}
709
710/**
711 * blk_add_trace_bio - Add a trace for a bio oriented action
712 * @q: queue the io is for
713 * @bio: the source bio
714 * @what: the action
715 *
716 * Description:
717 * Records an action against a bio. Will log the bio offset + size.
718 *
719 **/
720static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
721 u32 what)
722{
723 struct blk_trace *bt = q->blk_trace;
724
725 if (likely(!bt))
726 return;
727
728 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
729 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
730}
731
38516ab5
SR
732static void blk_add_trace_bio_bounce(void *ignore,
733 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
734{
735 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
736}
737
38516ab5
SR
738static void blk_add_trace_bio_complete(void *ignore,
739 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
740{
741 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
742}
743
38516ab5
SR
744static void blk_add_trace_bio_backmerge(void *ignore,
745 struct request_queue *q,
939b3669 746 struct bio *bio)
5f3ea37c
ACM
747{
748 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
749}
750
38516ab5
SR
751static void blk_add_trace_bio_frontmerge(void *ignore,
752 struct request_queue *q,
939b3669 753 struct bio *bio)
5f3ea37c
ACM
754{
755 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
756}
757
38516ab5
SR
758static void blk_add_trace_bio_queue(void *ignore,
759 struct request_queue *q, struct bio *bio)
5f3ea37c
ACM
760{
761 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
762}
763
38516ab5
SR
764static void blk_add_trace_getrq(void *ignore,
765 struct request_queue *q,
939b3669 766 struct bio *bio, int rw)
5f3ea37c
ACM
767{
768 if (bio)
769 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
770 else {
771 struct blk_trace *bt = q->blk_trace;
772
773 if (bt)
774 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
775 }
776}
777
778
38516ab5
SR
779static void blk_add_trace_sleeprq(void *ignore,
780 struct request_queue *q,
939b3669 781 struct bio *bio, int rw)
5f3ea37c
ACM
782{
783 if (bio)
784 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
785 else {
786 struct blk_trace *bt = q->blk_trace;
787
788 if (bt)
939b3669
ACM
789 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
790 0, 0, NULL);
5f3ea37c
ACM
791 }
792}
793
38516ab5 794static void blk_add_trace_plug(void *ignore, struct request_queue *q)
5f3ea37c
ACM
795{
796 struct blk_trace *bt = q->blk_trace;
797
798 if (bt)
799 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
800}
801
38516ab5 802static void blk_add_trace_unplug_io(void *ignore, struct request_queue *q)
5f3ea37c
ACM
803{
804 struct blk_trace *bt = q->blk_trace;
805
806 if (bt) {
807 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
808 __be64 rpdu = cpu_to_be64(pdu);
809
810 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
811 sizeof(rpdu), &rpdu);
812 }
813}
814
38516ab5 815static void blk_add_trace_unplug_timer(void *ignore, struct request_queue *q)
5f3ea37c
ACM
816{
817 struct blk_trace *bt = q->blk_trace;
818
819 if (bt) {
820 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
821 __be64 rpdu = cpu_to_be64(pdu);
822
823 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
824 sizeof(rpdu), &rpdu);
825 }
826}
827
38516ab5
SR
828static void blk_add_trace_split(void *ignore,
829 struct request_queue *q, struct bio *bio,
5f3ea37c
ACM
830 unsigned int pdu)
831{
832 struct blk_trace *bt = q->blk_trace;
833
834 if (bt) {
835 __be64 rpdu = cpu_to_be64(pdu);
836
837 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
838 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
839 sizeof(rpdu), &rpdu);
840 }
841}
842
843/**
844 * blk_add_trace_remap - Add a trace for a remap operation
845 * @q: queue the io is for
846 * @bio: the source bio
847 * @dev: target device
a42aaa3b 848 * @from: source sector
5f3ea37c
ACM
849 *
850 * Description:
851 * Device mapper or raid target sometimes need to split a bio because
852 * it spans a stripe (or similar). Add a trace for that action.
853 *
854 **/
38516ab5
SR
855static void blk_add_trace_remap(void *ignore,
856 struct request_queue *q, struct bio *bio,
857 dev_t dev, sector_t from)
5f3ea37c
ACM
858{
859 struct blk_trace *bt = q->blk_trace;
860 struct blk_io_trace_remap r;
861
862 if (likely(!bt))
863 return;
864
a42aaa3b
AB
865 r.device_from = cpu_to_be32(dev);
866 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
867 r.sector_from = cpu_to_be64(from);
5f3ea37c 868
22a7c31a
AB
869 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
870 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
871 sizeof(r), &r);
5f3ea37c
ACM
872}
873
b0da3f0d
JN
874/**
875 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
876 * @q: queue the io is for
877 * @rq: the source request
878 * @dev: target device
879 * @from: source sector
880 *
881 * Description:
882 * Device mapper remaps request to other devices.
883 * Add a trace for that action.
884 *
885 **/
38516ab5
SR
886static void blk_add_trace_rq_remap(void *ignore,
887 struct request_queue *q,
b0da3f0d
JN
888 struct request *rq, dev_t dev,
889 sector_t from)
890{
891 struct blk_trace *bt = q->blk_trace;
892 struct blk_io_trace_remap r;
893
894 if (likely(!bt))
895 return;
896
897 r.device_from = cpu_to_be32(dev);
898 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
899 r.sector_from = cpu_to_be64(from);
900
901 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
902 rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
903 sizeof(r), &r);
904}
905
5f3ea37c
ACM
906/**
907 * blk_add_driver_data - Add binary message with driver-specific data
908 * @q: queue the io is for
909 * @rq: io request
910 * @data: driver-specific data
911 * @len: length of driver-specific data
912 *
913 * Description:
914 * Some drivers might want to write driver-specific data per request.
915 *
916 **/
917void blk_add_driver_data(struct request_queue *q,
918 struct request *rq,
919 void *data, size_t len)
920{
921 struct blk_trace *bt = q->blk_trace;
922
923 if (likely(!bt))
924 return;
925
926 if (blk_pc_request(rq))
2e46e8b2
TH
927 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
928 BLK_TA_DRV_DATA, rq->errors, len, data);
5f3ea37c 929 else
2e46e8b2
TH
930 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
931 BLK_TA_DRV_DATA, rq->errors, len, data);
5f3ea37c
ACM
932}
933EXPORT_SYMBOL_GPL(blk_add_driver_data);
934
3c289ba7 935static void blk_register_tracepoints(void)
5f3ea37c
ACM
936{
937 int ret;
938
38516ab5 939 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
5f3ea37c 940 WARN_ON(ret);
38516ab5 941 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
5f3ea37c 942 WARN_ON(ret);
38516ab5 943 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
5f3ea37c 944 WARN_ON(ret);
38516ab5 945 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
5f3ea37c 946 WARN_ON(ret);
38516ab5 947 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
5f3ea37c 948 WARN_ON(ret);
38516ab5 949 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
5f3ea37c 950 WARN_ON(ret);
38516ab5 951 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
5f3ea37c 952 WARN_ON(ret);
38516ab5 953 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
5f3ea37c 954 WARN_ON(ret);
38516ab5 955 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
5f3ea37c 956 WARN_ON(ret);
38516ab5 957 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
5f3ea37c 958 WARN_ON(ret);
38516ab5 959 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
5f3ea37c 960 WARN_ON(ret);
38516ab5 961 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
5f3ea37c 962 WARN_ON(ret);
38516ab5 963 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
5f3ea37c 964 WARN_ON(ret);
38516ab5 965 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
5f3ea37c 966 WARN_ON(ret);
38516ab5 967 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
5f3ea37c 968 WARN_ON(ret);
38516ab5 969 ret = register_trace_block_split(blk_add_trace_split, NULL);
5f3ea37c 970 WARN_ON(ret);
38516ab5 971 ret = register_trace_block_remap(blk_add_trace_remap, NULL);
5f3ea37c 972 WARN_ON(ret);
38516ab5 973 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
b0da3f0d 974 WARN_ON(ret);
5f3ea37c
ACM
975}
976
977static void blk_unregister_tracepoints(void)
978{
38516ab5
SR
979 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
980 unregister_trace_block_remap(blk_add_trace_remap, NULL);
981 unregister_trace_block_split(blk_add_trace_split, NULL);
982 unregister_trace_block_unplug_io(blk_add_trace_unplug_io, NULL);
983 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer, NULL);
984 unregister_trace_block_plug(blk_add_trace_plug, NULL);
985 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
986 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
987 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
988 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
989 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
990 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
991 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
992 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
993 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
994 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
995 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
996 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
5f3ea37c
ACM
997
998 tracepoint_synchronize_unregister();
999}
c71a8961
ACM
1000
1001/*
1002 * struct blk_io_tracer formatting routines
1003 */
1004
1005static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1006{
157f9c00 1007 int i = 0;
65796348 1008 int tc = t->action >> BLK_TC_SHIFT;
157f9c00 1009
18cea459
LZ
1010 if (t->action == BLK_TN_MESSAGE) {
1011 rwbs[i++] = 'N';
1012 goto out;
1013 }
1014
65796348 1015 if (tc & BLK_TC_DISCARD)
157f9c00 1016 rwbs[i++] = 'D';
65796348 1017 else if (tc & BLK_TC_WRITE)
157f9c00
ACM
1018 rwbs[i++] = 'W';
1019 else if (t->bytes)
1020 rwbs[i++] = 'R';
1021 else
1022 rwbs[i++] = 'N';
1023
65796348 1024 if (tc & BLK_TC_AHEAD)
157f9c00 1025 rwbs[i++] = 'A';
65796348 1026 if (tc & BLK_TC_BARRIER)
157f9c00 1027 rwbs[i++] = 'B';
65796348 1028 if (tc & BLK_TC_SYNC)
157f9c00 1029 rwbs[i++] = 'S';
65796348 1030 if (tc & BLK_TC_META)
157f9c00 1031 rwbs[i++] = 'M';
18cea459 1032out:
157f9c00 1033 rwbs[i] = '\0';
c71a8961
ACM
1034}
1035
1036static inline
1037const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1038{
1039 return (const struct blk_io_trace *)ent;
1040}
1041
1042static inline const void *pdu_start(const struct trace_entry *ent)
1043{
1044 return te_blk_io_trace(ent) + 1;
1045}
1046
66de7792
LZ
1047static inline u32 t_action(const struct trace_entry *ent)
1048{
1049 return te_blk_io_trace(ent)->action;
1050}
1051
1052static inline u32 t_bytes(const struct trace_entry *ent)
1053{
1054 return te_blk_io_trace(ent)->bytes;
1055}
1056
c71a8961
ACM
1057static inline u32 t_sec(const struct trace_entry *ent)
1058{
1059 return te_blk_io_trace(ent)->bytes >> 9;
1060}
1061
1062static inline unsigned long long t_sector(const struct trace_entry *ent)
1063{
1064 return te_blk_io_trace(ent)->sector;
1065}
1066
1067static inline __u16 t_error(const struct trace_entry *ent)
1068{
e0dc81be 1069 return te_blk_io_trace(ent)->error;
c71a8961
ACM
1070}
1071
1072static __u64 get_pdu_int(const struct trace_entry *ent)
1073{
1074 const __u64 *val = pdu_start(ent);
1075 return be64_to_cpu(*val);
1076}
1077
1078static void get_pdu_remap(const struct trace_entry *ent,
1079 struct blk_io_trace_remap *r)
1080{
1081 const struct blk_io_trace_remap *__r = pdu_start(ent);
a42aaa3b 1082 __u64 sector_from = __r->sector_from;
c71a8961 1083
c71a8961 1084 r->device_from = be32_to_cpu(__r->device_from);
a42aaa3b
AB
1085 r->device_to = be32_to_cpu(__r->device_to);
1086 r->sector_from = be64_to_cpu(sector_from);
c71a8961
ACM
1087}
1088
b6a4b0c3
LZ
1089typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1090
1091static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
c71a8961
ACM
1092{
1093 char rwbs[6];
35ac51bf
LZ
1094 unsigned long long ts = iter->ts;
1095 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
c71a8961 1096 unsigned secs = (unsigned long)ts;
b6a4b0c3 1097 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
c71a8961
ACM
1098
1099 fill_rwbs(rwbs, t);
1100
1101 return trace_seq_printf(&iter->seq,
35ac51bf 1102 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
c71a8961 1103 MAJOR(t->device), MINOR(t->device), iter->cpu,
b6a4b0c3 1104 secs, nsec_rem, iter->ent->pid, act, rwbs);
c71a8961
ACM
1105}
1106
b6a4b0c3 1107static int blk_log_action(struct trace_iterator *iter, const char *act)
c71a8961
ACM
1108{
1109 char rwbs[6];
b6a4b0c3
LZ
1110 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1111
c71a8961 1112 fill_rwbs(rwbs, t);
b6a4b0c3 1113 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
c71a8961
ACM
1114 MAJOR(t->device), MINOR(t->device), act, rwbs);
1115}
1116
66de7792
LZ
1117static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1118{
04986257 1119 const unsigned char *pdu_buf;
66de7792
LZ
1120 int pdu_len;
1121 int i, end, ret;
1122
1123 pdu_buf = pdu_start(ent);
1124 pdu_len = te_blk_io_trace(ent)->pdu_len;
1125
1126 if (!pdu_len)
1127 return 1;
1128
1129 /* find the last zero that needs to be printed */
1130 for (end = pdu_len - 1; end >= 0; end--)
1131 if (pdu_buf[end])
1132 break;
1133 end++;
1134
1135 if (!trace_seq_putc(s, '('))
1136 return 0;
1137
1138 for (i = 0; i < pdu_len; i++) {
1139
1140 ret = trace_seq_printf(s, "%s%02x",
1141 i == 0 ? "" : " ", pdu_buf[i]);
1142 if (!ret)
1143 return ret;
1144
1145 /*
1146 * stop when the rest is just zeroes and indicate so
1147 * with a ".." appended
1148 */
1149 if (i == end && end != pdu_len - 1)
1150 return trace_seq_puts(s, " ..) ");
1151 }
1152
1153 return trace_seq_puts(s, ") ");
1154}
1155
c71a8961
ACM
1156static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1157{
4ca53085
SR
1158 char cmd[TASK_COMM_LEN];
1159
1160 trace_find_cmdline(ent->pid, cmd);
c71a8961 1161
66de7792
LZ
1162 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1163 int ret;
1164
1165 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1166 if (!ret)
1167 return 0;
1168 ret = blk_log_dump_pdu(s, ent);
1169 if (!ret)
1170 return 0;
1171 return trace_seq_printf(s, "[%s]\n", cmd);
1172 } else {
1173 if (t_sec(ent))
1174 return trace_seq_printf(s, "%llu + %u [%s]\n",
1175 t_sector(ent), t_sec(ent), cmd);
1176 return trace_seq_printf(s, "[%s]\n", cmd);
1177 }
c71a8961
ACM
1178}
1179
157f9c00
ACM
1180static int blk_log_with_error(struct trace_seq *s,
1181 const struct trace_entry *ent)
c71a8961 1182{
66de7792
LZ
1183 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1184 int ret;
1185
1186 ret = blk_log_dump_pdu(s, ent);
1187 if (ret)
1188 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1189 return 0;
1190 } else {
1191 if (t_sec(ent))
1192 return trace_seq_printf(s, "%llu + %u [%d]\n",
1193 t_sector(ent),
1194 t_sec(ent), t_error(ent));
1195 return trace_seq_printf(s, "%llu [%d]\n",
1196 t_sector(ent), t_error(ent));
1197 }
c71a8961
ACM
1198}
1199
1200static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1201{
a42aaa3b 1202 struct blk_io_trace_remap r = { .device_from = 0, };
c71a8961
ACM
1203
1204 get_pdu_remap(ent, &r);
1205 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
a42aaa3b
AB
1206 t_sector(ent), t_sec(ent),
1207 MAJOR(r.device_from), MINOR(r.device_from),
1208 (unsigned long long)r.sector_from);
c71a8961
ACM
1209}
1210
1211static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1212{
4ca53085
SR
1213 char cmd[TASK_COMM_LEN];
1214
1215 trace_find_cmdline(ent->pid, cmd);
1216
1217 return trace_seq_printf(s, "[%s]\n", cmd);
c71a8961
ACM
1218}
1219
1220static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1221{
4ca53085
SR
1222 char cmd[TASK_COMM_LEN];
1223
1224 trace_find_cmdline(ent->pid, cmd);
1225
1226 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
c71a8961
ACM
1227}
1228
1229static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1230{
4ca53085
SR
1231 char cmd[TASK_COMM_LEN];
1232
1233 trace_find_cmdline(ent->pid, cmd);
1234
c71a8961 1235 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
4ca53085 1236 get_pdu_int(ent), cmd);
c71a8961
ACM
1237}
1238
18cea459
LZ
1239static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1240{
1241 int ret;
1242 const struct blk_io_trace *t = te_blk_io_trace(ent);
1243
1244 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1245 if (ret)
1246 return trace_seq_putc(s, '\n');
1247 return ret;
1248}
1249
c71a8961
ACM
1250/*
1251 * struct tracer operations
1252 */
1253
1254static void blk_tracer_print_header(struct seq_file *m)
1255{
1256 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1257 return;
1258 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1259 "# | | | | | |\n");
1260}
1261
1262static void blk_tracer_start(struct trace_array *tr)
1263{
ad5dd549 1264 blk_tracer_enabled = true;
c71a8961
ACM
1265}
1266
1267static int blk_tracer_init(struct trace_array *tr)
1268{
1269 blk_tr = tr;
1270 blk_tracer_start(tr);
c71a8961
ACM
1271 return 0;
1272}
1273
1274static void blk_tracer_stop(struct trace_array *tr)
1275{
ad5dd549 1276 blk_tracer_enabled = false;
c71a8961
ACM
1277}
1278
1279static void blk_tracer_reset(struct trace_array *tr)
1280{
c71a8961
ACM
1281 blk_tracer_stop(tr);
1282}
1283
e4955c99 1284static const struct {
c71a8961 1285 const char *act[2];
ef18012b 1286 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
e4955c99 1287} what2act[] = {
ef18012b 1288 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
c71a8961
ACM
1289 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1290 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1291 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1292 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1293 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1294 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1295 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1296 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1297 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1298 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1299 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1300 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1301 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1302 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1303};
1304
b6a4b0c3
LZ
1305static enum print_line_t print_one_line(struct trace_iterator *iter,
1306 bool classic)
c71a8961 1307{
2c9b238e 1308 struct trace_seq *s = &iter->seq;
b6a4b0c3
LZ
1309 const struct blk_io_trace *t;
1310 u16 what;
c71a8961 1311 int ret;
b6a4b0c3
LZ
1312 bool long_act;
1313 blk_log_action_t *log_action;
c71a8961 1314
b6a4b0c3
LZ
1315 t = te_blk_io_trace(iter->ent);
1316 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1317 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1318 log_action = classic ? &blk_log_action_classic : &blk_log_action;
08a06b83 1319
18cea459
LZ
1320 if (t->action == BLK_TN_MESSAGE) {
1321 ret = log_action(iter, long_act ? "message" : "m");
1322 if (ret)
1323 ret = blk_log_msg(s, iter->ent);
1324 goto out;
1325 }
1326
eb08f8eb 1327 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
b78825d6 1328 ret = trace_seq_printf(s, "Unknown action %x\n", what);
c71a8961 1329 else {
b6a4b0c3 1330 ret = log_action(iter, what2act[what].act[long_act]);
c71a8961 1331 if (ret)
2c9b238e 1332 ret = what2act[what].print(s, iter->ent);
c71a8961 1333 }
18cea459 1334out:
c71a8961
ACM
1335 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1336}
1337
b6a4b0c3
LZ
1338static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1339 int flags)
1340{
b6a4b0c3
LZ
1341 return print_one_line(iter, false);
1342}
1343
08a06b83
ACM
1344static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1345{
1346 struct trace_seq *s = &iter->seq;
1347 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1348 const int offset = offsetof(struct blk_io_trace, sector);
1349 struct blk_io_trace old = {
1350 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
6c051ce0 1351 .time = iter->ts,
08a06b83
ACM
1352 };
1353
1354 if (!trace_seq_putmem(s, &old, offset))
1355 return 0;
1356 return trace_seq_putmem(s, &t->sector,
1357 sizeof(old) - offset + t->pdu_len);
1358}
1359
ae7462b4
ACM
1360static enum print_line_t
1361blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
08a06b83
ACM
1362{
1363 return blk_trace_synthesize_old_trace(iter) ?
1364 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1365}
1366
c71a8961
ACM
1367static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1368{
c71a8961
ACM
1369 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1370 return TRACE_TYPE_UNHANDLED;
1371
b6a4b0c3 1372 return print_one_line(iter, true);
c71a8961
ACM
1373}
1374
f3948f88
LZ
1375static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1376{
1377 /* don't output context-info for blk_classic output */
1378 if (bit == TRACE_BLK_OPT_CLASSIC) {
1379 if (set)
1380 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1381 else
1382 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1383 }
1384 return 0;
1385}
1386
c71a8961
ACM
1387static struct tracer blk_tracer __read_mostly = {
1388 .name = "blk",
1389 .init = blk_tracer_init,
1390 .reset = blk_tracer_reset,
1391 .start = blk_tracer_start,
1392 .stop = blk_tracer_stop,
1393 .print_header = blk_tracer_print_header,
1394 .print_line = blk_tracer_print_line,
1395 .flags = &blk_tracer_flags,
f3948f88 1396 .set_flag = blk_tracer_set_flag,
c71a8961
ACM
1397};
1398
1399static struct trace_event trace_blk_event = {
ef18012b 1400 .type = TRACE_BLK,
c71a8961 1401 .trace = blk_trace_event_print,
08a06b83 1402 .binary = blk_trace_event_print_binary,
c71a8961
ACM
1403};
1404
1405static int __init init_blk_tracer(void)
1406{
1407 if (!register_ftrace_event(&trace_blk_event)) {
1408 pr_warning("Warning: could not register block events\n");
1409 return 1;
1410 }
1411
1412 if (register_tracer(&blk_tracer) != 0) {
1413 pr_warning("Warning: could not register the block tracer\n");
1414 unregister_ftrace_event(&trace_blk_event);
1415 return 1;
1416 }
1417
1418 return 0;
1419}
1420
1421device_initcall(init_blk_tracer);
1422
1423static int blk_trace_remove_queue(struct request_queue *q)
1424{
1425 struct blk_trace *bt;
1426
1427 bt = xchg(&q->blk_trace, NULL);
1428 if (bt == NULL)
1429 return -EINVAL;
1430
17ba97e3
LZ
1431 if (atomic_dec_and_test(&blk_probes_ref))
1432 blk_unregister_tracepoints();
1433
ad5dd549 1434 blk_trace_free(bt);
c71a8961
ACM
1435 return 0;
1436}
1437
1438/*
1439 * Setup everything required to start tracing
1440 */
9908c309
LZ
1441static int blk_trace_setup_queue(struct request_queue *q,
1442 struct block_device *bdev)
c71a8961
ACM
1443{
1444 struct blk_trace *old_bt, *bt = NULL;
18cea459 1445 int ret = -ENOMEM;
c71a8961 1446
c71a8961
ACM
1447 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1448 if (!bt)
15152e44 1449 return -ENOMEM;
c71a8961 1450
18cea459
LZ
1451 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1452 if (!bt->msg_data)
1453 goto free_bt;
1454
9908c309 1455 bt->dev = bdev->bd_dev;
c71a8961 1456 bt->act_mask = (u16)-1;
9908c309
LZ
1457
1458 blk_trace_setup_lba(bt, bdev);
c71a8961
ACM
1459
1460 old_bt = xchg(&q->blk_trace, bt);
1461 if (old_bt != NULL) {
1462 (void)xchg(&q->blk_trace, old_bt);
18cea459
LZ
1463 ret = -EBUSY;
1464 goto free_bt;
c71a8961 1465 }
15152e44 1466
17ba97e3
LZ
1467 if (atomic_inc_return(&blk_probes_ref) == 1)
1468 blk_register_tracepoints();
c71a8961 1469 return 0;
18cea459
LZ
1470
1471free_bt:
1472 blk_trace_free(bt);
1473 return ret;
c71a8961
ACM
1474}
1475
1476/*
1477 * sysfs interface to enable and configure tracing
1478 */
1479
c71a8961
ACM
1480static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1481 struct device_attribute *attr,
1482 char *buf);
1483static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1484 struct device_attribute *attr,
1485 const char *buf, size_t count);
1486#define BLK_TRACE_DEVICE_ATTR(_name) \
1487 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1488 sysfs_blk_trace_attr_show, \
1489 sysfs_blk_trace_attr_store)
1490
cd649b8b 1491static BLK_TRACE_DEVICE_ATTR(enable);
c71a8961
ACM
1492static BLK_TRACE_DEVICE_ATTR(act_mask);
1493static BLK_TRACE_DEVICE_ATTR(pid);
1494static BLK_TRACE_DEVICE_ATTR(start_lba);
1495static BLK_TRACE_DEVICE_ATTR(end_lba);
1496
1497static struct attribute *blk_trace_attrs[] = {
1498 &dev_attr_enable.attr,
1499 &dev_attr_act_mask.attr,
1500 &dev_attr_pid.attr,
1501 &dev_attr_start_lba.attr,
1502 &dev_attr_end_lba.attr,
1503 NULL
1504};
1505
1506struct attribute_group blk_trace_attr_group = {
1507 .name = "trace",
1508 .attrs = blk_trace_attrs,
1509};
1510
09341997
LZ
1511static const struct {
1512 int mask;
1513 const char *str;
1514} mask_maps[] = {
1515 { BLK_TC_READ, "read" },
1516 { BLK_TC_WRITE, "write" },
1517 { BLK_TC_BARRIER, "barrier" },
1518 { BLK_TC_SYNC, "sync" },
1519 { BLK_TC_QUEUE, "queue" },
1520 { BLK_TC_REQUEUE, "requeue" },
1521 { BLK_TC_ISSUE, "issue" },
1522 { BLK_TC_COMPLETE, "complete" },
1523 { BLK_TC_FS, "fs" },
1524 { BLK_TC_PC, "pc" },
1525 { BLK_TC_AHEAD, "ahead" },
1526 { BLK_TC_META, "meta" },
1527 { BLK_TC_DISCARD, "discard" },
1528 { BLK_TC_DRV_DATA, "drv_data" },
1529};
1530
1531static int blk_trace_str2mask(const char *str)
c71a8961 1532{
09341997 1533 int i;
c71a8961 1534 int mask = 0;
9eb85125 1535 char *buf, *s, *token;
c71a8961 1536
9eb85125
LZ
1537 buf = kstrdup(str, GFP_KERNEL);
1538 if (buf == NULL)
c71a8961 1539 return -ENOMEM;
9eb85125 1540 s = strstrip(buf);
c71a8961
ACM
1541
1542 while (1) {
09341997
LZ
1543 token = strsep(&s, ",");
1544 if (token == NULL)
c71a8961
ACM
1545 break;
1546
09341997
LZ
1547 if (*token == '\0')
1548 continue;
1549
1550 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1551 if (strcasecmp(token, mask_maps[i].str) == 0) {
1552 mask |= mask_maps[i].mask;
1553 break;
1554 }
1555 }
1556 if (i == ARRAY_SIZE(mask_maps)) {
1557 mask = -EINVAL;
1558 break;
1559 }
c71a8961 1560 }
9eb85125 1561 kfree(buf);
c71a8961
ACM
1562
1563 return mask;
1564}
1565
09341997
LZ
1566static ssize_t blk_trace_mask2str(char *buf, int mask)
1567{
1568 int i;
1569 char *p = buf;
1570
1571 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1572 if (mask & mask_maps[i].mask) {
1573 p += sprintf(p, "%s%s",
1574 (p == buf) ? "" : ",", mask_maps[i].str);
1575 }
1576 }
1577 *p++ = '\n';
1578
1579 return p - buf;
1580}
1581
b125130b
LZ
1582static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1583{
1584 if (bdev->bd_disk == NULL)
1585 return NULL;
1586
1587 return bdev_get_queue(bdev);
1588}
1589
c71a8961
ACM
1590static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1591 struct device_attribute *attr,
1592 char *buf)
1593{
1594 struct hd_struct *p = dev_to_part(dev);
1595 struct request_queue *q;
1596 struct block_device *bdev;
1597 ssize_t ret = -ENXIO;
1598
1599 lock_kernel();
1600 bdev = bdget(part_devt(p));
1601 if (bdev == NULL)
1602 goto out_unlock_kernel;
1603
b125130b 1604 q = blk_trace_get_queue(bdev);
c71a8961
ACM
1605 if (q == NULL)
1606 goto out_bdput;
b125130b 1607
c71a8961 1608 mutex_lock(&bdev->bd_mutex);
cd649b8b
LZ
1609
1610 if (attr == &dev_attr_enable) {
1611 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1612 goto out_unlock_bdev;
1613 }
1614
c71a8961
ACM
1615 if (q->blk_trace == NULL)
1616 ret = sprintf(buf, "disabled\n");
1617 else if (attr == &dev_attr_act_mask)
09341997 1618 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
c71a8961
ACM
1619 else if (attr == &dev_attr_pid)
1620 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1621 else if (attr == &dev_attr_start_lba)
1622 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1623 else if (attr == &dev_attr_end_lba)
1624 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
cd649b8b
LZ
1625
1626out_unlock_bdev:
c71a8961
ACM
1627 mutex_unlock(&bdev->bd_mutex);
1628out_bdput:
1629 bdput(bdev);
1630out_unlock_kernel:
1631 unlock_kernel();
1632 return ret;
1633}
1634
1635static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1636 struct device_attribute *attr,
1637 const char *buf, size_t count)
1638{
1639 struct block_device *bdev;
1640 struct request_queue *q;
1641 struct hd_struct *p;
1642 u64 value;
09341997 1643 ssize_t ret = -EINVAL;
c71a8961
ACM
1644
1645 if (count == 0)
1646 goto out;
1647
1648 if (attr == &dev_attr_act_mask) {
1649 if (sscanf(buf, "%llx", &value) != 1) {
1650 /* Assume it is a list of trace category names */
09341997
LZ
1651 ret = blk_trace_str2mask(buf);
1652 if (ret < 0)
c71a8961 1653 goto out;
09341997 1654 value = ret;
c71a8961
ACM
1655 }
1656 } else if (sscanf(buf, "%llu", &value) != 1)
1657 goto out;
1658
09341997
LZ
1659 ret = -ENXIO;
1660
c71a8961
ACM
1661 lock_kernel();
1662 p = dev_to_part(dev);
1663 bdev = bdget(part_devt(p));
1664 if (bdev == NULL)
1665 goto out_unlock_kernel;
1666
b125130b 1667 q = blk_trace_get_queue(bdev);
c71a8961
ACM
1668 if (q == NULL)
1669 goto out_bdput;
1670
1671 mutex_lock(&bdev->bd_mutex);
cd649b8b
LZ
1672
1673 if (attr == &dev_attr_enable) {
1674 if (value)
9908c309 1675 ret = blk_trace_setup_queue(q, bdev);
cd649b8b
LZ
1676 else
1677 ret = blk_trace_remove_queue(q);
1678 goto out_unlock_bdev;
1679 }
1680
c71a8961
ACM
1681 ret = 0;
1682 if (q->blk_trace == NULL)
9908c309 1683 ret = blk_trace_setup_queue(q, bdev);
c71a8961
ACM
1684
1685 if (ret == 0) {
1686 if (attr == &dev_attr_act_mask)
1687 q->blk_trace->act_mask = value;
1688 else if (attr == &dev_attr_pid)
1689 q->blk_trace->pid = value;
1690 else if (attr == &dev_attr_start_lba)
1691 q->blk_trace->start_lba = value;
1692 else if (attr == &dev_attr_end_lba)
1693 q->blk_trace->end_lba = value;
c71a8961 1694 }
cd649b8b
LZ
1695
1696out_unlock_bdev:
c71a8961
ACM
1697 mutex_unlock(&bdev->bd_mutex);
1698out_bdput:
1699 bdput(bdev);
1700out_unlock_kernel:
1701 unlock_kernel();
1702out:
cd649b8b 1703 return ret ? ret : count;
c71a8961 1704}
cd649b8b 1705
1d54ad6d
LZ
1706int blk_trace_init_sysfs(struct device *dev)
1707{
1708 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1709}
1710
48c0d4d4
ZK
1711void blk_trace_remove_sysfs(struct device *dev)
1712{
1713 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1714}
1715
55782138
LZ
1716#endif /* CONFIG_BLK_DEV_IO_TRACE */
1717
1718#ifdef CONFIG_EVENT_TRACING
1719
1720void blk_dump_cmd(char *buf, struct request *rq)
1721{
1722 int i, end;
1723 int len = rq->cmd_len;
1724 unsigned char *cmd = rq->cmd;
1725
1726 if (!blk_pc_request(rq)) {
1727 buf[0] = '\0';
1728 return;
1729 }
1730
1731 for (end = len - 1; end >= 0; end--)
1732 if (cmd[end])
1733 break;
1734 end++;
1735
1736 for (i = 0; i < len; i++) {
1737 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1738 if (i == end && end != len - 1) {
1739 sprintf(buf, " ..");
1740 break;
1741 }
1742 }
1743}
1744
1745void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1746{
1747 int i = 0;
1748
1749 if (rw & WRITE)
1750 rwbs[i++] = 'W';
1751 else if (rw & 1 << BIO_RW_DISCARD)
1752 rwbs[i++] = 'D';
1753 else if (bytes)
1754 rwbs[i++] = 'R';
1755 else
1756 rwbs[i++] = 'N';
1757
1758 if (rw & 1 << BIO_RW_AHEAD)
1759 rwbs[i++] = 'A';
1760 if (rw & 1 << BIO_RW_BARRIER)
1761 rwbs[i++] = 'B';
1762 if (rw & 1 << BIO_RW_SYNCIO)
1763 rwbs[i++] = 'S';
1764 if (rw & 1 << BIO_RW_META)
1765 rwbs[i++] = 'M';
1766
1767 rwbs[i] = '\0';
1768}
1769
1770void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1771{
1772 int rw = rq->cmd_flags & 0x03;
1773 int bytes;
1774
1775 if (blk_discard_rq(rq))
1776 rw |= (1 << BIO_RW_DISCARD);
1777
c9059598 1778 bytes = blk_rq_bytes(rq);
55782138
LZ
1779
1780 blk_fill_rwbs(rwbs, rw, bytes);
1781}
1782
1783#endif /* CONFIG_EVENT_TRACING */
1784