blktrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/debugfs.h>
  25. #include <asm/uaccess.h>
  26. static DEFINE_PER_CPU(unsigned long long, blk_trace_cpu_offset) = { 0, };
  27. static unsigned int blktrace_seq __read_mostly = 1;
  28. /*
  29. * Send out a notify for this process, if we haven't done so since a trace
  30. * started
  31. */
  32. static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
  33. {
  34. struct blk_io_trace *t;
  35. t = relay_reserve(bt->rchan, sizeof(*t) + sizeof(tsk->comm));
  36. if (t) {
  37. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  38. t->device = bt->dev;
  39. t->action = BLK_TC_ACT(BLK_TC_NOTIFY);
  40. t->pid = tsk->pid;
  41. t->cpu = smp_processor_id();
  42. t->pdu_len = sizeof(tsk->comm);
  43. memcpy((void *) t + sizeof(*t), tsk->comm, t->pdu_len);
  44. tsk->btrace_seq = blktrace_seq;
  45. }
  46. }
  47. static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
  48. pid_t pid)
  49. {
  50. if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
  51. return 1;
  52. if (sector < bt->start_lba || sector > bt->end_lba)
  53. return 1;
  54. if (bt->pid && pid != bt->pid)
  55. return 1;
  56. return 0;
  57. }
  58. /*
  59. * Data direction bit lookup
  60. */
  61. static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
  62. /*
  63. * Bio action bits of interest
  64. */
  65. static u32 bio_act[9] __read_mostly = { 0, BLK_TC_ACT(BLK_TC_BARRIER), BLK_TC_ACT(BLK_TC_SYNC), 0, BLK_TC_ACT(BLK_TC_AHEAD), 0, 0, 0, BLK_TC_ACT(BLK_TC_META) };
  66. /*
  67. * More could be added as needed, taking care to increment the decrementer
  68. * to get correct indexing
  69. */
  70. #define trace_barrier_bit(rw) \
  71. (((rw) & (1 << BIO_RW_BARRIER)) >> (BIO_RW_BARRIER - 0))
  72. #define trace_sync_bit(rw) \
  73. (((rw) & (1 << BIO_RW_SYNC)) >> (BIO_RW_SYNC - 1))
  74. #define trace_ahead_bit(rw) \
  75. (((rw) & (1 << BIO_RW_AHEAD)) << (2 - BIO_RW_AHEAD))
  76. #define trace_meta_bit(rw) \
  77. (((rw) & (1 << BIO_RW_META)) >> (BIO_RW_META - 3))
  78. /*
  79. * The worker for the various blk_add_trace*() types. Fills out a
  80. * blk_io_trace structure and places it in a per-cpu subbuffer.
  81. */
  82. void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
  83. int rw, u32 what, int error, int pdu_len, void *pdu_data)
  84. {
  85. struct task_struct *tsk = current;
  86. struct blk_io_trace *t;
  87. unsigned long flags;
  88. unsigned long *sequence;
  89. pid_t pid;
  90. int cpu;
  91. if (unlikely(bt->trace_state != Blktrace_running))
  92. return;
  93. what |= ddir_act[rw & WRITE];
  94. what |= bio_act[trace_barrier_bit(rw)];
  95. what |= bio_act[trace_sync_bit(rw)];
  96. what |= bio_act[trace_ahead_bit(rw)];
  97. what |= bio_act[trace_meta_bit(rw)];
  98. pid = tsk->pid;
  99. if (unlikely(act_log_check(bt, what, sector, pid)))
  100. return;
  101. /*
  102. * A word about the locking here - we disable interrupts to reserve
  103. * some space in the relay per-cpu buffer, to prevent an irq
  104. * from coming in and stepping on our toes. Once reserved, it's
  105. * enough to get preemption disabled to prevent read of this data
  106. * before we are through filling it. get_cpu()/put_cpu() does this
  107. * for us
  108. */
  109. local_irq_save(flags);
  110. if (unlikely(tsk->btrace_seq != blktrace_seq))
  111. trace_note_tsk(bt, tsk);
  112. t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
  113. if (t) {
  114. cpu = smp_processor_id();
  115. sequence = per_cpu_ptr(bt->sequence, cpu);
  116. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  117. t->sequence = ++(*sequence);
  118. t->time = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
  119. t->sector = sector;
  120. t->bytes = bytes;
  121. t->action = what;
  122. t->pid = pid;
  123. t->device = bt->dev;
  124. t->cpu = cpu;
  125. t->error = error;
  126. t->pdu_len = pdu_len;
  127. if (pdu_len)
  128. memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
  129. }
  130. local_irq_restore(flags);
  131. }
  132. EXPORT_SYMBOL_GPL(__blk_add_trace);
  133. static struct dentry *blk_tree_root;
  134. static struct mutex blk_tree_mutex;
  135. static unsigned int root_users;
  136. static inline void blk_remove_root(void)
  137. {
  138. if (blk_tree_root) {
  139. debugfs_remove(blk_tree_root);
  140. blk_tree_root = NULL;
  141. }
  142. }
  143. static void blk_remove_tree(struct dentry *dir)
  144. {
  145. mutex_lock(&blk_tree_mutex);
  146. debugfs_remove(dir);
  147. if (--root_users == 0)
  148. blk_remove_root();
  149. mutex_unlock(&blk_tree_mutex);
  150. }
  151. static struct dentry *blk_create_tree(const char *blk_name)
  152. {
  153. struct dentry *dir = NULL;
  154. mutex_lock(&blk_tree_mutex);
  155. if (!blk_tree_root) {
  156. blk_tree_root = debugfs_create_dir("block", NULL);
  157. if (!blk_tree_root)
  158. goto err;
  159. }
  160. dir = debugfs_create_dir(blk_name, blk_tree_root);
  161. if (dir)
  162. root_users++;
  163. else
  164. blk_remove_root();
  165. err:
  166. mutex_unlock(&blk_tree_mutex);
  167. return dir;
  168. }
  169. static void blk_trace_cleanup(struct blk_trace *bt)
  170. {
  171. relay_close(bt->rchan);
  172. debugfs_remove(bt->dropped_file);
  173. blk_remove_tree(bt->dir);
  174. free_percpu(bt->sequence);
  175. kfree(bt);
  176. }
  177. static int blk_trace_remove(request_queue_t *q)
  178. {
  179. struct blk_trace *bt;
  180. bt = xchg(&q->blk_trace, NULL);
  181. if (!bt)
  182. return -EINVAL;
  183. if (bt->trace_state == Blktrace_setup ||
  184. bt->trace_state == Blktrace_stopped)
  185. blk_trace_cleanup(bt);
  186. return 0;
  187. }
  188. static int blk_dropped_open(struct inode *inode, struct file *filp)
  189. {
  190. filp->private_data = inode->i_private;
  191. return 0;
  192. }
  193. static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
  194. size_t count, loff_t *ppos)
  195. {
  196. struct blk_trace *bt = filp->private_data;
  197. char buf[16];
  198. snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
  199. return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  200. }
  201. static struct file_operations blk_dropped_fops = {
  202. .owner = THIS_MODULE,
  203. .open = blk_dropped_open,
  204. .read = blk_dropped_read,
  205. };
  206. /*
  207. * Keep track of how many times we encountered a full subbuffer, to aid
  208. * the user space app in telling how many lost events there were.
  209. */
  210. static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
  211. void *prev_subbuf, size_t prev_padding)
  212. {
  213. struct blk_trace *bt;
  214. if (!relay_buf_full(buf))
  215. return 1;
  216. bt = buf->chan->private_data;
  217. atomic_inc(&bt->dropped);
  218. return 0;
  219. }
  220. static int blk_remove_buf_file_callback(struct dentry *dentry)
  221. {
  222. debugfs_remove(dentry);
  223. return 0;
  224. }
  225. static struct dentry *blk_create_buf_file_callback(const char *filename,
  226. struct dentry *parent,
  227. int mode,
  228. struct rchan_buf *buf,
  229. int *is_global)
  230. {
  231. return debugfs_create_file(filename, mode, parent, buf,
  232. &relay_file_operations);
  233. }
  234. static struct rchan_callbacks blk_relay_callbacks = {
  235. .subbuf_start = blk_subbuf_start_callback,
  236. .create_buf_file = blk_create_buf_file_callback,
  237. .remove_buf_file = blk_remove_buf_file_callback,
  238. };
  239. /*
  240. * Setup everything required to start tracing
  241. */
  242. static int blk_trace_setup(request_queue_t *q, struct block_device *bdev,
  243. char __user *arg)
  244. {
  245. struct blk_user_trace_setup buts;
  246. struct blk_trace *old_bt, *bt = NULL;
  247. struct dentry *dir = NULL;
  248. char b[BDEVNAME_SIZE];
  249. int ret, i;
  250. if (copy_from_user(&buts, arg, sizeof(buts)))
  251. return -EFAULT;
  252. if (!buts.buf_size || !buts.buf_nr)
  253. return -EINVAL;
  254. strcpy(buts.name, bdevname(bdev, b));
  255. /*
  256. * some device names have larger paths - convert the slashes
  257. * to underscores for this to work as expected
  258. */
  259. for (i = 0; i < strlen(buts.name); i++)
  260. if (buts.name[i] == '/')
  261. buts.name[i] = '_';
  262. if (copy_to_user(arg, &buts, sizeof(buts)))
  263. return -EFAULT;
  264. ret = -ENOMEM;
  265. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  266. if (!bt)
  267. goto err;
  268. bt->sequence = alloc_percpu(unsigned long);
  269. if (!bt->sequence)
  270. goto err;
  271. ret = -ENOENT;
  272. dir = blk_create_tree(buts.name);
  273. if (!dir)
  274. goto err;
  275. bt->dir = dir;
  276. bt->dev = bdev->bd_dev;
  277. atomic_set(&bt->dropped, 0);
  278. ret = -EIO;
  279. bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
  280. if (!bt->dropped_file)
  281. goto err;
  282. bt->rchan = relay_open("trace", dir, buts.buf_size, buts.buf_nr, &blk_relay_callbacks);
  283. if (!bt->rchan)
  284. goto err;
  285. bt->rchan->private_data = bt;
  286. bt->act_mask = buts.act_mask;
  287. if (!bt->act_mask)
  288. bt->act_mask = (u16) -1;
  289. bt->start_lba = buts.start_lba;
  290. bt->end_lba = buts.end_lba;
  291. if (!bt->end_lba)
  292. bt->end_lba = -1ULL;
  293. bt->pid = buts.pid;
  294. bt->trace_state = Blktrace_setup;
  295. ret = -EBUSY;
  296. old_bt = xchg(&q->blk_trace, bt);
  297. if (old_bt) {
  298. (void) xchg(&q->blk_trace, old_bt);
  299. goto err;
  300. }
  301. return 0;
  302. err:
  303. if (dir)
  304. blk_remove_tree(dir);
  305. if (bt) {
  306. if (bt->dropped_file)
  307. debugfs_remove(bt->dropped_file);
  308. if (bt->sequence)
  309. free_percpu(bt->sequence);
  310. if (bt->rchan)
  311. relay_close(bt->rchan);
  312. kfree(bt);
  313. }
  314. return ret;
  315. }
  316. static int blk_trace_startstop(request_queue_t *q, int start)
  317. {
  318. struct blk_trace *bt;
  319. int ret;
  320. if ((bt = q->blk_trace) == NULL)
  321. return -EINVAL;
  322. /*
  323. * For starting a trace, we can transition from a setup or stopped
  324. * trace. For stopping a trace, the state must be running
  325. */
  326. ret = -EINVAL;
  327. if (start) {
  328. if (bt->trace_state == Blktrace_setup ||
  329. bt->trace_state == Blktrace_stopped) {
  330. blktrace_seq++;
  331. smp_mb();
  332. bt->trace_state = Blktrace_running;
  333. ret = 0;
  334. }
  335. } else {
  336. if (bt->trace_state == Blktrace_running) {
  337. bt->trace_state = Blktrace_stopped;
  338. relay_flush(bt->rchan);
  339. ret = 0;
  340. }
  341. }
  342. return ret;
  343. }
  344. /**
  345. * blk_trace_ioctl: - handle the ioctls associated with tracing
  346. * @bdev: the block device
  347. * @cmd: the ioctl cmd
  348. * @arg: the argument data, if any
  349. *
  350. **/
  351. int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
  352. {
  353. request_queue_t *q;
  354. int ret, start = 0;
  355. q = bdev_get_queue(bdev);
  356. if (!q)
  357. return -ENXIO;
  358. mutex_lock(&bdev->bd_mutex);
  359. switch (cmd) {
  360. case BLKTRACESETUP:
  361. ret = blk_trace_setup(q, bdev, arg);
  362. break;
  363. case BLKTRACESTART:
  364. start = 1;
  365. case BLKTRACESTOP:
  366. ret = blk_trace_startstop(q, start);
  367. break;
  368. case BLKTRACETEARDOWN:
  369. ret = blk_trace_remove(q);
  370. break;
  371. default:
  372. ret = -ENOTTY;
  373. break;
  374. }
  375. mutex_unlock(&bdev->bd_mutex);
  376. return ret;
  377. }
  378. /**
  379. * blk_trace_shutdown: - stop and cleanup trace structures
  380. * @q: the request queue associated with the device
  381. *
  382. **/
  383. void blk_trace_shutdown(request_queue_t *q)
  384. {
  385. if (q->blk_trace) {
  386. blk_trace_startstop(q, 0);
  387. blk_trace_remove(q);
  388. }
  389. }
  390. /*
  391. * Average offset over two calls to sched_clock() with a gettimeofday()
  392. * in the middle
  393. */
  394. static void blk_check_time(unsigned long long *t)
  395. {
  396. unsigned long long a, b;
  397. struct timeval tv;
  398. a = sched_clock();
  399. do_gettimeofday(&tv);
  400. b = sched_clock();
  401. *t = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
  402. *t -= (a + b) / 2;
  403. }
  404. /*
  405. * calibrate our inter-CPU timings
  406. */
  407. static void blk_trace_check_cpu_time(void *data)
  408. {
  409. unsigned long long *t;
  410. int cpu = get_cpu();
  411. t = &per_cpu(blk_trace_cpu_offset, cpu);
  412. /*
  413. * Just call it twice, hopefully the second call will be cache hot
  414. * and a little more precise
  415. */
  416. blk_check_time(t);
  417. blk_check_time(t);
  418. put_cpu();
  419. }
  420. static void blk_trace_set_ht_offsets(void)
  421. {
  422. #if defined(CONFIG_SCHED_SMT)
  423. int cpu, i;
  424. /*
  425. * now make sure HT siblings have the same time offset
  426. */
  427. preempt_disable();
  428. for_each_online_cpu(cpu) {
  429. unsigned long long *cpu_off, *sibling_off;
  430. for_each_cpu_mask(i, cpu_sibling_map[cpu]) {
  431. if (i == cpu)
  432. continue;
  433. cpu_off = &per_cpu(blk_trace_cpu_offset, cpu);
  434. sibling_off = &per_cpu(blk_trace_cpu_offset, i);
  435. *sibling_off = *cpu_off;
  436. }
  437. }
  438. preempt_enable();
  439. #endif
  440. }
  441. static __init int blk_trace_init(void)
  442. {
  443. mutex_init(&blk_tree_mutex);
  444. on_each_cpu(blk_trace_check_cpu_time, NULL, 1, 1);
  445. blk_trace_set_ht_offsets();
  446. return 0;
  447. }
  448. module_init(blk_trace_init);