aboutsummaryrefslogtreecommitdiffstats
path: root/block
diff options
context:
space:
mode:
Diffstat (limited to 'block')
-rw-r--r--block/Kconfig8
-rw-r--r--block/blk-barrier.c2
-rw-r--r--block/blk-core.c100
-rw-r--r--block/blk-integrity.c25
-rw-r--r--block/blk-sysfs.c58
-rw-r--r--block/blk.h8
-rw-r--r--block/blktrace.c728
-rw-r--r--block/cfq-iosched.c39
8 files changed, 861 insertions, 107 deletions
diff --git a/block/Kconfig b/block/Kconfig
index 0cbb3b88b59a..7cdaa1d72252 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -50,6 +50,8 @@ config BLK_DEV_IO_TRACE
50 select RELAY 50 select RELAY
51 select DEBUG_FS 51 select DEBUG_FS
52 select TRACEPOINTS 52 select TRACEPOINTS
53 select TRACING
54 select STACKTRACE
53 help 55 help
54 Say Y here if you want to be able to trace the block layer actions 56 Say Y here if you want to be able to trace the block layer actions
55 on a given queue. Tracing allows you to see any traffic happening 57 on a given queue. Tracing allows you to see any traffic happening
@@ -58,6 +60,12 @@ config BLK_DEV_IO_TRACE
58 60
59 git://git.kernel.dk/blktrace.git 61 git://git.kernel.dk/blktrace.git
60 62
63 Tracing also is possible using the ftrace interface, e.g.:
64
65 echo 1 > /sys/block/sda/sda1/trace/enable
66 echo blk > /sys/kernel/debug/tracing/current_tracer
67 cat /sys/kernel/debug/tracing/trace_pipe
68
61 If unsure, say N. 69 If unsure, say N.
62 70
63config BLK_DEV_BSG 71config BLK_DEV_BSG
diff --git a/block/blk-barrier.c b/block/blk-barrier.c
index 8eba4e43bb0c..f7dae57e6cab 100644
--- a/block/blk-barrier.c
+++ b/block/blk-barrier.c
@@ -302,7 +302,7 @@ static void bio_end_empty_barrier(struct bio *bio, int err)
302 * Description: 302 * Description:
303 * Issue a flush for the block device in question. Caller can supply 303 * Issue a flush for the block device in question. Caller can supply
304 * room for storing the error offset in case of a flush error, if they 304 * room for storing the error offset in case of a flush error, if they
305 * wish to. Caller must run wait_for_completion() on its own. 305 * wish to.
306 */ 306 */
307int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector) 307int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
308{ 308{
diff --git a/block/blk-core.c b/block/blk-core.c
index a824e49c0d0a..29bcfac6c688 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -64,11 +64,12 @@ static struct workqueue_struct *kblockd_workqueue;
64 64
65static void drive_stat_acct(struct request *rq, int new_io) 65static void drive_stat_acct(struct request *rq, int new_io)
66{ 66{
67 struct gendisk *disk = rq->rq_disk;
67 struct hd_struct *part; 68 struct hd_struct *part;
68 int rw = rq_data_dir(rq); 69 int rw = rq_data_dir(rq);
69 int cpu; 70 int cpu;
70 71
71 if (!blk_fs_request(rq) || !rq->rq_disk) 72 if (!blk_fs_request(rq) || !disk || !blk_do_io_stat(disk->queue))
72 return; 73 return;
73 74
74 cpu = part_stat_lock(); 75 cpu = part_stat_lock();
@@ -599,8 +600,7 @@ blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
599 q->request_fn = rfn; 600 q->request_fn = rfn;
600 q->prep_rq_fn = NULL; 601 q->prep_rq_fn = NULL;
601 q->unplug_fn = generic_unplug_device; 602 q->unplug_fn = generic_unplug_device;
602 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER | 603 q->queue_flags = QUEUE_FLAG_DEFAULT;
603 1 << QUEUE_FLAG_STACKABLE);
604 q->queue_lock = lock; 604 q->queue_lock = lock;
605 605
606 blk_queue_segment_boundary(q, BLK_SEG_BOUNDARY_MASK); 606 blk_queue_segment_boundary(q, BLK_SEG_BOUNDARY_MASK);
@@ -1125,6 +1125,8 @@ void init_request_from_bio(struct request *req, struct bio *bio)
1125 1125
1126 if (bio_sync(bio)) 1126 if (bio_sync(bio))
1127 req->cmd_flags |= REQ_RW_SYNC; 1127 req->cmd_flags |= REQ_RW_SYNC;
1128 if (bio_unplug(bio))
1129 req->cmd_flags |= REQ_UNPLUG;
1128 if (bio_rw_meta(bio)) 1130 if (bio_rw_meta(bio))
1129 req->cmd_flags |= REQ_RW_META; 1131 req->cmd_flags |= REQ_RW_META;
1130 1132
@@ -1141,6 +1143,7 @@ static int __make_request(struct request_queue *q, struct bio *bio)
1141 int el_ret, nr_sectors; 1143 int el_ret, nr_sectors;
1142 const unsigned short prio = bio_prio(bio); 1144 const unsigned short prio = bio_prio(bio);
1143 const int sync = bio_sync(bio); 1145 const int sync = bio_sync(bio);
1146 const int unplug = bio_unplug(bio);
1144 int rw_flags; 1147 int rw_flags;
1145 1148
1146 nr_sectors = bio_sectors(bio); 1149 nr_sectors = bio_sectors(bio);
@@ -1244,7 +1247,7 @@ get_rq:
1244 blk_plug_device(q); 1247 blk_plug_device(q);
1245 add_request(q, req); 1248 add_request(q, req);
1246out: 1249out:
1247 if (sync || blk_queue_nonrot(q)) 1250 if (unplug || blk_queue_nonrot(q))
1248 __generic_unplug_device(q); 1251 __generic_unplug_device(q);
1249 spin_unlock_irq(q->queue_lock); 1252 spin_unlock_irq(q->queue_lock);
1250 return 0; 1253 return 0;
@@ -1448,6 +1451,11 @@ static inline void __generic_make_request(struct bio *bio)
1448 err = -EOPNOTSUPP; 1451 err = -EOPNOTSUPP;
1449 goto end_io; 1452 goto end_io;
1450 } 1453 }
1454 if (bio_barrier(bio) && bio_has_data(bio) &&
1455 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1456 err = -EOPNOTSUPP;
1457 goto end_io;
1458 }
1451 1459
1452 ret = q->make_request_fn(q, bio); 1460 ret = q->make_request_fn(q, bio);
1453 } while (ret); 1461 } while (ret);
@@ -1655,6 +1663,55 @@ void blkdev_dequeue_request(struct request *req)
1655} 1663}
1656EXPORT_SYMBOL(blkdev_dequeue_request); 1664EXPORT_SYMBOL(blkdev_dequeue_request);
1657 1665
1666static void blk_account_io_completion(struct request *req, unsigned int bytes)
1667{
1668 struct gendisk *disk = req->rq_disk;
1669
1670 if (!disk || !blk_do_io_stat(disk->queue))
1671 return;
1672
1673 if (blk_fs_request(req)) {
1674 const int rw = rq_data_dir(req);
1675 struct hd_struct *part;
1676 int cpu;
1677
1678 cpu = part_stat_lock();
1679 part = disk_map_sector_rcu(req->rq_disk, req->sector);
1680 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1681 part_stat_unlock();
1682 }
1683}
1684
1685static void blk_account_io_done(struct request *req)
1686{
1687 struct gendisk *disk = req->rq_disk;
1688
1689 if (!disk || !blk_do_io_stat(disk->queue))
1690 return;
1691
1692 /*
1693 * Account IO completion. bar_rq isn't accounted as a normal
1694 * IO on queueing nor completion. Accounting the containing
1695 * request is enough.
1696 */
1697 if (blk_fs_request(req) && req != &req->q->bar_rq) {
1698 unsigned long duration = jiffies - req->start_time;
1699 const int rw = rq_data_dir(req);
1700 struct hd_struct *part;
1701 int cpu;
1702
1703 cpu = part_stat_lock();
1704 part = disk_map_sector_rcu(disk, req->sector);
1705
1706 part_stat_inc(cpu, part, ios[rw]);
1707 part_stat_add(cpu, part, ticks[rw], duration);
1708 part_round_stats(cpu, part);
1709 part_dec_in_flight(part);
1710
1711 part_stat_unlock();
1712 }
1713}
1714
1658/** 1715/**
1659 * __end_that_request_first - end I/O on a request 1716 * __end_that_request_first - end I/O on a request
1660 * @req: the request being processed 1717 * @req: the request being processed
@@ -1690,16 +1747,7 @@ static int __end_that_request_first(struct request *req, int error,
1690 (unsigned long long)req->sector); 1747 (unsigned long long)req->sector);
1691 } 1748 }
1692 1749
1693 if (blk_fs_request(req) && req->rq_disk) { 1750 blk_account_io_completion(req, nr_bytes);
1694 const int rw = rq_data_dir(req);
1695 struct hd_struct *part;
1696 int cpu;
1697
1698 cpu = part_stat_lock();
1699 part = disk_map_sector_rcu(req->rq_disk, req->sector);
1700 part_stat_add(cpu, part, sectors[rw], nr_bytes >> 9);
1701 part_stat_unlock();
1702 }
1703 1751
1704 total_bytes = bio_nbytes = 0; 1752 total_bytes = bio_nbytes = 0;
1705 while ((bio = req->bio) != NULL) { 1753 while ((bio = req->bio) != NULL) {
@@ -1779,8 +1827,6 @@ static int __end_that_request_first(struct request *req, int error,
1779 */ 1827 */
1780static void end_that_request_last(struct request *req, int error) 1828static void end_that_request_last(struct request *req, int error)
1781{ 1829{
1782 struct gendisk *disk = req->rq_disk;
1783
1784 if (blk_rq_tagged(req)) 1830 if (blk_rq_tagged(req))
1785 blk_queue_end_tag(req->q, req); 1831 blk_queue_end_tag(req->q, req);
1786 1832
@@ -1792,27 +1838,7 @@ static void end_that_request_last(struct request *req, int error)
1792 1838
1793 blk_delete_timer(req); 1839 blk_delete_timer(req);
1794 1840
1795 /* 1841 blk_account_io_done(req);
1796 * Account IO completion. bar_rq isn't accounted as a normal
1797 * IO on queueing nor completion. Accounting the containing
1798 * request is enough.
1799 */
1800 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1801 unsigned long duration = jiffies - req->start_time;
1802 const int rw = rq_data_dir(req);
1803 struct hd_struct *part;
1804 int cpu;
1805
1806 cpu = part_stat_lock();
1807 part = disk_map_sector_rcu(disk, req->sector);
1808
1809 part_stat_inc(cpu, part, ios[rw]);
1810 part_stat_add(cpu, part, ticks[rw], duration);
1811 part_round_stats(cpu, part);
1812 part_dec_in_flight(part);
1813
1814 part_stat_unlock();
1815 }
1816 1842
1817 if (req->end_io) 1843 if (req->end_io)
1818 req->end_io(req, error); 1844 req->end_io(req, error);
diff --git a/block/blk-integrity.c b/block/blk-integrity.c
index 61a8e2f8fdd0..91fa8e06b6a5 100644
--- a/block/blk-integrity.c
+++ b/block/blk-integrity.c
@@ -309,24 +309,24 @@ static struct kobj_type integrity_ktype = {
309/** 309/**
310 * blk_integrity_register - Register a gendisk as being integrity-capable 310 * blk_integrity_register - Register a gendisk as being integrity-capable
311 * @disk: struct gendisk pointer to make integrity-aware 311 * @disk: struct gendisk pointer to make integrity-aware
312 * @template: integrity profile 312 * @template: optional integrity profile to register
313 * 313 *
314 * Description: When a device needs to advertise itself as being able 314 * Description: When a device needs to advertise itself as being able
315 * to send/receive integrity metadata it must use this function to 315 * to send/receive integrity metadata it must use this function to
316 * register the capability with the block layer. The template is a 316 * register the capability with the block layer. The template is a
317 * blk_integrity struct with values appropriate for the underlying 317 * blk_integrity struct with values appropriate for the underlying
318 * hardware. See Documentation/block/data-integrity.txt. 318 * hardware. If template is NULL the new profile is allocated but
319 * not filled out. See Documentation/block/data-integrity.txt.
319 */ 320 */
320int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template) 321int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
321{ 322{
322 struct blk_integrity *bi; 323 struct blk_integrity *bi;
323 324
324 BUG_ON(disk == NULL); 325 BUG_ON(disk == NULL);
325 BUG_ON(template == NULL);
326 326
327 if (disk->integrity == NULL) { 327 if (disk->integrity == NULL) {
328 bi = kmem_cache_alloc(integrity_cachep, 328 bi = kmem_cache_alloc(integrity_cachep,
329 GFP_KERNEL | __GFP_ZERO); 329 GFP_KERNEL | __GFP_ZERO);
330 if (!bi) 330 if (!bi)
331 return -1; 331 return -1;
332 332
@@ -346,13 +346,16 @@ int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
346 bi = disk->integrity; 346 bi = disk->integrity;
347 347
348 /* Use the provided profile as template */ 348 /* Use the provided profile as template */
349 bi->name = template->name; 349 if (template != NULL) {
350 bi->generate_fn = template->generate_fn; 350 bi->name = template->name;
351 bi->verify_fn = template->verify_fn; 351 bi->generate_fn = template->generate_fn;
352 bi->tuple_size = template->tuple_size; 352 bi->verify_fn = template->verify_fn;
353 bi->set_tag_fn = template->set_tag_fn; 353 bi->tuple_size = template->tuple_size;
354 bi->get_tag_fn = template->get_tag_fn; 354 bi->set_tag_fn = template->set_tag_fn;
355 bi->tag_size = template->tag_size; 355 bi->get_tag_fn = template->get_tag_fn;
356 bi->tag_size = template->tag_size;
357 } else
358 bi->name = "unsupported";
356 359
357 return 0; 360 return 0;
358} 361}
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index a29cb788e408..e29ddfc73cf4 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -130,6 +130,27 @@ static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
130 return queue_var_show(max_hw_sectors_kb, (page)); 130 return queue_var_show(max_hw_sectors_kb, (page));
131} 131}
132 132
133static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
134{
135 return queue_var_show(!blk_queue_nonrot(q), page);
136}
137
138static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
139 size_t count)
140{
141 unsigned long nm;
142 ssize_t ret = queue_var_store(&nm, page, count);
143
144 spin_lock_irq(q->queue_lock);
145 if (nm)
146 queue_flag_clear(QUEUE_FLAG_NONROT, q);
147 else
148 queue_flag_set(QUEUE_FLAG_NONROT, q);
149 spin_unlock_irq(q->queue_lock);
150
151 return ret;
152}
153
133static ssize_t queue_nomerges_show(struct request_queue *q, char *page) 154static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
134{ 155{
135 return queue_var_show(blk_queue_nomerges(q), page); 156 return queue_var_show(blk_queue_nomerges(q), page);
@@ -146,8 +167,8 @@ static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
146 queue_flag_set(QUEUE_FLAG_NOMERGES, q); 167 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
147 else 168 else
148 queue_flag_clear(QUEUE_FLAG_NOMERGES, q); 169 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
149
150 spin_unlock_irq(q->queue_lock); 170 spin_unlock_irq(q->queue_lock);
171
151 return ret; 172 return ret;
152} 173}
153 174
@@ -176,6 +197,27 @@ queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
176 return ret; 197 return ret;
177} 198}
178 199
200static ssize_t queue_iostats_show(struct request_queue *q, char *page)
201{
202 return queue_var_show(blk_queue_io_stat(q), page);
203}
204
205static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
206 size_t count)
207{
208 unsigned long stats;
209 ssize_t ret = queue_var_store(&stats, page, count);
210
211 spin_lock_irq(q->queue_lock);
212 if (stats)
213 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
214 else
215 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
216 spin_unlock_irq(q->queue_lock);
217
218 return ret;
219}
220
179static struct queue_sysfs_entry queue_requests_entry = { 221static struct queue_sysfs_entry queue_requests_entry = {
180 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, 222 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
181 .show = queue_requests_show, 223 .show = queue_requests_show,
@@ -210,6 +252,12 @@ static struct queue_sysfs_entry queue_hw_sector_size_entry = {
210 .show = queue_hw_sector_size_show, 252 .show = queue_hw_sector_size_show,
211}; 253};
212 254
255static struct queue_sysfs_entry queue_nonrot_entry = {
256 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
257 .show = queue_nonrot_show,
258 .store = queue_nonrot_store,
259};
260
213static struct queue_sysfs_entry queue_nomerges_entry = { 261static struct queue_sysfs_entry queue_nomerges_entry = {
214 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, 262 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
215 .show = queue_nomerges_show, 263 .show = queue_nomerges_show,
@@ -222,6 +270,12 @@ static struct queue_sysfs_entry queue_rq_affinity_entry = {
222 .store = queue_rq_affinity_store, 270 .store = queue_rq_affinity_store,
223}; 271};
224 272
273static struct queue_sysfs_entry queue_iostats_entry = {
274 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
275 .show = queue_iostats_show,
276 .store = queue_iostats_store,
277};
278
225static struct attribute *default_attrs[] = { 279static struct attribute *default_attrs[] = {
226 &queue_requests_entry.attr, 280 &queue_requests_entry.attr,
227 &queue_ra_entry.attr, 281 &queue_ra_entry.attr,
@@ -229,8 +283,10 @@ static struct attribute *default_attrs[] = {
229 &queue_max_sectors_entry.attr, 283 &queue_max_sectors_entry.attr,
230 &queue_iosched_entry.attr, 284 &queue_iosched_entry.attr,
231 &queue_hw_sector_size_entry.attr, 285 &queue_hw_sector_size_entry.attr,
286 &queue_nonrot_entry.attr,
232 &queue_nomerges_entry.attr, 287 &queue_nomerges_entry.attr,
233 &queue_rq_affinity_entry.attr, 288 &queue_rq_affinity_entry.attr,
289 &queue_iostats_entry.attr,
234 NULL, 290 NULL,
235}; 291};
236 292
diff --git a/block/blk.h b/block/blk.h
index 6e1ed40534e9..0dce92c37496 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -108,4 +108,12 @@ static inline int blk_cpu_to_group(int cpu)
108#endif 108#endif
109} 109}
110 110
111static inline int blk_do_io_stat(struct request_queue *q)
112{
113 if (q)
114 return blk_queue_io_stat(q);
115
116 return 0;
117}
118
111#endif 119#endif
diff --git a/block/blktrace.c b/block/blktrace.c
index b0a2cae886db..3f25425ade12 100644
--- a/block/blktrace.c
+++ b/block/blktrace.c
@@ -25,9 +25,27 @@
25#include <linux/time.h> 25#include <linux/time.h>
26#include <trace/block.h> 26#include <trace/block.h>
27#include <asm/uaccess.h> 27#include <asm/uaccess.h>
28#include <../kernel/trace/trace_output.h>
28 29
29static unsigned int blktrace_seq __read_mostly = 1; 30static unsigned int blktrace_seq __read_mostly = 1;
30 31
32static struct trace_array *blk_tr;
33static int __read_mostly blk_tracer_enabled;
34
35/* Select an alternative, minimalistic output than the original one */
36#define TRACE_BLK_OPT_CLASSIC 0x1
37
38static struct tracer_opt blk_tracer_opts[] = {
39 /* Default disable the minimalistic output */
40 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
41 { }
42};
43
44static struct tracer_flags blk_tracer_flags = {
45 .val = 0,
46 .opts = blk_tracer_opts,
47};
48
31/* Global reference count of probes */ 49/* Global reference count of probes */
32static DEFINE_MUTEX(blk_probe_mutex); 50static DEFINE_MUTEX(blk_probe_mutex);
33static atomic_t blk_probes_ref = ATOMIC_INIT(0); 51static atomic_t blk_probes_ref = ATOMIC_INIT(0);
@@ -43,6 +61,9 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action,
43{ 61{
44 struct blk_io_trace *t; 62 struct blk_io_trace *t;
45 63
64 if (!bt->rchan)
65 return;
66
46 t = relay_reserve(bt->rchan, sizeof(*t) + len); 67 t = relay_reserve(bt->rchan, sizeof(*t) + len);
47 if (t) { 68 if (t) {
48 const int cpu = smp_processor_id(); 69 const int cpu = smp_processor_id();
@@ -90,6 +111,16 @@ void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
90 unsigned long flags; 111 unsigned long flags;
91 char *buf; 112 char *buf;
92 113
114 if (blk_tr) {
115 va_start(args, fmt);
116 ftrace_vprintk(fmt, args);
117 va_end(args);
118 return;
119 }
120
121 if (!bt->msg_data)
122 return;
123
93 local_irq_save(flags); 124 local_irq_save(flags);
94 buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); 125 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
95 va_start(args, fmt); 126 va_start(args, fmt);
@@ -131,13 +162,15 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
131 int rw, u32 what, int error, int pdu_len, void *pdu_data) 162 int rw, u32 what, int error, int pdu_len, void *pdu_data)
132{ 163{
133 struct task_struct *tsk = current; 164 struct task_struct *tsk = current;
165 struct ring_buffer_event *event = NULL;
134 struct blk_io_trace *t; 166 struct blk_io_trace *t;
135 unsigned long flags; 167 unsigned long flags;
136 unsigned long *sequence; 168 unsigned long *sequence;
137 pid_t pid; 169 pid_t pid;
138 int cpu; 170 int cpu, pc = 0;
139 171
140 if (unlikely(bt->trace_state != Blktrace_running)) 172 if (unlikely(bt->trace_state != Blktrace_running ||
173 !blk_tracer_enabled))
141 return; 174 return;
142 175
143 what |= ddir_act[rw & WRITE]; 176 what |= ddir_act[rw & WRITE];
@@ -150,6 +183,24 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
150 pid = tsk->pid; 183 pid = tsk->pid;
151 if (unlikely(act_log_check(bt, what, sector, pid))) 184 if (unlikely(act_log_check(bt, what, sector, pid)))
152 return; 185 return;
186 cpu = raw_smp_processor_id();
187
188 if (blk_tr) {
189 struct trace_entry *ent;
190 tracing_record_cmdline(current);
191
192 event = ring_buffer_lock_reserve(blk_tr->buffer,
193 sizeof(*t) + pdu_len, &flags);
194 if (!event)
195 return;
196
197 ent = ring_buffer_event_data(event);
198 t = (struct blk_io_trace *)ent;
199 pc = preempt_count();
200 tracing_generic_entry_update(ent, 0, pc);
201 ent->type = TRACE_BLK;
202 goto record_it;
203 }
153 204
154 /* 205 /*
155 * A word about the locking here - we disable interrupts to reserve 206 * A word about the locking here - we disable interrupts to reserve
@@ -163,23 +214,33 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
163 214
164 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len); 215 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
165 if (t) { 216 if (t) {
166 cpu = smp_processor_id();
167 sequence = per_cpu_ptr(bt->sequence, cpu); 217 sequence = per_cpu_ptr(bt->sequence, cpu);
168 218
169 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; 219 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
170 t->sequence = ++(*sequence); 220 t->sequence = ++(*sequence);
171 t->time = ktime_to_ns(ktime_get()); 221 t->time = ktime_to_ns(ktime_get());
222 t->cpu = cpu;
223 t->pid = pid;
224record_it:
172 t->sector = sector; 225 t->sector = sector;
173 t->bytes = bytes; 226 t->bytes = bytes;
174 t->action = what; 227 t->action = what;
175 t->pid = pid;
176 t->device = bt->dev; 228 t->device = bt->dev;
177 t->cpu = cpu;
178 t->error = error; 229 t->error = error;
179 t->pdu_len = pdu_len; 230 t->pdu_len = pdu_len;
180 231
181 if (pdu_len) 232 if (pdu_len)
182 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); 233 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
234
235 if (blk_tr) {
236 ring_buffer_unlock_commit(blk_tr->buffer, event, flags);
237 if (pid != 0 &&
238 !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) &&
239 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
240 __trace_stack(blk_tr, NULL, flags, 5, pc);
241 trace_wake_up();
242 return;
243 }
183 } 244 }
184 245
185 local_irq_restore(flags); 246 local_irq_restore(flags);
@@ -187,59 +248,12 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
187 248
188static struct dentry *blk_tree_root; 249static struct dentry *blk_tree_root;
189static DEFINE_MUTEX(blk_tree_mutex); 250static DEFINE_MUTEX(blk_tree_mutex);
190static unsigned int root_users;
191
192static inline void blk_remove_root(void)
193{
194 if (blk_tree_root) {
195 debugfs_remove(blk_tree_root);
196 blk_tree_root = NULL;
197 }
198}
199
200static void blk_remove_tree(struct dentry *dir)
201{
202 mutex_lock(&blk_tree_mutex);
203 debugfs_remove(dir);
204 if (--root_users == 0)
205 blk_remove_root();
206 mutex_unlock(&blk_tree_mutex);
207}
208
209static struct dentry *blk_create_tree(const char *blk_name)
210{
211 struct dentry *dir = NULL;
212 int created = 0;
213
214 mutex_lock(&blk_tree_mutex);
215
216 if (!blk_tree_root) {
217 blk_tree_root = debugfs_create_dir("block", NULL);
218 if (!blk_tree_root)
219 goto err;
220 created = 1;
221 }
222
223 dir = debugfs_create_dir(blk_name, blk_tree_root);
224 if (dir)
225 root_users++;
226 else {
227 /* Delete root only if we created it */
228 if (created)
229 blk_remove_root();
230 }
231
232err:
233 mutex_unlock(&blk_tree_mutex);
234 return dir;
235}
236 251
237static void blk_trace_cleanup(struct blk_trace *bt) 252static void blk_trace_cleanup(struct blk_trace *bt)
238{ 253{
239 relay_close(bt->rchan);
240 debugfs_remove(bt->msg_file); 254 debugfs_remove(bt->msg_file);
241 debugfs_remove(bt->dropped_file); 255 debugfs_remove(bt->dropped_file);
242 blk_remove_tree(bt->dir); 256 relay_close(bt->rchan);
243 free_percpu(bt->sequence); 257 free_percpu(bt->sequence);
244 free_percpu(bt->msg_data); 258 free_percpu(bt->msg_data);
245 kfree(bt); 259 kfree(bt);
@@ -346,7 +360,18 @@ static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
346 360
347static int blk_remove_buf_file_callback(struct dentry *dentry) 361static int blk_remove_buf_file_callback(struct dentry *dentry)
348{ 362{
363 struct dentry *parent = dentry->d_parent;
349 debugfs_remove(dentry); 364 debugfs_remove(dentry);
365
366 /*
367 * this will fail for all but the last file, but that is ok. what we
368 * care about is the top level buts->name directory going away, when
369 * the last trace file is gone. Then we don't have to rmdir() that
370 * manually on trace stop, so it nicely solves the issue with
371 * force killing of running traces.
372 */
373
374 debugfs_remove(parent);
350 return 0; 375 return 0;
351} 376}
352 377
@@ -404,7 +429,15 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
404 goto err; 429 goto err;
405 430
406 ret = -ENOENT; 431 ret = -ENOENT;
407 dir = blk_create_tree(buts->name); 432
433 if (!blk_tree_root) {
434 blk_tree_root = debugfs_create_dir("block", NULL);
435 if (!blk_tree_root)
436 return -ENOMEM;
437 }
438
439 dir = debugfs_create_dir(buts->name, blk_tree_root);
440
408 if (!dir) 441 if (!dir)
409 goto err; 442 goto err;
410 443
@@ -458,8 +491,6 @@ probe_err:
458 atomic_dec(&blk_probes_ref); 491 atomic_dec(&blk_probes_ref);
459 mutex_unlock(&blk_probe_mutex); 492 mutex_unlock(&blk_probe_mutex);
460err: 493err:
461 if (dir)
462 blk_remove_tree(dir);
463 if (bt) { 494 if (bt) {
464 if (bt->msg_file) 495 if (bt->msg_file)
465 debugfs_remove(bt->msg_file); 496 debugfs_remove(bt->msg_file);
@@ -888,3 +919,588 @@ static void blk_unregister_tracepoints(void)
888 919
889 tracepoint_synchronize_unregister(); 920 tracepoint_synchronize_unregister();
890} 921}
922
923/*
924 * struct blk_io_tracer formatting routines
925 */
926
927static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
928{
929 int i = 0;
930
931 if (t->action & BLK_TC_DISCARD)
932 rwbs[i++] = 'D';
933 else if (t->action & BLK_TC_WRITE)
934 rwbs[i++] = 'W';
935 else if (t->bytes)
936 rwbs[i++] = 'R';
937 else
938 rwbs[i++] = 'N';
939
940 if (t->action & BLK_TC_AHEAD)
941 rwbs[i++] = 'A';
942 if (t->action & BLK_TC_BARRIER)
943 rwbs[i++] = 'B';
944 if (t->action & BLK_TC_SYNC)
945 rwbs[i++] = 'S';
946 if (t->action & BLK_TC_META)
947 rwbs[i++] = 'M';
948
949 rwbs[i] = '\0';
950}
951
952static inline
953const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
954{
955 return (const struct blk_io_trace *)ent;
956}
957
958static inline const void *pdu_start(const struct trace_entry *ent)
959{
960 return te_blk_io_trace(ent) + 1;
961}
962
963static inline u32 t_sec(const struct trace_entry *ent)
964{
965 return te_blk_io_trace(ent)->bytes >> 9;
966}
967
968static inline unsigned long long t_sector(const struct trace_entry *ent)
969{
970 return te_blk_io_trace(ent)->sector;
971}
972
973static inline __u16 t_error(const struct trace_entry *ent)
974{
975 return te_blk_io_trace(ent)->sector;
976}
977
978static __u64 get_pdu_int(const struct trace_entry *ent)
979{
980 const __u64 *val = pdu_start(ent);
981 return be64_to_cpu(*val);
982}
983
984static void get_pdu_remap(const struct trace_entry *ent,
985 struct blk_io_trace_remap *r)
986{
987 const struct blk_io_trace_remap *__r = pdu_start(ent);
988 __u64 sector = __r->sector;
989
990 r->device = be32_to_cpu(__r->device);
991 r->device_from = be32_to_cpu(__r->device_from);
992 r->sector = be64_to_cpu(sector);
993}
994
995static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
996{
997 char rwbs[6];
998 unsigned long long ts = ns2usecs(iter->ts);
999 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1000 unsigned secs = (unsigned long)ts;
1001 const struct trace_entry *ent = iter->ent;
1002 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1003
1004 fill_rwbs(rwbs, t);
1005
1006 return trace_seq_printf(&iter->seq,
1007 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1008 MAJOR(t->device), MINOR(t->device), iter->cpu,
1009 secs, usec_rem, ent->pid, act, rwbs);
1010}
1011
1012static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1013 const char *act)
1014{
1015 char rwbs[6];
1016 fill_rwbs(rwbs, t);
1017 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1018 MAJOR(t->device), MINOR(t->device), act, rwbs);
1019}
1020
1021static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1022{
1023 const char *cmd = trace_find_cmdline(ent->pid);
1024
1025 if (t_sec(ent))
1026 return trace_seq_printf(s, "%llu + %u [%s]\n",
1027 t_sector(ent), t_sec(ent), cmd);
1028 return trace_seq_printf(s, "[%s]\n", cmd);
1029}
1030
1031static int blk_log_with_error(struct trace_seq *s,
1032 const struct trace_entry *ent)
1033{
1034 if (t_sec(ent))
1035 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1036 t_sec(ent), t_error(ent));
1037 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1038}
1039
1040static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1041{
1042 struct blk_io_trace_remap r = { .device = 0, };
1043
1044 get_pdu_remap(ent, &r);
1045 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1046 t_sector(ent),
1047 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1048 (unsigned long long)r.sector);
1049}
1050
1051static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1052{
1053 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1054}
1055
1056static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1057{
1058 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1059 get_pdu_int(ent));
1060}
1061
1062static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1063{
1064 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1065 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1066}
1067
1068/*
1069 * struct tracer operations
1070 */
1071
1072static void blk_tracer_print_header(struct seq_file *m)
1073{
1074 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1075 return;
1076 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1077 "# | | | | | |\n");
1078}
1079
1080static void blk_tracer_start(struct trace_array *tr)
1081{
1082 tracing_reset_online_cpus(tr);
1083
1084 mutex_lock(&blk_probe_mutex);
1085 if (atomic_add_return(1, &blk_probes_ref) == 1)
1086 if (blk_register_tracepoints())
1087 atomic_dec(&blk_probes_ref);
1088 mutex_unlock(&blk_probe_mutex);
1089}
1090
1091static int blk_tracer_init(struct trace_array *tr)
1092{
1093 blk_tr = tr;
1094 blk_tracer_start(tr);
1095 mutex_lock(&blk_probe_mutex);
1096 blk_tracer_enabled++;
1097 mutex_unlock(&blk_probe_mutex);
1098 return 0;
1099}
1100
1101static void blk_tracer_stop(struct trace_array *tr)
1102{
1103 mutex_lock(&blk_probe_mutex);
1104 if (atomic_dec_and_test(&blk_probes_ref))
1105 blk_unregister_tracepoints();
1106 mutex_unlock(&blk_probe_mutex);
1107}
1108
1109static void blk_tracer_reset(struct trace_array *tr)
1110{
1111 if (!atomic_read(&blk_probes_ref))
1112 return;
1113
1114 mutex_lock(&blk_probe_mutex);
1115 blk_tracer_enabled--;
1116 WARN_ON(blk_tracer_enabled < 0);
1117 mutex_unlock(&blk_probe_mutex);
1118
1119 blk_tracer_stop(tr);
1120}
1121
1122static struct {
1123 const char *act[2];
1124 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1125} what2act[] __read_mostly = {
1126 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1127 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1128 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1129 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1130 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1131 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1132 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1133 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1134 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1135 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1136 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1137 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1138 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1139 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1140 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1141};
1142
1143static int blk_trace_event_print(struct trace_seq *s, struct trace_entry *ent,
1144 int flags)
1145{
1146 const struct blk_io_trace *t = (struct blk_io_trace *)ent;
1147 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1148 int ret;
1149
1150 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1151 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1152 else {
1153 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1154 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1155 if (ret)
1156 ret = what2act[what].print(s, ent);
1157 }
1158
1159 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1160}
1161
1162static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1163{
1164 const struct blk_io_trace *t;
1165 u16 what;
1166 int ret;
1167
1168 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1169 return TRACE_TYPE_UNHANDLED;
1170
1171 t = (const struct blk_io_trace *)iter->ent;
1172 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1173
1174 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1175 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1176 else {
1177 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1178 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1179 if (ret)
1180 ret = what2act[what].print(&iter->seq, iter->ent);
1181 }
1182
1183 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1184}
1185
1186static struct tracer blk_tracer __read_mostly = {
1187 .name = "blk",
1188 .init = blk_tracer_init,
1189 .reset = blk_tracer_reset,
1190 .start = blk_tracer_start,
1191 .stop = blk_tracer_stop,
1192 .print_header = blk_tracer_print_header,
1193 .print_line = blk_tracer_print_line,
1194 .flags = &blk_tracer_flags,
1195};
1196
1197static struct trace_event trace_blk_event = {
1198 .type = TRACE_BLK,
1199 .trace = blk_trace_event_print,
1200 .latency_trace = blk_trace_event_print,
1201 .raw = trace_nop_print,
1202 .hex = trace_nop_print,
1203 .binary = trace_nop_print,
1204};
1205
1206static int __init init_blk_tracer(void)
1207{
1208 if (!register_ftrace_event(&trace_blk_event)) {
1209 pr_warning("Warning: could not register block events\n");
1210 return 1;
1211 }
1212
1213 if (register_tracer(&blk_tracer) != 0) {
1214 pr_warning("Warning: could not register the block tracer\n");
1215 unregister_ftrace_event(&trace_blk_event);
1216 return 1;
1217 }
1218
1219 return 0;
1220}
1221
1222device_initcall(init_blk_tracer);
1223
1224static int blk_trace_remove_queue(struct request_queue *q)
1225{
1226 struct blk_trace *bt;
1227
1228 bt = xchg(&q->blk_trace, NULL);
1229 if (bt == NULL)
1230 return -EINVAL;
1231
1232 kfree(bt);
1233 return 0;
1234}
1235
1236/*
1237 * Setup everything required to start tracing
1238 */
1239static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1240{
1241 struct blk_trace *old_bt, *bt = NULL;
1242 int ret;
1243
1244 ret = -ENOMEM;
1245 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1246 if (!bt)
1247 goto err;
1248
1249 bt->dev = dev;
1250 bt->act_mask = (u16)-1;
1251 bt->end_lba = -1ULL;
1252 bt->trace_state = Blktrace_running;
1253
1254 old_bt = xchg(&q->blk_trace, bt);
1255 if (old_bt != NULL) {
1256 (void)xchg(&q->blk_trace, old_bt);
1257 kfree(bt);
1258 ret = -EBUSY;
1259 }
1260 return 0;
1261err:
1262 return ret;
1263}
1264
1265/*
1266 * sysfs interface to enable and configure tracing
1267 */
1268
1269static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1270 struct device_attribute *attr,
1271 char *buf)
1272{
1273 struct hd_struct *p = dev_to_part(dev);
1274 struct block_device *bdev;
1275 ssize_t ret = -ENXIO;
1276
1277 lock_kernel();
1278 bdev = bdget(part_devt(p));
1279 if (bdev != NULL) {
1280 struct request_queue *q = bdev_get_queue(bdev);
1281
1282 if (q != NULL) {
1283 mutex_lock(&bdev->bd_mutex);
1284 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1285 mutex_unlock(&bdev->bd_mutex);
1286 }
1287
1288 bdput(bdev);
1289 }
1290
1291 unlock_kernel();
1292 return ret;
1293}
1294
1295static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1296 struct device_attribute *attr,
1297 const char *buf, size_t count)
1298{
1299 struct block_device *bdev;
1300 struct request_queue *q;
1301 struct hd_struct *p;
1302 int value;
1303 ssize_t ret = -ENXIO;
1304
1305 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1306 goto out;
1307
1308 lock_kernel();
1309 p = dev_to_part(dev);
1310 bdev = bdget(part_devt(p));
1311 if (bdev == NULL)
1312 goto out_unlock_kernel;
1313
1314 q = bdev_get_queue(bdev);
1315 if (q == NULL)
1316 goto out_bdput;
1317
1318 mutex_lock(&bdev->bd_mutex);
1319 if (value)
1320 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1321 else
1322 ret = blk_trace_remove_queue(q);
1323 mutex_unlock(&bdev->bd_mutex);
1324
1325 if (ret == 0)
1326 ret = count;
1327out_bdput:
1328 bdput(bdev);
1329out_unlock_kernel:
1330 unlock_kernel();
1331out:
1332 return ret;
1333}
1334
1335static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1336 struct device_attribute *attr,
1337 char *buf);
1338static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1339 struct device_attribute *attr,
1340 const char *buf, size_t count);
1341#define BLK_TRACE_DEVICE_ATTR(_name) \
1342 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1343 sysfs_blk_trace_attr_show, \
1344 sysfs_blk_trace_attr_store)
1345
1346static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1347 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1348static BLK_TRACE_DEVICE_ATTR(act_mask);
1349static BLK_TRACE_DEVICE_ATTR(pid);
1350static BLK_TRACE_DEVICE_ATTR(start_lba);
1351static BLK_TRACE_DEVICE_ATTR(end_lba);
1352
1353static struct attribute *blk_trace_attrs[] = {
1354 &dev_attr_enable.attr,
1355 &dev_attr_act_mask.attr,
1356 &dev_attr_pid.attr,
1357 &dev_attr_start_lba.attr,
1358 &dev_attr_end_lba.attr,
1359 NULL
1360};
1361
1362struct attribute_group blk_trace_attr_group = {
1363 .name = "trace",
1364 .attrs = blk_trace_attrs,
1365};
1366
1367static int blk_str2act_mask(const char *str)
1368{
1369 int mask = 0;
1370 char *copy = kstrdup(str, GFP_KERNEL), *s;
1371
1372 if (copy == NULL)
1373 return -ENOMEM;
1374
1375 s = strstrip(copy);
1376
1377 while (1) {
1378 char *sep = strchr(s, ',');
1379
1380 if (sep != NULL)
1381 *sep = '\0';
1382
1383 if (strcasecmp(s, "barrier") == 0)
1384 mask |= BLK_TC_BARRIER;
1385 else if (strcasecmp(s, "complete") == 0)
1386 mask |= BLK_TC_COMPLETE;
1387 else if (strcasecmp(s, "fs") == 0)
1388 mask |= BLK_TC_FS;
1389 else if (strcasecmp(s, "issue") == 0)
1390 mask |= BLK_TC_ISSUE;
1391 else if (strcasecmp(s, "pc") == 0)
1392 mask |= BLK_TC_PC;
1393 else if (strcasecmp(s, "queue") == 0)
1394 mask |= BLK_TC_QUEUE;
1395 else if (strcasecmp(s, "read") == 0)
1396 mask |= BLK_TC_READ;
1397 else if (strcasecmp(s, "requeue") == 0)
1398 mask |= BLK_TC_REQUEUE;
1399 else if (strcasecmp(s, "sync") == 0)
1400 mask |= BLK_TC_SYNC;
1401 else if (strcasecmp(s, "write") == 0)
1402 mask |= BLK_TC_WRITE;
1403
1404 if (sep == NULL)
1405 break;
1406
1407 s = sep + 1;
1408 }
1409 kfree(copy);
1410
1411 return mask;
1412}
1413
1414static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1415 struct device_attribute *attr,
1416 char *buf)
1417{
1418 struct hd_struct *p = dev_to_part(dev);
1419 struct request_queue *q;
1420 struct block_device *bdev;
1421 ssize_t ret = -ENXIO;
1422
1423 lock_kernel();
1424 bdev = bdget(part_devt(p));
1425 if (bdev == NULL)
1426 goto out_unlock_kernel;
1427
1428 q = bdev_get_queue(bdev);
1429 if (q == NULL)
1430 goto out_bdput;
1431 mutex_lock(&bdev->bd_mutex);
1432 if (q->blk_trace == NULL)
1433 ret = sprintf(buf, "disabled\n");
1434 else if (attr == &dev_attr_act_mask)
1435 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1436 else if (attr == &dev_attr_pid)
1437 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1438 else if (attr == &dev_attr_start_lba)
1439 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1440 else if (attr == &dev_attr_end_lba)
1441 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1442 mutex_unlock(&bdev->bd_mutex);
1443out_bdput:
1444 bdput(bdev);
1445out_unlock_kernel:
1446 unlock_kernel();
1447 return ret;
1448}
1449
1450static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1451 struct device_attribute *attr,
1452 const char *buf, size_t count)
1453{
1454 struct block_device *bdev;
1455 struct request_queue *q;
1456 struct hd_struct *p;
1457 u64 value;
1458 ssize_t ret = -ENXIO;
1459
1460 if (count == 0)
1461 goto out;
1462
1463 if (attr == &dev_attr_act_mask) {
1464 if (sscanf(buf, "%llx", &value) != 1) {
1465 /* Assume it is a list of trace category names */
1466 value = blk_str2act_mask(buf);
1467 if (value < 0)
1468 goto out;
1469 }
1470 } else if (sscanf(buf, "%llu", &value) != 1)
1471 goto out;
1472
1473 lock_kernel();
1474 p = dev_to_part(dev);
1475 bdev = bdget(part_devt(p));
1476 if (bdev == NULL)
1477 goto out_unlock_kernel;
1478
1479 q = bdev_get_queue(bdev);
1480 if (q == NULL)
1481 goto out_bdput;
1482
1483 mutex_lock(&bdev->bd_mutex);
1484 ret = 0;
1485 if (q->blk_trace == NULL)
1486 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1487
1488 if (ret == 0) {
1489 if (attr == &dev_attr_act_mask)
1490 q->blk_trace->act_mask = value;
1491 else if (attr == &dev_attr_pid)
1492 q->blk_trace->pid = value;
1493 else if (attr == &dev_attr_start_lba)
1494 q->blk_trace->start_lba = value;
1495 else if (attr == &dev_attr_end_lba)
1496 q->blk_trace->end_lba = value;
1497 ret = count;
1498 }
1499 mutex_unlock(&bdev->bd_mutex);
1500out_bdput:
1501 bdput(bdev);
1502out_unlock_kernel:
1503 unlock_kernel();
1504out:
1505 return ret;
1506}
diff --git a/block/cfq-iosched.c b/block/cfq-iosched.c
index e8525fa72823..664ebfd092ec 100644
--- a/block/cfq-iosched.c
+++ b/block/cfq-iosched.c
@@ -84,6 +84,11 @@ struct cfq_data {
84 */ 84 */
85 struct cfq_rb_root service_tree; 85 struct cfq_rb_root service_tree;
86 unsigned int busy_queues; 86 unsigned int busy_queues;
87 /*
88 * Used to track any pending rt requests so we can pre-empt current
89 * non-RT cfqq in service when this value is non-zero.
90 */
91 unsigned int busy_rt_queues;
87 92
88 int rq_in_driver; 93 int rq_in_driver;
89 int sync_flight; 94 int sync_flight;
@@ -562,6 +567,8 @@ static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
562 BUG_ON(cfq_cfqq_on_rr(cfqq)); 567 BUG_ON(cfq_cfqq_on_rr(cfqq));
563 cfq_mark_cfqq_on_rr(cfqq); 568 cfq_mark_cfqq_on_rr(cfqq);
564 cfqd->busy_queues++; 569 cfqd->busy_queues++;
570 if (cfq_class_rt(cfqq))
571 cfqd->busy_rt_queues++;
565 572
566 cfq_resort_rr_list(cfqd, cfqq); 573 cfq_resort_rr_list(cfqd, cfqq);
567} 574}
@@ -581,6 +588,8 @@ static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
581 588
582 BUG_ON(!cfqd->busy_queues); 589 BUG_ON(!cfqd->busy_queues);
583 cfqd->busy_queues--; 590 cfqd->busy_queues--;
591 if (cfq_class_rt(cfqq))
592 cfqd->busy_rt_queues--;
584} 593}
585 594
586/* 595/*
@@ -1005,6 +1014,20 @@ static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
1005 goto expire; 1014 goto expire;
1006 1015
1007 /* 1016 /*
1017 * If we have a RT cfqq waiting, then we pre-empt the current non-rt
1018 * cfqq.
1019 */
1020 if (!cfq_class_rt(cfqq) && cfqd->busy_rt_queues) {
1021 /*
1022 * We simulate this as cfqq timed out so that it gets to bank
1023 * the remaining of its time slice.
1024 */
1025 cfq_log_cfqq(cfqd, cfqq, "preempt");
1026 cfq_slice_expired(cfqd, 1);
1027 goto new_queue;
1028 }
1029
1030 /*
1008 * The active queue has requests and isn't expired, allow it to 1031 * The active queue has requests and isn't expired, allow it to
1009 * dispatch. 1032 * dispatch.
1010 */ 1033 */
@@ -1067,6 +1090,13 @@ __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1067 if (RB_EMPTY_ROOT(&cfqq->sort_list)) 1090 if (RB_EMPTY_ROOT(&cfqq->sort_list))
1068 break; 1091 break;
1069 1092
1093 /*
1094 * If there is a non-empty RT cfqq waiting for current
1095 * cfqq's timeslice to complete, pre-empt this cfqq
1096 */
1097 if (!cfq_class_rt(cfqq) && cfqd->busy_rt_queues)
1098 break;
1099
1070 } while (dispatched < max_dispatch); 1100 } while (dispatched < max_dispatch);
1071 1101
1072 /* 1102 /*
@@ -1801,6 +1831,12 @@ cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1801 if (rq_is_meta(rq) && !cfqq->meta_pending) 1831 if (rq_is_meta(rq) && !cfqq->meta_pending)
1802 return 1; 1832 return 1;
1803 1833
1834 /*
1835 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
1836 */
1837 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
1838 return 1;
1839
1804 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) 1840 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1805 return 0; 1841 return 0;
1806 1842
@@ -1870,7 +1906,8 @@ cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1870 /* 1906 /*
1871 * not the active queue - expire current slice if it is 1907 * not the active queue - expire current slice if it is
1872 * idle and has expired it's mean thinktime or this new queue 1908 * idle and has expired it's mean thinktime or this new queue
1873 * has some old slice time left and is of higher priority 1909 * has some old slice time left and is of higher priority or
1910 * this new queue is RT and the current one is BE
1874 */ 1911 */
1875 cfq_preempt_queue(cfqd, cfqq); 1912 cfq_preempt_queue(cfqd, cfqq);
1876 cfq_mark_cfqq_must_dispatch(cfqq); 1913 cfq_mark_cfqq_must_dispatch(cfqq);