blktrace.c 43 KB

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