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