blktrace.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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/debugfs.h>
  25. #include <linux/time.h>
  26. #include <trace/block.h>
  27. #include <linux/uaccess.h>
  28. #include "trace_output.h"
  29. static unsigned int blktrace_seq __read_mostly = 1;
  30. static struct trace_array *blk_tr;
  31. static bool blk_tracer_enabled __read_mostly;
  32. /* Select an alternative, minimalistic output than the original one */
  33. #define TRACE_BLK_OPT_CLASSIC 0x1
  34. static struct tracer_opt blk_tracer_opts[] = {
  35. /* Default disable the minimalistic output */
  36. { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
  37. { }
  38. };
  39. static struct tracer_flags blk_tracer_flags = {
  40. .val = 0,
  41. .opts = blk_tracer_opts,
  42. };
  43. /* Global reference count of probes */
  44. static atomic_t blk_probes_ref = ATOMIC_INIT(0);
  45. static void blk_register_tracepoints(void);
  46. static void blk_unregister_tracepoints(void);
  47. /*
  48. * Send out a notify message.
  49. */
  50. static void trace_note(struct blk_trace *bt, pid_t pid, int action,
  51. const void *data, size_t len)
  52. {
  53. struct blk_io_trace *t;
  54. if (!bt->rchan)
  55. return;
  56. t = relay_reserve(bt->rchan, sizeof(*t) + len);
  57. if (t) {
  58. const int cpu = smp_processor_id();
  59. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  60. t->time = ktime_to_ns(ktime_get());
  61. t->device = bt->dev;
  62. t->action = action;
  63. t->pid = pid;
  64. t->cpu = cpu;
  65. t->pdu_len = len;
  66. memcpy((void *) t + sizeof(*t), data, len);
  67. }
  68. }
  69. /*
  70. * Send out a notify for this process, if we haven't done so since a trace
  71. * started
  72. */
  73. static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
  74. {
  75. tsk->btrace_seq = blktrace_seq;
  76. trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
  77. }
  78. static void trace_note_time(struct blk_trace *bt)
  79. {
  80. struct timespec now;
  81. unsigned long flags;
  82. u32 words[2];
  83. getnstimeofday(&now);
  84. words[0] = now.tv_sec;
  85. words[1] = now.tv_nsec;
  86. local_irq_save(flags);
  87. trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
  88. local_irq_restore(flags);
  89. }
  90. void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
  91. {
  92. int n;
  93. va_list args;
  94. unsigned long flags;
  95. char *buf;
  96. if (blk_tr) {
  97. va_start(args, fmt);
  98. ftrace_vprintk(fmt, args);
  99. va_end(args);
  100. return;
  101. }
  102. if (!bt->msg_data)
  103. return;
  104. local_irq_save(flags);
  105. buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
  106. va_start(args, fmt);
  107. n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
  108. va_end(args);
  109. trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
  110. local_irq_restore(flags);
  111. }
  112. EXPORT_SYMBOL_GPL(__trace_note_message);
  113. static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
  114. pid_t pid)
  115. {
  116. if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
  117. return 1;
  118. if (sector < bt->start_lba || sector > bt->end_lba)
  119. return 1;
  120. if (bt->pid && pid != bt->pid)
  121. return 1;
  122. return 0;
  123. }
  124. /*
  125. * Data direction bit lookup
  126. */
  127. static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ),
  128. BLK_TC_ACT(BLK_TC_WRITE) };
  129. /* The ilog2() calls fall out because they're constant */
  130. #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
  131. (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
  132. /*
  133. * The worker for the various blk_add_trace*() types. Fills out a
  134. * blk_io_trace structure and places it in a per-cpu subbuffer.
  135. */
  136. static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
  137. int rw, u32 what, int error, int pdu_len, void *pdu_data)
  138. {
  139. struct task_struct *tsk = current;
  140. struct ring_buffer_event *event = NULL;
  141. struct blk_io_trace *t;
  142. unsigned long flags = 0;
  143. unsigned long *sequence;
  144. pid_t pid;
  145. int cpu, pc = 0;
  146. if (unlikely(bt->trace_state != Blktrace_running ||
  147. !blk_tracer_enabled))
  148. return;
  149. what |= ddir_act[rw & WRITE];
  150. what |= MASK_TC_BIT(rw, BARRIER);
  151. what |= MASK_TC_BIT(rw, SYNCIO);
  152. what |= MASK_TC_BIT(rw, AHEAD);
  153. what |= MASK_TC_BIT(rw, META);
  154. what |= MASK_TC_BIT(rw, DISCARD);
  155. pid = tsk->pid;
  156. if (unlikely(act_log_check(bt, what, sector, pid)))
  157. return;
  158. cpu = raw_smp_processor_id();
  159. if (blk_tr) {
  160. tracing_record_cmdline(current);
  161. pc = preempt_count();
  162. event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
  163. sizeof(*t) + pdu_len,
  164. 0, pc);
  165. if (!event)
  166. return;
  167. t = ring_buffer_event_data(event);
  168. goto record_it;
  169. }
  170. /*
  171. * A word about the locking here - we disable interrupts to reserve
  172. * some space in the relay per-cpu buffer, to prevent an irq
  173. * from coming in and stepping on our toes.
  174. */
  175. local_irq_save(flags);
  176. if (unlikely(tsk->btrace_seq != blktrace_seq))
  177. trace_note_tsk(bt, tsk);
  178. t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
  179. if (t) {
  180. sequence = per_cpu_ptr(bt->sequence, cpu);
  181. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  182. t->sequence = ++(*sequence);
  183. t->time = ktime_to_ns(ktime_get());
  184. record_it:
  185. /*
  186. * These two are not needed in ftrace as they are in the
  187. * generic trace_entry, filled by tracing_generic_entry_update,
  188. * but for the trace_event->bin() synthesizer benefit we do it
  189. * here too.
  190. */
  191. t->cpu = cpu;
  192. t->pid = pid;
  193. t->sector = sector;
  194. t->bytes = bytes;
  195. t->action = what;
  196. t->device = bt->dev;
  197. t->error = error;
  198. t->pdu_len = pdu_len;
  199. if (pdu_len)
  200. memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
  201. if (blk_tr) {
  202. trace_buffer_unlock_commit(blk_tr, event, 0, pc);
  203. return;
  204. }
  205. }
  206. local_irq_restore(flags);
  207. }
  208. static struct dentry *blk_tree_root;
  209. static DEFINE_MUTEX(blk_tree_mutex);
  210. static void blk_trace_cleanup(struct blk_trace *bt)
  211. {
  212. debugfs_remove(bt->msg_file);
  213. debugfs_remove(bt->dropped_file);
  214. relay_close(bt->rchan);
  215. free_percpu(bt->sequence);
  216. free_percpu(bt->msg_data);
  217. kfree(bt);
  218. if (atomic_dec_and_test(&blk_probes_ref))
  219. blk_unregister_tracepoints();
  220. }
  221. int blk_trace_remove(struct request_queue *q)
  222. {
  223. struct blk_trace *bt;
  224. bt = xchg(&q->blk_trace, NULL);
  225. if (!bt)
  226. return -EINVAL;
  227. if (bt->trace_state == Blktrace_setup ||
  228. bt->trace_state == Blktrace_stopped)
  229. blk_trace_cleanup(bt);
  230. return 0;
  231. }
  232. EXPORT_SYMBOL_GPL(blk_trace_remove);
  233. static int blk_dropped_open(struct inode *inode, struct file *filp)
  234. {
  235. filp->private_data = inode->i_private;
  236. return 0;
  237. }
  238. static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
  239. size_t count, loff_t *ppos)
  240. {
  241. struct blk_trace *bt = filp->private_data;
  242. char buf[16];
  243. snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
  244. return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  245. }
  246. static const struct file_operations blk_dropped_fops = {
  247. .owner = THIS_MODULE,
  248. .open = blk_dropped_open,
  249. .read = blk_dropped_read,
  250. };
  251. static int blk_msg_open(struct inode *inode, struct file *filp)
  252. {
  253. filp->private_data = inode->i_private;
  254. return 0;
  255. }
  256. static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
  257. size_t count, loff_t *ppos)
  258. {
  259. char *msg;
  260. struct blk_trace *bt;
  261. if (count > BLK_TN_MAX_MSG)
  262. return -EINVAL;
  263. msg = kmalloc(count, GFP_KERNEL);
  264. if (msg == NULL)
  265. return -ENOMEM;
  266. if (copy_from_user(msg, buffer, count)) {
  267. kfree(msg);
  268. return -EFAULT;
  269. }
  270. bt = filp->private_data;
  271. __trace_note_message(bt, "%s", msg);
  272. kfree(msg);
  273. return count;
  274. }
  275. static const struct file_operations blk_msg_fops = {
  276. .owner = THIS_MODULE,
  277. .open = blk_msg_open,
  278. .write = blk_msg_write,
  279. };
  280. /*
  281. * Keep track of how many times we encountered a full subbuffer, to aid
  282. * the user space app in telling how many lost events there were.
  283. */
  284. static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
  285. void *prev_subbuf, size_t prev_padding)
  286. {
  287. struct blk_trace *bt;
  288. if (!relay_buf_full(buf))
  289. return 1;
  290. bt = buf->chan->private_data;
  291. atomic_inc(&bt->dropped);
  292. return 0;
  293. }
  294. static int blk_remove_buf_file_callback(struct dentry *dentry)
  295. {
  296. struct dentry *parent = dentry->d_parent;
  297. debugfs_remove(dentry);
  298. /*
  299. * this will fail for all but the last file, but that is ok. what we
  300. * care about is the top level buts->name directory going away, when
  301. * the last trace file is gone. Then we don't have to rmdir() that
  302. * manually on trace stop, so it nicely solves the issue with
  303. * force killing of running traces.
  304. */
  305. debugfs_remove(parent);
  306. return 0;
  307. }
  308. static struct dentry *blk_create_buf_file_callback(const char *filename,
  309. struct dentry *parent,
  310. int mode,
  311. struct rchan_buf *buf,
  312. int *is_global)
  313. {
  314. return debugfs_create_file(filename, mode, parent, buf,
  315. &relay_file_operations);
  316. }
  317. static struct rchan_callbacks blk_relay_callbacks = {
  318. .subbuf_start = blk_subbuf_start_callback,
  319. .create_buf_file = blk_create_buf_file_callback,
  320. .remove_buf_file = blk_remove_buf_file_callback,
  321. };
  322. /*
  323. * Setup everything required to start tracing
  324. */
  325. int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  326. struct blk_user_trace_setup *buts)
  327. {
  328. struct blk_trace *old_bt, *bt = NULL;
  329. struct dentry *dir = NULL;
  330. int ret, i;
  331. if (!buts->buf_size || !buts->buf_nr)
  332. return -EINVAL;
  333. strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
  334. buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
  335. /*
  336. * some device names have larger paths - convert the slashes
  337. * to underscores for this to work as expected
  338. */
  339. for (i = 0; i < strlen(buts->name); i++)
  340. if (buts->name[i] == '/')
  341. buts->name[i] = '_';
  342. ret = -ENOMEM;
  343. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  344. if (!bt)
  345. goto err;
  346. bt->sequence = alloc_percpu(unsigned long);
  347. if (!bt->sequence)
  348. goto err;
  349. bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
  350. if (!bt->msg_data)
  351. goto err;
  352. ret = -ENOENT;
  353. if (!blk_tree_root) {
  354. blk_tree_root = debugfs_create_dir("block", NULL);
  355. if (!blk_tree_root)
  356. goto err;
  357. }
  358. dir = debugfs_create_dir(buts->name, blk_tree_root);
  359. if (!dir)
  360. goto err;
  361. bt->dir = dir;
  362. bt->dev = dev;
  363. atomic_set(&bt->dropped, 0);
  364. ret = -EIO;
  365. bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
  366. &blk_dropped_fops);
  367. if (!bt->dropped_file)
  368. goto err;
  369. bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
  370. if (!bt->msg_file)
  371. goto err;
  372. bt->rchan = relay_open("trace", dir, buts->buf_size,
  373. buts->buf_nr, &blk_relay_callbacks, bt);
  374. if (!bt->rchan)
  375. goto err;
  376. bt->act_mask = buts->act_mask;
  377. if (!bt->act_mask)
  378. bt->act_mask = (u16) -1;
  379. bt->start_lba = buts->start_lba;
  380. bt->end_lba = buts->end_lba;
  381. if (!bt->end_lba)
  382. bt->end_lba = -1ULL;
  383. bt->pid = buts->pid;
  384. bt->trace_state = Blktrace_setup;
  385. ret = -EBUSY;
  386. old_bt = xchg(&q->blk_trace, bt);
  387. if (old_bt) {
  388. (void) xchg(&q->blk_trace, old_bt);
  389. goto err;
  390. }
  391. if (atomic_add_return(1, &blk_probes_ref) == 1)
  392. blk_register_tracepoints();
  393. return 0;
  394. err:
  395. if (bt) {
  396. if (bt->msg_file)
  397. debugfs_remove(bt->msg_file);
  398. if (bt->dropped_file)
  399. debugfs_remove(bt->dropped_file);
  400. free_percpu(bt->sequence);
  401. free_percpu(bt->msg_data);
  402. if (bt->rchan)
  403. relay_close(bt->rchan);
  404. kfree(bt);
  405. }
  406. return ret;
  407. }
  408. int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  409. char __user *arg)
  410. {
  411. struct blk_user_trace_setup buts;
  412. int ret;
  413. ret = copy_from_user(&buts, arg, sizeof(buts));
  414. if (ret)
  415. return -EFAULT;
  416. ret = do_blk_trace_setup(q, name, dev, &buts);
  417. if (ret)
  418. return ret;
  419. if (copy_to_user(arg, &buts, sizeof(buts)))
  420. return -EFAULT;
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(blk_trace_setup);
  424. int blk_trace_startstop(struct request_queue *q, int start)
  425. {
  426. int ret;
  427. struct blk_trace *bt = q->blk_trace;
  428. if (bt == NULL)
  429. return -EINVAL;
  430. /*
  431. * For starting a trace, we can transition from a setup or stopped
  432. * trace. For stopping a trace, the state must be running
  433. */
  434. ret = -EINVAL;
  435. if (start) {
  436. if (bt->trace_state == Blktrace_setup ||
  437. bt->trace_state == Blktrace_stopped) {
  438. blktrace_seq++;
  439. smp_mb();
  440. bt->trace_state = Blktrace_running;
  441. trace_note_time(bt);
  442. ret = 0;
  443. }
  444. } else {
  445. if (bt->trace_state == Blktrace_running) {
  446. bt->trace_state = Blktrace_stopped;
  447. relay_flush(bt->rchan);
  448. ret = 0;
  449. }
  450. }
  451. return ret;
  452. }
  453. EXPORT_SYMBOL_GPL(blk_trace_startstop);
  454. /**
  455. * blk_trace_ioctl: - handle the ioctls associated with tracing
  456. * @bdev: the block device
  457. * @cmd: the ioctl cmd
  458. * @arg: the argument data, if any
  459. *
  460. **/
  461. int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
  462. {
  463. struct request_queue *q;
  464. int ret, start = 0;
  465. char b[BDEVNAME_SIZE];
  466. q = bdev_get_queue(bdev);
  467. if (!q)
  468. return -ENXIO;
  469. mutex_lock(&bdev->bd_mutex);
  470. switch (cmd) {
  471. case BLKTRACESETUP:
  472. bdevname(bdev, b);
  473. ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
  474. break;
  475. case BLKTRACESTART:
  476. start = 1;
  477. case BLKTRACESTOP:
  478. ret = blk_trace_startstop(q, start);
  479. break;
  480. case BLKTRACETEARDOWN:
  481. ret = blk_trace_remove(q);
  482. break;
  483. default:
  484. ret = -ENOTTY;
  485. break;
  486. }
  487. mutex_unlock(&bdev->bd_mutex);
  488. return ret;
  489. }
  490. /**
  491. * blk_trace_shutdown: - stop and cleanup trace structures
  492. * @q: the request queue associated with the device
  493. *
  494. **/
  495. void blk_trace_shutdown(struct request_queue *q)
  496. {
  497. if (q->blk_trace) {
  498. blk_trace_startstop(q, 0);
  499. blk_trace_remove(q);
  500. }
  501. }
  502. /*
  503. * blktrace probes
  504. */
  505. /**
  506. * blk_add_trace_rq - Add a trace for a request oriented action
  507. * @q: queue the io is for
  508. * @rq: the source request
  509. * @what: the action
  510. *
  511. * Description:
  512. * Records an action against a request. Will log the bio offset + size.
  513. *
  514. **/
  515. static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
  516. u32 what)
  517. {
  518. struct blk_trace *bt = q->blk_trace;
  519. int rw = rq->cmd_flags & 0x03;
  520. if (likely(!bt))
  521. return;
  522. if (blk_discard_rq(rq))
  523. rw |= (1 << BIO_RW_DISCARD);
  524. if (blk_pc_request(rq)) {
  525. what |= BLK_TC_ACT(BLK_TC_PC);
  526. __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
  527. sizeof(rq->cmd), rq->cmd);
  528. } else {
  529. what |= BLK_TC_ACT(BLK_TC_FS);
  530. __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
  531. rw, what, rq->errors, 0, NULL);
  532. }
  533. }
  534. static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
  535. {
  536. blk_add_trace_rq(q, rq, BLK_TA_ABORT);
  537. }
  538. static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
  539. {
  540. blk_add_trace_rq(q, rq, BLK_TA_INSERT);
  541. }
  542. static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
  543. {
  544. blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
  545. }
  546. static void blk_add_trace_rq_requeue(struct request_queue *q,
  547. struct request *rq)
  548. {
  549. blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
  550. }
  551. static void blk_add_trace_rq_complete(struct request_queue *q,
  552. struct request *rq)
  553. {
  554. blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
  555. }
  556. /**
  557. * blk_add_trace_bio - Add a trace for a bio oriented action
  558. * @q: queue the io is for
  559. * @bio: the source bio
  560. * @what: the action
  561. *
  562. * Description:
  563. * Records an action against a bio. Will log the bio offset + size.
  564. *
  565. **/
  566. static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
  567. u32 what)
  568. {
  569. struct blk_trace *bt = q->blk_trace;
  570. if (likely(!bt))
  571. return;
  572. __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
  573. !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
  574. }
  575. static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
  576. {
  577. blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
  578. }
  579. static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
  580. {
  581. blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
  582. }
  583. static void blk_add_trace_bio_backmerge(struct request_queue *q,
  584. struct bio *bio)
  585. {
  586. blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
  587. }
  588. static void blk_add_trace_bio_frontmerge(struct request_queue *q,
  589. struct bio *bio)
  590. {
  591. blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
  592. }
  593. static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
  594. {
  595. blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
  596. }
  597. static void blk_add_trace_getrq(struct request_queue *q,
  598. struct bio *bio, int rw)
  599. {
  600. if (bio)
  601. blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
  602. else {
  603. struct blk_trace *bt = q->blk_trace;
  604. if (bt)
  605. __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
  606. }
  607. }
  608. static void blk_add_trace_sleeprq(struct request_queue *q,
  609. struct bio *bio, int rw)
  610. {
  611. if (bio)
  612. blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
  613. else {
  614. struct blk_trace *bt = q->blk_trace;
  615. if (bt)
  616. __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
  617. 0, 0, NULL);
  618. }
  619. }
  620. static void blk_add_trace_plug(struct request_queue *q)
  621. {
  622. struct blk_trace *bt = q->blk_trace;
  623. if (bt)
  624. __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
  625. }
  626. static void blk_add_trace_unplug_io(struct request_queue *q)
  627. {
  628. struct blk_trace *bt = q->blk_trace;
  629. if (bt) {
  630. unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
  631. __be64 rpdu = cpu_to_be64(pdu);
  632. __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
  633. sizeof(rpdu), &rpdu);
  634. }
  635. }
  636. static void blk_add_trace_unplug_timer(struct request_queue *q)
  637. {
  638. struct blk_trace *bt = q->blk_trace;
  639. if (bt) {
  640. unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
  641. __be64 rpdu = cpu_to_be64(pdu);
  642. __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
  643. sizeof(rpdu), &rpdu);
  644. }
  645. }
  646. static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
  647. unsigned int pdu)
  648. {
  649. struct blk_trace *bt = q->blk_trace;
  650. if (bt) {
  651. __be64 rpdu = cpu_to_be64(pdu);
  652. __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
  653. BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
  654. sizeof(rpdu), &rpdu);
  655. }
  656. }
  657. /**
  658. * blk_add_trace_remap - Add a trace for a remap operation
  659. * @q: queue the io is for
  660. * @bio: the source bio
  661. * @dev: target device
  662. * @from: source sector
  663. * @to: target sector
  664. *
  665. * Description:
  666. * Device mapper or raid target sometimes need to split a bio because
  667. * it spans a stripe (or similar). Add a trace for that action.
  668. *
  669. **/
  670. static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
  671. dev_t dev, sector_t from, sector_t to)
  672. {
  673. struct blk_trace *bt = q->blk_trace;
  674. struct blk_io_trace_remap r;
  675. if (likely(!bt))
  676. return;
  677. r.device = cpu_to_be32(dev);
  678. r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
  679. r.sector = cpu_to_be64(to);
  680. __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
  681. !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
  682. }
  683. /**
  684. * blk_add_driver_data - Add binary message with driver-specific data
  685. * @q: queue the io is for
  686. * @rq: io request
  687. * @data: driver-specific data
  688. * @len: length of driver-specific data
  689. *
  690. * Description:
  691. * Some drivers might want to write driver-specific data per request.
  692. *
  693. **/
  694. void blk_add_driver_data(struct request_queue *q,
  695. struct request *rq,
  696. void *data, size_t len)
  697. {
  698. struct blk_trace *bt = q->blk_trace;
  699. if (likely(!bt))
  700. return;
  701. if (blk_pc_request(rq))
  702. __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
  703. rq->errors, len, data);
  704. else
  705. __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
  706. 0, BLK_TA_DRV_DATA, rq->errors, len, data);
  707. }
  708. EXPORT_SYMBOL_GPL(blk_add_driver_data);
  709. static void blk_register_tracepoints(void)
  710. {
  711. int ret;
  712. ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
  713. WARN_ON(ret);
  714. ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
  715. WARN_ON(ret);
  716. ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
  717. WARN_ON(ret);
  718. ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
  719. WARN_ON(ret);
  720. ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
  721. WARN_ON(ret);
  722. ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
  723. WARN_ON(ret);
  724. ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
  725. WARN_ON(ret);
  726. ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
  727. WARN_ON(ret);
  728. ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
  729. WARN_ON(ret);
  730. ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
  731. WARN_ON(ret);
  732. ret = register_trace_block_getrq(blk_add_trace_getrq);
  733. WARN_ON(ret);
  734. ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
  735. WARN_ON(ret);
  736. ret = register_trace_block_plug(blk_add_trace_plug);
  737. WARN_ON(ret);
  738. ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
  739. WARN_ON(ret);
  740. ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
  741. WARN_ON(ret);
  742. ret = register_trace_block_split(blk_add_trace_split);
  743. WARN_ON(ret);
  744. ret = register_trace_block_remap(blk_add_trace_remap);
  745. WARN_ON(ret);
  746. }
  747. static void blk_unregister_tracepoints(void)
  748. {
  749. unregister_trace_block_remap(blk_add_trace_remap);
  750. unregister_trace_block_split(blk_add_trace_split);
  751. unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
  752. unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
  753. unregister_trace_block_plug(blk_add_trace_plug);
  754. unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
  755. unregister_trace_block_getrq(blk_add_trace_getrq);
  756. unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
  757. unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
  758. unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
  759. unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
  760. unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
  761. unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
  762. unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
  763. unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
  764. unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
  765. unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
  766. tracepoint_synchronize_unregister();
  767. }
  768. /*
  769. * struct blk_io_tracer formatting routines
  770. */
  771. static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
  772. {
  773. int i = 0;
  774. if (t->action & BLK_TC_DISCARD)
  775. rwbs[i++] = 'D';
  776. else if (t->action & BLK_TC_WRITE)
  777. rwbs[i++] = 'W';
  778. else if (t->bytes)
  779. rwbs[i++] = 'R';
  780. else
  781. rwbs[i++] = 'N';
  782. if (t->action & BLK_TC_AHEAD)
  783. rwbs[i++] = 'A';
  784. if (t->action & BLK_TC_BARRIER)
  785. rwbs[i++] = 'B';
  786. if (t->action & BLK_TC_SYNC)
  787. rwbs[i++] = 'S';
  788. if (t->action & BLK_TC_META)
  789. rwbs[i++] = 'M';
  790. rwbs[i] = '\0';
  791. }
  792. static inline
  793. const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
  794. {
  795. return (const struct blk_io_trace *)ent;
  796. }
  797. static inline const void *pdu_start(const struct trace_entry *ent)
  798. {
  799. return te_blk_io_trace(ent) + 1;
  800. }
  801. static inline u32 t_sec(const struct trace_entry *ent)
  802. {
  803. return te_blk_io_trace(ent)->bytes >> 9;
  804. }
  805. static inline unsigned long long t_sector(const struct trace_entry *ent)
  806. {
  807. return te_blk_io_trace(ent)->sector;
  808. }
  809. static inline __u16 t_error(const struct trace_entry *ent)
  810. {
  811. return te_blk_io_trace(ent)->sector;
  812. }
  813. static __u64 get_pdu_int(const struct trace_entry *ent)
  814. {
  815. const __u64 *val = pdu_start(ent);
  816. return be64_to_cpu(*val);
  817. }
  818. static void get_pdu_remap(const struct trace_entry *ent,
  819. struct blk_io_trace_remap *r)
  820. {
  821. const struct blk_io_trace_remap *__r = pdu_start(ent);
  822. __u64 sector = __r->sector;
  823. r->device = be32_to_cpu(__r->device);
  824. r->device_from = be32_to_cpu(__r->device_from);
  825. r->sector = be64_to_cpu(sector);
  826. }
  827. static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
  828. {
  829. char rwbs[6];
  830. unsigned long long ts = ns2usecs(iter->ts);
  831. unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
  832. unsigned secs = (unsigned long)ts;
  833. const struct trace_entry *ent = iter->ent;
  834. const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
  835. fill_rwbs(rwbs, t);
  836. return trace_seq_printf(&iter->seq,
  837. "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
  838. MAJOR(t->device), MINOR(t->device), iter->cpu,
  839. secs, usec_rem, ent->pid, act, rwbs);
  840. }
  841. static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
  842. const char *act)
  843. {
  844. char rwbs[6];
  845. fill_rwbs(rwbs, t);
  846. return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
  847. MAJOR(t->device), MINOR(t->device), act, rwbs);
  848. }
  849. static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
  850. {
  851. char cmd[TASK_COMM_LEN];
  852. trace_find_cmdline(ent->pid, cmd);
  853. if (t_sec(ent))
  854. return trace_seq_printf(s, "%llu + %u [%s]\n",
  855. t_sector(ent), t_sec(ent), cmd);
  856. return trace_seq_printf(s, "[%s]\n", cmd);
  857. }
  858. static int blk_log_with_error(struct trace_seq *s,
  859. const struct trace_entry *ent)
  860. {
  861. if (t_sec(ent))
  862. return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
  863. t_sec(ent), t_error(ent));
  864. return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
  865. }
  866. static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
  867. {
  868. struct blk_io_trace_remap r = { .device = 0, };
  869. get_pdu_remap(ent, &r);
  870. return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
  871. t_sector(ent),
  872. t_sec(ent), MAJOR(r.device), MINOR(r.device),
  873. (unsigned long long)r.sector);
  874. }
  875. static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
  876. {
  877. char cmd[TASK_COMM_LEN];
  878. trace_find_cmdline(ent->pid, cmd);
  879. return trace_seq_printf(s, "[%s]\n", cmd);
  880. }
  881. static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
  882. {
  883. char cmd[TASK_COMM_LEN];
  884. trace_find_cmdline(ent->pid, cmd);
  885. return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
  886. }
  887. static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
  888. {
  889. char cmd[TASK_COMM_LEN];
  890. trace_find_cmdline(ent->pid, cmd);
  891. return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
  892. get_pdu_int(ent), cmd);
  893. }
  894. /*
  895. * struct tracer operations
  896. */
  897. static void blk_tracer_print_header(struct seq_file *m)
  898. {
  899. if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
  900. return;
  901. seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
  902. "# | | | | | |\n");
  903. }
  904. static void blk_tracer_start(struct trace_array *tr)
  905. {
  906. if (atomic_add_return(1, &blk_probes_ref) == 1)
  907. blk_register_tracepoints();
  908. trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
  909. }
  910. static int blk_tracer_init(struct trace_array *tr)
  911. {
  912. blk_tr = tr;
  913. blk_tracer_start(tr);
  914. blk_tracer_enabled = true;
  915. return 0;
  916. }
  917. static void blk_tracer_stop(struct trace_array *tr)
  918. {
  919. trace_flags |= TRACE_ITER_CONTEXT_INFO;
  920. if (atomic_dec_and_test(&blk_probes_ref))
  921. blk_unregister_tracepoints();
  922. }
  923. static void blk_tracer_reset(struct trace_array *tr)
  924. {
  925. if (!atomic_read(&blk_probes_ref))
  926. return;
  927. blk_tracer_enabled = false;
  928. blk_tracer_stop(tr);
  929. }
  930. static struct {
  931. const char *act[2];
  932. int (*print)(struct trace_seq *s, const struct trace_entry *ent);
  933. } what2act[] __read_mostly = {
  934. [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
  935. [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
  936. [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
  937. [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
  938. [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
  939. [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
  940. [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
  941. [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
  942. [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
  943. [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
  944. [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
  945. [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
  946. [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
  947. [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
  948. [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
  949. };
  950. static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
  951. int flags)
  952. {
  953. struct trace_seq *s = &iter->seq;
  954. const struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
  955. const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
  956. int ret;
  957. if (!trace_print_context(iter))
  958. return TRACE_TYPE_PARTIAL_LINE;
  959. if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
  960. ret = trace_seq_printf(s, "Bad pc action %x\n", what);
  961. else {
  962. const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
  963. ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
  964. if (ret)
  965. ret = what2act[what].print(s, iter->ent);
  966. }
  967. return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
  968. }
  969. static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
  970. {
  971. struct trace_seq *s = &iter->seq;
  972. struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
  973. const int offset = offsetof(struct blk_io_trace, sector);
  974. struct blk_io_trace old = {
  975. .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
  976. .time = ns2usecs(iter->ts),
  977. };
  978. if (!trace_seq_putmem(s, &old, offset))
  979. return 0;
  980. return trace_seq_putmem(s, &t->sector,
  981. sizeof(old) - offset + t->pdu_len);
  982. }
  983. static enum print_line_t
  984. blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
  985. {
  986. return blk_trace_synthesize_old_trace(iter) ?
  987. TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
  988. }
  989. static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
  990. {
  991. const struct blk_io_trace *t;
  992. u16 what;
  993. int ret;
  994. if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
  995. return TRACE_TYPE_UNHANDLED;
  996. t = (const struct blk_io_trace *)iter->ent;
  997. what = t->action & ((1 << BLK_TC_SHIFT) - 1);
  998. if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
  999. ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
  1000. else {
  1001. const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
  1002. ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
  1003. if (ret)
  1004. ret = what2act[what].print(&iter->seq, iter->ent);
  1005. }
  1006. return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
  1007. }
  1008. static struct tracer blk_tracer __read_mostly = {
  1009. .name = "blk",
  1010. .init = blk_tracer_init,
  1011. .reset = blk_tracer_reset,
  1012. .start = blk_tracer_start,
  1013. .stop = blk_tracer_stop,
  1014. .print_header = blk_tracer_print_header,
  1015. .print_line = blk_tracer_print_line,
  1016. .flags = &blk_tracer_flags,
  1017. };
  1018. static struct trace_event trace_blk_event = {
  1019. .type = TRACE_BLK,
  1020. .trace = blk_trace_event_print,
  1021. .binary = blk_trace_event_print_binary,
  1022. };
  1023. static int __init init_blk_tracer(void)
  1024. {
  1025. if (!register_ftrace_event(&trace_blk_event)) {
  1026. pr_warning("Warning: could not register block events\n");
  1027. return 1;
  1028. }
  1029. if (register_tracer(&blk_tracer) != 0) {
  1030. pr_warning("Warning: could not register the block tracer\n");
  1031. unregister_ftrace_event(&trace_blk_event);
  1032. return 1;
  1033. }
  1034. return 0;
  1035. }
  1036. device_initcall(init_blk_tracer);
  1037. static int blk_trace_remove_queue(struct request_queue *q)
  1038. {
  1039. struct blk_trace *bt;
  1040. bt = xchg(&q->blk_trace, NULL);
  1041. if (bt == NULL)
  1042. return -EINVAL;
  1043. kfree(bt);
  1044. return 0;
  1045. }
  1046. /*
  1047. * Setup everything required to start tracing
  1048. */
  1049. static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
  1050. {
  1051. struct blk_trace *old_bt, *bt = NULL;
  1052. int ret;
  1053. ret = -ENOMEM;
  1054. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  1055. if (!bt)
  1056. goto err;
  1057. bt->dev = dev;
  1058. bt->act_mask = (u16)-1;
  1059. bt->end_lba = -1ULL;
  1060. bt->trace_state = Blktrace_running;
  1061. old_bt = xchg(&q->blk_trace, bt);
  1062. if (old_bt != NULL) {
  1063. (void)xchg(&q->blk_trace, old_bt);
  1064. kfree(bt);
  1065. ret = -EBUSY;
  1066. }
  1067. return 0;
  1068. err:
  1069. return ret;
  1070. }
  1071. /*
  1072. * sysfs interface to enable and configure tracing
  1073. */
  1074. static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
  1075. struct device_attribute *attr,
  1076. char *buf)
  1077. {
  1078. struct hd_struct *p = dev_to_part(dev);
  1079. struct block_device *bdev;
  1080. ssize_t ret = -ENXIO;
  1081. lock_kernel();
  1082. bdev = bdget(part_devt(p));
  1083. if (bdev != NULL) {
  1084. struct request_queue *q = bdev_get_queue(bdev);
  1085. if (q != NULL) {
  1086. mutex_lock(&bdev->bd_mutex);
  1087. ret = sprintf(buf, "%u\n", !!q->blk_trace);
  1088. mutex_unlock(&bdev->bd_mutex);
  1089. }
  1090. bdput(bdev);
  1091. }
  1092. unlock_kernel();
  1093. return ret;
  1094. }
  1095. static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
  1096. struct device_attribute *attr,
  1097. const char *buf, size_t count)
  1098. {
  1099. struct block_device *bdev;
  1100. struct request_queue *q;
  1101. struct hd_struct *p;
  1102. int value;
  1103. ssize_t ret = -ENXIO;
  1104. if (count == 0 || sscanf(buf, "%d", &value) != 1)
  1105. goto out;
  1106. lock_kernel();
  1107. p = dev_to_part(dev);
  1108. bdev = bdget(part_devt(p));
  1109. if (bdev == NULL)
  1110. goto out_unlock_kernel;
  1111. q = bdev_get_queue(bdev);
  1112. if (q == NULL)
  1113. goto out_bdput;
  1114. mutex_lock(&bdev->bd_mutex);
  1115. if (value)
  1116. ret = blk_trace_setup_queue(q, bdev->bd_dev);
  1117. else
  1118. ret = blk_trace_remove_queue(q);
  1119. mutex_unlock(&bdev->bd_mutex);
  1120. if (ret == 0)
  1121. ret = count;
  1122. out_bdput:
  1123. bdput(bdev);
  1124. out_unlock_kernel:
  1125. unlock_kernel();
  1126. out:
  1127. return ret;
  1128. }
  1129. static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
  1130. struct device_attribute *attr,
  1131. char *buf);
  1132. static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
  1133. struct device_attribute *attr,
  1134. const char *buf, size_t count);
  1135. #define BLK_TRACE_DEVICE_ATTR(_name) \
  1136. DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
  1137. sysfs_blk_trace_attr_show, \
  1138. sysfs_blk_trace_attr_store)
  1139. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
  1140. sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
  1141. static BLK_TRACE_DEVICE_ATTR(act_mask);
  1142. static BLK_TRACE_DEVICE_ATTR(pid);
  1143. static BLK_TRACE_DEVICE_ATTR(start_lba);
  1144. static BLK_TRACE_DEVICE_ATTR(end_lba);
  1145. static struct attribute *blk_trace_attrs[] = {
  1146. &dev_attr_enable.attr,
  1147. &dev_attr_act_mask.attr,
  1148. &dev_attr_pid.attr,
  1149. &dev_attr_start_lba.attr,
  1150. &dev_attr_end_lba.attr,
  1151. NULL
  1152. };
  1153. struct attribute_group blk_trace_attr_group = {
  1154. .name = "trace",
  1155. .attrs = blk_trace_attrs,
  1156. };
  1157. static int blk_str2act_mask(const char *str)
  1158. {
  1159. int mask = 0;
  1160. char *copy = kstrdup(str, GFP_KERNEL), *s;
  1161. if (copy == NULL)
  1162. return -ENOMEM;
  1163. s = strstrip(copy);
  1164. while (1) {
  1165. char *sep = strchr(s, ',');
  1166. if (sep != NULL)
  1167. *sep = '\0';
  1168. if (strcasecmp(s, "barrier") == 0)
  1169. mask |= BLK_TC_BARRIER;
  1170. else if (strcasecmp(s, "complete") == 0)
  1171. mask |= BLK_TC_COMPLETE;
  1172. else if (strcasecmp(s, "fs") == 0)
  1173. mask |= BLK_TC_FS;
  1174. else if (strcasecmp(s, "issue") == 0)
  1175. mask |= BLK_TC_ISSUE;
  1176. else if (strcasecmp(s, "pc") == 0)
  1177. mask |= BLK_TC_PC;
  1178. else if (strcasecmp(s, "queue") == 0)
  1179. mask |= BLK_TC_QUEUE;
  1180. else if (strcasecmp(s, "read") == 0)
  1181. mask |= BLK_TC_READ;
  1182. else if (strcasecmp(s, "requeue") == 0)
  1183. mask |= BLK_TC_REQUEUE;
  1184. else if (strcasecmp(s, "sync") == 0)
  1185. mask |= BLK_TC_SYNC;
  1186. else if (strcasecmp(s, "write") == 0)
  1187. mask |= BLK_TC_WRITE;
  1188. if (sep == NULL)
  1189. break;
  1190. s = sep + 1;
  1191. }
  1192. kfree(copy);
  1193. return mask;
  1194. }
  1195. static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
  1196. struct device_attribute *attr,
  1197. char *buf)
  1198. {
  1199. struct hd_struct *p = dev_to_part(dev);
  1200. struct request_queue *q;
  1201. struct block_device *bdev;
  1202. ssize_t ret = -ENXIO;
  1203. lock_kernel();
  1204. bdev = bdget(part_devt(p));
  1205. if (bdev == NULL)
  1206. goto out_unlock_kernel;
  1207. q = bdev_get_queue(bdev);
  1208. if (q == NULL)
  1209. goto out_bdput;
  1210. mutex_lock(&bdev->bd_mutex);
  1211. if (q->blk_trace == NULL)
  1212. ret = sprintf(buf, "disabled\n");
  1213. else if (attr == &dev_attr_act_mask)
  1214. ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
  1215. else if (attr == &dev_attr_pid)
  1216. ret = sprintf(buf, "%u\n", q->blk_trace->pid);
  1217. else if (attr == &dev_attr_start_lba)
  1218. ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
  1219. else if (attr == &dev_attr_end_lba)
  1220. ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
  1221. mutex_unlock(&bdev->bd_mutex);
  1222. out_bdput:
  1223. bdput(bdev);
  1224. out_unlock_kernel:
  1225. unlock_kernel();
  1226. return ret;
  1227. }
  1228. static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
  1229. struct device_attribute *attr,
  1230. const char *buf, size_t count)
  1231. {
  1232. struct block_device *bdev;
  1233. struct request_queue *q;
  1234. struct hd_struct *p;
  1235. u64 value;
  1236. ssize_t ret = -ENXIO;
  1237. if (count == 0)
  1238. goto out;
  1239. if (attr == &dev_attr_act_mask) {
  1240. if (sscanf(buf, "%llx", &value) != 1) {
  1241. /* Assume it is a list of trace category names */
  1242. value = blk_str2act_mask(buf);
  1243. if (value < 0)
  1244. goto out;
  1245. }
  1246. } else if (sscanf(buf, "%llu", &value) != 1)
  1247. goto out;
  1248. lock_kernel();
  1249. p = dev_to_part(dev);
  1250. bdev = bdget(part_devt(p));
  1251. if (bdev == NULL)
  1252. goto out_unlock_kernel;
  1253. q = bdev_get_queue(bdev);
  1254. if (q == NULL)
  1255. goto out_bdput;
  1256. mutex_lock(&bdev->bd_mutex);
  1257. ret = 0;
  1258. if (q->blk_trace == NULL)
  1259. ret = blk_trace_setup_queue(q, bdev->bd_dev);
  1260. if (ret == 0) {
  1261. if (attr == &dev_attr_act_mask)
  1262. q->blk_trace->act_mask = value;
  1263. else if (attr == &dev_attr_pid)
  1264. q->blk_trace->pid = value;
  1265. else if (attr == &dev_attr_start_lba)
  1266. q->blk_trace->start_lba = value;
  1267. else if (attr == &dev_attr_end_lba)
  1268. q->blk_trace->end_lba = value;
  1269. ret = count;
  1270. }
  1271. mutex_unlock(&bdev->bd_mutex);
  1272. out_bdput:
  1273. bdput(bdev);
  1274. out_unlock_kernel:
  1275. unlock_kernel();
  1276. out:
  1277. return ret;
  1278. }