blob: 1e5499414cdf1033dd44f0e35b894bf9b07bdcd5 [file] [log] [blame]
Steven Rostedt (VMware)91c1e6b2018-08-16 11:19:04 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe2056a782006-03-23 20:00:26 +01002/*
Jens Axboe0fe23472006-09-04 15:41:16 +02003 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
Jens Axboe2056a782006-03-23 20:00:26 +01004 *
Jens Axboe2056a782006-03-23 20:00:26 +01005 */
Jens Axboe2056a782006-03-23 20:00:26 +01006#include <linux/kernel.h>
7#include <linux/blkdev.h>
8#include <linux/blktrace_api.h>
9#include <linux/percpu.h>
10#include <linux/init.h>
11#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jens Axboe2056a782006-03-23 20:00:26 +010013#include <linux/debugfs.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040014#include <linux/export.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010015#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020016#include <linux/uaccess.h>
Jan Karaa404d552013-09-17 22:30:31 +020017#include <linux/list.h>
Shaohua Lica1136c2017-07-12 11:49:53 -070018#include <linux/blk-cgroup.h>
Li Zefan55782132009-06-09 13:43:05 +080019
Omar Sandoval18fbda92017-01-31 14:53:20 -080020#include "../../block/blk.h"
21
Li Zefan55782132009-06-09 13:43:05 +080022#include <trace/events/block.h>
23
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010024#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010025
Li Zefan55782132009-06-09 13:43:05 +080026#ifdef CONFIG_BLK_DEV_IO_TRACE
27
Jens Axboe2056a782006-03-23 20:00:26 +010028static unsigned int blktrace_seq __read_mostly = 1;
29
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020030static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080031static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020032
Jan Karaa404d552013-09-17 22:30:31 +020033static LIST_HEAD(running_trace_list);
34static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
35
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020036/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040037#define TRACE_BLK_OPT_CLASSIC 0x1
Shaohua Lica1136c2017-07-12 11:49:53 -070038#define TRACE_BLK_OPT_CGROUP 0x2
Shaohua Li69fd5c32017-07-12 11:49:55 -070039#define TRACE_BLK_OPT_CGNAME 0x4
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020040
41static struct tracer_opt blk_tracer_opts[] = {
42 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020043 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Shaohua Lica1136c2017-07-12 11:49:53 -070044#ifdef CONFIG_BLK_CGROUP
45 { TRACER_OPT(blk_cgroup, TRACE_BLK_OPT_CGROUP) },
Shaohua Li69fd5c32017-07-12 11:49:55 -070046 { TRACER_OPT(blk_cgname, TRACE_BLK_OPT_CGNAME) },
Shaohua Lica1136c2017-07-12 11:49:53 -070047#endif
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020048 { }
49};
50
51static struct tracer_flags blk_tracer_flags = {
52 .val = 0,
53 .opts = blk_tracer_opts,
54};
55
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010056/* Global reference count of probes */
Jens Axboea6da0022017-11-05 09:16:09 -070057static DEFINE_MUTEX(blk_probe_mutex);
58static int blk_probes_ref;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010059
Li Zefan3c289ba2009-03-20 09:48:26 +080060static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010061static void blk_unregister_tracepoints(void);
62
Jens Axboe2056a782006-03-23 20:00:26 +010063/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010064 * Send out a notify message.
65 */
Jens Axboea8630552006-12-04 09:30:58 +010066static void trace_note(struct blk_trace *bt, pid_t pid, int action,
Tejun Heo67c04962019-11-04 15:54:30 -080067 const void *data, size_t len, u64 cgid)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010068{
69 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080070 struct ring_buffer_event *event = NULL;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -050071 struct trace_buffer *buffer = NULL;
Li Zefan18cea452009-03-27 10:21:54 +080072 int pc = 0;
73 int cpu = smp_processor_id();
74 bool blk_tracer = blk_tracer_enabled;
Tejun Heo67c04962019-11-04 15:54:30 -080075 ssize_t cgid_len = cgid ? sizeof(cgid) : 0;
Li Zefan18cea452009-03-27 10:21:54 +080076
77 if (blk_tracer) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -050078 buffer = blk_tr->array_buffer.buffer;
Li Zefan18cea452009-03-27 10:21:54 +080079 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -040080 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Shaohua Lica1136c2017-07-12 11:49:53 -070081 sizeof(*t) + len + cgid_len,
Li Zefan18cea452009-03-27 10:21:54 +080082 0, pc);
83 if (!event)
84 return;
85 t = ring_buffer_event_data(event);
86 goto record_it;
87 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010088
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020089 if (!bt->rchan)
90 return;
91
Shaohua Lica1136c2017-07-12 11:49:53 -070092 t = relay_reserve(bt->rchan, sizeof(*t) + len + cgid_len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010093 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010094 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010095 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080096record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +010097 t->device = bt->dev;
Shaohua Lica1136c2017-07-12 11:49:53 -070098 t->action = action | (cgid ? __BLK_TN_CGROUP : 0);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010099 t->pid = pid;
100 t->cpu = cpu;
Shaohua Lica1136c2017-07-12 11:49:53 -0700101 t->pdu_len = len + cgid_len;
Tejun Heo67c04962019-11-04 15:54:30 -0800102 if (cgid_len)
103 memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
Shaohua Lica1136c2017-07-12 11:49:53 -0700104 memcpy((void *) t + sizeof(*t) + cgid_len, data, len);
Li Zefan18cea452009-03-27 10:21:54 +0800105
106 if (blk_tracer)
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -0400107 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100108 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100109}
110
111/*
Jens Axboe2056a782006-03-23 20:00:26 +0100112 * Send out a notify for this process, if we haven't done so since a trace
113 * started
114 */
Jan Karaa404d552013-09-17 22:30:31 +0200115static void trace_note_tsk(struct task_struct *tsk)
Jens Axboe2056a782006-03-23 20:00:26 +0100116{
Jan Karaa404d552013-09-17 22:30:31 +0200117 unsigned long flags;
118 struct blk_trace *bt;
119
Jens Axboea8630552006-12-04 09:30:58 +0100120 tsk->btrace_seq = blktrace_seq;
Jan Karaa404d552013-09-17 22:30:31 +0200121 spin_lock_irqsave(&running_trace_lock, flags);
122 list_for_each_entry(bt, &running_trace_list, running_list) {
123 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
Tejun Heo67c04962019-11-04 15:54:30 -0800124 sizeof(tsk->comm), 0);
Jan Karaa404d552013-09-17 22:30:31 +0200125 }
126 spin_unlock_irqrestore(&running_trace_lock, flags);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100127}
Jens Axboe2056a782006-03-23 20:00:26 +0100128
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100129static void trace_note_time(struct blk_trace *bt)
130{
Arnd Bergmann59a37f82016-06-17 16:58:26 +0200131 struct timespec64 now;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100132 unsigned long flags;
133 u32 words[2];
134
Arnd Bergmann59a37f82016-06-17 16:58:26 +0200135 /* need to check user space to see if this breaks in y2038 or y2106 */
136 ktime_get_real_ts64(&now);
137 words[0] = (u32)now.tv_sec;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100138 words[1] = now.tv_nsec;
139
140 local_irq_save(flags);
Tejun Heo67c04962019-11-04 15:54:30 -0800141 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words), 0);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100142 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100143}
144
Shaohua Li35fe6d72017-07-12 11:49:56 -0700145void __trace_note_message(struct blk_trace *bt, struct blkcg *blkcg,
146 const char *fmt, ...)
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200147{
148 int n;
149 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200150 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200151 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200152
Li Zefan18cea452009-03-27 10:21:54 +0800153 if (unlikely(bt->trace_state != Blktrace_running &&
154 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200155 return;
156
Tao Ma490da402011-01-19 10:51:44 +0800157 /*
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
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200164 local_irq_save(flags);
Shan Weid8a03492012-11-13 09:53:04 +0800165 buf = this_cpu_ptr(bt->msg_data);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200166 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200167 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200168 va_end(args);
169
Shaohua Li35fe6d72017-07-12 11:49:56 -0700170 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
171 blkcg = NULL;
172#ifdef CONFIG_BLK_CGROUP
Jan Kara870c1532020-05-13 18:02:23 +0200173 trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n,
Tejun Heo74321032019-11-04 15:54:30 -0800174 blkcg ? cgroup_id(blkcg->css.cgroup) : 1);
Shaohua Li35fe6d72017-07-12 11:49:56 -0700175#else
Jan Kara870c1532020-05-13 18:02:23 +0200176 trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n, 0);
Shaohua Li35fe6d72017-07-12 11:49:56 -0700177#endif
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200178 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200179}
180EXPORT_SYMBOL_GPL(__trace_note_message);
181
Jens Axboe2056a782006-03-23 20:00:26 +0100182static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
183 pid_t pid)
184{
185 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
186 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800187 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100188 return 1;
189 if (bt->pid && pid != bt->pid)
190 return 1;
191
192 return 0;
193}
194
195/*
196 * Data direction bit lookup
197 */
Li Zefane4955c92009-03-24 16:04:37 +0800198static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
199 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100200
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200201#define BLK_TC_RAHEAD BLK_TC_AHEAD
Mike Christie28a8f0d2016-06-05 14:32:25 -0500202#define BLK_TC_PREFLUSH BLK_TC_FLUSH
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200203
David Woodhouse35ba8f72008-08-10 12:33:00 +0100204/* The ilog2() calls fall out because they're constant */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200205#define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
206 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100207
208/*
209 * The worker for the various blk_add_trace*() types. Fills out a
210 * blk_io_trace structure and places it in a per-cpu subbuffer.
211 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100212static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500213 int op, int op_flags, u32 what, int error, int pdu_len,
Tejun Heo67c04962019-11-04 15:54:30 -0800214 void *pdu_data, u64 cgid)
Jens Axboe2056a782006-03-23 20:00:26 +0100215{
216 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200217 struct ring_buffer_event *event = NULL;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500218 struct trace_buffer *buffer = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100219 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200220 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100221 unsigned long *sequence;
222 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200223 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800224 bool blk_tracer = blk_tracer_enabled;
Tejun Heo67c04962019-11-04 15:54:30 -0800225 ssize_t cgid_len = cgid ? sizeof(cgid) : 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100226
Li Zefan18cea452009-03-27 10:21:54 +0800227 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100228 return;
229
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500230 what |= ddir_act[op_is_write(op) ? WRITE : READ];
231 what |= MASK_TC_BIT(op_flags, SYNC);
232 what |= MASK_TC_BIT(op_flags, RAHEAD);
233 what |= MASK_TC_BIT(op_flags, META);
Mike Christie28a8f0d2016-06-05 14:32:25 -0500234 what |= MASK_TC_BIT(op_flags, PREFLUSH);
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500235 what |= MASK_TC_BIT(op_flags, FUA);
Adrian Hunter7afafc82016-08-16 10:59:35 +0300236 if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500237 what |= BLK_TC_ACT(BLK_TC_DISCARD);
Mike Christie3a5e02c2016-06-05 14:32:23 -0500238 if (op == REQ_OP_FLUSH)
239 what |= BLK_TC_ACT(BLK_TC_FLUSH);
Shaohua Lica1136c2017-07-12 11:49:53 -0700240 if (cgid)
241 what |= __BLK_TA_CGROUP;
Jens Axboe2056a782006-03-23 20:00:26 +0100242
243 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800244 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100245 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200246 cpu = raw_smp_processor_id();
247
Li Zefan18cea452009-03-27 10:21:54 +0800248 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200249 tracing_record_cmdline(current);
250
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500251 buffer = blk_tr->array_buffer.buffer;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200252 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -0400253 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Shaohua Lica1136c2017-07-12 11:49:53 -0700254 sizeof(*t) + pdu_len + cgid_len,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200255 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200256 if (!event)
257 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200258 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200259 goto record_it;
260 }
Jens Axboe2056a782006-03-23 20:00:26 +0100261
Jan Karaa404d552013-09-17 22:30:31 +0200262 if (unlikely(tsk->btrace_seq != blktrace_seq))
263 trace_note_tsk(tsk);
264
Jens Axboe2056a782006-03-23 20:00:26 +0100265 /*
266 * A word about the locking here - we disable interrupts to reserve
267 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200268 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100269 */
270 local_irq_save(flags);
Shaohua Lica1136c2017-07-12 11:49:53 -0700271 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len + cgid_len);
Jens Axboe2056a782006-03-23 20:00:26 +0100272 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100273 sequence = per_cpu_ptr(bt->sequence, cpu);
274
275 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
276 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100277 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200278record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200279 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200280 * These two are not needed in ftrace as they are in the
281 * generic trace_entry, filled by tracing_generic_entry_update,
282 * but for the trace_event->bin() synthesizer benefit we do it
283 * here too.
284 */
285 t->cpu = cpu;
286 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200287
Jens Axboe2056a782006-03-23 20:00:26 +0100288 t->sector = sector;
289 t->bytes = bytes;
290 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100291 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100292 t->error = error;
Shaohua Lica1136c2017-07-12 11:49:53 -0700293 t->pdu_len = pdu_len + cgid_len;
Jens Axboe2056a782006-03-23 20:00:26 +0100294
Shaohua Lica1136c2017-07-12 11:49:53 -0700295 if (cgid_len)
Tejun Heo67c04962019-11-04 15:54:30 -0800296 memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
Jens Axboe2056a782006-03-23 20:00:26 +0100297 if (pdu_len)
Shaohua Lica1136c2017-07-12 11:49:53 -0700298 memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200299
Li Zefan18cea452009-03-27 10:21:54 +0800300 if (blk_tracer) {
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -0400301 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200302 return;
303 }
Jens Axboe2056a782006-03-23 20:00:26 +0100304 }
305
306 local_irq_restore(flags);
307}
308
Li Zefanad5dd542009-03-27 10:20:24 +0800309static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100310{
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200311 debugfs_remove(bt->msg_file);
Jens Axboe2056a782006-03-23 20:00:26 +0100312 debugfs_remove(bt->dropped_file);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100313 relay_close(bt->rchan);
Alan D. Brunelle39cbb602009-08-07 12:01:08 -0400314 debugfs_remove(bt->dir);
Jens Axboe2056a782006-03-23 20:00:26 +0100315 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200316 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100317 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800318}
319
Jens Axboea6da0022017-11-05 09:16:09 -0700320static void get_probe_ref(void)
321{
322 mutex_lock(&blk_probe_mutex);
323 if (++blk_probes_ref == 1)
324 blk_register_tracepoints();
325 mutex_unlock(&blk_probe_mutex);
326}
327
328static void put_probe_ref(void)
329{
330 mutex_lock(&blk_probe_mutex);
331 if (!--blk_probes_ref)
332 blk_unregister_tracepoints();
333 mutex_unlock(&blk_probe_mutex);
334}
335
Li Zefanad5dd542009-03-27 10:20:24 +0800336static void blk_trace_cleanup(struct blk_trace *bt)
337{
Jan Karac780e862020-02-06 15:28:12 +0100338 synchronize_rcu();
Li Zefanad5dd542009-03-27 10:20:24 +0800339 blk_trace_free(bt);
Jens Axboea6da0022017-11-05 09:16:09 -0700340 put_probe_ref();
Jens Axboe2056a782006-03-23 20:00:26 +0100341}
342
Jens Axboe1f2cac12017-11-05 09:13:48 -0700343static int __blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100344{
345 struct blk_trace *bt;
346
347 bt = xchg(&q->blk_trace, NULL);
348 if (!bt)
349 return -EINVAL;
350
Li Zefan55547202009-03-25 17:21:26 +0800351 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100352 blk_trace_cleanup(bt);
353
354 return 0;
355}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700356
357int blk_trace_remove(struct request_queue *q)
358{
359 int ret;
360
361 mutex_lock(&q->blk_trace_mutex);
362 ret = __blk_trace_remove(q);
363 mutex_unlock(&q->blk_trace_mutex);
364
365 return ret;
366}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100367EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100368
Jens Axboe2056a782006-03-23 20:00:26 +0100369static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
370 size_t count, loff_t *ppos)
371{
372 struct blk_trace *bt = filp->private_data;
373 char buf[16];
374
375 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
376
377 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
378}
379
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800380static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100381 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700382 .open = simple_open,
Jens Axboe2056a782006-03-23 20:00:26 +0100383 .read = blk_dropped_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200384 .llseek = default_llseek,
Jens Axboe2056a782006-03-23 20:00:26 +0100385};
386
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200387static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
388 size_t count, loff_t *ppos)
389{
390 char *msg;
391 struct blk_trace *bt;
392
Li Zefan7635b032009-04-03 15:31:34 +0800393 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200394 return -EINVAL;
395
Al Viro16e5c1f2015-12-24 00:06:05 -0500396 msg = memdup_user_nul(buffer, count);
397 if (IS_ERR(msg))
398 return PTR_ERR(msg);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200399
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200400 bt = filp->private_data;
Shaohua Li35fe6d72017-07-12 11:49:56 -0700401 __trace_note_message(bt, NULL, "%s", msg);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200402 kfree(msg);
403
404 return count;
405}
406
407static const struct file_operations blk_msg_fops = {
408 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700409 .open = simple_open,
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200410 .write = blk_msg_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200411 .llseek = noop_llseek,
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200412};
413
Jens Axboe2056a782006-03-23 20:00:26 +0100414/*
415 * Keep track of how many times we encountered a full subbuffer, to aid
416 * the user space app in telling how many lost events there were.
417 */
418static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
419 void *prev_subbuf, size_t prev_padding)
420{
421 struct blk_trace *bt;
422
423 if (!relay_buf_full(buf))
424 return 1;
425
426 bt = buf->chan->private_data;
427 atomic_inc(&bt->dropped);
428 return 0;
429}
430
431static int blk_remove_buf_file_callback(struct dentry *dentry)
432{
433 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100434
Jens Axboe2056a782006-03-23 20:00:26 +0100435 return 0;
436}
437
438static struct dentry *blk_create_buf_file_callback(const char *filename,
439 struct dentry *parent,
Al Virof4ae40a62011-07-24 04:33:43 -0400440 umode_t mode,
Jens Axboe2056a782006-03-23 20:00:26 +0100441 struct rchan_buf *buf,
442 int *is_global)
443{
444 return debugfs_create_file(filename, mode, parent, buf,
445 &relay_file_operations);
446}
447
448static struct rchan_callbacks blk_relay_callbacks = {
449 .subbuf_start = blk_subbuf_start_callback,
450 .create_buf_file = blk_create_buf_file_callback,
451 .remove_buf_file = blk_remove_buf_file_callback,
452};
453
Li Zefan9908c302009-04-14 13:59:34 +0800454static void blk_trace_setup_lba(struct blk_trace *bt,
455 struct block_device *bdev)
456{
457 struct hd_struct *part = NULL;
458
459 if (bdev)
460 part = bdev->bd_part;
461
462 if (part) {
463 bt->start_lba = part->start_sect;
464 bt->end_lba = part->start_sect + part->nr_sects;
465 } else {
466 bt->start_lba = 0;
467 bt->end_lba = -1ULL;
468 }
469}
470
Jens Axboe2056a782006-03-23 20:00:26 +0100471/*
472 * Setup everything required to start tracing
473 */
Omar Sandovala428d312017-01-31 14:53:19 -0800474static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
475 struct block_device *bdev,
476 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100477{
Davidlohr Buesocdea01b2015-10-30 05:25:59 +0900478 struct blk_trace *bt = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100479 struct dentry *dir = NULL;
Rasmus Villemoesff144172015-06-25 15:02:28 -0700480 int ret;
Jens Axboe2056a782006-03-23 20:00:26 +0100481
Arnd Bergmann171044d42007-10-09 13:23:53 +0200482 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100483 return -EINVAL;
484
Liu Boe1a41322018-06-29 09:56:08 +0800485 if (!blk_debugfs_root)
486 return -ENOENT;
487
Jens Axboe0497b342008-10-01 16:16:25 +0200488 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
489 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100490
491 /*
492 * some device names have larger paths - convert the slashes
493 * to underscores for this to work as expected
494 */
Rasmus Villemoesff144172015-06-25 15:02:28 -0700495 strreplace(buts->name, '/', '_');
Jens Axboe2056a782006-03-23 20:00:26 +0100496
Jens Axboe2056a782006-03-23 20:00:26 +0100497 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
498 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800499 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100500
Li Zefanad5dd542009-03-27 10:20:24 +0800501 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100502 bt->sequence = alloc_percpu(unsigned long);
503 if (!bt->sequence)
504 goto err;
505
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100506 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200507 if (!bt->msg_data)
508 goto err;
509
Jens Axboe2056a782006-03-23 20:00:26 +0100510 ret = -ENOENT;
Jens Axboef48fc4d2009-01-05 10:17:25 +0100511
Omar Sandoval6ac93112017-01-31 14:53:22 -0800512 dir = debugfs_lookup(buts->name, blk_debugfs_root);
513 if (!dir)
514 bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root);
Jens Axboe2056a782006-03-23 20:00:26 +0100515
Christof Schmitt6da127a2008-01-11 10:09:43 +0100516 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100517 atomic_set(&bt->dropped, 0);
Jan Karaa404d552013-09-17 22:30:31 +0200518 INIT_LIST_HEAD(&bt->running_list);
Jens Axboe2056a782006-03-23 20:00:26 +0100519
520 ret = -EIO;
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200521 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
522 &blk_dropped_fops);
Jens Axboe2056a782006-03-23 20:00:26 +0100523
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200524 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200525
Arnd Bergmann171044d42007-10-09 13:23:53 +0200526 bt->rchan = relay_open("trace", dir, buts->buf_size,
527 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100528 if (!bt->rchan)
529 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100530
Arnd Bergmann171044d42007-10-09 13:23:53 +0200531 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100532 if (!bt->act_mask)
533 bt->act_mask = (u16) -1;
534
Li Zefan9908c302009-04-14 13:59:34 +0800535 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100536
Shawn Dud0deef52009-04-14 13:58:56 +0800537 /* overwrite with user settings */
538 if (buts->start_lba)
539 bt->start_lba = buts->start_lba;
540 if (buts->end_lba)
541 bt->end_lba = buts->end_lba;
542
Arnd Bergmann171044d42007-10-09 13:23:53 +0200543 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100544 bt->trace_state = Blktrace_setup;
545
546 ret = -EBUSY;
Davidlohr Buesocdea01b2015-10-30 05:25:59 +0900547 if (cmpxchg(&q->blk_trace, NULL, bt))
Jens Axboe2056a782006-03-23 20:00:26 +0100548 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100549
Jens Axboea6da0022017-11-05 09:16:09 -0700550 get_probe_ref();
Li Zefancbe28292009-03-20 09:48:47 +0800551
Omar Sandoval6ac93112017-01-31 14:53:22 -0800552 ret = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100553err:
Omar Sandoval6ac93112017-01-31 14:53:22 -0800554 if (dir && !bt->dir)
555 dput(dir);
556 if (ret)
557 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100558 return ret;
559}
560
Jens Axboe1f2cac12017-11-05 09:13:48 -0700561static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
562 struct block_device *bdev, char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200563{
564 struct blk_user_trace_setup buts;
565 int ret;
566
567 ret = copy_from_user(&buts, arg, sizeof(buts));
568 if (ret)
569 return -EFAULT;
570
Shawn Dud0deef52009-04-14 13:58:56 +0800571 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200572 if (ret)
573 return ret;
574
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300575 if (copy_to_user(arg, &buts, sizeof(buts))) {
Jens Axboe2967acb2017-11-19 11:52:55 -0700576 __blk_trace_remove(q);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200577 return -EFAULT;
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300578 }
Arnd Bergmann171044d42007-10-09 13:23:53 +0200579 return 0;
580}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700581
582int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
583 struct block_device *bdev,
584 char __user *arg)
585{
586 int ret;
587
588 mutex_lock(&q->blk_trace_mutex);
589 ret = __blk_trace_setup(q, name, dev, bdev, arg);
590 mutex_unlock(&q->blk_trace_mutex);
591
592 return ret;
593}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100594EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200595
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200596#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
597static int compat_blk_trace_setup(struct request_queue *q, char *name,
598 dev_t dev, struct block_device *bdev,
599 char __user *arg)
600{
601 struct blk_user_trace_setup buts;
602 struct compat_blk_user_trace_setup cbuts;
603 int ret;
604
605 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
606 return -EFAULT;
607
608 buts = (struct blk_user_trace_setup) {
609 .act_mask = cbuts.act_mask,
610 .buf_size = cbuts.buf_size,
611 .buf_nr = cbuts.buf_nr,
612 .start_lba = cbuts.start_lba,
613 .end_lba = cbuts.end_lba,
614 .pid = cbuts.pid,
615 };
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200616
617 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
618 if (ret)
619 return ret;
620
Chen Gangf8c5e942013-11-03 22:23:39 +0800621 if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
Jens Axboe2967acb2017-11-19 11:52:55 -0700622 __blk_trace_remove(q);
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200623 return -EFAULT;
624 }
625
626 return 0;
627}
628#endif
629
Jens Axboe1f2cac12017-11-05 09:13:48 -0700630static int __blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100631{
Jens Axboe2056a782006-03-23 20:00:26 +0100632 int ret;
Jan Karac780e862020-02-06 15:28:12 +0100633 struct blk_trace *bt;
Jens Axboe2056a782006-03-23 20:00:26 +0100634
Jan Karac780e862020-02-06 15:28:12 +0100635 bt = rcu_dereference_protected(q->blk_trace,
636 lockdep_is_held(&q->blk_trace_mutex));
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200637 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100638 return -EINVAL;
639
640 /*
641 * For starting a trace, we can transition from a setup or stopped
642 * trace. For stopping a trace, the state must be running
643 */
644 ret = -EINVAL;
645 if (start) {
646 if (bt->trace_state == Blktrace_setup ||
647 bt->trace_state == Blktrace_stopped) {
648 blktrace_seq++;
649 smp_mb();
650 bt->trace_state = Blktrace_running;
Jan Karaa404d552013-09-17 22:30:31 +0200651 spin_lock_irq(&running_trace_lock);
652 list_add(&bt->running_list, &running_trace_list);
653 spin_unlock_irq(&running_trace_lock);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100654
655 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100656 ret = 0;
657 }
658 } else {
659 if (bt->trace_state == Blktrace_running) {
660 bt->trace_state = Blktrace_stopped;
Jan Karaa404d552013-09-17 22:30:31 +0200661 spin_lock_irq(&running_trace_lock);
662 list_del_init(&bt->running_list);
663 spin_unlock_irq(&running_trace_lock);
Jens Axboe2056a782006-03-23 20:00:26 +0100664 relay_flush(bt->rchan);
665 ret = 0;
666 }
667 }
668
669 return ret;
670}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700671
672int blk_trace_startstop(struct request_queue *q, int start)
673{
674 int ret;
675
676 mutex_lock(&q->blk_trace_mutex);
677 ret = __blk_trace_startstop(q, start);
678 mutex_unlock(&q->blk_trace_mutex);
679
680 return ret;
681}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100682EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100683
Waiman Long5acb3cc2017-09-20 13:12:20 -0600684/*
685 * When reading or writing the blktrace sysfs files, the references to the
686 * opened sysfs or device files should prevent the underlying block device
687 * from being removed. So no further delete protection is really needed.
688 */
689
Jens Axboe2056a782006-03-23 20:00:26 +0100690/**
691 * blk_trace_ioctl: - handle the ioctls associated with tracing
692 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400693 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100694 * @arg: the argument data, if any
695 *
696 **/
697int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
698{
Jens Axboe165125e2007-07-24 09:28:11 +0200699 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100700 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100701 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100702
703 q = bdev_get_queue(bdev);
704 if (!q)
705 return -ENXIO;
706
Waiman Long5acb3cc2017-09-20 13:12:20 -0600707 mutex_lock(&q->blk_trace_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100708
709 switch (cmd) {
710 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700711 bdevname(bdev, b);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700712 ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100713 break;
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200714#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
715 case BLKTRACESETUP32:
716 bdevname(bdev, b);
717 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
718 break;
719#endif
Jens Axboe2056a782006-03-23 20:00:26 +0100720 case BLKTRACESTART:
721 start = 1;
Mathieu Malaterref6d85f02019-01-14 21:31:13 +0100722 /* fall through */
Jens Axboe2056a782006-03-23 20:00:26 +0100723 case BLKTRACESTOP:
Jens Axboe1f2cac12017-11-05 09:13:48 -0700724 ret = __blk_trace_startstop(q, start);
Jens Axboe2056a782006-03-23 20:00:26 +0100725 break;
726 case BLKTRACETEARDOWN:
Jens Axboe1f2cac12017-11-05 09:13:48 -0700727 ret = __blk_trace_remove(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100728 break;
729 default:
730 ret = -ENOTTY;
731 break;
732 }
733
Waiman Long5acb3cc2017-09-20 13:12:20 -0600734 mutex_unlock(&q->blk_trace_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100735 return ret;
736}
737
738/**
739 * blk_trace_shutdown: - stop and cleanup trace structures
740 * @q: the request queue associated with the device
741 *
742 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200743void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100744{
Jens Axboe1f2cac12017-11-05 09:13:48 -0700745 mutex_lock(&q->blk_trace_mutex);
Jan Karac780e862020-02-06 15:28:12 +0100746 if (rcu_dereference_protected(q->blk_trace,
747 lockdep_is_held(&q->blk_trace_mutex))) {
Jens Axboe1f2cac12017-11-05 09:13:48 -0700748 __blk_trace_startstop(q, 0);
749 __blk_trace_remove(q);
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700750 }
Jens Axboe1f2cac12017-11-05 09:13:48 -0700751
752 mutex_unlock(&q->blk_trace_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100753}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100754
Shaohua Lica1136c2017-07-12 11:49:53 -0700755#ifdef CONFIG_BLK_CGROUP
Tejun Heo67c04962019-11-04 15:54:30 -0800756static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
Shaohua Lica1136c2017-07-12 11:49:53 -0700757{
Jan Karac780e862020-02-06 15:28:12 +0100758 struct blk_trace *bt;
Shaohua Lica1136c2017-07-12 11:49:53 -0700759
Jan Karac780e862020-02-06 15:28:12 +0100760 /* We don't use the 'bt' value here except as an optimization... */
761 bt = rcu_dereference_protected(q->blk_trace, 1);
Shaohua Lica1136c2017-07-12 11:49:53 -0700762 if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
Tejun Heo67c04962019-11-04 15:54:30 -0800763 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700764
Dennis Zhoudb6638d2018-12-05 12:10:35 -0500765 if (!bio->bi_blkg)
Tejun Heo67c04962019-11-04 15:54:30 -0800766 return 0;
Tejun Heo74321032019-11-04 15:54:30 -0800767 return cgroup_id(bio_blkcg(bio)->css.cgroup);
Shaohua Lica1136c2017-07-12 11:49:53 -0700768}
769#else
Tejun Heo67c04962019-11-04 15:54:30 -0800770u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
Shaohua Lica1136c2017-07-12 11:49:53 -0700771{
Tejun Heo67c04962019-11-04 15:54:30 -0800772 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700773}
774#endif
775
Tejun Heo67c04962019-11-04 15:54:30 -0800776static u64
Shaohua Lica1136c2017-07-12 11:49:53 -0700777blk_trace_request_get_cgid(struct request_queue *q, struct request *rq)
778{
779 if (!rq->bio)
Tejun Heo67c04962019-11-04 15:54:30 -0800780 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700781 /* Use the first bio */
782 return blk_trace_bio_get_cgid(q, rq->bio);
783}
784
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100785/*
786 * blktrace probes
787 */
788
789/**
790 * blk_add_trace_rq - Add a trace for a request oriented action
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100791 * @rq: the source request
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200792 * @error: return status to log
Roman Penaf5040d2014-03-04 23:13:10 +0900793 * @nr_bytes: number of completed bytes
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100794 * @what: the action
Shaohua Lica1136c2017-07-12 11:49:53 -0700795 * @cgid: the cgroup info
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100796 *
797 * Description:
798 * Records an action against a request. Will log the bio offset + size.
799 *
800 **/
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200801static void blk_add_trace_rq(struct request *rq, int error,
Tejun Heo67c04962019-11-04 15:54:30 -0800802 unsigned int nr_bytes, u32 what, u64 cgid)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100803{
Jan Karac780e862020-02-06 15:28:12 +0100804 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100805
Jan Karac780e862020-02-06 15:28:12 +0100806 rcu_read_lock();
807 bt = rcu_dereference(rq->q->blk_trace);
808 if (likely(!bt)) {
809 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100810 return;
Jan Karac780e862020-02-06 15:28:12 +0100811 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100812
Christoph Hellwig57292b52017-01-31 16:57:29 +0100813 if (blk_rq_is_passthrough(rq))
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100814 what |= BLK_TC_ACT(BLK_TC_PC);
Christoph Hellwig48b77ad2017-01-27 09:35:54 +0100815 else
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100816 what |= BLK_TC_ACT(BLK_TC_FS);
Christoph Hellwig48b77ad2017-01-27 09:35:54 +0100817
818 __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, req_op(rq),
Shaohua Lica1136c2017-07-12 11:49:53 -0700819 rq->cmd_flags, what, error, 0, NULL, cgid);
Jan Karac780e862020-02-06 15:28:12 +0100820 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100821}
822
Steven Rostedt38516ab2010-04-20 17:04:50 -0400823static void blk_add_trace_rq_insert(void *ignore,
824 struct request_queue *q, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100825{
Shaohua Lica1136c2017-07-12 11:49:53 -0700826 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT,
827 blk_trace_request_get_cgid(q, rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100828}
829
Steven Rostedt38516ab2010-04-20 17:04:50 -0400830static void blk_add_trace_rq_issue(void *ignore,
831 struct request_queue *q, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100832{
Shaohua Lica1136c2017-07-12 11:49:53 -0700833 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE,
834 blk_trace_request_get_cgid(q, rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100835}
836
Steven Rostedt38516ab2010-04-20 17:04:50 -0400837static void blk_add_trace_rq_requeue(void *ignore,
838 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200839 struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100840{
Shaohua Lica1136c2017-07-12 11:49:53 -0700841 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE,
842 blk_trace_request_get_cgid(q, rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100843}
844
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200845static void blk_add_trace_rq_complete(void *ignore, struct request *rq,
846 int error, unsigned int nr_bytes)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100847{
Shaohua Lica1136c2017-07-12 11:49:53 -0700848 blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE,
849 blk_trace_request_get_cgid(rq->q, rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100850}
851
852/**
853 * blk_add_trace_bio - Add a trace for a bio oriented action
854 * @q: queue the io is for
855 * @bio: the source bio
856 * @what: the action
Jens Axboe797a4552011-01-10 09:06:44 +0100857 * @error: error, if any
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100858 *
859 * Description:
860 * Records an action against a bio. Will log the bio offset + size.
861 *
862 **/
863static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200864 u32 what, int error)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100865{
Jan Karac780e862020-02-06 15:28:12 +0100866 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100867
Jan Karac780e862020-02-06 15:28:12 +0100868 rcu_read_lock();
869 bt = rcu_dereference(q->blk_trace);
870 if (likely(!bt)) {
871 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100872 return;
Jan Karac780e862020-02-06 15:28:12 +0100873 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100874
Kent Overstreet4f024f32013-10-11 15:44:27 -0700875 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200876 bio_op(bio), bio->bi_opf, what, error, 0, NULL,
877 blk_trace_bio_get_cgid(q, bio));
Jan Karac780e862020-02-06 15:28:12 +0100878 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100879}
880
Steven Rostedt38516ab2010-04-20 17:04:50 -0400881static void blk_add_trace_bio_bounce(void *ignore,
882 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100883{
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200884 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100885}
886
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700887static void blk_add_trace_bio_complete(void *ignore,
Christoph Hellwigd24de76a2020-06-03 07:14:43 +0200888 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100889{
Christoph Hellwigd24de76a2020-06-03 07:14:43 +0200890 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE,
891 blk_status_to_errno(bio->bi_status));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100892}
893
Steven Rostedt38516ab2010-04-20 17:04:50 -0400894static void blk_add_trace_bio_backmerge(void *ignore,
895 struct request_queue *q,
Tejun Heo8c1cf6b2013-01-11 13:06:34 -0800896 struct request *rq,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200897 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100898{
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200899 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100900}
901
Steven Rostedt38516ab2010-04-20 17:04:50 -0400902static void blk_add_trace_bio_frontmerge(void *ignore,
903 struct request_queue *q,
Tejun Heo8c1cf6b2013-01-11 13:06:34 -0800904 struct request *rq,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200905 struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100906{
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200907 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100908}
909
Steven Rostedt38516ab2010-04-20 17:04:50 -0400910static void blk_add_trace_bio_queue(void *ignore,
911 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100912{
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200913 blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100914}
915
Steven Rostedt38516ab2010-04-20 17:04:50 -0400916static void blk_add_trace_getrq(void *ignore,
917 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200918 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100919{
920 if (bio)
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200921 blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100922 else {
Jan Karac780e862020-02-06 15:28:12 +0100923 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100924
Jan Karac780e862020-02-06 15:28:12 +0100925 rcu_read_lock();
926 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100927 if (bt)
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500928 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_GETRQ, 0, 0,
Tejun Heo67c04962019-11-04 15:54:30 -0800929 NULL, 0);
Jan Karac780e862020-02-06 15:28:12 +0100930 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100931 }
932}
933
934
Steven Rostedt38516ab2010-04-20 17:04:50 -0400935static void blk_add_trace_sleeprq(void *ignore,
936 struct request_queue *q,
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200937 struct bio *bio, int rw)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100938{
939 if (bio)
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200940 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100941 else {
Jan Karac780e862020-02-06 15:28:12 +0100942 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100943
Jan Karac780e862020-02-06 15:28:12 +0100944 rcu_read_lock();
945 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100946 if (bt)
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500947 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ,
Tejun Heo67c04962019-11-04 15:54:30 -0800948 0, 0, NULL, 0);
Jan Karac780e862020-02-06 15:28:12 +0100949 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100950 }
951}
952
Steven Rostedt38516ab2010-04-20 17:04:50 -0400953static void blk_add_trace_plug(void *ignore, struct request_queue *q)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100954{
Jan Karac780e862020-02-06 15:28:12 +0100955 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100956
Jan Karac780e862020-02-06 15:28:12 +0100957 rcu_read_lock();
958 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100959 if (bt)
Tejun Heo67c04962019-11-04 15:54:30 -0800960 __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL, 0);
Jan Karac780e862020-02-06 15:28:12 +0100961 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100962}
963
Jens Axboe49cac012011-04-16 13:51:05 +0200964static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
965 unsigned int depth, bool explicit)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100966{
Jan Karac780e862020-02-06 15:28:12 +0100967 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100968
Jan Karac780e862020-02-06 15:28:12 +0100969 rcu_read_lock();
970 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100971 if (bt) {
Jens Axboe94b5eb22011-04-12 10:12:19 +0200972 __be64 rpdu = cpu_to_be64(depth);
Jens Axboe49cac012011-04-16 13:51:05 +0200973 u32 what;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100974
Jens Axboe49cac012011-04-16 13:51:05 +0200975 if (explicit)
976 what = BLK_TA_UNPLUG_IO;
977 else
978 what = BLK_TA_UNPLUG_TIMER;
979
Tejun Heo67c04962019-11-04 15:54:30 -0800980 __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100981 }
Jan Karac780e862020-02-06 15:28:12 +0100982 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100983}
984
Steven Rostedt38516ab2010-04-20 17:04:50 -0400985static void blk_add_trace_split(void *ignore,
986 struct request_queue *q, struct bio *bio,
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100987 unsigned int pdu)
988{
Jan Karac780e862020-02-06 15:28:12 +0100989 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100990
Jan Karac780e862020-02-06 15:28:12 +0100991 rcu_read_lock();
992 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100993 if (bt) {
994 __be64 rpdu = cpu_to_be64(pdu);
995
Kent Overstreet4f024f32013-10-11 15:44:27 -0700996 __blk_add_trace(bt, bio->bi_iter.bi_sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -0600997 bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200998 BLK_TA_SPLIT, bio->bi_status, sizeof(rpdu),
Shaohua Lica1136c2017-07-12 11:49:53 -0700999 &rpdu, blk_trace_bio_get_cgid(q, bio));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001000 }
Jan Karac780e862020-02-06 15:28:12 +01001001 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001002}
1003
1004/**
Mike Snitzerd07335e2010-11-16 12:52:38 +01001005 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -07001006 * @ignore: trace callback data parameter (not used)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001007 * @q: queue the io is for
1008 * @bio: the source bio
1009 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001010 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001011 *
1012 * Description:
1013 * Device mapper or raid target sometimes need to split a bio because
1014 * it spans a stripe (or similar). Add a trace for that action.
1015 *
1016 **/
Mike Snitzerd07335e2010-11-16 12:52:38 +01001017static void blk_add_trace_bio_remap(void *ignore,
1018 struct request_queue *q, struct bio *bio,
1019 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001020{
Jan Karac780e862020-02-06 15:28:12 +01001021 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001022 struct blk_io_trace_remap r;
1023
Jan Karac780e862020-02-06 15:28:12 +01001024 rcu_read_lock();
1025 bt = rcu_dereference(q->blk_trace);
1026 if (likely(!bt)) {
1027 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001028 return;
Jan Karac780e862020-02-06 15:28:12 +01001029 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001030
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001031 r.device_from = cpu_to_be32(dev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001032 r.device_to = cpu_to_be32(bio_dev(bio));
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001033 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001034
Kent Overstreet4f024f32013-10-11 15:44:27 -07001035 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001036 bio_op(bio), bio->bi_opf, BLK_TA_REMAP, bio->bi_status,
Shaohua Lica1136c2017-07-12 11:49:53 -07001037 sizeof(r), &r, blk_trace_bio_get_cgid(q, bio));
Jan Karac780e862020-02-06 15:28:12 +01001038 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001039}
1040
1041/**
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001042 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -07001043 * @ignore: trace callback data parameter (not used)
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001044 * @q: queue the io is for
1045 * @rq: the source request
1046 * @dev: target device
1047 * @from: source sector
1048 *
1049 * Description:
1050 * Device mapper remaps request to other devices.
1051 * Add a trace for that action.
1052 *
1053 **/
Steven Rostedt38516ab2010-04-20 17:04:50 -04001054static void blk_add_trace_rq_remap(void *ignore,
1055 struct request_queue *q,
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001056 struct request *rq, dev_t dev,
1057 sector_t from)
1058{
Jan Karac780e862020-02-06 15:28:12 +01001059 struct blk_trace *bt;
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001060 struct blk_io_trace_remap r;
1061
Jan Karac780e862020-02-06 15:28:12 +01001062 rcu_read_lock();
1063 bt = rcu_dereference(q->blk_trace);
1064 if (likely(!bt)) {
1065 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001066 return;
Jan Karac780e862020-02-06 15:28:12 +01001067 }
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001068
1069 r.device_from = cpu_to_be32(dev);
1070 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
1071 r.sector_from = cpu_to_be64(from);
1072
1073 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
Christoph Hellwigcaf7df12017-04-20 16:03:16 +02001074 rq_data_dir(rq), 0, BLK_TA_REMAP, 0,
Shaohua Lica1136c2017-07-12 11:49:53 -07001075 sizeof(r), &r, blk_trace_request_get_cgid(q, rq));
Jan Karac780e862020-02-06 15:28:12 +01001076 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001077}
1078
1079/**
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001080 * blk_add_driver_data - Add binary message with driver-specific data
1081 * @q: queue the io is for
1082 * @rq: io request
1083 * @data: driver-specific data
1084 * @len: length of driver-specific data
1085 *
1086 * Description:
1087 * Some drivers might want to write driver-specific data per request.
1088 *
1089 **/
1090void blk_add_driver_data(struct request_queue *q,
1091 struct request *rq,
1092 void *data, size_t len)
1093{
Jan Karac780e862020-02-06 15:28:12 +01001094 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001095
Jan Karac780e862020-02-06 15:28:12 +01001096 rcu_read_lock();
1097 bt = rcu_dereference(q->blk_trace);
1098 if (likely(!bt)) {
1099 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001100 return;
Jan Karac780e862020-02-06 15:28:12 +01001101 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001102
Christoph Hellwig48b77ad2017-01-27 09:35:54 +01001103 __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0,
Shaohua Lica1136c2017-07-12 11:49:53 -07001104 BLK_TA_DRV_DATA, 0, len, data,
1105 blk_trace_request_get_cgid(q, rq));
Jan Karac780e862020-02-06 15:28:12 +01001106 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001107}
1108EXPORT_SYMBOL_GPL(blk_add_driver_data);
1109
Li Zefan3c289ba2009-03-20 09:48:26 +08001110static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001111{
1112 int ret;
1113
Steven Rostedt38516ab2010-04-20 17:04:50 -04001114 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001115 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001116 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001117 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001118 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001119 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001120 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001121 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001122 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001123 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001124 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001125 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001126 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001127 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001128 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001129 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001130 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001131 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001132 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001133 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001134 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001135 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001136 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001137 WARN_ON(ret);
Jens Axboe49cac012011-04-16 13:51:05 +02001138 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001139 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001140 ret = register_trace_block_split(blk_add_trace_split, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001141 WARN_ON(ret);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001142 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001143 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001144 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001145 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001146}
1147
1148static void blk_unregister_tracepoints(void)
1149{
Steven Rostedt38516ab2010-04-20 17:04:50 -04001150 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001151 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001152 unregister_trace_block_split(blk_add_trace_split, NULL);
Jens Axboe49cac012011-04-16 13:51:05 +02001153 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001154 unregister_trace_block_plug(blk_add_trace_plug, NULL);
1155 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1156 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1157 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1158 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1159 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1160 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1161 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1162 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1163 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1164 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1165 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001166
1167 tracepoint_synchronize_unregister();
1168}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001169
1170/*
1171 * struct blk_io_tracer formatting routines
1172 */
1173
1174static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1175{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001176 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +08001177 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001178
Shaohua Lica1136c2017-07-12 11:49:53 -07001179 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
Li Zefan18cea452009-03-27 10:21:54 +08001180 rwbs[i++] = 'N';
1181 goto out;
1182 }
1183
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001184 if (tc & BLK_TC_FLUSH)
1185 rwbs[i++] = 'F';
1186
Li Zefan65796342009-03-24 16:05:06 +08001187 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001188 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +08001189 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001190 rwbs[i++] = 'W';
1191 else if (t->bytes)
1192 rwbs[i++] = 'R';
1193 else
1194 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001195
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001196 if (tc & BLK_TC_FUA)
1197 rwbs[i++] = 'F';
Li Zefan65796342009-03-24 16:05:06 +08001198 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001199 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +08001200 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001201 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +08001202 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001203 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +08001204out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001205 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001206}
1207
1208static inline
1209const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1210{
1211 return (const struct blk_io_trace *)ent;
1212}
1213
Shaohua Lica1136c2017-07-12 11:49:53 -07001214static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001215{
Tejun Heo67c04962019-11-04 15:54:30 -08001216 return (void *)(te_blk_io_trace(ent) + 1) + (has_cg ? sizeof(u64) : 0);
Shaohua Lica1136c2017-07-12 11:49:53 -07001217}
1218
Tejun Heo67c04962019-11-04 15:54:30 -08001219static inline u64 t_cgid(const struct trace_entry *ent)
Shaohua Lica1136c2017-07-12 11:49:53 -07001220{
Tejun Heo67c04962019-11-04 15:54:30 -08001221 return *(u64 *)(te_blk_io_trace(ent) + 1);
Shaohua Lica1136c2017-07-12 11:49:53 -07001222}
1223
1224static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg)
1225{
Tejun Heo67c04962019-11-04 15:54:30 -08001226 return te_blk_io_trace(ent)->pdu_len - (has_cg ? sizeof(u64) : 0);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001227}
1228
Li Zefan66de7792009-04-01 16:19:19 +08001229static inline u32 t_action(const struct trace_entry *ent)
1230{
1231 return te_blk_io_trace(ent)->action;
1232}
1233
1234static inline u32 t_bytes(const struct trace_entry *ent)
1235{
1236 return te_blk_io_trace(ent)->bytes;
1237}
1238
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001239static inline u32 t_sec(const struct trace_entry *ent)
1240{
1241 return te_blk_io_trace(ent)->bytes >> 9;
1242}
1243
1244static inline unsigned long long t_sector(const struct trace_entry *ent)
1245{
1246 return te_blk_io_trace(ent)->sector;
1247}
1248
1249static inline __u16 t_error(const struct trace_entry *ent)
1250{
Li Zefane0dc81b2009-03-24 16:05:51 +08001251 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001252}
1253
Shaohua Lica1136c2017-07-12 11:49:53 -07001254static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001255{
Shaohua Lica1136c2017-07-12 11:49:53 -07001256 const __u64 *val = pdu_start(ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001257 return be64_to_cpu(*val);
1258}
1259
1260static void get_pdu_remap(const struct trace_entry *ent,
Shaohua Lica1136c2017-07-12 11:49:53 -07001261 struct blk_io_trace_remap *r, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001262{
Shaohua Lica1136c2017-07-12 11:49:53 -07001263 const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001264 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001265
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001266 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001267 r->device_to = be32_to_cpu(__r->device_to);
1268 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001269}
1270
Shaohua Lica1136c2017-07-12 11:49:53 -07001271typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act,
1272 bool has_cg);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001273
Shaohua Lica1136c2017-07-12 11:49:53 -07001274static void blk_log_action_classic(struct trace_iterator *iter, const char *act,
1275 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001276{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001277 char rwbs[RWBS_LEN];
Li Zefan35ac51b2009-03-27 10:19:46 +08001278 unsigned long long ts = iter->ts;
1279 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001280 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001281 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001282
1283 fill_rwbs(rwbs, t);
1284
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001285 trace_seq_printf(&iter->seq,
1286 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1287 MAJOR(t->device), MINOR(t->device), iter->cpu,
1288 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001289}
1290
Shaohua Lica1136c2017-07-12 11:49:53 -07001291static void blk_log_action(struct trace_iterator *iter, const char *act,
1292 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001293{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001294 char rwbs[RWBS_LEN];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001295 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1296
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001297 fill_rwbs(rwbs, t);
Shaohua Lica1136c2017-07-12 11:49:53 -07001298 if (has_cg) {
Tejun Heo67c04962019-11-04 15:54:30 -08001299 u64 id = t_cgid(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001300
Shaohua Li69fd5c32017-07-12 11:49:55 -07001301 if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) {
1302 char blkcg_name_buf[NAME_MAX + 1] = "<...>";
1303
1304 cgroup_path_from_kernfs_id(id, blkcg_name_buf,
1305 sizeof(blkcg_name_buf));
1306 trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ",
1307 MAJOR(t->device), MINOR(t->device),
1308 blkcg_name_buf, act, rwbs);
Tejun Heo40430452019-11-04 15:54:30 -08001309 } else {
1310 /*
1311 * The cgid portion used to be "INO,GEN". Userland
1312 * builds a FILEID_INO32_GEN fid out of them and
1313 * opens the cgroup using open_by_handle_at(2).
1314 * While 32bit ino setups are still the same, 64bit
1315 * ones now use the 64bit ino as the whole ID and
1316 * no longer use generation.
1317 *
1318 * Regarldess of the content, always output
1319 * "LOW32,HIGH32" so that FILEID_INO32_GEN fid can
1320 * be mapped back to @id on both 64 and 32bit ino
1321 * setups. See __kernfs_fh_to_dentry().
1322 */
Shaohua Li69fd5c32017-07-12 11:49:55 -07001323 trace_seq_printf(&iter->seq,
Tejun Heo40430452019-11-04 15:54:30 -08001324 "%3d,%-3d %llx,%-llx %2s %3s ",
Shaohua Lica1136c2017-07-12 11:49:53 -07001325 MAJOR(t->device), MINOR(t->device),
Tejun Heo40430452019-11-04 15:54:30 -08001326 id & U32_MAX, id >> 32, act, rwbs);
1327 }
Shaohua Lica1136c2017-07-12 11:49:53 -07001328 } else
1329 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1330 MAJOR(t->device), MINOR(t->device), act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001331}
1332
Shaohua Lica1136c2017-07-12 11:49:53 -07001333static void blk_log_dump_pdu(struct trace_seq *s,
1334 const struct trace_entry *ent, bool has_cg)
Li Zefan66de7792009-04-01 16:19:19 +08001335{
Li Zefan049862572009-05-11 14:33:23 +08001336 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001337 int pdu_len;
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001338 int i, end;
Li Zefan66de7792009-04-01 16:19:19 +08001339
Shaohua Lica1136c2017-07-12 11:49:53 -07001340 pdu_buf = pdu_start(ent, has_cg);
1341 pdu_len = pdu_real_len(ent, has_cg);
Li Zefan66de7792009-04-01 16:19:19 +08001342
1343 if (!pdu_len)
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001344 return;
Li Zefan66de7792009-04-01 16:19:19 +08001345
1346 /* find the last zero that needs to be printed */
1347 for (end = pdu_len - 1; end >= 0; end--)
1348 if (pdu_buf[end])
1349 break;
1350 end++;
1351
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001352 trace_seq_putc(s, '(');
Li Zefan66de7792009-04-01 16:19:19 +08001353
1354 for (i = 0; i < pdu_len; i++) {
1355
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001356 trace_seq_printf(s, "%s%02x",
1357 i == 0 ? "" : " ", pdu_buf[i]);
Li Zefan66de7792009-04-01 16:19:19 +08001358
1359 /*
1360 * stop when the rest is just zeroes and indicate so
1361 * with a ".." appended
1362 */
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001363 if (i == end && end != pdu_len - 1) {
1364 trace_seq_puts(s, " ..) ");
1365 return;
1366 }
Li Zefan66de7792009-04-01 16:19:19 +08001367 }
1368
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001369 trace_seq_puts(s, ") ");
Li Zefan66de7792009-04-01 16:19:19 +08001370}
1371
Shaohua Lica1136c2017-07-12 11:49:53 -07001372static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001373{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001374 char cmd[TASK_COMM_LEN];
1375
1376 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001377
Li Zefan66de7792009-04-01 16:19:19 +08001378 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001379 trace_seq_printf(s, "%u ", t_bytes(ent));
Shaohua Lica1136c2017-07-12 11:49:53 -07001380 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001381 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001382 } else {
1383 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001384 trace_seq_printf(s, "%llu + %u [%s]\n",
Li Zefan66de7792009-04-01 16:19:19 +08001385 t_sector(ent), t_sec(ent), cmd);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001386 else
1387 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001388 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001389}
1390
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001391static void blk_log_with_error(struct trace_seq *s,
Shaohua Lica1136c2017-07-12 11:49:53 -07001392 const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001393{
Li Zefan66de7792009-04-01 16:19:19 +08001394 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Shaohua Lica1136c2017-07-12 11:49:53 -07001395 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001396 trace_seq_printf(s, "[%d]\n", t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001397 } else {
1398 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001399 trace_seq_printf(s, "%llu + %u [%d]\n",
1400 t_sector(ent),
1401 t_sec(ent), t_error(ent));
1402 else
1403 trace_seq_printf(s, "%llu [%d]\n",
1404 t_sector(ent), t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001405 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001406}
1407
Shaohua Lica1136c2017-07-12 11:49:53 -07001408static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001409{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001410 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001411
Shaohua Lica1136c2017-07-12 11:49:53 -07001412 get_pdu_remap(ent, &r, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001413 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1414 t_sector(ent), t_sec(ent),
1415 MAJOR(r.device_from), MINOR(r.device_from),
1416 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001417}
1418
Shaohua Lica1136c2017-07-12 11:49:53 -07001419static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001420{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001421 char cmd[TASK_COMM_LEN];
1422
1423 trace_find_cmdline(ent->pid, cmd);
1424
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001425 trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001426}
1427
Shaohua Lica1136c2017-07-12 11:49:53 -07001428static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001429{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001430 char cmd[TASK_COMM_LEN];
1431
1432 trace_find_cmdline(ent->pid, cmd);
1433
Shaohua Lica1136c2017-07-12 11:49:53 -07001434 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001435}
1436
Shaohua Lica1136c2017-07-12 11:49:53 -07001437static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001438{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001439 char cmd[TASK_COMM_LEN];
1440
1441 trace_find_cmdline(ent->pid, cmd);
1442
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001443 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Shaohua Lica1136c2017-07-12 11:49:53 -07001444 get_pdu_int(ent, has_cg), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001445}
1446
Shaohua Lica1136c2017-07-12 11:49:53 -07001447static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent,
1448 bool has_cg)
Li Zefan18cea452009-03-27 10:21:54 +08001449{
Li Zefan18cea452009-03-27 10:21:54 +08001450
Shaohua Lica1136c2017-07-12 11:49:53 -07001451 trace_seq_putmem(s, pdu_start(ent, has_cg),
1452 pdu_real_len(ent, has_cg));
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001453 trace_seq_putc(s, '\n');
Li Zefan18cea452009-03-27 10:21:54 +08001454}
1455
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001456/*
1457 * struct tracer operations
1458 */
1459
1460static void blk_tracer_print_header(struct seq_file *m)
1461{
1462 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1463 return;
1464 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1465 "# | | | | | |\n");
1466}
1467
1468static void blk_tracer_start(struct trace_array *tr)
1469{
Li Zefanad5dd542009-03-27 10:20:24 +08001470 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001471}
1472
1473static int blk_tracer_init(struct trace_array *tr)
1474{
1475 blk_tr = tr;
1476 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001477 return 0;
1478}
1479
1480static void blk_tracer_stop(struct trace_array *tr)
1481{
Li Zefanad5dd542009-03-27 10:20:24 +08001482 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001483}
1484
1485static void blk_tracer_reset(struct trace_array *tr)
1486{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001487 blk_tracer_stop(tr);
1488}
1489
Li Zefane4955c92009-03-24 16:04:37 +08001490static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001491 const char *act[2];
Shaohua Lica1136c2017-07-12 11:49:53 -07001492 void (*print)(struct trace_seq *s, const struct trace_entry *ent,
1493 bool has_cg);
Li Zefane4955c92009-03-24 16:04:37 +08001494} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001495 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001496 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1497 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1498 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1499 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1500 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1501 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1502 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1503 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1504 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
Jens Axboe49cac012011-04-16 13:51:05 +02001505 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001506 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1507 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1508 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1509 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1510};
1511
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001512static enum print_line_t print_one_line(struct trace_iterator *iter,
1513 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001514{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001515 struct trace_array *tr = iter->tr;
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001516 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001517 const struct blk_io_trace *t;
1518 u16 what;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001519 bool long_act;
1520 blk_log_action_t *log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001521 bool has_cg;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001522
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001523 t = te_blk_io_trace(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001524 what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001525 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001526 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001527 has_cg = t->action & __BLK_TA_CGROUP;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001528
Shaohua Lica1136c2017-07-12 11:49:53 -07001529 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
1530 log_action(iter, long_act ? "message" : "m", has_cg);
1531 blk_log_msg(s, iter->ent, has_cg);
Shaohua Lib7d76412016-05-09 17:22:13 -07001532 return trace_handle_return(s);
Li Zefan18cea452009-03-27 10:21:54 +08001533 }
1534
Li Zefaneb08f8e2009-03-24 16:05:27 +08001535 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001536 trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001537 else {
Shaohua Lica1136c2017-07-12 11:49:53 -07001538 log_action(iter, what2act[what].act[long_act], has_cg);
1539 what2act[what].print(s, iter->ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001540 }
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001541
1542 return trace_handle_return(s);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001543}
1544
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001545static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
Steven Rostedta9a57762010-04-22 18:46:14 -04001546 int flags, struct trace_event *event)
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001547{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001548 return print_one_line(iter, false);
1549}
1550
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001551static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001552{
1553 struct trace_seq *s = &iter->seq;
1554 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1555 const int offset = offsetof(struct blk_io_trace, sector);
1556 struct blk_io_trace old = {
1557 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001558 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001559 };
1560
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001561 trace_seq_putmem(s, &old, offset);
1562 trace_seq_putmem(s, &t->sector,
1563 sizeof(old) - offset + t->pdu_len);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001564}
1565
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001566static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001567blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1568 struct trace_event *event)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001569{
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001570 blk_trace_synthesize_old_trace(iter);
1571
1572 return trace_handle_return(&iter->seq);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001573}
1574
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001575static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1576{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001577 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1578 return TRACE_TYPE_UNHANDLED;
1579
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001580 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001581}
1582
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05001583static int
1584blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Li Zefanf3948f82009-04-15 11:02:56 +08001585{
1586 /* don't output context-info for blk_classic output */
1587 if (bit == TRACE_BLK_OPT_CLASSIC) {
1588 if (set)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001589 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001590 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001591 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001592 }
1593 return 0;
1594}
1595
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001596static struct tracer blk_tracer __read_mostly = {
1597 .name = "blk",
1598 .init = blk_tracer_init,
1599 .reset = blk_tracer_reset,
1600 .start = blk_tracer_start,
1601 .stop = blk_tracer_stop,
1602 .print_header = blk_tracer_print_header,
1603 .print_line = blk_tracer_print_line,
1604 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001605 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001606};
1607
Steven Rostedta9a57762010-04-22 18:46:14 -04001608static struct trace_event_functions trace_blk_event_funcs = {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001609 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001610 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001611};
1612
Steven Rostedta9a57762010-04-22 18:46:14 -04001613static struct trace_event trace_blk_event = {
1614 .type = TRACE_BLK,
1615 .funcs = &trace_blk_event_funcs,
1616};
1617
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001618static int __init init_blk_tracer(void)
1619{
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001620 if (!register_trace_event(&trace_blk_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001621 pr_warn("Warning: could not register block events\n");
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001622 return 1;
1623 }
1624
1625 if (register_tracer(&blk_tracer) != 0) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001626 pr_warn("Warning: could not register the block tracer\n");
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001627 unregister_trace_event(&trace_blk_event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001628 return 1;
1629 }
1630
1631 return 0;
1632}
1633
1634device_initcall(init_blk_tracer);
1635
1636static int blk_trace_remove_queue(struct request_queue *q)
1637{
1638 struct blk_trace *bt;
1639
1640 bt = xchg(&q->blk_trace, NULL);
1641 if (bt == NULL)
1642 return -EINVAL;
1643
Jens Axboea6da0022017-11-05 09:16:09 -07001644 put_probe_ref();
Jan Karac780e862020-02-06 15:28:12 +01001645 synchronize_rcu();
Li Zefanad5dd542009-03-27 10:20:24 +08001646 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001647 return 0;
1648}
1649
1650/*
1651 * Setup everything required to start tracing
1652 */
Li Zefan9908c302009-04-14 13:59:34 +08001653static int blk_trace_setup_queue(struct request_queue *q,
1654 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001655{
Davidlohr Buesocdea01b2015-10-30 05:25:59 +09001656 struct blk_trace *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001657 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001658
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001659 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1660 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001661 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001662
Li Zefan18cea452009-03-27 10:21:54 +08001663 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1664 if (!bt->msg_data)
1665 goto free_bt;
1666
Li Zefan9908c302009-04-14 13:59:34 +08001667 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001668 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001669
1670 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001671
Davidlohr Buesocdea01b2015-10-30 05:25:59 +09001672 ret = -EBUSY;
1673 if (cmpxchg(&q->blk_trace, NULL, bt))
Li Zefan18cea452009-03-27 10:21:54 +08001674 goto free_bt;
Li Zefan15152e42009-03-20 09:49:08 +08001675
Jens Axboea6da0022017-11-05 09:16:09 -07001676 get_probe_ref();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001677 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001678
1679free_bt:
1680 blk_trace_free(bt);
1681 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001682}
1683
1684/*
1685 * sysfs interface to enable and configure tracing
1686 */
1687
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001688static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1689 struct device_attribute *attr,
1690 char *buf);
1691static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1692 struct device_attribute *attr,
1693 const char *buf, size_t count);
1694#define BLK_TRACE_DEVICE_ATTR(_name) \
1695 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1696 sysfs_blk_trace_attr_show, \
1697 sysfs_blk_trace_attr_store)
1698
Li Zefancd649b82009-03-20 11:33:55 +08001699static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001700static BLK_TRACE_DEVICE_ATTR(act_mask);
1701static BLK_TRACE_DEVICE_ATTR(pid);
1702static BLK_TRACE_DEVICE_ATTR(start_lba);
1703static BLK_TRACE_DEVICE_ATTR(end_lba);
1704
1705static struct attribute *blk_trace_attrs[] = {
1706 &dev_attr_enable.attr,
1707 &dev_attr_act_mask.attr,
1708 &dev_attr_pid.attr,
1709 &dev_attr_start_lba.attr,
1710 &dev_attr_end_lba.attr,
1711 NULL
1712};
1713
1714struct attribute_group blk_trace_attr_group = {
1715 .name = "trace",
1716 .attrs = blk_trace_attrs,
1717};
1718
Li Zefan09341992009-03-24 17:43:30 +08001719static const struct {
1720 int mask;
1721 const char *str;
1722} mask_maps[] = {
1723 { BLK_TC_READ, "read" },
1724 { BLK_TC_WRITE, "write" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001725 { BLK_TC_FLUSH, "flush" },
Li Zefan09341992009-03-24 17:43:30 +08001726 { BLK_TC_SYNC, "sync" },
1727 { BLK_TC_QUEUE, "queue" },
1728 { BLK_TC_REQUEUE, "requeue" },
1729 { BLK_TC_ISSUE, "issue" },
1730 { BLK_TC_COMPLETE, "complete" },
1731 { BLK_TC_FS, "fs" },
1732 { BLK_TC_PC, "pc" },
Shaohua Li8d1547e2016-05-09 17:22:14 -07001733 { BLK_TC_NOTIFY, "notify" },
Li Zefan09341992009-03-24 17:43:30 +08001734 { BLK_TC_AHEAD, "ahead" },
1735 { BLK_TC_META, "meta" },
1736 { BLK_TC_DISCARD, "discard" },
1737 { BLK_TC_DRV_DATA, "drv_data" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001738 { BLK_TC_FUA, "fua" },
Li Zefan09341992009-03-24 17:43:30 +08001739};
1740
1741static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001742{
Li Zefan09341992009-03-24 17:43:30 +08001743 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001744 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001745 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001746
Li Zefan9eb85122009-04-09 11:19:40 +08001747 buf = kstrdup(str, GFP_KERNEL);
1748 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001749 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001750 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001751
1752 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001753 token = strsep(&s, ",");
1754 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001755 break;
1756
Li Zefan09341992009-03-24 17:43:30 +08001757 if (*token == '\0')
1758 continue;
1759
1760 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1761 if (strcasecmp(token, mask_maps[i].str) == 0) {
1762 mask |= mask_maps[i].mask;
1763 break;
1764 }
1765 }
1766 if (i == ARRAY_SIZE(mask_maps)) {
1767 mask = -EINVAL;
1768 break;
1769 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001770 }
Li Zefan9eb85122009-04-09 11:19:40 +08001771 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001772
1773 return mask;
1774}
1775
Li Zefan09341992009-03-24 17:43:30 +08001776static ssize_t blk_trace_mask2str(char *buf, int mask)
1777{
1778 int i;
1779 char *p = buf;
1780
1781 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1782 if (mask & mask_maps[i].mask) {
1783 p += sprintf(p, "%s%s",
1784 (p == buf) ? "" : ",", mask_maps[i].str);
1785 }
1786 }
1787 *p++ = '\n';
1788
1789 return p - buf;
1790}
1791
Li Zefanb1251302009-03-20 10:34:00 +08001792static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1793{
1794 if (bdev->bd_disk == NULL)
1795 return NULL;
1796
1797 return bdev_get_queue(bdev);
1798}
1799
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001800static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1801 struct device_attribute *attr,
1802 char *buf)
1803{
1804 struct hd_struct *p = dev_to_part(dev);
1805 struct request_queue *q;
1806 struct block_device *bdev;
Jan Karac780e862020-02-06 15:28:12 +01001807 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001808 ssize_t ret = -ENXIO;
1809
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001810 bdev = bdget(part_devt(p));
1811 if (bdev == NULL)
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001812 goto out;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001813
Li Zefanb1251302009-03-20 10:34:00 +08001814 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001815 if (q == NULL)
1816 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001817
Waiman Long5acb3cc2017-09-20 13:12:20 -06001818 mutex_lock(&q->blk_trace_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001819
Jan Karac780e862020-02-06 15:28:12 +01001820 bt = rcu_dereference_protected(q->blk_trace,
1821 lockdep_is_held(&q->blk_trace_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001822 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001823 ret = sprintf(buf, "%u\n", !!bt);
Li Zefancd649b82009-03-20 11:33:55 +08001824 goto out_unlock_bdev;
1825 }
1826
Jan Karac780e862020-02-06 15:28:12 +01001827 if (bt == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001828 ret = sprintf(buf, "disabled\n");
1829 else if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001830 ret = blk_trace_mask2str(buf, bt->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001831 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001832 ret = sprintf(buf, "%u\n", bt->pid);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001833 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001834 ret = sprintf(buf, "%llu\n", bt->start_lba);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001835 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001836 ret = sprintf(buf, "%llu\n", bt->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001837
1838out_unlock_bdev:
Waiman Long5acb3cc2017-09-20 13:12:20 -06001839 mutex_unlock(&q->blk_trace_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001840out_bdput:
1841 bdput(bdev);
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001842out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001843 return ret;
1844}
1845
1846static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1847 struct device_attribute *attr,
1848 const char *buf, size_t count)
1849{
1850 struct block_device *bdev;
1851 struct request_queue *q;
1852 struct hd_struct *p;
Jan Karac780e862020-02-06 15:28:12 +01001853 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001854 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001855 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001856
1857 if (count == 0)
1858 goto out;
1859
1860 if (attr == &dev_attr_act_mask) {
Shaohua Li5f339452017-05-19 08:04:59 -07001861 if (kstrtoull(buf, 0, &value)) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001862 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001863 ret = blk_trace_str2mask(buf);
1864 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001865 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001866 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001867 }
Shaohua Li5f339452017-05-19 08:04:59 -07001868 } else if (kstrtoull(buf, 0, &value))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001869 goto out;
1870
Li Zefan09341992009-03-24 17:43:30 +08001871 ret = -ENXIO;
1872
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001873 p = dev_to_part(dev);
1874 bdev = bdget(part_devt(p));
1875 if (bdev == NULL)
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001876 goto out;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001877
Li Zefanb1251302009-03-20 10:34:00 +08001878 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001879 if (q == NULL)
1880 goto out_bdput;
1881
Waiman Long5acb3cc2017-09-20 13:12:20 -06001882 mutex_lock(&q->blk_trace_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001883
Jan Karac780e862020-02-06 15:28:12 +01001884 bt = rcu_dereference_protected(q->blk_trace,
1885 lockdep_is_held(&q->blk_trace_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001886 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001887 if (!!value == !!bt) {
Steven Rostedt (VMware)757d9142018-08-16 16:08:37 -04001888 ret = 0;
1889 goto out_unlock_bdev;
1890 }
Li Zefancd649b82009-03-20 11:33:55 +08001891 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001892 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001893 else
1894 ret = blk_trace_remove_queue(q);
1895 goto out_unlock_bdev;
1896 }
1897
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001898 ret = 0;
Cengiz Can153031a2020-03-04 13:58:19 +03001899 if (bt == NULL) {
Li Zefan9908c302009-04-14 13:59:34 +08001900 ret = blk_trace_setup_queue(q, bdev);
Cengiz Can153031a2020-03-04 13:58:19 +03001901 bt = rcu_dereference_protected(q->blk_trace,
1902 lockdep_is_held(&q->blk_trace_mutex));
1903 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001904
1905 if (ret == 0) {
1906 if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001907 bt->act_mask = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001908 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001909 bt->pid = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001910 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001911 bt->start_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001912 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001913 bt->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001914 }
Li Zefancd649b82009-03-20 11:33:55 +08001915
1916out_unlock_bdev:
Waiman Long5acb3cc2017-09-20 13:12:20 -06001917 mutex_unlock(&q->blk_trace_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001918out_bdput:
1919 bdput(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001920out:
Li Zefancd649b82009-03-20 11:33:55 +08001921 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001922}
Li Zefancd649b82009-03-20 11:33:55 +08001923
Li Zefan1d54ad62009-04-14 14:00:05 +08001924int blk_trace_init_sysfs(struct device *dev)
1925{
1926 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1927}
1928
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +02001929void blk_trace_remove_sysfs(struct device *dev)
1930{
1931 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1932}
1933
Li Zefan55782132009-06-09 13:43:05 +08001934#endif /* CONFIG_BLK_DEV_IO_TRACE */
1935
1936#ifdef CONFIG_EVENT_TRACING
1937
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001938void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes)
Li Zefan55782132009-06-09 13:43:05 +08001939{
1940 int i = 0;
1941
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001942 if (op & REQ_PREFLUSH)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001943 rwbs[i++] = 'F';
1944
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001945 switch (op & REQ_OP_MASK) {
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001946 case REQ_OP_WRITE:
1947 case REQ_OP_WRITE_SAME:
Li Zefan55782132009-06-09 13:43:05 +08001948 rwbs[i++] = 'W';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001949 break;
1950 case REQ_OP_DISCARD:
Li Zefan55782132009-06-09 13:43:05 +08001951 rwbs[i++] = 'D';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001952 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001953 case REQ_OP_SECURE_ERASE:
1954 rwbs[i++] = 'D';
1955 rwbs[i++] = 'E';
1956 break;
Mike Christie3a5e02c2016-06-05 14:32:23 -05001957 case REQ_OP_FLUSH:
1958 rwbs[i++] = 'F';
1959 break;
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001960 case REQ_OP_READ:
Li Zefan55782132009-06-09 13:43:05 +08001961 rwbs[i++] = 'R';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001962 break;
1963 default:
Li Zefan55782132009-06-09 13:43:05 +08001964 rwbs[i++] = 'N';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001965 }
Li Zefan55782132009-06-09 13:43:05 +08001966
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001967 if (op & REQ_FUA)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001968 rwbs[i++] = 'F';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001969 if (op & REQ_RAHEAD)
Li Zefan55782132009-06-09 13:43:05 +08001970 rwbs[i++] = 'A';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001971 if (op & REQ_SYNC)
Li Zefan55782132009-06-09 13:43:05 +08001972 rwbs[i++] = 'S';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001973 if (op & REQ_META)
Li Zefan55782132009-06-09 13:43:05 +08001974 rwbs[i++] = 'M';
1975
1976 rwbs[i] = '\0';
1977}
Kent Overstreet9ca8f8e2012-04-13 21:56:43 -04001978EXPORT_SYMBOL_GPL(blk_fill_rwbs);
Li Zefan55782132009-06-09 13:43:05 +08001979
Li Zefan55782132009-06-09 13:43:05 +08001980#endif /* CONFIG_EVENT_TRACING */
1981