Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * CFQ, or complete fairness queueing, disk scheduler. |
| 3 | * |
| 4 | * Based on ideas from a previously unfinished io |
| 5 | * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli. |
| 6 | * |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 7 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Al Viro | 1cc9be6 | 2006-03-18 12:29:52 -0500 | [diff] [blame] | 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/elevator.h> |
Randy Dunlap | ad5ebd2 | 2009-11-11 13:47:45 +0100 | [diff] [blame] | 13 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/rbtree.h> |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 15 | #include <linux/ioprio.h> |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 16 | #include <linux/blktrace_api.h> |
Tejun Heo | 6e736be | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 17 | #include "blk.h" |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 18 | #include "blk-cgroup.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * tunables |
| 22 | */ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 23 | /* max queue in one round of service */ |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 24 | static const int cfq_quantum = 8; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 25 | static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 26 | /* maximum backwards seek, in KiB */ |
| 27 | static const int cfq_back_max = 16 * 1024; |
| 28 | /* penalty of a backwards seek */ |
| 29 | static const int cfq_back_penalty = 2; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 30 | static const int cfq_slice_sync = HZ / 10; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 31 | static int cfq_slice_async = HZ / 25; |
Arjan van de Ven | 6410009 | 2006-01-06 09:46:02 +0100 | [diff] [blame] | 32 | static const int cfq_slice_async_rq = 2; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 33 | static int cfq_slice_idle = HZ / 125; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 34 | static int cfq_group_idle = HZ / 125; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 35 | static const int cfq_target_latency = HZ * 3/10; /* 300 ms */ |
| 36 | static const int cfq_hist_divisor = 4; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 37 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 38 | /* |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 39 | * offset from end of service tree |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 40 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 41 | #define CFQ_IDLE_DELAY (HZ / 5) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * below this threshold, we consider thinktime immediate |
| 45 | */ |
| 46 | #define CFQ_MIN_TT (2) |
| 47 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 48 | #define CFQ_SLICE_SCALE (5) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 49 | #define CFQ_HW_QUEUE_MIN (5) |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 50 | #define CFQ_SERVICE_SHIFT 12 |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 51 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 52 | #define CFQQ_SEEK_THR (sector_t)(8 * 100) |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 53 | #define CFQQ_CLOSE_THR (sector_t)(8 * 1024) |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 54 | #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32) |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 55 | #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8) |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 56 | |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 57 | #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq) |
| 58 | #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0]) |
| 59 | #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 61 | static struct kmem_cache *cfq_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 63 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
| 64 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 65 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
| 66 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 67 | #define sample_valid(samples) ((samples) > 80) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 68 | #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 69 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 70 | struct cfq_ttime { |
| 71 | unsigned long last_end_request; |
| 72 | |
| 73 | unsigned long ttime_total; |
| 74 | unsigned long ttime_samples; |
| 75 | unsigned long ttime_mean; |
| 76 | }; |
| 77 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 78 | /* |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 79 | * Most of our rbtree usage is for sorting with min extraction, so |
| 80 | * if we cache the leftmost node we don't have to walk down the tree |
| 81 | * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should |
| 82 | * move this into the elevator for the rq sorting as well. |
| 83 | */ |
| 84 | struct cfq_rb_root { |
| 85 | struct rb_root rb; |
| 86 | struct rb_node *left; |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 87 | unsigned count; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 88 | u64 min_vdisktime; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 89 | struct cfq_ttime ttime; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 90 | }; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 91 | #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \ |
| 92 | .ttime = {.last_end_request = jiffies,},} |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 93 | |
| 94 | /* |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 95 | * Per process-grouping structure |
| 96 | */ |
| 97 | struct cfq_queue { |
| 98 | /* reference count */ |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 99 | int ref; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 100 | /* various state flags, see below */ |
| 101 | unsigned int flags; |
| 102 | /* parent cfq_data */ |
| 103 | struct cfq_data *cfqd; |
| 104 | /* service_tree member */ |
| 105 | struct rb_node rb_node; |
| 106 | /* service_tree key */ |
| 107 | unsigned long rb_key; |
| 108 | /* prio tree member */ |
| 109 | struct rb_node p_node; |
| 110 | /* prio tree root we belong to, if any */ |
| 111 | struct rb_root *p_root; |
| 112 | /* sorted list of pending requests */ |
| 113 | struct rb_root sort_list; |
| 114 | /* if fifo isn't expired, next request to serve */ |
| 115 | struct request *next_rq; |
| 116 | /* requests queued in sort_list */ |
| 117 | int queued[2]; |
| 118 | /* currently allocated requests */ |
| 119 | int allocated[2]; |
| 120 | /* fifo list of requests in sort_list */ |
| 121 | struct list_head fifo; |
| 122 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 123 | /* time when queue got scheduled in to dispatch first request. */ |
| 124 | unsigned long dispatch_start; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 125 | unsigned int allocated_slice; |
Richard Kennedy | c4081ba | 2010-02-22 13:49:24 +0100 | [diff] [blame] | 126 | unsigned int slice_dispatch; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 127 | /* time when first request from queue completed and slice started. */ |
| 128 | unsigned long slice_start; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 129 | unsigned long slice_end; |
| 130 | long slice_resid; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 131 | |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 132 | /* pending priority requests */ |
| 133 | int prio_pending; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 134 | /* number of requests that are on the dispatch list or inside driver */ |
| 135 | int dispatched; |
| 136 | |
| 137 | /* io prio of this group */ |
| 138 | unsigned short ioprio, org_ioprio; |
Justin TerAvest | 4aede84 | 2011-07-12 08:31:45 +0200 | [diff] [blame] | 139 | unsigned short ioprio_class; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 140 | |
Richard Kennedy | c4081ba | 2010-02-22 13:49:24 +0100 | [diff] [blame] | 141 | pid_t pid; |
| 142 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 143 | u32 seek_history; |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 144 | sector_t last_request_pos; |
| 145 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 146 | struct cfq_rb_root *service_tree; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 147 | struct cfq_queue *new_cfqq; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 148 | struct cfq_group *cfqg; |
Vivek Goyal | c4e7893 | 2010-08-23 12:25:03 +0200 | [diff] [blame] | 149 | /* Number of sectors dispatched from queue in single dispatch round */ |
| 150 | unsigned long nr_sectors; |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 154 | * First index in the service_trees. |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 155 | * IDLE is handled separately, so it has negative index |
| 156 | */ |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 157 | enum wl_class_t { |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 158 | BE_WORKLOAD = 0, |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 159 | RT_WORKLOAD = 1, |
| 160 | IDLE_WORKLOAD = 2, |
Vivek Goyal | b462732 | 2010-10-22 09:48:43 +0200 | [diff] [blame] | 161 | CFQ_PRIO_NR, |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /* |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 165 | * Second index in the service_trees. |
| 166 | */ |
| 167 | enum wl_type_t { |
| 168 | ASYNC_WORKLOAD = 0, |
| 169 | SYNC_NOIDLE_WORKLOAD = 1, |
| 170 | SYNC_WORKLOAD = 2 |
| 171 | }; |
| 172 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 173 | struct cfqg_stats { |
| 174 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 175 | /* total bytes transferred */ |
| 176 | struct blkg_rwstat service_bytes; |
| 177 | /* total IOs serviced, post merge */ |
| 178 | struct blkg_rwstat serviced; |
| 179 | /* number of ios merged */ |
| 180 | struct blkg_rwstat merged; |
| 181 | /* total time spent on device in ns, may not be accurate w/ queueing */ |
| 182 | struct blkg_rwstat service_time; |
| 183 | /* total time spent waiting in scheduler queue in ns */ |
| 184 | struct blkg_rwstat wait_time; |
| 185 | /* number of IOs queued up */ |
| 186 | struct blkg_rwstat queued; |
| 187 | /* total sectors transferred */ |
| 188 | struct blkg_stat sectors; |
| 189 | /* total disk time and nr sectors dispatched by this group */ |
| 190 | struct blkg_stat time; |
| 191 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 192 | /* time not charged to this cgroup */ |
| 193 | struct blkg_stat unaccounted_time; |
| 194 | /* sum of number of ios queued across all samples */ |
| 195 | struct blkg_stat avg_queue_size_sum; |
| 196 | /* count of samples taken for average */ |
| 197 | struct blkg_stat avg_queue_size_samples; |
| 198 | /* how many times this group has been removed from service tree */ |
| 199 | struct blkg_stat dequeue; |
| 200 | /* total time spent waiting for it to be assigned a timeslice. */ |
| 201 | struct blkg_stat group_wait_time; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 202 | /* time spent idling for this blkcg_gq */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 203 | struct blkg_stat idle_time; |
| 204 | /* total time with empty current active q with other requests queued */ |
| 205 | struct blkg_stat empty_time; |
| 206 | /* fields after this shouldn't be cleared on stat reset */ |
| 207 | uint64_t start_group_wait_time; |
| 208 | uint64_t start_idle_time; |
| 209 | uint64_t start_empty_time; |
| 210 | uint16_t flags; |
| 211 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 212 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 213 | }; |
| 214 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 215 | /* This is per cgroup per device grouping structure */ |
| 216 | struct cfq_group { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 217 | /* must be the first member */ |
| 218 | struct blkg_policy_data pd; |
| 219 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 220 | /* group service_tree member */ |
| 221 | struct rb_node rb_node; |
| 222 | |
| 223 | /* group service_tree key */ |
| 224 | u64 vdisktime; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 225 | |
| 226 | /* |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 227 | * The number of active cfqgs and sum of their weights under this |
| 228 | * cfqg. This covers this cfqg's leaf_weight and all children's |
| 229 | * weights, but does not cover weights of further descendants. |
| 230 | * |
| 231 | * If a cfqg is on the service tree, it's active. An active cfqg |
| 232 | * also activates its parent and contributes to the children_weight |
| 233 | * of the parent. |
| 234 | */ |
| 235 | int nr_active; |
| 236 | unsigned int children_weight; |
| 237 | |
| 238 | /* |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 239 | * vfraction is the fraction of vdisktime that the tasks in this |
| 240 | * cfqg are entitled to. This is determined by compounding the |
| 241 | * ratios walking up from this cfqg to the root. |
| 242 | * |
| 243 | * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all |
| 244 | * vfractions on a service tree is approximately 1. The sum may |
| 245 | * deviate a bit due to rounding errors and fluctuations caused by |
| 246 | * cfqgs entering and leaving the service tree. |
| 247 | */ |
| 248 | unsigned int vfraction; |
| 249 | |
| 250 | /* |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 251 | * There are two weights - (internal) weight is the weight of this |
| 252 | * cfqg against the sibling cfqgs. leaf_weight is the wight of |
| 253 | * this cfqg against the child cfqgs. For the root cfqg, both |
| 254 | * weights are kept in sync for backward compatibility. |
| 255 | */ |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 256 | unsigned int weight; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 257 | unsigned int new_weight; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 258 | unsigned int dev_weight; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 259 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 260 | unsigned int leaf_weight; |
| 261 | unsigned int new_leaf_weight; |
| 262 | unsigned int dev_leaf_weight; |
| 263 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 264 | /* number of cfqq currently on this group */ |
| 265 | int nr_cfqq; |
| 266 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 267 | /* |
Kyungmin Park | 4495a7d | 2011-05-31 10:04:09 +0200 | [diff] [blame] | 268 | * Per group busy queues average. Useful for workload slice calc. We |
Vivek Goyal | b462732 | 2010-10-22 09:48:43 +0200 | [diff] [blame] | 269 | * create the array for each prio class but at run time it is used |
| 270 | * only for RT and BE class and slot for IDLE class remains unused. |
| 271 | * This is primarily done to avoid confusion and a gcc warning. |
| 272 | */ |
| 273 | unsigned int busy_queues_avg[CFQ_PRIO_NR]; |
| 274 | /* |
| 275 | * rr lists of queues with requests. We maintain service trees for |
| 276 | * RT and BE classes. These trees are subdivided in subclasses |
| 277 | * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE |
| 278 | * class there is no subclassification and all the cfq queues go on |
| 279 | * a single tree service_tree_idle. |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 280 | * Counts are embedded in the cfq_rb_root |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 281 | */ |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 282 | struct cfq_rb_root service_trees[2][3]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 283 | struct cfq_rb_root service_tree_idle; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 284 | |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 285 | unsigned long saved_wl_slice; |
| 286 | enum wl_type_t saved_wl_type; |
| 287 | enum wl_class_t saved_wl_class; |
Tejun Heo | 4eef304 | 2012-03-05 13:15:18 -0800 | [diff] [blame] | 288 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 289 | /* number of requests that are on the dispatch list or inside driver */ |
| 290 | int dispatched; |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 291 | struct cfq_ttime ttime; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 292 | struct cfqg_stats stats; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 293 | }; |
| 294 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 295 | struct cfq_io_cq { |
| 296 | struct io_cq icq; /* must be the first member */ |
| 297 | struct cfq_queue *cfqq[2]; |
| 298 | struct cfq_ttime ttime; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 299 | int ioprio; /* the current ioprio */ |
| 300 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 301 | uint64_t blkcg_id; /* the current blkcg ID */ |
| 302 | #endif |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 303 | }; |
| 304 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 305 | /* |
| 306 | * Per block device queue structure |
| 307 | */ |
| 308 | struct cfq_data { |
| 309 | struct request_queue *queue; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 310 | /* Root service tree for cfq_groups */ |
| 311 | struct cfq_rb_root grp_service_tree; |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 312 | struct cfq_group *root_group; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 313 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 314 | /* |
| 315 | * The priority currently being served |
| 316 | */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 317 | enum wl_class_t serving_wl_class; |
| 318 | enum wl_type_t serving_wl_type; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 319 | unsigned long workload_expires; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 320 | struct cfq_group *serving_group; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * Each priority tree is sorted by next_request position. These |
| 324 | * trees are used when determining if two or more queues are |
| 325 | * interleaving requests (see cfq_close_cooperator). |
| 326 | */ |
| 327 | struct rb_root prio_trees[CFQ_PRIO_LISTS]; |
| 328 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 329 | unsigned int busy_queues; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 330 | unsigned int busy_sync_queues; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 331 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 332 | int rq_in_driver; |
| 333 | int rq_in_flight[2]; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * queue-depth detection |
| 337 | */ |
| 338 | int rq_queued; |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 339 | int hw_tag; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 340 | /* |
| 341 | * hw_tag can be |
| 342 | * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection) |
| 343 | * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth) |
| 344 | * 0 => no NCQ |
| 345 | */ |
| 346 | int hw_tag_est_depth; |
| 347 | unsigned int hw_tag_samples; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 348 | |
| 349 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 350 | * idle window management |
| 351 | */ |
| 352 | struct timer_list idle_slice_timer; |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 353 | struct work_struct unplug_work; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 354 | |
| 355 | struct cfq_queue *active_queue; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 356 | struct cfq_io_cq *active_cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 357 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 358 | /* |
| 359 | * async queue for each priority case |
| 360 | */ |
| 361 | struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR]; |
| 362 | struct cfq_queue *async_idle_cfqq; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 363 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 364 | sector_t last_position; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | /* |
| 367 | * tunables, see top of file |
| 368 | */ |
| 369 | unsigned int cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 370 | unsigned int cfq_fifo_expire[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | unsigned int cfq_back_penalty; |
| 372 | unsigned int cfq_back_max; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 373 | unsigned int cfq_slice[2]; |
| 374 | unsigned int cfq_slice_async_rq; |
| 375 | unsigned int cfq_slice_idle; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 376 | unsigned int cfq_group_idle; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 377 | unsigned int cfq_latency; |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 378 | unsigned int cfq_target_latency; |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 379 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 380 | /* |
| 381 | * Fallback dummy cfqq for extreme OOM conditions |
| 382 | */ |
| 383 | struct cfq_queue oom_cfqq; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 384 | |
Corrado Zoccolo | 573412b | 2009-12-06 11:48:52 +0100 | [diff] [blame] | 385 | unsigned long last_delayed_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 388 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd); |
| 389 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 390 | static struct cfq_rb_root *st_for(struct cfq_group *cfqg, |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 391 | enum wl_class_t class, |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 392 | enum wl_type_t type) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 393 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 394 | if (!cfqg) |
| 395 | return NULL; |
| 396 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 397 | if (class == IDLE_WORKLOAD) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 398 | return &cfqg->service_tree_idle; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 399 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 400 | return &cfqg->service_trees[class][type]; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 403 | enum cfqq_state_flags { |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 404 | CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ |
| 405 | CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 406 | CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 407 | CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ |
Jens Axboe | b0b8d749 | 2007-01-19 11:35:30 +1100 | [diff] [blame] | 408 | CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ |
| 409 | CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ |
| 410 | CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 411 | CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 412 | CFQ_CFQQ_FLAG_sync, /* synchronous queue */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 413 | CFQ_CFQQ_FLAG_coop, /* cfqq is shared */ |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 414 | CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */ |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 415 | CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */ |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 416 | CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | #define CFQ_CFQQ_FNS(name) \ |
| 420 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ |
| 421 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 422 | (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 423 | } \ |
| 424 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ |
| 425 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 426 | (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 427 | } \ |
| 428 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ |
| 429 | { \ |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 430 | return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | CFQ_CFQQ_FNS(on_rr); |
| 434 | CFQ_CFQQ_FNS(wait_request); |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 435 | CFQ_CFQQ_FNS(must_dispatch); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 436 | CFQ_CFQQ_FNS(must_alloc_slice); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 437 | CFQ_CFQQ_FNS(fifo_expire); |
| 438 | CFQ_CFQQ_FNS(idle_window); |
| 439 | CFQ_CFQQ_FNS(prio_changed); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 440 | CFQ_CFQQ_FNS(slice_new); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 441 | CFQ_CFQQ_FNS(sync); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 442 | CFQ_CFQQ_FNS(coop); |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 443 | CFQ_CFQQ_FNS(split_coop); |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 444 | CFQ_CFQQ_FNS(deep); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 445 | CFQ_CFQQ_FNS(wait_busy); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 446 | #undef CFQ_CFQQ_FNS |
| 447 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 448 | static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd) |
| 449 | { |
| 450 | return pd ? container_of(pd, struct cfq_group, pd) : NULL; |
| 451 | } |
| 452 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 453 | static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg) |
| 454 | { |
| 455 | return pd_to_blkg(&cfqg->pd); |
| 456 | } |
| 457 | |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 458 | #if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 459 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 460 | /* cfqg stats flags */ |
| 461 | enum cfqg_stats_flags { |
| 462 | CFQG_stats_waiting = 0, |
| 463 | CFQG_stats_idling, |
| 464 | CFQG_stats_empty, |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 465 | }; |
| 466 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 467 | #define CFQG_FLAG_FNS(name) \ |
| 468 | static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 469 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 470 | stats->flags |= (1 << CFQG_stats_##name); \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 471 | } \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 472 | static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 473 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 474 | stats->flags &= ~(1 << CFQG_stats_##name); \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 475 | } \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 476 | static inline int cfqg_stats_##name(struct cfqg_stats *stats) \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 477 | { \ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 478 | return (stats->flags & (1 << CFQG_stats_##name)) != 0; \ |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 479 | } \ |
| 480 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 481 | CFQG_FLAG_FNS(waiting) |
| 482 | CFQG_FLAG_FNS(idling) |
| 483 | CFQG_FLAG_FNS(empty) |
| 484 | #undef CFQG_FLAG_FNS |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 485 | |
| 486 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 487 | static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 488 | { |
| 489 | unsigned long long now; |
| 490 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 491 | if (!cfqg_stats_waiting(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 492 | return; |
| 493 | |
| 494 | now = sched_clock(); |
| 495 | if (time_after64(now, stats->start_group_wait_time)) |
| 496 | blkg_stat_add(&stats->group_wait_time, |
| 497 | now - stats->start_group_wait_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 498 | cfqg_stats_clear_waiting(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 502 | static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, |
| 503 | struct cfq_group *curr_cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 504 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 505 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 506 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 507 | if (cfqg_stats_waiting(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 508 | return; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 509 | if (cfqg == curr_cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 510 | return; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 511 | stats->start_group_wait_time = sched_clock(); |
| 512 | cfqg_stats_mark_waiting(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* This should be called with the queue_lock held. */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 516 | static void cfqg_stats_end_empty_time(struct cfqg_stats *stats) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 517 | { |
| 518 | unsigned long long now; |
| 519 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 520 | if (!cfqg_stats_empty(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 521 | return; |
| 522 | |
| 523 | now = sched_clock(); |
| 524 | if (time_after64(now, stats->start_empty_time)) |
| 525 | blkg_stat_add(&stats->empty_time, |
| 526 | now - stats->start_empty_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 527 | cfqg_stats_clear_empty(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 530 | static void cfqg_stats_update_dequeue(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 531 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 532 | blkg_stat_add(&cfqg->stats.dequeue, 1); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 535 | static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 536 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 537 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 538 | |
Tejun Heo | 4d5e80a | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 539 | if (blkg_rwstat_total(&stats->queued)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 540 | return; |
| 541 | |
| 542 | /* |
| 543 | * group is already marked empty. This can happen if cfqq got new |
| 544 | * request in parent group and moved to this group while being added |
| 545 | * to service tree. Just ignore the event and move on. |
| 546 | */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 547 | if (cfqg_stats_empty(stats)) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 548 | return; |
| 549 | |
| 550 | stats->start_empty_time = sched_clock(); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 551 | cfqg_stats_mark_empty(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 554 | static void cfqg_stats_update_idle_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 555 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 556 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 557 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 558 | if (cfqg_stats_idling(stats)) { |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 559 | unsigned long long now = sched_clock(); |
| 560 | |
| 561 | if (time_after64(now, stats->start_idle_time)) |
| 562 | blkg_stat_add(&stats->idle_time, |
| 563 | now - stats->start_idle_time); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 564 | cfqg_stats_clear_idling(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 568 | static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 569 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 570 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 571 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 572 | BUG_ON(cfqg_stats_idling(stats)); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 573 | |
| 574 | stats->start_idle_time = sched_clock(); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 575 | cfqg_stats_mark_idling(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 578 | static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 579 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 580 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 581 | |
| 582 | blkg_stat_add(&stats->avg_queue_size_sum, |
Tejun Heo | 4d5e80a | 2013-01-09 08:05:12 -0800 | [diff] [blame] | 583 | blkg_rwstat_total(&stats->queued)); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 584 | blkg_stat_add(&stats->avg_queue_size_samples, 1); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 585 | cfqg_stats_update_group_wait_time(stats); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | #else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ |
| 589 | |
Tejun Heo | f48ec1d | 2012-04-13 13:11:25 -0700 | [diff] [blame] | 590 | static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { } |
| 591 | static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { } |
| 592 | static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { } |
| 593 | static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { } |
| 594 | static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { } |
| 595 | static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { } |
| 596 | static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { } |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 597 | |
| 598 | #endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */ |
| 599 | |
| 600 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 601 | |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 602 | static struct blkcg_policy blkcg_policy_cfq; |
| 603 | |
| 604 | static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg) |
| 605 | { |
| 606 | return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq)); |
| 607 | } |
| 608 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 609 | static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 610 | { |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 611 | struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 612 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 613 | return pblkg ? blkg_to_cfqg(pblkg) : NULL; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 616 | static inline void cfqg_get(struct cfq_group *cfqg) |
| 617 | { |
| 618 | return blkg_get(cfqg_to_blkg(cfqg)); |
| 619 | } |
| 620 | |
| 621 | static inline void cfqg_put(struct cfq_group *cfqg) |
| 622 | { |
| 623 | return blkg_put(cfqg_to_blkg(cfqg)); |
| 624 | } |
| 625 | |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 626 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \ |
| 627 | char __pbuf[128]; \ |
| 628 | \ |
| 629 | blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \ |
Vivek Goyal | b226e5c | 2012-10-03 16:57:01 -0400 | [diff] [blame] | 630 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \ |
| 631 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ |
| 632 | cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 633 | __pbuf, ##args); \ |
| 634 | } while (0) |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 635 | |
Tejun Heo | 54e7ed1 | 2012-04-16 13:57:23 -0700 | [diff] [blame] | 636 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \ |
| 637 | char __pbuf[128]; \ |
| 638 | \ |
| 639 | blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \ |
| 640 | blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \ |
| 641 | } while (0) |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 642 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 643 | static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, |
| 644 | struct cfq_group *curr_cfqg, int rw) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 645 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 646 | blkg_rwstat_add(&cfqg->stats.queued, rw, 1); |
| 647 | cfqg_stats_end_empty_time(&cfqg->stats); |
| 648 | cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 651 | static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, |
| 652 | unsigned long time, unsigned long unaccounted_time) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 653 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 654 | blkg_stat_add(&cfqg->stats.time, time); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 655 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 656 | blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 657 | #endif |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 660 | static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 661 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 662 | blkg_rwstat_add(&cfqg->stats.queued, rw, -1); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 665 | static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 666 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 667 | blkg_rwstat_add(&cfqg->stats.merged, rw, 1); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 670 | static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg, |
| 671 | uint64_t bytes, int rw) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 672 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 673 | blkg_stat_add(&cfqg->stats.sectors, bytes >> 9); |
| 674 | blkg_rwstat_add(&cfqg->stats.serviced, rw, 1); |
| 675 | blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 678 | static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, |
| 679 | uint64_t start_time, uint64_t io_start_time, int rw) |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 680 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 681 | struct cfqg_stats *stats = &cfqg->stats; |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 682 | unsigned long long now = sched_clock(); |
Tejun Heo | 629ed0b | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 683 | |
| 684 | if (time_after64(now, io_start_time)) |
| 685 | blkg_rwstat_add(&stats->service_time, rw, now - io_start_time); |
| 686 | if (time_after64(io_start_time, start_time)) |
| 687 | blkg_rwstat_add(&stats->wait_time, rw, |
| 688 | io_start_time - start_time); |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Tejun Heo | 689665a | 2013-01-09 08:05:13 -0800 | [diff] [blame^] | 691 | /* @stats = 0 */ |
| 692 | static void cfqg_stats_reset(struct cfqg_stats *stats) |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 693 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 694 | /* queued stats shouldn't be cleared */ |
| 695 | blkg_rwstat_reset(&stats->service_bytes); |
| 696 | blkg_rwstat_reset(&stats->serviced); |
| 697 | blkg_rwstat_reset(&stats->merged); |
| 698 | blkg_rwstat_reset(&stats->service_time); |
| 699 | blkg_rwstat_reset(&stats->wait_time); |
| 700 | blkg_stat_reset(&stats->time); |
| 701 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 702 | blkg_stat_reset(&stats->unaccounted_time); |
| 703 | blkg_stat_reset(&stats->avg_queue_size_sum); |
| 704 | blkg_stat_reset(&stats->avg_queue_size_samples); |
| 705 | blkg_stat_reset(&stats->dequeue); |
| 706 | blkg_stat_reset(&stats->group_wait_time); |
| 707 | blkg_stat_reset(&stats->idle_time); |
| 708 | blkg_stat_reset(&stats->empty_time); |
| 709 | #endif |
| 710 | } |
| 711 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 712 | #else /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 713 | |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 714 | static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; } |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 715 | static inline void cfqg_get(struct cfq_group *cfqg) { } |
| 716 | static inline void cfqg_put(struct cfq_group *cfqg) { } |
| 717 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 718 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
Vivek Goyal | b226e5c | 2012-10-03 16:57:01 -0400 | [diff] [blame] | 719 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \ |
| 720 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ |
| 721 | cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\ |
| 722 | ##args) |
Kyungmin Park | 4495a7d | 2011-05-31 10:04:09 +0200 | [diff] [blame] | 723 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0) |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 724 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 725 | static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg, |
| 726 | struct cfq_group *curr_cfqg, int rw) { } |
| 727 | static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg, |
| 728 | unsigned long time, unsigned long unaccounted_time) { } |
| 729 | static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { } |
| 730 | static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { } |
| 731 | static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg, |
| 732 | uint64_t bytes, int rw) { } |
| 733 | static inline void cfqg_stats_update_completion(struct cfq_group *cfqg, |
| 734 | uint64_t start_time, uint64_t io_start_time, int rw) { } |
Tejun Heo | 2ce4d50 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 735 | |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 736 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 737 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 738 | #define cfq_log(cfqd, fmt, args...) \ |
| 739 | blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) |
| 740 | |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 741 | /* Traverses through cfq group service trees */ |
| 742 | #define for_each_cfqg_st(cfqg, i, j, st) \ |
| 743 | for (i = 0; i <= IDLE_WORKLOAD; i++) \ |
| 744 | for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\ |
| 745 | : &cfqg->service_tree_idle; \ |
| 746 | (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \ |
| 747 | (i == IDLE_WORKLOAD && j == 0); \ |
| 748 | j++, st = i < IDLE_WORKLOAD ? \ |
| 749 | &cfqg->service_trees[i][j]: NULL) \ |
| 750 | |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 751 | static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd, |
| 752 | struct cfq_ttime *ttime, bool group_idle) |
| 753 | { |
| 754 | unsigned long slice; |
| 755 | if (!sample_valid(ttime->ttime_samples)) |
| 756 | return false; |
| 757 | if (group_idle) |
| 758 | slice = cfqd->cfq_group_idle; |
| 759 | else |
| 760 | slice = cfqd->cfq_slice_idle; |
| 761 | return ttime->ttime_mean > slice; |
| 762 | } |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 763 | |
Vivek Goyal | 02b3508 | 2010-08-23 12:23:53 +0200 | [diff] [blame] | 764 | static inline bool iops_mode(struct cfq_data *cfqd) |
| 765 | { |
| 766 | /* |
| 767 | * If we are not idling on queues and it is a NCQ drive, parallel |
| 768 | * execution of requests is on and measuring time is not possible |
| 769 | * in most of the cases until and unless we drive shallower queue |
| 770 | * depths and that becomes a performance bottleneck. In such cases |
| 771 | * switch to start providing fairness in terms of number of IOs. |
| 772 | */ |
| 773 | if (!cfqd->cfq_slice_idle && cfqd->hw_tag) |
| 774 | return true; |
| 775 | else |
| 776 | return false; |
| 777 | } |
| 778 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 779 | static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 780 | { |
| 781 | if (cfq_class_idle(cfqq)) |
| 782 | return IDLE_WORKLOAD; |
| 783 | if (cfq_class_rt(cfqq)) |
| 784 | return RT_WORKLOAD; |
| 785 | return BE_WORKLOAD; |
| 786 | } |
| 787 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 788 | |
| 789 | static enum wl_type_t cfqq_type(struct cfq_queue *cfqq) |
| 790 | { |
| 791 | if (!cfq_cfqq_sync(cfqq)) |
| 792 | return ASYNC_WORKLOAD; |
| 793 | if (!cfq_cfqq_idle_window(cfqq)) |
| 794 | return SYNC_NOIDLE_WORKLOAD; |
| 795 | return SYNC_WORKLOAD; |
| 796 | } |
| 797 | |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 798 | static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class, |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 799 | struct cfq_data *cfqd, |
| 800 | struct cfq_group *cfqg) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 801 | { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 802 | if (wl_class == IDLE_WORKLOAD) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 803 | return cfqg->service_tree_idle.count; |
| 804 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 805 | return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count + |
| 806 | cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count + |
| 807 | cfqg->service_trees[wl_class][SYNC_WORKLOAD].count; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 810 | static inline int cfqg_busy_async_queues(struct cfq_data *cfqd, |
| 811 | struct cfq_group *cfqg) |
| 812 | { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 813 | return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count + |
| 814 | cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 817 | static void cfq_dispatch_insert(struct request_queue *, struct request *); |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 818 | static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync, |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 819 | struct cfq_io_cq *cic, struct bio *bio, |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 820 | gfp_t gfp_mask); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 821 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 822 | static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq) |
| 823 | { |
| 824 | /* cic->icq is the first member, %NULL will convert to %NULL */ |
| 825 | return container_of(icq, struct cfq_io_cq, icq); |
| 826 | } |
| 827 | |
Tejun Heo | 47fdd4c | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 828 | static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd, |
| 829 | struct io_context *ioc) |
| 830 | { |
| 831 | if (ioc) |
| 832 | return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue)); |
| 833 | return NULL; |
| 834 | } |
| 835 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 836 | static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 837 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 838 | return cic->cfqq[is_sync]; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 841 | static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq, |
| 842 | bool is_sync) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 843 | { |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 844 | cic->cfqq[is_sync] = cfqq; |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 847 | static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic) |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 848 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 849 | return cic->icq.q->elevator->elevator_data; |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 850 | } |
| 851 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 852 | /* |
| 853 | * We regard a request as SYNC, if it's either a read or has the SYNC bit |
| 854 | * set (in which case it could also be direct WRITE). |
| 855 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 856 | static inline bool cfq_bio_sync(struct bio *bio) |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 857 | { |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 858 | return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | /* |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 862 | * scheduler run of queue, if there are requests pending and no one in the |
| 863 | * driver that will restart queueing |
| 864 | */ |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 865 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 866 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 867 | if (cfqd->busy_queues) { |
| 868 | cfq_log(cfqd, "schedule dispatch"); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 869 | kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work); |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 870 | } |
Andrew Morton | 99f95e5 | 2005-06-27 20:14:05 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | /* |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 874 | * Scale schedule slice based on io priority. Use the sync time slice only |
| 875 | * if a queue is marked sync and has sync io queued. A sync queue with async |
| 876 | * io only, should not get full sync slice length. |
| 877 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 878 | static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync, |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 879 | unsigned short prio) |
| 880 | { |
| 881 | const int base_slice = cfqd->cfq_slice[sync]; |
| 882 | |
| 883 | WARN_ON(prio >= IOPRIO_BE_NR); |
| 884 | |
| 885 | return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio)); |
| 886 | } |
| 887 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 888 | static inline int |
| 889 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 890 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 891 | return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 892 | } |
| 893 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 894 | /** |
| 895 | * cfqg_scale_charge - scale disk time charge according to cfqg weight |
| 896 | * @charge: disk time being charged |
| 897 | * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT |
| 898 | * |
| 899 | * Scale @charge according to @vfraction, which is in range (0, 1]. The |
| 900 | * scaling is inversely proportional. |
| 901 | * |
| 902 | * scaled = charge / vfraction |
| 903 | * |
| 904 | * The result is also in fixed point w/ CFQ_SERVICE_SHIFT. |
| 905 | */ |
| 906 | static inline u64 cfqg_scale_charge(unsigned long charge, |
| 907 | unsigned int vfraction) |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 908 | { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 909 | u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */ |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 910 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 911 | /* charge / vfraction */ |
| 912 | c <<= CFQ_SERVICE_SHIFT; |
| 913 | do_div(c, vfraction); |
| 914 | return c; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 918 | { |
| 919 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 920 | if (delta > 0) |
| 921 | min_vdisktime = vdisktime; |
| 922 | |
| 923 | return min_vdisktime; |
| 924 | } |
| 925 | |
| 926 | static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime) |
| 927 | { |
| 928 | s64 delta = (s64)(vdisktime - min_vdisktime); |
| 929 | if (delta < 0) |
| 930 | min_vdisktime = vdisktime; |
| 931 | |
| 932 | return min_vdisktime; |
| 933 | } |
| 934 | |
| 935 | static void update_min_vdisktime(struct cfq_rb_root *st) |
| 936 | { |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 937 | struct cfq_group *cfqg; |
| 938 | |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 939 | if (st->left) { |
| 940 | cfqg = rb_entry_cfqg(st->left); |
Gui Jianfeng | a603271 | 2011-03-07 09:28:09 +0100 | [diff] [blame] | 941 | st->min_vdisktime = max_vdisktime(st->min_vdisktime, |
| 942 | cfqg->vdisktime); |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 943 | } |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 944 | } |
| 945 | |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 946 | /* |
| 947 | * get averaged number of queues of RT/BE priority. |
| 948 | * average is updated, with a formula that gives more weight to higher numbers, |
| 949 | * to quickly follows sudden increases and decrease slowly |
| 950 | */ |
| 951 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 952 | static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd, |
| 953 | struct cfq_group *cfqg, bool rt) |
Jens Axboe | 5869619c | 2009-10-28 09:27:07 +0100 | [diff] [blame] | 954 | { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 955 | unsigned min_q, max_q; |
| 956 | unsigned mult = cfq_hist_divisor - 1; |
| 957 | unsigned round = cfq_hist_divisor / 2; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 958 | unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 959 | |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 960 | min_q = min(cfqg->busy_queues_avg[rt], busy); |
| 961 | max_q = max(cfqg->busy_queues_avg[rt], busy); |
| 962 | cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) / |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 963 | cfq_hist_divisor; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 964 | return cfqg->busy_queues_avg[rt]; |
| 965 | } |
| 966 | |
| 967 | static inline unsigned |
| 968 | cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg) |
| 969 | { |
Tejun Heo | 41cad6a | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 970 | return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 971 | } |
| 972 | |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 973 | static inline unsigned |
Vivek Goyal | ba5bd52 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 974 | cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 975 | { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 976 | unsigned slice = cfq_prio_to_slice(cfqd, cfqq); |
| 977 | if (cfqd->cfq_latency) { |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 978 | /* |
| 979 | * interested queues (we consider only the ones with the same |
| 980 | * priority class in the cfq group) |
| 981 | */ |
| 982 | unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg, |
| 983 | cfq_class_rt(cfqq)); |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 984 | unsigned sync_slice = cfqd->cfq_slice[1]; |
| 985 | unsigned expect_latency = sync_slice * iq; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 986 | unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg); |
| 987 | |
| 988 | if (expect_latency > group_slice) { |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 989 | unsigned base_low_slice = 2 * cfqd->cfq_slice_idle; |
| 990 | /* scale low_slice according to IO priority |
| 991 | * and sync vs async */ |
| 992 | unsigned low_slice = |
| 993 | min(slice, base_low_slice * slice / sync_slice); |
| 994 | /* the adapted slice value is scaled to fit all iqs |
| 995 | * into the target latency */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 996 | slice = max(slice * group_slice / expect_latency, |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 997 | low_slice); |
| 998 | } |
| 999 | } |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 1000 | return slice; |
| 1001 | } |
| 1002 | |
| 1003 | static inline void |
| 1004 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1005 | { |
Vivek Goyal | ba5bd52 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 1006 | unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq); |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 1007 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1008 | cfqq->slice_start = jiffies; |
Corrado Zoccolo | 5db5d64 | 2009-10-26 22:44:04 +0100 | [diff] [blame] | 1009 | cfqq->slice_end = jiffies + slice; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 1010 | cfqq->allocated_slice = slice; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 1011 | cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies); |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end |
| 1016 | * isn't valid until the first request from the dispatch is activated |
| 1017 | * and the slice time set. |
| 1018 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1019 | static inline bool cfq_slice_used(struct cfq_queue *cfqq) |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1020 | { |
| 1021 | if (cfq_cfqq_slice_new(cfqq)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1022 | return false; |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1023 | if (time_before(jiffies, cfqq->slice_end)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1024 | return false; |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1025 | |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 1026 | return true; |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1030 | * Lifted from AS - choose which of rq1 and rq2 that is best served now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | * We choose the request that is closest to the head right now. Distance |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1032 | * behind the head is penalized and only allowed to a certain extent. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1034 | static struct request * |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1035 | cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1037 | sector_t s1, s2, d1 = 0, d2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | unsigned long back_max; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1039 | #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ |
| 1040 | #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ |
| 1041 | unsigned wrap = 0; /* bit mask: requests behind the disk head? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1043 | if (rq1 == NULL || rq1 == rq2) |
| 1044 | return rq2; |
| 1045 | if (rq2 == NULL) |
| 1046 | return rq1; |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 1047 | |
Namhyung Kim | 229836b | 2011-05-24 10:23:21 +0200 | [diff] [blame] | 1048 | if (rq_is_sync(rq1) != rq_is_sync(rq2)) |
| 1049 | return rq_is_sync(rq1) ? rq1 : rq2; |
| 1050 | |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 1051 | if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO) |
| 1052 | return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2; |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 1053 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 1054 | s1 = blk_rq_pos(rq1); |
| 1055 | s2 = blk_rq_pos(rq2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | /* |
| 1058 | * by definition, 1KiB is 2 sectors |
| 1059 | */ |
| 1060 | back_max = cfqd->cfq_back_max * 2; |
| 1061 | |
| 1062 | /* |
| 1063 | * Strict one way elevator _except_ in the case where we allow |
| 1064 | * short backward seeks which are biased as twice the cost of a |
| 1065 | * similar forward seek. |
| 1066 | */ |
| 1067 | if (s1 >= last) |
| 1068 | d1 = s1 - last; |
| 1069 | else if (s1 + back_max >= last) |
| 1070 | d1 = (last - s1) * cfqd->cfq_back_penalty; |
| 1071 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1072 | wrap |= CFQ_RQ1_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
| 1074 | if (s2 >= last) |
| 1075 | d2 = s2 - last; |
| 1076 | else if (s2 + back_max >= last) |
| 1077 | d2 = (last - s2) * cfqd->cfq_back_penalty; |
| 1078 | else |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1079 | wrap |= CFQ_RQ2_WRAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* Found required data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1083 | /* |
| 1084 | * By doing switch() on the bit mask "wrap" we avoid having to |
| 1085 | * check two variables for all permutations: --> faster! |
| 1086 | */ |
| 1087 | switch (wrap) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1088 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1089 | if (d1 < d2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1090 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1091 | else if (d2 < d1) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1092 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1093 | else { |
| 1094 | if (s1 >= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1095 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1096 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1097 | return rq2; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | case CFQ_RQ2_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1101 | return rq1; |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1102 | case CFQ_RQ1_WRAP: |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1103 | return rq2; |
| 1104 | case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ |
Andreas Mohr | e8a9905 | 2006-03-28 08:59:49 +0200 | [diff] [blame] | 1105 | default: |
| 1106 | /* |
| 1107 | * Since both rqs are wrapped, |
| 1108 | * start with the one that's further behind head |
| 1109 | * (--> only *one* back seek required), |
| 1110 | * since back seek takes more time than forward. |
| 1111 | */ |
| 1112 | if (s1 <= s2) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1113 | return rq1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | else |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1115 | return rq2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1119 | /* |
| 1120 | * The below is leftmost cache rbtree addon |
| 1121 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1122 | static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1123 | { |
Vivek Goyal | 615f025 | 2009-12-03 12:59:39 -0500 | [diff] [blame] | 1124 | /* Service tree is empty */ |
| 1125 | if (!root->count) |
| 1126 | return NULL; |
| 1127 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1128 | if (!root->left) |
| 1129 | root->left = rb_first(&root->rb); |
| 1130 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1131 | if (root->left) |
| 1132 | return rb_entry(root->left, struct cfq_queue, rb_node); |
| 1133 | |
| 1134 | return NULL; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1137 | static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root) |
| 1138 | { |
| 1139 | if (!root->left) |
| 1140 | root->left = rb_first(&root->rb); |
| 1141 | |
| 1142 | if (root->left) |
| 1143 | return rb_entry_cfqg(root->left); |
| 1144 | |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1148 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
| 1149 | { |
| 1150 | rb_erase(n, root); |
| 1151 | RB_CLEAR_NODE(n); |
| 1152 | } |
| 1153 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1154 | static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) |
| 1155 | { |
| 1156 | if (root->left == n) |
| 1157 | root->left = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1158 | rb_erase_init(n, &root->rb); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1159 | --root->count; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | /* |
| 1163 | * would be nice to take fifo expire time into account as well |
| 1164 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1165 | static struct request * |
| 1166 | cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 1167 | struct request *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1169 | struct rb_node *rbnext = rb_next(&last->rb_node); |
| 1170 | struct rb_node *rbprev = rb_prev(&last->rb_node); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1171 | struct request *next = NULL, *prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1173 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
| 1175 | if (rbprev) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1176 | prev = rb_entry_rq(rbprev); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (rbnext) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1179 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1180 | else { |
| 1181 | rbnext = rb_first(&cfqq->sort_list); |
| 1182 | if (rbnext && rbnext != &last->rb_node) |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 1183 | next = rb_entry_rq(rbnext); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 1184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 1186 | return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1189 | static unsigned long cfq_slice_offset(struct cfq_data *cfqd, |
| 1190 | struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | { |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1192 | /* |
| 1193 | * just an approximation, should be ok. |
| 1194 | */ |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 1195 | return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) - |
Jens Axboe | 464191c | 2009-11-30 09:38:13 +0100 | [diff] [blame] | 1196 | cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1199 | static inline s64 |
| 1200 | cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1201 | { |
| 1202 | return cfqg->vdisktime - st->min_vdisktime; |
| 1203 | } |
| 1204 | |
| 1205 | static void |
| 1206 | __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1207 | { |
| 1208 | struct rb_node **node = &st->rb.rb_node; |
| 1209 | struct rb_node *parent = NULL; |
| 1210 | struct cfq_group *__cfqg; |
| 1211 | s64 key = cfqg_key(st, cfqg); |
| 1212 | int left = 1; |
| 1213 | |
| 1214 | while (*node != NULL) { |
| 1215 | parent = *node; |
| 1216 | __cfqg = rb_entry_cfqg(parent); |
| 1217 | |
| 1218 | if (key < cfqg_key(st, __cfqg)) |
| 1219 | node = &parent->rb_left; |
| 1220 | else { |
| 1221 | node = &parent->rb_right; |
| 1222 | left = 0; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (left) |
| 1227 | st->left = &cfqg->rb_node; |
| 1228 | |
| 1229 | rb_link_node(&cfqg->rb_node, parent, node); |
| 1230 | rb_insert_color(&cfqg->rb_node, &st->rb); |
| 1231 | } |
| 1232 | |
| 1233 | static void |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1234 | cfq_update_group_weight(struct cfq_group *cfqg) |
| 1235 | { |
| 1236 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1237 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1238 | if (cfqg->new_weight) { |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1239 | cfqg->weight = cfqg->new_weight; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1240 | cfqg->new_weight = 0; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1241 | } |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1242 | |
| 1243 | if (cfqg->new_leaf_weight) { |
| 1244 | cfqg->leaf_weight = cfqg->new_leaf_weight; |
| 1245 | cfqg->new_leaf_weight = 0; |
| 1246 | } |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | static void |
| 1250 | cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1251 | { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1252 | unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */ |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1253 | struct cfq_group *pos = cfqg; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1254 | struct cfq_group *parent; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1255 | bool propagate; |
| 1256 | |
| 1257 | /* add to the service tree */ |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1258 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); |
| 1259 | |
| 1260 | cfq_update_group_weight(cfqg); |
| 1261 | __cfq_group_service_tree_add(st, cfqg); |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1262 | |
| 1263 | /* |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1264 | * Activate @cfqg and calculate the portion of vfraction @cfqg is |
| 1265 | * entitled to. vfraction is calculated by walking the tree |
| 1266 | * towards the root calculating the fraction it has at each level. |
| 1267 | * The compounded ratio is how much vfraction @cfqg owns. |
| 1268 | * |
| 1269 | * Start with the proportion tasks in this cfqg has against active |
| 1270 | * children cfqgs - its leaf_weight against children_weight. |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1271 | */ |
| 1272 | propagate = !pos->nr_active++; |
| 1273 | pos->children_weight += pos->leaf_weight; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1274 | vfr = vfr * pos->leaf_weight / pos->children_weight; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1275 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1276 | /* |
| 1277 | * Compound ->weight walking up the tree. Both activation and |
| 1278 | * vfraction calculation are done in the same loop. Propagation |
| 1279 | * stops once an already activated node is met. vfraction |
| 1280 | * calculation should always continue to the root. |
| 1281 | */ |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1282 | while ((parent = cfqg_parent(pos))) { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1283 | if (propagate) { |
| 1284 | propagate = !parent->nr_active++; |
| 1285 | parent->children_weight += pos->weight; |
| 1286 | } |
| 1287 | vfr = vfr * pos->weight / parent->children_weight; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1288 | pos = parent; |
| 1289 | } |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1290 | |
| 1291 | cfqg->vfraction = max_t(unsigned, vfr, 1); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static void |
| 1295 | cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1296 | { |
| 1297 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 1298 | struct cfq_group *__cfqg; |
| 1299 | struct rb_node *n; |
| 1300 | |
| 1301 | cfqg->nr_cfqq++; |
Gui Jianfeng | 760701b | 2010-11-30 20:52:47 +0100 | [diff] [blame] | 1302 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1303 | return; |
| 1304 | |
| 1305 | /* |
| 1306 | * Currently put the group at the end. Later implement something |
| 1307 | * so that groups get lesser vtime based on their weights, so that |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1308 | * if group does not loose all if it was not continuously backlogged. |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1309 | */ |
| 1310 | n = rb_last(&st->rb); |
| 1311 | if (n) { |
| 1312 | __cfqg = rb_entry_cfqg(n); |
| 1313 | cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY; |
| 1314 | } else |
| 1315 | cfqg->vdisktime = st->min_vdisktime; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1316 | cfq_group_service_tree_add(st, cfqg); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | static void |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1320 | cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg) |
| 1321 | { |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1322 | struct cfq_group *pos = cfqg; |
| 1323 | bool propagate; |
| 1324 | |
| 1325 | /* |
| 1326 | * Undo activation from cfq_group_service_tree_add(). Deactivate |
| 1327 | * @cfqg and propagate deactivation upwards. |
| 1328 | */ |
| 1329 | propagate = !--pos->nr_active; |
| 1330 | pos->children_weight -= pos->leaf_weight; |
| 1331 | |
| 1332 | while (propagate) { |
Tejun Heo | d02f7aa | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1333 | struct cfq_group *parent = cfqg_parent(pos); |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1334 | |
| 1335 | /* @pos has 0 nr_active at this point */ |
| 1336 | WARN_ON_ONCE(pos->children_weight); |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1337 | pos->vfraction = 0; |
Tejun Heo | 7918ffb | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1338 | |
| 1339 | if (!parent) |
| 1340 | break; |
| 1341 | |
| 1342 | propagate = !--parent->nr_active; |
| 1343 | parent->children_weight -= pos->weight; |
| 1344 | pos = parent; |
| 1345 | } |
| 1346 | |
| 1347 | /* remove from the service tree */ |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1348 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
| 1349 | cfq_rb_erase(&cfqg->rb_node, st); |
| 1350 | } |
| 1351 | |
| 1352 | static void |
| 1353 | cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1354 | { |
| 1355 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
| 1356 | |
| 1357 | BUG_ON(cfqg->nr_cfqq < 1); |
| 1358 | cfqg->nr_cfqq--; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 1359 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1360 | /* If there are other cfq queues under this group, don't delete it */ |
| 1361 | if (cfqg->nr_cfqq) |
| 1362 | return; |
| 1363 | |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 1364 | cfq_log_cfqg(cfqd, cfqg, "del_from_rr group"); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1365 | cfq_group_service_tree_del(st, cfqg); |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1366 | cfqg->saved_wl_slice = 0; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1367 | cfqg_stats_update_dequeue(cfqg); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1368 | } |
| 1369 | |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1370 | static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq, |
| 1371 | unsigned int *unaccounted_time) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1372 | { |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 1373 | unsigned int slice_used; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1374 | |
| 1375 | /* |
| 1376 | * Queue got expired before even a single request completed or |
| 1377 | * got expired immediately after first request completion. |
| 1378 | */ |
| 1379 | if (!cfqq->slice_start || cfqq->slice_start == jiffies) { |
| 1380 | /* |
| 1381 | * Also charge the seek time incurred to the group, otherwise |
| 1382 | * if there are mutiple queues in the group, each can dispatch |
| 1383 | * a single request on seeky media and cause lots of seek time |
| 1384 | * and group will never know it. |
| 1385 | */ |
| 1386 | slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start), |
| 1387 | 1); |
| 1388 | } else { |
| 1389 | slice_used = jiffies - cfqq->slice_start; |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1390 | if (slice_used > cfqq->allocated_slice) { |
| 1391 | *unaccounted_time = slice_used - cfqq->allocated_slice; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 1392 | slice_used = cfqq->allocated_slice; |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1393 | } |
| 1394 | if (time_after(cfqq->slice_start, cfqq->dispatch_start)) |
| 1395 | *unaccounted_time += cfqq->slice_start - |
| 1396 | cfqq->dispatch_start; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1397 | } |
| 1398 | |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1399 | return slice_used; |
| 1400 | } |
| 1401 | |
| 1402 | static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg, |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 1403 | struct cfq_queue *cfqq) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1404 | { |
| 1405 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1406 | unsigned int used_sl, charge, unaccounted_sl = 0; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1407 | int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg) |
| 1408 | - cfqg->service_tree_idle.count; |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1409 | unsigned int vfr; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1410 | |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1411 | BUG_ON(nr_sync < 0); |
Justin TerAvest | 167400d | 2011-03-12 16:54:00 +0100 | [diff] [blame] | 1412 | used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl); |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 1413 | |
Vivek Goyal | 02b3508 | 2010-08-23 12:23:53 +0200 | [diff] [blame] | 1414 | if (iops_mode(cfqd)) |
| 1415 | charge = cfqq->slice_dispatch; |
| 1416 | else if (!cfq_cfqq_sync(cfqq) && !nr_sync) |
| 1417 | charge = cfqq->allocated_slice; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1418 | |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1419 | /* |
| 1420 | * Can't update vdisktime while on service tree and cfqg->vfraction |
| 1421 | * is valid only while on it. Cache vfr, leave the service tree, |
| 1422 | * update vdisktime and go back on. The re-addition to the tree |
| 1423 | * will also update the weights as necessary. |
| 1424 | */ |
| 1425 | vfr = cfqg->vfraction; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1426 | cfq_group_service_tree_del(st, cfqg); |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1427 | cfqg->vdisktime += cfqg_scale_charge(charge, vfr); |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1428 | cfq_group_service_tree_add(st, cfqg); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1429 | |
| 1430 | /* This group is being expired. Save the context */ |
| 1431 | if (time_after(cfqd->workload_expires, jiffies)) { |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1432 | cfqg->saved_wl_slice = cfqd->workload_expires |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1433 | - jiffies; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1434 | cfqg->saved_wl_type = cfqd->serving_wl_type; |
| 1435 | cfqg->saved_wl_class = cfqd->serving_wl_class; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1436 | } else |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 1437 | cfqg->saved_wl_slice = 0; |
Vivek Goyal | 2868ef7 | 2009-12-03 12:59:48 -0500 | [diff] [blame] | 1438 | |
| 1439 | cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime, |
| 1440 | st->min_vdisktime); |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 1441 | cfq_log_cfqq(cfqq->cfqd, cfqq, |
| 1442 | "sl_used=%u disp=%u charge=%u iops=%u sect=%lu", |
| 1443 | used_sl, cfqq->slice_dispatch, charge, |
| 1444 | iops_mode(cfqd), cfqq->nr_sectors); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1445 | cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl); |
| 1446 | cfqg_stats_set_start_empty_time(cfqg); |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 1447 | } |
| 1448 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1449 | /** |
| 1450 | * cfq_init_cfqg_base - initialize base part of a cfq_group |
| 1451 | * @cfqg: cfq_group to initialize |
| 1452 | * |
| 1453 | * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED |
| 1454 | * is enabled or not. |
| 1455 | */ |
| 1456 | static void cfq_init_cfqg_base(struct cfq_group *cfqg) |
| 1457 | { |
| 1458 | struct cfq_rb_root *st; |
| 1459 | int i, j; |
| 1460 | |
| 1461 | for_each_cfqg_st(cfqg, i, j, st) |
| 1462 | *st = CFQ_RB_ROOT; |
| 1463 | RB_CLEAR_NODE(&cfqg->rb_node); |
| 1464 | |
| 1465 | cfqg->ttime.last_end_request = jiffies; |
| 1466 | } |
| 1467 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1468 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1469 | static void cfq_pd_init(struct blkcg_gq *blkg) |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1470 | { |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1471 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1472 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1473 | cfq_init_cfqg_base(cfqg); |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1474 | cfqg->weight = blkg->blkcg->cfq_weight; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1475 | cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
Tejun Heo | 689665a | 2013-01-09 08:05:13 -0800 | [diff] [blame^] | 1478 | static void cfq_pd_reset_stats(struct blkcg_gq *blkg) |
| 1479 | { |
| 1480 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
| 1481 | |
| 1482 | cfqg_stats_reset(&cfqg->stats); |
| 1483 | } |
| 1484 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1485 | /* |
Vivek Goyal | 3e59cf9 | 2011-05-19 15:38:21 -0400 | [diff] [blame] | 1486 | * Search for the cfq group current task belongs to. request_queue lock must |
| 1487 | * be held. |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1488 | */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1489 | static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1490 | struct blkcg *blkcg) |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1491 | { |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1492 | struct request_queue *q = cfqd->queue; |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1493 | struct cfq_group *cfqg = NULL; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1494 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1495 | /* avoid lookup for the common case where there's no blkcg */ |
| 1496 | if (blkcg == &blkcg_root) { |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1497 | cfqg = cfqd->root_group; |
| 1498 | } else { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1499 | struct blkcg_gq *blkg; |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1500 | |
Tejun Heo | 3c96cb3 | 2012-04-13 13:11:34 -0700 | [diff] [blame] | 1501 | blkg = blkg_lookup_create(blkcg, q); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1502 | if (!IS_ERR(blkg)) |
Tejun Heo | 0381411 | 2012-03-05 13:15:14 -0800 | [diff] [blame] | 1503 | cfqg = blkg_to_cfqg(blkg); |
Vivek Goyal | f469a7b | 2011-05-19 15:38:23 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1506 | return cfqg; |
| 1507 | } |
| 1508 | |
| 1509 | static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) |
| 1510 | { |
| 1511 | /* Currently, all async queues are mapped to root group */ |
| 1512 | if (!cfq_cfqq_sync(cfqq)) |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1513 | cfqg = cfqq->cfqd->root_group; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1514 | |
| 1515 | cfqq->cfqg = cfqg; |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1516 | /* cfqq reference on cfqg */ |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 1517 | cfqg_get(cfqg); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 1518 | } |
| 1519 | |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1520 | static u64 cfqg_prfill_weight_device(struct seq_file *sf, |
| 1521 | struct blkg_policy_data *pd, int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1522 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1523 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1524 | |
| 1525 | if (!cfqg->dev_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1526 | return 0; |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1527 | return __blkg_prfill_u64(sf, pd, cfqg->dev_weight); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1530 | static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1531 | struct seq_file *sf) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1532 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1533 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), |
| 1534 | cfqg_prfill_weight_device, &blkcg_policy_cfq, 0, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1535 | false); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1539 | static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf, |
| 1540 | struct blkg_policy_data *pd, int off) |
| 1541 | { |
| 1542 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
| 1543 | |
| 1544 | if (!cfqg->dev_leaf_weight) |
| 1545 | return 0; |
| 1546 | return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight); |
| 1547 | } |
| 1548 | |
| 1549 | static int cfqg_print_leaf_weight_device(struct cgroup *cgrp, |
| 1550 | struct cftype *cft, |
| 1551 | struct seq_file *sf) |
| 1552 | { |
| 1553 | blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), |
| 1554 | cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0, |
| 1555 | false); |
| 1556 | return 0; |
| 1557 | } |
| 1558 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1559 | static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft, |
| 1560 | struct seq_file *sf) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1561 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1562 | seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1563 | return 0; |
| 1564 | } |
| 1565 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1566 | static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft, |
| 1567 | struct seq_file *sf) |
| 1568 | { |
| 1569 | seq_printf(sf, "%u\n", |
| 1570 | cgroup_to_blkcg(cgrp)->cfq_leaf_weight); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1575 | const char *buf, bool is_leaf_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1576 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1577 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1578 | struct blkg_conf_ctx ctx; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1579 | struct cfq_group *cfqg; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1580 | int ret; |
| 1581 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1582 | ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1583 | if (ret) |
| 1584 | return ret; |
| 1585 | |
| 1586 | ret = -EINVAL; |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1587 | cfqg = blkg_to_cfqg(ctx.blkg); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 1588 | if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) { |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1589 | if (!is_leaf_weight) { |
| 1590 | cfqg->dev_weight = ctx.v; |
| 1591 | cfqg->new_weight = ctx.v ?: blkcg->cfq_weight; |
| 1592 | } else { |
| 1593 | cfqg->dev_leaf_weight = ctx.v; |
| 1594 | cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight; |
| 1595 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1596 | ret = 0; |
| 1597 | } |
| 1598 | |
| 1599 | blkg_conf_finish(&ctx); |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1603 | static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1604 | const char *buf) |
| 1605 | { |
| 1606 | return __cfqg_set_weight_device(cgrp, cft, buf, false); |
| 1607 | } |
| 1608 | |
| 1609 | static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft, |
| 1610 | const char *buf) |
| 1611 | { |
| 1612 | return __cfqg_set_weight_device(cgrp, cft, buf, true); |
| 1613 | } |
| 1614 | |
| 1615 | static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val, |
| 1616 | bool is_leaf_weight) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1617 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1618 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
| 1619 | struct blkcg_gq *blkg; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1620 | struct hlist_node *n; |
| 1621 | |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1622 | if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1623 | return -EINVAL; |
| 1624 | |
| 1625 | spin_lock_irq(&blkcg->lock); |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1626 | |
| 1627 | if (!is_leaf_weight) |
| 1628 | blkcg->cfq_weight = val; |
| 1629 | else |
| 1630 | blkcg->cfq_leaf_weight = val; |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1631 | |
| 1632 | hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) { |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1633 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1634 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1635 | if (!cfqg) |
| 1636 | continue; |
| 1637 | |
| 1638 | if (!is_leaf_weight) { |
| 1639 | if (!cfqg->dev_weight) |
| 1640 | cfqg->new_weight = blkcg->cfq_weight; |
| 1641 | } else { |
| 1642 | if (!cfqg->dev_leaf_weight) |
| 1643 | cfqg->new_leaf_weight = blkcg->cfq_leaf_weight; |
| 1644 | } |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | spin_unlock_irq(&blkcg->lock); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1651 | static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val) |
| 1652 | { |
| 1653 | return __cfq_set_weight(cgrp, cft, val, false); |
| 1654 | } |
| 1655 | |
| 1656 | static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val) |
| 1657 | { |
| 1658 | return __cfq_set_weight(cgrp, cft, val, true); |
| 1659 | } |
| 1660 | |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1661 | static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft, |
| 1662 | struct seq_file *sf) |
| 1663 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1664 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1665 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1666 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq, |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1667 | cft->private, false); |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft, |
| 1672 | struct seq_file *sf) |
| 1673 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1674 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1675 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1676 | blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq, |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1677 | cft->private, true); |
| 1678 | return 0; |
| 1679 | } |
| 1680 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1681 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1682 | static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf, |
| 1683 | struct blkg_policy_data *pd, int off) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1684 | { |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1685 | struct cfq_group *cfqg = pd_to_cfqg(pd); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1686 | u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1687 | u64 v = 0; |
| 1688 | |
| 1689 | if (samples) { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1690 | v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1691 | do_div(v, samples); |
| 1692 | } |
Tejun Heo | f95a04a | 2012-04-16 13:57:26 -0700 | [diff] [blame] | 1693 | __blkg_prfill_u64(sf, pd, v); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* print avg_queue_size */ |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1698 | static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft, |
| 1699 | struct seq_file *sf) |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1700 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1701 | struct blkcg *blkcg = cgroup_to_blkcg(cgrp); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1702 | |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1703 | blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1704 | &blkcg_policy_cfq, 0, false); |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1705 | return 0; |
| 1706 | } |
| 1707 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 1708 | |
| 1709 | static struct cftype cfq_blkcg_files[] = { |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1710 | /* on root, weight is mapped to leaf_weight */ |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1711 | { |
| 1712 | .name = "weight_device", |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1713 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 1714 | .read_seq_string = cfqg_print_leaf_weight_device, |
| 1715 | .write_string = cfqg_set_leaf_weight_device, |
| 1716 | .max_write_len = 256, |
| 1717 | }, |
| 1718 | { |
| 1719 | .name = "weight", |
| 1720 | .flags = CFTYPE_ONLY_ON_ROOT, |
| 1721 | .read_seq_string = cfq_print_leaf_weight, |
| 1722 | .write_u64 = cfq_set_leaf_weight, |
| 1723 | }, |
| 1724 | |
| 1725 | /* no such mapping necessary for !roots */ |
| 1726 | { |
| 1727 | .name = "weight_device", |
| 1728 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1729 | .read_seq_string = cfqg_print_weight_device, |
| 1730 | .write_string = cfqg_set_weight_device, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1731 | .max_write_len = 256, |
| 1732 | }, |
| 1733 | { |
| 1734 | .name = "weight", |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1735 | .flags = CFTYPE_NOT_ON_ROOT, |
Tejun Heo | 1d3650f | 2013-01-09 08:05:11 -0800 | [diff] [blame] | 1736 | .read_seq_string = cfq_print_weight, |
| 1737 | .write_u64 = cfq_set_weight, |
| 1738 | }, |
| 1739 | |
| 1740 | { |
| 1741 | .name = "leaf_weight_device", |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1742 | .read_seq_string = cfqg_print_leaf_weight_device, |
| 1743 | .write_string = cfqg_set_leaf_weight_device, |
| 1744 | .max_write_len = 256, |
| 1745 | }, |
| 1746 | { |
| 1747 | .name = "leaf_weight", |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 1748 | .read_seq_string = cfq_print_leaf_weight, |
| 1749 | .write_u64 = cfq_set_leaf_weight, |
| 1750 | }, |
| 1751 | |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1752 | { |
| 1753 | .name = "time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1754 | .private = offsetof(struct cfq_group, stats.time), |
| 1755 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1756 | }, |
| 1757 | { |
| 1758 | .name = "sectors", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1759 | .private = offsetof(struct cfq_group, stats.sectors), |
| 1760 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1761 | }, |
| 1762 | { |
| 1763 | .name = "io_service_bytes", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1764 | .private = offsetof(struct cfq_group, stats.service_bytes), |
| 1765 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1766 | }, |
| 1767 | { |
| 1768 | .name = "io_serviced", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1769 | .private = offsetof(struct cfq_group, stats.serviced), |
| 1770 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1771 | }, |
| 1772 | { |
| 1773 | .name = "io_service_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1774 | .private = offsetof(struct cfq_group, stats.service_time), |
| 1775 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1776 | }, |
| 1777 | { |
| 1778 | .name = "io_wait_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1779 | .private = offsetof(struct cfq_group, stats.wait_time), |
| 1780 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1781 | }, |
| 1782 | { |
| 1783 | .name = "io_merged", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1784 | .private = offsetof(struct cfq_group, stats.merged), |
| 1785 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1786 | }, |
| 1787 | { |
| 1788 | .name = "io_queued", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1789 | .private = offsetof(struct cfq_group, stats.queued), |
| 1790 | .read_seq_string = cfqg_print_rwstat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1791 | }, |
| 1792 | #ifdef CONFIG_DEBUG_BLK_CGROUP |
| 1793 | { |
| 1794 | .name = "avg_queue_size", |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 1795 | .read_seq_string = cfqg_print_avg_queue_size, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1796 | }, |
| 1797 | { |
| 1798 | .name = "group_wait_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1799 | .private = offsetof(struct cfq_group, stats.group_wait_time), |
| 1800 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1801 | }, |
| 1802 | { |
| 1803 | .name = "idle_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1804 | .private = offsetof(struct cfq_group, stats.idle_time), |
| 1805 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1806 | }, |
| 1807 | { |
| 1808 | .name = "empty_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1809 | .private = offsetof(struct cfq_group, stats.empty_time), |
| 1810 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1811 | }, |
| 1812 | { |
| 1813 | .name = "dequeue", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1814 | .private = offsetof(struct cfq_group, stats.dequeue), |
| 1815 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1816 | }, |
| 1817 | { |
| 1818 | .name = "unaccounted_time", |
Tejun Heo | 5bc4afb1 | 2012-04-01 14:38:45 -0700 | [diff] [blame] | 1819 | .private = offsetof(struct cfq_group, stats.unaccounted_time), |
| 1820 | .read_seq_string = cfqg_print_stat, |
Tejun Heo | 60c2bc2 | 2012-04-01 14:38:43 -0700 | [diff] [blame] | 1821 | }, |
| 1822 | #endif /* CONFIG_DEBUG_BLK_CGROUP */ |
| 1823 | { } /* terminate */ |
| 1824 | }; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1825 | #else /* GROUP_IOSCHED */ |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 1826 | static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd, |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 1827 | struct blkcg *blkcg) |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1828 | { |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 1829 | return cfqd->root_group; |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1830 | } |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 1831 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 1832 | static inline void |
| 1833 | cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) { |
| 1834 | cfqq->cfqg = cfqg; |
| 1835 | } |
| 1836 | |
| 1837 | #endif /* GROUP_IOSCHED */ |
| 1838 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1839 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1840 | * The cfqd->service_trees holds all pending cfq_queue's that have |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1841 | * requests waiting to be processed. It is sorted in the order that |
| 1842 | * we will service the queues. |
| 1843 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1844 | static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 1845 | bool add_front) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1846 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1847 | struct rb_node **p, *parent; |
| 1848 | struct cfq_queue *__cfqq; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1849 | unsigned long rb_key; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1850 | struct cfq_rb_root *st; |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1851 | int left; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1852 | int new_cfqq = 1; |
Vivek Goyal | ae30c28 | 2009-12-03 12:59:55 -0500 | [diff] [blame] | 1853 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1854 | st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq)); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1855 | if (cfq_class_idle(cfqq)) { |
| 1856 | rb_key = CFQ_IDLE_DELAY; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1857 | parent = rb_last(&st->rb); |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1858 | if (parent && parent != &cfqq->rb_node) { |
| 1859 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 1860 | rb_key += __cfqq->rb_key; |
| 1861 | } else |
| 1862 | rb_key += jiffies; |
| 1863 | } else if (!add_front) { |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 1864 | /* |
| 1865 | * Get our rb key offset. Subtract any residual slice |
| 1866 | * value carried from last service. A negative resid |
| 1867 | * count indicates slice overrun, and this should position |
| 1868 | * the next service time further away in the tree. |
| 1869 | */ |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1870 | rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies; |
Jens Axboe | b9c8946 | 2009-10-06 20:53:44 +0200 | [diff] [blame] | 1871 | rb_key -= cfqq->slice_resid; |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1872 | cfqq->slice_resid = 0; |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 1873 | } else { |
| 1874 | rb_key = -HZ; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1875 | __cfqq = cfq_rb_first(st); |
Corrado Zoccolo | 48e025e | 2009-10-05 08:49:23 +0200 | [diff] [blame] | 1876 | rb_key += __cfqq ? __cfqq->rb_key : jiffies; |
| 1877 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1878 | |
| 1879 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1880 | new_cfqq = 0; |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 1881 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1882 | * same position, nothing more to do |
Jens Axboe | 99f9628 | 2007-02-05 11:56:25 +0100 | [diff] [blame] | 1883 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1884 | if (rb_key == cfqq->rb_key && cfqq->service_tree == st) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1885 | return; |
Jens Axboe | 53b03744 | 2006-07-28 09:48:51 +0200 | [diff] [blame] | 1886 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 1887 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 1888 | cfqq->service_tree = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1889 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1890 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1891 | left = 1; |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 1892 | parent = NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1893 | cfqq->service_tree = st; |
| 1894 | p = &st->rb.rb_node; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1895 | while (*p) { |
| 1896 | parent = *p; |
| 1897 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); |
| 1898 | |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 1899 | /* |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1900 | * sort by key, that represents service time. |
Jens Axboe | 0c534e0 | 2007-04-18 20:01:57 +0200 | [diff] [blame] | 1901 | */ |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1902 | if (time_before(rb_key, __cfqq->rb_key)) |
Vivek Goyal | 1f23f12 | 2012-10-03 16:57:00 -0400 | [diff] [blame] | 1903 | p = &parent->rb_left; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1904 | else { |
Vivek Goyal | 1f23f12 | 2012-10-03 16:57:00 -0400 | [diff] [blame] | 1905 | p = &parent->rb_right; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1906 | left = 0; |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 1907 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1910 | if (left) |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1911 | st->left = &cfqq->rb_node; |
Jens Axboe | cc09e29 | 2007-04-26 12:53:50 +0200 | [diff] [blame] | 1912 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 1913 | cfqq->rb_key = rb_key; |
| 1914 | rb_link_node(&cfqq->rb_node, parent, p); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 1915 | rb_insert_color(&cfqq->rb_node, &st->rb); |
| 1916 | st->count++; |
Namhyung Kim | 20359f2 | 2011-05-24 10:23:22 +0200 | [diff] [blame] | 1917 | if (add_front || !new_cfqq) |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 1918 | return; |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 1919 | cfq_group_notify_queue_add(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1922 | static struct cfq_queue * |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1923 | cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root, |
| 1924 | sector_t sector, struct rb_node **ret_parent, |
| 1925 | struct rb_node ***rb_link) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1926 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1927 | struct rb_node **p, *parent; |
| 1928 | struct cfq_queue *cfqq = NULL; |
| 1929 | |
| 1930 | parent = NULL; |
| 1931 | p = &root->rb_node; |
| 1932 | while (*p) { |
| 1933 | struct rb_node **n; |
| 1934 | |
| 1935 | parent = *p; |
| 1936 | cfqq = rb_entry(parent, struct cfq_queue, p_node); |
| 1937 | |
| 1938 | /* |
| 1939 | * Sort strictly based on sector. Smallest to the left, |
| 1940 | * largest to the right. |
| 1941 | */ |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1942 | if (sector > blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1943 | n = &(*p)->rb_right; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1944 | else if (sector < blk_rq_pos(cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1945 | n = &(*p)->rb_left; |
| 1946 | else |
| 1947 | break; |
| 1948 | p = n; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1949 | cfqq = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | *ret_parent = parent; |
| 1953 | if (rb_link) |
| 1954 | *rb_link = p; |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1955 | return cfqq; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 1959 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1960 | struct rb_node **p, *parent; |
| 1961 | struct cfq_queue *__cfqq; |
| 1962 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1963 | if (cfqq->p_root) { |
| 1964 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 1965 | cfqq->p_root = NULL; |
| 1966 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1967 | |
| 1968 | if (cfq_class_idle(cfqq)) |
| 1969 | return; |
| 1970 | if (!cfqq->next_rq) |
| 1971 | return; |
| 1972 | |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1973 | cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio]; |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 1974 | __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root, |
| 1975 | blk_rq_pos(cfqq->next_rq), &parent, &p); |
Jens Axboe | 3ac6c9f | 2009-04-23 12:14:56 +0200 | [diff] [blame] | 1976 | if (!__cfqq) { |
| 1977 | rb_link_node(&cfqq->p_node, parent, p); |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 1978 | rb_insert_color(&cfqq->p_node, cfqq->p_root); |
| 1979 | } else |
| 1980 | cfqq->p_root = NULL; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1981 | } |
| 1982 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 1983 | /* |
| 1984 | * Update cfqq's position in the service tree. |
| 1985 | */ |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1986 | static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1987 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1988 | /* |
| 1989 | * Resorting requires the cfqq to be on the RR list already. |
| 1990 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1991 | if (cfq_cfqq_on_rr(cfqq)) { |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 1992 | cfq_service_tree_add(cfqd, cfqq, 0); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 1993 | cfq_prio_tree_add(cfqd, cfqq); |
| 1994 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | /* |
| 1998 | * add to busy list of queues for service, trying to be fair in ordering |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 1999 | * the pending list according to last request service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2001 | static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2003 | cfq_log_cfqq(cfqd, cfqq, "add_to_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2004 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
| 2005 | cfq_mark_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | cfqd->busy_queues++; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 2007 | if (cfq_cfqq_sync(cfqq)) |
| 2008 | cfqd->busy_sync_queues++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2010 | cfq_resort_rr_list(cfqd, cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2013 | /* |
| 2014 | * Called when the cfqq no longer has requests pending, remove it from |
| 2015 | * the service tree. |
| 2016 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2017 | static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2019 | cfq_log_cfqq(cfqd, cfqq, "del_from_rr"); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2020 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
| 2021 | cfq_clear_cfqq_on_rr(cfqq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 2023 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
| 2024 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
| 2025 | cfqq->service_tree = NULL; |
| 2026 | } |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2027 | if (cfqq->p_root) { |
| 2028 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 2029 | cfqq->p_root = NULL; |
| 2030 | } |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2031 | |
Justin TerAvest | 8184f93 | 2011-03-17 16:12:36 +0100 | [diff] [blame] | 2032 | cfq_group_notify_queue_del(cfqd, cfqq->cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | BUG_ON(!cfqd->busy_queues); |
| 2034 | cfqd->busy_queues--; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 2035 | if (cfq_cfqq_sync(cfqq)) |
| 2036 | cfqd->busy_sync_queues--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * rb tree support functions |
| 2041 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2042 | static void cfq_del_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2044 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2045 | const int sync = rq_is_sync(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2047 | BUG_ON(!cfqq->queued[sync]); |
| 2048 | cfqq->queued[sync]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2050 | elv_rb_del(&cfqq->sort_list, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2052 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) { |
| 2053 | /* |
| 2054 | * Queue will be deleted from service tree when we actually |
| 2055 | * expire it later. Right now just remove it from prio tree |
| 2056 | * as it is empty. |
| 2057 | */ |
| 2058 | if (cfqq->p_root) { |
| 2059 | rb_erase(&cfqq->p_node, cfqq->p_root); |
| 2060 | cfqq->p_root = NULL; |
| 2061 | } |
| 2062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2065 | static void cfq_add_rq_rb(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2067 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | struct cfq_data *cfqd = cfqq->cfqd; |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 2069 | struct request *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2071 | cfqq->queued[rq_is_sync(rq)]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | |
Jeff Moyer | 796d511 | 2011-06-02 21:19:05 +0200 | [diff] [blame] | 2073 | elv_rb_add(&cfqq->sort_list, rq); |
Jens Axboe | 5fccbf6 | 2006-10-31 14:21:55 +0100 | [diff] [blame] | 2074 | |
| 2075 | if (!cfq_cfqq_on_rr(cfqq)) |
| 2076 | cfq_add_cfqq_rr(cfqd, cfqq); |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 2077 | |
| 2078 | /* |
| 2079 | * check if this request is a better next-serve candidate |
| 2080 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2081 | prev = cfqq->next_rq; |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2082 | cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* |
| 2085 | * adjust priority tree position, if ->next_rq changes |
| 2086 | */ |
| 2087 | if (prev != cfqq->next_rq) |
| 2088 | cfq_prio_tree_add(cfqd, cfqq); |
| 2089 | |
Jens Axboe | 5044eed | 2007-04-25 11:53:48 +0200 | [diff] [blame] | 2090 | BUG_ON(!cfqq->next_rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2093 | static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | { |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2095 | elv_rb_del(&cfqq->sort_list, rq); |
| 2096 | cfqq->queued[rq_is_sync(rq)]--; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2097 | cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2098 | cfq_add_rq_rb(rq); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2099 | cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group, |
| 2100 | rq->cmd_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2103 | static struct request * |
| 2104 | cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | { |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2106 | struct task_struct *tsk = current; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2107 | struct cfq_io_cq *cic; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2108 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 2110 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2111 | if (!cic) |
| 2112 | return NULL; |
| 2113 | |
| 2114 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2115 | if (cfqq) { |
| 2116 | sector_t sector = bio->bi_sector + bio_sectors(bio); |
| 2117 | |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2118 | return elv_rb_find(&cfqq->sort_list, sector); |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | return NULL; |
| 2122 | } |
| 2123 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2124 | static void cfq_activate_request(struct request_queue *q, struct request *rq) |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2125 | { |
| 2126 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2127 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2128 | cfqd->rq_in_driver++; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2129 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2130 | cfqd->rq_in_driver); |
Jens Axboe | 25776e3 | 2006-06-01 10:12:26 +0200 | [diff] [blame] | 2131 | |
Tejun Heo | 5b93629 | 2009-05-07 22:24:38 +0900 | [diff] [blame] | 2132 | cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2133 | } |
| 2134 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2135 | static void cfq_deactivate_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2137 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2139 | WARN_ON(!cfqd->rq_in_driver); |
| 2140 | cfqd->rq_in_driver--; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2141 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2142 | cfqd->rq_in_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2145 | static void cfq_remove_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2147 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2148 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2149 | if (cfqq->next_rq == rq) |
| 2150 | cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2152 | list_del_init(&rq->queuelist); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2153 | cfq_del_rq_rb(rq); |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 2154 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 2155 | cfqq->cfqd->rq_queued--; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2156 | cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags); |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 2157 | if (rq->cmd_flags & REQ_PRIO) { |
| 2158 | WARN_ON(!cfqq->prio_pending); |
| 2159 | cfqq->prio_pending--; |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 2160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2163 | static int cfq_merge(struct request_queue *q, struct request **req, |
| 2164 | struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | { |
| 2166 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2167 | struct request *__rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2169 | __rq = cfq_find_rq_fmerge(cfqd, bio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2170 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
Jens Axboe | 9817064 | 2006-07-28 09:23:08 +0200 | [diff] [blame] | 2171 | *req = __rq; |
| 2172 | return ELEVATOR_FRONT_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | return ELEVATOR_NO_MERGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | } |
| 2177 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2178 | static void cfq_merged_request(struct request_queue *q, struct request *req, |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2179 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | { |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 2181 | if (type == ELEVATOR_FRONT_MERGE) { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2182 | struct cfq_queue *cfqq = RQ_CFQQ(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2184 | cfq_reposition_rq_rb(cfqq, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 2188 | static void cfq_bio_merged(struct request_queue *q, struct request *req, |
| 2189 | struct bio *bio) |
| 2190 | { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2191 | cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw); |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | static void |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2195 | cfq_merged_requests(struct request_queue *q, struct request *rq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | struct request *next) |
| 2197 | { |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2198 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Shaohua Li | 4a0b75c | 2011-12-16 14:00:22 +0100 | [diff] [blame] | 2199 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 2200 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2201 | /* |
| 2202 | * reposition in fifo if next is older than rq |
| 2203 | */ |
| 2204 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && |
Shaohua Li | 3d106fba | 2012-11-06 12:39:51 +0100 | [diff] [blame] | 2205 | time_before(rq_fifo_time(next), rq_fifo_time(rq)) && |
| 2206 | cfqq == RQ_CFQQ(next)) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2207 | list_move(&rq->queuelist, &next->queuelist); |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 2208 | rq_set_fifo_time(rq, rq_fifo_time(next)); |
| 2209 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2210 | |
Corrado Zoccolo | cf7c25c | 2009-11-08 17:16:46 +0100 | [diff] [blame] | 2211 | if (cfqq->next_rq == next) |
| 2212 | cfqq->next_rq = rq; |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 2213 | cfq_remove_request(next); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2214 | cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags); |
Shaohua Li | 4a0b75c | 2011-12-16 14:00:22 +0100 | [diff] [blame] | 2215 | |
| 2216 | cfqq = RQ_CFQQ(next); |
| 2217 | /* |
| 2218 | * all requests of this queue are merged to other queues, delete it |
| 2219 | * from the service tree. If it's the active_queue, |
| 2220 | * cfq_dispatch_requests() will choose to expire it or do idle |
| 2221 | */ |
| 2222 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) && |
| 2223 | cfqq != cfqd->active_queue) |
| 2224 | cfq_del_cfqq_rr(cfqd, cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2225 | } |
| 2226 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2227 | static int cfq_allow_merge(struct request_queue *q, struct request *rq, |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2228 | struct bio *bio) |
| 2229 | { |
| 2230 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2231 | struct cfq_io_cq *cic; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2232 | struct cfq_queue *cfqq; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2233 | |
| 2234 | /* |
Jens Axboe | ec8acb6 | 2007-01-02 18:32:11 +0100 | [diff] [blame] | 2235 | * Disallow merge of a sync bio into an async request. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2236 | */ |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2237 | if (cfq_bio_sync(bio) && !rq_is_sync(rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2238 | return false; |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2239 | |
| 2240 | /* |
Tejun Heo | f1a4f4d | 2011-12-14 00:33:39 +0100 | [diff] [blame] | 2241 | * Lookup the cfqq that this bio will be queued with and allow |
Tejun Heo | 07c2bd3 | 2012-02-08 09:19:42 +0100 | [diff] [blame] | 2242 | * merge only if rq is queued there. |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2243 | */ |
Tejun Heo | 07c2bd3 | 2012-02-08 09:19:42 +0100 | [diff] [blame] | 2244 | cic = cfq_cic_lookup(cfqd, current->io_context); |
| 2245 | if (!cic) |
| 2246 | return false; |
Jens Axboe | 719d340 | 2006-12-22 09:38:53 +0100 | [diff] [blame] | 2247 | |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 2248 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 2249 | return cfqq == RQ_CFQQ(rq); |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2252 | static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2253 | { |
| 2254 | del_timer(&cfqd->idle_slice_timer); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2255 | cfqg_stats_update_idle_time(cfqq->cfqg); |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2258 | static void __cfq_set_active_queue(struct cfq_data *cfqd, |
| 2259 | struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2260 | { |
| 2261 | if (cfqq) { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2262 | cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d", |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2263 | cfqd->serving_wl_class, cfqd->serving_wl_type); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2264 | cfqg_stats_update_avg_queue_size(cfqq->cfqg); |
Justin TerAvest | 62a37f6 | 2011-03-23 08:25:44 +0100 | [diff] [blame] | 2265 | cfqq->slice_start = 0; |
| 2266 | cfqq->dispatch_start = jiffies; |
| 2267 | cfqq->allocated_slice = 0; |
| 2268 | cfqq->slice_end = 0; |
| 2269 | cfqq->slice_dispatch = 0; |
| 2270 | cfqq->nr_sectors = 0; |
| 2271 | |
| 2272 | cfq_clear_cfqq_wait_request(cfqq); |
| 2273 | cfq_clear_cfqq_must_dispatch(cfqq); |
| 2274 | cfq_clear_cfqq_must_alloc_slice(cfqq); |
| 2275 | cfq_clear_cfqq_fifo_expire(cfqq); |
| 2276 | cfq_mark_cfqq_slice_new(cfqq); |
| 2277 | |
| 2278 | cfq_del_timer(cfqd, cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | cfqd->active_queue = cfqq; |
| 2282 | } |
| 2283 | |
| 2284 | /* |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2285 | * current cfqq expired its slice (or was too idle), select new one |
| 2286 | */ |
| 2287 | static void |
| 2288 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2289 | bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2290 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2291 | cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out); |
| 2292 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2293 | if (cfq_cfqq_wait_request(cfqq)) |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 2294 | cfq_del_timer(cfqd, cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2295 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2296 | cfq_clear_cfqq_wait_request(cfqq); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 2297 | cfq_clear_cfqq_wait_busy(cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2298 | |
| 2299 | /* |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 2300 | * If this cfqq is shared between multiple processes, check to |
| 2301 | * make sure that those processes are still issuing I/Os within |
| 2302 | * the mean seek distance. If not, it may be time to break the |
| 2303 | * queues apart again. |
| 2304 | */ |
| 2305 | if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq)) |
| 2306 | cfq_mark_cfqq_split_coop(cfqq); |
| 2307 | |
| 2308 | /* |
Jens Axboe | 6084cdd | 2007-04-23 08:25:00 +0200 | [diff] [blame] | 2309 | * store what was left of this slice, if the queue idled/timed out |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2310 | */ |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 2311 | if (timed_out) { |
| 2312 | if (cfq_cfqq_slice_new(cfqq)) |
Vivek Goyal | ba5bd52 | 2011-01-19 08:25:02 -0700 | [diff] [blame] | 2313 | cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq); |
Shaohua Li | c553f8e | 2011-01-14 08:41:03 +0100 | [diff] [blame] | 2314 | else |
| 2315 | cfqq->slice_resid = cfqq->slice_end - jiffies; |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2316 | cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid); |
| 2317 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2318 | |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2319 | cfq_group_served(cfqd, cfqq->cfqg, cfqq); |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2320 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2321 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 2322 | cfq_del_cfqq_rr(cfqd, cfqq); |
| 2323 | |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2324 | cfq_resort_rr_list(cfqd, cfqq); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2325 | |
| 2326 | if (cfqq == cfqd->active_queue) |
| 2327 | cfqd->active_queue = NULL; |
| 2328 | |
| 2329 | if (cfqd->active_cic) { |
Tejun Heo | 11a3122 | 2012-02-07 07:51:30 +0100 | [diff] [blame] | 2330 | put_io_context(cfqd->active_cic->icq.ioc); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2331 | cfqd->active_cic = NULL; |
| 2332 | } |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2333 | } |
| 2334 | |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2335 | static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out) |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2336 | { |
| 2337 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 2338 | |
| 2339 | if (cfqq) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2340 | __cfq_slice_expired(cfqd, cfqq, timed_out); |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2343 | /* |
| 2344 | * Get next queue for service. Unless we have a queue preemption, |
| 2345 | * we'll simply select the first cfqq in the service tree. |
| 2346 | */ |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2347 | static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2348 | { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2349 | struct cfq_rb_root *st = st_for(cfqd->serving_group, |
| 2350 | cfqd->serving_wl_class, cfqd->serving_wl_type); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 2351 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2352 | if (!cfqd->rq_queued) |
| 2353 | return NULL; |
| 2354 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2355 | /* There is nothing to dispatch */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2356 | if (!st) |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2357 | return NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2358 | if (RB_EMPTY_ROOT(&st->rb)) |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2359 | return NULL; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2360 | return cfq_rb_first(st); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2361 | } |
| 2362 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2363 | static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd) |
| 2364 | { |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2365 | struct cfq_group *cfqg; |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2366 | struct cfq_queue *cfqq; |
| 2367 | int i, j; |
| 2368 | struct cfq_rb_root *st; |
| 2369 | |
| 2370 | if (!cfqd->rq_queued) |
| 2371 | return NULL; |
| 2372 | |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 2373 | cfqg = cfq_get_next_cfqg(cfqd); |
| 2374 | if (!cfqg) |
| 2375 | return NULL; |
| 2376 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2377 | for_each_cfqg_st(cfqg, i, j, st) |
| 2378 | if ((cfqq = cfq_rb_first(st)) != NULL) |
| 2379 | return cfqq; |
| 2380 | return NULL; |
| 2381 | } |
| 2382 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2383 | /* |
| 2384 | * Get and set a new active queue for service. |
| 2385 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2386 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, |
| 2387 | struct cfq_queue *cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2388 | { |
Jens Axboe | e00ef79 | 2009-11-04 08:54:55 +0100 | [diff] [blame] | 2389 | if (!cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2390 | cfqq = cfq_get_next_queue(cfqd); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2391 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2392 | __cfq_set_active_queue(cfqd, cfqq); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2393 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2396 | static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, |
| 2397 | struct request *rq) |
| 2398 | { |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 2399 | if (blk_rq_pos(rq) >= cfqd->last_position) |
| 2400 | return blk_rq_pos(rq) - cfqd->last_position; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2401 | else |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 2402 | return cfqd->last_position - blk_rq_pos(rq); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 2405 | static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2406 | struct request *rq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2407 | { |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2408 | return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2409 | } |
| 2410 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2411 | static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, |
| 2412 | struct cfq_queue *cur_cfqq) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2413 | { |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2414 | struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio]; |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2415 | struct rb_node *parent, *node; |
| 2416 | struct cfq_queue *__cfqq; |
| 2417 | sector_t sector = cfqd->last_position; |
| 2418 | |
| 2419 | if (RB_EMPTY_ROOT(root)) |
| 2420 | return NULL; |
| 2421 | |
| 2422 | /* |
| 2423 | * First, if we find a request starting at the end of the last |
| 2424 | * request, choose it. |
| 2425 | */ |
Jens Axboe | f2d1f0a | 2009-04-23 12:19:38 +0200 | [diff] [blame] | 2426 | __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2427 | if (__cfqq) |
| 2428 | return __cfqq; |
| 2429 | |
| 2430 | /* |
| 2431 | * If the exact sector wasn't found, the parent of the NULL leaf |
| 2432 | * will contain the closest sector. |
| 2433 | */ |
| 2434 | __cfqq = rb_entry(parent, struct cfq_queue, p_node); |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2435 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2436 | return __cfqq; |
| 2437 | |
Tejun Heo | 2e46e8b | 2009-05-07 22:24:41 +0900 | [diff] [blame] | 2438 | if (blk_rq_pos(__cfqq->next_rq) < sector) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2439 | node = rb_next(&__cfqq->p_node); |
| 2440 | else |
| 2441 | node = rb_prev(&__cfqq->p_node); |
| 2442 | if (!node) |
| 2443 | return NULL; |
| 2444 | |
| 2445 | __cfqq = rb_entry(node, struct cfq_queue, p_node); |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 2446 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2447 | return __cfqq; |
| 2448 | |
| 2449 | return NULL; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * cfqd - obvious |
| 2454 | * cur_cfqq - passed in so that we don't decide that the current queue is |
| 2455 | * closely cooperating with itself. |
| 2456 | * |
| 2457 | * So, basically we're assuming that that cur_cfqq has dispatched at least |
| 2458 | * one request, and that cfqd->last_position reflects a position on the disk |
| 2459 | * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid |
| 2460 | * assumption. |
| 2461 | */ |
| 2462 | static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 2463 | struct cfq_queue *cur_cfqq) |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2464 | { |
| 2465 | struct cfq_queue *cfqq; |
| 2466 | |
Divyesh Shah | 39c01b2 | 2010-03-25 15:45:57 +0100 | [diff] [blame] | 2467 | if (cfq_class_idle(cur_cfqq)) |
| 2468 | return NULL; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2469 | if (!cfq_cfqq_sync(cur_cfqq)) |
| 2470 | return NULL; |
| 2471 | if (CFQQ_SEEKY(cur_cfqq)) |
| 2472 | return NULL; |
| 2473 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2474 | /* |
Gui Jianfeng | b9d8f4c | 2009-12-08 08:54:17 +0100 | [diff] [blame] | 2475 | * Don't search priority tree if it's the only queue in the group. |
| 2476 | */ |
| 2477 | if (cur_cfqq->cfqg->nr_cfqq == 1) |
| 2478 | return NULL; |
| 2479 | |
| 2480 | /* |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2481 | * We should notice if some of the queues are cooperating, eg |
| 2482 | * working closely on the same area of the disk. In that case, |
| 2483 | * we can group them together and don't waste time idling. |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2484 | */ |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2485 | cfqq = cfqq_close(cfqd, cur_cfqq); |
| 2486 | if (!cfqq) |
| 2487 | return NULL; |
| 2488 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 2489 | /* If new queue belongs to different cfq_group, don't choose it */ |
| 2490 | if (cur_cfqq->cfqg != cfqq->cfqg) |
| 2491 | return NULL; |
| 2492 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2493 | /* |
| 2494 | * It only makes sense to merge sync queues. |
| 2495 | */ |
| 2496 | if (!cfq_cfqq_sync(cfqq)) |
| 2497 | return NULL; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2498 | if (CFQQ_SEEKY(cfqq)) |
| 2499 | return NULL; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2500 | |
Corrado Zoccolo | c0324a0 | 2009-10-27 19:16:03 +0100 | [diff] [blame] | 2501 | /* |
| 2502 | * Do not merge queues of different priority classes |
| 2503 | */ |
| 2504 | if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq)) |
| 2505 | return NULL; |
| 2506 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2507 | return cfqq; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2508 | } |
| 2509 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2510 | /* |
| 2511 | * Determine whether we should enforce idle window for this queue. |
| 2512 | */ |
| 2513 | |
| 2514 | static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2515 | { |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2516 | enum wl_class_t wl_class = cfqq_class(cfqq); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2517 | struct cfq_rb_root *st = cfqq->service_tree; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2518 | |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2519 | BUG_ON(!st); |
| 2520 | BUG_ON(!st->count); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2521 | |
Vivek Goyal | b6508c1 | 2010-08-23 12:23:33 +0200 | [diff] [blame] | 2522 | if (!cfqd->cfq_slice_idle) |
| 2523 | return false; |
| 2524 | |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2525 | /* We never do for idle class queues. */ |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2526 | if (wl_class == IDLE_WORKLOAD) |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2527 | return false; |
| 2528 | |
| 2529 | /* We do for queues that were marked with idle window flag. */ |
Shaohua Li | 3c764b7 | 2009-12-04 13:12:06 +0100 | [diff] [blame] | 2530 | if (cfq_cfqq_idle_window(cfqq) && |
| 2531 | !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)) |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2532 | return true; |
| 2533 | |
| 2534 | /* |
| 2535 | * Otherwise, we do only if they are the last ones |
| 2536 | * in their service tree. |
| 2537 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2538 | if (st->count == 1 && cfq_cfqq_sync(cfqq) && |
| 2539 | !cfq_io_thinktime_big(cfqd, &st->ttime, false)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 2540 | return true; |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2541 | cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count); |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 2542 | return false; |
Corrado Zoccolo | a6d44e9 | 2009-10-26 22:45:11 +0100 | [diff] [blame] | 2543 | } |
| 2544 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2545 | static void cfq_arm_slice_timer(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2546 | { |
Jens Axboe | 1792669 | 2007-01-19 11:59:30 +1100 | [diff] [blame] | 2547 | struct cfq_queue *cfqq = cfqd->active_queue; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 2548 | struct cfq_io_cq *cic; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2549 | unsigned long sl, group_idle = 0; |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2550 | |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2551 | /* |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 2552 | * SSD device without seek penalty, disable idling. But only do so |
| 2553 | * for devices that support queuing, otherwise we still have a problem |
| 2554 | * with sync vs async workloads. |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2555 | */ |
Jens Axboe | f7d7b7a | 2008-09-25 11:37:50 +0200 | [diff] [blame] | 2556 | if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag) |
Jens Axboe | a68bbddba | 2008-09-24 13:03:33 +0200 | [diff] [blame] | 2557 | return; |
| 2558 | |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 2559 | WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2560 | WARN_ON(cfq_cfqq_slice_new(cfqq)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2561 | |
| 2562 | /* |
| 2563 | * idle is disabled, either manually or by past process history |
| 2564 | */ |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2565 | if (!cfq_should_idle(cfqd, cfqq)) { |
| 2566 | /* no queue idling. Check for group idling */ |
| 2567 | if (cfqd->cfq_group_idle) |
| 2568 | group_idle = cfqd->cfq_group_idle; |
| 2569 | else |
| 2570 | return; |
| 2571 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2572 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2573 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2574 | * still active requests from this queue, don't idle |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2575 | */ |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 2576 | if (cfqq->dispatched) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2577 | return; |
| 2578 | |
| 2579 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2580 | * task has exited, don't wait |
| 2581 | */ |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2582 | cic = cfqd->active_cic; |
Tejun Heo | f6e8d01 | 2012-03-05 13:15:26 -0800 | [diff] [blame] | 2583 | if (!cic || !atomic_read(&cic->icq.ioc->active_ref)) |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2584 | return; |
| 2585 | |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2586 | /* |
| 2587 | * If our average think time is larger than the remaining time |
| 2588 | * slice, then don't idle. This avoids overrunning the allotted |
| 2589 | * time slice. |
| 2590 | */ |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 2591 | if (sample_valid(cic->ttime.ttime_samples) && |
| 2592 | (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) { |
Joe Perches | fd16d26 | 2011-06-13 10:42:49 +0200 | [diff] [blame] | 2593 | cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu", |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 2594 | cic->ttime.ttime_mean); |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2595 | return; |
Divyesh Shah | b1ffe73 | 2010-03-25 15:45:03 +0100 | [diff] [blame] | 2596 | } |
Corrado Zoccolo | 355b659 | 2009-10-08 08:43:32 +0200 | [diff] [blame] | 2597 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2598 | /* There are other queues in the group, don't do group idle */ |
| 2599 | if (group_idle && cfqq->cfqg->nr_cfqq > 1) |
| 2600 | return; |
| 2601 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2602 | cfq_mark_cfqq_wait_request(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2603 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2604 | if (group_idle) |
| 2605 | sl = cfqd->cfq_group_idle; |
| 2606 | else |
| 2607 | sl = cfqd->cfq_slice_idle; |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 2608 | |
Jens Axboe | 7b14e3b | 2006-02-28 09:35:11 +0100 | [diff] [blame] | 2609 | mod_timer(&cfqd->idle_slice_timer, jiffies + sl); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2610 | cfqg_stats_set_start_idle_time(cfqq->cfqg); |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2611 | cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl, |
| 2612 | group_idle ? 1 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2615 | /* |
| 2616 | * Move request from internal lists to the request queue dispatch list. |
| 2617 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2618 | static void cfq_dispatch_insert(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | { |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 2620 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 2621 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2622 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2623 | cfq_log_cfqq(cfqd, cfqq, "dispatch_insert"); |
| 2624 | |
Jeff Moyer | 06d2188 | 2009-09-11 17:08:59 +0200 | [diff] [blame] | 2625 | cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq); |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2626 | cfq_remove_request(rq); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2627 | cfqq->dispatched++; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2628 | (RQ_CFQG(rq))->dispatched++; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 2629 | elv_dispatch_sort(q, rq); |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 2630 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 2631 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++; |
Vivek Goyal | c4e7893 | 2010-08-23 12:25:03 +0200 | [diff] [blame] | 2632 | cfqq->nr_sectors += blk_rq_sectors(rq); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 2633 | cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* |
| 2637 | * return expired entry, or NULL to just start from scratch in rbtree |
| 2638 | */ |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2639 | static struct request *cfq_check_fifo(struct cfq_queue *cfqq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | { |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 2641 | struct request *rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2643 | if (cfq_cfqq_fifo_expire(cfqq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | return NULL; |
Jens Axboe | cb88741 | 2007-01-19 12:01:16 +1100 | [diff] [blame] | 2645 | |
| 2646 | cfq_mark_cfqq_fifo_expire(cfqq); |
| 2647 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2648 | if (list_empty(&cfqq->fifo)) |
| 2649 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 2651 | rq = rq_entry_fifo(cfqq->fifo.next); |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 2652 | if (time_before(jiffies, rq_fifo_time(rq))) |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 2653 | rq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 2655 | cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2656 | return rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2659 | static inline int |
| 2660 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 2661 | { |
| 2662 | const int base_rq = cfqd->cfq_slice_async_rq; |
| 2663 | |
| 2664 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
| 2665 | |
Namhyung Kim | b9f8ce0 | 2011-05-24 10:23:21 +0200 | [diff] [blame] | 2666 | return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2670 | * Must be called with the queue_lock held. |
| 2671 | */ |
| 2672 | static int cfqq_process_refs(struct cfq_queue *cfqq) |
| 2673 | { |
| 2674 | int process_refs, io_refs; |
| 2675 | |
| 2676 | io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE]; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 2677 | process_refs = cfqq->ref - io_refs; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2678 | BUG_ON(process_refs < 0); |
| 2679 | return process_refs; |
| 2680 | } |
| 2681 | |
| 2682 | static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq) |
| 2683 | { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2684 | int process_refs, new_process_refs; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2685 | struct cfq_queue *__cfqq; |
| 2686 | |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 2687 | /* |
| 2688 | * If there are no process references on the new_cfqq, then it is |
| 2689 | * unsafe to follow the ->new_cfqq chain as other cfqq's in the |
| 2690 | * chain may have dropped their last reference (not just their |
| 2691 | * last process reference). |
| 2692 | */ |
| 2693 | if (!cfqq_process_refs(new_cfqq)) |
| 2694 | return; |
| 2695 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2696 | /* Avoid a circular list and skip interim queue merges */ |
| 2697 | while ((__cfqq = new_cfqq->new_cfqq)) { |
| 2698 | if (__cfqq == cfqq) |
| 2699 | return; |
| 2700 | new_cfqq = __cfqq; |
| 2701 | } |
| 2702 | |
| 2703 | process_refs = cfqq_process_refs(cfqq); |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 2704 | new_process_refs = cfqq_process_refs(new_cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2705 | /* |
| 2706 | * If the process for the cfqq has gone away, there is no |
| 2707 | * sense in merging the queues. |
| 2708 | */ |
Jeff Moyer | c10b61f | 2010-06-17 10:19:11 -0400 | [diff] [blame] | 2709 | if (process_refs == 0 || new_process_refs == 0) |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2710 | return; |
| 2711 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2712 | /* |
| 2713 | * Merge in the direction of the lesser amount of work. |
| 2714 | */ |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2715 | if (new_process_refs >= process_refs) { |
| 2716 | cfqq->new_cfqq = new_cfqq; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 2717 | new_cfqq->ref += process_refs; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2718 | } else { |
| 2719 | new_cfqq->new_cfqq = cfqq; |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 2720 | cfqq->ref += new_process_refs; |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 2721 | } |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2722 | } |
| 2723 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 2724 | static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd, |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2725 | struct cfq_group *cfqg, enum wl_class_t wl_class) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2726 | { |
| 2727 | struct cfq_queue *queue; |
| 2728 | int i; |
| 2729 | bool key_valid = false; |
| 2730 | unsigned long lowest_key = 0; |
| 2731 | enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD; |
| 2732 | |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 2733 | for (i = 0; i <= SYNC_WORKLOAD; ++i) { |
| 2734 | /* select the one with lowest rb_key */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2735 | queue = cfq_rb_first(st_for(cfqg, wl_class, i)); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2736 | if (queue && |
| 2737 | (!key_valid || time_before(queue->rb_key, lowest_key))) { |
| 2738 | lowest_key = queue->rb_key; |
| 2739 | cur_best = i; |
| 2740 | key_valid = true; |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | return cur_best; |
| 2745 | } |
| 2746 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 2747 | static void |
| 2748 | choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2749 | { |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2750 | unsigned slice; |
| 2751 | unsigned count; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2752 | struct cfq_rb_root *st; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 2753 | unsigned group_slice; |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2754 | enum wl_class_t original_class = cfqd->serving_wl_class; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2755 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2756 | /* Choose next priority. RT > BE > IDLE */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 2757 | if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2758 | cfqd->serving_wl_class = RT_WORKLOAD; |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 2759 | else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2760 | cfqd->serving_wl_class = BE_WORKLOAD; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2761 | else { |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2762 | cfqd->serving_wl_class = IDLE_WORKLOAD; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2763 | cfqd->workload_expires = jiffies + 1; |
| 2764 | return; |
| 2765 | } |
| 2766 | |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2767 | if (original_class != cfqd->serving_wl_class) |
Shaohua Li writes | e4ea0c1 | 2010-12-13 14:32:22 +0100 | [diff] [blame] | 2768 | goto new_workload; |
| 2769 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2770 | /* |
| 2771 | * For RT and BE, we have to choose also the type |
| 2772 | * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload |
| 2773 | * expiration time |
| 2774 | */ |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2775 | st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2776 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2777 | |
| 2778 | /* |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 2779 | * check workload expiration, and that we still have other queues ready |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2780 | */ |
Vivek Goyal | 65b32a5 | 2009-12-16 17:52:59 -0500 | [diff] [blame] | 2781 | if (count && !time_after(jiffies, cfqd->workload_expires)) |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2782 | return; |
| 2783 | |
Shaohua Li writes | e4ea0c1 | 2010-12-13 14:32:22 +0100 | [diff] [blame] | 2784 | new_workload: |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2785 | /* otherwise select new workload type */ |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 2786 | cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg, |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2787 | cfqd->serving_wl_class); |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 2788 | st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2789 | count = st->count; |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* |
| 2792 | * the workload slice is computed as a fraction of target latency |
| 2793 | * proportional to the number of queues in that workload, over |
| 2794 | * all the queues in the same priority class |
| 2795 | */ |
Vivek Goyal | 58ff82f | 2009-12-03 12:59:44 -0500 | [diff] [blame] | 2796 | group_slice = cfq_group_slice(cfqd, cfqg); |
| 2797 | |
| 2798 | slice = group_slice * count / |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2799 | max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class], |
| 2800 | cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd, |
Vivek Goyal | 3bf10fe | 2012-10-03 16:56:56 -0400 | [diff] [blame] | 2801 | cfqg)); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2802 | |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2803 | if (cfqd->serving_wl_type == ASYNC_WORKLOAD) { |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 2804 | unsigned int tmp; |
| 2805 | |
| 2806 | /* |
| 2807 | * Async queues are currently system wide. Just taking |
| 2808 | * proportion of queues with-in same group will lead to higher |
| 2809 | * async ratio system wide as generally root group is going |
| 2810 | * to have higher weight. A more accurate thing would be to |
| 2811 | * calculate system wide asnc/sync ratio. |
| 2812 | */ |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 2813 | tmp = cfqd->cfq_target_latency * |
| 2814 | cfqg_busy_async_queues(cfqd, cfqg); |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 2815 | tmp = tmp/cfqd->busy_queues; |
| 2816 | slice = min_t(unsigned, slice, tmp); |
| 2817 | |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2818 | /* async workload slice is scaled down according to |
| 2819 | * the sync/async slice ratio. */ |
| 2820 | slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1]; |
Vivek Goyal | f26bd1f | 2009-12-03 12:59:54 -0500 | [diff] [blame] | 2821 | } else |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2822 | /* sync workload slice is at least 2 * cfq_slice_idle */ |
| 2823 | slice = max(slice, 2 * cfqd->cfq_slice_idle); |
| 2824 | |
| 2825 | slice = max_t(unsigned, slice, CFQ_MIN_TT); |
Divyesh Shah | b1ffe73 | 2010-03-25 15:45:03 +0100 | [diff] [blame] | 2826 | cfq_log(cfqd, "workload slice:%d", slice); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2827 | cfqd->workload_expires = jiffies + slice; |
| 2828 | } |
| 2829 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2830 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd) |
| 2831 | { |
| 2832 | struct cfq_rb_root *st = &cfqd->grp_service_tree; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 2833 | struct cfq_group *cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2834 | |
| 2835 | if (RB_EMPTY_ROOT(&st->rb)) |
| 2836 | return NULL; |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 2837 | cfqg = cfq_rb_first_group(st); |
Vivek Goyal | 25bc6b0 | 2009-12-03 12:59:43 -0500 | [diff] [blame] | 2838 | update_min_vdisktime(st); |
| 2839 | return cfqg; |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2840 | } |
| 2841 | |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2842 | static void cfq_choose_cfqg(struct cfq_data *cfqd) |
| 2843 | { |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 2844 | struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd); |
| 2845 | |
| 2846 | cfqd->serving_group = cfqg; |
Vivek Goyal | dae739e | 2009-12-03 12:59:45 -0500 | [diff] [blame] | 2847 | |
| 2848 | /* Restore the workload type data */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 2849 | if (cfqg->saved_wl_slice) { |
| 2850 | cfqd->workload_expires = jiffies + cfqg->saved_wl_slice; |
| 2851 | cfqd->serving_wl_type = cfqg->saved_wl_type; |
| 2852 | cfqd->serving_wl_class = cfqg->saved_wl_class; |
Gui Jianfeng | 66ae291 | 2009-12-15 10:08:45 +0100 | [diff] [blame] | 2853 | } else |
| 2854 | cfqd->workload_expires = jiffies - 1; |
| 2855 | |
Vivek Goyal | 6d816ec | 2012-10-03 16:56:59 -0400 | [diff] [blame] | 2856 | choose_wl_class_and_type(cfqd, cfqg); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2857 | } |
| 2858 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2859 | /* |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2860 | * Select a queue for service. If we have a current active queue, |
| 2861 | * check whether to continue servicing it, or retrieve and set a new one. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2862 | */ |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2863 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2864 | { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2865 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2866 | |
| 2867 | cfqq = cfqd->active_queue; |
| 2868 | if (!cfqq) |
| 2869 | goto new_queue; |
| 2870 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2871 | if (!cfqd->rq_queued) |
| 2872 | return NULL; |
Vivek Goyal | c244bb5 | 2009-12-08 17:52:57 -0500 | [diff] [blame] | 2873 | |
| 2874 | /* |
| 2875 | * We were waiting for group to get backlogged. Expire the queue |
| 2876 | */ |
| 2877 | if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 2878 | goto expire; |
| 2879 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2880 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2881 | * The active queue has run out of time, expire it and select new. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2882 | */ |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 2883 | if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) { |
| 2884 | /* |
| 2885 | * If slice had not expired at the completion of last request |
| 2886 | * we might not have turned on wait_busy flag. Don't expire |
| 2887 | * the queue yet. Allow the group to get backlogged. |
| 2888 | * |
| 2889 | * The very fact that we have used the slice, that means we |
| 2890 | * have been idling all along on this queue and it should be |
| 2891 | * ok to wait for this request to complete. |
| 2892 | */ |
Vivek Goyal | 82bbbf2 | 2009-12-10 19:25:41 +0100 | [diff] [blame] | 2893 | if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list) |
| 2894 | && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { |
| 2895 | cfqq = NULL; |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 2896 | goto keep_queue; |
Vivek Goyal | 82bbbf2 | 2009-12-10 19:25:41 +0100 | [diff] [blame] | 2897 | } else |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2898 | goto check_group_idle; |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 2899 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2900 | |
| 2901 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2902 | * The active queue has requests and isn't expired, allow it to |
| 2903 | * dispatch. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2904 | */ |
Jens Axboe | dd67d05 | 2006-06-21 09:36:18 +0200 | [diff] [blame] | 2905 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2906 | goto keep_queue; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2907 | |
| 2908 | /* |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2909 | * If another queue has a request waiting within our mean seek |
| 2910 | * distance, let it run. The expire code will check for close |
| 2911 | * cooperators and put the close queue at the front of the service |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2912 | * tree. If possible, merge the expiring queue with the new cfqq. |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2913 | */ |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 2914 | new_cfqq = cfq_close_cooperator(cfqd, cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2915 | if (new_cfqq) { |
| 2916 | if (!cfqq->new_cfqq) |
| 2917 | cfq_setup_merge(cfqq, new_cfqq); |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2918 | goto expire; |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 2919 | } |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2920 | |
| 2921 | /* |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 2922 | * No requests pending. If the active queue still has requests in |
| 2923 | * flight or is idling for a new request, allow either of these |
| 2924 | * conditions to happen (or time out) before selecting a new queue. |
| 2925 | */ |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2926 | if (timer_pending(&cfqd->idle_slice_timer)) { |
| 2927 | cfqq = NULL; |
| 2928 | goto keep_queue; |
| 2929 | } |
| 2930 | |
Shaohua Li | 8e1ac66 | 2010-11-08 15:01:04 +0100 | [diff] [blame] | 2931 | /* |
| 2932 | * This is a deep seek queue, but the device is much faster than |
| 2933 | * the queue can deliver, don't idle |
| 2934 | **/ |
| 2935 | if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) && |
| 2936 | (cfq_cfqq_slice_new(cfqq) || |
| 2937 | (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) { |
| 2938 | cfq_clear_cfqq_deep(cfqq); |
| 2939 | cfq_clear_cfqq_idle_window(cfqq); |
| 2940 | } |
| 2941 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 2942 | if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { |
| 2943 | cfqq = NULL; |
| 2944 | goto keep_queue; |
| 2945 | } |
| 2946 | |
| 2947 | /* |
| 2948 | * If group idle is enabled and there are requests dispatched from |
| 2949 | * this group, wait for requests to complete. |
| 2950 | */ |
| 2951 | check_group_idle: |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 2952 | if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 && |
| 2953 | cfqq->cfqg->dispatched && |
| 2954 | !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) { |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 2955 | cfqq = NULL; |
| 2956 | goto keep_queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2957 | } |
| 2958 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2959 | expire: |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2960 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2961 | new_queue: |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2962 | /* |
| 2963 | * Current queue expired. Check if we have to switch to a new |
| 2964 | * service tree |
| 2965 | */ |
| 2966 | if (!new_cfqq) |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2967 | cfq_choose_cfqg(cfqd); |
Corrado Zoccolo | 718eee0 | 2009-10-26 22:45:29 +0100 | [diff] [blame] | 2968 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 2969 | cfqq = cfq_set_active_queue(cfqd, new_cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2970 | keep_queue: |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 2971 | return cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Jens Axboe | febffd6 | 2008-01-28 13:19:43 +0100 | [diff] [blame] | 2974 | static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2975 | { |
| 2976 | int dispatched = 0; |
| 2977 | |
| 2978 | while (cfqq->next_rq) { |
| 2979 | cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); |
| 2980 | dispatched++; |
| 2981 | } |
| 2982 | |
| 2983 | BUG_ON(!list_empty(&cfqq->fifo)); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 2984 | |
| 2985 | /* By default cfqq is not expired if it is empty. Do it explicitly */ |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 2986 | __cfq_slice_expired(cfqq->cfqd, cfqq, 0); |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2987 | return dispatched; |
| 2988 | } |
| 2989 | |
Jens Axboe | 498d3aa2 | 2007-04-26 12:54:48 +0200 | [diff] [blame] | 2990 | /* |
| 2991 | * Drain our current requests. Used for barriers and when switching |
| 2992 | * io schedulers on-the-fly. |
| 2993 | */ |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2994 | static int cfq_forced_dispatch(struct cfq_data *cfqd) |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 2995 | { |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 2996 | struct cfq_queue *cfqq; |
Jens Axboe | d9e7620 | 2007-04-20 14:27:50 +0200 | [diff] [blame] | 2997 | int dispatched = 0; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 2998 | |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 2999 | /* Expire the timeslice of the current active queue first */ |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3000 | cfq_slice_expired(cfqd, 0); |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 3001 | while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) { |
| 3002 | __cfq_set_active_queue(cfqd, cfqq); |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3003 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); |
Divyesh Shah | 3440c49 | 2010-04-09 09:29:57 +0200 | [diff] [blame] | 3004 | } |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3005 | |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3006 | BUG_ON(cfqd->busy_queues); |
| 3007 | |
Jeff Moyer | 6923715a | 2009-06-12 15:29:30 +0200 | [diff] [blame] | 3008 | cfq_log(cfqd, "forced_dispatch=%d", dispatched); |
Tejun Heo | 1b5ed5e1 | 2005-11-10 08:49:19 +0100 | [diff] [blame] | 3009 | return dispatched; |
| 3010 | } |
| 3011 | |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3012 | static inline bool cfq_slice_used_soon(struct cfq_data *cfqd, |
| 3013 | struct cfq_queue *cfqq) |
| 3014 | { |
| 3015 | /* the queue hasn't finished any request, can't estimate */ |
| 3016 | if (cfq_cfqq_slice_new(cfqq)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3017 | return true; |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3018 | if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched, |
| 3019 | cfqq->slice_end)) |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3020 | return true; |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3021 | |
Shaohua Li | c1e4475 | 2010-11-08 15:01:02 +0100 | [diff] [blame] | 3022 | return false; |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3023 | } |
| 3024 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3025 | static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3026 | { |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3027 | unsigned int max_dispatch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3029 | /* |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3030 | * Drain async requests before we start sync IO |
| 3031 | */ |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3032 | if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC]) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3033 | return false; |
Jens Axboe | 5ad531d | 2009-07-03 12:57:48 +0200 | [diff] [blame] | 3034 | |
| 3035 | /* |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3036 | * If this is an async queue and we have sync IO in flight, let it wait |
| 3037 | */ |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3038 | if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3039 | return false; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3040 | |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3041 | max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3042 | if (cfq_class_idle(cfqq)) |
| 3043 | max_dispatch = 1; |
| 3044 | |
| 3045 | /* |
| 3046 | * Does this cfqq already have too much IO in flight? |
| 3047 | */ |
| 3048 | if (cfqq->dispatched >= max_dispatch) { |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3049 | bool promote_sync = false; |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3050 | /* |
| 3051 | * idle queue must always only have a single IO in flight |
| 3052 | */ |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3053 | if (cfq_class_idle(cfqq)) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3054 | return false; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3055 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3056 | /* |
Li, Shaohua | c4ade94 | 2011-03-23 08:30:34 +0100 | [diff] [blame] | 3057 | * If there is only one sync queue |
| 3058 | * we can ignore async queue here and give the sync |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3059 | * queue no dispatch limit. The reason is a sync queue can |
| 3060 | * preempt async queue, limiting the sync queue doesn't make |
| 3061 | * sense. This is useful for aiostress test. |
| 3062 | */ |
Li, Shaohua | c4ade94 | 2011-03-23 08:30:34 +0100 | [diff] [blame] | 3063 | if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1) |
| 3064 | promote_sync = true; |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3065 | |
| 3066 | /* |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3067 | * We have other queues, don't allow more IO from this one |
| 3068 | */ |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3069 | if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) && |
| 3070 | !promote_sync) |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3071 | return false; |
Jens Axboe | 9ede209 | 2007-01-19 12:11:44 +1100 | [diff] [blame] | 3072 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3073 | /* |
Shaohua Li | 474b18c | 2009-12-03 12:58:05 +0100 | [diff] [blame] | 3074 | * Sole queue user, no limit |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3075 | */ |
Shaohua Li | ef8a41d | 2011-03-07 09:26:29 +0100 | [diff] [blame] | 3076 | if (cfqd->busy_queues == 1 || promote_sync) |
Shaohua Li | abc3c74 | 2010-03-01 09:20:54 +0100 | [diff] [blame] | 3077 | max_dispatch = -1; |
| 3078 | else |
| 3079 | /* |
| 3080 | * Normally we start throttling cfqq when cfq_quantum/2 |
| 3081 | * requests have been dispatched. But we can drive |
| 3082 | * deeper queue depths at the beginning of slice |
| 3083 | * subjected to upper limit of cfq_quantum. |
| 3084 | * */ |
| 3085 | max_dispatch = cfqd->cfq_quantum; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3086 | } |
| 3087 | |
| 3088 | /* |
| 3089 | * Async queues must wait a bit before being allowed dispatch. |
| 3090 | * We also ramp up the dispatch depth gradually for async IO, |
| 3091 | * based on the last sync IO we serviced |
| 3092 | */ |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 3093 | if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { |
Corrado Zoccolo | 573412b | 2009-12-06 11:48:52 +0100 | [diff] [blame] | 3094 | unsigned long last_sync = jiffies - cfqd->last_delayed_sync; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3095 | unsigned int depth; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3096 | |
Jens Axboe | 61f0c1d | 2009-10-03 19:46:03 +0200 | [diff] [blame] | 3097 | depth = last_sync / cfqd->cfq_slice[1]; |
Jens Axboe | e00c54c | 2009-10-04 20:36:19 +0200 | [diff] [blame] | 3098 | if (!depth && !cfqq->dispatched) |
| 3099 | depth = 1; |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3100 | if (depth < max_dispatch) |
| 3101 | max_dispatch = depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3104 | /* |
| 3105 | * If we're below the current max, allow a dispatch |
| 3106 | */ |
| 3107 | return cfqq->dispatched < max_dispatch; |
| 3108 | } |
| 3109 | |
| 3110 | /* |
| 3111 | * Dispatch a request from cfqq, moving them to the request queue |
| 3112 | * dispatch list. |
| 3113 | */ |
| 3114 | static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3115 | { |
| 3116 | struct request *rq; |
| 3117 | |
| 3118 | BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); |
| 3119 | |
| 3120 | if (!cfq_may_dispatch(cfqd, cfqq)) |
| 3121 | return false; |
| 3122 | |
| 3123 | /* |
| 3124 | * follow expired path, else get first next available |
| 3125 | */ |
| 3126 | rq = cfq_check_fifo(cfqq); |
| 3127 | if (!rq) |
| 3128 | rq = cfqq->next_rq; |
| 3129 | |
| 3130 | /* |
| 3131 | * insert request into driver dispatch list |
| 3132 | */ |
| 3133 | cfq_dispatch_insert(cfqd->queue, rq); |
| 3134 | |
| 3135 | if (!cfqd->active_cic) { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3136 | struct cfq_io_cq *cic = RQ_CIC(rq); |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3137 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3138 | atomic_long_inc(&cic->icq.ioc->refcount); |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3139 | cfqd->active_cic = cic; |
| 3140 | } |
| 3141 | |
| 3142 | return true; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
| 3146 | * Find the cfqq that we need to service and move a request from that to the |
| 3147 | * dispatch list |
| 3148 | */ |
| 3149 | static int cfq_dispatch_requests(struct request_queue *q, int force) |
| 3150 | { |
| 3151 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 3152 | struct cfq_queue *cfqq; |
| 3153 | |
| 3154 | if (!cfqd->busy_queues) |
| 3155 | return 0; |
| 3156 | |
| 3157 | if (unlikely(force)) |
| 3158 | return cfq_forced_dispatch(cfqd); |
| 3159 | |
| 3160 | cfqq = cfq_select_queue(cfqd); |
| 3161 | if (!cfqq) |
Jens Axboe | 8e29675 | 2009-10-03 16:26:03 +0200 | [diff] [blame] | 3162 | return 0; |
| 3163 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3164 | /* |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3165 | * Dispatch a request from this cfqq, if it is allowed |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3166 | */ |
Jens Axboe | 0b182d6 | 2009-10-06 20:49:37 +0200 | [diff] [blame] | 3167 | if (!cfq_dispatch_request(cfqd, cfqq)) |
| 3168 | return 0; |
| 3169 | |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3170 | cfqq->slice_dispatch++; |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 3171 | cfq_clear_cfqq_must_dispatch(cfqq); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3172 | |
| 3173 | /* |
| 3174 | * expire an async queue immediately if it has used up its slice. idle |
| 3175 | * queue always expire after 1 dispatch round. |
| 3176 | */ |
| 3177 | if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && |
| 3178 | cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) || |
| 3179 | cfq_class_idle(cfqq))) { |
| 3180 | cfqq->slice_end = jiffies + 1; |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3181 | cfq_slice_expired(cfqd, 0); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3182 | } |
| 3183 | |
Shan Wei | b217a90 | 2009-09-01 10:06:42 +0200 | [diff] [blame] | 3184 | cfq_log_cfqq(cfqd, cfqq, "dispatched a request"); |
Jens Axboe | 2f5cb73 | 2009-04-07 08:51:19 +0200 | [diff] [blame] | 3185 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3189 | * task holds one reference to the queue, dropped when task exits. each rq |
| 3190 | * in-flight on this queue also holds a reference, dropped when rq is freed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | * |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3192 | * Each cfq queue took a reference on the parent group. Drop it now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | * queue lock must be held here. |
| 3194 | */ |
| 3195 | static void cfq_put_queue(struct cfq_queue *cfqq) |
| 3196 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3197 | struct cfq_data *cfqd = cfqq->cfqd; |
Justin TerAvest | 0bbfeb8 | 2011-03-01 15:05:08 -0500 | [diff] [blame] | 3198 | struct cfq_group *cfqg; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3199 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3200 | BUG_ON(cfqq->ref <= 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3202 | cfqq->ref--; |
| 3203 | if (cfqq->ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | return; |
| 3205 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3206 | cfq_log_cfqq(cfqd, cfqq, "put_queue"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | BUG_ON(rb_first(&cfqq->sort_list)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3208 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
Vivek Goyal | b1c3576 | 2009-12-03 12:59:47 -0500 | [diff] [blame] | 3209 | cfqg = cfqq->cfqg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 3211 | if (unlikely(cfqd->active_queue == cfqq)) { |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3212 | __cfq_slice_expired(cfqd, cfqq, 0); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3213 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 28f95cbc | 2007-01-19 12:09:53 +1100 | [diff] [blame] | 3214 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3215 | |
Vivek Goyal | f04a642 | 2009-12-03 12:59:40 -0500 | [diff] [blame] | 3216 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | kmem_cache_free(cfq_pool, cfqq); |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 3218 | cfqg_put(cfqg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 3221 | static void cfq_put_cooperator(struct cfq_queue *cfqq) |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3222 | { |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3223 | struct cfq_queue *__cfqq, *next; |
| 3224 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3225 | /* |
| 3226 | * If this queue was scheduled to merge with another queue, be |
| 3227 | * sure to drop the reference taken on that queue (and others in |
| 3228 | * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs. |
| 3229 | */ |
| 3230 | __cfqq = cfqq->new_cfqq; |
| 3231 | while (__cfqq) { |
| 3232 | if (__cfqq == cfqq) { |
| 3233 | WARN(1, "cfqq->new_cfqq loop detected\n"); |
| 3234 | break; |
| 3235 | } |
| 3236 | next = __cfqq->new_cfqq; |
| 3237 | cfq_put_queue(__cfqq); |
| 3238 | __cfqq = next; |
| 3239 | } |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3243 | { |
| 3244 | if (unlikely(cfqq == cfqd->active_queue)) { |
| 3245 | __cfq_slice_expired(cfqd, cfqq, 0); |
| 3246 | cfq_schedule_dispatch(cfqd); |
| 3247 | } |
| 3248 | |
| 3249 | cfq_put_cooperator(cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3250 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3251 | cfq_put_queue(cfqq); |
| 3252 | } |
| 3253 | |
Tejun Heo | 9b84cac | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 3254 | static void cfq_init_icq(struct io_cq *icq) |
| 3255 | { |
| 3256 | struct cfq_io_cq *cic = icq_to_cic(icq); |
| 3257 | |
| 3258 | cic->ttime.last_end_request = jiffies; |
| 3259 | } |
| 3260 | |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3261 | static void cfq_exit_icq(struct io_cq *icq) |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3262 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3263 | struct cfq_io_cq *cic = icq_to_cic(icq); |
Tejun Heo | 283287a | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 3264 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Fabio Checconi | 4faa3c8 | 2008-04-10 08:28:01 +0200 | [diff] [blame] | 3265 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 3266 | if (cic->cfqq[BLK_RW_ASYNC]) { |
| 3267 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]); |
| 3268 | cic->cfqq[BLK_RW_ASYNC] = NULL; |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 3271 | if (cic->cfqq[BLK_RW_SYNC]) { |
| 3272 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]); |
| 3273 | cic->cfqq[BLK_RW_SYNC] = NULL; |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3274 | } |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3275 | } |
| 3276 | |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3277 | static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3278 | { |
| 3279 | struct task_struct *tsk = current; |
| 3280 | int ioprio_class; |
| 3281 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3282 | if (!cfq_cfqq_prio_changed(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3283 | return; |
| 3284 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3285 | ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3286 | switch (ioprio_class) { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3287 | default: |
| 3288 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); |
| 3289 | case IOPRIO_CLASS_NONE: |
| 3290 | /* |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 3291 | * no prio set, inherit CPU scheduling settings |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3292 | */ |
| 3293 | cfqq->ioprio = task_nice_ioprio(tsk); |
Jens Axboe | 6d63c27 | 2008-05-07 09:51:23 +0200 | [diff] [blame] | 3294 | cfqq->ioprio_class = task_nice_ioclass(tsk); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3295 | break; |
| 3296 | case IOPRIO_CLASS_RT: |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3297 | cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3298 | cfqq->ioprio_class = IOPRIO_CLASS_RT; |
| 3299 | break; |
| 3300 | case IOPRIO_CLASS_BE: |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3301 | cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3302 | cfqq->ioprio_class = IOPRIO_CLASS_BE; |
| 3303 | break; |
| 3304 | case IOPRIO_CLASS_IDLE: |
| 3305 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; |
| 3306 | cfqq->ioprio = 7; |
| 3307 | cfq_clear_cfqq_idle_window(cfqq); |
| 3308 | break; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | /* |
| 3312 | * keep track of original prio settings in case we have to temporarily |
| 3313 | * elevate the priority of this queue |
| 3314 | */ |
| 3315 | cfqq->org_ioprio = cfqq->ioprio; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3316 | cfq_clear_cfqq_prio_changed(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3319 | static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3320 | { |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3321 | int ioprio = cic->icq.ioc->ioprio; |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 3322 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Al Viro | 478a82b | 2006-03-18 13:25:24 -0500 | [diff] [blame] | 3323 | struct cfq_queue *cfqq; |
Jens Axboe | 35e6077 | 2006-06-14 09:10:45 +0200 | [diff] [blame] | 3324 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3325 | /* |
| 3326 | * Check whether ioprio has changed. The condition may trigger |
| 3327 | * spuriously on a newly created cic but there's no harm. |
| 3328 | */ |
| 3329 | if (unlikely(!cfqd) || likely(cic->ioprio == ioprio)) |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3330 | return; |
| 3331 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 3332 | cfqq = cic->cfqq[BLK_RW_ASYNC]; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3333 | if (cfqq) { |
| 3334 | struct cfq_queue *new_cfqq; |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3335 | new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio, |
| 3336 | GFP_ATOMIC); |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3337 | if (new_cfqq) { |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 3338 | cic->cfqq[BLK_RW_ASYNC] = new_cfqq; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3339 | cfq_put_queue(cfqq); |
| 3340 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3341 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3342 | |
Jens Axboe | ff6657c | 2009-04-08 10:58:57 +0200 | [diff] [blame] | 3343 | cfqq = cic->cfqq[BLK_RW_SYNC]; |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3344 | if (cfqq) |
| 3345 | cfq_mark_cfqq_prio_changed(cfqq); |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3346 | |
| 3347 | cic->ioprio = ioprio; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3350 | static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3351 | pid_t pid, bool is_sync) |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3352 | { |
| 3353 | RB_CLEAR_NODE(&cfqq->rb_node); |
| 3354 | RB_CLEAR_NODE(&cfqq->p_node); |
| 3355 | INIT_LIST_HEAD(&cfqq->fifo); |
| 3356 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3357 | cfqq->ref = 0; |
Jens Axboe | d5036d7 | 2009-06-26 10:44:34 +0200 | [diff] [blame] | 3358 | cfqq->cfqd = cfqd; |
| 3359 | |
| 3360 | cfq_mark_cfqq_prio_changed(cfqq); |
| 3361 | |
| 3362 | if (is_sync) { |
| 3363 | if (!cfq_class_idle(cfqq)) |
| 3364 | cfq_mark_cfqq_idle_window(cfqq); |
| 3365 | cfq_mark_cfqq_sync(cfqq); |
| 3366 | } |
| 3367 | cfqq->pid = pid; |
| 3368 | } |
| 3369 | |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3370 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3371 | static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3372 | { |
Konstantin Khlebnikov | bca4b91 | 2010-05-20 23:21:34 +0400 | [diff] [blame] | 3373 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3374 | struct cfq_queue *sync_cfqq; |
| 3375 | uint64_t id; |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3376 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3377 | rcu_read_lock(); |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 3378 | id = bio_blkcg(bio)->id; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3379 | rcu_read_unlock(); |
| 3380 | |
| 3381 | /* |
| 3382 | * Check whether blkcg has changed. The condition may trigger |
| 3383 | * spuriously on a newly created cic but there's no harm. |
| 3384 | */ |
| 3385 | if (unlikely(!cfqd) || likely(cic->blkcg_id == id)) |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3386 | return; |
| 3387 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3388 | sync_cfqq = cic_to_cfqq(cic, 1); |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3389 | if (sync_cfqq) { |
| 3390 | /* |
| 3391 | * Drop reference to sync queue. A new sync queue will be |
| 3392 | * assigned in new group upon arrival of a fresh request. |
| 3393 | */ |
| 3394 | cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup"); |
| 3395 | cic_set_cfqq(cic, NULL, 1); |
| 3396 | cfq_put_queue(sync_cfqq); |
| 3397 | } |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3398 | |
| 3399 | cic->blkcg_id = id; |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3400 | } |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3401 | #else |
| 3402 | static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { } |
Vivek Goyal | 24610333 | 2009-12-03 12:59:51 -0500 | [diff] [blame] | 3403 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
| 3404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | static struct cfq_queue * |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3406 | cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic, |
| 3407 | struct bio *bio, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | { |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 3409 | struct blkcg *blkcg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3411 | struct cfq_group *cfqg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3412 | |
| 3413 | retry: |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 3414 | rcu_read_lock(); |
| 3415 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 3416 | blkcg = bio_blkcg(bio); |
Tejun Heo | cd1604f | 2012-03-05 13:15:06 -0800 | [diff] [blame] | 3417 | cfqg = cfq_lookup_create_cfqg(cfqd, blkcg); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3418 | cfqq = cic_to_cfqq(cic, is_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3420 | /* |
| 3421 | * Always try a new alloc if we fell back to the OOM cfqq |
| 3422 | * originally, since it should just be a temporary situation. |
| 3423 | */ |
| 3424 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
| 3425 | cfqq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | if (new_cfqq) { |
| 3427 | cfqq = new_cfqq; |
| 3428 | new_cfqq = NULL; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3429 | } else if (gfp_mask & __GFP_WAIT) { |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 3430 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | spin_unlock_irq(cfqd->queue->queue_lock); |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 3432 | new_cfqq = kmem_cache_alloc_node(cfq_pool, |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3433 | gfp_mask | __GFP_ZERO, |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 3434 | cfqd->queue->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | spin_lock_irq(cfqd->queue->queue_lock); |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3436 | if (new_cfqq) |
| 3437 | goto retry; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3438 | } else { |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 3439 | cfqq = kmem_cache_alloc_node(cfq_pool, |
| 3440 | gfp_mask | __GFP_ZERO, |
| 3441 | cfqd->queue->node); |
Kiyoshi Ueda | db3b584 | 2005-06-17 16:15:10 +0200 | [diff] [blame] | 3442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3444 | if (cfqq) { |
| 3445 | cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3446 | cfq_init_prio_data(cfqq, cic); |
Vivek Goyal | cdb16e8 | 2009-12-03 12:59:38 -0500 | [diff] [blame] | 3447 | cfq_link_cfqq_cfqg(cfqq, cfqg); |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3448 | cfq_log_cfqq(cfqd, cfqq, "alloced"); |
| 3449 | } else |
| 3450 | cfqq = &cfqd->oom_cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | } |
| 3452 | |
| 3453 | if (new_cfqq) |
| 3454 | kmem_cache_free(cfq_pool, new_cfqq); |
| 3455 | |
Tejun Heo | 2a7f124 | 2012-03-05 13:15:01 -0800 | [diff] [blame] | 3456 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | return cfqq; |
| 3458 | } |
| 3459 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3460 | static struct cfq_queue ** |
| 3461 | cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio) |
| 3462 | { |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 3463 | switch (ioprio_class) { |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3464 | case IOPRIO_CLASS_RT: |
| 3465 | return &cfqd->async_cfqq[0][ioprio]; |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3466 | case IOPRIO_CLASS_NONE: |
| 3467 | ioprio = IOPRIO_NORM; |
| 3468 | /* fall through */ |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3469 | case IOPRIO_CLASS_BE: |
| 3470 | return &cfqd->async_cfqq[1][ioprio]; |
| 3471 | case IOPRIO_CLASS_IDLE: |
| 3472 | return &cfqd->async_idle_cfqq; |
| 3473 | default: |
| 3474 | BUG(); |
| 3475 | } |
| 3476 | } |
| 3477 | |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3478 | static struct cfq_queue * |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3479 | cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic, |
Tejun Heo | 4f85cb9 | 2012-03-05 13:15:28 -0800 | [diff] [blame] | 3480 | struct bio *bio, gfp_t gfp_mask) |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3481 | { |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 3482 | const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio); |
| 3483 | const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3484 | struct cfq_queue **async_cfqq = NULL; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3485 | struct cfq_queue *cfqq = NULL; |
| 3486 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3487 | if (!is_sync) { |
| 3488 | async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio); |
| 3489 | cfqq = *async_cfqq; |
| 3490 | } |
| 3491 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 3492 | if (!cfqq) |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3493 | cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask); |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3494 | |
| 3495 | /* |
| 3496 | * pin the queue now that it's allocated, scheduler exit will prune it |
| 3497 | */ |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3498 | if (!is_sync && !(*async_cfqq)) { |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3499 | cfqq->ref++; |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 3500 | *async_cfqq = cfqq; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3501 | } |
| 3502 | |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 3503 | cfqq->ref++; |
Jens Axboe | 15c31be | 2007-07-10 13:43:25 +0200 | [diff] [blame] | 3504 | return cfqq; |
| 3505 | } |
| 3506 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3507 | static void |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3508 | __cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3509 | { |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3510 | unsigned long elapsed = jiffies - ttime->last_end_request; |
| 3511 | elapsed = min(elapsed, 2UL * slice_idle); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3512 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3513 | ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; |
| 3514 | ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8; |
| 3515 | ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples; |
| 3516 | } |
| 3517 | |
| 3518 | static void |
| 3519 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3520 | struct cfq_io_cq *cic) |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3521 | { |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3522 | if (cfq_cfqq_sync(cfqq)) { |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3523 | __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle); |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3524 | __cfq_update_io_thinktime(&cfqq->service_tree->ttime, |
| 3525 | cfqd->cfq_slice_idle); |
| 3526 | } |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 3527 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 3528 | __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle); |
| 3529 | #endif |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3532 | static void |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3533 | cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3534 | struct request *rq) |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3535 | { |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3536 | sector_t sdist = 0; |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 3537 | sector_t n_sec = blk_rq_sectors(rq); |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3538 | if (cfqq->last_request_pos) { |
| 3539 | if (cfqq->last_request_pos < blk_rq_pos(rq)) |
| 3540 | sdist = blk_rq_pos(rq) - cfqq->last_request_pos; |
| 3541 | else |
| 3542 | sdist = cfqq->last_request_pos - blk_rq_pos(rq); |
| 3543 | } |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3544 | |
Corrado Zoccolo | 3dde36d | 2010-02-27 19:45:39 +0100 | [diff] [blame] | 3545 | cfqq->seek_history <<= 1; |
Corrado Zoccolo | 41647e7 | 2010-02-27 19:45:40 +0100 | [diff] [blame] | 3546 | if (blk_queue_nonrot(cfqd->queue)) |
| 3547 | cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT); |
| 3548 | else |
| 3549 | cfqq->seek_history |= (sdist > CFQQ_SEEK_THR); |
Jens Axboe | 206dc69 | 2006-03-28 13:03:44 +0200 | [diff] [blame] | 3550 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3551 | |
| 3552 | /* |
| 3553 | * Disable idle window if the process thinks too long or seeks so much that |
| 3554 | * it doesn't matter |
| 3555 | */ |
| 3556 | static void |
| 3557 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3558 | struct cfq_io_cq *cic) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3559 | { |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3560 | int old_idle, enable_idle; |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 3561 | |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 3562 | /* |
| 3563 | * Don't idle for async or idle io prio class |
| 3564 | */ |
| 3565 | if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq)) |
Jens Axboe | 1be92f2f | 2007-04-19 14:32:26 +0200 | [diff] [blame] | 3566 | return; |
| 3567 | |
Jens Axboe | c265a7f | 2008-06-26 13:49:33 +0200 | [diff] [blame] | 3568 | enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3569 | |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3570 | if (cfqq->queued[0] + cfqq->queued[1] >= 4) |
| 3571 | cfq_mark_cfqq_deep(cfqq); |
| 3572 | |
Corrado Zoccolo | 749ef9f | 2010-09-20 15:24:50 +0200 | [diff] [blame] | 3573 | if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE)) |
| 3574 | enable_idle = 0; |
Tejun Heo | f6e8d01 | 2012-03-05 13:15:26 -0800 | [diff] [blame] | 3575 | else if (!atomic_read(&cic->icq.ioc->active_ref) || |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3576 | !cfqd->cfq_slice_idle || |
| 3577 | (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq))) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3578 | enable_idle = 0; |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3579 | else if (sample_valid(cic->ttime.ttime_samples)) { |
| 3580 | if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3581 | enable_idle = 0; |
| 3582 | else |
| 3583 | enable_idle = 1; |
| 3584 | } |
| 3585 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3586 | if (old_idle != enable_idle) { |
| 3587 | cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle); |
| 3588 | if (enable_idle) |
| 3589 | cfq_mark_cfqq_idle_window(cfqq); |
| 3590 | else |
| 3591 | cfq_clear_cfqq_idle_window(cfqq); |
| 3592 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3593 | } |
| 3594 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3595 | /* |
| 3596 | * Check if new_cfqq should preempt the currently active queue. Return 0 for |
| 3597 | * no or if we aren't sure, a 1 will cause a preempt. |
| 3598 | */ |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3599 | static bool |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3600 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3601 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3602 | { |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3603 | struct cfq_queue *cfqq; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3604 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3605 | cfqq = cfqd->active_queue; |
| 3606 | if (!cfqq) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3607 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3608 | |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3609 | if (cfq_class_idle(new_cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3610 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3611 | |
| 3612 | if (cfq_class_idle(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3613 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3614 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3615 | /* |
Divyesh Shah | 875feb6 | 2010-01-06 18:58:20 -0800 | [diff] [blame] | 3616 | * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice. |
| 3617 | */ |
| 3618 | if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq)) |
| 3619 | return false; |
| 3620 | |
| 3621 | /* |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3622 | * if the new request is sync, but the currently running queue is |
| 3623 | * not, let the sync request have priority. |
| 3624 | */ |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3625 | if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3626 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3627 | |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 3628 | if (new_cfqq->cfqg != cfqq->cfqg) |
| 3629 | return false; |
| 3630 | |
| 3631 | if (cfq_slice_used(cfqq)) |
| 3632 | return true; |
| 3633 | |
| 3634 | /* Allow preemption only if we are idling on sync-noidle tree */ |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3635 | if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD && |
Vivek Goyal | 8682e1f | 2009-12-03 12:59:50 -0500 | [diff] [blame] | 3636 | cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD && |
| 3637 | new_cfqq->service_tree->count == 2 && |
| 3638 | RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 3639 | return true; |
| 3640 | |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3641 | /* |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 3642 | * So both queues are sync. Let the new request get disk time if |
| 3643 | * it's a metadata request and the current queue is doing regular IO. |
| 3644 | */ |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 3645 | if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending) |
Jens Axboe | b53d1ed | 2011-08-19 08:34:48 +0200 | [diff] [blame] | 3646 | return true; |
| 3647 | |
| 3648 | /* |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3649 | * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice. |
| 3650 | */ |
| 3651 | if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3652 | return true; |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3653 | |
Shaohua Li | d2d59e1 | 2010-11-08 15:01:03 +0100 | [diff] [blame] | 3654 | /* An idle queue should not be idle now for some reason */ |
| 3655 | if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq)) |
| 3656 | return true; |
| 3657 | |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3658 | if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3659 | return false; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3660 | |
| 3661 | /* |
| 3662 | * if this request is as-good as one we would expect from the |
| 3663 | * current cfqq, let it preempt |
| 3664 | */ |
Shaohua Li | e9ce335 | 2010-03-19 08:03:04 +0100 | [diff] [blame] | 3665 | if (cfq_rq_close(cfqd, cfqq, rq)) |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3666 | return true; |
Jens Axboe | 1e3335d | 2007-02-14 19:59:49 +0100 | [diff] [blame] | 3667 | |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 3668 | return false; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | /* |
| 3672 | * cfqq preempts the active queue. if we allowed preempt with no slice left, |
| 3673 | * let it have half of its nominal slice. |
| 3674 | */ |
| 3675 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3676 | { |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 3677 | enum wl_type_t old_type = cfqq_type(cfqd->active_queue); |
| 3678 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3679 | cfq_log_cfqq(cfqd, cfqq, "preempt"); |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 3680 | cfq_slice_expired(cfqd, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3681 | |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 3682 | /* |
Shaohua Li | f8ae6e3 | 2011-01-14 08:41:02 +0100 | [diff] [blame] | 3683 | * workload type is changed, don't save slice, otherwise preempt |
| 3684 | * doesn't happen |
| 3685 | */ |
Shaohua Li | df0793a | 2012-01-19 09:20:09 +0100 | [diff] [blame] | 3686 | if (old_type != cfqq_type(cfqq)) |
Vivek Goyal | 4d2ceea | 2012-10-03 16:56:57 -0400 | [diff] [blame] | 3687 | cfqq->cfqg->saved_wl_slice = 0; |
Shaohua Li | f8ae6e3 | 2011-01-14 08:41:02 +0100 | [diff] [blame] | 3688 | |
| 3689 | /* |
Jens Axboe | bf57225 | 2006-07-19 20:29:12 +0200 | [diff] [blame] | 3690 | * Put the new queue at the front of the of the current list, |
| 3691 | * so we know that it will be selected next. |
| 3692 | */ |
| 3693 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
Jens Axboe | edd75ff | 2007-04-19 12:03:34 +0200 | [diff] [blame] | 3694 | |
| 3695 | cfq_service_tree_add(cfqd, cfqq, 1); |
Justin TerAvest | eda5e0c9 | 2011-03-22 21:26:49 +0100 | [diff] [blame] | 3696 | |
Justin TerAvest | 62a37f6 | 2011-03-23 08:25:44 +0100 | [diff] [blame] | 3697 | cfqq->slice_end = 0; |
| 3698 | cfq_mark_cfqq_slice_new(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | /* |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3702 | * Called when a new fs request (rq) is added (to cfqq). Check if there's |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3703 | * something we should do about it |
| 3704 | */ |
| 3705 | static void |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3706 | cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
| 3707 | struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3708 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3709 | struct cfq_io_cq *cic = RQ_CIC(rq); |
Jens Axboe | 12e9fdd | 2006-06-01 10:09:56 +0200 | [diff] [blame] | 3710 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3711 | cfqd->rq_queued++; |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 3712 | if (rq->cmd_flags & REQ_PRIO) |
| 3713 | cfqq->prio_pending++; |
Jens Axboe | 374f84a | 2006-07-23 01:42:19 +0200 | [diff] [blame] | 3714 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3715 | cfq_update_io_thinktime(cfqd, cfqq, cic); |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3716 | cfq_update_io_seektime(cfqd, cfqq, rq); |
Jens Axboe | 9c2c38a | 2005-08-24 14:57:54 +0200 | [diff] [blame] | 3717 | cfq_update_idle_window(cfqd, cfqq, cic); |
| 3718 | |
Jeff Moyer | b2c18e1 | 2009-10-23 17:14:49 -0400 | [diff] [blame] | 3719 | cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3720 | |
| 3721 | if (cfqq == cfqd->active_queue) { |
| 3722 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 3723 | * Remember that we saw a request from this process, but |
| 3724 | * don't start queuing just yet. Otherwise we risk seeing lots |
| 3725 | * of tiny requests, because we disrupt the normal plugging |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3726 | * and merging. If the request is already larger than a single |
| 3727 | * page, let it rip immediately. For that case we assume that |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 3728 | * merging is already done. Ditto for a busy system that |
| 3729 | * has other work pending, don't risk delaying until the |
| 3730 | * idle timer unplug to continue working. |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3731 | */ |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3732 | if (cfq_cfqq_wait_request(cfqq)) { |
Jens Axboe | 2d87072 | 2009-04-15 12:12:46 +0200 | [diff] [blame] | 3733 | if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE || |
| 3734 | cfqd->busy_queues > 1) { |
Divyesh Shah | 812df48 | 2010-04-08 21:15:35 -0700 | [diff] [blame] | 3735 | cfq_del_timer(cfqd, cfqq); |
Gui Jianfeng | 554554f | 2009-12-10 09:38:39 +0100 | [diff] [blame] | 3736 | cfq_clear_cfqq_wait_request(cfqq); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 3737 | __blk_run_queue(cfqd->queue); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 3738 | } else { |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 3739 | cfqg_stats_update_idle_time(cfqq->cfqg); |
Vivek Goyal | bf791937 | 2009-12-03 12:59:37 -0500 | [diff] [blame] | 3740 | cfq_mark_cfqq_must_dispatch(cfqq); |
Divyesh Shah | a11cdaa | 2010-04-13 19:59:17 +0200 | [diff] [blame] | 3741 | } |
Jens Axboe | d6ceb25 | 2009-04-14 14:18:16 +0200 | [diff] [blame] | 3742 | } |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3743 | } else if (cfq_should_preempt(cfqd, cfqq, rq)) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3744 | /* |
| 3745 | * not the active queue - expire current slice if it is |
| 3746 | * idle and has expired it's mean thinktime or this new queue |
Divyesh Shah | 3a9a3f6 | 2009-01-30 12:46:41 +0100 | [diff] [blame] | 3747 | * has some old slice time left and is of higher priority or |
| 3748 | * this new queue is RT and the current one is BE |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3749 | */ |
| 3750 | cfq_preempt_queue(cfqd, cfqq); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 3751 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3752 | } |
| 3753 | } |
| 3754 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3755 | static void cfq_insert_request(struct request_queue *q, struct request *rq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3756 | { |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3757 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3758 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3759 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 3760 | cfq_log_cfqq(cfqd, cfqq, "insert_request"); |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3761 | cfq_init_prio_data(cfqq, RQ_CIC(rq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | |
Jens Axboe | 30996f4 | 2009-10-05 11:03:39 +0200 | [diff] [blame] | 3763 | rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3764 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
Corrado Zoccolo | aa6f6a3 | 2009-10-26 22:44:33 +0100 | [diff] [blame] | 3765 | cfq_add_rq_rb(rq); |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 3766 | cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group, |
| 3767 | rq->cmd_flags); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3768 | cfq_rq_enqueued(cfqd, cfqq, rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3771 | /* |
| 3772 | * Update hw_tag based on peak queue depth over 50 samples under |
| 3773 | * sufficient load. |
| 3774 | */ |
| 3775 | static void cfq_update_hw_tag(struct cfq_data *cfqd) |
| 3776 | { |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 3777 | struct cfq_queue *cfqq = cfqd->active_queue; |
| 3778 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3779 | if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth) |
| 3780 | cfqd->hw_tag_est_depth = cfqd->rq_in_driver; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 3781 | |
| 3782 | if (cfqd->hw_tag == 1) |
| 3783 | return; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3784 | |
| 3785 | if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3786 | cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3787 | return; |
| 3788 | |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 3789 | /* |
| 3790 | * If active queue hasn't enough requests and can idle, cfq might not |
| 3791 | * dispatch sufficient requests to hardware. Don't zero hw_tag in this |
| 3792 | * case |
| 3793 | */ |
| 3794 | if (cfqq && cfq_cfqq_idle_window(cfqq) && |
| 3795 | cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] < |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3796 | CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN) |
Shaohua Li | 1a1238a | 2009-10-27 08:46:23 +0100 | [diff] [blame] | 3797 | return; |
| 3798 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3799 | if (cfqd->hw_tag_samples++ < 50) |
| 3800 | return; |
| 3801 | |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 3802 | if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN) |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3803 | cfqd->hw_tag = 1; |
| 3804 | else |
| 3805 | cfqd->hw_tag = 0; |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3808 | static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
| 3809 | { |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3810 | struct cfq_io_cq *cic = cfqd->active_cic; |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3811 | |
Justin TerAvest | 02a8f01 | 2011-02-09 14:20:03 +0100 | [diff] [blame] | 3812 | /* If the queue already has requests, don't wait */ |
| 3813 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
| 3814 | return false; |
| 3815 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3816 | /* If there are other queues in the group, don't wait */ |
| 3817 | if (cfqq->cfqg->nr_cfqq > 1) |
| 3818 | return false; |
| 3819 | |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 3820 | /* the only queue in the group, but think time is big */ |
| 3821 | if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) |
| 3822 | return false; |
| 3823 | |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3824 | if (cfq_slice_used(cfqq)) |
| 3825 | return true; |
| 3826 | |
| 3827 | /* if slice left is less than think time, wait busy */ |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3828 | if (cic && sample_valid(cic->ttime.ttime_samples) |
| 3829 | && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3830 | return true; |
| 3831 | |
| 3832 | /* |
| 3833 | * If think times is less than a jiffy than ttime_mean=0 and above |
| 3834 | * will not be true. It might happen that slice has not expired yet |
| 3835 | * but will expire soon (4-5 ns) during select_queue(). To cover the |
| 3836 | * case where think time is less than a jiffy, mark the queue wait |
| 3837 | * busy if only 1 jiffy is left in the slice. |
| 3838 | */ |
| 3839 | if (cfqq->slice_end - jiffies == 1) |
| 3840 | return true; |
| 3841 | |
| 3842 | return false; |
| 3843 | } |
| 3844 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3845 | static void cfq_completed_request(struct request_queue *q, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3847 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3848 | struct cfq_data *cfqd = cfqq->cfqd; |
Jens Axboe | 5380a10 | 2006-07-13 12:37:56 +0200 | [diff] [blame] | 3849 | const int sync = rq_is_sync(rq); |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3850 | unsigned long now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 3852 | now = jiffies; |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 3853 | cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", |
| 3854 | !!(rq->cmd_flags & REQ_NOIDLE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | |
Aaron Carroll | 45333d5 | 2008-08-26 15:52:36 +0200 | [diff] [blame] | 3856 | cfq_update_hw_tag(cfqd); |
| 3857 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3858 | WARN_ON(!cfqd->rq_in_driver); |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3859 | WARN_ON(!cfqq->dispatched); |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3860 | cfqd->rq_in_driver--; |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3861 | cfqq->dispatched--; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3862 | (RQ_CFQG(rq))->dispatched--; |
Tejun Heo | 155fead | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 3863 | cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq), |
| 3864 | rq_io_start_time_ns(rq), rq->cmd_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3866 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--; |
Jens Axboe | 3ed9a29 | 2007-04-23 08:33:33 +0200 | [diff] [blame] | 3867 | |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3868 | if (sync) { |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3869 | struct cfq_rb_root *st; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3870 | |
Shaohua Li | 383cd72 | 2011-07-12 14:24:35 +0200 | [diff] [blame] | 3871 | RQ_CIC(rq)->ttime.last_end_request = now; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3872 | |
| 3873 | if (cfq_cfqq_on_rr(cfqq)) |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3874 | st = cfqq->service_tree; |
Shaohua Li | f5f2b6c | 2011-07-12 14:24:55 +0200 | [diff] [blame] | 3875 | else |
Vivek Goyal | 34b98d0 | 2012-10-03 16:56:58 -0400 | [diff] [blame] | 3876 | st = st_for(cfqq->cfqg, cfqq_class(cfqq), |
| 3877 | cfqq_type(cfqq)); |
| 3878 | |
| 3879 | st->ttime.last_end_request = now; |
Corrado Zoccolo | 573412b | 2009-12-06 11:48:52 +0100 | [diff] [blame] | 3880 | if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now)) |
| 3881 | cfqd->last_delayed_sync = now; |
Vivek Goyal | 365722b | 2009-10-03 15:21:27 +0200 | [diff] [blame] | 3882 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3883 | |
Shaohua Li | 7700fc4 | 2011-07-12 14:24:56 +0200 | [diff] [blame] | 3884 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 3885 | cfqq->cfqg->ttime.last_end_request = now; |
| 3886 | #endif |
| 3887 | |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3888 | /* |
| 3889 | * If this is the active queue, check if it needs to be expired, |
| 3890 | * or if we want to idle in case it has no pending requests. |
| 3891 | */ |
| 3892 | if (cfqd->active_queue == cfqq) { |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3893 | const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list); |
| 3894 | |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 3895 | if (cfq_cfqq_slice_new(cfqq)) { |
| 3896 | cfq_set_prio_slice(cfqd, cfqq); |
| 3897 | cfq_clear_cfqq_slice_new(cfqq); |
| 3898 | } |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 3899 | |
| 3900 | /* |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3901 | * Should we wait for next request to come in before we expire |
| 3902 | * the queue. |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 3903 | */ |
Vivek Goyal | 7667aa0 | 2009-12-08 17:52:58 -0500 | [diff] [blame] | 3904 | if (cfq_should_wait_busy(cfqd, cfqq)) { |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 3905 | unsigned long extend_sl = cfqd->cfq_slice_idle; |
| 3906 | if (!cfqd->cfq_slice_idle) |
| 3907 | extend_sl = cfqd->cfq_group_idle; |
| 3908 | cfqq->slice_end = jiffies + extend_sl; |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 3909 | cfq_mark_cfqq_wait_busy(cfqq); |
Divyesh Shah | b1ffe73 | 2010-03-25 15:45:03 +0100 | [diff] [blame] | 3910 | cfq_log_cfqq(cfqd, cfqq, "will busy wait"); |
Vivek Goyal | f75edf2 | 2009-12-03 12:59:53 -0500 | [diff] [blame] | 3911 | } |
| 3912 | |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3913 | /* |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3914 | * Idling is not enabled on: |
| 3915 | * - expired queues |
| 3916 | * - idle-priority queues |
| 3917 | * - async queues |
| 3918 | * - queues with still some requests queued |
| 3919 | * - when there is a close cooperator |
Jens Axboe | a36e71f | 2009-04-15 12:15:11 +0200 | [diff] [blame] | 3920 | */ |
Jens Axboe | 0871714 | 2008-01-28 11:38:15 +0100 | [diff] [blame] | 3921 | if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 3922 | cfq_slice_expired(cfqd, 1); |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3923 | else if (sync && cfqq_empty && |
| 3924 | !cfq_close_cooperator(cfqd, cfqq)) { |
Corrado Zoccolo | 749ef9f | 2010-09-20 15:24:50 +0200 | [diff] [blame] | 3925 | cfq_arm_slice_timer(cfqd); |
Corrado Zoccolo | 8e55063 | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 3926 | } |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 3927 | } |
Jens Axboe | 6d048f5 | 2007-04-25 12:44:27 +0200 | [diff] [blame] | 3928 | |
Corrado Zoccolo | 53c583d | 2010-02-28 19:45:05 +0100 | [diff] [blame] | 3929 | if (!cfqd->rq_in_driver) |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 3930 | cfq_schedule_dispatch(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3933 | static inline int __cfq_may_queue(struct cfq_queue *cfqq) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3934 | { |
Jens Axboe | 1b379d8 | 2009-08-11 08:26:11 +0200 | [diff] [blame] | 3935 | if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) { |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3936 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3937 | return ELV_MQUEUE_MUST; |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 3938 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3939 | |
| 3940 | return ELV_MQUEUE_MAY; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3941 | } |
| 3942 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3943 | static int cfq_may_queue(struct request_queue *q, int rw) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3944 | { |
| 3945 | struct cfq_data *cfqd = q->elevator->elevator_data; |
| 3946 | struct task_struct *tsk = current; |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3947 | struct cfq_io_cq *cic; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3948 | struct cfq_queue *cfqq; |
| 3949 | |
| 3950 | /* |
| 3951 | * don't force setup of a queue from here, as a call to may_queue |
| 3952 | * does not necessarily imply that a request actually will be queued. |
| 3953 | * so just lookup a possibly existing queue, or return 'may queue' |
| 3954 | * if that fails |
| 3955 | */ |
Jens Axboe | 4ac845a | 2008-01-24 08:44:49 +0100 | [diff] [blame] | 3956 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 3957 | if (!cic) |
| 3958 | return ELV_MQUEUE_MAY; |
| 3959 | |
Jens Axboe | b0b78f8 | 2009-04-08 10:56:08 +0200 | [diff] [blame] | 3960 | cfqq = cic_to_cfqq(cic, rw_is_sync(rw)); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3961 | if (cfqq) { |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 3962 | cfq_init_prio_data(cfqq, cic); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3963 | |
Jens Axboe | 89850f7 | 2006-07-22 16:48:31 +0200 | [diff] [blame] | 3964 | return __cfq_may_queue(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | return ELV_MQUEUE_MAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | } |
| 3969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | /* |
| 3971 | * queue lock held here |
| 3972 | */ |
Jens Axboe | bb37b94 | 2006-12-01 10:42:33 +0100 | [diff] [blame] | 3973 | static void cfq_put_request(struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | { |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3975 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 3977 | if (cfqq) { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3978 | const int rw = rq_data_dir(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 3980 | BUG_ON(!cfqq->allocated[rw]); |
| 3981 | cfqq->allocated[rw]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 3983 | /* Put down rq reference on cfqg */ |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 3984 | cfqg_put(RQ_CFQG(rq)); |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3985 | rq->elv.priv[0] = NULL; |
| 3986 | rq->elv.priv[1] = NULL; |
Vivek Goyal | 7f1dc8a | 2010-04-21 17:44:16 +0200 | [diff] [blame] | 3987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | cfq_put_queue(cfqq); |
| 3989 | } |
| 3990 | } |
| 3991 | |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3992 | static struct cfq_queue * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 3993 | cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic, |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3994 | struct cfq_queue *cfqq) |
| 3995 | { |
| 3996 | cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq); |
| 3997 | cic_set_cfqq(cic, cfqq->new_cfqq, 1); |
Jeff Moyer | b3b6d04 | 2009-10-23 17:14:51 -0400 | [diff] [blame] | 3998 | cfq_mark_cfqq_coop(cfqq->new_cfqq); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 3999 | cfq_put_queue(cfqq); |
| 4000 | return cic_to_cfqq(cic, 1); |
| 4001 | } |
| 4002 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4003 | /* |
| 4004 | * Returns NULL if a new cfqq should be allocated, or the old cfqq if this |
| 4005 | * was the last process referring to said cfqq. |
| 4006 | */ |
| 4007 | static struct cfq_queue * |
Tejun Heo | c586980 | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4008 | split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq) |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4009 | { |
| 4010 | if (cfqq_process_refs(cfqq) == 1) { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4011 | cfqq->pid = current->pid; |
| 4012 | cfq_clear_cfqq_coop(cfqq); |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 4013 | cfq_clear_cfqq_split_coop(cfqq); |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4014 | return cfqq; |
| 4015 | } |
| 4016 | |
| 4017 | cic_set_cfqq(cic, NULL, 1); |
Shaohua Li | d02a2c0 | 2010-05-25 10:16:53 +0200 | [diff] [blame] | 4018 | |
| 4019 | cfq_put_cooperator(cfqq); |
| 4020 | |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4021 | cfq_put_queue(cfqq); |
| 4022 | return NULL; |
| 4023 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4025 | * Allocate cfq data structures associated with this request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | */ |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4027 | static int |
Tejun Heo | 852c788 | 2012-03-05 13:15:27 -0800 | [diff] [blame] | 4028 | cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio, |
| 4029 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | { |
| 4031 | struct cfq_data *cfqd = q->elevator->elevator_data; |
Tejun Heo | f1f8cc9 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4032 | struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | const int rw = rq_data_dir(rq); |
Jens Axboe | a6151c3 | 2009-10-07 20:02:57 +0200 | [diff] [blame] | 4034 | const bool is_sync = rq_is_sync(rq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4035 | struct cfq_queue *cfqq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | |
| 4037 | might_sleep_if(gfp_mask & __GFP_WAIT); |
| 4038 | |
Tejun Heo | 216284c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 4039 | spin_lock_irq(q->queue_lock); |
Tejun Heo | f1f8cc9 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4040 | |
Tejun Heo | 598971b | 2012-03-19 15:10:58 -0700 | [diff] [blame] | 4041 | check_ioprio_changed(cic, bio); |
| 4042 | check_blkcg_changed(cic, bio); |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4043 | new_queue: |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4044 | cfqq = cic_to_cfqq(cic, is_sync); |
Vivek Goyal | 32f2e80 | 2009-07-09 22:13:16 +0200 | [diff] [blame] | 4045 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
Tejun Heo | abede6d | 2012-03-19 15:10:57 -0700 | [diff] [blame] | 4046 | cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4047 | cic_set_cfqq(cic, cfqq, is_sync); |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4048 | } else { |
| 4049 | /* |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4050 | * If the queue was seeky for too long, break it apart. |
| 4051 | */ |
Shaohua Li | ae54abe | 2010-02-05 13:11:45 +0100 | [diff] [blame] | 4052 | if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) { |
Jeff Moyer | e6c5bc7 | 2009-10-23 17:14:52 -0400 | [diff] [blame] | 4053 | cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq"); |
| 4054 | cfqq = split_cfqq(cic, cfqq); |
| 4055 | if (!cfqq) |
| 4056 | goto new_queue; |
| 4057 | } |
| 4058 | |
| 4059 | /* |
Jeff Moyer | df5fe3e | 2009-10-23 17:14:50 -0400 | [diff] [blame] | 4060 | * Check to see if this queue is scheduled to merge with |
| 4061 | * another, closely cooperating queue. The merging of |
| 4062 | * queues happens here as it must be done in process context. |
| 4063 | * The reference on new_cfqq was taken in merge_cfqqs. |
| 4064 | */ |
| 4065 | if (cfqq->new_cfqq) |
| 4066 | cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq); |
Vasily Tarasov | 91fac31 | 2007-04-25 12:29:51 +0200 | [diff] [blame] | 4067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | |
| 4069 | cfqq->allocated[rw]++; |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4070 | |
Jens Axboe | 6fae9c2 | 2011-03-01 15:04:39 -0500 | [diff] [blame] | 4071 | cfqq->ref++; |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 4072 | cfqg_get(cfqq->cfqg); |
Tejun Heo | a612fdd | 2011-12-14 00:33:41 +0100 | [diff] [blame] | 4073 | rq->elv.priv[0] = cfqq; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4074 | rq->elv.priv[1] = cfqq->cfqg; |
Tejun Heo | 216284c | 2011-12-14 00:33:38 +0100 | [diff] [blame] | 4075 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 5e70537 | 2006-07-13 12:39:25 +0200 | [diff] [blame] | 4076 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | } |
| 4078 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4079 | static void cfq_kick_queue(struct work_struct *work) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4080 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 4081 | struct cfq_data *cfqd = |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4082 | container_of(work, struct cfq_data, unplug_work); |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4083 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4084 | |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 4085 | spin_lock_irq(q->queue_lock); |
Christoph Hellwig | 24ecfbe | 2011-04-18 11:41:33 +0200 | [diff] [blame] | 4086 | __blk_run_queue(cfqd->queue); |
Jens Axboe | 40bb54d | 2009-04-15 12:11:10 +0200 | [diff] [blame] | 4087 | spin_unlock_irq(q->queue_lock); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | /* |
| 4091 | * Timer running if the active_queue is currently idling inside its time slice |
| 4092 | */ |
| 4093 | static void cfq_idle_slice_timer(unsigned long data) |
| 4094 | { |
| 4095 | struct cfq_data *cfqd = (struct cfq_data *) data; |
| 4096 | struct cfq_queue *cfqq; |
| 4097 | unsigned long flags; |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 4098 | int timed_out = 1; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4099 | |
Jens Axboe | 7b67913 | 2008-05-30 12:23:07 +0200 | [diff] [blame] | 4100 | cfq_log(cfqd, "idle timer fired"); |
| 4101 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4102 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
| 4103 | |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4104 | cfqq = cfqd->active_queue; |
| 4105 | if (cfqq) { |
Jens Axboe | 3c6bd2f | 2007-01-19 12:06:33 +1100 | [diff] [blame] | 4106 | timed_out = 0; |
| 4107 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4108 | /* |
Jens Axboe | b029195 | 2009-04-07 11:38:31 +0200 | [diff] [blame] | 4109 | * We saw a request before the queue expired, let it through |
| 4110 | */ |
| 4111 | if (cfq_cfqq_must_dispatch(cfqq)) |
| 4112 | goto out_kick; |
| 4113 | |
| 4114 | /* |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4115 | * expired |
| 4116 | */ |
Jens Axboe | 44f7c16 | 2007-01-19 11:51:58 +1100 | [diff] [blame] | 4117 | if (cfq_slice_used(cfqq)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4118 | goto expire; |
| 4119 | |
| 4120 | /* |
| 4121 | * only expire and reinvoke request handler, if there are |
| 4122 | * other queues with pending requests |
| 4123 | */ |
Jens Axboe | caaa5f9 | 2006-06-16 11:23:00 +0200 | [diff] [blame] | 4124 | if (!cfqd->busy_queues) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4125 | goto out_cont; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4126 | |
| 4127 | /* |
| 4128 | * not expired and it has a request pending, let it dispatch |
| 4129 | */ |
Jens Axboe | 75e5098 | 2009-04-07 08:56:14 +0200 | [diff] [blame] | 4130 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4131 | goto out_kick; |
Corrado Zoccolo | 76280af | 2009-11-26 10:02:58 +0100 | [diff] [blame] | 4132 | |
| 4133 | /* |
| 4134 | * Queue depth flag is reset only when the idle didn't succeed |
| 4135 | */ |
| 4136 | cfq_clear_cfqq_deep(cfqq); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4137 | } |
| 4138 | expire: |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 4139 | cfq_slice_expired(cfqd, timed_out); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4140 | out_kick: |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4141 | cfq_schedule_dispatch(cfqd); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4142 | out_cont: |
| 4143 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
| 4144 | } |
| 4145 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4146 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
| 4147 | { |
| 4148 | del_timer_sync(&cfqd->idle_slice_timer); |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4149 | cancel_work_sync(&cfqd->unplug_work); |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4150 | } |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4151 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 4152 | static void cfq_put_async_queues(struct cfq_data *cfqd) |
| 4153 | { |
| 4154 | int i; |
| 4155 | |
| 4156 | for (i = 0; i < IOPRIO_BE_NR; i++) { |
| 4157 | if (cfqd->async_cfqq[0][i]) |
| 4158 | cfq_put_queue(cfqd->async_cfqq[0][i]); |
| 4159 | if (cfqd->async_cfqq[1][i]) |
| 4160 | cfq_put_queue(cfqd->async_cfqq[1][i]); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 4161 | } |
Oleg Nesterov | 2389d1e | 2007-11-05 08:58:05 +0100 | [diff] [blame] | 4162 | |
| 4163 | if (cfqd->async_idle_cfqq) |
| 4164 | cfq_put_queue(cfqd->async_idle_cfqq); |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 4165 | } |
| 4166 | |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4167 | static void cfq_exit_queue(struct elevator_queue *e) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | { |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4169 | struct cfq_data *cfqd = e->elevator_data; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4170 | struct request_queue *q = cfqd->queue; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4171 | |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4172 | cfq_shutdown_timer_wq(cfqd); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4173 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 4174 | spin_lock_irq(q->queue_lock); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4175 | |
Al Viro | d9ff418 | 2006-03-18 13:51:22 -0500 | [diff] [blame] | 4176 | if (cfqd->active_queue) |
Vivek Goyal | e5ff082 | 2010-04-26 19:25:11 +0200 | [diff] [blame] | 4177 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0); |
Jens Axboe | e2d74ac | 2006-03-28 08:59:01 +0200 | [diff] [blame] | 4178 | |
Vasily Tarasov | c2dea2d | 2007-07-20 10:06:38 +0200 | [diff] [blame] | 4179 | cfq_put_async_queues(cfqd); |
Tejun Heo | 03aa264 | 2012-03-05 13:15:19 -0800 | [diff] [blame] | 4180 | |
| 4181 | spin_unlock_irq(q->queue_lock); |
| 4182 | |
Al Viro | a90d742 | 2006-03-18 12:05:37 -0500 | [diff] [blame] | 4183 | cfq_shutdown_timer_wq(cfqd); |
| 4184 | |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4185 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 4186 | blkcg_deactivate_policy(q, &blkcg_policy_cfq); |
| 4187 | #else |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4188 | kfree(cfqd->root_group); |
Vivek Goyal | 2abae55 | 2011-05-23 10:02:19 +0200 | [diff] [blame] | 4189 | #endif |
Vivek Goyal | 56edf7d | 2011-05-19 15:38:22 -0400 | [diff] [blame] | 4190 | kfree(cfqd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | } |
| 4192 | |
Tejun Heo | b2fab5a | 2012-03-05 13:14:57 -0800 | [diff] [blame] | 4193 | static int cfq_init_queue(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | { |
| 4195 | struct cfq_data *cfqd; |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4196 | struct blkcg_gq *blkg __maybe_unused; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4197 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | |
Christoph Lameter | 94f6030 | 2007-07-17 04:03:29 -0700 | [diff] [blame] | 4199 | cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node); |
Tejun Heo | a73f730 | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 4200 | if (!cfqd) |
Tejun Heo | b2fab5a | 2012-03-05 13:14:57 -0800 | [diff] [blame] | 4201 | return -ENOMEM; |
Konstantin Khlebnikov | 80b15c7 | 2010-05-20 23:21:41 +0400 | [diff] [blame] | 4202 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4203 | cfqd->queue = q; |
| 4204 | q->elevator->elevator_data = cfqd; |
| 4205 | |
Vivek Goyal | 1fa8f6d | 2009-12-03 12:59:41 -0500 | [diff] [blame] | 4206 | /* Init root service tree */ |
| 4207 | cfqd->grp_service_tree = CFQ_RB_ROOT; |
| 4208 | |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4209 | /* Init root group and prefer root group over other groups by default */ |
Vivek Goyal | 25fb516 | 2009-12-03 12:59:46 -0500 | [diff] [blame] | 4210 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4211 | ret = blkcg_activate_policy(q, &blkcg_policy_cfq); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4212 | if (ret) |
| 4213 | goto out_free; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4214 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4215 | cfqd->root_group = blkg_to_cfqg(q->root_blkg); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4216 | #else |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4217 | ret = -ENOMEM; |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4218 | cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group), |
| 4219 | GFP_KERNEL, cfqd->queue->node); |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4220 | if (!cfqd->root_group) |
| 4221 | goto out_free; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4222 | |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4223 | cfq_init_cfqg_base(cfqd->root_group); |
| 4224 | #endif |
Tejun Heo | 3381cb8 | 2012-04-01 14:38:44 -0700 | [diff] [blame] | 4225 | cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT; |
Tejun Heo | e71357e | 2013-01-09 08:05:10 -0800 | [diff] [blame] | 4226 | cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT; |
Vivek Goyal | 5624a4e | 2011-05-19 15:38:28 -0400 | [diff] [blame] | 4227 | |
Jens Axboe | 26a2ac0 | 2009-04-23 12:13:27 +0200 | [diff] [blame] | 4228 | /* |
| 4229 | * Not strictly needed (since RB_ROOT just clears the node and we |
| 4230 | * zeroed cfqd on alloc), but better be safe in case someone decides |
| 4231 | * to add magic to the rb code |
| 4232 | */ |
| 4233 | for (i = 0; i < CFQ_PRIO_LISTS; i++) |
| 4234 | cfqd->prio_trees[i] = RB_ROOT; |
| 4235 | |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 4236 | /* |
| 4237 | * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues. |
| 4238 | * Grab a permanent reference to it, so that the normal code flow |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4239 | * will not attempt to free it. oom_cfqq is linked to root_group |
| 4240 | * but shouldn't hold a reference as it'll never be unlinked. Lose |
| 4241 | * the reference from linking right away. |
Jens Axboe | 6118b70 | 2009-06-30 09:34:12 +0200 | [diff] [blame] | 4242 | */ |
| 4243 | cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); |
Shaohua Li | 30d7b94 | 2011-01-07 08:46:59 +0100 | [diff] [blame] | 4244 | cfqd->oom_cfqq.ref++; |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4245 | |
| 4246 | spin_lock_irq(q->queue_lock); |
Tejun Heo | f51b802 | 2012-03-05 13:15:05 -0800 | [diff] [blame] | 4247 | cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group); |
Tejun Heo | eb7d8c07 | 2012-03-23 14:02:53 +0100 | [diff] [blame] | 4248 | cfqg_put(cfqd->root_group); |
Tejun Heo | 1adaf3d | 2012-03-05 13:15:15 -0800 | [diff] [blame] | 4249 | spin_unlock_irq(q->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4251 | init_timer(&cfqd->idle_slice_timer); |
| 4252 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; |
| 4253 | cfqd->idle_slice_timer.data = (unsigned long) cfqd; |
| 4254 | |
Jens Axboe | 23e018a | 2009-10-05 08:52:35 +0200 | [diff] [blame] | 4255 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | cfqd->cfq_quantum = cfq_quantum; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4258 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
| 4259 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | cfqd->cfq_back_max = cfq_back_max; |
| 4261 | cfqd->cfq_back_penalty = cfq_back_penalty; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4262 | cfqd->cfq_slice[0] = cfq_slice_async; |
| 4263 | cfqd->cfq_slice[1] = cfq_slice_sync; |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4264 | cfqd->cfq_target_latency = cfq_target_latency; |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4265 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; |
| 4266 | cfqd->cfq_slice_idle = cfq_slice_idle; |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4267 | cfqd->cfq_group_idle = cfq_group_idle; |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4268 | cfqd->cfq_latency = 1; |
Corrado Zoccolo | e459dd0 | 2009-11-26 10:02:57 +0100 | [diff] [blame] | 4269 | cfqd->hw_tag = -1; |
Corrado Zoccolo | edc7113 | 2009-12-09 20:56:04 +0100 | [diff] [blame] | 4270 | /* |
| 4271 | * we optimistically start assuming sync ops weren't delayed in last |
| 4272 | * second, in order to have larger depth for async operations. |
| 4273 | */ |
Corrado Zoccolo | 573412b | 2009-12-06 11:48:52 +0100 | [diff] [blame] | 4274 | cfqd->last_delayed_sync = jiffies - HZ; |
Tejun Heo | b2fab5a | 2012-03-05 13:14:57 -0800 | [diff] [blame] | 4275 | return 0; |
Tejun Heo | a2b1693 | 2012-04-13 13:11:33 -0700 | [diff] [blame] | 4276 | |
| 4277 | out_free: |
| 4278 | kfree(cfqd); |
| 4279 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | /* |
| 4283 | * sysfs parts below --> |
| 4284 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | static ssize_t |
| 4286 | cfq_var_show(unsigned int var, char *page) |
| 4287 | { |
| 4288 | return sprintf(page, "%d\n", var); |
| 4289 | } |
| 4290 | |
| 4291 | static ssize_t |
| 4292 | cfq_var_store(unsigned int *var, const char *page, size_t count) |
| 4293 | { |
| 4294 | char *p = (char *) page; |
| 4295 | |
| 4296 | *var = simple_strtoul(p, &p, 10); |
| 4297 | return count; |
| 4298 | } |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4301 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4303 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | unsigned int __data = __VAR; \ |
| 4305 | if (__CONV) \ |
| 4306 | __data = jiffies_to_msecs(__data); \ |
| 4307 | return cfq_var_show(__data, (page)); \ |
| 4308 | } |
| 4309 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4310 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
| 4311 | SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4312 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
| 4313 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4314 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4315 | SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4316 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); |
| 4317 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); |
| 4318 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4319 | SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4320 | SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | #undef SHOW_FUNCTION |
| 4322 | |
| 4323 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ |
Jens Axboe | b374d18 | 2008-10-31 10:05:07 +0100 | [diff] [blame] | 4324 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | { \ |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4326 | struct cfq_data *cfqd = e->elevator_data; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | unsigned int __data; \ |
| 4328 | int ret = cfq_var_store(&__data, (page), count); \ |
| 4329 | if (__data < (MIN)) \ |
| 4330 | __data = (MIN); \ |
| 4331 | else if (__data > (MAX)) \ |
| 4332 | __data = (MAX); \ |
| 4333 | if (__CONV) \ |
| 4334 | *(__PTR) = msecs_to_jiffies(__data); \ |
| 4335 | else \ |
| 4336 | *(__PTR) = __data; \ |
| 4337 | return ret; \ |
| 4338 | } |
| 4339 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4340 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, |
| 4341 | UINT_MAX, 1); |
| 4342 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, |
| 4343 | UINT_MAX, 1); |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4344 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4345 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, |
| 4346 | UINT_MAX, 0); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4347 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4348 | STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1); |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4349 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); |
| 4350 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); |
Jens Axboe | fe094d9 | 2008-01-31 13:08:54 +0100 | [diff] [blame] | 4351 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, |
| 4352 | UINT_MAX, 0); |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4353 | STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4354 | STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | #undef STORE_FUNCTION |
| 4356 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4357 | #define CFQ_ATTR(name) \ |
| 4358 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) |
Jens Axboe | 3b18152 | 2005-06-27 10:56:24 +0200 | [diff] [blame] | 4359 | |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4360 | static struct elv_fs_entry cfq_attrs[] = { |
| 4361 | CFQ_ATTR(quantum), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4362 | CFQ_ATTR(fifo_expire_sync), |
| 4363 | CFQ_ATTR(fifo_expire_async), |
| 4364 | CFQ_ATTR(back_seek_max), |
| 4365 | CFQ_ATTR(back_seek_penalty), |
| 4366 | CFQ_ATTR(slice_sync), |
| 4367 | CFQ_ATTR(slice_async), |
| 4368 | CFQ_ATTR(slice_async_rq), |
| 4369 | CFQ_ATTR(slice_idle), |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4370 | CFQ_ATTR(group_idle), |
Jens Axboe | 963b72f | 2009-10-03 19:42:18 +0200 | [diff] [blame] | 4371 | CFQ_ATTR(low_latency), |
Tao Ma | 5bf14c0 | 2012-04-01 14:33:39 -0700 | [diff] [blame] | 4372 | CFQ_ATTR(target_latency), |
Al Viro | e572ec7 | 2006-03-18 22:27:18 -0500 | [diff] [blame] | 4373 | __ATTR_NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | }; |
| 4375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | static struct elevator_type iosched_cfq = { |
| 4377 | .ops = { |
| 4378 | .elevator_merge_fn = cfq_merge, |
| 4379 | .elevator_merged_fn = cfq_merged_request, |
| 4380 | .elevator_merge_req_fn = cfq_merged_requests, |
Jens Axboe | da77526 | 2006-12-20 11:04:12 +0100 | [diff] [blame] | 4381 | .elevator_allow_merge_fn = cfq_allow_merge, |
Divyesh Shah | 812d402 | 2010-04-08 21:14:23 -0700 | [diff] [blame] | 4382 | .elevator_bio_merged_fn = cfq_bio_merged, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4383 | .elevator_dispatch_fn = cfq_dispatch_requests, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | .elevator_add_req_fn = cfq_insert_request, |
Jens Axboe | b4878f2 | 2005-10-20 16:42:29 +0200 | [diff] [blame] | 4385 | .elevator_activate_req_fn = cfq_activate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | .elevator_completed_req_fn = cfq_completed_request, |
Jens Axboe | 21183b0 | 2006-07-13 12:33:14 +0200 | [diff] [blame] | 4388 | .elevator_former_req_fn = elv_rb_former_request, |
| 4389 | .elevator_latter_req_fn = elv_rb_latter_request, |
Tejun Heo | 9b84cac | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4390 | .elevator_init_icq_fn = cfq_init_icq, |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4391 | .elevator_exit_icq_fn = cfq_exit_icq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | .elevator_set_req_fn = cfq_set_request, |
| 4393 | .elevator_put_req_fn = cfq_put_request, |
| 4394 | .elevator_may_queue_fn = cfq_may_queue, |
| 4395 | .elevator_init_fn = cfq_init_queue, |
| 4396 | .elevator_exit_fn = cfq_exit_queue, |
| 4397 | }, |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4398 | .icq_size = sizeof(struct cfq_io_cq), |
| 4399 | .icq_align = __alignof__(struct cfq_io_cq), |
Al Viro | 3d1ab40 | 2006-03-18 18:35:43 -0500 | [diff] [blame] | 4400 | .elevator_attrs = cfq_attrs, |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4401 | .elevator_name = "cfq", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | .elevator_owner = THIS_MODULE, |
| 4403 | }; |
| 4404 | |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4405 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4406 | static struct blkcg_policy blkcg_policy_cfq = { |
Tejun Heo | f9fcc2d | 2012-04-16 13:57:27 -0700 | [diff] [blame] | 4407 | .pd_size = sizeof(struct cfq_group), |
| 4408 | .cftypes = cfq_blkcg_files, |
| 4409 | |
| 4410 | .pd_init_fn = cfq_pd_init, |
| 4411 | .pd_reset_stats_fn = cfq_pd_reset_stats, |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4412 | }; |
Vivek Goyal | 3e25206 | 2009-12-04 10:36:42 -0500 | [diff] [blame] | 4413 | #endif |
| 4414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | static int __init cfq_init(void) |
| 4416 | { |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4417 | int ret; |
| 4418 | |
Jens Axboe | 22e2c50 | 2005-06-27 10:55:12 +0200 | [diff] [blame] | 4419 | /* |
| 4420 | * could be 0 on HZ < 1000 setups |
| 4421 | */ |
| 4422 | if (!cfq_slice_async) |
| 4423 | cfq_slice_async = 1; |
| 4424 | if (!cfq_slice_idle) |
| 4425 | cfq_slice_idle = 1; |
| 4426 | |
Vivek Goyal | 80bdf0c | 2010-08-23 12:24:26 +0200 | [diff] [blame] | 4427 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
| 4428 | if (!cfq_group_idle) |
| 4429 | cfq_group_idle = 1; |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4430 | |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4431 | ret = blkcg_policy_register(&blkcg_policy_cfq); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4432 | if (ret) |
| 4433 | return ret; |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4434 | #else |
| 4435 | cfq_group_idle = 0; |
| 4436 | #endif |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4437 | |
Tejun Heo | fd79495 | 2012-06-04 10:01:38 +0200 | [diff] [blame] | 4438 | ret = -ENOMEM; |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4439 | cfq_pool = KMEM_CACHE(cfq_queue, 0); |
| 4440 | if (!cfq_pool) |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4441 | goto err_pol_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4443 | ret = elv_register(&iosched_cfq); |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4444 | if (ret) |
| 4445 | goto err_free_pool; |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4446 | |
Adrian Bunk | 2fdd82b | 2007-12-12 18:51:56 +0100 | [diff] [blame] | 4447 | return 0; |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4448 | |
| 4449 | err_free_pool: |
| 4450 | kmem_cache_destroy(cfq_pool); |
| 4451 | err_pol_unreg: |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4452 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4453 | blkcg_policy_unregister(&blkcg_policy_cfq); |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4454 | #endif |
Tejun Heo | 8bd435b | 2012-04-13 13:11:28 -0700 | [diff] [blame] | 4455 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | static void __exit cfq_exit(void) |
| 4459 | { |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4460 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
Tejun Heo | 3c79839 | 2012-04-16 13:57:25 -0700 | [diff] [blame] | 4461 | blkcg_policy_unregister(&blkcg_policy_cfq); |
Tejun Heo | ffea73f | 2012-06-04 10:02:29 +0200 | [diff] [blame] | 4462 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | elv_unregister(&iosched_cfq); |
Tejun Heo | 3d3c237 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 4464 | kmem_cache_destroy(cfq_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | module_init(cfq_init); |
| 4468 | module_exit(cfq_exit); |
| 4469 | |
| 4470 | MODULE_AUTHOR("Jens Axboe"); |
| 4471 | MODULE_LICENSE("GPL"); |
| 4472 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |