blob: 7701c3fe49b9798e4df48892cbc361476fb61685 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * CFQ, or complete fairness queueing, disk scheduler.
3 *
4 * Based on ideas from a previously unfinished io
5 * scheduler (round robin per-process disk scheduling) and Andrea Arcangeli.
6 *
Jens Axboe0fe23472006-09-04 15:41:16 +02007 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Al Viro1cc9be62006-03-18 12:29:52 -050011#include <linux/blkdev.h>
12#include <linux/elevator.h>
Randy Dunlapad5ebd22009-11-11 13:47:45 +010013#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020015#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020016#include <linux/blktrace_api.h>
Tejun Heo6e736be2011-12-14 00:33:38 +010017#include "blk.h"
Tejun Heo629ed0b2012-04-01 14:38:44 -070018#include "blk-cgroup.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * tunables
22 */
Jens Axboefe094d92008-01-31 13:08:54 +010023/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010024static const int cfq_quantum = 8;
Arjan van de Ven64100092006-01-06 09:46:02 +010025static const int cfq_fifo_expire[2] = { HZ / 4, HZ / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010026/* maximum backwards seek, in KiB */
27static const int cfq_back_max = 16 * 1024;
28/* penalty of a backwards seek */
29static const int cfq_back_penalty = 2;
Arjan van de Ven64100092006-01-06 09:46:02 +010030static const int cfq_slice_sync = HZ / 10;
Jens Axboe3b181522005-06-27 10:56:24 +020031static int cfq_slice_async = HZ / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010032static const int cfq_slice_async_rq = 2;
Jens Axboecaaa5f92006-06-16 11:23:00 +020033static int cfq_slice_idle = HZ / 125;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +020034static int cfq_group_idle = HZ / 125;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010035static const int cfq_target_latency = HZ * 3/10; /* 300 ms */
36static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020037
Jens Axboed9e76202007-04-20 14:27:50 +020038/*
Jens Axboe08717142008-01-28 11:38:15 +010039 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020040 */
Jens Axboe08717142008-01-28 11:38:15 +010041#define CFQ_IDLE_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020042
43/*
44 * below this threshold, we consider thinktime immediate
45 */
46#define CFQ_MIN_TT (2)
47
Jens Axboe22e2c502005-06-27 10:55:12 +020048#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020049#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050050#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020051
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010052#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010053#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010054#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010055#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010056
Tejun Heoa612fdd2011-12-14 00:33:41 +010057#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
58#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
59#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Christoph Lametere18b8902006-12-06 20:33:20 -080061static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jens Axboe22e2c502005-06-27 10:55:12 +020063#define CFQ_PRIO_LISTS IOPRIO_BE_NR
64#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
66
Jens Axboe206dc692006-03-28 13:03:44 +020067#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050068#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020069
Tejun Heoc5869802011-12-14 00:33:41 +010070struct cfq_ttime {
71 unsigned long last_end_request;
72
73 unsigned long ttime_total;
74 unsigned long ttime_samples;
75 unsigned long ttime_mean;
76};
77
Jens Axboe22e2c502005-06-27 10:55:12 +020078/*
Jens Axboecc09e292007-04-26 12:53:50 +020079 * Most of our rbtree usage is for sorting with min extraction, so
80 * if we cache the leftmost node we don't have to walk down the tree
81 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
82 * move this into the elevator for the rq sorting as well.
83 */
84struct cfq_rb_root {
85 struct rb_root rb;
86 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010087 unsigned count;
Richard Kennedy73e9ffd2010-03-01 10:50:20 +010088 unsigned total_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050089 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020090 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020091};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020092#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
93 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020094
95/*
Jens Axboe6118b702009-06-30 09:34:12 +020096 * Per process-grouping structure
97 */
98struct cfq_queue {
99 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100100 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200101 /* various state flags, see below */
102 unsigned int flags;
103 /* parent cfq_data */
104 struct cfq_data *cfqd;
105 /* service_tree member */
106 struct rb_node rb_node;
107 /* service_tree key */
108 unsigned long rb_key;
109 /* prio tree member */
110 struct rb_node p_node;
111 /* prio tree root we belong to, if any */
112 struct rb_root *p_root;
113 /* sorted list of pending requests */
114 struct rb_root sort_list;
115 /* if fifo isn't expired, next request to serve */
116 struct request *next_rq;
117 /* requests queued in sort_list */
118 int queued[2];
119 /* currently allocated requests */
120 int allocated[2];
121 /* fifo list of requests in sort_list */
122 struct list_head fifo;
123
Vivek Goyaldae739e2009-12-03 12:59:45 -0500124 /* time when queue got scheduled in to dispatch first request. */
125 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500126 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100127 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500128 /* time when first request from queue completed and slice started. */
129 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200130 unsigned long slice_end;
131 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200132
Christoph Hellwig65299a32011-08-23 14:50:29 +0200133 /* pending priority requests */
134 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200135 /* number of requests that are on the dispatch list or inside driver */
136 int dispatched;
137
138 /* io prio of this group */
139 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200140 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200141
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100142 pid_t pid;
143
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100144 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400145 sector_t last_request_pos;
146
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100147 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400148 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500149 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200150 /* Number of sectors dispatched from queue in single dispatch round */
151 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200152};
153
154/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100155 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100156 * IDLE is handled separately, so it has negative index
157 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400158enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100159 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500160 RT_WORKLOAD = 1,
161 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200162 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100163};
164
165/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100166 * Second index in the service_trees.
167 */
168enum wl_type_t {
169 ASYNC_WORKLOAD = 0,
170 SYNC_NOIDLE_WORKLOAD = 1,
171 SYNC_WORKLOAD = 2
172};
173
Tejun Heo155fead2012-04-01 14:38:44 -0700174struct cfqg_stats {
175#ifdef CONFIG_CFQ_GROUP_IOSCHED
176 /* total bytes transferred */
177 struct blkg_rwstat service_bytes;
178 /* total IOs serviced, post merge */
179 struct blkg_rwstat serviced;
180 /* number of ios merged */
181 struct blkg_rwstat merged;
182 /* total time spent on device in ns, may not be accurate w/ queueing */
183 struct blkg_rwstat service_time;
184 /* total time spent waiting in scheduler queue in ns */
185 struct blkg_rwstat wait_time;
186 /* number of IOs queued up */
187 struct blkg_rwstat queued;
188 /* total sectors transferred */
189 struct blkg_stat sectors;
190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time;
192#ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700203 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700204 struct blkg_stat idle_time;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time;
209 uint64_t start_idle_time;
210 uint64_t start_empty_time;
211 uint16_t flags;
212#endif /* CONFIG_DEBUG_BLK_CGROUP */
213#endif /* CONFIG_CFQ_GROUP_IOSCHED */
214};
215
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500216/* This is per cgroup per device grouping structure */
217struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700218 /* must be the first member */
219 struct blkg_policy_data pd;
220
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500221 /* group service_tree member */
222 struct rb_node rb_node;
223
224 /* group service_tree key */
225 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800226
227 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800228 * The number of active cfqgs and sum of their weights under this
229 * cfqg. This covers this cfqg's leaf_weight and all children's
230 * weights, but does not cover weights of further descendants.
231 *
232 * If a cfqg is on the service tree, it's active. An active cfqg
233 * also activates its parent and contributes to the children_weight
234 * of the parent.
235 */
236 int nr_active;
237 unsigned int children_weight;
238
239 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800240 * There are two weights - (internal) weight is the weight of this
241 * cfqg against the sibling cfqgs. leaf_weight is the wight of
242 * this cfqg against the child cfqgs. For the root cfqg, both
243 * weights are kept in sync for backward compatibility.
244 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500245 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100246 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700247 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500248
Tejun Heoe71357e2013-01-09 08:05:10 -0800249 unsigned int leaf_weight;
250 unsigned int new_leaf_weight;
251 unsigned int dev_leaf_weight;
252
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500253 /* number of cfqq currently on this group */
254 int nr_cfqq;
255
Jens Axboe22e2c502005-06-27 10:55:12 +0200256 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200257 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200258 * create the array for each prio class but at run time it is used
259 * only for RT and BE class and slot for IDLE class remains unused.
260 * This is primarily done to avoid confusion and a gcc warning.
261 */
262 unsigned int busy_queues_avg[CFQ_PRIO_NR];
263 /*
264 * rr lists of queues with requests. We maintain service trees for
265 * RT and BE classes. These trees are subdivided in subclasses
266 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
267 * class there is no subclassification and all the cfq queues go on
268 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100269 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200270 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100271 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100272 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500273
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400274 unsigned long saved_wl_slice;
275 enum wl_type_t saved_wl_type;
276 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800277
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200278 /* number of requests that are on the dispatch list or inside driver */
279 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200280 struct cfq_ttime ttime;
Tejun Heo155fead2012-04-01 14:38:44 -0700281 struct cfqg_stats stats;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500282};
283
Tejun Heoc5869802011-12-14 00:33:41 +0100284struct cfq_io_cq {
285 struct io_cq icq; /* must be the first member */
286 struct cfq_queue *cfqq[2];
287 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700288 int ioprio; /* the current ioprio */
289#ifdef CONFIG_CFQ_GROUP_IOSCHED
290 uint64_t blkcg_id; /* the current blkcg ID */
291#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100292};
293
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500294/*
295 * Per block device queue structure
296 */
297struct cfq_data {
298 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500299 /* Root service tree for cfq_groups */
300 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800301 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500302
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100303 /*
304 * The priority currently being served
305 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400306 enum wl_class_t serving_wl_class;
307 enum wl_type_t serving_wl_type;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100308 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500309 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200310
311 /*
312 * Each priority tree is sorted by next_request position. These
313 * trees are used when determining if two or more queues are
314 * interleaving requests (see cfq_close_cooperator).
315 */
316 struct rb_root prio_trees[CFQ_PRIO_LISTS];
317
Jens Axboe22e2c502005-06-27 10:55:12 +0200318 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100319 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200320
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100321 int rq_in_driver;
322 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200323
324 /*
325 * queue-depth detection
326 */
327 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200328 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100329 /*
330 * hw_tag can be
331 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
332 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
333 * 0 => no NCQ
334 */
335 int hw_tag_est_depth;
336 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200337
338 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200339 * idle window management
340 */
341 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200342 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200343
344 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100345 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200346
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200347 /*
348 * async queue for each priority case
349 */
350 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
351 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200352
Jens Axboe6d048f52007-04-25 12:44:27 +0200353 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * tunables, see top of file
357 */
358 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200359 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned int cfq_back_penalty;
361 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200362 unsigned int cfq_slice[2];
363 unsigned int cfq_slice_async_rq;
364 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200365 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200366 unsigned int cfq_latency;
Tao Ma5bf14c02012-04-01 14:33:39 -0700367 unsigned int cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500368
Jens Axboe6118b702009-06-30 09:34:12 +0200369 /*
370 * Fallback dummy cfqq for extreme OOM conditions
371 */
372 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200373
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100374 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375};
376
Vivek Goyal25fb5162009-12-03 12:59:46 -0500377static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
378
Vivek Goyal34b98d02012-10-03 16:56:58 -0400379static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400380 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500381 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100382{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500383 if (!cfqg)
384 return NULL;
385
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400386 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500387 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100388
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400389 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100390}
391
Jens Axboe3b181522005-06-27 10:56:24 +0200392enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100393 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
394 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200395 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100396 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100397 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
398 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
399 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100400 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200401 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400402 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100403 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100404 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500405 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200406};
407
408#define CFQ_CFQQ_FNS(name) \
409static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
410{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100411 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200412} \
413static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
414{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100415 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200416} \
417static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
418{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100419 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200420}
421
422CFQ_CFQQ_FNS(on_rr);
423CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200424CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200425CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200426CFQ_CFQQ_FNS(fifo_expire);
427CFQ_CFQQ_FNS(idle_window);
428CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100429CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200430CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200431CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100432CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100433CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500434CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200435#undef CFQ_CFQQ_FNS
436
Tejun Heof95a04a2012-04-16 13:57:26 -0700437static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
438{
439 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
440}
441
Tejun Heof95a04a2012-04-16 13:57:26 -0700442static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
443{
444 return pd_to_blkg(&cfqg->pd);
445}
446
Tejun Heo629ed0b2012-04-01 14:38:44 -0700447#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700448
Tejun Heo155fead2012-04-01 14:38:44 -0700449/* cfqg stats flags */
450enum cfqg_stats_flags {
451 CFQG_stats_waiting = 0,
452 CFQG_stats_idling,
453 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700454};
455
Tejun Heo155fead2012-04-01 14:38:44 -0700456#define CFQG_FLAG_FNS(name) \
457static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700458{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700459 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700460} \
Tejun Heo155fead2012-04-01 14:38:44 -0700461static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700462{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700463 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700464} \
Tejun Heo155fead2012-04-01 14:38:44 -0700465static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700466{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700467 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700468} \
469
Tejun Heo155fead2012-04-01 14:38:44 -0700470CFQG_FLAG_FNS(waiting)
471CFQG_FLAG_FNS(idling)
472CFQG_FLAG_FNS(empty)
473#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700474
475/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700476static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700477{
478 unsigned long long now;
479
Tejun Heo155fead2012-04-01 14:38:44 -0700480 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700481 return;
482
483 now = sched_clock();
484 if (time_after64(now, stats->start_group_wait_time))
485 blkg_stat_add(&stats->group_wait_time,
486 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700487 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700488}
489
490/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700491static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
492 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700493{
Tejun Heo155fead2012-04-01 14:38:44 -0700494 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700495
Tejun Heo155fead2012-04-01 14:38:44 -0700496 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700497 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700498 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700499 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700500 stats->start_group_wait_time = sched_clock();
501 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700502}
503
504/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700505static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700506{
507 unsigned long long now;
508
Tejun Heo155fead2012-04-01 14:38:44 -0700509 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700510 return;
511
512 now = sched_clock();
513 if (time_after64(now, stats->start_empty_time))
514 blkg_stat_add(&stats->empty_time,
515 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700516 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700517}
518
Tejun Heo155fead2012-04-01 14:38:44 -0700519static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700520{
Tejun Heo155fead2012-04-01 14:38:44 -0700521 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700522}
523
Tejun Heo155fead2012-04-01 14:38:44 -0700524static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700525{
Tejun Heo155fead2012-04-01 14:38:44 -0700526 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700527
528 if (blkg_rwstat_sum(&stats->queued))
529 return;
530
531 /*
532 * group is already marked empty. This can happen if cfqq got new
533 * request in parent group and moved to this group while being added
534 * to service tree. Just ignore the event and move on.
535 */
Tejun Heo155fead2012-04-01 14:38:44 -0700536 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700537 return;
538
539 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700540 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700541}
542
Tejun Heo155fead2012-04-01 14:38:44 -0700543static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700544{
Tejun Heo155fead2012-04-01 14:38:44 -0700545 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700546
Tejun Heo155fead2012-04-01 14:38:44 -0700547 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700548 unsigned long long now = sched_clock();
549
550 if (time_after64(now, stats->start_idle_time))
551 blkg_stat_add(&stats->idle_time,
552 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700553 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700554 }
555}
556
Tejun Heo155fead2012-04-01 14:38:44 -0700557static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700558{
Tejun Heo155fead2012-04-01 14:38:44 -0700559 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700560
Tejun Heo155fead2012-04-01 14:38:44 -0700561 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700562
563 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700564 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700565}
566
Tejun Heo155fead2012-04-01 14:38:44 -0700567static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700568{
Tejun Heo155fead2012-04-01 14:38:44 -0700569 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700570
571 blkg_stat_add(&stats->avg_queue_size_sum,
572 blkg_rwstat_sum(&stats->queued));
573 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700574 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700575}
576
577#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
578
Tejun Heof48ec1d2012-04-13 13:11:25 -0700579static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
580static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
581static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
582static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
583static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
584static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
585static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700586
587#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
588
589#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700590
Tejun Heoffea73f2012-06-04 10:02:29 +0200591static struct blkcg_policy blkcg_policy_cfq;
592
593static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
594{
595 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
596}
597
Tejun Heo7918ffb2013-01-09 08:05:11 -0800598/*
599 * Determine the parent cfqg for weight calculation. Currently, cfqg
600 * scheduling is flat and the root is the parent of everyone else.
601 */
602static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg)
603{
604 struct blkcg_gq *blkg = cfqg_to_blkg(cfqg);
605 struct cfq_group *root;
606
607 while (blkg->parent)
608 blkg = blkg->parent;
609 root = blkg_to_cfqg(blkg);
610
611 return root != cfqg ? root : NULL;
612}
613
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100614static inline void cfqg_get(struct cfq_group *cfqg)
615{
616 return blkg_get(cfqg_to_blkg(cfqg));
617}
618
619static inline void cfqg_put(struct cfq_group *cfqg)
620{
621 return blkg_put(cfqg_to_blkg(cfqg));
622}
623
Tejun Heo54e7ed12012-04-16 13:57:23 -0700624#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
625 char __pbuf[128]; \
626 \
627 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400628 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
629 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
630 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700631 __pbuf, ##args); \
632} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500633
Tejun Heo54e7ed12012-04-16 13:57:23 -0700634#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
635 char __pbuf[128]; \
636 \
637 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
638 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
639} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500640
Tejun Heo155fead2012-04-01 14:38:44 -0700641static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
642 struct cfq_group *curr_cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700643{
Tejun Heo155fead2012-04-01 14:38:44 -0700644 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
645 cfqg_stats_end_empty_time(&cfqg->stats);
646 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700647}
648
Tejun Heo155fead2012-04-01 14:38:44 -0700649static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
650 unsigned long time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700651{
Tejun Heo155fead2012-04-01 14:38:44 -0700652 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700653#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700654 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700655#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700656}
657
Tejun Heo155fead2012-04-01 14:38:44 -0700658static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700659{
Tejun Heo155fead2012-04-01 14:38:44 -0700660 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700661}
662
Tejun Heo155fead2012-04-01 14:38:44 -0700663static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700664{
Tejun Heo155fead2012-04-01 14:38:44 -0700665 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700666}
667
Tejun Heo155fead2012-04-01 14:38:44 -0700668static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
669 uint64_t bytes, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700670{
Tejun Heo155fead2012-04-01 14:38:44 -0700671 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
672 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
673 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700674}
675
Tejun Heo155fead2012-04-01 14:38:44 -0700676static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
677 uint64_t start_time, uint64_t io_start_time, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700678{
Tejun Heo155fead2012-04-01 14:38:44 -0700679 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700680 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700681
682 if (time_after64(now, io_start_time))
683 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
684 if (time_after64(io_start_time, start_time))
685 blkg_rwstat_add(&stats->wait_time, rw,
686 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700687}
688
Tejun Heo3c798392012-04-16 13:57:25 -0700689static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo155fead2012-04-01 14:38:44 -0700690{
691 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
692 struct cfqg_stats *stats = &cfqg->stats;
693
694 /* queued stats shouldn't be cleared */
695 blkg_rwstat_reset(&stats->service_bytes);
696 blkg_rwstat_reset(&stats->serviced);
697 blkg_rwstat_reset(&stats->merged);
698 blkg_rwstat_reset(&stats->service_time);
699 blkg_rwstat_reset(&stats->wait_time);
700 blkg_stat_reset(&stats->time);
701#ifdef CONFIG_DEBUG_BLK_CGROUP
702 blkg_stat_reset(&stats->unaccounted_time);
703 blkg_stat_reset(&stats->avg_queue_size_sum);
704 blkg_stat_reset(&stats->avg_queue_size_samples);
705 blkg_stat_reset(&stats->dequeue);
706 blkg_stat_reset(&stats->group_wait_time);
707 blkg_stat_reset(&stats->idle_time);
708 blkg_stat_reset(&stats->empty_time);
709#endif
710}
711
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100712#else /* CONFIG_CFQ_GROUP_IOSCHED */
713
Tejun Heo7918ffb2013-01-09 08:05:11 -0800714static inline struct cfq_group *cfqg_flat_parent(struct cfq_group *cfqg) { return NULL; }
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100715static inline void cfqg_get(struct cfq_group *cfqg) { }
716static inline void cfqg_put(struct cfq_group *cfqg) { }
717
Jens Axboe7b679132008-05-30 12:23:07 +0200718#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400719 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
720 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
721 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
722 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200723#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100724
Tejun Heo155fead2012-04-01 14:38:44 -0700725static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
726 struct cfq_group *curr_cfqg, int rw) { }
727static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
728 unsigned long time, unsigned long unaccounted_time) { }
729static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
730static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
731static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
732 uint64_t bytes, int rw) { }
733static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
734 uint64_t start_time, uint64_t io_start_time, int rw) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700735
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100736#endif /* CONFIG_CFQ_GROUP_IOSCHED */
737
Jens Axboe7b679132008-05-30 12:23:07 +0200738#define cfq_log(cfqd, fmt, args...) \
739 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
740
Vivek Goyal615f0252009-12-03 12:59:39 -0500741/* Traverses through cfq group service trees */
742#define for_each_cfqg_st(cfqg, i, j, st) \
743 for (i = 0; i <= IDLE_WORKLOAD; i++) \
744 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
745 : &cfqg->service_tree_idle; \
746 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
747 (i == IDLE_WORKLOAD && j == 0); \
748 j++, st = i < IDLE_WORKLOAD ? \
749 &cfqg->service_trees[i][j]: NULL) \
750
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200751static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
752 struct cfq_ttime *ttime, bool group_idle)
753{
754 unsigned long slice;
755 if (!sample_valid(ttime->ttime_samples))
756 return false;
757 if (group_idle)
758 slice = cfqd->cfq_group_idle;
759 else
760 slice = cfqd->cfq_slice_idle;
761 return ttime->ttime_mean > slice;
762}
Vivek Goyal615f0252009-12-03 12:59:39 -0500763
Vivek Goyal02b35082010-08-23 12:23:53 +0200764static inline bool iops_mode(struct cfq_data *cfqd)
765{
766 /*
767 * If we are not idling on queues and it is a NCQ drive, parallel
768 * execution of requests is on and measuring time is not possible
769 * in most of the cases until and unless we drive shallower queue
770 * depths and that becomes a performance bottleneck. In such cases
771 * switch to start providing fairness in terms of number of IOs.
772 */
773 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
774 return true;
775 else
776 return false;
777}
778
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400779static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100780{
781 if (cfq_class_idle(cfqq))
782 return IDLE_WORKLOAD;
783 if (cfq_class_rt(cfqq))
784 return RT_WORKLOAD;
785 return BE_WORKLOAD;
786}
787
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100788
789static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
790{
791 if (!cfq_cfqq_sync(cfqq))
792 return ASYNC_WORKLOAD;
793 if (!cfq_cfqq_idle_window(cfqq))
794 return SYNC_NOIDLE_WORKLOAD;
795 return SYNC_WORKLOAD;
796}
797
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400798static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500799 struct cfq_data *cfqd,
800 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100801{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400802 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500803 return cfqg->service_tree_idle.count;
804
Vivek Goyal34b98d02012-10-03 16:56:58 -0400805 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
806 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
807 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100808}
809
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500810static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
811 struct cfq_group *cfqg)
812{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400813 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
814 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500815}
816
Jens Axboe165125e2007-07-24 09:28:11 +0200817static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800818static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700819 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800820 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200821
Tejun Heoc5869802011-12-14 00:33:41 +0100822static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
823{
824 /* cic->icq is the first member, %NULL will convert to %NULL */
825 return container_of(icq, struct cfq_io_cq, icq);
826}
827
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100828static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
829 struct io_context *ioc)
830{
831 if (ioc)
832 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
833 return NULL;
834}
835
Tejun Heoc5869802011-12-14 00:33:41 +0100836static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200837{
Jens Axboea6151c32009-10-07 20:02:57 +0200838 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200839}
840
Tejun Heoc5869802011-12-14 00:33:41 +0100841static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
842 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200843{
Jens Axboea6151c32009-10-07 20:02:57 +0200844 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200845}
846
Tejun Heoc5869802011-12-14 00:33:41 +0100847static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400848{
Tejun Heoc5869802011-12-14 00:33:41 +0100849 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400850}
851
Vasily Tarasov91fac312007-04-25 12:29:51 +0200852/*
853 * We regard a request as SYNC, if it's either a read or has the SYNC bit
854 * set (in which case it could also be direct WRITE).
855 */
Jens Axboea6151c32009-10-07 20:02:57 +0200856static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200857{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200858 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700862 * scheduler run of queue, if there are requests pending and no one in the
863 * driver that will restart queueing
864 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200865static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700866{
Jens Axboe7b679132008-05-30 12:23:07 +0200867 if (cfqd->busy_queues) {
868 cfq_log(cfqd, "schedule dispatch");
Jens Axboe23e018a2009-10-05 08:52:35 +0200869 kblockd_schedule_work(cfqd->queue, &cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200870 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100874 * Scale schedule slice based on io priority. Use the sync time slice only
875 * if a queue is marked sync and has sync io queued. A sync queue with async
876 * io only, should not get full sync slice length.
877 */
Jens Axboea6151c32009-10-07 20:02:57 +0200878static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200879 unsigned short prio)
880{
881 const int base_slice = cfqd->cfq_slice[sync];
882
883 WARN_ON(prio >= IOPRIO_BE_NR);
884
885 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
886}
887
Jens Axboe44f7c162007-01-19 11:51:58 +1100888static inline int
889cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
890{
Jens Axboed9e76202007-04-20 14:27:50 +0200891 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100892}
893
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500894static inline u64 cfq_scale_slice(unsigned long delta, struct cfq_group *cfqg)
895{
896 u64 d = delta << CFQ_SERVICE_SHIFT;
897
Tejun Heo3381cb82012-04-01 14:38:44 -0700898 d = d * CFQ_WEIGHT_DEFAULT;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500899 do_div(d, cfqg->weight);
900 return d;
901}
902
903static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
904{
905 s64 delta = (s64)(vdisktime - min_vdisktime);
906 if (delta > 0)
907 min_vdisktime = vdisktime;
908
909 return min_vdisktime;
910}
911
912static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
913{
914 s64 delta = (s64)(vdisktime - min_vdisktime);
915 if (delta < 0)
916 min_vdisktime = vdisktime;
917
918 return min_vdisktime;
919}
920
921static void update_min_vdisktime(struct cfq_rb_root *st)
922{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500923 struct cfq_group *cfqg;
924
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500925 if (st->left) {
926 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100927 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
928 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500929 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500930}
931
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100932/*
933 * get averaged number of queues of RT/BE priority.
934 * average is updated, with a formula that gives more weight to higher numbers,
935 * to quickly follows sudden increases and decrease slowly
936 */
937
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500938static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
939 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +0100940{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100941 unsigned min_q, max_q;
942 unsigned mult = cfq_hist_divisor - 1;
943 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500944 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100945
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500946 min_q = min(cfqg->busy_queues_avg[rt], busy);
947 max_q = max(cfqg->busy_queues_avg[rt], busy);
948 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100949 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500950 return cfqg->busy_queues_avg[rt];
951}
952
953static inline unsigned
954cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
955{
956 struct cfq_rb_root *st = &cfqd->grp_service_tree;
957
Tao Ma5bf14c02012-04-01 14:33:39 -0700958 return cfqd->cfq_target_latency * cfqg->weight / st->total_weight;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100959}
960
Shaohua Lic553f8e2011-01-14 08:41:03 +0100961static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -0700962cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +1100963{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100964 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
965 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500966 /*
967 * interested queues (we consider only the ones with the same
968 * priority class in the cfq group)
969 */
970 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
971 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100972 unsigned sync_slice = cfqd->cfq_slice[1];
973 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500974 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
975
976 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100977 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
978 /* scale low_slice according to IO priority
979 * and sync vs async */
980 unsigned low_slice =
981 min(slice, base_low_slice * slice / sync_slice);
982 /* the adapted slice value is scaled to fit all iqs
983 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500984 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100985 low_slice);
986 }
987 }
Shaohua Lic553f8e2011-01-14 08:41:03 +0100988 return slice;
989}
990
991static inline void
992cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
993{
Vivek Goyalba5bd522011-01-19 08:25:02 -0700994 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +0100995
Vivek Goyaldae739e2009-12-03 12:59:45 -0500996 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100997 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500998 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +0200999 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +11001000}
1001
1002/*
1003 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1004 * isn't valid until the first request from the dispatch is activated
1005 * and the slice time set.
1006 */
Jens Axboea6151c32009-10-07 20:02:57 +02001007static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001008{
1009 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001010 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001011 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01001012 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001013
Shaohua Lic1e44752010-11-08 15:01:02 +01001014 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001015}
1016
1017/*
Jens Axboe5e705372006-07-13 12:39:25 +02001018 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001020 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 */
Jens Axboe5e705372006-07-13 12:39:25 +02001022static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001023cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001025 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001027#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1028#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1029 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Jens Axboe5e705372006-07-13 12:39:25 +02001031 if (rq1 == NULL || rq1 == rq2)
1032 return rq2;
1033 if (rq2 == NULL)
1034 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001035
Namhyung Kim229836b2011-05-24 10:23:21 +02001036 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1037 return rq_is_sync(rq1) ? rq1 : rq2;
1038
Christoph Hellwig65299a32011-08-23 14:50:29 +02001039 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1040 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001041
Tejun Heo83096eb2009-05-07 22:24:39 +09001042 s1 = blk_rq_pos(rq1);
1043 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * by definition, 1KiB is 2 sectors
1047 */
1048 back_max = cfqd->cfq_back_max * 2;
1049
1050 /*
1051 * Strict one way elevator _except_ in the case where we allow
1052 * short backward seeks which are biased as twice the cost of a
1053 * similar forward seek.
1054 */
1055 if (s1 >= last)
1056 d1 = s1 - last;
1057 else if (s1 + back_max >= last)
1058 d1 = (last - s1) * cfqd->cfq_back_penalty;
1059 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001060 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (s2 >= last)
1063 d2 = s2 - last;
1064 else if (s2 + back_max >= last)
1065 d2 = (last - s2) * cfqd->cfq_back_penalty;
1066 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001067 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Andreas Mohre8a99052006-03-28 08:59:49 +02001071 /*
1072 * By doing switch() on the bit mask "wrap" we avoid having to
1073 * check two variables for all permutations: --> faster!
1074 */
1075 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001076 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001077 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001078 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001079 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001080 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001081 else {
1082 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001083 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001084 else
Jens Axboe5e705372006-07-13 12:39:25 +02001085 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001086 }
1087
1088 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001089 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001090 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001091 return rq2;
1092 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001093 default:
1094 /*
1095 * Since both rqs are wrapped,
1096 * start with the one that's further behind head
1097 * (--> only *one* back seek required),
1098 * since back seek takes more time than forward.
1099 */
1100 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001101 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 else
Jens Axboe5e705372006-07-13 12:39:25 +02001103 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105}
1106
Jens Axboe498d3aa22007-04-26 12:54:48 +02001107/*
1108 * The below is leftmost cache rbtree addon
1109 */
Jens Axboe08717142008-01-28 11:38:15 +01001110static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001111{
Vivek Goyal615f0252009-12-03 12:59:39 -05001112 /* Service tree is empty */
1113 if (!root->count)
1114 return NULL;
1115
Jens Axboecc09e292007-04-26 12:53:50 +02001116 if (!root->left)
1117 root->left = rb_first(&root->rb);
1118
Jens Axboe08717142008-01-28 11:38:15 +01001119 if (root->left)
1120 return rb_entry(root->left, struct cfq_queue, rb_node);
1121
1122 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001123}
1124
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001125static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1126{
1127 if (!root->left)
1128 root->left = rb_first(&root->rb);
1129
1130 if (root->left)
1131 return rb_entry_cfqg(root->left);
1132
1133 return NULL;
1134}
1135
Jens Axboea36e71f2009-04-15 12:15:11 +02001136static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1137{
1138 rb_erase(n, root);
1139 RB_CLEAR_NODE(n);
1140}
1141
Jens Axboecc09e292007-04-26 12:53:50 +02001142static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1143{
1144 if (root->left == n)
1145 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001146 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001147 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/*
1151 * would be nice to take fifo expire time into account as well
1152 */
Jens Axboe5e705372006-07-13 12:39:25 +02001153static struct request *
1154cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1155 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jens Axboe21183b02006-07-13 12:33:14 +02001157 struct rb_node *rbnext = rb_next(&last->rb_node);
1158 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001159 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jens Axboe21183b02006-07-13 12:33:14 +02001161 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001164 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001167 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001168 else {
1169 rbnext = rb_first(&cfqq->sort_list);
1170 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001171 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001174 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Jens Axboed9e76202007-04-20 14:27:50 +02001177static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1178 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Jens Axboed9e76202007-04-20 14:27:50 +02001180 /*
1181 * just an approximation, should be ok.
1182 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001183 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001184 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001187static inline s64
1188cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1189{
1190 return cfqg->vdisktime - st->min_vdisktime;
1191}
1192
1193static void
1194__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1195{
1196 struct rb_node **node = &st->rb.rb_node;
1197 struct rb_node *parent = NULL;
1198 struct cfq_group *__cfqg;
1199 s64 key = cfqg_key(st, cfqg);
1200 int left = 1;
1201
1202 while (*node != NULL) {
1203 parent = *node;
1204 __cfqg = rb_entry_cfqg(parent);
1205
1206 if (key < cfqg_key(st, __cfqg))
1207 node = &parent->rb_left;
1208 else {
1209 node = &parent->rb_right;
1210 left = 0;
1211 }
1212 }
1213
1214 if (left)
1215 st->left = &cfqg->rb_node;
1216
1217 rb_link_node(&cfqg->rb_node, parent, node);
1218 rb_insert_color(&cfqg->rb_node, &st->rb);
1219}
1220
1221static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001222cfq_update_group_weight(struct cfq_group *cfqg)
1223{
1224 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001225
Tejun Heo3381cb82012-04-01 14:38:44 -07001226 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001227 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001228 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001229 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001230
1231 if (cfqg->new_leaf_weight) {
1232 cfqg->leaf_weight = cfqg->new_leaf_weight;
1233 cfqg->new_leaf_weight = 0;
1234 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001235}
1236
1237static void
1238cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1239{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001240 struct cfq_group *pos = cfqg;
1241 bool propagate;
1242
1243 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001244 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1245
1246 cfq_update_group_weight(cfqg);
1247 __cfq_group_service_tree_add(st, cfqg);
1248 st->total_weight += cfqg->weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001249
1250 /*
1251 * Activate @cfqg and propagate activation upwards until we meet an
1252 * already activated node or reach root.
1253 */
1254 propagate = !pos->nr_active++;
1255 pos->children_weight += pos->leaf_weight;
1256
1257 while (propagate) {
1258 struct cfq_group *parent = cfqg_flat_parent(pos);
1259
1260 if (!parent)
1261 break;
1262
1263 propagate = !parent->nr_active++;
1264 parent->children_weight += pos->weight;
1265 pos = parent;
1266 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001267}
1268
1269static void
1270cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001271{
1272 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1273 struct cfq_group *__cfqg;
1274 struct rb_node *n;
1275
1276 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001277 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001278 return;
1279
1280 /*
1281 * Currently put the group at the end. Later implement something
1282 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001283 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001284 */
1285 n = rb_last(&st->rb);
1286 if (n) {
1287 __cfqg = rb_entry_cfqg(n);
1288 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1289 } else
1290 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001291 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001292}
1293
1294static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001295cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1296{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001297 struct cfq_group *pos = cfqg;
1298 bool propagate;
1299
1300 /*
1301 * Undo activation from cfq_group_service_tree_add(). Deactivate
1302 * @cfqg and propagate deactivation upwards.
1303 */
1304 propagate = !--pos->nr_active;
1305 pos->children_weight -= pos->leaf_weight;
1306
1307 while (propagate) {
1308 struct cfq_group *parent = cfqg_flat_parent(pos);
1309
1310 /* @pos has 0 nr_active at this point */
1311 WARN_ON_ONCE(pos->children_weight);
1312
1313 if (!parent)
1314 break;
1315
1316 propagate = !--parent->nr_active;
1317 parent->children_weight -= pos->weight;
1318 pos = parent;
1319 }
1320
1321 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001322 st->total_weight -= cfqg->weight;
1323 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1324 cfq_rb_erase(&cfqg->rb_node, st);
1325}
1326
1327static void
1328cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001329{
1330 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1331
1332 BUG_ON(cfqg->nr_cfqq < 1);
1333 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001334
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001335 /* If there are other cfq queues under this group, don't delete it */
1336 if (cfqg->nr_cfqq)
1337 return;
1338
Vivek Goyal2868ef72009-12-03 12:59:48 -05001339 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001340 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001341 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001342 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001343}
1344
Justin TerAvest167400d2011-03-12 16:54:00 +01001345static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1346 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001347{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001348 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001349
1350 /*
1351 * Queue got expired before even a single request completed or
1352 * got expired immediately after first request completion.
1353 */
1354 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1355 /*
1356 * Also charge the seek time incurred to the group, otherwise
1357 * if there are mutiple queues in the group, each can dispatch
1358 * a single request on seeky media and cause lots of seek time
1359 * and group will never know it.
1360 */
1361 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1362 1);
1363 } else {
1364 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001365 if (slice_used > cfqq->allocated_slice) {
1366 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001367 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001368 }
1369 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1370 *unaccounted_time += cfqq->slice_start -
1371 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001372 }
1373
Vivek Goyaldae739e2009-12-03 12:59:45 -05001374 return slice_used;
1375}
1376
1377static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001378 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001379{
1380 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001381 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001382 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1383 - cfqg->service_tree_idle.count;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001384
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001385 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001386 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001387
Vivek Goyal02b35082010-08-23 12:23:53 +02001388 if (iops_mode(cfqd))
1389 charge = cfqq->slice_dispatch;
1390 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1391 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001392
1393 /* Can't update vdisktime while group is on service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001394 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal02b35082010-08-23 12:23:53 +02001395 cfqg->vdisktime += cfq_scale_slice(charge, cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001396 /* If a new weight was requested, update now, off tree */
1397 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001398
1399 /* This group is being expired. Save the context */
1400 if (time_after(cfqd->workload_expires, jiffies)) {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001401 cfqg->saved_wl_slice = cfqd->workload_expires
Vivek Goyaldae739e2009-12-03 12:59:45 -05001402 - jiffies;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001403 cfqg->saved_wl_type = cfqd->serving_wl_type;
1404 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001405 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001406 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001407
1408 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1409 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001410 cfq_log_cfqq(cfqq->cfqd, cfqq,
1411 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1412 used_sl, cfqq->slice_dispatch, charge,
1413 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001414 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1415 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001416}
1417
Tejun Heof51b8022012-03-05 13:15:05 -08001418/**
1419 * cfq_init_cfqg_base - initialize base part of a cfq_group
1420 * @cfqg: cfq_group to initialize
1421 *
1422 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1423 * is enabled or not.
1424 */
1425static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1426{
1427 struct cfq_rb_root *st;
1428 int i, j;
1429
1430 for_each_cfqg_st(cfqg, i, j, st)
1431 *st = CFQ_RB_ROOT;
1432 RB_CLEAR_NODE(&cfqg->rb_node);
1433
1434 cfqg->ttime.last_end_request = jiffies;
1435}
1436
Vivek Goyal25fb5162009-12-03 12:59:46 -05001437#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07001438static void cfq_pd_init(struct blkcg_gq *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001439{
Tejun Heo03814112012-03-05 13:15:14 -08001440 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001441
Tejun Heof51b8022012-03-05 13:15:05 -08001442 cfq_init_cfqg_base(cfqg);
Tejun Heo3381cb82012-04-01 14:38:44 -07001443 cfqg->weight = blkg->blkcg->cfq_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001444 cfqg->leaf_weight = blkg->blkcg->cfq_leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001445}
1446
1447/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001448 * Search for the cfq group current task belongs to. request_queue lock must
1449 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001450 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001451static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001452 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001453{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001454 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001455 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001456
Tejun Heo3c798392012-04-16 13:57:25 -07001457 /* avoid lookup for the common case where there's no blkcg */
1458 if (blkcg == &blkcg_root) {
Tejun Heocd1604f2012-03-05 13:15:06 -08001459 cfqg = cfqd->root_group;
1460 } else {
Tejun Heo3c798392012-04-16 13:57:25 -07001461 struct blkcg_gq *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001462
Tejun Heo3c96cb32012-04-13 13:11:34 -07001463 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -08001464 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001465 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001466 }
1467
Vivek Goyal25fb5162009-12-03 12:59:46 -05001468 return cfqg;
1469}
1470
1471static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1472{
1473 /* Currently, all async queues are mapped to root group */
1474 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001475 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001476
1477 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001478 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001479 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001480}
1481
Tejun Heof95a04a2012-04-16 13:57:26 -07001482static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1483 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001484{
Tejun Heof95a04a2012-04-16 13:57:26 -07001485 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001486
1487 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001488 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001489 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001490}
1491
Tejun Heo3381cb82012-04-01 14:38:44 -07001492static int cfqg_print_weight_device(struct cgroup *cgrp, struct cftype *cft,
1493 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001494{
Tejun Heo3c798392012-04-16 13:57:25 -07001495 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1496 cfqg_prfill_weight_device, &blkcg_policy_cfq, 0,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001497 false);
1498 return 0;
1499}
1500
Tejun Heoe71357e2013-01-09 08:05:10 -08001501static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1502 struct blkg_policy_data *pd, int off)
1503{
1504 struct cfq_group *cfqg = pd_to_cfqg(pd);
1505
1506 if (!cfqg->dev_leaf_weight)
1507 return 0;
1508 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1509}
1510
1511static int cfqg_print_leaf_weight_device(struct cgroup *cgrp,
1512 struct cftype *cft,
1513 struct seq_file *sf)
1514{
1515 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp),
1516 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq, 0,
1517 false);
1518 return 0;
1519}
1520
Tejun Heo3381cb82012-04-01 14:38:44 -07001521static int cfq_print_weight(struct cgroup *cgrp, struct cftype *cft,
1522 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001523{
Tejun Heo3c798392012-04-16 13:57:25 -07001524 seq_printf(sf, "%u\n", cgroup_to_blkcg(cgrp)->cfq_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001525 return 0;
1526}
1527
Tejun Heoe71357e2013-01-09 08:05:10 -08001528static int cfq_print_leaf_weight(struct cgroup *cgrp, struct cftype *cft,
1529 struct seq_file *sf)
1530{
1531 seq_printf(sf, "%u\n",
1532 cgroup_to_blkcg(cgrp)->cfq_leaf_weight);
1533 return 0;
1534}
1535
1536static int __cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1537 const char *buf, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001538{
Tejun Heo3c798392012-04-16 13:57:25 -07001539 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001540 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001541 struct cfq_group *cfqg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001542 int ret;
1543
Tejun Heo3c798392012-04-16 13:57:25 -07001544 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001545 if (ret)
1546 return ret;
1547
1548 ret = -EINVAL;
Tejun Heo3381cb82012-04-01 14:38:44 -07001549 cfqg = blkg_to_cfqg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -07001550 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001551 if (!is_leaf_weight) {
1552 cfqg->dev_weight = ctx.v;
1553 cfqg->new_weight = ctx.v ?: blkcg->cfq_weight;
1554 } else {
1555 cfqg->dev_leaf_weight = ctx.v;
1556 cfqg->new_leaf_weight = ctx.v ?: blkcg->cfq_leaf_weight;
1557 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001558 ret = 0;
1559 }
1560
1561 blkg_conf_finish(&ctx);
1562 return ret;
1563}
1564
Tejun Heoe71357e2013-01-09 08:05:10 -08001565static int cfqg_set_weight_device(struct cgroup *cgrp, struct cftype *cft,
1566 const char *buf)
1567{
1568 return __cfqg_set_weight_device(cgrp, cft, buf, false);
1569}
1570
1571static int cfqg_set_leaf_weight_device(struct cgroup *cgrp, struct cftype *cft,
1572 const char *buf)
1573{
1574 return __cfqg_set_weight_device(cgrp, cft, buf, true);
1575}
1576
1577static int __cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val,
1578 bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001579{
Tejun Heo3c798392012-04-16 13:57:25 -07001580 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
1581 struct blkcg_gq *blkg;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001582 struct hlist_node *n;
1583
Tejun Heo3381cb82012-04-01 14:38:44 -07001584 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001585 return -EINVAL;
1586
1587 spin_lock_irq(&blkcg->lock);
Tejun Heoe71357e2013-01-09 08:05:10 -08001588
1589 if (!is_leaf_weight)
1590 blkcg->cfq_weight = val;
1591 else
1592 blkcg->cfq_leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001593
1594 hlist_for_each_entry(blkg, n, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001595 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001596
Tejun Heoe71357e2013-01-09 08:05:10 -08001597 if (!cfqg)
1598 continue;
1599
1600 if (!is_leaf_weight) {
1601 if (!cfqg->dev_weight)
1602 cfqg->new_weight = blkcg->cfq_weight;
1603 } else {
1604 if (!cfqg->dev_leaf_weight)
1605 cfqg->new_leaf_weight = blkcg->cfq_leaf_weight;
1606 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001607 }
1608
1609 spin_unlock_irq(&blkcg->lock);
1610 return 0;
1611}
1612
Tejun Heoe71357e2013-01-09 08:05:10 -08001613static int cfq_set_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1614{
1615 return __cfq_set_weight(cgrp, cft, val, false);
1616}
1617
1618static int cfq_set_leaf_weight(struct cgroup *cgrp, struct cftype *cft, u64 val)
1619{
1620 return __cfq_set_weight(cgrp, cft, val, true);
1621}
1622
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001623static int cfqg_print_stat(struct cgroup *cgrp, struct cftype *cft,
1624 struct seq_file *sf)
1625{
Tejun Heo3c798392012-04-16 13:57:25 -07001626 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001627
Tejun Heo3c798392012-04-16 13:57:25 -07001628 blkcg_print_blkgs(sf, blkcg, blkg_prfill_stat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001629 cft->private, false);
1630 return 0;
1631}
1632
1633static int cfqg_print_rwstat(struct cgroup *cgrp, struct cftype *cft,
1634 struct seq_file *sf)
1635{
Tejun Heo3c798392012-04-16 13:57:25 -07001636 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001637
Tejun Heo3c798392012-04-16 13:57:25 -07001638 blkcg_print_blkgs(sf, blkcg, blkg_prfill_rwstat, &blkcg_policy_cfq,
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001639 cft->private, true);
1640 return 0;
1641}
1642
Tejun Heo60c2bc22012-04-01 14:38:43 -07001643#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001644static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1645 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001646{
Tejun Heof95a04a2012-04-16 13:57:26 -07001647 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001648 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001649 u64 v = 0;
1650
1651 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001652 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001653 do_div(v, samples);
1654 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001655 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001656 return 0;
1657}
1658
1659/* print avg_queue_size */
Tejun Heo155fead2012-04-01 14:38:44 -07001660static int cfqg_print_avg_queue_size(struct cgroup *cgrp, struct cftype *cft,
1661 struct seq_file *sf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001662{
Tejun Heo3c798392012-04-16 13:57:25 -07001663 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001664
Tejun Heo155fead2012-04-01 14:38:44 -07001665 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_avg_queue_size,
Tejun Heo3c798392012-04-16 13:57:25 -07001666 &blkcg_policy_cfq, 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001667 return 0;
1668}
1669#endif /* CONFIG_DEBUG_BLK_CGROUP */
1670
1671static struct cftype cfq_blkcg_files[] = {
1672 {
1673 .name = "weight_device",
Tejun Heo3381cb82012-04-01 14:38:44 -07001674 .read_seq_string = cfqg_print_weight_device,
1675 .write_string = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001676 .max_write_len = 256,
1677 },
1678 {
1679 .name = "weight",
Tejun Heo3381cb82012-04-01 14:38:44 -07001680 .read_seq_string = cfq_print_weight,
1681 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001682 },
Tejun Heoe71357e2013-01-09 08:05:10 -08001683
1684 /* on root, leaf_weight is mapped to weight */
1685 {
1686 .name = "leaf_weight_device",
1687 .flags = CFTYPE_ONLY_ON_ROOT,
1688 .read_seq_string = cfqg_print_weight_device,
1689 .write_string = cfqg_set_weight_device,
1690 .max_write_len = 256,
1691 },
1692 {
1693 .name = "leaf_weight",
1694 .flags = CFTYPE_ONLY_ON_ROOT,
1695 .read_seq_string = cfq_print_weight,
1696 .write_u64 = cfq_set_weight,
1697 },
1698
1699 /* no such mapping necessary for !roots */
1700 {
1701 .name = "leaf_weight_device",
1702 .flags = CFTYPE_NOT_ON_ROOT,
1703 .read_seq_string = cfqg_print_leaf_weight_device,
1704 .write_string = cfqg_set_leaf_weight_device,
1705 .max_write_len = 256,
1706 },
1707 {
1708 .name = "leaf_weight",
1709 .flags = CFTYPE_NOT_ON_ROOT,
1710 .read_seq_string = cfq_print_leaf_weight,
1711 .write_u64 = cfq_set_leaf_weight,
1712 },
1713
Tejun Heo60c2bc22012-04-01 14:38:43 -07001714 {
1715 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001716 .private = offsetof(struct cfq_group, stats.time),
1717 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001718 },
1719 {
1720 .name = "sectors",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001721 .private = offsetof(struct cfq_group, stats.sectors),
1722 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001723 },
1724 {
1725 .name = "io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001726 .private = offsetof(struct cfq_group, stats.service_bytes),
1727 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001728 },
1729 {
1730 .name = "io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001731 .private = offsetof(struct cfq_group, stats.serviced),
1732 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001733 },
1734 {
1735 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001736 .private = offsetof(struct cfq_group, stats.service_time),
1737 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001738 },
1739 {
1740 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001741 .private = offsetof(struct cfq_group, stats.wait_time),
1742 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001743 },
1744 {
1745 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001746 .private = offsetof(struct cfq_group, stats.merged),
1747 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001748 },
1749 {
1750 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001751 .private = offsetof(struct cfq_group, stats.queued),
1752 .read_seq_string = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001753 },
1754#ifdef CONFIG_DEBUG_BLK_CGROUP
1755 {
1756 .name = "avg_queue_size",
Tejun Heo155fead2012-04-01 14:38:44 -07001757 .read_seq_string = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001758 },
1759 {
1760 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001761 .private = offsetof(struct cfq_group, stats.group_wait_time),
1762 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001763 },
1764 {
1765 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001766 .private = offsetof(struct cfq_group, stats.idle_time),
1767 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001768 },
1769 {
1770 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001771 .private = offsetof(struct cfq_group, stats.empty_time),
1772 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001773 },
1774 {
1775 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001776 .private = offsetof(struct cfq_group, stats.dequeue),
1777 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001778 },
1779 {
1780 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001781 .private = offsetof(struct cfq_group, stats.unaccounted_time),
1782 .read_seq_string = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001783 },
1784#endif /* CONFIG_DEBUG_BLK_CGROUP */
1785 { } /* terminate */
1786};
Vivek Goyal25fb5162009-12-03 12:59:46 -05001787#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08001788static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001789 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001790{
Tejun Heof51b8022012-03-05 13:15:05 -08001791 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001792}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02001793
Vivek Goyal25fb5162009-12-03 12:59:46 -05001794static inline void
1795cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
1796 cfqq->cfqg = cfqg;
1797}
1798
1799#endif /* GROUP_IOSCHED */
1800
Jens Axboe498d3aa22007-04-26 12:54:48 +02001801/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001802 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02001803 * requests waiting to be processed. It is sorted in the order that
1804 * we will service the queues.
1805 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001806static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02001807 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02001808{
Jens Axboe08717142008-01-28 11:38:15 +01001809 struct rb_node **p, *parent;
1810 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02001811 unsigned long rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001812 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02001813 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001814 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05001815
Vivek Goyal34b98d02012-10-03 16:56:58 -04001816 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01001817 if (cfq_class_idle(cfqq)) {
1818 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001819 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01001820 if (parent && parent != &cfqq->rb_node) {
1821 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1822 rb_key += __cfqq->rb_key;
1823 } else
1824 rb_key += jiffies;
1825 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02001826 /*
1827 * Get our rb key offset. Subtract any residual slice
1828 * value carried from last service. A negative resid
1829 * count indicates slice overrun, and this should position
1830 * the next service time further away in the tree.
1831 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001832 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02001833 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02001834 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001835 } else {
1836 rb_key = -HZ;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001837 __cfqq = cfq_rb_first(st);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02001838 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
1839 }
Jens Axboed9e76202007-04-20 14:27:50 +02001840
1841 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001842 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01001843 /*
Jens Axboed9e76202007-04-20 14:27:50 +02001844 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01001845 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04001846 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02001847 return;
Jens Axboe53b037442006-07-28 09:48:51 +02001848
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001849 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1850 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02001851 }
Jens Axboed9e76202007-04-20 14:27:50 +02001852
Jens Axboe498d3aa22007-04-26 12:54:48 +02001853 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01001854 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04001855 cfqq->service_tree = st;
1856 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02001857 while (*p) {
1858 parent = *p;
1859 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
1860
Jens Axboe0c534e02007-04-18 20:01:57 +02001861 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001862 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02001863 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001864 if (time_before(rb_key, __cfqq->rb_key))
Vivek Goyal1f23f122012-10-03 16:57:00 -04001865 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001866 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04001867 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02001868 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01001869 }
Jens Axboed9e76202007-04-20 14:27:50 +02001870 }
1871
Jens Axboecc09e292007-04-26 12:53:50 +02001872 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04001873 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02001874
Jens Axboed9e76202007-04-20 14:27:50 +02001875 cfqq->rb_key = rb_key;
1876 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04001877 rb_insert_color(&cfqq->rb_node, &st->rb);
1878 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02001879 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001880 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01001881 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Jens Axboea36e71f2009-04-15 12:15:11 +02001884static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001885cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
1886 sector_t sector, struct rb_node **ret_parent,
1887 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02001888{
Jens Axboea36e71f2009-04-15 12:15:11 +02001889 struct rb_node **p, *parent;
1890 struct cfq_queue *cfqq = NULL;
1891
1892 parent = NULL;
1893 p = &root->rb_node;
1894 while (*p) {
1895 struct rb_node **n;
1896
1897 parent = *p;
1898 cfqq = rb_entry(parent, struct cfq_queue, p_node);
1899
1900 /*
1901 * Sort strictly based on sector. Smallest to the left,
1902 * largest to the right.
1903 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001904 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001905 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001906 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02001907 n = &(*p)->rb_left;
1908 else
1909 break;
1910 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001911 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001912 }
1913
1914 *ret_parent = parent;
1915 if (rb_link)
1916 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001917 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02001918}
1919
1920static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1921{
Jens Axboea36e71f2009-04-15 12:15:11 +02001922 struct rb_node **p, *parent;
1923 struct cfq_queue *__cfqq;
1924
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001925 if (cfqq->p_root) {
1926 rb_erase(&cfqq->p_node, cfqq->p_root);
1927 cfqq->p_root = NULL;
1928 }
Jens Axboea36e71f2009-04-15 12:15:11 +02001929
1930 if (cfq_class_idle(cfqq))
1931 return;
1932 if (!cfqq->next_rq)
1933 return;
1934
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001935 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09001936 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
1937 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02001938 if (!__cfqq) {
1939 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001940 rb_insert_color(&cfqq->p_node, cfqq->p_root);
1941 } else
1942 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001943}
1944
Jens Axboe498d3aa22007-04-26 12:54:48 +02001945/*
1946 * Update cfqq's position in the service tree.
1947 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02001948static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02001949{
Jens Axboe6d048f52007-04-25 12:44:27 +02001950 /*
1951 * Resorting requires the cfqq to be on the RR list already.
1952 */
Jens Axboea36e71f2009-04-15 12:15:11 +02001953 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02001954 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02001955 cfq_prio_tree_add(cfqd, cfqq);
1956 }
Jens Axboe6d048f52007-04-25 12:44:27 +02001957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/*
1960 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02001961 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Jens Axboefebffd62008-01-28 13:19:43 +01001963static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
Jens Axboe7b679132008-05-30 12:23:07 +02001965 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001966 BUG_ON(cfq_cfqq_on_rr(cfqq));
1967 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001969 if (cfq_cfqq_sync(cfqq))
1970 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Jens Axboeedd75ff2007-04-19 12:03:34 +02001972 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Jens Axboe498d3aa22007-04-26 12:54:48 +02001975/*
1976 * Called when the cfqq no longer has requests pending, remove it from
1977 * the service tree.
1978 */
Jens Axboefebffd62008-01-28 13:19:43 +01001979static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Jens Axboe7b679132008-05-30 12:23:07 +02001981 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02001982 BUG_ON(!cfq_cfqq_on_rr(cfqq));
1983 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001985 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
1986 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
1987 cfqq->service_tree = NULL;
1988 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02001989 if (cfqq->p_root) {
1990 rb_erase(&cfqq->p_node, cfqq->p_root);
1991 cfqq->p_root = NULL;
1992 }
Jens Axboed9e76202007-04-20 14:27:50 +02001993
Justin TerAvest8184f932011-03-17 16:12:36 +01001994 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 BUG_ON(!cfqd->busy_queues);
1996 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01001997 if (cfq_cfqq_sync(cfqq))
1998 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
2001/*
2002 * rb tree support functions
2003 */
Jens Axboefebffd62008-01-28 13:19:43 +01002004static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Jens Axboe5e705372006-07-13 12:39:25 +02002006 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002007 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Jens Axboeb4878f22005-10-20 16:42:29 +02002009 BUG_ON(!cfqq->queued[sync]);
2010 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Jens Axboe5e705372006-07-13 12:39:25 +02002012 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Vivek Goyalf04a6422009-12-03 12:59:40 -05002014 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2015 /*
2016 * Queue will be deleted from service tree when we actually
2017 * expire it later. Right now just remove it from prio tree
2018 * as it is empty.
2019 */
2020 if (cfqq->p_root) {
2021 rb_erase(&cfqq->p_node, cfqq->p_root);
2022 cfqq->p_root = NULL;
2023 }
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Jens Axboe5e705372006-07-13 12:39:25 +02002027static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Jens Axboe5e705372006-07-13 12:39:25 +02002029 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002031 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Jens Axboe5380a102006-07-13 12:37:56 +02002033 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Jeff Moyer796d5112011-06-02 21:19:05 +02002035 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002036
2037 if (!cfq_cfqq_on_rr(cfqq))
2038 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002039
2040 /*
2041 * check if this request is a better next-serve candidate
2042 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002043 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002044 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002045
2046 /*
2047 * adjust priority tree position, if ->next_rq changes
2048 */
2049 if (prev != cfqq->next_rq)
2050 cfq_prio_tree_add(cfqd, cfqq);
2051
Jens Axboe5044eed2007-04-25 11:53:48 +02002052 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Jens Axboefebffd62008-01-28 13:19:43 +01002055static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Jens Axboe5380a102006-07-13 12:37:56 +02002057 elv_rb_del(&cfqq->sort_list, rq);
2058 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo155fead2012-04-01 14:38:44 -07002059 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002060 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002061 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2062 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
Jens Axboe206dc692006-03-28 13:03:44 +02002065static struct request *
2066cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Jens Axboe206dc692006-03-28 13:03:44 +02002068 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002069 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002070 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Jens Axboe4ac845a2008-01-24 08:44:49 +01002072 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002073 if (!cic)
2074 return NULL;
2075
2076 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboe89850f72006-07-22 16:48:31 +02002077 if (cfqq) {
2078 sector_t sector = bio->bi_sector + bio_sectors(bio);
2079
Jens Axboe21183b02006-07-13 12:33:14 +02002080 return elv_rb_find(&cfqq->sort_list, sector);
Jens Axboe89850f72006-07-22 16:48:31 +02002081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return NULL;
2084}
2085
Jens Axboe165125e2007-07-24 09:28:11 +02002086static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002087{
2088 struct cfq_data *cfqd = q->elevator->elevator_data;
2089
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002090 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002091 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002092 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002093
Tejun Heo5b936292009-05-07 22:24:38 +09002094 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002095}
2096
Jens Axboe165125e2007-07-24 09:28:11 +02002097static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Jens Axboe22e2c502005-06-27 10:55:12 +02002099 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002101 WARN_ON(!cfqd->rq_in_driver);
2102 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002103 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002104 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Jens Axboeb4878f22005-10-20 16:42:29 +02002107static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Jens Axboe5e705372006-07-13 12:39:25 +02002109 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002110
Jens Axboe5e705372006-07-13 12:39:25 +02002111 if (cfqq->next_rq == rq)
2112 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Jens Axboeb4878f22005-10-20 16:42:29 +02002114 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002115 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002116
Aaron Carroll45333d52008-08-26 15:52:36 +02002117 cfqq->cfqd->rq_queued--;
Tejun Heo155fead2012-04-01 14:38:44 -07002118 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002119 if (rq->cmd_flags & REQ_PRIO) {
2120 WARN_ON(!cfqq->prio_pending);
2121 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Jens Axboe165125e2007-07-24 09:28:11 +02002125static int cfq_merge(struct request_queue *q, struct request **req,
2126 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 struct cfq_data *cfqd = q->elevator->elevator_data;
2129 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Jens Axboe206dc692006-03-28 13:03:44 +02002131 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002132 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002133 *req = __rq;
2134 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
2137 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Jens Axboe165125e2007-07-24 09:28:11 +02002140static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02002141 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Jens Axboe21183b02006-07-13 12:33:14 +02002143 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002144 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Jens Axboe5e705372006-07-13 12:39:25 +02002146 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Divyesh Shah812d4022010-04-08 21:14:23 -07002150static void cfq_bio_merged(struct request_queue *q, struct request *req,
2151 struct bio *bio)
2152{
Tejun Heo155fead2012-04-01 14:38:44 -07002153 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
Divyesh Shah812d4022010-04-08 21:14:23 -07002154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156static void
Jens Axboe165125e2007-07-24 09:28:11 +02002157cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct request *next)
2159{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002160 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002161 struct cfq_data *cfqd = q->elevator->elevator_data;
2162
Jens Axboe22e2c502005-06-27 10:55:12 +02002163 /*
2164 * reposition in fifo if next is older than rq
2165 */
2166 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002167 time_before(rq_fifo_time(next), rq_fifo_time(rq)) &&
2168 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002169 list_move(&rq->queuelist, &next->queuelist);
Jens Axboe30996f42009-10-05 11:03:39 +02002170 rq_set_fifo_time(rq, rq_fifo_time(next));
2171 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002172
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002173 if (cfqq->next_rq == next)
2174 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002175 cfq_remove_request(next);
Tejun Heo155fead2012-04-01 14:38:44 -07002176 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002177
2178 cfqq = RQ_CFQQ(next);
2179 /*
2180 * all requests of this queue are merged to other queues, delete it
2181 * from the service tree. If it's the active_queue,
2182 * cfq_dispatch_requests() will choose to expire it or do idle
2183 */
2184 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2185 cfqq != cfqd->active_queue)
2186 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002187}
2188
Jens Axboe165125e2007-07-24 09:28:11 +02002189static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002190 struct bio *bio)
2191{
2192 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002193 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002194 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002195
2196 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002197 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002198 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002199 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002200 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002201
2202 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002203 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002204 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002205 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002206 cic = cfq_cic_lookup(cfqd, current->io_context);
2207 if (!cic)
2208 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002209
Vasily Tarasov91fac312007-04-25 12:29:51 +02002210 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002211 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002212}
2213
Divyesh Shah812df482010-04-08 21:15:35 -07002214static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2215{
2216 del_timer(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002217 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002218}
2219
Jens Axboefebffd62008-01-28 13:19:43 +01002220static void __cfq_set_active_queue(struct cfq_data *cfqd,
2221 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002222{
2223 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002224 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002225 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002226 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002227 cfqq->slice_start = 0;
2228 cfqq->dispatch_start = jiffies;
2229 cfqq->allocated_slice = 0;
2230 cfqq->slice_end = 0;
2231 cfqq->slice_dispatch = 0;
2232 cfqq->nr_sectors = 0;
2233
2234 cfq_clear_cfqq_wait_request(cfqq);
2235 cfq_clear_cfqq_must_dispatch(cfqq);
2236 cfq_clear_cfqq_must_alloc_slice(cfqq);
2237 cfq_clear_cfqq_fifo_expire(cfqq);
2238 cfq_mark_cfqq_slice_new(cfqq);
2239
2240 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002241 }
2242
2243 cfqd->active_queue = cfqq;
2244}
2245
2246/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002247 * current cfqq expired its slice (or was too idle), select new one
2248 */
2249static void
2250__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002251 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002252{
Jens Axboe7b679132008-05-30 12:23:07 +02002253 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2254
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002255 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002256 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002257
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002258 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002259 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002260
2261 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002262 * If this cfqq is shared between multiple processes, check to
2263 * make sure that those processes are still issuing I/Os within
2264 * the mean seek distance. If not, it may be time to break the
2265 * queues apart again.
2266 */
2267 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2268 cfq_mark_cfqq_split_coop(cfqq);
2269
2270 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002271 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002272 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002273 if (timed_out) {
2274 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002275 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002276 else
2277 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002278 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2279 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002280
Vivek Goyale5ff0822010-04-26 19:25:11 +02002281 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002282
Vivek Goyalf04a6422009-12-03 12:59:40 -05002283 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2284 cfq_del_cfqq_rr(cfqd, cfqq);
2285
Jens Axboeedd75ff2007-04-19 12:03:34 +02002286 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002287
2288 if (cfqq == cfqd->active_queue)
2289 cfqd->active_queue = NULL;
2290
2291 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002292 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002293 cfqd->active_cic = NULL;
2294 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002295}
2296
Vivek Goyale5ff0822010-04-26 19:25:11 +02002297static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002298{
2299 struct cfq_queue *cfqq = cfqd->active_queue;
2300
2301 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002302 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002303}
2304
Jens Axboe498d3aa22007-04-26 12:54:48 +02002305/*
2306 * Get next queue for service. Unless we have a queue preemption,
2307 * we'll simply select the first cfqq in the service tree.
2308 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002309static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002310{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002311 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2312 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002313
Vivek Goyalf04a6422009-12-03 12:59:40 -05002314 if (!cfqd->rq_queued)
2315 return NULL;
2316
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002317 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002318 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002319 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002320 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002321 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002322 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002323}
2324
Vivek Goyalf04a6422009-12-03 12:59:40 -05002325static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2326{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002327 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002328 struct cfq_queue *cfqq;
2329 int i, j;
2330 struct cfq_rb_root *st;
2331
2332 if (!cfqd->rq_queued)
2333 return NULL;
2334
Vivek Goyal25fb5162009-12-03 12:59:46 -05002335 cfqg = cfq_get_next_cfqg(cfqd);
2336 if (!cfqg)
2337 return NULL;
2338
Vivek Goyalf04a6422009-12-03 12:59:40 -05002339 for_each_cfqg_st(cfqg, i, j, st)
2340 if ((cfqq = cfq_rb_first(st)) != NULL)
2341 return cfqq;
2342 return NULL;
2343}
2344
Jens Axboe498d3aa22007-04-26 12:54:48 +02002345/*
2346 * Get and set a new active queue for service.
2347 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002348static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2349 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002350{
Jens Axboee00ef792009-11-04 08:54:55 +01002351 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002352 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002353
Jens Axboe22e2c502005-06-27 10:55:12 +02002354 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002355 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002356}
2357
Jens Axboed9e76202007-04-20 14:27:50 +02002358static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2359 struct request *rq)
2360{
Tejun Heo83096eb2009-05-07 22:24:39 +09002361 if (blk_rq_pos(rq) >= cfqd->last_position)
2362 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002363 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002364 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002365}
2366
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002367static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002368 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002369{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002370 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002371}
2372
Jens Axboea36e71f2009-04-15 12:15:11 +02002373static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2374 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002375{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002376 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002377 struct rb_node *parent, *node;
2378 struct cfq_queue *__cfqq;
2379 sector_t sector = cfqd->last_position;
2380
2381 if (RB_EMPTY_ROOT(root))
2382 return NULL;
2383
2384 /*
2385 * First, if we find a request starting at the end of the last
2386 * request, choose it.
2387 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002388 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002389 if (__cfqq)
2390 return __cfqq;
2391
2392 /*
2393 * If the exact sector wasn't found, the parent of the NULL leaf
2394 * will contain the closest sector.
2395 */
2396 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002397 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002398 return __cfqq;
2399
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002400 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002401 node = rb_next(&__cfqq->p_node);
2402 else
2403 node = rb_prev(&__cfqq->p_node);
2404 if (!node)
2405 return NULL;
2406
2407 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002408 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002409 return __cfqq;
2410
2411 return NULL;
2412}
2413
2414/*
2415 * cfqd - obvious
2416 * cur_cfqq - passed in so that we don't decide that the current queue is
2417 * closely cooperating with itself.
2418 *
2419 * So, basically we're assuming that that cur_cfqq has dispatched at least
2420 * one request, and that cfqd->last_position reflects a position on the disk
2421 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2422 * assumption.
2423 */
2424static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002425 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002426{
2427 struct cfq_queue *cfqq;
2428
Divyesh Shah39c01b22010-03-25 15:45:57 +01002429 if (cfq_class_idle(cur_cfqq))
2430 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002431 if (!cfq_cfqq_sync(cur_cfqq))
2432 return NULL;
2433 if (CFQQ_SEEKY(cur_cfqq))
2434 return NULL;
2435
Jens Axboea36e71f2009-04-15 12:15:11 +02002436 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002437 * Don't search priority tree if it's the only queue in the group.
2438 */
2439 if (cur_cfqq->cfqg->nr_cfqq == 1)
2440 return NULL;
2441
2442 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002443 * We should notice if some of the queues are cooperating, eg
2444 * working closely on the same area of the disk. In that case,
2445 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002446 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002447 cfqq = cfqq_close(cfqd, cur_cfqq);
2448 if (!cfqq)
2449 return NULL;
2450
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002451 /* If new queue belongs to different cfq_group, don't choose it */
2452 if (cur_cfqq->cfqg != cfqq->cfqg)
2453 return NULL;
2454
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002455 /*
2456 * It only makes sense to merge sync queues.
2457 */
2458 if (!cfq_cfqq_sync(cfqq))
2459 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002460 if (CFQQ_SEEKY(cfqq))
2461 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002462
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002463 /*
2464 * Do not merge queues of different priority classes
2465 */
2466 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2467 return NULL;
2468
Jens Axboea36e71f2009-04-15 12:15:11 +02002469 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002470}
2471
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002472/*
2473 * Determine whether we should enforce idle window for this queue.
2474 */
2475
2476static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2477{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002478 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002479 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002480
Vivek Goyal34b98d02012-10-03 16:56:58 -04002481 BUG_ON(!st);
2482 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002483
Vivek Goyalb6508c12010-08-23 12:23:33 +02002484 if (!cfqd->cfq_slice_idle)
2485 return false;
2486
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002487 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002488 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002489 return false;
2490
2491 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002492 if (cfq_cfqq_idle_window(cfqq) &&
2493 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002494 return true;
2495
2496 /*
2497 * Otherwise, we do only if they are the last ones
2498 * in their service tree.
2499 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002500 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2501 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002502 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002503 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002504 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002505}
2506
Jens Axboe6d048f52007-04-25 12:44:27 +02002507static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002508{
Jens Axboe17926692007-01-19 11:59:30 +11002509 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002510 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002511 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002512
Jens Axboea68bbddba2008-09-24 13:03:33 +02002513 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002514 * SSD device without seek penalty, disable idling. But only do so
2515 * for devices that support queuing, otherwise we still have a problem
2516 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002517 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002518 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002519 return;
2520
Jens Axboedd67d052006-06-21 09:36:18 +02002521 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002522 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002523
2524 /*
2525 * idle is disabled, either manually or by past process history
2526 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002527 if (!cfq_should_idle(cfqd, cfqq)) {
2528 /* no queue idling. Check for group idling */
2529 if (cfqd->cfq_group_idle)
2530 group_idle = cfqd->cfq_group_idle;
2531 else
2532 return;
2533 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002534
Jens Axboe22e2c502005-06-27 10:55:12 +02002535 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002536 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002537 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002538 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002539 return;
2540
2541 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002542 * task has exited, don't wait
2543 */
Jens Axboe206dc692006-03-28 13:03:44 +02002544 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002545 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002546 return;
2547
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002548 /*
2549 * If our average think time is larger than the remaining time
2550 * slice, then don't idle. This avoids overrunning the allotted
2551 * time slice.
2552 */
Shaohua Li383cd722011-07-12 14:24:35 +02002553 if (sample_valid(cic->ttime.ttime_samples) &&
2554 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002555 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002556 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002557 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002558 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002559
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002560 /* There are other queues in the group, don't do group idle */
2561 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2562 return;
2563
Jens Axboe3b181522005-06-27 10:56:24 +02002564 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002565
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002566 if (group_idle)
2567 sl = cfqd->cfq_group_idle;
2568 else
2569 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002570
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002571 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo155fead2012-04-01 14:38:44 -07002572 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002573 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2574 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Jens Axboe498d3aa22007-04-26 12:54:48 +02002577/*
2578 * Move request from internal lists to the request queue dispatch list.
2579 */
Jens Axboe165125e2007-07-24 09:28:11 +02002580static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002582 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002583 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002584
Jens Axboe7b679132008-05-30 12:23:07 +02002585 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2586
Jeff Moyer06d21882009-09-11 17:08:59 +02002587 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002588 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002589 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002590 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002591 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002592
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002593 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002594 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002595 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
2598/*
2599 * return expired entry, or NULL to just start from scratch in rbtree
2600 */
Jens Axboefebffd62008-01-28 13:19:43 +01002601static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Jens Axboe30996f42009-10-05 11:03:39 +02002603 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Jens Axboe3b181522005-06-27 10:56:24 +02002605 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002607
2608 cfq_mark_cfqq_fifo_expire(cfqq);
2609
Jens Axboe89850f72006-07-22 16:48:31 +02002610 if (list_empty(&cfqq->fifo))
2611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Jens Axboe89850f72006-07-22 16:48:31 +02002613 rq = rq_entry_fifo(cfqq->fifo.next);
Jens Axboe30996f42009-10-05 11:03:39 +02002614 if (time_before(jiffies, rq_fifo_time(rq)))
Jens Axboe7b679132008-05-30 12:23:07 +02002615 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Jens Axboe30996f42009-10-05 11:03:39 +02002617 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002618 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
Jens Axboe22e2c502005-06-27 10:55:12 +02002621static inline int
2622cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2623{
2624 const int base_rq = cfqd->cfq_slice_async_rq;
2625
2626 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2627
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002628 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002629}
2630
2631/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002632 * Must be called with the queue_lock held.
2633 */
2634static int cfqq_process_refs(struct cfq_queue *cfqq)
2635{
2636 int process_refs, io_refs;
2637
2638 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002639 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002640 BUG_ON(process_refs < 0);
2641 return process_refs;
2642}
2643
2644static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2645{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002646 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002647 struct cfq_queue *__cfqq;
2648
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002649 /*
2650 * If there are no process references on the new_cfqq, then it is
2651 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2652 * chain may have dropped their last reference (not just their
2653 * last process reference).
2654 */
2655 if (!cfqq_process_refs(new_cfqq))
2656 return;
2657
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002658 /* Avoid a circular list and skip interim queue merges */
2659 while ((__cfqq = new_cfqq->new_cfqq)) {
2660 if (__cfqq == cfqq)
2661 return;
2662 new_cfqq = __cfqq;
2663 }
2664
2665 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002666 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002667 /*
2668 * If the process for the cfqq has gone away, there is no
2669 * sense in merging the queues.
2670 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002671 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002672 return;
2673
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002674 /*
2675 * Merge in the direction of the lesser amount of work.
2676 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002677 if (new_process_refs >= process_refs) {
2678 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002679 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002680 } else {
2681 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002682 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002683 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002684}
2685
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002686static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002687 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002688{
2689 struct cfq_queue *queue;
2690 int i;
2691 bool key_valid = false;
2692 unsigned long lowest_key = 0;
2693 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2694
Vivek Goyal65b32a52009-12-16 17:52:59 -05002695 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2696 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002697 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002698 if (queue &&
2699 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2700 lowest_key = queue->rb_key;
2701 cur_best = i;
2702 key_valid = true;
2703 }
2704 }
2705
2706 return cur_best;
2707}
2708
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002709static void
2710choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002711{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002712 unsigned slice;
2713 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002714 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002715 unsigned group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002716 enum wl_class_t original_class = cfqd->serving_wl_class;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002717
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002718 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002719 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002720 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002721 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002722 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002723 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002724 cfqd->serving_wl_class = IDLE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002725 cfqd->workload_expires = jiffies + 1;
2726 return;
2727 }
2728
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002729 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002730 goto new_workload;
2731
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002732 /*
2733 * For RT and BE, we have to choose also the type
2734 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
2735 * expiration time
2736 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002737 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002738 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002739
2740 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05002741 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002742 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05002743 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002744 return;
2745
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01002746new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002747 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002748 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002749 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002750 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002751 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002752
2753 /*
2754 * the workload slice is computed as a fraction of target latency
2755 * proportional to the number of queues in that workload, over
2756 * all the queues in the same priority class
2757 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002758 group_slice = cfq_group_slice(cfqd, cfqg);
2759
2760 slice = group_slice * count /
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002761 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
2762 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002763 cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002764
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002765 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002766 unsigned int tmp;
2767
2768 /*
2769 * Async queues are currently system wide. Just taking
2770 * proportion of queues with-in same group will lead to higher
2771 * async ratio system wide as generally root group is going
2772 * to have higher weight. A more accurate thing would be to
2773 * calculate system wide asnc/sync ratio.
2774 */
Tao Ma5bf14c02012-04-01 14:33:39 -07002775 tmp = cfqd->cfq_target_latency *
2776 cfqg_busy_async_queues(cfqd, cfqg);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002777 tmp = tmp/cfqd->busy_queues;
2778 slice = min_t(unsigned, slice, tmp);
2779
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002780 /* async workload slice is scaled down according to
2781 * the sync/async slice ratio. */
2782 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05002783 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002784 /* sync workload slice is at least 2 * cfq_slice_idle */
2785 slice = max(slice, 2 * cfqd->cfq_slice_idle);
2786
2787 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002788 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002789 cfqd->workload_expires = jiffies + slice;
2790}
2791
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002792static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
2793{
2794 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002795 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002796
2797 if (RB_EMPTY_ROOT(&st->rb))
2798 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002799 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05002800 update_min_vdisktime(st);
2801 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002802}
2803
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002804static void cfq_choose_cfqg(struct cfq_data *cfqd)
2805{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002806 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
2807
2808 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002809
2810 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002811 if (cfqg->saved_wl_slice) {
2812 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
2813 cfqd->serving_wl_type = cfqg->saved_wl_type;
2814 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01002815 } else
2816 cfqd->workload_expires = jiffies - 1;
2817
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002818 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002819}
2820
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002821/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02002822 * Select a queue for service. If we have a current active queue,
2823 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02002824 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002825static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002826{
Jens Axboea36e71f2009-04-15 12:15:11 +02002827 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002828
2829 cfqq = cfqd->active_queue;
2830 if (!cfqq)
2831 goto new_queue;
2832
Vivek Goyalf04a6422009-12-03 12:59:40 -05002833 if (!cfqd->rq_queued)
2834 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05002835
2836 /*
2837 * We were waiting for group to get backlogged. Expire the queue
2838 */
2839 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
2840 goto expire;
2841
Jens Axboe22e2c502005-06-27 10:55:12 +02002842 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002843 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02002844 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05002845 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
2846 /*
2847 * If slice had not expired at the completion of last request
2848 * we might not have turned on wait_busy flag. Don't expire
2849 * the queue yet. Allow the group to get backlogged.
2850 *
2851 * The very fact that we have used the slice, that means we
2852 * have been idling all along on this queue and it should be
2853 * ok to wait for this request to complete.
2854 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002855 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
2856 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2857 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002858 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01002859 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002860 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05002861 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002862
2863 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002864 * The active queue has requests and isn't expired, allow it to
2865 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02002866 */
Jens Axboedd67d052006-06-21 09:36:18 +02002867 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02002868 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02002869
2870 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02002871 * If another queue has a request waiting within our mean seek
2872 * distance, let it run. The expire code will check for close
2873 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002874 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02002875 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002876 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002877 if (new_cfqq) {
2878 if (!cfqq->new_cfqq)
2879 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02002880 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002881 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002882
2883 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02002884 * No requests pending. If the active queue still has requests in
2885 * flight or is idling for a new request, allow either of these
2886 * conditions to happen (or time out) before selecting a new queue.
2887 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002888 if (timer_pending(&cfqd->idle_slice_timer)) {
2889 cfqq = NULL;
2890 goto keep_queue;
2891 }
2892
Shaohua Li8e1ac662010-11-08 15:01:04 +01002893 /*
2894 * This is a deep seek queue, but the device is much faster than
2895 * the queue can deliver, don't idle
2896 **/
2897 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
2898 (cfq_cfqq_slice_new(cfqq) ||
2899 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
2900 cfq_clear_cfqq_deep(cfqq);
2901 cfq_clear_cfqq_idle_window(cfqq);
2902 }
2903
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002904 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
2905 cfqq = NULL;
2906 goto keep_queue;
2907 }
2908
2909 /*
2910 * If group idle is enabled and there are requests dispatched from
2911 * this group, wait for requests to complete.
2912 */
2913check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02002914 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
2915 cfqq->cfqg->dispatched &&
2916 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02002917 cfqq = NULL;
2918 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02002919 }
2920
Jens Axboe3b181522005-06-27 10:56:24 +02002921expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02002922 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02002923new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002924 /*
2925 * Current queue expired. Check if we have to switch to a new
2926 * service tree
2927 */
2928 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002929 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002930
Jens Axboea36e71f2009-04-15 12:15:11 +02002931 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002932keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02002933 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002934}
2935
Jens Axboefebffd62008-01-28 13:19:43 +01002936static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02002937{
2938 int dispatched = 0;
2939
2940 while (cfqq->next_rq) {
2941 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
2942 dispatched++;
2943 }
2944
2945 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05002946
2947 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002948 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02002949 return dispatched;
2950}
2951
Jens Axboe498d3aa22007-04-26 12:54:48 +02002952/*
2953 * Drain our current requests. Used for barriers and when switching
2954 * io schedulers on-the-fly.
2955 */
Jens Axboed9e76202007-04-20 14:27:50 +02002956static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002957{
Jens Axboe08717142008-01-28 11:38:15 +01002958 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002959 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002960
Divyesh Shah3440c492010-04-09 09:29:57 +02002961 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02002962 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02002963 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
2964 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002965 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02002966 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002967
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002968 BUG_ON(cfqd->busy_queues);
2969
Jeff Moyer6923715a2009-06-12 15:29:30 +02002970 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01002971 return dispatched;
2972}
2973
Shaohua Liabc3c742010-03-01 09:20:54 +01002974static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
2975 struct cfq_queue *cfqq)
2976{
2977 /* the queue hasn't finished any request, can't estimate */
2978 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01002979 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002980 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
2981 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01002982 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01002983
Shaohua Lic1e44752010-11-08 15:01:02 +01002984 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01002985}
2986
Jens Axboe0b182d62009-10-06 20:49:37 +02002987static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02002988{
Jens Axboe2f5cb732009-04-07 08:51:19 +02002989 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Jens Axboe2f5cb732009-04-07 08:51:19 +02002991 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02002992 * Drain async requests before we start sync IO
2993 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002994 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02002995 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02002996
2997 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02002998 * If this is an async queue and we have sync IO in flight, let it wait
2999 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003000 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003001 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003002
Shaohua Liabc3c742010-03-01 09:20:54 +01003003 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003004 if (cfq_class_idle(cfqq))
3005 max_dispatch = 1;
3006
3007 /*
3008 * Does this cfqq already have too much IO in flight?
3009 */
3010 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003011 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003012 /*
3013 * idle queue must always only have a single IO in flight
3014 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003015 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003016 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003017
Jens Axboe2f5cb732009-04-07 08:51:19 +02003018 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003019 * If there is only one sync queue
3020 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003021 * queue no dispatch limit. The reason is a sync queue can
3022 * preempt async queue, limiting the sync queue doesn't make
3023 * sense. This is useful for aiostress test.
3024 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003025 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3026 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003027
3028 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003029 * We have other queues, don't allow more IO from this one
3030 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003031 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3032 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003033 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003034
Jens Axboe2f5cb732009-04-07 08:51:19 +02003035 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003036 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003037 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003038 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003039 max_dispatch = -1;
3040 else
3041 /*
3042 * Normally we start throttling cfqq when cfq_quantum/2
3043 * requests have been dispatched. But we can drive
3044 * deeper queue depths at the beginning of slice
3045 * subjected to upper limit of cfq_quantum.
3046 * */
3047 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003048 }
3049
3050 /*
3051 * Async queues must wait a bit before being allowed dispatch.
3052 * We also ramp up the dispatch depth gradually for async IO,
3053 * based on the last sync IO we serviced
3054 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003055 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003056 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003057 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003058
Jens Axboe61f0c1d2009-10-03 19:46:03 +02003059 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02003060 if (!depth && !cfqq->dispatched)
3061 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003062 if (depth < max_dispatch)
3063 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
Jens Axboe0b182d62009-10-06 20:49:37 +02003066 /*
3067 * If we're below the current max, allow a dispatch
3068 */
3069 return cfqq->dispatched < max_dispatch;
3070}
3071
3072/*
3073 * Dispatch a request from cfqq, moving them to the request queue
3074 * dispatch list.
3075 */
3076static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3077{
3078 struct request *rq;
3079
3080 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3081
3082 if (!cfq_may_dispatch(cfqd, cfqq))
3083 return false;
3084
3085 /*
3086 * follow expired path, else get first next available
3087 */
3088 rq = cfq_check_fifo(cfqq);
3089 if (!rq)
3090 rq = cfqq->next_rq;
3091
3092 /*
3093 * insert request into driver dispatch list
3094 */
3095 cfq_dispatch_insert(cfqd->queue, rq);
3096
3097 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003098 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003099
Tejun Heoc5869802011-12-14 00:33:41 +01003100 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003101 cfqd->active_cic = cic;
3102 }
3103
3104 return true;
3105}
3106
3107/*
3108 * Find the cfqq that we need to service and move a request from that to the
3109 * dispatch list
3110 */
3111static int cfq_dispatch_requests(struct request_queue *q, int force)
3112{
3113 struct cfq_data *cfqd = q->elevator->elevator_data;
3114 struct cfq_queue *cfqq;
3115
3116 if (!cfqd->busy_queues)
3117 return 0;
3118
3119 if (unlikely(force))
3120 return cfq_forced_dispatch(cfqd);
3121
3122 cfqq = cfq_select_queue(cfqd);
3123 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003124 return 0;
3125
Jens Axboe2f5cb732009-04-07 08:51:19 +02003126 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003127 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003128 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003129 if (!cfq_dispatch_request(cfqd, cfqq))
3130 return 0;
3131
Jens Axboe2f5cb732009-04-07 08:51:19 +02003132 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003133 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003134
3135 /*
3136 * expire an async queue immediately if it has used up its slice. idle
3137 * queue always expire after 1 dispatch round.
3138 */
3139 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3140 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3141 cfq_class_idle(cfqq))) {
3142 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003143 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003144 }
3145
Shan Weib217a902009-09-01 10:06:42 +02003146 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003147 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150/*
Jens Axboe5e705372006-07-13 12:39:25 +02003151 * task holds one reference to the queue, dropped when task exits. each rq
3152 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003154 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 * queue lock must be held here.
3156 */
3157static void cfq_put_queue(struct cfq_queue *cfqq)
3158{
Jens Axboe22e2c502005-06-27 10:55:12 +02003159 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003160 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003161
Shaohua Li30d7b942011-01-07 08:46:59 +01003162 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Shaohua Li30d7b942011-01-07 08:46:59 +01003164 cfqq->ref--;
3165 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return;
3167
Jens Axboe7b679132008-05-30 12:23:07 +02003168 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003170 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003171 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003173 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003174 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003175 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003176 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003177
Vivek Goyalf04a6422009-12-03 12:59:40 -05003178 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003180 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Shaohua Lid02a2c02010-05-25 10:16:53 +02003183static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003184{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003185 struct cfq_queue *__cfqq, *next;
3186
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003187 /*
3188 * If this queue was scheduled to merge with another queue, be
3189 * sure to drop the reference taken on that queue (and others in
3190 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3191 */
3192 __cfqq = cfqq->new_cfqq;
3193 while (__cfqq) {
3194 if (__cfqq == cfqq) {
3195 WARN(1, "cfqq->new_cfqq loop detected\n");
3196 break;
3197 }
3198 next = __cfqq->new_cfqq;
3199 cfq_put_queue(__cfqq);
3200 __cfqq = next;
3201 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003202}
3203
3204static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3205{
3206 if (unlikely(cfqq == cfqd->active_queue)) {
3207 __cfq_slice_expired(cfqd, cfqq, 0);
3208 cfq_schedule_dispatch(cfqd);
3209 }
3210
3211 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003212
Jens Axboe89850f72006-07-22 16:48:31 +02003213 cfq_put_queue(cfqq);
3214}
3215
Tejun Heo9b84cac2011-12-14 00:33:42 +01003216static void cfq_init_icq(struct io_cq *icq)
3217{
3218 struct cfq_io_cq *cic = icq_to_cic(icq);
3219
3220 cic->ttime.last_end_request = jiffies;
3221}
3222
Tejun Heoc5869802011-12-14 00:33:41 +01003223static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003224{
Tejun Heoc5869802011-12-14 00:33:41 +01003225 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003226 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003227
Jens Axboeff6657c2009-04-08 10:58:57 +02003228 if (cic->cfqq[BLK_RW_ASYNC]) {
3229 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3230 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003231 }
3232
Jens Axboeff6657c2009-04-08 10:58:57 +02003233 if (cic->cfqq[BLK_RW_SYNC]) {
3234 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3235 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003236 }
Jens Axboe89850f72006-07-22 16:48:31 +02003237}
3238
Tejun Heoabede6d2012-03-19 15:10:57 -07003239static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003240{
3241 struct task_struct *tsk = current;
3242 int ioprio_class;
3243
Jens Axboe3b181522005-06-27 10:56:24 +02003244 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003245 return;
3246
Tejun Heo598971b2012-03-19 15:10:58 -07003247 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003248 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003249 default:
3250 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3251 case IOPRIO_CLASS_NONE:
3252 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003253 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003254 */
3255 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003256 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003257 break;
3258 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003259 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003260 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3261 break;
3262 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003263 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003264 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3265 break;
3266 case IOPRIO_CLASS_IDLE:
3267 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3268 cfqq->ioprio = 7;
3269 cfq_clear_cfqq_idle_window(cfqq);
3270 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003271 }
3272
3273 /*
3274 * keep track of original prio settings in case we have to temporarily
3275 * elevate the priority of this queue
3276 */
3277 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003278 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003279}
3280
Tejun Heo598971b2012-03-19 15:10:58 -07003281static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003282{
Tejun Heo598971b2012-03-19 15:10:58 -07003283 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003284 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003285 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003286
Tejun Heo598971b2012-03-19 15:10:58 -07003287 /*
3288 * Check whether ioprio has changed. The condition may trigger
3289 * spuriously on a newly created cic but there's no harm.
3290 */
3291 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003292 return;
3293
Jens Axboeff6657c2009-04-08 10:58:57 +02003294 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003295 if (cfqq) {
3296 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003297 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3298 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003299 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003300 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003301 cfq_put_queue(cfqq);
3302 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003303 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003304
Jens Axboeff6657c2009-04-08 10:58:57 +02003305 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003306 if (cfqq)
3307 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003308
3309 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003310}
3311
Jens Axboed5036d72009-06-26 10:44:34 +02003312static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003313 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003314{
3315 RB_CLEAR_NODE(&cfqq->rb_node);
3316 RB_CLEAR_NODE(&cfqq->p_node);
3317 INIT_LIST_HEAD(&cfqq->fifo);
3318
Shaohua Li30d7b942011-01-07 08:46:59 +01003319 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003320 cfqq->cfqd = cfqd;
3321
3322 cfq_mark_cfqq_prio_changed(cfqq);
3323
3324 if (is_sync) {
3325 if (!cfq_class_idle(cfqq))
3326 cfq_mark_cfqq_idle_window(cfqq);
3327 cfq_mark_cfqq_sync(cfqq);
3328 }
3329 cfqq->pid = pid;
3330}
3331
Vivek Goyal246103332009-12-03 12:59:51 -05003332#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003333static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003334{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003335 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003336 struct cfq_queue *sync_cfqq;
3337 uint64_t id;
Vivek Goyal246103332009-12-03 12:59:51 -05003338
Tejun Heo598971b2012-03-19 15:10:58 -07003339 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07003340 id = bio_blkcg(bio)->id;
Tejun Heo598971b2012-03-19 15:10:58 -07003341 rcu_read_unlock();
3342
3343 /*
3344 * Check whether blkcg has changed. The condition may trigger
3345 * spuriously on a newly created cic but there's no harm.
3346 */
3347 if (unlikely(!cfqd) || likely(cic->blkcg_id == id))
Vivek Goyal246103332009-12-03 12:59:51 -05003348 return;
3349
Tejun Heo598971b2012-03-19 15:10:58 -07003350 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003351 if (sync_cfqq) {
3352 /*
3353 * Drop reference to sync queue. A new sync queue will be
3354 * assigned in new group upon arrival of a fresh request.
3355 */
3356 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3357 cic_set_cfqq(cic, NULL, 1);
3358 cfq_put_queue(sync_cfqq);
3359 }
Tejun Heo598971b2012-03-19 15:10:58 -07003360
3361 cic->blkcg_id = id;
Vivek Goyal246103332009-12-03 12:59:51 -05003362}
Tejun Heo598971b2012-03-19 15:10:58 -07003363#else
3364static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003365#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003368cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3369 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Tejun Heo3c798392012-04-16 13:57:25 -07003371 struct blkcg *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003373 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003376 rcu_read_lock();
3377
Tejun Heo3c798392012-04-16 13:57:25 -07003378 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003379 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003380 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Jens Axboe6118b702009-06-30 09:34:12 +02003382 /*
3383 * Always try a new alloc if we fell back to the OOM cfqq
3384 * originally, since it should just be a temporary situation.
3385 */
3386 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3387 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 if (new_cfqq) {
3389 cfqq = new_cfqq;
3390 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003391 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003392 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003394 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003395 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003396 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003398 if (new_cfqq)
3399 goto retry;
Jens Axboe22e2c502005-06-27 10:55:12 +02003400 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003401 cfqq = kmem_cache_alloc_node(cfq_pool,
3402 gfp_mask | __GFP_ZERO,
3403 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Jens Axboe6118b702009-06-30 09:34:12 +02003406 if (cfqq) {
3407 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003408 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003409 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003410 cfq_log_cfqq(cfqd, cfqq, "alloced");
3411 } else
3412 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
3414
3415 if (new_cfqq)
3416 kmem_cache_free(cfq_pool, new_cfqq);
3417
Tejun Heo2a7f1242012-03-05 13:15:01 -08003418 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 return cfqq;
3420}
3421
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003422static struct cfq_queue **
3423cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3424{
Jens Axboefe094d92008-01-31 13:08:54 +01003425 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003426 case IOPRIO_CLASS_RT:
3427 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003428 case IOPRIO_CLASS_NONE:
3429 ioprio = IOPRIO_NORM;
3430 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003431 case IOPRIO_CLASS_BE:
3432 return &cfqd->async_cfqq[1][ioprio];
3433 case IOPRIO_CLASS_IDLE:
3434 return &cfqd->async_idle_cfqq;
3435 default:
3436 BUG();
3437 }
3438}
3439
Jens Axboe15c31be2007-07-10 13:43:25 +02003440static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003441cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003442 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003443{
Tejun Heo598971b2012-03-19 15:10:58 -07003444 const int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3445 const int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003446 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003447 struct cfq_queue *cfqq = NULL;
3448
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003449 if (!is_sync) {
3450 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3451 cfqq = *async_cfqq;
3452 }
3453
Jens Axboe6118b702009-06-30 09:34:12 +02003454 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003455 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003456
3457 /*
3458 * pin the queue now that it's allocated, scheduler exit will prune it
3459 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003460 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003461 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003462 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003463 }
3464
Shaohua Li30d7b942011-01-07 08:46:59 +01003465 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003466 return cfqq;
3467}
3468
Jens Axboe22e2c502005-06-27 10:55:12 +02003469static void
Shaohua Li383cd722011-07-12 14:24:35 +02003470__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003471{
Shaohua Li383cd722011-07-12 14:24:35 +02003472 unsigned long elapsed = jiffies - ttime->last_end_request;
3473 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003474
Shaohua Li383cd722011-07-12 14:24:35 +02003475 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3476 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3477 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3478}
3479
3480static void
3481cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003482 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003483{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003484 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003485 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003486 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3487 cfqd->cfq_slice_idle);
3488 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003489#ifdef CONFIG_CFQ_GROUP_IOSCHED
3490 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3491#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003492}
3493
Jens Axboe206dc692006-03-28 13:03:44 +02003494static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003495cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003496 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003497{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003498 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003499 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003500 if (cfqq->last_request_pos) {
3501 if (cfqq->last_request_pos < blk_rq_pos(rq))
3502 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3503 else
3504 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3505 }
Jens Axboe206dc692006-03-28 13:03:44 +02003506
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003507 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003508 if (blk_queue_nonrot(cfqd->queue))
3509 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3510 else
3511 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003512}
Jens Axboe22e2c502005-06-27 10:55:12 +02003513
3514/*
3515 * Disable idle window if the process thinks too long or seeks so much that
3516 * it doesn't matter
3517 */
3518static void
3519cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003520 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003521{
Jens Axboe7b679132008-05-30 12:23:07 +02003522 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003523
Jens Axboe08717142008-01-28 11:38:15 +01003524 /*
3525 * Don't idle for async or idle io prio class
3526 */
3527 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003528 return;
3529
Jens Axboec265a7f2008-06-26 13:49:33 +02003530 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003531
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003532 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3533 cfq_mark_cfqq_deep(cfqq);
3534
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003535 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3536 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003537 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003538 !cfqd->cfq_slice_idle ||
3539 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003540 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003541 else if (sample_valid(cic->ttime.ttime_samples)) {
3542 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003543 enable_idle = 0;
3544 else
3545 enable_idle = 1;
3546 }
3547
Jens Axboe7b679132008-05-30 12:23:07 +02003548 if (old_idle != enable_idle) {
3549 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3550 if (enable_idle)
3551 cfq_mark_cfqq_idle_window(cfqq);
3552 else
3553 cfq_clear_cfqq_idle_window(cfqq);
3554 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003555}
3556
Jens Axboe22e2c502005-06-27 10:55:12 +02003557/*
3558 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3559 * no or if we aren't sure, a 1 will cause a preempt.
3560 */
Jens Axboea6151c32009-10-07 20:02:57 +02003561static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003562cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003563 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003564{
Jens Axboe6d048f52007-04-25 12:44:27 +02003565 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003566
Jens Axboe6d048f52007-04-25 12:44:27 +02003567 cfqq = cfqd->active_queue;
3568 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003569 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003570
Jens Axboe6d048f52007-04-25 12:44:27 +02003571 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003572 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003573
3574 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003575 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003576
Jens Axboe22e2c502005-06-27 10:55:12 +02003577 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003578 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3579 */
3580 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3581 return false;
3582
3583 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003584 * if the new request is sync, but the currently running queue is
3585 * not, let the sync request have priority.
3586 */
Jens Axboe5e705372006-07-13 12:39:25 +02003587 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003588 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003589
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003590 if (new_cfqq->cfqg != cfqq->cfqg)
3591 return false;
3592
3593 if (cfq_slice_used(cfqq))
3594 return true;
3595
3596 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003597 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003598 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3599 new_cfqq->service_tree->count == 2 &&
3600 RB_EMPTY_ROOT(&cfqq->sort_list))
3601 return true;
3602
Jens Axboe374f84a2006-07-23 01:42:19 +02003603 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003604 * So both queues are sync. Let the new request get disk time if
3605 * it's a metadata request and the current queue is doing regular IO.
3606 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003607 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003608 return true;
3609
3610 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003611 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3612 */
3613 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003614 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003615
Shaohua Lid2d59e12010-11-08 15:01:03 +01003616 /* An idle queue should not be idle now for some reason */
3617 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3618 return true;
3619
Jens Axboe1e3335d2007-02-14 19:59:49 +01003620 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003621 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003622
3623 /*
3624 * if this request is as-good as one we would expect from the
3625 * current cfqq, let it preempt
3626 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003627 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003628 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003629
Jens Axboea6151c32009-10-07 20:02:57 +02003630 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003631}
3632
3633/*
3634 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3635 * let it have half of its nominal slice.
3636 */
3637static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3638{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003639 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3640
Jens Axboe7b679132008-05-30 12:23:07 +02003641 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003642 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003643
Jens Axboebf572252006-07-19 20:29:12 +02003644 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003645 * workload type is changed, don't save slice, otherwise preempt
3646 * doesn't happen
3647 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003648 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003649 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003650
3651 /*
Jens Axboebf572252006-07-19 20:29:12 +02003652 * Put the new queue at the front of the of the current list,
3653 * so we know that it will be selected next.
3654 */
3655 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003656
3657 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003658
Justin TerAvest62a37f62011-03-23 08:25:44 +01003659 cfqq->slice_end = 0;
3660 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003661}
3662
3663/*
Jens Axboe5e705372006-07-13 12:39:25 +02003664 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003665 * something we should do about it
3666 */
3667static void
Jens Axboe5e705372006-07-13 12:39:25 +02003668cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3669 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003670{
Tejun Heoc5869802011-12-14 00:33:41 +01003671 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003672
Aaron Carroll45333d52008-08-26 15:52:36 +02003673 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003674 if (rq->cmd_flags & REQ_PRIO)
3675 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003676
Shaohua Li383cd722011-07-12 14:24:35 +02003677 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003678 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003679 cfq_update_idle_window(cfqd, cfqq, cic);
3680
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003681 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003682
3683 if (cfqq == cfqd->active_queue) {
3684 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003685 * Remember that we saw a request from this process, but
3686 * don't start queuing just yet. Otherwise we risk seeing lots
3687 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003688 * and merging. If the request is already larger than a single
3689 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003690 * merging is already done. Ditto for a busy system that
3691 * has other work pending, don't risk delaying until the
3692 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003693 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003694 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003695 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3696 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003697 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003698 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003699 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003700 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07003701 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003702 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003703 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003704 }
Jens Axboe5e705372006-07-13 12:39:25 +02003705 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003706 /*
3707 * not the active queue - expire current slice if it is
3708 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003709 * has some old slice time left and is of higher priority or
3710 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02003711 */
3712 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003713 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02003714 }
3715}
3716
Jens Axboe165125e2007-07-24 09:28:11 +02003717static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003718{
Jens Axboeb4878f22005-10-20 16:42:29 +02003719 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003720 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003721
Jens Axboe7b679132008-05-30 12:23:07 +02003722 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07003723 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Jens Axboe30996f42009-10-05 11:03:39 +02003725 rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
Jens Axboe22e2c502005-06-27 10:55:12 +02003726 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01003727 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07003728 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
3729 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02003730 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Aaron Carroll45333d52008-08-26 15:52:36 +02003733/*
3734 * Update hw_tag based on peak queue depth over 50 samples under
3735 * sufficient load.
3736 */
3737static void cfq_update_hw_tag(struct cfq_data *cfqd)
3738{
Shaohua Li1a1238a2009-10-27 08:46:23 +01003739 struct cfq_queue *cfqq = cfqd->active_queue;
3740
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003741 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
3742 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003743
3744 if (cfqd->hw_tag == 1)
3745 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02003746
3747 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003748 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003749 return;
3750
Shaohua Li1a1238a2009-10-27 08:46:23 +01003751 /*
3752 * If active queue hasn't enough requests and can idle, cfq might not
3753 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
3754 * case
3755 */
3756 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
3757 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003758 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01003759 return;
3760
Aaron Carroll45333d52008-08-26 15:52:36 +02003761 if (cfqd->hw_tag_samples++ < 50)
3762 return;
3763
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01003764 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02003765 cfqd->hw_tag = 1;
3766 else
3767 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02003768}
3769
Vivek Goyal7667aa02009-12-08 17:52:58 -05003770static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3771{
Tejun Heoc5869802011-12-14 00:33:41 +01003772 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003773
Justin TerAvest02a8f012011-02-09 14:20:03 +01003774 /* If the queue already has requests, don't wait */
3775 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
3776 return false;
3777
Vivek Goyal7667aa02009-12-08 17:52:58 -05003778 /* If there are other queues in the group, don't wait */
3779 if (cfqq->cfqg->nr_cfqq > 1)
3780 return false;
3781
Shaohua Li7700fc42011-07-12 14:24:56 +02003782 /* the only queue in the group, but think time is big */
3783 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
3784 return false;
3785
Vivek Goyal7667aa02009-12-08 17:52:58 -05003786 if (cfq_slice_used(cfqq))
3787 return true;
3788
3789 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02003790 if (cic && sample_valid(cic->ttime.ttime_samples)
3791 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05003792 return true;
3793
3794 /*
3795 * If think times is less than a jiffy than ttime_mean=0 and above
3796 * will not be true. It might happen that slice has not expired yet
3797 * but will expire soon (4-5 ns) during select_queue(). To cover the
3798 * case where think time is less than a jiffy, mark the queue wait
3799 * busy if only 1 jiffy is left in the slice.
3800 */
3801 if (cfqq->slice_end - jiffies == 1)
3802 return true;
3803
3804 return false;
3805}
3806
Jens Axboe165125e2007-07-24 09:28:11 +02003807static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
Jens Axboe5e705372006-07-13 12:39:25 +02003809 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003810 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02003811 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02003812 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Jens Axboeb4878f22005-10-20 16:42:29 +02003814 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02003815 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
3816 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Aaron Carroll45333d52008-08-26 15:52:36 +02003818 cfq_update_hw_tag(cfqd);
3819
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003820 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02003821 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003822 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02003823 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003824 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07003825 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
3826 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003828 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003829
Vivek Goyal365722b2009-10-03 15:21:27 +02003830 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04003831 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003832
Shaohua Li383cd722011-07-12 14:24:35 +02003833 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003834
3835 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04003836 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003837 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04003838 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
3839 cfqq_type(cfqq));
3840
3841 st->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003842 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
3843 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02003844 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003845
Shaohua Li7700fc42011-07-12 14:24:56 +02003846#ifdef CONFIG_CFQ_GROUP_IOSCHED
3847 cfqq->cfqg->ttime.last_end_request = now;
3848#endif
3849
Jens Axboecaaa5f92006-06-16 11:23:00 +02003850 /*
3851 * If this is the active queue, check if it needs to be expired,
3852 * or if we want to idle in case it has no pending requests.
3853 */
3854 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02003855 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
3856
Jens Axboe44f7c162007-01-19 11:51:58 +11003857 if (cfq_cfqq_slice_new(cfqq)) {
3858 cfq_set_prio_slice(cfqd, cfqq);
3859 cfq_clear_cfqq_slice_new(cfqq);
3860 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05003861
3862 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05003863 * Should we wait for next request to come in before we expire
3864 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05003865 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003866 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003867 unsigned long extend_sl = cfqd->cfq_slice_idle;
3868 if (!cfqd->cfq_slice_idle)
3869 extend_sl = cfqd->cfq_group_idle;
3870 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05003871 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003872 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05003873 }
3874
Jens Axboea36e71f2009-04-15 12:15:11 +02003875 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003876 * Idling is not enabled on:
3877 * - expired queues
3878 * - idle-priority queues
3879 * - async queues
3880 * - queues with still some requests queued
3881 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02003882 */
Jens Axboe08717142008-01-28 11:38:15 +01003883 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02003884 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003885 else if (sync && cfqq_empty &&
3886 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003887 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003888 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003889 }
Jens Axboe6d048f52007-04-25 12:44:27 +02003890
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003891 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02003892 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
Jens Axboe89850f72006-07-22 16:48:31 +02003895static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003896{
Jens Axboe1b379d82009-08-11 08:26:11 +02003897 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02003898 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003899 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02003900 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003901
3902 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02003903}
3904
Jens Axboe165125e2007-07-24 09:28:11 +02003905static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02003906{
3907 struct cfq_data *cfqd = q->elevator->elevator_data;
3908 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01003909 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02003910 struct cfq_queue *cfqq;
3911
3912 /*
3913 * don't force setup of a queue from here, as a call to may_queue
3914 * does not necessarily imply that a request actually will be queued.
3915 * so just lookup a possibly existing queue, or return 'may queue'
3916 * if that fails
3917 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01003918 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02003919 if (!cic)
3920 return ELV_MQUEUE_MAY;
3921
Jens Axboeb0b78f82009-04-08 10:56:08 +02003922 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02003923 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07003924 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02003925
Jens Axboe89850f72006-07-22 16:48:31 +02003926 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003927 }
3928
3929 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932/*
3933 * queue lock held here
3934 */
Jens Axboebb37b942006-12-01 10:42:33 +01003935static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Jens Axboe5e705372006-07-13 12:39:25 +02003937 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Jens Axboe5e705372006-07-13 12:39:25 +02003939 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003940 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Jens Axboe22e2c502005-06-27 10:55:12 +02003942 BUG_ON(!cfqq->allocated[rw]);
3943 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003945 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003946 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01003947 rq->elv.priv[0] = NULL;
3948 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02003949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 cfq_put_queue(cfqq);
3951 }
3952}
3953
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003954static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003955cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003956 struct cfq_queue *cfqq)
3957{
3958 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
3959 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003960 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003961 cfq_put_queue(cfqq);
3962 return cic_to_cfqq(cic, 1);
3963}
3964
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003965/*
3966 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
3967 * was the last process referring to said cfqq.
3968 */
3969static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01003970split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003971{
3972 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003973 cfqq->pid = current->pid;
3974 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01003975 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003976 return cfqq;
3977 }
3978
3979 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02003980
3981 cfq_put_cooperator(cfqq);
3982
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003983 cfq_put_queue(cfqq);
3984 return NULL;
3985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986/*
Jens Axboe22e2c502005-06-27 10:55:12 +02003987 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 */
Jens Axboe22e2c502005-06-27 10:55:12 +02003989static int
Tejun Heo852c7882012-03-05 13:15:27 -08003990cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
3991 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
3993 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01003994 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02003996 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003997 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 might_sleep_if(gfp_mask & __GFP_WAIT);
4000
Tejun Heo216284c2011-12-14 00:33:38 +01004001 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004002
Tejun Heo598971b2012-03-19 15:10:58 -07004003 check_ioprio_changed(cic, bio);
4004 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004005new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004006 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004007 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004008 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004009 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004010 } else {
4011 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004012 * If the queue was seeky for too long, break it apart.
4013 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004014 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004015 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4016 cfqq = split_cfqq(cic, cfqq);
4017 if (!cfqq)
4018 goto new_queue;
4019 }
4020
4021 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004022 * Check to see if this queue is scheduled to merge with
4023 * another, closely cooperating queue. The merging of
4024 * queues happens here as it must be done in process context.
4025 * The reference on new_cfqq was taken in merge_cfqqs.
4026 */
4027 if (cfqq->new_cfqq)
4028 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004032
Jens Axboe6fae9c22011-03-01 15:04:39 -05004033 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004034 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004035 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004036 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01004037 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02004038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
David Howells65f27f32006-11-22 14:55:48 +00004041static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004042{
David Howells65f27f32006-11-22 14:55:48 +00004043 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004044 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004045 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004046
Jens Axboe40bb54d2009-04-15 12:11:10 +02004047 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004048 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004049 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004050}
4051
4052/*
4053 * Timer running if the active_queue is currently idling inside its time slice
4054 */
4055static void cfq_idle_slice_timer(unsigned long data)
4056{
4057 struct cfq_data *cfqd = (struct cfq_data *) data;
4058 struct cfq_queue *cfqq;
4059 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004060 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004061
Jens Axboe7b679132008-05-30 12:23:07 +02004062 cfq_log(cfqd, "idle timer fired");
4063
Jens Axboe22e2c502005-06-27 10:55:12 +02004064 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4065
Jens Axboefe094d92008-01-31 13:08:54 +01004066 cfqq = cfqd->active_queue;
4067 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004068 timed_out = 0;
4069
Jens Axboe22e2c502005-06-27 10:55:12 +02004070 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004071 * We saw a request before the queue expired, let it through
4072 */
4073 if (cfq_cfqq_must_dispatch(cfqq))
4074 goto out_kick;
4075
4076 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004077 * expired
4078 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004079 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004080 goto expire;
4081
4082 /*
4083 * only expire and reinvoke request handler, if there are
4084 * other queues with pending requests
4085 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004086 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004087 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004088
4089 /*
4090 * not expired and it has a request pending, let it dispatch
4091 */
Jens Axboe75e50982009-04-07 08:56:14 +02004092 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004093 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004094
4095 /*
4096 * Queue depth flag is reset only when the idle didn't succeed
4097 */
4098 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004099 }
4100expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004101 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004102out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004103 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004104out_cont:
4105 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4106}
4107
Jens Axboe3b181522005-06-27 10:56:24 +02004108static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4109{
4110 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004111 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004112}
Jens Axboe22e2c502005-06-27 10:55:12 +02004113
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004114static void cfq_put_async_queues(struct cfq_data *cfqd)
4115{
4116 int i;
4117
4118 for (i = 0; i < IOPRIO_BE_NR; i++) {
4119 if (cfqd->async_cfqq[0][i])
4120 cfq_put_queue(cfqd->async_cfqq[0][i]);
4121 if (cfqd->async_cfqq[1][i])
4122 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004123 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01004124
4125 if (cfqd->async_idle_cfqq)
4126 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004127}
4128
Jens Axboeb374d182008-10-31 10:05:07 +01004129static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Jens Axboe22e2c502005-06-27 10:55:12 +02004131 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004132 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004133
Jens Axboe3b181522005-06-27 10:56:24 +02004134 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004135
Al Virod9ff4182006-03-18 13:51:22 -05004136 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004137
Al Virod9ff4182006-03-18 13:51:22 -05004138 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004139 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004140
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004141 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08004142
4143 spin_unlock_irq(q->queue_lock);
4144
Al Viroa90d7422006-03-18 12:05:37 -05004145 cfq_shutdown_timer_wq(cfqd);
4146
Tejun Heoffea73f2012-06-04 10:02:29 +02004147#ifdef CONFIG_CFQ_GROUP_IOSCHED
4148 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4149#else
Tejun Heof51b8022012-03-05 13:15:05 -08004150 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004151#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004152 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
Tejun Heob2fab5a2012-03-05 13:14:57 -08004155static int cfq_init_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004158 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004159 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Christoph Lameter94f60302007-07-17 04:03:29 -07004161 cfqd = kmalloc_node(sizeof(*cfqd), GFP_KERNEL | __GFP_ZERO, q->node);
Tejun Heoa73f7302011-12-14 00:33:37 +01004162 if (!cfqd)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004163 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004164
Tejun Heof51b8022012-03-05 13:15:05 -08004165 cfqd->queue = q;
4166 q->elevator->elevator_data = cfqd;
4167
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004168 /* Init root service tree */
4169 cfqd->grp_service_tree = CFQ_RB_ROOT;
4170
Tejun Heof51b8022012-03-05 13:15:05 -08004171 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004172#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004173 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004174 if (ret)
4175 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004176
Tejun Heoa2b16932012-04-13 13:11:33 -07004177 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004178#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004179 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004180 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4181 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004182 if (!cfqd->root_group)
4183 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004184
Tejun Heoa2b16932012-04-13 13:11:33 -07004185 cfq_init_cfqg_base(cfqd->root_group);
4186#endif
Tejun Heo3381cb82012-04-01 14:38:44 -07004187 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -08004188 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004189
Jens Axboe26a2ac02009-04-23 12:13:27 +02004190 /*
4191 * Not strictly needed (since RB_ROOT just clears the node and we
4192 * zeroed cfqd on alloc), but better be safe in case someone decides
4193 * to add magic to the rb code
4194 */
4195 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4196 cfqd->prio_trees[i] = RB_ROOT;
4197
Jens Axboe6118b702009-06-30 09:34:12 +02004198 /*
4199 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4200 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004201 * will not attempt to free it. oom_cfqq is linked to root_group
4202 * but shouldn't hold a reference as it'll never be unlinked. Lose
4203 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004204 */
4205 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004206 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004207
4208 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004209 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004210 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004211 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Jens Axboe22e2c502005-06-27 10:55:12 +02004213 init_timer(&cfqd->idle_slice_timer);
4214 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4215 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4216
Jens Axboe23e018a2009-10-05 08:52:35 +02004217 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004220 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4221 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 cfqd->cfq_back_max = cfq_back_max;
4223 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004224 cfqd->cfq_slice[0] = cfq_slice_async;
4225 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004226 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004227 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
4228 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004229 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004230 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004231 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004232 /*
4233 * we optimistically start assuming sync ops weren't delayed in last
4234 * second, in order to have larger depth for async operations.
4235 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004236 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004237 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004238
4239out_free:
4240 kfree(cfqd);
4241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/*
4245 * sysfs parts below -->
4246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247static ssize_t
4248cfq_var_show(unsigned int var, char *page)
4249{
4250 return sprintf(page, "%d\n", var);
4251}
4252
4253static ssize_t
4254cfq_var_store(unsigned int *var, const char *page, size_t count)
4255{
4256 char *p = (char *) page;
4257
4258 *var = simple_strtoul(p, &p, 10);
4259 return count;
4260}
4261
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004263static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004265 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 unsigned int __data = __VAR; \
4267 if (__CONV) \
4268 __data = jiffies_to_msecs(__data); \
4269 return cfq_var_show(__data, (page)); \
4270}
4271SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004272SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4273SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004274SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4275SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004276SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004277SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004278SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4279SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4280SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004281SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004282SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283#undef SHOW_FUNCTION
4284
4285#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004286static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004288 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 unsigned int __data; \
4290 int ret = cfq_var_store(&__data, (page), count); \
4291 if (__data < (MIN)) \
4292 __data = (MIN); \
4293 else if (__data > (MAX)) \
4294 __data = (MAX); \
4295 if (__CONV) \
4296 *(__PTR) = msecs_to_jiffies(__data); \
4297 else \
4298 *(__PTR) = __data; \
4299 return ret; \
4300}
4301STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004302STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4303 UINT_MAX, 1);
4304STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4305 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004306STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004307STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4308 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004309STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004310STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004311STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4312STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004313STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4314 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004315STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004316STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317#undef STORE_FUNCTION
4318
Al Viroe572ec72006-03-18 22:27:18 -05004319#define CFQ_ATTR(name) \
4320 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004321
Al Viroe572ec72006-03-18 22:27:18 -05004322static struct elv_fs_entry cfq_attrs[] = {
4323 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004324 CFQ_ATTR(fifo_expire_sync),
4325 CFQ_ATTR(fifo_expire_async),
4326 CFQ_ATTR(back_seek_max),
4327 CFQ_ATTR(back_seek_penalty),
4328 CFQ_ATTR(slice_sync),
4329 CFQ_ATTR(slice_async),
4330 CFQ_ATTR(slice_async_rq),
4331 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004332 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004333 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004334 CFQ_ATTR(target_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004335 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336};
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338static struct elevator_type iosched_cfq = {
4339 .ops = {
4340 .elevator_merge_fn = cfq_merge,
4341 .elevator_merged_fn = cfq_merged_request,
4342 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004343 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004344 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004345 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004347 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004350 .elevator_former_req_fn = elv_rb_former_request,
4351 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004352 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004353 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 .elevator_set_req_fn = cfq_set_request,
4355 .elevator_put_req_fn = cfq_put_request,
4356 .elevator_may_queue_fn = cfq_may_queue,
4357 .elevator_init_fn = cfq_init_queue,
4358 .elevator_exit_fn = cfq_exit_queue,
4359 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004360 .icq_size = sizeof(struct cfq_io_cq),
4361 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004362 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004363 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 .elevator_owner = THIS_MODULE,
4365};
4366
Vivek Goyal3e252062009-12-04 10:36:42 -05004367#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004368static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004369 .pd_size = sizeof(struct cfq_group),
4370 .cftypes = cfq_blkcg_files,
4371
4372 .pd_init_fn = cfq_pd_init,
4373 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004374};
Vivek Goyal3e252062009-12-04 10:36:42 -05004375#endif
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static int __init cfq_init(void)
4378{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004379 int ret;
4380
Jens Axboe22e2c502005-06-27 10:55:12 +02004381 /*
4382 * could be 0 on HZ < 1000 setups
4383 */
4384 if (!cfq_slice_async)
4385 cfq_slice_async = 1;
4386 if (!cfq_slice_idle)
4387 cfq_slice_idle = 1;
4388
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004389#ifdef CONFIG_CFQ_GROUP_IOSCHED
4390 if (!cfq_group_idle)
4391 cfq_group_idle = 1;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004392
Tejun Heo3c798392012-04-16 13:57:25 -07004393 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004394 if (ret)
4395 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004396#else
4397 cfq_group_idle = 0;
4398#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004399
Tejun Heofd794952012-06-04 10:01:38 +02004400 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004401 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4402 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004403 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Tejun Heo3d3c2372011-12-14 00:33:42 +01004405 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004406 if (ret)
4407 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004408
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004409 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004410
4411err_free_pool:
4412 kmem_cache_destroy(cfq_pool);
4413err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004414#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004415 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004416#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
4420static void __exit cfq_exit(void)
4421{
Tejun Heoffea73f2012-06-04 10:02:29 +02004422#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004423 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004424#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004426 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429module_init(cfq_init);
4430module_exit(cfq_exit);
4431
4432MODULE_AUTHOR("Jens Axboe");
4433MODULE_LICENSE("GPL");
4434MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");