blktrace.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2006 Jens Axboe <[email protected]>
  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. strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
  441. buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
  442. /*
  443. * some device names have larger paths - convert the slashes
  444. * to underscores for this to work as expected
  445. */
  446. strreplace(buts->name, '/', '_');
  447. /*
  448. * bdev can be NULL, as with scsi-generic, this is a helpful as
  449. * we can be.
  450. */
  451. if (rcu_dereference_protected(q->blk_trace,
  452. lockdep_is_held(&q->debugfs_mutex))) {
  453. pr_warn("Concurrent blktraces are not allowed on %s\n",
  454. buts->name);
  455. return -EBUSY;
  456. }
  457. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  458. if (!bt)
  459. return -ENOMEM;
  460. ret = -ENOMEM;
  461. bt->sequence = alloc_percpu(unsigned long);
  462. if (!bt->sequence)
  463. goto err;
  464. bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
  465. if (!bt->msg_data)
  466. goto err;
  467. /*
  468. * When tracing the whole disk reuse the existing debugfs directory
  469. * created by the block layer on init. For partitions block devices,
  470. * and scsi-generic block devices we create a temporary new debugfs
  471. * directory that will be removed once the trace ends.
  472. */
  473. if (bdev && !bdev_is_partition(bdev))
  474. dir = q->debugfs_dir;
  475. else
  476. bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root);
  477. /*
  478. * As blktrace relies on debugfs for its interface the debugfs directory
  479. * is required, contrary to the usual mantra of not checking for debugfs
  480. * files or directories.
  481. */
  482. if (IS_ERR_OR_NULL(dir)) {
  483. pr_warn("debugfs_dir not present for %s so skipping\n",
  484. buts->name);
  485. ret = -ENOENT;
  486. goto err;
  487. }
  488. bt->dev = dev;
  489. atomic_set(&bt->dropped, 0);
  490. INIT_LIST_HEAD(&bt->running_list);
  491. ret = -EIO;
  492. debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
  493. debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
  494. bt->rchan = relay_open("trace", dir, buts->buf_size,
  495. buts->buf_nr, &blk_relay_callbacks, bt);
  496. if (!bt->rchan)
  497. goto err;
  498. bt->act_mask = buts->act_mask;
  499. if (!bt->act_mask)
  500. bt->act_mask = (u16) -1;
  501. blk_trace_setup_lba(bt, bdev);
  502. /* overwrite with user settings */
  503. if (buts->start_lba)
  504. bt->start_lba = buts->start_lba;
  505. if (buts->end_lba)
  506. bt->end_lba = buts->end_lba;
  507. bt->pid = buts->pid;
  508. bt->trace_state = Blktrace_setup;
  509. rcu_assign_pointer(q->blk_trace, bt);
  510. get_probe_ref();
  511. ret = 0;
  512. err:
  513. if (ret)
  514. blk_trace_free(q, bt);
  515. return ret;
  516. }
  517. static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  518. struct block_device *bdev, char __user *arg)
  519. {
  520. struct blk_user_trace_setup buts;
  521. int ret;
  522. ret = copy_from_user(&buts, arg, sizeof(buts));
  523. if (ret)
  524. return -EFAULT;
  525. ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
  526. if (ret)
  527. return ret;
  528. if (copy_to_user(arg, &buts, sizeof(buts))) {
  529. __blk_trace_remove(q);
  530. return -EFAULT;
  531. }
  532. return 0;
  533. }
  534. int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  535. struct block_device *bdev,
  536. char __user *arg)
  537. {
  538. int ret;
  539. mutex_lock(&q->debugfs_mutex);
  540. ret = __blk_trace_setup(q, name, dev, bdev, arg);
  541. mutex_unlock(&q->debugfs_mutex);
  542. return ret;
  543. }
  544. EXPORT_SYMBOL_GPL(blk_trace_setup);
  545. #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
  546. static int compat_blk_trace_setup(struct request_queue *q, char *name,
  547. dev_t dev, struct block_device *bdev,
  548. char __user *arg)
  549. {
  550. struct blk_user_trace_setup buts;
  551. struct compat_blk_user_trace_setup cbuts;
  552. int ret;
  553. if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
  554. return -EFAULT;
  555. buts = (struct blk_user_trace_setup) {
  556. .act_mask = cbuts.act_mask,
  557. .buf_size = cbuts.buf_size,
  558. .buf_nr = cbuts.buf_nr,
  559. .start_lba = cbuts.start_lba,
  560. .end_lba = cbuts.end_lba,
  561. .pid = cbuts.pid,
  562. };
  563. ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
  564. if (ret)
  565. return ret;
  566. if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
  567. __blk_trace_remove(q);
  568. return -EFAULT;
  569. }
  570. return 0;
  571. }
  572. #endif
  573. static int __blk_trace_startstop(struct request_queue *q, int start)
  574. {
  575. struct blk_trace *bt;
  576. bt = rcu_dereference_protected(q->blk_trace,
  577. lockdep_is_held(&q->debugfs_mutex));
  578. if (bt == NULL)
  579. return -EINVAL;
  580. if (start)
  581. return blk_trace_start(bt);
  582. else
  583. return blk_trace_stop(bt);
  584. }
  585. int blk_trace_startstop(struct request_queue *q, int start)
  586. {
  587. int ret;
  588. mutex_lock(&q->debugfs_mutex);
  589. ret = __blk_trace_startstop(q, start);
  590. mutex_unlock(&q->debugfs_mutex);
  591. return ret;
  592. }
  593. EXPORT_SYMBOL_GPL(blk_trace_startstop);
  594. /*
  595. * When reading or writing the blktrace sysfs files, the references to the
  596. * opened sysfs or device files should prevent the underlying block device
  597. * from being removed. So no further delete protection is really needed.
  598. */
  599. /**
  600. * blk_trace_ioctl: - handle the ioctls associated with tracing
  601. * @bdev: the block device
  602. * @cmd: the ioctl cmd
  603. * @arg: the argument data, if any
  604. *
  605. **/
  606. int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
  607. {
  608. struct request_queue *q;
  609. int ret, start = 0;
  610. char b[BDEVNAME_SIZE];
  611. q = bdev_get_queue(bdev);
  612. if (!q)
  613. return -ENXIO;
  614. mutex_lock(&q->debugfs_mutex);
  615. switch (cmd) {
  616. case BLKTRACESETUP:
  617. snprintf(b, sizeof(b), "%pg", bdev);
  618. ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
  619. break;
  620. #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
  621. case BLKTRACESETUP32:
  622. snprintf(b, sizeof(b), "%pg", bdev);
  623. ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
  624. break;
  625. #endif
  626. case BLKTRACESTART:
  627. start = 1;
  628. fallthrough;
  629. case BLKTRACESTOP:
  630. ret = __blk_trace_startstop(q, start);
  631. break;
  632. case BLKTRACETEARDOWN:
  633. ret = __blk_trace_remove(q);
  634. break;
  635. default:
  636. ret = -ENOTTY;
  637. break;
  638. }
  639. mutex_unlock(&q->debugfs_mutex);
  640. return ret;
  641. }
  642. /**
  643. * blk_trace_shutdown: - stop and cleanup trace structures
  644. * @q: the request queue associated with the device
  645. *
  646. **/
  647. void blk_trace_shutdown(struct request_queue *q)
  648. {
  649. if (rcu_dereference_protected(q->blk_trace,
  650. lockdep_is_held(&q->debugfs_mutex)))
  651. __blk_trace_remove(q);
  652. }
  653. #ifdef CONFIG_BLK_CGROUP
  654. static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
  655. {
  656. struct cgroup_subsys_state *blkcg_css;
  657. struct blk_trace *bt;
  658. /* We don't use the 'bt' value here except as an optimization... */
  659. bt = rcu_dereference_protected(q->blk_trace, 1);
  660. if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
  661. return 0;
  662. blkcg_css = bio_blkcg_css(bio);
  663. if (!blkcg_css)
  664. return 0;
  665. return cgroup_id(blkcg_css->cgroup);
  666. }
  667. #else
  668. static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
  669. {
  670. return 0;
  671. }
  672. #endif
  673. static u64
  674. blk_trace_request_get_cgid(struct request *rq)
  675. {
  676. if (!rq->bio)
  677. return 0;
  678. /* Use the first bio */
  679. return blk_trace_bio_get_cgid(rq->q, rq->bio);
  680. }
  681. /*
  682. * blktrace probes
  683. */
  684. /**
  685. * blk_add_trace_rq - Add a trace for a request oriented action
  686. * @rq: the source request
  687. * @error: return status to log
  688. * @nr_bytes: number of completed bytes
  689. * @what: the action
  690. * @cgid: the cgroup info
  691. *
  692. * Description:
  693. * Records an action against a request. Will log the bio offset + size.
  694. *
  695. **/
  696. static void blk_add_trace_rq(struct request *rq, blk_status_t error,
  697. unsigned int nr_bytes, u32 what, u64 cgid)
  698. {
  699. struct blk_trace *bt;
  700. rcu_read_lock();
  701. bt = rcu_dereference(rq->q->blk_trace);
  702. if (likely(!bt)) {
  703. rcu_read_unlock();
  704. return;
  705. }
  706. if (blk_rq_is_passthrough(rq))
  707. what |= BLK_TC_ACT(BLK_TC_PC);
  708. else
  709. what |= BLK_TC_ACT(BLK_TC_FS);
  710. __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, rq->cmd_flags,
  711. what, blk_status_to_errno(error), 0, NULL, cgid);
  712. rcu_read_unlock();
  713. }
  714. static void blk_add_trace_rq_insert(void *ignore, struct request *rq)
  715. {
  716. blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT,
  717. blk_trace_request_get_cgid(rq));
  718. }
  719. static void blk_add_trace_rq_issue(void *ignore, struct request *rq)
  720. {
  721. blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE,
  722. blk_trace_request_get_cgid(rq));
  723. }
  724. static void blk_add_trace_rq_merge(void *ignore, struct request *rq)
  725. {
  726. blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_BACKMERGE,
  727. blk_trace_request_get_cgid(rq));
  728. }
  729. static void blk_add_trace_rq_requeue(void *ignore, struct request *rq)
  730. {
  731. blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE,
  732. blk_trace_request_get_cgid(rq));
  733. }
  734. static void blk_add_trace_rq_complete(void *ignore, struct request *rq,
  735. blk_status_t error, unsigned int nr_bytes)
  736. {
  737. blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE,
  738. blk_trace_request_get_cgid(rq));
  739. }
  740. /**
  741. * blk_add_trace_bio - Add a trace for a bio oriented action
  742. * @q: queue the io is for
  743. * @bio: the source bio
  744. * @what: the action
  745. * @error: error, if any
  746. *
  747. * Description:
  748. * Records an action against a bio. Will log the bio offset + size.
  749. *
  750. **/
  751. static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
  752. u32 what, int error)
  753. {
  754. struct blk_trace *bt;
  755. rcu_read_lock();
  756. bt = rcu_dereference(q->blk_trace);
  757. if (likely(!bt)) {
  758. rcu_read_unlock();
  759. return;
  760. }
  761. __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
  762. bio->bi_opf, what, error, 0, NULL,
  763. blk_trace_bio_get_cgid(q, bio));
  764. rcu_read_unlock();
  765. }
  766. static void blk_add_trace_bio_bounce(void *ignore, struct bio *bio)
  767. {
  768. blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_BOUNCE, 0);
  769. }
  770. static void blk_add_trace_bio_complete(void *ignore,
  771. struct request_queue *q, struct bio *bio)
  772. {
  773. blk_add_trace_bio(q, bio, BLK_TA_COMPLETE,
  774. blk_status_to_errno(bio->bi_status));
  775. }
  776. static void blk_add_trace_bio_backmerge(void *ignore, struct bio *bio)
  777. {
  778. blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_BACKMERGE,
  779. 0);
  780. }
  781. static void blk_add_trace_bio_frontmerge(void *ignore, struct bio *bio)
  782. {
  783. blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_FRONTMERGE,
  784. 0);
  785. }
  786. static void blk_add_trace_bio_queue(void *ignore, struct bio *bio)
  787. {
  788. blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_QUEUE, 0);
  789. }
  790. static void blk_add_trace_getrq(void *ignore, struct bio *bio)
  791. {
  792. blk_add_trace_bio(bio->bi_bdev->bd_disk->queue, bio, BLK_TA_GETRQ, 0);
  793. }
  794. static void blk_add_trace_plug(void *ignore, struct request_queue *q)
  795. {
  796. struct blk_trace *bt;
  797. rcu_read_lock();
  798. bt = rcu_dereference(q->blk_trace);
  799. if (bt)
  800. __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL, 0);
  801. rcu_read_unlock();
  802. }
  803. static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
  804. unsigned int depth, bool explicit)
  805. {
  806. struct blk_trace *bt;
  807. rcu_read_lock();
  808. bt = rcu_dereference(q->blk_trace);
  809. if (bt) {
  810. __be64 rpdu = cpu_to_be64(depth);
  811. u32 what;
  812. if (explicit)
  813. what = BLK_TA_UNPLUG_IO;
  814. else
  815. what = BLK_TA_UNPLUG_TIMER;
  816. __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu, 0);
  817. }
  818. rcu_read_unlock();
  819. }
  820. static void blk_add_trace_split(void *ignore, struct bio *bio, unsigned int pdu)
  821. {
  822. struct request_queue *q = bio->bi_bdev->bd_disk->queue;
  823. struct blk_trace *bt;
  824. rcu_read_lock();
  825. bt = rcu_dereference(q->blk_trace);
  826. if (bt) {
  827. __be64 rpdu = cpu_to_be64(pdu);
  828. __blk_add_trace(bt, bio->bi_iter.bi_sector,
  829. bio->bi_iter.bi_size, bio->bi_opf, BLK_TA_SPLIT,
  830. blk_status_to_errno(bio->bi_status),
  831. sizeof(rpdu), &rpdu,
  832. blk_trace_bio_get_cgid(q, bio));
  833. }
  834. rcu_read_unlock();
  835. }
  836. /**
  837. * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
  838. * @ignore: trace callback data parameter (not used)
  839. * @bio: the source bio
  840. * @dev: source device
  841. * @from: source sector
  842. *
  843. * Called after a bio is remapped to a different device and/or sector.
  844. **/
  845. static void blk_add_trace_bio_remap(void *ignore, struct bio *bio, dev_t dev,
  846. sector_t from)
  847. {
  848. struct request_queue *q = bio->bi_bdev->bd_disk->queue;
  849. struct blk_trace *bt;
  850. struct blk_io_trace_remap r;
  851. rcu_read_lock();
  852. bt = rcu_dereference(q->blk_trace);
  853. if (likely(!bt)) {
  854. rcu_read_unlock();
  855. return;
  856. }
  857. r.device_from = cpu_to_be32(dev);
  858. r.device_to = cpu_to_be32(bio_dev(bio));
  859. r.sector_from = cpu_to_be64(from);
  860. __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
  861. bio->bi_opf, BLK_TA_REMAP,
  862. blk_status_to_errno(bio->bi_status),
  863. sizeof(r), &r, blk_trace_bio_get_cgid(q, bio));
  864. rcu_read_unlock();
  865. }
  866. /**
  867. * blk_add_trace_rq_remap - Add a trace for a request-remap operation
  868. * @ignore: trace callback data parameter (not used)
  869. * @rq: the source request
  870. * @dev: target device
  871. * @from: source sector
  872. *
  873. * Description:
  874. * Device mapper remaps request to other devices.
  875. * Add a trace for that action.
  876. *
  877. **/
  878. static void blk_add_trace_rq_remap(void *ignore, struct request *rq, dev_t dev,
  879. sector_t from)
  880. {
  881. struct blk_trace *bt;
  882. struct blk_io_trace_remap r;
  883. rcu_read_lock();
  884. bt = rcu_dereference(rq->q->blk_trace);
  885. if (likely(!bt)) {
  886. rcu_read_unlock();
  887. return;
  888. }
  889. r.device_from = cpu_to_be32(dev);
  890. r.device_to = cpu_to_be32(disk_devt(rq->q->disk));
  891. r.sector_from = cpu_to_be64(from);
  892. __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
  893. rq->cmd_flags, BLK_TA_REMAP, 0,
  894. sizeof(r), &r, blk_trace_request_get_cgid(rq));
  895. rcu_read_unlock();
  896. }
  897. /**
  898. * blk_add_driver_data - Add binary message with driver-specific data
  899. * @rq: io request
  900. * @data: driver-specific data
  901. * @len: length of driver-specific data
  902. *
  903. * Description:
  904. * Some drivers might want to write driver-specific data per request.
  905. *
  906. **/
  907. void blk_add_driver_data(struct request *rq, void *data, size_t len)
  908. {
  909. struct blk_trace *bt;
  910. rcu_read_lock();
  911. bt = rcu_dereference(rq->q->blk_trace);
  912. if (likely(!bt)) {
  913. rcu_read_unlock();
  914. return;
  915. }
  916. __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0,
  917. BLK_TA_DRV_DATA, 0, len, data,
  918. blk_trace_request_get_cgid(rq));
  919. rcu_read_unlock();
  920. }
  921. EXPORT_SYMBOL_GPL(blk_add_driver_data);
  922. static void blk_register_tracepoints(void)
  923. {
  924. int ret;
  925. ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
  926. WARN_ON(ret);
  927. ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
  928. WARN_ON(ret);
  929. ret = register_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
  930. WARN_ON(ret);
  931. ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
  932. WARN_ON(ret);
  933. ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
  934. WARN_ON(ret);
  935. ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
  936. WARN_ON(ret);
  937. ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
  938. WARN_ON(ret);
  939. ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
  940. WARN_ON(ret);
  941. ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
  942. WARN_ON(ret);
  943. ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
  944. WARN_ON(ret);
  945. ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
  946. WARN_ON(ret);
  947. ret = register_trace_block_plug(blk_add_trace_plug, NULL);
  948. WARN_ON(ret);
  949. ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
  950. WARN_ON(ret);
  951. ret = register_trace_block_split(blk_add_trace_split, NULL);
  952. WARN_ON(ret);
  953. ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
  954. WARN_ON(ret);
  955. ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
  956. WARN_ON(ret);
  957. }
  958. static void blk_unregister_tracepoints(void)
  959. {
  960. unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
  961. unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
  962. unregister_trace_block_split(blk_add_trace_split, NULL);
  963. unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
  964. unregister_trace_block_plug(blk_add_trace_plug, NULL);
  965. unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
  966. unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
  967. unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
  968. unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
  969. unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
  970. unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
  971. unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
  972. unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
  973. unregister_trace_block_rq_merge(blk_add_trace_rq_merge, NULL);
  974. unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
  975. unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
  976. tracepoint_synchronize_unregister();
  977. }
  978. /*
  979. * struct blk_io_tracer formatting routines
  980. */
  981. static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
  982. {
  983. int i = 0;
  984. int tc = t->action >> BLK_TC_SHIFT;
  985. if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
  986. rwbs[i++] = 'N';
  987. goto out;
  988. }
  989. if (tc & BLK_TC_FLUSH)
  990. rwbs[i++] = 'F';
  991. if (tc & BLK_TC_DISCARD)
  992. rwbs[i++] = 'D';
  993. else if (tc & BLK_TC_WRITE)
  994. rwbs[i++] = 'W';
  995. else if (t->bytes)
  996. rwbs[i++] = 'R';
  997. else
  998. rwbs[i++] = 'N';
  999. if (tc & BLK_TC_FUA)
  1000. rwbs[i++] = 'F';
  1001. if (tc & BLK_TC_AHEAD)
  1002. rwbs[i++] = 'A';
  1003. if (tc & BLK_TC_SYNC)
  1004. rwbs[i++] = 'S';
  1005. if (tc & BLK_TC_META)
  1006. rwbs[i++] = 'M';
  1007. out:
  1008. rwbs[i] = '\0';
  1009. }
  1010. static inline
  1011. const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
  1012. {
  1013. return (const struct blk_io_trace *)ent;
  1014. }
  1015. static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg)
  1016. {
  1017. return (void *)(te_blk_io_trace(ent) + 1) + (has_cg ? sizeof(u64) : 0);
  1018. }
  1019. static inline u64 t_cgid(const struct trace_entry *ent)
  1020. {
  1021. return *(u64 *)(te_blk_io_trace(ent) + 1);
  1022. }
  1023. static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg)
  1024. {
  1025. return te_blk_io_trace(ent)->pdu_len - (has_cg ? sizeof(u64) : 0);
  1026. }
  1027. static inline u32 t_action(const struct trace_entry *ent)
  1028. {
  1029. return te_blk_io_trace(ent)->action;
  1030. }
  1031. static inline u32 t_bytes(const struct trace_entry *ent)
  1032. {
  1033. return te_blk_io_trace(ent)->bytes;
  1034. }
  1035. static inline u32 t_sec(const struct trace_entry *ent)
  1036. {
  1037. return te_blk_io_trace(ent)->bytes >> 9;
  1038. }
  1039. static inline unsigned long long t_sector(const struct trace_entry *ent)
  1040. {
  1041. return te_blk_io_trace(ent)->sector;
  1042. }
  1043. static inline __u16 t_error(const struct trace_entry *ent)
  1044. {
  1045. return te_blk_io_trace(ent)->error;
  1046. }
  1047. static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg)
  1048. {
  1049. const __be64 *val = pdu_start(ent, has_cg);
  1050. return be64_to_cpu(*val);
  1051. }
  1052. typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act,
  1053. bool has_cg);
  1054. static void blk_log_action_classic(struct trace_iterator *iter, const char *act,
  1055. bool has_cg)
  1056. {
  1057. char rwbs[RWBS_LEN];
  1058. unsigned long long ts = iter->ts;
  1059. unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
  1060. unsigned secs = (unsigned long)ts;
  1061. const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
  1062. fill_rwbs(rwbs, t);
  1063. trace_seq_printf(&iter->seq,
  1064. "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
  1065. MAJOR(t->device), MINOR(t->device), iter->cpu,
  1066. secs, nsec_rem, iter->ent->pid, act, rwbs);
  1067. }
  1068. static void blk_log_action(struct trace_iterator *iter, const char *act,
  1069. bool has_cg)
  1070. {
  1071. char rwbs[RWBS_LEN];
  1072. const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
  1073. fill_rwbs(rwbs, t);
  1074. if (has_cg) {
  1075. u64 id = t_cgid(iter->ent);
  1076. if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) {
  1077. char blkcg_name_buf[NAME_MAX + 1] = "<...>";
  1078. cgroup_path_from_kernfs_id(id, blkcg_name_buf,
  1079. sizeof(blkcg_name_buf));
  1080. trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ",
  1081. MAJOR(t->device), MINOR(t->device),
  1082. blkcg_name_buf, act, rwbs);
  1083. } else {
  1084. /*
  1085. * The cgid portion used to be "INO,GEN". Userland
  1086. * builds a FILEID_INO32_GEN fid out of them and
  1087. * opens the cgroup using open_by_handle_at(2).
  1088. * While 32bit ino setups are still the same, 64bit
  1089. * ones now use the 64bit ino as the whole ID and
  1090. * no longer use generation.
  1091. *
  1092. * Regardless of the content, always output
  1093. * "LOW32,HIGH32" so that FILEID_INO32_GEN fid can
  1094. * be mapped back to @id on both 64 and 32bit ino
  1095. * setups. See __kernfs_fh_to_dentry().
  1096. */
  1097. trace_seq_printf(&iter->seq,
  1098. "%3d,%-3d %llx,%-llx %2s %3s ",
  1099. MAJOR(t->device), MINOR(t->device),
  1100. id & U32_MAX, id >> 32, act, rwbs);
  1101. }
  1102. } else
  1103. trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
  1104. MAJOR(t->device), MINOR(t->device), act, rwbs);
  1105. }
  1106. static void blk_log_dump_pdu(struct trace_seq *s,
  1107. const struct trace_entry *ent, bool has_cg)
  1108. {
  1109. const unsigned char *pdu_buf;
  1110. int pdu_len;
  1111. int i, end;
  1112. pdu_buf = pdu_start(ent, has_cg);
  1113. pdu_len = pdu_real_len(ent, has_cg);
  1114. if (!pdu_len)
  1115. return;
  1116. /* find the last zero that needs to be printed */
  1117. for (end = pdu_len - 1; end >= 0; end--)
  1118. if (pdu_buf[end])
  1119. break;
  1120. end++;
  1121. trace_seq_putc(s, '(');
  1122. for (i = 0; i < pdu_len; i++) {
  1123. trace_seq_printf(s, "%s%02x",
  1124. i == 0 ? "" : " ", pdu_buf[i]);
  1125. /*
  1126. * stop when the rest is just zeros and indicate so
  1127. * with a ".." appended
  1128. */
  1129. if (i == end && end != pdu_len - 1) {
  1130. trace_seq_puts(s, " ..) ");
  1131. return;
  1132. }
  1133. }
  1134. trace_seq_puts(s, ") ");
  1135. }
  1136. static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
  1137. {
  1138. char cmd[TASK_COMM_LEN];
  1139. trace_find_cmdline(ent->pid, cmd);
  1140. if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
  1141. trace_seq_printf(s, "%u ", t_bytes(ent));
  1142. blk_log_dump_pdu(s, ent, has_cg);
  1143. trace_seq_printf(s, "[%s]\n", cmd);
  1144. } else {
  1145. if (t_sec(ent))
  1146. trace_seq_printf(s, "%llu + %u [%s]\n",
  1147. t_sector(ent), t_sec(ent), cmd);
  1148. else
  1149. trace_seq_printf(s, "[%s]\n", cmd);
  1150. }
  1151. }
  1152. static void blk_log_with_error(struct trace_seq *s,
  1153. const struct trace_entry *ent, bool has_cg)
  1154. {
  1155. if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
  1156. blk_log_dump_pdu(s, ent, has_cg);
  1157. trace_seq_printf(s, "[%d]\n", t_error(ent));
  1158. } else {
  1159. if (t_sec(ent))
  1160. trace_seq_printf(s, "%llu + %u [%d]\n",
  1161. t_sector(ent),
  1162. t_sec(ent), t_error(ent));
  1163. else
  1164. trace_seq_printf(s, "%llu [%d]\n",
  1165. t_sector(ent), t_error(ent));
  1166. }
  1167. }
  1168. static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
  1169. {
  1170. const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg);
  1171. trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
  1172. t_sector(ent), t_sec(ent),
  1173. MAJOR(be32_to_cpu(__r->device_from)),
  1174. MINOR(be32_to_cpu(__r->device_from)),
  1175. be64_to_cpu(__r->sector_from));
  1176. }
  1177. static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
  1178. {
  1179. char cmd[TASK_COMM_LEN];
  1180. trace_find_cmdline(ent->pid, cmd);
  1181. trace_seq_printf(s, "[%s]\n", cmd);
  1182. }
  1183. static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
  1184. {
  1185. char cmd[TASK_COMM_LEN];
  1186. trace_find_cmdline(ent->pid, cmd);
  1187. trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg));
  1188. }
  1189. static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent, bool has_cg)
  1190. {
  1191. char cmd[TASK_COMM_LEN];
  1192. trace_find_cmdline(ent->pid, cmd);
  1193. trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
  1194. get_pdu_int(ent, has_cg), cmd);
  1195. }
  1196. static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent,
  1197. bool has_cg)
  1198. {
  1199. trace_seq_putmem(s, pdu_start(ent, has_cg),
  1200. pdu_real_len(ent, has_cg));
  1201. trace_seq_putc(s, '\n');
  1202. }
  1203. /*
  1204. * struct tracer operations
  1205. */
  1206. static void blk_tracer_print_header(struct seq_file *m)
  1207. {
  1208. if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
  1209. return;
  1210. seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
  1211. "# | | | | | |\n");
  1212. }
  1213. static void blk_tracer_start(struct trace_array *tr)
  1214. {
  1215. blk_tracer_enabled = true;
  1216. }
  1217. static int blk_tracer_init(struct trace_array *tr)
  1218. {
  1219. blk_tr = tr;
  1220. blk_tracer_start(tr);
  1221. return 0;
  1222. }
  1223. static void blk_tracer_stop(struct trace_array *tr)
  1224. {
  1225. blk_tracer_enabled = false;
  1226. }
  1227. static void blk_tracer_reset(struct trace_array *tr)
  1228. {
  1229. blk_tracer_stop(tr);
  1230. }
  1231. static const struct {
  1232. const char *act[2];
  1233. void (*print)(struct trace_seq *s, const struct trace_entry *ent,
  1234. bool has_cg);
  1235. } what2act[] = {
  1236. [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
  1237. [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
  1238. [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
  1239. [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
  1240. [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
  1241. [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
  1242. [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
  1243. [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
  1244. [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
  1245. [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
  1246. [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
  1247. [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
  1248. [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
  1249. [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
  1250. [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
  1251. };
  1252. static enum print_line_t print_one_line(struct trace_iterator *iter,
  1253. bool classic)
  1254. {
  1255. struct trace_array *tr = iter->tr;
  1256. struct trace_seq *s = &iter->seq;
  1257. const struct blk_io_trace *t;
  1258. u16 what;
  1259. bool long_act;
  1260. blk_log_action_t *log_action;
  1261. bool has_cg;
  1262. t = te_blk_io_trace(iter->ent);
  1263. what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP;
  1264. long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
  1265. log_action = classic ? &blk_log_action_classic : &blk_log_action;
  1266. has_cg = t->action & __BLK_TA_CGROUP;
  1267. if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) {
  1268. log_action(iter, long_act ? "message" : "m", has_cg);
  1269. blk_log_msg(s, iter->ent, has_cg);
  1270. return trace_handle_return(s);
  1271. }
  1272. if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
  1273. trace_seq_printf(s, "Unknown action %x\n", what);
  1274. else {
  1275. log_action(iter, what2act[what].act[long_act], has_cg);
  1276. what2act[what].print(s, iter->ent, has_cg);
  1277. }
  1278. return trace_handle_return(s);
  1279. }
  1280. static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
  1281. int flags, struct trace_event *event)
  1282. {
  1283. return print_one_line(iter, false);
  1284. }
  1285. static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
  1286. {
  1287. struct trace_seq *s = &iter->seq;
  1288. struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
  1289. const int offset = offsetof(struct blk_io_trace, sector);
  1290. struct blk_io_trace old = {
  1291. .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
  1292. .time = iter->ts,
  1293. };
  1294. trace_seq_putmem(s, &old, offset);
  1295. trace_seq_putmem(s, &t->sector,
  1296. sizeof(old) - offset + t->pdu_len);
  1297. }
  1298. static enum print_line_t
  1299. blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
  1300. struct trace_event *event)
  1301. {
  1302. blk_trace_synthesize_old_trace(iter);
  1303. return trace_handle_return(&iter->seq);
  1304. }
  1305. static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
  1306. {
  1307. if ((iter->ent->type != TRACE_BLK) ||
  1308. !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
  1309. return TRACE_TYPE_UNHANDLED;
  1310. return print_one_line(iter, true);
  1311. }
  1312. static int
  1313. blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  1314. {
  1315. /* don't output context-info for blk_classic output */
  1316. if (bit == TRACE_BLK_OPT_CLASSIC) {
  1317. if (set)
  1318. tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
  1319. else
  1320. tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
  1321. }
  1322. return 0;
  1323. }
  1324. static struct tracer blk_tracer __read_mostly = {
  1325. .name = "blk",
  1326. .init = blk_tracer_init,
  1327. .reset = blk_tracer_reset,
  1328. .start = blk_tracer_start,
  1329. .stop = blk_tracer_stop,
  1330. .print_header = blk_tracer_print_header,
  1331. .print_line = blk_tracer_print_line,
  1332. .flags = &blk_tracer_flags,
  1333. .set_flag = blk_tracer_set_flag,
  1334. };
  1335. static struct trace_event_functions trace_blk_event_funcs = {
  1336. .trace = blk_trace_event_print,
  1337. .binary = blk_trace_event_print_binary,
  1338. };
  1339. static struct trace_event trace_blk_event = {
  1340. .type = TRACE_BLK,
  1341. .funcs = &trace_blk_event_funcs,
  1342. };
  1343. static int __init init_blk_tracer(void)
  1344. {
  1345. if (!register_trace_event(&trace_blk_event)) {
  1346. pr_warn("Warning: could not register block events\n");
  1347. return 1;
  1348. }
  1349. if (register_tracer(&blk_tracer) != 0) {
  1350. pr_warn("Warning: could not register the block tracer\n");
  1351. unregister_trace_event(&trace_blk_event);
  1352. return 1;
  1353. }
  1354. return 0;
  1355. }
  1356. device_initcall(init_blk_tracer);
  1357. static int blk_trace_remove_queue(struct request_queue *q)
  1358. {
  1359. struct blk_trace *bt;
  1360. bt = rcu_replace_pointer(q->blk_trace, NULL,
  1361. lockdep_is_held(&q->debugfs_mutex));
  1362. if (bt == NULL)
  1363. return -EINVAL;
  1364. blk_trace_stop(bt);
  1365. put_probe_ref();
  1366. synchronize_rcu();
  1367. blk_trace_free(q, bt);
  1368. return 0;
  1369. }
  1370. /*
  1371. * Setup everything required to start tracing
  1372. */
  1373. static int blk_trace_setup_queue(struct request_queue *q,
  1374. struct block_device *bdev)
  1375. {
  1376. struct blk_trace *bt = NULL;
  1377. int ret = -ENOMEM;
  1378. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  1379. if (!bt)
  1380. return -ENOMEM;
  1381. bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
  1382. if (!bt->msg_data)
  1383. goto free_bt;
  1384. bt->dev = bdev->bd_dev;
  1385. bt->act_mask = (u16)-1;
  1386. blk_trace_setup_lba(bt, bdev);
  1387. rcu_assign_pointer(q->blk_trace, bt);
  1388. get_probe_ref();
  1389. return 0;
  1390. free_bt:
  1391. blk_trace_free(q, bt);
  1392. return ret;
  1393. }
  1394. /*
  1395. * sysfs interface to enable and configure tracing
  1396. */
  1397. static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
  1398. struct device_attribute *attr,
  1399. char *buf);
  1400. static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
  1401. struct device_attribute *attr,
  1402. const char *buf, size_t count);
  1403. #define BLK_TRACE_DEVICE_ATTR(_name) \
  1404. DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
  1405. sysfs_blk_trace_attr_show, \
  1406. sysfs_blk_trace_attr_store)
  1407. static BLK_TRACE_DEVICE_ATTR(enable);
  1408. static BLK_TRACE_DEVICE_ATTR(act_mask);
  1409. static BLK_TRACE_DEVICE_ATTR(pid);
  1410. static BLK_TRACE_DEVICE_ATTR(start_lba);
  1411. static BLK_TRACE_DEVICE_ATTR(end_lba);
  1412. static struct attribute *blk_trace_attrs[] = {
  1413. &dev_attr_enable.attr,
  1414. &dev_attr_act_mask.attr,
  1415. &dev_attr_pid.attr,
  1416. &dev_attr_start_lba.attr,
  1417. &dev_attr_end_lba.attr,
  1418. NULL
  1419. };
  1420. struct attribute_group blk_trace_attr_group = {
  1421. .name = "trace",
  1422. .attrs = blk_trace_attrs,
  1423. };
  1424. static const struct {
  1425. int mask;
  1426. const char *str;
  1427. } mask_maps[] = {
  1428. { BLK_TC_READ, "read" },
  1429. { BLK_TC_WRITE, "write" },
  1430. { BLK_TC_FLUSH, "flush" },
  1431. { BLK_TC_SYNC, "sync" },
  1432. { BLK_TC_QUEUE, "queue" },
  1433. { BLK_TC_REQUEUE, "requeue" },
  1434. { BLK_TC_ISSUE, "issue" },
  1435. { BLK_TC_COMPLETE, "complete" },
  1436. { BLK_TC_FS, "fs" },
  1437. { BLK_TC_PC, "pc" },
  1438. { BLK_TC_NOTIFY, "notify" },
  1439. { BLK_TC_AHEAD, "ahead" },
  1440. { BLK_TC_META, "meta" },
  1441. { BLK_TC_DISCARD, "discard" },
  1442. { BLK_TC_DRV_DATA, "drv_data" },
  1443. { BLK_TC_FUA, "fua" },
  1444. };
  1445. static int blk_trace_str2mask(const char *str)
  1446. {
  1447. int i;
  1448. int mask = 0;
  1449. char *buf, *s, *token;
  1450. buf = kstrdup(str, GFP_KERNEL);
  1451. if (buf == NULL)
  1452. return -ENOMEM;
  1453. s = strstrip(buf);
  1454. while (1) {
  1455. token = strsep(&s, ",");
  1456. if (token == NULL)
  1457. break;
  1458. if (*token == '\0')
  1459. continue;
  1460. for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
  1461. if (strcasecmp(token, mask_maps[i].str) == 0) {
  1462. mask |= mask_maps[i].mask;
  1463. break;
  1464. }
  1465. }
  1466. if (i == ARRAY_SIZE(mask_maps)) {
  1467. mask = -EINVAL;
  1468. break;
  1469. }
  1470. }
  1471. kfree(buf);
  1472. return mask;
  1473. }
  1474. static ssize_t blk_trace_mask2str(char *buf, int mask)
  1475. {
  1476. int i;
  1477. char *p = buf;
  1478. for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
  1479. if (mask & mask_maps[i].mask) {
  1480. p += sprintf(p, "%s%s",
  1481. (p == buf) ? "" : ",", mask_maps[i].str);
  1482. }
  1483. }
  1484. *p++ = '\n';
  1485. return p - buf;
  1486. }
  1487. static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
  1488. struct device_attribute *attr,
  1489. char *buf)
  1490. {
  1491. struct block_device *bdev = dev_to_bdev(dev);
  1492. struct request_queue *q = bdev_get_queue(bdev);
  1493. struct blk_trace *bt;
  1494. ssize_t ret = -ENXIO;
  1495. mutex_lock(&q->debugfs_mutex);
  1496. bt = rcu_dereference_protected(q->blk_trace,
  1497. lockdep_is_held(&q->debugfs_mutex));
  1498. if (attr == &dev_attr_enable) {
  1499. ret = sprintf(buf, "%u\n", !!bt);
  1500. goto out_unlock_bdev;
  1501. }
  1502. if (bt == NULL)
  1503. ret = sprintf(buf, "disabled\n");
  1504. else if (attr == &dev_attr_act_mask)
  1505. ret = blk_trace_mask2str(buf, bt->act_mask);
  1506. else if (attr == &dev_attr_pid)
  1507. ret = sprintf(buf, "%u\n", bt->pid);
  1508. else if (attr == &dev_attr_start_lba)
  1509. ret = sprintf(buf, "%llu\n", bt->start_lba);
  1510. else if (attr == &dev_attr_end_lba)
  1511. ret = sprintf(buf, "%llu\n", bt->end_lba);
  1512. out_unlock_bdev:
  1513. mutex_unlock(&q->debugfs_mutex);
  1514. return ret;
  1515. }
  1516. static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
  1517. struct device_attribute *attr,
  1518. const char *buf, size_t count)
  1519. {
  1520. struct block_device *bdev = dev_to_bdev(dev);
  1521. struct request_queue *q = bdev_get_queue(bdev);
  1522. struct blk_trace *bt;
  1523. u64 value;
  1524. ssize_t ret = -EINVAL;
  1525. if (count == 0)
  1526. goto out;
  1527. if (attr == &dev_attr_act_mask) {
  1528. if (kstrtoull(buf, 0, &value)) {
  1529. /* Assume it is a list of trace category names */
  1530. ret = blk_trace_str2mask(buf);
  1531. if (ret < 0)
  1532. goto out;
  1533. value = ret;
  1534. }
  1535. } else {
  1536. if (kstrtoull(buf, 0, &value))
  1537. goto out;
  1538. }
  1539. mutex_lock(&q->debugfs_mutex);
  1540. bt = rcu_dereference_protected(q->blk_trace,
  1541. lockdep_is_held(&q->debugfs_mutex));
  1542. if (attr == &dev_attr_enable) {
  1543. if (!!value == !!bt) {
  1544. ret = 0;
  1545. goto out_unlock_bdev;
  1546. }
  1547. if (value)
  1548. ret = blk_trace_setup_queue(q, bdev);
  1549. else
  1550. ret = blk_trace_remove_queue(q);
  1551. goto out_unlock_bdev;
  1552. }
  1553. ret = 0;
  1554. if (bt == NULL) {
  1555. ret = blk_trace_setup_queue(q, bdev);
  1556. bt = rcu_dereference_protected(q->blk_trace,
  1557. lockdep_is_held(&q->debugfs_mutex));
  1558. }
  1559. if (ret == 0) {
  1560. if (attr == &dev_attr_act_mask)
  1561. bt->act_mask = value;
  1562. else if (attr == &dev_attr_pid)
  1563. bt->pid = value;
  1564. else if (attr == &dev_attr_start_lba)
  1565. bt->start_lba = value;
  1566. else if (attr == &dev_attr_end_lba)
  1567. bt->end_lba = value;
  1568. }
  1569. out_unlock_bdev:
  1570. mutex_unlock(&q->debugfs_mutex);
  1571. out:
  1572. return ret ? ret : count;
  1573. }
  1574. #endif /* CONFIG_BLK_DEV_IO_TRACE */
  1575. #ifdef CONFIG_EVENT_TRACING
  1576. /**
  1577. * blk_fill_rwbs - Fill the buffer rwbs by mapping op to character string.
  1578. * @rwbs: buffer to be filled
  1579. * @opf: request operation type (REQ_OP_XXX) and flags for the tracepoint
  1580. *
  1581. * Description:
  1582. * Maps each request operation and flag to a single character and fills the
  1583. * buffer provided by the caller with resulting string.
  1584. *
  1585. **/
  1586. void blk_fill_rwbs(char *rwbs, blk_opf_t opf)
  1587. {
  1588. int i = 0;
  1589. if (opf & REQ_PREFLUSH)
  1590. rwbs[i++] = 'F';
  1591. switch (opf & REQ_OP_MASK) {
  1592. case REQ_OP_WRITE:
  1593. rwbs[i++] = 'W';
  1594. break;
  1595. case REQ_OP_DISCARD:
  1596. rwbs[i++] = 'D';
  1597. break;
  1598. case REQ_OP_SECURE_ERASE:
  1599. rwbs[i++] = 'D';
  1600. rwbs[i++] = 'E';
  1601. break;
  1602. case REQ_OP_FLUSH:
  1603. rwbs[i++] = 'F';
  1604. break;
  1605. case REQ_OP_READ:
  1606. rwbs[i++] = 'R';
  1607. break;
  1608. default:
  1609. rwbs[i++] = 'N';
  1610. }
  1611. if (opf & REQ_FUA)
  1612. rwbs[i++] = 'F';
  1613. if (opf & REQ_RAHEAD)
  1614. rwbs[i++] = 'A';
  1615. if (opf & REQ_SYNC)
  1616. rwbs[i++] = 'S';
  1617. if (opf & REQ_META)
  1618. rwbs[i++] = 'M';
  1619. rwbs[i] = '\0';
  1620. }
  1621. EXPORT_SYMBOL_GPL(blk_fill_rwbs);
  1622. #endif /* CONFIG_EVENT_TRACING */