blktrace.c 36 KB

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