blob: c62bb2e650b8c741e64ead5c9f32b090cbf19730 [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 Heoeea8f412015-05-22 17:13:17 -040017#include <linux/blk-cgroup.h>
Tejun Heo6e736be2011-12-14 00:33:38 +010018#include "blk.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
Arianna Avanzinie48453c2015-06-05 23:38:42 +020070/* blkio-related constants */
71#define CFQ_WEIGHT_MIN 10
72#define CFQ_WEIGHT_MAX 1000
73#define CFQ_WEIGHT_DEFAULT 500
74
Tejun Heoc5869802011-12-14 00:33:41 +010075struct cfq_ttime {
76 unsigned long last_end_request;
77
78 unsigned long ttime_total;
79 unsigned long ttime_samples;
80 unsigned long ttime_mean;
81};
82
Jens Axboe22e2c502005-06-27 10:55:12 +020083/*
Jens Axboecc09e292007-04-26 12:53:50 +020084 * Most of our rbtree usage is for sorting with min extraction, so
85 * if we cache the leftmost node we don't have to walk down the tree
86 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
87 * move this into the elevator for the rq sorting as well.
88 */
89struct cfq_rb_root {
90 struct rb_root rb;
91 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010092 unsigned count;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050093 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020094 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020095};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020096#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
97 .ttime = {.last_end_request = jiffies,},}
Jens Axboecc09e292007-04-26 12:53:50 +020098
99/*
Jens Axboe6118b702009-06-30 09:34:12 +0200100 * Per process-grouping structure
101 */
102struct cfq_queue {
103 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100104 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200105 /* various state flags, see below */
106 unsigned int flags;
107 /* parent cfq_data */
108 struct cfq_data *cfqd;
109 /* service_tree member */
110 struct rb_node rb_node;
111 /* service_tree key */
112 unsigned long rb_key;
113 /* prio tree member */
114 struct rb_node p_node;
115 /* prio tree root we belong to, if any */
116 struct rb_root *p_root;
117 /* sorted list of pending requests */
118 struct rb_root sort_list;
119 /* if fifo isn't expired, next request to serve */
120 struct request *next_rq;
121 /* requests queued in sort_list */
122 int queued[2];
123 /* currently allocated requests */
124 int allocated[2];
125 /* fifo list of requests in sort_list */
126 struct list_head fifo;
127
Vivek Goyaldae739e2009-12-03 12:59:45 -0500128 /* time when queue got scheduled in to dispatch first request. */
129 unsigned long dispatch_start;
Vivek Goyalf75edf22009-12-03 12:59:53 -0500130 unsigned int allocated_slice;
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100131 unsigned int slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500132 /* time when first request from queue completed and slice started. */
133 unsigned long slice_start;
Jens Axboe6118b702009-06-30 09:34:12 +0200134 unsigned long slice_end;
135 long slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200136
Christoph Hellwig65299a32011-08-23 14:50:29 +0200137 /* pending priority requests */
138 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200139 /* number of requests that are on the dispatch list or inside driver */
140 int dispatched;
141
142 /* io prio of this group */
143 unsigned short ioprio, org_ioprio;
Justin TerAvest4aede842011-07-12 08:31:45 +0200144 unsigned short ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200145
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100146 pid_t pid;
147
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100148 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400149 sector_t last_request_pos;
150
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100151 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400152 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500153 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200154 /* Number of sectors dispatched from queue in single dispatch round */
155 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200156};
157
158/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100159 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100160 * IDLE is handled separately, so it has negative index
161 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400162enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100163 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500164 RT_WORKLOAD = 1,
165 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200166 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100167};
168
169/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100170 * Second index in the service_trees.
171 */
172enum wl_type_t {
173 ASYNC_WORKLOAD = 0,
174 SYNC_NOIDLE_WORKLOAD = 1,
175 SYNC_WORKLOAD = 2
176};
177
Tejun Heo155fead2012-04-01 14:38:44 -0700178struct cfqg_stats {
179#ifdef CONFIG_CFQ_GROUP_IOSCHED
180 /* total bytes transferred */
181 struct blkg_rwstat service_bytes;
182 /* total IOs serviced, post merge */
183 struct blkg_rwstat serviced;
184 /* number of ios merged */
185 struct blkg_rwstat merged;
186 /* total time spent on device in ns, may not be accurate w/ queueing */
187 struct blkg_rwstat service_time;
188 /* total time spent waiting in scheduler queue in ns */
189 struct blkg_rwstat wait_time;
190 /* number of IOs queued up */
191 struct blkg_rwstat queued;
192 /* total sectors transferred */
193 struct blkg_stat sectors;
194 /* total disk time and nr sectors dispatched by this group */
195 struct blkg_stat time;
196#ifdef CONFIG_DEBUG_BLK_CGROUP
197 /* time not charged to this cgroup */
198 struct blkg_stat unaccounted_time;
199 /* sum of number of ios queued across all samples */
200 struct blkg_stat avg_queue_size_sum;
201 /* count of samples taken for average */
202 struct blkg_stat avg_queue_size_samples;
203 /* how many times this group has been removed from service tree */
204 struct blkg_stat dequeue;
205 /* total time spent waiting for it to be assigned a timeslice. */
206 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700207 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700208 struct blkg_stat idle_time;
209 /* total time with empty current active q with other requests queued */
210 struct blkg_stat empty_time;
211 /* fields after this shouldn't be cleared on stat reset */
212 uint64_t start_group_wait_time;
213 uint64_t start_idle_time;
214 uint64_t start_empty_time;
215 uint16_t flags;
216#endif /* CONFIG_DEBUG_BLK_CGROUP */
217#endif /* CONFIG_CFQ_GROUP_IOSCHED */
218};
219
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200220/* Per-cgroup data */
221struct cfq_group_data {
222 /* must be the first member */
223 struct blkcg_policy_data pd;
224
225 unsigned int weight;
226 unsigned int leaf_weight;
227};
228
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500229/* This is per cgroup per device grouping structure */
230struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700231 /* must be the first member */
232 struct blkg_policy_data pd;
233
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500234 /* group service_tree member */
235 struct rb_node rb_node;
236
237 /* group service_tree key */
238 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800239
240 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800241 * The number of active cfqgs and sum of their weights under this
242 * cfqg. This covers this cfqg's leaf_weight and all children's
243 * weights, but does not cover weights of further descendants.
244 *
245 * If a cfqg is on the service tree, it's active. An active cfqg
246 * also activates its parent and contributes to the children_weight
247 * of the parent.
248 */
249 int nr_active;
250 unsigned int children_weight;
251
252 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800253 * vfraction is the fraction of vdisktime that the tasks in this
254 * cfqg are entitled to. This is determined by compounding the
255 * ratios walking up from this cfqg to the root.
256 *
257 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
258 * vfractions on a service tree is approximately 1. The sum may
259 * deviate a bit due to rounding errors and fluctuations caused by
260 * cfqgs entering and leaving the service tree.
261 */
262 unsigned int vfraction;
263
264 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800265 * There are two weights - (internal) weight is the weight of this
266 * cfqg against the sibling cfqgs. leaf_weight is the wight of
267 * this cfqg against the child cfqgs. For the root cfqg, both
268 * weights are kept in sync for backward compatibility.
269 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500270 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100271 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700272 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500273
Tejun Heoe71357e2013-01-09 08:05:10 -0800274 unsigned int leaf_weight;
275 unsigned int new_leaf_weight;
276 unsigned int dev_leaf_weight;
277
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500278 /* number of cfqq currently on this group */
279 int nr_cfqq;
280
Jens Axboe22e2c502005-06-27 10:55:12 +0200281 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200282 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200283 * create the array for each prio class but at run time it is used
284 * only for RT and BE class and slot for IDLE class remains unused.
285 * This is primarily done to avoid confusion and a gcc warning.
286 */
287 unsigned int busy_queues_avg[CFQ_PRIO_NR];
288 /*
289 * rr lists of queues with requests. We maintain service trees for
290 * RT and BE classes. These trees are subdivided in subclasses
291 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
292 * class there is no subclassification and all the cfq queues go on
293 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100294 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200295 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100296 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100297 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500298
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400299 unsigned long saved_wl_slice;
300 enum wl_type_t saved_wl_type;
301 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800302
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200303 /* number of requests that are on the dispatch list or inside driver */
304 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200305 struct cfq_ttime ttime;
Tejun Heo0b399202013-01-09 08:05:13 -0800306 struct cfqg_stats stats; /* stats for this cfqg */
307 struct cfqg_stats dead_stats; /* stats pushed from dead children */
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500308};
309
Tejun Heoc5869802011-12-14 00:33:41 +0100310struct cfq_io_cq {
311 struct io_cq icq; /* must be the first member */
312 struct cfq_queue *cfqq[2];
313 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700314 int ioprio; /* the current ioprio */
315#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof4da8072014-09-08 08:15:20 +0900316 uint64_t blkcg_serial_nr; /* the current blkcg serial */
Tejun Heo598971b2012-03-19 15:10:58 -0700317#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100318};
319
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500320/*
321 * Per block device queue structure
322 */
323struct cfq_data {
324 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500325 /* Root service tree for cfq_groups */
326 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800327 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500328
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100329 /*
330 * The priority currently being served
331 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400332 enum wl_class_t serving_wl_class;
333 enum wl_type_t serving_wl_type;
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100334 unsigned long workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500335 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200336
337 /*
338 * Each priority tree is sorted by next_request position. These
339 * trees are used when determining if two or more queues are
340 * interleaving requests (see cfq_close_cooperator).
341 */
342 struct rb_root prio_trees[CFQ_PRIO_LISTS];
343
Jens Axboe22e2c502005-06-27 10:55:12 +0200344 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100345 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200346
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100347 int rq_in_driver;
348 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200349
350 /*
351 * queue-depth detection
352 */
353 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200354 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100355 /*
356 * hw_tag can be
357 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
358 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
359 * 0 => no NCQ
360 */
361 int hw_tag_est_depth;
362 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200363
364 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200365 * idle window management
366 */
367 struct timer_list idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200368 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200369
370 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100371 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200372
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +0200373 /*
374 * async queue for each priority case
375 */
376 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
377 struct cfq_queue *async_idle_cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +0200378
Jens Axboe6d048f52007-04-25 12:44:27 +0200379 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * tunables, see top of file
383 */
384 unsigned int cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +0200385 unsigned int cfq_fifo_expire[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned int cfq_back_penalty;
387 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200388 unsigned int cfq_slice[2];
389 unsigned int cfq_slice_async_rq;
390 unsigned int cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200391 unsigned int cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +0200392 unsigned int cfq_latency;
Tao Ma5bf14c02012-04-01 14:33:39 -0700393 unsigned int cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500394
Jens Axboe6118b702009-06-30 09:34:12 +0200395 /*
396 * Fallback dummy cfqq for extreme OOM conditions
397 */
398 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200399
Corrado Zoccolo573412b2009-12-06 11:48:52 +0100400 unsigned long last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Vivek Goyal25fb5162009-12-03 12:59:46 -0500403static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
404
Vivek Goyal34b98d02012-10-03 16:56:58 -0400405static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400406 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500407 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100408{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500409 if (!cfqg)
410 return NULL;
411
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400412 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500413 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100414
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400415 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100416}
417
Jens Axboe3b181522005-06-27 10:56:24 +0200418enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100419 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
420 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200421 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100422 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100423 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
424 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
425 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100426 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200427 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400428 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100429 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100430 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500431 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200432};
433
434#define CFQ_CFQQ_FNS(name) \
435static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
436{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100437 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200438} \
439static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
440{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100441 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200442} \
443static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
444{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100445 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200446}
447
448CFQ_CFQQ_FNS(on_rr);
449CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200450CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200451CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200452CFQ_CFQQ_FNS(fifo_expire);
453CFQ_CFQQ_FNS(idle_window);
454CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100455CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200456CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200457CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100458CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100459CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500460CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200461#undef CFQ_CFQQ_FNS
462
Tejun Heo629ed0b2012-04-01 14:38:44 -0700463#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700464
Tejun Heo155fead2012-04-01 14:38:44 -0700465/* cfqg stats flags */
466enum cfqg_stats_flags {
467 CFQG_stats_waiting = 0,
468 CFQG_stats_idling,
469 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700470};
471
Tejun Heo155fead2012-04-01 14:38:44 -0700472#define CFQG_FLAG_FNS(name) \
473static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700474{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700475 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700476} \
Tejun Heo155fead2012-04-01 14:38:44 -0700477static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700478{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700479 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700480} \
Tejun Heo155fead2012-04-01 14:38:44 -0700481static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700482{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700483 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700484} \
485
Tejun Heo155fead2012-04-01 14:38:44 -0700486CFQG_FLAG_FNS(waiting)
487CFQG_FLAG_FNS(idling)
488CFQG_FLAG_FNS(empty)
489#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700490
491/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700492static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700493{
494 unsigned long long now;
495
Tejun Heo155fead2012-04-01 14:38:44 -0700496 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700497 return;
498
499 now = sched_clock();
500 if (time_after64(now, stats->start_group_wait_time))
501 blkg_stat_add(&stats->group_wait_time,
502 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700503 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700504}
505
506/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700507static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
508 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700509{
Tejun Heo155fead2012-04-01 14:38:44 -0700510 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700511
Tejun Heo155fead2012-04-01 14:38:44 -0700512 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700513 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700514 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700515 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700516 stats->start_group_wait_time = sched_clock();
517 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700518}
519
520/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700521static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700522{
523 unsigned long long now;
524
Tejun Heo155fead2012-04-01 14:38:44 -0700525 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700526 return;
527
528 now = sched_clock();
529 if (time_after64(now, stats->start_empty_time))
530 blkg_stat_add(&stats->empty_time,
531 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700532 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700533}
534
Tejun Heo155fead2012-04-01 14:38:44 -0700535static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700536{
Tejun Heo155fead2012-04-01 14:38:44 -0700537 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700538}
539
Tejun Heo155fead2012-04-01 14:38:44 -0700540static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700541{
Tejun Heo155fead2012-04-01 14:38:44 -0700542 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700543
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800544 if (blkg_rwstat_total(&stats->queued))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700545 return;
546
547 /*
548 * group is already marked empty. This can happen if cfqq got new
549 * request in parent group and moved to this group while being added
550 * to service tree. Just ignore the event and move on.
551 */
Tejun Heo155fead2012-04-01 14:38:44 -0700552 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700553 return;
554
555 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700556 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700557}
558
Tejun Heo155fead2012-04-01 14:38:44 -0700559static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700560{
Tejun Heo155fead2012-04-01 14:38:44 -0700561 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700562
Tejun Heo155fead2012-04-01 14:38:44 -0700563 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700564 unsigned long long now = sched_clock();
565
566 if (time_after64(now, stats->start_idle_time))
567 blkg_stat_add(&stats->idle_time,
568 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700569 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700570 }
571}
572
Tejun Heo155fead2012-04-01 14:38:44 -0700573static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700574{
Tejun Heo155fead2012-04-01 14:38:44 -0700575 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700576
Tejun Heo155fead2012-04-01 14:38:44 -0700577 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700578
579 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700580 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700581}
582
Tejun Heo155fead2012-04-01 14:38:44 -0700583static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700584{
Tejun Heo155fead2012-04-01 14:38:44 -0700585 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700586
587 blkg_stat_add(&stats->avg_queue_size_sum,
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800588 blkg_rwstat_total(&stats->queued));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700589 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700590 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700591}
592
593#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
594
Tejun Heof48ec1d2012-04-13 13:11:25 -0700595static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
596static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
597static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
598static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
599static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
600static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
601static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700602
603#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
604
605#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700606
Jens Axboe4ceab712015-06-19 10:13:01 -0600607static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
608{
609 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
610}
611
612static struct cfq_group_data
613*cpd_to_cfqgd(struct blkcg_policy_data *cpd)
614{
615 return cpd ? container_of(cpd, struct cfq_group_data, pd) : NULL;
616}
617
618static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
619{
620 return pd_to_blkg(&cfqg->pd);
621}
622
Tejun Heoffea73f2012-06-04 10:02:29 +0200623static struct blkcg_policy blkcg_policy_cfq;
624
625static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
626{
627 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
628}
629
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200630static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
631{
632 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
633}
634
Tejun Heod02f7aa2013-01-09 08:05:11 -0800635static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
Tejun Heo7918ffb2013-01-09 08:05:11 -0800636{
Tejun Heod02f7aa2013-01-09 08:05:11 -0800637 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800638
Tejun Heod02f7aa2013-01-09 08:05:11 -0800639 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800640}
641
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100642static inline void cfqg_get(struct cfq_group *cfqg)
643{
644 return blkg_get(cfqg_to_blkg(cfqg));
645}
646
647static inline void cfqg_put(struct cfq_group *cfqg)
648{
649 return blkg_put(cfqg_to_blkg(cfqg));
650}
651
Tejun Heo54e7ed12012-04-16 13:57:23 -0700652#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
653 char __pbuf[128]; \
654 \
655 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400656 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
657 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
658 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700659 __pbuf, ##args); \
660} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500661
Tejun Heo54e7ed12012-04-16 13:57:23 -0700662#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
663 char __pbuf[128]; \
664 \
665 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
666 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
667} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500668
Tejun Heo155fead2012-04-01 14:38:44 -0700669static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
670 struct cfq_group *curr_cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700671{
Tejun Heo155fead2012-04-01 14:38:44 -0700672 blkg_rwstat_add(&cfqg->stats.queued, rw, 1);
673 cfqg_stats_end_empty_time(&cfqg->stats);
674 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700675}
676
Tejun Heo155fead2012-04-01 14:38:44 -0700677static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
678 unsigned long time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700679{
Tejun Heo155fead2012-04-01 14:38:44 -0700680 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700681#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700682 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700683#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700684}
685
Tejun Heo155fead2012-04-01 14:38:44 -0700686static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700687{
Tejun Heo155fead2012-04-01 14:38:44 -0700688 blkg_rwstat_add(&cfqg->stats.queued, rw, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700689}
690
Tejun Heo155fead2012-04-01 14:38:44 -0700691static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700692{
Tejun Heo155fead2012-04-01 14:38:44 -0700693 blkg_rwstat_add(&cfqg->stats.merged, rw, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700694}
695
Tejun Heo155fead2012-04-01 14:38:44 -0700696static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
697 uint64_t bytes, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700698{
Tejun Heo155fead2012-04-01 14:38:44 -0700699 blkg_stat_add(&cfqg->stats.sectors, bytes >> 9);
700 blkg_rwstat_add(&cfqg->stats.serviced, rw, 1);
701 blkg_rwstat_add(&cfqg->stats.service_bytes, rw, bytes);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700702}
703
Tejun Heo155fead2012-04-01 14:38:44 -0700704static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
705 uint64_t start_time, uint64_t io_start_time, int rw)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700706{
Tejun Heo155fead2012-04-01 14:38:44 -0700707 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700708 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700709
710 if (time_after64(now, io_start_time))
711 blkg_rwstat_add(&stats->service_time, rw, now - io_start_time);
712 if (time_after64(io_start_time, start_time))
713 blkg_rwstat_add(&stats->wait_time, rw,
714 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700715}
716
Tejun Heo689665a2013-01-09 08:05:13 -0800717/* @stats = 0 */
718static void cfqg_stats_reset(struct cfqg_stats *stats)
Tejun Heo155fead2012-04-01 14:38:44 -0700719{
Tejun Heo155fead2012-04-01 14:38:44 -0700720 /* queued stats shouldn't be cleared */
721 blkg_rwstat_reset(&stats->service_bytes);
722 blkg_rwstat_reset(&stats->serviced);
723 blkg_rwstat_reset(&stats->merged);
724 blkg_rwstat_reset(&stats->service_time);
725 blkg_rwstat_reset(&stats->wait_time);
726 blkg_stat_reset(&stats->time);
727#ifdef CONFIG_DEBUG_BLK_CGROUP
728 blkg_stat_reset(&stats->unaccounted_time);
729 blkg_stat_reset(&stats->avg_queue_size_sum);
730 blkg_stat_reset(&stats->avg_queue_size_samples);
731 blkg_stat_reset(&stats->dequeue);
732 blkg_stat_reset(&stats->group_wait_time);
733 blkg_stat_reset(&stats->idle_time);
734 blkg_stat_reset(&stats->empty_time);
735#endif
736}
737
Tejun Heo0b399202013-01-09 08:05:13 -0800738/* @to += @from */
739static void cfqg_stats_merge(struct cfqg_stats *to, struct cfqg_stats *from)
740{
741 /* queued stats shouldn't be cleared */
742 blkg_rwstat_merge(&to->service_bytes, &from->service_bytes);
743 blkg_rwstat_merge(&to->serviced, &from->serviced);
744 blkg_rwstat_merge(&to->merged, &from->merged);
745 blkg_rwstat_merge(&to->service_time, &from->service_time);
746 blkg_rwstat_merge(&to->wait_time, &from->wait_time);
747 blkg_stat_merge(&from->time, &from->time);
748#ifdef CONFIG_DEBUG_BLK_CGROUP
749 blkg_stat_merge(&to->unaccounted_time, &from->unaccounted_time);
750 blkg_stat_merge(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
751 blkg_stat_merge(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
752 blkg_stat_merge(&to->dequeue, &from->dequeue);
753 blkg_stat_merge(&to->group_wait_time, &from->group_wait_time);
754 blkg_stat_merge(&to->idle_time, &from->idle_time);
755 blkg_stat_merge(&to->empty_time, &from->empty_time);
756#endif
757}
758
759/*
760 * Transfer @cfqg's stats to its parent's dead_stats so that the ancestors'
761 * recursive stats can still account for the amount used by this cfqg after
762 * it's gone.
763 */
764static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
765{
766 struct cfq_group *parent = cfqg_parent(cfqg);
767
768 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
769
770 if (unlikely(!parent))
771 return;
772
773 cfqg_stats_merge(&parent->dead_stats, &cfqg->stats);
774 cfqg_stats_merge(&parent->dead_stats, &cfqg->dead_stats);
775 cfqg_stats_reset(&cfqg->stats);
776 cfqg_stats_reset(&cfqg->dead_stats);
777}
778
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100779#else /* CONFIG_CFQ_GROUP_IOSCHED */
780
Tejun Heod02f7aa2013-01-09 08:05:11 -0800781static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100782static inline void cfqg_get(struct cfq_group *cfqg) { }
783static inline void cfqg_put(struct cfq_group *cfqg) { }
784
Jens Axboe7b679132008-05-30 12:23:07 +0200785#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400786 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
787 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
788 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
789 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200790#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100791
Tejun Heo155fead2012-04-01 14:38:44 -0700792static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
793 struct cfq_group *curr_cfqg, int rw) { }
794static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
795 unsigned long time, unsigned long unaccounted_time) { }
796static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg, int rw) { }
797static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg, int rw) { }
798static inline void cfqg_stats_update_dispatch(struct cfq_group *cfqg,
799 uint64_t bytes, int rw) { }
800static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
801 uint64_t start_time, uint64_t io_start_time, int rw) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700802
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100803#endif /* CONFIG_CFQ_GROUP_IOSCHED */
804
Jens Axboe7b679132008-05-30 12:23:07 +0200805#define cfq_log(cfqd, fmt, args...) \
806 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
807
Vivek Goyal615f0252009-12-03 12:59:39 -0500808/* Traverses through cfq group service trees */
809#define for_each_cfqg_st(cfqg, i, j, st) \
810 for (i = 0; i <= IDLE_WORKLOAD; i++) \
811 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
812 : &cfqg->service_tree_idle; \
813 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
814 (i == IDLE_WORKLOAD && j == 0); \
815 j++, st = i < IDLE_WORKLOAD ? \
816 &cfqg->service_trees[i][j]: NULL) \
817
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200818static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
819 struct cfq_ttime *ttime, bool group_idle)
820{
821 unsigned long slice;
822 if (!sample_valid(ttime->ttime_samples))
823 return false;
824 if (group_idle)
825 slice = cfqd->cfq_group_idle;
826 else
827 slice = cfqd->cfq_slice_idle;
828 return ttime->ttime_mean > slice;
829}
Vivek Goyal615f0252009-12-03 12:59:39 -0500830
Vivek Goyal02b35082010-08-23 12:23:53 +0200831static inline bool iops_mode(struct cfq_data *cfqd)
832{
833 /*
834 * If we are not idling on queues and it is a NCQ drive, parallel
835 * execution of requests is on and measuring time is not possible
836 * in most of the cases until and unless we drive shallower queue
837 * depths and that becomes a performance bottleneck. In such cases
838 * switch to start providing fairness in terms of number of IOs.
839 */
840 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
841 return true;
842 else
843 return false;
844}
845
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400846static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100847{
848 if (cfq_class_idle(cfqq))
849 return IDLE_WORKLOAD;
850 if (cfq_class_rt(cfqq))
851 return RT_WORKLOAD;
852 return BE_WORKLOAD;
853}
854
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100855
856static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
857{
858 if (!cfq_cfqq_sync(cfqq))
859 return ASYNC_WORKLOAD;
860 if (!cfq_cfqq_idle_window(cfqq))
861 return SYNC_NOIDLE_WORKLOAD;
862 return SYNC_WORKLOAD;
863}
864
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400865static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500866 struct cfq_data *cfqd,
867 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100868{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400869 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500870 return cfqg->service_tree_idle.count;
871
Vivek Goyal34b98d02012-10-03 16:56:58 -0400872 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
873 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
874 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100875}
876
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500877static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
878 struct cfq_group *cfqg)
879{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400880 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
881 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500882}
883
Jens Axboe165125e2007-07-24 09:28:11 +0200884static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800885static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heoabede6d2012-03-19 15:10:57 -0700886 struct cfq_io_cq *cic, struct bio *bio,
Tejun Heo4f85cb92012-03-05 13:15:28 -0800887 gfp_t gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200888
Tejun Heoc5869802011-12-14 00:33:41 +0100889static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
890{
891 /* cic->icq is the first member, %NULL will convert to %NULL */
892 return container_of(icq, struct cfq_io_cq, icq);
893}
894
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100895static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
896 struct io_context *ioc)
897{
898 if (ioc)
899 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
900 return NULL;
901}
902
Tejun Heoc5869802011-12-14 00:33:41 +0100903static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200904{
Jens Axboea6151c32009-10-07 20:02:57 +0200905 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200906}
907
Tejun Heoc5869802011-12-14 00:33:41 +0100908static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
909 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200910{
Jens Axboea6151c32009-10-07 20:02:57 +0200911 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200912}
913
Tejun Heoc5869802011-12-14 00:33:41 +0100914static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400915{
Tejun Heoc5869802011-12-14 00:33:41 +0100916 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400917}
918
Vasily Tarasov91fac312007-04-25 12:29:51 +0200919/*
920 * We regard a request as SYNC, if it's either a read or has the SYNC bit
921 * set (in which case it could also be direct WRITE).
922 */
Jens Axboea6151c32009-10-07 20:02:57 +0200923static inline bool cfq_bio_sync(struct bio *bio)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200924{
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200925 return bio_data_dir(bio) == READ || (bio->bi_rw & REQ_SYNC);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700929 * scheduler run of queue, if there are requests pending and no one in the
930 * driver that will restart queueing
931 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200932static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700933{
Jens Axboe7b679132008-05-30 12:23:07 +0200934 if (cfqd->busy_queues) {
935 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600936 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200937 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100941 * Scale schedule slice based on io priority. Use the sync time slice only
942 * if a queue is marked sync and has sync io queued. A sync queue with async
943 * io only, should not get full sync slice length.
944 */
Jens Axboea6151c32009-10-07 20:02:57 +0200945static inline int cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200946 unsigned short prio)
947{
948 const int base_slice = cfqd->cfq_slice[sync];
949
950 WARN_ON(prio >= IOPRIO_BE_NR);
951
952 return base_slice + (base_slice/CFQ_SLICE_SCALE * (4 - prio));
953}
954
Jens Axboe44f7c162007-01-19 11:51:58 +1100955static inline int
956cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
957{
Jens Axboed9e76202007-04-20 14:27:50 +0200958 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100959}
960
Tejun Heo1d3650f2013-01-09 08:05:11 -0800961/**
962 * cfqg_scale_charge - scale disk time charge according to cfqg weight
963 * @charge: disk time being charged
964 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
965 *
966 * Scale @charge according to @vfraction, which is in range (0, 1]. The
967 * scaling is inversely proportional.
968 *
969 * scaled = charge / vfraction
970 *
971 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
972 */
973static inline u64 cfqg_scale_charge(unsigned long charge,
974 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500975{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800976 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500977
Tejun Heo1d3650f2013-01-09 08:05:11 -0800978 /* charge / vfraction */
979 c <<= CFQ_SERVICE_SHIFT;
980 do_div(c, vfraction);
981 return c;
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500982}
983
984static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
985{
986 s64 delta = (s64)(vdisktime - min_vdisktime);
987 if (delta > 0)
988 min_vdisktime = vdisktime;
989
990 return min_vdisktime;
991}
992
993static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
994{
995 s64 delta = (s64)(vdisktime - min_vdisktime);
996 if (delta < 0)
997 min_vdisktime = vdisktime;
998
999 return min_vdisktime;
1000}
1001
1002static void update_min_vdisktime(struct cfq_rb_root *st)
1003{
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001004 struct cfq_group *cfqg;
1005
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001006 if (st->left) {
1007 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +01001008 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
1009 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001010 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001011}
1012
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001013/*
1014 * get averaged number of queues of RT/BE priority.
1015 * average is updated, with a formula that gives more weight to higher numbers,
1016 * to quickly follows sudden increases and decrease slowly
1017 */
1018
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001019static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1020 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001021{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001022 unsigned min_q, max_q;
1023 unsigned mult = cfq_hist_divisor - 1;
1024 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001025 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001026
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001027 min_q = min(cfqg->busy_queues_avg[rt], busy);
1028 max_q = max(cfqg->busy_queues_avg[rt], busy);
1029 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001030 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001031 return cfqg->busy_queues_avg[rt];
1032}
1033
1034static inline unsigned
1035cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1036{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001037 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001038}
1039
Shaohua Lic553f8e2011-01-14 08:41:03 +01001040static inline unsigned
Vivek Goyalba5bd522011-01-19 08:25:02 -07001041cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001042{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001043 unsigned slice = cfq_prio_to_slice(cfqd, cfqq);
1044 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001045 /*
1046 * interested queues (we consider only the ones with the same
1047 * priority class in the cfq group)
1048 */
1049 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1050 cfq_class_rt(cfqq));
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001051 unsigned sync_slice = cfqd->cfq_slice[1];
1052 unsigned expect_latency = sync_slice * iq;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001053 unsigned group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
1054
1055 if (expect_latency > group_slice) {
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001056 unsigned base_low_slice = 2 * cfqd->cfq_slice_idle;
1057 /* scale low_slice according to IO priority
1058 * and sync vs async */
1059 unsigned low_slice =
1060 min(slice, base_low_slice * slice / sync_slice);
1061 /* the adapted slice value is scaled to fit all iqs
1062 * into the target latency */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001063 slice = max(slice * group_slice / expect_latency,
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001064 low_slice);
1065 }
1066 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001067 return slice;
1068}
1069
1070static inline void
1071cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1072{
Vivek Goyalba5bd522011-01-19 08:25:02 -07001073 unsigned slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01001074
Vivek Goyaldae739e2009-12-03 12:59:45 -05001075 cfqq->slice_start = jiffies;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001076 cfqq->slice_end = jiffies + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001077 cfqq->allocated_slice = slice;
Jens Axboe7b679132008-05-30 12:23:07 +02001078 cfq_log_cfqq(cfqd, cfqq, "set_slice=%lu", cfqq->slice_end - jiffies);
Jens Axboe44f7c162007-01-19 11:51:58 +11001079}
1080
1081/*
1082 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1083 * isn't valid until the first request from the dispatch is activated
1084 * and the slice time set.
1085 */
Jens Axboea6151c32009-10-07 20:02:57 +02001086static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001087{
1088 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001089 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001090 if (time_before(jiffies, cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01001091 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001092
Shaohua Lic1e44752010-11-08 15:01:02 +01001093 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001094}
1095
1096/*
Jens Axboe5e705372006-07-13 12:39:25 +02001097 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001099 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Jens Axboe5e705372006-07-13 12:39:25 +02001101static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001102cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001104 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001106#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1107#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1108 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Jens Axboe5e705372006-07-13 12:39:25 +02001110 if (rq1 == NULL || rq1 == rq2)
1111 return rq2;
1112 if (rq2 == NULL)
1113 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001114
Namhyung Kim229836b2011-05-24 10:23:21 +02001115 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1116 return rq_is_sync(rq1) ? rq1 : rq2;
1117
Christoph Hellwig65299a32011-08-23 14:50:29 +02001118 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1119 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001120
Tejun Heo83096eb2009-05-07 22:24:39 +09001121 s1 = blk_rq_pos(rq1);
1122 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 /*
1125 * by definition, 1KiB is 2 sectors
1126 */
1127 back_max = cfqd->cfq_back_max * 2;
1128
1129 /*
1130 * Strict one way elevator _except_ in the case where we allow
1131 * short backward seeks which are biased as twice the cost of a
1132 * similar forward seek.
1133 */
1134 if (s1 >= last)
1135 d1 = s1 - last;
1136 else if (s1 + back_max >= last)
1137 d1 = (last - s1) * cfqd->cfq_back_penalty;
1138 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001139 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (s2 >= last)
1142 d2 = s2 - last;
1143 else if (s2 + back_max >= last)
1144 d2 = (last - s2) * cfqd->cfq_back_penalty;
1145 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001146 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Andreas Mohre8a99052006-03-28 08:59:49 +02001150 /*
1151 * By doing switch() on the bit mask "wrap" we avoid having to
1152 * check two variables for all permutations: --> faster!
1153 */
1154 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001155 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001156 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001157 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001158 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001159 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001160 else {
1161 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001162 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001163 else
Jens Axboe5e705372006-07-13 12:39:25 +02001164 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001165 }
1166
1167 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001168 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001169 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001170 return rq2;
1171 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001172 default:
1173 /*
1174 * Since both rqs are wrapped,
1175 * start with the one that's further behind head
1176 * (--> only *one* back seek required),
1177 * since back seek takes more time than forward.
1178 */
1179 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001180 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 else
Jens Axboe5e705372006-07-13 12:39:25 +02001182 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184}
1185
Jens Axboe498d3aa22007-04-26 12:54:48 +02001186/*
1187 * The below is leftmost cache rbtree addon
1188 */
Jens Axboe08717142008-01-28 11:38:15 +01001189static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001190{
Vivek Goyal615f0252009-12-03 12:59:39 -05001191 /* Service tree is empty */
1192 if (!root->count)
1193 return NULL;
1194
Jens Axboecc09e292007-04-26 12:53:50 +02001195 if (!root->left)
1196 root->left = rb_first(&root->rb);
1197
Jens Axboe08717142008-01-28 11:38:15 +01001198 if (root->left)
1199 return rb_entry(root->left, struct cfq_queue, rb_node);
1200
1201 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001202}
1203
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001204static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1205{
1206 if (!root->left)
1207 root->left = rb_first(&root->rb);
1208
1209 if (root->left)
1210 return rb_entry_cfqg(root->left);
1211
1212 return NULL;
1213}
1214
Jens Axboea36e71f2009-04-15 12:15:11 +02001215static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1216{
1217 rb_erase(n, root);
1218 RB_CLEAR_NODE(n);
1219}
1220
Jens Axboecc09e292007-04-26 12:53:50 +02001221static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1222{
1223 if (root->left == n)
1224 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001225 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001226 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/*
1230 * would be nice to take fifo expire time into account as well
1231 */
Jens Axboe5e705372006-07-13 12:39:25 +02001232static struct request *
1233cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1234 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jens Axboe21183b02006-07-13 12:33:14 +02001236 struct rb_node *rbnext = rb_next(&last->rb_node);
1237 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001238 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jens Axboe21183b02006-07-13 12:33:14 +02001240 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001243 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001246 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001247 else {
1248 rbnext = rb_first(&cfqq->sort_list);
1249 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001250 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001253 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Jens Axboed9e76202007-04-20 14:27:50 +02001256static unsigned long cfq_slice_offset(struct cfq_data *cfqd,
1257 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Jens Axboed9e76202007-04-20 14:27:50 +02001259 /*
1260 * just an approximation, should be ok.
1261 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001262 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001263 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001266static inline s64
1267cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1268{
1269 return cfqg->vdisktime - st->min_vdisktime;
1270}
1271
1272static void
1273__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1274{
1275 struct rb_node **node = &st->rb.rb_node;
1276 struct rb_node *parent = NULL;
1277 struct cfq_group *__cfqg;
1278 s64 key = cfqg_key(st, cfqg);
1279 int left = 1;
1280
1281 while (*node != NULL) {
1282 parent = *node;
1283 __cfqg = rb_entry_cfqg(parent);
1284
1285 if (key < cfqg_key(st, __cfqg))
1286 node = &parent->rb_left;
1287 else {
1288 node = &parent->rb_right;
1289 left = 0;
1290 }
1291 }
1292
1293 if (left)
1294 st->left = &cfqg->rb_node;
1295
1296 rb_link_node(&cfqg->rb_node, parent, node);
1297 rb_insert_color(&cfqg->rb_node, &st->rb);
1298}
1299
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001300/*
1301 * This has to be called only on activation of cfqg
1302 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001303static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001304cfq_update_group_weight(struct cfq_group *cfqg)
1305{
Tejun Heo3381cb82012-04-01 14:38:44 -07001306 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001307 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001308 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001309 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001310}
1311
1312static void
1313cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1314{
1315 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001316
1317 if (cfqg->new_leaf_weight) {
1318 cfqg->leaf_weight = cfqg->new_leaf_weight;
1319 cfqg->new_leaf_weight = 0;
1320 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001321}
1322
1323static void
1324cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1325{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001326 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001327 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001328 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001329 bool propagate;
1330
1331 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001332 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1333
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001334 /*
1335 * Update leaf_weight. We cannot update weight at this point
1336 * because cfqg might already have been activated and is
1337 * contributing its current weight to the parent's child_weight.
1338 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001339 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001340 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001341
1342 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001343 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1344 * entitled to. vfraction is calculated by walking the tree
1345 * towards the root calculating the fraction it has at each level.
1346 * The compounded ratio is how much vfraction @cfqg owns.
1347 *
1348 * Start with the proportion tasks in this cfqg has against active
1349 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001350 */
1351 propagate = !pos->nr_active++;
1352 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001353 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001354
Tejun Heo1d3650f2013-01-09 08:05:11 -08001355 /*
1356 * Compound ->weight walking up the tree. Both activation and
1357 * vfraction calculation are done in the same loop. Propagation
1358 * stops once an already activated node is met. vfraction
1359 * calculation should always continue to the root.
1360 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001361 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001362 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001363 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001364 propagate = !parent->nr_active++;
1365 parent->children_weight += pos->weight;
1366 }
1367 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001368 pos = parent;
1369 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001370
1371 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001372}
1373
1374static void
1375cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001376{
1377 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1378 struct cfq_group *__cfqg;
1379 struct rb_node *n;
1380
1381 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001382 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001383 return;
1384
1385 /*
1386 * Currently put the group at the end. Later implement something
1387 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001388 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001389 */
1390 n = rb_last(&st->rb);
1391 if (n) {
1392 __cfqg = rb_entry_cfqg(n);
1393 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1394 } else
1395 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001396 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001397}
1398
1399static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001400cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1401{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001402 struct cfq_group *pos = cfqg;
1403 bool propagate;
1404
1405 /*
1406 * Undo activation from cfq_group_service_tree_add(). Deactivate
1407 * @cfqg and propagate deactivation upwards.
1408 */
1409 propagate = !--pos->nr_active;
1410 pos->children_weight -= pos->leaf_weight;
1411
1412 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001413 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001414
1415 /* @pos has 0 nr_active at this point */
1416 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001417 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001418
1419 if (!parent)
1420 break;
1421
1422 propagate = !--parent->nr_active;
1423 parent->children_weight -= pos->weight;
1424 pos = parent;
1425 }
1426
1427 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001428 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1429 cfq_rb_erase(&cfqg->rb_node, st);
1430}
1431
1432static void
1433cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001434{
1435 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1436
1437 BUG_ON(cfqg->nr_cfqq < 1);
1438 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001439
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001440 /* If there are other cfq queues under this group, don't delete it */
1441 if (cfqg->nr_cfqq)
1442 return;
1443
Vivek Goyal2868ef72009-12-03 12:59:48 -05001444 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001445 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001446 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001447 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001448}
1449
Justin TerAvest167400d2011-03-12 16:54:00 +01001450static inline unsigned int cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1451 unsigned int *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001452{
Vivek Goyalf75edf22009-12-03 12:59:53 -05001453 unsigned int slice_used;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001454
1455 /*
1456 * Queue got expired before even a single request completed or
1457 * got expired immediately after first request completion.
1458 */
1459 if (!cfqq->slice_start || cfqq->slice_start == jiffies) {
1460 /*
1461 * Also charge the seek time incurred to the group, otherwise
1462 * if there are mutiple queues in the group, each can dispatch
1463 * a single request on seeky media and cause lots of seek time
1464 * and group will never know it.
1465 */
1466 slice_used = max_t(unsigned, (jiffies - cfqq->dispatch_start),
1467 1);
1468 } else {
1469 slice_used = jiffies - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001470 if (slice_used > cfqq->allocated_slice) {
1471 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001472 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001473 }
1474 if (time_after(cfqq->slice_start, cfqq->dispatch_start))
1475 *unaccounted_time += cfqq->slice_start -
1476 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001477 }
1478
Vivek Goyaldae739e2009-12-03 12:59:45 -05001479 return slice_used;
1480}
1481
1482static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001483 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001484{
1485 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Justin TerAvest167400d2011-03-12 16:54:00 +01001486 unsigned int used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001487 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1488 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001489 unsigned int vfr;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001490
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001491 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001492 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001493
Vivek Goyal02b35082010-08-23 12:23:53 +02001494 if (iops_mode(cfqd))
1495 charge = cfqq->slice_dispatch;
1496 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1497 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001498
Tejun Heo1d3650f2013-01-09 08:05:11 -08001499 /*
1500 * Can't update vdisktime while on service tree and cfqg->vfraction
1501 * is valid only while on it. Cache vfr, leave the service tree,
1502 * update vdisktime and go back on. The re-addition to the tree
1503 * will also update the weights as necessary.
1504 */
1505 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001506 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001507 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001508 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001509
1510 /* This group is being expired. Save the context */
1511 if (time_after(cfqd->workload_expires, jiffies)) {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001512 cfqg->saved_wl_slice = cfqd->workload_expires
Vivek Goyaldae739e2009-12-03 12:59:45 -05001513 - jiffies;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001514 cfqg->saved_wl_type = cfqd->serving_wl_type;
1515 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001516 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001517 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001518
1519 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1520 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001521 cfq_log_cfqq(cfqq->cfqd, cfqq,
1522 "sl_used=%u disp=%u charge=%u iops=%u sect=%lu",
1523 used_sl, cfqq->slice_dispatch, charge,
1524 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001525 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1526 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001527}
1528
Tejun Heof51b8022012-03-05 13:15:05 -08001529/**
1530 * cfq_init_cfqg_base - initialize base part of a cfq_group
1531 * @cfqg: cfq_group to initialize
1532 *
1533 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1534 * is enabled or not.
1535 */
1536static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1537{
1538 struct cfq_rb_root *st;
1539 int i, j;
1540
1541 for_each_cfqg_st(cfqg, i, j, st)
1542 *st = CFQ_RB_ROOT;
1543 RB_CLEAR_NODE(&cfqg->rb_node);
1544
1545 cfqg->ttime.last_end_request = jiffies;
1546}
1547
Vivek Goyal25fb5162009-12-03 12:59:46 -05001548#ifdef CONFIG_CFQ_GROUP_IOSCHED
Peter Zijlstra90d38392013-11-12 19:42:14 -08001549static void cfqg_stats_init(struct cfqg_stats *stats)
1550{
1551 blkg_rwstat_init(&stats->service_bytes);
1552 blkg_rwstat_init(&stats->serviced);
1553 blkg_rwstat_init(&stats->merged);
1554 blkg_rwstat_init(&stats->service_time);
1555 blkg_rwstat_init(&stats->wait_time);
1556 blkg_rwstat_init(&stats->queued);
1557
1558 blkg_stat_init(&stats->sectors);
1559 blkg_stat_init(&stats->time);
1560
1561#ifdef CONFIG_DEBUG_BLK_CGROUP
1562 blkg_stat_init(&stats->unaccounted_time);
1563 blkg_stat_init(&stats->avg_queue_size_sum);
1564 blkg_stat_init(&stats->avg_queue_size_samples);
1565 blkg_stat_init(&stats->dequeue);
1566 blkg_stat_init(&stats->group_wait_time);
1567 blkg_stat_init(&stats->idle_time);
1568 blkg_stat_init(&stats->empty_time);
1569#endif
1570}
1571
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001572static void cfq_cpd_init(const struct blkcg *blkcg)
1573{
1574 struct cfq_group_data *cgd =
1575 cpd_to_cfqgd(blkcg->pd[blkcg_policy_cfq.plid]);
1576
1577 if (blkcg == &blkcg_root) {
1578 cgd->weight = 2 * CFQ_WEIGHT_DEFAULT;
1579 cgd->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
1580 } else {
1581 cgd->weight = CFQ_WEIGHT_DEFAULT;
1582 cgd->leaf_weight = CFQ_WEIGHT_DEFAULT;
1583 }
1584}
1585
Tejun Heo3c798392012-04-16 13:57:25 -07001586static void cfq_pd_init(struct blkcg_gq *blkg)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001587{
Tejun Heo03814112012-03-05 13:15:14 -08001588 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001589 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001590
Tejun Heof51b8022012-03-05 13:15:05 -08001591 cfq_init_cfqg_base(cfqg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001592 cfqg->weight = cgd->weight;
1593 cfqg->leaf_weight = cgd->leaf_weight;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001594 cfqg_stats_init(&cfqg->stats);
1595 cfqg_stats_init(&cfqg->dead_stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001596}
1597
Tejun Heo0b399202013-01-09 08:05:13 -08001598static void cfq_pd_offline(struct blkcg_gq *blkg)
1599{
1600 /*
1601 * @blkg is going offline and will be ignored by
1602 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1603 * that they don't get lost. If IOs complete after this point, the
1604 * stats for them will be lost. Oh well...
1605 */
1606 cfqg_stats_xfer_dead(blkg_to_cfqg(blkg));
1607}
1608
Tejun Heo43114012013-01-09 08:05:13 -08001609/* offset delta from cfqg->stats to cfqg->dead_stats */
1610static const int dead_stats_off_delta = offsetof(struct cfq_group, dead_stats) -
1611 offsetof(struct cfq_group, stats);
1612
1613/* to be used by recursive prfill, sums live and dead stats recursively */
1614static u64 cfqg_stat_pd_recursive_sum(struct blkg_policy_data *pd, int off)
1615{
1616 u64 sum = 0;
1617
1618 sum += blkg_stat_recursive_sum(pd, off);
1619 sum += blkg_stat_recursive_sum(pd, off + dead_stats_off_delta);
1620 return sum;
1621}
1622
1623/* to be used by recursive prfill, sums live and dead rwstats recursively */
1624static struct blkg_rwstat cfqg_rwstat_pd_recursive_sum(struct blkg_policy_data *pd,
1625 int off)
1626{
1627 struct blkg_rwstat a, b;
1628
1629 a = blkg_rwstat_recursive_sum(pd, off);
1630 b = blkg_rwstat_recursive_sum(pd, off + dead_stats_off_delta);
1631 blkg_rwstat_merge(&a, &b);
1632 return a;
1633}
1634
Tejun Heo689665a2013-01-09 08:05:13 -08001635static void cfq_pd_reset_stats(struct blkcg_gq *blkg)
1636{
1637 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1638
1639 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -08001640 cfqg_stats_reset(&cfqg->dead_stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001641}
1642
1643/*
Vivek Goyal3e59cf92011-05-19 15:38:21 -04001644 * Search for the cfq group current task belongs to. request_queue lock must
1645 * be held.
Vivek Goyal25fb5162009-12-03 12:59:46 -05001646 */
Tejun Heocd1604f2012-03-05 13:15:06 -08001647static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07001648 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001649{
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001650 struct request_queue *q = cfqd->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -08001651 struct cfq_group *cfqg = NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001652
Tejun Heo3c798392012-04-16 13:57:25 -07001653 /* avoid lookup for the common case where there's no blkcg */
1654 if (blkcg == &blkcg_root) {
Tejun Heocd1604f2012-03-05 13:15:06 -08001655 cfqg = cfqd->root_group;
1656 } else {
Tejun Heo3c798392012-04-16 13:57:25 -07001657 struct blkcg_gq *blkg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001658
Tejun Heo3c96cb32012-04-13 13:11:34 -07001659 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -08001660 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001661 cfqg = blkg_to_cfqg(blkg);
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001662 }
1663
Vivek Goyal25fb5162009-12-03 12:59:46 -05001664 return cfqg;
1665}
1666
1667static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1668{
1669 /* Currently, all async queues are mapped to root group */
1670 if (!cfq_cfqq_sync(cfqq))
Tejun Heof51b8022012-03-05 13:15:05 -08001671 cfqg = cfqq->cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001672
1673 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001674 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001675 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001676}
1677
Tejun Heof95a04a2012-04-16 13:57:26 -07001678static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1679 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001680{
Tejun Heof95a04a2012-04-16 13:57:26 -07001681 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001682
1683 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001684 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001685 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001686}
1687
Tejun Heo2da8ca82013-12-05 12:28:04 -05001688static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001689{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001690 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1691 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1692 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001693 return 0;
1694}
1695
Tejun Heoe71357e2013-01-09 08:05:10 -08001696static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1697 struct blkg_policy_data *pd, int off)
1698{
1699 struct cfq_group *cfqg = pd_to_cfqg(pd);
1700
1701 if (!cfqg->dev_leaf_weight)
1702 return 0;
1703 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1704}
1705
Tejun Heo2da8ca82013-12-05 12:28:04 -05001706static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001707{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001708 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1709 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1710 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001711 return 0;
1712}
1713
Tejun Heo2da8ca82013-12-05 12:28:04 -05001714static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001715{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001716 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001717 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1718 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001719
Jens Axboe9470e4a2015-06-19 10:19:36 -06001720 if (cgd)
1721 val = cgd->weight;
1722
1723 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001724 return 0;
1725}
1726
Tejun Heo2da8ca82013-12-05 12:28:04 -05001727static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001728{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001729 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001730 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1731 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001732
Jens Axboe9470e4a2015-06-19 10:19:36 -06001733 if (cgd)
1734 val = cgd->leaf_weight;
1735
1736 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001737 return 0;
1738}
1739
Tejun Heo451af502014-05-13 12:16:21 -04001740static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1741 char *buf, size_t nbytes, loff_t off,
1742 bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001743{
Tejun Heo451af502014-05-13 12:16:21 -04001744 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001745 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001746 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001747 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001748 int ret;
1749
Tejun Heo3c798392012-04-16 13:57:25 -07001750 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001751 if (ret)
1752 return ret;
1753
1754 ret = -EINVAL;
Tejun Heo3381cb82012-04-01 14:38:44 -07001755 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001756 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001757 if (!cfqg || !cfqgd)
1758 goto err;
1759
Tejun Heoa2b16932012-04-13 13:11:33 -07001760 if (!ctx.v || (ctx.v >= CFQ_WEIGHT_MIN && ctx.v <= CFQ_WEIGHT_MAX)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001761 if (!is_leaf_weight) {
1762 cfqg->dev_weight = ctx.v;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001763 cfqg->new_weight = ctx.v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001764 } else {
1765 cfqg->dev_leaf_weight = ctx.v;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001766 cfqg->new_leaf_weight = ctx.v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001767 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001768 ret = 0;
1769 }
1770
Jens Axboeae994ea2015-06-20 10:26:50 -06001771err:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001772 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001773 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001774}
1775
Tejun Heo451af502014-05-13 12:16:21 -04001776static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1777 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001778{
Tejun Heo451af502014-05-13 12:16:21 -04001779 return __cfqg_set_weight_device(of, buf, nbytes, off, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001780}
1781
Tejun Heo451af502014-05-13 12:16:21 -04001782static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1783 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001784{
Tejun Heo451af502014-05-13 12:16:21 -04001785 return __cfqg_set_weight_device(of, buf, nbytes, off, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001786}
1787
Tejun Heo182446d2013-08-08 20:11:24 -04001788static int __cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1789 u64 val, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001790{
Tejun Heo182446d2013-08-08 20:11:24 -04001791 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001792 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001793 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001794 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001795
Tejun Heo3381cb82012-04-01 14:38:44 -07001796 if (val < CFQ_WEIGHT_MIN || val > CFQ_WEIGHT_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001797 return -EINVAL;
1798
1799 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001800 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001801 if (!cfqgd) {
1802 ret = -EINVAL;
1803 goto out;
1804 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001805
1806 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001807 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001808 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001809 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001810
Sasha Levinb67bfe02013-02-27 17:06:00 -08001811 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001812 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001813
Tejun Heoe71357e2013-01-09 08:05:10 -08001814 if (!cfqg)
1815 continue;
1816
1817 if (!is_leaf_weight) {
1818 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001819 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001820 } else {
1821 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001822 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001823 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001824 }
1825
Jens Axboeae994ea2015-06-20 10:26:50 -06001826out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001827 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001828 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001829}
1830
Tejun Heo182446d2013-08-08 20:11:24 -04001831static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1832 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001833{
Tejun Heo182446d2013-08-08 20:11:24 -04001834 return __cfq_set_weight(css, cft, val, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001835}
1836
Tejun Heo182446d2013-08-08 20:11:24 -04001837static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1838 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001839{
Tejun Heo182446d2013-08-08 20:11:24 -04001840 return __cfq_set_weight(css, cft, val, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001841}
1842
Tejun Heo2da8ca82013-12-05 12:28:04 -05001843static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001844{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001845 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1846 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001847 return 0;
1848}
1849
Tejun Heo2da8ca82013-12-05 12:28:04 -05001850static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001851{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001852 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1853 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001854 return 0;
1855}
1856
Tejun Heo43114012013-01-09 08:05:13 -08001857static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1858 struct blkg_policy_data *pd, int off)
1859{
1860 u64 sum = cfqg_stat_pd_recursive_sum(pd, off);
1861
1862 return __blkg_prfill_u64(sf, pd, sum);
1863}
1864
1865static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1866 struct blkg_policy_data *pd, int off)
1867{
1868 struct blkg_rwstat sum = cfqg_rwstat_pd_recursive_sum(pd, off);
1869
1870 return __blkg_prfill_rwstat(sf, pd, &sum);
1871}
1872
Tejun Heo2da8ca82013-12-05 12:28:04 -05001873static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001874{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001875 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1876 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1877 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001878 return 0;
1879}
1880
Tejun Heo2da8ca82013-12-05 12:28:04 -05001881static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001882{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001883 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1884 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1885 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001886 return 0;
1887}
1888
Tejun Heo60c2bc22012-04-01 14:38:43 -07001889#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001890static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1891 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001892{
Tejun Heof95a04a2012-04-16 13:57:26 -07001893 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001894 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001895 u64 v = 0;
1896
1897 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001898 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06001899 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001900 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001901 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001902 return 0;
1903}
1904
1905/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001906static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001907{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001908 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1909 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1910 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001911 return 0;
1912}
1913#endif /* CONFIG_DEBUG_BLK_CGROUP */
1914
1915static struct cftype cfq_blkcg_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001916 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001917 {
1918 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001919 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001920 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04001921 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001922 },
1923 {
1924 .name = "weight",
1925 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001926 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001927 .write_u64 = cfq_set_leaf_weight,
1928 },
1929
1930 /* no such mapping necessary for !roots */
1931 {
1932 .name = "weight_device",
1933 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001934 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04001935 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001936 },
1937 {
1938 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08001939 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001940 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07001941 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001942 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08001943
1944 {
1945 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001946 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04001947 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08001948 },
1949 {
1950 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05001951 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08001952 .write_u64 = cfq_set_leaf_weight,
1953 },
1954
Tejun Heo43114012013-01-09 08:05:13 -08001955 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001956 {
1957 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001958 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001959 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001960 },
1961 {
1962 .name = "sectors",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001963 .private = offsetof(struct cfq_group, stats.sectors),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001964 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001965 },
1966 {
1967 .name = "io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001968 .private = offsetof(struct cfq_group, stats.service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001969 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001970 },
1971 {
1972 .name = "io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001973 .private = offsetof(struct cfq_group, stats.serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001974 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001975 },
1976 {
1977 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001978 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001979 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001980 },
1981 {
1982 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001983 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001984 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001985 },
1986 {
1987 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001988 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001989 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001990 },
1991 {
1992 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001993 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001994 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001995 },
Tejun Heo43114012013-01-09 08:05:13 -08001996
1997 /* the same statictics which cover the cfqg and its descendants */
1998 {
1999 .name = "time_recursive",
2000 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002001 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002002 },
2003 {
2004 .name = "sectors_recursive",
2005 .private = offsetof(struct cfq_group, stats.sectors),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002006 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002007 },
2008 {
2009 .name = "io_service_bytes_recursive",
2010 .private = offsetof(struct cfq_group, stats.service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002011 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002012 },
2013 {
2014 .name = "io_serviced_recursive",
2015 .private = offsetof(struct cfq_group, stats.serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002016 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002017 },
2018 {
2019 .name = "io_service_time_recursive",
2020 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002021 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002022 },
2023 {
2024 .name = "io_wait_time_recursive",
2025 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002026 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002027 },
2028 {
2029 .name = "io_merged_recursive",
2030 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002031 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002032 },
2033 {
2034 .name = "io_queued_recursive",
2035 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002036 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002037 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002038#ifdef CONFIG_DEBUG_BLK_CGROUP
2039 {
2040 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002041 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002042 },
2043 {
2044 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002045 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002046 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002047 },
2048 {
2049 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002050 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002051 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002052 },
2053 {
2054 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002055 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002056 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002057 },
2058 {
2059 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002060 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002061 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002062 },
2063 {
2064 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002065 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002066 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002067 },
2068#endif /* CONFIG_DEBUG_BLK_CGROUP */
2069 { } /* terminate */
2070};
Vivek Goyal25fb5162009-12-03 12:59:46 -05002071#else /* GROUP_IOSCHED */
Tejun Heocd1604f2012-03-05 13:15:06 -08002072static struct cfq_group *cfq_lookup_create_cfqg(struct cfq_data *cfqd,
Tejun Heo3c798392012-04-16 13:57:25 -07002073 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002074{
Tejun Heof51b8022012-03-05 13:15:05 -08002075 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002076}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002077
Vivek Goyal25fb5162009-12-03 12:59:46 -05002078static inline void
2079cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2080 cfqq->cfqg = cfqg;
2081}
2082
2083#endif /* GROUP_IOSCHED */
2084
Jens Axboe498d3aa22007-04-26 12:54:48 +02002085/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002086 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002087 * requests waiting to be processed. It is sorted in the order that
2088 * we will service the queues.
2089 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002090static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002091 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002092{
Jens Axboe08717142008-01-28 11:38:15 +01002093 struct rb_node **p, *parent;
2094 struct cfq_queue *__cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02002095 unsigned long rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002096 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02002097 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002098 int new_cfqq = 1;
Vivek Goyalae30c282009-12-03 12:59:55 -05002099
Vivek Goyal34b98d02012-10-03 16:56:58 -04002100 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002101 if (cfq_class_idle(cfqq)) {
2102 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002103 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01002104 if (parent && parent != &cfqq->rb_node) {
2105 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2106 rb_key += __cfqq->rb_key;
2107 } else
2108 rb_key += jiffies;
2109 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002110 /*
2111 * Get our rb key offset. Subtract any residual slice
2112 * value carried from last service. A negative resid
2113 * count indicates slice overrun, and this should position
2114 * the next service time further away in the tree.
2115 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002116 rb_key = cfq_slice_offset(cfqd, cfqq) + jiffies;
Jens Axboeb9c89462009-10-06 20:53:44 +02002117 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002118 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002119 } else {
2120 rb_key = -HZ;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002121 __cfqq = cfq_rb_first(st);
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002122 rb_key += __cfqq ? __cfqq->rb_key : jiffies;
2123 }
Jens Axboed9e76202007-04-20 14:27:50 +02002124
2125 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002126 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002127 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002128 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002129 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002130 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002131 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002132
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002133 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2134 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002135 }
Jens Axboed9e76202007-04-20 14:27:50 +02002136
Jens Axboe498d3aa22007-04-26 12:54:48 +02002137 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01002138 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002139 cfqq->service_tree = st;
2140 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002141 while (*p) {
2142 parent = *p;
2143 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2144
Jens Axboe0c534e02007-04-18 20:01:57 +02002145 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002146 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002147 */
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002148 if (time_before(rb_key, __cfqq->rb_key))
Vivek Goyal1f23f122012-10-03 16:57:00 -04002149 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002150 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002151 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02002152 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002153 }
Jens Axboed9e76202007-04-20 14:27:50 +02002154 }
2155
Jens Axboecc09e292007-04-26 12:53:50 +02002156 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04002157 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02002158
Jens Axboed9e76202007-04-20 14:27:50 +02002159 cfqq->rb_key = rb_key;
2160 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002161 rb_insert_color(&cfqq->rb_node, &st->rb);
2162 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002163 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002164 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002165 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Jens Axboea36e71f2009-04-15 12:15:11 +02002168static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002169cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2170 sector_t sector, struct rb_node **ret_parent,
2171 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002172{
Jens Axboea36e71f2009-04-15 12:15:11 +02002173 struct rb_node **p, *parent;
2174 struct cfq_queue *cfqq = NULL;
2175
2176 parent = NULL;
2177 p = &root->rb_node;
2178 while (*p) {
2179 struct rb_node **n;
2180
2181 parent = *p;
2182 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2183
2184 /*
2185 * Sort strictly based on sector. Smallest to the left,
2186 * largest to the right.
2187 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002188 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002189 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002190 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002191 n = &(*p)->rb_left;
2192 else
2193 break;
2194 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002195 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002196 }
2197
2198 *ret_parent = parent;
2199 if (rb_link)
2200 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002201 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002202}
2203
2204static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2205{
Jens Axboea36e71f2009-04-15 12:15:11 +02002206 struct rb_node **p, *parent;
2207 struct cfq_queue *__cfqq;
2208
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002209 if (cfqq->p_root) {
2210 rb_erase(&cfqq->p_node, cfqq->p_root);
2211 cfqq->p_root = NULL;
2212 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002213
2214 if (cfq_class_idle(cfqq))
2215 return;
2216 if (!cfqq->next_rq)
2217 return;
2218
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002219 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002220 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2221 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002222 if (!__cfqq) {
2223 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002224 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2225 } else
2226 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002227}
2228
Jens Axboe498d3aa22007-04-26 12:54:48 +02002229/*
2230 * Update cfqq's position in the service tree.
2231 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002232static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002233{
Jens Axboe6d048f52007-04-25 12:44:27 +02002234 /*
2235 * Resorting requires the cfqq to be on the RR list already.
2236 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002237 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002238 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002239 cfq_prio_tree_add(cfqd, cfqq);
2240 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002241}
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243/*
2244 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002245 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 */
Jens Axboefebffd62008-01-28 13:19:43 +01002247static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Jens Axboe7b679132008-05-30 12:23:07 +02002249 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002250 BUG_ON(cfq_cfqq_on_rr(cfqq));
2251 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002253 if (cfq_cfqq_sync(cfqq))
2254 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Jens Axboeedd75ff2007-04-19 12:03:34 +02002256 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
Jens Axboe498d3aa22007-04-26 12:54:48 +02002259/*
2260 * Called when the cfqq no longer has requests pending, remove it from
2261 * the service tree.
2262 */
Jens Axboefebffd62008-01-28 13:19:43 +01002263static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Jens Axboe7b679132008-05-30 12:23:07 +02002265 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002266 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2267 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002269 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2270 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2271 cfqq->service_tree = NULL;
2272 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002273 if (cfqq->p_root) {
2274 rb_erase(&cfqq->p_node, cfqq->p_root);
2275 cfqq->p_root = NULL;
2276 }
Jens Axboed9e76202007-04-20 14:27:50 +02002277
Justin TerAvest8184f932011-03-17 16:12:36 +01002278 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 BUG_ON(!cfqd->busy_queues);
2280 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002281 if (cfq_cfqq_sync(cfqq))
2282 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285/*
2286 * rb tree support functions
2287 */
Jens Axboefebffd62008-01-28 13:19:43 +01002288static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Jens Axboe5e705372006-07-13 12:39:25 +02002290 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002291 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jens Axboeb4878f22005-10-20 16:42:29 +02002293 BUG_ON(!cfqq->queued[sync]);
2294 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Jens Axboe5e705372006-07-13 12:39:25 +02002296 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Vivek Goyalf04a6422009-12-03 12:59:40 -05002298 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2299 /*
2300 * Queue will be deleted from service tree when we actually
2301 * expire it later. Right now just remove it from prio tree
2302 * as it is empty.
2303 */
2304 if (cfqq->p_root) {
2305 rb_erase(&cfqq->p_node, cfqq->p_root);
2306 cfqq->p_root = NULL;
2307 }
2308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Jens Axboe5e705372006-07-13 12:39:25 +02002311static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Jens Axboe5e705372006-07-13 12:39:25 +02002313 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002315 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Jens Axboe5380a102006-07-13 12:37:56 +02002317 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Jeff Moyer796d5112011-06-02 21:19:05 +02002319 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002320
2321 if (!cfq_cfqq_on_rr(cfqq))
2322 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002323
2324 /*
2325 * check if this request is a better next-serve candidate
2326 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002327 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002328 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002329
2330 /*
2331 * adjust priority tree position, if ->next_rq changes
2332 */
2333 if (prev != cfqq->next_rq)
2334 cfq_prio_tree_add(cfqd, cfqq);
2335
Jens Axboe5044eed2007-04-25 11:53:48 +02002336 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Jens Axboefebffd62008-01-28 13:19:43 +01002339static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Jens Axboe5380a102006-07-13 12:37:56 +02002341 elv_rb_del(&cfqq->sort_list, rq);
2342 cfqq->queued[rq_is_sync(rq)]--;
Tejun Heo155fead2012-04-01 14:38:44 -07002343 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002344 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002345 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
2346 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Jens Axboe206dc692006-03-28 13:03:44 +02002349static struct request *
2350cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Jens Axboe206dc692006-03-28 13:03:44 +02002352 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002353 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002354 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Jens Axboe4ac845a2008-01-24 08:44:49 +01002356 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002357 if (!cic)
2358 return NULL;
2359
2360 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002361 if (cfqq)
2362 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return NULL;
2365}
2366
Jens Axboe165125e2007-07-24 09:28:11 +02002367static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002368{
2369 struct cfq_data *cfqd = q->elevator->elevator_data;
2370
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002371 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002372 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002373 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002374
Tejun Heo5b936292009-05-07 22:24:38 +09002375 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002376}
2377
Jens Axboe165125e2007-07-24 09:28:11 +02002378static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Jens Axboe22e2c502005-06-27 10:55:12 +02002380 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002382 WARN_ON(!cfqd->rq_in_driver);
2383 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002384 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002385 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Jens Axboeb4878f22005-10-20 16:42:29 +02002388static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Jens Axboe5e705372006-07-13 12:39:25 +02002390 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002391
Jens Axboe5e705372006-07-13 12:39:25 +02002392 if (cfqq->next_rq == rq)
2393 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Jens Axboeb4878f22005-10-20 16:42:29 +02002395 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002396 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002397
Aaron Carroll45333d52008-08-26 15:52:36 +02002398 cfqq->cfqd->rq_queued--;
Tejun Heo155fead2012-04-01 14:38:44 -07002399 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002400 if (rq->cmd_flags & REQ_PRIO) {
2401 WARN_ON(!cfqq->prio_pending);
2402 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Jens Axboe165125e2007-07-24 09:28:11 +02002406static int cfq_merge(struct request_queue *q, struct request **req,
2407 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 struct cfq_data *cfqd = q->elevator->elevator_data;
2410 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Jens Axboe206dc692006-03-28 13:03:44 +02002412 __rq = cfq_find_rq_fmerge(cfqd, bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002413 if (__rq && elv_rq_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002414 *req = __rq;
2415 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417
2418 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Jens Axboe165125e2007-07-24 09:28:11 +02002421static void cfq_merged_request(struct request_queue *q, struct request *req,
Jens Axboe21183b02006-07-13 12:33:14 +02002422 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Jens Axboe21183b02006-07-13 12:33:14 +02002424 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002425 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Jens Axboe5e705372006-07-13 12:39:25 +02002427 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Divyesh Shah812d4022010-04-08 21:14:23 -07002431static void cfq_bio_merged(struct request_queue *q, struct request *req,
2432 struct bio *bio)
2433{
Tejun Heo155fead2012-04-01 14:38:44 -07002434 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_rw);
Divyesh Shah812d4022010-04-08 21:14:23 -07002435}
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437static void
Jens Axboe165125e2007-07-24 09:28:11 +02002438cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct request *next)
2440{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002441 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002442 struct cfq_data *cfqd = q->elevator->elevator_data;
2443
Jens Axboe22e2c502005-06-27 10:55:12 +02002444 /*
2445 * reposition in fifo if next is older than rq
2446 */
2447 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jan Kara8b4922d2014-02-24 16:39:52 +01002448 time_before(next->fifo_time, rq->fifo_time) &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002449 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002450 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002451 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002452 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002453
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002454 if (cfqq->next_rq == next)
2455 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002456 cfq_remove_request(next);
Tejun Heo155fead2012-04-01 14:38:44 -07002457 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002458
2459 cfqq = RQ_CFQQ(next);
2460 /*
2461 * all requests of this queue are merged to other queues, delete it
2462 * from the service tree. If it's the active_queue,
2463 * cfq_dispatch_requests() will choose to expire it or do idle
2464 */
2465 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2466 cfqq != cfqd->active_queue)
2467 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002468}
2469
Jens Axboe165125e2007-07-24 09:28:11 +02002470static int cfq_allow_merge(struct request_queue *q, struct request *rq,
Jens Axboeda775262006-12-20 11:04:12 +01002471 struct bio *bio)
2472{
2473 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heoc5869802011-12-14 00:33:41 +01002474 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002475 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002476
2477 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002478 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002479 */
Vasily Tarasov91fac312007-04-25 12:29:51 +02002480 if (cfq_bio_sync(bio) && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002481 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002482
2483 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002484 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002485 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002486 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002487 cic = cfq_cic_lookup(cfqd, current->io_context);
2488 if (!cic)
2489 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002490
Vasily Tarasov91fac312007-04-25 12:29:51 +02002491 cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
Jens Axboea6151c32009-10-07 20:02:57 +02002492 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002493}
2494
Divyesh Shah812df482010-04-08 21:15:35 -07002495static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2496{
2497 del_timer(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002498 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002499}
2500
Jens Axboefebffd62008-01-28 13:19:43 +01002501static void __cfq_set_active_queue(struct cfq_data *cfqd,
2502 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002503{
2504 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002505 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002506 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002507 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002508 cfqq->slice_start = 0;
2509 cfqq->dispatch_start = jiffies;
2510 cfqq->allocated_slice = 0;
2511 cfqq->slice_end = 0;
2512 cfqq->slice_dispatch = 0;
2513 cfqq->nr_sectors = 0;
2514
2515 cfq_clear_cfqq_wait_request(cfqq);
2516 cfq_clear_cfqq_must_dispatch(cfqq);
2517 cfq_clear_cfqq_must_alloc_slice(cfqq);
2518 cfq_clear_cfqq_fifo_expire(cfqq);
2519 cfq_mark_cfqq_slice_new(cfqq);
2520
2521 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002522 }
2523
2524 cfqd->active_queue = cfqq;
2525}
2526
2527/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002528 * current cfqq expired its slice (or was too idle), select new one
2529 */
2530static void
2531__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002532 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002533{
Jens Axboe7b679132008-05-30 12:23:07 +02002534 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2535
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002536 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002537 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002538
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002539 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002540 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002541
2542 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002543 * If this cfqq is shared between multiple processes, check to
2544 * make sure that those processes are still issuing I/Os within
2545 * the mean seek distance. If not, it may be time to break the
2546 * queues apart again.
2547 */
2548 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2549 cfq_mark_cfqq_split_coop(cfqq);
2550
2551 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002552 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002553 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002554 if (timed_out) {
2555 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002556 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002557 else
2558 cfqq->slice_resid = cfqq->slice_end - jiffies;
Jens Axboe7b679132008-05-30 12:23:07 +02002559 cfq_log_cfqq(cfqd, cfqq, "resid=%ld", cfqq->slice_resid);
2560 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002561
Vivek Goyale5ff0822010-04-26 19:25:11 +02002562 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002563
Vivek Goyalf04a6422009-12-03 12:59:40 -05002564 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2565 cfq_del_cfqq_rr(cfqd, cfqq);
2566
Jens Axboeedd75ff2007-04-19 12:03:34 +02002567 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002568
2569 if (cfqq == cfqd->active_queue)
2570 cfqd->active_queue = NULL;
2571
2572 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002573 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002574 cfqd->active_cic = NULL;
2575 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002576}
2577
Vivek Goyale5ff0822010-04-26 19:25:11 +02002578static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002579{
2580 struct cfq_queue *cfqq = cfqd->active_queue;
2581
2582 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002583 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002584}
2585
Jens Axboe498d3aa22007-04-26 12:54:48 +02002586/*
2587 * Get next queue for service. Unless we have a queue preemption,
2588 * we'll simply select the first cfqq in the service tree.
2589 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002590static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002591{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002592 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2593 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002594
Vivek Goyalf04a6422009-12-03 12:59:40 -05002595 if (!cfqd->rq_queued)
2596 return NULL;
2597
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002598 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002599 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002600 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002601 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002602 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002603 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002604}
2605
Vivek Goyalf04a6422009-12-03 12:59:40 -05002606static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2607{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002608 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002609 struct cfq_queue *cfqq;
2610 int i, j;
2611 struct cfq_rb_root *st;
2612
2613 if (!cfqd->rq_queued)
2614 return NULL;
2615
Vivek Goyal25fb5162009-12-03 12:59:46 -05002616 cfqg = cfq_get_next_cfqg(cfqd);
2617 if (!cfqg)
2618 return NULL;
2619
Vivek Goyalf04a6422009-12-03 12:59:40 -05002620 for_each_cfqg_st(cfqg, i, j, st)
2621 if ((cfqq = cfq_rb_first(st)) != NULL)
2622 return cfqq;
2623 return NULL;
2624}
2625
Jens Axboe498d3aa22007-04-26 12:54:48 +02002626/*
2627 * Get and set a new active queue for service.
2628 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002629static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2630 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002631{
Jens Axboee00ef792009-11-04 08:54:55 +01002632 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002633 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002634
Jens Axboe22e2c502005-06-27 10:55:12 +02002635 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002636 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002637}
2638
Jens Axboed9e76202007-04-20 14:27:50 +02002639static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2640 struct request *rq)
2641{
Tejun Heo83096eb2009-05-07 22:24:39 +09002642 if (blk_rq_pos(rq) >= cfqd->last_position)
2643 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002644 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002645 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002646}
2647
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002648static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002649 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002650{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002651 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002652}
2653
Jens Axboea36e71f2009-04-15 12:15:11 +02002654static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2655 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002656{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002657 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002658 struct rb_node *parent, *node;
2659 struct cfq_queue *__cfqq;
2660 sector_t sector = cfqd->last_position;
2661
2662 if (RB_EMPTY_ROOT(root))
2663 return NULL;
2664
2665 /*
2666 * First, if we find a request starting at the end of the last
2667 * request, choose it.
2668 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002669 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002670 if (__cfqq)
2671 return __cfqq;
2672
2673 /*
2674 * If the exact sector wasn't found, the parent of the NULL leaf
2675 * will contain the closest sector.
2676 */
2677 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002678 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002679 return __cfqq;
2680
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002681 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002682 node = rb_next(&__cfqq->p_node);
2683 else
2684 node = rb_prev(&__cfqq->p_node);
2685 if (!node)
2686 return NULL;
2687
2688 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002689 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002690 return __cfqq;
2691
2692 return NULL;
2693}
2694
2695/*
2696 * cfqd - obvious
2697 * cur_cfqq - passed in so that we don't decide that the current queue is
2698 * closely cooperating with itself.
2699 *
2700 * So, basically we're assuming that that cur_cfqq has dispatched at least
2701 * one request, and that cfqd->last_position reflects a position on the disk
2702 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2703 * assumption.
2704 */
2705static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002706 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002707{
2708 struct cfq_queue *cfqq;
2709
Divyesh Shah39c01b22010-03-25 15:45:57 +01002710 if (cfq_class_idle(cur_cfqq))
2711 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002712 if (!cfq_cfqq_sync(cur_cfqq))
2713 return NULL;
2714 if (CFQQ_SEEKY(cur_cfqq))
2715 return NULL;
2716
Jens Axboea36e71f2009-04-15 12:15:11 +02002717 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002718 * Don't search priority tree if it's the only queue in the group.
2719 */
2720 if (cur_cfqq->cfqg->nr_cfqq == 1)
2721 return NULL;
2722
2723 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002724 * We should notice if some of the queues are cooperating, eg
2725 * working closely on the same area of the disk. In that case,
2726 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002727 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002728 cfqq = cfqq_close(cfqd, cur_cfqq);
2729 if (!cfqq)
2730 return NULL;
2731
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002732 /* If new queue belongs to different cfq_group, don't choose it */
2733 if (cur_cfqq->cfqg != cfqq->cfqg)
2734 return NULL;
2735
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002736 /*
2737 * It only makes sense to merge sync queues.
2738 */
2739 if (!cfq_cfqq_sync(cfqq))
2740 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002741 if (CFQQ_SEEKY(cfqq))
2742 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002743
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002744 /*
2745 * Do not merge queues of different priority classes
2746 */
2747 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2748 return NULL;
2749
Jens Axboea36e71f2009-04-15 12:15:11 +02002750 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002751}
2752
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002753/*
2754 * Determine whether we should enforce idle window for this queue.
2755 */
2756
2757static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2758{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002759 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002760 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002761
Vivek Goyal34b98d02012-10-03 16:56:58 -04002762 BUG_ON(!st);
2763 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002764
Vivek Goyalb6508c12010-08-23 12:23:33 +02002765 if (!cfqd->cfq_slice_idle)
2766 return false;
2767
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002768 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002769 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002770 return false;
2771
2772 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002773 if (cfq_cfqq_idle_window(cfqq) &&
2774 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002775 return true;
2776
2777 /*
2778 * Otherwise, we do only if they are the last ones
2779 * in their service tree.
2780 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002781 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2782 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002783 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002784 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002785 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002786}
2787
Jens Axboe6d048f52007-04-25 12:44:27 +02002788static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002789{
Jens Axboe17926692007-01-19 11:59:30 +11002790 struct cfq_queue *cfqq = cfqd->active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +01002791 struct cfq_io_cq *cic;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002792 unsigned long sl, group_idle = 0;
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002793
Jens Axboea68bbdd2008-09-24 13:03:33 +02002794 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002795 * SSD device without seek penalty, disable idling. But only do so
2796 * for devices that support queuing, otherwise we still have a problem
2797 * with sync vs async workloads.
Jens Axboea68bbdd2008-09-24 13:03:33 +02002798 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002799 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbdd2008-09-24 13:03:33 +02002800 return;
2801
Jens Axboedd67d052006-06-21 09:36:18 +02002802 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002803 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002804
2805 /*
2806 * idle is disabled, either manually or by past process history
2807 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002808 if (!cfq_should_idle(cfqd, cfqq)) {
2809 /* no queue idling. Check for group idling */
2810 if (cfqd->cfq_group_idle)
2811 group_idle = cfqd->cfq_group_idle;
2812 else
2813 return;
2814 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002815
Jens Axboe22e2c502005-06-27 10:55:12 +02002816 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002817 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002818 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002819 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002820 return;
2821
2822 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002823 * task has exited, don't wait
2824 */
Jens Axboe206dc692006-03-28 13:03:44 +02002825 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002826 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002827 return;
2828
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002829 /*
2830 * If our average think time is larger than the remaining time
2831 * slice, then don't idle. This avoids overrunning the allotted
2832 * time slice.
2833 */
Shaohua Li383cd722011-07-12 14:24:35 +02002834 if (sample_valid(cic->ttime.ttime_samples) &&
2835 (cfqq->slice_end - jiffies < cic->ttime.ttime_mean)) {
Joe Perchesfd16d262011-06-13 10:42:49 +02002836 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%lu",
Shaohua Li383cd722011-07-12 14:24:35 +02002837 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002838 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002839 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002840
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002841 /* There are other queues in the group, don't do group idle */
2842 if (group_idle && cfqq->cfqg->nr_cfqq > 1)
2843 return;
2844
Jens Axboe3b181522005-06-27 10:56:24 +02002845 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002846
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002847 if (group_idle)
2848 sl = cfqd->cfq_group_idle;
2849 else
2850 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002851
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002852 mod_timer(&cfqd->idle_slice_timer, jiffies + sl);
Tejun Heo155fead2012-04-01 14:38:44 -07002853 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002854 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %lu group_idle: %d", sl,
2855 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
Jens Axboe498d3aa22007-04-26 12:54:48 +02002858/*
2859 * Move request from internal lists to the request queue dispatch list.
2860 */
Jens Axboe165125e2007-07-24 09:28:11 +02002861static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002863 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002864 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002865
Jens Axboe7b679132008-05-30 12:23:07 +02002866 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2867
Jeff Moyer06d21882009-09-11 17:08:59 +02002868 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002869 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002870 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002871 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002872 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002873
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002874 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002875 cfqq->nr_sectors += blk_rq_sectors(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002876 cfqg_stats_update_dispatch(cfqq->cfqg, blk_rq_bytes(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
2879/*
2880 * return expired entry, or NULL to just start from scratch in rbtree
2881 */
Jens Axboefebffd62008-01-28 13:19:43 +01002882static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Jens Axboe30996f42009-10-05 11:03:39 +02002884 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Jens Axboe3b181522005-06-27 10:56:24 +02002886 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11002888
2889 cfq_mark_cfqq_fifo_expire(cfqq);
2890
Jens Axboe89850f72006-07-22 16:48:31 +02002891 if (list_empty(&cfqq->fifo))
2892 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Jens Axboe89850f72006-07-22 16:48:31 +02002894 rq = rq_entry_fifo(cfqq->fifo.next);
Jan Kara8b4922d2014-02-24 16:39:52 +01002895 if (time_before(jiffies, rq->fifo_time))
Jens Axboe7b679132008-05-30 12:23:07 +02002896 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Jens Axboe30996f42009-10-05 11:03:39 +02002898 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002899 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
Jens Axboe22e2c502005-06-27 10:55:12 +02002902static inline int
2903cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2904{
2905 const int base_rq = cfqd->cfq_slice_async_rq;
2906
2907 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
2908
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02002909 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002910}
2911
2912/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002913 * Must be called with the queue_lock held.
2914 */
2915static int cfqq_process_refs(struct cfq_queue *cfqq)
2916{
2917 int process_refs, io_refs;
2918
2919 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01002920 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002921 BUG_ON(process_refs < 0);
2922 return process_refs;
2923}
2924
2925static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
2926{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002927 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002928 struct cfq_queue *__cfqq;
2929
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002930 /*
2931 * If there are no process references on the new_cfqq, then it is
2932 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
2933 * chain may have dropped their last reference (not just their
2934 * last process reference).
2935 */
2936 if (!cfqq_process_refs(new_cfqq))
2937 return;
2938
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002939 /* Avoid a circular list and skip interim queue merges */
2940 while ((__cfqq = new_cfqq->new_cfqq)) {
2941 if (__cfqq == cfqq)
2942 return;
2943 new_cfqq = __cfqq;
2944 }
2945
2946 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002947 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002948 /*
2949 * If the process for the cfqq has gone away, there is no
2950 * sense in merging the queues.
2951 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04002952 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002953 return;
2954
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002955 /*
2956 * Merge in the direction of the lesser amount of work.
2957 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002958 if (new_process_refs >= process_refs) {
2959 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002960 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002961 } else {
2962 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01002963 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002964 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002965}
2966
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002967static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002968 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002969{
2970 struct cfq_queue *queue;
2971 int i;
2972 bool key_valid = false;
2973 unsigned long lowest_key = 0;
2974 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
2975
Vivek Goyal65b32a52009-12-16 17:52:59 -05002976 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
2977 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002978 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002979 if (queue &&
2980 (!key_valid || time_before(queue->rb_key, lowest_key))) {
2981 lowest_key = queue->rb_key;
2982 cur_best = i;
2983 key_valid = true;
2984 }
2985 }
2986
2987 return cur_best;
2988}
2989
Vivek Goyal6d816ec2012-10-03 16:56:59 -04002990static void
2991choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002992{
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002993 unsigned slice;
2994 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05002995 struct cfq_rb_root *st;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05002996 unsigned group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002997 enum wl_class_t original_class = cfqd->serving_wl_class;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002998
Corrado Zoccolo718eee02009-10-26 22:45:29 +01002999 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003000 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003001 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003002 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003003 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003004 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003005 cfqd->serving_wl_class = IDLE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003006 cfqd->workload_expires = jiffies + 1;
3007 return;
3008 }
3009
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003010 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003011 goto new_workload;
3012
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003013 /*
3014 * For RT and BE, we have to choose also the type
3015 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3016 * expiration time
3017 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003018 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003019 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003020
3021 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003022 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003023 */
Vivek Goyal65b32a52009-12-16 17:52:59 -05003024 if (count && !time_after(jiffies, cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003025 return;
3026
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003027new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003028 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003029 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003030 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003031 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003032 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003033
3034 /*
3035 * the workload slice is computed as a fraction of target latency
3036 * proportional to the number of queues in that workload, over
3037 * all the queues in the same priority class
3038 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003039 group_slice = cfq_group_slice(cfqd, cfqg);
3040
3041 slice = group_slice * count /
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003042 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3043 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003044 cfqg));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003045
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003046 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003047 unsigned int tmp;
3048
3049 /*
3050 * Async queues are currently system wide. Just taking
3051 * proportion of queues with-in same group will lead to higher
3052 * async ratio system wide as generally root group is going
3053 * to have higher weight. A more accurate thing would be to
3054 * calculate system wide asnc/sync ratio.
3055 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003056 tmp = cfqd->cfq_target_latency *
3057 cfqg_busy_async_queues(cfqd, cfqg);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003058 tmp = tmp/cfqd->busy_queues;
3059 slice = min_t(unsigned, slice, tmp);
3060
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003061 /* async workload slice is scaled down according to
3062 * the sync/async slice ratio. */
3063 slice = slice * cfqd->cfq_slice[0] / cfqd->cfq_slice[1];
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003064 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003065 /* sync workload slice is at least 2 * cfq_slice_idle */
3066 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3067
3068 slice = max_t(unsigned, slice, CFQ_MIN_TT);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003069 cfq_log(cfqd, "workload slice:%d", slice);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003070 cfqd->workload_expires = jiffies + slice;
3071}
3072
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003073static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3074{
3075 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003076 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003077
3078 if (RB_EMPTY_ROOT(&st->rb))
3079 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003080 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003081 update_min_vdisktime(st);
3082 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003083}
3084
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003085static void cfq_choose_cfqg(struct cfq_data *cfqd)
3086{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003087 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
3088
3089 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003090
3091 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003092 if (cfqg->saved_wl_slice) {
3093 cfqd->workload_expires = jiffies + cfqg->saved_wl_slice;
3094 cfqd->serving_wl_type = cfqg->saved_wl_type;
3095 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003096 } else
3097 cfqd->workload_expires = jiffies - 1;
3098
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003099 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003100}
3101
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003102/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003103 * Select a queue for service. If we have a current active queue,
3104 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003105 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003106static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003107{
Jens Axboea36e71f2009-04-15 12:15:11 +02003108 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003109
3110 cfqq = cfqd->active_queue;
3111 if (!cfqq)
3112 goto new_queue;
3113
Vivek Goyalf04a6422009-12-03 12:59:40 -05003114 if (!cfqd->rq_queued)
3115 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003116
3117 /*
3118 * We were waiting for group to get backlogged. Expire the queue
3119 */
3120 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3121 goto expire;
3122
Jens Axboe22e2c502005-06-27 10:55:12 +02003123 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003124 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003125 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003126 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3127 /*
3128 * If slice had not expired at the completion of last request
3129 * we might not have turned on wait_busy flag. Don't expire
3130 * the queue yet. Allow the group to get backlogged.
3131 *
3132 * The very fact that we have used the slice, that means we
3133 * have been idling all along on this queue and it should be
3134 * ok to wait for this request to complete.
3135 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003136 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3137 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3138 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003139 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003140 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003141 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003142 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003143
3144 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003145 * The active queue has requests and isn't expired, allow it to
3146 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003147 */
Jens Axboedd67d052006-06-21 09:36:18 +02003148 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003149 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003150
3151 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003152 * If another queue has a request waiting within our mean seek
3153 * distance, let it run. The expire code will check for close
3154 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003155 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003156 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003157 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003158 if (new_cfqq) {
3159 if (!cfqq->new_cfqq)
3160 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003161 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003162 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003163
3164 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003165 * No requests pending. If the active queue still has requests in
3166 * flight or is idling for a new request, allow either of these
3167 * conditions to happen (or time out) before selecting a new queue.
3168 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003169 if (timer_pending(&cfqd->idle_slice_timer)) {
3170 cfqq = NULL;
3171 goto keep_queue;
3172 }
3173
Shaohua Li8e1ac662010-11-08 15:01:04 +01003174 /*
3175 * This is a deep seek queue, but the device is much faster than
3176 * the queue can deliver, don't idle
3177 **/
3178 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3179 (cfq_cfqq_slice_new(cfqq) ||
3180 (cfqq->slice_end - jiffies > jiffies - cfqq->slice_start))) {
3181 cfq_clear_cfqq_deep(cfqq);
3182 cfq_clear_cfqq_idle_window(cfqq);
3183 }
3184
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003185 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3186 cfqq = NULL;
3187 goto keep_queue;
3188 }
3189
3190 /*
3191 * If group idle is enabled and there are requests dispatched from
3192 * this group, wait for requests to complete.
3193 */
3194check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02003195 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3196 cfqq->cfqg->dispatched &&
3197 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003198 cfqq = NULL;
3199 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003200 }
3201
Jens Axboe3b181522005-06-27 10:56:24 +02003202expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003203 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003204new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003205 /*
3206 * Current queue expired. Check if we have to switch to a new
3207 * service tree
3208 */
3209 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003210 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003211
Jens Axboea36e71f2009-04-15 12:15:11 +02003212 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003213keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003214 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003215}
3216
Jens Axboefebffd62008-01-28 13:19:43 +01003217static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003218{
3219 int dispatched = 0;
3220
3221 while (cfqq->next_rq) {
3222 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3223 dispatched++;
3224 }
3225
3226 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003227
3228 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003229 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003230 return dispatched;
3231}
3232
Jens Axboe498d3aa22007-04-26 12:54:48 +02003233/*
3234 * Drain our current requests. Used for barriers and when switching
3235 * io schedulers on-the-fly.
3236 */
Jens Axboed9e76202007-04-20 14:27:50 +02003237static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003238{
Jens Axboe08717142008-01-28 11:38:15 +01003239 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003240 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003241
Divyesh Shah3440c492010-04-09 09:29:57 +02003242 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003243 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003244 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3245 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003246 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003247 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003248
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003249 BUG_ON(cfqd->busy_queues);
3250
Jeff Moyer69237152009-06-12 15:29:30 +02003251 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003252 return dispatched;
3253}
3254
Shaohua Liabc3c742010-03-01 09:20:54 +01003255static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3256 struct cfq_queue *cfqq)
3257{
3258 /* the queue hasn't finished any request, can't estimate */
3259 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003260 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003261 if (time_after(jiffies + cfqd->cfq_slice_idle * cfqq->dispatched,
3262 cfqq->slice_end))
Shaohua Lic1e44752010-11-08 15:01:02 +01003263 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003264
Shaohua Lic1e44752010-11-08 15:01:02 +01003265 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003266}
3267
Jens Axboe0b182d62009-10-06 20:49:37 +02003268static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003269{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003270 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Jens Axboe2f5cb732009-04-07 08:51:19 +02003272 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003273 * Drain async requests before we start sync IO
3274 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003275 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003276 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003277
3278 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003279 * If this is an async queue and we have sync IO in flight, let it wait
3280 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003281 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003282 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003283
Shaohua Liabc3c742010-03-01 09:20:54 +01003284 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003285 if (cfq_class_idle(cfqq))
3286 max_dispatch = 1;
3287
3288 /*
3289 * Does this cfqq already have too much IO in flight?
3290 */
3291 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003292 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003293 /*
3294 * idle queue must always only have a single IO in flight
3295 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003296 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003297 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003298
Jens Axboe2f5cb732009-04-07 08:51:19 +02003299 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003300 * If there is only one sync queue
3301 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003302 * queue no dispatch limit. The reason is a sync queue can
3303 * preempt async queue, limiting the sync queue doesn't make
3304 * sense. This is useful for aiostress test.
3305 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003306 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3307 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003308
3309 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003310 * We have other queues, don't allow more IO from this one
3311 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003312 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3313 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003314 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003315
Jens Axboe2f5cb732009-04-07 08:51:19 +02003316 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003317 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003318 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003319 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003320 max_dispatch = -1;
3321 else
3322 /*
3323 * Normally we start throttling cfqq when cfq_quantum/2
3324 * requests have been dispatched. But we can drive
3325 * deeper queue depths at the beginning of slice
3326 * subjected to upper limit of cfq_quantum.
3327 * */
3328 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003329 }
3330
3331 /*
3332 * Async queues must wait a bit before being allowed dispatch.
3333 * We also ramp up the dispatch depth gradually for async IO,
3334 * based on the last sync IO we serviced
3335 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003336 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Corrado Zoccolo573412b2009-12-06 11:48:52 +01003337 unsigned long last_sync = jiffies - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003338 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003339
Jens Axboe61f0c1d2009-10-03 19:46:03 +02003340 depth = last_sync / cfqd->cfq_slice[1];
Jens Axboee00c54c2009-10-04 20:36:19 +02003341 if (!depth && !cfqq->dispatched)
3342 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003343 if (depth < max_dispatch)
3344 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346
Jens Axboe0b182d62009-10-06 20:49:37 +02003347 /*
3348 * If we're below the current max, allow a dispatch
3349 */
3350 return cfqq->dispatched < max_dispatch;
3351}
3352
3353/*
3354 * Dispatch a request from cfqq, moving them to the request queue
3355 * dispatch list.
3356 */
3357static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3358{
3359 struct request *rq;
3360
3361 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3362
3363 if (!cfq_may_dispatch(cfqd, cfqq))
3364 return false;
3365
3366 /*
3367 * follow expired path, else get first next available
3368 */
3369 rq = cfq_check_fifo(cfqq);
3370 if (!rq)
3371 rq = cfqq->next_rq;
3372
3373 /*
3374 * insert request into driver dispatch list
3375 */
3376 cfq_dispatch_insert(cfqd->queue, rq);
3377
3378 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003379 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003380
Tejun Heoc5869802011-12-14 00:33:41 +01003381 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003382 cfqd->active_cic = cic;
3383 }
3384
3385 return true;
3386}
3387
3388/*
3389 * Find the cfqq that we need to service and move a request from that to the
3390 * dispatch list
3391 */
3392static int cfq_dispatch_requests(struct request_queue *q, int force)
3393{
3394 struct cfq_data *cfqd = q->elevator->elevator_data;
3395 struct cfq_queue *cfqq;
3396
3397 if (!cfqd->busy_queues)
3398 return 0;
3399
3400 if (unlikely(force))
3401 return cfq_forced_dispatch(cfqd);
3402
3403 cfqq = cfq_select_queue(cfqd);
3404 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003405 return 0;
3406
Jens Axboe2f5cb732009-04-07 08:51:19 +02003407 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003408 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003409 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003410 if (!cfq_dispatch_request(cfqd, cfqq))
3411 return 0;
3412
Jens Axboe2f5cb732009-04-07 08:51:19 +02003413 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003414 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003415
3416 /*
3417 * expire an async queue immediately if it has used up its slice. idle
3418 * queue always expire after 1 dispatch round.
3419 */
3420 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3421 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3422 cfq_class_idle(cfqq))) {
3423 cfqq->slice_end = jiffies + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003424 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003425 }
3426
Shan Weib217a902009-09-01 10:06:42 +02003427 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003428 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429}
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431/*
Jens Axboe5e705372006-07-13 12:39:25 +02003432 * task holds one reference to the queue, dropped when task exits. each rq
3433 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003435 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 * queue lock must be held here.
3437 */
3438static void cfq_put_queue(struct cfq_queue *cfqq)
3439{
Jens Axboe22e2c502005-06-27 10:55:12 +02003440 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003441 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003442
Shaohua Li30d7b942011-01-07 08:46:59 +01003443 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Shaohua Li30d7b942011-01-07 08:46:59 +01003445 cfqq->ref--;
3446 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 return;
3448
Jens Axboe7b679132008-05-30 12:23:07 +02003449 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003451 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003452 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003454 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003455 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003456 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003457 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003458
Vivek Goyalf04a6422009-12-03 12:59:40 -05003459 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003461 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Shaohua Lid02a2c02010-05-25 10:16:53 +02003464static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003465{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003466 struct cfq_queue *__cfqq, *next;
3467
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003468 /*
3469 * If this queue was scheduled to merge with another queue, be
3470 * sure to drop the reference taken on that queue (and others in
3471 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3472 */
3473 __cfqq = cfqq->new_cfqq;
3474 while (__cfqq) {
3475 if (__cfqq == cfqq) {
3476 WARN(1, "cfqq->new_cfqq loop detected\n");
3477 break;
3478 }
3479 next = __cfqq->new_cfqq;
3480 cfq_put_queue(__cfqq);
3481 __cfqq = next;
3482 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003483}
3484
3485static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3486{
3487 if (unlikely(cfqq == cfqd->active_queue)) {
3488 __cfq_slice_expired(cfqd, cfqq, 0);
3489 cfq_schedule_dispatch(cfqd);
3490 }
3491
3492 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003493
Jens Axboe89850f72006-07-22 16:48:31 +02003494 cfq_put_queue(cfqq);
3495}
3496
Tejun Heo9b84cac2011-12-14 00:33:42 +01003497static void cfq_init_icq(struct io_cq *icq)
3498{
3499 struct cfq_io_cq *cic = icq_to_cic(icq);
3500
3501 cic->ttime.last_end_request = jiffies;
3502}
3503
Tejun Heoc5869802011-12-14 00:33:41 +01003504static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003505{
Tejun Heoc5869802011-12-14 00:33:41 +01003506 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003507 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003508
Jens Axboeff6657c2009-04-08 10:58:57 +02003509 if (cic->cfqq[BLK_RW_ASYNC]) {
3510 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_ASYNC]);
3511 cic->cfqq[BLK_RW_ASYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003512 }
3513
Jens Axboeff6657c2009-04-08 10:58:57 +02003514 if (cic->cfqq[BLK_RW_SYNC]) {
3515 cfq_exit_cfqq(cfqd, cic->cfqq[BLK_RW_SYNC]);
3516 cic->cfqq[BLK_RW_SYNC] = NULL;
Jens Axboe89850f72006-07-22 16:48:31 +02003517 }
Jens Axboe89850f72006-07-22 16:48:31 +02003518}
3519
Tejun Heoabede6d2012-03-19 15:10:57 -07003520static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003521{
3522 struct task_struct *tsk = current;
3523 int ioprio_class;
3524
Jens Axboe3b181522005-06-27 10:56:24 +02003525 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003526 return;
3527
Tejun Heo598971b2012-03-19 15:10:58 -07003528 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003529 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003530 default:
3531 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3532 case IOPRIO_CLASS_NONE:
3533 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003534 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003535 */
3536 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003537 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003538 break;
3539 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003540 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003541 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3542 break;
3543 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003544 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003545 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3546 break;
3547 case IOPRIO_CLASS_IDLE:
3548 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3549 cfqq->ioprio = 7;
3550 cfq_clear_cfqq_idle_window(cfqq);
3551 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003552 }
3553
3554 /*
3555 * keep track of original prio settings in case we have to temporarily
3556 * elevate the priority of this queue
3557 */
3558 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboe3b181522005-06-27 10:56:24 +02003559 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003560}
3561
Tejun Heo598971b2012-03-19 15:10:58 -07003562static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003563{
Tejun Heo598971b2012-03-19 15:10:58 -07003564 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003565 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003566 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003567
Tejun Heo598971b2012-03-19 15:10:58 -07003568 /*
3569 * Check whether ioprio has changed. The condition may trigger
3570 * spuriously on a newly created cic but there's no harm.
3571 */
3572 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003573 return;
3574
Jens Axboeff6657c2009-04-08 10:58:57 +02003575 cfqq = cic->cfqq[BLK_RW_ASYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003576 if (cfqq) {
3577 struct cfq_queue *new_cfqq;
Tejun Heoabede6d2012-03-19 15:10:57 -07003578 new_cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio,
3579 GFP_ATOMIC);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003580 if (new_cfqq) {
Jens Axboeff6657c2009-04-08 10:58:57 +02003581 cic->cfqq[BLK_RW_ASYNC] = new_cfqq;
Jens Axboecaaa5f92006-06-16 11:23:00 +02003582 cfq_put_queue(cfqq);
3583 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003584 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003585
Jens Axboeff6657c2009-04-08 10:58:57 +02003586 cfqq = cic->cfqq[BLK_RW_SYNC];
Jens Axboecaaa5f92006-06-16 11:23:00 +02003587 if (cfqq)
3588 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003589
3590 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003591}
3592
Jens Axboed5036d72009-06-26 10:44:34 +02003593static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003594 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003595{
3596 RB_CLEAR_NODE(&cfqq->rb_node);
3597 RB_CLEAR_NODE(&cfqq->p_node);
3598 INIT_LIST_HEAD(&cfqq->fifo);
3599
Shaohua Li30d7b942011-01-07 08:46:59 +01003600 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003601 cfqq->cfqd = cfqd;
3602
3603 cfq_mark_cfqq_prio_changed(cfqq);
3604
3605 if (is_sync) {
3606 if (!cfq_class_idle(cfqq))
3607 cfq_mark_cfqq_idle_window(cfqq);
3608 cfq_mark_cfqq_sync(cfqq);
3609 }
3610 cfqq->pid = pid;
3611}
3612
Vivek Goyal246103332009-12-03 12:59:51 -05003613#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo598971b2012-03-19 15:10:58 -07003614static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003615{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003616 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo598971b2012-03-19 15:10:58 -07003617 struct cfq_queue *sync_cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003618 uint64_t serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003619
Tejun Heo598971b2012-03-19 15:10:58 -07003620 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003621 serial_nr = bio_blkcg(bio)->css.serial_nr;
Tejun Heo598971b2012-03-19 15:10:58 -07003622 rcu_read_unlock();
3623
3624 /*
3625 * Check whether blkcg has changed. The condition may trigger
3626 * spuriously on a newly created cic but there's no harm.
3627 */
Tejun Heof4da8072014-09-08 08:15:20 +09003628 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Vivek Goyal246103332009-12-03 12:59:51 -05003629 return;
3630
Tejun Heo598971b2012-03-19 15:10:58 -07003631 sync_cfqq = cic_to_cfqq(cic, 1);
Vivek Goyal246103332009-12-03 12:59:51 -05003632 if (sync_cfqq) {
3633 /*
3634 * Drop reference to sync queue. A new sync queue will be
3635 * assigned in new group upon arrival of a fresh request.
3636 */
3637 cfq_log_cfqq(cfqd, sync_cfqq, "changed cgroup");
3638 cic_set_cfqq(cic, NULL, 1);
3639 cfq_put_queue(sync_cfqq);
3640 }
Tejun Heo598971b2012-03-19 15:10:58 -07003641
Tejun Heof4da8072014-09-08 08:15:20 +09003642 cic->blkcg_serial_nr = serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003643}
Tejun Heo598971b2012-03-19 15:10:58 -07003644#else
3645static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio) { }
Vivek Goyal246103332009-12-03 12:59:51 -05003646#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003649cfq_find_alloc_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
3650 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651{
Tejun Heo3c798392012-04-16 13:57:25 -07003652 struct blkcg *blkcg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct cfq_queue *cfqq, *new_cfqq = NULL;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003654 struct cfq_group *cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656retry:
Tejun Heo2a7f1242012-03-05 13:15:01 -08003657 rcu_read_lock();
3658
Tejun Heo3c798392012-04-16 13:57:25 -07003659 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08003660 cfqg = cfq_lookup_create_cfqg(cfqd, blkcg);
Konstantin Khlebnikov69abaff2015-02-09 16:42:49 +03003661 if (!cfqg) {
3662 cfqq = &cfqd->oom_cfqq;
3663 goto out;
3664 }
3665
Vasily Tarasov91fac312007-04-25 12:29:51 +02003666 cfqq = cic_to_cfqq(cic, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Jens Axboe6118b702009-06-30 09:34:12 +02003668 /*
3669 * Always try a new alloc if we fell back to the OOM cfqq
3670 * originally, since it should just be a temporary situation.
3671 */
3672 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
3673 cfqq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (new_cfqq) {
3675 cfqq = new_cfqq;
3676 new_cfqq = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003677 } else if (gfp_mask & __GFP_WAIT) {
Tejun Heo2a7f1242012-03-05 13:15:01 -08003678 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 spin_unlock_irq(cfqd->queue->queue_lock);
Christoph Lameter94f60302007-07-17 04:03:29 -07003680 new_cfqq = kmem_cache_alloc_node(cfq_pool,
Jens Axboe6118b702009-06-30 09:34:12 +02003681 gfp_mask | __GFP_ZERO,
Christoph Lameter94f60302007-07-17 04:03:29 -07003682 cfqd->queue->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 spin_lock_irq(cfqd->queue->queue_lock);
Jens Axboe6118b702009-06-30 09:34:12 +02003684 if (new_cfqq)
3685 goto retry;
Glauber Costaa3cc86c2013-02-21 15:16:41 -08003686 else
3687 return &cfqd->oom_cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003688 } else {
Christoph Lameter94f60302007-07-17 04:03:29 -07003689 cfqq = kmem_cache_alloc_node(cfq_pool,
3690 gfp_mask | __GFP_ZERO,
3691 cfqd->queue->node);
Kiyoshi Ueda db3b5842005-06-17 16:15:10 +02003692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Jens Axboe6118b702009-06-30 09:34:12 +02003694 if (cfqq) {
3695 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
Tejun Heoabede6d2012-03-19 15:10:57 -07003696 cfq_init_prio_data(cfqq, cic);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003697 cfq_link_cfqq_cfqg(cfqq, cfqg);
Jens Axboe6118b702009-06-30 09:34:12 +02003698 cfq_log_cfqq(cfqd, cfqq, "alloced");
3699 } else
3700 cfqq = &cfqd->oom_cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 }
Konstantin Khlebnikov69abaff2015-02-09 16:42:49 +03003702out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (new_cfqq)
3704 kmem_cache_free(cfq_pool, new_cfqq);
3705
Tejun Heo2a7f1242012-03-05 13:15:01 -08003706 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return cfqq;
3708}
3709
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003710static struct cfq_queue **
3711cfq_async_queue_prio(struct cfq_data *cfqd, int ioprio_class, int ioprio)
3712{
Jens Axboefe094d92008-01-31 13:08:54 +01003713 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003714 case IOPRIO_CLASS_RT:
3715 return &cfqd->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003716 case IOPRIO_CLASS_NONE:
3717 ioprio = IOPRIO_NORM;
3718 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003719 case IOPRIO_CLASS_BE:
3720 return &cfqd->async_cfqq[1][ioprio];
3721 case IOPRIO_CLASS_IDLE:
3722 return &cfqd->async_idle_cfqq;
3723 default:
3724 BUG();
3725 }
3726}
3727
Jens Axboe15c31be2007-07-10 13:43:25 +02003728static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003729cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo4f85cb92012-03-05 13:15:28 -08003730 struct bio *bio, gfp_t gfp_mask)
Jens Axboe15c31be2007-07-10 13:43:25 +02003731{
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003732 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3733 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003734 struct cfq_queue **async_cfqq = NULL;
Jens Axboe15c31be2007-07-10 13:43:25 +02003735 struct cfq_queue *cfqq = NULL;
3736
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003737 if (!is_sync) {
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003738 if (!ioprio_valid(cic->ioprio)) {
3739 struct task_struct *tsk = current;
3740 ioprio = task_nice_ioprio(tsk);
3741 ioprio_class = task_nice_ioclass(tsk);
3742 }
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003743 async_cfqq = cfq_async_queue_prio(cfqd, ioprio_class, ioprio);
3744 cfqq = *async_cfqq;
3745 }
3746
Jens Axboe6118b702009-06-30 09:34:12 +02003747 if (!cfqq)
Tejun Heoabede6d2012-03-19 15:10:57 -07003748 cfqq = cfq_find_alloc_queue(cfqd, is_sync, cic, bio, gfp_mask);
Jens Axboe15c31be2007-07-10 13:43:25 +02003749
3750 /*
3751 * pin the queue now that it's allocated, scheduler exit will prune it
3752 */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003753 if (!is_sync && !(*async_cfqq)) {
Shaohua Li30d7b942011-01-07 08:46:59 +01003754 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003755 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003756 }
3757
Shaohua Li30d7b942011-01-07 08:46:59 +01003758 cfqq->ref++;
Jens Axboe15c31be2007-07-10 13:43:25 +02003759 return cfqq;
3760}
3761
Jens Axboe22e2c502005-06-27 10:55:12 +02003762static void
Shaohua Li383cd722011-07-12 14:24:35 +02003763__cfq_update_io_thinktime(struct cfq_ttime *ttime, unsigned long slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003764{
Shaohua Li383cd722011-07-12 14:24:35 +02003765 unsigned long elapsed = jiffies - ttime->last_end_request;
3766 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003767
Shaohua Li383cd722011-07-12 14:24:35 +02003768 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
3769 ttime->ttime_total = (7*ttime->ttime_total + 256*elapsed) / 8;
3770 ttime->ttime_mean = (ttime->ttime_total + 128) / ttime->ttime_samples;
3771}
3772
3773static void
3774cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003775 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003776{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003777 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003778 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003779 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3780 cfqd->cfq_slice_idle);
3781 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003782#ifdef CONFIG_CFQ_GROUP_IOSCHED
3783 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3784#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003785}
3786
Jens Axboe206dc692006-03-28 13:03:44 +02003787static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003788cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003789 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003790{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003791 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003792 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003793 if (cfqq->last_request_pos) {
3794 if (cfqq->last_request_pos < blk_rq_pos(rq))
3795 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3796 else
3797 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3798 }
Jens Axboe206dc692006-03-28 13:03:44 +02003799
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003800 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003801 if (blk_queue_nonrot(cfqd->queue))
3802 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3803 else
3804 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003805}
Jens Axboe22e2c502005-06-27 10:55:12 +02003806
3807/*
3808 * Disable idle window if the process thinks too long or seeks so much that
3809 * it doesn't matter
3810 */
3811static void
3812cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003813 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003814{
Jens Axboe7b679132008-05-30 12:23:07 +02003815 int old_idle, enable_idle;
Jens Axboe1be92f22007-04-19 14:32:26 +02003816
Jens Axboe08717142008-01-28 11:38:15 +01003817 /*
3818 * Don't idle for async or idle io prio class
3819 */
3820 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f22007-04-19 14:32:26 +02003821 return;
3822
Jens Axboec265a7f2008-06-26 13:49:33 +02003823 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003824
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003825 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3826 cfq_mark_cfqq_deep(cfqq);
3827
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003828 if (cfqq->next_rq && (cfqq->next_rq->cmd_flags & REQ_NOIDLE))
3829 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003830 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003831 !cfqd->cfq_slice_idle ||
3832 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003833 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003834 else if (sample_valid(cic->ttime.ttime_samples)) {
3835 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003836 enable_idle = 0;
3837 else
3838 enable_idle = 1;
3839 }
3840
Jens Axboe7b679132008-05-30 12:23:07 +02003841 if (old_idle != enable_idle) {
3842 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3843 if (enable_idle)
3844 cfq_mark_cfqq_idle_window(cfqq);
3845 else
3846 cfq_clear_cfqq_idle_window(cfqq);
3847 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003848}
3849
Jens Axboe22e2c502005-06-27 10:55:12 +02003850/*
3851 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3852 * no or if we aren't sure, a 1 will cause a preempt.
3853 */
Jens Axboea6151c32009-10-07 20:02:57 +02003854static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003855cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003856 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003857{
Jens Axboe6d048f52007-04-25 12:44:27 +02003858 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003859
Jens Axboe6d048f52007-04-25 12:44:27 +02003860 cfqq = cfqd->active_queue;
3861 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003862 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003863
Jens Axboe6d048f52007-04-25 12:44:27 +02003864 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003865 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003866
3867 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003868 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003869
Jens Axboe22e2c502005-06-27 10:55:12 +02003870 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003871 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3872 */
3873 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3874 return false;
3875
3876 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003877 * if the new request is sync, but the currently running queue is
3878 * not, let the sync request have priority.
3879 */
Jens Axboe5e705372006-07-13 12:39:25 +02003880 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003881 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003882
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003883 if (new_cfqq->cfqg != cfqq->cfqg)
3884 return false;
3885
3886 if (cfq_slice_used(cfqq))
3887 return true;
3888
3889 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003890 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003891 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
3892 new_cfqq->service_tree->count == 2 &&
3893 RB_EMPTY_ROOT(&cfqq->sort_list))
3894 return true;
3895
Jens Axboe374f84a2006-07-23 01:42:19 +02003896 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003897 * So both queues are sync. Let the new request get disk time if
3898 * it's a metadata request and the current queue is doing regular IO.
3899 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02003900 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02003901 return true;
3902
3903 /*
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003904 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
3905 */
3906 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003907 return true;
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01003908
Shaohua Lid2d59e12010-11-08 15:01:03 +01003909 /* An idle queue should not be idle now for some reason */
3910 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
3911 return true;
3912
Jens Axboe1e3335d2007-02-14 19:59:49 +01003913 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003914 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003915
3916 /*
3917 * if this request is as-good as one we would expect from the
3918 * current cfqq, let it preempt
3919 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01003920 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02003921 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003922
Jens Axboea6151c32009-10-07 20:02:57 +02003923 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003924}
3925
3926/*
3927 * cfqq preempts the active queue. if we allowed preempt with no slice left,
3928 * let it have half of its nominal slice.
3929 */
3930static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3931{
Shaohua Lidf0793a2012-01-19 09:20:09 +01003932 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
3933
Jens Axboe7b679132008-05-30 12:23:07 +02003934 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01003935 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003936
Jens Axboebf572252006-07-19 20:29:12 +02003937 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003938 * workload type is changed, don't save slice, otherwise preempt
3939 * doesn't happen
3940 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01003941 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003942 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01003943
3944 /*
Jens Axboebf572252006-07-19 20:29:12 +02003945 * Put the new queue at the front of the of the current list,
3946 * so we know that it will be selected next.
3947 */
3948 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02003949
3950 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01003951
Justin TerAvest62a37f62011-03-23 08:25:44 +01003952 cfqq->slice_end = 0;
3953 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003954}
3955
3956/*
Jens Axboe5e705372006-07-13 12:39:25 +02003957 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02003958 * something we should do about it
3959 */
3960static void
Jens Axboe5e705372006-07-13 12:39:25 +02003961cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
3962 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003963{
Tejun Heoc5869802011-12-14 00:33:41 +01003964 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02003965
Aaron Carroll45333d52008-08-26 15:52:36 +02003966 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003967 if (rq->cmd_flags & REQ_PRIO)
3968 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02003969
Shaohua Li383cd722011-07-12 14:24:35 +02003970 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003971 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02003972 cfq_update_idle_window(cfqd, cfqq, cic);
3973
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003974 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003975
3976 if (cfqq == cfqd->active_queue) {
3977 /*
Jens Axboeb0291952009-04-07 11:38:31 +02003978 * Remember that we saw a request from this process, but
3979 * don't start queuing just yet. Otherwise we risk seeing lots
3980 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02003981 * and merging. If the request is already larger than a single
3982 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02003983 * merging is already done. Ditto for a busy system that
3984 * has other work pending, don't risk delaying until the
3985 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02003986 */
Jens Axboed6ceb252009-04-14 14:18:16 +02003987 if (cfq_cfqq_wait_request(cfqq)) {
Jens Axboe2d870722009-04-15 12:12:46 +02003988 if (blk_rq_bytes(rq) > PAGE_CACHE_SIZE ||
3989 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07003990 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01003991 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003992 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003993 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07003994 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05003995 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02003996 }
Jens Axboed6ceb252009-04-14 14:18:16 +02003997 }
Jens Axboe5e705372006-07-13 12:39:25 +02003998 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02003999 /*
4000 * not the active queue - expire current slice if it is
4001 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01004002 * has some old slice time left and is of higher priority or
4003 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02004004 */
4005 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004006 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004007 }
4008}
4009
Jens Axboe165125e2007-07-24 09:28:11 +02004010static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004011{
Jens Axboeb4878f22005-10-20 16:42:29 +02004012 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02004013 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004014
Jens Axboe7b679132008-05-30 12:23:07 +02004015 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07004016 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Jan Kara8b4922d2014-02-24 16:39:52 +01004018 rq->fifo_time = jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
Jens Axboe22e2c502005-06-27 10:55:12 +02004019 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01004020 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07004021 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
4022 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02004023 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
4025
Aaron Carroll45333d52008-08-26 15:52:36 +02004026/*
4027 * Update hw_tag based on peak queue depth over 50 samples under
4028 * sufficient load.
4029 */
4030static void cfq_update_hw_tag(struct cfq_data *cfqd)
4031{
Shaohua Li1a1238a2009-10-27 08:46:23 +01004032 struct cfq_queue *cfqq = cfqd->active_queue;
4033
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004034 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4035 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004036
4037 if (cfqd->hw_tag == 1)
4038 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02004039
4040 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004041 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004042 return;
4043
Shaohua Li1a1238a2009-10-27 08:46:23 +01004044 /*
4045 * If active queue hasn't enough requests and can idle, cfq might not
4046 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4047 * case
4048 */
4049 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4050 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004051 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01004052 return;
4053
Aaron Carroll45333d52008-08-26 15:52:36 +02004054 if (cfqd->hw_tag_samples++ < 50)
4055 return;
4056
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004057 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004058 cfqd->hw_tag = 1;
4059 else
4060 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02004061}
4062
Vivek Goyal7667aa02009-12-08 17:52:58 -05004063static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4064{
Tejun Heoc5869802011-12-14 00:33:41 +01004065 struct cfq_io_cq *cic = cfqd->active_cic;
Vivek Goyal7667aa02009-12-08 17:52:58 -05004066
Justin TerAvest02a8f012011-02-09 14:20:03 +01004067 /* If the queue already has requests, don't wait */
4068 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4069 return false;
4070
Vivek Goyal7667aa02009-12-08 17:52:58 -05004071 /* If there are other queues in the group, don't wait */
4072 if (cfqq->cfqg->nr_cfqq > 1)
4073 return false;
4074
Shaohua Li7700fc42011-07-12 14:24:56 +02004075 /* the only queue in the group, but think time is big */
4076 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4077 return false;
4078
Vivek Goyal7667aa02009-12-08 17:52:58 -05004079 if (cfq_slice_used(cfqq))
4080 return true;
4081
4082 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02004083 if (cic && sample_valid(cic->ttime.ttime_samples)
4084 && (cfqq->slice_end - jiffies < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004085 return true;
4086
4087 /*
4088 * If think times is less than a jiffy than ttime_mean=0 and above
4089 * will not be true. It might happen that slice has not expired yet
4090 * but will expire soon (4-5 ns) during select_queue(). To cover the
4091 * case where think time is less than a jiffy, mark the queue wait
4092 * busy if only 1 jiffy is left in the slice.
4093 */
4094 if (cfqq->slice_end - jiffies == 1)
4095 return true;
4096
4097 return false;
4098}
4099
Jens Axboe165125e2007-07-24 09:28:11 +02004100static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Jens Axboe5e705372006-07-13 12:39:25 +02004102 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004103 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02004104 const int sync = rq_is_sync(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004105 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
Jens Axboeb4878f22005-10-20 16:42:29 +02004107 now = jiffies;
Christoph Hellwig33659eb2010-08-07 18:17:56 +02004108 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d",
4109 !!(rq->cmd_flags & REQ_NOIDLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Aaron Carroll45333d52008-08-26 15:52:36 +02004111 cfq_update_hw_tag(cfqd);
4112
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004113 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02004114 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004115 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02004116 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004117 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07004118 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
4119 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004121 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02004122
Vivek Goyal365722b2009-10-03 15:21:27 +02004123 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04004124 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004125
Shaohua Li383cd722011-07-12 14:24:35 +02004126 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004127
4128 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04004129 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004130 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04004131 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4132 cfqq_type(cfqq));
4133
4134 st->ttime.last_end_request = now;
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004135 if (!time_after(rq->start_time + cfqd->cfq_fifo_expire[1], now))
4136 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02004137 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004138
Shaohua Li7700fc42011-07-12 14:24:56 +02004139#ifdef CONFIG_CFQ_GROUP_IOSCHED
4140 cfqq->cfqg->ttime.last_end_request = now;
4141#endif
4142
Jens Axboecaaa5f92006-06-16 11:23:00 +02004143 /*
4144 * If this is the active queue, check if it needs to be expired,
4145 * or if we want to idle in case it has no pending requests.
4146 */
4147 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02004148 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4149
Jens Axboe44f7c162007-01-19 11:51:58 +11004150 if (cfq_cfqq_slice_new(cfqq)) {
4151 cfq_set_prio_slice(cfqd, cfqq);
4152 cfq_clear_cfqq_slice_new(cfqq);
4153 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05004154
4155 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05004156 * Should we wait for next request to come in before we expire
4157 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05004158 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05004159 if (cfq_should_wait_busy(cfqd, cfqq)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004160 unsigned long extend_sl = cfqd->cfq_slice_idle;
4161 if (!cfqd->cfq_slice_idle)
4162 extend_sl = cfqd->cfq_group_idle;
4163 cfqq->slice_end = jiffies + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05004164 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01004165 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05004166 }
4167
Jens Axboea36e71f2009-04-15 12:15:11 +02004168 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004169 * Idling is not enabled on:
4170 * - expired queues
4171 * - idle-priority queues
4172 * - async queues
4173 * - queues with still some requests queued
4174 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02004175 */
Jens Axboe08717142008-01-28 11:38:15 +01004176 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02004177 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004178 else if (sync && cfqq_empty &&
4179 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004180 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004181 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004182 }
Jens Axboe6d048f52007-04-25 12:44:27 +02004183
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004184 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02004185 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Jens Axboe89850f72006-07-22 16:48:31 +02004188static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004189{
Jens Axboe1b379d82009-08-11 08:26:11 +02004190 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02004191 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004192 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02004193 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004194
4195 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02004196}
4197
Jens Axboe165125e2007-07-24 09:28:11 +02004198static int cfq_may_queue(struct request_queue *q, int rw)
Jens Axboe22e2c502005-06-27 10:55:12 +02004199{
4200 struct cfq_data *cfqd = q->elevator->elevator_data;
4201 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01004202 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02004203 struct cfq_queue *cfqq;
4204
4205 /*
4206 * don't force setup of a queue from here, as a call to may_queue
4207 * does not necessarily imply that a request actually will be queued.
4208 * so just lookup a possibly existing queue, or return 'may queue'
4209 * if that fails
4210 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01004211 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004212 if (!cic)
4213 return ELV_MQUEUE_MAY;
4214
Jens Axboeb0b78f82009-04-08 10:56:08 +02004215 cfqq = cic_to_cfqq(cic, rw_is_sync(rw));
Jens Axboe22e2c502005-06-27 10:55:12 +02004216 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004217 cfq_init_prio_data(cfqq, cic);
Jens Axboe22e2c502005-06-27 10:55:12 +02004218
Jens Axboe89850f72006-07-22 16:48:31 +02004219 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004220 }
4221
4222 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
4224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225/*
4226 * queue lock held here
4227 */
Jens Axboebb37b942006-12-01 10:42:33 +01004228static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Jens Axboe5e705372006-07-13 12:39:25 +02004230 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Jens Axboe5e705372006-07-13 12:39:25 +02004232 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004233 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Jens Axboe22e2c502005-06-27 10:55:12 +02004235 BUG_ON(!cfqq->allocated[rw]);
4236 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004238 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004239 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01004240 rq->elv.priv[0] = NULL;
4241 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 cfq_put_queue(cfqq);
4244 }
4245}
4246
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004247static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004248cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004249 struct cfq_queue *cfqq)
4250{
4251 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4252 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004253 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004254 cfq_put_queue(cfqq);
4255 return cic_to_cfqq(cic, 1);
4256}
4257
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004258/*
4259 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4260 * was the last process referring to said cfqq.
4261 */
4262static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004263split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004264{
4265 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004266 cfqq->pid = current->pid;
4267 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004268 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004269 return cfqq;
4270 }
4271
4272 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004273
4274 cfq_put_cooperator(cfqq);
4275
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004276 cfq_put_queue(cfqq);
4277 return NULL;
4278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004280 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004282static int
Tejun Heo852c7882012-03-05 13:15:27 -08004283cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4284 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
4286 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004287 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004289 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004290 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 might_sleep_if(gfp_mask & __GFP_WAIT);
4293
Tejun Heo216284c2011-12-14 00:33:38 +01004294 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004295
Tejun Heo598971b2012-03-19 15:10:58 -07004296 check_ioprio_changed(cic, bio);
4297 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004298new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004299 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004300 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004301 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio, gfp_mask);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004302 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004303 } else {
4304 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004305 * If the queue was seeky for too long, break it apart.
4306 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004307 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004308 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4309 cfqq = split_cfqq(cic, cfqq);
4310 if (!cfqq)
4311 goto new_queue;
4312 }
4313
4314 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004315 * Check to see if this queue is scheduled to merge with
4316 * another, closely cooperating queue. The merging of
4317 * queues happens here as it must be done in process context.
4318 * The reference on new_cfqq was taken in merge_cfqqs.
4319 */
4320 if (cfqq->new_cfqq)
4321 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004325
Jens Axboe6fae9c22011-03-01 15:04:39 -05004326 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004327 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004328 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004329 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c2011-12-14 00:33:38 +01004330 spin_unlock_irq(q->queue_lock);
Jens Axboe5e705372006-07-13 12:39:25 +02004331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
David Howells65f27f32006-11-22 14:55:48 +00004334static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004335{
David Howells65f27f32006-11-22 14:55:48 +00004336 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004337 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004338 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004339
Jens Axboe40bb54d2009-04-15 12:11:10 +02004340 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004341 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004342 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004343}
4344
4345/*
4346 * Timer running if the active_queue is currently idling inside its time slice
4347 */
4348static void cfq_idle_slice_timer(unsigned long data)
4349{
4350 struct cfq_data *cfqd = (struct cfq_data *) data;
4351 struct cfq_queue *cfqq;
4352 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004353 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004354
Jens Axboe7b679132008-05-30 12:23:07 +02004355 cfq_log(cfqd, "idle timer fired");
4356
Jens Axboe22e2c502005-06-27 10:55:12 +02004357 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4358
Jens Axboefe094d92008-01-31 13:08:54 +01004359 cfqq = cfqd->active_queue;
4360 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004361 timed_out = 0;
4362
Jens Axboe22e2c502005-06-27 10:55:12 +02004363 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004364 * We saw a request before the queue expired, let it through
4365 */
4366 if (cfq_cfqq_must_dispatch(cfqq))
4367 goto out_kick;
4368
4369 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004370 * expired
4371 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004372 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004373 goto expire;
4374
4375 /*
4376 * only expire and reinvoke request handler, if there are
4377 * other queues with pending requests
4378 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004379 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004380 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004381
4382 /*
4383 * not expired and it has a request pending, let it dispatch
4384 */
Jens Axboe75e50982009-04-07 08:56:14 +02004385 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004386 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004387
4388 /*
4389 * Queue depth flag is reset only when the idle didn't succeed
4390 */
4391 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004392 }
4393expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004394 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004395out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004396 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004397out_cont:
4398 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
4399}
4400
Jens Axboe3b181522005-06-27 10:56:24 +02004401static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4402{
4403 del_timer_sync(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004404 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004405}
Jens Axboe22e2c502005-06-27 10:55:12 +02004406
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004407static void cfq_put_async_queues(struct cfq_data *cfqd)
4408{
4409 int i;
4410
4411 for (i = 0; i < IOPRIO_BE_NR; i++) {
4412 if (cfqd->async_cfqq[0][i])
4413 cfq_put_queue(cfqd->async_cfqq[0][i]);
4414 if (cfqd->async_cfqq[1][i])
4415 cfq_put_queue(cfqd->async_cfqq[1][i]);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004416 }
Oleg Nesterov2389d1e2007-11-05 08:58:05 +01004417
4418 if (cfqd->async_idle_cfqq)
4419 cfq_put_queue(cfqd->async_idle_cfqq);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004420}
4421
Jens Axboeb374d182008-10-31 10:05:07 +01004422static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Jens Axboe22e2c502005-06-27 10:55:12 +02004424 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004425 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004426
Jens Axboe3b181522005-06-27 10:56:24 +02004427 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004428
Al Virod9ff4182006-03-18 13:51:22 -05004429 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004430
Al Virod9ff4182006-03-18 13:51:22 -05004431 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004432 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004433
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02004434 cfq_put_async_queues(cfqd);
Tejun Heo03aa2642012-03-05 13:15:19 -08004435
4436 spin_unlock_irq(q->queue_lock);
4437
Al Viroa90d7422006-03-18 12:05:37 -05004438 cfq_shutdown_timer_wq(cfqd);
4439
Tejun Heoffea73f2012-06-04 10:02:29 +02004440#ifdef CONFIG_CFQ_GROUP_IOSCHED
4441 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4442#else
Tejun Heof51b8022012-03-05 13:15:05 -08004443 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004444#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004445 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446}
4447
Jianpeng Mad50235b2013-07-03 13:25:24 +02004448static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
4450 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004451 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004452 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004453 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Jianpeng Mad50235b2013-07-03 13:25:24 +02004455 eq = elevator_alloc(q, e);
4456 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004457 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004458
Joe Perchesc1b511e2013-08-29 15:21:42 -07004459 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004460 if (!cfqd) {
4461 kobject_put(&eq->kobj);
4462 return -ENOMEM;
4463 }
4464 eq->elevator_data = cfqd;
4465
Tejun Heof51b8022012-03-05 13:15:05 -08004466 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004467 spin_lock_irq(q->queue_lock);
4468 q->elevator = eq;
4469 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004470
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004471 /* Init root service tree */
4472 cfqd->grp_service_tree = CFQ_RB_ROOT;
4473
Tejun Heof51b8022012-03-05 13:15:05 -08004474 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004475#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004476 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004477 if (ret)
4478 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004479
Tejun Heoa2b16932012-04-13 13:11:33 -07004480 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004481#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004482 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004483 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4484 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004485 if (!cfqd->root_group)
4486 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004487
Tejun Heoa2b16932012-04-13 13:11:33 -07004488 cfq_init_cfqg_base(cfqd->root_group);
4489#endif
Tejun Heo3381cb82012-04-01 14:38:44 -07004490 cfqd->root_group->weight = 2 * CFQ_WEIGHT_DEFAULT;
Tejun Heoe71357e2013-01-09 08:05:10 -08004491 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_DEFAULT;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004492
Jens Axboe26a2ac02009-04-23 12:13:27 +02004493 /*
4494 * Not strictly needed (since RB_ROOT just clears the node and we
4495 * zeroed cfqd on alloc), but better be safe in case someone decides
4496 * to add magic to the rb code
4497 */
4498 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4499 cfqd->prio_trees[i] = RB_ROOT;
4500
Jens Axboe6118b702009-06-30 09:34:12 +02004501 /*
4502 * Our fallback cfqq if cfq_find_alloc_queue() runs into OOM issues.
4503 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004504 * will not attempt to free it. oom_cfqq is linked to root_group
4505 * but shouldn't hold a reference as it'll never be unlinked. Lose
4506 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004507 */
4508 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004509 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004510
4511 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004512 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004513 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004514 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Jens Axboe22e2c502005-06-27 10:55:12 +02004516 init_timer(&cfqd->idle_slice_timer);
4517 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
4518 cfqd->idle_slice_timer.data = (unsigned long) cfqd;
4519
Jens Axboe23e018a2009-10-05 08:52:35 +02004520 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004523 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4524 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 cfqd->cfq_back_max = cfq_back_max;
4526 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004527 cfqd->cfq_slice[0] = cfq_slice_async;
4528 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004529 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004530 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004531 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004532 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004533 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004534 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004535 /*
4536 * we optimistically start assuming sync ops weren't delayed in last
4537 * second, in order to have larger depth for async operations.
4538 */
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004539 cfqd->last_delayed_sync = jiffies - HZ;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004540 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004541
4542out_free:
4543 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004544 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
Jens Axboe0bb97942015-06-10 08:01:20 -06004548static void cfq_registered_queue(struct request_queue *q)
4549{
4550 struct elevator_queue *e = q->elevator;
4551 struct cfq_data *cfqd = e->elevator_data;
4552
4553 /*
4554 * Default to IOPS mode with no idling for SSDs
4555 */
4556 if (blk_queue_nonrot(q))
4557 cfqd->cfq_slice_idle = 0;
4558}
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560/*
4561 * sysfs parts below -->
4562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563static ssize_t
4564cfq_var_show(unsigned int var, char *page)
4565{
Masanari Iida176167a2014-04-28 12:38:34 +09004566 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
4569static ssize_t
4570cfq_var_store(unsigned int *var, const char *page, size_t count)
4571{
4572 char *p = (char *) page;
4573
4574 *var = simple_strtoul(p, &p, 10);
4575 return count;
4576}
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004579static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004581 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 unsigned int __data = __VAR; \
4583 if (__CONV) \
4584 __data = jiffies_to_msecs(__data); \
4585 return cfq_var_show(__data, (page)); \
4586}
4587SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004588SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4589SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004590SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4591SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004592SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004593SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004594SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4595SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4596SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004597SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004598SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599#undef SHOW_FUNCTION
4600
4601#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004602static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004604 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 unsigned int __data; \
4606 int ret = cfq_var_store(&__data, (page), count); \
4607 if (__data < (MIN)) \
4608 __data = (MIN); \
4609 else if (__data > (MAX)) \
4610 __data = (MAX); \
4611 if (__CONV) \
4612 *(__PTR) = msecs_to_jiffies(__data); \
4613 else \
4614 *(__PTR) = __data; \
4615 return ret; \
4616}
4617STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004618STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4619 UINT_MAX, 1);
4620STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4621 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004622STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004623STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4624 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004625STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004626STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004627STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4628STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004629STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4630 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004631STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004632STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633#undef STORE_FUNCTION
4634
Al Viroe572ec72006-03-18 22:27:18 -05004635#define CFQ_ATTR(name) \
4636 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004637
Al Viroe572ec72006-03-18 22:27:18 -05004638static struct elv_fs_entry cfq_attrs[] = {
4639 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004640 CFQ_ATTR(fifo_expire_sync),
4641 CFQ_ATTR(fifo_expire_async),
4642 CFQ_ATTR(back_seek_max),
4643 CFQ_ATTR(back_seek_penalty),
4644 CFQ_ATTR(slice_sync),
4645 CFQ_ATTR(slice_async),
4646 CFQ_ATTR(slice_async_rq),
4647 CFQ_ATTR(slice_idle),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004648 CFQ_ATTR(group_idle),
Jens Axboe963b72f2009-10-03 19:42:18 +02004649 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004650 CFQ_ATTR(target_latency),
Al Viroe572ec72006-03-18 22:27:18 -05004651 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652};
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654static struct elevator_type iosched_cfq = {
4655 .ops = {
4656 .elevator_merge_fn = cfq_merge,
4657 .elevator_merged_fn = cfq_merged_request,
4658 .elevator_merge_req_fn = cfq_merged_requests,
Jens Axboeda775262006-12-20 11:04:12 +01004659 .elevator_allow_merge_fn = cfq_allow_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004660 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004661 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004663 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004666 .elevator_former_req_fn = elv_rb_former_request,
4667 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004668 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004669 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 .elevator_set_req_fn = cfq_set_request,
4671 .elevator_put_req_fn = cfq_put_request,
4672 .elevator_may_queue_fn = cfq_may_queue,
4673 .elevator_init_fn = cfq_init_queue,
4674 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004675 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004677 .icq_size = sizeof(struct cfq_io_cq),
4678 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004679 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004680 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 .elevator_owner = THIS_MODULE,
4682};
4683
Vivek Goyal3e252062009-12-04 10:36:42 -05004684#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004685static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004686 .pd_size = sizeof(struct cfq_group),
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004687 .cpd_size = sizeof(struct cfq_group_data),
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004688 .cftypes = cfq_blkcg_files,
4689
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004690 .cpd_init_fn = cfq_cpd_init,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004691 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004692 .pd_offline_fn = cfq_pd_offline,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004693 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004694};
Vivek Goyal3e252062009-12-04 10:36:42 -05004695#endif
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697static int __init cfq_init(void)
4698{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004699 int ret;
4700
Jens Axboe22e2c502005-06-27 10:55:12 +02004701 /*
4702 * could be 0 on HZ < 1000 setups
4703 */
4704 if (!cfq_slice_async)
4705 cfq_slice_async = 1;
4706 if (!cfq_slice_idle)
4707 cfq_slice_idle = 1;
4708
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004709#ifdef CONFIG_CFQ_GROUP_IOSCHED
4710 if (!cfq_group_idle)
4711 cfq_group_idle = 1;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004712
Tejun Heo3c798392012-04-16 13:57:25 -07004713 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004714 if (ret)
4715 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004716#else
4717 cfq_group_idle = 0;
4718#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004719
Tejun Heofd794952012-06-04 10:01:38 +02004720 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004721 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4722 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004723 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Tejun Heo3d3c2372011-12-14 00:33:42 +01004725 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004726 if (ret)
4727 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004728
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004729 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004730
4731err_free_pool:
4732 kmem_cache_destroy(cfq_pool);
4733err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004734#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004735 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004736#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
4740static void __exit cfq_exit(void)
4741{
Tejun Heoffea73f2012-06-04 10:02:29 +02004742#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004743 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004744#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004746 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747}
4748
4749module_init(cfq_init);
4750module_exit(cfq_exit);
4751
4752MODULE_AUTHOR("Jens Axboe");
4753MODULE_LICENSE("GPL");
4754MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");