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