blob: ee342826fd98e89a877c8a76e60bb016689047ca [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 Heo7918ffb2013-01-09 08:05:11 -0800609/*
610 * Determine the parent cfqg for weight calculation. Currently, cfqg
611 * scheduling is flat and the root is the parent of everyone else.
612 */
613static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg)
614{
615 struct blkcg_gq *blkg = cfqg_to_blkg(cfqg);
616 struct cfq_group *root;
617
618 while (blkg->parent)
619 blkg = blkg->parent;
620 root = blkg_to_cfqg(blkg);
621
622 return root != cfqg ? root : NULL;
623}
624
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100625static inline void cfqg_get(struct cfq_group *cfqg)
626{
627 return blkg_get(cfqg_to_blkg(cfqg));
628}
629
630static inline void cfqg_put(struct cfq_group *cfqg)
631{
632 return blkg_put(cfqg_to_blkg(cfqg));
633}
634
Tejun Heo54e7ed12012-04-16 13:57:23 -0700635#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
636 char __pbuf[128]; \
637 \
638 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400639 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
640 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
641 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700642 __pbuf, ##args); \
643} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500644
Tejun Heo54e7ed12012-04-16 13:57:23 -0700645#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
646 char __pbuf[128]; \
647 \
648 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
649 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
650} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500651
Tejun Heo155fead2012-04-01 14:38:44 -0700652static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
653 struct cfq_group *curr_cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700654{
Tejun Heo155fead2012-04-01 14:38:44 -0700655 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
656 cfqg_stats_end_empty_time(&cfqg->stats);
657 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700658}
659
Tejun Heo155fead2012-04-01 14:38:44 -0700660static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
661 unsigned long time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700662{
Tejun Heo155fead2012-04-01 14:38:44 -0700663 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700664#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700665 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700666#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700667}
668
Tejun Heo155fead2012-04-01 14:38:44 -0700669static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700670{
Tejun Heo155fead2012-04-01 14:38:44 -0700671 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700672}
673
Tejun Heo155fead2012-04-01 14:38:44 -0700674static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700675{
Tejun Heo155fead2012-04-01 14:38:44 -0700676 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700677}
678
Tejun Heo155fead2012-04-01 14:38:44 -0700679static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
680 uint64_t bytes, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700681{
Tejun Heo155fead2012-04-01 14:38:44 -0700682 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
683 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
684 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700685}
686
Tejun Heo155fead2012-04-01 14:38:44 -0700687static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
688 uint64_t start_time, uint64_t io_start_time, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700689{
Tejun Heo155fead2012-04-01 14:38:44 -0700690 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700691 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700692
693 if (time_after64(now, io_start_time))
694 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
695 if (time_after64(io_start_time, start_time))
696 blkg_rwstat_add(&stats->wait_time, rw,
697 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700698}
699
Tejun Heo3c798392012-04-16 13:57:25 -0700700static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo155fead2012-04-01 14:38:44 -0700701{
702 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
703 struct cfqg_stats *stats = &cfqg->stats;
704
705 /* queued stats shouldn't be cleared */
706 blkg_rwstat_reset(&stats->service_bytes);
707 blkg_rwstat_reset(&stats->serviced);
708 blkg_rwstat_reset(&stats->merged);
709 blkg_rwstat_reset(&stats->service_time);
710 blkg_rwstat_reset(&stats->wait_time);
711 blkg_stat_reset(&stats->time);
712#ifdef CONFIG_DEBUG_BLK_CGROUP
713 blkg_stat_reset(&stats->unaccounted_time);
714 blkg_stat_reset(&stats->avg_queue_size_sum);
715 blkg_stat_reset(&stats->avg_queue_size_samples);
716 blkg_stat_reset(&stats->dequeue);
717 blkg_stat_reset(&stats->group_wait_time);
718 blkg_stat_reset(&stats->idle_time);
719 blkg_stat_reset(&stats->empty_time);
720#endif
721}
722
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100723#else /* CONFIG_CFQ_GROUP_IOSCHED */
724
Tejun Heo7918ffb2013-01-09 08:05:11 -0800725static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg) { return NULL; }
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100726static inline void cfqg_get(struct cfq_group *cfqg) { }
727static inline void cfqg_put(struct cfq_group *cfqg) { }
728
Jens Axboe7b679132008-05-30 12:23:07 +0200729#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400730 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
731 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
732 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
733 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200734#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100735
Tejun Heo155fead2012-04-01 14:38:44 -0700736static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
737 struct cfq_group *curr_cfqg, int rw) { }
738static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
739 unsigned long time, unsigned long unaccounted_time) { }
740static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
741static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
742static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
743 uint64_t bytes, int rw) { }
744static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
745 uint64_t start_time, uint64_t io_start_time, int rw) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700746
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100747#endif /* CONFIG_CFQ_GROUP_IOSCHED */
748
Jens Axboe7b679132008-05-30 12:23:07 +0200749#define cfq_log(cfqd, fmt, args...) \
750 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
751
Vivek Goyal615f0252009-12-03 12:59:39 -0500752/* Traverses through cfq group service trees */
753#define for_each_cfqg_st(cfqg, i, j, st) \
754 for (i = 0; i <= IDLE_WORKLOAD; i++) \
755 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
756 : &cfqg->service_tree_idle; \
757 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
758 (i == IDLE_WORKLOAD && j == 0); \
759 j++, st = i < IDLE_WORKLOAD ? \
760 &cfqg->service_trees[i][j]: NULL) \
761
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200762static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
763 struct cfq_ttime *ttime, bool group_idle)
764{
765 unsigned long slice;
766 if (!sample_valid(ttime->ttime_samples))
767 return false;
768 if (group_idle)
769 slice = cfqd->cfq_group_idle;
770 else
771 slice = cfqd->cfq_slice_idle;
772 return ttime->ttime_mean > slice;
773}
Vivek Goyal615f0252009-12-03 12:59:39 -0500774
Vivek Goyal02b35082010-08-23 12:23:53 +0200775static inline bool iops_mode(struct cfq_data *cfqd)
776{
777 /*
778 * If we are not idling on queues and it is a NCQ drive, parallel
779 * execution of requests is on and measuring time is not possible
780 * in most of the cases until and unless we drive shallower queue
781 * depths and that becomes a performance bottleneck. In such cases
782 * switch to start providing fairness in terms of number of IOs.
783 */
784 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
785 return true;
786 else
787 return false;
788}
789
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400790static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100791{
792 if (cfq_class_idle(cfqq))
793 return IDLE_WORKLOAD;
794 if (cfq_class_rt(cfqq))
795 return RT_WORKLOAD;
796 return BE_WORKLOAD;
797}
798
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100799
800static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
801{
802 if (!cfq_cfqq_sync(cfqq))
803 return ASYNC_WORKLOAD;
804 if (!cfq_cfqq_idle_window(cfqq))
805 return SYNC_NOIDLE_WORKLOAD;
806 return SYNC_WORKLOAD;
807}
808
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400809static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500810 struct cfq_data *cfqd,
811 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100812{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400813 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500814 return cfqg->service_tree_idle.count;
815
Vivek Goyal34b98d02012-10-03 16:56:58 -0400816 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
817 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
818 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100819}
820
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500821static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
822 struct cfq_group *cfqg)
823{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400824 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
825 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500826}
827
Jens Axboe165125e2007-07-24 09:28:11 +0200828static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800829static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700830 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800831 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200832
Tejun Heoc5869802011-12-14 00:33:41 +0100833static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
834{
835 /* cic->icq is the first member, %NULL will convert to %NULL */
836 return container_of(icq, struct cfq_io_cq, icq);
837}
838
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100839static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
840 struct io_context *ioc)
841{
842 if (ioc)
843 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
844 return NULL;
845}
846
Tejun Heoc5869802011-12-14 00:33:41 +0100847static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200848{
Jens Axboea6151c32009-10-07 20:02:57 +0200849 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200850}
851
Tejun Heoc5869802011-12-14 00:33:41 +0100852static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
853 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200854{
Jens Axboea6151c32009-10-07 20:02:57 +0200855 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200856}
857
Tejun Heoc5869802011-12-14 00:33:41 +0100858static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400859{
Tejun Heoc5869802011-12-14 00:33:41 +0100860 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400861}
862
Vasily Tarasov91fac312007-04-25 12:29:51 +0200863/*
864 * We regard a request as SYNC, if it's either a read or has the SYNC bit
865 * set (in which case it could also be direct WRITE).
866 */
Jens Axboea6151c32009-10-07 20:02:57 +0200867static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200868{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200869 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200870}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700873 * scheduler run of queue, if there are requests pending and no one in the
874 * driver that will restart queueing
875 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200876static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700877{
Jens Axboe7b679132008-05-30 12:23:07 +0200878 if (cfqd->busy_queues) {
879 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200880 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200881 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100885 * Scale schedule slice based on io priority. Use the sync time slice only
886 * if a queue is marked sync and has sync io queued. A sync queue with async
887 * io only, should not get full sync slice length.
888 */
Jens Axboea6151c32009-10-07 20:02:57 +0200889static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200890 unsigned short prio)
891{
892 const int base_slice = cfqd->cfq_slice[sync];
893
894 WARN_ON(prio >= IOPRIO_BE_NR);
895
896 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
897}
898
Jens Axboe44f7c162007-01-19 11:51:58 +1100899static inline int
900cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
901{
Jens Axboed9e76202007-04-20 14:27:50 +0200902 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100903}
904
Tejun Heo1d3650f2013-01-09 08:05:11 -0800905/**
906 * cfqg_scale_charge - scale disk time charge according to cfqg weight
907 * @charge: disk time being charged
908 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
909 *
910 * Scale @charge according to @vfraction, which is in range (0, 1]. The
911 * scaling is inversely proportional.
912 *
913 * scaled = charge / vfraction
914 *
915 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
916 */
917static inline u64 cfqg_scale_charge(unsigned long charge,
918 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500919{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800920 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500921
Tejun Heo1d3650f2013-01-09 08:05:11 -0800922 /* charge / vfraction */
923 c <<= CFQ_SERVICE_SHIFT;
924 do_div(c, vfraction);
925 return c;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500926}
927
928static inline u64 max_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 inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
938{
939 s64 delta = (s64)(vdisktime - min_vdisktime);
940 if (delta < 0)
941 min_vdisktime = vdisktime;
942
943 return min_vdisktime;
944}
945
946static void update_min_vdisktime(struct cfq_rb_root *st)
947{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500948 struct cfq_group *cfqg;
949
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500950 if (st->left) {
951 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100952 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
953 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500954 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500955}
956
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100957/*
958 * get averaged number of queues of RT/BE priority.
959 * average is updated, with a formula that gives more weight to higher numbers,
960 * to quickly follows sudden increases and decrease slowly
961 */
962
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500963static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
964 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100965{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100966 unsigned min_q, max_q;
967 unsigned mult = cfq_hist_divisor - 1;
968 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500969 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100970
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500971 min_q = min(cfqg->busy_queues_avg[rt], busy);
972 max_q = max(cfqg->busy_queues_avg[rt], busy);
973 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100974 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500975 return cfqg->busy_queues_avg[rt];
976}
977
978static inline unsigned
979cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
980{
Tejun Heo41cad6a2013-01-09 08:05:11 -0800981 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100982}
983
Shaohua Lic553f8e2011-01-14 08:41:03 +0100984static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700985cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100986{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100987 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
988 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500989 /*
990 * interested queues (we consider only the ones with the same
991 * priority class in the cfq group)
992 */
993 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
994 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100995 unsigned sync_slice = cfqd->cfq_slice[1];
996 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500997 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
998
999 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001000 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
1001 /* scale low_slice according to IO priority
1002 * and sync vs async */
1003 unsigned low_slice =
1004 min(slice, base_low_slice * slice / sync_slice);
1005 /* the adapted slice value is scaled to fit all iqs
1006 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001007 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001008 low_slice);
1009 }
1010 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001011 return slice;
1012}
1013
1014static inline void
1015cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1016{
Vivek Goyalba5bd522011-01-19 08:25:02 -07001017 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001018
Vivek Goyaldae739e2009-12-03 12:59:45 -05001019 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001020 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001021 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +02001022 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +11001023}
1024
1025/*
1026 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1027 * isn't valid until the first request from the dispatch is activated
1028 * and the slice time set.
1029 */
Jens Axboea6151c32009-10-07 20:02:57 +02001030static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001031{
1032 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001033 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001034 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01001035 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001036
Shaohua Lic1e44752010-11-08 15:01:02 +01001037 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001038}
1039
1040/*
Jens Axboe5e705372006-07-13 12:39:25 +02001041 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001043 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Jens Axboe5e705372006-07-13 12:39:25 +02001045static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001046cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001048 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001050#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1051#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1052 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Jens Axboe5e705372006-07-13 12:39:25 +02001054 if (rq1 == NULL || rq1 == rq2)
1055 return rq2;
1056 if (rq2 == NULL)
1057 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001058
Namhyung Kim229836b2011-05-24 10:23:21 +02001059 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1060 return rq_is_sync(rq1) ? rq1 : rq2;
1061
Christoph Hellwig65299a32011-08-23 14:50:29 +02001062 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1063 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001064
Tejun Heo83096eb2009-05-07 22:24:39 +09001065 s1 = blk_rq_pos(rq1);
1066 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * by definition, 1KiB is 2 sectors
1070 */
1071 back_max = cfqd->cfq_back_max * 2;
1072
1073 /*
1074 * Strict one way elevator _except_ in the case where we allow
1075 * short backward seeks which are biased as twice the cost of a
1076 * similar forward seek.
1077 */
1078 if (s1 >= last)
1079 d1 = s1 - last;
1080 else if (s1 + back_max >= last)
1081 d1 = (last - s1) * cfqd->cfq_back_penalty;
1082 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001083 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (s2 >= last)
1086 d2 = s2 - last;
1087 else if (s2 + back_max >= last)
1088 d2 = (last - s2) * cfqd->cfq_back_penalty;
1089 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001090 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Andreas Mohre8a99052006-03-28 08:59:49 +02001094 /*
1095 * By doing switch() on the bit mask "wrap" we avoid having to
1096 * check two variables for all permutations: --> faster!
1097 */
1098 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001099 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001100 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001101 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001102 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001103 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001104 else {
1105 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001106 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001107 else
Jens Axboe5e705372006-07-13 12:39:25 +02001108 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001109 }
1110
1111 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001112 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001113 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001114 return rq2;
1115 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001116 default:
1117 /*
1118 * Since both rqs are wrapped,
1119 * start with the one that's further behind head
1120 * (--> only *one* back seek required),
1121 * since back seek takes more time than forward.
1122 */
1123 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001124 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 else
Jens Axboe5e705372006-07-13 12:39:25 +02001126 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128}
1129
Jens Axboe498d3aa22007-04-26 12:54:48 +02001130/*
1131 * The below is leftmost cache rbtree addon
1132 */
Jens Axboe08717142008-01-28 11:38:15 +01001133static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001134{
Vivek Goyal615f0252009-12-03 12:59:39 -05001135 /* Service tree is empty */
1136 if (!root->count)
1137 return NULL;
1138
Jens Axboecc09e292007-04-26 12:53:50 +02001139 if (!root->left)
1140 root->left = rb_first(&root->rb);
1141
Jens Axboe08717142008-01-28 11:38:15 +01001142 if (root->left)
1143 return rb_entry(root->left, struct cfq_queue, rb_node);
1144
1145 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001146}
1147
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001148static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1149{
1150 if (!root->left)
1151 root->left = rb_first(&root->rb);
1152
1153 if (root->left)
1154 return rb_entry_cfqg(root->left);
1155
1156 return NULL;
1157}
1158
Jens Axboea36e71f2009-04-15 12:15:11 +02001159static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1160{
1161 rb_erase(n, root);
1162 RB_CLEAR_NODE(n);
1163}
1164
Jens Axboecc09e292007-04-26 12:53:50 +02001165static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1166{
1167 if (root->left == n)
1168 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001169 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001170 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/*
1174 * would be nice to take fifo expire time into account as well
1175 */
Jens Axboe5e705372006-07-13 12:39:25 +02001176static struct request *
1177cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1178 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jens Axboe21183b02006-07-13 12:33:14 +02001180 struct rb_node *rbnext = rb_next(&last->rb_node);
1181 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001182 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Jens Axboe21183b02006-07-13 12:33:14 +02001184 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001187 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001190 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001191 else {
1192 rbnext = rb_first(&cfqq->sort_list);
1193 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001194 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001197 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
Jens Axboed9e76202007-04-20 14:27:50 +02001200static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1201 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Jens Axboed9e76202007-04-20 14:27:50 +02001203 /*
1204 * just an approximation, should be ok.
1205 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001206 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001207 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001210static inline s64
1211cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1212{
1213 return cfqg->vdisktime - st->min_vdisktime;
1214}
1215
1216static void
1217__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1218{
1219 struct rb_node **node = &st->rb.rb_node;
1220 struct rb_node *parent = NULL;
1221 struct cfq_group *__cfqg;
1222 s64 key = cfqg_key(st, cfqg);
1223 int left = 1;
1224
1225 while (*node != NULL) {
1226 parent = *node;
1227 __cfqg = rb_entry_cfqg(parent);
1228
1229 if (key < cfqg_key(st, __cfqg))
1230 node = &parent->rb_left;
1231 else {
1232 node = &parent->rb_right;
1233 left = 0;
1234 }
1235 }
1236
1237 if (left)
1238 st->left = &cfqg->rb_node;
1239
1240 rb_link_node(&cfqg->rb_node, parent, node);
1241 rb_insert_color(&cfqg->rb_node, &st->rb);
1242}
1243
1244static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001245cfq_update_group_weight(struct cfq_group *cfqg)
1246{
1247 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001248
Tejun Heo3381cb82012-04-01 14:38:44 -07001249 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001250 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001251 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001252 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001253
1254 if (cfqg->new_leaf_weight) {
1255 cfqg->leaf_weight = cfqg->new_leaf_weight;
1256 cfqg->new_leaf_weight = 0;
1257 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001258}
1259
1260static void
1261cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1262{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001263 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001264 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001265 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001266 bool propagate;
1267
1268 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001269 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1270
1271 cfq_update_group_weight(cfqg);
1272 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001273
1274 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001275 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1276 * entitled to. vfraction is calculated by walking the tree
1277 * towards the root calculating the fraction it has at each level.
1278 * The compounded ratio is how much vfraction @cfqg owns.
1279 *
1280 * Start with the proportion tasks in this cfqg has against active
1281 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001282 */
1283 propagate = !pos->nr_active++;
1284 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001285 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001286
Tejun Heo1d3650f2013-01-09 08:05:11 -08001287 /*
1288 * Compound ->weight walking up the tree. Both activation and
1289 * vfraction calculation are done in the same loop. Propagation
1290 * stops once an already activated node is met. vfraction
1291 * calculation should always continue to the root.
1292 */
1293 while ((parent = cfqg_flat_parent(pos))) {
1294 if (propagate) {
1295 propagate = !parent->nr_active++;
1296 parent->children_weight += pos->weight;
1297 }
1298 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001299 pos = parent;
1300 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001301
1302 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001303}
1304
1305static void
1306cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001307{
1308 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1309 struct cfq_group *__cfqg;
1310 struct rb_node *n;
1311
1312 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001313 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001314 return;
1315
1316 /*
1317 * Currently put the group at the end. Later implement something
1318 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001319 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001320 */
1321 n = rb_last(&st->rb);
1322 if (n) {
1323 __cfqg = rb_entry_cfqg(n);
1324 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1325 } else
1326 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001327 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001328}
1329
1330static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001331cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1332{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001333 struct cfq_group *pos = cfqg;
1334 bool propagate;
1335
1336 /*
1337 * Undo activation from cfq_group_service_tree_add(). Deactivate
1338 * @cfqg and propagate deactivation upwards.
1339 */
1340 propagate = !--pos->nr_active;
1341 pos->children_weight -= pos->leaf_weight;
1342
1343 while (propagate) {
1344 struct cfq_group *parent = cfqg_flat_parent(pos);
1345
1346 /* @pos has 0 nr_active at this point */
1347 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001348 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001349
1350 if (!parent)
1351 break;
1352
1353 propagate = !--parent->nr_active;
1354 parent->children_weight -= pos->weight;
1355 pos = parent;
1356 }
1357
1358 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001359 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1360 cfq_rb_erase(&cfqg->rb_node, st);
1361}
1362
1363static void
1364cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001365{
1366 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1367
1368 BUG_ON(cfqg->nr_cfqq < 1);
1369 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001370
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001371 /* If there are other cfq queues under this group, don't delete it */
1372 if (cfqg->nr_cfqq)
1373 return;
1374
Vivek Goyal2868ef72009-12-03 12:59:48 -05001375 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001376 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001377 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001378 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001379}
1380
Justin TerAvest167400d2011-03-12 16:54:00 +01001381static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1382 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001383{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001384 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001385
1386 /*
1387 * Queue got expired before even a single request completed or
1388 * got expired immediately after first request completion.
1389 */
1390 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1391 /*
1392 * Also charge the seek time incurred to the group, otherwise
1393 * if there are mutiple queues in the group, each can dispatch
1394 * a single request on seeky media and cause lots of seek time
1395 * and group will never know it.
1396 */
1397 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1398 1);
1399 } else {
1400 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001401 if (slice_used > cfqq->allocated_slice) {
1402 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001403 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001404 }
1405 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1406 *unaccounted_time += cfqq->slice_start -
1407 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001408 }
1409
Vivek Goyaldae739e2009-12-03 12:59:45 -05001410 return slice_used;
1411}
1412
1413static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001414 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001415{
1416 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001417 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001418 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1419 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001420 unsigned int vfr;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001421
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001422 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001423 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001424
Vivek Goyal02b35082010-08-23 12:23:53 +02001425 if (iops_mode(cfqd))
1426 charge = cfqq->slice_dispatch;
1427 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1428 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001429
Tejun Heo1d3650f2013-01-09 08:05:11 -08001430 /*
1431 * Can't update vdisktime while on service tree and cfqg->vfraction
1432 * is valid only while on it. Cache vfr, leave the service tree,
1433 * update vdisktime and go back on. The re-addition to the tree
1434 * will also update the weights as necessary.
1435 */
1436 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001437 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001438 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001439 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001440
1441 /* This group is being expired. Save the context */
1442 if (time_after(cfqd->workload_expires, jiffies)) {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001443 cfqg->saved_wl_slice = cfqd->workload_expires
Vivek Goyaldae739e2009-12-03 12:59:45 -05001444 - jiffies;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001445 cfqg->saved_wl_type = cfqd->serving_wl_type;
1446 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001447 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001448 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001449
1450 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1451 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001452 cfq_log_cfqq(cfqq->cfqd, cfqq,
1453 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1454 used_sl, cfqq->slice_dispatch, charge,
1455 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001456 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1457 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001458}
1459
Tejun Heof51b8022012-03-05 13:15:05 -08001460/**
1461 * cfq_init_cfqg_base - initialize base part of a cfq_group
1462 * @cfqg: cfq_group to initialize
1463 *
1464 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1465 * is enabled or not.
1466 */
1467static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1468{
1469 struct cfq_rb_root *st;
1470 int i, j;
1471
1472 for_each_cfqg_st(cfqg, i, j, st)
1473 *st = CFQ_RB_ROOT;
1474 RB_CLEAR_NODE(&cfqg->rb_node);
1475
1476 cfqg->ttime.last_end_request = jiffies;
1477}
1478
Vivek Goyal25fb5162009-12-03 12:59:46 -05001479#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07001480static void cfq_pd_init(struct blkcg_gq *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001481{
Tejun Heo03814112012-03-05 13:15:14 -08001482 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001483
Tejun Heof51b8022012-03-05 13:15:05 -08001484 cfq_init_cfqg_base(cfqg);
Tejun Heo3381cb82012-04-01 14:38:44 -07001485 cfqg->weight = blkg->blkcg->cfq_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001486 cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001487}
1488
1489/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001490 * Search for the cfq group current task belongs to. request_queue lock must
1491 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001492 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001493static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001494 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001495{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001496 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001497 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001498
Tejun Heo3c798392012-04-16 13:57:25 -07001499 /* avoid lookup for the common case where there's no blkcg */
1500 if (blkcg == &blkcg_root) {
Tejun Heocd1604f2012-03-05 13:15:06 -08001501 cfqg = cfqd->root_group;
1502 } else {
Tejun Heo3c798392012-04-16 13:57:25 -07001503 struct blkcg_gq *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001504
Tejun Heo3c96cb32012-04-13 13:11:34 -07001505 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -08001506 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001507 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001508 }
1509
Vivek Goyal25fb5162009-12-03 12:59:46 -05001510 return cfqg;
1511}
1512
1513static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1514{
1515 /* Currently, all async queues are mapped to root group */
1516 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001517 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001518
1519 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001520 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001521 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001522}
1523
Tejun Heof95a04a2012-04-16 13:57:26 -07001524static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1525 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001526{
Tejun Heof95a04a2012-04-16 13:57:26 -07001527 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001528
1529 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001530 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001531 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001532}
1533
Tejun Heo3381cb82012-04-01 14:38:44 -07001534static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1535 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001536{
Tejun Heo3c798392012-04-16 13:57:25 -07001537 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1538 cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001539 false);
1540 return 0;
1541}
1542
Tejun Heoe71357e2013-01-09 08:05:10 -08001543static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1544 struct blkg_policy_data *pd, int off)
1545{
1546 struct cfq_group *cfqg = pd_to_cfqg(pd);
1547
1548 if (!cfqg->dev_leaf_weight)
1549 return 0;
1550 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1551}
1552
1553static int cfqg_print_leaf_weight_device(struct cgroup *cgrp,
1554 struct cftype *cft,
1555 struct seq_file *sf)
1556{
1557 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1558 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0,
1559 false);
1560 return 0;
1561}
1562
Tejun Heo3381cb82012-04-01 14:38:44 -07001563static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
1564 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001565{
Tejun Heo3c798392012-04-16 13:57:25 -07001566 seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001567 return 0;
1568}
1569
Tejun Heoe71357e2013-01-09 08:05:10 -08001570static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft,
1571 struct seq_file *sf)
1572{
1573 seq_printf(sf, "%u\n",
1574 cgroup_to_blkcg(cgrp)->cfq_leaf_weight);
1575 return 0;
1576}
1577
1578static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1579 const char *buf, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001580{
Tejun Heo3c798392012-04-16 13:57:25 -07001581 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001582 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001583 struct cfq_group *cfqg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001584 int ret;
1585
Tejun Heo3c798392012-04-16 13:57:25 -07001586 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001587 if (ret)
1588 return ret;
1589
1590 ret = -EINVAL;
Tejun Heo3381cb82012-04-01 14:38:44 -07001591 cfqg = blkg_to_cfqg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -07001592 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001593 if (!is_leaf_weight) {
1594 cfqg->dev_weight = ctx.v;
1595 cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
1596 } else {
1597 cfqg->dev_leaf_weight = ctx.v;
1598 cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
1599 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001600 ret = 0;
1601 }
1602
1603 blkg_conf_finish(&ctx);
1604 return ret;
1605}
1606
Tejun Heoe71357e2013-01-09 08:05:10 -08001607static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1608 const char *buf)
1609{
1610 return __cfqg_set_weight_device(cgrp, cft, buf, false);
1611}
1612
1613static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft,
1614 const char *buf)
1615{
1616 return __cfqg_set_weight_device(cgrp, cft, buf, true);
1617}
1618
1619static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val,
1620 bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001621{
Tejun Heo3c798392012-04-16 13:57:25 -07001622 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
1623 struct blkcg_gq *blkg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001624 struct hlist_node *n;
1625
Tejun Heo3381cb82012-04-01 14:38:44 -07001626 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001627 return -EINVAL;
1628
1629 spin_lock_irq(&blkcg->lock);
Tejun Heoe71357e2013-01-09 08:05:10 -08001630
1631 if (!is_leaf_weight)
1632 blkcg->cfq_weight = val;
1633 else
1634 blkcg->cfq_leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001635
1636 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001637 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001638
Tejun Heoe71357e2013-01-09 08:05:10 -08001639 if (!cfqg)
1640 continue;
1641
1642 if (!is_leaf_weight) {
1643 if (!cfqg->dev_weight)
1644 cfqg->new_weight = blkcg->cfq_weight;
1645 } else {
1646 if (!cfqg->dev_leaf_weight)
1647 cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
1648 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001649 }
1650
1651 spin_unlock_irq(&blkcg->lock);
1652 return 0;
1653}
1654
Tejun Heoe71357e2013-01-09 08:05:10 -08001655static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1656{
1657 return __cfq_set_weight(cgrp, cft, val, false);
1658}
1659
1660static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1661{
1662 return __cfq_set_weight(cgrp, cft, val, true);
1663}
1664
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001665static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
1666 struct seq_file *sf)
1667{
Tejun Heo3c798392012-04-16 13:57:25 -07001668 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001669
Tejun Heo3c798392012-04-16 13:57:25 -07001670 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001671 cft->private, false);
1672 return 0;
1673}
1674
1675static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
1676 struct seq_file *sf)
1677{
Tejun Heo3c798392012-04-16 13:57:25 -07001678 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001679
Tejun Heo3c798392012-04-16 13:57:25 -07001680 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001681 cft->private, true);
1682 return 0;
1683}
1684
Tejun Heo60c2bc22012-04-01 14:38:43 -07001685#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001686static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1687 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001688{
Tejun Heof95a04a2012-04-16 13:57:26 -07001689 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001690 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001691 u64 v = 0;
1692
1693 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001694 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001695 do_div(v, samples);
1696 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001697 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001698 return 0;
1699}
1700
1701/* print avg_queue_size */
Tejun Heo155fead2012-04-01 14:38:44 -07001702static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1703 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001704{
Tejun Heo3c798392012-04-16 13:57:25 -07001705 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001706
Tejun Heo155fead2012-04-01 14:38:44 -07001707 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
Tejun Heo3c798392012-04-16 13:57:25 -07001708 &blkcg_policy_cfq, 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001709 return 0;
1710}
1711#endif /* CONFIG_DEBUG_BLK_CGROUP */
1712
1713static struct cftype cfq_blkcg_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001714 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001715 {
1716 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001717 .flags = CFTYPE_ONLY_ON_ROOT,
1718 .read_seq_string = cfqg_print_leaf_weight_device,
1719 .write_string = cfqg_set_leaf_weight_device,
1720 .max_write_len = 256,
1721 },
1722 {
1723 .name = "weight",
1724 .flags = CFTYPE_ONLY_ON_ROOT,
1725 .read_seq_string = cfq_print_leaf_weight,
1726 .write_u64 = cfq_set_leaf_weight,
1727 },
1728
1729 /* no such mapping necessary for !roots */
1730 {
1731 .name = "weight_device",
1732 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo3381cb82012-04-01 14:38:44 -07001733 .read_seq_string = cfqg_print_weight_device,
1734 .write_string = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001735 .max_write_len = 256,
1736 },
1737 {
1738 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001739 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001740 .read_seq_string = cfq_print_weight,
1741 .write_u64 = cfq_set_weight,
1742 },
1743
1744 {
1745 .name = "leaf_weight_device",
Tejun Heoe71357e2013-01-09 08:05:10 -08001746 .read_seq_string = cfqg_print_leaf_weight_device,
1747 .write_string = cfqg_set_leaf_weight_device,
1748 .max_write_len = 256,
1749 },
1750 {
1751 .name = "leaf_weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001752 .read_seq_string = cfq_print_leaf_weight,
1753 .write_u64 = cfq_set_leaf_weight,
1754 },
1755
Tejun Heo60c2bc22012-04-01 14:38:43 -07001756 {
1757 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001758 .private = offsetof(struct cfq_group, stats.time),
1759 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001760 },
1761 {
1762 .name = "sectors",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001763 .private = offsetof(struct cfq_group, stats.sectors),
1764 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001765 },
1766 {
1767 .name = "io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001768 .private = offsetof(struct cfq_group, stats.service_bytes),
1769 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001770 },
1771 {
1772 .name = "io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001773 .private = offsetof(struct cfq_group, stats.serviced),
1774 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001775 },
1776 {
1777 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001778 .private = offsetof(struct cfq_group, stats.service_time),
1779 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001780 },
1781 {
1782 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001783 .private = offsetof(struct cfq_group, stats.wait_time),
1784 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001785 },
1786 {
1787 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001788 .private = offsetof(struct cfq_group, stats.merged),
1789 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001790 },
1791 {
1792 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001793 .private = offsetof(struct cfq_group, stats.queued),
1794 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001795 },
1796#ifdef CONFIG_DEBUG_BLK_CGROUP
1797 {
1798 .name = "avg_queue_size",
Tejun Heo155fead2012-04-01 14:38:44 -07001799 .read_seq_string = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001800 },
1801 {
1802 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001803 .private = offsetof(struct cfq_group, stats.group_wait_time),
1804 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001805 },
1806 {
1807 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001808 .private = offsetof(struct cfq_group, stats.idle_time),
1809 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001810 },
1811 {
1812 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001813 .private = offsetof(struct cfq_group, stats.empty_time),
1814 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001815 },
1816 {
1817 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001818 .private = offsetof(struct cfq_group, stats.dequeue),
1819 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001820 },
1821 {
1822 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001823 .private = offsetof(struct cfq_group, stats.unaccounted_time),
1824 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001825 },
1826#endif /* CONFIG_DEBUG_BLK_CGROUP */
1827 { } /* terminate */
1828};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001829#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001830static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001831 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001832{
Tejun Heof51b8022012-03-05 13:15:05 -08001833 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001834}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001835
Vivek Goyal25fb5162009-12-03 12:59:46 -05001836static inline void
1837cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1838 cfqq->cfqg = cfqg;
1839}
1840
1841#endif /* GROUP_IOSCHED */
1842
Jens Axboe498d3aa22007-04-26 12:54:48 +02001843/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001844 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001845 * requests waiting to be processed. It is sorted in the order that
1846 * we will service the queues.
1847 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001848static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001849 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001850{
Jens Axboe08717142008-01-28 11:38:15 +01001851 struct rb_node **p, *parent;
1852 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001853 unsigned long rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001854 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001855 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001856 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001857
Vivek Goyal34b98d02012-10-03 16:56:58 -04001858 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001859 if (cfq_class_idle(cfqq)) {
1860 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001861 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001862 if (parent && parent != &cfqq->rb_node) {
1863 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1864 rb_key += __cfqq->rb_key;
1865 } else
1866 rb_key += jiffies;
1867 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001868 /*
1869 * Get our rb key offset. Subtract any residual slice
1870 * value carried from last service. A negative resid
1871 * count indicates slice overrun, and this should position
1872 * the next service time further away in the tree.
1873 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001874 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001875 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001876 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001877 } else {
1878 rb_key = -HZ;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001879 __cfqq = cfq_rb_first(st);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001880 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1881 }
Jens Axboed9e76202007-04-20 14:27:50 +02001882
1883 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001884 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001885 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001886 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001887 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04001888 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02001889 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001890
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001891 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1892 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001893 }
Jens Axboed9e76202007-04-20 14:27:50 +02001894
Jens Axboe498d3aa22007-04-26 12:54:48 +02001895 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001896 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001897 cfqq->service_tree = st;
1898 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001899 while (*p) {
1900 parent = *p;
1901 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1902
Jens Axboe0c534e02007-04-18 20:01:57 +02001903 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001904 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001905 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001906 if (time_before(rb_key, __cfqq->rb_key))
Vivek Goyal1f23f122012-10-03 16:57:00 -04001907 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001908 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04001909 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001910 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001911 }
Jens Axboed9e76202007-04-20 14:27:50 +02001912 }
1913
Jens Axboecc09e292007-04-26 12:53:50 +02001914 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04001915 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001916
Jens Axboed9e76202007-04-20 14:27:50 +02001917 cfqq->rb_key = rb_key;
1918 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04001919 rb_insert_color(&cfqq->rb_node, &st->rb);
1920 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001921 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001922 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001923 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Jens Axboea36e71f2009-04-15 12:15:11 +02001926static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001927cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1928 sector_t sector, struct rb_node **ret_parent,
1929 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001930{
Jens Axboea36e71f2009-04-15 12:15:11 +02001931 struct rb_node **p, *parent;
1932 struct cfq_queue *cfqq = NULL;
1933
1934 parent = NULL;
1935 p = &root->rb_node;
1936 while (*p) {
1937 struct rb_node **n;
1938
1939 parent = *p;
1940 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1941
1942 /*
1943 * Sort strictly based on sector. Smallest to the left,
1944 * largest to the right.
1945 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001946 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001947 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001948 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001949 n = &(*p)->rb_left;
1950 else
1951 break;
1952 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001953 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001954 }
1955
1956 *ret_parent = parent;
1957 if (rb_link)
1958 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001959 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001960}
1961
1962static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1963{
Jens Axboea36e71f2009-04-15 12:15:11 +02001964 struct rb_node **p, *parent;
1965 struct cfq_queue *__cfqq;
1966
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001967 if (cfqq->p_root) {
1968 rb_erase(&cfqq->p_node, cfqq->p_root);
1969 cfqq->p_root = NULL;
1970 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001971
1972 if (cfq_class_idle(cfqq))
1973 return;
1974 if (!cfqq->next_rq)
1975 return;
1976
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001977 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001978 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1979 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001980 if (!__cfqq) {
1981 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001982 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1983 } else
1984 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001985}
1986
Jens Axboe498d3aa22007-04-26 12:54:48 +02001987/*
1988 * Update cfqq's position in the service tree.
1989 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001990static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001991{
Jens Axboe6d048f52007-04-25 12:44:27 +02001992 /*
1993 * Resorting requires the cfqq to be on the RR list already.
1994 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001995 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001996 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001997 cfq_prio_tree_add(cfqd, cfqq);
1998 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001999}
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/*
2002 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002003 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Jens Axboefebffd62008-01-28 13:19:43 +01002005static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Jens Axboe7b679132008-05-30 12:23:07 +02002007 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002008 BUG_ON(cfq_cfqq_on_rr(cfqq));
2009 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002011 if (cfq_cfqq_sync(cfqq))
2012 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Jens Axboeedd75ff2007-04-19 12:03:34 +02002014 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Jens Axboe498d3aa22007-04-26 12:54:48 +02002017/*
2018 * Called when the cfqq no longer has requests pending, remove it from
2019 * the service tree.
2020 */
Jens Axboefebffd62008-01-28 13:19:43 +01002021static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Jens Axboe7b679132008-05-30 12:23:07 +02002023 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002024 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2025 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002027 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2028 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2029 cfqq->service_tree = NULL;
2030 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002031 if (cfqq->p_root) {
2032 rb_erase(&cfqq->p_node, cfqq->p_root);
2033 cfqq->p_root = NULL;
2034 }
Jens Axboed9e76202007-04-20 14:27:50 +02002035
Justin TerAvest8184f932011-03-17 16:12:36 +01002036 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 BUG_ON(!cfqd->busy_queues);
2038 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002039 if (cfq_cfqq_sync(cfqq))
2040 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
2043/*
2044 * rb tree support functions
2045 */
Jens Axboefebffd62008-01-28 13:19:43 +01002046static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Jens Axboe5e705372006-07-13 12:39:25 +02002048 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002049 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Jens Axboeb4878f22005-10-20 16:42:29 +02002051 BUG_ON(!cfqq->queued[sync]);
2052 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Jens Axboe5e705372006-07-13 12:39:25 +02002054 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Vivek Goyalf04a6422009-12-03 12:59:40 -05002056 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2057 /*
2058 * Queue will be deleted from service tree when we actually
2059 * expire it later. Right now just remove it from prio tree
2060 * as it is empty.
2061 */
2062 if (cfqq->p_root) {
2063 rb_erase(&cfqq->p_node, cfqq->p_root);
2064 cfqq->p_root = NULL;
2065 }
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Jens Axboe5e705372006-07-13 12:39:25 +02002069static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Jens Axboe5e705372006-07-13 12:39:25 +02002071 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002073 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Jens Axboe5380a102006-07-13 12:37:56 +02002075 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Jeff Moyer796d5112011-06-02 21:19:05 +02002077 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002078
2079 if (!cfq_cfqq_on_rr(cfqq))
2080 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002081
2082 /*
2083 * check if this request is a better next-serve candidate
2084 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002085 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002086 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002087
2088 /*
2089 * adjust priority tree position, if ->next_rq changes
2090 */
2091 if (prev != cfqq->next_rq)
2092 cfq_prio_tree_add(cfqd, cfqq);
2093
Jens Axboe5044eed2007-04-25 11:53:48 +02002094 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Jens Axboefebffd62008-01-28 13:19:43 +01002097static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Jens Axboe5380a102006-07-13 12:37:56 +02002099 elv_rb_del(&cfqq->sort_list, rq);
2100 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo155fead2012-04-01 14:38:44 -07002101 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002102 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002103 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2104 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Jens Axboe206dc692006-03-28 13:03:44 +02002107static struct request *
2108cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Jens Axboe206dc692006-03-28 13:03:44 +02002110 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002111 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002112 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Jens Axboe4ac845a2008-01-24 08:44:49 +01002114 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002115 if (!cic)
2116 return NULL;
2117
2118 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02002119 if (cfqq) {
2120 sector_t sector = bio->bi_sector + bio_sectors(bio);
2121
Jens Axboe21183b02006-07-13 12:33:14 +02002122 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return NULL;
2126}
2127
Jens Axboe165125e2007-07-24 09:28:11 +02002128static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002129{
2130 struct cfq_data *cfqd = q->elevator->elevator_data;
2131
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002132 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002133 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002134 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002135
Tejun Heo5b936292009-05-07 22:24:38 +09002136 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002137}
2138
Jens Axboe165125e2007-07-24 09:28:11 +02002139static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Jens Axboe22e2c502005-06-27 10:55:12 +02002141 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002143 WARN_ON(!cfqd->rq_in_driver);
2144 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002145 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002146 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Jens Axboeb4878f22005-10-20 16:42:29 +02002149static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Jens Axboe5e705372006-07-13 12:39:25 +02002151 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002152
Jens Axboe5e705372006-07-13 12:39:25 +02002153 if (cfqq->next_rq == rq)
2154 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Jens Axboeb4878f22005-10-20 16:42:29 +02002156 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002157 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002158
Aaron Carroll45333d52008-08-26 15:52:36 +02002159 cfqq->cfqd->rq_queued--;
Tejun Heo155fead2012-04-01 14:38:44 -07002160 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002161 if (rq->cmd_flags & REQ_PRIO) {
2162 WARN_ON(!cfqq->prio_pending);
2163 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Jens Axboe165125e2007-07-24 09:28:11 +02002167static int cfq_merge(struct request_queue *q, struct request **req,
2168 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct cfq_data *cfqd = q->elevator->elevator_data;
2171 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Jens Axboe206dc692006-03-28 13:03:44 +02002173 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002174 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002175 *req = __rq;
2176 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178
2179 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Jens Axboe165125e2007-07-24 09:28:11 +02002182static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02002183 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Jens Axboe21183b02006-07-13 12:33:14 +02002185 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002186 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jens Axboe5e705372006-07-13 12:39:25 +02002188 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Divyesh Shah812d4022010-04-08 21:14:23 -07002192static void cfq_bio_merged(struct request_queue *q, struct request *req,
2193 struct bio *bio)
2194{
Tejun Heo155fead2012-04-01 14:38:44 -07002195 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
Divyesh Shah812d4022010-04-08 21:14:23 -07002196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198static void
Jens Axboe165125e2007-07-24 09:28:11 +02002199cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct request *next)
2201{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002202 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002203 struct cfq_data *cfqd = q->elevator->elevator_data;
2204
Jens Axboe22e2c502005-06-27 10:55:12 +02002205 /*
2206 * reposition in fifo if next is older than rq
2207 */
2208 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002209 time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
2210 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002211 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02002212 rq_set_fifo_time(rq, rq_fifo_time(next));
2213 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002214
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002215 if (cfqq->next_rq == next)
2216 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002217 cfq_remove_request(next);
Tejun Heo155fead2012-04-01 14:38:44 -07002218 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002219
2220 cfqq = RQ_CFQQ(next);
2221 /*
2222 * all requests of this queue are merged to other queues, delete it
2223 * from the service tree. If it's the active_queue,
2224 * cfq_dispatch_requests() will choose to expire it or do idle
2225 */
2226 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2227 cfqq != cfqd->active_queue)
2228 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002229}
2230
Jens Axboe165125e2007-07-24 09:28:11 +02002231static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002232 struct bio *bio)
2233{
2234 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002235 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002236 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002237
2238 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002239 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002240 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002241 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002242 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002243
2244 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002245 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002246 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002247 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002248 cic = cfq_cic_lookup(cfqd, current->io_context);
2249 if (!cic)
2250 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002251
Vasily Tarasov91fac312007-04-25 12:29:51 +02002252 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002253 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002254}
2255
Divyesh Shah812df482010-04-08 21:15:35 -07002256static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2257{
2258 del_timer(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002259 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002260}
2261
Jens Axboefebffd62008-01-28 13:19:43 +01002262static void __cfq_set_active_queue(struct cfq_data *cfqd,
2263 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002264{
2265 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002266 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002267 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002268 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002269 cfqq->slice_start = 0;
2270 cfqq->dispatch_start = jiffies;
2271 cfqq->allocated_slice = 0;
2272 cfqq->slice_end = 0;
2273 cfqq->slice_dispatch = 0;
2274 cfqq->nr_sectors = 0;
2275
2276 cfq_clear_cfqq_wait_request(cfqq);
2277 cfq_clear_cfqq_must_dispatch(cfqq);
2278 cfq_clear_cfqq_must_alloc_slice(cfqq);
2279 cfq_clear_cfqq_fifo_expire(cfqq);
2280 cfq_mark_cfqq_slice_new(cfqq);
2281
2282 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002283 }
2284
2285 cfqd->active_queue = cfqq;
2286}
2287
2288/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002289 * current cfqq expired its slice (or was too idle), select new one
2290 */
2291static void
2292__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002293 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002294{
Jens Axboe7b679132008-05-30 12:23:07 +02002295 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2296
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002297 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002298 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002299
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002300 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002301 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002302
2303 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002304 * If this cfqq is shared between multiple processes, check to
2305 * make sure that those processes are still issuing I/Os within
2306 * the mean seek distance. If not, it may be time to break the
2307 * queues apart again.
2308 */
2309 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2310 cfq_mark_cfqq_split_coop(cfqq);
2311
2312 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002313 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002314 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002315 if (timed_out) {
2316 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002317 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002318 else
2319 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002320 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2321 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002322
Vivek Goyale5ff0822010-04-26 19:25:11 +02002323 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002324
Vivek Goyalf04a6422009-12-03 12:59:40 -05002325 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2326 cfq_del_cfqq_rr(cfqd, cfqq);
2327
Jens Axboeedd75ff2007-04-19 12:03:34 +02002328 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002329
2330 if (cfqq == cfqd->active_queue)
2331 cfqd->active_queue = NULL;
2332
2333 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002334 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002335 cfqd->active_cic = NULL;
2336 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002337}
2338
Vivek Goyale5ff0822010-04-26 19:25:11 +02002339static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002340{
2341 struct cfq_queue *cfqq = cfqd->active_queue;
2342
2343 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002344 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002345}
2346
Jens Axboe498d3aa22007-04-26 12:54:48 +02002347/*
2348 * Get next queue for service. Unless we have a queue preemption,
2349 * we'll simply select the first cfqq in the service tree.
2350 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002351static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002352{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002353 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2354 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002355
Vivek Goyalf04a6422009-12-03 12:59:40 -05002356 if (!cfqd->rq_queued)
2357 return NULL;
2358
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002359 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002360 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002361 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002362 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002363 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002364 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002365}
2366
Vivek Goyalf04a6422009-12-03 12:59:40 -05002367static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2368{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002369 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002370 struct cfq_queue *cfqq;
2371 int i, j;
2372 struct cfq_rb_root *st;
2373
2374 if (!cfqd->rq_queued)
2375 return NULL;
2376
Vivek Goyal25fb5162009-12-03 12:59:46 -05002377 cfqg = cfq_get_next_cfqg(cfqd);
2378 if (!cfqg)
2379 return NULL;
2380
Vivek Goyalf04a6422009-12-03 12:59:40 -05002381 for_each_cfqg_st(cfqg, i, j, st)
2382 if ((cfqq = cfq_rb_first(st)) != NULL)
2383 return cfqq;
2384 return NULL;
2385}
2386
Jens Axboe498d3aa22007-04-26 12:54:48 +02002387/*
2388 * Get and set a new active queue for service.
2389 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002390static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2391 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002392{
Jens Axboee00ef792009-11-04 08:54:55 +01002393 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002394 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002395
Jens Axboe22e2c502005-06-27 10:55:12 +02002396 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002397 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002398}
2399
Jens Axboed9e76202007-04-20 14:27:50 +02002400static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2401 struct request *rq)
2402{
Tejun Heo83096eb2009-05-07 22:24:39 +09002403 if (blk_rq_pos(rq) >= cfqd->last_position)
2404 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002405 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002406 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002407}
2408
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002409static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002410 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002411{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002412 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002413}
2414
Jens Axboea36e71f2009-04-15 12:15:11 +02002415static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2416 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002417{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002418 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002419 struct rb_node *parent, *node;
2420 struct cfq_queue *__cfqq;
2421 sector_t sector = cfqd->last_position;
2422
2423 if (RB_EMPTY_ROOT(root))
2424 return NULL;
2425
2426 /*
2427 * First, if we find a request starting at the end of the last
2428 * request, choose it.
2429 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002430 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002431 if (__cfqq)
2432 return __cfqq;
2433
2434 /*
2435 * If the exact sector wasn't found, the parent of the NULL leaf
2436 * will contain the closest sector.
2437 */
2438 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002439 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002440 return __cfqq;
2441
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002442 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002443 node = rb_next(&__cfqq->p_node);
2444 else
2445 node = rb_prev(&__cfqq->p_node);
2446 if (!node)
2447 return NULL;
2448
2449 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002450 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002451 return __cfqq;
2452
2453 return NULL;
2454}
2455
2456/*
2457 * cfqd - obvious
2458 * cur_cfqq - passed in so that we don't decide that the current queue is
2459 * closely cooperating with itself.
2460 *
2461 * So, basically we're assuming that that cur_cfqq has dispatched at least
2462 * one request, and that cfqd->last_position reflects a position on the disk
2463 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2464 * assumption.
2465 */
2466static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002467 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002468{
2469 struct cfq_queue *cfqq;
2470
Divyesh Shah39c01b22010-03-25 15:45:57 +01002471 if (cfq_class_idle(cur_cfqq))
2472 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002473 if (!cfq_cfqq_sync(cur_cfqq))
2474 return NULL;
2475 if (CFQQ_SEEKY(cur_cfqq))
2476 return NULL;
2477
Jens Axboea36e71f2009-04-15 12:15:11 +02002478 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002479 * Don't search priority tree if it's the only queue in the group.
2480 */
2481 if (cur_cfqq->cfqg->nr_cfqq == 1)
2482 return NULL;
2483
2484 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002485 * We should notice if some of the queues are cooperating, eg
2486 * working closely on the same area of the disk. In that case,
2487 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002488 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002489 cfqq = cfqq_close(cfqd, cur_cfqq);
2490 if (!cfqq)
2491 return NULL;
2492
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002493 /* If new queue belongs to different cfq_group, don't choose it */
2494 if (cur_cfqq->cfqg != cfqq->cfqg)
2495 return NULL;
2496
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002497 /*
2498 * It only makes sense to merge sync queues.
2499 */
2500 if (!cfq_cfqq_sync(cfqq))
2501 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002502 if (CFQQ_SEEKY(cfqq))
2503 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002504
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002505 /*
2506 * Do not merge queues of different priority classes
2507 */
2508 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2509 return NULL;
2510
Jens Axboea36e71f2009-04-15 12:15:11 +02002511 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002512}
2513
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002514/*
2515 * Determine whether we should enforce idle window for this queue.
2516 */
2517
2518static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2519{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002520 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002521 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002522
Vivek Goyal34b98d02012-10-03 16:56:58 -04002523 BUG_ON(!st);
2524 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002525
Vivek Goyalb6508c12010-08-23 12:23:33 +02002526 if (!cfqd->cfq_slice_idle)
2527 return false;
2528
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002529 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002530 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002531 return false;
2532
2533 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002534 if (cfq_cfqq_idle_window(cfqq) &&
2535 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002536 return true;
2537
2538 /*
2539 * Otherwise, we do only if they are the last ones
2540 * in their service tree.
2541 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002542 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2543 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002544 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002545 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002546 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002547}
2548
Jens Axboe6d048f52007-04-25 12:44:27 +02002549static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002550{
Jens Axboe17926692007-01-19 11:59:30 +11002551 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002552 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002553 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002554
Jens Axboea68bbdd2008-09-24 13:03:33 +02002555 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002556 * SSD device without seek penalty, disable idling. But only do so
2557 * for devices that support queuing, otherwise we still have a problem
2558 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002559 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002560 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002561 return;
2562
Jens Axboedd67d052006-06-21 09:36:18 +02002563 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002564 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002565
2566 /*
2567 * idle is disabled, either manually or by past process history
2568 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002569 if (!cfq_should_idle(cfqd, cfqq)) {
2570 /* no queue idling. Check for group idling */
2571 if (cfqd->cfq_group_idle)
2572 group_idle = cfqd->cfq_group_idle;
2573 else
2574 return;
2575 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002576
Jens Axboe22e2c502005-06-27 10:55:12 +02002577 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002578 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002579 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002580 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002581 return;
2582
2583 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002584 * task has exited, don't wait
2585 */
Jens Axboe206dc692006-03-28 13:03:44 +02002586 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002587 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002588 return;
2589
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002590 /*
2591 * If our average think time is larger than the remaining time
2592 * slice, then don't idle. This avoids overrunning the allotted
2593 * time slice.
2594 */
Shaohua Li383cd722011-07-12 14:24:35 +02002595 if (sample_valid(cic->ttime.ttime_samples) &&
2596 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002597 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002598 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002599 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002600 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002601
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002602 /* There are other queues in the group, don't do group idle */
2603 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2604 return;
2605
Jens Axboe3b181522005-06-27 10:56:24 +02002606 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002607
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002608 if (group_idle)
2609 sl = cfqd->cfq_group_idle;
2610 else
2611 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002612
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002613 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo155fead2012-04-01 14:38:44 -07002614 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002615 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2616 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Jens Axboe498d3aa22007-04-26 12:54:48 +02002619/*
2620 * Move request from internal lists to the request queue dispatch list.
2621 */
Jens Axboe165125e2007-07-24 09:28:11 +02002622static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002624 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002625 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002626
Jens Axboe7b679132008-05-30 12:23:07 +02002627 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2628
Jeff Moyer06d21882009-09-11 17:08:59 +02002629 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002630 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002631 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002632 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002633 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002634
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002635 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002636 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002637 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640/*
2641 * return expired entry, or NULL to just start from scratch in rbtree
2642 */
Jens Axboefebffd62008-01-28 13:19:43 +01002643static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Jens Axboe30996f42009-10-05 11:03:39 +02002645 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Jens Axboe3b181522005-06-27 10:56:24 +02002647 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002649
2650 cfq_mark_cfqq_fifo_expire(cfqq);
2651
Jens Axboe89850f72006-07-22 16:48:31 +02002652 if (list_empty(&cfqq->fifo))
2653 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Jens Axboe89850f72006-07-22 16:48:31 +02002655 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002656 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002657 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Jens Axboe30996f42009-10-05 11:03:39 +02002659 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002660 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Jens Axboe22e2c502005-06-27 10:55:12 +02002663static inline int
2664cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2665{
2666 const int base_rq = cfqd->cfq_slice_async_rq;
2667
2668 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2669
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002670 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002671}
2672
2673/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002674 * Must be called with the queue_lock held.
2675 */
2676static int cfqq_process_refs(struct cfq_queue *cfqq)
2677{
2678 int process_refs, io_refs;
2679
2680 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002681 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002682 BUG_ON(process_refs < 0);
2683 return process_refs;
2684}
2685
2686static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2687{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002688 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002689 struct cfq_queue *__cfqq;
2690
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002691 /*
2692 * If there are no process references on the new_cfqq, then it is
2693 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2694 * chain may have dropped their last reference (not just their
2695 * last process reference).
2696 */
2697 if (!cfqq_process_refs(new_cfqq))
2698 return;
2699
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002700 /* Avoid a circular list and skip interim queue merges */
2701 while ((__cfqq = new_cfqq->new_cfqq)) {
2702 if (__cfqq == cfqq)
2703 return;
2704 new_cfqq = __cfqq;
2705 }
2706
2707 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002708 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002709 /*
2710 * If the process for the cfqq has gone away, there is no
2711 * sense in merging the queues.
2712 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002713 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002714 return;
2715
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002716 /*
2717 * Merge in the direction of the lesser amount of work.
2718 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002719 if (new_process_refs >= process_refs) {
2720 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002721 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002722 } else {
2723 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002724 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002725 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002726}
2727
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002728static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002729 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002730{
2731 struct cfq_queue *queue;
2732 int i;
2733 bool key_valid = false;
2734 unsigned long lowest_key = 0;
2735 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2736
Vivek Goyal65b32a52009-12-16 17:52:59 -05002737 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2738 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002739 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002740 if (queue &&
2741 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2742 lowest_key = queue->rb_key;
2743 cur_best = i;
2744 key_valid = true;
2745 }
2746 }
2747
2748 return cur_best;
2749}
2750
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002751static void
2752choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002753{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002754 unsigned slice;
2755 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002756 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002757 unsigned group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002758 enum wl_class_t original_class = cfqd->serving_wl_class;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002759
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002760 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002761 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002762 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002763 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002764 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002765 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002766 cfqd->serving_wl_class = IDLE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002767 cfqd->workload_expires = jiffies + 1;
2768 return;
2769 }
2770
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002771 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002772 goto new_workload;
2773
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002774 /*
2775 * For RT and BE, we have to choose also the type
2776 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2777 * expiration time
2778 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002779 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002780 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002781
2782 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002783 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002784 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002785 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002786 return;
2787
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002788new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002789 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002790 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002791 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002792 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002793 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002794
2795 /*
2796 * the workload slice is computed as a fraction of target latency
2797 * proportional to the number of queues in that workload, over
2798 * all the queues in the same priority class
2799 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002800 group_slice = cfq_group_slice(cfqd, cfqg);
2801
2802 slice = group_slice * count /
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002803 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
2804 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002805 cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002806
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002807 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002808 unsigned int tmp;
2809
2810 /*
2811 * Async queues are currently system wide. Just taking
2812 * proportion of queues with-in same group will lead to higher
2813 * async ratio system wide as generally root group is going
2814 * to have higher weight. A more accurate thing would be to
2815 * calculate system wide asnc/sync ratio.
2816 */
Tao Ma5bf14c02012-04-01 14:33:39 -07002817 tmp = cfqd->cfq_target_latency *
2818 cfqg_busy_async_queues(cfqd, cfqg);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002819 tmp = tmp/cfqd->busy_queues;
2820 slice = min_t(unsigned, slice, tmp);
2821
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002822 /* async workload slice is scaled down according to
2823 * the sync/async slice ratio. */
2824 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002825 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002826 /* sync workload slice is at least 2 * cfq_slice_idle */
2827 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2828
2829 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002830 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002831 cfqd->workload_expires = jiffies + slice;
2832}
2833
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002834static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2835{
2836 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002837 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002838
2839 if (RB_EMPTY_ROOT(&st->rb))
2840 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002841 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002842 update_min_vdisktime(st);
2843 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002844}
2845
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002846static void cfq_choose_cfqg(struct cfq_data *cfqd)
2847{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002848 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2849
2850 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002851
2852 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002853 if (cfqg->saved_wl_slice) {
2854 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
2855 cfqd->serving_wl_type = cfqg->saved_wl_type;
2856 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002857 } else
2858 cfqd->workload_expires = jiffies - 1;
2859
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002860 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002861}
2862
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002863/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002864 * Select a queue for service. If we have a current active queue,
2865 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002866 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002867static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002868{
Jens Axboea36e71f2009-04-15 12:15:11 +02002869 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002870
2871 cfqq = cfqd->active_queue;
2872 if (!cfqq)
2873 goto new_queue;
2874
Vivek Goyalf04a6422009-12-03 12:59:40 -05002875 if (!cfqd->rq_queued)
2876 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002877
2878 /*
2879 * We were waiting for group to get backlogged. Expire the queue
2880 */
2881 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2882 goto expire;
2883
Jens Axboe22e2c502005-06-27 10:55:12 +02002884 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002885 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002886 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002887 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2888 /*
2889 * If slice had not expired at the completion of last request
2890 * we might not have turned on wait_busy flag. Don't expire
2891 * the queue yet. Allow the group to get backlogged.
2892 *
2893 * The very fact that we have used the slice, that means we
2894 * have been idling all along on this queue and it should be
2895 * ok to wait for this request to complete.
2896 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002897 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2898 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2899 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002900 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002901 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002902 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002903 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002904
2905 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002906 * The active queue has requests and isn't expired, allow it to
2907 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002908 */
Jens Axboedd67d052006-06-21 09:36:18 +02002909 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002910 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002911
2912 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002913 * If another queue has a request waiting within our mean seek
2914 * distance, let it run. The expire code will check for close
2915 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002916 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002917 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002918 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002919 if (new_cfqq) {
2920 if (!cfqq->new_cfqq)
2921 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002922 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002923 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002924
2925 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002926 * No requests pending. If the active queue still has requests in
2927 * flight or is idling for a new request, allow either of these
2928 * conditions to happen (or time out) before selecting a new queue.
2929 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002930 if (timer_pending(&cfqd->idle_slice_timer)) {
2931 cfqq = NULL;
2932 goto keep_queue;
2933 }
2934
Shaohua Li8e1ac662010-11-08 15:01:04 +01002935 /*
2936 * This is a deep seek queue, but the device is much faster than
2937 * the queue can deliver, don't idle
2938 **/
2939 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2940 (cfq_cfqq_slice_new(cfqq) ||
2941 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2942 cfq_clear_cfqq_deep(cfqq);
2943 cfq_clear_cfqq_idle_window(cfqq);
2944 }
2945
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002946 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2947 cfqq = NULL;
2948 goto keep_queue;
2949 }
2950
2951 /*
2952 * If group idle is enabled and there are requests dispatched from
2953 * this group, wait for requests to complete.
2954 */
2955check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002956 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2957 cfqq->cfqg->dispatched &&
2958 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002959 cfqq = NULL;
2960 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002961 }
2962
Jens Axboe3b181522005-06-27 10:56:24 +02002963expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002964 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002965new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002966 /*
2967 * Current queue expired. Check if we have to switch to a new
2968 * service tree
2969 */
2970 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002971 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002972
Jens Axboea36e71f2009-04-15 12:15:11 +02002973 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002974keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002975 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002976}
2977
Jens Axboefebffd62008-01-28 13:19:43 +01002978static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002979{
2980 int dispatched = 0;
2981
2982 while (cfqq->next_rq) {
2983 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2984 dispatched++;
2985 }
2986
2987 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002988
2989 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002990 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002991 return dispatched;
2992}
2993
Jens Axboe498d3aa22007-04-26 12:54:48 +02002994/*
2995 * Drain our current requests. Used for barriers and when switching
2996 * io schedulers on-the-fly.
2997 */
Jens Axboed9e76202007-04-20 14:27:50 +02002998static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002999{
Jens Axboe08717142008-01-28 11:38:15 +01003000 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003001 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003002
Divyesh Shah3440c492010-04-09 09:29:57 +02003003 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003004 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003005 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3006 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003007 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003008 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003009
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003010 BUG_ON(cfqd->busy_queues);
3011
Jeff Moyer69237152009-06-12 15:29:30 +02003012 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003013 return dispatched;
3014}
3015
Shaohua Liabc3c742010-03-01 09:20:54 +01003016static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3017 struct cfq_queue *cfqq)
3018{
3019 /* the queue hasn't finished any request, can't estimate */
3020 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003021 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003022 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
3023 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01003024 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003025
Shaohua Lic1e44752010-11-08 15:01:02 +01003026 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003027}
3028
Jens Axboe0b182d62009-10-06 20:49:37 +02003029static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003030{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003031 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Jens Axboe2f5cb732009-04-07 08:51:19 +02003033 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003034 * Drain async requests before we start sync IO
3035 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003036 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003037 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003038
3039 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003040 * If this is an async queue and we have sync IO in flight, let it wait
3041 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003042 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003043 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003044
Shaohua Liabc3c742010-03-01 09:20:54 +01003045 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003046 if (cfq_class_idle(cfqq))
3047 max_dispatch = 1;
3048
3049 /*
3050 * Does this cfqq already have too much IO in flight?
3051 */
3052 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003053 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003054 /*
3055 * idle queue must always only have a single IO in flight
3056 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003057 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003058 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003059
Jens Axboe2f5cb732009-04-07 08:51:19 +02003060 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003061 * If there is only one sync queue
3062 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003063 * queue no dispatch limit. The reason is a sync queue can
3064 * preempt async queue, limiting the sync queue doesn't make
3065 * sense. This is useful for aiostress test.
3066 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003067 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3068 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003069
3070 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003071 * We have other queues, don't allow more IO from this one
3072 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003073 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3074 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003075 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003076
Jens Axboe2f5cb732009-04-07 08:51:19 +02003077 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003078 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003079 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003080 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003081 max_dispatch = -1;
3082 else
3083 /*
3084 * Normally we start throttling cfqq when cfq_quantum/2
3085 * requests have been dispatched. But we can drive
3086 * deeper queue depths at the beginning of slice
3087 * subjected to upper limit of cfq_quantum.
3088 * */
3089 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003090 }
3091
3092 /*
3093 * Async queues must wait a bit before being allowed dispatch.
3094 * We also ramp up the dispatch depth gradually for async IO,
3095 * based on the last sync IO we serviced
3096 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003097 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003098 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003099 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003100
Jens Axboe61f0c1d2009-10-03 19:46:03 +02003101 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02003102 if (!depth && !cfqq->dispatched)
3103 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003104 if (depth < max_dispatch)
3105 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 }
3107
Jens Axboe0b182d62009-10-06 20:49:37 +02003108 /*
3109 * If we're below the current max, allow a dispatch
3110 */
3111 return cfqq->dispatched < max_dispatch;
3112}
3113
3114/*
3115 * Dispatch a request from cfqq, moving them to the request queue
3116 * dispatch list.
3117 */
3118static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3119{
3120 struct request *rq;
3121
3122 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3123
3124 if (!cfq_may_dispatch(cfqd, cfqq))
3125 return false;
3126
3127 /*
3128 * follow expired path, else get first next available
3129 */
3130 rq = cfq_check_fifo(cfqq);
3131 if (!rq)
3132 rq = cfqq->next_rq;
3133
3134 /*
3135 * insert request into driver dispatch list
3136 */
3137 cfq_dispatch_insert(cfqd->queue, rq);
3138
3139 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003140 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003141
Tejun Heoc5869802011-12-14 00:33:41 +01003142 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003143 cfqd->active_cic = cic;
3144 }
3145
3146 return true;
3147}
3148
3149/*
3150 * Find the cfqq that we need to service and move a request from that to the
3151 * dispatch list
3152 */
3153static int cfq_dispatch_requests(struct request_queue *q, int force)
3154{
3155 struct cfq_data *cfqd = q->elevator->elevator_data;
3156 struct cfq_queue *cfqq;
3157
3158 if (!cfqd->busy_queues)
3159 return 0;
3160
3161 if (unlikely(force))
3162 return cfq_forced_dispatch(cfqd);
3163
3164 cfqq = cfq_select_queue(cfqd);
3165 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003166 return 0;
3167
Jens Axboe2f5cb732009-04-07 08:51:19 +02003168 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003169 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003170 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003171 if (!cfq_dispatch_request(cfqd, cfqq))
3172 return 0;
3173
Jens Axboe2f5cb732009-04-07 08:51:19 +02003174 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003175 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003176
3177 /*
3178 * expire an async queue immediately if it has used up its slice. idle
3179 * queue always expire after 1 dispatch round.
3180 */
3181 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3182 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3183 cfq_class_idle(cfqq))) {
3184 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003185 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003186 }
3187
Shan Weib217a902009-09-01 10:06:42 +02003188 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192/*
Jens Axboe5e705372006-07-13 12:39:25 +02003193 * task holds one reference to the queue, dropped when task exits. each rq
3194 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003196 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * queue lock must be held here.
3198 */
3199static void cfq_put_queue(struct cfq_queue *cfqq)
3200{
Jens Axboe22e2c502005-06-27 10:55:12 +02003201 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003202 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003203
Shaohua Li30d7b942011-01-07 08:46:59 +01003204 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Shaohua Li30d7b942011-01-07 08:46:59 +01003206 cfqq->ref--;
3207 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return;
3209
Jens Axboe7b679132008-05-30 12:23:07 +02003210 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003212 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003213 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003215 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003216 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003217 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003218 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003219
Vivek Goyalf04a6422009-12-03 12:59:40 -05003220 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003222 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
Shaohua Lid02a2c02010-05-25 10:16:53 +02003225static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003226{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003227 struct cfq_queue *__cfqq, *next;
3228
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003229 /*
3230 * If this queue was scheduled to merge with another queue, be
3231 * sure to drop the reference taken on that queue (and others in
3232 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3233 */
3234 __cfqq = cfqq->new_cfqq;
3235 while (__cfqq) {
3236 if (__cfqq == cfqq) {
3237 WARN(1, "cfqq->new_cfqq loop detected\n");
3238 break;
3239 }
3240 next = __cfqq->new_cfqq;
3241 cfq_put_queue(__cfqq);
3242 __cfqq = next;
3243 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003244}
3245
3246static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3247{
3248 if (unlikely(cfqq == cfqd->active_queue)) {
3249 __cfq_slice_expired(cfqd, cfqq, 0);
3250 cfq_schedule_dispatch(cfqd);
3251 }
3252
3253 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003254
Jens Axboe89850f72006-07-22 16:48:31 +02003255 cfq_put_queue(cfqq);
3256}
3257
Tejun Heo9b84cac2011-12-14 00:33:42 +01003258static void cfq_init_icq(struct io_cq *icq)
3259{
3260 struct cfq_io_cq *cic = icq_to_cic(icq);
3261
3262 cic->ttime.last_end_request = jiffies;
3263}
3264
Tejun Heoc5869802011-12-14 00:33:41 +01003265static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003266{
Tejun Heoc5869802011-12-14 00:33:41 +01003267 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003268 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003269
Jens Axboeff6657c2009-04-08 10:58:57 +02003270 if (cic->cfqq[BLK_RW_ASYNC]) {
3271 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3272 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003273 }
3274
Jens Axboeff6657c2009-04-08 10:58:57 +02003275 if (cic->cfqq[BLK_RW_SYNC]) {
3276 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3277 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003278 }
Jens Axboe89850f72006-07-22 16:48:31 +02003279}
3280
Tejun Heoabede6d2012-03-19 15:10:57 -07003281static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003282{
3283 struct task_struct *tsk = current;
3284 int ioprio_class;
3285
Jens Axboe3b181522005-06-27 10:56:24 +02003286 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003287 return;
3288
Tejun Heo598971b2012-03-19 15:10:58 -07003289 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003290 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003291 default:
3292 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3293 case IOPRIO_CLASS_NONE:
3294 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003295 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003296 */
3297 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003298 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003299 break;
3300 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003301 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003302 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3303 break;
3304 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003305 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003306 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3307 break;
3308 case IOPRIO_CLASS_IDLE:
3309 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3310 cfqq->ioprio = 7;
3311 cfq_clear_cfqq_idle_window(cfqq);
3312 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003313 }
3314
3315 /*
3316 * keep track of original prio settings in case we have to temporarily
3317 * elevate the priority of this queue
3318 */
3319 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003320 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003321}
3322
Tejun Heo598971b2012-03-19 15:10:58 -07003323static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003324{
Tejun Heo598971b2012-03-19 15:10:58 -07003325 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003326 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003327 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003328
Tejun Heo598971b2012-03-19 15:10:58 -07003329 /*
3330 * Check whether ioprio has changed. The condition may trigger
3331 * spuriously on a newly created cic but there's no harm.
3332 */
3333 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003334 return;
3335
Jens Axboeff6657c2009-04-08 10:58:57 +02003336 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003337 if (cfqq) {
3338 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003339 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3340 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003341 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003342 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003343 cfq_put_queue(cfqq);
3344 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003345 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003346
Jens Axboeff6657c2009-04-08 10:58:57 +02003347 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003348 if (cfqq)
3349 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003350
3351 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003352}
3353
Jens Axboed5036d72009-06-26 10:44:34 +02003354static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003355 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003356{
3357 RB_CLEAR_NODE(&cfqq->rb_node);
3358 RB_CLEAR_NODE(&cfqq->p_node);
3359 INIT_LIST_HEAD(&cfqq->fifo);
3360
Shaohua Li30d7b942011-01-07 08:46:59 +01003361 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003362 cfqq->cfqd = cfqd;
3363
3364 cfq_mark_cfqq_prio_changed(cfqq);
3365
3366 if (is_sync) {
3367 if (!cfq_class_idle(cfqq))
3368 cfq_mark_cfqq_idle_window(cfqq);
3369 cfq_mark_cfqq_sync(cfqq);
3370 }
3371 cfqq->pid = pid;
3372}
3373
Vivek Goyal246103332009-12-03 12:59:51 -05003374#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003375static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003376{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003377 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003378 struct cfq_queue *sync_cfqq;
3379 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003380
Tejun Heo598971b2012-03-19 15:10:58 -07003381 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07003382 id = bio_blkcg(bio)->id;
Tejun Heo598971b2012-03-19 15:10:58 -07003383 rcu_read_unlock();
3384
3385 /*
3386 * Check whether blkcg has changed. The condition may trigger
3387 * spuriously on a newly created cic but there's no harm.
3388 */
3389 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003390 return;
3391
Tejun Heo598971b2012-03-19 15:10:58 -07003392 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003393 if (sync_cfqq) {
3394 /*
3395 * Drop reference to sync queue. A new sync queue will be
3396 * assigned in new group upon arrival of a fresh request.
3397 */
3398 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3399 cic_set_cfqq(cic, NULL, 1);
3400 cfq_put_queue(sync_cfqq);
3401 }
Tejun Heo598971b2012-03-19 15:10:58 -07003402
3403 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003404}
Tejun Heo598971b2012-03-19 15:10:58 -07003405#else
3406static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003407#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003410cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3411 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412{
Tejun Heo3c798392012-04-16 13:57:25 -07003413 struct blkcg *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003415 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
3417retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003418 rcu_read_lock();
3419
Tejun Heo3c798392012-04-16 13:57:25 -07003420 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003421 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003422 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Jens Axboe6118b702009-06-30 09:34:12 +02003424 /*
3425 * Always try a new alloc if we fell back to the OOM cfqq
3426 * originally, since it should just be a temporary situation.
3427 */
3428 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3429 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 if (new_cfqq) {
3431 cfqq = new_cfqq;
3432 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003433 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003434 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003436 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003437 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003438 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003440 if (new_cfqq)
3441 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003442 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003443 cfqq = kmem_cache_alloc_node(cfq_pool,
3444 gfp_mask | __GFP_ZERO,
3445 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
Jens Axboe6118b702009-06-30 09:34:12 +02003448 if (cfqq) {
3449 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003450 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003451 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003452 cfq_log_cfqq(cfqd, cfqq, "alloced");
3453 } else
3454 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
3456
3457 if (new_cfqq)
3458 kmem_cache_free(cfq_pool, new_cfqq);
3459
Tejun Heo2a7f1242012-03-05 13:15:01 -08003460 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 return cfqq;
3462}
3463
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003464static struct cfq_queue **
3465cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3466{
Jens Axboefe094d92008-01-31 13:08:54 +01003467 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003468 case IOPRIO_CLASS_RT:
3469 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003470 case IOPRIO_CLASS_NONE:
3471 ioprio = IOPRIO_NORM;
3472 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003473 case IOPRIO_CLASS_BE:
3474 return &cfqd->async_cfqq[1][ioprio];
3475 case IOPRIO_CLASS_IDLE:
3476 return &cfqd->async_idle_cfqq;
3477 default:
3478 BUG();
3479 }
3480}
3481
Jens Axboe15c31be2007-07-10 13:43:25 +02003482static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003483cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003484 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003485{
Tejun Heo598971b2012-03-19 15:10:58 -07003486 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3487 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003488 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003489 struct cfq_queue *cfqq = NULL;
3490
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003491 if (!is_sync) {
3492 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3493 cfqq = *async_cfqq;
3494 }
3495
Jens Axboe6118b702009-06-30 09:34:12 +02003496 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003497 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003498
3499 /*
3500 * pin the queue now that it's allocated, scheduler exit will prune it
3501 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003502 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003503 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003504 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003505 }
3506
Shaohua Li30d7b942011-01-07 08:46:59 +01003507 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003508 return cfqq;
3509}
3510
Jens Axboe22e2c502005-06-27 10:55:12 +02003511static void
Shaohua Li383cd722011-07-12 14:24:35 +02003512__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003513{
Shaohua Li383cd722011-07-12 14:24:35 +02003514 unsigned long elapsed = jiffies - ttime->last_end_request;
3515 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003516
Shaohua Li383cd722011-07-12 14:24:35 +02003517 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3518 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3519 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3520}
3521
3522static void
3523cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003524 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003525{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003526 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003527 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003528 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3529 cfqd->cfq_slice_idle);
3530 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003531#ifdef CONFIG_CFQ_GROUP_IOSCHED
3532 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3533#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003534}
3535
Jens Axboe206dc692006-03-28 13:03:44 +02003536static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003537cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003538 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003539{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003540 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003541 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003542 if (cfqq->last_request_pos) {
3543 if (cfqq->last_request_pos < blk_rq_pos(rq))
3544 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3545 else
3546 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3547 }
Jens Axboe206dc692006-03-28 13:03:44 +02003548
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003549 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003550 if (blk_queue_nonrot(cfqd->queue))
3551 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3552 else
3553 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003554}
Jens Axboe22e2c502005-06-27 10:55:12 +02003555
3556/*
3557 * Disable idle window if the process thinks too long or seeks so much that
3558 * it doesn't matter
3559 */
3560static void
3561cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003562 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003563{
Jens Axboe7b679132008-05-30 12:23:07 +02003564 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003565
Jens Axboe08717142008-01-28 11:38:15 +01003566 /*
3567 * Don't idle for async or idle io prio class
3568 */
3569 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003570 return;
3571
Jens Axboec265a7f2008-06-26 13:49:33 +02003572 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003573
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003574 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3575 cfq_mark_cfqq_deep(cfqq);
3576
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003577 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3578 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003579 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003580 !cfqd->cfq_slice_idle ||
3581 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003582 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003583 else if (sample_valid(cic->ttime.ttime_samples)) {
3584 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003585 enable_idle = 0;
3586 else
3587 enable_idle = 1;
3588 }
3589
Jens Axboe7b679132008-05-30 12:23:07 +02003590 if (old_idle != enable_idle) {
3591 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3592 if (enable_idle)
3593 cfq_mark_cfqq_idle_window(cfqq);
3594 else
3595 cfq_clear_cfqq_idle_window(cfqq);
3596 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003597}
3598
Jens Axboe22e2c502005-06-27 10:55:12 +02003599/*
3600 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3601 * no or if we aren't sure, a 1 will cause a preempt.
3602 */
Jens Axboea6151c32009-10-07 20:02:57 +02003603static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003604cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003605 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003606{
Jens Axboe6d048f52007-04-25 12:44:27 +02003607 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003608
Jens Axboe6d048f52007-04-25 12:44:27 +02003609 cfqq = cfqd->active_queue;
3610 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003611 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003612
Jens Axboe6d048f52007-04-25 12:44:27 +02003613 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003614 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003615
3616 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003617 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003618
Jens Axboe22e2c502005-06-27 10:55:12 +02003619 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003620 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3621 */
3622 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3623 return false;
3624
3625 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003626 * if the new request is sync, but the currently running queue is
3627 * not, let the sync request have priority.
3628 */
Jens Axboe5e705372006-07-13 12:39:25 +02003629 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003630 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003631
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003632 if (new_cfqq->cfqg != cfqq->cfqg)
3633 return false;
3634
3635 if (cfq_slice_used(cfqq))
3636 return true;
3637
3638 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003639 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003640 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3641 new_cfqq->service_tree->count == 2 &&
3642 RB_EMPTY_ROOT(&cfqq->sort_list))
3643 return true;
3644
Jens Axboe374f84a2006-07-23 01:42:19 +02003645 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003646 * So both queues are sync. Let the new request get disk time if
3647 * it's a metadata request and the current queue is doing regular IO.
3648 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003649 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003650 return true;
3651
3652 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003653 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3654 */
3655 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003656 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003657
Shaohua Lid2d59e12010-11-08 15:01:03 +01003658 /* An idle queue should not be idle now for some reason */
3659 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3660 return true;
3661
Jens Axboe1e3335d2007-02-14 19:59:49 +01003662 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003663 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003664
3665 /*
3666 * if this request is as-good as one we would expect from the
3667 * current cfqq, let it preempt
3668 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003669 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003670 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003671
Jens Axboea6151c32009-10-07 20:02:57 +02003672 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003673}
3674
3675/*
3676 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3677 * let it have half of its nominal slice.
3678 */
3679static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3680{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003681 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3682
Jens Axboe7b679132008-05-30 12:23:07 +02003683 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003684 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003685
Jens Axboebf572252006-07-19 20:29:12 +02003686 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003687 * workload type is changed, don't save slice, otherwise preempt
3688 * doesn't happen
3689 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003690 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003691 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003692
3693 /*
Jens Axboebf572252006-07-19 20:29:12 +02003694 * Put the new queue at the front of the of the current list,
3695 * so we know that it will be selected next.
3696 */
3697 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003698
3699 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003700
Justin TerAvest62a37f62011-03-23 08:25:44 +01003701 cfqq->slice_end = 0;
3702 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003703}
3704
3705/*
Jens Axboe5e705372006-07-13 12:39:25 +02003706 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003707 * something we should do about it
3708 */
3709static void
Jens Axboe5e705372006-07-13 12:39:25 +02003710cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3711 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003712{
Tejun Heoc5869802011-12-14 00:33:41 +01003713 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003714
Aaron Carroll45333d52008-08-26 15:52:36 +02003715 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003716 if (rq->cmd_flags & REQ_PRIO)
3717 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003718
Shaohua Li383cd722011-07-12 14:24:35 +02003719 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003720 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003721 cfq_update_idle_window(cfqd, cfqq, cic);
3722
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003723 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003724
3725 if (cfqq == cfqd->active_queue) {
3726 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003727 * Remember that we saw a request from this process, but
3728 * don't start queuing just yet. Otherwise we risk seeing lots
3729 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003730 * and merging. If the request is already larger than a single
3731 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003732 * merging is already done. Ditto for a busy system that
3733 * has other work pending, don't risk delaying until the
3734 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003735 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003736 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003737 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3738 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003739 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003740 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003741 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003742 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07003743 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003744 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003745 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003746 }
Jens Axboe5e705372006-07-13 12:39:25 +02003747 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003748 /*
3749 * not the active queue - expire current slice if it is
3750 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003751 * has some old slice time left and is of higher priority or
3752 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003753 */
3754 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003755 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003756 }
3757}
3758
Jens Axboe165125e2007-07-24 09:28:11 +02003759static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003760{
Jens Axboeb4878f22005-10-20 16:42:29 +02003761 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003762 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003763
Jens Axboe7b679132008-05-30 12:23:07 +02003764 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003765 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Jens Axboe30996f42009-10-05 11:03:39 +02003767 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003768 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003769 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07003770 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
3771 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02003772 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
Aaron Carroll45333d52008-08-26 15:52:36 +02003775/*
3776 * Update hw_tag based on peak queue depth over 50 samples under
3777 * sufficient load.
3778 */
3779static void cfq_update_hw_tag(struct cfq_data *cfqd)
3780{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003781 struct cfq_queue *cfqq = cfqd->active_queue;
3782
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003783 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3784 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003785
3786 if (cfqd->hw_tag == 1)
3787 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003788
3789 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003790 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003791 return;
3792
Shaohua Li1a1238a2009-10-27 08:46:23 +01003793 /*
3794 * If active queue hasn't enough requests and can idle, cfq might not
3795 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3796 * case
3797 */
3798 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3799 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003800 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003801 return;
3802
Aaron Carroll45333d52008-08-26 15:52:36 +02003803 if (cfqd->hw_tag_samples++ < 50)
3804 return;
3805
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003806 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003807 cfqd->hw_tag = 1;
3808 else
3809 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003810}
3811
Vivek Goyal7667aa02009-12-08 17:52:58 -05003812static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3813{
Tejun Heoc5869802011-12-14 00:33:41 +01003814 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003815
Justin TerAvest02a8f012011-02-09 14:20:03 +01003816 /* If the queue already has requests, don't wait */
3817 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3818 return false;
3819
Vivek Goyal7667aa02009-12-08 17:52:58 -05003820 /* If there are other queues in the group, don't wait */
3821 if (cfqq->cfqg->nr_cfqq > 1)
3822 return false;
3823
Shaohua Li7700fc42011-07-12 14:24:56 +02003824 /* the only queue in the group, but think time is big */
3825 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3826 return false;
3827
Vivek Goyal7667aa02009-12-08 17:52:58 -05003828 if (cfq_slice_used(cfqq))
3829 return true;
3830
3831 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003832 if (cic && sample_valid(cic->ttime.ttime_samples)
3833 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003834 return true;
3835
3836 /*
3837 * If think times is less than a jiffy than ttime_mean=0 and above
3838 * will not be true. It might happen that slice has not expired yet
3839 * but will expire soon (4-5 ns) during select_queue(). To cover the
3840 * case where think time is less than a jiffy, mark the queue wait
3841 * busy if only 1 jiffy is left in the slice.
3842 */
3843 if (cfqq->slice_end - jiffies == 1)
3844 return true;
3845
3846 return false;
3847}
3848
Jens Axboe165125e2007-07-24 09:28:11 +02003849static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Jens Axboe5e705372006-07-13 12:39:25 +02003851 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003852 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003853 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003854 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Jens Axboeb4878f22005-10-20 16:42:29 +02003856 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003857 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3858 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Aaron Carroll45333d52008-08-26 15:52:36 +02003860 cfq_update_hw_tag(cfqd);
3861
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003862 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003863 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003864 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003865 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003866 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07003867 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
3868 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003870 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003871
Vivek Goyal365722b2009-10-03 15:21:27 +02003872 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04003873 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003874
Shaohua Li383cd722011-07-12 14:24:35 +02003875 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003876
3877 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04003878 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003879 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04003880 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
3881 cfqq_type(cfqq));
3882
3883 st->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003884 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3885 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003886 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003887
Shaohua Li7700fc42011-07-12 14:24:56 +02003888#ifdef CONFIG_CFQ_GROUP_IOSCHED
3889 cfqq->cfqg->ttime.last_end_request = now;
3890#endif
3891
Jens Axboecaaa5f92006-06-16 11:23:00 +02003892 /*
3893 * If this is the active queue, check if it needs to be expired,
3894 * or if we want to idle in case it has no pending requests.
3895 */
3896 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003897 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3898
Jens Axboe44f7c162007-01-19 11:51:58 +11003899 if (cfq_cfqq_slice_new(cfqq)) {
3900 cfq_set_prio_slice(cfqd, cfqq);
3901 cfq_clear_cfqq_slice_new(cfqq);
3902 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003903
3904 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003905 * Should we wait for next request to come in before we expire
3906 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003907 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003908 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003909 unsigned long extend_sl = cfqd->cfq_slice_idle;
3910 if (!cfqd->cfq_slice_idle)
3911 extend_sl = cfqd->cfq_group_idle;
3912 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003913 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003914 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003915 }
3916
Jens Axboea36e71f2009-04-15 12:15:11 +02003917 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003918 * Idling is not enabled on:
3919 * - expired queues
3920 * - idle-priority queues
3921 * - async queues
3922 * - queues with still some requests queued
3923 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003924 */
Jens Axboe08717142008-01-28 11:38:15 +01003925 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003926 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003927 else if (sync && cfqq_empty &&
3928 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003929 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003930 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003931 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003932
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003933 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003934 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Jens Axboe89850f72006-07-22 16:48:31 +02003937static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003938{
Jens Axboe1b379d82009-08-11 08:26:11 +02003939 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003940 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003941 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003942 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003943
3944 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003945}
3946
Jens Axboe165125e2007-07-24 09:28:11 +02003947static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003948{
3949 struct cfq_data *cfqd = q->elevator->elevator_data;
3950 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003951 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003952 struct cfq_queue *cfqq;
3953
3954 /*
3955 * don't force setup of a queue from here, as a call to may_queue
3956 * does not necessarily imply that a request actually will be queued.
3957 * so just lookup a possibly existing queue, or return 'may queue'
3958 * if that fails
3959 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003960 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003961 if (!cic)
3962 return ELV_MQUEUE_MAY;
3963
Jens Axboeb0b78f82009-04-08 10:56:08 +02003964 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003965 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003966 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003967
Jens Axboe89850f72006-07-22 16:48:31 +02003968 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003969 }
3970
3971 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974/*
3975 * queue lock held here
3976 */
Jens Axboebb37b942006-12-01 10:42:33 +01003977static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
Jens Axboe5e705372006-07-13 12:39:25 +02003979 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Jens Axboe5e705372006-07-13 12:39:25 +02003981 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003982 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Jens Axboe22e2c502005-06-27 10:55:12 +02003984 BUG_ON(!cfqq->allocated[rw]);
3985 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003987 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003988 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003989 rq->elv.priv[0] = NULL;
3990 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003991
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 cfq_put_queue(cfqq);
3993 }
3994}
3995
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003996static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003997cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003998 struct cfq_queue *cfqq)
3999{
4000 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4001 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004002 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004003 cfq_put_queue(cfqq);
4004 return cic_to_cfqq(cic, 1);
4005}
4006
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004007/*
4008 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4009 * was the last process referring to said cfqq.
4010 */
4011static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004012split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004013{
4014 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004015 cfqq->pid = current->pid;
4016 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004017 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004018 return cfqq;
4019 }
4020
4021 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004022
4023 cfq_put_cooperator(cfqq);
4024
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004025 cfq_put_queue(cfqq);
4026 return NULL;
4027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004029 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004031static int
Tejun Heo852c7882012-03-05 13:15:27 -08004032cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4033 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004036 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004038 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004039 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041 might_sleep_if(gfp_mask & __GFP_WAIT);
4042
Tejun Heo216284c2011-12-14 00:33:38 +01004043 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004044
Tejun Heo598971b2012-03-19 15:10:58 -07004045 check_ioprio_changed(cic, bio);
4046 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004047new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004048 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004049 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004050 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004051 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004052 } else {
4053 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004054 * If the queue was seeky for too long, break it apart.
4055 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004056 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004057 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4058 cfqq = split_cfqq(cic, cfqq);
4059 if (!cfqq)
4060 goto new_queue;
4061 }
4062
4063 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004064 * Check to see if this queue is scheduled to merge with
4065 * another, closely cooperating queue. The merging of
4066 * queues happens here as it must be done in process context.
4067 * The reference on new_cfqq was taken in merge_cfqqs.
4068 */
4069 if (cfqq->new_cfqq)
4070 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
4073 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004074
Jens Axboe6fae9c22011-03-01 15:04:39 -05004075 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004076 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004077 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004078 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01004079 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02004080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
David Howells65f27f32006-11-22 14:55:48 +00004083static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004084{
David Howells65f27f32006-11-22 14:55:48 +00004085 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004086 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004087 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004088
Jens Axboe40bb54d2009-04-15 12:11:10 +02004089 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004090 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004091 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004092}
4093
4094/*
4095 * Timer running if the active_queue is currently idling inside its time slice
4096 */
4097static void cfq_idle_slice_timer(unsigned long data)
4098{
4099 struct cfq_data *cfqd = (struct cfq_data *) data;
4100 struct cfq_queue *cfqq;
4101 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004102 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004103
Jens Axboe7b679132008-05-30 12:23:07 +02004104 cfq_log(cfqd, "idle timer fired");
4105
Jens Axboe22e2c502005-06-27 10:55:12 +02004106 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4107
Jens Axboefe094d92008-01-31 13:08:54 +01004108 cfqq = cfqd->active_queue;
4109 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004110 timed_out = 0;
4111
Jens Axboe22e2c502005-06-27 10:55:12 +02004112 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004113 * We saw a request before the queue expired, let it through
4114 */
4115 if (cfq_cfqq_must_dispatch(cfqq))
4116 goto out_kick;
4117
4118 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004119 * expired
4120 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004121 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004122 goto expire;
4123
4124 /*
4125 * only expire and reinvoke request handler, if there are
4126 * other queues with pending requests
4127 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004128 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004129 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004130
4131 /*
4132 * not expired and it has a request pending, let it dispatch
4133 */
Jens Axboe75e50982009-04-07 08:56:14 +02004134 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004135 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004136
4137 /*
4138 * Queue depth flag is reset only when the idle didn't succeed
4139 */
4140 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004141 }
4142expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004143 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004144out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004145 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004146out_cont:
4147 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4148}
4149
Jens Axboe3b181522005-06-27 10:56:24 +02004150static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4151{
4152 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004153 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004154}
Jens Axboe22e2c502005-06-27 10:55:12 +02004155
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004156static void cfq_put_async_queues(struct cfq_data *cfqd)
4157{
4158 int i;
4159
4160 for (i = 0; i < IOPRIO_BE_NR; i++) {
4161 if (cfqd->async_cfqq[0][i])
4162 cfq_put_queue(cfqd->async_cfqq[0][i]);
4163 if (cfqd->async_cfqq[1][i])
4164 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004165 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01004166
4167 if (cfqd->async_idle_cfqq)
4168 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004169}
4170
Jens Axboeb374d182008-10-31 10:05:07 +01004171static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Jens Axboe22e2c502005-06-27 10:55:12 +02004173 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004174 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004175
Jens Axboe3b181522005-06-27 10:56:24 +02004176 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004177
Al Virod9ff4182006-03-18 13:51:22 -05004178 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004179
Al Virod9ff4182006-03-18 13:51:22 -05004180 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004181 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004182
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004183 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08004184
4185 spin_unlock_irq(q->queue_lock);
4186
Al Viroa90d7422006-03-18 12:05:37 -05004187 cfq_shutdown_timer_wq(cfqd);
4188
Tejun Heoffea73f2012-06-04 10:02:29 +02004189#ifdef CONFIG_CFQ_GROUP_IOSCHED
4190 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4191#else
Tejun Heof51b8022012-03-05 13:15:05 -08004192 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004193#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004194 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Tejun Heob2fab5a2012-03-05 13:14:57 -08004197static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004200 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004201 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Christoph Lameter94f60302007-07-17 04:03:29 -07004203 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01004204 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004205 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004206
Tejun Heof51b8022012-03-05 13:15:05 -08004207 cfqd->queue = q;
4208 q->elevator->elevator_data = cfqd;
4209
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004210 /* Init root service tree */
4211 cfqd->grp_service_tree = CFQ_RB_ROOT;
4212
Tejun Heof51b8022012-03-05 13:15:05 -08004213 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004214#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004215 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004216 if (ret)
4217 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004218
Tejun Heoa2b16932012-04-13 13:11:33 -07004219 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004220#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004221 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004222 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4223 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004224 if (!cfqd->root_group)
4225 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004226
Tejun Heoa2b16932012-04-13 13:11:33 -07004227 cfq_init_cfqg_base(cfqd->root_group);
4228#endif
Tejun Heo3381cb82012-04-01 14:38:44 -07004229 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -08004230 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004231
Jens Axboe26a2ac02009-04-23 12:13:27 +02004232 /*
4233 * Not strictly needed (since RB_ROOT just clears the node and we
4234 * zeroed cfqd on alloc), but better be safe in case someone decides
4235 * to add magic to the rb code
4236 */
4237 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4238 cfqd->prio_trees[i] = RB_ROOT;
4239
Jens Axboe6118b702009-06-30 09:34:12 +02004240 /*
4241 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4242 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004243 * will not attempt to free it. oom_cfqq is linked to root_group
4244 * but shouldn't hold a reference as it'll never be unlinked. Lose
4245 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004246 */
4247 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004248 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004249
4250 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004251 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004252 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004253 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Jens Axboe22e2c502005-06-27 10:55:12 +02004255 init_timer(&cfqd->idle_slice_timer);
4256 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4257 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4258
Jens Axboe23e018a2009-10-05 08:52:35 +02004259 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004262 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4263 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 cfqd->cfq_back_max = cfq_back_max;
4265 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004266 cfqd->cfq_slice[0] = cfq_slice_async;
4267 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004268 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004269 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4270 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004271 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004272 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004273 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004274 /*
4275 * we optimistically start assuming sync ops weren't delayed in last
4276 * second, in order to have larger depth for async operations.
4277 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004278 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004279 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004280
4281out_free:
4282 kfree(cfqd);
4283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/*
4287 * sysfs parts below -->
4288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289static ssize_t
4290cfq_var_show(unsigned int var, char *page)
4291{
4292 return sprintf(page, "%d\n", var);
4293}
4294
4295static ssize_t
4296cfq_var_store(unsigned int *var, const char *page, size_t count)
4297{
4298 char *p = (char *) page;
4299
4300 *var = simple_strtoul(p, &p, 10);
4301 return count;
4302}
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004305static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004307 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 unsigned int __data = __VAR; \
4309 if (__CONV) \
4310 __data = jiffies_to_msecs(__data); \
4311 return cfq_var_show(__data, (page)); \
4312}
4313SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004314SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4315SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004316SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4317SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004318SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004319SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004320SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4321SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4322SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004323SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004324SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325#undef SHOW_FUNCTION
4326
4327#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004328static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004330 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 unsigned int __data; \
4332 int ret = cfq_var_store(&__data, (page), count); \
4333 if (__data < (MIN)) \
4334 __data = (MIN); \
4335 else if (__data > (MAX)) \
4336 __data = (MAX); \
4337 if (__CONV) \
4338 *(__PTR) = msecs_to_jiffies(__data); \
4339 else \
4340 *(__PTR) = __data; \
4341 return ret; \
4342}
4343STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004344STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4345 UINT_MAX, 1);
4346STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4347 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004348STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004349STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4350 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004351STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004352STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004353STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4354STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004355STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4356 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004357STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004358STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359#undef STORE_FUNCTION
4360
Al Viroe572ec72006-03-18 22:27:18 -05004361#define CFQ_ATTR(name) \
4362 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004363
Al Viroe572ec72006-03-18 22:27:18 -05004364static struct elv_fs_entry cfq_attrs[] = {
4365 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004366 CFQ_ATTR(fifo_expire_sync),
4367 CFQ_ATTR(fifo_expire_async),
4368 CFQ_ATTR(back_seek_max),
4369 CFQ_ATTR(back_seek_penalty),
4370 CFQ_ATTR(slice_sync),
4371 CFQ_ATTR(slice_async),
4372 CFQ_ATTR(slice_async_rq),
4373 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004374 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004375 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004376 CFQ_ATTR(target_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004377 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378};
4379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380static struct elevator_type iosched_cfq = {
4381 .ops = {
4382 .elevator_merge_fn = cfq_merge,
4383 .elevator_merged_fn = cfq_merged_request,
4384 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004385 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004386 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004387 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004389 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004392 .elevator_former_req_fn = elv_rb_former_request,
4393 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004394 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004395 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 .elevator_set_req_fn = cfq_set_request,
4397 .elevator_put_req_fn = cfq_put_request,
4398 .elevator_may_queue_fn = cfq_may_queue,
4399 .elevator_init_fn = cfq_init_queue,
4400 .elevator_exit_fn = cfq_exit_queue,
4401 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004402 .icq_size = sizeof(struct cfq_io_cq),
4403 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004404 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004405 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 .elevator_owner = THIS_MODULE,
4407};
4408
Vivek Goyal3e252062009-12-04 10:36:42 -05004409#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004410static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004411 .pd_size = sizeof(struct cfq_group),
4412 .cftypes = cfq_blkcg_files,
4413
4414 .pd_init_fn = cfq_pd_init,
4415 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004416};
Vivek Goyal3e252062009-12-04 10:36:42 -05004417#endif
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419static int __init cfq_init(void)
4420{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004421 int ret;
4422
Jens Axboe22e2c502005-06-27 10:55:12 +02004423 /*
4424 * could be 0 on HZ < 1000 setups
4425 */
4426 if (!cfq_slice_async)
4427 cfq_slice_async = 1;
4428 if (!cfq_slice_idle)
4429 cfq_slice_idle = 1;
4430
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004431#ifdef CONFIG_CFQ_GROUP_IOSCHED
4432 if (!cfq_group_idle)
4433 cfq_group_idle = 1;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004434
Tejun Heo3c798392012-04-16 13:57:25 -07004435 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004436 if (ret)
4437 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004438#else
4439 cfq_group_idle = 0;
4440#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004441
Tejun Heofd794952012-06-04 10:01:38 +02004442 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004443 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4444 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004445 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Tejun Heo3d3c2372011-12-14 00:33:42 +01004447 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004448 if (ret)
4449 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004450
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004451 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004452
4453err_free_pool:
4454 kmem_cache_destroy(cfq_pool);
4455err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004456#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004457 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004458#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004459 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462static void __exit cfq_exit(void)
4463{
Tejun Heoffea73f2012-06-04 10:02:29 +02004464#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004465 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004466#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004468 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
4471module_init(cfq_init);
4472module_exit(cfq_exit);
4473
4474MODULE_AUTHOR("Jens Axboe");
4475MODULE_LICENSE("GPL");
4476MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");