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