blob: dfceb6386bd5e88478d56088c07b9c0babb995cc [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010013#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020016#include <linux/blktrace_api.h>
Vivek Goyale98ef892010-06-18 10:39:47 -040017#include "cfq.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/*
20 * tunables
21 */
Jens Axboefe094d92008-01-31 13:08:54 +010022/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010023static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010024static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010025/* maximum backwards seek, in KiB */
26static const int cfq_back_max = 16 * 1024;
27/* penalty of a backwards seek */
28static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010029static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020030static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010031static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020032static int cfq_slice_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010033static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
34static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020035
Jens Axboed9e76202007-04-20 14:27:50 +020036/*
Jens Axboe08717142008-01-28 11:38:15 +010037 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020038 */
Jens Axboe08717142008-01-28 11:38:15 +010039#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020040
41/*
42 * below this threshold, we consider thinktime immediate
43 */
44#define CFQ_MIN_TT (2)
45
Jens Axboe22e2c502005-06-27 10:55:12 +020046#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020047#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050048#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020049
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010050#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010051#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010052#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010053#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010054
Jens Axboefe094d92008-01-31 13:08:54 +010055#define RQ_CIC(rq) \
56 ((struct cfq_io_context *) (rq)->elevator_private)
Jens Axboe7b679132008-05-30 12:23:07 +020057#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elevator_private2)
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +020058#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elevator_private3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Christoph Lametere18b8902006-12-06 20:33:20 -080060static struct kmem_cache *cfq_pool;
61static struct kmem_cache *cfq_ioc_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Tejun Heo245b2e72009-06-24 15:13:48 +090063static DEFINE_PER_CPU(unsigned long, cfq_ioc_count);
Al Viro334e94d2006-03-18 15:05:53 -050064static struct completion *ioc_gone;
Jens Axboe9a11b4e2008-05-29 09:32:08 +020065static DEFINE_SPINLOCK(ioc_gone_lock);
Al Viro334e94d2006-03-18 15:05:53 -050066
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +040067static DEFINE_SPINLOCK(cic_index_lock);
68static DEFINE_IDA(cic_index_ida);
69
Jens Axboe22e2c502005-06-27 10:55:12 +020070#define CFQ_PRIO_LISTS IOPRIO_BE_NR
71#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020072#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
73
Jens Axboe206dc692006-03-28 13:03:44 +020074#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050075#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020076
Jens Axboe22e2c502005-06-27 10:55:12 +020077/*
Jens Axboecc09e292007-04-26 12:53:50 +020078 * Most of our rbtree usage is for sorting with min extraction, so
79 * if we cache the leftmost node we don't have to walk down the tree
80 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
81 * move this into the elevator for the rq sorting as well.
82 */
83struct cfq_rb_root {
84 struct rb_root rb;
85 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010086 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010087 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050088 u64 min_vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -050089 struct rb_node *active;
Jens Axboecc09e292007-04-26 12:53:50 +020090};
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010091#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, .left = NULL, \
92 .count = 0, .min_vdisktime = 0, }
Jens Axboecc09e292007-04-26 12:53:50 +020093
94/*
Jens Axboe6118b702009-06-30 09:34:12 +020095 * Per process-grouping structure
96 */
97struct cfq_queue {
98 /* reference count */
99 atomic_t ref;
100 /* various state flags, see below */
101 unsigned int flags;
102 /* parent cfq_data */
103 struct cfq_data *cfqd;
104 /* service_tree member */
105 struct rb_node rb_node;
106 /* service_tree key */
107 unsigned long rb_key;
108 /* prio tree member */
109 struct rb_node p_node;
110 /* prio tree root we belong to, if any */
111 struct rb_root *p_root;
112 /* sorted list of pending requests */
113 struct rb_root sort_list;
114 /* if fifo isn't expired, next request to serve */
115 struct request *next_rq;
116 /* requests queued in sort_list */
117 int queued[2];
118 /* currently allocated requests */
119 int allocated[2];
120 /* fifo list of requests in sort_list */
121 struct list_head fifo;
122
Vivek Goyaldae739e2009-12-03 12:59:45 -0500123 /* time when queue got scheduled in to dispatch first request. */
124 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500125 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100126 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500127 /* time when first request from queue completed and slice started. */
128 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200129 unsigned long slice_end;
130 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200131
132 /* pending metadata requests */
133 int meta_pending;
134 /* number of requests that are on the dispatch list or inside driver */
135 int dispatched;
136
137 /* io prio of this group */
138 unsigned short ioprio, org_ioprio;
139 unsigned short ioprio_class, org_ioprio_class;
140
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100141 pid_t pid;
142
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100143 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400144 sector_t last_request_pos;
145
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100146 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400147 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500148 struct cfq_group *cfqg;
Vivek Goyalae30c282009-12-03 12:59:55 -0500149 struct cfq_group *orig_cfqg;
Jens Axboe6118b702009-06-30 09:34:12 +0200150};
151
152/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100153 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100154 * IDLE is handled separately, so it has negative index
155 */
156enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100157 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500158 RT_WORKLOAD = 1,
159 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200160 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100161};
162
163/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100164 * Second index in the service_trees.
165 */
166enum wl_type_t {
167 ASYNC_WORKLOAD = 0,
168 SYNC_NOIDLE_WORKLOAD = 1,
169 SYNC_WORKLOAD = 2
170};
171
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500172/* This is per cgroup per device grouping structure */
173struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500174 /* group service_tree member */
175 struct rb_node rb_node;
176
177 /* group service_tree key */
178 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500179 unsigned int weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500180 bool on_st;
181
182 /* number of cfqq currently on this group */
183 int nr_cfqq;
184
Jens Axboe22e2c502005-06-27 10:55:12 +0200185 /*
Vivek Goyalb4627322010-10-22 09:48:43 +0200186 * Per group busy queus average. Useful for workload slice calc. We
187 * create the array for each prio class but at run time it is used
188 * only for RT and BE class and slot for IDLE class remains unused.
189 * This is primarily done to avoid confusion and a gcc warning.
190 */
191 unsigned int busy_queues_avg[CFQ_PRIO_NR];
192 /*
193 * rr lists of queues with requests. We maintain service trees for
194 * RT and BE classes. These trees are subdivided in subclasses
195 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
196 * class there is no subclassification and all the cfq queues go on
197 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100198 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200199 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100200 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100201 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500202
203 unsigned long saved_workload_slice;
204 enum wl_type_t saved_workload;
205 enum wl_prio_t saved_serving_prio;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500206 struct blkio_group blkg;
207#ifdef CONFIG_CFQ_GROUP_IOSCHED
208 struct hlist_node cfqd_node;
Vivek Goyalb1c35762009-12-03 12:59:47 -0500209 atomic_t ref;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500210#endif
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500211};
212
213/*
214 * Per block device queue structure
215 */
216struct cfq_data {
217 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500218 /* Root service tree for cfq_groups */
219 struct cfq_rb_root grp_service_tree;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500220 struct cfq_group root_group;
221
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100222 /*
223 * The priority currently being served
224 */
225 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100226 enum wl_type_t serving_type;
227 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500228 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200229
230 /*
231 * Each priority tree is sorted by next_request position. These
232 * trees are used when determining if two or more queues are
233 * interleaving requests (see cfq_close_cooperator).
234 */
235 struct rb_root prio_trees[CFQ_PRIO_LISTS];
236
Jens Axboe22e2c502005-06-27 10:55:12 +0200237 unsigned int busy_queues;
238
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100239 int rq_in_driver;
240 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200241
242 /*
243 * queue-depth detection
244 */
245 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200246 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100247 /*
248 * hw_tag can be
249 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
250 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
251 * 0 => no NCQ
252 */
253 int hw_tag_est_depth;
254 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200255
256 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200257 * idle window management
258 */
259 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200260 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200261
262 struct cfq_queue *active_queue;
263 struct cfq_io_context *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200264
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200265 /*
266 * async queue for each priority case
267 */
268 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
269 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200270
Jens Axboe6d048f52007-04-25 12:44:27 +0200271 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /*
274 * tunables, see top of file
275 */
276 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200277 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned int cfq_back_penalty;
279 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200280 unsigned int cfq_slice[2];
281 unsigned int cfq_slice_async_rq;
282 unsigned int cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200283 unsigned int cfq_latency;
Vivek Goyalae30c282009-12-03 12:59:55 -0500284 unsigned int cfq_group_isolation;
Al Virod9ff4182006-03-18 13:51:22 -0500285
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +0400286 unsigned int cic_index;
Al Virod9ff4182006-03-18 13:51:22 -0500287 struct list_head cic_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Jens Axboe6118b702009-06-30 09:34:12 +0200289 /*
290 * Fallback dummy cfqq for extreme OOM conditions
291 */
292 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200293
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100294 unsigned long last_delayed_sync;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500295
296 /* List of cfq groups being managed on this device*/
297 struct hlist_head cfqg_list;
Jens Axboebb729bc2009-12-06 09:54:19 +0100298 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Vivek Goyal25fb5162009-12-03 12:59:46 -0500301static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
302
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500303static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
304 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500305 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100306{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500307 if (!cfqg)
308 return NULL;
309
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100310 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500311 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100312
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500313 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100314}
315
Jens Axboe3b181522005-06-27 10:56:24 +0200316enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100317 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
318 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200319 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100320 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100321 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
322 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
323 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100324 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200325 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400326 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100327 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100328 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500329 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200330};
331
332#define CFQ_CFQQ_FNS(name) \
333static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
334{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100335 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200336} \
337static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
338{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100339 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200340} \
341static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
342{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100343 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200344}
345
346CFQ_CFQQ_FNS(on_rr);
347CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200348CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200349CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200350CFQ_CFQQ_FNS(fifo_expire);
351CFQ_CFQQ_FNS(idle_window);
352CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100353CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200354CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200355CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100356CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100357CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500358CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200359#undef CFQ_CFQQ_FNS
360
Vivek Goyalafc24d42010-04-26 19:27:56 +0200361#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal2868ef72009-12-03 12:59:48 -0500362#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
363 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
364 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
365 blkg_path(&(cfqq)->cfqg->blkg), ##args);
366
367#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
368 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
369 blkg_path(&(cfqg)->blkg), ##args); \
370
371#else
Jens Axboe7b679132008-05-30 12:23:07 +0200372#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
373 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500374#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0);
375#endif
Jens Axboe7b679132008-05-30 12:23:07 +0200376#define cfq_log(cfqd, fmt, args...) \
377 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
378
Vivek Goyal615f0252009-12-03 12:59:39 -0500379/* Traverses through cfq group service trees */
380#define for_each_cfqg_st(cfqg, i, j, st) \
381 for (i = 0; i <= IDLE_WORKLOAD; i++) \
382 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
383 : &cfqg->service_tree_idle; \
384 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
385 (i == IDLE_WORKLOAD && j == 0); \
386 j++, st = i < IDLE_WORKLOAD ? \
387 &cfqg->service_trees[i][j]: NULL) \
388
389
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100390static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
391{
392 if (cfq_class_idle(cfqq))
393 return IDLE_WORKLOAD;
394 if (cfq_class_rt(cfqq))
395 return RT_WORKLOAD;
396 return BE_WORKLOAD;
397}
398
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100399
400static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
401{
402 if (!cfq_cfqq_sync(cfqq))
403 return ASYNC_WORKLOAD;
404 if (!cfq_cfqq_idle_window(cfqq))
405 return SYNC_NOIDLE_WORKLOAD;
406 return SYNC_WORKLOAD;
407}
408
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500409static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
410 struct cfq_data *cfqd,
411 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100412{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500413 if (wl == IDLE_WORKLOAD)
414 return cfqg->service_tree_idle.count;
415
416 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
417 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
418 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100419}
420
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500421static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
422 struct cfq_group *cfqg)
423{
424 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
425 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
426}
427
Jens Axboe165125e2007-07-24 09:28:11 +0200428static void cfq_dispatch_insert(struct request_queue *, struct request *);
Jens Axboea6151c32009-10-07 20:02:57 +0200429static struct cfq_queue *cfq_get_queue(struct cfq_data *, bool,
Jens Axboefd0928d2008-01-24 08:52:45 +0100430 struct io_context *, gfp_t);
Jens Axboe4ac845a2008-01-24 08:44:49 +0100431static struct cfq_io_context *cfq_cic_lookup(struct cfq_data *,
Vasily Tarasov91fac312007-04-25 12:29:51 +0200432 struct io_context *);
433
434static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200435 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200436{
Jens Axboea6151c32009-10-07 20:02:57 +0200437 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200438}
439
440static inline void cic_set_cfqq(struct cfq_io_context *cic,
Jens Axboea6151c32009-10-07 20:02:57 +0200441 struct cfq_queue *cfqq, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200442{
Jens Axboea6151c32009-10-07 20:02:57 +0200443 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200444}
445
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400446#define CIC_DEAD_KEY 1ul
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +0400447#define CIC_DEAD_INDEX_SHIFT 1
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400448
449static inline void *cfqd_dead_key(struct cfq_data *cfqd)
450{
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +0400451 return (void *)(cfqd->cic_index << CIC_DEAD_INDEX_SHIFT | CIC_DEAD_KEY);
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400452}
453
454static inline struct cfq_data *cic_to_cfqd(struct cfq_io_context *cic)
455{
456 struct cfq_data *cfqd = cic->key;
457
458 if (unlikely((unsigned long) cfqd & CIC_DEAD_KEY))
459 return NULL;
460
461 return cfqd;
462}
463
Vasily Tarasov91fac312007-04-25 12:29:51 +0200464/*
465 * We regard a request as SYNC, if it's either a read or has the SYNC bit
466 * set (in which case it could also be direct WRITE).
467 */
Jens Axboea6151c32009-10-07 20:02:57 +0200468static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200469{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200470 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700474 * scheduler run of queue, if there are requests pending and no one in the
475 * driver that will restart queueing
476 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200477static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700478{
Jens Axboe7b679132008-05-30 12:23:07 +0200479 if (cfqd->busy_queues) {
480 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200481 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200482 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700483}
484
Jens Axboe165125e2007-07-24 09:28:11 +0200485static int cfq_queue_empty(struct request_queue *q)
Andrew Morton99f95e52005-06-27 20:14:05 -0700486{
487 struct cfq_data *cfqd = q->elevator->elevator_data;
488
Vivek Goyalf04a6422009-12-03 12:59:40 -0500489 return !cfqd->rq_queued;
Andrew Morton99f95e52005-06-27 20:14:05 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100493 * Scale schedule slice based on io priority. Use the sync time slice only
494 * if a queue is marked sync and has sync io queued. A sync queue with async
495 * io only, should not get full sync slice length.
496 */
Jens Axboea6151c32009-10-07 20:02:57 +0200497static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200498 unsigned short prio)
499{
500 const int base_slice = cfqd->cfq_slice[sync];
501
502 WARN_ON(prio >= IOPRIO_BE_NR);
503
504 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
505}
506
Jens Axboe44f7c162007-01-19 11:51:58 +1100507static inline int
508cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
509{
Jens Axboed9e76202007-04-20 14:27:50 +0200510 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100511}
512
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500513static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
514{
515 u64 d = delta << CFQ_SERVICE_SHIFT;
516
517 d = d * BLKIO_WEIGHT_DEFAULT;
518 do_div(d, cfqg->weight);
519 return d;
520}
521
522static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
523{
524 s64 delta = (s64)(vdisktime - min_vdisktime);
525 if (delta > 0)
526 min_vdisktime = vdisktime;
527
528 return min_vdisktime;
529}
530
531static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
532{
533 s64 delta = (s64)(vdisktime - min_vdisktime);
534 if (delta < 0)
535 min_vdisktime = vdisktime;
536
537 return min_vdisktime;
538}
539
540static void update_min_vdisktime(struct cfq_rb_root *st)
541{
542 u64 vdisktime = st->min_vdisktime;
543 struct cfq_group *cfqg;
544
545 if (st->active) {
546 cfqg = rb_entry_cfqg(st->active);
547 vdisktime = cfqg->vdisktime;
548 }
549
550 if (st->left) {
551 cfqg = rb_entry_cfqg(st->left);
552 vdisktime = min_vdisktime(vdisktime, cfqg->vdisktime);
553 }
554
555 st->min_vdisktime = max_vdisktime(st->min_vdisktime, vdisktime);
556}
557
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100558/*
559 * get averaged number of queues of RT/BE priority.
560 * average is updated, with a formula that gives more weight to higher numbers,
561 * to quickly follows sudden increases and decrease slowly
562 */
563
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500564static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
565 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100566{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100567 unsigned min_q, max_q;
568 unsigned mult = cfq_hist_divisor - 1;
569 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500570 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100571
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500572 min_q = min(cfqg->busy_queues_avg[rt], busy);
573 max_q = max(cfqg->busy_queues_avg[rt], busy);
574 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100575 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500576 return cfqg->busy_queues_avg[rt];
577}
578
579static inline unsigned
580cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
581{
582 struct cfq_rb_root *st = &cfqd->grp_service_tree;
583
584 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100585}
586
Jens Axboe44f7c162007-01-19 11:51:58 +1100587static inline void
588cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
589{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100590 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
591 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500592 /*
593 * interested queues (we consider only the ones with the same
594 * priority class in the cfq group)
595 */
596 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
597 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100598 unsigned sync_slice = cfqd->cfq_slice[1];
599 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500600 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
601
602 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100603 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
604 /* scale low_slice according to IO priority
605 * and sync vs async */
606 unsigned low_slice =
607 min(slice, base_low_slice * slice / sync_slice);
608 /* the adapted slice value is scaled to fit all iqs
609 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500610 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100611 low_slice);
612 }
613 }
Vivek Goyaldae739e2009-12-03 12:59:45 -0500614 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100615 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500616 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200617 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100618}
619
620/*
621 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
622 * isn't valid until the first request from the dispatch is activated
623 * and the slice time set.
624 */
Jens Axboea6151c32009-10-07 20:02:57 +0200625static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100626{
627 if (cfq_cfqq_slice_new(cfqq))
628 return 0;
629 if (time_before(jiffies, cfqq->slice_end))
630 return 0;
631
632 return 1;
633}
634
635/*
Jens Axboe5e705372006-07-13 12:39:25 +0200636 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200638 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
Jens Axboe5e705372006-07-13 12:39:25 +0200640static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100641cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100643 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200645#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
646#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
647 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jens Axboe5e705372006-07-13 12:39:25 +0200649 if (rq1 == NULL || rq1 == rq2)
650 return rq2;
651 if (rq2 == NULL)
652 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200653
Jens Axboe5e705372006-07-13 12:39:25 +0200654 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
655 return rq1;
656 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
657 return rq2;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200658 if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
Jens Axboe374f84a2006-07-23 01:42:19 +0200659 return rq1;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200660 else if ((rq2->cmd_flags & REQ_META) &&
661 !(rq1->cmd_flags & REQ_META))
Jens Axboe374f84a2006-07-23 01:42:19 +0200662 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Tejun Heo83096eb2009-05-07 22:24:39 +0900664 s1 = blk_rq_pos(rq1);
665 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * by definition, 1KiB is 2 sectors
669 */
670 back_max = cfqd->cfq_back_max * 2;
671
672 /*
673 * Strict one way elevator _except_ in the case where we allow
674 * short backward seeks which are biased as twice the cost of a
675 * similar forward seek.
676 */
677 if (s1 >= last)
678 d1 = s1 - last;
679 else if (s1 + back_max >= last)
680 d1 = (last - s1) * cfqd->cfq_back_penalty;
681 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200682 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (s2 >= last)
685 d2 = s2 - last;
686 else if (s2 + back_max >= last)
687 d2 = (last - s2) * cfqd->cfq_back_penalty;
688 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200689 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Andreas Mohre8a99052006-03-28 08:59:49 +0200693 /*
694 * By doing switch() on the bit mask "wrap" we avoid having to
695 * check two variables for all permutations: --> faster!
696 */
697 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200698 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200699 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200700 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200701 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200702 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200703 else {
704 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200705 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200706 else
Jens Axboe5e705372006-07-13 12:39:25 +0200707 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200708 }
709
710 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200711 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200712 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200713 return rq2;
714 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200715 default:
716 /*
717 * Since both rqs are wrapped,
718 * start with the one that's further behind head
719 * (--> only *one* back seek required),
720 * since back seek takes more time than forward.
721 */
722 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200723 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 else
Jens Axboe5e705372006-07-13 12:39:25 +0200725 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727}
728
Jens Axboe498d3aa22007-04-26 12:54:48 +0200729/*
730 * The below is leftmost cache rbtree addon
731 */
Jens Axboe08717142008-01-28 11:38:15 +0100732static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200733{
Vivek Goyal615f0252009-12-03 12:59:39 -0500734 /* Service tree is empty */
735 if (!root->count)
736 return NULL;
737
Jens Axboecc09e292007-04-26 12:53:50 +0200738 if (!root->left)
739 root->left = rb_first(&root->rb);
740
Jens Axboe08717142008-01-28 11:38:15 +0100741 if (root->left)
742 return rb_entry(root->left, struct cfq_queue, rb_node);
743
744 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200745}
746
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500747static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
748{
749 if (!root->left)
750 root->left = rb_first(&root->rb);
751
752 if (root->left)
753 return rb_entry_cfqg(root->left);
754
755 return NULL;
756}
757
Jens Axboea36e71f2009-04-15 12:15:11 +0200758static void rb_erase_init(struct rb_node *n, struct rb_root *root)
759{
760 rb_erase(n, root);
761 RB_CLEAR_NODE(n);
762}
763
Jens Axboecc09e292007-04-26 12:53:50 +0200764static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
765{
766 if (root->left == n)
767 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200768 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100769 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/*
773 * would be nice to take fifo expire time into account as well
774 */
Jens Axboe5e705372006-07-13 12:39:25 +0200775static struct request *
776cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
777 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Jens Axboe21183b02006-07-13 12:33:14 +0200779 struct rb_node *rbnext = rb_next(&last->rb_node);
780 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200781 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jens Axboe21183b02006-07-13 12:33:14 +0200783 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200786 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200789 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200790 else {
791 rbnext = rb_first(&cfqq->sort_list);
792 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200793 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100796 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Jens Axboed9e76202007-04-20 14:27:50 +0200799static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
800 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Jens Axboed9e76202007-04-20 14:27:50 +0200802 /*
803 * just an approximation, should be ok.
804 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500805 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100806 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200807}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500809static inline s64
810cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
811{
812 return cfqg->vdisktime - st->min_vdisktime;
813}
814
815static void
816__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
817{
818 struct rb_node **node = &st->rb.rb_node;
819 struct rb_node *parent = NULL;
820 struct cfq_group *__cfqg;
821 s64 key = cfqg_key(st, cfqg);
822 int left = 1;
823
824 while (*node != NULL) {
825 parent = *node;
826 __cfqg = rb_entry_cfqg(parent);
827
828 if (key < cfqg_key(st, __cfqg))
829 node = &parent->rb_left;
830 else {
831 node = &parent->rb_right;
832 left = 0;
833 }
834 }
835
836 if (left)
837 st->left = &cfqg->rb_node;
838
839 rb_link_node(&cfqg->rb_node, parent, node);
840 rb_insert_color(&cfqg->rb_node, &st->rb);
841}
842
843static void
844cfq_group_service_tree_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
845{
846 struct cfq_rb_root *st = &cfqd->grp_service_tree;
847 struct cfq_group *__cfqg;
848 struct rb_node *n;
849
850 cfqg->nr_cfqq++;
851 if (cfqg->on_st)
852 return;
853
854 /*
855 * Currently put the group at the end. Later implement something
856 * so that groups get lesser vtime based on their weights, so that
857 * if group does not loose all if it was not continously backlogged.
858 */
859 n = rb_last(&st->rb);
860 if (n) {
861 __cfqg = rb_entry_cfqg(n);
862 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
863 } else
864 cfqg->vdisktime = st->min_vdisktime;
865
866 __cfq_group_service_tree_add(st, cfqg);
867 cfqg->on_st = true;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500868 st->total_weight += cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500869}
870
871static void
872cfq_group_service_tree_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
873{
874 struct cfq_rb_root *st = &cfqd->grp_service_tree;
875
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500876 if (st->active == &cfqg->rb_node)
877 st->active = NULL;
878
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500879 BUG_ON(cfqg->nr_cfqq < 1);
880 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500881
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500882 /* If there are other cfq queues under this group, don't delete it */
883 if (cfqg->nr_cfqq)
884 return;
885
Vivek Goyal2868ef72009-12-03 12:59:48 -0500886 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500887 cfqg->on_st = false;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500888 st->total_weight -= cfqg->weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500889 if (!RB_EMPTY_NODE(&cfqg->rb_node))
890 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500891 cfqg->saved_workload_slice = 0;
Vivek Goyale98ef892010-06-18 10:39:47 -0400892 cfq_blkiocg_update_dequeue_stats(&cfqg->blkg, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500893}
894
895static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq)
896{
Vivek Goyalf75edf22009-12-03 12:59:53 -0500897 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500898
899 /*
900 * Queue got expired before even a single request completed or
901 * got expired immediately after first request completion.
902 */
903 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
904 /*
905 * Also charge the seek time incurred to the group, otherwise
906 * if there are mutiple queues in the group, each can dispatch
907 * a single request on seeky media and cause lots of seek time
908 * and group will never know it.
909 */
910 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
911 1);
912 } else {
913 slice_used = jiffies - cfqq->slice_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500914 if (slice_used > cfqq->allocated_slice)
915 slice_used = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500916 }
917
Divyesh Shah9a0785b2010-04-01 15:01:04 -0700918 cfq_log_cfqq(cfqq->cfqd, cfqq, "sl_used=%u", slice_used);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500919 return slice_used;
920}
921
922static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +0200923 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -0500924{
925 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500926 unsigned int used_sl, charge_sl;
927 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
928 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500929
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500930 BUG_ON(nr_sync < 0);
931 used_sl = charge_sl = cfq_cfqq_slice_usage(cfqq);
932
933 if (!cfq_cfqq_sync(cfqq) && !nr_sync)
934 charge_sl = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500935
936 /* Can't update vdisktime while group is on service tree */
937 cfq_rb_erase(&cfqg->rb_node, st);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500938 cfqg->vdisktime += cfq_scale_slice(charge_sl, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -0500939 __cfq_group_service_tree_add(st, cfqg);
940
941 /* This group is being expired. Save the context */
942 if (time_after(cfqd->workload_expires, jiffies)) {
943 cfqg->saved_workload_slice = cfqd->workload_expires
944 - jiffies;
945 cfqg->saved_workload = cfqd->serving_type;
946 cfqg->saved_serving_prio = cfqd->serving_prio;
947 } else
948 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -0500949
950 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
951 st->min_vdisktime);
Vivek Goyale98ef892010-06-18 10:39:47 -0400952 cfq_blkiocg_update_timeslice_used(&cfqg->blkg, used_sl);
953 cfq_blkiocg_set_start_empty_time(&cfqg->blkg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500954}
955
Vivek Goyal25fb5162009-12-03 12:59:46 -0500956#ifdef CONFIG_CFQ_GROUP_IOSCHED
957static inline struct cfq_group *cfqg_of_blkg(struct blkio_group *blkg)
958{
959 if (blkg)
960 return container_of(blkg, struct cfq_group, blkg);
961 return NULL;
962}
963
Vivek Goyalfe071432010-10-01 14:49:49 +0200964void cfq_update_blkio_group_weight(void *key, struct blkio_group *blkg,
965 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -0500966{
967 cfqg_of_blkg(blkg)->weight = weight;
968}
969
Vivek Goyal25fb5162009-12-03 12:59:46 -0500970static struct cfq_group *
971cfq_find_alloc_cfqg(struct cfq_data *cfqd, struct cgroup *cgroup, int create)
972{
973 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
974 struct cfq_group *cfqg = NULL;
975 void *key = cfqd;
976 int i, j;
977 struct cfq_rb_root *st;
Vivek Goyal22084192009-12-03 12:59:49 -0500978 struct backing_dev_info *bdi = &cfqd->queue->backing_dev_info;
979 unsigned int major, minor;
Vivek Goyal25fb5162009-12-03 12:59:46 -0500980
Vivek Goyal25fb5162009-12-03 12:59:46 -0500981 cfqg = cfqg_of_blkg(blkiocg_lookup_group(blkcg, key));
Ricky Beniteza74b2ad2010-04-05 18:22:17 +0200982 if (cfqg && !cfqg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
983 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
984 cfqg->blkg.dev = MKDEV(major, minor);
985 goto done;
986 }
Vivek Goyal25fb5162009-12-03 12:59:46 -0500987 if (cfqg || !create)
988 goto done;
989
990 cfqg = kzalloc_node(sizeof(*cfqg), GFP_ATOMIC, cfqd->queue->node);
991 if (!cfqg)
992 goto done;
993
Vivek Goyal25fb5162009-12-03 12:59:46 -0500994 for_each_cfqg_st(cfqg, i, j, st)
995 *st = CFQ_RB_ROOT;
996 RB_CLEAR_NODE(&cfqg->rb_node);
997
Vivek Goyalb1c35762009-12-03 12:59:47 -0500998 /*
999 * Take the initial reference that will be released on destroy
1000 * This can be thought of a joint reference by cgroup and
1001 * elevator which will be dropped by either elevator exit
1002 * or cgroup deletion path depending on who is exiting first.
1003 */
1004 atomic_set(&cfqg->ref, 1);
1005
Vivek Goyal25fb5162009-12-03 12:59:46 -05001006 /* Add group onto cgroup list */
Vivek Goyal22084192009-12-03 12:59:49 -05001007 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
Vivek Goyale98ef892010-06-18 10:39:47 -04001008 cfq_blkiocg_add_blkio_group(blkcg, &cfqg->blkg, (void *)cfqd,
Vivek Goyal22084192009-12-03 12:59:49 -05001009 MKDEV(major, minor));
Gui Jianfeng34d0f172010-04-13 16:05:49 +08001010 cfqg->weight = blkcg_get_weight(blkcg, cfqg->blkg.dev);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001011
1012 /* Add group on cfqd list */
1013 hlist_add_head(&cfqg->cfqd_node, &cfqd->cfqg_list);
1014
1015done:
Vivek Goyal25fb5162009-12-03 12:59:46 -05001016 return cfqg;
1017}
1018
1019/*
1020 * Search for the cfq group current task belongs to. If create = 1, then also
1021 * create the cfq group if it does not exist. request_queue lock must be held.
1022 */
1023static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1024{
1025 struct cgroup *cgroup;
1026 struct cfq_group *cfqg = NULL;
1027
1028 rcu_read_lock();
1029 cgroup = task_cgroup(current, blkio_subsys_id);
1030 cfqg = cfq_find_alloc_cfqg(cfqd, cgroup, create);
1031 if (!cfqg && create)
1032 cfqg = &cfqd->root_group;
1033 rcu_read_unlock();
1034 return cfqg;
1035}
1036
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001037static inline struct cfq_group *cfq_ref_get_cfqg(struct cfq_group *cfqg)
1038{
1039 atomic_inc(&cfqg->ref);
1040 return cfqg;
1041}
1042
Vivek Goyal25fb5162009-12-03 12:59:46 -05001043static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1044{
1045 /* Currently, all async queues are mapped to root group */
1046 if (!cfq_cfqq_sync(cfqq))
1047 cfqg = &cfqq->cfqd->root_group;
1048
1049 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001050 /* cfqq reference on cfqg */
1051 atomic_inc(&cfqq->cfqg->ref);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001052}
Vivek Goyalb1c35762009-12-03 12:59:47 -05001053
1054static void cfq_put_cfqg(struct cfq_group *cfqg)
1055{
1056 struct cfq_rb_root *st;
1057 int i, j;
1058
1059 BUG_ON(atomic_read(&cfqg->ref) <= 0);
1060 if (!atomic_dec_and_test(&cfqg->ref))
1061 return;
1062 for_each_cfqg_st(cfqg, i, j, st)
1063 BUG_ON(!RB_EMPTY_ROOT(&st->rb) || st->active != NULL);
1064 kfree(cfqg);
1065}
1066
1067static void cfq_destroy_cfqg(struct cfq_data *cfqd, struct cfq_group *cfqg)
1068{
1069 /* Something wrong if we are trying to remove same group twice */
1070 BUG_ON(hlist_unhashed(&cfqg->cfqd_node));
1071
1072 hlist_del_init(&cfqg->cfqd_node);
1073
1074 /*
1075 * Put the reference taken at the time of creation so that when all
1076 * queues are gone, group can be destroyed.
1077 */
1078 cfq_put_cfqg(cfqg);
1079}
1080
1081static void cfq_release_cfq_groups(struct cfq_data *cfqd)
1082{
1083 struct hlist_node *pos, *n;
1084 struct cfq_group *cfqg;
1085
1086 hlist_for_each_entry_safe(cfqg, pos, n, &cfqd->cfqg_list, cfqd_node) {
1087 /*
1088 * If cgroup removal path got to blk_group first and removed
1089 * it from cgroup list, then it will take care of destroying
1090 * cfqg also.
1091 */
Vivek Goyale98ef892010-06-18 10:39:47 -04001092 if (!cfq_blkiocg_del_blkio_group(&cfqg->blkg))
Vivek Goyalb1c35762009-12-03 12:59:47 -05001093 cfq_destroy_cfqg(cfqd, cfqg);
1094 }
1095}
1096
1097/*
1098 * Blk cgroup controller notification saying that blkio_group object is being
1099 * delinked as associated cgroup object is going away. That also means that
1100 * no new IO will come in this group. So get rid of this group as soon as
1101 * any pending IO in the group is finished.
1102 *
1103 * This function is called under rcu_read_lock(). key is the rcu protected
1104 * pointer. That means "key" is a valid cfq_data pointer as long as we are rcu
1105 * read lock.
1106 *
1107 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1108 * it should not be NULL as even if elevator was exiting, cgroup deltion
1109 * path got to it first.
1110 */
1111void cfq_unlink_blkio_group(void *key, struct blkio_group *blkg)
1112{
1113 unsigned long flags;
1114 struct cfq_data *cfqd = key;
1115
1116 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
1117 cfq_destroy_cfqg(cfqd, cfqg_of_blkg(blkg));
1118 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
1119}
1120
Vivek Goyal25fb5162009-12-03 12:59:46 -05001121#else /* GROUP_IOSCHED */
1122static struct cfq_group *cfq_get_cfqg(struct cfq_data *cfqd, int create)
1123{
1124 return &cfqd->root_group;
1125}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001126
1127static inline struct cfq_group *cfq_ref_get_cfqg(struct cfq_group *cfqg)
1128{
Dmitry Monakhov50eaeb32010-04-28 19:50:33 +02001129 return cfqg;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001130}
1131
Vivek Goyal25fb5162009-12-03 12:59:46 -05001132static inline void
1133cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1134 cfqq->cfqg = cfqg;
1135}
1136
Vivek Goyalb1c35762009-12-03 12:59:47 -05001137static void cfq_release_cfq_groups(struct cfq_data *cfqd) {}
1138static inline void cfq_put_cfqg(struct cfq_group *cfqg) {}
1139
Vivek Goyal25fb5162009-12-03 12:59:46 -05001140#endif /* GROUP_IOSCHED */
1141
Jens Axboe498d3aa22007-04-26 12:54:48 +02001142/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001143 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001144 * requests waiting to be processed. It is sorted in the order that
1145 * we will service the queues.
1146 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001147static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001148 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001149{
Jens Axboe08717142008-01-28 11:38:15 +01001150 struct rb_node **p, *parent;
1151 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001152 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001153 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001154 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001155 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001156 int group_changed = 0;
1157
1158#ifdef CONFIG_CFQ_GROUP_IOSCHED
1159 if (!cfqd->cfq_group_isolation
1160 && cfqq_type(cfqq) == SYNC_NOIDLE_WORKLOAD
1161 && cfqq->cfqg && cfqq->cfqg != &cfqd->root_group) {
1162 /* Move this cfq to root group */
1163 cfq_log_cfqq(cfqd, cfqq, "moving to root group");
1164 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1165 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1166 cfqq->orig_cfqg = cfqq->cfqg;
1167 cfqq->cfqg = &cfqd->root_group;
1168 atomic_inc(&cfqd->root_group.ref);
1169 group_changed = 1;
1170 } else if (!cfqd->cfq_group_isolation
1171 && cfqq_type(cfqq) == SYNC_WORKLOAD && cfqq->orig_cfqg) {
1172 /* cfqq is sequential now needs to go to its original group */
1173 BUG_ON(cfqq->cfqg != &cfqd->root_group);
1174 if (!RB_EMPTY_NODE(&cfqq->rb_node))
1175 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
1176 cfq_put_cfqg(cfqq->cfqg);
1177 cfqq->cfqg = cfqq->orig_cfqg;
1178 cfqq->orig_cfqg = NULL;
1179 group_changed = 1;
1180 cfq_log_cfqq(cfqd, cfqq, "moved to origin group");
1181 }
1182#endif
Jens Axboed9e76202007-04-20 14:27:50 +02001183
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001184 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001185 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001186 if (cfq_class_idle(cfqq)) {
1187 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001188 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001189 if (parent && parent != &cfqq->rb_node) {
1190 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1191 rb_key += __cfqq->rb_key;
1192 } else
1193 rb_key += jiffies;
1194 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001195 /*
1196 * Get our rb key offset. Subtract any residual slice
1197 * value carried from last service. A negative resid
1198 * count indicates slice overrun, and this should position
1199 * the next service time further away in the tree.
1200 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001201 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001202 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001203 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001204 } else {
1205 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001206 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001207 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1208 }
Jens Axboed9e76202007-04-20 14:27:50 +02001209
1210 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001211 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001212 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001213 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001214 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001215 if (rb_key == cfqq->rb_key &&
1216 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001217 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001218
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001219 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1220 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001221 }
Jens Axboed9e76202007-04-20 14:27:50 +02001222
Jens Axboe498d3aa22007-04-26 12:54:48 +02001223 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001224 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001225 cfqq->service_tree = service_tree;
1226 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001227 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001228 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001229
Jens Axboed9e76202007-04-20 14:27:50 +02001230 parent = *p;
1231 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1232
Jens Axboe0c534e02007-04-18 20:01:57 +02001233 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001234 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001235 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001236 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001237 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001238 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001239 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001240 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001241 }
Jens Axboe67060e32007-04-18 20:13:32 +02001242
1243 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001244 }
1245
Jens Axboecc09e292007-04-26 12:53:50 +02001246 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001247 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001248
Jens Axboed9e76202007-04-20 14:27:50 +02001249 cfqq->rb_key = rb_key;
1250 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001251 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1252 service_tree->count++;
Vivek Goyalae30c282009-12-03 12:59:55 -05001253 if ((add_front || !new_cfqq) && !group_changed)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001254 return;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001255 cfq_group_service_tree_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Jens Axboea36e71f2009-04-15 12:15:11 +02001258static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001259cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1260 sector_t sector, struct rb_node **ret_parent,
1261 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001262{
Jens Axboea36e71f2009-04-15 12:15:11 +02001263 struct rb_node **p, *parent;
1264 struct cfq_queue *cfqq = NULL;
1265
1266 parent = NULL;
1267 p = &root->rb_node;
1268 while (*p) {
1269 struct rb_node **n;
1270
1271 parent = *p;
1272 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1273
1274 /*
1275 * Sort strictly based on sector. Smallest to the left,
1276 * largest to the right.
1277 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001278 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001279 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001280 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001281 n = &(*p)->rb_left;
1282 else
1283 break;
1284 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001285 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001286 }
1287
1288 *ret_parent = parent;
1289 if (rb_link)
1290 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001291 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001292}
1293
1294static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1295{
Jens Axboea36e71f2009-04-15 12:15:11 +02001296 struct rb_node **p, *parent;
1297 struct cfq_queue *__cfqq;
1298
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001299 if (cfqq->p_root) {
1300 rb_erase(&cfqq->p_node, cfqq->p_root);
1301 cfqq->p_root = NULL;
1302 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001303
1304 if (cfq_class_idle(cfqq))
1305 return;
1306 if (!cfqq->next_rq)
1307 return;
1308
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001309 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001310 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1311 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001312 if (!__cfqq) {
1313 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001314 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1315 } else
1316 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001317}
1318
Jens Axboe498d3aa22007-04-26 12:54:48 +02001319/*
1320 * Update cfqq's position in the service tree.
1321 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001322static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001323{
Jens Axboe6d048f52007-04-25 12:44:27 +02001324 /*
1325 * Resorting requires the cfqq to be on the RR list already.
1326 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001327 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001328 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001329 cfq_prio_tree_add(cfqd, cfqq);
1330 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001335 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Jens Axboefebffd62008-01-28 13:19:43 +01001337static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Jens Axboe7b679132008-05-30 12:23:07 +02001339 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001340 BUG_ON(cfq_cfqq_on_rr(cfqq));
1341 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 cfqd->busy_queues++;
1343
Jens Axboeedd75ff2007-04-19 12:03:34 +02001344 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Jens Axboe498d3aa22007-04-26 12:54:48 +02001347/*
1348 * Called when the cfqq no longer has requests pending, remove it from
1349 * the service tree.
1350 */
Jens Axboefebffd62008-01-28 13:19:43 +01001351static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Jens Axboe7b679132008-05-30 12:23:07 +02001353 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001354 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1355 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001357 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1358 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1359 cfqq->service_tree = NULL;
1360 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001361 if (cfqq->p_root) {
1362 rb_erase(&cfqq->p_node, cfqq->p_root);
1363 cfqq->p_root = NULL;
1364 }
Jens Axboed9e76202007-04-20 14:27:50 +02001365
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001366 cfq_group_service_tree_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 BUG_ON(!cfqd->busy_queues);
1368 cfqd->busy_queues--;
1369}
1370
1371/*
1372 * rb tree support functions
1373 */
Jens Axboefebffd62008-01-28 13:19:43 +01001374static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Jens Axboe5e705372006-07-13 12:39:25 +02001376 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001377 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Jens Axboeb4878f22005-10-20 16:42:29 +02001379 BUG_ON(!cfqq->queued[sync]);
1380 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jens Axboe5e705372006-07-13 12:39:25 +02001382 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Vivek Goyalf04a6422009-12-03 12:59:40 -05001384 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1385 /*
1386 * Queue will be deleted from service tree when we actually
1387 * expire it later. Right now just remove it from prio tree
1388 * as it is empty.
1389 */
1390 if (cfqq->p_root) {
1391 rb_erase(&cfqq->p_node, cfqq->p_root);
1392 cfqq->p_root = NULL;
1393 }
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Jens Axboe5e705372006-07-13 12:39:25 +02001397static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Jens Axboe5e705372006-07-13 12:39:25 +02001399 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboea36e71f2009-04-15 12:15:11 +02001401 struct request *__alias, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Jens Axboe5380a102006-07-13 12:37:56 +02001403 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /*
1406 * looks a little odd, but the first insert might return an alias.
1407 * if that happens, put the alias on the dispatch list
1408 */
Jens Axboe21183b02006-07-13 12:33:14 +02001409 while ((__alias = elv_rb_add(&cfqq->sort_list, rq)) != NULL)
Jens Axboe5e705372006-07-13 12:39:25 +02001410 cfq_dispatch_insert(cfqd->queue, __alias);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001411
1412 if (!cfq_cfqq_on_rr(cfqq))
1413 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001414
1415 /*
1416 * check if this request is a better next-serve candidate
1417 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001418 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001419 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001420
1421 /*
1422 * adjust priority tree position, if ->next_rq changes
1423 */
1424 if (prev != cfqq->next_rq)
1425 cfq_prio_tree_add(cfqd, cfqq);
1426
Jens Axboe5044eed2007-04-25 11:53:48 +02001427 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Jens Axboefebffd62008-01-28 13:19:43 +01001430static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Jens Axboe5380a102006-07-13 12:37:56 +02001432 elv_rb_del(&cfqq->sort_list, rq);
1433 cfqq->queued[rq_is_sync(rq)]--;
Vivek Goyale98ef892010-06-18 10:39:47 -04001434 cfq_blkiocg_update_io_remove_stats(&(RQ_CFQG(rq))->blkg,
1435 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001436 cfq_add_rq_rb(rq);
Vivek Goyale98ef892010-06-18 10:39:47 -04001437 cfq_blkiocg_update_io_add_stats(&(RQ_CFQG(rq))->blkg,
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001438 &cfqq->cfqd->serving_group->blkg, rq_data_dir(rq),
1439 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Jens Axboe206dc692006-03-28 13:03:44 +02001442static struct request *
1443cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Jens Axboe206dc692006-03-28 13:03:44 +02001445 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001446 struct cfq_io_context *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001447 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Jens Axboe4ac845a2008-01-24 08:44:49 +01001449 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001450 if (!cic)
1451 return NULL;
1452
1453 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001454 if (cfqq) {
1455 sector_t sector = bio->bi_sector + bio_sectors(bio);
1456
Jens Axboe21183b02006-07-13 12:33:14 +02001457 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return NULL;
1461}
1462
Jens Axboe165125e2007-07-24 09:28:11 +02001463static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001464{
1465 struct cfq_data *cfqd = q->elevator->elevator_data;
1466
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001467 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001468 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001469 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001470
Tejun Heo5b936292009-05-07 22:24:38 +09001471 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001472}
1473
Jens Axboe165125e2007-07-24 09:28:11 +02001474static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Jens Axboe22e2c502005-06-27 10:55:12 +02001476 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001478 WARN_ON(!cfqd->rq_in_driver);
1479 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001480 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001481 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Jens Axboeb4878f22005-10-20 16:42:29 +02001484static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Jens Axboe5e705372006-07-13 12:39:25 +02001486 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001487
Jens Axboe5e705372006-07-13 12:39:25 +02001488 if (cfqq->next_rq == rq)
1489 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jens Axboeb4878f22005-10-20 16:42:29 +02001491 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001492 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001493
Aaron Carroll45333d52008-08-26 15:52:36 +02001494 cfqq->cfqd->rq_queued--;
Vivek Goyale98ef892010-06-18 10:39:47 -04001495 cfq_blkiocg_update_io_remove_stats(&(RQ_CFQG(rq))->blkg,
1496 rq_data_dir(rq), rq_is_sync(rq));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001497 if (rq->cmd_flags & REQ_META) {
Jens Axboe374f84a2006-07-23 01:42:19 +02001498 WARN_ON(!cfqq->meta_pending);
1499 cfqq->meta_pending--;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Jens Axboe165125e2007-07-24 09:28:11 +02001503static int cfq_merge(struct request_queue *q, struct request **req,
1504 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 struct cfq_data *cfqd = q->elevator->elevator_data;
1507 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jens Axboe206dc692006-03-28 13:03:44 +02001509 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001510 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001511 *req = __rq;
1512 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514
1515 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Jens Axboe165125e2007-07-24 09:28:11 +02001518static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001519 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Jens Axboe21183b02006-07-13 12:33:14 +02001521 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001522 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Jens Axboe5e705372006-07-13 12:39:25 +02001524 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Divyesh Shah812d4022010-04-08 21:14:23 -07001528static void cfq_bio_merged(struct request_queue *q, struct request *req,
1529 struct bio *bio)
1530{
Vivek Goyale98ef892010-06-18 10:39:47 -04001531 cfq_blkiocg_update_io_merged_stats(&(RQ_CFQG(req))->blkg,
1532 bio_data_dir(bio), cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535static void
Jens Axboe165125e2007-07-24 09:28:11 +02001536cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct request *next)
1538{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001539 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001540 /*
1541 * reposition in fifo if next is older than rq
1542 */
1543 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001544 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001545 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001546 rq_set_fifo_time(rq, rq_fifo_time(next));
1547 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001548
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001549 if (cfqq->next_rq == next)
1550 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001551 cfq_remove_request(next);
Vivek Goyale98ef892010-06-18 10:39:47 -04001552 cfq_blkiocg_update_io_merged_stats(&(RQ_CFQG(rq))->blkg,
1553 rq_data_dir(next), rq_is_sync(next));
Jens Axboe22e2c502005-06-27 10:55:12 +02001554}
1555
Jens Axboe165125e2007-07-24 09:28:11 +02001556static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001557 struct bio *bio)
1558{
1559 struct cfq_data *cfqd = q->elevator->elevator_data;
Vasily Tarasov91fac312007-04-25 12:29:51 +02001560 struct cfq_io_context *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001561 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001562
1563 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001564 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001565 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001566 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001567 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001568
1569 /*
Jens Axboe719d3402006-12-22 09:38:53 +01001570 * Lookup the cfqq that this bio will be queued with. Allow
1571 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001572 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01001573 cic = cfq_cic_lookup(cfqd, current->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001574 if (!cic)
Jens Axboea6151c32009-10-07 20:02:57 +02001575 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001576
Vasily Tarasov91fac312007-04-25 12:29:51 +02001577 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001578 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001579}
1580
Divyesh Shah812df482010-04-08 21:15:35 -07001581static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1582{
1583 del_timer(&cfqd->idle_slice_timer);
Vivek Goyale98ef892010-06-18 10:39:47 -04001584 cfq_blkiocg_update_idle_time_stats(&cfqq->cfqg->blkg);
Divyesh Shah812df482010-04-08 21:15:35 -07001585}
1586
Jens Axboefebffd62008-01-28 13:19:43 +01001587static void __cfq_set_active_queue(struct cfq_data *cfqd,
1588 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001589{
1590 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001591 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1592 cfqd->serving_prio, cfqd->serving_type);
Vivek Goyale98ef892010-06-18 10:39:47 -04001593 cfq_blkiocg_update_avg_queue_size_stats(&cfqq->cfqg->blkg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001594 cfqq->slice_start = 0;
1595 cfqq->dispatch_start = jiffies;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001596 cfqq->allocated_slice = 0;
Jens Axboe22e2c502005-06-27 10:55:12 +02001597 cfqq->slice_end = 0;
Jens Axboe2f5cb732009-04-07 08:51:19 +02001598 cfqq->slice_dispatch = 0;
1599
Jens Axboe2f5cb732009-04-07 08:51:19 +02001600 cfq_clear_cfqq_wait_request(cfqq);
Jens Axboeb0291952009-04-07 11:38:31 +02001601 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001602 cfq_clear_cfqq_must_alloc_slice(cfqq);
1603 cfq_clear_cfqq_fifo_expire(cfqq);
Jens Axboe44f7c162007-01-19 11:51:58 +11001604 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02001605
Divyesh Shah812df482010-04-08 21:15:35 -07001606 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001607 }
1608
1609 cfqd->active_queue = cfqq;
1610}
1611
1612/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001613 * current cfqq expired its slice (or was too idle), select new one
1614 */
1615static void
1616__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001617 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001618{
Jens Axboe7b679132008-05-30 12:23:07 +02001619 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1620
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001621 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07001622 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001623
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001624 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001625 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001626
1627 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01001628 * If this cfqq is shared between multiple processes, check to
1629 * make sure that those processes are still issuing I/Os within
1630 * the mean seek distance. If not, it may be time to break the
1631 * queues apart again.
1632 */
1633 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1634 cfq_mark_cfqq_split_coop(cfqq);
1635
1636 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001637 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001638 */
Jens Axboe7b679132008-05-30 12:23:07 +02001639 if (timed_out && !cfq_cfqq_slice_new(cfqq)) {
Jens Axboec5b680f2007-01-19 11:56:49 +11001640 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001641 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1642 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001643
Vivek Goyale5ff0822010-04-26 19:25:11 +02001644 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001645
Vivek Goyalf04a6422009-12-03 12:59:40 -05001646 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1647 cfq_del_cfqq_rr(cfqd, cfqq);
1648
Jens Axboeedd75ff2007-04-19 12:03:34 +02001649 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001650
1651 if (cfqq == cfqd->active_queue)
1652 cfqd->active_queue = NULL;
1653
Vivek Goyaldae739e2009-12-03 12:59:45 -05001654 if (&cfqq->cfqg->rb_node == cfqd->grp_service_tree.active)
1655 cfqd->grp_service_tree.active = NULL;
1656
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001657 if (cfqd->active_cic) {
1658 put_io_context(cfqd->active_cic->ioc);
1659 cfqd->active_cic = NULL;
1660 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001661}
1662
Vivek Goyale5ff0822010-04-26 19:25:11 +02001663static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001664{
1665 struct cfq_queue *cfqq = cfqd->active_queue;
1666
1667 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02001668 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001669}
1670
Jens Axboe498d3aa22007-04-26 12:54:48 +02001671/*
1672 * Get next queue for service. Unless we have a queue preemption,
1673 * we'll simply select the first cfqq in the service tree.
1674 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001675static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001676{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001677 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001678 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05001679 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001680
Vivek Goyalf04a6422009-12-03 12:59:40 -05001681 if (!cfqd->rq_queued)
1682 return NULL;
1683
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001684 /* There is nothing to dispatch */
1685 if (!service_tree)
1686 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001687 if (RB_EMPTY_ROOT(&service_tree->rb))
1688 return NULL;
1689 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001690}
1691
Vivek Goyalf04a6422009-12-03 12:59:40 -05001692static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1693{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001694 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001695 struct cfq_queue *cfqq;
1696 int i, j;
1697 struct cfq_rb_root *st;
1698
1699 if (!cfqd->rq_queued)
1700 return NULL;
1701
Vivek Goyal25fb5162009-12-03 12:59:46 -05001702 cfqg = cfq_get_next_cfqg(cfqd);
1703 if (!cfqg)
1704 return NULL;
1705
Vivek Goyalf04a6422009-12-03 12:59:40 -05001706 for_each_cfqg_st(cfqg, i, j, st)
1707 if ((cfqq = cfq_rb_first(st)) != NULL)
1708 return cfqq;
1709 return NULL;
1710}
1711
Jens Axboe498d3aa22007-04-26 12:54:48 +02001712/*
1713 * Get and set a new active queue for service.
1714 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001715static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
1716 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001717{
Jens Axboee00ef792009-11-04 08:54:55 +01001718 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001719 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02001720
Jens Axboe22e2c502005-06-27 10:55:12 +02001721 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02001722 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02001723}
1724
Jens Axboed9e76202007-04-20 14:27:50 +02001725static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
1726 struct request *rq)
1727{
Tejun Heo83096eb2009-05-07 22:24:39 +09001728 if (blk_rq_pos(rq) >= cfqd->last_position)
1729 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02001730 else
Tejun Heo83096eb2009-05-07 22:24:39 +09001731 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02001732}
1733
Jeff Moyerb2c18e12009-10-23 17:14:49 -04001734static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01001735 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001736{
Shaohua Lie9ce3352010-03-19 08:03:04 +01001737 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02001738}
1739
Jens Axboea36e71f2009-04-15 12:15:11 +02001740static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
1741 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001742{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001743 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02001744 struct rb_node *parent, *node;
1745 struct cfq_queue *__cfqq;
1746 sector_t sector = cfqd->last_position;
1747
1748 if (RB_EMPTY_ROOT(root))
1749 return NULL;
1750
1751 /*
1752 * First, if we find a request starting at the end of the last
1753 * request, choose it.
1754 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001755 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02001756 if (__cfqq)
1757 return __cfqq;
1758
1759 /*
1760 * If the exact sector wasn't found, the parent of the NULL leaf
1761 * will contain the closest sector.
1762 */
1763 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001764 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001765 return __cfqq;
1766
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001767 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02001768 node = rb_next(&__cfqq->p_node);
1769 else
1770 node = rb_prev(&__cfqq->p_node);
1771 if (!node)
1772 return NULL;
1773
1774 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01001775 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001776 return __cfqq;
1777
1778 return NULL;
1779}
1780
1781/*
1782 * cfqd - obvious
1783 * cur_cfqq - passed in so that we don't decide that the current queue is
1784 * closely cooperating with itself.
1785 *
1786 * So, basically we're assuming that that cur_cfqq has dispatched at least
1787 * one request, and that cfqd->last_position reflects a position on the disk
1788 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
1789 * assumption.
1790 */
1791static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04001792 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02001793{
1794 struct cfq_queue *cfqq;
1795
Divyesh Shah39c01b22010-03-25 15:45:57 +01001796 if (cfq_class_idle(cur_cfqq))
1797 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001798 if (!cfq_cfqq_sync(cur_cfqq))
1799 return NULL;
1800 if (CFQQ_SEEKY(cur_cfqq))
1801 return NULL;
1802
Jens Axboea36e71f2009-04-15 12:15:11 +02001803 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01001804 * Don't search priority tree if it's the only queue in the group.
1805 */
1806 if (cur_cfqq->cfqg->nr_cfqq == 1)
1807 return NULL;
1808
1809 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001810 * We should notice if some of the queues are cooperating, eg
1811 * working closely on the same area of the disk. In that case,
1812 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02001813 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001814 cfqq = cfqq_close(cfqd, cur_cfqq);
1815 if (!cfqq)
1816 return NULL;
1817
Vivek Goyal8682e1f2009-12-03 12:59:50 -05001818 /* If new queue belongs to different cfq_group, don't choose it */
1819 if (cur_cfqq->cfqg != cfqq->cfqg)
1820 return NULL;
1821
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001822 /*
1823 * It only makes sense to merge sync queues.
1824 */
1825 if (!cfq_cfqq_sync(cfqq))
1826 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001827 if (CFQQ_SEEKY(cfqq))
1828 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001829
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001830 /*
1831 * Do not merge queues of different priority classes
1832 */
1833 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
1834 return NULL;
1835
Jens Axboea36e71f2009-04-15 12:15:11 +02001836 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02001837}
1838
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001839/*
1840 * Determine whether we should enforce idle window for this queue.
1841 */
1842
1843static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1844{
1845 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01001846 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001847
Vivek Goyalf04a6422009-12-03 12:59:40 -05001848 BUG_ON(!service_tree);
1849 BUG_ON(!service_tree->count);
1850
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001851 /* We never do for idle class queues. */
1852 if (prio == IDLE_WORKLOAD)
1853 return false;
1854
1855 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01001856 if (cfq_cfqq_idle_window(cfqq) &&
1857 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001858 return true;
1859
1860 /*
1861 * Otherwise, we do only if they are the last ones
1862 * in their service tree.
1863 */
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001864 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq))
1865 return 1;
1866 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
1867 service_tree->count);
1868 return 0;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001869}
1870
Jens Axboe6d048f52007-04-25 12:44:27 +02001871static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001872{
Jens Axboe17926692007-01-19 11:59:30 +11001873 struct cfq_queue *cfqq = cfqd->active_queue;
Jens Axboe206dc692006-03-28 13:03:44 +02001874 struct cfq_io_context *cic;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001875 unsigned long sl;
1876
Jens Axboea68bbdd2008-09-24 13:03:33 +02001877 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001878 * SSD device without seek penalty, disable idling. But only do so
1879 * for devices that support queuing, otherwise we still have a problem
1880 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02001881 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02001882 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02001883 return;
1884
Jens Axboedd67d052006-06-21 09:36:18 +02001885 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02001886 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02001887
1888 /*
1889 * idle is disabled, either manually or by past process history
1890 */
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01001891 if (!cfqd->cfq_slice_idle || !cfq_should_idle(cfqd, cfqq))
Jens Axboe6d048f52007-04-25 12:44:27 +02001892 return;
1893
Jens Axboe22e2c502005-06-27 10:55:12 +02001894 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001895 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02001896 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01001897 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02001898 return;
1899
1900 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02001901 * task has exited, don't wait
1902 */
Jens Axboe206dc692006-03-28 13:03:44 +02001903 cic = cfqd->active_cic;
Nikanth Karthikesan66dac982007-11-27 12:47:04 +01001904 if (!cic || !atomic_read(&cic->ioc->nr_tasks))
Jens Axboe6d048f52007-04-25 12:44:27 +02001905 return;
1906
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001907 /*
1908 * If our average think time is larger than the remaining time
1909 * slice, then don't idle. This avoids overrunning the allotted
1910 * time slice.
1911 */
1912 if (sample_valid(cic->ttime_samples) &&
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001913 (cfqq->slice_end - jiffies < cic->ttime_mean)) {
1914 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%d",
1915 cic->ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001916 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001917 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02001918
Jens Axboe3b181522005-06-27 10:56:24 +02001919 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001920
Jens Axboe6d048f52007-04-25 12:44:27 +02001921 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02001922
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001923 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Vivek Goyale98ef892010-06-18 10:39:47 -04001924 cfq_blkiocg_update_set_idle_time_stats(&cfqq->cfqg->blkg);
Jens Axboe9481ffd2009-04-15 12:14:13 +02001925 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu", sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Jens Axboe498d3aa22007-04-26 12:54:48 +02001928/*
1929 * Move request from internal lists to the request queue dispatch list.
1930 */
Jens Axboe165125e2007-07-24 09:28:11 +02001931static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Jens Axboe3ed9a292007-04-23 08:33:33 +02001933 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02001934 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001935
Jens Axboe7b679132008-05-30 12:23:07 +02001936 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
1937
Jeff Moyer06d21882009-09-11 17:08:59 +02001938 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02001939 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001940 cfqq->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02001941 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02001942
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001943 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyale98ef892010-06-18 10:39:47 -04001944 cfq_blkiocg_update_dispatch_stats(&cfqq->cfqg->blkg, blk_rq_bytes(rq),
Divyesh Shah84c124d2010-04-09 08:31:19 +02001945 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948/*
1949 * return expired entry, or NULL to just start from scratch in rbtree
1950 */
Jens Axboefebffd62008-01-28 13:19:43 +01001951static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Jens Axboe30996f42009-10-05 11:03:39 +02001953 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Jens Axboe3b181522005-06-27 10:56:24 +02001955 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11001957
1958 cfq_mark_cfqq_fifo_expire(cfqq);
1959
Jens Axboe89850f72006-07-22 16:48:31 +02001960 if (list_empty(&cfqq->fifo))
1961 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Jens Axboe89850f72006-07-22 16:48:31 +02001963 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02001964 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02001965 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Jens Axboe30996f42009-10-05 11:03:39 +02001967 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02001968 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
Jens Axboe22e2c502005-06-27 10:55:12 +02001971static inline int
1972cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1973{
1974 const int base_rq = cfqd->cfq_slice_async_rq;
1975
1976 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
1977
1978 return 2 * (base_rq + base_rq * (CFQ_PRIO_LISTS - 1 - cfqq->ioprio));
1979}
1980
1981/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001982 * Must be called with the queue_lock held.
1983 */
1984static int cfqq_process_refs(struct cfq_queue *cfqq)
1985{
1986 int process_refs, io_refs;
1987
1988 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
1989 process_refs = atomic_read(&cfqq->ref) - io_refs;
1990 BUG_ON(process_refs < 0);
1991 return process_refs;
1992}
1993
1994static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
1995{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04001996 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04001997 struct cfq_queue *__cfqq;
1998
Jeff Moyerc10b61f2010-06-17 10:19:11 -04001999 /*
2000 * If there are no process references on the new_cfqq, then it is
2001 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2002 * chain may have dropped their last reference (not just their
2003 * last process reference).
2004 */
2005 if (!cfqq_process_refs(new_cfqq))
2006 return;
2007
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002008 /* Avoid a circular list and skip interim queue merges */
2009 while ((__cfqq = new_cfqq->new_cfqq)) {
2010 if (__cfqq == cfqq)
2011 return;
2012 new_cfqq = __cfqq;
2013 }
2014
2015 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002016 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002017 /*
2018 * If the process for the cfqq has gone away, there is no
2019 * sense in merging the queues.
2020 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002021 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002022 return;
2023
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002024 /*
2025 * Merge in the direction of the lesser amount of work.
2026 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002027 if (new_process_refs >= process_refs) {
2028 cfqq->new_cfqq = new_cfqq;
2029 atomic_add(process_refs, &new_cfqq->ref);
2030 } else {
2031 new_cfqq->new_cfqq = cfqq;
2032 atomic_add(new_process_refs, &cfqq->ref);
2033 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002034}
2035
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002036static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002037 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002038{
2039 struct cfq_queue *queue;
2040 int i;
2041 bool key_valid = false;
2042 unsigned long lowest_key = 0;
2043 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2044
Vivek Goyal65b32a52009-12-16 17:52:59 -05002045 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2046 /* select the one with lowest rb_key */
2047 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002048 if (queue &&
2049 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2050 lowest_key = queue->rb_key;
2051 cur_best = i;
2052 key_valid = true;
2053 }
2054 }
2055
2056 return cur_best;
2057}
2058
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002059static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002060{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002061 unsigned slice;
2062 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002063 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002064 unsigned group_slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002065
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002066 if (!cfqg) {
2067 cfqd->serving_prio = IDLE_WORKLOAD;
2068 cfqd->workload_expires = jiffies + 1;
2069 return;
2070 }
2071
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002072 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002073 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002074 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002075 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002076 cfqd->serving_prio = BE_WORKLOAD;
2077 else {
2078 cfqd->serving_prio = IDLE_WORKLOAD;
2079 cfqd->workload_expires = jiffies + 1;
2080 return;
2081 }
2082
2083 /*
2084 * For RT and BE, we have to choose also the type
2085 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2086 * expiration time
2087 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002088 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002089 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002090
2091 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002092 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002093 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002094 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002095 return;
2096
2097 /* otherwise select new workload type */
2098 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002099 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2100 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002101 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002102
2103 /*
2104 * the workload slice is computed as a fraction of target latency
2105 * proportional to the number of queues in that workload, over
2106 * all the queues in the same priority class
2107 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002108 group_slice = cfq_group_slice(cfqd, cfqg);
2109
2110 slice = group_slice * count /
2111 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2112 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002113
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002114 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2115 unsigned int tmp;
2116
2117 /*
2118 * Async queues are currently system wide. Just taking
2119 * proportion of queues with-in same group will lead to higher
2120 * async ratio system wide as generally root group is going
2121 * to have higher weight. A more accurate thing would be to
2122 * calculate system wide asnc/sync ratio.
2123 */
2124 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2125 tmp = tmp/cfqd->busy_queues;
2126 slice = min_t(unsigned, slice, tmp);
2127
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002128 /* async workload slice is scaled down according to
2129 * the sync/async slice ratio. */
2130 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002131 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002132 /* sync workload slice is at least 2 * cfq_slice_idle */
2133 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2134
2135 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002136 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002137 cfqd->workload_expires = jiffies + slice;
2138}
2139
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002140static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2141{
2142 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002143 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002144
2145 if (RB_EMPTY_ROOT(&st->rb))
2146 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002147 cfqg = cfq_rb_first_group(st);
2148 st->active = &cfqg->rb_node;
2149 update_min_vdisktime(st);
2150 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002151}
2152
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002153static void cfq_choose_cfqg(struct cfq_data *cfqd)
2154{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002155 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2156
2157 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002158
2159 /* Restore the workload type data */
2160 if (cfqg->saved_workload_slice) {
2161 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2162 cfqd->serving_type = cfqg->saved_workload;
2163 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002164 } else
2165 cfqd->workload_expires = jiffies - 1;
2166
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002167 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002168}
2169
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002170/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002171 * Select a queue for service. If we have a current active queue,
2172 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002173 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002174static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002175{
Jens Axboea36e71f2009-04-15 12:15:11 +02002176 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002177
2178 cfqq = cfqd->active_queue;
2179 if (!cfqq)
2180 goto new_queue;
2181
Vivek Goyalf04a6422009-12-03 12:59:40 -05002182 if (!cfqd->rq_queued)
2183 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002184
2185 /*
2186 * We were waiting for group to get backlogged. Expire the queue
2187 */
2188 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2189 goto expire;
2190
Jens Axboe22e2c502005-06-27 10:55:12 +02002191 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002192 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002193 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002194 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2195 /*
2196 * If slice had not expired at the completion of last request
2197 * we might not have turned on wait_busy flag. Don't expire
2198 * the queue yet. Allow the group to get backlogged.
2199 *
2200 * The very fact that we have used the slice, that means we
2201 * have been idling all along on this queue and it should be
2202 * ok to wait for this request to complete.
2203 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002204 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2205 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2206 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002207 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002208 } else
Vivek Goyal7667aa02009-12-08 17:52:58 -05002209 goto expire;
2210 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002211
2212 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002213 * The active queue has requests and isn't expired, allow it to
2214 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002215 */
Jens Axboedd67d052006-06-21 09:36:18 +02002216 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002217 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002218
2219 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002220 * If another queue has a request waiting within our mean seek
2221 * distance, let it run. The expire code will check for close
2222 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002223 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002224 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002225 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002226 if (new_cfqq) {
2227 if (!cfqq->new_cfqq)
2228 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002229 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002230 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002231
2232 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002233 * No requests pending. If the active queue still has requests in
2234 * flight or is idling for a new request, allow either of these
2235 * conditions to happen (or time out) before selecting a new queue.
2236 */
Jens Axboecc197472007-04-20 20:45:39 +02002237 if (timer_pending(&cfqd->idle_slice_timer) ||
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002238 (cfqq->dispatched && cfq_should_idle(cfqd, cfqq))) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002239 cfqq = NULL;
2240 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002241 }
2242
Jens Axboe3b181522005-06-27 10:56:24 +02002243expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002244 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002245new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002246 /*
2247 * Current queue expired. Check if we have to switch to a new
2248 * service tree
2249 */
2250 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002251 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002252
Jens Axboea36e71f2009-04-15 12:15:11 +02002253 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002254keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002255 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002256}
2257
Jens Axboefebffd62008-01-28 13:19:43 +01002258static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002259{
2260 int dispatched = 0;
2261
2262 while (cfqq->next_rq) {
2263 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2264 dispatched++;
2265 }
2266
2267 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002268
2269 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002270 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002271 return dispatched;
2272}
2273
Jens Axboe498d3aa22007-04-26 12:54:48 +02002274/*
2275 * Drain our current requests. Used for barriers and when switching
2276 * io schedulers on-the-fly.
2277 */
Jens Axboed9e76202007-04-20 14:27:50 +02002278static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002279{
Jens Axboe08717142008-01-28 11:38:15 +01002280 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002281 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002282
Divyesh Shah3440c492010-04-09 09:29:57 +02002283 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002284 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002285 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2286 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002287 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002288 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002289
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002290 BUG_ON(cfqd->busy_queues);
2291
Jeff Moyer69237152009-06-12 15:29:30 +02002292 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002293 return dispatched;
2294}
2295
Shaohua Liabc3c742010-03-01 09:20:54 +01002296static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2297 struct cfq_queue *cfqq)
2298{
2299 /* the queue hasn't finished any request, can't estimate */
2300 if (cfq_cfqq_slice_new(cfqq))
2301 return 1;
2302 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2303 cfqq->slice_end))
2304 return 1;
2305
2306 return 0;
2307}
2308
Jens Axboe0b182d62009-10-06 20:49:37 +02002309static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002310{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002311 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jens Axboe2f5cb732009-04-07 08:51:19 +02002313 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002314 * Drain async requests before we start sync IO
2315 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002316 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002317 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002318
2319 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002320 * If this is an async queue and we have sync IO in flight, let it wait
2321 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002322 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002323 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002324
Shaohua Liabc3c742010-03-01 09:20:54 +01002325 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002326 if (cfq_class_idle(cfqq))
2327 max_dispatch = 1;
2328
2329 /*
2330 * Does this cfqq already have too much IO in flight?
2331 */
2332 if (cfqq->dispatched >= max_dispatch) {
2333 /*
2334 * idle queue must always only have a single IO in flight
2335 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002336 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002337 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002338
Jens Axboe2f5cb732009-04-07 08:51:19 +02002339 /*
2340 * We have other queues, don't allow more IO from this one
2341 */
Shaohua Liabc3c742010-03-01 09:20:54 +01002342 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002343 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002344
Jens Axboe2f5cb732009-04-07 08:51:19 +02002345 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002346 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002347 */
Shaohua Liabc3c742010-03-01 09:20:54 +01002348 if (cfqd->busy_queues == 1)
2349 max_dispatch = -1;
2350 else
2351 /*
2352 * Normally we start throttling cfqq when cfq_quantum/2
2353 * requests have been dispatched. But we can drive
2354 * deeper queue depths at the beginning of slice
2355 * subjected to upper limit of cfq_quantum.
2356 * */
2357 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002358 }
2359
2360 /*
2361 * Async queues must wait a bit before being allowed dispatch.
2362 * We also ramp up the dispatch depth gradually for async IO,
2363 * based on the last sync IO we serviced
2364 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002365 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002366 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002367 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002368
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002369 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002370 if (!depth && !cfqq->dispatched)
2371 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002372 if (depth < max_dispatch)
2373 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Jens Axboe0b182d62009-10-06 20:49:37 +02002376 /*
2377 * If we're below the current max, allow a dispatch
2378 */
2379 return cfqq->dispatched < max_dispatch;
2380}
2381
2382/*
2383 * Dispatch a request from cfqq, moving them to the request queue
2384 * dispatch list.
2385 */
2386static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2387{
2388 struct request *rq;
2389
2390 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2391
2392 if (!cfq_may_dispatch(cfqd, cfqq))
2393 return false;
2394
2395 /*
2396 * follow expired path, else get first next available
2397 */
2398 rq = cfq_check_fifo(cfqq);
2399 if (!rq)
2400 rq = cfqq->next_rq;
2401
2402 /*
2403 * insert request into driver dispatch list
2404 */
2405 cfq_dispatch_insert(cfqd->queue, rq);
2406
2407 if (!cfqd->active_cic) {
2408 struct cfq_io_context *cic = RQ_CIC(rq);
2409
2410 atomic_long_inc(&cic->ioc->refcount);
2411 cfqd->active_cic = cic;
2412 }
2413
2414 return true;
2415}
2416
2417/*
2418 * Find the cfqq that we need to service and move a request from that to the
2419 * dispatch list
2420 */
2421static int cfq_dispatch_requests(struct request_queue *q, int force)
2422{
2423 struct cfq_data *cfqd = q->elevator->elevator_data;
2424 struct cfq_queue *cfqq;
2425
2426 if (!cfqd->busy_queues)
2427 return 0;
2428
2429 if (unlikely(force))
2430 return cfq_forced_dispatch(cfqd);
2431
2432 cfqq = cfq_select_queue(cfqd);
2433 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002434 return 0;
2435
Jens Axboe2f5cb732009-04-07 08:51:19 +02002436 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002437 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002438 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002439 if (!cfq_dispatch_request(cfqd, cfqq))
2440 return 0;
2441
Jens Axboe2f5cb732009-04-07 08:51:19 +02002442 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002443 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002444
2445 /*
2446 * expire an async queue immediately if it has used up its slice. idle
2447 * queue always expire after 1 dispatch round.
2448 */
2449 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2450 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2451 cfq_class_idle(cfqq))) {
2452 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002453 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002454 }
2455
Shan Weib217a902009-09-01 10:06:42 +02002456 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002457 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460/*
Jens Axboe5e705372006-07-13 12:39:25 +02002461 * task holds one reference to the queue, dropped when task exits. each rq
2462 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002464 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 * queue lock must be held here.
2466 */
2467static void cfq_put_queue(struct cfq_queue *cfqq)
2468{
Jens Axboe22e2c502005-06-27 10:55:12 +02002469 struct cfq_data *cfqd = cfqq->cfqd;
Vivek Goyal878eadd2009-12-07 19:37:15 +01002470 struct cfq_group *cfqg, *orig_cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002471
2472 BUG_ON(atomic_read(&cfqq->ref) <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 if (!atomic_dec_and_test(&cfqq->ref))
2475 return;
2476
Jens Axboe7b679132008-05-30 12:23:07 +02002477 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002479 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002480 cfqg = cfqq->cfqg;
Vivek Goyal878eadd2009-12-07 19:37:15 +01002481 orig_cfqg = cfqq->orig_cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002483 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002484 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002485 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002486 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002487
Vivek Goyalf04a6422009-12-03 12:59:40 -05002488 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 kmem_cache_free(cfq_pool, cfqq);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002490 cfq_put_cfqg(cfqg);
Vivek Goyal878eadd2009-12-07 19:37:15 +01002491 if (orig_cfqg)
2492 cfq_put_cfqg(orig_cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
Jens Axboed6de8be2008-05-28 14:46:59 +02002495/*
2496 * Must always be called with the rcu_read_lock() held
2497 */
Jens Axboe07416d22008-05-07 09:17:12 +02002498static void
2499__call_for_each_cic(struct io_context *ioc,
2500 void (*func)(struct io_context *, struct cfq_io_context *))
2501{
2502 struct cfq_io_context *cic;
2503 struct hlist_node *n;
2504
2505 hlist_for_each_entry_rcu(cic, n, &ioc->cic_list, cic_list)
2506 func(ioc, cic);
2507}
2508
Jens Axboe4ac845a2008-01-24 08:44:49 +01002509/*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002510 * Call func for each cic attached to this ioc.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002511 */
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002512static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002513call_for_each_cic(struct io_context *ioc,
2514 void (*func)(struct io_context *, struct cfq_io_context *))
2515{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002516 rcu_read_lock();
Jens Axboe07416d22008-05-07 09:17:12 +02002517 __call_for_each_cic(ioc, func);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002518 rcu_read_unlock();
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002519}
Jens Axboe4ac845a2008-01-24 08:44:49 +01002520
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002521static void cfq_cic_free_rcu(struct rcu_head *head)
2522{
2523 struct cfq_io_context *cic;
2524
2525 cic = container_of(head, struct cfq_io_context, rcu_head);
2526
2527 kmem_cache_free(cfq_ioc_pool, cic);
Tejun Heo245b2e72009-06-24 15:13:48 +09002528 elv_ioc_count_dec(cfq_ioc_count);
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002529
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002530 if (ioc_gone) {
2531 /*
2532 * CFQ scheduler is exiting, grab exit lock and check
2533 * the pending io context count. If it hits zero,
2534 * complete ioc_gone and set it back to NULL
2535 */
2536 spin_lock(&ioc_gone_lock);
Tejun Heo245b2e72009-06-24 15:13:48 +09002537 if (ioc_gone && !elv_ioc_count_read(cfq_ioc_count)) {
Jens Axboe9a11b4e2008-05-29 09:32:08 +02002538 complete(ioc_gone);
2539 ioc_gone = NULL;
2540 }
2541 spin_unlock(&ioc_gone_lock);
2542 }
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002543}
2544
2545static void cfq_cic_free(struct cfq_io_context *cic)
2546{
2547 call_rcu(&cic->rcu_head, cfq_cic_free_rcu);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002548}
2549
2550static void cic_free_func(struct io_context *ioc, struct cfq_io_context *cic)
2551{
2552 unsigned long flags;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002553 unsigned long dead_key = (unsigned long) cic->key;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002554
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002555 BUG_ON(!(dead_key & CIC_DEAD_KEY));
Jens Axboe4ac845a2008-01-24 08:44:49 +01002556
2557 spin_lock_irqsave(&ioc->lock, flags);
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04002558 radix_tree_delete(&ioc->radix_root, dead_key >> CIC_DEAD_INDEX_SHIFT);
Jens Axboeffc4e752008-02-19 10:02:29 +01002559 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002560 spin_unlock_irqrestore(&ioc->lock, flags);
2561
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002562 cfq_cic_free(cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002563}
2564
Jens Axboed6de8be2008-05-28 14:46:59 +02002565/*
2566 * Must be called with rcu_read_lock() held or preemption otherwise disabled.
2567 * Only two callers of this - ->dtor() which is called with the rcu_read_lock(),
2568 * and ->trim() which is called with the task lock held
2569 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002570static void cfq_free_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002572 /*
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02002573 * ioc->refcount is zero here, or we are called from elv_unregister(),
2574 * so no more cic's are allowed to be linked into this ioc. So it
2575 * should be ok to iterate over the known list, we will see all cic's
2576 * since no new ones are added.
Jens Axboe4ac845a2008-01-24 08:44:49 +01002577 */
Jens Axboe07416d22008-05-07 09:17:12 +02002578 __call_for_each_cic(ioc, cic_free_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Shaohua Lid02a2c02010-05-25 10:16:53 +02002581static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002582{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002583 struct cfq_queue *__cfqq, *next;
2584
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002585 /*
2586 * If this queue was scheduled to merge with another queue, be
2587 * sure to drop the reference taken on that queue (and others in
2588 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2589 */
2590 __cfqq = cfqq->new_cfqq;
2591 while (__cfqq) {
2592 if (__cfqq == cfqq) {
2593 WARN(1, "cfqq->new_cfqq loop detected\n");
2594 break;
2595 }
2596 next = __cfqq->new_cfqq;
2597 cfq_put_queue(__cfqq);
2598 __cfqq = next;
2599 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02002600}
2601
2602static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2603{
2604 if (unlikely(cfqq == cfqd->active_queue)) {
2605 __cfq_slice_expired(cfqd, cfqq, 0);
2606 cfq_schedule_dispatch(cfqd);
2607 }
2608
2609 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002610
Jens Axboe89850f72006-07-22 16:48:31 +02002611 cfq_put_queue(cfqq);
2612}
2613
2614static void __cfq_exit_single_io_context(struct cfq_data *cfqd,
2615 struct cfq_io_context *cic)
2616{
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002617 struct io_context *ioc = cic->ioc;
2618
Jens Axboefc463792006-08-29 09:05:44 +02002619 list_del_init(&cic->queue_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002620
2621 /*
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002622 * Make sure dead mark is seen for dead queues
Jens Axboe4ac845a2008-01-24 08:44:49 +01002623 */
Jens Axboefc463792006-08-29 09:05:44 +02002624 smp_wmb();
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002625 cic->key = cfqd_dead_key(cfqd);
Jens Axboefc463792006-08-29 09:05:44 +02002626
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002627 if (ioc->ioc_data == cic)
2628 rcu_assign_pointer(ioc->ioc_data, NULL);
2629
Jens Axboeff6657c2009-04-08 10:58:57 +02002630 if (cic->cfqq[BLK_RW_ASYNC]) {
2631 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2632 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002633 }
2634
Jens Axboeff6657c2009-04-08 10:58:57 +02002635 if (cic->cfqq[BLK_RW_SYNC]) {
2636 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2637 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002638 }
Jens Axboe89850f72006-07-22 16:48:31 +02002639}
2640
Jens Axboe4ac845a2008-01-24 08:44:49 +01002641static void cfq_exit_single_io_context(struct io_context *ioc,
2642 struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002643{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002644 struct cfq_data *cfqd = cic_to_cfqd(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02002645
Jens Axboe89850f72006-07-22 16:48:31 +02002646 if (cfqd) {
Jens Axboe165125e2007-07-24 09:28:11 +02002647 struct request_queue *q = cfqd->queue;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002648 unsigned long flags;
Jens Axboe22e2c502005-06-27 10:55:12 +02002649
Jens Axboe4ac845a2008-01-24 08:44:49 +01002650 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe62c1fe92008-12-15 21:19:25 +01002651
2652 /*
2653 * Ensure we get a fresh copy of the ->key to prevent
2654 * race between exiting task and queue
2655 */
2656 smp_read_barrier_depends();
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002657 if (cic->key == cfqd)
Jens Axboe62c1fe92008-12-15 21:19:25 +01002658 __cfq_exit_single_io_context(cfqd, cic);
2659
Jens Axboe4ac845a2008-01-24 08:44:49 +01002660 spin_unlock_irqrestore(q->queue_lock, flags);
Al Viro12a05732006-03-18 13:38:01 -05002661 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002662}
2663
Jens Axboe498d3aa22007-04-26 12:54:48 +02002664/*
2665 * The process that ioc belongs to has exited, we need to clean up
2666 * and put the internal structures we have that belongs to that process.
2667 */
Jens Axboee2d74ac2006-03-28 08:59:01 +02002668static void cfq_exit_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002670 call_for_each_cic(ioc, cfq_exit_single_io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672
Jens Axboe22e2c502005-06-27 10:55:12 +02002673static struct cfq_io_context *
Al Viro8267e262005-10-21 03:20:53 -04002674cfq_alloc_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Jens Axboeb5deef92006-07-19 23:39:40 +02002676 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Christoph Lameter94f60302007-07-17 04:03:29 -07002678 cic = kmem_cache_alloc_node(cfq_ioc_pool, gfp_mask | __GFP_ZERO,
2679 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (cic) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002681 cic->last_end_request = jiffies;
Jens Axboe553698f2006-06-14 19:11:57 +02002682 INIT_LIST_HEAD(&cic->queue_list);
Jens Axboeffc4e752008-02-19 10:02:29 +01002683 INIT_HLIST_NODE(&cic->cic_list);
Jens Axboe22e2c502005-06-27 10:55:12 +02002684 cic->dtor = cfq_free_io_context;
2685 cic->exit = cfq_exit_io_context;
Tejun Heo245b2e72009-06-24 15:13:48 +09002686 elv_ioc_count_inc(cfq_ioc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688
2689 return cic;
2690}
2691
Jens Axboefd0928d2008-01-24 08:52:45 +01002692static void cfq_init_prio_data(struct cfq_queue *cfqq, struct io_context *ioc)
Jens Axboe22e2c502005-06-27 10:55:12 +02002693{
2694 struct task_struct *tsk = current;
2695 int ioprio_class;
2696
Jens Axboe3b181522005-06-27 10:56:24 +02002697 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002698 return;
2699
Jens Axboefd0928d2008-01-24 08:52:45 +01002700 ioprio_class = IOPRIO_PRIO_CLASS(ioc->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002701 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002702 default:
2703 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2704 case IOPRIO_CLASS_NONE:
2705 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002706 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002707 */
2708 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002709 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002710 break;
2711 case IOPRIO_CLASS_RT:
2712 cfqq->ioprio = task_ioprio(ioc);
2713 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2714 break;
2715 case IOPRIO_CLASS_BE:
2716 cfqq->ioprio = task_ioprio(ioc);
2717 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2718 break;
2719 case IOPRIO_CLASS_IDLE:
2720 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2721 cfqq->ioprio = 7;
2722 cfq_clear_cfqq_idle_window(cfqq);
2723 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002724 }
2725
2726 /*
2727 * keep track of original prio settings in case we have to temporarily
2728 * elevate the priority of this queue
2729 */
2730 cfqq->org_ioprio = cfqq->ioprio;
2731 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02002732 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002733}
2734
Jens Axboefebffd62008-01-28 13:19:43 +01002735static void changed_ioprio(struct io_context *ioc, struct cfq_io_context *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002736{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002737 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05002738 struct cfq_queue *cfqq;
Jens Axboec1b707d2006-10-30 19:54:23 +01002739 unsigned long flags;
Jens Axboe35e60772006-06-14 09:10:45 +02002740
Jens Axboecaaa5f92006-06-16 11:23:00 +02002741 if (unlikely(!cfqd))
2742 return;
2743
Jens Axboec1b707d2006-10-30 19:54:23 +01002744 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002745
Jens Axboeff6657c2009-04-08 10:58:57 +02002746 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002747 if (cfqq) {
2748 struct cfq_queue *new_cfqq;
Jens Axboeff6657c2009-04-08 10:58:57 +02002749 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic->ioc,
2750 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002751 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002752 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002753 cfq_put_queue(cfqq);
2754 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002755 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002756
Jens Axboeff6657c2009-04-08 10:58:57 +02002757 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002758 if (cfqq)
2759 cfq_mark_cfqq_prio_changed(cfqq);
2760
Jens Axboec1b707d2006-10-30 19:54:23 +01002761 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02002762}
2763
Jens Axboefc463792006-08-29 09:05:44 +02002764static void cfq_ioc_set_ioprio(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002766 call_for_each_cic(ioc, changed_ioprio);
Jens Axboefc463792006-08-29 09:05:44 +02002767 ioc->ioprio_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
Jens Axboed5036d72009-06-26 10:44:34 +02002770static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002771 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002772{
2773 RB_CLEAR_NODE(&cfqq->rb_node);
2774 RB_CLEAR_NODE(&cfqq->p_node);
2775 INIT_LIST_HEAD(&cfqq->fifo);
2776
2777 atomic_set(&cfqq->ref, 0);
2778 cfqq->cfqd = cfqd;
2779
2780 cfq_mark_cfqq_prio_changed(cfqq);
2781
2782 if (is_sync) {
2783 if (!cfq_class_idle(cfqq))
2784 cfq_mark_cfqq_idle_window(cfqq);
2785 cfq_mark_cfqq_sync(cfqq);
2786 }
2787 cfqq->pid = pid;
2788}
2789
Vivek Goyal246103332009-12-03 12:59:51 -05002790#ifdef CONFIG_CFQ_GROUP_IOSCHED
2791static void changed_cgroup(struct io_context *ioc, struct cfq_io_context *cic)
2792{
2793 struct cfq_queue *sync_cfqq = cic_to_cfqq(cic, 1);
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002794 struct cfq_data *cfqd = cic_to_cfqd(cic);
Vivek Goyal246103332009-12-03 12:59:51 -05002795 unsigned long flags;
2796 struct request_queue *q;
2797
2798 if (unlikely(!cfqd))
2799 return;
2800
2801 q = cfqd->queue;
2802
2803 spin_lock_irqsave(q->queue_lock, flags);
2804
2805 if (sync_cfqq) {
2806 /*
2807 * Drop reference to sync queue. A new sync queue will be
2808 * assigned in new group upon arrival of a fresh request.
2809 */
2810 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
2811 cic_set_cfqq(cic, NULL, 1);
2812 cfq_put_queue(sync_cfqq);
2813 }
2814
2815 spin_unlock_irqrestore(q->queue_lock, flags);
2816}
2817
2818static void cfq_ioc_set_cgroup(struct io_context *ioc)
2819{
2820 call_for_each_cic(ioc, changed_cgroup);
2821 ioc->cgroup_changed = 0;
2822}
2823#endif /* CONFIG_CFQ_GROUP_IOSCHED */
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002826cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync,
Jens Axboefd0928d2008-01-24 08:52:45 +01002827 struct io_context *ioc, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vasily Tarasov91fac312007-04-25 12:29:51 +02002830 struct cfq_io_context *cic;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002831 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833retry:
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002834 cfqg = cfq_get_cfqg(cfqd, 1);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002835 cic = cfq_cic_lookup(cfqd, ioc);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002836 /* cic always exists here */
2837 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Jens Axboe6118b702009-06-30 09:34:12 +02002839 /*
2840 * Always try a new alloc if we fell back to the OOM cfqq
2841 * originally, since it should just be a temporary situation.
2842 */
2843 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
2844 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (new_cfqq) {
2846 cfqq = new_cfqq;
2847 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002848 } else if (gfp_mask & __GFP_WAIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07002850 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02002851 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07002852 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02002854 if (new_cfqq)
2855 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02002856 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07002857 cfqq = kmem_cache_alloc_node(cfq_pool,
2858 gfp_mask | __GFP_ZERO,
2859 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02002860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Jens Axboe6118b702009-06-30 09:34:12 +02002862 if (cfqq) {
2863 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
2864 cfq_init_prio_data(cfqq, ioc);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002865 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02002866 cfq_log_cfqq(cfqd, cfqq, "alloced");
2867 } else
2868 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870
2871 if (new_cfqq)
2872 kmem_cache_free(cfq_pool, new_cfqq);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return cfqq;
2875}
2876
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002877static struct cfq_queue **
2878cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
2879{
Jens Axboefe094d92008-01-31 13:08:54 +01002880 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002881 case IOPRIO_CLASS_RT:
2882 return &cfqd->async_cfqq[0][ioprio];
2883 case IOPRIO_CLASS_BE:
2884 return &cfqd->async_cfqq[1][ioprio];
2885 case IOPRIO_CLASS_IDLE:
2886 return &cfqd->async_idle_cfqq;
2887 default:
2888 BUG();
2889 }
2890}
2891
Jens Axboe15c31be2007-07-10 13:43:25 +02002892static struct cfq_queue *
Jens Axboea6151c32009-10-07 20:02:57 +02002893cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct io_context *ioc,
Jens Axboe15c31be2007-07-10 13:43:25 +02002894 gfp_t gfp_mask)
2895{
Jens Axboefd0928d2008-01-24 08:52:45 +01002896 const int ioprio = task_ioprio(ioc);
2897 const int ioprio_class = task_ioprio_class(ioc);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002898 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02002899 struct cfq_queue *cfqq = NULL;
2900
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002901 if (!is_sync) {
2902 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
2903 cfqq = *async_cfqq;
2904 }
2905
Jens Axboe6118b702009-06-30 09:34:12 +02002906 if (!cfqq)
Jens Axboefd0928d2008-01-24 08:52:45 +01002907 cfqq = cfq_find_alloc_queue(cfqd, is_sync, ioc, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02002908
2909 /*
2910 * pin the queue now that it's allocated, scheduler exit will prune it
2911 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002912 if (!is_sync && !(*async_cfqq)) {
Jens Axboe15c31be2007-07-10 13:43:25 +02002913 atomic_inc(&cfqq->ref);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02002914 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02002915 }
2916
2917 atomic_inc(&cfqq->ref);
2918 return cfqq;
2919}
2920
Jens Axboe498d3aa22007-04-26 12:54:48 +02002921/*
2922 * We drop cfq io contexts lazily, so we may find a dead one.
2923 */
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002924static void
Jens Axboe4ac845a2008-01-24 08:44:49 +01002925cfq_drop_dead_cic(struct cfq_data *cfqd, struct io_context *ioc,
2926 struct cfq_io_context *cic)
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002927{
Jens Axboe4ac845a2008-01-24 08:44:49 +01002928 unsigned long flags;
2929
Jens Axboefc463792006-08-29 09:05:44 +02002930 WARN_ON(!list_empty(&cic->queue_list));
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002931 BUG_ON(cic->key != cfqd_dead_key(cfqd));
Jens Axboe597bc482007-04-24 21:23:53 +02002932
Jens Axboe4ac845a2008-01-24 08:44:49 +01002933 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe597bc482007-04-24 21:23:53 +02002934
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002935 BUG_ON(ioc->ioc_data == cic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002936
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04002937 radix_tree_delete(&ioc->radix_root, cfqd->cic_index);
Jens Axboeffc4e752008-02-19 10:02:29 +01002938 hlist_del_rcu(&cic->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002939 spin_unlock_irqrestore(&ioc->lock, flags);
2940
2941 cfq_cic_free(cic);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002942}
2943
Jens Axboee2d74ac2006-03-28 08:59:01 +02002944static struct cfq_io_context *
Jens Axboe4ac845a2008-01-24 08:44:49 +01002945cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002946{
Jens Axboee2d74ac2006-03-28 08:59:01 +02002947 struct cfq_io_context *cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002948 unsigned long flags;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002949
Vasily Tarasov91fac312007-04-25 12:29:51 +02002950 if (unlikely(!ioc))
2951 return NULL;
2952
Jens Axboed6de8be2008-05-28 14:46:59 +02002953 rcu_read_lock();
2954
Jens Axboe597bc482007-04-24 21:23:53 +02002955 /*
2956 * we maintain a last-hit cache, to avoid browsing over the tree
2957 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01002958 cic = rcu_dereference(ioc->ioc_data);
Jens Axboed6de8be2008-05-28 14:46:59 +02002959 if (cic && cic->key == cfqd) {
2960 rcu_read_unlock();
Jens Axboe597bc482007-04-24 21:23:53 +02002961 return cic;
Jens Axboed6de8be2008-05-28 14:46:59 +02002962 }
Jens Axboe597bc482007-04-24 21:23:53 +02002963
Jens Axboe4ac845a2008-01-24 08:44:49 +01002964 do {
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04002965 cic = radix_tree_lookup(&ioc->radix_root, cfqd->cic_index);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002966 rcu_read_unlock();
2967 if (!cic)
2968 break;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002969 if (unlikely(cic->key != cfqd)) {
Jens Axboe4ac845a2008-01-24 08:44:49 +01002970 cfq_drop_dead_cic(cfqd, ioc, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002971 rcu_read_lock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01002972 continue;
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02002973 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02002974
Jens Axboed6de8be2008-05-28 14:46:59 +02002975 spin_lock_irqsave(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002976 rcu_assign_pointer(ioc->ioc_data, cic);
Jens Axboed6de8be2008-05-28 14:46:59 +02002977 spin_unlock_irqrestore(&ioc->lock, flags);
Jens Axboe4ac845a2008-01-24 08:44:49 +01002978 break;
2979 } while (1);
Jens Axboee2d74ac2006-03-28 08:59:01 +02002980
Jens Axboe4ac845a2008-01-24 08:44:49 +01002981 return cic;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002982}
2983
Jens Axboe4ac845a2008-01-24 08:44:49 +01002984/*
2985 * Add cic into ioc, using cfqd as the search key. This enables us to lookup
2986 * the process specific cfq io context when entered from the block layer.
2987 * Also adds the cic to a per-cfqd list, used when this queue is removed.
2988 */
Jens Axboefebffd62008-01-28 13:19:43 +01002989static int cfq_cic_link(struct cfq_data *cfqd, struct io_context *ioc,
2990 struct cfq_io_context *cic, gfp_t gfp_mask)
Jens Axboee2d74ac2006-03-28 08:59:01 +02002991{
Jens Axboe0261d682006-10-30 19:07:48 +01002992 unsigned long flags;
Jens Axboe4ac845a2008-01-24 08:44:49 +01002993 int ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002994
Jens Axboe4ac845a2008-01-24 08:44:49 +01002995 ret = radix_tree_preload(gfp_mask);
2996 if (!ret) {
2997 cic->ioc = ioc;
2998 cic->key = cfqd;
Jens Axboee2d74ac2006-03-28 08:59:01 +02002999
Jens Axboe4ac845a2008-01-24 08:44:49 +01003000 spin_lock_irqsave(&ioc->lock, flags);
3001 ret = radix_tree_insert(&ioc->radix_root,
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003002 cfqd->cic_index, cic);
Jens Axboeffc4e752008-02-19 10:02:29 +01003003 if (!ret)
3004 hlist_add_head_rcu(&cic->cic_list, &ioc->cic_list);
Jens Axboe4ac845a2008-01-24 08:44:49 +01003005 spin_unlock_irqrestore(&ioc->lock, flags);
3006
3007 radix_tree_preload_end();
3008
3009 if (!ret) {
3010 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3011 list_add(&cic->queue_list, &cfqd->cic_list);
3012 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
OGAWA Hirofumidbecf3a2006-04-18 09:45:18 +02003013 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02003014 }
3015
Jens Axboe4ac845a2008-01-24 08:44:49 +01003016 if (ret)
3017 printk(KERN_ERR "cfq: cic link failed!\n");
Jens Axboefc463792006-08-29 09:05:44 +02003018
Jens Axboe4ac845a2008-01-24 08:44:49 +01003019 return ret;
Jens Axboee2d74ac2006-03-28 08:59:01 +02003020}
3021
Jens Axboe22e2c502005-06-27 10:55:12 +02003022/*
3023 * Setup general io context and cfq io context. There can be several cfq
3024 * io contexts per general io context, if this process is doing io to more
Jens Axboee2d74ac2006-03-28 08:59:01 +02003025 * than one device managed by cfq.
Jens Axboe22e2c502005-06-27 10:55:12 +02003026 */
3027static struct cfq_io_context *
Jens Axboee2d74ac2006-03-28 08:59:01 +02003028cfq_get_io_context(struct cfq_data *cfqd, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Jens Axboe22e2c502005-06-27 10:55:12 +02003030 struct io_context *ioc = NULL;
3031 struct cfq_io_context *cic;
3032
3033 might_sleep_if(gfp_mask & __GFP_WAIT);
3034
Jens Axboeb5deef92006-07-19 23:39:40 +02003035 ioc = get_io_context(gfp_mask, cfqd->queue->node);
Jens Axboe22e2c502005-06-27 10:55:12 +02003036 if (!ioc)
3037 return NULL;
3038
Jens Axboe4ac845a2008-01-24 08:44:49 +01003039 cic = cfq_cic_lookup(cfqd, ioc);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003040 if (cic)
3041 goto out;
Jens Axboe22e2c502005-06-27 10:55:12 +02003042
Jens Axboee2d74ac2006-03-28 08:59:01 +02003043 cic = cfq_alloc_io_context(cfqd, gfp_mask);
3044 if (cic == NULL)
3045 goto err;
Jens Axboe22e2c502005-06-27 10:55:12 +02003046
Jens Axboe4ac845a2008-01-24 08:44:49 +01003047 if (cfq_cic_link(cfqd, ioc, cic, gfp_mask))
3048 goto err_free;
3049
Jens Axboe22e2c502005-06-27 10:55:12 +02003050out:
Jens Axboefc463792006-08-29 09:05:44 +02003051 smp_read_barrier_depends();
3052 if (unlikely(ioc->ioprio_changed))
3053 cfq_ioc_set_ioprio(ioc);
3054
Vivek Goyal246103332009-12-03 12:59:51 -05003055#ifdef CONFIG_CFQ_GROUP_IOSCHED
3056 if (unlikely(ioc->cgroup_changed))
3057 cfq_ioc_set_cgroup(ioc);
3058#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003059 return cic;
Jens Axboe4ac845a2008-01-24 08:44:49 +01003060err_free:
3061 cfq_cic_free(cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003062err:
3063 put_io_context(ioc);
3064 return NULL;
3065}
3066
3067static void
3068cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_io_context *cic)
3069{
Jens Axboeaaf1228dd2007-01-19 11:30:16 +11003070 unsigned long elapsed = jiffies - cic->last_end_request;
3071 unsigned long ttime = min(elapsed, 2UL * cfqd->cfq_slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003072
3073 cic->ttime_samples = (7*cic->ttime_samples + 256) / 8;
3074 cic->ttime_total = (7*cic->ttime_total + 256*ttime) / 8;
3075 cic->ttime_mean = (cic->ttime_total + 128) / cic->ttime_samples;
3076}
3077
Jens Axboe206dc692006-03-28 13:03:44 +02003078static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003079cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003080 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003081{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003082 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003083 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003084 if (cfqq->last_request_pos) {
3085 if (cfqq->last_request_pos < blk_rq_pos(rq))
3086 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3087 else
3088 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3089 }
Jens Axboe206dc692006-03-28 13:03:44 +02003090
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003091 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003092 if (blk_queue_nonrot(cfqd->queue))
3093 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3094 else
3095 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003096}
Jens Axboe22e2c502005-06-27 10:55:12 +02003097
3098/*
3099 * Disable idle window if the process thinks too long or seeks so much that
3100 * it doesn't matter
3101 */
3102static void
3103cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3104 struct cfq_io_context *cic)
3105{
Jens Axboe7b679132008-05-30 12:23:07 +02003106 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003107
Jens Axboe08717142008-01-28 11:38:15 +01003108 /*
3109 * Don't idle for async or idle io prio class
3110 */
3111 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003112 return;
3113
Jens Axboec265a7f2008-06-26 13:49:33 +02003114 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003115
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003116 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3117 cfq_mark_cfqq_deep(cfqq);
3118
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003119 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3120 enable_idle = 0;
3121 else if (!atomic_read(&cic->ioc->nr_tasks) || !cfqd->cfq_slice_idle ||
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003122 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003123 enable_idle = 0;
3124 else if (sample_valid(cic->ttime_samples)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003125 if (cic->ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003126 enable_idle = 0;
3127 else
3128 enable_idle = 1;
3129 }
3130
Jens Axboe7b679132008-05-30 12:23:07 +02003131 if (old_idle != enable_idle) {
3132 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3133 if (enable_idle)
3134 cfq_mark_cfqq_idle_window(cfqq);
3135 else
3136 cfq_clear_cfqq_idle_window(cfqq);
3137 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003138}
3139
Jens Axboe22e2c502005-06-27 10:55:12 +02003140/*
3141 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3142 * no or if we aren't sure, a 1 will cause a preempt.
3143 */
Jens Axboea6151c32009-10-07 20:02:57 +02003144static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003145cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003146 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003147{
Jens Axboe6d048f52007-04-25 12:44:27 +02003148 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003149
Jens Axboe6d048f52007-04-25 12:44:27 +02003150 cfqq = cfqd->active_queue;
3151 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003152 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003153
Jens Axboe6d048f52007-04-25 12:44:27 +02003154 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003155 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003156
3157 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003158 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003159
Jens Axboe22e2c502005-06-27 10:55:12 +02003160 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003161 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3162 */
3163 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3164 return false;
3165
3166 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003167 * if the new request is sync, but the currently running queue is
3168 * not, let the sync request have priority.
3169 */
Jens Axboe5e705372006-07-13 12:39:25 +02003170 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003171 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003172
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003173 if (new_cfqq->cfqg != cfqq->cfqg)
3174 return false;
3175
3176 if (cfq_slice_used(cfqq))
3177 return true;
3178
3179 /* Allow preemption only if we are idling on sync-noidle tree */
3180 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3181 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3182 new_cfqq->service_tree->count == 2 &&
3183 RB_EMPTY_ROOT(&cfqq->sort_list))
3184 return true;
3185
Jens Axboe374f84a2006-07-23 01:42:19 +02003186 /*
3187 * So both queues are sync. Let the new request get disk time if
3188 * it's a metadata request and the current queue is doing regular IO.
3189 */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003190 if ((rq->cmd_flags & REQ_META) && !cfqq->meta_pending)
Jens Axboee6ec4fe2009-11-03 20:21:35 +01003191 return true;
Jens Axboe22e2c502005-06-27 10:55:12 +02003192
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003193 /*
3194 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3195 */
3196 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003197 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003198
Jens Axboe1e3335d2007-02-14 19:59:49 +01003199 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003200 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003201
3202 /*
3203 * if this request is as-good as one we would expect from the
3204 * current cfqq, let it preempt
3205 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003206 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003207 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003208
Jens Axboea6151c32009-10-07 20:02:57 +02003209 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003210}
3211
3212/*
3213 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3214 * let it have half of its nominal slice.
3215 */
3216static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3217{
Jens Axboe7b679132008-05-30 12:23:07 +02003218 cfq_log_cfqq(cfqd, cfqq, "preempt");
Vivek Goyale5ff0822010-04-26 19:25:11 +02003219 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003220
Jens Axboebf572252006-07-19 20:29:12 +02003221 /*
3222 * Put the new queue at the front of the of the current list,
3223 * so we know that it will be selected next.
3224 */
3225 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003226
3227 cfq_service_tree_add(cfqd, cfqq, 1);
Jens Axboebf572252006-07-19 20:29:12 +02003228
Jens Axboe44f7c162007-01-19 11:51:58 +11003229 cfqq->slice_end = 0;
3230 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003231}
3232
3233/*
Jens Axboe5e705372006-07-13 12:39:25 +02003234 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003235 * something we should do about it
3236 */
3237static void
Jens Axboe5e705372006-07-13 12:39:25 +02003238cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3239 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003240{
Jens Axboe5e705372006-07-13 12:39:25 +02003241 struct cfq_io_context *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003242
Aaron Carroll45333d52008-08-26 15:52:36 +02003243 cfqd->rq_queued++;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02003244 if (rq->cmd_flags & REQ_META)
Jens Axboe374f84a2006-07-23 01:42:19 +02003245 cfqq->meta_pending++;
3246
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003247 cfq_update_io_thinktime(cfqd, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003248 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003249 cfq_update_idle_window(cfqd, cfqq, cic);
3250
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003251 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003252
3253 if (cfqq == cfqd->active_queue) {
3254 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003255 * Remember that we saw a request from this process, but
3256 * don't start queuing just yet. Otherwise we risk seeing lots
3257 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003258 * and merging. If the request is already larger than a single
3259 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003260 * merging is already done. Ditto for a busy system that
3261 * has other work pending, don't risk delaying until the
3262 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003263 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003264 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003265 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3266 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003267 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003268 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003269 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003270 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003271 cfq_blkiocg_update_idle_time_stats(
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003272 &cfqq->cfqg->blkg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003273 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003274 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003275 }
Jens Axboe5e705372006-07-13 12:39:25 +02003276 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003277 /*
3278 * not the active queue - expire current slice if it is
3279 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003280 * has some old slice time left and is of higher priority or
3281 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003282 */
3283 cfq_preempt_queue(cfqd, cfqq);
Tejun Heoa7f55792009-04-23 11:05:17 +09003284 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003285 }
3286}
3287
Jens Axboe165125e2007-07-24 09:28:11 +02003288static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003289{
Jens Axboeb4878f22005-10-20 16:42:29 +02003290 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003291 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003292
Jens Axboe7b679132008-05-30 12:23:07 +02003293 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Jens Axboefd0928d2008-01-24 08:52:45 +01003294 cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Jens Axboe30996f42009-10-05 11:03:39 +02003296 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003297 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003298 cfq_add_rq_rb(rq);
Vivek Goyale98ef892010-06-18 10:39:47 -04003299 cfq_blkiocg_update_io_add_stats(&(RQ_CFQG(rq))->blkg,
Divyesh Shahcdc11842010-04-08 21:15:10 -07003300 &cfqd->serving_group->blkg, rq_data_dir(rq),
3301 rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003302 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Aaron Carroll45333d52008-08-26 15:52:36 +02003305/*
3306 * Update hw_tag based on peak queue depth over 50 samples under
3307 * sufficient load.
3308 */
3309static void cfq_update_hw_tag(struct cfq_data *cfqd)
3310{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003311 struct cfq_queue *cfqq = cfqd->active_queue;
3312
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003313 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3314 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003315
3316 if (cfqd->hw_tag == 1)
3317 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003318
3319 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003320 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003321 return;
3322
Shaohua Li1a1238a2009-10-27 08:46:23 +01003323 /*
3324 * If active queue hasn't enough requests and can idle, cfq might not
3325 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3326 * case
3327 */
3328 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3329 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003330 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003331 return;
3332
Aaron Carroll45333d52008-08-26 15:52:36 +02003333 if (cfqd->hw_tag_samples++ < 50)
3334 return;
3335
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003336 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003337 cfqd->hw_tag = 1;
3338 else
3339 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003340}
3341
Vivek Goyal7667aa02009-12-08 17:52:58 -05003342static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3343{
3344 struct cfq_io_context *cic = cfqd->active_cic;
3345
3346 /* If there are other queues in the group, don't wait */
3347 if (cfqq->cfqg->nr_cfqq > 1)
3348 return false;
3349
3350 if (cfq_slice_used(cfqq))
3351 return true;
3352
3353 /* if slice left is less than think time, wait busy */
3354 if (cic && sample_valid(cic->ttime_samples)
3355 && (cfqq->slice_end - jiffies < cic->ttime_mean))
3356 return true;
3357
3358 /*
3359 * If think times is less than a jiffy than ttime_mean=0 and above
3360 * will not be true. It might happen that slice has not expired yet
3361 * but will expire soon (4-5 ns) during select_queue(). To cover the
3362 * case where think time is less than a jiffy, mark the queue wait
3363 * busy if only 1 jiffy is left in the slice.
3364 */
3365 if (cfqq->slice_end - jiffies == 1)
3366 return true;
3367
3368 return false;
3369}
3370
Jens Axboe165125e2007-07-24 09:28:11 +02003371static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Jens Axboe5e705372006-07-13 12:39:25 +02003373 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003374 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003375 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003376 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Jens Axboeb4878f22005-10-20 16:42:29 +02003378 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003379 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3380 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Aaron Carroll45333d52008-08-26 15:52:36 +02003382 cfq_update_hw_tag(cfqd);
3383
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003384 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003385 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003386 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003387 cfqq->dispatched--;
Vivek Goyale98ef892010-06-18 10:39:47 -04003388 cfq_blkiocg_update_completion_stats(&cfqq->cfqg->blkg,
3389 rq_start_time_ns(rq), rq_io_start_time_ns(rq),
3390 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003392 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003393
Vivek Goyal365722b2009-10-03 15:21:27 +02003394 if (sync) {
Jens Axboe5e705372006-07-13 12:39:25 +02003395 RQ_CIC(rq)->last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003396 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3397 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003398 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003399
3400 /*
3401 * If this is the active queue, check if it needs to be expired,
3402 * or if we want to idle in case it has no pending requests.
3403 */
3404 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003405 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3406
Jens Axboe44f7c162007-01-19 11:51:58 +11003407 if (cfq_cfqq_slice_new(cfqq)) {
3408 cfq_set_prio_slice(cfqd, cfqq);
3409 cfq_clear_cfqq_slice_new(cfqq);
3410 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003411
3412 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003413 * Should we wait for next request to come in before we expire
3414 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003415 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003416 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyalf75edf22009-12-03 12:59:53 -05003417 cfqq->slice_end = jiffies + cfqd->cfq_slice_idle;
3418 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003419 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003420 }
3421
Jens Axboea36e71f2009-04-15 12:15:11 +02003422 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003423 * Idling is not enabled on:
3424 * - expired queues
3425 * - idle-priority queues
3426 * - async queues
3427 * - queues with still some requests queued
3428 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003429 */
Jens Axboe08717142008-01-28 11:38:15 +01003430 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003431 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003432 else if (sync && cfqq_empty &&
3433 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003434 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003435 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003436 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003437
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003438 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003439 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440}
3441
Jens Axboe22e2c502005-06-27 10:55:12 +02003442/*
3443 * we temporarily boost lower priority queues if they are holding fs exclusive
3444 * resources. they are boosted to normal prio (CLASS_BE/4)
3445 */
3446static void cfq_prio_boost(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Jens Axboe22e2c502005-06-27 10:55:12 +02003448 if (has_fs_excl()) {
3449 /*
3450 * boost idle prio on transactions that would lock out other
3451 * users of the filesystem
3452 */
3453 if (cfq_class_idle(cfqq))
3454 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3455 if (cfqq->ioprio > IOPRIO_NORM)
3456 cfqq->ioprio = IOPRIO_NORM;
3457 } else {
3458 /*
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003459 * unboost the queue (if needed)
Jens Axboe22e2c502005-06-27 10:55:12 +02003460 */
Corrado Zoccolodddb7452009-11-02 10:40:37 +01003461 cfqq->ioprio_class = cfqq->org_ioprio_class;
3462 cfqq->ioprio = cfqq->org_ioprio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003464}
3465
Jens Axboe89850f72006-07-22 16:48:31 +02003466static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003467{
Jens Axboe1b379d82009-08-11 08:26:11 +02003468 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003469 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003470 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003471 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003472
3473 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003474}
3475
Jens Axboe165125e2007-07-24 09:28:11 +02003476static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003477{
3478 struct cfq_data *cfqd = q->elevator->elevator_data;
3479 struct task_struct *tsk = current;
Vasily Tarasov91fac312007-04-25 12:29:51 +02003480 struct cfq_io_context *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003481 struct cfq_queue *cfqq;
3482
3483 /*
3484 * don't force setup of a queue from here, as a call to may_queue
3485 * does not necessarily imply that a request actually will be queued.
3486 * so just lookup a possibly existing queue, or return 'may queue'
3487 * if that fails
3488 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003489 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003490 if (!cic)
3491 return ELV_MQUEUE_MAY;
3492
Jens Axboeb0b78f82009-04-08 10:56:08 +02003493 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003494 if (cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003495 cfq_init_prio_data(cfqq, cic->ioc);
Jens Axboe22e2c502005-06-27 10:55:12 +02003496 cfq_prio_boost(cfqq);
3497
Jens Axboe89850f72006-07-22 16:48:31 +02003498 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003499 }
3500
3501 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504/*
3505 * queue lock held here
3506 */
Jens Axboebb37b942006-12-01 10:42:33 +01003507static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Jens Axboe5e705372006-07-13 12:39:25 +02003509 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Jens Axboe5e705372006-07-13 12:39:25 +02003511 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003512 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Jens Axboe22e2c502005-06-27 10:55:12 +02003514 BUG_ON(!cfqq->allocated[rw]);
3515 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Jens Axboe5e705372006-07-13 12:39:25 +02003517 put_io_context(RQ_CIC(rq)->ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 rq->elevator_private = NULL;
Jens Axboe5e705372006-07-13 12:39:25 +02003520 rq->elevator_private2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003522 /* Put down rq reference on cfqg */
3523 cfq_put_cfqg(RQ_CFQG(rq));
3524 rq->elevator_private3 = NULL;
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 cfq_put_queue(cfqq);
3527 }
3528}
3529
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003530static struct cfq_queue *
3531cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_context *cic,
3532 struct cfq_queue *cfqq)
3533{
3534 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3535 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003536 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003537 cfq_put_queue(cfqq);
3538 return cic_to_cfqq(cic, 1);
3539}
3540
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003541/*
3542 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3543 * was the last process referring to said cfqq.
3544 */
3545static struct cfq_queue *
3546split_cfqq(struct cfq_io_context *cic, struct cfq_queue *cfqq)
3547{
3548 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003549 cfqq->pid = current->pid;
3550 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003551 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003552 return cfqq;
3553 }
3554
3555 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003556
3557 cfq_put_cooperator(cfqq);
3558
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003559 cfq_put_queue(cfqq);
3560 return NULL;
3561}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003563 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003565static int
Jens Axboe165125e2007-07-24 09:28:11 +02003566cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 struct cfq_data *cfqd = q->elevator->elevator_data;
3569 struct cfq_io_context *cic;
3570 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003571 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003572 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 unsigned long flags;
3574
3575 might_sleep_if(gfp_mask & __GFP_WAIT);
3576
Jens Axboee2d74ac2006-03-28 08:59:01 +02003577 cic = cfq_get_io_context(cfqd, gfp_mask);
Jens Axboe22e2c502005-06-27 10:55:12 +02003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 spin_lock_irqsave(q->queue_lock, flags);
3580
Jens Axboe22e2c502005-06-27 10:55:12 +02003581 if (!cic)
3582 goto queue_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003584new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003585 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003586 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Jens Axboefd0928d2008-01-24 08:52:45 +01003587 cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003588 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003589 } else {
3590 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003591 * If the queue was seeky for too long, break it apart.
3592 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003593 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003594 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3595 cfqq = split_cfqq(cic, cfqq);
3596 if (!cfqq)
3597 goto new_queue;
3598 }
3599
3600 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003601 * Check to see if this queue is scheduled to merge with
3602 * another, closely cooperating queue. The merging of
3603 * queues happens here as it must be done in process context.
3604 * The reference on new_cfqq was taken in merge_cfqqs.
3605 */
3606 if (cfqq->new_cfqq)
3607 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
3610 cfqq->allocated[rw]++;
Jens Axboe22e2c502005-06-27 10:55:12 +02003611 atomic_inc(&cfqq->ref);
Jens Axboe5e705372006-07-13 12:39:25 +02003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 spin_unlock_irqrestore(q->queue_lock, flags);
3614
Jens Axboe5e705372006-07-13 12:39:25 +02003615 rq->elevator_private = cic;
3616 rq->elevator_private2 = cfqq;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003617 rq->elevator_private3 = cfq_ref_get_cfqg(cfqq->cfqg);
Jens Axboe5e705372006-07-13 12:39:25 +02003618 return 0;
Jens Axboe3b181522005-06-27 10:56:24 +02003619
Jens Axboe22e2c502005-06-27 10:55:12 +02003620queue_fail:
3621 if (cic)
3622 put_io_context(cic->ioc);
Jens Axboe89850f72006-07-22 16:48:31 +02003623
Jens Axboe23e018a2009-10-05 08:52:35 +02003624 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 spin_unlock_irqrestore(q->queue_lock, flags);
Jens Axboe7b679132008-05-30 12:23:07 +02003626 cfq_log(cfqd, "set_request fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return 1;
3628}
3629
David Howells65f27f32006-11-22 14:55:48 +00003630static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003631{
David Howells65f27f32006-11-22 14:55:48 +00003632 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003633 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003634 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003635
Jens Axboe40bb54d2009-04-15 12:11:10 +02003636 spin_lock_irq(q->queue_lock);
Tejun Heoa7f55792009-04-23 11:05:17 +09003637 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003638 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003639}
3640
3641/*
3642 * Timer running if the active_queue is currently idling inside its time slice
3643 */
3644static void cfq_idle_slice_timer(unsigned long data)
3645{
3646 struct cfq_data *cfqd = (struct cfq_data *) data;
3647 struct cfq_queue *cfqq;
3648 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003649 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003650
Jens Axboe7b679132008-05-30 12:23:07 +02003651 cfq_log(cfqd, "idle timer fired");
3652
Jens Axboe22e2c502005-06-27 10:55:12 +02003653 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3654
Jens Axboefe094d92008-01-31 13:08:54 +01003655 cfqq = cfqd->active_queue;
3656 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003657 timed_out = 0;
3658
Jens Axboe22e2c502005-06-27 10:55:12 +02003659 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003660 * We saw a request before the queue expired, let it through
3661 */
3662 if (cfq_cfqq_must_dispatch(cfqq))
3663 goto out_kick;
3664
3665 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003666 * expired
3667 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003668 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003669 goto expire;
3670
3671 /*
3672 * only expire and reinvoke request handler, if there are
3673 * other queues with pending requests
3674 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003675 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003676 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003677
3678 /*
3679 * not expired and it has a request pending, let it dispatch
3680 */
Jens Axboe75e50982009-04-07 08:56:14 +02003681 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003682 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003683
3684 /*
3685 * Queue depth flag is reset only when the idle didn't succeed
3686 */
3687 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003688 }
3689expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003690 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003691out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003692 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003693out_cont:
3694 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3695}
3696
Jens Axboe3b181522005-06-27 10:56:24 +02003697static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3698{
3699 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003700 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003701}
Jens Axboe22e2c502005-06-27 10:55:12 +02003702
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003703static void cfq_put_async_queues(struct cfq_data *cfqd)
3704{
3705 int i;
3706
3707 for (i = 0; i < IOPRIO_BE_NR; i++) {
3708 if (cfqd->async_cfqq[0][i])
3709 cfq_put_queue(cfqd->async_cfqq[0][i]);
3710 if (cfqd->async_cfqq[1][i])
3711 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003712 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003713
3714 if (cfqd->async_idle_cfqq)
3715 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003716}
3717
Jens Axboebb729bc2009-12-06 09:54:19 +01003718static void cfq_cfqd_free(struct rcu_head *head)
3719{
3720 kfree(container_of(head, struct cfq_data, rcu));
3721}
3722
Jens Axboeb374d182008-10-31 10:05:07 +01003723static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
Jens Axboe22e2c502005-06-27 10:55:12 +02003725 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003726 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003727
Jens Axboe3b181522005-06-27 10:56:24 +02003728 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003729
Al Virod9ff4182006-03-18 13:51:22 -05003730 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003731
Al Virod9ff4182006-03-18 13:51:22 -05003732 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003733 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003734
3735 while (!list_empty(&cfqd->cic_list)) {
Al Virod9ff4182006-03-18 13:51:22 -05003736 struct cfq_io_context *cic = list_entry(cfqd->cic_list.next,
3737 struct cfq_io_context,
3738 queue_list);
Jens Axboe89850f72006-07-22 16:48:31 +02003739
3740 __cfq_exit_single_io_context(cfqd, cic);
Al Virod9ff4182006-03-18 13:51:22 -05003741 }
Jens Axboee2d74ac2006-03-28 08:59:01 +02003742
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003743 cfq_put_async_queues(cfqd);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003744 cfq_release_cfq_groups(cfqd);
Vivek Goyale98ef892010-06-18 10:39:47 -04003745 cfq_blkiocg_del_blkio_group(&cfqd->root_group.blkg);
Jens Axboe15c31be2007-07-10 13:43:25 +02003746
Al Virod9ff4182006-03-18 13:51:22 -05003747 spin_unlock_irq(q->queue_lock);
Al Viroa90d7422006-03-18 12:05:37 -05003748
3749 cfq_shutdown_timer_wq(cfqd);
3750
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003751 spin_lock(&cic_index_lock);
3752 ida_remove(&cic_index_ida, cfqd->cic_index);
3753 spin_unlock(&cic_index_lock);
3754
Vivek Goyalb1c35762009-12-03 12:59:47 -05003755 /* Wait for cfqg->blkg->key accessors to exit their grace periods. */
Jens Axboebb729bc2009-12-06 09:54:19 +01003756 call_rcu(&cfqd->rcu, cfq_cfqd_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003759static int cfq_alloc_cic_index(void)
3760{
3761 int index, error;
3762
3763 do {
3764 if (!ida_pre_get(&cic_index_ida, GFP_KERNEL))
3765 return -ENOMEM;
3766
3767 spin_lock(&cic_index_lock);
3768 error = ida_get_new(&cic_index_ida, &index);
3769 spin_unlock(&cic_index_lock);
3770 if (error && error != -EAGAIN)
3771 return error;
3772 } while (error);
3773
3774 return index;
3775}
3776
Jens Axboe165125e2007-07-24 09:28:11 +02003777static void *cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 struct cfq_data *cfqd;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003780 int i, j;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003781 struct cfq_group *cfqg;
Vivek Goyal615f0252009-12-03 12:59:39 -05003782 struct cfq_rb_root *st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003784 i = cfq_alloc_cic_index();
3785 if (i < 0)
3786 return NULL;
3787
Christoph Lameter94f60302007-07-17 04:03:29 -07003788 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if (!cfqd)
Jens Axboebc1c1162006-06-08 08:49:06 +02003790 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003792 cfqd->cic_index = i;
3793
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003794 /* Init root service tree */
3795 cfqd->grp_service_tree = CFQ_RB_ROOT;
3796
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003797 /* Init root group */
3798 cfqg = &cfqd->root_group;
Vivek Goyal615f0252009-12-03 12:59:39 -05003799 for_each_cfqg_st(cfqg, i, j, st)
3800 *st = CFQ_RB_ROOT;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003801 RB_CLEAR_NODE(&cfqg->rb_node);
Jens Axboe26a2ac02009-04-23 12:13:27 +02003802
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003803 /* Give preference to root group over other groups */
3804 cfqg->weight = 2*BLKIO_WEIGHT_DEFAULT;
3805
Vivek Goyal25fb5162009-12-03 12:59:46 -05003806#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyalb1c35762009-12-03 12:59:47 -05003807 /*
3808 * Take a reference to root group which we never drop. This is just
3809 * to make sure that cfq_put_cfqg() does not try to kfree root group
3810 */
3811 atomic_set(&cfqg->ref, 1);
Vivek Goyaldcf097b2010-04-22 11:54:52 -04003812 rcu_read_lock();
Vivek Goyale98ef892010-06-18 10:39:47 -04003813 cfq_blkiocg_add_blkio_group(&blkio_root_cgroup, &cfqg->blkg,
3814 (void *)cfqd, 0);
Vivek Goyaldcf097b2010-04-22 11:54:52 -04003815 rcu_read_unlock();
Vivek Goyal25fb5162009-12-03 12:59:46 -05003816#endif
Jens Axboe26a2ac02009-04-23 12:13:27 +02003817 /*
3818 * Not strictly needed (since RB_ROOT just clears the node and we
3819 * zeroed cfqd on alloc), but better be safe in case someone decides
3820 * to add magic to the rb code
3821 */
3822 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3823 cfqd->prio_trees[i] = RB_ROOT;
3824
Jens Axboe6118b702009-06-30 09:34:12 +02003825 /*
3826 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3827 * Grab a permanent reference to it, so that the normal code flow
3828 * will not attempt to free it.
3829 */
3830 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
3831 atomic_inc(&cfqd->oom_cfqq.ref);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003832 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, &cfqd->root_group);
Jens Axboe6118b702009-06-30 09:34:12 +02003833
Al Virod9ff4182006-03-18 13:51:22 -05003834 INIT_LIST_HEAD(&cfqd->cic_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 cfqd->queue = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Jens Axboe22e2c502005-06-27 10:55:12 +02003838 init_timer(&cfqd->idle_slice_timer);
3839 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3840 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3841
Jens Axboe23e018a2009-10-05 08:52:35 +02003842 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003845 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3846 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 cfqd->cfq_back_max = cfq_back_max;
3848 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003849 cfqd->cfq_slice[0] = cfq_slice_async;
3850 cfqd->cfq_slice[1] = cfq_slice_sync;
3851 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3852 cfqd->cfq_slice_idle = cfq_slice_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003853 cfqd->cfq_latency = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05003854 cfqd->cfq_group_isolation = 0;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003855 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01003856 /*
3857 * we optimistically start assuming sync ops weren't delayed in last
3858 * second, in order to have larger depth for async operations.
3859 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003860 cfqd->last_delayed_sync = jiffies - HZ;
Jens Axboebc1c1162006-06-08 08:49:06 +02003861 return cfqd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862}
3863
3864static void cfq_slab_kill(void)
3865{
Jens Axboed6de8be2008-05-28 14:46:59 +02003866 /*
3867 * Caller already ensured that pending RCU callbacks are completed,
3868 * so we should have no busy allocations at this point.
3869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 if (cfq_pool)
3871 kmem_cache_destroy(cfq_pool);
3872 if (cfq_ioc_pool)
3873 kmem_cache_destroy(cfq_ioc_pool);
3874}
3875
3876static int __init cfq_slab_setup(void)
3877{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003878 cfq_pool = KMEM_CACHE(cfq_queue, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (!cfq_pool)
3880 goto fail;
3881
Fabio Checconi34e6bbf2008-04-02 14:31:02 +02003882 cfq_ioc_pool = KMEM_CACHE(cfq_io_context, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 if (!cfq_ioc_pool)
3884 goto fail;
3885
3886 return 0;
3887fail:
3888 cfq_slab_kill();
3889 return -ENOMEM;
3890}
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892/*
3893 * sysfs parts below -->
3894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895static ssize_t
3896cfq_var_show(unsigned int var, char *page)
3897{
3898 return sprintf(page, "%d\n", var);
3899}
3900
3901static ssize_t
3902cfq_var_store(unsigned int *var, const char *page, size_t count)
3903{
3904 char *p = (char *) page;
3905
3906 *var = simple_strtoul(p, &p, 10);
3907 return count;
3908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003911static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003913 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 unsigned int __data = __VAR; \
3915 if (__CONV) \
3916 __data = jiffies_to_msecs(__data); \
3917 return cfq_var_show(__data, (page)); \
3918}
3919SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003920SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3921SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003922SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3923SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003924SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
3925SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3926SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3927SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003928SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Vivek Goyalae30c282009-12-03 12:59:55 -05003929SHOW_FUNCTION(cfq_group_isolation_show, cfqd->cfq_group_isolation, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930#undef SHOW_FUNCTION
3931
3932#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003933static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003935 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 unsigned int __data; \
3937 int ret = cfq_var_store(&__data, (page), count); \
3938 if (__data < (MIN)) \
3939 __data = (MIN); \
3940 else if (__data > (MAX)) \
3941 __data = (MAX); \
3942 if (__CONV) \
3943 *(__PTR) = msecs_to_jiffies(__data); \
3944 else \
3945 *(__PTR) = __data; \
3946 return ret; \
3947}
3948STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003949STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3950 UINT_MAX, 1);
3951STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3952 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003953STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003954STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3955 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003956STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
3957STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3958STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003959STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3960 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003961STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Vivek Goyalae30c282009-12-03 12:59:55 -05003962STORE_FUNCTION(cfq_group_isolation_store, &cfqd->cfq_group_isolation, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963#undef STORE_FUNCTION
3964
Al Viroe572ec72006-03-18 22:27:18 -05003965#define CFQ_ATTR(name) \
3966 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003967
Al Viroe572ec72006-03-18 22:27:18 -05003968static struct elv_fs_entry cfq_attrs[] = {
3969 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003970 CFQ_ATTR(fifo_expire_sync),
3971 CFQ_ATTR(fifo_expire_async),
3972 CFQ_ATTR(back_seek_max),
3973 CFQ_ATTR(back_seek_penalty),
3974 CFQ_ATTR(slice_sync),
3975 CFQ_ATTR(slice_async),
3976 CFQ_ATTR(slice_async_rq),
3977 CFQ_ATTR(slice_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003978 CFQ_ATTR(low_latency),
Vivek Goyalae30c282009-12-03 12:59:55 -05003979 CFQ_ATTR(group_isolation),
Al Viroe572ec72006-03-18 22:27:18 -05003980 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981};
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983static struct elevator_type iosched_cfq = {
3984 .ops = {
3985 .elevator_merge_fn = cfq_merge,
3986 .elevator_merged_fn = cfq_merged_request,
3987 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003988 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07003989 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02003990 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02003992 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 .elevator_deactivate_req_fn = cfq_deactivate_request,
3994 .elevator_queue_empty_fn = cfq_queue_empty,
3995 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02003996 .elevator_former_req_fn = elv_rb_former_request,
3997 .elevator_latter_req_fn = elv_rb_latter_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 .elevator_set_req_fn = cfq_set_request,
3999 .elevator_put_req_fn = cfq_put_request,
4000 .elevator_may_queue_fn = cfq_may_queue,
4001 .elevator_init_fn = cfq_init_queue,
4002 .elevator_exit_fn = cfq_exit_queue,
Jens Axboefc463792006-08-29 09:05:44 +02004003 .trim = cfq_free_io_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 },
Al Viro3d1ab402006-03-18 18:35:43 -05004005 .elevator_attrs = cfq_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 .elevator_name = "cfq",
4007 .elevator_owner = THIS_MODULE,
4008};
4009
Vivek Goyal3e252062009-12-04 10:36:42 -05004010#ifdef CONFIG_CFQ_GROUP_IOSCHED
4011static struct blkio_policy_type blkio_policy_cfq = {
4012 .ops = {
4013 .blkio_unlink_group_fn = cfq_unlink_blkio_group,
4014 .blkio_update_group_weight_fn = cfq_update_blkio_group_weight,
4015 },
Vivek Goyal062a6442010-09-15 17:06:33 -04004016 .plid = BLKIO_POLICY_PROP,
Vivek Goyal3e252062009-12-04 10:36:42 -05004017};
4018#else
4019static struct blkio_policy_type blkio_policy_cfq;
4020#endif
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022static int __init cfq_init(void)
4023{
Jens Axboe22e2c502005-06-27 10:55:12 +02004024 /*
4025 * could be 0 on HZ < 1000 setups
4026 */
4027 if (!cfq_slice_async)
4028 cfq_slice_async = 1;
4029 if (!cfq_slice_idle)
4030 cfq_slice_idle = 1;
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (cfq_slab_setup())
4033 return -ENOMEM;
4034
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004035 elv_register(&iosched_cfq);
Vivek Goyal3e252062009-12-04 10:36:42 -05004036 blkio_policy_register(&blkio_policy_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
4041static void __exit cfq_exit(void)
4042{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07004043 DECLARE_COMPLETION_ONSTACK(all_gone);
Vivek Goyal3e252062009-12-04 10:36:42 -05004044 blkio_policy_unregister(&blkio_policy_cfq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 elv_unregister(&iosched_cfq);
Al Viro334e94d2006-03-18 15:05:53 -05004046 ioc_gone = &all_gone;
OGAWA Hirofumifba82272006-04-18 09:44:06 +02004047 /* ioc_gone's update must be visible before reading ioc_count */
4048 smp_wmb();
Jens Axboed6de8be2008-05-28 14:46:59 +02004049
4050 /*
4051 * this also protects us from entering cfq_slab_kill() with
4052 * pending RCU callbacks
4053 */
Tejun Heo245b2e72009-06-24 15:13:48 +09004054 if (elv_ioc_count_read(cfq_ioc_count))
Jens Axboe9a11b4e2008-05-29 09:32:08 +02004055 wait_for_completion(&all_gone);
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004056 ida_destroy(&cic_index_ida);
Christoph Hellwig83521d32005-10-30 15:01:39 -08004057 cfq_slab_kill();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
4060module_init(cfq_init);
4061module_exit(cfq_exit);
4062
4063MODULE_AUTHOR("Jens Axboe");
4064MODULE_LICENSE("GPL");
4065MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");