blob: 2e13e9e689bddc7b52a8183a70093f0bc77207f0 [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>
Tejun Heo6e736be2011-12-14 00:33:38 +010017#include "blk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Tejun Heo03814112012-03-05 13:15:14 -080019static struct blkio_policy_type blkio_policy_cfq;
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
22 * tunables
23 */
Jens Axboefe094d92008-01-31 13:08:54 +010024/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010025static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010026static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010027/* maximum backwards seek, in KiB */
28static const int cfq_back_max = 16 * 1024;
29/* penalty of a backwards seek */
30static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010031static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020032static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010033static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020034static int cfq_slice_idle = HZ / 125;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +020035static int cfq_group_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010036static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
37static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020038
Jens Axboed9e76202007-04-20 14:27:50 +020039/*
Jens Axboe08717142008-01-28 11:38:15 +010040 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020041 */
Jens Axboe08717142008-01-28 11:38:15 +010042#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020043
44/*
45 * below this threshold, we consider thinktime immediate
46 */
47#define CFQ_MIN_TT (2)
48
Jens Axboe22e2c502005-06-27 10:55:12 +020049#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020050#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050051#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020052
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010053#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010054#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010055#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010056#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010057
Tejun Heoa612fdd2011-12-14 00:33:41 +010058#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
59#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
60#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Christoph Lametere18b8902006-12-06 20:33:20 -080062static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jens Axboe22e2c502005-06-27 10:55:12 +020064#define CFQ_PRIO_LISTS IOPRIO_BE_NR
65#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020066#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
67
Jens Axboe206dc692006-03-28 13:03:44 +020068#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050069#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020070
Tejun Heoc5869802011-12-14 00:33:41 +010071struct cfq_ttime {
72 unsigned long last_end_request;
73
74 unsigned long ttime_total;
75 unsigned long ttime_samples;
76 unsigned long ttime_mean;
77};
78
Jens Axboe22e2c502005-06-27 10:55:12 +020079/*
Jens Axboecc09e292007-04-26 12:53:50 +020080 * Most of our rbtree usage is for sorting with min extraction, so
81 * if we cache the leftmost node we don't have to walk down the tree
82 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
83 * move this into the elevator for the rq sorting as well.
84 */
85struct cfq_rb_root {
86 struct rb_root rb;
87 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010088 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010089 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050090 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020091 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020092};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020093#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
94 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020095
96/*
Jens Axboe6118b702009-06-30 09:34:12 +020097 * Per process-grouping structure
98 */
99struct cfq_queue {
100 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100101 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200102 /* various state flags, see below */
103 unsigned int flags;
104 /* parent cfq_data */
105 struct cfq_data *cfqd;
106 /* service_tree member */
107 struct rb_node rb_node;
108 /* service_tree key */
109 unsigned long rb_key;
110 /* prio tree member */
111 struct rb_node p_node;
112 /* prio tree root we belong to, if any */
113 struct rb_root *p_root;
114 /* sorted list of pending requests */
115 struct rb_root sort_list;
116 /* if fifo isn't expired, next request to serve */
117 struct request *next_rq;
118 /* requests queued in sort_list */
119 int queued[2];
120 /* currently allocated requests */
121 int allocated[2];
122 /* fifo list of requests in sort_list */
123 struct list_head fifo;
124
Vivek Goyaldae739e2009-12-03 12:59:45 -0500125 /* time when queue got scheduled in to dispatch first request. */
126 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500127 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100128 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500129 /* time when first request from queue completed and slice started. */
130 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200131 unsigned long slice_end;
132 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200133
Christoph Hellwig65299a32011-08-23 14:50:29 +0200134 /* pending priority requests */
135 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200136 /* number of requests that are on the dispatch list or inside driver */
137 int dispatched;
138
139 /* io prio of this group */
140 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200141 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200142
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100143 pid_t pid;
144
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100145 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400146 sector_t last_request_pos;
147
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100148 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400149 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500150 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200151 /* Number of sectors dispatched from queue in single dispatch round */
152 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200153};
154
155/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100156 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100157 * IDLE is handled separately, so it has negative index
158 */
159enum wl_prio_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100160 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500161 RT_WORKLOAD = 1,
162 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200163 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100164};
165
166/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100167 * Second index in the service_trees.
168 */
169enum wl_type_t {
170 ASYNC_WORKLOAD = 0,
171 SYNC_NOIDLE_WORKLOAD = 1,
172 SYNC_WORKLOAD = 2
173};
174
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500175/* This is per cgroup per device grouping structure */
176struct cfq_group {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500177 /* group service_tree member */
178 struct rb_node rb_node;
179
180 /* group service_tree key */
181 u64 vdisktime;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500182 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100183 unsigned int new_weight;
184 bool needs_update;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500185
186 /* number of cfqq currently on this group */
187 int nr_cfqq;
188
Jens Axboe22e2c502005-06-27 10:55:12 +0200189 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200190 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200191 * create the array for each prio class but at run time it is used
192 * only for RT and BE class and slot for IDLE class remains unused.
193 * This is primarily done to avoid confusion and a gcc warning.
194 */
195 unsigned int busy_queues_avg[CFQ_PRIO_NR];
196 /*
197 * rr lists of queues with requests. We maintain service trees for
198 * RT and BE classes. These trees are subdivided in subclasses
199 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
200 * class there is no subclassification and all the cfq queues go on
201 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100202 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200203 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100204 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100205 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500206
207 unsigned long saved_workload_slice;
208 enum wl_type_t saved_workload;
209 enum wl_prio_t saved_serving_prio;
Tejun Heo4eef3042012-03-05 13:15:18 -0800210
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200211 /* number of requests that are on the dispatch list or inside driver */
212 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200213 struct cfq_ttime ttime;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500214};
215
Tejun Heoc5869802011-12-14 00:33:41 +0100216struct cfq_io_cq {
217 struct io_cq icq; /* must be the first member */
218 struct cfq_queue *cfqq[2];
219 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700220 int ioprio; /* the current ioprio */
221#ifdef CONFIG_CFQ_GROUP_IOSCHED
222 uint64_t blkcg_id; /* the current blkcg ID */
223#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100224};
225
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500226/*
227 * Per block device queue structure
228 */
229struct cfq_data {
230 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500231 /* Root service tree for cfq_groups */
232 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800233 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500234
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100235 /*
236 * The priority currently being served
237 */
238 enum wl_prio_t serving_prio;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100239 enum wl_type_t serving_type;
240 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500241 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200242
243 /*
244 * Each priority tree is sorted by next_request position. These
245 * trees are used when determining if two or more queues are
246 * interleaving requests (see cfq_close_cooperator).
247 */
248 struct rb_root prio_trees[CFQ_PRIO_LISTS];
249
Jens Axboe22e2c502005-06-27 10:55:12 +0200250 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100251 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200252
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100253 int rq_in_driver;
254 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200255
256 /*
257 * queue-depth detection
258 */
259 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200260 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100261 /*
262 * hw_tag can be
263 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
264 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
265 * 0 => no NCQ
266 */
267 int hw_tag_est_depth;
268 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200269
270 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200271 * idle window management
272 */
273 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200274 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200275
276 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100277 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200278
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200279 /*
280 * async queue for each priority case
281 */
282 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
283 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200284
Jens Axboe6d048f52007-04-25 12:44:27 +0200285 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * tunables, see top of file
289 */
290 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200291 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 unsigned int cfq_back_penalty;
293 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200294 unsigned int cfq_slice[2];
295 unsigned int cfq_slice_async_rq;
296 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200297 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200298 unsigned int cfq_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500299
Jens Axboe6118b702009-06-30 09:34:12 +0200300 /*
301 * Fallback dummy cfqq for extreme OOM conditions
302 */
303 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200304
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100305 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Vivek Goyal25fb5162009-12-03 12:59:46 -0500308static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
309
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500310static struct cfq_rb_root *service_tree_for(struct cfq_group *cfqg,
311 enum wl_prio_t prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500312 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100313{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500314 if (!cfqg)
315 return NULL;
316
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100317 if (prio == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500318 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100319
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500320 return &cfqg->service_trees[prio][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100321}
322
Jens Axboe3b181522005-06-27 10:56:24 +0200323enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100324 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
325 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200326 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100327 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100328 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
329 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
330 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100331 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200332 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400333 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100334 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100335 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500336 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200337};
338
339#define CFQ_CFQQ_FNS(name) \
340static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
341{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100342 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200343} \
344static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
345{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100346 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200347} \
348static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
349{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100350 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200351}
352
353CFQ_CFQQ_FNS(on_rr);
354CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200355CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200356CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200357CFQ_CFQQ_FNS(fifo_expire);
358CFQ_CFQQ_FNS(idle_window);
359CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100360CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200361CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200362CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100363CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100364CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500365CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200366#undef CFQ_CFQQ_FNS
367
Vivek Goyalafc24d42010-04-26 19:27:56 +0200368#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700369
370#include "blk-cgroup.h"
371
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100372static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg)
373{
374 return blkg_to_pdata(blkg, &blkio_policy_cfq);
375}
376
377static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg)
378{
Tejun Heoaaec55a2012-04-01 14:38:42 -0700379 return pdata_to_blkg(cfqg);
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100380}
381
382static inline void cfqg_get(struct cfq_group *cfqg)
383{
384 return blkg_get(cfqg_to_blkg(cfqg));
385}
386
387static inline void cfqg_put(struct cfq_group *cfqg)
388{
389 return blkg_put(cfqg_to_blkg(cfqg));
390}
391
Vivek Goyal2868ef72009-12-03 12:59:48 -0500392#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
393 blk_add_trace_msg((cfqd)->queue, "cfq%d%c %s " fmt, (cfqq)->pid, \
394 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
Tejun Heo03814112012-03-05 13:15:14 -0800395 blkg_path(cfqg_to_blkg((cfqq)->cfqg)), ##args)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500396
397#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) \
398 blk_add_trace_msg((cfqd)->queue, "%s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800399 blkg_path(cfqg_to_blkg((cfqg))), ##args) \
Vivek Goyal2868ef72009-12-03 12:59:48 -0500400
Tejun Heo2ce4d502012-04-01 14:38:43 -0700401static inline void cfq_blkiocg_update_io_add_stats(struct blkio_group *blkg,
402 struct blkio_policy_type *pol,
403 struct blkio_group *curr_blkg,
404 bool direction, bool sync)
405{
406 blkiocg_update_io_add_stats(blkg, pol, curr_blkg, direction, sync);
407}
408
409static inline void cfq_blkiocg_update_dequeue_stats(struct blkio_group *blkg,
410 struct blkio_policy_type *pol, unsigned long dequeue)
411{
412 blkiocg_update_dequeue_stats(blkg, pol, dequeue);
413}
414
415static inline void cfq_blkiocg_update_timeslice_used(struct blkio_group *blkg,
416 struct blkio_policy_type *pol, unsigned long time,
417 unsigned long unaccounted_time)
418{
419 blkiocg_update_timeslice_used(blkg, pol, time, unaccounted_time);
420}
421
422static inline void cfq_blkiocg_set_start_empty_time(struct blkio_group *blkg,
423 struct blkio_policy_type *pol)
424{
425 blkiocg_set_start_empty_time(blkg, pol);
426}
427
428static inline void cfq_blkiocg_update_io_remove_stats(struct blkio_group *blkg,
429 struct blkio_policy_type *pol, bool direction,
430 bool sync)
431{
432 blkiocg_update_io_remove_stats(blkg, pol, direction, sync);
433}
434
435static inline void cfq_blkiocg_update_io_merged_stats(struct blkio_group *blkg,
436 struct blkio_policy_type *pol, bool direction,
437 bool sync)
438{
439 blkiocg_update_io_merged_stats(blkg, pol, direction, sync);
440}
441
442static inline void cfq_blkiocg_update_idle_time_stats(struct blkio_group *blkg,
443 struct blkio_policy_type *pol)
444{
445 blkiocg_update_idle_time_stats(blkg, pol);
446}
447
448static inline void
449cfq_blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
450 struct blkio_policy_type *pol)
451{
452 blkiocg_update_avg_queue_size_stats(blkg, pol);
453}
454
455static inline void
456cfq_blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
457 struct blkio_policy_type *pol)
458{
459 blkiocg_update_set_idle_time_stats(blkg, pol);
460}
461
462static inline void cfq_blkiocg_update_dispatch_stats(struct blkio_group *blkg,
463 struct blkio_policy_type *pol, uint64_t bytes,
464 bool direction, bool sync)
465{
466 blkiocg_update_dispatch_stats(blkg, pol, bytes, direction, sync);
467}
468
469static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
470 struct blkio_policy_type *pol, uint64_t start_time,
471 uint64_t io_start_time, bool direction, bool sync)
472{
473 blkiocg_update_completion_stats(blkg, pol, start_time, io_start_time,
474 direction, sync);
475}
476
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100477#else /* CONFIG_CFQ_GROUP_IOSCHED */
478
479static inline struct cfq_group *blkg_to_cfqg(struct blkio_group *blkg) { return NULL; }
480static inline struct blkio_group *cfqg_to_blkg(struct cfq_group *cfqg) { return NULL; }
481static inline void cfqg_get(struct cfq_group *cfqg) { }
482static inline void cfqg_put(struct cfq_group *cfqg) { }
483
Jens Axboe7b679132008-05-30 12:23:07 +0200484#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
485 blk_add_trace_msg((cfqd)->queue, "cfq%d " fmt, (cfqq)->pid, ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200486#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100487
Tejun Heo2ce4d502012-04-01 14:38:43 -0700488static inline void cfq_blkiocg_update_io_add_stats(struct blkio_group *blkg,
489 struct blkio_policy_type *pol,
490 struct blkio_group *curr_blkg, bool direction,
491 bool sync) { }
492static inline void cfq_blkiocg_update_dequeue_stats(struct blkio_group *blkg,
493 struct blkio_policy_type *pol, unsigned long dequeue) { }
494static inline void cfq_blkiocg_update_timeslice_used(struct blkio_group *blkg,
495 struct blkio_policy_type *pol, unsigned long time,
496 unsigned long unaccounted_time) { }
497static inline void cfq_blkiocg_set_start_empty_time(struct blkio_group *blkg,
498 struct blkio_policy_type *pol) { }
499static inline void cfq_blkiocg_update_io_remove_stats(struct blkio_group *blkg,
500 struct blkio_policy_type *pol, bool direction,
501 bool sync) { }
502static inline void cfq_blkiocg_update_io_merged_stats(struct blkio_group *blkg,
503 struct blkio_policy_type *pol, bool direction,
504 bool sync) { }
505static inline void cfq_blkiocg_update_idle_time_stats(struct blkio_group *blkg,
506 struct blkio_policy_type *pol) { }
507static inline void
508cfq_blkiocg_update_avg_queue_size_stats(struct blkio_group *blkg,
509 struct blkio_policy_type *pol) { }
510
511static inline void
512cfq_blkiocg_update_set_idle_time_stats(struct blkio_group *blkg,
513 struct blkio_policy_type *pol) { }
514
515static inline void cfq_blkiocg_update_dispatch_stats(struct blkio_group *blkg,
516 struct blkio_policy_type *pol, uint64_t bytes,
517 bool direction, bool sync) { }
518static inline void cfq_blkiocg_update_completion_stats(struct blkio_group *blkg,
519 struct blkio_policy_type *pol, uint64_t start_time,
520 uint64_t io_start_time, bool direction, bool sync) { }
521
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100522#endif /* CONFIG_CFQ_GROUP_IOSCHED */
523
Jens Axboe7b679132008-05-30 12:23:07 +0200524#define cfq_log(cfqd, fmt, args...) \
525 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
526
Vivek Goyal615f0252009-12-03 12:59:39 -0500527/* Traverses through cfq group service trees */
528#define for_each_cfqg_st(cfqg, i, j, st) \
529 for (i = 0; i <= IDLE_WORKLOAD; i++) \
530 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
531 : &cfqg->service_tree_idle; \
532 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
533 (i == IDLE_WORKLOAD && j == 0); \
534 j++, st = i < IDLE_WORKLOAD ? \
535 &cfqg->service_trees[i][j]: NULL) \
536
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200537static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
538 struct cfq_ttime *ttime, bool group_idle)
539{
540 unsigned long slice;
541 if (!sample_valid(ttime->ttime_samples))
542 return false;
543 if (group_idle)
544 slice = cfqd->cfq_group_idle;
545 else
546 slice = cfqd->cfq_slice_idle;
547 return ttime->ttime_mean > slice;
548}
Vivek Goyal615f0252009-12-03 12:59:39 -0500549
Vivek Goyal02b35082010-08-23 12:23:53 +0200550static inline bool iops_mode(struct cfq_data *cfqd)
551{
552 /*
553 * If we are not idling on queues and it is a NCQ drive, parallel
554 * execution of requests is on and measuring time is not possible
555 * in most of the cases until and unless we drive shallower queue
556 * depths and that becomes a performance bottleneck. In such cases
557 * switch to start providing fairness in terms of number of IOs.
558 */
559 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
560 return true;
561 else
562 return false;
563}
564
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100565static inline enum wl_prio_t cfqq_prio(struct cfq_queue *cfqq)
566{
567 if (cfq_class_idle(cfqq))
568 return IDLE_WORKLOAD;
569 if (cfq_class_rt(cfqq))
570 return RT_WORKLOAD;
571 return BE_WORKLOAD;
572}
573
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100574
575static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
576{
577 if (!cfq_cfqq_sync(cfqq))
578 return ASYNC_WORKLOAD;
579 if (!cfq_cfqq_idle_window(cfqq))
580 return SYNC_NOIDLE_WORKLOAD;
581 return SYNC_WORKLOAD;
582}
583
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500584static inline int cfq_group_busy_queues_wl(enum wl_prio_t wl,
585 struct cfq_data *cfqd,
586 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100587{
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500588 if (wl == IDLE_WORKLOAD)
589 return cfqg->service_tree_idle.count;
590
591 return cfqg->service_trees[wl][ASYNC_WORKLOAD].count
592 + cfqg->service_trees[wl][SYNC_NOIDLE_WORKLOAD].count
593 + cfqg->service_trees[wl][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100594}
595
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500596static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
597 struct cfq_group *cfqg)
598{
599 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count
600 + cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
601}
602
Jens Axboe165125e2007-07-24 09:28:11 +0200603static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800604static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700605 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800606 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200607
Tejun Heoc5869802011-12-14 00:33:41 +0100608static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
609{
610 /* cic->icq is the first member, %NULL will convert to %NULL */
611 return container_of(icq, struct cfq_io_cq, icq);
612}
613
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100614static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
615 struct io_context *ioc)
616{
617 if (ioc)
618 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
619 return NULL;
620}
621
Tejun Heoc5869802011-12-14 00:33:41 +0100622static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200623{
Jens Axboea6151c32009-10-07 20:02:57 +0200624 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200625}
626
Tejun Heoc5869802011-12-14 00:33:41 +0100627static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
628 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200629{
Jens Axboea6151c32009-10-07 20:02:57 +0200630 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200631}
632
Tejun Heoc5869802011-12-14 00:33:41 +0100633static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400634{
Tejun Heoc5869802011-12-14 00:33:41 +0100635 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400636}
637
Vasily Tarasov91fac312007-04-25 12:29:51 +0200638/*
639 * We regard a request as SYNC, if it's either a read or has the SYNC bit
640 * set (in which case it could also be direct WRITE).
641 */
Jens Axboea6151c32009-10-07 20:02:57 +0200642static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200643{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200644 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700648 * scheduler run of queue, if there are requests pending and no one in the
649 * driver that will restart queueing
650 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200651static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700652{
Jens Axboe7b679132008-05-30 12:23:07 +0200653 if (cfqd->busy_queues) {
654 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200655 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200656 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100660 * Scale schedule slice based on io priority. Use the sync time slice only
661 * if a queue is marked sync and has sync io queued. A sync queue with async
662 * io only, should not get full sync slice length.
663 */
Jens Axboea6151c32009-10-07 20:02:57 +0200664static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200665 unsigned short prio)
666{
667 const int base_slice = cfqd->cfq_slice[sync];
668
669 WARN_ON(prio >= IOPRIO_BE_NR);
670
671 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
672}
673
Jens Axboe44f7c162007-01-19 11:51:58 +1100674static inline int
675cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
676{
Jens Axboed9e76202007-04-20 14:27:50 +0200677 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100678}
679
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500680static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
681{
682 u64 d = delta << CFQ_SERVICE_SHIFT;
683
684 d = d * BLKIO_WEIGHT_DEFAULT;
685 do_div(d, cfqg->weight);
686 return d;
687}
688
689static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
690{
691 s64 delta = (s64)(vdisktime - min_vdisktime);
692 if (delta > 0)
693 min_vdisktime = vdisktime;
694
695 return min_vdisktime;
696}
697
698static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
699{
700 s64 delta = (s64)(vdisktime - min_vdisktime);
701 if (delta < 0)
702 min_vdisktime = vdisktime;
703
704 return min_vdisktime;
705}
706
707static void update_min_vdisktime(struct cfq_rb_root *st)
708{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500709 struct cfq_group *cfqg;
710
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500711 if (st->left) {
712 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100713 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
714 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500715 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500716}
717
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100718/*
719 * get averaged number of queues of RT/BE priority.
720 * average is updated, with a formula that gives more weight to higher numbers,
721 * to quickly follows sudden increases and decrease slowly
722 */
723
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500724static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
725 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100726{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100727 unsigned min_q, max_q;
728 unsigned mult = cfq_hist_divisor - 1;
729 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500730 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100731
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500732 min_q = min(cfqg->busy_queues_avg[rt], busy);
733 max_q = max(cfqg->busy_queues_avg[rt], busy);
734 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100735 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500736 return cfqg->busy_queues_avg[rt];
737}
738
739static inline unsigned
740cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
741{
742 struct cfq_rb_root *st = &cfqd->grp_service_tree;
743
744 return cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100745}
746
Shaohua Lic553f8e2011-01-14 08:41:03 +0100747static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700748cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100749{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100750 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
751 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500752 /*
753 * interested queues (we consider only the ones with the same
754 * priority class in the cfq group)
755 */
756 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
757 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100758 unsigned sync_slice = cfqd->cfq_slice[1];
759 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500760 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
761
762 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100763 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
764 /* scale low_slice according to IO priority
765 * and sync vs async */
766 unsigned low_slice =
767 min(slice, base_low_slice * slice / sync_slice);
768 /* the adapted slice value is scaled to fit all iqs
769 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500770 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100771 low_slice);
772 }
773 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100774 return slice;
775}
776
777static inline void
778cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
779{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700780 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100781
Vivek Goyaldae739e2009-12-03 12:59:45 -0500782 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100783 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500784 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200785 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +1100786}
787
788/*
789 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
790 * isn't valid until the first request from the dispatch is activated
791 * and the slice time set.
792 */
Jens Axboea6151c32009-10-07 20:02:57 +0200793static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100794{
795 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +0100796 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100797 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +0100798 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +1100799
Shaohua Lic1e44752010-11-08 15:01:02 +0100800 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +1100801}
802
803/*
Jens Axboe5e705372006-07-13 12:39:25 +0200804 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +0200806 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 */
Jens Axboe5e705372006-07-13 12:39:25 +0200808static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100809cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100811 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +0200813#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
814#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
815 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Jens Axboe5e705372006-07-13 12:39:25 +0200817 if (rq1 == NULL || rq1 == rq2)
818 return rq2;
819 if (rq2 == NULL)
820 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +0200821
Namhyung Kim229836b2011-05-24 10:23:21 +0200822 if (rq_is_sync(rq1) != rq_is_sync(rq2))
823 return rq_is_sync(rq1) ? rq1 : rq2;
824
Christoph Hellwig65299a32011-08-23 14:50:29 +0200825 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
826 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +0200827
Tejun Heo83096eb2009-05-07 22:24:39 +0900828 s1 = blk_rq_pos(rq1);
829 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /*
832 * by definition, 1KiB is 2 sectors
833 */
834 back_max = cfqd->cfq_back_max * 2;
835
836 /*
837 * Strict one way elevator _except_ in the case where we allow
838 * short backward seeks which are biased as twice the cost of a
839 * similar forward seek.
840 */
841 if (s1 >= last)
842 d1 = s1 - last;
843 else if (s1 + back_max >= last)
844 d1 = (last - s1) * cfqd->cfq_back_penalty;
845 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200846 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (s2 >= last)
849 d2 = s2 - last;
850 else if (s2 + back_max >= last)
851 d2 = (last - s2) * cfqd->cfq_back_penalty;
852 else
Andreas Mohre8a99052006-03-28 08:59:49 +0200853 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Andreas Mohre8a99052006-03-28 08:59:49 +0200857 /*
858 * By doing switch() on the bit mask "wrap" we avoid having to
859 * check two variables for all permutations: --> faster!
860 */
861 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +0200862 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200863 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +0200864 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200865 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +0200866 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200867 else {
868 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200869 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200870 else
Jens Axboe5e705372006-07-13 12:39:25 +0200871 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +0200872 }
873
874 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200875 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +0200876 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +0200877 return rq2;
878 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +0200879 default:
880 /*
881 * Since both rqs are wrapped,
882 * start with the one that's further behind head
883 * (--> only *one* back seek required),
884 * since back seek takes more time than forward.
885 */
886 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +0200887 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 else
Jens Axboe5e705372006-07-13 12:39:25 +0200889 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891}
892
Jens Axboe498d3aa22007-04-26 12:54:48 +0200893/*
894 * The below is leftmost cache rbtree addon
895 */
Jens Axboe08717142008-01-28 11:38:15 +0100896static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +0200897{
Vivek Goyal615f0252009-12-03 12:59:39 -0500898 /* Service tree is empty */
899 if (!root->count)
900 return NULL;
901
Jens Axboecc09e292007-04-26 12:53:50 +0200902 if (!root->left)
903 root->left = rb_first(&root->rb);
904
Jens Axboe08717142008-01-28 11:38:15 +0100905 if (root->left)
906 return rb_entry(root->left, struct cfq_queue, rb_node);
907
908 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +0200909}
910
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500911static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
912{
913 if (!root->left)
914 root->left = rb_first(&root->rb);
915
916 if (root->left)
917 return rb_entry_cfqg(root->left);
918
919 return NULL;
920}
921
Jens Axboea36e71f2009-04-15 12:15:11 +0200922static void rb_erase_init(struct rb_node *n, struct rb_root *root)
923{
924 rb_erase(n, root);
925 RB_CLEAR_NODE(n);
926}
927
Jens Axboecc09e292007-04-26 12:53:50 +0200928static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
929{
930 if (root->left == n)
931 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +0200932 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100933 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +0200934}
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936/*
937 * would be nice to take fifo expire time into account as well
938 */
Jens Axboe5e705372006-07-13 12:39:25 +0200939static struct request *
940cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
941 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Jens Axboe21183b02006-07-13 12:33:14 +0200943 struct rb_node *rbnext = rb_next(&last->rb_node);
944 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +0200945 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jens Axboe21183b02006-07-13 12:33:14 +0200947 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +0200950 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +0200951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +0200953 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200954 else {
955 rbnext = rb_first(&cfqq->sort_list);
956 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +0200957 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +0200958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +0100960 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Jens Axboed9e76202007-04-20 14:27:50 +0200963static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
964 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Jens Axboed9e76202007-04-20 14:27:50 +0200966 /*
967 * just an approximation, should be ok.
968 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500969 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +0100970 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +0200971}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500973static inline s64
974cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
975{
976 return cfqg->vdisktime - st->min_vdisktime;
977}
978
979static void
980__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
981{
982 struct rb_node **node = &st->rb.rb_node;
983 struct rb_node *parent = NULL;
984 struct cfq_group *__cfqg;
985 s64 key = cfqg_key(st, cfqg);
986 int left = 1;
987
988 while (*node != NULL) {
989 parent = *node;
990 __cfqg = rb_entry_cfqg(parent);
991
992 if (key < cfqg_key(st, __cfqg))
993 node = &parent->rb_left;
994 else {
995 node = &parent->rb_right;
996 left = 0;
997 }
998 }
999
1000 if (left)
1001 st->left = &cfqg->rb_node;
1002
1003 rb_link_node(&cfqg->rb_node, parent, node);
1004 rb_insert_color(&cfqg->rb_node, &st->rb);
1005}
1006
1007static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001008cfq_update_group_weight(struct cfq_group *cfqg)
1009{
1010 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1011 if (cfqg->needs_update) {
1012 cfqg->weight = cfqg->new_weight;
1013 cfqg->needs_update = false;
1014 }
1015}
1016
1017static void
1018cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1019{
1020 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1021
1022 cfq_update_group_weight(cfqg);
1023 __cfq_group_service_tree_add(st, cfqg);
1024 st->total_weight += cfqg->weight;
1025}
1026
1027static void
1028cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001029{
1030 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1031 struct cfq_group *__cfqg;
1032 struct rb_node *n;
1033
1034 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001035 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001036 return;
1037
1038 /*
1039 * Currently put the group at the end. Later implement something
1040 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001041 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001042 */
1043 n = rb_last(&st->rb);
1044 if (n) {
1045 __cfqg = rb_entry_cfqg(n);
1046 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1047 } else
1048 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001049 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001050}
1051
1052static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001053cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1054{
1055 st->total_weight -= cfqg->weight;
1056 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1057 cfq_rb_erase(&cfqg->rb_node, st);
1058}
1059
1060static void
1061cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001062{
1063 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1064
1065 BUG_ON(cfqg->nr_cfqq < 1);
1066 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001067
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001068 /* If there are other cfq queues under this group, don't delete it */
1069 if (cfqg->nr_cfqq)
1070 return;
1071
Vivek Goyal2868ef72009-12-03 12:59:48 -05001072 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001073 cfq_group_service_tree_del(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001074 cfqg->saved_workload_slice = 0;
Tejun Heoc1768262012-03-05 13:15:17 -08001075 cfq_blkiocg_update_dequeue_stats(cfqg_to_blkg(cfqg),
1076 &blkio_policy_cfq, 1);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001077}
1078
Justin TerAvest167400d2011-03-12 16:54:00 +01001079static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1080 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001081{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001082 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001083
1084 /*
1085 * Queue got expired before even a single request completed or
1086 * got expired immediately after first request completion.
1087 */
1088 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1089 /*
1090 * Also charge the seek time incurred to the group, otherwise
1091 * if there are mutiple queues in the group, each can dispatch
1092 * a single request on seeky media and cause lots of seek time
1093 * and group will never know it.
1094 */
1095 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1096 1);
1097 } else {
1098 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001099 if (slice_used > cfqq->allocated_slice) {
1100 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001101 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001102 }
1103 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1104 *unaccounted_time += cfqq->slice_start -
1105 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001106 }
1107
Vivek Goyaldae739e2009-12-03 12:59:45 -05001108 return slice_used;
1109}
1110
1111static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001112 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001113{
1114 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001115 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001116 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1117 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001118
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001119 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001120 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001121
Vivek Goyal02b35082010-08-23 12:23:53 +02001122 if (iops_mode(cfqd))
1123 charge = cfqq->slice_dispatch;
1124 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1125 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001126
1127 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001128 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001129 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001130 /* If a new weight was requested, update now, off tree */
1131 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001132
1133 /* This group is being expired. Save the context */
1134 if (time_after(cfqd->workload_expires, jiffies)) {
1135 cfqg->saved_workload_slice = cfqd->workload_expires
1136 - jiffies;
1137 cfqg->saved_workload = cfqd->serving_type;
1138 cfqg->saved_serving_prio = cfqd->serving_prio;
1139 } else
1140 cfqg->saved_workload_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001141
1142 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1143 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001144 cfq_log_cfqq(cfqq->cfqd, cfqq,
1145 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1146 used_sl, cfqq->slice_dispatch, charge,
1147 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heoc1768262012-03-05 13:15:17 -08001148 cfq_blkiocg_update_timeslice_used(cfqg_to_blkg(cfqg), &blkio_policy_cfq,
1149 used_sl, unaccounted_sl);
1150 cfq_blkiocg_set_start_empty_time(cfqg_to_blkg(cfqg), &blkio_policy_cfq);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001151}
1152
Tejun Heof51b8022012-03-05 13:15:05 -08001153/**
1154 * cfq_init_cfqg_base - initialize base part of a cfq_group
1155 * @cfqg: cfq_group to initialize
1156 *
1157 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1158 * is enabled or not.
1159 */
1160static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1161{
1162 struct cfq_rb_root *st;
1163 int i, j;
1164
1165 for_each_cfqg_st(cfqg, i, j, st)
1166 *st = CFQ_RB_ROOT;
1167 RB_CLEAR_NODE(&cfqg->rb_node);
1168
1169 cfqg->ttime.last_end_request = jiffies;
1170}
1171
Vivek Goyal25fb5162009-12-03 12:59:46 -05001172#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo60c2bc22012-04-01 14:38:43 -07001173static void cfq_update_blkio_group_weight(struct blkio_group *blkg,
Paul Bolle8aea4542011-06-06 05:07:54 +02001174 unsigned int weight)
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001175{
Tejun Heo03814112012-03-05 13:15:14 -08001176 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1177
Justin TerAvest8184f932011-03-17 16:12:36 +01001178 cfqg->new_weight = weight;
1179 cfqg->needs_update = true;
Vivek Goyalf8d461d2009-12-03 12:59:52 -05001180}
1181
Tejun Heo03814112012-03-05 13:15:14 -08001182static void cfq_init_blkio_group(struct blkio_group *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001183{
Tejun Heo03814112012-03-05 13:15:14 -08001184 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001185
Tejun Heof51b8022012-03-05 13:15:05 -08001186 cfq_init_cfqg_base(cfqg);
Tejun Heo03814112012-03-05 13:15:14 -08001187 cfqg->weight = blkg->blkcg->weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001188}
1189
1190/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001191 * Search for the cfq group current task belongs to. request_queue lock must
1192 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001193 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001194static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1195 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001196{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001197 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001198 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001199
Tejun Heocd1604f2012-03-05 13:15:06 -08001200 /* avoid lookup for the common case where there's no blkio cgroup */
1201 if (blkcg == &blkio_root_cgroup) {
1202 cfqg = cfqd->root_group;
1203 } else {
1204 struct blkio_group *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001205
Tejun Heoaaec55a2012-04-01 14:38:42 -07001206 blkg = blkg_lookup_create(blkcg, q, false);
Tejun Heocd1604f2012-03-05 13:15:06 -08001207 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001208 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001209 }
1210
Vivek Goyal25fb5162009-12-03 12:59:46 -05001211 return cfqg;
1212}
1213
1214static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1215{
1216 /* Currently, all async queues are mapped to root group */
1217 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001218 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001219
1220 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001221 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001222 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001223}
1224
Tejun Heo60c2bc22012-04-01 14:38:43 -07001225static u64 blkg_prfill_weight_device(struct seq_file *sf,
1226 struct blkg_policy_data *pd, int off)
1227{
1228 if (!pd->conf.weight)
1229 return 0;
1230 return __blkg_prfill_u64(sf, pd, pd->conf.weight);
1231}
1232
1233static int blkcg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1234 struct seq_file *sf)
1235{
1236 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp),
1237 blkg_prfill_weight_device, BLKIO_POLICY_PROP, 0,
1238 false);
1239 return 0;
1240}
1241
1242static int blkcg_print_weight(struct cgroup *cgrp, struct cftype *cft,
1243 struct seq_file *sf)
1244{
1245 seq_printf(sf, "%u\n", cgroup_to_blkio_cgroup(cgrp)->weight);
1246 return 0;
1247}
1248
1249static int blkcg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1250 const char *buf)
1251{
1252 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1253 struct blkg_policy_data *pd;
1254 struct blkg_conf_ctx ctx;
1255 int ret;
1256
1257 ret = blkg_conf_prep(blkcg, buf, &ctx);
1258 if (ret)
1259 return ret;
1260
1261 ret = -EINVAL;
1262 pd = ctx.blkg->pd[BLKIO_POLICY_PROP];
1263 if (pd && (!ctx.v || (ctx.v >= BLKIO_WEIGHT_MIN &&
1264 ctx.v <= BLKIO_WEIGHT_MAX))) {
1265 pd->conf.weight = ctx.v;
1266 cfq_update_blkio_group_weight(ctx.blkg, ctx.v ?: blkcg->weight);
1267 ret = 0;
1268 }
1269
1270 blkg_conf_finish(&ctx);
1271 return ret;
1272}
1273
1274static int blkcg_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1275{
1276 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1277 struct blkio_group *blkg;
1278 struct hlist_node *n;
1279
1280 if (val < BLKIO_WEIGHT_MIN || val > BLKIO_WEIGHT_MAX)
1281 return -EINVAL;
1282
1283 spin_lock_irq(&blkcg->lock);
1284 blkcg->weight = (unsigned int)val;
1285
1286 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
1287 struct blkg_policy_data *pd = blkg->pd[BLKIO_POLICY_PROP];
1288
1289 if (pd && !pd->conf.weight)
1290 cfq_update_blkio_group_weight(blkg, blkcg->weight);
1291 }
1292
1293 spin_unlock_irq(&blkcg->lock);
1294 return 0;
1295}
1296
1297#ifdef CONFIG_DEBUG_BLK_CGROUP
1298static u64 blkg_prfill_avg_queue_size(struct seq_file *sf,
1299 struct blkg_policy_data *pd, int off)
1300{
1301 u64 samples = blkg_stat_read(&pd->stats.avg_queue_size_samples);
1302 u64 v = 0;
1303
1304 if (samples) {
1305 v = blkg_stat_read(&pd->stats.avg_queue_size_sum);
1306 do_div(v, samples);
1307 }
1308 __blkg_prfill_u64(sf, pd, v);
1309 return 0;
1310}
1311
1312/* print avg_queue_size */
1313static int blkcg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1314 struct seq_file *sf)
1315{
1316 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
1317
1318 blkcg_print_blkgs(sf, blkcg, blkg_prfill_avg_queue_size,
1319 BLKIO_POLICY_PROP, 0, false);
1320 return 0;
1321}
1322#endif /* CONFIG_DEBUG_BLK_CGROUP */
1323
1324static struct cftype cfq_blkcg_files[] = {
1325 {
1326 .name = "weight_device",
1327 .read_seq_string = blkcg_print_weight_device,
1328 .write_string = blkcg_set_weight_device,
1329 .max_write_len = 256,
1330 },
1331 {
1332 .name = "weight",
1333 .read_seq_string = blkcg_print_weight,
1334 .write_u64 = blkcg_set_weight,
1335 },
1336 {
1337 .name = "time",
1338 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1339 offsetof(struct blkio_group_stats, time)),
1340 .read_seq_string = blkcg_print_stat,
1341 },
1342 {
1343 .name = "sectors",
1344 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1345 offsetof(struct blkio_group_stats_cpu, sectors)),
1346 .read_seq_string = blkcg_print_cpu_stat,
1347 },
1348 {
1349 .name = "io_service_bytes",
1350 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1351 offsetof(struct blkio_group_stats_cpu, service_bytes)),
1352 .read_seq_string = blkcg_print_cpu_rwstat,
1353 },
1354 {
1355 .name = "io_serviced",
1356 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1357 offsetof(struct blkio_group_stats_cpu, serviced)),
1358 .read_seq_string = blkcg_print_cpu_rwstat,
1359 },
1360 {
1361 .name = "io_service_time",
1362 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1363 offsetof(struct blkio_group_stats, service_time)),
1364 .read_seq_string = blkcg_print_rwstat,
1365 },
1366 {
1367 .name = "io_wait_time",
1368 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1369 offsetof(struct blkio_group_stats, wait_time)),
1370 .read_seq_string = blkcg_print_rwstat,
1371 },
1372 {
1373 .name = "io_merged",
1374 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1375 offsetof(struct blkio_group_stats, merged)),
1376 .read_seq_string = blkcg_print_rwstat,
1377 },
1378 {
1379 .name = "io_queued",
1380 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1381 offsetof(struct blkio_group_stats, queued)),
1382 .read_seq_string = blkcg_print_rwstat,
1383 },
1384#ifdef CONFIG_DEBUG_BLK_CGROUP
1385 {
1386 .name = "avg_queue_size",
1387 .read_seq_string = blkcg_print_avg_queue_size,
1388 },
1389 {
1390 .name = "group_wait_time",
1391 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1392 offsetof(struct blkio_group_stats, group_wait_time)),
1393 .read_seq_string = blkcg_print_stat,
1394 },
1395 {
1396 .name = "idle_time",
1397 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1398 offsetof(struct blkio_group_stats, idle_time)),
1399 .read_seq_string = blkcg_print_stat,
1400 },
1401 {
1402 .name = "empty_time",
1403 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1404 offsetof(struct blkio_group_stats, empty_time)),
1405 .read_seq_string = blkcg_print_stat,
1406 },
1407 {
1408 .name = "dequeue",
1409 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1410 offsetof(struct blkio_group_stats, dequeue)),
1411 .read_seq_string = blkcg_print_stat,
1412 },
1413 {
1414 .name = "unaccounted_time",
1415 .private = BLKCG_STAT_PRIV(BLKIO_POLICY_PROP,
1416 offsetof(struct blkio_group_stats, unaccounted_time)),
1417 .read_seq_string = blkcg_print_stat,
1418 },
1419#endif /* CONFIG_DEBUG_BLK_CGROUP */
1420 { } /* terminate */
1421};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001422#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001423static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
1424 struct blkio_cgroup *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001425{
Tejun Heof51b8022012-03-05 13:15:05 -08001426 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001427}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001428
Vivek Goyal25fb5162009-12-03 12:59:46 -05001429static inline void
1430cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1431 cfqq->cfqg = cfqg;
1432}
1433
1434#endif /* GROUP_IOSCHED */
1435
Jens Axboe498d3aa22007-04-26 12:54:48 +02001436/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001437 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001438 * requests waiting to be processed. It is sorted in the order that
1439 * we will service the queues.
1440 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001441static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001442 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001443{
Jens Axboe08717142008-01-28 11:38:15 +01001444 struct rb_node **p, *parent;
1445 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001446 unsigned long rb_key;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001447 struct cfq_rb_root *service_tree;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001448 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001449 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001450
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001451 service_tree = service_tree_for(cfqq->cfqg, cfqq_prio(cfqq),
Vivek Goyal65b32a52009-12-16 17:52:59 -05001452 cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001453 if (cfq_class_idle(cfqq)) {
1454 rb_key = CFQ_IDLE_DELAY;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001455 parent = rb_last(&service_tree->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001456 if (parent && parent != &cfqq->rb_node) {
1457 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1458 rb_key += __cfqq->rb_key;
1459 } else
1460 rb_key += jiffies;
1461 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001462 /*
1463 * Get our rb key offset. Subtract any residual slice
1464 * value carried from last service. A negative resid
1465 * count indicates slice overrun, and this should position
1466 * the next service time further away in the tree.
1467 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001468 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001469 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001470 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001471 } else {
1472 rb_key = -HZ;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001473 __cfqq = cfq_rb_first(service_tree);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001474 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1475 }
Jens Axboed9e76202007-04-20 14:27:50 +02001476
1477 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001478 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001479 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001480 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001481 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001482 if (rb_key == cfqq->rb_key &&
1483 cfqq->service_tree == service_tree)
Jens Axboed9e76202007-04-20 14:27:50 +02001484 return;
Jens Axboe53b03742006-07-28 09:48:51 +02001485
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001486 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1487 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001488 }
Jens Axboed9e76202007-04-20 14:27:50 +02001489
Jens Axboe498d3aa22007-04-26 12:54:48 +02001490 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001491 parent = NULL;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001492 cfqq->service_tree = service_tree;
1493 p = &service_tree->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001494 while (*p) {
Jens Axboe67060e32007-04-18 20:13:32 +02001495 struct rb_node **n;
Jens Axboecc09e292007-04-26 12:53:50 +02001496
Jens Axboed9e76202007-04-20 14:27:50 +02001497 parent = *p;
1498 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1499
Jens Axboe0c534e02007-04-18 20:01:57 +02001500 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001501 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001502 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001503 if (time_before(rb_key, __cfqq->rb_key))
Jens Axboe67060e32007-04-18 20:13:32 +02001504 n = &(*p)->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001505 else {
Jens Axboe67060e32007-04-18 20:13:32 +02001506 n = &(*p)->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001507 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001508 }
Jens Axboe67060e32007-04-18 20:13:32 +02001509
1510 p = n;
Jens Axboed9e76202007-04-20 14:27:50 +02001511 }
1512
Jens Axboecc09e292007-04-26 12:53:50 +02001513 if (left)
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001514 service_tree->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001515
Jens Axboed9e76202007-04-20 14:27:50 +02001516 cfqq->rb_key = rb_key;
1517 rb_link_node(&cfqq->rb_node, parent, p);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001518 rb_insert_color(&cfqq->rb_node, &service_tree->rb);
1519 service_tree->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001520 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001521 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001522 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Jens Axboea36e71f2009-04-15 12:15:11 +02001525static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001526cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1527 sector_t sector, struct rb_node **ret_parent,
1528 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001529{
Jens Axboea36e71f2009-04-15 12:15:11 +02001530 struct rb_node **p, *parent;
1531 struct cfq_queue *cfqq = NULL;
1532
1533 parent = NULL;
1534 p = &root->rb_node;
1535 while (*p) {
1536 struct rb_node **n;
1537
1538 parent = *p;
1539 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1540
1541 /*
1542 * Sort strictly based on sector. Smallest to the left,
1543 * largest to the right.
1544 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001545 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001546 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001547 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001548 n = &(*p)->rb_left;
1549 else
1550 break;
1551 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001552 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001553 }
1554
1555 *ret_parent = parent;
1556 if (rb_link)
1557 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001558 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001559}
1560
1561static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1562{
Jens Axboea36e71f2009-04-15 12:15:11 +02001563 struct rb_node **p, *parent;
1564 struct cfq_queue *__cfqq;
1565
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001566 if (cfqq->p_root) {
1567 rb_erase(&cfqq->p_node, cfqq->p_root);
1568 cfqq->p_root = NULL;
1569 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001570
1571 if (cfq_class_idle(cfqq))
1572 return;
1573 if (!cfqq->next_rq)
1574 return;
1575
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001576 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001577 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1578 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001579 if (!__cfqq) {
1580 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001581 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1582 } else
1583 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001584}
1585
Jens Axboe498d3aa22007-04-26 12:54:48 +02001586/*
1587 * Update cfqq's position in the service tree.
1588 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001589static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001590{
Jens Axboe6d048f52007-04-25 12:44:27 +02001591 /*
1592 * Resorting requires the cfqq to be on the RR list already.
1593 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001594 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001595 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001596 cfq_prio_tree_add(cfqd, cfqq);
1597 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/*
1601 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001602 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 */
Jens Axboefebffd62008-01-28 13:19:43 +01001604static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Jens Axboe7b679132008-05-30 12:23:07 +02001606 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001607 BUG_ON(cfq_cfqq_on_rr(cfqq));
1608 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001610 if (cfq_cfqq_sync(cfqq))
1611 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Jens Axboeedd75ff2007-04-19 12:03:34 +02001613 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Jens Axboe498d3aa22007-04-26 12:54:48 +02001616/*
1617 * Called when the cfqq no longer has requests pending, remove it from
1618 * the service tree.
1619 */
Jens Axboefebffd62008-01-28 13:19:43 +01001620static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Jens Axboe7b679132008-05-30 12:23:07 +02001622 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001623 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1624 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001626 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1627 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1628 cfqq->service_tree = NULL;
1629 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001630 if (cfqq->p_root) {
1631 rb_erase(&cfqq->p_node, cfqq->p_root);
1632 cfqq->p_root = NULL;
1633 }
Jens Axboed9e76202007-04-20 14:27:50 +02001634
Justin TerAvest8184f932011-03-17 16:12:36 +01001635 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 BUG_ON(!cfqd->busy_queues);
1637 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001638 if (cfq_cfqq_sync(cfqq))
1639 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642/*
1643 * rb tree support functions
1644 */
Jens Axboefebffd62008-01-28 13:19:43 +01001645static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Jens Axboe5e705372006-07-13 12:39:25 +02001647 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02001648 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Jens Axboeb4878f22005-10-20 16:42:29 +02001650 BUG_ON(!cfqq->queued[sync]);
1651 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jens Axboe5e705372006-07-13 12:39:25 +02001653 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Vivek Goyalf04a6422009-12-03 12:59:40 -05001655 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
1656 /*
1657 * Queue will be deleted from service tree when we actually
1658 * expire it later. Right now just remove it from prio tree
1659 * as it is empty.
1660 */
1661 if (cfqq->p_root) {
1662 rb_erase(&cfqq->p_node, cfqq->p_root);
1663 cfqq->p_root = NULL;
1664 }
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Jens Axboe5e705372006-07-13 12:39:25 +02001668static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Jens Axboe5e705372006-07-13 12:39:25 +02001670 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02001672 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jens Axboe5380a102006-07-13 12:37:56 +02001674 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jeff Moyer796d5112011-06-02 21:19:05 +02001676 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01001677
1678 if (!cfq_cfqq_on_rr(cfqq))
1679 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02001680
1681 /*
1682 * check if this request is a better next-serve candidate
1683 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001684 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001685 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02001686
1687 /*
1688 * adjust priority tree position, if ->next_rq changes
1689 */
1690 if (prev != cfqq->next_rq)
1691 cfq_prio_tree_add(cfqd, cfqq);
1692
Jens Axboe5044eed2007-04-25 11:53:48 +02001693 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Jens Axboefebffd62008-01-28 13:19:43 +01001696static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
Jens Axboe5380a102006-07-13 12:37:56 +02001698 elv_rb_del(&cfqq->sort_list, rq);
1699 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo03814112012-03-05 13:15:14 -08001700 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001701 &blkio_policy_cfq, rq_data_dir(rq),
1702 rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02001703 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08001704 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001705 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08001706 cfqg_to_blkg(cfqq->cfqd->serving_group),
1707 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Jens Axboe206dc692006-03-28 13:03:44 +02001710static struct request *
1711cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Jens Axboe206dc692006-03-28 13:03:44 +02001713 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01001714 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02001715 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Jens Axboe4ac845a2008-01-24 08:44:49 +01001717 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02001718 if (!cic)
1719 return NULL;
1720
1721 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02001722 if (cfqq) {
1723 sector_t sector = bio->bi_sector + bio_sectors(bio);
1724
Jens Axboe21183b02006-07-13 12:33:14 +02001725 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02001726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return NULL;
1729}
1730
Jens Axboe165125e2007-07-24 09:28:11 +02001731static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02001732{
1733 struct cfq_data *cfqd = q->elevator->elevator_data;
1734
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001735 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02001736 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001737 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02001738
Tejun Heo5b936292009-05-07 22:24:38 +09001739 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02001740}
1741
Jens Axboe165125e2007-07-24 09:28:11 +02001742static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Jens Axboe22e2c502005-06-27 10:55:12 +02001744 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001746 WARN_ON(!cfqd->rq_in_driver);
1747 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02001748 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01001749 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Jens Axboeb4878f22005-10-20 16:42:29 +02001752static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Jens Axboe5e705372006-07-13 12:39:25 +02001754 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02001755
Jens Axboe5e705372006-07-13 12:39:25 +02001756 if (cfqq->next_rq == rq)
1757 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Jens Axboeb4878f22005-10-20 16:42:29 +02001759 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02001760 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02001761
Aaron Carroll45333d52008-08-26 15:52:36 +02001762 cfqq->cfqd->rq_queued--;
Tejun Heo03814112012-03-05 13:15:14 -08001763 cfq_blkiocg_update_io_remove_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001764 &blkio_policy_cfq, rq_data_dir(rq),
1765 rq_is_sync(rq));
Christoph Hellwig65299a32011-08-23 14:50:29 +02001766 if (rq->cmd_flags & REQ_PRIO) {
1767 WARN_ON(!cfqq->prio_pending);
1768 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Jens Axboe165125e2007-07-24 09:28:11 +02001772static int cfq_merge(struct request_queue *q, struct request **req,
1773 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct cfq_data *cfqd = q->elevator->elevator_data;
1776 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Jens Axboe206dc692006-03-28 13:03:44 +02001778 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02001779 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02001780 *req = __rq;
1781 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
1784 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Jens Axboe165125e2007-07-24 09:28:11 +02001787static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02001788 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Jens Axboe21183b02006-07-13 12:33:14 +02001790 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02001791 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jens Axboe5e705372006-07-13 12:39:25 +02001793 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Divyesh Shah812d4022010-04-08 21:14:23 -07001797static void cfq_bio_merged(struct request_queue *q, struct request *req,
1798 struct bio *bio)
1799{
Tejun Heo03814112012-03-05 13:15:14 -08001800 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(req)),
Tejun Heoc1768262012-03-05 13:15:17 -08001801 &blkio_policy_cfq, bio_data_dir(bio),
1802 cfq_bio_sync(bio));
Divyesh Shah812d4022010-04-08 21:14:23 -07001803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805static void
Jens Axboe165125e2007-07-24 09:28:11 +02001806cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct request *next)
1808{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001809 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001810 struct cfq_data *cfqd = q->elevator->elevator_data;
1811
Jens Axboe22e2c502005-06-27 10:55:12 +02001812 /*
1813 * reposition in fifo if next is older than rq
1814 */
1815 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jens Axboe30996f42009-10-05 11:03:39 +02001816 time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001817 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02001818 rq_set_fifo_time(rq, rq_fifo_time(next));
1819 }
Jens Axboe22e2c502005-06-27 10:55:12 +02001820
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001821 if (cfqq->next_rq == next)
1822 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02001823 cfq_remove_request(next);
Tejun Heo03814112012-03-05 13:15:14 -08001824 cfq_blkiocg_update_io_merged_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08001825 &blkio_policy_cfq, rq_data_dir(next),
1826 rq_is_sync(next));
Shaohua Li4a0b75c2011-12-16 14:00:22 +01001827
1828 cfqq = RQ_CFQQ(next);
1829 /*
1830 * all requests of this queue are merged to other queues, delete it
1831 * from the service tree. If it's the active_queue,
1832 * cfq_dispatch_requests() will choose to expire it or do idle
1833 */
1834 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
1835 cfqq != cfqd->active_queue)
1836 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001837}
1838
Jens Axboe165125e2007-07-24 09:28:11 +02001839static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01001840 struct bio *bio)
1841{
1842 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01001843 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01001844 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01001845
1846 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01001847 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01001848 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02001849 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02001850 return false;
Jens Axboeda775262006-12-20 11:04:12 +01001851
1852 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01001853 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01001854 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01001855 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01001856 cic = cfq_cic_lookup(cfqd, current->io_context);
1857 if (!cic)
1858 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01001859
Vasily Tarasov91fac312007-04-25 12:29:51 +02001860 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02001861 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01001862}
1863
Divyesh Shah812df482010-04-08 21:15:35 -07001864static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1865{
1866 del_timer(&cfqd->idle_slice_timer);
Tejun Heoc1768262012-03-05 13:15:17 -08001867 cfq_blkiocg_update_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
1868 &blkio_policy_cfq);
Divyesh Shah812df482010-04-08 21:15:35 -07001869}
1870
Jens Axboefebffd62008-01-28 13:19:43 +01001871static void __cfq_set_active_queue(struct cfq_data *cfqd,
1872 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02001873{
1874 if (cfqq) {
Divyesh Shahb1ffe732010-03-25 15:45:03 +01001875 cfq_log_cfqq(cfqd, cfqq, "set_active wl_prio:%d wl_type:%d",
1876 cfqd->serving_prio, cfqd->serving_type);
Tejun Heoc1768262012-03-05 13:15:17 -08001877 cfq_blkiocg_update_avg_queue_size_stats(cfqg_to_blkg(cfqq->cfqg),
1878 &blkio_policy_cfq);
Justin TerAvest62a37f62011-03-23 08:25:44 +01001879 cfqq->slice_start = 0;
1880 cfqq->dispatch_start = jiffies;
1881 cfqq->allocated_slice = 0;
1882 cfqq->slice_end = 0;
1883 cfqq->slice_dispatch = 0;
1884 cfqq->nr_sectors = 0;
1885
1886 cfq_clear_cfqq_wait_request(cfqq);
1887 cfq_clear_cfqq_must_dispatch(cfqq);
1888 cfq_clear_cfqq_must_alloc_slice(cfqq);
1889 cfq_clear_cfqq_fifo_expire(cfqq);
1890 cfq_mark_cfqq_slice_new(cfqq);
1891
1892 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02001893 }
1894
1895 cfqd->active_queue = cfqq;
1896}
1897
1898/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001899 * current cfqq expired its slice (or was too idle), select new one
1900 */
1901static void
1902__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001903 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001904{
Jens Axboe7b679132008-05-30 12:23:07 +02001905 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
1906
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001907 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07001908 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001909
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001910 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05001911 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001912
1913 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01001914 * If this cfqq is shared between multiple processes, check to
1915 * make sure that those processes are still issuing I/Os within
1916 * the mean seek distance. If not, it may be time to break the
1917 * queues apart again.
1918 */
1919 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
1920 cfq_mark_cfqq_split_coop(cfqq);
1921
1922 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02001923 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001924 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01001925 if (timed_out) {
1926 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07001927 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001928 else
1929 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02001930 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
1931 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001932
Vivek Goyale5ff0822010-04-26 19:25:11 +02001933 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001934
Vivek Goyalf04a6422009-12-03 12:59:40 -05001935 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
1936 cfq_del_cfqq_rr(cfqd, cfqq);
1937
Jens Axboeedd75ff2007-04-19 12:03:34 +02001938 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001939
1940 if (cfqq == cfqd->active_queue)
1941 cfqd->active_queue = NULL;
1942
1943 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01001944 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001945 cfqd->active_cic = NULL;
1946 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001947}
1948
Vivek Goyale5ff0822010-04-26 19:25:11 +02001949static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001950{
1951 struct cfq_queue *cfqq = cfqd->active_queue;
1952
1953 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02001954 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01001955}
1956
Jens Axboe498d3aa22007-04-26 12:54:48 +02001957/*
1958 * Get next queue for service. Unless we have a queue preemption,
1959 * we'll simply select the first cfqq in the service tree.
1960 */
Jens Axboe6d048f52007-04-25 12:44:27 +02001961static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02001962{
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001963 struct cfq_rb_root *service_tree =
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001964 service_tree_for(cfqd->serving_group, cfqd->serving_prio,
Vivek Goyal65b32a52009-12-16 17:52:59 -05001965 cfqd->serving_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02001966
Vivek Goyalf04a6422009-12-03 12:59:40 -05001967 if (!cfqd->rq_queued)
1968 return NULL;
1969
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001970 /* There is nothing to dispatch */
1971 if (!service_tree)
1972 return NULL;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001973 if (RB_EMPTY_ROOT(&service_tree->rb))
1974 return NULL;
1975 return cfq_rb_first(service_tree);
Jens Axboe6d048f52007-04-25 12:44:27 +02001976}
1977
Vivek Goyalf04a6422009-12-03 12:59:40 -05001978static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
1979{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001980 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05001981 struct cfq_queue *cfqq;
1982 int i, j;
1983 struct cfq_rb_root *st;
1984
1985 if (!cfqd->rq_queued)
1986 return NULL;
1987
Vivek Goyal25fb5162009-12-03 12:59:46 -05001988 cfqg = cfq_get_next_cfqg(cfqd);
1989 if (!cfqg)
1990 return NULL;
1991
Vivek Goyalf04a6422009-12-03 12:59:40 -05001992 for_each_cfqg_st(cfqg, i, j, st)
1993 if ((cfqq = cfq_rb_first(st)) != NULL)
1994 return cfqq;
1995 return NULL;
1996}
1997
Jens Axboe498d3aa22007-04-26 12:54:48 +02001998/*
1999 * Get and set a new active queue for service.
2000 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002001static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2002 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002003{
Jens Axboee00ef792009-11-04 08:54:55 +01002004 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002005 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002006
Jens Axboe22e2c502005-06-27 10:55:12 +02002007 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002008 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002009}
2010
Jens Axboed9e76202007-04-20 14:27:50 +02002011static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2012 struct request *rq)
2013{
Tejun Heo83096eb2009-05-07 22:24:39 +09002014 if (blk_rq_pos(rq) >= cfqd->last_position)
2015 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002016 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002017 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002018}
2019
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002020static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002021 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002022{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002023 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002024}
2025
Jens Axboea36e71f2009-04-15 12:15:11 +02002026static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2027 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002028{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002029 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002030 struct rb_node *parent, *node;
2031 struct cfq_queue *__cfqq;
2032 sector_t sector = cfqd->last_position;
2033
2034 if (RB_EMPTY_ROOT(root))
2035 return NULL;
2036
2037 /*
2038 * First, if we find a request starting at the end of the last
2039 * request, choose it.
2040 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002041 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002042 if (__cfqq)
2043 return __cfqq;
2044
2045 /*
2046 * If the exact sector wasn't found, the parent of the NULL leaf
2047 * will contain the closest sector.
2048 */
2049 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002050 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002051 return __cfqq;
2052
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002053 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002054 node = rb_next(&__cfqq->p_node);
2055 else
2056 node = rb_prev(&__cfqq->p_node);
2057 if (!node)
2058 return NULL;
2059
2060 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002061 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002062 return __cfqq;
2063
2064 return NULL;
2065}
2066
2067/*
2068 * cfqd - obvious
2069 * cur_cfqq - passed in so that we don't decide that the current queue is
2070 * closely cooperating with itself.
2071 *
2072 * So, basically we're assuming that that cur_cfqq has dispatched at least
2073 * one request, and that cfqd->last_position reflects a position on the disk
2074 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2075 * assumption.
2076 */
2077static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002078 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002079{
2080 struct cfq_queue *cfqq;
2081
Divyesh Shah39c01b22010-03-25 15:45:57 +01002082 if (cfq_class_idle(cur_cfqq))
2083 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002084 if (!cfq_cfqq_sync(cur_cfqq))
2085 return NULL;
2086 if (CFQQ_SEEKY(cur_cfqq))
2087 return NULL;
2088
Jens Axboea36e71f2009-04-15 12:15:11 +02002089 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002090 * Don't search priority tree if it's the only queue in the group.
2091 */
2092 if (cur_cfqq->cfqg->nr_cfqq == 1)
2093 return NULL;
2094
2095 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002096 * We should notice if some of the queues are cooperating, eg
2097 * working closely on the same area of the disk. In that case,
2098 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002099 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002100 cfqq = cfqq_close(cfqd, cur_cfqq);
2101 if (!cfqq)
2102 return NULL;
2103
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002104 /* If new queue belongs to different cfq_group, don't choose it */
2105 if (cur_cfqq->cfqg != cfqq->cfqg)
2106 return NULL;
2107
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002108 /*
2109 * It only makes sense to merge sync queues.
2110 */
2111 if (!cfq_cfqq_sync(cfqq))
2112 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002113 if (CFQQ_SEEKY(cfqq))
2114 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002115
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002116 /*
2117 * Do not merge queues of different priority classes
2118 */
2119 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2120 return NULL;
2121
Jens Axboea36e71f2009-04-15 12:15:11 +02002122 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002123}
2124
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002125/*
2126 * Determine whether we should enforce idle window for this queue.
2127 */
2128
2129static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2130{
2131 enum wl_prio_t prio = cfqq_prio(cfqq);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002132 struct cfq_rb_root *service_tree = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002133
Vivek Goyalf04a6422009-12-03 12:59:40 -05002134 BUG_ON(!service_tree);
2135 BUG_ON(!service_tree->count);
2136
Vivek Goyalb6508c12010-08-23 12:23:33 +02002137 if (!cfqd->cfq_slice_idle)
2138 return false;
2139
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002140 /* We never do for idle class queues. */
2141 if (prio == IDLE_WORKLOAD)
2142 return false;
2143
2144 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002145 if (cfq_cfqq_idle_window(cfqq) &&
2146 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002147 return true;
2148
2149 /*
2150 * Otherwise, we do only if they are the last ones
2151 * in their service tree.
2152 */
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02002153 if (service_tree->count == 1 && cfq_cfqq_sync(cfqq) &&
2154 !cfq_io_thinktime_big(cfqd, &service_tree->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002155 return true;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002156 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d",
2157 service_tree->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002158 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002159}
2160
Jens Axboe6d048f52007-04-25 12:44:27 +02002161static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002162{
Jens Axboe17926692007-01-19 11:59:30 +11002163 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002164 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002165 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002166
Jens Axboea68bbdd2008-09-24 13:03:33 +02002167 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002168 * SSD device without seek penalty, disable idling. But only do so
2169 * for devices that support queuing, otherwise we still have a problem
2170 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002171 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002172 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002173 return;
2174
Jens Axboedd67d052006-06-21 09:36:18 +02002175 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002176 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002177
2178 /*
2179 * idle is disabled, either manually or by past process history
2180 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002181 if (!cfq_should_idle(cfqd, cfqq)) {
2182 /* no queue idling. Check for group idling */
2183 if (cfqd->cfq_group_idle)
2184 group_idle = cfqd->cfq_group_idle;
2185 else
2186 return;
2187 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002188
Jens Axboe22e2c502005-06-27 10:55:12 +02002189 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002190 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002191 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002192 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002193 return;
2194
2195 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002196 * task has exited, don't wait
2197 */
Jens Axboe206dc692006-03-28 13:03:44 +02002198 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002199 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002200 return;
2201
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002202 /*
2203 * If our average think time is larger than the remaining time
2204 * slice, then don't idle. This avoids overrunning the allotted
2205 * time slice.
2206 */
Shaohua Li383cd722011-07-12 14:24:35 +02002207 if (sample_valid(cic->ttime.ttime_samples) &&
2208 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002209 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002210 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002211 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002212 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002213
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002214 /* There are other queues in the group, don't do group idle */
2215 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2216 return;
2217
Jens Axboe3b181522005-06-27 10:56:24 +02002218 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002219
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002220 if (group_idle)
2221 sl = cfqd->cfq_group_idle;
2222 else
2223 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002224
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002225 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heoc1768262012-03-05 13:15:17 -08002226 cfq_blkiocg_update_set_idle_time_stats(cfqg_to_blkg(cfqq->cfqg),
2227 &blkio_policy_cfq);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002228 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2229 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Jens Axboe498d3aa22007-04-26 12:54:48 +02002232/*
2233 * Move request from internal lists to the request queue dispatch list.
2234 */
Jens Axboe165125e2007-07-24 09:28:11 +02002235static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002237 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002238 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002239
Jens Axboe7b679132008-05-30 12:23:07 +02002240 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2241
Jeff Moyer06d21882009-09-11 17:08:59 +02002242 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002243 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002244 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002245 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002246 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002247
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002248 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002249 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo03814112012-03-05 13:15:14 -08002250 cfq_blkiocg_update_dispatch_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08002251 &blkio_policy_cfq, blk_rq_bytes(rq),
2252 rq_data_dir(rq), rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
2255/*
2256 * return expired entry, or NULL to just start from scratch in rbtree
2257 */
Jens Axboefebffd62008-01-28 13:19:43 +01002258static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Jens Axboe30996f42009-10-05 11:03:39 +02002260 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Jens Axboe3b181522005-06-27 10:56:24 +02002262 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002264
2265 cfq_mark_cfqq_fifo_expire(cfqq);
2266
Jens Axboe89850f72006-07-22 16:48:31 +02002267 if (list_empty(&cfqq->fifo))
2268 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Jens Axboe89850f72006-07-22 16:48:31 +02002270 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002271 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002272 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Jens Axboe30996f42009-10-05 11:03:39 +02002274 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002275 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Jens Axboe22e2c502005-06-27 10:55:12 +02002278static inline int
2279cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2280{
2281 const int base_rq = cfqd->cfq_slice_async_rq;
2282
2283 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2284
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002285 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002286}
2287
2288/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002289 * Must be called with the queue_lock held.
2290 */
2291static int cfqq_process_refs(struct cfq_queue *cfqq)
2292{
2293 int process_refs, io_refs;
2294
2295 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002296 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002297 BUG_ON(process_refs < 0);
2298 return process_refs;
2299}
2300
2301static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2302{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002303 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002304 struct cfq_queue *__cfqq;
2305
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002306 /*
2307 * If there are no process references on the new_cfqq, then it is
2308 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2309 * chain may have dropped their last reference (not just their
2310 * last process reference).
2311 */
2312 if (!cfqq_process_refs(new_cfqq))
2313 return;
2314
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002315 /* Avoid a circular list and skip interim queue merges */
2316 while ((__cfqq = new_cfqq->new_cfqq)) {
2317 if (__cfqq == cfqq)
2318 return;
2319 new_cfqq = __cfqq;
2320 }
2321
2322 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002323 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002324 /*
2325 * If the process for the cfqq has gone away, there is no
2326 * sense in merging the queues.
2327 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002328 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002329 return;
2330
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002331 /*
2332 * Merge in the direction of the lesser amount of work.
2333 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002334 if (new_process_refs >= process_refs) {
2335 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002336 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002337 } else {
2338 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002339 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002340 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002341}
2342
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002343static enum wl_type_t cfq_choose_wl(struct cfq_data *cfqd,
Vivek Goyal65b32a52009-12-16 17:52:59 -05002344 struct cfq_group *cfqg, enum wl_prio_t prio)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002345{
2346 struct cfq_queue *queue;
2347 int i;
2348 bool key_valid = false;
2349 unsigned long lowest_key = 0;
2350 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2351
Vivek Goyal65b32a52009-12-16 17:52:59 -05002352 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2353 /* select the one with lowest rb_key */
2354 queue = cfq_rb_first(service_tree_for(cfqg, prio, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002355 if (queue &&
2356 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2357 lowest_key = queue->rb_key;
2358 cur_best = i;
2359 key_valid = true;
2360 }
2361 }
2362
2363 return cur_best;
2364}
2365
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002366static void choose_service_tree(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002367{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002368 unsigned slice;
2369 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002370 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002371 unsigned group_slice;
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002372 enum wl_prio_t original_prio = cfqd->serving_prio;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002373
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002374 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002375 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002376 cfqd->serving_prio = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002377 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002378 cfqd->serving_prio = BE_WORKLOAD;
2379 else {
2380 cfqd->serving_prio = IDLE_WORKLOAD;
2381 cfqd->workload_expires = jiffies + 1;
2382 return;
2383 }
2384
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002385 if (original_prio != cfqd->serving_prio)
2386 goto new_workload;
2387
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002388 /*
2389 * For RT and BE, we have to choose also the type
2390 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2391 * expiration time
2392 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002393 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002394 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002395
2396 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002397 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002398 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002399 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002400 return;
2401
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002402new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002403 /* otherwise select new workload type */
2404 cfqd->serving_type =
Vivek Goyal65b32a52009-12-16 17:52:59 -05002405 cfq_choose_wl(cfqd, cfqg, cfqd->serving_prio);
2406 st = service_tree_for(cfqg, cfqd->serving_prio, cfqd->serving_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002407 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002408
2409 /*
2410 * the workload slice is computed as a fraction of target latency
2411 * proportional to the number of queues in that workload, over
2412 * all the queues in the same priority class
2413 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002414 group_slice = cfq_group_slice(cfqd, cfqg);
2415
2416 slice = group_slice * count /
2417 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_prio],
2418 cfq_group_busy_queues_wl(cfqd->serving_prio, cfqd, cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002419
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002420 if (cfqd->serving_type == ASYNC_WORKLOAD) {
2421 unsigned int tmp;
2422
2423 /*
2424 * Async queues are currently system wide. Just taking
2425 * proportion of queues with-in same group will lead to higher
2426 * async ratio system wide as generally root group is going
2427 * to have higher weight. A more accurate thing would be to
2428 * calculate system wide asnc/sync ratio.
2429 */
2430 tmp = cfq_target_latency * cfqg_busy_async_queues(cfqd, cfqg);
2431 tmp = tmp/cfqd->busy_queues;
2432 slice = min_t(unsigned, slice, tmp);
2433
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002434 /* async workload slice is scaled down according to
2435 * the sync/async slice ratio. */
2436 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002437 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002438 /* sync workload slice is at least 2 * cfq_slice_idle */
2439 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2440
2441 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002442 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002443 cfqd->workload_expires = jiffies + slice;
2444}
2445
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002446static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2447{
2448 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002449 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002450
2451 if (RB_EMPTY_ROOT(&st->rb))
2452 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002453 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002454 update_min_vdisktime(st);
2455 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002456}
2457
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002458static void cfq_choose_cfqg(struct cfq_data *cfqd)
2459{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002460 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2461
2462 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002463
2464 /* Restore the workload type data */
2465 if (cfqg->saved_workload_slice) {
2466 cfqd->workload_expires = jiffies + cfqg->saved_workload_slice;
2467 cfqd->serving_type = cfqg->saved_workload;
2468 cfqd->serving_prio = cfqg->saved_serving_prio;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002469 } else
2470 cfqd->workload_expires = jiffies - 1;
2471
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002472 choose_service_tree(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002473}
2474
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002475/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002476 * Select a queue for service. If we have a current active queue,
2477 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002478 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002479static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002480{
Jens Axboea36e71f2009-04-15 12:15:11 +02002481 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002482
2483 cfqq = cfqd->active_queue;
2484 if (!cfqq)
2485 goto new_queue;
2486
Vivek Goyalf04a6422009-12-03 12:59:40 -05002487 if (!cfqd->rq_queued)
2488 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002489
2490 /*
2491 * We were waiting for group to get backlogged. Expire the queue
2492 */
2493 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2494 goto expire;
2495
Jens Axboe22e2c502005-06-27 10:55:12 +02002496 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002497 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002498 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002499 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2500 /*
2501 * If slice had not expired at the completion of last request
2502 * we might not have turned on wait_busy flag. Don't expire
2503 * the queue yet. Allow the group to get backlogged.
2504 *
2505 * The very fact that we have used the slice, that means we
2506 * have been idling all along on this queue and it should be
2507 * ok to wait for this request to complete.
2508 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002509 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2510 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2511 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002512 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002513 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002514 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002515 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002516
2517 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002518 * The active queue has requests and isn't expired, allow it to
2519 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002520 */
Jens Axboedd67d052006-06-21 09:36:18 +02002521 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002522 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002523
2524 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002525 * If another queue has a request waiting within our mean seek
2526 * distance, let it run. The expire code will check for close
2527 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002528 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002529 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002530 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002531 if (new_cfqq) {
2532 if (!cfqq->new_cfqq)
2533 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002534 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002535 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002536
2537 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002538 * No requests pending. If the active queue still has requests in
2539 * flight or is idling for a new request, allow either of these
2540 * conditions to happen (or time out) before selecting a new queue.
2541 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002542 if (timer_pending(&cfqd->idle_slice_timer)) {
2543 cfqq = NULL;
2544 goto keep_queue;
2545 }
2546
Shaohua Li8e1ac662010-11-08 15:01:04 +01002547 /*
2548 * This is a deep seek queue, but the device is much faster than
2549 * the queue can deliver, don't idle
2550 **/
2551 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2552 (cfq_cfqq_slice_new(cfqq) ||
2553 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2554 cfq_clear_cfqq_deep(cfqq);
2555 cfq_clear_cfqq_idle_window(cfqq);
2556 }
2557
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002558 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2559 cfqq = NULL;
2560 goto keep_queue;
2561 }
2562
2563 /*
2564 * If group idle is enabled and there are requests dispatched from
2565 * this group, wait for requests to complete.
2566 */
2567check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002568 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2569 cfqq->cfqg->dispatched &&
2570 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002571 cfqq = NULL;
2572 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002573 }
2574
Jens Axboe3b181522005-06-27 10:56:24 +02002575expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002576 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002577new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002578 /*
2579 * Current queue expired. Check if we have to switch to a new
2580 * service tree
2581 */
2582 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002583 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002584
Jens Axboea36e71f2009-04-15 12:15:11 +02002585 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002586keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002587 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002588}
2589
Jens Axboefebffd62008-01-28 13:19:43 +01002590static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002591{
2592 int dispatched = 0;
2593
2594 while (cfqq->next_rq) {
2595 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2596 dispatched++;
2597 }
2598
2599 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002600
2601 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002602 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002603 return dispatched;
2604}
2605
Jens Axboe498d3aa22007-04-26 12:54:48 +02002606/*
2607 * Drain our current requests. Used for barriers and when switching
2608 * io schedulers on-the-fly.
2609 */
Jens Axboed9e76202007-04-20 14:27:50 +02002610static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002611{
Jens Axboe08717142008-01-28 11:38:15 +01002612 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002613 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002614
Divyesh Shah3440c492010-04-09 09:29:57 +02002615 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002616 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002617 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2618 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002619 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002620 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002621
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002622 BUG_ON(cfqd->busy_queues);
2623
Jeff Moyer69237152009-06-12 15:29:30 +02002624 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002625 return dispatched;
2626}
2627
Shaohua Liabc3c742010-03-01 09:20:54 +01002628static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2629 struct cfq_queue *cfqq)
2630{
2631 /* the queue hasn't finished any request, can't estimate */
2632 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002633 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002634 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2635 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002636 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002637
Shaohua Lic1e44752010-11-08 15:01:02 +01002638 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002639}
2640
Jens Axboe0b182d62009-10-06 20:49:37 +02002641static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002642{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002643 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Jens Axboe2f5cb732009-04-07 08:51:19 +02002645 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002646 * Drain async requests before we start sync IO
2647 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002648 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002649 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002650
2651 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002652 * If this is an async queue and we have sync IO in flight, let it wait
2653 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002654 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002655 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002656
Shaohua Liabc3c742010-03-01 09:20:54 +01002657 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002658 if (cfq_class_idle(cfqq))
2659 max_dispatch = 1;
2660
2661 /*
2662 * Does this cfqq already have too much IO in flight?
2663 */
2664 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01002665 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02002666 /*
2667 * idle queue must always only have a single IO in flight
2668 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02002669 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02002670 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02002671
Jens Axboe2f5cb732009-04-07 08:51:19 +02002672 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01002673 * If there is only one sync queue
2674 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01002675 * queue no dispatch limit. The reason is a sync queue can
2676 * preempt async queue, limiting the sync queue doesn't make
2677 * sense. This is useful for aiostress test.
2678 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01002679 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
2680 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002681
2682 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002683 * We have other queues, don't allow more IO from this one
2684 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002685 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
2686 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02002687 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11002688
Jens Axboe2f5cb732009-04-07 08:51:19 +02002689 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01002690 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02002691 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01002692 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01002693 max_dispatch = -1;
2694 else
2695 /*
2696 * Normally we start throttling cfqq when cfq_quantum/2
2697 * requests have been dispatched. But we can drive
2698 * deeper queue depths at the beginning of slice
2699 * subjected to upper limit of cfq_quantum.
2700 * */
2701 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02002702 }
2703
2704 /*
2705 * Async queues must wait a bit before being allowed dispatch.
2706 * We also ramp up the dispatch depth gradually for async IO,
2707 * based on the last sync IO we serviced
2708 */
Jens Axboe963b72f2009-10-03 19:42:18 +02002709 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01002710 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02002711 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02002712
Jens Axboe61f0c1d2009-10-03 19:46:03 +02002713 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02002714 if (!depth && !cfqq->dispatched)
2715 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02002716 if (depth < max_dispatch)
2717 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719
Jens Axboe0b182d62009-10-06 20:49:37 +02002720 /*
2721 * If we're below the current max, allow a dispatch
2722 */
2723 return cfqq->dispatched < max_dispatch;
2724}
2725
2726/*
2727 * Dispatch a request from cfqq, moving them to the request queue
2728 * dispatch list.
2729 */
2730static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2731{
2732 struct request *rq;
2733
2734 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
2735
2736 if (!cfq_may_dispatch(cfqd, cfqq))
2737 return false;
2738
2739 /*
2740 * follow expired path, else get first next available
2741 */
2742 rq = cfq_check_fifo(cfqq);
2743 if (!rq)
2744 rq = cfqq->next_rq;
2745
2746 /*
2747 * insert request into driver dispatch list
2748 */
2749 cfq_dispatch_insert(cfqd->queue, rq);
2750
2751 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01002752 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02002753
Tejun Heoc5869802011-12-14 00:33:41 +01002754 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02002755 cfqd->active_cic = cic;
2756 }
2757
2758 return true;
2759}
2760
2761/*
2762 * Find the cfqq that we need to service and move a request from that to the
2763 * dispatch list
2764 */
2765static int cfq_dispatch_requests(struct request_queue *q, int force)
2766{
2767 struct cfq_data *cfqd = q->elevator->elevator_data;
2768 struct cfq_queue *cfqq;
2769
2770 if (!cfqd->busy_queues)
2771 return 0;
2772
2773 if (unlikely(force))
2774 return cfq_forced_dispatch(cfqd);
2775
2776 cfqq = cfq_select_queue(cfqd);
2777 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02002778 return 0;
2779
Jens Axboe2f5cb732009-04-07 08:51:19 +02002780 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02002781 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02002782 */
Jens Axboe0b182d62009-10-06 20:49:37 +02002783 if (!cfq_dispatch_request(cfqd, cfqq))
2784 return 0;
2785
Jens Axboe2f5cb732009-04-07 08:51:19 +02002786 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02002787 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002788
2789 /*
2790 * expire an async queue immediately if it has used up its slice. idle
2791 * queue always expire after 1 dispatch round.
2792 */
2793 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
2794 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
2795 cfq_class_idle(cfqq))) {
2796 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02002797 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02002798 }
2799
Shan Weib217a902009-09-01 10:06:42 +02002800 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02002801 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/*
Jens Axboe5e705372006-07-13 12:39:25 +02002805 * task holds one reference to the queue, dropped when task exits. each rq
2806 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05002808 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 * queue lock must be held here.
2810 */
2811static void cfq_put_queue(struct cfq_queue *cfqq)
2812{
Jens Axboe22e2c502005-06-27 10:55:12 +02002813 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05002814 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02002815
Shaohua Li30d7b942011-01-07 08:46:59 +01002816 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Shaohua Li30d7b942011-01-07 08:46:59 +01002818 cfqq->ref--;
2819 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return;
2821
Jens Axboe7b679132008-05-30 12:23:07 +02002822 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02002824 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05002825 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002827 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02002828 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02002829 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11002830 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002831
Vivek Goyalf04a6422009-12-03 12:59:40 -05002832 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01002834 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
Shaohua Lid02a2c02010-05-25 10:16:53 +02002837static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02002838{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002839 struct cfq_queue *__cfqq, *next;
2840
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002841 /*
2842 * If this queue was scheduled to merge with another queue, be
2843 * sure to drop the reference taken on that queue (and others in
2844 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
2845 */
2846 __cfqq = cfqq->new_cfqq;
2847 while (__cfqq) {
2848 if (__cfqq == cfqq) {
2849 WARN(1, "cfqq->new_cfqq loop detected\n");
2850 break;
2851 }
2852 next = __cfqq->new_cfqq;
2853 cfq_put_queue(__cfqq);
2854 __cfqq = next;
2855 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02002856}
2857
2858static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2859{
2860 if (unlikely(cfqq == cfqd->active_queue)) {
2861 __cfq_slice_expired(cfqd, cfqq, 0);
2862 cfq_schedule_dispatch(cfqd);
2863 }
2864
2865 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002866
Jens Axboe89850f72006-07-22 16:48:31 +02002867 cfq_put_queue(cfqq);
2868}
2869
Tejun Heo9b84cac2011-12-14 00:33:42 +01002870static void cfq_init_icq(struct io_cq *icq)
2871{
2872 struct cfq_io_cq *cic = icq_to_cic(icq);
2873
2874 cic->ttime.last_end_request = jiffies;
2875}
2876
Tejun Heoc5869802011-12-14 00:33:41 +01002877static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02002878{
Tejun Heoc5869802011-12-14 00:33:41 +01002879 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01002880 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02002881
Jens Axboeff6657c2009-04-08 10:58:57 +02002882 if (cic->cfqq[BLK_RW_ASYNC]) {
2883 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
2884 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002885 }
2886
Jens Axboeff6657c2009-04-08 10:58:57 +02002887 if (cic->cfqq[BLK_RW_SYNC]) {
2888 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
2889 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02002890 }
Jens Axboe89850f72006-07-22 16:48:31 +02002891}
2892
Tejun Heoabede6d2012-03-19 15:10:57 -07002893static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02002894{
2895 struct task_struct *tsk = current;
2896 int ioprio_class;
2897
Jens Axboe3b181522005-06-27 10:56:24 +02002898 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02002899 return;
2900
Tejun Heo598971b2012-03-19 15:10:58 -07002901 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002902 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01002903 default:
2904 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
2905 case IOPRIO_CLASS_NONE:
2906 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02002907 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01002908 */
2909 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02002910 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01002911 break;
2912 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07002913 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01002914 cfqq->ioprio_class = IOPRIO_CLASS_RT;
2915 break;
2916 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07002917 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01002918 cfqq->ioprio_class = IOPRIO_CLASS_BE;
2919 break;
2920 case IOPRIO_CLASS_IDLE:
2921 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
2922 cfqq->ioprio = 7;
2923 cfq_clear_cfqq_idle_window(cfqq);
2924 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02002925 }
2926
2927 /*
2928 * keep track of original prio settings in case we have to temporarily
2929 * elevate the priority of this queue
2930 */
2931 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02002932 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002933}
2934
Tejun Heo598971b2012-03-19 15:10:58 -07002935static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02002936{
Tejun Heo598971b2012-03-19 15:10:58 -07002937 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002938 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05002939 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02002940
Tejun Heo598971b2012-03-19 15:10:58 -07002941 /*
2942 * Check whether ioprio has changed. The condition may trigger
2943 * spuriously on a newly created cic but there's no harm.
2944 */
2945 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02002946 return;
2947
Jens Axboeff6657c2009-04-08 10:58:57 +02002948 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002949 if (cfqq) {
2950 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07002951 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
2952 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02002953 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02002954 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02002955 cfq_put_queue(cfqq);
2956 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002957 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02002958
Jens Axboeff6657c2009-04-08 10:58:57 +02002959 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02002960 if (cfqq)
2961 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07002962
2963 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02002964}
2965
Jens Axboed5036d72009-06-26 10:44:34 +02002966static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002967 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02002968{
2969 RB_CLEAR_NODE(&cfqq->rb_node);
2970 RB_CLEAR_NODE(&cfqq->p_node);
2971 INIT_LIST_HEAD(&cfqq->fifo);
2972
Shaohua Li30d7b942011-01-07 08:46:59 +01002973 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02002974 cfqq->cfqd = cfqd;
2975
2976 cfq_mark_cfqq_prio_changed(cfqq);
2977
2978 if (is_sync) {
2979 if (!cfq_class_idle(cfqq))
2980 cfq_mark_cfqq_idle_window(cfqq);
2981 cfq_mark_cfqq_sync(cfqq);
2982 }
2983 cfqq->pid = pid;
2984}
2985
Vivek Goyal246103332009-12-03 12:59:51 -05002986#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07002987static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05002988{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04002989 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07002990 struct cfq_queue *sync_cfqq;
2991 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05002992
Tejun Heo598971b2012-03-19 15:10:58 -07002993 rcu_read_lock();
2994 id = bio_blkio_cgroup(bio)->id;
2995 rcu_read_unlock();
2996
2997 /*
2998 * Check whether blkcg has changed. The condition may trigger
2999 * spuriously on a newly created cic but there's no harm.
3000 */
3001 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003002 return;
3003
Tejun Heo598971b2012-03-19 15:10:58 -07003004 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003005 if (sync_cfqq) {
3006 /*
3007 * Drop reference to sync queue. A new sync queue will be
3008 * assigned in new group upon arrival of a fresh request.
3009 */
3010 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3011 cic_set_cfqq(cic, NULL, 1);
3012 cfq_put_queue(sync_cfqq);
3013 }
Tejun Heo598971b2012-03-19 15:10:58 -07003014
3015 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003016}
Tejun Heo598971b2012-03-19 15:10:58 -07003017#else
3018static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003019#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003022cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3023 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Tejun Heo0a5a7d02012-03-05 13:15:02 -08003025 struct blkio_cgroup *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003027 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003030 rcu_read_lock();
3031
Tejun Heo4f85cb92012-03-05 13:15:28 -08003032 blkcg = bio_blkio_cgroup(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003033 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003034 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jens Axboe6118b702009-06-30 09:34:12 +02003036 /*
3037 * Always try a new alloc if we fell back to the OOM cfqq
3038 * originally, since it should just be a temporary situation.
3039 */
3040 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3041 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (new_cfqq) {
3043 cfqq = new_cfqq;
3044 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003045 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003046 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003048 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003049 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003050 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003052 if (new_cfqq)
3053 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003054 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003055 cfqq = kmem_cache_alloc_node(cfq_pool,
3056 gfp_mask | __GFP_ZERO,
3057 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Jens Axboe6118b702009-06-30 09:34:12 +02003060 if (cfqq) {
3061 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003062 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003063 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003064 cfq_log_cfqq(cfqd, cfqq, "alloced");
3065 } else
3066 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068
3069 if (new_cfqq)
3070 kmem_cache_free(cfq_pool, new_cfqq);
3071
Tejun Heo2a7f1242012-03-05 13:15:01 -08003072 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return cfqq;
3074}
3075
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003076static struct cfq_queue **
3077cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3078{
Jens Axboefe094d92008-01-31 13:08:54 +01003079 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003080 case IOPRIO_CLASS_RT:
3081 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003082 case IOPRIO_CLASS_NONE:
3083 ioprio = IOPRIO_NORM;
3084 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003085 case IOPRIO_CLASS_BE:
3086 return &cfqd->async_cfqq[1][ioprio];
3087 case IOPRIO_CLASS_IDLE:
3088 return &cfqd->async_idle_cfqq;
3089 default:
3090 BUG();
3091 }
3092}
3093
Jens Axboe15c31be2007-07-10 13:43:25 +02003094static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003095cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003096 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003097{
Tejun Heo598971b2012-03-19 15:10:58 -07003098 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3099 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003100 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003101 struct cfq_queue *cfqq = NULL;
3102
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003103 if (!is_sync) {
3104 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3105 cfqq = *async_cfqq;
3106 }
3107
Jens Axboe6118b702009-06-30 09:34:12 +02003108 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003109 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003110
3111 /*
3112 * pin the queue now that it's allocated, scheduler exit will prune it
3113 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003114 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003115 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003116 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003117 }
3118
Shaohua Li30d7b942011-01-07 08:46:59 +01003119 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003120 return cfqq;
3121}
3122
Jens Axboe22e2c502005-06-27 10:55:12 +02003123static void
Shaohua Li383cd722011-07-12 14:24:35 +02003124__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003125{
Shaohua Li383cd722011-07-12 14:24:35 +02003126 unsigned long elapsed = jiffies - ttime->last_end_request;
3127 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003128
Shaohua Li383cd722011-07-12 14:24:35 +02003129 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3130 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3131 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3132}
3133
3134static void
3135cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003136 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003137{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003138 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003139 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003140 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3141 cfqd->cfq_slice_idle);
3142 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003143#ifdef CONFIG_CFQ_GROUP_IOSCHED
3144 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3145#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003146}
3147
Jens Axboe206dc692006-03-28 13:03:44 +02003148static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003149cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003150 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003151{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003152 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003153 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003154 if (cfqq->last_request_pos) {
3155 if (cfqq->last_request_pos < blk_rq_pos(rq))
3156 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3157 else
3158 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3159 }
Jens Axboe206dc692006-03-28 13:03:44 +02003160
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003161 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003162 if (blk_queue_nonrot(cfqd->queue))
3163 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3164 else
3165 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003166}
Jens Axboe22e2c502005-06-27 10:55:12 +02003167
3168/*
3169 * Disable idle window if the process thinks too long or seeks so much that
3170 * it doesn't matter
3171 */
3172static void
3173cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003174 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003175{
Jens Axboe7b679132008-05-30 12:23:07 +02003176 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003177
Jens Axboe08717142008-01-28 11:38:15 +01003178 /*
3179 * Don't idle for async or idle io prio class
3180 */
3181 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003182 return;
3183
Jens Axboec265a7f2008-06-26 13:49:33 +02003184 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003185
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003186 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3187 cfq_mark_cfqq_deep(cfqq);
3188
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003189 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3190 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003191 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003192 !cfqd->cfq_slice_idle ||
3193 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003194 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003195 else if (sample_valid(cic->ttime.ttime_samples)) {
3196 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003197 enable_idle = 0;
3198 else
3199 enable_idle = 1;
3200 }
3201
Jens Axboe7b679132008-05-30 12:23:07 +02003202 if (old_idle != enable_idle) {
3203 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3204 if (enable_idle)
3205 cfq_mark_cfqq_idle_window(cfqq);
3206 else
3207 cfq_clear_cfqq_idle_window(cfqq);
3208 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003209}
3210
Jens Axboe22e2c502005-06-27 10:55:12 +02003211/*
3212 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3213 * no or if we aren't sure, a 1 will cause a preempt.
3214 */
Jens Axboea6151c32009-10-07 20:02:57 +02003215static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003216cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003217 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003218{
Jens Axboe6d048f52007-04-25 12:44:27 +02003219 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003220
Jens Axboe6d048f52007-04-25 12:44:27 +02003221 cfqq = cfqd->active_queue;
3222 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003223 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003224
Jens Axboe6d048f52007-04-25 12:44:27 +02003225 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003226 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003227
3228 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003229 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003230
Jens Axboe22e2c502005-06-27 10:55:12 +02003231 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003232 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3233 */
3234 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3235 return false;
3236
3237 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003238 * if the new request is sync, but the currently running queue is
3239 * not, let the sync request have priority.
3240 */
Jens Axboe5e705372006-07-13 12:39:25 +02003241 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003242 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003243
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003244 if (new_cfqq->cfqg != cfqq->cfqg)
3245 return false;
3246
3247 if (cfq_slice_used(cfqq))
3248 return true;
3249
3250 /* Allow preemption only if we are idling on sync-noidle tree */
3251 if (cfqd->serving_type == SYNC_NOIDLE_WORKLOAD &&
3252 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3253 new_cfqq->service_tree->count == 2 &&
3254 RB_EMPTY_ROOT(&cfqq->sort_list))
3255 return true;
3256
Jens Axboe374f84a2006-07-23 01:42:19 +02003257 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003258 * So both queues are sync. Let the new request get disk time if
3259 * it's a metadata request and the current queue is doing regular IO.
3260 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003261 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003262 return true;
3263
3264 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003265 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3266 */
3267 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003268 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003269
Shaohua Lid2d59e12010-11-08 15:01:03 +01003270 /* An idle queue should not be idle now for some reason */
3271 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3272 return true;
3273
Jens Axboe1e3335d2007-02-14 19:59:49 +01003274 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003275 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003276
3277 /*
3278 * if this request is as-good as one we would expect from the
3279 * current cfqq, let it preempt
3280 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003281 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003282 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003283
Jens Axboea6151c32009-10-07 20:02:57 +02003284 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003285}
3286
3287/*
3288 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3289 * let it have half of its nominal slice.
3290 */
3291static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3292{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003293 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3294
Jens Axboe7b679132008-05-30 12:23:07 +02003295 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003296 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003297
Jens Axboebf572252006-07-19 20:29:12 +02003298 /*
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003299 * workload type is changed, don't save slice, otherwise preempt
3300 * doesn't happen
3301 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003302 if (old_type != cfqq_type(cfqq))
Shaohua Lif8ae6e3e2011-01-14 08:41:02 +01003303 cfqq->cfqg->saved_workload_slice = 0;
3304
3305 /*
Jens Axboebf572252006-07-19 20:29:12 +02003306 * Put the new queue at the front of the of the current list,
3307 * so we know that it will be selected next.
3308 */
3309 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003310
3311 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003312
Justin TerAvest62a37f62011-03-23 08:25:44 +01003313 cfqq->slice_end = 0;
3314 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003315}
3316
3317/*
Jens Axboe5e705372006-07-13 12:39:25 +02003318 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003319 * something we should do about it
3320 */
3321static void
Jens Axboe5e705372006-07-13 12:39:25 +02003322cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3323 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003324{
Tejun Heoc5869802011-12-14 00:33:41 +01003325 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003326
Aaron Carroll45333d52008-08-26 15:52:36 +02003327 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003328 if (rq->cmd_flags & REQ_PRIO)
3329 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003330
Shaohua Li383cd722011-07-12 14:24:35 +02003331 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003332 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003333 cfq_update_idle_window(cfqd, cfqq, cic);
3334
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003335 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003336
3337 if (cfqq == cfqd->active_queue) {
3338 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003339 * Remember that we saw a request from this process, but
3340 * don't start queuing just yet. Otherwise we risk seeing lots
3341 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003342 * and merging. If the request is already larger than a single
3343 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003344 * merging is already done. Ditto for a busy system that
3345 * has other work pending, don't risk delaying until the
3346 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003347 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003348 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003349 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3350 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003351 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003352 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003353 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003354 } else {
Vivek Goyale98ef892010-06-18 10:39:47 -04003355 cfq_blkiocg_update_idle_time_stats(
Tejun Heoc1768262012-03-05 13:15:17 -08003356 cfqg_to_blkg(cfqq->cfqg),
3357 &blkio_policy_cfq);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003358 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003359 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003360 }
Jens Axboe5e705372006-07-13 12:39:25 +02003361 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003362 /*
3363 * not the active queue - expire current slice if it is
3364 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003365 * has some old slice time left and is of higher priority or
3366 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003367 */
3368 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003369 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003370 }
3371}
3372
Jens Axboe165125e2007-07-24 09:28:11 +02003373static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003374{
Jens Axboeb4878f22005-10-20 16:42:29 +02003375 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003376 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003377
Jens Axboe7b679132008-05-30 12:23:07 +02003378 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003379 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Jens Axboe30996f42009-10-05 11:03:39 +02003381 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003382 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003383 cfq_add_rq_rb(rq);
Tejun Heo03814112012-03-05 13:15:14 -08003384 cfq_blkiocg_update_io_add_stats(cfqg_to_blkg(RQ_CFQG(rq)),
Tejun Heoc1768262012-03-05 13:15:17 -08003385 &blkio_policy_cfq,
Tejun Heo03814112012-03-05 13:15:14 -08003386 cfqg_to_blkg(cfqd->serving_group),
3387 rq_data_dir(rq), rq_is_sync(rq));
Jens Axboe5e705372006-07-13 12:39:25 +02003388 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Aaron Carroll45333d52008-08-26 15:52:36 +02003391/*
3392 * Update hw_tag based on peak queue depth over 50 samples under
3393 * sufficient load.
3394 */
3395static void cfq_update_hw_tag(struct cfq_data *cfqd)
3396{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003397 struct cfq_queue *cfqq = cfqd->active_queue;
3398
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003399 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3400 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003401
3402 if (cfqd->hw_tag == 1)
3403 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003404
3405 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003406 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003407 return;
3408
Shaohua Li1a1238a2009-10-27 08:46:23 +01003409 /*
3410 * If active queue hasn't enough requests and can idle, cfq might not
3411 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3412 * case
3413 */
3414 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3415 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003416 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003417 return;
3418
Aaron Carroll45333d52008-08-26 15:52:36 +02003419 if (cfqd->hw_tag_samples++ < 50)
3420 return;
3421
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003422 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003423 cfqd->hw_tag = 1;
3424 else
3425 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003426}
3427
Vivek Goyal7667aa02009-12-08 17:52:58 -05003428static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3429{
Tejun Heoc5869802011-12-14 00:33:41 +01003430 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003431
Justin TerAvest02a8f012011-02-09 14:20:03 +01003432 /* If the queue already has requests, don't wait */
3433 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3434 return false;
3435
Vivek Goyal7667aa02009-12-08 17:52:58 -05003436 /* If there are other queues in the group, don't wait */
3437 if (cfqq->cfqg->nr_cfqq > 1)
3438 return false;
3439
Shaohua Li7700fc42011-07-12 14:24:56 +02003440 /* the only queue in the group, but think time is big */
3441 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3442 return false;
3443
Vivek Goyal7667aa02009-12-08 17:52:58 -05003444 if (cfq_slice_used(cfqq))
3445 return true;
3446
3447 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003448 if (cic && sample_valid(cic->ttime.ttime_samples)
3449 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003450 return true;
3451
3452 /*
3453 * If think times is less than a jiffy than ttime_mean=0 and above
3454 * will not be true. It might happen that slice has not expired yet
3455 * but will expire soon (4-5 ns) during select_queue(). To cover the
3456 * case where think time is less than a jiffy, mark the queue wait
3457 * busy if only 1 jiffy is left in the slice.
3458 */
3459 if (cfqq->slice_end - jiffies == 1)
3460 return true;
3461
3462 return false;
3463}
3464
Jens Axboe165125e2007-07-24 09:28:11 +02003465static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Jens Axboe5e705372006-07-13 12:39:25 +02003467 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003468 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003469 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003470 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Jens Axboeb4878f22005-10-20 16:42:29 +02003472 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003473 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3474 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Aaron Carroll45333d52008-08-26 15:52:36 +02003476 cfq_update_hw_tag(cfqd);
3477
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003478 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003479 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003480 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003481 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003482 (RQ_CFQG(rq))->dispatched--;
Tejun Heo03814112012-03-05 13:15:14 -08003483 cfq_blkiocg_update_completion_stats(cfqg_to_blkg(cfqq->cfqg),
Tejun Heoc1768262012-03-05 13:15:17 -08003484 &blkio_policy_cfq, rq_start_time_ns(rq),
3485 rq_io_start_time_ns(rq), rq_data_dir(rq),
3486 rq_is_sync(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003488 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003489
Vivek Goyal365722b2009-10-03 15:21:27 +02003490 if (sync) {
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003491 struct cfq_rb_root *service_tree;
3492
Shaohua Li383cd722011-07-12 14:24:35 +02003493 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003494
3495 if (cfq_cfqq_on_rr(cfqq))
3496 service_tree = cfqq->service_tree;
3497 else
3498 service_tree = service_tree_for(cfqq->cfqg,
3499 cfqq_prio(cfqq), cfqq_type(cfqq));
3500 service_tree->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003501 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3502 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003503 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003504
Shaohua Li7700fc42011-07-12 14:24:56 +02003505#ifdef CONFIG_CFQ_GROUP_IOSCHED
3506 cfqq->cfqg->ttime.last_end_request = now;
3507#endif
3508
Jens Axboecaaa5f92006-06-16 11:23:00 +02003509 /*
3510 * If this is the active queue, check if it needs to be expired,
3511 * or if we want to idle in case it has no pending requests.
3512 */
3513 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003514 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3515
Jens Axboe44f7c162007-01-19 11:51:58 +11003516 if (cfq_cfqq_slice_new(cfqq)) {
3517 cfq_set_prio_slice(cfqd, cfqq);
3518 cfq_clear_cfqq_slice_new(cfqq);
3519 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003520
3521 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003522 * Should we wait for next request to come in before we expire
3523 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003524 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003525 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003526 unsigned long extend_sl = cfqd->cfq_slice_idle;
3527 if (!cfqd->cfq_slice_idle)
3528 extend_sl = cfqd->cfq_group_idle;
3529 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003530 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003531 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003532 }
3533
Jens Axboea36e71f2009-04-15 12:15:11 +02003534 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003535 * Idling is not enabled on:
3536 * - expired queues
3537 * - idle-priority queues
3538 * - async queues
3539 * - queues with still some requests queued
3540 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003541 */
Jens Axboe08717142008-01-28 11:38:15 +01003542 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003543 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003544 else if (sync && cfqq_empty &&
3545 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003546 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003547 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003548 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003549
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003550 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003551 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
Jens Axboe89850f72006-07-22 16:48:31 +02003554static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003555{
Jens Axboe1b379d82009-08-11 08:26:11 +02003556 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003557 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003558 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003559 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003560
3561 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003562}
3563
Jens Axboe165125e2007-07-24 09:28:11 +02003564static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003565{
3566 struct cfq_data *cfqd = q->elevator->elevator_data;
3567 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003568 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003569 struct cfq_queue *cfqq;
3570
3571 /*
3572 * don't force setup of a queue from here, as a call to may_queue
3573 * does not necessarily imply that a request actually will be queued.
3574 * so just lookup a possibly existing queue, or return 'may queue'
3575 * if that fails
3576 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003577 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003578 if (!cic)
3579 return ELV_MQUEUE_MAY;
3580
Jens Axboeb0b78f82009-04-08 10:56:08 +02003581 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003582 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003583 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003584
Jens Axboe89850f72006-07-22 16:48:31 +02003585 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003586 }
3587
3588 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591/*
3592 * queue lock held here
3593 */
Jens Axboebb37b942006-12-01 10:42:33 +01003594static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
Jens Axboe5e705372006-07-13 12:39:25 +02003596 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Jens Axboe5e705372006-07-13 12:39:25 +02003598 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003599 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Jens Axboe22e2c502005-06-27 10:55:12 +02003601 BUG_ON(!cfqq->allocated[rw]);
3602 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003604 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003605 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003606 rq->elv.priv[0] = NULL;
3607 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 cfq_put_queue(cfqq);
3610 }
3611}
3612
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003613static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003614cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003615 struct cfq_queue *cfqq)
3616{
3617 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3618 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003619 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003620 cfq_put_queue(cfqq);
3621 return cic_to_cfqq(cic, 1);
3622}
3623
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003624/*
3625 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3626 * was the last process referring to said cfqq.
3627 */
3628static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003629split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003630{
3631 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003632 cfqq->pid = current->pid;
3633 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003634 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003635 return cfqq;
3636 }
3637
3638 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003639
3640 cfq_put_cooperator(cfqq);
3641
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003642 cfq_put_queue(cfqq);
3643 return NULL;
3644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003646 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003648static int
Tejun Heo852c7882012-03-05 13:15:27 -08003649cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
3650 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
3652 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003653 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003655 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003656 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 might_sleep_if(gfp_mask & __GFP_WAIT);
3659
Tejun Heo216284c2011-12-14 00:33:38 +01003660 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01003661
Tejun Heo598971b2012-03-19 15:10:58 -07003662 check_ioprio_changed(cic, bio);
3663 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003664new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02003665 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02003666 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003667 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003668 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003669 } else {
3670 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003671 * If the queue was seeky for too long, break it apart.
3672 */
Shaohua Liae54abe2010-02-05 13:11:45 +01003673 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003674 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
3675 cfqq = split_cfqq(cic, cfqq);
3676 if (!cfqq)
3677 goto new_queue;
3678 }
3679
3680 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003681 * Check to see if this queue is scheduled to merge with
3682 * another, closely cooperating queue. The merging of
3683 * queues happens here as it must be done in process context.
3684 * The reference on new_cfqq was taken in merge_cfqqs.
3685 */
3686 if (cfqq->new_cfqq)
3687 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
3690 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02003691
Jens Axboe6fae9c22011-03-01 15:04:39 -05003692 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003693 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01003694 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003695 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01003696 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02003697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
David Howells65f27f32006-11-22 14:55:48 +00003700static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02003701{
David Howells65f27f32006-11-22 14:55:48 +00003702 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02003703 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02003704 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003705
Jens Axboe40bb54d2009-04-15 12:11:10 +02003706 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003707 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02003708 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02003709}
3710
3711/*
3712 * Timer running if the active_queue is currently idling inside its time slice
3713 */
3714static void cfq_idle_slice_timer(unsigned long data)
3715{
3716 struct cfq_data *cfqd = (struct cfq_data *) data;
3717 struct cfq_queue *cfqq;
3718 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003719 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02003720
Jens Axboe7b679132008-05-30 12:23:07 +02003721 cfq_log(cfqd, "idle timer fired");
3722
Jens Axboe22e2c502005-06-27 10:55:12 +02003723 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
3724
Jens Axboefe094d92008-01-31 13:08:54 +01003725 cfqq = cfqd->active_queue;
3726 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11003727 timed_out = 0;
3728
Jens Axboe22e2c502005-06-27 10:55:12 +02003729 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003730 * We saw a request before the queue expired, let it through
3731 */
3732 if (cfq_cfqq_must_dispatch(cfqq))
3733 goto out_kick;
3734
3735 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003736 * expired
3737 */
Jens Axboe44f7c162007-01-19 11:51:58 +11003738 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003739 goto expire;
3740
3741 /*
3742 * only expire and reinvoke request handler, if there are
3743 * other queues with pending requests
3744 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02003745 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02003746 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02003747
3748 /*
3749 * not expired and it has a request pending, let it dispatch
3750 */
Jens Axboe75e50982009-04-07 08:56:14 +02003751 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003752 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003753
3754 /*
3755 * Queue depth flag is reset only when the idle didn't succeed
3756 */
3757 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003758 }
3759expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003760 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02003761out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02003762 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02003763out_cont:
3764 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
3765}
3766
Jens Axboe3b181522005-06-27 10:56:24 +02003767static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
3768{
3769 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02003770 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02003771}
Jens Axboe22e2c502005-06-27 10:55:12 +02003772
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003773static void cfq_put_async_queues(struct cfq_data *cfqd)
3774{
3775 int i;
3776
3777 for (i = 0; i < IOPRIO_BE_NR; i++) {
3778 if (cfqd->async_cfqq[0][i])
3779 cfq_put_queue(cfqd->async_cfqq[0][i]);
3780 if (cfqd->async_cfqq[1][i])
3781 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003782 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01003783
3784 if (cfqd->async_idle_cfqq)
3785 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003786}
3787
Jens Axboeb374d182008-10-31 10:05:07 +01003788static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789{
Jens Axboe22e2c502005-06-27 10:55:12 +02003790 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02003791 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003792
Jens Axboe3b181522005-06-27 10:56:24 +02003793 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003794
Al Virod9ff4182006-03-18 13:51:22 -05003795 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003796
Al Virod9ff4182006-03-18 13:51:22 -05003797 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02003798 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003799
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003800 cfq_put_async_queues(cfqd);
Tejun Heo03aa264a2012-03-05 13:15:19 -08003801
3802 spin_unlock_irq(q->queue_lock);
3803
Al Viroa90d7422006-03-18 12:05:37 -05003804 cfq_shutdown_timer_wq(cfqd);
3805
Tejun Heof51b8022012-03-05 13:15:05 -08003806#ifndef CONFIG_CFQ_GROUP_IOSCHED
3807 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02003808#endif
Tejun Heoe8989fa2012-03-05 13:15:20 -08003809 update_root_blkg_pd(q, BLKIO_POLICY_PROP);
Vivek Goyal56edf7d2011-05-19 15:38:22 -04003810 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Tejun Heob2fab5a2012-03-05 13:14:57 -08003813static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 struct cfq_data *cfqd;
Tejun Heocd1604f2012-03-05 13:15:06 -08003816 struct blkio_group *blkg __maybe_unused;
Tejun Heof51b8022012-03-05 13:15:05 -08003817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Christoph Lameter94f60302007-07-17 04:03:29 -07003819 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01003820 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08003821 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04003822
Tejun Heof51b8022012-03-05 13:15:05 -08003823 cfqd->queue = q;
3824 q->elevator->elevator_data = cfqd;
3825
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003826 /* Init root service tree */
3827 cfqd->grp_service_tree = CFQ_RB_ROOT;
3828
Tejun Heof51b8022012-03-05 13:15:05 -08003829 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05003830#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof51b8022012-03-05 13:15:05 -08003831 rcu_read_lock();
3832 spin_lock_irq(q->queue_lock);
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003833
Tejun Heoaaec55a2012-04-01 14:38:42 -07003834 blkg = blkg_lookup_create(&blkio_root_cgroup, q, true);
Tejun Heocd1604f2012-03-05 13:15:06 -08003835 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08003836 cfqd->root_group = blkg_to_cfqg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08003837
3838 spin_unlock_irq(q->queue_lock);
3839 rcu_read_unlock();
3840#else
3841 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
3842 GFP_KERNEL, cfqd->queue->node);
3843 if (cfqd->root_group)
3844 cfq_init_cfqg_base(cfqd->root_group);
3845#endif
3846 if (!cfqd->root_group) {
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003847 kfree(cfqd);
Tejun Heob2fab5a2012-03-05 13:14:57 -08003848 return -ENOMEM;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003849 }
3850
Tejun Heof51b8022012-03-05 13:15:05 -08003851 cfqd->root_group->weight = 2*BLKIO_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04003852
Jens Axboe26a2ac02009-04-23 12:13:27 +02003853 /*
3854 * Not strictly needed (since RB_ROOT just clears the node and we
3855 * zeroed cfqd on alloc), but better be safe in case someone decides
3856 * to add magic to the rb code
3857 */
3858 for (i = 0; i < CFQ_PRIO_LISTS; i++)
3859 cfqd->prio_trees[i] = RB_ROOT;
3860
Jens Axboe6118b702009-06-30 09:34:12 +02003861 /*
3862 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
3863 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08003864 * will not attempt to free it. oom_cfqq is linked to root_group
3865 * but shouldn't hold a reference as it'll never be unlinked. Lose
3866 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02003867 */
3868 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01003869 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003870
3871 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08003872 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003873 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08003874 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Jens Axboe22e2c502005-06-27 10:55:12 +02003876 init_timer(&cfqd->idle_slice_timer);
3877 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
3878 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
3879
Jens Axboe23e018a2009-10-05 08:52:35 +02003880 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003881
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02003883 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
3884 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 cfqd->cfq_back_max = cfq_back_max;
3886 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02003887 cfqd->cfq_slice[0] = cfq_slice_async;
3888 cfqd->cfq_slice[1] = cfq_slice_sync;
3889 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
3890 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003891 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02003892 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003893 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01003894 /*
3895 * we optimistically start assuming sync ops weren't delayed in last
3896 * second, in order to have larger depth for async operations.
3897 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003898 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08003899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902/*
3903 * sysfs parts below -->
3904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905static ssize_t
3906cfq_var_show(unsigned int var, char *page)
3907{
3908 return sprintf(page, "%d\n", var);
3909}
3910
3911static ssize_t
3912cfq_var_store(unsigned int *var, const char *page, size_t count)
3913{
3914 char *p = (char *) page;
3915
3916 *var = simple_strtoul(p, &p, 10);
3917 return count;
3918}
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003921static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003923 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 unsigned int __data = __VAR; \
3925 if (__CONV) \
3926 __data = jiffies_to_msecs(__data); \
3927 return cfq_var_show(__data, (page)); \
3928}
3929SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003930SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
3931SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05003932SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
3933SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003934SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003935SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003936SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
3937SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
3938SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003939SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940#undef SHOW_FUNCTION
3941
3942#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01003943static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{ \
Al Viro3d1ab402006-03-18 18:35:43 -05003945 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 unsigned int __data; \
3947 int ret = cfq_var_store(&__data, (page), count); \
3948 if (__data < (MIN)) \
3949 __data = (MIN); \
3950 else if (__data > (MAX)) \
3951 __data = (MAX); \
3952 if (__CONV) \
3953 *(__PTR) = msecs_to_jiffies(__data); \
3954 else \
3955 *(__PTR) = __data; \
3956 return ret; \
3957}
3958STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003959STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
3960 UINT_MAX, 1);
3961STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
3962 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05003963STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01003964STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
3965 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02003966STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003967STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003968STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
3969STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01003970STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
3971 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02003972STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973#undef STORE_FUNCTION
3974
Al Viroe572ec72006-03-18 22:27:18 -05003975#define CFQ_ATTR(name) \
3976 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02003977
Al Viroe572ec72006-03-18 22:27:18 -05003978static struct elv_fs_entry cfq_attrs[] = {
3979 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05003980 CFQ_ATTR(fifo_expire_sync),
3981 CFQ_ATTR(fifo_expire_async),
3982 CFQ_ATTR(back_seek_max),
3983 CFQ_ATTR(back_seek_penalty),
3984 CFQ_ATTR(slice_sync),
3985 CFQ_ATTR(slice_async),
3986 CFQ_ATTR(slice_async_rq),
3987 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003988 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02003989 CFQ_ATTR(low_latency),
Al Viroe572ec72006-03-18 22:27:18 -05003990 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991};
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static struct elevator_type iosched_cfq = {
3994 .ops = {
3995 .elevator_merge_fn = cfq_merge,
3996 .elevator_merged_fn = cfq_merged_request,
3997 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01003998 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07003999 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004000 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004002 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004005 .elevator_former_req_fn = elv_rb_former_request,
4006 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004007 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004008 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 .elevator_set_req_fn = cfq_set_request,
4010 .elevator_put_req_fn = cfq_put_request,
4011 .elevator_may_queue_fn = cfq_may_queue,
4012 .elevator_init_fn = cfq_init_queue,
4013 .elevator_exit_fn = cfq_exit_queue,
4014 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004015 .icq_size = sizeof(struct cfq_io_cq),
4016 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004017 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004018 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 .elevator_owner = THIS_MODULE,
4020};
4021
Vivek Goyal3e252062009-12-04 10:36:42 -05004022#ifdef CONFIG_CFQ_GROUP_IOSCHED
4023static struct blkio_policy_type blkio_policy_cfq = {
4024 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08004025 .blkio_init_group_fn = cfq_init_blkio_group,
Vivek Goyal3e252062009-12-04 10:36:42 -05004026 },
Vivek Goyal062a6442010-09-15 17:06:33 -04004027 .plid = BLKIO_POLICY_PROP,
Tejun Heo03814112012-03-05 13:15:14 -08004028 .pdata_size = sizeof(struct cfq_group),
Tejun Heo60c2bc22012-04-01 14:38:43 -07004029 .cftypes = cfq_blkcg_files,
Vivek Goyal3e252062009-12-04 10:36:42 -05004030};
Vivek Goyal3e252062009-12-04 10:36:42 -05004031#endif
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033static int __init cfq_init(void)
4034{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004035 int ret;
4036
Jens Axboe22e2c502005-06-27 10:55:12 +02004037 /*
4038 * could be 0 on HZ < 1000 setups
4039 */
4040 if (!cfq_slice_async)
4041 cfq_slice_async = 1;
4042 if (!cfq_slice_idle)
4043 cfq_slice_idle = 1;
4044
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004045#ifdef CONFIG_CFQ_GROUP_IOSCHED
4046 if (!cfq_group_idle)
4047 cfq_group_idle = 1;
4048#else
4049 cfq_group_idle = 0;
4050#endif
Tejun Heo3d3c2372011-12-14 00:33:42 +01004051 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4052 if (!cfq_pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return -ENOMEM;
4054
Tejun Heo3d3c2372011-12-14 00:33:42 +01004055 ret = elv_register(&iosched_cfq);
4056 if (ret) {
4057 kmem_cache_destroy(cfq_pool);
4058 return ret;
4059 }
Tejun Heo3d3c2372011-12-14 00:33:42 +01004060
Tejun Heob95ada52012-03-05 13:14:55 -08004061#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004062 blkio_policy_register(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004063#endif
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
4067static void __exit cfq_exit(void)
4068{
Tejun Heob95ada52012-03-05 13:14:55 -08004069#ifdef CONFIG_CFQ_GROUP_IOSCHED
Vivek Goyal3e252062009-12-04 10:36:42 -05004070 blkio_policy_unregister(&blkio_policy_cfq);
Tejun Heob95ada52012-03-05 13:14:55 -08004071#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004073 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
4076module_init(cfq_init);
4077module_exit(cfq_exit);
4078
4079MODULE_AUTHOR("Jens Axboe");
4080MODULE_LICENSE("GPL");
4081MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");