blob: f857b4684bebec57f929a17852711f96b0a80bc1 [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,
Chaitanya Kulkarni48bc3cd2020-06-04 00:13:28 -0700998 BLK_TA_SPLIT,
999 blk_status_to_errno(bio->bi_status),
1000 sizeof(rpdu), &rpdu,
1001 blk_trace_bio_get_cgid(q, bio));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001002 }
Jan Karac780e862020-02-06 15:28:12 +01001003 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001004}
1005
1006/**
Mike Snitzerd07335e2010-11-16 12:52:38 +01001007 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -07001008 * @ignore: trace callback data parameter (not used)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001009 * @q: queue the io is for
1010 * @bio: the source bio
1011 * @dev: target device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001012 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001013 *
1014 * Description:
1015 * Device mapper or raid target sometimes need to split a bio because
1016 * it spans a stripe (or similar). Add a trace for that action.
1017 *
1018 **/
Mike Snitzerd07335e2010-11-16 12:52:38 +01001019static void blk_add_trace_bio_remap(void *ignore,
1020 struct request_queue *q, struct bio *bio,
1021 dev_t dev, sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001022{
Jan Karac780e862020-02-06 15:28:12 +01001023 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001024 struct blk_io_trace_remap r;
1025
Jan Karac780e862020-02-06 15:28:12 +01001026 rcu_read_lock();
1027 bt = rcu_dereference(q->blk_trace);
1028 if (likely(!bt)) {
1029 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001030 return;
Jan Karac780e862020-02-06 15:28:12 +01001031 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001032
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001033 r.device_from = cpu_to_be32(dev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001034 r.device_to = cpu_to_be32(bio_dev(bio));
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001035 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001036
Kent Overstreet4f024f32013-10-11 15:44:27 -07001037 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
Chaitanya Kulkarni48bc3cd2020-06-04 00:13:28 -07001038 bio_op(bio), bio->bi_opf, BLK_TA_REMAP,
1039 blk_status_to_errno(bio->bi_status),
Shaohua Lica1136c2017-07-12 11:49:53 -07001040 sizeof(r), &r, blk_trace_bio_get_cgid(q, bio));
Jan Karac780e862020-02-06 15:28:12 +01001041 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001042}
1043
1044/**
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001045 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -07001046 * @ignore: trace callback data parameter (not used)
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001047 * @q: queue the io is for
1048 * @rq: the source request
1049 * @dev: target device
1050 * @from: source sector
1051 *
1052 * Description:
1053 * Device mapper remaps request to other devices.
1054 * Add a trace for that action.
1055 *
1056 **/
Steven Rostedt38516ab2010-04-20 17:04:50 -04001057static void blk_add_trace_rq_remap(void *ignore,
1058 struct request_queue *q,
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001059 struct request *rq, dev_t dev,
1060 sector_t from)
1061{
Jan Karac780e862020-02-06 15:28:12 +01001062 struct blk_trace *bt;
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001063 struct blk_io_trace_remap r;
1064
Jan Karac780e862020-02-06 15:28:12 +01001065 rcu_read_lock();
1066 bt = rcu_dereference(q->blk_trace);
1067 if (likely(!bt)) {
1068 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001069 return;
Jan Karac780e862020-02-06 15:28:12 +01001070 }
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001071
1072 r.device_from = cpu_to_be32(dev);
1073 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
1074 r.sector_from = cpu_to_be64(from);
1075
1076 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
Christoph Hellwigcaf7df12017-04-20 16:03:16 +02001077 rq_data_dir(rq), 0, BLK_TA_REMAP, 0,
Shaohua Lica1136c2017-07-12 11:49:53 -07001078 sizeof(r), &r, blk_trace_request_get_cgid(q, rq));
Jan Karac780e862020-02-06 15:28:12 +01001079 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001080}
1081
1082/**
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001083 * blk_add_driver_data - Add binary message with driver-specific data
1084 * @q: queue the io is for
1085 * @rq: io request
1086 * @data: driver-specific data
1087 * @len: length of driver-specific data
1088 *
1089 * Description:
1090 * Some drivers might want to write driver-specific data per request.
1091 *
1092 **/
1093void blk_add_driver_data(struct request_queue *q,
1094 struct request *rq,
1095 void *data, size_t len)
1096{
Jan Karac780e862020-02-06 15:28:12 +01001097 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001098
Jan Karac780e862020-02-06 15:28:12 +01001099 rcu_read_lock();
1100 bt = rcu_dereference(q->blk_trace);
1101 if (likely(!bt)) {
1102 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001103 return;
Jan Karac780e862020-02-06 15:28:12 +01001104 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001105
Christoph Hellwig48b77ad2017-01-27 09:35:54 +01001106 __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0,
Shaohua Lica1136c2017-07-12 11:49:53 -07001107 BLK_TA_DRV_DATA, 0, len, data,
1108 blk_trace_request_get_cgid(q, rq));
Jan Karac780e862020-02-06 15:28:12 +01001109 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001110}
1111EXPORT_SYMBOL_GPL(blk_add_driver_data);
1112
Li Zefan3c289ba2009-03-20 09:48:26 +08001113static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001114{
1115 int ret;
1116
Steven Rostedt38516ab2010-04-20 17:04:50 -04001117 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001118 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001119 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001120 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001121 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001122 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001123 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001124 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001125 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001126 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001127 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001128 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001129 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001130 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001131 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001132 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001133 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001134 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001135 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001136 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001137 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001138 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001139 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001140 WARN_ON(ret);
Jens Axboe49cac012011-04-16 13:51:05 +02001141 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001142 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001143 ret = register_trace_block_split(blk_add_trace_split, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001144 WARN_ON(ret);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001145 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001146 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001147 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001148 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001149}
1150
1151static void blk_unregister_tracepoints(void)
1152{
Steven Rostedt38516ab2010-04-20 17:04:50 -04001153 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001154 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001155 unregister_trace_block_split(blk_add_trace_split, NULL);
Jens Axboe49cac012011-04-16 13:51:05 +02001156 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001157 unregister_trace_block_plug(blk_add_trace_plug, NULL);
1158 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1159 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1160 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1161 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1162 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1163 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1164 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1165 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1166 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1167 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1168 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001169
1170 tracepoint_synchronize_unregister();
1171}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001172
1173/*
1174 * struct blk_io_tracer formatting routines
1175 */
1176
1177static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1178{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001179 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +08001180 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001181
Shaohua Lica1136c2017-07-12 11:49:53 -07001182 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
Li Zefan18cea452009-03-27 10:21:54 +08001183 rwbs[i++] = 'N';
1184 goto out;
1185 }
1186
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001187 if (tc & BLK_TC_FLUSH)
1188 rwbs[i++] = 'F';
1189
Li Zefan65796342009-03-24 16:05:06 +08001190 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001191 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +08001192 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001193 rwbs[i++] = 'W';
1194 else if (t->bytes)
1195 rwbs[i++] = 'R';
1196 else
1197 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001198
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001199 if (tc & BLK_TC_FUA)
1200 rwbs[i++] = 'F';
Li Zefan65796342009-03-24 16:05:06 +08001201 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001202 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +08001203 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001204 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +08001205 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001206 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +08001207out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001208 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001209}
1210
1211static inline
1212const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1213{
1214 return (const struct blk_io_trace *)ent;
1215}
1216
Shaohua Lica1136c2017-07-12 11:49:53 -07001217static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001218{
Tejun Heo67c04962019-11-04 15:54:30 -08001219 return (void *)(te_blk_io_trace(ent) + 1) + (has_cg ? sizeof(u64) : 0);
Shaohua Lica1136c2017-07-12 11:49:53 -07001220}
1221
Tejun Heo67c04962019-11-04 15:54:30 -08001222static inline u64 t_cgid(const struct trace_entry *ent)
Shaohua Lica1136c2017-07-12 11:49:53 -07001223{
Tejun Heo67c04962019-11-04 15:54:30 -08001224 return *(u64 *)(te_blk_io_trace(ent) + 1);
Shaohua Lica1136c2017-07-12 11:49:53 -07001225}
1226
1227static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg)
1228{
Tejun Heo67c04962019-11-04 15:54:30 -08001229 return te_blk_io_trace(ent)->pdu_len - (has_cg ? sizeof(u64) : 0);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001230}
1231
Li Zefan66de7792009-04-01 16:19:19 +08001232static inline u32 t_action(const struct trace_entry *ent)
1233{
1234 return te_blk_io_trace(ent)->action;
1235}
1236
1237static inline u32 t_bytes(const struct trace_entry *ent)
1238{
1239 return te_blk_io_trace(ent)->bytes;
1240}
1241
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001242static inline u32 t_sec(const struct trace_entry *ent)
1243{
1244 return te_blk_io_trace(ent)->bytes >> 9;
1245}
1246
1247static inline unsigned long long t_sector(const struct trace_entry *ent)
1248{
1249 return te_blk_io_trace(ent)->sector;
1250}
1251
1252static inline __u16 t_error(const struct trace_entry *ent)
1253{
Li Zefane0dc81b2009-03-24 16:05:51 +08001254 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001255}
1256
Shaohua Lica1136c2017-07-12 11:49:53 -07001257static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001258{
Chaitanya Kulkarni71df3fd2020-06-04 00:13:29 -07001259 const __be64 *val = pdu_start(ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001260 return be64_to_cpu(*val);
1261}
1262
1263static void get_pdu_remap(const struct trace_entry *ent,
Shaohua Lica1136c2017-07-12 11:49:53 -07001264 struct blk_io_trace_remap *r, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001265{
Shaohua Lica1136c2017-07-12 11:49:53 -07001266 const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001267 __u64 sector_from = __r->sector_from;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001268
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001269 r->device_from = be32_to_cpu(__r->device_from);
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001270 r->device_to = be32_to_cpu(__r->device_to);
1271 r->sector_from = be64_to_cpu(sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001272}
1273
Shaohua Lica1136c2017-07-12 11:49:53 -07001274typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act,
1275 bool has_cg);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001276
Shaohua Lica1136c2017-07-12 11:49:53 -07001277static void blk_log_action_classic(struct trace_iterator *iter, const char *act,
1278 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001279{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001280 char rwbs[RWBS_LEN];
Li Zefan35ac51b2009-03-27 10:19:46 +08001281 unsigned long long ts = iter->ts;
1282 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001283 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001284 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001285
1286 fill_rwbs(rwbs, t);
1287
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001288 trace_seq_printf(&iter->seq,
1289 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1290 MAJOR(t->device), MINOR(t->device), iter->cpu,
1291 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001292}
1293
Shaohua Lica1136c2017-07-12 11:49:53 -07001294static void blk_log_action(struct trace_iterator *iter, const char *act,
1295 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001296{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001297 char rwbs[RWBS_LEN];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001298 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1299
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001300 fill_rwbs(rwbs, t);
Shaohua Lica1136c2017-07-12 11:49:53 -07001301 if (has_cg) {
Tejun Heo67c04962019-11-04 15:54:30 -08001302 u64 id = t_cgid(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001303
Shaohua Li69fd5c32017-07-12 11:49:55 -07001304 if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) {
1305 char blkcg_name_buf[NAME_MAX + 1] = "<...>";
1306
1307 cgroup_path_from_kernfs_id(id, blkcg_name_buf,
1308 sizeof(blkcg_name_buf));
1309 trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ",
1310 MAJOR(t->device), MINOR(t->device),
1311 blkcg_name_buf, act, rwbs);
Tejun Heo40430452019-11-04 15:54:30 -08001312 } else {
1313 /*
1314 * The cgid portion used to be "INO,GEN". Userland
1315 * builds a FILEID_INO32_GEN fid out of them and
1316 * opens the cgroup using open_by_handle_at(2).
1317 * While 32bit ino setups are still the same, 64bit
1318 * ones now use the 64bit ino as the whole ID and
1319 * no longer use generation.
1320 *
1321 * Regarldess of the content, always output
1322 * "LOW32,HIGH32" so that FILEID_INO32_GEN fid can
1323 * be mapped back to @id on both 64 and 32bit ino
1324 * setups. See __kernfs_fh_to_dentry().
1325 */
Shaohua Li69fd5c32017-07-12 11:49:55 -07001326 trace_seq_printf(&iter->seq,
Tejun Heo40430452019-11-04 15:54:30 -08001327 "%3d,%-3d %llx,%-llx %2s %3s ",
Shaohua Lica1136c2017-07-12 11:49:53 -07001328 MAJOR(t->device), MINOR(t->device),
Tejun Heo40430452019-11-04 15:54:30 -08001329 id & U32_MAX, id >> 32, act, rwbs);
1330 }
Shaohua Lica1136c2017-07-12 11:49:53 -07001331 } else
1332 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1333 MAJOR(t->device), MINOR(t->device), act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001334}
1335
Shaohua Lica1136c2017-07-12 11:49:53 -07001336static void blk_log_dump_pdu(struct trace_seq *s,
1337 const struct trace_entry *ent, bool has_cg)
Li Zefan66de7792009-04-01 16:19:19 +08001338{
Li Zefan049862572009-05-11 14:33:23 +08001339 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001340 int pdu_len;
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001341 int i, end;
Li Zefan66de7792009-04-01 16:19:19 +08001342
Shaohua Lica1136c2017-07-12 11:49:53 -07001343 pdu_buf = pdu_start(ent, has_cg);
1344 pdu_len = pdu_real_len(ent, has_cg);
Li Zefan66de7792009-04-01 16:19:19 +08001345
1346 if (!pdu_len)
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001347 return;
Li Zefan66de7792009-04-01 16:19:19 +08001348
1349 /* find the last zero that needs to be printed */
1350 for (end = pdu_len - 1; end >= 0; end--)
1351 if (pdu_buf[end])
1352 break;
1353 end++;
1354
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001355 trace_seq_putc(s, '(');
Li Zefan66de7792009-04-01 16:19:19 +08001356
1357 for (i = 0; i < pdu_len; i++) {
1358
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001359 trace_seq_printf(s, "%s%02x",
1360 i == 0 ? "" : " ", pdu_buf[i]);
Li Zefan66de7792009-04-01 16:19:19 +08001361
1362 /*
1363 * stop when the rest is just zeroes and indicate so
1364 * with a ".." appended
1365 */
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001366 if (i == end && end != pdu_len - 1) {
1367 trace_seq_puts(s, " ..) ");
1368 return;
1369 }
Li Zefan66de7792009-04-01 16:19:19 +08001370 }
1371
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001372 trace_seq_puts(s, ") ");
Li Zefan66de7792009-04-01 16:19:19 +08001373}
1374
Shaohua Lica1136c2017-07-12 11:49:53 -07001375static 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 -02001376{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001377 char cmd[TASK_COMM_LEN];
1378
1379 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001380
Li Zefan66de7792009-04-01 16:19:19 +08001381 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001382 trace_seq_printf(s, "%u ", t_bytes(ent));
Shaohua Lica1136c2017-07-12 11:49:53 -07001383 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001384 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001385 } else {
1386 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001387 trace_seq_printf(s, "%llu + %u [%s]\n",
Li Zefan66de7792009-04-01 16:19:19 +08001388 t_sector(ent), t_sec(ent), cmd);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001389 else
1390 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001391 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001392}
1393
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001394static void blk_log_with_error(struct trace_seq *s,
Shaohua Lica1136c2017-07-12 11:49:53 -07001395 const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001396{
Li Zefan66de7792009-04-01 16:19:19 +08001397 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Shaohua Lica1136c2017-07-12 11:49:53 -07001398 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001399 trace_seq_printf(s, "[%d]\n", t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001400 } else {
1401 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001402 trace_seq_printf(s, "%llu + %u [%d]\n",
1403 t_sector(ent),
1404 t_sec(ent), t_error(ent));
1405 else
1406 trace_seq_printf(s, "%llu [%d]\n",
1407 t_sector(ent), t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001408 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001409}
1410
Shaohua Lica1136c2017-07-12 11:49:53 -07001411static 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 -02001412{
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001413 struct blk_io_trace_remap r = { .device_from = 0, };
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001414
Shaohua Lica1136c2017-07-12 11:49:53 -07001415 get_pdu_remap(ent, &r, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001416 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1417 t_sector(ent), t_sec(ent),
1418 MAJOR(r.device_from), MINOR(r.device_from),
1419 (unsigned long long)r.sector_from);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001420}
1421
Shaohua Lica1136c2017-07-12 11:49:53 -07001422static 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 -02001423{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001424 char cmd[TASK_COMM_LEN];
1425
1426 trace_find_cmdline(ent->pid, cmd);
1427
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001428 trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001429}
1430
Shaohua Lica1136c2017-07-12 11:49:53 -07001431static 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 -02001432{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001433 char cmd[TASK_COMM_LEN];
1434
1435 trace_find_cmdline(ent->pid, cmd);
1436
Shaohua Lica1136c2017-07-12 11:49:53 -07001437 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001438}
1439
Shaohua Lica1136c2017-07-12 11:49:53 -07001440static 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 -02001441{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001442 char cmd[TASK_COMM_LEN];
1443
1444 trace_find_cmdline(ent->pid, cmd);
1445
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001446 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Shaohua Lica1136c2017-07-12 11:49:53 -07001447 get_pdu_int(ent, has_cg), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001448}
1449
Shaohua Lica1136c2017-07-12 11:49:53 -07001450static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent,
1451 bool has_cg)
Li Zefan18cea452009-03-27 10:21:54 +08001452{
Li Zefan18cea452009-03-27 10:21:54 +08001453
Shaohua Lica1136c2017-07-12 11:49:53 -07001454 trace_seq_putmem(s, pdu_start(ent, has_cg),
1455 pdu_real_len(ent, has_cg));
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001456 trace_seq_putc(s, '\n');
Li Zefan18cea452009-03-27 10:21:54 +08001457}
1458
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001459/*
1460 * struct tracer operations
1461 */
1462
1463static void blk_tracer_print_header(struct seq_file *m)
1464{
1465 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1466 return;
1467 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1468 "# | | | | | |\n");
1469}
1470
1471static void blk_tracer_start(struct trace_array *tr)
1472{
Li Zefanad5dd542009-03-27 10:20:24 +08001473 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001474}
1475
1476static int blk_tracer_init(struct trace_array *tr)
1477{
1478 blk_tr = tr;
1479 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001480 return 0;
1481}
1482
1483static void blk_tracer_stop(struct trace_array *tr)
1484{
Li Zefanad5dd542009-03-27 10:20:24 +08001485 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001486}
1487
1488static void blk_tracer_reset(struct trace_array *tr)
1489{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001490 blk_tracer_stop(tr);
1491}
1492
Li Zefane4955c92009-03-24 16:04:37 +08001493static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001494 const char *act[2];
Shaohua Lica1136c2017-07-12 11:49:53 -07001495 void (*print)(struct trace_seq *s, const struct trace_entry *ent,
1496 bool has_cg);
Li Zefane4955c92009-03-24 16:04:37 +08001497} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001498 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001499 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1500 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1501 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1502 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1503 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1504 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1505 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1506 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1507 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
Jens Axboe49cac012011-04-16 13:51:05 +02001508 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001509 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1510 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1511 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1512 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1513};
1514
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001515static enum print_line_t print_one_line(struct trace_iterator *iter,
1516 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001517{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001518 struct trace_array *tr = iter->tr;
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001519 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001520 const struct blk_io_trace *t;
1521 u16 what;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001522 bool long_act;
1523 blk_log_action_t *log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001524 bool has_cg;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001525
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001526 t = te_blk_io_trace(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001527 what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001528 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001529 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001530 has_cg = t->action & __BLK_TA_CGROUP;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001531
Shaohua Lica1136c2017-07-12 11:49:53 -07001532 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
1533 log_action(iter, long_act ? "message" : "m", has_cg);
1534 blk_log_msg(s, iter->ent, has_cg);
Shaohua Lib7d76412016-05-09 17:22:13 -07001535 return trace_handle_return(s);
Li Zefan18cea452009-03-27 10:21:54 +08001536 }
1537
Li Zefaneb08f8e2009-03-24 16:05:27 +08001538 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001539 trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001540 else {
Shaohua Lica1136c2017-07-12 11:49:53 -07001541 log_action(iter, what2act[what].act[long_act], has_cg);
1542 what2act[what].print(s, iter->ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001543 }
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001544
1545 return trace_handle_return(s);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001546}
1547
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001548static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
Steven Rostedta9a57762010-04-22 18:46:14 -04001549 int flags, struct trace_event *event)
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001550{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001551 return print_one_line(iter, false);
1552}
1553
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001554static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001555{
1556 struct trace_seq *s = &iter->seq;
1557 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1558 const int offset = offsetof(struct blk_io_trace, sector);
1559 struct blk_io_trace old = {
1560 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001561 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001562 };
1563
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001564 trace_seq_putmem(s, &old, offset);
1565 trace_seq_putmem(s, &t->sector,
1566 sizeof(old) - offset + t->pdu_len);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001567}
1568
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001569static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001570blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1571 struct trace_event *event)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001572{
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001573 blk_trace_synthesize_old_trace(iter);
1574
1575 return trace_handle_return(&iter->seq);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001576}
1577
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001578static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1579{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001580 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1581 return TRACE_TYPE_UNHANDLED;
1582
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001583 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001584}
1585
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05001586static int
1587blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Li Zefanf3948f82009-04-15 11:02:56 +08001588{
1589 /* don't output context-info for blk_classic output */
1590 if (bit == TRACE_BLK_OPT_CLASSIC) {
1591 if (set)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001592 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001593 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001594 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001595 }
1596 return 0;
1597}
1598
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001599static struct tracer blk_tracer __read_mostly = {
1600 .name = "blk",
1601 .init = blk_tracer_init,
1602 .reset = blk_tracer_reset,
1603 .start = blk_tracer_start,
1604 .stop = blk_tracer_stop,
1605 .print_header = blk_tracer_print_header,
1606 .print_line = blk_tracer_print_line,
1607 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001608 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001609};
1610
Steven Rostedta9a57762010-04-22 18:46:14 -04001611static struct trace_event_functions trace_blk_event_funcs = {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001612 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001613 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001614};
1615
Steven Rostedta9a57762010-04-22 18:46:14 -04001616static struct trace_event trace_blk_event = {
1617 .type = TRACE_BLK,
1618 .funcs = &trace_blk_event_funcs,
1619};
1620
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001621static int __init init_blk_tracer(void)
1622{
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001623 if (!register_trace_event(&trace_blk_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001624 pr_warn("Warning: could not register block events\n");
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001625 return 1;
1626 }
1627
1628 if (register_tracer(&blk_tracer) != 0) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001629 pr_warn("Warning: could not register the block tracer\n");
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001630 unregister_trace_event(&trace_blk_event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001631 return 1;
1632 }
1633
1634 return 0;
1635}
1636
1637device_initcall(init_blk_tracer);
1638
1639static int blk_trace_remove_queue(struct request_queue *q)
1640{
1641 struct blk_trace *bt;
1642
1643 bt = xchg(&q->blk_trace, NULL);
1644 if (bt == NULL)
1645 return -EINVAL;
1646
Jens Axboea6da0022017-11-05 09:16:09 -07001647 put_probe_ref();
Jan Karac780e862020-02-06 15:28:12 +01001648 synchronize_rcu();
Li Zefanad5dd542009-03-27 10:20:24 +08001649 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001650 return 0;
1651}
1652
1653/*
1654 * Setup everything required to start tracing
1655 */
Li Zefan9908c302009-04-14 13:59:34 +08001656static int blk_trace_setup_queue(struct request_queue *q,
1657 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001658{
Davidlohr Buesocdea01b2015-10-30 05:25:59 +09001659 struct blk_trace *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001660 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001661
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001662 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1663 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001664 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001665
Li Zefan18cea452009-03-27 10:21:54 +08001666 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1667 if (!bt->msg_data)
1668 goto free_bt;
1669
Li Zefan9908c302009-04-14 13:59:34 +08001670 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001671 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001672
1673 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001674
Davidlohr Buesocdea01b2015-10-30 05:25:59 +09001675 ret = -EBUSY;
1676 if (cmpxchg(&q->blk_trace, NULL, bt))
Li Zefan18cea452009-03-27 10:21:54 +08001677 goto free_bt;
Li Zefan15152e42009-03-20 09:49:08 +08001678
Jens Axboea6da0022017-11-05 09:16:09 -07001679 get_probe_ref();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001680 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001681
1682free_bt:
1683 blk_trace_free(bt);
1684 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001685}
1686
1687/*
1688 * sysfs interface to enable and configure tracing
1689 */
1690
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001691static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1692 struct device_attribute *attr,
1693 char *buf);
1694static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1695 struct device_attribute *attr,
1696 const char *buf, size_t count);
1697#define BLK_TRACE_DEVICE_ATTR(_name) \
1698 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1699 sysfs_blk_trace_attr_show, \
1700 sysfs_blk_trace_attr_store)
1701
Li Zefancd649b82009-03-20 11:33:55 +08001702static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001703static BLK_TRACE_DEVICE_ATTR(act_mask);
1704static BLK_TRACE_DEVICE_ATTR(pid);
1705static BLK_TRACE_DEVICE_ATTR(start_lba);
1706static BLK_TRACE_DEVICE_ATTR(end_lba);
1707
1708static struct attribute *blk_trace_attrs[] = {
1709 &dev_attr_enable.attr,
1710 &dev_attr_act_mask.attr,
1711 &dev_attr_pid.attr,
1712 &dev_attr_start_lba.attr,
1713 &dev_attr_end_lba.attr,
1714 NULL
1715};
1716
1717struct attribute_group blk_trace_attr_group = {
1718 .name = "trace",
1719 .attrs = blk_trace_attrs,
1720};
1721
Li Zefan09341992009-03-24 17:43:30 +08001722static const struct {
1723 int mask;
1724 const char *str;
1725} mask_maps[] = {
1726 { BLK_TC_READ, "read" },
1727 { BLK_TC_WRITE, "write" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001728 { BLK_TC_FLUSH, "flush" },
Li Zefan09341992009-03-24 17:43:30 +08001729 { BLK_TC_SYNC, "sync" },
1730 { BLK_TC_QUEUE, "queue" },
1731 { BLK_TC_REQUEUE, "requeue" },
1732 { BLK_TC_ISSUE, "issue" },
1733 { BLK_TC_COMPLETE, "complete" },
1734 { BLK_TC_FS, "fs" },
1735 { BLK_TC_PC, "pc" },
Shaohua Li8d1547e2016-05-09 17:22:14 -07001736 { BLK_TC_NOTIFY, "notify" },
Li Zefan09341992009-03-24 17:43:30 +08001737 { BLK_TC_AHEAD, "ahead" },
1738 { BLK_TC_META, "meta" },
1739 { BLK_TC_DISCARD, "discard" },
1740 { BLK_TC_DRV_DATA, "drv_data" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001741 { BLK_TC_FUA, "fua" },
Li Zefan09341992009-03-24 17:43:30 +08001742};
1743
1744static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001745{
Li Zefan09341992009-03-24 17:43:30 +08001746 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001747 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001748 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001749
Li Zefan9eb85122009-04-09 11:19:40 +08001750 buf = kstrdup(str, GFP_KERNEL);
1751 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001752 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001753 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001754
1755 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001756 token = strsep(&s, ",");
1757 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001758 break;
1759
Li Zefan09341992009-03-24 17:43:30 +08001760 if (*token == '\0')
1761 continue;
1762
1763 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1764 if (strcasecmp(token, mask_maps[i].str) == 0) {
1765 mask |= mask_maps[i].mask;
1766 break;
1767 }
1768 }
1769 if (i == ARRAY_SIZE(mask_maps)) {
1770 mask = -EINVAL;
1771 break;
1772 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001773 }
Li Zefan9eb85122009-04-09 11:19:40 +08001774 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001775
1776 return mask;
1777}
1778
Li Zefan09341992009-03-24 17:43:30 +08001779static ssize_t blk_trace_mask2str(char *buf, int mask)
1780{
1781 int i;
1782 char *p = buf;
1783
1784 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1785 if (mask & mask_maps[i].mask) {
1786 p += sprintf(p, "%s%s",
1787 (p == buf) ? "" : ",", mask_maps[i].str);
1788 }
1789 }
1790 *p++ = '\n';
1791
1792 return p - buf;
1793}
1794
Li Zefanb1251302009-03-20 10:34:00 +08001795static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1796{
1797 if (bdev->bd_disk == NULL)
1798 return NULL;
1799
1800 return bdev_get_queue(bdev);
1801}
1802
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001803static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1804 struct device_attribute *attr,
1805 char *buf)
1806{
1807 struct hd_struct *p = dev_to_part(dev);
1808 struct request_queue *q;
1809 struct block_device *bdev;
Jan Karac780e862020-02-06 15:28:12 +01001810 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001811 ssize_t ret = -ENXIO;
1812
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001813 bdev = bdget(part_devt(p));
1814 if (bdev == NULL)
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001815 goto out;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001816
Li Zefanb1251302009-03-20 10:34:00 +08001817 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001818 if (q == NULL)
1819 goto out_bdput;
Li Zefanb1251302009-03-20 10:34:00 +08001820
Waiman Long5acb3cc2017-09-20 13:12:20 -06001821 mutex_lock(&q->blk_trace_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001822
Jan Karac780e862020-02-06 15:28:12 +01001823 bt = rcu_dereference_protected(q->blk_trace,
1824 lockdep_is_held(&q->blk_trace_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001825 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001826 ret = sprintf(buf, "%u\n", !!bt);
Li Zefancd649b82009-03-20 11:33:55 +08001827 goto out_unlock_bdev;
1828 }
1829
Jan Karac780e862020-02-06 15:28:12 +01001830 if (bt == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001831 ret = sprintf(buf, "disabled\n");
1832 else if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001833 ret = blk_trace_mask2str(buf, bt->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001834 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001835 ret = sprintf(buf, "%u\n", bt->pid);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001836 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001837 ret = sprintf(buf, "%llu\n", bt->start_lba);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001838 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001839 ret = sprintf(buf, "%llu\n", bt->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001840
1841out_unlock_bdev:
Waiman Long5acb3cc2017-09-20 13:12:20 -06001842 mutex_unlock(&q->blk_trace_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001843out_bdput:
1844 bdput(bdev);
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001845out:
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001846 return ret;
1847}
1848
1849static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1850 struct device_attribute *attr,
1851 const char *buf, size_t count)
1852{
1853 struct block_device *bdev;
1854 struct request_queue *q;
1855 struct hd_struct *p;
Jan Karac780e862020-02-06 15:28:12 +01001856 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001857 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001858 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001859
1860 if (count == 0)
1861 goto out;
1862
1863 if (attr == &dev_attr_act_mask) {
Shaohua Li5f339452017-05-19 08:04:59 -07001864 if (kstrtoull(buf, 0, &value)) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001865 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001866 ret = blk_trace_str2mask(buf);
1867 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001868 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001869 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001870 }
Shaohua Li5f339452017-05-19 08:04:59 -07001871 } else if (kstrtoull(buf, 0, &value))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001872 goto out;
1873
Li Zefan09341992009-03-24 17:43:30 +08001874 ret = -ENXIO;
1875
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001876 p = dev_to_part(dev);
1877 bdev = bdget(part_devt(p));
1878 if (bdev == NULL)
Arnd Bergmann01b284f2010-09-17 20:39:22 +02001879 goto out;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001880
Li Zefanb1251302009-03-20 10:34:00 +08001881 q = blk_trace_get_queue(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001882 if (q == NULL)
1883 goto out_bdput;
1884
Waiman Long5acb3cc2017-09-20 13:12:20 -06001885 mutex_lock(&q->blk_trace_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001886
Jan Karac780e862020-02-06 15:28:12 +01001887 bt = rcu_dereference_protected(q->blk_trace,
1888 lockdep_is_held(&q->blk_trace_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001889 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001890 if (!!value == !!bt) {
Steven Rostedt (VMware)757d9142018-08-16 16:08:37 -04001891 ret = 0;
1892 goto out_unlock_bdev;
1893 }
Li Zefancd649b82009-03-20 11:33:55 +08001894 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001895 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001896 else
1897 ret = blk_trace_remove_queue(q);
1898 goto out_unlock_bdev;
1899 }
1900
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001901 ret = 0;
Cengiz Can153031a2020-03-04 13:58:19 +03001902 if (bt == NULL) {
Li Zefan9908c302009-04-14 13:59:34 +08001903 ret = blk_trace_setup_queue(q, bdev);
Cengiz Can153031a2020-03-04 13:58:19 +03001904 bt = rcu_dereference_protected(q->blk_trace,
1905 lockdep_is_held(&q->blk_trace_mutex));
1906 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001907
1908 if (ret == 0) {
1909 if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001910 bt->act_mask = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001911 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001912 bt->pid = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001913 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001914 bt->start_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001915 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001916 bt->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001917 }
Li Zefancd649b82009-03-20 11:33:55 +08001918
1919out_unlock_bdev:
Waiman Long5acb3cc2017-09-20 13:12:20 -06001920 mutex_unlock(&q->blk_trace_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001921out_bdput:
1922 bdput(bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001923out:
Li Zefancd649b82009-03-20 11:33:55 +08001924 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001925}
Li Zefancd649b82009-03-20 11:33:55 +08001926
Li Zefan1d54ad62009-04-14 14:00:05 +08001927int blk_trace_init_sysfs(struct device *dev)
1928{
1929 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1930}
1931
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +02001932void blk_trace_remove_sysfs(struct device *dev)
1933{
1934 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1935}
1936
Li Zefan55782132009-06-09 13:43:05 +08001937#endif /* CONFIG_BLK_DEV_IO_TRACE */
1938
1939#ifdef CONFIG_EVENT_TRACING
1940
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001941void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes)
Li Zefan55782132009-06-09 13:43:05 +08001942{
1943 int i = 0;
1944
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001945 if (op & REQ_PREFLUSH)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001946 rwbs[i++] = 'F';
1947
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001948 switch (op & REQ_OP_MASK) {
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001949 case REQ_OP_WRITE:
1950 case REQ_OP_WRITE_SAME:
Li Zefan55782132009-06-09 13:43:05 +08001951 rwbs[i++] = 'W';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001952 break;
1953 case REQ_OP_DISCARD:
Li Zefan55782132009-06-09 13:43:05 +08001954 rwbs[i++] = 'D';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001955 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001956 case REQ_OP_SECURE_ERASE:
1957 rwbs[i++] = 'D';
1958 rwbs[i++] = 'E';
1959 break;
Mike Christie3a5e02c2016-06-05 14:32:23 -05001960 case REQ_OP_FLUSH:
1961 rwbs[i++] = 'F';
1962 break;
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001963 case REQ_OP_READ:
Li Zefan55782132009-06-09 13:43:05 +08001964 rwbs[i++] = 'R';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001965 break;
1966 default:
Li Zefan55782132009-06-09 13:43:05 +08001967 rwbs[i++] = 'N';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001968 }
Li Zefan55782132009-06-09 13:43:05 +08001969
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001970 if (op & REQ_FUA)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001971 rwbs[i++] = 'F';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001972 if (op & REQ_RAHEAD)
Li Zefan55782132009-06-09 13:43:05 +08001973 rwbs[i++] = 'A';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001974 if (op & REQ_SYNC)
Li Zefan55782132009-06-09 13:43:05 +08001975 rwbs[i++] = 'S';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001976 if (op & REQ_META)
Li Zefan55782132009-06-09 13:43:05 +08001977 rwbs[i++] = 'M';
1978
1979 rwbs[i] = '\0';
1980}
Kent Overstreet9ca8f8e2012-04-13 21:56:43 -04001981EXPORT_SYMBOL_GPL(blk_fill_rwbs);
Li Zefan55782132009-06-09 13:43:05 +08001982
Li Zefan55782132009-06-09 13:43:05 +08001983#endif /* CONFIG_EVENT_TRACING */
1984