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> |
1cc9be68 AV |
10 | #include <linux/blkdev.h> |
11 | #include <linux/elevator.h> | |
1da177e4 LT |
12 | #include <linux/hash.h> |
13 | #include <linux/rbtree.h> | |
22e2c507 | 14 | #include <linux/ioprio.h> |
1da177e4 LT |
15 | |
16 | /* | |
17 | * tunables | |
18 | */ | |
64100099 | 19 | static const int cfq_quantum = 4; /* max queue in one round of service */ |
64100099 AV |
20 | static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 }; |
21 | static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */ | |
22 | static const int cfq_back_penalty = 2; /* penalty of a backwards seek */ | |
1da177e4 | 23 | |
64100099 | 24 | static const int cfq_slice_sync = HZ / 10; |
3b18152c | 25 | static int cfq_slice_async = HZ / 25; |
64100099 | 26 | static const int cfq_slice_async_rq = 2; |
caaa5f9f | 27 | static int cfq_slice_idle = HZ / 125; |
22e2c507 | 28 | |
d9e7620e JA |
29 | /* |
30 | * grace period before allowing idle class to get disk access | |
31 | */ | |
22e2c507 | 32 | #define CFQ_IDLE_GRACE (HZ / 10) |
d9e7620e JA |
33 | |
34 | /* | |
35 | * below this threshold, we consider thinktime immediate | |
36 | */ | |
37 | #define CFQ_MIN_TT (2) | |
38 | ||
22e2c507 JA |
39 | #define CFQ_SLICE_SCALE (5) |
40 | ||
41 | #define CFQ_KEY_ASYNC (0) | |
22e2c507 | 42 | |
1da177e4 LT |
43 | /* |
44 | * for the hash of cfqq inside the cfqd | |
45 | */ | |
46 | #define CFQ_QHASH_SHIFT 6 | |
47 | #define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT) | |
48 | #define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash) | |
49 | ||
1da177e4 LT |
50 | #define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list) |
51 | ||
5e705374 JA |
52 | #define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private) |
53 | #define RQ_CFQQ(rq) ((rq)->elevator_private2) | |
1da177e4 | 54 | |
e18b890b CL |
55 | static struct kmem_cache *cfq_pool; |
56 | static struct kmem_cache *cfq_ioc_pool; | |
1da177e4 | 57 | |
4050cf16 | 58 | static DEFINE_PER_CPU(unsigned long, ioc_count); |
334e94de AV |
59 | static struct completion *ioc_gone; |
60 | ||
22e2c507 JA |
61 | #define CFQ_PRIO_LISTS IOPRIO_BE_NR |
62 | #define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE) | |
22e2c507 JA |
63 | #define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT) |
64 | ||
3b18152c JA |
65 | #define ASYNC (0) |
66 | #define SYNC (1) | |
67 | ||
6d048f53 | 68 | #define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC) |
22e2c507 | 69 | |
206dc69b JA |
70 | #define sample_valid(samples) ((samples) > 80) |
71 | ||
cc09e299 JA |
72 | /* |
73 | * Most of our rbtree usage is for sorting with min extraction, so | |
74 | * if we cache the leftmost node we don't have to walk down the tree | |
75 | * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should | |
76 | * move this into the elevator for the rq sorting as well. | |
77 | */ | |
78 | struct cfq_rb_root { | |
79 | struct rb_root rb; | |
80 | struct rb_node *left; | |
81 | }; | |
82 | #define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, } | |
83 | ||
22e2c507 JA |
84 | /* |
85 | * Per block device queue structure | |
86 | */ | |
1da177e4 | 87 | struct cfq_data { |
22e2c507 JA |
88 | request_queue_t *queue; |
89 | ||
90 | /* | |
91 | * rr list of queues with requests and the count of them | |
92 | */ | |
cc09e299 | 93 | struct cfq_rb_root service_tree; |
22e2c507 | 94 | struct list_head cur_rr; |
22e2c507 JA |
95 | unsigned int busy_queues; |
96 | ||
22e2c507 JA |
97 | /* |
98 | * cfqq lookup hash | |
99 | */ | |
1da177e4 | 100 | struct hlist_head *cfq_hash; |
1da177e4 | 101 | |
22e2c507 | 102 | int rq_in_driver; |
25776e35 | 103 | int hw_tag; |
1da177e4 | 104 | |
22e2c507 JA |
105 | /* |
106 | * idle window management | |
107 | */ | |
108 | struct timer_list idle_slice_timer; | |
109 | struct work_struct unplug_work; | |
1da177e4 | 110 | |
22e2c507 JA |
111 | struct cfq_queue *active_queue; |
112 | struct cfq_io_context *active_cic; | |
22e2c507 JA |
113 | unsigned int dispatch_slice; |
114 | ||
115 | struct timer_list idle_class_timer; | |
1da177e4 | 116 | |
6d048f53 | 117 | sector_t last_position; |
22e2c507 | 118 | unsigned long last_end_request; |
1da177e4 | 119 | |
1da177e4 LT |
120 | /* |
121 | * tunables, see top of file | |
122 | */ | |
123 | unsigned int cfq_quantum; | |
22e2c507 | 124 | unsigned int cfq_fifo_expire[2]; |
1da177e4 LT |
125 | unsigned int cfq_back_penalty; |
126 | unsigned int cfq_back_max; | |
22e2c507 JA |
127 | unsigned int cfq_slice[2]; |
128 | unsigned int cfq_slice_async_rq; | |
129 | unsigned int cfq_slice_idle; | |
d9ff4187 AV |
130 | |
131 | struct list_head cic_list; | |
6d048f53 JA |
132 | |
133 | sector_t new_seek_mean; | |
134 | u64 new_seek_total; | |
1da177e4 LT |
135 | }; |
136 | ||
22e2c507 JA |
137 | /* |
138 | * Per process-grouping structure | |
139 | */ | |
1da177e4 LT |
140 | struct cfq_queue { |
141 | /* reference count */ | |
142 | atomic_t ref; | |
143 | /* parent cfq_data */ | |
144 | struct cfq_data *cfqd; | |
22e2c507 | 145 | /* cfqq lookup hash */ |
1da177e4 LT |
146 | struct hlist_node cfq_hash; |
147 | /* hash key */ | |
22e2c507 | 148 | unsigned int key; |
981a7973 | 149 | /* member of the rr/busy/cur/idle cfqd list */ |
1da177e4 | 150 | struct list_head cfq_list; |
d9e7620e JA |
151 | /* service_tree member */ |
152 | struct rb_node rb_node; | |
153 | /* service_tree key */ | |
154 | unsigned long rb_key; | |
1da177e4 LT |
155 | /* sorted list of pending requests */ |
156 | struct rb_root sort_list; | |
157 | /* if fifo isn't expired, next request to serve */ | |
5e705374 | 158 | struct request *next_rq; |
1da177e4 LT |
159 | /* requests queued in sort_list */ |
160 | int queued[2]; | |
161 | /* currently allocated requests */ | |
162 | int allocated[2]; | |
374f84ac JA |
163 | /* pending metadata requests */ |
164 | int meta_pending; | |
1da177e4 | 165 | /* fifo list of requests in sort_list */ |
22e2c507 | 166 | struct list_head fifo; |
1da177e4 | 167 | |
22e2c507 | 168 | unsigned long slice_end; |
c5b680f3 | 169 | long slice_resid; |
1da177e4 | 170 | |
6d048f53 JA |
171 | /* number of requests that are on the dispatch list or inside driver */ |
172 | int dispatched; | |
22e2c507 JA |
173 | |
174 | /* io prio of this group */ | |
175 | unsigned short ioprio, org_ioprio; | |
176 | unsigned short ioprio_class, org_ioprio_class; | |
177 | ||
3b18152c JA |
178 | /* various state flags, see below */ |
179 | unsigned int flags; | |
6d048f53 JA |
180 | |
181 | sector_t last_request_pos; | |
1da177e4 LT |
182 | }; |
183 | ||
3b18152c | 184 | enum cfqq_state_flags { |
b0b8d749 JA |
185 | CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */ |
186 | CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */ | |
187 | CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */ | |
188 | CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */ | |
189 | CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */ | |
190 | CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */ | |
191 | CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */ | |
192 | CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */ | |
193 | CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */ | |
44f7c160 | 194 | CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */ |
3b18152c JA |
195 | }; |
196 | ||
197 | #define CFQ_CFQQ_FNS(name) \ | |
198 | static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \ | |
199 | { \ | |
200 | cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \ | |
201 | } \ | |
202 | static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \ | |
203 | { \ | |
204 | cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \ | |
205 | } \ | |
206 | static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \ | |
207 | { \ | |
208 | return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \ | |
209 | } | |
210 | ||
211 | CFQ_CFQQ_FNS(on_rr); | |
212 | CFQ_CFQQ_FNS(wait_request); | |
213 | CFQ_CFQQ_FNS(must_alloc); | |
214 | CFQ_CFQQ_FNS(must_alloc_slice); | |
215 | CFQ_CFQQ_FNS(must_dispatch); | |
216 | CFQ_CFQQ_FNS(fifo_expire); | |
217 | CFQ_CFQQ_FNS(idle_window); | |
218 | CFQ_CFQQ_FNS(prio_changed); | |
53b03744 | 219 | CFQ_CFQQ_FNS(queue_new); |
44f7c160 | 220 | CFQ_CFQQ_FNS(slice_new); |
3b18152c JA |
221 | #undef CFQ_CFQQ_FNS |
222 | ||
3b18152c | 223 | static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short); |
5e705374 | 224 | static void cfq_dispatch_insert(request_queue_t *, struct request *); |
498d3aa2 | 225 | static struct cfq_queue *cfq_get_queue(struct cfq_data *, unsigned int, struct task_struct *, gfp_t); |
1da177e4 | 226 | |
99f95e52 AM |
227 | /* |
228 | * scheduler run of queue, if there are requests pending and no one in the | |
229 | * driver that will restart queueing | |
230 | */ | |
231 | static inline void cfq_schedule_dispatch(struct cfq_data *cfqd) | |
232 | { | |
7b14e3b5 | 233 | if (cfqd->busy_queues) |
99f95e52 AM |
234 | kblockd_schedule_work(&cfqd->unplug_work); |
235 | } | |
236 | ||
237 | static int cfq_queue_empty(request_queue_t *q) | |
238 | { | |
239 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
240 | ||
b4878f24 | 241 | return !cfqd->busy_queues; |
99f95e52 AM |
242 | } |
243 | ||
7749a8d4 | 244 | static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync) |
206dc69b | 245 | { |
7749a8d4 JA |
246 | /* |
247 | * Use the per-process queue, for read requests and syncronous writes | |
248 | */ | |
249 | if (!(rw & REQ_RW) || is_sync) | |
206dc69b JA |
250 | return task->pid; |
251 | ||
252 | return CFQ_KEY_ASYNC; | |
253 | } | |
254 | ||
44f7c160 JA |
255 | /* |
256 | * Scale schedule slice based on io priority. Use the sync time slice only | |
257 | * if a queue is marked sync and has sync io queued. A sync queue with async | |
258 | * io only, should not get full sync slice length. | |
259 | */ | |
d9e7620e JA |
260 | static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync, |
261 | unsigned short prio) | |
44f7c160 | 262 | { |
d9e7620e | 263 | const int base_slice = cfqd->cfq_slice[sync]; |
44f7c160 | 264 | |
d9e7620e JA |
265 | WARN_ON(prio >= IOPRIO_BE_NR); |
266 | ||
267 | return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio)); | |
268 | } | |
44f7c160 | 269 | |
d9e7620e JA |
270 | static inline int |
271 | cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
272 | { | |
273 | return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio); | |
44f7c160 JA |
274 | } |
275 | ||
276 | static inline void | |
277 | cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
278 | { | |
279 | cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies; | |
280 | } | |
281 | ||
282 | /* | |
283 | * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end | |
284 | * isn't valid until the first request from the dispatch is activated | |
285 | * and the slice time set. | |
286 | */ | |
287 | static inline int cfq_slice_used(struct cfq_queue *cfqq) | |
288 | { | |
289 | if (cfq_cfqq_slice_new(cfqq)) | |
290 | return 0; | |
291 | if (time_before(jiffies, cfqq->slice_end)) | |
292 | return 0; | |
293 | ||
294 | return 1; | |
295 | } | |
296 | ||
1da177e4 | 297 | /* |
5e705374 | 298 | * Lifted from AS - choose which of rq1 and rq2 that is best served now. |
1da177e4 | 299 | * We choose the request that is closest to the head right now. Distance |
e8a99053 | 300 | * behind the head is penalized and only allowed to a certain extent. |
1da177e4 | 301 | */ |
5e705374 JA |
302 | static struct request * |
303 | cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2) | |
1da177e4 LT |
304 | { |
305 | sector_t last, s1, s2, d1 = 0, d2 = 0; | |
1da177e4 | 306 | unsigned long back_max; |
e8a99053 AM |
307 | #define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */ |
308 | #define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */ | |
309 | unsigned wrap = 0; /* bit mask: requests behind the disk head? */ | |
1da177e4 | 310 | |
5e705374 JA |
311 | if (rq1 == NULL || rq1 == rq2) |
312 | return rq2; | |
313 | if (rq2 == NULL) | |
314 | return rq1; | |
9c2c38a1 | 315 | |
5e705374 JA |
316 | if (rq_is_sync(rq1) && !rq_is_sync(rq2)) |
317 | return rq1; | |
318 | else if (rq_is_sync(rq2) && !rq_is_sync(rq1)) | |
319 | return rq2; | |
374f84ac JA |
320 | if (rq_is_meta(rq1) && !rq_is_meta(rq2)) |
321 | return rq1; | |
322 | else if (rq_is_meta(rq2) && !rq_is_meta(rq1)) | |
323 | return rq2; | |
1da177e4 | 324 | |
5e705374 JA |
325 | s1 = rq1->sector; |
326 | s2 = rq2->sector; | |
1da177e4 | 327 | |
6d048f53 | 328 | last = cfqd->last_position; |
1da177e4 | 329 | |
1da177e4 LT |
330 | /* |
331 | * by definition, 1KiB is 2 sectors | |
332 | */ | |
333 | back_max = cfqd->cfq_back_max * 2; | |
334 | ||
335 | /* | |
336 | * Strict one way elevator _except_ in the case where we allow | |
337 | * short backward seeks which are biased as twice the cost of a | |
338 | * similar forward seek. | |
339 | */ | |
340 | if (s1 >= last) | |
341 | d1 = s1 - last; | |
342 | else if (s1 + back_max >= last) | |
343 | d1 = (last - s1) * cfqd->cfq_back_penalty; | |
344 | else | |
e8a99053 | 345 | wrap |= CFQ_RQ1_WRAP; |
1da177e4 LT |
346 | |
347 | if (s2 >= last) | |
348 | d2 = s2 - last; | |
349 | else if (s2 + back_max >= last) | |
350 | d2 = (last - s2) * cfqd->cfq_back_penalty; | |
351 | else | |
e8a99053 | 352 | wrap |= CFQ_RQ2_WRAP; |
1da177e4 LT |
353 | |
354 | /* Found required data */ | |
e8a99053 AM |
355 | |
356 | /* | |
357 | * By doing switch() on the bit mask "wrap" we avoid having to | |
358 | * check two variables for all permutations: --> faster! | |
359 | */ | |
360 | switch (wrap) { | |
5e705374 | 361 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ |
e8a99053 | 362 | if (d1 < d2) |
5e705374 | 363 | return rq1; |
e8a99053 | 364 | else if (d2 < d1) |
5e705374 | 365 | return rq2; |
e8a99053 AM |
366 | else { |
367 | if (s1 >= s2) | |
5e705374 | 368 | return rq1; |
e8a99053 | 369 | else |
5e705374 | 370 | return rq2; |
e8a99053 | 371 | } |
1da177e4 | 372 | |
e8a99053 | 373 | case CFQ_RQ2_WRAP: |
5e705374 | 374 | return rq1; |
e8a99053 | 375 | case CFQ_RQ1_WRAP: |
5e705374 JA |
376 | return rq2; |
377 | case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */ | |
e8a99053 AM |
378 | default: |
379 | /* | |
380 | * Since both rqs are wrapped, | |
381 | * start with the one that's further behind head | |
382 | * (--> only *one* back seek required), | |
383 | * since back seek takes more time than forward. | |
384 | */ | |
385 | if (s1 <= s2) | |
5e705374 | 386 | return rq1; |
1da177e4 | 387 | else |
5e705374 | 388 | return rq2; |
1da177e4 LT |
389 | } |
390 | } | |
391 | ||
498d3aa2 JA |
392 | /* |
393 | * The below is leftmost cache rbtree addon | |
394 | */ | |
cc09e299 JA |
395 | static struct rb_node *cfq_rb_first(struct cfq_rb_root *root) |
396 | { | |
397 | if (!root->left) | |
398 | root->left = rb_first(&root->rb); | |
399 | ||
400 | return root->left; | |
401 | } | |
402 | ||
403 | static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root) | |
404 | { | |
405 | if (root->left == n) | |
406 | root->left = NULL; | |
407 | ||
408 | rb_erase(n, &root->rb); | |
409 | RB_CLEAR_NODE(n); | |
410 | } | |
411 | ||
1da177e4 LT |
412 | /* |
413 | * would be nice to take fifo expire time into account as well | |
414 | */ | |
5e705374 JA |
415 | static struct request * |
416 | cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
417 | struct request *last) | |
1da177e4 | 418 | { |
21183b07 JA |
419 | struct rb_node *rbnext = rb_next(&last->rb_node); |
420 | struct rb_node *rbprev = rb_prev(&last->rb_node); | |
5e705374 | 421 | struct request *next = NULL, *prev = NULL; |
1da177e4 | 422 | |
21183b07 | 423 | BUG_ON(RB_EMPTY_NODE(&last->rb_node)); |
1da177e4 LT |
424 | |
425 | if (rbprev) | |
5e705374 | 426 | prev = rb_entry_rq(rbprev); |
1da177e4 | 427 | |
21183b07 | 428 | if (rbnext) |
5e705374 | 429 | next = rb_entry_rq(rbnext); |
21183b07 JA |
430 | else { |
431 | rbnext = rb_first(&cfqq->sort_list); | |
432 | if (rbnext && rbnext != &last->rb_node) | |
5e705374 | 433 | next = rb_entry_rq(rbnext); |
21183b07 | 434 | } |
1da177e4 | 435 | |
21183b07 | 436 | return cfq_choose_req(cfqd, next, prev); |
1da177e4 LT |
437 | } |
438 | ||
d9e7620e JA |
439 | static unsigned long cfq_slice_offset(struct cfq_data *cfqd, |
440 | struct cfq_queue *cfqq) | |
1da177e4 | 441 | { |
d9e7620e JA |
442 | /* |
443 | * just an approximation, should be ok. | |
444 | */ | |
445 | return ((cfqd->busy_queues - 1) * cfq_prio_slice(cfqd, 1, 0)); | |
446 | } | |
447 | ||
498d3aa2 JA |
448 | /* |
449 | * The cfqd->service_tree holds all pending cfq_queue's that have | |
450 | * requests waiting to be processed. It is sorted in the order that | |
451 | * we will service the queues. | |
452 | */ | |
d9e7620e JA |
453 | static void cfq_service_tree_add(struct cfq_data *cfqd, |
454 | struct cfq_queue *cfqq) | |
455 | { | |
cc09e299 | 456 | struct rb_node **p = &cfqd->service_tree.rb.rb_node; |
d9e7620e | 457 | struct rb_node *parent = NULL; |
d9e7620e | 458 | unsigned long rb_key; |
498d3aa2 | 459 | int left; |
d9e7620e JA |
460 | |
461 | rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies; | |
462 | rb_key += cfqq->slice_resid; | |
463 | cfqq->slice_resid = 0; | |
1da177e4 | 464 | |
d9e7620e | 465 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) { |
99f9628a | 466 | /* |
d9e7620e | 467 | * same position, nothing more to do |
99f9628a | 468 | */ |
d9e7620e JA |
469 | if (rb_key == cfqq->rb_key) |
470 | return; | |
1da177e4 | 471 | |
cc09e299 | 472 | cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree); |
1da177e4 | 473 | } |
d9e7620e | 474 | |
498d3aa2 | 475 | left = 1; |
d9e7620e | 476 | while (*p) { |
cc09e299 | 477 | struct cfq_queue *__cfqq; |
67060e37 | 478 | struct rb_node **n; |
cc09e299 | 479 | |
d9e7620e JA |
480 | parent = *p; |
481 | __cfqq = rb_entry(parent, struct cfq_queue, rb_node); | |
482 | ||
0c534e0a JA |
483 | /* |
484 | * sort RT queues first, we always want to give | |
67060e37 JA |
485 | * preference to them. IDLE queues goes to the back. |
486 | * after that, sort on the next service time. | |
0c534e0a JA |
487 | */ |
488 | if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq)) | |
67060e37 | 489 | n = &(*p)->rb_left; |
0c534e0a | 490 | else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq)) |
67060e37 JA |
491 | n = &(*p)->rb_right; |
492 | else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq)) | |
493 | n = &(*p)->rb_left; | |
494 | else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq)) | |
495 | n = &(*p)->rb_right; | |
0c534e0a | 496 | else if (rb_key < __cfqq->rb_key) |
67060e37 JA |
497 | n = &(*p)->rb_left; |
498 | else | |
499 | n = &(*p)->rb_right; | |
500 | ||
501 | if (n == &(*p)->rb_right) | |
cc09e299 | 502 | left = 0; |
67060e37 JA |
503 | |
504 | p = n; | |
d9e7620e JA |
505 | } |
506 | ||
cc09e299 JA |
507 | if (left) |
508 | cfqd->service_tree.left = &cfqq->rb_node; | |
509 | ||
d9e7620e JA |
510 | cfqq->rb_key = rb_key; |
511 | rb_link_node(&cfqq->rb_node, parent, p); | |
cc09e299 | 512 | rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb); |
1da177e4 LT |
513 | } |
514 | ||
498d3aa2 JA |
515 | /* |
516 | * Update cfqq's position in the service tree. | |
517 | */ | |
6d048f53 JA |
518 | static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted) |
519 | { | |
6d048f53 JA |
520 | /* |
521 | * Resorting requires the cfqq to be on the RR list already. | |
522 | */ | |
498d3aa2 JA |
523 | if (cfq_cfqq_on_rr(cfqq)) |
524 | cfq_service_tree_add(cfqq->cfqd, cfqq); | |
6d048f53 JA |
525 | } |
526 | ||
1da177e4 LT |
527 | /* |
528 | * add to busy list of queues for service, trying to be fair in ordering | |
22e2c507 | 529 | * the pending list according to last request service |
1da177e4 LT |
530 | */ |
531 | static inline void | |
b4878f24 | 532 | cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
1da177e4 | 533 | { |
3b18152c JA |
534 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
535 | cfq_mark_cfqq_on_rr(cfqq); | |
1da177e4 LT |
536 | cfqd->busy_queues++; |
537 | ||
b4878f24 | 538 | cfq_resort_rr_list(cfqq, 0); |
1da177e4 LT |
539 | } |
540 | ||
498d3aa2 JA |
541 | /* |
542 | * Called when the cfqq no longer has requests pending, remove it from | |
543 | * the service tree. | |
544 | */ | |
1da177e4 LT |
545 | static inline void |
546 | cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
547 | { | |
3b18152c JA |
548 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); |
549 | cfq_clear_cfqq_on_rr(cfqq); | |
981a7973 | 550 | list_del_init(&cfqq->cfq_list); |
1da177e4 | 551 | |
cc09e299 JA |
552 | if (!RB_EMPTY_NODE(&cfqq->rb_node)) |
553 | cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree); | |
d9e7620e | 554 | |
1da177e4 LT |
555 | BUG_ON(!cfqd->busy_queues); |
556 | cfqd->busy_queues--; | |
557 | } | |
558 | ||
559 | /* | |
560 | * rb tree support functions | |
561 | */ | |
5e705374 | 562 | static inline void cfq_del_rq_rb(struct request *rq) |
1da177e4 | 563 | { |
5e705374 | 564 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
b4878f24 | 565 | struct cfq_data *cfqd = cfqq->cfqd; |
5e705374 | 566 | const int sync = rq_is_sync(rq); |
1da177e4 | 567 | |
b4878f24 JA |
568 | BUG_ON(!cfqq->queued[sync]); |
569 | cfqq->queued[sync]--; | |
1da177e4 | 570 | |
5e705374 | 571 | elv_rb_del(&cfqq->sort_list, rq); |
1da177e4 | 572 | |
dd67d051 | 573 | if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) |
b4878f24 | 574 | cfq_del_cfqq_rr(cfqd, cfqq); |
1da177e4 LT |
575 | } |
576 | ||
5e705374 | 577 | static void cfq_add_rq_rb(struct request *rq) |
1da177e4 | 578 | { |
5e705374 | 579 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
1da177e4 | 580 | struct cfq_data *cfqd = cfqq->cfqd; |
21183b07 | 581 | struct request *__alias; |
1da177e4 | 582 | |
5380a101 | 583 | cfqq->queued[rq_is_sync(rq)]++; |
1da177e4 LT |
584 | |
585 | /* | |
586 | * looks a little odd, but the first insert might return an alias. | |
587 | * if that happens, put the alias on the dispatch list | |
588 | */ | |
21183b07 | 589 | while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL) |
5e705374 | 590 | cfq_dispatch_insert(cfqd->queue, __alias); |
5fccbf61 JA |
591 | |
592 | if (!cfq_cfqq_on_rr(cfqq)) | |
593 | cfq_add_cfqq_rr(cfqd, cfqq); | |
5044eed4 JA |
594 | |
595 | /* | |
596 | * check if this request is a better next-serve candidate | |
597 | */ | |
598 | cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq); | |
599 | BUG_ON(!cfqq->next_rq); | |
1da177e4 LT |
600 | } |
601 | ||
602 | static inline void | |
5e705374 | 603 | cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq) |
1da177e4 | 604 | { |
5380a101 JA |
605 | elv_rb_del(&cfqq->sort_list, rq); |
606 | cfqq->queued[rq_is_sync(rq)]--; | |
5e705374 | 607 | cfq_add_rq_rb(rq); |
1da177e4 LT |
608 | } |
609 | ||
206dc69b JA |
610 | static struct request * |
611 | cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio) | |
1da177e4 | 612 | { |
206dc69b | 613 | struct task_struct *tsk = current; |
7749a8d4 | 614 | pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio)); |
206dc69b | 615 | struct cfq_queue *cfqq; |
1da177e4 | 616 | |
206dc69b | 617 | cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio); |
89850f7e JA |
618 | if (cfqq) { |
619 | sector_t sector = bio->bi_sector + bio_sectors(bio); | |
620 | ||
21183b07 | 621 | return elv_rb_find(&cfqq->sort_list, sector); |
89850f7e | 622 | } |
1da177e4 | 623 | |
1da177e4 LT |
624 | return NULL; |
625 | } | |
626 | ||
b4878f24 | 627 | static void cfq_activate_request(request_queue_t *q, struct request *rq) |
1da177e4 | 628 | { |
22e2c507 | 629 | struct cfq_data *cfqd = q->elevator->elevator_data; |
3b18152c | 630 | |
b4878f24 | 631 | cfqd->rq_in_driver++; |
25776e35 JA |
632 | |
633 | /* | |
634 | * If the depth is larger 1, it really could be queueing. But lets | |
635 | * make the mark a little higher - idling could still be good for | |
636 | * low queueing, and a low queueing number could also just indicate | |
637 | * a SCSI mid layer like behaviour where limit+1 is often seen. | |
638 | */ | |
639 | if (!cfqd->hw_tag && cfqd->rq_in_driver > 4) | |
640 | cfqd->hw_tag = 1; | |
6d048f53 JA |
641 | |
642 | cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors; | |
1da177e4 LT |
643 | } |
644 | ||
b4878f24 | 645 | static void cfq_deactivate_request(request_queue_t *q, struct request *rq) |
1da177e4 | 646 | { |
b4878f24 JA |
647 | struct cfq_data *cfqd = q->elevator->elevator_data; |
648 | ||
649 | WARN_ON(!cfqd->rq_in_driver); | |
650 | cfqd->rq_in_driver--; | |
1da177e4 LT |
651 | } |
652 | ||
b4878f24 | 653 | static void cfq_remove_request(struct request *rq) |
1da177e4 | 654 | { |
5e705374 | 655 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
21183b07 | 656 | |
5e705374 JA |
657 | if (cfqq->next_rq == rq) |
658 | cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq); | |
1da177e4 | 659 | |
b4878f24 | 660 | list_del_init(&rq->queuelist); |
5e705374 | 661 | cfq_del_rq_rb(rq); |
374f84ac JA |
662 | |
663 | if (rq_is_meta(rq)) { | |
664 | WARN_ON(!cfqq->meta_pending); | |
665 | cfqq->meta_pending--; | |
666 | } | |
1da177e4 LT |
667 | } |
668 | ||
498d3aa2 | 669 | static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio) |
1da177e4 LT |
670 | { |
671 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
672 | struct request *__rq; | |
1da177e4 | 673 | |
206dc69b | 674 | __rq = cfq_find_rq_fmerge(cfqd, bio); |
22e2c507 | 675 | if (__rq && elv_rq_merge_ok(__rq, bio)) { |
9817064b JA |
676 | *req = __rq; |
677 | return ELEVATOR_FRONT_MERGE; | |
1da177e4 LT |
678 | } |
679 | ||
680 | return ELEVATOR_NO_MERGE; | |
1da177e4 LT |
681 | } |
682 | ||
21183b07 JA |
683 | static void cfq_merged_request(request_queue_t *q, struct request *req, |
684 | int type) | |
1da177e4 | 685 | { |
21183b07 | 686 | if (type == ELEVATOR_FRONT_MERGE) { |
5e705374 | 687 | struct cfq_queue *cfqq = RQ_CFQQ(req); |
1da177e4 | 688 | |
5e705374 | 689 | cfq_reposition_rq_rb(cfqq, req); |
1da177e4 | 690 | } |
1da177e4 LT |
691 | } |
692 | ||
693 | static void | |
694 | cfq_merged_requests(request_queue_t *q, struct request *rq, | |
695 | struct request *next) | |
696 | { | |
22e2c507 JA |
697 | /* |
698 | * reposition in fifo if next is older than rq | |
699 | */ | |
700 | if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && | |
701 | time_before(next->start_time, rq->start_time)) | |
702 | list_move(&rq->queuelist, &next->queuelist); | |
703 | ||
b4878f24 | 704 | cfq_remove_request(next); |
22e2c507 JA |
705 | } |
706 | ||
da775265 JA |
707 | static int cfq_allow_merge(request_queue_t *q, struct request *rq, |
708 | struct bio *bio) | |
709 | { | |
710 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
711 | const int rw = bio_data_dir(bio); | |
712 | struct cfq_queue *cfqq; | |
713 | pid_t key; | |
714 | ||
715 | /* | |
ec8acb69 | 716 | * Disallow merge of a sync bio into an async request. |
da775265 | 717 | */ |
ec8acb69 | 718 | if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq)) |
da775265 JA |
719 | return 0; |
720 | ||
721 | /* | |
719d3402 JA |
722 | * Lookup the cfqq that this bio will be queued with. Allow |
723 | * merge only if rq is queued there. | |
da775265 | 724 | */ |
719d3402 | 725 | key = cfq_queue_pid(current, rw, bio_sync(bio)); |
da775265 | 726 | cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio); |
719d3402 JA |
727 | |
728 | if (cfqq == RQ_CFQQ(rq)) | |
729 | return 1; | |
da775265 | 730 | |
ec8acb69 | 731 | return 0; |
da775265 JA |
732 | } |
733 | ||
22e2c507 JA |
734 | static inline void |
735 | __cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
736 | { | |
737 | if (cfqq) { | |
738 | /* | |
739 | * stop potential idle class queues waiting service | |
740 | */ | |
741 | del_timer(&cfqd->idle_class_timer); | |
742 | ||
22e2c507 | 743 | cfqq->slice_end = 0; |
3b18152c JA |
744 | cfq_clear_cfqq_must_alloc_slice(cfqq); |
745 | cfq_clear_cfqq_fifo_expire(cfqq); | |
44f7c160 | 746 | cfq_mark_cfqq_slice_new(cfqq); |
1afba045 | 747 | cfq_clear_cfqq_queue_new(cfqq); |
22e2c507 JA |
748 | } |
749 | ||
750 | cfqd->active_queue = cfqq; | |
751 | } | |
752 | ||
7b14e3b5 JA |
753 | /* |
754 | * current cfqq expired its slice (or was too idle), select new one | |
755 | */ | |
756 | static void | |
757 | __cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
3c6bd2f8 | 758 | int preempted, int timed_out) |
7b14e3b5 | 759 | { |
7b14e3b5 JA |
760 | if (cfq_cfqq_wait_request(cfqq)) |
761 | del_timer(&cfqd->idle_slice_timer); | |
762 | ||
7b14e3b5 JA |
763 | cfq_clear_cfqq_must_dispatch(cfqq); |
764 | cfq_clear_cfqq_wait_request(cfqq); | |
765 | ||
766 | /* | |
767 | * store what was left of this slice, if the queue idled out | |
768 | * or was preempted | |
769 | */ | |
3c6bd2f8 | 770 | if (timed_out && !cfq_cfqq_slice_new(cfqq)) |
c5b680f3 | 771 | cfqq->slice_resid = cfqq->slice_end - jiffies; |
7b14e3b5 | 772 | |
98e41c7d | 773 | cfq_resort_rr_list(cfqq, preempted); |
7b14e3b5 JA |
774 | |
775 | if (cfqq == cfqd->active_queue) | |
776 | cfqd->active_queue = NULL; | |
777 | ||
778 | if (cfqd->active_cic) { | |
779 | put_io_context(cfqd->active_cic->ioc); | |
780 | cfqd->active_cic = NULL; | |
781 | } | |
782 | ||
783 | cfqd->dispatch_slice = 0; | |
784 | } | |
785 | ||
3c6bd2f8 JA |
786 | static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted, |
787 | int timed_out) | |
7b14e3b5 JA |
788 | { |
789 | struct cfq_queue *cfqq = cfqd->active_queue; | |
790 | ||
791 | if (cfqq) | |
3c6bd2f8 | 792 | __cfq_slice_expired(cfqd, cfqq, preempted, timed_out); |
7b14e3b5 JA |
793 | } |
794 | ||
498d3aa2 JA |
795 | /* |
796 | * Get next queue for service. Unless we have a queue preemption, | |
797 | * we'll simply select the first cfqq in the service tree. | |
798 | */ | |
6d048f53 | 799 | static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd) |
22e2c507 | 800 | { |
7b14e3b5 | 801 | struct cfq_queue *cfqq = NULL; |
22e2c507 | 802 | |
d9e7620e | 803 | if (!list_empty(&cfqd->cur_rr)) { |
89850f7e | 804 | /* |
d9e7620e | 805 | * if current list is non-empty, grab first entry. |
89850f7e | 806 | */ |
d9e7620e | 807 | cfqq = list_entry_cfqq(cfqd->cur_rr.next); |
cc09e299 JA |
808 | } else if (!RB_EMPTY_ROOT(&cfqd->service_tree.rb)) { |
809 | struct rb_node *n = cfq_rb_first(&cfqd->service_tree); | |
d9e7620e JA |
810 | |
811 | cfqq = rb_entry(n, struct cfq_queue, rb_node); | |
67060e37 | 812 | if (cfq_class_idle(cfqq)) { |
498d3aa2 JA |
813 | unsigned long end; |
814 | ||
67060e37 JA |
815 | /* |
816 | * if we have idle queues and no rt or be queues had | |
817 | * pending requests, either allow immediate service if | |
818 | * the grace period has passed or arm the idle grace | |
819 | * timer | |
820 | */ | |
821 | end = cfqd->last_end_request + CFQ_IDLE_GRACE; | |
822 | if (time_before(jiffies, end)) { | |
823 | mod_timer(&cfqd->idle_class_timer, end); | |
824 | cfqq = NULL; | |
825 | } | |
826 | } | |
22e2c507 JA |
827 | } |
828 | ||
6d048f53 JA |
829 | return cfqq; |
830 | } | |
831 | ||
498d3aa2 JA |
832 | /* |
833 | * Get and set a new active queue for service. | |
834 | */ | |
6d048f53 JA |
835 | static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd) |
836 | { | |
837 | struct cfq_queue *cfqq; | |
838 | ||
d9e7620e | 839 | cfqq = cfq_get_next_queue(cfqd); |
22e2c507 | 840 | __cfq_set_active_queue(cfqd, cfqq); |
3b18152c | 841 | return cfqq; |
22e2c507 JA |
842 | } |
843 | ||
d9e7620e JA |
844 | static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd, |
845 | struct request *rq) | |
846 | { | |
847 | if (rq->sector >= cfqd->last_position) | |
848 | return rq->sector - cfqd->last_position; | |
849 | else | |
850 | return cfqd->last_position - rq->sector; | |
851 | } | |
852 | ||
6d048f53 JA |
853 | static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq) |
854 | { | |
855 | struct cfq_io_context *cic = cfqd->active_cic; | |
856 | ||
857 | if (!sample_valid(cic->seek_samples)) | |
858 | return 0; | |
859 | ||
860 | return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean; | |
861 | } | |
862 | ||
d9e7620e JA |
863 | static int cfq_close_cooperator(struct cfq_data *cfq_data, |
864 | struct cfq_queue *cfqq) | |
6d048f53 | 865 | { |
6d048f53 | 866 | /* |
d9e7620e JA |
867 | * We should notice if some of the queues are cooperating, eg |
868 | * working closely on the same area of the disk. In that case, | |
869 | * we can group them together and don't waste time idling. | |
6d048f53 | 870 | */ |
d9e7620e | 871 | return 0; |
6d048f53 JA |
872 | } |
873 | ||
874 | #define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024)) | |
caaa5f9f | 875 | |
6d048f53 | 876 | static void cfq_arm_slice_timer(struct cfq_data *cfqd) |
22e2c507 | 877 | { |
1792669c | 878 | struct cfq_queue *cfqq = cfqd->active_queue; |
206dc69b | 879 | struct cfq_io_context *cic; |
7b14e3b5 JA |
880 | unsigned long sl; |
881 | ||
dd67d051 | 882 | WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list)); |
6d048f53 | 883 | WARN_ON(cfq_cfqq_slice_new(cfqq)); |
22e2c507 JA |
884 | |
885 | /* | |
886 | * idle is disabled, either manually or by past process history | |
887 | */ | |
6d048f53 JA |
888 | if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq)) |
889 | return; | |
890 | ||
22e2c507 JA |
891 | /* |
892 | * task has exited, don't wait | |
893 | */ | |
206dc69b JA |
894 | cic = cfqd->active_cic; |
895 | if (!cic || !cic->ioc->task) | |
6d048f53 JA |
896 | return; |
897 | ||
898 | /* | |
899 | * See if this prio level has a good candidate | |
900 | */ | |
1afba045 JA |
901 | if (cfq_close_cooperator(cfqd, cfqq) && |
902 | (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2)) | |
6d048f53 | 903 | return; |
22e2c507 | 904 | |
3b18152c JA |
905 | cfq_mark_cfqq_must_dispatch(cfqq); |
906 | cfq_mark_cfqq_wait_request(cfqq); | |
22e2c507 | 907 | |
206dc69b JA |
908 | /* |
909 | * we don't want to idle for seeks, but we do want to allow | |
910 | * fair distribution of slice time for a process doing back-to-back | |
911 | * seeks. so allow a little bit of time for him to submit a new rq | |
912 | */ | |
6d048f53 | 913 | sl = cfqd->cfq_slice_idle; |
caaa5f9f | 914 | if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic)) |
d9e7620e | 915 | sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT)); |
206dc69b | 916 | |
7b14e3b5 | 917 | mod_timer(&cfqd->idle_slice_timer, jiffies + sl); |
1da177e4 LT |
918 | } |
919 | ||
498d3aa2 JA |
920 | /* |
921 | * Move request from internal lists to the request queue dispatch list. | |
922 | */ | |
5e705374 | 923 | static void cfq_dispatch_insert(request_queue_t *q, struct request *rq) |
1da177e4 | 924 | { |
5e705374 | 925 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
22e2c507 | 926 | |
5380a101 | 927 | cfq_remove_request(rq); |
6d048f53 | 928 | cfqq->dispatched++; |
5380a101 | 929 | elv_dispatch_sort(q, rq); |
1da177e4 LT |
930 | } |
931 | ||
932 | /* | |
933 | * return expired entry, or NULL to just start from scratch in rbtree | |
934 | */ | |
5e705374 | 935 | static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq) |
1da177e4 LT |
936 | { |
937 | struct cfq_data *cfqd = cfqq->cfqd; | |
22e2c507 | 938 | struct request *rq; |
89850f7e | 939 | int fifo; |
1da177e4 | 940 | |
3b18152c | 941 | if (cfq_cfqq_fifo_expire(cfqq)) |
1da177e4 | 942 | return NULL; |
cb887411 JA |
943 | |
944 | cfq_mark_cfqq_fifo_expire(cfqq); | |
945 | ||
89850f7e JA |
946 | if (list_empty(&cfqq->fifo)) |
947 | return NULL; | |
1da177e4 | 948 | |
6d048f53 | 949 | fifo = cfq_cfqq_sync(cfqq); |
89850f7e | 950 | rq = rq_entry_fifo(cfqq->fifo.next); |
1da177e4 | 951 | |
6d048f53 JA |
952 | if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) |
953 | return NULL; | |
1da177e4 | 954 | |
6d048f53 | 955 | return rq; |
1da177e4 LT |
956 | } |
957 | ||
22e2c507 JA |
958 | static inline int |
959 | cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
960 | { | |
961 | const int base_rq = cfqd->cfq_slice_async_rq; | |
1da177e4 | 962 | |
22e2c507 | 963 | WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR); |
1da177e4 | 964 | |
22e2c507 | 965 | return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio)); |
1da177e4 LT |
966 | } |
967 | ||
22e2c507 | 968 | /* |
498d3aa2 JA |
969 | * Select a queue for service. If we have a current active queue, |
970 | * check whether to continue servicing it, or retrieve and set a new one. | |
22e2c507 | 971 | */ |
1b5ed5e1 | 972 | static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd) |
1da177e4 | 973 | { |
1da177e4 | 974 | struct cfq_queue *cfqq; |
1da177e4 | 975 | |
22e2c507 JA |
976 | cfqq = cfqd->active_queue; |
977 | if (!cfqq) | |
978 | goto new_queue; | |
1da177e4 | 979 | |
22e2c507 | 980 | /* |
6d048f53 | 981 | * The active queue has run out of time, expire it and select new. |
22e2c507 | 982 | */ |
6d048f53 | 983 | if (cfq_slice_used(cfqq)) |
3b18152c | 984 | goto expire; |
1da177e4 | 985 | |
22e2c507 | 986 | /* |
6d048f53 JA |
987 | * The active queue has requests and isn't expired, allow it to |
988 | * dispatch. | |
22e2c507 | 989 | */ |
dd67d051 | 990 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) |
22e2c507 | 991 | goto keep_queue; |
6d048f53 JA |
992 | |
993 | /* | |
994 | * No requests pending. If the active queue still has requests in | |
995 | * flight or is idling for a new request, allow either of these | |
996 | * conditions to happen (or time out) before selecting a new queue. | |
997 | */ | |
998 | if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) { | |
caaa5f9f JA |
999 | cfqq = NULL; |
1000 | goto keep_queue; | |
22e2c507 JA |
1001 | } |
1002 | ||
3b18152c | 1003 | expire: |
3c6bd2f8 | 1004 | cfq_slice_expired(cfqd, 0, 0); |
3b18152c JA |
1005 | new_queue: |
1006 | cfqq = cfq_set_active_queue(cfqd); | |
22e2c507 | 1007 | keep_queue: |
3b18152c | 1008 | return cfqq; |
22e2c507 JA |
1009 | } |
1010 | ||
498d3aa2 JA |
1011 | /* |
1012 | * Dispatch some requests from cfqq, moving them to the request queue | |
1013 | * dispatch list. | |
1014 | */ | |
22e2c507 JA |
1015 | static int |
1016 | __cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
1017 | int max_dispatch) | |
1018 | { | |
1019 | int dispatched = 0; | |
1020 | ||
dd67d051 | 1021 | BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list)); |
22e2c507 JA |
1022 | |
1023 | do { | |
5e705374 | 1024 | struct request *rq; |
1da177e4 LT |
1025 | |
1026 | /* | |
22e2c507 | 1027 | * follow expired path, else get first next available |
1da177e4 | 1028 | */ |
5e705374 JA |
1029 | if ((rq = cfq_check_fifo(cfqq)) == NULL) |
1030 | rq = cfqq->next_rq; | |
22e2c507 JA |
1031 | |
1032 | /* | |
1033 | * finally, insert request into driver dispatch list | |
1034 | */ | |
5e705374 | 1035 | cfq_dispatch_insert(cfqd->queue, rq); |
1da177e4 | 1036 | |
22e2c507 JA |
1037 | cfqd->dispatch_slice++; |
1038 | dispatched++; | |
1da177e4 | 1039 | |
22e2c507 | 1040 | if (!cfqd->active_cic) { |
5e705374 JA |
1041 | atomic_inc(&RQ_CIC(rq)->ioc->refcount); |
1042 | cfqd->active_cic = RQ_CIC(rq); | |
22e2c507 | 1043 | } |
1da177e4 | 1044 | |
dd67d051 | 1045 | if (RB_EMPTY_ROOT(&cfqq->sort_list)) |
22e2c507 JA |
1046 | break; |
1047 | ||
1048 | } while (dispatched < max_dispatch); | |
1049 | ||
22e2c507 JA |
1050 | /* |
1051 | * expire an async queue immediately if it has used up its slice. idle | |
1052 | * queue always expire after 1 dispatch round. | |
1053 | */ | |
a9938006 | 1054 | if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) && |
22e2c507 | 1055 | cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) || |
a9938006 | 1056 | cfq_class_idle(cfqq))) { |
44f7c160 | 1057 | cfqq->slice_end = jiffies + 1; |
3c6bd2f8 | 1058 | cfq_slice_expired(cfqd, 0, 0); |
44f7c160 | 1059 | } |
22e2c507 JA |
1060 | |
1061 | return dispatched; | |
1062 | } | |
1063 | ||
d9e7620e JA |
1064 | static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq) |
1065 | { | |
1066 | int dispatched = 0; | |
1067 | ||
1068 | while (cfqq->next_rq) { | |
1069 | cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq); | |
1070 | dispatched++; | |
1071 | } | |
1072 | ||
1073 | BUG_ON(!list_empty(&cfqq->fifo)); | |
1074 | return dispatched; | |
1075 | } | |
1076 | ||
1077 | static int cfq_forced_dispatch_cfqqs(struct list_head *list) | |
1b5ed5e1 | 1078 | { |
1b5ed5e1 | 1079 | struct cfq_queue *cfqq, *next; |
caaa5f9f | 1080 | int dispatched; |
1b5ed5e1 | 1081 | |
caaa5f9f | 1082 | dispatched = 0; |
d9e7620e JA |
1083 | list_for_each_entry_safe(cfqq, next, list, cfq_list) |
1084 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); | |
caaa5f9f | 1085 | |
1b5ed5e1 TH |
1086 | return dispatched; |
1087 | } | |
1088 | ||
498d3aa2 JA |
1089 | /* |
1090 | * Drain our current requests. Used for barriers and when switching | |
1091 | * io schedulers on-the-fly. | |
1092 | */ | |
d9e7620e | 1093 | static int cfq_forced_dispatch(struct cfq_data *cfqd) |
1b5ed5e1 | 1094 | { |
d9e7620e JA |
1095 | int dispatched = 0; |
1096 | struct rb_node *n; | |
1097 | ||
cc09e299 | 1098 | while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) { |
d9e7620e | 1099 | struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node); |
1b5ed5e1 | 1100 | |
d9e7620e JA |
1101 | dispatched += __cfq_forced_dispatch_cfqq(cfqq); |
1102 | } | |
1b5ed5e1 | 1103 | |
1b5ed5e1 | 1104 | dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr); |
1b5ed5e1 | 1105 | |
3c6bd2f8 | 1106 | cfq_slice_expired(cfqd, 0, 0); |
1b5ed5e1 TH |
1107 | |
1108 | BUG_ON(cfqd->busy_queues); | |
1109 | ||
1110 | return dispatched; | |
1111 | } | |
1112 | ||
d9e7620e | 1113 | static int cfq_dispatch_requests(request_queue_t *q, int force) |
22e2c507 JA |
1114 | { |
1115 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
6d048f53 | 1116 | struct cfq_queue *cfqq; |
caaa5f9f | 1117 | int dispatched; |
22e2c507 JA |
1118 | |
1119 | if (!cfqd->busy_queues) | |
1120 | return 0; | |
1121 | ||
1b5ed5e1 TH |
1122 | if (unlikely(force)) |
1123 | return cfq_forced_dispatch(cfqd); | |
1124 | ||
caaa5f9f | 1125 | dispatched = 0; |
caaa5f9f | 1126 | while ((cfqq = cfq_select_queue(cfqd)) != NULL) { |
b4878f24 JA |
1127 | int max_dispatch; |
1128 | ||
a9938006 | 1129 | if (cfqd->busy_queues > 1) { |
a9938006 JA |
1130 | /* |
1131 | * So we have dispatched before in this round, if the | |
1132 | * next queue has idling enabled (must be sync), don't | |
6d048f53 | 1133 | * allow it service until the previous have completed. |
a9938006 | 1134 | */ |
6d048f53 JA |
1135 | if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) && |
1136 | dispatched) | |
1137 | break; | |
1138 | if (cfqq->dispatched >= cfqd->cfq_quantum) | |
a9938006 JA |
1139 | break; |
1140 | } | |
9ede209e | 1141 | |
3b18152c JA |
1142 | cfq_clear_cfqq_must_dispatch(cfqq); |
1143 | cfq_clear_cfqq_wait_request(cfqq); | |
22e2c507 JA |
1144 | del_timer(&cfqd->idle_slice_timer); |
1145 | ||
1b5ed5e1 TH |
1146 | max_dispatch = cfqd->cfq_quantum; |
1147 | if (cfq_class_idle(cfqq)) | |
1148 | max_dispatch = 1; | |
1da177e4 | 1149 | |
caaa5f9f | 1150 | dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch); |
1da177e4 LT |
1151 | } |
1152 | ||
caaa5f9f | 1153 | return dispatched; |
1da177e4 LT |
1154 | } |
1155 | ||
1da177e4 | 1156 | /* |
5e705374 JA |
1157 | * task holds one reference to the queue, dropped when task exits. each rq |
1158 | * in-flight on this queue also holds a reference, dropped when rq is freed. | |
1da177e4 LT |
1159 | * |
1160 | * queue lock must be held here. | |
1161 | */ | |
1162 | static void cfq_put_queue(struct cfq_queue *cfqq) | |
1163 | { | |
22e2c507 JA |
1164 | struct cfq_data *cfqd = cfqq->cfqd; |
1165 | ||
1166 | BUG_ON(atomic_read(&cfqq->ref) <= 0); | |
1da177e4 LT |
1167 | |
1168 | if (!atomic_dec_and_test(&cfqq->ref)) | |
1169 | return; | |
1170 | ||
1171 | BUG_ON(rb_first(&cfqq->sort_list)); | |
22e2c507 | 1172 | BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]); |
3b18152c | 1173 | BUG_ON(cfq_cfqq_on_rr(cfqq)); |
1da177e4 | 1174 | |
28f95cbc | 1175 | if (unlikely(cfqd->active_queue == cfqq)) { |
3c6bd2f8 | 1176 | __cfq_slice_expired(cfqd, cfqq, 0, 0); |
28f95cbc JA |
1177 | cfq_schedule_dispatch(cfqd); |
1178 | } | |
22e2c507 | 1179 | |
1da177e4 LT |
1180 | /* |
1181 | * it's on the empty list and still hashed | |
1182 | */ | |
1da177e4 LT |
1183 | hlist_del(&cfqq->cfq_hash); |
1184 | kmem_cache_free(cfq_pool, cfqq); | |
1185 | } | |
1186 | ||
1ea25ecb | 1187 | static struct cfq_queue * |
3b18152c JA |
1188 | __cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio, |
1189 | const int hashval) | |
1da177e4 LT |
1190 | { |
1191 | struct hlist_head *hash_list = &cfqd->cfq_hash[hashval]; | |
206dc69b JA |
1192 | struct hlist_node *entry; |
1193 | struct cfq_queue *__cfqq; | |
1da177e4 | 1194 | |
206dc69b | 1195 | hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) { |
b0a6916b | 1196 | const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio); |
1da177e4 | 1197 | |
206dc69b | 1198 | if (__cfqq->key == key && (__p == prio || !prio)) |
1da177e4 LT |
1199 | return __cfqq; |
1200 | } | |
1201 | ||
1202 | return NULL; | |
1203 | } | |
1204 | ||
1205 | static struct cfq_queue * | |
3b18152c | 1206 | cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio) |
1da177e4 | 1207 | { |
3b18152c | 1208 | return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT)); |
1da177e4 LT |
1209 | } |
1210 | ||
e2d74ac0 | 1211 | static void cfq_free_io_context(struct io_context *ioc) |
1da177e4 | 1212 | { |
22e2c507 | 1213 | struct cfq_io_context *__cic; |
e2d74ac0 JA |
1214 | struct rb_node *n; |
1215 | int freed = 0; | |
1da177e4 | 1216 | |
e2d74ac0 JA |
1217 | while ((n = rb_first(&ioc->cic_root)) != NULL) { |
1218 | __cic = rb_entry(n, struct cfq_io_context, rb_node); | |
1219 | rb_erase(&__cic->rb_node, &ioc->cic_root); | |
22e2c507 | 1220 | kmem_cache_free(cfq_ioc_pool, __cic); |
334e94de | 1221 | freed++; |
1da177e4 LT |
1222 | } |
1223 | ||
4050cf16 JA |
1224 | elv_ioc_count_mod(ioc_count, -freed); |
1225 | ||
1226 | if (ioc_gone && !elv_ioc_count_read(ioc_count)) | |
334e94de | 1227 | complete(ioc_gone); |
1da177e4 LT |
1228 | } |
1229 | ||
89850f7e | 1230 | static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq) |
1da177e4 | 1231 | { |
28f95cbc | 1232 | if (unlikely(cfqq == cfqd->active_queue)) { |
3c6bd2f8 | 1233 | __cfq_slice_expired(cfqd, cfqq, 0, 0); |
28f95cbc JA |
1234 | cfq_schedule_dispatch(cfqd); |
1235 | } | |
22e2c507 | 1236 | |
89850f7e JA |
1237 | cfq_put_queue(cfqq); |
1238 | } | |
22e2c507 | 1239 | |
89850f7e JA |
1240 | static void __cfq_exit_single_io_context(struct cfq_data *cfqd, |
1241 | struct cfq_io_context *cic) | |
1242 | { | |
fc46379d JA |
1243 | list_del_init(&cic->queue_list); |
1244 | smp_wmb(); | |
1245 | cic->key = NULL; | |
1246 | ||
12a05732 | 1247 | if (cic->cfqq[ASYNC]) { |
89850f7e | 1248 | cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]); |
12a05732 AV |
1249 | cic->cfqq[ASYNC] = NULL; |
1250 | } | |
1251 | ||
1252 | if (cic->cfqq[SYNC]) { | |
89850f7e | 1253 | cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]); |
12a05732 AV |
1254 | cic->cfqq[SYNC] = NULL; |
1255 | } | |
89850f7e JA |
1256 | } |
1257 | ||
89850f7e JA |
1258 | static void cfq_exit_single_io_context(struct cfq_io_context *cic) |
1259 | { | |
1260 | struct cfq_data *cfqd = cic->key; | |
1261 | ||
89850f7e JA |
1262 | if (cfqd) { |
1263 | request_queue_t *q = cfqd->queue; | |
1264 | ||
fc46379d | 1265 | spin_lock_irq(q->queue_lock); |
89850f7e | 1266 | __cfq_exit_single_io_context(cfqd, cic); |
fc46379d | 1267 | spin_unlock_irq(q->queue_lock); |
89850f7e | 1268 | } |
1da177e4 LT |
1269 | } |
1270 | ||
498d3aa2 JA |
1271 | /* |
1272 | * The process that ioc belongs to has exited, we need to clean up | |
1273 | * and put the internal structures we have that belongs to that process. | |
1274 | */ | |
e2d74ac0 | 1275 | static void cfq_exit_io_context(struct io_context *ioc) |
1da177e4 | 1276 | { |
22e2c507 | 1277 | struct cfq_io_context *__cic; |
e2d74ac0 | 1278 | struct rb_node *n; |
22e2c507 | 1279 | |
1da177e4 LT |
1280 | /* |
1281 | * put the reference this task is holding to the various queues | |
1282 | */ | |
e2d74ac0 JA |
1283 | |
1284 | n = rb_first(&ioc->cic_root); | |
1285 | while (n != NULL) { | |
1286 | __cic = rb_entry(n, struct cfq_io_context, rb_node); | |
1287 | ||
22e2c507 | 1288 | cfq_exit_single_io_context(__cic); |
e2d74ac0 | 1289 | n = rb_next(n); |
1da177e4 | 1290 | } |
1da177e4 LT |
1291 | } |
1292 | ||
22e2c507 | 1293 | static struct cfq_io_context * |
8267e268 | 1294 | cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) |
1da177e4 | 1295 | { |
b5deef90 | 1296 | struct cfq_io_context *cic; |
1da177e4 | 1297 | |
b5deef90 | 1298 | cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node); |
1da177e4 | 1299 | if (cic) { |
553698f9 | 1300 | memset(cic, 0, sizeof(*cic)); |
22e2c507 | 1301 | cic->last_end_request = jiffies; |
553698f9 | 1302 | INIT_LIST_HEAD(&cic->queue_list); |
22e2c507 JA |
1303 | cic->dtor = cfq_free_io_context; |
1304 | cic->exit = cfq_exit_io_context; | |
4050cf16 | 1305 | elv_ioc_count_inc(ioc_count); |
1da177e4 LT |
1306 | } |
1307 | ||
1308 | return cic; | |
1309 | } | |
1310 | ||
22e2c507 JA |
1311 | static void cfq_init_prio_data(struct cfq_queue *cfqq) |
1312 | { | |
1313 | struct task_struct *tsk = current; | |
1314 | int ioprio_class; | |
1315 | ||
3b18152c | 1316 | if (!cfq_cfqq_prio_changed(cfqq)) |
22e2c507 JA |
1317 | return; |
1318 | ||
1319 | ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio); | |
1320 | switch (ioprio_class) { | |
1321 | default: | |
1322 | printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class); | |
1323 | case IOPRIO_CLASS_NONE: | |
1324 | /* | |
1325 | * no prio set, place us in the middle of the BE classes | |
1326 | */ | |
1327 | cfqq->ioprio = task_nice_ioprio(tsk); | |
1328 | cfqq->ioprio_class = IOPRIO_CLASS_BE; | |
1329 | break; | |
1330 | case IOPRIO_CLASS_RT: | |
1331 | cfqq->ioprio = task_ioprio(tsk); | |
1332 | cfqq->ioprio_class = IOPRIO_CLASS_RT; | |
1333 | break; | |
1334 | case IOPRIO_CLASS_BE: | |
1335 | cfqq->ioprio = task_ioprio(tsk); | |
1336 | cfqq->ioprio_class = IOPRIO_CLASS_BE; | |
1337 | break; | |
1338 | case IOPRIO_CLASS_IDLE: | |
1339 | cfqq->ioprio_class = IOPRIO_CLASS_IDLE; | |
1340 | cfqq->ioprio = 7; | |
3b18152c | 1341 | cfq_clear_cfqq_idle_window(cfqq); |
22e2c507 JA |
1342 | break; |
1343 | } | |
1344 | ||
1345 | /* | |
1346 | * keep track of original prio settings in case we have to temporarily | |
1347 | * elevate the priority of this queue | |
1348 | */ | |
1349 | cfqq->org_ioprio = cfqq->ioprio; | |
1350 | cfqq->org_ioprio_class = cfqq->ioprio_class; | |
3b18152c | 1351 | cfq_clear_cfqq_prio_changed(cfqq); |
22e2c507 JA |
1352 | } |
1353 | ||
478a82b0 | 1354 | static inline void changed_ioprio(struct cfq_io_context *cic) |
22e2c507 | 1355 | { |
478a82b0 AV |
1356 | struct cfq_data *cfqd = cic->key; |
1357 | struct cfq_queue *cfqq; | |
c1b707d2 | 1358 | unsigned long flags; |
35e6077c | 1359 | |
caaa5f9f JA |
1360 | if (unlikely(!cfqd)) |
1361 | return; | |
1362 | ||
c1b707d2 | 1363 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
caaa5f9f JA |
1364 | |
1365 | cfqq = cic->cfqq[ASYNC]; | |
1366 | if (cfqq) { | |
1367 | struct cfq_queue *new_cfqq; | |
1368 | new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task, | |
1369 | GFP_ATOMIC); | |
1370 | if (new_cfqq) { | |
1371 | cic->cfqq[ASYNC] = new_cfqq; | |
1372 | cfq_put_queue(cfqq); | |
1373 | } | |
22e2c507 | 1374 | } |
caaa5f9f JA |
1375 | |
1376 | cfqq = cic->cfqq[SYNC]; | |
1377 | if (cfqq) | |
1378 | cfq_mark_cfqq_prio_changed(cfqq); | |
1379 | ||
c1b707d2 | 1380 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
22e2c507 JA |
1381 | } |
1382 | ||
fc46379d | 1383 | static void cfq_ioc_set_ioprio(struct io_context *ioc) |
22e2c507 | 1384 | { |
a6a0763a | 1385 | struct cfq_io_context *cic; |
e2d74ac0 | 1386 | struct rb_node *n; |
a6a0763a | 1387 | |
fc46379d | 1388 | ioc->ioprio_changed = 0; |
a6a0763a | 1389 | |
e2d74ac0 JA |
1390 | n = rb_first(&ioc->cic_root); |
1391 | while (n != NULL) { | |
1392 | cic = rb_entry(n, struct cfq_io_context, rb_node); | |
3793c65c | 1393 | |
478a82b0 | 1394 | changed_ioprio(cic); |
e2d74ac0 JA |
1395 | n = rb_next(n); |
1396 | } | |
22e2c507 JA |
1397 | } |
1398 | ||
1399 | static struct cfq_queue * | |
6f325a13 | 1400 | cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk, |
8267e268 | 1401 | gfp_t gfp_mask) |
22e2c507 JA |
1402 | { |
1403 | const int hashval = hash_long(key, CFQ_QHASH_SHIFT); | |
1404 | struct cfq_queue *cfqq, *new_cfqq = NULL; | |
6f325a13 | 1405 | unsigned short ioprio; |
22e2c507 JA |
1406 | |
1407 | retry: | |
6f325a13 | 1408 | ioprio = tsk->ioprio; |
3b18152c | 1409 | cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval); |
22e2c507 JA |
1410 | |
1411 | if (!cfqq) { | |
1412 | if (new_cfqq) { | |
1413 | cfqq = new_cfqq; | |
1414 | new_cfqq = NULL; | |
1415 | } else if (gfp_mask & __GFP_WAIT) { | |
89850f7e JA |
1416 | /* |
1417 | * Inform the allocator of the fact that we will | |
1418 | * just repeat this allocation if it fails, to allow | |
1419 | * the allocator to do whatever it needs to attempt to | |
1420 | * free memory. | |
1421 | */ | |
22e2c507 | 1422 | spin_unlock_irq(cfqd->queue->queue_lock); |
b5deef90 | 1423 | new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node); |
22e2c507 JA |
1424 | spin_lock_irq(cfqd->queue->queue_lock); |
1425 | goto retry; | |
1426 | } else { | |
b5deef90 | 1427 | cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node); |
22e2c507 JA |
1428 | if (!cfqq) |
1429 | goto out; | |
1430 | } | |
1431 | ||
1432 | memset(cfqq, 0, sizeof(*cfqq)); | |
1433 | ||
1434 | INIT_HLIST_NODE(&cfqq->cfq_hash); | |
1435 | INIT_LIST_HEAD(&cfqq->cfq_list); | |
d9e7620e | 1436 | RB_CLEAR_NODE(&cfqq->rb_node); |
22e2c507 JA |
1437 | INIT_LIST_HEAD(&cfqq->fifo); |
1438 | ||
1439 | cfqq->key = key; | |
1440 | hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]); | |
1441 | atomic_set(&cfqq->ref, 0); | |
1442 | cfqq->cfqd = cfqd; | |
c5b680f3 | 1443 | |
a9938006 JA |
1444 | if (key != CFQ_KEY_ASYNC) |
1445 | cfq_mark_cfqq_idle_window(cfqq); | |
1446 | ||
3b18152c | 1447 | cfq_mark_cfqq_prio_changed(cfqq); |
53b03744 | 1448 | cfq_mark_cfqq_queue_new(cfqq); |
3b18152c | 1449 | cfq_init_prio_data(cfqq); |
22e2c507 JA |
1450 | } |
1451 | ||
1452 | if (new_cfqq) | |
1453 | kmem_cache_free(cfq_pool, new_cfqq); | |
1454 | ||
1455 | atomic_inc(&cfqq->ref); | |
1456 | out: | |
1457 | WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq); | |
1458 | return cfqq; | |
1459 | } | |
1460 | ||
498d3aa2 JA |
1461 | /* |
1462 | * We drop cfq io contexts lazily, so we may find a dead one. | |
1463 | */ | |
dbecf3ab OH |
1464 | static void |
1465 | cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic) | |
1466 | { | |
fc46379d | 1467 | WARN_ON(!list_empty(&cic->queue_list)); |
dbecf3ab | 1468 | rb_erase(&cic->rb_node, &ioc->cic_root); |
dbecf3ab | 1469 | kmem_cache_free(cfq_ioc_pool, cic); |
4050cf16 | 1470 | elv_ioc_count_dec(ioc_count); |
dbecf3ab OH |
1471 | } |
1472 | ||
e2d74ac0 JA |
1473 | static struct cfq_io_context * |
1474 | cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc) | |
1475 | { | |
dbecf3ab | 1476 | struct rb_node *n; |
e2d74ac0 | 1477 | struct cfq_io_context *cic; |
be3b0753 | 1478 | void *k, *key = cfqd; |
e2d74ac0 | 1479 | |
dbecf3ab OH |
1480 | restart: |
1481 | n = ioc->cic_root.rb_node; | |
e2d74ac0 JA |
1482 | while (n) { |
1483 | cic = rb_entry(n, struct cfq_io_context, rb_node); | |
be3b0753 OH |
1484 | /* ->key must be copied to avoid race with cfq_exit_queue() */ |
1485 | k = cic->key; | |
1486 | if (unlikely(!k)) { | |
dbecf3ab OH |
1487 | cfq_drop_dead_cic(ioc, cic); |
1488 | goto restart; | |
1489 | } | |
e2d74ac0 | 1490 | |
be3b0753 | 1491 | if (key < k) |
e2d74ac0 | 1492 | n = n->rb_left; |
be3b0753 | 1493 | else if (key > k) |
e2d74ac0 JA |
1494 | n = n->rb_right; |
1495 | else | |
1496 | return cic; | |
1497 | } | |
1498 | ||
1499 | return NULL; | |
1500 | } | |
1501 | ||
1502 | static inline void | |
1503 | cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc, | |
1504 | struct cfq_io_context *cic) | |
1505 | { | |
dbecf3ab OH |
1506 | struct rb_node **p; |
1507 | struct rb_node *parent; | |
e2d74ac0 | 1508 | struct cfq_io_context *__cic; |
0261d688 | 1509 | unsigned long flags; |
be3b0753 | 1510 | void *k; |
e2d74ac0 | 1511 | |
e2d74ac0 JA |
1512 | cic->ioc = ioc; |
1513 | cic->key = cfqd; | |
1514 | ||
dbecf3ab OH |
1515 | restart: |
1516 | parent = NULL; | |
1517 | p = &ioc->cic_root.rb_node; | |
e2d74ac0 JA |
1518 | while (*p) { |
1519 | parent = *p; | |
1520 | __cic = rb_entry(parent, struct cfq_io_context, rb_node); | |
be3b0753 OH |
1521 | /* ->key must be copied to avoid race with cfq_exit_queue() */ |
1522 | k = __cic->key; | |
1523 | if (unlikely(!k)) { | |
be33c3a6 | 1524 | cfq_drop_dead_cic(ioc, __cic); |
dbecf3ab OH |
1525 | goto restart; |
1526 | } | |
e2d74ac0 | 1527 | |
be3b0753 | 1528 | if (cic->key < k) |
e2d74ac0 | 1529 | p = &(*p)->rb_left; |
be3b0753 | 1530 | else if (cic->key > k) |
e2d74ac0 JA |
1531 | p = &(*p)->rb_right; |
1532 | else | |
1533 | BUG(); | |
1534 | } | |
1535 | ||
1536 | rb_link_node(&cic->rb_node, parent, p); | |
1537 | rb_insert_color(&cic->rb_node, &ioc->cic_root); | |
fc46379d | 1538 | |
0261d688 | 1539 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); |
e2d74ac0 | 1540 | list_add(&cic->queue_list, &cfqd->cic_list); |
0261d688 | 1541 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); |
e2d74ac0 JA |
1542 | } |
1543 | ||
1da177e4 LT |
1544 | /* |
1545 | * Setup general io context and cfq io context. There can be several cfq | |
1546 | * io contexts per general io context, if this process is doing io to more | |
e2d74ac0 | 1547 | * than one device managed by cfq. |
1da177e4 LT |
1548 | */ |
1549 | static struct cfq_io_context * | |
e2d74ac0 | 1550 | cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask) |
1da177e4 | 1551 | { |
22e2c507 | 1552 | struct io_context *ioc = NULL; |
1da177e4 | 1553 | struct cfq_io_context *cic; |
1da177e4 | 1554 | |
22e2c507 | 1555 | might_sleep_if(gfp_mask & __GFP_WAIT); |
1da177e4 | 1556 | |
b5deef90 | 1557 | ioc = get_io_context(gfp_mask, cfqd->queue->node); |
1da177e4 LT |
1558 | if (!ioc) |
1559 | return NULL; | |
1560 | ||
e2d74ac0 JA |
1561 | cic = cfq_cic_rb_lookup(cfqd, ioc); |
1562 | if (cic) | |
1563 | goto out; | |
1da177e4 | 1564 | |
e2d74ac0 JA |
1565 | cic = cfq_alloc_io_context(cfqd, gfp_mask); |
1566 | if (cic == NULL) | |
1567 | goto err; | |
1da177e4 | 1568 | |
e2d74ac0 | 1569 | cfq_cic_link(cfqd, ioc, cic); |
1da177e4 | 1570 | out: |
fc46379d JA |
1571 | smp_read_barrier_depends(); |
1572 | if (unlikely(ioc->ioprio_changed)) | |
1573 | cfq_ioc_set_ioprio(ioc); | |
1574 | ||
1da177e4 LT |
1575 | return cic; |
1576 | err: | |
1577 | put_io_context(ioc); | |
1578 | return NULL; | |
1579 | } | |
1580 | ||
22e2c507 JA |
1581 | static void |
1582 | cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic) | |
1da177e4 | 1583 | { |
aaf1228d JA |
1584 | unsigned long elapsed = jiffies - cic->last_end_request; |
1585 | unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle); | |
db3b5848 | 1586 | |
22e2c507 JA |
1587 | cic->ttime_samples = (7*cic->ttime_samples + 256) / 8; |
1588 | cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8; | |
1589 | cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples; | |
1590 | } | |
1da177e4 | 1591 | |
206dc69b | 1592 | static void |
6d048f53 JA |
1593 | cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic, |
1594 | struct request *rq) | |
206dc69b JA |
1595 | { |
1596 | sector_t sdist; | |
1597 | u64 total; | |
1598 | ||
5e705374 JA |
1599 | if (cic->last_request_pos < rq->sector) |
1600 | sdist = rq->sector - cic->last_request_pos; | |
206dc69b | 1601 | else |
5e705374 | 1602 | sdist = cic->last_request_pos - rq->sector; |
206dc69b | 1603 | |
6d048f53 JA |
1604 | if (!cic->seek_samples) { |
1605 | cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8; | |
1606 | cfqd->new_seek_mean = cfqd->new_seek_total / 256; | |
1607 | } | |
1608 | ||
206dc69b JA |
1609 | /* |
1610 | * Don't allow the seek distance to get too large from the | |
1611 | * odd fragment, pagein, etc | |
1612 | */ | |
1613 | if (cic->seek_samples <= 60) /* second&third seek */ | |
1614 | sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024); | |
1615 | else | |
1616 | sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64); | |
1617 | ||
1618 | cic->seek_samples = (7*cic->seek_samples + 256) / 8; | |
1619 | cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8; | |
1620 | total = cic->seek_total + (cic->seek_samples/2); | |
1621 | do_div(total, cic->seek_samples); | |
1622 | cic->seek_mean = (sector_t)total; | |
1623 | } | |
1da177e4 | 1624 | |
22e2c507 JA |
1625 | /* |
1626 | * Disable idle window if the process thinks too long or seeks so much that | |
1627 | * it doesn't matter | |
1628 | */ | |
1629 | static void | |
1630 | cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq, | |
1631 | struct cfq_io_context *cic) | |
1632 | { | |
3b18152c | 1633 | int enable_idle = cfq_cfqq_idle_window(cfqq); |
1da177e4 | 1634 | |
caaa5f9f JA |
1635 | if (!cic->ioc->task || !cfqd->cfq_slice_idle || |
1636 | (cfqd->hw_tag && CIC_SEEKY(cic))) | |
22e2c507 JA |
1637 | enable_idle = 0; |
1638 | else if (sample_valid(cic->ttime_samples)) { | |
1639 | if (cic->ttime_mean > cfqd->cfq_slice_idle) | |
1640 | enable_idle = 0; | |
1641 | else | |
1642 | enable_idle = 1; | |
1da177e4 LT |
1643 | } |
1644 | ||
3b18152c JA |
1645 | if (enable_idle) |
1646 | cfq_mark_cfqq_idle_window(cfqq); | |
1647 | else | |
1648 | cfq_clear_cfqq_idle_window(cfqq); | |
22e2c507 | 1649 | } |
1da177e4 | 1650 | |
22e2c507 JA |
1651 | /* |
1652 | * Check if new_cfqq should preempt the currently active queue. Return 0 for | |
1653 | * no or if we aren't sure, a 1 will cause a preempt. | |
1654 | */ | |
1655 | static int | |
1656 | cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq, | |
5e705374 | 1657 | struct request *rq) |
22e2c507 | 1658 | { |
6d048f53 | 1659 | struct cfq_queue *cfqq; |
22e2c507 | 1660 | |
6d048f53 JA |
1661 | cfqq = cfqd->active_queue; |
1662 | if (!cfqq) | |
22e2c507 JA |
1663 | return 0; |
1664 | ||
6d048f53 JA |
1665 | if (cfq_slice_used(cfqq)) |
1666 | return 1; | |
1667 | ||
1668 | if (cfq_class_idle(new_cfqq)) | |
caaa5f9f | 1669 | return 0; |
22e2c507 JA |
1670 | |
1671 | if (cfq_class_idle(cfqq)) | |
1672 | return 1; | |
1e3335de | 1673 | |
374f84ac JA |
1674 | /* |
1675 | * if the new request is sync, but the currently running queue is | |
1676 | * not, let the sync request have priority. | |
1677 | */ | |
5e705374 | 1678 | if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq)) |
22e2c507 | 1679 | return 1; |
1e3335de | 1680 | |
374f84ac JA |
1681 | /* |
1682 | * So both queues are sync. Let the new request get disk time if | |
1683 | * it's a metadata request and the current queue is doing regular IO. | |
1684 | */ | |
1685 | if (rq_is_meta(rq) && !cfqq->meta_pending) | |
1686 | return 1; | |
22e2c507 | 1687 | |
1e3335de JA |
1688 | if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq)) |
1689 | return 0; | |
1690 | ||
1691 | /* | |
1692 | * if this request is as-good as one we would expect from the | |
1693 | * current cfqq, let it preempt | |
1694 | */ | |
6d048f53 | 1695 | if (cfq_rq_close(cfqd, rq)) |
1e3335de JA |
1696 | return 1; |
1697 | ||
22e2c507 JA |
1698 | return 0; |
1699 | } | |
1700 | ||
1701 | /* | |
1702 | * cfqq preempts the active queue. if we allowed preempt with no slice left, | |
1703 | * let it have half of its nominal slice. | |
1704 | */ | |
1705 | static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq) | |
1706 | { | |
3c6bd2f8 | 1707 | cfq_slice_expired(cfqd, 1, 1); |
22e2c507 | 1708 | |
bf572256 JA |
1709 | /* |
1710 | * Put the new queue at the front of the of the current list, | |
1711 | * so we know that it will be selected next. | |
1712 | */ | |
1713 | BUG_ON(!cfq_cfqq_on_rr(cfqq)); | |
d9e7620e JA |
1714 | list_del_init(&cfqq->cfq_list); |
1715 | list_add(&cfqq->cfq_list, &cfqd->cur_rr); | |
bf572256 | 1716 | |
44f7c160 JA |
1717 | cfqq->slice_end = 0; |
1718 | cfq_mark_cfqq_slice_new(cfqq); | |
22e2c507 JA |
1719 | } |
1720 | ||
22e2c507 | 1721 | /* |
5e705374 | 1722 | * Called when a new fs request (rq) is added (to cfqq). Check if there's |
22e2c507 JA |
1723 | * something we should do about it |
1724 | */ | |
1725 | static void | |
5e705374 JA |
1726 | cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq, |
1727 | struct request *rq) | |
22e2c507 | 1728 | { |
5e705374 | 1729 | struct cfq_io_context *cic = RQ_CIC(rq); |
12e9fddd | 1730 | |
374f84ac JA |
1731 | if (rq_is_meta(rq)) |
1732 | cfqq->meta_pending++; | |
1733 | ||
9c2c38a1 | 1734 | cfq_update_io_thinktime(cfqd, cic); |
6d048f53 | 1735 | cfq_update_io_seektime(cfqd, cic, rq); |
9c2c38a1 JA |
1736 | cfq_update_idle_window(cfqd, cfqq, cic); |
1737 | ||
5e705374 | 1738 | cic->last_request_pos = rq->sector + rq->nr_sectors; |
6d048f53 | 1739 | cfqq->last_request_pos = cic->last_request_pos; |
22e2c507 JA |
1740 | |
1741 | if (cfqq == cfqd->active_queue) { | |
1742 | /* | |
1743 | * if we are waiting for a request for this queue, let it rip | |
1744 | * immediately and flag that we must not expire this queue | |
1745 | * just now | |
1746 | */ | |
3b18152c JA |
1747 | if (cfq_cfqq_wait_request(cfqq)) { |
1748 | cfq_mark_cfqq_must_dispatch(cfqq); | |
22e2c507 | 1749 | del_timer(&cfqd->idle_slice_timer); |
dc72ef4a | 1750 | blk_start_queueing(cfqd->queue); |
22e2c507 | 1751 | } |
5e705374 | 1752 | } else if (cfq_should_preempt(cfqd, cfqq, rq)) { |
22e2c507 JA |
1753 | /* |
1754 | * not the active queue - expire current slice if it is | |
1755 | * idle and has expired it's mean thinktime or this new queue | |
1756 | * has some old slice time left and is of higher priority | |
1757 | */ | |
1758 | cfq_preempt_queue(cfqd, cfqq); | |
3b18152c | 1759 | cfq_mark_cfqq_must_dispatch(cfqq); |
dc72ef4a | 1760 | blk_start_queueing(cfqd->queue); |
22e2c507 | 1761 | } |
1da177e4 LT |
1762 | } |
1763 | ||
b4878f24 | 1764 | static void cfq_insert_request(request_queue_t *q, struct request *rq) |
1da177e4 | 1765 | { |
b4878f24 | 1766 | struct cfq_data *cfqd = q->elevator->elevator_data; |
5e705374 | 1767 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
22e2c507 JA |
1768 | |
1769 | cfq_init_prio_data(cfqq); | |
1da177e4 | 1770 | |
5e705374 | 1771 | cfq_add_rq_rb(rq); |
1da177e4 | 1772 | |
22e2c507 JA |
1773 | list_add_tail(&rq->queuelist, &cfqq->fifo); |
1774 | ||
5e705374 | 1775 | cfq_rq_enqueued(cfqd, cfqq, rq); |
1da177e4 LT |
1776 | } |
1777 | ||
1da177e4 LT |
1778 | static void cfq_completed_request(request_queue_t *q, struct request *rq) |
1779 | { | |
5e705374 | 1780 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
b4878f24 | 1781 | struct cfq_data *cfqd = cfqq->cfqd; |
5380a101 | 1782 | const int sync = rq_is_sync(rq); |
b4878f24 | 1783 | unsigned long now; |
1da177e4 | 1784 | |
b4878f24 | 1785 | now = jiffies; |
1da177e4 | 1786 | |
b4878f24 | 1787 | WARN_ON(!cfqd->rq_in_driver); |
6d048f53 | 1788 | WARN_ON(!cfqq->dispatched); |
b4878f24 | 1789 | cfqd->rq_in_driver--; |
6d048f53 | 1790 | cfqq->dispatched--; |
1da177e4 | 1791 | |
b4878f24 JA |
1792 | if (!cfq_class_idle(cfqq)) |
1793 | cfqd->last_end_request = now; | |
3b18152c | 1794 | |
caaa5f9f | 1795 | if (sync) |
5e705374 | 1796 | RQ_CIC(rq)->last_end_request = now; |
caaa5f9f JA |
1797 | |
1798 | /* | |
1799 | * If this is the active queue, check if it needs to be expired, | |
1800 | * or if we want to idle in case it has no pending requests. | |
1801 | */ | |
1802 | if (cfqd->active_queue == cfqq) { | |
44f7c160 JA |
1803 | if (cfq_cfqq_slice_new(cfqq)) { |
1804 | cfq_set_prio_slice(cfqd, cfqq); | |
1805 | cfq_clear_cfqq_slice_new(cfqq); | |
1806 | } | |
1807 | if (cfq_slice_used(cfqq)) | |
3c6bd2f8 | 1808 | cfq_slice_expired(cfqd, 0, 1); |
6d048f53 JA |
1809 | else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list)) |
1810 | cfq_arm_slice_timer(cfqd); | |
caaa5f9f | 1811 | } |
6d048f53 JA |
1812 | |
1813 | if (!cfqd->rq_in_driver) | |
1814 | cfq_schedule_dispatch(cfqd); | |
1da177e4 LT |
1815 | } |
1816 | ||
22e2c507 JA |
1817 | /* |
1818 | * we temporarily boost lower priority queues if they are holding fs exclusive | |
1819 | * resources. they are boosted to normal prio (CLASS_BE/4) | |
1820 | */ | |
1821 | static void cfq_prio_boost(struct cfq_queue *cfqq) | |
1da177e4 | 1822 | { |
22e2c507 JA |
1823 | if (has_fs_excl()) { |
1824 | /* | |
1825 | * boost idle prio on transactions that would lock out other | |
1826 | * users of the filesystem | |
1827 | */ | |
1828 | if (cfq_class_idle(cfqq)) | |
1829 | cfqq->ioprio_class = IOPRIO_CLASS_BE; | |
1830 | if (cfqq->ioprio > IOPRIO_NORM) | |
1831 | cfqq->ioprio = IOPRIO_NORM; | |
1832 | } else { | |
1833 | /* | |
1834 | * check if we need to unboost the queue | |
1835 | */ | |
1836 | if (cfqq->ioprio_class != cfqq->org_ioprio_class) | |
1837 | cfqq->ioprio_class = cfqq->org_ioprio_class; | |
1838 | if (cfqq->ioprio != cfqq->org_ioprio) | |
1839 | cfqq->ioprio = cfqq->org_ioprio; | |
1840 | } | |
22e2c507 | 1841 | } |
1da177e4 | 1842 | |
89850f7e | 1843 | static inline int __cfq_may_queue(struct cfq_queue *cfqq) |
22e2c507 | 1844 | { |
3b18152c | 1845 | if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) && |
99f95e52 | 1846 | !cfq_cfqq_must_alloc_slice(cfqq)) { |
3b18152c | 1847 | cfq_mark_cfqq_must_alloc_slice(cfqq); |
22e2c507 | 1848 | return ELV_MQUEUE_MUST; |
3b18152c | 1849 | } |
1da177e4 | 1850 | |
22e2c507 | 1851 | return ELV_MQUEUE_MAY; |
22e2c507 JA |
1852 | } |
1853 | ||
cb78b285 | 1854 | static int cfq_may_queue(request_queue_t *q, int rw) |
22e2c507 JA |
1855 | { |
1856 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
1857 | struct task_struct *tsk = current; | |
1858 | struct cfq_queue *cfqq; | |
7749a8d4 JA |
1859 | unsigned int key; |
1860 | ||
1861 | key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC); | |
22e2c507 JA |
1862 | |
1863 | /* | |
1864 | * don't force setup of a queue from here, as a call to may_queue | |
1865 | * does not necessarily imply that a request actually will be queued. | |
1866 | * so just lookup a possibly existing queue, or return 'may queue' | |
1867 | * if that fails | |
1868 | */ | |
7749a8d4 | 1869 | cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio); |
22e2c507 JA |
1870 | if (cfqq) { |
1871 | cfq_init_prio_data(cfqq); | |
1872 | cfq_prio_boost(cfqq); | |
1873 | ||
89850f7e | 1874 | return __cfq_may_queue(cfqq); |
22e2c507 JA |
1875 | } |
1876 | ||
1877 | return ELV_MQUEUE_MAY; | |
1da177e4 LT |
1878 | } |
1879 | ||
1da177e4 LT |
1880 | /* |
1881 | * queue lock held here | |
1882 | */ | |
bb37b94c | 1883 | static void cfq_put_request(struct request *rq) |
1da177e4 | 1884 | { |
5e705374 | 1885 | struct cfq_queue *cfqq = RQ_CFQQ(rq); |
1da177e4 | 1886 | |
5e705374 | 1887 | if (cfqq) { |
22e2c507 | 1888 | const int rw = rq_data_dir(rq); |
1da177e4 | 1889 | |
22e2c507 JA |
1890 | BUG_ON(!cfqq->allocated[rw]); |
1891 | cfqq->allocated[rw]--; | |
1da177e4 | 1892 | |
5e705374 | 1893 | put_io_context(RQ_CIC(rq)->ioc); |
1da177e4 | 1894 | |
1da177e4 | 1895 | rq->elevator_private = NULL; |
5e705374 | 1896 | rq->elevator_private2 = NULL; |
1da177e4 | 1897 | |
1da177e4 LT |
1898 | cfq_put_queue(cfqq); |
1899 | } | |
1900 | } | |
1901 | ||
1902 | /* | |
22e2c507 | 1903 | * Allocate cfq data structures associated with this request. |
1da177e4 | 1904 | */ |
22e2c507 | 1905 | static int |
cb78b285 | 1906 | cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask) |
1da177e4 LT |
1907 | { |
1908 | struct cfq_data *cfqd = q->elevator->elevator_data; | |
3b18152c | 1909 | struct task_struct *tsk = current; |
1da177e4 LT |
1910 | struct cfq_io_context *cic; |
1911 | const int rw = rq_data_dir(rq); | |
7749a8d4 JA |
1912 | const int is_sync = rq_is_sync(rq); |
1913 | pid_t key = cfq_queue_pid(tsk, rw, is_sync); | |
22e2c507 | 1914 | struct cfq_queue *cfqq; |
1da177e4 LT |
1915 | unsigned long flags; |
1916 | ||
1917 | might_sleep_if(gfp_mask & __GFP_WAIT); | |
1918 | ||
e2d74ac0 | 1919 | cic = cfq_get_io_context(cfqd, gfp_mask); |
22e2c507 | 1920 | |
1da177e4 LT |
1921 | spin_lock_irqsave(q->queue_lock, flags); |
1922 | ||
22e2c507 JA |
1923 | if (!cic) |
1924 | goto queue_fail; | |
1925 | ||
12a05732 | 1926 | if (!cic->cfqq[is_sync]) { |
6f325a13 | 1927 | cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask); |
22e2c507 JA |
1928 | if (!cfqq) |
1929 | goto queue_fail; | |
1da177e4 | 1930 | |
12a05732 | 1931 | cic->cfqq[is_sync] = cfqq; |
22e2c507 | 1932 | } else |
12a05732 | 1933 | cfqq = cic->cfqq[is_sync]; |
1da177e4 LT |
1934 | |
1935 | cfqq->allocated[rw]++; | |
3b18152c | 1936 | cfq_clear_cfqq_must_alloc(cfqq); |
22e2c507 | 1937 | atomic_inc(&cfqq->ref); |
1da177e4 | 1938 | |
5e705374 | 1939 | spin_unlock_irqrestore(q->queue_lock, flags); |
3b18152c | 1940 | |
5e705374 JA |
1941 | rq->elevator_private = cic; |
1942 | rq->elevator_private2 = cfqq; | |
1943 | return 0; | |
1da177e4 | 1944 | |
22e2c507 JA |
1945 | queue_fail: |
1946 | if (cic) | |
1947 | put_io_context(cic->ioc); | |
89850f7e | 1948 | |
3b18152c | 1949 | cfq_schedule_dispatch(cfqd); |
1da177e4 LT |
1950 | spin_unlock_irqrestore(q->queue_lock, flags); |
1951 | return 1; | |
1952 | } | |
1953 | ||
65f27f38 | 1954 | static void cfq_kick_queue(struct work_struct *work) |
22e2c507 | 1955 | { |
65f27f38 DH |
1956 | struct cfq_data *cfqd = |
1957 | container_of(work, struct cfq_data, unplug_work); | |
1958 | request_queue_t *q = cfqd->queue; | |
22e2c507 JA |
1959 | unsigned long flags; |
1960 | ||
1961 | spin_lock_irqsave(q->queue_lock, flags); | |
dc72ef4a | 1962 | blk_start_queueing(q); |
22e2c507 JA |
1963 | spin_unlock_irqrestore(q->queue_lock, flags); |
1964 | } | |
1965 | ||
1966 | /* | |
1967 | * Timer running if the active_queue is currently idling inside its time slice | |
1968 | */ | |
1969 | static void cfq_idle_slice_timer(unsigned long data) | |
1970 | { | |
1971 | struct cfq_data *cfqd = (struct cfq_data *) data; | |
1972 | struct cfq_queue *cfqq; | |
1973 | unsigned long flags; | |
3c6bd2f8 | 1974 | int timed_out = 1; |
22e2c507 JA |
1975 | |
1976 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); | |
1977 | ||
1978 | if ((cfqq = cfqd->active_queue) != NULL) { | |
3c6bd2f8 JA |
1979 | timed_out = 0; |
1980 | ||
22e2c507 JA |
1981 | /* |
1982 | * expired | |
1983 | */ | |
44f7c160 | 1984 | if (cfq_slice_used(cfqq)) |
22e2c507 JA |
1985 | goto expire; |
1986 | ||
1987 | /* | |
1988 | * only expire and reinvoke request handler, if there are | |
1989 | * other queues with pending requests | |
1990 | */ | |
caaa5f9f | 1991 | if (!cfqd->busy_queues) |
22e2c507 | 1992 | goto out_cont; |
22e2c507 JA |
1993 | |
1994 | /* | |
1995 | * not expired and it has a request pending, let it dispatch | |
1996 | */ | |
dd67d051 | 1997 | if (!RB_EMPTY_ROOT(&cfqq->sort_list)) { |
3b18152c | 1998 | cfq_mark_cfqq_must_dispatch(cfqq); |
22e2c507 JA |
1999 | goto out_kick; |
2000 | } | |
2001 | } | |
2002 | expire: | |
3c6bd2f8 | 2003 | cfq_slice_expired(cfqd, 0, timed_out); |
22e2c507 | 2004 | out_kick: |
3b18152c | 2005 | cfq_schedule_dispatch(cfqd); |
22e2c507 JA |
2006 | out_cont: |
2007 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); | |
2008 | } | |
2009 | ||
2010 | /* | |
2011 | * Timer running if an idle class queue is waiting for service | |
2012 | */ | |
2013 | static void cfq_idle_class_timer(unsigned long data) | |
2014 | { | |
2015 | struct cfq_data *cfqd = (struct cfq_data *) data; | |
2016 | unsigned long flags, end; | |
2017 | ||
2018 | spin_lock_irqsave(cfqd->queue->queue_lock, flags); | |
2019 | ||
2020 | /* | |
2021 | * race with a non-idle queue, reset timer | |
2022 | */ | |
2023 | end = cfqd->last_end_request + CFQ_IDLE_GRACE; | |
ae818a38 JA |
2024 | if (!time_after_eq(jiffies, end)) |
2025 | mod_timer(&cfqd->idle_class_timer, end); | |
2026 | else | |
3b18152c | 2027 | cfq_schedule_dispatch(cfqd); |
22e2c507 JA |
2028 | |
2029 | spin_unlock_irqrestore(cfqd->queue->queue_lock, flags); | |
2030 | } | |
2031 | ||
3b18152c JA |
2032 | static void cfq_shutdown_timer_wq(struct cfq_data *cfqd) |
2033 | { | |
2034 | del_timer_sync(&cfqd->idle_slice_timer); | |
2035 | del_timer_sync(&cfqd->idle_class_timer); | |
2036 | blk_sync_queue(cfqd->queue); | |
2037 | } | |
22e2c507 | 2038 | |
1da177e4 LT |
2039 | static void cfq_exit_queue(elevator_t *e) |
2040 | { | |
22e2c507 | 2041 | struct cfq_data *cfqd = e->elevator_data; |
d9ff4187 | 2042 | request_queue_t *q = cfqd->queue; |
22e2c507 | 2043 | |
3b18152c | 2044 | cfq_shutdown_timer_wq(cfqd); |
e2d74ac0 | 2045 | |
d9ff4187 | 2046 | spin_lock_irq(q->queue_lock); |
e2d74ac0 | 2047 | |
d9ff4187 | 2048 | if (cfqd->active_queue) |
3c6bd2f8 | 2049 | __cfq_slice_expired(cfqd, cfqd->active_queue, 0, 0); |
e2d74ac0 JA |
2050 | |
2051 | while (!list_empty(&cfqd->cic_list)) { | |
d9ff4187 AV |
2052 | struct cfq_io_context *cic = list_entry(cfqd->cic_list.next, |
2053 | struct cfq_io_context, | |
2054 | queue_list); | |
89850f7e JA |
2055 | |
2056 | __cfq_exit_single_io_context(cfqd, cic); | |
d9ff4187 | 2057 | } |
e2d74ac0 | 2058 | |
d9ff4187 | 2059 | spin_unlock_irq(q->queue_lock); |
a90d742e AV |
2060 | |
2061 | cfq_shutdown_timer_wq(cfqd); | |
2062 | ||
a90d742e AV |
2063 | kfree(cfqd->cfq_hash); |
2064 | kfree(cfqd); | |
1da177e4 LT |
2065 | } |
2066 | ||
bb37b94c | 2067 | static void *cfq_init_queue(request_queue_t *q) |
1da177e4 LT |
2068 | { |
2069 | struct cfq_data *cfqd; | |
2070 | int i; | |
2071 | ||
b5deef90 | 2072 | cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node); |
1da177e4 | 2073 | if (!cfqd) |
bc1c1169 | 2074 | return NULL; |
1da177e4 LT |
2075 | |
2076 | memset(cfqd, 0, sizeof(*cfqd)); | |
22e2c507 | 2077 | |
cc09e299 | 2078 | cfqd->service_tree = CFQ_RB_ROOT; |
22e2c507 | 2079 | INIT_LIST_HEAD(&cfqd->cur_rr); |
d9ff4187 | 2080 | INIT_LIST_HEAD(&cfqd->cic_list); |
1da177e4 | 2081 | |
b5deef90 | 2082 | cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node); |
1da177e4 | 2083 | if (!cfqd->cfq_hash) |
5e705374 | 2084 | goto out_free; |
1da177e4 | 2085 | |
1da177e4 LT |
2086 | for (i = 0; i < CFQ_QHASH_ENTRIES; i++) |
2087 | INIT_HLIST_HEAD(&cfqd->cfq_hash[i]); | |
2088 | ||
1da177e4 | 2089 | cfqd->queue = q; |
1da177e4 | 2090 | |
22e2c507 JA |
2091 | init_timer(&cfqd->idle_slice_timer); |
2092 | cfqd->idle_slice_timer.function = cfq_idle_slice_timer; | |
2093 | cfqd->idle_slice_timer.data = (unsigned long) cfqd; | |
2094 | ||
2095 | init_timer(&cfqd->idle_class_timer); | |
2096 | cfqd->idle_class_timer.function = cfq_idle_class_timer; | |
2097 | cfqd->idle_class_timer.data = (unsigned long) cfqd; | |
2098 | ||
65f27f38 | 2099 | INIT_WORK(&cfqd->unplug_work, cfq_kick_queue); |
22e2c507 | 2100 | |
1da177e4 | 2101 | cfqd->cfq_quantum = cfq_quantum; |
22e2c507 JA |
2102 | cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0]; |
2103 | cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1]; | |
1da177e4 LT |
2104 | cfqd->cfq_back_max = cfq_back_max; |
2105 | cfqd->cfq_back_penalty = cfq_back_penalty; | |
22e2c507 JA |
2106 | cfqd->cfq_slice[0] = cfq_slice_async; |
2107 | cfqd->cfq_slice[1] = cfq_slice_sync; | |
2108 | cfqd->cfq_slice_async_rq = cfq_slice_async_rq; | |
2109 | cfqd->cfq_slice_idle = cfq_slice_idle; | |
3b18152c | 2110 | |
bc1c1169 | 2111 | return cfqd; |
5e705374 | 2112 | out_free: |
1da177e4 | 2113 | kfree(cfqd); |
bc1c1169 | 2114 | return NULL; |
1da177e4 LT |
2115 | } |
2116 | ||
2117 | static void cfq_slab_kill(void) | |
2118 | { | |
1da177e4 LT |
2119 | if (cfq_pool) |
2120 | kmem_cache_destroy(cfq_pool); | |
2121 | if (cfq_ioc_pool) | |
2122 | kmem_cache_destroy(cfq_ioc_pool); | |
2123 | } | |
2124 | ||
2125 | static int __init cfq_slab_setup(void) | |
2126 | { | |
1da177e4 LT |
2127 | cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0, |
2128 | NULL, NULL); | |
2129 | if (!cfq_pool) | |
2130 | goto fail; | |
2131 | ||
2132 | cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool", | |
2133 | sizeof(struct cfq_io_context), 0, 0, NULL, NULL); | |
2134 | if (!cfq_ioc_pool) | |
2135 | goto fail; | |
2136 | ||
2137 | return 0; | |
2138 | fail: | |
2139 | cfq_slab_kill(); | |
2140 | return -ENOMEM; | |
2141 | } | |
2142 | ||
1da177e4 LT |
2143 | /* |
2144 | * sysfs parts below --> | |
2145 | */ | |
1da177e4 LT |
2146 | static ssize_t |
2147 | cfq_var_show(unsigned int var, char *page) | |
2148 | { | |
2149 | return sprintf(page, "%d\n", var); | |
2150 | } | |
2151 | ||
2152 | static ssize_t | |
2153 | cfq_var_store(unsigned int *var, const char *page, size_t count) | |
2154 | { | |
2155 | char *p = (char *) page; | |
2156 | ||
2157 | *var = simple_strtoul(p, &p, 10); | |
2158 | return count; | |
2159 | } | |
2160 | ||
1da177e4 | 2161 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ |
3d1ab40f | 2162 | static ssize_t __FUNC(elevator_t *e, char *page) \ |
1da177e4 | 2163 | { \ |
3d1ab40f | 2164 | struct cfq_data *cfqd = e->elevator_data; \ |
1da177e4 LT |
2165 | unsigned int __data = __VAR; \ |
2166 | if (__CONV) \ | |
2167 | __data = jiffies_to_msecs(__data); \ | |
2168 | return cfq_var_show(__data, (page)); \ | |
2169 | } | |
2170 | SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0); | |
22e2c507 JA |
2171 | SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1); |
2172 | SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1); | |
e572ec7e AV |
2173 | SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0); |
2174 | SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0); | |
22e2c507 JA |
2175 | SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1); |
2176 | SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1); | |
2177 | SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1); | |
2178 | SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0); | |
1da177e4 LT |
2179 | #undef SHOW_FUNCTION |
2180 | ||
2181 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ | |
3d1ab40f | 2182 | static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \ |
1da177e4 | 2183 | { \ |
3d1ab40f | 2184 | struct cfq_data *cfqd = e->elevator_data; \ |
1da177e4 LT |
2185 | unsigned int __data; \ |
2186 | int ret = cfq_var_store(&__data, (page), count); \ | |
2187 | if (__data < (MIN)) \ | |
2188 | __data = (MIN); \ | |
2189 | else if (__data > (MAX)) \ | |
2190 | __data = (MAX); \ | |
2191 | if (__CONV) \ | |
2192 | *(__PTR) = msecs_to_jiffies(__data); \ | |
2193 | else \ | |
2194 | *(__PTR) = __data; \ | |
2195 | return ret; \ | |
2196 | } | |
2197 | STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0); | |
22e2c507 JA |
2198 | STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1); |
2199 | STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1); | |
e572ec7e AV |
2200 | STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0); |
2201 | STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0); | |
22e2c507 JA |
2202 | STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1); |
2203 | STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1); | |
2204 | STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1); | |
2205 | STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0); | |
1da177e4 LT |
2206 | #undef STORE_FUNCTION |
2207 | ||
e572ec7e AV |
2208 | #define CFQ_ATTR(name) \ |
2209 | __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store) | |
2210 | ||
2211 | static struct elv_fs_entry cfq_attrs[] = { | |
2212 | CFQ_ATTR(quantum), | |
e572ec7e AV |
2213 | CFQ_ATTR(fifo_expire_sync), |
2214 | CFQ_ATTR(fifo_expire_async), | |
2215 | CFQ_ATTR(back_seek_max), | |
2216 | CFQ_ATTR(back_seek_penalty), | |
2217 | CFQ_ATTR(slice_sync), | |
2218 | CFQ_ATTR(slice_async), | |
2219 | CFQ_ATTR(slice_async_rq), | |
2220 | CFQ_ATTR(slice_idle), | |
e572ec7e | 2221 | __ATTR_NULL |
1da177e4 LT |
2222 | }; |
2223 | ||
1da177e4 LT |
2224 | static struct elevator_type iosched_cfq = { |
2225 | .ops = { | |
2226 | .elevator_merge_fn = cfq_merge, | |
2227 | .elevator_merged_fn = cfq_merged_request, | |
2228 | .elevator_merge_req_fn = cfq_merged_requests, | |
da775265 | 2229 | .elevator_allow_merge_fn = cfq_allow_merge, |
b4878f24 | 2230 | .elevator_dispatch_fn = cfq_dispatch_requests, |
1da177e4 | 2231 | .elevator_add_req_fn = cfq_insert_request, |
b4878f24 | 2232 | .elevator_activate_req_fn = cfq_activate_request, |
1da177e4 LT |
2233 | .elevator_deactivate_req_fn = cfq_deactivate_request, |
2234 | .elevator_queue_empty_fn = cfq_queue_empty, | |
2235 | .elevator_completed_req_fn = cfq_completed_request, | |
21183b07 JA |
2236 | .elevator_former_req_fn = elv_rb_former_request, |
2237 | .elevator_latter_req_fn = elv_rb_latter_request, | |
1da177e4 LT |
2238 | .elevator_set_req_fn = cfq_set_request, |
2239 | .elevator_put_req_fn = cfq_put_request, | |
2240 | .elevator_may_queue_fn = cfq_may_queue, | |
2241 | .elevator_init_fn = cfq_init_queue, | |
2242 | .elevator_exit_fn = cfq_exit_queue, | |
fc46379d | 2243 | .trim = cfq_free_io_context, |
1da177e4 | 2244 | }, |
3d1ab40f | 2245 | .elevator_attrs = cfq_attrs, |
1da177e4 LT |
2246 | .elevator_name = "cfq", |
2247 | .elevator_owner = THIS_MODULE, | |
2248 | }; | |
2249 | ||
2250 | static int __init cfq_init(void) | |
2251 | { | |
2252 | int ret; | |
2253 | ||
22e2c507 JA |
2254 | /* |
2255 | * could be 0 on HZ < 1000 setups | |
2256 | */ | |
2257 | if (!cfq_slice_async) | |
2258 | cfq_slice_async = 1; | |
2259 | if (!cfq_slice_idle) | |
2260 | cfq_slice_idle = 1; | |
2261 | ||
1da177e4 LT |
2262 | if (cfq_slab_setup()) |
2263 | return -ENOMEM; | |
2264 | ||
2265 | ret = elv_register(&iosched_cfq); | |
22e2c507 JA |
2266 | if (ret) |
2267 | cfq_slab_kill(); | |
1da177e4 | 2268 | |
1da177e4 LT |
2269 | return ret; |
2270 | } | |
2271 | ||
2272 | static void __exit cfq_exit(void) | |
2273 | { | |
6e9a4738 | 2274 | DECLARE_COMPLETION_ONSTACK(all_gone); |
1da177e4 | 2275 | elv_unregister(&iosched_cfq); |
334e94de | 2276 | ioc_gone = &all_gone; |
fba82272 OH |
2277 | /* ioc_gone's update must be visible before reading ioc_count */ |
2278 | smp_wmb(); | |
4050cf16 | 2279 | if (elv_ioc_count_read(ioc_count)) |
fba82272 | 2280 | wait_for_completion(ioc_gone); |
334e94de | 2281 | synchronize_rcu(); |
83521d3e | 2282 | cfq_slab_kill(); |
1da177e4 LT |
2283 | } |
2284 | ||
2285 | module_init(cfq_init); | |
2286 | module_exit(cfq_exit); | |
2287 | ||
2288 | MODULE_AUTHOR("Jens Axboe"); | |
2289 | MODULE_LICENSE("GPL"); | |
2290 | MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler"); |