blktrace.c 42 KB

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