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