Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
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 | * | |
0fe23479 | 7 | * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk> |
1da177e4 | 8 | */ |
1da177e4 | 9 | #include <linux/module.h> |
5a0e3ad6 | 10 | #include <linux/slab.h> |
1cc9be68 AV |
11 | #include <linux/blkdev.h> |
12 | #include <linux/elevator.h> | |
ad5ebd2f | 13 | #include <linux/jiffies.h> |
1da177e4 | 14 | #include <linux/rbtree.h> |
22e2c507 | 15 | #include <linux/ioprio.h> |
7b679138 | 16 | #include <linux/blktrace_api.h> |
6e736be7 | 17 | #include "blk.h" |
e98ef89b | 18 | #include "cfq.h" |
1da177e4 | 19 | |
0381411e TH |
20 | static struct blkio_policy_type blkio_policy_cfq; |
21 | ||
1da177e4 LT |
22 | /* |
23 | * tunables | |
24 | */ | |
fe094d98 | 25 | /* max queue in one round of service */ |
abc3c744 | 26 | static const int cfq_quantum = 8; |
64100099 | 27 | static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; |
fe094d98 JA |
28 | /* maximum backwards seek, in KiB */ |
29 | static const int cfq_back_max = 16 * 1024; | |
30 | /* penalty of a backwards seek */ | |
31 | static const int cfq_back_penalty = 2; | |
64100099 | 32 | static const int cfq_slice_sync = HZ / 10; |
3b18152c | 33 | static int cfq_slice_async = HZ / 25; |
64100099 | 34 | static const int cfq_slice_async_rq = 2; |
caaa5f9f | 35 | static int cfq_slice_idle = HZ / 125; |
80bdf0c7 | 36 | static int cfq_group_idle = HZ / 125; |
5db5d642 CZ |
37 | static const int cfq_target_latency = HZ * 3/10; /* 300 ms */ |
38 | static const int cfq_hist_divisor = 4; | |
22e2c507 | 39 | |
d9e7620e | 40 | /* |
0871714e | 41 | * offset from end of service tree |
d9e7620e | 42 | */ |
0871714e | 43 | #define CFQ_IDLE_DELAY (HZ / 5) |
d9e7620e JA |
44 | |
45 | /* | |
46 | * below this threshold, we consider thinktime immediate | |
47 | */ | |
48 | #define CFQ_MIN_TT (2) | |
49 | ||
22e2c507 | 50 | #define CFQ_SLICE_SCALE (5) |
45333d5a | 51 | #define CFQ_HW_QUEUE_MIN (5) |
25bc6b07 | 52 | #define CFQ_SERVICE_SHIFT 12 |
22e2c507 | 53 | |
3dde36dd | 54 | #define CFQQ_SEEK_THR (sector_t)(8 * 100) |
e9ce335d | 55 | #define CFQQ_CLOSE_THR (sector_t)(8 * 1024) |
41647e7a | 56 | #define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32) |
3dde36dd | 57 | #define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8) |
ae54abed | 58 | |
a612fddf TH |
59 | #define RQ_CIC(rq) icq_to_cic((rq)->elv.icq) |
60 | #define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0]) | |
61 | #define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1]) | |
1da177e4 | 62 | |
e18b890b | 63 | static struct kmem_cache *cfq_pool; |
1da177e4 | 64 | |
22e2c507 JA |
65 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
66 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) | |
22e2c507 JA |
67 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
68 | ||
206dc69b | 69 | #define sample_valid(samples) ((samples) > 80) |
1fa8f6d6 | 70 | #define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node) |
206dc69b | 71 | |
c5869807 TH |
72 | struct cfq_ttime { |
73 | unsigned long last_end_request; | |
74 | ||
75 | unsigned long ttime_total; | |
76 | unsigned long ttime_samples; | |
77 | unsigned long ttime_mean; | |
78 | }; | |
79 | ||
cc09e299 JA |
80 | /* |
81 | * Most of our rbtree usage is for sorting with min extraction, so | |
82 | * if we cache the leftmost node we don't have to walk down the tree | |
83 | * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should | |
84 | * move this into the elevator for the rq sorting as well. | |
85 | */ | |
86 | struct cfq_rb_root { | |
87 | struct rb_root rb; | |
88 | struct rb_node *left; | |
aa6f6a3d | 89 | unsigned count; |
73e9ffdd | 90 | unsigned total_weight; |
1fa8f6d6 | 91 | u64 min_vdisktime; |
f5f2b6ce | 92 | struct cfq_ttime ttime; |
cc09e299 | 93 | }; |
f5f2b6ce SL |
94 | #define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \ |
95 | .ttime = {.last_end_request = jiffies,},} | |
cc09e299 | 96 | |
6118b70b JA |
97 | /* |
98 | * Per process-grouping structure | |
99 | */ | |
100 | struct cfq_queue { | |
101 | /* reference count */ | |
30d7b944 | 102 | int ref; |
6118b70b JA |
103 | /* various state flags, see below */ |
104 | unsigned int flags; | |
105 | /* parent cfq_data */ | |
106 | struct cfq_data *cfqd; | |
107 | /* service_tree member */ | |
108 | struct rb_node rb_node; | |
109 | /* service_tree key */ | |
110 | unsigned long rb_key; | |
111 | /* prio tree member */ | |
112 | struct rb_node p_node; | |
113 | /* prio tree root we belong to, if any */ | |
114 | struct rb_root *p_root; | |
115 | /* sorted list of pending requests */ | |
116 | struct rb_root sort_list; | |
117 | /* if fifo isn't expired, next request to serve */ | |
118 | struct request *next_rq; | |
119 | /* requests queued in sort_list */ | |
120 | int queued[2]; | |
121 | /* currently allocated requests */ | |
122 | int allocated[2]; | |
123 | /* fifo list of requests in sort_list */ | |
124 | struct list_head fifo; | |
125 | ||
dae739eb VG |
126 | /* time when queue got scheduled in to dispatch first request. */ |
127 | unsigned long dispatch_start; | |
f75edf2d | 128 | unsigned int allocated_slice; |
c4081ba5 | 129 | unsigned int slice_dispatch; |
dae739eb VG |
130 | /* time when first request from queue completed and slice started. */ |
131 | unsigned long slice_start; | |
6118b70b JA |
132 | unsigned long slice_end; |
133 | long slice_resid; | |
6118b70b | 134 | |
65299a3b CH |
135 | /* pending priority requests */ |
136 | int prio_pending; | |
6118b70b JA |
137 | /* number of requests that are on the dispatch list or inside driver */ |
138 | int dispatched; | |
139 | ||
140 | /* io prio of this group */ | |
141 | unsigned short ioprio, org_ioprio; | |
4aede84b | 142 | unsigned short ioprio_class; |
6118b70b | 143 | |
c4081ba5 RK |
144 | pid_t pid; |
145 | ||
3dde36dd | 146 | u32 seek_history; |
b2c18e1e JM |
147 | sector_t last_request_pos; |
148 | ||
aa6f6a3d | 149 | struct cfq_rb_root *service_tree; |
df5fe3e8 | 150 | struct cfq_queue *new_cfqq; |
cdb16e8f | 151 | struct cfq_group *cfqg; |
c4e7893e VG |
152 | /* Number of sectors dispatched from queue in single dispatch round */ |
153 | unsigned long nr_sectors; | |
6118b70b JA |
154 | }; |
155 | ||
c0324a02 | 156 | /* |
718eee05 | 157 | * First index in the service_trees. |
c0324a02 CZ |
158 | * IDLE is handled separately, so it has negative index |
159 | */ | |
160 | enum wl_prio_t { | |
c0324a02 | 161 | BE_WORKLOAD = 0, |
615f0259 VG |
162 | RT_WORKLOAD = 1, |
163 | IDLE_WORKLOAD = 2, | |
b4627321 | 164 | CFQ_PRIO_NR, |
c0324a02 CZ |
165 | }; |
166 | ||
718eee05 CZ |
167 | /* |
168 | * Second index in the service_trees. | |
169 | */ | |
170 | enum wl_type_t { | |
171 | ASYNC_WORKLOAD = 0, | |
172 | SYNC_NOIDLE_WORKLOAD = 1, | |
173 | SYNC_WORKLOAD = 2 | |
174 | }; | |
175 | ||
cdb16e8f VG |
176 | /* This is per cgroup per device grouping structure */ |
177 | struct cfq_group { | |
1fa8f6d6 VG |
178 | /* group service_tree member */ |
179 | struct rb_node rb_node; | |
180 | ||
181 | /* group service_tree key */ | |
182 | u64 vdisktime; | |
25bc6b07 | 183 | unsigned int weight; |
8184f93e JT |
184 | unsigned int new_weight; |
185 | bool needs_update; | |
1fa8f6d6 VG |
186 | |
187 | /* number of cfqq currently on this group */ | |
188 | int nr_cfqq; | |
189 | ||
cdb16e8f | 190 | /* |
4495a7d4 | 191 | * Per group busy queues average. Useful for workload slice calc. We |
b4627321 VG |
192 | * create the array for each prio class but at run time it is used |
193 | * only for RT and BE class and slot for IDLE class remains unused. | |
194 | * This is primarily done to avoid confusion and a gcc warning. | |
195 | */ | |
196 | unsigned int busy_queues_avg[CFQ_PRIO_NR]; | |
197 | /* | |
198 | * rr lists of queues with requests. We maintain service trees for | |
199 | * RT and BE classes. These trees are subdivided in subclasses | |
200 | * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE | |
201 | * class there is no subclassification and all the cfq queues go on | |
202 | * a single tree service_tree_idle. | |
cdb16e8f VG |
203 | * Counts are embedded in the cfq_rb_root |
204 | */ | |
205 | struct cfq_rb_root service_trees[2][3]; | |
206 | struct cfq_rb_root service_tree_idle; | |
dae739eb VG |
207 | |
208 | unsigned long saved_workload_slice; | |
209 | enum wl_type_t saved_workload; | |
210 | enum wl_prio_t saved_serving_prio; | |
4eef3049 | 211 | |
80bdf0c7 VG |
212 | /* number of requests that are on the dispatch list or inside driver */ |
213 | int dispatched; | |
7700fc4f | 214 | struct cfq_ttime ttime; |
cdb16e8f | 215 | }; |
718eee05 | 216 | |
c5869807 TH |
217 | struct cfq_io_cq { |
218 | struct io_cq icq; /* must be the first member */ | |
219 | struct cfq_queue *cfqq[2]; | |
220 | struct cfq_ttime ttime; | |
221 | }; | |
222 | ||
22e2c507 JA |
223 | /* |
224 | * Per block device queue structure | |
225 | */ | |
1da177e4 | 226 | struct cfq_data { |
165125e1 | 227 | struct request_queue *queue; |
1fa8f6d6 VG |
228 | /* Root service tree for cfq_groups */ |
229 | struct cfq_rb_root grp_service_tree; | |
f51b802c | 230 | struct cfq_group *root_group; |
22e2c507 | 231 | |
c0324a02 CZ |
232 | /* |
233 | * The priority currently being served | |
22e2c507 | 234 | */ |
c0324a02 | 235 | enum wl_prio_t serving_prio; |
718eee05 CZ |
236 | enum wl_type_t serving_type; |
237 | unsigned long workload_expires; | |
cdb16e8f | 238 | struct cfq_group *serving_group; |
a36e71f9 JA |
239 | |
240 | /* | |
241 | * Each priority tree is sorted by next_request position. These | |
242 | * trees are used when determining if two or more queues are | |
243 | * interleaving requests (see cfq_close_cooperator). | |
244 | */ | |
245 | struct rb_root prio_trees[CFQ_PRIO_LISTS]; | |
246 | ||
22e2c507 | 247 | unsigned int busy_queues; |
ef8a41df | 248 | unsigned int busy_sync_queues; |
22e2c507 | 249 | |
53c583d2 CZ |
250 | int rq_in_driver; |
251 | int rq_in_flight[2]; | |
45333d5a AC |
252 | |
253 | /* | |
254 | * queue-depth detection | |
255 | */ | |
256 | int rq_queued; | |
25776e35 | 257 | int hw_tag; |
e459dd08 CZ |
258 | /* |
259 | * hw_tag can be | |
260 | * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection) | |
261 | * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth) | |
262 | * 0 => no NCQ | |
263 | */ | |
264 | int hw_tag_est_depth; | |
265 | unsigned int hw_tag_samples; | |
1da177e4 | 266 | |
22e2c507 JA |
267 | /* |
268 | * idle window management | |
269 | */ | |
270 | struct timer_list idle_slice_timer; | |
23e018a1 | 271 | struct work_struct unplug_work; |
1da177e4 | 272 | |
22e2c507 | 273 | struct cfq_queue *active_queue; |
c5869807 | 274 | struct cfq_io_cq *active_cic; |
22e2c507 | 275 | |
c2dea2d1 VT |
276 | /* |
277 | * async queue for each priority case | |
278 | */ | |
279 | struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR]; | |
280 | struct cfq_queue *async_idle_cfqq; | |
15c31be4 | 281 | |
6d048f53 | 282 | sector_t last_position; |
1da177e4 | 283 | |
1da177e4 LT |
284 | /* |
285 | * tunables, see top of file | |
286 | */ | |
287 | unsigned int cfq_quantum; | |
22e2c507 | 288 | unsigned int cfq_fifo_expire[2]; |
1da177e4 LT |
289 | unsigned int cfq_back_penalty; |
290 | unsigned int cfq_back_max; | |
22e2c507 JA |
291 | unsigned int cfq_slice[2]; |
292 | unsigned int cfq_slice_async_rq; | |
293 | unsigned int cfq_slice_idle; | |
80bdf0c7 | 294 | unsigned int cfq_group_idle; |
963b72fc | 295 | unsigned int cfq_latency; |
d9ff4187 | 296 | |
6118b70b JA |
297 | /* |
298 | * Fallback dummy cfqq for extreme OOM conditions | |
299 | */ | |
300 | struct cfq_queue oom_cfqq; | |
365722bb | 301 | |
573412b2 | 302 | unsigned long last_delayed_sync; |
1da177e4 LT |
303 | }; |
304 | ||
0381411e TH |
305 | static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg) |
306 | { | |
307 | return blkg_to_pdata(blkg, &blkio_policy_cfq); | |
308 | } | |
309 | ||
310 | static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg) | |
311 | { | |
312 | return pdata_to_blkg(cfqg, &blkio_policy_cfq); | |
313 | } | |
314 | ||
25fb5169 VG |
315 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd); |
316 | ||
cdb16e8f VG |
317 | static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg, |
318 | enum wl_prio_t prio, | |
65b32a57 | 319 | enum wl_type_t type) |
c0324a02 | 320 | { |
1fa8f6d6 VG |
321 | if (!cfqg) |
322 | return NULL; | |
323 | ||
c0324a02 | 324 | if (prio == IDLE_WORKLOAD) |
cdb16e8f | 325 | return &cfqg->service_tree_idle; |
c0324a02 | 326 | |
cdb16e8f | 327 | return &cfqg->service_trees[prio][type]; |
c0324a02 CZ |
328 | } |
329 | ||
3b18152c | 330 | enum cfqq_state_flags { |
b0b8d749 JA |
331 | CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ |
332 | CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ | |
b029195d | 333 | CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */ |
b0b8d749 | 334 | CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ |
b0b8d749 JA |
335 | CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ |
336 | CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ | |
337 | CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ | |
44f7c160 | 338 | CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ |
91fac317 | 339 | CFQ_CFQQ_FLAG_sync, /* synchronous queue */ |
b3b6d040 | 340 | CFQ_CFQQ_FLAG_coop, /* cfqq is shared */ |
ae54abed | 341 | CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */ |
76280aff | 342 | CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */ |
f75edf2d | 343 | CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */ |
3b18152c JA |
344 | }; |
345 | ||
346 | #define CFQ_CFQQ_FNS(name) \ | |
347 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ | |
348 | { \ | |
fe094d98 | 349 | (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ |
3b18152c JA |
350 | } \ |
351 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ | |
352 | { \ | |
fe094d98 | 353 | (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ |
3b18152c JA |
354 | } \ |
355 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ | |
356 | { \ | |
fe094d98 | 357 | return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ |
3b18152c JA |
358 | } |
359 | ||
360 | CFQ_CFQQ_FNS(on_rr); | |
361 | CFQ_CFQQ_FNS(wait_request); | |
b029195d | 362 | CFQ_CFQQ_FNS(must_dispatch); |
3b18152c | 363 | CFQ_CFQQ_FNS(must_alloc_slice); |
3b18152c JA |
364 | CFQ_CFQQ_FNS(fifo_expire); |
365 | CFQ_CFQQ_FNS(idle_window); | |
366 | CFQ_CFQQ_FNS(prio_changed); | |
44f7c160 | 367 | CFQ_CFQQ_FNS(slice_new); |
91fac317 | 368 | CFQ_CFQQ_FNS(sync); |
a36e71f9 | 369 | CFQ_CFQQ_FNS(coop); |
ae54abed | 370 | CFQ_CFQQ_FNS(split_coop); |
76280aff | 371 | CFQ_CFQQ_FNS(deep); |
f75edf2d | 372 | CFQ_CFQQ_FNS(wait_busy); |
3b18152c JA |
373 | #undef CFQ_CFQQ_FNS |
374 | ||
afc24d49 | 375 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
2868ef7b VG |
376 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
377 | blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \ | |
378 | cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \ | |
0381411e | 379 | blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args) |
2868ef7b VG |
380 | |
381 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \ | |
382 | blk_add_trace_msg((cfqd)->queue, "%s " fmt, \ | |
0381411e | 383 | blkg_path(cfqg_to_blkg((cfqg))), ##args) \ |
2868ef7b VG |
384 | |
385 | #else | |
7b679138 JA |
386 | #define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \ |
387 | blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args) | |
4495a7d4 | 388 | #define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0) |
2868ef7b | 389 | #endif |
7b679138 JA |
390 | #define cfq_log(cfqd, fmt, args...) \ |
391 | blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args) | |
392 | ||
615f0259 VG |
393 | /* Traverses through cfq group service trees */ |
394 | #define for_each_cfqg_st(cfqg, i, j, st) \ | |
395 | for (i = 0; i <= IDLE_WORKLOAD; i++) \ | |
396 | for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\ | |
397 | : &cfqg->service_tree_idle; \ | |
398 | (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \ | |
399 | (i == IDLE_WORKLOAD && j == 0); \ | |
400 | j++, st = i < IDLE_WORKLOAD ? \ | |
401 | &cfqg->service_trees[i][j]: NULL) \ | |
402 | ||
f5f2b6ce SL |
403 | static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd, |
404 | struct cfq_ttime *ttime, bool group_idle) | |
405 | { | |
406 | unsigned long slice; | |
407 | if (!sample_valid(ttime->ttime_samples)) | |
408 | return false; | |
409 | if (group_idle) | |
410 | slice = cfqd->cfq_group_idle; | |
411 | else | |
412 | slice = cfqd->cfq_slice_idle; | |
413 | return ttime->ttime_mean > slice; | |
414 | } | |
615f0259 | 415 | |
02b35081 VG |
416 | static inline bool iops_mode(struct cfq_data *cfqd) |
417 | { | |
418 | /* | |
419 | * If we are not idling on queues and it is a NCQ drive, parallel | |
420 | * execution of requests is on and measuring time is not possible | |
421 | * in most of the cases until and unless we drive shallower queue | |
422 | * depths and that becomes a performance bottleneck. In such cases | |
423 | * switch to start providing fairness in terms of number of IOs. | |
424 | */ | |
425 | if (!cfqd->cfq_slice_idle && cfqd->hw_tag) | |
426 | return true; | |
427 | else | |
428 | return false; | |
429 | } | |
430 | ||
c0324a02 CZ |
431 | static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq) |
432 | { | |
433 | if (cfq_class_idle(cfqq)) | |
434 | return IDLE_WORKLOAD; | |
435 | if (cfq_class_rt(cfqq)) | |
436 | return RT_WORKLOAD; | |
437 | return BE_WORKLOAD; | |
438 | } | |
439 | ||
718eee05 CZ |
440 | |
441 | static enum wl_type_t cfqq_type(struct cfq_queue *cfqq) | |
442 | { | |
443 | if (!cfq_cfqq_sync(cfqq)) | |
444 | return ASYNC_WORKLOAD; | |
445 | if (!cfq_cfqq_idle_window(cfqq)) | |
446 | return SYNC_NOIDLE_WORKLOAD; | |
447 | return SYNC_WORKLOAD; | |
448 | } | |
449 | ||
58ff82f3 VG |
450 | static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl, |
451 | struct cfq_data *cfqd, | |
452 | struct cfq_group *cfqg) | |
c0324a02 CZ |
453 | { |
454 | if (wl == IDLE_WORKLOAD) | |
cdb16e8f | 455 | return cfqg->service_tree_idle.count; |
c0324a02 | 456 | |
cdb16e8f VG |
457 | return cfqg->service_trees[wl][ASYNC_WORKLOAD].count |
458 | + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count | |
459 | + cfqg->service_trees[wl][SYNC_WORKLOAD].count; | |
c0324a02 CZ |
460 | } |
461 | ||
f26bd1f0 VG |
462 | static inline int cfqg_busy_async_queues(struct cfq_data *cfqd, |
463 | struct cfq_group *cfqg) | |
464 | { | |
465 | return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count | |
466 | + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count; | |
467 | } | |
468 | ||
165125e1 | 469 | static void cfq_dispatch_insert(struct request_queue *, struct request *); |
4f85cb96 TH |
470 | static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync, |
471 | struct io_context *ioc, struct bio *bio, | |
472 | gfp_t gfp_mask); | |
91fac317 | 473 | |
c5869807 TH |
474 | static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq) |
475 | { | |
476 | /* cic->icq is the first member, %NULL will convert to %NULL */ | |
477 | return container_of(icq, struct cfq_io_cq, icq); | |
478 | } | |
479 | ||
47fdd4ca TH |
480 | static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd, |
481 | struct io_context *ioc) | |
482 | { | |
483 | if (ioc) | |
484 | return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue)); | |
485 | return NULL; | |
486 | } | |
487 | ||
c5869807 | 488 | static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync) |
91fac317 | 489 | { |
a6151c3a | 490 | return cic->cfqq[is_sync]; |
91fac317 VT |
491 | } |
492 | ||
c5869807 TH |
493 | static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq, |
494 | bool is_sync) | |
91fac317 | 495 | { |
a6151c3a | 496 | cic->cfqq[is_sync] = cfqq; |
91fac317 VT |
497 | } |
498 | ||
c5869807 | 499 | static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic) |
bca4b914 | 500 | { |
c5869807 | 501 | return cic->icq.q->elevator->elevator_data; |
bca4b914 KK |
502 | } |
503 | ||
91fac317 VT |
504 | /* |
505 | * We regard a request as SYNC, if it's either a read or has the SYNC bit | |
506 | * set (in which case it could also be direct WRITE). | |
507 | */ | |
a6151c3a | 508 | static inline bool cfq_bio_sync(struct bio *bio) |
91fac317 | 509 | { |
7b6d91da | 510 | return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC); |
91fac317 | 511 | } |
1da177e4 | 512 | |
99f95e52 AM |
513 | /* |
514 | * scheduler run of queue, if there are requests pending and no one in the | |
515 | * driver that will restart queueing | |
516 | */ | |
23e018a1 | 517 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) |
99f95e52 | 518 | { |
7b679138 JA |
519 | if (cfqd->busy_queues) { |
520 | cfq_log(cfqd, "schedule dispatch"); | |
23e018a1 | 521 | kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work); |
7b679138 | 522 | } |
99f95e52 AM |
523 | } |
524 | ||
44f7c160 JA |
525 | /* |
526 | * Scale schedule slice based on io priority. Use the sync time slice only | |
527 | * if a queue is marked sync and has sync io queued. A sync queue with async | |
528 | * io only, should not get full sync slice length. | |
529 | */ | |
a6151c3a | 530 | static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync, |
d9e7620e | 531 | unsigned short prio) |
44f7c160 | 532 | { |
d9e7620e | 533 | const int base_slice = cfqd->cfq_slice[sync]; |
44f7c160 | 534 | |
d9e7620e JA |
535 | WARN_ON(prio >= IOPRIO_BE_NR); |
536 | ||
537 | return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio)); | |
538 | } | |
44f7c160 | 539 | |
d9e7620e JA |
540 | static inline int |
541 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
542 | { | |
543 | return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); | |
44f7c160 JA |
544 | } |
545 | ||
25bc6b07 VG |
546 | static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg) |
547 | { | |
548 | u64 d = delta << CFQ_SERVICE_SHIFT; | |
549 | ||
550 | d = d * BLKIO_WEIGHT_DEFAULT; | |
551 | do_div(d, cfqg->weight); | |
552 | return d; | |
553 | } | |
554 | ||
555 | static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime) | |
556 | { | |
557 | s64 delta = (s64)(vdisktime - min_vdisktime); | |
558 | if (delta > 0) | |
559 | min_vdisktime = vdisktime; | |
560 | ||
561 | return min_vdisktime; | |
562 | } | |
563 | ||
564 | static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime) | |
565 | { | |
566 | s64 delta = (s64)(vdisktime - min_vdisktime); | |
567 | if (delta < 0) | |
568 | min_vdisktime = vdisktime; | |
569 | ||
570 | return min_vdisktime; | |
571 | } | |
572 | ||
573 | static void update_min_vdisktime(struct cfq_rb_root *st) | |
574 | { | |
25bc6b07 VG |
575 | struct cfq_group *cfqg; |
576 | ||
25bc6b07 VG |
577 | if (st->left) { |
578 | cfqg = rb_entry_cfqg(st->left); | |
a6032710 GJ |
579 | st->min_vdisktime = max_vdisktime(st->min_vdisktime, |
580 | cfqg->vdisktime); | |
25bc6b07 | 581 | } |
25bc6b07 VG |
582 | } |
583 | ||
5db5d642 CZ |
584 | /* |
585 | * get averaged number of queues of RT/BE priority. | |
586 | * average is updated, with a formula that gives more weight to higher numbers, | |
587 | * to quickly follows sudden increases and decrease slowly | |
588 | */ | |
589 | ||
58ff82f3 VG |
590 | static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd, |
591 | struct cfq_group *cfqg, bool rt) | |
5869619c | 592 | { |
5db5d642 CZ |
593 | unsigned min_q, max_q; |
594 | unsigned mult = cfq_hist_divisor - 1; | |
595 | unsigned round = cfq_hist_divisor / 2; | |
58ff82f3 | 596 | unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg); |
5db5d642 | 597 | |
58ff82f3 VG |
598 | min_q = min(cfqg->busy_queues_avg[rt], busy); |
599 | max_q = max(cfqg->busy_queues_avg[rt], busy); | |
600 | cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) / | |
5db5d642 | 601 | cfq_hist_divisor; |
58ff82f3 VG |
602 | return cfqg->busy_queues_avg[rt]; |
603 | } | |
604 | ||
605 | static inline unsigned | |
606 | cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg) | |
607 | { | |
608 | struct cfq_rb_root *st = &cfqd->grp_service_tree; | |
609 | ||
610 | return cfq_target_latency * cfqg->weight / st->total_weight; | |
5db5d642 CZ |
611 | } |
612 | ||
c553f8e3 | 613 | static inline unsigned |
ba5bd520 | 614 | cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
44f7c160 | 615 | { |
5db5d642 CZ |
616 | unsigned slice = cfq_prio_to_slice(cfqd, cfqq); |
617 | if (cfqd->cfq_latency) { | |
58ff82f3 VG |
618 | /* |
619 | * interested queues (we consider only the ones with the same | |
620 | * priority class in the cfq group) | |
621 | */ | |
622 | unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg, | |
623 | cfq_class_rt(cfqq)); | |
5db5d642 CZ |
624 | unsigned sync_slice = cfqd->cfq_slice[1]; |
625 | unsigned expect_latency = sync_slice * iq; | |
58ff82f3 VG |
626 | unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg); |
627 | ||
628 | if (expect_latency > group_slice) { | |
5db5d642 CZ |
629 | unsigned base_low_slice = 2 * cfqd->cfq_slice_idle; |
630 | /* scale low_slice according to IO priority | |
631 | * and sync vs async */ | |
632 | unsigned low_slice = | |
633 | min(slice, base_low_slice * slice / sync_slice); | |
634 | /* the adapted slice value is scaled to fit all iqs | |
635 | * into the target latency */ | |
58ff82f3 | 636 | slice = max(slice * group_slice / expect_latency, |
5db5d642 CZ |
637 | low_slice); |
638 | } | |
639 | } | |
c553f8e3 SL |
640 | return slice; |
641 | } | |
642 | ||
643 | static inline void | |
644 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
645 | { | |
ba5bd520 | 646 | unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq); |
c553f8e3 | 647 | |
dae739eb | 648 | cfqq->slice_start = jiffies; |
5db5d642 | 649 | cfqq->slice_end = jiffies + slice; |
f75edf2d | 650 | cfqq->allocated_slice = slice; |
7b679138 | 651 | cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies); |
44f7c160 JA |
652 | } |
653 | ||
654 | /* | |
655 | * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end | |
656 | * isn't valid until the first request from the dispatch is activated | |
657 | * and the slice time set. | |
658 | */ | |
a6151c3a | 659 | static inline bool cfq_slice_used(struct cfq_queue *cfqq) |
44f7c160 JA |
660 | { |
661 | if (cfq_cfqq_slice_new(cfqq)) | |
c1e44756 | 662 | return false; |
44f7c160 | 663 | if (time_before(jiffies, cfqq->slice_end)) |
c1e44756 | 664 | return false; |
44f7c160 | 665 | |
c1e44756 | 666 | return true; |
44f7c160 JA |
667 | } |
668 | ||
1da177e4 | 669 | /* |
5e705374 | 670 | * Lifted from AS - choose which of rq1 and rq2 that is best served now. |
1da177e4 | 671 | * We choose the request that is closest to the head right now. Distance |
e8a99053 | 672 | * behind the head is penalized and only allowed to a certain extent. |
1da177e4 | 673 | */ |
5e705374 | 674 | static struct request * |
cf7c25cf | 675 | cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last) |
1da177e4 | 676 | { |
cf7c25cf | 677 | sector_t s1, s2, d1 = 0, d2 = 0; |
1da177e4 | 678 | unsigned long back_max; |
e8a99053 AM |
679 | #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ |
680 | #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ | |
681 | unsigned wrap = 0; /* bit mask: requests behind the disk head? */ | |
1da177e4 | 682 | |
5e705374 JA |
683 | if (rq1 == NULL || rq1 == rq2) |
684 | return rq2; | |
685 | if (rq2 == NULL) | |
686 | return rq1; | |
9c2c38a1 | 687 | |
229836bd NK |
688 | if (rq_is_sync(rq1) != rq_is_sync(rq2)) |
689 | return rq_is_sync(rq1) ? rq1 : rq2; | |
690 | ||
65299a3b CH |
691 | if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO) |
692 | return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2; | |
b53d1ed7 | 693 | |
83096ebf TH |
694 | s1 = blk_rq_pos(rq1); |
695 | s2 = blk_rq_pos(rq2); | |
1da177e4 | 696 | |
1da177e4 LT |
697 | /* |
698 | * by definition, 1KiB is 2 sectors | |
699 | */ | |
700 | back_max = cfqd->cfq_back_max * 2; | |
701 | ||
702 | /* | |
703 | * Strict one way elevator _except_ in the case where we allow | |
704 | * short backward seeks which are biased as twice the cost of a | |
705 | * similar forward seek. | |
706 | */ | |
707 | if (s1 >= last) | |
708 | d1 = s1 - last; | |
709 | else if (s1 + back_max >= last) | |
710 | d1 = (last - s1) * cfqd->cfq_back_penalty; | |
711 | else | |
e8a99053 | 712 | wrap |= CFQ_RQ1_WRAP; |
1da177e4 LT |
713 | |
714 | if (s2 >= last) | |
715 | d2 = s2 - last; | |
716 | else if (s2 + back_max >= last) | |
717 | d2 = (last - s2) * cfqd->cfq_back_penalty; | |
718 | else | |
e8a99053 | 719 | wrap |= CFQ_RQ2_WRAP; |
1da177e4 LT |
720 | |
721 | /* Found required data */ | |
e8a99053 AM |
722 | |
723 | /* | |
724 | * By doing switch() on the bit mask "wrap" we avoid having to | |
725 | * check two variables for all permutations: --> faster! | |
726 | */ | |
727 | switch (wrap) { | |
5e705374 | 728 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ |
e8a99053 | 729 | if (d1 < d2) |
5e705374 | 730 | return rq1; |
e8a99053 | 731 | else if (d2 < d1) |
5e705374 | 732 | return rq2; |
e8a99053 AM |
733 | else { |
734 | if (s1 >= s2) | |
5e705374 | 735 | return rq1; |
e8a99053 | 736 | else |
5e705374 | 737 | return rq2; |
e8a99053 | 738 | } |
1da177e4 | 739 | |
e8a99053 | 740 | case CFQ_RQ2_WRAP: |
5e705374 | 741 | return rq1; |
e8a99053 | 742 | case CFQ_RQ1_WRAP: |
5e705374 JA |
743 | return rq2; |
744 | case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ | |
e8a99053 AM |
745 | default: |
746 | /* | |
747 | * Since both rqs are wrapped, | |
748 | * start with the one that's further behind head | |
749 | * (--> only *one* back seek required), | |
750 | * since back seek takes more time than forward. | |
751 | */ | |
752 | if (s1 <= s2) | |
5e705374 | 753 | return rq1; |
1da177e4 | 754 | else |
5e705374 | 755 | return rq2; |
1da177e4 LT |
756 | } |
757 | } | |
758 | ||
498d3aa2 JA |
759 | /* |
760 | * The below is leftmost cache rbtree addon | |
761 | */ | |
0871714e | 762 | static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root) |
cc09e299 | 763 | { |
615f0259 VG |
764 | /* Service tree is empty */ |
765 | if (!root->count) | |
766 | return NULL; | |
767 | ||
cc09e299 JA |
768 | if (!root->left) |
769 | root->left = rb_first(&root->rb); | |
770 | ||
0871714e JA |
771 | if (root->left) |
772 | return rb_entry(root->left, struct cfq_queue, rb_node); | |
773 | ||
774 | return NULL; | |
cc09e299 JA |
775 | } |
776 | ||
1fa8f6d6 VG |
777 | static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root) |
778 | { | |
779 | if (!root->left) | |
780 | root->left = rb_first(&root->rb); | |
781 | ||
782 | if (root->left) | |
783 | return rb_entry_cfqg(root->left); | |
784 | ||
785 | return NULL; | |
786 | } | |
787 | ||
a36e71f9 JA |
788 | static void rb_erase_init(struct rb_node *n, struct rb_root *root) |
789 | { | |
790 | rb_erase(n, root); | |
791 | RB_CLEAR_NODE(n); | |
792 | } | |
793 | ||
cc09e299 JA |
794 | static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) |
795 | { | |
796 | if (root->left == n) | |
797 | root->left = NULL; | |
a36e71f9 | 798 | rb_erase_init(n, &root->rb); |
aa6f6a3d | 799 | --root->count; |
cc09e299 JA |
800 | } |
801 | ||
1da177e4 LT |
802 | /* |
803 | * would be nice to take fifo expire time into account as well | |
804 | */ | |
5e705374 JA |
805 | static struct request * |
806 | cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
807 | struct request *last) | |
1da177e4 | 808 | { |
21183b07 JA |
809 | struct rb_node *rbnext = rb_next(&last->rb_node); |
810 | struct rb_node *rbprev = rb_prev(&last->rb_node); | |
5e705374 | 811 | struct request *next = NULL, *prev = NULL; |
1da177e4 | 812 | |
21183b07 | 813 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
1da177e4 LT |
814 | |
815 | if (rbprev) | |
5e705374 | 816 | prev = rb_entry_rq(rbprev); |
1da177e4 | 817 | |
21183b07 | 818 | if (rbnext) |
5e705374 | 819 | next = rb_entry_rq(rbnext); |
21183b07 JA |
820 | else { |
821 | rbnext = rb_first(&cfqq->sort_list); | |
822 | if (rbnext && rbnext != &last->rb_node) | |
5e705374 | 823 | next = rb_entry_rq(rbnext); |
21183b07 | 824 | } |
1da177e4 | 825 | |
cf7c25cf | 826 | return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last)); |
1da177e4 LT |
827 | } |
828 | ||
d9e7620e JA |
829 | static unsigned long cfq_slice_offset(struct cfq_data *cfqd, |
830 | struct cfq_queue *cfqq) | |
1da177e4 | 831 | { |
d9e7620e JA |
832 | /* |
833 | * just an approximation, should be ok. | |
834 | */ | |
cdb16e8f | 835 | return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) - |
464191c6 | 836 | cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio)); |
d9e7620e JA |
837 | } |
838 | ||
1fa8f6d6 VG |
839 | static inline s64 |
840 | cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg) | |
841 | { | |
842 | return cfqg->vdisktime - st->min_vdisktime; | |
843 | } | |
844 | ||
845 | static void | |
846 | __cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) | |
847 | { | |
848 | struct rb_node **node = &st->rb.rb_node; | |
849 | struct rb_node *parent = NULL; | |
850 | struct cfq_group *__cfqg; | |
851 | s64 key = cfqg_key(st, cfqg); | |
852 | int left = 1; | |
853 | ||
854 | while (*node != NULL) { | |
855 | parent = *node; | |
856 | __cfqg = rb_entry_cfqg(parent); | |
857 | ||
858 | if (key < cfqg_key(st, __cfqg)) | |
859 | node = &parent->rb_left; | |
860 | else { | |
861 | node = &parent->rb_right; | |
862 | left = 0; | |
863 | } | |
864 | } | |
865 | ||
866 | if (left) | |
867 | st->left = &cfqg->rb_node; | |
868 | ||
869 | rb_link_node(&cfqg->rb_node, parent, node); | |
870 | rb_insert_color(&cfqg->rb_node, &st->rb); | |
871 | } | |
872 | ||
873 | static void | |
8184f93e JT |
874 | cfq_update_group_weight(struct cfq_group *cfqg) |
875 | { | |
876 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); | |
877 | if (cfqg->needs_update) { | |
878 | cfqg->weight = cfqg->new_weight; | |
879 | cfqg->needs_update = false; | |
880 | } | |
881 | } | |
882 | ||
883 | static void | |
884 | cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg) | |
885 | { | |
886 | BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node)); | |
887 | ||
888 | cfq_update_group_weight(cfqg); | |
889 | __cfq_group_service_tree_add(st, cfqg); | |
890 | st->total_weight += cfqg->weight; | |
891 | } | |
892 | ||
893 | static void | |
894 | cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg) | |
1fa8f6d6 VG |
895 | { |
896 | struct cfq_rb_root *st = &cfqd->grp_service_tree; | |
897 | struct cfq_group *__cfqg; | |
898 | struct rb_node *n; | |
899 | ||
900 | cfqg->nr_cfqq++; | |
760701bf | 901 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) |
1fa8f6d6 VG |
902 | return; |
903 | ||
904 | /* | |
905 | * Currently put the group at the end. Later implement something | |
906 | * so that groups get lesser vtime based on their weights, so that | |
25985edc | 907 | * if group does not loose all if it was not continuously backlogged. |
1fa8f6d6 VG |
908 | */ |
909 | n = rb_last(&st->rb); | |
910 | if (n) { | |
911 | __cfqg = rb_entry_cfqg(n); | |
912 | cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY; | |
913 | } else | |
914 | cfqg->vdisktime = st->min_vdisktime; | |
8184f93e JT |
915 | cfq_group_service_tree_add(st, cfqg); |
916 | } | |
1fa8f6d6 | 917 | |
8184f93e JT |
918 | static void |
919 | cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg) | |
920 | { | |
921 | st->total_weight -= cfqg->weight; | |
922 | if (!RB_EMPTY_NODE(&cfqg->rb_node)) | |
923 | cfq_rb_erase(&cfqg->rb_node, st); | |
1fa8f6d6 VG |
924 | } |
925 | ||
926 | static void | |
8184f93e | 927 | cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg) |
1fa8f6d6 VG |
928 | { |
929 | struct cfq_rb_root *st = &cfqd->grp_service_tree; | |
930 | ||
931 | BUG_ON(cfqg->nr_cfqq < 1); | |
932 | cfqg->nr_cfqq--; | |
25bc6b07 | 933 | |
1fa8f6d6 VG |
934 | /* If there are other cfq queues under this group, don't delete it */ |
935 | if (cfqg->nr_cfqq) | |
936 | return; | |
937 | ||
2868ef7b | 938 | cfq_log_cfqg(cfqd, cfqg, "del_from_rr group"); |
8184f93e | 939 | cfq_group_service_tree_del(st, cfqg); |
dae739eb | 940 | cfqg->saved_workload_slice = 0; |
c1768268 TH |
941 | cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg), |
942 | &blkio_policy_cfq, 1); | |
dae739eb VG |
943 | } |
944 | ||
167400d3 JT |
945 | static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq, |
946 | unsigned int *unaccounted_time) | |
dae739eb | 947 | { |
f75edf2d | 948 | unsigned int slice_used; |
dae739eb VG |
949 | |
950 | /* | |
951 | * Queue got expired before even a single request completed or | |
952 | * got expired immediately after first request completion. | |
953 | */ | |
954 | if (!cfqq->slice_start || cfqq->slice_start == jiffies) { | |
955 | /* | |
956 | * Also charge the seek time incurred to the group, otherwise | |
957 | * if there are mutiple queues in the group, each can dispatch | |
958 | * a single request on seeky media and cause lots of seek time | |
959 | * and group will never know it. | |
960 | */ | |
961 | slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start), | |
962 | 1); | |
963 | } else { | |
964 | slice_used = jiffies - cfqq->slice_start; | |
167400d3 JT |
965 | if (slice_used > cfqq->allocated_slice) { |
966 | *unaccounted_time = slice_used - cfqq->allocated_slice; | |
f75edf2d | 967 | slice_used = cfqq->allocated_slice; |
167400d3 JT |
968 | } |
969 | if (time_after(cfqq->slice_start, cfqq->dispatch_start)) | |
970 | *unaccounted_time += cfqq->slice_start - | |
971 | cfqq->dispatch_start; | |
dae739eb VG |
972 | } |
973 | ||
dae739eb VG |
974 | return slice_used; |
975 | } | |
976 | ||
977 | static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg, | |
e5ff082e | 978 | struct cfq_queue *cfqq) |
dae739eb VG |
979 | { |
980 | struct cfq_rb_root *st = &cfqd->grp_service_tree; | |
167400d3 | 981 | unsigned int used_sl, charge, unaccounted_sl = 0; |
f26bd1f0 VG |
982 | int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg) |
983 | - cfqg->service_tree_idle.count; | |
984 | ||
985 | BUG_ON(nr_sync < 0); | |
167400d3 | 986 | used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl); |
dae739eb | 987 | |
02b35081 VG |
988 | if (iops_mode(cfqd)) |
989 | charge = cfqq->slice_dispatch; | |
990 | else if (!cfq_cfqq_sync(cfqq) && !nr_sync) | |
991 | charge = cfqq->allocated_slice; | |
dae739eb VG |
992 | |
993 | /* Can't update vdisktime while group is on service tree */ | |
8184f93e | 994 | cfq_group_service_tree_del(st, cfqg); |
02b35081 | 995 | cfqg->vdisktime += cfq_scale_slice(charge, cfqg); |
8184f93e JT |
996 | /* If a new weight was requested, update now, off tree */ |
997 | cfq_group_service_tree_add(st, cfqg); | |
dae739eb VG |
998 | |
999 | /* This group is being expired. Save the context */ | |
1000 | if (time_after(cfqd->workload_expires, jiffies)) { | |
1001 | cfqg->saved_workload_slice = cfqd->workload_expires | |
1002 | - jiffies; | |
1003 | cfqg->saved_workload = cfqd->serving_type; | |
1004 | cfqg->saved_serving_prio = cfqd->serving_prio; | |
1005 | } else | |
1006 | cfqg->saved_workload_slice = 0; | |
2868ef7b VG |
1007 | |
1008 | cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime, | |
1009 | st->min_vdisktime); | |
fd16d263 JP |
1010 | cfq_log_cfqq(cfqq->cfqd, cfqq, |
1011 | "sl_used=%u disp=%u charge=%u iops=%u sect=%lu", | |
1012 | used_sl, cfqq->slice_dispatch, charge, | |
1013 | iops_mode(cfqd), cfqq->nr_sectors); | |
c1768268 TH |
1014 | cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), &blkio_policy_cfq, |
1015 | used_sl, unaccounted_sl); | |
1016 | cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg), &blkio_policy_cfq); | |
1fa8f6d6 VG |
1017 | } |
1018 | ||
f51b802c TH |
1019 | /** |
1020 | * cfq_init_cfqg_base - initialize base part of a cfq_group | |
1021 | * @cfqg: cfq_group to initialize | |
1022 | * | |
1023 | * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED | |
1024 | * is enabled or not. | |
1025 | */ | |
1026 | static void cfq_init_cfqg_base(struct cfq_group *cfqg) | |
1027 | { | |
1028 | struct cfq_rb_root *st; | |
1029 | int i, j; | |
1030 | ||
1031 | for_each_cfqg_st(cfqg, i, j, st) | |
1032 | *st = CFQ_RB_ROOT; | |
1033 | RB_CLEAR_NODE(&cfqg->rb_node); | |
1034 | ||
1035 | cfqg->ttime.last_end_request = jiffies; | |
1036 | } | |
1037 | ||
25fb5169 | 1038 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
ca32aefc TH |
1039 | static void cfq_update_blkio_group_weight(struct request_queue *q, |
1040 | struct blkio_group *blkg, | |
8aea4545 | 1041 | unsigned int weight) |
f8d461d6 | 1042 | { |
0381411e TH |
1043 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
1044 | ||
8184f93e JT |
1045 | cfqg->new_weight = weight; |
1046 | cfqg->needs_update = true; | |
f8d461d6 VG |
1047 | } |
1048 | ||
0381411e | 1049 | static void cfq_init_blkio_group(struct blkio_group *blkg) |
f469a7b4 | 1050 | { |
0381411e | 1051 | struct cfq_group *cfqg = blkg_to_cfqg(blkg); |
25fb5169 | 1052 | |
f51b802c | 1053 | cfq_init_cfqg_base(cfqg); |
0381411e | 1054 | cfqg->weight = blkg->blkcg->weight; |
25fb5169 VG |
1055 | } |
1056 | ||
1057 | /* | |
3e59cf9d VG |
1058 | * Search for the cfq group current task belongs to. request_queue lock must |
1059 | * be held. | |
25fb5169 | 1060 | */ |
cd1604fa TH |
1061 | static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd, |
1062 | struct blkio_cgroup *blkcg) | |
25fb5169 | 1063 | { |
f469a7b4 | 1064 | struct request_queue *q = cfqd->queue; |
cd1604fa | 1065 | struct cfq_group *cfqg = NULL; |
25fb5169 | 1066 | |
cd1604fa TH |
1067 | /* avoid lookup for the common case where there's no blkio cgroup */ |
1068 | if (blkcg == &blkio_root_cgroup) { | |
1069 | cfqg = cfqd->root_group; | |
1070 | } else { | |
1071 | struct blkio_group *blkg; | |
f469a7b4 | 1072 | |
cd1604fa TH |
1073 | blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_PROP, false); |
1074 | if (!IS_ERR(blkg)) | |
0381411e | 1075 | cfqg = blkg_to_cfqg(blkg); |
cd1604fa | 1076 | } |
f469a7b4 | 1077 | |
25fb5169 VG |
1078 | return cfqg; |
1079 | } | |
1080 | ||
1081 | static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) | |
1082 | { | |
1083 | /* Currently, all async queues are mapped to root group */ | |
1084 | if (!cfq_cfqq_sync(cfqq)) | |
f51b802c | 1085 | cfqg = cfqq->cfqd->root_group; |
25fb5169 VG |
1086 | |
1087 | cfqq->cfqg = cfqg; | |
b1c35769 | 1088 | /* cfqq reference on cfqg */ |
1adaf3dd | 1089 | blkg_get(cfqg_to_blkg(cfqg)); |
b1c35769 VG |
1090 | } |
1091 | ||
25fb5169 | 1092 | #else /* GROUP_IOSCHED */ |
cd1604fa TH |
1093 | static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd, |
1094 | struct blkio_cgroup *blkcg) | |
25fb5169 | 1095 | { |
f51b802c | 1096 | return cfqd->root_group; |
25fb5169 | 1097 | } |
7f1dc8a2 | 1098 | |
25fb5169 VG |
1099 | static inline void |
1100 | cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) { | |
1101 | cfqq->cfqg = cfqg; | |
1102 | } | |
1103 | ||
1104 | #endif /* GROUP_IOSCHED */ | |
1105 | ||
498d3aa2 | 1106 | /* |
c0324a02 | 1107 | * The cfqd->service_trees holds all pending cfq_queue's that have |
498d3aa2 JA |
1108 | * requests waiting to be processed. It is sorted in the order that |
1109 | * we will service the queues. | |
1110 | */ | |
a36e71f9 | 1111 | static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
a6151c3a | 1112 | bool add_front) |
d9e7620e | 1113 | { |
0871714e JA |
1114 | struct rb_node **p, *parent; |
1115 | struct cfq_queue *__cfqq; | |
d9e7620e | 1116 | unsigned long rb_key; |
c0324a02 | 1117 | struct cfq_rb_root *service_tree; |
498d3aa2 | 1118 | int left; |
dae739eb | 1119 | int new_cfqq = 1; |
ae30c286 | 1120 | |
cdb16e8f | 1121 | service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq), |
65b32a57 | 1122 | cfqq_type(cfqq)); |
0871714e JA |
1123 | if (cfq_class_idle(cfqq)) { |
1124 | rb_key = CFQ_IDLE_DELAY; | |
aa6f6a3d | 1125 | parent = rb_last(&service_tree->rb); |
0871714e JA |
1126 | if (parent && parent != &cfqq->rb_node) { |
1127 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); | |
1128 | rb_key += __cfqq->rb_key; | |
1129 | } else | |
1130 | rb_key += jiffies; | |
1131 | } else if (!add_front) { | |
b9c8946b JA |
1132 | /* |
1133 | * Get our rb key offset. Subtract any residual slice | |
1134 | * value carried from last service. A negative resid | |
1135 | * count indicates slice overrun, and this should position | |
1136 | * the next service time further away in the tree. | |
1137 | */ | |
edd75ffd | 1138 | rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies; |
b9c8946b | 1139 | rb_key -= cfqq->slice_resid; |
edd75ffd | 1140 | cfqq->slice_resid = 0; |
48e025e6 CZ |
1141 | } else { |
1142 | rb_key = -HZ; | |
aa6f6a3d | 1143 | __cfqq = cfq_rb_first(service_tree); |
48e025e6 CZ |
1144 | rb_key += __cfqq ? __cfqq->rb_key : jiffies; |
1145 | } | |
1da177e4 | 1146 | |
d9e7620e | 1147 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
dae739eb | 1148 | new_cfqq = 0; |
99f9628a | 1149 | /* |
d9e7620e | 1150 | * same position, nothing more to do |
99f9628a | 1151 | */ |
c0324a02 CZ |
1152 | if (rb_key == cfqq->rb_key && |
1153 | cfqq->service_tree == service_tree) | |
d9e7620e | 1154 | return; |
1da177e4 | 1155 | |
aa6f6a3d CZ |
1156 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); |
1157 | cfqq->service_tree = NULL; | |
1da177e4 | 1158 | } |
d9e7620e | 1159 | |
498d3aa2 | 1160 | left = 1; |
0871714e | 1161 | parent = NULL; |
aa6f6a3d CZ |
1162 | cfqq->service_tree = service_tree; |
1163 | p = &service_tree->rb.rb_node; | |
d9e7620e | 1164 | while (*p) { |
67060e37 | 1165 | struct rb_node **n; |
cc09e299 | 1166 | |
d9e7620e JA |
1167 | parent = *p; |
1168 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); | |
1169 | ||
0c534e0a | 1170 | /* |
c0324a02 | 1171 | * sort by key, that represents service time. |
0c534e0a | 1172 | */ |
c0324a02 | 1173 | if (time_before(rb_key, __cfqq->rb_key)) |
67060e37 | 1174 | n = &(*p)->rb_left; |
c0324a02 | 1175 | else { |
67060e37 | 1176 | n = &(*p)->rb_right; |
cc09e299 | 1177 | left = 0; |
c0324a02 | 1178 | } |
67060e37 JA |
1179 | |
1180 | p = n; | |
d9e7620e JA |
1181 | } |
1182 | ||
cc09e299 | 1183 | if (left) |
aa6f6a3d | 1184 | service_tree->left = &cfqq->rb_node; |
cc09e299 | 1185 | |
d9e7620e JA |
1186 | cfqq->rb_key = rb_key; |
1187 | rb_link_node(&cfqq->rb_node, parent, p); | |
aa6f6a3d CZ |
1188 | rb_insert_color(&cfqq->rb_node, &service_tree->rb); |
1189 | service_tree->count++; | |
20359f27 | 1190 | if (add_front || !new_cfqq) |
dae739eb | 1191 | return; |
8184f93e | 1192 | cfq_group_notify_queue_add(cfqd, cfqq->cfqg); |
1da177e4 LT |
1193 | } |
1194 | ||
a36e71f9 | 1195 | static struct cfq_queue * |
f2d1f0ae JA |
1196 | cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root, |
1197 | sector_t sector, struct rb_node **ret_parent, | |
1198 | struct rb_node ***rb_link) | |
a36e71f9 | 1199 | { |
a36e71f9 JA |
1200 | struct rb_node **p, *parent; |
1201 | struct cfq_queue *cfqq = NULL; | |
1202 | ||
1203 | parent = NULL; | |
1204 | p = &root->rb_node; | |
1205 | while (*p) { | |
1206 | struct rb_node **n; | |
1207 | ||
1208 | parent = *p; | |
1209 | cfqq = rb_entry(parent, struct cfq_queue, p_node); | |
1210 | ||
1211 | /* | |
1212 | * Sort strictly based on sector. Smallest to the left, | |
1213 | * largest to the right. | |
1214 | */ | |
2e46e8b2 | 1215 | if (sector > blk_rq_pos(cfqq->next_rq)) |
a36e71f9 | 1216 | n = &(*p)->rb_right; |
2e46e8b2 | 1217 | else if (sector < blk_rq_pos(cfqq->next_rq)) |
a36e71f9 JA |
1218 | n = &(*p)->rb_left; |
1219 | else | |
1220 | break; | |
1221 | p = n; | |
3ac6c9f8 | 1222 | cfqq = NULL; |
a36e71f9 JA |
1223 | } |
1224 | ||
1225 | *ret_parent = parent; | |
1226 | if (rb_link) | |
1227 | *rb_link = p; | |
3ac6c9f8 | 1228 | return cfqq; |
a36e71f9 JA |
1229 | } |
1230 | ||
1231 | static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
1232 | { | |
a36e71f9 JA |
1233 | struct rb_node **p, *parent; |
1234 | struct cfq_queue *__cfqq; | |
1235 | ||
f2d1f0ae JA |
1236 | if (cfqq->p_root) { |
1237 | rb_erase(&cfqq->p_node, cfqq->p_root); | |
1238 | cfqq->p_root = NULL; | |
1239 | } | |
a36e71f9 JA |
1240 | |
1241 | if (cfq_class_idle(cfqq)) | |
1242 | return; | |
1243 | if (!cfqq->next_rq) | |
1244 | return; | |
1245 | ||
f2d1f0ae | 1246 | cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio]; |
2e46e8b2 TH |
1247 | __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root, |
1248 | blk_rq_pos(cfqq->next_rq), &parent, &p); | |
3ac6c9f8 JA |
1249 | if (!__cfqq) { |
1250 | rb_link_node(&cfqq->p_node, parent, p); | |
f2d1f0ae JA |
1251 | rb_insert_color(&cfqq->p_node, cfqq->p_root); |
1252 | } else | |
1253 | cfqq->p_root = NULL; | |
a36e71f9 JA |
1254 | } |
1255 | ||
498d3aa2 JA |
1256 | /* |
1257 | * Update cfqq's position in the service tree. | |
1258 | */ | |
edd75ffd | 1259 | static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
6d048f53 | 1260 | { |
6d048f53 JA |
1261 | /* |
1262 | * Resorting requires the cfqq to be on the RR list already. | |
1263 | */ | |
a36e71f9 | 1264 | if (cfq_cfqq_on_rr(cfqq)) { |
edd75ffd | 1265 | cfq_service_tree_add(cfqd, cfqq, 0); |
a36e71f9 JA |
1266 | cfq_prio_tree_add(cfqd, cfqq); |
1267 | } | |
6d048f53 JA |
1268 | } |
1269 | ||
1da177e4 LT |
1270 | /* |
1271 | * add to busy list of queues for service, trying to be fair in ordering | |
22e2c507 | 1272 | * the pending list according to last request service |
1da177e4 | 1273 | */ |
febffd61 | 1274 | static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
1da177e4 | 1275 | { |
7b679138 | 1276 | cfq_log_cfqq(cfqd, cfqq, "add_to_rr"); |
3b18152c JA |
1277 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
1278 | cfq_mark_cfqq_on_rr(cfqq); | |
1da177e4 | 1279 | cfqd->busy_queues++; |
ef8a41df SL |
1280 | if (cfq_cfqq_sync(cfqq)) |
1281 | cfqd->busy_sync_queues++; | |
1da177e4 | 1282 | |
edd75ffd | 1283 | cfq_resort_rr_list(cfqd, cfqq); |
1da177e4 LT |
1284 | } |
1285 | ||
498d3aa2 JA |
1286 | /* |
1287 | * Called when the cfqq no longer has requests pending, remove it from | |
1288 | * the service tree. | |
1289 | */ | |
febffd61 | 1290 | static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
1da177e4 | 1291 | { |
7b679138 | 1292 | cfq_log_cfqq(cfqd, cfqq, "del_from_rr"); |
3b18152c JA |
1293 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
1294 | cfq_clear_cfqq_on_rr(cfqq); | |
1da177e4 | 1295 | |
aa6f6a3d CZ |
1296 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
1297 | cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree); | |
1298 | cfqq->service_tree = NULL; | |
1299 | } | |
f2d1f0ae JA |
1300 | if (cfqq->p_root) { |
1301 | rb_erase(&cfqq->p_node, cfqq->p_root); | |
1302 | cfqq->p_root = NULL; | |
1303 | } | |
d9e7620e | 1304 | |
8184f93e | 1305 | cfq_group_notify_queue_del(cfqd, cfqq->cfqg); |
1da177e4 LT |
1306 | BUG_ON(!cfqd->busy_queues); |
1307 | cfqd->busy_queues--; | |
ef8a41df SL |
1308 | if (cfq_cfqq_sync(cfqq)) |
1309 | cfqd->busy_sync_queues--; | |
1da177e4 LT |
1310 | } |
1311 | ||
1312 | /* | |
1313 | * rb tree support functions | |
1314 | */ | |
febffd61 | 1315 | static void cfq_del_rq_rb(struct request *rq) |
1da177e4 | 1316 | { |
5e705374 | 1317 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
5e705374 | 1318 | const int sync = rq_is_sync(rq); |
1da177e4 | 1319 | |
b4878f24 JA |
1320 | BUG_ON(!cfqq->queued[sync]); |
1321 | cfqq->queued[sync]--; | |
1da177e4 | 1322 | |
5e705374 | 1323 | elv_rb_del(&cfqq->sort_list, rq); |
1da177e4 | 1324 | |
f04a6424 VG |
1325 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) { |
1326 | /* | |
1327 | * Queue will be deleted from service tree when we actually | |
1328 | * expire it later. Right now just remove it from prio tree | |
1329 | * as it is empty. | |
1330 | */ | |
1331 | if (cfqq->p_root) { | |
1332 | rb_erase(&cfqq->p_node, cfqq->p_root); | |
1333 | cfqq->p_root = NULL; | |
1334 | } | |
1335 | } | |
1da177e4 LT |
1336 | } |
1337 | ||
5e705374 | 1338 | static void cfq_add_rq_rb(struct request *rq) |
1da177e4 | 1339 | { |
5e705374 | 1340 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
1da177e4 | 1341 | struct cfq_data *cfqd = cfqq->cfqd; |
796d5116 | 1342 | struct request *prev; |
1da177e4 | 1343 | |
5380a101 | 1344 | cfqq->queued[rq_is_sync(rq)]++; |
1da177e4 | 1345 | |
796d5116 | 1346 | elv_rb_add(&cfqq->sort_list, rq); |
5fccbf61 JA |
1347 | |
1348 | if (!cfq_cfqq_on_rr(cfqq)) | |
1349 | cfq_add_cfqq_rr(cfqd, cfqq); | |
5044eed4 JA |
1350 | |
1351 | /* | |
1352 | * check if this request is a better next-serve candidate | |
1353 | */ | |
a36e71f9 | 1354 | prev = cfqq->next_rq; |
cf7c25cf | 1355 | cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position); |
a36e71f9 JA |
1356 | |
1357 | /* | |
1358 | * adjust priority tree position, if ->next_rq changes | |
1359 | */ | |
1360 | if (prev != cfqq->next_rq) | |
1361 | cfq_prio_tree_add(cfqd, cfqq); | |
1362 | ||
5044eed4 | 1363 | BUG_ON(!cfqq->next_rq); |
1da177e4 LT |
1364 | } |
1365 | ||
febffd61 | 1366 | static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) |
1da177e4 | 1367 | { |
5380a101 JA |
1368 | elv_rb_del(&cfqq->sort_list, rq); |
1369 | cfqq->queued[rq_is_sync(rq)]--; | |
0381411e | 1370 | cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)), |
c1768268 TH |
1371 | &blkio_policy_cfq, rq_data_dir(rq), |
1372 | rq_is_sync(rq)); | |
5e705374 | 1373 | cfq_add_rq_rb(rq); |
0381411e | 1374 | cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)), |
c1768268 | 1375 | &blkio_policy_cfq, |
0381411e TH |
1376 | cfqg_to_blkg(cfqq->cfqd->serving_group), |
1377 | rq_data_dir(rq), rq_is_sync(rq)); | |
1da177e4 LT |
1378 | } |
1379 | ||
206dc69b JA |
1380 | static struct request * |
1381 | cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) | |
1da177e4 | 1382 | { |
206dc69b | 1383 | struct task_struct *tsk = current; |
c5869807 | 1384 | struct cfq_io_cq *cic; |
206dc69b | 1385 | struct cfq_queue *cfqq; |
1da177e4 | 1386 | |
4ac845a2 | 1387 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
91fac317 VT |
1388 | if (!cic) |
1389 | return NULL; | |
1390 | ||
1391 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); | |
89850f7e JA |
1392 | if (cfqq) { |
1393 | sector_t sector = bio->bi_sector + bio_sectors(bio); | |
1394 | ||
21183b07 | 1395 | return elv_rb_find(&cfqq->sort_list, sector); |
89850f7e | 1396 | } |
1da177e4 | 1397 | |
1da177e4 LT |
1398 | return NULL; |
1399 | } | |
1400 | ||
165125e1 | 1401 | static void cfq_activate_request(struct request_queue *q, struct request *rq) |
1da177e4 | 1402 | { |
22e2c507 | 1403 | struct cfq_data *cfqd = q->elevator->elevator_data; |
3b18152c | 1404 | |
53c583d2 | 1405 | cfqd->rq_in_driver++; |
7b679138 | 1406 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d", |
53c583d2 | 1407 | cfqd->rq_in_driver); |
25776e35 | 1408 | |
5b93629b | 1409 | cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); |
1da177e4 LT |
1410 | } |
1411 | ||
165125e1 | 1412 | static void cfq_deactivate_request(struct request_queue *q, struct request *rq) |
1da177e4 | 1413 | { |
b4878f24 JA |
1414 | struct cfq_data *cfqd = q->elevator->elevator_data; |
1415 | ||
53c583d2 CZ |
1416 | WARN_ON(!cfqd->rq_in_driver); |
1417 | cfqd->rq_in_driver--; | |
7b679138 | 1418 | cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d", |
53c583d2 | 1419 | cfqd->rq_in_driver); |
1da177e4 LT |
1420 | } |
1421 | ||
b4878f24 | 1422 | static void cfq_remove_request(struct request *rq) |
1da177e4 | 1423 | { |
5e705374 | 1424 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
21183b07 | 1425 | |
5e705374 JA |
1426 | if (cfqq->next_rq == rq) |
1427 | cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); | |
1da177e4 | 1428 | |
b4878f24 | 1429 | list_del_init(&rq->queuelist); |
5e705374 | 1430 | cfq_del_rq_rb(rq); |
374f84ac | 1431 | |
45333d5a | 1432 | cfqq->cfqd->rq_queued--; |
0381411e | 1433 | cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)), |
c1768268 TH |
1434 | &blkio_policy_cfq, rq_data_dir(rq), |
1435 | rq_is_sync(rq)); | |
65299a3b CH |
1436 | if (rq->cmd_flags & REQ_PRIO) { |
1437 | WARN_ON(!cfqq->prio_pending); | |
1438 | cfqq->prio_pending--; | |
b53d1ed7 | 1439 | } |
1da177e4 LT |
1440 | } |
1441 | ||
165125e1 JA |
1442 | static int cfq_merge(struct request_queue *q, struct request **req, |
1443 | struct bio *bio) | |
1da177e4 LT |
1444 | { |
1445 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
1446 | struct request *__rq; | |
1da177e4 | 1447 | |
206dc69b | 1448 | __rq = cfq_find_rq_fmerge(cfqd, bio); |
22e2c507 | 1449 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
9817064b JA |
1450 | *req = __rq; |
1451 | return ELEVATOR_FRONT_MERGE; | |
1da177e4 LT |
1452 | } |
1453 | ||
1454 | return ELEVATOR_NO_MERGE; | |
1da177e4 LT |
1455 | } |
1456 | ||
165125e1 | 1457 | static void cfq_merged_request(struct request_queue *q, struct request *req, |
21183b07 | 1458 | int type) |
1da177e4 | 1459 | { |
21183b07 | 1460 | if (type == ELEVATOR_FRONT_MERGE) { |
5e705374 | 1461 | struct cfq_queue *cfqq = RQ_CFQQ(req); |
1da177e4 | 1462 | |
5e705374 | 1463 | cfq_reposition_rq_rb(cfqq, req); |
1da177e4 | 1464 | } |
1da177e4 LT |
1465 | } |
1466 | ||
812d4026 DS |
1467 | static void cfq_bio_merged(struct request_queue *q, struct request *req, |
1468 | struct bio *bio) | |
1469 | { | |
0381411e | 1470 | cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)), |
c1768268 TH |
1471 | &blkio_policy_cfq, bio_data_dir(bio), |
1472 | cfq_bio_sync(bio)); | |
812d4026 DS |
1473 | } |
1474 | ||
1da177e4 | 1475 | static void |
165125e1 | 1476 | cfq_merged_requests(struct request_queue *q, struct request *rq, |
1da177e4 LT |
1477 | struct request *next) |
1478 | { | |
cf7c25cf | 1479 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
4a0b75c7 SL |
1480 | struct cfq_data *cfqd = q->elevator->elevator_data; |
1481 | ||
22e2c507 JA |
1482 | /* |
1483 | * reposition in fifo if next is older than rq | |
1484 | */ | |
1485 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && | |
30996f40 | 1486 | time_before(rq_fifo_time(next), rq_fifo_time(rq))) { |
22e2c507 | 1487 | list_move(&rq->queuelist, &next->queuelist); |
30996f40 JA |
1488 | rq_set_fifo_time(rq, rq_fifo_time(next)); |
1489 | } | |
22e2c507 | 1490 | |
cf7c25cf CZ |
1491 | if (cfqq->next_rq == next) |
1492 | cfqq->next_rq = rq; | |
b4878f24 | 1493 | cfq_remove_request(next); |
0381411e | 1494 | cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)), |
c1768268 TH |
1495 | &blkio_policy_cfq, rq_data_dir(next), |
1496 | rq_is_sync(next)); | |
4a0b75c7 SL |
1497 | |
1498 | cfqq = RQ_CFQQ(next); | |
1499 | /* | |
1500 | * all requests of this queue are merged to other queues, delete it | |
1501 | * from the service tree. If it's the active_queue, | |
1502 | * cfq_dispatch_requests() will choose to expire it or do idle | |
1503 | */ | |
1504 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) && | |
1505 | cfqq != cfqd->active_queue) | |
1506 | cfq_del_cfqq_rr(cfqd, cfqq); | |
22e2c507 JA |
1507 | } |
1508 | ||
165125e1 | 1509 | static int cfq_allow_merge(struct request_queue *q, struct request *rq, |
da775265 JA |
1510 | struct bio *bio) |
1511 | { | |
1512 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
c5869807 | 1513 | struct cfq_io_cq *cic; |
da775265 | 1514 | struct cfq_queue *cfqq; |
da775265 JA |
1515 | |
1516 | /* | |
ec8acb69 | 1517 | * Disallow merge of a sync bio into an async request. |
da775265 | 1518 | */ |
91fac317 | 1519 | if (cfq_bio_sync(bio) && !rq_is_sync(rq)) |
a6151c3a | 1520 | return false; |
da775265 JA |
1521 | |
1522 | /* | |
f1a4f4d3 | 1523 | * Lookup the cfqq that this bio will be queued with and allow |
07c2bd37 | 1524 | * merge only if rq is queued there. |
f1a4f4d3 | 1525 | */ |
07c2bd37 TH |
1526 | cic = cfq_cic_lookup(cfqd, current->io_context); |
1527 | if (!cic) | |
1528 | return false; | |
719d3402 | 1529 | |
91fac317 | 1530 | cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio)); |
a6151c3a | 1531 | return cfqq == RQ_CFQQ(rq); |
da775265 JA |
1532 | } |
1533 | ||
812df48d DS |
1534 | static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
1535 | { | |
1536 | del_timer(&cfqd->idle_slice_timer); | |
c1768268 TH |
1537 | cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg), |
1538 | &blkio_policy_cfq); | |
812df48d DS |
1539 | } |
1540 | ||
febffd61 JA |
1541 | static void __cfq_set_active_queue(struct cfq_data *cfqd, |
1542 | struct cfq_queue *cfqq) | |
22e2c507 JA |
1543 | { |
1544 | if (cfqq) { | |
b1ffe737 DS |
1545 | cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d", |
1546 | cfqd->serving_prio, cfqd->serving_type); | |
c1768268 TH |
1547 | cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg), |
1548 | &blkio_policy_cfq); | |
62a37f6b JT |
1549 | cfqq->slice_start = 0; |
1550 | cfqq->dispatch_start = jiffies; | |
1551 | cfqq->allocated_slice = 0; | |
1552 | cfqq->slice_end = 0; | |
1553 | cfqq->slice_dispatch = 0; | |
1554 | cfqq->nr_sectors = 0; | |
1555 | ||
1556 | cfq_clear_cfqq_wait_request(cfqq); | |
1557 | cfq_clear_cfqq_must_dispatch(cfqq); | |
1558 | cfq_clear_cfqq_must_alloc_slice(cfqq); | |
1559 | cfq_clear_cfqq_fifo_expire(cfqq); | |
1560 | cfq_mark_cfqq_slice_new(cfqq); | |
1561 | ||
1562 | cfq_del_timer(cfqd, cfqq); | |
22e2c507 JA |
1563 | } |
1564 | ||
1565 | cfqd->active_queue = cfqq; | |
1566 | } | |
1567 | ||
7b14e3b5 JA |
1568 | /* |
1569 | * current cfqq expired its slice (or was too idle), select new one | |
1570 | */ | |
1571 | static void | |
1572 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
e5ff082e | 1573 | bool timed_out) |
7b14e3b5 | 1574 | { |
7b679138 JA |
1575 | cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out); |
1576 | ||
7b14e3b5 | 1577 | if (cfq_cfqq_wait_request(cfqq)) |
812df48d | 1578 | cfq_del_timer(cfqd, cfqq); |
7b14e3b5 | 1579 | |
7b14e3b5 | 1580 | cfq_clear_cfqq_wait_request(cfqq); |
f75edf2d | 1581 | cfq_clear_cfqq_wait_busy(cfqq); |
7b14e3b5 | 1582 | |
ae54abed SL |
1583 | /* |
1584 | * If this cfqq is shared between multiple processes, check to | |
1585 | * make sure that those processes are still issuing I/Os within | |
1586 | * the mean seek distance. If not, it may be time to break the | |
1587 | * queues apart again. | |
1588 | */ | |
1589 | if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq)) | |
1590 | cfq_mark_cfqq_split_coop(cfqq); | |
1591 | ||
7b14e3b5 | 1592 | /* |
6084cdda | 1593 | * store what was left of this slice, if the queue idled/timed out |
7b14e3b5 | 1594 | */ |
c553f8e3 SL |
1595 | if (timed_out) { |
1596 | if (cfq_cfqq_slice_new(cfqq)) | |
ba5bd520 | 1597 | cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq); |
c553f8e3 SL |
1598 | else |
1599 | cfqq->slice_resid = cfqq->slice_end - jiffies; | |
7b679138 JA |
1600 | cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid); |
1601 | } | |
7b14e3b5 | 1602 | |
e5ff082e | 1603 | cfq_group_served(cfqd, cfqq->cfqg, cfqq); |
dae739eb | 1604 | |
f04a6424 VG |
1605 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) |
1606 | cfq_del_cfqq_rr(cfqd, cfqq); | |
1607 | ||
edd75ffd | 1608 | cfq_resort_rr_list(cfqd, cfqq); |
7b14e3b5 JA |
1609 | |
1610 | if (cfqq == cfqd->active_queue) | |
1611 | cfqd->active_queue = NULL; | |
1612 | ||
1613 | if (cfqd->active_cic) { | |
11a3122f | 1614 | put_io_context(cfqd->active_cic->icq.ioc); |
7b14e3b5 JA |
1615 | cfqd->active_cic = NULL; |
1616 | } | |
7b14e3b5 JA |
1617 | } |
1618 | ||
e5ff082e | 1619 | static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out) |
7b14e3b5 JA |
1620 | { |
1621 | struct cfq_queue *cfqq = cfqd->active_queue; | |
1622 | ||
1623 | if (cfqq) | |
e5ff082e | 1624 | __cfq_slice_expired(cfqd, cfqq, timed_out); |
7b14e3b5 JA |
1625 | } |
1626 | ||
498d3aa2 JA |
1627 | /* |
1628 | * Get next queue for service. Unless we have a queue preemption, | |
1629 | * we'll simply select the first cfqq in the service tree. | |
1630 | */ | |
6d048f53 | 1631 | static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) |
22e2c507 | 1632 | { |
c0324a02 | 1633 | struct cfq_rb_root *service_tree = |
cdb16e8f | 1634 | service_tree_for(cfqd->serving_group, cfqd->serving_prio, |
65b32a57 | 1635 | cfqd->serving_type); |
d9e7620e | 1636 | |
f04a6424 VG |
1637 | if (!cfqd->rq_queued) |
1638 | return NULL; | |
1639 | ||
1fa8f6d6 VG |
1640 | /* There is nothing to dispatch */ |
1641 | if (!service_tree) | |
1642 | return NULL; | |
c0324a02 CZ |
1643 | if (RB_EMPTY_ROOT(&service_tree->rb)) |
1644 | return NULL; | |
1645 | return cfq_rb_first(service_tree); | |
6d048f53 JA |
1646 | } |
1647 | ||
f04a6424 VG |
1648 | static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd) |
1649 | { | |
25fb5169 | 1650 | struct cfq_group *cfqg; |
f04a6424 VG |
1651 | struct cfq_queue *cfqq; |
1652 | int i, j; | |
1653 | struct cfq_rb_root *st; | |
1654 | ||
1655 | if (!cfqd->rq_queued) | |
1656 | return NULL; | |
1657 | ||
25fb5169 VG |
1658 | cfqg = cfq_get_next_cfqg(cfqd); |
1659 | if (!cfqg) | |
1660 | return NULL; | |
1661 | ||
f04a6424 VG |
1662 | for_each_cfqg_st(cfqg, i, j, st) |
1663 | if ((cfqq = cfq_rb_first(st)) != NULL) | |
1664 | return cfqq; | |
1665 | return NULL; | |
1666 | } | |
1667 | ||
498d3aa2 JA |
1668 | /* |
1669 | * Get and set a new active queue for service. | |
1670 | */ | |
a36e71f9 JA |
1671 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd, |
1672 | struct cfq_queue *cfqq) | |
6d048f53 | 1673 | { |
e00ef799 | 1674 | if (!cfqq) |
a36e71f9 | 1675 | cfqq = cfq_get_next_queue(cfqd); |
6d048f53 | 1676 | |
22e2c507 | 1677 | __cfq_set_active_queue(cfqd, cfqq); |
3b18152c | 1678 | return cfqq; |
22e2c507 JA |
1679 | } |
1680 | ||
d9e7620e JA |
1681 | static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, |
1682 | struct request *rq) | |
1683 | { | |
83096ebf TH |
1684 | if (blk_rq_pos(rq) >= cfqd->last_position) |
1685 | return blk_rq_pos(rq) - cfqd->last_position; | |
d9e7620e | 1686 | else |
83096ebf | 1687 | return cfqd->last_position - blk_rq_pos(rq); |
d9e7620e JA |
1688 | } |
1689 | ||
b2c18e1e | 1690 | static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
e9ce335d | 1691 | struct request *rq) |
6d048f53 | 1692 | { |
e9ce335d | 1693 | return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR; |
6d048f53 JA |
1694 | } |
1695 | ||
a36e71f9 JA |
1696 | static struct cfq_queue *cfqq_close(struct cfq_data *cfqd, |
1697 | struct cfq_queue *cur_cfqq) | |
1698 | { | |
f2d1f0ae | 1699 | struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio]; |
a36e71f9 JA |
1700 | struct rb_node *parent, *node; |
1701 | struct cfq_queue *__cfqq; | |
1702 | sector_t sector = cfqd->last_position; | |
1703 | ||
1704 | if (RB_EMPTY_ROOT(root)) | |
1705 | return NULL; | |
1706 | ||
1707 | /* | |
1708 | * First, if we find a request starting at the end of the last | |
1709 | * request, choose it. | |
1710 | */ | |
f2d1f0ae | 1711 | __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL); |
a36e71f9 JA |
1712 | if (__cfqq) |
1713 | return __cfqq; | |
1714 | ||
1715 | /* | |
1716 | * If the exact sector wasn't found, the parent of the NULL leaf | |
1717 | * will contain the closest sector. | |
1718 | */ | |
1719 | __cfqq = rb_entry(parent, struct cfq_queue, p_node); | |
e9ce335d | 1720 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
a36e71f9 JA |
1721 | return __cfqq; |
1722 | ||
2e46e8b2 | 1723 | if (blk_rq_pos(__cfqq->next_rq) < sector) |
a36e71f9 JA |
1724 | node = rb_next(&__cfqq->p_node); |
1725 | else | |
1726 | node = rb_prev(&__cfqq->p_node); | |
1727 | if (!node) | |
1728 | return NULL; | |
1729 | ||
1730 | __cfqq = rb_entry(node, struct cfq_queue, p_node); | |
e9ce335d | 1731 | if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq)) |
a36e71f9 JA |
1732 | return __cfqq; |
1733 | ||
1734 | return NULL; | |
1735 | } | |
1736 | ||
1737 | /* | |
1738 | * cfqd - obvious | |
1739 | * cur_cfqq - passed in so that we don't decide that the current queue is | |
1740 | * closely cooperating with itself. | |
1741 | * | |
1742 | * So, basically we're assuming that that cur_cfqq has dispatched at least | |
1743 | * one request, and that cfqd->last_position reflects a position on the disk | |
1744 | * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid | |
1745 | * assumption. | |
1746 | */ | |
1747 | static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd, | |
b3b6d040 | 1748 | struct cfq_queue *cur_cfqq) |
6d048f53 | 1749 | { |
a36e71f9 JA |
1750 | struct cfq_queue *cfqq; |
1751 | ||
39c01b21 DS |
1752 | if (cfq_class_idle(cur_cfqq)) |
1753 | return NULL; | |
e6c5bc73 JM |
1754 | if (!cfq_cfqq_sync(cur_cfqq)) |
1755 | return NULL; | |
1756 | if (CFQQ_SEEKY(cur_cfqq)) | |
1757 | return NULL; | |
1758 | ||
b9d8f4c7 GJ |
1759 | /* |
1760 | * Don't search priority tree if it's the only queue in the group. | |
1761 | */ | |
1762 | if (cur_cfqq->cfqg->nr_cfqq == 1) | |
1763 | return NULL; | |
1764 | ||
6d048f53 | 1765 | /* |
d9e7620e JA |
1766 | * We should notice if some of the queues are cooperating, eg |
1767 | * working closely on the same area of the disk. In that case, | |
1768 | * we can group them together and don't waste time idling. | |
6d048f53 | 1769 | */ |
a36e71f9 JA |
1770 | cfqq = cfqq_close(cfqd, cur_cfqq); |
1771 | if (!cfqq) | |
1772 | return NULL; | |
1773 | ||
8682e1f1 VG |
1774 | /* If new queue belongs to different cfq_group, don't choose it */ |
1775 | if (cur_cfqq->cfqg != cfqq->cfqg) | |
1776 | return NULL; | |
1777 | ||
df5fe3e8 JM |
1778 | /* |
1779 | * It only makes sense to merge sync queues. | |
1780 | */ | |
1781 | if (!cfq_cfqq_sync(cfqq)) | |
1782 | return NULL; | |
e6c5bc73 JM |
1783 | if (CFQQ_SEEKY(cfqq)) |
1784 | return NULL; | |
df5fe3e8 | 1785 | |
c0324a02 CZ |
1786 | /* |
1787 | * Do not merge queues of different priority classes | |
1788 | */ | |
1789 | if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq)) | |
1790 | return NULL; | |
1791 | ||
a36e71f9 | 1792 | return cfqq; |
6d048f53 JA |
1793 | } |
1794 | ||
a6d44e98 CZ |
1795 | /* |
1796 | * Determine whether we should enforce idle window for this queue. | |
1797 | */ | |
1798 | ||
1799 | static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
1800 | { | |
1801 | enum wl_prio_t prio = cfqq_prio(cfqq); | |
718eee05 | 1802 | struct cfq_rb_root *service_tree = cfqq->service_tree; |
a6d44e98 | 1803 | |
f04a6424 VG |
1804 | BUG_ON(!service_tree); |
1805 | BUG_ON(!service_tree->count); | |
1806 | ||
b6508c16 VG |
1807 | if (!cfqd->cfq_slice_idle) |
1808 | return false; | |
1809 | ||
a6d44e98 CZ |
1810 | /* We never do for idle class queues. */ |
1811 | if (prio == IDLE_WORKLOAD) | |
1812 | return false; | |
1813 | ||
1814 | /* We do for queues that were marked with idle window flag. */ | |
3c764b7a SL |
1815 | if (cfq_cfqq_idle_window(cfqq) && |
1816 | !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)) | |
a6d44e98 CZ |
1817 | return true; |
1818 | ||
1819 | /* | |
1820 | * Otherwise, we do only if they are the last ones | |
1821 | * in their service tree. | |
1822 | */ | |
f5f2b6ce SL |
1823 | if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) && |
1824 | !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false)) | |
c1e44756 | 1825 | return true; |
b1ffe737 DS |
1826 | cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", |
1827 | service_tree->count); | |
c1e44756 | 1828 | return false; |
a6d44e98 CZ |
1829 | } |
1830 | ||
6d048f53 | 1831 | static void cfq_arm_slice_timer(struct cfq_data *cfqd) |
22e2c507 | 1832 | { |
1792669c | 1833 | struct cfq_queue *cfqq = cfqd->active_queue; |
c5869807 | 1834 | struct cfq_io_cq *cic; |
80bdf0c7 | 1835 | unsigned long sl, group_idle = 0; |
7b14e3b5 | 1836 | |
a68bbddb | 1837 | /* |
f7d7b7a7 JA |
1838 | * SSD device without seek penalty, disable idling. But only do so |
1839 | * for devices that support queuing, otherwise we still have a problem | |
1840 | * with sync vs async workloads. | |
a68bbddb | 1841 | */ |
f7d7b7a7 | 1842 | if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag) |
a68bbddb JA |
1843 | return; |
1844 | ||
dd67d051 | 1845 | WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); |
6d048f53 | 1846 | WARN_ON(cfq_cfqq_slice_new(cfqq)); |
22e2c507 JA |
1847 | |
1848 | /* | |
1849 | * idle is disabled, either manually or by past process history | |
1850 | */ | |
80bdf0c7 VG |
1851 | if (!cfq_should_idle(cfqd, cfqq)) { |
1852 | /* no queue idling. Check for group idling */ | |
1853 | if (cfqd->cfq_group_idle) | |
1854 | group_idle = cfqd->cfq_group_idle; | |
1855 | else | |
1856 | return; | |
1857 | } | |
6d048f53 | 1858 | |
7b679138 | 1859 | /* |
8e550632 | 1860 | * still active requests from this queue, don't idle |
7b679138 | 1861 | */ |
8e550632 | 1862 | if (cfqq->dispatched) |
7b679138 JA |
1863 | return; |
1864 | ||
22e2c507 JA |
1865 | /* |
1866 | * task has exited, don't wait | |
1867 | */ | |
206dc69b | 1868 | cic = cfqd->active_cic; |
f6e8d01b | 1869 | if (!cic || !atomic_read(&cic->icq.ioc->active_ref)) |
6d048f53 JA |
1870 | return; |
1871 | ||
355b659c CZ |
1872 | /* |
1873 | * If our average think time is larger than the remaining time | |
1874 | * slice, then don't idle. This avoids overrunning the allotted | |
1875 | * time slice. | |
1876 | */ | |
383cd721 SL |
1877 | if (sample_valid(cic->ttime.ttime_samples) && |
1878 | (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) { | |
fd16d263 | 1879 | cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu", |
383cd721 | 1880 | cic->ttime.ttime_mean); |
355b659c | 1881 | return; |
b1ffe737 | 1882 | } |
355b659c | 1883 | |
80bdf0c7 VG |
1884 | /* There are other queues in the group, don't do group idle */ |
1885 | if (group_idle && cfqq->cfqg->nr_cfqq > 1) | |
1886 | return; | |
1887 | ||
3b18152c | 1888 | cfq_mark_cfqq_wait_request(cfqq); |
22e2c507 | 1889 | |
80bdf0c7 VG |
1890 | if (group_idle) |
1891 | sl = cfqd->cfq_group_idle; | |
1892 | else | |
1893 | sl = cfqd->cfq_slice_idle; | |
206dc69b | 1894 | |
7b14e3b5 | 1895 | mod_timer(&cfqd->idle_slice_timer, jiffies + sl); |
c1768268 TH |
1896 | cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg), |
1897 | &blkio_policy_cfq); | |
80bdf0c7 VG |
1898 | cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl, |
1899 | group_idle ? 1 : 0); | |
1da177e4 LT |
1900 | } |
1901 | ||
498d3aa2 JA |
1902 | /* |
1903 | * Move request from internal lists to the request queue dispatch list. | |
1904 | */ | |
165125e1 | 1905 | static void cfq_dispatch_insert(struct request_queue *q, struct request *rq) |
1da177e4 | 1906 | { |
3ed9a296 | 1907 | struct cfq_data *cfqd = q->elevator->elevator_data; |
5e705374 | 1908 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
22e2c507 | 1909 | |
7b679138 JA |
1910 | cfq_log_cfqq(cfqd, cfqq, "dispatch_insert"); |
1911 | ||
06d21886 | 1912 | cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq); |
5380a101 | 1913 | cfq_remove_request(rq); |
6d048f53 | 1914 | cfqq->dispatched++; |
80bdf0c7 | 1915 | (RQ_CFQG(rq))->dispatched++; |
5380a101 | 1916 | elv_dispatch_sort(q, rq); |
3ed9a296 | 1917 | |
53c583d2 | 1918 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++; |
c4e7893e | 1919 | cfqq->nr_sectors += blk_rq_sectors(rq); |
0381411e | 1920 | cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg), |
c1768268 TH |
1921 | &blkio_policy_cfq, blk_rq_bytes(rq), |
1922 | rq_data_dir(rq), rq_is_sync(rq)); | |
1da177e4 LT |
1923 | } |
1924 | ||
1925 | /* | |
1926 | * return expired entry, or NULL to just start from scratch in rbtree | |
1927 | */ | |
febffd61 | 1928 | static struct request *cfq_check_fifo(struct cfq_queue *cfqq) |
1da177e4 | 1929 | { |
30996f40 | 1930 | struct request *rq = NULL; |
1da177e4 | 1931 | |
3b18152c | 1932 | if (cfq_cfqq_fifo_expire(cfqq)) |
1da177e4 | 1933 | return NULL; |
cb887411 JA |
1934 | |
1935 | cfq_mark_cfqq_fifo_expire(cfqq); | |
1936 | ||
89850f7e JA |
1937 | if (list_empty(&cfqq->fifo)) |
1938 | return NULL; | |
1da177e4 | 1939 | |
89850f7e | 1940 | rq = rq_entry_fifo(cfqq->fifo.next); |
30996f40 | 1941 | if (time_before(jiffies, rq_fifo_time(rq))) |
7b679138 | 1942 | rq = NULL; |
1da177e4 | 1943 | |
30996f40 | 1944 | cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq); |
6d048f53 | 1945 | return rq; |
1da177e4 LT |
1946 | } |
1947 | ||
22e2c507 JA |
1948 | static inline int |
1949 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
1950 | { | |
1951 | const int base_rq = cfqd->cfq_slice_async_rq; | |
1da177e4 | 1952 | |
22e2c507 | 1953 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
1da177e4 | 1954 | |
b9f8ce05 | 1955 | return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio); |
1da177e4 LT |
1956 | } |
1957 | ||
df5fe3e8 JM |
1958 | /* |
1959 | * Must be called with the queue_lock held. | |
1960 | */ | |
1961 | static int cfqq_process_refs(struct cfq_queue *cfqq) | |
1962 | { | |
1963 | int process_refs, io_refs; | |
1964 | ||
1965 | io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE]; | |
30d7b944 | 1966 | process_refs = cfqq->ref - io_refs; |
df5fe3e8 JM |
1967 | BUG_ON(process_refs < 0); |
1968 | return process_refs; | |
1969 | } | |
1970 | ||
1971 | static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq) | |
1972 | { | |
e6c5bc73 | 1973 | int process_refs, new_process_refs; |
df5fe3e8 JM |
1974 | struct cfq_queue *__cfqq; |
1975 | ||
c10b61f0 JM |
1976 | /* |
1977 | * If there are no process references on the new_cfqq, then it is | |
1978 | * unsafe to follow the ->new_cfqq chain as other cfqq's in the | |
1979 | * chain may have dropped their last reference (not just their | |
1980 | * last process reference). | |
1981 | */ | |
1982 | if (!cfqq_process_refs(new_cfqq)) | |
1983 | return; | |
1984 | ||
df5fe3e8 JM |
1985 | /* Avoid a circular list and skip interim queue merges */ |
1986 | while ((__cfqq = new_cfqq->new_cfqq)) { | |
1987 | if (__cfqq == cfqq) | |
1988 | return; | |
1989 | new_cfqq = __cfqq; | |
1990 | } | |
1991 | ||
1992 | process_refs = cfqq_process_refs(cfqq); | |
c10b61f0 | 1993 | new_process_refs = cfqq_process_refs(new_cfqq); |
df5fe3e8 JM |
1994 | /* |
1995 | * If the process for the cfqq has gone away, there is no | |
1996 | * sense in merging the queues. | |
1997 | */ | |
c10b61f0 | 1998 | if (process_refs == 0 || new_process_refs == 0) |
df5fe3e8 JM |
1999 | return; |
2000 | ||
e6c5bc73 JM |
2001 | /* |
2002 | * Merge in the direction of the lesser amount of work. | |
2003 | */ | |
e6c5bc73 JM |
2004 | if (new_process_refs >= process_refs) { |
2005 | cfqq->new_cfqq = new_cfqq; | |
30d7b944 | 2006 | new_cfqq->ref += process_refs; |
e6c5bc73 JM |
2007 | } else { |
2008 | new_cfqq->new_cfqq = cfqq; | |
30d7b944 | 2009 | cfqq->ref += new_process_refs; |
e6c5bc73 | 2010 | } |
df5fe3e8 JM |
2011 | } |
2012 | ||
cdb16e8f | 2013 | static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd, |
65b32a57 | 2014 | struct cfq_group *cfqg, enum wl_prio_t prio) |
718eee05 CZ |
2015 | { |
2016 | struct cfq_queue *queue; | |
2017 | int i; | |
2018 | bool key_valid = false; | |
2019 | unsigned long lowest_key = 0; | |
2020 | enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD; | |
2021 | ||
65b32a57 VG |
2022 | for (i = 0; i <= SYNC_WORKLOAD; ++i) { |
2023 | /* select the one with lowest rb_key */ | |
2024 | queue = cfq_rb_first(service_tree_for(cfqg, prio, i)); | |
718eee05 CZ |
2025 | if (queue && |
2026 | (!key_valid || time_before(queue->rb_key, lowest_key))) { | |
2027 | lowest_key = queue->rb_key; | |
2028 | cur_best = i; | |
2029 | key_valid = true; | |
2030 | } | |
2031 | } | |
2032 | ||
2033 | return cur_best; | |
2034 | } | |
2035 | ||
cdb16e8f | 2036 | static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg) |
718eee05 | 2037 | { |
718eee05 CZ |
2038 | unsigned slice; |
2039 | unsigned count; | |
cdb16e8f | 2040 | struct cfq_rb_root *st; |
58ff82f3 | 2041 | unsigned group_slice; |
e4ea0c16 | 2042 | enum wl_prio_t original_prio = cfqd->serving_prio; |
1fa8f6d6 | 2043 | |
718eee05 | 2044 | /* Choose next priority. RT > BE > IDLE */ |
58ff82f3 | 2045 | if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg)) |
718eee05 | 2046 | cfqd->serving_prio = RT_WORKLOAD; |
58ff82f3 | 2047 | else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg)) |
718eee05 CZ |
2048 | cfqd->serving_prio = BE_WORKLOAD; |
2049 | else { | |
2050 | cfqd->serving_prio = IDLE_WORKLOAD; | |
2051 | cfqd->workload_expires = jiffies + 1; | |
2052 | return; | |
2053 | } | |
2054 | ||
e4ea0c16 SL |
2055 | if (original_prio != cfqd->serving_prio) |
2056 | goto new_workload; | |
2057 | ||
718eee05 CZ |
2058 | /* |
2059 | * For RT and BE, we have to choose also the type | |
2060 | * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload | |
2061 | * expiration time | |
2062 | */ | |
65b32a57 | 2063 | st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type); |
cdb16e8f | 2064 | count = st->count; |
718eee05 CZ |
2065 | |
2066 | /* | |
65b32a57 | 2067 | * check workload expiration, and that we still have other queues ready |
718eee05 | 2068 | */ |
65b32a57 | 2069 | if (count && !time_after(jiffies, cfqd->workload_expires)) |
718eee05 CZ |
2070 | return; |
2071 | ||
e4ea0c16 | 2072 | new_workload: |
718eee05 CZ |
2073 | /* otherwise select new workload type */ |
2074 | cfqd->serving_type = | |
65b32a57 VG |
2075 | cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio); |
2076 | st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type); | |
cdb16e8f | 2077 | count = st->count; |
718eee05 CZ |
2078 | |
2079 | /* | |
2080 | * the workload slice is computed as a fraction of target latency | |
2081 | * proportional to the number of queues in that workload, over | |
2082 | * all the queues in the same priority class | |
2083 | */ | |
58ff82f3 VG |
2084 | group_slice = cfq_group_slice(cfqd, cfqg); |
2085 | ||
2086 | slice = group_slice * count / | |
2087 | max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio], | |
2088 | cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg)); | |
718eee05 | 2089 | |
f26bd1f0 VG |
2090 | if (cfqd->serving_type == ASYNC_WORKLOAD) { |
2091 | unsigned int tmp; | |
2092 | ||
2093 | /* | |
2094 | * Async queues are currently system wide. Just taking | |
2095 | * proportion of queues with-in same group will lead to higher | |
2096 | * async ratio system wide as generally root group is going | |
2097 | * to have higher weight. A more accurate thing would be to | |
2098 | * calculate system wide asnc/sync ratio. | |
2099 | */ | |
2100 | tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg); | |
2101 | tmp = tmp/cfqd->busy_queues; | |
2102 | slice = min_t(unsigned, slice, tmp); | |
2103 | ||
718eee05 CZ |
2104 | /* async workload slice is scaled down according to |
2105 | * the sync/async slice ratio. */ | |
2106 | slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1]; | |
f26bd1f0 | 2107 | } else |
718eee05 CZ |
2108 | /* sync workload slice is at least 2 * cfq_slice_idle */ |
2109 | slice = max(slice, 2 * cfqd->cfq_slice_idle); | |
2110 | ||
2111 | slice = max_t(unsigned, slice, CFQ_MIN_TT); | |
b1ffe737 | 2112 | cfq_log(cfqd, "workload slice:%d", slice); |
718eee05 CZ |
2113 | cfqd->workload_expires = jiffies + slice; |
2114 | } | |
2115 | ||
1fa8f6d6 VG |
2116 | static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd) |
2117 | { | |
2118 | struct cfq_rb_root *st = &cfqd->grp_service_tree; | |
25bc6b07 | 2119 | struct cfq_group *cfqg; |
1fa8f6d6 VG |
2120 | |
2121 | if (RB_EMPTY_ROOT(&st->rb)) | |
2122 | return NULL; | |
25bc6b07 | 2123 | cfqg = cfq_rb_first_group(st); |
25bc6b07 VG |
2124 | update_min_vdisktime(st); |
2125 | return cfqg; | |
1fa8f6d6 VG |
2126 | } |
2127 | ||
cdb16e8f VG |
2128 | static void cfq_choose_cfqg(struct cfq_data *cfqd) |
2129 | { | |
1fa8f6d6 VG |
2130 | struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd); |
2131 | ||
2132 | cfqd->serving_group = cfqg; | |
dae739eb VG |
2133 | |
2134 | /* Restore the workload type data */ | |
2135 | if (cfqg->saved_workload_slice) { | |
2136 | cfqd->workload_expires = jiffies + cfqg->saved_workload_slice; | |
2137 | cfqd->serving_type = cfqg->saved_workload; | |
2138 | cfqd->serving_prio = cfqg->saved_serving_prio; | |
66ae2919 GJ |
2139 | } else |
2140 | cfqd->workload_expires = jiffies - 1; | |
2141 | ||
1fa8f6d6 | 2142 | choose_service_tree(cfqd, cfqg); |
cdb16e8f VG |
2143 | } |
2144 | ||
22e2c507 | 2145 | /* |
498d3aa2 JA |
2146 | * Select a queue for service. If we have a current active queue, |
2147 | * check whether to continue servicing it, or retrieve and set a new one. | |
22e2c507 | 2148 | */ |
1b5ed5e1 | 2149 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
1da177e4 | 2150 | { |
a36e71f9 | 2151 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
1da177e4 | 2152 | |
22e2c507 JA |
2153 | cfqq = cfqd->active_queue; |
2154 | if (!cfqq) | |
2155 | goto new_queue; | |
1da177e4 | 2156 | |
f04a6424 VG |
2157 | if (!cfqd->rq_queued) |
2158 | return NULL; | |
c244bb50 VG |
2159 | |
2160 | /* | |
2161 | * We were waiting for group to get backlogged. Expire the queue | |
2162 | */ | |
2163 | if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list)) | |
2164 | goto expire; | |
2165 | ||
22e2c507 | 2166 | /* |
6d048f53 | 2167 | * The active queue has run out of time, expire it and select new. |
22e2c507 | 2168 | */ |
7667aa06 VG |
2169 | if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) { |
2170 | /* | |
2171 | * If slice had not expired at the completion of last request | |
2172 | * we might not have turned on wait_busy flag. Don't expire | |
2173 | * the queue yet. Allow the group to get backlogged. | |
2174 | * | |
2175 | * The very fact that we have used the slice, that means we | |
2176 | * have been idling all along on this queue and it should be | |
2177 | * ok to wait for this request to complete. | |
2178 | */ | |
82bbbf28 VG |
2179 | if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list) |
2180 | && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { | |
2181 | cfqq = NULL; | |
7667aa06 | 2182 | goto keep_queue; |
82bbbf28 | 2183 | } else |
80bdf0c7 | 2184 | goto check_group_idle; |
7667aa06 | 2185 | } |
1da177e4 | 2186 | |
22e2c507 | 2187 | /* |
6d048f53 JA |
2188 | * The active queue has requests and isn't expired, allow it to |
2189 | * dispatch. | |
22e2c507 | 2190 | */ |
dd67d051 | 2191 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
22e2c507 | 2192 | goto keep_queue; |
6d048f53 | 2193 | |
a36e71f9 JA |
2194 | /* |
2195 | * If another queue has a request waiting within our mean seek | |
2196 | * distance, let it run. The expire code will check for close | |
2197 | * cooperators and put the close queue at the front of the service | |
df5fe3e8 | 2198 | * tree. If possible, merge the expiring queue with the new cfqq. |
a36e71f9 | 2199 | */ |
b3b6d040 | 2200 | new_cfqq = cfq_close_cooperator(cfqd, cfqq); |
df5fe3e8 JM |
2201 | if (new_cfqq) { |
2202 | if (!cfqq->new_cfqq) | |
2203 | cfq_setup_merge(cfqq, new_cfqq); | |
a36e71f9 | 2204 | goto expire; |
df5fe3e8 | 2205 | } |
a36e71f9 | 2206 | |
6d048f53 JA |
2207 | /* |
2208 | * No requests pending. If the active queue still has requests in | |
2209 | * flight or is idling for a new request, allow either of these | |
2210 | * conditions to happen (or time out) before selecting a new queue. | |
2211 | */ | |
80bdf0c7 VG |
2212 | if (timer_pending(&cfqd->idle_slice_timer)) { |
2213 | cfqq = NULL; | |
2214 | goto keep_queue; | |
2215 | } | |
2216 | ||
8e1ac665 SL |
2217 | /* |
2218 | * This is a deep seek queue, but the device is much faster than | |
2219 | * the queue can deliver, don't idle | |
2220 | **/ | |
2221 | if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) && | |
2222 | (cfq_cfqq_slice_new(cfqq) || | |
2223 | (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) { | |
2224 | cfq_clear_cfqq_deep(cfqq); | |
2225 | cfq_clear_cfqq_idle_window(cfqq); | |
2226 | } | |
2227 | ||
80bdf0c7 VG |
2228 | if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) { |
2229 | cfqq = NULL; | |
2230 | goto keep_queue; | |
2231 | } | |
2232 | ||
2233 | /* | |
2234 | * If group idle is enabled and there are requests dispatched from | |
2235 | * this group, wait for requests to complete. | |
2236 | */ | |
2237 | check_group_idle: | |
7700fc4f SL |
2238 | if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 && |
2239 | cfqq->cfqg->dispatched && | |
2240 | !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) { | |
caaa5f9f JA |
2241 | cfqq = NULL; |
2242 | goto keep_queue; | |
22e2c507 JA |
2243 | } |
2244 | ||
3b18152c | 2245 | expire: |
e5ff082e | 2246 | cfq_slice_expired(cfqd, 0); |
3b18152c | 2247 | new_queue: |
718eee05 CZ |
2248 | /* |
2249 | * Current queue expired. Check if we have to switch to a new | |
2250 | * service tree | |
2251 | */ | |
2252 | if (!new_cfqq) | |
cdb16e8f | 2253 | cfq_choose_cfqg(cfqd); |
718eee05 | 2254 | |
a36e71f9 | 2255 | cfqq = cfq_set_active_queue(cfqd, new_cfqq); |
22e2c507 | 2256 | keep_queue: |
3b18152c | 2257 | return cfqq; |
22e2c507 JA |
2258 | } |
2259 | ||
febffd61 | 2260 | static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) |
d9e7620e JA |
2261 | { |
2262 | int dispatched = 0; | |
2263 | ||
2264 | while (cfqq->next_rq) { | |
2265 | cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); | |
2266 | dispatched++; | |
2267 | } | |
2268 | ||
2269 | BUG_ON(!list_empty(&cfqq->fifo)); | |
f04a6424 VG |
2270 | |
2271 | /* By default cfqq is not expired if it is empty. Do it explicitly */ | |
e5ff082e | 2272 | __cfq_slice_expired(cfqq->cfqd, cfqq, 0); |
d9e7620e JA |
2273 | return dispatched; |
2274 | } | |
2275 | ||
498d3aa2 JA |
2276 | /* |
2277 | * Drain our current requests. Used for barriers and when switching | |
2278 | * io schedulers on-the-fly. | |
2279 | */ | |
d9e7620e | 2280 | static int cfq_forced_dispatch(struct cfq_data *cfqd) |
1b5ed5e1 | 2281 | { |
0871714e | 2282 | struct cfq_queue *cfqq; |
d9e7620e | 2283 | int dispatched = 0; |
cdb16e8f | 2284 | |
3440c49f | 2285 | /* Expire the timeslice of the current active queue first */ |
e5ff082e | 2286 | cfq_slice_expired(cfqd, 0); |
3440c49f DS |
2287 | while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) { |
2288 | __cfq_set_active_queue(cfqd, cfqq); | |
f04a6424 | 2289 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); |
3440c49f | 2290 | } |
1b5ed5e1 | 2291 | |
1b5ed5e1 TH |
2292 | BUG_ON(cfqd->busy_queues); |
2293 | ||
6923715a | 2294 | cfq_log(cfqd, "forced_dispatch=%d", dispatched); |
1b5ed5e1 TH |
2295 | return dispatched; |
2296 | } | |
2297 | ||
abc3c744 SL |
2298 | static inline bool cfq_slice_used_soon(struct cfq_data *cfqd, |
2299 | struct cfq_queue *cfqq) | |
2300 | { | |
2301 | /* the queue hasn't finished any request, can't estimate */ | |
2302 | if (cfq_cfqq_slice_new(cfqq)) | |
c1e44756 | 2303 | return true; |
abc3c744 SL |
2304 | if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched, |
2305 | cfqq->slice_end)) | |
c1e44756 | 2306 | return true; |
abc3c744 | 2307 | |
c1e44756 | 2308 | return false; |
abc3c744 SL |
2309 | } |
2310 | ||
0b182d61 | 2311 | static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
2f5cb738 | 2312 | { |
2f5cb738 | 2313 | unsigned int max_dispatch; |
22e2c507 | 2314 | |
5ad531db JA |
2315 | /* |
2316 | * Drain async requests before we start sync IO | |
2317 | */ | |
53c583d2 | 2318 | if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC]) |
0b182d61 | 2319 | return false; |
5ad531db | 2320 | |
2f5cb738 JA |
2321 | /* |
2322 | * If this is an async queue and we have sync IO in flight, let it wait | |
2323 | */ | |
53c583d2 | 2324 | if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq)) |
0b182d61 | 2325 | return false; |
2f5cb738 | 2326 | |
abc3c744 | 2327 | max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1); |
2f5cb738 JA |
2328 | if (cfq_class_idle(cfqq)) |
2329 | max_dispatch = 1; | |
b4878f24 | 2330 | |
2f5cb738 JA |
2331 | /* |
2332 | * Does this cfqq already have too much IO in flight? | |
2333 | */ | |
2334 | if (cfqq->dispatched >= max_dispatch) { | |
ef8a41df | 2335 | bool promote_sync = false; |
2f5cb738 JA |
2336 | /* |
2337 | * idle queue must always only have a single IO in flight | |
2338 | */ | |
3ed9a296 | 2339 | if (cfq_class_idle(cfqq)) |
0b182d61 | 2340 | return false; |
3ed9a296 | 2341 | |
ef8a41df | 2342 | /* |
c4ade94f LS |
2343 | * If there is only one sync queue |
2344 | * we can ignore async queue here and give the sync | |
ef8a41df SL |
2345 | * queue no dispatch limit. The reason is a sync queue can |
2346 | * preempt async queue, limiting the sync queue doesn't make | |
2347 | * sense. This is useful for aiostress test. | |
2348 | */ | |
c4ade94f LS |
2349 | if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1) |
2350 | promote_sync = true; | |
ef8a41df | 2351 | |
2f5cb738 JA |
2352 | /* |
2353 | * We have other queues, don't allow more IO from this one | |
2354 | */ | |
ef8a41df SL |
2355 | if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) && |
2356 | !promote_sync) | |
0b182d61 | 2357 | return false; |
9ede209e | 2358 | |
365722bb | 2359 | /* |
474b18cc | 2360 | * Sole queue user, no limit |
365722bb | 2361 | */ |
ef8a41df | 2362 | if (cfqd->busy_queues == 1 || promote_sync) |
abc3c744 SL |
2363 | max_dispatch = -1; |
2364 | else | |
2365 | /* | |
2366 | * Normally we start throttling cfqq when cfq_quantum/2 | |
2367 | * requests have been dispatched. But we can drive | |
2368 | * deeper queue depths at the beginning of slice | |
2369 | * subjected to upper limit of cfq_quantum. | |
2370 | * */ | |
2371 | max_dispatch = cfqd->cfq_quantum; | |
8e296755 JA |
2372 | } |
2373 | ||
2374 | /* | |
2375 | * Async queues must wait a bit before being allowed dispatch. | |
2376 | * We also ramp up the dispatch depth gradually for async IO, | |
2377 | * based on the last sync IO we serviced | |
2378 | */ | |
963b72fc | 2379 | if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) { |
573412b2 | 2380 | unsigned long last_sync = jiffies - cfqd->last_delayed_sync; |
8e296755 | 2381 | unsigned int depth; |
365722bb | 2382 | |
61f0c1dc | 2383 | depth = last_sync / cfqd->cfq_slice[1]; |
e00c54c3 JA |
2384 | if (!depth && !cfqq->dispatched) |
2385 | depth = 1; | |
8e296755 JA |
2386 | if (depth < max_dispatch) |
2387 | max_dispatch = depth; | |
2f5cb738 | 2388 | } |
3ed9a296 | 2389 | |
0b182d61 JA |
2390 | /* |
2391 | * If we're below the current max, allow a dispatch | |
2392 | */ | |
2393 | return cfqq->dispatched < max_dispatch; | |
2394 | } | |
2395 | ||
2396 | /* | |
2397 | * Dispatch a request from cfqq, moving them to the request queue | |
2398 | * dispatch list. | |
2399 | */ | |
2400 | static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
2401 | { | |
2402 | struct request *rq; | |
2403 | ||
2404 | BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); | |
2405 | ||
2406 | if (!cfq_may_dispatch(cfqd, cfqq)) | |
2407 | return false; | |
2408 | ||
2409 | /* | |
2410 | * follow expired path, else get first next available | |
2411 | */ | |
2412 | rq = cfq_check_fifo(cfqq); | |
2413 | if (!rq) | |
2414 | rq = cfqq->next_rq; | |
2415 | ||
2416 | /* | |
2417 | * insert request into driver dispatch list | |
2418 | */ | |
2419 | cfq_dispatch_insert(cfqd->queue, rq); | |
2420 | ||
2421 | if (!cfqd->active_cic) { | |
c5869807 | 2422 | struct cfq_io_cq *cic = RQ_CIC(rq); |
0b182d61 | 2423 | |
c5869807 | 2424 | atomic_long_inc(&cic->icq.ioc->refcount); |
0b182d61 JA |
2425 | cfqd->active_cic = cic; |
2426 | } | |
2427 | ||
2428 | return true; | |
2429 | } | |
2430 | ||
2431 | /* | |
2432 | * Find the cfqq that we need to service and move a request from that to the | |
2433 | * dispatch list | |
2434 | */ | |
2435 | static int cfq_dispatch_requests(struct request_queue *q, int force) | |
2436 | { | |
2437 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
2438 | struct cfq_queue *cfqq; | |
2439 | ||
2440 | if (!cfqd->busy_queues) | |
2441 | return 0; | |
2442 | ||
2443 | if (unlikely(force)) | |
2444 | return cfq_forced_dispatch(cfqd); | |
2445 | ||
2446 | cfqq = cfq_select_queue(cfqd); | |
2447 | if (!cfqq) | |
8e296755 JA |
2448 | return 0; |
2449 | ||
2f5cb738 | 2450 | /* |
0b182d61 | 2451 | * Dispatch a request from this cfqq, if it is allowed |
2f5cb738 | 2452 | */ |
0b182d61 JA |
2453 | if (!cfq_dispatch_request(cfqd, cfqq)) |
2454 | return 0; | |
2455 | ||
2f5cb738 | 2456 | cfqq->slice_dispatch++; |
b029195d | 2457 | cfq_clear_cfqq_must_dispatch(cfqq); |
22e2c507 | 2458 | |
2f5cb738 JA |
2459 | /* |
2460 | * expire an async queue immediately if it has used up its slice. idle | |
2461 | * queue always expire after 1 dispatch round. | |
2462 | */ | |
2463 | if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && | |
2464 | cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) || | |
2465 | cfq_class_idle(cfqq))) { | |
2466 | cfqq->slice_end = jiffies + 1; | |
e5ff082e | 2467 | cfq_slice_expired(cfqd, 0); |
1da177e4 LT |
2468 | } |
2469 | ||
b217a903 | 2470 | cfq_log_cfqq(cfqd, cfqq, "dispatched a request"); |
2f5cb738 | 2471 | return 1; |
1da177e4 LT |
2472 | } |
2473 | ||
1da177e4 | 2474 | /* |
5e705374 JA |
2475 | * task holds one reference to the queue, dropped when task exits. each rq |
2476 | * in-flight on this queue also holds a reference, dropped when rq is freed. | |
1da177e4 | 2477 | * |
b1c35769 | 2478 | * Each cfq queue took a reference on the parent group. Drop it now. |
1da177e4 LT |
2479 | * queue lock must be held here. |
2480 | */ | |
2481 | static void cfq_put_queue(struct cfq_queue *cfqq) | |
2482 | { | |
22e2c507 | 2483 | struct cfq_data *cfqd = cfqq->cfqd; |
0bbfeb83 | 2484 | struct cfq_group *cfqg; |
22e2c507 | 2485 | |
30d7b944 | 2486 | BUG_ON(cfqq->ref <= 0); |
1da177e4 | 2487 | |
30d7b944 SL |
2488 | cfqq->ref--; |
2489 | if (cfqq->ref) | |
1da177e4 LT |
2490 | return; |
2491 | ||
7b679138 | 2492 | cfq_log_cfqq(cfqd, cfqq, "put_queue"); |
1da177e4 | 2493 | BUG_ON(rb_first(&cfqq->sort_list)); |
22e2c507 | 2494 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
b1c35769 | 2495 | cfqg = cfqq->cfqg; |
1da177e4 | 2496 | |
28f95cbc | 2497 | if (unlikely(cfqd->active_queue == cfqq)) { |
e5ff082e | 2498 | __cfq_slice_expired(cfqd, cfqq, 0); |
23e018a1 | 2499 | cfq_schedule_dispatch(cfqd); |
28f95cbc | 2500 | } |
22e2c507 | 2501 | |
f04a6424 | 2502 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
1da177e4 | 2503 | kmem_cache_free(cfq_pool, cfqq); |
1adaf3dd | 2504 | blkg_put(cfqg_to_blkg(cfqg)); |
1da177e4 LT |
2505 | } |
2506 | ||
d02a2c07 | 2507 | static void cfq_put_cooperator(struct cfq_queue *cfqq) |
1da177e4 | 2508 | { |
df5fe3e8 JM |
2509 | struct cfq_queue *__cfqq, *next; |
2510 | ||
df5fe3e8 JM |
2511 | /* |
2512 | * If this queue was scheduled to merge with another queue, be | |
2513 | * sure to drop the reference taken on that queue (and others in | |
2514 | * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs. | |
2515 | */ | |
2516 | __cfqq = cfqq->new_cfqq; | |
2517 | while (__cfqq) { | |
2518 | if (__cfqq == cfqq) { | |
2519 | WARN(1, "cfqq->new_cfqq loop detected\n"); | |
2520 | break; | |
2521 | } | |
2522 | next = __cfqq->new_cfqq; | |
2523 | cfq_put_queue(__cfqq); | |
2524 | __cfqq = next; | |
2525 | } | |
d02a2c07 SL |
2526 | } |
2527 | ||
2528 | static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
2529 | { | |
2530 | if (unlikely(cfqq == cfqd->active_queue)) { | |
2531 | __cfq_slice_expired(cfqd, cfqq, 0); | |
2532 | cfq_schedule_dispatch(cfqd); | |
2533 | } | |
2534 | ||
2535 | cfq_put_cooperator(cfqq); | |
df5fe3e8 | 2536 | |
89850f7e JA |
2537 | cfq_put_queue(cfqq); |
2538 | } | |
22e2c507 | 2539 | |
9b84cacd TH |
2540 | static void cfq_init_icq(struct io_cq *icq) |
2541 | { | |
2542 | struct cfq_io_cq *cic = icq_to_cic(icq); | |
2543 | ||
2544 | cic->ttime.last_end_request = jiffies; | |
2545 | } | |
2546 | ||
c5869807 | 2547 | static void cfq_exit_icq(struct io_cq *icq) |
89850f7e | 2548 | { |
c5869807 | 2549 | struct cfq_io_cq *cic = icq_to_cic(icq); |
283287a5 | 2550 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
4faa3c81 | 2551 | |
ff6657c6 JA |
2552 | if (cic->cfqq[BLK_RW_ASYNC]) { |
2553 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]); | |
2554 | cic->cfqq[BLK_RW_ASYNC] = NULL; | |
12a05732 AV |
2555 | } |
2556 | ||
ff6657c6 JA |
2557 | if (cic->cfqq[BLK_RW_SYNC]) { |
2558 | cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]); | |
2559 | cic->cfqq[BLK_RW_SYNC] = NULL; | |
12a05732 | 2560 | } |
89850f7e JA |
2561 | } |
2562 | ||
fd0928df | 2563 | static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc) |
22e2c507 JA |
2564 | { |
2565 | struct task_struct *tsk = current; | |
2566 | int ioprio_class; | |
2567 | ||
3b18152c | 2568 | if (!cfq_cfqq_prio_changed(cfqq)) |
22e2c507 JA |
2569 | return; |
2570 | ||
fd0928df | 2571 | ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio); |
22e2c507 | 2572 | switch (ioprio_class) { |
fe094d98 JA |
2573 | default: |
2574 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); | |
2575 | case IOPRIO_CLASS_NONE: | |
2576 | /* | |
6d63c275 | 2577 | * no prio set, inherit CPU scheduling settings |
fe094d98 JA |
2578 | */ |
2579 | cfqq->ioprio = task_nice_ioprio(tsk); | |
6d63c275 | 2580 | cfqq->ioprio_class = task_nice_ioclass(tsk); |
fe094d98 JA |
2581 | break; |
2582 | case IOPRIO_CLASS_RT: | |
2583 | cfqq->ioprio = task_ioprio(ioc); | |
2584 | cfqq->ioprio_class = IOPRIO_CLASS_RT; | |
2585 | break; | |
2586 | case IOPRIO_CLASS_BE: | |
2587 | cfqq->ioprio = task_ioprio(ioc); | |
2588 | cfqq->ioprio_class = IOPRIO_CLASS_BE; | |
2589 | break; | |
2590 | case IOPRIO_CLASS_IDLE: | |
2591 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; | |
2592 | cfqq->ioprio = 7; | |
2593 | cfq_clear_cfqq_idle_window(cfqq); | |
2594 | break; | |
22e2c507 JA |
2595 | } |
2596 | ||
2597 | /* | |
2598 | * keep track of original prio settings in case we have to temporarily | |
2599 | * elevate the priority of this queue | |
2600 | */ | |
2601 | cfqq->org_ioprio = cfqq->ioprio; | |
3b18152c | 2602 | cfq_clear_cfqq_prio_changed(cfqq); |
22e2c507 JA |
2603 | } |
2604 | ||
4f85cb96 | 2605 | static void changed_ioprio(struct cfq_io_cq *cic, struct bio *bio) |
22e2c507 | 2606 | { |
bca4b914 | 2607 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
478a82b0 | 2608 | struct cfq_queue *cfqq; |
35e6077c | 2609 | |
caaa5f9f JA |
2610 | if (unlikely(!cfqd)) |
2611 | return; | |
2612 | ||
ff6657c6 | 2613 | cfqq = cic->cfqq[BLK_RW_ASYNC]; |
caaa5f9f JA |
2614 | if (cfqq) { |
2615 | struct cfq_queue *new_cfqq; | |
c5869807 | 2616 | new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->icq.ioc, |
4f85cb96 | 2617 | bio, GFP_ATOMIC); |
caaa5f9f | 2618 | if (new_cfqq) { |
ff6657c6 | 2619 | cic->cfqq[BLK_RW_ASYNC] = new_cfqq; |
caaa5f9f JA |
2620 | cfq_put_queue(cfqq); |
2621 | } | |
22e2c507 | 2622 | } |
caaa5f9f | 2623 | |
ff6657c6 | 2624 | cfqq = cic->cfqq[BLK_RW_SYNC]; |
caaa5f9f JA |
2625 | if (cfqq) |
2626 | cfq_mark_cfqq_prio_changed(cfqq); | |
22e2c507 JA |
2627 | } |
2628 | ||
d5036d77 | 2629 | static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
a6151c3a | 2630 | pid_t pid, bool is_sync) |
d5036d77 JA |
2631 | { |
2632 | RB_CLEAR_NODE(&cfqq->rb_node); | |
2633 | RB_CLEAR_NODE(&cfqq->p_node); | |
2634 | INIT_LIST_HEAD(&cfqq->fifo); | |
2635 | ||
30d7b944 | 2636 | cfqq->ref = 0; |
d5036d77 JA |
2637 | cfqq->cfqd = cfqd; |
2638 | ||
2639 | cfq_mark_cfqq_prio_changed(cfqq); | |
2640 | ||
2641 | if (is_sync) { | |
2642 | if (!cfq_class_idle(cfqq)) | |
2643 | cfq_mark_cfqq_idle_window(cfqq); | |
2644 | cfq_mark_cfqq_sync(cfqq); | |
2645 | } | |
2646 | cfqq->pid = pid; | |
2647 | } | |
2648 | ||
24610333 | 2649 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
c5869807 | 2650 | static void changed_cgroup(struct cfq_io_cq *cic) |
24610333 VG |
2651 | { |
2652 | struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1); | |
bca4b914 | 2653 | struct cfq_data *cfqd = cic_to_cfqd(cic); |
24610333 VG |
2654 | struct request_queue *q; |
2655 | ||
2656 | if (unlikely(!cfqd)) | |
2657 | return; | |
2658 | ||
2659 | q = cfqd->queue; | |
2660 | ||
24610333 VG |
2661 | if (sync_cfqq) { |
2662 | /* | |
2663 | * Drop reference to sync queue. A new sync queue will be | |
2664 | * assigned in new group upon arrival of a fresh request. | |
2665 | */ | |
2666 | cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup"); | |
2667 | cic_set_cfqq(cic, NULL, 1); | |
2668 | cfq_put_queue(sync_cfqq); | |
2669 | } | |
24610333 | 2670 | } |
24610333 VG |
2671 | #endif /* CONFIG_CFQ_GROUP_IOSCHED */ |
2672 | ||
22e2c507 | 2673 | static struct cfq_queue * |
a6151c3a | 2674 | cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, |
4f85cb96 | 2675 | struct io_context *ioc, struct bio *bio, gfp_t gfp_mask) |
22e2c507 | 2676 | { |
0a5a7d0e | 2677 | struct blkio_cgroup *blkcg; |
22e2c507 | 2678 | struct cfq_queue *cfqq, *new_cfqq = NULL; |
c5869807 | 2679 | struct cfq_io_cq *cic; |
cdb16e8f | 2680 | struct cfq_group *cfqg; |
22e2c507 JA |
2681 | |
2682 | retry: | |
2a7f1244 TH |
2683 | rcu_read_lock(); |
2684 | ||
4f85cb96 | 2685 | blkcg = bio_blkio_cgroup(bio); |
0a5a7d0e | 2686 | |
cd1604fa TH |
2687 | cfqg = cfq_lookup_create_cfqg(cfqd, blkcg); |
2688 | ||
4ac845a2 | 2689 | cic = cfq_cic_lookup(cfqd, ioc); |
91fac317 VT |
2690 | /* cic always exists here */ |
2691 | cfqq = cic_to_cfqq(cic, is_sync); | |
22e2c507 | 2692 | |
6118b70b JA |
2693 | /* |
2694 | * Always try a new alloc if we fell back to the OOM cfqq | |
2695 | * originally, since it should just be a temporary situation. | |
2696 | */ | |
2697 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { | |
2698 | cfqq = NULL; | |
22e2c507 JA |
2699 | if (new_cfqq) { |
2700 | cfqq = new_cfqq; | |
2701 | new_cfqq = NULL; | |
2702 | } else if (gfp_mask & __GFP_WAIT) { | |
2a7f1244 | 2703 | rcu_read_unlock(); |
22e2c507 | 2704 | spin_unlock_irq(cfqd->queue->queue_lock); |
94f6030c | 2705 | new_cfqq = kmem_cache_alloc_node(cfq_pool, |
6118b70b | 2706 | gfp_mask | __GFP_ZERO, |
94f6030c | 2707 | cfqd->queue->node); |
22e2c507 | 2708 | spin_lock_irq(cfqd->queue->queue_lock); |
6118b70b JA |
2709 | if (new_cfqq) |
2710 | goto retry; | |
22e2c507 | 2711 | } else { |
94f6030c CL |
2712 | cfqq = kmem_cache_alloc_node(cfq_pool, |
2713 | gfp_mask | __GFP_ZERO, | |
2714 | cfqd->queue->node); | |
22e2c507 JA |
2715 | } |
2716 | ||
6118b70b JA |
2717 | if (cfqq) { |
2718 | cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync); | |
2719 | cfq_init_prio_data(cfqq, ioc); | |
cdb16e8f | 2720 | cfq_link_cfqq_cfqg(cfqq, cfqg); |
6118b70b JA |
2721 | cfq_log_cfqq(cfqd, cfqq, "alloced"); |
2722 | } else | |
2723 | cfqq = &cfqd->oom_cfqq; | |
22e2c507 JA |
2724 | } |
2725 | ||
2726 | if (new_cfqq) | |
2727 | kmem_cache_free(cfq_pool, new_cfqq); | |
2728 | ||
2a7f1244 | 2729 | rcu_read_unlock(); |
22e2c507 JA |
2730 | return cfqq; |
2731 | } | |
2732 | ||
c2dea2d1 VT |
2733 | static struct cfq_queue ** |
2734 | cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio) | |
2735 | { | |
fe094d98 | 2736 | switch (ioprio_class) { |
c2dea2d1 VT |
2737 | case IOPRIO_CLASS_RT: |
2738 | return &cfqd->async_cfqq[0][ioprio]; | |
2739 | case IOPRIO_CLASS_BE: | |
2740 | return &cfqd->async_cfqq[1][ioprio]; | |
2741 | case IOPRIO_CLASS_IDLE: | |
2742 | return &cfqd->async_idle_cfqq; | |
2743 | default: | |
2744 | BUG(); | |
2745 | } | |
2746 | } | |
2747 | ||
15c31be4 | 2748 | static struct cfq_queue * |
a6151c3a | 2749 | cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc, |
4f85cb96 | 2750 | struct bio *bio, gfp_t gfp_mask) |
15c31be4 | 2751 | { |
fd0928df JA |
2752 | const int ioprio = task_ioprio(ioc); |
2753 | const int ioprio_class = task_ioprio_class(ioc); | |
c2dea2d1 | 2754 | struct cfq_queue **async_cfqq = NULL; |
15c31be4 JA |
2755 | struct cfq_queue *cfqq = NULL; |
2756 | ||
c2dea2d1 VT |
2757 | if (!is_sync) { |
2758 | async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio); | |
2759 | cfqq = *async_cfqq; | |
2760 | } | |
2761 | ||
6118b70b | 2762 | if (!cfqq) |
4f85cb96 | 2763 | cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, bio, gfp_mask); |
15c31be4 JA |
2764 | |
2765 | /* | |
2766 | * pin the queue now that it's allocated, scheduler exit will prune it | |
2767 | */ | |
c2dea2d1 | 2768 | if (!is_sync && !(*async_cfqq)) { |
30d7b944 | 2769 | cfqq->ref++; |
c2dea2d1 | 2770 | *async_cfqq = cfqq; |
15c31be4 JA |
2771 | } |
2772 | ||
30d7b944 | 2773 | cfqq->ref++; |
15c31be4 JA |
2774 | return cfqq; |
2775 | } | |
2776 | ||
22e2c507 | 2777 | static void |
383cd721 | 2778 | __cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle) |
1da177e4 | 2779 | { |
383cd721 SL |
2780 | unsigned long elapsed = jiffies - ttime->last_end_request; |
2781 | elapsed = min(elapsed, 2UL * slice_idle); | |
db3b5848 | 2782 | |
383cd721 SL |
2783 | ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; |
2784 | ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8; | |
2785 | ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples; | |
2786 | } | |
2787 | ||
2788 | static void | |
2789 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
c5869807 | 2790 | struct cfq_io_cq *cic) |
383cd721 | 2791 | { |
f5f2b6ce | 2792 | if (cfq_cfqq_sync(cfqq)) { |
383cd721 | 2793 | __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle); |
f5f2b6ce SL |
2794 | __cfq_update_io_thinktime(&cfqq->service_tree->ttime, |
2795 | cfqd->cfq_slice_idle); | |
2796 | } | |
7700fc4f SL |
2797 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
2798 | __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle); | |
2799 | #endif | |
22e2c507 | 2800 | } |
1da177e4 | 2801 | |
206dc69b | 2802 | static void |
b2c18e1e | 2803 | cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
6d048f53 | 2804 | struct request *rq) |
206dc69b | 2805 | { |
3dde36dd | 2806 | sector_t sdist = 0; |
41647e7a | 2807 | sector_t n_sec = blk_rq_sectors(rq); |
3dde36dd CZ |
2808 | if (cfqq->last_request_pos) { |
2809 | if (cfqq->last_request_pos < blk_rq_pos(rq)) | |
2810 | sdist = blk_rq_pos(rq) - cfqq->last_request_pos; | |
2811 | else | |
2812 | sdist = cfqq->last_request_pos - blk_rq_pos(rq); | |
2813 | } | |
206dc69b | 2814 | |
3dde36dd | 2815 | cfqq->seek_history <<= 1; |
41647e7a CZ |
2816 | if (blk_queue_nonrot(cfqd->queue)) |
2817 | cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT); | |
2818 | else | |
2819 | cfqq->seek_history |= (sdist > CFQQ_SEEK_THR); | |
206dc69b | 2820 | } |
1da177e4 | 2821 | |
22e2c507 JA |
2822 | /* |
2823 | * Disable idle window if the process thinks too long or seeks so much that | |
2824 | * it doesn't matter | |
2825 | */ | |
2826 | static void | |
2827 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
c5869807 | 2828 | struct cfq_io_cq *cic) |
22e2c507 | 2829 | { |
7b679138 | 2830 | int old_idle, enable_idle; |
1be92f2f | 2831 | |
0871714e JA |
2832 | /* |
2833 | * Don't idle for async or idle io prio class | |
2834 | */ | |
2835 | if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq)) | |
1be92f2f JA |
2836 | return; |
2837 | ||
c265a7f4 | 2838 | enable_idle = old_idle = cfq_cfqq_idle_window(cfqq); |
1da177e4 | 2839 | |
76280aff CZ |
2840 | if (cfqq->queued[0] + cfqq->queued[1] >= 4) |
2841 | cfq_mark_cfqq_deep(cfqq); | |
2842 | ||
749ef9f8 CZ |
2843 | if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE)) |
2844 | enable_idle = 0; | |
f6e8d01b | 2845 | else if (!atomic_read(&cic->icq.ioc->active_ref) || |
c5869807 TH |
2846 | !cfqd->cfq_slice_idle || |
2847 | (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq))) | |
22e2c507 | 2848 | enable_idle = 0; |
383cd721 SL |
2849 | else if (sample_valid(cic->ttime.ttime_samples)) { |
2850 | if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle) | |
22e2c507 JA |
2851 | enable_idle = 0; |
2852 | else | |
2853 | enable_idle = 1; | |
1da177e4 LT |
2854 | } |
2855 | ||
7b679138 JA |
2856 | if (old_idle != enable_idle) { |
2857 | cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle); | |
2858 | if (enable_idle) | |
2859 | cfq_mark_cfqq_idle_window(cfqq); | |
2860 | else | |
2861 | cfq_clear_cfqq_idle_window(cfqq); | |
2862 | } | |
22e2c507 | 2863 | } |
1da177e4 | 2864 | |
22e2c507 JA |
2865 | /* |
2866 | * Check if new_cfqq should preempt the currently active queue. Return 0 for | |
2867 | * no or if we aren't sure, a 1 will cause a preempt. | |
2868 | */ | |
a6151c3a | 2869 | static bool |
22e2c507 | 2870 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, |
5e705374 | 2871 | struct request *rq) |
22e2c507 | 2872 | { |
6d048f53 | 2873 | struct cfq_queue *cfqq; |
22e2c507 | 2874 | |
6d048f53 JA |
2875 | cfqq = cfqd->active_queue; |
2876 | if (!cfqq) | |
a6151c3a | 2877 | return false; |
22e2c507 | 2878 | |
6d048f53 | 2879 | if (cfq_class_idle(new_cfqq)) |
a6151c3a | 2880 | return false; |
22e2c507 JA |
2881 | |
2882 | if (cfq_class_idle(cfqq)) | |
a6151c3a | 2883 | return true; |
1e3335de | 2884 | |
875feb63 DS |
2885 | /* |
2886 | * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice. | |
2887 | */ | |
2888 | if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq)) | |
2889 | return false; | |
2890 | ||
374f84ac JA |
2891 | /* |
2892 | * if the new request is sync, but the currently running queue is | |
2893 | * not, let the sync request have priority. | |
2894 | */ | |
5e705374 | 2895 | if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq)) |
a6151c3a | 2896 | return true; |
1e3335de | 2897 | |
8682e1f1 VG |
2898 | if (new_cfqq->cfqg != cfqq->cfqg) |
2899 | return false; | |
2900 | ||
2901 | if (cfq_slice_used(cfqq)) | |
2902 | return true; | |
2903 | ||
2904 | /* Allow preemption only if we are idling on sync-noidle tree */ | |
2905 | if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD && | |
2906 | cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD && | |
2907 | new_cfqq->service_tree->count == 2 && | |
2908 | RB_EMPTY_ROOT(&cfqq->sort_list)) | |
2909 | return true; | |
2910 | ||
b53d1ed7 JA |
2911 | /* |
2912 | * So both queues are sync. Let the new request get disk time if | |
2913 | * it's a metadata request and the current queue is doing regular IO. | |
2914 | */ | |
65299a3b | 2915 | if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending) |
b53d1ed7 JA |
2916 | return true; |
2917 | ||
3a9a3f6c DS |
2918 | /* |
2919 | * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice. | |
2920 | */ | |
2921 | if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq)) | |
a6151c3a | 2922 | return true; |
3a9a3f6c | 2923 | |
d2d59e18 SL |
2924 | /* An idle queue should not be idle now for some reason */ |
2925 | if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq)) | |
2926 | return true; | |
2927 | ||
1e3335de | 2928 | if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) |
a6151c3a | 2929 | return false; |
1e3335de JA |
2930 | |
2931 | /* | |
2932 | * if this request is as-good as one we would expect from the | |
2933 | * current cfqq, let it preempt | |
2934 | */ | |
e9ce335d | 2935 | if (cfq_rq_close(cfqd, cfqq, rq)) |
a6151c3a | 2936 | return true; |
1e3335de | 2937 | |
a6151c3a | 2938 | return false; |
22e2c507 JA |
2939 | } |
2940 | ||
2941 | /* | |
2942 | * cfqq preempts the active queue. if we allowed preempt with no slice left, | |
2943 | * let it have half of its nominal slice. | |
2944 | */ | |
2945 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
2946 | { | |
df0793ab SL |
2947 | enum wl_type_t old_type = cfqq_type(cfqd->active_queue); |
2948 | ||
7b679138 | 2949 | cfq_log_cfqq(cfqd, cfqq, "preempt"); |
df0793ab | 2950 | cfq_slice_expired(cfqd, 1); |
22e2c507 | 2951 | |
f8ae6e3e SL |
2952 | /* |
2953 | * workload type is changed, don't save slice, otherwise preempt | |
2954 | * doesn't happen | |
2955 | */ | |
df0793ab | 2956 | if (old_type != cfqq_type(cfqq)) |
f8ae6e3e SL |
2957 | cfqq->cfqg->saved_workload_slice = 0; |
2958 | ||
bf572256 JA |
2959 | /* |
2960 | * Put the new queue at the front of the of the current list, | |
2961 | * so we know that it will be selected next. | |
2962 | */ | |
2963 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); | |
edd75ffd JA |
2964 | |
2965 | cfq_service_tree_add(cfqd, cfqq, 1); | |
eda5e0c9 | 2966 | |
62a37f6b JT |
2967 | cfqq->slice_end = 0; |
2968 | cfq_mark_cfqq_slice_new(cfqq); | |
22e2c507 JA |
2969 | } |
2970 | ||
22e2c507 | 2971 | /* |
5e705374 | 2972 | * Called when a new fs request (rq) is added (to cfqq). Check if there's |
22e2c507 JA |
2973 | * something we should do about it |
2974 | */ | |
2975 | static void | |
5e705374 JA |
2976 | cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
2977 | struct request *rq) | |
22e2c507 | 2978 | { |
c5869807 | 2979 | struct cfq_io_cq *cic = RQ_CIC(rq); |
12e9fddd | 2980 | |
45333d5a | 2981 | cfqd->rq_queued++; |
65299a3b CH |
2982 | if (rq->cmd_flags & REQ_PRIO) |
2983 | cfqq->prio_pending++; | |
374f84ac | 2984 | |
383cd721 | 2985 | cfq_update_io_thinktime(cfqd, cfqq, cic); |
b2c18e1e | 2986 | cfq_update_io_seektime(cfqd, cfqq, rq); |
9c2c38a1 JA |
2987 | cfq_update_idle_window(cfqd, cfqq, cic); |
2988 | ||
b2c18e1e | 2989 | cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); |
22e2c507 JA |
2990 | |
2991 | if (cfqq == cfqd->active_queue) { | |
2992 | /* | |
b029195d JA |
2993 | * Remember that we saw a request from this process, but |
2994 | * don't start queuing just yet. Otherwise we risk seeing lots | |
2995 | * of tiny requests, because we disrupt the normal plugging | |
d6ceb25e JA |
2996 | * and merging. If the request is already larger than a single |
2997 | * page, let it rip immediately. For that case we assume that | |
2d870722 JA |
2998 | * merging is already done. Ditto for a busy system that |
2999 | * has other work pending, don't risk delaying until the | |
3000 | * idle timer unplug to continue working. | |
22e2c507 | 3001 | */ |
d6ceb25e | 3002 | if (cfq_cfqq_wait_request(cfqq)) { |
2d870722 JA |
3003 | if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE || |
3004 | cfqd->busy_queues > 1) { | |
812df48d | 3005 | cfq_del_timer(cfqd, cfqq); |
554554f6 | 3006 | cfq_clear_cfqq_wait_request(cfqq); |
24ecfbe2 | 3007 | __blk_run_queue(cfqd->queue); |
a11cdaa7 | 3008 | } else { |
e98ef89b | 3009 | cfq_blkiocg_update_idle_time_stats( |
c1768268 TH |
3010 | cfqg_to_blkg(cfqq->cfqg), |
3011 | &blkio_policy_cfq); | |
bf791937 | 3012 | cfq_mark_cfqq_must_dispatch(cfqq); |
a11cdaa7 | 3013 | } |
d6ceb25e | 3014 | } |
5e705374 | 3015 | } else if (cfq_should_preempt(cfqd, cfqq, rq)) { |
22e2c507 JA |
3016 | /* |
3017 | * not the active queue - expire current slice if it is | |
3018 | * idle and has expired it's mean thinktime or this new queue | |
3a9a3f6c DS |
3019 | * has some old slice time left and is of higher priority or |
3020 | * this new queue is RT and the current one is BE | |
22e2c507 JA |
3021 | */ |
3022 | cfq_preempt_queue(cfqd, cfqq); | |
24ecfbe2 | 3023 | __blk_run_queue(cfqd->queue); |
22e2c507 | 3024 | } |
1da177e4 LT |
3025 | } |
3026 | ||
165125e1 | 3027 | static void cfq_insert_request(struct request_queue *q, struct request *rq) |
1da177e4 | 3028 | { |
b4878f24 | 3029 | struct cfq_data *cfqd = q->elevator->elevator_data; |
5e705374 | 3030 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
22e2c507 | 3031 | |
7b679138 | 3032 | cfq_log_cfqq(cfqd, cfqq, "insert_request"); |
c5869807 | 3033 | cfq_init_prio_data(cfqq, RQ_CIC(rq)->icq.ioc); |
1da177e4 | 3034 | |
30996f40 | 3035 | rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]); |
22e2c507 | 3036 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
aa6f6a3d | 3037 | cfq_add_rq_rb(rq); |
0381411e | 3038 | cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)), |
c1768268 | 3039 | &blkio_policy_cfq, |
0381411e TH |
3040 | cfqg_to_blkg(cfqd->serving_group), |
3041 | rq_data_dir(rq), rq_is_sync(rq)); | |
5e705374 | 3042 | cfq_rq_enqueued(cfqd, cfqq, rq); |
1da177e4 LT |
3043 | } |
3044 | ||
45333d5a AC |
3045 | /* |
3046 | * Update hw_tag based on peak queue depth over 50 samples under | |
3047 | * sufficient load. | |
3048 | */ | |
3049 | static void cfq_update_hw_tag(struct cfq_data *cfqd) | |
3050 | { | |
1a1238a7 SL |
3051 | struct cfq_queue *cfqq = cfqd->active_queue; |
3052 | ||
53c583d2 CZ |
3053 | if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth) |
3054 | cfqd->hw_tag_est_depth = cfqd->rq_in_driver; | |
e459dd08 CZ |
3055 | |
3056 | if (cfqd->hw_tag == 1) | |
3057 | return; | |
45333d5a AC |
3058 | |
3059 | if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN && | |
53c583d2 | 3060 | cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN) |
45333d5a AC |
3061 | return; |
3062 | ||
1a1238a7 SL |
3063 | /* |
3064 | * If active queue hasn't enough requests and can idle, cfq might not | |
3065 | * dispatch sufficient requests to hardware. Don't zero hw_tag in this | |
3066 | * case | |
3067 | */ | |
3068 | if (cfqq && cfq_cfqq_idle_window(cfqq) && | |
3069 | cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] < | |
53c583d2 | 3070 | CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN) |
1a1238a7 SL |
3071 | return; |
3072 | ||
45333d5a AC |
3073 | if (cfqd->hw_tag_samples++ < 50) |
3074 | return; | |
3075 | ||
e459dd08 | 3076 | if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN) |
45333d5a AC |
3077 | cfqd->hw_tag = 1; |
3078 | else | |
3079 | cfqd->hw_tag = 0; | |
45333d5a AC |
3080 | } |
3081 | ||
7667aa06 VG |
3082 | static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
3083 | { | |
c5869807 | 3084 | struct cfq_io_cq *cic = cfqd->active_cic; |
7667aa06 | 3085 | |
02a8f01b JT |
3086 | /* If the queue already has requests, don't wait */ |
3087 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) | |
3088 | return false; | |
3089 | ||
7667aa06 VG |
3090 | /* If there are other queues in the group, don't wait */ |
3091 | if (cfqq->cfqg->nr_cfqq > 1) | |
3092 | return false; | |
3093 | ||
7700fc4f SL |
3094 | /* the only queue in the group, but think time is big */ |
3095 | if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) | |
3096 | return false; | |
3097 | ||
7667aa06 VG |
3098 | if (cfq_slice_used(cfqq)) |
3099 | return true; | |
3100 | ||
3101 | /* if slice left is less than think time, wait busy */ | |
383cd721 SL |
3102 | if (cic && sample_valid(cic->ttime.ttime_samples) |
3103 | && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) | |
7667aa06 VG |
3104 | return true; |
3105 | ||
3106 | /* | |
3107 | * If think times is less than a jiffy than ttime_mean=0 and above | |
3108 | * will not be true. It might happen that slice has not expired yet | |
3109 | * but will expire soon (4-5 ns) during select_queue(). To cover the | |
3110 | * case where think time is less than a jiffy, mark the queue wait | |
3111 | * busy if only 1 jiffy is left in the slice. | |
3112 | */ | |
3113 | if (cfqq->slice_end - jiffies == 1) | |
3114 | return true; | |
3115 | ||
3116 | return false; | |
3117 | } | |
3118 | ||
165125e1 | 3119 | static void cfq_completed_request(struct request_queue *q, struct request *rq) |
1da177e4 | 3120 | { |
5e705374 | 3121 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
b4878f24 | 3122 | struct cfq_data *cfqd = cfqq->cfqd; |
5380a101 | 3123 | const int sync = rq_is_sync(rq); |
b4878f24 | 3124 | unsigned long now; |
1da177e4 | 3125 | |
b4878f24 | 3126 | now = jiffies; |
33659ebb CH |
3127 | cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", |
3128 | !!(rq->cmd_flags & REQ_NOIDLE)); | |
1da177e4 | 3129 | |
45333d5a AC |
3130 | cfq_update_hw_tag(cfqd); |
3131 | ||
53c583d2 | 3132 | WARN_ON(!cfqd->rq_in_driver); |
6d048f53 | 3133 | WARN_ON(!cfqq->dispatched); |
53c583d2 | 3134 | cfqd->rq_in_driver--; |
6d048f53 | 3135 | cfqq->dispatched--; |
80bdf0c7 | 3136 | (RQ_CFQG(rq))->dispatched--; |
0381411e | 3137 | cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg), |
c1768268 TH |
3138 | &blkio_policy_cfq, rq_start_time_ns(rq), |
3139 | rq_io_start_time_ns(rq), rq_data_dir(rq), | |
3140 | rq_is_sync(rq)); | |
1da177e4 | 3141 | |
53c583d2 | 3142 | cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--; |
3ed9a296 | 3143 | |
365722bb | 3144 | if (sync) { |
f5f2b6ce SL |
3145 | struct cfq_rb_root *service_tree; |
3146 | ||
383cd721 | 3147 | RQ_CIC(rq)->ttime.last_end_request = now; |
f5f2b6ce SL |
3148 | |
3149 | if (cfq_cfqq_on_rr(cfqq)) | |
3150 | service_tree = cfqq->service_tree; | |
3151 | else | |
3152 | service_tree = service_tree_for(cfqq->cfqg, | |
3153 | cfqq_prio(cfqq), cfqq_type(cfqq)); | |
3154 | service_tree->ttime.last_end_request = now; | |
573412b2 CZ |
3155 | if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now)) |
3156 | cfqd->last_delayed_sync = now; | |
365722bb | 3157 | } |
caaa5f9f | 3158 | |
7700fc4f SL |
3159 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
3160 | cfqq->cfqg->ttime.last_end_request = now; | |
3161 | #endif | |
3162 | ||
caaa5f9f JA |
3163 | /* |
3164 | * If this is the active queue, check if it needs to be expired, | |
3165 | * or if we want to idle in case it has no pending requests. | |
3166 | */ | |
3167 | if (cfqd->active_queue == cfqq) { | |
a36e71f9 JA |
3168 | const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list); |
3169 | ||
44f7c160 JA |
3170 | if (cfq_cfqq_slice_new(cfqq)) { |
3171 | cfq_set_prio_slice(cfqd, cfqq); | |
3172 | cfq_clear_cfqq_slice_new(cfqq); | |
3173 | } | |
f75edf2d VG |
3174 | |
3175 | /* | |
7667aa06 VG |
3176 | * Should we wait for next request to come in before we expire |
3177 | * the queue. | |
f75edf2d | 3178 | */ |
7667aa06 | 3179 | if (cfq_should_wait_busy(cfqd, cfqq)) { |
80bdf0c7 VG |
3180 | unsigned long extend_sl = cfqd->cfq_slice_idle; |
3181 | if (!cfqd->cfq_slice_idle) | |
3182 | extend_sl = cfqd->cfq_group_idle; | |
3183 | cfqq->slice_end = jiffies + extend_sl; | |
f75edf2d | 3184 | cfq_mark_cfqq_wait_busy(cfqq); |
b1ffe737 | 3185 | cfq_log_cfqq(cfqd, cfqq, "will busy wait"); |
f75edf2d VG |
3186 | } |
3187 | ||
a36e71f9 | 3188 | /* |
8e550632 CZ |
3189 | * Idling is not enabled on: |
3190 | * - expired queues | |
3191 | * - idle-priority queues | |
3192 | * - async queues | |
3193 | * - queues with still some requests queued | |
3194 | * - when there is a close cooperator | |
a36e71f9 | 3195 | */ |
0871714e | 3196 | if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq)) |
e5ff082e | 3197 | cfq_slice_expired(cfqd, 1); |
8e550632 CZ |
3198 | else if (sync && cfqq_empty && |
3199 | !cfq_close_cooperator(cfqd, cfqq)) { | |
749ef9f8 | 3200 | cfq_arm_slice_timer(cfqd); |
8e550632 | 3201 | } |
caaa5f9f | 3202 | } |
6d048f53 | 3203 | |
53c583d2 | 3204 | if (!cfqd->rq_in_driver) |
23e018a1 | 3205 | cfq_schedule_dispatch(cfqd); |
1da177e4 LT |
3206 | } |
3207 | ||
89850f7e | 3208 | static inline int __cfq_may_queue(struct cfq_queue *cfqq) |
22e2c507 | 3209 | { |
1b379d8d | 3210 | if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) { |
3b18152c | 3211 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
22e2c507 | 3212 | return ELV_MQUEUE_MUST; |
3b18152c | 3213 | } |
1da177e4 | 3214 | |
22e2c507 | 3215 | return ELV_MQUEUE_MAY; |
22e2c507 JA |
3216 | } |
3217 | ||
165125e1 | 3218 | static int cfq_may_queue(struct request_queue *q, int rw) |
22e2c507 JA |
3219 | { |
3220 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
3221 | struct task_struct *tsk = current; | |
c5869807 | 3222 | struct cfq_io_cq *cic; |
22e2c507 JA |
3223 | struct cfq_queue *cfqq; |
3224 | ||
3225 | /* | |
3226 | * don't force setup of a queue from here, as a call to may_queue | |
3227 | * does not necessarily imply that a request actually will be queued. | |
3228 | * so just lookup a possibly existing queue, or return 'may queue' | |
3229 | * if that fails | |
3230 | */ | |
4ac845a2 | 3231 | cic = cfq_cic_lookup(cfqd, tsk->io_context); |
91fac317 VT |
3232 | if (!cic) |
3233 | return ELV_MQUEUE_MAY; | |
3234 | ||
b0b78f81 | 3235 | cfqq = cic_to_cfqq(cic, rw_is_sync(rw)); |
22e2c507 | 3236 | if (cfqq) { |
c5869807 | 3237 | cfq_init_prio_data(cfqq, cic->icq.ioc); |
22e2c507 | 3238 | |
89850f7e | 3239 | return __cfq_may_queue(cfqq); |
22e2c507 JA |
3240 | } |
3241 | ||
3242 | return ELV_MQUEUE_MAY; | |
1da177e4 LT |
3243 | } |
3244 | ||
1da177e4 LT |
3245 | /* |
3246 | * queue lock held here | |
3247 | */ | |
bb37b94c | 3248 | static void cfq_put_request(struct request *rq) |
1da177e4 | 3249 | { |
5e705374 | 3250 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
1da177e4 | 3251 | |
5e705374 | 3252 | if (cfqq) { |
22e2c507 | 3253 | const int rw = rq_data_dir(rq); |
1da177e4 | 3254 | |
22e2c507 JA |
3255 | BUG_ON(!cfqq->allocated[rw]); |
3256 | cfqq->allocated[rw]--; | |
1da177e4 | 3257 | |
7f1dc8a2 | 3258 | /* Put down rq reference on cfqg */ |
1adaf3dd | 3259 | blkg_put(cfqg_to_blkg(RQ_CFQG(rq))); |
a612fddf TH |
3260 | rq->elv.priv[0] = NULL; |
3261 | rq->elv.priv[1] = NULL; | |
7f1dc8a2 | 3262 | |
1da177e4 LT |
3263 | cfq_put_queue(cfqq); |
3264 | } | |
3265 | } | |
3266 | ||
df5fe3e8 | 3267 | static struct cfq_queue * |
c5869807 | 3268 | cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic, |
df5fe3e8 JM |
3269 | struct cfq_queue *cfqq) |
3270 | { | |
3271 | cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq); | |
3272 | cic_set_cfqq(cic, cfqq->new_cfqq, 1); | |
b3b6d040 | 3273 | cfq_mark_cfqq_coop(cfqq->new_cfqq); |
df5fe3e8 JM |
3274 | cfq_put_queue(cfqq); |
3275 | return cic_to_cfqq(cic, 1); | |
3276 | } | |
3277 | ||
e6c5bc73 JM |
3278 | /* |
3279 | * Returns NULL if a new cfqq should be allocated, or the old cfqq if this | |
3280 | * was the last process referring to said cfqq. | |
3281 | */ | |
3282 | static struct cfq_queue * | |
c5869807 | 3283 | split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq) |
e6c5bc73 JM |
3284 | { |
3285 | if (cfqq_process_refs(cfqq) == 1) { | |
e6c5bc73 JM |
3286 | cfqq->pid = current->pid; |
3287 | cfq_clear_cfqq_coop(cfqq); | |
ae54abed | 3288 | cfq_clear_cfqq_split_coop(cfqq); |
e6c5bc73 JM |
3289 | return cfqq; |
3290 | } | |
3291 | ||
3292 | cic_set_cfqq(cic, NULL, 1); | |
d02a2c07 SL |
3293 | |
3294 | cfq_put_cooperator(cfqq); | |
3295 | ||
e6c5bc73 JM |
3296 | cfq_put_queue(cfqq); |
3297 | return NULL; | |
3298 | } | |
1da177e4 | 3299 | /* |
22e2c507 | 3300 | * Allocate cfq data structures associated with this request. |
1da177e4 | 3301 | */ |
22e2c507 | 3302 | static int |
852c788f TH |
3303 | cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio, |
3304 | gfp_t gfp_mask) | |
1da177e4 LT |
3305 | { |
3306 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
f1f8cc94 | 3307 | struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq); |
1da177e4 | 3308 | const int rw = rq_data_dir(rq); |
a6151c3a | 3309 | const bool is_sync = rq_is_sync(rq); |
22e2c507 | 3310 | struct cfq_queue *cfqq; |
d705ae6b | 3311 | unsigned int changed; |
1da177e4 LT |
3312 | |
3313 | might_sleep_if(gfp_mask & __GFP_WAIT); | |
3314 | ||
216284c3 | 3315 | spin_lock_irq(q->queue_lock); |
f1f8cc94 TH |
3316 | |
3317 | /* handle changed notifications */ | |
d705ae6b TH |
3318 | changed = icq_get_changed(&cic->icq); |
3319 | if (unlikely(changed & ICQ_IOPRIO_CHANGED)) | |
4f85cb96 | 3320 | changed_ioprio(cic, bio); |
f1f8cc94 | 3321 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
d705ae6b TH |
3322 | if (unlikely(changed & ICQ_CGROUP_CHANGED)) |
3323 | changed_cgroup(cic); | |
f1f8cc94 | 3324 | #endif |
22e2c507 | 3325 | |
e6c5bc73 | 3326 | new_queue: |
91fac317 | 3327 | cfqq = cic_to_cfqq(cic, is_sync); |
32f2e807 | 3328 | if (!cfqq || cfqq == &cfqd->oom_cfqq) { |
4f85cb96 | 3329 | cfqq = cfq_get_queue(cfqd, is_sync, cic->icq.ioc, bio, gfp_mask); |
91fac317 | 3330 | cic_set_cfqq(cic, cfqq, is_sync); |
df5fe3e8 | 3331 | } else { |
e6c5bc73 JM |
3332 | /* |
3333 | * If the queue was seeky for too long, break it apart. | |
3334 | */ | |
ae54abed | 3335 | if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) { |
e6c5bc73 JM |
3336 | cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq"); |
3337 | cfqq = split_cfqq(cic, cfqq); | |
3338 | if (!cfqq) | |
3339 | goto new_queue; | |
3340 | } | |
3341 | ||
df5fe3e8 JM |
3342 | /* |
3343 | * Check to see if this queue is scheduled to merge with | |
3344 | * another, closely cooperating queue. The merging of | |
3345 | * queues happens here as it must be done in process context. | |
3346 | * The reference on new_cfqq was taken in merge_cfqqs. | |
3347 | */ | |
3348 | if (cfqq->new_cfqq) | |
3349 | cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq); | |
91fac317 | 3350 | } |
1da177e4 LT |
3351 | |
3352 | cfqq->allocated[rw]++; | |
1da177e4 | 3353 | |
6fae9c25 | 3354 | cfqq->ref++; |
1adaf3dd | 3355 | blkg_get(cfqg_to_blkg(cfqq->cfqg)); |
a612fddf | 3356 | rq->elv.priv[0] = cfqq; |
1adaf3dd | 3357 | rq->elv.priv[1] = cfqq->cfqg; |
216284c3 | 3358 | spin_unlock_irq(q->queue_lock); |
5e705374 | 3359 | return 0; |
1da177e4 LT |
3360 | } |
3361 | ||
65f27f38 | 3362 | static void cfq_kick_queue(struct work_struct *work) |
22e2c507 | 3363 | { |
65f27f38 | 3364 | struct cfq_data *cfqd = |
23e018a1 | 3365 | container_of(work, struct cfq_data, unplug_work); |
165125e1 | 3366 | struct request_queue *q = cfqd->queue; |
22e2c507 | 3367 | |
40bb54d1 | 3368 | spin_lock_irq(q->queue_lock); |
24ecfbe2 | 3369 | __blk_run_queue(cfqd->queue); |
40bb54d1 | 3370 | spin_unlock_irq(q->queue_lock); |
22e2c507 JA |
3371 | } |
3372 | ||
3373 | /* | |
3374 | * Timer running if the active_queue is currently idling inside its time slice | |
3375 | */ | |
3376 | static void cfq_idle_slice_timer(unsigned long data) | |
3377 | { | |
3378 | struct cfq_data *cfqd = (struct cfq_data *) data; | |
3379 | struct cfq_queue *cfqq; | |
3380 | unsigned long flags; | |
3c6bd2f8 | 3381 | int timed_out = 1; |
22e2c507 | 3382 | |
7b679138 JA |
3383 | cfq_log(cfqd, "idle timer fired"); |
3384 | ||
22e2c507 JA |
3385 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
3386 | ||
fe094d98 JA |
3387 | cfqq = cfqd->active_queue; |
3388 | if (cfqq) { | |
3c6bd2f8 JA |
3389 | timed_out = 0; |
3390 | ||
b029195d JA |
3391 | /* |
3392 | * We saw a request before the queue expired, let it through | |
3393 | */ | |
3394 | if (cfq_cfqq_must_dispatch(cfqq)) | |
3395 | goto out_kick; | |
3396 | ||
22e2c507 JA |
3397 | /* |
3398 | * expired | |
3399 | */ | |
44f7c160 | 3400 | if (cfq_slice_used(cfqq)) |
22e2c507 JA |
3401 | goto expire; |
3402 | ||
3403 | /* | |
3404 | * only expire and reinvoke request handler, if there are | |
3405 | * other queues with pending requests | |
3406 | */ | |
caaa5f9f | 3407 | if (!cfqd->busy_queues) |
22e2c507 | 3408 | goto out_cont; |
22e2c507 JA |
3409 | |
3410 | /* | |
3411 | * not expired and it has a request pending, let it dispatch | |
3412 | */ | |
75e50984 | 3413 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
22e2c507 | 3414 | goto out_kick; |
76280aff CZ |
3415 | |
3416 | /* | |
3417 | * Queue depth flag is reset only when the idle didn't succeed | |
3418 | */ | |
3419 | cfq_clear_cfqq_deep(cfqq); | |
22e2c507 JA |
3420 | } |
3421 | expire: | |
e5ff082e | 3422 | cfq_slice_expired(cfqd, timed_out); |
22e2c507 | 3423 | out_kick: |
23e018a1 | 3424 | cfq_schedule_dispatch(cfqd); |
22e2c507 JA |
3425 | out_cont: |
3426 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); | |
3427 | } | |
3428 | ||
3b18152c JA |
3429 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
3430 | { | |
3431 | del_timer_sync(&cfqd->idle_slice_timer); | |
23e018a1 | 3432 | cancel_work_sync(&cfqd->unplug_work); |
3b18152c | 3433 | } |
22e2c507 | 3434 | |
c2dea2d1 VT |
3435 | static void cfq_put_async_queues(struct cfq_data *cfqd) |
3436 | { | |
3437 | int i; | |
3438 | ||
3439 | for (i = 0; i < IOPRIO_BE_NR; i++) { | |
3440 | if (cfqd->async_cfqq[0][i]) | |
3441 | cfq_put_queue(cfqd->async_cfqq[0][i]); | |
3442 | if (cfqd->async_cfqq[1][i]) | |
3443 | cfq_put_queue(cfqd->async_cfqq[1][i]); | |
c2dea2d1 | 3444 | } |
2389d1ef ON |
3445 | |
3446 | if (cfqd->async_idle_cfqq) | |
3447 | cfq_put_queue(cfqd->async_idle_cfqq); | |
c2dea2d1 VT |
3448 | } |
3449 | ||
b374d18a | 3450 | static void cfq_exit_queue(struct elevator_queue *e) |
1da177e4 | 3451 | { |
22e2c507 | 3452 | struct cfq_data *cfqd = e->elevator_data; |
165125e1 | 3453 | struct request_queue *q = cfqd->queue; |
22e2c507 | 3454 | |
3b18152c | 3455 | cfq_shutdown_timer_wq(cfqd); |
e2d74ac0 | 3456 | |
d9ff4187 | 3457 | spin_lock_irq(q->queue_lock); |
e2d74ac0 | 3458 | |
d9ff4187 | 3459 | if (cfqd->active_queue) |
e5ff082e | 3460 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0); |
e2d74ac0 | 3461 | |
c2dea2d1 | 3462 | cfq_put_async_queues(cfqd); |
03aa264a TH |
3463 | |
3464 | spin_unlock_irq(q->queue_lock); | |
3465 | ||
a90d742e AV |
3466 | cfq_shutdown_timer_wq(cfqd); |
3467 | ||
f51b802c TH |
3468 | #ifndef CONFIG_CFQ_GROUP_IOSCHED |
3469 | kfree(cfqd->root_group); | |
2abae55f | 3470 | #endif |
e8989fae | 3471 | update_root_blkg_pd(q, BLKIO_POLICY_PROP); |
56edf7d7 | 3472 | kfree(cfqd); |
1da177e4 LT |
3473 | } |
3474 | ||
b2fab5ac | 3475 | static int cfq_init_queue(struct request_queue *q) |
1da177e4 LT |
3476 | { |
3477 | struct cfq_data *cfqd; | |
cd1604fa | 3478 | struct blkio_group *blkg __maybe_unused; |
f51b802c | 3479 | int i; |
1da177e4 | 3480 | |
94f6030c | 3481 | cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node); |
a73f730d | 3482 | if (!cfqd) |
b2fab5ac | 3483 | return -ENOMEM; |
80b15c73 | 3484 | |
f51b802c TH |
3485 | cfqd->queue = q; |
3486 | q->elevator->elevator_data = cfqd; | |
3487 | ||
1fa8f6d6 VG |
3488 | /* Init root service tree */ |
3489 | cfqd->grp_service_tree = CFQ_RB_ROOT; | |
3490 | ||
f51b802c | 3491 | /* Init root group and prefer root group over other groups by default */ |
25fb5169 | 3492 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
f51b802c TH |
3493 | rcu_read_lock(); |
3494 | spin_lock_irq(q->queue_lock); | |
5624a4e4 | 3495 | |
cd1604fa TH |
3496 | blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_PROP, |
3497 | true); | |
3498 | if (!IS_ERR(blkg)) | |
0381411e | 3499 | cfqd->root_group = blkg_to_cfqg(blkg); |
f51b802c TH |
3500 | |
3501 | spin_unlock_irq(q->queue_lock); | |
3502 | rcu_read_unlock(); | |
3503 | #else | |
3504 | cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group), | |
3505 | GFP_KERNEL, cfqd->queue->node); | |
3506 | if (cfqd->root_group) | |
3507 | cfq_init_cfqg_base(cfqd->root_group); | |
3508 | #endif | |
3509 | if (!cfqd->root_group) { | |
5624a4e4 | 3510 | kfree(cfqd); |
b2fab5ac | 3511 | return -ENOMEM; |
5624a4e4 VG |
3512 | } |
3513 | ||
f51b802c | 3514 | cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT; |
5624a4e4 | 3515 | |
26a2ac00 JA |
3516 | /* |
3517 | * Not strictly needed (since RB_ROOT just clears the node and we | |
3518 | * zeroed cfqd on alloc), but better be safe in case someone decides | |
3519 | * to add magic to the rb code | |
3520 | */ | |
3521 | for (i = 0; i < CFQ_PRIO_LISTS; i++) | |
3522 | cfqd->prio_trees[i] = RB_ROOT; | |
3523 | ||
6118b70b JA |
3524 | /* |
3525 | * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues. | |
3526 | * Grab a permanent reference to it, so that the normal code flow | |
f51b802c TH |
3527 | * will not attempt to free it. oom_cfqq is linked to root_group |
3528 | * but shouldn't hold a reference as it'll never be unlinked. Lose | |
3529 | * the reference from linking right away. | |
6118b70b JA |
3530 | */ |
3531 | cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0); | |
30d7b944 | 3532 | cfqd->oom_cfqq.ref++; |
1adaf3dd TH |
3533 | |
3534 | spin_lock_irq(q->queue_lock); | |
f51b802c | 3535 | cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group); |
1adaf3dd TH |
3536 | blkg_put(cfqg_to_blkg(cfqd->root_group)); |
3537 | spin_unlock_irq(q->queue_lock); | |
1da177e4 | 3538 | |
22e2c507 JA |
3539 | init_timer(&cfqd->idle_slice_timer); |
3540 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; | |
3541 | cfqd->idle_slice_timer.data = (unsigned long) cfqd; | |
3542 | ||
23e018a1 | 3543 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); |
22e2c507 | 3544 | |
1da177e4 | 3545 | cfqd->cfq_quantum = cfq_quantum; |
22e2c507 JA |
3546 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
3547 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; | |
1da177e4 LT |
3548 | cfqd->cfq_back_max = cfq_back_max; |
3549 | cfqd->cfq_back_penalty = cfq_back_penalty; | |
22e2c507 JA |
3550 | cfqd->cfq_slice[0] = cfq_slice_async; |
3551 | cfqd->cfq_slice[1] = cfq_slice_sync; | |
3552 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; | |
3553 | cfqd->cfq_slice_idle = cfq_slice_idle; | |
80bdf0c7 | 3554 | cfqd->cfq_group_idle = cfq_group_idle; |
963b72fc | 3555 | cfqd->cfq_latency = 1; |
e459dd08 | 3556 | cfqd->hw_tag = -1; |
edc71131 CZ |
3557 | /* |
3558 | * we optimistically start assuming sync ops weren't delayed in last | |
3559 | * second, in order to have larger depth for async operations. | |
3560 | */ | |
573412b2 | 3561 | cfqd->last_delayed_sync = jiffies - HZ; |
b2fab5ac | 3562 | return 0; |
1da177e4 LT |
3563 | } |
3564 | ||
1da177e4 LT |
3565 | /* |
3566 | * sysfs parts below --> | |
3567 | */ | |
1da177e4 LT |
3568 | static ssize_t |
3569 | cfq_var_show(unsigned int var, char *page) | |
3570 | { | |
3571 | return sprintf(page, "%d\n", var); | |
3572 | } | |
3573 | ||
3574 | static ssize_t | |
3575 | cfq_var_store(unsigned int *var, const char *page, size_t count) | |
3576 | { | |
3577 | char *p = (char *) page; | |
3578 | ||
3579 | *var = simple_strtoul(p, &p, 10); | |
3580 | return count; | |
3581 | } | |
3582 | ||
1da177e4 | 3583 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
b374d18a | 3584 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ |
1da177e4 | 3585 | { \ |
3d1ab40f | 3586 | struct cfq_data *cfqd = e->elevator_data; \ |
1da177e4 LT |
3587 | unsigned int __data = __VAR; \ |
3588 | if (__CONV) \ | |
3589 | __data = jiffies_to_msecs(__data); \ | |
3590 | return cfq_var_show(__data, (page)); \ | |
3591 | } | |
3592 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); | |
22e2c507 JA |
3593 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
3594 | SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); | |
e572ec7e AV |
3595 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
3596 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); | |
22e2c507 | 3597 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
80bdf0c7 | 3598 | SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1); |
22e2c507 JA |
3599 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); |
3600 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); | |
3601 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); | |
963b72fc | 3602 | SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0); |
1da177e4 LT |
3603 | #undef SHOW_FUNCTION |
3604 | ||
3605 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ | |
b374d18a | 3606 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \ |
1da177e4 | 3607 | { \ |
3d1ab40f | 3608 | struct cfq_data *cfqd = e->elevator_data; \ |
1da177e4 LT |
3609 | unsigned int __data; \ |
3610 | int ret = cfq_var_store(&__data, (page), count); \ | |
3611 | if (__data < (MIN)) \ | |
3612 | __data = (MIN); \ | |
3613 | else if (__data > (MAX)) \ | |
3614 | __data = (MAX); \ | |
3615 | if (__CONV) \ | |
3616 | *(__PTR) = msecs_to_jiffies(__data); \ | |
3617 | else \ | |
3618 | *(__PTR) = __data; \ | |
3619 | return ret; \ | |
3620 | } | |
3621 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); | |
fe094d98 JA |
3622 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, |
3623 | UINT_MAX, 1); | |
3624 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, | |
3625 | UINT_MAX, 1); | |
e572ec7e | 3626 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
fe094d98 JA |
3627 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, |
3628 | UINT_MAX, 0); | |
22e2c507 | 3629 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
80bdf0c7 | 3630 | STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1); |
22e2c507 JA |
3631 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); |
3632 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); | |
fe094d98 JA |
3633 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, |
3634 | UINT_MAX, 0); | |
963b72fc | 3635 | STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0); |
1da177e4 LT |
3636 | #undef STORE_FUNCTION |
3637 | ||
e572ec7e AV |
3638 | #define CFQ_ATTR(name) \ |
3639 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) | |
3640 | ||
3641 | static struct elv_fs_entry cfq_attrs[] = { | |
3642 | CFQ_ATTR(quantum), | |
e572ec7e AV |
3643 | CFQ_ATTR(fifo_expire_sync), |
3644 | CFQ_ATTR(fifo_expire_async), | |
3645 | CFQ_ATTR(back_seek_max), | |
3646 | CFQ_ATTR(back_seek_penalty), | |
3647 | CFQ_ATTR(slice_sync), | |
3648 | CFQ_ATTR(slice_async), | |
3649 | CFQ_ATTR(slice_async_rq), | |
3650 | CFQ_ATTR(slice_idle), | |
80bdf0c7 | 3651 | CFQ_ATTR(group_idle), |
963b72fc | 3652 | CFQ_ATTR(low_latency), |
e572ec7e | 3653 | __ATTR_NULL |
1da177e4 LT |
3654 | }; |
3655 | ||
1da177e4 LT |
3656 | static struct elevator_type iosched_cfq = { |
3657 | .ops = { | |
3658 | .elevator_merge_fn = cfq_merge, | |
3659 | .elevator_merged_fn = cfq_merged_request, | |
3660 | .elevator_merge_req_fn = cfq_merged_requests, | |
da775265 | 3661 | .elevator_allow_merge_fn = cfq_allow_merge, |
812d4026 | 3662 | .elevator_bio_merged_fn = cfq_bio_merged, |
b4878f24 | 3663 | .elevator_dispatch_fn = cfq_dispatch_requests, |
1da177e4 | 3664 | .elevator_add_req_fn = cfq_insert_request, |
b4878f24 | 3665 | .elevator_activate_req_fn = cfq_activate_request, |
1da177e4 | 3666 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
1da177e4 | 3667 | .elevator_completed_req_fn = cfq_completed_request, |
21183b07 JA |
3668 | .elevator_former_req_fn = elv_rb_former_request, |
3669 | .elevator_latter_req_fn = elv_rb_latter_request, | |
9b84cacd | 3670 | .elevator_init_icq_fn = cfq_init_icq, |
7e5a8794 | 3671 | .elevator_exit_icq_fn = cfq_exit_icq, |
1da177e4 LT |
3672 | .elevator_set_req_fn = cfq_set_request, |
3673 | .elevator_put_req_fn = cfq_put_request, | |
3674 | .elevator_may_queue_fn = cfq_may_queue, | |
3675 | .elevator_init_fn = cfq_init_queue, | |
3676 | .elevator_exit_fn = cfq_exit_queue, | |
3677 | }, | |
3d3c2379 TH |
3678 | .icq_size = sizeof(struct cfq_io_cq), |
3679 | .icq_align = __alignof__(struct cfq_io_cq), | |
3d1ab40f | 3680 | .elevator_attrs = cfq_attrs, |
3d3c2379 | 3681 | .elevator_name = "cfq", |
1da177e4 LT |
3682 | .elevator_owner = THIS_MODULE, |
3683 | }; | |
3684 | ||
3e252066 VG |
3685 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
3686 | static struct blkio_policy_type blkio_policy_cfq = { | |
3687 | .ops = { | |
0381411e | 3688 | .blkio_init_group_fn = cfq_init_blkio_group, |
3e252066 VG |
3689 | .blkio_update_group_weight_fn = cfq_update_blkio_group_weight, |
3690 | }, | |
062a644d | 3691 | .plid = BLKIO_POLICY_PROP, |
0381411e | 3692 | .pdata_size = sizeof(struct cfq_group), |
3e252066 | 3693 | }; |
3e252066 VG |
3694 | #endif |
3695 | ||
1da177e4 LT |
3696 | static int __init cfq_init(void) |
3697 | { | |
3d3c2379 TH |
3698 | int ret; |
3699 | ||
22e2c507 JA |
3700 | /* |
3701 | * could be 0 on HZ < 1000 setups | |
3702 | */ | |
3703 | if (!cfq_slice_async) | |
3704 | cfq_slice_async = 1; | |
3705 | if (!cfq_slice_idle) | |
3706 | cfq_slice_idle = 1; | |
3707 | ||
80bdf0c7 VG |
3708 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
3709 | if (!cfq_group_idle) | |
3710 | cfq_group_idle = 1; | |
3711 | #else | |
3712 | cfq_group_idle = 0; | |
3713 | #endif | |
3d3c2379 TH |
3714 | cfq_pool = KMEM_CACHE(cfq_queue, 0); |
3715 | if (!cfq_pool) | |
1da177e4 LT |
3716 | return -ENOMEM; |
3717 | ||
3d3c2379 TH |
3718 | ret = elv_register(&iosched_cfq); |
3719 | if (ret) { | |
3720 | kmem_cache_destroy(cfq_pool); | |
3721 | return ret; | |
3722 | } | |
3d3c2379 | 3723 | |
b95ada55 | 3724 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
3e252066 | 3725 | blkio_policy_register(&blkio_policy_cfq); |
b95ada55 | 3726 | #endif |
2fdd82bd | 3727 | return 0; |
1da177e4 LT |
3728 | } |
3729 | ||
3730 | static void __exit cfq_exit(void) | |
3731 | { | |
b95ada55 | 3732 | #ifdef CONFIG_CFQ_GROUP_IOSCHED |
3e252066 | 3733 | blkio_policy_unregister(&blkio_policy_cfq); |
b95ada55 | 3734 | #endif |
1da177e4 | 3735 | elv_unregister(&iosched_cfq); |
3d3c2379 | 3736 | kmem_cache_destroy(cfq_pool); |
1da177e4 LT |
3737 | } |
3738 | ||
3739 | module_init(cfq_init); | |
3740 | module_exit(cfq_exit); | |
3741 | ||
3742 | MODULE_AUTHOR("Jens Axboe"); | |
3743 | MODULE_LICENSE("GPL"); | |
3744 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |