blob: 9ad521195376d123ea26f0de3c1f1f26cf4895e6 [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;
Rick Yiu82d2ef72018-09-26 16:45:50 +0800228 u64 group_idle;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200229};
230
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500231/* This is per cgroup per device grouping structure */
232struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700233 /* must be the first member */
234 struct blkg_policy_data pd;
235
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500236 /* group service_tree member */
237 struct rb_node rb_node;
238
239 /* group service_tree key */
240 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800241
242 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800243 * The number of active cfqgs and sum of their weights under this
244 * cfqg. This covers this cfqg's leaf_weight and all children's
245 * weights, but does not cover weights of further descendants.
246 *
247 * If a cfqg is on the service tree, it's active. An active cfqg
248 * also activates its parent and contributes to the children_weight
249 * of the parent.
250 */
251 int nr_active;
252 unsigned int children_weight;
253
254 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800255 * vfraction is the fraction of vdisktime that the tasks in this
256 * cfqg are entitled to. This is determined by compounding the
257 * ratios walking up from this cfqg to the root.
258 *
259 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
260 * vfractions on a service tree is approximately 1. The sum may
261 * deviate a bit due to rounding errors and fluctuations caused by
262 * cfqgs entering and leaving the service tree.
263 */
264 unsigned int vfraction;
265
266 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800267 * There are two weights - (internal) weight is the weight of this
268 * cfqg against the sibling cfqgs. leaf_weight is the wight of
269 * this cfqg against the child cfqgs. For the root cfqg, both
270 * weights are kept in sync for backward compatibility.
271 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500272 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100273 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700274 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500275
Tejun Heoe71357e2013-01-09 08:05:10 -0800276 unsigned int leaf_weight;
277 unsigned int new_leaf_weight;
278 unsigned int dev_leaf_weight;
279
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500280 /* number of cfqq currently on this group */
281 int nr_cfqq;
282
Jens Axboe22e2c502005-06-27 10:55:12 +0200283 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200284 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200285 * create the array for each prio class but at run time it is used
286 * only for RT and BE class and slot for IDLE class remains unused.
287 * This is primarily done to avoid confusion and a gcc warning.
288 */
289 unsigned int busy_queues_avg[CFQ_PRIO_NR];
290 /*
291 * rr lists of queues with requests. We maintain service trees for
292 * RT and BE classes. These trees are subdivided in subclasses
293 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
294 * class there is no subclassification and all the cfq queues go on
295 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100296 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200297 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100298 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100299 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500300
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600301 u64 saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400302 enum wl_type_t saved_wl_type;
303 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800304
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200305 /* number of requests that are on the dispatch list or inside driver */
306 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200307 struct cfq_ttime ttime;
Tejun Heo0b399202013-01-09 08:05:13 -0800308 struct cfqg_stats stats; /* stats for this cfqg */
Tejun Heo60a83702015-08-18 14:55:05 -0700309
310 /* async queue for each priority case */
311 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
312 struct cfq_queue *async_idle_cfqq;
313
Rick Yiu82d2ef72018-09-26 16:45:50 +0800314 u64 group_idle;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500315};
316
Tejun Heoc5869802011-12-14 00:33:41 +0100317struct cfq_io_cq {
318 struct io_cq icq; /* must be the first member */
319 struct cfq_queue *cfqq[2];
320 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700321 int ioprio; /* the current ioprio */
322#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof4da8072014-09-08 08:15:20 +0900323 uint64_t blkcg_serial_nr; /* the current blkcg serial */
Tejun Heo598971b2012-03-19 15:10:58 -0700324#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100325};
326
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500327/*
328 * Per block device queue structure
329 */
330struct cfq_data {
331 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500332 /* Root service tree for cfq_groups */
333 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800334 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500335
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100336 /*
337 * The priority currently being served
338 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400339 enum wl_class_t serving_wl_class;
340 enum wl_type_t serving_wl_type;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600341 u64 workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500342 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200343
344 /*
345 * Each priority tree is sorted by next_request position. These
346 * trees are used when determining if two or more queues are
347 * interleaving requests (see cfq_close_cooperator).
348 */
349 struct rb_root prio_trees[CFQ_PRIO_LISTS];
350
Jens Axboe22e2c502005-06-27 10:55:12 +0200351 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100352 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200353
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100354 int rq_in_driver;
355 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200356
357 /*
358 * queue-depth detection
359 */
360 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200361 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100362 /*
363 * hw_tag can be
364 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
365 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
366 * 0 => no NCQ
367 */
368 int hw_tag_est_depth;
369 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200370
371 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200372 * idle window management
373 */
Jan Kara91148322016-06-08 15:11:39 +0200374 struct hrtimer idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200375 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200376
377 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100378 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200379
Jens Axboe6d048f52007-04-25 12:44:27 +0200380 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * tunables, see top of file
384 */
385 unsigned int cfq_quantum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned int cfq_back_penalty;
387 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200388 unsigned int cfq_slice_async_rq;
Jens Axboe963b72f2009-10-03 19:42:18 +0200389 unsigned int cfq_latency;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600390 u64 cfq_fifo_expire[2];
391 u64 cfq_slice[2];
392 u64 cfq_slice_idle;
393 u64 cfq_group_idle;
394 u64 cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500395
Jens Axboe6118b702009-06-30 09:34:12 +0200396 /*
397 * Fallback dummy cfqq for extreme OOM conditions
398 */
399 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200400
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600401 u64 last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Vivek Goyal25fb5162009-12-03 12:59:46 -0500404static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
Tejun Heo60a83702015-08-18 14:55:05 -0700405static void cfq_put_queue(struct cfq_queue *cfqq);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500406
Vivek Goyal34b98d02012-10-03 16:56:58 -0400407static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400408 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500409 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100410{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500411 if (!cfqg)
412 return NULL;
413
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400414 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500415 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100416
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400417 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100418}
419
Jens Axboe3b181522005-06-27 10:56:24 +0200420enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100421 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
422 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200423 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100424 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100425 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
426 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
427 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100428 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200429 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400430 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100431 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100432 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500433 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200434};
435
436#define CFQ_CFQQ_FNS(name) \
437static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
438{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100439 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200440} \
441static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
442{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100443 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200444} \
445static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
446{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100447 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200448}
449
450CFQ_CFQQ_FNS(on_rr);
451CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200452CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200453CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200454CFQ_CFQQ_FNS(fifo_expire);
455CFQ_CFQQ_FNS(idle_window);
456CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100457CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200458CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200459CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100460CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100461CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500462CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200463#undef CFQ_CFQQ_FNS
464
Tejun Heo629ed0b2012-04-01 14:38:44 -0700465#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700466
Tejun Heo155fead2012-04-01 14:38:44 -0700467/* cfqg stats flags */
468enum cfqg_stats_flags {
469 CFQG_stats_waiting = 0,
470 CFQG_stats_idling,
471 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700472};
473
Tejun Heo155fead2012-04-01 14:38:44 -0700474#define CFQG_FLAG_FNS(name) \
475static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700476{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700477 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700478} \
Tejun Heo155fead2012-04-01 14:38:44 -0700479static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700480{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700481 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700482} \
Tejun Heo155fead2012-04-01 14:38:44 -0700483static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700484{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700485 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700486} \
487
Tejun Heo155fead2012-04-01 14:38:44 -0700488CFQG_FLAG_FNS(waiting)
489CFQG_FLAG_FNS(idling)
490CFQG_FLAG_FNS(empty)
491#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700492
493/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700494static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700495{
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700496 u64 now;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700497
Tejun Heo155fead2012-04-01 14:38:44 -0700498 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700499 return;
500
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700501 now = ktime_get_ns();
502 if (now > stats->start_group_wait_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700503 blkg_stat_add(&stats->group_wait_time,
504 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700505 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700506}
507
508/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700509static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
510 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700511{
Tejun Heo155fead2012-04-01 14:38:44 -0700512 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700513
Tejun Heo155fead2012-04-01 14:38:44 -0700514 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700515 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700516 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700517 return;
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700518 stats->start_group_wait_time = ktime_get_ns();
Tejun Heo155fead2012-04-01 14:38:44 -0700519 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700520}
521
522/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700523static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700524{
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700525 u64 now;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700526
Tejun Heo155fead2012-04-01 14:38:44 -0700527 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700528 return;
529
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700530 now = ktime_get_ns();
531 if (now > stats->start_empty_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700532 blkg_stat_add(&stats->empty_time,
533 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700534 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700535}
536
Tejun Heo155fead2012-04-01 14:38:44 -0700537static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700538{
Tejun Heo155fead2012-04-01 14:38:44 -0700539 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700540}
541
Tejun Heo155fead2012-04-01 14:38:44 -0700542static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700543{
Tejun Heo155fead2012-04-01 14:38:44 -0700544 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700545
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800546 if (blkg_rwstat_total(&stats->queued))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700547 return;
548
549 /*
550 * group is already marked empty. This can happen if cfqq got new
551 * request in parent group and moved to this group while being added
552 * to service tree. Just ignore the event and move on.
553 */
Tejun Heo155fead2012-04-01 14:38:44 -0700554 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700555 return;
556
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700557 stats->start_empty_time = ktime_get_ns();
Tejun Heo155fead2012-04-01 14:38:44 -0700558 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700559}
560
Tejun Heo155fead2012-04-01 14:38:44 -0700561static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700562{
Tejun Heo155fead2012-04-01 14:38:44 -0700563 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700564
Tejun Heo155fead2012-04-01 14:38:44 -0700565 if (cfqg_stats_idling(stats)) {
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700566 u64 now = ktime_get_ns();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700567
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700568 if (now > stats->start_idle_time)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700569 blkg_stat_add(&stats->idle_time,
570 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700571 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700572 }
573}
574
Tejun Heo155fead2012-04-01 14:38:44 -0700575static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700576{
Tejun Heo155fead2012-04-01 14:38:44 -0700577 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700578
Tejun Heo155fead2012-04-01 14:38:44 -0700579 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700580
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700581 stats->start_idle_time = ktime_get_ns();
Tejun Heo155fead2012-04-01 14:38:44 -0700582 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700583}
584
Tejun Heo155fead2012-04-01 14:38:44 -0700585static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700586{
Tejun Heo155fead2012-04-01 14:38:44 -0700587 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700588
589 blkg_stat_add(&stats->avg_queue_size_sum,
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800590 blkg_rwstat_total(&stats->queued));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700591 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700592 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700593}
594
595#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
596
Tejun Heof48ec1d2012-04-13 13:11:25 -0700597static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
598static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
599static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
600static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
601static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
602static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
603static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700604
605#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
606
607#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700608
Jens Axboe4ceab712015-06-19 10:13:01 -0600609static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
610{
611 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
612}
613
614static struct cfq_group_data
615*cpd_to_cfqgd(struct blkcg_policy_data *cpd)
616{
Tejun Heo81437642015-08-18 14:55:15 -0700617 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
Jens Axboe4ceab712015-06-19 10:13:01 -0600618}
619
620static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
621{
622 return pd_to_blkg(&cfqg->pd);
623}
624
Tejun Heoffea73f2012-06-04 10:02:29 +0200625static struct blkcg_policy blkcg_policy_cfq;
626
627static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
628{
629 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
630}
631
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200632static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
633{
634 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
635}
636
Tejun Heod02f7aa2013-01-09 08:05:11 -0800637static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
Tejun Heo7918ffb2013-01-09 08:05:11 -0800638{
Tejun Heod02f7aa2013-01-09 08:05:11 -0800639 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800640
Tejun Heod02f7aa2013-01-09 08:05:11 -0800641 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800642}
643
Jan Kara3984aa52016-01-12 16:24:19 +0100644static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
645 struct cfq_group *ancestor)
646{
647 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
648 cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
649}
650
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100651static inline void cfqg_get(struct cfq_group *cfqg)
652{
653 return blkg_get(cfqg_to_blkg(cfqg));
654}
655
656static inline void cfqg_put(struct cfq_group *cfqg)
657{
658 return blkg_put(cfqg_to_blkg(cfqg));
659}
660
Tejun Heo54e7ed12012-04-16 13:57:23 -0700661#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
Shaohua Li35fe6d72017-07-12 11:49:56 -0700662 blk_add_cgroup_trace_msg((cfqd)->queue, \
663 cfqg_to_blkg((cfqq)->cfqg)->blkcg, \
664 "cfq%d%c%c " fmt, (cfqq)->pid, \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400665 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
666 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Shaohua Li35fe6d72017-07-12 11:49:56 -0700667 ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700668} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500669
Tejun Heo54e7ed12012-04-16 13:57:23 -0700670#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
Shaohua Li35fe6d72017-07-12 11:49:56 -0700671 blk_add_cgroup_trace_msg((cfqd)->queue, \
672 cfqg_to_blkg(cfqg)->blkcg, fmt, ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700673} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500674
Tejun Heo155fead2012-04-01 14:38:44 -0700675static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600676 struct cfq_group *curr_cfqg,
677 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700678{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600679 blkg_rwstat_add(&cfqg->stats.queued, op, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700680 cfqg_stats_end_empty_time(&cfqg->stats);
681 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700682}
683
Tejun Heo155fead2012-04-01 14:38:44 -0700684static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600685 uint64_t time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700686{
Tejun Heo155fead2012-04-01 14:38:44 -0700687 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700688#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700689 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700690#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700691}
692
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600693static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
694 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700695{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600696 blkg_rwstat_add(&cfqg->stats.queued, op, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700697}
698
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600699static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
700 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700701{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600702 blkg_rwstat_add(&cfqg->stats.merged, op, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700703}
704
Tejun Heo155fead2012-04-01 14:38:44 -0700705static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700706 u64 start_time_ns,
707 u64 io_start_time_ns,
708 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700709{
Tejun Heo155fead2012-04-01 14:38:44 -0700710 struct cfqg_stats *stats = &cfqg->stats;
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700711 u64 now = ktime_get_ns();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700712
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700713 if (now > io_start_time_ns)
714 blkg_rwstat_add(&stats->service_time, op,
715 now - io_start_time_ns);
716 if (io_start_time_ns > start_time_ns)
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600717 blkg_rwstat_add(&stats->wait_time, op,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700718 io_start_time_ns - start_time_ns);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700719}
720
Tejun Heo689665a2013-01-09 08:05:13 -0800721/* @stats = 0 */
722static void cfqg_stats_reset(struct cfqg_stats *stats)
Tejun Heo155fead2012-04-01 14:38:44 -0700723{
Tejun Heo155fead2012-04-01 14:38:44 -0700724 /* queued stats shouldn't be cleared */
Tejun Heo155fead2012-04-01 14:38:44 -0700725 blkg_rwstat_reset(&stats->merged);
726 blkg_rwstat_reset(&stats->service_time);
727 blkg_rwstat_reset(&stats->wait_time);
728 blkg_stat_reset(&stats->time);
729#ifdef CONFIG_DEBUG_BLK_CGROUP
730 blkg_stat_reset(&stats->unaccounted_time);
731 blkg_stat_reset(&stats->avg_queue_size_sum);
732 blkg_stat_reset(&stats->avg_queue_size_samples);
733 blkg_stat_reset(&stats->dequeue);
734 blkg_stat_reset(&stats->group_wait_time);
735 blkg_stat_reset(&stats->idle_time);
736 blkg_stat_reset(&stats->empty_time);
737#endif
738}
739
Tejun Heo0b399202013-01-09 08:05:13 -0800740/* @to += @from */
Tejun Heoe6269c42015-08-18 14:55:21 -0700741static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
Tejun Heo0b399202013-01-09 08:05:13 -0800742{
743 /* queued stats shouldn't be cleared */
Tejun Heoe6269c42015-08-18 14:55:21 -0700744 blkg_rwstat_add_aux(&to->merged, &from->merged);
745 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
746 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
747 blkg_stat_add_aux(&from->time, &from->time);
Tejun Heo0b399202013-01-09 08:05:13 -0800748#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoe6269c42015-08-18 14:55:21 -0700749 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
750 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
751 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
752 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
753 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
754 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
755 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
Tejun Heo0b399202013-01-09 08:05:13 -0800756#endif
757}
758
759/*
Tejun Heoe6269c42015-08-18 14:55:21 -0700760 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
Tejun Heo0b399202013-01-09 08:05:13 -0800761 * recursive stats can still account for the amount used by this cfqg after
762 * it's gone.
763 */
764static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
765{
766 struct cfq_group *parent = cfqg_parent(cfqg);
767
768 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
769
770 if (unlikely(!parent))
771 return;
772
Tejun Heoe6269c42015-08-18 14:55:21 -0700773 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800774 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800775}
776
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100777#else /* CONFIG_CFQ_GROUP_IOSCHED */
778
Tejun Heod02f7aa2013-01-09 08:05:11 -0800779static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Jan Kara3984aa52016-01-12 16:24:19 +0100780static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
781 struct cfq_group *ancestor)
782{
783 return true;
784}
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100785static inline void cfqg_get(struct cfq_group *cfqg) { }
786static inline void cfqg_put(struct cfq_group *cfqg) { }
787
Jens Axboe7b679132008-05-30 12:23:07 +0200788#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400789 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
790 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
791 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
792 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200793#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100794
Tejun Heo155fead2012-04-01 14:38:44 -0700795static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600796 struct cfq_group *curr_cfqg, unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700797static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600798 uint64_t time, unsigned long unaccounted_time) { }
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600799static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
800 unsigned int op) { }
801static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
802 unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700803static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700804 u64 start_time_ns,
805 u64 io_start_time_ns,
806 unsigned int op) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700807
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100808#endif /* CONFIG_CFQ_GROUP_IOSCHED */
809
Rick Yiu82d2ef72018-09-26 16:45:50 +0800810static inline u64 get_group_idle(struct cfq_data *cfqd)
811{
812#ifdef CONFIG_CFQ_GROUP_IOSCHED
813 struct cfq_queue *cfqq = cfqd->active_queue;
814
815 if (cfqq && cfqq->cfqg)
816 return cfqq->cfqg->group_idle;
817#endif
818 return cfqd->cfq_group_idle;
819}
820
Jens Axboe7b679132008-05-30 12:23:07 +0200821#define cfq_log(cfqd, fmt, args...) \
822 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
823
Vivek Goyal615f0252009-12-03 12:59:39 -0500824/* Traverses through cfq group service trees */
825#define for_each_cfqg_st(cfqg, i, j, st) \
826 for (i = 0; i <= IDLE_WORKLOAD; i++) \
827 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
828 : &cfqg->service_tree_idle; \
829 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
830 (i == IDLE_WORKLOAD && j == 0); \
831 j++, st = i < IDLE_WORKLOAD ? \
832 &cfqg->service_trees[i][j]: NULL) \
833
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200834static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
835 struct cfq_ttime *ttime, bool group_idle)
836{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600837 u64 slice;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200838 if (!sample_valid(ttime->ttime_samples))
839 return false;
840 if (group_idle)
Rick Yiu82d2ef72018-09-26 16:45:50 +0800841 slice = get_group_idle(cfqd);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200842 else
843 slice = cfqd->cfq_slice_idle;
844 return ttime->ttime_mean > slice;
845}
Vivek Goyal615f0252009-12-03 12:59:39 -0500846
Vivek Goyal02b35082010-08-23 12:23:53 +0200847static inline bool iops_mode(struct cfq_data *cfqd)
848{
849 /*
850 * If we are not idling on queues and it is a NCQ drive, parallel
851 * execution of requests is on and measuring time is not possible
852 * in most of the cases until and unless we drive shallower queue
853 * depths and that becomes a performance bottleneck. In such cases
854 * switch to start providing fairness in terms of number of IOs.
855 */
856 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
857 return true;
858 else
859 return false;
860}
861
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400862static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100863{
864 if (cfq_class_idle(cfqq))
865 return IDLE_WORKLOAD;
866 if (cfq_class_rt(cfqq))
867 return RT_WORKLOAD;
868 return BE_WORKLOAD;
869}
870
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100871
872static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
873{
874 if (!cfq_cfqq_sync(cfqq))
875 return ASYNC_WORKLOAD;
876 if (!cfq_cfqq_idle_window(cfqq))
877 return SYNC_NOIDLE_WORKLOAD;
878 return SYNC_WORKLOAD;
879}
880
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400881static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500882 struct cfq_data *cfqd,
883 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100884{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400885 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500886 return cfqg->service_tree_idle.count;
887
Vivek Goyal34b98d02012-10-03 16:56:58 -0400888 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
889 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
890 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100891}
892
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500893static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
894 struct cfq_group *cfqg)
895{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400896 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
897 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500898}
899
Jens Axboe165125e2007-07-24 09:28:11 +0200900static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800901static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heo2da8de02015-08-18 14:55:02 -0700902 struct cfq_io_cq *cic, struct bio *bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200903
Tejun Heoc5869802011-12-14 00:33:41 +0100904static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
905{
906 /* cic->icq is the first member, %NULL will convert to %NULL */
907 return container_of(icq, struct cfq_io_cq, icq);
908}
909
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100910static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
911 struct io_context *ioc)
912{
913 if (ioc)
914 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
915 return NULL;
916}
917
Tejun Heoc5869802011-12-14 00:33:41 +0100918static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200919{
Jens Axboea6151c32009-10-07 20:02:57 +0200920 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200921}
922
Tejun Heoc5869802011-12-14 00:33:41 +0100923static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
924 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200925{
Jens Axboea6151c32009-10-07 20:02:57 +0200926 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200927}
928
Tejun Heoc5869802011-12-14 00:33:41 +0100929static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400930{
Tejun Heoc5869802011-12-14 00:33:41 +0100931 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400932}
933
Vasily Tarasov91fac312007-04-25 12:29:51 +0200934/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700935 * scheduler run of queue, if there are requests pending and no one in the
936 * driver that will restart queueing
937 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200938static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700939{
Jens Axboe7b679132008-05-30 12:23:07 +0200940 if (cfqd->busy_queues) {
941 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600942 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200943 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100947 * Scale schedule slice based on io priority. Use the sync time slice only
948 * if a queue is marked sync and has sync io queued. A sync queue with async
949 * io only, should not get full sync slice length.
950 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600951static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200952 unsigned short prio)
953{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600954 u64 base_slice = cfqd->cfq_slice[sync];
955 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
Jens Axboed9e76202007-04-20 14:27:50 +0200956
957 WARN_ON(prio >= IOPRIO_BE_NR);
958
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600959 return base_slice + (slice * (4 - prio));
Jens Axboed9e76202007-04-20 14:27:50 +0200960}
961
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600962static inline u64
Jens Axboe44f7c162007-01-19 11:51:58 +1100963cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
964{
Jens Axboed9e76202007-04-20 14:27:50 +0200965 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100966}
967
Tejun Heo1d3650f2013-01-09 08:05:11 -0800968/**
969 * cfqg_scale_charge - scale disk time charge according to cfqg weight
970 * @charge: disk time being charged
971 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
972 *
973 * Scale @charge according to @vfraction, which is in range (0, 1]. The
974 * scaling is inversely proportional.
975 *
976 * scaled = charge / vfraction
977 *
978 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
979 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600980static inline u64 cfqg_scale_charge(u64 charge,
Tejun Heo1d3650f2013-01-09 08:05:11 -0800981 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500982{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800983 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500984
Tejun Heo1d3650f2013-01-09 08:05:11 -0800985 /* charge / vfraction */
986 c <<= CFQ_SERVICE_SHIFT;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600987 return div_u64(c, vfraction);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500988}
989
990static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
991{
992 s64 delta = (s64)(vdisktime - min_vdisktime);
993 if (delta > 0)
994 min_vdisktime = vdisktime;
995
996 return min_vdisktime;
997}
998
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500999static void update_min_vdisktime(struct cfq_rb_root *st)
1000{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001001 if (!RB_EMPTY_ROOT(&st->rb.rb_root)) {
1002 struct cfq_group *cfqg = rb_entry_cfqg(st->rb.rb_leftmost);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001003
Gui Jianfenga6032712011-03-07 09:28:09 +01001004 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
1005 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001006 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001007}
1008
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001009/*
1010 * get averaged number of queues of RT/BE priority.
1011 * average is updated, with a formula that gives more weight to higher numbers,
1012 * to quickly follows sudden increases and decrease slowly
1013 */
1014
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001015static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1016 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001017{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001018 unsigned min_q, max_q;
1019 unsigned mult = cfq_hist_divisor - 1;
1020 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001021 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001022
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001023 min_q = min(cfqg->busy_queues_avg[rt], busy);
1024 max_q = max(cfqg->busy_queues_avg[rt], busy);
1025 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001026 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001027 return cfqg->busy_queues_avg[rt];
1028}
1029
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001030static inline u64
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001031cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1032{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001033 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001034}
1035
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001036static inline u64
Vivek Goyalba5bd522011-01-19 08:25:02 -07001037cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001038{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001039 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001040 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001041 /*
1042 * interested queues (we consider only the ones with the same
1043 * priority class in the cfq group)
1044 */
1045 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1046 cfq_class_rt(cfqq));
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001047 u64 sync_slice = cfqd->cfq_slice[1];
1048 u64 expect_latency = sync_slice * iq;
1049 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001050
1051 if (expect_latency > group_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001052 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1053 u64 low_slice;
1054
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001055 /* scale low_slice according to IO priority
1056 * and sync vs async */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001057 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1058 low_slice = min(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001059 /* the adapted slice value is scaled to fit all iqs
1060 * into the target latency */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001061 slice = div64_u64(slice*group_slice, expect_latency);
1062 slice = max(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001063 }
1064 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001065 return slice;
1066}
1067
1068static inline void
1069cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1070{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001071 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1072 u64 now = ktime_get_ns();
Shaohua Lic553f8e2011-01-14 08:41:03 +01001073
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001074 cfqq->slice_start = now;
1075 cfqq->slice_end = now + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001076 cfqq->allocated_slice = slice;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001077 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
Jens Axboe44f7c162007-01-19 11:51:58 +11001078}
1079
1080/*
1081 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1082 * isn't valid until the first request from the dispatch is activated
1083 * and the slice time set.
1084 */
Jens Axboea6151c32009-10-07 20:02:57 +02001085static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001086{
1087 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001088 return false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001089 if (ktime_get_ns() < cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01001090 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001091
Shaohua Lic1e44752010-11-08 15:01:02 +01001092 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001093}
1094
1095/*
Jens Axboe5e705372006-07-13 12:39:25 +02001096 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001098 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Jens Axboe5e705372006-07-13 12:39:25 +02001100static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001101cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001103 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001105#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1106#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1107 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Jens Axboe5e705372006-07-13 12:39:25 +02001109 if (rq1 == NULL || rq1 == rq2)
1110 return rq2;
1111 if (rq2 == NULL)
1112 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001113
Namhyung Kim229836b2011-05-24 10:23:21 +02001114 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1115 return rq_is_sync(rq1) ? rq1 : rq2;
1116
Christoph Hellwig65299a32011-08-23 14:50:29 +02001117 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1118 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001119
Tejun Heo83096eb2009-05-07 22:24:39 +09001120 s1 = blk_rq_pos(rq1);
1121 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /*
1124 * by definition, 1KiB is 2 sectors
1125 */
1126 back_max = cfqd->cfq_back_max * 2;
1127
1128 /*
1129 * Strict one way elevator _except_ in the case where we allow
1130 * short backward seeks which are biased as twice the cost of a
1131 * similar forward seek.
1132 */
1133 if (s1 >= last)
1134 d1 = s1 - last;
1135 else if (s1 + back_max >= last)
1136 d1 = (last - s1) * cfqd->cfq_back_penalty;
1137 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001138 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (s2 >= last)
1141 d2 = s2 - last;
1142 else if (s2 + back_max >= last)
1143 d2 = (last - s2) * cfqd->cfq_back_penalty;
1144 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001145 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Andreas Mohre8a99052006-03-28 08:59:49 +02001149 /*
1150 * By doing switch() on the bit mask "wrap" we avoid having to
1151 * check two variables for all permutations: --> faster!
1152 */
1153 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001154 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001155 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001156 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001157 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001158 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001159 else {
1160 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001161 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001162 else
Jens Axboe5e705372006-07-13 12:39:25 +02001163 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001164 }
1165
1166 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001167 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001168 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001169 return rq2;
1170 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001171 default:
1172 /*
1173 * Since both rqs are wrapped,
1174 * start with the one that's further behind head
1175 * (--> only *one* back seek required),
1176 * since back seek takes more time than forward.
1177 */
1178 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001179 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 else
Jens Axboe5e705372006-07-13 12:39:25 +02001181 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183}
1184
Jens Axboe08717142008-01-28 11:38:15 +01001185static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001186{
Vivek Goyal615f0252009-12-03 12:59:39 -05001187 /* Service tree is empty */
1188 if (!root->count)
1189 return NULL;
1190
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001191 return rb_entry(rb_first_cached(&root->rb), struct cfq_queue, rb_node);
Jens Axboecc09e292007-04-26 12:53:50 +02001192}
1193
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001194static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1195{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001196 return rb_entry_cfqg(rb_first_cached(&root->rb));
Jens Axboea36e71f2009-04-15 12:15:11 +02001197}
1198
Jens Axboecc09e292007-04-26 12:53:50 +02001199static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1200{
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001201 if (root->rb_rightmost == n)
1202 root->rb_rightmost = rb_prev(n);
1203
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001204 rb_erase_cached(n, &root->rb);
1205 RB_CLEAR_NODE(n);
1206
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001207 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * would be nice to take fifo expire time into account as well
1212 */
Jens Axboe5e705372006-07-13 12:39:25 +02001213static struct request *
1214cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1215 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Jens Axboe21183b02006-07-13 12:33:14 +02001217 struct rb_node *rbnext = rb_next(&last->rb_node);
1218 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001219 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jens Axboe21183b02006-07-13 12:33:14 +02001221 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001224 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001227 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001228 else {
1229 rbnext = rb_first(&cfqq->sort_list);
1230 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001231 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001234 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001237static u64 cfq_slice_offset(struct cfq_data *cfqd,
1238 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Jens Axboed9e76202007-04-20 14:27:50 +02001240 /*
1241 * just an approximation, should be ok.
1242 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001243 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001244 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001247static inline s64
1248cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1249{
1250 return cfqg->vdisktime - st->min_vdisktime;
1251}
1252
1253static void
1254__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1255{
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001256 struct rb_node **node = &st->rb.rb_root.rb_node;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001257 struct rb_node *parent = NULL;
1258 struct cfq_group *__cfqg;
1259 s64 key = cfqg_key(st, cfqg);
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001260 bool leftmost = true, rightmost = true;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001261
1262 while (*node != NULL) {
1263 parent = *node;
1264 __cfqg = rb_entry_cfqg(parent);
1265
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001266 if (key < cfqg_key(st, __cfqg)) {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001267 node = &parent->rb_left;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001268 rightmost = false;
1269 } else {
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001270 node = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001271 leftmost = false;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001272 }
1273 }
1274
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001275 if (rightmost)
1276 st->rb_rightmost = &cfqg->rb_node;
1277
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001278 rb_link_node(&cfqg->rb_node, parent, node);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07001279 rb_insert_color_cached(&cfqg->rb_node, &st->rb, leftmost);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001280}
1281
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001282/*
1283 * This has to be called only on activation of cfqg
1284 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001285static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001286cfq_update_group_weight(struct cfq_group *cfqg)
1287{
Tejun Heo3381cb82012-04-01 14:38:44 -07001288 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001289 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001290 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001291 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001292}
1293
1294static void
1295cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1296{
1297 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001298
1299 if (cfqg->new_leaf_weight) {
1300 cfqg->leaf_weight = cfqg->new_leaf_weight;
1301 cfqg->new_leaf_weight = 0;
1302 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001303}
1304
1305static void
1306cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1307{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001308 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001309 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001310 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001311 bool propagate;
1312
1313 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001314 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1315
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001316 /*
1317 * Update leaf_weight. We cannot update weight at this point
1318 * because cfqg might already have been activated and is
1319 * contributing its current weight to the parent's child_weight.
1320 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001321 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001322 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001323
1324 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001325 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1326 * entitled to. vfraction is calculated by walking the tree
1327 * towards the root calculating the fraction it has at each level.
1328 * The compounded ratio is how much vfraction @cfqg owns.
1329 *
1330 * Start with the proportion tasks in this cfqg has against active
1331 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001332 */
1333 propagate = !pos->nr_active++;
1334 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001335 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001336
Tejun Heo1d3650f2013-01-09 08:05:11 -08001337 /*
1338 * Compound ->weight walking up the tree. Both activation and
1339 * vfraction calculation are done in the same loop. Propagation
1340 * stops once an already activated node is met. vfraction
1341 * calculation should always continue to the root.
1342 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001343 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001344 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001345 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001346 propagate = !parent->nr_active++;
1347 parent->children_weight += pos->weight;
1348 }
1349 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001350 pos = parent;
1351 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001352
1353 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001354}
1355
Hou Tao5be6b752017-03-01 09:02:33 +08001356static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
1357{
1358 if (!iops_mode(cfqd))
1359 return CFQ_SLICE_MODE_GROUP_DELAY;
1360 else
1361 return CFQ_IOPS_MODE_GROUP_DELAY;
1362}
1363
Justin TerAvest8184f932011-03-17 16:12:36 +01001364static void
1365cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001366{
1367 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1368 struct cfq_group *__cfqg;
1369 struct rb_node *n;
1370
1371 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001372 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001373 return;
1374
1375 /*
1376 * Currently put the group at the end. Later implement something
1377 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001378 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001379 */
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07001380 n = st->rb_rightmost;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001381 if (n) {
1382 __cfqg = rb_entry_cfqg(n);
Hou Tao5be6b752017-03-01 09:02:33 +08001383 cfqg->vdisktime = __cfqg->vdisktime +
1384 cfq_get_cfqg_vdisktime_delay(cfqd);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001385 } else
1386 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001387 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001388}
1389
1390static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001391cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1392{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001393 struct cfq_group *pos = cfqg;
1394 bool propagate;
1395
1396 /*
1397 * Undo activation from cfq_group_service_tree_add(). Deactivate
1398 * @cfqg and propagate deactivation upwards.
1399 */
1400 propagate = !--pos->nr_active;
1401 pos->children_weight -= pos->leaf_weight;
1402
1403 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001404 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001405
1406 /* @pos has 0 nr_active at this point */
1407 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001408 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001409
1410 if (!parent)
1411 break;
1412
1413 propagate = !--parent->nr_active;
1414 parent->children_weight -= pos->weight;
1415 pos = parent;
1416 }
1417
1418 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001419 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1420 cfq_rb_erase(&cfqg->rb_node, st);
1421}
1422
1423static void
1424cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001425{
1426 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1427
1428 BUG_ON(cfqg->nr_cfqq < 1);
1429 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001430
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001431 /* If there are other cfq queues under this group, don't delete it */
1432 if (cfqg->nr_cfqq)
1433 return;
1434
Vivek Goyal2868ef72009-12-03 12:59:48 -05001435 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001436 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001437 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001438 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001439}
1440
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001441static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1442 u64 *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001443{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001444 u64 slice_used;
1445 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001446
1447 /*
1448 * Queue got expired before even a single request completed or
1449 * got expired immediately after first request completion.
1450 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001451 if (!cfqq->slice_start || cfqq->slice_start == now) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001452 /*
1453 * Also charge the seek time incurred to the group, otherwise
1454 * if there are mutiple queues in the group, each can dispatch
1455 * a single request on seeky media and cause lots of seek time
1456 * and group will never know it.
1457 */
Jan Kara0b31c102016-06-28 09:04:02 +02001458 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1459 jiffies_to_nsecs(1));
Vivek Goyaldae739e2009-12-03 12:59:45 -05001460 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001461 slice_used = now - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001462 if (slice_used > cfqq->allocated_slice) {
1463 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001464 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001465 }
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001466 if (cfqq->slice_start > cfqq->dispatch_start)
Justin TerAvest167400d2011-03-12 16:54:00 +01001467 *unaccounted_time += cfqq->slice_start -
1468 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001469 }
1470
Vivek Goyaldae739e2009-12-03 12:59:45 -05001471 return slice_used;
1472}
1473
1474static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001475 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001476{
1477 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001478 u64 used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001479 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1480 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001481 unsigned int vfr;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001482 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001483
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001484 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001485 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001486
Vivek Goyal02b35082010-08-23 12:23:53 +02001487 if (iops_mode(cfqd))
1488 charge = cfqq->slice_dispatch;
1489 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1490 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001491
Tejun Heo1d3650f2013-01-09 08:05:11 -08001492 /*
1493 * Can't update vdisktime while on service tree and cfqg->vfraction
1494 * is valid only while on it. Cache vfr, leave the service tree,
1495 * update vdisktime and go back on. The re-addition to the tree
1496 * will also update the weights as necessary.
1497 */
1498 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001499 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001500 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001501 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001502
1503 /* This group is being expired. Save the context */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001504 if (cfqd->workload_expires > now) {
1505 cfqg->saved_wl_slice = cfqd->workload_expires - now;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001506 cfqg->saved_wl_type = cfqd->serving_wl_type;
1507 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001508 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001509 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001510
1511 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1512 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001513 cfq_log_cfqq(cfqq->cfqd, cfqq,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001514 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
Joe Perchesfd16d262011-06-13 10:42:49 +02001515 used_sl, cfqq->slice_dispatch, charge,
1516 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001517 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1518 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001519}
1520
Tejun Heof51b8022012-03-05 13:15:05 -08001521/**
1522 * cfq_init_cfqg_base - initialize base part of a cfq_group
1523 * @cfqg: cfq_group to initialize
1524 *
1525 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1526 * is enabled or not.
1527 */
1528static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1529{
1530 struct cfq_rb_root *st;
1531 int i, j;
1532
1533 for_each_cfqg_st(cfqg, i, j, st)
1534 *st = CFQ_RB_ROOT;
1535 RB_CLEAR_NODE(&cfqg->rb_node);
1536
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001537 cfqg->ttime.last_end_request = ktime_get_ns();
Tejun Heof51b8022012-03-05 13:15:05 -08001538}
1539
Vivek Goyal25fb5162009-12-03 12:59:46 -05001540#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo69d7fde2015-08-18 14:55:36 -07001541static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1542 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1543
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001544static void cfqg_stats_exit(struct cfqg_stats *stats)
Peter Zijlstra90d38392013-11-12 19:42:14 -08001545{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001546 blkg_rwstat_exit(&stats->merged);
1547 blkg_rwstat_exit(&stats->service_time);
1548 blkg_rwstat_exit(&stats->wait_time);
1549 blkg_rwstat_exit(&stats->queued);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001550 blkg_stat_exit(&stats->time);
1551#ifdef CONFIG_DEBUG_BLK_CGROUP
1552 blkg_stat_exit(&stats->unaccounted_time);
1553 blkg_stat_exit(&stats->avg_queue_size_sum);
1554 blkg_stat_exit(&stats->avg_queue_size_samples);
1555 blkg_stat_exit(&stats->dequeue);
1556 blkg_stat_exit(&stats->group_wait_time);
1557 blkg_stat_exit(&stats->idle_time);
1558 blkg_stat_exit(&stats->empty_time);
1559#endif
1560}
1561
1562static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1563{
Tejun Heo77ea7332015-08-18 14:55:24 -07001564 if (blkg_rwstat_init(&stats->merged, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001565 blkg_rwstat_init(&stats->service_time, gfp) ||
1566 blkg_rwstat_init(&stats->wait_time, gfp) ||
1567 blkg_rwstat_init(&stats->queued, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001568 blkg_stat_init(&stats->time, gfp))
1569 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001570
1571#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001572 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1573 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1574 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1575 blkg_stat_init(&stats->dequeue, gfp) ||
1576 blkg_stat_init(&stats->group_wait_time, gfp) ||
1577 blkg_stat_init(&stats->idle_time, gfp) ||
1578 blkg_stat_init(&stats->empty_time, gfp))
1579 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001580#endif
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001581 return 0;
1582err:
1583 cfqg_stats_exit(stats);
1584 return -ENOMEM;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001585}
1586
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001587static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1588{
1589 struct cfq_group_data *cgd;
1590
Tejun Heoebc4ff62016-11-10 11:16:37 -05001591 cgd = kzalloc(sizeof(*cgd), gfp);
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001592 if (!cgd)
1593 return NULL;
1594 return &cgd->cpd;
1595}
1596
Tejun Heo81437642015-08-18 14:55:15 -07001597static void cfq_cpd_init(struct blkcg_policy_data *cpd)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001598{
Tejun Heo81437642015-08-18 14:55:15 -07001599 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001600 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
Tejun Heo69d7fde2015-08-18 14:55:36 -07001601 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001602
Tejun Heo69d7fde2015-08-18 14:55:36 -07001603 if (cpd_to_blkcg(cpd) == &blkcg_root)
1604 weight *= 2;
1605
1606 cgd->weight = weight;
1607 cgd->leaf_weight = weight;
Rick Yiu82d2ef72018-09-26 16:45:50 +08001608 cgd->group_idle = cfq_group_idle;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001609}
1610
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001611static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1612{
1613 kfree(cpd_to_cfqgd(cpd));
1614}
1615
Tejun Heo69d7fde2015-08-18 14:55:36 -07001616static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1617{
1618 struct blkcg *blkcg = cpd_to_blkcg(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001619 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
Tejun Heo69d7fde2015-08-18 14:55:36 -07001620 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1621
1622 if (blkcg == &blkcg_root)
1623 weight *= 2;
1624
1625 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1626 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1627}
1628
Tejun Heo001bea72015-08-18 14:55:11 -07001629static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1630{
Tejun Heob2ce2642015-08-18 14:55:13 -07001631 struct cfq_group *cfqg;
1632
1633 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1634 if (!cfqg)
1635 return NULL;
1636
1637 cfq_init_cfqg_base(cfqg);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001638 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1639 kfree(cfqg);
1640 return NULL;
1641 }
Tejun Heob2ce2642015-08-18 14:55:13 -07001642
1643 return &cfqg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -07001644}
1645
Tejun Heoa9520cd2015-08-18 14:55:14 -07001646static void cfq_pd_init(struct blkg_policy_data *pd)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001647{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001648 struct cfq_group *cfqg = pd_to_cfqg(pd);
1649 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001650
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001651 cfqg->weight = cgd->weight;
1652 cfqg->leaf_weight = cgd->leaf_weight;
Rick Yiu82d2ef72018-09-26 16:45:50 +08001653 cfqg->group_idle = cgd->group_idle;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001654}
1655
Tejun Heoa9520cd2015-08-18 14:55:14 -07001656static void cfq_pd_offline(struct blkg_policy_data *pd)
Tejun Heo0b399202013-01-09 08:05:13 -08001657{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001658 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo60a83702015-08-18 14:55:05 -07001659 int i;
1660
1661 for (i = 0; i < IOPRIO_BE_NR; i++) {
1662 if (cfqg->async_cfqq[0][i])
1663 cfq_put_queue(cfqg->async_cfqq[0][i]);
1664 if (cfqg->async_cfqq[1][i])
1665 cfq_put_queue(cfqg->async_cfqq[1][i]);
1666 }
1667
1668 if (cfqg->async_idle_cfqq)
1669 cfq_put_queue(cfqg->async_idle_cfqq);
1670
Tejun Heo0b399202013-01-09 08:05:13 -08001671 /*
1672 * @blkg is going offline and will be ignored by
1673 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1674 * that they don't get lost. If IOs complete after this point, the
1675 * stats for them will be lost. Oh well...
1676 */
Tejun Heo60a83702015-08-18 14:55:05 -07001677 cfqg_stats_xfer_dead(cfqg);
Tejun Heo0b399202013-01-09 08:05:13 -08001678}
1679
Tejun Heo001bea72015-08-18 14:55:11 -07001680static void cfq_pd_free(struct blkg_policy_data *pd)
1681{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001682 struct cfq_group *cfqg = pd_to_cfqg(pd);
1683
1684 cfqg_stats_exit(&cfqg->stats);
1685 return kfree(cfqg);
Tejun Heo001bea72015-08-18 14:55:11 -07001686}
1687
Tejun Heoa9520cd2015-08-18 14:55:14 -07001688static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo689665a2013-01-09 08:05:13 -08001689{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001690 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo689665a2013-01-09 08:05:13 -08001691
1692 cfqg_stats_reset(&cfqg->stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001693}
1694
Tejun Heoae118892015-08-18 14:55:20 -07001695static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1696 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001697{
Tejun Heoae118892015-08-18 14:55:20 -07001698 struct blkcg_gq *blkg;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001699
Tejun Heoae118892015-08-18 14:55:20 -07001700 blkg = blkg_lookup(blkcg, cfqd->queue);
1701 if (likely(blkg))
1702 return blkg_to_cfqg(blkg);
1703 return NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001704}
1705
1706static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1707{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001708 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001709 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001710 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001711}
1712
Tejun Heof95a04a2012-04-16 13:57:26 -07001713static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1714 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001715{
Tejun Heof95a04a2012-04-16 13:57:26 -07001716 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001717
1718 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001719 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001720 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001721}
1722
Tejun Heo2da8ca82013-12-05 12:28:04 -05001723static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001724{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001725 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1726 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1727 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001728 return 0;
1729}
1730
Tejun Heoe71357e2013-01-09 08:05:10 -08001731static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1732 struct blkg_policy_data *pd, int off)
1733{
1734 struct cfq_group *cfqg = pd_to_cfqg(pd);
1735
1736 if (!cfqg->dev_leaf_weight)
1737 return 0;
1738 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1739}
1740
Tejun Heo2da8ca82013-12-05 12:28:04 -05001741static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001742{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001743 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1744 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1745 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001746 return 0;
1747}
1748
Tejun Heo2da8ca82013-12-05 12:28:04 -05001749static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001750{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001751 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001752 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1753 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001754
Jens Axboe9470e4a2015-06-19 10:19:36 -06001755 if (cgd)
1756 val = cgd->weight;
1757
1758 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001759 return 0;
1760}
1761
Tejun Heo2da8ca82013-12-05 12:28:04 -05001762static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001763{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001764 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001765 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1766 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001767
Jens Axboe9470e4a2015-06-19 10:19:36 -06001768 if (cgd)
1769 val = cgd->leaf_weight;
1770
1771 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001772 return 0;
1773}
1774
Rick Yiu82d2ef72018-09-26 16:45:50 +08001775static int cfq_print_group_idle(struct seq_file *sf, void *v)
1776{
1777 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
1778 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1779 u64 val = 0;
1780
1781 if (cgd)
1782 val = cgd->group_idle;
1783
1784 seq_printf(sf, "%llu\n", div_u64(val, NSEC_PER_USEC));
1785 return 0;
1786}
1787
Tejun Heo451af502014-05-13 12:16:21 -04001788static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1789 char *buf, size_t nbytes, loff_t off,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001790 bool on_dfl, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001791{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001792 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1793 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo451af502014-05-13 12:16:21 -04001794 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001795 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001796 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001797 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001798 int ret;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001799 u64 v;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001800
Tejun Heo3c798392012-04-16 13:57:25 -07001801 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001802 if (ret)
1803 return ret;
1804
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001805 if (sscanf(ctx.body, "%llu", &v) == 1) {
1806 /* require "default" on dfl */
1807 ret = -ERANGE;
1808 if (!v && on_dfl)
1809 goto out_finish;
1810 } else if (!strcmp(strim(ctx.body), "default")) {
1811 v = 0;
1812 } else {
1813 ret = -EINVAL;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001814 goto out_finish;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001815 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001816
Tejun Heo3381cb82012-04-01 14:38:44 -07001817 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001818 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001819
Tejun Heo20386ce2015-08-18 14:55:28 -07001820 ret = -ERANGE;
Tejun Heo69d7fde2015-08-18 14:55:36 -07001821 if (!v || (v >= min && v <= max)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001822 if (!is_leaf_weight) {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001823 cfqg->dev_weight = v;
1824 cfqg->new_weight = v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001825 } else {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001826 cfqg->dev_leaf_weight = v;
1827 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001828 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001829 ret = 0;
1830 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001831out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001832 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001833 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001834}
1835
Tejun Heo451af502014-05-13 12:16:21 -04001836static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1837 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001838{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001839 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001840}
1841
Tejun Heo451af502014-05-13 12:16:21 -04001842static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1843 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001844{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001845 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001846}
1847
Tejun Heodd165eb2015-08-18 14:55:33 -07001848static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
Tejun Heo69d7fde2015-08-18 14:55:36 -07001849 bool on_dfl, bool reset_dev, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001850{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001851 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1852 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo182446d2013-08-08 20:11:24 -04001853 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001854 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001855 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001856 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001857
Tejun Heo69d7fde2015-08-18 14:55:36 -07001858 if (val < min || val > max)
1859 return -ERANGE;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001860
1861 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001862 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001863 if (!cfqgd) {
1864 ret = -EINVAL;
1865 goto out;
1866 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001867
1868 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001869 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001870 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001871 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001872
Sasha Levinb67bfe02013-02-27 17:06:00 -08001873 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001874 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001875
Tejun Heoe71357e2013-01-09 08:05:10 -08001876 if (!cfqg)
1877 continue;
1878
1879 if (!is_leaf_weight) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001880 if (reset_dev)
1881 cfqg->dev_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001882 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001883 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001884 } else {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001885 if (reset_dev)
1886 cfqg->dev_leaf_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001887 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001888 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001889 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001890 }
1891
Jens Axboeae994ea2015-06-20 10:26:50 -06001892out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001893 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001894 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001895}
1896
Tejun Heo182446d2013-08-08 20:11:24 -04001897static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1898 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001899{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001900 return __cfq_set_weight(css, val, false, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001901}
1902
Tejun Heo182446d2013-08-08 20:11:24 -04001903static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1904 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001905{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001906 return __cfq_set_weight(css, val, false, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001907}
1908
Rick Yiu82d2ef72018-09-26 16:45:50 +08001909static int cfq_set_group_idle(struct cgroup_subsys_state *css,
1910 struct cftype *cft, u64 val)
1911{
1912 struct blkcg *blkcg = css_to_blkcg(css);
1913 struct cfq_group_data *cfqgd;
1914 struct blkcg_gq *blkg;
1915 int ret = 0;
1916
1917 spin_lock_irq(&blkcg->lock);
1918 cfqgd = blkcg_to_cfqgd(blkcg);
1919 if (!cfqgd) {
1920 ret = -EINVAL;
1921 goto out;
1922 }
1923
1924 cfqgd->group_idle = val * NSEC_PER_USEC;
1925
1926 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
1927 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
1928
1929 if (!cfqg)
1930 continue;
1931
1932 cfqg->group_idle = cfqgd->group_idle;
1933 }
1934
1935out:
1936 spin_unlock_irq(&blkcg->lock);
1937 return ret;
1938}
1939
Tejun Heo2da8ca82013-12-05 12:28:04 -05001940static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001941{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001942 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1943 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001944 return 0;
1945}
1946
Tejun Heo2da8ca82013-12-05 12:28:04 -05001947static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001948{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001949 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1950 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001951 return 0;
1952}
1953
Tejun Heo43114012013-01-09 08:05:13 -08001954static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1955 struct blkg_policy_data *pd, int off)
1956{
Tejun Heof12c74c2015-08-18 14:55:23 -07001957 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1958 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001959 return __blkg_prfill_u64(sf, pd, sum);
1960}
1961
1962static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1963 struct blkg_policy_data *pd, int off)
1964{
Tejun Heof12c74c2015-08-18 14:55:23 -07001965 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1966 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001967 return __blkg_prfill_rwstat(sf, pd, &sum);
1968}
1969
Tejun Heo2da8ca82013-12-05 12:28:04 -05001970static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001971{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001972 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1973 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1974 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001975 return 0;
1976}
1977
Tejun Heo2da8ca82013-12-05 12:28:04 -05001978static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001979{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001980 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1981 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1982 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001983 return 0;
1984}
1985
Tejun Heo702747c2015-08-18 14:55:25 -07001986static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1987 int off)
1988{
1989 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1990
1991 return __blkg_prfill_u64(sf, pd, sum >> 9);
1992}
1993
1994static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1995{
1996 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1997 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1998 return 0;
1999}
2000
2001static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
2002 struct blkg_policy_data *pd, int off)
2003{
2004 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
2005 offsetof(struct blkcg_gq, stat_bytes));
2006 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
2007 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
2008
2009 return __blkg_prfill_u64(sf, pd, sum >> 9);
2010}
2011
2012static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
2013{
2014 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
2015 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
2016 false);
2017 return 0;
2018}
2019
Tejun Heo60c2bc22012-04-01 14:38:43 -07002020#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07002021static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
2022 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07002023{
Tejun Heof95a04a2012-04-16 13:57:26 -07002024 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07002025 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07002026 u64 v = 0;
2027
2028 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07002029 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06002030 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07002031 }
Tejun Heof95a04a2012-04-16 13:57:26 -07002032 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07002033 return 0;
2034}
2035
2036/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05002037static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07002038{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002039 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
2040 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
2041 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07002042 return 0;
2043}
2044#endif /* CONFIG_DEBUG_BLK_CGROUP */
2045
Tejun Heo880f50e2015-08-18 14:55:30 -07002046static struct cftype cfq_blkcg_legacy_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08002047 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002048 {
2049 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08002050 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002051 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002052 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002053 },
2054 {
2055 .name = "weight",
2056 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002057 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002058 .write_u64 = cfq_set_leaf_weight,
2059 },
2060
2061 /* no such mapping necessary for !roots */
2062 {
2063 .name = "weight_device",
2064 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002065 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002066 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002067 },
2068 {
2069 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08002070 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002071 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07002072 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002073 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08002074
2075 {
2076 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002077 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002078 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08002079 },
2080 {
2081 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002082 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08002083 .write_u64 = cfq_set_leaf_weight,
2084 },
Rick Yiu82d2ef72018-09-26 16:45:50 +08002085 {
2086 .name = "group_idle",
2087 .seq_show = cfq_print_group_idle,
2088 .write_u64 = cfq_set_group_idle,
2089 },
Tejun Heoe71357e2013-01-09 08:05:10 -08002090
Tejun Heo43114012013-01-09 08:05:13 -08002091 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002092 {
2093 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002094 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002095 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002096 },
2097 {
2098 .name = "sectors",
Tejun Heo702747c2015-08-18 14:55:25 -07002099 .seq_show = cfqg_print_stat_sectors,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002100 },
2101 {
2102 .name = "io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07002103 .private = (unsigned long)&blkcg_policy_cfq,
2104 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002105 },
2106 {
2107 .name = "io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07002108 .private = (unsigned long)&blkcg_policy_cfq,
2109 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002110 },
2111 {
2112 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002113 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002114 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002115 },
2116 {
2117 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002118 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002119 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002120 },
2121 {
2122 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002123 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002124 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002125 },
2126 {
2127 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002128 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002129 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002130 },
Tejun Heo43114012013-01-09 08:05:13 -08002131
2132 /* the same statictics which cover the cfqg and its descendants */
2133 {
2134 .name = "time_recursive",
2135 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002136 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002137 },
2138 {
2139 .name = "sectors_recursive",
Tejun Heo702747c2015-08-18 14:55:25 -07002140 .seq_show = cfqg_print_stat_sectors_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002141 },
2142 {
2143 .name = "io_service_bytes_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002144 .private = (unsigned long)&blkcg_policy_cfq,
2145 .seq_show = blkg_print_stat_bytes_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002146 },
2147 {
2148 .name = "io_serviced_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002149 .private = (unsigned long)&blkcg_policy_cfq,
2150 .seq_show = blkg_print_stat_ios_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002151 },
2152 {
2153 .name = "io_service_time_recursive",
2154 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002155 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002156 },
2157 {
2158 .name = "io_wait_time_recursive",
2159 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002160 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002161 },
2162 {
2163 .name = "io_merged_recursive",
2164 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002165 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002166 },
2167 {
2168 .name = "io_queued_recursive",
2169 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002170 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002171 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002172#ifdef CONFIG_DEBUG_BLK_CGROUP
2173 {
2174 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002175 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002176 },
2177 {
2178 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002179 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002180 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002181 },
2182 {
2183 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002184 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002185 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002186 },
2187 {
2188 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002189 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002190 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002191 },
2192 {
2193 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002194 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002195 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002196 },
2197 {
2198 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002199 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002200 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002201 },
2202#endif /* CONFIG_DEBUG_BLK_CGROUP */
2203 { } /* terminate */
2204};
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002205
2206static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2207{
2208 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2209 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2210
2211 seq_printf(sf, "default %u\n", cgd->weight);
2212 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2213 &blkcg_policy_cfq, 0, false);
2214 return 0;
2215}
2216
2217static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2218 char *buf, size_t nbytes, loff_t off)
2219{
2220 char *endp;
2221 int ret;
2222 u64 v;
2223
2224 buf = strim(buf);
2225
2226 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2227 v = simple_strtoull(buf, &endp, 0);
2228 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07002229 ret = __cfq_set_weight(of_css(of), v, true, false, false);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002230 return ret ?: nbytes;
2231 }
2232
2233 /* "MAJ:MIN WEIGHT" */
2234 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2235}
2236
2237static struct cftype cfq_blkcg_files[] = {
2238 {
2239 .name = "weight",
2240 .flags = CFTYPE_NOT_ON_ROOT,
2241 .seq_show = cfq_print_weight_on_dfl,
2242 .write = cfq_set_weight_on_dfl,
2243 },
2244 { } /* terminate */
2245};
2246
Vivek Goyal25fb5162009-12-03 12:59:46 -05002247#else /* GROUP_IOSCHED */
Tejun Heoae118892015-08-18 14:55:20 -07002248static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2249 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002250{
Tejun Heof51b8022012-03-05 13:15:05 -08002251 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002252}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002253
Vivek Goyal25fb5162009-12-03 12:59:46 -05002254static inline void
2255cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2256 cfqq->cfqg = cfqg;
2257}
2258
2259#endif /* GROUP_IOSCHED */
2260
Jens Axboe498d3aa22007-04-26 12:54:48 +02002261/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002262 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002263 * requests waiting to be processed. It is sorted in the order that
2264 * we will service the queues.
2265 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002266static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002267 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002268{
Jens Axboe08717142008-01-28 11:38:15 +01002269 struct rb_node **p, *parent;
2270 struct cfq_queue *__cfqq;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002271 u64 rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002272 struct cfq_rb_root *st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002273 bool leftmost = true;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002274 int new_cfqq = 1;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002275 u64 now = ktime_get_ns();
Vivek Goyalae30c282009-12-03 12:59:55 -05002276
Vivek Goyal34b98d02012-10-03 16:56:58 -04002277 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002278 if (cfq_class_idle(cfqq)) {
2279 rb_key = CFQ_IDLE_DELAY;
Davidlohr Buesof0f1a452017-09-08 16:15:25 -07002280 parent = st->rb_rightmost;
Jens Axboe08717142008-01-28 11:38:15 +01002281 if (parent && parent != &cfqq->rb_node) {
2282 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2283 rb_key += __cfqq->rb_key;
2284 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002285 rb_key += now;
Jens Axboe08717142008-01-28 11:38:15 +01002286 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002287 /*
2288 * Get our rb key offset. Subtract any residual slice
2289 * value carried from last service. A negative resid
2290 * count indicates slice overrun, and this should position
2291 * the next service time further away in the tree.
2292 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002293 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
Jens Axboeb9c89462009-10-06 20:53:44 +02002294 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002295 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002296 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002297 rb_key = -NSEC_PER_SEC;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002298 __cfqq = cfq_rb_first(st);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002299 rb_key += __cfqq ? __cfqq->rb_key : now;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002300 }
Jens Axboed9e76202007-04-20 14:27:50 +02002301
2302 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002303 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002304 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002305 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002306 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002307 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002308 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002309
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002310 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2311 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002312 }
Jens Axboed9e76202007-04-20 14:27:50 +02002313
Jens Axboe08717142008-01-28 11:38:15 +01002314 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002315 cfqq->service_tree = st;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002316 p = &st->rb.rb_root.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002317 while (*p) {
2318 parent = *p;
2319 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2320
Jens Axboe0c534e02007-04-18 20:01:57 +02002321 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002322 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002323 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002324 if (rb_key < __cfqq->rb_key)
Vivek Goyal1f23f122012-10-03 16:57:00 -04002325 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002326 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002327 p = &parent->rb_right;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002328 leftmost = false;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002329 }
Jens Axboed9e76202007-04-20 14:27:50 +02002330 }
2331
2332 cfqq->rb_key = rb_key;
2333 rb_link_node(&cfqq->rb_node, parent, p);
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002334 rb_insert_color_cached(&cfqq->rb_node, &st->rb, leftmost);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002335 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002336 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002337 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002338 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Jens Axboea36e71f2009-04-15 12:15:11 +02002341static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002342cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2343 sector_t sector, struct rb_node **ret_parent,
2344 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002345{
Jens Axboea36e71f2009-04-15 12:15:11 +02002346 struct rb_node **p, *parent;
2347 struct cfq_queue *cfqq = NULL;
2348
2349 parent = NULL;
2350 p = &root->rb_node;
2351 while (*p) {
2352 struct rb_node **n;
2353
2354 parent = *p;
2355 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2356
2357 /*
2358 * Sort strictly based on sector. Smallest to the left,
2359 * largest to the right.
2360 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002361 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002362 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002363 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002364 n = &(*p)->rb_left;
2365 else
2366 break;
2367 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002368 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002369 }
2370
2371 *ret_parent = parent;
2372 if (rb_link)
2373 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002374 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002375}
2376
2377static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2378{
Jens Axboea36e71f2009-04-15 12:15:11 +02002379 struct rb_node **p, *parent;
2380 struct cfq_queue *__cfqq;
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 Axboea36e71f2009-04-15 12:15:11 +02002386
2387 if (cfq_class_idle(cfqq))
2388 return;
2389 if (!cfqq->next_rq)
2390 return;
2391
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002392 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002393 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2394 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002395 if (!__cfqq) {
2396 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002397 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2398 } else
2399 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002400}
2401
Jens Axboe498d3aa22007-04-26 12:54:48 +02002402/*
2403 * Update cfqq's position in the service tree.
2404 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002405static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002406{
Jens Axboe6d048f52007-04-25 12:44:27 +02002407 /*
2408 * Resorting requires the cfqq to be on the RR list already.
2409 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002410 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002411 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002412 cfq_prio_tree_add(cfqd, cfqq);
2413 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/*
2417 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002418 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 */
Jens Axboefebffd62008-01-28 13:19:43 +01002420static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Jens Axboe7b679132008-05-30 12:23:07 +02002422 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002423 BUG_ON(cfq_cfqq_on_rr(cfqq));
2424 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002426 if (cfq_cfqq_sync(cfqq))
2427 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Jens Axboeedd75ff2007-04-19 12:03:34 +02002429 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
Jens Axboe498d3aa22007-04-26 12:54:48 +02002432/*
2433 * Called when the cfqq no longer has requests pending, remove it from
2434 * the service tree.
2435 */
Jens Axboefebffd62008-01-28 13:19:43 +01002436static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Jens Axboe7b679132008-05-30 12:23:07 +02002438 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002439 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2440 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002442 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2443 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2444 cfqq->service_tree = NULL;
2445 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002446 if (cfqq->p_root) {
2447 rb_erase(&cfqq->p_node, cfqq->p_root);
2448 cfqq->p_root = NULL;
2449 }
Jens Axboed9e76202007-04-20 14:27:50 +02002450
Justin TerAvest8184f932011-03-17 16:12:36 +01002451 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 BUG_ON(!cfqd->busy_queues);
2453 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002454 if (cfq_cfqq_sync(cfqq))
2455 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458/*
2459 * rb tree support functions
2460 */
Jens Axboefebffd62008-01-28 13:19:43 +01002461static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Jens Axboe5e705372006-07-13 12:39:25 +02002463 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002464 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Jens Axboeb4878f22005-10-20 16:42:29 +02002466 BUG_ON(!cfqq->queued[sync]);
2467 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Jens Axboe5e705372006-07-13 12:39:25 +02002469 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Vivek Goyalf04a6422009-12-03 12:59:40 -05002471 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2472 /*
2473 * Queue will be deleted from service tree when we actually
2474 * expire it later. Right now just remove it from prio tree
2475 * as it is empty.
2476 */
2477 if (cfqq->p_root) {
2478 rb_erase(&cfqq->p_node, cfqq->p_root);
2479 cfqq->p_root = NULL;
2480 }
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Jens Axboe5e705372006-07-13 12:39:25 +02002484static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Jens Axboe5e705372006-07-13 12:39:25 +02002486 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002488 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Jens Axboe5380a102006-07-13 12:37:56 +02002490 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Jeff Moyer796d5112011-06-02 21:19:05 +02002492 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002493
2494 if (!cfq_cfqq_on_rr(cfqq))
2495 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002496
2497 /*
2498 * check if this request is a better next-serve candidate
2499 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002500 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002501 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002502
2503 /*
2504 * adjust priority tree position, if ->next_rq changes
2505 */
2506 if (prev != cfqq->next_rq)
2507 cfq_prio_tree_add(cfqd, cfqq);
2508
Jens Axboe5044eed2007-04-25 11:53:48 +02002509 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Jens Axboefebffd62008-01-28 13:19:43 +01002512static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Jens Axboe5380a102006-07-13 12:37:56 +02002514 elv_rb_del(&cfqq->sort_list, rq);
2515 cfqq->queued[rq_is_sync(rq)]--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002516 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002517 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002518 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002519 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Jens Axboe206dc692006-03-28 13:03:44 +02002522static struct request *
2523cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Jens Axboe206dc692006-03-28 13:03:44 +02002525 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002526 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002527 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Jens Axboe4ac845a2008-01-24 08:44:49 +01002529 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002530 if (!cic)
2531 return NULL;
2532
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002533 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002534 if (cfqq)
2535 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return NULL;
2538}
2539
Jens Axboe165125e2007-07-24 09:28:11 +02002540static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002541{
2542 struct cfq_data *cfqd = q->elevator->elevator_data;
2543
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002544 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002545 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002546 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002547
Tejun Heo5b936292009-05-07 22:24:38 +09002548 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002549}
2550
Jens Axboe165125e2007-07-24 09:28:11 +02002551static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Jens Axboe22e2c502005-06-27 10:55:12 +02002553 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002555 WARN_ON(!cfqd->rq_in_driver);
2556 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002557 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002558 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Jens Axboeb4878f22005-10-20 16:42:29 +02002561static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Jens Axboe5e705372006-07-13 12:39:25 +02002563 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002564
Jens Axboe5e705372006-07-13 12:39:25 +02002565 if (cfqq->next_rq == rq)
2566 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Jens Axboeb4878f22005-10-20 16:42:29 +02002568 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002569 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002570
Aaron Carroll45333d52008-08-26 15:52:36 +02002571 cfqq->cfqd->rq_queued--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002572 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002573 if (rq->cmd_flags & REQ_PRIO) {
2574 WARN_ON(!cfqq->prio_pending);
2575 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002579static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
Jens Axboe165125e2007-07-24 09:28:11 +02002580 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct cfq_data *cfqd = q->elevator->elevator_data;
2583 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Jens Axboe206dc692006-03-28 13:03:44 +02002585 __rq = cfq_find_rq_fmerge(cfqd, bio);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002586 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002587 *req = __rq;
2588 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
2591 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Jens Axboe165125e2007-07-24 09:28:11 +02002594static void cfq_merged_request(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002595 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Jens Axboe21183b02006-07-13 12:33:14 +02002597 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002598 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Jens Axboe5e705372006-07-13 12:39:25 +02002600 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Divyesh Shah812d4022010-04-08 21:14:23 -07002604static void cfq_bio_merged(struct request_queue *q, struct request *req,
2605 struct bio *bio)
2606{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002607 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
Divyesh Shah812d4022010-04-08 21:14:23 -07002608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610static void
Jens Axboe165125e2007-07-24 09:28:11 +02002611cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct request *next)
2613{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002614 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002615 struct cfq_data *cfqd = q->elevator->elevator_data;
2616
Jens Axboe22e2c502005-06-27 10:55:12 +02002617 /*
2618 * reposition in fifo if next is older than rq
2619 */
2620 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002621 next->fifo_time < rq->fifo_time &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002622 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002623 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002624 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002625 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002626
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002627 if (cfqq->next_rq == next)
2628 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002629 cfq_remove_request(next);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002630 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002631
2632 cfqq = RQ_CFQQ(next);
2633 /*
2634 * all requests of this queue are merged to other queues, delete it
2635 * from the service tree. If it's the active_queue,
2636 * cfq_dispatch_requests() will choose to expire it or do idle
2637 */
2638 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2639 cfqq != cfqd->active_queue)
2640 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002641}
2642
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002643static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2644 struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +01002645{
2646 struct cfq_data *cfqd = q->elevator->elevator_data;
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002647 bool is_sync = op_is_sync(bio->bi_opf);
Tejun Heoc5869802011-12-14 00:33:41 +01002648 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002649 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002650
2651 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002652 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002653 */
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002654 if (is_sync && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002655 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002656
2657 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002658 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002659 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002660 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002661 cic = cfq_cic_lookup(cfqd, current->io_context);
2662 if (!cic)
2663 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002664
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002665 cfqq = cic_to_cfqq(cic, is_sync);
Jens Axboea6151c32009-10-07 20:02:57 +02002666 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002667}
2668
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002669static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2670 struct request *next)
2671{
2672 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2673}
2674
Divyesh Shah812df482010-04-08 21:15:35 -07002675static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2676{
Jan Kara91148322016-06-08 15:11:39 +02002677 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002678 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002679}
2680
Jens Axboefebffd62008-01-28 13:19:43 +01002681static void __cfq_set_active_queue(struct cfq_data *cfqd,
2682 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002683{
2684 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002685 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002686 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002687 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002688 cfqq->slice_start = 0;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002689 cfqq->dispatch_start = ktime_get_ns();
Justin TerAvest62a37f62011-03-23 08:25:44 +01002690 cfqq->allocated_slice = 0;
2691 cfqq->slice_end = 0;
2692 cfqq->slice_dispatch = 0;
2693 cfqq->nr_sectors = 0;
2694
2695 cfq_clear_cfqq_wait_request(cfqq);
2696 cfq_clear_cfqq_must_dispatch(cfqq);
2697 cfq_clear_cfqq_must_alloc_slice(cfqq);
2698 cfq_clear_cfqq_fifo_expire(cfqq);
2699 cfq_mark_cfqq_slice_new(cfqq);
2700
2701 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002702 }
2703
2704 cfqd->active_queue = cfqq;
2705}
2706
2707/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002708 * current cfqq expired its slice (or was too idle), select new one
2709 */
2710static void
2711__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002712 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002713{
Jens Axboe7b679132008-05-30 12:23:07 +02002714 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2715
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002716 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002717 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002718
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002719 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002720 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002721
2722 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002723 * If this cfqq is shared between multiple processes, check to
2724 * make sure that those processes are still issuing I/Os within
2725 * the mean seek distance. If not, it may be time to break the
2726 * queues apart again.
2727 */
2728 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2729 cfq_mark_cfqq_split_coop(cfqq);
2730
2731 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002732 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002733 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002734 if (timed_out) {
2735 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002736 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002737 else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002738 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
Jan Kara93fdf142016-06-28 09:04:00 +02002739 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
Jens Axboe7b679132008-05-30 12:23:07 +02002740 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002741
Vivek Goyale5ff0822010-04-26 19:25:11 +02002742 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002743
Vivek Goyalf04a6422009-12-03 12:59:40 -05002744 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2745 cfq_del_cfqq_rr(cfqd, cfqq);
2746
Jens Axboeedd75ff2007-04-19 12:03:34 +02002747 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002748
2749 if (cfqq == cfqd->active_queue)
2750 cfqd->active_queue = NULL;
2751
2752 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002753 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002754 cfqd->active_cic = NULL;
2755 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002756}
2757
Vivek Goyale5ff0822010-04-26 19:25:11 +02002758static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002759{
2760 struct cfq_queue *cfqq = cfqd->active_queue;
2761
2762 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002763 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002764}
2765
Jens Axboe498d3aa22007-04-26 12:54:48 +02002766/*
2767 * Get next queue for service. Unless we have a queue preemption,
2768 * we'll simply select the first cfqq in the service tree.
2769 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002770static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002771{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002772 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2773 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002774
Vivek Goyalf04a6422009-12-03 12:59:40 -05002775 if (!cfqd->rq_queued)
2776 return NULL;
2777
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002778 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002779 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002780 return NULL;
Davidlohr Bueso09663c82017-09-08 16:15:05 -07002781 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002782 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002783 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002784}
2785
Vivek Goyalf04a6422009-12-03 12:59:40 -05002786static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2787{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002788 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002789 struct cfq_queue *cfqq;
2790 int i, j;
2791 struct cfq_rb_root *st;
2792
2793 if (!cfqd->rq_queued)
2794 return NULL;
2795
Vivek Goyal25fb5162009-12-03 12:59:46 -05002796 cfqg = cfq_get_next_cfqg(cfqd);
2797 if (!cfqg)
2798 return NULL;
2799
Markus Elfring1cf41752017-01-21 22:44:07 +01002800 for_each_cfqg_st(cfqg, i, j, st) {
2801 cfqq = cfq_rb_first(st);
2802 if (cfqq)
Vivek Goyalf04a6422009-12-03 12:59:40 -05002803 return cfqq;
Markus Elfring1cf41752017-01-21 22:44:07 +01002804 }
Vivek Goyalf04a6422009-12-03 12:59:40 -05002805 return NULL;
2806}
2807
Jens Axboe498d3aa22007-04-26 12:54:48 +02002808/*
2809 * Get and set a new active queue for service.
2810 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002811static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2812 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002813{
Jens Axboee00ef792009-11-04 08:54:55 +01002814 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002815 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002816
Jens Axboe22e2c502005-06-27 10:55:12 +02002817 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002818 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002819}
2820
Jens Axboed9e76202007-04-20 14:27:50 +02002821static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2822 struct request *rq)
2823{
Tejun Heo83096eb2009-05-07 22:24:39 +09002824 if (blk_rq_pos(rq) >= cfqd->last_position)
2825 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002826 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002827 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002828}
2829
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002830static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002831 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002832{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002833 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002834}
2835
Jens Axboea36e71f2009-04-15 12:15:11 +02002836static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2837 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002838{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002839 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002840 struct rb_node *parent, *node;
2841 struct cfq_queue *__cfqq;
2842 sector_t sector = cfqd->last_position;
2843
2844 if (RB_EMPTY_ROOT(root))
2845 return NULL;
2846
2847 /*
2848 * First, if we find a request starting at the end of the last
2849 * request, choose it.
2850 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002851 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002852 if (__cfqq)
2853 return __cfqq;
2854
2855 /*
2856 * If the exact sector wasn't found, the parent of the NULL leaf
2857 * will contain the closest sector.
2858 */
2859 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002860 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002861 return __cfqq;
2862
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002863 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002864 node = rb_next(&__cfqq->p_node);
2865 else
2866 node = rb_prev(&__cfqq->p_node);
2867 if (!node)
2868 return NULL;
2869
2870 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002871 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002872 return __cfqq;
2873
2874 return NULL;
2875}
2876
2877/*
2878 * cfqd - obvious
2879 * cur_cfqq - passed in so that we don't decide that the current queue is
2880 * closely cooperating with itself.
2881 *
2882 * So, basically we're assuming that that cur_cfqq has dispatched at least
2883 * one request, and that cfqd->last_position reflects a position on the disk
2884 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2885 * assumption.
2886 */
2887static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002888 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002889{
2890 struct cfq_queue *cfqq;
2891
Divyesh Shah39c01b22010-03-25 15:45:57 +01002892 if (cfq_class_idle(cur_cfqq))
2893 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002894 if (!cfq_cfqq_sync(cur_cfqq))
2895 return NULL;
2896 if (CFQQ_SEEKY(cur_cfqq))
2897 return NULL;
2898
Jens Axboea36e71f2009-04-15 12:15:11 +02002899 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002900 * Don't search priority tree if it's the only queue in the group.
2901 */
2902 if (cur_cfqq->cfqg->nr_cfqq == 1)
2903 return NULL;
2904
2905 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002906 * We should notice if some of the queues are cooperating, eg
2907 * working closely on the same area of the disk. In that case,
2908 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002909 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002910 cfqq = cfqq_close(cfqd, cur_cfqq);
2911 if (!cfqq)
2912 return NULL;
2913
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002914 /* If new queue belongs to different cfq_group, don't choose it */
2915 if (cur_cfqq->cfqg != cfqq->cfqg)
2916 return NULL;
2917
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002918 /*
2919 * It only makes sense to merge sync queues.
2920 */
2921 if (!cfq_cfqq_sync(cfqq))
2922 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002923 if (CFQQ_SEEKY(cfqq))
2924 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002925
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002926 /*
2927 * Do not merge queues of different priority classes
2928 */
2929 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2930 return NULL;
2931
Jens Axboea36e71f2009-04-15 12:15:11 +02002932 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002933}
2934
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002935/*
2936 * Determine whether we should enforce idle window for this queue.
2937 */
2938
2939static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2940{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002941 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002942 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002943
Vivek Goyal34b98d02012-10-03 16:56:58 -04002944 BUG_ON(!st);
2945 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002946
Vivek Goyalb6508c12010-08-23 12:23:33 +02002947 if (!cfqd->cfq_slice_idle)
2948 return false;
2949
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002950 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002951 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002952 return false;
2953
2954 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002955 if (cfq_cfqq_idle_window(cfqq) &&
2956 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002957 return true;
2958
2959 /*
2960 * Otherwise, we do only if they are the last ones
2961 * in their service tree.
2962 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002963 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2964 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002965 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002966 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002967 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002968}
2969
Jens Axboe6d048f52007-04-25 12:44:27 +02002970static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002971{
Jens Axboe17926692007-01-19 11:59:30 +11002972 struct cfq_queue *cfqq = cfqd->active_queue;
Jan Karae7954212016-01-12 16:24:15 +01002973 struct cfq_rb_root *st = cfqq->service_tree;
Tejun Heoc5869802011-12-14 00:33:41 +01002974 struct cfq_io_cq *cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002975 u64 sl, group_idle = 0;
2976 u64 now = ktime_get_ns();
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002977
Jens Axboea68bbddba2008-09-24 13:03:33 +02002978 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002979 * SSD device without seek penalty, disable idling. But only do so
2980 * for devices that support queuing, otherwise we still have a problem
2981 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002982 */
Ritesh Harjanib3193bc2017-08-09 18:28:32 +05302983 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag &&
Rick Yiu82d2ef72018-09-26 16:45:50 +08002984 !get_group_idle(cfqd))
Jens Axboea68bbddba2008-09-24 13:03:33 +02002985 return;
2986
Jens Axboedd67d052006-06-21 09:36:18 +02002987 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002988 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002989
2990 /*
2991 * idle is disabled, either manually or by past process history
2992 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002993 if (!cfq_should_idle(cfqd, cfqq)) {
2994 /* no queue idling. Check for group idling */
Rick Yiu82d2ef72018-09-26 16:45:50 +08002995 group_idle = get_group_idle(cfqd);
2996 if (!group_idle)
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002997 return;
2998 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002999
Jens Axboe22e2c502005-06-27 10:55:12 +02003000 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003001 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02003002 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01003003 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02003004 return;
3005
3006 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02003007 * task has exited, don't wait
3008 */
Jens Axboe206dc692006-03-28 13:03:44 +02003009 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08003010 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02003011 return;
3012
Corrado Zoccolo355b6592009-10-08 08:43:32 +02003013 /*
3014 * If our average think time is larger than the remaining time
3015 * slice, then don't idle. This avoids overrunning the allotted
3016 * time slice.
3017 */
Shaohua Li383cd722011-07-12 14:24:35 +02003018 if (sample_valid(cic->ttime.ttime_samples) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003019 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
3020 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
Shaohua Li383cd722011-07-12 14:24:35 +02003021 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02003022 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01003023 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02003024
Jan Karae7954212016-01-12 16:24:15 +01003025 /*
3026 * There are other queues in the group or this is the only group and
3027 * it has too big thinktime, don't do group idle.
3028 */
3029 if (group_idle &&
3030 (cfqq->cfqg->nr_cfqq > 1 ||
3031 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003032 return;
3033
Jens Axboe3b181522005-06-27 10:56:24 +02003034 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003035
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003036 if (group_idle)
Rick Yiu82d2ef72018-09-26 16:45:50 +08003037 sl = group_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003038 else
3039 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02003040
Jan Kara91148322016-06-08 15:11:39 +02003041 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
3042 HRTIMER_MODE_REL);
Tejun Heo155fead2012-04-01 14:38:44 -07003043 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003044 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003045 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Jens Axboe498d3aa22007-04-26 12:54:48 +02003048/*
3049 * Move request from internal lists to the request queue dispatch list.
3050 */
Jens Axboe165125e2007-07-24 09:28:11 +02003051static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Jens Axboe3ed9a292007-04-23 08:33:33 +02003053 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003054 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003055
Jens Axboe7b679132008-05-30 12:23:07 +02003056 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
3057
Jeff Moyer06d21882009-09-11 17:08:59 +02003058 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02003059 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02003060 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003061 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02003062 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02003063
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003064 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02003065 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068/*
3069 * return expired entry, or NULL to just start from scratch in rbtree
3070 */
Jens Axboefebffd62008-01-28 13:19:43 +01003071static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Jens Axboe30996f42009-10-05 11:03:39 +02003073 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Jens Axboe3b181522005-06-27 10:56:24 +02003075 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11003077
3078 cfq_mark_cfqq_fifo_expire(cfqq);
3079
Jens Axboe89850f72006-07-22 16:48:31 +02003080 if (list_empty(&cfqq->fifo))
3081 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Jens Axboe89850f72006-07-22 16:48:31 +02003083 rq = rq_entry_fifo(cfqq->fifo.next);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003084 if (ktime_get_ns() < rq->fifo_time)
Jens Axboe7b679132008-05-30 12:23:07 +02003085 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Jens Axboe6d048f52007-04-25 12:44:27 +02003087 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Jens Axboe22e2c502005-06-27 10:55:12 +02003090static inline int
3091cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3092{
3093 const int base_rq = cfqd->cfq_slice_async_rq;
3094
3095 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3096
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02003097 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003098}
3099
3100/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003101 * Must be called with the queue_lock held.
3102 */
3103static int cfqq_process_refs(struct cfq_queue *cfqq)
3104{
3105 int process_refs, io_refs;
3106
3107 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01003108 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003109 BUG_ON(process_refs < 0);
3110 return process_refs;
3111}
3112
3113static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3114{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003115 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003116 struct cfq_queue *__cfqq;
3117
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003118 /*
3119 * If there are no process references on the new_cfqq, then it is
3120 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3121 * chain may have dropped their last reference (not just their
3122 * last process reference).
3123 */
3124 if (!cfqq_process_refs(new_cfqq))
3125 return;
3126
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003127 /* Avoid a circular list and skip interim queue merges */
3128 while ((__cfqq = new_cfqq->new_cfqq)) {
3129 if (__cfqq == cfqq)
3130 return;
3131 new_cfqq = __cfqq;
3132 }
3133
3134 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003135 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003136 /*
3137 * If the process for the cfqq has gone away, there is no
3138 * sense in merging the queues.
3139 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003140 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003141 return;
3142
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003143 /*
3144 * Merge in the direction of the lesser amount of work.
3145 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003146 if (new_process_refs >= process_refs) {
3147 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003148 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003149 } else {
3150 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003151 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003152 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003153}
3154
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003155static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003156 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003157{
3158 struct cfq_queue *queue;
3159 int i;
3160 bool key_valid = false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003161 u64 lowest_key = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003162 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3163
Vivek Goyal65b32a52009-12-16 17:52:59 -05003164 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3165 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003166 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003167 if (queue &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003168 (!key_valid || queue->rb_key < lowest_key)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003169 lowest_key = queue->rb_key;
3170 cur_best = i;
3171 key_valid = true;
3172 }
3173 }
3174
3175 return cur_best;
3176}
3177
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003178static void
3179choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003180{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003181 u64 slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003182 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003183 struct cfq_rb_root *st;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003184 u64 group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003185 enum wl_class_t original_class = cfqd->serving_wl_class;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003186 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003187
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003188 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003189 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003190 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003191 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003192 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003193 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003194 cfqd->serving_wl_class = IDLE_WORKLOAD;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003195 cfqd->workload_expires = now + jiffies_to_nsecs(1);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003196 return;
3197 }
3198
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003199 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003200 goto new_workload;
3201
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003202 /*
3203 * For RT and BE, we have to choose also the type
3204 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3205 * expiration time
3206 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003207 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003208 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003209
3210 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003211 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003212 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003213 if (count && !(now > cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003214 return;
3215
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003216new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003217 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003218 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003219 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003220 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003221 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003222
3223 /*
3224 * the workload slice is computed as a fraction of target latency
3225 * proportional to the number of queues in that workload, over
3226 * all the queues in the same priority class
3227 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003228 group_slice = cfq_group_slice(cfqd, cfqg);
3229
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003230 slice = div_u64(group_slice * count,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003231 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3232 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003233 cfqg)));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003234
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003235 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003236 u64 tmp;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003237
3238 /*
3239 * Async queues are currently system wide. Just taking
3240 * proportion of queues with-in same group will lead to higher
3241 * async ratio system wide as generally root group is going
3242 * to have higher weight. A more accurate thing would be to
3243 * calculate system wide asnc/sync ratio.
3244 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003245 tmp = cfqd->cfq_target_latency *
3246 cfqg_busy_async_queues(cfqd, cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003247 tmp = div_u64(tmp, cfqd->busy_queues);
3248 slice = min_t(u64, slice, tmp);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003249
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003250 /* async workload slice is scaled down according to
3251 * the sync/async slice ratio. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003252 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003253 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003254 /* sync workload slice is at least 2 * cfq_slice_idle */
3255 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3256
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003257 slice = max_t(u64, slice, CFQ_MIN_TT);
3258 cfq_log(cfqd, "workload slice:%llu", slice);
3259 cfqd->workload_expires = now + slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003260}
3261
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003262static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3263{
3264 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003265 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003266
Davidlohr Bueso09663c82017-09-08 16:15:05 -07003267 if (RB_EMPTY_ROOT(&st->rb.rb_root))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003268 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003269 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003270 update_min_vdisktime(st);
3271 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003272}
3273
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003274static void cfq_choose_cfqg(struct cfq_data *cfqd)
3275{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003276 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003277 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003278
3279 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003280
3281 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003282 if (cfqg->saved_wl_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003283 cfqd->workload_expires = now + cfqg->saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003284 cfqd->serving_wl_type = cfqg->saved_wl_type;
3285 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003286 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003287 cfqd->workload_expires = now - 1;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003288
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003289 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003290}
3291
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003292/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003293 * Select a queue for service. If we have a current active queue,
3294 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003295 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003296static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003297{
Jens Axboea36e71f2009-04-15 12:15:11 +02003298 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003299 u64 now = ktime_get_ns();
Jens Axboe22e2c502005-06-27 10:55:12 +02003300
3301 cfqq = cfqd->active_queue;
3302 if (!cfqq)
3303 goto new_queue;
3304
Vivek Goyalf04a6422009-12-03 12:59:40 -05003305 if (!cfqd->rq_queued)
3306 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003307
3308 /*
3309 * We were waiting for group to get backlogged. Expire the queue
3310 */
3311 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3312 goto expire;
3313
Jens Axboe22e2c502005-06-27 10:55:12 +02003314 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003315 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003316 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003317 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3318 /*
3319 * If slice had not expired at the completion of last request
3320 * we might not have turned on wait_busy flag. Don't expire
3321 * the queue yet. Allow the group to get backlogged.
3322 *
3323 * The very fact that we have used the slice, that means we
3324 * have been idling all along on this queue and it should be
3325 * ok to wait for this request to complete.
3326 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003327 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3328 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3329 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003330 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003331 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003332 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003333 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003334
3335 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003336 * The active queue has requests and isn't expired, allow it to
3337 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003338 */
Jens Axboedd67d052006-06-21 09:36:18 +02003339 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003340 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003341
3342 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003343 * If another queue has a request waiting within our mean seek
3344 * distance, let it run. The expire code will check for close
3345 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003346 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003347 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003348 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003349 if (new_cfqq) {
3350 if (!cfqq->new_cfqq)
3351 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003352 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003353 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003354
3355 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003356 * No requests pending. If the active queue still has requests in
3357 * flight or is idling for a new request, allow either of these
3358 * conditions to happen (or time out) before selecting a new queue.
3359 */
Jan Kara91148322016-06-08 15:11:39 +02003360 if (hrtimer_active(&cfqd->idle_slice_timer)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003361 cfqq = NULL;
3362 goto keep_queue;
3363 }
3364
Shaohua Li8e1ac662010-11-08 15:01:04 +01003365 /*
3366 * This is a deep seek queue, but the device is much faster than
3367 * the queue can deliver, don't idle
3368 **/
3369 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3370 (cfq_cfqq_slice_new(cfqq) ||
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003371 (cfqq->slice_end - now > now - cfqq->slice_start))) {
Shaohua Li8e1ac662010-11-08 15:01:04 +01003372 cfq_clear_cfqq_deep(cfqq);
3373 cfq_clear_cfqq_idle_window(cfqq);
3374 }
3375
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003376 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3377 cfqq = NULL;
3378 goto keep_queue;
3379 }
3380
3381 /*
3382 * If group idle is enabled and there are requests dispatched from
3383 * this group, wait for requests to complete.
3384 */
3385check_group_idle:
Rick Yiu82d2ef72018-09-26 16:45:50 +08003386 if (get_group_idle(cfqd) && cfqq->cfqg->nr_cfqq == 1 &&
Shaohua Li7700fc42011-07-12 14:24:56 +02003387 cfqq->cfqg->dispatched &&
3388 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003389 cfqq = NULL;
3390 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003391 }
3392
Jens Axboe3b181522005-06-27 10:56:24 +02003393expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003394 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003395new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003396 /*
3397 * Current queue expired. Check if we have to switch to a new
3398 * service tree
3399 */
3400 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003401 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003402
Jens Axboea36e71f2009-04-15 12:15:11 +02003403 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003404keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003405 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003406}
3407
Jens Axboefebffd62008-01-28 13:19:43 +01003408static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003409{
3410 int dispatched = 0;
3411
3412 while (cfqq->next_rq) {
3413 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3414 dispatched++;
3415 }
3416
3417 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003418
3419 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003420 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003421 return dispatched;
3422}
3423
Jens Axboe498d3aa22007-04-26 12:54:48 +02003424/*
3425 * Drain our current requests. Used for barriers and when switching
3426 * io schedulers on-the-fly.
3427 */
Jens Axboed9e76202007-04-20 14:27:50 +02003428static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003429{
Jens Axboe08717142008-01-28 11:38:15 +01003430 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003431 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003432
Divyesh Shah3440c492010-04-09 09:29:57 +02003433 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003434 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003435 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3436 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003437 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003438 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003439
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003440 BUG_ON(cfqd->busy_queues);
3441
Jeff Moyer69237152009-06-12 15:29:30 +02003442 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003443 return dispatched;
3444}
3445
Shaohua Liabc3c742010-03-01 09:20:54 +01003446static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3447 struct cfq_queue *cfqq)
3448{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003449 u64 now = ktime_get_ns();
3450
Shaohua Liabc3c742010-03-01 09:20:54 +01003451 /* the queue hasn't finished any request, can't estimate */
3452 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003453 return true;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003454 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01003455 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003456
Shaohua Lic1e44752010-11-08 15:01:02 +01003457 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003458}
3459
Jens Axboe0b182d62009-10-06 20:49:37 +02003460static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003461{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003462 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Glauber Costa3932a862016-09-22 20:59:59 -04003464 if (cfq_cfqq_must_dispatch(cfqq))
3465 return true;
3466
Jens Axboe2f5cb732009-04-07 08:51:19 +02003467 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003468 * Drain async requests before we start sync IO
3469 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003470 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003471 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003472
3473 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003474 * If this is an async queue and we have sync IO in flight, let it wait
3475 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003476 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003477 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003478
Shaohua Liabc3c742010-03-01 09:20:54 +01003479 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003480 if (cfq_class_idle(cfqq))
3481 max_dispatch = 1;
3482
3483 /*
3484 * Does this cfqq already have too much IO in flight?
3485 */
3486 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003487 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003488 /*
3489 * idle queue must always only have a single IO in flight
3490 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003491 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003492 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003493
Jens Axboe2f5cb732009-04-07 08:51:19 +02003494 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003495 * If there is only one sync queue
3496 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003497 * queue no dispatch limit. The reason is a sync queue can
3498 * preempt async queue, limiting the sync queue doesn't make
3499 * sense. This is useful for aiostress test.
3500 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003501 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3502 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003503
3504 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003505 * We have other queues, don't allow more IO from this one
3506 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003507 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3508 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003509 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003510
Jens Axboe2f5cb732009-04-07 08:51:19 +02003511 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003512 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003513 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003514 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003515 max_dispatch = -1;
3516 else
3517 /*
3518 * Normally we start throttling cfqq when cfq_quantum/2
3519 * requests have been dispatched. But we can drive
3520 * deeper queue depths at the beginning of slice
3521 * subjected to upper limit of cfq_quantum.
3522 * */
3523 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003524 }
3525
3526 /*
3527 * Async queues must wait a bit before being allowed dispatch.
3528 * We also ramp up the dispatch depth gradually for async IO,
3529 * based on the last sync IO we serviced
3530 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003531 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003532 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003533 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003534
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003535 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
Jens Axboee00c54c2009-10-04 20:36:19 +02003536 if (!depth && !cfqq->dispatched)
3537 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003538 if (depth < max_dispatch)
3539 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
3541
Jens Axboe0b182d62009-10-06 20:49:37 +02003542 /*
3543 * If we're below the current max, allow a dispatch
3544 */
3545 return cfqq->dispatched < max_dispatch;
3546}
3547
3548/*
3549 * Dispatch a request from cfqq, moving them to the request queue
3550 * dispatch list.
3551 */
3552static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3553{
3554 struct request *rq;
3555
3556 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3557
Glauber Costa3932a862016-09-22 20:59:59 -04003558 rq = cfq_check_fifo(cfqq);
3559 if (rq)
3560 cfq_mark_cfqq_must_dispatch(cfqq);
3561
Jens Axboe0b182d62009-10-06 20:49:37 +02003562 if (!cfq_may_dispatch(cfqd, cfqq))
3563 return false;
3564
3565 /*
3566 * follow expired path, else get first next available
3567 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003568 if (!rq)
3569 rq = cfqq->next_rq;
Glauber Costa3932a862016-09-22 20:59:59 -04003570 else
3571 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003572
3573 /*
3574 * insert request into driver dispatch list
3575 */
3576 cfq_dispatch_insert(cfqd->queue, rq);
3577
3578 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003579 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003580
Tejun Heoc5869802011-12-14 00:33:41 +01003581 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003582 cfqd->active_cic = cic;
3583 }
3584
3585 return true;
3586}
3587
3588/*
3589 * Find the cfqq that we need to service and move a request from that to the
3590 * dispatch list
3591 */
3592static int cfq_dispatch_requests(struct request_queue *q, int force)
3593{
3594 struct cfq_data *cfqd = q->elevator->elevator_data;
3595 struct cfq_queue *cfqq;
3596
3597 if (!cfqd->busy_queues)
3598 return 0;
3599
3600 if (unlikely(force))
3601 return cfq_forced_dispatch(cfqd);
3602
3603 cfqq = cfq_select_queue(cfqd);
3604 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003605 return 0;
3606
Jens Axboe2f5cb732009-04-07 08:51:19 +02003607 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003608 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003609 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003610 if (!cfq_dispatch_request(cfqd, cfqq))
3611 return 0;
3612
Jens Axboe2f5cb732009-04-07 08:51:19 +02003613 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003614 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003615
3616 /*
3617 * expire an async queue immediately if it has used up its slice. idle
3618 * queue always expire after 1 dispatch round.
3619 */
3620 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3621 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3622 cfq_class_idle(cfqq))) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003623 cfqq->slice_end = ktime_get_ns() + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003624 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003625 }
3626
Shan Weib217a902009-09-01 10:06:42 +02003627 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003628 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631/*
Jens Axboe5e705372006-07-13 12:39:25 +02003632 * task holds one reference to the queue, dropped when task exits. each rq
3633 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003635 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 * queue lock must be held here.
3637 */
3638static void cfq_put_queue(struct cfq_queue *cfqq)
3639{
Jens Axboe22e2c502005-06-27 10:55:12 +02003640 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003641 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003642
Shaohua Li30d7b942011-01-07 08:46:59 +01003643 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Shaohua Li30d7b942011-01-07 08:46:59 +01003645 cfqq->ref--;
3646 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return;
3648
Jens Axboe7b679132008-05-30 12:23:07 +02003649 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003651 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003652 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003654 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003655 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003656 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003657 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003658
Vivek Goyalf04a6422009-12-03 12:59:40 -05003659 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003661 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Shaohua Lid02a2c02010-05-25 10:16:53 +02003664static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003665{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003666 struct cfq_queue *__cfqq, *next;
3667
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003668 /*
3669 * If this queue was scheduled to merge with another queue, be
3670 * sure to drop the reference taken on that queue (and others in
3671 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3672 */
3673 __cfqq = cfqq->new_cfqq;
3674 while (__cfqq) {
3675 if (__cfqq == cfqq) {
3676 WARN(1, "cfqq->new_cfqq loop detected\n");
3677 break;
3678 }
3679 next = __cfqq->new_cfqq;
3680 cfq_put_queue(__cfqq);
3681 __cfqq = next;
3682 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003683}
3684
3685static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3686{
3687 if (unlikely(cfqq == cfqd->active_queue)) {
3688 __cfq_slice_expired(cfqd, cfqq, 0);
3689 cfq_schedule_dispatch(cfqd);
3690 }
3691
3692 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003693
Jens Axboe89850f72006-07-22 16:48:31 +02003694 cfq_put_queue(cfqq);
3695}
3696
Tejun Heo9b84cac2011-12-14 00:33:42 +01003697static void cfq_init_icq(struct io_cq *icq)
3698{
3699 struct cfq_io_cq *cic = icq_to_cic(icq);
3700
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003701 cic->ttime.last_end_request = ktime_get_ns();
Tejun Heo9b84cac2011-12-14 00:33:42 +01003702}
3703
Tejun Heoc5869802011-12-14 00:33:41 +01003704static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003705{
Tejun Heoc5869802011-12-14 00:33:41 +01003706 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003707 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003708
Tejun Heo563180a2015-08-18 14:55:00 -07003709 if (cic_to_cfqq(cic, false)) {
3710 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3711 cic_set_cfqq(cic, NULL, false);
Jens Axboe89850f72006-07-22 16:48:31 +02003712 }
3713
Tejun Heo563180a2015-08-18 14:55:00 -07003714 if (cic_to_cfqq(cic, true)) {
3715 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3716 cic_set_cfqq(cic, NULL, true);
Jens Axboe89850f72006-07-22 16:48:31 +02003717 }
Jens Axboe89850f72006-07-22 16:48:31 +02003718}
3719
Tejun Heoabede6d2012-03-19 15:10:57 -07003720static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003721{
3722 struct task_struct *tsk = current;
3723 int ioprio_class;
3724
Jens Axboe3b181522005-06-27 10:56:24 +02003725 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003726 return;
3727
Tejun Heo598971b2012-03-19 15:10:58 -07003728 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003729 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003730 default:
3731 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
Bart Van Assche9b4f4342018-08-07 16:17:28 -07003732 /* fall through */
Jens Axboefe094d92008-01-31 13:08:54 +01003733 case IOPRIO_CLASS_NONE:
3734 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003735 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003736 */
3737 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003738 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003739 break;
3740 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003741 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003742 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3743 break;
3744 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003745 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003746 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3747 break;
3748 case IOPRIO_CLASS_IDLE:
3749 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3750 cfqq->ioprio = 7;
3751 cfq_clear_cfqq_idle_window(cfqq);
3752 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003753 }
3754
3755 /*
3756 * keep track of original prio settings in case we have to temporarily
3757 * elevate the priority of this queue
3758 */
3759 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -06003760 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02003761 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003762}
3763
Tejun Heo598971b2012-03-19 15:10:58 -07003764static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003765{
Tejun Heo598971b2012-03-19 15:10:58 -07003766 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003767 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003768 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003769
Tejun Heo598971b2012-03-19 15:10:58 -07003770 /*
3771 * Check whether ioprio has changed. The condition may trigger
3772 * spuriously on a newly created cic but there's no harm.
3773 */
3774 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003775 return;
3776
Tejun Heo563180a2015-08-18 14:55:00 -07003777 cfqq = cic_to_cfqq(cic, false);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003778 if (cfqq) {
Tejun Heo563180a2015-08-18 14:55:00 -07003779 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07003780 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
Tejun Heo563180a2015-08-18 14:55:00 -07003781 cic_set_cfqq(cic, cfqq, false);
Jens Axboe22e2c502005-06-27 10:55:12 +02003782 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003783
Tejun Heo563180a2015-08-18 14:55:00 -07003784 cfqq = cic_to_cfqq(cic, true);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003785 if (cfqq)
3786 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003787
3788 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003789}
3790
Jens Axboed5036d72009-06-26 10:44:34 +02003791static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003792 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003793{
3794 RB_CLEAR_NODE(&cfqq->rb_node);
3795 RB_CLEAR_NODE(&cfqq->p_node);
3796 INIT_LIST_HEAD(&cfqq->fifo);
3797
Shaohua Li30d7b942011-01-07 08:46:59 +01003798 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003799 cfqq->cfqd = cfqd;
3800
3801 cfq_mark_cfqq_prio_changed(cfqq);
3802
3803 if (is_sync) {
3804 if (!cfq_class_idle(cfqq))
3805 cfq_mark_cfqq_idle_window(cfqq);
3806 cfq_mark_cfqq_sync(cfqq);
3807 }
3808 cfqq->pid = pid;
3809}
3810
Vivek Goyal246103332009-12-03 12:59:51 -05003811#ifdef CONFIG_CFQ_GROUP_IOSCHED
Jan Kara142bbdf2017-04-04 14:31:30 +02003812static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003813{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003814 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo60a83702015-08-18 14:55:05 -07003815 struct cfq_queue *cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003816 uint64_t serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003817
Tejun Heo598971b2012-03-19 15:10:58 -07003818 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003819 serial_nr = bio_blkcg(bio)->css.serial_nr;
Tejun Heo598971b2012-03-19 15:10:58 -07003820 rcu_read_unlock();
3821
3822 /*
3823 * Check whether blkcg has changed. The condition may trigger
3824 * spuriously on a newly created cic but there's no harm.
3825 */
Tejun Heof4da8072014-09-08 08:15:20 +09003826 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Jan Kara142bbdf2017-04-04 14:31:30 +02003827 return;
Jens Axboe87760e52016-11-09 12:38:14 -07003828
3829 /*
Tejun Heo60a83702015-08-18 14:55:05 -07003830 * Drop reference to queues. New queues will be assigned in new
3831 * group upon arrival of fresh requests.
3832 */
3833 cfqq = cic_to_cfqq(cic, false);
3834 if (cfqq) {
3835 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3836 cic_set_cfqq(cic, NULL, false);
3837 cfq_put_queue(cfqq);
3838 }
3839
3840 cfqq = cic_to_cfqq(cic, true);
3841 if (cfqq) {
3842 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3843 cic_set_cfqq(cic, NULL, true);
3844 cfq_put_queue(cfqq);
Vivek Goyal246103332009-12-03 12:59:51 -05003845 }
Tejun Heo598971b2012-03-19 15:10:58 -07003846
Tejun Heof4da8072014-09-08 08:15:20 +09003847 cic->blkcg_serial_nr = serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003848}
Tejun Heo598971b2012-03-19 15:10:58 -07003849#else
Jan Kara142bbdf2017-04-04 14:31:30 +02003850static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003851{
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003852}
Vivek Goyal246103332009-12-03 12:59:51 -05003853#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3854
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003855static struct cfq_queue **
Tejun Heo60a83702015-08-18 14:55:05 -07003856cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003857{
Jens Axboefe094d92008-01-31 13:08:54 +01003858 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003859 case IOPRIO_CLASS_RT:
Tejun Heo60a83702015-08-18 14:55:05 -07003860 return &cfqg->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003861 case IOPRIO_CLASS_NONE:
3862 ioprio = IOPRIO_NORM;
3863 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003864 case IOPRIO_CLASS_BE:
Tejun Heo60a83702015-08-18 14:55:05 -07003865 return &cfqg->async_cfqq[1][ioprio];
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003866 case IOPRIO_CLASS_IDLE:
Tejun Heo60a83702015-08-18 14:55:05 -07003867 return &cfqg->async_idle_cfqq;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003868 default:
3869 BUG();
3870 }
3871}
3872
Jens Axboe15c31be2007-07-10 13:43:25 +02003873static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003874cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo2da8de02015-08-18 14:55:02 -07003875 struct bio *bio)
Jens Axboe15c31be2007-07-10 13:43:25 +02003876{
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003877 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3878 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Tejun Heod4aad7f2015-08-18 14:55:04 -07003879 struct cfq_queue **async_cfqq = NULL;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003880 struct cfq_queue *cfqq;
Tejun Heo322731e2015-08-18 14:55:03 -07003881 struct cfq_group *cfqg;
3882
3883 rcu_read_lock();
Tejun Heoae118892015-08-18 14:55:20 -07003884 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
Tejun Heo322731e2015-08-18 14:55:03 -07003885 if (!cfqg) {
3886 cfqq = &cfqd->oom_cfqq;
3887 goto out;
3888 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003889
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003890 if (!is_sync) {
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003891 if (!ioprio_valid(cic->ioprio)) {
3892 struct task_struct *tsk = current;
3893 ioprio = task_nice_ioprio(tsk);
3894 ioprio_class = task_nice_ioclass(tsk);
3895 }
Tejun Heo60a83702015-08-18 14:55:05 -07003896 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003897 cfqq = *async_cfqq;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003898 if (cfqq)
3899 goto out;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003900 }
3901
Tejun Heoe00f4f42016-11-21 18:03:32 -05003902 cfqq = kmem_cache_alloc_node(cfq_pool,
3903 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
Tejun Heod4aad7f2015-08-18 14:55:04 -07003904 cfqd->queue->node);
3905 if (!cfqq) {
3906 cfqq = &cfqd->oom_cfqq;
3907 goto out;
3908 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003909
Alexander Potapenko4d608ba2017-01-23 15:06:43 +01003910 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3911 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
Tejun Heod4aad7f2015-08-18 14:55:04 -07003912 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3913 cfq_init_prio_data(cfqq, cic);
3914 cfq_link_cfqq_cfqg(cfqq, cfqg);
3915 cfq_log_cfqq(cfqd, cfqq, "alloced");
3916
3917 if (async_cfqq) {
3918 /* a new async queue is created, pin and remember */
Shaohua Li30d7b942011-01-07 08:46:59 +01003919 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003920 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003921 }
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003922out:
Shaohua Li30d7b942011-01-07 08:46:59 +01003923 cfqq->ref++;
Tejun Heo322731e2015-08-18 14:55:03 -07003924 rcu_read_unlock();
Jens Axboe15c31be2007-07-10 13:43:25 +02003925 return cfqq;
3926}
3927
Jens Axboe22e2c502005-06-27 10:55:12 +02003928static void
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003929__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003930{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003931 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
Shaohua Li383cd722011-07-12 14:24:35 +02003932 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003933
Shaohua Li383cd722011-07-12 14:24:35 +02003934 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003935 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3936 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3937 ttime->ttime_samples);
Shaohua Li383cd722011-07-12 14:24:35 +02003938}
3939
3940static void
3941cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003942 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003943{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003944 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003945 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003946 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3947 cfqd->cfq_slice_idle);
3948 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003949#ifdef CONFIG_CFQ_GROUP_IOSCHED
Rick Yiu82d2ef72018-09-26 16:45:50 +08003950 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, get_group_idle(cfqd));
Shaohua Li7700fc42011-07-12 14:24:56 +02003951#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003952}
3953
Jens Axboe206dc692006-03-28 13:03:44 +02003954static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003955cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003956 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003957{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003958 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003959 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003960 if (cfqq->last_request_pos) {
3961 if (cfqq->last_request_pos < blk_rq_pos(rq))
3962 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3963 else
3964 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3965 }
Jens Axboe206dc692006-03-28 13:03:44 +02003966
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003967 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003968 if (blk_queue_nonrot(cfqd->queue))
3969 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3970 else
3971 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003972}
Jens Axboe22e2c502005-06-27 10:55:12 +02003973
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003974static inline bool req_noidle(struct request *req)
3975{
3976 return req_op(req) == REQ_OP_WRITE &&
3977 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3978}
3979
Jens Axboe22e2c502005-06-27 10:55:12 +02003980/*
3981 * Disable idle window if the process thinks too long or seeks so much that
3982 * it doesn't matter
3983 */
3984static void
3985cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003986 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003987{
Jens Axboe7b679132008-05-30 12:23:07 +02003988 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003989
Jens Axboe08717142008-01-28 11:38:15 +01003990 /*
3991 * Don't idle for async or idle io prio class
3992 */
3993 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003994 return;
3995
Jens Axboec265a7f2008-06-26 13:49:33 +02003996 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003997
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003998 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3999 cfq_mark_cfqq_deep(cfqq);
4000
Christoph Hellwiga2b80962016-11-01 07:40:09 -06004001 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004002 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08004003 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01004004 !cfqd->cfq_slice_idle ||
4005 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02004006 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02004007 else if (sample_valid(cic->ttime.ttime_samples)) {
4008 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02004009 enable_idle = 0;
4010 else
4011 enable_idle = 1;
4012 }
4013
Jens Axboe7b679132008-05-30 12:23:07 +02004014 if (old_idle != enable_idle) {
4015 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
4016 if (enable_idle)
4017 cfq_mark_cfqq_idle_window(cfqq);
4018 else
4019 cfq_clear_cfqq_idle_window(cfqq);
4020 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004021}
4022
Jens Axboe22e2c502005-06-27 10:55:12 +02004023/*
4024 * Check if new_cfqq should preempt the currently active queue. Return 0 for
4025 * no or if we aren't sure, a 1 will cause a preempt.
4026 */
Jens Axboea6151c32009-10-07 20:02:57 +02004027static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02004028cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02004029 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004030{
Jens Axboe6d048f52007-04-25 12:44:27 +02004031 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02004032
Jens Axboe6d048f52007-04-25 12:44:27 +02004033 cfqq = cfqd->active_queue;
4034 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02004035 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004036
Jens Axboe6d048f52007-04-25 12:44:27 +02004037 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004038 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004039
4040 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004041 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004042
Jens Axboe22e2c502005-06-27 10:55:12 +02004043 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08004044 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
4045 */
4046 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
4047 return false;
4048
4049 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02004050 * if the new request is sync, but the currently running queue is
4051 * not, let the sync request have priority.
4052 */
Glauber Costa3932a862016-09-22 20:59:59 -04004053 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004054 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004055
Jan Kara3984aa52016-01-12 16:24:19 +01004056 /*
4057 * Treat ancestors of current cgroup the same way as current cgroup.
4058 * For anybody else we disallow preemption to guarantee service
4059 * fairness among cgroups.
4060 */
4061 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004062 return false;
4063
4064 if (cfq_slice_used(cfqq))
4065 return true;
4066
Jan Kara6c80731c2016-01-12 16:24:16 +01004067 /*
4068 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4069 */
4070 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4071 return true;
4072
4073 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004074 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004075 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004076 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004077 RB_EMPTY_ROOT(&cfqq->sort_list))
4078 return true;
4079
Jens Axboe374f84a2006-07-23 01:42:19 +02004080 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004081 * So both queues are sync. Let the new request get disk time if
4082 * it's a metadata request and the current queue is doing regular IO.
4083 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02004084 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004085 return true;
4086
Shaohua Lid2d59e12010-11-08 15:01:03 +01004087 /* An idle queue should not be idle now for some reason */
4088 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4089 return true;
4090
Jens Axboe1e3335d2007-02-14 19:59:49 +01004091 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004092 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004093
4094 /*
4095 * if this request is as-good as one we would expect from the
4096 * current cfqq, let it preempt
4097 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01004098 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02004099 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004100
Jens Axboea6151c32009-10-07 20:02:57 +02004101 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004102}
4103
4104/*
4105 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4106 * let it have half of its nominal slice.
4107 */
4108static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4109{
Shaohua Lidf0793a2012-01-19 09:20:09 +01004110 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4111
Jens Axboe7b679132008-05-30 12:23:07 +02004112 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01004113 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004114
Jens Axboebf572252006-07-19 20:29:12 +02004115 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004116 * workload type is changed, don't save slice, otherwise preempt
4117 * doesn't happen
4118 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01004119 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004120 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004121
4122 /*
Jens Axboebf572252006-07-19 20:29:12 +02004123 * Put the new queue at the front of the of the current list,
4124 * so we know that it will be selected next.
4125 */
4126 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02004127
4128 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01004129
Justin TerAvest62a37f62011-03-23 08:25:44 +01004130 cfqq->slice_end = 0;
4131 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004132}
4133
4134/*
Jens Axboe5e705372006-07-13 12:39:25 +02004135 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02004136 * something we should do about it
4137 */
4138static void
Jens Axboe5e705372006-07-13 12:39:25 +02004139cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4140 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004141{
Tejun Heoc5869802011-12-14 00:33:41 +01004142 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02004143
Aaron Carroll45333d52008-08-26 15:52:36 +02004144 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02004145 if (rq->cmd_flags & REQ_PRIO)
4146 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02004147
Shaohua Li383cd722011-07-12 14:24:35 +02004148 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004149 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02004150 cfq_update_idle_window(cfqd, cfqq, cic);
4151
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004152 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004153
4154 if (cfqq == cfqd->active_queue) {
4155 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004156 * Remember that we saw a request from this process, but
4157 * don't start queuing just yet. Otherwise we risk seeing lots
4158 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02004159 * and merging. If the request is already larger than a single
4160 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02004161 * merging is already done. Ditto for a busy system that
4162 * has other work pending, don't risk delaying until the
4163 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02004164 */
Jens Axboed6ceb252009-04-14 14:18:16 +02004165 if (cfq_cfqq_wait_request(cfqq)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004166 if (blk_rq_bytes(rq) > PAGE_SIZE ||
Jens Axboe2d870722009-04-15 12:12:46 +02004167 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07004168 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01004169 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004170 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004171 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07004172 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05004173 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004174 }
Jens Axboed6ceb252009-04-14 14:18:16 +02004175 }
Jens Axboe5e705372006-07-13 12:39:25 +02004176 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004177 /*
4178 * not the active queue - expire current slice if it is
4179 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01004180 * has some old slice time left and is of higher priority or
4181 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02004182 */
4183 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004184 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004185 }
4186}
4187
Jens Axboe165125e2007-07-24 09:28:11 +02004188static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004189{
Jens Axboeb4878f22005-10-20 16:42:29 +02004190 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02004191 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004192
Jens Axboe7b679132008-05-30 12:23:07 +02004193 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07004194 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004196 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
Jens Axboe22e2c502005-06-27 10:55:12 +02004197 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01004198 cfq_add_rq_rb(rq);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004199 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
Tejun Heo155fead2012-04-01 14:38:44 -07004200 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02004201 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Aaron Carroll45333d52008-08-26 15:52:36 +02004204/*
4205 * Update hw_tag based on peak queue depth over 50 samples under
4206 * sufficient load.
4207 */
4208static void cfq_update_hw_tag(struct cfq_data *cfqd)
4209{
Shaohua Li1a1238a2009-10-27 08:46:23 +01004210 struct cfq_queue *cfqq = cfqd->active_queue;
4211
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004212 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4213 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004214
4215 if (cfqd->hw_tag == 1)
4216 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02004217
4218 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004219 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004220 return;
4221
Shaohua Li1a1238a2009-10-27 08:46:23 +01004222 /*
4223 * If active queue hasn't enough requests and can idle, cfq might not
4224 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4225 * case
4226 */
4227 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4228 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004229 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01004230 return;
4231
Aaron Carroll45333d52008-08-26 15:52:36 +02004232 if (cfqd->hw_tag_samples++ < 50)
4233 return;
4234
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004235 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004236 cfqd->hw_tag = 1;
4237 else
4238 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02004239}
4240
Vivek Goyal7667aa02009-12-08 17:52:58 -05004241static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4242{
Tejun Heoc5869802011-12-14 00:33:41 +01004243 struct cfq_io_cq *cic = cfqd->active_cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004244 u64 now = ktime_get_ns();
Vivek Goyal7667aa02009-12-08 17:52:58 -05004245
Justin TerAvest02a8f012011-02-09 14:20:03 +01004246 /* If the queue already has requests, don't wait */
4247 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4248 return false;
4249
Vivek Goyal7667aa02009-12-08 17:52:58 -05004250 /* If there are other queues in the group, don't wait */
4251 if (cfqq->cfqg->nr_cfqq > 1)
4252 return false;
4253
Shaohua Li7700fc42011-07-12 14:24:56 +02004254 /* the only queue in the group, but think time is big */
4255 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4256 return false;
4257
Vivek Goyal7667aa02009-12-08 17:52:58 -05004258 if (cfq_slice_used(cfqq))
4259 return true;
4260
4261 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02004262 if (cic && sample_valid(cic->ttime.ttime_samples)
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004263 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004264 return true;
4265
4266 /*
4267 * If think times is less than a jiffy than ttime_mean=0 and above
4268 * will not be true. It might happen that slice has not expired yet
4269 * but will expire soon (4-5 ns) during select_queue(). To cover the
4270 * case where think time is less than a jiffy, mark the queue wait
4271 * busy if only 1 jiffy is left in the slice.
4272 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004273 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004274 return true;
4275
4276 return false;
4277}
4278
Jens Axboe165125e2007-07-24 09:28:11 +02004279static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Jens Axboe5e705372006-07-13 12:39:25 +02004281 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004282 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02004283 const int sync = rq_is_sync(rq);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004284 u64 now = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Christoph Hellwiga2b80962016-11-01 07:40:09 -06004286 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Aaron Carroll45333d52008-08-26 15:52:36 +02004288 cfq_update_hw_tag(cfqd);
4289
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004290 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02004291 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004292 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02004293 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004294 (RQ_CFQG(rq))->dispatched--;
Omar Sandoval522a7772018-05-09 02:08:53 -07004295 cfqg_stats_update_completion(cfqq->cfqg, rq->start_time_ns,
4296 rq->io_start_time_ns, rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004298 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02004299
Vivek Goyal365722b2009-10-03 15:21:27 +02004300 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04004301 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004302
Shaohua Li383cd722011-07-12 14:24:35 +02004303 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004304
4305 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04004306 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004307 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04004308 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4309 cfqq_type(cfqq));
4310
4311 st->ttime.last_end_request = now;
Omar Sandoval522a7772018-05-09 02:08:53 -07004312 if (rq->start_time_ns + cfqd->cfq_fifo_expire[1] <= now)
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004313 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02004314 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004315
Shaohua Li7700fc42011-07-12 14:24:56 +02004316#ifdef CONFIG_CFQ_GROUP_IOSCHED
4317 cfqq->cfqg->ttime.last_end_request = now;
4318#endif
4319
Jens Axboecaaa5f92006-06-16 11:23:00 +02004320 /*
4321 * If this is the active queue, check if it needs to be expired,
4322 * or if we want to idle in case it has no pending requests.
4323 */
4324 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02004325 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4326
Jens Axboe44f7c162007-01-19 11:51:58 +11004327 if (cfq_cfqq_slice_new(cfqq)) {
4328 cfq_set_prio_slice(cfqd, cfqq);
4329 cfq_clear_cfqq_slice_new(cfqq);
4330 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05004331
4332 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05004333 * Should we wait for next request to come in before we expire
4334 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05004335 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05004336 if (cfq_should_wait_busy(cfqd, cfqq)) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004337 u64 extend_sl = cfqd->cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004338 if (!cfqd->cfq_slice_idle)
Rick Yiu82d2ef72018-09-26 16:45:50 +08004339 extend_sl = get_group_idle(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004340 cfqq->slice_end = now + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05004341 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01004342 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05004343 }
4344
Jens Axboea36e71f2009-04-15 12:15:11 +02004345 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004346 * Idling is not enabled on:
4347 * - expired queues
4348 * - idle-priority queues
4349 * - async queues
4350 * - queues with still some requests queued
4351 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02004352 */
Jens Axboe08717142008-01-28 11:38:15 +01004353 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02004354 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004355 else if (sync && cfqq_empty &&
4356 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004357 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004358 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004359 }
Jens Axboe6d048f52007-04-25 12:44:27 +02004360
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004361 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02004362 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004365static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
Jens Axboeb8269db2016-06-09 15:47:29 -06004366{
4367 /*
4368 * If REQ_PRIO is set, boost class and prio level, if it's below
4369 * BE/NORM. If prio is not set, restore the potentially boosted
4370 * class/prio level.
4371 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004372 if (!(op & REQ_PRIO)) {
Jens Axboeb8269db2016-06-09 15:47:29 -06004373 cfqq->ioprio_class = cfqq->org_ioprio_class;
4374 cfqq->ioprio = cfqq->org_ioprio;
4375 } else {
4376 if (cfq_class_idle(cfqq))
4377 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4378 if (cfqq->ioprio > IOPRIO_NORM)
4379 cfqq->ioprio = IOPRIO_NORM;
4380 }
4381}
4382
Jens Axboe89850f72006-07-22 16:48:31 +02004383static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004384{
Jens Axboe1b379d82009-08-11 08:26:11 +02004385 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02004386 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004387 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02004388 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004389
4390 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02004391}
4392
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004393static int cfq_may_queue(struct request_queue *q, unsigned int op)
Jens Axboe22e2c502005-06-27 10:55:12 +02004394{
4395 struct cfq_data *cfqd = q->elevator->elevator_data;
4396 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01004397 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02004398 struct cfq_queue *cfqq;
4399
4400 /*
4401 * don't force setup of a queue from here, as a call to may_queue
4402 * does not necessarily imply that a request actually will be queued.
4403 * so just lookup a possibly existing queue, or return 'may queue'
4404 * if that fails
4405 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01004406 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004407 if (!cic)
4408 return ELV_MQUEUE_MAY;
4409
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004410 cfqq = cic_to_cfqq(cic, op_is_sync(op));
Jens Axboe22e2c502005-06-27 10:55:12 +02004411 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004412 cfq_init_prio_data(cfqq, cic);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004413 cfqq_boost_on_prio(cfqq, op);
Jens Axboe22e2c502005-06-27 10:55:12 +02004414
Jens Axboe89850f72006-07-22 16:48:31 +02004415 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004416 }
4417
4418 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419}
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421/*
4422 * queue lock held here
4423 */
Jens Axboebb37b942006-12-01 10:42:33 +01004424static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Jens Axboe5e705372006-07-13 12:39:25 +02004426 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Jens Axboe5e705372006-07-13 12:39:25 +02004428 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004429 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Jens Axboe22e2c502005-06-27 10:55:12 +02004431 BUG_ON(!cfqq->allocated[rw]);
4432 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004434 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004435 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01004436 rq->elv.priv[0] = NULL;
4437 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 cfq_put_queue(cfqq);
4440 }
4441}
4442
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004443static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004444cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004445 struct cfq_queue *cfqq)
4446{
4447 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4448 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004449 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004450 cfq_put_queue(cfqq);
4451 return cic_to_cfqq(cic, 1);
4452}
4453
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004454/*
4455 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4456 * was the last process referring to said cfqq.
4457 */
4458static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004459split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004460{
4461 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004462 cfqq->pid = current->pid;
4463 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004464 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004465 return cfqq;
4466 }
4467
4468 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004469
4470 cfq_put_cooperator(cfqq);
4471
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004472 cfq_put_queue(cfqq);
4473 return NULL;
4474}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004476 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004478static int
Tejun Heo852c7882012-03-05 13:15:27 -08004479cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4480 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
4482 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004483 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004485 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004486 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Tejun Heo216284c32011-12-14 00:33:38 +01004488 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004489
Tejun Heo598971b2012-03-19 15:10:58 -07004490 check_ioprio_changed(cic, bio);
Jan Kara142bbdf2017-04-04 14:31:30 +02004491 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004492new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004493 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004494 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heobce61332015-08-18 14:54:59 -07004495 if (cfqq)
4496 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07004497 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004498 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004499 } else {
4500 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004501 * If the queue was seeky for too long, break it apart.
4502 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004503 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004504 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4505 cfqq = split_cfqq(cic, cfqq);
4506 if (!cfqq)
4507 goto new_queue;
4508 }
4509
4510 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004511 * Check to see if this queue is scheduled to merge with
4512 * another, closely cooperating queue. The merging of
4513 * queues happens here as it must be done in process context.
4514 * The reference on new_cfqq was taken in merge_cfqqs.
4515 */
4516 if (cfqq->new_cfqq)
4517 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004521
Jens Axboe6fae9c22011-03-01 15:04:39 -05004522 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004523 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004524 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004525 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c32011-12-14 00:33:38 +01004526 spin_unlock_irq(q->queue_lock);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004527
Jens Axboe5e705372006-07-13 12:39:25 +02004528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
4530
David Howells65f27f32006-11-22 14:55:48 +00004531static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004532{
David Howells65f27f32006-11-22 14:55:48 +00004533 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004534 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004535 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004536
Jens Axboe40bb54d2009-04-15 12:11:10 +02004537 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004538 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004539 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004540}
4541
4542/*
4543 * Timer running if the active_queue is currently idling inside its time slice
4544 */
Jan Kara91148322016-06-08 15:11:39 +02004545static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
Jens Axboe22e2c502005-06-27 10:55:12 +02004546{
Jan Kara91148322016-06-08 15:11:39 +02004547 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4548 idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02004549 struct cfq_queue *cfqq;
4550 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004551 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004552
Jens Axboe7b679132008-05-30 12:23:07 +02004553 cfq_log(cfqd, "idle timer fired");
4554
Jens Axboe22e2c502005-06-27 10:55:12 +02004555 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4556
Jens Axboefe094d92008-01-31 13:08:54 +01004557 cfqq = cfqd->active_queue;
4558 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004559 timed_out = 0;
4560
Jens Axboe22e2c502005-06-27 10:55:12 +02004561 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004562 * We saw a request before the queue expired, let it through
4563 */
4564 if (cfq_cfqq_must_dispatch(cfqq))
4565 goto out_kick;
4566
4567 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004568 * expired
4569 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004570 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004571 goto expire;
4572
4573 /*
4574 * only expire and reinvoke request handler, if there are
4575 * other queues with pending requests
4576 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004577 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004578 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004579
4580 /*
4581 * not expired and it has a request pending, let it dispatch
4582 */
Jens Axboe75e50982009-04-07 08:56:14 +02004583 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004584 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004585
4586 /*
4587 * Queue depth flag is reset only when the idle didn't succeed
4588 */
4589 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004590 }
4591expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004592 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004593out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004594 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004595out_cont:
4596 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jan Kara91148322016-06-08 15:11:39 +02004597 return HRTIMER_NORESTART;
Jens Axboe22e2c502005-06-27 10:55:12 +02004598}
4599
Jens Axboe3b181522005-06-27 10:56:24 +02004600static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4601{
Jan Kara91148322016-06-08 15:11:39 +02004602 hrtimer_cancel(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004603 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004604}
Jens Axboe22e2c502005-06-27 10:55:12 +02004605
Jens Axboeb374d182008-10-31 10:05:07 +01004606static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Jens Axboe22e2c502005-06-27 10:55:12 +02004608 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004609 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004610
Jens Axboe3b181522005-06-27 10:56:24 +02004611 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004612
Al Virod9ff4182006-03-18 13:51:22 -05004613 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004614
Al Virod9ff4182006-03-18 13:51:22 -05004615 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004616 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004617
Tejun Heo03aa2642012-03-05 13:15:19 -08004618 spin_unlock_irq(q->queue_lock);
4619
Al Viroa90d7422006-03-18 12:05:37 -05004620 cfq_shutdown_timer_wq(cfqd);
4621
Tejun Heoffea73f2012-06-04 10:02:29 +02004622#ifdef CONFIG_CFQ_GROUP_IOSCHED
4623 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4624#else
Tejun Heof51b8022012-03-05 13:15:05 -08004625 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004626#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004627 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Jianpeng Mad50235b2013-07-03 13:25:24 +02004630static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
4632 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004633 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004634 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004635 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Jianpeng Mad50235b2013-07-03 13:25:24 +02004637 eq = elevator_alloc(q, e);
4638 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004639 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004640
Joe Perchesc1b511e2013-08-29 15:21:42 -07004641 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004642 if (!cfqd) {
4643 kobject_put(&eq->kobj);
4644 return -ENOMEM;
4645 }
4646 eq->elevator_data = cfqd;
4647
Tejun Heof51b8022012-03-05 13:15:05 -08004648 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004649 spin_lock_irq(q->queue_lock);
4650 q->elevator = eq;
4651 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004652
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004653 /* Init root service tree */
4654 cfqd->grp_service_tree = CFQ_RB_ROOT;
4655
Tejun Heof51b8022012-03-05 13:15:05 -08004656 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004657#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004658 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004659 if (ret)
4660 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004661
Tejun Heoa2b16932012-04-13 13:11:33 -07004662 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004663#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004664 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004665 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4666 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004667 if (!cfqd->root_group)
4668 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004669
Tejun Heoa2b16932012-04-13 13:11:33 -07004670 cfq_init_cfqg_base(cfqd->root_group);
Tejun Heo3ecca622015-08-18 14:55:35 -07004671 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4672 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
Tejun Heo69d7fde2015-08-18 14:55:36 -07004673#endif
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004674
Jens Axboe26a2ac02009-04-23 12:13:27 +02004675 /*
4676 * Not strictly needed (since RB_ROOT just clears the node and we
4677 * zeroed cfqd on alloc), but better be safe in case someone decides
4678 * to add magic to the rb code
4679 */
4680 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4681 cfqd->prio_trees[i] = RB_ROOT;
4682
Jens Axboe6118b702009-06-30 09:34:12 +02004683 /*
Tejun Heod4aad7f2015-08-18 14:55:04 -07004684 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
Jens Axboe6118b702009-06-30 09:34:12 +02004685 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004686 * will not attempt to free it. oom_cfqq is linked to root_group
4687 * but shouldn't hold a reference as it'll never be unlinked. Lose
4688 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004689 */
4690 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004691 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004692
4693 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004694 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004695 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004696 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Jan Kara91148322016-06-08 15:11:39 +02004698 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4699 HRTIMER_MODE_REL);
Jens Axboe22e2c502005-06-27 10:55:12 +02004700 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
Jens Axboe22e2c502005-06-27 10:55:12 +02004701
Jens Axboe23e018a2009-10-05 08:52:35 +02004702 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004703
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004705 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4706 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 cfqd->cfq_back_max = cfq_back_max;
4708 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004709 cfqd->cfq_slice[0] = cfq_slice_async;
4710 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004711 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004712 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004713 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004714 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004715 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004716 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004717 /*
4718 * we optimistically start assuming sync ops weren't delayed in last
4719 * second, in order to have larger depth for async operations.
4720 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004721 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004722 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004723
4724out_free:
4725 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004726 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004727 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Jens Axboe0bb97942015-06-10 08:01:20 -06004730static void cfq_registered_queue(struct request_queue *q)
4731{
4732 struct elevator_queue *e = q->elevator;
4733 struct cfq_data *cfqd = e->elevator_data;
4734
4735 /*
4736 * Default to IOPS mode with no idling for SSDs
4737 */
4738 if (blk_queue_nonrot(q))
4739 cfqd->cfq_slice_idle = 0;
Jan Kara142bbdf2017-04-04 14:31:30 +02004740 wbt_disable_default(q);
Jens Axboe0bb97942015-06-10 08:01:20 -06004741}
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/*
4744 * sysfs parts below -->
4745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746static ssize_t
4747cfq_var_show(unsigned int var, char *page)
4748{
Masanari Iida176167a2014-04-28 12:38:34 +09004749 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
4751
weiping zhang235f8da2017-08-25 01:11:33 +08004752static void
4753cfq_var_store(unsigned int *var, const char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
4755 char *p = (char *) page;
4756
4757 *var = simple_strtoul(p, &p, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004761static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004763 struct cfq_data *cfqd = e->elevator_data; \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004764 u64 __data = __VAR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004766 __data = div_u64(__data, NSEC_PER_MSEC); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 return cfq_var_show(__data, (page)); \
4768}
4769SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004770SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4771SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004772SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4773SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004774SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004775SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004776SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4777SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4778SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004779SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004780SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781#undef SHOW_FUNCTION
4782
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004783#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4784static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4785{ \
4786 struct cfq_data *cfqd = e->elevator_data; \
4787 u64 __data = __VAR; \
4788 __data = div_u64(__data, NSEC_PER_USEC); \
4789 return cfq_var_show(__data, (page)); \
4790}
4791USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4792USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4793USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4794USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4795USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4796#undef USEC_SHOW_FUNCTION
4797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004799static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004801 struct cfq_data *cfqd = e->elevator_data; \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004802 unsigned int __data, __min = (MIN), __max = (MAX); \
4803 \
weiping zhang235f8da2017-08-25 01:11:33 +08004804 cfq_var_store(&__data, (page)); \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004805 if (__data < __min) \
4806 __data = __min; \
4807 else if (__data > __max) \
4808 __data = __max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004810 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 else \
4812 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08004813 return count; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
4815STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004816STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4817 UINT_MAX, 1);
4818STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4819 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004820STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004821STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4822 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004823STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004824STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004825STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4826STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004827STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4828 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004829STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004830STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831#undef STORE_FUNCTION
4832
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004833#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4834static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4835{ \
4836 struct cfq_data *cfqd = e->elevator_data; \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004837 unsigned int __data, __min = (MIN), __max = (MAX); \
4838 \
weiping zhang235f8da2017-08-25 01:11:33 +08004839 cfq_var_store(&__data, (page)); \
Bart Van Asschef7ecb1b2018-08-07 16:17:29 -07004840 if (__data < __min) \
4841 __data = __min; \
4842 else if (__data > __max) \
4843 __data = __max; \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004844 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
weiping zhang235f8da2017-08-25 01:11:33 +08004845 return count; \
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004846}
4847USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4848USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4849USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4850USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4851USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4852#undef USEC_STORE_FUNCTION
4853
Al Viroe572ec72006-03-18 22:27:18 -05004854#define CFQ_ATTR(name) \
Joe Perches5657a812018-05-24 13:38:59 -06004855 __ATTR(name, 0644, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004856
Al Viroe572ec72006-03-18 22:27:18 -05004857static struct elv_fs_entry cfq_attrs[] = {
4858 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004859 CFQ_ATTR(fifo_expire_sync),
4860 CFQ_ATTR(fifo_expire_async),
4861 CFQ_ATTR(back_seek_max),
4862 CFQ_ATTR(back_seek_penalty),
4863 CFQ_ATTR(slice_sync),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004864 CFQ_ATTR(slice_sync_us),
Al Viroe572ec72006-03-18 22:27:18 -05004865 CFQ_ATTR(slice_async),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004866 CFQ_ATTR(slice_async_us),
Al Viroe572ec72006-03-18 22:27:18 -05004867 CFQ_ATTR(slice_async_rq),
4868 CFQ_ATTR(slice_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004869 CFQ_ATTR(slice_idle_us),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004870 CFQ_ATTR(group_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004871 CFQ_ATTR(group_idle_us),
Jens Axboe963b72f2009-10-03 19:42:18 +02004872 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004873 CFQ_ATTR(target_latency),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004874 CFQ_ATTR(target_latency_us),
Al Viroe572ec72006-03-18 22:27:18 -05004875 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876};
4877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878static struct elevator_type iosched_cfq = {
Jens Axboec51ca6c2016-12-10 15:13:59 -07004879 .ops.sq = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 .elevator_merge_fn = cfq_merge,
4881 .elevator_merged_fn = cfq_merged_request,
4882 .elevator_merge_req_fn = cfq_merged_requests,
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07004883 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4884 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004885 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004886 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004888 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004891 .elevator_former_req_fn = elv_rb_former_request,
4892 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004893 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004894 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 .elevator_set_req_fn = cfq_set_request,
4896 .elevator_put_req_fn = cfq_put_request,
4897 .elevator_may_queue_fn = cfq_may_queue,
4898 .elevator_init_fn = cfq_init_queue,
4899 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004900 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004902 .icq_size = sizeof(struct cfq_io_cq),
4903 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004904 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004905 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 .elevator_owner = THIS_MODULE,
4907};
4908
Vivek Goyal3e252062009-12-04 10:36:42 -05004909#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004910static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07004911 .dfl_cftypes = cfq_blkcg_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07004912 .legacy_cftypes = cfq_blkcg_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004913
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004914 .cpd_alloc_fn = cfq_cpd_alloc,
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004915 .cpd_init_fn = cfq_cpd_init,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004916 .cpd_free_fn = cfq_cpd_free,
Tejun Heo69d7fde2015-08-18 14:55:36 -07004917 .cpd_bind_fn = cfq_cpd_bind,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004918
Tejun Heo001bea72015-08-18 14:55:11 -07004919 .pd_alloc_fn = cfq_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004920 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004921 .pd_offline_fn = cfq_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07004922 .pd_free_fn = cfq_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004923 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004924};
Vivek Goyal3e252062009-12-04 10:36:42 -05004925#endif
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927static int __init cfq_init(void)
4928{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004929 int ret;
4930
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004931#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004932 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004933 if (ret)
4934 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004935#else
4936 cfq_group_idle = 0;
4937#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004938
Tejun Heofd794952012-06-04 10:01:38 +02004939 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004940 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4941 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004942 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Tejun Heo3d3c2372011-12-14 00:33:42 +01004944 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004945 if (ret)
4946 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004947
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004948 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004949
4950err_free_pool:
4951 kmem_cache_destroy(cfq_pool);
4952err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004953#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004954 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004955#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004956 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
4958
4959static void __exit cfq_exit(void)
4960{
Tejun Heoffea73f2012-06-04 10:02:29 +02004961#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004962 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004965 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
4968module_init(cfq_init);
4969module_exit(cfq_exit);
4970
4971MODULE_AUTHOR("Jens Axboe");
4972MODULE_LICENSE("GPL");
4973MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");