blob: 97d946585bc311dd63271dac6e8fcdedcd9f5970 [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/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020013#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020014#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/*
17 * tunables
18 */
Jens Axboefe094d92008-01-31 13:08:54 +010019/* max queue in one round of service */
20static const int cfq_quantum = 4;
Arjan van de Ven64100092006-01-06 09:46:02 +010021static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010022/* maximum backwards seek, in KiB */
23static const int cfq_back_max = 16 * 1024;
24/* penalty of a backwards seek */
25static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020027static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010028static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020029static int cfq_slice_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010030static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
31static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020032
Jens Axboed9e76202007-04-20 14:27:50 +020033/*
Jens Axboe08717142008-01-28 11:38:15 +010034 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020035 */
Jens Axboe08717142008-01-28 11:38:15 +010036#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020037
38/*
39 * below this threshold, we consider thinktime immediate
40 */
41#define CFQ_MIN_TT (2)
42
Jeff Moyere6c5bc72009-10-23 17:14:52 -040043/*
44 * Allow merged cfqqs to perform this amount of seeky I/O before
45 * deciding to break the queues up again.
46 */
47#define CFQQ_COOP_TOUT (HZ)
48
Jens Axboe22e2c502005-06-27 10:55:12 +020049#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020050#define CFQ_HW_QUEUE_MIN (5)
Jens Axboe22e2c502005-06-27 10:55:12 +020051
Jens Axboefe094d92008-01-31 13:08:54 +010052#define RQ_CIC(rq) \
53 ((struct cfq_io_context *) (rq)->elevator_private)
Jens Axboe7b679132008-05-30 12:23:07 +020054#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Lametere18b8902006-12-06 20:33:20 -080056static struct kmem_cache *cfq_pool;
57static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Tejun Heo245b2e72009-06-24 15:13:48 +090059static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050060static struct completion *ioc_gone;
Jens Axboe9a11b4e2008-05-29 09:32:08 +020061static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -050062
Jens Axboe22e2c502005-06-27 10:55:12 +020063#define CFQ_PRIO_LISTS IOPRIO_BE_NR
64#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
66
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 Axboe6118b702009-06-30 09:34:12 +020082 * Per process-grouping structure
83 */
84struct cfq_queue {
85 /* reference count */
86 atomic_t ref;
87 /* various state flags, see below */
88 unsigned int flags;
89 /* parent cfq_data */
90 struct cfq_data *cfqd;
91 /* service_tree member */
92 struct rb_node rb_node;
93 /* service_tree key */
94 unsigned long rb_key;
95 /* prio tree member */
96 struct rb_node p_node;
97 /* prio tree root we belong to, if any */
98 struct rb_root *p_root;
99 /* sorted list of pending requests */
100 struct rb_root sort_list;
101 /* if fifo isn't expired, next request to serve */
102 struct request *next_rq;
103 /* requests queued in sort_list */
104 int queued[2];
105 /* currently allocated requests */
106 int allocated[2];
107 /* fifo list of requests in sort_list */
108 struct list_head fifo;
109
110 unsigned long slice_end;
111 long slice_resid;
112 unsigned int slice_dispatch;
113
114 /* pending metadata requests */
115 int meta_pending;
116 /* number of requests that are on the dispatch list or inside driver */
117 int dispatched;
118
119 /* io prio of this group */
120 unsigned short ioprio, org_ioprio;
121 unsigned short ioprio_class, org_ioprio_class;
122
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400123 unsigned int seek_samples;
124 u64 seek_total;
125 sector_t seek_mean;
126 sector_t last_request_pos;
Jeff Moyere6c5bc72009-10-23 17:14:52 -0400127 unsigned long seeky_start;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400128
Jens Axboe6118b702009-06-30 09:34:12 +0200129 pid_t pid;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400130
131 struct cfq_queue *new_cfqq;
Jens Axboe6118b702009-06-30 09:34:12 +0200132};
133
134/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200135 * Per block device queue structure
136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct cfq_data {
Jens Axboe165125e2007-07-24 09:28:11 +0200138 struct request_queue *queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200139
140 /*
141 * rr list of queues with requests and the count of them
142 */
Jens Axboecc09e292007-04-26 12:53:50 +0200143 struct cfq_rb_root service_tree;
Jens Axboea36e71f2009-04-15 12:15:11 +0200144
145 /*
146 * Each priority tree is sorted by next_request position. These
147 * trees are used when determining if two or more queues are
148 * interleaving requests (see cfq_close_cooperator).
149 */
150 struct rb_root prio_trees[CFQ_PRIO_LISTS];
151
Jens Axboe22e2c502005-06-27 10:55:12 +0200152 unsigned int busy_queues;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100153 unsigned int busy_rt_queues;
154 unsigned int busy_queues_avg[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200155
Jens Axboe5ad531d2009-07-03 12:57:48 +0200156 int rq_in_driver[2];
Jens Axboe3ed9a292007-04-23 08:33:33 +0200157 int sync_flight;
Aaron Carroll45333d52008-08-26 15:52:36 +0200158
159 /*
160 * queue-depth detection
161 */
162 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200163 int hw_tag;
Aaron Carroll45333d52008-08-26 15:52:36 +0200164 int hw_tag_samples;
165 int rq_in_driver_peak;
Jens Axboe22e2c502005-06-27 10:55:12 +0200166
167 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200168 * idle window management
169 */
170 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200171 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200172
173 struct cfq_queue *active_queue;
174 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200175
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200176 /*
177 * async queue for each priority case
178 */
179 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
180 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200181
Jens Axboe6d048f52007-04-25 12:44:27 +0200182 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
185 * tunables, see top of file
186 */
187 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200188 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned int cfq_back_penalty;
190 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200191 unsigned int cfq_slice[2];
192 unsigned int cfq_slice_async_rq;
193 unsigned int cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200194 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500195
196 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jens Axboe6118b702009-06-30 09:34:12 +0200198 /*
199 * Fallback dummy cfqq for extreme OOM conditions
200 */
201 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200202
203 unsigned long last_end_sync_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Jens Axboe3b181522005-06-27 10:56:24 +0200206enum cfqq_state_flags {
Jens Axboeb0b8d742007-01-19 11:35:30 +1100207 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
208 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200209 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d742007-01-19 11:35:30 +1100210 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d742007-01-19 11:35:30 +1100211 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
212 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
213 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100214 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200215 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400216 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Jens Axboe3b181522005-06-27 10:56:24 +0200217};
218
219#define CFQ_CFQQ_FNS(name) \
220static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
221{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100222 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200223} \
224static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
225{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100226 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200227} \
228static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
229{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100230 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200231}
232
233CFQ_CFQQ_FNS(on_rr);
234CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200235CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200236CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200237CFQ_CFQQ_FNS(fifo_expire);
238CFQ_CFQQ_FNS(idle_window);
239CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100240CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200241CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200242CFQ_CFQQ_FNS(coop);
Jens Axboe3b181522005-06-27 10:56:24 +0200243#undef CFQ_CFQQ_FNS
244
Jens Axboe7b679132008-05-30 12:23:07 +0200245#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
246 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
247#define cfq_log(cfqd, fmt, args...) \
248 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
249
Jens Axboe165125e2007-07-24 09:28:11 +0200250static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200251static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100252 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100253static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200254 struct io_context *);
255
Jens Axboe5ad531d2009-07-03 12:57:48 +0200256static inline int rq_in_driver(struct cfq_data *cfqd)
257{
258 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
259}
260
Vasily Tarasov91fac312007-04-25 12:29:51 +0200261static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200262 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200263{
Jens Axboea6151c32009-10-07 20:02:57 +0200264 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200265}
266
267static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200268 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200269{
Jens Axboea6151c32009-10-07 20:02:57 +0200270 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200271}
272
273/*
274 * We regard a request as SYNC, if it's either a read or has the SYNC bit
275 * set (in which case it could also be direct WRITE).
276 */
Jens Axboea6151c32009-10-07 20:02:57 +0200277static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200278{
Jens Axboea6151c32009-10-07 20:02:57 +0200279 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700283 * scheduler run of queue, if there are requests pending and no one in the
284 * driver that will restart queueing
285 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200286static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700287{
Jens Axboe7b679132008-05-30 12:23:07 +0200288 if (cfqd->busy_queues) {
289 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200290 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200291 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700292}
293
Jens Axboe165125e2007-07-24 09:28:11 +0200294static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700295{
296 struct cfq_data *cfqd = q->elevator->elevator_data;
297
Jens Axboeb4878f22005-10-20 16:42:29 +0200298 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100302 * Scale schedule slice based on io priority. Use the sync time slice only
303 * if a queue is marked sync and has sync io queued. A sync queue with async
304 * io only, should not get full sync slice length.
305 */
Jens Axboea6151c32009-10-07 20:02:57 +0200306static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200307 unsigned short prio)
308{
309 const int base_slice = cfqd->cfq_slice[sync];
310
311 WARN_ON(prio >= IOPRIO_BE_NR);
312
313 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
314}
315
Jens Axboe44f7c162007-01-19 11:51:58 +1100316static inline int
317cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
318{
Jens Axboed9e76202007-04-20 14:27:50 +0200319 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100320}
321
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100322/*
323 * get averaged number of queues of RT/BE priority.
324 * average is updated, with a formula that gives more weight to higher numbers,
325 * to quickly follows sudden increases and decrease slowly
326 */
327
328static inline unsigned
329cfq_get_avg_queues(struct cfq_data *cfqd, bool rt) {
330 unsigned min_q, max_q;
331 unsigned mult = cfq_hist_divisor - 1;
332 unsigned round = cfq_hist_divisor / 2;
333 unsigned busy = cfqd->busy_rt_queues;
334
335 if (!rt)
336 busy = cfqd->busy_queues - cfqd->busy_rt_queues;
337
338 min_q = min(cfqd->busy_queues_avg[rt], busy);
339 max_q = max(cfqd->busy_queues_avg[rt], busy);
340 cfqd->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
341 cfq_hist_divisor;
342 return cfqd->busy_queues_avg[rt];
343}
344
Jens Axboe44f7c162007-01-19 11:51:58 +1100345static inline void
346cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
347{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100348 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
349 if (cfqd->cfq_latency) {
350 /* interested queues (we consider only the ones with the same
351 * priority class) */
352 unsigned iq = cfq_get_avg_queues(cfqd, cfq_class_rt(cfqq));
353 unsigned sync_slice = cfqd->cfq_slice[1];
354 unsigned expect_latency = sync_slice * iq;
355 if (expect_latency > cfq_target_latency) {
356 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
357 /* scale low_slice according to IO priority
358 * and sync vs async */
359 unsigned low_slice =
360 min(slice, base_low_slice * slice / sync_slice);
361 /* the adapted slice value is scaled to fit all iqs
362 * into the target latency */
363 slice = max(slice * cfq_target_latency / expect_latency,
364 low_slice);
365 }
366 }
367 cfqq->slice_end = jiffies + slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200368 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100369}
370
371/*
372 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
373 * isn't valid until the first request from the dispatch is activated
374 * and the slice time set.
375 */
Jens Axboea6151c32009-10-07 20:02:57 +0200376static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100377{
378 if (cfq_cfqq_slice_new(cfqq))
379 return 0;
380 if (time_before(jiffies, cfqq->slice_end))
381 return 0;
382
383 return 1;
384}
385
386/*
Jens Axboe5e705372006-07-13 12:39:25 +0200387 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200389 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
Jens Axboe5e705372006-07-13 12:39:25 +0200391static struct request *
392cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 sector_t last, s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200396#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
397#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
398 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jens Axboe5e705372006-07-13 12:39:25 +0200400 if (rq1 == NULL || rq1 == rq2)
401 return rq2;
402 if (rq2 == NULL)
403 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200404
Jens Axboe5e705372006-07-13 12:39:25 +0200405 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
406 return rq1;
407 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
408 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200409 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
410 return rq1;
411 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
412 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Tejun Heo83096eb2009-05-07 22:24:39 +0900414 s1 = blk_rq_pos(rq1);
415 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jens Axboe6d048f52007-04-25 12:44:27 +0200417 last = cfqd->last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * by definition, 1KiB is 2 sectors
421 */
422 back_max = cfqd->cfq_back_max * 2;
423
424 /*
425 * Strict one way elevator _except_ in the case where we allow
426 * short backward seeks which are biased as twice the cost of a
427 * similar forward seek.
428 */
429 if (s1 >= last)
430 d1 = s1 - last;
431 else if (s1 + back_max >= last)
432 d1 = (last - s1) * cfqd->cfq_back_penalty;
433 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200434 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (s2 >= last)
437 d2 = s2 - last;
438 else if (s2 + back_max >= last)
439 d2 = (last - s2) * cfqd->cfq_back_penalty;
440 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200441 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Andreas Mohre8a99052006-03-28 08:59:49 +0200445 /*
446 * By doing switch() on the bit mask "wrap" we avoid having to
447 * check two variables for all permutations: --> faster!
448 */
449 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200450 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200451 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200452 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200453 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200454 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200455 else {
456 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200457 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200458 else
Jens Axboe5e705372006-07-13 12:39:25 +0200459 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200460 }
461
462 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200463 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200464 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200465 return rq2;
466 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200467 default:
468 /*
469 * Since both rqs are wrapped,
470 * start with the one that's further behind head
471 * (--> only *one* back seek required),
472 * since back seek takes more time than forward.
473 */
474 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200475 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
Jens Axboe5e705372006-07-13 12:39:25 +0200477 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479}
480
Jens Axboe498d3aa2007-04-26 12:54:48 +0200481/*
482 * The below is leftmost cache rbtree addon
483 */
Jens Axboe08717142008-01-28 11:38:15 +0100484static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200485{
486 if (!root->left)
487 root->left = rb_first(&root->rb);
488
Jens Axboe08717142008-01-28 11:38:15 +0100489 if (root->left)
490 return rb_entry(root->left, struct cfq_queue, rb_node);
491
492 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200493}
494
Jens Axboea36e71f2009-04-15 12:15:11 +0200495static void rb_erase_init(struct rb_node *n, struct rb_root *root)
496{
497 rb_erase(n, root);
498 RB_CLEAR_NODE(n);
499}
500
Jens Axboecc09e292007-04-26 12:53:50 +0200501static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
502{
503 if (root->left == n)
504 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200505 rb_erase_init(n, &root->rb);
Jens Axboecc09e292007-04-26 12:53:50 +0200506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
509 * would be nice to take fifo expire time into account as well
510 */
Jens Axboe5e705372006-07-13 12:39:25 +0200511static struct request *
512cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
513 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jens Axboe21183b02006-07-13 12:33:14 +0200515 struct rb_node *rbnext = rb_next(&last->rb_node);
516 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200517 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Jens Axboe21183b02006-07-13 12:33:14 +0200519 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200522 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200525 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200526 else {
527 rbnext = rb_first(&cfqq->sort_list);
528 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200529 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jens Axboe21183b02006-07-13 12:33:14 +0200532 return cfq_choose_req(cfqd, next, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Jens Axboed9e76202007-04-20 14:27:50 +0200535static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
536 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Jens Axboed9e76202007-04-20 14:27:50 +0200538 /*
539 * just an approximation, should be ok.
540 */
Jens Axboe67e6b492007-04-20 14:18:00 +0200541 return (cfqd->busy_queues - 1) * (cfq_prio_slice(cfqd, 1, 0) -
542 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200543}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jens Axboe498d3aa2007-04-26 12:54:48 +0200545/*
546 * The cfqd->service_tree holds all pending cfq_queue's that have
547 * requests waiting to be processed. It is sorted in the order that
548 * we will service the queues.
549 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200550static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +0200551 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +0200552{
Jens Axboe08717142008-01-28 11:38:15 +0100553 struct rb_node **p, *parent;
554 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +0200555 unsigned long rb_key;
Jens Axboe498d3aa2007-04-26 12:54:48 +0200556 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200557
Jens Axboe08717142008-01-28 11:38:15 +0100558 if (cfq_class_idle(cfqq)) {
559 rb_key = CFQ_IDLE_DELAY;
560 parent = rb_last(&cfqd->service_tree.rb);
561 if (parent && parent != &cfqq->rb_node) {
562 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
563 rb_key += __cfqq->rb_key;
564 } else
565 rb_key += jiffies;
566 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +0200567 /*
568 * Get our rb key offset. Subtract any residual slice
569 * value carried from last service. A negative resid
570 * count indicates slice overrun, and this should position
571 * the next service time further away in the tree.
572 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200573 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +0200574 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +0200575 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +0200576 } else {
577 rb_key = -HZ;
578 __cfqq = cfq_rb_first(&cfqd->service_tree);
579 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
580 }
Jens Axboed9e76202007-04-20 14:27:50 +0200581
582 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100583 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200584 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100585 */
Jens Axboed9e76202007-04-20 14:27:50 +0200586 if (rb_key == cfqq->rb_key)
587 return;
Jens Axboe53b03742006-07-28 09:48:51 +0200588
Jens Axboecc09e292007-04-26 12:53:50 +0200589 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboe22e2c502005-06-27 10:55:12 +0200590 }
Jens Axboed9e76202007-04-20 14:27:50 +0200591
Jens Axboe498d3aa2007-04-26 12:54:48 +0200592 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +0100593 parent = NULL;
594 p = &cfqd->service_tree.rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200595 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +0200596 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200597
Jens Axboed9e76202007-04-20 14:27:50 +0200598 parent = *p;
599 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
600
Jens Axboe0c534e02007-04-18 20:01:57 +0200601 /*
602 * sort RT queues first, we always want to give
Jens Axboe67060e32007-04-18 20:13:32 +0200603 * preference to them. IDLE queues goes to the back.
604 * after that, sort on the next service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200605 */
606 if (cfq_class_rt(cfqq) > cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200607 n = &(*p)->rb_left;
Jens Axboe0c534e02007-04-18 20:01:57 +0200608 else if (cfq_class_rt(cfqq) < cfq_class_rt(__cfqq))
Jens Axboe67060e32007-04-18 20:13:32 +0200609 n = &(*p)->rb_right;
610 else if (cfq_class_idle(cfqq) < cfq_class_idle(__cfqq))
611 n = &(*p)->rb_left;
612 else if (cfq_class_idle(cfqq) > cfq_class_idle(__cfqq))
613 n = &(*p)->rb_right;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +0200614 else if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +0200615 n = &(*p)->rb_left;
616 else
617 n = &(*p)->rb_right;
618
619 if (n == &(*p)->rb_right)
Jens Axboecc09e292007-04-26 12:53:50 +0200620 left = 0;
Jens Axboe67060e32007-04-18 20:13:32 +0200621
622 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200623 }
624
Jens Axboecc09e292007-04-26 12:53:50 +0200625 if (left)
626 cfqd->service_tree.left = &cfqq->rb_node;
627
Jens Axboed9e76202007-04-20 14:27:50 +0200628 cfqq->rb_key = rb_key;
629 rb_link_node(&cfqq->rb_node, parent, p);
Jens Axboecc09e292007-04-26 12:53:50 +0200630 rb_insert_color(&cfqq->rb_node, &cfqd->service_tree.rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Jens Axboea36e71f2009-04-15 12:15:11 +0200633static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200634cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
635 sector_t sector, struct rb_node **ret_parent,
636 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +0200637{
Jens Axboea36e71f2009-04-15 12:15:11 +0200638 struct rb_node **p, *parent;
639 struct cfq_queue *cfqq = NULL;
640
641 parent = NULL;
642 p = &root->rb_node;
643 while (*p) {
644 struct rb_node **n;
645
646 parent = *p;
647 cfqq = rb_entry(parent, struct cfq_queue, p_node);
648
649 /*
650 * Sort strictly based on sector. Smallest to the left,
651 * largest to the right.
652 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900653 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +0200654 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900655 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +0200656 n = &(*p)->rb_left;
657 else
658 break;
659 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200660 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200661 }
662
663 *ret_parent = parent;
664 if (rb_link)
665 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200666 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +0200667}
668
669static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
670{
Jens Axboea36e71f2009-04-15 12:15:11 +0200671 struct rb_node **p, *parent;
672 struct cfq_queue *__cfqq;
673
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200674 if (cfqq->p_root) {
675 rb_erase(&cfqq->p_node, cfqq->p_root);
676 cfqq->p_root = NULL;
677 }
Jens Axboea36e71f2009-04-15 12:15:11 +0200678
679 if (cfq_class_idle(cfqq))
680 return;
681 if (!cfqq->next_rq)
682 return;
683
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200684 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900685 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
686 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200687 if (!__cfqq) {
688 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200689 rb_insert_color(&cfqq->p_node, cfqq->p_root);
690 } else
691 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200692}
693
Jens Axboe498d3aa2007-04-26 12:54:48 +0200694/*
695 * Update cfqq's position in the service tree.
696 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200697static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200698{
Jens Axboe6d048f52007-04-25 12:44:27 +0200699 /*
700 * Resorting requires the cfqq to be on the RR list already.
701 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200702 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +0200703 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +0200704 cfq_prio_tree_add(cfqd, cfqq);
705 }
Jens Axboe6d048f52007-04-25 12:44:27 +0200706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200710 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Jens Axboefebffd62008-01-28 13:19:43 +0100712static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Jens Axboe7b679132008-05-30 12:23:07 +0200714 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +0200715 BUG_ON(cfq_cfqq_on_rr(cfqq));
716 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cfqd->busy_queues++;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100718 if (cfq_class_rt(cfqq))
719 cfqd->busy_rt_queues++;
Jens Axboeedd75ff2007-04-19 12:03:34 +0200720 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Jens Axboe498d3aa2007-04-26 12:54:48 +0200723/*
724 * Called when the cfqq no longer has requests pending, remove it from
725 * the service tree.
726 */
Jens Axboefebffd62008-01-28 13:19:43 +0100727static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Jens Axboe7b679132008-05-30 12:23:07 +0200729 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +0200730 BUG_ON(!cfq_cfqq_on_rr(cfqq));
731 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jens Axboecc09e292007-04-26 12:53:50 +0200733 if (!RB_EMPTY_NODE(&cfqq->rb_node))
734 cfq_rb_erase(&cfqq->rb_node, &cfqd->service_tree);
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200735 if (cfqq->p_root) {
736 rb_erase(&cfqq->p_node, cfqq->p_root);
737 cfqq->p_root = NULL;
738 }
Jens Axboed9e76202007-04-20 14:27:50 +0200739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 BUG_ON(!cfqd->busy_queues);
741 cfqd->busy_queues--;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100742 if (cfq_class_rt(cfqq))
743 cfqd->busy_rt_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746/*
747 * rb tree support functions
748 */
Jens Axboefebffd62008-01-28 13:19:43 +0100749static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Jens Axboe5e705372006-07-13 12:39:25 +0200751 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200752 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200753 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jens Axboeb4878f22005-10-20 16:42:29 +0200755 BUG_ON(!cfqq->queued[sync]);
756 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Jens Axboe5e705372006-07-13 12:39:25 +0200758 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Jens Axboedd67d052006-06-21 09:36:18 +0200760 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200761 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Jens Axboe5e705372006-07-13 12:39:25 +0200764static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Jens Axboe5e705372006-07-13 12:39:25 +0200766 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +0200768 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jens Axboe5380a102006-07-13 12:37:56 +0200770 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /*
773 * looks a little odd, but the first insert might return an alias.
774 * if that happens, put the alias on the dispatch list
775 */
Jens Axboe21183b02006-07-13 12:33:14 +0200776 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200777 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100778
779 if (!cfq_cfqq_on_rr(cfqq))
780 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200781
782 /*
783 * check if this request is a better next-serve candidate
784 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200785 prev = cfqq->next_rq;
Jens Axboe5044eed2007-04-25 11:53:48 +0200786 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq);
Jens Axboea36e71f2009-04-15 12:15:11 +0200787
788 /*
789 * adjust priority tree position, if ->next_rq changes
790 */
791 if (prev != cfqq->next_rq)
792 cfq_prio_tree_add(cfqd, cfqq);
793
Jens Axboe5044eed2007-04-25 11:53:48 +0200794 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Jens Axboefebffd62008-01-28 13:19:43 +0100797static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jens Axboe5380a102006-07-13 12:37:56 +0200799 elv_rb_del(&cfqq->sort_list, rq);
800 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200801 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Jens Axboe206dc692006-03-28 13:03:44 +0200804static struct request *
805cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Jens Axboe206dc692006-03-28 13:03:44 +0200807 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200808 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +0200809 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Jens Axboe4ac845a2008-01-24 08:44:49 +0100811 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200812 if (!cic)
813 return NULL;
814
815 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +0200816 if (cfqq) {
817 sector_t sector = bio->bi_sector + bio_sectors(bio);
818
Jens Axboe21183b02006-07-13 12:33:14 +0200819 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return NULL;
823}
824
Jens Axboe165125e2007-07-24 09:28:11 +0200825static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +0200826{
827 struct cfq_data *cfqd = q->elevator->elevator_data;
828
Jens Axboe5ad531d2009-07-03 12:57:48 +0200829 cfqd->rq_in_driver[rq_is_sync(rq)]++;
Jens Axboe7b679132008-05-30 12:23:07 +0200830 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +0200831 rq_in_driver(cfqd));
Jens Axboe25776e32006-06-01 10:12:26 +0200832
Tejun Heo5b936292009-05-07 22:24:38 +0900833 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200834}
835
Jens Axboe165125e2007-07-24 09:28:11 +0200836static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Jens Axboe22e2c502005-06-27 10:55:12 +0200838 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5ad531d2009-07-03 12:57:48 +0200839 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Jens Axboe5ad531d2009-07-03 12:57:48 +0200841 WARN_ON(!cfqd->rq_in_driver[sync]);
842 cfqd->rq_in_driver[sync]--;
Jens Axboe7b679132008-05-30 12:23:07 +0200843 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +0200844 rq_in_driver(cfqd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Jens Axboeb4878f22005-10-20 16:42:29 +0200847static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Jens Axboe5e705372006-07-13 12:39:25 +0200849 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200850
Jens Axboe5e705372006-07-13 12:39:25 +0200851 if (cfqq->next_rq == rq)
852 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Jens Axboeb4878f22005-10-20 16:42:29 +0200854 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200855 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200856
Aaron Carroll45333d52008-08-26 15:52:36 +0200857 cfqq->cfqd->rq_queued--;
Jens Axboe374f84a2006-07-23 01:42:19 +0200858 if (rq_is_meta(rq)) {
859 WARN_ON(!cfqq->meta_pending);
860 cfqq->meta_pending--;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Jens Axboe165125e2007-07-24 09:28:11 +0200864static int cfq_merge(struct request_queue *q, struct request **req,
865 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct cfq_data *cfqd = q->elevator->elevator_data;
868 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Jens Axboe206dc692006-03-28 13:03:44 +0200870 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200871 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200872 *req = __rq;
873 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Jens Axboe165125e2007-07-24 09:28:11 +0200879static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +0200880 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Jens Axboe21183b02006-07-13 12:33:14 +0200882 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200883 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jens Axboe5e705372006-07-13 12:39:25 +0200885 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889static void
Jens Axboe165125e2007-07-24 09:28:11 +0200890cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct request *next)
892{
Jens Axboe22e2c502005-06-27 10:55:12 +0200893 /*
894 * reposition in fifo if next is older than rq
895 */
896 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +0200897 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200898 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +0200899 rq_set_fifo_time(rq, rq_fifo_time(next));
900 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200901
Jens Axboeb4878f22005-10-20 16:42:29 +0200902 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200903}
904
Jens Axboe165125e2007-07-24 09:28:11 +0200905static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +0100906 struct bio *bio)
907{
908 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200909 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +0100910 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +0100911
912 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100913 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100914 */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200915 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +0200916 return false;
Jens Axboeda775262006-12-20 11:04:12 +0100917
918 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100919 * Lookup the cfqq that this bio will be queued with. Allow
920 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100921 */
Jens Axboe4ac845a2008-01-24 08:44:49 +0100922 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200923 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +0200924 return false;
Jens Axboe719d3402006-12-22 09:38:53 +0100925
Vasily Tarasov91fac312007-04-25 12:29:51 +0200926 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +0200927 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +0100928}
929
Jens Axboefebffd62008-01-28 13:19:43 +0100930static void __cfq_set_active_queue(struct cfq_data *cfqd,
931 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +0200932{
933 if (cfqq) {
Jens Axboe7b679132008-05-30 12:23:07 +0200934 cfq_log_cfqq(cfqd, cfqq, "set_active");
Jens Axboe22e2c502005-06-27 10:55:12 +0200935 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +0200936 cfqq->slice_dispatch = 0;
937
Jens Axboe2f5cb732009-04-07 08:51:19 +0200938 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +0200939 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +0200940 cfq_clear_cfqq_must_alloc_slice(cfqq);
941 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +1100942 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +0200943
944 del_timer(&cfqd->idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +0200945 }
946
947 cfqd->active_queue = cfqq;
948}
949
950/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100951 * current cfqq expired its slice (or was too idle), select new one
952 */
953static void
954__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +0200955 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100956{
Jens Axboe7b679132008-05-30 12:23:07 +0200957 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
958
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100959 if (cfq_cfqq_wait_request(cfqq))
960 del_timer(&cfqd->idle_slice_timer);
961
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100962 cfq_clear_cfqq_wait_request(cfqq);
963
964 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +0200965 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100966 */
Jens Axboe7b679132008-05-30 12:23:07 +0200967 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +1100968 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +0200969 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
970 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100971
Jens Axboeedd75ff2007-04-19 12:03:34 +0200972 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100973
974 if (cfqq == cfqd->active_queue)
975 cfqd->active_queue = NULL;
976
977 if (cfqd->active_cic) {
978 put_io_context(cfqd->active_cic->ioc);
979 cfqd->active_cic = NULL;
980 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100981}
982
Jens Axboea6151c32009-10-07 20:02:57 +0200983static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100984{
985 struct cfq_queue *cfqq = cfqd->active_queue;
986
987 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +0200988 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +0100989}
990
Jens Axboe498d3aa2007-04-26 12:54:48 +0200991/*
992 * Get next queue for service. Unless we have a queue preemption,
993 * we'll simply select the first cfqq in the service tree.
994 */
Jens Axboe6d048f52007-04-25 12:44:27 +0200995static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +0200996{
Jens Axboeedd75ff2007-04-19 12:03:34 +0200997 if (RB_EMPTY_ROOT(&cfqd->service_tree.rb))
998 return NULL;
999
Jens Axboe08717142008-01-28 11:38:15 +01001000 return cfq_rb_first(&cfqd->service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001001}
1002
Jens Axboe498d3aa2007-04-26 12:54:48 +02001003/*
1004 * Get and set a new active queue for service.
1005 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001006static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1007 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001008{
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001009 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001010 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001011
Jens Axboe22e2c502005-06-27 10:55:12 +02001012 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001013 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001014}
1015
Jens Axboed9e76202007-04-20 14:27:50 +02001016static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1017 struct request *rq)
1018{
Tejun Heo83096eb2009-05-07 22:24:39 +09001019 if (blk_rq_pos(rq) >= cfqd->last_position)
1020 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001021 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001022 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001023}
1024
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001025#define CFQQ_SEEK_THR 8 * 1024
1026#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001027
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001028static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1029 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001030{
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001031 sector_t sdist = cfqq->seek_mean;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001032
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001033 if (!sample_valid(cfqq->seek_samples))
1034 sdist = CFQQ_SEEK_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001035
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001036 return cfq_dist_from_last(cfqd, rq) <= sdist;
Jens Axboe6d048f52007-04-25 12:44:27 +02001037}
1038
Jens Axboea36e71f2009-04-15 12:15:11 +02001039static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1040 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001041{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001042 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001043 struct rb_node *parent, *node;
1044 struct cfq_queue *__cfqq;
1045 sector_t sector = cfqd->last_position;
1046
1047 if (RB_EMPTY_ROOT(root))
1048 return NULL;
1049
1050 /*
1051 * First, if we find a request starting at the end of the last
1052 * request, choose it.
1053 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001054 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001055 if (__cfqq)
1056 return __cfqq;
1057
1058 /*
1059 * If the exact sector wasn't found, the parent of the NULL leaf
1060 * will contain the closest sector.
1061 */
1062 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001063 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001064 return __cfqq;
1065
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001066 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001067 node = rb_next(&__cfqq->p_node);
1068 else
1069 node = rb_prev(&__cfqq->p_node);
1070 if (!node)
1071 return NULL;
1072
1073 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001074 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001075 return __cfqq;
1076
1077 return NULL;
1078}
1079
1080/*
1081 * cfqd - obvious
1082 * cur_cfqq - passed in so that we don't decide that the current queue is
1083 * closely cooperating with itself.
1084 *
1085 * So, basically we're assuming that that cur_cfqq has dispatched at least
1086 * one request, and that cfqd->last_position reflects a position on the disk
1087 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1088 * assumption.
1089 */
1090static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001091 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001092{
1093 struct cfq_queue *cfqq;
1094
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001095 if (!cfq_cfqq_sync(cur_cfqq))
1096 return NULL;
1097 if (CFQQ_SEEKY(cur_cfqq))
1098 return NULL;
1099
Jens Axboea36e71f2009-04-15 12:15:11 +02001100 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001101 * We should notice if some of the queues are cooperating, eg
1102 * working closely on the same area of the disk. In that case,
1103 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001104 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001105 cfqq = cfqq_close(cfqd, cur_cfqq);
1106 if (!cfqq)
1107 return NULL;
1108
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001109 /*
1110 * It only makes sense to merge sync queues.
1111 */
1112 if (!cfq_cfqq_sync(cfqq))
1113 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001114 if (CFQQ_SEEKY(cfqq))
1115 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001116
Jens Axboea36e71f2009-04-15 12:15:11 +02001117 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001118}
1119
Jens Axboe6d048f52007-04-25 12:44:27 +02001120static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001121{
Jens Axboe17926692007-01-19 11:59:30 +11001122 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001123 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001124 unsigned long sl;
1125
Jens Axboea68bbdd2008-09-24 13:03:33 +02001126 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001127 * SSD device without seek penalty, disable idling. But only do so
1128 * for devices that support queuing, otherwise we still have a problem
1129 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001130 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001131 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001132 return;
1133
Jens Axboedd67d052006-06-21 09:36:18 +02001134 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001135 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001136
1137 /*
1138 * idle is disabled, either manually or by past process history
1139 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001140 if (!cfqd->cfq_slice_idle || !cfq_cfqq_idle_window(cfqq))
1141 return;
1142
Jens Axboe22e2c502005-06-27 10:55:12 +02001143 /*
Jens Axboe7b679132008-05-30 12:23:07 +02001144 * still requests with the driver, don't idle
1145 */
Jens Axboe5ad531d2009-07-03 12:57:48 +02001146 if (rq_in_driver(cfqd))
Jens Axboe7b679132008-05-30 12:23:07 +02001147 return;
1148
1149 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001150 * task has exited, don't wait
1151 */
Jens Axboe206dc692006-03-28 13:03:44 +02001152 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001153 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001154 return;
1155
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001156 /*
1157 * If our average think time is larger than the remaining time
1158 * slice, then don't idle. This avoids overrunning the allotted
1159 * time slice.
1160 */
1161 if (sample_valid(cic->ttime_samples) &&
1162 (cfqq->slice_end - jiffies < cic->ttime_mean))
1163 return;
1164
Jens Axboe3b181522005-06-27 10:56:24 +02001165 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001166
Jens Axboe206dc692006-03-28 13:03:44 +02001167 /*
1168 * we don't want to idle for seeks, but we do want to allow
1169 * fair distribution of slice time for a process doing back-to-back
1170 * seeks. so allow a little bit of time for him to submit a new rq
1171 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001172 sl = cfqd->cfq_slice_idle;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001173 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
Jens Axboed9e76202007-04-20 14:27:50 +02001174 sl = min(sl, msecs_to_jiffies(CFQ_MIN_TT));
Jens Axboe206dc692006-03-28 13:03:44 +02001175
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001176 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001177 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Jens Axboe498d3aa2007-04-26 12:54:48 +02001180/*
1181 * Move request from internal lists to the request queue dispatch list.
1182 */
Jens Axboe165125e2007-07-24 09:28:11 +02001183static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001185 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001186 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001187
Jens Axboe7b679132008-05-30 12:23:07 +02001188 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1189
Jeff Moyer06d21882009-09-11 17:08:59 +02001190 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001191 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001192 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001193 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001194
1195 if (cfq_cfqq_sync(cfqq))
1196 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199/*
1200 * return expired entry, or NULL to just start from scratch in rbtree
1201 */
Jens Axboefebffd62008-01-28 13:19:43 +01001202static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jens Axboe30996f42009-10-05 11:03:39 +02001204 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jens Axboe3b181522005-06-27 10:56:24 +02001206 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001208
1209 cfq_mark_cfqq_fifo_expire(cfqq);
1210
Jens Axboe89850f72006-07-22 16:48:31 +02001211 if (list_empty(&cfqq->fifo))
1212 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jens Axboe89850f72006-07-22 16:48:31 +02001214 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001215 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001216 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Jens Axboe30996f42009-10-05 11:03:39 +02001218 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001219 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Jens Axboe22e2c502005-06-27 10:55:12 +02001222static inline int
1223cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1224{
1225 const int base_rq = cfqd->cfq_slice_async_rq;
1226
1227 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1228
1229 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1230}
1231
1232/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001233 * Must be called with the queue_lock held.
1234 */
1235static int cfqq_process_refs(struct cfq_queue *cfqq)
1236{
1237 int process_refs, io_refs;
1238
1239 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1240 process_refs = atomic_read(&cfqq->ref) - io_refs;
1241 BUG_ON(process_refs < 0);
1242 return process_refs;
1243}
1244
1245static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1246{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001247 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001248 struct cfq_queue *__cfqq;
1249
1250 /* Avoid a circular list and skip interim queue merges */
1251 while ((__cfqq = new_cfqq->new_cfqq)) {
1252 if (__cfqq == cfqq)
1253 return;
1254 new_cfqq = __cfqq;
1255 }
1256
1257 process_refs = cfqq_process_refs(cfqq);
1258 /*
1259 * If the process for the cfqq has gone away, there is no
1260 * sense in merging the queues.
1261 */
1262 if (process_refs == 0)
1263 return;
1264
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001265 /*
1266 * Merge in the direction of the lesser amount of work.
1267 */
1268 new_process_refs = cfqq_process_refs(new_cfqq);
1269 if (new_process_refs >= process_refs) {
1270 cfqq->new_cfqq = new_cfqq;
1271 atomic_add(process_refs, &new_cfqq->ref);
1272 } else {
1273 new_cfqq->new_cfqq = cfqq;
1274 atomic_add(new_process_refs, &cfqq->ref);
1275 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001276}
1277
1278/*
Jens Axboe498d3aa2007-04-26 12:54:48 +02001279 * Select a queue for service. If we have a current active queue,
1280 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02001281 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001282static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001283{
Jens Axboea36e71f2009-04-15 12:15:11 +02001284 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001285
1286 cfqq = cfqd->active_queue;
1287 if (!cfqq)
1288 goto new_queue;
1289
1290 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001291 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02001292 */
Jens Axboeb0291952009-04-07 11:38:31 +02001293 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001294 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001295
1296 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001297 * The active queue has requests and isn't expired, allow it to
1298 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02001299 */
Jens Axboedd67d052006-06-21 09:36:18 +02001300 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001301 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02001302
1303 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02001304 * If another queue has a request waiting within our mean seek
1305 * distance, let it run. The expire code will check for close
1306 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001307 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02001308 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001309 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001310 if (new_cfqq) {
1311 if (!cfqq->new_cfqq)
1312 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02001313 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001314 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001315
1316 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001317 * No requests pending. If the active queue still has requests in
1318 * flight or is idling for a new request, allow either of these
1319 * conditions to happen (or time out) before selecting a new queue.
1320 */
Jens Axboecc197472007-04-20 20:45:39 +02001321 if (timer_pending(&cfqd->idle_slice_timer) ||
1322 (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001323 cfqq = NULL;
1324 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001325 }
1326
Jens Axboe3b181522005-06-27 10:56:24 +02001327expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02001328 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001329new_queue:
Jens Axboea36e71f2009-04-15 12:15:11 +02001330 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001331keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001332 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001333}
1334
Jens Axboefebffd62008-01-28 13:19:43 +01001335static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02001336{
1337 int dispatched = 0;
1338
1339 while (cfqq->next_rq) {
1340 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1341 dispatched++;
1342 }
1343
1344 BUG_ON(!list_empty(&cfqq->fifo));
1345 return dispatched;
1346}
1347
Jens Axboe498d3aa2007-04-26 12:54:48 +02001348/*
1349 * Drain our current requests. Used for barriers and when switching
1350 * io schedulers on-the-fly.
1351 */
Jens Axboed9e76202007-04-20 14:27:50 +02001352static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001353{
Jens Axboe08717142008-01-28 11:38:15 +01001354 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001355 int dispatched = 0;
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001356
Jens Axboe08717142008-01-28 11:38:15 +01001357 while ((cfqq = cfq_rb_first(&cfqd->service_tree)) != NULL)
Jens Axboed9e76202007-04-20 14:27:50 +02001358 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001359
Jens Axboe6084cdd2007-04-23 08:25:00 +02001360 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001361
1362 BUG_ON(cfqd->busy_queues);
1363
Jeff Moyer69237152009-06-12 15:29:30 +02001364 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001365 return dispatched;
1366}
1367
Jens Axboe0b182d62009-10-06 20:49:37 +02001368static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02001369{
Jens Axboe2f5cb732009-04-07 08:51:19 +02001370 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Jens Axboe2f5cb732009-04-07 08:51:19 +02001372 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02001373 * Drain async requests before we start sync IO
1374 */
1375 if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02001376 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02001377
1378 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02001379 * If this is an async queue and we have sync IO in flight, let it wait
1380 */
1381 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02001382 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001383
1384 max_dispatch = cfqd->cfq_quantum;
1385 if (cfq_class_idle(cfqq))
1386 max_dispatch = 1;
1387
1388 /*
1389 * Does this cfqq already have too much IO in flight?
1390 */
1391 if (cfqq->dispatched >= max_dispatch) {
1392 /*
1393 * idle queue must always only have a single IO in flight
1394 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02001395 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02001396 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02001397
Jens Axboe2f5cb732009-04-07 08:51:19 +02001398 /*
1399 * We have other queues, don't allow more IO from this one
1400 */
1401 if (cfqd->busy_queues > 1)
Jens Axboe0b182d62009-10-06 20:49:37 +02001402 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11001403
Jens Axboe2f5cb732009-04-07 08:51:19 +02001404 /*
Jens Axboe8e296752009-10-03 16:26:03 +02001405 * Sole queue user, allow bigger slice
Vivek Goyal365722b2009-10-03 15:21:27 +02001406 */
Jens Axboe8e296752009-10-03 16:26:03 +02001407 max_dispatch *= 4;
1408 }
1409
1410 /*
1411 * Async queues must wait a bit before being allowed dispatch.
1412 * We also ramp up the dispatch depth gradually for async IO,
1413 * based on the last sync IO we serviced
1414 */
Jens Axboe963b72f2009-10-03 19:42:18 +02001415 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jens Axboe8e296752009-10-03 16:26:03 +02001416 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
1417 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02001418
Jens Axboe61f0c1d2009-10-03 19:46:03 +02001419 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02001420 if (!depth && !cfqq->dispatched)
1421 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02001422 if (depth < max_dispatch)
1423 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
Jens Axboe0b182d62009-10-06 20:49:37 +02001426 /*
1427 * If we're below the current max, allow a dispatch
1428 */
1429 return cfqq->dispatched < max_dispatch;
1430}
1431
1432/*
1433 * Dispatch a request from cfqq, moving them to the request queue
1434 * dispatch list.
1435 */
1436static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1437{
1438 struct request *rq;
1439
1440 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1441
1442 if (!cfq_may_dispatch(cfqd, cfqq))
1443 return false;
1444
1445 /*
1446 * follow expired path, else get first next available
1447 */
1448 rq = cfq_check_fifo(cfqq);
1449 if (!rq)
1450 rq = cfqq->next_rq;
1451
1452 /*
1453 * insert request into driver dispatch list
1454 */
1455 cfq_dispatch_insert(cfqd->queue, rq);
1456
1457 if (!cfqd->active_cic) {
1458 struct cfq_io_context *cic = RQ_CIC(rq);
1459
1460 atomic_long_inc(&cic->ioc->refcount);
1461 cfqd->active_cic = cic;
1462 }
1463
1464 return true;
1465}
1466
1467/*
1468 * Find the cfqq that we need to service and move a request from that to the
1469 * dispatch list
1470 */
1471static int cfq_dispatch_requests(struct request_queue *q, int force)
1472{
1473 struct cfq_data *cfqd = q->elevator->elevator_data;
1474 struct cfq_queue *cfqq;
1475
1476 if (!cfqd->busy_queues)
1477 return 0;
1478
1479 if (unlikely(force))
1480 return cfq_forced_dispatch(cfqd);
1481
1482 cfqq = cfq_select_queue(cfqd);
1483 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02001484 return 0;
1485
Jens Axboe2f5cb732009-04-07 08:51:19 +02001486 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02001487 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02001488 */
Jens Axboe0b182d62009-10-06 20:49:37 +02001489 if (!cfq_dispatch_request(cfqd, cfqq))
1490 return 0;
1491
Jens Axboe2f5cb732009-04-07 08:51:19 +02001492 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02001493 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001494
1495 /*
1496 * expire an async queue immediately if it has used up its slice. idle
1497 * queue always expire after 1 dispatch round.
1498 */
1499 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1500 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1501 cfq_class_idle(cfqq))) {
1502 cfqq->slice_end = jiffies + 1;
1503 cfq_slice_expired(cfqd, 0);
1504 }
1505
Shan Weib217a902009-09-01 10:06:42 +02001506 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02001507 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/*
Jens Axboe5e705372006-07-13 12:39:25 +02001511 * task holds one reference to the queue, dropped when task exits. each rq
1512 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 *
1514 * queue lock must be held here.
1515 */
1516static void cfq_put_queue(struct cfq_queue *cfqq)
1517{
Jens Axboe22e2c502005-06-27 10:55:12 +02001518 struct cfq_data *cfqd = cfqq->cfqd;
1519
1520 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (!atomic_dec_and_test(&cfqq->ref))
1523 return;
1524
Jens Axboe7b679132008-05-30 12:23:07 +02001525 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001527 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001528 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001530 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001531 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02001532 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001533 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kmem_cache_free(cfq_pool, cfqq);
1536}
1537
Jens Axboed6de8be2008-05-28 14:46:59 +02001538/*
1539 * Must always be called with the rcu_read_lock() held
1540 */
Jens Axboe07416d22008-05-07 09:17:12 +02001541static void
1542__call_for_each_cic(struct io_context *ioc,
1543 void (*func)(struct io_context *, struct cfq_io_context *))
1544{
1545 struct cfq_io_context *cic;
1546 struct hlist_node *n;
1547
1548 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
1549 func(ioc, cic);
1550}
1551
Jens Axboe4ac845a2008-01-24 08:44:49 +01001552/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001553 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01001554 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001555static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01001556call_for_each_cic(struct io_context *ioc,
1557 void (*func)(struct io_context *, struct cfq_io_context *))
1558{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001559 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02001560 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001561 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001562}
Jens Axboe4ac845a2008-01-24 08:44:49 +01001563
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001564static void cfq_cic_free_rcu(struct rcu_head *head)
1565{
1566 struct cfq_io_context *cic;
1567
1568 cic = container_of(head, struct cfq_io_context, rcu_head);
1569
1570 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09001571 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001572
Jens Axboe9a11b4e2008-05-29 09:32:08 +02001573 if (ioc_gone) {
1574 /*
1575 * CFQ scheduler is exiting, grab exit lock and check
1576 * the pending io context count. If it hits zero,
1577 * complete ioc_gone and set it back to NULL
1578 */
1579 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09001580 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02001581 complete(ioc_gone);
1582 ioc_gone = NULL;
1583 }
1584 spin_unlock(&ioc_gone_lock);
1585 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001586}
1587
1588static void cfq_cic_free(struct cfq_io_context *cic)
1589{
1590 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001591}
1592
1593static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
1594{
1595 unsigned long flags;
1596
1597 BUG_ON(!cic->dead_key);
1598
1599 spin_lock_irqsave(&ioc->lock, flags);
1600 radix_tree_delete(&ioc->radix_root, cic->dead_key);
Jens Axboeffc4e752008-02-19 10:02:29 +01001601 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001602 spin_unlock_irqrestore(&ioc->lock, flags);
1603
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001604 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001605}
1606
Jens Axboed6de8be2008-05-28 14:46:59 +02001607/*
1608 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
1609 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
1610 * and ->trim() which is called with the task lock held
1611 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001612static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001614 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001615 * ioc->refcount is zero here, or we are called from elv_unregister(),
1616 * so no more cic's are allowed to be linked into this ioc. So it
1617 * should be ok to iterate over the known list, we will see all cic's
1618 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01001619 */
Jens Axboe07416d22008-05-07 09:17:12 +02001620 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Jens Axboe89850f72006-07-22 16:48:31 +02001623static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1624{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001625 struct cfq_queue *__cfqq, *next;
1626
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001627 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001628 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02001629 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001630 }
Jens Axboe89850f72006-07-22 16:48:31 +02001631
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001632 /*
1633 * If this queue was scheduled to merge with another queue, be
1634 * sure to drop the reference taken on that queue (and others in
1635 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
1636 */
1637 __cfqq = cfqq->new_cfqq;
1638 while (__cfqq) {
1639 if (__cfqq == cfqq) {
1640 WARN(1, "cfqq->new_cfqq loop detected\n");
1641 break;
1642 }
1643 next = __cfqq->new_cfqq;
1644 cfq_put_queue(__cfqq);
1645 __cfqq = next;
1646 }
1647
Jens Axboe89850f72006-07-22 16:48:31 +02001648 cfq_put_queue(cfqq);
1649}
1650
1651static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1652 struct cfq_io_context *cic)
1653{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02001654 struct io_context *ioc = cic->ioc;
1655
Jens Axboefc463792006-08-29 09:05:44 +02001656 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001657
1658 /*
1659 * Make sure key == NULL is seen for dead queues
1660 */
Jens Axboefc463792006-08-29 09:05:44 +02001661 smp_wmb();
Jens Axboe4ac845a2008-01-24 08:44:49 +01001662 cic->dead_key = (unsigned long) cic->key;
Jens Axboefc463792006-08-29 09:05:44 +02001663 cic->key = NULL;
1664
Fabio Checconi4faa3c82008-04-10 08:28:01 +02001665 if (ioc->ioc_data == cic)
1666 rcu_assign_pointer(ioc->ioc_data, NULL);
1667
Jens Axboeff6657c2009-04-08 10:58:57 +02001668 if (cic->cfqq[BLK_RW_ASYNC]) {
1669 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
1670 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02001671 }
1672
Jens Axboeff6657c2009-04-08 10:58:57 +02001673 if (cic->cfqq[BLK_RW_SYNC]) {
1674 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
1675 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02001676 }
Jens Axboe89850f72006-07-22 16:48:31 +02001677}
1678
Jens Axboe4ac845a2008-01-24 08:44:49 +01001679static void cfq_exit_single_io_context(struct io_context *ioc,
1680 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001681{
Al Viro478a82b2006-03-18 13:25:24 -05001682 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001683
Jens Axboe89850f72006-07-22 16:48:31 +02001684 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02001685 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01001686 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02001687
Jens Axboe4ac845a2008-01-24 08:44:49 +01001688 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01001689
1690 /*
1691 * Ensure we get a fresh copy of the ->key to prevent
1692 * race between exiting task and queue
1693 */
1694 smp_read_barrier_depends();
1695 if (cic->key)
1696 __cfq_exit_single_io_context(cfqd, cic);
1697
Jens Axboe4ac845a2008-01-24 08:44:49 +01001698 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05001699 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001700}
1701
Jens Axboe498d3aa2007-04-26 12:54:48 +02001702/*
1703 * The process that ioc belongs to has exited, we need to clean up
1704 * and put the internal structures we have that belongs to that process.
1705 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001706static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001708 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Jens Axboe22e2c502005-06-27 10:55:12 +02001711static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001712cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Jens Axboeb5deef92006-07-19 23:39:40 +02001714 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Christoph Lameter94f60302007-07-17 04:03:29 -07001716 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
1717 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001719 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001720 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01001721 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001722 cic->dtor = cfq_free_io_context;
1723 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09001724 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
1727 return cic;
1728}
1729
Jens Axboefd0928d2008-01-24 08:52:45 +01001730static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02001731{
1732 struct task_struct *tsk = current;
1733 int ioprio_class;
1734
Jens Axboe3b181522005-06-27 10:56:24 +02001735 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001736 return;
1737
Jens Axboefd0928d2008-01-24 08:52:45 +01001738 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001739 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01001740 default:
1741 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1742 case IOPRIO_CLASS_NONE:
1743 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02001744 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01001745 */
1746 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02001747 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01001748 break;
1749 case IOPRIO_CLASS_RT:
1750 cfqq->ioprio = task_ioprio(ioc);
1751 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1752 break;
1753 case IOPRIO_CLASS_BE:
1754 cfqq->ioprio = task_ioprio(ioc);
1755 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1756 break;
1757 case IOPRIO_CLASS_IDLE:
1758 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1759 cfqq->ioprio = 7;
1760 cfq_clear_cfqq_idle_window(cfqq);
1761 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02001762 }
1763
1764 /*
1765 * keep track of original prio settings in case we have to temporarily
1766 * elevate the priority of this queue
1767 */
1768 cfqq->org_ioprio = cfqq->ioprio;
1769 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001770 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001771}
1772
Jens Axboefebffd62008-01-28 13:19:43 +01001773static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001774{
Al Viro478a82b2006-03-18 13:25:24 -05001775 struct cfq_data *cfqd = cic->key;
1776 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001777 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001778
Jens Axboecaaa5f92006-06-16 11:23:00 +02001779 if (unlikely(!cfqd))
1780 return;
1781
Jens Axboec1b707d2006-10-30 19:54:23 +01001782 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001783
Jens Axboeff6657c2009-04-08 10:58:57 +02001784 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02001785 if (cfqq) {
1786 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02001787 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
1788 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001789 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02001790 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001791 cfq_put_queue(cfqq);
1792 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001793 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02001794
Jens Axboeff6657c2009-04-08 10:58:57 +02001795 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02001796 if (cfqq)
1797 cfq_mark_cfqq_prio_changed(cfqq);
1798
Jens Axboec1b707d2006-10-30 19:54:23 +01001799 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02001800}
1801
Jens Axboefc463792006-08-29 09:05:44 +02001802static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001804 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02001805 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Jens Axboed5036d72009-06-26 10:44:34 +02001808static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001809 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02001810{
1811 RB_CLEAR_NODE(&cfqq->rb_node);
1812 RB_CLEAR_NODE(&cfqq->p_node);
1813 INIT_LIST_HEAD(&cfqq->fifo);
1814
1815 atomic_set(&cfqq->ref, 0);
1816 cfqq->cfqd = cfqd;
1817
1818 cfq_mark_cfqq_prio_changed(cfqq);
1819
1820 if (is_sync) {
1821 if (!cfq_class_idle(cfqq))
1822 cfq_mark_cfqq_idle_window(cfqq);
1823 cfq_mark_cfqq_sync(cfqq);
1824 }
1825 cfqq->pid = pid;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02001829cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01001830 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001833 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835retry:
Jens Axboe4ac845a2008-01-24 08:44:49 +01001836 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001837 /* cic always exists here */
1838 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Jens Axboe6118b702009-06-30 09:34:12 +02001840 /*
1841 * Always try a new alloc if we fell back to the OOM cfqq
1842 * originally, since it should just be a temporary situation.
1843 */
1844 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
1845 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (new_cfqq) {
1847 cfqq = new_cfqq;
1848 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001849 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07001851 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02001852 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07001853 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02001855 if (new_cfqq)
1856 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02001857 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07001858 cfqq = kmem_cache_alloc_node(cfq_pool,
1859 gfp_mask | __GFP_ZERO,
1860 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02001861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Jens Axboe6118b702009-06-30 09:34:12 +02001863 if (cfqq) {
1864 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
1865 cfq_init_prio_data(cfqq, ioc);
1866 cfq_log_cfqq(cfqd, cfqq, "alloced");
1867 } else
1868 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
1871 if (new_cfqq)
1872 kmem_cache_free(cfq_pool, new_cfqq);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return cfqq;
1875}
1876
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001877static struct cfq_queue **
1878cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
1879{
Jens Axboefe094d92008-01-31 13:08:54 +01001880 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001881 case IOPRIO_CLASS_RT:
1882 return &cfqd->async_cfqq[0][ioprio];
1883 case IOPRIO_CLASS_BE:
1884 return &cfqd->async_cfqq[1][ioprio];
1885 case IOPRIO_CLASS_IDLE:
1886 return &cfqd->async_idle_cfqq;
1887 default:
1888 BUG();
1889 }
1890}
1891
Jens Axboe15c31be2007-07-10 13:43:25 +02001892static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02001893cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02001894 gfp_t gfp_mask)
1895{
Jens Axboefd0928d2008-01-24 08:52:45 +01001896 const int ioprio = task_ioprio(ioc);
1897 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001898 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02001899 struct cfq_queue *cfqq = NULL;
1900
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001901 if (!is_sync) {
1902 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
1903 cfqq = *async_cfqq;
1904 }
1905
Jens Axboe6118b702009-06-30 09:34:12 +02001906 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01001907 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02001908
1909 /*
1910 * pin the queue now that it's allocated, scheduler exit will prune it
1911 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001912 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02001913 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02001914 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02001915 }
1916
1917 atomic_inc(&cfqq->ref);
1918 return cfqq;
1919}
1920
Jens Axboe498d3aa2007-04-26 12:54:48 +02001921/*
1922 * We drop cfq io contexts lazily, so we may find a dead one.
1923 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001924static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01001925cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
1926 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001927{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001928 unsigned long flags;
1929
Jens Axboefc463792006-08-29 09:05:44 +02001930 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02001931
Jens Axboe4ac845a2008-01-24 08:44:49 +01001932 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02001933
Fabio Checconi4faa3c82008-04-10 08:28:01 +02001934 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001935
1936 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
Jens Axboeffc4e752008-02-19 10:02:29 +01001937 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001938 spin_unlock_irqrestore(&ioc->lock, flags);
1939
1940 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001941}
1942
Jens Axboee2d74ac2006-03-28 08:59:01 +02001943static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01001944cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001945{
Jens Axboee2d74ac2006-03-28 08:59:01 +02001946 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02001947 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01001948 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001949
Vasily Tarasov91fac312007-04-25 12:29:51 +02001950 if (unlikely(!ioc))
1951 return NULL;
1952
Jens Axboed6de8be2008-05-28 14:46:59 +02001953 rcu_read_lock();
1954
Jens Axboe597bc482007-04-24 21:23:53 +02001955 /*
1956 * we maintain a last-hit cache, to avoid browsing over the tree
1957 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01001958 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02001959 if (cic && cic->key == cfqd) {
1960 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02001961 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02001962 }
Jens Axboe597bc482007-04-24 21:23:53 +02001963
Jens Axboe4ac845a2008-01-24 08:44:49 +01001964 do {
Jens Axboe4ac845a2008-01-24 08:44:49 +01001965 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
1966 rcu_read_unlock();
1967 if (!cic)
1968 break;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02001969 /* ->key must be copied to avoid race with cfq_exit_queue() */
1970 k = cic->key;
1971 if (unlikely(!k)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01001972 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02001973 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01001974 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02001975 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02001976
Jens Axboed6de8be2008-05-28 14:46:59 +02001977 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001978 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02001979 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001980 break;
1981 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02001982
Jens Axboe4ac845a2008-01-24 08:44:49 +01001983 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001984}
1985
Jens Axboe4ac845a2008-01-24 08:44:49 +01001986/*
1987 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
1988 * the process specific cfq io context when entered from the block layer.
1989 * Also adds the cic to a per-cfqd list, used when this queue is removed.
1990 */
Jens Axboefebffd62008-01-28 13:19:43 +01001991static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
1992 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02001993{
Jens Axboe0261d682006-10-30 19:07:48 +01001994 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01001995 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02001996
Jens Axboe4ac845a2008-01-24 08:44:49 +01001997 ret = radix_tree_preload(gfp_mask);
1998 if (!ret) {
1999 cic->ioc = ioc;
2000 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002001
Jens Axboe4ac845a2008-01-24 08:44:49 +01002002 spin_lock_irqsave(&ioc->lock, flags);
2003 ret = radix_tree_insert(&ioc->radix_root,
2004 (unsigned long) cfqd, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01002005 if (!ret)
2006 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002007 spin_unlock_irqrestore(&ioc->lock, flags);
2008
2009 radix_tree_preload_end();
2010
2011 if (!ret) {
2012 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2013 list_add(&cic->queue_list, &cfqd->cic_list);
2014 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002015 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002016 }
2017
Jens Axboe4ac845a2008-01-24 08:44:49 +01002018 if (ret)
2019 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02002020
Jens Axboe4ac845a2008-01-24 08:44:49 +01002021 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002022}
2023
Jens Axboe22e2c502005-06-27 10:55:12 +02002024/*
2025 * Setup general io context and cfq io context. There can be several cfq
2026 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02002027 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02002028 */
2029static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02002030cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Jens Axboe22e2c502005-06-27 10:55:12 +02002032 struct io_context *ioc = NULL;
2033 struct cfq_io_context *cic;
2034
2035 might_sleep_if(gfp_mask & __GFP_WAIT);
2036
Jens Axboeb5deef92006-07-19 23:39:40 +02002037 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02002038 if (!ioc)
2039 return NULL;
2040
Jens Axboe4ac845a2008-01-24 08:44:49 +01002041 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002042 if (cic)
2043 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02002044
Jens Axboee2d74ac2006-03-28 08:59:01 +02002045 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2046 if (cic == NULL)
2047 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02002048
Jens Axboe4ac845a2008-01-24 08:44:49 +01002049 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2050 goto err_free;
2051
Jens Axboe22e2c502005-06-27 10:55:12 +02002052out:
Jens Axboefc463792006-08-29 09:05:44 +02002053 smp_read_barrier_depends();
2054 if (unlikely(ioc->ioprio_changed))
2055 cfq_ioc_set_ioprio(ioc);
2056
Jens Axboe22e2c502005-06-27 10:55:12 +02002057 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002058err_free:
2059 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002060err:
2061 put_io_context(ioc);
2062 return NULL;
2063}
2064
2065static void
2066cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2067{
Jens Axboeaaf12282007-01-19 11:30:16 +11002068 unsigned long elapsed = jiffies - cic->last_end_request;
2069 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002070
2071 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2072 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2073 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2074}
2075
Jens Axboe206dc692006-03-28 13:03:44 +02002076static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002077cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002078 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002079{
2080 sector_t sdist;
2081 u64 total;
2082
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002083 if (!cfqq->last_request_pos)
Jeff Moyer4d00aa42009-04-21 07:25:04 +02002084 sdist = 0;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002085 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2086 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02002087 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002088 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
Jens Axboe206dc692006-03-28 13:03:44 +02002089
2090 /*
2091 * Don't allow the seek distance to get too large from the
2092 * odd fragment, pagein, etc
2093 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002094 if (cfqq->seek_samples <= 60) /* second&third seek */
2095 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
Jens Axboe206dc692006-03-28 13:03:44 +02002096 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002097 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
Jens Axboe206dc692006-03-28 13:03:44 +02002098
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002099 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2100 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2101 total = cfqq->seek_total + (cfqq->seek_samples/2);
2102 do_div(total, cfqq->seek_samples);
2103 cfqq->seek_mean = (sector_t)total;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002104
2105 /*
2106 * If this cfqq is shared between multiple processes, check to
2107 * make sure that those processes are still issuing I/Os within
2108 * the mean seek distance. If not, it may be time to break the
2109 * queues apart again.
2110 */
2111 if (cfq_cfqq_coop(cfqq)) {
2112 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2113 cfqq->seeky_start = jiffies;
2114 else if (!CFQQ_SEEKY(cfqq))
2115 cfqq->seeky_start = 0;
2116 }
Jens Axboe206dc692006-03-28 13:03:44 +02002117}
Jens Axboe22e2c502005-06-27 10:55:12 +02002118
2119/*
2120 * Disable idle window if the process thinks too long or seeks so much that
2121 * it doesn't matter
2122 */
2123static void
2124cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2125 struct cfq_io_context *cic)
2126{
Jens Axboe7b679132008-05-30 12:23:07 +02002127 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002128
Jens Axboe08717142008-01-28 11:38:15 +01002129 /*
2130 * Don't idle for async or idle io prio class
2131 */
2132 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002133 return;
2134
Jens Axboec265a7f2008-06-26 13:49:33 +02002135 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002136
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01002137 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002138 (!cfqd->cfq_latency && cfqd->hw_tag && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002139 enable_idle = 0;
2140 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccoloec60e4f2009-10-07 19:51:54 +02002141 unsigned int slice_idle = cfqd->cfq_slice_idle;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002142 if (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq))
Corrado Zoccoloec60e4f2009-10-07 19:51:54 +02002143 slice_idle = msecs_to_jiffies(CFQ_MIN_TT);
2144 if (cic->ttime_mean > slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002145 enable_idle = 0;
2146 else
2147 enable_idle = 1;
2148 }
2149
Jens Axboe7b679132008-05-30 12:23:07 +02002150 if (old_idle != enable_idle) {
2151 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2152 if (enable_idle)
2153 cfq_mark_cfqq_idle_window(cfqq);
2154 else
2155 cfq_clear_cfqq_idle_window(cfqq);
2156 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002157}
2158
Jens Axboe22e2c502005-06-27 10:55:12 +02002159/*
2160 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2161 * no or if we aren't sure, a 1 will cause a preempt.
2162 */
Jens Axboea6151c32009-10-07 20:02:57 +02002163static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002164cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002165 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002166{
Jens Axboe6d048f52007-04-25 12:44:27 +02002167 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002168
Jens Axboe6d048f52007-04-25 12:44:27 +02002169 cfqq = cfqd->active_queue;
2170 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002171 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002172
Jens Axboe6d048f52007-04-25 12:44:27 +02002173 if (cfq_slice_used(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002174 return true;
Jens Axboe6d048f52007-04-25 12:44:27 +02002175
2176 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002177 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002178
2179 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002180 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002181
Jens Axboe22e2c502005-06-27 10:55:12 +02002182 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002183 * if the new request is sync, but the currently running queue is
2184 * not, let the sync request have priority.
2185 */
Jens Axboe5e705372006-07-13 12:39:25 +02002186 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002187 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002188
Jens Axboe374f84a2006-07-23 01:42:19 +02002189 /*
2190 * So both queues are sync. Let the new request get disk time if
2191 * it's a metadata request and the current queue is doing regular IO.
2192 */
2193 if (rq_is_meta(rq) && !cfqq->meta_pending)
Jens Axboea6151c32009-10-07 20:02:57 +02002194 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002195
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002196 /*
2197 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2198 */
2199 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002200 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002201
Jens Axboe1e3335d2007-02-14 19:59:49 +01002202 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002203 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002204
2205 /*
2206 * if this request is as-good as one we would expect from the
2207 * current cfqq, let it preempt
2208 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002209 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002210 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002211
Jens Axboea6151c32009-10-07 20:02:57 +02002212 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002213}
2214
2215/*
2216 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2217 * let it have half of its nominal slice.
2218 */
2219static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2220{
Jens Axboe7b679132008-05-30 12:23:07 +02002221 cfq_log_cfqq(cfqd, cfqq, "preempt");
Jens Axboe6084cdd2007-04-23 08:25:00 +02002222 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02002223
Jens Axboebf572252006-07-19 20:29:12 +02002224 /*
2225 * Put the new queue at the front of the of the current list,
2226 * so we know that it will be selected next.
2227 */
2228 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02002229
2230 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02002231
Jens Axboe44f7c162007-01-19 11:51:58 +11002232 cfqq->slice_end = 0;
2233 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002234}
2235
2236/*
Jens Axboe5e705372006-07-13 12:39:25 +02002237 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02002238 * something we should do about it
2239 */
2240static void
Jens Axboe5e705372006-07-13 12:39:25 +02002241cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2242 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002243{
Jens Axboe5e705372006-07-13 12:39:25 +02002244 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02002245
Aaron Carroll45333d52008-08-26 15:52:36 +02002246 cfqd->rq_queued++;
Jens Axboe374f84a2006-07-23 01:42:19 +02002247 if (rq_is_meta(rq))
2248 cfqq->meta_pending++;
2249
Jens Axboe9c2c38a2005-08-24 14:57:54 +02002250 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002251 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02002252 cfq_update_idle_window(cfqd, cfqq, cic);
2253
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002254 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002255
2256 if (cfqq == cfqd->active_queue) {
2257 /*
Jens Axboeb0291952009-04-07 11:38:31 +02002258 * Remember that we saw a request from this process, but
2259 * don't start queuing just yet. Otherwise we risk seeing lots
2260 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02002261 * and merging. If the request is already larger than a single
2262 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02002263 * merging is already done. Ditto for a busy system that
2264 * has other work pending, don't risk delaying until the
2265 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02002266 */
Jens Axboed6ceb252009-04-14 14:18:16 +02002267 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02002268 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2269 cfqd->busy_queues > 1) {
Jens Axboed6ceb252009-04-14 14:18:16 +02002270 del_timer(&cfqd->idle_slice_timer);
Tejun Heoa7f55792009-04-23 11:05:17 +09002271 __blk_run_queue(cfqd->queue);
Jens Axboed6ceb252009-04-14 14:18:16 +02002272 }
Jens Axboeb0291952009-04-07 11:38:31 +02002273 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboed6ceb252009-04-14 14:18:16 +02002274 }
Jens Axboe5e705372006-07-13 12:39:25 +02002275 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002276 /*
2277 * not the active queue - expire current slice if it is
2278 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002279 * has some old slice time left and is of higher priority or
2280 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02002281 */
2282 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09002283 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002284 }
2285}
2286
Jens Axboe165125e2007-07-24 09:28:11 +02002287static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002288{
Jens Axboeb4878f22005-10-20 16:42:29 +02002289 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002290 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002291
Jens Axboe7b679132008-05-30 12:23:07 +02002292 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01002293 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Jens Axboe5e705372006-07-13 12:39:25 +02002295 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Jens Axboe30996f42009-10-05 11:03:39 +02002297 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02002298 list_add_tail(&rq->queuelist, &cfqq->fifo);
2299
Jens Axboe5e705372006-07-13 12:39:25 +02002300 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Aaron Carroll45333d52008-08-26 15:52:36 +02002303/*
2304 * Update hw_tag based on peak queue depth over 50 samples under
2305 * sufficient load.
2306 */
2307static void cfq_update_hw_tag(struct cfq_data *cfqd)
2308{
Shaohua Li1a1238a2009-10-27 08:46:23 +01002309 struct cfq_queue *cfqq = cfqd->active_queue;
2310
Jens Axboe5ad531d2009-07-03 12:57:48 +02002311 if (rq_in_driver(cfqd) > cfqd->rq_in_driver_peak)
2312 cfqd->rq_in_driver_peak = rq_in_driver(cfqd);
Aaron Carroll45333d52008-08-26 15:52:36 +02002313
2314 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Jens Axboe5ad531d2009-07-03 12:57:48 +02002315 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02002316 return;
2317
Shaohua Li1a1238a2009-10-27 08:46:23 +01002318 /*
2319 * If active queue hasn't enough requests and can idle, cfq might not
2320 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
2321 * case
2322 */
2323 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
2324 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
2325 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
2326 return;
2327
Aaron Carroll45333d52008-08-26 15:52:36 +02002328 if (cfqd->hw_tag_samples++ < 50)
2329 return;
2330
2331 if (cfqd->rq_in_driver_peak >= CFQ_HW_QUEUE_MIN)
2332 cfqd->hw_tag = 1;
2333 else
2334 cfqd->hw_tag = 0;
2335
2336 cfqd->hw_tag_samples = 0;
2337 cfqd->rq_in_driver_peak = 0;
2338}
2339
Jens Axboe165125e2007-07-24 09:28:11 +02002340static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
Jens Axboe5e705372006-07-13 12:39:25 +02002342 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002343 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02002344 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002345 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Jens Axboeb4878f22005-10-20 16:42:29 +02002347 now = jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002348 cfq_log_cfqq(cfqd, cfqq, "complete");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Aaron Carroll45333d52008-08-26 15:52:36 +02002350 cfq_update_hw_tag(cfqd);
2351
Jens Axboe5ad531d2009-07-03 12:57:48 +02002352 WARN_ON(!cfqd->rq_in_driver[sync]);
Jens Axboe6d048f52007-04-25 12:44:27 +02002353 WARN_ON(!cfqq->dispatched);
Jens Axboe5ad531d2009-07-03 12:57:48 +02002354 cfqd->rq_in_driver[sync]--;
Jens Axboe6d048f52007-04-25 12:44:27 +02002355 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Jens Axboe3ed9a292007-04-23 08:33:33 +02002357 if (cfq_cfqq_sync(cfqq))
2358 cfqd->sync_flight--;
2359
Vivek Goyal365722b2009-10-03 15:21:27 +02002360 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02002361 RQ_CIC(rq)->last_end_request = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02002362 cfqd->last_end_sync_rq = now;
2363 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002364
2365 /*
2366 * If this is the active queue, check if it needs to be expired,
2367 * or if we want to idle in case it has no pending requests.
2368 */
2369 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02002370 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
2371
Jens Axboe44f7c162007-01-19 11:51:58 +11002372 if (cfq_cfqq_slice_new(cfqq)) {
2373 cfq_set_prio_slice(cfqd, cfqq);
2374 cfq_clear_cfqq_slice_new(cfqq);
2375 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002376 /*
2377 * If there are no requests waiting in this queue, and
2378 * there are other queues ready to issue requests, AND
2379 * those other queues are issuing requests within our
2380 * mean seek distance, give them a chance to run instead
2381 * of idling.
2382 */
Jens Axboe08717142008-01-28 11:38:15 +01002383 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02002384 cfq_slice_expired(cfqd, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002385 else if (cfqq_empty && !cfq_close_cooperator(cfqd, cfqq) &&
Jens Axboea36e71f2009-04-15 12:15:11 +02002386 sync && !rq_noidle(rq))
Jens Axboe6d048f52007-04-25 12:44:27 +02002387 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002388 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002389
Jens Axboe5ad531d2009-07-03 12:57:48 +02002390 if (!rq_in_driver(cfqd))
Jens Axboe23e018a2009-10-05 08:52:35 +02002391 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
Jens Axboe22e2c502005-06-27 10:55:12 +02002394/*
2395 * we temporarily boost lower priority queues if they are holding fs exclusive
2396 * resources. they are boosted to normal prio (CLASS_BE/4)
2397 */
2398static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Jens Axboe22e2c502005-06-27 10:55:12 +02002400 if (has_fs_excl()) {
2401 /*
2402 * boost idle prio on transactions that would lock out other
2403 * users of the filesystem
2404 */
2405 if (cfq_class_idle(cfqq))
2406 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2407 if (cfqq->ioprio > IOPRIO_NORM)
2408 cfqq->ioprio = IOPRIO_NORM;
2409 } else {
2410 /*
2411 * check if we need to unboost the queue
2412 */
2413 if (cfqq->ioprio_class != cfqq->org_ioprio_class)
2414 cfqq->ioprio_class = cfqq->org_ioprio_class;
2415 if (cfqq->ioprio != cfqq->org_ioprio)
2416 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002418}
2419
Jens Axboe89850f72006-07-22 16:48:31 +02002420static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002421{
Jens Axboe1b379d82009-08-11 08:26:11 +02002422 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002423 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002424 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02002425 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002426
2427 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02002428}
2429
Jens Axboe165125e2007-07-24 09:28:11 +02002430static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02002431{
2432 struct cfq_data *cfqd = q->elevator->elevator_data;
2433 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002434 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02002435 struct cfq_queue *cfqq;
2436
2437 /*
2438 * don't force setup of a queue from here, as a call to may_queue
2439 * does not necessarily imply that a request actually will be queued.
2440 * so just lookup a possibly existing queue, or return 'may queue'
2441 * if that fails
2442 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002443 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002444 if (!cic)
2445 return ELV_MQUEUE_MAY;
2446
Jens Axboeb0b78f82009-04-08 10:56:08 +02002447 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02002448 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01002449 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02002450 cfq_prio_boost(cfqq);
2451
Jens Axboe89850f72006-07-22 16:48:31 +02002452 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002453 }
2454
2455 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458/*
2459 * queue lock held here
2460 */
Jens Axboebb37b942006-12-01 10:42:33 +01002461static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Jens Axboe5e705372006-07-13 12:39:25 +02002463 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Jens Axboe5e705372006-07-13 12:39:25 +02002465 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002466 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Jens Axboe22e2c502005-06-27 10:55:12 +02002468 BUG_ON(!cfqq->allocated[rw]);
2469 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jens Axboe5e705372006-07-13 12:39:25 +02002471 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02002474 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 cfq_put_queue(cfqq);
2477 }
2478}
2479
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002480static struct cfq_queue *
2481cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
2482 struct cfq_queue *cfqq)
2483{
2484 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
2485 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002486 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002487 cfq_put_queue(cfqq);
2488 return cic_to_cfqq(cic, 1);
2489}
2490
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002491static int should_split_cfqq(struct cfq_queue *cfqq)
2492{
2493 if (cfqq->seeky_start &&
2494 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
2495 return 1;
2496 return 0;
2497}
2498
2499/*
2500 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
2501 * was the last process referring to said cfqq.
2502 */
2503static struct cfq_queue *
2504split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
2505{
2506 if (cfqq_process_refs(cfqq) == 1) {
2507 cfqq->seeky_start = 0;
2508 cfqq->pid = current->pid;
2509 cfq_clear_cfqq_coop(cfqq);
2510 return cfqq;
2511 }
2512
2513 cic_set_cfqq(cic, NULL, 1);
2514 cfq_put_queue(cfqq);
2515 return NULL;
2516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/*
Jens Axboe22e2c502005-06-27 10:55:12 +02002518 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002520static int
Jens Axboe165125e2007-07-24 09:28:11 +02002521cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
2523 struct cfq_data *cfqd = q->elevator->elevator_data;
2524 struct cfq_io_context *cic;
2525 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02002526 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002527 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 unsigned long flags;
2529
2530 might_sleep_if(gfp_mask & __GFP_WAIT);
2531
Jens Axboee2d74ac2006-03-28 08:59:01 +02002532 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 spin_lock_irqsave(q->queue_lock, flags);
2535
Jens Axboe22e2c502005-06-27 10:55:12 +02002536 if (!cic)
2537 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002539new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02002540 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02002541 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01002542 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002543 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002544 } else {
2545 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002546 * If the queue was seeky for too long, break it apart.
2547 */
2548 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
2549 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
2550 cfqq = split_cfqq(cic, cfqq);
2551 if (!cfqq)
2552 goto new_queue;
2553 }
2554
2555 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002556 * Check to see if this queue is scheduled to merge with
2557 * another, closely cooperating queue. The merging of
2558 * queues happens here as it must be done in process context.
2559 * The reference on new_cfqq was taken in merge_cfqqs.
2560 */
2561 if (cfqq->new_cfqq)
2562 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02002566 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02002567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 spin_unlock_irqrestore(q->queue_lock, flags);
2569
Jens Axboe5e705372006-07-13 12:39:25 +02002570 rq->elevator_private = cic;
2571 rq->elevator_private2 = cfqq;
2572 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02002573
Jens Axboe22e2c502005-06-27 10:55:12 +02002574queue_fail:
2575 if (cic)
2576 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02002577
Jens Axboe23e018a2009-10-05 08:52:35 +02002578 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02002580 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return 1;
2582}
2583
David Howells65f27f32006-11-22 14:55:48 +00002584static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02002585{
David Howells65f27f32006-11-22 14:55:48 +00002586 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02002587 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02002588 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002589
Jens Axboe40bb54d2009-04-15 12:11:10 +02002590 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09002591 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02002592 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02002593}
2594
2595/*
2596 * Timer running if the active_queue is currently idling inside its time slice
2597 */
2598static void cfq_idle_slice_timer(unsigned long data)
2599{
2600 struct cfq_data *cfqd = (struct cfq_data *) data;
2601 struct cfq_queue *cfqq;
2602 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002603 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02002604
Jens Axboe7b679132008-05-30 12:23:07 +02002605 cfq_log(cfqd, "idle timer fired");
2606
Jens Axboe22e2c502005-06-27 10:55:12 +02002607 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2608
Jens Axboefe094d92008-01-31 13:08:54 +01002609 cfqq = cfqd->active_queue;
2610 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002611 timed_out = 0;
2612
Jens Axboe22e2c502005-06-27 10:55:12 +02002613 /*
Jens Axboeb0291952009-04-07 11:38:31 +02002614 * We saw a request before the queue expired, let it through
2615 */
2616 if (cfq_cfqq_must_dispatch(cfqq))
2617 goto out_kick;
2618
2619 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002620 * expired
2621 */
Jens Axboe44f7c162007-01-19 11:51:58 +11002622 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002623 goto expire;
2624
2625 /*
2626 * only expire and reinvoke request handler, if there are
2627 * other queues with pending requests
2628 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002629 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002630 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002631
2632 /*
2633 * not expired and it has a request pending, let it dispatch
2634 */
Jens Axboe75e50982009-04-07 08:56:14 +02002635 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002636 goto out_kick;
Jens Axboe22e2c502005-06-27 10:55:12 +02002637 }
2638expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002639 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002640out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02002641 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002642out_cont:
2643 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2644}
2645
Jens Axboe3b181522005-06-27 10:56:24 +02002646static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2647{
2648 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02002649 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02002650}
Jens Axboe22e2c502005-06-27 10:55:12 +02002651
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002652static void cfq_put_async_queues(struct cfq_data *cfqd)
2653{
2654 int i;
2655
2656 for (i = 0; i < IOPRIO_BE_NR; i++) {
2657 if (cfqd->async_cfqq[0][i])
2658 cfq_put_queue(cfqd->async_cfqq[0][i]);
2659 if (cfqd->async_cfqq[1][i])
2660 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002661 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01002662
2663 if (cfqd->async_idle_cfqq)
2664 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002665}
2666
Jens Axboeb374d182008-10-31 10:05:07 +01002667static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Jens Axboe22e2c502005-06-27 10:55:12 +02002669 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02002670 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002671
Jens Axboe3b181522005-06-27 10:56:24 +02002672 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002673
Al Virod9ff4182006-03-18 13:51:22 -05002674 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002675
Al Virod9ff4182006-03-18 13:51:22 -05002676 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02002677 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002678
2679 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002680 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2681 struct cfq_io_context,
2682 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002683
2684 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002685 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002686
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002687 cfq_put_async_queues(cfqd);
Jens Axboe15c31be2007-07-10 13:43:25 +02002688
Al Virod9ff4182006-03-18 13:51:22 -05002689 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002690
2691 cfq_shutdown_timer_wq(cfqd);
2692
Al Viroa90d7422006-03-18 12:05:37 -05002693 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Jens Axboe165125e2007-07-24 09:28:11 +02002696static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
2698 struct cfq_data *cfqd;
Jens Axboe26a2ac02009-04-23 12:13:27 +02002699 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Christoph Lameter94f60302007-07-17 04:03:29 -07002701 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002703 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Jens Axboecc09e292007-04-26 12:53:50 +02002705 cfqd->service_tree = CFQ_RB_ROOT;
Jens Axboe26a2ac02009-04-23 12:13:27 +02002706
2707 /*
2708 * Not strictly needed (since RB_ROOT just clears the node and we
2709 * zeroed cfqd on alloc), but better be safe in case someone decides
2710 * to add magic to the rb code
2711 */
2712 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2713 cfqd->prio_trees[i] = RB_ROOT;
2714
Jens Axboe6118b702009-06-30 09:34:12 +02002715 /*
2716 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
2717 * Grab a permanent reference to it, so that the normal code flow
2718 * will not attempt to free it.
2719 */
2720 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
2721 atomic_inc(&cfqd->oom_cfqq.ref);
2722
Al Virod9ff4182006-03-18 13:51:22 -05002723 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Jens Axboe22e2c502005-06-27 10:55:12 +02002727 init_timer(&cfqd->idle_slice_timer);
2728 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2729 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2730
Jens Axboe23e018a2009-10-05 08:52:35 +02002731 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002734 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2735 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 cfqd->cfq_back_max = cfq_back_max;
2737 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002738 cfqd->cfq_slice[0] = cfq_slice_async;
2739 cfqd->cfq_slice[1] = cfq_slice_sync;
2740 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2741 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02002742 cfqd->cfq_latency = 1;
Aaron Carroll45333d52008-08-26 15:52:36 +02002743 cfqd->hw_tag = 1;
Vivek Goyal365722b2009-10-03 15:21:27 +02002744 cfqd->last_end_sync_rq = jiffies;
Jens Axboebc1c1162006-06-08 08:49:06 +02002745 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
2748static void cfq_slab_kill(void)
2749{
Jens Axboed6de8be2008-05-28 14:46:59 +02002750 /*
2751 * Caller already ensured that pending RCU callbacks are completed,
2752 * so we should have no busy allocations at this point.
2753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (cfq_pool)
2755 kmem_cache_destroy(cfq_pool);
2756 if (cfq_ioc_pool)
2757 kmem_cache_destroy(cfq_ioc_pool);
2758}
2759
2760static int __init cfq_slab_setup(void)
2761{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002762 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (!cfq_pool)
2764 goto fail;
2765
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002766 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (!cfq_ioc_pool)
2768 goto fail;
2769
2770 return 0;
2771fail:
2772 cfq_slab_kill();
2773 return -ENOMEM;
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776/*
2777 * sysfs parts below -->
2778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779static ssize_t
2780cfq_var_show(unsigned int var, char *page)
2781{
2782 return sprintf(page, "%d\n", var);
2783}
2784
2785static ssize_t
2786cfq_var_store(unsigned int *var, const char *page, size_t count)
2787{
2788 char *p = (char *) page;
2789
2790 *var = simple_strtoul(p, &p, 10);
2791 return count;
2792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01002795static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002797 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 unsigned int __data = __VAR; \
2799 if (__CONV) \
2800 __data = jiffies_to_msecs(__data); \
2801 return cfq_var_show(__data, (page)); \
2802}
2803SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002804SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
2805SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05002806SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
2807SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002808SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
2809SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
2810SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
2811SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02002812SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813#undef SHOW_FUNCTION
2814
2815#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01002816static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{ \
Al Viro3d1ab402006-03-18 18:35:43 -05002818 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 unsigned int __data; \
2820 int ret = cfq_var_store(&__data, (page), count); \
2821 if (__data < (MIN)) \
2822 __data = (MIN); \
2823 else if (__data > (MAX)) \
2824 __data = (MAX); \
2825 if (__CONV) \
2826 *(__PTR) = msecs_to_jiffies(__data); \
2827 else \
2828 *(__PTR) = __data; \
2829 return ret; \
2830}
2831STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01002832STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
2833 UINT_MAX, 1);
2834STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
2835 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05002836STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01002837STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
2838 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02002839STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
2840STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
2841STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01002842STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
2843 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02002844STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845#undef STORE_FUNCTION
2846
Al Viroe572ec72006-03-18 22:27:18 -05002847#define CFQ_ATTR(name) \
2848 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02002849
Al Viroe572ec72006-03-18 22:27:18 -05002850static struct elv_fs_entry cfq_attrs[] = {
2851 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05002852 CFQ_ATTR(fifo_expire_sync),
2853 CFQ_ATTR(fifo_expire_async),
2854 CFQ_ATTR(back_seek_max),
2855 CFQ_ATTR(back_seek_penalty),
2856 CFQ_ATTR(slice_sync),
2857 CFQ_ATTR(slice_async),
2858 CFQ_ATTR(slice_async_rq),
2859 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02002860 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05002861 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862};
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864static struct elevator_type iosched_cfq = {
2865 .ops = {
2866 .elevator_merge_fn = cfq_merge,
2867 .elevator_merged_fn = cfq_merged_request,
2868 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01002869 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02002870 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02002872 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 .elevator_deactivate_req_fn = cfq_deactivate_request,
2874 .elevator_queue_empty_fn = cfq_queue_empty,
2875 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02002876 .elevator_former_req_fn = elv_rb_former_request,
2877 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .elevator_set_req_fn = cfq_set_request,
2879 .elevator_put_req_fn = cfq_put_request,
2880 .elevator_may_queue_fn = cfq_may_queue,
2881 .elevator_init_fn = cfq_init_queue,
2882 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02002883 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 },
Al Viro3d1ab402006-03-18 18:35:43 -05002885 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 .elevator_name = "cfq",
2887 .elevator_owner = THIS_MODULE,
2888};
2889
2890static int __init cfq_init(void)
2891{
Jens Axboe22e2c502005-06-27 10:55:12 +02002892 /*
2893 * could be 0 on HZ < 1000 setups
2894 */
2895 if (!cfq_slice_async)
2896 cfq_slice_async = 1;
2897 if (!cfq_slice_idle)
2898 cfq_slice_idle = 1;
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (cfq_slab_setup())
2901 return -ENOMEM;
2902
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01002903 elv_register(&iosched_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01002905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908static void __exit cfq_exit(void)
2909{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002910 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05002912 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02002913 /* ioc_gone's update must be visible before reading ioc_count */
2914 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02002915
2916 /*
2917 * this also protects us from entering cfq_slab_kill() with
2918 * pending RCU callbacks
2919 */
Tejun Heo245b2e72009-06-24 15:13:48 +09002920 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002921 wait_for_completion(&all_gone);
Christoph Hellwig83521d32005-10-30 15:01:39 -08002922 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
2925module_init(cfq_init);
2926module_exit(cfq_exit);
2927
2928MODULE_AUTHOR("Jens Axboe");
2929MODULE_LICENSE("GPL");
2930MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");