blob: 3d5c289457191ed8e1718ac3ebb5892a5343ff66 [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 {
96 struct rb_root rb;
97 struct rb_node *left;
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};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200102#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600103 .ttime = {.last_end_request = ktime_get_ns(),},}
Jens Axboecc09e292007-04-26 12:53:50 +0200104
105/*
Jens Axboe6118b702009-06-30 09:34:12 +0200106 * Per process-grouping structure
107 */
108struct cfq_queue {
109 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100110 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200111 /* various state flags, see below */
112 unsigned int flags;
113 /* parent cfq_data */
114 struct cfq_data *cfqd;
115 /* service_tree member */
116 struct rb_node rb_node;
117 /* service_tree key */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600118 u64 rb_key;
Jens Axboe6118b702009-06-30 09:34:12 +0200119 /* prio tree member */
120 struct rb_node p_node;
121 /* prio tree root we belong to, if any */
122 struct rb_root *p_root;
123 /* sorted list of pending requests */
124 struct rb_root sort_list;
125 /* if fifo isn't expired, next request to serve */
126 struct request *next_rq;
127 /* requests queued in sort_list */
128 int queued[2];
129 /* currently allocated requests */
130 int allocated[2];
131 /* fifo list of requests in sort_list */
132 struct list_head fifo;
133
Vivek Goyaldae739e2009-12-03 12:59:45 -0500134 /* time when queue got scheduled in to dispatch first request. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600135 u64 dispatch_start;
136 u64 allocated_slice;
137 u64 slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500138 /* time when first request from queue completed and slice started. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600139 u64 slice_start;
140 u64 slice_end;
Jan Kara93fdf142016-06-28 09:04:00 +0200141 s64 slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200142
Christoph Hellwig65299a32011-08-23 14:50:29 +0200143 /* pending priority requests */
144 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200145 /* number of requests that are on the dispatch list or inside driver */
146 int dispatched;
147
148 /* io prio of this group */
149 unsigned short ioprio, org_ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -0600150 unsigned short ioprio_class, org_ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200151
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100152 pid_t pid;
153
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100154 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400155 sector_t last_request_pos;
156
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100157 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400158 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500159 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200160 /* Number of sectors dispatched from queue in single dispatch round */
161 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200162};
163
164/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100165 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100166 * IDLE is handled separately, so it has negative index
167 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400168enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100169 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500170 RT_WORKLOAD = 1,
171 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200172 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100173};
174
175/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100176 * Second index in the service_trees.
177 */
178enum wl_type_t {
179 ASYNC_WORKLOAD = 0,
180 SYNC_NOIDLE_WORKLOAD = 1,
181 SYNC_WORKLOAD = 2
182};
183
Tejun Heo155fead2012-04-01 14:38:44 -0700184struct cfqg_stats {
185#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo155fead2012-04-01 14:38:44 -0700186 /* number of ios merged */
187 struct blkg_rwstat merged;
188 /* total time spent on device in ns, may not be accurate w/ queueing */
189 struct blkg_rwstat service_time;
190 /* total time spent waiting in scheduler queue in ns */
191 struct blkg_rwstat wait_time;
192 /* number of IOs queued up */
193 struct blkg_rwstat queued;
Tejun Heo155fead2012-04-01 14:38:44 -0700194 /* total disk time and nr sectors dispatched by this group */
195 struct blkg_stat time;
196#ifdef CONFIG_DEBUG_BLK_CGROUP
197 /* time not charged to this cgroup */
198 struct blkg_stat unaccounted_time;
199 /* sum of number of ios queued across all samples */
200 struct blkg_stat avg_queue_size_sum;
201 /* count of samples taken for average */
202 struct blkg_stat avg_queue_size_samples;
203 /* how many times this group has been removed from service tree */
204 struct blkg_stat dequeue;
205 /* total time spent waiting for it to be assigned a timeslice. */
206 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700207 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700208 struct blkg_stat idle_time;
209 /* total time with empty current active q with other requests queued */
210 struct blkg_stat empty_time;
211 /* fields after this shouldn't be cleared on stat reset */
212 uint64_t start_group_wait_time;
213 uint64_t start_idle_time;
214 uint64_t start_empty_time;
215 uint16_t flags;
216#endif /* CONFIG_DEBUG_BLK_CGROUP */
217#endif /* CONFIG_CFQ_GROUP_IOSCHED */
218};
219
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200220/* Per-cgroup data */
221struct cfq_group_data {
222 /* must be the first member */
Tejun Heo81437642015-08-18 14:55:15 -0700223 struct blkcg_policy_data cpd;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200224
225 unsigned int weight;
226 unsigned int leaf_weight;
227};
228
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500229/* This is per cgroup per device grouping structure */
230struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700231 /* must be the first member */
232 struct blkg_policy_data pd;
233
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500234 /* group service_tree member */
235 struct rb_node rb_node;
236
237 /* group service_tree key */
238 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800239
240 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800241 * The number of active cfqgs and sum of their weights under this
242 * cfqg. This covers this cfqg's leaf_weight and all children's
243 * weights, but does not cover weights of further descendants.
244 *
245 * If a cfqg is on the service tree, it's active. An active cfqg
246 * also activates its parent and contributes to the children_weight
247 * of the parent.
248 */
249 int nr_active;
250 unsigned int children_weight;
251
252 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800253 * vfraction is the fraction of vdisktime that the tasks in this
254 * cfqg are entitled to. This is determined by compounding the
255 * ratios walking up from this cfqg to the root.
256 *
257 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
258 * vfractions on a service tree is approximately 1. The sum may
259 * deviate a bit due to rounding errors and fluctuations caused by
260 * cfqgs entering and leaving the service tree.
261 */
262 unsigned int vfraction;
263
264 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800265 * There are two weights - (internal) weight is the weight of this
266 * cfqg against the sibling cfqgs. leaf_weight is the wight of
267 * this cfqg against the child cfqgs. For the root cfqg, both
268 * weights are kept in sync for backward compatibility.
269 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500270 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100271 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700272 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500273
Tejun Heoe71357e2013-01-09 08:05:10 -0800274 unsigned int leaf_weight;
275 unsigned int new_leaf_weight;
276 unsigned int dev_leaf_weight;
277
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500278 /* number of cfqq currently on this group */
279 int nr_cfqq;
280
Jens Axboe22e2c502005-06-27 10:55:12 +0200281 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200282 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200283 * create the array for each prio class but at run time it is used
284 * only for RT and BE class and slot for IDLE class remains unused.
285 * This is primarily done to avoid confusion and a gcc warning.
286 */
287 unsigned int busy_queues_avg[CFQ_PRIO_NR];
288 /*
289 * rr lists of queues with requests. We maintain service trees for
290 * RT and BE classes. These trees are subdivided in subclasses
291 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
292 * class there is no subclassification and all the cfq queues go on
293 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100294 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200295 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100296 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100297 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500298
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600299 u64 saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400300 enum wl_type_t saved_wl_type;
301 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800302
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200303 /* number of requests that are on the dispatch list or inside driver */
304 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200305 struct cfq_ttime ttime;
Tejun Heo0b399202013-01-09 08:05:13 -0800306 struct cfqg_stats stats; /* stats for this cfqg */
Tejun Heo60a83702015-08-18 14:55:05 -0700307
308 /* async queue for each priority case */
309 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
310 struct cfq_queue *async_idle_cfqq;
311
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500312};
313
Tejun Heoc5869802011-12-14 00:33:41 +0100314struct cfq_io_cq {
315 struct io_cq icq; /* must be the first member */
316 struct cfq_queue *cfqq[2];
317 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700318 int ioprio; /* the current ioprio */
319#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof4da8072014-09-08 08:15:20 +0900320 uint64_t blkcg_serial_nr; /* the current blkcg serial */
Tejun Heo598971b2012-03-19 15:10:58 -0700321#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100322};
323
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500324/*
325 * Per block device queue structure
326 */
327struct cfq_data {
328 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500329 /* Root service tree for cfq_groups */
330 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800331 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500332
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100333 /*
334 * The priority currently being served
335 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400336 enum wl_class_t serving_wl_class;
337 enum wl_type_t serving_wl_type;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600338 u64 workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500339 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200340
341 /*
342 * Each priority tree is sorted by next_request position. These
343 * trees are used when determining if two or more queues are
344 * interleaving requests (see cfq_close_cooperator).
345 */
346 struct rb_root prio_trees[CFQ_PRIO_LISTS];
347
Jens Axboe22e2c502005-06-27 10:55:12 +0200348 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100349 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200350
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100351 int rq_in_driver;
352 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200353
354 /*
355 * queue-depth detection
356 */
357 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200358 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100359 /*
360 * hw_tag can be
361 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
362 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
363 * 0 => no NCQ
364 */
365 int hw_tag_est_depth;
366 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200367
368 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200369 * idle window management
370 */
Jan Kara91148322016-06-08 15:11:39 +0200371 struct hrtimer idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200372 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200373
374 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100375 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200376
Jens Axboe6d048f52007-04-25 12:44:27 +0200377 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * tunables, see top of file
381 */
382 unsigned int cfq_quantum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned int cfq_back_penalty;
384 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200385 unsigned int cfq_slice_async_rq;
Jens Axboe963b72f2009-10-03 19:42:18 +0200386 unsigned int cfq_latency;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600387 u64 cfq_fifo_expire[2];
388 u64 cfq_slice[2];
389 u64 cfq_slice_idle;
390 u64 cfq_group_idle;
391 u64 cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500392
Jens Axboe6118b702009-06-30 09:34:12 +0200393 /*
394 * Fallback dummy cfqq for extreme OOM conditions
395 */
396 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200397
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600398 u64 last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
Vivek Goyal25fb5162009-12-03 12:59:46 -0500401static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
Tejun Heo60a83702015-08-18 14:55:05 -0700402static void cfq_put_queue(struct cfq_queue *cfqq);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500403
Vivek Goyal34b98d02012-10-03 16:56:58 -0400404static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400405 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500406 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100407{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500408 if (!cfqg)
409 return NULL;
410
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400411 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500412 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100413
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400414 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100415}
416
Jens Axboe3b181522005-06-27 10:56:24 +0200417enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100418 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
419 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200420 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100421 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100422 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
423 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
424 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100425 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200426 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400427 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100428 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100429 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500430 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200431};
432
433#define CFQ_CFQQ_FNS(name) \
434static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
435{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100436 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200437} \
438static inline void cfq_clear_cfqq_##name(struct cfq_queue *cfqq) \
439{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100440 (cfqq)->flags &= ~(1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200441} \
442static inline int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
443{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100444 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200445}
446
447CFQ_CFQQ_FNS(on_rr);
448CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200449CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200450CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200451CFQ_CFQQ_FNS(fifo_expire);
452CFQ_CFQQ_FNS(idle_window);
453CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100454CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200455CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200456CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100457CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100458CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500459CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200460#undef CFQ_CFQQ_FNS
461
Tejun Heo629ed0b2012-04-01 14:38:44 -0700462#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700463
Tejun Heo155fead2012-04-01 14:38:44 -0700464/* cfqg stats flags */
465enum cfqg_stats_flags {
466 CFQG_stats_waiting = 0,
467 CFQG_stats_idling,
468 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700469};
470
Tejun Heo155fead2012-04-01 14:38:44 -0700471#define CFQG_FLAG_FNS(name) \
472static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700473{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700474 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700475} \
Tejun Heo155fead2012-04-01 14:38:44 -0700476static inline void cfqg_stats_clear_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700477{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700478 stats->flags &= ~(1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700479} \
Tejun Heo155fead2012-04-01 14:38:44 -0700480static inline int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700481{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700482 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700483} \
484
Tejun Heo155fead2012-04-01 14:38:44 -0700485CFQG_FLAG_FNS(waiting)
486CFQG_FLAG_FNS(idling)
487CFQG_FLAG_FNS(empty)
488#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700489
490/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700491static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700492{
493 unsigned long long now;
494
Tejun Heo155fead2012-04-01 14:38:44 -0700495 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700496 return;
497
498 now = sched_clock();
499 if (time_after64(now, stats->start_group_wait_time))
500 blkg_stat_add(&stats->group_wait_time,
501 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700502 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700503}
504
505/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700506static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
507 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700508{
Tejun Heo155fead2012-04-01 14:38:44 -0700509 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700510
Tejun Heo155fead2012-04-01 14:38:44 -0700511 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700512 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700513 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700514 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700515 stats->start_group_wait_time = sched_clock();
516 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700517}
518
519/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700520static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700521{
522 unsigned long long now;
523
Tejun Heo155fead2012-04-01 14:38:44 -0700524 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700525 return;
526
527 now = sched_clock();
528 if (time_after64(now, stats->start_empty_time))
529 blkg_stat_add(&stats->empty_time,
530 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700531 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700532}
533
Tejun Heo155fead2012-04-01 14:38:44 -0700534static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700535{
Tejun Heo155fead2012-04-01 14:38:44 -0700536 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700537}
538
Tejun Heo155fead2012-04-01 14:38:44 -0700539static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700540{
Tejun Heo155fead2012-04-01 14:38:44 -0700541 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700542
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800543 if (blkg_rwstat_total(&stats->queued))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700544 return;
545
546 /*
547 * group is already marked empty. This can happen if cfqq got new
548 * request in parent group and moved to this group while being added
549 * to service tree. Just ignore the event and move on.
550 */
Tejun Heo155fead2012-04-01 14:38:44 -0700551 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700552 return;
553
554 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700555 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700556}
557
Tejun Heo155fead2012-04-01 14:38:44 -0700558static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700559{
Tejun Heo155fead2012-04-01 14:38:44 -0700560 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700561
Tejun Heo155fead2012-04-01 14:38:44 -0700562 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700563 unsigned long long now = sched_clock();
564
565 if (time_after64(now, stats->start_idle_time))
566 blkg_stat_add(&stats->idle_time,
567 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700568 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700569 }
570}
571
Tejun Heo155fead2012-04-01 14:38:44 -0700572static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700573{
Tejun Heo155fead2012-04-01 14:38:44 -0700574 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700575
Tejun Heo155fead2012-04-01 14:38:44 -0700576 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700577
578 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700579 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700580}
581
Tejun Heo155fead2012-04-01 14:38:44 -0700582static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700583{
Tejun Heo155fead2012-04-01 14:38:44 -0700584 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700585
586 blkg_stat_add(&stats->avg_queue_size_sum,
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800587 blkg_rwstat_total(&stats->queued));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700588 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700589 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700590}
591
592#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
593
Tejun Heof48ec1d2012-04-13 13:11:25 -0700594static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
595static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
596static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
597static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
598static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
599static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
600static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700601
602#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
603
604#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700605
Jens Axboe4ceab712015-06-19 10:13:01 -0600606static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
607{
608 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
609}
610
611static struct cfq_group_data
612*cpd_to_cfqgd(struct blkcg_policy_data *cpd)
613{
Tejun Heo81437642015-08-18 14:55:15 -0700614 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
Jens Axboe4ceab712015-06-19 10:13:01 -0600615}
616
617static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
618{
619 return pd_to_blkg(&cfqg->pd);
620}
621
Tejun Heoffea73f2012-06-04 10:02:29 +0200622static struct blkcg_policy blkcg_policy_cfq;
623
624static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
625{
626 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
627}
628
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200629static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
630{
631 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
632}
633
Tejun Heod02f7aa2013-01-09 08:05:11 -0800634static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
Tejun Heo7918ffb2013-01-09 08:05:11 -0800635{
Tejun Heod02f7aa2013-01-09 08:05:11 -0800636 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800637
Tejun Heod02f7aa2013-01-09 08:05:11 -0800638 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800639}
640
Jan Kara3984aa52016-01-12 16:24:19 +0100641static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
642 struct cfq_group *ancestor)
643{
644 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
645 cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
646}
647
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100648static inline void cfqg_get(struct cfq_group *cfqg)
649{
650 return blkg_get(cfqg_to_blkg(cfqg));
651}
652
653static inline void cfqg_put(struct cfq_group *cfqg)
654{
655 return blkg_put(cfqg_to_blkg(cfqg));
656}
657
Tejun Heo54e7ed12012-04-16 13:57:23 -0700658#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
659 char __pbuf[128]; \
660 \
661 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400662 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
663 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
664 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700665 __pbuf, ##args); \
666} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500667
Tejun Heo54e7ed12012-04-16 13:57:23 -0700668#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
669 char __pbuf[128]; \
670 \
671 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
672 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
673} 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,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600706 uint64_t start_time, uint64_t io_start_time,
707 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700708{
Tejun Heo155fead2012-04-01 14:38:44 -0700709 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700710 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700711
712 if (time_after64(now, io_start_time))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600713 blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700714 if (time_after64(io_start_time, start_time))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600715 blkg_rwstat_add(&stats->wait_time, op,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700716 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700717}
718
Tejun Heo689665a2013-01-09 08:05:13 -0800719/* @stats = 0 */
720static void cfqg_stats_reset(struct cfqg_stats *stats)
Tejun Heo155fead2012-04-01 14:38:44 -0700721{
Tejun Heo155fead2012-04-01 14:38:44 -0700722 /* queued stats shouldn't be cleared */
Tejun Heo155fead2012-04-01 14:38:44 -0700723 blkg_rwstat_reset(&stats->merged);
724 blkg_rwstat_reset(&stats->service_time);
725 blkg_rwstat_reset(&stats->wait_time);
726 blkg_stat_reset(&stats->time);
727#ifdef CONFIG_DEBUG_BLK_CGROUP
728 blkg_stat_reset(&stats->unaccounted_time);
729 blkg_stat_reset(&stats->avg_queue_size_sum);
730 blkg_stat_reset(&stats->avg_queue_size_samples);
731 blkg_stat_reset(&stats->dequeue);
732 blkg_stat_reset(&stats->group_wait_time);
733 blkg_stat_reset(&stats->idle_time);
734 blkg_stat_reset(&stats->empty_time);
735#endif
736}
737
Tejun Heo0b399202013-01-09 08:05:13 -0800738/* @to += @from */
Tejun Heoe6269c42015-08-18 14:55:21 -0700739static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
Tejun Heo0b399202013-01-09 08:05:13 -0800740{
741 /* queued stats shouldn't be cleared */
Tejun Heoe6269c42015-08-18 14:55:21 -0700742 blkg_rwstat_add_aux(&to->merged, &from->merged);
743 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
744 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
745 blkg_stat_add_aux(&from->time, &from->time);
Tejun Heo0b399202013-01-09 08:05:13 -0800746#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoe6269c42015-08-18 14:55:21 -0700747 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
748 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
749 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
750 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
751 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
752 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
753 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
Tejun Heo0b399202013-01-09 08:05:13 -0800754#endif
755}
756
757/*
Tejun Heoe6269c42015-08-18 14:55:21 -0700758 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
Tejun Heo0b399202013-01-09 08:05:13 -0800759 * recursive stats can still account for the amount used by this cfqg after
760 * it's gone.
761 */
762static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
763{
764 struct cfq_group *parent = cfqg_parent(cfqg);
765
766 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
767
768 if (unlikely(!parent))
769 return;
770
Tejun Heoe6269c42015-08-18 14:55:21 -0700771 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800772 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800773}
774
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100775#else /* CONFIG_CFQ_GROUP_IOSCHED */
776
Tejun Heod02f7aa2013-01-09 08:05:11 -0800777static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Jan Kara3984aa52016-01-12 16:24:19 +0100778static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
779 struct cfq_group *ancestor)
780{
781 return true;
782}
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100783static inline void cfqg_get(struct cfq_group *cfqg) { }
784static inline void cfqg_put(struct cfq_group *cfqg) { }
785
Jens Axboe7b679132008-05-30 12:23:07 +0200786#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400787 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
788 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
789 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
790 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200791#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100792
Tejun Heo155fead2012-04-01 14:38:44 -0700793static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600794 struct cfq_group *curr_cfqg, unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700795static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600796 uint64_t time, unsigned long unaccounted_time) { }
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600797static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
798 unsigned int op) { }
799static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
800 unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700801static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600802 uint64_t start_time, uint64_t io_start_time,
803 unsigned int op) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700804
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100805#endif /* CONFIG_CFQ_GROUP_IOSCHED */
806
Jens Axboe7b679132008-05-30 12:23:07 +0200807#define cfq_log(cfqd, fmt, args...) \
808 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
809
Vivek Goyal615f0252009-12-03 12:59:39 -0500810/* Traverses through cfq group service trees */
811#define for_each_cfqg_st(cfqg, i, j, st) \
812 for (i = 0; i <= IDLE_WORKLOAD; i++) \
813 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
814 : &cfqg->service_tree_idle; \
815 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
816 (i == IDLE_WORKLOAD && j == 0); \
817 j++, st = i < IDLE_WORKLOAD ? \
818 &cfqg->service_trees[i][j]: NULL) \
819
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200820static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
821 struct cfq_ttime *ttime, bool group_idle)
822{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600823 u64 slice;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200824 if (!sample_valid(ttime->ttime_samples))
825 return false;
826 if (group_idle)
827 slice = cfqd->cfq_group_idle;
828 else
829 slice = cfqd->cfq_slice_idle;
830 return ttime->ttime_mean > slice;
831}
Vivek Goyal615f0252009-12-03 12:59:39 -0500832
Vivek Goyal02b35082010-08-23 12:23:53 +0200833static inline bool iops_mode(struct cfq_data *cfqd)
834{
835 /*
836 * If we are not idling on queues and it is a NCQ drive, parallel
837 * execution of requests is on and measuring time is not possible
838 * in most of the cases until and unless we drive shallower queue
839 * depths and that becomes a performance bottleneck. In such cases
840 * switch to start providing fairness in terms of number of IOs.
841 */
842 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
843 return true;
844 else
845 return false;
846}
847
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400848static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100849{
850 if (cfq_class_idle(cfqq))
851 return IDLE_WORKLOAD;
852 if (cfq_class_rt(cfqq))
853 return RT_WORKLOAD;
854 return BE_WORKLOAD;
855}
856
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100857
858static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
859{
860 if (!cfq_cfqq_sync(cfqq))
861 return ASYNC_WORKLOAD;
862 if (!cfq_cfqq_idle_window(cfqq))
863 return SYNC_NOIDLE_WORKLOAD;
864 return SYNC_WORKLOAD;
865}
866
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400867static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500868 struct cfq_data *cfqd,
869 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100870{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400871 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500872 return cfqg->service_tree_idle.count;
873
Vivek Goyal34b98d02012-10-03 16:56:58 -0400874 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
875 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
876 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100877}
878
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500879static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
880 struct cfq_group *cfqg)
881{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400882 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
883 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500884}
885
Jens Axboe165125e2007-07-24 09:28:11 +0200886static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800887static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heo2da8de02015-08-18 14:55:02 -0700888 struct cfq_io_cq *cic, struct bio *bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200889
Tejun Heoc5869802011-12-14 00:33:41 +0100890static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
891{
892 /* cic->icq is the first member, %NULL will convert to %NULL */
893 return container_of(icq, struct cfq_io_cq, icq);
894}
895
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100896static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
897 struct io_context *ioc)
898{
899 if (ioc)
900 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
901 return NULL;
902}
903
Tejun Heoc5869802011-12-14 00:33:41 +0100904static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200905{
Jens Axboea6151c32009-10-07 20:02:57 +0200906 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200907}
908
Tejun Heoc5869802011-12-14 00:33:41 +0100909static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
910 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200911{
Jens Axboea6151c32009-10-07 20:02:57 +0200912 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200913}
914
Tejun Heoc5869802011-12-14 00:33:41 +0100915static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400916{
Tejun Heoc5869802011-12-14 00:33:41 +0100917 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400918}
919
Vasily Tarasov91fac312007-04-25 12:29:51 +0200920/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700921 * scheduler run of queue, if there are requests pending and no one in the
922 * driver that will restart queueing
923 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200924static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700925{
Jens Axboe7b679132008-05-30 12:23:07 +0200926 if (cfqd->busy_queues) {
927 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600928 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200929 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100933 * Scale schedule slice based on io priority. Use the sync time slice only
934 * if a queue is marked sync and has sync io queued. A sync queue with async
935 * io only, should not get full sync slice length.
936 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600937static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200938 unsigned short prio)
939{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600940 u64 base_slice = cfqd->cfq_slice[sync];
941 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
Jens Axboed9e76202007-04-20 14:27:50 +0200942
943 WARN_ON(prio >= IOPRIO_BE_NR);
944
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600945 return base_slice + (slice * (4 - prio));
Jens Axboed9e76202007-04-20 14:27:50 +0200946}
947
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600948static inline u64
Jens Axboe44f7c162007-01-19 11:51:58 +1100949cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
950{
Jens Axboed9e76202007-04-20 14:27:50 +0200951 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100952}
953
Tejun Heo1d3650f2013-01-09 08:05:11 -0800954/**
955 * cfqg_scale_charge - scale disk time charge according to cfqg weight
956 * @charge: disk time being charged
957 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
958 *
959 * Scale @charge according to @vfraction, which is in range (0, 1]. The
960 * scaling is inversely proportional.
961 *
962 * scaled = charge / vfraction
963 *
964 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
965 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600966static inline u64 cfqg_scale_charge(u64 charge,
Tejun Heo1d3650f2013-01-09 08:05:11 -0800967 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500968{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800969 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500970
Tejun Heo1d3650f2013-01-09 08:05:11 -0800971 /* charge / vfraction */
972 c <<= CFQ_SERVICE_SHIFT;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600973 return div_u64(c, vfraction);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500974}
975
976static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
977{
978 s64 delta = (s64)(vdisktime - min_vdisktime);
979 if (delta > 0)
980 min_vdisktime = vdisktime;
981
982 return min_vdisktime;
983}
984
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500985static void update_min_vdisktime(struct cfq_rb_root *st)
986{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500987 struct cfq_group *cfqg;
988
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500989 if (st->left) {
990 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100991 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
992 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500993 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500994}
995
Corrado Zoccolo5db5d642009-10-26 22:44:04 +0100996/*
997 * get averaged number of queues of RT/BE priority.
998 * average is updated, with a formula that gives more weight to higher numbers,
999 * to quickly follows sudden increases and decrease slowly
1000 */
1001
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001002static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1003 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001004{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001005 unsigned min_q, max_q;
1006 unsigned mult = cfq_hist_divisor - 1;
1007 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001008 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001009
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001010 min_q = min(cfqg->busy_queues_avg[rt], busy);
1011 max_q = max(cfqg->busy_queues_avg[rt], busy);
1012 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001013 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001014 return cfqg->busy_queues_avg[rt];
1015}
1016
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001017static inline u64
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001018cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1019{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001020 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001021}
1022
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001023static inline u64
Vivek Goyalba5bd522011-01-19 08:25:02 -07001024cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001025{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001026 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001027 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001028 /*
1029 * interested queues (we consider only the ones with the same
1030 * priority class in the cfq group)
1031 */
1032 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1033 cfq_class_rt(cfqq));
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001034 u64 sync_slice = cfqd->cfq_slice[1];
1035 u64 expect_latency = sync_slice * iq;
1036 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001037
1038 if (expect_latency > group_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001039 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1040 u64 low_slice;
1041
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001042 /* scale low_slice according to IO priority
1043 * and sync vs async */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001044 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1045 low_slice = min(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001046 /* the adapted slice value is scaled to fit all iqs
1047 * into the target latency */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001048 slice = div64_u64(slice*group_slice, expect_latency);
1049 slice = max(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001050 }
1051 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001052 return slice;
1053}
1054
1055static inline void
1056cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1057{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001058 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1059 u64 now = ktime_get_ns();
Shaohua Lic553f8e2011-01-14 08:41:03 +01001060
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001061 cfqq->slice_start = now;
1062 cfqq->slice_end = now + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001063 cfqq->allocated_slice = slice;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001064 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
Jens Axboe44f7c162007-01-19 11:51:58 +11001065}
1066
1067/*
1068 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1069 * isn't valid until the first request from the dispatch is activated
1070 * and the slice time set.
1071 */
Jens Axboea6151c32009-10-07 20:02:57 +02001072static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001073{
1074 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001075 return false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001076 if (ktime_get_ns() < cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01001077 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001078
Shaohua Lic1e44752010-11-08 15:01:02 +01001079 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001080}
1081
1082/*
Jens Axboe5e705372006-07-13 12:39:25 +02001083 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001085 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
Jens Axboe5e705372006-07-13 12:39:25 +02001087static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001088cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001090 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001092#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1093#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1094 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Jens Axboe5e705372006-07-13 12:39:25 +02001096 if (rq1 == NULL || rq1 == rq2)
1097 return rq2;
1098 if (rq2 == NULL)
1099 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001100
Namhyung Kim229836b2011-05-24 10:23:21 +02001101 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1102 return rq_is_sync(rq1) ? rq1 : rq2;
1103
Christoph Hellwig65299a32011-08-23 14:50:29 +02001104 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1105 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001106
Tejun Heo83096eb2009-05-07 22:24:39 +09001107 s1 = blk_rq_pos(rq1);
1108 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * by definition, 1KiB is 2 sectors
1112 */
1113 back_max = cfqd->cfq_back_max * 2;
1114
1115 /*
1116 * Strict one way elevator _except_ in the case where we allow
1117 * short backward seeks which are biased as twice the cost of a
1118 * similar forward seek.
1119 */
1120 if (s1 >= last)
1121 d1 = s1 - last;
1122 else if (s1 + back_max >= last)
1123 d1 = (last - s1) * cfqd->cfq_back_penalty;
1124 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001125 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if (s2 >= last)
1128 d2 = s2 - last;
1129 else if (s2 + back_max >= last)
1130 d2 = (last - s2) * cfqd->cfq_back_penalty;
1131 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001132 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Andreas Mohre8a99052006-03-28 08:59:49 +02001136 /*
1137 * By doing switch() on the bit mask "wrap" we avoid having to
1138 * check two variables for all permutations: --> faster!
1139 */
1140 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001141 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001142 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001143 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001144 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001145 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001146 else {
1147 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001148 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001149 else
Jens Axboe5e705372006-07-13 12:39:25 +02001150 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001151 }
1152
1153 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001154 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001155 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001156 return rq2;
1157 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001158 default:
1159 /*
1160 * Since both rqs are wrapped,
1161 * start with the one that's further behind head
1162 * (--> only *one* back seek required),
1163 * since back seek takes more time than forward.
1164 */
1165 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001166 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 else
Jens Axboe5e705372006-07-13 12:39:25 +02001168 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170}
1171
Jens Axboe498d3aa22007-04-26 12:54:48 +02001172/*
1173 * The below is leftmost cache rbtree addon
1174 */
Jens Axboe08717142008-01-28 11:38:15 +01001175static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001176{
Vivek Goyal615f0252009-12-03 12:59:39 -05001177 /* Service tree is empty */
1178 if (!root->count)
1179 return NULL;
1180
Jens Axboecc09e292007-04-26 12:53:50 +02001181 if (!root->left)
1182 root->left = rb_first(&root->rb);
1183
Jens Axboe08717142008-01-28 11:38:15 +01001184 if (root->left)
1185 return rb_entry(root->left, struct cfq_queue, rb_node);
1186
1187 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001188}
1189
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001190static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1191{
1192 if (!root->left)
1193 root->left = rb_first(&root->rb);
1194
1195 if (root->left)
1196 return rb_entry_cfqg(root->left);
1197
1198 return NULL;
1199}
1200
Jens Axboea36e71f2009-04-15 12:15:11 +02001201static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1202{
1203 rb_erase(n, root);
1204 RB_CLEAR_NODE(n);
1205}
1206
Jens Axboecc09e292007-04-26 12:53:50 +02001207static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1208{
1209 if (root->left == n)
1210 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001211 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001212 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/*
1216 * would be nice to take fifo expire time into account as well
1217 */
Jens Axboe5e705372006-07-13 12:39:25 +02001218static struct request *
1219cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1220 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Jens Axboe21183b02006-07-13 12:33:14 +02001222 struct rb_node *rbnext = rb_next(&last->rb_node);
1223 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001224 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jens Axboe21183b02006-07-13 12:33:14 +02001226 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001229 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001232 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001233 else {
1234 rbnext = rb_first(&cfqq->sort_list);
1235 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001236 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001239 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001242static u64 cfq_slice_offset(struct cfq_data *cfqd,
1243 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Jens Axboed9e76202007-04-20 14:27:50 +02001245 /*
1246 * just an approximation, should be ok.
1247 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001248 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001249 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001252static inline s64
1253cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1254{
1255 return cfqg->vdisktime - st->min_vdisktime;
1256}
1257
1258static void
1259__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1260{
1261 struct rb_node **node = &st->rb.rb_node;
1262 struct rb_node *parent = NULL;
1263 struct cfq_group *__cfqg;
1264 s64 key = cfqg_key(st, cfqg);
1265 int left = 1;
1266
1267 while (*node != NULL) {
1268 parent = *node;
1269 __cfqg = rb_entry_cfqg(parent);
1270
1271 if (key < cfqg_key(st, __cfqg))
1272 node = &parent->rb_left;
1273 else {
1274 node = &parent->rb_right;
1275 left = 0;
1276 }
1277 }
1278
1279 if (left)
1280 st->left = &cfqg->rb_node;
1281
1282 rb_link_node(&cfqg->rb_node, parent, node);
1283 rb_insert_color(&cfqg->rb_node, &st->rb);
1284}
1285
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001286/*
1287 * This has to be called only on activation of cfqg
1288 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001289static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001290cfq_update_group_weight(struct cfq_group *cfqg)
1291{
Tejun Heo3381cb82012-04-01 14:38:44 -07001292 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001293 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001294 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001295 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001296}
1297
1298static void
1299cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1300{
1301 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001302
1303 if (cfqg->new_leaf_weight) {
1304 cfqg->leaf_weight = cfqg->new_leaf_weight;
1305 cfqg->new_leaf_weight = 0;
1306 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001307}
1308
1309static void
1310cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1311{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001312 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001313 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001314 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001315 bool propagate;
1316
1317 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001318 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1319
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001320 /*
1321 * Update leaf_weight. We cannot update weight at this point
1322 * because cfqg might already have been activated and is
1323 * contributing its current weight to the parent's child_weight.
1324 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001325 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001326 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001327
1328 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001329 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1330 * entitled to. vfraction is calculated by walking the tree
1331 * towards the root calculating the fraction it has at each level.
1332 * The compounded ratio is how much vfraction @cfqg owns.
1333 *
1334 * Start with the proportion tasks in this cfqg has against active
1335 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001336 */
1337 propagate = !pos->nr_active++;
1338 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001339 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001340
Tejun Heo1d3650f2013-01-09 08:05:11 -08001341 /*
1342 * Compound ->weight walking up the tree. Both activation and
1343 * vfraction calculation are done in the same loop. Propagation
1344 * stops once an already activated node is met. vfraction
1345 * calculation should always continue to the root.
1346 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001347 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001348 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001349 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001350 propagate = !parent->nr_active++;
1351 parent->children_weight += pos->weight;
1352 }
1353 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001354 pos = parent;
1355 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001356
1357 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001358}
1359
Hou Tao5be6b752017-03-01 09:02:33 +08001360static inline u64 cfq_get_cfqg_vdisktime_delay(struct cfq_data *cfqd)
1361{
1362 if (!iops_mode(cfqd))
1363 return CFQ_SLICE_MODE_GROUP_DELAY;
1364 else
1365 return CFQ_IOPS_MODE_GROUP_DELAY;
1366}
1367
Justin TerAvest8184f932011-03-17 16:12:36 +01001368static void
1369cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001370{
1371 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1372 struct cfq_group *__cfqg;
1373 struct rb_node *n;
1374
1375 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001376 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001377 return;
1378
1379 /*
1380 * Currently put the group at the end. Later implement something
1381 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001382 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001383 */
1384 n = rb_last(&st->rb);
1385 if (n) {
1386 __cfqg = rb_entry_cfqg(n);
Hou Tao5be6b752017-03-01 09:02:33 +08001387 cfqg->vdisktime = __cfqg->vdisktime +
1388 cfq_get_cfqg_vdisktime_delay(cfqd);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001389 } else
1390 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001391 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001392}
1393
1394static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001395cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1396{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001397 struct cfq_group *pos = cfqg;
1398 bool propagate;
1399
1400 /*
1401 * Undo activation from cfq_group_service_tree_add(). Deactivate
1402 * @cfqg and propagate deactivation upwards.
1403 */
1404 propagate = !--pos->nr_active;
1405 pos->children_weight -= pos->leaf_weight;
1406
1407 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001408 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001409
1410 /* @pos has 0 nr_active at this point */
1411 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001412 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001413
1414 if (!parent)
1415 break;
1416
1417 propagate = !--parent->nr_active;
1418 parent->children_weight -= pos->weight;
1419 pos = parent;
1420 }
1421
1422 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001423 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1424 cfq_rb_erase(&cfqg->rb_node, st);
1425}
1426
1427static void
1428cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001429{
1430 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1431
1432 BUG_ON(cfqg->nr_cfqq < 1);
1433 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001434
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001435 /* If there are other cfq queues under this group, don't delete it */
1436 if (cfqg->nr_cfqq)
1437 return;
1438
Vivek Goyal2868ef72009-12-03 12:59:48 -05001439 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001440 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001441 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001442 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001443}
1444
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001445static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1446 u64 *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001447{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001448 u64 slice_used;
1449 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001450
1451 /*
1452 * Queue got expired before even a single request completed or
1453 * got expired immediately after first request completion.
1454 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001455 if (!cfqq->slice_start || cfqq->slice_start == now) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001456 /*
1457 * Also charge the seek time incurred to the group, otherwise
1458 * if there are mutiple queues in the group, each can dispatch
1459 * a single request on seeky media and cause lots of seek time
1460 * and group will never know it.
1461 */
Jan Kara0b31c102016-06-28 09:04:02 +02001462 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1463 jiffies_to_nsecs(1));
Vivek Goyaldae739e2009-12-03 12:59:45 -05001464 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001465 slice_used = now - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001466 if (slice_used > cfqq->allocated_slice) {
1467 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001468 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001469 }
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001470 if (cfqq->slice_start > cfqq->dispatch_start)
Justin TerAvest167400d2011-03-12 16:54:00 +01001471 *unaccounted_time += cfqq->slice_start -
1472 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001473 }
1474
Vivek Goyaldae739e2009-12-03 12:59:45 -05001475 return slice_used;
1476}
1477
1478static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001479 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001480{
1481 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001482 u64 used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001483 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1484 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001485 unsigned int vfr;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001486 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001487
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001488 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001489 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001490
Vivek Goyal02b35082010-08-23 12:23:53 +02001491 if (iops_mode(cfqd))
1492 charge = cfqq->slice_dispatch;
1493 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1494 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001495
Tejun Heo1d3650f2013-01-09 08:05:11 -08001496 /*
1497 * Can't update vdisktime while on service tree and cfqg->vfraction
1498 * is valid only while on it. Cache vfr, leave the service tree,
1499 * update vdisktime and go back on. The re-addition to the tree
1500 * will also update the weights as necessary.
1501 */
1502 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001503 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001504 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001505 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001506
1507 /* This group is being expired. Save the context */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001508 if (cfqd->workload_expires > now) {
1509 cfqg->saved_wl_slice = cfqd->workload_expires - now;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001510 cfqg->saved_wl_type = cfqd->serving_wl_type;
1511 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001512 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001513 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001514
1515 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1516 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001517 cfq_log_cfqq(cfqq->cfqd, cfqq,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001518 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
Joe Perchesfd16d262011-06-13 10:42:49 +02001519 used_sl, cfqq->slice_dispatch, charge,
1520 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001521 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1522 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001523}
1524
Tejun Heof51b8022012-03-05 13:15:05 -08001525/**
1526 * cfq_init_cfqg_base - initialize base part of a cfq_group
1527 * @cfqg: cfq_group to initialize
1528 *
1529 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1530 * is enabled or not.
1531 */
1532static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1533{
1534 struct cfq_rb_root *st;
1535 int i, j;
1536
1537 for_each_cfqg_st(cfqg, i, j, st)
1538 *st = CFQ_RB_ROOT;
1539 RB_CLEAR_NODE(&cfqg->rb_node);
1540
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001541 cfqg->ttime.last_end_request = ktime_get_ns();
Tejun Heof51b8022012-03-05 13:15:05 -08001542}
1543
Vivek Goyal25fb5162009-12-03 12:59:46 -05001544#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo69d7fde2015-08-18 14:55:36 -07001545static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1546 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1547
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001548static void cfqg_stats_exit(struct cfqg_stats *stats)
Peter Zijlstra90d38392013-11-12 19:42:14 -08001549{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001550 blkg_rwstat_exit(&stats->merged);
1551 blkg_rwstat_exit(&stats->service_time);
1552 blkg_rwstat_exit(&stats->wait_time);
1553 blkg_rwstat_exit(&stats->queued);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001554 blkg_stat_exit(&stats->time);
1555#ifdef CONFIG_DEBUG_BLK_CGROUP
1556 blkg_stat_exit(&stats->unaccounted_time);
1557 blkg_stat_exit(&stats->avg_queue_size_sum);
1558 blkg_stat_exit(&stats->avg_queue_size_samples);
1559 blkg_stat_exit(&stats->dequeue);
1560 blkg_stat_exit(&stats->group_wait_time);
1561 blkg_stat_exit(&stats->idle_time);
1562 blkg_stat_exit(&stats->empty_time);
1563#endif
1564}
1565
1566static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1567{
Tejun Heo77ea7332015-08-18 14:55:24 -07001568 if (blkg_rwstat_init(&stats->merged, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001569 blkg_rwstat_init(&stats->service_time, gfp) ||
1570 blkg_rwstat_init(&stats->wait_time, gfp) ||
1571 blkg_rwstat_init(&stats->queued, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001572 blkg_stat_init(&stats->time, gfp))
1573 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001574
1575#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001576 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1577 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1578 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1579 blkg_stat_init(&stats->dequeue, gfp) ||
1580 blkg_stat_init(&stats->group_wait_time, gfp) ||
1581 blkg_stat_init(&stats->idle_time, gfp) ||
1582 blkg_stat_init(&stats->empty_time, gfp))
1583 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001584#endif
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001585 return 0;
1586err:
1587 cfqg_stats_exit(stats);
1588 return -ENOMEM;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001589}
1590
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001591static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1592{
1593 struct cfq_group_data *cgd;
1594
Tejun Heoebc4ff62016-11-10 11:16:37 -05001595 cgd = kzalloc(sizeof(*cgd), gfp);
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001596 if (!cgd)
1597 return NULL;
1598 return &cgd->cpd;
1599}
1600
Tejun Heo81437642015-08-18 14:55:15 -07001601static void cfq_cpd_init(struct blkcg_policy_data *cpd)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001602{
Tejun Heo81437642015-08-18 14:55:15 -07001603 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001604 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
Tejun Heo69d7fde2015-08-18 14:55:36 -07001605 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001606
Tejun Heo69d7fde2015-08-18 14:55:36 -07001607 if (cpd_to_blkcg(cpd) == &blkcg_root)
1608 weight *= 2;
1609
1610 cgd->weight = weight;
1611 cgd->leaf_weight = weight;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001612}
1613
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001614static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1615{
1616 kfree(cpd_to_cfqgd(cpd));
1617}
1618
Tejun Heo69d7fde2015-08-18 14:55:36 -07001619static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1620{
1621 struct blkcg *blkcg = cpd_to_blkcg(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001622 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
Tejun Heo69d7fde2015-08-18 14:55:36 -07001623 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1624
1625 if (blkcg == &blkcg_root)
1626 weight *= 2;
1627
1628 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1629 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1630}
1631
Tejun Heo001bea72015-08-18 14:55:11 -07001632static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1633{
Tejun Heob2ce2642015-08-18 14:55:13 -07001634 struct cfq_group *cfqg;
1635
1636 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1637 if (!cfqg)
1638 return NULL;
1639
1640 cfq_init_cfqg_base(cfqg);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001641 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1642 kfree(cfqg);
1643 return NULL;
1644 }
Tejun Heob2ce2642015-08-18 14:55:13 -07001645
1646 return &cfqg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -07001647}
1648
Tejun Heoa9520cd2015-08-18 14:55:14 -07001649static void cfq_pd_init(struct blkg_policy_data *pd)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001650{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001651 struct cfq_group *cfqg = pd_to_cfqg(pd);
1652 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001653
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001654 cfqg->weight = cgd->weight;
1655 cfqg->leaf_weight = cgd->leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001656}
1657
Tejun Heoa9520cd2015-08-18 14:55:14 -07001658static void cfq_pd_offline(struct blkg_policy_data *pd)
Tejun Heo0b399202013-01-09 08:05:13 -08001659{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001660 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo60a83702015-08-18 14:55:05 -07001661 int i;
1662
1663 for (i = 0; i < IOPRIO_BE_NR; i++) {
1664 if (cfqg->async_cfqq[0][i])
1665 cfq_put_queue(cfqg->async_cfqq[0][i]);
1666 if (cfqg->async_cfqq[1][i])
1667 cfq_put_queue(cfqg->async_cfqq[1][i]);
1668 }
1669
1670 if (cfqg->async_idle_cfqq)
1671 cfq_put_queue(cfqg->async_idle_cfqq);
1672
Tejun Heo0b399202013-01-09 08:05:13 -08001673 /*
1674 * @blkg is going offline and will be ignored by
1675 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1676 * that they don't get lost. If IOs complete after this point, the
1677 * stats for them will be lost. Oh well...
1678 */
Tejun Heo60a83702015-08-18 14:55:05 -07001679 cfqg_stats_xfer_dead(cfqg);
Tejun Heo0b399202013-01-09 08:05:13 -08001680}
1681
Tejun Heo001bea72015-08-18 14:55:11 -07001682static void cfq_pd_free(struct blkg_policy_data *pd)
1683{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001684 struct cfq_group *cfqg = pd_to_cfqg(pd);
1685
1686 cfqg_stats_exit(&cfqg->stats);
1687 return kfree(cfqg);
Tejun Heo001bea72015-08-18 14:55:11 -07001688}
1689
Tejun Heoa9520cd2015-08-18 14:55:14 -07001690static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo689665a2013-01-09 08:05:13 -08001691{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001692 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo689665a2013-01-09 08:05:13 -08001693
1694 cfqg_stats_reset(&cfqg->stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001695}
1696
Tejun Heoae118892015-08-18 14:55:20 -07001697static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1698 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001699{
Tejun Heoae118892015-08-18 14:55:20 -07001700 struct blkcg_gq *blkg;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001701
Tejun Heoae118892015-08-18 14:55:20 -07001702 blkg = blkg_lookup(blkcg, cfqd->queue);
1703 if (likely(blkg))
1704 return blkg_to_cfqg(blkg);
1705 return NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001706}
1707
1708static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1709{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001710 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001711 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001712 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001713}
1714
Tejun Heof95a04a2012-04-16 13:57:26 -07001715static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1716 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001717{
Tejun Heof95a04a2012-04-16 13:57:26 -07001718 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001719
1720 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001721 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001722 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001723}
1724
Tejun Heo2da8ca82013-12-05 12:28:04 -05001725static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001726{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001727 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1728 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1729 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001730 return 0;
1731}
1732
Tejun Heoe71357e2013-01-09 08:05:10 -08001733static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1734 struct blkg_policy_data *pd, int off)
1735{
1736 struct cfq_group *cfqg = pd_to_cfqg(pd);
1737
1738 if (!cfqg->dev_leaf_weight)
1739 return 0;
1740 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1741}
1742
Tejun Heo2da8ca82013-12-05 12:28:04 -05001743static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001744{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001745 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1746 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1747 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001748 return 0;
1749}
1750
Tejun Heo2da8ca82013-12-05 12:28:04 -05001751static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001752{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001753 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001754 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1755 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001756
Jens Axboe9470e4a2015-06-19 10:19:36 -06001757 if (cgd)
1758 val = cgd->weight;
1759
1760 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001761 return 0;
1762}
1763
Tejun Heo2da8ca82013-12-05 12:28:04 -05001764static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001765{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001766 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001767 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1768 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001769
Jens Axboe9470e4a2015-06-19 10:19:36 -06001770 if (cgd)
1771 val = cgd->leaf_weight;
1772
1773 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001774 return 0;
1775}
1776
Tejun Heo451af502014-05-13 12:16:21 -04001777static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1778 char *buf, size_t nbytes, loff_t off,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001779 bool on_dfl, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001780{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001781 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1782 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo451af502014-05-13 12:16:21 -04001783 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001784 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001785 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001786 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001787 int ret;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001788 u64 v;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001789
Tejun Heo3c798392012-04-16 13:57:25 -07001790 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001791 if (ret)
1792 return ret;
1793
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001794 if (sscanf(ctx.body, "%llu", &v) == 1) {
1795 /* require "default" on dfl */
1796 ret = -ERANGE;
1797 if (!v && on_dfl)
1798 goto out_finish;
1799 } else if (!strcmp(strim(ctx.body), "default")) {
1800 v = 0;
1801 } else {
1802 ret = -EINVAL;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001803 goto out_finish;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001804 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001805
Tejun Heo3381cb82012-04-01 14:38:44 -07001806 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001807 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001808
Tejun Heo20386ce2015-08-18 14:55:28 -07001809 ret = -ERANGE;
Tejun Heo69d7fde2015-08-18 14:55:36 -07001810 if (!v || (v >= min && v <= max)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001811 if (!is_leaf_weight) {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001812 cfqg->dev_weight = v;
1813 cfqg->new_weight = v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001814 } else {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001815 cfqg->dev_leaf_weight = v;
1816 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001817 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001818 ret = 0;
1819 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001820out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001821 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001822 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001823}
1824
Tejun Heo451af502014-05-13 12:16:21 -04001825static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1826 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001827{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001828 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001829}
1830
Tejun Heo451af502014-05-13 12:16:21 -04001831static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1832 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001833{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001834 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001835}
1836
Tejun Heodd165eb2015-08-18 14:55:33 -07001837static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
Tejun Heo69d7fde2015-08-18 14:55:36 -07001838 bool on_dfl, bool reset_dev, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001839{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001840 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1841 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo182446d2013-08-08 20:11:24 -04001842 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001843 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001844 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001845 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001846
Tejun Heo69d7fde2015-08-18 14:55:36 -07001847 if (val < min || val > max)
1848 return -ERANGE;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001849
1850 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001851 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001852 if (!cfqgd) {
1853 ret = -EINVAL;
1854 goto out;
1855 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001856
1857 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001858 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001859 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001860 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001861
Sasha Levinb67bfe02013-02-27 17:06:00 -08001862 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001863 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001864
Tejun Heoe71357e2013-01-09 08:05:10 -08001865 if (!cfqg)
1866 continue;
1867
1868 if (!is_leaf_weight) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001869 if (reset_dev)
1870 cfqg->dev_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001871 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001872 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001873 } else {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001874 if (reset_dev)
1875 cfqg->dev_leaf_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001876 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001877 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001878 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001879 }
1880
Jens Axboeae994ea2015-06-20 10:26:50 -06001881out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001882 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001883 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001884}
1885
Tejun Heo182446d2013-08-08 20:11:24 -04001886static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1887 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001888{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001889 return __cfq_set_weight(css, val, false, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001890}
1891
Tejun Heo182446d2013-08-08 20:11:24 -04001892static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1893 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001894{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001895 return __cfq_set_weight(css, val, false, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001896}
1897
Tejun Heo2da8ca82013-12-05 12:28:04 -05001898static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001899{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001900 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1901 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001902 return 0;
1903}
1904
Tejun Heo2da8ca82013-12-05 12:28:04 -05001905static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001906{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001907 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1908 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001909 return 0;
1910}
1911
Tejun Heo43114012013-01-09 08:05:13 -08001912static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1913 struct blkg_policy_data *pd, int off)
1914{
Tejun Heof12c74c2015-08-18 14:55:23 -07001915 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1916 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001917 return __blkg_prfill_u64(sf, pd, sum);
1918}
1919
1920static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1921 struct blkg_policy_data *pd, int off)
1922{
Tejun Heof12c74c2015-08-18 14:55:23 -07001923 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1924 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001925 return __blkg_prfill_rwstat(sf, pd, &sum);
1926}
1927
Tejun Heo2da8ca82013-12-05 12:28:04 -05001928static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001929{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001930 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1931 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1932 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001933 return 0;
1934}
1935
Tejun Heo2da8ca82013-12-05 12:28:04 -05001936static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001937{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001938 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1939 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1940 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001941 return 0;
1942}
1943
Tejun Heo702747c2015-08-18 14:55:25 -07001944static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1945 int off)
1946{
1947 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1948
1949 return __blkg_prfill_u64(sf, pd, sum >> 9);
1950}
1951
1952static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1953{
1954 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1955 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1956 return 0;
1957}
1958
1959static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
1960 struct blkg_policy_data *pd, int off)
1961{
1962 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
1963 offsetof(struct blkcg_gq, stat_bytes));
1964 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
1965 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
1966
1967 return __blkg_prfill_u64(sf, pd, sum >> 9);
1968}
1969
1970static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1971{
1972 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1973 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
1974 false);
1975 return 0;
1976}
1977
Tejun Heo60c2bc22012-04-01 14:38:43 -07001978#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001979static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1980 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001981{
Tejun Heof95a04a2012-04-16 13:57:26 -07001982 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001983 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001984 u64 v = 0;
1985
1986 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001987 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06001988 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001989 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001990 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001991 return 0;
1992}
1993
1994/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001995static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001996{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001997 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1998 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1999 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07002000 return 0;
2001}
2002#endif /* CONFIG_DEBUG_BLK_CGROUP */
2003
Tejun Heo880f50e2015-08-18 14:55:30 -07002004static struct cftype cfq_blkcg_legacy_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08002005 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002006 {
2007 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08002008 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002009 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002010 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002011 },
2012 {
2013 .name = "weight",
2014 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002015 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002016 .write_u64 = cfq_set_leaf_weight,
2017 },
2018
2019 /* no such mapping necessary for !roots */
2020 {
2021 .name = "weight_device",
2022 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002023 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002024 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002025 },
2026 {
2027 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08002028 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002029 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07002030 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002031 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08002032
2033 {
2034 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002035 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002036 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08002037 },
2038 {
2039 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002040 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08002041 .write_u64 = cfq_set_leaf_weight,
2042 },
2043
Tejun Heo43114012013-01-09 08:05:13 -08002044 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002045 {
2046 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002047 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002048 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002049 },
2050 {
2051 .name = "sectors",
Tejun Heo702747c2015-08-18 14:55:25 -07002052 .seq_show = cfqg_print_stat_sectors,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002053 },
2054 {
2055 .name = "io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07002056 .private = (unsigned long)&blkcg_policy_cfq,
2057 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002058 },
2059 {
2060 .name = "io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07002061 .private = (unsigned long)&blkcg_policy_cfq,
2062 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002063 },
2064 {
2065 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002066 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002067 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002068 },
2069 {
2070 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002071 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002072 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002073 },
2074 {
2075 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002076 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002077 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002078 },
2079 {
2080 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002081 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002082 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002083 },
Tejun Heo43114012013-01-09 08:05:13 -08002084
2085 /* the same statictics which cover the cfqg and its descendants */
2086 {
2087 .name = "time_recursive",
2088 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002089 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002090 },
2091 {
2092 .name = "sectors_recursive",
Tejun Heo702747c2015-08-18 14:55:25 -07002093 .seq_show = cfqg_print_stat_sectors_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002094 },
2095 {
2096 .name = "io_service_bytes_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002097 .private = (unsigned long)&blkcg_policy_cfq,
2098 .seq_show = blkg_print_stat_bytes_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002099 },
2100 {
2101 .name = "io_serviced_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002102 .private = (unsigned long)&blkcg_policy_cfq,
2103 .seq_show = blkg_print_stat_ios_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002104 },
2105 {
2106 .name = "io_service_time_recursive",
2107 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002108 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002109 },
2110 {
2111 .name = "io_wait_time_recursive",
2112 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002113 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002114 },
2115 {
2116 .name = "io_merged_recursive",
2117 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002118 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002119 },
2120 {
2121 .name = "io_queued_recursive",
2122 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002123 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002124 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002125#ifdef CONFIG_DEBUG_BLK_CGROUP
2126 {
2127 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002128 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002129 },
2130 {
2131 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002132 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002133 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002134 },
2135 {
2136 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002137 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002138 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002139 },
2140 {
2141 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002142 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002143 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002144 },
2145 {
2146 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002147 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002148 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002149 },
2150 {
2151 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002152 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002153 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002154 },
2155#endif /* CONFIG_DEBUG_BLK_CGROUP */
2156 { } /* terminate */
2157};
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002158
2159static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2160{
2161 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2162 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2163
2164 seq_printf(sf, "default %u\n", cgd->weight);
2165 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2166 &blkcg_policy_cfq, 0, false);
2167 return 0;
2168}
2169
2170static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2171 char *buf, size_t nbytes, loff_t off)
2172{
2173 char *endp;
2174 int ret;
2175 u64 v;
2176
2177 buf = strim(buf);
2178
2179 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2180 v = simple_strtoull(buf, &endp, 0);
2181 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07002182 ret = __cfq_set_weight(of_css(of), v, true, false, false);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002183 return ret ?: nbytes;
2184 }
2185
2186 /* "MAJ:MIN WEIGHT" */
2187 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2188}
2189
2190static struct cftype cfq_blkcg_files[] = {
2191 {
2192 .name = "weight",
2193 .flags = CFTYPE_NOT_ON_ROOT,
2194 .seq_show = cfq_print_weight_on_dfl,
2195 .write = cfq_set_weight_on_dfl,
2196 },
2197 { } /* terminate */
2198};
2199
Vivek Goyal25fb5162009-12-03 12:59:46 -05002200#else /* GROUP_IOSCHED */
Tejun Heoae118892015-08-18 14:55:20 -07002201static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2202 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002203{
Tejun Heof51b8022012-03-05 13:15:05 -08002204 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002205}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002206
Vivek Goyal25fb5162009-12-03 12:59:46 -05002207static inline void
2208cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2209 cfqq->cfqg = cfqg;
2210}
2211
2212#endif /* GROUP_IOSCHED */
2213
Jens Axboe498d3aa22007-04-26 12:54:48 +02002214/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002215 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002216 * requests waiting to be processed. It is sorted in the order that
2217 * we will service the queues.
2218 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002219static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002220 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002221{
Jens Axboe08717142008-01-28 11:38:15 +01002222 struct rb_node **p, *parent;
2223 struct cfq_queue *__cfqq;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002224 u64 rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002225 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02002226 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002227 int new_cfqq = 1;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002228 u64 now = ktime_get_ns();
Vivek Goyalae30c282009-12-03 12:59:55 -05002229
Vivek Goyal34b98d02012-10-03 16:56:58 -04002230 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002231 if (cfq_class_idle(cfqq)) {
2232 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002233 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01002234 if (parent && parent != &cfqq->rb_node) {
2235 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2236 rb_key += __cfqq->rb_key;
2237 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002238 rb_key += now;
Jens Axboe08717142008-01-28 11:38:15 +01002239 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002240 /*
2241 * Get our rb key offset. Subtract any residual slice
2242 * value carried from last service. A negative resid
2243 * count indicates slice overrun, and this should position
2244 * the next service time further away in the tree.
2245 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002246 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
Jens Axboeb9c89462009-10-06 20:53:44 +02002247 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002248 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002249 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002250 rb_key = -NSEC_PER_SEC;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002251 __cfqq = cfq_rb_first(st);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002252 rb_key += __cfqq ? __cfqq->rb_key : now;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002253 }
Jens Axboed9e76202007-04-20 14:27:50 +02002254
2255 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002256 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002257 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002258 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002259 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002260 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002261 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002262
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002263 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2264 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002265 }
Jens Axboed9e76202007-04-20 14:27:50 +02002266
Jens Axboe498d3aa22007-04-26 12:54:48 +02002267 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01002268 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002269 cfqq->service_tree = st;
2270 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002271 while (*p) {
2272 parent = *p;
2273 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2274
Jens Axboe0c534e02007-04-18 20:01:57 +02002275 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002276 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002277 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002278 if (rb_key < __cfqq->rb_key)
Vivek Goyal1f23f122012-10-03 16:57:00 -04002279 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002280 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002281 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02002282 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002283 }
Jens Axboed9e76202007-04-20 14:27:50 +02002284 }
2285
Jens Axboecc09e292007-04-26 12:53:50 +02002286 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04002287 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02002288
Jens Axboed9e76202007-04-20 14:27:50 +02002289 cfqq->rb_key = rb_key;
2290 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002291 rb_insert_color(&cfqq->rb_node, &st->rb);
2292 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002293 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002294 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002295 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Jens Axboea36e71f2009-04-15 12:15:11 +02002298static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002299cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2300 sector_t sector, struct rb_node **ret_parent,
2301 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002302{
Jens Axboea36e71f2009-04-15 12:15:11 +02002303 struct rb_node **p, *parent;
2304 struct cfq_queue *cfqq = NULL;
2305
2306 parent = NULL;
2307 p = &root->rb_node;
2308 while (*p) {
2309 struct rb_node **n;
2310
2311 parent = *p;
2312 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2313
2314 /*
2315 * Sort strictly based on sector. Smallest to the left,
2316 * largest to the right.
2317 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002318 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002319 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002320 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002321 n = &(*p)->rb_left;
2322 else
2323 break;
2324 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002325 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002326 }
2327
2328 *ret_parent = parent;
2329 if (rb_link)
2330 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002331 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002332}
2333
2334static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2335{
Jens Axboea36e71f2009-04-15 12:15:11 +02002336 struct rb_node **p, *parent;
2337 struct cfq_queue *__cfqq;
2338
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002339 if (cfqq->p_root) {
2340 rb_erase(&cfqq->p_node, cfqq->p_root);
2341 cfqq->p_root = NULL;
2342 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002343
2344 if (cfq_class_idle(cfqq))
2345 return;
2346 if (!cfqq->next_rq)
2347 return;
2348
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002349 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002350 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2351 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002352 if (!__cfqq) {
2353 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002354 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2355 } else
2356 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002357}
2358
Jens Axboe498d3aa22007-04-26 12:54:48 +02002359/*
2360 * Update cfqq's position in the service tree.
2361 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002362static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002363{
Jens Axboe6d048f52007-04-25 12:44:27 +02002364 /*
2365 * Resorting requires the cfqq to be on the RR list already.
2366 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002367 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002368 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002369 cfq_prio_tree_add(cfqd, cfqq);
2370 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002371}
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373/*
2374 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002375 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 */
Jens Axboefebffd62008-01-28 13:19:43 +01002377static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Jens Axboe7b679132008-05-30 12:23:07 +02002379 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002380 BUG_ON(cfq_cfqq_on_rr(cfqq));
2381 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002383 if (cfq_cfqq_sync(cfqq))
2384 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Jens Axboeedd75ff2007-04-19 12:03:34 +02002386 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Jens Axboe498d3aa22007-04-26 12:54:48 +02002389/*
2390 * Called when the cfqq no longer has requests pending, remove it from
2391 * the service tree.
2392 */
Jens Axboefebffd62008-01-28 13:19:43 +01002393static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Jens Axboe7b679132008-05-30 12:23:07 +02002395 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002396 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2397 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002399 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2400 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2401 cfqq->service_tree = NULL;
2402 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002403 if (cfqq->p_root) {
2404 rb_erase(&cfqq->p_node, cfqq->p_root);
2405 cfqq->p_root = NULL;
2406 }
Jens Axboed9e76202007-04-20 14:27:50 +02002407
Justin TerAvest8184f932011-03-17 16:12:36 +01002408 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 BUG_ON(!cfqd->busy_queues);
2410 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002411 if (cfq_cfqq_sync(cfqq))
2412 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
2415/*
2416 * rb tree support functions
2417 */
Jens Axboefebffd62008-01-28 13:19:43 +01002418static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Jens Axboe5e705372006-07-13 12:39:25 +02002420 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002421 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jens Axboeb4878f22005-10-20 16:42:29 +02002423 BUG_ON(!cfqq->queued[sync]);
2424 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Jens Axboe5e705372006-07-13 12:39:25 +02002426 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Vivek Goyalf04a6422009-12-03 12:59:40 -05002428 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2429 /*
2430 * Queue will be deleted from service tree when we actually
2431 * expire it later. Right now just remove it from prio tree
2432 * as it is empty.
2433 */
2434 if (cfqq->p_root) {
2435 rb_erase(&cfqq->p_node, cfqq->p_root);
2436 cfqq->p_root = NULL;
2437 }
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Jens Axboe5e705372006-07-13 12:39:25 +02002441static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Jens Axboe5e705372006-07-13 12:39:25 +02002443 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002445 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Jens Axboe5380a102006-07-13 12:37:56 +02002447 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Jeff Moyer796d5112011-06-02 21:19:05 +02002449 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002450
2451 if (!cfq_cfqq_on_rr(cfqq))
2452 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002453
2454 /*
2455 * check if this request is a better next-serve candidate
2456 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002457 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002458 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002459
2460 /*
2461 * adjust priority tree position, if ->next_rq changes
2462 */
2463 if (prev != cfqq->next_rq)
2464 cfq_prio_tree_add(cfqd, cfqq);
2465
Jens Axboe5044eed2007-04-25 11:53:48 +02002466 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Jens Axboefebffd62008-01-28 13:19:43 +01002469static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Jens Axboe5380a102006-07-13 12:37:56 +02002471 elv_rb_del(&cfqq->sort_list, rq);
2472 cfqq->queued[rq_is_sync(rq)]--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002473 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002474 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002475 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002476 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Jens Axboe206dc692006-03-28 13:03:44 +02002479static struct request *
2480cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Jens Axboe206dc692006-03-28 13:03:44 +02002482 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002483 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002484 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jens Axboe4ac845a2008-01-24 08:44:49 +01002486 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002487 if (!cic)
2488 return NULL;
2489
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002490 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002491 if (cfqq)
2492 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 return NULL;
2495}
2496
Jens Axboe165125e2007-07-24 09:28:11 +02002497static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002498{
2499 struct cfq_data *cfqd = q->elevator->elevator_data;
2500
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002501 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002502 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002503 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002504
Tejun Heo5b936292009-05-07 22:24:38 +09002505 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002506}
2507
Jens Axboe165125e2007-07-24 09:28:11 +02002508static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Jens Axboe22e2c502005-06-27 10:55:12 +02002510 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002512 WARN_ON(!cfqd->rq_in_driver);
2513 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002514 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002515 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Jens Axboeb4878f22005-10-20 16:42:29 +02002518static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Jens Axboe5e705372006-07-13 12:39:25 +02002520 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002521
Jens Axboe5e705372006-07-13 12:39:25 +02002522 if (cfqq->next_rq == rq)
2523 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Jens Axboeb4878f22005-10-20 16:42:29 +02002525 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002526 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002527
Aaron Carroll45333d52008-08-26 15:52:36 +02002528 cfqq->cfqd->rq_queued--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002529 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002530 if (rq->cmd_flags & REQ_PRIO) {
2531 WARN_ON(!cfqq->prio_pending);
2532 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002536static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
Jens Axboe165125e2007-07-24 09:28:11 +02002537 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
2539 struct cfq_data *cfqd = q->elevator->elevator_data;
2540 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Jens Axboe206dc692006-03-28 13:03:44 +02002542 __rq = cfq_find_rq_fmerge(cfqd, bio);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002543 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002544 *req = __rq;
2545 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547
2548 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
2550
Jens Axboe165125e2007-07-24 09:28:11 +02002551static void cfq_merged_request(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002552 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Jens Axboe21183b02006-07-13 12:33:14 +02002554 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002555 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Jens Axboe5e705372006-07-13 12:39:25 +02002557 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
Divyesh Shah812d4022010-04-08 21:14:23 -07002561static void cfq_bio_merged(struct request_queue *q, struct request *req,
2562 struct bio *bio)
2563{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002564 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
Divyesh Shah812d4022010-04-08 21:14:23 -07002565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567static void
Jens Axboe165125e2007-07-24 09:28:11 +02002568cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct request *next)
2570{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002571 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002572 struct cfq_data *cfqd = q->elevator->elevator_data;
2573
Jens Axboe22e2c502005-06-27 10:55:12 +02002574 /*
2575 * reposition in fifo if next is older than rq
2576 */
2577 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002578 next->fifo_time < rq->fifo_time &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002579 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002580 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002581 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002582 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002583
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002584 if (cfqq->next_rq == next)
2585 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002586 cfq_remove_request(next);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002587 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002588
2589 cfqq = RQ_CFQQ(next);
2590 /*
2591 * all requests of this queue are merged to other queues, delete it
2592 * from the service tree. If it's the active_queue,
2593 * cfq_dispatch_requests() will choose to expire it or do idle
2594 */
2595 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2596 cfqq != cfqd->active_queue)
2597 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002598}
2599
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002600static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2601 struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +01002602{
2603 struct cfq_data *cfqd = q->elevator->elevator_data;
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002604 bool is_sync = op_is_sync(bio->bi_opf);
Tejun Heoc5869802011-12-14 00:33:41 +01002605 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002606 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002607
2608 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002609 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002610 */
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002611 if (is_sync && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002612 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002613
2614 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002615 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002616 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002617 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002618 cic = cfq_cic_lookup(cfqd, current->io_context);
2619 if (!cic)
2620 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002621
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002622 cfqq = cic_to_cfqq(cic, is_sync);
Jens Axboea6151c32009-10-07 20:02:57 +02002623 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002624}
2625
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002626static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2627 struct request *next)
2628{
2629 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2630}
2631
Divyesh Shah812df482010-04-08 21:15:35 -07002632static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2633{
Jan Kara91148322016-06-08 15:11:39 +02002634 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002635 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002636}
2637
Jens Axboefebffd62008-01-28 13:19:43 +01002638static void __cfq_set_active_queue(struct cfq_data *cfqd,
2639 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002640{
2641 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002642 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002643 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002644 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002645 cfqq->slice_start = 0;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002646 cfqq->dispatch_start = ktime_get_ns();
Justin TerAvest62a37f62011-03-23 08:25:44 +01002647 cfqq->allocated_slice = 0;
2648 cfqq->slice_end = 0;
2649 cfqq->slice_dispatch = 0;
2650 cfqq->nr_sectors = 0;
2651
2652 cfq_clear_cfqq_wait_request(cfqq);
2653 cfq_clear_cfqq_must_dispatch(cfqq);
2654 cfq_clear_cfqq_must_alloc_slice(cfqq);
2655 cfq_clear_cfqq_fifo_expire(cfqq);
2656 cfq_mark_cfqq_slice_new(cfqq);
2657
2658 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002659 }
2660
2661 cfqd->active_queue = cfqq;
2662}
2663
2664/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002665 * current cfqq expired its slice (or was too idle), select new one
2666 */
2667static void
2668__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002669 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002670{
Jens Axboe7b679132008-05-30 12:23:07 +02002671 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2672
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002673 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002674 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002675
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002676 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002677 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002678
2679 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002680 * If this cfqq is shared between multiple processes, check to
2681 * make sure that those processes are still issuing I/Os within
2682 * the mean seek distance. If not, it may be time to break the
2683 * queues apart again.
2684 */
2685 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2686 cfq_mark_cfqq_split_coop(cfqq);
2687
2688 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002689 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002690 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002691 if (timed_out) {
2692 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002693 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002694 else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002695 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
Jan Kara93fdf142016-06-28 09:04:00 +02002696 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
Jens Axboe7b679132008-05-30 12:23:07 +02002697 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002698
Vivek Goyale5ff0822010-04-26 19:25:11 +02002699 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002700
Vivek Goyalf04a6422009-12-03 12:59:40 -05002701 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2702 cfq_del_cfqq_rr(cfqd, cfqq);
2703
Jens Axboeedd75ff2007-04-19 12:03:34 +02002704 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002705
2706 if (cfqq == cfqd->active_queue)
2707 cfqd->active_queue = NULL;
2708
2709 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002710 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002711 cfqd->active_cic = NULL;
2712 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002713}
2714
Vivek Goyale5ff0822010-04-26 19:25:11 +02002715static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002716{
2717 struct cfq_queue *cfqq = cfqd->active_queue;
2718
2719 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002720 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002721}
2722
Jens Axboe498d3aa22007-04-26 12:54:48 +02002723/*
2724 * Get next queue for service. Unless we have a queue preemption,
2725 * we'll simply select the first cfqq in the service tree.
2726 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002727static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002728{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002729 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2730 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002731
Vivek Goyalf04a6422009-12-03 12:59:40 -05002732 if (!cfqd->rq_queued)
2733 return NULL;
2734
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002735 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002736 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002737 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002738 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002739 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002740 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002741}
2742
Vivek Goyalf04a6422009-12-03 12:59:40 -05002743static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2744{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002745 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002746 struct cfq_queue *cfqq;
2747 int i, j;
2748 struct cfq_rb_root *st;
2749
2750 if (!cfqd->rq_queued)
2751 return NULL;
2752
Vivek Goyal25fb5162009-12-03 12:59:46 -05002753 cfqg = cfq_get_next_cfqg(cfqd);
2754 if (!cfqg)
2755 return NULL;
2756
Markus Elfring1cf41752017-01-21 22:44:07 +01002757 for_each_cfqg_st(cfqg, i, j, st) {
2758 cfqq = cfq_rb_first(st);
2759 if (cfqq)
Vivek Goyalf04a6422009-12-03 12:59:40 -05002760 return cfqq;
Markus Elfring1cf41752017-01-21 22:44:07 +01002761 }
Vivek Goyalf04a6422009-12-03 12:59:40 -05002762 return NULL;
2763}
2764
Jens Axboe498d3aa22007-04-26 12:54:48 +02002765/*
2766 * Get and set a new active queue for service.
2767 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002768static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2769 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002770{
Jens Axboee00ef792009-11-04 08:54:55 +01002771 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002772 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002773
Jens Axboe22e2c502005-06-27 10:55:12 +02002774 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002775 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002776}
2777
Jens Axboed9e76202007-04-20 14:27:50 +02002778static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2779 struct request *rq)
2780{
Tejun Heo83096eb2009-05-07 22:24:39 +09002781 if (blk_rq_pos(rq) >= cfqd->last_position)
2782 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002783 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002784 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002785}
2786
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002787static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002788 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002789{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002790 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002791}
2792
Jens Axboea36e71f2009-04-15 12:15:11 +02002793static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2794 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002795{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002796 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002797 struct rb_node *parent, *node;
2798 struct cfq_queue *__cfqq;
2799 sector_t sector = cfqd->last_position;
2800
2801 if (RB_EMPTY_ROOT(root))
2802 return NULL;
2803
2804 /*
2805 * First, if we find a request starting at the end of the last
2806 * request, choose it.
2807 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002808 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002809 if (__cfqq)
2810 return __cfqq;
2811
2812 /*
2813 * If the exact sector wasn't found, the parent of the NULL leaf
2814 * will contain the closest sector.
2815 */
2816 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002817 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002818 return __cfqq;
2819
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002820 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002821 node = rb_next(&__cfqq->p_node);
2822 else
2823 node = rb_prev(&__cfqq->p_node);
2824 if (!node)
2825 return NULL;
2826
2827 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002828 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002829 return __cfqq;
2830
2831 return NULL;
2832}
2833
2834/*
2835 * cfqd - obvious
2836 * cur_cfqq - passed in so that we don't decide that the current queue is
2837 * closely cooperating with itself.
2838 *
2839 * So, basically we're assuming that that cur_cfqq has dispatched at least
2840 * one request, and that cfqd->last_position reflects a position on the disk
2841 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2842 * assumption.
2843 */
2844static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002845 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002846{
2847 struct cfq_queue *cfqq;
2848
Divyesh Shah39c01b22010-03-25 15:45:57 +01002849 if (cfq_class_idle(cur_cfqq))
2850 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002851 if (!cfq_cfqq_sync(cur_cfqq))
2852 return NULL;
2853 if (CFQQ_SEEKY(cur_cfqq))
2854 return NULL;
2855
Jens Axboea36e71f2009-04-15 12:15:11 +02002856 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002857 * Don't search priority tree if it's the only queue in the group.
2858 */
2859 if (cur_cfqq->cfqg->nr_cfqq == 1)
2860 return NULL;
2861
2862 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002863 * We should notice if some of the queues are cooperating, eg
2864 * working closely on the same area of the disk. In that case,
2865 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002866 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002867 cfqq = cfqq_close(cfqd, cur_cfqq);
2868 if (!cfqq)
2869 return NULL;
2870
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002871 /* If new queue belongs to different cfq_group, don't choose it */
2872 if (cur_cfqq->cfqg != cfqq->cfqg)
2873 return NULL;
2874
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002875 /*
2876 * It only makes sense to merge sync queues.
2877 */
2878 if (!cfq_cfqq_sync(cfqq))
2879 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002880 if (CFQQ_SEEKY(cfqq))
2881 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002882
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002883 /*
2884 * Do not merge queues of different priority classes
2885 */
2886 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2887 return NULL;
2888
Jens Axboea36e71f2009-04-15 12:15:11 +02002889 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002890}
2891
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002892/*
2893 * Determine whether we should enforce idle window for this queue.
2894 */
2895
2896static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2897{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002898 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002899 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002900
Vivek Goyal34b98d02012-10-03 16:56:58 -04002901 BUG_ON(!st);
2902 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002903
Vivek Goyalb6508c12010-08-23 12:23:33 +02002904 if (!cfqd->cfq_slice_idle)
2905 return false;
2906
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002907 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002908 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002909 return false;
2910
2911 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002912 if (cfq_cfqq_idle_window(cfqq) &&
2913 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002914 return true;
2915
2916 /*
2917 * Otherwise, we do only if they are the last ones
2918 * in their service tree.
2919 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002920 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2921 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002922 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002923 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002924 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002925}
2926
Jens Axboe6d048f52007-04-25 12:44:27 +02002927static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002928{
Jens Axboe17926692007-01-19 11:59:30 +11002929 struct cfq_queue *cfqq = cfqd->active_queue;
Jan Karae7954212016-01-12 16:24:15 +01002930 struct cfq_rb_root *st = cfqq->service_tree;
Tejun Heoc5869802011-12-14 00:33:41 +01002931 struct cfq_io_cq *cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002932 u64 sl, group_idle = 0;
2933 u64 now = ktime_get_ns();
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002934
Jens Axboea68bbddba2008-09-24 13:03:33 +02002935 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002936 * SSD device without seek penalty, disable idling. But only do so
2937 * for devices that support queuing, otherwise we still have a problem
2938 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002939 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002940 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002941 return;
2942
Jens Axboedd67d052006-06-21 09:36:18 +02002943 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002944 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002945
2946 /*
2947 * idle is disabled, either manually or by past process history
2948 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002949 if (!cfq_should_idle(cfqd, cfqq)) {
2950 /* no queue idling. Check for group idling */
2951 if (cfqd->cfq_group_idle)
2952 group_idle = cfqd->cfq_group_idle;
2953 else
2954 return;
2955 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002956
Jens Axboe22e2c502005-06-27 10:55:12 +02002957 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002958 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002959 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002960 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002961 return;
2962
2963 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002964 * task has exited, don't wait
2965 */
Jens Axboe206dc692006-03-28 13:03:44 +02002966 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002967 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002968 return;
2969
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002970 /*
2971 * If our average think time is larger than the remaining time
2972 * slice, then don't idle. This avoids overrunning the allotted
2973 * time slice.
2974 */
Shaohua Li383cd722011-07-12 14:24:35 +02002975 if (sample_valid(cic->ttime.ttime_samples) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002976 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2977 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
Shaohua Li383cd722011-07-12 14:24:35 +02002978 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002979 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002980 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002981
Jan Karae7954212016-01-12 16:24:15 +01002982 /*
2983 * There are other queues in the group or this is the only group and
2984 * it has too big thinktime, don't do group idle.
2985 */
2986 if (group_idle &&
2987 (cfqq->cfqg->nr_cfqq > 1 ||
2988 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002989 return;
2990
Jens Axboe3b181522005-06-27 10:56:24 +02002991 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002992
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002993 if (group_idle)
2994 sl = cfqd->cfq_group_idle;
2995 else
2996 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002997
Jan Kara91148322016-06-08 15:11:39 +02002998 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2999 HRTIMER_MODE_REL);
Tejun Heo155fead2012-04-01 14:38:44 -07003000 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003001 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003002 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Jens Axboe498d3aa22007-04-26 12:54:48 +02003005/*
3006 * Move request from internal lists to the request queue dispatch list.
3007 */
Jens Axboe165125e2007-07-24 09:28:11 +02003008static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Jens Axboe3ed9a292007-04-23 08:33:33 +02003010 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003011 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003012
Jens Axboe7b679132008-05-30 12:23:07 +02003013 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
3014
Jeff Moyer06d21882009-09-11 17:08:59 +02003015 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02003016 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02003017 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003018 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02003019 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02003020
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003021 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02003022 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025/*
3026 * return expired entry, or NULL to just start from scratch in rbtree
3027 */
Jens Axboefebffd62008-01-28 13:19:43 +01003028static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Jens Axboe30996f42009-10-05 11:03:39 +02003030 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Jens Axboe3b181522005-06-27 10:56:24 +02003032 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11003034
3035 cfq_mark_cfqq_fifo_expire(cfqq);
3036
Jens Axboe89850f72006-07-22 16:48:31 +02003037 if (list_empty(&cfqq->fifo))
3038 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Jens Axboe89850f72006-07-22 16:48:31 +02003040 rq = rq_entry_fifo(cfqq->fifo.next);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003041 if (ktime_get_ns() < rq->fifo_time)
Jens Axboe7b679132008-05-30 12:23:07 +02003042 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Jens Axboe6d048f52007-04-25 12:44:27 +02003044 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Jens Axboe22e2c502005-06-27 10:55:12 +02003047static inline int
3048cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3049{
3050 const int base_rq = cfqd->cfq_slice_async_rq;
3051
3052 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3053
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02003054 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003055}
3056
3057/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003058 * Must be called with the queue_lock held.
3059 */
3060static int cfqq_process_refs(struct cfq_queue *cfqq)
3061{
3062 int process_refs, io_refs;
3063
3064 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01003065 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003066 BUG_ON(process_refs < 0);
3067 return process_refs;
3068}
3069
3070static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3071{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003072 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003073 struct cfq_queue *__cfqq;
3074
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003075 /*
3076 * If there are no process references on the new_cfqq, then it is
3077 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3078 * chain may have dropped their last reference (not just their
3079 * last process reference).
3080 */
3081 if (!cfqq_process_refs(new_cfqq))
3082 return;
3083
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003084 /* Avoid a circular list and skip interim queue merges */
3085 while ((__cfqq = new_cfqq->new_cfqq)) {
3086 if (__cfqq == cfqq)
3087 return;
3088 new_cfqq = __cfqq;
3089 }
3090
3091 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003092 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003093 /*
3094 * If the process for the cfqq has gone away, there is no
3095 * sense in merging the queues.
3096 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003097 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003098 return;
3099
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003100 /*
3101 * Merge in the direction of the lesser amount of work.
3102 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003103 if (new_process_refs >= process_refs) {
3104 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003105 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003106 } else {
3107 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003108 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003109 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003110}
3111
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003112static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003113 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003114{
3115 struct cfq_queue *queue;
3116 int i;
3117 bool key_valid = false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003118 u64 lowest_key = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003119 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3120
Vivek Goyal65b32a52009-12-16 17:52:59 -05003121 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3122 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003123 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003124 if (queue &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003125 (!key_valid || queue->rb_key < lowest_key)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003126 lowest_key = queue->rb_key;
3127 cur_best = i;
3128 key_valid = true;
3129 }
3130 }
3131
3132 return cur_best;
3133}
3134
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003135static void
3136choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003137{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003138 u64 slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003139 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003140 struct cfq_rb_root *st;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003141 u64 group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003142 enum wl_class_t original_class = cfqd->serving_wl_class;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003143 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003144
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003145 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003146 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003147 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003148 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003149 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003150 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003151 cfqd->serving_wl_class = IDLE_WORKLOAD;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003152 cfqd->workload_expires = now + jiffies_to_nsecs(1);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003153 return;
3154 }
3155
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003156 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003157 goto new_workload;
3158
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003159 /*
3160 * For RT and BE, we have to choose also the type
3161 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3162 * expiration time
3163 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003164 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003165 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003166
3167 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003168 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003169 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003170 if (count && !(now > cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003171 return;
3172
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003173new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003174 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003175 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003176 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003177 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003178 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003179
3180 /*
3181 * the workload slice is computed as a fraction of target latency
3182 * proportional to the number of queues in that workload, over
3183 * all the queues in the same priority class
3184 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003185 group_slice = cfq_group_slice(cfqd, cfqg);
3186
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003187 slice = div_u64(group_slice * count,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003188 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3189 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003190 cfqg)));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003191
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003192 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003193 u64 tmp;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003194
3195 /*
3196 * Async queues are currently system wide. Just taking
3197 * proportion of queues with-in same group will lead to higher
3198 * async ratio system wide as generally root group is going
3199 * to have higher weight. A more accurate thing would be to
3200 * calculate system wide asnc/sync ratio.
3201 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003202 tmp = cfqd->cfq_target_latency *
3203 cfqg_busy_async_queues(cfqd, cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003204 tmp = div_u64(tmp, cfqd->busy_queues);
3205 slice = min_t(u64, slice, tmp);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003206
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003207 /* async workload slice is scaled down according to
3208 * the sync/async slice ratio. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003209 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003210 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003211 /* sync workload slice is at least 2 * cfq_slice_idle */
3212 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3213
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003214 slice = max_t(u64, slice, CFQ_MIN_TT);
3215 cfq_log(cfqd, "workload slice:%llu", slice);
3216 cfqd->workload_expires = now + slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003217}
3218
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003219static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3220{
3221 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003222 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003223
3224 if (RB_EMPTY_ROOT(&st->rb))
3225 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003226 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003227 update_min_vdisktime(st);
3228 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003229}
3230
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003231static void cfq_choose_cfqg(struct cfq_data *cfqd)
3232{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003233 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003234 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003235
3236 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003237
3238 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003239 if (cfqg->saved_wl_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003240 cfqd->workload_expires = now + cfqg->saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003241 cfqd->serving_wl_type = cfqg->saved_wl_type;
3242 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003243 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003244 cfqd->workload_expires = now - 1;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003245
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003246 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003247}
3248
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003249/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003250 * Select a queue for service. If we have a current active queue,
3251 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003252 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003253static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003254{
Jens Axboea36e71f2009-04-15 12:15:11 +02003255 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003256 u64 now = ktime_get_ns();
Jens Axboe22e2c502005-06-27 10:55:12 +02003257
3258 cfqq = cfqd->active_queue;
3259 if (!cfqq)
3260 goto new_queue;
3261
Vivek Goyalf04a6422009-12-03 12:59:40 -05003262 if (!cfqd->rq_queued)
3263 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003264
3265 /*
3266 * We were waiting for group to get backlogged. Expire the queue
3267 */
3268 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3269 goto expire;
3270
Jens Axboe22e2c502005-06-27 10:55:12 +02003271 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003272 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003273 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003274 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3275 /*
3276 * If slice had not expired at the completion of last request
3277 * we might not have turned on wait_busy flag. Don't expire
3278 * the queue yet. Allow the group to get backlogged.
3279 *
3280 * The very fact that we have used the slice, that means we
3281 * have been idling all along on this queue and it should be
3282 * ok to wait for this request to complete.
3283 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003284 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3285 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3286 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003287 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003288 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003289 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003290 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003291
3292 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003293 * The active queue has requests and isn't expired, allow it to
3294 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003295 */
Jens Axboedd67d052006-06-21 09:36:18 +02003296 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003297 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003298
3299 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003300 * If another queue has a request waiting within our mean seek
3301 * distance, let it run. The expire code will check for close
3302 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003303 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003304 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003305 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003306 if (new_cfqq) {
3307 if (!cfqq->new_cfqq)
3308 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003309 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003310 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003311
3312 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003313 * No requests pending. If the active queue still has requests in
3314 * flight or is idling for a new request, allow either of these
3315 * conditions to happen (or time out) before selecting a new queue.
3316 */
Jan Kara91148322016-06-08 15:11:39 +02003317 if (hrtimer_active(&cfqd->idle_slice_timer)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003318 cfqq = NULL;
3319 goto keep_queue;
3320 }
3321
Shaohua Li8e1ac662010-11-08 15:01:04 +01003322 /*
3323 * This is a deep seek queue, but the device is much faster than
3324 * the queue can deliver, don't idle
3325 **/
3326 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3327 (cfq_cfqq_slice_new(cfqq) ||
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003328 (cfqq->slice_end - now > now - cfqq->slice_start))) {
Shaohua Li8e1ac662010-11-08 15:01:04 +01003329 cfq_clear_cfqq_deep(cfqq);
3330 cfq_clear_cfqq_idle_window(cfqq);
3331 }
3332
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003333 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3334 cfqq = NULL;
3335 goto keep_queue;
3336 }
3337
3338 /*
3339 * If group idle is enabled and there are requests dispatched from
3340 * this group, wait for requests to complete.
3341 */
3342check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02003343 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3344 cfqq->cfqg->dispatched &&
3345 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003346 cfqq = NULL;
3347 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003348 }
3349
Jens Axboe3b181522005-06-27 10:56:24 +02003350expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003351 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003352new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003353 /*
3354 * Current queue expired. Check if we have to switch to a new
3355 * service tree
3356 */
3357 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003358 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003359
Jens Axboea36e71f2009-04-15 12:15:11 +02003360 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003361keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003362 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003363}
3364
Jens Axboefebffd62008-01-28 13:19:43 +01003365static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003366{
3367 int dispatched = 0;
3368
3369 while (cfqq->next_rq) {
3370 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3371 dispatched++;
3372 }
3373
3374 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003375
3376 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003377 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003378 return dispatched;
3379}
3380
Jens Axboe498d3aa22007-04-26 12:54:48 +02003381/*
3382 * Drain our current requests. Used for barriers and when switching
3383 * io schedulers on-the-fly.
3384 */
Jens Axboed9e76202007-04-20 14:27:50 +02003385static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003386{
Jens Axboe08717142008-01-28 11:38:15 +01003387 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003388 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003389
Divyesh Shah3440c492010-04-09 09:29:57 +02003390 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003391 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003392 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3393 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003394 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003395 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003396
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003397 BUG_ON(cfqd->busy_queues);
3398
Jeff Moyer69237152009-06-12 15:29:30 +02003399 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003400 return dispatched;
3401}
3402
Shaohua Liabc3c742010-03-01 09:20:54 +01003403static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3404 struct cfq_queue *cfqq)
3405{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003406 u64 now = ktime_get_ns();
3407
Shaohua Liabc3c742010-03-01 09:20:54 +01003408 /* the queue hasn't finished any request, can't estimate */
3409 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003410 return true;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003411 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01003412 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003413
Shaohua Lic1e44752010-11-08 15:01:02 +01003414 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003415}
3416
Jens Axboe0b182d62009-10-06 20:49:37 +02003417static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003418{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003419 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Glauber Costa3932a862016-09-22 20:59:59 -04003421 if (cfq_cfqq_must_dispatch(cfqq))
3422 return true;
3423
Jens Axboe2f5cb732009-04-07 08:51:19 +02003424 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003425 * Drain async requests before we start sync IO
3426 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003427 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003428 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003429
3430 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003431 * If this is an async queue and we have sync IO in flight, let it wait
3432 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003433 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003434 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003435
Shaohua Liabc3c742010-03-01 09:20:54 +01003436 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003437 if (cfq_class_idle(cfqq))
3438 max_dispatch = 1;
3439
3440 /*
3441 * Does this cfqq already have too much IO in flight?
3442 */
3443 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003444 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003445 /*
3446 * idle queue must always only have a single IO in flight
3447 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003448 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003449 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003450
Jens Axboe2f5cb732009-04-07 08:51:19 +02003451 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003452 * If there is only one sync queue
3453 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003454 * queue no dispatch limit. The reason is a sync queue can
3455 * preempt async queue, limiting the sync queue doesn't make
3456 * sense. This is useful for aiostress test.
3457 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003458 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3459 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003460
3461 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003462 * We have other queues, don't allow more IO from this one
3463 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003464 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3465 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003466 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003467
Jens Axboe2f5cb732009-04-07 08:51:19 +02003468 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003469 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003470 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003471 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003472 max_dispatch = -1;
3473 else
3474 /*
3475 * Normally we start throttling cfqq when cfq_quantum/2
3476 * requests have been dispatched. But we can drive
3477 * deeper queue depths at the beginning of slice
3478 * subjected to upper limit of cfq_quantum.
3479 * */
3480 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003481 }
3482
3483 /*
3484 * Async queues must wait a bit before being allowed dispatch.
3485 * We also ramp up the dispatch depth gradually for async IO,
3486 * based on the last sync IO we serviced
3487 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003488 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003489 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003490 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003491
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003492 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
Jens Axboee00c54c2009-10-04 20:36:19 +02003493 if (!depth && !cfqq->dispatched)
3494 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003495 if (depth < max_dispatch)
3496 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
3498
Jens Axboe0b182d62009-10-06 20:49:37 +02003499 /*
3500 * If we're below the current max, allow a dispatch
3501 */
3502 return cfqq->dispatched < max_dispatch;
3503}
3504
3505/*
3506 * Dispatch a request from cfqq, moving them to the request queue
3507 * dispatch list.
3508 */
3509static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3510{
3511 struct request *rq;
3512
3513 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3514
Glauber Costa3932a862016-09-22 20:59:59 -04003515 rq = cfq_check_fifo(cfqq);
3516 if (rq)
3517 cfq_mark_cfqq_must_dispatch(cfqq);
3518
Jens Axboe0b182d62009-10-06 20:49:37 +02003519 if (!cfq_may_dispatch(cfqd, cfqq))
3520 return false;
3521
3522 /*
3523 * follow expired path, else get first next available
3524 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003525 if (!rq)
3526 rq = cfqq->next_rq;
Glauber Costa3932a862016-09-22 20:59:59 -04003527 else
3528 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003529
3530 /*
3531 * insert request into driver dispatch list
3532 */
3533 cfq_dispatch_insert(cfqd->queue, rq);
3534
3535 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003536 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003537
Tejun Heoc5869802011-12-14 00:33:41 +01003538 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003539 cfqd->active_cic = cic;
3540 }
3541
3542 return true;
3543}
3544
3545/*
3546 * Find the cfqq that we need to service and move a request from that to the
3547 * dispatch list
3548 */
3549static int cfq_dispatch_requests(struct request_queue *q, int force)
3550{
3551 struct cfq_data *cfqd = q->elevator->elevator_data;
3552 struct cfq_queue *cfqq;
3553
3554 if (!cfqd->busy_queues)
3555 return 0;
3556
3557 if (unlikely(force))
3558 return cfq_forced_dispatch(cfqd);
3559
3560 cfqq = cfq_select_queue(cfqd);
3561 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003562 return 0;
3563
Jens Axboe2f5cb732009-04-07 08:51:19 +02003564 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003565 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003566 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003567 if (!cfq_dispatch_request(cfqd, cfqq))
3568 return 0;
3569
Jens Axboe2f5cb732009-04-07 08:51:19 +02003570 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003571 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003572
3573 /*
3574 * expire an async queue immediately if it has used up its slice. idle
3575 * queue always expire after 1 dispatch round.
3576 */
3577 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3578 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3579 cfq_class_idle(cfqq))) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003580 cfqq->slice_end = ktime_get_ns() + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003581 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003582 }
3583
Shan Weib217a902009-09-01 10:06:42 +02003584 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003585 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588/*
Jens Axboe5e705372006-07-13 12:39:25 +02003589 * task holds one reference to the queue, dropped when task exits. each rq
3590 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003592 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 * queue lock must be held here.
3594 */
3595static void cfq_put_queue(struct cfq_queue *cfqq)
3596{
Jens Axboe22e2c502005-06-27 10:55:12 +02003597 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003598 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003599
Shaohua Li30d7b942011-01-07 08:46:59 +01003600 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Shaohua Li30d7b942011-01-07 08:46:59 +01003602 cfqq->ref--;
3603 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return;
3605
Jens Axboe7b679132008-05-30 12:23:07 +02003606 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003608 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003609 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003611 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003612 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003613 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003614 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003615
Vivek Goyalf04a6422009-12-03 12:59:40 -05003616 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003618 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
Shaohua Lid02a2c02010-05-25 10:16:53 +02003621static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003622{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003623 struct cfq_queue *__cfqq, *next;
3624
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003625 /*
3626 * If this queue was scheduled to merge with another queue, be
3627 * sure to drop the reference taken on that queue (and others in
3628 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3629 */
3630 __cfqq = cfqq->new_cfqq;
3631 while (__cfqq) {
3632 if (__cfqq == cfqq) {
3633 WARN(1, "cfqq->new_cfqq loop detected\n");
3634 break;
3635 }
3636 next = __cfqq->new_cfqq;
3637 cfq_put_queue(__cfqq);
3638 __cfqq = next;
3639 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003640}
3641
3642static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3643{
3644 if (unlikely(cfqq == cfqd->active_queue)) {
3645 __cfq_slice_expired(cfqd, cfqq, 0);
3646 cfq_schedule_dispatch(cfqd);
3647 }
3648
3649 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003650
Jens Axboe89850f72006-07-22 16:48:31 +02003651 cfq_put_queue(cfqq);
3652}
3653
Tejun Heo9b84cac2011-12-14 00:33:42 +01003654static void cfq_init_icq(struct io_cq *icq)
3655{
3656 struct cfq_io_cq *cic = icq_to_cic(icq);
3657
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003658 cic->ttime.last_end_request = ktime_get_ns();
Tejun Heo9b84cac2011-12-14 00:33:42 +01003659}
3660
Tejun Heoc5869802011-12-14 00:33:41 +01003661static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003662{
Tejun Heoc5869802011-12-14 00:33:41 +01003663 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003664 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003665
Tejun Heo563180a2015-08-18 14:55:00 -07003666 if (cic_to_cfqq(cic, false)) {
3667 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3668 cic_set_cfqq(cic, NULL, false);
Jens Axboe89850f72006-07-22 16:48:31 +02003669 }
3670
Tejun Heo563180a2015-08-18 14:55:00 -07003671 if (cic_to_cfqq(cic, true)) {
3672 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3673 cic_set_cfqq(cic, NULL, true);
Jens Axboe89850f72006-07-22 16:48:31 +02003674 }
Jens Axboe89850f72006-07-22 16:48:31 +02003675}
3676
Tejun Heoabede6d2012-03-19 15:10:57 -07003677static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003678{
3679 struct task_struct *tsk = current;
3680 int ioprio_class;
3681
Jens Axboe3b181522005-06-27 10:56:24 +02003682 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003683 return;
3684
Tejun Heo598971b2012-03-19 15:10:58 -07003685 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003686 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003687 default:
3688 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3689 case IOPRIO_CLASS_NONE:
3690 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003691 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003692 */
3693 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003694 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003695 break;
3696 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003697 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003698 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3699 break;
3700 case IOPRIO_CLASS_BE:
Tejun Heo598971b2012-03-19 15:10:58 -07003701 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003702 cfqq->ioprio_class = IOPRIO_CLASS_BE;
3703 break;
3704 case IOPRIO_CLASS_IDLE:
3705 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3706 cfqq->ioprio = 7;
3707 cfq_clear_cfqq_idle_window(cfqq);
3708 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003709 }
3710
3711 /*
3712 * keep track of original prio settings in case we have to temporarily
3713 * elevate the priority of this queue
3714 */
3715 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -06003716 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02003717 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003718}
3719
Tejun Heo598971b2012-03-19 15:10:58 -07003720static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003721{
Tejun Heo598971b2012-03-19 15:10:58 -07003722 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003723 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003724 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003725
Tejun Heo598971b2012-03-19 15:10:58 -07003726 /*
3727 * Check whether ioprio has changed. The condition may trigger
3728 * spuriously on a newly created cic but there's no harm.
3729 */
3730 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003731 return;
3732
Tejun Heo563180a2015-08-18 14:55:00 -07003733 cfqq = cic_to_cfqq(cic, false);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003734 if (cfqq) {
Tejun Heo563180a2015-08-18 14:55:00 -07003735 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07003736 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
Tejun Heo563180a2015-08-18 14:55:00 -07003737 cic_set_cfqq(cic, cfqq, false);
Jens Axboe22e2c502005-06-27 10:55:12 +02003738 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003739
Tejun Heo563180a2015-08-18 14:55:00 -07003740 cfqq = cic_to_cfqq(cic, true);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003741 if (cfqq)
3742 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003743
3744 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003745}
3746
Jens Axboed5036d72009-06-26 10:44:34 +02003747static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003748 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003749{
3750 RB_CLEAR_NODE(&cfqq->rb_node);
3751 RB_CLEAR_NODE(&cfqq->p_node);
3752 INIT_LIST_HEAD(&cfqq->fifo);
3753
Shaohua Li30d7b942011-01-07 08:46:59 +01003754 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003755 cfqq->cfqd = cfqd;
3756
3757 cfq_mark_cfqq_prio_changed(cfqq);
3758
3759 if (is_sync) {
3760 if (!cfq_class_idle(cfqq))
3761 cfq_mark_cfqq_idle_window(cfqq);
3762 cfq_mark_cfqq_sync(cfqq);
3763 }
3764 cfqq->pid = pid;
3765}
3766
Vivek Goyal246103332009-12-03 12:59:51 -05003767#ifdef CONFIG_CFQ_GROUP_IOSCHED
Jan Kara142bbdf2017-04-04 14:31:30 +02003768static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003769{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003770 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo60a83702015-08-18 14:55:05 -07003771 struct cfq_queue *cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003772 uint64_t serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003773
Tejun Heo598971b2012-03-19 15:10:58 -07003774 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003775 serial_nr = bio_blkcg(bio)->css.serial_nr;
Tejun Heo598971b2012-03-19 15:10:58 -07003776 rcu_read_unlock();
3777
3778 /*
3779 * Check whether blkcg has changed. The condition may trigger
3780 * spuriously on a newly created cic but there's no harm.
3781 */
Tejun Heof4da8072014-09-08 08:15:20 +09003782 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Jan Kara142bbdf2017-04-04 14:31:30 +02003783 return;
Jens Axboe87760e52016-11-09 12:38:14 -07003784
3785 /*
Tejun Heo60a83702015-08-18 14:55:05 -07003786 * Drop reference to queues. New queues will be assigned in new
3787 * group upon arrival of fresh requests.
3788 */
3789 cfqq = cic_to_cfqq(cic, false);
3790 if (cfqq) {
3791 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3792 cic_set_cfqq(cic, NULL, false);
3793 cfq_put_queue(cfqq);
3794 }
3795
3796 cfqq = cic_to_cfqq(cic, true);
3797 if (cfqq) {
3798 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3799 cic_set_cfqq(cic, NULL, true);
3800 cfq_put_queue(cfqq);
Vivek Goyal246103332009-12-03 12:59:51 -05003801 }
Tejun Heo598971b2012-03-19 15:10:58 -07003802
Tejun Heof4da8072014-09-08 08:15:20 +09003803 cic->blkcg_serial_nr = serial_nr;
Vivek Goyal246103332009-12-03 12:59:51 -05003804}
Tejun Heo598971b2012-03-19 15:10:58 -07003805#else
Jan Kara142bbdf2017-04-04 14:31:30 +02003806static inline void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003807{
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003808}
Vivek Goyal246103332009-12-03 12:59:51 -05003809#endif /* CONFIG_CFQ_GROUP_IOSCHED */
3810
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003811static struct cfq_queue **
Tejun Heo60a83702015-08-18 14:55:05 -07003812cfq_async_queue_prio(struct cfq_group *cfqg, int ioprio_class, int ioprio)
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003813{
Jens Axboefe094d92008-01-31 13:08:54 +01003814 switch (ioprio_class) {
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003815 case IOPRIO_CLASS_RT:
Tejun Heo60a83702015-08-18 14:55:05 -07003816 return &cfqg->async_cfqq[0][ioprio];
Tejun Heo598971b2012-03-19 15:10:58 -07003817 case IOPRIO_CLASS_NONE:
3818 ioprio = IOPRIO_NORM;
3819 /* fall through */
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003820 case IOPRIO_CLASS_BE:
Tejun Heo60a83702015-08-18 14:55:05 -07003821 return &cfqg->async_cfqq[1][ioprio];
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003822 case IOPRIO_CLASS_IDLE:
Tejun Heo60a83702015-08-18 14:55:05 -07003823 return &cfqg->async_idle_cfqq;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003824 default:
3825 BUG();
3826 }
3827}
3828
Jens Axboe15c31be2007-07-10 13:43:25 +02003829static struct cfq_queue *
Tejun Heoabede6d2012-03-19 15:10:57 -07003830cfq_get_queue(struct cfq_data *cfqd, bool is_sync, struct cfq_io_cq *cic,
Tejun Heo2da8de02015-08-18 14:55:02 -07003831 struct bio *bio)
Jens Axboe15c31be2007-07-10 13:43:25 +02003832{
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003833 int ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
3834 int ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Tejun Heod4aad7f2015-08-18 14:55:04 -07003835 struct cfq_queue **async_cfqq = NULL;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003836 struct cfq_queue *cfqq;
Tejun Heo322731e2015-08-18 14:55:03 -07003837 struct cfq_group *cfqg;
3838
3839 rcu_read_lock();
Tejun Heoae118892015-08-18 14:55:20 -07003840 cfqg = cfq_lookup_cfqg(cfqd, bio_blkcg(bio));
Tejun Heo322731e2015-08-18 14:55:03 -07003841 if (!cfqg) {
3842 cfqq = &cfqd->oom_cfqq;
3843 goto out;
3844 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003845
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003846 if (!is_sync) {
Jeff Moyerc6ce1942015-01-12 15:21:01 -05003847 if (!ioprio_valid(cic->ioprio)) {
3848 struct task_struct *tsk = current;
3849 ioprio = task_nice_ioprio(tsk);
3850 ioprio_class = task_nice_ioclass(tsk);
3851 }
Tejun Heo60a83702015-08-18 14:55:05 -07003852 async_cfqq = cfq_async_queue_prio(cfqg, ioprio_class, ioprio);
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003853 cfqq = *async_cfqq;
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003854 if (cfqq)
3855 goto out;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003856 }
3857
Tejun Heoe00f4f42016-11-21 18:03:32 -05003858 cfqq = kmem_cache_alloc_node(cfq_pool,
3859 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
Tejun Heod4aad7f2015-08-18 14:55:04 -07003860 cfqd->queue->node);
3861 if (!cfqq) {
3862 cfqq = &cfqd->oom_cfqq;
3863 goto out;
3864 }
Jens Axboe15c31be2007-07-10 13:43:25 +02003865
Alexander Potapenko4d608ba2017-01-23 15:06:43 +01003866 /* cfq_init_cfqq() assumes cfqq->ioprio_class is initialized. */
3867 cfqq->ioprio_class = IOPRIO_CLASS_NONE;
Tejun Heod4aad7f2015-08-18 14:55:04 -07003868 cfq_init_cfqq(cfqd, cfqq, current->pid, is_sync);
3869 cfq_init_prio_data(cfqq, cic);
3870 cfq_link_cfqq_cfqg(cfqq, cfqg);
3871 cfq_log_cfqq(cfqd, cfqq, "alloced");
3872
3873 if (async_cfqq) {
3874 /* a new async queue is created, pin and remember */
Shaohua Li30d7b942011-01-07 08:46:59 +01003875 cfqq->ref++;
Vasily Tarasovc2dea2d2007-07-20 10:06:38 +02003876 *async_cfqq = cfqq;
Jens Axboe15c31be2007-07-10 13:43:25 +02003877 }
Tejun Heo4ebc1c62015-08-18 14:54:57 -07003878out:
Shaohua Li30d7b942011-01-07 08:46:59 +01003879 cfqq->ref++;
Tejun Heo322731e2015-08-18 14:55:03 -07003880 rcu_read_unlock();
Jens Axboe15c31be2007-07-10 13:43:25 +02003881 return cfqq;
3882}
3883
Jens Axboe22e2c502005-06-27 10:55:12 +02003884static void
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003885__cfq_update_io_thinktime(struct cfq_ttime *ttime, u64 slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003886{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003887 u64 elapsed = ktime_get_ns() - ttime->last_end_request;
Shaohua Li383cd722011-07-12 14:24:35 +02003888 elapsed = min(elapsed, 2UL * slice_idle);
Jens Axboe22e2c502005-06-27 10:55:12 +02003889
Shaohua Li383cd722011-07-12 14:24:35 +02003890 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003891 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
3892 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
3893 ttime->ttime_samples);
Shaohua Li383cd722011-07-12 14:24:35 +02003894}
3895
3896static void
3897cfq_update_io_thinktime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003898 struct cfq_io_cq *cic)
Shaohua Li383cd722011-07-12 14:24:35 +02003899{
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003900 if (cfq_cfqq_sync(cfqq)) {
Shaohua Li383cd722011-07-12 14:24:35 +02003901 __cfq_update_io_thinktime(&cic->ttime, cfqd->cfq_slice_idle);
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02003902 __cfq_update_io_thinktime(&cfqq->service_tree->ttime,
3903 cfqd->cfq_slice_idle);
3904 }
Shaohua Li7700fc42011-07-12 14:24:56 +02003905#ifdef CONFIG_CFQ_GROUP_IOSCHED
3906 __cfq_update_io_thinktime(&cfqq->cfqg->ttime, cfqd->cfq_group_idle);
3907#endif
Jens Axboe22e2c502005-06-27 10:55:12 +02003908}
3909
Jens Axboe206dc692006-03-28 13:03:44 +02003910static void
Jeff Moyerb2c18e12009-10-23 17:14:49 -04003911cfq_update_io_seektime(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboe6d048f52007-04-25 12:44:27 +02003912 struct request *rq)
Jens Axboe206dc692006-03-28 13:03:44 +02003913{
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003914 sector_t sdist = 0;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003915 sector_t n_sec = blk_rq_sectors(rq);
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003916 if (cfqq->last_request_pos) {
3917 if (cfqq->last_request_pos < blk_rq_pos(rq))
3918 sdist = blk_rq_pos(rq) - cfqq->last_request_pos;
3919 else
3920 sdist = cfqq->last_request_pos - blk_rq_pos(rq);
3921 }
Jens Axboe206dc692006-03-28 13:03:44 +02003922
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +01003923 cfqq->seek_history <<= 1;
Corrado Zoccolo41647e72010-02-27 19:45:40 +01003924 if (blk_queue_nonrot(cfqd->queue))
3925 cfqq->seek_history |= (n_sec < CFQQ_SECT_THR_NONROT);
3926 else
3927 cfqq->seek_history |= (sdist > CFQQ_SEEK_THR);
Jens Axboe206dc692006-03-28 13:03:44 +02003928}
Jens Axboe22e2c502005-06-27 10:55:12 +02003929
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003930static inline bool req_noidle(struct request *req)
3931{
3932 return req_op(req) == REQ_OP_WRITE &&
3933 (req->cmd_flags & (REQ_SYNC | REQ_IDLE)) == REQ_SYNC;
3934}
3935
Jens Axboe22e2c502005-06-27 10:55:12 +02003936/*
3937 * Disable idle window if the process thinks too long or seeks so much that
3938 * it doesn't matter
3939 */
3940static void
3941cfq_update_idle_window(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Tejun Heoc5869802011-12-14 00:33:41 +01003942 struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003943{
Jens Axboe7b679132008-05-30 12:23:07 +02003944 int old_idle, enable_idle;
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003945
Jens Axboe08717142008-01-28 11:38:15 +01003946 /*
3947 * Don't idle for async or idle io prio class
3948 */
3949 if (!cfq_cfqq_sync(cfqq) || cfq_class_idle(cfqq))
Jens Axboe1be92f2f2007-04-19 14:32:26 +02003950 return;
3951
Jens Axboec265a7f2008-06-26 13:49:33 +02003952 enable_idle = old_idle = cfq_cfqq_idle_window(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003953
Corrado Zoccolo76280af2009-11-26 10:02:58 +01003954 if (cfqq->queued[0] + cfqq->queued[1] >= 4)
3955 cfq_mark_cfqq_deep(cfqq);
3956
Christoph Hellwiga2b80962016-11-01 07:40:09 -06003957 if (cfqq->next_rq && req_noidle(cfqq->next_rq))
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02003958 enable_idle = 0;
Tejun Heof6e8d012012-03-05 13:15:26 -08003959 else if (!atomic_read(&cic->icq.ioc->active_ref) ||
Tejun Heoc5869802011-12-14 00:33:41 +01003960 !cfqd->cfq_slice_idle ||
3961 (!cfq_cfqq_deep(cfqq) && CFQQ_SEEKY(cfqq)))
Jens Axboe22e2c502005-06-27 10:55:12 +02003962 enable_idle = 0;
Shaohua Li383cd722011-07-12 14:24:35 +02003963 else if (sample_valid(cic->ttime.ttime_samples)) {
3964 if (cic->ttime.ttime_mean > cfqd->cfq_slice_idle)
Jens Axboe22e2c502005-06-27 10:55:12 +02003965 enable_idle = 0;
3966 else
3967 enable_idle = 1;
3968 }
3969
Jens Axboe7b679132008-05-30 12:23:07 +02003970 if (old_idle != enable_idle) {
3971 cfq_log_cfqq(cfqd, cfqq, "idle=%d", enable_idle);
3972 if (enable_idle)
3973 cfq_mark_cfqq_idle_window(cfqq);
3974 else
3975 cfq_clear_cfqq_idle_window(cfqq);
3976 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003977}
3978
Jens Axboe22e2c502005-06-27 10:55:12 +02003979/*
3980 * Check if new_cfqq should preempt the currently active queue. Return 0 for
3981 * no or if we aren't sure, a 1 will cause a preempt.
3982 */
Jens Axboea6151c32009-10-07 20:02:57 +02003983static bool
Jens Axboe22e2c502005-06-27 10:55:12 +02003984cfq_should_preempt(struct cfq_data *cfqd, struct cfq_queue *new_cfqq,
Jens Axboe5e705372006-07-13 12:39:25 +02003985 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02003986{
Jens Axboe6d048f52007-04-25 12:44:27 +02003987 struct cfq_queue *cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003988
Jens Axboe6d048f52007-04-25 12:44:27 +02003989 cfqq = cfqd->active_queue;
3990 if (!cfqq)
Jens Axboea6151c32009-10-07 20:02:57 +02003991 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003992
Jens Axboe6d048f52007-04-25 12:44:27 +02003993 if (cfq_class_idle(new_cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003994 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02003995
3996 if (cfq_class_idle(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02003997 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01003998
Jens Axboe22e2c502005-06-27 10:55:12 +02003999 /*
Divyesh Shah875feb62010-01-06 18:58:20 -08004000 * Don't allow a non-RT request to preempt an ongoing RT cfqq timeslice.
4001 */
4002 if (cfq_class_rt(cfqq) && !cfq_class_rt(new_cfqq))
4003 return false;
4004
4005 /*
Jens Axboe374f84a2006-07-23 01:42:19 +02004006 * if the new request is sync, but the currently running queue is
4007 * not, let the sync request have priority.
4008 */
Glauber Costa3932a862016-09-22 20:59:59 -04004009 if (rq_is_sync(rq) && !cfq_cfqq_sync(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004010 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004011
Jan Kara3984aa52016-01-12 16:24:19 +01004012 /*
4013 * Treat ancestors of current cgroup the same way as current cgroup.
4014 * For anybody else we disallow preemption to guarantee service
4015 * fairness among cgroups.
4016 */
4017 if (!cfqg_is_descendant(cfqq->cfqg, new_cfqq->cfqg))
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004018 return false;
4019
4020 if (cfq_slice_used(cfqq))
4021 return true;
4022
Jan Kara6c80731c2016-01-12 16:24:16 +01004023 /*
4024 * Allow an RT request to pre-empt an ongoing non-RT cfqq timeslice.
4025 */
4026 if (cfq_class_rt(new_cfqq) && !cfq_class_rt(cfqq))
4027 return true;
4028
4029 WARN_ON_ONCE(cfqq->ioprio_class != new_cfqq->ioprio_class);
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004030 /* Allow preemption only if we are idling on sync-noidle tree */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004031 if (cfqd->serving_wl_type == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004032 cfqq_type(new_cfqq) == SYNC_NOIDLE_WORKLOAD &&
Vivek Goyal8682e1f2009-12-03 12:59:50 -05004033 RB_EMPTY_ROOT(&cfqq->sort_list))
4034 return true;
4035
Jens Axboe374f84a2006-07-23 01:42:19 +02004036 /*
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004037 * So both queues are sync. Let the new request get disk time if
4038 * it's a metadata request and the current queue is doing regular IO.
4039 */
Christoph Hellwig65299a32011-08-23 14:50:29 +02004040 if ((rq->cmd_flags & REQ_PRIO) && !cfqq->prio_pending)
Jens Axboeb53d1ed2011-08-19 08:34:48 +02004041 return true;
4042
Shaohua Lid2d59e12010-11-08 15:01:03 +01004043 /* An idle queue should not be idle now for some reason */
4044 if (RB_EMPTY_ROOT(&cfqq->sort_list) && !cfq_should_idle(cfqd, cfqq))
4045 return true;
4046
Jens Axboe1e3335d2007-02-14 19:59:49 +01004047 if (!cfqd->active_cic || !cfq_cfqq_wait_request(cfqq))
Jens Axboea6151c32009-10-07 20:02:57 +02004048 return false;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004049
4050 /*
4051 * if this request is as-good as one we would expect from the
4052 * current cfqq, let it preempt
4053 */
Shaohua Lie9ce3352010-03-19 08:03:04 +01004054 if (cfq_rq_close(cfqd, cfqq, rq))
Jens Axboea6151c32009-10-07 20:02:57 +02004055 return true;
Jens Axboe1e3335d2007-02-14 19:59:49 +01004056
Jens Axboea6151c32009-10-07 20:02:57 +02004057 return false;
Jens Axboe22e2c502005-06-27 10:55:12 +02004058}
4059
4060/*
4061 * cfqq preempts the active queue. if we allowed preempt with no slice left,
4062 * let it have half of its nominal slice.
4063 */
4064static void cfq_preempt_queue(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4065{
Shaohua Lidf0793a2012-01-19 09:20:09 +01004066 enum wl_type_t old_type = cfqq_type(cfqd->active_queue);
4067
Jens Axboe7b679132008-05-30 12:23:07 +02004068 cfq_log_cfqq(cfqd, cfqq, "preempt");
Shaohua Lidf0793a2012-01-19 09:20:09 +01004069 cfq_slice_expired(cfqd, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004070
Jens Axboebf572252006-07-19 20:29:12 +02004071 /*
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004072 * workload type is changed, don't save slice, otherwise preempt
4073 * doesn't happen
4074 */
Shaohua Lidf0793a2012-01-19 09:20:09 +01004075 if (old_type != cfqq_type(cfqq))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04004076 cfqq->cfqg->saved_wl_slice = 0;
Shaohua Lif8ae6e32011-01-14 08:41:02 +01004077
4078 /*
Jens Axboebf572252006-07-19 20:29:12 +02004079 * Put the new queue at the front of the of the current list,
4080 * so we know that it will be selected next.
4081 */
4082 BUG_ON(!cfq_cfqq_on_rr(cfqq));
Jens Axboeedd75ff2007-04-19 12:03:34 +02004083
4084 cfq_service_tree_add(cfqd, cfqq, 1);
Justin TerAvesteda5e0c2011-03-22 21:26:49 +01004085
Justin TerAvest62a37f62011-03-23 08:25:44 +01004086 cfqq->slice_end = 0;
4087 cfq_mark_cfqq_slice_new(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004088}
4089
4090/*
Jens Axboe5e705372006-07-13 12:39:25 +02004091 * Called when a new fs request (rq) is added (to cfqq). Check if there's
Jens Axboe22e2c502005-06-27 10:55:12 +02004092 * something we should do about it
4093 */
4094static void
Jens Axboe5e705372006-07-13 12:39:25 +02004095cfq_rq_enqueued(struct cfq_data *cfqd, struct cfq_queue *cfqq,
4096 struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004097{
Tejun Heoc5869802011-12-14 00:33:41 +01004098 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe12e9fdd2006-06-01 10:09:56 +02004099
Aaron Carroll45333d52008-08-26 15:52:36 +02004100 cfqd->rq_queued++;
Christoph Hellwig65299a32011-08-23 14:50:29 +02004101 if (rq->cmd_flags & REQ_PRIO)
4102 cfqq->prio_pending++;
Jens Axboe374f84a2006-07-23 01:42:19 +02004103
Shaohua Li383cd722011-07-12 14:24:35 +02004104 cfq_update_io_thinktime(cfqd, cfqq, cic);
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004105 cfq_update_io_seektime(cfqd, cfqq, rq);
Jens Axboe9c2c38a2005-08-24 14:57:54 +02004106 cfq_update_idle_window(cfqd, cfqq, cic);
4107
Jeff Moyerb2c18e12009-10-23 17:14:49 -04004108 cfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004109
4110 if (cfqq == cfqd->active_queue) {
4111 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004112 * Remember that we saw a request from this process, but
4113 * don't start queuing just yet. Otherwise we risk seeing lots
4114 * of tiny requests, because we disrupt the normal plugging
Jens Axboed6ceb252009-04-14 14:18:16 +02004115 * and merging. If the request is already larger than a single
4116 * page, let it rip immediately. For that case we assume that
Jens Axboe2d870722009-04-15 12:12:46 +02004117 * merging is already done. Ditto for a busy system that
4118 * has other work pending, don't risk delaying until the
4119 * idle timer unplug to continue working.
Jens Axboe22e2c502005-06-27 10:55:12 +02004120 */
Jens Axboed6ceb252009-04-14 14:18:16 +02004121 if (cfq_cfqq_wait_request(cfqq)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004122 if (blk_rq_bytes(rq) > PAGE_SIZE ||
Jens Axboe2d870722009-04-15 12:12:46 +02004123 cfqd->busy_queues > 1) {
Divyesh Shah812df482010-04-08 21:15:35 -07004124 cfq_del_timer(cfqd, cfqq);
Gui Jianfeng554554f2009-12-10 09:38:39 +01004125 cfq_clear_cfqq_wait_request(cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004126 __blk_run_queue(cfqd->queue);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004127 } else {
Tejun Heo155fead2012-04-01 14:38:44 -07004128 cfqg_stats_update_idle_time(cfqq->cfqg);
Vivek Goyalbf7919372009-12-03 12:59:37 -05004129 cfq_mark_cfqq_must_dispatch(cfqq);
Divyesh Shaha11cdaa2010-04-13 19:59:17 +02004130 }
Jens Axboed6ceb252009-04-14 14:18:16 +02004131 }
Jens Axboe5e705372006-07-13 12:39:25 +02004132 } else if (cfq_should_preempt(cfqd, cfqq, rq)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004133 /*
4134 * not the active queue - expire current slice if it is
4135 * idle and has expired it's mean thinktime or this new queue
Divyesh Shah3a9a3f62009-01-30 12:46:41 +01004136 * has some old slice time left and is of higher priority or
4137 * this new queue is RT and the current one is BE
Jens Axboe22e2c502005-06-27 10:55:12 +02004138 */
4139 cfq_preempt_queue(cfqd, cfqq);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004140 __blk_run_queue(cfqd->queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004141 }
4142}
4143
Jens Axboe165125e2007-07-24 09:28:11 +02004144static void cfq_insert_request(struct request_queue *q, struct request *rq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004145{
Jens Axboeb4878f22005-10-20 16:42:29 +02004146 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02004147 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004148
Jens Axboe7b679132008-05-30 12:23:07 +02004149 cfq_log_cfqq(cfqd, cfqq, "insert_request");
Tejun Heoabede6d2012-03-19 15:10:57 -07004150 cfq_init_prio_data(cfqq, RQ_CIC(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004152 rq->fifo_time = ktime_get_ns() + cfqd->cfq_fifo_expire[rq_is_sync(rq)];
Jens Axboe22e2c502005-06-27 10:55:12 +02004153 list_add_tail(&rq->queuelist, &cfqq->fifo);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01004154 cfq_add_rq_rb(rq);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004155 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqd->serving_group,
Tejun Heo155fead2012-04-01 14:38:44 -07004156 rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02004157 cfq_rq_enqueued(cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
Aaron Carroll45333d52008-08-26 15:52:36 +02004160/*
4161 * Update hw_tag based on peak queue depth over 50 samples under
4162 * sufficient load.
4163 */
4164static void cfq_update_hw_tag(struct cfq_data *cfqd)
4165{
Shaohua Li1a1238a2009-10-27 08:46:23 +01004166 struct cfq_queue *cfqq = cfqd->active_queue;
4167
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004168 if (cfqd->rq_in_driver > cfqd->hw_tag_est_depth)
4169 cfqd->hw_tag_est_depth = cfqd->rq_in_driver;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004170
4171 if (cfqd->hw_tag == 1)
4172 return;
Aaron Carroll45333d52008-08-26 15:52:36 +02004173
4174 if (cfqd->rq_queued <= CFQ_HW_QUEUE_MIN &&
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004175 cfqd->rq_in_driver <= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004176 return;
4177
Shaohua Li1a1238a2009-10-27 08:46:23 +01004178 /*
4179 * If active queue hasn't enough requests and can idle, cfq might not
4180 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
4181 * case
4182 */
4183 if (cfqq && cfq_cfqq_idle_window(cfqq) &&
4184 cfqq->dispatched + cfqq->queued[0] + cfqq->queued[1] <
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004185 CFQ_HW_QUEUE_MIN && cfqd->rq_in_driver < CFQ_HW_QUEUE_MIN)
Shaohua Li1a1238a2009-10-27 08:46:23 +01004186 return;
4187
Aaron Carroll45333d52008-08-26 15:52:36 +02004188 if (cfqd->hw_tag_samples++ < 50)
4189 return;
4190
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004191 if (cfqd->hw_tag_est_depth >= CFQ_HW_QUEUE_MIN)
Aaron Carroll45333d52008-08-26 15:52:36 +02004192 cfqd->hw_tag = 1;
4193 else
4194 cfqd->hw_tag = 0;
Aaron Carroll45333d52008-08-26 15:52:36 +02004195}
4196
Vivek Goyal7667aa02009-12-08 17:52:58 -05004197static bool cfq_should_wait_busy(struct cfq_data *cfqd, struct cfq_queue *cfqq)
4198{
Tejun Heoc5869802011-12-14 00:33:41 +01004199 struct cfq_io_cq *cic = cfqd->active_cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004200 u64 now = ktime_get_ns();
Vivek Goyal7667aa02009-12-08 17:52:58 -05004201
Justin TerAvest02a8f012011-02-09 14:20:03 +01004202 /* If the queue already has requests, don't wait */
4203 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
4204 return false;
4205
Vivek Goyal7667aa02009-12-08 17:52:58 -05004206 /* If there are other queues in the group, don't wait */
4207 if (cfqq->cfqg->nr_cfqq > 1)
4208 return false;
4209
Shaohua Li7700fc42011-07-12 14:24:56 +02004210 /* the only queue in the group, but think time is big */
4211 if (cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true))
4212 return false;
4213
Vivek Goyal7667aa02009-12-08 17:52:58 -05004214 if (cfq_slice_used(cfqq))
4215 return true;
4216
4217 /* if slice left is less than think time, wait busy */
Shaohua Li383cd722011-07-12 14:24:35 +02004218 if (cic && sample_valid(cic->ttime.ttime_samples)
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004219 && (cfqq->slice_end - now < cic->ttime.ttime_mean))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004220 return true;
4221
4222 /*
4223 * If think times is less than a jiffy than ttime_mean=0 and above
4224 * will not be true. It might happen that slice has not expired yet
4225 * but will expire soon (4-5 ns) during select_queue(). To cover the
4226 * case where think time is less than a jiffy, mark the queue wait
4227 * busy if only 1 jiffy is left in the slice.
4228 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004229 if (cfqq->slice_end - now <= jiffies_to_nsecs(1))
Vivek Goyal7667aa02009-12-08 17:52:58 -05004230 return true;
4231
4232 return false;
4233}
4234
Jens Axboe165125e2007-07-24 09:28:11 +02004235static void cfq_completed_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236{
Jens Axboe5e705372006-07-13 12:39:25 +02004237 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02004238 struct cfq_data *cfqd = cfqq->cfqd;
Jens Axboe5380a102006-07-13 12:37:56 +02004239 const int sync = rq_is_sync(rq);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004240 u64 now = ktime_get_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Christoph Hellwiga2b80962016-11-01 07:40:09 -06004242 cfq_log_cfqq(cfqd, cfqq, "complete rqnoidle %d", req_noidle(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Aaron Carroll45333d52008-08-26 15:52:36 +02004244 cfq_update_hw_tag(cfqd);
4245
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004246 WARN_ON(!cfqd->rq_in_driver);
Jens Axboe6d048f52007-04-25 12:44:27 +02004247 WARN_ON(!cfqq->dispatched);
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004248 cfqd->rq_in_driver--;
Jens Axboe6d048f52007-04-25 12:44:27 +02004249 cfqq->dispatched--;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004250 (RQ_CFQG(rq))->dispatched--;
Tejun Heo155fead2012-04-01 14:38:44 -07004251 cfqg_stats_update_completion(cfqq->cfqg, rq_start_time_ns(rq),
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004252 rq_io_start_time_ns(rq), rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004254 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]--;
Jens Axboe3ed9a292007-04-23 08:33:33 +02004255
Vivek Goyal365722b2009-10-03 15:21:27 +02004256 if (sync) {
Vivek Goyal34b98d02012-10-03 16:56:58 -04004257 struct cfq_rb_root *st;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004258
Shaohua Li383cd722011-07-12 14:24:35 +02004259 RQ_CIC(rq)->ttime.last_end_request = now;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004260
4261 if (cfq_cfqq_on_rr(cfqq))
Vivek Goyal34b98d02012-10-03 16:56:58 -04004262 st = cfqq->service_tree;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +02004263 else
Vivek Goyal34b98d02012-10-03 16:56:58 -04004264 st = st_for(cfqq->cfqg, cfqq_class(cfqq),
4265 cfqq_type(cfqq));
4266
4267 st->ttime.last_end_request = now;
Jan Kara149321a2016-06-28 09:04:01 +02004268 /*
4269 * We have to do this check in jiffies since start_time is in
4270 * jiffies and it is not trivial to convert to ns. If
4271 * cfq_fifo_expire[1] ever comes close to 1 jiffie, this test
4272 * will become problematic but so far we are fine (the default
4273 * is 128 ms).
4274 */
4275 if (!time_after(rq->start_time +
4276 nsecs_to_jiffies(cfqd->cfq_fifo_expire[1]),
4277 jiffies))
Corrado Zoccolo573412b2009-12-06 11:48:52 +01004278 cfqd->last_delayed_sync = now;
Vivek Goyal365722b2009-10-03 15:21:27 +02004279 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004280
Shaohua Li7700fc42011-07-12 14:24:56 +02004281#ifdef CONFIG_CFQ_GROUP_IOSCHED
4282 cfqq->cfqg->ttime.last_end_request = now;
4283#endif
4284
Jens Axboecaaa5f92006-06-16 11:23:00 +02004285 /*
4286 * If this is the active queue, check if it needs to be expired,
4287 * or if we want to idle in case it has no pending requests.
4288 */
4289 if (cfqd->active_queue == cfqq) {
Jens Axboea36e71f2009-04-15 12:15:11 +02004290 const bool cfqq_empty = RB_EMPTY_ROOT(&cfqq->sort_list);
4291
Jens Axboe44f7c162007-01-19 11:51:58 +11004292 if (cfq_cfqq_slice_new(cfqq)) {
4293 cfq_set_prio_slice(cfqd, cfqq);
4294 cfq_clear_cfqq_slice_new(cfqq);
4295 }
Vivek Goyalf75edf22009-12-03 12:59:53 -05004296
4297 /*
Vivek Goyal7667aa02009-12-08 17:52:58 -05004298 * Should we wait for next request to come in before we expire
4299 * the queue.
Vivek Goyalf75edf22009-12-03 12:59:53 -05004300 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05004301 if (cfq_should_wait_busy(cfqd, cfqq)) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004302 u64 extend_sl = cfqd->cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004303 if (!cfqd->cfq_slice_idle)
4304 extend_sl = cfqd->cfq_group_idle;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004305 cfqq->slice_end = now + extend_sl;
Vivek Goyalf75edf22009-12-03 12:59:53 -05004306 cfq_mark_cfqq_wait_busy(cfqq);
Divyesh Shahb1ffe732010-03-25 15:45:03 +01004307 cfq_log_cfqq(cfqd, cfqq, "will busy wait");
Vivek Goyalf75edf22009-12-03 12:59:53 -05004308 }
4309
Jens Axboea36e71f2009-04-15 12:15:11 +02004310 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004311 * Idling is not enabled on:
4312 * - expired queues
4313 * - idle-priority queues
4314 * - async queues
4315 * - queues with still some requests queued
4316 * - when there is a close cooperator
Jens Axboea36e71f2009-04-15 12:15:11 +02004317 */
Jens Axboe08717142008-01-28 11:38:15 +01004318 if (cfq_slice_used(cfqq) || cfq_class_idle(cfqq))
Vivek Goyale5ff0822010-04-26 19:25:11 +02004319 cfq_slice_expired(cfqd, 1);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004320 else if (sync && cfqq_empty &&
4321 !cfq_close_cooperator(cfqd, cfqq)) {
Corrado Zoccolo749ef9f2010-09-20 15:24:50 +02004322 cfq_arm_slice_timer(cfqd);
Corrado Zoccolo8e550632009-11-26 10:02:58 +01004323 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02004324 }
Jens Axboe6d048f52007-04-25 12:44:27 +02004325
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01004326 if (!cfqd->rq_in_driver)
Jens Axboe23e018a2009-10-05 08:52:35 +02004327 cfq_schedule_dispatch(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328}
4329
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004330static void cfqq_boost_on_prio(struct cfq_queue *cfqq, unsigned int op)
Jens Axboeb8269db2016-06-09 15:47:29 -06004331{
4332 /*
4333 * If REQ_PRIO is set, boost class and prio level, if it's below
4334 * BE/NORM. If prio is not set, restore the potentially boosted
4335 * class/prio level.
4336 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004337 if (!(op & REQ_PRIO)) {
Jens Axboeb8269db2016-06-09 15:47:29 -06004338 cfqq->ioprio_class = cfqq->org_ioprio_class;
4339 cfqq->ioprio = cfqq->org_ioprio;
4340 } else {
4341 if (cfq_class_idle(cfqq))
4342 cfqq->ioprio_class = IOPRIO_CLASS_BE;
4343 if (cfqq->ioprio > IOPRIO_NORM)
4344 cfqq->ioprio = IOPRIO_NORM;
4345 }
4346}
4347
Jens Axboe89850f72006-07-22 16:48:31 +02004348static inline int __cfq_may_queue(struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02004349{
Jens Axboe1b379d82009-08-11 08:26:11 +02004350 if (cfq_cfqq_wait_request(cfqq) && !cfq_cfqq_must_alloc_slice(cfqq)) {
Jens Axboe3b181522005-06-27 10:56:24 +02004351 cfq_mark_cfqq_must_alloc_slice(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004352 return ELV_MQUEUE_MUST;
Jens Axboe3b181522005-06-27 10:56:24 +02004353 }
Jens Axboe22e2c502005-06-27 10:55:12 +02004354
4355 return ELV_MQUEUE_MAY;
Jens Axboe22e2c502005-06-27 10:55:12 +02004356}
4357
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004358static int cfq_may_queue(struct request_queue *q, unsigned int op)
Jens Axboe22e2c502005-06-27 10:55:12 +02004359{
4360 struct cfq_data *cfqd = q->elevator->elevator_data;
4361 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01004362 struct cfq_io_cq *cic;
Jens Axboe22e2c502005-06-27 10:55:12 +02004363 struct cfq_queue *cfqq;
4364
4365 /*
4366 * don't force setup of a queue from here, as a call to may_queue
4367 * does not necessarily imply that a request actually will be queued.
4368 * so just lookup a possibly existing queue, or return 'may queue'
4369 * if that fails
4370 */
Jens Axboe4ac845a2008-01-24 08:44:49 +01004371 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004372 if (!cic)
4373 return ELV_MQUEUE_MAY;
4374
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004375 cfqq = cic_to_cfqq(cic, op_is_sync(op));
Jens Axboe22e2c502005-06-27 10:55:12 +02004376 if (cfqq) {
Tejun Heoabede6d2012-03-19 15:10:57 -07004377 cfq_init_prio_data(cfqq, cic);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06004378 cfqq_boost_on_prio(cfqq, op);
Jens Axboe22e2c502005-06-27 10:55:12 +02004379
Jens Axboe89850f72006-07-22 16:48:31 +02004380 return __cfq_may_queue(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004381 }
4382
4383 return ELV_MQUEUE_MAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384}
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386/*
4387 * queue lock held here
4388 */
Jens Axboebb37b942006-12-01 10:42:33 +01004389static void cfq_put_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Jens Axboe5e705372006-07-13 12:39:25 +02004391 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Jens Axboe5e705372006-07-13 12:39:25 +02004393 if (cfqq) {
Jens Axboe22e2c502005-06-27 10:55:12 +02004394 const int rw = rq_data_dir(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Jens Axboe22e2c502005-06-27 10:55:12 +02004396 BUG_ON(!cfqq->allocated[rw]);
4397 cfqq->allocated[rw]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004399 /* Put down rq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004400 cfqg_put(RQ_CFQG(rq));
Tejun Heoa612fdd2011-12-14 00:33:41 +01004401 rq->elv.priv[0] = NULL;
4402 rq->elv.priv[1] = NULL;
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02004403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 cfq_put_queue(cfqq);
4405 }
4406}
4407
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004408static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004409cfq_merge_cfqqs(struct cfq_data *cfqd, struct cfq_io_cq *cic,
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004410 struct cfq_queue *cfqq)
4411{
4412 cfq_log_cfqq(cfqd, cfqq, "merging with queue %p", cfqq->new_cfqq);
4413 cic_set_cfqq(cic, cfqq->new_cfqq, 1);
Jeff Moyerb3b6d042009-10-23 17:14:51 -04004414 cfq_mark_cfqq_coop(cfqq->new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004415 cfq_put_queue(cfqq);
4416 return cic_to_cfqq(cic, 1);
4417}
4418
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004419/*
4420 * Returns NULL if a new cfqq should be allocated, or the old cfqq if this
4421 * was the last process referring to said cfqq.
4422 */
4423static struct cfq_queue *
Tejun Heoc5869802011-12-14 00:33:41 +01004424split_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq)
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004425{
4426 if (cfqq_process_refs(cfqq) == 1) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004427 cfqq->pid = current->pid;
4428 cfq_clear_cfqq_coop(cfqq);
Shaohua Liae54abe2010-02-05 13:11:45 +01004429 cfq_clear_cfqq_split_coop(cfqq);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004430 return cfqq;
4431 }
4432
4433 cic_set_cfqq(cic, NULL, 1);
Shaohua Lid02a2c02010-05-25 10:16:53 +02004434
4435 cfq_put_cooperator(cfqq);
4436
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004437 cfq_put_queue(cfqq);
4438 return NULL;
4439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440/*
Jens Axboe22e2c502005-06-27 10:55:12 +02004441 * Allocate cfq data structures associated with this request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 */
Jens Axboe22e2c502005-06-27 10:55:12 +02004443static int
Tejun Heo852c7882012-03-05 13:15:27 -08004444cfq_set_request(struct request_queue *q, struct request *rq, struct bio *bio,
4445 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446{
4447 struct cfq_data *cfqd = q->elevator->elevator_data;
Tejun Heof1f8cc92011-12-14 00:33:42 +01004448 struct cfq_io_cq *cic = icq_to_cic(rq->elv.icq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 const int rw = rq_data_dir(rq);
Jens Axboea6151c32009-10-07 20:02:57 +02004450 const bool is_sync = rq_is_sync(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004451 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Tejun Heo216284c32011-12-14 00:33:38 +01004453 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004454
Tejun Heo598971b2012-03-19 15:10:58 -07004455 check_ioprio_changed(cic, bio);
Jan Kara142bbdf2017-04-04 14:31:30 +02004456 check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004457new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004458 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004459 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heobce61332015-08-18 14:54:59 -07004460 if (cfqq)
4461 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07004462 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004463 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004464 } else {
4465 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004466 * If the queue was seeky for too long, break it apart.
4467 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004468 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004469 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4470 cfqq = split_cfqq(cic, cfqq);
4471 if (!cfqq)
4472 goto new_queue;
4473 }
4474
4475 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004476 * Check to see if this queue is scheduled to merge with
4477 * another, closely cooperating queue. The merging of
4478 * queues happens here as it must be done in process context.
4479 * The reference on new_cfqq was taken in merge_cfqqs.
4480 */
4481 if (cfqq->new_cfqq)
4482 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004486
Jens Axboe6fae9c22011-03-01 15:04:39 -05004487 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004488 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004489 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004490 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c32011-12-14 00:33:38 +01004491 spin_unlock_irq(q->queue_lock);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004492
Jens Axboe5e705372006-07-13 12:39:25 +02004493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494}
4495
David Howells65f27f32006-11-22 14:55:48 +00004496static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004497{
David Howells65f27f32006-11-22 14:55:48 +00004498 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004499 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004500 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004501
Jens Axboe40bb54d2009-04-15 12:11:10 +02004502 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004503 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004504 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004505}
4506
4507/*
4508 * Timer running if the active_queue is currently idling inside its time slice
4509 */
Jan Kara91148322016-06-08 15:11:39 +02004510static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
Jens Axboe22e2c502005-06-27 10:55:12 +02004511{
Jan Kara91148322016-06-08 15:11:39 +02004512 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4513 idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02004514 struct cfq_queue *cfqq;
4515 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004516 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004517
Jens Axboe7b679132008-05-30 12:23:07 +02004518 cfq_log(cfqd, "idle timer fired");
4519
Jens Axboe22e2c502005-06-27 10:55:12 +02004520 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4521
Jens Axboefe094d92008-01-31 13:08:54 +01004522 cfqq = cfqd->active_queue;
4523 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004524 timed_out = 0;
4525
Jens Axboe22e2c502005-06-27 10:55:12 +02004526 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004527 * We saw a request before the queue expired, let it through
4528 */
4529 if (cfq_cfqq_must_dispatch(cfqq))
4530 goto out_kick;
4531
4532 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004533 * expired
4534 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004535 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004536 goto expire;
4537
4538 /*
4539 * only expire and reinvoke request handler, if there are
4540 * other queues with pending requests
4541 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004542 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004543 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004544
4545 /*
4546 * not expired and it has a request pending, let it dispatch
4547 */
Jens Axboe75e50982009-04-07 08:56:14 +02004548 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004549 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004550
4551 /*
4552 * Queue depth flag is reset only when the idle didn't succeed
4553 */
4554 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004555 }
4556expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004557 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004558out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004559 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004560out_cont:
4561 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jan Kara91148322016-06-08 15:11:39 +02004562 return HRTIMER_NORESTART;
Jens Axboe22e2c502005-06-27 10:55:12 +02004563}
4564
Jens Axboe3b181522005-06-27 10:56:24 +02004565static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4566{
Jan Kara91148322016-06-08 15:11:39 +02004567 hrtimer_cancel(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004568 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004569}
Jens Axboe22e2c502005-06-27 10:55:12 +02004570
Jens Axboeb374d182008-10-31 10:05:07 +01004571static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
Jens Axboe22e2c502005-06-27 10:55:12 +02004573 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004574 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004575
Jens Axboe3b181522005-06-27 10:56:24 +02004576 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004577
Al Virod9ff4182006-03-18 13:51:22 -05004578 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004579
Al Virod9ff4182006-03-18 13:51:22 -05004580 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004581 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004582
Tejun Heo03aa2642012-03-05 13:15:19 -08004583 spin_unlock_irq(q->queue_lock);
4584
Al Viroa90d7422006-03-18 12:05:37 -05004585 cfq_shutdown_timer_wq(cfqd);
4586
Tejun Heoffea73f2012-06-04 10:02:29 +02004587#ifdef CONFIG_CFQ_GROUP_IOSCHED
4588 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4589#else
Tejun Heof51b8022012-03-05 13:15:05 -08004590 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004591#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004592 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
4594
Jianpeng Mad50235b2013-07-03 13:25:24 +02004595static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004598 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004599 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004600 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Jianpeng Mad50235b2013-07-03 13:25:24 +02004602 eq = elevator_alloc(q, e);
4603 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004604 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004605
Joe Perchesc1b511e2013-08-29 15:21:42 -07004606 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004607 if (!cfqd) {
4608 kobject_put(&eq->kobj);
4609 return -ENOMEM;
4610 }
4611 eq->elevator_data = cfqd;
4612
Tejun Heof51b8022012-03-05 13:15:05 -08004613 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004614 spin_lock_irq(q->queue_lock);
4615 q->elevator = eq;
4616 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004617
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004618 /* Init root service tree */
4619 cfqd->grp_service_tree = CFQ_RB_ROOT;
4620
Tejun Heof51b8022012-03-05 13:15:05 -08004621 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004622#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004623 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004624 if (ret)
4625 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004626
Tejun Heoa2b16932012-04-13 13:11:33 -07004627 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004628#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004629 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004630 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4631 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004632 if (!cfqd->root_group)
4633 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004634
Tejun Heoa2b16932012-04-13 13:11:33 -07004635 cfq_init_cfqg_base(cfqd->root_group);
Tejun Heo3ecca622015-08-18 14:55:35 -07004636 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4637 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
Tejun Heo69d7fde2015-08-18 14:55:36 -07004638#endif
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004639
Jens Axboe26a2ac02009-04-23 12:13:27 +02004640 /*
4641 * Not strictly needed (since RB_ROOT just clears the node and we
4642 * zeroed cfqd on alloc), but better be safe in case someone decides
4643 * to add magic to the rb code
4644 */
4645 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4646 cfqd->prio_trees[i] = RB_ROOT;
4647
Jens Axboe6118b702009-06-30 09:34:12 +02004648 /*
Tejun Heod4aad7f2015-08-18 14:55:04 -07004649 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
Jens Axboe6118b702009-06-30 09:34:12 +02004650 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004651 * will not attempt to free it. oom_cfqq is linked to root_group
4652 * but shouldn't hold a reference as it'll never be unlinked. Lose
4653 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004654 */
4655 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004656 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004657
4658 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004659 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004660 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004661 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Jan Kara91148322016-06-08 15:11:39 +02004663 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4664 HRTIMER_MODE_REL);
Jens Axboe22e2c502005-06-27 10:55:12 +02004665 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
Jens Axboe22e2c502005-06-27 10:55:12 +02004666
Jens Axboe23e018a2009-10-05 08:52:35 +02004667 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004670 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4671 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 cfqd->cfq_back_max = cfq_back_max;
4673 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004674 cfqd->cfq_slice[0] = cfq_slice_async;
4675 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004676 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004677 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004678 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004679 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004680 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004681 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004682 /*
4683 * we optimistically start assuming sync ops weren't delayed in last
4684 * second, in order to have larger depth for async operations.
4685 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004686 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004687 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004688
4689out_free:
4690 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004691 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
Jens Axboe0bb97942015-06-10 08:01:20 -06004695static void cfq_registered_queue(struct request_queue *q)
4696{
4697 struct elevator_queue *e = q->elevator;
4698 struct cfq_data *cfqd = e->elevator_data;
4699
4700 /*
4701 * Default to IOPS mode with no idling for SSDs
4702 */
4703 if (blk_queue_nonrot(q))
4704 cfqd->cfq_slice_idle = 0;
Jan Kara142bbdf2017-04-04 14:31:30 +02004705 wbt_disable_default(q);
Jens Axboe0bb97942015-06-10 08:01:20 -06004706}
4707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708/*
4709 * sysfs parts below -->
4710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711static ssize_t
4712cfq_var_show(unsigned int var, char *page)
4713{
Masanari Iida176167a2014-04-28 12:38:34 +09004714 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static ssize_t
4718cfq_var_store(unsigned int *var, const char *page, size_t count)
4719{
4720 char *p = (char *) page;
4721
4722 *var = simple_strtoul(p, &p, 10);
4723 return count;
4724}
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004727static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004729 struct cfq_data *cfqd = e->elevator_data; \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004730 u64 __data = __VAR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004732 __data = div_u64(__data, NSEC_PER_MSEC); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 return cfq_var_show(__data, (page)); \
4734}
4735SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004736SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4737SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004738SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4739SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004740SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004741SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004742SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4743SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4744SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004745SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004746SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747#undef SHOW_FUNCTION
4748
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004749#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4750static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4751{ \
4752 struct cfq_data *cfqd = e->elevator_data; \
4753 u64 __data = __VAR; \
4754 __data = div_u64(__data, NSEC_PER_USEC); \
4755 return cfq_var_show(__data, (page)); \
4756}
4757USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4758USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4759USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4760USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4761USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4762#undef USEC_SHOW_FUNCTION
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004765static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004767 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 unsigned int __data; \
4769 int ret = cfq_var_store(&__data, (page), count); \
4770 if (__data < (MIN)) \
4771 __data = (MIN); \
4772 else if (__data > (MAX)) \
4773 __data = (MAX); \
4774 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004775 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 else \
4777 *(__PTR) = __data; \
4778 return ret; \
4779}
4780STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004781STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4782 UINT_MAX, 1);
4783STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4784 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004785STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004786STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4787 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004788STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004789STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004790STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4791STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004792STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4793 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004794STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004795STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796#undef STORE_FUNCTION
4797
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004798#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4799static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4800{ \
4801 struct cfq_data *cfqd = e->elevator_data; \
4802 unsigned int __data; \
4803 int ret = cfq_var_store(&__data, (page), count); \
4804 if (__data < (MIN)) \
4805 __data = (MIN); \
4806 else if (__data > (MAX)) \
4807 __data = (MAX); \
4808 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
4809 return ret; \
4810}
4811USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4812USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4813USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4814USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4815USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4816#undef USEC_STORE_FUNCTION
4817
Al Viroe572ec72006-03-18 22:27:18 -05004818#define CFQ_ATTR(name) \
4819 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004820
Al Viroe572ec72006-03-18 22:27:18 -05004821static struct elv_fs_entry cfq_attrs[] = {
4822 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004823 CFQ_ATTR(fifo_expire_sync),
4824 CFQ_ATTR(fifo_expire_async),
4825 CFQ_ATTR(back_seek_max),
4826 CFQ_ATTR(back_seek_penalty),
4827 CFQ_ATTR(slice_sync),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004828 CFQ_ATTR(slice_sync_us),
Al Viroe572ec72006-03-18 22:27:18 -05004829 CFQ_ATTR(slice_async),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004830 CFQ_ATTR(slice_async_us),
Al Viroe572ec72006-03-18 22:27:18 -05004831 CFQ_ATTR(slice_async_rq),
4832 CFQ_ATTR(slice_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004833 CFQ_ATTR(slice_idle_us),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004834 CFQ_ATTR(group_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004835 CFQ_ATTR(group_idle_us),
Jens Axboe963b72f2009-10-03 19:42:18 +02004836 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004837 CFQ_ATTR(target_latency),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004838 CFQ_ATTR(target_latency_us),
Al Viroe572ec72006-03-18 22:27:18 -05004839 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840};
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842static struct elevator_type iosched_cfq = {
Jens Axboec51ca6c2016-12-10 15:13:59 -07004843 .ops.sq = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 .elevator_merge_fn = cfq_merge,
4845 .elevator_merged_fn = cfq_merged_request,
4846 .elevator_merge_req_fn = cfq_merged_requests,
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07004847 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4848 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004849 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004850 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004852 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004855 .elevator_former_req_fn = elv_rb_former_request,
4856 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004857 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004858 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 .elevator_set_req_fn = cfq_set_request,
4860 .elevator_put_req_fn = cfq_put_request,
4861 .elevator_may_queue_fn = cfq_may_queue,
4862 .elevator_init_fn = cfq_init_queue,
4863 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004864 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004866 .icq_size = sizeof(struct cfq_io_cq),
4867 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004868 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004869 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 .elevator_owner = THIS_MODULE,
4871};
4872
Vivek Goyal3e252062009-12-04 10:36:42 -05004873#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004874static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07004875 .dfl_cftypes = cfq_blkcg_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07004876 .legacy_cftypes = cfq_blkcg_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004877
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004878 .cpd_alloc_fn = cfq_cpd_alloc,
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004879 .cpd_init_fn = cfq_cpd_init,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004880 .cpd_free_fn = cfq_cpd_free,
Tejun Heo69d7fde2015-08-18 14:55:36 -07004881 .cpd_bind_fn = cfq_cpd_bind,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004882
Tejun Heo001bea72015-08-18 14:55:11 -07004883 .pd_alloc_fn = cfq_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004884 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004885 .pd_offline_fn = cfq_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07004886 .pd_free_fn = cfq_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004887 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004888};
Vivek Goyal3e252062009-12-04 10:36:42 -05004889#endif
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891static int __init cfq_init(void)
4892{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004893 int ret;
4894
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004895#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004896 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004897 if (ret)
4898 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004899#else
4900 cfq_group_idle = 0;
4901#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004902
Tejun Heofd794952012-06-04 10:01:38 +02004903 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004904 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4905 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004906 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Tejun Heo3d3c2372011-12-14 00:33:42 +01004908 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004909 if (ret)
4910 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004911
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004912 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004913
4914err_free_pool:
4915 kmem_cache_destroy(cfq_pool);
4916err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004917#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004918 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004919#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
4923static void __exit cfq_exit(void)
4924{
Tejun Heoffea73f2012-06-04 10:02:29 +02004925#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004926 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004929 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931
4932module_init(cfq_init);
4933module_exit(cfq_exit);
4934
4935MODULE_AUTHOR("Jens Axboe");
4936MODULE_LICENSE("GPL");
4937MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");