blktrace.c 42 KB

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