blob: 2c1086acddfa4f91134bc40bf3cf44d14c63a785 [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>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010012#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020014#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020015#include <linux/blktrace_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
18 * tunables
19 */
Jens Axboefe094d92008-01-31 13:08:54 +010020/* max queue in one round of service */
21static const int cfq_quantum = 4;
Arjan van de Ven64100092006-01-06 09:46:02 +010022static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010023/* maximum backwards seek, in KiB */
24static const int cfq_back_max = 16 * 1024;
25/* penalty of a backwards seek */
26static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010027static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020028static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010029static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020030static int cfq_slice_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010031static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
32static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020033
Jens Axboed9e76202007-04-20 14:27:50 +020034/*
Jens Axboe08717142008-01-28 11:38:15 +010035 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020036 */
Jens Axboe08717142008-01-28 11:38:15 +010037#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020038
39/*
40 * below this threshold, we consider thinktime immediate
41 */
42#define CFQ_MIN_TT (2)
43
Jeff Moyere6c5bc72009-10-23 17:14:52 -040044/*
45 * Allow merged cfqqs to perform this amount of seeky I/O before
46 * deciding to break the queues up again.
47 */
48#define CFQQ_COOP_TOUT (HZ)
49
Jens Axboe22e2c502005-06-27 10:55:12 +020050#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020051#define CFQ_HW_QUEUE_MIN (5)
Jens Axboe22e2c502005-06-27 10:55:12 +020052
Jens Axboefe094d92008-01-31 13:08:54 +010053#define RQ_CIC(rq) \
54 ((struct cfq_io_context *) (rq)->elevator_private)
Jens Axboe7b679132008-05-30 12:23:07 +020055#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Christoph Lametere18b8902006-12-06 20:33:20 -080057static struct kmem_cache *cfq_pool;
58static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Tejun Heo245b2e72009-06-24 15:13:48 +090060static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050061static struct completion *ioc_gone;
Jens Axboe9a11b4e2008-05-29 09:32:08 +020062static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -050063
Jens Axboe22e2c502005-06-27 10:55:12 +020064#define CFQ_PRIO_LISTS IOPRIO_BE_NR
65#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020066#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
67
Jens Axboe206dc692006-03-28 13:03:44 +020068#define sample_valid(samples) ((samples) > 80)
69
Jens Axboe22e2c502005-06-27 10:55:12 +020070/*
Jens Axboecc09e292007-04-26 12:53:50 +020071 * Most of our rbtree usage is for sorting with min extraction, so
72 * if we cache the leftmost node we don't have to walk down the tree
73 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
74 * move this into the elevator for the rq sorting as well.
75 */
76struct cfq_rb_root {
77 struct rb_root rb;
78 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010079 unsigned count;
Jens Axboecc09e292007-04-26 12:53:50 +020080};
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010081#define CFQ_RB_ROOT (struct cfq_rb_root) { RB_ROOT, NULL, 0, }
Jens Axboecc09e292007-04-26 12:53:50 +020082
83/*
Jens Axboe6118b702009-06-30 09:34:12 +020084 * Per process-grouping structure
85 */
86struct cfq_queue {
87 /* reference count */
88 atomic_t ref;
89 /* various state flags, see below */
90 unsigned int flags;
91 /* parent cfq_data */
92 struct cfq_data *cfqd;
93 /* service_tree member */
94 struct rb_node rb_node;
95 /* service_tree key */
96 unsigned long rb_key;
97 /* prio tree member */
98 struct rb_node p_node;
99 /* prio tree root we belong to, if any */
100 struct rb_root *p_root;
101 /* sorted list of pending requests */
102 struct rb_root sort_list;
103 /* if fifo isn't expired, next request to serve */
104 struct request *next_rq;
105 /* requests queued in sort_list */
106 int queued[2];
107 /* currently allocated requests */
108 int allocated[2];
109 /* fifo list of requests in sort_list */
110 struct list_head fifo;
111
112 unsigned long slice_end;
113 long slice_resid;
114 unsigned int slice_dispatch;
115
116 /* pending metadata requests */
117 int meta_pending;
118 /* number of requests that are on the dispatch list or inside driver */
119 int dispatched;
120
121 /* io prio of this group */
122 unsigned short ioprio, org_ioprio;
123 unsigned short ioprio_class, org_ioprio_class;
124
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400125 unsigned int seek_samples;
126 u64 seek_total;
127 sector_t seek_mean;
128 sector_t last_request_pos;
Jeff Moyere6c5bc72009-10-23 17:14:52 -0400129 unsigned long seeky_start;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400130
Jens Axboe6118b702009-06-30 09:34:12 +0200131 pid_t pid;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400132
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100133 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400134 struct cfq_queue *new_cfqq;
Jens Axboe6118b702009-06-30 09:34:12 +0200135};
136
137/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100138 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100139 * IDLE is handled separately, so it has negative index
140 */
141enum wl_prio_t {
142 IDLE_WORKLOAD = -1,
143 BE_WORKLOAD = 0,
144 RT_WORKLOAD = 1
145};
146
147/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100148 * Second index in the service_trees.
149 */
150enum wl_type_t {
151 ASYNC_WORKLOAD = 0,
152 SYNC_NOIDLE_WORKLOAD = 1,
153 SYNC_WORKLOAD = 2
154};
155
156
157/*
Jens Axboe22e2c502005-06-27 10:55:12 +0200158 * Per block device queue structure
159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160struct cfq_data {
Jens Axboe165125e2007-07-24 09:28:11 +0200161 struct request_queue *queue;
Jens Axboe22e2c502005-06-27 10:55:12 +0200162
163 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100164 * rr lists of queues with requests, onle rr for each priority class.
165 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200166 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100167 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100168 struct cfq_rb_root service_tree_idle;
169 /*
170 * The priority currently being served
171 */
172 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100173 enum wl_type_t serving_type;
174 unsigned long workload_expires;
Jens Axboea36e71f2009-04-15 12:15:11 +0200175
176 /*
177 * Each priority tree is sorted by next_request position. These
178 * trees are used when determining if two or more queues are
179 * interleaving requests (see cfq_close_cooperator).
180 */
181 struct rb_root prio_trees[CFQ_PRIO_LISTS];
182
Jens Axboe22e2c502005-06-27 10:55:12 +0200183 unsigned int busy_queues;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100184 unsigned int busy_queues_avg[2];
Jens Axboe22e2c502005-06-27 10:55:12 +0200185
Jens Axboe5ad531d2009-07-03 12:57:48 +0200186 int rq_in_driver[2];
Jens Axboe3ed9a292007-04-23 08:33:33 +0200187 int sync_flight;
Aaron Carroll45333d52008-08-26 15:52:36 +0200188
189 /*
190 * queue-depth detection
191 */
192 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200193 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100194 /*
195 * hw_tag can be
196 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
197 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
198 * 0 => no NCQ
199 */
200 int hw_tag_est_depth;
201 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200202
203 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200204 * idle window management
205 */
206 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200207 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200208
209 struct cfq_queue *active_queue;
210 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200211
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200212 /*
213 * async queue for each priority case
214 */
215 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
216 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200217
Jens Axboe6d048f52007-04-25 12:44:27 +0200218 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
221 * tunables, see top of file
222 */
223 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200224 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned int cfq_back_penalty;
226 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200227 unsigned int cfq_slice[2];
228 unsigned int cfq_slice_async_rq;
229 unsigned int cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200230 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500231
232 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Jens Axboe6118b702009-06-30 09:34:12 +0200234 /*
235 * Fallback dummy cfqq for extreme OOM conditions
236 */
237 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200238
239 unsigned long last_end_sync_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100242static struct cfq_rb_root *service_tree_for(enum wl_prio_t prio,
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100243 enum wl_type_t type,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100244 struct cfq_data *cfqd)
245{
246 if (prio == IDLE_WORKLOAD)
247 return &cfqd->service_tree_idle;
248
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100249 return &cfqd->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100250}
251
Jens Axboe3b181522005-06-27 10:56:24 +0200252enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100253 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
254 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200255 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100256 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100257 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
258 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
259 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100260 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200261 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400262 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Jens Axboe3b181522005-06-27 10:56:24 +0200263};
264
265#define CFQ_CFQQ_FNS(name) \
266static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
267{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100268 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200269} \
270static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
271{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100272 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200273} \
274static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
275{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100276 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200277}
278
279CFQ_CFQQ_FNS(on_rr);
280CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200281CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200282CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200283CFQ_CFQQ_FNS(fifo_expire);
284CFQ_CFQQ_FNS(idle_window);
285CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100286CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200287CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200288CFQ_CFQQ_FNS(coop);
Jens Axboe3b181522005-06-27 10:56:24 +0200289#undef CFQ_CFQQ_FNS
290
Jens Axboe7b679132008-05-30 12:23:07 +0200291#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
292 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
293#define cfq_log(cfqd, fmt, args...) \
294 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
295
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100296static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
297{
298 if (cfq_class_idle(cfqq))
299 return IDLE_WORKLOAD;
300 if (cfq_class_rt(cfqq))
301 return RT_WORKLOAD;
302 return BE_WORKLOAD;
303}
304
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100305
306static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
307{
308 if (!cfq_cfqq_sync(cfqq))
309 return ASYNC_WORKLOAD;
310 if (!cfq_cfqq_idle_window(cfqq))
311 return SYNC_NOIDLE_WORKLOAD;
312 return SYNC_WORKLOAD;
313}
314
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100315static inline int cfq_busy_queues_wl(enum wl_prio_t wl, struct cfq_data *cfqd)
316{
317 if (wl == IDLE_WORKLOAD)
318 return cfqd->service_tree_idle.count;
319
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100320 return cfqd->service_trees[wl][ASYNC_WORKLOAD].count
321 + cfqd->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
322 + cfqd->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100323}
324
Jens Axboe165125e2007-07-24 09:28:11 +0200325static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200326static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100327 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100328static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200329 struct io_context *);
330
Jens Axboe5ad531d2009-07-03 12:57:48 +0200331static inline int rq_in_driver(struct cfq_data *cfqd)
332{
333 return cfqd->rq_in_driver[0] + cfqd->rq_in_driver[1];
334}
335
Vasily Tarasov91fac312007-04-25 12:29:51 +0200336static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200337 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200338{
Jens Axboea6151c32009-10-07 20:02:57 +0200339 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200340}
341
342static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200343 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200344{
Jens Axboea6151c32009-10-07 20:02:57 +0200345 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200346}
347
348/*
349 * We regard a request as SYNC, if it's either a read or has the SYNC bit
350 * set (in which case it could also be direct WRITE).
351 */
Jens Axboea6151c32009-10-07 20:02:57 +0200352static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200353{
Jens Axboea6151c32009-10-07 20:02:57 +0200354 return bio_data_dir(bio) == READ || bio_rw_flagged(bio, BIO_RW_SYNCIO);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200355}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700358 * scheduler run of queue, if there are requests pending and no one in the
359 * driver that will restart queueing
360 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200361static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700362{
Jens Axboe7b679132008-05-30 12:23:07 +0200363 if (cfqd->busy_queues) {
364 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200365 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200366 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700367}
368
Jens Axboe165125e2007-07-24 09:28:11 +0200369static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700370{
371 struct cfq_data *cfqd = q->elevator->elevator_data;
372
Jens Axboeb4878f22005-10-20 16:42:29 +0200373 return !cfqd->busy_queues;
Andrew Morton99f95e52005-06-27 20:14:05 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100377 * Scale schedule slice based on io priority. Use the sync time slice only
378 * if a queue is marked sync and has sync io queued. A sync queue with async
379 * io only, should not get full sync slice length.
380 */
Jens Axboea6151c32009-10-07 20:02:57 +0200381static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200382 unsigned short prio)
383{
384 const int base_slice = cfqd->cfq_slice[sync];
385
386 WARN_ON(prio >= IOPRIO_BE_NR);
387
388 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
389}
390
Jens Axboe44f7c162007-01-19 11:51:58 +1100391static inline int
392cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
393{
Jens Axboed9e76202007-04-20 14:27:50 +0200394 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100395}
396
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100397/*
398 * get averaged number of queues of RT/BE priority.
399 * average is updated, with a formula that gives more weight to higher numbers,
400 * to quickly follows sudden increases and decrease slowly
401 */
402
Jens Axboe5869619c2009-10-28 09:27:07 +0100403static inline unsigned cfq_get_avg_queues(struct cfq_data *cfqd, bool rt)
404{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100405 unsigned min_q, max_q;
406 unsigned mult = cfq_hist_divisor - 1;
407 unsigned round = cfq_hist_divisor / 2;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100408 unsigned busy = cfq_busy_queues_wl(rt, cfqd);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100409
410 min_q = min(cfqd->busy_queues_avg[rt], busy);
411 max_q = max(cfqd->busy_queues_avg[rt], busy);
412 cfqd->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
413 cfq_hist_divisor;
414 return cfqd->busy_queues_avg[rt];
415}
416
Jens Axboe44f7c162007-01-19 11:51:58 +1100417static inline void
418cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
419{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100420 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
421 if (cfqd->cfq_latency) {
422 /* interested queues (we consider only the ones with the same
423 * priority class) */
424 unsigned iq = cfq_get_avg_queues(cfqd, cfq_class_rt(cfqq));
425 unsigned sync_slice = cfqd->cfq_slice[1];
426 unsigned expect_latency = sync_slice * iq;
427 if (expect_latency > cfq_target_latency) {
428 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
429 /* scale low_slice according to IO priority
430 * and sync vs async */
431 unsigned low_slice =
432 min(slice, base_low_slice * slice / sync_slice);
433 /* the adapted slice value is scaled to fit all iqs
434 * into the target latency */
435 slice = max(slice * cfq_target_latency / expect_latency,
436 low_slice);
437 }
438 }
439 cfqq->slice_end = jiffies + slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200440 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100441}
442
443/*
444 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
445 * isn't valid until the first request from the dispatch is activated
446 * and the slice time set.
447 */
Jens Axboea6151c32009-10-07 20:02:57 +0200448static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100449{
450 if (cfq_cfqq_slice_new(cfqq))
451 return 0;
452 if (time_before(jiffies, cfqq->slice_end))
453 return 0;
454
455 return 1;
456}
457
458/*
Jens Axboe5e705372006-07-13 12:39:25 +0200459 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200461 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Jens Axboe5e705372006-07-13 12:39:25 +0200463static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100464cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100466 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200468#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
469#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
470 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Jens Axboe5e705372006-07-13 12:39:25 +0200472 if (rq1 == NULL || rq1 == rq2)
473 return rq2;
474 if (rq2 == NULL)
475 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200476
Jens Axboe5e705372006-07-13 12:39:25 +0200477 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
478 return rq1;
479 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
480 return rq2;
Jens Axboe374f84a2006-07-23 01:42:19 +0200481 if (rq_is_meta(rq1) && !rq_is_meta(rq2))
482 return rq1;
483 else if (rq_is_meta(rq2) && !rq_is_meta(rq1))
484 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Tejun Heo83096eb2009-05-07 22:24:39 +0900486 s1 = blk_rq_pos(rq1);
487 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * by definition, 1KiB is 2 sectors
491 */
492 back_max = cfqd->cfq_back_max * 2;
493
494 /*
495 * Strict one way elevator _except_ in the case where we allow
496 * short backward seeks which are biased as twice the cost of a
497 * similar forward seek.
498 */
499 if (s1 >= last)
500 d1 = s1 - last;
501 else if (s1 + back_max >= last)
502 d1 = (last - s1) * cfqd->cfq_back_penalty;
503 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200504 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (s2 >= last)
507 d2 = s2 - last;
508 else if (s2 + back_max >= last)
509 d2 = (last - s2) * cfqd->cfq_back_penalty;
510 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200511 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Andreas Mohre8a99052006-03-28 08:59:49 +0200515 /*
516 * By doing switch() on the bit mask "wrap" we avoid having to
517 * check two variables for all permutations: --> faster!
518 */
519 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200520 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200521 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200522 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200523 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200524 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200525 else {
526 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200527 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200528 else
Jens Axboe5e705372006-07-13 12:39:25 +0200529 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200530 }
531
532 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200533 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200534 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200535 return rq2;
536 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200537 default:
538 /*
539 * Since both rqs are wrapped,
540 * start with the one that's further behind head
541 * (--> only *one* back seek required),
542 * since back seek takes more time than forward.
543 */
544 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200545 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 else
Jens Axboe5e705372006-07-13 12:39:25 +0200547 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549}
550
Jens Axboe498d3aa22007-04-26 12:54:48 +0200551/*
552 * The below is leftmost cache rbtree addon
553 */
Jens Axboe08717142008-01-28 11:38:15 +0100554static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200555{
556 if (!root->left)
557 root->left = rb_first(&root->rb);
558
Jens Axboe08717142008-01-28 11:38:15 +0100559 if (root->left)
560 return rb_entry(root->left, struct cfq_queue, rb_node);
561
562 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200563}
564
Jens Axboea36e71f2009-04-15 12:15:11 +0200565static void rb_erase_init(struct rb_node *n, struct rb_root *root)
566{
567 rb_erase(n, root);
568 RB_CLEAR_NODE(n);
569}
570
Jens Axboecc09e292007-04-26 12:53:50 +0200571static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
572{
573 if (root->left == n)
574 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200575 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100576 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * would be nice to take fifo expire time into account as well
581 */
Jens Axboe5e705372006-07-13 12:39:25 +0200582static struct request *
583cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
584 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Jens Axboe21183b02006-07-13 12:33:14 +0200586 struct rb_node *rbnext = rb_next(&last->rb_node);
587 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200588 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Jens Axboe21183b02006-07-13 12:33:14 +0200590 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200593 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200596 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200597 else {
598 rbnext = rb_first(&cfqq->sort_list);
599 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200600 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100603 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Jens Axboed9e76202007-04-20 14:27:50 +0200606static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
607 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Gui Jianfeng3586e912009-11-26 09:14:11 +0100609 struct cfq_rb_root *service_tree;
610
611 service_tree = service_tree_for(cfqq_prio(cfqq), cfqq_type(cfqq), cfqd);
612
Jens Axboed9e76202007-04-20 14:27:50 +0200613 /*
614 * just an approximation, should be ok.
615 */
Gui Jianfeng3586e912009-11-26 09:14:11 +0100616 return service_tree->count * (cfq_prio_slice(cfqd, 1, 0) -
617 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jens Axboe498d3aa22007-04-26 12:54:48 +0200620/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100621 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +0200622 * requests waiting to be processed. It is sorted in the order that
623 * we will service the queues.
624 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200625static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +0200626 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +0200627{
Jens Axboe08717142008-01-28 11:38:15 +0100628 struct rb_node **p, *parent;
629 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +0200630 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100631 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +0200632 int left;
Jens Axboed9e76202007-04-20 14:27:50 +0200633
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100634 service_tree = service_tree_for(cfqq_prio(cfqq), cfqq_type(cfqq), cfqd);
Jens Axboe08717142008-01-28 11:38:15 +0100635 if (cfq_class_idle(cfqq)) {
636 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100637 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +0100638 if (parent && parent != &cfqq->rb_node) {
639 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
640 rb_key += __cfqq->rb_key;
641 } else
642 rb_key += jiffies;
643 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +0200644 /*
645 * Get our rb key offset. Subtract any residual slice
646 * value carried from last service. A negative resid
647 * count indicates slice overrun, and this should position
648 * the next service time further away in the tree.
649 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200650 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +0200651 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +0200652 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +0200653 } else {
654 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100655 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +0200656 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
657 }
Jens Axboed9e76202007-04-20 14:27:50 +0200658
659 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Jens Axboe99f96282007-02-05 11:56:25 +0100660 /*
Jens Axboed9e76202007-04-20 14:27:50 +0200661 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +0100662 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100663 if (rb_key == cfqq->rb_key &&
664 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +0200665 return;
Jens Axboe53b037442006-07-28 09:48:51 +0200666
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100667 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
668 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200669 }
Jens Axboed9e76202007-04-20 14:27:50 +0200670
Jens Axboe498d3aa22007-04-26 12:54:48 +0200671 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +0100672 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100673 cfqq->service_tree = service_tree;
674 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +0200675 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +0200676 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +0200677
Jens Axboed9e76202007-04-20 14:27:50 +0200678 parent = *p;
679 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
680
Jens Axboe0c534e02007-04-18 20:01:57 +0200681 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100682 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +0200683 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100684 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +0200685 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100686 else {
Jens Axboe67060e32007-04-18 20:13:32 +0200687 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +0200688 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100689 }
Jens Axboe67060e32007-04-18 20:13:32 +0200690
691 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +0200692 }
693
Jens Axboecc09e292007-04-26 12:53:50 +0200694 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100695 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +0200696
Jens Axboed9e76202007-04-20 14:27:50 +0200697 cfqq->rb_key = rb_key;
698 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100699 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
700 service_tree->count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Jens Axboea36e71f2009-04-15 12:15:11 +0200703static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200704cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
705 sector_t sector, struct rb_node **ret_parent,
706 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +0200707{
Jens Axboea36e71f2009-04-15 12:15:11 +0200708 struct rb_node **p, *parent;
709 struct cfq_queue *cfqq = NULL;
710
711 parent = NULL;
712 p = &root->rb_node;
713 while (*p) {
714 struct rb_node **n;
715
716 parent = *p;
717 cfqq = rb_entry(parent, struct cfq_queue, p_node);
718
719 /*
720 * Sort strictly based on sector. Smallest to the left,
721 * largest to the right.
722 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900723 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +0200724 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900725 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +0200726 n = &(*p)->rb_left;
727 else
728 break;
729 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200730 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200731 }
732
733 *ret_parent = parent;
734 if (rb_link)
735 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200736 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +0200737}
738
739static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
740{
Jens Axboea36e71f2009-04-15 12:15:11 +0200741 struct rb_node **p, *parent;
742 struct cfq_queue *__cfqq;
743
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200744 if (cfqq->p_root) {
745 rb_erase(&cfqq->p_node, cfqq->p_root);
746 cfqq->p_root = NULL;
747 }
Jens Axboea36e71f2009-04-15 12:15:11 +0200748
749 if (cfq_class_idle(cfqq))
750 return;
751 if (!cfqq->next_rq)
752 return;
753
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200754 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +0900755 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
756 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +0200757 if (!__cfqq) {
758 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200759 rb_insert_color(&cfqq->p_node, cfqq->p_root);
760 } else
761 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200762}
763
Jens Axboe498d3aa22007-04-26 12:54:48 +0200764/*
765 * Update cfqq's position in the service tree.
766 */
Jens Axboeedd75ff2007-04-19 12:03:34 +0200767static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +0200768{
Jens Axboe6d048f52007-04-25 12:44:27 +0200769 /*
770 * Resorting requires the cfqq to be on the RR list already.
771 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200772 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +0200773 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +0200774 cfq_prio_tree_add(cfqd, cfqq);
775 }
Jens Axboe6d048f52007-04-25 12:44:27 +0200776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +0200780 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 */
Jens Axboefebffd62008-01-28 13:19:43 +0100782static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jens Axboe7b679132008-05-30 12:23:07 +0200784 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +0200785 BUG_ON(cfq_cfqq_on_rr(cfqq));
786 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 cfqd->busy_queues++;
788
Jens Axboeedd75ff2007-04-19 12:03:34 +0200789 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Jens Axboe498d3aa22007-04-26 12:54:48 +0200792/*
793 * Called when the cfqq no longer has requests pending, remove it from
794 * the service tree.
795 */
Jens Axboefebffd62008-01-28 13:19:43 +0100796static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Jens Axboe7b679132008-05-30 12:23:07 +0200798 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +0200799 BUG_ON(!cfq_cfqq_on_rr(cfqq));
800 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100802 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
803 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
804 cfqq->service_tree = NULL;
805 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +0200806 if (cfqq->p_root) {
807 rb_erase(&cfqq->p_node, cfqq->p_root);
808 cfqq->p_root = NULL;
809 }
Jens Axboed9e76202007-04-20 14:27:50 +0200810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 BUG_ON(!cfqd->busy_queues);
812 cfqd->busy_queues--;
813}
814
815/*
816 * rb tree support functions
817 */
Jens Axboefebffd62008-01-28 13:19:43 +0100818static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Jens Axboe5e705372006-07-13 12:39:25 +0200820 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200821 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5e705372006-07-13 12:39:25 +0200822 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Jens Axboeb4878f22005-10-20 16:42:29 +0200824 BUG_ON(!cfqq->queued[sync]);
825 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jens Axboe5e705372006-07-13 12:39:25 +0200827 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jens Axboedd67d052006-06-21 09:36:18 +0200829 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboeb4878f22005-10-20 16:42:29 +0200830 cfq_del_cfqq_rr(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Jens Axboe5e705372006-07-13 12:39:25 +0200833static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Jens Axboe5e705372006-07-13 12:39:25 +0200835 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +0200837 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jens Axboe5380a102006-07-13 12:37:56 +0200839 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /*
842 * looks a little odd, but the first insert might return an alias.
843 * if that happens, put the alias on the dispatch list
844 */
Jens Axboe21183b02006-07-13 12:33:14 +0200845 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +0200846 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +0100847
848 if (!cfq_cfqq_on_rr(cfqq))
849 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +0200850
851 /*
852 * check if this request is a better next-serve candidate
853 */
Jens Axboea36e71f2009-04-15 12:15:11 +0200854 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100855 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +0200856
857 /*
858 * adjust priority tree position, if ->next_rq changes
859 */
860 if (prev != cfqq->next_rq)
861 cfq_prio_tree_add(cfqd, cfqq);
862
Jens Axboe5044eed2007-04-25 11:53:48 +0200863 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Jens Axboefebffd62008-01-28 13:19:43 +0100866static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Jens Axboe5380a102006-07-13 12:37:56 +0200868 elv_rb_del(&cfqq->sort_list, rq);
869 cfqq->queued[rq_is_sync(rq)]--;
Jens Axboe5e705372006-07-13 12:39:25 +0200870 cfq_add_rq_rb(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Jens Axboe206dc692006-03-28 13:03:44 +0200873static struct request *
874cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jens Axboe206dc692006-03-28 13:03:44 +0200876 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200877 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +0200878 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jens Axboe4ac845a2008-01-24 08:44:49 +0100880 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200881 if (!cic)
882 return NULL;
883
884 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +0200885 if (cfqq) {
886 sector_t sector = bio->bi_sector + bio_sectors(bio);
887
Jens Axboe21183b02006-07-13 12:33:14 +0200888 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +0200889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return NULL;
892}
893
Jens Axboe165125e2007-07-24 09:28:11 +0200894static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +0200895{
896 struct cfq_data *cfqd = q->elevator->elevator_data;
897
Jens Axboe5ad531d2009-07-03 12:57:48 +0200898 cfqd->rq_in_driver[rq_is_sync(rq)]++;
Jens Axboe7b679132008-05-30 12:23:07 +0200899 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +0200900 rq_in_driver(cfqd));
Jens Axboe25776e32006-06-01 10:12:26 +0200901
Tejun Heo5b936292009-05-07 22:24:38 +0900902 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +0200903}
904
Jens Axboe165125e2007-07-24 09:28:11 +0200905static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jens Axboe22e2c502005-06-27 10:55:12 +0200907 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5ad531d2009-07-03 12:57:48 +0200908 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Jens Axboe5ad531d2009-07-03 12:57:48 +0200910 WARN_ON(!cfqd->rq_in_driver[sync]);
911 cfqd->rq_in_driver[sync]--;
Jens Axboe7b679132008-05-30 12:23:07 +0200912 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Jens Axboe5ad531d2009-07-03 12:57:48 +0200913 rq_in_driver(cfqd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Jens Axboeb4878f22005-10-20 16:42:29 +0200916static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Jens Axboe5e705372006-07-13 12:39:25 +0200918 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +0200919
Jens Axboe5e705372006-07-13 12:39:25 +0200920 if (cfqq->next_rq == rq)
921 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Jens Axboeb4878f22005-10-20 16:42:29 +0200923 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +0200924 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +0200925
Aaron Carroll45333d52008-08-26 15:52:36 +0200926 cfqq->cfqd->rq_queued--;
Jens Axboe374f84a2006-07-23 01:42:19 +0200927 if (rq_is_meta(rq)) {
928 WARN_ON(!cfqq->meta_pending);
929 cfqq->meta_pending--;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Jens Axboe165125e2007-07-24 09:28:11 +0200933static int cfq_merge(struct request_queue *q, struct request **req,
934 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct cfq_data *cfqd = q->elevator->elevator_data;
937 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jens Axboe206dc692006-03-28 13:03:44 +0200939 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +0200940 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +0200941 *req = __rq;
942 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
945 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Jens Axboe165125e2007-07-24 09:28:11 +0200948static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +0200949 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Jens Axboe21183b02006-07-13 12:33:14 +0200951 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +0200952 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jens Axboe5e705372006-07-13 12:39:25 +0200954 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958static void
Jens Axboe165125e2007-07-24 09:28:11 +0200959cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct request *next)
961{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100962 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +0200963 /*
964 * reposition in fifo if next is older than rq
965 */
966 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +0200967 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +0200968 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +0200969 rq_set_fifo_time(rq, rq_fifo_time(next));
970 }
Jens Axboe22e2c502005-06-27 10:55:12 +0200971
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100972 if (cfqq->next_rq == next)
973 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +0200974 cfq_remove_request(next);
Jens Axboe22e2c502005-06-27 10:55:12 +0200975}
976
Jens Axboe165125e2007-07-24 09:28:11 +0200977static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +0100978 struct bio *bio)
979{
980 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200981 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +0100982 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +0100983
984 /*
Jens Axboeec8acb62007-01-02 18:32:11 +0100985 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +0100986 */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200987 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +0200988 return false;
Jens Axboeda775262006-12-20 11:04:12 +0100989
990 /*
Jens Axboe719d3402006-12-22 09:38:53 +0100991 * Lookup the cfqq that this bio will be queued with. Allow
992 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +0100993 */
Jens Axboe4ac845a2008-01-24 08:44:49 +0100994 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200995 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +0200996 return false;
Jens Axboe719d3402006-12-22 09:38:53 +0100997
Vasily Tarasov91fac312007-04-25 12:29:51 +0200998 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +0200999 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001000}
1001
Jens Axboefebffd62008-01-28 13:19:43 +01001002static void __cfq_set_active_queue(struct cfq_data *cfqd,
1003 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001004{
1005 if (cfqq) {
Jens Axboe7b679132008-05-30 12:23:07 +02001006 cfq_log_cfqq(cfqd, cfqq, "set_active");
Jens Axboe22e2c502005-06-27 10:55:12 +02001007 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001008 cfqq->slice_dispatch = 0;
1009
Jens Axboe2f5cb732009-04-07 08:51:19 +02001010 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +02001011 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001012 cfq_clear_cfqq_must_alloc_slice(cfqq);
1013 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +11001014 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001015
1016 del_timer(&cfqd->idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02001017 }
1018
1019 cfqd->active_queue = cfqq;
1020}
1021
1022/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001023 * current cfqq expired its slice (or was too idle), select new one
1024 */
1025static void
1026__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001027 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001028{
Jens Axboe7b679132008-05-30 12:23:07 +02001029 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1030
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001031 if (cfq_cfqq_wait_request(cfqq))
1032 del_timer(&cfqd->idle_slice_timer);
1033
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001034 cfq_clear_cfqq_wait_request(cfqq);
1035
1036 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001037 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001038 */
Jens Axboe7b679132008-05-30 12:23:07 +02001039 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +11001040 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001041 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1042 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001043
Jens Axboeedd75ff2007-04-19 12:03:34 +02001044 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001045
1046 if (cfqq == cfqd->active_queue)
1047 cfqd->active_queue = NULL;
1048
1049 if (cfqd->active_cic) {
1050 put_io_context(cfqd->active_cic->ioc);
1051 cfqd->active_cic = NULL;
1052 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001053}
1054
Jens Axboea6151c32009-10-07 20:02:57 +02001055static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001056{
1057 struct cfq_queue *cfqq = cfqd->active_queue;
1058
1059 if (cfqq)
Jens Axboe6084cdd2007-04-23 08:25:00 +02001060 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001061}
1062
Jens Axboe498d3aa22007-04-26 12:54:48 +02001063/*
1064 * Get next queue for service. Unless we have a queue preemption,
1065 * we'll simply select the first cfqq in the service tree.
1066 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001067static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001068{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001069 struct cfq_rb_root *service_tree =
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001070 service_tree_for(cfqd->serving_prio, cfqd->serving_type, cfqd);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001071
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001072 if (RB_EMPTY_ROOT(&service_tree->rb))
1073 return NULL;
1074 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001075}
1076
Jens Axboe498d3aa22007-04-26 12:54:48 +02001077/*
1078 * Get and set a new active queue for service.
1079 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001080static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1081 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001082{
Jens Axboee00ef792009-11-04 08:54:55 +01001083 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001084 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001085
Jens Axboe22e2c502005-06-27 10:55:12 +02001086 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001087 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001088}
1089
Jens Axboed9e76202007-04-20 14:27:50 +02001090static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1091 struct request *rq)
1092{
Tejun Heo83096eb2009-05-07 22:24:39 +09001093 if (blk_rq_pos(rq) >= cfqd->last_position)
1094 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001095 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001096 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001097}
1098
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001099#define CFQQ_SEEK_THR 8 * 1024
1100#define CFQQ_SEEKY(cfqq) ((cfqq)->seek_mean > CFQQ_SEEK_THR)
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001101
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001102static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1103 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001104{
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001105 sector_t sdist = cfqq->seek_mean;
Jens Axboecaaa5f92006-06-16 11:23:00 +02001106
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001107 if (!sample_valid(cfqq->seek_samples))
1108 sdist = CFQQ_SEEK_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001109
Jeff Moyer04dc6e72009-04-21 07:31:56 +02001110 return cfq_dist_from_last(cfqd, rq) <= sdist;
Jens Axboe6d048f52007-04-25 12:44:27 +02001111}
1112
Jens Axboea36e71f2009-04-15 12:15:11 +02001113static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1114 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001115{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001116 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001117 struct rb_node *parent, *node;
1118 struct cfq_queue *__cfqq;
1119 sector_t sector = cfqd->last_position;
1120
1121 if (RB_EMPTY_ROOT(root))
1122 return NULL;
1123
1124 /*
1125 * First, if we find a request starting at the end of the last
1126 * request, choose it.
1127 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001128 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001129 if (__cfqq)
1130 return __cfqq;
1131
1132 /*
1133 * If the exact sector wasn't found, the parent of the NULL leaf
1134 * will contain the closest sector.
1135 */
1136 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001137 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001138 return __cfqq;
1139
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001140 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001141 node = rb_next(&__cfqq->p_node);
1142 else
1143 node = rb_prev(&__cfqq->p_node);
1144 if (!node)
1145 return NULL;
1146
1147 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001148 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001149 return __cfqq;
1150
1151 return NULL;
1152}
1153
1154/*
1155 * cfqd - obvious
1156 * cur_cfqq - passed in so that we don't decide that the current queue is
1157 * closely cooperating with itself.
1158 *
1159 * So, basically we're assuming that that cur_cfqq has dispatched at least
1160 * one request, and that cfqd->last_position reflects a position on the disk
1161 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1162 * assumption.
1163 */
1164static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001165 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001166{
1167 struct cfq_queue *cfqq;
1168
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001169 if (!cfq_cfqq_sync(cur_cfqq))
1170 return NULL;
1171 if (CFQQ_SEEKY(cur_cfqq))
1172 return NULL;
1173
Jens Axboea36e71f2009-04-15 12:15:11 +02001174 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001175 * We should notice if some of the queues are cooperating, eg
1176 * working closely on the same area of the disk. In that case,
1177 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001178 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001179 cfqq = cfqq_close(cfqd, cur_cfqq);
1180 if (!cfqq)
1181 return NULL;
1182
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001183 /*
1184 * It only makes sense to merge sync queues.
1185 */
1186 if (!cfq_cfqq_sync(cfqq))
1187 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001188 if (CFQQ_SEEKY(cfqq))
1189 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001190
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001191 /*
1192 * Do not merge queues of different priority classes
1193 */
1194 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1195 return NULL;
1196
Jens Axboea36e71f2009-04-15 12:15:11 +02001197 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001198}
1199
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001200/*
1201 * Determine whether we should enforce idle window for this queue.
1202 */
1203
1204static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1205{
1206 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001207 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001208
1209 /* We never do for idle class queues. */
1210 if (prio == IDLE_WORKLOAD)
1211 return false;
1212
1213 /* We do for queues that were marked with idle window flag. */
1214 if (cfq_cfqq_idle_window(cfqq))
1215 return true;
1216
1217 /*
1218 * Otherwise, we do only if they are the last ones
1219 * in their service tree.
1220 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001221 if (!service_tree)
1222 service_tree = service_tree_for(prio, cfqq_type(cfqq), cfqd);
1223
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001224 if (service_tree->count == 0)
1225 return true;
1226
1227 return (service_tree->count == 1 && cfq_rb_first(service_tree) == cfqq);
1228}
1229
Jens Axboe6d048f52007-04-25 12:44:27 +02001230static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001231{
Jens Axboe17926692007-01-19 11:59:30 +11001232 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001233 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001234 unsigned long sl;
1235
Jens Axboea68bbdd2008-09-24 13:03:33 +02001236 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001237 * SSD device without seek penalty, disable idling. But only do so
1238 * for devices that support queuing, otherwise we still have a problem
1239 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001240 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001241 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001242 return;
1243
Jens Axboedd67d052006-06-21 09:36:18 +02001244 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001245 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001246
1247 /*
1248 * idle is disabled, either manually or by past process history
1249 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001250 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
Jens Axboe6d048f52007-04-25 12:44:27 +02001251 return;
1252
Jens Axboe22e2c502005-06-27 10:55:12 +02001253 /*
Jens Axboe7b679132008-05-30 12:23:07 +02001254 * still requests with the driver, don't idle
1255 */
Jens Axboe5ad531d2009-07-03 12:57:48 +02001256 if (rq_in_driver(cfqd))
Jens Axboe7b679132008-05-30 12:23:07 +02001257 return;
1258
1259 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001260 * task has exited, don't wait
1261 */
Jens Axboe206dc692006-03-28 13:03:44 +02001262 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001263 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001264 return;
1265
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001266 /*
1267 * If our average think time is larger than the remaining time
1268 * slice, then don't idle. This avoids overrunning the allotted
1269 * time slice.
1270 */
1271 if (sample_valid(cic->ttime_samples) &&
1272 (cfqq->slice_end - jiffies < cic->ttime_mean))
1273 return;
1274
Jens Axboe3b181522005-06-27 10:56:24 +02001275 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001276
Jens Axboe6d048f52007-04-25 12:44:27 +02001277 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001278
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001279 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001280 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Jens Axboe498d3aa22007-04-26 12:54:48 +02001283/*
1284 * Move request from internal lists to the request queue dispatch list.
1285 */
Jens Axboe165125e2007-07-24 09:28:11 +02001286static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001288 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001289 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001290
Jens Axboe7b679132008-05-30 12:23:07 +02001291 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1292
Jeff Moyer06d21882009-09-11 17:08:59 +02001293 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001294 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001295 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001296 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001297
1298 if (cfq_cfqq_sync(cfqq))
1299 cfqd->sync_flight++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/*
1303 * return expired entry, or NULL to just start from scratch in rbtree
1304 */
Jens Axboefebffd62008-01-28 13:19:43 +01001305static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Jens Axboe30996f42009-10-05 11:03:39 +02001307 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Jens Axboe3b181522005-06-27 10:56:24 +02001309 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001311
1312 cfq_mark_cfqq_fifo_expire(cfqq);
1313
Jens Axboe89850f72006-07-22 16:48:31 +02001314 if (list_empty(&cfqq->fifo))
1315 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Jens Axboe89850f72006-07-22 16:48:31 +02001317 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001318 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001319 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Jens Axboe30996f42009-10-05 11:03:39 +02001321 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001322 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Jens Axboe22e2c502005-06-27 10:55:12 +02001325static inline int
1326cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1327{
1328 const int base_rq = cfqd->cfq_slice_async_rq;
1329
1330 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1331
1332 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1333}
1334
1335/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001336 * Must be called with the queue_lock held.
1337 */
1338static int cfqq_process_refs(struct cfq_queue *cfqq)
1339{
1340 int process_refs, io_refs;
1341
1342 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1343 process_refs = atomic_read(&cfqq->ref) - io_refs;
1344 BUG_ON(process_refs < 0);
1345 return process_refs;
1346}
1347
1348static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1349{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001350 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001351 struct cfq_queue *__cfqq;
1352
1353 /* Avoid a circular list and skip interim queue merges */
1354 while ((__cfqq = new_cfqq->new_cfqq)) {
1355 if (__cfqq == cfqq)
1356 return;
1357 new_cfqq = __cfqq;
1358 }
1359
1360 process_refs = cfqq_process_refs(cfqq);
1361 /*
1362 * If the process for the cfqq has gone away, there is no
1363 * sense in merging the queues.
1364 */
1365 if (process_refs == 0)
1366 return;
1367
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001368 /*
1369 * Merge in the direction of the lesser amount of work.
1370 */
1371 new_process_refs = cfqq_process_refs(new_cfqq);
1372 if (new_process_refs >= process_refs) {
1373 cfqq->new_cfqq = new_cfqq;
1374 atomic_add(process_refs, &new_cfqq->ref);
1375 } else {
1376 new_cfqq->new_cfqq = cfqq;
1377 atomic_add(new_process_refs, &cfqq->ref);
1378 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001379}
1380
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001381static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd, enum wl_prio_t prio,
1382 bool prio_changed)
1383{
1384 struct cfq_queue *queue;
1385 int i;
1386 bool key_valid = false;
1387 unsigned long lowest_key = 0;
1388 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
1389
1390 if (prio_changed) {
1391 /*
1392 * When priorities switched, we prefer starting
1393 * from SYNC_NOIDLE (first choice), or just SYNC
1394 * over ASYNC
1395 */
1396 if (service_tree_for(prio, cur_best, cfqd)->count)
1397 return cur_best;
1398 cur_best = SYNC_WORKLOAD;
1399 if (service_tree_for(prio, cur_best, cfqd)->count)
1400 return cur_best;
1401
1402 return ASYNC_WORKLOAD;
1403 }
1404
1405 for (i = 0; i < 3; ++i) {
1406 /* otherwise, select the one with lowest rb_key */
1407 queue = cfq_rb_first(service_tree_for(prio, i, cfqd));
1408 if (queue &&
1409 (!key_valid || time_before(queue->rb_key, lowest_key))) {
1410 lowest_key = queue->rb_key;
1411 cur_best = i;
1412 key_valid = true;
1413 }
1414 }
1415
1416 return cur_best;
1417}
1418
1419static void choose_service_tree(struct cfq_data *cfqd)
1420{
1421 enum wl_prio_t previous_prio = cfqd->serving_prio;
1422 bool prio_changed;
1423 unsigned slice;
1424 unsigned count;
1425
1426 /* Choose next priority. RT > BE > IDLE */
1427 if (cfq_busy_queues_wl(RT_WORKLOAD, cfqd))
1428 cfqd->serving_prio = RT_WORKLOAD;
1429 else if (cfq_busy_queues_wl(BE_WORKLOAD, cfqd))
1430 cfqd->serving_prio = BE_WORKLOAD;
1431 else {
1432 cfqd->serving_prio = IDLE_WORKLOAD;
1433 cfqd->workload_expires = jiffies + 1;
1434 return;
1435 }
1436
1437 /*
1438 * For RT and BE, we have to choose also the type
1439 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
1440 * expiration time
1441 */
1442 prio_changed = (cfqd->serving_prio != previous_prio);
1443 count = service_tree_for(cfqd->serving_prio, cfqd->serving_type, cfqd)
1444 ->count;
1445
1446 /*
1447 * If priority didn't change, check workload expiration,
1448 * and that we still have other queues ready
1449 */
1450 if (!prio_changed && count &&
1451 !time_after(jiffies, cfqd->workload_expires))
1452 return;
1453
1454 /* otherwise select new workload type */
1455 cfqd->serving_type =
1456 cfq_choose_wl(cfqd, cfqd->serving_prio, prio_changed);
1457 count = service_tree_for(cfqd->serving_prio, cfqd->serving_type, cfqd)
1458 ->count;
1459
1460 /*
1461 * the workload slice is computed as a fraction of target latency
1462 * proportional to the number of queues in that workload, over
1463 * all the queues in the same priority class
1464 */
1465 slice = cfq_target_latency * count /
1466 max_t(unsigned, cfqd->busy_queues_avg[cfqd->serving_prio],
1467 cfq_busy_queues_wl(cfqd->serving_prio, cfqd));
1468
1469 if (cfqd->serving_type == ASYNC_WORKLOAD)
1470 /* async workload slice is scaled down according to
1471 * the sync/async slice ratio. */
1472 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
1473 else
1474 /* sync workload slice is at least 2 * cfq_slice_idle */
1475 slice = max(slice, 2 * cfqd->cfq_slice_idle);
1476
1477 slice = max_t(unsigned, slice, CFQ_MIN_TT);
1478 cfqd->workload_expires = jiffies + slice;
1479}
1480
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001481/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02001482 * Select a queue for service. If we have a current active queue,
1483 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02001484 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001485static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001486{
Jens Axboea36e71f2009-04-15 12:15:11 +02001487 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001488
1489 cfqq = cfqd->active_queue;
1490 if (!cfqq)
1491 goto new_queue;
1492
1493 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001494 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02001495 */
Jens Axboeb0291952009-04-07 11:38:31 +02001496 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboe3b181522005-06-27 10:56:24 +02001497 goto expire;
Jens Axboe22e2c502005-06-27 10:55:12 +02001498
1499 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001500 * The active queue has requests and isn't expired, allow it to
1501 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02001502 */
Jens Axboedd67d052006-06-21 09:36:18 +02001503 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02001504 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02001505
1506 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02001507 * If another queue has a request waiting within our mean seek
1508 * distance, let it run. The expire code will check for close
1509 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001510 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02001511 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001512 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001513 if (new_cfqq) {
1514 if (!cfqq->new_cfqq)
1515 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02001516 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001517 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001518
1519 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02001520 * No requests pending. If the active queue still has requests in
1521 * flight or is idling for a new request, allow either of these
1522 * conditions to happen (or time out) before selecting a new queue.
1523 */
Jens Axboecc197472007-04-20 20:45:39 +02001524 if (timer_pending(&cfqd->idle_slice_timer) ||
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001525 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02001526 cfqq = NULL;
1527 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02001528 }
1529
Jens Axboe3b181522005-06-27 10:56:24 +02001530expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02001531 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02001532new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001533 /*
1534 * Current queue expired. Check if we have to switch to a new
1535 * service tree
1536 */
1537 if (!new_cfqq)
1538 choose_service_tree(cfqd);
1539
Jens Axboea36e71f2009-04-15 12:15:11 +02001540 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001541keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02001542 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001543}
1544
Jens Axboefebffd62008-01-28 13:19:43 +01001545static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02001546{
1547 int dispatched = 0;
1548
1549 while (cfqq->next_rq) {
1550 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
1551 dispatched++;
1552 }
1553
1554 BUG_ON(!list_empty(&cfqq->fifo));
1555 return dispatched;
1556}
1557
Jens Axboe498d3aa22007-04-26 12:54:48 +02001558/*
1559 * Drain our current requests. Used for barriers and when switching
1560 * io schedulers on-the-fly.
1561 */
Jens Axboed9e76202007-04-20 14:27:50 +02001562static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001563{
Jens Axboe08717142008-01-28 11:38:15 +01001564 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001565 int dispatched = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001566 int i, j;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001567 for (i = 0; i < 2; ++i)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001568 for (j = 0; j < 3; ++j)
1569 while ((cfqq = cfq_rb_first(&cfqd->service_trees[i][j]))
1570 != NULL)
1571 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001572
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001573 while ((cfqq = cfq_rb_first(&cfqd->service_tree_idle)) != NULL)
Jens Axboed9e76202007-04-20 14:27:50 +02001574 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001575
Jens Axboe6084cdd2007-04-23 08:25:00 +02001576 cfq_slice_expired(cfqd, 0);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001577
1578 BUG_ON(cfqd->busy_queues);
1579
Jeff Moyer69237152009-06-12 15:29:30 +02001580 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01001581 return dispatched;
1582}
1583
Jens Axboe0b182d62009-10-06 20:49:37 +02001584static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02001585{
Jens Axboe2f5cb732009-04-07 08:51:19 +02001586 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Jens Axboe2f5cb732009-04-07 08:51:19 +02001588 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02001589 * Drain async requests before we start sync IO
1590 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001591 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02001592 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02001593
1594 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02001595 * If this is an async queue and we have sync IO in flight, let it wait
1596 */
1597 if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02001598 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001599
1600 max_dispatch = cfqd->cfq_quantum;
1601 if (cfq_class_idle(cfqq))
1602 max_dispatch = 1;
1603
1604 /*
1605 * Does this cfqq already have too much IO in flight?
1606 */
1607 if (cfqq->dispatched >= max_dispatch) {
1608 /*
1609 * idle queue must always only have a single IO in flight
1610 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02001611 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02001612 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02001613
Jens Axboe2f5cb732009-04-07 08:51:19 +02001614 /*
1615 * We have other queues, don't allow more IO from this one
1616 */
1617 if (cfqd->busy_queues > 1)
Jens Axboe0b182d62009-10-06 20:49:37 +02001618 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11001619
Jens Axboe2f5cb732009-04-07 08:51:19 +02001620 /*
Jens Axboe8e296752009-10-03 16:26:03 +02001621 * Sole queue user, allow bigger slice
Vivek Goyal365722b2009-10-03 15:21:27 +02001622 */
Jens Axboe8e296752009-10-03 16:26:03 +02001623 max_dispatch *= 4;
1624 }
1625
1626 /*
1627 * Async queues must wait a bit before being allowed dispatch.
1628 * We also ramp up the dispatch depth gradually for async IO,
1629 * based on the last sync IO we serviced
1630 */
Jens Axboe963b72f2009-10-03 19:42:18 +02001631 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jens Axboe8e296752009-10-03 16:26:03 +02001632 unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
1633 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02001634
Jens Axboe61f0c1d2009-10-03 19:46:03 +02001635 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02001636 if (!depth && !cfqq->dispatched)
1637 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02001638 if (depth < max_dispatch)
1639 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
Jens Axboe0b182d62009-10-06 20:49:37 +02001642 /*
1643 * If we're below the current max, allow a dispatch
1644 */
1645 return cfqq->dispatched < max_dispatch;
1646}
1647
1648/*
1649 * Dispatch a request from cfqq, moving them to the request queue
1650 * dispatch list.
1651 */
1652static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1653{
1654 struct request *rq;
1655
1656 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
1657
1658 if (!cfq_may_dispatch(cfqd, cfqq))
1659 return false;
1660
1661 /*
1662 * follow expired path, else get first next available
1663 */
1664 rq = cfq_check_fifo(cfqq);
1665 if (!rq)
1666 rq = cfqq->next_rq;
1667
1668 /*
1669 * insert request into driver dispatch list
1670 */
1671 cfq_dispatch_insert(cfqd->queue, rq);
1672
1673 if (!cfqd->active_cic) {
1674 struct cfq_io_context *cic = RQ_CIC(rq);
1675
1676 atomic_long_inc(&cic->ioc->refcount);
1677 cfqd->active_cic = cic;
1678 }
1679
1680 return true;
1681}
1682
1683/*
1684 * Find the cfqq that we need to service and move a request from that to the
1685 * dispatch list
1686 */
1687static int cfq_dispatch_requests(struct request_queue *q, int force)
1688{
1689 struct cfq_data *cfqd = q->elevator->elevator_data;
1690 struct cfq_queue *cfqq;
1691
1692 if (!cfqd->busy_queues)
1693 return 0;
1694
1695 if (unlikely(force))
1696 return cfq_forced_dispatch(cfqd);
1697
1698 cfqq = cfq_select_queue(cfqd);
1699 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02001700 return 0;
1701
Jens Axboe2f5cb732009-04-07 08:51:19 +02001702 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02001703 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02001704 */
Jens Axboe0b182d62009-10-06 20:49:37 +02001705 if (!cfq_dispatch_request(cfqd, cfqq))
1706 return 0;
1707
Jens Axboe2f5cb732009-04-07 08:51:19 +02001708 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02001709 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001710
1711 /*
1712 * expire an async queue immediately if it has used up its slice. idle
1713 * queue always expire after 1 dispatch round.
1714 */
1715 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
1716 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
1717 cfq_class_idle(cfqq))) {
1718 cfqq->slice_end = jiffies + 1;
1719 cfq_slice_expired(cfqd, 0);
1720 }
1721
Shan Weib217a902009-09-01 10:06:42 +02001722 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02001723 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*
Jens Axboe5e705372006-07-13 12:39:25 +02001727 * task holds one reference to the queue, dropped when task exits. each rq
1728 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 *
1730 * queue lock must be held here.
1731 */
1732static void cfq_put_queue(struct cfq_queue *cfqq)
1733{
Jens Axboe22e2c502005-06-27 10:55:12 +02001734 struct cfq_data *cfqd = cfqq->cfqd;
1735
1736 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (!atomic_dec_and_test(&cfqq->ref))
1739 return;
1740
Jens Axboe7b679132008-05-30 12:23:07 +02001741 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02001743 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Jens Axboe3b181522005-06-27 10:56:24 +02001744 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001746 if (unlikely(cfqd->active_queue == cfqq)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001747 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02001748 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001749 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 kmem_cache_free(cfq_pool, cfqq);
1752}
1753
Jens Axboed6de8be2008-05-28 14:46:59 +02001754/*
1755 * Must always be called with the rcu_read_lock() held
1756 */
Jens Axboe07416d22008-05-07 09:17:12 +02001757static void
1758__call_for_each_cic(struct io_context *ioc,
1759 void (*func)(struct io_context *, struct cfq_io_context *))
1760{
1761 struct cfq_io_context *cic;
1762 struct hlist_node *n;
1763
1764 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
1765 func(ioc, cic);
1766}
1767
Jens Axboe4ac845a2008-01-24 08:44:49 +01001768/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001769 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01001770 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001771static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01001772call_for_each_cic(struct io_context *ioc,
1773 void (*func)(struct io_context *, struct cfq_io_context *))
1774{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001775 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02001776 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001777 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001778}
Jens Axboe4ac845a2008-01-24 08:44:49 +01001779
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001780static void cfq_cic_free_rcu(struct rcu_head *head)
1781{
1782 struct cfq_io_context *cic;
1783
1784 cic = container_of(head, struct cfq_io_context, rcu_head);
1785
1786 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09001787 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001788
Jens Axboe9a11b4e2008-05-29 09:32:08 +02001789 if (ioc_gone) {
1790 /*
1791 * CFQ scheduler is exiting, grab exit lock and check
1792 * the pending io context count. If it hits zero,
1793 * complete ioc_gone and set it back to NULL
1794 */
1795 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09001796 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02001797 complete(ioc_gone);
1798 ioc_gone = NULL;
1799 }
1800 spin_unlock(&ioc_gone_lock);
1801 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001802}
1803
1804static void cfq_cic_free(struct cfq_io_context *cic)
1805{
1806 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001807}
1808
1809static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
1810{
1811 unsigned long flags;
1812
1813 BUG_ON(!cic->dead_key);
1814
1815 spin_lock_irqsave(&ioc->lock, flags);
1816 radix_tree_delete(&ioc->radix_root, cic->dead_key);
Jens Axboeffc4e752008-02-19 10:02:29 +01001817 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001818 spin_unlock_irqrestore(&ioc->lock, flags);
1819
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001820 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001821}
1822
Jens Axboed6de8be2008-05-28 14:46:59 +02001823/*
1824 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
1825 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
1826 * and ->trim() which is called with the task lock held
1827 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001828static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001830 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02001831 * ioc->refcount is zero here, or we are called from elv_unregister(),
1832 * so no more cic's are allowed to be linked into this ioc. So it
1833 * should be ok to iterate over the known list, we will see all cic's
1834 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01001835 */
Jens Axboe07416d22008-05-07 09:17:12 +02001836 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Jens Axboe89850f72006-07-22 16:48:31 +02001839static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1840{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001841 struct cfq_queue *__cfqq, *next;
1842
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001843 if (unlikely(cfqq == cfqd->active_queue)) {
Jens Axboe6084cdd2007-04-23 08:25:00 +02001844 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02001845 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11001846 }
Jens Axboe89850f72006-07-22 16:48:31 +02001847
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001848 /*
1849 * If this queue was scheduled to merge with another queue, be
1850 * sure to drop the reference taken on that queue (and others in
1851 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
1852 */
1853 __cfqq = cfqq->new_cfqq;
1854 while (__cfqq) {
1855 if (__cfqq == cfqq) {
1856 WARN(1, "cfqq->new_cfqq loop detected\n");
1857 break;
1858 }
1859 next = __cfqq->new_cfqq;
1860 cfq_put_queue(__cfqq);
1861 __cfqq = next;
1862 }
1863
Jens Axboe89850f72006-07-22 16:48:31 +02001864 cfq_put_queue(cfqq);
1865}
1866
1867static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
1868 struct cfq_io_context *cic)
1869{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02001870 struct io_context *ioc = cic->ioc;
1871
Jens Axboefc463792006-08-29 09:05:44 +02001872 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01001873
1874 /*
1875 * Make sure key == NULL is seen for dead queues
1876 */
Jens Axboefc463792006-08-29 09:05:44 +02001877 smp_wmb();
Jens Axboe4ac845a2008-01-24 08:44:49 +01001878 cic->dead_key = (unsigned long) cic->key;
Jens Axboefc463792006-08-29 09:05:44 +02001879 cic->key = NULL;
1880
Fabio Checconi4faa3c82008-04-10 08:28:01 +02001881 if (ioc->ioc_data == cic)
1882 rcu_assign_pointer(ioc->ioc_data, NULL);
1883
Jens Axboeff6657c2009-04-08 10:58:57 +02001884 if (cic->cfqq[BLK_RW_ASYNC]) {
1885 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
1886 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02001887 }
1888
Jens Axboeff6657c2009-04-08 10:58:57 +02001889 if (cic->cfqq[BLK_RW_SYNC]) {
1890 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
1891 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02001892 }
Jens Axboe89850f72006-07-22 16:48:31 +02001893}
1894
Jens Axboe4ac845a2008-01-24 08:44:49 +01001895static void cfq_exit_single_io_context(struct io_context *ioc,
1896 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001897{
Al Viro478a82b2006-03-18 13:25:24 -05001898 struct cfq_data *cfqd = cic->key;
Jens Axboe22e2c502005-06-27 10:55:12 +02001899
Jens Axboe89850f72006-07-22 16:48:31 +02001900 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02001901 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01001902 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02001903
Jens Axboe4ac845a2008-01-24 08:44:49 +01001904 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01001905
1906 /*
1907 * Ensure we get a fresh copy of the ->key to prevent
1908 * race between exiting task and queue
1909 */
1910 smp_read_barrier_depends();
1911 if (cic->key)
1912 __cfq_exit_single_io_context(cfqd, cic);
1913
Jens Axboe4ac845a2008-01-24 08:44:49 +01001914 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05001915 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001916}
1917
Jens Axboe498d3aa22007-04-26 12:54:48 +02001918/*
1919 * The process that ioc belongs to has exited, we need to clean up
1920 * and put the internal structures we have that belongs to that process.
1921 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02001922static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Jens Axboe4ac845a2008-01-24 08:44:49 +01001924 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Jens Axboe22e2c502005-06-27 10:55:12 +02001927static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04001928cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Jens Axboeb5deef92006-07-19 23:39:40 +02001930 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Christoph Lameter94f60302007-07-17 04:03:29 -07001932 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
1933 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001935 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02001936 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01001937 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02001938 cic->dtor = cfq_free_io_context;
1939 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09001940 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 return cic;
1944}
1945
Jens Axboefd0928d2008-01-24 08:52:45 +01001946static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02001947{
1948 struct task_struct *tsk = current;
1949 int ioprio_class;
1950
Jens Axboe3b181522005-06-27 10:56:24 +02001951 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02001952 return;
1953
Jens Axboefd0928d2008-01-24 08:52:45 +01001954 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001955 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01001956 default:
1957 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
1958 case IOPRIO_CLASS_NONE:
1959 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02001960 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01001961 */
1962 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02001963 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01001964 break;
1965 case IOPRIO_CLASS_RT:
1966 cfqq->ioprio = task_ioprio(ioc);
1967 cfqq->ioprio_class = IOPRIO_CLASS_RT;
1968 break;
1969 case IOPRIO_CLASS_BE:
1970 cfqq->ioprio = task_ioprio(ioc);
1971 cfqq->ioprio_class = IOPRIO_CLASS_BE;
1972 break;
1973 case IOPRIO_CLASS_IDLE:
1974 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
1975 cfqq->ioprio = 7;
1976 cfq_clear_cfqq_idle_window(cfqq);
1977 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02001978 }
1979
1980 /*
1981 * keep track of original prio settings in case we have to temporarily
1982 * elevate the priority of this queue
1983 */
1984 cfqq->org_ioprio = cfqq->ioprio;
1985 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02001986 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001987}
1988
Jens Axboefebffd62008-01-28 13:19:43 +01001989static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02001990{
Al Viro478a82b2006-03-18 13:25:24 -05001991 struct cfq_data *cfqd = cic->key;
1992 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01001993 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02001994
Jens Axboecaaa5f92006-06-16 11:23:00 +02001995 if (unlikely(!cfqd))
1996 return;
1997
Jens Axboec1b707d2006-10-30 19:54:23 +01001998 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02001999
Jens Axboeff6657c2009-04-08 10:58:57 +02002000 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002001 if (cfqq) {
2002 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02002003 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2004 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002005 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002006 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002007 cfq_put_queue(cfqq);
2008 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002009 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002010
Jens Axboeff6657c2009-04-08 10:58:57 +02002011 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002012 if (cfqq)
2013 cfq_mark_cfqq_prio_changed(cfqq);
2014
Jens Axboec1b707d2006-10-30 19:54:23 +01002015 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02002016}
2017
Jens Axboefc463792006-08-29 09:05:44 +02002018static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002020 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02002021 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Jens Axboed5036d72009-06-26 10:44:34 +02002024static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002025 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002026{
2027 RB_CLEAR_NODE(&cfqq->rb_node);
2028 RB_CLEAR_NODE(&cfqq->p_node);
2029 INIT_LIST_HEAD(&cfqq->fifo);
2030
2031 atomic_set(&cfqq->ref, 0);
2032 cfqq->cfqd = cfqd;
2033
2034 cfq_mark_cfqq_prio_changed(cfqq);
2035
2036 if (is_sync) {
2037 if (!cfq_class_idle(cfqq))
2038 cfq_mark_cfqq_idle_window(cfqq);
2039 cfq_mark_cfqq_sync(cfqq);
2040 }
2041 cfqq->pid = pid;
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002045cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002046 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002049 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051retry:
Jens Axboe4ac845a2008-01-24 08:44:49 +01002052 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002053 /* cic always exists here */
2054 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jens Axboe6118b702009-06-30 09:34:12 +02002056 /*
2057 * Always try a new alloc if we fell back to the OOM cfqq
2058 * originally, since it should just be a temporary situation.
2059 */
2060 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2061 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (new_cfqq) {
2063 cfqq = new_cfqq;
2064 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002065 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002067 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002068 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002069 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002071 if (new_cfqq)
2072 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002073 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002074 cfqq = kmem_cache_alloc_node(cfq_pool,
2075 gfp_mask | __GFP_ZERO,
2076 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Jens Axboe6118b702009-06-30 09:34:12 +02002079 if (cfqq) {
2080 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2081 cfq_init_prio_data(cfqq, ioc);
2082 cfq_log_cfqq(cfqd, cfqq, "alloced");
2083 } else
2084 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086
2087 if (new_cfqq)
2088 kmem_cache_free(cfq_pool, new_cfqq);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return cfqq;
2091}
2092
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002093static struct cfq_queue **
2094cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2095{
Jens Axboefe094d92008-01-31 13:08:54 +01002096 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002097 case IOPRIO_CLASS_RT:
2098 return &cfqd->async_cfqq[0][ioprio];
2099 case IOPRIO_CLASS_BE:
2100 return &cfqd->async_cfqq[1][ioprio];
2101 case IOPRIO_CLASS_IDLE:
2102 return &cfqd->async_idle_cfqq;
2103 default:
2104 BUG();
2105 }
2106}
2107
Jens Axboe15c31be2007-07-10 13:43:25 +02002108static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002109cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002110 gfp_t gfp_mask)
2111{
Jens Axboefd0928d2008-01-24 08:52:45 +01002112 const int ioprio = task_ioprio(ioc);
2113 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002114 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002115 struct cfq_queue *cfqq = NULL;
2116
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002117 if (!is_sync) {
2118 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2119 cfqq = *async_cfqq;
2120 }
2121
Jens Axboe6118b702009-06-30 09:34:12 +02002122 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002123 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002124
2125 /*
2126 * pin the queue now that it's allocated, scheduler exit will prune it
2127 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002128 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02002129 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002130 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002131 }
2132
2133 atomic_inc(&cfqq->ref);
2134 return cfqq;
2135}
2136
Jens Axboe498d3aa22007-04-26 12:54:48 +02002137/*
2138 * We drop cfq io contexts lazily, so we may find a dead one.
2139 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002140static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002141cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2142 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002143{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002144 unsigned long flags;
2145
Jens Axboefc463792006-08-29 09:05:44 +02002146 WARN_ON(!list_empty(&cic->queue_list));
Jens Axboe597bc482007-04-24 21:23:53 +02002147
Jens Axboe4ac845a2008-01-24 08:44:49 +01002148 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02002149
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002150 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002151
2152 radix_tree_delete(&ioc->radix_root, (unsigned long) cfqd);
Jens Axboeffc4e752008-02-19 10:02:29 +01002153 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002154 spin_unlock_irqrestore(&ioc->lock, flags);
2155
2156 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002157}
2158
Jens Axboee2d74ac2006-03-28 08:59:01 +02002159static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01002160cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002161{
Jens Axboee2d74ac2006-03-28 08:59:01 +02002162 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002163 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002164 void *k;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002165
Vasily Tarasov91fac312007-04-25 12:29:51 +02002166 if (unlikely(!ioc))
2167 return NULL;
2168
Jens Axboed6de8be2008-05-28 14:46:59 +02002169 rcu_read_lock();
2170
Jens Axboe597bc482007-04-24 21:23:53 +02002171 /*
2172 * we maintain a last-hit cache, to avoid browsing over the tree
2173 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002174 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02002175 if (cic && cic->key == cfqd) {
2176 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02002177 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002178 }
Jens Axboe597bc482007-04-24 21:23:53 +02002179
Jens Axboe4ac845a2008-01-24 08:44:49 +01002180 do {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002181 cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
2182 rcu_read_unlock();
2183 if (!cic)
2184 break;
OGAWA Hirofumibe3b0752006-04-18 19:18:31 +02002185 /* ->key must be copied to avoid race with cfq_exit_queue() */
2186 k = cic->key;
2187 if (unlikely(!k)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002188 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002189 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002190 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002191 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002192
Jens Axboed6de8be2008-05-28 14:46:59 +02002193 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002194 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002195 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002196 break;
2197 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002198
Jens Axboe4ac845a2008-01-24 08:44:49 +01002199 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002200}
2201
Jens Axboe4ac845a2008-01-24 08:44:49 +01002202/*
2203 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2204 * the process specific cfq io context when entered from the block layer.
2205 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2206 */
Jens Axboefebffd62008-01-28 13:19:43 +01002207static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2208 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002209{
Jens Axboe0261d682006-10-30 19:07:48 +01002210 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002211 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002212
Jens Axboe4ac845a2008-01-24 08:44:49 +01002213 ret = radix_tree_preload(gfp_mask);
2214 if (!ret) {
2215 cic->ioc = ioc;
2216 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002217
Jens Axboe4ac845a2008-01-24 08:44:49 +01002218 spin_lock_irqsave(&ioc->lock, flags);
2219 ret = radix_tree_insert(&ioc->radix_root,
2220 (unsigned long) cfqd, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01002221 if (!ret)
2222 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002223 spin_unlock_irqrestore(&ioc->lock, flags);
2224
2225 radix_tree_preload_end();
2226
2227 if (!ret) {
2228 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2229 list_add(&cic->queue_list, &cfqd->cic_list);
2230 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002231 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002232 }
2233
Jens Axboe4ac845a2008-01-24 08:44:49 +01002234 if (ret)
2235 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02002236
Jens Axboe4ac845a2008-01-24 08:44:49 +01002237 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002238}
2239
Jens Axboe22e2c502005-06-27 10:55:12 +02002240/*
2241 * Setup general io context and cfq io context. There can be several cfq
2242 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02002243 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02002244 */
2245static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02002246cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Jens Axboe22e2c502005-06-27 10:55:12 +02002248 struct io_context *ioc = NULL;
2249 struct cfq_io_context *cic;
2250
2251 might_sleep_if(gfp_mask & __GFP_WAIT);
2252
Jens Axboeb5deef92006-07-19 23:39:40 +02002253 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02002254 if (!ioc)
2255 return NULL;
2256
Jens Axboe4ac845a2008-01-24 08:44:49 +01002257 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002258 if (cic)
2259 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02002260
Jens Axboee2d74ac2006-03-28 08:59:01 +02002261 cic = cfq_alloc_io_context(cfqd, gfp_mask);
2262 if (cic == NULL)
2263 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02002264
Jens Axboe4ac845a2008-01-24 08:44:49 +01002265 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
2266 goto err_free;
2267
Jens Axboe22e2c502005-06-27 10:55:12 +02002268out:
Jens Axboefc463792006-08-29 09:05:44 +02002269 smp_read_barrier_depends();
2270 if (unlikely(ioc->ioprio_changed))
2271 cfq_ioc_set_ioprio(ioc);
2272
Jens Axboe22e2c502005-06-27 10:55:12 +02002273 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002274err_free:
2275 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002276err:
2277 put_io_context(ioc);
2278 return NULL;
2279}
2280
2281static void
2282cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
2283{
Jens Axboeaaf12282007-01-19 11:30:16 +11002284 unsigned long elapsed = jiffies - cic->last_end_request;
2285 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02002286
2287 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
2288 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
2289 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
2290}
2291
Jens Axboe206dc692006-03-28 13:03:44 +02002292static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002293cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02002294 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02002295{
2296 sector_t sdist;
2297 u64 total;
2298
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002299 if (!cfqq->last_request_pos)
Jeff Moyer4d00aa42009-04-21 07:25:04 +02002300 sdist = 0;
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002301 else if (cfqq->last_request_pos < blk_rq_pos(rq))
2302 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
Jens Axboe206dc692006-03-28 13:03:44 +02002303 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002304 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
Jens Axboe206dc692006-03-28 13:03:44 +02002305
2306 /*
2307 * Don't allow the seek distance to get too large from the
2308 * odd fragment, pagein, etc
2309 */
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002310 if (cfqq->seek_samples <= 60) /* second&third seek */
2311 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*1024);
Jens Axboe206dc692006-03-28 13:03:44 +02002312 else
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002313 sdist = min(sdist, (cfqq->seek_mean * 4) + 2*1024*64);
Jens Axboe206dc692006-03-28 13:03:44 +02002314
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002315 cfqq->seek_samples = (7*cfqq->seek_samples + 256) / 8;
2316 cfqq->seek_total = (7*cfqq->seek_total + (u64)256*sdist) / 8;
2317 total = cfqq->seek_total + (cfqq->seek_samples/2);
2318 do_div(total, cfqq->seek_samples);
2319 cfqq->seek_mean = (sector_t)total;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002320
2321 /*
2322 * If this cfqq is shared between multiple processes, check to
2323 * make sure that those processes are still issuing I/Os within
2324 * the mean seek distance. If not, it may be time to break the
2325 * queues apart again.
2326 */
2327 if (cfq_cfqq_coop(cfqq)) {
2328 if (CFQQ_SEEKY(cfqq) && !cfqq->seeky_start)
2329 cfqq->seeky_start = jiffies;
2330 else if (!CFQQ_SEEKY(cfqq))
2331 cfqq->seeky_start = 0;
2332 }
Jens Axboe206dc692006-03-28 13:03:44 +02002333}
Jens Axboe22e2c502005-06-27 10:55:12 +02002334
2335/*
2336 * Disable idle window if the process thinks too long or seeks so much that
2337 * it doesn't matter
2338 */
2339static void
2340cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2341 struct cfq_io_context *cic)
2342{
Jens Axboe7b679132008-05-30 12:23:07 +02002343 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02002344
Jens Axboe08717142008-01-28 11:38:15 +01002345 /*
2346 * Don't idle for async or idle io prio class
2347 */
2348 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02002349 return;
2350
Jens Axboec265a7f2008-06-26 13:49:33 +02002351 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002352
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01002353 if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002354 (sample_valid(cfqq->seek_samples) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02002355 enable_idle = 0;
2356 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002357 if (cic->ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02002358 enable_idle = 0;
2359 else
2360 enable_idle = 1;
2361 }
2362
Jens Axboe7b679132008-05-30 12:23:07 +02002363 if (old_idle != enable_idle) {
2364 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
2365 if (enable_idle)
2366 cfq_mark_cfqq_idle_window(cfqq);
2367 else
2368 cfq_clear_cfqq_idle_window(cfqq);
2369 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002370}
2371
Jens Axboe22e2c502005-06-27 10:55:12 +02002372/*
2373 * Check if new_cfqq should preempt the currently active queue. Return 0 for
2374 * no or if we aren't sure, a 1 will cause a preempt.
2375 */
Jens Axboea6151c32009-10-07 20:02:57 +02002376static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02002377cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02002378 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002379{
Jens Axboe6d048f52007-04-25 12:44:27 +02002380 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002381
Jens Axboe6d048f52007-04-25 12:44:27 +02002382 cfqq = cfqd->active_queue;
2383 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02002384 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002385
Jens Axboe6d048f52007-04-25 12:44:27 +02002386 if (cfq_slice_used(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002387 return true;
Jens Axboe6d048f52007-04-25 12:44:27 +02002388
2389 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002390 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002391
2392 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002393 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002394
Corrado Zoccoloe4a22912009-11-26 10:02:58 +01002395 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
2396 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
2397 new_cfqq->service_tree->count == 1)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002398 return true;
2399
Jens Axboe22e2c502005-06-27 10:55:12 +02002400 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02002401 * if the new request is sync, but the currently running queue is
2402 * not, let the sync request have priority.
2403 */
Jens Axboe5e705372006-07-13 12:39:25 +02002404 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002405 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002406
Jens Axboe374f84a2006-07-23 01:42:19 +02002407 /*
2408 * So both queues are sync. Let the new request get disk time if
2409 * it's a metadata request and the current queue is doing regular IO.
2410 */
2411 if (rq_is_meta(rq) && !cfqq->meta_pending)
Jens Axboee6ec4fe2009-11-03 20:21:35 +01002412 return true;
Jens Axboe22e2c502005-06-27 10:55:12 +02002413
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002414 /*
2415 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
2416 */
2417 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002418 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002419
Jens Axboe1e3335d2007-02-14 19:59:49 +01002420 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02002421 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002422
2423 /*
2424 * if this request is as-good as one we would expect from the
2425 * current cfqq, let it preempt
2426 */
Jens Axboee00ef792009-11-04 08:54:55 +01002427 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002428 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01002429
Jens Axboea6151c32009-10-07 20:02:57 +02002430 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02002431}
2432
2433/*
2434 * cfqq preempts the active queue. if we allowed preempt with no slice left,
2435 * let it have half of its nominal slice.
2436 */
2437static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2438{
Jens Axboe7b679132008-05-30 12:23:07 +02002439 cfq_log_cfqq(cfqd, cfqq, "preempt");
Jens Axboe6084cdd2007-04-23 08:25:00 +02002440 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02002441
Jens Axboebf572252006-07-19 20:29:12 +02002442 /*
2443 * Put the new queue at the front of the of the current list,
2444 * so we know that it will be selected next.
2445 */
2446 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02002447
2448 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02002449
Jens Axboe44f7c162007-01-19 11:51:58 +11002450 cfqq->slice_end = 0;
2451 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002452}
2453
2454/*
Jens Axboe5e705372006-07-13 12:39:25 +02002455 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02002456 * something we should do about it
2457 */
2458static void
Jens Axboe5e705372006-07-13 12:39:25 +02002459cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
2460 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002461{
Jens Axboe5e705372006-07-13 12:39:25 +02002462 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02002463
Aaron Carroll45333d52008-08-26 15:52:36 +02002464 cfqd->rq_queued++;
Jens Axboe374f84a2006-07-23 01:42:19 +02002465 if (rq_is_meta(rq))
2466 cfqq->meta_pending++;
2467
Jens Axboe9c2c38a2005-08-24 14:57:54 +02002468 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002469 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02002470 cfq_update_idle_window(cfqd, cfqq, cic);
2471
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002472 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002473
2474 if (cfqq == cfqd->active_queue) {
2475 /*
Jens Axboeb0291952009-04-07 11:38:31 +02002476 * Remember that we saw a request from this process, but
2477 * don't start queuing just yet. Otherwise we risk seeing lots
2478 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02002479 * and merging. If the request is already larger than a single
2480 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02002481 * merging is already done. Ditto for a busy system that
2482 * has other work pending, don't risk delaying until the
2483 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02002484 */
Jens Axboed6ceb252009-04-14 14:18:16 +02002485 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02002486 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
2487 cfqd->busy_queues > 1) {
Jens Axboed6ceb252009-04-14 14:18:16 +02002488 del_timer(&cfqd->idle_slice_timer);
Tejun Heoa7f55792009-04-23 11:05:17 +09002489 __blk_run_queue(cfqd->queue);
Jens Axboed6ceb252009-04-14 14:18:16 +02002490 }
Jens Axboeb0291952009-04-07 11:38:31 +02002491 cfq_mark_cfqq_must_dispatch(cfqq);
Jens Axboed6ceb252009-04-14 14:18:16 +02002492 }
Jens Axboe5e705372006-07-13 12:39:25 +02002493 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002494 /*
2495 * not the active queue - expire current slice if it is
2496 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01002497 * has some old slice time left and is of higher priority or
2498 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02002499 */
2500 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09002501 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002502 }
2503}
2504
Jens Axboe165125e2007-07-24 09:28:11 +02002505static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002506{
Jens Axboeb4878f22005-10-20 16:42:29 +02002507 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002508 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002509
Jens Axboe7b679132008-05-30 12:23:07 +02002510 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01002511 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Jens Axboe30996f42009-10-05 11:03:39 +02002513 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02002514 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002515 cfq_add_rq_rb(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002516
Jens Axboe5e705372006-07-13 12:39:25 +02002517 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Aaron Carroll45333d52008-08-26 15:52:36 +02002520/*
2521 * Update hw_tag based on peak queue depth over 50 samples under
2522 * sufficient load.
2523 */
2524static void cfq_update_hw_tag(struct cfq_data *cfqd)
2525{
Shaohua Li1a1238a2009-10-27 08:46:23 +01002526 struct cfq_queue *cfqq = cfqd->active_queue;
2527
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01002528 if (rq_in_driver(cfqd) > cfqd->hw_tag_est_depth)
2529 cfqd->hw_tag_est_depth = rq_in_driver(cfqd);
2530
2531 if (cfqd->hw_tag == 1)
2532 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02002533
2534 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Jens Axboe5ad531d2009-07-03 12:57:48 +02002535 rq_in_driver(cfqd) <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02002536 return;
2537
Shaohua Li1a1238a2009-10-27 08:46:23 +01002538 /*
2539 * If active queue hasn't enough requests and can idle, cfq might not
2540 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
2541 * case
2542 */
2543 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
2544 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
2545 CFQ_HW_QUEUE_MIN && rq_in_driver(cfqd) < CFQ_HW_QUEUE_MIN)
2546 return;
2547
Aaron Carroll45333d52008-08-26 15:52:36 +02002548 if (cfqd->hw_tag_samples++ < 50)
2549 return;
2550
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01002551 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02002552 cfqd->hw_tag = 1;
2553 else
2554 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02002555}
2556
Jens Axboe165125e2007-07-24 09:28:11 +02002557static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Jens Axboe5e705372006-07-13 12:39:25 +02002559 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002560 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02002561 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002562 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Jens Axboeb4878f22005-10-20 16:42:29 +02002564 now = jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002565 cfq_log_cfqq(cfqd, cfqq, "complete");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Aaron Carroll45333d52008-08-26 15:52:36 +02002567 cfq_update_hw_tag(cfqd);
2568
Jens Axboe5ad531d2009-07-03 12:57:48 +02002569 WARN_ON(!cfqd->rq_in_driver[sync]);
Jens Axboe6d048f52007-04-25 12:44:27 +02002570 WARN_ON(!cfqq->dispatched);
Jens Axboe5ad531d2009-07-03 12:57:48 +02002571 cfqd->rq_in_driver[sync]--;
Jens Axboe6d048f52007-04-25 12:44:27 +02002572 cfqq->dispatched--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Jens Axboe3ed9a292007-04-23 08:33:33 +02002574 if (cfq_cfqq_sync(cfqq))
2575 cfqd->sync_flight--;
2576
Vivek Goyal365722b2009-10-03 15:21:27 +02002577 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02002578 RQ_CIC(rq)->last_end_request = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02002579 cfqd->last_end_sync_rq = now;
2580 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002581
2582 /*
2583 * If this is the active queue, check if it needs to be expired,
2584 * or if we want to idle in case it has no pending requests.
2585 */
2586 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02002587 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
2588
Jens Axboe44f7c162007-01-19 11:51:58 +11002589 if (cfq_cfqq_slice_new(cfqq)) {
2590 cfq_set_prio_slice(cfqd, cfqq);
2591 cfq_clear_cfqq_slice_new(cfqq);
2592 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002593 /*
2594 * If there are no requests waiting in this queue, and
2595 * there are other queues ready to issue requests, AND
2596 * those other queues are issuing requests within our
2597 * mean seek distance, give them a chance to run instead
2598 * of idling.
2599 */
Jens Axboe08717142008-01-28 11:38:15 +01002600 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Jens Axboe6084cdd2007-04-23 08:25:00 +02002601 cfq_slice_expired(cfqd, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002602 else if (cfqq_empty && !cfq_close_cooperator(cfqd, cfqq) &&
Jens Axboea36e71f2009-04-15 12:15:11 +02002603 sync && !rq_noidle(rq))
Jens Axboe6d048f52007-04-25 12:44:27 +02002604 cfq_arm_slice_timer(cfqd);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002605 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002606
Jens Axboe5ad531d2009-07-03 12:57:48 +02002607 if (!rq_in_driver(cfqd))
Jens Axboe23e018a2009-10-05 08:52:35 +02002608 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Jens Axboe22e2c502005-06-27 10:55:12 +02002611/*
2612 * we temporarily boost lower priority queues if they are holding fs exclusive
2613 * resources. they are boosted to normal prio (CLASS_BE/4)
2614 */
2615static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
Jens Axboe22e2c502005-06-27 10:55:12 +02002617 if (has_fs_excl()) {
2618 /*
2619 * boost idle prio on transactions that would lock out other
2620 * users of the filesystem
2621 */
2622 if (cfq_class_idle(cfqq))
2623 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2624 if (cfqq->ioprio > IOPRIO_NORM)
2625 cfqq->ioprio = IOPRIO_NORM;
2626 } else {
2627 /*
Corrado Zoccolodddb7452009-11-02 10:40:37 +01002628 * unboost the queue (if needed)
Jens Axboe22e2c502005-06-27 10:55:12 +02002629 */
Corrado Zoccolodddb7452009-11-02 10:40:37 +01002630 cfqq->ioprio_class = cfqq->org_ioprio_class;
2631 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002633}
2634
Jens Axboe89850f72006-07-22 16:48:31 +02002635static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002636{
Jens Axboe1b379d82009-08-11 08:26:11 +02002637 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02002638 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002639 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02002640 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002641
2642 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02002643}
2644
Jens Axboe165125e2007-07-24 09:28:11 +02002645static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02002646{
2647 struct cfq_data *cfqd = q->elevator->elevator_data;
2648 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002649 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02002650 struct cfq_queue *cfqq;
2651
2652 /*
2653 * don't force setup of a queue from here, as a call to may_queue
2654 * does not necessarily imply that a request actually will be queued.
2655 * so just lookup a possibly existing queue, or return 'may queue'
2656 * if that fails
2657 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002658 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002659 if (!cic)
2660 return ELV_MQUEUE_MAY;
2661
Jens Axboeb0b78f82009-04-08 10:56:08 +02002662 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02002663 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01002664 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02002665 cfq_prio_boost(cfqq);
2666
Jens Axboe89850f72006-07-22 16:48:31 +02002667 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002668 }
2669
2670 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673/*
2674 * queue lock held here
2675 */
Jens Axboebb37b942006-12-01 10:42:33 +01002676static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Jens Axboe5e705372006-07-13 12:39:25 +02002678 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Jens Axboe5e705372006-07-13 12:39:25 +02002680 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002681 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Jens Axboe22e2c502005-06-27 10:55:12 +02002683 BUG_ON(!cfqq->allocated[rw]);
2684 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Jens Axboe5e705372006-07-13 12:39:25 +02002686 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02002689 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 cfq_put_queue(cfqq);
2692 }
2693}
2694
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002695static struct cfq_queue *
2696cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
2697 struct cfq_queue *cfqq)
2698{
2699 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
2700 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002701 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002702 cfq_put_queue(cfqq);
2703 return cic_to_cfqq(cic, 1);
2704}
2705
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002706static int should_split_cfqq(struct cfq_queue *cfqq)
2707{
2708 if (cfqq->seeky_start &&
2709 time_after(jiffies, cfqq->seeky_start + CFQQ_COOP_TOUT))
2710 return 1;
2711 return 0;
2712}
2713
2714/*
2715 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
2716 * was the last process referring to said cfqq.
2717 */
2718static struct cfq_queue *
2719split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
2720{
2721 if (cfqq_process_refs(cfqq) == 1) {
2722 cfqq->seeky_start = 0;
2723 cfqq->pid = current->pid;
2724 cfq_clear_cfqq_coop(cfqq);
2725 return cfqq;
2726 }
2727
2728 cic_set_cfqq(cic, NULL, 1);
2729 cfq_put_queue(cfqq);
2730 return NULL;
2731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732/*
Jens Axboe22e2c502005-06-27 10:55:12 +02002733 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002735static int
Jens Axboe165125e2007-07-24 09:28:11 +02002736cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 struct cfq_data *cfqd = q->elevator->elevator_data;
2739 struct cfq_io_context *cic;
2740 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02002741 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002742 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 unsigned long flags;
2744
2745 might_sleep_if(gfp_mask & __GFP_WAIT);
2746
Jens Axboee2d74ac2006-03-28 08:59:01 +02002747 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 spin_lock_irqsave(q->queue_lock, flags);
2750
Jens Axboe22e2c502005-06-27 10:55:12 +02002751 if (!cic)
2752 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002754new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02002755 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02002756 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01002757 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002758 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002759 } else {
2760 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002761 * If the queue was seeky for too long, break it apart.
2762 */
2763 if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
2764 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
2765 cfqq = split_cfqq(cic, cfqq);
2766 if (!cfqq)
2767 goto new_queue;
2768 }
2769
2770 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002771 * Check to see if this queue is scheduled to merge with
2772 * another, closely cooperating queue. The merging of
2773 * queues happens here as it must be done in process context.
2774 * The reference on new_cfqq was taken in merge_cfqqs.
2775 */
2776 if (cfqq->new_cfqq)
2777 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02002781 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02002782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 spin_unlock_irqrestore(q->queue_lock, flags);
2784
Jens Axboe5e705372006-07-13 12:39:25 +02002785 rq->elevator_private = cic;
2786 rq->elevator_private2 = cfqq;
2787 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02002788
Jens Axboe22e2c502005-06-27 10:55:12 +02002789queue_fail:
2790 if (cic)
2791 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02002792
Jens Axboe23e018a2009-10-05 08:52:35 +02002793 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02002795 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return 1;
2797}
2798
David Howells65f27f32006-11-22 14:55:48 +00002799static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02002800{
David Howells65f27f32006-11-22 14:55:48 +00002801 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02002802 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02002803 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002804
Jens Axboe40bb54d2009-04-15 12:11:10 +02002805 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09002806 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02002807 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02002808}
2809
2810/*
2811 * Timer running if the active_queue is currently idling inside its time slice
2812 */
2813static void cfq_idle_slice_timer(unsigned long data)
2814{
2815 struct cfq_data *cfqd = (struct cfq_data *) data;
2816 struct cfq_queue *cfqq;
2817 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002818 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02002819
Jens Axboe7b679132008-05-30 12:23:07 +02002820 cfq_log(cfqd, "idle timer fired");
2821
Jens Axboe22e2c502005-06-27 10:55:12 +02002822 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
2823
Jens Axboefe094d92008-01-31 13:08:54 +01002824 cfqq = cfqd->active_queue;
2825 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11002826 timed_out = 0;
2827
Jens Axboe22e2c502005-06-27 10:55:12 +02002828 /*
Jens Axboeb0291952009-04-07 11:38:31 +02002829 * We saw a request before the queue expired, let it through
2830 */
2831 if (cfq_cfqq_must_dispatch(cfqq))
2832 goto out_kick;
2833
2834 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002835 * expired
2836 */
Jens Axboe44f7c162007-01-19 11:51:58 +11002837 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002838 goto expire;
2839
2840 /*
2841 * only expire and reinvoke request handler, if there are
2842 * other queues with pending requests
2843 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02002844 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02002845 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02002846
2847 /*
2848 * not expired and it has a request pending, let it dispatch
2849 */
Jens Axboe75e50982009-04-07 08:56:14 +02002850 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002851 goto out_kick;
Jens Axboe22e2c502005-06-27 10:55:12 +02002852 }
2853expire:
Jens Axboe6084cdd2007-04-23 08:25:00 +02002854 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02002855out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02002856 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02002857out_cont:
2858 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
2859}
2860
Jens Axboe3b181522005-06-27 10:56:24 +02002861static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
2862{
2863 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02002864 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02002865}
Jens Axboe22e2c502005-06-27 10:55:12 +02002866
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002867static void cfq_put_async_queues(struct cfq_data *cfqd)
2868{
2869 int i;
2870
2871 for (i = 0; i < IOPRIO_BE_NR; i++) {
2872 if (cfqd->async_cfqq[0][i])
2873 cfq_put_queue(cfqd->async_cfqq[0][i]);
2874 if (cfqd->async_cfqq[1][i])
2875 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002876 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01002877
2878 if (cfqd->async_idle_cfqq)
2879 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002880}
2881
Jens Axboeb374d182008-10-31 10:05:07 +01002882static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Jens Axboe22e2c502005-06-27 10:55:12 +02002884 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02002885 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002886
Jens Axboe3b181522005-06-27 10:56:24 +02002887 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002888
Al Virod9ff4182006-03-18 13:51:22 -05002889 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002890
Al Virod9ff4182006-03-18 13:51:22 -05002891 if (cfqd->active_queue)
Jens Axboe6084cdd2007-04-23 08:25:00 +02002892 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002893
2894 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05002895 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
2896 struct cfq_io_context,
2897 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02002898
2899 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05002900 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002901
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002902 cfq_put_async_queues(cfqd);
Jens Axboe15c31be2007-07-10 13:43:25 +02002903
Al Virod9ff4182006-03-18 13:51:22 -05002904 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05002905
2906 cfq_shutdown_timer_wq(cfqd);
2907
Al Viroa90d7422006-03-18 12:05:37 -05002908 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
Jens Axboe165125e2007-07-24 09:28:11 +02002911static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct cfq_data *cfqd;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002914 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Christoph Lameter94f60302007-07-17 04:03:29 -07002916 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02002918 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002920 for (i = 0; i < 2; ++i)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002921 for (j = 0; j < 3; ++j)
2922 cfqd->service_trees[i][j] = CFQ_RB_ROOT;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002923 cfqd->service_tree_idle = CFQ_RB_ROOT;
Jens Axboe26a2ac02009-04-23 12:13:27 +02002924
2925 /*
2926 * Not strictly needed (since RB_ROOT just clears the node and we
2927 * zeroed cfqd on alloc), but better be safe in case someone decides
2928 * to add magic to the rb code
2929 */
2930 for (i = 0; i < CFQ_PRIO_LISTS; i++)
2931 cfqd->prio_trees[i] = RB_ROOT;
2932
Jens Axboe6118b702009-06-30 09:34:12 +02002933 /*
2934 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
2935 * Grab a permanent reference to it, so that the normal code flow
2936 * will not attempt to free it.
2937 */
2938 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
2939 atomic_inc(&cfqd->oom_cfqq.ref);
2940
Al Virod9ff4182006-03-18 13:51:22 -05002941 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Jens Axboe22e2c502005-06-27 10:55:12 +02002945 init_timer(&cfqd->idle_slice_timer);
2946 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
2947 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
2948
Jens Axboe23e018a2009-10-05 08:52:35 +02002949 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02002952 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
2953 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 cfqd->cfq_back_max = cfq_back_max;
2955 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02002956 cfqd->cfq_slice[0] = cfq_slice_async;
2957 cfqd->cfq_slice[1] = cfq_slice_sync;
2958 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
2959 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02002960 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01002961 cfqd->hw_tag = -1;
Vivek Goyal365722b2009-10-03 15:21:27 +02002962 cfqd->last_end_sync_rq = jiffies;
Jens Axboebc1c1162006-06-08 08:49:06 +02002963 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966static void cfq_slab_kill(void)
2967{
Jens Axboed6de8be2008-05-28 14:46:59 +02002968 /*
2969 * Caller already ensured that pending RCU callbacks are completed,
2970 * so we should have no busy allocations at this point.
2971 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (cfq_pool)
2973 kmem_cache_destroy(cfq_pool);
2974 if (cfq_ioc_pool)
2975 kmem_cache_destroy(cfq_ioc_pool);
2976}
2977
2978static int __init cfq_slab_setup(void)
2979{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002980 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (!cfq_pool)
2982 goto fail;
2983
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002984 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (!cfq_ioc_pool)
2986 goto fail;
2987
2988 return 0;
2989fail:
2990 cfq_slab_kill();
2991 return -ENOMEM;
2992}
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994/*
2995 * sysfs parts below -->
2996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997static ssize_t
2998cfq_var_show(unsigned int var, char *page)
2999{
3000 return sprintf(page, "%d\n", var);
3001}
3002
3003static ssize_t
3004cfq_var_store(unsigned int *var, const char *page, size_t count)
3005{
3006 char *p = (char *) page;
3007
3008 *var = simple_strtoul(p, &p, 10);
3009 return count;
3010}
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003013static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003015 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 unsigned int __data = __VAR; \
3017 if (__CONV) \
3018 __data = jiffies_to_msecs(__data); \
3019 return cfq_var_show(__data, (page)); \
3020}
3021SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003022SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3023SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003024SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3025SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003026SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3027SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3028SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3029SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003030SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#undef SHOW_FUNCTION
3032
3033#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003034static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003036 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 unsigned int __data; \
3038 int ret = cfq_var_store(&__data, (page), count); \
3039 if (__data < (MIN)) \
3040 __data = (MIN); \
3041 else if (__data > (MAX)) \
3042 __data = (MAX); \
3043 if (__CONV) \
3044 *(__PTR) = msecs_to_jiffies(__data); \
3045 else \
3046 *(__PTR) = __data; \
3047 return ret; \
3048}
3049STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003050STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3051 UINT_MAX, 1);
3052STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3053 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003054STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003055STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3056 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003057STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3058STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3059STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003060STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3061 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003062STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063#undef STORE_FUNCTION
3064
Al Viroe572ec72006-03-18 22:27:18 -05003065#define CFQ_ATTR(name) \
3066 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003067
Al Viroe572ec72006-03-18 22:27:18 -05003068static struct elv_fs_entry cfq_attrs[] = {
3069 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003070 CFQ_ATTR(fifo_expire_sync),
3071 CFQ_ATTR(fifo_expire_async),
3072 CFQ_ATTR(back_seek_max),
3073 CFQ_ATTR(back_seek_penalty),
3074 CFQ_ATTR(slice_sync),
3075 CFQ_ATTR(slice_async),
3076 CFQ_ATTR(slice_async_rq),
3077 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003078 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003079 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080};
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static struct elevator_type iosched_cfq = {
3083 .ops = {
3084 .elevator_merge_fn = cfq_merge,
3085 .elevator_merged_fn = cfq_merged_request,
3086 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003087 .elevator_allow_merge_fn = cfq_allow_merge,
Jens Axboeb4878f22005-10-20 16:42:29 +02003088 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003090 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 .elevator_deactivate_req_fn = cfq_deactivate_request,
3092 .elevator_queue_empty_fn = cfq_queue_empty,
3093 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003094 .elevator_former_req_fn = elv_rb_former_request,
3095 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 .elevator_set_req_fn = cfq_set_request,
3097 .elevator_put_req_fn = cfq_put_request,
3098 .elevator_may_queue_fn = cfq_may_queue,
3099 .elevator_init_fn = cfq_init_queue,
3100 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02003101 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 },
Al Viro3d1ab402006-03-18 18:35:43 -05003103 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 .elevator_name = "cfq",
3105 .elevator_owner = THIS_MODULE,
3106};
3107
3108static int __init cfq_init(void)
3109{
Jens Axboe22e2c502005-06-27 10:55:12 +02003110 /*
3111 * could be 0 on HZ < 1000 setups
3112 */
3113 if (!cfq_slice_async)
3114 cfq_slice_async = 1;
3115 if (!cfq_slice_idle)
3116 cfq_slice_idle = 1;
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (cfq_slab_setup())
3119 return -ENOMEM;
3120
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003121 elv_register(&iosched_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01003123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126static void __exit cfq_exit(void)
3127{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003128 DECLARE_COMPLETION_ONSTACK(all_gone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05003130 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02003131 /* ioc_gone's update must be visible before reading ioc_count */
3132 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02003133
3134 /*
3135 * this also protects us from entering cfq_slab_kill() with
3136 * pending RCU callbacks
3137 */
Tejun Heo245b2e72009-06-24 15:13:48 +09003138 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02003139 wait_for_completion(&all_gone);
Christoph Hellwig83521d32005-10-30 15:01:39 -08003140 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143module_init(cfq_init);
3144module_exit(cfq_exit);
3145
3146MODULE_AUTHOR("Jens Axboe");
3147MODULE_LICENSE("GPL");
3148MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");