blktrace.c 48 KB

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