blktrace.c 46 KB

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