blob: df82755ac40be58e4b4851ff9ef62edd79f05261 [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 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Al Viro1cc9be62006-03-18 12:29:52 -050010#include <linux/blkdev.h>
11#include <linux/elevator.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/hash.h>
13#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * tunables
18 */
Arjan van de Ven64100092006-01-06 09:46:02 +010019static const int cfq_quantum = 4; /* max queue in one round of service */
Arjan van de Ven64100092006-01-06 09:46:02 +010020static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
21static const int cfq_back_max = 16 * 1024; /* maximum backwards seek, in KiB */
22static const int cfq_back_penalty = 2; /* penalty of a backwards seek */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Arjan van de Ven64100092006-01-06 09:46:02 +010024static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020025static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020027static int cfq_slice_idle = HZ / 125;
Jens Axboe22e2c502005-06-27 10:55:12 +020028
Jens Axboed9e76202007-04-20 14:27:50 +020029/*
30 * grace period before allowing idle class to get disk access
31 */
Jens Axboe22e2c502005-06-27 10:55:12 +020032#define CFQ_IDLE_GRACE (HZ / 10)
Jens Axboed9e76202007-04-20 14:27:50 +020033
34/*
35 * below this threshold, we consider thinktime immediate
36 */
37#define CFQ_MIN_TT (2)
38
Jens Axboe22e2c502005-06-27 10:55:12 +020039#define CFQ_SLICE_SCALE (5)
40
41#define CFQ_KEY_ASYNC (0)
Jens Axboe22e2c502005-06-27 10:55:12 +020042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * for the hash of cfqq inside the cfqd
45 */
46#define CFQ_QHASH_SHIFT 6
47#define CFQ_QHASH_ENTRIES (1 << CFQ_QHASH_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jens Axboe5e705372006-07-13 12:39:25 +020049#define RQ_CIC(rq) ((struct cfq_io_context*)(rq)->elevator_private)
50#define RQ_CFQQ(rq) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Christoph Lametere18b8902006-12-06 20:33:20 -080052static struct kmem_cache *cfq_pool;
53static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jens Axboe4050cf12006-07-19 05:07:12 +020055static DEFINE_PER_CPU(unsigned long, ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050056static struct completion *ioc_gone;
57
Jens Axboe22e2c502005-06-27 10:55:12 +020058#define CFQ_PRIO_LISTS IOPRIO_BE_NR
59#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020060#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
61
Jens Axboe3b181522005-06-27 10:56:24 +020062#define ASYNC (0)
63#define SYNC (1)
64
Jens Axboe6d048f52007-04-25 12:44:27 +020065#define cfq_cfqq_sync(cfqq) ((cfqq)->key != CFQ_KEY_ASYNC)
Jens Axboe22e2c502005-06-27 10:55:12 +020066
Jens Axboe206dc692006-03-28 13:03:44 +020067#define sample_valid(samples) ((samples) > 80)
68
Jens Axboe22e2c502005-06-27 10:55:12 +020069/*
Jens Axboecc09e292007-04-26 12:53:50 +020070 * Most of our rbtree usage is for sorting with min extraction, so
71 * if we cache the leftmost node we don't have to walk down the tree
72 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
73 * move this into the elevator for the rq sorting as well.
74 */
75struct cfq_rb_root {
76 struct rb_root rb;
77 struct rb_node *left;
78};
79#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, }
80
81/*
Jens Axboe22e2c502005-06-27 10:55:12 +020082 * Per block device queue structure
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct cfq_data {
Jens Axboe22e2c502005-06-27 10:55:12 +020085 request_queue_t *queue;
86
87 /*
88 * rr list of queues with requests and the count of them
89 */
Jens Axboecc09e292007-04-26 12:53:50 +020090 struct cfq_rb_root service_tree;
Jens Axboe22e2c502005-06-27 10:55:12 +020091 unsigned int busy_queues;
92
93 /*
Jens Axboe22e2c502005-06-27 10:55:12 +020094 * cfqq lookup hash
95 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct hlist_head *cfq_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe22e2c502005-06-27 10:55:12 +020098 int rq_in_driver;
Jens Axboe25776e32006-06-01 10:12:26 +020099 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200100
101 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200102 * idle window management
103 */
104 struct timer_list idle_slice_timer;
105 struct work_struct unplug_work;
106
107 struct cfq_queue *active_queue;
108 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200109
110 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jens Axboe6d048f52007-04-25 12:44:27 +0200112 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200113 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /*
116 * tunables, see top of file
117 */
118 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200119 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unsigned int cfq_back_penalty;
121 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200122 unsigned int cfq_slice[2];
123 unsigned int cfq_slice_async_rq;
124 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500125
126 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200127
128 sector_t new_seek_mean;
129 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Jens Axboe22e2c502005-06-27 10:55:12 +0200132/*
133 * Per process-grouping structure
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135struct cfq_queue {
136 /* reference count */
137 atomic_t ref;
138 /* parent cfq_data */
139 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200140 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct hlist_node cfq_hash;
142 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200143 unsigned int key;
Jens Axboed9e76202007-04-20 14:27:50 +0200144 /* service_tree member */
145 struct rb_node rb_node;
146 /* service_tree key */
147 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* sorted list of pending requests */
149 struct rb_root sort_list;
150 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200151 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* requests queued in sort_list */
153 int queued[2];
154 /* currently allocated requests */
155 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200156 /* pending metadata requests */
157 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200159 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jens Axboe22e2c502005-06-27 10:55:12 +0200161 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100162 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jens Axboe6d048f52007-04-25 12:44:27 +0200164 /* number of requests that are on the dispatch list or inside driver */
165 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200166
167 /* io prio of this group */
168 unsigned short ioprio, org_ioprio;
169 unsigned short ioprio_class, org_ioprio_class;
170
Jens Axboe3b181522005-06-27 10:56:24 +0200171 /* various state flags, see below */
172 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200173
174 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Jens Axboe3b181522005-06-27 10:56:24 +0200177enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100178 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
179 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
180 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
181 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
182 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
183 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
184 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
185 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
186 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100187 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200188};
189
190#define CFQ_CFQQ_FNS(name) \
191static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
192{ \
193 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
194} \
195static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
196{ \
197 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
198} \
199static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
200{ \
201 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
202}
203
204CFQ_CFQQ_FNS(on_rr);
205CFQ_CFQQ_FNS(wait_request);
206CFQ_CFQQ_FNS(must_alloc);
207CFQ_CFQQ_FNS(must_alloc_slice);
208CFQ_CFQQ_FNS(must_dispatch);
209CFQ_CFQQ_FNS(fifo_expire);
210CFQ_CFQQ_FNS(idle_window);
211CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200212CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100213CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200214#undef CFQ_CFQQ_FNS
215
Jens Axboe3b181522005-06-27 10:56:24 +0200216static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200217static void cfq_dispatch_insert(request_queue_t *, struct request *);
Jens Axboe498d3aa2007-04-26 12:54:48 +0200218static struct cfq_queue *cfq_get_queue(struct cfq_data *, unsigned int, struct task_struct *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700221 * scheduler run of queue, if there are requests pending and no one in the
222 * driver that will restart queueing
223 */
224static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
225{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100226 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700227 kblockd_schedule_work(&cfqd->unplug_work);
228}
229
230static int cfq_queue_empty(request_queue_t *q)
231{
232 struct cfq_data *cfqd = q->elevator->elevator_data;
233
Jens Axboeb4878f22005-10-20 16:42:29 +0200234 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700235}
236
Jens Axboe7749a8d2006-12-13 13:02:26 +0100237static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200238{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100239 /*
240 * Use the per-process queue, for read requests and syncronous writes
241 */
242 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200243 return task->pid;
244
245 return CFQ_KEY_ASYNC;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100249 * Scale schedule slice based on io priority. Use the sync time slice only
250 * if a queue is marked sync and has sync io queued. A sync queue with async
251 * io only, should not get full sync slice length.
252 */
Jens Axboed9e76202007-04-20 14:27:50 +0200253static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
254 unsigned short prio)
255{
256 const int base_slice = cfqd->cfq_slice[sync];
257
258 WARN_ON(prio >= IOPRIO_BE_NR);
259
260 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
261}
262
Jens Axboe44f7c162007-01-19 11:51:58 +1100263static inline int
264cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
265{
Jens Axboed9e76202007-04-20 14:27:50 +0200266 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100267}
268
269static inline void
270cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
271{
272 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
273}
274
275/*
276 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
277 * isn't valid until the first request from the dispatch is activated
278 * and the slice time set.
279 */
280static inline int cfq_slice_used(struct cfq_queue *cfqq)
281{
282 if (cfq_cfqq_slice_new(cfqq))
283 return 0;
284 if (time_before(jiffies, cfqq->slice_end))
285 return 0;
286
287 return 1;
288}
289
290/*
Jens Axboe5e705372006-07-13 12:39:25 +0200291 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200293 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
Jens Axboe5e705372006-07-13 12:39:25 +0200295static struct request *
296cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200300#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
301#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
302 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jens Axboe5e705372006-07-13 12:39:25 +0200304 if (rq1 == NULL || rq1 == rq2)
305 return rq2;
306 if (rq2 == NULL)
307 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200308
Jens Axboe5e705372006-07-13 12:39:25 +0200309 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
310 return rq1;
311 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
312 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200313 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
314 return rq1;
315 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
316 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jens Axboe5e705372006-07-13 12:39:25 +0200318 s1 = rq1->sector;
319 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jens Axboe6d048f52007-04-25 12:44:27 +0200321 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * by definition, 1KiB is 2 sectors
325 */
326 back_max = cfqd->cfq_back_max * 2;
327
328 /*
329 * Strict one way elevator _except_ in the case where we allow
330 * short backward seeks which are biased as twice the cost of a
331 * similar forward seek.
332 */
333 if (s1 >= last)
334 d1 = s1 - last;
335 else if (s1 + back_max >= last)
336 d1 = (last - s1) * cfqd->cfq_back_penalty;
337 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200338 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (s2 >= last)
341 d2 = s2 - last;
342 else if (s2 + back_max >= last)
343 d2 = (last - s2) * cfqd->cfq_back_penalty;
344 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200345 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Andreas Mohre8a99052006-03-28 08:59:49 +0200349 /*
350 * By doing switch() on the bit mask "wrap" we avoid having to
351 * check two variables for all permutations: --> faster!
352 */
353 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200354 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200355 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200356 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200357 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200358 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200359 else {
360 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200361 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200362 else
Jens Axboe5e705372006-07-13 12:39:25 +0200363 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200364 }
365
366 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200367 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200368 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200369 return rq2;
370 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200371 default:
372 /*
373 * Since both rqs are wrapped,
374 * start with the one that's further behind head
375 * (--> only *one* back seek required),
376 * since back seek takes more time than forward.
377 */
378 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200379 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 else
Jens Axboe5e705372006-07-13 12:39:25 +0200381 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383}
384
Jens Axboe498d3aa2007-04-26 12:54:48 +0200385/*
386 * The below is leftmost cache rbtree addon
387 */
Jens Axboecc09e292007-04-26 12:53:50 +0200388static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
389{
390 if (!root->left)
391 root->left = rb_first(&root->rb);
392
393 return root->left;
394}
395
396static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
397{
398 if (root->left == n)
399 root->left = NULL;
400
401 rb_erase(n, &root->rb);
402 RB_CLEAR_NODE(n);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*
406 * would be nice to take fifo expire time into account as well
407 */
Jens Axboe5e705372006-07-13 12:39:25 +0200408static struct request *
409cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
410 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Jens Axboe21183b02006-07-13 12:33:14 +0200412 struct rb_node *rbnext = rb_next(&last->rb_node);
413 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200414 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jens Axboe21183b02006-07-13 12:33:14 +0200416 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200419 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200422 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200423 else {
424 rbnext = rb_first(&cfqq->sort_list);
425 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200426 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Jens Axboe21183b02006-07-13 12:33:14 +0200429 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Jens Axboed9e76202007-04-20 14:27:50 +0200432static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
433 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Jens Axboed9e76202007-04-20 14:27:50 +0200435 /*
436 * just an approximation, should be ok.
437 */
Jens Axboe67e6b492007-04-20 14:18:00 +0200438 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
439 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jens Axboe498d3aa2007-04-26 12:54:48 +0200442/*
443 * The cfqd->service_tree holds all pending cfq_queue's that have
444 * requests waiting to be processed. It is sorted in the order that
445 * we will service the queues.
446 */
Jens Axboed9e76202007-04-20 14:27:50 +0200447static void cfq_service_tree_add(struct cfq_data *cfqd,
Jens Axboeedd75ff2007-04-19 12:03:34 +0200448 struct cfq_queue *cfqq, int add_front)
Jens Axboed9e76202007-04-20 14:27:50 +0200449{
Jens Axboecc09e292007-04-26 12:53:50 +0200450 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200451 struct rb_node *parent = NULL;
Jens Axboed9e76202007-04-20 14:27:50 +0200452 unsigned long rb_key;
Jens Axboe498d3aa2007-04-26 12:54:48 +0200453 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200454
Jens Axboeedd75ff2007-04-19 12:03:34 +0200455 if (!add_front) {
456 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
457 rb_key += cfqq->slice_resid;
458 cfqq->slice_resid = 0;
459 } else
460 rb_key = 0;
Jens Axboed9e76202007-04-20 14:27:50 +0200461
462 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100463 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200464 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100465 */
Jens Axboed9e76202007-04-20 14:27:50 +0200466 if (rb_key == cfqq->rb_key)
467 return;
Jens Axboe53b03742006-07-28 09:48:51 +0200468
Jens Axboecc09e292007-04-26 12:53:50 +0200469 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200470 }
Jens Axboed9e76202007-04-20 14:27:50 +0200471
Jens Axboe498d3aa2007-04-26 12:54:48 +0200472 left = 1;
Jens Axboed9e76202007-04-20 14:27:50 +0200473 while (*p) {
Jens Axboecc09e292007-04-26 12:53:50 +0200474 struct cfq_queue *__cfqq;
Jens Axboe67060e32007-04-18 20:13:32 +0200475 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200476
Jens Axboed9e76202007-04-20 14:27:50 +0200477 parent = *p;
478 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
479
Jens Axboe0c534e02007-04-18 20:01:57 +0200480 /*
481 * sort RT queues first, we always want to give
Jens Axboe67060e32007-04-18 20:13:32 +0200482 * preference to them. IDLE queues goes to the back.
483 * after that, sort on the next service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200484 */
485 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200486 n = &(*p)->rb_left;
Jens Axboe0c534e02007-04-18 20:01:57 +0200487 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200488 n = &(*p)->rb_right;
489 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
490 n = &(*p)->rb_left;
491 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
492 n = &(*p)->rb_right;
Jens Axboe0c534e02007-04-18 20:01:57 +0200493 else if (rb_key < __cfqq->rb_key)
Jens Axboe67060e32007-04-18 20:13:32 +0200494 n = &(*p)->rb_left;
495 else
496 n = &(*p)->rb_right;
497
498 if (n == &(*p)->rb_right)
Jens Axboecc09e292007-04-26 12:53:50 +0200499 left = 0;
Jens Axboe67060e32007-04-18 20:13:32 +0200500
501 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200502 }
503
Jens Axboecc09e292007-04-26 12:53:50 +0200504 if (left)
505 cfqd->service_tree.left = &cfqq->rb_node;
506
Jens Axboed9e76202007-04-20 14:27:50 +0200507 cfqq->rb_key = rb_key;
508 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200509 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Jens Axboe498d3aa2007-04-26 12:54:48 +0200512/*
513 * Update cfqq's position in the service tree.
514 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200515static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200516{
Jens Axboe6d048f52007-04-25 12:44:27 +0200517 /*
518 * Resorting requires the cfqq to be on the RR list already.
519 */
Jens Axboe498d3aa2007-04-26 12:54:48 +0200520 if (cfq_cfqq_on_rr(cfqq))
Jens Axboeedd75ff2007-04-19 12:03:34 +0200521 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboe6d048f52007-04-25 12:44:27 +0200522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
525 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200526 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200529cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Jens Axboe3b181522005-06-27 10:56:24 +0200531 BUG_ON(cfq_cfqq_on_rr(cfqq));
532 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 cfqd->busy_queues++;
534
Jens Axboeedd75ff2007-04-19 12:03:34 +0200535 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Jens Axboe498d3aa2007-04-26 12:54:48 +0200538/*
539 * Called when the cfqq no longer has requests pending, remove it from
540 * the service tree.
541 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static inline void
543cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
544{
Jens Axboe3b181522005-06-27 10:56:24 +0200545 BUG_ON(!cfq_cfqq_on_rr(cfqq));
546 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Jens Axboecc09e292007-04-26 12:53:50 +0200548 if (!RB_EMPTY_NODE(&cfqq->rb_node))
549 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 BUG_ON(!cfqd->busy_queues);
552 cfqd->busy_queues--;
553}
554
555/*
556 * rb tree support functions
557 */
Jens Axboe5e705372006-07-13 12:39:25 +0200558static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Jens Axboe5e705372006-07-13 12:39:25 +0200560 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200561 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200562 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jens Axboeb4878f22005-10-20 16:42:29 +0200564 BUG_ON(!cfqq->queued[sync]);
565 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jens Axboe5e705372006-07-13 12:39:25 +0200567 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Jens Axboedd67d052006-06-21 09:36:18 +0200569 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200570 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Jens Axboe5e705372006-07-13 12:39:25 +0200573static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jens Axboe5e705372006-07-13 12:39:25 +0200575 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200577 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jens Axboe5380a102006-07-13 12:37:56 +0200579 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /*
582 * looks a little odd, but the first insert might return an alias.
583 * if that happens, put the alias on the dispatch list
584 */
Jens Axboe21183b02006-07-13 12:33:14 +0200585 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200586 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100587
588 if (!cfq_cfqq_on_rr(cfqq))
589 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200590
591 /*
592 * check if this request is a better next-serve candidate
593 */
594 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
595 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200599cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Jens Axboe5380a102006-07-13 12:37:56 +0200601 elv_rb_del(&cfqq->sort_list, rq);
602 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200603 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Jens Axboe206dc692006-03-28 13:03:44 +0200606static struct request *
607cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Jens Axboe206dc692006-03-28 13:03:44 +0200609 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100610 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200611 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Jens Axboe206dc692006-03-28 13:03:44 +0200613 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200614 if (cfqq) {
615 sector_t sector = bio->bi_sector + bio_sectors(bio);
616
Jens Axboe21183b02006-07-13 12:33:14 +0200617 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return NULL;
621}
622
Jens Axboeb4878f22005-10-20 16:42:29 +0200623static void cfq_activate_request(request_queue_t *q, struct request *rq)
624{
625 struct cfq_data *cfqd = q->elevator->elevator_data;
626
627 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200628
629 /*
630 * If the depth is larger 1, it really could be queueing. But lets
631 * make the mark a little higher - idling could still be good for
632 * low queueing, and a low queueing number could also just indicate
633 * a SCSI mid layer like behaviour where limit+1 is often seen.
634 */
635 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
636 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200637
638 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
642{
Jens Axboe22e2c502005-06-27 10:55:12 +0200643 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jens Axboeb4878f22005-10-20 16:42:29 +0200645 WARN_ON(!cfqd->rq_in_driver);
646 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Jens Axboeb4878f22005-10-20 16:42:29 +0200649static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Jens Axboe5e705372006-07-13 12:39:25 +0200651 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200652
Jens Axboe5e705372006-07-13 12:39:25 +0200653 if (cfqq->next_rq == rq)
654 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Jens Axboeb4878f22005-10-20 16:42:29 +0200656 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200657 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200658
659 if (rq_is_meta(rq)) {
660 WARN_ON(!cfqq->meta_pending);
661 cfqq->meta_pending--;
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Jens Axboe498d3aa2007-04-26 12:54:48 +0200665static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct cfq_data *cfqd = q->elevator->elevator_data;
668 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Jens Axboe206dc692006-03-28 13:03:44 +0200670 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200671 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200672 *req = __rq;
673 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Jens Axboe21183b02006-07-13 12:33:14 +0200679static void cfq_merged_request(request_queue_t *q, struct request *req,
680 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Jens Axboe21183b02006-07-13 12:33:14 +0200682 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200683 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Jens Axboe5e705372006-07-13 12:39:25 +0200685 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static void
690cfq_merged_requests(request_queue_t *q, struct request *rq,
691 struct request *next)
692{
Jens Axboe22e2c502005-06-27 10:55:12 +0200693 /*
694 * reposition in fifo if next is older than rq
695 */
696 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
697 time_before(next->start_time, rq->start_time))
698 list_move(&rq->queuelist, &next->queuelist);
699
Jens Axboeb4878f22005-10-20 16:42:29 +0200700 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200701}
702
Jens Axboeda775262006-12-20 11:04:12 +0100703static int cfq_allow_merge(request_queue_t *q, struct request *rq,
704 struct bio *bio)
705{
706 struct cfq_data *cfqd = q->elevator->elevator_data;
707 const int rw = bio_data_dir(bio);
708 struct cfq_queue *cfqq;
709 pid_t key;
710
711 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100712 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100713 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100714 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100715 return 0;
716
717 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100718 * Lookup the cfqq that this bio will be queued with. Allow
719 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100720 */
Jens Axboe719d3402006-12-22 09:38:53 +0100721 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100722 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100723
724 if (cfqq == RQ_CFQQ(rq))
725 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100726
Jens Axboeec8acb62007-01-02 18:32:11 +0100727 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100728}
729
Jens Axboe22e2c502005-06-27 10:55:12 +0200730static inline void
731__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
732{
733 if (cfqq) {
734 /*
735 * stop potential idle class queues waiting service
736 */
737 del_timer(&cfqd->idle_class_timer);
738
Jens Axboe22e2c502005-06-27 10:55:12 +0200739 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200740 cfq_clear_cfqq_must_alloc_slice(cfqq);
741 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100742 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200743 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200744 }
745
746 cfqd->active_queue = cfqq;
747}
748
749/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100750 * current cfqq expired its slice (or was too idle), select new one
751 */
752static void
753__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6084cdd2007-04-23 08:25:00 +0200754 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100755{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100756 if (cfq_cfqq_wait_request(cfqq))
757 del_timer(&cfqd->idle_slice_timer);
758
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100759 cfq_clear_cfqq_must_dispatch(cfqq);
760 cfq_clear_cfqq_wait_request(cfqq);
761
762 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +0200763 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100764 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100765 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100766 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100767
Jens Axboeedd75ff2007-04-19 12:03:34 +0200768 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100769
770 if (cfqq == cfqd->active_queue)
771 cfqd->active_queue = NULL;
772
773 if (cfqd->active_cic) {
774 put_io_context(cfqd->active_cic->ioc);
775 cfqd->active_cic = NULL;
776 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100777}
778
Jens Axboe6084cdd2007-04-23 08:25:00 +0200779static inline void cfq_slice_expired(struct cfq_data *cfqd, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100780{
781 struct cfq_queue *cfqq = cfqd->active_queue;
782
783 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +0200784 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100785}
786
Jens Axboe498d3aa2007-04-26 12:54:48 +0200787/*
788 * Get next queue for service. Unless we have a queue preemption,
789 * we'll simply select the first cfqq in the service tree.
790 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200791static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200792{
Jens Axboeedd75ff2007-04-19 12:03:34 +0200793 struct cfq_queue *cfqq;
794 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +0200795
Jens Axboeedd75ff2007-04-19 12:03:34 +0200796 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
797 return NULL;
798
799 n = cfq_rb_first(&cfqd->service_tree);
800 cfqq = rb_entry(n, struct cfq_queue, rb_node);
801
802 if (cfq_class_idle(cfqq)) {
803 unsigned long end;
804
Jens Axboe89850f72006-07-22 16:48:31 +0200805 /*
Jens Axboeedd75ff2007-04-19 12:03:34 +0200806 * if we have idle queues and no rt or be queues had
807 * pending requests, either allow immediate service if
808 * the grace period has passed or arm the idle grace
809 * timer
Jens Axboe89850f72006-07-22 16:48:31 +0200810 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200811 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
812 if (time_before(jiffies, end)) {
813 mod_timer(&cfqd->idle_class_timer, end);
814 cfqq = NULL;
Jens Axboe67060e32007-04-18 20:13:32 +0200815 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200816 }
817
Jens Axboe6d048f52007-04-25 12:44:27 +0200818 return cfqq;
819}
820
Jens Axboe498d3aa2007-04-26 12:54:48 +0200821/*
822 * Get and set a new active queue for service.
823 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200824static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
825{
826 struct cfq_queue *cfqq;
827
Jens Axboed9e76202007-04-20 14:27:50 +0200828 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200829 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200830 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200831}
832
Jens Axboed9e76202007-04-20 14:27:50 +0200833static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
834 struct request *rq)
835{
836 if (rq->sector >= cfqd->last_position)
837 return rq->sector - cfqd->last_position;
838 else
839 return cfqd->last_position - rq->sector;
840}
841
Jens Axboe6d048f52007-04-25 12:44:27 +0200842static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
843{
844 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200845
Jens Axboe6d048f52007-04-25 12:44:27 +0200846 if (!sample_valid(cic->seek_samples))
847 return 0;
848
849 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
850}
851
Jens Axboed9e76202007-04-20 14:27:50 +0200852static int cfq_close_cooperator(struct cfq_data *cfq_data,
853 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200854{
Jens Axboe6d048f52007-04-25 12:44:27 +0200855 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200856 * We should notice if some of the queues are cooperating, eg
857 * working closely on the same area of the disk. In that case,
858 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200859 */
Jens Axboed9e76202007-04-20 14:27:50 +0200860 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200861}
862
863#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
864
865static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200866{
Jens Axboe17926692007-01-19 11:59:30 +1100867 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200868 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100869 unsigned long sl;
870
Jens Axboedd67d052006-06-21 09:36:18 +0200871 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200872 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200873
874 /*
875 * idle is disabled, either manually or by past process history
876 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200877 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
878 return;
879
Jens Axboe22e2c502005-06-27 10:55:12 +0200880 /*
881 * task has exited, don't wait
882 */
Jens Axboe206dc692006-03-28 13:03:44 +0200883 cic = cfqd->active_cic;
884 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200885 return;
886
887 /*
888 * See if this prio level has a good candidate
889 */
Jens Axboe1afba042007-04-17 12:47:55 +0200890 if (cfq_close_cooperator(cfqd, cfqq) &&
891 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200892 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200893
Jens Axboe3b181522005-06-27 10:56:24 +0200894 cfq_mark_cfqq_must_dispatch(cfqq);
895 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200896
Jens Axboe206dc692006-03-28 13:03:44 +0200897 /*
898 * we don't want to idle for seeks, but we do want to allow
899 * fair distribution of slice time for a process doing back-to-back
900 * seeks. so allow a little bit of time for him to submit a new rq
901 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200902 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200903 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200904 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200905
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100906 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Jens Axboe498d3aa2007-04-26 12:54:48 +0200909/*
910 * Move request from internal lists to the request queue dispatch list.
911 */
Jens Axboe5e705372006-07-13 12:39:25 +0200912static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Jens Axboe5e705372006-07-13 12:39:25 +0200914 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200915
Jens Axboe5380a102006-07-13 12:37:56 +0200916 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200917 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200918 elv_dispatch_sort(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921/*
922 * return expired entry, or NULL to just start from scratch in rbtree
923 */
Jens Axboe5e705372006-07-13 12:39:25 +0200924static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200927 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200928 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jens Axboe3b181522005-06-27 10:56:24 +0200930 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100932
933 cfq_mark_cfqq_fifo_expire(cfqq);
934
Jens Axboe89850f72006-07-22 16:48:31 +0200935 if (list_empty(&cfqq->fifo))
936 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Jens Axboe6d048f52007-04-25 12:44:27 +0200938 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200939 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Jens Axboe6d048f52007-04-25 12:44:27 +0200941 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
942 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Jens Axboe6d048f52007-04-25 12:44:27 +0200944 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Jens Axboe22e2c502005-06-27 10:55:12 +0200947static inline int
948cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
949{
950 const int base_rq = cfqd->cfq_slice_async_rq;
951
952 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
953
954 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
955}
956
957/*
Jens Axboe498d3aa2007-04-26 12:54:48 +0200958 * Select a queue for service. If we have a current active queue,
959 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +0200960 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100961static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200962{
Jens Axboe22e2c502005-06-27 10:55:12 +0200963 struct cfq_queue *cfqq;
964
965 cfqq = cfqd->active_queue;
966 if (!cfqq)
967 goto new_queue;
968
969 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200970 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200971 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200972 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200973 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200974
975 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200976 * The active queue has requests and isn't expired, allow it to
977 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200978 */
Jens Axboedd67d052006-06-21 09:36:18 +0200979 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200980 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200981
982 /*
983 * No requests pending. If the active queue still has requests in
984 * flight or is idling for a new request, allow either of these
985 * conditions to happen (or time out) before selecting a new queue.
986 */
987 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +0200988 cfqq = NULL;
989 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200990 }
991
Jens Axboe3b181522005-06-27 10:56:24 +0200992expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +0200993 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200994new_queue:
995 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200996keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +0200997 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200998}
999
Jens Axboe498d3aa2007-04-26 12:54:48 +02001000/*
1001 * Dispatch some requests from cfqq, moving them to the request queue
1002 * dispatch list.
1003 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001004static int
1005__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1006 int max_dispatch)
1007{
1008 int dispatched = 0;
1009
Jens Axboedd67d052006-06-21 09:36:18 +02001010 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001011
1012 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001013 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001014
1015 /*
1016 * follow expired path, else get first next available
1017 */
Jens Axboe5e705372006-07-13 12:39:25 +02001018 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1019 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001020
1021 /*
1022 * finally, insert request into driver dispatch list
1023 */
Jens Axboe5e705372006-07-13 12:39:25 +02001024 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001025
Jens Axboe22e2c502005-06-27 10:55:12 +02001026 dispatched++;
1027
1028 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001029 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1030 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001031 }
1032
Jens Axboedd67d052006-06-21 09:36:18 +02001033 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001034 break;
1035
1036 } while (dispatched < max_dispatch);
1037
1038 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001039 * expire an async queue immediately if it has used up its slice. idle
1040 * queue always expire after 1 dispatch round.
1041 */
Jens Axboea9938002007-04-20 08:55:52 +02001042 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe20e493a2007-04-23 08:26:36 +02001043 dispatched >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001044 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001045 cfqq->slice_end = jiffies + 1;
Jens Axboe6084cdd2007-04-23 08:25:00 +02001046 cfq_slice_expired(cfqd, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001047 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001048
1049 return dispatched;
1050}
1051
Jens Axboed9e76202007-04-20 14:27:50 +02001052static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1053{
1054 int dispatched = 0;
1055
1056 while (cfqq->next_rq) {
1057 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1058 dispatched++;
1059 }
1060
1061 BUG_ON(!list_empty(&cfqq->fifo));
1062 return dispatched;
1063}
1064
Jens Axboe498d3aa2007-04-26 12:54:48 +02001065/*
1066 * Drain our current requests. Used for barriers and when switching
1067 * io schedulers on-the-fly.
1068 */
Jens Axboed9e76202007-04-20 14:27:50 +02001069static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001070{
Jens Axboed9e76202007-04-20 14:27:50 +02001071 int dispatched = 0;
1072 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001073
Jens Axboecc09e292007-04-26 12:53:50 +02001074 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
Jens Axboed9e76202007-04-20 14:27:50 +02001075 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1076
1077 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1078 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001079
Jens Axboe6084cdd2007-04-23 08:25:00 +02001080 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001081
1082 BUG_ON(cfqd->busy_queues);
1083
1084 return dispatched;
1085}
1086
Jens Axboed9e76202007-04-20 14:27:50 +02001087static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001090 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001091 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jens Axboe22e2c502005-06-27 10:55:12 +02001093 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return 0;
1095
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001096 if (unlikely(force))
1097 return cfq_forced_dispatch(cfqd);
1098
Jens Axboecaaa5f92006-06-16 11:23:00 +02001099 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001100 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001101 int max_dispatch;
1102
Jens Axboea9938002007-04-20 08:55:52 +02001103 if (cfqd->busy_queues > 1) {
1104 /*
Jens Axboea9938002007-04-20 08:55:52 +02001105 * So we have dispatched before in this round, if the
1106 * next queue has idling enabled (must be sync), don't
Jens Axboe6d048f52007-04-25 12:44:27 +02001107 * allow it service until the previous have completed.
Jens Axboea9938002007-04-20 08:55:52 +02001108 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001109 if (cfqd->rq_in_driver && cfq_cfqq_idle_window(cfqq) &&
1110 dispatched)
1111 break;
1112 if (cfqq->dispatched >= cfqd->cfq_quantum)
Jens Axboea9938002007-04-20 08:55:52 +02001113 break;
1114 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001115
Jens Axboe3b181522005-06-27 10:56:24 +02001116 cfq_clear_cfqq_must_dispatch(cfqq);
1117 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001118 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001120 max_dispatch = cfqd->cfq_quantum;
1121 if (cfq_class_idle(cfqq))
1122 max_dispatch = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jens Axboecaaa5f92006-06-16 11:23:00 +02001124 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
Jens Axboecaaa5f92006-06-16 11:23:00 +02001127 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
Jens Axboe5e705372006-07-13 12:39:25 +02001131 * task holds one reference to the queue, dropped when task exits. each rq
1132 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
1134 * queue lock must be held here.
1135 */
1136static void cfq_put_queue(struct cfq_queue *cfqq)
1137{
Jens Axboe22e2c502005-06-27 10:55:12 +02001138 struct cfq_data *cfqd = cfqq->cfqd;
1139
1140 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (!atomic_dec_and_test(&cfqq->ref))
1143 return;
1144
1145 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001146 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001147 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001149 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001150 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001151 cfq_schedule_dispatch(cfqd);
1152 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /*
1155 * it's on the empty list and still hashed
1156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 hlist_del(&cfqq->cfq_hash);
1158 kmem_cache_free(cfq_pool, cfqq);
1159}
1160
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001161static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001162__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1163 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001166 struct hlist_node *entry;
1167 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Jens Axboe206dc692006-03-28 13:03:44 +02001169 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001170 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Jens Axboe206dc692006-03-28 13:03:44 +02001172 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return __cfqq;
1174 }
1175
1176 return NULL;
1177}
1178
1179static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001180cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Jens Axboe3b181522005-06-27 10:56:24 +02001182 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Jens Axboee2d74ac2006-03-28 08:59:01 +02001185static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Jens Axboe22e2c502005-06-27 10:55:12 +02001187 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001188 struct rb_node *n;
1189 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001190
Jens Axboee2d74ac2006-03-28 08:59:01 +02001191 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1192 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1193 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001194 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001195 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001196 }
1197
Jens Axboe4050cf12006-07-19 05:07:12 +02001198 elv_ioc_count_mod(ioc_count, -freed);
1199
1200 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001201 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Jens Axboe89850f72006-07-22 16:48:31 +02001204static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1205{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001206 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001207 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001208 cfq_schedule_dispatch(cfqd);
1209 }
Jens Axboe89850f72006-07-22 16:48:31 +02001210
1211 cfq_put_queue(cfqq);
1212}
1213
1214static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1215 struct cfq_io_context *cic)
1216{
Jens Axboefc463792006-08-29 09:05:44 +02001217 list_del_init(&cic->queue_list);
1218 smp_wmb();
1219 cic->key = NULL;
1220
Jens Axboe89850f72006-07-22 16:48:31 +02001221 if (cic->cfqq[ASYNC]) {
1222 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1223 cic->cfqq[ASYNC] = NULL;
1224 }
1225
1226 if (cic->cfqq[SYNC]) {
1227 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1228 cic->cfqq[SYNC] = NULL;
1229 }
Jens Axboe89850f72006-07-22 16:48:31 +02001230}
1231
Jens Axboe22e2c502005-06-27 10:55:12 +02001232static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1233{
Al Viro478a82b2006-03-18 13:25:24 -05001234 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001235
Jens Axboe89850f72006-07-22 16:48:31 +02001236 if (cfqd) {
1237 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001238
Jens Axboefc463792006-08-29 09:05:44 +02001239 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001240 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001241 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001242 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001243}
1244
Jens Axboe498d3aa2007-04-26 12:54:48 +02001245/*
1246 * The process that ioc belongs to has exited, we need to clean up
1247 * and put the internal structures we have that belongs to that process.
1248 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001249static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jens Axboe22e2c502005-06-27 10:55:12 +02001251 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001252 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * put the reference this task is holding to the various queues
1256 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001257
1258 n = rb_first(&ioc->cic_root);
1259 while (n != NULL) {
1260 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1261
Jens Axboe22e2c502005-06-27 10:55:12 +02001262 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001263 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Jens Axboe22e2c502005-06-27 10:55:12 +02001267static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001268cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Jens Axboeb5deef92006-07-19 23:39:40 +02001270 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jens Axboeb5deef92006-07-19 23:39:40 +02001272 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001274 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001275 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001276 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001277 cic->dtor = cfq_free_io_context;
1278 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001279 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 return cic;
1283}
1284
Jens Axboe22e2c502005-06-27 10:55:12 +02001285static void cfq_init_prio_data(struct cfq_queue *cfqq)
1286{
1287 struct task_struct *tsk = current;
1288 int ioprio_class;
1289
Jens Axboe3b181522005-06-27 10:56:24 +02001290 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001291 return;
1292
1293 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1294 switch (ioprio_class) {
1295 default:
1296 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1297 case IOPRIO_CLASS_NONE:
1298 /*
1299 * no prio set, place us in the middle of the BE classes
1300 */
1301 cfqq->ioprio = task_nice_ioprio(tsk);
1302 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1303 break;
1304 case IOPRIO_CLASS_RT:
1305 cfqq->ioprio = task_ioprio(tsk);
1306 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1307 break;
1308 case IOPRIO_CLASS_BE:
1309 cfqq->ioprio = task_ioprio(tsk);
1310 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1311 break;
1312 case IOPRIO_CLASS_IDLE:
1313 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1314 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001315 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001316 break;
1317 }
1318
1319 /*
1320 * keep track of original prio settings in case we have to temporarily
1321 * elevate the priority of this queue
1322 */
1323 cfqq->org_ioprio = cfqq->ioprio;
1324 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001325 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001326}
1327
Al Viro478a82b2006-03-18 13:25:24 -05001328static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001329{
Al Viro478a82b2006-03-18 13:25:24 -05001330 struct cfq_data *cfqd = cic->key;
1331 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001332 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001333
Jens Axboecaaa5f92006-06-16 11:23:00 +02001334 if (unlikely(!cfqd))
1335 return;
1336
Jens Axboec1b707d2006-10-30 19:54:23 +01001337 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001338
1339 cfqq = cic->cfqq[ASYNC];
1340 if (cfqq) {
1341 struct cfq_queue *new_cfqq;
1342 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1343 GFP_ATOMIC);
1344 if (new_cfqq) {
1345 cic->cfqq[ASYNC] = new_cfqq;
1346 cfq_put_queue(cfqq);
1347 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001348 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001349
1350 cfqq = cic->cfqq[SYNC];
1351 if (cfqq)
1352 cfq_mark_cfqq_prio_changed(cfqq);
1353
Jens Axboec1b707d2006-10-30 19:54:23 +01001354 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001355}
1356
Jens Axboefc463792006-08-29 09:05:44 +02001357static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Al Viroa6a07632006-03-18 13:26:44 -05001359 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001360 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001361
Jens Axboefc463792006-08-29 09:05:44 +02001362 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001363
Jens Axboee2d74ac2006-03-28 08:59:01 +02001364 n = rb_first(&ioc->cic_root);
1365 while (n != NULL) {
1366 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001367
Al Viro478a82b2006-03-18 13:25:24 -05001368 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001369 n = rb_next(n);
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001374cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001375 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1378 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001379 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381retry:
Al Viro6f325a12006-03-18 14:58:37 -05001382 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001383 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (!cfqq) {
1386 if (new_cfqq) {
1387 cfqq = new_cfqq;
1388 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001389 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001390 /*
1391 * Inform the allocator of the fact that we will
1392 * just repeat this allocation if it fails, to allow
1393 * the allocator to do whatever it needs to attempt to
1394 * free memory.
1395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001397 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_lock_irq(cfqd->queue->queue_lock);
1399 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001400 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001401 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001402 if (!cfqq)
1403 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 memset(cfqq, 0, sizeof(*cfqq));
1407
1408 INIT_HLIST_NODE(&cfqq->cfq_hash);
Jens Axboed9e76202007-04-20 14:27:50 +02001409 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001410 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 cfqq->key = key;
1413 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1414 atomic_set(&cfqq->ref, 0);
1415 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001416
Jens Axboea9938002007-04-20 08:55:52 +02001417 if (key != CFQ_KEY_ASYNC)
1418 cfq_mark_cfqq_idle_window(cfqq);
1419
Jens Axboe3b181522005-06-27 10:56:24 +02001420 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001421 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001422 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 if (new_cfqq)
1426 kmem_cache_free(cfq_pool, new_cfqq);
1427
1428 atomic_inc(&cfqq->ref);
1429out:
1430 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1431 return cfqq;
1432}
1433
Jens Axboe498d3aa2007-04-26 12:54:48 +02001434/*
1435 * We drop cfq io contexts lazily, so we may find a dead one.
1436 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001437static void
1438cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1439{
Jens Axboefc463792006-08-29 09:05:44 +02001440 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001441 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001442 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001443 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001444}
1445
Jens Axboee2d74ac2006-03-28 08:59:01 +02001446static struct cfq_io_context *
1447cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1448{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001449 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001450 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001451 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001452
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001453restart:
1454 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001455 while (n) {
1456 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001457 /* ->key must be copied to avoid race with cfq_exit_queue() */
1458 k = cic->key;
1459 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001460 cfq_drop_dead_cic(ioc, cic);
1461 goto restart;
1462 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001463
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001464 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001465 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001466 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001467 n = n->rb_right;
1468 else
1469 return cic;
1470 }
1471
1472 return NULL;
1473}
1474
1475static inline void
1476cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1477 struct cfq_io_context *cic)
1478{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001479 struct rb_node **p;
1480 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001481 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001482 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001483 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001484
Jens Axboee2d74ac2006-03-28 08:59:01 +02001485 cic->ioc = ioc;
1486 cic->key = cfqd;
1487
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001488restart:
1489 parent = NULL;
1490 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001491 while (*p) {
1492 parent = *p;
1493 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001494 /* ->key must be copied to avoid race with cfq_exit_queue() */
1495 k = __cic->key;
1496 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001497 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001498 goto restart;
1499 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001500
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001501 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001502 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001503 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001504 p = &(*p)->rb_right;
1505 else
1506 BUG();
1507 }
1508
1509 rb_link_node(&cic->rb_node, parent, p);
1510 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001511
Jens Axboe0261d682006-10-30 19:07:48 +01001512 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001513 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001514 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001515}
1516
Jens Axboe22e2c502005-06-27 10:55:12 +02001517/*
1518 * Setup general io context and cfq io context. There can be several cfq
1519 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001520 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001521 */
1522static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001523cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Jens Axboe22e2c502005-06-27 10:55:12 +02001525 struct io_context *ioc = NULL;
1526 struct cfq_io_context *cic;
1527
1528 might_sleep_if(gfp_mask & __GFP_WAIT);
1529
Jens Axboeb5deef92006-07-19 23:39:40 +02001530 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001531 if (!ioc)
1532 return NULL;
1533
Jens Axboee2d74ac2006-03-28 08:59:01 +02001534 cic = cfq_cic_rb_lookup(cfqd, ioc);
1535 if (cic)
1536 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001537
Jens Axboee2d74ac2006-03-28 08:59:01 +02001538 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1539 if (cic == NULL)
1540 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001541
Jens Axboee2d74ac2006-03-28 08:59:01 +02001542 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001543out:
Jens Axboefc463792006-08-29 09:05:44 +02001544 smp_read_barrier_depends();
1545 if (unlikely(ioc->ioprio_changed))
1546 cfq_ioc_set_ioprio(ioc);
1547
Jens Axboe22e2c502005-06-27 10:55:12 +02001548 return cic;
1549err:
1550 put_io_context(ioc);
1551 return NULL;
1552}
1553
1554static void
1555cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1556{
Jens Axboeaaf12282007-01-19 11:30:16 +11001557 unsigned long elapsed = jiffies - cic->last_end_request;
1558 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001559
1560 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1561 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1562 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1563}
1564
Jens Axboe206dc692006-03-28 13:03:44 +02001565static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001566cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1567 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001568{
1569 sector_t sdist;
1570 u64 total;
1571
Jens Axboe5e705372006-07-13 12:39:25 +02001572 if (cic->last_request_pos < rq->sector)
1573 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001574 else
Jens Axboe5e705372006-07-13 12:39:25 +02001575 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001576
Jens Axboe6d048f52007-04-25 12:44:27 +02001577 if (!cic->seek_samples) {
1578 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1579 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1580 }
1581
Jens Axboe206dc692006-03-28 13:03:44 +02001582 /*
1583 * Don't allow the seek distance to get too large from the
1584 * odd fragment, pagein, etc
1585 */
1586 if (cic->seek_samples <= 60) /* second&third seek */
1587 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1588 else
1589 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1590
1591 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1592 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1593 total = cic->seek_total + (cic->seek_samples/2);
1594 do_div(total, cic->seek_samples);
1595 cic->seek_mean = (sector_t)total;
1596}
Jens Axboe22e2c502005-06-27 10:55:12 +02001597
1598/*
1599 * Disable idle window if the process thinks too long or seeks so much that
1600 * it doesn't matter
1601 */
1602static void
1603cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1604 struct cfq_io_context *cic)
1605{
Jens Axboe1be92f22007-04-19 14:32:26 +02001606 int enable_idle;
1607
1608 if (!cfq_cfqq_sync(cfqq))
1609 return;
1610
1611 enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001612
Jens Axboecaaa5f92006-06-16 11:23:00 +02001613 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1614 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001615 enable_idle = 0;
1616 else if (sample_valid(cic->ttime_samples)) {
1617 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1618 enable_idle = 0;
1619 else
1620 enable_idle = 1;
1621 }
1622
Jens Axboe3b181522005-06-27 10:56:24 +02001623 if (enable_idle)
1624 cfq_mark_cfqq_idle_window(cfqq);
1625 else
1626 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001627}
1628
Jens Axboe22e2c502005-06-27 10:55:12 +02001629/*
1630 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1631 * no or if we aren't sure, a 1 will cause a preempt.
1632 */
1633static int
1634cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001635 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001636{
Jens Axboe6d048f52007-04-25 12:44:27 +02001637 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001638
Jens Axboe6d048f52007-04-25 12:44:27 +02001639 cfqq = cfqd->active_queue;
1640 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001641 return 0;
1642
Jens Axboe6d048f52007-04-25 12:44:27 +02001643 if (cfq_slice_used(cfqq))
1644 return 1;
1645
1646 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001647 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001648
1649 if (cfq_class_idle(cfqq))
1650 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001651
Jens Axboe22e2c502005-06-27 10:55:12 +02001652 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001653 * if the new request is sync, but the currently running queue is
1654 * not, let the sync request have priority.
1655 */
Jens Axboe5e705372006-07-13 12:39:25 +02001656 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001657 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001658
Jens Axboe374f84a2006-07-23 01:42:19 +02001659 /*
1660 * So both queues are sync. Let the new request get disk time if
1661 * it's a metadata request and the current queue is doing regular IO.
1662 */
1663 if (rq_is_meta(rq) && !cfqq->meta_pending)
1664 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001665
Jens Axboe1e3335d2007-02-14 19:59:49 +01001666 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1667 return 0;
1668
1669 /*
1670 * if this request is as-good as one we would expect from the
1671 * current cfqq, let it preempt
1672 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001673 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001674 return 1;
1675
Jens Axboe22e2c502005-06-27 10:55:12 +02001676 return 0;
1677}
1678
1679/*
1680 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1681 * let it have half of its nominal slice.
1682 */
1683static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1684{
Jens Axboe6084cdd2007-04-23 08:25:00 +02001685 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001686
Jens Axboebf572252006-07-19 20:29:12 +02001687 /*
1688 * Put the new queue at the front of the of the current list,
1689 * so we know that it will be selected next.
1690 */
1691 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02001692
1693 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02001694
Jens Axboe44f7c162007-01-19 11:51:58 +11001695 cfqq->slice_end = 0;
1696 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001697}
1698
1699/*
Jens Axboe5e705372006-07-13 12:39:25 +02001700 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001701 * something we should do about it
1702 */
1703static void
Jens Axboe5e705372006-07-13 12:39:25 +02001704cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1705 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001706{
Jens Axboe5e705372006-07-13 12:39:25 +02001707 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001708
Jens Axboe374f84a2006-07-23 01:42:19 +02001709 if (rq_is_meta(rq))
1710 cfqq->meta_pending++;
1711
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001712 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001713 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001714 cfq_update_idle_window(cfqd, cfqq, cic);
1715
Jens Axboe5e705372006-07-13 12:39:25 +02001716 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001717 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001718
1719 if (cfqq == cfqd->active_queue) {
1720 /*
1721 * if we are waiting for a request for this queue, let it rip
1722 * immediately and flag that we must not expire this queue
1723 * just now
1724 */
Jens Axboe3b181522005-06-27 10:56:24 +02001725 if (cfq_cfqq_wait_request(cfqq)) {
1726 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001727 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001728 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001729 }
Jens Axboe5e705372006-07-13 12:39:25 +02001730 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001731 /*
1732 * not the active queue - expire current slice if it is
1733 * idle and has expired it's mean thinktime or this new queue
1734 * has some old slice time left and is of higher priority
1735 */
1736 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001737 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001738 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001739 }
1740}
1741
Jens Axboeb4878f22005-10-20 16:42:29 +02001742static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001743{
Jens Axboeb4878f22005-10-20 16:42:29 +02001744 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001745 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001746
1747 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Jens Axboe5e705372006-07-13 12:39:25 +02001749 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jens Axboe22e2c502005-06-27 10:55:12 +02001751 list_add_tail(&rq->queuelist, &cfqq->fifo);
1752
Jens Axboe5e705372006-07-13 12:39:25 +02001753 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756static void cfq_completed_request(request_queue_t *q, struct request *rq)
1757{
Jens Axboe5e705372006-07-13 12:39:25 +02001758 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001759 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001760 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001761 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Jens Axboeb4878f22005-10-20 16:42:29 +02001763 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Jens Axboeb4878f22005-10-20 16:42:29 +02001765 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001766 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001767 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001768 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Jens Axboeb4878f22005-10-20 16:42:29 +02001770 if (!cfq_class_idle(cfqq))
1771 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001772
Jens Axboecaaa5f92006-06-16 11:23:00 +02001773 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001774 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001775
1776 /*
1777 * If this is the active queue, check if it needs to be expired,
1778 * or if we want to idle in case it has no pending requests.
1779 */
1780 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001781 if (cfq_cfqq_slice_new(cfqq)) {
1782 cfq_set_prio_slice(cfqd, cfqq);
1783 cfq_clear_cfqq_slice_new(cfqq);
1784 }
1785 if (cfq_slice_used(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02001786 cfq_slice_expired(cfqd, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001787 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1788 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001789 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001790
1791 if (!cfqd->rq_in_driver)
1792 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Jens Axboe22e2c502005-06-27 10:55:12 +02001795/*
1796 * we temporarily boost lower priority queues if they are holding fs exclusive
1797 * resources. they are boosted to normal prio (CLASS_BE/4)
1798 */
1799static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Jens Axboe22e2c502005-06-27 10:55:12 +02001801 if (has_fs_excl()) {
1802 /*
1803 * boost idle prio on transactions that would lock out other
1804 * users of the filesystem
1805 */
1806 if (cfq_class_idle(cfqq))
1807 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1808 if (cfqq->ioprio > IOPRIO_NORM)
1809 cfqq->ioprio = IOPRIO_NORM;
1810 } else {
1811 /*
1812 * check if we need to unboost the queue
1813 */
1814 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1815 cfqq->ioprio_class = cfqq->org_ioprio_class;
1816 if (cfqq->ioprio != cfqq->org_ioprio)
1817 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001819}
1820
Jens Axboe89850f72006-07-22 16:48:31 +02001821static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001822{
Jens Axboe3b181522005-06-27 10:56:24 +02001823 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001824 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001825 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001826 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001827 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001828
1829 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001830}
1831
Jens Axboecb78b282006-07-28 09:32:57 +02001832static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001833{
1834 struct cfq_data *cfqd = q->elevator->elevator_data;
1835 struct task_struct *tsk = current;
1836 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001837 unsigned int key;
1838
1839 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001840
1841 /*
1842 * don't force setup of a queue from here, as a call to may_queue
1843 * does not necessarily imply that a request actually will be queued.
1844 * so just lookup a possibly existing queue, or return 'may queue'
1845 * if that fails
1846 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001847 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001848 if (cfqq) {
1849 cfq_init_prio_data(cfqq);
1850 cfq_prio_boost(cfqq);
1851
Jens Axboe89850f72006-07-22 16:48:31 +02001852 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001853 }
1854
1855 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
1859 * queue lock held here
1860 */
Jens Axboebb37b942006-12-01 10:42:33 +01001861static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Jens Axboe5e705372006-07-13 12:39:25 +02001863 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jens Axboe5e705372006-07-13 12:39:25 +02001865 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001866 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Jens Axboe22e2c502005-06-27 10:55:12 +02001868 BUG_ON(!cfqq->allocated[rw]);
1869 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Jens Axboe5e705372006-07-13 12:39:25 +02001871 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001874 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 cfq_put_queue(cfqq);
1877 }
1878}
1879
1880/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001881 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001883static int
Jens Axboecb78b282006-07-28 09:32:57 +02001884cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001887 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct cfq_io_context *cic;
1889 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001890 const int is_sync = rq_is_sync(rq);
1891 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001892 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned long flags;
1894
1895 might_sleep_if(gfp_mask & __GFP_WAIT);
1896
Jens Axboee2d74ac2006-03-28 08:59:01 +02001897 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 spin_lock_irqsave(q->queue_lock, flags);
1900
Jens Axboe22e2c502005-06-27 10:55:12 +02001901 if (!cic)
1902 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Al Viro12a05732006-03-18 13:38:01 -05001904 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001905 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001906 if (!cfqq)
1907 goto queue_fail;
1908
Al Viro12a05732006-03-18 13:38:01 -05001909 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001910 } else
Al Viro12a05732006-03-18 13:38:01 -05001911 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001914 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001915 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 spin_unlock_irqrestore(q->queue_lock, flags);
1918
Jens Axboe5e705372006-07-13 12:39:25 +02001919 rq->elevator_private = cic;
1920 rq->elevator_private2 = cfqq;
1921 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001922
Jens Axboe22e2c502005-06-27 10:55:12 +02001923queue_fail:
1924 if (cic)
1925 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001926
Jens Axboe3b181522005-06-27 10:56:24 +02001927 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 spin_unlock_irqrestore(q->queue_lock, flags);
1929 return 1;
1930}
1931
David Howells65f27f32006-11-22 14:55:48 +00001932static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001933{
David Howells65f27f32006-11-22 14:55:48 +00001934 struct cfq_data *cfqd =
1935 container_of(work, struct cfq_data, unplug_work);
1936 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001937 unsigned long flags;
1938
1939 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001940 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001941 spin_unlock_irqrestore(q->queue_lock, flags);
1942}
1943
1944/*
1945 * Timer running if the active_queue is currently idling inside its time slice
1946 */
1947static void cfq_idle_slice_timer(unsigned long data)
1948{
1949 struct cfq_data *cfqd = (struct cfq_data *) data;
1950 struct cfq_queue *cfqq;
1951 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001952 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001953
1954 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1955
1956 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001957 timed_out = 0;
1958
Jens Axboe22e2c502005-06-27 10:55:12 +02001959 /*
1960 * expired
1961 */
Jens Axboe44f7c162007-01-19 11:51:58 +11001962 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001963 goto expire;
1964
1965 /*
1966 * only expire and reinvoke request handler, if there are
1967 * other queues with pending requests
1968 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001969 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001970 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001971
1972 /*
1973 * not expired and it has a request pending, let it dispatch
1974 */
Jens Axboedd67d052006-06-21 09:36:18 +02001975 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001976 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001977 goto out_kick;
1978 }
1979 }
1980expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02001981 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02001982out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001983 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001984out_cont:
1985 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1986}
1987
1988/*
1989 * Timer running if an idle class queue is waiting for service
1990 */
1991static void cfq_idle_class_timer(unsigned long data)
1992{
1993 struct cfq_data *cfqd = (struct cfq_data *) data;
1994 unsigned long flags, end;
1995
1996 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1997
1998 /*
1999 * race with a non-idle queue, reset timer
2000 */
2001 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002002 if (!time_after_eq(jiffies, end))
2003 mod_timer(&cfqd->idle_class_timer, end);
2004 else
Jens Axboe3b181522005-06-27 10:56:24 +02002005 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002006
2007 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2008}
2009
Jens Axboe3b181522005-06-27 10:56:24 +02002010static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2011{
2012 del_timer_sync(&cfqd->idle_slice_timer);
2013 del_timer_sync(&cfqd->idle_class_timer);
2014 blk_sync_queue(cfqd->queue);
2015}
Jens Axboe22e2c502005-06-27 10:55:12 +02002016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017static void cfq_exit_queue(elevator_t *e)
2018{
Jens Axboe22e2c502005-06-27 10:55:12 +02002019 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002020 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002021
Jens Axboe3b181522005-06-27 10:56:24 +02002022 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002023
Al Virod9ff4182006-03-18 13:51:22 -05002024 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002025
Al Virod9ff4182006-03-18 13:51:22 -05002026 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02002027 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002028
2029 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002030 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2031 struct cfq_io_context,
2032 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002033
2034 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002035 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002036
Al Virod9ff4182006-03-18 13:51:22 -05002037 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002038
2039 cfq_shutdown_timer_wq(cfqd);
2040
Al Viroa90d7422006-03-18 12:05:37 -05002041 kfree(cfqd->cfq_hash);
2042 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Jens Axboebb37b942006-12-01 10:42:33 +01002045static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 struct cfq_data *cfqd;
2048 int i;
2049
Jens Axboeb5deef92006-07-19 23:39:40 +02002050 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002052 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002055
Jens Axboecc09e292007-04-26 12:53:50 +02002056 cfqd->service_tree = CFQ_RB_ROOT;
Al Virod9ff4182006-03-18 13:51:22 -05002057 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Jens Axboeb5deef92006-07-19 23:39:40 +02002059 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002061 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2064 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Jens Axboe22e2c502005-06-27 10:55:12 +02002068 init_timer(&cfqd->idle_slice_timer);
2069 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2070 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2071
2072 init_timer(&cfqd->idle_class_timer);
2073 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2074 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2075
David Howells65f27f32006-11-22 14:55:48 +00002076 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002079 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2080 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 cfqd->cfq_back_max = cfq_back_max;
2082 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002083 cfqd->cfq_slice[0] = cfq_slice_async;
2084 cfqd->cfq_slice[1] = cfq_slice_sync;
2085 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2086 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002087
Jens Axboebc1c1162006-06-08 08:49:06 +02002088 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002089out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002091 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094static void cfq_slab_kill(void)
2095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (cfq_pool)
2097 kmem_cache_destroy(cfq_pool);
2098 if (cfq_ioc_pool)
2099 kmem_cache_destroy(cfq_ioc_pool);
2100}
2101
2102static int __init cfq_slab_setup(void)
2103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2105 NULL, NULL);
2106 if (!cfq_pool)
2107 goto fail;
2108
2109 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2110 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2111 if (!cfq_ioc_pool)
2112 goto fail;
2113
2114 return 0;
2115fail:
2116 cfq_slab_kill();
2117 return -ENOMEM;
2118}
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120/*
2121 * sysfs parts below -->
2122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static ssize_t
2124cfq_var_show(unsigned int var, char *page)
2125{
2126 return sprintf(page, "%d\n", var);
2127}
2128
2129static ssize_t
2130cfq_var_store(unsigned int *var, const char *page, size_t count)
2131{
2132 char *p = (char *) page;
2133
2134 *var = simple_strtoul(p, &p, 10);
2135 return count;
2136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002139static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002141 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 unsigned int __data = __VAR; \
2143 if (__CONV) \
2144 __data = jiffies_to_msecs(__data); \
2145 return cfq_var_show(__data, (page)); \
2146}
2147SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002148SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2149SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002150SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2151SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002152SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2153SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2154SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2155SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#undef SHOW_FUNCTION
2157
2158#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002159static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002161 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 unsigned int __data; \
2163 int ret = cfq_var_store(&__data, (page), count); \
2164 if (__data < (MIN)) \
2165 __data = (MIN); \
2166 else if (__data > (MAX)) \
2167 __data = (MAX); \
2168 if (__CONV) \
2169 *(__PTR) = msecs_to_jiffies(__data); \
2170 else \
2171 *(__PTR) = __data; \
2172 return ret; \
2173}
2174STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002175STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2176STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002177STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2178STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002179STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2180STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2181STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2182STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183#undef STORE_FUNCTION
2184
Al Viroe572ec72006-03-18 22:27:18 -05002185#define CFQ_ATTR(name) \
2186 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002187
Al Viroe572ec72006-03-18 22:27:18 -05002188static struct elv_fs_entry cfq_attrs[] = {
2189 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002190 CFQ_ATTR(fifo_expire_sync),
2191 CFQ_ATTR(fifo_expire_async),
2192 CFQ_ATTR(back_seek_max),
2193 CFQ_ATTR(back_seek_penalty),
2194 CFQ_ATTR(slice_sync),
2195 CFQ_ATTR(slice_async),
2196 CFQ_ATTR(slice_async_rq),
2197 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002198 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199};
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static struct elevator_type iosched_cfq = {
2202 .ops = {
2203 .elevator_merge_fn = cfq_merge,
2204 .elevator_merged_fn = cfq_merged_request,
2205 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002206 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002207 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002209 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 .elevator_deactivate_req_fn = cfq_deactivate_request,
2211 .elevator_queue_empty_fn = cfq_queue_empty,
2212 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002213 .elevator_former_req_fn = elv_rb_former_request,
2214 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 .elevator_set_req_fn = cfq_set_request,
2216 .elevator_put_req_fn = cfq_put_request,
2217 .elevator_may_queue_fn = cfq_may_queue,
2218 .elevator_init_fn = cfq_init_queue,
2219 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002220 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 },
Al Viro3d1ab402006-03-18 18:35:43 -05002222 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 .elevator_name = "cfq",
2224 .elevator_owner = THIS_MODULE,
2225};
2226
2227static int __init cfq_init(void)
2228{
2229 int ret;
2230
Jens Axboe22e2c502005-06-27 10:55:12 +02002231 /*
2232 * could be 0 on HZ < 1000 setups
2233 */
2234 if (!cfq_slice_async)
2235 cfq_slice_async = 1;
2236 if (!cfq_slice_idle)
2237 cfq_slice_idle = 1;
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (cfq_slab_setup())
2240 return -ENOMEM;
2241
2242 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002243 if (ret)
2244 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return ret;
2247}
2248
2249static void __exit cfq_exit(void)
2250{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002251 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002253 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002254 /* ioc_gone's update must be visible before reading ioc_count */
2255 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002256 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002257 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002258 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002259 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262module_init(cfq_init);
2263module_exit(cfq_exit);
2264
2265MODULE_AUTHOR("Jens Axboe");
2266MODULE_LICENSE("GPL");
2267MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");