blob: 3fc6e505e9c87c39db7f78fac42396e7f4a1a5da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 */
9#include <linux/kernel.h>
10#include <linux/fs.h>
11#include <linux/blkdev.h>
12#include <linux/elevator.h>
13#include <linux/bio.h>
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/compiler.h>
19#include <linux/hash.h>
20#include <linux/rbtree.h>
21#include <linux/mempool.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020022#include <linux/ioprio.h>
23#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * tunables
27 */
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_quantum = 4; /* max queue in one round of service */
29static const int cfq_queued = 8; /* minimum rq allocate limit per-queue*/
30static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
31static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
32static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020035static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010036static const int cfq_slice_async_rq = 2;
Jens Axboe3b181522005-06-27 10:56:24 +020037static int cfq_slice_idle = HZ / 100;
Jens Axboe22e2c502005-06-27 10:55:12 +020038
39#define CFQ_IDLE_GRACE (HZ / 10)
40#define CFQ_SLICE_SCALE (5)
41
42#define CFQ_KEY_ASYNC (0)
Jens Axboe3b181522005-06-27 10:56:24 +020043#define CFQ_KEY_ANY (0xffff)
Jens Axboe22e2c502005-06-27 10:55:12 +020044
45/*
46 * disable queueing at the driver/hardware level
47 */
Arjan van de Ven64100092006-01-06 09:46:02 +010048static const int cfq_max_depth = 2;
Jens Axboe22e2c502005-06-27 10:55:12 +020049
Al Viroa6a07632006-03-18 13:26:44 -050050static DEFINE_RWLOCK(cfq_exit_lock);
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * for the hash of cfqq inside the cfqd
54 */
55#define CFQ_QHASH_SHIFT 6
56#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
57#define list_entry_qhash(entry) hlist_entry((entry), struct cfq_queue, cfq_hash)
58
59/*
60 * for the hash of crq inside the cfqq
61 */
62#define CFQ_MHASH_SHIFT 6
63#define CFQ_MHASH_BLOCK(sec) ((sec) >> 3)
64#define CFQ_MHASH_ENTRIES (1 << CFQ_MHASH_SHIFT)
65#define CFQ_MHASH_FN(sec) hash_long(CFQ_MHASH_BLOCK(sec), CFQ_MHASH_SHIFT)
66#define rq_hash_key(rq) ((rq)->sector + (rq)->nr_sectors)
67#define list_entry_hash(ptr) hlist_entry((ptr), struct cfq_rq, hash)
68
69#define list_entry_cfqq(ptr) list_entry((ptr), struct cfq_queue, cfq_list)
Jens Axboe22e2c502005-06-27 10:55:12 +020070#define list_entry_fifo(ptr) list_entry((ptr), struct request, queuelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define RQ_DATA(rq) (rq)->elevator_private
73
74/*
75 * rb-tree defines
76 */
77#define RB_NONE (2)
78#define RB_EMPTY(node) ((node)->rb_node == NULL)
79#define RB_CLEAR_COLOR(node) (node)->rb_color = RB_NONE
80#define RB_CLEAR(node) do { \
81 (node)->rb_parent = NULL; \
82 RB_CLEAR_COLOR((node)); \
83 (node)->rb_right = NULL; \
84 (node)->rb_left = NULL; \
85} while (0)
86#define RB_CLEAR_ROOT(root) ((root)->rb_node = NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#define rb_entry_crq(node) rb_entry((node), struct cfq_rq, rb_node)
88#define rq_rb_key(rq) (rq)->sector
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static kmem_cache_t *crq_pool;
91static kmem_cache_t *cfq_pool;
92static kmem_cache_t *cfq_ioc_pool;
93
Jens Axboe22e2c502005-06-27 10:55:12 +020094#define CFQ_PRIO_LISTS IOPRIO_BE_NR
95#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
96#define cfq_class_be(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_BE)
97#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
98
Jens Axboe3b181522005-06-27 10:56:24 +020099#define ASYNC (0)
100#define SYNC (1)
101
102#define cfq_cfqq_dispatched(cfqq) \
103 ((cfqq)->on_dispatch[ASYNC] + (cfqq)->on_dispatch[SYNC])
104
105#define cfq_cfqq_class_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
106
107#define cfq_cfqq_sync(cfqq) \
108 (cfq_cfqq_class_sync(cfqq) || (cfqq)->on_dispatch[SYNC])
Jens Axboe22e2c502005-06-27 10:55:12 +0200109
110/*
111 * Per block device queue structure
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +0200114 atomic_t ref;
115 request_queue_t *queue;
116
117 /*
118 * rr list of queues with requests and the count of them
119 */
120 struct list_head rr_list[CFQ_PRIO_LISTS];
121 struct list_head busy_rr;
122 struct list_head cur_rr;
123 struct list_head idle_rr;
124 unsigned int busy_queues;
125
126 /*
127 * non-ordered list of empty cfqq's
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct list_head empty_list;
130
Jens Axboe22e2c502005-06-27 10:55:12 +0200131 /*
132 * cfqq lookup hash
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jens Axboe22e2c502005-06-27 10:55:12 +0200136 /*
137 * global crq hash for all queues
138 */
139 struct hlist_head *crq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 unsigned int max_queued;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 mempool_t *crq_pool;
144
Jens Axboe22e2c502005-06-27 10:55:12 +0200145 int rq_in_driver;
146
147 /*
148 * schedule slice state info
149 */
150 /*
151 * idle window management
152 */
153 struct timer_list idle_slice_timer;
154 struct work_struct unplug_work;
155
156 struct cfq_queue *active_queue;
157 struct cfq_io_context *active_cic;
158 int cur_prio, cur_end_prio;
159 unsigned int dispatch_slice;
160
161 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 sector_t last_sector;
Jens Axboe22e2c502005-06-27 10:55:12 +0200164 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 unsigned int rq_starved;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /*
169 * tunables, see top of file
170 */
171 unsigned int cfq_quantum;
172 unsigned int cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +0200173 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned int cfq_back_penalty;
175 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200176 unsigned int cfq_slice[2];
177 unsigned int cfq_slice_async_rq;
178 unsigned int cfq_slice_idle;
179 unsigned int cfq_max_depth;
Al Virod9ff4182006-03-18 13:51:22 -0500180
181 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Jens Axboe22e2c502005-06-27 10:55:12 +0200184/*
185 * Per process-grouping structure
186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187struct cfq_queue {
188 /* reference count */
189 atomic_t ref;
190 /* parent cfq_data */
191 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200192 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct hlist_node cfq_hash;
194 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200195 unsigned int key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* on either rr or empty list of cfqd */
197 struct list_head cfq_list;
198 /* sorted list of pending requests */
199 struct rb_root sort_list;
200 /* if fifo isn't expired, next request to serve */
201 struct cfq_rq *next_crq;
202 /* requests queued in sort_list */
203 int queued[2];
204 /* currently allocated requests */
205 int allocated[2];
206 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200207 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jens Axboe22e2c502005-06-27 10:55:12 +0200209 unsigned long slice_start;
210 unsigned long slice_end;
211 unsigned long slice_left;
212 unsigned long service_last;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jens Axboe3b181522005-06-27 10:56:24 +0200214 /* number of requests that are on the dispatch list */
215 int on_dispatch[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200216
217 /* io prio of this group */
218 unsigned short ioprio, org_ioprio;
219 unsigned short ioprio_class, org_ioprio_class;
220
Jens Axboe3b181522005-06-27 10:56:24 +0200221 /* various state flags, see below */
222 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
225struct cfq_rq {
226 struct rb_node rb_node;
227 sector_t rb_key;
228 struct request *request;
229 struct hlist_node hash;
230
231 struct cfq_queue *cfq_queue;
232 struct cfq_io_context *io_context;
233
Jens Axboe3b181522005-06-27 10:56:24 +0200234 unsigned int crq_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
Jens Axboe3b181522005-06-27 10:56:24 +0200237enum cfqq_state_flags {
238 CFQ_CFQQ_FLAG_on_rr = 0,
239 CFQ_CFQQ_FLAG_wait_request,
240 CFQ_CFQQ_FLAG_must_alloc,
241 CFQ_CFQQ_FLAG_must_alloc_slice,
242 CFQ_CFQQ_FLAG_must_dispatch,
243 CFQ_CFQQ_FLAG_fifo_expire,
244 CFQ_CFQQ_FLAG_idle_window,
245 CFQ_CFQQ_FLAG_prio_changed,
Jens Axboe3b181522005-06-27 10:56:24 +0200246};
247
248#define CFQ_CFQQ_FNS(name) \
249static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
250{ \
251 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
252} \
253static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
254{ \
255 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
256} \
257static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
258{ \
259 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
260}
261
262CFQ_CFQQ_FNS(on_rr);
263CFQ_CFQQ_FNS(wait_request);
264CFQ_CFQQ_FNS(must_alloc);
265CFQ_CFQQ_FNS(must_alloc_slice);
266CFQ_CFQQ_FNS(must_dispatch);
267CFQ_CFQQ_FNS(fifo_expire);
268CFQ_CFQQ_FNS(idle_window);
269CFQ_CFQQ_FNS(prio_changed);
Jens Axboe3b181522005-06-27 10:56:24 +0200270#undef CFQ_CFQQ_FNS
271
272enum cfq_rq_state_flags {
Jens Axboeb4878f22005-10-20 16:42:29 +0200273 CFQ_CRQ_FLAG_is_sync = 0,
Jens Axboe3b181522005-06-27 10:56:24 +0200274};
275
276#define CFQ_CRQ_FNS(name) \
277static inline void cfq_mark_crq_##name(struct cfq_rq *crq) \
278{ \
279 crq->crq_flags |= (1 << CFQ_CRQ_FLAG_##name); \
280} \
281static inline void cfq_clear_crq_##name(struct cfq_rq *crq) \
282{ \
283 crq->crq_flags &= ~(1 << CFQ_CRQ_FLAG_##name); \
284} \
285static inline int cfq_crq_##name(const struct cfq_rq *crq) \
286{ \
287 return (crq->crq_flags & (1 << CFQ_CRQ_FLAG_##name)) != 0; \
288}
289
Jens Axboe3b181522005-06-27 10:56:24 +0200290CFQ_CRQ_FNS(is_sync);
Jens Axboe3b181522005-06-27 10:56:24 +0200291#undef CFQ_CRQ_FNS
292
293static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboeb4878f22005-10-20 16:42:29 +0200294static void cfq_dispatch_insert(request_queue_t *, struct cfq_rq *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static void cfq_put_cfqd(struct cfq_data *cfqd);
296
Jens Axboe22e2c502005-06-27 10:55:12 +0200297#define process_sync(tsk) ((tsk)->flags & PF_SYNCWRITE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299/*
300 * lots of deadline iosched dupes, can be abstracted later...
301 */
302static inline void cfq_del_crq_hash(struct cfq_rq *crq)
303{
304 hlist_del_init(&crq->hash);
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static inline void cfq_add_crq_hash(struct cfq_data *cfqd, struct cfq_rq *crq)
308{
309 const int hash_idx = CFQ_MHASH_FN(rq_hash_key(crq->request));
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 hlist_add_head(&crq->hash, &cfqd->crq_hash[hash_idx]);
312}
313
314static struct request *cfq_find_rq_hash(struct cfq_data *cfqd, sector_t offset)
315{
316 struct hlist_head *hash_list = &cfqd->crq_hash[CFQ_MHASH_FN(offset)];
317 struct hlist_node *entry, *next;
318
319 hlist_for_each_safe(entry, next, hash_list) {
320 struct cfq_rq *crq = list_entry_hash(entry);
321 struct request *__rq = crq->request;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!rq_mergeable(__rq)) {
324 cfq_del_crq_hash(crq);
325 continue;
326 }
327
328 if (rq_hash_key(__rq) == offset)
329 return __rq;
330 }
331
332 return NULL;
333}
334
Andrew Morton99f95e52005-06-27 20:14:05 -0700335/*
336 * scheduler run of queue, if there are requests pending and no one in the
337 * driver that will restart queueing
338 */
339static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
340{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100341 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700342 kblockd_schedule_work(&cfqd->unplug_work);
343}
344
345static int cfq_queue_empty(request_queue_t *q)
346{
347 struct cfq_data *cfqd = q->elevator->elevator_data;
348
Jens Axboeb4878f22005-10-20 16:42:29 +0200349 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
353 * Lifted from AS - choose which of crq1 and crq2 that is best served now.
354 * We choose the request that is closest to the head right now. Distance
355 * behind the head are penalized and only allowed to a certain extent.
356 */
357static struct cfq_rq *
358cfq_choose_req(struct cfq_data *cfqd, struct cfq_rq *crq1, struct cfq_rq *crq2)
359{
360 sector_t last, s1, s2, d1 = 0, d2 = 0;
361 int r1_wrap = 0, r2_wrap = 0; /* requests are behind the disk head */
362 unsigned long back_max;
363
364 if (crq1 == NULL || crq1 == crq2)
365 return crq2;
366 if (crq2 == NULL)
367 return crq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200368
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200369 if (cfq_crq_is_sync(crq1) && !cfq_crq_is_sync(crq2))
370 return crq1;
371 else if (cfq_crq_is_sync(crq2) && !cfq_crq_is_sync(crq1))
Jens Axboe22e2c502005-06-27 10:55:12 +0200372 return crq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 s1 = crq1->request->sector;
375 s2 = crq2->request->sector;
376
377 last = cfqd->last_sector;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * by definition, 1KiB is 2 sectors
381 */
382 back_max = cfqd->cfq_back_max * 2;
383
384 /*
385 * Strict one way elevator _except_ in the case where we allow
386 * short backward seeks which are biased as twice the cost of a
387 * similar forward seek.
388 */
389 if (s1 >= last)
390 d1 = s1 - last;
391 else if (s1 + back_max >= last)
392 d1 = (last - s1) * cfqd->cfq_back_penalty;
393 else
394 r1_wrap = 1;
395
396 if (s2 >= last)
397 d2 = s2 - last;
398 else if (s2 + back_max >= last)
399 d2 = (last - s2) * cfqd->cfq_back_penalty;
400 else
401 r2_wrap = 1;
402
403 /* Found required data */
404 if (!r1_wrap && r2_wrap)
405 return crq1;
406 else if (!r2_wrap && r1_wrap)
407 return crq2;
408 else if (r1_wrap && r2_wrap) {
409 /* both behind the head */
410 if (s1 <= s2)
411 return crq1;
412 else
413 return crq2;
414 }
415
416 /* Both requests in front of the head */
417 if (d1 < d2)
418 return crq1;
419 else if (d2 < d1)
420 return crq2;
421 else {
422 if (s1 >= s2)
423 return crq1;
424 else
425 return crq2;
426 }
427}
428
429/*
430 * would be nice to take fifo expire time into account as well
431 */
432static struct cfq_rq *
433cfq_find_next_crq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
434 struct cfq_rq *last)
435{
436 struct cfq_rq *crq_next = NULL, *crq_prev = NULL;
437 struct rb_node *rbnext, *rbprev;
438
Jens Axboeb4878f22005-10-20 16:42:29 +0200439 if (!(rbnext = rb_next(&last->rb_node))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 rbnext = rb_first(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +0200441 if (rbnext == &last->rb_node)
442 rbnext = NULL;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 rbprev = rb_prev(&last->rb_node);
446
447 if (rbprev)
448 crq_prev = rb_entry_crq(rbprev);
449 if (rbnext)
450 crq_next = rb_entry_crq(rbnext);
451
452 return cfq_choose_req(cfqd, crq_next, crq_prev);
453}
454
455static void cfq_update_next_crq(struct cfq_rq *crq)
456{
457 struct cfq_queue *cfqq = crq->cfq_queue;
458
459 if (cfqq->next_crq == crq)
460 cfqq->next_crq = cfq_find_next_crq(cfqq->cfqd, cfqq, crq);
461}
462
Jens Axboe22e2c502005-06-27 10:55:12 +0200463static void cfq_resort_rr_list(struct cfq_queue *cfqq, int preempted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jens Axboe22e2c502005-06-27 10:55:12 +0200465 struct cfq_data *cfqd = cfqq->cfqd;
466 struct list_head *list, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jens Axboe3b181522005-06-27 10:56:24 +0200468 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 list_del(&cfqq->cfq_list);
471
Jens Axboe22e2c502005-06-27 10:55:12 +0200472 if (cfq_class_rt(cfqq))
473 list = &cfqd->cur_rr;
474 else if (cfq_class_idle(cfqq))
475 list = &cfqd->idle_rr;
476 else {
477 /*
478 * if cfqq has requests in flight, don't allow it to be
479 * found in cfq_set_active_queue before it has finished them.
480 * this is done to increase fairness between a process that
481 * has lots of io pending vs one that only generates one
482 * sporadically or synchronously
483 */
Jens Axboe3b181522005-06-27 10:56:24 +0200484 if (cfq_cfqq_dispatched(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200485 list = &cfqd->busy_rr;
486 else
487 list = &cfqd->rr_list[cfqq->ioprio];
488 }
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200491 * if queue was preempted, just add to front to be fair. busy_rr
492 * isn't sorted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200494 if (preempted || list == &cfqd->busy_rr) {
495 list_add(&cfqq->cfq_list, list);
496 return;
497 }
498
499 /*
500 * sort by when queue was last serviced
501 */
502 entry = list;
503 while ((entry = entry->prev) != list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct cfq_queue *__cfqq = list_entry_cfqq(entry);
505
Jens Axboe22e2c502005-06-27 10:55:12 +0200506 if (!__cfqq->service_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
Jens Axboe22e2c502005-06-27 10:55:12 +0200508 if (time_before(__cfqq->service_last, cfqq->service_last))
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
512 list_add(&cfqq->cfq_list, entry);
513}
514
515/*
516 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200517 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
519static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200520cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Jens Axboe3b181522005-06-27 10:56:24 +0200522 BUG_ON(cfq_cfqq_on_rr(cfqq));
523 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 cfqd->busy_queues++;
525
Jens Axboeb4878f22005-10-20 16:42:29 +0200526 cfq_resort_rr_list(cfqq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static inline void
530cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
531{
Jens Axboe3b181522005-06-27 10:56:24 +0200532 BUG_ON(!cfq_cfqq_on_rr(cfqq));
533 cfq_clear_cfqq_on_rr(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200534 list_move(&cfqq->cfq_list, &cfqd->empty_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 BUG_ON(!cfqd->busy_queues);
537 cfqd->busy_queues--;
538}
539
540/*
541 * rb tree support functions
542 */
543static inline void cfq_del_crq_rb(struct cfq_rq *crq)
544{
545 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboeb4878f22005-10-20 16:42:29 +0200546 struct cfq_data *cfqd = cfqq->cfqd;
547 const int sync = cfq_crq_is_sync(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens Axboeb4878f22005-10-20 16:42:29 +0200549 BUG_ON(!cfqq->queued[sync]);
550 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Jens Axboeb4878f22005-10-20 16:42:29 +0200552 cfq_update_next_crq(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jens Axboeb4878f22005-10-20 16:42:29 +0200554 rb_erase(&crq->rb_node, &cfqq->sort_list);
555 RB_CLEAR_COLOR(&crq->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jens Axboeb4878f22005-10-20 16:42:29 +0200557 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY(&cfqq->sort_list))
558 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static struct cfq_rq *
562__cfq_add_crq_rb(struct cfq_rq *crq)
563{
564 struct rb_node **p = &crq->cfq_queue->sort_list.rb_node;
565 struct rb_node *parent = NULL;
566 struct cfq_rq *__crq;
567
568 while (*p) {
569 parent = *p;
570 __crq = rb_entry_crq(parent);
571
572 if (crq->rb_key < __crq->rb_key)
573 p = &(*p)->rb_left;
574 else if (crq->rb_key > __crq->rb_key)
575 p = &(*p)->rb_right;
576 else
577 return __crq;
578 }
579
580 rb_link_node(&crq->rb_node, parent, p);
581 return NULL;
582}
583
584static void cfq_add_crq_rb(struct cfq_rq *crq)
585{
586 struct cfq_queue *cfqq = crq->cfq_queue;
587 struct cfq_data *cfqd = cfqq->cfqd;
588 struct request *rq = crq->request;
589 struct cfq_rq *__alias;
590
591 crq->rb_key = rq_rb_key(rq);
Jens Axboe3b181522005-06-27 10:56:24 +0200592 cfqq->queued[cfq_crq_is_sync(crq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
595 * looks a little odd, but the first insert might return an alias.
596 * if that happens, put the alias on the dispatch list
597 */
598 while ((__alias = __cfq_add_crq_rb(crq)) != NULL)
Jens Axboeb4878f22005-10-20 16:42:29 +0200599 cfq_dispatch_insert(cfqd->queue, __alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 rb_insert_color(&crq->rb_node, &cfqq->sort_list);
602
Jens Axboe3b181522005-06-27 10:56:24 +0200603 if (!cfq_cfqq_on_rr(cfqq))
Jens Axboeb4878f22005-10-20 16:42:29 +0200604 cfq_add_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /*
607 * check if this request is a better next-serve candidate
608 */
609 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
610}
611
612static inline void
613cfq_reposition_crq_rb(struct cfq_queue *cfqq, struct cfq_rq *crq)
614{
Jens Axboeb4878f22005-10-20 16:42:29 +0200615 rb_erase(&crq->rb_node, &cfqq->sort_list);
616 cfqq->queued[cfq_crq_is_sync(crq)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 cfq_add_crq_rb(crq);
619}
620
Jens Axboe22e2c502005-06-27 10:55:12 +0200621static struct request *cfq_find_rq_rb(struct cfq_data *cfqd, sector_t sector)
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Jens Axboe3b181522005-06-27 10:56:24 +0200624 struct cfq_queue *cfqq = cfq_find_cfq_hash(cfqd, current->pid, CFQ_KEY_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct rb_node *n;
626
627 if (!cfqq)
628 goto out;
629
630 n = cfqq->sort_list.rb_node;
631 while (n) {
632 struct cfq_rq *crq = rb_entry_crq(n);
633
634 if (sector < crq->rb_key)
635 n = n->rb_left;
636 else if (sector > crq->rb_key)
637 n = n->rb_right;
638 else
639 return crq->request;
640 }
641
642out:
643 return NULL;
644}
645
Jens Axboeb4878f22005-10-20 16:42:29 +0200646static void cfq_activate_request(request_queue_t *q, struct request *rq)
647{
648 struct cfq_data *cfqd = q->elevator->elevator_data;
649
650 cfqd->rq_in_driver++;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
654{
Jens Axboe22e2c502005-06-27 10:55:12 +0200655 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jens Axboeb4878f22005-10-20 16:42:29 +0200657 WARN_ON(!cfqd->rq_in_driver);
658 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Jens Axboeb4878f22005-10-20 16:42:29 +0200661static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct cfq_rq *crq = RQ_DATA(rq);
664
Jens Axboeb4878f22005-10-20 16:42:29 +0200665 list_del_init(&rq->queuelist);
666 cfq_del_crq_rb(crq);
Tejun Heo98b11472005-10-20 16:46:54 +0200667 cfq_del_crq_hash(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static int
671cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
672{
673 struct cfq_data *cfqd = q->elevator->elevator_data;
674 struct request *__rq;
675 int ret;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 __rq = cfq_find_rq_hash(cfqd, bio->bi_sector);
Jens Axboe22e2c502005-06-27 10:55:12 +0200678 if (__rq && elv_rq_merge_ok(__rq, bio)) {
679 ret = ELEVATOR_BACK_MERGE;
680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 __rq = cfq_find_rq_rb(cfqd, bio->bi_sector + bio_sectors(bio));
Jens Axboe22e2c502005-06-27 10:55:12 +0200684 if (__rq && elv_rq_merge_ok(__rq, bio)) {
685 ret = ELEVATOR_FRONT_MERGE;
686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 return ELEVATOR_NO_MERGE;
690out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 *req = __rq;
692 return ret;
693}
694
695static void cfq_merged_request(request_queue_t *q, struct request *req)
696{
697 struct cfq_data *cfqd = q->elevator->elevator_data;
698 struct cfq_rq *crq = RQ_DATA(req);
699
700 cfq_del_crq_hash(crq);
701 cfq_add_crq_hash(cfqd, crq);
702
Jens Axboeb4878f22005-10-20 16:42:29 +0200703 if (rq_rb_key(req) != crq->rb_key) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct cfq_queue *cfqq = crq->cfq_queue;
705
706 cfq_update_next_crq(crq);
707 cfq_reposition_crq_rb(cfqq, crq);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static void
712cfq_merged_requests(request_queue_t *q, struct request *rq,
713 struct request *next)
714{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cfq_merged_request(q, rq);
716
Jens Axboe22e2c502005-06-27 10:55:12 +0200717 /*
718 * reposition in fifo if next is older than rq
719 */
720 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
721 time_before(next->start_time, rq->start_time))
722 list_move(&rq->queuelist, &next->queuelist);
723
Jens Axboeb4878f22005-10-20 16:42:29 +0200724 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200725}
726
727static inline void
728__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
729{
730 if (cfqq) {
731 /*
732 * stop potential idle class queues waiting service
733 */
734 del_timer(&cfqd->idle_class_timer);
735
736 cfqq->slice_start = jiffies;
737 cfqq->slice_end = 0;
738 cfqq->slice_left = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200739 cfq_clear_cfqq_must_alloc_slice(cfqq);
740 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200741 }
742
743 cfqd->active_queue = cfqq;
744}
745
746/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100747 * current cfqq expired its slice (or was too idle), select new one
748 */
749static void
750__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
751 int preempted)
752{
753 unsigned long now = jiffies;
754
755 if (cfq_cfqq_wait_request(cfqq))
756 del_timer(&cfqd->idle_slice_timer);
757
758 if (!preempted && !cfq_cfqq_dispatched(cfqq)) {
759 cfqq->service_last = now;
760 cfq_schedule_dispatch(cfqd);
761 }
762
763 cfq_clear_cfqq_must_dispatch(cfqq);
764 cfq_clear_cfqq_wait_request(cfqq);
765
766 /*
767 * store what was left of this slice, if the queue idled out
768 * or was preempted
769 */
770 if (time_after(cfqq->slice_end, now))
771 cfqq->slice_left = cfqq->slice_end - now;
772 else
773 cfqq->slice_left = 0;
774
775 if (cfq_cfqq_on_rr(cfqq))
776 cfq_resort_rr_list(cfqq, preempted);
777
778 if (cfqq == cfqd->active_queue)
779 cfqd->active_queue = NULL;
780
781 if (cfqd->active_cic) {
782 put_io_context(cfqd->active_cic->ioc);
783 cfqd->active_cic = NULL;
784 }
785
786 cfqd->dispatch_slice = 0;
787}
788
789static inline void cfq_slice_expired(struct cfq_data *cfqd, int preempted)
790{
791 struct cfq_queue *cfqq = cfqd->active_queue;
792
793 if (cfqq)
794 __cfq_slice_expired(cfqd, cfqq, preempted);
795}
796
797/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200798 * 0
799 * 0,1
800 * 0,1,2
801 * 0,1,2,3
802 * 0,1,2,3,4
803 * 0,1,2,3,4,5
804 * 0,1,2,3,4,5,6
805 * 0,1,2,3,4,5,6,7
806 */
807static int cfq_get_next_prio_level(struct cfq_data *cfqd)
808{
809 int prio, wrap;
810
811 prio = -1;
812 wrap = 0;
813 do {
814 int p;
815
816 for (p = cfqd->cur_prio; p <= cfqd->cur_end_prio; p++) {
817 if (!list_empty(&cfqd->rr_list[p])) {
818 prio = p;
819 break;
820 }
821 }
822
823 if (prio != -1)
824 break;
825 cfqd->cur_prio = 0;
826 if (++cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
827 cfqd->cur_end_prio = 0;
828 if (wrap)
829 break;
830 wrap = 1;
831 }
832 } while (1);
833
834 if (unlikely(prio == -1))
835 return -1;
836
837 BUG_ON(prio >= CFQ_PRIO_LISTS);
838
839 list_splice_init(&cfqd->rr_list[prio], &cfqd->cur_rr);
840
841 cfqd->cur_prio = prio + 1;
842 if (cfqd->cur_prio > cfqd->cur_end_prio) {
843 cfqd->cur_end_prio = cfqd->cur_prio;
844 cfqd->cur_prio = 0;
845 }
846 if (cfqd->cur_end_prio == CFQ_PRIO_LISTS) {
847 cfqd->cur_prio = 0;
848 cfqd->cur_end_prio = 0;
849 }
850
851 return prio;
852}
853
Jens Axboe3b181522005-06-27 10:56:24 +0200854static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200855{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100856 struct cfq_queue *cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200857
858 /*
859 * if current list is non-empty, grab first entry. if it is empty,
860 * get next prio level and grab first entry then if any are spliced
861 */
862 if (!list_empty(&cfqd->cur_rr) || cfq_get_next_prio_level(cfqd) != -1)
863 cfqq = list_entry_cfqq(cfqd->cur_rr.next);
864
865 /*
866 * if we have idle queues and no rt or be queues had pending
867 * requests, either allow immediate service if the grace period
868 * has passed or arm the idle grace timer
869 */
870 if (!cfqq && !list_empty(&cfqd->idle_rr)) {
871 unsigned long end = cfqd->last_end_request + CFQ_IDLE_GRACE;
872
873 if (time_after_eq(jiffies, end))
874 cfqq = list_entry_cfqq(cfqd->idle_rr.next);
875 else
876 mod_timer(&cfqd->idle_class_timer, end);
877 }
878
879 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200880 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200881}
882
Jens Axboe22e2c502005-06-27 10:55:12 +0200883static int cfq_arm_slice_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
884
885{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100886 unsigned long sl;
887
Jens Axboe22e2c502005-06-27 10:55:12 +0200888 WARN_ON(!RB_EMPTY(&cfqq->sort_list));
889 WARN_ON(cfqq != cfqd->active_queue);
890
891 /*
892 * idle is disabled, either manually or by past process history
893 */
894 if (!cfqd->cfq_slice_idle)
895 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200896 if (!cfq_cfqq_idle_window(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +0200897 return 0;
898 /*
899 * task has exited, don't wait
900 */
901 if (cfqd->active_cic && !cfqd->active_cic->ioc->task)
902 return 0;
903
Jens Axboe3b181522005-06-27 10:56:24 +0200904 cfq_mark_cfqq_must_dispatch(cfqq);
905 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200906
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100907 sl = min(cfqq->slice_end - 1, (unsigned long) cfqd->cfq_slice_idle);
908 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe22e2c502005-06-27 10:55:12 +0200909 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Jens Axboeb4878f22005-10-20 16:42:29 +0200912static void cfq_dispatch_insert(request_queue_t *q, struct cfq_rq *crq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct cfq_data *cfqd = q->elevator->elevator_data;
915 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200916
917 cfqq->next_crq = cfq_find_next_crq(cfqd, cfqq, crq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200918 cfq_remove_request(crq->request);
Jens Axboe3b181522005-06-27 10:56:24 +0200919 cfqq->on_dispatch[cfq_crq_is_sync(crq)]++;
Jens Axboeb4878f22005-10-20 16:42:29 +0200920 elv_dispatch_sort(q, crq->request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923/*
924 * return expired entry, or NULL to just start from scratch in rbtree
925 */
926static inline struct cfq_rq *cfq_check_fifo(struct cfq_queue *cfqq)
927{
928 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200929 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 struct cfq_rq *crq;
931
Jens Axboe3b181522005-06-27 10:56:24 +0200932 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return NULL;
934
Jens Axboe22e2c502005-06-27 10:55:12 +0200935 if (!list_empty(&cfqq->fifo)) {
Jens Axboe3b181522005-06-27 10:56:24 +0200936 int fifo = cfq_cfqq_class_sync(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jens Axboe22e2c502005-06-27 10:55:12 +0200938 crq = RQ_DATA(list_entry_fifo(cfqq->fifo.next));
939 rq = crq->request;
940 if (time_after(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo])) {
Jens Axboe3b181522005-06-27 10:56:24 +0200941 cfq_mark_cfqq_fifo_expire(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200942 return crq;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 return NULL;
947}
948
949/*
Jens Axboe3b181522005-06-27 10:56:24 +0200950 * Scale schedule slice based on io priority. Use the sync time slice only
951 * if a queue is marked sync and has sync io queued. A sync queue with async
952 * io only, should not get full sync slice length.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Jens Axboe22e2c502005-06-27 10:55:12 +0200954static inline int
955cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Jens Axboe22e2c502005-06-27 10:55:12 +0200957 const int base_slice = cfqd->cfq_slice[cfq_cfqq_sync(cfqq)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jens Axboe22e2c502005-06-27 10:55:12 +0200959 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Jens Axboe22e2c502005-06-27 10:55:12 +0200961 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - cfqq->ioprio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Jens Axboe22e2c502005-06-27 10:55:12 +0200964static inline void
965cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
966{
967 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
968}
969
970static inline int
971cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
972{
973 const int base_rq = cfqd->cfq_slice_async_rq;
974
975 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
976
977 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
978}
979
980/*
981 * get next queue for service
982 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100983static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200984{
985 unsigned long now = jiffies;
986 struct cfq_queue *cfqq;
987
988 cfqq = cfqd->active_queue;
989 if (!cfqq)
990 goto new_queue;
991
992 /*
993 * slice has expired
994 */
Jens Axboe3b181522005-06-27 10:56:24 +0200995 if (!cfq_cfqq_must_dispatch(cfqq) && time_after(now, cfqq->slice_end))
996 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200997
998 /*
999 * if queue has requests, dispatch one. if not, check if
1000 * enough slice is left to wait for one
1001 */
1002 if (!RB_EMPTY(&cfqq->sort_list))
1003 goto keep_queue;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001004 else if (cfq_cfqq_class_sync(cfqq) &&
Jens Axboe22e2c502005-06-27 10:55:12 +02001005 time_before(now, cfqq->slice_end)) {
1006 if (cfq_arm_slice_timer(cfqd, cfqq))
1007 return NULL;
1008 }
1009
Jens Axboe3b181522005-06-27 10:56:24 +02001010expire:
Jens Axboe22e2c502005-06-27 10:55:12 +02001011 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001012new_queue:
1013 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001014keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001015 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001016}
1017
1018static int
1019__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1020 int max_dispatch)
1021{
1022 int dispatched = 0;
1023
1024 BUG_ON(RB_EMPTY(&cfqq->sort_list));
1025
1026 do {
1027 struct cfq_rq *crq;
1028
1029 /*
1030 * follow expired path, else get first next available
1031 */
1032 if ((crq = cfq_check_fifo(cfqq)) == NULL)
1033 crq = cfqq->next_crq;
1034
1035 /*
1036 * finally, insert request into driver dispatch list
1037 */
Jens Axboeb4878f22005-10-20 16:42:29 +02001038 cfq_dispatch_insert(cfqd->queue, crq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001039
1040 cfqd->dispatch_slice++;
1041 dispatched++;
1042
1043 if (!cfqd->active_cic) {
1044 atomic_inc(&crq->io_context->ioc->refcount);
1045 cfqd->active_cic = crq->io_context;
1046 }
1047
1048 if (RB_EMPTY(&cfqq->sort_list))
1049 break;
1050
1051 } while (dispatched < max_dispatch);
1052
1053 /*
1054 * if slice end isn't set yet, set it. if at least one request was
1055 * sync, use the sync time slice value
1056 */
1057 if (!cfqq->slice_end)
1058 cfq_set_prio_slice(cfqd, cfqq);
1059
1060 /*
1061 * expire an async queue immediately if it has used up its slice. idle
1062 * queue always expire after 1 dispatch round.
1063 */
1064 if ((!cfq_cfqq_sync(cfqq) &&
1065 cfqd->dispatch_slice >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1066 cfq_class_idle(cfqq))
1067 cfq_slice_expired(cfqd, 0);
1068
1069 return dispatched;
1070}
1071
1072static int
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001073cfq_forced_dispatch_cfqqs(struct list_head *list)
1074{
1075 int dispatched = 0;
1076 struct cfq_queue *cfqq, *next;
1077 struct cfq_rq *crq;
1078
1079 list_for_each_entry_safe(cfqq, next, list, cfq_list) {
1080 while ((crq = cfqq->next_crq)) {
1081 cfq_dispatch_insert(cfqq->cfqd->queue, crq);
1082 dispatched++;
1083 }
1084 BUG_ON(!list_empty(&cfqq->fifo));
1085 }
1086 return dispatched;
1087}
1088
1089static int
1090cfq_forced_dispatch(struct cfq_data *cfqd)
1091{
1092 int i, dispatched = 0;
1093
1094 for (i = 0; i < CFQ_PRIO_LISTS; i++)
1095 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->rr_list[i]);
1096
1097 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->busy_rr);
1098 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->cur_rr);
1099 dispatched += cfq_forced_dispatch_cfqqs(&cfqd->idle_rr);
1100
1101 cfq_slice_expired(cfqd, 0);
1102
1103 BUG_ON(cfqd->busy_queues);
1104
1105 return dispatched;
1106}
1107
1108static int
Jens Axboeb4878f22005-10-20 16:42:29 +02001109cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct cfq_data *cfqd = q->elevator->elevator_data;
1112 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jens Axboe22e2c502005-06-27 10:55:12 +02001114 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001117 if (unlikely(force))
1118 return cfq_forced_dispatch(cfqd);
1119
1120 cfqq = cfq_select_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001121 if (cfqq) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001122 int max_dispatch;
1123
1124 /*
1125 * if idle window is disabled, allow queue buildup
1126 */
1127 if (!cfq_cfqq_idle_window(cfqq) &&
1128 cfqd->rq_in_driver >= cfqd->cfq_max_depth)
1129 return 0;
1130
Jens Axboe3b181522005-06-27 10:56:24 +02001131 cfq_clear_cfqq_must_dispatch(cfqq);
1132 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001133 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001135 max_dispatch = cfqd->cfq_quantum;
1136 if (cfq_class_idle(cfqq))
1137 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Jens Axboe22e2c502005-06-27 10:55:12 +02001139 return __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
Jens Axboe22e2c502005-06-27 10:55:12 +02001142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145/*
1146 * task holds one reference to the queue, dropped when task exits. each crq
1147 * in-flight on this queue also holds a reference, dropped when crq is freed.
1148 *
1149 * queue lock must be held here.
1150 */
1151static void cfq_put_queue(struct cfq_queue *cfqq)
1152{
Jens Axboe22e2c502005-06-27 10:55:12 +02001153 struct cfq_data *cfqd = cfqq->cfqd;
1154
1155 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (!atomic_dec_and_test(&cfqq->ref))
1158 return;
1159
1160 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001161 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001162 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001164 if (unlikely(cfqd->active_queue == cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001165 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 cfq_put_cfqd(cfqq->cfqd);
1168
1169 /*
1170 * it's on the empty list and still hashed
1171 */
1172 list_del(&cfqq->cfq_list);
1173 hlist_del(&cfqq->cfq_hash);
1174 kmem_cache_free(cfq_pool, cfqq);
1175}
1176
1177static inline struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001178__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1179 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
1181 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
1182 struct hlist_node *entry, *next;
1183
1184 hlist_for_each_safe(entry, next, hash_list) {
1185 struct cfq_queue *__cfqq = list_entry_qhash(entry);
Al Virob0a69162006-03-14 15:32:50 -05001186 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Jens Axboe3b181522005-06-27 10:56:24 +02001188 if (__cfqq->key == key && (__p == prio || prio == CFQ_KEY_ANY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return __cfqq;
1190 }
1191
1192 return NULL;
1193}
1194
1195static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001196cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Jens Axboe3b181522005-06-27 10:56:24 +02001198 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201static void cfq_free_io_context(struct cfq_io_context *cic)
1202{
Jens Axboe22e2c502005-06-27 10:55:12 +02001203 struct cfq_io_context *__cic;
1204 struct list_head *entry, *next;
1205
1206 list_for_each_safe(entry, next, &cic->list) {
1207 __cic = list_entry(entry, struct cfq_io_context, list);
1208 kmem_cache_free(cfq_ioc_pool, __cic);
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 kmem_cache_free(cfq_ioc_pool, cic);
1212}
1213
1214/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001215 * Called with interrupts disabled
1216 */
1217static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1218{
Al Viro478a82b2006-03-18 13:25:24 -05001219 struct cfq_data *cfqd = cic->key;
Al Virod9ff4182006-03-18 13:51:22 -05001220 request_queue_t *q;
1221
1222 if (!cfqd)
1223 return;
1224
1225 q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001226
1227 WARN_ON(!irqs_disabled());
1228
1229 spin_lock(q->queue_lock);
1230
Al Viro12a05732006-03-18 13:38:01 -05001231 if (cic->cfqq[ASYNC]) {
1232 if (unlikely(cic->cfqq[ASYNC] == cfqd->active_queue))
1233 __cfq_slice_expired(cfqd, cic->cfqq[ASYNC], 0);
1234 cfq_put_queue(cic->cfqq[ASYNC]);
1235 cic->cfqq[ASYNC] = NULL;
1236 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001237
Al Viro12a05732006-03-18 13:38:01 -05001238 if (cic->cfqq[SYNC]) {
1239 if (unlikely(cic->cfqq[SYNC] == cfqd->active_queue))
1240 __cfq_slice_expired(cfqd, cic->cfqq[SYNC], 0);
1241 cfq_put_queue(cic->cfqq[SYNC]);
1242 cic->cfqq[SYNC] = NULL;
1243 }
1244
Al Viro478a82b2006-03-18 13:25:24 -05001245 cic->key = NULL;
Al Virod9ff4182006-03-18 13:51:22 -05001246 list_del_init(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001247 spin_unlock(q->queue_lock);
1248}
1249
1250/*
1251 * Another task may update the task cic list, if it is doing a queue lookup
1252 * on its behalf. cfq_cic_lock excludes such concurrent updates
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
1254static void cfq_exit_io_context(struct cfq_io_context *cic)
1255{
Jens Axboe22e2c502005-06-27 10:55:12 +02001256 struct cfq_io_context *__cic;
1257 struct list_head *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 unsigned long flags;
1259
Jens Axboe22e2c502005-06-27 10:55:12 +02001260 local_irq_save(flags);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /*
1263 * put the reference this task is holding to the various queues
1264 */
Al Virod9ff4182006-03-18 13:51:22 -05001265 read_lock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001266 list_for_each(entry, &cic->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 __cic = list_entry(entry, struct cfq_io_context, list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001268 cfq_exit_single_io_context(__cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
Jens Axboe22e2c502005-06-27 10:55:12 +02001271 cfq_exit_single_io_context(cic);
Al Virod9ff4182006-03-18 13:51:22 -05001272 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001273 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Jens Axboe22e2c502005-06-27 10:55:12 +02001276static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001277cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Jens Axboe22e2c502005-06-27 10:55:12 +02001279 struct cfq_io_context *cic = kmem_cache_alloc(cfq_ioc_pool, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (cic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 INIT_LIST_HEAD(&cic->list);
Al Viro12a05732006-03-18 13:38:01 -05001283 cic->cfqq[ASYNC] = NULL;
1284 cic->cfqq[SYNC] = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001285 cic->key = NULL;
1286 cic->last_end_request = jiffies;
1287 cic->ttime_total = 0;
1288 cic->ttime_samples = 0;
1289 cic->ttime_mean = 0;
1290 cic->dtor = cfq_free_io_context;
1291 cic->exit = cfq_exit_io_context;
Al Virod9ff4182006-03-18 13:51:22 -05001292 INIT_LIST_HEAD(&cic->queue_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295 return cic;
1296}
1297
Jens Axboe22e2c502005-06-27 10:55:12 +02001298static void cfq_init_prio_data(struct cfq_queue *cfqq)
1299{
1300 struct task_struct *tsk = current;
1301 int ioprio_class;
1302
Jens Axboe3b181522005-06-27 10:56:24 +02001303 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001304 return;
1305
1306 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1307 switch (ioprio_class) {
1308 default:
1309 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1310 case IOPRIO_CLASS_NONE:
1311 /*
1312 * no prio set, place us in the middle of the BE classes
1313 */
1314 cfqq->ioprio = task_nice_ioprio(tsk);
1315 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1316 break;
1317 case IOPRIO_CLASS_RT:
1318 cfqq->ioprio = task_ioprio(tsk);
1319 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1320 break;
1321 case IOPRIO_CLASS_BE:
1322 cfqq->ioprio = task_ioprio(tsk);
1323 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1324 break;
1325 case IOPRIO_CLASS_IDLE:
1326 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1327 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001328 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001329 break;
1330 }
1331
1332 /*
1333 * keep track of original prio settings in case we have to temporarily
1334 * elevate the priority of this queue
1335 */
1336 cfqq->org_ioprio = cfqq->ioprio;
1337 cfqq->org_ioprio_class = cfqq->ioprio_class;
1338
Jens Axboe3b181522005-06-27 10:56:24 +02001339 if (cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001340 cfq_resort_rr_list(cfqq, 0);
1341
Jens Axboe3b181522005-06-27 10:56:24 +02001342 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001343}
1344
Al Viro478a82b2006-03-18 13:25:24 -05001345static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001346{
Al Viro478a82b2006-03-18 13:25:24 -05001347 struct cfq_data *cfqd = cic->key;
1348 struct cfq_queue *cfqq;
1349 if (cfqd) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001350 spin_lock(cfqd->queue->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001351 cfqq = cic->cfqq[ASYNC];
1352 if (cfqq) {
1353 cfq_mark_cfqq_prio_changed(cfqq);
1354 cfq_init_prio_data(cfqq);
1355 }
1356 cfqq = cic->cfqq[SYNC];
Al Viro478a82b2006-03-18 13:25:24 -05001357 if (cfqq) {
1358 cfq_mark_cfqq_prio_changed(cfqq);
1359 cfq_init_prio_data(cfqq);
1360 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001361 spin_unlock(cfqd->queue->queue_lock);
1362 }
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001366 * callback from sys_ioprio_set, irqs are disabled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001368static int cfq_ioc_set_ioprio(struct io_context *ioc, unsigned int ioprio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Al Viroa6a07632006-03-18 13:26:44 -05001370 struct cfq_io_context *cic;
1371
1372 write_lock(&cfq_exit_lock);
1373
1374 cic = ioc->cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Al Viro478a82b2006-03-18 13:25:24 -05001376 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Jens Axboe22e2c502005-06-27 10:55:12 +02001378 list_for_each_entry(cic, &cic->list, list)
Al Viro478a82b2006-03-18 13:25:24 -05001379 changed_ioprio(cic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Al Viroa6a07632006-03-18 13:26:44 -05001381 write_unlock(&cfq_exit_lock);
1382
Jens Axboe22e2c502005-06-27 10:55:12 +02001383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001387cfq_get_queue(struct cfq_data *cfqd, unsigned int key, unsigned short ioprio,
Al Viro8267e262005-10-21 03:20:53 -04001388 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1391 struct cfq_queue *cfqq, *new_cfqq = NULL;
1392
1393retry:
Jens Axboe3b181522005-06-27 10:56:24 +02001394 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 if (!cfqq) {
1397 if (new_cfqq) {
1398 cfqq = new_cfqq;
1399 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001400 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 spin_unlock_irq(cfqd->queue->queue_lock);
1402 new_cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1403 spin_lock_irq(cfqd->queue->queue_lock);
1404 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001405 } else {
1406 cfqq = kmem_cache_alloc(cfq_pool, gfp_mask);
1407 if (!cfqq)
1408 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 memset(cfqq, 0, sizeof(*cfqq));
1412
1413 INIT_HLIST_NODE(&cfqq->cfq_hash);
1414 INIT_LIST_HEAD(&cfqq->cfq_list);
1415 RB_CLEAR_ROOT(&cfqq->sort_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001416 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 cfqq->key = key;
1419 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1420 atomic_set(&cfqq->ref, 0);
1421 cfqq->cfqd = cfqd;
1422 atomic_inc(&cfqd->ref);
Jens Axboe22e2c502005-06-27 10:55:12 +02001423 cfqq->service_last = 0;
1424 /*
1425 * set ->slice_left to allow preemption for a new process
1426 */
1427 cfqq->slice_left = 2 * cfqd->cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02001428 cfq_mark_cfqq_idle_window(cfqq);
1429 cfq_mark_cfqq_prio_changed(cfqq);
1430 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
1433 if (new_cfqq)
1434 kmem_cache_free(cfq_pool, new_cfqq);
1435
1436 atomic_inc(&cfqq->ref);
1437out:
1438 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1439 return cfqq;
1440}
1441
Jens Axboe22e2c502005-06-27 10:55:12 +02001442/*
1443 * Setup general io context and cfq io context. There can be several cfq
1444 * io contexts per general io context, if this process is doing io to more
1445 * than one device managed by cfq. Note that caller is holding a reference to
1446 * cfqq, so we don't need to worry about it disappearing
1447 */
1448static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001449cfq_get_io_context(struct cfq_data *cfqd, pid_t pid, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Jens Axboe22e2c502005-06-27 10:55:12 +02001451 struct io_context *ioc = NULL;
1452 struct cfq_io_context *cic;
1453
1454 might_sleep_if(gfp_mask & __GFP_WAIT);
1455
1456 ioc = get_io_context(gfp_mask);
1457 if (!ioc)
1458 return NULL;
1459
Al Virod9ff4182006-03-18 13:51:22 -05001460restart:
Jens Axboe22e2c502005-06-27 10:55:12 +02001461 if ((cic = ioc->cic) == NULL) {
1462 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1463
1464 if (cic == NULL)
1465 goto err;
1466
1467 /*
1468 * manually increment generic io_context usage count, it
1469 * cannot go away since we are already holding one ref to it
1470 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001471 cic->ioc = ioc;
1472 cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001473 read_lock(&cfq_exit_lock);
Al Viro478a82b2006-03-18 13:25:24 -05001474 ioc->set_ioprio = cfq_ioc_set_ioprio;
1475 ioc->cic = cic;
Al Virod9ff4182006-03-18 13:51:22 -05001476 list_add(&cic->queue_list, &cfqd->cic_list);
Al Viroa6a07632006-03-18 13:26:44 -05001477 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001478 } else {
1479 struct cfq_io_context *__cic;
1480
1481 /*
1482 * the first cic on the list is actually the head itself
1483 */
1484 if (cic->key == cfqd)
1485 goto out;
1486
Al Virod9ff4182006-03-18 13:51:22 -05001487 if (unlikely(!cic->key)) {
1488 read_lock(&cfq_exit_lock);
1489 if (list_empty(&cic->list))
1490 ioc->cic = NULL;
1491 else
1492 ioc->cic = list_entry(cic->list.next,
1493 struct cfq_io_context,
1494 list);
1495 read_unlock(&cfq_exit_lock);
1496 kmem_cache_free(cfq_ioc_pool, cic);
1497 goto restart;
1498 }
1499
Jens Axboe22e2c502005-06-27 10:55:12 +02001500 /*
1501 * cic exists, check if we already are there. linear search
1502 * should be ok here, the list will usually not be more than
1503 * 1 or a few entries long
1504 */
1505 list_for_each_entry(__cic, &cic->list, list) {
1506 /*
1507 * this process is already holding a reference to
1508 * this queue, so no need to get one more
1509 */
1510 if (__cic->key == cfqd) {
1511 cic = __cic;
1512 goto out;
1513 }
Al Virod9ff4182006-03-18 13:51:22 -05001514 if (unlikely(!__cic->key)) {
1515 read_lock(&cfq_exit_lock);
1516 list_del(&__cic->list);
1517 read_unlock(&cfq_exit_lock);
1518 kmem_cache_free(cfq_ioc_pool, __cic);
1519 goto restart;
1520 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001521 }
1522
1523 /*
1524 * nope, process doesn't have a cic assoicated with this
1525 * cfqq yet. get a new one and add to list
1526 */
1527 __cic = cfq_alloc_io_context(cfqd, gfp_mask);
1528 if (__cic == NULL)
1529 goto err;
1530
1531 __cic->ioc = ioc;
1532 __cic->key = cfqd;
Al Viroa6a07632006-03-18 13:26:44 -05001533 read_lock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001534 list_add(&__cic->list, &cic->list);
Al Virod9ff4182006-03-18 13:51:22 -05001535 list_add(&__cic->queue_list, &cfqd->cic_list);
Al Viroa6a07632006-03-18 13:26:44 -05001536 read_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02001537 cic = __cic;
1538 }
1539
1540out:
1541 return cic;
1542err:
1543 put_io_context(ioc);
1544 return NULL;
1545}
1546
1547static void
1548cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1549{
1550 unsigned long elapsed, ttime;
1551
1552 /*
1553 * if this context already has stuff queued, thinktime is from
1554 * last queue not last end
1555 */
1556#if 0
1557 if (time_after(cic->last_end_request, cic->last_queue))
1558 elapsed = jiffies - cic->last_end_request;
1559 else
1560 elapsed = jiffies - cic->last_queue;
1561#else
1562 elapsed = jiffies - cic->last_end_request;
1563#endif
1564
1565 ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
1566
1567 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1568 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1569 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1570}
1571
1572#define sample_valid(samples) ((samples) > 80)
1573
1574/*
1575 * Disable idle window if the process thinks too long or seeks so much that
1576 * it doesn't matter
1577 */
1578static void
1579cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1580 struct cfq_io_context *cic)
1581{
Jens Axboe3b181522005-06-27 10:56:24 +02001582 int enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001583
1584 if (!cic->ioc->task || !cfqd->cfq_slice_idle)
1585 enable_idle = 0;
1586 else if (sample_valid(cic->ttime_samples)) {
1587 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1588 enable_idle = 0;
1589 else
1590 enable_idle = 1;
1591 }
1592
Jens Axboe3b181522005-06-27 10:56:24 +02001593 if (enable_idle)
1594 cfq_mark_cfqq_idle_window(cfqq);
1595 else
1596 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001597}
1598
1599
1600/*
1601 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1602 * no or if we aren't sure, a 1 will cause a preempt.
1603 */
1604static int
1605cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
1606 struct cfq_rq *crq)
1607{
1608 struct cfq_queue *cfqq = cfqd->active_queue;
1609
1610 if (cfq_class_idle(new_cfqq))
1611 return 0;
1612
1613 if (!cfqq)
1614 return 1;
1615
1616 if (cfq_class_idle(cfqq))
1617 return 1;
Jens Axboe3b181522005-06-27 10:56:24 +02001618 if (!cfq_cfqq_wait_request(new_cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001619 return 0;
1620 /*
1621 * if it doesn't have slice left, forget it
1622 */
1623 if (new_cfqq->slice_left < cfqd->cfq_slice_idle)
1624 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001625 if (cfq_crq_is_sync(crq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001626 return 1;
1627
1628 return 0;
1629}
1630
1631/*
1632 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1633 * let it have half of its nominal slice.
1634 */
1635static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1636{
1637 struct cfq_queue *__cfqq, *next;
1638
1639 list_for_each_entry_safe(__cfqq, next, &cfqd->cur_rr, cfq_list)
1640 cfq_resort_rr_list(__cfqq, 1);
1641
1642 if (!cfqq->slice_left)
1643 cfqq->slice_left = cfq_prio_to_slice(cfqd, cfqq) / 2;
1644
1645 cfqq->slice_end = cfqq->slice_left + jiffies;
Jens Axboe3b181522005-06-27 10:56:24 +02001646 __cfq_slice_expired(cfqd, cfqq, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001647 __cfq_set_active_queue(cfqd, cfqq);
1648}
1649
1650/*
1651 * should really be a ll_rw_blk.c helper
1652 */
1653static void cfq_start_queueing(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1654{
1655 request_queue_t *q = cfqd->queue;
1656
1657 if (!blk_queue_plugged(q))
1658 q->request_fn(q);
1659 else
1660 __generic_unplug_device(q);
1661}
1662
1663/*
1664 * Called when a new fs request (crq) is added (to cfqq). Check if there's
1665 * something we should do about it
1666 */
1667static void
1668cfq_crq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1669 struct cfq_rq *crq)
1670{
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001671 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02001672
1673 cfqq->next_crq = cfq_choose_req(cfqd, cfqq->next_crq, crq);
1674
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001675 /*
1676 * we never wait for an async request and we don't allow preemption
1677 * of an async request. so just return early
1678 */
1679 if (!cfq_crq_is_sync(crq))
1680 return;
Jens Axboe22e2c502005-06-27 10:55:12 +02001681
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001682 cic = crq->io_context;
Jens Axboe22e2c502005-06-27 10:55:12 +02001683
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001684 cfq_update_io_thinktime(cfqd, cic);
1685 cfq_update_idle_window(cfqd, cfqq, cic);
1686
1687 cic->last_queue = jiffies;
Jens Axboe22e2c502005-06-27 10:55:12 +02001688
1689 if (cfqq == cfqd->active_queue) {
1690 /*
1691 * if we are waiting for a request for this queue, let it rip
1692 * immediately and flag that we must not expire this queue
1693 * just now
1694 */
Jens Axboe3b181522005-06-27 10:56:24 +02001695 if (cfq_cfqq_wait_request(cfqq)) {
1696 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001697 del_timer(&cfqd->idle_slice_timer);
1698 cfq_start_queueing(cfqd, cfqq);
1699 }
1700 } else if (cfq_should_preempt(cfqd, cfqq, crq)) {
1701 /*
1702 * not the active queue - expire current slice if it is
1703 * idle and has expired it's mean thinktime or this new queue
1704 * has some old slice time left and is of higher priority
1705 */
1706 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001707 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001708 cfq_start_queueing(cfqd, cfqq);
1709 }
1710}
1711
Jens Axboeb4878f22005-10-20 16:42:29 +02001712static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001713{
Jens Axboeb4878f22005-10-20 16:42:29 +02001714 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe22e2c502005-06-27 10:55:12 +02001715 struct cfq_rq *crq = RQ_DATA(rq);
1716 struct cfq_queue *cfqq = crq->cfq_queue;
1717
1718 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 cfq_add_crq_rb(crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Jens Axboe22e2c502005-06-27 10:55:12 +02001722 list_add_tail(&rq->queuelist, &cfqq->fifo);
1723
Tejun Heo98b11472005-10-20 16:46:54 +02001724 if (rq_mergeable(rq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001725 cfq_add_crq_hash(cfqd, crq);
1726
Jens Axboe22e2c502005-06-27 10:55:12 +02001727 cfq_crq_enqueued(cfqd, cfqq, crq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static void cfq_completed_request(request_queue_t *q, struct request *rq)
1731{
1732 struct cfq_rq *crq = RQ_DATA(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001733 struct cfq_queue *cfqq = crq->cfq_queue;
1734 struct cfq_data *cfqd = cfqq->cfqd;
1735 const int sync = cfq_crq_is_sync(crq);
1736 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Jens Axboeb4878f22005-10-20 16:42:29 +02001738 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Jens Axboeb4878f22005-10-20 16:42:29 +02001740 WARN_ON(!cfqd->rq_in_driver);
1741 WARN_ON(!cfqq->on_dispatch[sync]);
1742 cfqd->rq_in_driver--;
1743 cfqq->on_dispatch[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Jens Axboeb4878f22005-10-20 16:42:29 +02001745 if (!cfq_class_idle(cfqq))
1746 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001747
Jens Axboeb4878f22005-10-20 16:42:29 +02001748 if (!cfq_cfqq_dispatched(cfqq)) {
1749 if (cfq_cfqq_on_rr(cfqq)) {
1750 cfqq->service_last = now;
1751 cfq_resort_rr_list(cfqq, 0);
1752 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001753 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Jens Axboeb4878f22005-10-20 16:42:29 +02001756 if (cfq_crq_is_sync(crq))
1757 crq->io_context->last_end_request = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760static struct request *
1761cfq_former_request(request_queue_t *q, struct request *rq)
1762{
1763 struct cfq_rq *crq = RQ_DATA(rq);
1764 struct rb_node *rbprev = rb_prev(&crq->rb_node);
1765
1766 if (rbprev)
1767 return rb_entry_crq(rbprev)->request;
1768
1769 return NULL;
1770}
1771
1772static struct request *
1773cfq_latter_request(request_queue_t *q, struct request *rq)
1774{
1775 struct cfq_rq *crq = RQ_DATA(rq);
1776 struct rb_node *rbnext = rb_next(&crq->rb_node);
1777
1778 if (rbnext)
1779 return rb_entry_crq(rbnext)->request;
1780
1781 return NULL;
1782}
1783
Jens Axboe22e2c502005-06-27 10:55:12 +02001784/*
1785 * we temporarily boost lower priority queues if they are holding fs exclusive
1786 * resources. they are boosted to normal prio (CLASS_BE/4)
1787 */
1788static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Jens Axboe22e2c502005-06-27 10:55:12 +02001790 const int ioprio_class = cfqq->ioprio_class;
1791 const int ioprio = cfqq->ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jens Axboe22e2c502005-06-27 10:55:12 +02001793 if (has_fs_excl()) {
1794 /*
1795 * boost idle prio on transactions that would lock out other
1796 * users of the filesystem
1797 */
1798 if (cfq_class_idle(cfqq))
1799 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1800 if (cfqq->ioprio > IOPRIO_NORM)
1801 cfqq->ioprio = IOPRIO_NORM;
1802 } else {
1803 /*
1804 * check if we need to unboost the queue
1805 */
1806 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1807 cfqq->ioprio_class = cfqq->org_ioprio_class;
1808 if (cfqq->ioprio != cfqq->org_ioprio)
1809 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Jens Axboe22e2c502005-06-27 10:55:12 +02001812 /*
1813 * refile between round-robin lists if we moved the priority class
1814 */
1815 if ((ioprio_class != cfqq->ioprio_class || ioprio != cfqq->ioprio) &&
Jens Axboe3b181522005-06-27 10:56:24 +02001816 cfq_cfqq_on_rr(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001817 cfq_resort_rr_list(cfqq, 0);
1818}
1819
1820static inline pid_t cfq_queue_pid(struct task_struct *task, int rw)
1821{
1822 if (rw == READ || process_sync(task))
1823 return task->pid;
1824
1825 return CFQ_KEY_ASYNC;
1826}
1827
1828static inline int
1829__cfq_may_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1830 struct task_struct *task, int rw)
1831{
Jens Axboe3b181522005-06-27 10:56:24 +02001832#if 1
1833 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001834 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001835 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001836 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001837 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001838
1839 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001840#else
Jens Axboe22e2c502005-06-27 10:55:12 +02001841 if (!cfqq || task->flags & PF_MEMALLOC)
1842 return ELV_MQUEUE_MAY;
Jens Axboe3b181522005-06-27 10:56:24 +02001843 if (!cfqq->allocated[rw] || cfq_cfqq_must_alloc(cfqq)) {
1844 if (cfq_cfqq_wait_request(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001845 return ELV_MQUEUE_MUST;
1846
1847 /*
1848 * only allow 1 ELV_MQUEUE_MUST per slice, otherwise we
1849 * can quickly flood the queue with writes from a single task
1850 */
Andrew Morton99f95e52005-06-27 20:14:05 -07001851 if (rw == READ || !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001852 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001853 return ELV_MQUEUE_MUST;
1854 }
1855
1856 return ELV_MQUEUE_MAY;
1857 }
1858 if (cfq_class_idle(cfqq))
1859 return ELV_MQUEUE_NO;
1860 if (cfqq->allocated[rw] >= cfqd->max_queued) {
1861 struct io_context *ioc = get_io_context(GFP_ATOMIC);
1862 int ret = ELV_MQUEUE_NO;
1863
1864 if (ioc && ioc->nr_batch_requests)
1865 ret = ELV_MQUEUE_MAY;
1866
1867 put_io_context(ioc);
1868 return ret;
1869 }
1870
1871 return ELV_MQUEUE_MAY;
1872#endif
1873}
1874
1875static int cfq_may_queue(request_queue_t *q, int rw, struct bio *bio)
1876{
1877 struct cfq_data *cfqd = q->elevator->elevator_data;
1878 struct task_struct *tsk = current;
1879 struct cfq_queue *cfqq;
1880
1881 /*
1882 * don't force setup of a queue from here, as a call to may_queue
1883 * does not necessarily imply that a request actually will be queued.
1884 * so just lookup a possibly existing queue, or return 'may queue'
1885 * if that fails
1886 */
Jens Axboe3b181522005-06-27 10:56:24 +02001887 cfqq = cfq_find_cfq_hash(cfqd, cfq_queue_pid(tsk, rw), tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001888 if (cfqq) {
1889 cfq_init_prio_data(cfqq);
1890 cfq_prio_boost(cfqq);
1891
1892 return __cfq_may_queue(cfqd, cfqq, tsk, rw);
1893 }
1894
1895 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898static void cfq_check_waiters(request_queue_t *q, struct cfq_queue *cfqq)
1899{
Jens Axboe22e2c502005-06-27 10:55:12 +02001900 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct request_list *rl = &q->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Jens Axboe22e2c502005-06-27 10:55:12 +02001903 if (cfqq->allocated[READ] <= cfqd->max_queued || cfqd->rq_starved) {
1904 smp_mb();
1905 if (waitqueue_active(&rl->wait[READ]))
1906 wake_up(&rl->wait[READ]);
1907 }
1908
1909 if (cfqq->allocated[WRITE] <= cfqd->max_queued || cfqd->rq_starved) {
1910 smp_mb();
1911 if (waitqueue_active(&rl->wait[WRITE]))
1912 wake_up(&rl->wait[WRITE]);
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/*
1917 * queue lock held here
1918 */
1919static void cfq_put_request(request_queue_t *q, struct request *rq)
1920{
1921 struct cfq_data *cfqd = q->elevator->elevator_data;
1922 struct cfq_rq *crq = RQ_DATA(rq);
1923
1924 if (crq) {
1925 struct cfq_queue *cfqq = crq->cfq_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001926 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Jens Axboe22e2c502005-06-27 10:55:12 +02001928 BUG_ON(!cfqq->allocated[rw]);
1929 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Jens Axboe22e2c502005-06-27 10:55:12 +02001931 put_io_context(crq->io_context->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 mempool_free(crq, cfqd->crq_pool);
1934 rq->elevator_private = NULL;
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 cfq_check_waiters(q, cfqq);
1937 cfq_put_queue(cfqq);
1938 }
1939}
1940
1941/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001942 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001944static int
1945cfq_set_request(request_queue_t *q, struct request *rq, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04001946 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001949 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct cfq_io_context *cic;
1951 const int rw = rq_data_dir(rq);
Jens Axboe3b181522005-06-27 10:56:24 +02001952 pid_t key = cfq_queue_pid(tsk, rw);
Jens Axboe22e2c502005-06-27 10:55:12 +02001953 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct cfq_rq *crq;
1955 unsigned long flags;
Al Viro12a05732006-03-18 13:38:01 -05001956 int is_sync = key != CFQ_KEY_ASYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 might_sleep_if(gfp_mask & __GFP_WAIT);
1959
Jens Axboe3b181522005-06-27 10:56:24 +02001960 cic = cfq_get_io_context(cfqd, key, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 spin_lock_irqsave(q->queue_lock, flags);
1963
Jens Axboe22e2c502005-06-27 10:55:12 +02001964 if (!cic)
1965 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Al Viro12a05732006-03-18 13:38:01 -05001967 if (!cic->cfqq[is_sync]) {
Jens Axboe3b181522005-06-27 10:56:24 +02001968 cfqq = cfq_get_queue(cfqd, key, tsk->ioprio, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001969 if (!cfqq)
1970 goto queue_fail;
1971
Al Viro12a05732006-03-18 13:38:01 -05001972 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001973 } else
Al Viro12a05732006-03-18 13:38:01 -05001974 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001977 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001978 cfqd->rq_starved = 0;
1979 atomic_inc(&cfqq->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 spin_unlock_irqrestore(q->queue_lock, flags);
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 crq = mempool_alloc(cfqd->crq_pool, gfp_mask);
1983 if (crq) {
1984 RB_CLEAR(&crq->rb_node);
1985 crq->rb_key = 0;
1986 crq->request = rq;
1987 INIT_HLIST_NODE(&crq->hash);
1988 crq->cfq_queue = cfqq;
1989 crq->io_context = cic;
Jens Axboe3b181522005-06-27 10:56:24 +02001990
Al Viro12a05732006-03-18 13:38:01 -05001991 if (is_sync)
Jens Axboe3b181522005-06-27 10:56:24 +02001992 cfq_mark_crq_is_sync(crq);
1993 else
1994 cfq_clear_crq_is_sync(crq);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 rq->elevator_private = crq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return 0;
1998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 spin_lock_irqsave(q->queue_lock, flags);
2001 cfqq->allocated[rw]--;
Jens Axboe22e2c502005-06-27 10:55:12 +02002002 if (!(cfqq->allocated[0] + cfqq->allocated[1]))
Jens Axboe3b181522005-06-27 10:56:24 +02002003 cfq_mark_cfqq_must_alloc(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 cfq_put_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002005queue_fail:
2006 if (cic)
2007 put_io_context(cic->ioc);
2008 /*
2009 * mark us rq allocation starved. we need to kickstart the process
2010 * ourselves if there are no pending requests that can do it for us.
2011 * that would be an extremely rare OOM situation
2012 */
2013 cfqd->rq_starved = 1;
Jens Axboe3b181522005-06-27 10:56:24 +02002014 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 spin_unlock_irqrestore(q->queue_lock, flags);
2016 return 1;
2017}
2018
Jens Axboe22e2c502005-06-27 10:55:12 +02002019static void cfq_kick_queue(void *data)
2020{
2021 request_queue_t *q = data;
2022 struct cfq_data *cfqd = q->elevator->elevator_data;
2023 unsigned long flags;
2024
2025 spin_lock_irqsave(q->queue_lock, flags);
2026
2027 if (cfqd->rq_starved) {
2028 struct request_list *rl = &q->rq;
2029
2030 /*
2031 * we aren't guaranteed to get a request after this, but we
2032 * have to be opportunistic
2033 */
2034 smp_mb();
2035 if (waitqueue_active(&rl->wait[READ]))
2036 wake_up(&rl->wait[READ]);
2037 if (waitqueue_active(&rl->wait[WRITE]))
2038 wake_up(&rl->wait[WRITE]);
2039 }
2040
2041 blk_remove_plug(q);
2042 q->request_fn(q);
2043 spin_unlock_irqrestore(q->queue_lock, flags);
2044}
2045
2046/*
2047 * Timer running if the active_queue is currently idling inside its time slice
2048 */
2049static void cfq_idle_slice_timer(unsigned long data)
2050{
2051 struct cfq_data *cfqd = (struct cfq_data *) data;
2052 struct cfq_queue *cfqq;
2053 unsigned long flags;
2054
2055 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2056
2057 if ((cfqq = cfqd->active_queue) != NULL) {
2058 unsigned long now = jiffies;
2059
2060 /*
2061 * expired
2062 */
2063 if (time_after(now, cfqq->slice_end))
2064 goto expire;
2065
2066 /*
2067 * only expire and reinvoke request handler, if there are
2068 * other queues with pending requests
2069 */
Jens Axboeb4878f22005-10-20 16:42:29 +02002070 if (!cfqd->busy_queues) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002071 cfqd->idle_slice_timer.expires = min(now + cfqd->cfq_slice_idle, cfqq->slice_end);
2072 add_timer(&cfqd->idle_slice_timer);
2073 goto out_cont;
2074 }
2075
2076 /*
2077 * not expired and it has a request pending, let it dispatch
2078 */
2079 if (!RB_EMPTY(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002080 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002081 goto out_kick;
2082 }
2083 }
2084expire:
2085 cfq_slice_expired(cfqd, 0);
2086out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02002087 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002088out_cont:
2089 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2090}
2091
2092/*
2093 * Timer running if an idle class queue is waiting for service
2094 */
2095static void cfq_idle_class_timer(unsigned long data)
2096{
2097 struct cfq_data *cfqd = (struct cfq_data *) data;
2098 unsigned long flags, end;
2099
2100 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2101
2102 /*
2103 * race with a non-idle queue, reset timer
2104 */
2105 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
2106 if (!time_after_eq(jiffies, end)) {
2107 cfqd->idle_class_timer.expires = end;
2108 add_timer(&cfqd->idle_class_timer);
2109 } else
Jens Axboe3b181522005-06-27 10:56:24 +02002110 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002111
2112 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2113}
2114
Jens Axboe3b181522005-06-27 10:56:24 +02002115static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2116{
2117 del_timer_sync(&cfqd->idle_slice_timer);
2118 del_timer_sync(&cfqd->idle_class_timer);
2119 blk_sync_queue(cfqd->queue);
2120}
Jens Axboe22e2c502005-06-27 10:55:12 +02002121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122static void cfq_put_cfqd(struct cfq_data *cfqd)
2123{
2124 request_queue_t *q = cfqd->queue;
2125
2126 if (!atomic_dec_and_test(&cfqd->ref))
2127 return;
2128
Jens Axboe96c51ce2005-06-27 14:49:39 +02002129 cfq_shutdown_timer_wq(cfqd);
Jens Axboe4fc20742005-10-31 13:51:33 +01002130 blk_put_queue(q);
Jens Axboe96c51ce2005-06-27 14:49:39 +02002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 mempool_destroy(cfqd->crq_pool);
2133 kfree(cfqd->crq_hash);
2134 kfree(cfqd->cfq_hash);
2135 kfree(cfqd);
2136}
2137
2138static void cfq_exit_queue(elevator_t *e)
2139{
Jens Axboe22e2c502005-06-27 10:55:12 +02002140 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002141 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002142
Jens Axboe3b181522005-06-27 10:56:24 +02002143 cfq_shutdown_timer_wq(cfqd);
Al Virod9ff4182006-03-18 13:51:22 -05002144 write_lock(&cfq_exit_lock);
2145 spin_lock_irq(q->queue_lock);
2146 if (cfqd->active_queue)
2147 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
2148 while(!list_empty(&cfqd->cic_list)) {
2149 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2150 struct cfq_io_context,
2151 queue_list);
2152 if (cic->cfqq[ASYNC]) {
2153 cfq_put_queue(cic->cfqq[ASYNC]);
2154 cic->cfqq[ASYNC] = NULL;
2155 }
2156 if (cic->cfqq[SYNC]) {
2157 cfq_put_queue(cic->cfqq[SYNC]);
2158 cic->cfqq[SYNC] = NULL;
2159 }
2160 cic->key = NULL;
2161 list_del_init(&cic->queue_list);
2162 }
2163 spin_unlock_irq(q->queue_lock);
2164 write_unlock(&cfq_exit_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02002165 cfq_put_cfqd(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168static int cfq_init_queue(request_queue_t *q, elevator_t *e)
2169{
2170 struct cfq_data *cfqd;
2171 int i;
2172
2173 cfqd = kmalloc(sizeof(*cfqd), GFP_KERNEL);
2174 if (!cfqd)
2175 return -ENOMEM;
2176
2177 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002178
2179 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2180 INIT_LIST_HEAD(&cfqd->rr_list[i]);
2181
2182 INIT_LIST_HEAD(&cfqd->busy_rr);
2183 INIT_LIST_HEAD(&cfqd->cur_rr);
2184 INIT_LIST_HEAD(&cfqd->idle_rr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 INIT_LIST_HEAD(&cfqd->empty_list);
Al Virod9ff4182006-03-18 13:51:22 -05002186 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 cfqd->crq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_MHASH_ENTRIES, GFP_KERNEL);
2189 if (!cfqd->crq_hash)
2190 goto out_crqhash;
2191
2192 cfqd->cfq_hash = kmalloc(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL);
2193 if (!cfqd->cfq_hash)
2194 goto out_cfqhash;
2195
2196 cfqd->crq_pool = mempool_create(BLKDEV_MIN_RQ, mempool_alloc_slab, mempool_free_slab, crq_pool);
2197 if (!cfqd->crq_pool)
2198 goto out_crqpool;
2199
2200 for (i = 0; i < CFQ_MHASH_ENTRIES; i++)
2201 INIT_HLIST_HEAD(&cfqd->crq_hash[i]);
2202 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2203 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2204
2205 e->elevator_data = cfqd;
2206
2207 cfqd->queue = q;
Jens Axboe35797132005-09-10 14:17:10 +02002208 atomic_inc(&q->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Jens Axboe22e2c502005-06-27 10:55:12 +02002210 cfqd->max_queued = q->nr_requests / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 q->nr_batching = cfq_queued;
Jens Axboe22e2c502005-06-27 10:55:12 +02002212
2213 init_timer(&cfqd->idle_slice_timer);
2214 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2215 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2216
2217 init_timer(&cfqd->idle_class_timer);
2218 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2219 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2220
2221 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue, q);
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 atomic_set(&cfqd->ref, 1);
2224
2225 cfqd->cfq_queued = cfq_queued;
2226 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002227 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2228 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 cfqd->cfq_back_max = cfq_back_max;
2230 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002231 cfqd->cfq_slice[0] = cfq_slice_async;
2232 cfqd->cfq_slice[1] = cfq_slice_sync;
2233 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2234 cfqd->cfq_slice_idle = cfq_slice_idle;
2235 cfqd->cfq_max_depth = cfq_max_depth;
Jens Axboe3b181522005-06-27 10:56:24 +02002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return 0;
2238out_crqpool:
2239 kfree(cfqd->cfq_hash);
2240out_cfqhash:
2241 kfree(cfqd->crq_hash);
2242out_crqhash:
2243 kfree(cfqd);
2244 return -ENOMEM;
2245}
2246
2247static void cfq_slab_kill(void)
2248{
2249 if (crq_pool)
2250 kmem_cache_destroy(crq_pool);
2251 if (cfq_pool)
2252 kmem_cache_destroy(cfq_pool);
2253 if (cfq_ioc_pool)
2254 kmem_cache_destroy(cfq_ioc_pool);
2255}
2256
2257static int __init cfq_slab_setup(void)
2258{
2259 crq_pool = kmem_cache_create("crq_pool", sizeof(struct cfq_rq), 0, 0,
2260 NULL, NULL);
2261 if (!crq_pool)
2262 goto fail;
2263
2264 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2265 NULL, NULL);
2266 if (!cfq_pool)
2267 goto fail;
2268
2269 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2270 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2271 if (!cfq_ioc_pool)
2272 goto fail;
2273
2274 return 0;
2275fail:
2276 cfq_slab_kill();
2277 return -ENOMEM;
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/*
2281 * sysfs parts below -->
2282 */
2283struct cfq_fs_entry {
2284 struct attribute attr;
2285 ssize_t (*show)(struct cfq_data *, char *);
2286 ssize_t (*store)(struct cfq_data *, const char *, size_t);
2287};
2288
2289static ssize_t
2290cfq_var_show(unsigned int var, char *page)
2291{
2292 return sprintf(page, "%d\n", var);
2293}
2294
2295static ssize_t
2296cfq_var_store(unsigned int *var, const char *page, size_t count)
2297{
2298 char *p = (char *) page;
2299
2300 *var = simple_strtoul(p, &p, 10);
2301 return count;
2302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
2305static ssize_t __FUNC(struct cfq_data *cfqd, char *page) \
2306{ \
2307 unsigned int __data = __VAR; \
2308 if (__CONV) \
2309 __data = jiffies_to_msecs(__data); \
2310 return cfq_var_show(__data, (page)); \
2311}
2312SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
2313SHOW_FUNCTION(cfq_queued_show, cfqd->cfq_queued, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002314SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2315SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316SHOW_FUNCTION(cfq_back_max_show, cfqd->cfq_back_max, 0);
2317SHOW_FUNCTION(cfq_back_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002318SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2319SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2320SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2321SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
2322SHOW_FUNCTION(cfq_max_depth_show, cfqd->cfq_max_depth, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323#undef SHOW_FUNCTION
2324
2325#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
2326static ssize_t __FUNC(struct cfq_data *cfqd, const char *page, size_t count) \
2327{ \
2328 unsigned int __data; \
2329 int ret = cfq_var_store(&__data, (page), count); \
2330 if (__data < (MIN)) \
2331 __data = (MIN); \
2332 else if (__data > (MAX)) \
2333 __data = (MAX); \
2334 if (__CONV) \
2335 *(__PTR) = msecs_to_jiffies(__data); \
2336 else \
2337 *(__PTR) = __data; \
2338 return ret; \
2339}
2340STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
2341STORE_FUNCTION(cfq_queued_store, &cfqd->cfq_queued, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002342STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2343STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344STORE_FUNCTION(cfq_back_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2345STORE_FUNCTION(cfq_back_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002346STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2347STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2348STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2349STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
2350STORE_FUNCTION(cfq_max_depth_store, &cfqd->cfq_max_depth, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351#undef STORE_FUNCTION
2352
2353static struct cfq_fs_entry cfq_quantum_entry = {
2354 .attr = {.name = "quantum", .mode = S_IRUGO | S_IWUSR },
2355 .show = cfq_quantum_show,
2356 .store = cfq_quantum_store,
2357};
2358static struct cfq_fs_entry cfq_queued_entry = {
2359 .attr = {.name = "queued", .mode = S_IRUGO | S_IWUSR },
2360 .show = cfq_queued_show,
2361 .store = cfq_queued_store,
2362};
Jens Axboe22e2c502005-06-27 10:55:12 +02002363static struct cfq_fs_entry cfq_fifo_expire_sync_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 .attr = {.name = "fifo_expire_sync", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002365 .show = cfq_fifo_expire_sync_show,
2366 .store = cfq_fifo_expire_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367};
Jens Axboe22e2c502005-06-27 10:55:12 +02002368static struct cfq_fs_entry cfq_fifo_expire_async_entry = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 .attr = {.name = "fifo_expire_async", .mode = S_IRUGO | S_IWUSR },
Jens Axboe22e2c502005-06-27 10:55:12 +02002370 .show = cfq_fifo_expire_async_show,
2371 .store = cfq_fifo_expire_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372};
2373static struct cfq_fs_entry cfq_back_max_entry = {
2374 .attr = {.name = "back_seek_max", .mode = S_IRUGO | S_IWUSR },
2375 .show = cfq_back_max_show,
2376 .store = cfq_back_max_store,
2377};
2378static struct cfq_fs_entry cfq_back_penalty_entry = {
2379 .attr = {.name = "back_seek_penalty", .mode = S_IRUGO | S_IWUSR },
2380 .show = cfq_back_penalty_show,
2381 .store = cfq_back_penalty_store,
2382};
Jens Axboe22e2c502005-06-27 10:55:12 +02002383static struct cfq_fs_entry cfq_slice_sync_entry = {
2384 .attr = {.name = "slice_sync", .mode = S_IRUGO | S_IWUSR },
2385 .show = cfq_slice_sync_show,
2386 .store = cfq_slice_sync_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387};
Jens Axboe22e2c502005-06-27 10:55:12 +02002388static struct cfq_fs_entry cfq_slice_async_entry = {
2389 .attr = {.name = "slice_async", .mode = S_IRUGO | S_IWUSR },
2390 .show = cfq_slice_async_show,
2391 .store = cfq_slice_async_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392};
Jens Axboe22e2c502005-06-27 10:55:12 +02002393static struct cfq_fs_entry cfq_slice_async_rq_entry = {
2394 .attr = {.name = "slice_async_rq", .mode = S_IRUGO | S_IWUSR },
2395 .show = cfq_slice_async_rq_show,
2396 .store = cfq_slice_async_rq_store,
2397};
2398static struct cfq_fs_entry cfq_slice_idle_entry = {
2399 .attr = {.name = "slice_idle", .mode = S_IRUGO | S_IWUSR },
2400 .show = cfq_slice_idle_show,
2401 .store = cfq_slice_idle_store,
2402};
2403static struct cfq_fs_entry cfq_max_depth_entry = {
2404 .attr = {.name = "max_depth", .mode = S_IRUGO | S_IWUSR },
2405 .show = cfq_max_depth_show,
2406 .store = cfq_max_depth_store,
2407};
Jens Axboe3b181522005-06-27 10:56:24 +02002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409static struct attribute *default_attrs[] = {
2410 &cfq_quantum_entry.attr,
2411 &cfq_queued_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002412 &cfq_fifo_expire_sync_entry.attr,
2413 &cfq_fifo_expire_async_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 &cfq_back_max_entry.attr,
2415 &cfq_back_penalty_entry.attr,
Jens Axboe22e2c502005-06-27 10:55:12 +02002416 &cfq_slice_sync_entry.attr,
2417 &cfq_slice_async_entry.attr,
2418 &cfq_slice_async_rq_entry.attr,
2419 &cfq_slice_idle_entry.attr,
2420 &cfq_max_depth_entry.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 NULL,
2422};
2423
2424#define to_cfq(atr) container_of((atr), struct cfq_fs_entry, attr)
2425
2426static ssize_t
2427cfq_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2428{
2429 elevator_t *e = container_of(kobj, elevator_t, kobj);
2430 struct cfq_fs_entry *entry = to_cfq(attr);
2431
2432 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002433 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 return entry->show(e->elevator_data, page);
2436}
2437
2438static ssize_t
2439cfq_attr_store(struct kobject *kobj, struct attribute *attr,
2440 const char *page, size_t length)
2441{
2442 elevator_t *e = container_of(kobj, elevator_t, kobj);
2443 struct cfq_fs_entry *entry = to_cfq(attr);
2444
2445 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05002446 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 return entry->store(e->elevator_data, page, length);
2449}
2450
2451static struct sysfs_ops cfq_sysfs_ops = {
2452 .show = cfq_attr_show,
2453 .store = cfq_attr_store,
2454};
2455
2456static struct kobj_type cfq_ktype = {
2457 .sysfs_ops = &cfq_sysfs_ops,
2458 .default_attrs = default_attrs,
2459};
2460
2461static struct elevator_type iosched_cfq = {
2462 .ops = {
2463 .elevator_merge_fn = cfq_merge,
2464 .elevator_merged_fn = cfq_merged_request,
2465 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeb4878f22005-10-20 16:42:29 +02002466 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002468 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 .elevator_deactivate_req_fn = cfq_deactivate_request,
2470 .elevator_queue_empty_fn = cfq_queue_empty,
2471 .elevator_completed_req_fn = cfq_completed_request,
2472 .elevator_former_req_fn = cfq_former_request,
2473 .elevator_latter_req_fn = cfq_latter_request,
2474 .elevator_set_req_fn = cfq_set_request,
2475 .elevator_put_req_fn = cfq_put_request,
2476 .elevator_may_queue_fn = cfq_may_queue,
2477 .elevator_init_fn = cfq_init_queue,
2478 .elevator_exit_fn = cfq_exit_queue,
2479 },
2480 .elevator_ktype = &cfq_ktype,
2481 .elevator_name = "cfq",
2482 .elevator_owner = THIS_MODULE,
2483};
2484
2485static int __init cfq_init(void)
2486{
2487 int ret;
2488
Jens Axboe22e2c502005-06-27 10:55:12 +02002489 /*
2490 * could be 0 on HZ < 1000 setups
2491 */
2492 if (!cfq_slice_async)
2493 cfq_slice_async = 1;
2494 if (!cfq_slice_idle)
2495 cfq_slice_idle = 1;
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (cfq_slab_setup())
2498 return -ENOMEM;
2499
2500 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002501 if (ret)
2502 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return ret;
2505}
2506
2507static void __exit cfq_exit(void)
2508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 elv_unregister(&iosched_cfq);
Christoph Hellwig83521d32005-10-30 15:01:39 -08002510 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513module_init(cfq_init);
2514module_exit(cfq_exit);
2515
2516MODULE_AUTHOR("Jens Axboe");
2517MODULE_LICENSE("GPL");
2518MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");