blktrace.c 46 KB

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