xref: /openbmc/linux/kernel/trace/blktrace.c (revision a36954f5)
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  * @rq:		the source request
694  * @error:	return status to log
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 *rq, int error,
703 			     unsigned int nr_bytes, u32 what)
704 {
705 	struct blk_trace *bt = rq->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, error, 0, NULL);
717 }
718 
719 static void blk_add_trace_rq_insert(void *ignore,
720 				    struct request_queue *q, struct request *rq)
721 {
722 	blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT);
723 }
724 
725 static void blk_add_trace_rq_issue(void *ignore,
726 				   struct request_queue *q, struct request *rq)
727 {
728 	blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE);
729 }
730 
731 static void blk_add_trace_rq_requeue(void *ignore,
732 				     struct request_queue *q,
733 				     struct request *rq)
734 {
735 	blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE);
736 }
737 
738 static void blk_add_trace_rq_complete(void *ignore, struct request *rq,
739 			int error, unsigned int nr_bytes)
740 {
741 	blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE);
742 }
743 
744 /**
745  * blk_add_trace_bio - Add a trace for a bio oriented action
746  * @q:		queue the io is for
747  * @bio:	the source bio
748  * @what:	the action
749  * @error:	error, if any
750  *
751  * Description:
752  *     Records an action against a bio. Will log the bio offset + size.
753  *
754  **/
755 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
756 			      u32 what, int error)
757 {
758 	struct blk_trace *bt = q->blk_trace;
759 
760 	if (likely(!bt))
761 		return;
762 
763 	__blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
764 			bio_op(bio), bio->bi_opf, what, error, 0, NULL);
765 }
766 
767 static void blk_add_trace_bio_bounce(void *ignore,
768 				     struct request_queue *q, struct bio *bio)
769 {
770 	blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
771 }
772 
773 static void blk_add_trace_bio_complete(void *ignore,
774 				       struct request_queue *q, struct bio *bio,
775 				       int error)
776 {
777 	blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
778 }
779 
780 static void blk_add_trace_bio_backmerge(void *ignore,
781 					struct request_queue *q,
782 					struct request *rq,
783 					struct bio *bio)
784 {
785 	blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
786 }
787 
788 static void blk_add_trace_bio_frontmerge(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_FRONTMERGE, 0);
794 }
795 
796 static void blk_add_trace_bio_queue(void *ignore,
797 				    struct request_queue *q, struct bio *bio)
798 {
799 	blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
800 }
801 
802 static void blk_add_trace_getrq(void *ignore,
803 				struct request_queue *q,
804 				struct bio *bio, int rw)
805 {
806 	if (bio)
807 		blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
808 	else {
809 		struct blk_trace *bt = q->blk_trace;
810 
811 		if (bt)
812 			__blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_GETRQ, 0, 0,
813 					NULL);
814 	}
815 }
816 
817 
818 static void blk_add_trace_sleeprq(void *ignore,
819 				  struct request_queue *q,
820 				  struct bio *bio, int rw)
821 {
822 	if (bio)
823 		blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
824 	else {
825 		struct blk_trace *bt = q->blk_trace;
826 
827 		if (bt)
828 			__blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ,
829 					0, 0, NULL);
830 	}
831 }
832 
833 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
834 {
835 	struct blk_trace *bt = q->blk_trace;
836 
837 	if (bt)
838 		__blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
839 }
840 
841 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
842 				    unsigned int depth, bool explicit)
843 {
844 	struct blk_trace *bt = q->blk_trace;
845 
846 	if (bt) {
847 		__be64 rpdu = cpu_to_be64(depth);
848 		u32 what;
849 
850 		if (explicit)
851 			what = BLK_TA_UNPLUG_IO;
852 		else
853 			what = BLK_TA_UNPLUG_TIMER;
854 
855 		__blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
856 	}
857 }
858 
859 static void blk_add_trace_split(void *ignore,
860 				struct request_queue *q, struct bio *bio,
861 				unsigned int pdu)
862 {
863 	struct blk_trace *bt = q->blk_trace;
864 
865 	if (bt) {
866 		__be64 rpdu = cpu_to_be64(pdu);
867 
868 		__blk_add_trace(bt, bio->bi_iter.bi_sector,
869 				bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf,
870 				BLK_TA_SPLIT, bio->bi_error, sizeof(rpdu),
871 				&rpdu);
872 	}
873 }
874 
875 /**
876  * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
877  * @ignore:	trace callback data parameter (not used)
878  * @q:		queue the io is for
879  * @bio:	the source bio
880  * @dev:	target device
881  * @from:	source sector
882  *
883  * Description:
884  *     Device mapper or raid target sometimes need to split a bio because
885  *     it spans a stripe (or similar). Add a trace for that action.
886  *
887  **/
888 static void blk_add_trace_bio_remap(void *ignore,
889 				    struct request_queue *q, struct bio *bio,
890 				    dev_t dev, sector_t from)
891 {
892 	struct blk_trace *bt = q->blk_trace;
893 	struct blk_io_trace_remap r;
894 
895 	if (likely(!bt))
896 		return;
897 
898 	r.device_from = cpu_to_be32(dev);
899 	r.device_to   = cpu_to_be32(bio->bi_bdev->bd_dev);
900 	r.sector_from = cpu_to_be64(from);
901 
902 	__blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
903 			bio_op(bio), bio->bi_opf, BLK_TA_REMAP, bio->bi_error,
904 			sizeof(r), &r);
905 }
906 
907 /**
908  * blk_add_trace_rq_remap - Add a trace for a request-remap operation
909  * @ignore:	trace callback data parameter (not used)
910  * @q:		queue the io is for
911  * @rq:		the source request
912  * @dev:	target device
913  * @from:	source sector
914  *
915  * Description:
916  *     Device mapper remaps request to other devices.
917  *     Add a trace for that action.
918  *
919  **/
920 static void blk_add_trace_rq_remap(void *ignore,
921 				   struct request_queue *q,
922 				   struct request *rq, dev_t dev,
923 				   sector_t from)
924 {
925 	struct blk_trace *bt = q->blk_trace;
926 	struct blk_io_trace_remap r;
927 
928 	if (likely(!bt))
929 		return;
930 
931 	r.device_from = cpu_to_be32(dev);
932 	r.device_to   = cpu_to_be32(disk_devt(rq->rq_disk));
933 	r.sector_from = cpu_to_be64(from);
934 
935 	__blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
936 			rq_data_dir(rq), 0, BLK_TA_REMAP, 0,
937 			sizeof(r), &r);
938 }
939 
940 /**
941  * blk_add_driver_data - Add binary message with driver-specific data
942  * @q:		queue the io is for
943  * @rq:		io request
944  * @data:	driver-specific data
945  * @len:	length of driver-specific data
946  *
947  * Description:
948  *     Some drivers might want to write driver-specific data per request.
949  *
950  **/
951 void blk_add_driver_data(struct request_queue *q,
952 			 struct request *rq,
953 			 void *data, size_t len)
954 {
955 	struct blk_trace *bt = q->blk_trace;
956 
957 	if (likely(!bt))
958 		return;
959 
960 	__blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0,
961 				BLK_TA_DRV_DATA, 0, len, data);
962 }
963 EXPORT_SYMBOL_GPL(blk_add_driver_data);
964 
965 static void blk_register_tracepoints(void)
966 {
967 	int ret;
968 
969 	ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
970 	WARN_ON(ret);
971 	ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
972 	WARN_ON(ret);
973 	ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
974 	WARN_ON(ret);
975 	ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
976 	WARN_ON(ret);
977 	ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
978 	WARN_ON(ret);
979 	ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
980 	WARN_ON(ret);
981 	ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
982 	WARN_ON(ret);
983 	ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
984 	WARN_ON(ret);
985 	ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
986 	WARN_ON(ret);
987 	ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
988 	WARN_ON(ret);
989 	ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
990 	WARN_ON(ret);
991 	ret = register_trace_block_plug(blk_add_trace_plug, NULL);
992 	WARN_ON(ret);
993 	ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
994 	WARN_ON(ret);
995 	ret = register_trace_block_split(blk_add_trace_split, NULL);
996 	WARN_ON(ret);
997 	ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
998 	WARN_ON(ret);
999 	ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1000 	WARN_ON(ret);
1001 }
1002 
1003 static void blk_unregister_tracepoints(void)
1004 {
1005 	unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1006 	unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1007 	unregister_trace_block_split(blk_add_trace_split, NULL);
1008 	unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1009 	unregister_trace_block_plug(blk_add_trace_plug, NULL);
1010 	unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1011 	unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1012 	unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1013 	unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1014 	unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1015 	unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1016 	unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1017 	unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1018 	unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1019 	unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1020 	unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1021 
1022 	tracepoint_synchronize_unregister();
1023 }
1024 
1025 /*
1026  * struct blk_io_tracer formatting routines
1027  */
1028 
1029 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1030 {
1031 	int i = 0;
1032 	int tc = t->action >> BLK_TC_SHIFT;
1033 
1034 	if (t->action == BLK_TN_MESSAGE) {
1035 		rwbs[i++] = 'N';
1036 		goto out;
1037 	}
1038 
1039 	if (tc & BLK_TC_FLUSH)
1040 		rwbs[i++] = 'F';
1041 
1042 	if (tc & BLK_TC_DISCARD)
1043 		rwbs[i++] = 'D';
1044 	else if (tc & BLK_TC_WRITE)
1045 		rwbs[i++] = 'W';
1046 	else if (t->bytes)
1047 		rwbs[i++] = 'R';
1048 	else
1049 		rwbs[i++] = 'N';
1050 
1051 	if (tc & BLK_TC_FUA)
1052 		rwbs[i++] = 'F';
1053 	if (tc & BLK_TC_AHEAD)
1054 		rwbs[i++] = 'A';
1055 	if (tc & BLK_TC_SYNC)
1056 		rwbs[i++] = 'S';
1057 	if (tc & BLK_TC_META)
1058 		rwbs[i++] = 'M';
1059 out:
1060 	rwbs[i] = '\0';
1061 }
1062 
1063 static inline
1064 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1065 {
1066 	return (const struct blk_io_trace *)ent;
1067 }
1068 
1069 static inline const void *pdu_start(const struct trace_entry *ent)
1070 {
1071 	return te_blk_io_trace(ent) + 1;
1072 }
1073 
1074 static inline u32 t_action(const struct trace_entry *ent)
1075 {
1076 	return te_blk_io_trace(ent)->action;
1077 }
1078 
1079 static inline u32 t_bytes(const struct trace_entry *ent)
1080 {
1081 	return te_blk_io_trace(ent)->bytes;
1082 }
1083 
1084 static inline u32 t_sec(const struct trace_entry *ent)
1085 {
1086 	return te_blk_io_trace(ent)->bytes >> 9;
1087 }
1088 
1089 static inline unsigned long long t_sector(const struct trace_entry *ent)
1090 {
1091 	return te_blk_io_trace(ent)->sector;
1092 }
1093 
1094 static inline __u16 t_error(const struct trace_entry *ent)
1095 {
1096 	return te_blk_io_trace(ent)->error;
1097 }
1098 
1099 static __u64 get_pdu_int(const struct trace_entry *ent)
1100 {
1101 	const __u64 *val = pdu_start(ent);
1102 	return be64_to_cpu(*val);
1103 }
1104 
1105 static void get_pdu_remap(const struct trace_entry *ent,
1106 			  struct blk_io_trace_remap *r)
1107 {
1108 	const struct blk_io_trace_remap *__r = pdu_start(ent);
1109 	__u64 sector_from = __r->sector_from;
1110 
1111 	r->device_from = be32_to_cpu(__r->device_from);
1112 	r->device_to   = be32_to_cpu(__r->device_to);
1113 	r->sector_from = be64_to_cpu(sector_from);
1114 }
1115 
1116 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1117 
1118 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1119 {
1120 	char rwbs[RWBS_LEN];
1121 	unsigned long long ts  = iter->ts;
1122 	unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1123 	unsigned secs	       = (unsigned long)ts;
1124 	const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1125 
1126 	fill_rwbs(rwbs, t);
1127 
1128 	trace_seq_printf(&iter->seq,
1129 			 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1130 			 MAJOR(t->device), MINOR(t->device), iter->cpu,
1131 			 secs, nsec_rem, iter->ent->pid, act, rwbs);
1132 }
1133 
1134 static void blk_log_action(struct trace_iterator *iter, const char *act)
1135 {
1136 	char rwbs[RWBS_LEN];
1137 	const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1138 
1139 	fill_rwbs(rwbs, t);
1140 	trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1141 			 MAJOR(t->device), MINOR(t->device), act, rwbs);
1142 }
1143 
1144 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1145 {
1146 	const unsigned char *pdu_buf;
1147 	int pdu_len;
1148 	int i, end;
1149 
1150 	pdu_buf = pdu_start(ent);
1151 	pdu_len = te_blk_io_trace(ent)->pdu_len;
1152 
1153 	if (!pdu_len)
1154 		return;
1155 
1156 	/* find the last zero that needs to be printed */
1157 	for (end = pdu_len - 1; end >= 0; end--)
1158 		if (pdu_buf[end])
1159 			break;
1160 	end++;
1161 
1162 	trace_seq_putc(s, '(');
1163 
1164 	for (i = 0; i < pdu_len; i++) {
1165 
1166 		trace_seq_printf(s, "%s%02x",
1167 				 i == 0 ? "" : " ", pdu_buf[i]);
1168 
1169 		/*
1170 		 * stop when the rest is just zeroes and indicate so
1171 		 * with a ".." appended
1172 		 */
1173 		if (i == end && end != pdu_len - 1) {
1174 			trace_seq_puts(s, " ..) ");
1175 			return;
1176 		}
1177 	}
1178 
1179 	trace_seq_puts(s, ") ");
1180 }
1181 
1182 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1183 {
1184 	char cmd[TASK_COMM_LEN];
1185 
1186 	trace_find_cmdline(ent->pid, cmd);
1187 
1188 	if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1189 		trace_seq_printf(s, "%u ", t_bytes(ent));
1190 		blk_log_dump_pdu(s, ent);
1191 		trace_seq_printf(s, "[%s]\n", cmd);
1192 	} else {
1193 		if (t_sec(ent))
1194 			trace_seq_printf(s, "%llu + %u [%s]\n",
1195 						t_sector(ent), t_sec(ent), cmd);
1196 		else
1197 			trace_seq_printf(s, "[%s]\n", cmd);
1198 	}
1199 }
1200 
1201 static void blk_log_with_error(struct trace_seq *s,
1202 			      const struct trace_entry *ent)
1203 {
1204 	if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1205 		blk_log_dump_pdu(s, ent);
1206 		trace_seq_printf(s, "[%d]\n", t_error(ent));
1207 	} else {
1208 		if (t_sec(ent))
1209 			trace_seq_printf(s, "%llu + %u [%d]\n",
1210 					 t_sector(ent),
1211 					 t_sec(ent), t_error(ent));
1212 		else
1213 			trace_seq_printf(s, "%llu [%d]\n",
1214 					 t_sector(ent), t_error(ent));
1215 	}
1216 }
1217 
1218 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1219 {
1220 	struct blk_io_trace_remap r = { .device_from = 0, };
1221 
1222 	get_pdu_remap(ent, &r);
1223 	trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1224 			 t_sector(ent), t_sec(ent),
1225 			 MAJOR(r.device_from), MINOR(r.device_from),
1226 			 (unsigned long long)r.sector_from);
1227 }
1228 
1229 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1230 {
1231 	char cmd[TASK_COMM_LEN];
1232 
1233 	trace_find_cmdline(ent->pid, cmd);
1234 
1235 	trace_seq_printf(s, "[%s]\n", cmd);
1236 }
1237 
1238 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1239 {
1240 	char cmd[TASK_COMM_LEN];
1241 
1242 	trace_find_cmdline(ent->pid, cmd);
1243 
1244 	trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1245 }
1246 
1247 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1248 {
1249 	char cmd[TASK_COMM_LEN];
1250 
1251 	trace_find_cmdline(ent->pid, cmd);
1252 
1253 	trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1254 			 get_pdu_int(ent), cmd);
1255 }
1256 
1257 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1258 {
1259 	const struct blk_io_trace *t = te_blk_io_trace(ent);
1260 
1261 	trace_seq_putmem(s, t + 1, t->pdu_len);
1262 	trace_seq_putc(s, '\n');
1263 }
1264 
1265 /*
1266  * struct tracer operations
1267  */
1268 
1269 static void blk_tracer_print_header(struct seq_file *m)
1270 {
1271 	if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1272 		return;
1273 	seq_puts(m, "# DEV   CPU TIMESTAMP     PID ACT FLG\n"
1274 		    "#  |     |     |           |   |   |\n");
1275 }
1276 
1277 static void blk_tracer_start(struct trace_array *tr)
1278 {
1279 	blk_tracer_enabled = true;
1280 }
1281 
1282 static int blk_tracer_init(struct trace_array *tr)
1283 {
1284 	blk_tr = tr;
1285 	blk_tracer_start(tr);
1286 	return 0;
1287 }
1288 
1289 static void blk_tracer_stop(struct trace_array *tr)
1290 {
1291 	blk_tracer_enabled = false;
1292 }
1293 
1294 static void blk_tracer_reset(struct trace_array *tr)
1295 {
1296 	blk_tracer_stop(tr);
1297 }
1298 
1299 static const struct {
1300 	const char *act[2];
1301 	void	   (*print)(struct trace_seq *s, const struct trace_entry *ent);
1302 } what2act[] = {
1303 	[__BLK_TA_QUEUE]	= {{  "Q", "queue" },	   blk_log_generic },
1304 	[__BLK_TA_BACKMERGE]	= {{  "M", "backmerge" },  blk_log_generic },
1305 	[__BLK_TA_FRONTMERGE]	= {{  "F", "frontmerge" }, blk_log_generic },
1306 	[__BLK_TA_GETRQ]	= {{  "G", "getrq" },	   blk_log_generic },
1307 	[__BLK_TA_SLEEPRQ]	= {{  "S", "sleeprq" },	   blk_log_generic },
1308 	[__BLK_TA_REQUEUE]	= {{  "R", "requeue" },	   blk_log_with_error },
1309 	[__BLK_TA_ISSUE]	= {{  "D", "issue" },	   blk_log_generic },
1310 	[__BLK_TA_COMPLETE]	= {{  "C", "complete" },   blk_log_with_error },
1311 	[__BLK_TA_PLUG]		= {{  "P", "plug" },	   blk_log_plug },
1312 	[__BLK_TA_UNPLUG_IO]	= {{  "U", "unplug_io" },  blk_log_unplug },
1313 	[__BLK_TA_UNPLUG_TIMER]	= {{ "UT", "unplug_timer" }, blk_log_unplug },
1314 	[__BLK_TA_INSERT]	= {{  "I", "insert" },	   blk_log_generic },
1315 	[__BLK_TA_SPLIT]	= {{  "X", "split" },	   blk_log_split },
1316 	[__BLK_TA_BOUNCE]	= {{  "B", "bounce" },	   blk_log_generic },
1317 	[__BLK_TA_REMAP]	= {{  "A", "remap" },	   blk_log_remap },
1318 };
1319 
1320 static enum print_line_t print_one_line(struct trace_iterator *iter,
1321 					bool classic)
1322 {
1323 	struct trace_array *tr = iter->tr;
1324 	struct trace_seq *s = &iter->seq;
1325 	const struct blk_io_trace *t;
1326 	u16 what;
1327 	bool long_act;
1328 	blk_log_action_t *log_action;
1329 
1330 	t	   = te_blk_io_trace(iter->ent);
1331 	what	   = t->action & ((1 << BLK_TC_SHIFT) - 1);
1332 	long_act   = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
1333 	log_action = classic ? &blk_log_action_classic : &blk_log_action;
1334 
1335 	if (t->action == BLK_TN_MESSAGE) {
1336 		log_action(iter, long_act ? "message" : "m");
1337 		blk_log_msg(s, iter->ent);
1338 		return trace_handle_return(s);
1339 	}
1340 
1341 	if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1342 		trace_seq_printf(s, "Unknown action %x\n", what);
1343 	else {
1344 		log_action(iter, what2act[what].act[long_act]);
1345 		what2act[what].print(s, iter->ent);
1346 	}
1347 
1348 	return trace_handle_return(s);
1349 }
1350 
1351 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1352 					       int flags, struct trace_event *event)
1353 {
1354 	return print_one_line(iter, false);
1355 }
1356 
1357 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1358 {
1359 	struct trace_seq *s = &iter->seq;
1360 	struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1361 	const int offset = offsetof(struct blk_io_trace, sector);
1362 	struct blk_io_trace old = {
1363 		.magic	  = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1364 		.time     = iter->ts,
1365 	};
1366 
1367 	trace_seq_putmem(s, &old, offset);
1368 	trace_seq_putmem(s, &t->sector,
1369 			 sizeof(old) - offset + t->pdu_len);
1370 }
1371 
1372 static enum print_line_t
1373 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1374 			     struct trace_event *event)
1375 {
1376 	blk_trace_synthesize_old_trace(iter);
1377 
1378 	return trace_handle_return(&iter->seq);
1379 }
1380 
1381 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1382 {
1383 	if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1384 		return TRACE_TYPE_UNHANDLED;
1385 
1386 	return print_one_line(iter, true);
1387 }
1388 
1389 static int
1390 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1391 {
1392 	/* don't output context-info for blk_classic output */
1393 	if (bit == TRACE_BLK_OPT_CLASSIC) {
1394 		if (set)
1395 			tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1396 		else
1397 			tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
1398 	}
1399 	return 0;
1400 }
1401 
1402 static struct tracer blk_tracer __read_mostly = {
1403 	.name		= "blk",
1404 	.init		= blk_tracer_init,
1405 	.reset		= blk_tracer_reset,
1406 	.start		= blk_tracer_start,
1407 	.stop		= blk_tracer_stop,
1408 	.print_header	= blk_tracer_print_header,
1409 	.print_line	= blk_tracer_print_line,
1410 	.flags		= &blk_tracer_flags,
1411 	.set_flag	= blk_tracer_set_flag,
1412 };
1413 
1414 static struct trace_event_functions trace_blk_event_funcs = {
1415 	.trace		= blk_trace_event_print,
1416 	.binary		= blk_trace_event_print_binary,
1417 };
1418 
1419 static struct trace_event trace_blk_event = {
1420 	.type		= TRACE_BLK,
1421 	.funcs		= &trace_blk_event_funcs,
1422 };
1423 
1424 static int __init init_blk_tracer(void)
1425 {
1426 	if (!register_trace_event(&trace_blk_event)) {
1427 		pr_warn("Warning: could not register block events\n");
1428 		return 1;
1429 	}
1430 
1431 	if (register_tracer(&blk_tracer) != 0) {
1432 		pr_warn("Warning: could not register the block tracer\n");
1433 		unregister_trace_event(&trace_blk_event);
1434 		return 1;
1435 	}
1436 
1437 	return 0;
1438 }
1439 
1440 device_initcall(init_blk_tracer);
1441 
1442 static int blk_trace_remove_queue(struct request_queue *q)
1443 {
1444 	struct blk_trace *bt;
1445 
1446 	bt = xchg(&q->blk_trace, NULL);
1447 	if (bt == NULL)
1448 		return -EINVAL;
1449 
1450 	if (atomic_dec_and_test(&blk_probes_ref))
1451 		blk_unregister_tracepoints();
1452 
1453 	blk_trace_free(bt);
1454 	return 0;
1455 }
1456 
1457 /*
1458  * Setup everything required to start tracing
1459  */
1460 static int blk_trace_setup_queue(struct request_queue *q,
1461 				 struct block_device *bdev)
1462 {
1463 	struct blk_trace *bt = NULL;
1464 	int ret = -ENOMEM;
1465 
1466 	bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1467 	if (!bt)
1468 		return -ENOMEM;
1469 
1470 	bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1471 	if (!bt->msg_data)
1472 		goto free_bt;
1473 
1474 	bt->dev = bdev->bd_dev;
1475 	bt->act_mask = (u16)-1;
1476 
1477 	blk_trace_setup_lba(bt, bdev);
1478 
1479 	ret = -EBUSY;
1480 	if (cmpxchg(&q->blk_trace, NULL, bt))
1481 		goto free_bt;
1482 
1483 	if (atomic_inc_return(&blk_probes_ref) == 1)
1484 		blk_register_tracepoints();
1485 	return 0;
1486 
1487 free_bt:
1488 	blk_trace_free(bt);
1489 	return ret;
1490 }
1491 
1492 /*
1493  * sysfs interface to enable and configure tracing
1494  */
1495 
1496 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1497 					 struct device_attribute *attr,
1498 					 char *buf);
1499 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1500 					  struct device_attribute *attr,
1501 					  const char *buf, size_t count);
1502 #define BLK_TRACE_DEVICE_ATTR(_name) \
1503 	DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1504 		    sysfs_blk_trace_attr_show, \
1505 		    sysfs_blk_trace_attr_store)
1506 
1507 static BLK_TRACE_DEVICE_ATTR(enable);
1508 static BLK_TRACE_DEVICE_ATTR(act_mask);
1509 static BLK_TRACE_DEVICE_ATTR(pid);
1510 static BLK_TRACE_DEVICE_ATTR(start_lba);
1511 static BLK_TRACE_DEVICE_ATTR(end_lba);
1512 
1513 static struct attribute *blk_trace_attrs[] = {
1514 	&dev_attr_enable.attr,
1515 	&dev_attr_act_mask.attr,
1516 	&dev_attr_pid.attr,
1517 	&dev_attr_start_lba.attr,
1518 	&dev_attr_end_lba.attr,
1519 	NULL
1520 };
1521 
1522 struct attribute_group blk_trace_attr_group = {
1523 	.name  = "trace",
1524 	.attrs = blk_trace_attrs,
1525 };
1526 
1527 static const struct {
1528 	int mask;
1529 	const char *str;
1530 } mask_maps[] = {
1531 	{ BLK_TC_READ,		"read"		},
1532 	{ BLK_TC_WRITE,		"write"		},
1533 	{ BLK_TC_FLUSH,		"flush"		},
1534 	{ BLK_TC_SYNC,		"sync"		},
1535 	{ BLK_TC_QUEUE,		"queue"		},
1536 	{ BLK_TC_REQUEUE,	"requeue"	},
1537 	{ BLK_TC_ISSUE,		"issue"		},
1538 	{ BLK_TC_COMPLETE,	"complete"	},
1539 	{ BLK_TC_FS,		"fs"		},
1540 	{ BLK_TC_PC,		"pc"		},
1541 	{ BLK_TC_NOTIFY,	"notify"	},
1542 	{ BLK_TC_AHEAD,		"ahead"		},
1543 	{ BLK_TC_META,		"meta"		},
1544 	{ BLK_TC_DISCARD,	"discard"	},
1545 	{ BLK_TC_DRV_DATA,	"drv_data"	},
1546 	{ BLK_TC_FUA,		"fua"		},
1547 };
1548 
1549 static int blk_trace_str2mask(const char *str)
1550 {
1551 	int i;
1552 	int mask = 0;
1553 	char *buf, *s, *token;
1554 
1555 	buf = kstrdup(str, GFP_KERNEL);
1556 	if (buf == NULL)
1557 		return -ENOMEM;
1558 	s = strstrip(buf);
1559 
1560 	while (1) {
1561 		token = strsep(&s, ",");
1562 		if (token == NULL)
1563 			break;
1564 
1565 		if (*token == '\0')
1566 			continue;
1567 
1568 		for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1569 			if (strcasecmp(token, mask_maps[i].str) == 0) {
1570 				mask |= mask_maps[i].mask;
1571 				break;
1572 			}
1573 		}
1574 		if (i == ARRAY_SIZE(mask_maps)) {
1575 			mask = -EINVAL;
1576 			break;
1577 		}
1578 	}
1579 	kfree(buf);
1580 
1581 	return mask;
1582 }
1583 
1584 static ssize_t blk_trace_mask2str(char *buf, int mask)
1585 {
1586 	int i;
1587 	char *p = buf;
1588 
1589 	for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1590 		if (mask & mask_maps[i].mask) {
1591 			p += sprintf(p, "%s%s",
1592 				    (p == buf) ? "" : ",", mask_maps[i].str);
1593 		}
1594 	}
1595 	*p++ = '\n';
1596 
1597 	return p - buf;
1598 }
1599 
1600 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1601 {
1602 	if (bdev->bd_disk == NULL)
1603 		return NULL;
1604 
1605 	return bdev_get_queue(bdev);
1606 }
1607 
1608 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1609 					 struct device_attribute *attr,
1610 					 char *buf)
1611 {
1612 	struct hd_struct *p = dev_to_part(dev);
1613 	struct request_queue *q;
1614 	struct block_device *bdev;
1615 	ssize_t ret = -ENXIO;
1616 
1617 	bdev = bdget(part_devt(p));
1618 	if (bdev == NULL)
1619 		goto out;
1620 
1621 	q = blk_trace_get_queue(bdev);
1622 	if (q == NULL)
1623 		goto out_bdput;
1624 
1625 	mutex_lock(&bdev->bd_mutex);
1626 
1627 	if (attr == &dev_attr_enable) {
1628 		ret = sprintf(buf, "%u\n", !!q->blk_trace);
1629 		goto out_unlock_bdev;
1630 	}
1631 
1632 	if (q->blk_trace == NULL)
1633 		ret = sprintf(buf, "disabled\n");
1634 	else if (attr == &dev_attr_act_mask)
1635 		ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1636 	else if (attr == &dev_attr_pid)
1637 		ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1638 	else if (attr == &dev_attr_start_lba)
1639 		ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1640 	else if (attr == &dev_attr_end_lba)
1641 		ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1642 
1643 out_unlock_bdev:
1644 	mutex_unlock(&bdev->bd_mutex);
1645 out_bdput:
1646 	bdput(bdev);
1647 out:
1648 	return ret;
1649 }
1650 
1651 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1652 					  struct device_attribute *attr,
1653 					  const char *buf, size_t count)
1654 {
1655 	struct block_device *bdev;
1656 	struct request_queue *q;
1657 	struct hd_struct *p;
1658 	u64 value;
1659 	ssize_t ret = -EINVAL;
1660 
1661 	if (count == 0)
1662 		goto out;
1663 
1664 	if (attr == &dev_attr_act_mask) {
1665 		if (kstrtoull(buf, 0, &value)) {
1666 			/* Assume it is a list of trace category names */
1667 			ret = blk_trace_str2mask(buf);
1668 			if (ret < 0)
1669 				goto out;
1670 			value = ret;
1671 		}
1672 	} else if (kstrtoull(buf, 0, &value))
1673 		goto out;
1674 
1675 	ret = -ENXIO;
1676 
1677 	p = dev_to_part(dev);
1678 	bdev = bdget(part_devt(p));
1679 	if (bdev == NULL)
1680 		goto out;
1681 
1682 	q = blk_trace_get_queue(bdev);
1683 	if (q == NULL)
1684 		goto out_bdput;
1685 
1686 	mutex_lock(&bdev->bd_mutex);
1687 
1688 	if (attr == &dev_attr_enable) {
1689 		if (value)
1690 			ret = blk_trace_setup_queue(q, bdev);
1691 		else
1692 			ret = blk_trace_remove_queue(q);
1693 		goto out_unlock_bdev;
1694 	}
1695 
1696 	ret = 0;
1697 	if (q->blk_trace == NULL)
1698 		ret = blk_trace_setup_queue(q, bdev);
1699 
1700 	if (ret == 0) {
1701 		if (attr == &dev_attr_act_mask)
1702 			q->blk_trace->act_mask = value;
1703 		else if (attr == &dev_attr_pid)
1704 			q->blk_trace->pid = value;
1705 		else if (attr == &dev_attr_start_lba)
1706 			q->blk_trace->start_lba = value;
1707 		else if (attr == &dev_attr_end_lba)
1708 			q->blk_trace->end_lba = value;
1709 	}
1710 
1711 out_unlock_bdev:
1712 	mutex_unlock(&bdev->bd_mutex);
1713 out_bdput:
1714 	bdput(bdev);
1715 out:
1716 	return ret ? ret : count;
1717 }
1718 
1719 int blk_trace_init_sysfs(struct device *dev)
1720 {
1721 	return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1722 }
1723 
1724 void blk_trace_remove_sysfs(struct device *dev)
1725 {
1726 	sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1727 }
1728 
1729 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1730 
1731 #ifdef CONFIG_EVENT_TRACING
1732 
1733 void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes)
1734 {
1735 	int i = 0;
1736 
1737 	if (op & REQ_PREFLUSH)
1738 		rwbs[i++] = 'F';
1739 
1740 	switch (op & REQ_OP_MASK) {
1741 	case REQ_OP_WRITE:
1742 	case REQ_OP_WRITE_SAME:
1743 		rwbs[i++] = 'W';
1744 		break;
1745 	case REQ_OP_DISCARD:
1746 		rwbs[i++] = 'D';
1747 		break;
1748 	case REQ_OP_SECURE_ERASE:
1749 		rwbs[i++] = 'D';
1750 		rwbs[i++] = 'E';
1751 		break;
1752 	case REQ_OP_FLUSH:
1753 		rwbs[i++] = 'F';
1754 		break;
1755 	case REQ_OP_READ:
1756 		rwbs[i++] = 'R';
1757 		break;
1758 	default:
1759 		rwbs[i++] = 'N';
1760 	}
1761 
1762 	if (op & REQ_FUA)
1763 		rwbs[i++] = 'F';
1764 	if (op & REQ_RAHEAD)
1765 		rwbs[i++] = 'A';
1766 	if (op & REQ_SYNC)
1767 		rwbs[i++] = 'S';
1768 	if (op & REQ_META)
1769 		rwbs[i++] = 'M';
1770 
1771 	rwbs[i] = '\0';
1772 }
1773 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1774 
1775 #endif /* CONFIG_EVENT_TRACING */
1776 
1777