blob: 9f342ef1ad426fa7d60e00fffeb313409cbbb3f4 [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>
Ingo Molnare6017572017-02-01 16:36:40 +010011#include <linux/sched/clock.h>
Al Viro1cc9be62006-03-18 12:29:52 -050012#include <linux/blkdev.h>
13#include <linux/elevator.h>
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060014#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/rbtree.h>
Jens Axboe22e2c502005-06-27 10:55:12 +020016#include <linux/ioprio.h>
Jens Axboe7b679132008-05-30 12:23:07 +020017#include <linux/blktrace_api.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040018#include <linux/blk-cgroup.h>
Tejun Heo6e736be2011-12-14 00:33:38 +010019#include "blk.h"
Jens Axboe87760e52016-11-09 12:38:14 -070020#include "blk-wbt.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * tunables
24 */
Jens Axboefe094d92008-01-31 13:08:54 +010025/* max queue in one round of service */
Shaohua Liabc3c742010-03-01 09:20:54 +010026static const int cfq_quantum = 8;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060027static const u64 cfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
Jens Axboefe094d92008-01-31 13:08:54 +010028/* maximum backwards seek, in KiB */
29static const int cfq_back_max = 16 * 1024;
30/* penalty of a backwards seek */
31static const int cfq_back_penalty = 2;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060032static const u64 cfq_slice_sync = NSEC_PER_SEC / 10;
33static u64 cfq_slice_async = NSEC_PER_SEC / 25;
Arjan van de Ven64100092006-01-06 09:46:02 +010034static const int cfq_slice_async_rq = 2;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060035static u64 cfq_slice_idle = NSEC_PER_SEC / 125;
36static u64 cfq_group_idle = NSEC_PER_SEC / 125;
37static const u64 cfq_target_latency = (u64)NSEC_PER_SEC * 3/10; /* 300 ms */
Corrado Zoccolo5db5d642009-10-26 22:44:04 +010038static const int cfq_hist_divisor = 4;
Jens Axboe22e2c502005-06-27 10:55:12 +020039
Jens Axboed9e76202007-04-20 14:27:50 +020040/*
Hou Tao5be6b752017-03-01 09:02:33 +080041 * offset from end of queue service tree for idle class
Jens Axboed9e76202007-04-20 14:27:50 +020042 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060043#define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5)
Hou Tao5be6b752017-03-01 09:02:33 +080044/* offset from end of group service tree under time slice mode */
45#define CFQ_SLICE_MODE_GROUP_DELAY (NSEC_PER_SEC / 5)
46/* offset from end of group service under IOPS mode */
47#define CFQ_IOPS_MODE_GROUP_DELAY (HZ / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020048
49/*
50 * below this threshold, we consider thinktime immediate
51 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060052#define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ)
Jens Axboed9e76202007-04-20 14:27:50 +020053
Jens Axboe22e2c502005-06-27 10:55:12 +020054#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020055#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050056#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020057
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010058#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010059#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010060#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010061#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010062
Tejun Heoa612fdd2011-12-14 00:33:41 +010063#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
64#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
65#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Christoph Lametere18b8902006-12-06 20:33:20 -080067static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jens Axboe22e2c502005-06-27 10:55:12 +020069#define CFQ_PRIO_LISTS IOPRIO_BE_NR
70#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020071#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
72
Jens Axboe206dc692006-03-28 13:03:44 +020073#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050074#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020075
Arianna Avanzinie48453c2015-06-05 23:38:42 +020076/* blkio-related constants */
Tejun Heo3ecca622015-08-18 14:55:35 -070077#define CFQ_WEIGHT_LEGACY_MIN 10
78#define CFQ_WEIGHT_LEGACY_DFL 500
79#define CFQ_WEIGHT_LEGACY_MAX 1000
Arianna Avanzinie48453c2015-06-05 23:38:42 +020080
Tejun Heoc5869802011-12-14 00:33:41 +010081struct cfq_ttime {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060082 u64 last_end_request;
Tejun Heoc5869802011-12-14 00:33:41 +010083
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060084 u64 ttime_total;
85 u64 ttime_mean;
Tejun Heoc5869802011-12-14 00:33:41 +010086 unsigned long ttime_samples;
Tejun Heoc5869802011-12-14 00:33:41 +010087};
88
Jens Axboe22e2c502005-06-27 10:55:12 +020089/*
Jens Axboecc09e292007-04-26 12:53:50 +020090 * Most of our rbtree usage is for sorting with min extraction, so
91 * if we cache the leftmost node we don't have to walk down the tree
92 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
93 * move this into the elevator for the rq sorting as well.
94 */
95struct cfq_rb_root {
Davidlohr Bueso09663c82017-09-08 16:15:05 -070096 struct rb_root_cached rb;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -070097 struct rb_node *rb_rightmost;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010098 unsigned count;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050099 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200100 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +0200101};
Davidlohr Bueso09663c82017-09-08 16:15:05 -0700102#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT_CACHED, \
Davidlohr Buesof0f1a452017-09-08 16:15:25 -0700103 .rb_rightmost = NULL, \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600104 .ttime = {.last_end_request = ktime_get_ns(),},}
Jens Axboecc09e292007-04-26 12:53:50 +0200105
106/*
Jens Axboe6118b702009-06-30 09:34:12 +0200107 * Per process-grouping structure
108 */
109struct cfq_queue {
110 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100111 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200112 /* various state flags, see below */
113 unsigned int flags;
114 /* parent cfq_data */
115 struct cfq_data *cfqd;
116 /* service_tree member */
117 struct rb_node rb_node;
118 /* service_tree key */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600119 u64 rb_key;
Jens Axboe6118b702009-06-30 09:34:12 +0200120 /* prio tree member */
121 struct rb_node p_node;
122 /* prio tree root we belong to, if any */
123 struct rb_root *p_root;
124 /* sorted list of pending requests */
125 struct rb_root sort_list;
126 /* if fifo isn't expired, next request to serve */
127 struct request *next_rq;
128 /* requests queued in sort_list */
129 int queued[2];
130 /* currently allocated requests */
131 int allocated[2];
132 /* fifo list of requests in sort_list */
133 struct list_head fifo;
134
Vivek Goyaldae739e2009-12-03 12:59:45 -0500135 /* time when queue got scheduled in to dispatch first request. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600136 u64 dispatch_start;
137 u64 allocated_slice;
138 u64 slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500139 /* time when first request from queue completed and slice started. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600140 u64 slice_start;
141 u64 slice_end;
Jan Kara93fdf142016-06-28 09:04:00 +0200142 s64 slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200143
Christoph Hellwig65299a32011-08-23 14:50:29 +0200144 /* pending priority requests */
145 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200146 /* number of requests that are on the dispatch list or inside driver */
147 int dispatched;
148
149 /* io prio of this group */
150 unsigned short ioprio, org_ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -0600151 unsigned short ioprio_class, org_ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200152
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100153 pid_t pid;
154
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100155 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400156 sector_t last_request_pos;
157
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100158 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400159 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500160 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200161 /* Number of sectors dispatched from queue in single dispatch round */
162 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200163};
164
165/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100166 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100167 * IDLE is handled separately, so it has negative index
168 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400169enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100170 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500171 RT_WORKLOAD = 1,
172 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200173 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100174};
175
176/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100177 * Second index in the service_trees.
178 */
179enum wl_type_t {
180 ASYNC_WORKLOAD = 0,
181 SYNC_NOIDLE_WORKLOAD = 1,
182 SYNC_WORKLOAD = 2
183};
184
Tejun Heo155fead2012-04-01 14:38:44 -0700185struct cfqg_stats {
186#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo155fead2012-04-01 14:38:44 -0700187 /* number of ios merged */
188 struct blkg_rwstat merged;
189 /* total time spent on device in ns, may not be accurate w/ queueing */
190 struct blkg_rwstat service_time;
191 /* total time spent waiting in scheduler queue in ns */
192 struct blkg_rwstat wait_time;
193 /* number of IOs queued up */
194 struct blkg_rwstat queued;
Tejun Heo155fead2012-04-01 14:38:44 -0700195 /* total disk time and nr sectors dispatched by this group */
196 struct blkg_stat time;
197#ifdef CONFIG_DEBUG_BLK_CGROUP
198 /* time not charged to this cgroup */
199 struct blkg_stat unaccounted_time;
200 /* sum of number of ios queued across all samples */
201 struct blkg_stat avg_queue_size_sum;
202 /* count of samples taken for average */
203 struct blkg_stat avg_queue_size_samples;
204 /* how many times this group has been removed from service tree */
205 struct blkg_stat dequeue;
206 /* total time spent waiting for it to be assigned a timeslice. */
207 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700208 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700209 struct blkg_stat idle_time;
210 /* total time with empty current active q with other requests queued */
211 struct blkg_stat empty_time;
212 /* fields after this shouldn't be cleared on stat reset */
213 uint64_t start_group_wait_time;
214 uint64_t start_idle_time;
215 uint64_t start_empty_time;
216 uint16_t flags;
217#endif /* CONFIG_DEBUG_BLK_CGROUP */
218#endif /* CONFIG_CFQ_GROUP_IOSCHED */
219};
220
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200221/* Per-cgroup data */
222struct cfq_group_data {
223 /* must be the first member */
Tejun Heo81437642015-08-18 14:55:15 -0700224 struct blkcg_policy_data cpd;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200225
226 unsigned int weight;
227 unsigned int leaf_weight;
228};
229
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500230/* This is per cgroup per device grouping structure */
231struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700232 /* must be the first member */
233 struct blkg_policy_data pd;
234
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500235 /* group service_tree member */
236 struct rb_node rb_node;
237
238 /* group service_tree key */
239 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800240
241 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800242 * The number of active cfqgs and sum of their weights under this
243 * cfqg. This covers this cfqg's leaf_weight and all children's
244 * weights, but does not cover weights of further descendants.
245 *
246 * If a cfqg is on the service tree, it's active. An active cfqg
247 * also activates its parent and contributes to the children_weight
248 * of the parent.
249 */
250 int nr_active;
251 unsigned int children_weight;
252
253 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800254 * vfraction is the fraction of vdisktime that the tasks in this
255 * cfqg are entitled to. This is determined by compounding the
256 * ratios walking up from this cfqg to the root.
257 *
258 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
259 * vfractions on a service tree is approximately 1. The sum may
260 * deviate a bit due to rounding errors and fluctuations caused by
261 * cfqgs entering and leaving the service tree.
262 */
263 unsigned int vfraction;
264
265 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800266 * There are two weights - (internal) weight is the weight of this
267 * cfqg against the sibling cfqgs. leaf_weight is the wight of
268 * this cfqg against the child cfqgs. For the root cfqg, both
269 * weights are kept in sync for backward compatibility.
270 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500271 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100272 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700273 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500274
Tejun Heoe71357e2013-01-09 08:05:10 -0800275 unsigned int leaf_weight;
276 unsigned int new_leaf_weight;
277 unsigned int dev_leaf_weight;
278
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500279 /* number of cfqq currently on this group */
280 int nr_cfqq;
281
Jens Axboe22e2c502005-06-27 10:55:12 +0200282 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200283 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200284 * create the array for each prio class but at run time it is used
285 * only for RT and BE class and slot for IDLE class remains unused.
286 * This is primarily done to avoid confusion and a gcc warning.
287 */
288 unsigned int busy_queues_avg[CFQ_PRIO_NR];
289 /*
290 * rr lists of queues with requests. We maintain service trees for
291 * RT and BE classes. These trees are subdivided in subclasses
292 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
293 * class there is no subclassification and all the cfq queues go on
294 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100295 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200296 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100297 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100298 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500299
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600300 u64 saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400301 enum wl_type_t saved_wl_type;
302 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800303
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200304 /* number of requests that are on the dispatch list or inside driver */
305 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200306 struct cfq_ttime ttime;
Tejun Heo0b399202013-01-09 08:05:13 -0800307 struct cfqg_stats stats; /* stats for this cfqg */
Tejun Heo60a83702015-08-18 14:55:05 -0700308
309 /* async queue for each priority case */
310 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
311 struct cfq_queue *async_idle_cfqq;
312
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500313};
314
Tejun Heoc5869802011-12-14 00:33:41 +0100315struct cfq_io_cq {
316 struct io_cq icq; /* must be the first member */
317 struct cfq_queue *cfqq[2];
318 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700319 int ioprio; /* the current ioprio */
320#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof4da8072014-09-08 08:15:20 +0900321 uint64_t blkcg_serial_nr; /* the current blkcg serial */
Tejun Heo598971b2012-03-19 15:10:58 -0700322#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100323};
324
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500325/*
326 * Per block device queue structure
327 */
328struct cfq_data {
329 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500330 /* Root service tree for cfq_groups */
331 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800332 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500333
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100334 /*
335 * The priority currently being served
336 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400337 enum wl_class_t serving_wl_class;
338 enum wl_type_t serving_wl_type;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600339 u64 workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500340 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200341
342 /*
343 * Each priority tree is sorted by next_request position. These
344 * trees are used when determining if two or more queues are
345 * interleaving requests (see cfq_close_cooperator).
346 */
347 struct rb_root prio_trees[CFQ_PRIO_LISTS];
348
Jens Axboe22e2c502005-06-27 10:55:12 +0200349 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100350 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200351
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100352 int rq_in_driver;
353 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200354
355 /*
356 * queue-depth detection
357 */
358 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200359 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100360 /*
361 * hw_tag can be
362 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
363 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
364 * 0 => no NCQ
365 */
366 int hw_tag_est_depth;
367 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200368
369 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200370 * idle window management
371 */
Jan Kara91148322016-06-08 15:11:39 +0200372 struct hrtimer idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200373 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200374
375 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100376 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200377
Jens Axboe6d048f52007-04-25 12:44:27 +0200378 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * tunables, see top of file
382 */
383 unsigned int cfq_quantum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned int cfq_back_penalty;
385 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200386 unsigned int cfq_slice_async_rq;
Jens Axboe963b72f2009-10-03 19:42:18 +0200387 unsigned int cfq_latency;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600388 u64 cfq_fifo_expire[2];
389 u64 cfq_slice[2];
390 u64 cfq_slice_idle;
391 u64 cfq_group_idle;
392 u64 cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500393
Jens Axboe6118b702009-06-30 09:34:12 +0200394 /*
395 * Fallback dummy cfqq for extreme OOM conditions
396 */
397 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200398
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600399 u64 last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Vivek Goyal25fb5162009-12-03 12:59:46 -0500402static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
Tejun Heo60a83702015-08-18 14:55:05 -0700403static void cfq_put_queue(struct cfq_queue *cfqq);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500404
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{
Tejun Heo81437642015-08-18 14:55:15 -0700615 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
Jens Axboe4ceab712015-06-19 10:13:01 -0600616}
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
Jan Kara3984aa52016-01-12 16:24:19 +0100642static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
643 struct cfq_group *ancestor)
644{
645 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
646 cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
647}
648
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100649static inline void cfqg_get(struct cfq_group *cfqg)
650{
651 return blkg_get(cfqg_to_blkg(cfqg));
652}
653
654static inline void cfqg_put(struct cfq_group *cfqg)
655{
656 return blkg_put(cfqg_to_blkg(cfqg));
657}
658
Tejun Heo54e7ed12012-04-16 13:57:23 -0700659#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
Shaohua Li35fe6d72017-07-12 11:49:56 -0700660 blk_add_cgroup_trace_msg((cfqd)->queue, \
661 cfqg_to_blkg((cfqq)->cfqg)->blkcg, \
662 "cfq%d%c%c " fmt, (cfqq)->pid, \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400663 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
664 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Shaohua Li35fe6d72017-07-12 11:49:56 -0700665 ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700666} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500667
Tejun Heo54e7ed12012-04-16 13:57:23 -0700668#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
Shaohua Li35fe6d72017-07-12 11:49:56 -0700669 blk_add_cgroup_trace_msg((cfqd)->queue, \
670 cfqg_to_blkg(cfqg)->blkcg, fmt, ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700671} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500672
Tejun Heo155fead2012-04-01 14:38:44 -0700673static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600674 struct cfq_group *curr_cfqg,
675 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700676{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600677 blkg_rwstat_add(&cfqg->stats.queued, op, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700678 cfqg_stats_end_empty_time(&cfqg->stats);
679 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700680}
681
Tejun Heo155fead2012-04-01 14:38:44 -0700682static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600683 uint64_t time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700684{
Tejun Heo155fead2012-04-01 14:38:44 -0700685 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700686#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700687 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700688#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700689}
690
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600691static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
692 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700693{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600694 blkg_rwstat_add(&cfqg->stats.queued, op, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700695}
696
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600697static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
698 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700699{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600700 blkg_rwstat_add(&cfqg->stats.merged, op, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700701}
702
Tejun Heo155fead2012-04-01 14:38:44 -0700703static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600704 uint64_t start_time, uint64_t io_start_time,
705 unsigned int op)
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))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600711 blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700712 if (time_after64(io_start_time, start_time))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600713 blkg_rwstat_add(&stats->wait_time, op,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700714 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 */
Tejun Heo155fead2012-04-01 14:38:44 -0700721 blkg_rwstat_reset(&stats->merged);
722 blkg_rwstat_reset(&stats->service_time);
723 blkg_rwstat_reset(&stats->wait_time);
724 blkg_stat_reset(&stats->time);
725#ifdef CONFIG_DEBUG_BLK_CGROUP
726 blkg_stat_reset(&stats->unaccounted_time);
727 blkg_stat_reset(&stats->avg_queue_size_sum);
728 blkg_stat_reset(&stats->avg_queue_size_samples);
729 blkg_stat_reset(&stats->dequeue);
730 blkg_stat_reset(&stats->group_wait_time);
731 blkg_stat_reset(&stats->idle_time);
732 blkg_stat_reset(&stats->empty_time);
733#endif
734}
735
Tejun Heo0b399202013-01-09 08:05:13 -0800736/* @to += @from */
Tejun Heoe6269c42015-08-18 14:55:21 -0700737static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
Tejun Heo0b399202013-01-09 08:05:13 -0800738{
739 /* queued stats shouldn't be cleared */
Tejun Heoe6269c42015-08-18 14:55:21 -0700740 blkg_rwstat_add_aux(&to->merged, &from->merged);
741 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
742 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
743 blkg_stat_add_aux(&from->time, &from->time);
Tejun Heo0b399202013-01-09 08:05:13 -0800744#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoe6269c42015-08-18 14:55:21 -0700745 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
746 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
747 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
748 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
749 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
750 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
751 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
Tejun Heo0b399202013-01-09 08:05:13 -0800752#endif
753}
754
755/*
Tejun Heoe6269c42015-08-18 14:55:21 -0700756 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
Tejun Heo0b399202013-01-09 08:05:13 -0800757 * recursive stats can still account for the amount used by this cfqg after
758 * it's gone.
759 */
760static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
761{
762 struct cfq_group *parent = cfqg_parent(cfqg);
763
764 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
765
766 if (unlikely(!parent))
767 return;
768
Tejun Heoe6269c42015-08-18 14:55:21 -0700769 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800770 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800771}
772
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100773#else /* CONFIG_CFQ_GROUP_IOSCHED */
774
Tejun Heod02f7aa2013-01-09 08:05:11 -0800775static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Jan Kara3984aa52016-01-12 16:24:19 +0100776static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
777 struct cfq_group *ancestor)
778{
779 return true;
780}
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100781static inline void cfqg_get(struct cfq_group *cfqg) { }
782static inline void cfqg_put(struct cfq_group *cfqg) { }
783
Jens Axboe7b679132008-05-30 12:23:07 +0200784#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400785 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
786 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
787 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
788 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200789#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100790
Tejun Heo155fead2012-04-01 14:38:44 -0700791static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600792 struct cfq_group *curr_cfqg, unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700793static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600794 uint64_t time, unsigned long unaccounted_time) { }
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600795static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
796 unsigned int op) { }
797static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
798 unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700799static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600800 uint64_t start_time, uint64_t io_start_time,
801 unsigned int op) { }
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{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600821 u64 slice;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200822 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 Heo2da8de02015-08-18 14:55:02 -0700886 struct cfq_io_cq *cic, struct bio *bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200887
Tejun Heoc5869802011-12-14 00:33:41 +0100888static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
889{
890 /* cic->icq is the first member, %NULL will convert to %NULL */
891 return container_of(icq, struct cfq_io_cq, icq);
892}
893
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100894static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
895 struct io_context *ioc)
896{
897 if (ioc)
898 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
899 return NULL;
900}
901
Tejun Heoc5869802011-12-14 00:33:41 +0100902static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200903{
Jens Axboea6151c32009-10-07 20:02:57 +0200904 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200905}
906
Tejun Heoc5869802011-12-14 00:33:41 +0100907static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
908 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200909{
Jens Axboea6151c32009-10-07 20:02:57 +0200910 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200911}
912
Tejun Heoc5869802011-12-14 00:33:41 +0100913static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400914{
Tejun Heoc5869802011-12-14 00:33:41 +0100915 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400916}
917
Vasily Tarasov91fac312007-04-25 12:29:51 +0200918/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700919 * scheduler run of queue, if there are requests pending and no one in the
920 * driver that will restart queueing
921 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200922static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700923{
Jens Axboe7b679132008-05-30 12:23:07 +0200924 if (cfqd->busy_queues) {
925 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600926 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200927 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100931 * Scale schedule slice based on io priority. Use the sync time slice only
932 * if a queue is marked sync and has sync io queued. A sync queue with async
933 * io only, should not get full sync slice length.
934 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600935static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200936 unsigned short prio)
937{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600938 u64 base_slice = cfqd->cfq_slice[sync];
939 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
Jens Axboed9e76202007-04-20 14:27:50 +0200940
941 WARN_ON(prio >= IOPRIO_BE_NR);
942
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600943 return base_slice + (slice * (4 - prio));
Jens Axboed9e76202007-04-20 14:27:50 +0200944}
945
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600946static inline u64
Jens Axboe44f7c162007-01-19 11:51:58 +1100947cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
948{
Jens Axboed9e76202007-04-20 14:27:50 +0200949 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100950}
951
Tejun Heo1d3650f2013-01-09 08:05:11 -0800952/**
953 * cfqg_scale_charge - scale disk time charge according to cfqg weight
954 * @charge: disk time being charged
955 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
956 *
957 * Scale @charge according to @vfraction, which is in range (0, 1]. The
958 * scaling is inversely proportional.
959 *
960 * scaled = charge / vfraction
961 *
962 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
963 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600964static inline u64 cfqg_scale_charge(u64 charge,
Tejun Heo1d3650f2013-01-09 08:05:11 -0800965 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500966{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800967 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500968
Tejun Heo1d3650f2013-01-09 08:05:11 -0800969 /* charge / vfraction */
970 c <<= CFQ_SERVICE_SHIFT;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600971 return div_u64(c, vfraction);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500972}
973
974static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
975{
976 s64 delta = (s64)(vdisktime - min_vdisktime);
977 if (delta > 0)
978 min_vdisktime = vdisktime;
979
980 return min_vdisktime;
981}
982
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500983static void update_min_vdisktime(struct cfq_rb_root *st)
984{
Davidlohr Bueso09663c82017-09-08 16:15:05 -0700985 if (!RB_EMPTY_ROOT(&st->rb.rb_root)) {
986 struct cfq_group *cfqg = rb_entry_cfqg(st->rb.rb_leftmost);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500987
Gui Jianfenga6032712011-03-07 09:28:09 +0100988 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
989 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500990 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500991}
992
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100993/*
994 * get averaged number of queues of RT/BE priority.
995 * average is updated, with a formula that gives more weight to higher numbers,
996 * to quickly follows sudden increases and decrease slowly
997 */
998
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500999static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1000 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001001{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001002 unsigned min_q, max_q;
1003 unsigned mult = cfq_hist_divisor - 1;
1004 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001005 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001006
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001007 min_q = min(cfqg->busy_queues_avg[rt], busy);
1008 max_q = max(cfqg->busy_queues_avg[rt], busy);
1009 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001010 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001011 return cfqg->busy_queues_avg[rt];
1012}
1013
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001014static inline u64
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001015cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1016{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001017 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001018}
1019
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001020static inline u64
Vivek Goyalba5bd522011-01-19 08:25:02 -07001021cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001022{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001023 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001024 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001025 /*
1026 * interested queues (we consider only the ones with the same
1027 * priority class in the cfq group)
1028 */
1029 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1030 cfq_class_rt(cfqq));
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001031 u64 sync_slice = cfqd->cfq_slice[1];
1032 u64 expect_latency = sync_slice * iq;
1033 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001034
1035 if (expect_latency > group_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001036 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1037 u64 low_slice;
1038
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001039 /* scale low_slice according to IO priority
1040 * and sync vs async */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001041 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1042 low_slice = min(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001043 /* the adapted slice value is scaled to fit all iqs
1044 * into the target latency */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001045 slice = div64_u64(slice*group_slice, expect_latency);
1046 slice = max(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001047 }
1048 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001049 return slice;
1050}
1051
1052static inline void
1053cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1054{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001055 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1056 u64 now = ktime_get_ns();
Shaohua Lic553f8e2011-01-14 08:41:03 +01001057
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001058 cfqq->slice_start = now;
1059 cfqq->slice_end = now + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001060 cfqq->allocated_slice = slice;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001061 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
Jens Axboe44f7c162007-01-19 11:51:58 +11001062}
1063
1064/*
1065 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1066 * isn't valid until the first request from the dispatch is activated
1067 * and the slice time set.
1068 */
Jens Axboea6151c32009-10-07 20:02:57 +02001069static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001070{
1071 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001072 return false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001073 if (ktime_get_ns() < cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01001074 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001075
Shaohua Lic1e44752010-11-08 15:01:02 +01001076 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001077}
1078
1079/*
Jens Axboe5e705372006-07-13 12:39:25 +02001080 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001082 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Jens Axboe5e705372006-07-13 12:39:25 +02001084static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001085cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001087 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001089#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1090#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1091 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jens Axboe5e705372006-07-13 12:39:25 +02001093 if (rq1 == NULL || rq1 == rq2)
1094 return rq2;
1095 if (rq2 == NULL)
1096 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001097
Namhyung Kim229836b2011-05-24 10:23:21 +02001098 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1099 return rq_is_sync(rq1) ? rq1 : rq2;
1100
Christoph Hellwig65299a32011-08-23 14:50:29 +02001101 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1102 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001103
Tejun Heo83096eb2009-05-07 22:24:39 +09001104 s1 = blk_rq_pos(rq1);
1105 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /*
1108 * by definition, 1KiB is 2 sectors
1109 */
1110 back_max = cfqd->cfq_back_max * 2;
1111
1112 /*
1113 * Strict one way elevator _except_ in the case where we allow
1114 * short backward seeks which are biased as twice the cost of a
1115 * similar forward seek.
1116 */
1117 if (s1 >= last)
1118 d1 = s1 - last;
1119 else if (s1 + back_max >= last)
1120 d1 = (last - s1) * cfqd->cfq_back_penalty;
1121 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001122 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (s2 >= last)
1125 d2 = s2 - last;
1126 else if (s2 + back_max >= last)
1127 d2 = (last - s2) * cfqd->cfq_back_penalty;
1128 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001129 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Andreas Mohre8a99052006-03-28 08:59:49 +02001133 /*
1134 * By doing switch() on the bit mask "wrap" we avoid having to
1135 * check two variables for all permutations: --> faster!
1136 */
1137 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001138 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001139 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001140 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001141 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001142 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001143 else {
1144 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001145 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001146 else
Jens Axboe5e705372006-07-13 12:39:25 +02001147 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001148 }
1149
1150 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001151 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001152 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001153 return rq2;
1154 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001155 default:
1156 /*
1157 * Since both rqs are wrapped,
1158 * start with the one that's further behind head
1159 * (--> only *one* back seek required),
1160 * since back seek takes more time than forward.
1161 */
1162 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001163 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 else
Jens Axboe5e705372006-07-13 12:39:25 +02001165 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167}
1168
Jens Axboe08717142008-01-28 11:38:15 +01001169static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001170{
Vivek Goyal615f0252009-12-03 12:59:39 -05001171 /* Service tree is empty */
1172 if (!root->count)
1173 return NULL;
1174
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001175 return rb_entry(rb_first_cached(&root->rb), struct cfq_queue, rb_node);
Jens Axboecc09e292007-04-26 12:53:50 +02001176}
1177
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001178static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1179{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001180 return rb_entry_cfqg(rb_first_cached(&root->rb));
Jens Axboea36e71f2009-04-15 12:15:11 +02001181}
1182
Jens Axboecc09e292007-04-26 12:53:50 +02001183static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1184{
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001185 if (root->rb_rightmost == n)
1186 root->rb_rightmost = rb_prev(n);
1187
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001188 rb_erase_cached(n, &root->rb);
1189 RB_CLEAR_NODE(n);
1190
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001191 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
1195 * would be nice to take fifo expire time into account as well
1196 */
Jens Axboe5e705372006-07-13 12:39:25 +02001197static struct request *
1198cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1199 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Jens Axboe21183b02006-07-13 12:33:14 +02001201 struct rb_node *rbnext = rb_next(&last->rb_node);
1202 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001203 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jens Axboe21183b02006-07-13 12:33:14 +02001205 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001208 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001211 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001212 else {
1213 rbnext = rb_first(&cfqq->sort_list);
1214 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001215 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001218 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001221static u64 cfq_slice_offset(struct cfq_data *cfqd,
1222 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jens Axboed9e76202007-04-20 14:27:50 +02001224 /*
1225 * just an approximation, should be ok.
1226 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001227 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001228 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001229}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001231static inline s64
1232cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1233{
1234 return cfqg->vdisktime - st->min_vdisktime;
1235}
1236
1237static void
1238__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1239{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001240 struct rb_node **node = &st->rb.rb_root.rb_node;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001241 struct rb_node *parent = NULL;
1242 struct cfq_group *__cfqg;
1243 s64 key = cfqg_key(st, cfqg);
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001244 bool leftmost = true, rightmost = true;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001245
1246 while (*node != NULL) {
1247 parent = *node;
1248 __cfqg = rb_entry_cfqg(parent);
1249
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001250 if (key < cfqg_key(st, __cfqg)) {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001251 node = &parent->rb_left;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001252 rightmost = false;
1253 } else {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001254 node = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001255 leftmost = false;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001256 }
1257 }
1258
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001259 if (rightmost)
1260 st->rb_rightmost = &cfqg->rb_node;
1261
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001262 rb_link_node(&cfqg->rb_node, parent, node);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001263 rb_insert_color_cached(&cfqg->rb_node, &st->rb, leftmost);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001264}
1265
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001266/*
1267 * This has to be called only on activation of cfqg
1268 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001269static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001270cfq_update_group_weight(struct cfq_group *cfqg)
1271{
Tejun Heo3381cb82012-04-01 14:38:44 -07001272 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001273 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001274 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001275 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001276}
1277
1278static void
1279cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1280{
1281 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001282
1283 if (cfqg->new_leaf_weight) {
1284 cfqg->leaf_weight = cfqg->new_leaf_weight;
1285 cfqg->new_leaf_weight = 0;
1286 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001287}
1288
1289static void
1290cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1291{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001292 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001293 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001294 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001295 bool propagate;
1296
1297 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001298 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1299
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001300 /*
1301 * Update leaf_weight. We cannot update weight at this point
1302 * because cfqg might already have been activated and is
1303 * contributing its current weight to the parent's child_weight.
1304 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001305 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001306 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001307
1308 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001309 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1310 * entitled to. vfraction is calculated by walking the tree
1311 * towards the root calculating the fraction it has at each level.
1312 * The compounded ratio is how much vfraction @cfqg owns.
1313 *
1314 * Start with the proportion tasks in this cfqg has against active
1315 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001316 */
1317 propagate = !pos->nr_active++;
1318 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001319 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001320
Tejun Heo1d3650f2013-01-09 08:05:11 -08001321 /*
1322 * Compound ->weight walking up the tree. Both activation and
1323 * vfraction calculation are done in the same loop. Propagation
1324 * stops once an already activated node is met. vfraction
1325 * calculation should always continue to the root.
1326 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001327 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001328 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001329 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001330 propagate = !parent->nr_active++;
1331 parent->children_weight += pos->weight;
1332 }
1333 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001334 pos = parent;
1335 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001336
1337 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001338}
1339
Hou Tao5be6b752017-03-01 09:02:33 +08001340static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
1341{
1342 if (!iops_mode(cfqd))
1343 return CFQ_SLICE_MODE_GROUP_DELAY;
1344 else
1345 return CFQ_IOPS_MODE_GROUP_DELAY;
1346}
1347
Justin TerAvest8184f932011-03-17 16:12:36 +01001348static void
1349cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001350{
1351 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1352 struct cfq_group *__cfqg;
1353 struct rb_node *n;
1354
1355 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001356 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001357 return;
1358
1359 /*
1360 * Currently put the group at the end. Later implement something
1361 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001362 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001363 */
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001364 n = st->rb_rightmost;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001365 if (n) {
1366 __cfqg = rb_entry_cfqg(n);
Hou Tao5be6b752017-03-01 09:02:33 +08001367 cfqg->vdisktime = __cfqg->vdisktime +
1368 cfq_get_cfqg_vdisktime_delay(cfqd);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001369 } else
1370 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001371 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001372}
1373
1374static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001375cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1376{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001377 struct cfq_group *pos = cfqg;
1378 bool propagate;
1379
1380 /*
1381 * Undo activation from cfq_group_service_tree_add(). Deactivate
1382 * @cfqg and propagate deactivation upwards.
1383 */
1384 propagate = !--pos->nr_active;
1385 pos->children_weight -= pos->leaf_weight;
1386
1387 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001388 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001389
1390 /* @pos has 0 nr_active at this point */
1391 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001392 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001393
1394 if (!parent)
1395 break;
1396
1397 propagate = !--parent->nr_active;
1398 parent->children_weight -= pos->weight;
1399 pos = parent;
1400 }
1401
1402 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001403 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1404 cfq_rb_erase(&cfqg->rb_node, st);
1405}
1406
1407static void
1408cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001409{
1410 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1411
1412 BUG_ON(cfqg->nr_cfqq < 1);
1413 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001414
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001415 /* If there are other cfq queues under this group, don't delete it */
1416 if (cfqg->nr_cfqq)
1417 return;
1418
Vivek Goyal2868ef72009-12-03 12:59:48 -05001419 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001420 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001421 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001422 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001423}
1424
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001425static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1426 u64 *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001427{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001428 u64 slice_used;
1429 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001430
1431 /*
1432 * Queue got expired before even a single request completed or
1433 * got expired immediately after first request completion.
1434 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001435 if (!cfqq->slice_start || cfqq->slice_start == now) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001436 /*
1437 * Also charge the seek time incurred to the group, otherwise
1438 * if there are mutiple queues in the group, each can dispatch
1439 * a single request on seeky media and cause lots of seek time
1440 * and group will never know it.
1441 */
Jan Kara0b31c102016-06-28 09:04:02 +02001442 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1443 jiffies_to_nsecs(1));
Vivek Goyaldae739e2009-12-03 12:59:45 -05001444 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001445 slice_used = now - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001446 if (slice_used > cfqq->allocated_slice) {
1447 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001448 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001449 }
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001450 if (cfqq->slice_start > cfqq->dispatch_start)
Justin TerAvest167400d2011-03-12 16:54:00 +01001451 *unaccounted_time += cfqq->slice_start -
1452 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001453 }
1454
Vivek Goyaldae739e2009-12-03 12:59:45 -05001455 return slice_used;
1456}
1457
1458static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001459 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001460{
1461 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001462 u64 used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001463 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1464 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001465 unsigned int vfr;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001466 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001467
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001468 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001469 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001470
Vivek Goyal02b35082010-08-23 12:23:53 +02001471 if (iops_mode(cfqd))
1472 charge = cfqq->slice_dispatch;
1473 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1474 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001475
Tejun Heo1d3650f2013-01-09 08:05:11 -08001476 /*
1477 * Can't update vdisktime while on service tree and cfqg->vfraction
1478 * is valid only while on it. Cache vfr, leave the service tree,
1479 * update vdisktime and go back on. The re-addition to the tree
1480 * will also update the weights as necessary.
1481 */
1482 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001483 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001484 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001485 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001486
1487 /* This group is being expired. Save the context */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001488 if (cfqd->workload_expires > now) {
1489 cfqg->saved_wl_slice = cfqd->workload_expires - now;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001490 cfqg->saved_wl_type = cfqd->serving_wl_type;
1491 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001492 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001493 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001494
1495 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1496 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001497 cfq_log_cfqq(cfqq->cfqd, cfqq,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001498 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
Joe Perchesfd16d262011-06-13 10:42:49 +02001499 used_sl, cfqq->slice_dispatch, charge,
1500 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001501 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1502 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001503}
1504
Tejun Heof51b8022012-03-05 13:15:05 -08001505/**
1506 * cfq_init_cfqg_base - initialize base part of a cfq_group
1507 * @cfqg: cfq_group to initialize
1508 *
1509 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1510 * is enabled or not.
1511 */
1512static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1513{
1514 struct cfq_rb_root *st;
1515 int i, j;
1516
1517 for_each_cfqg_st(cfqg, i, j, st)
1518 *st = CFQ_RB_ROOT;
1519 RB_CLEAR_NODE(&cfqg->rb_node);
1520
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001521 cfqg->ttime.last_end_request = ktime_get_ns();
Tejun Heof51b8022012-03-05 13:15:05 -08001522}
1523
Vivek Goyal25fb5162009-12-03 12:59:46 -05001524#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo69d7fde2015-08-18 14:55:36 -07001525static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1526 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1527
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001528static void cfqg_stats_exit(struct cfqg_stats *stats)
Peter Zijlstra90d38392013-11-12 19:42:14 -08001529{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001530 blkg_rwstat_exit(&stats->merged);
1531 blkg_rwstat_exit(&stats->service_time);
1532 blkg_rwstat_exit(&stats->wait_time);
1533 blkg_rwstat_exit(&stats->queued);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001534 blkg_stat_exit(&stats->time);
1535#ifdef CONFIG_DEBUG_BLK_CGROUP
1536 blkg_stat_exit(&stats->unaccounted_time);
1537 blkg_stat_exit(&stats->avg_queue_size_sum);
1538 blkg_stat_exit(&stats->avg_queue_size_samples);
1539 blkg_stat_exit(&stats->dequeue);
1540 blkg_stat_exit(&stats->group_wait_time);
1541 blkg_stat_exit(&stats->idle_time);
1542 blkg_stat_exit(&stats->empty_time);
1543#endif
1544}
1545
1546static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1547{
Tejun Heo77ea7332015-08-18 14:55:24 -07001548 if (blkg_rwstat_init(&stats->merged, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001549 blkg_rwstat_init(&stats->service_time, gfp) ||
1550 blkg_rwstat_init(&stats->wait_time, gfp) ||
1551 blkg_rwstat_init(&stats->queued, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001552 blkg_stat_init(&stats->time, gfp))
1553 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001554
1555#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001556 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1557 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1558 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1559 blkg_stat_init(&stats->dequeue, gfp) ||
1560 blkg_stat_init(&stats->group_wait_time, gfp) ||
1561 blkg_stat_init(&stats->idle_time, gfp) ||
1562 blkg_stat_init(&stats->empty_time, gfp))
1563 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001564#endif
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001565 return 0;
1566err:
1567 cfqg_stats_exit(stats);
1568 return -ENOMEM;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001569}
1570
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001571static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1572{
1573 struct cfq_group_data *cgd;
1574
Tejun Heoebc4ff62016-11-10 11:16:37 -05001575 cgd = kzalloc(sizeof(*cgd), gfp);
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001576 if (!cgd)
1577 return NULL;
1578 return &cgd->cpd;
1579}
1580
Tejun Heo81437642015-08-18 14:55:15 -07001581static void cfq_cpd_init(struct blkcg_policy_data *cpd)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001582{
Tejun Heo81437642015-08-18 14:55:15 -07001583 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001584 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
Tejun Heo69d7fde2015-08-18 14:55:36 -07001585 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001586
Tejun Heo69d7fde2015-08-18 14:55:36 -07001587 if (cpd_to_blkcg(cpd) == &blkcg_root)
1588 weight *= 2;
1589
1590 cgd->weight = weight;
1591 cgd->leaf_weight = weight;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001592}
1593
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001594static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1595{
1596 kfree(cpd_to_cfqgd(cpd));
1597}
1598
Tejun Heo69d7fde2015-08-18 14:55:36 -07001599static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1600{
1601 struct blkcg *blkcg = cpd_to_blkcg(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001602 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
Tejun Heo69d7fde2015-08-18 14:55:36 -07001603 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1604
1605 if (blkcg == &blkcg_root)
1606 weight *= 2;
1607
1608 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1609 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1610}
1611
Tejun Heo001bea72015-08-18 14:55:11 -07001612static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1613{
Tejun Heob2ce2642015-08-18 14:55:13 -07001614 struct cfq_group *cfqg;
1615
1616 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1617 if (!cfqg)
1618 return NULL;
1619
1620 cfq_init_cfqg_base(cfqg);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001621 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1622 kfree(cfqg);
1623 return NULL;
1624 }
Tejun Heob2ce2642015-08-18 14:55:13 -07001625
1626 return &cfqg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -07001627}
1628
Tejun Heoa9520cd2015-08-18 14:55:14 -07001629static void cfq_pd_init(struct blkg_policy_data *pd)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001630{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001631 struct cfq_group *cfqg = pd_to_cfqg(pd);
1632 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001633
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001634 cfqg->weight = cgd->weight;
1635 cfqg->leaf_weight = cgd->leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001636}
1637
Tejun Heoa9520cd2015-08-18 14:55:14 -07001638static void cfq_pd_offline(struct blkg_policy_data *pd)
Tejun Heo0b399202013-01-09 08:05:13 -08001639{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001640 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo60a83702015-08-18 14:55:05 -07001641 int i;
1642
1643 for (i = 0; i < IOPRIO_BE_NR; i++) {
1644 if (cfqg->async_cfqq[0][i])
1645 cfq_put_queue(cfqg->async_cfqq[0][i]);
1646 if (cfqg->async_cfqq[1][i])
1647 cfq_put_queue(cfqg->async_cfqq[1][i]);
1648 }
1649
1650 if (cfqg->async_idle_cfqq)
1651 cfq_put_queue(cfqg->async_idle_cfqq);
1652
Tejun Heo0b399202013-01-09 08:05:13 -08001653 /*
1654 * @blkg is going offline and will be ignored by
1655 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1656 * that they don't get lost. If IOs complete after this point, the
1657 * stats for them will be lost. Oh well...
1658 */
Tejun Heo60a83702015-08-18 14:55:05 -07001659 cfqg_stats_xfer_dead(cfqg);
Tejun Heo0b399202013-01-09 08:05:13 -08001660}
1661
Tejun Heo001bea72015-08-18 14:55:11 -07001662static void cfq_pd_free(struct blkg_policy_data *pd)
1663{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001664 struct cfq_group *cfqg = pd_to_cfqg(pd);
1665
1666 cfqg_stats_exit(&cfqg->stats);
1667 return kfree(cfqg);
Tejun Heo001bea72015-08-18 14:55:11 -07001668}
1669
Tejun Heoa9520cd2015-08-18 14:55:14 -07001670static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo689665a2013-01-09 08:05:13 -08001671{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001672 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo689665a2013-01-09 08:05:13 -08001673
1674 cfqg_stats_reset(&cfqg->stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001675}
1676
Tejun Heoae118892015-08-18 14:55:20 -07001677static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1678 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001679{
Tejun Heoae118892015-08-18 14:55:20 -07001680 struct blkcg_gq *blkg;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001681
Tejun Heoae118892015-08-18 14:55:20 -07001682 blkg = blkg_lookup(blkcg, cfqd->queue);
1683 if (likely(blkg))
1684 return blkg_to_cfqg(blkg);
1685 return NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001686}
1687
1688static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1689{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001690 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001691 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001692 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001693}
1694
Tejun Heof95a04a2012-04-16 13:57:26 -07001695static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1696 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001697{
Tejun Heof95a04a2012-04-16 13:57:26 -07001698 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001699
1700 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001701 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001702 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001703}
1704
Tejun Heo2da8ca82013-12-05 12:28:04 -05001705static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001706{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001707 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1708 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1709 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001710 return 0;
1711}
1712
Tejun Heoe71357e2013-01-09 08:05:10 -08001713static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1714 struct blkg_policy_data *pd, int off)
1715{
1716 struct cfq_group *cfqg = pd_to_cfqg(pd);
1717
1718 if (!cfqg->dev_leaf_weight)
1719 return 0;
1720 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1721}
1722
Tejun Heo2da8ca82013-12-05 12:28:04 -05001723static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001724{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001725 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1726 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1727 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001728 return 0;
1729}
1730
Tejun Heo2da8ca82013-12-05 12:28:04 -05001731static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001732{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001733 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001734 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1735 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001736
Jens Axboe9470e4a2015-06-19 10:19:36 -06001737 if (cgd)
1738 val = cgd->weight;
1739
1740 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001741 return 0;
1742}
1743
Tejun Heo2da8ca82013-12-05 12:28:04 -05001744static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001745{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001746 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001747 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1748 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001749
Jens Axboe9470e4a2015-06-19 10:19:36 -06001750 if (cgd)
1751 val = cgd->leaf_weight;
1752
1753 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001754 return 0;
1755}
1756
Tejun Heo451af502014-05-13 12:16:21 -04001757static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1758 char *buf, size_t nbytes, loff_t off,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001759 bool on_dfl, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001760{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001761 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1762 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo451af502014-05-13 12:16:21 -04001763 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001764 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001765 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001766 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001767 int ret;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001768 u64 v;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001769
Tejun Heo3c798392012-04-16 13:57:25 -07001770 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001771 if (ret)
1772 return ret;
1773
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001774 if (sscanf(ctx.body, "%llu", &v) == 1) {
1775 /* require "default" on dfl */
1776 ret = -ERANGE;
1777 if (!v && on_dfl)
1778 goto out_finish;
1779 } else if (!strcmp(strim(ctx.body), "default")) {
1780 v = 0;
1781 } else {
1782 ret = -EINVAL;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001783 goto out_finish;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001784 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001785
Tejun Heo3381cb82012-04-01 14:38:44 -07001786 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001787 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001788
Tejun Heo20386ce2015-08-18 14:55:28 -07001789 ret = -ERANGE;
Tejun Heo69d7fde2015-08-18 14:55:36 -07001790 if (!v || (v >= min && v <= max)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001791 if (!is_leaf_weight) {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001792 cfqg->dev_weight = v;
1793 cfqg->new_weight = v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001794 } else {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001795 cfqg->dev_leaf_weight = v;
1796 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001797 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001798 ret = 0;
1799 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001800out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001801 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001802 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001803}
1804
Tejun Heo451af502014-05-13 12:16:21 -04001805static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1806 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001807{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001808 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001809}
1810
Tejun Heo451af502014-05-13 12:16:21 -04001811static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1812 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001813{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001814 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001815}
1816
Tejun Heodd165eb2015-08-18 14:55:33 -07001817static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
Tejun Heo69d7fde2015-08-18 14:55:36 -07001818 bool on_dfl, bool reset_dev, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001819{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001820 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1821 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo182446d2013-08-08 20:11:24 -04001822 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001823 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001824 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001825 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001826
Tejun Heo69d7fde2015-08-18 14:55:36 -07001827 if (val < min || val > max)
1828 return -ERANGE;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001829
1830 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001831 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001832 if (!cfqgd) {
1833 ret = -EINVAL;
1834 goto out;
1835 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001836
1837 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001838 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001839 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001840 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001841
Sasha Levinb67bfe02013-02-27 17:06:00 -08001842 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001843 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001844
Tejun Heoe71357e2013-01-09 08:05:10 -08001845 if (!cfqg)
1846 continue;
1847
1848 if (!is_leaf_weight) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001849 if (reset_dev)
1850 cfqg->dev_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001851 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001852 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001853 } else {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001854 if (reset_dev)
1855 cfqg->dev_leaf_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001856 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001857 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001858 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001859 }
1860
Jens Axboeae994ea2015-06-20 10:26:50 -06001861out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001862 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001863 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001864}
1865
Tejun Heo182446d2013-08-08 20:11:24 -04001866static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1867 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001868{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001869 return __cfq_set_weight(css, val, false, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001870}
1871
Tejun Heo182446d2013-08-08 20:11:24 -04001872static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1873 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001874{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001875 return __cfq_set_weight(css, val, false, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001876}
1877
Tejun Heo2da8ca82013-12-05 12:28:04 -05001878static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001879{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001880 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1881 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001882 return 0;
1883}
1884
Tejun Heo2da8ca82013-12-05 12:28:04 -05001885static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001886{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001887 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1888 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001889 return 0;
1890}
1891
Tejun Heo43114012013-01-09 08:05:13 -08001892static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1893 struct blkg_policy_data *pd, int off)
1894{
Tejun Heof12c74c2015-08-18 14:55:23 -07001895 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1896 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001897 return __blkg_prfill_u64(sf, pd, sum);
1898}
1899
1900static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1901 struct blkg_policy_data *pd, int off)
1902{
Tejun Heof12c74c2015-08-18 14:55:23 -07001903 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1904 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001905 return __blkg_prfill_rwstat(sf, pd, &sum);
1906}
1907
Tejun Heo2da8ca82013-12-05 12:28:04 -05001908static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001909{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001910 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1911 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1912 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001913 return 0;
1914}
1915
Tejun Heo2da8ca82013-12-05 12:28:04 -05001916static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001917{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001918 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1919 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1920 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001921 return 0;
1922}
1923
Tejun Heo702747c2015-08-18 14:55:25 -07001924static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1925 int off)
1926{
1927 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1928
1929 return __blkg_prfill_u64(sf, pd, sum >> 9);
1930}
1931
1932static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1933{
1934 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1935 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1936 return 0;
1937}
1938
1939static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
1940 struct blkg_policy_data *pd, int off)
1941{
1942 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
1943 offsetof(struct blkcg_gq, stat_bytes));
1944 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
1945 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
1946
1947 return __blkg_prfill_u64(sf, pd, sum >> 9);
1948}
1949
1950static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1951{
1952 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1953 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
1954 false);
1955 return 0;
1956}
1957
Tejun Heo60c2bc22012-04-01 14:38:43 -07001958#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001959static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1960 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001961{
Tejun Heof95a04a2012-04-16 13:57:26 -07001962 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001963 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001964 u64 v = 0;
1965
1966 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001967 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06001968 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001969 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001970 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001971 return 0;
1972}
1973
1974/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001975static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001976{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001977 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1978 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1979 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001980 return 0;
1981}
1982#endif /* CONFIG_DEBUG_BLK_CGROUP */
1983
Tejun Heo880f50e2015-08-18 14:55:30 -07001984static struct cftype cfq_blkcg_legacy_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001985 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001986 {
1987 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001988 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001989 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04001990 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001991 },
1992 {
1993 .name = "weight",
1994 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001995 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001996 .write_u64 = cfq_set_leaf_weight,
1997 },
1998
1999 /* no such mapping necessary for !roots */
2000 {
2001 .name = "weight_device",
2002 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002003 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002004 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002005 },
2006 {
2007 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08002008 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002009 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07002010 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002011 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08002012
2013 {
2014 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002015 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002016 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08002017 },
2018 {
2019 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002020 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08002021 .write_u64 = cfq_set_leaf_weight,
2022 },
2023
Tejun Heo43114012013-01-09 08:05:13 -08002024 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002025 {
2026 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002027 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002028 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002029 },
2030 {
2031 .name = "sectors",
Tejun Heo702747c2015-08-18 14:55:25 -07002032 .seq_show = cfqg_print_stat_sectors,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002033 },
2034 {
2035 .name = "io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07002036 .private = (unsigned long)&blkcg_policy_cfq,
2037 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002038 },
2039 {
2040 .name = "io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07002041 .private = (unsigned long)&blkcg_policy_cfq,
2042 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002043 },
2044 {
2045 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002046 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002047 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002048 },
2049 {
2050 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002051 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002052 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002053 },
2054 {
2055 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002056 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002057 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002058 },
2059 {
2060 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002061 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002062 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002063 },
Tejun Heo43114012013-01-09 08:05:13 -08002064
2065 /* the same statictics which cover the cfqg and its descendants */
2066 {
2067 .name = "time_recursive",
2068 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002069 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002070 },
2071 {
2072 .name = "sectors_recursive",
Tejun Heo702747c2015-08-18 14:55:25 -07002073 .seq_show = cfqg_print_stat_sectors_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002074 },
2075 {
2076 .name = "io_service_bytes_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002077 .private = (unsigned long)&blkcg_policy_cfq,
2078 .seq_show = blkg_print_stat_bytes_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002079 },
2080 {
2081 .name = "io_serviced_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002082 .private = (unsigned long)&blkcg_policy_cfq,
2083 .seq_show = blkg_print_stat_ios_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002084 },
2085 {
2086 .name = "io_service_time_recursive",
2087 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002088 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002089 },
2090 {
2091 .name = "io_wait_time_recursive",
2092 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002093 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002094 },
2095 {
2096 .name = "io_merged_recursive",
2097 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002098 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002099 },
2100 {
2101 .name = "io_queued_recursive",
2102 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002103 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002104 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002105#ifdef CONFIG_DEBUG_BLK_CGROUP
2106 {
2107 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002108 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002109 },
2110 {
2111 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002112 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002113 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002114 },
2115 {
2116 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002117 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002118 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002119 },
2120 {
2121 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002122 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002123 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002124 },
2125 {
2126 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002127 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002128 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002129 },
2130 {
2131 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002132 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002133 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002134 },
2135#endif /* CONFIG_DEBUG_BLK_CGROUP */
2136 { } /* terminate */
2137};
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002138
2139static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2140{
2141 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2142 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2143
2144 seq_printf(sf, "default %u\n", cgd->weight);
2145 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2146 &blkcg_policy_cfq, 0, false);
2147 return 0;
2148}
2149
2150static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2151 char *buf, size_t nbytes, loff_t off)
2152{
2153 char *endp;
2154 int ret;
2155 u64 v;
2156
2157 buf = strim(buf);
2158
2159 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2160 v = simple_strtoull(buf, &endp, 0);
2161 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07002162 ret = __cfq_set_weight(of_css(of), v, true, false, false);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002163 return ret ?: nbytes;
2164 }
2165
2166 /* "MAJ:MIN WEIGHT" */
2167 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2168}
2169
2170static struct cftype cfq_blkcg_files[] = {
2171 {
2172 .name = "weight",
2173 .flags = CFTYPE_NOT_ON_ROOT,
2174 .seq_show = cfq_print_weight_on_dfl,
2175 .write = cfq_set_weight_on_dfl,
2176 },
2177 { } /* terminate */
2178};
2179
Vivek Goyal25fb5162009-12-03 12:59:46 -05002180#else /* GROUP_IOSCHED */
Tejun Heoae118892015-08-18 14:55:20 -07002181static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2182 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002183{
Tejun Heof51b8022012-03-05 13:15:05 -08002184 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002185}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002186
Vivek Goyal25fb5162009-12-03 12:59:46 -05002187static inline void
2188cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2189 cfqq->cfqg = cfqg;
2190}
2191
2192#endif /* GROUP_IOSCHED */
2193
Jens Axboe498d3aa22007-04-26 12:54:48 +02002194/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002195 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002196 * requests waiting to be processed. It is sorted in the order that
2197 * we will service the queues.
2198 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002199static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002200 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002201{
Jens Axboe08717142008-01-28 11:38:15 +01002202 struct rb_node **p, *parent;
2203 struct cfq_queue *__cfqq;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002204 u64 rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002205 struct cfq_rb_root *st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002206 bool leftmost = true;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002207 int new_cfqq = 1;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002208 u64 now = ktime_get_ns();
Vivek Goyalae30c282009-12-03 12:59:55 -05002209
Vivek Goyal34b98d02012-10-03 16:56:58 -04002210 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002211 if (cfq_class_idle(cfqq)) {
2212 rb_key = CFQ_IDLE_DELAY;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07002213 parent = st->rb_rightmost;
Jens Axboe08717142008-01-28 11:38:15 +01002214 if (parent && parent != &cfqq->rb_node) {
2215 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2216 rb_key += __cfqq->rb_key;
2217 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002218 rb_key += now;
Jens Axboe08717142008-01-28 11:38:15 +01002219 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002220 /*
2221 * Get our rb key offset. Subtract any residual slice
2222 * value carried from last service. A negative resid
2223 * count indicates slice overrun, and this should position
2224 * the next service time further away in the tree.
2225 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002226 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
Jens Axboeb9c89462009-10-06 20:53:44 +02002227 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002228 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002229 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002230 rb_key = -NSEC_PER_SEC;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002231 __cfqq = cfq_rb_first(st);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002232 rb_key += __cfqq ? __cfqq->rb_key : now;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002233 }
Jens Axboed9e76202007-04-20 14:27:50 +02002234
2235 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002236 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002237 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002238 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002239 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002240 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002241 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002242
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002243 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2244 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002245 }
Jens Axboed9e76202007-04-20 14:27:50 +02002246
Jens Axboe08717142008-01-28 11:38:15 +01002247 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002248 cfqq->service_tree = st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002249 p = &st->rb.rb_root.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002250 while (*p) {
2251 parent = *p;
2252 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2253
Jens Axboe0c534e02007-04-18 20:01:57 +02002254 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002255 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002256 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002257 if (rb_key < __cfqq->rb_key)
Vivek Goyal1f23f122012-10-03 16:57:00 -04002258 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002259 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002260 p = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002261 leftmost = false;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002262 }
Jens Axboed9e76202007-04-20 14:27:50 +02002263 }
2264
2265 cfqq->rb_key = rb_key;
2266 rb_link_node(&cfqq->rb_node, parent, p);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002267 rb_insert_color_cached(&cfqq->rb_node, &st->rb, leftmost);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002268 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002269 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002270 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002271 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Jens Axboea36e71f2009-04-15 12:15:11 +02002274static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002275cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2276 sector_t sector, struct rb_node **ret_parent,
2277 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002278{
Jens Axboea36e71f2009-04-15 12:15:11 +02002279 struct rb_node **p, *parent;
2280 struct cfq_queue *cfqq = NULL;
2281
2282 parent = NULL;
2283 p = &root->rb_node;
2284 while (*p) {
2285 struct rb_node **n;
2286
2287 parent = *p;
2288 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2289
2290 /*
2291 * Sort strictly based on sector. Smallest to the left,
2292 * largest to the right.
2293 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002294 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002295 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002296 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002297 n = &(*p)->rb_left;
2298 else
2299 break;
2300 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002301 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002302 }
2303
2304 *ret_parent = parent;
2305 if (rb_link)
2306 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002307 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002308}
2309
2310static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2311{
Jens Axboea36e71f2009-04-15 12:15:11 +02002312 struct rb_node **p, *parent;
2313 struct cfq_queue *__cfqq;
2314
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002315 if (cfqq->p_root) {
2316 rb_erase(&cfqq->p_node, cfqq->p_root);
2317 cfqq->p_root = NULL;
2318 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002319
2320 if (cfq_class_idle(cfqq))
2321 return;
2322 if (!cfqq->next_rq)
2323 return;
2324
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002325 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002326 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2327 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002328 if (!__cfqq) {
2329 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002330 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2331 } else
2332 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002333}
2334
Jens Axboe498d3aa22007-04-26 12:54:48 +02002335/*
2336 * Update cfqq's position in the service tree.
2337 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002338static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002339{
Jens Axboe6d048f52007-04-25 12:44:27 +02002340 /*
2341 * Resorting requires the cfqq to be on the RR list already.
2342 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002343 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002344 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002345 cfq_prio_tree_add(cfqd, cfqq);
2346 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002347}
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/*
2350 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002351 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 */
Jens Axboefebffd62008-01-28 13:19:43 +01002353static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Jens Axboe7b679132008-05-30 12:23:07 +02002355 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002356 BUG_ON(cfq_cfqq_on_rr(cfqq));
2357 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002359 if (cfq_cfqq_sync(cfqq))
2360 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jens Axboeedd75ff2007-04-19 12:03:34 +02002362 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Jens Axboe498d3aa22007-04-26 12:54:48 +02002365/*
2366 * Called when the cfqq no longer has requests pending, remove it from
2367 * the service tree.
2368 */
Jens Axboefebffd62008-01-28 13:19:43 +01002369static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Jens Axboe7b679132008-05-30 12:23:07 +02002371 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002372 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2373 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002375 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2376 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2377 cfqq->service_tree = NULL;
2378 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002379 if (cfqq->p_root) {
2380 rb_erase(&cfqq->p_node, cfqq->p_root);
2381 cfqq->p_root = NULL;
2382 }
Jens Axboed9e76202007-04-20 14:27:50 +02002383
Justin TerAvest8184f932011-03-17 16:12:36 +01002384 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 BUG_ON(!cfqd->busy_queues);
2386 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002387 if (cfq_cfqq_sync(cfqq))
2388 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
2391/*
2392 * rb tree support functions
2393 */
Jens Axboefebffd62008-01-28 13:19:43 +01002394static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Jens Axboe5e705372006-07-13 12:39:25 +02002396 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002397 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Jens Axboeb4878f22005-10-20 16:42:29 +02002399 BUG_ON(!cfqq->queued[sync]);
2400 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Jens Axboe5e705372006-07-13 12:39:25 +02002402 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Vivek Goyalf04a6422009-12-03 12:59:40 -05002404 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2405 /*
2406 * Queue will be deleted from service tree when we actually
2407 * expire it later. Right now just remove it from prio tree
2408 * as it is empty.
2409 */
2410 if (cfqq->p_root) {
2411 rb_erase(&cfqq->p_node, cfqq->p_root);
2412 cfqq->p_root = NULL;
2413 }
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Jens Axboe5e705372006-07-13 12:39:25 +02002417static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Jens Axboe5e705372006-07-13 12:39:25 +02002419 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002421 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jens Axboe5380a102006-07-13 12:37:56 +02002423 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Jeff Moyer796d5112011-06-02 21:19:05 +02002425 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002426
2427 if (!cfq_cfqq_on_rr(cfqq))
2428 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002429
2430 /*
2431 * check if this request is a better next-serve candidate
2432 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002433 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002434 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002435
2436 /*
2437 * adjust priority tree position, if ->next_rq changes
2438 */
2439 if (prev != cfqq->next_rq)
2440 cfq_prio_tree_add(cfqd, cfqq);
2441
Jens Axboe5044eed2007-04-25 11:53:48 +02002442 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Jens Axboefebffd62008-01-28 13:19:43 +01002445static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Jens Axboe5380a102006-07-13 12:37:56 +02002447 elv_rb_del(&cfqq->sort_list, rq);
2448 cfqq->queued[rq_is_sync(rq)]--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002449 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002450 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002451 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002452 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Jens Axboe206dc692006-03-28 13:03:44 +02002455static struct request *
2456cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Jens Axboe206dc692006-03-28 13:03:44 +02002458 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002459 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002460 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Jens Axboe4ac845a2008-01-24 08:44:49 +01002462 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002463 if (!cic)
2464 return NULL;
2465
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002466 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002467 if (cfqq)
2468 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 return NULL;
2471}
2472
Jens Axboe165125e2007-07-24 09:28:11 +02002473static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002474{
2475 struct cfq_data *cfqd = q->elevator->elevator_data;
2476
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002477 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002478 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002479 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002480
Tejun Heo5b936292009-05-07 22:24:38 +09002481 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002482}
2483
Jens Axboe165125e2007-07-24 09:28:11 +02002484static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Jens Axboe22e2c502005-06-27 10:55:12 +02002486 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002488 WARN_ON(!cfqd->rq_in_driver);
2489 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002490 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002491 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Jens Axboeb4878f22005-10-20 16:42:29 +02002494static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
Jens Axboe5e705372006-07-13 12:39:25 +02002496 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002497
Jens Axboe5e705372006-07-13 12:39:25 +02002498 if (cfqq->next_rq == rq)
2499 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Jens Axboeb4878f22005-10-20 16:42:29 +02002501 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002502 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002503
Aaron Carroll45333d52008-08-26 15:52:36 +02002504 cfqq->cfqd->rq_queued--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002505 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002506 if (rq->cmd_flags & REQ_PRIO) {
2507 WARN_ON(!cfqq->prio_pending);
2508 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002512static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
Jens Axboe165125e2007-07-24 09:28:11 +02002513 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct cfq_data *cfqd = q->elevator->elevator_data;
2516 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Jens Axboe206dc692006-03-28 13:03:44 +02002518 __rq = cfq_find_rq_fmerge(cfqd, bio);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002519 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002520 *req = __rq;
2521 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523
2524 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Jens Axboe165125e2007-07-24 09:28:11 +02002527static void cfq_merged_request(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002528 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Jens Axboe21183b02006-07-13 12:33:14 +02002530 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002531 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Jens Axboe5e705372006-07-13 12:39:25 +02002533 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Divyesh Shah812d4022010-04-08 21:14:23 -07002537static void cfq_bio_merged(struct request_queue *q, struct request *req,
2538 struct bio *bio)
2539{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002540 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
Divyesh Shah812d4022010-04-08 21:14:23 -07002541}
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543static void
Jens Axboe165125e2007-07-24 09:28:11 +02002544cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct request *next)
2546{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002547 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002548 struct cfq_data *cfqd = q->elevator->elevator_data;
2549
Jens Axboe22e2c502005-06-27 10:55:12 +02002550 /*
2551 * reposition in fifo if next is older than rq
2552 */
2553 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002554 next->fifo_time < rq->fifo_time &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002555 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002556 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002557 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002558 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002559
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002560 if (cfqq->next_rq == next)
2561 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002562 cfq_remove_request(next);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002563 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002564
2565 cfqq = RQ_CFQQ(next);
2566 /*
2567 * all requests of this queue are merged to other queues, delete it
2568 * from the service tree. If it's the active_queue,
2569 * cfq_dispatch_requests() will choose to expire it or do idle
2570 */
2571 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2572 cfqq != cfqd->active_queue)
2573 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002574}
2575
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002576static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2577 struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +01002578{
2579 struct cfq_data *cfqd = q->elevator->elevator_data;
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002580 bool is_sync = op_is_sync(bio->bi_opf);
Tejun Heoc5869802011-12-14 00:33:41 +01002581 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002582 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002583
2584 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002585 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002586 */
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002587 if (is_sync && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002588 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002589
2590 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002591 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002592 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002593 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002594 cic = cfq_cic_lookup(cfqd, current->io_context);
2595 if (!cic)
2596 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002597
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002598 cfqq = cic_to_cfqq(cic, is_sync);
Jens Axboea6151c32009-10-07 20:02:57 +02002599 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002600}
2601
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002602static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2603 struct request *next)
2604{
2605 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2606}
2607
Divyesh Shah812df482010-04-08 21:15:35 -07002608static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2609{
Jan Kara91148322016-06-08 15:11:39 +02002610 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002611 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002612}
2613
Jens Axboefebffd62008-01-28 13:19:43 +01002614static void __cfq_set_active_queue(struct cfq_data *cfqd,
2615 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002616{
2617 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002618 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002619 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002620 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002621 cfqq->slice_start = 0;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002622 cfqq->dispatch_start = ktime_get_ns();
Justin TerAvest62a37f62011-03-23 08:25:44 +01002623 cfqq->allocated_slice = 0;
2624 cfqq->slice_end = 0;
2625 cfqq->slice_dispatch = 0;
2626 cfqq->nr_sectors = 0;
2627
2628 cfq_clear_cfqq_wait_request(cfqq);
2629 cfq_clear_cfqq_must_dispatch(cfqq);
2630 cfq_clear_cfqq_must_alloc_slice(cfqq);
2631 cfq_clear_cfqq_fifo_expire(cfqq);
2632 cfq_mark_cfqq_slice_new(cfqq);
2633
2634 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002635 }
2636
2637 cfqd->active_queue = cfqq;
2638}
2639
2640/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002641 * current cfqq expired its slice (or was too idle), select new one
2642 */
2643static void
2644__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002645 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002646{
Jens Axboe7b679132008-05-30 12:23:07 +02002647 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2648
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002649 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002650 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002651
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002652 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002653 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002654
2655 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002656 * If this cfqq is shared between multiple processes, check to
2657 * make sure that those processes are still issuing I/Os within
2658 * the mean seek distance. If not, it may be time to break the
2659 * queues apart again.
2660 */
2661 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2662 cfq_mark_cfqq_split_coop(cfqq);
2663
2664 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002665 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002666 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002667 if (timed_out) {
2668 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002669 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002670 else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002671 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
Jan Kara93fdf142016-06-28 09:04:00 +02002672 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
Jens Axboe7b679132008-05-30 12:23:07 +02002673 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002674
Vivek Goyale5ff0822010-04-26 19:25:11 +02002675 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002676
Vivek Goyalf04a6422009-12-03 12:59:40 -05002677 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2678 cfq_del_cfqq_rr(cfqd, cfqq);
2679
Jens Axboeedd75ff2007-04-19 12:03:34 +02002680 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002681
2682 if (cfqq == cfqd->active_queue)
2683 cfqd->active_queue = NULL;
2684
2685 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002686 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002687 cfqd->active_cic = NULL;
2688 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002689}
2690
Vivek Goyale5ff0822010-04-26 19:25:11 +02002691static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002692{
2693 struct cfq_queue *cfqq = cfqd->active_queue;
2694
2695 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002696 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002697}
2698
Jens Axboe498d3aa22007-04-26 12:54:48 +02002699/*
2700 * Get next queue for service. Unless we have a queue preemption,
2701 * we'll simply select the first cfqq in the service tree.
2702 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002703static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002704{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002705 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2706 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002707
Vivek Goyalf04a6422009-12-03 12:59:40 -05002708 if (!cfqd->rq_queued)
2709 return NULL;
2710
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002711 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002712 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002713 return NULL;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002714 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002715 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002716 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002717}
2718
Vivek Goyalf04a6422009-12-03 12:59:40 -05002719static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2720{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002721 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002722 struct cfq_queue *cfqq;
2723 int i, j;
2724 struct cfq_rb_root *st;
2725
2726 if (!cfqd->rq_queued)
2727 return NULL;
2728
Vivek Goyal25fb5162009-12-03 12:59:46 -05002729 cfqg = cfq_get_next_cfqg(cfqd);
2730 if (!cfqg)
2731 return NULL;
2732
Markus Elfring1cf41752017-01-21 22:44:07 +01002733 for_each_cfqg_st(cfqg, i, j, st) {
2734 cfqq = cfq_rb_first(st);
2735 if (cfqq)
Vivek Goyalf04a6422009-12-03 12:59:40 -05002736 return cfqq;
Markus Elfring1cf41752017-01-21 22:44:07 +01002737 }
Vivek Goyalf04a6422009-12-03 12:59:40 -05002738 return NULL;
2739}
2740
Jens Axboe498d3aa22007-04-26 12:54:48 +02002741/*
2742 * Get and set a new active queue for service.
2743 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002744static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2745 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002746{
Jens Axboee00ef792009-11-04 08:54:55 +01002747 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002748 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002749
Jens Axboe22e2c502005-06-27 10:55:12 +02002750 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002751 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002752}
2753
Jens Axboed9e76202007-04-20 14:27:50 +02002754static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2755 struct request *rq)
2756{
Tejun Heo83096eb2009-05-07 22:24:39 +09002757 if (blk_rq_pos(rq) >= cfqd->last_position)
2758 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002759 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002760 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002761}
2762
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002763static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002764 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002765{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002766 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002767}
2768
Jens Axboea36e71f2009-04-15 12:15:11 +02002769static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2770 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002771{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002772 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002773 struct rb_node *parent, *node;
2774 struct cfq_queue *__cfqq;
2775 sector_t sector = cfqd->last_position;
2776
2777 if (RB_EMPTY_ROOT(root))
2778 return NULL;
2779
2780 /*
2781 * First, if we find a request starting at the end of the last
2782 * request, choose it.
2783 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002784 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002785 if (__cfqq)
2786 return __cfqq;
2787
2788 /*
2789 * If the exact sector wasn't found, the parent of the NULL leaf
2790 * will contain the closest sector.
2791 */
2792 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002793 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002794 return __cfqq;
2795
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002796 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002797 node = rb_next(&__cfqq->p_node);
2798 else
2799 node = rb_prev(&__cfqq->p_node);
2800 if (!node)
2801 return NULL;
2802
2803 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002804 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002805 return __cfqq;
2806
2807 return NULL;
2808}
2809
2810/*
2811 * cfqd - obvious
2812 * cur_cfqq - passed in so that we don't decide that the current queue is
2813 * closely cooperating with itself.
2814 *
2815 * So, basically we're assuming that that cur_cfqq has dispatched at least
2816 * one request, and that cfqd->last_position reflects a position on the disk
2817 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2818 * assumption.
2819 */
2820static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002821 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002822{
2823 struct cfq_queue *cfqq;
2824
Divyesh Shah39c01b22010-03-25 15:45:57 +01002825 if (cfq_class_idle(cur_cfqq))
2826 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002827 if (!cfq_cfqq_sync(cur_cfqq))
2828 return NULL;
2829 if (CFQQ_SEEKY(cur_cfqq))
2830 return NULL;
2831
Jens Axboea36e71f2009-04-15 12:15:11 +02002832 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002833 * Don't search priority tree if it's the only queue in the group.
2834 */
2835 if (cur_cfqq->cfqg->nr_cfqq == 1)
2836 return NULL;
2837
2838 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002839 * We should notice if some of the queues are cooperating, eg
2840 * working closely on the same area of the disk. In that case,
2841 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002842 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002843 cfqq = cfqq_close(cfqd, cur_cfqq);
2844 if (!cfqq)
2845 return NULL;
2846
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002847 /* If new queue belongs to different cfq_group, don't choose it */
2848 if (cur_cfqq->cfqg != cfqq->cfqg)
2849 return NULL;
2850
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002851 /*
2852 * It only makes sense to merge sync queues.
2853 */
2854 if (!cfq_cfqq_sync(cfqq))
2855 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002856 if (CFQQ_SEEKY(cfqq))
2857 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002858
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002859 /*
2860 * Do not merge queues of different priority classes
2861 */
2862 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2863 return NULL;
2864
Jens Axboea36e71f2009-04-15 12:15:11 +02002865 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002866}
2867
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002868/*
2869 * Determine whether we should enforce idle window for this queue.
2870 */
2871
2872static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2873{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002874 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002875 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002876
Vivek Goyal34b98d02012-10-03 16:56:58 -04002877 BUG_ON(!st);
2878 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002879
Vivek Goyalb6508c12010-08-23 12:23:33 +02002880 if (!cfqd->cfq_slice_idle)
2881 return false;
2882
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002883 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002884 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002885 return false;
2886
2887 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002888 if (cfq_cfqq_idle_window(cfqq) &&
2889 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002890 return true;
2891
2892 /*
2893 * Otherwise, we do only if they are the last ones
2894 * in their service tree.
2895 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002896 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2897 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002898 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002899 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002900 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002901}
2902
Jens Axboe6d048f52007-04-25 12:44:27 +02002903static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002904{
Jens Axboe17926692007-01-19 11:59:30 +11002905 struct cfq_queue *cfqq = cfqd->active_queue;
Jan Karae7954212016-01-12 16:24:15 +01002906 struct cfq_rb_root *st = cfqq->service_tree;
Tejun Heoc5869802011-12-14 00:33:41 +01002907 struct cfq_io_cq *cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002908 u64 sl, group_idle = 0;
2909 u64 now = ktime_get_ns();
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002910
Jens Axboea68bbddba2008-09-24 13:03:33 +02002911 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002912 * SSD device without seek penalty, disable idling. But only do so
2913 * for devices that support queuing, otherwise we still have a problem
2914 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002915 */
Ritesh Harjanib3193bc2017-08-09 18:28:32 +05302916 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag &&
2917 !cfqd->cfq_group_idle)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002918 return;
2919
Jens Axboedd67d052006-06-21 09:36:18 +02002920 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002921 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002922
2923 /*
2924 * idle is disabled, either manually or by past process history
2925 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002926 if (!cfq_should_idle(cfqd, cfqq)) {
2927 /* no queue idling. Check for group idling */
2928 if (cfqd->cfq_group_idle)
2929 group_idle = cfqd->cfq_group_idle;
2930 else
2931 return;
2932 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002933
Jens Axboe22e2c502005-06-27 10:55:12 +02002934 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002935 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002936 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002937 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002938 return;
2939
2940 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002941 * task has exited, don't wait
2942 */
Jens Axboe206dc692006-03-28 13:03:44 +02002943 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002944 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002945 return;
2946
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002947 /*
2948 * If our average think time is larger than the remaining time
2949 * slice, then don't idle. This avoids overrunning the allotted
2950 * time slice.
2951 */
Shaohua Li383cd722011-07-12 14:24:35 +02002952 if (sample_valid(cic->ttime.ttime_samples) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002953 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2954 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
Shaohua Li383cd722011-07-12 14:24:35 +02002955 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002956 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002957 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002958
Jan Karae7954212016-01-12 16:24:15 +01002959 /*
2960 * There are other queues in the group or this is the only group and
2961 * it has too big thinktime, don't do group idle.
2962 */
2963 if (group_idle &&
2964 (cfqq->cfqg->nr_cfqq > 1 ||
2965 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002966 return;
2967
Jens Axboe3b181522005-06-27 10:56:24 +02002968 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002969
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002970 if (group_idle)
2971 sl = cfqd->cfq_group_idle;
2972 else
2973 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002974
Jan Kara91148322016-06-08 15:11:39 +02002975 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2976 HRTIMER_MODE_REL);
Tejun Heo155fead2012-04-01 14:38:44 -07002977 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002978 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002979 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
Jens Axboe498d3aa22007-04-26 12:54:48 +02002982/*
2983 * Move request from internal lists to the request queue dispatch list.
2984 */
Jens Axboe165125e2007-07-24 09:28:11 +02002985static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002987 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002988 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002989
Jens Axboe7b679132008-05-30 12:23:07 +02002990 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2991
Jeff Moyer06d21882009-09-11 17:08:59 +02002992 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002993 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002994 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002995 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002996 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02002997
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002998 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02002999 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000}
3001
3002/*
3003 * return expired entry, or NULL to just start from scratch in rbtree
3004 */
Jens Axboefebffd62008-01-28 13:19:43 +01003005static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
Jens Axboe30996f42009-10-05 11:03:39 +02003007 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Jens Axboe3b181522005-06-27 10:56:24 +02003009 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11003011
3012 cfq_mark_cfqq_fifo_expire(cfqq);
3013
Jens Axboe89850f72006-07-22 16:48:31 +02003014 if (list_empty(&cfqq->fifo))
3015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Jens Axboe89850f72006-07-22 16:48:31 +02003017 rq = rq_entry_fifo(cfqq->fifo.next);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003018 if (ktime_get_ns() < rq->fifo_time)
Jens Axboe7b679132008-05-30 12:23:07 +02003019 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Jens Axboe6d048f52007-04-25 12:44:27 +02003021 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Jens Axboe22e2c502005-06-27 10:55:12 +02003024static inline int
3025cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3026{
3027 const int base_rq = cfqd->cfq_slice_async_rq;
3028
3029 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3030
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02003031 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003032}
3033
3034/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003035 * Must be called with the queue_lock held.
3036 */
3037static int cfqq_process_refs(struct cfq_queue *cfqq)
3038{
3039 int process_refs, io_refs;
3040
3041 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01003042 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003043 BUG_ON(process_refs < 0);
3044 return process_refs;
3045}
3046
3047static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3048{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003049 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003050 struct cfq_queue *__cfqq;
3051
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003052 /*
3053 * If there are no process references on the new_cfqq, then it is
3054 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3055 * chain may have dropped their last reference (not just their
3056 * last process reference).
3057 */
3058 if (!cfqq_process_refs(new_cfqq))
3059 return;
3060
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003061 /* Avoid a circular list and skip interim queue merges */
3062 while ((__cfqq = new_cfqq->new_cfqq)) {
3063 if (__cfqq == cfqq)
3064 return;
3065 new_cfqq = __cfqq;
3066 }
3067
3068 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003069 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003070 /*
3071 * If the process for the cfqq has gone away, there is no
3072 * sense in merging the queues.
3073 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003074 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003075 return;
3076
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003077 /*
3078 * Merge in the direction of the lesser amount of work.
3079 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003080 if (new_process_refs >= process_refs) {
3081 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003082 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003083 } else {
3084 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003085 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003086 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003087}
3088
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003089static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003090 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003091{
3092 struct cfq_queue *queue;
3093 int i;
3094 bool key_valid = false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003095 u64 lowest_key = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003096 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3097
Vivek Goyal65b32a52009-12-16 17:52:59 -05003098 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3099 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003100 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003101 if (queue &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003102 (!key_valid || queue->rb_key < lowest_key)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003103 lowest_key = queue->rb_key;
3104 cur_best = i;
3105 key_valid = true;
3106 }
3107 }
3108
3109 return cur_best;
3110}
3111
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003112static void
3113choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003114{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003115 u64 slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003116 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003117 struct cfq_rb_root *st;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003118 u64 group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003119 enum wl_class_t original_class = cfqd->serving_wl_class;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003120 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003121
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003122 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003123 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003124 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003125 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003126 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003127 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003128 cfqd->serving_wl_class = IDLE_WORKLOAD;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003129 cfqd->workload_expires = now + jiffies_to_nsecs(1);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003130 return;
3131 }
3132
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003133 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003134 goto new_workload;
3135
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003136 /*
3137 * For RT and BE, we have to choose also the type
3138 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3139 * expiration time
3140 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003141 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003142 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003143
3144 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003145 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003146 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003147 if (count && !(now > cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003148 return;
3149
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003150new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003151 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003152 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003153 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003154 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003155 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003156
3157 /*
3158 * the workload slice is computed as a fraction of target latency
3159 * proportional to the number of queues in that workload, over
3160 * all the queues in the same priority class
3161 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003162 group_slice = cfq_group_slice(cfqd, cfqg);
3163
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003164 slice = div_u64(group_slice * count,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003165 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3166 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003167 cfqg)));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003168
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003169 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003170 u64 tmp;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003171
3172 /*
3173 * Async queues are currently system wide. Just taking
3174 * proportion of queues with-in same group will lead to higher
3175 * async ratio system wide as generally root group is going
3176 * to have higher weight. A more accurate thing would be to
3177 * calculate system wide asnc/sync ratio.
3178 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003179 tmp = cfqd->cfq_target_latency *
3180 cfqg_busy_async_queues(cfqd, cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003181 tmp = div_u64(tmp, cfqd->busy_queues);
3182 slice = min_t(u64, slice, tmp);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003183
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003184 /* async workload slice is scaled down according to
3185 * the sync/async slice ratio. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003186 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003187 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003188 /* sync workload slice is at least 2 * cfq_slice_idle */
3189 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3190
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003191 slice = max_t(u64, slice, CFQ_MIN_TT);
3192 cfq_log(cfqd, "workload slice:%llu", slice);
3193 cfqd->workload_expires = now + slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003194}
3195
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003196static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3197{
3198 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003199 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003200
Davidlohr Bueso09663c82017-09-08 16:15:05 -07003201 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003202 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003203 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003204 update_min_vdisktime(st);
3205 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003206}
3207
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003208static void cfq_choose_cfqg(struct cfq_data *cfqd)
3209{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003210 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003211 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003212
3213 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003214
3215 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003216 if (cfqg->saved_wl_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003217 cfqd->workload_expires = now + cfqg->saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003218 cfqd->serving_wl_type = cfqg->saved_wl_type;
3219 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003220 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003221 cfqd->workload_expires = now - 1;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003222
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003223 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003224}
3225
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003226/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003227 * Select a queue for service. If we have a current active queue,
3228 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003229 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003230static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003231{
Jens Axboea36e71f2009-04-15 12:15:11 +02003232 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003233 u64 now = ktime_get_ns();
Jens Axboe22e2c502005-06-27 10:55:12 +02003234
3235 cfqq = cfqd->active_queue;
3236 if (!cfqq)
3237 goto new_queue;
3238
Vivek Goyalf04a6422009-12-03 12:59:40 -05003239 if (!cfqd->rq_queued)
3240 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003241
3242 /*
3243 * We were waiting for group to get backlogged. Expire the queue
3244 */
3245 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3246 goto expire;
3247
Jens Axboe22e2c502005-06-27 10:55:12 +02003248 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003249 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003250 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003251 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3252 /*
3253 * If slice had not expired at the completion of last request
3254 * we might not have turned on wait_busy flag. Don't expire
3255 * the queue yet. Allow the group to get backlogged.
3256 *
3257 * The very fact that we have used the slice, that means we
3258 * have been idling all along on this queue and it should be
3259 * ok to wait for this request to complete.
3260 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003261 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3262 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3263 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003264 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003265 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003266 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003267 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003268
3269 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003270 * The active queue has requests and isn't expired, allow it to
3271 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003272 */
Jens Axboedd67d052006-06-21 09:36:18 +02003273 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003274 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003275
3276 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003277 * If another queue has a request waiting within our mean seek
3278 * distance, let it run. The expire code will check for close
3279 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003280 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003281 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003282 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003283 if (new_cfqq) {
3284 if (!cfqq->new_cfqq)
3285 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003286 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003287 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003288
3289 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003290 * No requests pending. If the active queue still has requests in
3291 * flight or is idling for a new request, allow either of these
3292 * conditions to happen (or time out) before selecting a new queue.
3293 */
Jan Kara91148322016-06-08 15:11:39 +02003294 if (hrtimer_active(&cfqd->idle_slice_timer)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003295 cfqq = NULL;
3296 goto keep_queue;
3297 }
3298
Shaohua Li8e1ac662010-11-08 15:01:04 +01003299 /*
3300 * This is a deep seek queue, but the device is much faster than
3301 * the queue can deliver, don't idle
3302 **/
3303 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3304 (cfq_cfqq_slice_new(cfqq) ||
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003305 (cfqq->slice_end - now > now - cfqq->slice_start))) {
Shaohua Li8e1ac662010-11-08 15:01:04 +01003306 cfq_clear_cfqq_deep(cfqq);
3307 cfq_clear_cfqq_idle_window(cfqq);
3308 }
3309
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003310 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3311 cfqq = NULL;
3312 goto keep_queue;
3313 }
3314
3315 /*
3316 * If group idle is enabled and there are requests dispatched from
3317 * this group, wait for requests to complete.
3318 */
3319check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02003320 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3321 cfqq->cfqg->dispatched &&
3322 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003323 cfqq = NULL;
3324 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003325 }
3326
Jens Axboe3b181522005-06-27 10:56:24 +02003327expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003328 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003329new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003330 /*
3331 * Current queue expired. Check if we have to switch to a new
3332 * service tree
3333 */
3334 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003335 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003336
Jens Axboea36e71f2009-04-15 12:15:11 +02003337 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003338keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003339 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003340}
3341
Jens Axboefebffd62008-01-28 13:19:43 +01003342static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003343{
3344 int dispatched = 0;
3345
3346 while (cfqq->next_rq) {
3347 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3348 dispatched++;
3349 }
3350
3351 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003352
3353 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003354 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003355 return dispatched;
3356}
3357
Jens Axboe498d3aa22007-04-26 12:54:48 +02003358/*
3359 * Drain our current requests. Used for barriers and when switching
3360 * io schedulers on-the-fly.
3361 */
Jens Axboed9e76202007-04-20 14:27:50 +02003362static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003363{
Jens Axboe08717142008-01-28 11:38:15 +01003364 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003365 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003366
Divyesh Shah3440c492010-04-09 09:29:57 +02003367 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003368 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003369 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3370 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003371 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003372 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003373
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003374 BUG_ON(cfqd->busy_queues);
3375
Jeff Moyer69237152009-06-12 15:29:30 +02003376 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003377 return dispatched;
3378}
3379
Shaohua Liabc3c742010-03-01 09:20:54 +01003380static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3381 struct cfq_queue *cfqq)
3382{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003383 u64 now = ktime_get_ns();
3384
Shaohua Liabc3c742010-03-01 09:20:54 +01003385 /* the queue hasn't finished any request, can't estimate */
3386 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003387 return true;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003388 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01003389 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003390
Shaohua Lic1e44752010-11-08 15:01:02 +01003391 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003392}
3393
Jens Axboe0b182d62009-10-06 20:49:37 +02003394static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003395{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003396 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Glauber Costa3932a862016-09-22 20:59:59 -04003398 if (cfq_cfqq_must_dispatch(cfqq))
3399 return true;
3400
Jens Axboe2f5cb732009-04-07 08:51:19 +02003401 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003402 * Drain async requests before we start sync IO
3403 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003404 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003405 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003406
3407 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003408 * If this is an async queue and we have sync IO in flight, let it wait
3409 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003410 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003411 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003412
Shaohua Liabc3c742010-03-01 09:20:54 +01003413 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003414 if (cfq_class_idle(cfqq))
3415 max_dispatch = 1;
3416
3417 /*
3418 * Does this cfqq already have too much IO in flight?
3419 */
3420 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003421 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003422 /*
3423 * idle queue must always only have a single IO in flight
3424 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003425 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003426 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003427
Jens Axboe2f5cb732009-04-07 08:51:19 +02003428 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003429 * If there is only one sync queue
3430 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003431 * queue no dispatch limit. The reason is a sync queue can
3432 * preempt async queue, limiting the sync queue doesn't make
3433 * sense. This is useful for aiostress test.
3434 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003435 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3436 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003437
3438 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003439 * We have other queues, don't allow more IO from this one
3440 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003441 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3442 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003443 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003444
Jens Axboe2f5cb732009-04-07 08:51:19 +02003445 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003446 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003447 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003448 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003449 max_dispatch = -1;
3450 else
3451 /*
3452 * Normally we start throttling cfqq when cfq_quantum/2
3453 * requests have been dispatched. But we can drive
3454 * deeper queue depths at the beginning of slice
3455 * subjected to upper limit of cfq_quantum.
3456 * */
3457 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003458 }
3459
3460 /*
3461 * Async queues must wait a bit before being allowed dispatch.
3462 * We also ramp up the dispatch depth gradually for async IO,
3463 * based on the last sync IO we serviced
3464 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003465 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003466 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003467 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003468
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003469 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
Jens Axboee00c54c2009-10-04 20:36:19 +02003470 if (!depth && !cfqq->dispatched)
3471 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003472 if (depth < max_dispatch)
3473 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475
Jens Axboe0b182d62009-10-06 20:49:37 +02003476 /*
3477 * If we're below the current max, allow a dispatch
3478 */
3479 return cfqq->dispatched < max_dispatch;
3480}
3481
3482/*
3483 * Dispatch a request from cfqq, moving them to the request queue
3484 * dispatch list.
3485 */
3486static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3487{
3488 struct request *rq;
3489
3490 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3491
Glauber Costa3932a862016-09-22 20:59:59 -04003492 rq = cfq_check_fifo(cfqq);
3493 if (rq)
3494 cfq_mark_cfqq_must_dispatch(cfqq);
3495
Jens Axboe0b182d62009-10-06 20:49:37 +02003496 if (!cfq_may_dispatch(cfqd, cfqq))
3497 return false;
3498
3499 /*
3500 * follow expired path, else get first next available
3501 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003502 if (!rq)
3503 rq = cfqq->next_rq;
Glauber Costa3932a862016-09-22 20:59:59 -04003504 else
3505 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003506
3507 /*
3508 * insert request into driver dispatch list
3509 */
3510 cfq_dispatch_insert(cfqd->queue, rq);
3511
3512 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003513 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003514
Tejun Heoc5869802011-12-14 00:33:41 +01003515 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003516 cfqd->active_cic = cic;
3517 }
3518
3519 return true;
3520}
3521
3522/*
3523 * Find the cfqq that we need to service and move a request from that to the
3524 * dispatch list
3525 */
3526static int cfq_dispatch_requests(struct request_queue *q, int force)
3527{
3528 struct cfq_data *cfqd = q->elevator->elevator_data;
3529 struct cfq_queue *cfqq;
3530
3531 if (!cfqd->busy_queues)
3532 return 0;
3533
3534 if (unlikely(force))
3535 return cfq_forced_dispatch(cfqd);
3536
3537 cfqq = cfq_select_queue(cfqd);
3538 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003539 return 0;
3540
Jens Axboe2f5cb732009-04-07 08:51:19 +02003541 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003542 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003543 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003544 if (!cfq_dispatch_request(cfqd, cfqq))
3545 return 0;
3546
Jens Axboe2f5cb732009-04-07 08:51:19 +02003547 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003548 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003549
3550 /*
3551 * expire an async queue immediately if it has used up its slice. idle
3552 * queue always expire after 1 dispatch round.
3553 */
3554 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3555 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3556 cfq_class_idle(cfqq))) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003557 cfqq->slice_end = ktime_get_ns() + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003558 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003559 }
3560
Shan Weib217a902009-09-01 10:06:42 +02003561 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003562 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565/*
Jens Axboe5e705372006-07-13 12:39:25 +02003566 * task holds one reference to the queue, dropped when task exits. each rq
3567 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003569 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 * queue lock must be held here.
3571 */
3572static void cfq_put_queue(struct cfq_queue *cfqq)
3573{
Jens Axboe22e2c502005-06-27 10:55:12 +02003574 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003575 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003576
Shaohua Li30d7b942011-01-07 08:46:59 +01003577 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Shaohua Li30d7b942011-01-07 08:46:59 +01003579 cfqq->ref--;
3580 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 return;
3582
Jens Axboe7b679132008-05-30 12:23:07 +02003583 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003585 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003586 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003588 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003589 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003590 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003591 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003592
Vivek Goyalf04a6422009-12-03 12:59:40 -05003593 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003595 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Shaohua Lid02a2c02010-05-25 10:16:53 +02003598static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003599{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003600 struct cfq_queue *__cfqq, *next;
3601
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003602 /*
3603 * If this queue was scheduled to merge with another queue, be
3604 * sure to drop the reference taken on that queue (and others in
3605 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3606 */
3607 __cfqq = cfqq->new_cfqq;
3608 while (__cfqq) {
3609 if (__cfqq == cfqq) {
3610 WARN(1, "cfqq->new_cfqq loop detected\n");
3611 break;
3612 }
3613 next = __cfqq->new_cfqq;
3614 cfq_put_queue(__cfqq);
3615 __cfqq = next;
3616 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003617}
3618
3619static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3620{
3621 if (unlikely(cfqq == cfqd->active_queue)) {
3622 __cfq_slice_expired(cfqd, cfqq, 0);
3623 cfq_schedule_dispatch(cfqd);
3624 }
3625
3626 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003627
Jens Axboe89850f72006-07-22 16:48:31 +02003628 cfq_put_queue(cfqq);
3629}
3630
Tejun Heo9b84cac2011-12-14 00:33:42 +01003631static void cfq_init_icq(struct io_cq *icq)
3632{
3633 struct cfq_io_cq *cic = icq_to_cic(icq);
3634
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003635 cic->ttime.last_end_request = ktime_get_ns();
Tejun Heo9b84cac2011-12-14 00:33:42 +01003636}
3637
Tejun Heoc5869802011-12-14 00:33:41 +01003638static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003639{
Tejun Heoc5869802011-12-14 00:33:41 +01003640 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003641 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003642
Tejun Heo563180a2015-08-18 14:55:00 -07003643 if (cic_to_cfqq(cic, false)) {
3644 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3645 cic_set_cfqq(cic, NULL, false);
Jens Axboe89850f72006-07-22 16:48:31 +02003646 }
3647
Tejun Heo563180a2015-08-18 14:55:00 -07003648 if (cic_to_cfqq(cic, true)) {
3649 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3650 cic_set_cfqq(cic, NULL, true);
Jens Axboe89850f72006-07-22 16:48:31 +02003651 }
Jens Axboe89850f72006-07-22 16:48:31 +02003652}
3653
Tejun Heoabede6d2012-03-19 15:10:57 -07003654static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003655{
3656 struct task_struct *tsk = current;
3657 int ioprio_class;
3658
Jens Axboe3b181522005-06-27 10:56:24 +02003659 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003660 return;
3661
Tejun Heo598971b2012-03-19 15:10:58 -07003662 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003663 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003664 default:
3665 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3666 case IOPRIO_CLASS_NONE:
3667 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003668 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003669 */
3670 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003671 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003672 break;
3673 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003674 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003675 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3676 break;
3677 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003678 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003679 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3680 break;
3681 case IOPRIO_CLASS_IDLE:
3682 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3683 cfqq->ioprio = 7;
3684 cfq_clear_cfqq_idle_window(cfqq);
3685 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003686 }
3687
3688 /*
3689 * keep track of original prio settings in case we have to temporarily
3690 * elevate the priority of this queue
3691 */
3692 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -06003693 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02003694 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003695}
3696
Tejun Heo598971b2012-03-19 15:10:58 -07003697static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003698{
Tejun Heo598971b2012-03-19 15:10:58 -07003699 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003700 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003701 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003702
Tejun Heo598971b2012-03-19 15:10:58 -07003703 /*
3704 * Check whether ioprio has changed. The condition may trigger
3705 * spuriously on a newly created cic but there's no harm.
3706 */
3707 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003708 return;
3709
Tejun Heo563180a2015-08-18 14:55:00 -07003710 cfqq = cic_to_cfqq(cic, false);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003711 if (cfqq) {
Tejun Heo563180a2015-08-18 14:55:00 -07003712 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07003713 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
Tejun Heo563180a2015-08-18 14:55:00 -07003714 cic_set_cfqq(cic, cfqq, false);
Jens Axboe22e2c502005-06-27 10:55:12 +02003715 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003716
Tejun Heo563180a2015-08-18 14:55:00 -07003717 cfqq = cic_to_cfqq(cic, true);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003718 if (cfqq)
3719 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003720
3721 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003722}
3723
Jens Axboed5036d72009-06-26 10:44:34 +02003724static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003725 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003726{
3727 RB_CLEAR_NODE(&cfqq->rb_node);
3728 RB_CLEAR_NODE(&cfqq->p_node);
3729 INIT_LIST_HEAD(&cfqq->fifo);
3730
Shaohua Li30d7b942011-01-07 08:46:59 +01003731 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003732 cfqq->cfqd = cfqd;
3733
3734 cfq_mark_cfqq_prio_changed(cfqq);
3735
3736 if (is_sync) {
3737 if (!cfq_class_idle(cfqq))
3738 cfq_mark_cfqq_idle_window(cfqq);
3739 cfq_mark_cfqq_sync(cfqq);
3740 }
3741 cfqq->pid = pid;
3742}
3743
Vivek Goyal246103332009-12-03 12:59:51 -05003744#ifdef CONFIG_CFQ_GROUP_IOSCHED
Jan Kara142bbdf2017-04-04 14:31:30 +02003745static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003746{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003747 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo60a83702015-08-18 14:55:05 -07003748 struct cfq_queue *cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003749 uint64_t serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003750
Tejun Heo598971b2012-03-19 15:10:58 -07003751 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003752 serial_nr = bio_blkcg(bio)->css.serial_nr;
Tejun Heo598971b2012-03-19 15:10:58 -07003753 rcu_read_unlock();
3754
3755 /*
3756 * Check whether blkcg has changed. The condition may trigger
3757 * spuriously on a newly created cic but there's no harm.
3758 */
Tejun Heof4da8072014-09-08 08:15:20 +09003759 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Jan Kara142bbdf2017-04-04 14:31:30 +02003760 return;
Jens Axboe87760e52016-11-09 12:38:14 -07003761
3762 /*
Tejun Heo60a83702015-08-18 14:55:05 -07003763 * Drop reference to queues. New queues will be assigned in new
3764 * group upon arrival of fresh requests.
3765 */
3766 cfqq = cic_to_cfqq(cic, false);
3767 if (cfqq) {
3768 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3769 cic_set_cfqq(cic, NULL, false);
3770 cfq_put_queue(cfqq);
3771 }
3772
3773 cfqq = cic_to_cfqq(cic, true);
3774 if (cfqq) {
3775 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3776 cic_set_cfqq(cic, NULL, true);
3777 cfq_put_queue(cfqq);
Vivek Goyal246103332009-12-03 12:59:51 -05003778 }
Tejun Heo598971b2012-03-19 15:10:58 -07003779
Tejun Heof4da8072014-09-08 08:15:20 +09003780 cic->blkcg_serial_nr = serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003781}
Tejun Heo598971b2012-03-19 15:10:58 -07003782#else
Jan Kara142bbdf2017-04-04 14:31:30 +02003783static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003784{
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003785}
Vivek Goyal246103332009-12-03 12:59:51 -05003786#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3787
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003788static struct cfq_queue **
Tejun Heo60a83702015-08-18 14:55:05 -07003789cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003790{
Jens Axboefe094d92008-01-31 13:08:54 +01003791 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003792 case IOPRIO_CLASS_RT:
Tejun Heo60a83702015-08-18 14:55:05 -07003793 return &cfqg->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003794 case IOPRIO_CLASS_NONE:
3795 ioprio = IOPRIO_NORM;
3796 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003797 case IOPRIO_CLASS_BE:
Tejun Heo60a83702015-08-18 14:55:05 -07003798 return &cfqg->async_cfqq[1][ioprio];
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003799 case IOPRIO_CLASS_IDLE:
Tejun Heo60a83702015-08-18 14:55:05 -07003800 return &cfqg->async_idle_cfqq;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003801 default:
3802 BUG();
3803 }
3804}
3805
Jens Axboe15c31be2007-07-10 13:43:25 +02003806static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003807cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo2da8de02015-08-18 14:55:02 -07003808 struct bio *bio)
Jens Axboe15c31be2007-07-10 13:43:25 +02003809{
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003810 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3811 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Tejun Heod4aad7f2015-08-18 14:55:04 -07003812 struct cfq_queue **async_cfqq = NULL;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003813 struct cfq_queue *cfqq;
Tejun Heo322731e2015-08-18 14:55:03 -07003814 struct cfq_group *cfqg;
3815
3816 rcu_read_lock();
Tejun Heoae118892015-08-18 14:55:20 -07003817 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
Tejun Heo322731e2015-08-18 14:55:03 -07003818 if (!cfqg) {
3819 cfqq = &cfqd->oom_cfqq;
3820 goto out;
3821 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003822
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003823 if (!is_sync) {
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003824 if (!ioprio_valid(cic->ioprio)) {
3825 struct task_struct *tsk = current;
3826 ioprio = task_nice_ioprio(tsk);
3827 ioprio_class = task_nice_ioclass(tsk);
3828 }
Tejun Heo60a83702015-08-18 14:55:05 -07003829 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003830 cfqq = *async_cfqq;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003831 if (cfqq)
3832 goto out;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003833 }
3834
Tejun Heoe00f4f42016-11-21 18:03:32 -05003835 cfqq = kmem_cache_alloc_node(cfq_pool,
3836 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
Tejun Heod4aad7f2015-08-18 14:55:04 -07003837 cfqd->queue->node);
3838 if (!cfqq) {
3839 cfqq = &cfqd->oom_cfqq;
3840 goto out;
3841 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003842
Alexander Potapenko4d608ba2017-01-23 15:06:43 +01003843 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3844 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
Tejun Heod4aad7f2015-08-18 14:55:04 -07003845 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3846 cfq_init_prio_data(cfqq, cic);
3847 cfq_link_cfqq_cfqg(cfqq, cfqg);
3848 cfq_log_cfqq(cfqd, cfqq, "alloced");
3849
3850 if (async_cfqq) {
3851 /* a new async queue is created, pin and remember */
Shaohua Li30d7b942011-01-07 08:46:59 +01003852 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003853 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003854 }
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003855out:
Shaohua Li30d7b942011-01-07 08:46:59 +01003856 cfqq->ref++;
Tejun Heo322731e2015-08-18 14:55:03 -07003857 rcu_read_unlock();
Jens Axboe15c31be2007-07-10 13:43:25 +02003858 return cfqq;
3859}
3860
Jens Axboe22e2c502005-06-27 10:55:12 +02003861static void
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003862__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003863{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003864 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
Shaohua Li383cd722011-07-12 14:24:35 +02003865 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003866
Shaohua Li383cd722011-07-12 14:24:35 +02003867 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003868 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3869 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3870 ttime->ttime_samples);
Shaohua Li383cd722011-07-12 14:24:35 +02003871}
3872
3873static void
3874cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003875 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003876{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003877 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003878 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003879 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3880 cfqd->cfq_slice_idle);
3881 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003882#ifdef CONFIG_CFQ_GROUP_IOSCHED
3883 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3884#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003885}
3886
Jens Axboe206dc692006-03-28 13:03:44 +02003887static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003888cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003889 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003890{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003891 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003892 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003893 if (cfqq->last_request_pos) {
3894 if (cfqq->last_request_pos < blk_rq_pos(rq))
3895 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3896 else
3897 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3898 }
Jens Axboe206dc692006-03-28 13:03:44 +02003899
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003900 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003901 if (blk_queue_nonrot(cfqd->queue))
3902 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3903 else
3904 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003905}
Jens Axboe22e2c502005-06-27 10:55:12 +02003906
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003907static inline bool req_noidle(struct request *req)
3908{
3909 return req_op(req) == REQ_OP_WRITE &&
3910 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3911}
3912
Jens Axboe22e2c502005-06-27 10:55:12 +02003913/*
3914 * Disable idle window if the process thinks too long or seeks so much that
3915 * it doesn't matter
3916 */
3917static void
3918cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003919 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003920{
Jens Axboe7b679132008-05-30 12:23:07 +02003921 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003922
Jens Axboe08717142008-01-28 11:38:15 +01003923 /*
3924 * Don't idle for async or idle io prio class
3925 */
3926 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003927 return;
3928
Jens Axboec265a7f2008-06-26 13:49:33 +02003929 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003930
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003931 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3932 cfq_mark_cfqq_deep(cfqq);
3933
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003934 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003935 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003936 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003937 !cfqd->cfq_slice_idle ||
3938 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003939 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003940 else if (sample_valid(cic->ttime.ttime_samples)) {
3941 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003942 enable_idle = 0;
3943 else
3944 enable_idle = 1;
3945 }
3946
Jens Axboe7b679132008-05-30 12:23:07 +02003947 if (old_idle != enable_idle) {
3948 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3949 if (enable_idle)
3950 cfq_mark_cfqq_idle_window(cfqq);
3951 else
3952 cfq_clear_cfqq_idle_window(cfqq);
3953 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003954}
3955
Jens Axboe22e2c502005-06-27 10:55:12 +02003956/*
3957 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3958 * no or if we aren't sure, a 1 will cause a preempt.
3959 */
Jens Axboea6151c32009-10-07 20:02:57 +02003960static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003961cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003962 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003963{
Jens Axboe6d048f52007-04-25 12:44:27 +02003964 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003965
Jens Axboe6d048f52007-04-25 12:44:27 +02003966 cfqq = cfqd->active_queue;
3967 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003968 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003969
Jens Axboe6d048f52007-04-25 12:44:27 +02003970 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003971 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003972
3973 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003974 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003975
Jens Axboe22e2c502005-06-27 10:55:12 +02003976 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003977 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3978 */
3979 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3980 return false;
3981
3982 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003983 * if the new request is sync, but the currently running queue is
3984 * not, let the sync request have priority.
3985 */
Glauber Costa3932a862016-09-22 20:59:59 -04003986 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003987 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003988
Jan Kara3984aa52016-01-12 16:24:19 +01003989 /*
3990 * Treat ancestors of current cgroup the same way as current cgroup.
3991 * For anybody else we disallow preemption to guarantee service
3992 * fairness among cgroups.
3993 */
3994 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003995 return false;
3996
3997 if (cfq_slice_used(cfqq))
3998 return true;
3999
Jan Kara6c80731c2016-01-12 16:24:16 +01004000 /*
4001 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4002 */
4003 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4004 return true;
4005
4006 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004007 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004008 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004009 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004010 RB_EMPTY_ROOT(&cfqq->sort_list))
4011 return true;
4012
Jens Axboe374f84a2006-07-23 01:42:19 +02004013 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004014 * So both queues are sync. Let the new request get disk time if
4015 * it's a metadata request and the current queue is doing regular IO.
4016 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02004017 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004018 return true;
4019
Shaohua Lid2d59e12010-11-08 15:01:03 +01004020 /* An idle queue should not be idle now for some reason */
4021 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4022 return true;
4023
Jens Axboe1e3335d2007-02-14 19:59:49 +01004024 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004025 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004026
4027 /*
4028 * if this request is as-good as one we would expect from the
4029 * current cfqq, let it preempt
4030 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01004031 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02004032 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004033
Jens Axboea6151c32009-10-07 20:02:57 +02004034 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004035}
4036
4037/*
4038 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4039 * let it have half of its nominal slice.
4040 */
4041static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4042{
Shaohua Lidf0793a2012-01-19 09:20:09 +01004043 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4044
Jens Axboe7b679132008-05-30 12:23:07 +02004045 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01004046 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004047
Jens Axboebf572252006-07-19 20:29:12 +02004048 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004049 * workload type is changed, don't save slice, otherwise preempt
4050 * doesn't happen
4051 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01004052 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004053 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004054
4055 /*
Jens Axboebf572252006-07-19 20:29:12 +02004056 * Put the new queue at the front of the of the current list,
4057 * so we know that it will be selected next.
4058 */
4059 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02004060
4061 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01004062
Justin TerAvest62a37f62011-03-23 08:25:44 +01004063 cfqq->slice_end = 0;
4064 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004065}
4066
4067/*
Jens Axboe5e705372006-07-13 12:39:25 +02004068 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02004069 * something we should do about it
4070 */
4071static void
Jens Axboe5e705372006-07-13 12:39:25 +02004072cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4073 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004074{
Tejun Heoc5869802011-12-14 00:33:41 +01004075 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02004076
Aaron Carroll45333d52008-08-26 15:52:36 +02004077 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02004078 if (rq->cmd_flags & REQ_PRIO)
4079 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02004080
Shaohua Li383cd722011-07-12 14:24:35 +02004081 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004082 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02004083 cfq_update_idle_window(cfqd, cfqq, cic);
4084
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004085 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004086
4087 if (cfqq == cfqd->active_queue) {
4088 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004089 * Remember that we saw a request from this process, but
4090 * don't start queuing just yet. Otherwise we risk seeing lots
4091 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02004092 * and merging. If the request is already larger than a single
4093 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02004094 * merging is already done. Ditto for a busy system that
4095 * has other work pending, don't risk delaying until the
4096 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02004097 */
Jens Axboed6ceb252009-04-14 14:18:16 +02004098 if (cfq_cfqq_wait_request(cfqq)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004099 if (blk_rq_bytes(rq) > PAGE_SIZE ||
Jens Axboe2d870722009-04-15 12:12:46 +02004100 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07004101 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01004102 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004103 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004104 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07004105 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05004106 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004107 }
Jens Axboed6ceb252009-04-14 14:18:16 +02004108 }
Jens Axboe5e705372006-07-13 12:39:25 +02004109 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004110 /*
4111 * not the active queue - expire current slice if it is
4112 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01004113 * has some old slice time left and is of higher priority or
4114 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02004115 */
4116 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004117 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004118 }
4119}
4120
Jens Axboe165125e2007-07-24 09:28:11 +02004121static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004122{
Jens Axboeb4878f22005-10-20 16:42:29 +02004123 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02004124 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004125
Jens Axboe7b679132008-05-30 12:23:07 +02004126 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07004127 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004129 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
Jens Axboe22e2c502005-06-27 10:55:12 +02004130 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01004131 cfq_add_rq_rb(rq);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004132 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
Tejun Heo155fead2012-04-01 14:38:44 -07004133 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02004134 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
Aaron Carroll45333d52008-08-26 15:52:36 +02004137/*
4138 * Update hw_tag based on peak queue depth over 50 samples under
4139 * sufficient load.
4140 */
4141static void cfq_update_hw_tag(struct cfq_data *cfqd)
4142{
Shaohua Li1a1238a2009-10-27 08:46:23 +01004143 struct cfq_queue *cfqq = cfqd->active_queue;
4144
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004145 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4146 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004147
4148 if (cfqd->hw_tag == 1)
4149 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02004150
4151 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004152 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004153 return;
4154
Shaohua Li1a1238a2009-10-27 08:46:23 +01004155 /*
4156 * If active queue hasn't enough requests and can idle, cfq might not
4157 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4158 * case
4159 */
4160 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4161 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004162 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01004163 return;
4164
Aaron Carroll45333d52008-08-26 15:52:36 +02004165 if (cfqd->hw_tag_samples++ < 50)
4166 return;
4167
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004168 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004169 cfqd->hw_tag = 1;
4170 else
4171 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02004172}
4173
Vivek Goyal7667aa02009-12-08 17:52:58 -05004174static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4175{
Tejun Heoc5869802011-12-14 00:33:41 +01004176 struct cfq_io_cq *cic = cfqd->active_cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004177 u64 now = ktime_get_ns();
Vivek Goyal7667aa02009-12-08 17:52:58 -05004178
Justin TerAvest02a8f012011-02-09 14:20:03 +01004179 /* If the queue already has requests, don't wait */
4180 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4181 return false;
4182
Vivek Goyal7667aa02009-12-08 17:52:58 -05004183 /* If there are other queues in the group, don't wait */
4184 if (cfqq->cfqg->nr_cfqq > 1)
4185 return false;
4186
Shaohua Li7700fc42011-07-12 14:24:56 +02004187 /* the only queue in the group, but think time is big */
4188 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4189 return false;
4190
Vivek Goyal7667aa02009-12-08 17:52:58 -05004191 if (cfq_slice_used(cfqq))
4192 return true;
4193
4194 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02004195 if (cic && sample_valid(cic->ttime.ttime_samples)
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004196 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004197 return true;
4198
4199 /*
4200 * If think times is less than a jiffy than ttime_mean=0 and above
4201 * will not be true. It might happen that slice has not expired yet
4202 * but will expire soon (4-5 ns) during select_queue(). To cover the
4203 * case where think time is less than a jiffy, mark the queue wait
4204 * busy if only 1 jiffy is left in the slice.
4205 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004206 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004207 return true;
4208
4209 return false;
4210}
4211
Jens Axboe165125e2007-07-24 09:28:11 +02004212static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213{
Jens Axboe5e705372006-07-13 12:39:25 +02004214 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004215 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02004216 const int sync = rq_is_sync(rq);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004217 u64 now = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Christoph Hellwiga2b80962016-11-01 07:40:09 -06004219 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Aaron Carroll45333d52008-08-26 15:52:36 +02004221 cfq_update_hw_tag(cfqd);
4222
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004223 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02004224 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004225 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02004226 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004227 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07004228 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004229 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004231 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02004232
Vivek Goyal365722b2009-10-03 15:21:27 +02004233 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04004234 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004235
Shaohua Li383cd722011-07-12 14:24:35 +02004236 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004237
4238 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04004239 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004240 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04004241 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4242 cfqq_type(cfqq));
4243
4244 st->ttime.last_end_request = now;
Jan Kara149321a2016-06-28 09:04:01 +02004245 /*
4246 * We have to do this check in jiffies since start_time is in
4247 * jiffies and it is not trivial to convert to ns. If
4248 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
4249 * will become problematic but so far we are fine (the default
4250 * is 128 ms).
4251 */
4252 if (!time_after(rq->start_time +
4253 nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
4254 jiffies))
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004255 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02004256 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004257
Shaohua Li7700fc42011-07-12 14:24:56 +02004258#ifdef CONFIG_CFQ_GROUP_IOSCHED
4259 cfqq->cfqg->ttime.last_end_request = now;
4260#endif
4261
Jens Axboecaaa5f92006-06-16 11:23:00 +02004262 /*
4263 * If this is the active queue, check if it needs to be expired,
4264 * or if we want to idle in case it has no pending requests.
4265 */
4266 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02004267 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4268
Jens Axboe44f7c162007-01-19 11:51:58 +11004269 if (cfq_cfqq_slice_new(cfqq)) {
4270 cfq_set_prio_slice(cfqd, cfqq);
4271 cfq_clear_cfqq_slice_new(cfqq);
4272 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05004273
4274 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05004275 * Should we wait for next request to come in before we expire
4276 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05004277 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05004278 if (cfq_should_wait_busy(cfqd, cfqq)) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004279 u64 extend_sl = cfqd->cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004280 if (!cfqd->cfq_slice_idle)
4281 extend_sl = cfqd->cfq_group_idle;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004282 cfqq->slice_end = now + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05004283 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01004284 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05004285 }
4286
Jens Axboea36e71f2009-04-15 12:15:11 +02004287 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004288 * Idling is not enabled on:
4289 * - expired queues
4290 * - idle-priority queues
4291 * - async queues
4292 * - queues with still some requests queued
4293 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02004294 */
Jens Axboe08717142008-01-28 11:38:15 +01004295 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02004296 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004297 else if (sync && cfqq_empty &&
4298 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004299 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004300 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004301 }
Jens Axboe6d048f52007-04-25 12:44:27 +02004302
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004303 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02004304 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004307static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
Jens Axboeb8269db2016-06-09 15:47:29 -06004308{
4309 /*
4310 * If REQ_PRIO is set, boost class and prio level, if it's below
4311 * BE/NORM. If prio is not set, restore the potentially boosted
4312 * class/prio level.
4313 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004314 if (!(op & REQ_PRIO)) {
Jens Axboeb8269db2016-06-09 15:47:29 -06004315 cfqq->ioprio_class = cfqq->org_ioprio_class;
4316 cfqq->ioprio = cfqq->org_ioprio;
4317 } else {
4318 if (cfq_class_idle(cfqq))
4319 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4320 if (cfqq->ioprio > IOPRIO_NORM)
4321 cfqq->ioprio = IOPRIO_NORM;
4322 }
4323}
4324
Jens Axboe89850f72006-07-22 16:48:31 +02004325static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004326{
Jens Axboe1b379d82009-08-11 08:26:11 +02004327 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02004328 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004329 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02004330 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004331
4332 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02004333}
4334
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004335static int cfq_may_queue(struct request_queue *q, unsigned int op)
Jens Axboe22e2c502005-06-27 10:55:12 +02004336{
4337 struct cfq_data *cfqd = q->elevator->elevator_data;
4338 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01004339 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02004340 struct cfq_queue *cfqq;
4341
4342 /*
4343 * don't force setup of a queue from here, as a call to may_queue
4344 * does not necessarily imply that a request actually will be queued.
4345 * so just lookup a possibly existing queue, or return 'may queue'
4346 * if that fails
4347 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01004348 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004349 if (!cic)
4350 return ELV_MQUEUE_MAY;
4351
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004352 cfqq = cic_to_cfqq(cic, op_is_sync(op));
Jens Axboe22e2c502005-06-27 10:55:12 +02004353 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004354 cfq_init_prio_data(cfqq, cic);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004355 cfqq_boost_on_prio(cfqq, op);
Jens Axboe22e2c502005-06-27 10:55:12 +02004356
Jens Axboe89850f72006-07-22 16:48:31 +02004357 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004358 }
4359
4360 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363/*
4364 * queue lock held here
4365 */
Jens Axboebb37b942006-12-01 10:42:33 +01004366static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Jens Axboe5e705372006-07-13 12:39:25 +02004368 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Jens Axboe5e705372006-07-13 12:39:25 +02004370 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004371 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Jens Axboe22e2c502005-06-27 10:55:12 +02004373 BUG_ON(!cfqq->allocated[rw]);
4374 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004376 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004377 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01004378 rq->elv.priv[0] = NULL;
4379 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 cfq_put_queue(cfqq);
4382 }
4383}
4384
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004385static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004386cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004387 struct cfq_queue *cfqq)
4388{
4389 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4390 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004391 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004392 cfq_put_queue(cfqq);
4393 return cic_to_cfqq(cic, 1);
4394}
4395
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004396/*
4397 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4398 * was the last process referring to said cfqq.
4399 */
4400static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004401split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004402{
4403 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004404 cfqq->pid = current->pid;
4405 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004406 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004407 return cfqq;
4408 }
4409
4410 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004411
4412 cfq_put_cooperator(cfqq);
4413
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004414 cfq_put_queue(cfqq);
4415 return NULL;
4416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004418 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004420static int
Tejun Heo852c7882012-03-05 13:15:27 -08004421cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4422 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004425 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004427 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004428 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Tejun Heo216284c32011-12-14 00:33:38 +01004430 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004431
Tejun Heo598971b2012-03-19 15:10:58 -07004432 check_ioprio_changed(cic, bio);
Jan Kara142bbdf2017-04-04 14:31:30 +02004433 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004434new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004435 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004436 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heobce61332015-08-18 14:54:59 -07004437 if (cfqq)
4438 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07004439 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004440 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004441 } else {
4442 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004443 * If the queue was seeky for too long, break it apart.
4444 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004445 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004446 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4447 cfqq = split_cfqq(cic, cfqq);
4448 if (!cfqq)
4449 goto new_queue;
4450 }
4451
4452 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004453 * Check to see if this queue is scheduled to merge with
4454 * another, closely cooperating queue. The merging of
4455 * queues happens here as it must be done in process context.
4456 * The reference on new_cfqq was taken in merge_cfqqs.
4457 */
4458 if (cfqq->new_cfqq)
4459 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004463
Jens Axboe6fae9c22011-03-01 15:04:39 -05004464 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004465 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004466 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004467 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c32011-12-14 00:33:38 +01004468 spin_unlock_irq(q->queue_lock);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004469
Jens Axboe5e705372006-07-13 12:39:25 +02004470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
4472
David Howells65f27f32006-11-22 14:55:48 +00004473static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004474{
David Howells65f27f32006-11-22 14:55:48 +00004475 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004476 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004477 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004478
Jens Axboe40bb54d2009-04-15 12:11:10 +02004479 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004480 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004481 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004482}
4483
4484/*
4485 * Timer running if the active_queue is currently idling inside its time slice
4486 */
Jan Kara91148322016-06-08 15:11:39 +02004487static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
Jens Axboe22e2c502005-06-27 10:55:12 +02004488{
Jan Kara91148322016-06-08 15:11:39 +02004489 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4490 idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02004491 struct cfq_queue *cfqq;
4492 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004493 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004494
Jens Axboe7b679132008-05-30 12:23:07 +02004495 cfq_log(cfqd, "idle timer fired");
4496
Jens Axboe22e2c502005-06-27 10:55:12 +02004497 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4498
Jens Axboefe094d92008-01-31 13:08:54 +01004499 cfqq = cfqd->active_queue;
4500 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004501 timed_out = 0;
4502
Jens Axboe22e2c502005-06-27 10:55:12 +02004503 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004504 * We saw a request before the queue expired, let it through
4505 */
4506 if (cfq_cfqq_must_dispatch(cfqq))
4507 goto out_kick;
4508
4509 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004510 * expired
4511 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004512 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004513 goto expire;
4514
4515 /*
4516 * only expire and reinvoke request handler, if there are
4517 * other queues with pending requests
4518 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004519 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004520 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004521
4522 /*
4523 * not expired and it has a request pending, let it dispatch
4524 */
Jens Axboe75e50982009-04-07 08:56:14 +02004525 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004526 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004527
4528 /*
4529 * Queue depth flag is reset only when the idle didn't succeed
4530 */
4531 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004532 }
4533expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004534 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004535out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004536 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004537out_cont:
4538 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jan Kara91148322016-06-08 15:11:39 +02004539 return HRTIMER_NORESTART;
Jens Axboe22e2c502005-06-27 10:55:12 +02004540}
4541
Jens Axboe3b181522005-06-27 10:56:24 +02004542static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4543{
Jan Kara91148322016-06-08 15:11:39 +02004544 hrtimer_cancel(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004545 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004546}
Jens Axboe22e2c502005-06-27 10:55:12 +02004547
Jens Axboeb374d182008-10-31 10:05:07 +01004548static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Jens Axboe22e2c502005-06-27 10:55:12 +02004550 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004551 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004552
Jens Axboe3b181522005-06-27 10:56:24 +02004553 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004554
Al Virod9ff4182006-03-18 13:51:22 -05004555 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004556
Al Virod9ff4182006-03-18 13:51:22 -05004557 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004558 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004559
Tejun Heo03aa2642012-03-05 13:15:19 -08004560 spin_unlock_irq(q->queue_lock);
4561
Al Viroa90d7422006-03-18 12:05:37 -05004562 cfq_shutdown_timer_wq(cfqd);
4563
Tejun Heoffea73f2012-06-04 10:02:29 +02004564#ifdef CONFIG_CFQ_GROUP_IOSCHED
4565 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4566#else
Tejun Heof51b8022012-03-05 13:15:05 -08004567 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004568#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004569 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
4571
Jianpeng Mad50235b2013-07-03 13:25:24 +02004572static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
4574 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004575 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004576 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004577 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Jianpeng Mad50235b2013-07-03 13:25:24 +02004579 eq = elevator_alloc(q, e);
4580 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004581 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004582
Joe Perchesc1b511e2013-08-29 15:21:42 -07004583 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004584 if (!cfqd) {
4585 kobject_put(&eq->kobj);
4586 return -ENOMEM;
4587 }
4588 eq->elevator_data = cfqd;
4589
Tejun Heof51b8022012-03-05 13:15:05 -08004590 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004591 spin_lock_irq(q->queue_lock);
4592 q->elevator = eq;
4593 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004594
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004595 /* Init root service tree */
4596 cfqd->grp_service_tree = CFQ_RB_ROOT;
4597
Tejun Heof51b8022012-03-05 13:15:05 -08004598 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004599#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004600 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004601 if (ret)
4602 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004603
Tejun Heoa2b16932012-04-13 13:11:33 -07004604 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004605#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004606 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004607 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4608 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004609 if (!cfqd->root_group)
4610 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004611
Tejun Heoa2b16932012-04-13 13:11:33 -07004612 cfq_init_cfqg_base(cfqd->root_group);
Tejun Heo3ecca622015-08-18 14:55:35 -07004613 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4614 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
Tejun Heo69d7fde2015-08-18 14:55:36 -07004615#endif
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004616
Jens Axboe26a2ac02009-04-23 12:13:27 +02004617 /*
4618 * Not strictly needed (since RB_ROOT just clears the node and we
4619 * zeroed cfqd on alloc), but better be safe in case someone decides
4620 * to add magic to the rb code
4621 */
4622 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4623 cfqd->prio_trees[i] = RB_ROOT;
4624
Jens Axboe6118b702009-06-30 09:34:12 +02004625 /*
Tejun Heod4aad7f2015-08-18 14:55:04 -07004626 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
Jens Axboe6118b702009-06-30 09:34:12 +02004627 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004628 * will not attempt to free it. oom_cfqq is linked to root_group
4629 * but shouldn't hold a reference as it'll never be unlinked. Lose
4630 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004631 */
4632 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004633 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004634
4635 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004636 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004637 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004638 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Jan Kara91148322016-06-08 15:11:39 +02004640 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4641 HRTIMER_MODE_REL);
Jens Axboe22e2c502005-06-27 10:55:12 +02004642 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
Jens Axboe22e2c502005-06-27 10:55:12 +02004643
Jens Axboe23e018a2009-10-05 08:52:35 +02004644 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004647 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4648 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 cfqd->cfq_back_max = cfq_back_max;
4650 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004651 cfqd->cfq_slice[0] = cfq_slice_async;
4652 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004653 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004654 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004655 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004656 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004657 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004658 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004659 /*
4660 * we optimistically start assuming sync ops weren't delayed in last
4661 * second, in order to have larger depth for async operations.
4662 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004663 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004664 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004665
4666out_free:
4667 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004668 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
Jens Axboe0bb97942015-06-10 08:01:20 -06004672static void cfq_registered_queue(struct request_queue *q)
4673{
4674 struct elevator_queue *e = q->elevator;
4675 struct cfq_data *cfqd = e->elevator_data;
4676
4677 /*
4678 * Default to IOPS mode with no idling for SSDs
4679 */
4680 if (blk_queue_nonrot(q))
4681 cfqd->cfq_slice_idle = 0;
Jan Kara142bbdf2017-04-04 14:31:30 +02004682 wbt_disable_default(q);
Jens Axboe0bb97942015-06-10 08:01:20 -06004683}
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685/*
4686 * sysfs parts below -->
4687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688static ssize_t
4689cfq_var_show(unsigned int var, char *page)
4690{
Masanari Iida176167a2014-04-28 12:38:34 +09004691 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692}
4693
weiping zhang235f8da2017-08-25 01:11:33 +08004694static void
4695cfq_var_store(unsigned int *var, const char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
4697 char *p = (char *) page;
4698
4699 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004703static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004705 struct cfq_data *cfqd = e->elevator_data; \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004706 u64 __data = __VAR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004708 __data = div_u64(__data, NSEC_PER_MSEC); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return cfq_var_show(__data, (page)); \
4710}
4711SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004712SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4713SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004714SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4715SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004716SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004717SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004718SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4719SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4720SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004721SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004722SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723#undef SHOW_FUNCTION
4724
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004725#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4726static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4727{ \
4728 struct cfq_data *cfqd = e->elevator_data; \
4729 u64 __data = __VAR; \
4730 __data = div_u64(__data, NSEC_PER_USEC); \
4731 return cfq_var_show(__data, (page)); \
4732}
4733USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4734USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4735USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4736USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4737USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4738#undef USEC_SHOW_FUNCTION
4739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004741static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004743 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 unsigned int __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08004745 cfq_var_store(&__data, (page)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (__data < (MIN)) \
4747 __data = (MIN); \
4748 else if (__data > (MAX)) \
4749 __data = (MAX); \
4750 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004751 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 else \
4753 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08004754 return count; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004757STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4758 UINT_MAX, 1);
4759STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4760 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004761STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004762STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4763 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004764STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004765STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004766STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4767STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004768STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4769 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004770STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004771STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772#undef STORE_FUNCTION
4773
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004774#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4775static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4776{ \
4777 struct cfq_data *cfqd = e->elevator_data; \
4778 unsigned int __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08004779 cfq_var_store(&__data, (page)); \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004780 if (__data < (MIN)) \
4781 __data = (MIN); \
4782 else if (__data > (MAX)) \
4783 __data = (MAX); \
4784 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
weiping zhang235f8da2017-08-25 01:11:33 +08004785 return count; \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004786}
4787USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4788USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4789USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4790USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4791USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4792#undef USEC_STORE_FUNCTION
4793
Al Viroe572ec72006-03-18 22:27:18 -05004794#define CFQ_ATTR(name) \
4795 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004796
Al Viroe572ec72006-03-18 22:27:18 -05004797static struct elv_fs_entry cfq_attrs[] = {
4798 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004799 CFQ_ATTR(fifo_expire_sync),
4800 CFQ_ATTR(fifo_expire_async),
4801 CFQ_ATTR(back_seek_max),
4802 CFQ_ATTR(back_seek_penalty),
4803 CFQ_ATTR(slice_sync),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004804 CFQ_ATTR(slice_sync_us),
Al Viroe572ec72006-03-18 22:27:18 -05004805 CFQ_ATTR(slice_async),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004806 CFQ_ATTR(slice_async_us),
Al Viroe572ec72006-03-18 22:27:18 -05004807 CFQ_ATTR(slice_async_rq),
4808 CFQ_ATTR(slice_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004809 CFQ_ATTR(slice_idle_us),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004810 CFQ_ATTR(group_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004811 CFQ_ATTR(group_idle_us),
Jens Axboe963b72f2009-10-03 19:42:18 +02004812 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004813 CFQ_ATTR(target_latency),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004814 CFQ_ATTR(target_latency_us),
Al Viroe572ec72006-03-18 22:27:18 -05004815 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816};
4817
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818static struct elevator_type iosched_cfq = {
Jens Axboec51ca6c2016-12-10 15:13:59 -07004819 .ops.sq = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 .elevator_merge_fn = cfq_merge,
4821 .elevator_merged_fn = cfq_merged_request,
4822 .elevator_merge_req_fn = cfq_merged_requests,
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07004823 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4824 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004825 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004826 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004828 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004831 .elevator_former_req_fn = elv_rb_former_request,
4832 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004833 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004834 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 .elevator_set_req_fn = cfq_set_request,
4836 .elevator_put_req_fn = cfq_put_request,
4837 .elevator_may_queue_fn = cfq_may_queue,
4838 .elevator_init_fn = cfq_init_queue,
4839 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004840 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004842 .icq_size = sizeof(struct cfq_io_cq),
4843 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004844 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004845 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 .elevator_owner = THIS_MODULE,
4847};
4848
Vivek Goyal3e252062009-12-04 10:36:42 -05004849#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004850static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07004851 .dfl_cftypes = cfq_blkcg_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07004852 .legacy_cftypes = cfq_blkcg_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004853
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004854 .cpd_alloc_fn = cfq_cpd_alloc,
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004855 .cpd_init_fn = cfq_cpd_init,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004856 .cpd_free_fn = cfq_cpd_free,
Tejun Heo69d7fde2015-08-18 14:55:36 -07004857 .cpd_bind_fn = cfq_cpd_bind,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004858
Tejun Heo001bea72015-08-18 14:55:11 -07004859 .pd_alloc_fn = cfq_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004860 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004861 .pd_offline_fn = cfq_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07004862 .pd_free_fn = cfq_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004863 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004864};
Vivek Goyal3e252062009-12-04 10:36:42 -05004865#endif
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867static int __init cfq_init(void)
4868{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004869 int ret;
4870
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004871#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004872 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004873 if (ret)
4874 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004875#else
4876 cfq_group_idle = 0;
4877#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004878
Tejun Heofd794952012-06-04 10:01:38 +02004879 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004880 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4881 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004882 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Tejun Heo3d3c2372011-12-14 00:33:42 +01004884 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004885 if (ret)
4886 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004887
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004888 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004889
4890err_free_pool:
4891 kmem_cache_destroy(cfq_pool);
4892err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004893#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004894 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004895#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004896 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897}
4898
4899static void __exit cfq_exit(void)
4900{
Tejun Heoffea73f2012-06-04 10:02:29 +02004901#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004902 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004905 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
4908module_init(cfq_init);
4909module_exit(cfq_exit);
4910
4911MODULE_AUTHOR("Jens Axboe");
4912MODULE_LICENSE("GPL");
4913MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");