blktrace.c 46 KB

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