blktrace.c 12 KB

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