Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/block/as-iosched.c | |
3 | * | |
4 | * Anticipatory & deadline i/o scheduler. | |
5 | * | |
6 | * Copyright (C) 2002 Jens Axboe <axboe@suse.de> | |
f5b3db00 | 7 | * Nick Piggin <nickpiggin@yahoo.com.au> |
1da177e4 LT |
8 | * |
9 | */ | |
10 | #include <linux/kernel.h> | |
11 | #include <linux/fs.h> | |
12 | #include <linux/blkdev.h> | |
13 | #include <linux/elevator.h> | |
14 | #include <linux/bio.h> | |
15 | #include <linux/config.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/compiler.h> | |
20 | #include <linux/hash.h> | |
21 | #include <linux/rbtree.h> | |
22 | #include <linux/interrupt.h> | |
23 | ||
24 | #define REQ_SYNC 1 | |
25 | #define REQ_ASYNC 0 | |
26 | ||
27 | /* | |
28 | * See Documentation/block/as-iosched.txt | |
29 | */ | |
30 | ||
31 | /* | |
32 | * max time before a read is submitted. | |
33 | */ | |
34 | #define default_read_expire (HZ / 8) | |
35 | ||
36 | /* | |
37 | * ditto for writes, these limits are not hard, even | |
38 | * if the disk is capable of satisfying them. | |
39 | */ | |
40 | #define default_write_expire (HZ / 4) | |
41 | ||
42 | /* | |
43 | * read_batch_expire describes how long we will allow a stream of reads to | |
44 | * persist before looking to see whether it is time to switch over to writes. | |
45 | */ | |
46 | #define default_read_batch_expire (HZ / 2) | |
47 | ||
48 | /* | |
49 | * write_batch_expire describes how long we want a stream of writes to run for. | |
50 | * This is not a hard limit, but a target we set for the auto-tuning thingy. | |
51 | * See, the problem is: we can send a lot of writes to disk cache / TCQ in | |
52 | * a short amount of time... | |
53 | */ | |
54 | #define default_write_batch_expire (HZ / 8) | |
55 | ||
56 | /* | |
57 | * max time we may wait to anticipate a read (default around 6ms) | |
58 | */ | |
59 | #define default_antic_expire ((HZ / 150) ? HZ / 150 : 1) | |
60 | ||
61 | /* | |
62 | * Keep track of up to 20ms thinktimes. We can go as big as we like here, | |
63 | * however huge values tend to interfere and not decay fast enough. A program | |
64 | * might be in a non-io phase of operation. Waiting on user input for example, | |
65 | * or doing a lengthy computation. A small penalty can be justified there, and | |
66 | * will still catch out those processes that constantly have large thinktimes. | |
67 | */ | |
68 | #define MAX_THINKTIME (HZ/50UL) | |
69 | ||
70 | /* Bits in as_io_context.state */ | |
71 | enum as_io_states { | |
f5b3db00 | 72 | AS_TASK_RUNNING=0, /* Process has not exited */ |
1da177e4 LT |
73 | AS_TASK_IOSTARTED, /* Process has started some IO */ |
74 | AS_TASK_IORUNNING, /* Process has completed some IO */ | |
75 | }; | |
76 | ||
77 | enum anticipation_status { | |
78 | ANTIC_OFF=0, /* Not anticipating (normal operation) */ | |
79 | ANTIC_WAIT_REQ, /* The last read has not yet completed */ | |
80 | ANTIC_WAIT_NEXT, /* Currently anticipating a request vs | |
81 | last read (which has completed) */ | |
82 | ANTIC_FINISHED, /* Anticipating but have found a candidate | |
83 | * or timed out */ | |
84 | }; | |
85 | ||
86 | struct as_data { | |
87 | /* | |
88 | * run time data | |
89 | */ | |
90 | ||
91 | struct request_queue *q; /* the "owner" queue */ | |
92 | ||
93 | /* | |
94 | * requests (as_rq s) are present on both sort_list and fifo_list | |
95 | */ | |
96 | struct rb_root sort_list[2]; | |
97 | struct list_head fifo_list[2]; | |
98 | ||
99 | struct as_rq *next_arq[2]; /* next in sort order */ | |
100 | sector_t last_sector[2]; /* last REQ_SYNC & REQ_ASYNC sectors */ | |
1da177e4 LT |
101 | struct list_head *hash; /* request hash */ |
102 | ||
103 | unsigned long exit_prob; /* probability a task will exit while | |
104 | being waited on */ | |
f5b3db00 NP |
105 | unsigned long exit_no_coop; /* probablility an exited task will |
106 | not be part of a later cooperating | |
107 | request */ | |
1da177e4 LT |
108 | unsigned long new_ttime_total; /* mean thinktime on new proc */ |
109 | unsigned long new_ttime_mean; | |
110 | u64 new_seek_total; /* mean seek on new proc */ | |
111 | sector_t new_seek_mean; | |
112 | ||
113 | unsigned long current_batch_expires; | |
114 | unsigned long last_check_fifo[2]; | |
115 | int changed_batch; /* 1: waiting for old batch to end */ | |
116 | int new_batch; /* 1: waiting on first read complete */ | |
117 | int batch_data_dir; /* current batch REQ_SYNC / REQ_ASYNC */ | |
118 | int write_batch_count; /* max # of reqs in a write batch */ | |
119 | int current_write_count; /* how many requests left this batch */ | |
120 | int write_batch_idled; /* has the write batch gone idle? */ | |
121 | mempool_t *arq_pool; | |
122 | ||
123 | enum anticipation_status antic_status; | |
124 | unsigned long antic_start; /* jiffies: when it started */ | |
125 | struct timer_list antic_timer; /* anticipatory scheduling timer */ | |
126 | struct work_struct antic_work; /* Deferred unplugging */ | |
127 | struct io_context *io_context; /* Identify the expected process */ | |
128 | int ioc_finished; /* IO associated with io_context is finished */ | |
129 | int nr_dispatched; | |
130 | ||
131 | /* | |
132 | * settings that change how the i/o scheduler behaves | |
133 | */ | |
134 | unsigned long fifo_expire[2]; | |
135 | unsigned long batch_expire[2]; | |
136 | unsigned long antic_expire; | |
137 | }; | |
138 | ||
139 | #define list_entry_fifo(ptr) list_entry((ptr), struct as_rq, fifo) | |
140 | ||
141 | /* | |
142 | * per-request data. | |
143 | */ | |
144 | enum arq_state { | |
145 | AS_RQ_NEW=0, /* New - not referenced and not on any lists */ | |
146 | AS_RQ_QUEUED, /* In the request queue. It belongs to the | |
147 | scheduler */ | |
148 | AS_RQ_DISPATCHED, /* On the dispatch list. It belongs to the | |
149 | driver now */ | |
150 | AS_RQ_PRESCHED, /* Debug poisoning for requests being used */ | |
151 | AS_RQ_REMOVED, | |
152 | AS_RQ_MERGED, | |
153 | AS_RQ_POSTSCHED, /* when they shouldn't be */ | |
154 | }; | |
155 | ||
156 | struct as_rq { | |
157 | /* | |
158 | * rbtree index, key is the starting offset | |
159 | */ | |
160 | struct rb_node rb_node; | |
161 | sector_t rb_key; | |
162 | ||
163 | struct request *request; | |
164 | ||
165 | struct io_context *io_context; /* The submitting task */ | |
166 | ||
167 | /* | |
168 | * request hash, key is the ending offset (for back merge lookup) | |
169 | */ | |
170 | struct list_head hash; | |
171 | unsigned int on_hash; | |
172 | ||
173 | /* | |
174 | * expire fifo | |
175 | */ | |
176 | struct list_head fifo; | |
177 | unsigned long expires; | |
178 | ||
179 | unsigned int is_sync; | |
180 | enum arq_state state; | |
181 | }; | |
182 | ||
183 | #define RQ_DATA(rq) ((struct as_rq *) (rq)->elevator_private) | |
184 | ||
185 | static kmem_cache_t *arq_pool; | |
186 | ||
187 | /* | |
188 | * IO Context helper functions | |
189 | */ | |
190 | ||
191 | /* Called to deallocate the as_io_context */ | |
192 | static void free_as_io_context(struct as_io_context *aic) | |
193 | { | |
194 | kfree(aic); | |
195 | } | |
196 | ||
197 | /* Called when the task exits */ | |
198 | static void exit_as_io_context(struct as_io_context *aic) | |
199 | { | |
200 | WARN_ON(!test_bit(AS_TASK_RUNNING, &aic->state)); | |
201 | clear_bit(AS_TASK_RUNNING, &aic->state); | |
202 | } | |
203 | ||
204 | static struct as_io_context *alloc_as_io_context(void) | |
205 | { | |
206 | struct as_io_context *ret; | |
207 | ||
208 | ret = kmalloc(sizeof(*ret), GFP_ATOMIC); | |
209 | if (ret) { | |
210 | ret->dtor = free_as_io_context; | |
211 | ret->exit = exit_as_io_context; | |
212 | ret->state = 1 << AS_TASK_RUNNING; | |
213 | atomic_set(&ret->nr_queued, 0); | |
214 | atomic_set(&ret->nr_dispatched, 0); | |
215 | spin_lock_init(&ret->lock); | |
216 | ret->ttime_total = 0; | |
217 | ret->ttime_samples = 0; | |
218 | ret->ttime_mean = 0; | |
219 | ret->seek_total = 0; | |
220 | ret->seek_samples = 0; | |
221 | ret->seek_mean = 0; | |
222 | } | |
223 | ||
224 | return ret; | |
225 | } | |
226 | ||
227 | /* | |
228 | * If the current task has no AS IO context then create one and initialise it. | |
229 | * Then take a ref on the task's io context and return it. | |
230 | */ | |
231 | static struct io_context *as_get_io_context(void) | |
232 | { | |
233 | struct io_context *ioc = get_io_context(GFP_ATOMIC); | |
234 | if (ioc && !ioc->aic) { | |
235 | ioc->aic = alloc_as_io_context(); | |
236 | if (!ioc->aic) { | |
237 | put_io_context(ioc); | |
238 | ioc = NULL; | |
239 | } | |
240 | } | |
241 | return ioc; | |
242 | } | |
243 | ||
b4878f24 JA |
244 | static void as_put_io_context(struct as_rq *arq) |
245 | { | |
246 | struct as_io_context *aic; | |
247 | ||
248 | if (unlikely(!arq->io_context)) | |
249 | return; | |
250 | ||
251 | aic = arq->io_context->aic; | |
252 | ||
253 | if (arq->is_sync == REQ_SYNC && aic) { | |
254 | spin_lock(&aic->lock); | |
255 | set_bit(AS_TASK_IORUNNING, &aic->state); | |
256 | aic->last_end_request = jiffies; | |
257 | spin_unlock(&aic->lock); | |
258 | } | |
259 | ||
260 | put_io_context(arq->io_context); | |
261 | } | |
262 | ||
1da177e4 LT |
263 | /* |
264 | * the back merge hash support functions | |
265 | */ | |
266 | static const int as_hash_shift = 6; | |
267 | #define AS_HASH_BLOCK(sec) ((sec) >> 3) | |
268 | #define AS_HASH_FN(sec) (hash_long(AS_HASH_BLOCK((sec)), as_hash_shift)) | |
269 | #define AS_HASH_ENTRIES (1 << as_hash_shift) | |
270 | #define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors) | |
271 | #define list_entry_hash(ptr) list_entry((ptr), struct as_rq, hash) | |
272 | ||
273 | static inline void __as_del_arq_hash(struct as_rq *arq) | |
274 | { | |
275 | arq->on_hash = 0; | |
276 | list_del_init(&arq->hash); | |
277 | } | |
278 | ||
279 | static inline void as_del_arq_hash(struct as_rq *arq) | |
280 | { | |
281 | if (arq->on_hash) | |
282 | __as_del_arq_hash(arq); | |
283 | } | |
284 | ||
1da177e4 LT |
285 | static void as_add_arq_hash(struct as_data *ad, struct as_rq *arq) |
286 | { | |
287 | struct request *rq = arq->request; | |
288 | ||
289 | BUG_ON(arq->on_hash); | |
290 | ||
291 | arq->on_hash = 1; | |
292 | list_add(&arq->hash, &ad->hash[AS_HASH_FN(rq_hash_key(rq))]); | |
293 | } | |
294 | ||
295 | /* | |
296 | * move hot entry to front of chain | |
297 | */ | |
298 | static inline void as_hot_arq_hash(struct as_data *ad, struct as_rq *arq) | |
299 | { | |
300 | struct request *rq = arq->request; | |
301 | struct list_head *head = &ad->hash[AS_HASH_FN(rq_hash_key(rq))]; | |
302 | ||
303 | if (!arq->on_hash) { | |
304 | WARN_ON(1); | |
305 | return; | |
306 | } | |
307 | ||
308 | if (arq->hash.prev != head) { | |
309 | list_del(&arq->hash); | |
310 | list_add(&arq->hash, head); | |
311 | } | |
312 | } | |
313 | ||
314 | static struct request *as_find_arq_hash(struct as_data *ad, sector_t offset) | |
315 | { | |
316 | struct list_head *hash_list = &ad->hash[AS_HASH_FN(offset)]; | |
317 | struct list_head *entry, *next = hash_list->next; | |
318 | ||
319 | while ((entry = next) != hash_list) { | |
320 | struct as_rq *arq = list_entry_hash(entry); | |
321 | struct request *__rq = arq->request; | |
322 | ||
323 | next = entry->next; | |
324 | ||
325 | BUG_ON(!arq->on_hash); | |
326 | ||
327 | if (!rq_mergeable(__rq)) { | |
98b11471 | 328 | as_del_arq_hash(arq); |
1da177e4 LT |
329 | continue; |
330 | } | |
331 | ||
332 | if (rq_hash_key(__rq) == offset) | |
333 | return __rq; | |
334 | } | |
335 | ||
336 | return NULL; | |
337 | } | |
338 | ||
339 | /* | |
340 | * rb tree support functions | |
341 | */ | |
342 | #define RB_NONE (2) | |
343 | #define RB_EMPTY(root) ((root)->rb_node == NULL) | |
344 | #define ON_RB(node) ((node)->rb_color != RB_NONE) | |
345 | #define RB_CLEAR(node) ((node)->rb_color = RB_NONE) | |
346 | #define rb_entry_arq(node) rb_entry((node), struct as_rq, rb_node) | |
347 | #define ARQ_RB_ROOT(ad, arq) (&(ad)->sort_list[(arq)->is_sync]) | |
348 | #define rq_rb_key(rq) (rq)->sector | |
349 | ||
350 | /* | |
351 | * as_find_first_arq finds the first (lowest sector numbered) request | |
352 | * for the specified data_dir. Used to sweep back to the start of the disk | |
353 | * (1-way elevator) after we process the last (highest sector) request. | |
354 | */ | |
355 | static struct as_rq *as_find_first_arq(struct as_data *ad, int data_dir) | |
356 | { | |
357 | struct rb_node *n = ad->sort_list[data_dir].rb_node; | |
358 | ||
359 | if (n == NULL) | |
360 | return NULL; | |
361 | ||
362 | for (;;) { | |
363 | if (n->rb_left == NULL) | |
364 | return rb_entry_arq(n); | |
365 | ||
366 | n = n->rb_left; | |
367 | } | |
368 | } | |
369 | ||
370 | /* | |
371 | * Add the request to the rb tree if it is unique. If there is an alias (an | |
372 | * existing request against the same sector), which can happen when using | |
373 | * direct IO, then return the alias. | |
374 | */ | |
375 | static struct as_rq *as_add_arq_rb(struct as_data *ad, struct as_rq *arq) | |
376 | { | |
377 | struct rb_node **p = &ARQ_RB_ROOT(ad, arq)->rb_node; | |
378 | struct rb_node *parent = NULL; | |
379 | struct as_rq *__arq; | |
380 | struct request *rq = arq->request; | |
381 | ||
382 | arq->rb_key = rq_rb_key(rq); | |
383 | ||
384 | while (*p) { | |
385 | parent = *p; | |
386 | __arq = rb_entry_arq(parent); | |
387 | ||
388 | if (arq->rb_key < __arq->rb_key) | |
389 | p = &(*p)->rb_left; | |
390 | else if (arq->rb_key > __arq->rb_key) | |
391 | p = &(*p)->rb_right; | |
392 | else | |
393 | return __arq; | |
394 | } | |
395 | ||
396 | rb_link_node(&arq->rb_node, parent, p); | |
397 | rb_insert_color(&arq->rb_node, ARQ_RB_ROOT(ad, arq)); | |
398 | ||
399 | return NULL; | |
400 | } | |
401 | ||
402 | static inline void as_del_arq_rb(struct as_data *ad, struct as_rq *arq) | |
403 | { | |
404 | if (!ON_RB(&arq->rb_node)) { | |
405 | WARN_ON(1); | |
406 | return; | |
407 | } | |
408 | ||
409 | rb_erase(&arq->rb_node, ARQ_RB_ROOT(ad, arq)); | |
410 | RB_CLEAR(&arq->rb_node); | |
411 | } | |
412 | ||
413 | static struct request * | |
414 | as_find_arq_rb(struct as_data *ad, sector_t sector, int data_dir) | |
415 | { | |
416 | struct rb_node *n = ad->sort_list[data_dir].rb_node; | |
417 | struct as_rq *arq; | |
418 | ||
419 | while (n) { | |
420 | arq = rb_entry_arq(n); | |
421 | ||
422 | if (sector < arq->rb_key) | |
423 | n = n->rb_left; | |
424 | else if (sector > arq->rb_key) | |
425 | n = n->rb_right; | |
426 | else | |
427 | return arq->request; | |
428 | } | |
429 | ||
430 | return NULL; | |
431 | } | |
432 | ||
433 | /* | |
434 | * IO Scheduler proper | |
435 | */ | |
436 | ||
437 | #define MAXBACK (1024 * 1024) /* | |
438 | * Maximum distance the disk will go backward | |
439 | * for a request. | |
440 | */ | |
441 | ||
442 | #define BACK_PENALTY 2 | |
443 | ||
444 | /* | |
445 | * as_choose_req selects the preferred one of two requests of the same data_dir | |
446 | * ignoring time - eg. timeouts, which is the job of as_dispatch_request | |
447 | */ | |
448 | static struct as_rq * | |
449 | as_choose_req(struct as_data *ad, struct as_rq *arq1, struct as_rq *arq2) | |
450 | { | |
451 | int data_dir; | |
452 | sector_t last, s1, s2, d1, d2; | |
453 | int r1_wrap=0, r2_wrap=0; /* requests are behind the disk head */ | |
454 | const sector_t maxback = MAXBACK; | |
455 | ||
456 | if (arq1 == NULL || arq1 == arq2) | |
457 | return arq2; | |
458 | if (arq2 == NULL) | |
459 | return arq1; | |
460 | ||
461 | data_dir = arq1->is_sync; | |
462 | ||
463 | last = ad->last_sector[data_dir]; | |
464 | s1 = arq1->request->sector; | |
465 | s2 = arq2->request->sector; | |
466 | ||
467 | BUG_ON(data_dir != arq2->is_sync); | |
468 | ||
469 | /* | |
470 | * Strict one way elevator _except_ in the case where we allow | |
471 | * short backward seeks which are biased as twice the cost of a | |
472 | * similar forward seek. | |
473 | */ | |
474 | if (s1 >= last) | |
475 | d1 = s1 - last; | |
476 | else if (s1+maxback >= last) | |
477 | d1 = (last - s1)*BACK_PENALTY; | |
478 | else { | |
479 | r1_wrap = 1; | |
480 | d1 = 0; /* shut up, gcc */ | |
481 | } | |
482 | ||
483 | if (s2 >= last) | |
484 | d2 = s2 - last; | |
485 | else if (s2+maxback >= last) | |
486 | d2 = (last - s2)*BACK_PENALTY; | |
487 | else { | |
488 | r2_wrap = 1; | |
489 | d2 = 0; | |
490 | } | |
491 | ||
492 | /* Found required data */ | |
493 | if (!r1_wrap && r2_wrap) | |
494 | return arq1; | |
495 | else if (!r2_wrap && r1_wrap) | |
496 | return arq2; | |
497 | else if (r1_wrap && r2_wrap) { | |
498 | /* both behind the head */ | |
499 | if (s1 <= s2) | |
500 | return arq1; | |
501 | else | |
502 | return arq2; | |
503 | } | |
504 | ||
505 | /* Both requests in front of the head */ | |
506 | if (d1 < d2) | |
507 | return arq1; | |
508 | else if (d2 < d1) | |
509 | return arq2; | |
510 | else { | |
511 | if (s1 >= s2) | |
512 | return arq1; | |
513 | else | |
514 | return arq2; | |
515 | } | |
516 | } | |
517 | ||
518 | /* | |
519 | * as_find_next_arq finds the next request after @prev in elevator order. | |
520 | * this with as_choose_req form the basis for how the scheduler chooses | |
521 | * what request to process next. Anticipation works on top of this. | |
522 | */ | |
523 | static struct as_rq *as_find_next_arq(struct as_data *ad, struct as_rq *last) | |
524 | { | |
525 | const int data_dir = last->is_sync; | |
526 | struct as_rq *ret; | |
527 | struct rb_node *rbnext = rb_next(&last->rb_node); | |
528 | struct rb_node *rbprev = rb_prev(&last->rb_node); | |
529 | struct as_rq *arq_next, *arq_prev; | |
530 | ||
531 | BUG_ON(!ON_RB(&last->rb_node)); | |
532 | ||
533 | if (rbprev) | |
534 | arq_prev = rb_entry_arq(rbprev); | |
535 | else | |
536 | arq_prev = NULL; | |
537 | ||
538 | if (rbnext) | |
539 | arq_next = rb_entry_arq(rbnext); | |
540 | else { | |
541 | arq_next = as_find_first_arq(ad, data_dir); | |
542 | if (arq_next == last) | |
543 | arq_next = NULL; | |
544 | } | |
545 | ||
546 | ret = as_choose_req(ad, arq_next, arq_prev); | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | /* | |
552 | * anticipatory scheduling functions follow | |
553 | */ | |
554 | ||
555 | /* | |
556 | * as_antic_expired tells us when we have anticipated too long. | |
557 | * The funny "absolute difference" math on the elapsed time is to handle | |
558 | * jiffy wraps, and disks which have been idle for 0x80000000 jiffies. | |
559 | */ | |
560 | static int as_antic_expired(struct as_data *ad) | |
561 | { | |
562 | long delta_jif; | |
563 | ||
564 | delta_jif = jiffies - ad->antic_start; | |
565 | if (unlikely(delta_jif < 0)) | |
566 | delta_jif = -delta_jif; | |
567 | if (delta_jif < ad->antic_expire) | |
568 | return 0; | |
569 | ||
570 | return 1; | |
571 | } | |
572 | ||
573 | /* | |
574 | * as_antic_waitnext starts anticipating that a nice request will soon be | |
575 | * submitted. See also as_antic_waitreq | |
576 | */ | |
577 | static void as_antic_waitnext(struct as_data *ad) | |
578 | { | |
579 | unsigned long timeout; | |
580 | ||
581 | BUG_ON(ad->antic_status != ANTIC_OFF | |
582 | && ad->antic_status != ANTIC_WAIT_REQ); | |
583 | ||
584 | timeout = ad->antic_start + ad->antic_expire; | |
585 | ||
586 | mod_timer(&ad->antic_timer, timeout); | |
587 | ||
588 | ad->antic_status = ANTIC_WAIT_NEXT; | |
589 | } | |
590 | ||
591 | /* | |
592 | * as_antic_waitreq starts anticipating. We don't start timing the anticipation | |
593 | * until the request that we're anticipating on has finished. This means we | |
594 | * are timing from when the candidate process wakes up hopefully. | |
595 | */ | |
596 | static void as_antic_waitreq(struct as_data *ad) | |
597 | { | |
598 | BUG_ON(ad->antic_status == ANTIC_FINISHED); | |
599 | if (ad->antic_status == ANTIC_OFF) { | |
600 | if (!ad->io_context || ad->ioc_finished) | |
601 | as_antic_waitnext(ad); | |
602 | else | |
603 | ad->antic_status = ANTIC_WAIT_REQ; | |
604 | } | |
605 | } | |
606 | ||
607 | /* | |
608 | * This is called directly by the functions in this file to stop anticipation. | |
609 | * We kill the timer and schedule a call to the request_fn asap. | |
610 | */ | |
611 | static void as_antic_stop(struct as_data *ad) | |
612 | { | |
613 | int status = ad->antic_status; | |
614 | ||
615 | if (status == ANTIC_WAIT_REQ || status == ANTIC_WAIT_NEXT) { | |
616 | if (status == ANTIC_WAIT_NEXT) | |
617 | del_timer(&ad->antic_timer); | |
618 | ad->antic_status = ANTIC_FINISHED; | |
619 | /* see as_work_handler */ | |
620 | kblockd_schedule_work(&ad->antic_work); | |
621 | } | |
622 | } | |
623 | ||
624 | /* | |
625 | * as_antic_timeout is the timer function set by as_antic_waitnext. | |
626 | */ | |
627 | static void as_antic_timeout(unsigned long data) | |
628 | { | |
629 | struct request_queue *q = (struct request_queue *)data; | |
630 | struct as_data *ad = q->elevator->elevator_data; | |
631 | unsigned long flags; | |
632 | ||
633 | spin_lock_irqsave(q->queue_lock, flags); | |
634 | if (ad->antic_status == ANTIC_WAIT_REQ | |
635 | || ad->antic_status == ANTIC_WAIT_NEXT) { | |
636 | struct as_io_context *aic = ad->io_context->aic; | |
637 | ||
638 | ad->antic_status = ANTIC_FINISHED; | |
639 | kblockd_schedule_work(&ad->antic_work); | |
640 | ||
641 | if (aic->ttime_samples == 0) { | |
f5b3db00 | 642 | /* process anticipated on has exited or timed out*/ |
1da177e4 LT |
643 | ad->exit_prob = (7*ad->exit_prob + 256)/8; |
644 | } | |
f5b3db00 NP |
645 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
646 | /* process not "saved" by a cooperating request */ | |
647 | ad->exit_no_coop = (7*ad->exit_no_coop + 256)/8; | |
648 | } | |
1da177e4 LT |
649 | } |
650 | spin_unlock_irqrestore(q->queue_lock, flags); | |
651 | } | |
652 | ||
f5b3db00 NP |
653 | static void as_update_thinktime(struct as_data *ad, struct as_io_context *aic, |
654 | unsigned long ttime) | |
655 | { | |
656 | /* fixed point: 1.0 == 1<<8 */ | |
657 | if (aic->ttime_samples == 0) { | |
658 | ad->new_ttime_total = (7*ad->new_ttime_total + 256*ttime) / 8; | |
659 | ad->new_ttime_mean = ad->new_ttime_total / 256; | |
660 | ||
661 | ad->exit_prob = (7*ad->exit_prob)/8; | |
662 | } | |
663 | aic->ttime_samples = (7*aic->ttime_samples + 256) / 8; | |
664 | aic->ttime_total = (7*aic->ttime_total + 256*ttime) / 8; | |
665 | aic->ttime_mean = (aic->ttime_total + 128) / aic->ttime_samples; | |
666 | } | |
667 | ||
668 | static void as_update_seekdist(struct as_data *ad, struct as_io_context *aic, | |
669 | sector_t sdist) | |
670 | { | |
671 | u64 total; | |
672 | ||
673 | if (aic->seek_samples == 0) { | |
674 | ad->new_seek_total = (7*ad->new_seek_total + 256*(u64)sdist)/8; | |
675 | ad->new_seek_mean = ad->new_seek_total / 256; | |
676 | } | |
677 | ||
678 | /* | |
679 | * Don't allow the seek distance to get too large from the | |
680 | * odd fragment, pagein, etc | |
681 | */ | |
682 | if (aic->seek_samples <= 60) /* second&third seek */ | |
683 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*1024); | |
684 | else | |
685 | sdist = min(sdist, (aic->seek_mean * 4) + 2*1024*64); | |
686 | ||
687 | aic->seek_samples = (7*aic->seek_samples + 256) / 8; | |
688 | aic->seek_total = (7*aic->seek_total + (u64)256*sdist) / 8; | |
689 | total = aic->seek_total + (aic->seek_samples/2); | |
690 | do_div(total, aic->seek_samples); | |
691 | aic->seek_mean = (sector_t)total; | |
692 | } | |
693 | ||
694 | /* | |
695 | * as_update_iohist keeps a decaying histogram of IO thinktimes, and | |
696 | * updates @aic->ttime_mean based on that. It is called when a new | |
697 | * request is queued. | |
698 | */ | |
699 | static void as_update_iohist(struct as_data *ad, struct as_io_context *aic, | |
700 | struct request *rq) | |
701 | { | |
702 | struct as_rq *arq = RQ_DATA(rq); | |
703 | int data_dir = arq->is_sync; | |
704 | unsigned long thinktime = 0; | |
705 | sector_t seek_dist; | |
706 | ||
707 | if (aic == NULL) | |
708 | return; | |
709 | ||
710 | if (data_dir == REQ_SYNC) { | |
711 | unsigned long in_flight = atomic_read(&aic->nr_queued) | |
712 | + atomic_read(&aic->nr_dispatched); | |
713 | spin_lock(&aic->lock); | |
714 | if (test_bit(AS_TASK_IORUNNING, &aic->state) || | |
715 | test_bit(AS_TASK_IOSTARTED, &aic->state)) { | |
716 | /* Calculate read -> read thinktime */ | |
717 | if (test_bit(AS_TASK_IORUNNING, &aic->state) | |
718 | && in_flight == 0) { | |
719 | thinktime = jiffies - aic->last_end_request; | |
720 | thinktime = min(thinktime, MAX_THINKTIME-1); | |
721 | } | |
722 | as_update_thinktime(ad, aic, thinktime); | |
723 | ||
724 | /* Calculate read -> read seek distance */ | |
725 | if (aic->last_request_pos < rq->sector) | |
726 | seek_dist = rq->sector - aic->last_request_pos; | |
727 | else | |
728 | seek_dist = aic->last_request_pos - rq->sector; | |
729 | as_update_seekdist(ad, aic, seek_dist); | |
730 | } | |
731 | aic->last_request_pos = rq->sector + rq->nr_sectors; | |
732 | set_bit(AS_TASK_IOSTARTED, &aic->state); | |
733 | spin_unlock(&aic->lock); | |
734 | } | |
735 | } | |
736 | ||
1da177e4 LT |
737 | /* |
738 | * as_close_req decides if one request is considered "close" to the | |
739 | * previous one issued. | |
740 | */ | |
f5b3db00 NP |
741 | static int as_close_req(struct as_data *ad, struct as_io_context *aic, |
742 | struct as_rq *arq) | |
1da177e4 LT |
743 | { |
744 | unsigned long delay; /* milliseconds */ | |
745 | sector_t last = ad->last_sector[ad->batch_data_dir]; | |
746 | sector_t next = arq->request->sector; | |
747 | sector_t delta; /* acceptable close offset (in sectors) */ | |
f5b3db00 | 748 | sector_t s; |
1da177e4 LT |
749 | |
750 | if (ad->antic_status == ANTIC_OFF || !ad->ioc_finished) | |
751 | delay = 0; | |
752 | else | |
753 | delay = ((jiffies - ad->antic_start) * 1000) / HZ; | |
754 | ||
f5b3db00 NP |
755 | if (delay == 0) |
756 | delta = 8192; | |
1da177e4 | 757 | else if (delay <= 20 && delay <= ad->antic_expire) |
f5b3db00 | 758 | delta = 8192 << delay; |
1da177e4 LT |
759 | else |
760 | return 1; | |
761 | ||
f5b3db00 NP |
762 | if ((last <= next + (delta>>1)) && (next <= last + delta)) |
763 | return 1; | |
764 | ||
765 | if (last < next) | |
766 | s = next - last; | |
767 | else | |
768 | s = last - next; | |
769 | ||
770 | if (aic->seek_samples == 0) { | |
771 | /* | |
772 | * Process has just started IO. Use past statistics to | |
773 | * gauge success possibility | |
774 | */ | |
775 | if (ad->new_seek_mean > s) { | |
776 | /* this request is better than what we're expecting */ | |
777 | return 1; | |
778 | } | |
779 | ||
780 | } else { | |
781 | if (aic->seek_mean > s) { | |
782 | /* this request is better than what we're expecting */ | |
783 | return 1; | |
784 | } | |
785 | } | |
786 | ||
787 | return 0; | |
1da177e4 LT |
788 | } |
789 | ||
790 | /* | |
791 | * as_can_break_anticipation returns true if we have been anticipating this | |
792 | * request. | |
793 | * | |
794 | * It also returns true if the process against which we are anticipating | |
795 | * submits a write - that's presumably an fsync, O_SYNC write, etc. We want to | |
796 | * dispatch it ASAP, because we know that application will not be submitting | |
797 | * any new reads. | |
798 | * | |
f5b3db00 | 799 | * If the task which has submitted the request has exited, break anticipation. |
1da177e4 LT |
800 | * |
801 | * If this task has queued some other IO, do not enter enticipation. | |
802 | */ | |
803 | static int as_can_break_anticipation(struct as_data *ad, struct as_rq *arq) | |
804 | { | |
805 | struct io_context *ioc; | |
806 | struct as_io_context *aic; | |
1da177e4 LT |
807 | |
808 | ioc = ad->io_context; | |
809 | BUG_ON(!ioc); | |
810 | ||
811 | if (arq && ioc == arq->io_context) { | |
812 | /* request from same process */ | |
813 | return 1; | |
814 | } | |
815 | ||
816 | if (ad->ioc_finished && as_antic_expired(ad)) { | |
817 | /* | |
818 | * In this situation status should really be FINISHED, | |
819 | * however the timer hasn't had the chance to run yet. | |
820 | */ | |
821 | return 1; | |
822 | } | |
823 | ||
824 | aic = ioc->aic; | |
825 | if (!aic) | |
826 | return 0; | |
827 | ||
1da177e4 LT |
828 | if (atomic_read(&aic->nr_queued) > 0) { |
829 | /* process has more requests queued */ | |
830 | return 1; | |
831 | } | |
832 | ||
833 | if (atomic_read(&aic->nr_dispatched) > 0) { | |
834 | /* process has more requests dispatched */ | |
835 | return 1; | |
836 | } | |
837 | ||
f5b3db00 | 838 | if (arq && arq->is_sync == REQ_SYNC && as_close_req(ad, aic, arq)) { |
1da177e4 LT |
839 | /* |
840 | * Found a close request that is not one of ours. | |
841 | * | |
f5b3db00 NP |
842 | * This makes close requests from another process update |
843 | * our IO history. Is generally useful when there are | |
1da177e4 LT |
844 | * two or more cooperating processes working in the same |
845 | * area. | |
846 | */ | |
f5b3db00 NP |
847 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
848 | if (aic->ttime_samples == 0) | |
849 | ad->exit_prob = (7*ad->exit_prob + 256)/8; | |
850 | ||
851 | ad->exit_no_coop = (7*ad->exit_no_coop)/8; | |
852 | } | |
853 | ||
854 | as_update_iohist(ad, aic, arq->request); | |
1da177e4 LT |
855 | return 1; |
856 | } | |
857 | ||
f5b3db00 NP |
858 | if (!test_bit(AS_TASK_RUNNING, &aic->state)) { |
859 | /* process anticipated on has exited */ | |
860 | if (aic->ttime_samples == 0) | |
861 | ad->exit_prob = (7*ad->exit_prob + 256)/8; | |
862 | ||
863 | if (ad->exit_no_coop > 128) | |
864 | return 1; | |
865 | } | |
1da177e4 LT |
866 | |
867 | if (aic->ttime_samples == 0) { | |
868 | if (ad->new_ttime_mean > ad->antic_expire) | |
869 | return 1; | |
f5b3db00 | 870 | if (ad->exit_prob * ad->exit_no_coop > 128*256) |
1da177e4 LT |
871 | return 1; |
872 | } else if (aic->ttime_mean > ad->antic_expire) { | |
873 | /* the process thinks too much between requests */ | |
874 | return 1; | |
875 | } | |
876 | ||
1da177e4 LT |
877 | return 0; |
878 | } | |
879 | ||
880 | /* | |
881 | * as_can_anticipate indicates weather we should either run arq | |
882 | * or keep anticipating a better request. | |
883 | */ | |
884 | static int as_can_anticipate(struct as_data *ad, struct as_rq *arq) | |
885 | { | |
886 | if (!ad->io_context) | |
887 | /* | |
888 | * Last request submitted was a write | |
889 | */ | |
890 | return 0; | |
891 | ||
892 | if (ad->antic_status == ANTIC_FINISHED) | |
893 | /* | |
894 | * Don't restart if we have just finished. Run the next request | |
895 | */ | |
896 | return 0; | |
897 | ||
898 | if (as_can_break_anticipation(ad, arq)) | |
899 | /* | |
900 | * This request is a good candidate. Don't keep anticipating, | |
901 | * run it. | |
902 | */ | |
903 | return 0; | |
904 | ||
905 | /* | |
906 | * OK from here, we haven't finished, and don't have a decent request! | |
907 | * Status is either ANTIC_OFF so start waiting, | |
908 | * ANTIC_WAIT_REQ so continue waiting for request to finish | |
909 | * or ANTIC_WAIT_NEXT so continue waiting for an acceptable request. | |
1da177e4 LT |
910 | */ |
911 | ||
912 | return 1; | |
913 | } | |
914 | ||
1da177e4 LT |
915 | /* |
916 | * as_update_arq must be called whenever a request (arq) is added to | |
917 | * the sort_list. This function keeps caches up to date, and checks if the | |
918 | * request might be one we are "anticipating" | |
919 | */ | |
920 | static void as_update_arq(struct as_data *ad, struct as_rq *arq) | |
921 | { | |
922 | const int data_dir = arq->is_sync; | |
923 | ||
924 | /* keep the next_arq cache up to date */ | |
925 | ad->next_arq[data_dir] = as_choose_req(ad, arq, ad->next_arq[data_dir]); | |
926 | ||
927 | /* | |
928 | * have we been anticipating this request? | |
929 | * or does it come from the same process as the one we are anticipating | |
930 | * for? | |
931 | */ | |
932 | if (ad->antic_status == ANTIC_WAIT_REQ | |
933 | || ad->antic_status == ANTIC_WAIT_NEXT) { | |
934 | if (as_can_break_anticipation(ad, arq)) | |
935 | as_antic_stop(ad); | |
936 | } | |
937 | } | |
938 | ||
939 | /* | |
940 | * Gathers timings and resizes the write batch automatically | |
941 | */ | |
942 | static void update_write_batch(struct as_data *ad) | |
943 | { | |
944 | unsigned long batch = ad->batch_expire[REQ_ASYNC]; | |
945 | long write_time; | |
946 | ||
947 | write_time = (jiffies - ad->current_batch_expires) + batch; | |
948 | if (write_time < 0) | |
949 | write_time = 0; | |
950 | ||
951 | if (write_time > batch && !ad->write_batch_idled) { | |
952 | if (write_time > batch * 3) | |
953 | ad->write_batch_count /= 2; | |
954 | else | |
955 | ad->write_batch_count--; | |
956 | } else if (write_time < batch && ad->current_write_count == 0) { | |
957 | if (batch > write_time * 3) | |
958 | ad->write_batch_count *= 2; | |
959 | else | |
960 | ad->write_batch_count++; | |
961 | } | |
962 | ||
963 | if (ad->write_batch_count < 1) | |
964 | ad->write_batch_count = 1; | |
965 | } | |
966 | ||
967 | /* | |
968 | * as_completed_request is to be called when a request has completed and | |
969 | * returned something to the requesting process, be it an error or data. | |
970 | */ | |
971 | static void as_completed_request(request_queue_t *q, struct request *rq) | |
972 | { | |
973 | struct as_data *ad = q->elevator->elevator_data; | |
974 | struct as_rq *arq = RQ_DATA(rq); | |
975 | ||
976 | WARN_ON(!list_empty(&rq->queuelist)); | |
977 | ||
1da177e4 LT |
978 | if (arq->state != AS_RQ_REMOVED) { |
979 | printk("arq->state %d\n", arq->state); | |
980 | WARN_ON(1); | |
981 | goto out; | |
982 | } | |
983 | ||
1da177e4 LT |
984 | if (ad->changed_batch && ad->nr_dispatched == 1) { |
985 | kblockd_schedule_work(&ad->antic_work); | |
986 | ad->changed_batch = 0; | |
987 | ||
988 | if (ad->batch_data_dir == REQ_SYNC) | |
989 | ad->new_batch = 1; | |
990 | } | |
991 | WARN_ON(ad->nr_dispatched == 0); | |
992 | ad->nr_dispatched--; | |
993 | ||
994 | /* | |
995 | * Start counting the batch from when a request of that direction is | |
996 | * actually serviced. This should help devices with big TCQ windows | |
997 | * and writeback caches | |
998 | */ | |
999 | if (ad->new_batch && ad->batch_data_dir == arq->is_sync) { | |
1000 | update_write_batch(ad); | |
1001 | ad->current_batch_expires = jiffies + | |
1002 | ad->batch_expire[REQ_SYNC]; | |
1003 | ad->new_batch = 0; | |
1004 | } | |
1005 | ||
1006 | if (ad->io_context == arq->io_context && ad->io_context) { | |
1007 | ad->antic_start = jiffies; | |
1008 | ad->ioc_finished = 1; | |
1009 | if (ad->antic_status == ANTIC_WAIT_REQ) { | |
1010 | /* | |
1011 | * We were waiting on this request, now anticipate | |
1012 | * the next one | |
1013 | */ | |
1014 | as_antic_waitnext(ad); | |
1015 | } | |
1016 | } | |
1017 | ||
b4878f24 | 1018 | as_put_io_context(arq); |
1da177e4 LT |
1019 | out: |
1020 | arq->state = AS_RQ_POSTSCHED; | |
1021 | } | |
1022 | ||
1023 | /* | |
1024 | * as_remove_queued_request removes a request from the pre dispatch queue | |
1025 | * without updating refcounts. It is expected the caller will drop the | |
1026 | * reference unless it replaces the request at somepart of the elevator | |
1027 | * (ie. the dispatch queue) | |
1028 | */ | |
1029 | static void as_remove_queued_request(request_queue_t *q, struct request *rq) | |
1030 | { | |
1031 | struct as_rq *arq = RQ_DATA(rq); | |
1032 | const int data_dir = arq->is_sync; | |
1033 | struct as_data *ad = q->elevator->elevator_data; | |
1034 | ||
1035 | WARN_ON(arq->state != AS_RQ_QUEUED); | |
1036 | ||
1037 | if (arq->io_context && arq->io_context->aic) { | |
1038 | BUG_ON(!atomic_read(&arq->io_context->aic->nr_queued)); | |
1039 | atomic_dec(&arq->io_context->aic->nr_queued); | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * Update the "next_arq" cache if we are about to remove its | |
1044 | * entry | |
1045 | */ | |
1046 | if (ad->next_arq[data_dir] == arq) | |
1047 | ad->next_arq[data_dir] = as_find_next_arq(ad, arq); | |
1048 | ||
1049 | list_del_init(&arq->fifo); | |
98b11471 | 1050 | as_del_arq_hash(arq); |
1da177e4 LT |
1051 | as_del_arq_rb(ad, arq); |
1052 | } | |
1053 | ||
1da177e4 LT |
1054 | /* |
1055 | * as_fifo_expired returns 0 if there are no expired reads on the fifo, | |
1056 | * 1 otherwise. It is ratelimited so that we only perform the check once per | |
1057 | * `fifo_expire' interval. Otherwise a large number of expired requests | |
1058 | * would create a hopeless seekstorm. | |
1059 | * | |
1060 | * See as_antic_expired comment. | |
1061 | */ | |
1062 | static int as_fifo_expired(struct as_data *ad, int adir) | |
1063 | { | |
1064 | struct as_rq *arq; | |
1065 | long delta_jif; | |
1066 | ||
1067 | delta_jif = jiffies - ad->last_check_fifo[adir]; | |
1068 | if (unlikely(delta_jif < 0)) | |
1069 | delta_jif = -delta_jif; | |
1070 | if (delta_jif < ad->fifo_expire[adir]) | |
1071 | return 0; | |
1072 | ||
1073 | ad->last_check_fifo[adir] = jiffies; | |
1074 | ||
1075 | if (list_empty(&ad->fifo_list[adir])) | |
1076 | return 0; | |
1077 | ||
1078 | arq = list_entry_fifo(ad->fifo_list[adir].next); | |
1079 | ||
1080 | return time_after(jiffies, arq->expires); | |
1081 | } | |
1082 | ||
1083 | /* | |
1084 | * as_batch_expired returns true if the current batch has expired. A batch | |
1085 | * is a set of reads or a set of writes. | |
1086 | */ | |
1087 | static inline int as_batch_expired(struct as_data *ad) | |
1088 | { | |
1089 | if (ad->changed_batch || ad->new_batch) | |
1090 | return 0; | |
1091 | ||
1092 | if (ad->batch_data_dir == REQ_SYNC) | |
1093 | /* TODO! add a check so a complete fifo gets written? */ | |
1094 | return time_after(jiffies, ad->current_batch_expires); | |
1095 | ||
1096 | return time_after(jiffies, ad->current_batch_expires) | |
1097 | || ad->current_write_count == 0; | |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * move an entry to dispatch queue | |
1102 | */ | |
1103 | static void as_move_to_dispatch(struct as_data *ad, struct as_rq *arq) | |
1104 | { | |
1105 | struct request *rq = arq->request; | |
1da177e4 LT |
1106 | const int data_dir = arq->is_sync; |
1107 | ||
1108 | BUG_ON(!ON_RB(&arq->rb_node)); | |
1109 | ||
1110 | as_antic_stop(ad); | |
1111 | ad->antic_status = ANTIC_OFF; | |
1112 | ||
1113 | /* | |
1114 | * This has to be set in order to be correctly updated by | |
1115 | * as_find_next_arq | |
1116 | */ | |
1117 | ad->last_sector[data_dir] = rq->sector + rq->nr_sectors; | |
1118 | ||
1119 | if (data_dir == REQ_SYNC) { | |
1120 | /* In case we have to anticipate after this */ | |
1121 | copy_io_context(&ad->io_context, &arq->io_context); | |
1122 | } else { | |
1123 | if (ad->io_context) { | |
1124 | put_io_context(ad->io_context); | |
1125 | ad->io_context = NULL; | |
1126 | } | |
1127 | ||
1128 | if (ad->current_write_count != 0) | |
1129 | ad->current_write_count--; | |
1130 | } | |
1131 | ad->ioc_finished = 0; | |
1132 | ||
1133 | ad->next_arq[data_dir] = as_find_next_arq(ad, arq); | |
1134 | ||
1135 | /* | |
1136 | * take it off the sort and fifo list, add to dispatch queue | |
1137 | */ | |
1da177e4 LT |
1138 | while (!list_empty(&rq->queuelist)) { |
1139 | struct request *__rq = list_entry_rq(rq->queuelist.next); | |
1140 | struct as_rq *__arq = RQ_DATA(__rq); | |
1141 | ||
b4878f24 JA |
1142 | list_del(&__rq->queuelist); |
1143 | ||
1144 | elv_dispatch_add_tail(ad->q, __rq); | |
1da177e4 LT |
1145 | |
1146 | if (__arq->io_context && __arq->io_context->aic) | |
1147 | atomic_inc(&__arq->io_context->aic->nr_dispatched); | |
1148 | ||
1149 | WARN_ON(__arq->state != AS_RQ_QUEUED); | |
1150 | __arq->state = AS_RQ_DISPATCHED; | |
1151 | ||
1152 | ad->nr_dispatched++; | |
1153 | } | |
1154 | ||
1155 | as_remove_queued_request(ad->q, rq); | |
1156 | WARN_ON(arq->state != AS_RQ_QUEUED); | |
1157 | ||
b4878f24 JA |
1158 | elv_dispatch_sort(ad->q, rq); |
1159 | ||
1da177e4 LT |
1160 | arq->state = AS_RQ_DISPATCHED; |
1161 | if (arq->io_context && arq->io_context->aic) | |
1162 | atomic_inc(&arq->io_context->aic->nr_dispatched); | |
1163 | ad->nr_dispatched++; | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * as_dispatch_request selects the best request according to | |
1168 | * read/write expire, batch expire, etc, and moves it to the dispatch | |
1169 | * queue. Returns 1 if a request was found, 0 otherwise. | |
1170 | */ | |
b4878f24 | 1171 | static int as_dispatch_request(request_queue_t *q, int force) |
1da177e4 | 1172 | { |
b4878f24 | 1173 | struct as_data *ad = q->elevator->elevator_data; |
1da177e4 LT |
1174 | struct as_rq *arq; |
1175 | const int reads = !list_empty(&ad->fifo_list[REQ_SYNC]); | |
1176 | const int writes = !list_empty(&ad->fifo_list[REQ_ASYNC]); | |
1177 | ||
b4878f24 JA |
1178 | if (unlikely(force)) { |
1179 | /* | |
1180 | * Forced dispatch, accounting is useless. Reset | |
1181 | * accounting states and dump fifo_lists. Note that | |
1182 | * batch_data_dir is reset to REQ_SYNC to avoid | |
1183 | * screwing write batch accounting as write batch | |
1184 | * accounting occurs on W->R transition. | |
1185 | */ | |
1186 | int dispatched = 0; | |
1187 | ||
1188 | ad->batch_data_dir = REQ_SYNC; | |
1189 | ad->changed_batch = 0; | |
1190 | ad->new_batch = 0; | |
1191 | ||
1192 | while (ad->next_arq[REQ_SYNC]) { | |
1193 | as_move_to_dispatch(ad, ad->next_arq[REQ_SYNC]); | |
1194 | dispatched++; | |
1195 | } | |
1196 | ad->last_check_fifo[REQ_SYNC] = jiffies; | |
1197 | ||
1198 | while (ad->next_arq[REQ_ASYNC]) { | |
1199 | as_move_to_dispatch(ad, ad->next_arq[REQ_ASYNC]); | |
1200 | dispatched++; | |
1201 | } | |
1202 | ad->last_check_fifo[REQ_ASYNC] = jiffies; | |
1203 | ||
1204 | return dispatched; | |
1205 | } | |
1206 | ||
1da177e4 LT |
1207 | /* Signal that the write batch was uncontended, so we can't time it */ |
1208 | if (ad->batch_data_dir == REQ_ASYNC && !reads) { | |
1209 | if (ad->current_write_count == 0 || !writes) | |
1210 | ad->write_batch_idled = 1; | |
1211 | } | |
1212 | ||
1213 | if (!(reads || writes) | |
1214 | || ad->antic_status == ANTIC_WAIT_REQ | |
1215 | || ad->antic_status == ANTIC_WAIT_NEXT | |
1216 | || ad->changed_batch) | |
1217 | return 0; | |
1218 | ||
f5b3db00 | 1219 | if (!(reads && writes && as_batch_expired(ad))) { |
1da177e4 LT |
1220 | /* |
1221 | * batch is still running or no reads or no writes | |
1222 | */ | |
1223 | arq = ad->next_arq[ad->batch_data_dir]; | |
1224 | ||
1225 | if (ad->batch_data_dir == REQ_SYNC && ad->antic_expire) { | |
1226 | if (as_fifo_expired(ad, REQ_SYNC)) | |
1227 | goto fifo_expired; | |
1228 | ||
1229 | if (as_can_anticipate(ad, arq)) { | |
1230 | as_antic_waitreq(ad); | |
1231 | return 0; | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | if (arq) { | |
1236 | /* we have a "next request" */ | |
1237 | if (reads && !writes) | |
1238 | ad->current_batch_expires = | |
1239 | jiffies + ad->batch_expire[REQ_SYNC]; | |
1240 | goto dispatch_request; | |
1241 | } | |
1242 | } | |
1243 | ||
1244 | /* | |
1245 | * at this point we are not running a batch. select the appropriate | |
1246 | * data direction (read / write) | |
1247 | */ | |
1248 | ||
1249 | if (reads) { | |
1250 | BUG_ON(RB_EMPTY(&ad->sort_list[REQ_SYNC])); | |
1251 | ||
1252 | if (writes && ad->batch_data_dir == REQ_SYNC) | |
1253 | /* | |
1254 | * Last batch was a read, switch to writes | |
1255 | */ | |
1256 | goto dispatch_writes; | |
1257 | ||
1258 | if (ad->batch_data_dir == REQ_ASYNC) { | |
1259 | WARN_ON(ad->new_batch); | |
1260 | ad->changed_batch = 1; | |
1261 | } | |
1262 | ad->batch_data_dir = REQ_SYNC; | |
1263 | arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next); | |
1264 | ad->last_check_fifo[ad->batch_data_dir] = jiffies; | |
1265 | goto dispatch_request; | |
1266 | } | |
1267 | ||
1268 | /* | |
1269 | * the last batch was a read | |
1270 | */ | |
1271 | ||
1272 | if (writes) { | |
1273 | dispatch_writes: | |
1274 | BUG_ON(RB_EMPTY(&ad->sort_list[REQ_ASYNC])); | |
1275 | ||
1276 | if (ad->batch_data_dir == REQ_SYNC) { | |
1277 | ad->changed_batch = 1; | |
1278 | ||
1279 | /* | |
1280 | * new_batch might be 1 when the queue runs out of | |
1281 | * reads. A subsequent submission of a write might | |
1282 | * cause a change of batch before the read is finished. | |
1283 | */ | |
1284 | ad->new_batch = 0; | |
1285 | } | |
1286 | ad->batch_data_dir = REQ_ASYNC; | |
1287 | ad->current_write_count = ad->write_batch_count; | |
1288 | ad->write_batch_idled = 0; | |
1289 | arq = ad->next_arq[ad->batch_data_dir]; | |
1290 | goto dispatch_request; | |
1291 | } | |
1292 | ||
1293 | BUG(); | |
1294 | return 0; | |
1295 | ||
1296 | dispatch_request: | |
1297 | /* | |
1298 | * If a request has expired, service it. | |
1299 | */ | |
1300 | ||
1301 | if (as_fifo_expired(ad, ad->batch_data_dir)) { | |
1302 | fifo_expired: | |
1303 | arq = list_entry_fifo(ad->fifo_list[ad->batch_data_dir].next); | |
1304 | BUG_ON(arq == NULL); | |
1305 | } | |
1306 | ||
1307 | if (ad->changed_batch) { | |
1308 | WARN_ON(ad->new_batch); | |
1309 | ||
1310 | if (ad->nr_dispatched) | |
1311 | return 0; | |
1312 | ||
1313 | if (ad->batch_data_dir == REQ_ASYNC) | |
1314 | ad->current_batch_expires = jiffies + | |
1315 | ad->batch_expire[REQ_ASYNC]; | |
1316 | else | |
1317 | ad->new_batch = 1; | |
1318 | ||
1319 | ad->changed_batch = 0; | |
1320 | } | |
1321 | ||
1322 | /* | |
1323 | * arq is the selected appropriate request. | |
1324 | */ | |
1325 | as_move_to_dispatch(ad, arq); | |
1326 | ||
1327 | return 1; | |
1328 | } | |
1329 | ||
1da177e4 LT |
1330 | /* |
1331 | * Add arq to a list behind alias | |
1332 | */ | |
1333 | static inline void | |
f5b3db00 NP |
1334 | as_add_aliased_request(struct as_data *ad, struct as_rq *arq, |
1335 | struct as_rq *alias) | |
1da177e4 LT |
1336 | { |
1337 | struct request *req = arq->request; | |
1338 | struct list_head *insert = alias->request->queuelist.prev; | |
1339 | ||
1340 | /* | |
1341 | * Transfer list of aliases | |
1342 | */ | |
1343 | while (!list_empty(&req->queuelist)) { | |
1344 | struct request *__rq = list_entry_rq(req->queuelist.next); | |
1345 | struct as_rq *__arq = RQ_DATA(__rq); | |
1346 | ||
1347 | list_move_tail(&__rq->queuelist, &alias->request->queuelist); | |
1348 | ||
1349 | WARN_ON(__arq->state != AS_RQ_QUEUED); | |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Another request with the same start sector on the rbtree. | |
1354 | * Link this request to that sector. They are untangled in | |
1355 | * as_move_to_dispatch | |
1356 | */ | |
1357 | list_add(&arq->request->queuelist, insert); | |
1358 | ||
1359 | /* | |
1360 | * Don't want to have to handle merges. | |
1361 | */ | |
98b11471 | 1362 | as_del_arq_hash(arq); |
47e627ce | 1363 | arq->request->flags |= REQ_NOMERGE; |
1da177e4 LT |
1364 | } |
1365 | ||
1366 | /* | |
1367 | * add arq to rbtree and fifo | |
1368 | */ | |
b4878f24 | 1369 | static void as_add_request(request_queue_t *q, struct request *rq) |
1da177e4 | 1370 | { |
b4878f24 JA |
1371 | struct as_data *ad = q->elevator->elevator_data; |
1372 | struct as_rq *arq = RQ_DATA(rq); | |
1da177e4 LT |
1373 | struct as_rq *alias; |
1374 | int data_dir; | |
1375 | ||
b4878f24 JA |
1376 | if (arq->state != AS_RQ_PRESCHED) { |
1377 | printk("arq->state: %d\n", arq->state); | |
1378 | WARN_ON(1); | |
1379 | } | |
1380 | arq->state = AS_RQ_NEW; | |
1381 | ||
1da177e4 LT |
1382 | if (rq_data_dir(arq->request) == READ |
1383 | || current->flags&PF_SYNCWRITE) | |
1384 | arq->is_sync = 1; | |
1385 | else | |
1386 | arq->is_sync = 0; | |
1387 | data_dir = arq->is_sync; | |
1388 | ||
1389 | arq->io_context = as_get_io_context(); | |
1390 | ||
1391 | if (arq->io_context) { | |
1392 | as_update_iohist(ad, arq->io_context->aic, arq->request); | |
1393 | atomic_inc(&arq->io_context->aic->nr_queued); | |
1394 | } | |
1395 | ||
1396 | alias = as_add_arq_rb(ad, arq); | |
1397 | if (!alias) { | |
1398 | /* | |
1399 | * set expire time (only used for reads) and add to fifo list | |
1400 | */ | |
1401 | arq->expires = jiffies + ad->fifo_expire[data_dir]; | |
1402 | list_add_tail(&arq->fifo, &ad->fifo_list[data_dir]); | |
1403 | ||
98b11471 | 1404 | if (rq_mergeable(arq->request)) |
1da177e4 | 1405 | as_add_arq_hash(ad, arq); |
1da177e4 LT |
1406 | as_update_arq(ad, arq); /* keep state machine up to date */ |
1407 | ||
1408 | } else { | |
1409 | as_add_aliased_request(ad, arq, alias); | |
1410 | ||
1411 | /* | |
1412 | * have we been anticipating this request? | |
1413 | * or does it come from the same process as the one we are | |
1414 | * anticipating for? | |
1415 | */ | |
1416 | if (ad->antic_status == ANTIC_WAIT_REQ | |
1417 | || ad->antic_status == ANTIC_WAIT_NEXT) { | |
1418 | if (as_can_break_anticipation(ad, arq)) | |
1419 | as_antic_stop(ad); | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | arq->state = AS_RQ_QUEUED; | |
1424 | } | |
1425 | ||
b4878f24 | 1426 | static void as_activate_request(request_queue_t *q, struct request *rq) |
1da177e4 | 1427 | { |
1da177e4 LT |
1428 | struct as_rq *arq = RQ_DATA(rq); |
1429 | ||
b4878f24 JA |
1430 | WARN_ON(arq->state != AS_RQ_DISPATCHED); |
1431 | arq->state = AS_RQ_REMOVED; | |
1432 | if (arq->io_context && arq->io_context->aic) | |
1433 | atomic_dec(&arq->io_context->aic->nr_dispatched); | |
1da177e4 LT |
1434 | } |
1435 | ||
b4878f24 | 1436 | static void as_deactivate_request(request_queue_t *q, struct request *rq) |
1da177e4 | 1437 | { |
1da177e4 LT |
1438 | struct as_rq *arq = RQ_DATA(rq); |
1439 | ||
b4878f24 JA |
1440 | WARN_ON(arq->state != AS_RQ_REMOVED); |
1441 | arq->state = AS_RQ_DISPATCHED; | |
1442 | if (arq->io_context && arq->io_context->aic) | |
1443 | atomic_inc(&arq->io_context->aic->nr_dispatched); | |
1da177e4 LT |
1444 | } |
1445 | ||
1446 | /* | |
1447 | * as_queue_empty tells us if there are requests left in the device. It may | |
1448 | * not be the case that a driver can get the next request even if the queue | |
1449 | * is not empty - it is used in the block layer to check for plugging and | |
1450 | * merging opportunities | |
1451 | */ | |
1452 | static int as_queue_empty(request_queue_t *q) | |
1453 | { | |
1454 | struct as_data *ad = q->elevator->elevator_data; | |
1455 | ||
b4878f24 JA |
1456 | return list_empty(&ad->fifo_list[REQ_ASYNC]) |
1457 | && list_empty(&ad->fifo_list[REQ_SYNC]); | |
1da177e4 LT |
1458 | } |
1459 | ||
f5b3db00 NP |
1460 | static struct request *as_former_request(request_queue_t *q, |
1461 | struct request *rq) | |
1da177e4 LT |
1462 | { |
1463 | struct as_rq *arq = RQ_DATA(rq); | |
1464 | struct rb_node *rbprev = rb_prev(&arq->rb_node); | |
1465 | struct request *ret = NULL; | |
1466 | ||
1467 | if (rbprev) | |
1468 | ret = rb_entry_arq(rbprev)->request; | |
1469 | ||
1470 | return ret; | |
1471 | } | |
1472 | ||
f5b3db00 NP |
1473 | static struct request *as_latter_request(request_queue_t *q, |
1474 | struct request *rq) | |
1da177e4 LT |
1475 | { |
1476 | struct as_rq *arq = RQ_DATA(rq); | |
1477 | struct rb_node *rbnext = rb_next(&arq->rb_node); | |
1478 | struct request *ret = NULL; | |
1479 | ||
1480 | if (rbnext) | |
1481 | ret = rb_entry_arq(rbnext)->request; | |
1482 | ||
1483 | return ret; | |
1484 | } | |
1485 | ||
1486 | static int | |
1487 | as_merge(request_queue_t *q, struct request **req, struct bio *bio) | |
1488 | { | |
1489 | struct as_data *ad = q->elevator->elevator_data; | |
1490 | sector_t rb_key = bio->bi_sector + bio_sectors(bio); | |
1491 | struct request *__rq; | |
1492 | int ret; | |
1493 | ||
1da177e4 LT |
1494 | /* |
1495 | * see if the merge hash can satisfy a back merge | |
1496 | */ | |
1497 | __rq = as_find_arq_hash(ad, bio->bi_sector); | |
1498 | if (__rq) { | |
1499 | BUG_ON(__rq->sector + __rq->nr_sectors != bio->bi_sector); | |
1500 | ||
1501 | if (elv_rq_merge_ok(__rq, bio)) { | |
1502 | ret = ELEVATOR_BACK_MERGE; | |
1503 | goto out; | |
1504 | } | |
1505 | } | |
1506 | ||
1507 | /* | |
1508 | * check for front merge | |
1509 | */ | |
1510 | __rq = as_find_arq_rb(ad, rb_key, bio_data_dir(bio)); | |
1511 | if (__rq) { | |
1512 | BUG_ON(rb_key != rq_rb_key(__rq)); | |
1513 | ||
1514 | if (elv_rq_merge_ok(__rq, bio)) { | |
1515 | ret = ELEVATOR_FRONT_MERGE; | |
1516 | goto out; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | return ELEVATOR_NO_MERGE; | |
1521 | out: | |
1da177e4 LT |
1522 | if (ret) { |
1523 | if (rq_mergeable(__rq)) | |
1524 | as_hot_arq_hash(ad, RQ_DATA(__rq)); | |
1525 | } | |
1526 | *req = __rq; | |
1527 | return ret; | |
1528 | } | |
1529 | ||
1530 | static void as_merged_request(request_queue_t *q, struct request *req) | |
1531 | { | |
1532 | struct as_data *ad = q->elevator->elevator_data; | |
1533 | struct as_rq *arq = RQ_DATA(req); | |
1534 | ||
1535 | /* | |
1536 | * hash always needs to be repositioned, key is end sector | |
1537 | */ | |
1538 | as_del_arq_hash(arq); | |
1539 | as_add_arq_hash(ad, arq); | |
1540 | ||
1541 | /* | |
1542 | * if the merge was a front merge, we need to reposition request | |
1543 | */ | |
1544 | if (rq_rb_key(req) != arq->rb_key) { | |
1545 | struct as_rq *alias, *next_arq = NULL; | |
1546 | ||
1547 | if (ad->next_arq[arq->is_sync] == arq) | |
1548 | next_arq = as_find_next_arq(ad, arq); | |
1549 | ||
1550 | /* | |
1551 | * Note! We should really be moving any old aliased requests | |
1552 | * off this request and try to insert them into the rbtree. We | |
1553 | * currently don't bother. Ditto the next function. | |
1554 | */ | |
1555 | as_del_arq_rb(ad, arq); | |
f5b3db00 | 1556 | if ((alias = as_add_arq_rb(ad, arq))) { |
1da177e4 LT |
1557 | list_del_init(&arq->fifo); |
1558 | as_add_aliased_request(ad, arq, alias); | |
1559 | if (next_arq) | |
1560 | ad->next_arq[arq->is_sync] = next_arq; | |
1561 | } | |
1562 | /* | |
1563 | * Note! At this stage of this and the next function, our next | |
1564 | * request may not be optimal - eg the request may have "grown" | |
1565 | * behind the disk head. We currently don't bother adjusting. | |
1566 | */ | |
1567 | } | |
1da177e4 LT |
1568 | } |
1569 | ||
f5b3db00 NP |
1570 | static void as_merged_requests(request_queue_t *q, struct request *req, |
1571 | struct request *next) | |
1da177e4 LT |
1572 | { |
1573 | struct as_data *ad = q->elevator->elevator_data; | |
1574 | struct as_rq *arq = RQ_DATA(req); | |
1575 | struct as_rq *anext = RQ_DATA(next); | |
1576 | ||
1577 | BUG_ON(!arq); | |
1578 | BUG_ON(!anext); | |
1579 | ||
1580 | /* | |
1581 | * reposition arq (this is the merged request) in hash, and in rbtree | |
1582 | * in case of a front merge | |
1583 | */ | |
1584 | as_del_arq_hash(arq); | |
1585 | as_add_arq_hash(ad, arq); | |
1586 | ||
1587 | if (rq_rb_key(req) != arq->rb_key) { | |
1588 | struct as_rq *alias, *next_arq = NULL; | |
1589 | ||
1590 | if (ad->next_arq[arq->is_sync] == arq) | |
1591 | next_arq = as_find_next_arq(ad, arq); | |
1592 | ||
1593 | as_del_arq_rb(ad, arq); | |
f5b3db00 | 1594 | if ((alias = as_add_arq_rb(ad, arq))) { |
1da177e4 LT |
1595 | list_del_init(&arq->fifo); |
1596 | as_add_aliased_request(ad, arq, alias); | |
1597 | if (next_arq) | |
1598 | ad->next_arq[arq->is_sync] = next_arq; | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | /* | |
1603 | * if anext expires before arq, assign its expire time to arq | |
1604 | * and move into anext position (anext will be deleted) in fifo | |
1605 | */ | |
1606 | if (!list_empty(&arq->fifo) && !list_empty(&anext->fifo)) { | |
1607 | if (time_before(anext->expires, arq->expires)) { | |
1608 | list_move(&arq->fifo, &anext->fifo); | |
1609 | arq->expires = anext->expires; | |
1610 | /* | |
1611 | * Don't copy here but swap, because when anext is | |
1612 | * removed below, it must contain the unused context | |
1613 | */ | |
1614 | swap_io_context(&arq->io_context, &anext->io_context); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | /* | |
1619 | * Transfer list of aliases | |
1620 | */ | |
1621 | while (!list_empty(&next->queuelist)) { | |
1622 | struct request *__rq = list_entry_rq(next->queuelist.next); | |
1623 | struct as_rq *__arq = RQ_DATA(__rq); | |
1624 | ||
1625 | list_move_tail(&__rq->queuelist, &req->queuelist); | |
1626 | ||
1627 | WARN_ON(__arq->state != AS_RQ_QUEUED); | |
1628 | } | |
1629 | ||
1630 | /* | |
1631 | * kill knowledge of next, this one is a goner | |
1632 | */ | |
1633 | as_remove_queued_request(q, next); | |
b4878f24 | 1634 | as_put_io_context(anext); |
1da177e4 LT |
1635 | |
1636 | anext->state = AS_RQ_MERGED; | |
1637 | } | |
1638 | ||
1639 | /* | |
1640 | * This is executed in a "deferred" process context, by kblockd. It calls the | |
1641 | * driver's request_fn so the driver can submit that request. | |
1642 | * | |
1643 | * IMPORTANT! This guy will reenter the elevator, so set up all queue global | |
1644 | * state before calling, and don't rely on any state over calls. | |
1645 | * | |
1646 | * FIXME! dispatch queue is not a queue at all! | |
1647 | */ | |
1648 | static void as_work_handler(void *data) | |
1649 | { | |
1650 | struct request_queue *q = data; | |
1651 | unsigned long flags; | |
1652 | ||
1653 | spin_lock_irqsave(q->queue_lock, flags); | |
b4878f24 | 1654 | if (!as_queue_empty(q)) |
1da177e4 LT |
1655 | q->request_fn(q); |
1656 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1657 | } | |
1658 | ||
1659 | static void as_put_request(request_queue_t *q, struct request *rq) | |
1660 | { | |
1661 | struct as_data *ad = q->elevator->elevator_data; | |
1662 | struct as_rq *arq = RQ_DATA(rq); | |
1663 | ||
1664 | if (!arq) { | |
1665 | WARN_ON(1); | |
1666 | return; | |
1667 | } | |
1668 | ||
b4878f24 JA |
1669 | if (unlikely(arq->state != AS_RQ_POSTSCHED && |
1670 | arq->state != AS_RQ_PRESCHED && | |
1671 | arq->state != AS_RQ_MERGED)) { | |
1da177e4 LT |
1672 | printk("arq->state %d\n", arq->state); |
1673 | WARN_ON(1); | |
1674 | } | |
1675 | ||
1676 | mempool_free(arq, ad->arq_pool); | |
1677 | rq->elevator_private = NULL; | |
1678 | } | |
1679 | ||
22e2c507 | 1680 | static int as_set_request(request_queue_t *q, struct request *rq, |
8267e268 | 1681 | struct bio *bio, gfp_t gfp_mask) |
1da177e4 LT |
1682 | { |
1683 | struct as_data *ad = q->elevator->elevator_data; | |
1684 | struct as_rq *arq = mempool_alloc(ad->arq_pool, gfp_mask); | |
1685 | ||
1686 | if (arq) { | |
1687 | memset(arq, 0, sizeof(*arq)); | |
1688 | RB_CLEAR(&arq->rb_node); | |
1689 | arq->request = rq; | |
1690 | arq->state = AS_RQ_PRESCHED; | |
1691 | arq->io_context = NULL; | |
1692 | INIT_LIST_HEAD(&arq->hash); | |
1693 | arq->on_hash = 0; | |
1694 | INIT_LIST_HEAD(&arq->fifo); | |
1695 | rq->elevator_private = arq; | |
1696 | return 0; | |
1697 | } | |
1698 | ||
1699 | return 1; | |
1700 | } | |
1701 | ||
22e2c507 | 1702 | static int as_may_queue(request_queue_t *q, int rw, struct bio *bio) |
1da177e4 LT |
1703 | { |
1704 | int ret = ELV_MQUEUE_MAY; | |
1705 | struct as_data *ad = q->elevator->elevator_data; | |
1706 | struct io_context *ioc; | |
1707 | if (ad->antic_status == ANTIC_WAIT_REQ || | |
1708 | ad->antic_status == ANTIC_WAIT_NEXT) { | |
1709 | ioc = as_get_io_context(); | |
1710 | if (ad->io_context == ioc) | |
1711 | ret = ELV_MQUEUE_MUST; | |
1712 | put_io_context(ioc); | |
1713 | } | |
1714 | ||
1715 | return ret; | |
1716 | } | |
1717 | ||
1718 | static void as_exit_queue(elevator_t *e) | |
1719 | { | |
1720 | struct as_data *ad = e->elevator_data; | |
1721 | ||
1722 | del_timer_sync(&ad->antic_timer); | |
1723 | kblockd_flush(); | |
1724 | ||
1725 | BUG_ON(!list_empty(&ad->fifo_list[REQ_SYNC])); | |
1726 | BUG_ON(!list_empty(&ad->fifo_list[REQ_ASYNC])); | |
1727 | ||
1728 | mempool_destroy(ad->arq_pool); | |
1729 | put_io_context(ad->io_context); | |
1730 | kfree(ad->hash); | |
1731 | kfree(ad); | |
1732 | } | |
1733 | ||
1734 | /* | |
1735 | * initialize elevator private data (as_data), and alloc a arq for | |
1736 | * each request on the free lists | |
1737 | */ | |
1738 | static int as_init_queue(request_queue_t *q, elevator_t *e) | |
1739 | { | |
1740 | struct as_data *ad; | |
1741 | int i; | |
1742 | ||
1743 | if (!arq_pool) | |
1744 | return -ENOMEM; | |
1745 | ||
1946089a | 1746 | ad = kmalloc_node(sizeof(*ad), GFP_KERNEL, q->node); |
1da177e4 LT |
1747 | if (!ad) |
1748 | return -ENOMEM; | |
1749 | memset(ad, 0, sizeof(*ad)); | |
1750 | ||
1751 | ad->q = q; /* Identify what queue the data belongs to */ | |
1752 | ||
1946089a CL |
1753 | ad->hash = kmalloc_node(sizeof(struct list_head)*AS_HASH_ENTRIES, |
1754 | GFP_KERNEL, q->node); | |
1da177e4 LT |
1755 | if (!ad->hash) { |
1756 | kfree(ad); | |
1757 | return -ENOMEM; | |
1758 | } | |
1759 | ||
1946089a CL |
1760 | ad->arq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, |
1761 | mempool_free_slab, arq_pool, q->node); | |
1da177e4 LT |
1762 | if (!ad->arq_pool) { |
1763 | kfree(ad->hash); | |
1764 | kfree(ad); | |
1765 | return -ENOMEM; | |
1766 | } | |
1767 | ||
1768 | /* anticipatory scheduling helpers */ | |
1769 | ad->antic_timer.function = as_antic_timeout; | |
1770 | ad->antic_timer.data = (unsigned long)q; | |
1771 | init_timer(&ad->antic_timer); | |
1772 | INIT_WORK(&ad->antic_work, as_work_handler, q); | |
1773 | ||
1774 | for (i = 0; i < AS_HASH_ENTRIES; i++) | |
1775 | INIT_LIST_HEAD(&ad->hash[i]); | |
1776 | ||
1777 | INIT_LIST_HEAD(&ad->fifo_list[REQ_SYNC]); | |
1778 | INIT_LIST_HEAD(&ad->fifo_list[REQ_ASYNC]); | |
1779 | ad->sort_list[REQ_SYNC] = RB_ROOT; | |
1780 | ad->sort_list[REQ_ASYNC] = RB_ROOT; | |
1da177e4 LT |
1781 | ad->fifo_expire[REQ_SYNC] = default_read_expire; |
1782 | ad->fifo_expire[REQ_ASYNC] = default_write_expire; | |
1783 | ad->antic_expire = default_antic_expire; | |
1784 | ad->batch_expire[REQ_SYNC] = default_read_batch_expire; | |
1785 | ad->batch_expire[REQ_ASYNC] = default_write_batch_expire; | |
1786 | e->elevator_data = ad; | |
1787 | ||
1788 | ad->current_batch_expires = jiffies + ad->batch_expire[REQ_SYNC]; | |
1789 | ad->write_batch_count = ad->batch_expire[REQ_ASYNC] / 10; | |
1790 | if (ad->write_batch_count < 2) | |
1791 | ad->write_batch_count = 2; | |
1792 | ||
1793 | return 0; | |
1794 | } | |
1795 | ||
1796 | /* | |
1797 | * sysfs parts below | |
1798 | */ | |
1799 | struct as_fs_entry { | |
1800 | struct attribute attr; | |
1801 | ssize_t (*show)(struct as_data *, char *); | |
1802 | ssize_t (*store)(struct as_data *, const char *, size_t); | |
1803 | }; | |
1804 | ||
1805 | static ssize_t | |
1806 | as_var_show(unsigned int var, char *page) | |
1807 | { | |
1da177e4 LT |
1808 | return sprintf(page, "%d\n", var); |
1809 | } | |
1810 | ||
1811 | static ssize_t | |
1812 | as_var_store(unsigned long *var, const char *page, size_t count) | |
1813 | { | |
1da177e4 LT |
1814 | char *p = (char *) page; |
1815 | ||
c9b3ad67 | 1816 | *var = simple_strtoul(p, &p, 10); |
1da177e4 LT |
1817 | return count; |
1818 | } | |
1819 | ||
1820 | static ssize_t as_est_show(struct as_data *ad, char *page) | |
1821 | { | |
1822 | int pos = 0; | |
1823 | ||
f5b3db00 NP |
1824 | pos += sprintf(page+pos, "%lu %% exit probability\n", |
1825 | 100*ad->exit_prob/256); | |
1826 | pos += sprintf(page+pos, "%lu %% probability of exiting without a " | |
1827 | "cooperating process submitting IO\n", | |
1828 | 100*ad->exit_no_coop/256); | |
1da177e4 | 1829 | pos += sprintf(page+pos, "%lu ms new thinktime\n", ad->new_ttime_mean); |
f5b3db00 NP |
1830 | pos += sprintf(page+pos, "%llu sectors new seek distance\n", |
1831 | (unsigned long long)ad->new_seek_mean); | |
1da177e4 LT |
1832 | |
1833 | return pos; | |
1834 | } | |
1835 | ||
1836 | #define SHOW_FUNCTION(__FUNC, __VAR) \ | |
1837 | static ssize_t __FUNC(struct as_data *ad, char *page) \ | |
1838 | { \ | |
1839 | return as_var_show(jiffies_to_msecs((__VAR)), (page)); \ | |
1840 | } | |
1841 | SHOW_FUNCTION(as_readexpire_show, ad->fifo_expire[REQ_SYNC]); | |
1842 | SHOW_FUNCTION(as_writeexpire_show, ad->fifo_expire[REQ_ASYNC]); | |
1843 | SHOW_FUNCTION(as_anticexpire_show, ad->antic_expire); | |
1844 | SHOW_FUNCTION(as_read_batchexpire_show, ad->batch_expire[REQ_SYNC]); | |
1845 | SHOW_FUNCTION(as_write_batchexpire_show, ad->batch_expire[REQ_ASYNC]); | |
1846 | #undef SHOW_FUNCTION | |
1847 | ||
1848 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ | |
1849 | static ssize_t __FUNC(struct as_data *ad, const char *page, size_t count) \ | |
1850 | { \ | |
1851 | int ret = as_var_store(__PTR, (page), count); \ | |
1852 | if (*(__PTR) < (MIN)) \ | |
1853 | *(__PTR) = (MIN); \ | |
1854 | else if (*(__PTR) > (MAX)) \ | |
1855 | *(__PTR) = (MAX); \ | |
1856 | *(__PTR) = msecs_to_jiffies(*(__PTR)); \ | |
1857 | return ret; \ | |
1858 | } | |
1859 | STORE_FUNCTION(as_readexpire_store, &ad->fifo_expire[REQ_SYNC], 0, INT_MAX); | |
1860 | STORE_FUNCTION(as_writeexpire_store, &ad->fifo_expire[REQ_ASYNC], 0, INT_MAX); | |
1861 | STORE_FUNCTION(as_anticexpire_store, &ad->antic_expire, 0, INT_MAX); | |
1862 | STORE_FUNCTION(as_read_batchexpire_store, | |
1863 | &ad->batch_expire[REQ_SYNC], 0, INT_MAX); | |
1864 | STORE_FUNCTION(as_write_batchexpire_store, | |
1865 | &ad->batch_expire[REQ_ASYNC], 0, INT_MAX); | |
1866 | #undef STORE_FUNCTION | |
1867 | ||
1868 | static struct as_fs_entry as_est_entry = { | |
1869 | .attr = {.name = "est_time", .mode = S_IRUGO }, | |
1870 | .show = as_est_show, | |
1871 | }; | |
1872 | static struct as_fs_entry as_readexpire_entry = { | |
1873 | .attr = {.name = "read_expire", .mode = S_IRUGO | S_IWUSR }, | |
1874 | .show = as_readexpire_show, | |
1875 | .store = as_readexpire_store, | |
1876 | }; | |
1877 | static struct as_fs_entry as_writeexpire_entry = { | |
1878 | .attr = {.name = "write_expire", .mode = S_IRUGO | S_IWUSR }, | |
1879 | .show = as_writeexpire_show, | |
1880 | .store = as_writeexpire_store, | |
1881 | }; | |
1882 | static struct as_fs_entry as_anticexpire_entry = { | |
1883 | .attr = {.name = "antic_expire", .mode = S_IRUGO | S_IWUSR }, | |
1884 | .show = as_anticexpire_show, | |
1885 | .store = as_anticexpire_store, | |
1886 | }; | |
1887 | static struct as_fs_entry as_read_batchexpire_entry = { | |
1888 | .attr = {.name = "read_batch_expire", .mode = S_IRUGO | S_IWUSR }, | |
1889 | .show = as_read_batchexpire_show, | |
1890 | .store = as_read_batchexpire_store, | |
1891 | }; | |
1892 | static struct as_fs_entry as_write_batchexpire_entry = { | |
1893 | .attr = {.name = "write_batch_expire", .mode = S_IRUGO | S_IWUSR }, | |
1894 | .show = as_write_batchexpire_show, | |
1895 | .store = as_write_batchexpire_store, | |
1896 | }; | |
1897 | ||
1898 | static struct attribute *default_attrs[] = { | |
1899 | &as_est_entry.attr, | |
1900 | &as_readexpire_entry.attr, | |
1901 | &as_writeexpire_entry.attr, | |
1902 | &as_anticexpire_entry.attr, | |
1903 | &as_read_batchexpire_entry.attr, | |
1904 | &as_write_batchexpire_entry.attr, | |
1905 | NULL, | |
1906 | }; | |
1907 | ||
1908 | #define to_as(atr) container_of((atr), struct as_fs_entry, attr) | |
1909 | ||
1910 | static ssize_t | |
1911 | as_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
1912 | { | |
1913 | elevator_t *e = container_of(kobj, elevator_t, kobj); | |
1914 | struct as_fs_entry *entry = to_as(attr); | |
1915 | ||
1916 | if (!entry->show) | |
6c1852a0 | 1917 | return -EIO; |
1da177e4 LT |
1918 | |
1919 | return entry->show(e->elevator_data, page); | |
1920 | } | |
1921 | ||
1922 | static ssize_t | |
1923 | as_attr_store(struct kobject *kobj, struct attribute *attr, | |
1924 | const char *page, size_t length) | |
1925 | { | |
1926 | elevator_t *e = container_of(kobj, elevator_t, kobj); | |
1927 | struct as_fs_entry *entry = to_as(attr); | |
1928 | ||
1929 | if (!entry->store) | |
6c1852a0 | 1930 | return -EIO; |
1da177e4 LT |
1931 | |
1932 | return entry->store(e->elevator_data, page, length); | |
1933 | } | |
1934 | ||
1935 | static struct sysfs_ops as_sysfs_ops = { | |
1936 | .show = as_attr_show, | |
1937 | .store = as_attr_store, | |
1938 | }; | |
1939 | ||
1940 | static struct kobj_type as_ktype = { | |
1941 | .sysfs_ops = &as_sysfs_ops, | |
1942 | .default_attrs = default_attrs, | |
1943 | }; | |
1944 | ||
1945 | static struct elevator_type iosched_as = { | |
1946 | .ops = { | |
1947 | .elevator_merge_fn = as_merge, | |
1948 | .elevator_merged_fn = as_merged_request, | |
1949 | .elevator_merge_req_fn = as_merged_requests, | |
b4878f24 JA |
1950 | .elevator_dispatch_fn = as_dispatch_request, |
1951 | .elevator_add_req_fn = as_add_request, | |
1952 | .elevator_activate_req_fn = as_activate_request, | |
1da177e4 LT |
1953 | .elevator_deactivate_req_fn = as_deactivate_request, |
1954 | .elevator_queue_empty_fn = as_queue_empty, | |
1955 | .elevator_completed_req_fn = as_completed_request, | |
1956 | .elevator_former_req_fn = as_former_request, | |
1957 | .elevator_latter_req_fn = as_latter_request, | |
1958 | .elevator_set_req_fn = as_set_request, | |
1959 | .elevator_put_req_fn = as_put_request, | |
1960 | .elevator_may_queue_fn = as_may_queue, | |
1961 | .elevator_init_fn = as_init_queue, | |
1962 | .elevator_exit_fn = as_exit_queue, | |
1963 | }, | |
1964 | ||
1965 | .elevator_ktype = &as_ktype, | |
1966 | .elevator_name = "anticipatory", | |
1967 | .elevator_owner = THIS_MODULE, | |
1968 | }; | |
1969 | ||
1970 | static int __init as_init(void) | |
1971 | { | |
1972 | int ret; | |
1973 | ||
1974 | arq_pool = kmem_cache_create("as_arq", sizeof(struct as_rq), | |
1975 | 0, 0, NULL, NULL); | |
1976 | if (!arq_pool) | |
1977 | return -ENOMEM; | |
1978 | ||
1979 | ret = elv_register(&iosched_as); | |
1980 | if (!ret) { | |
1981 | /* | |
1982 | * don't allow AS to get unregistered, since we would have | |
1983 | * to browse all tasks in the system and release their | |
1984 | * as_io_context first | |
1985 | */ | |
1986 | __module_get(THIS_MODULE); | |
1987 | return 0; | |
1988 | } | |
1989 | ||
1990 | kmem_cache_destroy(arq_pool); | |
1991 | return ret; | |
1992 | } | |
1993 | ||
1994 | static void __exit as_exit(void) | |
1995 | { | |
1da177e4 | 1996 | elv_unregister(&iosched_as); |
83521d3e | 1997 | kmem_cache_destroy(arq_pool); |
1da177e4 LT |
1998 | } |
1999 | ||
2000 | module_init(as_init); | |
2001 | module_exit(as_exit); | |
2002 | ||
2003 | MODULE_AUTHOR("Nick Piggin"); | |
2004 | MODULE_LICENSE("GPL"); | |
2005 | MODULE_DESCRIPTION("anticipatory IO scheduler"); |