blktrace.c 12 KB

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