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