blob: ca6f0ceba09b762703aa87a7347b80c9cf6356e7 [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 */
Luis Chamberlain1b0b2832020-06-05 16:58:36 +02006
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Jens Axboe2056a782006-03-23 20:00:26 +01009#include <linux/kernel.h>
10#include <linux/blkdev.h>
11#include <linux/blktrace_api.h>
12#include <linux/percpu.h>
13#include <linux/init.h>
14#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Jens Axboe2056a782006-03-23 20:00:26 +010016#include <linux/debugfs.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040017#include <linux/export.h>
Olaf Kirchbe1c6342006-12-01 10:39:12 +010018#include <linux/time.h>
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -020019#include <linux/uaccess.h>
Jan Karaa404d552013-09-17 22:30:31 +020020#include <linux/list.h>
Shaohua Lica1136c2017-07-12 11:49:53 -070021#include <linux/blk-cgroup.h>
Li Zefan55782132009-06-09 13:43:05 +080022
Omar Sandoval18fbda92017-01-31 14:53:20 -080023#include "../../block/blk.h"
24
Li Zefan55782132009-06-09 13:43:05 +080025#include <trace/events/block.h>
26
Frederic Weisbecker2db270a2009-02-07 20:46:45 +010027#include "trace_output.h"
Jens Axboe2056a782006-03-23 20:00:26 +010028
Li Zefan55782132009-06-09 13:43:05 +080029#ifdef CONFIG_BLK_DEV_IO_TRACE
30
Jens Axboe2056a782006-03-23 20:00:26 +010031static unsigned int blktrace_seq __read_mostly = 1;
32
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020033static struct trace_array *blk_tr;
Li Zefan5006ea72009-03-20 09:48:03 +080034static bool blk_tracer_enabled __read_mostly;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020035
Jan Karaa404d552013-09-17 22:30:31 +020036static LIST_HEAD(running_trace_list);
37static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
38
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020039/* Select an alternative, minimalistic output than the original one */
Steven Rostedtef180122009-03-10 14:10:56 -040040#define TRACE_BLK_OPT_CLASSIC 0x1
Shaohua Lica1136c2017-07-12 11:49:53 -070041#define TRACE_BLK_OPT_CGROUP 0x2
Shaohua Li69fd5c32017-07-12 11:49:55 -070042#define TRACE_BLK_OPT_CGNAME 0x4
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020043
44static struct tracer_opt blk_tracer_opts[] = {
45 /* Default disable the minimalistic output */
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -020046 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
Shaohua Lica1136c2017-07-12 11:49:53 -070047#ifdef CONFIG_BLK_CGROUP
48 { TRACER_OPT(blk_cgroup, TRACE_BLK_OPT_CGROUP) },
Shaohua Li69fd5c32017-07-12 11:49:55 -070049 { TRACER_OPT(blk_cgname, TRACE_BLK_OPT_CGNAME) },
Shaohua Lica1136c2017-07-12 11:49:53 -070050#endif
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020051 { }
52};
53
54static struct tracer_flags blk_tracer_flags = {
55 .val = 0,
56 .opts = blk_tracer_opts,
57};
58
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010059/* Global reference count of probes */
Jens Axboea6da0022017-11-05 09:16:09 -070060static DEFINE_MUTEX(blk_probe_mutex);
61static int blk_probes_ref;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010062
Li Zefan3c289ba2009-03-20 09:48:26 +080063static void blk_register_tracepoints(void);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +010064static void blk_unregister_tracepoints(void);
65
Jens Axboe2056a782006-03-23 20:00:26 +010066/*
Olaf Kirchbe1c6342006-12-01 10:39:12 +010067 * Send out a notify message.
68 */
Jens Axboea8630552006-12-04 09:30:58 +010069static void trace_note(struct blk_trace *bt, pid_t pid, int action,
Tejun Heo67c04962019-11-04 15:54:30 -080070 const void *data, size_t len, u64 cgid)
Olaf Kirchbe1c6342006-12-01 10:39:12 +010071{
72 struct blk_io_trace *t;
Li Zefan18cea452009-03-27 10:21:54 +080073 struct ring_buffer_event *event = NULL;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -050074 struct trace_buffer *buffer = NULL;
Li Zefan18cea452009-03-27 10:21:54 +080075 int pc = 0;
76 int cpu = smp_processor_id();
77 bool blk_tracer = blk_tracer_enabled;
Tejun Heo67c04962019-11-04 15:54:30 -080078 ssize_t cgid_len = cgid ? sizeof(cgid) : 0;
Li Zefan18cea452009-03-27 10:21:54 +080079
80 if (blk_tracer) {
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -050081 buffer = blk_tr->array_buffer.buffer;
Li Zefan18cea452009-03-27 10:21:54 +080082 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -040083 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Shaohua Lica1136c2017-07-12 11:49:53 -070084 sizeof(*t) + len + cgid_len,
Li Zefan18cea452009-03-27 10:21:54 +080085 0, pc);
86 if (!event)
87 return;
88 t = ring_buffer_event_data(event);
89 goto record_it;
90 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +010091
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -020092 if (!bt->rchan)
93 return;
94
Shaohua Lica1136c2017-07-12 11:49:53 -070095 t = relay_reserve(bt->rchan, sizeof(*t) + len + cgid_len);
Jens Axboed3d9d2a2006-12-04 09:27:41 +010096 if (t) {
Jens Axboed3d9d2a2006-12-04 09:27:41 +010097 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
Ingo Molnar2997c8c2008-01-11 13:35:54 +010098 t->time = ktime_to_ns(ktime_get());
Li Zefan18cea452009-03-27 10:21:54 +080099record_it:
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100100 t->device = bt->dev;
Shaohua Lica1136c2017-07-12 11:49:53 -0700101 t->action = action | (cgid ? __BLK_TN_CGROUP : 0);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100102 t->pid = pid;
103 t->cpu = cpu;
Shaohua Lica1136c2017-07-12 11:49:53 -0700104 t->pdu_len = len + cgid_len;
Tejun Heo67c04962019-11-04 15:54:30 -0800105 if (cgid_len)
106 memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
Shaohua Lica1136c2017-07-12 11:49:53 -0700107 memcpy((void *) t + sizeof(*t) + cgid_len, data, len);
Li Zefan18cea452009-03-27 10:21:54 +0800108
109 if (blk_tracer)
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -0400110 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
Jens Axboed3d9d2a2006-12-04 09:27:41 +0100111 }
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100112}
113
114/*
Jens Axboe2056a782006-03-23 20:00:26 +0100115 * Send out a notify for this process, if we haven't done so since a trace
116 * started
117 */
Jan Karaa404d552013-09-17 22:30:31 +0200118static void trace_note_tsk(struct task_struct *tsk)
Jens Axboe2056a782006-03-23 20:00:26 +0100119{
Jan Karaa404d552013-09-17 22:30:31 +0200120 unsigned long flags;
121 struct blk_trace *bt;
122
Jens Axboea8630552006-12-04 09:30:58 +0100123 tsk->btrace_seq = blktrace_seq;
Jan Karaa404d552013-09-17 22:30:31 +0200124 spin_lock_irqsave(&running_trace_lock, flags);
125 list_for_each_entry(bt, &running_trace_list, running_list) {
126 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
Tejun Heo67c04962019-11-04 15:54:30 -0800127 sizeof(tsk->comm), 0);
Jan Karaa404d552013-09-17 22:30:31 +0200128 }
129 spin_unlock_irqrestore(&running_trace_lock, flags);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100130}
Jens Axboe2056a782006-03-23 20:00:26 +0100131
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100132static void trace_note_time(struct blk_trace *bt)
133{
Arnd Bergmann59a37f82016-06-17 16:58:26 +0200134 struct timespec64 now;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100135 unsigned long flags;
136 u32 words[2];
137
Arnd Bergmann59a37f82016-06-17 16:58:26 +0200138 /* need to check user space to see if this breaks in y2038 or y2106 */
139 ktime_get_real_ts64(&now);
140 words[0] = (u32)now.tv_sec;
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100141 words[1] = now.tv_nsec;
142
143 local_irq_save(flags);
Tejun Heo67c04962019-11-04 15:54:30 -0800144 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words), 0);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100145 local_irq_restore(flags);
Jens Axboe2056a782006-03-23 20:00:26 +0100146}
147
Shaohua Li35fe6d72017-07-12 11:49:56 -0700148void __trace_note_message(struct blk_trace *bt, struct blkcg *blkcg,
149 const char *fmt, ...)
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200150{
151 int n;
152 va_list args;
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200153 unsigned long flags;
Jens Axboe64565912008-05-28 14:45:33 +0200154 char *buf;
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200155
Li Zefan18cea452009-03-27 10:21:54 +0800156 if (unlikely(bt->trace_state != Blktrace_running &&
157 !blk_tracer_enabled))
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200158 return;
159
Tao Ma490da402011-01-19 10:51:44 +0800160 /*
161 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
162 * message to the trace.
163 */
164 if (!(bt->act_mask & BLK_TC_NOTIFY))
165 return;
166
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200167 local_irq_save(flags);
Shan Weid8a03492012-11-13 09:53:04 +0800168 buf = this_cpu_ptr(bt->msg_data);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200169 va_start(args, fmt);
Jens Axboe64565912008-05-28 14:45:33 +0200170 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200171 va_end(args);
172
Shaohua Li35fe6d72017-07-12 11:49:56 -0700173 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
174 blkcg = NULL;
175#ifdef CONFIG_BLK_CGROUP
Jan Kara870c1532020-05-13 18:02:23 +0200176 trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n,
Tejun Heo74321032019-11-04 15:54:30 -0800177 blkcg ? cgroup_id(blkcg->css.cgroup) : 1);
Shaohua Li35fe6d72017-07-12 11:49:56 -0700178#else
Jan Kara870c1532020-05-13 18:02:23 +0200179 trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n, 0);
Shaohua Li35fe6d72017-07-12 11:49:56 -0700180#endif
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200181 local_irq_restore(flags);
Alan D. Brunelle9d5f09a2008-05-27 14:54:41 +0200182}
183EXPORT_SYMBOL_GPL(__trace_note_message);
184
Jens Axboe2056a782006-03-23 20:00:26 +0100185static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
186 pid_t pid)
187{
188 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
189 return 1;
Shawn Dud0deef52009-04-14 13:58:56 +0800190 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
Jens Axboe2056a782006-03-23 20:00:26 +0100191 return 1;
192 if (bt->pid && pid != bt->pid)
193 return 1;
194
195 return 0;
196}
197
198/*
199 * Data direction bit lookup
200 */
Li Zefane4955c92009-03-24 16:04:37 +0800201static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
202 BLK_TC_ACT(BLK_TC_WRITE) };
Jens Axboe2056a782006-03-23 20:00:26 +0100203
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200204#define BLK_TC_RAHEAD BLK_TC_AHEAD
Mike Christie28a8f0d2016-06-05 14:32:25 -0500205#define BLK_TC_PREFLUSH BLK_TC_FLUSH
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200206
David Woodhouse35ba8f72008-08-10 12:33:00 +0100207/* The ilog2() calls fall out because they're constant */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200208#define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
209 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
Jens Axboe2056a782006-03-23 20:00:26 +0100210
211/*
212 * The worker for the various blk_add_trace*() types. Fills out a
213 * blk_io_trace structure and places it in a per-cpu subbuffer.
214 */
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100215static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500216 int op, int op_flags, u32 what, int error, int pdu_len,
Tejun Heo67c04962019-11-04 15:54:30 -0800217 void *pdu_data, u64 cgid)
Jens Axboe2056a782006-03-23 20:00:26 +0100218{
219 struct task_struct *tsk = current;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200220 struct ring_buffer_event *event = NULL;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500221 struct trace_buffer *buffer = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100222 struct blk_io_trace *t;
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200223 unsigned long flags = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100224 unsigned long *sequence;
225 pid_t pid;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200226 int cpu, pc = 0;
Li Zefan18cea452009-03-27 10:21:54 +0800227 bool blk_tracer = blk_tracer_enabled;
Tejun Heo67c04962019-11-04 15:54:30 -0800228 ssize_t cgid_len = cgid ? sizeof(cgid) : 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100229
Li Zefan18cea452009-03-27 10:21:54 +0800230 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
Jens Axboe2056a782006-03-23 20:00:26 +0100231 return;
232
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500233 what |= ddir_act[op_is_write(op) ? WRITE : READ];
234 what |= MASK_TC_BIT(op_flags, SYNC);
235 what |= MASK_TC_BIT(op_flags, RAHEAD);
236 what |= MASK_TC_BIT(op_flags, META);
Mike Christie28a8f0d2016-06-05 14:32:25 -0500237 what |= MASK_TC_BIT(op_flags, PREFLUSH);
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500238 what |= MASK_TC_BIT(op_flags, FUA);
Adrian Hunter7afafc82016-08-16 10:59:35 +0300239 if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)
Mike Christie1b9a9ab2016-06-05 14:32:18 -0500240 what |= BLK_TC_ACT(BLK_TC_DISCARD);
Mike Christie3a5e02c2016-06-05 14:32:23 -0500241 if (op == REQ_OP_FLUSH)
242 what |= BLK_TC_ACT(BLK_TC_FLUSH);
Shaohua Lica1136c2017-07-12 11:49:53 -0700243 if (cgid)
244 what |= __BLK_TA_CGROUP;
Jens Axboe2056a782006-03-23 20:00:26 +0100245
246 pid = tsk->pid;
Shawn Dud0deef52009-04-14 13:58:56 +0800247 if (act_log_check(bt, what, sector, pid))
Jens Axboe2056a782006-03-23 20:00:26 +0100248 return;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200249 cpu = raw_smp_processor_id();
250
Li Zefan18cea452009-03-27 10:21:54 +0800251 if (blk_tracer) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200252 tracing_record_cmdline(current);
253
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500254 buffer = blk_tr->array_buffer.buffer;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200255 pc = preempt_count();
Steven Rostedte77405a2009-09-02 14:17:06 -0400256 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
Shaohua Lica1136c2017-07-12 11:49:53 -0700257 sizeof(*t) + pdu_len + cgid_len,
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200258 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200259 if (!event)
260 return;
Arnaldo Carvalho de Melo51a763d2009-02-05 16:14:13 -0200261 t = ring_buffer_event_data(event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200262 goto record_it;
263 }
Jens Axboe2056a782006-03-23 20:00:26 +0100264
Jan Karaa404d552013-09-17 22:30:31 +0200265 if (unlikely(tsk->btrace_seq != blktrace_seq))
266 trace_note_tsk(tsk);
267
Jens Axboe2056a782006-03-23 20:00:26 +0100268 /*
269 * A word about the locking here - we disable interrupts to reserve
270 * some space in the relay per-cpu buffer, to prevent an irq
Carl Henrik Lunde14a73f52008-06-12 20:13:58 +0200271 * from coming in and stepping on our toes.
Jens Axboe2056a782006-03-23 20:00:26 +0100272 */
273 local_irq_save(flags);
Shaohua Lica1136c2017-07-12 11:49:53 -0700274 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len + cgid_len);
Jens Axboe2056a782006-03-23 20:00:26 +0100275 if (t) {
Jens Axboe2056a782006-03-23 20:00:26 +0100276 sequence = per_cpu_ptr(bt->sequence, cpu);
277
278 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
279 t->sequence = ++(*sequence);
Ingo Molnar2997c8c2008-01-11 13:35:54 +0100280 t->time = ktime_to_ns(ktime_get());
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200281record_it:
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200282 /*
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200283 * These two are not needed in ftrace as they are in the
284 * generic trace_entry, filled by tracing_generic_entry_update,
285 * but for the trace_event->bin() synthesizer benefit we do it
286 * here too.
287 */
288 t->cpu = cpu;
289 t->pid = pid;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -0200290
Jens Axboe2056a782006-03-23 20:00:26 +0100291 t->sector = sector;
292 t->bytes = bytes;
293 t->action = what;
Jens Axboe2056a782006-03-23 20:00:26 +0100294 t->device = bt->dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100295 t->error = error;
Shaohua Lica1136c2017-07-12 11:49:53 -0700296 t->pdu_len = pdu_len + cgid_len;
Jens Axboe2056a782006-03-23 20:00:26 +0100297
Shaohua Lica1136c2017-07-12 11:49:53 -0700298 if (cgid_len)
Tejun Heo67c04962019-11-04 15:54:30 -0800299 memcpy((void *)t + sizeof(*t), &cgid, cgid_len);
Jens Axboe2056a782006-03-23 20:00:26 +0100300 if (pdu_len)
Shaohua Lica1136c2017-07-12 11:49:53 -0700301 memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200302
Li Zefan18cea452009-03-27 10:21:54 +0800303 if (blk_tracer) {
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -0400304 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -0200305 return;
306 }
Jens Axboe2056a782006-03-23 20:00:26 +0100307 }
308
309 local_irq_restore(flags);
310}
311
Li Zefanad5dd542009-03-27 10:20:24 +0800312static void blk_trace_free(struct blk_trace *bt)
Jens Axboe2056a782006-03-23 20:00:26 +0100313{
Jens Axboef48fc4d2009-01-05 10:17:25 +0100314 relay_close(bt->rchan);
Alan D. Brunelle39cbb602009-08-07 12:01:08 -0400315 debugfs_remove(bt->dir);
Jens Axboe2056a782006-03-23 20:00:26 +0100316 free_percpu(bt->sequence);
Jens Axboe64565912008-05-28 14:45:33 +0200317 free_percpu(bt->msg_data);
Jens Axboe2056a782006-03-23 20:00:26 +0100318 kfree(bt);
Li Zefanad5dd542009-03-27 10:20:24 +0800319}
320
Jens Axboea6da0022017-11-05 09:16:09 -0700321static void get_probe_ref(void)
322{
323 mutex_lock(&blk_probe_mutex);
324 if (++blk_probes_ref == 1)
325 blk_register_tracepoints();
326 mutex_unlock(&blk_probe_mutex);
327}
328
329static void put_probe_ref(void)
330{
331 mutex_lock(&blk_probe_mutex);
332 if (!--blk_probes_ref)
333 blk_unregister_tracepoints();
334 mutex_unlock(&blk_probe_mutex);
335}
336
Li Zefanad5dd542009-03-27 10:20:24 +0800337static void blk_trace_cleanup(struct blk_trace *bt)
338{
Jan Karac780e862020-02-06 15:28:12 +0100339 synchronize_rcu();
Li Zefanad5dd542009-03-27 10:20:24 +0800340 blk_trace_free(bt);
Jens Axboea6da0022017-11-05 09:16:09 -0700341 put_probe_ref();
Jens Axboe2056a782006-03-23 20:00:26 +0100342}
343
Jens Axboe1f2cac12017-11-05 09:13:48 -0700344static int __blk_trace_remove(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100345{
346 struct blk_trace *bt;
347
Jan Karac3dbe542020-06-05 16:58:37 +0200348 bt = rcu_replace_pointer(q->blk_trace, NULL,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000349 lockdep_is_held(&q->debugfs_mutex));
Jens Axboe2056a782006-03-23 20:00:26 +0100350 if (!bt)
351 return -EINVAL;
352
Li Zefan55547202009-03-25 17:21:26 +0800353 if (bt->trace_state != Blktrace_running)
Jens Axboe2056a782006-03-23 20:00:26 +0100354 blk_trace_cleanup(bt);
355
356 return 0;
357}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700358
359int blk_trace_remove(struct request_queue *q)
360{
361 int ret;
362
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000363 mutex_lock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700364 ret = __blk_trace_remove(q);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000365 mutex_unlock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700366
367 return ret;
368}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100369EXPORT_SYMBOL_GPL(blk_trace_remove);
Jens Axboe2056a782006-03-23 20:00:26 +0100370
Jens Axboe2056a782006-03-23 20:00:26 +0100371static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
372 size_t count, loff_t *ppos)
373{
374 struct blk_trace *bt = filp->private_data;
375 char buf[16];
376
377 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
378
379 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
380}
381
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800382static const struct file_operations blk_dropped_fops = {
Jens Axboe2056a782006-03-23 20:00:26 +0100383 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700384 .open = simple_open,
Jens Axboe2056a782006-03-23 20:00:26 +0100385 .read = blk_dropped_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200386 .llseek = default_llseek,
Jens Axboe2056a782006-03-23 20:00:26 +0100387};
388
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200389static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
390 size_t count, loff_t *ppos)
391{
392 char *msg;
393 struct blk_trace *bt;
394
Li Zefan7635b032009-04-03 15:31:34 +0800395 if (count >= BLK_TN_MAX_MSG)
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200396 return -EINVAL;
397
Al Viro16e5c1f2015-12-24 00:06:05 -0500398 msg = memdup_user_nul(buffer, count);
399 if (IS_ERR(msg))
400 return PTR_ERR(msg);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200401
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200402 bt = filp->private_data;
Shaohua Li35fe6d72017-07-12 11:49:56 -0700403 __trace_note_message(bt, NULL, "%s", msg);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200404 kfree(msg);
405
406 return count;
407}
408
409static const struct file_operations blk_msg_fops = {
410 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -0700411 .open = simple_open,
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200412 .write = blk_msg_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200413 .llseek = noop_llseek,
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200414};
415
Jens Axboe2056a782006-03-23 20:00:26 +0100416/*
417 * Keep track of how many times we encountered a full subbuffer, to aid
418 * the user space app in telling how many lost events there were.
419 */
420static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
421 void *prev_subbuf, size_t prev_padding)
422{
423 struct blk_trace *bt;
424
425 if (!relay_buf_full(buf))
426 return 1;
427
428 bt = buf->chan->private_data;
429 atomic_inc(&bt->dropped);
430 return 0;
431}
432
433static int blk_remove_buf_file_callback(struct dentry *dentry)
434{
435 debugfs_remove(dentry);
Jens Axboef48fc4d2009-01-05 10:17:25 +0100436
Jens Axboe2056a782006-03-23 20:00:26 +0100437 return 0;
438}
439
440static struct dentry *blk_create_buf_file_callback(const char *filename,
441 struct dentry *parent,
Al Virof4ae40a62011-07-24 04:33:43 -0400442 umode_t mode,
Jens Axboe2056a782006-03-23 20:00:26 +0100443 struct rchan_buf *buf,
444 int *is_global)
445{
446 return debugfs_create_file(filename, mode, parent, buf,
447 &relay_file_operations);
448}
449
Jani Nikulaabf4e002020-12-15 20:46:13 -0800450static const struct rchan_callbacks blk_relay_callbacks = {
Jens Axboe2056a782006-03-23 20:00:26 +0100451 .subbuf_start = blk_subbuf_start_callback,
452 .create_buf_file = blk_create_buf_file_callback,
453 .remove_buf_file = blk_remove_buf_file_callback,
454};
455
Li Zefan9908c302009-04-14 13:59:34 +0800456static void blk_trace_setup_lba(struct blk_trace *bt,
457 struct block_device *bdev)
458{
Christoph Hellwig29ff57c2020-11-24 09:34:24 +0100459 if (bdev) {
460 bt->start_lba = bdev->bd_start_sect;
461 bt->end_lba = bdev->bd_start_sect + bdev_nr_sectors(bdev);
Li Zefan9908c302009-04-14 13:59:34 +0800462 } else {
463 bt->start_lba = 0;
464 bt->end_lba = -1ULL;
465 }
466}
467
Jens Axboe2056a782006-03-23 20:00:26 +0100468/*
469 * Setup everything required to start tracing
470 */
Omar Sandovala428d312017-01-31 14:53:19 -0800471static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
472 struct block_device *bdev,
473 struct blk_user_trace_setup *buts)
Jens Axboe2056a782006-03-23 20:00:26 +0100474{
Davidlohr Buesocdea01b2015-10-30 05:25:59 +0900475 struct blk_trace *bt = NULL;
Jens Axboe2056a782006-03-23 20:00:26 +0100476 struct dentry *dir = NULL;
Rasmus Villemoesff144172015-06-25 15:02:28 -0700477 int ret;
Jens Axboe2056a782006-03-23 20:00:26 +0100478
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000479 lockdep_assert_held(&q->debugfs_mutex);
Luis Chamberlaina67549c2020-06-19 20:47:26 +0000480
Arnd Bergmann171044d42007-10-09 13:23:53 +0200481 if (!buts->buf_size || !buts->buf_nr)
Jens Axboe2056a782006-03-23 20:00:26 +0100482 return -EINVAL;
483
Jens Axboe0497b342008-10-01 16:16:25 +0200484 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
485 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
Jens Axboe2056a782006-03-23 20:00:26 +0100486
487 /*
488 * some device names have larger paths - convert the slashes
489 * to underscores for this to work as expected
490 */
Rasmus Villemoesff144172015-06-25 15:02:28 -0700491 strreplace(buts->name, '/', '_');
Jens Axboe2056a782006-03-23 20:00:26 +0100492
Luis Chamberlain1b0b2832020-06-05 16:58:36 +0200493 /*
494 * bdev can be NULL, as with scsi-generic, this is a helpful as
495 * we can be.
496 */
Jan Karac3dbe542020-06-05 16:58:37 +0200497 if (rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000498 lockdep_is_held(&q->debugfs_mutex))) {
Luis Chamberlain1b0b2832020-06-05 16:58:36 +0200499 pr_warn("Concurrent blktraces are not allowed on %s\n",
500 buts->name);
501 return -EBUSY;
502 }
503
Jens Axboe2056a782006-03-23 20:00:26 +0100504 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
505 if (!bt)
Li Zefanad5dd542009-03-27 10:20:24 +0800506 return -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100507
Li Zefanad5dd542009-03-27 10:20:24 +0800508 ret = -ENOMEM;
Jens Axboe2056a782006-03-23 20:00:26 +0100509 bt->sequence = alloc_percpu(unsigned long);
510 if (!bt->sequence)
511 goto err;
512
Ingo Molnarf0ef0392009-03-06 16:44:14 +0100513 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
Jens Axboe64565912008-05-28 14:45:33 +0200514 if (!bt->msg_data)
515 goto err;
516
Luis Chamberlainbad8e642020-06-19 20:47:28 +0000517 /*
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000518 * When tracing the whole disk reuse the existing debugfs directory
519 * created by the block layer on init. For partitions block devices,
Luis Chamberlainbad8e642020-06-19 20:47:28 +0000520 * and scsi-generic block devices we create a temporary new debugfs
521 * directory that will be removed once the trace ends.
522 */
Christoph Hellwigfa01b1e2020-09-03 07:40:57 +0200523 if (bdev && !bdev_is_partition(bdev))
Luis Chamberlainbad8e642020-06-19 20:47:28 +0000524 dir = q->debugfs_dir;
525 else
Omar Sandoval6ac93112017-01-31 14:53:22 -0800526 bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root);
Jens Axboe2056a782006-03-23 20:00:26 +0100527
Luis Chamberlainb431ef82020-06-19 20:47:29 +0000528 /*
529 * As blktrace relies on debugfs for its interface the debugfs directory
530 * is required, contrary to the usual mantra of not checking for debugfs
531 * files or directories.
532 */
533 if (IS_ERR_OR_NULL(dir)) {
534 pr_warn("debugfs_dir not present for %s so skipping\n",
535 buts->name);
536 ret = -ENOENT;
537 goto err;
538 }
539
Christof Schmitt6da127a2008-01-11 10:09:43 +0100540 bt->dev = dev;
Jens Axboe2056a782006-03-23 20:00:26 +0100541 atomic_set(&bt->dropped, 0);
Jan Karaa404d552013-09-17 22:30:31 +0200542 INIT_LIST_HEAD(&bt->running_list);
Jens Axboe2056a782006-03-23 20:00:26 +0100543
544 ret = -EIO;
Greg Kroah-Hartmanc0ea5762021-02-16 16:52:47 +0100545 debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
546 debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
Alan D. Brunelle02c62302008-06-11 09:12:52 +0200547
Arnd Bergmann171044d42007-10-09 13:23:53 +0200548 bt->rchan = relay_open("trace", dir, buts->buf_size,
549 buts->buf_nr, &blk_relay_callbacks, bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100550 if (!bt->rchan)
551 goto err;
Jens Axboe2056a782006-03-23 20:00:26 +0100552
Arnd Bergmann171044d42007-10-09 13:23:53 +0200553 bt->act_mask = buts->act_mask;
Jens Axboe2056a782006-03-23 20:00:26 +0100554 if (!bt->act_mask)
555 bt->act_mask = (u16) -1;
556
Li Zefan9908c302009-04-14 13:59:34 +0800557 blk_trace_setup_lba(bt, bdev);
Jens Axboe2056a782006-03-23 20:00:26 +0100558
Shawn Dud0deef52009-04-14 13:58:56 +0800559 /* overwrite with user settings */
560 if (buts->start_lba)
561 bt->start_lba = buts->start_lba;
562 if (buts->end_lba)
563 bt->end_lba = buts->end_lba;
564
Arnd Bergmann171044d42007-10-09 13:23:53 +0200565 bt->pid = buts->pid;
Jens Axboe2056a782006-03-23 20:00:26 +0100566 bt->trace_state = Blktrace_setup;
567
Jan Karac3dbe542020-06-05 16:58:37 +0200568 rcu_assign_pointer(q->blk_trace, bt);
Jens Axboea6da0022017-11-05 09:16:09 -0700569 get_probe_ref();
Li Zefancbe28292009-03-20 09:48:47 +0800570
Omar Sandoval6ac93112017-01-31 14:53:22 -0800571 ret = 0;
Jens Axboe2056a782006-03-23 20:00:26 +0100572err:
Omar Sandoval6ac93112017-01-31 14:53:22 -0800573 if (ret)
574 blk_trace_free(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100575 return ret;
576}
577
Jens Axboe1f2cac12017-11-05 09:13:48 -0700578static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
579 struct block_device *bdev, char __user *arg)
Arnd Bergmann171044d42007-10-09 13:23:53 +0200580{
581 struct blk_user_trace_setup buts;
582 int ret;
583
584 ret = copy_from_user(&buts, arg, sizeof(buts));
585 if (ret)
586 return -EFAULT;
587
Shawn Dud0deef52009-04-14 13:58:56 +0800588 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200589 if (ret)
590 return ret;
591
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300592 if (copy_to_user(arg, &buts, sizeof(buts))) {
Jens Axboe2967acb2017-11-19 11:52:55 -0700593 __blk_trace_remove(q);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200594 return -EFAULT;
Dmitry Monakhov9a8c28c2010-02-27 20:35:13 +0300595 }
Arnd Bergmann171044d42007-10-09 13:23:53 +0200596 return 0;
597}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700598
599int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
600 struct block_device *bdev,
601 char __user *arg)
602{
603 int ret;
604
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000605 mutex_lock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700606 ret = __blk_trace_setup(q, name, dev, bdev, arg);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000607 mutex_unlock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700608
609 return ret;
610}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100611EXPORT_SYMBOL_GPL(blk_trace_setup);
Arnd Bergmann171044d42007-10-09 13:23:53 +0200612
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200613#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
614static int compat_blk_trace_setup(struct request_queue *q, char *name,
615 dev_t dev, struct block_device *bdev,
616 char __user *arg)
617{
618 struct blk_user_trace_setup buts;
619 struct compat_blk_user_trace_setup cbuts;
620 int ret;
621
622 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
623 return -EFAULT;
624
625 buts = (struct blk_user_trace_setup) {
626 .act_mask = cbuts.act_mask,
627 .buf_size = cbuts.buf_size,
628 .buf_nr = cbuts.buf_nr,
629 .start_lba = cbuts.start_lba,
630 .end_lba = cbuts.end_lba,
631 .pid = cbuts.pid,
632 };
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200633
634 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
635 if (ret)
636 return ret;
637
Chen Gangf8c5e942013-11-03 22:23:39 +0800638 if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
Jens Axboe2967acb2017-11-19 11:52:55 -0700639 __blk_trace_remove(q);
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200640 return -EFAULT;
641 }
642
643 return 0;
644}
645#endif
646
Jens Axboe1f2cac12017-11-05 09:13:48 -0700647static int __blk_trace_startstop(struct request_queue *q, int start)
Jens Axboe2056a782006-03-23 20:00:26 +0100648{
Jens Axboe2056a782006-03-23 20:00:26 +0100649 int ret;
Jan Karac780e862020-02-06 15:28:12 +0100650 struct blk_trace *bt;
Jens Axboe2056a782006-03-23 20:00:26 +0100651
Jan Karac780e862020-02-06 15:28:12 +0100652 bt = rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000653 lockdep_is_held(&q->debugfs_mutex));
Arnaldo Carvalho de Melo939b3662009-02-03 11:58:29 -0200654 if (bt == NULL)
Jens Axboe2056a782006-03-23 20:00:26 +0100655 return -EINVAL;
656
657 /*
658 * For starting a trace, we can transition from a setup or stopped
659 * trace. For stopping a trace, the state must be running
660 */
661 ret = -EINVAL;
662 if (start) {
663 if (bt->trace_state == Blktrace_setup ||
664 bt->trace_state == Blktrace_stopped) {
665 blktrace_seq++;
666 smp_mb();
667 bt->trace_state = Blktrace_running;
Jan Karaa404d552013-09-17 22:30:31 +0200668 spin_lock_irq(&running_trace_lock);
669 list_add(&bt->running_list, &running_trace_list);
670 spin_unlock_irq(&running_trace_lock);
Olaf Kirchbe1c6342006-12-01 10:39:12 +0100671
672 trace_note_time(bt);
Jens Axboe2056a782006-03-23 20:00:26 +0100673 ret = 0;
674 }
675 } else {
676 if (bt->trace_state == Blktrace_running) {
677 bt->trace_state = Blktrace_stopped;
Jan Karaa404d552013-09-17 22:30:31 +0200678 spin_lock_irq(&running_trace_lock);
679 list_del_init(&bt->running_list);
680 spin_unlock_irq(&running_trace_lock);
Jens Axboe2056a782006-03-23 20:00:26 +0100681 relay_flush(bt->rchan);
682 ret = 0;
683 }
684 }
685
686 return ret;
687}
Jens Axboe1f2cac12017-11-05 09:13:48 -0700688
689int blk_trace_startstop(struct request_queue *q, int start)
690{
691 int ret;
692
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000693 mutex_lock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700694 ret = __blk_trace_startstop(q, start);
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000695 mutex_unlock(&q->debugfs_mutex);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700696
697 return ret;
698}
Christof Schmitt6da127a2008-01-11 10:09:43 +0100699EXPORT_SYMBOL_GPL(blk_trace_startstop);
Jens Axboe2056a782006-03-23 20:00:26 +0100700
Waiman Long5acb3cc2017-09-20 13:12:20 -0600701/*
702 * When reading or writing the blktrace sysfs files, the references to the
703 * opened sysfs or device files should prevent the underlying block device
704 * from being removed. So no further delete protection is really needed.
705 */
706
Jens Axboe2056a782006-03-23 20:00:26 +0100707/**
708 * blk_trace_ioctl: - handle the ioctls associated with tracing
709 * @bdev: the block device
Steven Rostedtef180122009-03-10 14:10:56 -0400710 * @cmd: the ioctl cmd
Jens Axboe2056a782006-03-23 20:00:26 +0100711 * @arg: the argument data, if any
712 *
713 **/
714int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
715{
Jens Axboe165125e2007-07-24 09:28:11 +0200716 struct request_queue *q;
Jens Axboe2056a782006-03-23 20:00:26 +0100717 int ret, start = 0;
Christof Schmitt6da127a2008-01-11 10:09:43 +0100718 char b[BDEVNAME_SIZE];
Jens Axboe2056a782006-03-23 20:00:26 +0100719
720 q = bdev_get_queue(bdev);
721 if (!q)
722 return -ENXIO;
723
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000724 mutex_lock(&q->debugfs_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100725
726 switch (cmd) {
727 case BLKTRACESETUP:
Jean Delvaref36f21e2008-05-12 14:02:33 -0700728 bdevname(bdev, b);
Jens Axboe1f2cac12017-11-05 09:13:48 -0700729 ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
Jens Axboe2056a782006-03-23 20:00:26 +0100730 break;
Arnd Bergmann62c2a7d2010-07-07 16:51:26 +0200731#if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
732 case BLKTRACESETUP32:
733 bdevname(bdev, b);
734 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
735 break;
736#endif
Jens Axboe2056a782006-03-23 20:00:26 +0100737 case BLKTRACESTART:
738 start = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500739 fallthrough;
Jens Axboe2056a782006-03-23 20:00:26 +0100740 case BLKTRACESTOP:
Jens Axboe1f2cac12017-11-05 09:13:48 -0700741 ret = __blk_trace_startstop(q, start);
Jens Axboe2056a782006-03-23 20:00:26 +0100742 break;
743 case BLKTRACETEARDOWN:
Jens Axboe1f2cac12017-11-05 09:13:48 -0700744 ret = __blk_trace_remove(q);
Jens Axboe2056a782006-03-23 20:00:26 +0100745 break;
746 default:
747 ret = -ENOTTY;
748 break;
749 }
750
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000751 mutex_unlock(&q->debugfs_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100752 return ret;
753}
754
755/**
756 * blk_trace_shutdown: - stop and cleanup trace structures
757 * @q: the request queue associated with the device
758 *
759 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200760void blk_trace_shutdown(struct request_queue *q)
Jens Axboe2056a782006-03-23 20:00:26 +0100761{
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000762 mutex_lock(&q->debugfs_mutex);
Jan Karac780e862020-02-06 15:28:12 +0100763 if (rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000764 lockdep_is_held(&q->debugfs_mutex))) {
Jens Axboe1f2cac12017-11-05 09:13:48 -0700765 __blk_trace_startstop(q, 0);
766 __blk_trace_remove(q);
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -0700767 }
Jens Axboe1f2cac12017-11-05 09:13:48 -0700768
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000769 mutex_unlock(&q->debugfs_mutex);
Jens Axboe2056a782006-03-23 20:00:26 +0100770}
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100771
Shaohua Lica1136c2017-07-12 11:49:53 -0700772#ifdef CONFIG_BLK_CGROUP
Tejun Heo67c04962019-11-04 15:54:30 -0800773static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
Shaohua Lica1136c2017-07-12 11:49:53 -0700774{
Jan Karac780e862020-02-06 15:28:12 +0100775 struct blk_trace *bt;
Shaohua Lica1136c2017-07-12 11:49:53 -0700776
Jan Karac780e862020-02-06 15:28:12 +0100777 /* We don't use the 'bt' value here except as an optimization... */
778 bt = rcu_dereference_protected(q->blk_trace, 1);
Shaohua Lica1136c2017-07-12 11:49:53 -0700779 if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
Tejun Heo67c04962019-11-04 15:54:30 -0800780 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700781
Dennis Zhoudb6638d2018-12-05 12:10:35 -0500782 if (!bio->bi_blkg)
Tejun Heo67c04962019-11-04 15:54:30 -0800783 return 0;
Tejun Heo74321032019-11-04 15:54:30 -0800784 return cgroup_id(bio_blkcg(bio)->css.cgroup);
Shaohua Lica1136c2017-07-12 11:49:53 -0700785}
786#else
Wang Haie75ad2c2020-09-07 22:06:52 +0800787static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
Shaohua Lica1136c2017-07-12 11:49:53 -0700788{
Tejun Heo67c04962019-11-04 15:54:30 -0800789 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700790}
791#endif
792
Tejun Heo67c04962019-11-04 15:54:30 -0800793static u64
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100794blk_trace_request_get_cgid(struct request *rq)
Shaohua Lica1136c2017-07-12 11:49:53 -0700795{
796 if (!rq->bio)
Tejun Heo67c04962019-11-04 15:54:30 -0800797 return 0;
Shaohua Lica1136c2017-07-12 11:49:53 -0700798 /* Use the first bio */
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100799 return blk_trace_bio_get_cgid(rq->q, rq->bio);
Shaohua Lica1136c2017-07-12 11:49:53 -0700800}
801
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100802/*
803 * blktrace probes
804 */
805
806/**
807 * blk_add_trace_rq - Add a trace for a request oriented action
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100808 * @rq: the source request
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200809 * @error: return status to log
Roman Penaf5040d2014-03-04 23:13:10 +0900810 * @nr_bytes: number of completed bytes
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100811 * @what: the action
Shaohua Lica1136c2017-07-12 11:49:53 -0700812 * @cgid: the cgroup info
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100813 *
814 * Description:
815 * Records an action against a request. Will log the bio offset + size.
816 *
817 **/
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200818static void blk_add_trace_rq(struct request *rq, int error,
Tejun Heo67c04962019-11-04 15:54:30 -0800819 unsigned int nr_bytes, u32 what, u64 cgid)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100820{
Jan Karac780e862020-02-06 15:28:12 +0100821 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100822
Jan Karac780e862020-02-06 15:28:12 +0100823 rcu_read_lock();
824 bt = rcu_dereference(rq->q->blk_trace);
825 if (likely(!bt)) {
826 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100827 return;
Jan Karac780e862020-02-06 15:28:12 +0100828 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100829
Christoph Hellwig57292b52017-01-31 16:57:29 +0100830 if (blk_rq_is_passthrough(rq))
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100831 what |= BLK_TC_ACT(BLK_TC_PC);
Christoph Hellwig48b77ad2017-01-27 09:35:54 +0100832 else
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100833 what |= BLK_TC_ACT(BLK_TC_FS);
Christoph Hellwig48b77ad2017-01-27 09:35:54 +0100834
835 __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, req_op(rq),
Shaohua Lica1136c2017-07-12 11:49:53 -0700836 rq->cmd_flags, what, error, 0, NULL, cgid);
Jan Karac780e862020-02-06 15:28:12 +0100837 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100838}
839
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100840static void blk_add_trace_rq_insert(void *ignore, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100841{
Shaohua Lica1136c2017-07-12 11:49:53 -0700842 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT,
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100843 blk_trace_request_get_cgid(rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100844}
845
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100846static void blk_add_trace_rq_issue(void *ignore, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100847{
Shaohua Lica1136c2017-07-12 11:49:53 -0700848 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE,
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100849 blk_trace_request_get_cgid(rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100850}
851
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100852static void blk_add_trace_rq_merge(void *ignore, struct request *rq)
Jan Karaf3bdc622020-06-17 15:58:23 +0200853{
854 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_BACKMERGE,
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100855 blk_trace_request_get_cgid(rq));
Jan Karaf3bdc622020-06-17 15:58:23 +0200856}
857
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100858static void blk_add_trace_rq_requeue(void *ignore, struct request *rq)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100859{
Shaohua Lica1136c2017-07-12 11:49:53 -0700860 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE,
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100861 blk_trace_request_get_cgid(rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100862}
863
Christoph Hellwigcaf7df12017-04-20 16:03:16 +0200864static void blk_add_trace_rq_complete(void *ignore, struct request *rq,
865 int error, unsigned int nr_bytes)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100866{
Shaohua Lica1136c2017-07-12 11:49:53 -0700867 blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE,
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100868 blk_trace_request_get_cgid(rq));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100869}
870
871/**
872 * blk_add_trace_bio - Add a trace for a bio oriented action
873 * @q: queue the io is for
874 * @bio: the source bio
875 * @what: the action
Jens Axboe797a4552011-01-10 09:06:44 +0100876 * @error: error, if any
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100877 *
878 * Description:
879 * Records an action against a bio. Will log the bio offset + size.
880 *
881 **/
882static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200883 u32 what, int error)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100884{
Jan Karac780e862020-02-06 15:28:12 +0100885 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100886
Jan Karac780e862020-02-06 15:28:12 +0100887 rcu_read_lock();
888 bt = rcu_dereference(q->blk_trace);
889 if (likely(!bt)) {
890 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100891 return;
Jan Karac780e862020-02-06 15:28:12 +0100892 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100893
Kent Overstreet4f024f32013-10-11 15:44:27 -0700894 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
Marcos Paulo de Souza16901022017-11-19 16:48:13 -0200895 bio_op(bio), bio->bi_opf, what, error, 0, NULL,
896 blk_trace_bio_get_cgid(q, bio));
Jan Karac780e862020-02-06 15:28:12 +0100897 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100898}
899
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100900static void blk_add_trace_bio_bounce(void *ignore, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100901{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100902 blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_BOUNCE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100903}
904
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700905static void blk_add_trace_bio_complete(void *ignore,
Christoph Hellwigd24de76a2020-06-03 07:14:43 +0200906 struct request_queue *q, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100907{
Christoph Hellwigd24de76a2020-06-03 07:14:43 +0200908 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE,
909 blk_status_to_errno(bio->bi_status));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100910}
911
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100912static void blk_add_trace_bio_backmerge(void *ignore, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100913{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100914 blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_BACKMERGE,
915 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100916}
917
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100918static void blk_add_trace_bio_frontmerge(void *ignore, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100919{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100920 blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_FRONTMERGE,
921 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100922}
923
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100924static void blk_add_trace_bio_queue(void *ignore, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100925{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100926 blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_QUEUE, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100927}
928
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100929static void blk_add_trace_getrq(void *ignore, struct bio *bio)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100930{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100931 blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_GETRQ, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100932}
933
Steven Rostedt38516ab2010-04-20 17:04:50 -0400934static void blk_add_trace_plug(void *ignore, struct request_queue *q)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100935{
Jan Karac780e862020-02-06 15:28:12 +0100936 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100937
Jan Karac780e862020-02-06 15:28:12 +0100938 rcu_read_lock();
939 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100940 if (bt)
Tejun Heo67c04962019-11-04 15:54:30 -0800941 __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL, 0);
Jan Karac780e862020-02-06 15:28:12 +0100942 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100943}
944
Jens Axboe49cac012011-04-16 13:51:05 +0200945static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
946 unsigned int depth, bool explicit)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100947{
Jan Karac780e862020-02-06 15:28:12 +0100948 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100949
Jan Karac780e862020-02-06 15:28:12 +0100950 rcu_read_lock();
951 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100952 if (bt) {
Jens Axboe94b5eb22011-04-12 10:12:19 +0200953 __be64 rpdu = cpu_to_be64(depth);
Jens Axboe49cac012011-04-16 13:51:05 +0200954 u32 what;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100955
Jens Axboe49cac012011-04-16 13:51:05 +0200956 if (explicit)
957 what = BLK_TA_UNPLUG_IO;
958 else
959 what = BLK_TA_UNPLUG_TIMER;
960
Tejun Heo67c04962019-11-04 15:54:30 -0800961 __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu, 0);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100962 }
Jan Karac780e862020-02-06 15:28:12 +0100963 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100964}
965
Christoph Hellwigeb6f7f72020-12-03 17:21:37 +0100966static void blk_add_trace_split(void *ignore, struct bio *bio, unsigned int pdu)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100967{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100968 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
Jan Karac780e862020-02-06 15:28:12 +0100969 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100970
Jan Karac780e862020-02-06 15:28:12 +0100971 rcu_read_lock();
972 bt = rcu_dereference(q->blk_trace);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100973 if (bt) {
974 __be64 rpdu = cpu_to_be64(pdu);
975
Kent Overstreet4f024f32013-10-11 15:44:27 -0700976 __blk_add_trace(bt, bio->bi_iter.bi_sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -0600977 bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf,
Chaitanya Kulkarni48bc3cd2020-06-04 00:13:28 -0700978 BLK_TA_SPLIT,
979 blk_status_to_errno(bio->bi_status),
980 sizeof(rpdu), &rpdu,
981 blk_trace_bio_get_cgid(q, bio));
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100982 }
Jan Karac780e862020-02-06 15:28:12 +0100983 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100984}
985
986/**
Mike Snitzerd07335e2010-11-16 12:52:38 +0100987 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -0700988 * @ignore: trace callback data parameter (not used)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100989 * @bio: the source bio
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100990 * @dev: source device
Alan D. Brunellea42aaa32009-05-04 16:27:26 -0400991 * @from: source sector
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100992 *
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100993 * Called after a bio is remapped to a different device and/or sector.
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100994 **/
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100995static void blk_add_trace_bio_remap(void *ignore, struct bio *bio, dev_t dev,
996 sector_t from)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +0100997{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100998 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
Jan Karac780e862020-02-06 15:28:12 +0100999 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001000 struct blk_io_trace_remap r;
1001
Jan Karac780e862020-02-06 15:28:12 +01001002 rcu_read_lock();
1003 bt = rcu_dereference(q->blk_trace);
1004 if (likely(!bt)) {
1005 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001006 return;
Jan Karac780e862020-02-06 15:28:12 +01001007 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001008
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001009 r.device_from = cpu_to_be32(dev);
Christoph Hellwig74d46992017-08-23 19:10:32 +02001010 r.device_to = cpu_to_be32(bio_dev(bio));
Alan D. Brunellea42aaa32009-05-04 16:27:26 -04001011 r.sector_from = cpu_to_be64(from);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001012
Kent Overstreet4f024f32013-10-11 15:44:27 -07001013 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
Chaitanya Kulkarni48bc3cd2020-06-04 00:13:28 -07001014 bio_op(bio), bio->bi_opf, BLK_TA_REMAP,
1015 blk_status_to_errno(bio->bi_status),
Shaohua Lica1136c2017-07-12 11:49:53 -07001016 sizeof(r), &r, blk_trace_bio_get_cgid(q, bio));
Jan Karac780e862020-02-06 15:28:12 +01001017 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001018}
1019
1020/**
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001021 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
Randy Dunlap546cf442010-05-29 11:45:07 -07001022 * @ignore: trace callback data parameter (not used)
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001023 * @rq: the source request
1024 * @dev: target device
1025 * @from: source sector
1026 *
1027 * Description:
1028 * Device mapper remaps request to other devices.
1029 * Add a trace for that action.
1030 *
1031 **/
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001032static void blk_add_trace_rq_remap(void *ignore, struct request *rq, dev_t dev,
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001033 sector_t from)
1034{
Jan Karac780e862020-02-06 15:28:12 +01001035 struct blk_trace *bt;
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001036 struct blk_io_trace_remap r;
1037
Jan Karac780e862020-02-06 15:28:12 +01001038 rcu_read_lock();
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001039 bt = rcu_dereference(rq->q->blk_trace);
Jan Karac780e862020-02-06 15:28:12 +01001040 if (likely(!bt)) {
1041 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001042 return;
Jan Karac780e862020-02-06 15:28:12 +01001043 }
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001044
1045 r.device_from = cpu_to_be32(dev);
1046 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
1047 r.sector_from = cpu_to_be64(from);
1048
1049 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
Christoph Hellwigcaf7df12017-04-20 16:03:16 +02001050 rq_data_dir(rq), 0, BLK_TA_REMAP, 0,
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001051 sizeof(r), &r, blk_trace_request_get_cgid(rq));
Jan Karac780e862020-02-06 15:28:12 +01001052 rcu_read_unlock();
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001053}
1054
1055/**
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001056 * blk_add_driver_data - Add binary message with driver-specific data
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001057 * @rq: io request
1058 * @data: driver-specific data
1059 * @len: length of driver-specific data
1060 *
1061 * Description:
1062 * Some drivers might want to write driver-specific data per request.
1063 *
1064 **/
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001065void blk_add_driver_data(struct request *rq, void *data, size_t len)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001066{
Jan Karac780e862020-02-06 15:28:12 +01001067 struct blk_trace *bt;
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001068
Jan Karac780e862020-02-06 15:28:12 +01001069 rcu_read_lock();
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001070 bt = rcu_dereference(rq->q->blk_trace);
Jan Karac780e862020-02-06 15:28:12 +01001071 if (likely(!bt)) {
1072 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001073 return;
Jan Karac780e862020-02-06 15:28:12 +01001074 }
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001075
Christoph Hellwig48b77ad2017-01-27 09:35:54 +01001076 __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0,
Shaohua Lica1136c2017-07-12 11:49:53 -07001077 BLK_TA_DRV_DATA, 0, len, data,
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001078 blk_trace_request_get_cgid(rq));
Jan Karac780e862020-02-06 15:28:12 +01001079 rcu_read_unlock();
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001080}
1081EXPORT_SYMBOL_GPL(blk_add_driver_data);
1082
Li Zefan3c289ba2009-03-20 09:48:26 +08001083static void blk_register_tracepoints(void)
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001084{
1085 int ret;
1086
Steven Rostedt38516ab2010-04-20 17:04:50 -04001087 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001088 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001089 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001090 WARN_ON(ret);
Jan Karaf3bdc622020-06-17 15:58:23 +02001091 ret = register_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
1092 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001093 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001094 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001095 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001096 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001097 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001098 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001099 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001100 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001101 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001102 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001103 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001104 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001105 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001106 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001107 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001108 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001109 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001110 WARN_ON(ret);
Jens Axboe49cac012011-04-16 13:51:05 +02001111 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001112 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001113 ret = register_trace_block_split(blk_add_trace_split, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001114 WARN_ON(ret);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001115 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001116 WARN_ON(ret);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001117 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +02001118 WARN_ON(ret);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001119}
1120
1121static void blk_unregister_tracepoints(void)
1122{
Steven Rostedt38516ab2010-04-20 17:04:50 -04001123 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
Mike Snitzerd07335e2010-11-16 12:52:38 +01001124 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001125 unregister_trace_block_split(blk_add_trace_split, NULL);
Jens Axboe49cac012011-04-16 13:51:05 +02001126 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001127 unregister_trace_block_plug(blk_add_trace_plug, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001128 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1129 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1130 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1131 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1132 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1133 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1134 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1135 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
Jan Karaf3bdc622020-06-17 15:58:23 +02001136 unregister_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
Steven Rostedt38516ab2010-04-20 17:04:50 -04001137 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1138 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001139
1140 tracepoint_synchronize_unregister();
1141}
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001142
1143/*
1144 * struct blk_io_tracer formatting routines
1145 */
1146
1147static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1148{
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001149 int i = 0;
Li Zefan65796342009-03-24 16:05:06 +08001150 int tc = t->action >> BLK_TC_SHIFT;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001151
Shaohua Lica1136c2017-07-12 11:49:53 -07001152 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
Li Zefan18cea452009-03-27 10:21:54 +08001153 rwbs[i++] = 'N';
1154 goto out;
1155 }
1156
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001157 if (tc & BLK_TC_FLUSH)
1158 rwbs[i++] = 'F';
1159
Li Zefan65796342009-03-24 16:05:06 +08001160 if (tc & BLK_TC_DISCARD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001161 rwbs[i++] = 'D';
Li Zefan65796342009-03-24 16:05:06 +08001162 else if (tc & BLK_TC_WRITE)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001163 rwbs[i++] = 'W';
1164 else if (t->bytes)
1165 rwbs[i++] = 'R';
1166 else
1167 rwbs[i++] = 'N';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001168
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001169 if (tc & BLK_TC_FUA)
1170 rwbs[i++] = 'F';
Li Zefan65796342009-03-24 16:05:06 +08001171 if (tc & BLK_TC_AHEAD)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001172 rwbs[i++] = 'A';
Li Zefan65796342009-03-24 16:05:06 +08001173 if (tc & BLK_TC_SYNC)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001174 rwbs[i++] = 'S';
Li Zefan65796342009-03-24 16:05:06 +08001175 if (tc & BLK_TC_META)
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001176 rwbs[i++] = 'M';
Li Zefan18cea452009-03-27 10:21:54 +08001177out:
Arnaldo Carvalho de Melo157f9c02009-01-26 15:00:56 -02001178 rwbs[i] = '\0';
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001179}
1180
1181static inline
1182const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1183{
1184 return (const struct blk_io_trace *)ent;
1185}
1186
Shaohua Lica1136c2017-07-12 11:49:53 -07001187static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001188{
Tejun Heo67c04962019-11-04 15:54:30 -08001189 return (void *)(te_blk_io_trace(ent) + 1) + (has_cg ? sizeof(u64) : 0);
Shaohua Lica1136c2017-07-12 11:49:53 -07001190}
1191
Tejun Heo67c04962019-11-04 15:54:30 -08001192static inline u64 t_cgid(const struct trace_entry *ent)
Shaohua Lica1136c2017-07-12 11:49:53 -07001193{
Tejun Heo67c04962019-11-04 15:54:30 -08001194 return *(u64 *)(te_blk_io_trace(ent) + 1);
Shaohua Lica1136c2017-07-12 11:49:53 -07001195}
1196
1197static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg)
1198{
Tejun Heo67c04962019-11-04 15:54:30 -08001199 return te_blk_io_trace(ent)->pdu_len - (has_cg ? sizeof(u64) : 0);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001200}
1201
Li Zefan66de7792009-04-01 16:19:19 +08001202static inline u32 t_action(const struct trace_entry *ent)
1203{
1204 return te_blk_io_trace(ent)->action;
1205}
1206
1207static inline u32 t_bytes(const struct trace_entry *ent)
1208{
1209 return te_blk_io_trace(ent)->bytes;
1210}
1211
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001212static inline u32 t_sec(const struct trace_entry *ent)
1213{
1214 return te_blk_io_trace(ent)->bytes >> 9;
1215}
1216
1217static inline unsigned long long t_sector(const struct trace_entry *ent)
1218{
1219 return te_blk_io_trace(ent)->sector;
1220}
1221
1222static inline __u16 t_error(const struct trace_entry *ent)
1223{
Li Zefane0dc81b2009-03-24 16:05:51 +08001224 return te_blk_io_trace(ent)->error;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001225}
1226
Shaohua Lica1136c2017-07-12 11:49:53 -07001227static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001228{
Chaitanya Kulkarni71df3fd2020-06-04 00:13:29 -07001229 const __be64 *val = pdu_start(ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001230 return be64_to_cpu(*val);
1231}
1232
Shaohua Lica1136c2017-07-12 11:49:53 -07001233typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act,
1234 bool has_cg);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001235
Shaohua Lica1136c2017-07-12 11:49:53 -07001236static void blk_log_action_classic(struct trace_iterator *iter, const char *act,
1237 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001238{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001239 char rwbs[RWBS_LEN];
Li Zefan35ac51b2009-03-27 10:19:46 +08001240 unsigned long long ts = iter->ts;
1241 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001242 unsigned secs = (unsigned long)ts;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001243 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001244
1245 fill_rwbs(rwbs, t);
1246
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001247 trace_seq_printf(&iter->seq,
1248 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1249 MAJOR(t->device), MINOR(t->device), iter->cpu,
1250 secs, nsec_rem, iter->ent->pid, act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001251}
1252
Shaohua Lica1136c2017-07-12 11:49:53 -07001253static void blk_log_action(struct trace_iterator *iter, const char *act,
1254 bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001255{
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001256 char rwbs[RWBS_LEN];
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001257 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1258
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001259 fill_rwbs(rwbs, t);
Shaohua Lica1136c2017-07-12 11:49:53 -07001260 if (has_cg) {
Tejun Heo67c04962019-11-04 15:54:30 -08001261 u64 id = t_cgid(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001262
Shaohua Li69fd5c32017-07-12 11:49:55 -07001263 if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) {
1264 char blkcg_name_buf[NAME_MAX + 1] = "<...>";
1265
1266 cgroup_path_from_kernfs_id(id, blkcg_name_buf,
1267 sizeof(blkcg_name_buf));
1268 trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ",
1269 MAJOR(t->device), MINOR(t->device),
1270 blkcg_name_buf, act, rwbs);
Tejun Heo40430452019-11-04 15:54:30 -08001271 } else {
1272 /*
1273 * The cgid portion used to be "INO,GEN". Userland
1274 * builds a FILEID_INO32_GEN fid out of them and
1275 * opens the cgroup using open_by_handle_at(2).
1276 * While 32bit ino setups are still the same, 64bit
1277 * ones now use the 64bit ino as the whole ID and
1278 * no longer use generation.
1279 *
Qiujun Huang2b5894c2020-10-29 23:05:54 +08001280 * Regardless of the content, always output
Tejun Heo40430452019-11-04 15:54:30 -08001281 * "LOW32,HIGH32" so that FILEID_INO32_GEN fid can
1282 * be mapped back to @id on both 64 and 32bit ino
1283 * setups. See __kernfs_fh_to_dentry().
1284 */
Shaohua Li69fd5c32017-07-12 11:49:55 -07001285 trace_seq_printf(&iter->seq,
Tejun Heo40430452019-11-04 15:54:30 -08001286 "%3d,%-3d %llx,%-llx %2s %3s ",
Shaohua Lica1136c2017-07-12 11:49:53 -07001287 MAJOR(t->device), MINOR(t->device),
Tejun Heo40430452019-11-04 15:54:30 -08001288 id & U32_MAX, id >> 32, act, rwbs);
1289 }
Shaohua Lica1136c2017-07-12 11:49:53 -07001290 } else
1291 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1292 MAJOR(t->device), MINOR(t->device), act, rwbs);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001293}
1294
Shaohua Lica1136c2017-07-12 11:49:53 -07001295static void blk_log_dump_pdu(struct trace_seq *s,
1296 const struct trace_entry *ent, bool has_cg)
Li Zefan66de7792009-04-01 16:19:19 +08001297{
Li Zefan049862572009-05-11 14:33:23 +08001298 const unsigned char *pdu_buf;
Li Zefan66de7792009-04-01 16:19:19 +08001299 int pdu_len;
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001300 int i, end;
Li Zefan66de7792009-04-01 16:19:19 +08001301
Shaohua Lica1136c2017-07-12 11:49:53 -07001302 pdu_buf = pdu_start(ent, has_cg);
1303 pdu_len = pdu_real_len(ent, has_cg);
Li Zefan66de7792009-04-01 16:19:19 +08001304
1305 if (!pdu_len)
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001306 return;
Li Zefan66de7792009-04-01 16:19:19 +08001307
1308 /* find the last zero that needs to be printed */
1309 for (end = pdu_len - 1; end >= 0; end--)
1310 if (pdu_buf[end])
1311 break;
1312 end++;
1313
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001314 trace_seq_putc(s, '(');
Li Zefan66de7792009-04-01 16:19:19 +08001315
1316 for (i = 0; i < pdu_len; i++) {
1317
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001318 trace_seq_printf(s, "%s%02x",
1319 i == 0 ? "" : " ", pdu_buf[i]);
Li Zefan66de7792009-04-01 16:19:19 +08001320
1321 /*
Qiujun Huang2b5894c2020-10-29 23:05:54 +08001322 * stop when the rest is just zeros and indicate so
Li Zefan66de7792009-04-01 16:19:19 +08001323 * with a ".." appended
1324 */
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001325 if (i == end && end != pdu_len - 1) {
1326 trace_seq_puts(s, " ..) ");
1327 return;
1328 }
Li Zefan66de7792009-04-01 16:19:19 +08001329 }
1330
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001331 trace_seq_puts(s, ") ");
Li Zefan66de7792009-04-01 16:19:19 +08001332}
1333
Shaohua Lica1136c2017-07-12 11:49:53 -07001334static 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 -02001335{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001336 char cmd[TASK_COMM_LEN];
1337
1338 trace_find_cmdline(ent->pid, cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001339
Li Zefan66de7792009-04-01 16:19:19 +08001340 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001341 trace_seq_printf(s, "%u ", t_bytes(ent));
Shaohua Lica1136c2017-07-12 11:49:53 -07001342 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001343 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001344 } else {
1345 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001346 trace_seq_printf(s, "%llu + %u [%s]\n",
Li Zefan66de7792009-04-01 16:19:19 +08001347 t_sector(ent), t_sec(ent), cmd);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001348 else
1349 trace_seq_printf(s, "[%s]\n", cmd);
Li Zefan66de7792009-04-01 16:19:19 +08001350 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001351}
1352
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001353static void blk_log_with_error(struct trace_seq *s,
Shaohua Lica1136c2017-07-12 11:49:53 -07001354 const struct trace_entry *ent, bool has_cg)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001355{
Li Zefan66de7792009-04-01 16:19:19 +08001356 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
Shaohua Lica1136c2017-07-12 11:49:53 -07001357 blk_log_dump_pdu(s, ent, has_cg);
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001358 trace_seq_printf(s, "[%d]\n", t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001359 } else {
1360 if (t_sec(ent))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001361 trace_seq_printf(s, "%llu + %u [%d]\n",
1362 t_sector(ent),
1363 t_sec(ent), t_error(ent));
1364 else
1365 trace_seq_printf(s, "%llu [%d]\n",
1366 t_sector(ent), t_error(ent));
Li Zefan66de7792009-04-01 16:19:19 +08001367 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001368}
1369
Shaohua Lica1136c2017-07-12 11:49:53 -07001370static 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 -02001371{
Chaitanya Kulkarni5aec5982020-06-04 00:13:30 -07001372 const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001373
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001374 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1375 t_sector(ent), t_sec(ent),
Chaitanya Kulkarni5aec5982020-06-04 00:13:30 -07001376 MAJOR(be32_to_cpu(__r->device_from)),
1377 MINOR(be32_to_cpu(__r->device_from)),
1378 be64_to_cpu(__r->sector_from));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001379}
1380
Shaohua Lica1136c2017-07-12 11:49:53 -07001381static 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 -02001382{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001383 char cmd[TASK_COMM_LEN];
1384
1385 trace_find_cmdline(ent->pid, cmd);
1386
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001387 trace_seq_printf(s, "[%s]\n", cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001388}
1389
Shaohua Lica1136c2017-07-12 11:49:53 -07001390static 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 -02001391{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001392 char cmd[TASK_COMM_LEN];
1393
1394 trace_find_cmdline(ent->pid, cmd);
1395
Shaohua Lica1136c2017-07-12 11:49:53 -07001396 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001397}
1398
Shaohua Lica1136c2017-07-12 11:49:53 -07001399static 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 -02001400{
Steven Rostedt4ca530852009-03-16 19:20:15 -04001401 char cmd[TASK_COMM_LEN];
1402
1403 trace_find_cmdline(ent->pid, cmd);
1404
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001405 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
Shaohua Lica1136c2017-07-12 11:49:53 -07001406 get_pdu_int(ent, has_cg), cmd);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001407}
1408
Shaohua Lica1136c2017-07-12 11:49:53 -07001409static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent,
1410 bool has_cg)
Li Zefan18cea452009-03-27 10:21:54 +08001411{
Li Zefan18cea452009-03-27 10:21:54 +08001412
Shaohua Lica1136c2017-07-12 11:49:53 -07001413 trace_seq_putmem(s, pdu_start(ent, has_cg),
1414 pdu_real_len(ent, has_cg));
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001415 trace_seq_putc(s, '\n');
Li Zefan18cea452009-03-27 10:21:54 +08001416}
1417
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001418/*
1419 * struct tracer operations
1420 */
1421
1422static void blk_tracer_print_header(struct seq_file *m)
1423{
1424 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1425 return;
1426 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1427 "# | | | | | |\n");
1428}
1429
1430static void blk_tracer_start(struct trace_array *tr)
1431{
Li Zefanad5dd542009-03-27 10:20:24 +08001432 blk_tracer_enabled = true;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001433}
1434
1435static int blk_tracer_init(struct trace_array *tr)
1436{
1437 blk_tr = tr;
1438 blk_tracer_start(tr);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001439 return 0;
1440}
1441
1442static void blk_tracer_stop(struct trace_array *tr)
1443{
Li Zefanad5dd542009-03-27 10:20:24 +08001444 blk_tracer_enabled = false;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001445}
1446
1447static void blk_tracer_reset(struct trace_array *tr)
1448{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001449 blk_tracer_stop(tr);
1450}
1451
Li Zefane4955c92009-03-24 16:04:37 +08001452static const struct {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001453 const char *act[2];
Shaohua Lica1136c2017-07-12 11:49:53 -07001454 void (*print)(struct trace_seq *s, const struct trace_entry *ent,
1455 bool has_cg);
Li Zefane4955c92009-03-24 16:04:37 +08001456} what2act[] = {
Steven Rostedtef180122009-03-10 14:10:56 -04001457 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001458 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1459 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1460 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1461 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1462 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1463 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1464 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1465 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1466 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
Jens Axboe49cac012011-04-16 13:51:05 +02001467 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001468 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1469 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1470 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1471 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1472};
1473
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001474static enum print_line_t print_one_line(struct trace_iterator *iter,
1475 bool classic)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001476{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001477 struct trace_array *tr = iter->tr;
Arnaldo Carvalho de Melo2c9b2382009-02-02 20:30:12 -02001478 struct trace_seq *s = &iter->seq;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001479 const struct blk_io_trace *t;
1480 u16 what;
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001481 bool long_act;
1482 blk_log_action_t *log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001483 bool has_cg;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001484
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001485 t = te_blk_io_trace(iter->ent);
Shaohua Lica1136c2017-07-12 11:49:53 -07001486 what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001487 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001488 log_action = classic ? &blk_log_action_classic : &blk_log_action;
Shaohua Lica1136c2017-07-12 11:49:53 -07001489 has_cg = t->action & __BLK_TA_CGROUP;
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001490
Shaohua Lica1136c2017-07-12 11:49:53 -07001491 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
1492 log_action(iter, long_act ? "message" : "m", has_cg);
1493 blk_log_msg(s, iter->ent, has_cg);
Shaohua Lib7d76412016-05-09 17:22:13 -07001494 return trace_handle_return(s);
Li Zefan18cea452009-03-27 10:21:54 +08001495 }
1496
Li Zefaneb08f8e2009-03-24 16:05:27 +08001497 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001498 trace_seq_printf(s, "Unknown action %x\n", what);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001499 else {
Shaohua Lica1136c2017-07-12 11:49:53 -07001500 log_action(iter, what2act[what].act[long_act], has_cg);
1501 what2act[what].print(s, iter->ent, has_cg);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001502 }
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001503
1504 return trace_handle_return(s);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001505}
1506
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001507static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
Steven Rostedta9a57762010-04-22 18:46:14 -04001508 int flags, struct trace_event *event)
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001509{
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001510 return print_one_line(iter, false);
1511}
1512
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001513static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001514{
1515 struct trace_seq *s = &iter->seq;
1516 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1517 const int offset = offsetof(struct blk_io_trace, sector);
1518 struct blk_io_trace old = {
1519 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
Li Zefan6c051ce2009-03-25 17:18:56 +08001520 .time = iter->ts,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001521 };
1522
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001523 trace_seq_putmem(s, &old, offset);
1524 trace_seq_putmem(s, &t->sector,
1525 sizeof(old) - offset + t->pdu_len);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001526}
1527
Arnaldo Carvalho de Meloae7462b2009-02-03 22:05:50 -02001528static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001529blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1530 struct trace_event *event)
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001531{
Steven Rostedt (Red Hat)f4a1d082014-11-12 11:35:48 -05001532 blk_trace_synthesize_old_trace(iter);
1533
1534 return trace_handle_return(&iter->seq);
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001535}
1536
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001537static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1538{
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001539 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1540 return TRACE_TYPE_UNHANDLED;
1541
Li Zefanb6a4b0c2009-03-27 10:21:23 +08001542 return print_one_line(iter, true);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001543}
1544
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05001545static int
1546blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Li Zefanf3948f82009-04-15 11:02:56 +08001547{
1548 /* don't output context-info for blk_classic output */
1549 if (bit == TRACE_BLK_OPT_CLASSIC) {
1550 if (set)
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001551 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001552 else
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001553 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
Li Zefanf3948f82009-04-15 11:02:56 +08001554 }
1555 return 0;
1556}
1557
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001558static struct tracer blk_tracer __read_mostly = {
1559 .name = "blk",
1560 .init = blk_tracer_init,
1561 .reset = blk_tracer_reset,
1562 .start = blk_tracer_start,
1563 .stop = blk_tracer_stop,
1564 .print_header = blk_tracer_print_header,
1565 .print_line = blk_tracer_print_line,
1566 .flags = &blk_tracer_flags,
Li Zefanf3948f82009-04-15 11:02:56 +08001567 .set_flag = blk_tracer_set_flag,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001568};
1569
Steven Rostedta9a57762010-04-22 18:46:14 -04001570static struct trace_event_functions trace_blk_event_funcs = {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001571 .trace = blk_trace_event_print,
Arnaldo Carvalho de Melo08a06b82009-02-02 20:30:40 -02001572 .binary = blk_trace_event_print_binary,
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001573};
1574
Steven Rostedta9a57762010-04-22 18:46:14 -04001575static struct trace_event trace_blk_event = {
1576 .type = TRACE_BLK,
1577 .funcs = &trace_blk_event_funcs,
1578};
1579
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001580static int __init init_blk_tracer(void)
1581{
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001582 if (!register_trace_event(&trace_blk_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001583 pr_warn("Warning: could not register block events\n");
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001584 return 1;
1585 }
1586
1587 if (register_tracer(&blk_tracer) != 0) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001588 pr_warn("Warning: could not register the block tracer\n");
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001589 unregister_trace_event(&trace_blk_event);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001590 return 1;
1591 }
1592
1593 return 0;
1594}
1595
1596device_initcall(init_blk_tracer);
1597
1598static int blk_trace_remove_queue(struct request_queue *q)
1599{
1600 struct blk_trace *bt;
1601
Jan Karac3dbe542020-06-05 16:58:37 +02001602 bt = rcu_replace_pointer(q->blk_trace, NULL,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001603 lockdep_is_held(&q->debugfs_mutex));
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001604 if (bt == NULL)
1605 return -EINVAL;
1606
Jens Axboea6da0022017-11-05 09:16:09 -07001607 put_probe_ref();
Jan Karac780e862020-02-06 15:28:12 +01001608 synchronize_rcu();
Li Zefanad5dd542009-03-27 10:20:24 +08001609 blk_trace_free(bt);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001610 return 0;
1611}
1612
1613/*
1614 * Setup everything required to start tracing
1615 */
Li Zefan9908c302009-04-14 13:59:34 +08001616static int blk_trace_setup_queue(struct request_queue *q,
1617 struct block_device *bdev)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001618{
Davidlohr Buesocdea01b2015-10-30 05:25:59 +09001619 struct blk_trace *bt = NULL;
Li Zefan18cea452009-03-27 10:21:54 +08001620 int ret = -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001621
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001622 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1623 if (!bt)
Li Zefan15152e42009-03-20 09:49:08 +08001624 return -ENOMEM;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001625
Li Zefan18cea452009-03-27 10:21:54 +08001626 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1627 if (!bt->msg_data)
1628 goto free_bt;
1629
Li Zefan9908c302009-04-14 13:59:34 +08001630 bt->dev = bdev->bd_dev;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001631 bt->act_mask = (u16)-1;
Li Zefan9908c302009-04-14 13:59:34 +08001632
1633 blk_trace_setup_lba(bt, bdev);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001634
Jan Karac3dbe542020-06-05 16:58:37 +02001635 rcu_assign_pointer(q->blk_trace, bt);
Jens Axboea6da0022017-11-05 09:16:09 -07001636 get_probe_ref();
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001637 return 0;
Li Zefan18cea452009-03-27 10:21:54 +08001638
1639free_bt:
1640 blk_trace_free(bt);
1641 return ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001642}
1643
1644/*
1645 * sysfs interface to enable and configure tracing
1646 */
1647
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001648static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1649 struct device_attribute *attr,
1650 char *buf);
1651static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1652 struct device_attribute *attr,
1653 const char *buf, size_t count);
1654#define BLK_TRACE_DEVICE_ATTR(_name) \
1655 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1656 sysfs_blk_trace_attr_show, \
1657 sysfs_blk_trace_attr_store)
1658
Li Zefancd649b82009-03-20 11:33:55 +08001659static BLK_TRACE_DEVICE_ATTR(enable);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001660static BLK_TRACE_DEVICE_ATTR(act_mask);
1661static BLK_TRACE_DEVICE_ATTR(pid);
1662static BLK_TRACE_DEVICE_ATTR(start_lba);
1663static BLK_TRACE_DEVICE_ATTR(end_lba);
1664
1665static struct attribute *blk_trace_attrs[] = {
1666 &dev_attr_enable.attr,
1667 &dev_attr_act_mask.attr,
1668 &dev_attr_pid.attr,
1669 &dev_attr_start_lba.attr,
1670 &dev_attr_end_lba.attr,
1671 NULL
1672};
1673
1674struct attribute_group blk_trace_attr_group = {
1675 .name = "trace",
1676 .attrs = blk_trace_attrs,
1677};
1678
Li Zefan09341992009-03-24 17:43:30 +08001679static const struct {
1680 int mask;
1681 const char *str;
1682} mask_maps[] = {
1683 { BLK_TC_READ, "read" },
1684 { BLK_TC_WRITE, "write" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001685 { BLK_TC_FLUSH, "flush" },
Li Zefan09341992009-03-24 17:43:30 +08001686 { BLK_TC_SYNC, "sync" },
1687 { BLK_TC_QUEUE, "queue" },
1688 { BLK_TC_REQUEUE, "requeue" },
1689 { BLK_TC_ISSUE, "issue" },
1690 { BLK_TC_COMPLETE, "complete" },
1691 { BLK_TC_FS, "fs" },
1692 { BLK_TC_PC, "pc" },
Shaohua Li8d1547e2016-05-09 17:22:14 -07001693 { BLK_TC_NOTIFY, "notify" },
Li Zefan09341992009-03-24 17:43:30 +08001694 { BLK_TC_AHEAD, "ahead" },
1695 { BLK_TC_META, "meta" },
1696 { BLK_TC_DISCARD, "discard" },
1697 { BLK_TC_DRV_DATA, "drv_data" },
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001698 { BLK_TC_FUA, "fua" },
Li Zefan09341992009-03-24 17:43:30 +08001699};
1700
1701static int blk_trace_str2mask(const char *str)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001702{
Li Zefan09341992009-03-24 17:43:30 +08001703 int i;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001704 int mask = 0;
Li Zefan9eb85122009-04-09 11:19:40 +08001705 char *buf, *s, *token;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001706
Li Zefan9eb85122009-04-09 11:19:40 +08001707 buf = kstrdup(str, GFP_KERNEL);
1708 if (buf == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001709 return -ENOMEM;
Li Zefan9eb85122009-04-09 11:19:40 +08001710 s = strstrip(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001711
1712 while (1) {
Li Zefan09341992009-03-24 17:43:30 +08001713 token = strsep(&s, ",");
1714 if (token == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001715 break;
1716
Li Zefan09341992009-03-24 17:43:30 +08001717 if (*token == '\0')
1718 continue;
1719
1720 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1721 if (strcasecmp(token, mask_maps[i].str) == 0) {
1722 mask |= mask_maps[i].mask;
1723 break;
1724 }
1725 }
1726 if (i == ARRAY_SIZE(mask_maps)) {
1727 mask = -EINVAL;
1728 break;
1729 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001730 }
Li Zefan9eb85122009-04-09 11:19:40 +08001731 kfree(buf);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001732
1733 return mask;
1734}
1735
Li Zefan09341992009-03-24 17:43:30 +08001736static ssize_t blk_trace_mask2str(char *buf, int mask)
1737{
1738 int i;
1739 char *p = buf;
1740
1741 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1742 if (mask & mask_maps[i].mask) {
1743 p += sprintf(p, "%s%s",
1744 (p == buf) ? "" : ",", mask_maps[i].str);
1745 }
1746 }
1747 *p++ = '\n';
1748
1749 return p - buf;
1750}
1751
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001752static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1753 struct device_attribute *attr,
1754 char *buf)
1755{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001756 struct block_device *bdev = dev_to_bdev(dev);
1757 struct request_queue *q = bdev_get_queue(bdev);
Jan Karac780e862020-02-06 15:28:12 +01001758 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001759 ssize_t ret = -ENXIO;
1760
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001761 mutex_lock(&q->debugfs_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001762
Jan Karac780e862020-02-06 15:28:12 +01001763 bt = rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001764 lockdep_is_held(&q->debugfs_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001765 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001766 ret = sprintf(buf, "%u\n", !!bt);
Li Zefancd649b82009-03-20 11:33:55 +08001767 goto out_unlock_bdev;
1768 }
1769
Jan Karac780e862020-02-06 15:28:12 +01001770 if (bt == NULL)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001771 ret = sprintf(buf, "disabled\n");
1772 else if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001773 ret = blk_trace_mask2str(buf, bt->act_mask);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001774 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001775 ret = sprintf(buf, "%u\n", bt->pid);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001776 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001777 ret = sprintf(buf, "%llu\n", bt->start_lba);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001778 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001779 ret = sprintf(buf, "%llu\n", bt->end_lba);
Li Zefancd649b82009-03-20 11:33:55 +08001780
1781out_unlock_bdev:
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001782 mutex_unlock(&q->debugfs_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001783 return ret;
1784}
1785
1786static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1787 struct device_attribute *attr,
1788 const char *buf, size_t count)
1789{
Christoph Hellwig0d021292020-11-27 16:43:51 +01001790 struct block_device *bdev = dev_to_bdev(dev);
1791 struct request_queue *q = bdev_get_queue(bdev);
Jan Karac780e862020-02-06 15:28:12 +01001792 struct blk_trace *bt;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001793 u64 value;
Li Zefan09341992009-03-24 17:43:30 +08001794 ssize_t ret = -EINVAL;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001795
1796 if (count == 0)
1797 goto out;
1798
1799 if (attr == &dev_attr_act_mask) {
Shaohua Li5f339452017-05-19 08:04:59 -07001800 if (kstrtoull(buf, 0, &value)) {
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001801 /* Assume it is a list of trace category names */
Li Zefan09341992009-03-24 17:43:30 +08001802 ret = blk_trace_str2mask(buf);
1803 if (ret < 0)
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001804 goto out;
Li Zefan09341992009-03-24 17:43:30 +08001805 value = ret;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001806 }
Christoph Hellwig0d021292020-11-27 16:43:51 +01001807 } else {
1808 if (kstrtoull(buf, 0, &value))
1809 goto out;
1810 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001811
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001812 mutex_lock(&q->debugfs_mutex);
Li Zefancd649b82009-03-20 11:33:55 +08001813
Jan Karac780e862020-02-06 15:28:12 +01001814 bt = rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001815 lockdep_is_held(&q->debugfs_mutex));
Li Zefancd649b82009-03-20 11:33:55 +08001816 if (attr == &dev_attr_enable) {
Jan Karac780e862020-02-06 15:28:12 +01001817 if (!!value == !!bt) {
Steven Rostedt (VMware)757d9142018-08-16 16:08:37 -04001818 ret = 0;
1819 goto out_unlock_bdev;
1820 }
Li Zefancd649b82009-03-20 11:33:55 +08001821 if (value)
Li Zefan9908c302009-04-14 13:59:34 +08001822 ret = blk_trace_setup_queue(q, bdev);
Li Zefancd649b82009-03-20 11:33:55 +08001823 else
1824 ret = blk_trace_remove_queue(q);
1825 goto out_unlock_bdev;
1826 }
1827
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001828 ret = 0;
Cengiz Can153031a2020-03-04 13:58:19 +03001829 if (bt == NULL) {
Li Zefan9908c302009-04-14 13:59:34 +08001830 ret = blk_trace_setup_queue(q, bdev);
Cengiz Can153031a2020-03-04 13:58:19 +03001831 bt = rcu_dereference_protected(q->blk_trace,
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001832 lockdep_is_held(&q->debugfs_mutex));
Cengiz Can153031a2020-03-04 13:58:19 +03001833 }
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001834
1835 if (ret == 0) {
1836 if (attr == &dev_attr_act_mask)
Jan Karac780e862020-02-06 15:28:12 +01001837 bt->act_mask = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001838 else if (attr == &dev_attr_pid)
Jan Karac780e862020-02-06 15:28:12 +01001839 bt->pid = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001840 else if (attr == &dev_attr_start_lba)
Jan Karac780e862020-02-06 15:28:12 +01001841 bt->start_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001842 else if (attr == &dev_attr_end_lba)
Jan Karac780e862020-02-06 15:28:12 +01001843 bt->end_lba = value;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001844 }
Li Zefancd649b82009-03-20 11:33:55 +08001845
1846out_unlock_bdev:
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +00001847 mutex_unlock(&q->debugfs_mutex);
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001848out:
Li Zefancd649b82009-03-20 11:33:55 +08001849 return ret ? ret : count;
Arnaldo Carvalho de Meloc71a8962009-01-23 12:06:27 -02001850}
Li Zefancd649b82009-03-20 11:33:55 +08001851
Li Zefan1d54ad62009-04-14 14:00:05 +08001852int blk_trace_init_sysfs(struct device *dev)
1853{
1854 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1855}
1856
Zdenek Kabelac48c0d4d2009-09-25 06:19:26 +02001857void blk_trace_remove_sysfs(struct device *dev)
1858{
1859 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1860}
1861
Li Zefan55782132009-06-09 13:43:05 +08001862#endif /* CONFIG_BLK_DEV_IO_TRACE */
1863
1864#ifdef CONFIG_EVENT_TRACING
1865
Chaitanya Kulkarni1f83bb42021-02-21 21:29:56 -08001866/**
1867 * blk_fill_rwbs - Fill the buffer rwbs by mapping op to character string.
Chaitanya Kulkarni94d4bff2021-02-23 23:16:23 -08001868 * @rwbs: buffer to be filled
Chaitanya Kulkarni1f83bb42021-02-21 21:29:56 -08001869 * @op: REQ_OP_XXX for the tracepoint
1870 *
1871 * Description:
1872 * Maps the REQ_OP_XXX to character and fills the buffer provided by the
1873 * caller with resulting string.
1874 *
1875 **/
Chaitanya Kulkarni179d1602021-02-21 21:29:55 -08001876void blk_fill_rwbs(char *rwbs, unsigned int op)
Li Zefan55782132009-06-09 13:43:05 +08001877{
1878 int i = 0;
1879
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001880 if (op & REQ_PREFLUSH)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001881 rwbs[i++] = 'F';
1882
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001883 switch (op & REQ_OP_MASK) {
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001884 case REQ_OP_WRITE:
1885 case REQ_OP_WRITE_SAME:
Li Zefan55782132009-06-09 13:43:05 +08001886 rwbs[i++] = 'W';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001887 break;
1888 case REQ_OP_DISCARD:
Li Zefan55782132009-06-09 13:43:05 +08001889 rwbs[i++] = 'D';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001890 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001891 case REQ_OP_SECURE_ERASE:
1892 rwbs[i++] = 'D';
1893 rwbs[i++] = 'E';
1894 break;
Mike Christie3a5e02c2016-06-05 14:32:23 -05001895 case REQ_OP_FLUSH:
1896 rwbs[i++] = 'F';
1897 break;
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001898 case REQ_OP_READ:
Li Zefan55782132009-06-09 13:43:05 +08001899 rwbs[i++] = 'R';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001900 break;
1901 default:
Li Zefan55782132009-06-09 13:43:05 +08001902 rwbs[i++] = 'N';
Mike Christie1b9a9ab2016-06-05 14:32:18 -05001903 }
Li Zefan55782132009-06-09 13:43:05 +08001904
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001905 if (op & REQ_FUA)
Namhyung Kimc09c47c2011-08-11 10:36:05 +02001906 rwbs[i++] = 'F';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001907 if (op & REQ_RAHEAD)
Li Zefan55782132009-06-09 13:43:05 +08001908 rwbs[i++] = 'A';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001909 if (op & REQ_SYNC)
Li Zefan55782132009-06-09 13:43:05 +08001910 rwbs[i++] = 'S';
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001911 if (op & REQ_META)
Li Zefan55782132009-06-09 13:43:05 +08001912 rwbs[i++] = 'M';
1913
1914 rwbs[i] = '\0';
1915}
Kent Overstreet9ca8f8e2012-04-13 21:56:43 -04001916EXPORT_SYMBOL_GPL(blk_fill_rwbs);
Li Zefan55782132009-06-09 13:43:05 +08001917
Li Zefan55782132009-06-09 13:43:05 +08001918#endif /* CONFIG_EVENT_TRACING */
1919