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