blob: 2eb87444b157271fe4c3e9d57f4cffc303f0c0a5 [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 */
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700213 u64 start_group_wait_time;
214 u64 start_idle_time;
215 u64 start_empty_time;
Tejun Heo155fead2012-04-01 14:38:44 -0700216 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{
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700494 u64 now;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700495
Tejun Heo155fead2012-04-01 14:38:44 -0700496 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700497 return;
498
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700499 now = ktime_get_ns();
500 if (now > stats->start_group_wait_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700501 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;
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700516 stats->start_group_wait_time = ktime_get_ns();
Tejun Heo155fead2012-04-01 14:38:44 -0700517 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{
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700523 u64 now;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700524
Tejun Heo155fead2012-04-01 14:38:44 -0700525 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700526 return;
527
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700528 now = ktime_get_ns();
529 if (now > stats->start_empty_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700530 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
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700555 stats->start_empty_time = ktime_get_ns();
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)) {
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700564 u64 now = ktime_get_ns();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700565
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700566 if (now > stats->start_idle_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700567 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
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700579 stats->start_idle_time = ktime_get_ns();
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,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700704 u64 start_time_ns,
705 u64 io_start_time_ns,
706 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700707{
Tejun Heo155fead2012-04-01 14:38:44 -0700708 struct cfqg_stats *stats = &cfqg->stats;
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700709 u64 now = ktime_get_ns();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700710
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700711 if (now > io_start_time_ns)
712 blkg_rwstat_add(&stats->service_time, op,
713 now - io_start_time_ns);
714 if (io_start_time_ns > start_time_ns)
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600715 blkg_rwstat_add(&stats->wait_time, op,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700716 io_start_time_ns - start_time_ns);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700717}
718
Tejun Heo689665a2013-01-09 08:05:13 -0800719/* @stats = 0 */
720static void cfqg_stats_reset(struct cfqg_stats *stats)
Tejun Heo155fead2012-04-01 14:38:44 -0700721{
Tejun Heo155fead2012-04-01 14:38:44 -0700722 /* queued stats shouldn't be cleared */
Tejun Heo155fead2012-04-01 14:38:44 -0700723 blkg_rwstat_reset(&stats->merged);
724 blkg_rwstat_reset(&stats->service_time);
725 blkg_rwstat_reset(&stats->wait_time);
726 blkg_stat_reset(&stats->time);
727#ifdef CONFIG_DEBUG_BLK_CGROUP
728 blkg_stat_reset(&stats->unaccounted_time);
729 blkg_stat_reset(&stats->avg_queue_size_sum);
730 blkg_stat_reset(&stats->avg_queue_size_samples);
731 blkg_stat_reset(&stats->dequeue);
732 blkg_stat_reset(&stats->group_wait_time);
733 blkg_stat_reset(&stats->idle_time);
734 blkg_stat_reset(&stats->empty_time);
735#endif
736}
737
Tejun Heo0b399202013-01-09 08:05:13 -0800738/* @to += @from */
Tejun Heoe6269c42015-08-18 14:55:21 -0700739static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
Tejun Heo0b399202013-01-09 08:05:13 -0800740{
741 /* queued stats shouldn't be cleared */
Tejun Heoe6269c42015-08-18 14:55:21 -0700742 blkg_rwstat_add_aux(&to->merged, &from->merged);
743 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
744 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
745 blkg_stat_add_aux(&from->time, &from->time);
Tejun Heo0b399202013-01-09 08:05:13 -0800746#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoe6269c42015-08-18 14:55:21 -0700747 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
748 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
749 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
750 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
751 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
752 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
753 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
Tejun Heo0b399202013-01-09 08:05:13 -0800754#endif
755}
756
757/*
Tejun Heoe6269c42015-08-18 14:55:21 -0700758 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
Tejun Heo0b399202013-01-09 08:05:13 -0800759 * recursive stats can still account for the amount used by this cfqg after
760 * it's gone.
761 */
762static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
763{
764 struct cfq_group *parent = cfqg_parent(cfqg);
765
766 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
767
768 if (unlikely(!parent))
769 return;
770
Tejun Heoe6269c42015-08-18 14:55:21 -0700771 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800772 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800773}
774
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100775#else /* CONFIG_CFQ_GROUP_IOSCHED */
776
Tejun Heod02f7aa2013-01-09 08:05:11 -0800777static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Jan Kara3984aa52016-01-12 16:24:19 +0100778static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
779 struct cfq_group *ancestor)
780{
781 return true;
782}
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100783static inline void cfqg_get(struct cfq_group *cfqg) { }
784static inline void cfqg_put(struct cfq_group *cfqg) { }
785
Jens Axboe7b679132008-05-30 12:23:07 +0200786#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400787 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
788 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
789 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
790 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200791#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100792
Tejun Heo155fead2012-04-01 14:38:44 -0700793static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600794 struct cfq_group *curr_cfqg, unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700795static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600796 uint64_t time, unsigned long unaccounted_time) { }
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600797static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
798 unsigned int op) { }
799static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
800 unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700801static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700802 u64 start_time_ns,
803 u64 io_start_time_ns,
804 unsigned int op) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700805
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100806#endif /* CONFIG_CFQ_GROUP_IOSCHED */
807
Jens Axboe7b679132008-05-30 12:23:07 +0200808#define cfq_log(cfqd, fmt, args...) \
809 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
810
Vivek Goyal615f0252009-12-03 12:59:39 -0500811/* Traverses through cfq group service trees */
812#define for_each_cfqg_st(cfqg, i, j, st) \
813 for (i = 0; i <= IDLE_WORKLOAD; i++) \
814 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
815 : &cfqg->service_tree_idle; \
816 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
817 (i == IDLE_WORKLOAD && j == 0); \
818 j++, st = i < IDLE_WORKLOAD ? \
819 &cfqg->service_trees[i][j]: NULL) \
820
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200821static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
822 struct cfq_ttime *ttime, bool group_idle)
823{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600824 u64 slice;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200825 if (!sample_valid(ttime->ttime_samples))
826 return false;
827 if (group_idle)
828 slice = cfqd->cfq_group_idle;
829 else
830 slice = cfqd->cfq_slice_idle;
831 return ttime->ttime_mean > slice;
832}
Vivek Goyal615f0252009-12-03 12:59:39 -0500833
Vivek Goyal02b35082010-08-23 12:23:53 +0200834static inline bool iops_mode(struct cfq_data *cfqd)
835{
836 /*
837 * If we are not idling on queues and it is a NCQ drive, parallel
838 * execution of requests is on and measuring time is not possible
839 * in most of the cases until and unless we drive shallower queue
840 * depths and that becomes a performance bottleneck. In such cases
841 * switch to start providing fairness in terms of number of IOs.
842 */
843 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
844 return true;
845 else
846 return false;
847}
848
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400849static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100850{
851 if (cfq_class_idle(cfqq))
852 return IDLE_WORKLOAD;
853 if (cfq_class_rt(cfqq))
854 return RT_WORKLOAD;
855 return BE_WORKLOAD;
856}
857
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100858
859static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
860{
861 if (!cfq_cfqq_sync(cfqq))
862 return ASYNC_WORKLOAD;
863 if (!cfq_cfqq_idle_window(cfqq))
864 return SYNC_NOIDLE_WORKLOAD;
865 return SYNC_WORKLOAD;
866}
867
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400868static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500869 struct cfq_data *cfqd,
870 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100871{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400872 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500873 return cfqg->service_tree_idle.count;
874
Vivek Goyal34b98d02012-10-03 16:56:58 -0400875 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
876 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
877 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100878}
879
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500880static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
881 struct cfq_group *cfqg)
882{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400883 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
884 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500885}
886
Jens Axboe165125e2007-07-24 09:28:11 +0200887static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800888static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heo2da8de02015-08-18 14:55:02 -0700889 struct cfq_io_cq *cic, struct bio *bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200890
Tejun Heoc5869802011-12-14 00:33:41 +0100891static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
892{
893 /* cic->icq is the first member, %NULL will convert to %NULL */
894 return container_of(icq, struct cfq_io_cq, icq);
895}
896
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100897static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
898 struct io_context *ioc)
899{
900 if (ioc)
901 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
902 return NULL;
903}
904
Tejun Heoc5869802011-12-14 00:33:41 +0100905static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200906{
Jens Axboea6151c32009-10-07 20:02:57 +0200907 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200908}
909
Tejun Heoc5869802011-12-14 00:33:41 +0100910static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
911 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200912{
Jens Axboea6151c32009-10-07 20:02:57 +0200913 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200914}
915
Tejun Heoc5869802011-12-14 00:33:41 +0100916static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400917{
Tejun Heoc5869802011-12-14 00:33:41 +0100918 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400919}
920
Vasily Tarasov91fac312007-04-25 12:29:51 +0200921/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700922 * scheduler run of queue, if there are requests pending and no one in the
923 * driver that will restart queueing
924 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200925static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700926{
Jens Axboe7b679132008-05-30 12:23:07 +0200927 if (cfqd->busy_queues) {
928 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600929 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200930 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100934 * Scale schedule slice based on io priority. Use the sync time slice only
935 * if a queue is marked sync and has sync io queued. A sync queue with async
936 * io only, should not get full sync slice length.
937 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600938static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200939 unsigned short prio)
940{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600941 u64 base_slice = cfqd->cfq_slice[sync];
942 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
Jens Axboed9e76202007-04-20 14:27:50 +0200943
944 WARN_ON(prio >= IOPRIO_BE_NR);
945
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600946 return base_slice + (slice * (4 - prio));
Jens Axboed9e76202007-04-20 14:27:50 +0200947}
948
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600949static inline u64
Jens Axboe44f7c162007-01-19 11:51:58 +1100950cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
951{
Jens Axboed9e76202007-04-20 14:27:50 +0200952 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100953}
954
Tejun Heo1d3650f2013-01-09 08:05:11 -0800955/**
956 * cfqg_scale_charge - scale disk time charge according to cfqg weight
957 * @charge: disk time being charged
958 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
959 *
960 * Scale @charge according to @vfraction, which is in range (0, 1]. The
961 * scaling is inversely proportional.
962 *
963 * scaled = charge / vfraction
964 *
965 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
966 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600967static inline u64 cfqg_scale_charge(u64 charge,
Tejun Heo1d3650f2013-01-09 08:05:11 -0800968 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500969{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800970 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500971
Tejun Heo1d3650f2013-01-09 08:05:11 -0800972 /* charge / vfraction */
973 c <<= CFQ_SERVICE_SHIFT;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600974 return div_u64(c, vfraction);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500975}
976
977static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
978{
979 s64 delta = (s64)(vdisktime - min_vdisktime);
980 if (delta > 0)
981 min_vdisktime = vdisktime;
982
983 return min_vdisktime;
984}
985
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500986static void update_min_vdisktime(struct cfq_rb_root *st)
987{
Davidlohr Bueso09663c82017-09-08 16:15:05 -0700988 if (!RB_EMPTY_ROOT(&st->rb.rb_root)) {
989 struct cfq_group *cfqg = rb_entry_cfqg(st->rb.rb_leftmost);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500990
Gui Jianfenga6032712011-03-07 09:28:09 +0100991 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
992 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500993 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500994}
995
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100996/*
997 * get averaged number of queues of RT/BE priority.
998 * average is updated, with a formula that gives more weight to higher numbers,
999 * to quickly follows sudden increases and decrease slowly
1000 */
1001
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001002static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1003 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001004{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001005 unsigned min_q, max_q;
1006 unsigned mult = cfq_hist_divisor - 1;
1007 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001008 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001009
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001010 min_q = min(cfqg->busy_queues_avg[rt], busy);
1011 max_q = max(cfqg->busy_queues_avg[rt], busy);
1012 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001013 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001014 return cfqg->busy_queues_avg[rt];
1015}
1016
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001017static inline u64
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001018cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1019{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001020 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001021}
1022
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001023static inline u64
Vivek Goyalba5bd522011-01-19 08:25:02 -07001024cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001025{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001026 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001027 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001028 /*
1029 * interested queues (we consider only the ones with the same
1030 * priority class in the cfq group)
1031 */
1032 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1033 cfq_class_rt(cfqq));
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001034 u64 sync_slice = cfqd->cfq_slice[1];
1035 u64 expect_latency = sync_slice * iq;
1036 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001037
1038 if (expect_latency > group_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001039 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1040 u64 low_slice;
1041
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001042 /* scale low_slice according to IO priority
1043 * and sync vs async */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001044 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1045 low_slice = min(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001046 /* the adapted slice value is scaled to fit all iqs
1047 * into the target latency */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001048 slice = div64_u64(slice*group_slice, expect_latency);
1049 slice = max(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001050 }
1051 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001052 return slice;
1053}
1054
1055static inline void
1056cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1057{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001058 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1059 u64 now = ktime_get_ns();
Shaohua Lic553f8e2011-01-14 08:41:03 +01001060
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001061 cfqq->slice_start = now;
1062 cfqq->slice_end = now + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001063 cfqq->allocated_slice = slice;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001064 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
Jens Axboe44f7c162007-01-19 11:51:58 +11001065}
1066
1067/*
1068 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1069 * isn't valid until the first request from the dispatch is activated
1070 * and the slice time set.
1071 */
Jens Axboea6151c32009-10-07 20:02:57 +02001072static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001073{
1074 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001075 return false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001076 if (ktime_get_ns() < cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01001077 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001078
Shaohua Lic1e44752010-11-08 15:01:02 +01001079 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001080}
1081
1082/*
Jens Axboe5e705372006-07-13 12:39:25 +02001083 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001085 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Jens Axboe5e705372006-07-13 12:39:25 +02001087static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001088cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001090 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001092#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1093#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1094 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jens Axboe5e705372006-07-13 12:39:25 +02001096 if (rq1 == NULL || rq1 == rq2)
1097 return rq2;
1098 if (rq2 == NULL)
1099 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001100
Namhyung Kim229836b2011-05-24 10:23:21 +02001101 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1102 return rq_is_sync(rq1) ? rq1 : rq2;
1103
Christoph Hellwig65299a32011-08-23 14:50:29 +02001104 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1105 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001106
Tejun Heo83096eb2009-05-07 22:24:39 +09001107 s1 = blk_rq_pos(rq1);
1108 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * by definition, 1KiB is 2 sectors
1112 */
1113 back_max = cfqd->cfq_back_max * 2;
1114
1115 /*
1116 * Strict one way elevator _except_ in the case where we allow
1117 * short backward seeks which are biased as twice the cost of a
1118 * similar forward seek.
1119 */
1120 if (s1 >= last)
1121 d1 = s1 - last;
1122 else if (s1 + back_max >= last)
1123 d1 = (last - s1) * cfqd->cfq_back_penalty;
1124 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001125 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if (s2 >= last)
1128 d2 = s2 - last;
1129 else if (s2 + back_max >= last)
1130 d2 = (last - s2) * cfqd->cfq_back_penalty;
1131 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001132 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Andreas Mohre8a99052006-03-28 08:59:49 +02001136 /*
1137 * By doing switch() on the bit mask "wrap" we avoid having to
1138 * check two variables for all permutations: --> faster!
1139 */
1140 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001141 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001142 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001143 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001144 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001145 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001146 else {
1147 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001148 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001149 else
Jens Axboe5e705372006-07-13 12:39:25 +02001150 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001151 }
1152
1153 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001154 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001155 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001156 return rq2;
1157 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001158 default:
1159 /*
1160 * Since both rqs are wrapped,
1161 * start with the one that's further behind head
1162 * (--> only *one* back seek required),
1163 * since back seek takes more time than forward.
1164 */
1165 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001166 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 else
Jens Axboe5e705372006-07-13 12:39:25 +02001168 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170}
1171
Jens Axboe08717142008-01-28 11:38:15 +01001172static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001173{
Vivek Goyal615f0252009-12-03 12:59:39 -05001174 /* Service tree is empty */
1175 if (!root->count)
1176 return NULL;
1177
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001178 return rb_entry(rb_first_cached(&root->rb), struct cfq_queue, rb_node);
Jens Axboecc09e292007-04-26 12:53:50 +02001179}
1180
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001181static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1182{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001183 return rb_entry_cfqg(rb_first_cached(&root->rb));
Jens Axboea36e71f2009-04-15 12:15:11 +02001184}
1185
Jens Axboecc09e292007-04-26 12:53:50 +02001186static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1187{
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001188 if (root->rb_rightmost == n)
1189 root->rb_rightmost = rb_prev(n);
1190
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001191 rb_erase_cached(n, &root->rb);
1192 RB_CLEAR_NODE(n);
1193
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001194 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
1198 * would be nice to take fifo expire time into account as well
1199 */
Jens Axboe5e705372006-07-13 12:39:25 +02001200static struct request *
1201cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1202 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jens Axboe21183b02006-07-13 12:33:14 +02001204 struct rb_node *rbnext = rb_next(&last->rb_node);
1205 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001206 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Jens Axboe21183b02006-07-13 12:33:14 +02001208 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001211 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001214 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001215 else {
1216 rbnext = rb_first(&cfqq->sort_list);
1217 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001218 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001221 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001224static u64 cfq_slice_offset(struct cfq_data *cfqd,
1225 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jens Axboed9e76202007-04-20 14:27:50 +02001227 /*
1228 * just an approximation, should be ok.
1229 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001230 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001231 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001234static inline s64
1235cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1236{
1237 return cfqg->vdisktime - st->min_vdisktime;
1238}
1239
1240static void
1241__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1242{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001243 struct rb_node **node = &st->rb.rb_root.rb_node;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001244 struct rb_node *parent = NULL;
1245 struct cfq_group *__cfqg;
1246 s64 key = cfqg_key(st, cfqg);
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001247 bool leftmost = true, rightmost = true;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001248
1249 while (*node != NULL) {
1250 parent = *node;
1251 __cfqg = rb_entry_cfqg(parent);
1252
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001253 if (key < cfqg_key(st, __cfqg)) {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001254 node = &parent->rb_left;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001255 rightmost = false;
1256 } else {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001257 node = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001258 leftmost = false;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001259 }
1260 }
1261
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001262 if (rightmost)
1263 st->rb_rightmost = &cfqg->rb_node;
1264
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001265 rb_link_node(&cfqg->rb_node, parent, node);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001266 rb_insert_color_cached(&cfqg->rb_node, &st->rb, leftmost);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001267}
1268
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001269/*
1270 * This has to be called only on activation of cfqg
1271 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001272static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001273cfq_update_group_weight(struct cfq_group *cfqg)
1274{
Tejun Heo3381cb82012-04-01 14:38:44 -07001275 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001276 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001277 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001278 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001279}
1280
1281static void
1282cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1283{
1284 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001285
1286 if (cfqg->new_leaf_weight) {
1287 cfqg->leaf_weight = cfqg->new_leaf_weight;
1288 cfqg->new_leaf_weight = 0;
1289 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001290}
1291
1292static void
1293cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1294{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001295 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001296 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001297 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001298 bool propagate;
1299
1300 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001301 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1302
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001303 /*
1304 * Update leaf_weight. We cannot update weight at this point
1305 * because cfqg might already have been activated and is
1306 * contributing its current weight to the parent's child_weight.
1307 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001308 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001309 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001310
1311 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001312 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1313 * entitled to. vfraction is calculated by walking the tree
1314 * towards the root calculating the fraction it has at each level.
1315 * The compounded ratio is how much vfraction @cfqg owns.
1316 *
1317 * Start with the proportion tasks in this cfqg has against active
1318 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001319 */
1320 propagate = !pos->nr_active++;
1321 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001322 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001323
Tejun Heo1d3650f2013-01-09 08:05:11 -08001324 /*
1325 * Compound ->weight walking up the tree. Both activation and
1326 * vfraction calculation are done in the same loop. Propagation
1327 * stops once an already activated node is met. vfraction
1328 * calculation should always continue to the root.
1329 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001330 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001331 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001332 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001333 propagate = !parent->nr_active++;
1334 parent->children_weight += pos->weight;
1335 }
1336 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001337 pos = parent;
1338 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001339
1340 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001341}
1342
Hou Tao5be6b752017-03-01 09:02:33 +08001343static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
1344{
1345 if (!iops_mode(cfqd))
1346 return CFQ_SLICE_MODE_GROUP_DELAY;
1347 else
1348 return CFQ_IOPS_MODE_GROUP_DELAY;
1349}
1350
Justin TerAvest8184f932011-03-17 16:12:36 +01001351static void
1352cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001353{
1354 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1355 struct cfq_group *__cfqg;
1356 struct rb_node *n;
1357
1358 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001359 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001360 return;
1361
1362 /*
1363 * Currently put the group at the end. Later implement something
1364 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001365 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001366 */
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001367 n = st->rb_rightmost;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001368 if (n) {
1369 __cfqg = rb_entry_cfqg(n);
Hou Tao5be6b752017-03-01 09:02:33 +08001370 cfqg->vdisktime = __cfqg->vdisktime +
1371 cfq_get_cfqg_vdisktime_delay(cfqd);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001372 } else
1373 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001374 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001375}
1376
1377static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001378cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1379{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001380 struct cfq_group *pos = cfqg;
1381 bool propagate;
1382
1383 /*
1384 * Undo activation from cfq_group_service_tree_add(). Deactivate
1385 * @cfqg and propagate deactivation upwards.
1386 */
1387 propagate = !--pos->nr_active;
1388 pos->children_weight -= pos->leaf_weight;
1389
1390 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001391 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001392
1393 /* @pos has 0 nr_active at this point */
1394 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001395 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001396
1397 if (!parent)
1398 break;
1399
1400 propagate = !--parent->nr_active;
1401 parent->children_weight -= pos->weight;
1402 pos = parent;
1403 }
1404
1405 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001406 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1407 cfq_rb_erase(&cfqg->rb_node, st);
1408}
1409
1410static void
1411cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001412{
1413 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1414
1415 BUG_ON(cfqg->nr_cfqq < 1);
1416 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001417
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001418 /* If there are other cfq queues under this group, don't delete it */
1419 if (cfqg->nr_cfqq)
1420 return;
1421
Vivek Goyal2868ef72009-12-03 12:59:48 -05001422 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001423 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001424 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001425 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001426}
1427
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001428static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1429 u64 *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001430{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001431 u64 slice_used;
1432 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001433
1434 /*
1435 * Queue got expired before even a single request completed or
1436 * got expired immediately after first request completion.
1437 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001438 if (!cfqq->slice_start || cfqq->slice_start == now) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001439 /*
1440 * Also charge the seek time incurred to the group, otherwise
1441 * if there are mutiple queues in the group, each can dispatch
1442 * a single request on seeky media and cause lots of seek time
1443 * and group will never know it.
1444 */
Jan Kara0b31c102016-06-28 09:04:02 +02001445 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1446 jiffies_to_nsecs(1));
Vivek Goyaldae739e2009-12-03 12:59:45 -05001447 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001448 slice_used = now - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001449 if (slice_used > cfqq->allocated_slice) {
1450 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001451 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001452 }
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001453 if (cfqq->slice_start > cfqq->dispatch_start)
Justin TerAvest167400d2011-03-12 16:54:00 +01001454 *unaccounted_time += cfqq->slice_start -
1455 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001456 }
1457
Vivek Goyaldae739e2009-12-03 12:59:45 -05001458 return slice_used;
1459}
1460
1461static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001462 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001463{
1464 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001465 u64 used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001466 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1467 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001468 unsigned int vfr;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001469 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001470
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001471 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001472 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001473
Vivek Goyal02b35082010-08-23 12:23:53 +02001474 if (iops_mode(cfqd))
1475 charge = cfqq->slice_dispatch;
1476 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1477 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001478
Tejun Heo1d3650f2013-01-09 08:05:11 -08001479 /*
1480 * Can't update vdisktime while on service tree and cfqg->vfraction
1481 * is valid only while on it. Cache vfr, leave the service tree,
1482 * update vdisktime and go back on. The re-addition to the tree
1483 * will also update the weights as necessary.
1484 */
1485 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001486 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001487 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001488 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001489
1490 /* This group is being expired. Save the context */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001491 if (cfqd->workload_expires > now) {
1492 cfqg->saved_wl_slice = cfqd->workload_expires - now;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001493 cfqg->saved_wl_type = cfqd->serving_wl_type;
1494 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001495 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001496 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001497
1498 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1499 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001500 cfq_log_cfqq(cfqq->cfqd, cfqq,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001501 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
Joe Perchesfd16d262011-06-13 10:42:49 +02001502 used_sl, cfqq->slice_dispatch, charge,
1503 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001504 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1505 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001506}
1507
Tejun Heof51b8022012-03-05 13:15:05 -08001508/**
1509 * cfq_init_cfqg_base - initialize base part of a cfq_group
1510 * @cfqg: cfq_group to initialize
1511 *
1512 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1513 * is enabled or not.
1514 */
1515static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1516{
1517 struct cfq_rb_root *st;
1518 int i, j;
1519
1520 for_each_cfqg_st(cfqg, i, j, st)
1521 *st = CFQ_RB_ROOT;
1522 RB_CLEAR_NODE(&cfqg->rb_node);
1523
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001524 cfqg->ttime.last_end_request = ktime_get_ns();
Tejun Heof51b8022012-03-05 13:15:05 -08001525}
1526
Vivek Goyal25fb5162009-12-03 12:59:46 -05001527#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo69d7fde2015-08-18 14:55:36 -07001528static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1529 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1530
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001531static void cfqg_stats_exit(struct cfqg_stats *stats)
Peter Zijlstra90d38392013-11-12 19:42:14 -08001532{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001533 blkg_rwstat_exit(&stats->merged);
1534 blkg_rwstat_exit(&stats->service_time);
1535 blkg_rwstat_exit(&stats->wait_time);
1536 blkg_rwstat_exit(&stats->queued);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001537 blkg_stat_exit(&stats->time);
1538#ifdef CONFIG_DEBUG_BLK_CGROUP
1539 blkg_stat_exit(&stats->unaccounted_time);
1540 blkg_stat_exit(&stats->avg_queue_size_sum);
1541 blkg_stat_exit(&stats->avg_queue_size_samples);
1542 blkg_stat_exit(&stats->dequeue);
1543 blkg_stat_exit(&stats->group_wait_time);
1544 blkg_stat_exit(&stats->idle_time);
1545 blkg_stat_exit(&stats->empty_time);
1546#endif
1547}
1548
1549static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1550{
Tejun Heo77ea7332015-08-18 14:55:24 -07001551 if (blkg_rwstat_init(&stats->merged, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001552 blkg_rwstat_init(&stats->service_time, gfp) ||
1553 blkg_rwstat_init(&stats->wait_time, gfp) ||
1554 blkg_rwstat_init(&stats->queued, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001555 blkg_stat_init(&stats->time, gfp))
1556 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001557
1558#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001559 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1560 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1561 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1562 blkg_stat_init(&stats->dequeue, gfp) ||
1563 blkg_stat_init(&stats->group_wait_time, gfp) ||
1564 blkg_stat_init(&stats->idle_time, gfp) ||
1565 blkg_stat_init(&stats->empty_time, gfp))
1566 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001567#endif
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001568 return 0;
1569err:
1570 cfqg_stats_exit(stats);
1571 return -ENOMEM;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001572}
1573
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001574static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1575{
1576 struct cfq_group_data *cgd;
1577
Tejun Heoebc4ff62016-11-10 11:16:37 -05001578 cgd = kzalloc(sizeof(*cgd), gfp);
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001579 if (!cgd)
1580 return NULL;
1581 return &cgd->cpd;
1582}
1583
Tejun Heo81437642015-08-18 14:55:15 -07001584static void cfq_cpd_init(struct blkcg_policy_data *cpd)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001585{
Tejun Heo81437642015-08-18 14:55:15 -07001586 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001587 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
Tejun Heo69d7fde2015-08-18 14:55:36 -07001588 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001589
Tejun Heo69d7fde2015-08-18 14:55:36 -07001590 if (cpd_to_blkcg(cpd) == &blkcg_root)
1591 weight *= 2;
1592
1593 cgd->weight = weight;
1594 cgd->leaf_weight = weight;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001595}
1596
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001597static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1598{
1599 kfree(cpd_to_cfqgd(cpd));
1600}
1601
Tejun Heo69d7fde2015-08-18 14:55:36 -07001602static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1603{
1604 struct blkcg *blkcg = cpd_to_blkcg(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001605 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
Tejun Heo69d7fde2015-08-18 14:55:36 -07001606 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1607
1608 if (blkcg == &blkcg_root)
1609 weight *= 2;
1610
1611 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1612 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1613}
1614
Tejun Heo001bea72015-08-18 14:55:11 -07001615static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1616{
Tejun Heob2ce2642015-08-18 14:55:13 -07001617 struct cfq_group *cfqg;
1618
1619 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1620 if (!cfqg)
1621 return NULL;
1622
1623 cfq_init_cfqg_base(cfqg);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001624 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1625 kfree(cfqg);
1626 return NULL;
1627 }
Tejun Heob2ce2642015-08-18 14:55:13 -07001628
1629 return &cfqg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -07001630}
1631
Tejun Heoa9520cd2015-08-18 14:55:14 -07001632static void cfq_pd_init(struct blkg_policy_data *pd)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001633{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001634 struct cfq_group *cfqg = pd_to_cfqg(pd);
1635 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001636
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001637 cfqg->weight = cgd->weight;
1638 cfqg->leaf_weight = cgd->leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001639}
1640
Tejun Heoa9520cd2015-08-18 14:55:14 -07001641static void cfq_pd_offline(struct blkg_policy_data *pd)
Tejun Heo0b399202013-01-09 08:05:13 -08001642{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001643 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo60a83702015-08-18 14:55:05 -07001644 int i;
1645
1646 for (i = 0; i < IOPRIO_BE_NR; i++) {
1647 if (cfqg->async_cfqq[0][i])
1648 cfq_put_queue(cfqg->async_cfqq[0][i]);
1649 if (cfqg->async_cfqq[1][i])
1650 cfq_put_queue(cfqg->async_cfqq[1][i]);
1651 }
1652
1653 if (cfqg->async_idle_cfqq)
1654 cfq_put_queue(cfqg->async_idle_cfqq);
1655
Tejun Heo0b399202013-01-09 08:05:13 -08001656 /*
1657 * @blkg is going offline and will be ignored by
1658 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1659 * that they don't get lost. If IOs complete after this point, the
1660 * stats for them will be lost. Oh well...
1661 */
Tejun Heo60a83702015-08-18 14:55:05 -07001662 cfqg_stats_xfer_dead(cfqg);
Tejun Heo0b399202013-01-09 08:05:13 -08001663}
1664
Tejun Heo001bea72015-08-18 14:55:11 -07001665static void cfq_pd_free(struct blkg_policy_data *pd)
1666{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001667 struct cfq_group *cfqg = pd_to_cfqg(pd);
1668
1669 cfqg_stats_exit(&cfqg->stats);
1670 return kfree(cfqg);
Tejun Heo001bea72015-08-18 14:55:11 -07001671}
1672
Tejun Heoa9520cd2015-08-18 14:55:14 -07001673static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo689665a2013-01-09 08:05:13 -08001674{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001675 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo689665a2013-01-09 08:05:13 -08001676
1677 cfqg_stats_reset(&cfqg->stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001678}
1679
Tejun Heoae118892015-08-18 14:55:20 -07001680static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1681 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001682{
Tejun Heoae118892015-08-18 14:55:20 -07001683 struct blkcg_gq *blkg;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001684
Tejun Heoae118892015-08-18 14:55:20 -07001685 blkg = blkg_lookup(blkcg, cfqd->queue);
1686 if (likely(blkg))
1687 return blkg_to_cfqg(blkg);
1688 return NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001689}
1690
1691static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1692{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001693 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001694 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001695 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001696}
1697
Tejun Heof95a04a2012-04-16 13:57:26 -07001698static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1699 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001700{
Tejun Heof95a04a2012-04-16 13:57:26 -07001701 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001702
1703 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001704 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001705 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001706}
1707
Tejun Heo2da8ca82013-12-05 12:28:04 -05001708static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001709{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001710 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1711 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1712 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001713 return 0;
1714}
1715
Tejun Heoe71357e2013-01-09 08:05:10 -08001716static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1717 struct blkg_policy_data *pd, int off)
1718{
1719 struct cfq_group *cfqg = pd_to_cfqg(pd);
1720
1721 if (!cfqg->dev_leaf_weight)
1722 return 0;
1723 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1724}
1725
Tejun Heo2da8ca82013-12-05 12:28:04 -05001726static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001727{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001728 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1729 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1730 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001731 return 0;
1732}
1733
Tejun Heo2da8ca82013-12-05 12:28:04 -05001734static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001735{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001736 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001737 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1738 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001739
Jens Axboe9470e4a2015-06-19 10:19:36 -06001740 if (cgd)
1741 val = cgd->weight;
1742
1743 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001744 return 0;
1745}
1746
Tejun Heo2da8ca82013-12-05 12:28:04 -05001747static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001748{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001749 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001750 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1751 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001752
Jens Axboe9470e4a2015-06-19 10:19:36 -06001753 if (cgd)
1754 val = cgd->leaf_weight;
1755
1756 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001757 return 0;
1758}
1759
Tejun Heo451af502014-05-13 12:16:21 -04001760static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1761 char *buf, size_t nbytes, loff_t off,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001762 bool on_dfl, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001763{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001764 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1765 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo451af502014-05-13 12:16:21 -04001766 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001767 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001768 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001769 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001770 int ret;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001771 u64 v;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001772
Tejun Heo3c798392012-04-16 13:57:25 -07001773 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001774 if (ret)
1775 return ret;
1776
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001777 if (sscanf(ctx.body, "%llu", &v) == 1) {
1778 /* require "default" on dfl */
1779 ret = -ERANGE;
1780 if (!v && on_dfl)
1781 goto out_finish;
1782 } else if (!strcmp(strim(ctx.body), "default")) {
1783 v = 0;
1784 } else {
1785 ret = -EINVAL;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001786 goto out_finish;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001787 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001788
Tejun Heo3381cb82012-04-01 14:38:44 -07001789 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001790 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001791
Tejun Heo20386ce2015-08-18 14:55:28 -07001792 ret = -ERANGE;
Tejun Heo69d7fde2015-08-18 14:55:36 -07001793 if (!v || (v >= min && v <= max)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001794 if (!is_leaf_weight) {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001795 cfqg->dev_weight = v;
1796 cfqg->new_weight = v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001797 } else {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001798 cfqg->dev_leaf_weight = v;
1799 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001800 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001801 ret = 0;
1802 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001803out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001804 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001805 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001806}
1807
Tejun Heo451af502014-05-13 12:16:21 -04001808static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1809 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001810{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001811 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001812}
1813
Tejun Heo451af502014-05-13 12:16:21 -04001814static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1815 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001816{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001817 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001818}
1819
Tejun Heodd165eb2015-08-18 14:55:33 -07001820static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
Tejun Heo69d7fde2015-08-18 14:55:36 -07001821 bool on_dfl, bool reset_dev, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001822{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001823 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1824 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo182446d2013-08-08 20:11:24 -04001825 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001826 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001827 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001828 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001829
Tejun Heo69d7fde2015-08-18 14:55:36 -07001830 if (val < min || val > max)
1831 return -ERANGE;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001832
1833 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001834 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001835 if (!cfqgd) {
1836 ret = -EINVAL;
1837 goto out;
1838 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001839
1840 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001841 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001842 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001843 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001844
Sasha Levinb67bfe02013-02-27 17:06:00 -08001845 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001846 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001847
Tejun Heoe71357e2013-01-09 08:05:10 -08001848 if (!cfqg)
1849 continue;
1850
1851 if (!is_leaf_weight) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001852 if (reset_dev)
1853 cfqg->dev_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001854 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001855 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001856 } else {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001857 if (reset_dev)
1858 cfqg->dev_leaf_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001859 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001860 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001861 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001862 }
1863
Jens Axboeae994ea2015-06-20 10:26:50 -06001864out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001865 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001866 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001867}
1868
Tejun Heo182446d2013-08-08 20:11:24 -04001869static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1870 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001871{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001872 return __cfq_set_weight(css, val, false, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001873}
1874
Tejun Heo182446d2013-08-08 20:11:24 -04001875static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1876 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001877{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001878 return __cfq_set_weight(css, val, false, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001879}
1880
Tejun Heo2da8ca82013-12-05 12:28:04 -05001881static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001882{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001883 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1884 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001885 return 0;
1886}
1887
Tejun Heo2da8ca82013-12-05 12:28:04 -05001888static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001889{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001890 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1891 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001892 return 0;
1893}
1894
Tejun Heo43114012013-01-09 08:05:13 -08001895static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1896 struct blkg_policy_data *pd, int off)
1897{
Tejun Heof12c74c2015-08-18 14:55:23 -07001898 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1899 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001900 return __blkg_prfill_u64(sf, pd, sum);
1901}
1902
1903static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1904 struct blkg_policy_data *pd, int off)
1905{
Tejun Heof12c74c2015-08-18 14:55:23 -07001906 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1907 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001908 return __blkg_prfill_rwstat(sf, pd, &sum);
1909}
1910
Tejun Heo2da8ca82013-12-05 12:28:04 -05001911static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001912{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001913 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1914 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1915 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001916 return 0;
1917}
1918
Tejun Heo2da8ca82013-12-05 12:28:04 -05001919static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001920{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001921 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1922 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1923 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001924 return 0;
1925}
1926
Tejun Heo702747c2015-08-18 14:55:25 -07001927static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1928 int off)
1929{
1930 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1931
1932 return __blkg_prfill_u64(sf, pd, sum >> 9);
1933}
1934
1935static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1936{
1937 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1938 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1939 return 0;
1940}
1941
1942static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
1943 struct blkg_policy_data *pd, int off)
1944{
1945 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
1946 offsetof(struct blkcg_gq, stat_bytes));
1947 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
1948 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
1949
1950 return __blkg_prfill_u64(sf, pd, sum >> 9);
1951}
1952
1953static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1954{
1955 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1956 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
1957 false);
1958 return 0;
1959}
1960
Tejun Heo60c2bc22012-04-01 14:38:43 -07001961#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001962static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1963 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001964{
Tejun Heof95a04a2012-04-16 13:57:26 -07001965 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001966 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001967 u64 v = 0;
1968
1969 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001970 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06001971 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001972 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001973 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001974 return 0;
1975}
1976
1977/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001978static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001979{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001980 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1981 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1982 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001983 return 0;
1984}
1985#endif /* CONFIG_DEBUG_BLK_CGROUP */
1986
Tejun Heo880f50e2015-08-18 14:55:30 -07001987static struct cftype cfq_blkcg_legacy_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001988 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07001989 {
1990 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08001991 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001992 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04001993 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001994 },
1995 {
1996 .name = "weight",
1997 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05001998 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08001999 .write_u64 = cfq_set_leaf_weight,
2000 },
2001
2002 /* no such mapping necessary for !roots */
2003 {
2004 .name = "weight_device",
2005 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002006 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002007 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002008 },
2009 {
2010 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08002011 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002012 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07002013 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002014 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08002015
2016 {
2017 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002018 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002019 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08002020 },
2021 {
2022 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002023 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08002024 .write_u64 = cfq_set_leaf_weight,
2025 },
2026
Tejun Heo43114012013-01-09 08:05:13 -08002027 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002028 {
2029 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002030 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002031 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002032 },
2033 {
2034 .name = "sectors",
Tejun Heo702747c2015-08-18 14:55:25 -07002035 .seq_show = cfqg_print_stat_sectors,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002036 },
2037 {
2038 .name = "io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07002039 .private = (unsigned long)&blkcg_policy_cfq,
2040 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002041 },
2042 {
2043 .name = "io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07002044 .private = (unsigned long)&blkcg_policy_cfq,
2045 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002046 },
2047 {
2048 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002049 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002050 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002051 },
2052 {
2053 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002054 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002055 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002056 },
2057 {
2058 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002059 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002060 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002061 },
2062 {
2063 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002064 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002065 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002066 },
Tejun Heo43114012013-01-09 08:05:13 -08002067
2068 /* the same statictics which cover the cfqg and its descendants */
2069 {
2070 .name = "time_recursive",
2071 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002072 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002073 },
2074 {
2075 .name = "sectors_recursive",
Tejun Heo702747c2015-08-18 14:55:25 -07002076 .seq_show = cfqg_print_stat_sectors_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002077 },
2078 {
2079 .name = "io_service_bytes_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002080 .private = (unsigned long)&blkcg_policy_cfq,
2081 .seq_show = blkg_print_stat_bytes_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002082 },
2083 {
2084 .name = "io_serviced_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002085 .private = (unsigned long)&blkcg_policy_cfq,
2086 .seq_show = blkg_print_stat_ios_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002087 },
2088 {
2089 .name = "io_service_time_recursive",
2090 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002091 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002092 },
2093 {
2094 .name = "io_wait_time_recursive",
2095 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002096 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002097 },
2098 {
2099 .name = "io_merged_recursive",
2100 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002101 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002102 },
2103 {
2104 .name = "io_queued_recursive",
2105 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002106 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002107 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002108#ifdef CONFIG_DEBUG_BLK_CGROUP
2109 {
2110 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002111 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002112 },
2113 {
2114 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002115 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002116 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002117 },
2118 {
2119 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002120 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002121 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002122 },
2123 {
2124 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002125 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002126 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002127 },
2128 {
2129 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002130 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002131 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002132 },
2133 {
2134 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002135 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002136 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002137 },
2138#endif /* CONFIG_DEBUG_BLK_CGROUP */
2139 { } /* terminate */
2140};
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002141
2142static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2143{
2144 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2145 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2146
2147 seq_printf(sf, "default %u\n", cgd->weight);
2148 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2149 &blkcg_policy_cfq, 0, false);
2150 return 0;
2151}
2152
2153static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2154 char *buf, size_t nbytes, loff_t off)
2155{
2156 char *endp;
2157 int ret;
2158 u64 v;
2159
2160 buf = strim(buf);
2161
2162 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2163 v = simple_strtoull(buf, &endp, 0);
2164 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07002165 ret = __cfq_set_weight(of_css(of), v, true, false, false);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002166 return ret ?: nbytes;
2167 }
2168
2169 /* "MAJ:MIN WEIGHT" */
2170 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2171}
2172
2173static struct cftype cfq_blkcg_files[] = {
2174 {
2175 .name = "weight",
2176 .flags = CFTYPE_NOT_ON_ROOT,
2177 .seq_show = cfq_print_weight_on_dfl,
2178 .write = cfq_set_weight_on_dfl,
2179 },
2180 { } /* terminate */
2181};
2182
Vivek Goyal25fb5162009-12-03 12:59:46 -05002183#else /* GROUP_IOSCHED */
Tejun Heoae118892015-08-18 14:55:20 -07002184static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2185 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002186{
Tejun Heof51b8022012-03-05 13:15:05 -08002187 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002188}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002189
Vivek Goyal25fb5162009-12-03 12:59:46 -05002190static inline void
2191cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2192 cfqq->cfqg = cfqg;
2193}
2194
2195#endif /* GROUP_IOSCHED */
2196
Jens Axboe498d3aa22007-04-26 12:54:48 +02002197/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002198 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002199 * requests waiting to be processed. It is sorted in the order that
2200 * we will service the queues.
2201 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002202static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002203 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002204{
Jens Axboe08717142008-01-28 11:38:15 +01002205 struct rb_node **p, *parent;
2206 struct cfq_queue *__cfqq;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002207 u64 rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002208 struct cfq_rb_root *st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002209 bool leftmost = true;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002210 int new_cfqq = 1;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002211 u64 now = ktime_get_ns();
Vivek Goyalae30c282009-12-03 12:59:55 -05002212
Vivek Goyal34b98d02012-10-03 16:56:58 -04002213 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002214 if (cfq_class_idle(cfqq)) {
2215 rb_key = CFQ_IDLE_DELAY;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07002216 parent = st->rb_rightmost;
Jens Axboe08717142008-01-28 11:38:15 +01002217 if (parent && parent != &cfqq->rb_node) {
2218 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2219 rb_key += __cfqq->rb_key;
2220 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002221 rb_key += now;
Jens Axboe08717142008-01-28 11:38:15 +01002222 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002223 /*
2224 * Get our rb key offset. Subtract any residual slice
2225 * value carried from last service. A negative resid
2226 * count indicates slice overrun, and this should position
2227 * the next service time further away in the tree.
2228 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002229 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
Jens Axboeb9c89462009-10-06 20:53:44 +02002230 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002231 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002232 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002233 rb_key = -NSEC_PER_SEC;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002234 __cfqq = cfq_rb_first(st);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002235 rb_key += __cfqq ? __cfqq->rb_key : now;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002236 }
Jens Axboed9e76202007-04-20 14:27:50 +02002237
2238 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002239 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002240 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002241 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002242 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002243 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002244 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002245
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002246 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2247 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002248 }
Jens Axboed9e76202007-04-20 14:27:50 +02002249
Jens Axboe08717142008-01-28 11:38:15 +01002250 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002251 cfqq->service_tree = st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002252 p = &st->rb.rb_root.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002253 while (*p) {
2254 parent = *p;
2255 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2256
Jens Axboe0c534e02007-04-18 20:01:57 +02002257 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002258 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002259 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002260 if (rb_key < __cfqq->rb_key)
Vivek Goyal1f23f122012-10-03 16:57:00 -04002261 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002262 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002263 p = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002264 leftmost = false;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002265 }
Jens Axboed9e76202007-04-20 14:27:50 +02002266 }
2267
2268 cfqq->rb_key = rb_key;
2269 rb_link_node(&cfqq->rb_node, parent, p);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002270 rb_insert_color_cached(&cfqq->rb_node, &st->rb, leftmost);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002271 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002272 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002273 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002274 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Jens Axboea36e71f2009-04-15 12:15:11 +02002277static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002278cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2279 sector_t sector, struct rb_node **ret_parent,
2280 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002281{
Jens Axboea36e71f2009-04-15 12:15:11 +02002282 struct rb_node **p, *parent;
2283 struct cfq_queue *cfqq = NULL;
2284
2285 parent = NULL;
2286 p = &root->rb_node;
2287 while (*p) {
2288 struct rb_node **n;
2289
2290 parent = *p;
2291 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2292
2293 /*
2294 * Sort strictly based on sector. Smallest to the left,
2295 * largest to the right.
2296 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002297 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002298 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002299 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002300 n = &(*p)->rb_left;
2301 else
2302 break;
2303 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002304 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002305 }
2306
2307 *ret_parent = parent;
2308 if (rb_link)
2309 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002310 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002311}
2312
2313static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2314{
Jens Axboea36e71f2009-04-15 12:15:11 +02002315 struct rb_node **p, *parent;
2316 struct cfq_queue *__cfqq;
2317
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002318 if (cfqq->p_root) {
2319 rb_erase(&cfqq->p_node, cfqq->p_root);
2320 cfqq->p_root = NULL;
2321 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002322
2323 if (cfq_class_idle(cfqq))
2324 return;
2325 if (!cfqq->next_rq)
2326 return;
2327
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002328 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002329 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2330 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002331 if (!__cfqq) {
2332 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002333 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2334 } else
2335 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002336}
2337
Jens Axboe498d3aa22007-04-26 12:54:48 +02002338/*
2339 * Update cfqq's position in the service tree.
2340 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002341static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002342{
Jens Axboe6d048f52007-04-25 12:44:27 +02002343 /*
2344 * Resorting requires the cfqq to be on the RR list already.
2345 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002346 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002347 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002348 cfq_prio_tree_add(cfqd, cfqq);
2349 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002350}
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/*
2353 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002354 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 */
Jens Axboefebffd62008-01-28 13:19:43 +01002356static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Jens Axboe7b679132008-05-30 12:23:07 +02002358 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002359 BUG_ON(cfq_cfqq_on_rr(cfqq));
2360 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002362 if (cfq_cfqq_sync(cfqq))
2363 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Jens Axboeedd75ff2007-04-19 12:03:34 +02002365 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
Jens Axboe498d3aa22007-04-26 12:54:48 +02002368/*
2369 * Called when the cfqq no longer has requests pending, remove it from
2370 * the service tree.
2371 */
Jens Axboefebffd62008-01-28 13:19:43 +01002372static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Jens Axboe7b679132008-05-30 12:23:07 +02002374 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002375 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2376 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002378 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2379 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2380 cfqq->service_tree = NULL;
2381 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002382 if (cfqq->p_root) {
2383 rb_erase(&cfqq->p_node, cfqq->p_root);
2384 cfqq->p_root = NULL;
2385 }
Jens Axboed9e76202007-04-20 14:27:50 +02002386
Justin TerAvest8184f932011-03-17 16:12:36 +01002387 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 BUG_ON(!cfqd->busy_queues);
2389 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002390 if (cfq_cfqq_sync(cfqq))
2391 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392}
2393
2394/*
2395 * rb tree support functions
2396 */
Jens Axboefebffd62008-01-28 13:19:43 +01002397static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Jens Axboe5e705372006-07-13 12:39:25 +02002399 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002400 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Jens Axboeb4878f22005-10-20 16:42:29 +02002402 BUG_ON(!cfqq->queued[sync]);
2403 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Jens Axboe5e705372006-07-13 12:39:25 +02002405 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Vivek Goyalf04a6422009-12-03 12:59:40 -05002407 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2408 /*
2409 * Queue will be deleted from service tree when we actually
2410 * expire it later. Right now just remove it from prio tree
2411 * as it is empty.
2412 */
2413 if (cfqq->p_root) {
2414 rb_erase(&cfqq->p_node, cfqq->p_root);
2415 cfqq->p_root = NULL;
2416 }
2417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Jens Axboe5e705372006-07-13 12:39:25 +02002420static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Jens Axboe5e705372006-07-13 12:39:25 +02002422 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002424 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Jens Axboe5380a102006-07-13 12:37:56 +02002426 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Jeff Moyer796d5112011-06-02 21:19:05 +02002428 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002429
2430 if (!cfq_cfqq_on_rr(cfqq))
2431 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002432
2433 /*
2434 * check if this request is a better next-serve candidate
2435 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002436 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002437 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002438
2439 /*
2440 * adjust priority tree position, if ->next_rq changes
2441 */
2442 if (prev != cfqq->next_rq)
2443 cfq_prio_tree_add(cfqd, cfqq);
2444
Jens Axboe5044eed2007-04-25 11:53:48 +02002445 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Jens Axboefebffd62008-01-28 13:19:43 +01002448static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
Jens Axboe5380a102006-07-13 12:37:56 +02002450 elv_rb_del(&cfqq->sort_list, rq);
2451 cfqq->queued[rq_is_sync(rq)]--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002452 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002453 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002454 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002455 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Jens Axboe206dc692006-03-28 13:03:44 +02002458static struct request *
2459cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Jens Axboe206dc692006-03-28 13:03:44 +02002461 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002462 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002463 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Jens Axboe4ac845a2008-01-24 08:44:49 +01002465 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002466 if (!cic)
2467 return NULL;
2468
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002469 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002470 if (cfqq)
2471 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 return NULL;
2474}
2475
Jens Axboe165125e2007-07-24 09:28:11 +02002476static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002477{
2478 struct cfq_data *cfqd = q->elevator->elevator_data;
2479
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002480 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002481 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002482 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002483
Tejun Heo5b936292009-05-07 22:24:38 +09002484 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002485}
2486
Jens Axboe165125e2007-07-24 09:28:11 +02002487static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Jens Axboe22e2c502005-06-27 10:55:12 +02002489 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002491 WARN_ON(!cfqd->rq_in_driver);
2492 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002493 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002494 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Jens Axboeb4878f22005-10-20 16:42:29 +02002497static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Jens Axboe5e705372006-07-13 12:39:25 +02002499 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002500
Jens Axboe5e705372006-07-13 12:39:25 +02002501 if (cfqq->next_rq == rq)
2502 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Jens Axboeb4878f22005-10-20 16:42:29 +02002504 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002505 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002506
Aaron Carroll45333d52008-08-26 15:52:36 +02002507 cfqq->cfqd->rq_queued--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002508 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002509 if (rq->cmd_flags & REQ_PRIO) {
2510 WARN_ON(!cfqq->prio_pending);
2511 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002515static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
Jens Axboe165125e2007-07-24 09:28:11 +02002516 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 struct cfq_data *cfqd = q->elevator->elevator_data;
2519 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Jens Axboe206dc692006-03-28 13:03:44 +02002521 __rq = cfq_find_rq_fmerge(cfqd, bio);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002522 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002523 *req = __rq;
2524 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526
2527 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
Jens Axboe165125e2007-07-24 09:28:11 +02002530static void cfq_merged_request(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002531 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Jens Axboe21183b02006-07-13 12:33:14 +02002533 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002534 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Jens Axboe5e705372006-07-13 12:39:25 +02002536 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Divyesh Shah812d4022010-04-08 21:14:23 -07002540static void cfq_bio_merged(struct request_queue *q, struct request *req,
2541 struct bio *bio)
2542{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002543 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
Divyesh Shah812d4022010-04-08 21:14:23 -07002544}
2545
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546static void
Jens Axboe165125e2007-07-24 09:28:11 +02002547cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct request *next)
2549{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002550 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002551 struct cfq_data *cfqd = q->elevator->elevator_data;
2552
Jens Axboe22e2c502005-06-27 10:55:12 +02002553 /*
2554 * reposition in fifo if next is older than rq
2555 */
2556 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002557 next->fifo_time < rq->fifo_time &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002558 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002559 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002560 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002561 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002562
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002563 if (cfqq->next_rq == next)
2564 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002565 cfq_remove_request(next);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002566 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002567
2568 cfqq = RQ_CFQQ(next);
2569 /*
2570 * all requests of this queue are merged to other queues, delete it
2571 * from the service tree. If it's the active_queue,
2572 * cfq_dispatch_requests() will choose to expire it or do idle
2573 */
2574 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2575 cfqq != cfqd->active_queue)
2576 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002577}
2578
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002579static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2580 struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +01002581{
2582 struct cfq_data *cfqd = q->elevator->elevator_data;
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002583 bool is_sync = op_is_sync(bio->bi_opf);
Tejun Heoc5869802011-12-14 00:33:41 +01002584 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002585 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002586
2587 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002588 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002589 */
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002590 if (is_sync && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002591 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002592
2593 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002594 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002595 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002596 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002597 cic = cfq_cic_lookup(cfqd, current->io_context);
2598 if (!cic)
2599 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002600
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002601 cfqq = cic_to_cfqq(cic, is_sync);
Jens Axboea6151c32009-10-07 20:02:57 +02002602 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002603}
2604
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002605static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2606 struct request *next)
2607{
2608 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2609}
2610
Divyesh Shah812df482010-04-08 21:15:35 -07002611static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2612{
Jan Kara91148322016-06-08 15:11:39 +02002613 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002614 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002615}
2616
Jens Axboefebffd62008-01-28 13:19:43 +01002617static void __cfq_set_active_queue(struct cfq_data *cfqd,
2618 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002619{
2620 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002621 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002622 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002623 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002624 cfqq->slice_start = 0;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002625 cfqq->dispatch_start = ktime_get_ns();
Justin TerAvest62a37f62011-03-23 08:25:44 +01002626 cfqq->allocated_slice = 0;
2627 cfqq->slice_end = 0;
2628 cfqq->slice_dispatch = 0;
2629 cfqq->nr_sectors = 0;
2630
2631 cfq_clear_cfqq_wait_request(cfqq);
2632 cfq_clear_cfqq_must_dispatch(cfqq);
2633 cfq_clear_cfqq_must_alloc_slice(cfqq);
2634 cfq_clear_cfqq_fifo_expire(cfqq);
2635 cfq_mark_cfqq_slice_new(cfqq);
2636
2637 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002638 }
2639
2640 cfqd->active_queue = cfqq;
2641}
2642
2643/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002644 * current cfqq expired its slice (or was too idle), select new one
2645 */
2646static void
2647__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002648 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002649{
Jens Axboe7b679132008-05-30 12:23:07 +02002650 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2651
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002652 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002653 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002654
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002655 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002656 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002657
2658 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002659 * If this cfqq is shared between multiple processes, check to
2660 * make sure that those processes are still issuing I/Os within
2661 * the mean seek distance. If not, it may be time to break the
2662 * queues apart again.
2663 */
2664 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2665 cfq_mark_cfqq_split_coop(cfqq);
2666
2667 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002668 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002669 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002670 if (timed_out) {
2671 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002672 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002673 else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002674 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
Jan Kara93fdf142016-06-28 09:04:00 +02002675 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
Jens Axboe7b679132008-05-30 12:23:07 +02002676 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002677
Vivek Goyale5ff0822010-04-26 19:25:11 +02002678 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002679
Vivek Goyalf04a6422009-12-03 12:59:40 -05002680 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2681 cfq_del_cfqq_rr(cfqd, cfqq);
2682
Jens Axboeedd75ff2007-04-19 12:03:34 +02002683 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002684
2685 if (cfqq == cfqd->active_queue)
2686 cfqd->active_queue = NULL;
2687
2688 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002689 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002690 cfqd->active_cic = NULL;
2691 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002692}
2693
Vivek Goyale5ff0822010-04-26 19:25:11 +02002694static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002695{
2696 struct cfq_queue *cfqq = cfqd->active_queue;
2697
2698 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002699 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002700}
2701
Jens Axboe498d3aa22007-04-26 12:54:48 +02002702/*
2703 * Get next queue for service. Unless we have a queue preemption,
2704 * we'll simply select the first cfqq in the service tree.
2705 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002706static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002707{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002708 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2709 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002710
Vivek Goyalf04a6422009-12-03 12:59:40 -05002711 if (!cfqd->rq_queued)
2712 return NULL;
2713
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002714 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002715 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002716 return NULL;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002717 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002718 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002719 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002720}
2721
Vivek Goyalf04a6422009-12-03 12:59:40 -05002722static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2723{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002724 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002725 struct cfq_queue *cfqq;
2726 int i, j;
2727 struct cfq_rb_root *st;
2728
2729 if (!cfqd->rq_queued)
2730 return NULL;
2731
Vivek Goyal25fb5162009-12-03 12:59:46 -05002732 cfqg = cfq_get_next_cfqg(cfqd);
2733 if (!cfqg)
2734 return NULL;
2735
Markus Elfring1cf41752017-01-21 22:44:07 +01002736 for_each_cfqg_st(cfqg, i, j, st) {
2737 cfqq = cfq_rb_first(st);
2738 if (cfqq)
Vivek Goyalf04a6422009-12-03 12:59:40 -05002739 return cfqq;
Markus Elfring1cf41752017-01-21 22:44:07 +01002740 }
Vivek Goyalf04a6422009-12-03 12:59:40 -05002741 return NULL;
2742}
2743
Jens Axboe498d3aa22007-04-26 12:54:48 +02002744/*
2745 * Get and set a new active queue for service.
2746 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002747static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2748 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002749{
Jens Axboee00ef792009-11-04 08:54:55 +01002750 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002751 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002752
Jens Axboe22e2c502005-06-27 10:55:12 +02002753 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002754 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002755}
2756
Jens Axboed9e76202007-04-20 14:27:50 +02002757static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2758 struct request *rq)
2759{
Tejun Heo83096eb2009-05-07 22:24:39 +09002760 if (blk_rq_pos(rq) >= cfqd->last_position)
2761 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002762 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002763 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002764}
2765
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002766static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002767 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002768{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002769 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002770}
2771
Jens Axboea36e71f2009-04-15 12:15:11 +02002772static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2773 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002774{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002775 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002776 struct rb_node *parent, *node;
2777 struct cfq_queue *__cfqq;
2778 sector_t sector = cfqd->last_position;
2779
2780 if (RB_EMPTY_ROOT(root))
2781 return NULL;
2782
2783 /*
2784 * First, if we find a request starting at the end of the last
2785 * request, choose it.
2786 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002787 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002788 if (__cfqq)
2789 return __cfqq;
2790
2791 /*
2792 * If the exact sector wasn't found, the parent of the NULL leaf
2793 * will contain the closest sector.
2794 */
2795 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002796 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002797 return __cfqq;
2798
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002799 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002800 node = rb_next(&__cfqq->p_node);
2801 else
2802 node = rb_prev(&__cfqq->p_node);
2803 if (!node)
2804 return NULL;
2805
2806 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002807 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002808 return __cfqq;
2809
2810 return NULL;
2811}
2812
2813/*
2814 * cfqd - obvious
2815 * cur_cfqq - passed in so that we don't decide that the current queue is
2816 * closely cooperating with itself.
2817 *
2818 * So, basically we're assuming that that cur_cfqq has dispatched at least
2819 * one request, and that cfqd->last_position reflects a position on the disk
2820 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2821 * assumption.
2822 */
2823static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002824 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002825{
2826 struct cfq_queue *cfqq;
2827
Divyesh Shah39c01b22010-03-25 15:45:57 +01002828 if (cfq_class_idle(cur_cfqq))
2829 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002830 if (!cfq_cfqq_sync(cur_cfqq))
2831 return NULL;
2832 if (CFQQ_SEEKY(cur_cfqq))
2833 return NULL;
2834
Jens Axboea36e71f2009-04-15 12:15:11 +02002835 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002836 * Don't search priority tree if it's the only queue in the group.
2837 */
2838 if (cur_cfqq->cfqg->nr_cfqq == 1)
2839 return NULL;
2840
2841 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002842 * We should notice if some of the queues are cooperating, eg
2843 * working closely on the same area of the disk. In that case,
2844 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002845 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002846 cfqq = cfqq_close(cfqd, cur_cfqq);
2847 if (!cfqq)
2848 return NULL;
2849
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002850 /* If new queue belongs to different cfq_group, don't choose it */
2851 if (cur_cfqq->cfqg != cfqq->cfqg)
2852 return NULL;
2853
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002854 /*
2855 * It only makes sense to merge sync queues.
2856 */
2857 if (!cfq_cfqq_sync(cfqq))
2858 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002859 if (CFQQ_SEEKY(cfqq))
2860 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002861
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002862 /*
2863 * Do not merge queues of different priority classes
2864 */
2865 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2866 return NULL;
2867
Jens Axboea36e71f2009-04-15 12:15:11 +02002868 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002869}
2870
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002871/*
2872 * Determine whether we should enforce idle window for this queue.
2873 */
2874
2875static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2876{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002877 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002878 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002879
Vivek Goyal34b98d02012-10-03 16:56:58 -04002880 BUG_ON(!st);
2881 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002882
Vivek Goyalb6508c12010-08-23 12:23:33 +02002883 if (!cfqd->cfq_slice_idle)
2884 return false;
2885
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002886 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002887 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002888 return false;
2889
2890 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002891 if (cfq_cfqq_idle_window(cfqq) &&
2892 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002893 return true;
2894
2895 /*
2896 * Otherwise, we do only if they are the last ones
2897 * in their service tree.
2898 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002899 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2900 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002901 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002902 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002903 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002904}
2905
Jens Axboe6d048f52007-04-25 12:44:27 +02002906static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002907{
Jens Axboe17926692007-01-19 11:59:30 +11002908 struct cfq_queue *cfqq = cfqd->active_queue;
Jan Karae7954212016-01-12 16:24:15 +01002909 struct cfq_rb_root *st = cfqq->service_tree;
Tejun Heoc5869802011-12-14 00:33:41 +01002910 struct cfq_io_cq *cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002911 u64 sl, group_idle = 0;
2912 u64 now = ktime_get_ns();
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002913
Jens Axboea68bbddba2008-09-24 13:03:33 +02002914 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002915 * SSD device without seek penalty, disable idling. But only do so
2916 * for devices that support queuing, otherwise we still have a problem
2917 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002918 */
Ritesh Harjanib3193bc2017-08-09 18:28:32 +05302919 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag &&
2920 !cfqd->cfq_group_idle)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002921 return;
2922
Jens Axboedd67d052006-06-21 09:36:18 +02002923 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002924 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002925
2926 /*
2927 * idle is disabled, either manually or by past process history
2928 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002929 if (!cfq_should_idle(cfqd, cfqq)) {
2930 /* no queue idling. Check for group idling */
2931 if (cfqd->cfq_group_idle)
2932 group_idle = cfqd->cfq_group_idle;
2933 else
2934 return;
2935 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002936
Jens Axboe22e2c502005-06-27 10:55:12 +02002937 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002938 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002939 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002940 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002941 return;
2942
2943 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002944 * task has exited, don't wait
2945 */
Jens Axboe206dc692006-03-28 13:03:44 +02002946 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002947 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002948 return;
2949
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002950 /*
2951 * If our average think time is larger than the remaining time
2952 * slice, then don't idle. This avoids overrunning the allotted
2953 * time slice.
2954 */
Shaohua Li383cd722011-07-12 14:24:35 +02002955 if (sample_valid(cic->ttime.ttime_samples) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002956 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2957 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
Shaohua Li383cd722011-07-12 14:24:35 +02002958 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002959 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002960 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002961
Jan Karae7954212016-01-12 16:24:15 +01002962 /*
2963 * There are other queues in the group or this is the only group and
2964 * it has too big thinktime, don't do group idle.
2965 */
2966 if (group_idle &&
2967 (cfqq->cfqg->nr_cfqq > 1 ||
2968 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002969 return;
2970
Jens Axboe3b181522005-06-27 10:56:24 +02002971 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002972
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002973 if (group_idle)
2974 sl = cfqd->cfq_group_idle;
2975 else
2976 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002977
Jan Kara91148322016-06-08 15:11:39 +02002978 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2979 HRTIMER_MODE_REL);
Tejun Heo155fead2012-04-01 14:38:44 -07002980 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002981 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002982 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Jens Axboe498d3aa22007-04-26 12:54:48 +02002985/*
2986 * Move request from internal lists to the request queue dispatch list.
2987 */
Jens Axboe165125e2007-07-24 09:28:11 +02002988static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Jens Axboe3ed9a292007-04-23 08:33:33 +02002990 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02002991 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002992
Jens Axboe7b679132008-05-30 12:23:07 +02002993 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
2994
Jeff Moyer06d21882009-09-11 17:08:59 +02002995 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02002996 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02002997 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002998 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02002999 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02003000
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003001 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02003002 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
3005/*
3006 * return expired entry, or NULL to just start from scratch in rbtree
3007 */
Jens Axboefebffd62008-01-28 13:19:43 +01003008static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Jens Axboe30996f42009-10-05 11:03:39 +02003010 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Jens Axboe3b181522005-06-27 10:56:24 +02003012 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11003014
3015 cfq_mark_cfqq_fifo_expire(cfqq);
3016
Jens Axboe89850f72006-07-22 16:48:31 +02003017 if (list_empty(&cfqq->fifo))
3018 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Jens Axboe89850f72006-07-22 16:48:31 +02003020 rq = rq_entry_fifo(cfqq->fifo.next);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003021 if (ktime_get_ns() < rq->fifo_time)
Jens Axboe7b679132008-05-30 12:23:07 +02003022 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Jens Axboe6d048f52007-04-25 12:44:27 +02003024 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Jens Axboe22e2c502005-06-27 10:55:12 +02003027static inline int
3028cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3029{
3030 const int base_rq = cfqd->cfq_slice_async_rq;
3031
3032 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3033
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02003034 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003035}
3036
3037/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003038 * Must be called with the queue_lock held.
3039 */
3040static int cfqq_process_refs(struct cfq_queue *cfqq)
3041{
3042 int process_refs, io_refs;
3043
3044 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01003045 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003046 BUG_ON(process_refs < 0);
3047 return process_refs;
3048}
3049
3050static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3051{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003052 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003053 struct cfq_queue *__cfqq;
3054
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003055 /*
3056 * If there are no process references on the new_cfqq, then it is
3057 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3058 * chain may have dropped their last reference (not just their
3059 * last process reference).
3060 */
3061 if (!cfqq_process_refs(new_cfqq))
3062 return;
3063
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003064 /* Avoid a circular list and skip interim queue merges */
3065 while ((__cfqq = new_cfqq->new_cfqq)) {
3066 if (__cfqq == cfqq)
3067 return;
3068 new_cfqq = __cfqq;
3069 }
3070
3071 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003072 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003073 /*
3074 * If the process for the cfqq has gone away, there is no
3075 * sense in merging the queues.
3076 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003077 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003078 return;
3079
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003080 /*
3081 * Merge in the direction of the lesser amount of work.
3082 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003083 if (new_process_refs >= process_refs) {
3084 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003085 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003086 } else {
3087 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003088 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003089 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003090}
3091
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003092static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003093 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003094{
3095 struct cfq_queue *queue;
3096 int i;
3097 bool key_valid = false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003098 u64 lowest_key = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003099 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3100
Vivek Goyal65b32a52009-12-16 17:52:59 -05003101 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3102 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003103 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003104 if (queue &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003105 (!key_valid || queue->rb_key < lowest_key)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003106 lowest_key = queue->rb_key;
3107 cur_best = i;
3108 key_valid = true;
3109 }
3110 }
3111
3112 return cur_best;
3113}
3114
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003115static void
3116choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003117{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003118 u64 slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003119 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003120 struct cfq_rb_root *st;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003121 u64 group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003122 enum wl_class_t original_class = cfqd->serving_wl_class;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003123 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003124
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003125 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003126 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003127 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003128 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003129 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003130 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003131 cfqd->serving_wl_class = IDLE_WORKLOAD;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003132 cfqd->workload_expires = now + jiffies_to_nsecs(1);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003133 return;
3134 }
3135
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003136 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003137 goto new_workload;
3138
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003139 /*
3140 * For RT and BE, we have to choose also the type
3141 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3142 * expiration time
3143 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003144 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003145 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003146
3147 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003148 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003149 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003150 if (count && !(now > cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003151 return;
3152
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003153new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003154 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003155 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003156 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003157 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003158 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003159
3160 /*
3161 * the workload slice is computed as a fraction of target latency
3162 * proportional to the number of queues in that workload, over
3163 * all the queues in the same priority class
3164 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003165 group_slice = cfq_group_slice(cfqd, cfqg);
3166
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003167 slice = div_u64(group_slice * count,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003168 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3169 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003170 cfqg)));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003171
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003172 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003173 u64 tmp;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003174
3175 /*
3176 * Async queues are currently system wide. Just taking
3177 * proportion of queues with-in same group will lead to higher
3178 * async ratio system wide as generally root group is going
3179 * to have higher weight. A more accurate thing would be to
3180 * calculate system wide asnc/sync ratio.
3181 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003182 tmp = cfqd->cfq_target_latency *
3183 cfqg_busy_async_queues(cfqd, cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003184 tmp = div_u64(tmp, cfqd->busy_queues);
3185 slice = min_t(u64, slice, tmp);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003186
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003187 /* async workload slice is scaled down according to
3188 * the sync/async slice ratio. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003189 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003190 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003191 /* sync workload slice is at least 2 * cfq_slice_idle */
3192 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3193
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003194 slice = max_t(u64, slice, CFQ_MIN_TT);
3195 cfq_log(cfqd, "workload slice:%llu", slice);
3196 cfqd->workload_expires = now + slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003197}
3198
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003199static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3200{
3201 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003202 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003203
Davidlohr Bueso09663c82017-09-08 16:15:05 -07003204 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003205 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003206 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003207 update_min_vdisktime(st);
3208 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003209}
3210
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003211static void cfq_choose_cfqg(struct cfq_data *cfqd)
3212{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003213 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003214 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003215
3216 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003217
3218 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003219 if (cfqg->saved_wl_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003220 cfqd->workload_expires = now + cfqg->saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003221 cfqd->serving_wl_type = cfqg->saved_wl_type;
3222 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003223 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003224 cfqd->workload_expires = now - 1;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003225
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003226 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003227}
3228
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003229/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003230 * Select a queue for service. If we have a current active queue,
3231 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003232 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003233static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003234{
Jens Axboea36e71f2009-04-15 12:15:11 +02003235 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003236 u64 now = ktime_get_ns();
Jens Axboe22e2c502005-06-27 10:55:12 +02003237
3238 cfqq = cfqd->active_queue;
3239 if (!cfqq)
3240 goto new_queue;
3241
Vivek Goyalf04a6422009-12-03 12:59:40 -05003242 if (!cfqd->rq_queued)
3243 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003244
3245 /*
3246 * We were waiting for group to get backlogged. Expire the queue
3247 */
3248 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3249 goto expire;
3250
Jens Axboe22e2c502005-06-27 10:55:12 +02003251 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003252 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003253 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003254 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3255 /*
3256 * If slice had not expired at the completion of last request
3257 * we might not have turned on wait_busy flag. Don't expire
3258 * the queue yet. Allow the group to get backlogged.
3259 *
3260 * The very fact that we have used the slice, that means we
3261 * have been idling all along on this queue and it should be
3262 * ok to wait for this request to complete.
3263 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003264 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3265 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3266 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003267 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003268 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003269 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003270 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003271
3272 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003273 * The active queue has requests and isn't expired, allow it to
3274 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003275 */
Jens Axboedd67d052006-06-21 09:36:18 +02003276 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003277 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003278
3279 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003280 * If another queue has a request waiting within our mean seek
3281 * distance, let it run. The expire code will check for close
3282 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003283 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003284 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003285 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003286 if (new_cfqq) {
3287 if (!cfqq->new_cfqq)
3288 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003289 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003290 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003291
3292 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003293 * No requests pending. If the active queue still has requests in
3294 * flight or is idling for a new request, allow either of these
3295 * conditions to happen (or time out) before selecting a new queue.
3296 */
Jan Kara91148322016-06-08 15:11:39 +02003297 if (hrtimer_active(&cfqd->idle_slice_timer)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003298 cfqq = NULL;
3299 goto keep_queue;
3300 }
3301
Shaohua Li8e1ac662010-11-08 15:01:04 +01003302 /*
3303 * This is a deep seek queue, but the device is much faster than
3304 * the queue can deliver, don't idle
3305 **/
3306 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3307 (cfq_cfqq_slice_new(cfqq) ||
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003308 (cfqq->slice_end - now > now - cfqq->slice_start))) {
Shaohua Li8e1ac662010-11-08 15:01:04 +01003309 cfq_clear_cfqq_deep(cfqq);
3310 cfq_clear_cfqq_idle_window(cfqq);
3311 }
3312
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003313 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3314 cfqq = NULL;
3315 goto keep_queue;
3316 }
3317
3318 /*
3319 * If group idle is enabled and there are requests dispatched from
3320 * this group, wait for requests to complete.
3321 */
3322check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02003323 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3324 cfqq->cfqg->dispatched &&
3325 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003326 cfqq = NULL;
3327 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003328 }
3329
Jens Axboe3b181522005-06-27 10:56:24 +02003330expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003331 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003332new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003333 /*
3334 * Current queue expired. Check if we have to switch to a new
3335 * service tree
3336 */
3337 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003338 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003339
Jens Axboea36e71f2009-04-15 12:15:11 +02003340 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003341keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003342 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003343}
3344
Jens Axboefebffd62008-01-28 13:19:43 +01003345static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003346{
3347 int dispatched = 0;
3348
3349 while (cfqq->next_rq) {
3350 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3351 dispatched++;
3352 }
3353
3354 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003355
3356 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003357 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003358 return dispatched;
3359}
3360
Jens Axboe498d3aa22007-04-26 12:54:48 +02003361/*
3362 * Drain our current requests. Used for barriers and when switching
3363 * io schedulers on-the-fly.
3364 */
Jens Axboed9e76202007-04-20 14:27:50 +02003365static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003366{
Jens Axboe08717142008-01-28 11:38:15 +01003367 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003368 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003369
Divyesh Shah3440c492010-04-09 09:29:57 +02003370 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003371 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003372 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3373 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003374 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003375 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003376
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003377 BUG_ON(cfqd->busy_queues);
3378
Jeff Moyer69237152009-06-12 15:29:30 +02003379 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003380 return dispatched;
3381}
3382
Shaohua Liabc3c742010-03-01 09:20:54 +01003383static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3384 struct cfq_queue *cfqq)
3385{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003386 u64 now = ktime_get_ns();
3387
Shaohua Liabc3c742010-03-01 09:20:54 +01003388 /* the queue hasn't finished any request, can't estimate */
3389 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003390 return true;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003391 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01003392 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003393
Shaohua Lic1e44752010-11-08 15:01:02 +01003394 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003395}
3396
Jens Axboe0b182d62009-10-06 20:49:37 +02003397static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003398{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003399 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Glauber Costa3932a862016-09-22 20:59:59 -04003401 if (cfq_cfqq_must_dispatch(cfqq))
3402 return true;
3403
Jens Axboe2f5cb732009-04-07 08:51:19 +02003404 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003405 * Drain async requests before we start sync IO
3406 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003407 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003408 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003409
3410 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003411 * If this is an async queue and we have sync IO in flight, let it wait
3412 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003413 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003414 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003415
Shaohua Liabc3c742010-03-01 09:20:54 +01003416 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003417 if (cfq_class_idle(cfqq))
3418 max_dispatch = 1;
3419
3420 /*
3421 * Does this cfqq already have too much IO in flight?
3422 */
3423 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003424 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003425 /*
3426 * idle queue must always only have a single IO in flight
3427 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003428 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003429 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003430
Jens Axboe2f5cb732009-04-07 08:51:19 +02003431 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003432 * If there is only one sync queue
3433 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003434 * queue no dispatch limit. The reason is a sync queue can
3435 * preempt async queue, limiting the sync queue doesn't make
3436 * sense. This is useful for aiostress test.
3437 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003438 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3439 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003440
3441 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003442 * We have other queues, don't allow more IO from this one
3443 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003444 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3445 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003446 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003447
Jens Axboe2f5cb732009-04-07 08:51:19 +02003448 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003449 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003450 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003451 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003452 max_dispatch = -1;
3453 else
3454 /*
3455 * Normally we start throttling cfqq when cfq_quantum/2
3456 * requests have been dispatched. But we can drive
3457 * deeper queue depths at the beginning of slice
3458 * subjected to upper limit of cfq_quantum.
3459 * */
3460 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003461 }
3462
3463 /*
3464 * Async queues must wait a bit before being allowed dispatch.
3465 * We also ramp up the dispatch depth gradually for async IO,
3466 * based on the last sync IO we serviced
3467 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003468 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003469 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003470 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003471
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003472 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
Jens Axboee00c54c2009-10-04 20:36:19 +02003473 if (!depth && !cfqq->dispatched)
3474 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003475 if (depth < max_dispatch)
3476 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478
Jens Axboe0b182d62009-10-06 20:49:37 +02003479 /*
3480 * If we're below the current max, allow a dispatch
3481 */
3482 return cfqq->dispatched < max_dispatch;
3483}
3484
3485/*
3486 * Dispatch a request from cfqq, moving them to the request queue
3487 * dispatch list.
3488 */
3489static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3490{
3491 struct request *rq;
3492
3493 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3494
Glauber Costa3932a862016-09-22 20:59:59 -04003495 rq = cfq_check_fifo(cfqq);
3496 if (rq)
3497 cfq_mark_cfqq_must_dispatch(cfqq);
3498
Jens Axboe0b182d62009-10-06 20:49:37 +02003499 if (!cfq_may_dispatch(cfqd, cfqq))
3500 return false;
3501
3502 /*
3503 * follow expired path, else get first next available
3504 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003505 if (!rq)
3506 rq = cfqq->next_rq;
Glauber Costa3932a862016-09-22 20:59:59 -04003507 else
3508 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003509
3510 /*
3511 * insert request into driver dispatch list
3512 */
3513 cfq_dispatch_insert(cfqd->queue, rq);
3514
3515 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003516 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003517
Tejun Heoc5869802011-12-14 00:33:41 +01003518 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003519 cfqd->active_cic = cic;
3520 }
3521
3522 return true;
3523}
3524
3525/*
3526 * Find the cfqq that we need to service and move a request from that to the
3527 * dispatch list
3528 */
3529static int cfq_dispatch_requests(struct request_queue *q, int force)
3530{
3531 struct cfq_data *cfqd = q->elevator->elevator_data;
3532 struct cfq_queue *cfqq;
3533
3534 if (!cfqd->busy_queues)
3535 return 0;
3536
3537 if (unlikely(force))
3538 return cfq_forced_dispatch(cfqd);
3539
3540 cfqq = cfq_select_queue(cfqd);
3541 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003542 return 0;
3543
Jens Axboe2f5cb732009-04-07 08:51:19 +02003544 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003545 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003546 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003547 if (!cfq_dispatch_request(cfqd, cfqq))
3548 return 0;
3549
Jens Axboe2f5cb732009-04-07 08:51:19 +02003550 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003551 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003552
3553 /*
3554 * expire an async queue immediately if it has used up its slice. idle
3555 * queue always expire after 1 dispatch round.
3556 */
3557 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3558 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3559 cfq_class_idle(cfqq))) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003560 cfqq->slice_end = ktime_get_ns() + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003561 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003562 }
3563
Shan Weib217a902009-09-01 10:06:42 +02003564 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003565 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568/*
Jens Axboe5e705372006-07-13 12:39:25 +02003569 * task holds one reference to the queue, dropped when task exits. each rq
3570 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003572 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 * queue lock must be held here.
3574 */
3575static void cfq_put_queue(struct cfq_queue *cfqq)
3576{
Jens Axboe22e2c502005-06-27 10:55:12 +02003577 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003578 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003579
Shaohua Li30d7b942011-01-07 08:46:59 +01003580 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Shaohua Li30d7b942011-01-07 08:46:59 +01003582 cfqq->ref--;
3583 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return;
3585
Jens Axboe7b679132008-05-30 12:23:07 +02003586 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003588 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003589 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003591 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003592 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003593 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003594 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003595
Vivek Goyalf04a6422009-12-03 12:59:40 -05003596 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003598 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
Shaohua Lid02a2c02010-05-25 10:16:53 +02003601static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003602{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003603 struct cfq_queue *__cfqq, *next;
3604
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003605 /*
3606 * If this queue was scheduled to merge with another queue, be
3607 * sure to drop the reference taken on that queue (and others in
3608 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3609 */
3610 __cfqq = cfqq->new_cfqq;
3611 while (__cfqq) {
3612 if (__cfqq == cfqq) {
3613 WARN(1, "cfqq->new_cfqq loop detected\n");
3614 break;
3615 }
3616 next = __cfqq->new_cfqq;
3617 cfq_put_queue(__cfqq);
3618 __cfqq = next;
3619 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003620}
3621
3622static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3623{
3624 if (unlikely(cfqq == cfqd->active_queue)) {
3625 __cfq_slice_expired(cfqd, cfqq, 0);
3626 cfq_schedule_dispatch(cfqd);
3627 }
3628
3629 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003630
Jens Axboe89850f72006-07-22 16:48:31 +02003631 cfq_put_queue(cfqq);
3632}
3633
Tejun Heo9b84cac2011-12-14 00:33:42 +01003634static void cfq_init_icq(struct io_cq *icq)
3635{
3636 struct cfq_io_cq *cic = icq_to_cic(icq);
3637
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003638 cic->ttime.last_end_request = ktime_get_ns();
Tejun Heo9b84cac2011-12-14 00:33:42 +01003639}
3640
Tejun Heoc5869802011-12-14 00:33:41 +01003641static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003642{
Tejun Heoc5869802011-12-14 00:33:41 +01003643 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003644 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003645
Tejun Heo563180a2015-08-18 14:55:00 -07003646 if (cic_to_cfqq(cic, false)) {
3647 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3648 cic_set_cfqq(cic, NULL, false);
Jens Axboe89850f72006-07-22 16:48:31 +02003649 }
3650
Tejun Heo563180a2015-08-18 14:55:00 -07003651 if (cic_to_cfqq(cic, true)) {
3652 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3653 cic_set_cfqq(cic, NULL, true);
Jens Axboe89850f72006-07-22 16:48:31 +02003654 }
Jens Axboe89850f72006-07-22 16:48:31 +02003655}
3656
Tejun Heoabede6d2012-03-19 15:10:57 -07003657static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003658{
3659 struct task_struct *tsk = current;
3660 int ioprio_class;
3661
Jens Axboe3b181522005-06-27 10:56:24 +02003662 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003663 return;
3664
Tejun Heo598971b2012-03-19 15:10:58 -07003665 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003666 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003667 default:
3668 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
Bart Van Assche9b4f4342018-08-07 16:17:28 -07003669 /* fall through */
Jens Axboefe094d92008-01-31 13:08:54 +01003670 case IOPRIO_CLASS_NONE:
3671 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003672 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003673 */
3674 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003675 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003676 break;
3677 case IOPRIO_CLASS_RT:
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_RT;
3680 break;
3681 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003682 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003683 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3684 break;
3685 case IOPRIO_CLASS_IDLE:
3686 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3687 cfqq->ioprio = 7;
3688 cfq_clear_cfqq_idle_window(cfqq);
3689 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003690 }
3691
3692 /*
3693 * keep track of original prio settings in case we have to temporarily
3694 * elevate the priority of this queue
3695 */
3696 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -06003697 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02003698 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003699}
3700
Tejun Heo598971b2012-03-19 15:10:58 -07003701static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003702{
Tejun Heo598971b2012-03-19 15:10:58 -07003703 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003704 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003705 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003706
Tejun Heo598971b2012-03-19 15:10:58 -07003707 /*
3708 * Check whether ioprio has changed. The condition may trigger
3709 * spuriously on a newly created cic but there's no harm.
3710 */
3711 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003712 return;
3713
Tejun Heo563180a2015-08-18 14:55:00 -07003714 cfqq = cic_to_cfqq(cic, false);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003715 if (cfqq) {
Tejun Heo563180a2015-08-18 14:55:00 -07003716 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07003717 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
Tejun Heo563180a2015-08-18 14:55:00 -07003718 cic_set_cfqq(cic, cfqq, false);
Jens Axboe22e2c502005-06-27 10:55:12 +02003719 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003720
Tejun Heo563180a2015-08-18 14:55:00 -07003721 cfqq = cic_to_cfqq(cic, true);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003722 if (cfqq)
3723 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003724
3725 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003726}
3727
Jens Axboed5036d72009-06-26 10:44:34 +02003728static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003729 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003730{
3731 RB_CLEAR_NODE(&cfqq->rb_node);
3732 RB_CLEAR_NODE(&cfqq->p_node);
3733 INIT_LIST_HEAD(&cfqq->fifo);
3734
Shaohua Li30d7b942011-01-07 08:46:59 +01003735 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003736 cfqq->cfqd = cfqd;
3737
3738 cfq_mark_cfqq_prio_changed(cfqq);
3739
3740 if (is_sync) {
3741 if (!cfq_class_idle(cfqq))
3742 cfq_mark_cfqq_idle_window(cfqq);
3743 cfq_mark_cfqq_sync(cfqq);
3744 }
3745 cfqq->pid = pid;
3746}
3747
Vivek Goyal246103332009-12-03 12:59:51 -05003748#ifdef CONFIG_CFQ_GROUP_IOSCHED
Jan Kara142bbdf2017-04-04 14:31:30 +02003749static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003750{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003751 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo60a83702015-08-18 14:55:05 -07003752 struct cfq_queue *cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003753 uint64_t serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003754
Tejun Heo598971b2012-03-19 15:10:58 -07003755 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003756 serial_nr = bio_blkcg(bio)->css.serial_nr;
Tejun Heo598971b2012-03-19 15:10:58 -07003757 rcu_read_unlock();
3758
3759 /*
3760 * Check whether blkcg has changed. The condition may trigger
3761 * spuriously on a newly created cic but there's no harm.
3762 */
Tejun Heof4da8072014-09-08 08:15:20 +09003763 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Jan Kara142bbdf2017-04-04 14:31:30 +02003764 return;
Jens Axboe87760e52016-11-09 12:38:14 -07003765
3766 /*
Tejun Heo60a83702015-08-18 14:55:05 -07003767 * Drop reference to queues. New queues will be assigned in new
3768 * group upon arrival of fresh requests.
3769 */
3770 cfqq = cic_to_cfqq(cic, false);
3771 if (cfqq) {
3772 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3773 cic_set_cfqq(cic, NULL, false);
3774 cfq_put_queue(cfqq);
3775 }
3776
3777 cfqq = cic_to_cfqq(cic, true);
3778 if (cfqq) {
3779 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3780 cic_set_cfqq(cic, NULL, true);
3781 cfq_put_queue(cfqq);
Vivek Goyal246103332009-12-03 12:59:51 -05003782 }
Tejun Heo598971b2012-03-19 15:10:58 -07003783
Tejun Heof4da8072014-09-08 08:15:20 +09003784 cic->blkcg_serial_nr = serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003785}
Tejun Heo598971b2012-03-19 15:10:58 -07003786#else
Jan Kara142bbdf2017-04-04 14:31:30 +02003787static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003788{
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003789}
Vivek Goyal246103332009-12-03 12:59:51 -05003790#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3791
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003792static struct cfq_queue **
Tejun Heo60a83702015-08-18 14:55:05 -07003793cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003794{
Jens Axboefe094d92008-01-31 13:08:54 +01003795 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003796 case IOPRIO_CLASS_RT:
Tejun Heo60a83702015-08-18 14:55:05 -07003797 return &cfqg->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003798 case IOPRIO_CLASS_NONE:
3799 ioprio = IOPRIO_NORM;
3800 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003801 case IOPRIO_CLASS_BE:
Tejun Heo60a83702015-08-18 14:55:05 -07003802 return &cfqg->async_cfqq[1][ioprio];
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003803 case IOPRIO_CLASS_IDLE:
Tejun Heo60a83702015-08-18 14:55:05 -07003804 return &cfqg->async_idle_cfqq;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003805 default:
3806 BUG();
3807 }
3808}
3809
Jens Axboe15c31be2007-07-10 13:43:25 +02003810static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003811cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo2da8de02015-08-18 14:55:02 -07003812 struct bio *bio)
Jens Axboe15c31be2007-07-10 13:43:25 +02003813{
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003814 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3815 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Tejun Heod4aad7f2015-08-18 14:55:04 -07003816 struct cfq_queue **async_cfqq = NULL;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003817 struct cfq_queue *cfqq;
Tejun Heo322731e2015-08-18 14:55:03 -07003818 struct cfq_group *cfqg;
3819
3820 rcu_read_lock();
Tejun Heoae118892015-08-18 14:55:20 -07003821 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
Tejun Heo322731e2015-08-18 14:55:03 -07003822 if (!cfqg) {
3823 cfqq = &cfqd->oom_cfqq;
3824 goto out;
3825 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003826
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003827 if (!is_sync) {
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003828 if (!ioprio_valid(cic->ioprio)) {
3829 struct task_struct *tsk = current;
3830 ioprio = task_nice_ioprio(tsk);
3831 ioprio_class = task_nice_ioclass(tsk);
3832 }
Tejun Heo60a83702015-08-18 14:55:05 -07003833 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003834 cfqq = *async_cfqq;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003835 if (cfqq)
3836 goto out;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003837 }
3838
Tejun Heoe00f4f42016-11-21 18:03:32 -05003839 cfqq = kmem_cache_alloc_node(cfq_pool,
3840 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
Tejun Heod4aad7f2015-08-18 14:55:04 -07003841 cfqd->queue->node);
3842 if (!cfqq) {
3843 cfqq = &cfqd->oom_cfqq;
3844 goto out;
3845 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003846
Alexander Potapenko4d608ba2017-01-23 15:06:43 +01003847 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3848 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
Tejun Heod4aad7f2015-08-18 14:55:04 -07003849 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3850 cfq_init_prio_data(cfqq, cic);
3851 cfq_link_cfqq_cfqg(cfqq, cfqg);
3852 cfq_log_cfqq(cfqd, cfqq, "alloced");
3853
3854 if (async_cfqq) {
3855 /* a new async queue is created, pin and remember */
Shaohua Li30d7b942011-01-07 08:46:59 +01003856 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003857 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003858 }
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003859out:
Shaohua Li30d7b942011-01-07 08:46:59 +01003860 cfqq->ref++;
Tejun Heo322731e2015-08-18 14:55:03 -07003861 rcu_read_unlock();
Jens Axboe15c31be2007-07-10 13:43:25 +02003862 return cfqq;
3863}
3864
Jens Axboe22e2c502005-06-27 10:55:12 +02003865static void
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003866__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003867{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003868 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
Shaohua Li383cd722011-07-12 14:24:35 +02003869 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003870
Shaohua Li383cd722011-07-12 14:24:35 +02003871 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003872 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3873 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3874 ttime->ttime_samples);
Shaohua Li383cd722011-07-12 14:24:35 +02003875}
3876
3877static void
3878cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003879 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003880{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003881 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003882 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003883 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3884 cfqd->cfq_slice_idle);
3885 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003886#ifdef CONFIG_CFQ_GROUP_IOSCHED
3887 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3888#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003889}
3890
Jens Axboe206dc692006-03-28 13:03:44 +02003891static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003892cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003893 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003894{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003895 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003896 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003897 if (cfqq->last_request_pos) {
3898 if (cfqq->last_request_pos < blk_rq_pos(rq))
3899 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3900 else
3901 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3902 }
Jens Axboe206dc692006-03-28 13:03:44 +02003903
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003904 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003905 if (blk_queue_nonrot(cfqd->queue))
3906 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3907 else
3908 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003909}
Jens Axboe22e2c502005-06-27 10:55:12 +02003910
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003911static inline bool req_noidle(struct request *req)
3912{
3913 return req_op(req) == REQ_OP_WRITE &&
3914 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3915}
3916
Jens Axboe22e2c502005-06-27 10:55:12 +02003917/*
3918 * Disable idle window if the process thinks too long or seeks so much that
3919 * it doesn't matter
3920 */
3921static void
3922cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003923 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003924{
Jens Axboe7b679132008-05-30 12:23:07 +02003925 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003926
Jens Axboe08717142008-01-28 11:38:15 +01003927 /*
3928 * Don't idle for async or idle io prio class
3929 */
3930 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003931 return;
3932
Jens Axboec265a7f2008-06-26 13:49:33 +02003933 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003934
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003935 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3936 cfq_mark_cfqq_deep(cfqq);
3937
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003938 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003939 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003940 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003941 !cfqd->cfq_slice_idle ||
3942 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003943 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003944 else if (sample_valid(cic->ttime.ttime_samples)) {
3945 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003946 enable_idle = 0;
3947 else
3948 enable_idle = 1;
3949 }
3950
Jens Axboe7b679132008-05-30 12:23:07 +02003951 if (old_idle != enable_idle) {
3952 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3953 if (enable_idle)
3954 cfq_mark_cfqq_idle_window(cfqq);
3955 else
3956 cfq_clear_cfqq_idle_window(cfqq);
3957 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003958}
3959
Jens Axboe22e2c502005-06-27 10:55:12 +02003960/*
3961 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3962 * no or if we aren't sure, a 1 will cause a preempt.
3963 */
Jens Axboea6151c32009-10-07 20:02:57 +02003964static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003965cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003966 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003967{
Jens Axboe6d048f52007-04-25 12:44:27 +02003968 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003969
Jens Axboe6d048f52007-04-25 12:44:27 +02003970 cfqq = cfqd->active_queue;
3971 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003972 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003973
Jens Axboe6d048f52007-04-25 12:44:27 +02003974 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003975 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003976
3977 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003978 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003979
Jens Axboe22e2c502005-06-27 10:55:12 +02003980 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08003981 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
3982 */
3983 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
3984 return false;
3985
3986 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02003987 * if the new request is sync, but the currently running queue is
3988 * not, let the sync request have priority.
3989 */
Glauber Costa3932a862016-09-22 20:59:59 -04003990 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003991 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003992
Jan Kara3984aa52016-01-12 16:24:19 +01003993 /*
3994 * Treat ancestors of current cgroup the same way as current cgroup.
3995 * For anybody else we disallow preemption to guarantee service
3996 * fairness among cgroups.
3997 */
3998 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
Vivek Goyal8682e1f2009-12-03 12:59:50 -05003999 return false;
4000
4001 if (cfq_slice_used(cfqq))
4002 return true;
4003
Jan Kara6c80731c2016-01-12 16:24:16 +01004004 /*
4005 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4006 */
4007 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4008 return true;
4009
4010 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004011 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004012 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004013 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004014 RB_EMPTY_ROOT(&cfqq->sort_list))
4015 return true;
4016
Jens Axboe374f84a2006-07-23 01:42:19 +02004017 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004018 * So both queues are sync. Let the new request get disk time if
4019 * it's a metadata request and the current queue is doing regular IO.
4020 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02004021 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004022 return true;
4023
Shaohua Lid2d59e12010-11-08 15:01:03 +01004024 /* An idle queue should not be idle now for some reason */
4025 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4026 return true;
4027
Jens Axboe1e3335d2007-02-14 19:59:49 +01004028 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004029 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004030
4031 /*
4032 * if this request is as-good as one we would expect from the
4033 * current cfqq, let it preempt
4034 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01004035 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02004036 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004037
Jens Axboea6151c32009-10-07 20:02:57 +02004038 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004039}
4040
4041/*
4042 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4043 * let it have half of its nominal slice.
4044 */
4045static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4046{
Shaohua Lidf0793a2012-01-19 09:20:09 +01004047 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4048
Jens Axboe7b679132008-05-30 12:23:07 +02004049 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01004050 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004051
Jens Axboebf572252006-07-19 20:29:12 +02004052 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004053 * workload type is changed, don't save slice, otherwise preempt
4054 * doesn't happen
4055 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01004056 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004057 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004058
4059 /*
Jens Axboebf572252006-07-19 20:29:12 +02004060 * Put the new queue at the front of the of the current list,
4061 * so we know that it will be selected next.
4062 */
4063 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02004064
4065 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01004066
Justin TerAvest62a37f62011-03-23 08:25:44 +01004067 cfqq->slice_end = 0;
4068 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004069}
4070
4071/*
Jens Axboe5e705372006-07-13 12:39:25 +02004072 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02004073 * something we should do about it
4074 */
4075static void
Jens Axboe5e705372006-07-13 12:39:25 +02004076cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4077 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004078{
Tejun Heoc5869802011-12-14 00:33:41 +01004079 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02004080
Aaron Carroll45333d52008-08-26 15:52:36 +02004081 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02004082 if (rq->cmd_flags & REQ_PRIO)
4083 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02004084
Shaohua Li383cd722011-07-12 14:24:35 +02004085 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004086 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02004087 cfq_update_idle_window(cfqd, cfqq, cic);
4088
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004089 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004090
4091 if (cfqq == cfqd->active_queue) {
4092 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004093 * Remember that we saw a request from this process, but
4094 * don't start queuing just yet. Otherwise we risk seeing lots
4095 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02004096 * and merging. If the request is already larger than a single
4097 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02004098 * merging is already done. Ditto for a busy system that
4099 * has other work pending, don't risk delaying until the
4100 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02004101 */
Jens Axboed6ceb252009-04-14 14:18:16 +02004102 if (cfq_cfqq_wait_request(cfqq)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004103 if (blk_rq_bytes(rq) > PAGE_SIZE ||
Jens Axboe2d870722009-04-15 12:12:46 +02004104 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07004105 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01004106 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004107 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004108 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07004109 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05004110 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004111 }
Jens Axboed6ceb252009-04-14 14:18:16 +02004112 }
Jens Axboe5e705372006-07-13 12:39:25 +02004113 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004114 /*
4115 * not the active queue - expire current slice if it is
4116 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01004117 * has some old slice time left and is of higher priority or
4118 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02004119 */
4120 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004121 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004122 }
4123}
4124
Jens Axboe165125e2007-07-24 09:28:11 +02004125static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004126{
Jens Axboeb4878f22005-10-20 16:42:29 +02004127 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02004128 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004129
Jens Axboe7b679132008-05-30 12:23:07 +02004130 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07004131 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004133 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
Jens Axboe22e2c502005-06-27 10:55:12 +02004134 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01004135 cfq_add_rq_rb(rq);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004136 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
Tejun Heo155fead2012-04-01 14:38:44 -07004137 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02004138 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Aaron Carroll45333d52008-08-26 15:52:36 +02004141/*
4142 * Update hw_tag based on peak queue depth over 50 samples under
4143 * sufficient load.
4144 */
4145static void cfq_update_hw_tag(struct cfq_data *cfqd)
4146{
Shaohua Li1a1238a2009-10-27 08:46:23 +01004147 struct cfq_queue *cfqq = cfqd->active_queue;
4148
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004149 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4150 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004151
4152 if (cfqd->hw_tag == 1)
4153 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02004154
4155 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004156 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004157 return;
4158
Shaohua Li1a1238a2009-10-27 08:46:23 +01004159 /*
4160 * If active queue hasn't enough requests and can idle, cfq might not
4161 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4162 * case
4163 */
4164 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4165 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004166 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01004167 return;
4168
Aaron Carroll45333d52008-08-26 15:52:36 +02004169 if (cfqd->hw_tag_samples++ < 50)
4170 return;
4171
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004172 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004173 cfqd->hw_tag = 1;
4174 else
4175 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02004176}
4177
Vivek Goyal7667aa02009-12-08 17:52:58 -05004178static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4179{
Tejun Heoc5869802011-12-14 00:33:41 +01004180 struct cfq_io_cq *cic = cfqd->active_cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004181 u64 now = ktime_get_ns();
Vivek Goyal7667aa02009-12-08 17:52:58 -05004182
Justin TerAvest02a8f012011-02-09 14:20:03 +01004183 /* If the queue already has requests, don't wait */
4184 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4185 return false;
4186
Vivek Goyal7667aa02009-12-08 17:52:58 -05004187 /* If there are other queues in the group, don't wait */
4188 if (cfqq->cfqg->nr_cfqq > 1)
4189 return false;
4190
Shaohua Li7700fc42011-07-12 14:24:56 +02004191 /* the only queue in the group, but think time is big */
4192 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4193 return false;
4194
Vivek Goyal7667aa02009-12-08 17:52:58 -05004195 if (cfq_slice_used(cfqq))
4196 return true;
4197
4198 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02004199 if (cic && sample_valid(cic->ttime.ttime_samples)
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004200 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004201 return true;
4202
4203 /*
4204 * If think times is less than a jiffy than ttime_mean=0 and above
4205 * will not be true. It might happen that slice has not expired yet
4206 * but will expire soon (4-5 ns) during select_queue(). To cover the
4207 * case where think time is less than a jiffy, mark the queue wait
4208 * busy if only 1 jiffy is left in the slice.
4209 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004210 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004211 return true;
4212
4213 return false;
4214}
4215
Jens Axboe165125e2007-07-24 09:28:11 +02004216static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Jens Axboe5e705372006-07-13 12:39:25 +02004218 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004219 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02004220 const int sync = rq_is_sync(rq);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004221 u64 now = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Christoph Hellwiga2b80962016-11-01 07:40:09 -06004223 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Aaron Carroll45333d52008-08-26 15:52:36 +02004225 cfq_update_hw_tag(cfqd);
4226
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004227 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02004228 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004229 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02004230 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004231 (RQ_CFQG(rq))->dispatched--;
Omar Sandoval522a7772018-05-09 02:08:53 -07004232 cfqg_stats_update_completion(cfqq->cfqg, rq->start_time_ns,
4233 rq->io_start_time_ns, rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004235 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02004236
Vivek Goyal365722b2009-10-03 15:21:27 +02004237 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04004238 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004239
Shaohua Li383cd722011-07-12 14:24:35 +02004240 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004241
4242 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04004243 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004244 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04004245 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4246 cfqq_type(cfqq));
4247
4248 st->ttime.last_end_request = now;
Omar Sandoval522a7772018-05-09 02:08:53 -07004249 if (rq->start_time_ns + cfqd->cfq_fifo_expire[1] <= now)
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004250 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02004251 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004252
Shaohua Li7700fc42011-07-12 14:24:56 +02004253#ifdef CONFIG_CFQ_GROUP_IOSCHED
4254 cfqq->cfqg->ttime.last_end_request = now;
4255#endif
4256
Jens Axboecaaa5f92006-06-16 11:23:00 +02004257 /*
4258 * If this is the active queue, check if it needs to be expired,
4259 * or if we want to idle in case it has no pending requests.
4260 */
4261 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02004262 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4263
Jens Axboe44f7c162007-01-19 11:51:58 +11004264 if (cfq_cfqq_slice_new(cfqq)) {
4265 cfq_set_prio_slice(cfqd, cfqq);
4266 cfq_clear_cfqq_slice_new(cfqq);
4267 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05004268
4269 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05004270 * Should we wait for next request to come in before we expire
4271 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05004272 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05004273 if (cfq_should_wait_busy(cfqd, cfqq)) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004274 u64 extend_sl = cfqd->cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004275 if (!cfqd->cfq_slice_idle)
4276 extend_sl = cfqd->cfq_group_idle;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004277 cfqq->slice_end = now + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05004278 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01004279 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05004280 }
4281
Jens Axboea36e71f2009-04-15 12:15:11 +02004282 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004283 * Idling is not enabled on:
4284 * - expired queues
4285 * - idle-priority queues
4286 * - async queues
4287 * - queues with still some requests queued
4288 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02004289 */
Jens Axboe08717142008-01-28 11:38:15 +01004290 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02004291 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004292 else if (sync && cfqq_empty &&
4293 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004294 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004295 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004296 }
Jens Axboe6d048f52007-04-25 12:44:27 +02004297
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004298 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02004299 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004302static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
Jens Axboeb8269db2016-06-09 15:47:29 -06004303{
4304 /*
4305 * If REQ_PRIO is set, boost class and prio level, if it's below
4306 * BE/NORM. If prio is not set, restore the potentially boosted
4307 * class/prio level.
4308 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004309 if (!(op & REQ_PRIO)) {
Jens Axboeb8269db2016-06-09 15:47:29 -06004310 cfqq->ioprio_class = cfqq->org_ioprio_class;
4311 cfqq->ioprio = cfqq->org_ioprio;
4312 } else {
4313 if (cfq_class_idle(cfqq))
4314 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4315 if (cfqq->ioprio > IOPRIO_NORM)
4316 cfqq->ioprio = IOPRIO_NORM;
4317 }
4318}
4319
Jens Axboe89850f72006-07-22 16:48:31 +02004320static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004321{
Jens Axboe1b379d82009-08-11 08:26:11 +02004322 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02004323 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004324 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02004325 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004326
4327 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02004328}
4329
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004330static int cfq_may_queue(struct request_queue *q, unsigned int op)
Jens Axboe22e2c502005-06-27 10:55:12 +02004331{
4332 struct cfq_data *cfqd = q->elevator->elevator_data;
4333 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01004334 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02004335 struct cfq_queue *cfqq;
4336
4337 /*
4338 * don't force setup of a queue from here, as a call to may_queue
4339 * does not necessarily imply that a request actually will be queued.
4340 * so just lookup a possibly existing queue, or return 'may queue'
4341 * if that fails
4342 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01004343 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004344 if (!cic)
4345 return ELV_MQUEUE_MAY;
4346
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004347 cfqq = cic_to_cfqq(cic, op_is_sync(op));
Jens Axboe22e2c502005-06-27 10:55:12 +02004348 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004349 cfq_init_prio_data(cfqq, cic);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004350 cfqq_boost_on_prio(cfqq, op);
Jens Axboe22e2c502005-06-27 10:55:12 +02004351
Jens Axboe89850f72006-07-22 16:48:31 +02004352 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004353 }
4354
4355 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356}
4357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358/*
4359 * queue lock held here
4360 */
Jens Axboebb37b942006-12-01 10:42:33 +01004361static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Jens Axboe5e705372006-07-13 12:39:25 +02004363 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Jens Axboe5e705372006-07-13 12:39:25 +02004365 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004366 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Jens Axboe22e2c502005-06-27 10:55:12 +02004368 BUG_ON(!cfqq->allocated[rw]);
4369 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004371 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004372 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01004373 rq->elv.priv[0] = NULL;
4374 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 cfq_put_queue(cfqq);
4377 }
4378}
4379
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004380static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004381cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004382 struct cfq_queue *cfqq)
4383{
4384 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4385 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004386 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004387 cfq_put_queue(cfqq);
4388 return cic_to_cfqq(cic, 1);
4389}
4390
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004391/*
4392 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4393 * was the last process referring to said cfqq.
4394 */
4395static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004396split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004397{
4398 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004399 cfqq->pid = current->pid;
4400 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004401 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004402 return cfqq;
4403 }
4404
4405 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004406
4407 cfq_put_cooperator(cfqq);
4408
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004409 cfq_put_queue(cfqq);
4410 return NULL;
4411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004413 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004415static int
Tejun Heo852c7882012-03-05 13:15:27 -08004416cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4417 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418{
4419 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004420 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004422 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004423 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Tejun Heo216284c32011-12-14 00:33:38 +01004425 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004426
Tejun Heo598971b2012-03-19 15:10:58 -07004427 check_ioprio_changed(cic, bio);
Jan Kara142bbdf2017-04-04 14:31:30 +02004428 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004429new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004430 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004431 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heobce61332015-08-18 14:54:59 -07004432 if (cfqq)
4433 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07004434 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004435 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004436 } else {
4437 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004438 * If the queue was seeky for too long, break it apart.
4439 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004440 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004441 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4442 cfqq = split_cfqq(cic, cfqq);
4443 if (!cfqq)
4444 goto new_queue;
4445 }
4446
4447 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004448 * Check to see if this queue is scheduled to merge with
4449 * another, closely cooperating queue. The merging of
4450 * queues happens here as it must be done in process context.
4451 * The reference on new_cfqq was taken in merge_cfqqs.
4452 */
4453 if (cfqq->new_cfqq)
4454 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
4457 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004458
Jens Axboe6fae9c22011-03-01 15:04:39 -05004459 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004460 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004461 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004462 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c32011-12-14 00:33:38 +01004463 spin_unlock_irq(q->queue_lock);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004464
Jens Axboe5e705372006-07-13 12:39:25 +02004465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
David Howells65f27f32006-11-22 14:55:48 +00004468static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004469{
David Howells65f27f32006-11-22 14:55:48 +00004470 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004471 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004472 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004473
Jens Axboe40bb54d2009-04-15 12:11:10 +02004474 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004475 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004476 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004477}
4478
4479/*
4480 * Timer running if the active_queue is currently idling inside its time slice
4481 */
Jan Kara91148322016-06-08 15:11:39 +02004482static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
Jens Axboe22e2c502005-06-27 10:55:12 +02004483{
Jan Kara91148322016-06-08 15:11:39 +02004484 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4485 idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02004486 struct cfq_queue *cfqq;
4487 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004488 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004489
Jens Axboe7b679132008-05-30 12:23:07 +02004490 cfq_log(cfqd, "idle timer fired");
4491
Jens Axboe22e2c502005-06-27 10:55:12 +02004492 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4493
Jens Axboefe094d92008-01-31 13:08:54 +01004494 cfqq = cfqd->active_queue;
4495 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004496 timed_out = 0;
4497
Jens Axboe22e2c502005-06-27 10:55:12 +02004498 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004499 * We saw a request before the queue expired, let it through
4500 */
4501 if (cfq_cfqq_must_dispatch(cfqq))
4502 goto out_kick;
4503
4504 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004505 * expired
4506 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004507 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004508 goto expire;
4509
4510 /*
4511 * only expire and reinvoke request handler, if there are
4512 * other queues with pending requests
4513 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004514 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004515 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004516
4517 /*
4518 * not expired and it has a request pending, let it dispatch
4519 */
Jens Axboe75e50982009-04-07 08:56:14 +02004520 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004521 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004522
4523 /*
4524 * Queue depth flag is reset only when the idle didn't succeed
4525 */
4526 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004527 }
4528expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004529 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004530out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004531 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004532out_cont:
4533 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jan Kara91148322016-06-08 15:11:39 +02004534 return HRTIMER_NORESTART;
Jens Axboe22e2c502005-06-27 10:55:12 +02004535}
4536
Jens Axboe3b181522005-06-27 10:56:24 +02004537static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4538{
Jan Kara91148322016-06-08 15:11:39 +02004539 hrtimer_cancel(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004540 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004541}
Jens Axboe22e2c502005-06-27 10:55:12 +02004542
Jens Axboeb374d182008-10-31 10:05:07 +01004543static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Jens Axboe22e2c502005-06-27 10:55:12 +02004545 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004546 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004547
Jens Axboe3b181522005-06-27 10:56:24 +02004548 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004549
Al Virod9ff4182006-03-18 13:51:22 -05004550 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004551
Al Virod9ff4182006-03-18 13:51:22 -05004552 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004553 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004554
Tejun Heo03aa2642012-03-05 13:15:19 -08004555 spin_unlock_irq(q->queue_lock);
4556
Al Viroa90d7422006-03-18 12:05:37 -05004557 cfq_shutdown_timer_wq(cfqd);
4558
Tejun Heoffea73f2012-06-04 10:02:29 +02004559#ifdef CONFIG_CFQ_GROUP_IOSCHED
4560 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4561#else
Tejun Heof51b8022012-03-05 13:15:05 -08004562 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004563#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004564 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
Jianpeng Mad50235b2013-07-03 13:25:24 +02004567static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
4569 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004570 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004571 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004572 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Jianpeng Mad50235b2013-07-03 13:25:24 +02004574 eq = elevator_alloc(q, e);
4575 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004576 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004577
Joe Perchesc1b511e2013-08-29 15:21:42 -07004578 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004579 if (!cfqd) {
4580 kobject_put(&eq->kobj);
4581 return -ENOMEM;
4582 }
4583 eq->elevator_data = cfqd;
4584
Tejun Heof51b8022012-03-05 13:15:05 -08004585 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004586 spin_lock_irq(q->queue_lock);
4587 q->elevator = eq;
4588 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004589
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004590 /* Init root service tree */
4591 cfqd->grp_service_tree = CFQ_RB_ROOT;
4592
Tejun Heof51b8022012-03-05 13:15:05 -08004593 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004594#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004595 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004596 if (ret)
4597 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004598
Tejun Heoa2b16932012-04-13 13:11:33 -07004599 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004600#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004601 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004602 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4603 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004604 if (!cfqd->root_group)
4605 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004606
Tejun Heoa2b16932012-04-13 13:11:33 -07004607 cfq_init_cfqg_base(cfqd->root_group);
Tejun Heo3ecca622015-08-18 14:55:35 -07004608 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4609 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
Tejun Heo69d7fde2015-08-18 14:55:36 -07004610#endif
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004611
Jens Axboe26a2ac02009-04-23 12:13:27 +02004612 /*
4613 * Not strictly needed (since RB_ROOT just clears the node and we
4614 * zeroed cfqd on alloc), but better be safe in case someone decides
4615 * to add magic to the rb code
4616 */
4617 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4618 cfqd->prio_trees[i] = RB_ROOT;
4619
Jens Axboe6118b702009-06-30 09:34:12 +02004620 /*
Tejun Heod4aad7f2015-08-18 14:55:04 -07004621 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
Jens Axboe6118b702009-06-30 09:34:12 +02004622 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004623 * will not attempt to free it. oom_cfqq is linked to root_group
4624 * but shouldn't hold a reference as it'll never be unlinked. Lose
4625 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004626 */
4627 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004628 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004629
4630 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004631 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004632 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004633 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Jan Kara91148322016-06-08 15:11:39 +02004635 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4636 HRTIMER_MODE_REL);
Jens Axboe22e2c502005-06-27 10:55:12 +02004637 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
Jens Axboe22e2c502005-06-27 10:55:12 +02004638
Jens Axboe23e018a2009-10-05 08:52:35 +02004639 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004642 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4643 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 cfqd->cfq_back_max = cfq_back_max;
4645 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004646 cfqd->cfq_slice[0] = cfq_slice_async;
4647 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004648 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004649 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004650 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004651 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004652 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004653 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004654 /*
4655 * we optimistically start assuming sync ops weren't delayed in last
4656 * second, in order to have larger depth for async operations.
4657 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004658 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004659 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004660
4661out_free:
4662 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004663 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
Jens Axboe0bb97942015-06-10 08:01:20 -06004667static void cfq_registered_queue(struct request_queue *q)
4668{
4669 struct elevator_queue *e = q->elevator;
4670 struct cfq_data *cfqd = e->elevator_data;
4671
4672 /*
4673 * Default to IOPS mode with no idling for SSDs
4674 */
4675 if (blk_queue_nonrot(q))
4676 cfqd->cfq_slice_idle = 0;
Jan Kara142bbdf2017-04-04 14:31:30 +02004677 wbt_disable_default(q);
Jens Axboe0bb97942015-06-10 08:01:20 -06004678}
4679
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680/*
4681 * sysfs parts below -->
4682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683static ssize_t
4684cfq_var_show(unsigned int var, char *page)
4685{
Masanari Iida176167a2014-04-28 12:38:34 +09004686 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
weiping zhang235f8da2017-08-25 01:11:33 +08004689static void
4690cfq_var_store(unsigned int *var, const char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691{
4692 char *p = (char *) page;
4693
4694 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004698static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004700 struct cfq_data *cfqd = e->elevator_data; \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004701 u64 __data = __VAR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004703 __data = div_u64(__data, NSEC_PER_MSEC); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 return cfq_var_show(__data, (page)); \
4705}
4706SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004707SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4708SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004709SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4710SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004711SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004712SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004713SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4714SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4715SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004716SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004717SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718#undef SHOW_FUNCTION
4719
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004720#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4721static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4722{ \
4723 struct cfq_data *cfqd = e->elevator_data; \
4724 u64 __data = __VAR; \
4725 __data = div_u64(__data, NSEC_PER_USEC); \
4726 return cfq_var_show(__data, (page)); \
4727}
4728USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4729USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4730USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4731USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4732USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4733#undef USEC_SHOW_FUNCTION
4734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004736static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004738 struct cfq_data *cfqd = e->elevator_data; \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004739 unsigned int __data, __min = (MIN), __max = (MAX); \
4740 \
weiping zhang235f8da2017-08-25 01:11:33 +08004741 cfq_var_store(&__data, (page)); \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004742 if (__data < __min) \
4743 __data = __min; \
4744 else if (__data > __max) \
4745 __data = __max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004747 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 else \
4749 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08004750 return count; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004753STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4754 UINT_MAX, 1);
4755STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4756 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004757STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004758STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4759 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004760STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004761STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004762STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4763STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004764STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4765 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004766STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004767STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768#undef STORE_FUNCTION
4769
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004770#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4771static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4772{ \
4773 struct cfq_data *cfqd = e->elevator_data; \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004774 unsigned int __data, __min = (MIN), __max = (MAX); \
4775 \
weiping zhang235f8da2017-08-25 01:11:33 +08004776 cfq_var_store(&__data, (page)); \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004777 if (__data < __min) \
4778 __data = __min; \
4779 else if (__data > __max) \
4780 __data = __max; \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004781 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
weiping zhang235f8da2017-08-25 01:11:33 +08004782 return count; \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004783}
4784USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4785USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4786USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4787USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4788USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4789#undef USEC_STORE_FUNCTION
4790
Al Viroe572ec72006-03-18 22:27:18 -05004791#define CFQ_ATTR(name) \
Joe Perches5657a812018-05-24 13:38:59 -06004792 __ATTR(name, 0644, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004793
Al Viroe572ec72006-03-18 22:27:18 -05004794static struct elv_fs_entry cfq_attrs[] = {
4795 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004796 CFQ_ATTR(fifo_expire_sync),
4797 CFQ_ATTR(fifo_expire_async),
4798 CFQ_ATTR(back_seek_max),
4799 CFQ_ATTR(back_seek_penalty),
4800 CFQ_ATTR(slice_sync),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004801 CFQ_ATTR(slice_sync_us),
Al Viroe572ec72006-03-18 22:27:18 -05004802 CFQ_ATTR(slice_async),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004803 CFQ_ATTR(slice_async_us),
Al Viroe572ec72006-03-18 22:27:18 -05004804 CFQ_ATTR(slice_async_rq),
4805 CFQ_ATTR(slice_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004806 CFQ_ATTR(slice_idle_us),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004807 CFQ_ATTR(group_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004808 CFQ_ATTR(group_idle_us),
Jens Axboe963b72f2009-10-03 19:42:18 +02004809 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004810 CFQ_ATTR(target_latency),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004811 CFQ_ATTR(target_latency_us),
Al Viroe572ec72006-03-18 22:27:18 -05004812 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813};
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815static struct elevator_type iosched_cfq = {
Jens Axboec51ca6c2016-12-10 15:13:59 -07004816 .ops.sq = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 .elevator_merge_fn = cfq_merge,
4818 .elevator_merged_fn = cfq_merged_request,
4819 .elevator_merge_req_fn = cfq_merged_requests,
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07004820 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4821 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004822 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004823 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004825 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004828 .elevator_former_req_fn = elv_rb_former_request,
4829 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004830 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004831 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 .elevator_set_req_fn = cfq_set_request,
4833 .elevator_put_req_fn = cfq_put_request,
4834 .elevator_may_queue_fn = cfq_may_queue,
4835 .elevator_init_fn = cfq_init_queue,
4836 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004837 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004839 .icq_size = sizeof(struct cfq_io_cq),
4840 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004841 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004842 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 .elevator_owner = THIS_MODULE,
4844};
4845
Vivek Goyal3e252062009-12-04 10:36:42 -05004846#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004847static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07004848 .dfl_cftypes = cfq_blkcg_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07004849 .legacy_cftypes = cfq_blkcg_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004850
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004851 .cpd_alloc_fn = cfq_cpd_alloc,
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004852 .cpd_init_fn = cfq_cpd_init,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004853 .cpd_free_fn = cfq_cpd_free,
Tejun Heo69d7fde2015-08-18 14:55:36 -07004854 .cpd_bind_fn = cfq_cpd_bind,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004855
Tejun Heo001bea72015-08-18 14:55:11 -07004856 .pd_alloc_fn = cfq_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004857 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004858 .pd_offline_fn = cfq_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07004859 .pd_free_fn = cfq_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004860 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004861};
Vivek Goyal3e252062009-12-04 10:36:42 -05004862#endif
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864static int __init cfq_init(void)
4865{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004866 int ret;
4867
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004868#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004869 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004870 if (ret)
4871 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004872#else
4873 cfq_group_idle = 0;
4874#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004875
Tejun Heofd794952012-06-04 10:01:38 +02004876 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004877 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4878 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004879 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Tejun Heo3d3c2372011-12-14 00:33:42 +01004881 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004882 if (ret)
4883 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004884
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004885 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004886
4887err_free_pool:
4888 kmem_cache_destroy(cfq_pool);
4889err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004890#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004891 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004892#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004893 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
4896static void __exit cfq_exit(void)
4897{
Tejun Heoffea73f2012-06-04 10:02:29 +02004898#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004899 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004902 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
4905module_init(cfq_init);
4906module_exit(cfq_exit);
4907
4908MODULE_AUTHOR("Jens Axboe");
4909MODULE_LICENSE("GPL");
4910MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");