blob: b24acf66d5b545a5dd2f24e3c5ab65c807c8c080 [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"
Tejun Heo629ed0b2012-04-01 14:38:44 -070018#include "blk-cgroup.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * tunables
22 */
Jens Axboefe094d92008-01-31 13:08:54 +010023/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010024static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010025static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010026/* maximum backwards seek, in KiB */
27static const int cfq_back_max = 16 * 1024;
28/* penalty of a backwards seek */
29static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010030static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020031static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010032static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020033static int cfq_slice_idle = HZ / 125;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +020034static int cfq_group_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010035static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
36static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020037
Jens Axboed9e76202007-04-20 14:27:50 +020038/*
Jens Axboe08717142008-01-28 11:38:15 +010039 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020040 */
Jens Axboe08717142008-01-28 11:38:15 +010041#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020042
43/*
44 * below this threshold, we consider thinktime immediate
45 */
46#define CFQ_MIN_TT (2)
47
Jens Axboe22e2c502005-06-27 10:55:12 +020048#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020049#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050050#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020051
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010052#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010053#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010054#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010055#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010056
Tejun Heoa612fdd2011-12-14 00:33:41 +010057#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
58#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
59#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Christoph Lametere18b8902006-12-06 20:33:20 -080061static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jens Axboe22e2c502005-06-27 10:55:12 +020063#define CFQ_PRIO_LISTS IOPRIO_BE_NR
64#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
66
Jens Axboe206dc692006-03-28 13:03:44 +020067#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050068#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020069
Tejun Heoc5869802011-12-14 00:33:41 +010070struct cfq_ttime {
71 unsigned long last_end_request;
72
73 unsigned long ttime_total;
74 unsigned long ttime_samples;
75 unsigned long ttime_mean;
76};
77
Jens Axboe22e2c502005-06-27 10:55:12 +020078/*
Jens Axboecc09e292007-04-26 12:53:50 +020079 * Most of our rbtree usage is for sorting with min extraction, so
80 * if we cache the leftmost node we don't have to walk down the tree
81 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
82 * move this into the elevator for the rq sorting as well.
83 */
84struct cfq_rb_root {
85 struct rb_root rb;
86 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010087 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010088 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050089 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020090 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020091};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020092#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
93 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020094
95/*
Jens Axboe6118b702009-06-30 09:34:12 +020096 * Per process-grouping structure
97 */
98struct cfq_queue {
99 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100100 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200101 /* various state flags, see below */
102 unsigned int flags;
103 /* parent cfq_data */
104 struct cfq_data *cfqd;
105 /* service_tree member */
106 struct rb_node rb_node;
107 /* service_tree key */
108 unsigned long rb_key;
109 /* prio tree member */
110 struct rb_node p_node;
111 /* prio tree root we belong to, if any */
112 struct rb_root *p_root;
113 /* sorted list of pending requests */
114 struct rb_root sort_list;
115 /* if fifo isn't expired, next request to serve */
116 struct request *next_rq;
117 /* requests queued in sort_list */
118 int queued[2];
119 /* currently allocated requests */
120 int allocated[2];
121 /* fifo list of requests in sort_list */
122 struct list_head fifo;
123
Vivek Goyaldae739e2009-12-03 12:59:45 -0500124 /* time when queue got scheduled in to dispatch first request. */
125 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500126 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100127 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500128 /* time when first request from queue completed and slice started. */
129 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200130 unsigned long slice_end;
131 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200132
Christoph Hellwig65299a32011-08-23 14:50:29 +0200133 /* pending priority requests */
134 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200135 /* number of requests that are on the dispatch list or inside driver */
136 int dispatched;
137
138 /* io prio of this group */
139 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200140 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200141
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100142 pid_t pid;
143
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100144 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400145 sector_t last_request_pos;
146
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100147 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400148 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500149 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200150 /* Number of sectors dispatched from queue in single dispatch round */
151 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200152};
153
154/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100155 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100156 * IDLE is handled separately, so it has negative index
157 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400158enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100159 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500160 RT_WORKLOAD = 1,
161 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200162 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100163};
164
165/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100166 * Second index in the service_trees.
167 */
168enum wl_type_t {
169 ASYNC_WORKLOAD = 0,
170 SYNC_NOIDLE_WORKLOAD = 1,
171 SYNC_WORKLOAD = 2
172};
173
Tejun Heo155fead2012-04-01 14:38:44 -0700174struct cfqg_stats {
175#ifdef CONFIG_CFQ_GROUP_IOSCHED
176 /* total bytes transferred */
177 struct blkg_rwstat service_bytes;
178 /* total IOs serviced, post merge */
179 struct blkg_rwstat serviced;
180 /* number of ios merged */
181 struct blkg_rwstat merged;
182 /* total time spent on device in ns, may not be accurate w/ queueing */
183 struct blkg_rwstat service_time;
184 /* total time spent waiting in scheduler queue in ns */
185 struct blkg_rwstat wait_time;
186 /* number of IOs queued up */
187 struct blkg_rwstat queued;
188 /* total sectors transferred */
189 struct blkg_stat sectors;
190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time;
192#ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700203 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700204 struct blkg_stat idle_time;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time;
209 uint64_t start_idle_time;
210 uint64_t start_empty_time;
211 uint16_t flags;
212#endif /* CONFIG_DEBUG_BLK_CGROUP */
213#endif /* CONFIG_CFQ_GROUP_IOSCHED */
214};
215
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500216/* This is per cgroup per device grouping structure */
217struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700218 /* must be the first member */
219 struct blkg_policy_data pd;
220
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500221 /* group service_tree member */
222 struct rb_node rb_node;
223
224 /* group service_tree key */
225 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800226
227 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800228 * The number of active cfqgs and sum of their weights under this
229 * cfqg. This covers this cfqg's leaf_weight and all children's
230 * weights, but does not cover weights of further descendants.
231 *
232 * If a cfqg is on the service tree, it's active. An active cfqg
233 * also activates its parent and contributes to the children_weight
234 * of the parent.
235 */
236 int nr_active;
237 unsigned int children_weight;
238
239 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800240 * vfraction is the fraction of vdisktime that the tasks in this
241 * cfqg are entitled to. This is determined by compounding the
242 * ratios walking up from this cfqg to the root.
243 *
244 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
245 * vfractions on a service tree is approximately 1. The sum may
246 * deviate a bit due to rounding errors and fluctuations caused by
247 * cfqgs entering and leaving the service tree.
248 */
249 unsigned int vfraction;
250
251 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800252 * There are two weights - (internal) weight is the weight of this
253 * cfqg against the sibling cfqgs. leaf_weight is the wight of
254 * this cfqg against the child cfqgs. For the root cfqg, both
255 * weights are kept in sync for backward compatibility.
256 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500257 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100258 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700259 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500260
Tejun Heoe71357e2013-01-09 08:05:10 -0800261 unsigned int leaf_weight;
262 unsigned int new_leaf_weight;
263 unsigned int dev_leaf_weight;
264
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500265 /* number of cfqq currently on this group */
266 int nr_cfqq;
267
Jens Axboe22e2c502005-06-27 10:55:12 +0200268 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200269 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200270 * create the array for each prio class but at run time it is used
271 * only for RT and BE class and slot for IDLE class remains unused.
272 * This is primarily done to avoid confusion and a gcc warning.
273 */
274 unsigned int busy_queues_avg[CFQ_PRIO_NR];
275 /*
276 * rr lists of queues with requests. We maintain service trees for
277 * RT and BE classes. These trees are subdivided in subclasses
278 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
279 * class there is no subclassification and all the cfq queues go on
280 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100281 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200282 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100283 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100284 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500285
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400286 unsigned long saved_wl_slice;
287 enum wl_type_t saved_wl_type;
288 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800289
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200290 /* number of requests that are on the dispatch list or inside driver */
291 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200292 struct cfq_ttime ttime;
Tejun Heo155fead2012-04-01 14:38:44 -0700293 struct cfqg_stats stats;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500294};
295
Tejun Heoc5869802011-12-14 00:33:41 +0100296struct cfq_io_cq {
297 struct io_cq icq; /* must be the first member */
298 struct cfq_queue *cfqq[2];
299 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700300 int ioprio; /* the current ioprio */
301#ifdef CONFIG_CFQ_GROUP_IOSCHED
302 uint64_t blkcg_id; /* the current blkcg ID */
303#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100304};
305
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500306/*
307 * Per block device queue structure
308 */
309struct cfq_data {
310 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500311 /* Root service tree for cfq_groups */
312 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800313 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500314
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100315 /*
316 * The priority currently being served
317 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400318 enum wl_class_t serving_wl_class;
319 enum wl_type_t serving_wl_type;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100320 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500321 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200322
323 /*
324 * Each priority tree is sorted by next_request position. These
325 * trees are used when determining if two or more queues are
326 * interleaving requests (see cfq_close_cooperator).
327 */
328 struct rb_root prio_trees[CFQ_PRIO_LISTS];
329
Jens Axboe22e2c502005-06-27 10:55:12 +0200330 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100331 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200332
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100333 int rq_in_driver;
334 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200335
336 /*
337 * queue-depth detection
338 */
339 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200340 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100341 /*
342 * hw_tag can be
343 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
344 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
345 * 0 => no NCQ
346 */
347 int hw_tag_est_depth;
348 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200349
350 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200351 * idle window management
352 */
353 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200354 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200355
356 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100357 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200358
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200359 /*
360 * async queue for each priority case
361 */
362 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
363 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200364
Jens Axboe6d048f52007-04-25 12:44:27 +0200365 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /*
368 * tunables, see top of file
369 */
370 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200371 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int cfq_back_penalty;
373 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200374 unsigned int cfq_slice[2];
375 unsigned int cfq_slice_async_rq;
376 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200377 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200378 unsigned int cfq_latency;
Tao Ma5bf14c02012-04-01 14:33:39 -0700379 unsigned int cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500380
Jens Axboe6118b702009-06-30 09:34:12 +0200381 /*
382 * Fallback dummy cfqq for extreme OOM conditions
383 */
384 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200385
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100386 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387};
388
Vivek Goyal25fb5162009-12-03 12:59:46 -0500389static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
390
Vivek Goyal34b98d02012-10-03 16:56:58 -0400391static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400392 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500393 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100394{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500395 if (!cfqg)
396 return NULL;
397
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400398 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500399 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100400
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400401 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100402}
403
Jens Axboe3b181522005-06-27 10:56:24 +0200404enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100405 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
406 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200407 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100408 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100409 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
410 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
411 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100412 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200413 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400414 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100415 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100416 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500417 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200418};
419
420#define CFQ_CFQQ_FNS(name) \
421static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
422{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100423 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200424} \
425static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
426{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100427 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200428} \
429static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
430{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100431 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200432}
433
434CFQ_CFQQ_FNS(on_rr);
435CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200436CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200437CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200438CFQ_CFQQ_FNS(fifo_expire);
439CFQ_CFQQ_FNS(idle_window);
440CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100441CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200442CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200443CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100444CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100445CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500446CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200447#undef CFQ_CFQQ_FNS
448
Tejun Heof95a04a2012-04-16 13:57:26 -0700449static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
450{
451 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
452}
453
Tejun Heof95a04a2012-04-16 13:57:26 -0700454static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
455{
456 return pd_to_blkg(&cfqg->pd);
457}
458
Tejun Heo629ed0b2012-04-01 14:38:44 -0700459#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700460
Tejun Heo155fead2012-04-01 14:38:44 -0700461/* cfqg stats flags */
462enum cfqg_stats_flags {
463 CFQG_stats_waiting = 0,
464 CFQG_stats_idling,
465 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700466};
467
Tejun Heo155fead2012-04-01 14:38:44 -0700468#define CFQG_FLAG_FNS(name) \
469static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700470{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700471 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700472} \
Tejun Heo155fead2012-04-01 14:38:44 -0700473static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700474{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700475 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700476} \
Tejun Heo155fead2012-04-01 14:38:44 -0700477static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700478{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700479 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700480} \
481
Tejun Heo155fead2012-04-01 14:38:44 -0700482CFQG_FLAG_FNS(waiting)
483CFQG_FLAG_FNS(idling)
484CFQG_FLAG_FNS(empty)
485#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700486
487/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700488static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700489{
490 unsigned long long now;
491
Tejun Heo155fead2012-04-01 14:38:44 -0700492 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700493 return;
494
495 now = sched_clock();
496 if (time_after64(now, stats->start_group_wait_time))
497 blkg_stat_add(&stats->group_wait_time,
498 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700499 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700500}
501
502/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700503static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
504 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700505{
Tejun Heo155fead2012-04-01 14:38:44 -0700506 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700507
Tejun Heo155fead2012-04-01 14:38:44 -0700508 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700509 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700510 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700511 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700512 stats->start_group_wait_time = sched_clock();
513 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700514}
515
516/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700517static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700518{
519 unsigned long long now;
520
Tejun Heo155fead2012-04-01 14:38:44 -0700521 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700522 return;
523
524 now = sched_clock();
525 if (time_after64(now, stats->start_empty_time))
526 blkg_stat_add(&stats->empty_time,
527 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700528 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700529}
530
Tejun Heo155fead2012-04-01 14:38:44 -0700531static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700532{
Tejun Heo155fead2012-04-01 14:38:44 -0700533 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700534}
535
Tejun Heo155fead2012-04-01 14:38:44 -0700536static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700537{
Tejun Heo155fead2012-04-01 14:38:44 -0700538 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700539
540 if (blkg_rwstat_sum(&stats->queued))
541 return;
542
543 /*
544 * group is already marked empty. This can happen if cfqq got new
545 * request in parent group and moved to this group while being added
546 * to service tree. Just ignore the event and move on.
547 */
Tejun Heo155fead2012-04-01 14:38:44 -0700548 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700549 return;
550
551 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700552 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700553}
554
Tejun Heo155fead2012-04-01 14:38:44 -0700555static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700556{
Tejun Heo155fead2012-04-01 14:38:44 -0700557 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700558
Tejun Heo155fead2012-04-01 14:38:44 -0700559 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700560 unsigned long long now = sched_clock();
561
562 if (time_after64(now, stats->start_idle_time))
563 blkg_stat_add(&stats->idle_time,
564 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700565 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700566 }
567}
568
Tejun Heo155fead2012-04-01 14:38:44 -0700569static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700570{
Tejun Heo155fead2012-04-01 14:38:44 -0700571 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700572
Tejun Heo155fead2012-04-01 14:38:44 -0700573 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700574
575 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700576 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700577}
578
Tejun Heo155fead2012-04-01 14:38:44 -0700579static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700580{
Tejun Heo155fead2012-04-01 14:38:44 -0700581 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700582
583 blkg_stat_add(&stats->avg_queue_size_sum,
584 blkg_rwstat_sum(&stats->queued));
585 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700586 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700587}
588
589#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
590
Tejun Heof48ec1d2012-04-13 13:11:25 -0700591static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
592static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
593static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
594static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
595static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
596static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
597static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700598
599#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
600
601#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700602
Tejun Heoffea73f2012-06-04 10:02:29 +0200603static struct blkcg_policy blkcg_policy_cfq;
604
605static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
606{
607 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
608}
609
Tejun Heo7918ffb2013-01-09 08:05:11 -0800610/*
611 * Determine the parent cfqg for weight calculation. Currently, cfqg
612 * scheduling is flat and the root is the parent of everyone else.
613 */
614static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg)
615{
616 struct blkcg_gq *blkg = cfqg_to_blkg(cfqg);
617 struct cfq_group *root;
618
619 while (blkg->parent)
620 blkg = blkg->parent;
621 root = blkg_to_cfqg(blkg);
622
623 return root != cfqg ? root : NULL;
624}
625
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100626static inline void cfqg_get(struct cfq_group *cfqg)
627{
628 return blkg_get(cfqg_to_blkg(cfqg));
629}
630
631static inline void cfqg_put(struct cfq_group *cfqg)
632{
633 return blkg_put(cfqg_to_blkg(cfqg));
634}
635
Tejun Heo54e7ed12012-04-16 13:57:23 -0700636#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
637 char __pbuf[128]; \
638 \
639 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400640 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
641 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
642 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700643 __pbuf, ##args); \
644} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500645
Tejun Heo54e7ed12012-04-16 13:57:23 -0700646#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
647 char __pbuf[128]; \
648 \
649 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
650 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
651} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500652
Tejun Heo155fead2012-04-01 14:38:44 -0700653static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
654 struct cfq_group *curr_cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700655{
Tejun Heo155fead2012-04-01 14:38:44 -0700656 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
657 cfqg_stats_end_empty_time(&cfqg->stats);
658 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700659}
660
Tejun Heo155fead2012-04-01 14:38:44 -0700661static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
662 unsigned long time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700663{
Tejun Heo155fead2012-04-01 14:38:44 -0700664 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700665#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700666 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700667#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700668}
669
Tejun Heo155fead2012-04-01 14:38:44 -0700670static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700671{
Tejun Heo155fead2012-04-01 14:38:44 -0700672 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700673}
674
Tejun Heo155fead2012-04-01 14:38:44 -0700675static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700676{
Tejun Heo155fead2012-04-01 14:38:44 -0700677 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700678}
679
Tejun Heo155fead2012-04-01 14:38:44 -0700680static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
681 uint64_t bytes, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700682{
Tejun Heo155fead2012-04-01 14:38:44 -0700683 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
684 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
685 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700686}
687
Tejun Heo155fead2012-04-01 14:38:44 -0700688static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
689 uint64_t start_time, uint64_t io_start_time, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700690{
Tejun Heo155fead2012-04-01 14:38:44 -0700691 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700692 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700693
694 if (time_after64(now, io_start_time))
695 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
696 if (time_after64(io_start_time, start_time))
697 blkg_rwstat_add(&stats->wait_time, rw,
698 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700699}
700
Tejun Heo3c798392012-04-16 13:57:25 -0700701static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo155fead2012-04-01 14:38:44 -0700702{
703 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
704 struct cfqg_stats *stats = &cfqg->stats;
705
706 /* queued stats shouldn't be cleared */
707 blkg_rwstat_reset(&stats->service_bytes);
708 blkg_rwstat_reset(&stats->serviced);
709 blkg_rwstat_reset(&stats->merged);
710 blkg_rwstat_reset(&stats->service_time);
711 blkg_rwstat_reset(&stats->wait_time);
712 blkg_stat_reset(&stats->time);
713#ifdef CONFIG_DEBUG_BLK_CGROUP
714 blkg_stat_reset(&stats->unaccounted_time);
715 blkg_stat_reset(&stats->avg_queue_size_sum);
716 blkg_stat_reset(&stats->avg_queue_size_samples);
717 blkg_stat_reset(&stats->dequeue);
718 blkg_stat_reset(&stats->group_wait_time);
719 blkg_stat_reset(&stats->idle_time);
720 blkg_stat_reset(&stats->empty_time);
721#endif
722}
723
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100724#else /* CONFIG_CFQ_GROUP_IOSCHED */
725
Tejun Heo7918ffb2013-01-09 08:05:11 -0800726static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg) { return NULL; }
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100727static inline void cfqg_get(struct cfq_group *cfqg) { }
728static inline void cfqg_put(struct cfq_group *cfqg) { }
729
Jens Axboe7b679132008-05-30 12:23:07 +0200730#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400731 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
732 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
733 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
734 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200735#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100736
Tejun Heo155fead2012-04-01 14:38:44 -0700737static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
738 struct cfq_group *curr_cfqg, int rw) { }
739static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
740 unsigned long time, unsigned long unaccounted_time) { }
741static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
742static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
743static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
744 uint64_t bytes, int rw) { }
745static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
746 uint64_t start_time, uint64_t io_start_time, int rw) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700747
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100748#endif /* CONFIG_CFQ_GROUP_IOSCHED */
749
Jens Axboe7b679132008-05-30 12:23:07 +0200750#define cfq_log(cfqd, fmt, args...) \
751 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
752
Vivek Goyal615f0252009-12-03 12:59:39 -0500753/* Traverses through cfq group service trees */
754#define for_each_cfqg_st(cfqg, i, j, st) \
755 for (i = 0; i <= IDLE_WORKLOAD; i++) \
756 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
757 : &cfqg->service_tree_idle; \
758 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
759 (i == IDLE_WORKLOAD && j == 0); \
760 j++, st = i < IDLE_WORKLOAD ? \
761 &cfqg->service_trees[i][j]: NULL) \
762
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200763static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
764 struct cfq_ttime *ttime, bool group_idle)
765{
766 unsigned long slice;
767 if (!sample_valid(ttime->ttime_samples))
768 return false;
769 if (group_idle)
770 slice = cfqd->cfq_group_idle;
771 else
772 slice = cfqd->cfq_slice_idle;
773 return ttime->ttime_mean > slice;
774}
Vivek Goyal615f0252009-12-03 12:59:39 -0500775
Vivek Goyal02b35082010-08-23 12:23:53 +0200776static inline bool iops_mode(struct cfq_data *cfqd)
777{
778 /*
779 * If we are not idling on queues and it is a NCQ drive, parallel
780 * execution of requests is on and measuring time is not possible
781 * in most of the cases until and unless we drive shallower queue
782 * depths and that becomes a performance bottleneck. In such cases
783 * switch to start providing fairness in terms of number of IOs.
784 */
785 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
786 return true;
787 else
788 return false;
789}
790
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400791static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100792{
793 if (cfq_class_idle(cfqq))
794 return IDLE_WORKLOAD;
795 if (cfq_class_rt(cfqq))
796 return RT_WORKLOAD;
797 return BE_WORKLOAD;
798}
799
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100800
801static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
802{
803 if (!cfq_cfqq_sync(cfqq))
804 return ASYNC_WORKLOAD;
805 if (!cfq_cfqq_idle_window(cfqq))
806 return SYNC_NOIDLE_WORKLOAD;
807 return SYNC_WORKLOAD;
808}
809
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400810static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500811 struct cfq_data *cfqd,
812 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100813{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400814 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500815 return cfqg->service_tree_idle.count;
816
Vivek Goyal34b98d02012-10-03 16:56:58 -0400817 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
818 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
819 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100820}
821
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500822static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
823 struct cfq_group *cfqg)
824{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400825 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
826 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500827}
828
Jens Axboe165125e2007-07-24 09:28:11 +0200829static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800830static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700831 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800832 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200833
Tejun Heoc5869802011-12-14 00:33:41 +0100834static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
835{
836 /* cic->icq is the first member, %NULL will convert to %NULL */
837 return container_of(icq, struct cfq_io_cq, icq);
838}
839
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100840static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
841 struct io_context *ioc)
842{
843 if (ioc)
844 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
845 return NULL;
846}
847
Tejun Heoc5869802011-12-14 00:33:41 +0100848static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200849{
Jens Axboea6151c32009-10-07 20:02:57 +0200850 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200851}
852
Tejun Heoc5869802011-12-14 00:33:41 +0100853static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
854 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200855{
Jens Axboea6151c32009-10-07 20:02:57 +0200856 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200857}
858
Tejun Heoc5869802011-12-14 00:33:41 +0100859static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400860{
Tejun Heoc5869802011-12-14 00:33:41 +0100861 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400862}
863
Vasily Tarasov91fac312007-04-25 12:29:51 +0200864/*
865 * We regard a request as SYNC, if it's either a read or has the SYNC bit
866 * set (in which case it could also be direct WRITE).
867 */
Jens Axboea6151c32009-10-07 20:02:57 +0200868static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200869{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200870 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200871}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700874 * scheduler run of queue, if there are requests pending and no one in the
875 * driver that will restart queueing
876 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200877static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700878{
Jens Axboe7b679132008-05-30 12:23:07 +0200879 if (cfqd->busy_queues) {
880 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200881 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200882 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100886 * Scale schedule slice based on io priority. Use the sync time slice only
887 * if a queue is marked sync and has sync io queued. A sync queue with async
888 * io only, should not get full sync slice length.
889 */
Jens Axboea6151c32009-10-07 20:02:57 +0200890static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200891 unsigned short prio)
892{
893 const int base_slice = cfqd->cfq_slice[sync];
894
895 WARN_ON(prio >= IOPRIO_BE_NR);
896
897 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
898}
899
Jens Axboe44f7c162007-01-19 11:51:58 +1100900static inline int
901cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
902{
Jens Axboed9e76202007-04-20 14:27:50 +0200903 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100904}
905
Tejun Heo1d3650f2013-01-09 08:05:11 -0800906/**
907 * cfqg_scale_charge - scale disk time charge according to cfqg weight
908 * @charge: disk time being charged
909 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
910 *
911 * Scale @charge according to @vfraction, which is in range (0, 1]. The
912 * scaling is inversely proportional.
913 *
914 * scaled = charge / vfraction
915 *
916 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
917 */
918static inline u64 cfqg_scale_charge(unsigned long charge,
919 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500920{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800921 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500922
Tejun Heo1d3650f2013-01-09 08:05:11 -0800923 /* charge / vfraction */
924 c <<= CFQ_SERVICE_SHIFT;
925 do_div(c, vfraction);
926 return c;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500927}
928
929static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
930{
931 s64 delta = (s64)(vdisktime - min_vdisktime);
932 if (delta > 0)
933 min_vdisktime = vdisktime;
934
935 return min_vdisktime;
936}
937
938static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
939{
940 s64 delta = (s64)(vdisktime - min_vdisktime);
941 if (delta < 0)
942 min_vdisktime = vdisktime;
943
944 return min_vdisktime;
945}
946
947static void update_min_vdisktime(struct cfq_rb_root *st)
948{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500949 struct cfq_group *cfqg;
950
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500951 if (st->left) {
952 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100953 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
954 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500955 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500956}
957
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100958/*
959 * get averaged number of queues of RT/BE priority.
960 * average is updated, with a formula that gives more weight to higher numbers,
961 * to quickly follows sudden increases and decrease slowly
962 */
963
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500964static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
965 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100966{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100967 unsigned min_q, max_q;
968 unsigned mult = cfq_hist_divisor - 1;
969 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500970 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100971
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500972 min_q = min(cfqg->busy_queues_avg[rt], busy);
973 max_q = max(cfqg->busy_queues_avg[rt], busy);
974 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100975 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500976 return cfqg->busy_queues_avg[rt];
977}
978
979static inline unsigned
980cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
981{
982 struct cfq_rb_root *st = &cfqd->grp_service_tree;
983
Tao Ma5bf14c02012-04-01 14:33:39 -0700984 return cfqd->cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100985}
986
Shaohua Lic553f8e2011-01-14 08:41:03 +0100987static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700988cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100989{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100990 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
991 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500992 /*
993 * interested queues (we consider only the ones with the same
994 * priority class in the cfq group)
995 */
996 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
997 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100998 unsigned sync_slice = cfqd->cfq_slice[1];
999 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001000 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
1001
1002 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001003 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
1004 /* scale low_slice according to IO priority
1005 * and sync vs async */
1006 unsigned low_slice =
1007 min(slice, base_low_slice * slice / sync_slice);
1008 /* the adapted slice value is scaled to fit all iqs
1009 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001010 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001011 low_slice);
1012 }
1013 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001014 return slice;
1015}
1016
1017static inline void
1018cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1019{
Vivek Goyalba5bd522011-01-19 08:25:02 -07001020 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001021
Vivek Goyaldae739e2009-12-03 12:59:45 -05001022 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001023 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001024 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +02001025 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +11001026}
1027
1028/*
1029 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1030 * isn't valid until the first request from the dispatch is activated
1031 * and the slice time set.
1032 */
Jens Axboea6151c32009-10-07 20:02:57 +02001033static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001034{
1035 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001036 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001037 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01001038 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001039
Shaohua Lic1e44752010-11-08 15:01:02 +01001040 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001041}
1042
1043/*
Jens Axboe5e705372006-07-13 12:39:25 +02001044 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001046 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
Jens Axboe5e705372006-07-13 12:39:25 +02001048static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001049cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001051 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001053#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1054#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1055 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Jens Axboe5e705372006-07-13 12:39:25 +02001057 if (rq1 == NULL || rq1 == rq2)
1058 return rq2;
1059 if (rq2 == NULL)
1060 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001061
Namhyung Kim229836b2011-05-24 10:23:21 +02001062 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1063 return rq_is_sync(rq1) ? rq1 : rq2;
1064
Christoph Hellwig65299a32011-08-23 14:50:29 +02001065 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1066 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001067
Tejun Heo83096eb2009-05-07 22:24:39 +09001068 s1 = blk_rq_pos(rq1);
1069 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /*
1072 * by definition, 1KiB is 2 sectors
1073 */
1074 back_max = cfqd->cfq_back_max * 2;
1075
1076 /*
1077 * Strict one way elevator _except_ in the case where we allow
1078 * short backward seeks which are biased as twice the cost of a
1079 * similar forward seek.
1080 */
1081 if (s1 >= last)
1082 d1 = s1 - last;
1083 else if (s1 + back_max >= last)
1084 d1 = (last - s1) * cfqd->cfq_back_penalty;
1085 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001086 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (s2 >= last)
1089 d2 = s2 - last;
1090 else if (s2 + back_max >= last)
1091 d2 = (last - s2) * cfqd->cfq_back_penalty;
1092 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001093 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Andreas Mohre8a99052006-03-28 08:59:49 +02001097 /*
1098 * By doing switch() on the bit mask "wrap" we avoid having to
1099 * check two variables for all permutations: --> faster!
1100 */
1101 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001102 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001103 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001104 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001105 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001106 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001107 else {
1108 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001109 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001110 else
Jens Axboe5e705372006-07-13 12:39:25 +02001111 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001112 }
1113
1114 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001115 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001116 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001117 return rq2;
1118 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001119 default:
1120 /*
1121 * Since both rqs are wrapped,
1122 * start with the one that's further behind head
1123 * (--> only *one* back seek required),
1124 * since back seek takes more time than forward.
1125 */
1126 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001127 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 else
Jens Axboe5e705372006-07-13 12:39:25 +02001129 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131}
1132
Jens Axboe498d3aa22007-04-26 12:54:48 +02001133/*
1134 * The below is leftmost cache rbtree addon
1135 */
Jens Axboe08717142008-01-28 11:38:15 +01001136static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001137{
Vivek Goyal615f0252009-12-03 12:59:39 -05001138 /* Service tree is empty */
1139 if (!root->count)
1140 return NULL;
1141
Jens Axboecc09e292007-04-26 12:53:50 +02001142 if (!root->left)
1143 root->left = rb_first(&root->rb);
1144
Jens Axboe08717142008-01-28 11:38:15 +01001145 if (root->left)
1146 return rb_entry(root->left, struct cfq_queue, rb_node);
1147
1148 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001149}
1150
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001151static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1152{
1153 if (!root->left)
1154 root->left = rb_first(&root->rb);
1155
1156 if (root->left)
1157 return rb_entry_cfqg(root->left);
1158
1159 return NULL;
1160}
1161
Jens Axboea36e71f2009-04-15 12:15:11 +02001162static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1163{
1164 rb_erase(n, root);
1165 RB_CLEAR_NODE(n);
1166}
1167
Jens Axboecc09e292007-04-26 12:53:50 +02001168static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1169{
1170 if (root->left == n)
1171 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001172 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001173 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/*
1177 * would be nice to take fifo expire time into account as well
1178 */
Jens Axboe5e705372006-07-13 12:39:25 +02001179static struct request *
1180cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1181 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Jens Axboe21183b02006-07-13 12:33:14 +02001183 struct rb_node *rbnext = rb_next(&last->rb_node);
1184 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001185 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Jens Axboe21183b02006-07-13 12:33:14 +02001187 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001190 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001193 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001194 else {
1195 rbnext = rb_first(&cfqq->sort_list);
1196 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001197 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001200 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Jens Axboed9e76202007-04-20 14:27:50 +02001203static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1204 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Jens Axboed9e76202007-04-20 14:27:50 +02001206 /*
1207 * just an approximation, should be ok.
1208 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001209 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001210 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001213static inline s64
1214cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1215{
1216 return cfqg->vdisktime - st->min_vdisktime;
1217}
1218
1219static void
1220__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1221{
1222 struct rb_node **node = &st->rb.rb_node;
1223 struct rb_node *parent = NULL;
1224 struct cfq_group *__cfqg;
1225 s64 key = cfqg_key(st, cfqg);
1226 int left = 1;
1227
1228 while (*node != NULL) {
1229 parent = *node;
1230 __cfqg = rb_entry_cfqg(parent);
1231
1232 if (key < cfqg_key(st, __cfqg))
1233 node = &parent->rb_left;
1234 else {
1235 node = &parent->rb_right;
1236 left = 0;
1237 }
1238 }
1239
1240 if (left)
1241 st->left = &cfqg->rb_node;
1242
1243 rb_link_node(&cfqg->rb_node, parent, node);
1244 rb_insert_color(&cfqg->rb_node, &st->rb);
1245}
1246
1247static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001248cfq_update_group_weight(struct cfq_group *cfqg)
1249{
1250 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001251
Tejun Heo3381cb82012-04-01 14:38:44 -07001252 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001253 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001254 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001255 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001256
1257 if (cfqg->new_leaf_weight) {
1258 cfqg->leaf_weight = cfqg->new_leaf_weight;
1259 cfqg->new_leaf_weight = 0;
1260 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001261}
1262
1263static void
1264cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1265{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001266 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001267 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001268 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001269 bool propagate;
1270
1271 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001272 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1273
1274 cfq_update_group_weight(cfqg);
1275 __cfq_group_service_tree_add(st, cfqg);
1276 st->total_weight += cfqg->weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001277
1278 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001279 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1280 * entitled to. vfraction is calculated by walking the tree
1281 * towards the root calculating the fraction it has at each level.
1282 * The compounded ratio is how much vfraction @cfqg owns.
1283 *
1284 * Start with the proportion tasks in this cfqg has against active
1285 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001286 */
1287 propagate = !pos->nr_active++;
1288 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001289 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001290
Tejun Heo1d3650f2013-01-09 08:05:11 -08001291 /*
1292 * Compound ->weight walking up the tree. Both activation and
1293 * vfraction calculation are done in the same loop. Propagation
1294 * stops once an already activated node is met. vfraction
1295 * calculation should always continue to the root.
1296 */
1297 while ((parent = cfqg_flat_parent(pos))) {
1298 if (propagate) {
1299 propagate = !parent->nr_active++;
1300 parent->children_weight += pos->weight;
1301 }
1302 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001303 pos = parent;
1304 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001305
1306 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001307}
1308
1309static void
1310cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001311{
1312 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1313 struct cfq_group *__cfqg;
1314 struct rb_node *n;
1315
1316 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001317 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001318 return;
1319
1320 /*
1321 * Currently put the group at the end. Later implement something
1322 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001323 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001324 */
1325 n = rb_last(&st->rb);
1326 if (n) {
1327 __cfqg = rb_entry_cfqg(n);
1328 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1329 } else
1330 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001331 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001332}
1333
1334static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001335cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1336{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001337 struct cfq_group *pos = cfqg;
1338 bool propagate;
1339
1340 /*
1341 * Undo activation from cfq_group_service_tree_add(). Deactivate
1342 * @cfqg and propagate deactivation upwards.
1343 */
1344 propagate = !--pos->nr_active;
1345 pos->children_weight -= pos->leaf_weight;
1346
1347 while (propagate) {
1348 struct cfq_group *parent = cfqg_flat_parent(pos);
1349
1350 /* @pos has 0 nr_active at this point */
1351 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001352 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001353
1354 if (!parent)
1355 break;
1356
1357 propagate = !--parent->nr_active;
1358 parent->children_weight -= pos->weight;
1359 pos = parent;
1360 }
1361
1362 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001363 st->total_weight -= cfqg->weight;
1364 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1365 cfq_rb_erase(&cfqg->rb_node, st);
1366}
1367
1368static void
1369cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001370{
1371 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1372
1373 BUG_ON(cfqg->nr_cfqq < 1);
1374 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001375
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001376 /* If there are other cfq queues under this group, don't delete it */
1377 if (cfqg->nr_cfqq)
1378 return;
1379
Vivek Goyal2868ef72009-12-03 12:59:48 -05001380 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001381 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001382 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001383 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001384}
1385
Justin TerAvest167400d2011-03-12 16:54:00 +01001386static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1387 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001388{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001389 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001390
1391 /*
1392 * Queue got expired before even a single request completed or
1393 * got expired immediately after first request completion.
1394 */
1395 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1396 /*
1397 * Also charge the seek time incurred to the group, otherwise
1398 * if there are mutiple queues in the group, each can dispatch
1399 * a single request on seeky media and cause lots of seek time
1400 * and group will never know it.
1401 */
1402 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1403 1);
1404 } else {
1405 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001406 if (slice_used > cfqq->allocated_slice) {
1407 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001408 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001409 }
1410 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1411 *unaccounted_time += cfqq->slice_start -
1412 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001413 }
1414
Vivek Goyaldae739e2009-12-03 12:59:45 -05001415 return slice_used;
1416}
1417
1418static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001419 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001420{
1421 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001422 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001423 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1424 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001425 unsigned int vfr;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001426
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001427 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001428 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001429
Vivek Goyal02b35082010-08-23 12:23:53 +02001430 if (iops_mode(cfqd))
1431 charge = cfqq->slice_dispatch;
1432 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1433 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001434
Tejun Heo1d3650f2013-01-09 08:05:11 -08001435 /*
1436 * Can't update vdisktime while on service tree and cfqg->vfraction
1437 * is valid only while on it. Cache vfr, leave the service tree,
1438 * update vdisktime and go back on. The re-addition to the tree
1439 * will also update the weights as necessary.
1440 */
1441 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001442 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001443 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001444 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001445
1446 /* This group is being expired. Save the context */
1447 if (time_after(cfqd->workload_expires, jiffies)) {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001448 cfqg->saved_wl_slice = cfqd->workload_expires
Vivek Goyaldae739e2009-12-03 12:59:45 -05001449 - jiffies;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001450 cfqg->saved_wl_type = cfqd->serving_wl_type;
1451 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001452 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001453 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001454
1455 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1456 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001457 cfq_log_cfqq(cfqq->cfqd, cfqq,
1458 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1459 used_sl, cfqq->slice_dispatch, charge,
1460 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001461 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1462 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001463}
1464
Tejun Heof51b8022012-03-05 13:15:05 -08001465/**
1466 * cfq_init_cfqg_base - initialize base part of a cfq_group
1467 * @cfqg: cfq_group to initialize
1468 *
1469 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1470 * is enabled or not.
1471 */
1472static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1473{
1474 struct cfq_rb_root *st;
1475 int i, j;
1476
1477 for_each_cfqg_st(cfqg, i, j, st)
1478 *st = CFQ_RB_ROOT;
1479 RB_CLEAR_NODE(&cfqg->rb_node);
1480
1481 cfqg->ttime.last_end_request = jiffies;
1482}
1483
Vivek Goyal25fb5162009-12-03 12:59:46 -05001484#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07001485static void cfq_pd_init(struct blkcg_gq *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001486{
Tejun Heo03814112012-03-05 13:15:14 -08001487 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001488
Tejun Heof51b8022012-03-05 13:15:05 -08001489 cfq_init_cfqg_base(cfqg);
Tejun Heo3381cb82012-04-01 14:38:44 -07001490 cfqg->weight = blkg->blkcg->cfq_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001491 cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001492}
1493
1494/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001495 * Search for the cfq group current task belongs to. request_queue lock must
1496 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001497 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001498static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001499 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001500{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001501 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001502 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001503
Tejun Heo3c798392012-04-16 13:57:25 -07001504 /* avoid lookup for the common case where there's no blkcg */
1505 if (blkcg == &blkcg_root) {
Tejun Heocd1604f2012-03-05 13:15:06 -08001506 cfqg = cfqd->root_group;
1507 } else {
Tejun Heo3c798392012-04-16 13:57:25 -07001508 struct blkcg_gq *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001509
Tejun Heo3c96cb32012-04-13 13:11:34 -07001510 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -08001511 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001512 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001513 }
1514
Vivek Goyal25fb5162009-12-03 12:59:46 -05001515 return cfqg;
1516}
1517
1518static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1519{
1520 /* Currently, all async queues are mapped to root group */
1521 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001522 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001523
1524 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001525 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001526 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001527}
1528
Tejun Heof95a04a2012-04-16 13:57:26 -07001529static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1530 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001531{
Tejun Heof95a04a2012-04-16 13:57:26 -07001532 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001533
1534 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001535 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001536 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001537}
1538
Tejun Heo3381cb82012-04-01 14:38:44 -07001539static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1540 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001541{
Tejun Heo3c798392012-04-16 13:57:25 -07001542 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1543 cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001544 false);
1545 return 0;
1546}
1547
Tejun Heoe71357e2013-01-09 08:05:10 -08001548static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1549 struct blkg_policy_data *pd, int off)
1550{
1551 struct cfq_group *cfqg = pd_to_cfqg(pd);
1552
1553 if (!cfqg->dev_leaf_weight)
1554 return 0;
1555 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1556}
1557
1558static int cfqg_print_leaf_weight_device(struct cgroup *cgrp,
1559 struct cftype *cft,
1560 struct seq_file *sf)
1561{
1562 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1563 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0,
1564 false);
1565 return 0;
1566}
1567
Tejun Heo3381cb82012-04-01 14:38:44 -07001568static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
1569 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001570{
Tejun Heo3c798392012-04-16 13:57:25 -07001571 seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001572 return 0;
1573}
1574
Tejun Heoe71357e2013-01-09 08:05:10 -08001575static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft,
1576 struct seq_file *sf)
1577{
1578 seq_printf(sf, "%u\n",
1579 cgroup_to_blkcg(cgrp)->cfq_leaf_weight);
1580 return 0;
1581}
1582
1583static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1584 const char *buf, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001585{
Tejun Heo3c798392012-04-16 13:57:25 -07001586 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001587 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001588 struct cfq_group *cfqg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001589 int ret;
1590
Tejun Heo3c798392012-04-16 13:57:25 -07001591 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001592 if (ret)
1593 return ret;
1594
1595 ret = -EINVAL;
Tejun Heo3381cb82012-04-01 14:38:44 -07001596 cfqg = blkg_to_cfqg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -07001597 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001598 if (!is_leaf_weight) {
1599 cfqg->dev_weight = ctx.v;
1600 cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
1601 } else {
1602 cfqg->dev_leaf_weight = ctx.v;
1603 cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
1604 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001605 ret = 0;
1606 }
1607
1608 blkg_conf_finish(&ctx);
1609 return ret;
1610}
1611
Tejun Heoe71357e2013-01-09 08:05:10 -08001612static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1613 const char *buf)
1614{
1615 return __cfqg_set_weight_device(cgrp, cft, buf, false);
1616}
1617
1618static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft,
1619 const char *buf)
1620{
1621 return __cfqg_set_weight_device(cgrp, cft, buf, true);
1622}
1623
1624static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val,
1625 bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001626{
Tejun Heo3c798392012-04-16 13:57:25 -07001627 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
1628 struct blkcg_gq *blkg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001629 struct hlist_node *n;
1630
Tejun Heo3381cb82012-04-01 14:38:44 -07001631 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001632 return -EINVAL;
1633
1634 spin_lock_irq(&blkcg->lock);
Tejun Heoe71357e2013-01-09 08:05:10 -08001635
1636 if (!is_leaf_weight)
1637 blkcg->cfq_weight = val;
1638 else
1639 blkcg->cfq_leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001640
1641 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001642 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001643
Tejun Heoe71357e2013-01-09 08:05:10 -08001644 if (!cfqg)
1645 continue;
1646
1647 if (!is_leaf_weight) {
1648 if (!cfqg->dev_weight)
1649 cfqg->new_weight = blkcg->cfq_weight;
1650 } else {
1651 if (!cfqg->dev_leaf_weight)
1652 cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
1653 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001654 }
1655
1656 spin_unlock_irq(&blkcg->lock);
1657 return 0;
1658}
1659
Tejun Heoe71357e2013-01-09 08:05:10 -08001660static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1661{
1662 return __cfq_set_weight(cgrp, cft, val, false);
1663}
1664
1665static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1666{
1667 return __cfq_set_weight(cgrp, cft, val, true);
1668}
1669
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001670static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
1671 struct seq_file *sf)
1672{
Tejun Heo3c798392012-04-16 13:57:25 -07001673 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001674
Tejun Heo3c798392012-04-16 13:57:25 -07001675 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001676 cft->private, false);
1677 return 0;
1678}
1679
1680static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
1681 struct seq_file *sf)
1682{
Tejun Heo3c798392012-04-16 13:57:25 -07001683 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001684
Tejun Heo3c798392012-04-16 13:57:25 -07001685 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001686 cft->private, true);
1687 return 0;
1688}
1689
Tejun Heo60c2bc22012-04-01 14:38:43 -07001690#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001691static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1692 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001693{
Tejun Heof95a04a2012-04-16 13:57:26 -07001694 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001695 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001696 u64 v = 0;
1697
1698 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001699 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001700 do_div(v, samples);
1701 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001702 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001703 return 0;
1704}
1705
1706/* print avg_queue_size */
Tejun Heo155fead2012-04-01 14:38:44 -07001707static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1708 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001709{
Tejun Heo3c798392012-04-16 13:57:25 -07001710 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001711
Tejun Heo155fead2012-04-01 14:38:44 -07001712 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
Tejun Heo3c798392012-04-16 13:57:25 -07001713 &blkcg_policy_cfq, 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001714 return 0;
1715}
1716#endif /* CONFIG_DEBUG_BLK_CGROUP */
1717
1718static struct cftype cfq_blkcg_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001719 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001720 {
1721 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001722 .flags = CFTYPE_ONLY_ON_ROOT,
1723 .read_seq_string = cfqg_print_leaf_weight_device,
1724 .write_string = cfqg_set_leaf_weight_device,
1725 .max_write_len = 256,
1726 },
1727 {
1728 .name = "weight",
1729 .flags = CFTYPE_ONLY_ON_ROOT,
1730 .read_seq_string = cfq_print_leaf_weight,
1731 .write_u64 = cfq_set_leaf_weight,
1732 },
1733
1734 /* no such mapping necessary for !roots */
1735 {
1736 .name = "weight_device",
1737 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo3381cb82012-04-01 14:38:44 -07001738 .read_seq_string = cfqg_print_weight_device,
1739 .write_string = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001740 .max_write_len = 256,
1741 },
1742 {
1743 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001744 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001745 .read_seq_string = cfq_print_weight,
1746 .write_u64 = cfq_set_weight,
1747 },
1748
1749 {
1750 .name = "leaf_weight_device",
Tejun Heoe71357e2013-01-09 08:05:10 -08001751 .read_seq_string = cfqg_print_leaf_weight_device,
1752 .write_string = cfqg_set_leaf_weight_device,
1753 .max_write_len = 256,
1754 },
1755 {
1756 .name = "leaf_weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001757 .read_seq_string = cfq_print_leaf_weight,
1758 .write_u64 = cfq_set_leaf_weight,
1759 },
1760
Tejun Heo60c2bc22012-04-01 14:38:43 -07001761 {
1762 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001763 .private = offsetof(struct cfq_group, stats.time),
1764 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001765 },
1766 {
1767 .name = "sectors",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001768 .private = offsetof(struct cfq_group, stats.sectors),
1769 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001770 },
1771 {
1772 .name = "io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001773 .private = offsetof(struct cfq_group, stats.service_bytes),
1774 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001775 },
1776 {
1777 .name = "io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001778 .private = offsetof(struct cfq_group, stats.serviced),
1779 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001780 },
1781 {
1782 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001783 .private = offsetof(struct cfq_group, stats.service_time),
1784 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001785 },
1786 {
1787 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001788 .private = offsetof(struct cfq_group, stats.wait_time),
1789 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001790 },
1791 {
1792 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001793 .private = offsetof(struct cfq_group, stats.merged),
1794 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001795 },
1796 {
1797 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001798 .private = offsetof(struct cfq_group, stats.queued),
1799 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001800 },
1801#ifdef CONFIG_DEBUG_BLK_CGROUP
1802 {
1803 .name = "avg_queue_size",
Tejun Heo155fead2012-04-01 14:38:44 -07001804 .read_seq_string = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001805 },
1806 {
1807 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001808 .private = offsetof(struct cfq_group, stats.group_wait_time),
1809 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001810 },
1811 {
1812 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001813 .private = offsetof(struct cfq_group, stats.idle_time),
1814 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001815 },
1816 {
1817 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001818 .private = offsetof(struct cfq_group, stats.empty_time),
1819 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001820 },
1821 {
1822 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001823 .private = offsetof(struct cfq_group, stats.dequeue),
1824 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001825 },
1826 {
1827 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001828 .private = offsetof(struct cfq_group, stats.unaccounted_time),
1829 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001830 },
1831#endif /* CONFIG_DEBUG_BLK_CGROUP */
1832 { } /* terminate */
1833};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001834#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001835static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001836 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001837{
Tejun Heof51b8022012-03-05 13:15:05 -08001838 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001839}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001840
Vivek Goyal25fb5162009-12-03 12:59:46 -05001841static inline void
1842cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1843 cfqq->cfqg = cfqg;
1844}
1845
1846#endif /* GROUP_IOSCHED */
1847
Jens Axboe498d3aa22007-04-26 12:54:48 +02001848/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001849 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001850 * requests waiting to be processed. It is sorted in the order that
1851 * we will service the queues.
1852 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001853static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001854 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001855{
Jens Axboe08717142008-01-28 11:38:15 +01001856 struct rb_node **p, *parent;
1857 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001858 unsigned long rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001859 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001860 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001861 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001862
Vivek Goyal34b98d02012-10-03 16:56:58 -04001863 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001864 if (cfq_class_idle(cfqq)) {
1865 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001866 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001867 if (parent && parent != &cfqq->rb_node) {
1868 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1869 rb_key += __cfqq->rb_key;
1870 } else
1871 rb_key += jiffies;
1872 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001873 /*
1874 * Get our rb key offset. Subtract any residual slice
1875 * value carried from last service. A negative resid
1876 * count indicates slice overrun, and this should position
1877 * the next service time further away in the tree.
1878 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001879 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001880 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001881 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001882 } else {
1883 rb_key = -HZ;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001884 __cfqq = cfq_rb_first(st);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001885 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1886 }
Jens Axboed9e76202007-04-20 14:27:50 +02001887
1888 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001889 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001890 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001891 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001892 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04001893 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02001894 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001895
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001896 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1897 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001898 }
Jens Axboed9e76202007-04-20 14:27:50 +02001899
Jens Axboe498d3aa22007-04-26 12:54:48 +02001900 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001901 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001902 cfqq->service_tree = st;
1903 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001904 while (*p) {
1905 parent = *p;
1906 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1907
Jens Axboe0c534e02007-04-18 20:01:57 +02001908 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001909 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001910 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001911 if (time_before(rb_key, __cfqq->rb_key))
Vivek Goyal1f23f122012-10-03 16:57:00 -04001912 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001913 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04001914 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001915 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001916 }
Jens Axboed9e76202007-04-20 14:27:50 +02001917 }
1918
Jens Axboecc09e292007-04-26 12:53:50 +02001919 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04001920 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001921
Jens Axboed9e76202007-04-20 14:27:50 +02001922 cfqq->rb_key = rb_key;
1923 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04001924 rb_insert_color(&cfqq->rb_node, &st->rb);
1925 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001926 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001927 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001928 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Jens Axboea36e71f2009-04-15 12:15:11 +02001931static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001932cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1933 sector_t sector, struct rb_node **ret_parent,
1934 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001935{
Jens Axboea36e71f2009-04-15 12:15:11 +02001936 struct rb_node **p, *parent;
1937 struct cfq_queue *cfqq = NULL;
1938
1939 parent = NULL;
1940 p = &root->rb_node;
1941 while (*p) {
1942 struct rb_node **n;
1943
1944 parent = *p;
1945 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1946
1947 /*
1948 * Sort strictly based on sector. Smallest to the left,
1949 * largest to the right.
1950 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001951 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001952 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001953 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001954 n = &(*p)->rb_left;
1955 else
1956 break;
1957 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001958 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001959 }
1960
1961 *ret_parent = parent;
1962 if (rb_link)
1963 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001964 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001965}
1966
1967static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1968{
Jens Axboea36e71f2009-04-15 12:15:11 +02001969 struct rb_node **p, *parent;
1970 struct cfq_queue *__cfqq;
1971
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001972 if (cfqq->p_root) {
1973 rb_erase(&cfqq->p_node, cfqq->p_root);
1974 cfqq->p_root = NULL;
1975 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001976
1977 if (cfq_class_idle(cfqq))
1978 return;
1979 if (!cfqq->next_rq)
1980 return;
1981
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001982 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001983 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1984 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001985 if (!__cfqq) {
1986 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001987 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1988 } else
1989 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001990}
1991
Jens Axboe498d3aa22007-04-26 12:54:48 +02001992/*
1993 * Update cfqq's position in the service tree.
1994 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001995static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001996{
Jens Axboe6d048f52007-04-25 12:44:27 +02001997 /*
1998 * Resorting requires the cfqq to be on the RR list already.
1999 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002000 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002001 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002002 cfq_prio_tree_add(cfqd, cfqq);
2003 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
2007 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002008 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Jens Axboefebffd62008-01-28 13:19:43 +01002010static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Jens Axboe7b679132008-05-30 12:23:07 +02002012 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002013 BUG_ON(cfq_cfqq_on_rr(cfqq));
2014 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002016 if (cfq_cfqq_sync(cfqq))
2017 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Jens Axboeedd75ff2007-04-19 12:03:34 +02002019 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Jens Axboe498d3aa22007-04-26 12:54:48 +02002022/*
2023 * Called when the cfqq no longer has requests pending, remove it from
2024 * the service tree.
2025 */
Jens Axboefebffd62008-01-28 13:19:43 +01002026static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Jens Axboe7b679132008-05-30 12:23:07 +02002028 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002029 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2030 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002032 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2033 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2034 cfqq->service_tree = NULL;
2035 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002036 if (cfqq->p_root) {
2037 rb_erase(&cfqq->p_node, cfqq->p_root);
2038 cfqq->p_root = NULL;
2039 }
Jens Axboed9e76202007-04-20 14:27:50 +02002040
Justin TerAvest8184f932011-03-17 16:12:36 +01002041 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 BUG_ON(!cfqd->busy_queues);
2043 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002044 if (cfq_cfqq_sync(cfqq))
2045 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
2048/*
2049 * rb tree support functions
2050 */
Jens Axboefebffd62008-01-28 13:19:43 +01002051static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Jens Axboe5e705372006-07-13 12:39:25 +02002053 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002054 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Jens Axboeb4878f22005-10-20 16:42:29 +02002056 BUG_ON(!cfqq->queued[sync]);
2057 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Jens Axboe5e705372006-07-13 12:39:25 +02002059 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Vivek Goyalf04a6422009-12-03 12:59:40 -05002061 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2062 /*
2063 * Queue will be deleted from service tree when we actually
2064 * expire it later. Right now just remove it from prio tree
2065 * as it is empty.
2066 */
2067 if (cfqq->p_root) {
2068 rb_erase(&cfqq->p_node, cfqq->p_root);
2069 cfqq->p_root = NULL;
2070 }
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Jens Axboe5e705372006-07-13 12:39:25 +02002074static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Jens Axboe5e705372006-07-13 12:39:25 +02002076 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002078 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jens Axboe5380a102006-07-13 12:37:56 +02002080 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Jeff Moyer796d5112011-06-02 21:19:05 +02002082 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002083
2084 if (!cfq_cfqq_on_rr(cfqq))
2085 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002086
2087 /*
2088 * check if this request is a better next-serve candidate
2089 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002090 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002091 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002092
2093 /*
2094 * adjust priority tree position, if ->next_rq changes
2095 */
2096 if (prev != cfqq->next_rq)
2097 cfq_prio_tree_add(cfqd, cfqq);
2098
Jens Axboe5044eed2007-04-25 11:53:48 +02002099 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Jens Axboefebffd62008-01-28 13:19:43 +01002102static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Jens Axboe5380a102006-07-13 12:37:56 +02002104 elv_rb_del(&cfqq->sort_list, rq);
2105 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo155fead2012-04-01 14:38:44 -07002106 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002107 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002108 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2109 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Jens Axboe206dc692006-03-28 13:03:44 +02002112static struct request *
2113cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Jens Axboe206dc692006-03-28 13:03:44 +02002115 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002116 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002117 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Jens Axboe4ac845a2008-01-24 08:44:49 +01002119 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002120 if (!cic)
2121 return NULL;
2122
2123 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02002124 if (cfqq) {
2125 sector_t sector = bio->bi_sector + bio_sectors(bio);
2126
Jens Axboe21183b02006-07-13 12:33:14 +02002127 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02002128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return NULL;
2131}
2132
Jens Axboe165125e2007-07-24 09:28:11 +02002133static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002134{
2135 struct cfq_data *cfqd = q->elevator->elevator_data;
2136
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002137 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002138 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002139 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002140
Tejun Heo5b936292009-05-07 22:24:38 +09002141 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002142}
2143
Jens Axboe165125e2007-07-24 09:28:11 +02002144static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Jens Axboe22e2c502005-06-27 10:55:12 +02002146 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002148 WARN_ON(!cfqd->rq_in_driver);
2149 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002150 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002151 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Jens Axboeb4878f22005-10-20 16:42:29 +02002154static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Jens Axboe5e705372006-07-13 12:39:25 +02002156 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002157
Jens Axboe5e705372006-07-13 12:39:25 +02002158 if (cfqq->next_rq == rq)
2159 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Jens Axboeb4878f22005-10-20 16:42:29 +02002161 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002162 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002163
Aaron Carroll45333d52008-08-26 15:52:36 +02002164 cfqq->cfqd->rq_queued--;
Tejun Heo155fead2012-04-01 14:38:44 -07002165 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002166 if (rq->cmd_flags & REQ_PRIO) {
2167 WARN_ON(!cfqq->prio_pending);
2168 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Jens Axboe165125e2007-07-24 09:28:11 +02002172static int cfq_merge(struct request_queue *q, struct request **req,
2173 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 struct cfq_data *cfqd = q->elevator->elevator_data;
2176 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jens Axboe206dc692006-03-28 13:03:44 +02002178 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002179 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002180 *req = __rq;
2181 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
2184 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Jens Axboe165125e2007-07-24 09:28:11 +02002187static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02002188 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Jens Axboe21183b02006-07-13 12:33:14 +02002190 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002191 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jens Axboe5e705372006-07-13 12:39:25 +02002193 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
Divyesh Shah812d4022010-04-08 21:14:23 -07002197static void cfq_bio_merged(struct request_queue *q, struct request *req,
2198 struct bio *bio)
2199{
Tejun Heo155fead2012-04-01 14:38:44 -07002200 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
Divyesh Shah812d4022010-04-08 21:14:23 -07002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203static void
Jens Axboe165125e2007-07-24 09:28:11 +02002204cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 struct request *next)
2206{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002207 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002208 struct cfq_data *cfqd = q->elevator->elevator_data;
2209
Jens Axboe22e2c502005-06-27 10:55:12 +02002210 /*
2211 * reposition in fifo if next is older than rq
2212 */
2213 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002214 time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
2215 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002216 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02002217 rq_set_fifo_time(rq, rq_fifo_time(next));
2218 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002219
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002220 if (cfqq->next_rq == next)
2221 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002222 cfq_remove_request(next);
Tejun Heo155fead2012-04-01 14:38:44 -07002223 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002224
2225 cfqq = RQ_CFQQ(next);
2226 /*
2227 * all requests of this queue are merged to other queues, delete it
2228 * from the service tree. If it's the active_queue,
2229 * cfq_dispatch_requests() will choose to expire it or do idle
2230 */
2231 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2232 cfqq != cfqd->active_queue)
2233 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002234}
2235
Jens Axboe165125e2007-07-24 09:28:11 +02002236static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002237 struct bio *bio)
2238{
2239 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002240 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002241 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002242
2243 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002244 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002245 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002246 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002247 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002248
2249 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002250 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002251 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002252 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002253 cic = cfq_cic_lookup(cfqd, current->io_context);
2254 if (!cic)
2255 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002256
Vasily Tarasov91fac312007-04-25 12:29:51 +02002257 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002258 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002259}
2260
Divyesh Shah812df482010-04-08 21:15:35 -07002261static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2262{
2263 del_timer(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002264 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002265}
2266
Jens Axboefebffd62008-01-28 13:19:43 +01002267static void __cfq_set_active_queue(struct cfq_data *cfqd,
2268 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002269{
2270 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002271 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002272 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002273 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002274 cfqq->slice_start = 0;
2275 cfqq->dispatch_start = jiffies;
2276 cfqq->allocated_slice = 0;
2277 cfqq->slice_end = 0;
2278 cfqq->slice_dispatch = 0;
2279 cfqq->nr_sectors = 0;
2280
2281 cfq_clear_cfqq_wait_request(cfqq);
2282 cfq_clear_cfqq_must_dispatch(cfqq);
2283 cfq_clear_cfqq_must_alloc_slice(cfqq);
2284 cfq_clear_cfqq_fifo_expire(cfqq);
2285 cfq_mark_cfqq_slice_new(cfqq);
2286
2287 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002288 }
2289
2290 cfqd->active_queue = cfqq;
2291}
2292
2293/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002294 * current cfqq expired its slice (or was too idle), select new one
2295 */
2296static void
2297__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002298 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002299{
Jens Axboe7b679132008-05-30 12:23:07 +02002300 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2301
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002302 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002303 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002304
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002305 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002306 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002307
2308 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002309 * If this cfqq is shared between multiple processes, check to
2310 * make sure that those processes are still issuing I/Os within
2311 * the mean seek distance. If not, it may be time to break the
2312 * queues apart again.
2313 */
2314 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2315 cfq_mark_cfqq_split_coop(cfqq);
2316
2317 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002318 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002319 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002320 if (timed_out) {
2321 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002322 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002323 else
2324 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002325 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2326 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002327
Vivek Goyale5ff0822010-04-26 19:25:11 +02002328 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002329
Vivek Goyalf04a6422009-12-03 12:59:40 -05002330 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2331 cfq_del_cfqq_rr(cfqd, cfqq);
2332
Jens Axboeedd75ff2007-04-19 12:03:34 +02002333 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002334
2335 if (cfqq == cfqd->active_queue)
2336 cfqd->active_queue = NULL;
2337
2338 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002339 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002340 cfqd->active_cic = NULL;
2341 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002342}
2343
Vivek Goyale5ff0822010-04-26 19:25:11 +02002344static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002345{
2346 struct cfq_queue *cfqq = cfqd->active_queue;
2347
2348 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002349 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002350}
2351
Jens Axboe498d3aa22007-04-26 12:54:48 +02002352/*
2353 * Get next queue for service. Unless we have a queue preemption,
2354 * we'll simply select the first cfqq in the service tree.
2355 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002356static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002357{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002358 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2359 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002360
Vivek Goyalf04a6422009-12-03 12:59:40 -05002361 if (!cfqd->rq_queued)
2362 return NULL;
2363
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002364 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002365 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002366 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002367 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002368 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002369 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002370}
2371
Vivek Goyalf04a6422009-12-03 12:59:40 -05002372static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2373{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002374 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002375 struct cfq_queue *cfqq;
2376 int i, j;
2377 struct cfq_rb_root *st;
2378
2379 if (!cfqd->rq_queued)
2380 return NULL;
2381
Vivek Goyal25fb5162009-12-03 12:59:46 -05002382 cfqg = cfq_get_next_cfqg(cfqd);
2383 if (!cfqg)
2384 return NULL;
2385
Vivek Goyalf04a6422009-12-03 12:59:40 -05002386 for_each_cfqg_st(cfqg, i, j, st)
2387 if ((cfqq = cfq_rb_first(st)) != NULL)
2388 return cfqq;
2389 return NULL;
2390}
2391
Jens Axboe498d3aa22007-04-26 12:54:48 +02002392/*
2393 * Get and set a new active queue for service.
2394 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002395static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2396 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002397{
Jens Axboee00ef792009-11-04 08:54:55 +01002398 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002399 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002400
Jens Axboe22e2c502005-06-27 10:55:12 +02002401 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002402 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002403}
2404
Jens Axboed9e76202007-04-20 14:27:50 +02002405static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2406 struct request *rq)
2407{
Tejun Heo83096eb2009-05-07 22:24:39 +09002408 if (blk_rq_pos(rq) >= cfqd->last_position)
2409 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002410 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002411 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002412}
2413
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002414static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002415 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002416{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002417 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002418}
2419
Jens Axboea36e71f2009-04-15 12:15:11 +02002420static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2421 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002422{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002423 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002424 struct rb_node *parent, *node;
2425 struct cfq_queue *__cfqq;
2426 sector_t sector = cfqd->last_position;
2427
2428 if (RB_EMPTY_ROOT(root))
2429 return NULL;
2430
2431 /*
2432 * First, if we find a request starting at the end of the last
2433 * request, choose it.
2434 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002435 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002436 if (__cfqq)
2437 return __cfqq;
2438
2439 /*
2440 * If the exact sector wasn't found, the parent of the NULL leaf
2441 * will contain the closest sector.
2442 */
2443 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002444 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002445 return __cfqq;
2446
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002447 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002448 node = rb_next(&__cfqq->p_node);
2449 else
2450 node = rb_prev(&__cfqq->p_node);
2451 if (!node)
2452 return NULL;
2453
2454 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002455 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002456 return __cfqq;
2457
2458 return NULL;
2459}
2460
2461/*
2462 * cfqd - obvious
2463 * cur_cfqq - passed in so that we don't decide that the current queue is
2464 * closely cooperating with itself.
2465 *
2466 * So, basically we're assuming that that cur_cfqq has dispatched at least
2467 * one request, and that cfqd->last_position reflects a position on the disk
2468 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2469 * assumption.
2470 */
2471static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002472 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002473{
2474 struct cfq_queue *cfqq;
2475
Divyesh Shah39c01b22010-03-25 15:45:57 +01002476 if (cfq_class_idle(cur_cfqq))
2477 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002478 if (!cfq_cfqq_sync(cur_cfqq))
2479 return NULL;
2480 if (CFQQ_SEEKY(cur_cfqq))
2481 return NULL;
2482
Jens Axboea36e71f2009-04-15 12:15:11 +02002483 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002484 * Don't search priority tree if it's the only queue in the group.
2485 */
2486 if (cur_cfqq->cfqg->nr_cfqq == 1)
2487 return NULL;
2488
2489 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002490 * We should notice if some of the queues are cooperating, eg
2491 * working closely on the same area of the disk. In that case,
2492 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002493 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002494 cfqq = cfqq_close(cfqd, cur_cfqq);
2495 if (!cfqq)
2496 return NULL;
2497
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002498 /* If new queue belongs to different cfq_group, don't choose it */
2499 if (cur_cfqq->cfqg != cfqq->cfqg)
2500 return NULL;
2501
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002502 /*
2503 * It only makes sense to merge sync queues.
2504 */
2505 if (!cfq_cfqq_sync(cfqq))
2506 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002507 if (CFQQ_SEEKY(cfqq))
2508 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002509
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002510 /*
2511 * Do not merge queues of different priority classes
2512 */
2513 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2514 return NULL;
2515
Jens Axboea36e71f2009-04-15 12:15:11 +02002516 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002517}
2518
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002519/*
2520 * Determine whether we should enforce idle window for this queue.
2521 */
2522
2523static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2524{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002525 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002526 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002527
Vivek Goyal34b98d02012-10-03 16:56:58 -04002528 BUG_ON(!st);
2529 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002530
Vivek Goyalb6508c12010-08-23 12:23:33 +02002531 if (!cfqd->cfq_slice_idle)
2532 return false;
2533
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002534 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002535 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002536 return false;
2537
2538 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002539 if (cfq_cfqq_idle_window(cfqq) &&
2540 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002541 return true;
2542
2543 /*
2544 * Otherwise, we do only if they are the last ones
2545 * in their service tree.
2546 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002547 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2548 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002549 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002550 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002551 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002552}
2553
Jens Axboe6d048f52007-04-25 12:44:27 +02002554static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002555{
Jens Axboe17926692007-01-19 11:59:30 +11002556 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002557 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002558 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002559
Jens Axboea68bbdd2008-09-24 13:03:33 +02002560 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002561 * SSD device without seek penalty, disable idling. But only do so
2562 * for devices that support queuing, otherwise we still have a problem
2563 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002564 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002565 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002566 return;
2567
Jens Axboedd67d052006-06-21 09:36:18 +02002568 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002569 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002570
2571 /*
2572 * idle is disabled, either manually or by past process history
2573 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002574 if (!cfq_should_idle(cfqd, cfqq)) {
2575 /* no queue idling. Check for group idling */
2576 if (cfqd->cfq_group_idle)
2577 group_idle = cfqd->cfq_group_idle;
2578 else
2579 return;
2580 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002581
Jens Axboe22e2c502005-06-27 10:55:12 +02002582 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002583 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002584 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002585 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002586 return;
2587
2588 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002589 * task has exited, don't wait
2590 */
Jens Axboe206dc692006-03-28 13:03:44 +02002591 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002592 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002593 return;
2594
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002595 /*
2596 * If our average think time is larger than the remaining time
2597 * slice, then don't idle. This avoids overrunning the allotted
2598 * time slice.
2599 */
Shaohua Li383cd722011-07-12 14:24:35 +02002600 if (sample_valid(cic->ttime.ttime_samples) &&
2601 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002602 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002603 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002604 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002605 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002606
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002607 /* There are other queues in the group, don't do group idle */
2608 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2609 return;
2610
Jens Axboe3b181522005-06-27 10:56:24 +02002611 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002612
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002613 if (group_idle)
2614 sl = cfqd->cfq_group_idle;
2615 else
2616 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002617
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002618 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo155fead2012-04-01 14:38:44 -07002619 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002620 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2621 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Jens Axboe498d3aa22007-04-26 12:54:48 +02002624/*
2625 * Move request from internal lists to the request queue dispatch list.
2626 */
Jens Axboe165125e2007-07-24 09:28:11 +02002627static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002629 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002630 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002631
Jens Axboe7b679132008-05-30 12:23:07 +02002632 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2633
Jeff Moyer06d21882009-09-11 17:08:59 +02002634 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002635 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002636 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002637 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002638 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002639
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002640 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002641 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002642 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
2645/*
2646 * return expired entry, or NULL to just start from scratch in rbtree
2647 */
Jens Axboefebffd62008-01-28 13:19:43 +01002648static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Jens Axboe30996f42009-10-05 11:03:39 +02002650 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Jens Axboe3b181522005-06-27 10:56:24 +02002652 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002654
2655 cfq_mark_cfqq_fifo_expire(cfqq);
2656
Jens Axboe89850f72006-07-22 16:48:31 +02002657 if (list_empty(&cfqq->fifo))
2658 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Jens Axboe89850f72006-07-22 16:48:31 +02002660 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002661 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002662 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Jens Axboe30996f42009-10-05 11:03:39 +02002664 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002665 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Jens Axboe22e2c502005-06-27 10:55:12 +02002668static inline int
2669cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2670{
2671 const int base_rq = cfqd->cfq_slice_async_rq;
2672
2673 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2674
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002675 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002676}
2677
2678/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002679 * Must be called with the queue_lock held.
2680 */
2681static int cfqq_process_refs(struct cfq_queue *cfqq)
2682{
2683 int process_refs, io_refs;
2684
2685 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002686 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002687 BUG_ON(process_refs < 0);
2688 return process_refs;
2689}
2690
2691static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2692{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002693 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002694 struct cfq_queue *__cfqq;
2695
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002696 /*
2697 * If there are no process references on the new_cfqq, then it is
2698 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2699 * chain may have dropped their last reference (not just their
2700 * last process reference).
2701 */
2702 if (!cfqq_process_refs(new_cfqq))
2703 return;
2704
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002705 /* Avoid a circular list and skip interim queue merges */
2706 while ((__cfqq = new_cfqq->new_cfqq)) {
2707 if (__cfqq == cfqq)
2708 return;
2709 new_cfqq = __cfqq;
2710 }
2711
2712 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002713 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002714 /*
2715 * If the process for the cfqq has gone away, there is no
2716 * sense in merging the queues.
2717 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002718 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002719 return;
2720
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002721 /*
2722 * Merge in the direction of the lesser amount of work.
2723 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002724 if (new_process_refs >= process_refs) {
2725 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002726 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002727 } else {
2728 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002729 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002730 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002731}
2732
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002733static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002734 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002735{
2736 struct cfq_queue *queue;
2737 int i;
2738 bool key_valid = false;
2739 unsigned long lowest_key = 0;
2740 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2741
Vivek Goyal65b32a52009-12-16 17:52:59 -05002742 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2743 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002744 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002745 if (queue &&
2746 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2747 lowest_key = queue->rb_key;
2748 cur_best = i;
2749 key_valid = true;
2750 }
2751 }
2752
2753 return cur_best;
2754}
2755
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002756static void
2757choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002758{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002759 unsigned slice;
2760 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002761 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002762 unsigned group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002763 enum wl_class_t original_class = cfqd->serving_wl_class;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002764
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002765 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002766 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002767 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002768 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002769 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002770 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002771 cfqd->serving_wl_class = IDLE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002772 cfqd->workload_expires = jiffies + 1;
2773 return;
2774 }
2775
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002776 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002777 goto new_workload;
2778
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002779 /*
2780 * For RT and BE, we have to choose also the type
2781 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2782 * expiration time
2783 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002784 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002785 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002786
2787 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002788 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002789 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002790 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002791 return;
2792
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002793new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002794 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002795 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002796 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002797 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002798 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002799
2800 /*
2801 * the workload slice is computed as a fraction of target latency
2802 * proportional to the number of queues in that workload, over
2803 * all the queues in the same priority class
2804 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002805 group_slice = cfq_group_slice(cfqd, cfqg);
2806
2807 slice = group_slice * count /
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002808 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
2809 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002810 cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002811
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002812 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002813 unsigned int tmp;
2814
2815 /*
2816 * Async queues are currently system wide. Just taking
2817 * proportion of queues with-in same group will lead to higher
2818 * async ratio system wide as generally root group is going
2819 * to have higher weight. A more accurate thing would be to
2820 * calculate system wide asnc/sync ratio.
2821 */
Tao Ma5bf14c02012-04-01 14:33:39 -07002822 tmp = cfqd->cfq_target_latency *
2823 cfqg_busy_async_queues(cfqd, cfqg);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002824 tmp = tmp/cfqd->busy_queues;
2825 slice = min_t(unsigned, slice, tmp);
2826
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002827 /* async workload slice is scaled down according to
2828 * the sync/async slice ratio. */
2829 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002830 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002831 /* sync workload slice is at least 2 * cfq_slice_idle */
2832 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2833
2834 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002835 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002836 cfqd->workload_expires = jiffies + slice;
2837}
2838
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002839static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2840{
2841 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002842 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002843
2844 if (RB_EMPTY_ROOT(&st->rb))
2845 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002846 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002847 update_min_vdisktime(st);
2848 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002849}
2850
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002851static void cfq_choose_cfqg(struct cfq_data *cfqd)
2852{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002853 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2854
2855 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002856
2857 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002858 if (cfqg->saved_wl_slice) {
2859 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
2860 cfqd->serving_wl_type = cfqg->saved_wl_type;
2861 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002862 } else
2863 cfqd->workload_expires = jiffies - 1;
2864
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002865 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002866}
2867
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002868/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002869 * Select a queue for service. If we have a current active queue,
2870 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002871 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002872static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002873{
Jens Axboea36e71f2009-04-15 12:15:11 +02002874 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002875
2876 cfqq = cfqd->active_queue;
2877 if (!cfqq)
2878 goto new_queue;
2879
Vivek Goyalf04a6422009-12-03 12:59:40 -05002880 if (!cfqd->rq_queued)
2881 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002882
2883 /*
2884 * We were waiting for group to get backlogged. Expire the queue
2885 */
2886 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2887 goto expire;
2888
Jens Axboe22e2c502005-06-27 10:55:12 +02002889 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002890 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002891 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002892 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2893 /*
2894 * If slice had not expired at the completion of last request
2895 * we might not have turned on wait_busy flag. Don't expire
2896 * the queue yet. Allow the group to get backlogged.
2897 *
2898 * The very fact that we have used the slice, that means we
2899 * have been idling all along on this queue and it should be
2900 * ok to wait for this request to complete.
2901 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002902 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2903 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2904 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002905 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002906 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002907 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002908 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002909
2910 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002911 * The active queue has requests and isn't expired, allow it to
2912 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002913 */
Jens Axboedd67d052006-06-21 09:36:18 +02002914 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002915 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002916
2917 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002918 * If another queue has a request waiting within our mean seek
2919 * distance, let it run. The expire code will check for close
2920 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002921 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002922 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002923 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002924 if (new_cfqq) {
2925 if (!cfqq->new_cfqq)
2926 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002927 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002928 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002929
2930 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002931 * No requests pending. If the active queue still has requests in
2932 * flight or is idling for a new request, allow either of these
2933 * conditions to happen (or time out) before selecting a new queue.
2934 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002935 if (timer_pending(&cfqd->idle_slice_timer)) {
2936 cfqq = NULL;
2937 goto keep_queue;
2938 }
2939
Shaohua Li8e1ac662010-11-08 15:01:04 +01002940 /*
2941 * This is a deep seek queue, but the device is much faster than
2942 * the queue can deliver, don't idle
2943 **/
2944 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2945 (cfq_cfqq_slice_new(cfqq) ||
2946 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2947 cfq_clear_cfqq_deep(cfqq);
2948 cfq_clear_cfqq_idle_window(cfqq);
2949 }
2950
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002951 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2952 cfqq = NULL;
2953 goto keep_queue;
2954 }
2955
2956 /*
2957 * If group idle is enabled and there are requests dispatched from
2958 * this group, wait for requests to complete.
2959 */
2960check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002961 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2962 cfqq->cfqg->dispatched &&
2963 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002964 cfqq = NULL;
2965 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002966 }
2967
Jens Axboe3b181522005-06-27 10:56:24 +02002968expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002969 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002970new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002971 /*
2972 * Current queue expired. Check if we have to switch to a new
2973 * service tree
2974 */
2975 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002976 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002977
Jens Axboea36e71f2009-04-15 12:15:11 +02002978 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002979keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002980 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002981}
2982
Jens Axboefebffd62008-01-28 13:19:43 +01002983static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002984{
2985 int dispatched = 0;
2986
2987 while (cfqq->next_rq) {
2988 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2989 dispatched++;
2990 }
2991
2992 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002993
2994 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002995 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002996 return dispatched;
2997}
2998
Jens Axboe498d3aa22007-04-26 12:54:48 +02002999/*
3000 * Drain our current requests. Used for barriers and when switching
3001 * io schedulers on-the-fly.
3002 */
Jens Axboed9e76202007-04-20 14:27:50 +02003003static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003004{
Jens Axboe08717142008-01-28 11:38:15 +01003005 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003006 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003007
Divyesh Shah3440c492010-04-09 09:29:57 +02003008 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003009 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003010 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3011 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003012 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003013 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003014
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003015 BUG_ON(cfqd->busy_queues);
3016
Jeff Moyer69237152009-06-12 15:29:30 +02003017 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003018 return dispatched;
3019}
3020
Shaohua Liabc3c742010-03-01 09:20:54 +01003021static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3022 struct cfq_queue *cfqq)
3023{
3024 /* the queue hasn't finished any request, can't estimate */
3025 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003026 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003027 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
3028 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01003029 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003030
Shaohua Lic1e44752010-11-08 15:01:02 +01003031 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003032}
3033
Jens Axboe0b182d62009-10-06 20:49:37 +02003034static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003035{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003036 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Jens Axboe2f5cb732009-04-07 08:51:19 +02003038 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003039 * Drain async requests before we start sync IO
3040 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003041 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003042 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003043
3044 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003045 * If this is an async queue and we have sync IO in flight, let it wait
3046 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003047 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003048 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003049
Shaohua Liabc3c742010-03-01 09:20:54 +01003050 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003051 if (cfq_class_idle(cfqq))
3052 max_dispatch = 1;
3053
3054 /*
3055 * Does this cfqq already have too much IO in flight?
3056 */
3057 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003058 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003059 /*
3060 * idle queue must always only have a single IO in flight
3061 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003062 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003063 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003064
Jens Axboe2f5cb732009-04-07 08:51:19 +02003065 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003066 * If there is only one sync queue
3067 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003068 * queue no dispatch limit. The reason is a sync queue can
3069 * preempt async queue, limiting the sync queue doesn't make
3070 * sense. This is useful for aiostress test.
3071 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003072 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3073 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003074
3075 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003076 * We have other queues, don't allow more IO from this one
3077 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003078 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3079 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003080 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003081
Jens Axboe2f5cb732009-04-07 08:51:19 +02003082 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003083 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003084 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003085 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003086 max_dispatch = -1;
3087 else
3088 /*
3089 * Normally we start throttling cfqq when cfq_quantum/2
3090 * requests have been dispatched. But we can drive
3091 * deeper queue depths at the beginning of slice
3092 * subjected to upper limit of cfq_quantum.
3093 * */
3094 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003095 }
3096
3097 /*
3098 * Async queues must wait a bit before being allowed dispatch.
3099 * We also ramp up the dispatch depth gradually for async IO,
3100 * based on the last sync IO we serviced
3101 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003102 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003103 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003104 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003105
Jens Axboe61f0c1d2009-10-03 19:46:03 +02003106 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02003107 if (!depth && !cfqq->dispatched)
3108 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003109 if (depth < max_dispatch)
3110 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112
Jens Axboe0b182d62009-10-06 20:49:37 +02003113 /*
3114 * If we're below the current max, allow a dispatch
3115 */
3116 return cfqq->dispatched < max_dispatch;
3117}
3118
3119/*
3120 * Dispatch a request from cfqq, moving them to the request queue
3121 * dispatch list.
3122 */
3123static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3124{
3125 struct request *rq;
3126
3127 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3128
3129 if (!cfq_may_dispatch(cfqd, cfqq))
3130 return false;
3131
3132 /*
3133 * follow expired path, else get first next available
3134 */
3135 rq = cfq_check_fifo(cfqq);
3136 if (!rq)
3137 rq = cfqq->next_rq;
3138
3139 /*
3140 * insert request into driver dispatch list
3141 */
3142 cfq_dispatch_insert(cfqd->queue, rq);
3143
3144 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003145 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003146
Tejun Heoc5869802011-12-14 00:33:41 +01003147 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003148 cfqd->active_cic = cic;
3149 }
3150
3151 return true;
3152}
3153
3154/*
3155 * Find the cfqq that we need to service and move a request from that to the
3156 * dispatch list
3157 */
3158static int cfq_dispatch_requests(struct request_queue *q, int force)
3159{
3160 struct cfq_data *cfqd = q->elevator->elevator_data;
3161 struct cfq_queue *cfqq;
3162
3163 if (!cfqd->busy_queues)
3164 return 0;
3165
3166 if (unlikely(force))
3167 return cfq_forced_dispatch(cfqd);
3168
3169 cfqq = cfq_select_queue(cfqd);
3170 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003171 return 0;
3172
Jens Axboe2f5cb732009-04-07 08:51:19 +02003173 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003174 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003175 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003176 if (!cfq_dispatch_request(cfqd, cfqq))
3177 return 0;
3178
Jens Axboe2f5cb732009-04-07 08:51:19 +02003179 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003180 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003181
3182 /*
3183 * expire an async queue immediately if it has used up its slice. idle
3184 * queue always expire after 1 dispatch round.
3185 */
3186 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3187 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3188 cfq_class_idle(cfqq))) {
3189 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003190 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003191 }
3192
Shan Weib217a902009-09-01 10:06:42 +02003193 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003194 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197/*
Jens Axboe5e705372006-07-13 12:39:25 +02003198 * task holds one reference to the queue, dropped when task exits. each rq
3199 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003201 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 * queue lock must be held here.
3203 */
3204static void cfq_put_queue(struct cfq_queue *cfqq)
3205{
Jens Axboe22e2c502005-06-27 10:55:12 +02003206 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003207 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003208
Shaohua Li30d7b942011-01-07 08:46:59 +01003209 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Shaohua Li30d7b942011-01-07 08:46:59 +01003211 cfqq->ref--;
3212 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return;
3214
Jens Axboe7b679132008-05-30 12:23:07 +02003215 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003217 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003218 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003220 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003221 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003222 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003223 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003224
Vivek Goyalf04a6422009-12-03 12:59:40 -05003225 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003227 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Shaohua Lid02a2c02010-05-25 10:16:53 +02003230static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003231{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003232 struct cfq_queue *__cfqq, *next;
3233
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003234 /*
3235 * If this queue was scheduled to merge with another queue, be
3236 * sure to drop the reference taken on that queue (and others in
3237 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3238 */
3239 __cfqq = cfqq->new_cfqq;
3240 while (__cfqq) {
3241 if (__cfqq == cfqq) {
3242 WARN(1, "cfqq->new_cfqq loop detected\n");
3243 break;
3244 }
3245 next = __cfqq->new_cfqq;
3246 cfq_put_queue(__cfqq);
3247 __cfqq = next;
3248 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003249}
3250
3251static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3252{
3253 if (unlikely(cfqq == cfqd->active_queue)) {
3254 __cfq_slice_expired(cfqd, cfqq, 0);
3255 cfq_schedule_dispatch(cfqd);
3256 }
3257
3258 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003259
Jens Axboe89850f72006-07-22 16:48:31 +02003260 cfq_put_queue(cfqq);
3261}
3262
Tejun Heo9b84cac2011-12-14 00:33:42 +01003263static void cfq_init_icq(struct io_cq *icq)
3264{
3265 struct cfq_io_cq *cic = icq_to_cic(icq);
3266
3267 cic->ttime.last_end_request = jiffies;
3268}
3269
Tejun Heoc5869802011-12-14 00:33:41 +01003270static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003271{
Tejun Heoc5869802011-12-14 00:33:41 +01003272 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003273 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003274
Jens Axboeff6657c2009-04-08 10:58:57 +02003275 if (cic->cfqq[BLK_RW_ASYNC]) {
3276 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3277 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003278 }
3279
Jens Axboeff6657c2009-04-08 10:58:57 +02003280 if (cic->cfqq[BLK_RW_SYNC]) {
3281 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3282 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003283 }
Jens Axboe89850f72006-07-22 16:48:31 +02003284}
3285
Tejun Heoabede6d2012-03-19 15:10:57 -07003286static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003287{
3288 struct task_struct *tsk = current;
3289 int ioprio_class;
3290
Jens Axboe3b181522005-06-27 10:56:24 +02003291 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003292 return;
3293
Tejun Heo598971b2012-03-19 15:10:58 -07003294 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003295 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003296 default:
3297 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3298 case IOPRIO_CLASS_NONE:
3299 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003300 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003301 */
3302 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003303 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003304 break;
3305 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003306 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003307 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3308 break;
3309 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003310 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003311 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3312 break;
3313 case IOPRIO_CLASS_IDLE:
3314 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3315 cfqq->ioprio = 7;
3316 cfq_clear_cfqq_idle_window(cfqq);
3317 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003318 }
3319
3320 /*
3321 * keep track of original prio settings in case we have to temporarily
3322 * elevate the priority of this queue
3323 */
3324 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003325 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003326}
3327
Tejun Heo598971b2012-03-19 15:10:58 -07003328static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003329{
Tejun Heo598971b2012-03-19 15:10:58 -07003330 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003331 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003332 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003333
Tejun Heo598971b2012-03-19 15:10:58 -07003334 /*
3335 * Check whether ioprio has changed. The condition may trigger
3336 * spuriously on a newly created cic but there's no harm.
3337 */
3338 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003339 return;
3340
Jens Axboeff6657c2009-04-08 10:58:57 +02003341 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003342 if (cfqq) {
3343 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003344 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3345 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003346 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003347 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003348 cfq_put_queue(cfqq);
3349 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003350 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003351
Jens Axboeff6657c2009-04-08 10:58:57 +02003352 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003353 if (cfqq)
3354 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003355
3356 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003357}
3358
Jens Axboed5036d72009-06-26 10:44:34 +02003359static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003360 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003361{
3362 RB_CLEAR_NODE(&cfqq->rb_node);
3363 RB_CLEAR_NODE(&cfqq->p_node);
3364 INIT_LIST_HEAD(&cfqq->fifo);
3365
Shaohua Li30d7b942011-01-07 08:46:59 +01003366 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003367 cfqq->cfqd = cfqd;
3368
3369 cfq_mark_cfqq_prio_changed(cfqq);
3370
3371 if (is_sync) {
3372 if (!cfq_class_idle(cfqq))
3373 cfq_mark_cfqq_idle_window(cfqq);
3374 cfq_mark_cfqq_sync(cfqq);
3375 }
3376 cfqq->pid = pid;
3377}
3378
Vivek Goyal246103332009-12-03 12:59:51 -05003379#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003380static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003381{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003382 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003383 struct cfq_queue *sync_cfqq;
3384 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003385
Tejun Heo598971b2012-03-19 15:10:58 -07003386 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07003387 id = bio_blkcg(bio)->id;
Tejun Heo598971b2012-03-19 15:10:58 -07003388 rcu_read_unlock();
3389
3390 /*
3391 * Check whether blkcg has changed. The condition may trigger
3392 * spuriously on a newly created cic but there's no harm.
3393 */
3394 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003395 return;
3396
Tejun Heo598971b2012-03-19 15:10:58 -07003397 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003398 if (sync_cfqq) {
3399 /*
3400 * Drop reference to sync queue. A new sync queue will be
3401 * assigned in new group upon arrival of a fresh request.
3402 */
3403 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3404 cic_set_cfqq(cic, NULL, 1);
3405 cfq_put_queue(sync_cfqq);
3406 }
Tejun Heo598971b2012-03-19 15:10:58 -07003407
3408 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003409}
Tejun Heo598971b2012-03-19 15:10:58 -07003410#else
3411static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003412#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003415cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3416 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
Tejun Heo3c798392012-04-16 13:57:25 -07003418 struct blkcg *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003420 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
3422retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003423 rcu_read_lock();
3424
Tejun Heo3c798392012-04-16 13:57:25 -07003425 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003426 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003427 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Jens Axboe6118b702009-06-30 09:34:12 +02003429 /*
3430 * Always try a new alloc if we fell back to the OOM cfqq
3431 * originally, since it should just be a temporary situation.
3432 */
3433 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3434 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (new_cfqq) {
3436 cfqq = new_cfqq;
3437 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003438 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003439 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003441 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003442 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003443 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003445 if (new_cfqq)
3446 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003447 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003448 cfqq = kmem_cache_alloc_node(cfq_pool,
3449 gfp_mask | __GFP_ZERO,
3450 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Jens Axboe6118b702009-06-30 09:34:12 +02003453 if (cfqq) {
3454 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003455 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003456 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003457 cfq_log_cfqq(cfqd, cfqq, "alloced");
3458 } else
3459 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
3461
3462 if (new_cfqq)
3463 kmem_cache_free(cfq_pool, new_cfqq);
3464
Tejun Heo2a7f1242012-03-05 13:15:01 -08003465 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return cfqq;
3467}
3468
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003469static struct cfq_queue **
3470cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3471{
Jens Axboefe094d92008-01-31 13:08:54 +01003472 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003473 case IOPRIO_CLASS_RT:
3474 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003475 case IOPRIO_CLASS_NONE:
3476 ioprio = IOPRIO_NORM;
3477 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003478 case IOPRIO_CLASS_BE:
3479 return &cfqd->async_cfqq[1][ioprio];
3480 case IOPRIO_CLASS_IDLE:
3481 return &cfqd->async_idle_cfqq;
3482 default:
3483 BUG();
3484 }
3485}
3486
Jens Axboe15c31be2007-07-10 13:43:25 +02003487static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003488cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003489 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003490{
Tejun Heo598971b2012-03-19 15:10:58 -07003491 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3492 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003493 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003494 struct cfq_queue *cfqq = NULL;
3495
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003496 if (!is_sync) {
3497 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3498 cfqq = *async_cfqq;
3499 }
3500
Jens Axboe6118b702009-06-30 09:34:12 +02003501 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003502 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003503
3504 /*
3505 * pin the queue now that it's allocated, scheduler exit will prune it
3506 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003507 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003508 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003509 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003510 }
3511
Shaohua Li30d7b942011-01-07 08:46:59 +01003512 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003513 return cfqq;
3514}
3515
Jens Axboe22e2c502005-06-27 10:55:12 +02003516static void
Shaohua Li383cd722011-07-12 14:24:35 +02003517__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003518{
Shaohua Li383cd722011-07-12 14:24:35 +02003519 unsigned long elapsed = jiffies - ttime->last_end_request;
3520 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003521
Shaohua Li383cd722011-07-12 14:24:35 +02003522 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3523 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3524 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3525}
3526
3527static void
3528cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003529 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003530{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003531 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003532 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003533 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3534 cfqd->cfq_slice_idle);
3535 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003536#ifdef CONFIG_CFQ_GROUP_IOSCHED
3537 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3538#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003539}
3540
Jens Axboe206dc692006-03-28 13:03:44 +02003541static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003542cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003543 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003544{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003545 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003546 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003547 if (cfqq->last_request_pos) {
3548 if (cfqq->last_request_pos < blk_rq_pos(rq))
3549 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3550 else
3551 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3552 }
Jens Axboe206dc692006-03-28 13:03:44 +02003553
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003554 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003555 if (blk_queue_nonrot(cfqd->queue))
3556 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3557 else
3558 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003559}
Jens Axboe22e2c502005-06-27 10:55:12 +02003560
3561/*
3562 * Disable idle window if the process thinks too long or seeks so much that
3563 * it doesn't matter
3564 */
3565static void
3566cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003567 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003568{
Jens Axboe7b679132008-05-30 12:23:07 +02003569 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003570
Jens Axboe08717142008-01-28 11:38:15 +01003571 /*
3572 * Don't idle for async or idle io prio class
3573 */
3574 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003575 return;
3576
Jens Axboec265a7f2008-06-26 13:49:33 +02003577 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003578
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003579 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3580 cfq_mark_cfqq_deep(cfqq);
3581
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003582 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3583 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003584 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003585 !cfqd->cfq_slice_idle ||
3586 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003587 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003588 else if (sample_valid(cic->ttime.ttime_samples)) {
3589 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003590 enable_idle = 0;
3591 else
3592 enable_idle = 1;
3593 }
3594
Jens Axboe7b679132008-05-30 12:23:07 +02003595 if (old_idle != enable_idle) {
3596 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3597 if (enable_idle)
3598 cfq_mark_cfqq_idle_window(cfqq);
3599 else
3600 cfq_clear_cfqq_idle_window(cfqq);
3601 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003602}
3603
Jens Axboe22e2c502005-06-27 10:55:12 +02003604/*
3605 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3606 * no or if we aren't sure, a 1 will cause a preempt.
3607 */
Jens Axboea6151c32009-10-07 20:02:57 +02003608static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003609cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003610 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003611{
Jens Axboe6d048f52007-04-25 12:44:27 +02003612 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003613
Jens Axboe6d048f52007-04-25 12:44:27 +02003614 cfqq = cfqd->active_queue;
3615 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003616 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003617
Jens Axboe6d048f52007-04-25 12:44:27 +02003618 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003619 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003620
3621 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003622 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003623
Jens Axboe22e2c502005-06-27 10:55:12 +02003624 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003625 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3626 */
3627 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3628 return false;
3629
3630 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003631 * if the new request is sync, but the currently running queue is
3632 * not, let the sync request have priority.
3633 */
Jens Axboe5e705372006-07-13 12:39:25 +02003634 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003635 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003636
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003637 if (new_cfqq->cfqg != cfqq->cfqg)
3638 return false;
3639
3640 if (cfq_slice_used(cfqq))
3641 return true;
3642
3643 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003644 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003645 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3646 new_cfqq->service_tree->count == 2 &&
3647 RB_EMPTY_ROOT(&cfqq->sort_list))
3648 return true;
3649
Jens Axboe374f84a2006-07-23 01:42:19 +02003650 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003651 * So both queues are sync. Let the new request get disk time if
3652 * it's a metadata request and the current queue is doing regular IO.
3653 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003654 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003655 return true;
3656
3657 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003658 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3659 */
3660 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003661 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003662
Shaohua Lid2d59e12010-11-08 15:01:03 +01003663 /* An idle queue should not be idle now for some reason */
3664 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3665 return true;
3666
Jens Axboe1e3335d2007-02-14 19:59:49 +01003667 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003668 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003669
3670 /*
3671 * if this request is as-good as one we would expect from the
3672 * current cfqq, let it preempt
3673 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003674 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003675 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003676
Jens Axboea6151c32009-10-07 20:02:57 +02003677 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003678}
3679
3680/*
3681 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3682 * let it have half of its nominal slice.
3683 */
3684static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3685{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003686 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3687
Jens Axboe7b679132008-05-30 12:23:07 +02003688 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003689 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003690
Jens Axboebf572252006-07-19 20:29:12 +02003691 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003692 * workload type is changed, don't save slice, otherwise preempt
3693 * doesn't happen
3694 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003695 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003696 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003697
3698 /*
Jens Axboebf572252006-07-19 20:29:12 +02003699 * Put the new queue at the front of the of the current list,
3700 * so we know that it will be selected next.
3701 */
3702 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003703
3704 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003705
Justin TerAvest62a37f62011-03-23 08:25:44 +01003706 cfqq->slice_end = 0;
3707 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003708}
3709
3710/*
Jens Axboe5e705372006-07-13 12:39:25 +02003711 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003712 * something we should do about it
3713 */
3714static void
Jens Axboe5e705372006-07-13 12:39:25 +02003715cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3716 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003717{
Tejun Heoc5869802011-12-14 00:33:41 +01003718 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003719
Aaron Carroll45333d52008-08-26 15:52:36 +02003720 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003721 if (rq->cmd_flags & REQ_PRIO)
3722 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003723
Shaohua Li383cd722011-07-12 14:24:35 +02003724 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003725 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003726 cfq_update_idle_window(cfqd, cfqq, cic);
3727
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003728 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003729
3730 if (cfqq == cfqd->active_queue) {
3731 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003732 * Remember that we saw a request from this process, but
3733 * don't start queuing just yet. Otherwise we risk seeing lots
3734 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003735 * and merging. If the request is already larger than a single
3736 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003737 * merging is already done. Ditto for a busy system that
3738 * has other work pending, don't risk delaying until the
3739 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003740 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003741 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003742 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3743 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003744 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003745 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003746 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003747 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07003748 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003749 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003750 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003751 }
Jens Axboe5e705372006-07-13 12:39:25 +02003752 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003753 /*
3754 * not the active queue - expire current slice if it is
3755 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003756 * has some old slice time left and is of higher priority or
3757 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003758 */
3759 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003760 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003761 }
3762}
3763
Jens Axboe165125e2007-07-24 09:28:11 +02003764static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003765{
Jens Axboeb4878f22005-10-20 16:42:29 +02003766 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003767 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003768
Jens Axboe7b679132008-05-30 12:23:07 +02003769 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003770 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Jens Axboe30996f42009-10-05 11:03:39 +02003772 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003773 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003774 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07003775 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
3776 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02003777 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
Aaron Carroll45333d52008-08-26 15:52:36 +02003780/*
3781 * Update hw_tag based on peak queue depth over 50 samples under
3782 * sufficient load.
3783 */
3784static void cfq_update_hw_tag(struct cfq_data *cfqd)
3785{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003786 struct cfq_queue *cfqq = cfqd->active_queue;
3787
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003788 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3789 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003790
3791 if (cfqd->hw_tag == 1)
3792 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003793
3794 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003795 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003796 return;
3797
Shaohua Li1a1238a2009-10-27 08:46:23 +01003798 /*
3799 * If active queue hasn't enough requests and can idle, cfq might not
3800 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3801 * case
3802 */
3803 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3804 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003805 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003806 return;
3807
Aaron Carroll45333d52008-08-26 15:52:36 +02003808 if (cfqd->hw_tag_samples++ < 50)
3809 return;
3810
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003811 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003812 cfqd->hw_tag = 1;
3813 else
3814 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003815}
3816
Vivek Goyal7667aa02009-12-08 17:52:58 -05003817static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3818{
Tejun Heoc5869802011-12-14 00:33:41 +01003819 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003820
Justin TerAvest02a8f012011-02-09 14:20:03 +01003821 /* If the queue already has requests, don't wait */
3822 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3823 return false;
3824
Vivek Goyal7667aa02009-12-08 17:52:58 -05003825 /* If there are other queues in the group, don't wait */
3826 if (cfqq->cfqg->nr_cfqq > 1)
3827 return false;
3828
Shaohua Li7700fc42011-07-12 14:24:56 +02003829 /* the only queue in the group, but think time is big */
3830 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3831 return false;
3832
Vivek Goyal7667aa02009-12-08 17:52:58 -05003833 if (cfq_slice_used(cfqq))
3834 return true;
3835
3836 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003837 if (cic && sample_valid(cic->ttime.ttime_samples)
3838 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003839 return true;
3840
3841 /*
3842 * If think times is less than a jiffy than ttime_mean=0 and above
3843 * will not be true. It might happen that slice has not expired yet
3844 * but will expire soon (4-5 ns) during select_queue(). To cover the
3845 * case where think time is less than a jiffy, mark the queue wait
3846 * busy if only 1 jiffy is left in the slice.
3847 */
3848 if (cfqq->slice_end - jiffies == 1)
3849 return true;
3850
3851 return false;
3852}
3853
Jens Axboe165125e2007-07-24 09:28:11 +02003854static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Jens Axboe5e705372006-07-13 12:39:25 +02003856 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003857 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003858 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003859 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Jens Axboeb4878f22005-10-20 16:42:29 +02003861 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003862 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3863 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Aaron Carroll45333d52008-08-26 15:52:36 +02003865 cfq_update_hw_tag(cfqd);
3866
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003867 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003868 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003869 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003870 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003871 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07003872 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
3873 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003875 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003876
Vivek Goyal365722b2009-10-03 15:21:27 +02003877 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04003878 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003879
Shaohua Li383cd722011-07-12 14:24:35 +02003880 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003881
3882 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04003883 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003884 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04003885 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
3886 cfqq_type(cfqq));
3887
3888 st->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003889 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3890 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003891 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003892
Shaohua Li7700fc42011-07-12 14:24:56 +02003893#ifdef CONFIG_CFQ_GROUP_IOSCHED
3894 cfqq->cfqg->ttime.last_end_request = now;
3895#endif
3896
Jens Axboecaaa5f92006-06-16 11:23:00 +02003897 /*
3898 * If this is the active queue, check if it needs to be expired,
3899 * or if we want to idle in case it has no pending requests.
3900 */
3901 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003902 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3903
Jens Axboe44f7c162007-01-19 11:51:58 +11003904 if (cfq_cfqq_slice_new(cfqq)) {
3905 cfq_set_prio_slice(cfqd, cfqq);
3906 cfq_clear_cfqq_slice_new(cfqq);
3907 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003908
3909 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003910 * Should we wait for next request to come in before we expire
3911 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003912 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003913 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003914 unsigned long extend_sl = cfqd->cfq_slice_idle;
3915 if (!cfqd->cfq_slice_idle)
3916 extend_sl = cfqd->cfq_group_idle;
3917 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003918 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003919 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003920 }
3921
Jens Axboea36e71f2009-04-15 12:15:11 +02003922 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003923 * Idling is not enabled on:
3924 * - expired queues
3925 * - idle-priority queues
3926 * - async queues
3927 * - queues with still some requests queued
3928 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003929 */
Jens Axboe08717142008-01-28 11:38:15 +01003930 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003931 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003932 else if (sync && cfqq_empty &&
3933 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003934 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003935 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003936 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003937
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003938 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003939 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
Jens Axboe89850f72006-07-22 16:48:31 +02003942static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003943{
Jens Axboe1b379d82009-08-11 08:26:11 +02003944 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003945 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003946 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003947 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003948
3949 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003950}
3951
Jens Axboe165125e2007-07-24 09:28:11 +02003952static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003953{
3954 struct cfq_data *cfqd = q->elevator->elevator_data;
3955 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003956 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003957 struct cfq_queue *cfqq;
3958
3959 /*
3960 * don't force setup of a queue from here, as a call to may_queue
3961 * does not necessarily imply that a request actually will be queued.
3962 * so just lookup a possibly existing queue, or return 'may queue'
3963 * if that fails
3964 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003965 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003966 if (!cic)
3967 return ELV_MQUEUE_MAY;
3968
Jens Axboeb0b78f82009-04-08 10:56:08 +02003969 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003970 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003971 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003972
Jens Axboe89850f72006-07-22 16:48:31 +02003973 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003974 }
3975
3976 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979/*
3980 * queue lock held here
3981 */
Jens Axboebb37b942006-12-01 10:42:33 +01003982static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
Jens Axboe5e705372006-07-13 12:39:25 +02003984 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Jens Axboe5e705372006-07-13 12:39:25 +02003986 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003987 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Jens Axboe22e2c502005-06-27 10:55:12 +02003989 BUG_ON(!cfqq->allocated[rw]);
3990 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003992 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003993 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003994 rq->elv.priv[0] = NULL;
3995 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 cfq_put_queue(cfqq);
3998 }
3999}
4000
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004001static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004002cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004003 struct cfq_queue *cfqq)
4004{
4005 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4006 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004007 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004008 cfq_put_queue(cfqq);
4009 return cic_to_cfqq(cic, 1);
4010}
4011
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004012/*
4013 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4014 * was the last process referring to said cfqq.
4015 */
4016static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004017split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004018{
4019 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004020 cfqq->pid = current->pid;
4021 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004022 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004023 return cfqq;
4024 }
4025
4026 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004027
4028 cfq_put_cooperator(cfqq);
4029
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004030 cfq_put_queue(cfqq);
4031 return NULL;
4032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004034 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004036static int
Tejun Heo852c7882012-03-05 13:15:27 -08004037cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4038 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
4040 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004041 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004043 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004044 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 might_sleep_if(gfp_mask & __GFP_WAIT);
4047
Tejun Heo216284c2011-12-14 00:33:38 +01004048 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004049
Tejun Heo598971b2012-03-19 15:10:58 -07004050 check_ioprio_changed(cic, bio);
4051 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004052new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004053 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004054 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004055 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004056 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004057 } else {
4058 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004059 * If the queue was seeky for too long, break it apart.
4060 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004061 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004062 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4063 cfqq = split_cfqq(cic, cfqq);
4064 if (!cfqq)
4065 goto new_queue;
4066 }
4067
4068 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004069 * Check to see if this queue is scheduled to merge with
4070 * another, closely cooperating queue. The merging of
4071 * queues happens here as it must be done in process context.
4072 * The reference on new_cfqq was taken in merge_cfqqs.
4073 */
4074 if (cfqq->new_cfqq)
4075 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004079
Jens Axboe6fae9c22011-03-01 15:04:39 -05004080 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004081 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004082 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004083 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01004084 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02004085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
David Howells65f27f32006-11-22 14:55:48 +00004088static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004089{
David Howells65f27f32006-11-22 14:55:48 +00004090 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004091 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004092 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004093
Jens Axboe40bb54d2009-04-15 12:11:10 +02004094 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004095 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004096 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004097}
4098
4099/*
4100 * Timer running if the active_queue is currently idling inside its time slice
4101 */
4102static void cfq_idle_slice_timer(unsigned long data)
4103{
4104 struct cfq_data *cfqd = (struct cfq_data *) data;
4105 struct cfq_queue *cfqq;
4106 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004107 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004108
Jens Axboe7b679132008-05-30 12:23:07 +02004109 cfq_log(cfqd, "idle timer fired");
4110
Jens Axboe22e2c502005-06-27 10:55:12 +02004111 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4112
Jens Axboefe094d92008-01-31 13:08:54 +01004113 cfqq = cfqd->active_queue;
4114 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004115 timed_out = 0;
4116
Jens Axboe22e2c502005-06-27 10:55:12 +02004117 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004118 * We saw a request before the queue expired, let it through
4119 */
4120 if (cfq_cfqq_must_dispatch(cfqq))
4121 goto out_kick;
4122
4123 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004124 * expired
4125 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004126 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004127 goto expire;
4128
4129 /*
4130 * only expire and reinvoke request handler, if there are
4131 * other queues with pending requests
4132 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004133 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004134 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004135
4136 /*
4137 * not expired and it has a request pending, let it dispatch
4138 */
Jens Axboe75e50982009-04-07 08:56:14 +02004139 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004140 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004141
4142 /*
4143 * Queue depth flag is reset only when the idle didn't succeed
4144 */
4145 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004146 }
4147expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004148 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004149out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004150 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004151out_cont:
4152 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4153}
4154
Jens Axboe3b181522005-06-27 10:56:24 +02004155static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4156{
4157 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004158 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004159}
Jens Axboe22e2c502005-06-27 10:55:12 +02004160
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004161static void cfq_put_async_queues(struct cfq_data *cfqd)
4162{
4163 int i;
4164
4165 for (i = 0; i < IOPRIO_BE_NR; i++) {
4166 if (cfqd->async_cfqq[0][i])
4167 cfq_put_queue(cfqd->async_cfqq[0][i]);
4168 if (cfqd->async_cfqq[1][i])
4169 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004170 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01004171
4172 if (cfqd->async_idle_cfqq)
4173 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004174}
4175
Jens Axboeb374d182008-10-31 10:05:07 +01004176static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
Jens Axboe22e2c502005-06-27 10:55:12 +02004178 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004179 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004180
Jens Axboe3b181522005-06-27 10:56:24 +02004181 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004182
Al Virod9ff4182006-03-18 13:51:22 -05004183 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004184
Al Virod9ff4182006-03-18 13:51:22 -05004185 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004186 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004187
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004188 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08004189
4190 spin_unlock_irq(q->queue_lock);
4191
Al Viroa90d7422006-03-18 12:05:37 -05004192 cfq_shutdown_timer_wq(cfqd);
4193
Tejun Heoffea73f2012-06-04 10:02:29 +02004194#ifdef CONFIG_CFQ_GROUP_IOSCHED
4195 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4196#else
Tejun Heof51b8022012-03-05 13:15:05 -08004197 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004198#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004199 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Tejun Heob2fab5a2012-03-05 13:14:57 -08004202static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
4204 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004205 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004206 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
Christoph Lameter94f60302007-07-17 04:03:29 -07004208 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01004209 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004210 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004211
Tejun Heof51b8022012-03-05 13:15:05 -08004212 cfqd->queue = q;
4213 q->elevator->elevator_data = cfqd;
4214
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004215 /* Init root service tree */
4216 cfqd->grp_service_tree = CFQ_RB_ROOT;
4217
Tejun Heof51b8022012-03-05 13:15:05 -08004218 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004219#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004220 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004221 if (ret)
4222 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004223
Tejun Heoa2b16932012-04-13 13:11:33 -07004224 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004225#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004226 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004227 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4228 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004229 if (!cfqd->root_group)
4230 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004231
Tejun Heoa2b16932012-04-13 13:11:33 -07004232 cfq_init_cfqg_base(cfqd->root_group);
4233#endif
Tejun Heo3381cb82012-04-01 14:38:44 -07004234 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -08004235 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004236
Jens Axboe26a2ac02009-04-23 12:13:27 +02004237 /*
4238 * Not strictly needed (since RB_ROOT just clears the node and we
4239 * zeroed cfqd on alloc), but better be safe in case someone decides
4240 * to add magic to the rb code
4241 */
4242 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4243 cfqd->prio_trees[i] = RB_ROOT;
4244
Jens Axboe6118b702009-06-30 09:34:12 +02004245 /*
4246 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4247 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004248 * will not attempt to free it. oom_cfqq is linked to root_group
4249 * but shouldn't hold a reference as it'll never be unlinked. Lose
4250 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004251 */
4252 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004253 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004254
4255 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004256 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004257 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004258 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Jens Axboe22e2c502005-06-27 10:55:12 +02004260 init_timer(&cfqd->idle_slice_timer);
4261 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4262 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4263
Jens Axboe23e018a2009-10-05 08:52:35 +02004264 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004267 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4268 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 cfqd->cfq_back_max = cfq_back_max;
4270 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004271 cfqd->cfq_slice[0] = cfq_slice_async;
4272 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004273 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004274 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4275 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004276 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004277 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004278 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004279 /*
4280 * we optimistically start assuming sync ops weren't delayed in last
4281 * second, in order to have larger depth for async operations.
4282 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004283 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004284 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004285
4286out_free:
4287 kfree(cfqd);
4288 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291/*
4292 * sysfs parts below -->
4293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294static ssize_t
4295cfq_var_show(unsigned int var, char *page)
4296{
4297 return sprintf(page, "%d\n", var);
4298}
4299
4300static ssize_t
4301cfq_var_store(unsigned int *var, const char *page, size_t count)
4302{
4303 char *p = (char *) page;
4304
4305 *var = simple_strtoul(p, &p, 10);
4306 return count;
4307}
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004310static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004312 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 unsigned int __data = __VAR; \
4314 if (__CONV) \
4315 __data = jiffies_to_msecs(__data); \
4316 return cfq_var_show(__data, (page)); \
4317}
4318SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004319SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4320SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004321SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4322SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004323SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004324SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004325SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4326SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4327SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004328SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004329SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330#undef SHOW_FUNCTION
4331
4332#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004333static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004335 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 unsigned int __data; \
4337 int ret = cfq_var_store(&__data, (page), count); \
4338 if (__data < (MIN)) \
4339 __data = (MIN); \
4340 else if (__data > (MAX)) \
4341 __data = (MAX); \
4342 if (__CONV) \
4343 *(__PTR) = msecs_to_jiffies(__data); \
4344 else \
4345 *(__PTR) = __data; \
4346 return ret; \
4347}
4348STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004349STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4350 UINT_MAX, 1);
4351STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4352 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004353STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004354STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4355 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004356STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004357STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004358STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4359STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004360STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4361 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004362STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004363STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364#undef STORE_FUNCTION
4365
Al Viroe572ec72006-03-18 22:27:18 -05004366#define CFQ_ATTR(name) \
4367 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004368
Al Viroe572ec72006-03-18 22:27:18 -05004369static struct elv_fs_entry cfq_attrs[] = {
4370 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004371 CFQ_ATTR(fifo_expire_sync),
4372 CFQ_ATTR(fifo_expire_async),
4373 CFQ_ATTR(back_seek_max),
4374 CFQ_ATTR(back_seek_penalty),
4375 CFQ_ATTR(slice_sync),
4376 CFQ_ATTR(slice_async),
4377 CFQ_ATTR(slice_async_rq),
4378 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004379 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004380 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004381 CFQ_ATTR(target_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004382 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383};
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385static struct elevator_type iosched_cfq = {
4386 .ops = {
4387 .elevator_merge_fn = cfq_merge,
4388 .elevator_merged_fn = cfq_merged_request,
4389 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004390 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004391 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004392 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004394 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004397 .elevator_former_req_fn = elv_rb_former_request,
4398 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004399 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004400 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 .elevator_set_req_fn = cfq_set_request,
4402 .elevator_put_req_fn = cfq_put_request,
4403 .elevator_may_queue_fn = cfq_may_queue,
4404 .elevator_init_fn = cfq_init_queue,
4405 .elevator_exit_fn = cfq_exit_queue,
4406 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004407 .icq_size = sizeof(struct cfq_io_cq),
4408 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004409 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004410 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 .elevator_owner = THIS_MODULE,
4412};
4413
Vivek Goyal3e252062009-12-04 10:36:42 -05004414#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004415static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004416 .pd_size = sizeof(struct cfq_group),
4417 .cftypes = cfq_blkcg_files,
4418
4419 .pd_init_fn = cfq_pd_init,
4420 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004421};
Vivek Goyal3e252062009-12-04 10:36:42 -05004422#endif
4423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424static int __init cfq_init(void)
4425{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004426 int ret;
4427
Jens Axboe22e2c502005-06-27 10:55:12 +02004428 /*
4429 * could be 0 on HZ < 1000 setups
4430 */
4431 if (!cfq_slice_async)
4432 cfq_slice_async = 1;
4433 if (!cfq_slice_idle)
4434 cfq_slice_idle = 1;
4435
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004436#ifdef CONFIG_CFQ_GROUP_IOSCHED
4437 if (!cfq_group_idle)
4438 cfq_group_idle = 1;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004439
Tejun Heo3c798392012-04-16 13:57:25 -07004440 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004441 if (ret)
4442 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004443#else
4444 cfq_group_idle = 0;
4445#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004446
Tejun Heofd794952012-06-04 10:01:38 +02004447 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004448 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4449 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004450 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Tejun Heo3d3c2372011-12-14 00:33:42 +01004452 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004453 if (ret)
4454 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004455
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004456 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004457
4458err_free_pool:
4459 kmem_cache_destroy(cfq_pool);
4460err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004461#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004462 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004463#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465}
4466
4467static void __exit cfq_exit(void)
4468{
Tejun Heoffea73f2012-06-04 10:02:29 +02004469#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004470 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004471#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004473 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474}
4475
4476module_init(cfq_init);
4477module_exit(cfq_exit);
4478
4479MODULE_AUTHOR("Jens Axboe");
4480MODULE_LICENSE("GPL");
4481MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");