blktrace.c 45 KB

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