blob: a8237be97a28d12e6684c7a6744ea9f881253a78 [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 Axboe3ed9a292007-04-23 08:33:33 +020099 int sync_flight;
Jens Axboe25776e32006-06-01 10:12:26 +0200100 int hw_tag;
Jens Axboe22e2c502005-06-27 10:55:12 +0200101
102 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200103 * idle window management
104 */
105 struct timer_list idle_slice_timer;
106 struct work_struct unplug_work;
107
108 struct cfq_queue *active_queue;
109 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200110
111 struct timer_list idle_class_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Jens Axboe6d048f52007-04-25 12:44:27 +0200113 sector_t last_position;
Jens Axboe22e2c502005-06-27 10:55:12 +0200114 unsigned long last_end_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * tunables, see top of file
118 */
119 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200120 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned int cfq_back_penalty;
122 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200123 unsigned int cfq_slice[2];
124 unsigned int cfq_slice_async_rq;
125 unsigned int cfq_slice_idle;
Al Virod9ff4182006-03-18 13:51:22 -0500126
127 struct list_head cic_list;
Jens Axboe6d048f52007-04-25 12:44:27 +0200128
129 sector_t new_seek_mean;
130 u64 new_seek_total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Jens Axboe22e2c502005-06-27 10:55:12 +0200133/*
134 * Per process-grouping structure
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct cfq_queue {
137 /* reference count */
138 atomic_t ref;
139 /* parent cfq_data */
140 struct cfq_data *cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200141 /* cfqq lookup hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct hlist_node cfq_hash;
143 /* hash key */
Jens Axboe22e2c502005-06-27 10:55:12 +0200144 unsigned int key;
Jens Axboed9e76202007-04-20 14:27:50 +0200145 /* service_tree member */
146 struct rb_node rb_node;
147 /* service_tree key */
148 unsigned long rb_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* sorted list of pending requests */
150 struct rb_root sort_list;
151 /* if fifo isn't expired, next request to serve */
Jens Axboe5e705372006-07-13 12:39:25 +0200152 struct request *next_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* requests queued in sort_list */
154 int queued[2];
155 /* currently allocated requests */
156 int allocated[2];
Jens Axboe374f84a2006-07-23 01:42:19 +0200157 /* pending metadata requests */
158 int meta_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* fifo list of requests in sort_list */
Jens Axboe22e2c502005-06-27 10:55:12 +0200160 struct list_head fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jens Axboe22e2c502005-06-27 10:55:12 +0200162 unsigned long slice_end;
Jens Axboec5b680f2007-01-19 11:56:49 +1100163 long slice_resid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Jens Axboe6d048f52007-04-25 12:44:27 +0200165 /* number of requests that are on the dispatch list or inside driver */
166 int dispatched;
Jens Axboe22e2c502005-06-27 10:55:12 +0200167
168 /* io prio of this group */
169 unsigned short ioprio, org_ioprio;
170 unsigned short ioprio_class, org_ioprio_class;
171
Jens Axboe3b181522005-06-27 10:56:24 +0200172 /* various state flags, see below */
173 unsigned int flags;
Jens Axboe6d048f52007-04-25 12:44:27 +0200174
175 sector_t last_request_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Jens Axboe3b181522005-06-27 10:56:24 +0200178enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100179 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
180 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
181 CFQ_CFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
182 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
183 CFQ_CFQQ_FLAG_must_dispatch, /* must dispatch, even if expired */
184 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
185 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
186 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
187 CFQ_CFQQ_FLAG_queue_new, /* queue never been serviced */
Jens Axboe44f7c162007-01-19 11:51:58 +1100188 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Jens Axboe3b181522005-06-27 10:56:24 +0200189};
190
191#define CFQ_CFQQ_FNS(name) \
192static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
193{ \
194 cfqq->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
195} \
196static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
197{ \
198 cfqq->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
199} \
200static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
201{ \
202 return (cfqq->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
203}
204
205CFQ_CFQQ_FNS(on_rr);
206CFQ_CFQQ_FNS(wait_request);
207CFQ_CFQQ_FNS(must_alloc);
208CFQ_CFQQ_FNS(must_alloc_slice);
209CFQ_CFQQ_FNS(must_dispatch);
210CFQ_CFQQ_FNS(fifo_expire);
211CFQ_CFQQ_FNS(idle_window);
212CFQ_CFQQ_FNS(prio_changed);
Jens Axboe53b03742006-07-28 09:48:51 +0200213CFQ_CFQQ_FNS(queue_new);
Jens Axboe44f7c162007-01-19 11:51:58 +1100214CFQ_CFQQ_FNS(slice_new);
Jens Axboe3b181522005-06-27 10:56:24 +0200215#undef CFQ_CFQQ_FNS
216
Jens Axboe3b181522005-06-27 10:56:24 +0200217static struct cfq_queue *cfq_find_cfq_hash(struct cfq_data *, unsigned int, unsigned short);
Jens Axboe5e705372006-07-13 12:39:25 +0200218static void cfq_dispatch_insert(request_queue_t *, struct request *);
Jens Axboe498d3aa2007-04-26 12:54:48 +0200219static struct cfq_queue *cfq_get_queue(struct cfq_data *, unsigned int, struct task_struct *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700222 * scheduler run of queue, if there are requests pending and no one in the
223 * driver that will restart queueing
224 */
225static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
226{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100227 if (cfqd->busy_queues)
Andrew Morton99f95e52005-06-27 20:14:05 -0700228 kblockd_schedule_work(&cfqd->unplug_work);
229}
230
231static int cfq_queue_empty(request_queue_t *q)
232{
233 struct cfq_data *cfqd = q->elevator->elevator_data;
234
Jens Axboeb4878f22005-10-20 16:42:29 +0200235 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700236}
237
Jens Axboe7749a8d2006-12-13 13:02:26 +0100238static inline pid_t cfq_queue_pid(struct task_struct *task, int rw, int is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200239{
Jens Axboe7749a8d2006-12-13 13:02:26 +0100240 /*
241 * Use the per-process queue, for read requests and syncronous writes
242 */
243 if (!(rw & REQ_RW) || is_sync)
Jens Axboe206dc692006-03-28 13:03:44 +0200244 return task->pid;
245
246 return CFQ_KEY_ASYNC;
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100250 * Scale schedule slice based on io priority. Use the sync time slice only
251 * if a queue is marked sync and has sync io queued. A sync queue with async
252 * io only, should not get full sync slice length.
253 */
Jens Axboed9e76202007-04-20 14:27:50 +0200254static inline int cfq_prio_slice(struct cfq_data *cfqd, int sync,
255 unsigned short prio)
256{
257 const int base_slice = cfqd->cfq_slice[sync];
258
259 WARN_ON(prio >= IOPRIO_BE_NR);
260
261 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
262}
263
Jens Axboe44f7c162007-01-19 11:51:58 +1100264static inline int
265cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
266{
Jens Axboed9e76202007-04-20 14:27:50 +0200267 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100268}
269
270static inline void
271cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
272{
273 cfqq->slice_end = cfq_prio_to_slice(cfqd, cfqq) + jiffies;
274}
275
276/*
277 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
278 * isn't valid until the first request from the dispatch is activated
279 * and the slice time set.
280 */
281static inline int cfq_slice_used(struct cfq_queue *cfqq)
282{
283 if (cfq_cfqq_slice_new(cfqq))
284 return 0;
285 if (time_before(jiffies, cfqq->slice_end))
286 return 0;
287
288 return 1;
289}
290
291/*
Jens Axboe5e705372006-07-13 12:39:25 +0200292 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200294 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Jens Axboe5e705372006-07-13 12:39:25 +0200296static struct request *
297cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200301#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
302#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
303 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Jens Axboe5e705372006-07-13 12:39:25 +0200305 if (rq1 == NULL || rq1 == rq2)
306 return rq2;
307 if (rq2 == NULL)
308 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200309
Jens Axboe5e705372006-07-13 12:39:25 +0200310 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
311 return rq1;
312 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
313 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200314 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
315 return rq1;
316 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
317 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jens Axboe5e705372006-07-13 12:39:25 +0200319 s1 = rq1->sector;
320 s2 = rq2->sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jens Axboe6d048f52007-04-25 12:44:27 +0200322 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
325 * by definition, 1KiB is 2 sectors
326 */
327 back_max = cfqd->cfq_back_max * 2;
328
329 /*
330 * Strict one way elevator _except_ in the case where we allow
331 * short backward seeks which are biased as twice the cost of a
332 * similar forward seek.
333 */
334 if (s1 >= last)
335 d1 = s1 - last;
336 else if (s1 + back_max >= last)
337 d1 = (last - s1) * cfqd->cfq_back_penalty;
338 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200339 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (s2 >= last)
342 d2 = s2 - last;
343 else if (s2 + back_max >= last)
344 d2 = (last - s2) * cfqd->cfq_back_penalty;
345 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200346 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Andreas Mohre8a99052006-03-28 08:59:49 +0200350 /*
351 * By doing switch() on the bit mask "wrap" we avoid having to
352 * check two variables for all permutations: --> faster!
353 */
354 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200355 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200356 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200357 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200358 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200359 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200360 else {
361 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200362 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200363 else
Jens Axboe5e705372006-07-13 12:39:25 +0200364 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200365 }
366
367 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200368 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200369 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200370 return rq2;
371 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200372 default:
373 /*
374 * Since both rqs are wrapped,
375 * start with the one that's further behind head
376 * (--> only *one* back seek required),
377 * since back seek takes more time than forward.
378 */
379 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200380 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 else
Jens Axboe5e705372006-07-13 12:39:25 +0200382 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384}
385
Jens Axboe498d3aa2007-04-26 12:54:48 +0200386/*
387 * The below is leftmost cache rbtree addon
388 */
Jens Axboecc09e292007-04-26 12:53:50 +0200389static struct rb_node *cfq_rb_first(struct cfq_rb_root *root)
390{
391 if (!root->left)
392 root->left = rb_first(&root->rb);
393
394 return root->left;
395}
396
397static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
398{
399 if (root->left == n)
400 root->left = NULL;
401
402 rb_erase(n, &root->rb);
403 RB_CLEAR_NODE(n);
404}
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * would be nice to take fifo expire time into account as well
408 */
Jens Axboe5e705372006-07-13 12:39:25 +0200409static struct request *
410cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
411 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jens Axboe21183b02006-07-13 12:33:14 +0200413 struct rb_node *rbnext = rb_next(&last->rb_node);
414 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200415 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jens Axboe21183b02006-07-13 12:33:14 +0200417 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200420 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200423 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200424 else {
425 rbnext = rb_first(&cfqq->sort_list);
426 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200427 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Jens Axboe21183b02006-07-13 12:33:14 +0200430 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Jens Axboed9e76202007-04-20 14:27:50 +0200433static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
434 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Jens Axboed9e76202007-04-20 14:27:50 +0200436 /*
437 * just an approximation, should be ok.
438 */
Jens Axboe67e6b492007-04-20 14:18:00 +0200439 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
440 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jens Axboe498d3aa2007-04-26 12:54:48 +0200443/*
444 * The cfqd->service_tree holds all pending cfq_queue's that have
445 * requests waiting to be processed. It is sorted in the order that
446 * we will service the queues.
447 */
Jens Axboed9e76202007-04-20 14:27:50 +0200448static void cfq_service_tree_add(struct cfq_data *cfqd,
Jens Axboeedd75ff2007-04-19 12:03:34 +0200449 struct cfq_queue *cfqq, int add_front)
Jens Axboed9e76202007-04-20 14:27:50 +0200450{
Jens Axboecc09e292007-04-26 12:53:50 +0200451 struct rb_node **p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200452 struct rb_node *parent = NULL;
Jens Axboed9e76202007-04-20 14:27:50 +0200453 unsigned long rb_key;
Jens Axboe498d3aa2007-04-26 12:54:48 +0200454 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200455
Jens Axboeedd75ff2007-04-19 12:03:34 +0200456 if (!add_front) {
457 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
458 rb_key += cfqq->slice_resid;
459 cfqq->slice_resid = 0;
460 } else
461 rb_key = 0;
Jens Axboed9e76202007-04-20 14:27:50 +0200462
463 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100464 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200465 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100466 */
Jens Axboed9e76202007-04-20 14:27:50 +0200467 if (rb_key == cfqq->rb_key)
468 return;
Jens Axboe53b03742006-07-28 09:48:51 +0200469
Jens Axboecc09e292007-04-26 12:53:50 +0200470 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200471 }
Jens Axboed9e76202007-04-20 14:27:50 +0200472
Jens Axboe498d3aa2007-04-26 12:54:48 +0200473 left = 1;
Jens Axboed9e76202007-04-20 14:27:50 +0200474 while (*p) {
Jens Axboecc09e292007-04-26 12:53:50 +0200475 struct cfq_queue *__cfqq;
Jens Axboe67060e32007-04-18 20:13:32 +0200476 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200477
Jens Axboed9e76202007-04-20 14:27:50 +0200478 parent = *p;
479 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
480
Jens Axboe0c534e02007-04-18 20:01:57 +0200481 /*
482 * sort RT queues first, we always want to give
Jens Axboe67060e32007-04-18 20:13:32 +0200483 * preference to them. IDLE queues goes to the back.
484 * after that, sort on the next service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200485 */
486 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200487 n = &(*p)->rb_left;
Jens Axboe0c534e02007-04-18 20:01:57 +0200488 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200489 n = &(*p)->rb_right;
490 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
491 n = &(*p)->rb_left;
492 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
493 n = &(*p)->rb_right;
Jens Axboe0c534e02007-04-18 20:01:57 +0200494 else if (rb_key < __cfqq->rb_key)
Jens Axboe67060e32007-04-18 20:13:32 +0200495 n = &(*p)->rb_left;
496 else
497 n = &(*p)->rb_right;
498
499 if (n == &(*p)->rb_right)
Jens Axboecc09e292007-04-26 12:53:50 +0200500 left = 0;
Jens Axboe67060e32007-04-18 20:13:32 +0200501
502 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200503 }
504
Jens Axboecc09e292007-04-26 12:53:50 +0200505 if (left)
506 cfqd->service_tree.left = &cfqq->rb_node;
507
Jens Axboed9e76202007-04-20 14:27:50 +0200508 cfqq->rb_key = rb_key;
509 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200510 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Jens Axboe498d3aa2007-04-26 12:54:48 +0200513/*
514 * Update cfqq's position in the service tree.
515 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200516static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200517{
Jens Axboe6d048f52007-04-25 12:44:27 +0200518 /*
519 * Resorting requires the cfqq to be on the RR list already.
520 */
Jens Axboe498d3aa2007-04-26 12:54:48 +0200521 if (cfq_cfqq_on_rr(cfqq))
Jens Axboeedd75ff2007-04-19 12:03:34 +0200522 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboe6d048f52007-04-25 12:44:27 +0200523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
526 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200527 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529static inline void
Jens Axboeb4878f22005-10-20 16:42:29 +0200530cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Jens Axboe3b181522005-06-27 10:56:24 +0200532 BUG_ON(cfq_cfqq_on_rr(cfqq));
533 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 cfqd->busy_queues++;
535
Jens Axboeedd75ff2007-04-19 12:03:34 +0200536 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Jens Axboe498d3aa2007-04-26 12:54:48 +0200539/*
540 * Called when the cfqq no longer has requests pending, remove it from
541 * the service tree.
542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static inline void
544cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
545{
Jens Axboe3b181522005-06-27 10:56:24 +0200546 BUG_ON(!cfq_cfqq_on_rr(cfqq));
547 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Jens Axboecc09e292007-04-26 12:53:50 +0200549 if (!RB_EMPTY_NODE(&cfqq->rb_node))
550 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboed9e76202007-04-20 14:27:50 +0200551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 BUG_ON(!cfqd->busy_queues);
553 cfqd->busy_queues--;
554}
555
556/*
557 * rb tree support functions
558 */
Jens Axboe5e705372006-07-13 12:39:25 +0200559static inline void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jens Axboe5e705372006-07-13 12:39:25 +0200561 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200562 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200563 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Jens Axboeb4878f22005-10-20 16:42:29 +0200565 BUG_ON(!cfqq->queued[sync]);
566 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jens Axboe5e705372006-07-13 12:39:25 +0200568 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jens Axboedd67d052006-06-21 09:36:18 +0200570 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200571 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Jens Axboe5e705372006-07-13 12:39:25 +0200574static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jens Axboe5e705372006-07-13 12:39:25 +0200576 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe21183b02006-07-13 12:33:14 +0200578 struct request *__alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Jens Axboe5380a102006-07-13 12:37:56 +0200580 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /*
583 * looks a little odd, but the first insert might return an alias.
584 * if that happens, put the alias on the dispatch list
585 */
Jens Axboe21183b02006-07-13 12:33:14 +0200586 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200587 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100588
589 if (!cfq_cfqq_on_rr(cfqq))
590 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200591
592 /*
593 * check if this request is a better next-serve candidate
594 */
595 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
596 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static inline void
Jens Axboe5e705372006-07-13 12:39:25 +0200600cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Jens Axboe5380a102006-07-13 12:37:56 +0200602 elv_rb_del(&cfqq->sort_list, rq);
603 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200604 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Jens Axboe206dc692006-03-28 13:03:44 +0200607static struct request *
608cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Jens Axboe206dc692006-03-28 13:03:44 +0200610 struct task_struct *tsk = current;
Jens Axboe7749a8d2006-12-13 13:02:26 +0100611 pid_t key = cfq_queue_pid(tsk, bio_data_dir(bio), bio_sync(bio));
Jens Axboe206dc692006-03-28 13:03:44 +0200612 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Jens Axboe206dc692006-03-28 13:03:44 +0200614 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe89850f72006-07-22 16:48:31 +0200615 if (cfqq) {
616 sector_t sector = bio->bi_sector + bio_sectors(bio);
617
Jens Axboe21183b02006-07-13 12:33:14 +0200618 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return NULL;
622}
623
Jens Axboeb4878f22005-10-20 16:42:29 +0200624static void cfq_activate_request(request_queue_t *q, struct request *rq)
625{
626 struct cfq_data *cfqd = q->elevator->elevator_data;
627
628 cfqd->rq_in_driver++;
Jens Axboe25776e32006-06-01 10:12:26 +0200629
630 /*
631 * If the depth is larger 1, it really could be queueing. But lets
632 * make the mark a little higher - idling could still be good for
633 * low queueing, and a low queueing number could also just indicate
634 * a SCSI mid layer like behaviour where limit+1 is often seen.
635 */
636 if (!cfqd->hw_tag && cfqd->rq_in_driver > 4)
637 cfqd->hw_tag = 1;
Jens Axboe6d048f52007-04-25 12:44:27 +0200638
639 cfqd->last_position = rq->hard_sector + rq->hard_nr_sectors;
Jens Axboeb4878f22005-10-20 16:42:29 +0200640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static void cfq_deactivate_request(request_queue_t *q, struct request *rq)
643{
Jens Axboe22e2c502005-06-27 10:55:12 +0200644 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Jens Axboeb4878f22005-10-20 16:42:29 +0200646 WARN_ON(!cfqd->rq_in_driver);
647 cfqd->rq_in_driver--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Jens Axboeb4878f22005-10-20 16:42:29 +0200650static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Jens Axboe5e705372006-07-13 12:39:25 +0200652 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200653
Jens Axboe5e705372006-07-13 12:39:25 +0200654 if (cfqq->next_rq == rq)
655 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jens Axboeb4878f22005-10-20 16:42:29 +0200657 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200658 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200659
660 if (rq_is_meta(rq)) {
661 WARN_ON(!cfqq->meta_pending);
662 cfqq->meta_pending--;
663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Jens Axboe498d3aa2007-04-26 12:54:48 +0200666static int cfq_merge(request_queue_t *q, struct request **req, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct cfq_data *cfqd = q->elevator->elevator_data;
669 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Jens Axboe206dc692006-03-28 13:03:44 +0200671 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200672 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200673 *req = __rq;
674 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Jens Axboe21183b02006-07-13 12:33:14 +0200680static void cfq_merged_request(request_queue_t *q, struct request *req,
681 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Jens Axboe21183b02006-07-13 12:33:14 +0200683 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200684 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Jens Axboe5e705372006-07-13 12:39:25 +0200686 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static void
691cfq_merged_requests(request_queue_t *q, struct request *rq,
692 struct request *next)
693{
Jens Axboe22e2c502005-06-27 10:55:12 +0200694 /*
695 * reposition in fifo if next is older than rq
696 */
697 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
698 time_before(next->start_time, rq->start_time))
699 list_move(&rq->queuelist, &next->queuelist);
700
Jens Axboeb4878f22005-10-20 16:42:29 +0200701 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200702}
703
Jens Axboeda775262006-12-20 11:04:12 +0100704static int cfq_allow_merge(request_queue_t *q, struct request *rq,
705 struct bio *bio)
706{
707 struct cfq_data *cfqd = q->elevator->elevator_data;
708 const int rw = bio_data_dir(bio);
709 struct cfq_queue *cfqq;
710 pid_t key;
711
712 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100713 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100714 */
Jens Axboeec8acb62007-01-02 18:32:11 +0100715 if ((bio_data_dir(bio) == READ || bio_sync(bio)) && !rq_is_sync(rq))
Jens Axboeda775262006-12-20 11:04:12 +0100716 return 0;
717
718 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100719 * Lookup the cfqq that this bio will be queued with. Allow
720 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100721 */
Jens Axboe719d3402006-12-22 09:38:53 +0100722 key = cfq_queue_pid(current, rw, bio_sync(bio));
Jens Axboeda775262006-12-20 11:04:12 +0100723 cfqq = cfq_find_cfq_hash(cfqd, key, current->ioprio);
Jens Axboe719d3402006-12-22 09:38:53 +0100724
725 if (cfqq == RQ_CFQQ(rq))
726 return 1;
Jens Axboeda775262006-12-20 11:04:12 +0100727
Jens Axboeec8acb62007-01-02 18:32:11 +0100728 return 0;
Jens Axboeda775262006-12-20 11:04:12 +0100729}
730
Jens Axboe22e2c502005-06-27 10:55:12 +0200731static inline void
732__cfq_set_active_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
733{
734 if (cfqq) {
735 /*
736 * stop potential idle class queues waiting service
737 */
738 del_timer(&cfqd->idle_class_timer);
739
Jens Axboe22e2c502005-06-27 10:55:12 +0200740 cfqq->slice_end = 0;
Jens Axboe3b181522005-06-27 10:56:24 +0200741 cfq_clear_cfqq_must_alloc_slice(cfqq);
742 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100743 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe1afba042007-04-17 12:47:55 +0200744 cfq_clear_cfqq_queue_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200745 }
746
747 cfqd->active_queue = cfqq;
748}
749
750/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100751 * current cfqq expired its slice (or was too idle), select new one
752 */
753static void
754__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6084cdd2007-04-23 08:25:00 +0200755 int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100756{
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100757 if (cfq_cfqq_wait_request(cfqq))
758 del_timer(&cfqd->idle_slice_timer);
759
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100760 cfq_clear_cfqq_must_dispatch(cfqq);
761 cfq_clear_cfqq_wait_request(cfqq);
762
763 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +0200764 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100765 */
Jens Axboe3c6bd2f2007-01-19 12:06:33 +1100766 if (timed_out && !cfq_cfqq_slice_new(cfqq))
Jens Axboec5b680f2007-01-19 11:56:49 +1100767 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100768
Jens Axboeedd75ff2007-04-19 12:03:34 +0200769 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100770
771 if (cfqq == cfqd->active_queue)
772 cfqd->active_queue = NULL;
773
774 if (cfqd->active_cic) {
775 put_io_context(cfqd->active_cic->ioc);
776 cfqd->active_cic = NULL;
777 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100778}
779
Jens Axboe6084cdd2007-04-23 08:25:00 +0200780static inline void cfq_slice_expired(struct cfq_data *cfqd, int timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100781{
782 struct cfq_queue *cfqq = cfqd->active_queue;
783
784 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +0200785 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100786}
787
Jens Axboe498d3aa2007-04-26 12:54:48 +0200788/*
789 * Get next queue for service. Unless we have a queue preemption,
790 * we'll simply select the first cfqq in the service tree.
791 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200792static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200793{
Jens Axboeedd75ff2007-04-19 12:03:34 +0200794 struct cfq_queue *cfqq;
795 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +0200796
Jens Axboeedd75ff2007-04-19 12:03:34 +0200797 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
798 return NULL;
799
800 n = cfq_rb_first(&cfqd->service_tree);
801 cfqq = rb_entry(n, struct cfq_queue, rb_node);
802
803 if (cfq_class_idle(cfqq)) {
804 unsigned long end;
805
Jens Axboe89850f72006-07-22 16:48:31 +0200806 /*
Jens Axboeedd75ff2007-04-19 12:03:34 +0200807 * if we have idle queues and no rt or be queues had
808 * pending requests, either allow immediate service if
809 * the grace period has passed or arm the idle grace
810 * timer
Jens Axboe89850f72006-07-22 16:48:31 +0200811 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200812 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
813 if (time_before(jiffies, end)) {
814 mod_timer(&cfqd->idle_class_timer, end);
815 cfqq = NULL;
Jens Axboe67060e32007-04-18 20:13:32 +0200816 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200817 }
818
Jens Axboe6d048f52007-04-25 12:44:27 +0200819 return cfqq;
820}
821
Jens Axboe498d3aa2007-04-26 12:54:48 +0200822/*
823 * Get and set a new active queue for service.
824 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200825static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd)
826{
827 struct cfq_queue *cfqq;
828
Jens Axboed9e76202007-04-20 14:27:50 +0200829 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +0200830 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200831 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +0200832}
833
Jens Axboed9e76202007-04-20 14:27:50 +0200834static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
835 struct request *rq)
836{
837 if (rq->sector >= cfqd->last_position)
838 return rq->sector - cfqd->last_position;
839 else
840 return cfqd->last_position - rq->sector;
841}
842
Jens Axboe6d048f52007-04-25 12:44:27 +0200843static inline int cfq_rq_close(struct cfq_data *cfqd, struct request *rq)
844{
845 struct cfq_io_context *cic = cfqd->active_cic;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200846
Jens Axboe6d048f52007-04-25 12:44:27 +0200847 if (!sample_valid(cic->seek_samples))
848 return 0;
849
850 return cfq_dist_from_last(cfqd, rq) <= cic->seek_mean;
851}
852
Jens Axboed9e76202007-04-20 14:27:50 +0200853static int cfq_close_cooperator(struct cfq_data *cfq_data,
854 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200855{
Jens Axboe6d048f52007-04-25 12:44:27 +0200856 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200857 * We should notice if some of the queues are cooperating, eg
858 * working closely on the same area of the disk. In that case,
859 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +0200860 */
Jens Axboed9e76202007-04-20 14:27:50 +0200861 return 0;
Jens Axboe6d048f52007-04-25 12:44:27 +0200862}
863
864#define CIC_SEEKY(cic) ((cic)->seek_mean > (8 * 1024))
865
866static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200867{
Jens Axboe17926692007-01-19 11:59:30 +1100868 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +0200869 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100870 unsigned long sl;
871
Jens Axboedd67d052006-06-21 09:36:18 +0200872 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +0200873 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +0200874
875 /*
876 * idle is disabled, either manually or by past process history
877 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200878 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
879 return;
880
Jens Axboe22e2c502005-06-27 10:55:12 +0200881 /*
882 * task has exited, don't wait
883 */
Jens Axboe206dc692006-03-28 13:03:44 +0200884 cic = cfqd->active_cic;
885 if (!cic || !cic->ioc->task)
Jens Axboe6d048f52007-04-25 12:44:27 +0200886 return;
887
888 /*
889 * See if this prio level has a good candidate
890 */
Jens Axboe1afba042007-04-17 12:47:55 +0200891 if (cfq_close_cooperator(cfqd, cfqq) &&
892 (sample_valid(cic->ttime_samples) && cic->ttime_mean > 2))
Jens Axboe6d048f52007-04-25 12:44:27 +0200893 return;
Jens Axboe22e2c502005-06-27 10:55:12 +0200894
Jens Axboe3b181522005-06-27 10:56:24 +0200895 cfq_mark_cfqq_must_dispatch(cfqq);
896 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200897
Jens Axboe206dc692006-03-28 13:03:44 +0200898 /*
899 * we don't want to idle for seeks, but we do want to allow
900 * fair distribution of slice time for a process doing back-to-back
901 * seeks. so allow a little bit of time for him to submit a new rq
902 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200903 sl = cfqd->cfq_slice_idle;
Jens Axboecaaa5f92006-06-16 11:23:00 +0200904 if (sample_valid(cic->seek_samples) && CIC_SEEKY(cic))
Jens Axboed9e76202007-04-20 14:27:50 +0200905 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +0200906
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100907 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Jens Axboe498d3aa2007-04-26 12:54:48 +0200910/*
911 * Move request from internal lists to the request queue dispatch list.
912 */
Jens Axboe5e705372006-07-13 12:39:25 +0200913static void cfq_dispatch_insert(request_queue_t *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Jens Axboe3ed9a292007-04-23 08:33:33 +0200915 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +0200916 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200917
Jens Axboe5380a102006-07-13 12:37:56 +0200918 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +0200919 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +0200920 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +0200921
922 if (cfq_cfqq_sync(cfqq))
923 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/*
927 * return expired entry, or NULL to just start from scratch in rbtree
928 */
Jens Axboe5e705372006-07-13 12:39:25 +0200929static inline struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe22e2c502005-06-27 10:55:12 +0200932 struct request *rq;
Jens Axboe89850f72006-07-22 16:48:31 +0200933 int fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jens Axboe3b181522005-06-27 10:56:24 +0200935 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +1100937
938 cfq_mark_cfqq_fifo_expire(cfqq);
939
Jens Axboe89850f72006-07-22 16:48:31 +0200940 if (list_empty(&cfqq->fifo))
941 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jens Axboe6d048f52007-04-25 12:44:27 +0200943 fifo = cfq_cfqq_sync(cfqq);
Jens Axboe89850f72006-07-22 16:48:31 +0200944 rq = rq_entry_fifo(cfqq->fifo.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jens Axboe6d048f52007-04-25 12:44:27 +0200946 if (time_before(jiffies, rq->start_time + cfqd->cfq_fifo_expire[fifo]))
947 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jens Axboe6d048f52007-04-25 12:44:27 +0200949 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Jens Axboe22e2c502005-06-27 10:55:12 +0200952static inline int
953cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
954{
955 const int base_rq = cfqd->cfq_slice_async_rq;
956
957 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
958
959 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
960}
961
962/*
Jens Axboe498d3aa2007-04-26 12:54:48 +0200963 * Select a queue for service. If we have a current active queue,
964 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +0200965 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +0100966static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200967{
Jens Axboe22e2c502005-06-27 10:55:12 +0200968 struct cfq_queue *cfqq;
969
970 cfqq = cfqd->active_queue;
971 if (!cfqq)
972 goto new_queue;
973
974 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200975 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +0200976 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200977 if (cfq_slice_used(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +0200978 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +0200979
980 /*
Jens Axboe6d048f52007-04-25 12:44:27 +0200981 * The active queue has requests and isn't expired, allow it to
982 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +0200983 */
Jens Axboedd67d052006-06-21 09:36:18 +0200984 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +0200985 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +0200986
987 /*
988 * No requests pending. If the active queue still has requests in
989 * flight or is idling for a new request, allow either of these
990 * conditions to happen (or time out) before selecting a new queue.
991 */
992 if (cfqq->dispatched || timer_pending(&cfqd->idle_slice_timer)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +0200993 cfqq = NULL;
994 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200995 }
996
Jens Axboe3b181522005-06-27 10:56:24 +0200997expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +0200998 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +0200999new_queue:
1000 cfqq = cfq_set_active_queue(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001001keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001002 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001003}
1004
Jens Axboe498d3aa2007-04-26 12:54:48 +02001005/*
1006 * Dispatch some requests from cfqq, moving them to the request queue
1007 * dispatch list.
1008 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001009static int
1010__cfq_dispatch_requests(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1011 int max_dispatch)
1012{
1013 int dispatched = 0;
1014
Jens Axboedd67d052006-06-21 09:36:18 +02001015 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001016
1017 do {
Jens Axboe5e705372006-07-13 12:39:25 +02001018 struct request *rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001019
1020 /*
1021 * follow expired path, else get first next available
1022 */
Jens Axboe5e705372006-07-13 12:39:25 +02001023 if ((rq = cfq_check_fifo(cfqq)) == NULL)
1024 rq = cfqq->next_rq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001025
1026 /*
1027 * finally, insert request into driver dispatch list
1028 */
Jens Axboe5e705372006-07-13 12:39:25 +02001029 cfq_dispatch_insert(cfqd->queue, rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001030
Jens Axboe22e2c502005-06-27 10:55:12 +02001031 dispatched++;
1032
1033 if (!cfqd->active_cic) {
Jens Axboe5e705372006-07-13 12:39:25 +02001034 atomic_inc(&RQ_CIC(rq)->ioc->refcount);
1035 cfqd->active_cic = RQ_CIC(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001036 }
1037
Jens Axboedd67d052006-06-21 09:36:18 +02001038 if (RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001039 break;
1040
1041 } while (dispatched < max_dispatch);
1042
1043 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001044 * expire an async queue immediately if it has used up its slice. idle
1045 * queue always expire after 1 dispatch round.
1046 */
Jens Axboea9938002007-04-20 08:55:52 +02001047 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
Jens Axboe20e493a2007-04-23 08:26:36 +02001048 dispatched >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
Jens Axboea9938002007-04-20 08:55:52 +02001049 cfq_class_idle(cfqq))) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001050 cfqq->slice_end = jiffies + 1;
Jens Axboe6084cdd2007-04-23 08:25:00 +02001051 cfq_slice_expired(cfqd, 0);
Jens Axboe44f7c162007-01-19 11:51:58 +11001052 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001053
1054 return dispatched;
1055}
1056
Jens Axboed9e76202007-04-20 14:27:50 +02001057static inline int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
1058{
1059 int dispatched = 0;
1060
1061 while (cfqq->next_rq) {
1062 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1063 dispatched++;
1064 }
1065
1066 BUG_ON(!list_empty(&cfqq->fifo));
1067 return dispatched;
1068}
1069
Jens Axboe498d3aa2007-04-26 12:54:48 +02001070/*
1071 * Drain our current requests. Used for barriers and when switching
1072 * io schedulers on-the-fly.
1073 */
Jens Axboed9e76202007-04-20 14:27:50 +02001074static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001075{
Jens Axboed9e76202007-04-20 14:27:50 +02001076 int dispatched = 0;
1077 struct rb_node *n;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001078
Jens Axboecc09e292007-04-26 12:53:50 +02001079 while ((n = cfq_rb_first(&cfqd->service_tree)) != NULL) {
Jens Axboed9e76202007-04-20 14:27:50 +02001080 struct cfq_queue *cfqq = rb_entry(n, struct cfq_queue, rb_node);
1081
1082 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
1083 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001084
Jens Axboe6084cdd2007-04-23 08:25:00 +02001085 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001086
1087 BUG_ON(cfqd->busy_queues);
1088
1089 return dispatched;
1090}
1091
Jens Axboed9e76202007-04-20 14:27:50 +02001092static int cfq_dispatch_requests(request_queue_t *q, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe6d048f52007-04-25 12:44:27 +02001095 struct cfq_queue *cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001096 int dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jens Axboe22e2c502005-06-27 10:55:12 +02001098 if (!cfqd->busy_queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
1100
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001101 if (unlikely(force))
1102 return cfq_forced_dispatch(cfqd);
1103
Jens Axboecaaa5f92006-06-16 11:23:00 +02001104 dispatched = 0;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001105 while ((cfqq = cfq_select_queue(cfqd)) != NULL) {
Jens Axboeb4878f22005-10-20 16:42:29 +02001106 int max_dispatch;
1107
Jens Axboe3ed9a292007-04-23 08:33:33 +02001108 max_dispatch = cfqd->cfq_quantum;
1109 if (cfq_class_idle(cfqq))
1110 max_dispatch = 1;
1111
1112 if (cfqq->dispatched >= max_dispatch) {
1113 if (cfqd->busy_queues > 1)
Jens Axboe6d048f52007-04-25 12:44:27 +02001114 break;
Jens Axboe3ed9a292007-04-23 08:33:33 +02001115 if (cfqq->dispatched >= 4 * max_dispatch)
Jens Axboea9938002007-04-20 08:55:52 +02001116 break;
1117 }
Jens Axboe9ede2092007-01-19 12:11:44 +11001118
Jens Axboe3ed9a292007-04-23 08:33:33 +02001119 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
1120 break;
1121
Jens Axboe3b181522005-06-27 10:56:24 +02001122 cfq_clear_cfqq_must_dispatch(cfqq);
1123 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001124 del_timer(&cfqd->idle_slice_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Jens Axboecaaa5f92006-06-16 11:23:00 +02001126 dispatched += __cfq_dispatch_requests(cfqd, cfqq, max_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Jens Axboecaaa5f92006-06-16 11:23:00 +02001129 return dispatched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
Jens Axboe5e705372006-07-13 12:39:25 +02001133 * task holds one reference to the queue, dropped when task exits. each rq
1134 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 *
1136 * queue lock must be held here.
1137 */
1138static void cfq_put_queue(struct cfq_queue *cfqq)
1139{
Jens Axboe22e2c502005-06-27 10:55:12 +02001140 struct cfq_data *cfqd = cfqq->cfqd;
1141
1142 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (!atomic_dec_and_test(&cfqq->ref))
1145 return;
1146
1147 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001148 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001149 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001151 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001152 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001153 cfq_schedule_dispatch(cfqd);
1154 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * it's on the empty list and still hashed
1158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 hlist_del(&cfqq->cfq_hash);
1160 kmem_cache_free(cfq_pool, cfqq);
1161}
1162
Jens Axboe1ea25ec2006-07-18 22:24:11 +02001163static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001164__cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned int prio,
1165 const int hashval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct hlist_head *hash_list = &cfqd->cfq_hash[hashval];
Jens Axboe206dc692006-03-28 13:03:44 +02001168 struct hlist_node *entry;
1169 struct cfq_queue *__cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Jens Axboe206dc692006-03-28 13:03:44 +02001171 hlist_for_each_entry(__cfqq, entry, hash_list, cfq_hash) {
Al Virob0a69162006-03-14 15:32:50 -05001172 const unsigned short __p = IOPRIO_PRIO_VALUE(__cfqq->org_ioprio_class, __cfqq->org_ioprio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Jens Axboe206dc692006-03-28 13:03:44 +02001174 if (__cfqq->key == key && (__p == prio || !prio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return __cfqq;
1176 }
1177
1178 return NULL;
1179}
1180
1181static struct cfq_queue *
Jens Axboe3b181522005-06-27 10:56:24 +02001182cfq_find_cfq_hash(struct cfq_data *cfqd, unsigned int key, unsigned short prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Jens Axboe3b181522005-06-27 10:56:24 +02001184 return __cfq_find_cfq_hash(cfqd, key, prio, hash_long(key, CFQ_QHASH_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Jens Axboee2d74ac2006-03-28 08:59:01 +02001187static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Jens Axboe22e2c502005-06-27 10:55:12 +02001189 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001190 struct rb_node *n;
1191 int freed = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001192
Jens Axboee2d74ac2006-03-28 08:59:01 +02001193 while ((n = rb_first(&ioc->cic_root)) != NULL) {
1194 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1195 rb_erase(&__cic->rb_node, &ioc->cic_root);
Jens Axboe22e2c502005-06-27 10:55:12 +02001196 kmem_cache_free(cfq_ioc_pool, __cic);
Al Viro334e94d2006-03-18 15:05:53 -05001197 freed++;
Jens Axboe22e2c502005-06-27 10:55:12 +02001198 }
1199
Jens Axboe4050cf12006-07-19 05:07:12 +02001200 elv_ioc_count_mod(ioc_count, -freed);
1201
1202 if (ioc_gone && !elv_ioc_count_read(ioc_count))
Al Viro334e94d2006-03-18 15:05:53 -05001203 complete(ioc_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Jens Axboe89850f72006-07-22 16:48:31 +02001206static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1207{
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001208 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001209 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001210 cfq_schedule_dispatch(cfqd);
1211 }
Jens Axboe89850f72006-07-22 16:48:31 +02001212
1213 cfq_put_queue(cfqq);
1214}
1215
1216static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1217 struct cfq_io_context *cic)
1218{
Jens Axboefc463792006-08-29 09:05:44 +02001219 list_del_init(&cic->queue_list);
1220 smp_wmb();
1221 cic->key = NULL;
1222
Jens Axboe89850f72006-07-22 16:48:31 +02001223 if (cic->cfqq[ASYNC]) {
1224 cfq_exit_cfqq(cfqd, cic->cfqq[ASYNC]);
1225 cic->cfqq[ASYNC] = NULL;
1226 }
1227
1228 if (cic->cfqq[SYNC]) {
1229 cfq_exit_cfqq(cfqd, cic->cfqq[SYNC]);
1230 cic->cfqq[SYNC] = NULL;
1231 }
Jens Axboe89850f72006-07-22 16:48:31 +02001232}
1233
Jens Axboe22e2c502005-06-27 10:55:12 +02001234static void cfq_exit_single_io_context(struct cfq_io_context *cic)
1235{
Al Viro478a82b2006-03-18 13:25:24 -05001236 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001237
Jens Axboe89850f72006-07-22 16:48:31 +02001238 if (cfqd) {
1239 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001240
Jens Axboefc463792006-08-29 09:05:44 +02001241 spin_lock_irq(q->queue_lock);
Jens Axboe89850f72006-07-22 16:48:31 +02001242 __cfq_exit_single_io_context(cfqd, cic);
Jens Axboefc463792006-08-29 09:05:44 +02001243 spin_unlock_irq(q->queue_lock);
Al Viro12a05732006-03-18 13:38:01 -05001244 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001245}
1246
Jens Axboe498d3aa2007-04-26 12:54:48 +02001247/*
1248 * The process that ioc belongs to has exited, we need to clean up
1249 * and put the internal structures we have that belongs to that process.
1250 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001251static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Jens Axboe22e2c502005-06-27 10:55:12 +02001253 struct cfq_io_context *__cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001254 struct rb_node *n;
Jens Axboe22e2c502005-06-27 10:55:12 +02001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * put the reference this task is holding to the various queues
1258 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001259
1260 n = rb_first(&ioc->cic_root);
1261 while (n != NULL) {
1262 __cic = rb_entry(n, struct cfq_io_context, rb_node);
1263
Jens Axboe22e2c502005-06-27 10:55:12 +02001264 cfq_exit_single_io_context(__cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001265 n = rb_next(n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Jens Axboe22e2c502005-06-27 10:55:12 +02001269static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001270cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Jens Axboeb5deef92006-07-19 23:39:40 +02001272 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Jens Axboeb5deef92006-07-19 23:39:40 +02001274 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (cic) {
Jens Axboe553698f2006-06-14 19:11:57 +02001276 memset(cic, 0, sizeof(*cic));
Jens Axboe22e2c502005-06-27 10:55:12 +02001277 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001278 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001279 cic->dtor = cfq_free_io_context;
1280 cic->exit = cfq_exit_io_context;
Jens Axboe4050cf12006-07-19 05:07:12 +02001281 elv_ioc_count_inc(ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 return cic;
1285}
1286
Jens Axboe22e2c502005-06-27 10:55:12 +02001287static void cfq_init_prio_data(struct cfq_queue *cfqq)
1288{
1289 struct task_struct *tsk = current;
1290 int ioprio_class;
1291
Jens Axboe3b181522005-06-27 10:56:24 +02001292 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001293 return;
1294
1295 ioprio_class = IOPRIO_PRIO_CLASS(tsk->ioprio);
1296 switch (ioprio_class) {
1297 default:
1298 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1299 case IOPRIO_CLASS_NONE:
1300 /*
1301 * no prio set, place us in the middle of the BE classes
1302 */
1303 cfqq->ioprio = task_nice_ioprio(tsk);
1304 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1305 break;
1306 case IOPRIO_CLASS_RT:
1307 cfqq->ioprio = task_ioprio(tsk);
1308 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1309 break;
1310 case IOPRIO_CLASS_BE:
1311 cfqq->ioprio = task_ioprio(tsk);
1312 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1313 break;
1314 case IOPRIO_CLASS_IDLE:
1315 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1316 cfqq->ioprio = 7;
Jens Axboe3b181522005-06-27 10:56:24 +02001317 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001318 break;
1319 }
1320
1321 /*
1322 * keep track of original prio settings in case we have to temporarily
1323 * elevate the priority of this queue
1324 */
1325 cfqq->org_ioprio = cfqq->ioprio;
1326 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001327 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001328}
1329
Al Viro478a82b2006-03-18 13:25:24 -05001330static inline void changed_ioprio(struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001331{
Al Viro478a82b2006-03-18 13:25:24 -05001332 struct cfq_data *cfqd = cic->key;
1333 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001334 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001335
Jens Axboecaaa5f92006-06-16 11:23:00 +02001336 if (unlikely(!cfqd))
1337 return;
1338
Jens Axboec1b707d2006-10-30 19:54:23 +01001339 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001340
1341 cfqq = cic->cfqq[ASYNC];
1342 if (cfqq) {
1343 struct cfq_queue *new_cfqq;
1344 new_cfqq = cfq_get_queue(cfqd, CFQ_KEY_ASYNC, cic->ioc->task,
1345 GFP_ATOMIC);
1346 if (new_cfqq) {
1347 cic->cfqq[ASYNC] = new_cfqq;
1348 cfq_put_queue(cfqq);
1349 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001350 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001351
1352 cfqq = cic->cfqq[SYNC];
1353 if (cfqq)
1354 cfq_mark_cfqq_prio_changed(cfqq);
1355
Jens Axboec1b707d2006-10-30 19:54:23 +01001356 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001357}
1358
Jens Axboefc463792006-08-29 09:05:44 +02001359static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Al Viroa6a07632006-03-18 13:26:44 -05001361 struct cfq_io_context *cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001362 struct rb_node *n;
Al Viroa6a07632006-03-18 13:26:44 -05001363
Jens Axboefc463792006-08-29 09:05:44 +02001364 ioc->ioprio_changed = 0;
Al Viroa6a07632006-03-18 13:26:44 -05001365
Jens Axboee2d74ac2006-03-28 08:59:01 +02001366 n = rb_first(&ioc->cic_root);
1367 while (n != NULL) {
1368 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboe3793c652006-05-30 21:11:04 +02001369
Al Viro478a82b2006-03-18 13:25:24 -05001370 changed_ioprio(cic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001371 n = rb_next(n);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375static struct cfq_queue *
Al Viro6f325a12006-03-18 14:58:37 -05001376cfq_get_queue(struct cfq_data *cfqd, unsigned int key, struct task_struct *tsk,
Al Viro8267e262005-10-21 03:20:53 -04001377 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 const int hashval = hash_long(key, CFQ_QHASH_SHIFT);
1380 struct cfq_queue *cfqq, *new_cfqq = NULL;
Al Viro6f325a12006-03-18 14:58:37 -05001381 unsigned short ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383retry:
Al Viro6f325a12006-03-18 14:58:37 -05001384 ioprio = tsk->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02001385 cfqq = __cfq_find_cfq_hash(cfqd, key, ioprio, hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (!cfqq) {
1388 if (new_cfqq) {
1389 cfqq = new_cfqq;
1390 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001391 } else if (gfp_mask & __GFP_WAIT) {
Jens Axboe89850f72006-07-22 16:48:31 +02001392 /*
1393 * Inform the allocator of the fact that we will
1394 * just repeat this allocation if it fails, to allow
1395 * the allocator to do whatever it needs to attempt to
1396 * free memory.
1397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 spin_unlock_irq(cfqd->queue->queue_lock);
Jens Axboeb5deef92006-07-19 23:39:40 +02001399 new_cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask|__GFP_NOFAIL, cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 spin_lock_irq(cfqd->queue->queue_lock);
1401 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001402 } else {
Jens Axboeb5deef92006-07-19 23:39:40 +02001403 cfqq = kmem_cache_alloc_node(cfq_pool, gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001404 if (!cfqq)
1405 goto out;
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 memset(cfqq, 0, sizeof(*cfqq));
1409
1410 INIT_HLIST_NODE(&cfqq->cfq_hash);
Jens Axboed9e76202007-04-20 14:27:50 +02001411 RB_CLEAR_NODE(&cfqq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001412 INIT_LIST_HEAD(&cfqq->fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 cfqq->key = key;
1415 hlist_add_head(&cfqq->cfq_hash, &cfqd->cfq_hash[hashval]);
1416 atomic_set(&cfqq->ref, 0);
1417 cfqq->cfqd = cfqd;
Jens Axboec5b680f2007-01-19 11:56:49 +11001418
Jens Axboea9938002007-04-20 08:55:52 +02001419 if (key != CFQ_KEY_ASYNC)
1420 cfq_mark_cfqq_idle_window(cfqq);
1421
Jens Axboe3b181522005-06-27 10:56:24 +02001422 cfq_mark_cfqq_prio_changed(cfqq);
Jens Axboe53b03742006-07-28 09:48:51 +02001423 cfq_mark_cfqq_queue_new(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001424 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426
1427 if (new_cfqq)
1428 kmem_cache_free(cfq_pool, new_cfqq);
1429
1430 atomic_inc(&cfqq->ref);
1431out:
1432 WARN_ON((gfp_mask & __GFP_WAIT) && !cfqq);
1433 return cfqq;
1434}
1435
Jens Axboe498d3aa2007-04-26 12:54:48 +02001436/*
1437 * We drop cfq io contexts lazily, so we may find a dead one.
1438 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001439static void
1440cfq_drop_dead_cic(struct io_context *ioc, struct cfq_io_context *cic)
1441{
Jens Axboefc463792006-08-29 09:05:44 +02001442 WARN_ON(!list_empty(&cic->queue_list));
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001443 rb_erase(&cic->rb_node, &ioc->cic_root);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001444 kmem_cache_free(cfq_ioc_pool, cic);
Jens Axboe4050cf12006-07-19 05:07:12 +02001445 elv_ioc_count_dec(ioc_count);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001446}
1447
Jens Axboee2d74ac2006-03-28 08:59:01 +02001448static struct cfq_io_context *
1449cfq_cic_rb_lookup(struct cfq_data *cfqd, struct io_context *ioc)
1450{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001451 struct rb_node *n;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001452 struct cfq_io_context *cic;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001453 void *k, *key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001454
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001455restart:
1456 n = ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001457 while (n) {
1458 cic = rb_entry(n, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001459 /* ->key must be copied to avoid race with cfq_exit_queue() */
1460 k = cic->key;
1461 if (unlikely(!k)) {
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001462 cfq_drop_dead_cic(ioc, cic);
1463 goto restart;
1464 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001465
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001466 if (key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001467 n = n->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001468 else if (key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001469 n = n->rb_right;
1470 else
1471 return cic;
1472 }
1473
1474 return NULL;
1475}
1476
1477static inline void
1478cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1479 struct cfq_io_context *cic)
1480{
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001481 struct rb_node **p;
1482 struct rb_node *parent;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001483 struct cfq_io_context *__cic;
Jens Axboe0261d682006-10-30 19:07:48 +01001484 unsigned long flags;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001485 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001486
Jens Axboee2d74ac2006-03-28 08:59:01 +02001487 cic->ioc = ioc;
1488 cic->key = cfqd;
1489
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001490restart:
1491 parent = NULL;
1492 p = &ioc->cic_root.rb_node;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001493 while (*p) {
1494 parent = *p;
1495 __cic = rb_entry(parent, struct cfq_io_context, rb_node);
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001496 /* ->key must be copied to avoid race with cfq_exit_queue() */
1497 k = __cic->key;
1498 if (unlikely(!k)) {
Oleg Nesterovbe33c3a2006-08-21 08:36:12 +02001499 cfq_drop_dead_cic(ioc, __cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001500 goto restart;
1501 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001502
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001503 if (cic->key < k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001504 p = &(*p)->rb_left;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001505 else if (cic->key > k)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001506 p = &(*p)->rb_right;
1507 else
1508 BUG();
1509 }
1510
1511 rb_link_node(&cic->rb_node, parent, p);
1512 rb_insert_color(&cic->rb_node, &ioc->cic_root);
Jens Axboefc463792006-08-29 09:05:44 +02001513
Jens Axboe0261d682006-10-30 19:07:48 +01001514 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001515 list_add(&cic->queue_list, &cfqd->cic_list);
Jens Axboe0261d682006-10-30 19:07:48 +01001516 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001517}
1518
Jens Axboe22e2c502005-06-27 10:55:12 +02001519/*
1520 * Setup general io context and cfq io context. There can be several cfq
1521 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02001522 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02001523 */
1524static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02001525cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Jens Axboe22e2c502005-06-27 10:55:12 +02001527 struct io_context *ioc = NULL;
1528 struct cfq_io_context *cic;
1529
1530 might_sleep_if(gfp_mask & __GFP_WAIT);
1531
Jens Axboeb5deef92006-07-19 23:39:40 +02001532 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02001533 if (!ioc)
1534 return NULL;
1535
Jens Axboee2d74ac2006-03-28 08:59:01 +02001536 cic = cfq_cic_rb_lookup(cfqd, ioc);
1537 if (cic)
1538 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02001539
Jens Axboee2d74ac2006-03-28 08:59:01 +02001540 cic = cfq_alloc_io_context(cfqd, gfp_mask);
1541 if (cic == NULL)
1542 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02001543
Jens Axboee2d74ac2006-03-28 08:59:01 +02001544 cfq_cic_link(cfqd, ioc, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02001545out:
Jens Axboefc463792006-08-29 09:05:44 +02001546 smp_read_barrier_depends();
1547 if (unlikely(ioc->ioprio_changed))
1548 cfq_ioc_set_ioprio(ioc);
1549
Jens Axboe22e2c502005-06-27 10:55:12 +02001550 return cic;
1551err:
1552 put_io_context(ioc);
1553 return NULL;
1554}
1555
1556static void
1557cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
1558{
Jens Axboeaaf12282007-01-19 11:30:16 +11001559 unsigned long elapsed = jiffies - cic->last_end_request;
1560 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02001561
1562 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
1563 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
1564 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
1565}
1566
Jens Axboe206dc692006-03-28 13:03:44 +02001567static void
Jens Axboe6d048f52007-04-25 12:44:27 +02001568cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_io_context *cic,
1569 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02001570{
1571 sector_t sdist;
1572 u64 total;
1573
Jens Axboe5e705372006-07-13 12:39:25 +02001574 if (cic->last_request_pos < rq->sector)
1575 sdist = rq->sector - cic->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02001576 else
Jens Axboe5e705372006-07-13 12:39:25 +02001577 sdist = cic->last_request_pos - rq->sector;
Jens Axboe206dc692006-03-28 13:03:44 +02001578
Jens Axboe6d048f52007-04-25 12:44:27 +02001579 if (!cic->seek_samples) {
1580 cfqd->new_seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1581 cfqd->new_seek_mean = cfqd->new_seek_total / 256;
1582 }
1583
Jens Axboe206dc692006-03-28 13:03:44 +02001584 /*
1585 * Don't allow the seek distance to get too large from the
1586 * odd fragment, pagein, etc
1587 */
1588 if (cic->seek_samples <= 60) /* second&third seek */
1589 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*1024);
1590 else
1591 sdist = min(sdist, (cic->seek_mean * 4) + 2*1024*64);
1592
1593 cic->seek_samples = (7*cic->seek_samples + 256) / 8;
1594 cic->seek_total = (7*cic->seek_total + (u64)256*sdist) / 8;
1595 total = cic->seek_total + (cic->seek_samples/2);
1596 do_div(total, cic->seek_samples);
1597 cic->seek_mean = (sector_t)total;
1598}
Jens Axboe22e2c502005-06-27 10:55:12 +02001599
1600/*
1601 * Disable idle window if the process thinks too long or seeks so much that
1602 * it doesn't matter
1603 */
1604static void
1605cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1606 struct cfq_io_context *cic)
1607{
Jens Axboe1be92f22007-04-19 14:32:26 +02001608 int enable_idle;
1609
1610 if (!cfq_cfqq_sync(cfqq))
1611 return;
1612
1613 enable_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001614
Jens Axboecaaa5f92006-06-16 11:23:00 +02001615 if (!cic->ioc->task || !cfqd->cfq_slice_idle ||
1616 (cfqd->hw_tag && CIC_SEEKY(cic)))
Jens Axboe22e2c502005-06-27 10:55:12 +02001617 enable_idle = 0;
1618 else if (sample_valid(cic->ttime_samples)) {
1619 if (cic->ttime_mean > cfqd->cfq_slice_idle)
1620 enable_idle = 0;
1621 else
1622 enable_idle = 1;
1623 }
1624
Jens Axboe3b181522005-06-27 10:56:24 +02001625 if (enable_idle)
1626 cfq_mark_cfqq_idle_window(cfqq);
1627 else
1628 cfq_clear_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001629}
1630
Jens Axboe22e2c502005-06-27 10:55:12 +02001631/*
1632 * Check if new_cfqq should preempt the currently active queue. Return 0 for
1633 * no or if we aren't sure, a 1 will cause a preempt.
1634 */
1635static int
1636cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02001637 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001638{
Jens Axboe6d048f52007-04-25 12:44:27 +02001639 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001640
Jens Axboe6d048f52007-04-25 12:44:27 +02001641 cfqq = cfqd->active_queue;
1642 if (!cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001643 return 0;
1644
Jens Axboe6d048f52007-04-25 12:44:27 +02001645 if (cfq_slice_used(cfqq))
1646 return 1;
1647
1648 if (cfq_class_idle(new_cfqq))
Jens Axboecaaa5f92006-06-16 11:23:00 +02001649 return 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001650
1651 if (cfq_class_idle(cfqq))
1652 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001653
Jens Axboe22e2c502005-06-27 10:55:12 +02001654 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02001655 * if the new request is sync, but the currently running queue is
1656 * not, let the sync request have priority.
1657 */
Jens Axboe5e705372006-07-13 12:39:25 +02001658 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001659 return 1;
Jens Axboe1e3335d2007-02-14 19:59:49 +01001660
Jens Axboe374f84a2006-07-23 01:42:19 +02001661 /*
1662 * So both queues are sync. Let the new request get disk time if
1663 * it's a metadata request and the current queue is doing regular IO.
1664 */
1665 if (rq_is_meta(rq) && !cfqq->meta_pending)
1666 return 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001667
Jens Axboe1e3335d2007-02-14 19:59:49 +01001668 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
1669 return 0;
1670
1671 /*
1672 * if this request is as-good as one we would expect from the
1673 * current cfqq, let it preempt
1674 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001675 if (cfq_rq_close(cfqd, rq))
Jens Axboe1e3335d2007-02-14 19:59:49 +01001676 return 1;
1677
Jens Axboe22e2c502005-06-27 10:55:12 +02001678 return 0;
1679}
1680
1681/*
1682 * cfqq preempts the active queue. if we allowed preempt with no slice left,
1683 * let it have half of its nominal slice.
1684 */
1685static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1686{
Jens Axboe6084cdd2007-04-23 08:25:00 +02001687 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02001688
Jens Axboebf572252006-07-19 20:29:12 +02001689 /*
1690 * Put the new queue at the front of the of the current list,
1691 * so we know that it will be selected next.
1692 */
1693 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02001694
1695 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02001696
Jens Axboe44f7c162007-01-19 11:51:58 +11001697 cfqq->slice_end = 0;
1698 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001699}
1700
1701/*
Jens Axboe5e705372006-07-13 12:39:25 +02001702 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02001703 * something we should do about it
1704 */
1705static void
Jens Axboe5e705372006-07-13 12:39:25 +02001706cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1707 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001708{
Jens Axboe5e705372006-07-13 12:39:25 +02001709 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02001710
Jens Axboe374f84a2006-07-23 01:42:19 +02001711 if (rq_is_meta(rq))
1712 cfqq->meta_pending++;
1713
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001714 cfq_update_io_thinktime(cfqd, cic);
Jens Axboe6d048f52007-04-25 12:44:27 +02001715 cfq_update_io_seektime(cfqd, cic, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001716 cfq_update_idle_window(cfqd, cfqq, cic);
1717
Jens Axboe5e705372006-07-13 12:39:25 +02001718 cic->last_request_pos = rq->sector + rq->nr_sectors;
Jens Axboe6d048f52007-04-25 12:44:27 +02001719 cfqq->last_request_pos = cic->last_request_pos;
Jens Axboe22e2c502005-06-27 10:55:12 +02001720
1721 if (cfqq == cfqd->active_queue) {
1722 /*
1723 * if we are waiting for a request for this queue, let it rip
1724 * immediately and flag that we must not expire this queue
1725 * just now
1726 */
Jens Axboe3b181522005-06-27 10:56:24 +02001727 if (cfq_cfqq_wait_request(cfqq)) {
1728 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001729 del_timer(&cfqd->idle_slice_timer);
Jens Axboedc72ef42006-07-20 14:54:05 +02001730 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001731 }
Jens Axboe5e705372006-07-13 12:39:25 +02001732 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001733 /*
1734 * not the active queue - expire current slice if it is
1735 * idle and has expired it's mean thinktime or this new queue
1736 * has some old slice time left and is of higher priority
1737 */
1738 cfq_preempt_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001739 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboedc72ef42006-07-20 14:54:05 +02001740 blk_start_queueing(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02001741 }
1742}
1743
Jens Axboeb4878f22005-10-20 16:42:29 +02001744static void cfq_insert_request(request_queue_t *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001745{
Jens Axboeb4878f22005-10-20 16:42:29 +02001746 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001747 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001748
1749 cfq_init_prio_data(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jens Axboe5e705372006-07-13 12:39:25 +02001751 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jens Axboe22e2c502005-06-27 10:55:12 +02001753 list_add_tail(&rq->queuelist, &cfqq->fifo);
1754
Jens Axboe5e705372006-07-13 12:39:25 +02001755 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758static void cfq_completed_request(request_queue_t *q, struct request *rq)
1759{
Jens Axboe5e705372006-07-13 12:39:25 +02001760 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001761 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02001762 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001763 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Jens Axboeb4878f22005-10-20 16:42:29 +02001765 now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Jens Axboeb4878f22005-10-20 16:42:29 +02001767 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02001768 WARN_ON(!cfqq->dispatched);
Jens Axboeb4878f22005-10-20 16:42:29 +02001769 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02001770 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Jens Axboe3ed9a292007-04-23 08:33:33 +02001772 if (cfq_cfqq_sync(cfqq))
1773 cfqd->sync_flight--;
1774
Jens Axboeb4878f22005-10-20 16:42:29 +02001775 if (!cfq_class_idle(cfqq))
1776 cfqd->last_end_request = now;
Jens Axboe3b181522005-06-27 10:56:24 +02001777
Jens Axboecaaa5f92006-06-16 11:23:00 +02001778 if (sync)
Jens Axboe5e705372006-07-13 12:39:25 +02001779 RQ_CIC(rq)->last_end_request = now;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001780
1781 /*
1782 * If this is the active queue, check if it needs to be expired,
1783 * or if we want to idle in case it has no pending requests.
1784 */
1785 if (cfqd->active_queue == cfqq) {
Jens Axboe44f7c162007-01-19 11:51:58 +11001786 if (cfq_cfqq_slice_new(cfqq)) {
1787 cfq_set_prio_slice(cfqd, cfqq);
1788 cfq_clear_cfqq_slice_new(cfqq);
1789 }
1790 if (cfq_slice_used(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02001791 cfq_slice_expired(cfqd, 1);
Jens Axboe6d048f52007-04-25 12:44:27 +02001792 else if (sync && RB_EMPTY_ROOT(&cfqq->sort_list))
1793 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001794 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001795
1796 if (!cfqd->rq_in_driver)
1797 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Jens Axboe22e2c502005-06-27 10:55:12 +02001800/*
1801 * we temporarily boost lower priority queues if they are holding fs exclusive
1802 * resources. they are boosted to normal prio (CLASS_BE/4)
1803 */
1804static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Jens Axboe22e2c502005-06-27 10:55:12 +02001806 if (has_fs_excl()) {
1807 /*
1808 * boost idle prio on transactions that would lock out other
1809 * users of the filesystem
1810 */
1811 if (cfq_class_idle(cfqq))
1812 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1813 if (cfqq->ioprio > IOPRIO_NORM)
1814 cfqq->ioprio = IOPRIO_NORM;
1815 } else {
1816 /*
1817 * check if we need to unboost the queue
1818 */
1819 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
1820 cfqq->ioprio_class = cfqq->org_ioprio_class;
1821 if (cfqq->ioprio != cfqq->org_ioprio)
1822 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001824}
1825
Jens Axboe89850f72006-07-22 16:48:31 +02001826static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001827{
Jens Axboe3b181522005-06-27 10:56:24 +02001828 if ((cfq_cfqq_wait_request(cfqq) || cfq_cfqq_must_alloc(cfqq)) &&
Andrew Morton99f95e52005-06-27 20:14:05 -07001829 !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001830 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001831 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02001832 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001833
1834 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02001835}
1836
Jens Axboecb78b282006-07-28 09:32:57 +02001837static int cfq_may_queue(request_queue_t *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02001838{
1839 struct cfq_data *cfqd = q->elevator->elevator_data;
1840 struct task_struct *tsk = current;
1841 struct cfq_queue *cfqq;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001842 unsigned int key;
1843
1844 key = cfq_queue_pid(tsk, rw, rw & REQ_RW_SYNC);
Jens Axboe22e2c502005-06-27 10:55:12 +02001845
1846 /*
1847 * don't force setup of a queue from here, as a call to may_queue
1848 * does not necessarily imply that a request actually will be queued.
1849 * so just lookup a possibly existing queue, or return 'may queue'
1850 * if that fails
1851 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001852 cfqq = cfq_find_cfq_hash(cfqd, key, tsk->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001853 if (cfqq) {
1854 cfq_init_prio_data(cfqq);
1855 cfq_prio_boost(cfqq);
1856
Jens Axboe89850f72006-07-22 16:48:31 +02001857 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001858 }
1859
1860 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863/*
1864 * queue lock held here
1865 */
Jens Axboebb37b942006-12-01 10:42:33 +01001866static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Jens Axboe5e705372006-07-13 12:39:25 +02001868 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Jens Axboe5e705372006-07-13 12:39:25 +02001870 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001871 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jens Axboe22e2c502005-06-27 10:55:12 +02001873 BUG_ON(!cfqq->allocated[rw]);
1874 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Jens Axboe5e705372006-07-13 12:39:25 +02001876 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02001879 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 cfq_put_queue(cfqq);
1882 }
1883}
1884
1885/*
Jens Axboe22e2c502005-06-27 10:55:12 +02001886 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Jens Axboe22e2c502005-06-27 10:55:12 +02001888static int
Jens Axboecb78b282006-07-28 09:32:57 +02001889cfq_set_request(request_queue_t *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe3b181522005-06-27 10:56:24 +02001892 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 struct cfq_io_context *cic;
1894 const int rw = rq_data_dir(rq);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001895 const int is_sync = rq_is_sync(rq);
1896 pid_t key = cfq_queue_pid(tsk, rw, is_sync);
Jens Axboe22e2c502005-06-27 10:55:12 +02001897 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 unsigned long flags;
1899
1900 might_sleep_if(gfp_mask & __GFP_WAIT);
1901
Jens Axboee2d74ac2006-03-28 08:59:01 +02001902 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 spin_lock_irqsave(q->queue_lock, flags);
1905
Jens Axboe22e2c502005-06-27 10:55:12 +02001906 if (!cic)
1907 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Al Viro12a05732006-03-18 13:38:01 -05001909 if (!cic->cfqq[is_sync]) {
Al Viro6f325a12006-03-18 14:58:37 -05001910 cfqq = cfq_get_queue(cfqd, key, tsk, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02001911 if (!cfqq)
1912 goto queue_fail;
1913
Al Viro12a05732006-03-18 13:38:01 -05001914 cic->cfqq[is_sync] = cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001915 } else
Al Viro12a05732006-03-18 13:38:01 -05001916 cfqq = cic->cfqq[is_sync];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 cfqq->allocated[rw]++;
Jens Axboe3b181522005-06-27 10:56:24 +02001919 cfq_clear_cfqq_must_alloc(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001920 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 spin_unlock_irqrestore(q->queue_lock, flags);
1923
Jens Axboe5e705372006-07-13 12:39:25 +02001924 rq->elevator_private = cic;
1925 rq->elevator_private2 = cfqq;
1926 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02001927
Jens Axboe22e2c502005-06-27 10:55:12 +02001928queue_fail:
1929 if (cic)
1930 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02001931
Jens Axboe3b181522005-06-27 10:56:24 +02001932 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 spin_unlock_irqrestore(q->queue_lock, flags);
1934 return 1;
1935}
1936
David Howells65f27f32006-11-22 14:55:48 +00001937static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02001938{
David Howells65f27f32006-11-22 14:55:48 +00001939 struct cfq_data *cfqd =
1940 container_of(work, struct cfq_data, unplug_work);
1941 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001942 unsigned long flags;
1943
1944 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboedc72ef42006-07-20 14:54:05 +02001945 blk_start_queueing(q);
Jens Axboe22e2c502005-06-27 10:55:12 +02001946 spin_unlock_irqrestore(q->queue_lock, flags);
1947}
1948
1949/*
1950 * Timer running if the active_queue is currently idling inside its time slice
1951 */
1952static void cfq_idle_slice_timer(unsigned long data)
1953{
1954 struct cfq_data *cfqd = (struct cfq_data *) data;
1955 struct cfq_queue *cfqq;
1956 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001957 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02001958
1959 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1960
1961 if ((cfqq = cfqd->active_queue) != NULL) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11001962 timed_out = 0;
1963
Jens Axboe22e2c502005-06-27 10:55:12 +02001964 /*
1965 * expired
1966 */
Jens Axboe44f7c162007-01-19 11:51:58 +11001967 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001968 goto expire;
1969
1970 /*
1971 * only expire and reinvoke request handler, if there are
1972 * other queues with pending requests
1973 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02001974 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02001975 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02001976
1977 /*
1978 * not expired and it has a request pending, let it dispatch
1979 */
Jens Axboedd67d052006-06-21 09:36:18 +02001980 if (!RB_EMPTY_ROOT(&cfqq->sort_list)) {
Jens Axboe3b181522005-06-27 10:56:24 +02001981 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001982 goto out_kick;
1983 }
1984 }
1985expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02001986 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02001987out_kick:
Jens Axboe3b181522005-06-27 10:56:24 +02001988 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02001989out_cont:
1990 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1991}
1992
1993/*
1994 * Timer running if an idle class queue is waiting for service
1995 */
1996static void cfq_idle_class_timer(unsigned long data)
1997{
1998 struct cfq_data *cfqd = (struct cfq_data *) data;
1999 unsigned long flags, end;
2000
2001 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2002
2003 /*
2004 * race with a non-idle queue, reset timer
2005 */
2006 end = cfqd->last_end_request + CFQ_IDLE_GRACE;
Jens Axboeae818a32006-06-01 10:13:43 +02002007 if (!time_after_eq(jiffies, end))
2008 mod_timer(&cfqd->idle_class_timer, end);
2009 else
Jens Axboe3b181522005-06-27 10:56:24 +02002010 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002011
2012 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2013}
2014
Jens Axboe3b181522005-06-27 10:56:24 +02002015static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2016{
2017 del_timer_sync(&cfqd->idle_slice_timer);
2018 del_timer_sync(&cfqd->idle_class_timer);
2019 blk_sync_queue(cfqd->queue);
2020}
Jens Axboe22e2c502005-06-27 10:55:12 +02002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static void cfq_exit_queue(elevator_t *e)
2023{
Jens Axboe22e2c502005-06-27 10:55:12 +02002024 struct cfq_data *cfqd = e->elevator_data;
Al Virod9ff4182006-03-18 13:51:22 -05002025 request_queue_t *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002026
Jens Axboe3b181522005-06-27 10:56:24 +02002027 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002028
Al Virod9ff4182006-03-18 13:51:22 -05002029 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002030
Al Virod9ff4182006-03-18 13:51:22 -05002031 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02002032 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002033
2034 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002035 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2036 struct cfq_io_context,
2037 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002038
2039 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002040 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002041
Al Virod9ff4182006-03-18 13:51:22 -05002042 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002043
2044 cfq_shutdown_timer_wq(cfqd);
2045
Al Viroa90d7422006-03-18 12:05:37 -05002046 kfree(cfqd->cfq_hash);
2047 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Jens Axboebb37b942006-12-01 10:42:33 +01002050static void *cfq_init_queue(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct cfq_data *cfqd;
2053 int i;
2054
Jens Axboeb5deef92006-07-19 23:39:40 +02002055 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 memset(cfqd, 0, sizeof(*cfqd));
Jens Axboe22e2c502005-06-27 10:55:12 +02002060
Jens Axboecc09e292007-04-26 12:53:50 +02002061 cfqd->service_tree = CFQ_RB_ROOT;
Al Virod9ff4182006-03-18 13:51:22 -05002062 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Jens Axboeb5deef92006-07-19 23:39:40 +02002064 cfqd->cfq_hash = kmalloc_node(sizeof(struct hlist_head) * CFQ_QHASH_ENTRIES, GFP_KERNEL, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!cfqd->cfq_hash)
Jens Axboe5e705372006-07-13 12:39:25 +02002066 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 for (i = 0; i < CFQ_QHASH_ENTRIES; i++)
2069 INIT_HLIST_HEAD(&cfqd->cfq_hash[i]);
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Jens Axboe22e2c502005-06-27 10:55:12 +02002073 init_timer(&cfqd->idle_slice_timer);
2074 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2075 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2076
2077 init_timer(&cfqd->idle_class_timer);
2078 cfqd->idle_class_timer.function = cfq_idle_class_timer;
2079 cfqd->idle_class_timer.data = (unsigned long) cfqd;
2080
David Howells65f27f32006-11-22 14:55:48 +00002081 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002084 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2085 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 cfqd->cfq_back_max = cfq_back_max;
2087 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002088 cfqd->cfq_slice[0] = cfq_slice_async;
2089 cfqd->cfq_slice[1] = cfq_slice_sync;
2090 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2091 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe3b181522005-06-27 10:56:24 +02002092
Jens Axboebc1c1162006-06-08 08:49:06 +02002093 return cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +02002094out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 kfree(cfqd);
Jens Axboebc1c1162006-06-08 08:49:06 +02002096 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099static void cfq_slab_kill(void)
2100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (cfq_pool)
2102 kmem_cache_destroy(cfq_pool);
2103 if (cfq_ioc_pool)
2104 kmem_cache_destroy(cfq_ioc_pool);
2105}
2106
2107static int __init cfq_slab_setup(void)
2108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 cfq_pool = kmem_cache_create("cfq_pool", sizeof(struct cfq_queue), 0, 0,
2110 NULL, NULL);
2111 if (!cfq_pool)
2112 goto fail;
2113
2114 cfq_ioc_pool = kmem_cache_create("cfq_ioc_pool",
2115 sizeof(struct cfq_io_context), 0, 0, NULL, NULL);
2116 if (!cfq_ioc_pool)
2117 goto fail;
2118
2119 return 0;
2120fail:
2121 cfq_slab_kill();
2122 return -ENOMEM;
2123}
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/*
2126 * sysfs parts below -->
2127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128static ssize_t
2129cfq_var_show(unsigned int var, char *page)
2130{
2131 return sprintf(page, "%d\n", var);
2132}
2133
2134static ssize_t
2135cfq_var_store(unsigned int *var, const char *page, size_t count)
2136{
2137 char *p = (char *) page;
2138
2139 *var = simple_strtoul(p, &p, 10);
2140 return count;
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002144static ssize_t __FUNC(elevator_t *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002146 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 unsigned int __data = __VAR; \
2148 if (__CONV) \
2149 __data = jiffies_to_msecs(__data); \
2150 return cfq_var_show(__data, (page)); \
2151}
2152SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002153SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2154SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002155SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2156SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002157SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2158SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2159SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2160SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161#undef SHOW_FUNCTION
2162
2163#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Al Viro3d1ab402006-03-18 18:35:43 -05002164static ssize_t __FUNC(elevator_t *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002166 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 unsigned int __data; \
2168 int ret = cfq_var_store(&__data, (page), count); \
2169 if (__data < (MIN)) \
2170 __data = (MIN); \
2171 else if (__data > (MAX)) \
2172 __data = (MAX); \
2173 if (__CONV) \
2174 *(__PTR) = msecs_to_jiffies(__data); \
2175 else \
2176 *(__PTR) = __data; \
2177 return ret; \
2178}
2179STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002180STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1, UINT_MAX, 1);
2181STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1, UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002182STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
2183STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1, UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002184STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2185STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2186STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
2187STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1, UINT_MAX, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#undef STORE_FUNCTION
2189
Al Viroe572ec72006-03-18 22:27:18 -05002190#define CFQ_ATTR(name) \
2191 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002192
Al Viroe572ec72006-03-18 22:27:18 -05002193static struct elv_fs_entry cfq_attrs[] = {
2194 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002195 CFQ_ATTR(fifo_expire_sync),
2196 CFQ_ATTR(fifo_expire_async),
2197 CFQ_ATTR(back_seek_max),
2198 CFQ_ATTR(back_seek_penalty),
2199 CFQ_ATTR(slice_sync),
2200 CFQ_ATTR(slice_async),
2201 CFQ_ATTR(slice_async_rq),
2202 CFQ_ATTR(slice_idle),
Al Viroe572ec72006-03-18 22:27:18 -05002203 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204};
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206static struct elevator_type iosched_cfq = {
2207 .ops = {
2208 .elevator_merge_fn = cfq_merge,
2209 .elevator_merged_fn = cfq_merged_request,
2210 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002211 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002212 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002214 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 .elevator_deactivate_req_fn = cfq_deactivate_request,
2216 .elevator_queue_empty_fn = cfq_queue_empty,
2217 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002218 .elevator_former_req_fn = elv_rb_former_request,
2219 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 .elevator_set_req_fn = cfq_set_request,
2221 .elevator_put_req_fn = cfq_put_request,
2222 .elevator_may_queue_fn = cfq_may_queue,
2223 .elevator_init_fn = cfq_init_queue,
2224 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002225 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 },
Al Viro3d1ab402006-03-18 18:35:43 -05002227 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 .elevator_name = "cfq",
2229 .elevator_owner = THIS_MODULE,
2230};
2231
2232static int __init cfq_init(void)
2233{
2234 int ret;
2235
Jens Axboe22e2c502005-06-27 10:55:12 +02002236 /*
2237 * could be 0 on HZ < 1000 setups
2238 */
2239 if (!cfq_slice_async)
2240 cfq_slice_async = 1;
2241 if (!cfq_slice_idle)
2242 cfq_slice_idle = 1;
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (cfq_slab_setup())
2245 return -ENOMEM;
2246
2247 ret = elv_register(&iosched_cfq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002248 if (ret)
2249 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return ret;
2252}
2253
2254static void __exit cfq_exit(void)
2255{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002256 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002258 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002259 /* ioc_gone's update must be visible before reading ioc_count */
2260 smp_wmb();
Jens Axboe4050cf12006-07-19 05:07:12 +02002261 if (elv_ioc_count_read(ioc_count))
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002262 wait_for_completion(ioc_gone);
Al Viro334e94d2006-03-18 15:05:53 -05002263 synchronize_rcu();
Christoph Hellwig83521d32005-10-30 15:01:39 -08002264 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267module_init(cfq_init);
2268module_exit(cfq_exit);
2269
2270MODULE_AUTHOR("Jens Axboe");
2271MODULE_LICENSE("GPL");
2272MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");