blob: e8f31069b37927799c9d9d1c838862b6b4083472 [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;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050088 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020089 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020090};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020091#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
92 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020093
94/*
Jens Axboe6118b702009-06-30 09:34:12 +020095 * Per process-grouping structure
96 */
97struct cfq_queue {
98 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +010099 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200100 /* various state flags, see below */
101 unsigned int flags;
102 /* parent cfq_data */
103 struct cfq_data *cfqd;
104 /* service_tree member */
105 struct rb_node rb_node;
106 /* service_tree key */
107 unsigned long rb_key;
108 /* prio tree member */
109 struct rb_node p_node;
110 /* prio tree root we belong to, if any */
111 struct rb_root *p_root;
112 /* sorted list of pending requests */
113 struct rb_root sort_list;
114 /* if fifo isn't expired, next request to serve */
115 struct request *next_rq;
116 /* requests queued in sort_list */
117 int queued[2];
118 /* currently allocated requests */
119 int allocated[2];
120 /* fifo list of requests in sort_list */
121 struct list_head fifo;
122
Vivek Goyaldae739e2009-12-03 12:59:45 -0500123 /* time when queue got scheduled in to dispatch first request. */
124 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500125 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100126 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500127 /* time when first request from queue completed and slice started. */
128 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200129 unsigned long slice_end;
130 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200131
Christoph Hellwig65299a32011-08-23 14:50:29 +0200132 /* pending priority requests */
133 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200134 /* number of requests that are on the dispatch list or inside driver */
135 int dispatched;
136
137 /* io prio of this group */
138 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200139 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200140
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100141 pid_t pid;
142
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100143 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400144 sector_t last_request_pos;
145
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100146 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400147 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500148 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200149 /* Number of sectors dispatched from queue in single dispatch round */
150 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200151};
152
153/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100154 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100155 * IDLE is handled separately, so it has negative index
156 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400157enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100158 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500159 RT_WORKLOAD = 1,
160 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200161 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100162};
163
164/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100165 * Second index in the service_trees.
166 */
167enum wl_type_t {
168 ASYNC_WORKLOAD = 0,
169 SYNC_NOIDLE_WORKLOAD = 1,
170 SYNC_WORKLOAD = 2
171};
172
Tejun Heo155fead2012-04-01 14:38:44 -0700173struct cfqg_stats {
174#ifdef CONFIG_CFQ_GROUP_IOSCHED
175 /* total bytes transferred */
176 struct blkg_rwstat service_bytes;
177 /* total IOs serviced, post merge */
178 struct blkg_rwstat serviced;
179 /* number of ios merged */
180 struct blkg_rwstat merged;
181 /* total time spent on device in ns, may not be accurate w/ queueing */
182 struct blkg_rwstat service_time;
183 /* total time spent waiting in scheduler queue in ns */
184 struct blkg_rwstat wait_time;
185 /* number of IOs queued up */
186 struct blkg_rwstat queued;
187 /* total sectors transferred */
188 struct blkg_stat sectors;
189 /* total disk time and nr sectors dispatched by this group */
190 struct blkg_stat time;
191#ifdef CONFIG_DEBUG_BLK_CGROUP
192 /* time not charged to this cgroup */
193 struct blkg_stat unaccounted_time;
194 /* sum of number of ios queued across all samples */
195 struct blkg_stat avg_queue_size_sum;
196 /* count of samples taken for average */
197 struct blkg_stat avg_queue_size_samples;
198 /* how many times this group has been removed from service tree */
199 struct blkg_stat dequeue;
200 /* total time spent waiting for it to be assigned a timeslice. */
201 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700202 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700203 struct blkg_stat idle_time;
204 /* total time with empty current active q with other requests queued */
205 struct blkg_stat empty_time;
206 /* fields after this shouldn't be cleared on stat reset */
207 uint64_t start_group_wait_time;
208 uint64_t start_idle_time;
209 uint64_t start_empty_time;
210 uint16_t flags;
211#endif /* CONFIG_DEBUG_BLK_CGROUP */
212#endif /* CONFIG_CFQ_GROUP_IOSCHED */
213};
214
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500215/* This is per cgroup per device grouping structure */
216struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700217 /* must be the first member */
218 struct blkg_policy_data pd;
219
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500220 /* group service_tree member */
221 struct rb_node rb_node;
222
223 /* group service_tree key */
224 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800225
226 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800227 * The number of active cfqgs and sum of their weights under this
228 * cfqg. This covers this cfqg's leaf_weight and all children's
229 * weights, but does not cover weights of further descendants.
230 *
231 * If a cfqg is on the service tree, it's active. An active cfqg
232 * also activates its parent and contributes to the children_weight
233 * of the parent.
234 */
235 int nr_active;
236 unsigned int children_weight;
237
238 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800239 * vfraction is the fraction of vdisktime that the tasks in this
240 * cfqg are entitled to. This is determined by compounding the
241 * ratios walking up from this cfqg to the root.
242 *
243 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
244 * vfractions on a service tree is approximately 1. The sum may
245 * deviate a bit due to rounding errors and fluctuations caused by
246 * cfqgs entering and leaving the service tree.
247 */
248 unsigned int vfraction;
249
250 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800251 * There are two weights - (internal) weight is the weight of this
252 * cfqg against the sibling cfqgs. leaf_weight is the wight of
253 * this cfqg against the child cfqgs. For the root cfqg, both
254 * weights are kept in sync for backward compatibility.
255 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500256 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100257 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700258 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500259
Tejun Heoe71357e2013-01-09 08:05:10 -0800260 unsigned int leaf_weight;
261 unsigned int new_leaf_weight;
262 unsigned int dev_leaf_weight;
263
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500264 /* number of cfqq currently on this group */
265 int nr_cfqq;
266
Jens Axboe22e2c502005-06-27 10:55:12 +0200267 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200268 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200269 * create the array for each prio class but at run time it is used
270 * only for RT and BE class and slot for IDLE class remains unused.
271 * This is primarily done to avoid confusion and a gcc warning.
272 */
273 unsigned int busy_queues_avg[CFQ_PRIO_NR];
274 /*
275 * rr lists of queues with requests. We maintain service trees for
276 * RT and BE classes. These trees are subdivided in subclasses
277 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
278 * class there is no subclassification and all the cfq queues go on
279 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100280 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200281 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100282 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100283 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500284
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400285 unsigned long saved_wl_slice;
286 enum wl_type_t saved_wl_type;
287 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800288
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200289 /* number of requests that are on the dispatch list or inside driver */
290 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200291 struct cfq_ttime ttime;
Tejun Heo155fead2012-04-01 14:38:44 -0700292 struct cfqg_stats stats;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500293};
294
Tejun Heoc5869802011-12-14 00:33:41 +0100295struct cfq_io_cq {
296 struct io_cq icq; /* must be the first member */
297 struct cfq_queue *cfqq[2];
298 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700299 int ioprio; /* the current ioprio */
300#ifdef CONFIG_CFQ_GROUP_IOSCHED
301 uint64_t blkcg_id; /* the current blkcg ID */
302#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100303};
304
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500305/*
306 * Per block device queue structure
307 */
308struct cfq_data {
309 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500310 /* Root service tree for cfq_groups */
311 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800312 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500313
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100314 /*
315 * The priority currently being served
316 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400317 enum wl_class_t serving_wl_class;
318 enum wl_type_t serving_wl_type;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100319 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500320 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200321
322 /*
323 * Each priority tree is sorted by next_request position. These
324 * trees are used when determining if two or more queues are
325 * interleaving requests (see cfq_close_cooperator).
326 */
327 struct rb_root prio_trees[CFQ_PRIO_LISTS];
328
Jens Axboe22e2c502005-06-27 10:55:12 +0200329 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100330 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200331
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100332 int rq_in_driver;
333 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200334
335 /*
336 * queue-depth detection
337 */
338 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200339 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100340 /*
341 * hw_tag can be
342 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
343 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
344 * 0 => no NCQ
345 */
346 int hw_tag_est_depth;
347 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200348
349 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200350 * idle window management
351 */
352 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200353 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200354
355 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100356 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200357
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200358 /*
359 * async queue for each priority case
360 */
361 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
362 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200363
Jens Axboe6d048f52007-04-25 12:44:27 +0200364 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * tunables, see top of file
368 */
369 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200370 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 unsigned int cfq_back_penalty;
372 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200373 unsigned int cfq_slice[2];
374 unsigned int cfq_slice_async_rq;
375 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200376 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200377 unsigned int cfq_latency;
Tao Ma5bf14c02012-04-01 14:33:39 -0700378 unsigned int cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500379
Jens Axboe6118b702009-06-30 09:34:12 +0200380 /*
381 * Fallback dummy cfqq for extreme OOM conditions
382 */
383 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200384
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100385 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386};
387
Vivek Goyal25fb5162009-12-03 12:59:46 -0500388static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
389
Vivek Goyal34b98d02012-10-03 16:56:58 -0400390static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400391 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500392 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100393{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500394 if (!cfqg)
395 return NULL;
396
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400397 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500398 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100399
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400400 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100401}
402
Jens Axboe3b181522005-06-27 10:56:24 +0200403enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100404 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
405 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200406 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100407 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100408 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
409 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
410 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100411 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200412 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400413 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100414 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100415 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500416 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200417};
418
419#define CFQ_CFQQ_FNS(name) \
420static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
421{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100422 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200423} \
424static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
425{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100426 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200427} \
428static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
429{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100430 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200431}
432
433CFQ_CFQQ_FNS(on_rr);
434CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200435CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200436CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200437CFQ_CFQQ_FNS(fifo_expire);
438CFQ_CFQQ_FNS(idle_window);
439CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100440CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200441CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200442CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100443CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100444CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500445CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200446#undef CFQ_CFQQ_FNS
447
Tejun Heof95a04a2012-04-16 13:57:26 -0700448static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
449{
450 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
451}
452
Tejun Heof95a04a2012-04-16 13:57:26 -0700453static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
454{
455 return pd_to_blkg(&cfqg->pd);
456}
457
Tejun Heo629ed0b2012-04-01 14:38:44 -0700458#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700459
Tejun Heo155fead2012-04-01 14:38:44 -0700460/* cfqg stats flags */
461enum cfqg_stats_flags {
462 CFQG_stats_waiting = 0,
463 CFQG_stats_idling,
464 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700465};
466
Tejun Heo155fead2012-04-01 14:38:44 -0700467#define CFQG_FLAG_FNS(name) \
468static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700469{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700470 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700471} \
Tejun Heo155fead2012-04-01 14:38:44 -0700472static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700473{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700474 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700475} \
Tejun Heo155fead2012-04-01 14:38:44 -0700476static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700477{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700478 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700479} \
480
Tejun Heo155fead2012-04-01 14:38:44 -0700481CFQG_FLAG_FNS(waiting)
482CFQG_FLAG_FNS(idling)
483CFQG_FLAG_FNS(empty)
484#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700485
486/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700487static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700488{
489 unsigned long long now;
490
Tejun Heo155fead2012-04-01 14:38:44 -0700491 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700492 return;
493
494 now = sched_clock();
495 if (time_after64(now, stats->start_group_wait_time))
496 blkg_stat_add(&stats->group_wait_time,
497 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700498 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700499}
500
501/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700502static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
503 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700504{
Tejun Heo155fead2012-04-01 14:38:44 -0700505 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700506
Tejun Heo155fead2012-04-01 14:38:44 -0700507 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700508 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700509 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700510 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700511 stats->start_group_wait_time = sched_clock();
512 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700513}
514
515/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700516static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700517{
518 unsigned long long now;
519
Tejun Heo155fead2012-04-01 14:38:44 -0700520 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700521 return;
522
523 now = sched_clock();
524 if (time_after64(now, stats->start_empty_time))
525 blkg_stat_add(&stats->empty_time,
526 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700527 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700528}
529
Tejun Heo155fead2012-04-01 14:38:44 -0700530static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700531{
Tejun Heo155fead2012-04-01 14:38:44 -0700532 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700533}
534
Tejun Heo155fead2012-04-01 14:38:44 -0700535static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700536{
Tejun Heo155fead2012-04-01 14:38:44 -0700537 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700538
539 if (blkg_rwstat_sum(&stats->queued))
540 return;
541
542 /*
543 * group is already marked empty. This can happen if cfqq got new
544 * request in parent group and moved to this group while being added
545 * to service tree. Just ignore the event and move on.
546 */
Tejun Heo155fead2012-04-01 14:38:44 -0700547 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700548 return;
549
550 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700551 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700552}
553
Tejun Heo155fead2012-04-01 14:38:44 -0700554static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700555{
Tejun Heo155fead2012-04-01 14:38:44 -0700556 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700557
Tejun Heo155fead2012-04-01 14:38:44 -0700558 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700559 unsigned long long now = sched_clock();
560
561 if (time_after64(now, stats->start_idle_time))
562 blkg_stat_add(&stats->idle_time,
563 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700564 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700565 }
566}
567
Tejun Heo155fead2012-04-01 14:38:44 -0700568static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700569{
Tejun Heo155fead2012-04-01 14:38:44 -0700570 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700571
Tejun Heo155fead2012-04-01 14:38:44 -0700572 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700573
574 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700575 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700576}
577
Tejun Heo155fead2012-04-01 14:38:44 -0700578static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700579{
Tejun Heo155fead2012-04-01 14:38:44 -0700580 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700581
582 blkg_stat_add(&stats->avg_queue_size_sum,
583 blkg_rwstat_sum(&stats->queued));
584 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700585 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700586}
587
588#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
589
Tejun Heof48ec1d2012-04-13 13:11:25 -0700590static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
591static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
592static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
593static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
594static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
595static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
596static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700597
598#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
599
600#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700601
Tejun Heoffea73f2012-06-04 10:02:29 +0200602static struct blkcg_policy blkcg_policy_cfq;
603
604static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
605{
606 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
607}
608
Tejun Heod02f7aa2013-01-09 08:05:11 -0800609static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
Tejun Heo7918ffb2013-01-09 08:05:11 -0800610{
Tejun Heod02f7aa2013-01-09 08:05:11 -0800611 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800612
Tejun Heod02f7aa2013-01-09 08:05:11 -0800613 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800614}
615
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100616static inline void cfqg_get(struct cfq_group *cfqg)
617{
618 return blkg_get(cfqg_to_blkg(cfqg));
619}
620
621static inline void cfqg_put(struct cfq_group *cfqg)
622{
623 return blkg_put(cfqg_to_blkg(cfqg));
624}
625
Tejun Heo54e7ed12012-04-16 13:57:23 -0700626#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
627 char __pbuf[128]; \
628 \
629 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400630 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
631 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
632 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700633 __pbuf, ##args); \
634} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500635
Tejun Heo54e7ed12012-04-16 13:57:23 -0700636#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
637 char __pbuf[128]; \
638 \
639 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
640 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
641} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500642
Tejun Heo155fead2012-04-01 14:38:44 -0700643static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
644 struct cfq_group *curr_cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700645{
Tejun Heo155fead2012-04-01 14:38:44 -0700646 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
647 cfqg_stats_end_empty_time(&cfqg->stats);
648 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700649}
650
Tejun Heo155fead2012-04-01 14:38:44 -0700651static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
652 unsigned long time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700653{
Tejun Heo155fead2012-04-01 14:38:44 -0700654 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700655#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700656 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700657#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700658}
659
Tejun Heo155fead2012-04-01 14:38:44 -0700660static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700661{
Tejun Heo155fead2012-04-01 14:38:44 -0700662 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700663}
664
Tejun Heo155fead2012-04-01 14:38:44 -0700665static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700666{
Tejun Heo155fead2012-04-01 14:38:44 -0700667 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700668}
669
Tejun Heo155fead2012-04-01 14:38:44 -0700670static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
671 uint64_t bytes, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700672{
Tejun Heo155fead2012-04-01 14:38:44 -0700673 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
674 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
675 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700676}
677
Tejun Heo155fead2012-04-01 14:38:44 -0700678static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
679 uint64_t start_time, uint64_t io_start_time, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700680{
Tejun Heo155fead2012-04-01 14:38:44 -0700681 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700682 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700683
684 if (time_after64(now, io_start_time))
685 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
686 if (time_after64(io_start_time, start_time))
687 blkg_rwstat_add(&stats->wait_time, rw,
688 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700689}
690
Tejun Heo3c798392012-04-16 13:57:25 -0700691static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo155fead2012-04-01 14:38:44 -0700692{
693 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
694 struct cfqg_stats *stats = &cfqg->stats;
695
696 /* queued stats shouldn't be cleared */
697 blkg_rwstat_reset(&stats->service_bytes);
698 blkg_rwstat_reset(&stats->serviced);
699 blkg_rwstat_reset(&stats->merged);
700 blkg_rwstat_reset(&stats->service_time);
701 blkg_rwstat_reset(&stats->wait_time);
702 blkg_stat_reset(&stats->time);
703#ifdef CONFIG_DEBUG_BLK_CGROUP
704 blkg_stat_reset(&stats->unaccounted_time);
705 blkg_stat_reset(&stats->avg_queue_size_sum);
706 blkg_stat_reset(&stats->avg_queue_size_samples);
707 blkg_stat_reset(&stats->dequeue);
708 blkg_stat_reset(&stats->group_wait_time);
709 blkg_stat_reset(&stats->idle_time);
710 blkg_stat_reset(&stats->empty_time);
711#endif
712}
713
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100714#else /* CONFIG_CFQ_GROUP_IOSCHED */
715
Tejun Heod02f7aa2013-01-09 08:05:11 -0800716static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100717static inline void cfqg_get(struct cfq_group *cfqg) { }
718static inline void cfqg_put(struct cfq_group *cfqg) { }
719
Jens Axboe7b679132008-05-30 12:23:07 +0200720#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400721 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
722 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
723 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
724 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200725#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100726
Tejun Heo155fead2012-04-01 14:38:44 -0700727static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
728 struct cfq_group *curr_cfqg, int rw) { }
729static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
730 unsigned long time, unsigned long unaccounted_time) { }
731static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
732static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
733static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
734 uint64_t bytes, int rw) { }
735static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
736 uint64_t start_time, uint64_t io_start_time, int rw) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700737
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100738#endif /* CONFIG_CFQ_GROUP_IOSCHED */
739
Jens Axboe7b679132008-05-30 12:23:07 +0200740#define cfq_log(cfqd, fmt, args...) \
741 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
742
Vivek Goyal615f0252009-12-03 12:59:39 -0500743/* Traverses through cfq group service trees */
744#define for_each_cfqg_st(cfqg, i, j, st) \
745 for (i = 0; i <= IDLE_WORKLOAD; i++) \
746 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
747 : &cfqg->service_tree_idle; \
748 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
749 (i == IDLE_WORKLOAD && j == 0); \
750 j++, st = i < IDLE_WORKLOAD ? \
751 &cfqg->service_trees[i][j]: NULL) \
752
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200753static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
754 struct cfq_ttime *ttime, bool group_idle)
755{
756 unsigned long slice;
757 if (!sample_valid(ttime->ttime_samples))
758 return false;
759 if (group_idle)
760 slice = cfqd->cfq_group_idle;
761 else
762 slice = cfqd->cfq_slice_idle;
763 return ttime->ttime_mean > slice;
764}
Vivek Goyal615f0252009-12-03 12:59:39 -0500765
Vivek Goyal02b35082010-08-23 12:23:53 +0200766static inline bool iops_mode(struct cfq_data *cfqd)
767{
768 /*
769 * If we are not idling on queues and it is a NCQ drive, parallel
770 * execution of requests is on and measuring time is not possible
771 * in most of the cases until and unless we drive shallower queue
772 * depths and that becomes a performance bottleneck. In such cases
773 * switch to start providing fairness in terms of number of IOs.
774 */
775 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
776 return true;
777 else
778 return false;
779}
780
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400781static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100782{
783 if (cfq_class_idle(cfqq))
784 return IDLE_WORKLOAD;
785 if (cfq_class_rt(cfqq))
786 return RT_WORKLOAD;
787 return BE_WORKLOAD;
788}
789
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100790
791static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
792{
793 if (!cfq_cfqq_sync(cfqq))
794 return ASYNC_WORKLOAD;
795 if (!cfq_cfqq_idle_window(cfqq))
796 return SYNC_NOIDLE_WORKLOAD;
797 return SYNC_WORKLOAD;
798}
799
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400800static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500801 struct cfq_data *cfqd,
802 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100803{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400804 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500805 return cfqg->service_tree_idle.count;
806
Vivek Goyal34b98d02012-10-03 16:56:58 -0400807 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
808 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
809 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100810}
811
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500812static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
813 struct cfq_group *cfqg)
814{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400815 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
816 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500817}
818
Jens Axboe165125e2007-07-24 09:28:11 +0200819static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800820static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700821 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800822 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200823
Tejun Heoc5869802011-12-14 00:33:41 +0100824static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
825{
826 /* cic->icq is the first member, %NULL will convert to %NULL */
827 return container_of(icq, struct cfq_io_cq, icq);
828}
829
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100830static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
831 struct io_context *ioc)
832{
833 if (ioc)
834 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
835 return NULL;
836}
837
Tejun Heoc5869802011-12-14 00:33:41 +0100838static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200839{
Jens Axboea6151c32009-10-07 20:02:57 +0200840 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200841}
842
Tejun Heoc5869802011-12-14 00:33:41 +0100843static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
844 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200845{
Jens Axboea6151c32009-10-07 20:02:57 +0200846 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200847}
848
Tejun Heoc5869802011-12-14 00:33:41 +0100849static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400850{
Tejun Heoc5869802011-12-14 00:33:41 +0100851 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400852}
853
Vasily Tarasov91fac312007-04-25 12:29:51 +0200854/*
855 * We regard a request as SYNC, if it's either a read or has the SYNC bit
856 * set (in which case it could also be direct WRITE).
857 */
Jens Axboea6151c32009-10-07 20:02:57 +0200858static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200859{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200860 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700864 * scheduler run of queue, if there are requests pending and no one in the
865 * driver that will restart queueing
866 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200867static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700868{
Jens Axboe7b679132008-05-30 12:23:07 +0200869 if (cfqd->busy_queues) {
870 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200871 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200872 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100876 * Scale schedule slice based on io priority. Use the sync time slice only
877 * if a queue is marked sync and has sync io queued. A sync queue with async
878 * io only, should not get full sync slice length.
879 */
Jens Axboea6151c32009-10-07 20:02:57 +0200880static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200881 unsigned short prio)
882{
883 const int base_slice = cfqd->cfq_slice[sync];
884
885 WARN_ON(prio >= IOPRIO_BE_NR);
886
887 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
888}
889
Jens Axboe44f7c162007-01-19 11:51:58 +1100890static inline int
891cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
892{
Jens Axboed9e76202007-04-20 14:27:50 +0200893 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100894}
895
Tejun Heo1d3650f2013-01-09 08:05:11 -0800896/**
897 * cfqg_scale_charge - scale disk time charge according to cfqg weight
898 * @charge: disk time being charged
899 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
900 *
901 * Scale @charge according to @vfraction, which is in range (0, 1]. The
902 * scaling is inversely proportional.
903 *
904 * scaled = charge / vfraction
905 *
906 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
907 */
908static inline u64 cfqg_scale_charge(unsigned long charge,
909 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500910{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800911 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500912
Tejun Heo1d3650f2013-01-09 08:05:11 -0800913 /* charge / vfraction */
914 c <<= CFQ_SERVICE_SHIFT;
915 do_div(c, vfraction);
916 return c;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500917}
918
919static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
920{
921 s64 delta = (s64)(vdisktime - min_vdisktime);
922 if (delta > 0)
923 min_vdisktime = vdisktime;
924
925 return min_vdisktime;
926}
927
928static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
929{
930 s64 delta = (s64)(vdisktime - min_vdisktime);
931 if (delta < 0)
932 min_vdisktime = vdisktime;
933
934 return min_vdisktime;
935}
936
937static void update_min_vdisktime(struct cfq_rb_root *st)
938{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500939 struct cfq_group *cfqg;
940
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500941 if (st->left) {
942 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100943 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
944 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500945 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500946}
947
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100948/*
949 * get averaged number of queues of RT/BE priority.
950 * average is updated, with a formula that gives more weight to higher numbers,
951 * to quickly follows sudden increases and decrease slowly
952 */
953
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500954static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
955 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100956{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100957 unsigned min_q, max_q;
958 unsigned mult = cfq_hist_divisor - 1;
959 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500960 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100961
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500962 min_q = min(cfqg->busy_queues_avg[rt], busy);
963 max_q = max(cfqg->busy_queues_avg[rt], busy);
964 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100965 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500966 return cfqg->busy_queues_avg[rt];
967}
968
969static inline unsigned
970cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
971{
Tejun Heo41cad6a2013-01-09 08:05:11 -0800972 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100973}
974
Shaohua Lic553f8e2011-01-14 08:41:03 +0100975static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700976cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100977{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100978 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
979 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500980 /*
981 * interested queues (we consider only the ones with the same
982 * priority class in the cfq group)
983 */
984 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
985 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100986 unsigned sync_slice = cfqd->cfq_slice[1];
987 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500988 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
989
990 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100991 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
992 /* scale low_slice according to IO priority
993 * and sync vs async */
994 unsigned low_slice =
995 min(slice, base_low_slice * slice / sync_slice);
996 /* the adapted slice value is scaled to fit all iqs
997 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500998 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100999 low_slice);
1000 }
1001 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001002 return slice;
1003}
1004
1005static inline void
1006cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1007{
Vivek Goyalba5bd522011-01-19 08:25:02 -07001008 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001009
Vivek Goyaldae739e2009-12-03 12:59:45 -05001010 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001011 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001012 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +02001013 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +11001014}
1015
1016/*
1017 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1018 * isn't valid until the first request from the dispatch is activated
1019 * and the slice time set.
1020 */
Jens Axboea6151c32009-10-07 20:02:57 +02001021static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001022{
1023 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001024 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001025 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01001026 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001027
Shaohua Lic1e44752010-11-08 15:01:02 +01001028 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001029}
1030
1031/*
Jens Axboe5e705372006-07-13 12:39:25 +02001032 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001034 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Jens Axboe5e705372006-07-13 12:39:25 +02001036static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001037cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001039 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001041#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1042#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1043 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Jens Axboe5e705372006-07-13 12:39:25 +02001045 if (rq1 == NULL || rq1 == rq2)
1046 return rq2;
1047 if (rq2 == NULL)
1048 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001049
Namhyung Kim229836b2011-05-24 10:23:21 +02001050 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1051 return rq_is_sync(rq1) ? rq1 : rq2;
1052
Christoph Hellwig65299a32011-08-23 14:50:29 +02001053 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1054 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001055
Tejun Heo83096eb2009-05-07 22:24:39 +09001056 s1 = blk_rq_pos(rq1);
1057 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /*
1060 * by definition, 1KiB is 2 sectors
1061 */
1062 back_max = cfqd->cfq_back_max * 2;
1063
1064 /*
1065 * Strict one way elevator _except_ in the case where we allow
1066 * short backward seeks which are biased as twice the cost of a
1067 * similar forward seek.
1068 */
1069 if (s1 >= last)
1070 d1 = s1 - last;
1071 else if (s1 + back_max >= last)
1072 d1 = (last - s1) * cfqd->cfq_back_penalty;
1073 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001074 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (s2 >= last)
1077 d2 = s2 - last;
1078 else if (s2 + back_max >= last)
1079 d2 = (last - s2) * cfqd->cfq_back_penalty;
1080 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001081 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Andreas Mohre8a99052006-03-28 08:59:49 +02001085 /*
1086 * By doing switch() on the bit mask "wrap" we avoid having to
1087 * check two variables for all permutations: --> faster!
1088 */
1089 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001090 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001091 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001092 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001093 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001094 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001095 else {
1096 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001097 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001098 else
Jens Axboe5e705372006-07-13 12:39:25 +02001099 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001100 }
1101
1102 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001103 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001104 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001105 return rq2;
1106 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001107 default:
1108 /*
1109 * Since both rqs are wrapped,
1110 * start with the one that's further behind head
1111 * (--> only *one* back seek required),
1112 * since back seek takes more time than forward.
1113 */
1114 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001115 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 else
Jens Axboe5e705372006-07-13 12:39:25 +02001117 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
1120
Jens Axboe498d3aa22007-04-26 12:54:48 +02001121/*
1122 * The below is leftmost cache rbtree addon
1123 */
Jens Axboe08717142008-01-28 11:38:15 +01001124static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001125{
Vivek Goyal615f0252009-12-03 12:59:39 -05001126 /* Service tree is empty */
1127 if (!root->count)
1128 return NULL;
1129
Jens Axboecc09e292007-04-26 12:53:50 +02001130 if (!root->left)
1131 root->left = rb_first(&root->rb);
1132
Jens Axboe08717142008-01-28 11:38:15 +01001133 if (root->left)
1134 return rb_entry(root->left, struct cfq_queue, rb_node);
1135
1136 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001137}
1138
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001139static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1140{
1141 if (!root->left)
1142 root->left = rb_first(&root->rb);
1143
1144 if (root->left)
1145 return rb_entry_cfqg(root->left);
1146
1147 return NULL;
1148}
1149
Jens Axboea36e71f2009-04-15 12:15:11 +02001150static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1151{
1152 rb_erase(n, root);
1153 RB_CLEAR_NODE(n);
1154}
1155
Jens Axboecc09e292007-04-26 12:53:50 +02001156static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1157{
1158 if (root->left == n)
1159 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001160 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001161 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001162}
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164/*
1165 * would be nice to take fifo expire time into account as well
1166 */
Jens Axboe5e705372006-07-13 12:39:25 +02001167static struct request *
1168cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1169 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Jens Axboe21183b02006-07-13 12:33:14 +02001171 struct rb_node *rbnext = rb_next(&last->rb_node);
1172 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001173 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Jens Axboe21183b02006-07-13 12:33:14 +02001175 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001178 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001181 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001182 else {
1183 rbnext = rb_first(&cfqq->sort_list);
1184 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001185 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001188 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Jens Axboed9e76202007-04-20 14:27:50 +02001191static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1192 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Jens Axboed9e76202007-04-20 14:27:50 +02001194 /*
1195 * just an approximation, should be ok.
1196 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001197 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001198 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001199}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001201static inline s64
1202cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1203{
1204 return cfqg->vdisktime - st->min_vdisktime;
1205}
1206
1207static void
1208__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1209{
1210 struct rb_node **node = &st->rb.rb_node;
1211 struct rb_node *parent = NULL;
1212 struct cfq_group *__cfqg;
1213 s64 key = cfqg_key(st, cfqg);
1214 int left = 1;
1215
1216 while (*node != NULL) {
1217 parent = *node;
1218 __cfqg = rb_entry_cfqg(parent);
1219
1220 if (key < cfqg_key(st, __cfqg))
1221 node = &parent->rb_left;
1222 else {
1223 node = &parent->rb_right;
1224 left = 0;
1225 }
1226 }
1227
1228 if (left)
1229 st->left = &cfqg->rb_node;
1230
1231 rb_link_node(&cfqg->rb_node, parent, node);
1232 rb_insert_color(&cfqg->rb_node, &st->rb);
1233}
1234
1235static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001236cfq_update_group_weight(struct cfq_group *cfqg)
1237{
1238 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001239
Tejun Heo3381cb82012-04-01 14:38:44 -07001240 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001241 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001242 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001243 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001244
1245 if (cfqg->new_leaf_weight) {
1246 cfqg->leaf_weight = cfqg->new_leaf_weight;
1247 cfqg->new_leaf_weight = 0;
1248 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001249}
1250
1251static void
1252cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1253{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001254 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001255 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001256 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001257 bool propagate;
1258
1259 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001260 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1261
1262 cfq_update_group_weight(cfqg);
1263 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001264
1265 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001266 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1267 * entitled to. vfraction is calculated by walking the tree
1268 * towards the root calculating the fraction it has at each level.
1269 * The compounded ratio is how much vfraction @cfqg owns.
1270 *
1271 * Start with the proportion tasks in this cfqg has against active
1272 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001273 */
1274 propagate = !pos->nr_active++;
1275 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001276 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001277
Tejun Heo1d3650f2013-01-09 08:05:11 -08001278 /*
1279 * Compound ->weight walking up the tree. Both activation and
1280 * vfraction calculation are done in the same loop. Propagation
1281 * stops once an already activated node is met. vfraction
1282 * calculation should always continue to the root.
1283 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001284 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001285 if (propagate) {
1286 propagate = !parent->nr_active++;
1287 parent->children_weight += pos->weight;
1288 }
1289 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001290 pos = parent;
1291 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001292
1293 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001294}
1295
1296static void
1297cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001298{
1299 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1300 struct cfq_group *__cfqg;
1301 struct rb_node *n;
1302
1303 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001304 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001305 return;
1306
1307 /*
1308 * Currently put the group at the end. Later implement something
1309 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001310 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001311 */
1312 n = rb_last(&st->rb);
1313 if (n) {
1314 __cfqg = rb_entry_cfqg(n);
1315 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1316 } else
1317 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001318 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001319}
1320
1321static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001322cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1323{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001324 struct cfq_group *pos = cfqg;
1325 bool propagate;
1326
1327 /*
1328 * Undo activation from cfq_group_service_tree_add(). Deactivate
1329 * @cfqg and propagate deactivation upwards.
1330 */
1331 propagate = !--pos->nr_active;
1332 pos->children_weight -= pos->leaf_weight;
1333
1334 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001335 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001336
1337 /* @pos has 0 nr_active at this point */
1338 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001339 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001340
1341 if (!parent)
1342 break;
1343
1344 propagate = !--parent->nr_active;
1345 parent->children_weight -= pos->weight;
1346 pos = parent;
1347 }
1348
1349 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001350 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1351 cfq_rb_erase(&cfqg->rb_node, st);
1352}
1353
1354static void
1355cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001356{
1357 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1358
1359 BUG_ON(cfqg->nr_cfqq < 1);
1360 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001361
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001362 /* If there are other cfq queues under this group, don't delete it */
1363 if (cfqg->nr_cfqq)
1364 return;
1365
Vivek Goyal2868ef72009-12-03 12:59:48 -05001366 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001367 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001368 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001369 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001370}
1371
Justin TerAvest167400d2011-03-12 16:54:00 +01001372static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1373 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001374{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001375 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001376
1377 /*
1378 * Queue got expired before even a single request completed or
1379 * got expired immediately after first request completion.
1380 */
1381 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1382 /*
1383 * Also charge the seek time incurred to the group, otherwise
1384 * if there are mutiple queues in the group, each can dispatch
1385 * a single request on seeky media and cause lots of seek time
1386 * and group will never know it.
1387 */
1388 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1389 1);
1390 } else {
1391 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001392 if (slice_used > cfqq->allocated_slice) {
1393 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001394 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001395 }
1396 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1397 *unaccounted_time += cfqq->slice_start -
1398 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001399 }
1400
Vivek Goyaldae739e2009-12-03 12:59:45 -05001401 return slice_used;
1402}
1403
1404static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001405 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001406{
1407 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001408 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001409 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1410 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001411 unsigned int vfr;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001412
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001413 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001414 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001415
Vivek Goyal02b35082010-08-23 12:23:53 +02001416 if (iops_mode(cfqd))
1417 charge = cfqq->slice_dispatch;
1418 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1419 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001420
Tejun Heo1d3650f2013-01-09 08:05:11 -08001421 /*
1422 * Can't update vdisktime while on service tree and cfqg->vfraction
1423 * is valid only while on it. Cache vfr, leave the service tree,
1424 * update vdisktime and go back on. The re-addition to the tree
1425 * will also update the weights as necessary.
1426 */
1427 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001428 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001429 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001430 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001431
1432 /* This group is being expired. Save the context */
1433 if (time_after(cfqd->workload_expires, jiffies)) {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001434 cfqg->saved_wl_slice = cfqd->workload_expires
Vivek Goyaldae739e2009-12-03 12:59:45 -05001435 - jiffies;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001436 cfqg->saved_wl_type = cfqd->serving_wl_type;
1437 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001438 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001439 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001440
1441 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1442 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001443 cfq_log_cfqq(cfqq->cfqd, cfqq,
1444 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1445 used_sl, cfqq->slice_dispatch, charge,
1446 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001447 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1448 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001449}
1450
Tejun Heof51b8022012-03-05 13:15:05 -08001451/**
1452 * cfq_init_cfqg_base - initialize base part of a cfq_group
1453 * @cfqg: cfq_group to initialize
1454 *
1455 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1456 * is enabled or not.
1457 */
1458static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1459{
1460 struct cfq_rb_root *st;
1461 int i, j;
1462
1463 for_each_cfqg_st(cfqg, i, j, st)
1464 *st = CFQ_RB_ROOT;
1465 RB_CLEAR_NODE(&cfqg->rb_node);
1466
1467 cfqg->ttime.last_end_request = jiffies;
1468}
1469
Vivek Goyal25fb5162009-12-03 12:59:46 -05001470#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07001471static void cfq_pd_init(struct blkcg_gq *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001472{
Tejun Heo03814112012-03-05 13:15:14 -08001473 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001474
Tejun Heof51b8022012-03-05 13:15:05 -08001475 cfq_init_cfqg_base(cfqg);
Tejun Heo3381cb82012-04-01 14:38:44 -07001476 cfqg->weight = blkg->blkcg->cfq_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001477 cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001478}
1479
1480/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001481 * Search for the cfq group current task belongs to. request_queue lock must
1482 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001483 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001484static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001485 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001486{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001487 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001488 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001489
Tejun Heo3c798392012-04-16 13:57:25 -07001490 /* avoid lookup for the common case where there's no blkcg */
1491 if (blkcg == &blkcg_root) {
Tejun Heocd1604f2012-03-05 13:15:06 -08001492 cfqg = cfqd->root_group;
1493 } else {
Tejun Heo3c798392012-04-16 13:57:25 -07001494 struct blkcg_gq *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001495
Tejun Heo3c96cb32012-04-13 13:11:34 -07001496 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -08001497 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001498 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001499 }
1500
Vivek Goyal25fb5162009-12-03 12:59:46 -05001501 return cfqg;
1502}
1503
1504static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1505{
1506 /* Currently, all async queues are mapped to root group */
1507 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001508 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001509
1510 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001511 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001512 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001513}
1514
Tejun Heof95a04a2012-04-16 13:57:26 -07001515static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1516 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001517{
Tejun Heof95a04a2012-04-16 13:57:26 -07001518 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001519
1520 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001521 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001522 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001523}
1524
Tejun Heo3381cb82012-04-01 14:38:44 -07001525static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1526 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001527{
Tejun Heo3c798392012-04-16 13:57:25 -07001528 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1529 cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001530 false);
1531 return 0;
1532}
1533
Tejun Heoe71357e2013-01-09 08:05:10 -08001534static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1535 struct blkg_policy_data *pd, int off)
1536{
1537 struct cfq_group *cfqg = pd_to_cfqg(pd);
1538
1539 if (!cfqg->dev_leaf_weight)
1540 return 0;
1541 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1542}
1543
1544static int cfqg_print_leaf_weight_device(struct cgroup *cgrp,
1545 struct cftype *cft,
1546 struct seq_file *sf)
1547{
1548 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1549 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0,
1550 false);
1551 return 0;
1552}
1553
Tejun Heo3381cb82012-04-01 14:38:44 -07001554static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
1555 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001556{
Tejun Heo3c798392012-04-16 13:57:25 -07001557 seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001558 return 0;
1559}
1560
Tejun Heoe71357e2013-01-09 08:05:10 -08001561static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft,
1562 struct seq_file *sf)
1563{
1564 seq_printf(sf, "%u\n",
1565 cgroup_to_blkcg(cgrp)->cfq_leaf_weight);
1566 return 0;
1567}
1568
1569static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1570 const char *buf, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001571{
Tejun Heo3c798392012-04-16 13:57:25 -07001572 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001573 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001574 struct cfq_group *cfqg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001575 int ret;
1576
Tejun Heo3c798392012-04-16 13:57:25 -07001577 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001578 if (ret)
1579 return ret;
1580
1581 ret = -EINVAL;
Tejun Heo3381cb82012-04-01 14:38:44 -07001582 cfqg = blkg_to_cfqg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -07001583 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001584 if (!is_leaf_weight) {
1585 cfqg->dev_weight = ctx.v;
1586 cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
1587 } else {
1588 cfqg->dev_leaf_weight = ctx.v;
1589 cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
1590 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001591 ret = 0;
1592 }
1593
1594 blkg_conf_finish(&ctx);
1595 return ret;
1596}
1597
Tejun Heoe71357e2013-01-09 08:05:10 -08001598static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1599 const char *buf)
1600{
1601 return __cfqg_set_weight_device(cgrp, cft, buf, false);
1602}
1603
1604static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft,
1605 const char *buf)
1606{
1607 return __cfqg_set_weight_device(cgrp, cft, buf, true);
1608}
1609
1610static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val,
1611 bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001612{
Tejun Heo3c798392012-04-16 13:57:25 -07001613 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
1614 struct blkcg_gq *blkg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001615 struct hlist_node *n;
1616
Tejun Heo3381cb82012-04-01 14:38:44 -07001617 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001618 return -EINVAL;
1619
1620 spin_lock_irq(&blkcg->lock);
Tejun Heoe71357e2013-01-09 08:05:10 -08001621
1622 if (!is_leaf_weight)
1623 blkcg->cfq_weight = val;
1624 else
1625 blkcg->cfq_leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001626
1627 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001628 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001629
Tejun Heoe71357e2013-01-09 08:05:10 -08001630 if (!cfqg)
1631 continue;
1632
1633 if (!is_leaf_weight) {
1634 if (!cfqg->dev_weight)
1635 cfqg->new_weight = blkcg->cfq_weight;
1636 } else {
1637 if (!cfqg->dev_leaf_weight)
1638 cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
1639 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001640 }
1641
1642 spin_unlock_irq(&blkcg->lock);
1643 return 0;
1644}
1645
Tejun Heoe71357e2013-01-09 08:05:10 -08001646static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1647{
1648 return __cfq_set_weight(cgrp, cft, val, false);
1649}
1650
1651static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1652{
1653 return __cfq_set_weight(cgrp, cft, val, true);
1654}
1655
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001656static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
1657 struct seq_file *sf)
1658{
Tejun Heo3c798392012-04-16 13:57:25 -07001659 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001660
Tejun Heo3c798392012-04-16 13:57:25 -07001661 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001662 cft->private, false);
1663 return 0;
1664}
1665
1666static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
1667 struct seq_file *sf)
1668{
Tejun Heo3c798392012-04-16 13:57:25 -07001669 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001670
Tejun Heo3c798392012-04-16 13:57:25 -07001671 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001672 cft->private, true);
1673 return 0;
1674}
1675
Tejun Heo60c2bc22012-04-01 14:38:43 -07001676#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001677static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1678 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001679{
Tejun Heof95a04a2012-04-16 13:57:26 -07001680 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001681 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001682 u64 v = 0;
1683
1684 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001685 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001686 do_div(v, samples);
1687 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001688 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001689 return 0;
1690}
1691
1692/* print avg_queue_size */
Tejun Heo155fead2012-04-01 14:38:44 -07001693static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1694 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001695{
Tejun Heo3c798392012-04-16 13:57:25 -07001696 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001697
Tejun Heo155fead2012-04-01 14:38:44 -07001698 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
Tejun Heo3c798392012-04-16 13:57:25 -07001699 &blkcg_policy_cfq, 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001700 return 0;
1701}
1702#endif /* CONFIG_DEBUG_BLK_CGROUP */
1703
1704static struct cftype cfq_blkcg_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001705 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001706 {
1707 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001708 .flags = CFTYPE_ONLY_ON_ROOT,
1709 .read_seq_string = cfqg_print_leaf_weight_device,
1710 .write_string = cfqg_set_leaf_weight_device,
1711 .max_write_len = 256,
1712 },
1713 {
1714 .name = "weight",
1715 .flags = CFTYPE_ONLY_ON_ROOT,
1716 .read_seq_string = cfq_print_leaf_weight,
1717 .write_u64 = cfq_set_leaf_weight,
1718 },
1719
1720 /* no such mapping necessary for !roots */
1721 {
1722 .name = "weight_device",
1723 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo3381cb82012-04-01 14:38:44 -07001724 .read_seq_string = cfqg_print_weight_device,
1725 .write_string = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001726 .max_write_len = 256,
1727 },
1728 {
1729 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001730 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001731 .read_seq_string = cfq_print_weight,
1732 .write_u64 = cfq_set_weight,
1733 },
1734
1735 {
1736 .name = "leaf_weight_device",
Tejun Heoe71357e2013-01-09 08:05:10 -08001737 .read_seq_string = cfqg_print_leaf_weight_device,
1738 .write_string = cfqg_set_leaf_weight_device,
1739 .max_write_len = 256,
1740 },
1741 {
1742 .name = "leaf_weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001743 .read_seq_string = cfq_print_leaf_weight,
1744 .write_u64 = cfq_set_leaf_weight,
1745 },
1746
Tejun Heo60c2bc22012-04-01 14:38:43 -07001747 {
1748 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001749 .private = offsetof(struct cfq_group, stats.time),
1750 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001751 },
1752 {
1753 .name = "sectors",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001754 .private = offsetof(struct cfq_group, stats.sectors),
1755 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001756 },
1757 {
1758 .name = "io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001759 .private = offsetof(struct cfq_group, stats.service_bytes),
1760 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001761 },
1762 {
1763 .name = "io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001764 .private = offsetof(struct cfq_group, stats.serviced),
1765 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001766 },
1767 {
1768 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001769 .private = offsetof(struct cfq_group, stats.service_time),
1770 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001771 },
1772 {
1773 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001774 .private = offsetof(struct cfq_group, stats.wait_time),
1775 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001776 },
1777 {
1778 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001779 .private = offsetof(struct cfq_group, stats.merged),
1780 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001781 },
1782 {
1783 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001784 .private = offsetof(struct cfq_group, stats.queued),
1785 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001786 },
1787#ifdef CONFIG_DEBUG_BLK_CGROUP
1788 {
1789 .name = "avg_queue_size",
Tejun Heo155fead2012-04-01 14:38:44 -07001790 .read_seq_string = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001791 },
1792 {
1793 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001794 .private = offsetof(struct cfq_group, stats.group_wait_time),
1795 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001796 },
1797 {
1798 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001799 .private = offsetof(struct cfq_group, stats.idle_time),
1800 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001801 },
1802 {
1803 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001804 .private = offsetof(struct cfq_group, stats.empty_time),
1805 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001806 },
1807 {
1808 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001809 .private = offsetof(struct cfq_group, stats.dequeue),
1810 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001811 },
1812 {
1813 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001814 .private = offsetof(struct cfq_group, stats.unaccounted_time),
1815 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001816 },
1817#endif /* CONFIG_DEBUG_BLK_CGROUP */
1818 { } /* terminate */
1819};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001820#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001821static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001822 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001823{
Tejun Heof51b8022012-03-05 13:15:05 -08001824 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001825}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001826
Vivek Goyal25fb5162009-12-03 12:59:46 -05001827static inline void
1828cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1829 cfqq->cfqg = cfqg;
1830}
1831
1832#endif /* GROUP_IOSCHED */
1833
Jens Axboe498d3aa22007-04-26 12:54:48 +02001834/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001835 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001836 * requests waiting to be processed. It is sorted in the order that
1837 * we will service the queues.
1838 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001839static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001840 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001841{
Jens Axboe08717142008-01-28 11:38:15 +01001842 struct rb_node **p, *parent;
1843 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001844 unsigned long rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001845 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001846 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001847 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001848
Vivek Goyal34b98d02012-10-03 16:56:58 -04001849 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001850 if (cfq_class_idle(cfqq)) {
1851 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001852 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001853 if (parent && parent != &cfqq->rb_node) {
1854 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1855 rb_key += __cfqq->rb_key;
1856 } else
1857 rb_key += jiffies;
1858 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001859 /*
1860 * Get our rb key offset. Subtract any residual slice
1861 * value carried from last service. A negative resid
1862 * count indicates slice overrun, and this should position
1863 * the next service time further away in the tree.
1864 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001865 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001866 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001867 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001868 } else {
1869 rb_key = -HZ;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001870 __cfqq = cfq_rb_first(st);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001871 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1872 }
Jens Axboed9e76202007-04-20 14:27:50 +02001873
1874 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001875 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001876 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001877 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001878 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04001879 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02001880 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001881
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001882 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1883 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001884 }
Jens Axboed9e76202007-04-20 14:27:50 +02001885
Jens Axboe498d3aa22007-04-26 12:54:48 +02001886 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001887 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001888 cfqq->service_tree = st;
1889 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001890 while (*p) {
1891 parent = *p;
1892 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1893
Jens Axboe0c534e02007-04-18 20:01:57 +02001894 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001895 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001896 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001897 if (time_before(rb_key, __cfqq->rb_key))
Vivek Goyal1f23f122012-10-03 16:57:00 -04001898 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001899 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04001900 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001901 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001902 }
Jens Axboed9e76202007-04-20 14:27:50 +02001903 }
1904
Jens Axboecc09e292007-04-26 12:53:50 +02001905 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04001906 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001907
Jens Axboed9e76202007-04-20 14:27:50 +02001908 cfqq->rb_key = rb_key;
1909 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04001910 rb_insert_color(&cfqq->rb_node, &st->rb);
1911 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001912 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001913 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001914 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Jens Axboea36e71f2009-04-15 12:15:11 +02001917static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001918cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1919 sector_t sector, struct rb_node **ret_parent,
1920 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001921{
Jens Axboea36e71f2009-04-15 12:15:11 +02001922 struct rb_node **p, *parent;
1923 struct cfq_queue *cfqq = NULL;
1924
1925 parent = NULL;
1926 p = &root->rb_node;
1927 while (*p) {
1928 struct rb_node **n;
1929
1930 parent = *p;
1931 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1932
1933 /*
1934 * Sort strictly based on sector. Smallest to the left,
1935 * largest to the right.
1936 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001937 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001938 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001939 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001940 n = &(*p)->rb_left;
1941 else
1942 break;
1943 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001944 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001945 }
1946
1947 *ret_parent = parent;
1948 if (rb_link)
1949 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001950 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001951}
1952
1953static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1954{
Jens Axboea36e71f2009-04-15 12:15:11 +02001955 struct rb_node **p, *parent;
1956 struct cfq_queue *__cfqq;
1957
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001958 if (cfqq->p_root) {
1959 rb_erase(&cfqq->p_node, cfqq->p_root);
1960 cfqq->p_root = NULL;
1961 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001962
1963 if (cfq_class_idle(cfqq))
1964 return;
1965 if (!cfqq->next_rq)
1966 return;
1967
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001968 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001969 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1970 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001971 if (!__cfqq) {
1972 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001973 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1974 } else
1975 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001976}
1977
Jens Axboe498d3aa22007-04-26 12:54:48 +02001978/*
1979 * Update cfqq's position in the service tree.
1980 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001981static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001982{
Jens Axboe6d048f52007-04-25 12:44:27 +02001983 /*
1984 * Resorting requires the cfqq to be on the RR list already.
1985 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001986 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001987 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001988 cfq_prio_tree_add(cfqd, cfqq);
1989 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992/*
1993 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001994 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Jens Axboefebffd62008-01-28 13:19:43 +01001996static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Jens Axboe7b679132008-05-30 12:23:07 +02001998 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001999 BUG_ON(cfq_cfqq_on_rr(cfqq));
2000 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002002 if (cfq_cfqq_sync(cfqq))
2003 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Jens Axboeedd75ff2007-04-19 12:03:34 +02002005 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Jens Axboe498d3aa22007-04-26 12:54:48 +02002008/*
2009 * Called when the cfqq no longer has requests pending, remove it from
2010 * the service tree.
2011 */
Jens Axboefebffd62008-01-28 13:19:43 +01002012static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Jens Axboe7b679132008-05-30 12:23:07 +02002014 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002015 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2016 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002018 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2019 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2020 cfqq->service_tree = NULL;
2021 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002022 if (cfqq->p_root) {
2023 rb_erase(&cfqq->p_node, cfqq->p_root);
2024 cfqq->p_root = NULL;
2025 }
Jens Axboed9e76202007-04-20 14:27:50 +02002026
Justin TerAvest8184f932011-03-17 16:12:36 +01002027 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 BUG_ON(!cfqd->busy_queues);
2029 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002030 if (cfq_cfqq_sync(cfqq))
2031 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/*
2035 * rb tree support functions
2036 */
Jens Axboefebffd62008-01-28 13:19:43 +01002037static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Jens Axboe5e705372006-07-13 12:39:25 +02002039 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002040 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Jens Axboeb4878f22005-10-20 16:42:29 +02002042 BUG_ON(!cfqq->queued[sync]);
2043 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jens Axboe5e705372006-07-13 12:39:25 +02002045 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Vivek Goyalf04a6422009-12-03 12:59:40 -05002047 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2048 /*
2049 * Queue will be deleted from service tree when we actually
2050 * expire it later. Right now just remove it from prio tree
2051 * as it is empty.
2052 */
2053 if (cfqq->p_root) {
2054 rb_erase(&cfqq->p_node, cfqq->p_root);
2055 cfqq->p_root = NULL;
2056 }
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
Jens Axboe5e705372006-07-13 12:39:25 +02002060static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Jens Axboe5e705372006-07-13 12:39:25 +02002062 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002064 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Jens Axboe5380a102006-07-13 12:37:56 +02002066 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Jeff Moyer796d5112011-06-02 21:19:05 +02002068 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002069
2070 if (!cfq_cfqq_on_rr(cfqq))
2071 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002072
2073 /*
2074 * check if this request is a better next-serve candidate
2075 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002076 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002077 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002078
2079 /*
2080 * adjust priority tree position, if ->next_rq changes
2081 */
2082 if (prev != cfqq->next_rq)
2083 cfq_prio_tree_add(cfqd, cfqq);
2084
Jens Axboe5044eed2007-04-25 11:53:48 +02002085 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Jens Axboefebffd62008-01-28 13:19:43 +01002088static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Jens Axboe5380a102006-07-13 12:37:56 +02002090 elv_rb_del(&cfqq->sort_list, rq);
2091 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo155fead2012-04-01 14:38:44 -07002092 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002093 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002094 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2095 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Jens Axboe206dc692006-03-28 13:03:44 +02002098static struct request *
2099cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Jens Axboe206dc692006-03-28 13:03:44 +02002101 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002102 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002103 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Jens Axboe4ac845a2008-01-24 08:44:49 +01002105 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002106 if (!cic)
2107 return NULL;
2108
2109 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02002110 if (cfqq) {
2111 sector_t sector = bio->bi_sector + bio_sectors(bio);
2112
Jens Axboe21183b02006-07-13 12:33:14 +02002113 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return NULL;
2117}
2118
Jens Axboe165125e2007-07-24 09:28:11 +02002119static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002120{
2121 struct cfq_data *cfqd = q->elevator->elevator_data;
2122
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002123 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002124 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002125 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002126
Tejun Heo5b936292009-05-07 22:24:38 +09002127 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002128}
2129
Jens Axboe165125e2007-07-24 09:28:11 +02002130static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Jens Axboe22e2c502005-06-27 10:55:12 +02002132 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002134 WARN_ON(!cfqd->rq_in_driver);
2135 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002136 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002137 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Jens Axboeb4878f22005-10-20 16:42:29 +02002140static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Jens Axboe5e705372006-07-13 12:39:25 +02002142 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002143
Jens Axboe5e705372006-07-13 12:39:25 +02002144 if (cfqq->next_rq == rq)
2145 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Jens Axboeb4878f22005-10-20 16:42:29 +02002147 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002148 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002149
Aaron Carroll45333d52008-08-26 15:52:36 +02002150 cfqq->cfqd->rq_queued--;
Tejun Heo155fead2012-04-01 14:38:44 -07002151 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002152 if (rq->cmd_flags & REQ_PRIO) {
2153 WARN_ON(!cfqq->prio_pending);
2154 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
Jens Axboe165125e2007-07-24 09:28:11 +02002158static int cfq_merge(struct request_queue *q, struct request **req,
2159 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 struct cfq_data *cfqd = q->elevator->elevator_data;
2162 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jens Axboe206dc692006-03-28 13:03:44 +02002164 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002165 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002166 *req = __rq;
2167 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
2170 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Jens Axboe165125e2007-07-24 09:28:11 +02002173static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02002174 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Jens Axboe21183b02006-07-13 12:33:14 +02002176 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002177 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Jens Axboe5e705372006-07-13 12:39:25 +02002179 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Divyesh Shah812d4022010-04-08 21:14:23 -07002183static void cfq_bio_merged(struct request_queue *q, struct request *req,
2184 struct bio *bio)
2185{
Tejun Heo155fead2012-04-01 14:38:44 -07002186 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
Divyesh Shah812d4022010-04-08 21:14:23 -07002187}
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189static void
Jens Axboe165125e2007-07-24 09:28:11 +02002190cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct request *next)
2192{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002193 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002194 struct cfq_data *cfqd = q->elevator->elevator_data;
2195
Jens Axboe22e2c502005-06-27 10:55:12 +02002196 /*
2197 * reposition in fifo if next is older than rq
2198 */
2199 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002200 time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
2201 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002202 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02002203 rq_set_fifo_time(rq, rq_fifo_time(next));
2204 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002205
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002206 if (cfqq->next_rq == next)
2207 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002208 cfq_remove_request(next);
Tejun Heo155fead2012-04-01 14:38:44 -07002209 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002210
2211 cfqq = RQ_CFQQ(next);
2212 /*
2213 * all requests of this queue are merged to other queues, delete it
2214 * from the service tree. If it's the active_queue,
2215 * cfq_dispatch_requests() will choose to expire it or do idle
2216 */
2217 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2218 cfqq != cfqd->active_queue)
2219 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002220}
2221
Jens Axboe165125e2007-07-24 09:28:11 +02002222static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002223 struct bio *bio)
2224{
2225 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002226 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002227 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002228
2229 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002230 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002231 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002232 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002233 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002234
2235 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002236 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002237 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002238 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002239 cic = cfq_cic_lookup(cfqd, current->io_context);
2240 if (!cic)
2241 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002242
Vasily Tarasov91fac312007-04-25 12:29:51 +02002243 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002244 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002245}
2246
Divyesh Shah812df482010-04-08 21:15:35 -07002247static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2248{
2249 del_timer(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002250 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002251}
2252
Jens Axboefebffd62008-01-28 13:19:43 +01002253static void __cfq_set_active_queue(struct cfq_data *cfqd,
2254 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002255{
2256 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002257 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002258 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002259 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002260 cfqq->slice_start = 0;
2261 cfqq->dispatch_start = jiffies;
2262 cfqq->allocated_slice = 0;
2263 cfqq->slice_end = 0;
2264 cfqq->slice_dispatch = 0;
2265 cfqq->nr_sectors = 0;
2266
2267 cfq_clear_cfqq_wait_request(cfqq);
2268 cfq_clear_cfqq_must_dispatch(cfqq);
2269 cfq_clear_cfqq_must_alloc_slice(cfqq);
2270 cfq_clear_cfqq_fifo_expire(cfqq);
2271 cfq_mark_cfqq_slice_new(cfqq);
2272
2273 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002274 }
2275
2276 cfqd->active_queue = cfqq;
2277}
2278
2279/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002280 * current cfqq expired its slice (or was too idle), select new one
2281 */
2282static void
2283__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002284 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002285{
Jens Axboe7b679132008-05-30 12:23:07 +02002286 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2287
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002288 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002289 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002290
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002291 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002292 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002293
2294 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002295 * If this cfqq is shared between multiple processes, check to
2296 * make sure that those processes are still issuing I/Os within
2297 * the mean seek distance. If not, it may be time to break the
2298 * queues apart again.
2299 */
2300 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2301 cfq_mark_cfqq_split_coop(cfqq);
2302
2303 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002304 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002305 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002306 if (timed_out) {
2307 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002308 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002309 else
2310 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002311 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2312 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002313
Vivek Goyale5ff0822010-04-26 19:25:11 +02002314 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002315
Vivek Goyalf04a6422009-12-03 12:59:40 -05002316 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2317 cfq_del_cfqq_rr(cfqd, cfqq);
2318
Jens Axboeedd75ff2007-04-19 12:03:34 +02002319 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002320
2321 if (cfqq == cfqd->active_queue)
2322 cfqd->active_queue = NULL;
2323
2324 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002325 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002326 cfqd->active_cic = NULL;
2327 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002328}
2329
Vivek Goyale5ff0822010-04-26 19:25:11 +02002330static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002331{
2332 struct cfq_queue *cfqq = cfqd->active_queue;
2333
2334 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002335 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002336}
2337
Jens Axboe498d3aa22007-04-26 12:54:48 +02002338/*
2339 * Get next queue for service. Unless we have a queue preemption,
2340 * we'll simply select the first cfqq in the service tree.
2341 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002342static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002343{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002344 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2345 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002346
Vivek Goyalf04a6422009-12-03 12:59:40 -05002347 if (!cfqd->rq_queued)
2348 return NULL;
2349
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002350 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002351 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002352 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002353 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002354 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002355 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002356}
2357
Vivek Goyalf04a6422009-12-03 12:59:40 -05002358static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2359{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002360 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002361 struct cfq_queue *cfqq;
2362 int i, j;
2363 struct cfq_rb_root *st;
2364
2365 if (!cfqd->rq_queued)
2366 return NULL;
2367
Vivek Goyal25fb5162009-12-03 12:59:46 -05002368 cfqg = cfq_get_next_cfqg(cfqd);
2369 if (!cfqg)
2370 return NULL;
2371
Vivek Goyalf04a6422009-12-03 12:59:40 -05002372 for_each_cfqg_st(cfqg, i, j, st)
2373 if ((cfqq = cfq_rb_first(st)) != NULL)
2374 return cfqq;
2375 return NULL;
2376}
2377
Jens Axboe498d3aa22007-04-26 12:54:48 +02002378/*
2379 * Get and set a new active queue for service.
2380 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002381static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2382 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002383{
Jens Axboee00ef792009-11-04 08:54:55 +01002384 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002385 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002386
Jens Axboe22e2c502005-06-27 10:55:12 +02002387 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002388 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002389}
2390
Jens Axboed9e76202007-04-20 14:27:50 +02002391static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2392 struct request *rq)
2393{
Tejun Heo83096eb2009-05-07 22:24:39 +09002394 if (blk_rq_pos(rq) >= cfqd->last_position)
2395 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002396 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002397 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002398}
2399
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002400static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002401 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002402{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002403 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002404}
2405
Jens Axboea36e71f2009-04-15 12:15:11 +02002406static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2407 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002408{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002409 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002410 struct rb_node *parent, *node;
2411 struct cfq_queue *__cfqq;
2412 sector_t sector = cfqd->last_position;
2413
2414 if (RB_EMPTY_ROOT(root))
2415 return NULL;
2416
2417 /*
2418 * First, if we find a request starting at the end of the last
2419 * request, choose it.
2420 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002421 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002422 if (__cfqq)
2423 return __cfqq;
2424
2425 /*
2426 * If the exact sector wasn't found, the parent of the NULL leaf
2427 * will contain the closest sector.
2428 */
2429 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002430 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002431 return __cfqq;
2432
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002433 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002434 node = rb_next(&__cfqq->p_node);
2435 else
2436 node = rb_prev(&__cfqq->p_node);
2437 if (!node)
2438 return NULL;
2439
2440 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002441 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002442 return __cfqq;
2443
2444 return NULL;
2445}
2446
2447/*
2448 * cfqd - obvious
2449 * cur_cfqq - passed in so that we don't decide that the current queue is
2450 * closely cooperating with itself.
2451 *
2452 * So, basically we're assuming that that cur_cfqq has dispatched at least
2453 * one request, and that cfqd->last_position reflects a position on the disk
2454 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2455 * assumption.
2456 */
2457static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002458 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002459{
2460 struct cfq_queue *cfqq;
2461
Divyesh Shah39c01b22010-03-25 15:45:57 +01002462 if (cfq_class_idle(cur_cfqq))
2463 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002464 if (!cfq_cfqq_sync(cur_cfqq))
2465 return NULL;
2466 if (CFQQ_SEEKY(cur_cfqq))
2467 return NULL;
2468
Jens Axboea36e71f2009-04-15 12:15:11 +02002469 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002470 * Don't search priority tree if it's the only queue in the group.
2471 */
2472 if (cur_cfqq->cfqg->nr_cfqq == 1)
2473 return NULL;
2474
2475 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002476 * We should notice if some of the queues are cooperating, eg
2477 * working closely on the same area of the disk. In that case,
2478 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002479 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002480 cfqq = cfqq_close(cfqd, cur_cfqq);
2481 if (!cfqq)
2482 return NULL;
2483
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002484 /* If new queue belongs to different cfq_group, don't choose it */
2485 if (cur_cfqq->cfqg != cfqq->cfqg)
2486 return NULL;
2487
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002488 /*
2489 * It only makes sense to merge sync queues.
2490 */
2491 if (!cfq_cfqq_sync(cfqq))
2492 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002493 if (CFQQ_SEEKY(cfqq))
2494 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002495
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002496 /*
2497 * Do not merge queues of different priority classes
2498 */
2499 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2500 return NULL;
2501
Jens Axboea36e71f2009-04-15 12:15:11 +02002502 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002503}
2504
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002505/*
2506 * Determine whether we should enforce idle window for this queue.
2507 */
2508
2509static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2510{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002511 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002512 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002513
Vivek Goyal34b98d02012-10-03 16:56:58 -04002514 BUG_ON(!st);
2515 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002516
Vivek Goyalb6508c12010-08-23 12:23:33 +02002517 if (!cfqd->cfq_slice_idle)
2518 return false;
2519
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002520 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002521 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002522 return false;
2523
2524 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002525 if (cfq_cfqq_idle_window(cfqq) &&
2526 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002527 return true;
2528
2529 /*
2530 * Otherwise, we do only if they are the last ones
2531 * in their service tree.
2532 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002533 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2534 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002535 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002536 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002537 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002538}
2539
Jens Axboe6d048f52007-04-25 12:44:27 +02002540static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002541{
Jens Axboe17926692007-01-19 11:59:30 +11002542 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002543 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002544 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002545
Jens Axboea68bbdd2008-09-24 13:03:33 +02002546 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002547 * SSD device without seek penalty, disable idling. But only do so
2548 * for devices that support queuing, otherwise we still have a problem
2549 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002550 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002551 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002552 return;
2553
Jens Axboedd67d052006-06-21 09:36:18 +02002554 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002555 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002556
2557 /*
2558 * idle is disabled, either manually or by past process history
2559 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002560 if (!cfq_should_idle(cfqd, cfqq)) {
2561 /* no queue idling. Check for group idling */
2562 if (cfqd->cfq_group_idle)
2563 group_idle = cfqd->cfq_group_idle;
2564 else
2565 return;
2566 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002567
Jens Axboe22e2c502005-06-27 10:55:12 +02002568 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002569 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002570 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002571 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002572 return;
2573
2574 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002575 * task has exited, don't wait
2576 */
Jens Axboe206dc692006-03-28 13:03:44 +02002577 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002578 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002579 return;
2580
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002581 /*
2582 * If our average think time is larger than the remaining time
2583 * slice, then don't idle. This avoids overrunning the allotted
2584 * time slice.
2585 */
Shaohua Li383cd722011-07-12 14:24:35 +02002586 if (sample_valid(cic->ttime.ttime_samples) &&
2587 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002588 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002589 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002590 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002591 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002592
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002593 /* There are other queues in the group, don't do group idle */
2594 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2595 return;
2596
Jens Axboe3b181522005-06-27 10:56:24 +02002597 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002598
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002599 if (group_idle)
2600 sl = cfqd->cfq_group_idle;
2601 else
2602 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002603
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002604 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo155fead2012-04-01 14:38:44 -07002605 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002606 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2607 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Jens Axboe498d3aa22007-04-26 12:54:48 +02002610/*
2611 * Move request from internal lists to the request queue dispatch list.
2612 */
Jens Axboe165125e2007-07-24 09:28:11 +02002613static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002615 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002616 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002617
Jens Axboe7b679132008-05-30 12:23:07 +02002618 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2619
Jeff Moyer06d21882009-09-11 17:08:59 +02002620 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002621 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002622 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002623 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002624 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002625
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002626 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002627 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002628 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
2631/*
2632 * return expired entry, or NULL to just start from scratch in rbtree
2633 */
Jens Axboefebffd62008-01-28 13:19:43 +01002634static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Jens Axboe30996f42009-10-05 11:03:39 +02002636 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Jens Axboe3b181522005-06-27 10:56:24 +02002638 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002640
2641 cfq_mark_cfqq_fifo_expire(cfqq);
2642
Jens Axboe89850f72006-07-22 16:48:31 +02002643 if (list_empty(&cfqq->fifo))
2644 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Jens Axboe89850f72006-07-22 16:48:31 +02002646 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002647 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002648 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Jens Axboe30996f42009-10-05 11:03:39 +02002650 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002651 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Jens Axboe22e2c502005-06-27 10:55:12 +02002654static inline int
2655cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2656{
2657 const int base_rq = cfqd->cfq_slice_async_rq;
2658
2659 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2660
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002661 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002662}
2663
2664/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002665 * Must be called with the queue_lock held.
2666 */
2667static int cfqq_process_refs(struct cfq_queue *cfqq)
2668{
2669 int process_refs, io_refs;
2670
2671 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002672 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002673 BUG_ON(process_refs < 0);
2674 return process_refs;
2675}
2676
2677static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2678{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002679 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002680 struct cfq_queue *__cfqq;
2681
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002682 /*
2683 * If there are no process references on the new_cfqq, then it is
2684 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2685 * chain may have dropped their last reference (not just their
2686 * last process reference).
2687 */
2688 if (!cfqq_process_refs(new_cfqq))
2689 return;
2690
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002691 /* Avoid a circular list and skip interim queue merges */
2692 while ((__cfqq = new_cfqq->new_cfqq)) {
2693 if (__cfqq == cfqq)
2694 return;
2695 new_cfqq = __cfqq;
2696 }
2697
2698 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002699 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002700 /*
2701 * If the process for the cfqq has gone away, there is no
2702 * sense in merging the queues.
2703 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002704 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002705 return;
2706
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002707 /*
2708 * Merge in the direction of the lesser amount of work.
2709 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002710 if (new_process_refs >= process_refs) {
2711 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002712 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002713 } else {
2714 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002715 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002716 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002717}
2718
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002719static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002720 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002721{
2722 struct cfq_queue *queue;
2723 int i;
2724 bool key_valid = false;
2725 unsigned long lowest_key = 0;
2726 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2727
Vivek Goyal65b32a52009-12-16 17:52:59 -05002728 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2729 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002730 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002731 if (queue &&
2732 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2733 lowest_key = queue->rb_key;
2734 cur_best = i;
2735 key_valid = true;
2736 }
2737 }
2738
2739 return cur_best;
2740}
2741
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002742static void
2743choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002744{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002745 unsigned slice;
2746 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002747 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002748 unsigned group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002749 enum wl_class_t original_class = cfqd->serving_wl_class;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002750
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002751 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002752 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002753 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002754 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002755 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002756 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002757 cfqd->serving_wl_class = IDLE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002758 cfqd->workload_expires = jiffies + 1;
2759 return;
2760 }
2761
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002762 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002763 goto new_workload;
2764
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002765 /*
2766 * For RT and BE, we have to choose also the type
2767 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2768 * expiration time
2769 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002770 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002771 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002772
2773 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002774 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002775 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002776 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002777 return;
2778
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002779new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002780 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002781 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002782 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002783 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002784 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002785
2786 /*
2787 * the workload slice is computed as a fraction of target latency
2788 * proportional to the number of queues in that workload, over
2789 * all the queues in the same priority class
2790 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002791 group_slice = cfq_group_slice(cfqd, cfqg);
2792
2793 slice = group_slice * count /
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002794 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
2795 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002796 cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002797
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002798 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002799 unsigned int tmp;
2800
2801 /*
2802 * Async queues are currently system wide. Just taking
2803 * proportion of queues with-in same group will lead to higher
2804 * async ratio system wide as generally root group is going
2805 * to have higher weight. A more accurate thing would be to
2806 * calculate system wide asnc/sync ratio.
2807 */
Tao Ma5bf14c02012-04-01 14:33:39 -07002808 tmp = cfqd->cfq_target_latency *
2809 cfqg_busy_async_queues(cfqd, cfqg);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002810 tmp = tmp/cfqd->busy_queues;
2811 slice = min_t(unsigned, slice, tmp);
2812
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002813 /* async workload slice is scaled down according to
2814 * the sync/async slice ratio. */
2815 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002816 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002817 /* sync workload slice is at least 2 * cfq_slice_idle */
2818 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2819
2820 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002821 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002822 cfqd->workload_expires = jiffies + slice;
2823}
2824
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002825static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2826{
2827 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002828 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002829
2830 if (RB_EMPTY_ROOT(&st->rb))
2831 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002832 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002833 update_min_vdisktime(st);
2834 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002835}
2836
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002837static void cfq_choose_cfqg(struct cfq_data *cfqd)
2838{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002839 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2840
2841 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002842
2843 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002844 if (cfqg->saved_wl_slice) {
2845 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
2846 cfqd->serving_wl_type = cfqg->saved_wl_type;
2847 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002848 } else
2849 cfqd->workload_expires = jiffies - 1;
2850
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002851 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002852}
2853
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002854/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002855 * Select a queue for service. If we have a current active queue,
2856 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002857 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002858static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002859{
Jens Axboea36e71f2009-04-15 12:15:11 +02002860 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002861
2862 cfqq = cfqd->active_queue;
2863 if (!cfqq)
2864 goto new_queue;
2865
Vivek Goyalf04a6422009-12-03 12:59:40 -05002866 if (!cfqd->rq_queued)
2867 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002868
2869 /*
2870 * We were waiting for group to get backlogged. Expire the queue
2871 */
2872 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2873 goto expire;
2874
Jens Axboe22e2c502005-06-27 10:55:12 +02002875 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002876 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002877 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002878 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2879 /*
2880 * If slice had not expired at the completion of last request
2881 * we might not have turned on wait_busy flag. Don't expire
2882 * the queue yet. Allow the group to get backlogged.
2883 *
2884 * The very fact that we have used the slice, that means we
2885 * have been idling all along on this queue and it should be
2886 * ok to wait for this request to complete.
2887 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002888 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2889 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2890 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002891 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002892 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002893 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002894 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002895
2896 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002897 * The active queue has requests and isn't expired, allow it to
2898 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002899 */
Jens Axboedd67d052006-06-21 09:36:18 +02002900 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002901 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002902
2903 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002904 * If another queue has a request waiting within our mean seek
2905 * distance, let it run. The expire code will check for close
2906 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002907 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002908 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002909 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002910 if (new_cfqq) {
2911 if (!cfqq->new_cfqq)
2912 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002913 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002914 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002915
2916 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002917 * No requests pending. If the active queue still has requests in
2918 * flight or is idling for a new request, allow either of these
2919 * conditions to happen (or time out) before selecting a new queue.
2920 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002921 if (timer_pending(&cfqd->idle_slice_timer)) {
2922 cfqq = NULL;
2923 goto keep_queue;
2924 }
2925
Shaohua Li8e1ac662010-11-08 15:01:04 +01002926 /*
2927 * This is a deep seek queue, but the device is much faster than
2928 * the queue can deliver, don't idle
2929 **/
2930 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2931 (cfq_cfqq_slice_new(cfqq) ||
2932 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2933 cfq_clear_cfqq_deep(cfqq);
2934 cfq_clear_cfqq_idle_window(cfqq);
2935 }
2936
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002937 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2938 cfqq = NULL;
2939 goto keep_queue;
2940 }
2941
2942 /*
2943 * If group idle is enabled and there are requests dispatched from
2944 * this group, wait for requests to complete.
2945 */
2946check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002947 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2948 cfqq->cfqg->dispatched &&
2949 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002950 cfqq = NULL;
2951 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002952 }
2953
Jens Axboe3b181522005-06-27 10:56:24 +02002954expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002955 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002956new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002957 /*
2958 * Current queue expired. Check if we have to switch to a new
2959 * service tree
2960 */
2961 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002962 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002963
Jens Axboea36e71f2009-04-15 12:15:11 +02002964 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002965keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002966 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002967}
2968
Jens Axboefebffd62008-01-28 13:19:43 +01002969static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002970{
2971 int dispatched = 0;
2972
2973 while (cfqq->next_rq) {
2974 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2975 dispatched++;
2976 }
2977
2978 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002979
2980 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002981 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002982 return dispatched;
2983}
2984
Jens Axboe498d3aa22007-04-26 12:54:48 +02002985/*
2986 * Drain our current requests. Used for barriers and when switching
2987 * io schedulers on-the-fly.
2988 */
Jens Axboed9e76202007-04-20 14:27:50 +02002989static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002990{
Jens Axboe08717142008-01-28 11:38:15 +01002991 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002992 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002993
Divyesh Shah3440c492010-04-09 09:29:57 +02002994 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002995 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002996 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2997 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002998 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002999 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003000
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003001 BUG_ON(cfqd->busy_queues);
3002
Jeff Moyer69237152009-06-12 15:29:30 +02003003 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003004 return dispatched;
3005}
3006
Shaohua Liabc3c742010-03-01 09:20:54 +01003007static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3008 struct cfq_queue *cfqq)
3009{
3010 /* the queue hasn't finished any request, can't estimate */
3011 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003012 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003013 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
3014 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01003015 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003016
Shaohua Lic1e44752010-11-08 15:01:02 +01003017 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003018}
3019
Jens Axboe0b182d62009-10-06 20:49:37 +02003020static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003021{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003022 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Jens Axboe2f5cb732009-04-07 08:51:19 +02003024 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003025 * Drain async requests before we start sync IO
3026 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003027 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003028 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003029
3030 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003031 * If this is an async queue and we have sync IO in flight, let it wait
3032 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003033 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003034 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003035
Shaohua Liabc3c742010-03-01 09:20:54 +01003036 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003037 if (cfq_class_idle(cfqq))
3038 max_dispatch = 1;
3039
3040 /*
3041 * Does this cfqq already have too much IO in flight?
3042 */
3043 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003044 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003045 /*
3046 * idle queue must always only have a single IO in flight
3047 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003048 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003049 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003050
Jens Axboe2f5cb732009-04-07 08:51:19 +02003051 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003052 * If there is only one sync queue
3053 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003054 * queue no dispatch limit. The reason is a sync queue can
3055 * preempt async queue, limiting the sync queue doesn't make
3056 * sense. This is useful for aiostress test.
3057 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003058 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3059 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003060
3061 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003062 * We have other queues, don't allow more IO from this one
3063 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003064 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3065 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003066 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003067
Jens Axboe2f5cb732009-04-07 08:51:19 +02003068 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003069 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003070 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003071 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003072 max_dispatch = -1;
3073 else
3074 /*
3075 * Normally we start throttling cfqq when cfq_quantum/2
3076 * requests have been dispatched. But we can drive
3077 * deeper queue depths at the beginning of slice
3078 * subjected to upper limit of cfq_quantum.
3079 * */
3080 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003081 }
3082
3083 /*
3084 * Async queues must wait a bit before being allowed dispatch.
3085 * We also ramp up the dispatch depth gradually for async IO,
3086 * based on the last sync IO we serviced
3087 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003088 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003089 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003090 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003091
Jens Axboe61f0c1d2009-10-03 19:46:03 +02003092 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02003093 if (!depth && !cfqq->dispatched)
3094 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003095 if (depth < max_dispatch)
3096 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098
Jens Axboe0b182d62009-10-06 20:49:37 +02003099 /*
3100 * If we're below the current max, allow a dispatch
3101 */
3102 return cfqq->dispatched < max_dispatch;
3103}
3104
3105/*
3106 * Dispatch a request from cfqq, moving them to the request queue
3107 * dispatch list.
3108 */
3109static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3110{
3111 struct request *rq;
3112
3113 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3114
3115 if (!cfq_may_dispatch(cfqd, cfqq))
3116 return false;
3117
3118 /*
3119 * follow expired path, else get first next available
3120 */
3121 rq = cfq_check_fifo(cfqq);
3122 if (!rq)
3123 rq = cfqq->next_rq;
3124
3125 /*
3126 * insert request into driver dispatch list
3127 */
3128 cfq_dispatch_insert(cfqd->queue, rq);
3129
3130 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003131 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003132
Tejun Heoc5869802011-12-14 00:33:41 +01003133 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003134 cfqd->active_cic = cic;
3135 }
3136
3137 return true;
3138}
3139
3140/*
3141 * Find the cfqq that we need to service and move a request from that to the
3142 * dispatch list
3143 */
3144static int cfq_dispatch_requests(struct request_queue *q, int force)
3145{
3146 struct cfq_data *cfqd = q->elevator->elevator_data;
3147 struct cfq_queue *cfqq;
3148
3149 if (!cfqd->busy_queues)
3150 return 0;
3151
3152 if (unlikely(force))
3153 return cfq_forced_dispatch(cfqd);
3154
3155 cfqq = cfq_select_queue(cfqd);
3156 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003157 return 0;
3158
Jens Axboe2f5cb732009-04-07 08:51:19 +02003159 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003160 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003161 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003162 if (!cfq_dispatch_request(cfqd, cfqq))
3163 return 0;
3164
Jens Axboe2f5cb732009-04-07 08:51:19 +02003165 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003166 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003167
3168 /*
3169 * expire an async queue immediately if it has used up its slice. idle
3170 * queue always expire after 1 dispatch round.
3171 */
3172 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3173 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3174 cfq_class_idle(cfqq))) {
3175 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003176 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003177 }
3178
Shan Weib217a902009-09-01 10:06:42 +02003179 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003180 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183/*
Jens Axboe5e705372006-07-13 12:39:25 +02003184 * task holds one reference to the queue, dropped when task exits. each rq
3185 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003187 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 * queue lock must be held here.
3189 */
3190static void cfq_put_queue(struct cfq_queue *cfqq)
3191{
Jens Axboe22e2c502005-06-27 10:55:12 +02003192 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003193 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003194
Shaohua Li30d7b942011-01-07 08:46:59 +01003195 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Shaohua Li30d7b942011-01-07 08:46:59 +01003197 cfqq->ref--;
3198 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return;
3200
Jens Axboe7b679132008-05-30 12:23:07 +02003201 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003203 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003204 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003206 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003207 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003208 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003209 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003210
Vivek Goyalf04a6422009-12-03 12:59:40 -05003211 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003213 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Shaohua Lid02a2c02010-05-25 10:16:53 +02003216static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003217{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003218 struct cfq_queue *__cfqq, *next;
3219
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003220 /*
3221 * If this queue was scheduled to merge with another queue, be
3222 * sure to drop the reference taken on that queue (and others in
3223 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3224 */
3225 __cfqq = cfqq->new_cfqq;
3226 while (__cfqq) {
3227 if (__cfqq == cfqq) {
3228 WARN(1, "cfqq->new_cfqq loop detected\n");
3229 break;
3230 }
3231 next = __cfqq->new_cfqq;
3232 cfq_put_queue(__cfqq);
3233 __cfqq = next;
3234 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003235}
3236
3237static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3238{
3239 if (unlikely(cfqq == cfqd->active_queue)) {
3240 __cfq_slice_expired(cfqd, cfqq, 0);
3241 cfq_schedule_dispatch(cfqd);
3242 }
3243
3244 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003245
Jens Axboe89850f72006-07-22 16:48:31 +02003246 cfq_put_queue(cfqq);
3247}
3248
Tejun Heo9b84cac2011-12-14 00:33:42 +01003249static void cfq_init_icq(struct io_cq *icq)
3250{
3251 struct cfq_io_cq *cic = icq_to_cic(icq);
3252
3253 cic->ttime.last_end_request = jiffies;
3254}
3255
Tejun Heoc5869802011-12-14 00:33:41 +01003256static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003257{
Tejun Heoc5869802011-12-14 00:33:41 +01003258 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003259 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003260
Jens Axboeff6657c2009-04-08 10:58:57 +02003261 if (cic->cfqq[BLK_RW_ASYNC]) {
3262 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3263 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003264 }
3265
Jens Axboeff6657c2009-04-08 10:58:57 +02003266 if (cic->cfqq[BLK_RW_SYNC]) {
3267 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3268 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003269 }
Jens Axboe89850f72006-07-22 16:48:31 +02003270}
3271
Tejun Heoabede6d2012-03-19 15:10:57 -07003272static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003273{
3274 struct task_struct *tsk = current;
3275 int ioprio_class;
3276
Jens Axboe3b181522005-06-27 10:56:24 +02003277 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003278 return;
3279
Tejun Heo598971b2012-03-19 15:10:58 -07003280 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003281 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003282 default:
3283 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3284 case IOPRIO_CLASS_NONE:
3285 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003286 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003287 */
3288 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003289 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003290 break;
3291 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003292 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003293 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3294 break;
3295 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003296 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003297 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3298 break;
3299 case IOPRIO_CLASS_IDLE:
3300 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3301 cfqq->ioprio = 7;
3302 cfq_clear_cfqq_idle_window(cfqq);
3303 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003304 }
3305
3306 /*
3307 * keep track of original prio settings in case we have to temporarily
3308 * elevate the priority of this queue
3309 */
3310 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003311 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003312}
3313
Tejun Heo598971b2012-03-19 15:10:58 -07003314static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003315{
Tejun Heo598971b2012-03-19 15:10:58 -07003316 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003317 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003318 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003319
Tejun Heo598971b2012-03-19 15:10:58 -07003320 /*
3321 * Check whether ioprio has changed. The condition may trigger
3322 * spuriously on a newly created cic but there's no harm.
3323 */
3324 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003325 return;
3326
Jens Axboeff6657c2009-04-08 10:58:57 +02003327 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003328 if (cfqq) {
3329 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003330 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3331 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003332 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003333 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003334 cfq_put_queue(cfqq);
3335 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003336 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003337
Jens Axboeff6657c2009-04-08 10:58:57 +02003338 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003339 if (cfqq)
3340 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003341
3342 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003343}
3344
Jens Axboed5036d72009-06-26 10:44:34 +02003345static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003346 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003347{
3348 RB_CLEAR_NODE(&cfqq->rb_node);
3349 RB_CLEAR_NODE(&cfqq->p_node);
3350 INIT_LIST_HEAD(&cfqq->fifo);
3351
Shaohua Li30d7b942011-01-07 08:46:59 +01003352 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003353 cfqq->cfqd = cfqd;
3354
3355 cfq_mark_cfqq_prio_changed(cfqq);
3356
3357 if (is_sync) {
3358 if (!cfq_class_idle(cfqq))
3359 cfq_mark_cfqq_idle_window(cfqq);
3360 cfq_mark_cfqq_sync(cfqq);
3361 }
3362 cfqq->pid = pid;
3363}
3364
Vivek Goyal246103332009-12-03 12:59:51 -05003365#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003366static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003367{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003368 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003369 struct cfq_queue *sync_cfqq;
3370 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003371
Tejun Heo598971b2012-03-19 15:10:58 -07003372 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07003373 id = bio_blkcg(bio)->id;
Tejun Heo598971b2012-03-19 15:10:58 -07003374 rcu_read_unlock();
3375
3376 /*
3377 * Check whether blkcg has changed. The condition may trigger
3378 * spuriously on a newly created cic but there's no harm.
3379 */
3380 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003381 return;
3382
Tejun Heo598971b2012-03-19 15:10:58 -07003383 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003384 if (sync_cfqq) {
3385 /*
3386 * Drop reference to sync queue. A new sync queue will be
3387 * assigned in new group upon arrival of a fresh request.
3388 */
3389 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3390 cic_set_cfqq(cic, NULL, 1);
3391 cfq_put_queue(sync_cfqq);
3392 }
Tejun Heo598971b2012-03-19 15:10:58 -07003393
3394 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003395}
Tejun Heo598971b2012-03-19 15:10:58 -07003396#else
3397static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003398#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003401cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3402 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Tejun Heo3c798392012-04-16 13:57:25 -07003404 struct blkcg *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003406 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003409 rcu_read_lock();
3410
Tejun Heo3c798392012-04-16 13:57:25 -07003411 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003412 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003413 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Jens Axboe6118b702009-06-30 09:34:12 +02003415 /*
3416 * Always try a new alloc if we fell back to the OOM cfqq
3417 * originally, since it should just be a temporary situation.
3418 */
3419 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3420 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (new_cfqq) {
3422 cfqq = new_cfqq;
3423 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003424 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003425 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003427 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003428 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003429 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003431 if (new_cfqq)
3432 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003433 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003434 cfqq = kmem_cache_alloc_node(cfq_pool,
3435 gfp_mask | __GFP_ZERO,
3436 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Jens Axboe6118b702009-06-30 09:34:12 +02003439 if (cfqq) {
3440 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003441 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003442 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003443 cfq_log_cfqq(cfqd, cfqq, "alloced");
3444 } else
3445 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
3447
3448 if (new_cfqq)
3449 kmem_cache_free(cfq_pool, new_cfqq);
3450
Tejun Heo2a7f1242012-03-05 13:15:01 -08003451 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return cfqq;
3453}
3454
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003455static struct cfq_queue **
3456cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3457{
Jens Axboefe094d92008-01-31 13:08:54 +01003458 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003459 case IOPRIO_CLASS_RT:
3460 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003461 case IOPRIO_CLASS_NONE:
3462 ioprio = IOPRIO_NORM;
3463 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003464 case IOPRIO_CLASS_BE:
3465 return &cfqd->async_cfqq[1][ioprio];
3466 case IOPRIO_CLASS_IDLE:
3467 return &cfqd->async_idle_cfqq;
3468 default:
3469 BUG();
3470 }
3471}
3472
Jens Axboe15c31be2007-07-10 13:43:25 +02003473static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003474cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003475 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003476{
Tejun Heo598971b2012-03-19 15:10:58 -07003477 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3478 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003479 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003480 struct cfq_queue *cfqq = NULL;
3481
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003482 if (!is_sync) {
3483 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3484 cfqq = *async_cfqq;
3485 }
3486
Jens Axboe6118b702009-06-30 09:34:12 +02003487 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003488 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003489
3490 /*
3491 * pin the queue now that it's allocated, scheduler exit will prune it
3492 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003493 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003494 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003495 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003496 }
3497
Shaohua Li30d7b942011-01-07 08:46:59 +01003498 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003499 return cfqq;
3500}
3501
Jens Axboe22e2c502005-06-27 10:55:12 +02003502static void
Shaohua Li383cd722011-07-12 14:24:35 +02003503__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003504{
Shaohua Li383cd722011-07-12 14:24:35 +02003505 unsigned long elapsed = jiffies - ttime->last_end_request;
3506 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003507
Shaohua Li383cd722011-07-12 14:24:35 +02003508 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3509 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3510 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3511}
3512
3513static void
3514cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003515 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003516{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003517 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003518 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003519 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3520 cfqd->cfq_slice_idle);
3521 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003522#ifdef CONFIG_CFQ_GROUP_IOSCHED
3523 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3524#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003525}
3526
Jens Axboe206dc692006-03-28 13:03:44 +02003527static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003528cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003529 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003530{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003531 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003532 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003533 if (cfqq->last_request_pos) {
3534 if (cfqq->last_request_pos < blk_rq_pos(rq))
3535 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3536 else
3537 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3538 }
Jens Axboe206dc692006-03-28 13:03:44 +02003539
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003540 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003541 if (blk_queue_nonrot(cfqd->queue))
3542 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3543 else
3544 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003545}
Jens Axboe22e2c502005-06-27 10:55:12 +02003546
3547/*
3548 * Disable idle window if the process thinks too long or seeks so much that
3549 * it doesn't matter
3550 */
3551static void
3552cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003553 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003554{
Jens Axboe7b679132008-05-30 12:23:07 +02003555 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003556
Jens Axboe08717142008-01-28 11:38:15 +01003557 /*
3558 * Don't idle for async or idle io prio class
3559 */
3560 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003561 return;
3562
Jens Axboec265a7f2008-06-26 13:49:33 +02003563 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003564
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003565 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3566 cfq_mark_cfqq_deep(cfqq);
3567
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003568 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3569 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003570 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003571 !cfqd->cfq_slice_idle ||
3572 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003573 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003574 else if (sample_valid(cic->ttime.ttime_samples)) {
3575 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003576 enable_idle = 0;
3577 else
3578 enable_idle = 1;
3579 }
3580
Jens Axboe7b679132008-05-30 12:23:07 +02003581 if (old_idle != enable_idle) {
3582 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3583 if (enable_idle)
3584 cfq_mark_cfqq_idle_window(cfqq);
3585 else
3586 cfq_clear_cfqq_idle_window(cfqq);
3587 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003588}
3589
Jens Axboe22e2c502005-06-27 10:55:12 +02003590/*
3591 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3592 * no or if we aren't sure, a 1 will cause a preempt.
3593 */
Jens Axboea6151c32009-10-07 20:02:57 +02003594static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003595cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003596 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003597{
Jens Axboe6d048f52007-04-25 12:44:27 +02003598 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003599
Jens Axboe6d048f52007-04-25 12:44:27 +02003600 cfqq = cfqd->active_queue;
3601 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003602 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003603
Jens Axboe6d048f52007-04-25 12:44:27 +02003604 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003605 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003606
3607 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003608 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003609
Jens Axboe22e2c502005-06-27 10:55:12 +02003610 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003611 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3612 */
3613 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3614 return false;
3615
3616 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003617 * if the new request is sync, but the currently running queue is
3618 * not, let the sync request have priority.
3619 */
Jens Axboe5e705372006-07-13 12:39:25 +02003620 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003621 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003622
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003623 if (new_cfqq->cfqg != cfqq->cfqg)
3624 return false;
3625
3626 if (cfq_slice_used(cfqq))
3627 return true;
3628
3629 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003630 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003631 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3632 new_cfqq->service_tree->count == 2 &&
3633 RB_EMPTY_ROOT(&cfqq->sort_list))
3634 return true;
3635
Jens Axboe374f84a2006-07-23 01:42:19 +02003636 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003637 * So both queues are sync. Let the new request get disk time if
3638 * it's a metadata request and the current queue is doing regular IO.
3639 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003640 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003641 return true;
3642
3643 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003644 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3645 */
3646 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003647 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003648
Shaohua Lid2d59e12010-11-08 15:01:03 +01003649 /* An idle queue should not be idle now for some reason */
3650 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3651 return true;
3652
Jens Axboe1e3335d2007-02-14 19:59:49 +01003653 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003654 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003655
3656 /*
3657 * if this request is as-good as one we would expect from the
3658 * current cfqq, let it preempt
3659 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003660 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003661 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003662
Jens Axboea6151c32009-10-07 20:02:57 +02003663 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003664}
3665
3666/*
3667 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3668 * let it have half of its nominal slice.
3669 */
3670static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3671{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003672 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3673
Jens Axboe7b679132008-05-30 12:23:07 +02003674 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003675 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003676
Jens Axboebf572252006-07-19 20:29:12 +02003677 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003678 * workload type is changed, don't save slice, otherwise preempt
3679 * doesn't happen
3680 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003681 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003682 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003683
3684 /*
Jens Axboebf572252006-07-19 20:29:12 +02003685 * Put the new queue at the front of the of the current list,
3686 * so we know that it will be selected next.
3687 */
3688 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003689
3690 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003691
Justin TerAvest62a37f62011-03-23 08:25:44 +01003692 cfqq->slice_end = 0;
3693 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003694}
3695
3696/*
Jens Axboe5e705372006-07-13 12:39:25 +02003697 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003698 * something we should do about it
3699 */
3700static void
Jens Axboe5e705372006-07-13 12:39:25 +02003701cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3702 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003703{
Tejun Heoc5869802011-12-14 00:33:41 +01003704 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003705
Aaron Carroll45333d52008-08-26 15:52:36 +02003706 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003707 if (rq->cmd_flags & REQ_PRIO)
3708 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003709
Shaohua Li383cd722011-07-12 14:24:35 +02003710 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003711 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003712 cfq_update_idle_window(cfqd, cfqq, cic);
3713
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003714 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003715
3716 if (cfqq == cfqd->active_queue) {
3717 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003718 * Remember that we saw a request from this process, but
3719 * don't start queuing just yet. Otherwise we risk seeing lots
3720 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003721 * and merging. If the request is already larger than a single
3722 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003723 * merging is already done. Ditto for a busy system that
3724 * has other work pending, don't risk delaying until the
3725 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003726 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003727 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003728 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3729 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003730 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003731 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003732 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003733 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07003734 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003735 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003736 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003737 }
Jens Axboe5e705372006-07-13 12:39:25 +02003738 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003739 /*
3740 * not the active queue - expire current slice if it is
3741 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003742 * has some old slice time left and is of higher priority or
3743 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003744 */
3745 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003746 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003747 }
3748}
3749
Jens Axboe165125e2007-07-24 09:28:11 +02003750static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003751{
Jens Axboeb4878f22005-10-20 16:42:29 +02003752 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003753 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003754
Jens Axboe7b679132008-05-30 12:23:07 +02003755 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003756 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Jens Axboe30996f42009-10-05 11:03:39 +02003758 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003759 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003760 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07003761 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
3762 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02003763 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
Aaron Carroll45333d52008-08-26 15:52:36 +02003766/*
3767 * Update hw_tag based on peak queue depth over 50 samples under
3768 * sufficient load.
3769 */
3770static void cfq_update_hw_tag(struct cfq_data *cfqd)
3771{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003772 struct cfq_queue *cfqq = cfqd->active_queue;
3773
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003774 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3775 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003776
3777 if (cfqd->hw_tag == 1)
3778 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003779
3780 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003781 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003782 return;
3783
Shaohua Li1a1238a2009-10-27 08:46:23 +01003784 /*
3785 * If active queue hasn't enough requests and can idle, cfq might not
3786 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3787 * case
3788 */
3789 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3790 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003791 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003792 return;
3793
Aaron Carroll45333d52008-08-26 15:52:36 +02003794 if (cfqd->hw_tag_samples++ < 50)
3795 return;
3796
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003797 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003798 cfqd->hw_tag = 1;
3799 else
3800 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003801}
3802
Vivek Goyal7667aa02009-12-08 17:52:58 -05003803static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3804{
Tejun Heoc5869802011-12-14 00:33:41 +01003805 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003806
Justin TerAvest02a8f012011-02-09 14:20:03 +01003807 /* If the queue already has requests, don't wait */
3808 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3809 return false;
3810
Vivek Goyal7667aa02009-12-08 17:52:58 -05003811 /* If there are other queues in the group, don't wait */
3812 if (cfqq->cfqg->nr_cfqq > 1)
3813 return false;
3814
Shaohua Li7700fc42011-07-12 14:24:56 +02003815 /* the only queue in the group, but think time is big */
3816 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3817 return false;
3818
Vivek Goyal7667aa02009-12-08 17:52:58 -05003819 if (cfq_slice_used(cfqq))
3820 return true;
3821
3822 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003823 if (cic && sample_valid(cic->ttime.ttime_samples)
3824 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003825 return true;
3826
3827 /*
3828 * If think times is less than a jiffy than ttime_mean=0 and above
3829 * will not be true. It might happen that slice has not expired yet
3830 * but will expire soon (4-5 ns) during select_queue(). To cover the
3831 * case where think time is less than a jiffy, mark the queue wait
3832 * busy if only 1 jiffy is left in the slice.
3833 */
3834 if (cfqq->slice_end - jiffies == 1)
3835 return true;
3836
3837 return false;
3838}
3839
Jens Axboe165125e2007-07-24 09:28:11 +02003840static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
Jens Axboe5e705372006-07-13 12:39:25 +02003842 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003843 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003844 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003845 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Jens Axboeb4878f22005-10-20 16:42:29 +02003847 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003848 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3849 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Aaron Carroll45333d52008-08-26 15:52:36 +02003851 cfq_update_hw_tag(cfqd);
3852
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003853 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003854 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003855 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003856 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003857 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07003858 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
3859 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003861 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003862
Vivek Goyal365722b2009-10-03 15:21:27 +02003863 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04003864 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003865
Shaohua Li383cd722011-07-12 14:24:35 +02003866 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003867
3868 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04003869 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003870 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04003871 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
3872 cfqq_type(cfqq));
3873
3874 st->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003875 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3876 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003877 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003878
Shaohua Li7700fc42011-07-12 14:24:56 +02003879#ifdef CONFIG_CFQ_GROUP_IOSCHED
3880 cfqq->cfqg->ttime.last_end_request = now;
3881#endif
3882
Jens Axboecaaa5f92006-06-16 11:23:00 +02003883 /*
3884 * If this is the active queue, check if it needs to be expired,
3885 * or if we want to idle in case it has no pending requests.
3886 */
3887 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003888 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3889
Jens Axboe44f7c162007-01-19 11:51:58 +11003890 if (cfq_cfqq_slice_new(cfqq)) {
3891 cfq_set_prio_slice(cfqd, cfqq);
3892 cfq_clear_cfqq_slice_new(cfqq);
3893 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003894
3895 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003896 * Should we wait for next request to come in before we expire
3897 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003898 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003899 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003900 unsigned long extend_sl = cfqd->cfq_slice_idle;
3901 if (!cfqd->cfq_slice_idle)
3902 extend_sl = cfqd->cfq_group_idle;
3903 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003904 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003905 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003906 }
3907
Jens Axboea36e71f2009-04-15 12:15:11 +02003908 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003909 * Idling is not enabled on:
3910 * - expired queues
3911 * - idle-priority queues
3912 * - async queues
3913 * - queues with still some requests queued
3914 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003915 */
Jens Axboe08717142008-01-28 11:38:15 +01003916 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003917 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003918 else if (sync && cfqq_empty &&
3919 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003920 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003921 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003922 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003923
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003924 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003925 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
Jens Axboe89850f72006-07-22 16:48:31 +02003928static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003929{
Jens Axboe1b379d82009-08-11 08:26:11 +02003930 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003931 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003932 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003933 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003934
3935 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003936}
3937
Jens Axboe165125e2007-07-24 09:28:11 +02003938static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003939{
3940 struct cfq_data *cfqd = q->elevator->elevator_data;
3941 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003942 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003943 struct cfq_queue *cfqq;
3944
3945 /*
3946 * don't force setup of a queue from here, as a call to may_queue
3947 * does not necessarily imply that a request actually will be queued.
3948 * so just lookup a possibly existing queue, or return 'may queue'
3949 * if that fails
3950 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003951 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003952 if (!cic)
3953 return ELV_MQUEUE_MAY;
3954
Jens Axboeb0b78f82009-04-08 10:56:08 +02003955 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003956 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003957 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003958
Jens Axboe89850f72006-07-22 16:48:31 +02003959 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003960 }
3961
3962 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965/*
3966 * queue lock held here
3967 */
Jens Axboebb37b942006-12-01 10:42:33 +01003968static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Jens Axboe5e705372006-07-13 12:39:25 +02003970 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Jens Axboe5e705372006-07-13 12:39:25 +02003972 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003973 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Jens Axboe22e2c502005-06-27 10:55:12 +02003975 BUG_ON(!cfqq->allocated[rw]);
3976 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003978 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003979 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003980 rq->elv.priv[0] = NULL;
3981 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 cfq_put_queue(cfqq);
3984 }
3985}
3986
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003987static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003988cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003989 struct cfq_queue *cfqq)
3990{
3991 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3992 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003993 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003994 cfq_put_queue(cfqq);
3995 return cic_to_cfqq(cic, 1);
3996}
3997
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003998/*
3999 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4000 * was the last process referring to said cfqq.
4001 */
4002static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004003split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004004{
4005 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004006 cfqq->pid = current->pid;
4007 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004008 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004009 return cfqq;
4010 }
4011
4012 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004013
4014 cfq_put_cooperator(cfqq);
4015
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004016 cfq_put_queue(cfqq);
4017 return NULL;
4018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004020 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004022static int
Tejun Heo852c7882012-03-05 13:15:27 -08004023cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4024 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
4026 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004027 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004029 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004030 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 might_sleep_if(gfp_mask & __GFP_WAIT);
4033
Tejun Heo216284c2011-12-14 00:33:38 +01004034 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004035
Tejun Heo598971b2012-03-19 15:10:58 -07004036 check_ioprio_changed(cic, bio);
4037 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004038new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004039 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004040 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004041 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004042 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004043 } else {
4044 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004045 * If the queue was seeky for too long, break it apart.
4046 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004047 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004048 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4049 cfqq = split_cfqq(cic, cfqq);
4050 if (!cfqq)
4051 goto new_queue;
4052 }
4053
4054 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004055 * Check to see if this queue is scheduled to merge with
4056 * another, closely cooperating queue. The merging of
4057 * queues happens here as it must be done in process context.
4058 * The reference on new_cfqq was taken in merge_cfqqs.
4059 */
4060 if (cfqq->new_cfqq)
4061 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004065
Jens Axboe6fae9c22011-03-01 15:04:39 -05004066 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004067 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004068 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004069 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01004070 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02004071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
David Howells65f27f32006-11-22 14:55:48 +00004074static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004075{
David Howells65f27f32006-11-22 14:55:48 +00004076 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004077 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004078 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004079
Jens Axboe40bb54d2009-04-15 12:11:10 +02004080 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004081 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004082 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004083}
4084
4085/*
4086 * Timer running if the active_queue is currently idling inside its time slice
4087 */
4088static void cfq_idle_slice_timer(unsigned long data)
4089{
4090 struct cfq_data *cfqd = (struct cfq_data *) data;
4091 struct cfq_queue *cfqq;
4092 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004093 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004094
Jens Axboe7b679132008-05-30 12:23:07 +02004095 cfq_log(cfqd, "idle timer fired");
4096
Jens Axboe22e2c502005-06-27 10:55:12 +02004097 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4098
Jens Axboefe094d92008-01-31 13:08:54 +01004099 cfqq = cfqd->active_queue;
4100 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004101 timed_out = 0;
4102
Jens Axboe22e2c502005-06-27 10:55:12 +02004103 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004104 * We saw a request before the queue expired, let it through
4105 */
4106 if (cfq_cfqq_must_dispatch(cfqq))
4107 goto out_kick;
4108
4109 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004110 * expired
4111 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004112 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004113 goto expire;
4114
4115 /*
4116 * only expire and reinvoke request handler, if there are
4117 * other queues with pending requests
4118 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004119 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004120 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004121
4122 /*
4123 * not expired and it has a request pending, let it dispatch
4124 */
Jens Axboe75e50982009-04-07 08:56:14 +02004125 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004126 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004127
4128 /*
4129 * Queue depth flag is reset only when the idle didn't succeed
4130 */
4131 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004132 }
4133expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004134 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004135out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004136 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004137out_cont:
4138 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4139}
4140
Jens Axboe3b181522005-06-27 10:56:24 +02004141static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4142{
4143 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004144 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004145}
Jens Axboe22e2c502005-06-27 10:55:12 +02004146
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004147static void cfq_put_async_queues(struct cfq_data *cfqd)
4148{
4149 int i;
4150
4151 for (i = 0; i < IOPRIO_BE_NR; i++) {
4152 if (cfqd->async_cfqq[0][i])
4153 cfq_put_queue(cfqd->async_cfqq[0][i]);
4154 if (cfqd->async_cfqq[1][i])
4155 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004156 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01004157
4158 if (cfqd->async_idle_cfqq)
4159 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004160}
4161
Jens Axboeb374d182008-10-31 10:05:07 +01004162static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Jens Axboe22e2c502005-06-27 10:55:12 +02004164 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004165 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004166
Jens Axboe3b181522005-06-27 10:56:24 +02004167 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004168
Al Virod9ff4182006-03-18 13:51:22 -05004169 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004170
Al Virod9ff4182006-03-18 13:51:22 -05004171 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004172 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004173
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004174 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08004175
4176 spin_unlock_irq(q->queue_lock);
4177
Al Viroa90d7422006-03-18 12:05:37 -05004178 cfq_shutdown_timer_wq(cfqd);
4179
Tejun Heoffea73f2012-06-04 10:02:29 +02004180#ifdef CONFIG_CFQ_GROUP_IOSCHED
4181 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4182#else
Tejun Heof51b8022012-03-05 13:15:05 -08004183 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004184#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004185 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Tejun Heob2fab5a2012-03-05 13:14:57 -08004188static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189{
4190 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004191 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004192 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Christoph Lameter94f60302007-07-17 04:03:29 -07004194 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01004195 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004196 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004197
Tejun Heof51b8022012-03-05 13:15:05 -08004198 cfqd->queue = q;
4199 q->elevator->elevator_data = cfqd;
4200
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004201 /* Init root service tree */
4202 cfqd->grp_service_tree = CFQ_RB_ROOT;
4203
Tejun Heof51b8022012-03-05 13:15:05 -08004204 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004205#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004206 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004207 if (ret)
4208 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004209
Tejun Heoa2b16932012-04-13 13:11:33 -07004210 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004211#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004212 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004213 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4214 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004215 if (!cfqd->root_group)
4216 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004217
Tejun Heoa2b16932012-04-13 13:11:33 -07004218 cfq_init_cfqg_base(cfqd->root_group);
4219#endif
Tejun Heo3381cb82012-04-01 14:38:44 -07004220 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -08004221 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004222
Jens Axboe26a2ac02009-04-23 12:13:27 +02004223 /*
4224 * Not strictly needed (since RB_ROOT just clears the node and we
4225 * zeroed cfqd on alloc), but better be safe in case someone decides
4226 * to add magic to the rb code
4227 */
4228 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4229 cfqd->prio_trees[i] = RB_ROOT;
4230
Jens Axboe6118b702009-06-30 09:34:12 +02004231 /*
4232 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4233 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004234 * will not attempt to free it. oom_cfqq is linked to root_group
4235 * but shouldn't hold a reference as it'll never be unlinked. Lose
4236 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004237 */
4238 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004239 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004240
4241 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004242 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004243 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004244 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Jens Axboe22e2c502005-06-27 10:55:12 +02004246 init_timer(&cfqd->idle_slice_timer);
4247 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4248 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4249
Jens Axboe23e018a2009-10-05 08:52:35 +02004250 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004253 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4254 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 cfqd->cfq_back_max = cfq_back_max;
4256 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004257 cfqd->cfq_slice[0] = cfq_slice_async;
4258 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004259 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004260 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4261 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004262 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004263 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004264 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004265 /*
4266 * we optimistically start assuming sync ops weren't delayed in last
4267 * second, in order to have larger depth for async operations.
4268 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004269 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004270 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004271
4272out_free:
4273 kfree(cfqd);
4274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277/*
4278 * sysfs parts below -->
4279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280static ssize_t
4281cfq_var_show(unsigned int var, char *page)
4282{
4283 return sprintf(page, "%d\n", var);
4284}
4285
4286static ssize_t
4287cfq_var_store(unsigned int *var, const char *page, size_t count)
4288{
4289 char *p = (char *) page;
4290
4291 *var = simple_strtoul(p, &p, 10);
4292 return count;
4293}
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004296static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004298 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 unsigned int __data = __VAR; \
4300 if (__CONV) \
4301 __data = jiffies_to_msecs(__data); \
4302 return cfq_var_show(__data, (page)); \
4303}
4304SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004305SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4306SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004307SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4308SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004309SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004310SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004311SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4312SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4313SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004314SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004315SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316#undef SHOW_FUNCTION
4317
4318#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004319static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004321 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 unsigned int __data; \
4323 int ret = cfq_var_store(&__data, (page), count); \
4324 if (__data < (MIN)) \
4325 __data = (MIN); \
4326 else if (__data > (MAX)) \
4327 __data = (MAX); \
4328 if (__CONV) \
4329 *(__PTR) = msecs_to_jiffies(__data); \
4330 else \
4331 *(__PTR) = __data; \
4332 return ret; \
4333}
4334STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004335STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4336 UINT_MAX, 1);
4337STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4338 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004339STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004340STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4341 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004342STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004343STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004344STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4345STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004346STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4347 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004348STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004349STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350#undef STORE_FUNCTION
4351
Al Viroe572ec72006-03-18 22:27:18 -05004352#define CFQ_ATTR(name) \
4353 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004354
Al Viroe572ec72006-03-18 22:27:18 -05004355static struct elv_fs_entry cfq_attrs[] = {
4356 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004357 CFQ_ATTR(fifo_expire_sync),
4358 CFQ_ATTR(fifo_expire_async),
4359 CFQ_ATTR(back_seek_max),
4360 CFQ_ATTR(back_seek_penalty),
4361 CFQ_ATTR(slice_sync),
4362 CFQ_ATTR(slice_async),
4363 CFQ_ATTR(slice_async_rq),
4364 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004365 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004366 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004367 CFQ_ATTR(target_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004368 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369};
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371static struct elevator_type iosched_cfq = {
4372 .ops = {
4373 .elevator_merge_fn = cfq_merge,
4374 .elevator_merged_fn = cfq_merged_request,
4375 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004376 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004377 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004378 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004380 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004383 .elevator_former_req_fn = elv_rb_former_request,
4384 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004385 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004386 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 .elevator_set_req_fn = cfq_set_request,
4388 .elevator_put_req_fn = cfq_put_request,
4389 .elevator_may_queue_fn = cfq_may_queue,
4390 .elevator_init_fn = cfq_init_queue,
4391 .elevator_exit_fn = cfq_exit_queue,
4392 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004393 .icq_size = sizeof(struct cfq_io_cq),
4394 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004395 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004396 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 .elevator_owner = THIS_MODULE,
4398};
4399
Vivek Goyal3e252062009-12-04 10:36:42 -05004400#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004401static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004402 .pd_size = sizeof(struct cfq_group),
4403 .cftypes = cfq_blkcg_files,
4404
4405 .pd_init_fn = cfq_pd_init,
4406 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004407};
Vivek Goyal3e252062009-12-04 10:36:42 -05004408#endif
4409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410static int __init cfq_init(void)
4411{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004412 int ret;
4413
Jens Axboe22e2c502005-06-27 10:55:12 +02004414 /*
4415 * could be 0 on HZ < 1000 setups
4416 */
4417 if (!cfq_slice_async)
4418 cfq_slice_async = 1;
4419 if (!cfq_slice_idle)
4420 cfq_slice_idle = 1;
4421
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004422#ifdef CONFIG_CFQ_GROUP_IOSCHED
4423 if (!cfq_group_idle)
4424 cfq_group_idle = 1;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004425
Tejun Heo3c798392012-04-16 13:57:25 -07004426 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004427 if (ret)
4428 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004429#else
4430 cfq_group_idle = 0;
4431#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004432
Tejun Heofd794952012-06-04 10:01:38 +02004433 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004434 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4435 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004436 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Tejun Heo3d3c2372011-12-14 00:33:42 +01004438 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004439 if (ret)
4440 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004441
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004442 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004443
4444err_free_pool:
4445 kmem_cache_destroy(cfq_pool);
4446err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004447#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004448 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004449#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004450 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451}
4452
4453static void __exit cfq_exit(void)
4454{
Tejun Heoffea73f2012-06-04 10:02:29 +02004455#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004456 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004457#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004459 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462module_init(cfq_init);
4463module_exit(cfq_exit);
4464
4465MODULE_AUTHOR("Jens Axboe");
4466MODULE_LICENSE("GPL");
4467MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");