blob: 440b95ee593c97a81b749f02809dbc5e2ce42b72 [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/*
Jens Axboe08717142008-01-28 11:38:15 +010041 * offset from end of service tree
Jens Axboed9e76202007-04-20 14:27:50 +020042 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060043#define CFQ_IDLE_DELAY (NSEC_PER_SEC / 5)
Jens Axboed9e76202007-04-20 14:27:50 +020044
45/*
46 * below this threshold, we consider thinktime immediate
47 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060048#define CFQ_MIN_TT (2 * NSEC_PER_SEC / HZ)
Jens Axboed9e76202007-04-20 14:27:50 +020049
Jens Axboe22e2c502005-06-27 10:55:12 +020050#define CFQ_SLICE_SCALE (5)
Aaron Carroll45333d52008-08-26 15:52:36 +020051#define CFQ_HW_QUEUE_MIN (5)
Vivek Goyal25bc6b02009-12-03 12:59:43 -050052#define CFQ_SERVICE_SHIFT 12
Jens Axboe22e2c502005-06-27 10:55:12 +020053
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010054#define CFQQ_SEEK_THR (sector_t)(8 * 100)
Shaohua Lie9ce3352010-03-19 08:03:04 +010055#define CFQQ_CLOSE_THR (sector_t)(8 * 1024)
Corrado Zoccolo41647e72010-02-27 19:45:40 +010056#define CFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +010057#define CFQQ_SEEKY(cfqq) (hweight32(cfqq->seek_history) > 32/8)
Shaohua Liae54abe2010-02-05 13:11:45 +010058
Tejun Heoa612fdd2011-12-14 00:33:41 +010059#define RQ_CIC(rq) icq_to_cic((rq)->elv.icq)
60#define RQ_CFQQ(rq) (struct cfq_queue *) ((rq)->elv.priv[0])
61#define RQ_CFQG(rq) (struct cfq_group *) ((rq)->elv.priv[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Christoph Lametere18b8902006-12-06 20:33:20 -080063static struct kmem_cache *cfq_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jens Axboe22e2c502005-06-27 10:55:12 +020065#define CFQ_PRIO_LISTS IOPRIO_BE_NR
66#define cfq_class_idle(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
Jens Axboe22e2c502005-06-27 10:55:12 +020067#define cfq_class_rt(cfqq) ((cfqq)->ioprio_class == IOPRIO_CLASS_RT)
68
Jens Axboe206dc692006-03-28 13:03:44 +020069#define sample_valid(samples) ((samples) > 80)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050070#define rb_entry_cfqg(node) rb_entry((node), struct cfq_group, rb_node)
Jens Axboe206dc692006-03-28 13:03:44 +020071
Arianna Avanzinie48453c2015-06-05 23:38:42 +020072/* blkio-related constants */
Tejun Heo3ecca622015-08-18 14:55:35 -070073#define CFQ_WEIGHT_LEGACY_MIN 10
74#define CFQ_WEIGHT_LEGACY_DFL 500
75#define CFQ_WEIGHT_LEGACY_MAX 1000
Arianna Avanzinie48453c2015-06-05 23:38:42 +020076
Tejun Heoc5869802011-12-14 00:33:41 +010077struct cfq_ttime {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060078 u64 last_end_request;
Tejun Heoc5869802011-12-14 00:33:41 +010079
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060080 u64 ttime_total;
81 u64 ttime_mean;
Tejun Heoc5869802011-12-14 00:33:41 +010082 unsigned long ttime_samples;
Tejun Heoc5869802011-12-14 00:33:41 +010083};
84
Jens Axboe22e2c502005-06-27 10:55:12 +020085/*
Jens Axboecc09e292007-04-26 12:53:50 +020086 * Most of our rbtree usage is for sorting with min extraction, so
87 * if we cache the leftmost node we don't have to walk down the tree
88 * to find it. Idea borrowed from Ingo Molnars CFS scheduler. We should
89 * move this into the elevator for the rq sorting as well.
90 */
91struct cfq_rb_root {
92 struct rb_root rb;
93 struct rb_node *left;
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +010094 unsigned count;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -050095 u64 min_vdisktime;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020096 struct cfq_ttime ttime;
Jens Axboecc09e292007-04-26 12:53:50 +020097};
Shaohua Lif5f2b6c2011-07-12 14:24:55 +020098#define CFQ_RB_ROOT (struct cfq_rb_root) { .rb = RB_ROOT, \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -060099 .ttime = {.last_end_request = ktime_get_ns(),},}
Jens Axboecc09e292007-04-26 12:53:50 +0200100
101/*
Jens Axboe6118b702009-06-30 09:34:12 +0200102 * Per process-grouping structure
103 */
104struct cfq_queue {
105 /* reference count */
Shaohua Li30d7b942011-01-07 08:46:59 +0100106 int ref;
Jens Axboe6118b702009-06-30 09:34:12 +0200107 /* various state flags, see below */
108 unsigned int flags;
109 /* parent cfq_data */
110 struct cfq_data *cfqd;
111 /* service_tree member */
112 struct rb_node rb_node;
113 /* service_tree key */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600114 u64 rb_key;
Jens Axboe6118b702009-06-30 09:34:12 +0200115 /* prio tree member */
116 struct rb_node p_node;
117 /* prio tree root we belong to, if any */
118 struct rb_root *p_root;
119 /* sorted list of pending requests */
120 struct rb_root sort_list;
121 /* if fifo isn't expired, next request to serve */
122 struct request *next_rq;
123 /* requests queued in sort_list */
124 int queued[2];
125 /* currently allocated requests */
126 int allocated[2];
127 /* fifo list of requests in sort_list */
128 struct list_head fifo;
129
Vivek Goyaldae739e2009-12-03 12:59:45 -0500130 /* time when queue got scheduled in to dispatch first request. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600131 u64 dispatch_start;
132 u64 allocated_slice;
133 u64 slice_dispatch;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500134 /* time when first request from queue completed and slice started. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600135 u64 slice_start;
136 u64 slice_end;
Jan Kara93fdf142016-06-28 09:04:00 +0200137 s64 slice_resid;
Jens Axboe6118b702009-06-30 09:34:12 +0200138
Christoph Hellwig65299a32011-08-23 14:50:29 +0200139 /* pending priority requests */
140 int prio_pending;
Jens Axboe6118b702009-06-30 09:34:12 +0200141 /* number of requests that are on the dispatch list or inside driver */
142 int dispatched;
143
144 /* io prio of this group */
145 unsigned short ioprio, org_ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -0600146 unsigned short ioprio_class, org_ioprio_class;
Jens Axboe6118b702009-06-30 09:34:12 +0200147
Richard Kennedyc4081ba2010-02-22 13:49:24 +0100148 pid_t pid;
149
Corrado Zoccolo3dde36d2010-02-27 19:45:39 +0100150 u32 seek_history;
Jeff Moyerb2c18e12009-10-23 17:14:49 -0400151 sector_t last_request_pos;
152
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +0100153 struct cfq_rb_root *service_tree;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -0400154 struct cfq_queue *new_cfqq;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500155 struct cfq_group *cfqg;
Vivek Goyalc4e78932010-08-23 12:25:03 +0200156 /* Number of sectors dispatched from queue in single dispatch round */
157 unsigned long nr_sectors;
Jens Axboe6118b702009-06-30 09:34:12 +0200158};
159
160/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100161 * First index in the service_trees.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100162 * IDLE is handled separately, so it has negative index
163 */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400164enum wl_class_t {
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100165 BE_WORKLOAD = 0,
Vivek Goyal615f0252009-12-03 12:59:39 -0500166 RT_WORKLOAD = 1,
167 IDLE_WORKLOAD = 2,
Vivek Goyalb4627322010-10-22 09:48:43 +0200168 CFQ_PRIO_NR,
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100169};
170
171/*
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100172 * Second index in the service_trees.
173 */
174enum wl_type_t {
175 ASYNC_WORKLOAD = 0,
176 SYNC_NOIDLE_WORKLOAD = 1,
177 SYNC_WORKLOAD = 2
178};
179
Tejun Heo155fead2012-04-01 14:38:44 -0700180struct cfqg_stats {
181#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo155fead2012-04-01 14:38:44 -0700182 /* number of ios merged */
183 struct blkg_rwstat merged;
184 /* total time spent on device in ns, may not be accurate w/ queueing */
185 struct blkg_rwstat service_time;
186 /* total time spent waiting in scheduler queue in ns */
187 struct blkg_rwstat wait_time;
188 /* number of IOs queued up */
189 struct blkg_rwstat queued;
Tejun Heo155fead2012-04-01 14:38:44 -0700190 /* total disk time and nr sectors dispatched by this group */
191 struct blkg_stat time;
192#ifdef CONFIG_DEBUG_BLK_CGROUP
193 /* time not charged to this cgroup */
194 struct blkg_stat unaccounted_time;
195 /* sum of number of ios queued across all samples */
196 struct blkg_stat avg_queue_size_sum;
197 /* count of samples taken for average */
198 struct blkg_stat avg_queue_size_samples;
199 /* how many times this group has been removed from service tree */
200 struct blkg_stat dequeue;
201 /* total time spent waiting for it to be assigned a timeslice. */
202 struct blkg_stat group_wait_time;
Tejun Heo3c798392012-04-16 13:57:25 -0700203 /* time spent idling for this blkcg_gq */
Tejun Heo155fead2012-04-01 14:38:44 -0700204 struct blkg_stat idle_time;
205 /* total time with empty current active q with other requests queued */
206 struct blkg_stat empty_time;
207 /* fields after this shouldn't be cleared on stat reset */
208 uint64_t start_group_wait_time;
209 uint64_t start_idle_time;
210 uint64_t start_empty_time;
211 uint16_t flags;
212#endif /* CONFIG_DEBUG_BLK_CGROUP */
213#endif /* CONFIG_CFQ_GROUP_IOSCHED */
214};
215
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200216/* Per-cgroup data */
217struct cfq_group_data {
218 /* must be the first member */
Tejun Heo81437642015-08-18 14:55:15 -0700219 struct blkcg_policy_data cpd;
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200220
221 unsigned int weight;
222 unsigned int leaf_weight;
223};
224
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500225/* This is per cgroup per device grouping structure */
226struct cfq_group {
Tejun Heof95a04a2012-04-16 13:57:26 -0700227 /* must be the first member */
228 struct blkg_policy_data pd;
229
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500230 /* group service_tree member */
231 struct rb_node rb_node;
232
233 /* group service_tree key */
234 u64 vdisktime;
Tejun Heoe71357e2013-01-09 08:05:10 -0800235
236 /*
Tejun Heo7918ffb2013-01-09 08:05:11 -0800237 * The number of active cfqgs and sum of their weights under this
238 * cfqg. This covers this cfqg's leaf_weight and all children's
239 * weights, but does not cover weights of further descendants.
240 *
241 * If a cfqg is on the service tree, it's active. An active cfqg
242 * also activates its parent and contributes to the children_weight
243 * of the parent.
244 */
245 int nr_active;
246 unsigned int children_weight;
247
248 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -0800249 * vfraction is the fraction of vdisktime that the tasks in this
250 * cfqg are entitled to. This is determined by compounding the
251 * ratios walking up from this cfqg to the root.
252 *
253 * It is in fixed point w/ CFQ_SERVICE_SHIFT and the sum of all
254 * vfractions on a service tree is approximately 1. The sum may
255 * deviate a bit due to rounding errors and fluctuations caused by
256 * cfqgs entering and leaving the service tree.
257 */
258 unsigned int vfraction;
259
260 /*
Tejun Heoe71357e2013-01-09 08:05:10 -0800261 * There are two weights - (internal) weight is the weight of this
262 * cfqg against the sibling cfqgs. leaf_weight is the wight of
263 * this cfqg against the child cfqgs. For the root cfqg, both
264 * weights are kept in sync for backward compatibility.
265 */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500266 unsigned int weight;
Justin TerAvest8184f932011-03-17 16:12:36 +0100267 unsigned int new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -0700268 unsigned int dev_weight;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500269
Tejun Heoe71357e2013-01-09 08:05:10 -0800270 unsigned int leaf_weight;
271 unsigned int new_leaf_weight;
272 unsigned int dev_leaf_weight;
273
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500274 /* number of cfqq currently on this group */
275 int nr_cfqq;
276
Jens Axboe22e2c502005-06-27 10:55:12 +0200277 /*
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200278 * Per group busy queues average. Useful for workload slice calc. We
Vivek Goyalb4627322010-10-22 09:48:43 +0200279 * create the array for each prio class but at run time it is used
280 * only for RT and BE class and slot for IDLE class remains unused.
281 * This is primarily done to avoid confusion and a gcc warning.
282 */
283 unsigned int busy_queues_avg[CFQ_PRIO_NR];
284 /*
285 * rr lists of queues with requests. We maintain service trees for
286 * RT and BE classes. These trees are subdivided in subclasses
287 * of SYNC, SYNC_NOIDLE and ASYNC based on workload type. For IDLE
288 * class there is no subclassification and all the cfq queues go on
289 * a single tree service_tree_idle.
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100290 * Counts are embedded in the cfq_rb_root
Jens Axboe22e2c502005-06-27 10:55:12 +0200291 */
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100292 struct cfq_rb_root service_trees[2][3];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100293 struct cfq_rb_root service_tree_idle;
Vivek Goyaldae739e2009-12-03 12:59:45 -0500294
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600295 u64 saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400296 enum wl_type_t saved_wl_type;
297 enum wl_class_t saved_wl_class;
Tejun Heo4eef3042012-03-05 13:15:18 -0800298
Vivek Goyal80bdf0c2010-08-23 12:24:26 +0200299 /* number of requests that are on the dispatch list or inside driver */
300 int dispatched;
Shaohua Li7700fc42011-07-12 14:24:56 +0200301 struct cfq_ttime ttime;
Tejun Heo0b399202013-01-09 08:05:13 -0800302 struct cfqg_stats stats; /* stats for this cfqg */
Tejun Heo60a83702015-08-18 14:55:05 -0700303
304 /* async queue for each priority case */
305 struct cfq_queue *async_cfqq[2][IOPRIO_BE_NR];
306 struct cfq_queue *async_idle_cfqq;
307
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500308};
309
Tejun Heoc5869802011-12-14 00:33:41 +0100310struct cfq_io_cq {
311 struct io_cq icq; /* must be the first member */
312 struct cfq_queue *cfqq[2];
313 struct cfq_ttime ttime;
Tejun Heo598971b2012-03-19 15:10:58 -0700314 int ioprio; /* the current ioprio */
315#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heof4da8072014-09-08 08:15:20 +0900316 uint64_t blkcg_serial_nr; /* the current blkcg serial */
Tejun Heo598971b2012-03-19 15:10:58 -0700317#endif
Tejun Heoc5869802011-12-14 00:33:41 +0100318};
319
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500320/*
321 * Per block device queue structure
322 */
323struct cfq_data {
324 struct request_queue *queue;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500325 /* Root service tree for cfq_groups */
326 struct cfq_rb_root grp_service_tree;
Tejun Heof51b8022012-03-05 13:15:05 -0800327 struct cfq_group *root_group;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500328
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100329 /*
330 * The priority currently being served
331 */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -0400332 enum wl_class_t serving_wl_class;
333 enum wl_type_t serving_wl_type;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600334 u64 workload_expires;
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500335 struct cfq_group *serving_group;
Jens Axboea36e71f2009-04-15 12:15:11 +0200336
337 /*
338 * Each priority tree is sorted by next_request position. These
339 * trees are used when determining if two or more queues are
340 * interleaving requests (see cfq_close_cooperator).
341 */
342 struct rb_root prio_trees[CFQ_PRIO_LISTS];
343
Jens Axboe22e2c502005-06-27 10:55:12 +0200344 unsigned int busy_queues;
Shaohua Lief8a41d2011-03-07 09:26:29 +0100345 unsigned int busy_sync_queues;
Jens Axboe22e2c502005-06-27 10:55:12 +0200346
Corrado Zoccolo53c583d2010-02-28 19:45:05 +0100347 int rq_in_driver;
348 int rq_in_flight[2];
Aaron Carroll45333d52008-08-26 15:52:36 +0200349
350 /*
351 * queue-depth detection
352 */
353 int rq_queued;
Jens Axboe25776e32006-06-01 10:12:26 +0200354 int hw_tag;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +0100355 /*
356 * hw_tag can be
357 * -1 => indeterminate, (cfq will behave as if NCQ is present, to allow better detection)
358 * 1 => NCQ is present (hw_tag_est_depth is the estimated max depth)
359 * 0 => no NCQ
360 */
361 int hw_tag_est_depth;
362 unsigned int hw_tag_samples;
Jens Axboe22e2c502005-06-27 10:55:12 +0200363
364 /*
Jens Axboe22e2c502005-06-27 10:55:12 +0200365 * idle window management
366 */
Jan Kara91148322016-06-08 15:11:39 +0200367 struct hrtimer idle_slice_timer;
Jens Axboe23e018a2009-10-05 08:52:35 +0200368 struct work_struct unplug_work;
Jens Axboe22e2c502005-06-27 10:55:12 +0200369
370 struct cfq_queue *active_queue;
Tejun Heoc5869802011-12-14 00:33:41 +0100371 struct cfq_io_cq *active_cic;
Jens Axboe22e2c502005-06-27 10:55:12 +0200372
Jens Axboe6d048f52007-04-25 12:44:27 +0200373 sector_t last_position;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * tunables, see top of file
377 */
378 unsigned int cfq_quantum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned int cfq_back_penalty;
380 unsigned int cfq_back_max;
Jens Axboe22e2c502005-06-27 10:55:12 +0200381 unsigned int cfq_slice_async_rq;
Jens Axboe963b72f2009-10-03 19:42:18 +0200382 unsigned int cfq_latency;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600383 u64 cfq_fifo_expire[2];
384 u64 cfq_slice[2];
385 u64 cfq_slice_idle;
386 u64 cfq_group_idle;
387 u64 cfq_target_latency;
Al Virod9ff4182006-03-18 13:51:22 -0500388
Jens Axboe6118b702009-06-30 09:34:12 +0200389 /*
390 * Fallback dummy cfqq for extreme OOM conditions
391 */
392 struct cfq_queue oom_cfqq;
Vivek Goyal365722b2009-10-03 15:21:27 +0200393
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600394 u64 last_delayed_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Vivek Goyal25fb5162009-12-03 12:59:46 -0500397static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd);
Tejun Heo60a83702015-08-18 14:55:05 -0700398static void cfq_put_queue(struct cfq_queue *cfqq);
Vivek Goyal25fb5162009-12-03 12:59:46 -0500399
Vivek Goyal34b98d02012-10-03 16:56:58 -0400400static struct cfq_rb_root *st_for(struct cfq_group *cfqg,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400401 enum wl_class_t class,
Vivek Goyal65b32a52009-12-16 17:52:59 -0500402 enum wl_type_t type)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100403{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -0500404 if (!cfqg)
405 return NULL;
406
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400407 if (class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500408 return &cfqg->service_tree_idle;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100409
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400410 return &cfqg->service_trees[class][type];
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100411}
412
Jens Axboe3b181522005-06-27 10:56:24 +0200413enum cfqq_state_flags {
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100414 CFQ_CFQQ_FLAG_on_rr = 0, /* on round-robin busy list */
415 CFQ_CFQQ_FLAG_wait_request, /* waiting for a request */
Jens Axboeb0291952009-04-07 11:38:31 +0200416 CFQ_CFQQ_FLAG_must_dispatch, /* must be allowed a dispatch */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100417 CFQ_CFQQ_FLAG_must_alloc_slice, /* per-slice must_alloc flag */
Jens Axboeb0b8d7492007-01-19 11:35:30 +1100418 CFQ_CFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
419 CFQ_CFQQ_FLAG_idle_window, /* slice idling enabled */
420 CFQ_CFQQ_FLAG_prio_changed, /* task priority has changed */
Jens Axboe44f7c162007-01-19 11:51:58 +1100421 CFQ_CFQQ_FLAG_slice_new, /* no requests dispatched in slice */
Vasily Tarasov91fac312007-04-25 12:29:51 +0200422 CFQ_CFQQ_FLAG_sync, /* synchronous queue */
Jeff Moyerb3b6d042009-10-23 17:14:51 -0400423 CFQ_CFQQ_FLAG_coop, /* cfqq is shared */
Shaohua Liae54abe2010-02-05 13:11:45 +0100424 CFQ_CFQQ_FLAG_split_coop, /* shared cfqq will be splitted */
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100425 CFQ_CFQQ_FLAG_deep, /* sync cfqq experienced large depth */
Vivek Goyalf75edf22009-12-03 12:59:53 -0500426 CFQ_CFQQ_FLAG_wait_busy, /* Waiting for next request */
Jens Axboe3b181522005-06-27 10:56:24 +0200427};
428
429#define CFQ_CFQQ_FNS(name) \
430static inline void cfq_mark_cfqq_##name(struct cfq_queue *cfqq) \
431{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100432 (cfqq)->flags |= (1 << CFQ_CFQQ_FLAG_##name); \
Jens Axboe3b181522005-06-27 10:56:24 +0200433} \
434static inline void cfq_clear_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 int cfq_cfqq_##name(const struct cfq_queue *cfqq) \
439{ \
Jens Axboefe094d92008-01-31 13:08:54 +0100440 return ((cfqq)->flags & (1 << CFQ_CFQQ_FLAG_##name)) != 0; \
Jens Axboe3b181522005-06-27 10:56:24 +0200441}
442
443CFQ_CFQQ_FNS(on_rr);
444CFQ_CFQQ_FNS(wait_request);
Jens Axboeb0291952009-04-07 11:38:31 +0200445CFQ_CFQQ_FNS(must_dispatch);
Jens Axboe3b181522005-06-27 10:56:24 +0200446CFQ_CFQQ_FNS(must_alloc_slice);
Jens Axboe3b181522005-06-27 10:56:24 +0200447CFQ_CFQQ_FNS(fifo_expire);
448CFQ_CFQQ_FNS(idle_window);
449CFQ_CFQQ_FNS(prio_changed);
Jens Axboe44f7c162007-01-19 11:51:58 +1100450CFQ_CFQQ_FNS(slice_new);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200451CFQ_CFQQ_FNS(sync);
Jens Axboea36e71f2009-04-15 12:15:11 +0200452CFQ_CFQQ_FNS(coop);
Shaohua Liae54abe2010-02-05 13:11:45 +0100453CFQ_CFQQ_FNS(split_coop);
Corrado Zoccolo76280af2009-11-26 10:02:58 +0100454CFQ_CFQQ_FNS(deep);
Vivek Goyalf75edf22009-12-03 12:59:53 -0500455CFQ_CFQQ_FNS(wait_busy);
Jens Axboe3b181522005-06-27 10:56:24 +0200456#undef CFQ_CFQQ_FNS
457
Tejun Heo629ed0b2012-04-01 14:38:44 -0700458#if defined(CONFIG_CFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700459
Tejun Heo155fead2012-04-01 14:38:44 -0700460/* cfqg stats flags */
461enum cfqg_stats_flags {
462 CFQG_stats_waiting = 0,
463 CFQG_stats_idling,
464 CFQG_stats_empty,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700465};
466
Tejun Heo155fead2012-04-01 14:38:44 -0700467#define CFQG_FLAG_FNS(name) \
468static inline void cfqg_stats_mark_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700469{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700470 stats->flags |= (1 << CFQG_stats_##name); \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700471} \
Tejun Heo155fead2012-04-01 14:38:44 -0700472static inline void cfqg_stats_clear_##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 int cfqg_stats_##name(struct cfqg_stats *stats) \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700477{ \
Tejun Heo155fead2012-04-01 14:38:44 -0700478 return (stats->flags & (1 << CFQG_stats_##name)) != 0; \
Tejun Heo629ed0b2012-04-01 14:38:44 -0700479} \
480
Tejun Heo155fead2012-04-01 14:38:44 -0700481CFQG_FLAG_FNS(waiting)
482CFQG_FLAG_FNS(idling)
483CFQG_FLAG_FNS(empty)
484#undef CFQG_FLAG_FNS
Tejun Heo629ed0b2012-04-01 14:38:44 -0700485
486/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700487static void cfqg_stats_update_group_wait_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700488{
489 unsigned long long now;
490
Tejun Heo155fead2012-04-01 14:38:44 -0700491 if (!cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700492 return;
493
494 now = sched_clock();
495 if (time_after64(now, stats->start_group_wait_time))
496 blkg_stat_add(&stats->group_wait_time,
497 now - stats->start_group_wait_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700498 cfqg_stats_clear_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700499}
500
501/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700502static void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg,
503 struct cfq_group *curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700504{
Tejun Heo155fead2012-04-01 14:38:44 -0700505 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700506
Tejun Heo155fead2012-04-01 14:38:44 -0700507 if (cfqg_stats_waiting(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700508 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700509 if (cfqg == curr_cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700510 return;
Tejun Heo155fead2012-04-01 14:38:44 -0700511 stats->start_group_wait_time = sched_clock();
512 cfqg_stats_mark_waiting(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700513}
514
515/* This should be called with the queue_lock held. */
Tejun Heo155fead2012-04-01 14:38:44 -0700516static void cfqg_stats_end_empty_time(struct cfqg_stats *stats)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700517{
518 unsigned long long now;
519
Tejun Heo155fead2012-04-01 14:38:44 -0700520 if (!cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700521 return;
522
523 now = sched_clock();
524 if (time_after64(now, stats->start_empty_time))
525 blkg_stat_add(&stats->empty_time,
526 now - stats->start_empty_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700527 cfqg_stats_clear_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700528}
529
Tejun Heo155fead2012-04-01 14:38:44 -0700530static void cfqg_stats_update_dequeue(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700531{
Tejun Heo155fead2012-04-01 14:38:44 -0700532 blkg_stat_add(&cfqg->stats.dequeue, 1);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700533}
534
Tejun Heo155fead2012-04-01 14:38:44 -0700535static void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700536{
Tejun Heo155fead2012-04-01 14:38:44 -0700537 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700538
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800539 if (blkg_rwstat_total(&stats->queued))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700540 return;
541
542 /*
543 * group is already marked empty. This can happen if cfqq got new
544 * request in parent group and moved to this group while being added
545 * to service tree. Just ignore the event and move on.
546 */
Tejun Heo155fead2012-04-01 14:38:44 -0700547 if (cfqg_stats_empty(stats))
Tejun Heo629ed0b2012-04-01 14:38:44 -0700548 return;
549
550 stats->start_empty_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700551 cfqg_stats_mark_empty(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700552}
553
Tejun Heo155fead2012-04-01 14:38:44 -0700554static void cfqg_stats_update_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700555{
Tejun Heo155fead2012-04-01 14:38:44 -0700556 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700557
Tejun Heo155fead2012-04-01 14:38:44 -0700558 if (cfqg_stats_idling(stats)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -0700559 unsigned long long now = sched_clock();
560
561 if (time_after64(now, stats->start_idle_time))
562 blkg_stat_add(&stats->idle_time,
563 now - stats->start_idle_time);
Tejun Heo155fead2012-04-01 14:38:44 -0700564 cfqg_stats_clear_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700565 }
566}
567
Tejun Heo155fead2012-04-01 14:38:44 -0700568static void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700569{
Tejun Heo155fead2012-04-01 14:38:44 -0700570 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700571
Tejun Heo155fead2012-04-01 14:38:44 -0700572 BUG_ON(cfqg_stats_idling(stats));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700573
574 stats->start_idle_time = sched_clock();
Tejun Heo155fead2012-04-01 14:38:44 -0700575 cfqg_stats_mark_idling(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700576}
577
Tejun Heo155fead2012-04-01 14:38:44 -0700578static void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700579{
Tejun Heo155fead2012-04-01 14:38:44 -0700580 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700581
582 blkg_stat_add(&stats->avg_queue_size_sum,
Tejun Heo4d5e80a2013-01-09 08:05:12 -0800583 blkg_rwstat_total(&stats->queued));
Tejun Heo629ed0b2012-04-01 14:38:44 -0700584 blkg_stat_add(&stats->avg_queue_size_samples, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700585 cfqg_stats_update_group_wait_time(stats);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700586}
587
588#else /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
589
Tejun Heof48ec1d2012-04-13 13:11:25 -0700590static inline void cfqg_stats_set_start_group_wait_time(struct cfq_group *cfqg, struct cfq_group *curr_cfqg) { }
591static inline void cfqg_stats_end_empty_time(struct cfqg_stats *stats) { }
592static inline void cfqg_stats_update_dequeue(struct cfq_group *cfqg) { }
593static inline void cfqg_stats_set_start_empty_time(struct cfq_group *cfqg) { }
594static inline void cfqg_stats_update_idle_time(struct cfq_group *cfqg) { }
595static inline void cfqg_stats_set_start_idle_time(struct cfq_group *cfqg) { }
596static inline void cfqg_stats_update_avg_queue_size(struct cfq_group *cfqg) { }
Tejun Heo629ed0b2012-04-01 14:38:44 -0700597
598#endif /* CONFIG_CFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
599
600#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo2ce4d502012-04-01 14:38:43 -0700601
Jens Axboe4ceab712015-06-19 10:13:01 -0600602static inline struct cfq_group *pd_to_cfqg(struct blkg_policy_data *pd)
603{
604 return pd ? container_of(pd, struct cfq_group, pd) : NULL;
605}
606
607static struct cfq_group_data
608*cpd_to_cfqgd(struct blkcg_policy_data *cpd)
609{
Tejun Heo81437642015-08-18 14:55:15 -0700610 return cpd ? container_of(cpd, struct cfq_group_data, cpd) : NULL;
Jens Axboe4ceab712015-06-19 10:13:01 -0600611}
612
613static inline struct blkcg_gq *cfqg_to_blkg(struct cfq_group *cfqg)
614{
615 return pd_to_blkg(&cfqg->pd);
616}
617
Tejun Heoffea73f2012-06-04 10:02:29 +0200618static struct blkcg_policy blkcg_policy_cfq;
619
620static inline struct cfq_group *blkg_to_cfqg(struct blkcg_gq *blkg)
621{
622 return pd_to_cfqg(blkg_to_pd(blkg, &blkcg_policy_cfq));
623}
624
Arianna Avanzinie48453c2015-06-05 23:38:42 +0200625static struct cfq_group_data *blkcg_to_cfqgd(struct blkcg *blkcg)
626{
627 return cpd_to_cfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_cfq));
628}
629
Tejun Heod02f7aa2013-01-09 08:05:11 -0800630static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg)
Tejun Heo7918ffb2013-01-09 08:05:11 -0800631{
Tejun Heod02f7aa2013-01-09 08:05:11 -0800632 struct blkcg_gq *pblkg = cfqg_to_blkg(cfqg)->parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800633
Tejun Heod02f7aa2013-01-09 08:05:11 -0800634 return pblkg ? blkg_to_cfqg(pblkg) : NULL;
Tejun Heo7918ffb2013-01-09 08:05:11 -0800635}
636
Jan Kara3984aa52016-01-12 16:24:19 +0100637static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
638 struct cfq_group *ancestor)
639{
640 return cgroup_is_descendant(cfqg_to_blkg(cfqg)->blkcg->css.cgroup,
641 cfqg_to_blkg(ancestor)->blkcg->css.cgroup);
642}
643
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100644static inline void cfqg_get(struct cfq_group *cfqg)
645{
646 return blkg_get(cfqg_to_blkg(cfqg));
647}
648
649static inline void cfqg_put(struct cfq_group *cfqg)
650{
651 return blkg_put(cfqg_to_blkg(cfqg));
652}
653
Tejun Heo54e7ed12012-04-16 13:57:23 -0700654#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) do { \
655 char __pbuf[128]; \
656 \
657 blkg_path(cfqg_to_blkg((cfqq)->cfqg), __pbuf, sizeof(__pbuf)); \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400658 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c %s " fmt, (cfqq)->pid, \
659 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
660 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
Tejun Heo54e7ed12012-04-16 13:57:23 -0700661 __pbuf, ##args); \
662} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500663
Tejun Heo54e7ed12012-04-16 13:57:23 -0700664#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do { \
665 char __pbuf[128]; \
666 \
667 blkg_path(cfqg_to_blkg(cfqg), __pbuf, sizeof(__pbuf)); \
668 blk_add_trace_msg((cfqd)->queue, "%s " fmt, __pbuf, ##args); \
669} while (0)
Vivek Goyal2868ef72009-12-03 12:59:48 -0500670
Tejun Heo155fead2012-04-01 14:38:44 -0700671static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600672 struct cfq_group *curr_cfqg,
673 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700674{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600675 blkg_rwstat_add(&cfqg->stats.queued, op, 1);
Tejun Heo155fead2012-04-01 14:38:44 -0700676 cfqg_stats_end_empty_time(&cfqg->stats);
677 cfqg_stats_set_start_group_wait_time(cfqg, curr_cfqg);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700678}
679
Tejun Heo155fead2012-04-01 14:38:44 -0700680static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600681 uint64_t time, unsigned long unaccounted_time)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700682{
Tejun Heo155fead2012-04-01 14:38:44 -0700683 blkg_stat_add(&cfqg->stats.time, time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700684#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo155fead2012-04-01 14:38:44 -0700685 blkg_stat_add(&cfqg->stats.unaccounted_time, unaccounted_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700686#endif
Tejun Heo2ce4d502012-04-01 14:38:43 -0700687}
688
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600689static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
690 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700691{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600692 blkg_rwstat_add(&cfqg->stats.queued, op, -1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700693}
694
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600695static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
696 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700697{
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600698 blkg_rwstat_add(&cfqg->stats.merged, op, 1);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700699}
700
Tejun Heo155fead2012-04-01 14:38:44 -0700701static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600702 uint64_t start_time, uint64_t io_start_time,
703 unsigned int op)
Tejun Heo2ce4d502012-04-01 14:38:43 -0700704{
Tejun Heo155fead2012-04-01 14:38:44 -0700705 struct cfqg_stats *stats = &cfqg->stats;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700706 unsigned long long now = sched_clock();
Tejun Heo629ed0b2012-04-01 14:38:44 -0700707
708 if (time_after64(now, io_start_time))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600709 blkg_rwstat_add(&stats->service_time, op, now - io_start_time);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700710 if (time_after64(io_start_time, start_time))
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600711 blkg_rwstat_add(&stats->wait_time, op,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700712 io_start_time - start_time);
Tejun Heo2ce4d502012-04-01 14:38:43 -0700713}
714
Tejun Heo689665a2013-01-09 08:05:13 -0800715/* @stats = 0 */
716static void cfqg_stats_reset(struct cfqg_stats *stats)
Tejun Heo155fead2012-04-01 14:38:44 -0700717{
Tejun Heo155fead2012-04-01 14:38:44 -0700718 /* queued stats shouldn't be cleared */
Tejun Heo155fead2012-04-01 14:38:44 -0700719 blkg_rwstat_reset(&stats->merged);
720 blkg_rwstat_reset(&stats->service_time);
721 blkg_rwstat_reset(&stats->wait_time);
722 blkg_stat_reset(&stats->time);
723#ifdef CONFIG_DEBUG_BLK_CGROUP
724 blkg_stat_reset(&stats->unaccounted_time);
725 blkg_stat_reset(&stats->avg_queue_size_sum);
726 blkg_stat_reset(&stats->avg_queue_size_samples);
727 blkg_stat_reset(&stats->dequeue);
728 blkg_stat_reset(&stats->group_wait_time);
729 blkg_stat_reset(&stats->idle_time);
730 blkg_stat_reset(&stats->empty_time);
731#endif
732}
733
Tejun Heo0b399202013-01-09 08:05:13 -0800734/* @to += @from */
Tejun Heoe6269c42015-08-18 14:55:21 -0700735static void cfqg_stats_add_aux(struct cfqg_stats *to, struct cfqg_stats *from)
Tejun Heo0b399202013-01-09 08:05:13 -0800736{
737 /* queued stats shouldn't be cleared */
Tejun Heoe6269c42015-08-18 14:55:21 -0700738 blkg_rwstat_add_aux(&to->merged, &from->merged);
739 blkg_rwstat_add_aux(&to->service_time, &from->service_time);
740 blkg_rwstat_add_aux(&to->wait_time, &from->wait_time);
741 blkg_stat_add_aux(&from->time, &from->time);
Tejun Heo0b399202013-01-09 08:05:13 -0800742#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heoe6269c42015-08-18 14:55:21 -0700743 blkg_stat_add_aux(&to->unaccounted_time, &from->unaccounted_time);
744 blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum);
745 blkg_stat_add_aux(&to->avg_queue_size_samples, &from->avg_queue_size_samples);
746 blkg_stat_add_aux(&to->dequeue, &from->dequeue);
747 blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time);
748 blkg_stat_add_aux(&to->idle_time, &from->idle_time);
749 blkg_stat_add_aux(&to->empty_time, &from->empty_time);
Tejun Heo0b399202013-01-09 08:05:13 -0800750#endif
751}
752
753/*
Tejun Heoe6269c42015-08-18 14:55:21 -0700754 * Transfer @cfqg's stats to its parent's aux counts so that the ancestors'
Tejun Heo0b399202013-01-09 08:05:13 -0800755 * recursive stats can still account for the amount used by this cfqg after
756 * it's gone.
757 */
758static void cfqg_stats_xfer_dead(struct cfq_group *cfqg)
759{
760 struct cfq_group *parent = cfqg_parent(cfqg);
761
762 lockdep_assert_held(cfqg_to_blkg(cfqg)->q->queue_lock);
763
764 if (unlikely(!parent))
765 return;
766
Tejun Heoe6269c42015-08-18 14:55:21 -0700767 cfqg_stats_add_aux(&parent->stats, &cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800768 cfqg_stats_reset(&cfqg->stats);
Tejun Heo0b399202013-01-09 08:05:13 -0800769}
770
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100771#else /* CONFIG_CFQ_GROUP_IOSCHED */
772
Tejun Heod02f7aa2013-01-09 08:05:11 -0800773static inline struct cfq_group *cfqg_parent(struct cfq_group *cfqg) { return NULL; }
Jan Kara3984aa52016-01-12 16:24:19 +0100774static inline bool cfqg_is_descendant(struct cfq_group *cfqg,
775 struct cfq_group *ancestor)
776{
777 return true;
778}
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100779static inline void cfqg_get(struct cfq_group *cfqg) { }
780static inline void cfqg_put(struct cfq_group *cfqg) { }
781
Jens Axboe7b679132008-05-30 12:23:07 +0200782#define cfq_log_cfqq(cfqd, cfqq, fmt, args...) \
Vivek Goyalb226e5c2012-10-03 16:57:01 -0400783 blk_add_trace_msg((cfqd)->queue, "cfq%d%c%c " fmt, (cfqq)->pid, \
784 cfq_cfqq_sync((cfqq)) ? 'S' : 'A', \
785 cfqq_type((cfqq)) == SYNC_NOIDLE_WORKLOAD ? 'N' : ' ',\
786 ##args)
Kyungmin Park4495a7d2011-05-31 10:04:09 +0200787#define cfq_log_cfqg(cfqd, cfqg, fmt, args...) do {} while (0)
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100788
Tejun Heo155fead2012-04-01 14:38:44 -0700789static inline void cfqg_stats_update_io_add(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600790 struct cfq_group *curr_cfqg, unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700791static inline void cfqg_stats_update_timeslice_used(struct cfq_group *cfqg,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600792 uint64_t time, unsigned long unaccounted_time) { }
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600793static inline void cfqg_stats_update_io_remove(struct cfq_group *cfqg,
794 unsigned int op) { }
795static inline void cfqg_stats_update_io_merged(struct cfq_group *cfqg,
796 unsigned int op) { }
Tejun Heo155fead2012-04-01 14:38:44 -0700797static inline void cfqg_stats_update_completion(struct cfq_group *cfqg,
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600798 uint64_t start_time, uint64_t io_start_time,
799 unsigned int op) { }
Tejun Heo2ce4d502012-04-01 14:38:43 -0700800
Tejun Heoeb7d8c072012-03-23 14:02:53 +0100801#endif /* CONFIG_CFQ_GROUP_IOSCHED */
802
Jens Axboe7b679132008-05-30 12:23:07 +0200803#define cfq_log(cfqd, fmt, args...) \
804 blk_add_trace_msg((cfqd)->queue, "cfq " fmt, ##args)
805
Vivek Goyal615f0252009-12-03 12:59:39 -0500806/* Traverses through cfq group service trees */
807#define for_each_cfqg_st(cfqg, i, j, st) \
808 for (i = 0; i <= IDLE_WORKLOAD; i++) \
809 for (j = 0, st = i < IDLE_WORKLOAD ? &cfqg->service_trees[i][j]\
810 : &cfqg->service_tree_idle; \
811 (i < IDLE_WORKLOAD && j <= SYNC_WORKLOAD) || \
812 (i == IDLE_WORKLOAD && j == 0); \
813 j++, st = i < IDLE_WORKLOAD ? \
814 &cfqg->service_trees[i][j]: NULL) \
815
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200816static inline bool cfq_io_thinktime_big(struct cfq_data *cfqd,
817 struct cfq_ttime *ttime, bool group_idle)
818{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600819 u64 slice;
Shaohua Lif5f2b6c2011-07-12 14:24:55 +0200820 if (!sample_valid(ttime->ttime_samples))
821 return false;
822 if (group_idle)
823 slice = cfqd->cfq_group_idle;
824 else
825 slice = cfqd->cfq_slice_idle;
826 return ttime->ttime_mean > slice;
827}
Vivek Goyal615f0252009-12-03 12:59:39 -0500828
Vivek Goyal02b35082010-08-23 12:23:53 +0200829static inline bool iops_mode(struct cfq_data *cfqd)
830{
831 /*
832 * If we are not idling on queues and it is a NCQ drive, parallel
833 * execution of requests is on and measuring time is not possible
834 * in most of the cases until and unless we drive shallower queue
835 * depths and that becomes a performance bottleneck. In such cases
836 * switch to start providing fairness in terms of number of IOs.
837 */
838 if (!cfqd->cfq_slice_idle && cfqd->hw_tag)
839 return true;
840 else
841 return false;
842}
843
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400844static inline enum wl_class_t cfqq_class(struct cfq_queue *cfqq)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100845{
846 if (cfq_class_idle(cfqq))
847 return IDLE_WORKLOAD;
848 if (cfq_class_rt(cfqq))
849 return RT_WORKLOAD;
850 return BE_WORKLOAD;
851}
852
Corrado Zoccolo718eee02009-10-26 22:45:29 +0100853
854static enum wl_type_t cfqq_type(struct cfq_queue *cfqq)
855{
856 if (!cfq_cfqq_sync(cfqq))
857 return ASYNC_WORKLOAD;
858 if (!cfq_cfqq_idle_window(cfqq))
859 return SYNC_NOIDLE_WORKLOAD;
860 return SYNC_WORKLOAD;
861}
862
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400863static inline int cfq_group_busy_queues_wl(enum wl_class_t wl_class,
Vivek Goyal58ff82f2009-12-03 12:59:44 -0500864 struct cfq_data *cfqd,
865 struct cfq_group *cfqg)
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100866{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -0400867 if (wl_class == IDLE_WORKLOAD)
Vivek Goyalcdb16e82009-12-03 12:59:38 -0500868 return cfqg->service_tree_idle.count;
869
Vivek Goyal34b98d02012-10-03 16:56:58 -0400870 return cfqg->service_trees[wl_class][ASYNC_WORKLOAD].count +
871 cfqg->service_trees[wl_class][SYNC_NOIDLE_WORKLOAD].count +
872 cfqg->service_trees[wl_class][SYNC_WORKLOAD].count;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +0100873}
874
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500875static inline int cfqg_busy_async_queues(struct cfq_data *cfqd,
876 struct cfq_group *cfqg)
877{
Vivek Goyal34b98d02012-10-03 16:56:58 -0400878 return cfqg->service_trees[RT_WORKLOAD][ASYNC_WORKLOAD].count +
879 cfqg->service_trees[BE_WORKLOAD][ASYNC_WORKLOAD].count;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -0500880}
881
Jens Axboe165125e2007-07-24 09:28:11 +0200882static void cfq_dispatch_insert(struct request_queue *, struct request *);
Tejun Heo4f85cb92012-03-05 13:15:28 -0800883static struct cfq_queue *cfq_get_queue(struct cfq_data *cfqd, bool is_sync,
Tejun Heo2da8de02015-08-18 14:55:02 -0700884 struct cfq_io_cq *cic, struct bio *bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +0200885
Tejun Heoc5869802011-12-14 00:33:41 +0100886static inline struct cfq_io_cq *icq_to_cic(struct io_cq *icq)
887{
888 /* cic->icq is the first member, %NULL will convert to %NULL */
889 return container_of(icq, struct cfq_io_cq, icq);
890}
891
Tejun Heo47fdd4c2011-12-14 00:33:42 +0100892static inline struct cfq_io_cq *cfq_cic_lookup(struct cfq_data *cfqd,
893 struct io_context *ioc)
894{
895 if (ioc)
896 return icq_to_cic(ioc_lookup_icq(ioc, cfqd->queue));
897 return NULL;
898}
899
Tejun Heoc5869802011-12-14 00:33:41 +0100900static inline struct cfq_queue *cic_to_cfqq(struct cfq_io_cq *cic, bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200901{
Jens Axboea6151c32009-10-07 20:02:57 +0200902 return cic->cfqq[is_sync];
Vasily Tarasov91fac312007-04-25 12:29:51 +0200903}
904
Tejun Heoc5869802011-12-14 00:33:41 +0100905static inline void cic_set_cfqq(struct cfq_io_cq *cic, struct cfq_queue *cfqq,
906 bool is_sync)
Vasily Tarasov91fac312007-04-25 12:29:51 +0200907{
Jens Axboea6151c32009-10-07 20:02:57 +0200908 cic->cfqq[is_sync] = cfqq;
Vasily Tarasov91fac312007-04-25 12:29:51 +0200909}
910
Tejun Heoc5869802011-12-14 00:33:41 +0100911static inline struct cfq_data *cic_to_cfqd(struct cfq_io_cq *cic)
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400912{
Tejun Heoc5869802011-12-14 00:33:41 +0100913 return cic->icq.q->elevator->elevator_data;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +0400914}
915
Vasily Tarasov91fac312007-04-25 12:29:51 +0200916/*
Andrew Morton99f95e52005-06-27 20:14:05 -0700917 * scheduler run of queue, if there are requests pending and no one in the
918 * driver that will restart queueing
919 */
Jens Axboe23e018a2009-10-05 08:52:35 +0200920static inline void cfq_schedule_dispatch(struct cfq_data *cfqd)
Andrew Morton99f95e52005-06-27 20:14:05 -0700921{
Jens Axboe7b679132008-05-30 12:23:07 +0200922 if (cfqd->busy_queues) {
923 cfq_log(cfqd, "schedule dispatch");
Jens Axboe59c3d452014-04-08 09:15:35 -0600924 kblockd_schedule_work(&cfqd->unplug_work);
Jens Axboe7b679132008-05-30 12:23:07 +0200925 }
Andrew Morton99f95e52005-06-27 20:14:05 -0700926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
Jens Axboe44f7c162007-01-19 11:51:58 +1100929 * Scale schedule slice based on io priority. Use the sync time slice only
930 * if a queue is marked sync and has sync io queued. A sync queue with async
931 * io only, should not get full sync slice length.
932 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600933static inline u64 cfq_prio_slice(struct cfq_data *cfqd, bool sync,
Jens Axboed9e76202007-04-20 14:27:50 +0200934 unsigned short prio)
935{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600936 u64 base_slice = cfqd->cfq_slice[sync];
937 u64 slice = div_u64(base_slice, CFQ_SLICE_SCALE);
Jens Axboed9e76202007-04-20 14:27:50 +0200938
939 WARN_ON(prio >= IOPRIO_BE_NR);
940
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600941 return base_slice + (slice * (4 - prio));
Jens Axboed9e76202007-04-20 14:27:50 +0200942}
943
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600944static inline u64
Jens Axboe44f7c162007-01-19 11:51:58 +1100945cfq_prio_to_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
946{
Jens Axboed9e76202007-04-20 14:27:50 +0200947 return cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio);
Jens Axboe44f7c162007-01-19 11:51:58 +1100948}
949
Tejun Heo1d3650f2013-01-09 08:05:11 -0800950/**
951 * cfqg_scale_charge - scale disk time charge according to cfqg weight
952 * @charge: disk time being charged
953 * @vfraction: vfraction of the cfqg, fixed point w/ CFQ_SERVICE_SHIFT
954 *
955 * Scale @charge according to @vfraction, which is in range (0, 1]. The
956 * scaling is inversely proportional.
957 *
958 * scaled = charge / vfraction
959 *
960 * The result is also in fixed point w/ CFQ_SERVICE_SHIFT.
961 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600962static inline u64 cfqg_scale_charge(u64 charge,
Tejun Heo1d3650f2013-01-09 08:05:11 -0800963 unsigned int vfraction)
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500964{
Tejun Heo1d3650f2013-01-09 08:05:11 -0800965 u64 c = charge << CFQ_SERVICE_SHIFT; /* make it fixed point */
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500966
Tejun Heo1d3650f2013-01-09 08:05:11 -0800967 /* charge / vfraction */
968 c <<= CFQ_SERVICE_SHIFT;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -0600969 return div_u64(c, vfraction);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500970}
971
972static inline u64 max_vdisktime(u64 min_vdisktime, u64 vdisktime)
973{
974 s64 delta = (s64)(vdisktime - min_vdisktime);
975 if (delta > 0)
976 min_vdisktime = vdisktime;
977
978 return min_vdisktime;
979}
980
981static inline u64 min_vdisktime(u64 min_vdisktime, u64 vdisktime)
982{
983 s64 delta = (s64)(vdisktime - min_vdisktime);
984 if (delta < 0)
985 min_vdisktime = vdisktime;
986
987 return min_vdisktime;
988}
989
990static void update_min_vdisktime(struct cfq_rb_root *st)
991{
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500992 struct cfq_group *cfqg;
993
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500994 if (st->left) {
995 cfqg = rb_entry_cfqg(st->left);
Gui Jianfenga6032712011-03-07 09:28:09 +0100996 st->min_vdisktime = max_vdisktime(st->min_vdisktime,
997 cfqg->vdisktime);
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500998 }
Vivek Goyal25bc6b02009-12-03 12:59:43 -0500999}
1000
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001001/*
1002 * get averaged number of queues of RT/BE priority.
1003 * average is updated, with a formula that gives more weight to higher numbers,
1004 * to quickly follows sudden increases and decrease slowly
1005 */
1006
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001007static inline unsigned cfq_group_get_avg_queues(struct cfq_data *cfqd,
1008 struct cfq_group *cfqg, bool rt)
Jens Axboe5869619c2009-10-28 09:27:07 +01001009{
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001010 unsigned min_q, max_q;
1011 unsigned mult = cfq_hist_divisor - 1;
1012 unsigned round = cfq_hist_divisor / 2;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001013 unsigned busy = cfq_group_busy_queues_wl(rt, cfqd, cfqg);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001014
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001015 min_q = min(cfqg->busy_queues_avg[rt], busy);
1016 max_q = max(cfqg->busy_queues_avg[rt], busy);
1017 cfqg->busy_queues_avg[rt] = (mult * max_q + min_q + round) /
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001018 cfq_hist_divisor;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001019 return cfqg->busy_queues_avg[rt];
1020}
1021
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001022static inline u64
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001023cfq_group_slice(struct cfq_data *cfqd, struct cfq_group *cfqg)
1024{
Tejun Heo41cad6a2013-01-09 08:05:11 -08001025 return cfqd->cfq_target_latency * cfqg->vfraction >> CFQ_SERVICE_SHIFT;
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001026}
1027
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001028static inline u64
Vivek Goyalba5bd522011-01-19 08:25:02 -07001029cfq_scaled_cfqq_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001030{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001031 u64 slice = cfq_prio_to_slice(cfqd, cfqq);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001032 if (cfqd->cfq_latency) {
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001033 /*
1034 * interested queues (we consider only the ones with the same
1035 * priority class in the cfq group)
1036 */
1037 unsigned iq = cfq_group_get_avg_queues(cfqd, cfqq->cfqg,
1038 cfq_class_rt(cfqq));
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001039 u64 sync_slice = cfqd->cfq_slice[1];
1040 u64 expect_latency = sync_slice * iq;
1041 u64 group_slice = cfq_group_slice(cfqd, cfqq->cfqg);
Vivek Goyal58ff82f2009-12-03 12:59:44 -05001042
1043 if (expect_latency > group_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001044 u64 base_low_slice = 2 * cfqd->cfq_slice_idle;
1045 u64 low_slice;
1046
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001047 /* scale low_slice according to IO priority
1048 * and sync vs async */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001049 low_slice = div64_u64(base_low_slice*slice, sync_slice);
1050 low_slice = min(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001051 /* the adapted slice value is scaled to fit all iqs
1052 * into the target latency */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001053 slice = div64_u64(slice*group_slice, expect_latency);
1054 slice = max(slice, low_slice);
Corrado Zoccolo5db5d642009-10-26 22:44:04 +01001055 }
1056 }
Shaohua Lic553f8e2011-01-14 08:41:03 +01001057 return slice;
1058}
1059
1060static inline void
1061cfq_set_prio_slice(struct cfq_data *cfqd, struct cfq_queue *cfqq)
1062{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001063 u64 slice = cfq_scaled_cfqq_slice(cfqd, cfqq);
1064 u64 now = ktime_get_ns();
Shaohua Lic553f8e2011-01-14 08:41:03 +01001065
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001066 cfqq->slice_start = now;
1067 cfqq->slice_end = now + slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001068 cfqq->allocated_slice = slice;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001069 cfq_log_cfqq(cfqd, cfqq, "set_slice=%llu", cfqq->slice_end - now);
Jens Axboe44f7c162007-01-19 11:51:58 +11001070}
1071
1072/*
1073 * We need to wrap this check in cfq_cfqq_slice_new(), since ->slice_end
1074 * isn't valid until the first request from the dispatch is activated
1075 * and the slice time set.
1076 */
Jens Axboea6151c32009-10-07 20:02:57 +02001077static inline bool cfq_slice_used(struct cfq_queue *cfqq)
Jens Axboe44f7c162007-01-19 11:51:58 +11001078{
1079 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01001080 return false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001081 if (ktime_get_ns() < cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01001082 return false;
Jens Axboe44f7c162007-01-19 11:51:58 +11001083
Shaohua Lic1e44752010-11-08 15:01:02 +01001084 return true;
Jens Axboe44f7c162007-01-19 11:51:58 +11001085}
1086
1087/*
Jens Axboe5e705372006-07-13 12:39:25 +02001088 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * We choose the request that is closest to the head right now. Distance
Andreas Mohre8a99052006-03-28 08:59:49 +02001090 * behind the head is penalized and only allowed to a certain extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Jens Axboe5e705372006-07-13 12:39:25 +02001092static struct request *
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001093cfq_choose_req(struct cfq_data *cfqd, struct request *rq1, struct request *rq2, sector_t last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001095 sector_t s1, s2, d1 = 0, d2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned long back_max;
Andreas Mohre8a99052006-03-28 08:59:49 +02001097#define CFQ_RQ1_WRAP 0x01 /* request 1 wraps */
1098#define CFQ_RQ2_WRAP 0x02 /* request 2 wraps */
1099 unsigned wrap = 0; /* bit mask: requests behind the disk head? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jens Axboe5e705372006-07-13 12:39:25 +02001101 if (rq1 == NULL || rq1 == rq2)
1102 return rq2;
1103 if (rq2 == NULL)
1104 return rq1;
Jens Axboe9c2c38a2005-08-24 14:57:54 +02001105
Namhyung Kim229836b2011-05-24 10:23:21 +02001106 if (rq_is_sync(rq1) != rq_is_sync(rq2))
1107 return rq_is_sync(rq1) ? rq1 : rq2;
1108
Christoph Hellwig65299a32011-08-23 14:50:29 +02001109 if ((rq1->cmd_flags ^ rq2->cmd_flags) & REQ_PRIO)
1110 return rq1->cmd_flags & REQ_PRIO ? rq1 : rq2;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02001111
Tejun Heo83096eb2009-05-07 22:24:39 +09001112 s1 = blk_rq_pos(rq1);
1113 s2 = blk_rq_pos(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /*
1116 * by definition, 1KiB is 2 sectors
1117 */
1118 back_max = cfqd->cfq_back_max * 2;
1119
1120 /*
1121 * Strict one way elevator _except_ in the case where we allow
1122 * short backward seeks which are biased as twice the cost of a
1123 * similar forward seek.
1124 */
1125 if (s1 >= last)
1126 d1 = s1 - last;
1127 else if (s1 + back_max >= last)
1128 d1 = (last - s1) * cfqd->cfq_back_penalty;
1129 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001130 wrap |= CFQ_RQ1_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (s2 >= last)
1133 d2 = s2 - last;
1134 else if (s2 + back_max >= last)
1135 d2 = (last - s2) * cfqd->cfq_back_penalty;
1136 else
Andreas Mohre8a99052006-03-28 08:59:49 +02001137 wrap |= CFQ_RQ2_WRAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* Found required data */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Andreas Mohre8a99052006-03-28 08:59:49 +02001141 /*
1142 * By doing switch() on the bit mask "wrap" we avoid having to
1143 * check two variables for all permutations: --> faster!
1144 */
1145 switch (wrap) {
Jens Axboe5e705372006-07-13 12:39:25 +02001146 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001147 if (d1 < d2)
Jens Axboe5e705372006-07-13 12:39:25 +02001148 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001149 else if (d2 < d1)
Jens Axboe5e705372006-07-13 12:39:25 +02001150 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001151 else {
1152 if (s1 >= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001153 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001154 else
Jens Axboe5e705372006-07-13 12:39:25 +02001155 return rq2;
Andreas Mohre8a99052006-03-28 08:59:49 +02001156 }
1157
1158 case CFQ_RQ2_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001159 return rq1;
Andreas Mohre8a99052006-03-28 08:59:49 +02001160 case CFQ_RQ1_WRAP:
Jens Axboe5e705372006-07-13 12:39:25 +02001161 return rq2;
1162 case (CFQ_RQ1_WRAP|CFQ_RQ2_WRAP): /* both rqs wrapped */
Andreas Mohre8a99052006-03-28 08:59:49 +02001163 default:
1164 /*
1165 * Since both rqs are wrapped,
1166 * start with the one that's further behind head
1167 * (--> only *one* back seek required),
1168 * since back seek takes more time than forward.
1169 */
1170 if (s1 <= s2)
Jens Axboe5e705372006-07-13 12:39:25 +02001171 return rq1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 else
Jens Axboe5e705372006-07-13 12:39:25 +02001173 return rq2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175}
1176
Jens Axboe498d3aa22007-04-26 12:54:48 +02001177/*
1178 * The below is leftmost cache rbtree addon
1179 */
Jens Axboe08717142008-01-28 11:38:15 +01001180static struct cfq_queue *cfq_rb_first(struct cfq_rb_root *root)
Jens Axboecc09e292007-04-26 12:53:50 +02001181{
Vivek Goyal615f0252009-12-03 12:59:39 -05001182 /* Service tree is empty */
1183 if (!root->count)
1184 return NULL;
1185
Jens Axboecc09e292007-04-26 12:53:50 +02001186 if (!root->left)
1187 root->left = rb_first(&root->rb);
1188
Jens Axboe08717142008-01-28 11:38:15 +01001189 if (root->left)
1190 return rb_entry(root->left, struct cfq_queue, rb_node);
1191
1192 return NULL;
Jens Axboecc09e292007-04-26 12:53:50 +02001193}
1194
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001195static struct cfq_group *cfq_rb_first_group(struct cfq_rb_root *root)
1196{
1197 if (!root->left)
1198 root->left = rb_first(&root->rb);
1199
1200 if (root->left)
1201 return rb_entry_cfqg(root->left);
1202
1203 return NULL;
1204}
1205
Jens Axboea36e71f2009-04-15 12:15:11 +02001206static void rb_erase_init(struct rb_node *n, struct rb_root *root)
1207{
1208 rb_erase(n, root);
1209 RB_CLEAR_NODE(n);
1210}
1211
Jens Axboecc09e292007-04-26 12:53:50 +02001212static void cfq_rb_erase(struct rb_node *n, struct cfq_rb_root *root)
1213{
1214 if (root->left == n)
1215 root->left = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02001216 rb_erase_init(n, &root->rb);
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01001217 --root->count;
Jens Axboecc09e292007-04-26 12:53:50 +02001218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/*
1221 * would be nice to take fifo expire time into account as well
1222 */
Jens Axboe5e705372006-07-13 12:39:25 +02001223static struct request *
1224cfq_find_next_rq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
1225 struct request *last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jens Axboe21183b02006-07-13 12:33:14 +02001227 struct rb_node *rbnext = rb_next(&last->rb_node);
1228 struct rb_node *rbprev = rb_prev(&last->rb_node);
Jens Axboe5e705372006-07-13 12:39:25 +02001229 struct request *next = NULL, *prev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Jens Axboe21183b02006-07-13 12:33:14 +02001231 BUG_ON(RB_EMPTY_NODE(&last->rb_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (rbprev)
Jens Axboe5e705372006-07-13 12:39:25 +02001234 prev = rb_entry_rq(rbprev);
Jens Axboe21183b02006-07-13 12:33:14 +02001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (rbnext)
Jens Axboe5e705372006-07-13 12:39:25 +02001237 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001238 else {
1239 rbnext = rb_first(&cfqq->sort_list);
1240 if (rbnext && rbnext != &last->rb_node)
Jens Axboe5e705372006-07-13 12:39:25 +02001241 next = rb_entry_rq(rbnext);
Jens Axboe21183b02006-07-13 12:33:14 +02001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01001244 return cfq_choose_req(cfqd, next, prev, blk_rq_pos(last));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001247static u64 cfq_slice_offset(struct cfq_data *cfqd,
1248 struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Jens Axboed9e76202007-04-20 14:27:50 +02001250 /*
1251 * just an approximation, should be ok.
1252 */
Vivek Goyalcdb16e82009-12-03 12:59:38 -05001253 return (cfqq->cfqg->nr_cfqq - 1) * (cfq_prio_slice(cfqd, 1, 0) -
Jens Axboe464191c2009-11-30 09:38:13 +01001254 cfq_prio_slice(cfqd, cfq_cfqq_sync(cfqq), cfqq->ioprio));
Jens Axboed9e76202007-04-20 14:27:50 +02001255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001257static inline s64
1258cfqg_key(struct cfq_rb_root *st, struct cfq_group *cfqg)
1259{
1260 return cfqg->vdisktime - st->min_vdisktime;
1261}
1262
1263static void
1264__cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1265{
1266 struct rb_node **node = &st->rb.rb_node;
1267 struct rb_node *parent = NULL;
1268 struct cfq_group *__cfqg;
1269 s64 key = cfqg_key(st, cfqg);
1270 int left = 1;
1271
1272 while (*node != NULL) {
1273 parent = *node;
1274 __cfqg = rb_entry_cfqg(parent);
1275
1276 if (key < cfqg_key(st, __cfqg))
1277 node = &parent->rb_left;
1278 else {
1279 node = &parent->rb_right;
1280 left = 0;
1281 }
1282 }
1283
1284 if (left)
1285 st->left = &cfqg->rb_node;
1286
1287 rb_link_node(&cfqg->rb_node, parent, node);
1288 rb_insert_color(&cfqg->rb_node, &st->rb);
1289}
1290
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001291/*
1292 * This has to be called only on activation of cfqg
1293 */
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001294static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001295cfq_update_group_weight(struct cfq_group *cfqg)
1296{
Tejun Heo3381cb82012-04-01 14:38:44 -07001297 if (cfqg->new_weight) {
Justin TerAvest8184f932011-03-17 16:12:36 +01001298 cfqg->weight = cfqg->new_weight;
Tejun Heo3381cb82012-04-01 14:38:44 -07001299 cfqg->new_weight = 0;
Justin TerAvest8184f932011-03-17 16:12:36 +01001300 }
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001301}
1302
1303static void
1304cfq_update_group_leaf_weight(struct cfq_group *cfqg)
1305{
1306 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
Tejun Heoe71357e2013-01-09 08:05:10 -08001307
1308 if (cfqg->new_leaf_weight) {
1309 cfqg->leaf_weight = cfqg->new_leaf_weight;
1310 cfqg->new_leaf_weight = 0;
1311 }
Justin TerAvest8184f932011-03-17 16:12:36 +01001312}
1313
1314static void
1315cfq_group_service_tree_add(struct cfq_rb_root *st, struct cfq_group *cfqg)
1316{
Tejun Heo1d3650f2013-01-09 08:05:11 -08001317 unsigned int vfr = 1 << CFQ_SERVICE_SHIFT; /* start with 1 */
Tejun Heo7918ffb2013-01-09 08:05:11 -08001318 struct cfq_group *pos = cfqg;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001319 struct cfq_group *parent;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001320 bool propagate;
1321
1322 /* add to the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001323 BUG_ON(!RB_EMPTY_NODE(&cfqg->rb_node));
1324
Toshiaki Makita7b5af5c2014-08-28 17:14:58 +09001325 /*
1326 * Update leaf_weight. We cannot update weight at this point
1327 * because cfqg might already have been activated and is
1328 * contributing its current weight to the parent's child_weight.
1329 */
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001330 cfq_update_group_leaf_weight(cfqg);
Justin TerAvest8184f932011-03-17 16:12:36 +01001331 __cfq_group_service_tree_add(st, cfqg);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001332
1333 /*
Tejun Heo1d3650f2013-01-09 08:05:11 -08001334 * Activate @cfqg and calculate the portion of vfraction @cfqg is
1335 * entitled to. vfraction is calculated by walking the tree
1336 * towards the root calculating the fraction it has at each level.
1337 * The compounded ratio is how much vfraction @cfqg owns.
1338 *
1339 * Start with the proportion tasks in this cfqg has against active
1340 * children cfqgs - its leaf_weight against children_weight.
Tejun Heo7918ffb2013-01-09 08:05:11 -08001341 */
1342 propagate = !pos->nr_active++;
1343 pos->children_weight += pos->leaf_weight;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001344 vfr = vfr * pos->leaf_weight / pos->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001345
Tejun Heo1d3650f2013-01-09 08:05:11 -08001346 /*
1347 * Compound ->weight walking up the tree. Both activation and
1348 * vfraction calculation are done in the same loop. Propagation
1349 * stops once an already activated node is met. vfraction
1350 * calculation should always continue to the root.
1351 */
Tejun Heod02f7aa2013-01-09 08:05:11 -08001352 while ((parent = cfqg_parent(pos))) {
Tejun Heo1d3650f2013-01-09 08:05:11 -08001353 if (propagate) {
Toshiaki Makitae15693e2014-08-26 20:56:36 +09001354 cfq_update_group_weight(pos);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001355 propagate = !parent->nr_active++;
1356 parent->children_weight += pos->weight;
1357 }
1358 vfr = vfr * pos->weight / parent->children_weight;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001359 pos = parent;
1360 }
Tejun Heo1d3650f2013-01-09 08:05:11 -08001361
1362 cfqg->vfraction = max_t(unsigned, vfr, 1);
Justin TerAvest8184f932011-03-17 16:12:36 +01001363}
1364
1365static void
1366cfq_group_notify_queue_add(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001367{
1368 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1369 struct cfq_group *__cfqg;
1370 struct rb_node *n;
1371
1372 cfqg->nr_cfqq++;
Gui Jianfeng760701b2010-11-30 20:52:47 +01001373 if (!RB_EMPTY_NODE(&cfqg->rb_node))
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001374 return;
1375
1376 /*
1377 * Currently put the group at the end. Later implement something
1378 * so that groups get lesser vtime based on their weights, so that
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001379 * if group does not loose all if it was not continuously backlogged.
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001380 */
1381 n = rb_last(&st->rb);
1382 if (n) {
1383 __cfqg = rb_entry_cfqg(n);
1384 cfqg->vdisktime = __cfqg->vdisktime + CFQ_IDLE_DELAY;
1385 } else
1386 cfqg->vdisktime = st->min_vdisktime;
Justin TerAvest8184f932011-03-17 16:12:36 +01001387 cfq_group_service_tree_add(st, cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001388}
1389
1390static void
Justin TerAvest8184f932011-03-17 16:12:36 +01001391cfq_group_service_tree_del(struct cfq_rb_root *st, struct cfq_group *cfqg)
1392{
Tejun Heo7918ffb2013-01-09 08:05:11 -08001393 struct cfq_group *pos = cfqg;
1394 bool propagate;
1395
1396 /*
1397 * Undo activation from cfq_group_service_tree_add(). Deactivate
1398 * @cfqg and propagate deactivation upwards.
1399 */
1400 propagate = !--pos->nr_active;
1401 pos->children_weight -= pos->leaf_weight;
1402
1403 while (propagate) {
Tejun Heod02f7aa2013-01-09 08:05:11 -08001404 struct cfq_group *parent = cfqg_parent(pos);
Tejun Heo7918ffb2013-01-09 08:05:11 -08001405
1406 /* @pos has 0 nr_active at this point */
1407 WARN_ON_ONCE(pos->children_weight);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001408 pos->vfraction = 0;
Tejun Heo7918ffb2013-01-09 08:05:11 -08001409
1410 if (!parent)
1411 break;
1412
1413 propagate = !--parent->nr_active;
1414 parent->children_weight -= pos->weight;
1415 pos = parent;
1416 }
1417
1418 /* remove from the service tree */
Justin TerAvest8184f932011-03-17 16:12:36 +01001419 if (!RB_EMPTY_NODE(&cfqg->rb_node))
1420 cfq_rb_erase(&cfqg->rb_node, st);
1421}
1422
1423static void
1424cfq_group_notify_queue_del(struct cfq_data *cfqd, struct cfq_group *cfqg)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001425{
1426 struct cfq_rb_root *st = &cfqd->grp_service_tree;
1427
1428 BUG_ON(cfqg->nr_cfqq < 1);
1429 cfqg->nr_cfqq--;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05001430
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001431 /* If there are other cfq queues under this group, don't delete it */
1432 if (cfqg->nr_cfqq)
1433 return;
1434
Vivek Goyal2868ef72009-12-03 12:59:48 -05001435 cfq_log_cfqg(cfqd, cfqg, "del_from_rr group");
Justin TerAvest8184f932011-03-17 16:12:36 +01001436 cfq_group_service_tree_del(st, cfqg);
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001437 cfqg->saved_wl_slice = 0;
Tejun Heo155fead2012-04-01 14:38:44 -07001438 cfqg_stats_update_dequeue(cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001439}
1440
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001441static inline u64 cfq_cfqq_slice_usage(struct cfq_queue *cfqq,
1442 u64 *unaccounted_time)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001443{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001444 u64 slice_used;
1445 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001446
1447 /*
1448 * Queue got expired before even a single request completed or
1449 * got expired immediately after first request completion.
1450 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001451 if (!cfqq->slice_start || cfqq->slice_start == now) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05001452 /*
1453 * Also charge the seek time incurred to the group, otherwise
1454 * if there are mutiple queues in the group, each can dispatch
1455 * a single request on seeky media and cause lots of seek time
1456 * and group will never know it.
1457 */
Jan Kara0b31c102016-06-28 09:04:02 +02001458 slice_used = max_t(u64, (now - cfqq->dispatch_start),
1459 jiffies_to_nsecs(1));
Vivek Goyaldae739e2009-12-03 12:59:45 -05001460 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001461 slice_used = now - cfqq->slice_start;
Justin TerAvest167400d2011-03-12 16:54:00 +01001462 if (slice_used > cfqq->allocated_slice) {
1463 *unaccounted_time = slice_used - cfqq->allocated_slice;
Vivek Goyalf75edf22009-12-03 12:59:53 -05001464 slice_used = cfqq->allocated_slice;
Justin TerAvest167400d2011-03-12 16:54:00 +01001465 }
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001466 if (cfqq->slice_start > cfqq->dispatch_start)
Justin TerAvest167400d2011-03-12 16:54:00 +01001467 *unaccounted_time += cfqq->slice_start -
1468 cfqq->dispatch_start;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001469 }
1470
Vivek Goyaldae739e2009-12-03 12:59:45 -05001471 return slice_used;
1472}
1473
1474static void cfq_group_served(struct cfq_data *cfqd, struct cfq_group *cfqg,
Vivek Goyale5ff0822010-04-26 19:25:11 +02001475 struct cfq_queue *cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05001476{
1477 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001478 u64 used_sl, charge, unaccounted_sl = 0;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001479 int nr_sync = cfqg->nr_cfqq - cfqg_busy_async_queues(cfqd, cfqg)
1480 - cfqg->service_tree_idle.count;
Tejun Heo1d3650f2013-01-09 08:05:11 -08001481 unsigned int vfr;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001482 u64 now = ktime_get_ns();
Vivek Goyaldae739e2009-12-03 12:59:45 -05001483
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001484 BUG_ON(nr_sync < 0);
Justin TerAvest167400d2011-03-12 16:54:00 +01001485 used_sl = charge = cfq_cfqq_slice_usage(cfqq, &unaccounted_sl);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05001486
Vivek Goyal02b35082010-08-23 12:23:53 +02001487 if (iops_mode(cfqd))
1488 charge = cfqq->slice_dispatch;
1489 else if (!cfq_cfqq_sync(cfqq) && !nr_sync)
1490 charge = cfqq->allocated_slice;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001491
Tejun Heo1d3650f2013-01-09 08:05:11 -08001492 /*
1493 * Can't update vdisktime while on service tree and cfqg->vfraction
1494 * is valid only while on it. Cache vfr, leave the service tree,
1495 * update vdisktime and go back on. The re-addition to the tree
1496 * will also update the weights as necessary.
1497 */
1498 vfr = cfqg->vfraction;
Justin TerAvest8184f932011-03-17 16:12:36 +01001499 cfq_group_service_tree_del(st, cfqg);
Tejun Heo1d3650f2013-01-09 08:05:11 -08001500 cfqg->vdisktime += cfqg_scale_charge(charge, vfr);
Justin TerAvest8184f932011-03-17 16:12:36 +01001501 cfq_group_service_tree_add(st, cfqg);
Vivek Goyaldae739e2009-12-03 12:59:45 -05001502
1503 /* This group is being expired. Save the context */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001504 if (cfqd->workload_expires > now) {
1505 cfqg->saved_wl_slice = cfqd->workload_expires - now;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001506 cfqg->saved_wl_type = cfqd->serving_wl_type;
1507 cfqg->saved_wl_class = cfqd->serving_wl_class;
Vivek Goyaldae739e2009-12-03 12:59:45 -05001508 } else
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04001509 cfqg->saved_wl_slice = 0;
Vivek Goyal2868ef72009-12-03 12:59:48 -05001510
1511 cfq_log_cfqg(cfqd, cfqg, "served: vt=%llu min_vt=%llu", cfqg->vdisktime,
1512 st->min_vdisktime);
Joe Perchesfd16d262011-06-13 10:42:49 +02001513 cfq_log_cfqq(cfqq->cfqd, cfqq,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001514 "sl_used=%llu disp=%llu charge=%llu iops=%u sect=%lu",
Joe Perchesfd16d262011-06-13 10:42:49 +02001515 used_sl, cfqq->slice_dispatch, charge,
1516 iops_mode(cfqd), cfqq->nr_sectors);
Tejun Heo155fead2012-04-01 14:38:44 -07001517 cfqg_stats_update_timeslice_used(cfqg, used_sl, unaccounted_sl);
1518 cfqg_stats_set_start_empty_time(cfqg);
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05001519}
1520
Tejun Heof51b8022012-03-05 13:15:05 -08001521/**
1522 * cfq_init_cfqg_base - initialize base part of a cfq_group
1523 * @cfqg: cfq_group to initialize
1524 *
1525 * Initialize the base part which is used whether %CONFIG_CFQ_GROUP_IOSCHED
1526 * is enabled or not.
1527 */
1528static void cfq_init_cfqg_base(struct cfq_group *cfqg)
1529{
1530 struct cfq_rb_root *st;
1531 int i, j;
1532
1533 for_each_cfqg_st(cfqg, i, j, st)
1534 *st = CFQ_RB_ROOT;
1535 RB_CLEAR_NODE(&cfqg->rb_node);
1536
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06001537 cfqg->ttime.last_end_request = ktime_get_ns();
Tejun Heof51b8022012-03-05 13:15:05 -08001538}
1539
Vivek Goyal25fb5162009-12-03 12:59:46 -05001540#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo69d7fde2015-08-18 14:55:36 -07001541static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
1542 bool on_dfl, bool reset_dev, bool is_leaf_weight);
1543
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001544static void cfqg_stats_exit(struct cfqg_stats *stats)
Peter Zijlstra90d38392013-11-12 19:42:14 -08001545{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001546 blkg_rwstat_exit(&stats->merged);
1547 blkg_rwstat_exit(&stats->service_time);
1548 blkg_rwstat_exit(&stats->wait_time);
1549 blkg_rwstat_exit(&stats->queued);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001550 blkg_stat_exit(&stats->time);
1551#ifdef CONFIG_DEBUG_BLK_CGROUP
1552 blkg_stat_exit(&stats->unaccounted_time);
1553 blkg_stat_exit(&stats->avg_queue_size_sum);
1554 blkg_stat_exit(&stats->avg_queue_size_samples);
1555 blkg_stat_exit(&stats->dequeue);
1556 blkg_stat_exit(&stats->group_wait_time);
1557 blkg_stat_exit(&stats->idle_time);
1558 blkg_stat_exit(&stats->empty_time);
1559#endif
1560}
1561
1562static int cfqg_stats_init(struct cfqg_stats *stats, gfp_t gfp)
1563{
Tejun Heo77ea7332015-08-18 14:55:24 -07001564 if (blkg_rwstat_init(&stats->merged, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001565 blkg_rwstat_init(&stats->service_time, gfp) ||
1566 blkg_rwstat_init(&stats->wait_time, gfp) ||
1567 blkg_rwstat_init(&stats->queued, gfp) ||
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001568 blkg_stat_init(&stats->time, gfp))
1569 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001570
1571#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001572 if (blkg_stat_init(&stats->unaccounted_time, gfp) ||
1573 blkg_stat_init(&stats->avg_queue_size_sum, gfp) ||
1574 blkg_stat_init(&stats->avg_queue_size_samples, gfp) ||
1575 blkg_stat_init(&stats->dequeue, gfp) ||
1576 blkg_stat_init(&stats->group_wait_time, gfp) ||
1577 blkg_stat_init(&stats->idle_time, gfp) ||
1578 blkg_stat_init(&stats->empty_time, gfp))
1579 goto err;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001580#endif
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001581 return 0;
1582err:
1583 cfqg_stats_exit(stats);
1584 return -ENOMEM;
Peter Zijlstra90d38392013-11-12 19:42:14 -08001585}
1586
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001587static struct blkcg_policy_data *cfq_cpd_alloc(gfp_t gfp)
1588{
1589 struct cfq_group_data *cgd;
1590
Tejun Heoebc4ff62016-11-10 11:16:37 -05001591 cgd = kzalloc(sizeof(*cgd), gfp);
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001592 if (!cgd)
1593 return NULL;
1594 return &cgd->cpd;
1595}
1596
Tejun Heo81437642015-08-18 14:55:15 -07001597static void cfq_cpd_init(struct blkcg_policy_data *cpd)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001598{
Tejun Heo81437642015-08-18 14:55:15 -07001599 struct cfq_group_data *cgd = cpd_to_cfqgd(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001600 unsigned int weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ?
Tejun Heo69d7fde2015-08-18 14:55:36 -07001601 CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001602
Tejun Heo69d7fde2015-08-18 14:55:36 -07001603 if (cpd_to_blkcg(cpd) == &blkcg_root)
1604 weight *= 2;
1605
1606 cgd->weight = weight;
1607 cgd->leaf_weight = weight;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001608}
1609
Tejun Heoe4a9bde2015-08-18 14:55:16 -07001610static void cfq_cpd_free(struct blkcg_policy_data *cpd)
1611{
1612 kfree(cpd_to_cfqgd(cpd));
1613}
1614
Tejun Heo69d7fde2015-08-18 14:55:36 -07001615static void cfq_cpd_bind(struct blkcg_policy_data *cpd)
1616{
1617 struct blkcg *blkcg = cpd_to_blkcg(cpd);
Tejun Heo9e10a132015-09-18 11:56:28 -04001618 bool on_dfl = cgroup_subsys_on_dfl(io_cgrp_subsys);
Tejun Heo69d7fde2015-08-18 14:55:36 -07001619 unsigned int weight = on_dfl ? CGROUP_WEIGHT_DFL : CFQ_WEIGHT_LEGACY_DFL;
1620
1621 if (blkcg == &blkcg_root)
1622 weight *= 2;
1623
1624 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, false));
1625 WARN_ON_ONCE(__cfq_set_weight(&blkcg->css, weight, on_dfl, true, true));
1626}
1627
Tejun Heo001bea72015-08-18 14:55:11 -07001628static struct blkg_policy_data *cfq_pd_alloc(gfp_t gfp, int node)
1629{
Tejun Heob2ce2642015-08-18 14:55:13 -07001630 struct cfq_group *cfqg;
1631
1632 cfqg = kzalloc_node(sizeof(*cfqg), gfp, node);
1633 if (!cfqg)
1634 return NULL;
1635
1636 cfq_init_cfqg_base(cfqg);
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001637 if (cfqg_stats_init(&cfqg->stats, gfp)) {
1638 kfree(cfqg);
1639 return NULL;
1640 }
Tejun Heob2ce2642015-08-18 14:55:13 -07001641
1642 return &cfqg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -07001643}
1644
Tejun Heoa9520cd2015-08-18 14:55:14 -07001645static void cfq_pd_init(struct blkg_policy_data *pd)
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001646{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001647 struct cfq_group *cfqg = pd_to_cfqg(pd);
1648 struct cfq_group_data *cgd = blkcg_to_cfqgd(pd->blkg->blkcg);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001649
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001650 cfqg->weight = cgd->weight;
1651 cfqg->leaf_weight = cgd->leaf_weight;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001652}
1653
Tejun Heoa9520cd2015-08-18 14:55:14 -07001654static void cfq_pd_offline(struct blkg_policy_data *pd)
Tejun Heo0b399202013-01-09 08:05:13 -08001655{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001656 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo60a83702015-08-18 14:55:05 -07001657 int i;
1658
1659 for (i = 0; i < IOPRIO_BE_NR; i++) {
1660 if (cfqg->async_cfqq[0][i])
1661 cfq_put_queue(cfqg->async_cfqq[0][i]);
1662 if (cfqg->async_cfqq[1][i])
1663 cfq_put_queue(cfqg->async_cfqq[1][i]);
1664 }
1665
1666 if (cfqg->async_idle_cfqq)
1667 cfq_put_queue(cfqg->async_idle_cfqq);
1668
Tejun Heo0b399202013-01-09 08:05:13 -08001669 /*
1670 * @blkg is going offline and will be ignored by
1671 * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so
1672 * that they don't get lost. If IOs complete after this point, the
1673 * stats for them will be lost. Oh well...
1674 */
Tejun Heo60a83702015-08-18 14:55:05 -07001675 cfqg_stats_xfer_dead(cfqg);
Tejun Heo0b399202013-01-09 08:05:13 -08001676}
1677
Tejun Heo001bea72015-08-18 14:55:11 -07001678static void cfq_pd_free(struct blkg_policy_data *pd)
1679{
Tejun Heo24bdb8e2015-08-18 14:55:22 -07001680 struct cfq_group *cfqg = pd_to_cfqg(pd);
1681
1682 cfqg_stats_exit(&cfqg->stats);
1683 return kfree(cfqg);
Tejun Heo001bea72015-08-18 14:55:11 -07001684}
1685
Tejun Heoa9520cd2015-08-18 14:55:14 -07001686static void cfq_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo689665a2013-01-09 08:05:13 -08001687{
Tejun Heoa9520cd2015-08-18 14:55:14 -07001688 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo689665a2013-01-09 08:05:13 -08001689
1690 cfqg_stats_reset(&cfqg->stats);
Vivek Goyal25fb5162009-12-03 12:59:46 -05001691}
1692
Tejun Heoae118892015-08-18 14:55:20 -07001693static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
1694 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05001695{
Tejun Heoae118892015-08-18 14:55:20 -07001696 struct blkcg_gq *blkg;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001697
Tejun Heoae118892015-08-18 14:55:20 -07001698 blkg = blkg_lookup(blkcg, cfqd->queue);
1699 if (likely(blkg))
1700 return blkg_to_cfqg(blkg);
1701 return NULL;
Vivek Goyal25fb5162009-12-03 12:59:46 -05001702}
1703
1704static void cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg)
1705{
Vivek Goyal25fb5162009-12-03 12:59:46 -05001706 cfqq->cfqg = cfqg;
Vivek Goyalb1c35762009-12-03 12:59:47 -05001707 /* cfqq reference on cfqg */
Tejun Heoeb7d8c072012-03-23 14:02:53 +01001708 cfqg_get(cfqg);
Vivek Goyalb1c35762009-12-03 12:59:47 -05001709}
1710
Tejun Heof95a04a2012-04-16 13:57:26 -07001711static u64 cfqg_prfill_weight_device(struct seq_file *sf,
1712 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001713{
Tejun Heof95a04a2012-04-16 13:57:26 -07001714 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo3381cb82012-04-01 14:38:44 -07001715
1716 if (!cfqg->dev_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001717 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001718 return __blkg_prfill_u64(sf, pd, cfqg->dev_weight);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001719}
1720
Tejun Heo2da8ca82013-12-05 12:28:04 -05001721static int cfqg_print_weight_device(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001722{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001723 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1724 cfqg_prfill_weight_device, &blkcg_policy_cfq,
1725 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001726 return 0;
1727}
1728
Tejun Heoe71357e2013-01-09 08:05:10 -08001729static u64 cfqg_prfill_leaf_weight_device(struct seq_file *sf,
1730 struct blkg_policy_data *pd, int off)
1731{
1732 struct cfq_group *cfqg = pd_to_cfqg(pd);
1733
1734 if (!cfqg->dev_leaf_weight)
1735 return 0;
1736 return __blkg_prfill_u64(sf, pd, cfqg->dev_leaf_weight);
1737}
1738
Tejun Heo2da8ca82013-12-05 12:28:04 -05001739static int cfqg_print_leaf_weight_device(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001740{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001741 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1742 cfqg_prfill_leaf_weight_device, &blkcg_policy_cfq,
1743 0, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001744 return 0;
1745}
1746
Tejun Heo2da8ca82013-12-05 12:28:04 -05001747static int cfq_print_weight(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001748{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001749 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001750 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1751 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001752
Jens Axboe9470e4a2015-06-19 10:19:36 -06001753 if (cgd)
1754 val = cgd->weight;
1755
1756 seq_printf(sf, "%u\n", val);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001757 return 0;
1758}
1759
Tejun Heo2da8ca82013-12-05 12:28:04 -05001760static int cfq_print_leaf_weight(struct seq_file *sf, void *v)
Tejun Heoe71357e2013-01-09 08:05:10 -08001761{
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001762 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
Jens Axboe9470e4a2015-06-19 10:19:36 -06001763 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
1764 unsigned int val = 0;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001765
Jens Axboe9470e4a2015-06-19 10:19:36 -06001766 if (cgd)
1767 val = cgd->leaf_weight;
1768
1769 seq_printf(sf, "%u\n", val);
Tejun Heoe71357e2013-01-09 08:05:10 -08001770 return 0;
1771}
1772
Tejun Heo451af502014-05-13 12:16:21 -04001773static ssize_t __cfqg_set_weight_device(struct kernfs_open_file *of,
1774 char *buf, size_t nbytes, loff_t off,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001775 bool on_dfl, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001776{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001777 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1778 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo451af502014-05-13 12:16:21 -04001779 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001780 struct blkg_conf_ctx ctx;
Tejun Heo3381cb82012-04-01 14:38:44 -07001781 struct cfq_group *cfqg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001782 struct cfq_group_data *cfqgd;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001783 int ret;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001784 u64 v;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001785
Tejun Heo3c798392012-04-16 13:57:25 -07001786 ret = blkg_conf_prep(blkcg, &blkcg_policy_cfq, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001787 if (ret)
1788 return ret;
1789
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001790 if (sscanf(ctx.body, "%llu", &v) == 1) {
1791 /* require "default" on dfl */
1792 ret = -ERANGE;
1793 if (!v && on_dfl)
1794 goto out_finish;
1795 } else if (!strcmp(strim(ctx.body), "default")) {
1796 v = 0;
1797 } else {
1798 ret = -EINVAL;
Tejun Heo36aa9e52015-08-18 14:55:31 -07001799 goto out_finish;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001800 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001801
Tejun Heo3381cb82012-04-01 14:38:44 -07001802 cfqg = blkg_to_cfqg(ctx.blkg);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001803 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001804
Tejun Heo20386ce2015-08-18 14:55:28 -07001805 ret = -ERANGE;
Tejun Heo69d7fde2015-08-18 14:55:36 -07001806 if (!v || (v >= min && v <= max)) {
Tejun Heoe71357e2013-01-09 08:05:10 -08001807 if (!is_leaf_weight) {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001808 cfqg->dev_weight = v;
1809 cfqg->new_weight = v ?: cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001810 } else {
Tejun Heo36aa9e52015-08-18 14:55:31 -07001811 cfqg->dev_leaf_weight = v;
1812 cfqg->new_leaf_weight = v ?: cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001813 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001814 ret = 0;
1815 }
Tejun Heo36aa9e52015-08-18 14:55:31 -07001816out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001817 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001818 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001819}
1820
Tejun Heo451af502014-05-13 12:16:21 -04001821static ssize_t cfqg_set_weight_device(struct kernfs_open_file *of,
1822 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001823{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001824 return __cfqg_set_weight_device(of, buf, nbytes, off, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001825}
1826
Tejun Heo451af502014-05-13 12:16:21 -04001827static ssize_t cfqg_set_leaf_weight_device(struct kernfs_open_file *of,
1828 char *buf, size_t nbytes, loff_t off)
Tejun Heoe71357e2013-01-09 08:05:10 -08001829{
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001830 return __cfqg_set_weight_device(of, buf, nbytes, off, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001831}
1832
Tejun Heodd165eb2015-08-18 14:55:33 -07001833static int __cfq_set_weight(struct cgroup_subsys_state *css, u64 val,
Tejun Heo69d7fde2015-08-18 14:55:36 -07001834 bool on_dfl, bool reset_dev, bool is_leaf_weight)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001835{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001836 unsigned int min = on_dfl ? CGROUP_WEIGHT_MIN : CFQ_WEIGHT_LEGACY_MIN;
1837 unsigned int max = on_dfl ? CGROUP_WEIGHT_MAX : CFQ_WEIGHT_LEGACY_MAX;
Tejun Heo182446d2013-08-08 20:11:24 -04001838 struct blkcg *blkcg = css_to_blkcg(css);
Tejun Heo3c798392012-04-16 13:57:25 -07001839 struct blkcg_gq *blkg;
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001840 struct cfq_group_data *cfqgd;
Jens Axboeae994ea2015-06-20 10:26:50 -06001841 int ret = 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001842
Tejun Heo69d7fde2015-08-18 14:55:36 -07001843 if (val < min || val > max)
1844 return -ERANGE;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001845
1846 spin_lock_irq(&blkcg->lock);
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001847 cfqgd = blkcg_to_cfqgd(blkcg);
Jens Axboeae994ea2015-06-20 10:26:50 -06001848 if (!cfqgd) {
1849 ret = -EINVAL;
1850 goto out;
1851 }
Tejun Heoe71357e2013-01-09 08:05:10 -08001852
1853 if (!is_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001854 cfqgd->weight = val;
Tejun Heoe71357e2013-01-09 08:05:10 -08001855 else
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001856 cfqgd->leaf_weight = val;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001857
Sasha Levinb67bfe02013-02-27 17:06:00 -08001858 hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) {
Tejun Heo3381cb82012-04-01 14:38:44 -07001859 struct cfq_group *cfqg = blkg_to_cfqg(blkg);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001860
Tejun Heoe71357e2013-01-09 08:05:10 -08001861 if (!cfqg)
1862 continue;
1863
1864 if (!is_leaf_weight) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001865 if (reset_dev)
1866 cfqg->dev_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001867 if (!cfqg->dev_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001868 cfqg->new_weight = cfqgd->weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001869 } else {
Tejun Heo69d7fde2015-08-18 14:55:36 -07001870 if (reset_dev)
1871 cfqg->dev_leaf_weight = 0;
Tejun Heoe71357e2013-01-09 08:05:10 -08001872 if (!cfqg->dev_leaf_weight)
Arianna Avanzinie48453c2015-06-05 23:38:42 +02001873 cfqg->new_leaf_weight = cfqgd->leaf_weight;
Tejun Heoe71357e2013-01-09 08:05:10 -08001874 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001875 }
1876
Jens Axboeae994ea2015-06-20 10:26:50 -06001877out:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001878 spin_unlock_irq(&blkcg->lock);
Jens Axboeae994ea2015-06-20 10:26:50 -06001879 return ret;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001880}
1881
Tejun Heo182446d2013-08-08 20:11:24 -04001882static int cfq_set_weight(struct cgroup_subsys_state *css, struct cftype *cft,
1883 u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001884{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001885 return __cfq_set_weight(css, val, false, false, false);
Tejun Heoe71357e2013-01-09 08:05:10 -08001886}
1887
Tejun Heo182446d2013-08-08 20:11:24 -04001888static int cfq_set_leaf_weight(struct cgroup_subsys_state *css,
1889 struct cftype *cft, u64 val)
Tejun Heoe71357e2013-01-09 08:05:10 -08001890{
Tejun Heo69d7fde2015-08-18 14:55:36 -07001891 return __cfq_set_weight(css, val, false, false, true);
Tejun Heoe71357e2013-01-09 08:05:10 -08001892}
1893
Tejun Heo2da8ca82013-12-05 12:28:04 -05001894static int cfqg_print_stat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001895{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001896 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat,
1897 &blkcg_policy_cfq, seq_cft(sf)->private, false);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001898 return 0;
1899}
1900
Tejun Heo2da8ca82013-12-05 12:28:04 -05001901static int cfqg_print_rwstat(struct seq_file *sf, void *v)
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001902{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001903 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat,
1904 &blkcg_policy_cfq, seq_cft(sf)->private, true);
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001905 return 0;
1906}
1907
Tejun Heo43114012013-01-09 08:05:13 -08001908static u64 cfqg_prfill_stat_recursive(struct seq_file *sf,
1909 struct blkg_policy_data *pd, int off)
1910{
Tejun Heof12c74c2015-08-18 14:55:23 -07001911 u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd),
1912 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001913 return __blkg_prfill_u64(sf, pd, sum);
1914}
1915
1916static u64 cfqg_prfill_rwstat_recursive(struct seq_file *sf,
1917 struct blkg_policy_data *pd, int off)
1918{
Tejun Heof12c74c2015-08-18 14:55:23 -07001919 struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd),
1920 &blkcg_policy_cfq, off);
Tejun Heo43114012013-01-09 08:05:13 -08001921 return __blkg_prfill_rwstat(sf, pd, &sum);
1922}
1923
Tejun Heo2da8ca82013-12-05 12:28:04 -05001924static int cfqg_print_stat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001925{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001926 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1927 cfqg_prfill_stat_recursive, &blkcg_policy_cfq,
1928 seq_cft(sf)->private, false);
Tejun Heo43114012013-01-09 08:05:13 -08001929 return 0;
1930}
1931
Tejun Heo2da8ca82013-12-05 12:28:04 -05001932static int cfqg_print_rwstat_recursive(struct seq_file *sf, void *v)
Tejun Heo43114012013-01-09 08:05:13 -08001933{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001934 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1935 cfqg_prfill_rwstat_recursive, &blkcg_policy_cfq,
1936 seq_cft(sf)->private, true);
Tejun Heo43114012013-01-09 08:05:13 -08001937 return 0;
1938}
1939
Tejun Heo702747c2015-08-18 14:55:25 -07001940static u64 cfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd,
1941 int off)
1942{
1943 u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes);
1944
1945 return __blkg_prfill_u64(sf, pd, sum >> 9);
1946}
1947
1948static int cfqg_print_stat_sectors(struct seq_file *sf, void *v)
1949{
1950 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1951 cfqg_prfill_sectors, &blkcg_policy_cfq, 0, false);
1952 return 0;
1953}
1954
1955static u64 cfqg_prfill_sectors_recursive(struct seq_file *sf,
1956 struct blkg_policy_data *pd, int off)
1957{
1958 struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL,
1959 offsetof(struct blkcg_gq, stat_bytes));
1960 u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
1961 atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
1962
1963 return __blkg_prfill_u64(sf, pd, sum >> 9);
1964}
1965
1966static int cfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v)
1967{
1968 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1969 cfqg_prfill_sectors_recursive, &blkcg_policy_cfq, 0,
1970 false);
1971 return 0;
1972}
1973
Tejun Heo60c2bc22012-04-01 14:38:43 -07001974#ifdef CONFIG_DEBUG_BLK_CGROUP
Tejun Heof95a04a2012-04-16 13:57:26 -07001975static u64 cfqg_prfill_avg_queue_size(struct seq_file *sf,
1976 struct blkg_policy_data *pd, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001977{
Tejun Heof95a04a2012-04-16 13:57:26 -07001978 struct cfq_group *cfqg = pd_to_cfqg(pd);
Tejun Heo155fead2012-04-01 14:38:44 -07001979 u64 samples = blkg_stat_read(&cfqg->stats.avg_queue_size_samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001980 u64 v = 0;
1981
1982 if (samples) {
Tejun Heo155fead2012-04-01 14:38:44 -07001983 v = blkg_stat_read(&cfqg->stats.avg_queue_size_sum);
Anatol Pomozovf3cff252013-09-22 12:43:47 -06001984 v = div64_u64(v, samples);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001985 }
Tejun Heof95a04a2012-04-16 13:57:26 -07001986 __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001987 return 0;
1988}
1989
1990/* print avg_queue_size */
Tejun Heo2da8ca82013-12-05 12:28:04 -05001991static int cfqg_print_avg_queue_size(struct seq_file *sf, void *v)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001992{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001993 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
1994 cfqg_prfill_avg_queue_size, &blkcg_policy_cfq,
1995 0, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001996 return 0;
1997}
1998#endif /* CONFIG_DEBUG_BLK_CGROUP */
1999
Tejun Heo880f50e2015-08-18 14:55:30 -07002000static struct cftype cfq_blkcg_legacy_files[] = {
Tejun Heo1d3650f2013-01-09 08:05:11 -08002001 /* on root, weight is mapped to leaf_weight */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002002 {
2003 .name = "weight_device",
Tejun Heo1d3650f2013-01-09 08:05:11 -08002004 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002005 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002006 .write = cfqg_set_leaf_weight_device,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002007 },
2008 {
2009 .name = "weight",
2010 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002011 .seq_show = cfq_print_leaf_weight,
Tejun Heo1d3650f2013-01-09 08:05:11 -08002012 .write_u64 = cfq_set_leaf_weight,
2013 },
2014
2015 /* no such mapping necessary for !roots */
2016 {
2017 .name = "weight_device",
2018 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002019 .seq_show = cfqg_print_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002020 .write = cfqg_set_weight_device,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002021 },
2022 {
2023 .name = "weight",
Tejun Heoe71357e2013-01-09 08:05:10 -08002024 .flags = CFTYPE_NOT_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05002025 .seq_show = cfq_print_weight,
Tejun Heo3381cb82012-04-01 14:38:44 -07002026 .write_u64 = cfq_set_weight,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002027 },
Tejun Heo1d3650f2013-01-09 08:05:11 -08002028
2029 {
2030 .name = "leaf_weight_device",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002031 .seq_show = cfqg_print_leaf_weight_device,
Tejun Heo451af502014-05-13 12:16:21 -04002032 .write = cfqg_set_leaf_weight_device,
Tejun Heoe71357e2013-01-09 08:05:10 -08002033 },
2034 {
2035 .name = "leaf_weight",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002036 .seq_show = cfq_print_leaf_weight,
Tejun Heoe71357e2013-01-09 08:05:10 -08002037 .write_u64 = cfq_set_leaf_weight,
2038 },
2039
Tejun Heo43114012013-01-09 08:05:13 -08002040 /* statistics, covers only the tasks in the cfqg */
Tejun Heo60c2bc22012-04-01 14:38:43 -07002041 {
2042 .name = "time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002043 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002044 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002045 },
2046 {
2047 .name = "sectors",
Tejun Heo702747c2015-08-18 14:55:25 -07002048 .seq_show = cfqg_print_stat_sectors,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002049 },
2050 {
2051 .name = "io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07002052 .private = (unsigned long)&blkcg_policy_cfq,
2053 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002054 },
2055 {
2056 .name = "io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07002057 .private = (unsigned long)&blkcg_policy_cfq,
2058 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002059 },
2060 {
2061 .name = "io_service_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002062 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002063 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002064 },
2065 {
2066 .name = "io_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002067 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002068 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002069 },
2070 {
2071 .name = "io_merged",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002072 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002073 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002074 },
2075 {
2076 .name = "io_queued",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002077 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002078 .seq_show = cfqg_print_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002079 },
Tejun Heo43114012013-01-09 08:05:13 -08002080
2081 /* the same statictics which cover the cfqg and its descendants */
2082 {
2083 .name = "time_recursive",
2084 .private = offsetof(struct cfq_group, stats.time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002085 .seq_show = cfqg_print_stat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002086 },
2087 {
2088 .name = "sectors_recursive",
Tejun Heo702747c2015-08-18 14:55:25 -07002089 .seq_show = cfqg_print_stat_sectors_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002090 },
2091 {
2092 .name = "io_service_bytes_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002093 .private = (unsigned long)&blkcg_policy_cfq,
2094 .seq_show = blkg_print_stat_bytes_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002095 },
2096 {
2097 .name = "io_serviced_recursive",
Tejun Heo77ea7332015-08-18 14:55:24 -07002098 .private = (unsigned long)&blkcg_policy_cfq,
2099 .seq_show = blkg_print_stat_ios_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002100 },
2101 {
2102 .name = "io_service_time_recursive",
2103 .private = offsetof(struct cfq_group, stats.service_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002104 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002105 },
2106 {
2107 .name = "io_wait_time_recursive",
2108 .private = offsetof(struct cfq_group, stats.wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002109 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002110 },
2111 {
2112 .name = "io_merged_recursive",
2113 .private = offsetof(struct cfq_group, stats.merged),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002114 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002115 },
2116 {
2117 .name = "io_queued_recursive",
2118 .private = offsetof(struct cfq_group, stats.queued),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002119 .seq_show = cfqg_print_rwstat_recursive,
Tejun Heo43114012013-01-09 08:05:13 -08002120 },
Tejun Heo60c2bc22012-04-01 14:38:43 -07002121#ifdef CONFIG_DEBUG_BLK_CGROUP
2122 {
2123 .name = "avg_queue_size",
Tejun Heo2da8ca82013-12-05 12:28:04 -05002124 .seq_show = cfqg_print_avg_queue_size,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002125 },
2126 {
2127 .name = "group_wait_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002128 .private = offsetof(struct cfq_group, stats.group_wait_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002129 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002130 },
2131 {
2132 .name = "idle_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002133 .private = offsetof(struct cfq_group, stats.idle_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002134 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002135 },
2136 {
2137 .name = "empty_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002138 .private = offsetof(struct cfq_group, stats.empty_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002139 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002140 },
2141 {
2142 .name = "dequeue",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002143 .private = offsetof(struct cfq_group, stats.dequeue),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002144 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002145 },
2146 {
2147 .name = "unaccounted_time",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07002148 .private = offsetof(struct cfq_group, stats.unaccounted_time),
Tejun Heo2da8ca82013-12-05 12:28:04 -05002149 .seq_show = cfqg_print_stat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07002150 },
2151#endif /* CONFIG_DEBUG_BLK_CGROUP */
2152 { } /* terminate */
2153};
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002154
2155static int cfq_print_weight_on_dfl(struct seq_file *sf, void *v)
2156{
2157 struct blkcg *blkcg = css_to_blkcg(seq_css(sf));
2158 struct cfq_group_data *cgd = blkcg_to_cfqgd(blkcg);
2159
2160 seq_printf(sf, "default %u\n", cgd->weight);
2161 blkcg_print_blkgs(sf, blkcg, cfqg_prfill_weight_device,
2162 &blkcg_policy_cfq, 0, false);
2163 return 0;
2164}
2165
2166static ssize_t cfq_set_weight_on_dfl(struct kernfs_open_file *of,
2167 char *buf, size_t nbytes, loff_t off)
2168{
2169 char *endp;
2170 int ret;
2171 u64 v;
2172
2173 buf = strim(buf);
2174
2175 /* "WEIGHT" or "default WEIGHT" sets the default weight */
2176 v = simple_strtoull(buf, &endp, 0);
2177 if (*endp == '\0' || sscanf(buf, "default %llu", &v) == 1) {
Tejun Heo69d7fde2015-08-18 14:55:36 -07002178 ret = __cfq_set_weight(of_css(of), v, true, false, false);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07002179 return ret ?: nbytes;
2180 }
2181
2182 /* "MAJ:MIN WEIGHT" */
2183 return __cfqg_set_weight_device(of, buf, nbytes, off, true, false);
2184}
2185
2186static struct cftype cfq_blkcg_files[] = {
2187 {
2188 .name = "weight",
2189 .flags = CFTYPE_NOT_ON_ROOT,
2190 .seq_show = cfq_print_weight_on_dfl,
2191 .write = cfq_set_weight_on_dfl,
2192 },
2193 { } /* terminate */
2194};
2195
Vivek Goyal25fb5162009-12-03 12:59:46 -05002196#else /* GROUP_IOSCHED */
Tejun Heoae118892015-08-18 14:55:20 -07002197static struct cfq_group *cfq_lookup_cfqg(struct cfq_data *cfqd,
2198 struct blkcg *blkcg)
Vivek Goyal25fb5162009-12-03 12:59:46 -05002199{
Tejun Heof51b8022012-03-05 13:15:05 -08002200 return cfqd->root_group;
Vivek Goyal25fb5162009-12-03 12:59:46 -05002201}
Vivek Goyal7f1dc8a2010-04-21 17:44:16 +02002202
Vivek Goyal25fb5162009-12-03 12:59:46 -05002203static inline void
2204cfq_link_cfqq_cfqg(struct cfq_queue *cfqq, struct cfq_group *cfqg) {
2205 cfqq->cfqg = cfqg;
2206}
2207
2208#endif /* GROUP_IOSCHED */
2209
Jens Axboe498d3aa22007-04-26 12:54:48 +02002210/*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002211 * The cfqd->service_trees holds all pending cfq_queue's that have
Jens Axboe498d3aa22007-04-26 12:54:48 +02002212 * requests waiting to be processed. It is sorted in the order that
2213 * we will service the queues.
2214 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002215static void cfq_service_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02002216 bool add_front)
Jens Axboed9e76202007-04-20 14:27:50 +02002217{
Jens Axboe08717142008-01-28 11:38:15 +01002218 struct rb_node **p, *parent;
2219 struct cfq_queue *__cfqq;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002220 u64 rb_key;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002221 struct cfq_rb_root *st;
Jens Axboe498d3aa22007-04-26 12:54:48 +02002222 int left;
Vivek Goyaldae739e2009-12-03 12:59:45 -05002223 int new_cfqq = 1;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002224 u64 now = ktime_get_ns();
Vivek Goyalae30c282009-12-03 12:59:55 -05002225
Vivek Goyal34b98d02012-10-03 16:56:58 -04002226 st = st_for(cfqq->cfqg, cfqq_class(cfqq), cfqq_type(cfqq));
Jens Axboe08717142008-01-28 11:38:15 +01002227 if (cfq_class_idle(cfqq)) {
2228 rb_key = CFQ_IDLE_DELAY;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002229 parent = rb_last(&st->rb);
Jens Axboe08717142008-01-28 11:38:15 +01002230 if (parent && parent != &cfqq->rb_node) {
2231 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2232 rb_key += __cfqq->rb_key;
2233 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002234 rb_key += now;
Jens Axboe08717142008-01-28 11:38:15 +01002235 } else if (!add_front) {
Jens Axboeb9c89462009-10-06 20:53:44 +02002236 /*
2237 * Get our rb key offset. Subtract any residual slice
2238 * value carried from last service. A negative resid
2239 * count indicates slice overrun, and this should position
2240 * the next service time further away in the tree.
2241 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002242 rb_key = cfq_slice_offset(cfqd, cfqq) + now;
Jens Axboeb9c89462009-10-06 20:53:44 +02002243 rb_key -= cfqq->slice_resid;
Jens Axboeedd75ff2007-04-19 12:03:34 +02002244 cfqq->slice_resid = 0;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002245 } else {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002246 rb_key = -NSEC_PER_SEC;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002247 __cfqq = cfq_rb_first(st);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002248 rb_key += __cfqq ? __cfqq->rb_key : now;
Corrado Zoccolo48e025e2009-10-05 08:49:23 +02002249 }
Jens Axboed9e76202007-04-20 14:27:50 +02002250
2251 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
Vivek Goyaldae739e2009-12-03 12:59:45 -05002252 new_cfqq = 0;
Jens Axboe99f96282007-02-05 11:56:25 +01002253 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002254 * same position, nothing more to do
Jens Axboe99f96282007-02-05 11:56:25 +01002255 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002256 if (rb_key == cfqq->rb_key && cfqq->service_tree == st)
Jens Axboed9e76202007-04-20 14:27:50 +02002257 return;
Jens Axboe53b037442006-07-28 09:48:51 +02002258
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002259 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2260 cfqq->service_tree = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02002261 }
Jens Axboed9e76202007-04-20 14:27:50 +02002262
Jens Axboe498d3aa22007-04-26 12:54:48 +02002263 left = 1;
Jens Axboe08717142008-01-28 11:38:15 +01002264 parent = NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002265 cfqq->service_tree = st;
2266 p = &st->rb.rb_node;
Jens Axboed9e76202007-04-20 14:27:50 +02002267 while (*p) {
2268 parent = *p;
2269 __cfqq = rb_entry(parent, struct cfq_queue, rb_node);
2270
Jens Axboe0c534e02007-04-18 20:01:57 +02002271 /*
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002272 * sort by key, that represents service time.
Jens Axboe0c534e02007-04-18 20:01:57 +02002273 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002274 if (rb_key < __cfqq->rb_key)
Vivek Goyal1f23f122012-10-03 16:57:00 -04002275 p = &parent->rb_left;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002276 else {
Vivek Goyal1f23f122012-10-03 16:57:00 -04002277 p = &parent->rb_right;
Jens Axboecc09e292007-04-26 12:53:50 +02002278 left = 0;
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002279 }
Jens Axboed9e76202007-04-20 14:27:50 +02002280 }
2281
Jens Axboecc09e292007-04-26 12:53:50 +02002282 if (left)
Vivek Goyal34b98d02012-10-03 16:56:58 -04002283 st->left = &cfqq->rb_node;
Jens Axboecc09e292007-04-26 12:53:50 +02002284
Jens Axboed9e76202007-04-20 14:27:50 +02002285 cfqq->rb_key = rb_key;
2286 rb_link_node(&cfqq->rb_node, parent, p);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002287 rb_insert_color(&cfqq->rb_node, &st->rb);
2288 st->count++;
Namhyung Kim20359f22011-05-24 10:23:22 +02002289 if (add_front || !new_cfqq)
Vivek Goyaldae739e2009-12-03 12:59:45 -05002290 return;
Justin TerAvest8184f932011-03-17 16:12:36 +01002291 cfq_group_notify_queue_add(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Jens Axboea36e71f2009-04-15 12:15:11 +02002294static struct cfq_queue *
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002295cfq_prio_tree_lookup(struct cfq_data *cfqd, struct rb_root *root,
2296 sector_t sector, struct rb_node **ret_parent,
2297 struct rb_node ***rb_link)
Jens Axboea36e71f2009-04-15 12:15:11 +02002298{
Jens Axboea36e71f2009-04-15 12:15:11 +02002299 struct rb_node **p, *parent;
2300 struct cfq_queue *cfqq = NULL;
2301
2302 parent = NULL;
2303 p = &root->rb_node;
2304 while (*p) {
2305 struct rb_node **n;
2306
2307 parent = *p;
2308 cfqq = rb_entry(parent, struct cfq_queue, p_node);
2309
2310 /*
2311 * Sort strictly based on sector. Smallest to the left,
2312 * largest to the right.
2313 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002314 if (sector > blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002315 n = &(*p)->rb_right;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002316 else if (sector < blk_rq_pos(cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002317 n = &(*p)->rb_left;
2318 else
2319 break;
2320 p = n;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002321 cfqq = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002322 }
2323
2324 *ret_parent = parent;
2325 if (rb_link)
2326 *rb_link = p;
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002327 return cfqq;
Jens Axboea36e71f2009-04-15 12:15:11 +02002328}
2329
2330static void cfq_prio_tree_add(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2331{
Jens Axboea36e71f2009-04-15 12:15:11 +02002332 struct rb_node **p, *parent;
2333 struct cfq_queue *__cfqq;
2334
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002335 if (cfqq->p_root) {
2336 rb_erase(&cfqq->p_node, cfqq->p_root);
2337 cfqq->p_root = NULL;
2338 }
Jens Axboea36e71f2009-04-15 12:15:11 +02002339
2340 if (cfq_class_idle(cfqq))
2341 return;
2342 if (!cfqq->next_rq)
2343 return;
2344
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002345 cfqq->p_root = &cfqd->prio_trees[cfqq->org_ioprio];
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002346 __cfqq = cfq_prio_tree_lookup(cfqd, cfqq->p_root,
2347 blk_rq_pos(cfqq->next_rq), &parent, &p);
Jens Axboe3ac6c9f2009-04-23 12:14:56 +02002348 if (!__cfqq) {
2349 rb_link_node(&cfqq->p_node, parent, p);
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002350 rb_insert_color(&cfqq->p_node, cfqq->p_root);
2351 } else
2352 cfqq->p_root = NULL;
Jens Axboea36e71f2009-04-15 12:15:11 +02002353}
2354
Jens Axboe498d3aa22007-04-26 12:54:48 +02002355/*
2356 * Update cfqq's position in the service tree.
2357 */
Jens Axboeedd75ff2007-04-19 12:03:34 +02002358static void cfq_resort_rr_list(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002359{
Jens Axboe6d048f52007-04-25 12:44:27 +02002360 /*
2361 * Resorting requires the cfqq to be on the RR list already.
2362 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002363 if (cfq_cfqq_on_rr(cfqq)) {
Jens Axboeedd75ff2007-04-19 12:03:34 +02002364 cfq_service_tree_add(cfqd, cfqq, 0);
Jens Axboea36e71f2009-04-15 12:15:11 +02002365 cfq_prio_tree_add(cfqd, cfqq);
2366 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002367}
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
2370 * add to busy list of queues for service, trying to be fair in ordering
Jens Axboe22e2c502005-06-27 10:55:12 +02002371 * the pending list according to last request service
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
Jens Axboefebffd62008-01-28 13:19:43 +01002373static void cfq_add_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Jens Axboe7b679132008-05-30 12:23:07 +02002375 cfq_log_cfqq(cfqd, cfqq, "add_to_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002376 BUG_ON(cfq_cfqq_on_rr(cfqq));
2377 cfq_mark_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 cfqd->busy_queues++;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002379 if (cfq_cfqq_sync(cfqq))
2380 cfqd->busy_sync_queues++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Jens Axboeedd75ff2007-04-19 12:03:34 +02002382 cfq_resort_rr_list(cfqd, cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
Jens Axboe498d3aa22007-04-26 12:54:48 +02002385/*
2386 * Called when the cfqq no longer has requests pending, remove it from
2387 * the service tree.
2388 */
Jens Axboefebffd62008-01-28 13:19:43 +01002389static void cfq_del_cfqq_rr(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Jens Axboe7b679132008-05-30 12:23:07 +02002391 cfq_log_cfqq(cfqd, cfqq, "del_from_rr");
Jens Axboe3b181522005-06-27 10:56:24 +02002392 BUG_ON(!cfq_cfqq_on_rr(cfqq));
2393 cfq_clear_cfqq_on_rr(cfqq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Corrado Zoccoloaa6f6a32009-10-26 22:44:33 +01002395 if (!RB_EMPTY_NODE(&cfqq->rb_node)) {
2396 cfq_rb_erase(&cfqq->rb_node, cfqq->service_tree);
2397 cfqq->service_tree = NULL;
2398 }
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002399 if (cfqq->p_root) {
2400 rb_erase(&cfqq->p_node, cfqq->p_root);
2401 cfqq->p_root = NULL;
2402 }
Jens Axboed9e76202007-04-20 14:27:50 +02002403
Justin TerAvest8184f932011-03-17 16:12:36 +01002404 cfq_group_notify_queue_del(cfqd, cfqq->cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 BUG_ON(!cfqd->busy_queues);
2406 cfqd->busy_queues--;
Shaohua Lief8a41d2011-03-07 09:26:29 +01002407 if (cfq_cfqq_sync(cfqq))
2408 cfqd->busy_sync_queues--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411/*
2412 * rb tree support functions
2413 */
Jens Axboefebffd62008-01-28 13:19:43 +01002414static void cfq_del_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Jens Axboe5e705372006-07-13 12:39:25 +02002416 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe5e705372006-07-13 12:39:25 +02002417 const int sync = rq_is_sync(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Jens Axboeb4878f22005-10-20 16:42:29 +02002419 BUG_ON(!cfqq->queued[sync]);
2420 cfqq->queued[sync]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Jens Axboe5e705372006-07-13 12:39:25 +02002422 elv_rb_del(&cfqq->sort_list, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Vivek Goyalf04a6422009-12-03 12:59:40 -05002424 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list)) {
2425 /*
2426 * Queue will be deleted from service tree when we actually
2427 * expire it later. Right now just remove it from prio tree
2428 * as it is empty.
2429 */
2430 if (cfqq->p_root) {
2431 rb_erase(&cfqq->p_node, cfqq->p_root);
2432 cfqq->p_root = NULL;
2433 }
2434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Jens Axboe5e705372006-07-13 12:39:25 +02002437static void cfq_add_rq_rb(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Jens Axboe5e705372006-07-13 12:39:25 +02002439 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct cfq_data *cfqd = cfqq->cfqd;
Jeff Moyer796d5112011-06-02 21:19:05 +02002441 struct request *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Jens Axboe5380a102006-07-13 12:37:56 +02002443 cfqq->queued[rq_is_sync(rq)]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jeff Moyer796d5112011-06-02 21:19:05 +02002445 elv_rb_add(&cfqq->sort_list, rq);
Jens Axboe5fccbf62006-10-31 14:21:55 +01002446
2447 if (!cfq_cfqq_on_rr(cfqq))
2448 cfq_add_cfqq_rr(cfqd, cfqq);
Jens Axboe5044eed2007-04-25 11:53:48 +02002449
2450 /*
2451 * check if this request is a better next-serve candidate
2452 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002453 prev = cfqq->next_rq;
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002454 cfqq->next_rq = cfq_choose_req(cfqd, cfqq->next_rq, rq, cfqd->last_position);
Jens Axboea36e71f2009-04-15 12:15:11 +02002455
2456 /*
2457 * adjust priority tree position, if ->next_rq changes
2458 */
2459 if (prev != cfqq->next_rq)
2460 cfq_prio_tree_add(cfqd, cfqq);
2461
Jens Axboe5044eed2007-04-25 11:53:48 +02002462 BUG_ON(!cfqq->next_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Jens Axboefebffd62008-01-28 13:19:43 +01002465static void cfq_reposition_rq_rb(struct cfq_queue *cfqq, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Jens Axboe5380a102006-07-13 12:37:56 +02002467 elv_rb_del(&cfqq->sort_list, rq);
2468 cfqq->queued[rq_is_sync(rq)]--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002469 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Jens Axboe5e705372006-07-13 12:39:25 +02002470 cfq_add_rq_rb(rq);
Tejun Heo155fead2012-04-01 14:38:44 -07002471 cfqg_stats_update_io_add(RQ_CFQG(rq), cfqq->cfqd->serving_group,
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002472 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Jens Axboe206dc692006-03-28 13:03:44 +02002475static struct request *
2476cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477{
Jens Axboe206dc692006-03-28 13:03:44 +02002478 struct task_struct *tsk = current;
Tejun Heoc5869802011-12-14 00:33:41 +01002479 struct cfq_io_cq *cic;
Jens Axboe206dc692006-03-28 13:03:44 +02002480 struct cfq_queue *cfqq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Jens Axboe4ac845a2008-01-24 08:44:49 +01002482 cic = cfq_cic_lookup(cfqd, tsk->io_context);
Vasily Tarasov91fac312007-04-25 12:29:51 +02002483 if (!cic)
2484 return NULL;
2485
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002486 cfqq = cic_to_cfqq(cic, op_is_sync(bio->bi_opf));
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002487 if (cfqq)
2488 return elv_rb_find(&cfqq->sort_list, bio_end_sector(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return NULL;
2491}
2492
Jens Axboe165125e2007-07-24 09:28:11 +02002493static void cfq_activate_request(struct request_queue *q, struct request *rq)
Jens Axboeb4878f22005-10-20 16:42:29 +02002494{
2495 struct cfq_data *cfqd = q->elevator->elevator_data;
2496
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002497 cfqd->rq_in_driver++;
Jens Axboe7b679132008-05-30 12:23:07 +02002498 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "activate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002499 cfqd->rq_in_driver);
Jens Axboe25776e32006-06-01 10:12:26 +02002500
Tejun Heo5b936292009-05-07 22:24:38 +09002501 cfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Jens Axboeb4878f22005-10-20 16:42:29 +02002502}
2503
Jens Axboe165125e2007-07-24 09:28:11 +02002504static void cfq_deactivate_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Jens Axboe22e2c502005-06-27 10:55:12 +02002506 struct cfq_data *cfqd = q->elevator->elevator_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002508 WARN_ON(!cfqd->rq_in_driver);
2509 cfqd->rq_in_driver--;
Jens Axboe7b679132008-05-30 12:23:07 +02002510 cfq_log_cfqq(cfqd, RQ_CFQQ(rq), "deactivate rq, drv=%d",
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01002511 cfqd->rq_in_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Jens Axboeb4878f22005-10-20 16:42:29 +02002514static void cfq_remove_request(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Jens Axboe5e705372006-07-13 12:39:25 +02002516 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe21183b02006-07-13 12:33:14 +02002517
Jens Axboe5e705372006-07-13 12:39:25 +02002518 if (cfqq->next_rq == rq)
2519 cfqq->next_rq = cfq_find_next_rq(cfqq->cfqd, cfqq, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Jens Axboeb4878f22005-10-20 16:42:29 +02002521 list_del_init(&rq->queuelist);
Jens Axboe5e705372006-07-13 12:39:25 +02002522 cfq_del_rq_rb(rq);
Jens Axboe374f84a2006-07-23 01:42:19 +02002523
Aaron Carroll45333d52008-08-26 15:52:36 +02002524 cfqq->cfqd->rq_queued--;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002525 cfqg_stats_update_io_remove(RQ_CFQG(rq), rq->cmd_flags);
Christoph Hellwig65299a32011-08-23 14:50:29 +02002526 if (rq->cmd_flags & REQ_PRIO) {
2527 WARN_ON(!cfqq->prio_pending);
2528 cfqq->prio_pending--;
Jens Axboeb53d1ed2011-08-19 08:34:48 +02002529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002532static enum elv_merge cfq_merge(struct request_queue *q, struct request **req,
Jens Axboe165125e2007-07-24 09:28:11 +02002533 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 struct cfq_data *cfqd = q->elevator->elevator_data;
2536 struct request *__rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Jens Axboe206dc692006-03-28 13:03:44 +02002538 __rq = cfq_find_rq_fmerge(cfqd, bio);
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002539 if (__rq && elv_bio_merge_ok(__rq, bio)) {
Jens Axboe98170642006-07-28 09:23:08 +02002540 *req = __rq;
2541 return ELEVATOR_FRONT_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
2543
2544 return ELEVATOR_NO_MERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Jens Axboe165125e2007-07-24 09:28:11 +02002547static void cfq_merged_request(struct request_queue *q, struct request *req,
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01002548 enum elv_merge type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Jens Axboe21183b02006-07-13 12:33:14 +02002550 if (type == ELEVATOR_FRONT_MERGE) {
Jens Axboe5e705372006-07-13 12:39:25 +02002551 struct cfq_queue *cfqq = RQ_CFQQ(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Jens Axboe5e705372006-07-13 12:39:25 +02002553 cfq_reposition_rq_rb(cfqq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Divyesh Shah812d4022010-04-08 21:14:23 -07002557static void cfq_bio_merged(struct request_queue *q, struct request *req,
2558 struct bio *bio)
2559{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002560 cfqg_stats_update_io_merged(RQ_CFQG(req), bio->bi_opf);
Divyesh Shah812d4022010-04-08 21:14:23 -07002561}
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static void
Jens Axboe165125e2007-07-24 09:28:11 +02002564cfq_merged_requests(struct request_queue *q, struct request *rq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct request *next)
2566{
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002567 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002568 struct cfq_data *cfqd = q->elevator->elevator_data;
2569
Jens Axboe22e2c502005-06-27 10:55:12 +02002570 /*
2571 * reposition in fifo if next is older than rq
2572 */
2573 if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002574 next->fifo_time < rq->fifo_time &&
Shaohua Li3d106fba2012-11-06 12:39:51 +01002575 cfqq == RQ_CFQQ(next)) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002576 list_move(&rq->queuelist, &next->queuelist);
Jan Kara8b4922d2014-02-24 16:39:52 +01002577 rq->fifo_time = next->fifo_time;
Jens Axboe30996f42009-10-05 11:03:39 +02002578 }
Jens Axboe22e2c502005-06-27 10:55:12 +02002579
Corrado Zoccolocf7c25c2009-11-08 17:16:46 +01002580 if (cfqq->next_rq == next)
2581 cfqq->next_rq = rq;
Jens Axboeb4878f22005-10-20 16:42:29 +02002582 cfq_remove_request(next);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002583 cfqg_stats_update_io_merged(RQ_CFQG(rq), next->cmd_flags);
Shaohua Li4a0b75c2011-12-16 14:00:22 +01002584
2585 cfqq = RQ_CFQQ(next);
2586 /*
2587 * all requests of this queue are merged to other queues, delete it
2588 * from the service tree. If it's the active_queue,
2589 * cfq_dispatch_requests() will choose to expire it or do idle
2590 */
2591 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list) &&
2592 cfqq != cfqd->active_queue)
2593 cfq_del_cfqq_rr(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002594}
2595
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002596static int cfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2597 struct bio *bio)
Jens Axboeda775262006-12-20 11:04:12 +01002598{
2599 struct cfq_data *cfqd = q->elevator->elevator_data;
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002600 bool is_sync = op_is_sync(bio->bi_opf);
Tejun Heoc5869802011-12-14 00:33:41 +01002601 struct cfq_io_cq *cic;
Jens Axboeda775262006-12-20 11:04:12 +01002602 struct cfq_queue *cfqq;
Jens Axboeda775262006-12-20 11:04:12 +01002603
2604 /*
Jens Axboeec8acb62007-01-02 18:32:11 +01002605 * Disallow merge of a sync bio into an async request.
Jens Axboeda775262006-12-20 11:04:12 +01002606 */
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002607 if (is_sync && !rq_is_sync(rq))
Jens Axboea6151c32009-10-07 20:02:57 +02002608 return false;
Jens Axboeda775262006-12-20 11:04:12 +01002609
2610 /*
Tejun Heof1a4f4d2011-12-14 00:33:39 +01002611 * Lookup the cfqq that this bio will be queued with and allow
Tejun Heo07c2bd32012-02-08 09:19:42 +01002612 * merge only if rq is queued there.
Jens Axboeda775262006-12-20 11:04:12 +01002613 */
Tejun Heo07c2bd32012-02-08 09:19:42 +01002614 cic = cfq_cic_lookup(cfqd, current->io_context);
2615 if (!cic)
2616 return false;
Jens Axboe719d3402006-12-22 09:38:53 +01002617
Christoph Hellwigaa39ebd2016-11-01 07:40:02 -06002618 cfqq = cic_to_cfqq(cic, is_sync);
Jens Axboea6151c32009-10-07 20:02:57 +02002619 return cfqq == RQ_CFQQ(rq);
Jens Axboeda775262006-12-20 11:04:12 +01002620}
2621
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07002622static int cfq_allow_rq_merge(struct request_queue *q, struct request *rq,
2623 struct request *next)
2624{
2625 return RQ_CFQQ(rq) == RQ_CFQQ(next);
2626}
2627
Divyesh Shah812df482010-04-08 21:15:35 -07002628static inline void cfq_del_timer(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2629{
Jan Kara91148322016-06-08 15:11:39 +02002630 hrtimer_try_to_cancel(&cfqd->idle_slice_timer);
Tejun Heo155fead2012-04-01 14:38:44 -07002631 cfqg_stats_update_idle_time(cfqq->cfqg);
Divyesh Shah812df482010-04-08 21:15:35 -07002632}
2633
Jens Axboefebffd62008-01-28 13:19:43 +01002634static void __cfq_set_active_queue(struct cfq_data *cfqd,
2635 struct cfq_queue *cfqq)
Jens Axboe22e2c502005-06-27 10:55:12 +02002636{
2637 if (cfqq) {
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002638 cfq_log_cfqq(cfqd, cfqq, "set_active wl_class:%d wl_type:%d",
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04002639 cfqd->serving_wl_class, cfqd->serving_wl_type);
Tejun Heo155fead2012-04-01 14:38:44 -07002640 cfqg_stats_update_avg_queue_size(cfqq->cfqg);
Justin TerAvest62a37f62011-03-23 08:25:44 +01002641 cfqq->slice_start = 0;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002642 cfqq->dispatch_start = ktime_get_ns();
Justin TerAvest62a37f62011-03-23 08:25:44 +01002643 cfqq->allocated_slice = 0;
2644 cfqq->slice_end = 0;
2645 cfqq->slice_dispatch = 0;
2646 cfqq->nr_sectors = 0;
2647
2648 cfq_clear_cfqq_wait_request(cfqq);
2649 cfq_clear_cfqq_must_dispatch(cfqq);
2650 cfq_clear_cfqq_must_alloc_slice(cfqq);
2651 cfq_clear_cfqq_fifo_expire(cfqq);
2652 cfq_mark_cfqq_slice_new(cfqq);
2653
2654 cfq_del_timer(cfqd, cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002655 }
2656
2657 cfqd->active_queue = cfqq;
2658}
2659
2660/*
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002661 * current cfqq expired its slice (or was too idle), select new one
2662 */
2663static void
2664__cfq_slice_expired(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Vivek Goyale5ff0822010-04-26 19:25:11 +02002665 bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002666{
Jens Axboe7b679132008-05-30 12:23:07 +02002667 cfq_log_cfqq(cfqd, cfqq, "slice expired t=%d", timed_out);
2668
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002669 if (cfq_cfqq_wait_request(cfqq))
Divyesh Shah812df482010-04-08 21:15:35 -07002670 cfq_del_timer(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002671
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002672 cfq_clear_cfqq_wait_request(cfqq);
Vivek Goyalf75edf22009-12-03 12:59:53 -05002673 cfq_clear_cfqq_wait_busy(cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002674
2675 /*
Shaohua Liae54abe2010-02-05 13:11:45 +01002676 * If this cfqq is shared between multiple processes, check to
2677 * make sure that those processes are still issuing I/Os within
2678 * the mean seek distance. If not, it may be time to break the
2679 * queues apart again.
2680 */
2681 if (cfq_cfqq_coop(cfqq) && CFQQ_SEEKY(cfqq))
2682 cfq_mark_cfqq_split_coop(cfqq);
2683
2684 /*
Jens Axboe6084cdd2007-04-23 08:25:00 +02002685 * store what was left of this slice, if the queue idled/timed out
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002686 */
Shaohua Lic553f8e2011-01-14 08:41:03 +01002687 if (timed_out) {
2688 if (cfq_cfqq_slice_new(cfqq))
Vivek Goyalba5bd522011-01-19 08:25:02 -07002689 cfqq->slice_resid = cfq_scaled_cfqq_slice(cfqd, cfqq);
Shaohua Lic553f8e2011-01-14 08:41:03 +01002690 else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002691 cfqq->slice_resid = cfqq->slice_end - ktime_get_ns();
Jan Kara93fdf142016-06-28 09:04:00 +02002692 cfq_log_cfqq(cfqd, cfqq, "resid=%lld", cfqq->slice_resid);
Jens Axboe7b679132008-05-30 12:23:07 +02002693 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002694
Vivek Goyale5ff0822010-04-26 19:25:11 +02002695 cfq_group_served(cfqd, cfqq->cfqg, cfqq);
Vivek Goyaldae739e2009-12-03 12:59:45 -05002696
Vivek Goyalf04a6422009-12-03 12:59:40 -05002697 if (cfq_cfqq_on_rr(cfqq) && RB_EMPTY_ROOT(&cfqq->sort_list))
2698 cfq_del_cfqq_rr(cfqd, cfqq);
2699
Jens Axboeedd75ff2007-04-19 12:03:34 +02002700 cfq_resort_rr_list(cfqd, cfqq);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002701
2702 if (cfqq == cfqd->active_queue)
2703 cfqd->active_queue = NULL;
2704
2705 if (cfqd->active_cic) {
Tejun Heo11a31222012-02-07 07:51:30 +01002706 put_io_context(cfqd->active_cic->icq.ioc);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002707 cfqd->active_cic = NULL;
2708 }
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002709}
2710
Vivek Goyale5ff0822010-04-26 19:25:11 +02002711static inline void cfq_slice_expired(struct cfq_data *cfqd, bool timed_out)
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002712{
2713 struct cfq_queue *cfqq = cfqd->active_queue;
2714
2715 if (cfqq)
Vivek Goyale5ff0822010-04-26 19:25:11 +02002716 __cfq_slice_expired(cfqd, cfqq, timed_out);
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002717}
2718
Jens Axboe498d3aa22007-04-26 12:54:48 +02002719/*
2720 * Get next queue for service. Unless we have a queue preemption,
2721 * we'll simply select the first cfqq in the service tree.
2722 */
Jens Axboe6d048f52007-04-25 12:44:27 +02002723static struct cfq_queue *cfq_get_next_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002724{
Vivek Goyal34b98d02012-10-03 16:56:58 -04002725 struct cfq_rb_root *st = st_for(cfqd->serving_group,
2726 cfqd->serving_wl_class, cfqd->serving_wl_type);
Jens Axboeedd75ff2007-04-19 12:03:34 +02002727
Vivek Goyalf04a6422009-12-03 12:59:40 -05002728 if (!cfqd->rq_queued)
2729 return NULL;
2730
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002731 /* There is nothing to dispatch */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002732 if (!st)
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05002733 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002734 if (RB_EMPTY_ROOT(&st->rb))
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002735 return NULL;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002736 return cfq_rb_first(st);
Jens Axboe6d048f52007-04-25 12:44:27 +02002737}
2738
Vivek Goyalf04a6422009-12-03 12:59:40 -05002739static struct cfq_queue *cfq_get_next_queue_forced(struct cfq_data *cfqd)
2740{
Vivek Goyal25fb5162009-12-03 12:59:46 -05002741 struct cfq_group *cfqg;
Vivek Goyalf04a6422009-12-03 12:59:40 -05002742 struct cfq_queue *cfqq;
2743 int i, j;
2744 struct cfq_rb_root *st;
2745
2746 if (!cfqd->rq_queued)
2747 return NULL;
2748
Vivek Goyal25fb5162009-12-03 12:59:46 -05002749 cfqg = cfq_get_next_cfqg(cfqd);
2750 if (!cfqg)
2751 return NULL;
2752
Markus Elfring1cf41752017-01-21 22:44:07 +01002753 for_each_cfqg_st(cfqg, i, j, st) {
2754 cfqq = cfq_rb_first(st);
2755 if (cfqq)
Vivek Goyalf04a6422009-12-03 12:59:40 -05002756 return cfqq;
Markus Elfring1cf41752017-01-21 22:44:07 +01002757 }
Vivek Goyalf04a6422009-12-03 12:59:40 -05002758 return NULL;
2759}
2760
Jens Axboe498d3aa22007-04-26 12:54:48 +02002761/*
2762 * Get and set a new active queue for service.
2763 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002764static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
2765 struct cfq_queue *cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002766{
Jens Axboee00ef792009-11-04 08:54:55 +01002767 if (!cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002768 cfqq = cfq_get_next_queue(cfqd);
Jens Axboe6d048f52007-04-25 12:44:27 +02002769
Jens Axboe22e2c502005-06-27 10:55:12 +02002770 __cfq_set_active_queue(cfqd, cfqq);
Jens Axboe3b181522005-06-27 10:56:24 +02002771 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02002772}
2773
Jens Axboed9e76202007-04-20 14:27:50 +02002774static inline sector_t cfq_dist_from_last(struct cfq_data *cfqd,
2775 struct request *rq)
2776{
Tejun Heo83096eb2009-05-07 22:24:39 +09002777 if (blk_rq_pos(rq) >= cfqd->last_position)
2778 return blk_rq_pos(rq) - cfqd->last_position;
Jens Axboed9e76202007-04-20 14:27:50 +02002779 else
Tejun Heo83096eb2009-05-07 22:24:39 +09002780 return cfqd->last_position - blk_rq_pos(rq);
Jens Axboed9e76202007-04-20 14:27:50 +02002781}
2782
Jeff Moyerb2c18e12009-10-23 17:14:49 -04002783static inline int cfq_rq_close(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Shaohua Lie9ce3352010-03-19 08:03:04 +01002784 struct request *rq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002785{
Shaohua Lie9ce3352010-03-19 08:03:04 +01002786 return cfq_dist_from_last(cfqd, rq) <= CFQQ_CLOSE_THR;
Jens Axboe6d048f52007-04-25 12:44:27 +02002787}
2788
Jens Axboea36e71f2009-04-15 12:15:11 +02002789static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
2790 struct cfq_queue *cur_cfqq)
Jens Axboe6d048f52007-04-25 12:44:27 +02002791{
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002792 struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
Jens Axboea36e71f2009-04-15 12:15:11 +02002793 struct rb_node *parent, *node;
2794 struct cfq_queue *__cfqq;
2795 sector_t sector = cfqd->last_position;
2796
2797 if (RB_EMPTY_ROOT(root))
2798 return NULL;
2799
2800 /*
2801 * First, if we find a request starting at the end of the last
2802 * request, choose it.
2803 */
Jens Axboef2d1f0a2009-04-23 12:19:38 +02002804 __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
Jens Axboea36e71f2009-04-15 12:15:11 +02002805 if (__cfqq)
2806 return __cfqq;
2807
2808 /*
2809 * If the exact sector wasn't found, the parent of the NULL leaf
2810 * will contain the closest sector.
2811 */
2812 __cfqq = rb_entry(parent, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002813 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002814 return __cfqq;
2815
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002816 if (blk_rq_pos(__cfqq->next_rq) < sector)
Jens Axboea36e71f2009-04-15 12:15:11 +02002817 node = rb_next(&__cfqq->p_node);
2818 else
2819 node = rb_prev(&__cfqq->p_node);
2820 if (!node)
2821 return NULL;
2822
2823 __cfqq = rb_entry(node, struct cfq_queue, p_node);
Shaohua Lie9ce3352010-03-19 08:03:04 +01002824 if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
Jens Axboea36e71f2009-04-15 12:15:11 +02002825 return __cfqq;
2826
2827 return NULL;
2828}
2829
2830/*
2831 * cfqd - obvious
2832 * cur_cfqq - passed in so that we don't decide that the current queue is
2833 * closely cooperating with itself.
2834 *
2835 * So, basically we're assuming that that cur_cfqq has dispatched at least
2836 * one request, and that cfqd->last_position reflects a position on the disk
2837 * associated with the I/O issued by cur_cfqq. I'm not sure this is a valid
2838 * assumption.
2839 */
2840static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
Jeff Moyerb3b6d042009-10-23 17:14:51 -04002841 struct cfq_queue *cur_cfqq)
Jens Axboea36e71f2009-04-15 12:15:11 +02002842{
2843 struct cfq_queue *cfqq;
2844
Divyesh Shah39c01b22010-03-25 15:45:57 +01002845 if (cfq_class_idle(cur_cfqq))
2846 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002847 if (!cfq_cfqq_sync(cur_cfqq))
2848 return NULL;
2849 if (CFQQ_SEEKY(cur_cfqq))
2850 return NULL;
2851
Jens Axboea36e71f2009-04-15 12:15:11 +02002852 /*
Gui Jianfengb9d8f4c2009-12-08 08:54:17 +01002853 * Don't search priority tree if it's the only queue in the group.
2854 */
2855 if (cur_cfqq->cfqg->nr_cfqq == 1)
2856 return NULL;
2857
2858 /*
Jens Axboed9e76202007-04-20 14:27:50 +02002859 * We should notice if some of the queues are cooperating, eg
2860 * working closely on the same area of the disk. In that case,
2861 * we can group them together and don't waste time idling.
Jens Axboe6d048f52007-04-25 12:44:27 +02002862 */
Jens Axboea36e71f2009-04-15 12:15:11 +02002863 cfqq = cfqq_close(cfqd, cur_cfqq);
2864 if (!cfqq)
2865 return NULL;
2866
Vivek Goyal8682e1f2009-12-03 12:59:50 -05002867 /* If new queue belongs to different cfq_group, don't choose it */
2868 if (cur_cfqq->cfqg != cfqq->cfqg)
2869 return NULL;
2870
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002871 /*
2872 * It only makes sense to merge sync queues.
2873 */
2874 if (!cfq_cfqq_sync(cfqq))
2875 return NULL;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04002876 if (CFQQ_SEEKY(cfqq))
2877 return NULL;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04002878
Corrado Zoccoloc0324a02009-10-27 19:16:03 +01002879 /*
2880 * Do not merge queues of different priority classes
2881 */
2882 if (cfq_class_rt(cfqq) != cfq_class_rt(cur_cfqq))
2883 return NULL;
2884
Jens Axboea36e71f2009-04-15 12:15:11 +02002885 return cfqq;
Jens Axboe6d048f52007-04-25 12:44:27 +02002886}
2887
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002888/*
2889 * Determine whether we should enforce idle window for this queue.
2890 */
2891
2892static bool cfq_should_idle(struct cfq_data *cfqd, struct cfq_queue *cfqq)
2893{
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002894 enum wl_class_t wl_class = cfqq_class(cfqq);
Vivek Goyal34b98d02012-10-03 16:56:58 -04002895 struct cfq_rb_root *st = cfqq->service_tree;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002896
Vivek Goyal34b98d02012-10-03 16:56:58 -04002897 BUG_ON(!st);
2898 BUG_ON(!st->count);
Vivek Goyalf04a6422009-12-03 12:59:40 -05002899
Vivek Goyalb6508c12010-08-23 12:23:33 +02002900 if (!cfqd->cfq_slice_idle)
2901 return false;
2902
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002903 /* We never do for idle class queues. */
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04002904 if (wl_class == IDLE_WORKLOAD)
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002905 return false;
2906
2907 /* We do for queues that were marked with idle window flag. */
Shaohua Li3c764b72009-12-04 13:12:06 +01002908 if (cfq_cfqq_idle_window(cfqq) &&
2909 !(blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag))
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002910 return true;
2911
2912 /*
2913 * Otherwise, we do only if they are the last ones
2914 * in their service tree.
2915 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04002916 if (st->count == 1 && cfq_cfqq_sync(cfqq) &&
2917 !cfq_io_thinktime_big(cfqd, &st->ttime, false))
Shaohua Lic1e44752010-11-08 15:01:02 +01002918 return true;
Vivek Goyal34b98d02012-10-03 16:56:58 -04002919 cfq_log_cfqq(cfqd, cfqq, "Not idling. st->count:%d", st->count);
Shaohua Lic1e44752010-11-08 15:01:02 +01002920 return false;
Corrado Zoccoloa6d44e92009-10-26 22:45:11 +01002921}
2922
Jens Axboe6d048f52007-04-25 12:44:27 +02002923static void cfq_arm_slice_timer(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02002924{
Jens Axboe17926692007-01-19 11:59:30 +11002925 struct cfq_queue *cfqq = cfqd->active_queue;
Jan Karae7954212016-01-12 16:24:15 +01002926 struct cfq_rb_root *st = cfqq->service_tree;
Tejun Heoc5869802011-12-14 00:33:41 +01002927 struct cfq_io_cq *cic;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002928 u64 sl, group_idle = 0;
2929 u64 now = ktime_get_ns();
Jens Axboe7b14e3b2006-02-28 09:35:11 +01002930
Jens Axboea68bbddba2008-09-24 13:03:33 +02002931 /*
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002932 * SSD device without seek penalty, disable idling. But only do so
2933 * for devices that support queuing, otherwise we still have a problem
2934 * with sync vs async workloads.
Jens Axboea68bbddba2008-09-24 13:03:33 +02002935 */
Jens Axboef7d7b7a2008-09-25 11:37:50 +02002936 if (blk_queue_nonrot(cfqd->queue) && cfqd->hw_tag)
Jens Axboea68bbddba2008-09-24 13:03:33 +02002937 return;
2938
Jens Axboedd67d052006-06-21 09:36:18 +02002939 WARN_ON(!RB_EMPTY_ROOT(&cfqq->sort_list));
Jens Axboe6d048f52007-04-25 12:44:27 +02002940 WARN_ON(cfq_cfqq_slice_new(cfqq));
Jens Axboe22e2c502005-06-27 10:55:12 +02002941
2942 /*
2943 * idle is disabled, either manually or by past process history
2944 */
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002945 if (!cfq_should_idle(cfqd, cfqq)) {
2946 /* no queue idling. Check for group idling */
2947 if (cfqd->cfq_group_idle)
2948 group_idle = cfqd->cfq_group_idle;
2949 else
2950 return;
2951 }
Jens Axboe6d048f52007-04-25 12:44:27 +02002952
Jens Axboe22e2c502005-06-27 10:55:12 +02002953 /*
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002954 * still active requests from this queue, don't idle
Jens Axboe7b679132008-05-30 12:23:07 +02002955 */
Corrado Zoccolo8e550632009-11-26 10:02:58 +01002956 if (cfqq->dispatched)
Jens Axboe7b679132008-05-30 12:23:07 +02002957 return;
2958
2959 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02002960 * task has exited, don't wait
2961 */
Jens Axboe206dc692006-03-28 13:03:44 +02002962 cic = cfqd->active_cic;
Tejun Heof6e8d012012-03-05 13:15:26 -08002963 if (!cic || !atomic_read(&cic->icq.ioc->active_ref))
Jens Axboe6d048f52007-04-25 12:44:27 +02002964 return;
2965
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002966 /*
2967 * If our average think time is larger than the remaining time
2968 * slice, then don't idle. This avoids overrunning the allotted
2969 * time slice.
2970 */
Shaohua Li383cd722011-07-12 14:24:35 +02002971 if (sample_valid(cic->ttime.ttime_samples) &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002972 (cfqq->slice_end - now < cic->ttime.ttime_mean)) {
2973 cfq_log_cfqq(cfqd, cfqq, "Not idling. think_time:%llu",
Shaohua Li383cd722011-07-12 14:24:35 +02002974 cic->ttime.ttime_mean);
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002975 return;
Divyesh Shahb1ffe732010-03-25 15:45:03 +01002976 }
Corrado Zoccolo355b6592009-10-08 08:43:32 +02002977
Jan Karae7954212016-01-12 16:24:15 +01002978 /*
2979 * There are other queues in the group or this is the only group and
2980 * it has too big thinktime, don't do group idle.
2981 */
2982 if (group_idle &&
2983 (cfqq->cfqg->nr_cfqq > 1 ||
2984 cfq_io_thinktime_big(cfqd, &st->ttime, true)))
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002985 return;
2986
Jens Axboe3b181522005-06-27 10:56:24 +02002987 cfq_mark_cfqq_wait_request(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02002988
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002989 if (group_idle)
2990 sl = cfqd->cfq_group_idle;
2991 else
2992 sl = cfqd->cfq_slice_idle;
Jens Axboe206dc692006-03-28 13:03:44 +02002993
Jan Kara91148322016-06-08 15:11:39 +02002994 hrtimer_start(&cfqd->idle_slice_timer, ns_to_ktime(sl),
2995 HRTIMER_MODE_REL);
Tejun Heo155fead2012-04-01 14:38:44 -07002996 cfqg_stats_set_start_idle_time(cfqq->cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06002997 cfq_log_cfqq(cfqd, cfqq, "arm_idle: %llu group_idle: %d", sl,
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02002998 group_idle ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
Jens Axboe498d3aa22007-04-26 12:54:48 +02003001/*
3002 * Move request from internal lists to the request queue dispatch list.
3003 */
Jens Axboe165125e2007-07-24 09:28:11 +02003004static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Jens Axboe3ed9a292007-04-23 08:33:33 +02003006 struct cfq_data *cfqd = q->elevator->elevator_data;
Jens Axboe5e705372006-07-13 12:39:25 +02003007 struct cfq_queue *cfqq = RQ_CFQQ(rq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003008
Jens Axboe7b679132008-05-30 12:23:07 +02003009 cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
3010
Jeff Moyer06d21882009-09-11 17:08:59 +02003011 cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);
Jens Axboe5380a102006-07-13 12:37:56 +02003012 cfq_remove_request(rq);
Jens Axboe6d048f52007-04-25 12:44:27 +02003013 cfqq->dispatched++;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003014 (RQ_CFQG(rq))->dispatched++;
Jens Axboe5380a102006-07-13 12:37:56 +02003015 elv_dispatch_sort(q, rq);
Jens Axboe3ed9a292007-04-23 08:33:33 +02003016
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003017 cfqd->rq_in_flight[cfq_cfqq_sync(cfqq)]++;
Vivek Goyalc4e78932010-08-23 12:25:03 +02003018 cfqq->nr_sectors += blk_rq_sectors(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021/*
3022 * return expired entry, or NULL to just start from scratch in rbtree
3023 */
Jens Axboefebffd62008-01-28 13:19:43 +01003024static struct request *cfq_check_fifo(struct cfq_queue *cfqq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Jens Axboe30996f42009-10-05 11:03:39 +02003026 struct request *rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Jens Axboe3b181522005-06-27 10:56:24 +02003028 if (cfq_cfqq_fifo_expire(cfqq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return NULL;
Jens Axboecb887412007-01-19 12:01:16 +11003030
3031 cfq_mark_cfqq_fifo_expire(cfqq);
3032
Jens Axboe89850f72006-07-22 16:48:31 +02003033 if (list_empty(&cfqq->fifo))
3034 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jens Axboe89850f72006-07-22 16:48:31 +02003036 rq = rq_entry_fifo(cfqq->fifo.next);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003037 if (ktime_get_ns() < rq->fifo_time)
Jens Axboe7b679132008-05-30 12:23:07 +02003038 rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Jens Axboe6d048f52007-04-25 12:44:27 +02003040 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Jens Axboe22e2c502005-06-27 10:55:12 +02003043static inline int
3044cfq_prio_to_maxrq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3045{
3046 const int base_rq = cfqd->cfq_slice_async_rq;
3047
3048 WARN_ON(cfqq->ioprio >= IOPRIO_BE_NR);
3049
Namhyung Kimb9f8ce02011-05-24 10:23:21 +02003050 return 2 * base_rq * (IOPRIO_BE_NR - cfqq->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003051}
3052
3053/*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003054 * Must be called with the queue_lock held.
3055 */
3056static int cfqq_process_refs(struct cfq_queue *cfqq)
3057{
3058 int process_refs, io_refs;
3059
3060 io_refs = cfqq->allocated[READ] + cfqq->allocated[WRITE];
Shaohua Li30d7b942011-01-07 08:46:59 +01003061 process_refs = cfqq->ref - io_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003062 BUG_ON(process_refs < 0);
3063 return process_refs;
3064}
3065
3066static void cfq_setup_merge(struct cfq_queue *cfqq, struct cfq_queue *new_cfqq)
3067{
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003068 int process_refs, new_process_refs;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003069 struct cfq_queue *__cfqq;
3070
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003071 /*
3072 * If there are no process references on the new_cfqq, then it is
3073 * unsafe to follow the ->new_cfqq chain as other cfqq's in the
3074 * chain may have dropped their last reference (not just their
3075 * last process reference).
3076 */
3077 if (!cfqq_process_refs(new_cfqq))
3078 return;
3079
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003080 /* Avoid a circular list and skip interim queue merges */
3081 while ((__cfqq = new_cfqq->new_cfqq)) {
3082 if (__cfqq == cfqq)
3083 return;
3084 new_cfqq = __cfqq;
3085 }
3086
3087 process_refs = cfqq_process_refs(cfqq);
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003088 new_process_refs = cfqq_process_refs(new_cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003089 /*
3090 * If the process for the cfqq has gone away, there is no
3091 * sense in merging the queues.
3092 */
Jeff Moyerc10b61f2010-06-17 10:19:11 -04003093 if (process_refs == 0 || new_process_refs == 0)
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003094 return;
3095
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003096 /*
3097 * Merge in the direction of the lesser amount of work.
3098 */
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003099 if (new_process_refs >= process_refs) {
3100 cfqq->new_cfqq = new_cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003101 new_cfqq->ref += process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003102 } else {
3103 new_cfqq->new_cfqq = cfqq;
Shaohua Li30d7b942011-01-07 08:46:59 +01003104 cfqq->ref += new_process_refs;
Jeff Moyere6c5bc72009-10-23 17:14:52 -04003105 }
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003106}
3107
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003108static enum wl_type_t cfq_choose_wl_type(struct cfq_data *cfqd,
Vivek Goyal3bf10fe2012-10-03 16:56:56 -04003109 struct cfq_group *cfqg, enum wl_class_t wl_class)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003110{
3111 struct cfq_queue *queue;
3112 int i;
3113 bool key_valid = false;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003114 u64 lowest_key = 0;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003115 enum wl_type_t cur_best = SYNC_NOIDLE_WORKLOAD;
3116
Vivek Goyal65b32a52009-12-16 17:52:59 -05003117 for (i = 0; i <= SYNC_WORKLOAD; ++i) {
3118 /* select the one with lowest rb_key */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003119 queue = cfq_rb_first(st_for(cfqg, wl_class, i));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003120 if (queue &&
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003121 (!key_valid || queue->rb_key < lowest_key)) {
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003122 lowest_key = queue->rb_key;
3123 cur_best = i;
3124 key_valid = true;
3125 }
3126 }
3127
3128 return cur_best;
3129}
3130
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003131static void
3132choose_wl_class_and_type(struct cfq_data *cfqd, struct cfq_group *cfqg)
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003133{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003134 u64 slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003135 unsigned count;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003136 struct cfq_rb_root *st;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003137 u64 group_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003138 enum wl_class_t original_class = cfqd->serving_wl_class;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003139 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003140
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003141 /* Choose next priority. RT > BE > IDLE */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003142 if (cfq_group_busy_queues_wl(RT_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003143 cfqd->serving_wl_class = RT_WORKLOAD;
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003144 else if (cfq_group_busy_queues_wl(BE_WORKLOAD, cfqd, cfqg))
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003145 cfqd->serving_wl_class = BE_WORKLOAD;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003146 else {
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003147 cfqd->serving_wl_class = IDLE_WORKLOAD;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003148 cfqd->workload_expires = now + jiffies_to_nsecs(1);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003149 return;
3150 }
3151
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003152 if (original_class != cfqd->serving_wl_class)
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003153 goto new_workload;
3154
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003155 /*
3156 * For RT and BE, we have to choose also the type
3157 * (SYNC, SYNC_NOIDLE, ASYNC), and to compute a workload
3158 * expiration time
3159 */
Vivek Goyal34b98d02012-10-03 16:56:58 -04003160 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003161 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003162
3163 /*
Vivek Goyal65b32a52009-12-16 17:52:59 -05003164 * check workload expiration, and that we still have other queues ready
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003165 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003166 if (count && !(now > cfqd->workload_expires))
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003167 return;
3168
Shaohua Li writese4ea0c12010-12-13 14:32:22 +01003169new_workload:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003170 /* otherwise select new workload type */
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003171 cfqd->serving_wl_type = cfq_choose_wl_type(cfqd, cfqg,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003172 cfqd->serving_wl_class);
Vivek Goyal34b98d02012-10-03 16:56:58 -04003173 st = st_for(cfqg, cfqd->serving_wl_class, cfqd->serving_wl_type);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003174 count = st->count;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003175
3176 /*
3177 * the workload slice is computed as a fraction of target latency
3178 * proportional to the number of queues in that workload, over
3179 * all the queues in the same priority class
3180 */
Vivek Goyal58ff82f2009-12-03 12:59:44 -05003181 group_slice = cfq_group_slice(cfqd, cfqg);
3182
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003183 slice = div_u64(group_slice * count,
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003184 max_t(unsigned, cfqg->busy_queues_avg[cfqd->serving_wl_class],
3185 cfq_group_busy_queues_wl(cfqd->serving_wl_class, cfqd,
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003186 cfqg)));
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003187
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003188 if (cfqd->serving_wl_type == ASYNC_WORKLOAD) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003189 u64 tmp;
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003190
3191 /*
3192 * Async queues are currently system wide. Just taking
3193 * proportion of queues with-in same group will lead to higher
3194 * async ratio system wide as generally root group is going
3195 * to have higher weight. A more accurate thing would be to
3196 * calculate system wide asnc/sync ratio.
3197 */
Tao Ma5bf14c02012-04-01 14:33:39 -07003198 tmp = cfqd->cfq_target_latency *
3199 cfqg_busy_async_queues(cfqd, cfqg);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003200 tmp = div_u64(tmp, cfqd->busy_queues);
3201 slice = min_t(u64, slice, tmp);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003202
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003203 /* async workload slice is scaled down according to
3204 * the sync/async slice ratio. */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003205 slice = div64_u64(slice*cfqd->cfq_slice[0], cfqd->cfq_slice[1]);
Vivek Goyalf26bd1f2009-12-03 12:59:54 -05003206 } else
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003207 /* sync workload slice is at least 2 * cfq_slice_idle */
3208 slice = max(slice, 2 * cfqd->cfq_slice_idle);
3209
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003210 slice = max_t(u64, slice, CFQ_MIN_TT);
3211 cfq_log(cfqd, "workload slice:%llu", slice);
3212 cfqd->workload_expires = now + slice;
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003213}
3214
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003215static struct cfq_group *cfq_get_next_cfqg(struct cfq_data *cfqd)
3216{
3217 struct cfq_rb_root *st = &cfqd->grp_service_tree;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003218 struct cfq_group *cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003219
3220 if (RB_EMPTY_ROOT(&st->rb))
3221 return NULL;
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003222 cfqg = cfq_rb_first_group(st);
Vivek Goyal25bc6b02009-12-03 12:59:43 -05003223 update_min_vdisktime(st);
3224 return cfqg;
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003225}
3226
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003227static void cfq_choose_cfqg(struct cfq_data *cfqd)
3228{
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003229 struct cfq_group *cfqg = cfq_get_next_cfqg(cfqd);
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003230 u64 now = ktime_get_ns();
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05003231
3232 cfqd->serving_group = cfqg;
Vivek Goyaldae739e2009-12-03 12:59:45 -05003233
3234 /* Restore the workload type data */
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003235 if (cfqg->saved_wl_slice) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003236 cfqd->workload_expires = now + cfqg->saved_wl_slice;
Vivek Goyal4d2ceea2012-10-03 16:56:57 -04003237 cfqd->serving_wl_type = cfqg->saved_wl_type;
3238 cfqd->serving_wl_class = cfqg->saved_wl_class;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003239 } else
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003240 cfqd->workload_expires = now - 1;
Gui Jianfeng66ae2912009-12-15 10:08:45 +01003241
Vivek Goyal6d816ec2012-10-03 16:56:59 -04003242 choose_wl_class_and_type(cfqd, cfqg);
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003243}
3244
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003245/*
Jens Axboe498d3aa22007-04-26 12:54:48 +02003246 * Select a queue for service. If we have a current active queue,
3247 * check whether to continue servicing it, or retrieve and set a new one.
Jens Axboe22e2c502005-06-27 10:55:12 +02003248 */
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003249static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
Jens Axboe22e2c502005-06-27 10:55:12 +02003250{
Jens Axboea36e71f2009-04-15 12:15:11 +02003251 struct cfq_queue *cfqq, *new_cfqq = NULL;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003252 u64 now = ktime_get_ns();
Jens Axboe22e2c502005-06-27 10:55:12 +02003253
3254 cfqq = cfqd->active_queue;
3255 if (!cfqq)
3256 goto new_queue;
3257
Vivek Goyalf04a6422009-12-03 12:59:40 -05003258 if (!cfqd->rq_queued)
3259 return NULL;
Vivek Goyalc244bb52009-12-08 17:52:57 -05003260
3261 /*
3262 * We were waiting for group to get backlogged. Expire the queue
3263 */
3264 if (cfq_cfqq_wait_busy(cfqq) && !RB_EMPTY_ROOT(&cfqq->sort_list))
3265 goto expire;
3266
Jens Axboe22e2c502005-06-27 10:55:12 +02003267 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003268 * The active queue has run out of time, expire it and select new.
Jens Axboe22e2c502005-06-27 10:55:12 +02003269 */
Vivek Goyal7667aa02009-12-08 17:52:58 -05003270 if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq)) {
3271 /*
3272 * If slice had not expired at the completion of last request
3273 * we might not have turned on wait_busy flag. Don't expire
3274 * the queue yet. Allow the group to get backlogged.
3275 *
3276 * The very fact that we have used the slice, that means we
3277 * have been idling all along on this queue and it should be
3278 * ok to wait for this request to complete.
3279 */
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003280 if (cfqq->cfqg->nr_cfqq == 1 && RB_EMPTY_ROOT(&cfqq->sort_list)
3281 && cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3282 cfqq = NULL;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003283 goto keep_queue;
Vivek Goyal82bbbf22009-12-10 19:25:41 +01003284 } else
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003285 goto check_group_idle;
Vivek Goyal7667aa02009-12-08 17:52:58 -05003286 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003287
3288 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003289 * The active queue has requests and isn't expired, allow it to
3290 * dispatch.
Jens Axboe22e2c502005-06-27 10:55:12 +02003291 */
Jens Axboedd67d052006-06-21 09:36:18 +02003292 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02003293 goto keep_queue;
Jens Axboe6d048f52007-04-25 12:44:27 +02003294
3295 /*
Jens Axboea36e71f2009-04-15 12:15:11 +02003296 * If another queue has a request waiting within our mean seek
3297 * distance, let it run. The expire code will check for close
3298 * cooperators and put the close queue at the front of the service
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003299 * tree. If possible, merge the expiring queue with the new cfqq.
Jens Axboea36e71f2009-04-15 12:15:11 +02003300 */
Jeff Moyerb3b6d042009-10-23 17:14:51 -04003301 new_cfqq = cfq_close_cooperator(cfqd, cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003302 if (new_cfqq) {
3303 if (!cfqq->new_cfqq)
3304 cfq_setup_merge(cfqq, new_cfqq);
Jens Axboea36e71f2009-04-15 12:15:11 +02003305 goto expire;
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003306 }
Jens Axboea36e71f2009-04-15 12:15:11 +02003307
3308 /*
Jens Axboe6d048f52007-04-25 12:44:27 +02003309 * No requests pending. If the active queue still has requests in
3310 * flight or is idling for a new request, allow either of these
3311 * conditions to happen (or time out) before selecting a new queue.
3312 */
Jan Kara91148322016-06-08 15:11:39 +02003313 if (hrtimer_active(&cfqd->idle_slice_timer)) {
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003314 cfqq = NULL;
3315 goto keep_queue;
3316 }
3317
Shaohua Li8e1ac662010-11-08 15:01:04 +01003318 /*
3319 * This is a deep seek queue, but the device is much faster than
3320 * the queue can deliver, don't idle
3321 **/
3322 if (CFQQ_SEEKY(cfqq) && cfq_cfqq_idle_window(cfqq) &&
3323 (cfq_cfqq_slice_new(cfqq) ||
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003324 (cfqq->slice_end - now > now - cfqq->slice_start))) {
Shaohua Li8e1ac662010-11-08 15:01:04 +01003325 cfq_clear_cfqq_deep(cfqq);
3326 cfq_clear_cfqq_idle_window(cfqq);
3327 }
3328
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02003329 if (cfqq->dispatched && cfq_should_idle(cfqd, cfqq)) {
3330 cfqq = NULL;
3331 goto keep_queue;
3332 }
3333
3334 /*
3335 * If group idle is enabled and there are requests dispatched from
3336 * this group, wait for requests to complete.
3337 */
3338check_group_idle:
Shaohua Li7700fc42011-07-12 14:24:56 +02003339 if (cfqd->cfq_group_idle && cfqq->cfqg->nr_cfqq == 1 &&
3340 cfqq->cfqg->dispatched &&
3341 !cfq_io_thinktime_big(cfqd, &cfqq->cfqg->ttime, true)) {
Jens Axboecaaa5f92006-06-16 11:23:00 +02003342 cfqq = NULL;
3343 goto keep_queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02003344 }
3345
Jens Axboe3b181522005-06-27 10:56:24 +02003346expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02003347 cfq_slice_expired(cfqd, 0);
Jens Axboe3b181522005-06-27 10:56:24 +02003348new_queue:
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003349 /*
3350 * Current queue expired. Check if we have to switch to a new
3351 * service tree
3352 */
3353 if (!new_cfqq)
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003354 cfq_choose_cfqg(cfqd);
Corrado Zoccolo718eee02009-10-26 22:45:29 +01003355
Jens Axboea36e71f2009-04-15 12:15:11 +02003356 cfqq = cfq_set_active_queue(cfqd, new_cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003357keep_queue:
Jens Axboe3b181522005-06-27 10:56:24 +02003358 return cfqq;
Jens Axboe22e2c502005-06-27 10:55:12 +02003359}
3360
Jens Axboefebffd62008-01-28 13:19:43 +01003361static int __cfq_forced_dispatch_cfqq(struct cfq_queue *cfqq)
Jens Axboed9e76202007-04-20 14:27:50 +02003362{
3363 int dispatched = 0;
3364
3365 while (cfqq->next_rq) {
3366 cfq_dispatch_insert(cfqq->cfqd->queue, cfqq->next_rq);
3367 dispatched++;
3368 }
3369
3370 BUG_ON(!list_empty(&cfqq->fifo));
Vivek Goyalf04a6422009-12-03 12:59:40 -05003371
3372 /* By default cfqq is not expired if it is empty. Do it explicitly */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003373 __cfq_slice_expired(cfqq->cfqd, cfqq, 0);
Jens Axboed9e76202007-04-20 14:27:50 +02003374 return dispatched;
3375}
3376
Jens Axboe498d3aa22007-04-26 12:54:48 +02003377/*
3378 * Drain our current requests. Used for barriers and when switching
3379 * io schedulers on-the-fly.
3380 */
Jens Axboed9e76202007-04-20 14:27:50 +02003381static int cfq_forced_dispatch(struct cfq_data *cfqd)
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003382{
Jens Axboe08717142008-01-28 11:38:15 +01003383 struct cfq_queue *cfqq;
Jens Axboed9e76202007-04-20 14:27:50 +02003384 int dispatched = 0;
Vivek Goyalcdb16e82009-12-03 12:59:38 -05003385
Divyesh Shah3440c492010-04-09 09:29:57 +02003386 /* Expire the timeslice of the current active queue first */
Vivek Goyale5ff0822010-04-26 19:25:11 +02003387 cfq_slice_expired(cfqd, 0);
Divyesh Shah3440c492010-04-09 09:29:57 +02003388 while ((cfqq = cfq_get_next_queue_forced(cfqd)) != NULL) {
3389 __cfq_set_active_queue(cfqd, cfqq);
Vivek Goyalf04a6422009-12-03 12:59:40 -05003390 dispatched += __cfq_forced_dispatch_cfqq(cfqq);
Divyesh Shah3440c492010-04-09 09:29:57 +02003391 }
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003392
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003393 BUG_ON(cfqd->busy_queues);
3394
Jeff Moyer69237152009-06-12 15:29:30 +02003395 cfq_log(cfqd, "forced_dispatch=%d", dispatched);
Tejun Heo1b5ed5e12005-11-10 08:49:19 +01003396 return dispatched;
3397}
3398
Shaohua Liabc3c742010-03-01 09:20:54 +01003399static inline bool cfq_slice_used_soon(struct cfq_data *cfqd,
3400 struct cfq_queue *cfqq)
3401{
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003402 u64 now = ktime_get_ns();
3403
Shaohua Liabc3c742010-03-01 09:20:54 +01003404 /* the queue hasn't finished any request, can't estimate */
3405 if (cfq_cfqq_slice_new(cfqq))
Shaohua Lic1e44752010-11-08 15:01:02 +01003406 return true;
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003407 if (now + cfqd->cfq_slice_idle * cfqq->dispatched > cfqq->slice_end)
Shaohua Lic1e44752010-11-08 15:01:02 +01003408 return true;
Shaohua Liabc3c742010-03-01 09:20:54 +01003409
Shaohua Lic1e44752010-11-08 15:01:02 +01003410 return false;
Shaohua Liabc3c742010-03-01 09:20:54 +01003411}
3412
Jens Axboe0b182d62009-10-06 20:49:37 +02003413static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
Jens Axboe2f5cb732009-04-07 08:51:19 +02003414{
Jens Axboe2f5cb732009-04-07 08:51:19 +02003415 unsigned int max_dispatch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Glauber Costa3932a862016-09-22 20:59:59 -04003417 if (cfq_cfqq_must_dispatch(cfqq))
3418 return true;
3419
Jens Axboe2f5cb732009-04-07 08:51:19 +02003420 /*
Jens Axboe5ad531d2009-07-03 12:57:48 +02003421 * Drain async requests before we start sync IO
3422 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003423 if (cfq_should_idle(cfqd, cfqq) && cfqd->rq_in_flight[BLK_RW_ASYNC])
Jens Axboe0b182d62009-10-06 20:49:37 +02003424 return false;
Jens Axboe5ad531d2009-07-03 12:57:48 +02003425
3426 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003427 * If this is an async queue and we have sync IO in flight, let it wait
3428 */
Corrado Zoccolo53c583d2010-02-28 19:45:05 +01003429 if (cfqd->rq_in_flight[BLK_RW_SYNC] && !cfq_cfqq_sync(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003430 return false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003431
Shaohua Liabc3c742010-03-01 09:20:54 +01003432 max_dispatch = max_t(unsigned int, cfqd->cfq_quantum / 2, 1);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003433 if (cfq_class_idle(cfqq))
3434 max_dispatch = 1;
3435
3436 /*
3437 * Does this cfqq already have too much IO in flight?
3438 */
3439 if (cfqq->dispatched >= max_dispatch) {
Shaohua Lief8a41d2011-03-07 09:26:29 +01003440 bool promote_sync = false;
Jens Axboe2f5cb732009-04-07 08:51:19 +02003441 /*
3442 * idle queue must always only have a single IO in flight
3443 */
Jens Axboe3ed9a292007-04-23 08:33:33 +02003444 if (cfq_class_idle(cfqq))
Jens Axboe0b182d62009-10-06 20:49:37 +02003445 return false;
Jens Axboe3ed9a292007-04-23 08:33:33 +02003446
Jens Axboe2f5cb732009-04-07 08:51:19 +02003447 /*
Li, Shaohuac4ade942011-03-23 08:30:34 +01003448 * If there is only one sync queue
3449 * we can ignore async queue here and give the sync
Shaohua Lief8a41d2011-03-07 09:26:29 +01003450 * queue no dispatch limit. The reason is a sync queue can
3451 * preempt async queue, limiting the sync queue doesn't make
3452 * sense. This is useful for aiostress test.
3453 */
Li, Shaohuac4ade942011-03-23 08:30:34 +01003454 if (cfq_cfqq_sync(cfqq) && cfqd->busy_sync_queues == 1)
3455 promote_sync = true;
Shaohua Lief8a41d2011-03-07 09:26:29 +01003456
3457 /*
Jens Axboe2f5cb732009-04-07 08:51:19 +02003458 * We have other queues, don't allow more IO from this one
3459 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003460 if (cfqd->busy_queues > 1 && cfq_slice_used_soon(cfqd, cfqq) &&
3461 !promote_sync)
Jens Axboe0b182d62009-10-06 20:49:37 +02003462 return false;
Jens Axboe9ede2092007-01-19 12:11:44 +11003463
Jens Axboe2f5cb732009-04-07 08:51:19 +02003464 /*
Shaohua Li474b18c2009-12-03 12:58:05 +01003465 * Sole queue user, no limit
Vivek Goyal365722b2009-10-03 15:21:27 +02003466 */
Shaohua Lief8a41d2011-03-07 09:26:29 +01003467 if (cfqd->busy_queues == 1 || promote_sync)
Shaohua Liabc3c742010-03-01 09:20:54 +01003468 max_dispatch = -1;
3469 else
3470 /*
3471 * Normally we start throttling cfqq when cfq_quantum/2
3472 * requests have been dispatched. But we can drive
3473 * deeper queue depths at the beginning of slice
3474 * subjected to upper limit of cfq_quantum.
3475 * */
3476 max_dispatch = cfqd->cfq_quantum;
Jens Axboe8e296752009-10-03 16:26:03 +02003477 }
3478
3479 /*
3480 * Async queues must wait a bit before being allowed dispatch.
3481 * We also ramp up the dispatch depth gradually for async IO,
3482 * based on the last sync IO we serviced
3483 */
Jens Axboe963b72f2009-10-03 19:42:18 +02003484 if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003485 u64 last_sync = ktime_get_ns() - cfqd->last_delayed_sync;
Jens Axboe8e296752009-10-03 16:26:03 +02003486 unsigned int depth;
Vivek Goyal365722b2009-10-03 15:21:27 +02003487
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003488 depth = div64_u64(last_sync, cfqd->cfq_slice[1]);
Jens Axboee00c54c2009-10-04 20:36:19 +02003489 if (!depth && !cfqq->dispatched)
3490 depth = 1;
Jens Axboe8e296752009-10-03 16:26:03 +02003491 if (depth < max_dispatch)
3492 max_dispatch = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
3494
Jens Axboe0b182d62009-10-06 20:49:37 +02003495 /*
3496 * If we're below the current max, allow a dispatch
3497 */
3498 return cfqq->dispatched < max_dispatch;
3499}
3500
3501/*
3502 * Dispatch a request from cfqq, moving them to the request queue
3503 * dispatch list.
3504 */
3505static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3506{
3507 struct request *rq;
3508
3509 BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
3510
Glauber Costa3932a862016-09-22 20:59:59 -04003511 rq = cfq_check_fifo(cfqq);
3512 if (rq)
3513 cfq_mark_cfqq_must_dispatch(cfqq);
3514
Jens Axboe0b182d62009-10-06 20:49:37 +02003515 if (!cfq_may_dispatch(cfqd, cfqq))
3516 return false;
3517
3518 /*
3519 * follow expired path, else get first next available
3520 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003521 if (!rq)
3522 rq = cfqq->next_rq;
Glauber Costa3932a862016-09-22 20:59:59 -04003523 else
3524 cfq_log_cfqq(cfqq->cfqd, cfqq, "fifo=%p", rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003525
3526 /*
3527 * insert request into driver dispatch list
3528 */
3529 cfq_dispatch_insert(cfqd->queue, rq);
3530
3531 if (!cfqd->active_cic) {
Tejun Heoc5869802011-12-14 00:33:41 +01003532 struct cfq_io_cq *cic = RQ_CIC(rq);
Jens Axboe0b182d62009-10-06 20:49:37 +02003533
Tejun Heoc5869802011-12-14 00:33:41 +01003534 atomic_long_inc(&cic->icq.ioc->refcount);
Jens Axboe0b182d62009-10-06 20:49:37 +02003535 cfqd->active_cic = cic;
3536 }
3537
3538 return true;
3539}
3540
3541/*
3542 * Find the cfqq that we need to service and move a request from that to the
3543 * dispatch list
3544 */
3545static int cfq_dispatch_requests(struct request_queue *q, int force)
3546{
3547 struct cfq_data *cfqd = q->elevator->elevator_data;
3548 struct cfq_queue *cfqq;
3549
3550 if (!cfqd->busy_queues)
3551 return 0;
3552
3553 if (unlikely(force))
3554 return cfq_forced_dispatch(cfqd);
3555
3556 cfqq = cfq_select_queue(cfqd);
3557 if (!cfqq)
Jens Axboe8e296752009-10-03 16:26:03 +02003558 return 0;
3559
Jens Axboe2f5cb732009-04-07 08:51:19 +02003560 /*
Jens Axboe0b182d62009-10-06 20:49:37 +02003561 * Dispatch a request from this cfqq, if it is allowed
Jens Axboe2f5cb732009-04-07 08:51:19 +02003562 */
Jens Axboe0b182d62009-10-06 20:49:37 +02003563 if (!cfq_dispatch_request(cfqd, cfqq))
3564 return 0;
3565
Jens Axboe2f5cb732009-04-07 08:51:19 +02003566 cfqq->slice_dispatch++;
Jens Axboeb0291952009-04-07 11:38:31 +02003567 cfq_clear_cfqq_must_dispatch(cfqq);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003568
3569 /*
3570 * expire an async queue immediately if it has used up its slice. idle
3571 * queue always expire after 1 dispatch round.
3572 */
3573 if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
3574 cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
3575 cfq_class_idle(cfqq))) {
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003576 cfqq->slice_end = ktime_get_ns() + 1;
Vivek Goyale5ff0822010-04-26 19:25:11 +02003577 cfq_slice_expired(cfqd, 0);
Jens Axboe2f5cb732009-04-07 08:51:19 +02003578 }
3579
Shan Weib217a902009-09-01 10:06:42 +02003580 cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
Jens Axboe2f5cb732009-04-07 08:51:19 +02003581 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584/*
Jens Axboe5e705372006-07-13 12:39:25 +02003585 * task holds one reference to the queue, dropped when task exits. each rq
3586 * in-flight on this queue also holds a reference, dropped when rq is freed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 *
Vivek Goyalb1c35762009-12-03 12:59:47 -05003588 * Each cfq queue took a reference on the parent group. Drop it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 * queue lock must be held here.
3590 */
3591static void cfq_put_queue(struct cfq_queue *cfqq)
3592{
Jens Axboe22e2c502005-06-27 10:55:12 +02003593 struct cfq_data *cfqd = cfqq->cfqd;
Justin TerAvest0bbfeb82011-03-01 15:05:08 -05003594 struct cfq_group *cfqg;
Jens Axboe22e2c502005-06-27 10:55:12 +02003595
Shaohua Li30d7b942011-01-07 08:46:59 +01003596 BUG_ON(cfqq->ref <= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Shaohua Li30d7b942011-01-07 08:46:59 +01003598 cfqq->ref--;
3599 if (cfqq->ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return;
3601
Jens Axboe7b679132008-05-30 12:23:07 +02003602 cfq_log_cfqq(cfqd, cfqq, "put_queue");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 BUG_ON(rb_first(&cfqq->sort_list));
Jens Axboe22e2c502005-06-27 10:55:12 +02003604 BUG_ON(cfqq->allocated[READ] + cfqq->allocated[WRITE]);
Vivek Goyalb1c35762009-12-03 12:59:47 -05003605 cfqg = cfqq->cfqg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003607 if (unlikely(cfqd->active_queue == cfqq)) {
Vivek Goyale5ff0822010-04-26 19:25:11 +02003608 __cfq_slice_expired(cfqd, cfqq, 0);
Jens Axboe23e018a2009-10-05 08:52:35 +02003609 cfq_schedule_dispatch(cfqd);
Jens Axboe28f95cbc2007-01-19 12:09:53 +11003610 }
Jens Axboe22e2c502005-06-27 10:55:12 +02003611
Vivek Goyalf04a6422009-12-03 12:59:40 -05003612 BUG_ON(cfq_cfqq_on_rr(cfqq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 kmem_cache_free(cfq_pool, cfqq);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01003614 cfqg_put(cfqg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Shaohua Lid02a2c02010-05-25 10:16:53 +02003617static void cfq_put_cooperator(struct cfq_queue *cfqq)
Jens Axboe89850f72006-07-22 16:48:31 +02003618{
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003619 struct cfq_queue *__cfqq, *next;
3620
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003621 /*
3622 * If this queue was scheduled to merge with another queue, be
3623 * sure to drop the reference taken on that queue (and others in
3624 * the merge chain). See cfq_setup_merge and cfq_merge_cfqqs.
3625 */
3626 __cfqq = cfqq->new_cfqq;
3627 while (__cfqq) {
3628 if (__cfqq == cfqq) {
3629 WARN(1, "cfqq->new_cfqq loop detected\n");
3630 break;
3631 }
3632 next = __cfqq->new_cfqq;
3633 cfq_put_queue(__cfqq);
3634 __cfqq = next;
3635 }
Shaohua Lid02a2c02010-05-25 10:16:53 +02003636}
3637
3638static void cfq_exit_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq)
3639{
3640 if (unlikely(cfqq == cfqd->active_queue)) {
3641 __cfq_slice_expired(cfqd, cfqq, 0);
3642 cfq_schedule_dispatch(cfqd);
3643 }
3644
3645 cfq_put_cooperator(cfqq);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04003646
Jens Axboe89850f72006-07-22 16:48:31 +02003647 cfq_put_queue(cfqq);
3648}
3649
Tejun Heo9b84cac2011-12-14 00:33:42 +01003650static void cfq_init_icq(struct io_cq *icq)
3651{
3652 struct cfq_io_cq *cic = icq_to_cic(icq);
3653
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06003654 cic->ttime.last_end_request = ktime_get_ns();
Tejun Heo9b84cac2011-12-14 00:33:42 +01003655}
3656
Tejun Heoc5869802011-12-14 00:33:41 +01003657static void cfq_exit_icq(struct io_cq *icq)
Jens Axboe89850f72006-07-22 16:48:31 +02003658{
Tejun Heoc5869802011-12-14 00:33:41 +01003659 struct cfq_io_cq *cic = icq_to_cic(icq);
Tejun Heo283287a2011-12-14 00:33:38 +01003660 struct cfq_data *cfqd = cic_to_cfqd(cic);
Fabio Checconi4faa3c82008-04-10 08:28:01 +02003661
Tejun Heo563180a2015-08-18 14:55:00 -07003662 if (cic_to_cfqq(cic, false)) {
3663 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, false));
3664 cic_set_cfqq(cic, NULL, false);
Jens Axboe89850f72006-07-22 16:48:31 +02003665 }
3666
Tejun Heo563180a2015-08-18 14:55:00 -07003667 if (cic_to_cfqq(cic, true)) {
3668 cfq_exit_cfqq(cfqd, cic_to_cfqq(cic, true));
3669 cic_set_cfqq(cic, NULL, true);
Jens Axboe89850f72006-07-22 16:48:31 +02003670 }
Jens Axboe89850f72006-07-22 16:48:31 +02003671}
3672
Tejun Heoabede6d2012-03-19 15:10:57 -07003673static void cfq_init_prio_data(struct cfq_queue *cfqq, struct cfq_io_cq *cic)
Jens Axboe22e2c502005-06-27 10:55:12 +02003674{
3675 struct task_struct *tsk = current;
3676 int ioprio_class;
3677
Jens Axboe3b181522005-06-27 10:56:24 +02003678 if (!cfq_cfqq_prio_changed(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02003679 return;
3680
Tejun Heo598971b2012-03-19 15:10:58 -07003681 ioprio_class = IOPRIO_PRIO_CLASS(cic->ioprio);
Jens Axboe22e2c502005-06-27 10:55:12 +02003682 switch (ioprio_class) {
Jens Axboefe094d92008-01-31 13:08:54 +01003683 default:
3684 printk(KERN_ERR "cfq: bad prio %x\n", ioprio_class);
3685 case IOPRIO_CLASS_NONE:
3686 /*
Jens Axboe6d63c272008-05-07 09:51:23 +02003687 * no prio set, inherit CPU scheduling settings
Jens Axboefe094d92008-01-31 13:08:54 +01003688 */
3689 cfqq->ioprio = task_nice_ioprio(tsk);
Jens Axboe6d63c272008-05-07 09:51:23 +02003690 cfqq->ioprio_class = task_nice_ioclass(tsk);
Jens Axboefe094d92008-01-31 13:08:54 +01003691 break;
3692 case IOPRIO_CLASS_RT:
Tejun Heo598971b2012-03-19 15:10:58 -07003693 cfqq->ioprio = IOPRIO_PRIO_DATA(cic->ioprio);
Jens Axboefe094d92008-01-31 13:08:54 +01003694 cfqq->ioprio_class = IOPRIO_CLASS_RT;
3695 break;
3696 case IOPRIO_CLASS_BE:
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_BE;
3699 break;
3700 case IOPRIO_CLASS_IDLE:
3701 cfqq->ioprio_class = IOPRIO_CLASS_IDLE;
3702 cfqq->ioprio = 7;
3703 cfq_clear_cfqq_idle_window(cfqq);
3704 break;
Jens Axboe22e2c502005-06-27 10:55:12 +02003705 }
3706
3707 /*
3708 * keep track of original prio settings in case we have to temporarily
3709 * elevate the priority of this queue
3710 */
3711 cfqq->org_ioprio = cfqq->ioprio;
Jens Axboeb8269db2016-06-09 15:47:29 -06003712 cfqq->org_ioprio_class = cfqq->ioprio_class;
Jens Axboe3b181522005-06-27 10:56:24 +02003713 cfq_clear_cfqq_prio_changed(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02003714}
3715
Tejun Heo598971b2012-03-19 15:10:58 -07003716static void check_ioprio_changed(struct cfq_io_cq *cic, struct bio *bio)
Jens Axboe22e2c502005-06-27 10:55:12 +02003717{
Tejun Heo598971b2012-03-19 15:10:58 -07003718 int ioprio = cic->icq.ioc->ioprio;
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003719 struct cfq_data *cfqd = cic_to_cfqd(cic);
Al Viro478a82b2006-03-18 13:25:24 -05003720 struct cfq_queue *cfqq;
Jens Axboe35e60772006-06-14 09:10:45 +02003721
Tejun Heo598971b2012-03-19 15:10:58 -07003722 /*
3723 * Check whether ioprio has changed. The condition may trigger
3724 * spuriously on a newly created cic but there's no harm.
3725 */
3726 if (unlikely(!cfqd) || likely(cic->ioprio == ioprio))
Jens Axboecaaa5f92006-06-16 11:23:00 +02003727 return;
3728
Tejun Heo563180a2015-08-18 14:55:00 -07003729 cfqq = cic_to_cfqq(cic, false);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003730 if (cfqq) {
Tejun Heo563180a2015-08-18 14:55:00 -07003731 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07003732 cfqq = cfq_get_queue(cfqd, BLK_RW_ASYNC, cic, bio);
Tejun Heo563180a2015-08-18 14:55:00 -07003733 cic_set_cfqq(cic, cfqq, false);
Jens Axboe22e2c502005-06-27 10:55:12 +02003734 }
Jens Axboecaaa5f92006-06-16 11:23:00 +02003735
Tejun Heo563180a2015-08-18 14:55:00 -07003736 cfqq = cic_to_cfqq(cic, true);
Jens Axboecaaa5f92006-06-16 11:23:00 +02003737 if (cfqq)
3738 cfq_mark_cfqq_prio_changed(cfqq);
Tejun Heo598971b2012-03-19 15:10:58 -07003739
3740 cic->ioprio = ioprio;
Jens Axboe22e2c502005-06-27 10:55:12 +02003741}
3742
Jens Axboed5036d72009-06-26 10:44:34 +02003743static void cfq_init_cfqq(struct cfq_data *cfqd, struct cfq_queue *cfqq,
Jens Axboea6151c32009-10-07 20:02:57 +02003744 pid_t pid, bool is_sync)
Jens Axboed5036d72009-06-26 10:44:34 +02003745{
3746 RB_CLEAR_NODE(&cfqq->rb_node);
3747 RB_CLEAR_NODE(&cfqq->p_node);
3748 INIT_LIST_HEAD(&cfqq->fifo);
3749
Shaohua Li30d7b942011-01-07 08:46:59 +01003750 cfqq->ref = 0;
Jens Axboed5036d72009-06-26 10:44:34 +02003751 cfqq->cfqd = cfqd;
3752
3753 cfq_mark_cfqq_prio_changed(cfqq);
3754
3755 if (is_sync) {
3756 if (!cfq_class_idle(cfqq))
3757 cfq_mark_cfqq_idle_window(cfqq);
3758 cfq_mark_cfqq_sync(cfqq);
3759 }
3760 cfqq->pid = pid;
3761}
3762
Vivek Goyal246103332009-12-03 12:59:51 -05003763#ifdef CONFIG_CFQ_GROUP_IOSCHED
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003764static bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
Vivek Goyal246103332009-12-03 12:59:51 -05003765{
Konstantin Khlebnikovbca4b912010-05-20 23:21:34 +04003766 struct cfq_data *cfqd = cic_to_cfqd(cic);
Tejun Heo60a83702015-08-18 14:55:05 -07003767 struct cfq_queue *cfqq;
Tejun Heof4da8072014-09-08 08:15:20 +09003768 uint64_t serial_nr;
Jens Axboe87760e52016-11-09 12:38:14 -07003769 bool nonroot_cg;
Vivek Goyal246103332009-12-03 12:59:51 -05003770
Tejun Heo598971b2012-03-19 15:10:58 -07003771 rcu_read_lock();
Tejun Heof4da8072014-09-08 08:15:20 +09003772 serial_nr = bio_blkcg(bio)->css.serial_nr;
Jens Axboe87760e52016-11-09 12:38:14 -07003773 nonroot_cg = bio_blkcg(bio) != &blkcg_root;
Tejun Heo598971b2012-03-19 15:10:58 -07003774 rcu_read_unlock();
3775
3776 /*
3777 * Check whether blkcg has changed. The condition may trigger
3778 * spuriously on a newly created cic but there's no harm.
3779 */
Tejun Heof4da8072014-09-08 08:15:20 +09003780 if (unlikely(!cfqd) || likely(cic->blkcg_serial_nr == serial_nr))
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003781 return nonroot_cg;
Jens Axboe87760e52016-11-09 12:38:14 -07003782
3783 /*
Tejun Heo60a83702015-08-18 14:55:05 -07003784 * Drop reference to queues. New queues will be assigned in new
3785 * group upon arrival of fresh requests.
3786 */
3787 cfqq = cic_to_cfqq(cic, false);
3788 if (cfqq) {
3789 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3790 cic_set_cfqq(cic, NULL, false);
3791 cfq_put_queue(cfqq);
3792 }
3793
3794 cfqq = cic_to_cfqq(cic, true);
3795 if (cfqq) {
3796 cfq_log_cfqq(cfqd, cfqq, "changed cgroup");
3797 cic_set_cfqq(cic, NULL, true);
3798 cfq_put_queue(cfqq);
Vivek Goyal246103332009-12-03 12:59:51 -05003799 }
Tejun Heo598971b2012-03-19 15:10:58 -07003800
Tejun Heof4da8072014-09-08 08:15:20 +09003801 cic->blkcg_serial_nr = serial_nr;
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003802 return nonroot_cg;
Vivek Goyal246103332009-12-03 12:59:51 -05003803}
Tejun Heo598971b2012-03-19 15:10:58 -07003804#else
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07003805static inline bool check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
3806{
3807 return false;
3808}
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;
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004452 bool disable_wbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Tejun Heo216284c32011-12-14 00:33:38 +01004454 spin_lock_irq(q->queue_lock);
Tejun Heof1f8cc92011-12-14 00:33:42 +01004455
Tejun Heo598971b2012-03-19 15:10:58 -07004456 check_ioprio_changed(cic, bio);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004457 disable_wbt = check_blkcg_changed(cic, bio);
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004458new_queue:
Vasily Tarasov91fac312007-04-25 12:29:51 +02004459 cfqq = cic_to_cfqq(cic, is_sync);
Vivek Goyal32f2e802009-07-09 22:13:16 +02004460 if (!cfqq || cfqq == &cfqd->oom_cfqq) {
Tejun Heobce61332015-08-18 14:54:59 -07004461 if (cfqq)
4462 cfq_put_queue(cfqq);
Tejun Heo2da8de02015-08-18 14:55:02 -07004463 cfqq = cfq_get_queue(cfqd, is_sync, cic, bio);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004464 cic_set_cfqq(cic, cfqq, is_sync);
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004465 } else {
4466 /*
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004467 * If the queue was seeky for too long, break it apart.
4468 */
Shaohua Liae54abe2010-02-05 13:11:45 +01004469 if (cfq_cfqq_coop(cfqq) && cfq_cfqq_split_coop(cfqq)) {
Jeff Moyere6c5bc72009-10-23 17:14:52 -04004470 cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
4471 cfqq = split_cfqq(cic, cfqq);
4472 if (!cfqq)
4473 goto new_queue;
4474 }
4475
4476 /*
Jeff Moyerdf5fe3e2009-10-23 17:14:50 -04004477 * Check to see if this queue is scheduled to merge with
4478 * another, closely cooperating queue. The merging of
4479 * queues happens here as it must be done in process context.
4480 * The reference on new_cfqq was taken in merge_cfqqs.
4481 */
4482 if (cfqq->new_cfqq)
4483 cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
Vasily Tarasov91fac312007-04-25 12:29:51 +02004484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 cfqq->allocated[rw]++;
Jens Axboe5e705372006-07-13 12:39:25 +02004487
Jens Axboe6fae9c22011-03-01 15:04:39 -05004488 cfqq->ref++;
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004489 cfqg_get(cfqq->cfqg);
Tejun Heoa612fdd2011-12-14 00:33:41 +01004490 rq->elv.priv[0] = cfqq;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004491 rq->elv.priv[1] = cfqq->cfqg;
Tejun Heo216284c32011-12-14 00:33:38 +01004492 spin_unlock_irq(q->queue_lock);
Jens Axboe5d7f5ce2017-02-16 07:57:33 -07004493
4494 if (disable_wbt)
4495 wbt_disable_default(q);
4496
Jens Axboe5e705372006-07-13 12:39:25 +02004497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
David Howells65f27f32006-11-22 14:55:48 +00004500static void cfq_kick_queue(struct work_struct *work)
Jens Axboe22e2c502005-06-27 10:55:12 +02004501{
David Howells65f27f32006-11-22 14:55:48 +00004502 struct cfq_data *cfqd =
Jens Axboe23e018a2009-10-05 08:52:35 +02004503 container_of(work, struct cfq_data, unplug_work);
Jens Axboe165125e2007-07-24 09:28:11 +02004504 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004505
Jens Axboe40bb54d2009-04-15 12:11:10 +02004506 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02004507 __blk_run_queue(cfqd->queue);
Jens Axboe40bb54d2009-04-15 12:11:10 +02004508 spin_unlock_irq(q->queue_lock);
Jens Axboe22e2c502005-06-27 10:55:12 +02004509}
4510
4511/*
4512 * Timer running if the active_queue is currently idling inside its time slice
4513 */
Jan Kara91148322016-06-08 15:11:39 +02004514static enum hrtimer_restart cfq_idle_slice_timer(struct hrtimer *timer)
Jens Axboe22e2c502005-06-27 10:55:12 +02004515{
Jan Kara91148322016-06-08 15:11:39 +02004516 struct cfq_data *cfqd = container_of(timer, struct cfq_data,
4517 idle_slice_timer);
Jens Axboe22e2c502005-06-27 10:55:12 +02004518 struct cfq_queue *cfqq;
4519 unsigned long flags;
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004520 int timed_out = 1;
Jens Axboe22e2c502005-06-27 10:55:12 +02004521
Jens Axboe7b679132008-05-30 12:23:07 +02004522 cfq_log(cfqd, "idle timer fired");
4523
Jens Axboe22e2c502005-06-27 10:55:12 +02004524 spin_lock_irqsave(cfqd->queue->queue_lock, flags);
4525
Jens Axboefe094d92008-01-31 13:08:54 +01004526 cfqq = cfqd->active_queue;
4527 if (cfqq) {
Jens Axboe3c6bd2f2007-01-19 12:06:33 +11004528 timed_out = 0;
4529
Jens Axboe22e2c502005-06-27 10:55:12 +02004530 /*
Jens Axboeb0291952009-04-07 11:38:31 +02004531 * We saw a request before the queue expired, let it through
4532 */
4533 if (cfq_cfqq_must_dispatch(cfqq))
4534 goto out_kick;
4535
4536 /*
Jens Axboe22e2c502005-06-27 10:55:12 +02004537 * expired
4538 */
Jens Axboe44f7c162007-01-19 11:51:58 +11004539 if (cfq_slice_used(cfqq))
Jens Axboe22e2c502005-06-27 10:55:12 +02004540 goto expire;
4541
4542 /*
4543 * only expire and reinvoke request handler, if there are
4544 * other queues with pending requests
4545 */
Jens Axboecaaa5f92006-06-16 11:23:00 +02004546 if (!cfqd->busy_queues)
Jens Axboe22e2c502005-06-27 10:55:12 +02004547 goto out_cont;
Jens Axboe22e2c502005-06-27 10:55:12 +02004548
4549 /*
4550 * not expired and it has a request pending, let it dispatch
4551 */
Jens Axboe75e50982009-04-07 08:56:14 +02004552 if (!RB_EMPTY_ROOT(&cfqq->sort_list))
Jens Axboe22e2c502005-06-27 10:55:12 +02004553 goto out_kick;
Corrado Zoccolo76280af2009-11-26 10:02:58 +01004554
4555 /*
4556 * Queue depth flag is reset only when the idle didn't succeed
4557 */
4558 cfq_clear_cfqq_deep(cfqq);
Jens Axboe22e2c502005-06-27 10:55:12 +02004559 }
4560expire:
Vivek Goyale5ff0822010-04-26 19:25:11 +02004561 cfq_slice_expired(cfqd, timed_out);
Jens Axboe22e2c502005-06-27 10:55:12 +02004562out_kick:
Jens Axboe23e018a2009-10-05 08:52:35 +02004563 cfq_schedule_dispatch(cfqd);
Jens Axboe22e2c502005-06-27 10:55:12 +02004564out_cont:
4565 spin_unlock_irqrestore(cfqd->queue->queue_lock, flags);
Jan Kara91148322016-06-08 15:11:39 +02004566 return HRTIMER_NORESTART;
Jens Axboe22e2c502005-06-27 10:55:12 +02004567}
4568
Jens Axboe3b181522005-06-27 10:56:24 +02004569static void cfq_shutdown_timer_wq(struct cfq_data *cfqd)
4570{
Jan Kara91148322016-06-08 15:11:39 +02004571 hrtimer_cancel(&cfqd->idle_slice_timer);
Jens Axboe23e018a2009-10-05 08:52:35 +02004572 cancel_work_sync(&cfqd->unplug_work);
Jens Axboe3b181522005-06-27 10:56:24 +02004573}
Jens Axboe22e2c502005-06-27 10:55:12 +02004574
Jens Axboeb374d182008-10-31 10:05:07 +01004575static void cfq_exit_queue(struct elevator_queue *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Jens Axboe22e2c502005-06-27 10:55:12 +02004577 struct cfq_data *cfqd = e->elevator_data;
Jens Axboe165125e2007-07-24 09:28:11 +02004578 struct request_queue *q = cfqd->queue;
Jens Axboe22e2c502005-06-27 10:55:12 +02004579
Jens Axboe3b181522005-06-27 10:56:24 +02004580 cfq_shutdown_timer_wq(cfqd);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004581
Al Virod9ff4182006-03-18 13:51:22 -05004582 spin_lock_irq(q->queue_lock);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004583
Al Virod9ff4182006-03-18 13:51:22 -05004584 if (cfqd->active_queue)
Vivek Goyale5ff0822010-04-26 19:25:11 +02004585 __cfq_slice_expired(cfqd, cfqd->active_queue, 0);
Jens Axboee2d74ac2006-03-28 08:59:01 +02004586
Tejun Heo03aa2642012-03-05 13:15:19 -08004587 spin_unlock_irq(q->queue_lock);
4588
Al Viroa90d7422006-03-18 12:05:37 -05004589 cfq_shutdown_timer_wq(cfqd);
4590
Tejun Heoffea73f2012-06-04 10:02:29 +02004591#ifdef CONFIG_CFQ_GROUP_IOSCHED
4592 blkcg_deactivate_policy(q, &blkcg_policy_cfq);
4593#else
Tejun Heof51b8022012-03-05 13:15:05 -08004594 kfree(cfqd->root_group);
Vivek Goyal2abae552011-05-23 10:02:19 +02004595#endif
Vivek Goyal56edf7d2011-05-19 15:38:22 -04004596 kfree(cfqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597}
4598
Jianpeng Mad50235b2013-07-03 13:25:24 +02004599static int cfq_init_queue(struct request_queue *q, struct elevator_type *e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
4601 struct cfq_data *cfqd;
Tejun Heo3c798392012-04-16 13:57:25 -07004602 struct blkcg_gq *blkg __maybe_unused;
Tejun Heoa2b16932012-04-13 13:11:33 -07004603 int i, ret;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004604 struct elevator_queue *eq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Jianpeng Mad50235b2013-07-03 13:25:24 +02004606 eq = elevator_alloc(q, e);
4607 if (!eq)
Tejun Heob2fab5a2012-03-05 13:14:57 -08004608 return -ENOMEM;
Konstantin Khlebnikov80b15c72010-05-20 23:21:41 +04004609
Joe Perchesc1b511e2013-08-29 15:21:42 -07004610 cfqd = kzalloc_node(sizeof(*cfqd), GFP_KERNEL, q->node);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004611 if (!cfqd) {
4612 kobject_put(&eq->kobj);
4613 return -ENOMEM;
4614 }
4615 eq->elevator_data = cfqd;
4616
Tejun Heof51b8022012-03-05 13:15:05 -08004617 cfqd->queue = q;
Jianpeng Mad50235b2013-07-03 13:25:24 +02004618 spin_lock_irq(q->queue_lock);
4619 q->elevator = eq;
4620 spin_unlock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004621
Vivek Goyal1fa8f6d2009-12-03 12:59:41 -05004622 /* Init root service tree */
4623 cfqd->grp_service_tree = CFQ_RB_ROOT;
4624
Tejun Heof51b8022012-03-05 13:15:05 -08004625 /* Init root group and prefer root group over other groups by default */
Vivek Goyal25fb5162009-12-03 12:59:46 -05004626#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004627 ret = blkcg_activate_policy(q, &blkcg_policy_cfq);
Tejun Heoa2b16932012-04-13 13:11:33 -07004628 if (ret)
4629 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004630
Tejun Heoa2b16932012-04-13 13:11:33 -07004631 cfqd->root_group = blkg_to_cfqg(q->root_blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08004632#else
Tejun Heoa2b16932012-04-13 13:11:33 -07004633 ret = -ENOMEM;
Tejun Heof51b8022012-03-05 13:15:05 -08004634 cfqd->root_group = kzalloc_node(sizeof(*cfqd->root_group),
4635 GFP_KERNEL, cfqd->queue->node);
Tejun Heoa2b16932012-04-13 13:11:33 -07004636 if (!cfqd->root_group)
4637 goto out_free;
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004638
Tejun Heoa2b16932012-04-13 13:11:33 -07004639 cfq_init_cfqg_base(cfqd->root_group);
Tejun Heo3ecca622015-08-18 14:55:35 -07004640 cfqd->root_group->weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
4641 cfqd->root_group->leaf_weight = 2 * CFQ_WEIGHT_LEGACY_DFL;
Tejun Heo69d7fde2015-08-18 14:55:36 -07004642#endif
Vivek Goyal5624a4e2011-05-19 15:38:28 -04004643
Jens Axboe26a2ac02009-04-23 12:13:27 +02004644 /*
4645 * Not strictly needed (since RB_ROOT just clears the node and we
4646 * zeroed cfqd on alloc), but better be safe in case someone decides
4647 * to add magic to the rb code
4648 */
4649 for (i = 0; i < CFQ_PRIO_LISTS; i++)
4650 cfqd->prio_trees[i] = RB_ROOT;
4651
Jens Axboe6118b702009-06-30 09:34:12 +02004652 /*
Tejun Heod4aad7f2015-08-18 14:55:04 -07004653 * Our fallback cfqq if cfq_get_queue() runs into OOM issues.
Jens Axboe6118b702009-06-30 09:34:12 +02004654 * Grab a permanent reference to it, so that the normal code flow
Tejun Heof51b8022012-03-05 13:15:05 -08004655 * will not attempt to free it. oom_cfqq is linked to root_group
4656 * but shouldn't hold a reference as it'll never be unlinked. Lose
4657 * the reference from linking right away.
Jens Axboe6118b702009-06-30 09:34:12 +02004658 */
4659 cfq_init_cfqq(cfqd, &cfqd->oom_cfqq, 1, 0);
Shaohua Li30d7b942011-01-07 08:46:59 +01004660 cfqd->oom_cfqq.ref++;
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004661
4662 spin_lock_irq(q->queue_lock);
Tejun Heof51b8022012-03-05 13:15:05 -08004663 cfq_link_cfqq_cfqg(&cfqd->oom_cfqq, cfqd->root_group);
Tejun Heoeb7d8c072012-03-23 14:02:53 +01004664 cfqg_put(cfqd->root_group);
Tejun Heo1adaf3d2012-03-05 13:15:15 -08004665 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Jan Kara91148322016-06-08 15:11:39 +02004667 hrtimer_init(&cfqd->idle_slice_timer, CLOCK_MONOTONIC,
4668 HRTIMER_MODE_REL);
Jens Axboe22e2c502005-06-27 10:55:12 +02004669 cfqd->idle_slice_timer.function = cfq_idle_slice_timer;
Jens Axboe22e2c502005-06-27 10:55:12 +02004670
Jens Axboe23e018a2009-10-05 08:52:35 +02004671 INIT_WORK(&cfqd->unplug_work, cfq_kick_queue);
Jens Axboe22e2c502005-06-27 10:55:12 +02004672
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 cfqd->cfq_quantum = cfq_quantum;
Jens Axboe22e2c502005-06-27 10:55:12 +02004674 cfqd->cfq_fifo_expire[0] = cfq_fifo_expire[0];
4675 cfqd->cfq_fifo_expire[1] = cfq_fifo_expire[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 cfqd->cfq_back_max = cfq_back_max;
4677 cfqd->cfq_back_penalty = cfq_back_penalty;
Jens Axboe22e2c502005-06-27 10:55:12 +02004678 cfqd->cfq_slice[0] = cfq_slice_async;
4679 cfqd->cfq_slice[1] = cfq_slice_sync;
Tao Ma5bf14c02012-04-01 14:33:39 -07004680 cfqd->cfq_target_latency = cfq_target_latency;
Jens Axboe22e2c502005-06-27 10:55:12 +02004681 cfqd->cfq_slice_async_rq = cfq_slice_async_rq;
Jens Axboe0bb97942015-06-10 08:01:20 -06004682 cfqd->cfq_slice_idle = cfq_slice_idle;
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004683 cfqd->cfq_group_idle = cfq_group_idle;
Jens Axboe963b72f2009-10-03 19:42:18 +02004684 cfqd->cfq_latency = 1;
Corrado Zoccoloe459dd02009-11-26 10:02:57 +01004685 cfqd->hw_tag = -1;
Corrado Zoccoloedc71132009-12-09 20:56:04 +01004686 /*
4687 * we optimistically start assuming sync ops weren't delayed in last
4688 * second, in order to have larger depth for async operations.
4689 */
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004690 cfqd->last_delayed_sync = ktime_get_ns() - NSEC_PER_SEC;
Tejun Heob2fab5a2012-03-05 13:14:57 -08004691 return 0;
Tejun Heoa2b16932012-04-13 13:11:33 -07004692
4693out_free:
4694 kfree(cfqd);
Jianpeng Mad50235b2013-07-03 13:25:24 +02004695 kobject_put(&eq->kobj);
Tejun Heoa2b16932012-04-13 13:11:33 -07004696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Jens Axboe0bb97942015-06-10 08:01:20 -06004699static void cfq_registered_queue(struct request_queue *q)
4700{
4701 struct elevator_queue *e = q->elevator;
4702 struct cfq_data *cfqd = e->elevator_data;
4703
4704 /*
4705 * Default to IOPS mode with no idling for SSDs
4706 */
4707 if (blk_queue_nonrot(q))
4708 cfqd->cfq_slice_idle = 0;
4709}
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711/*
4712 * sysfs parts below -->
4713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714static ssize_t
4715cfq_var_show(unsigned int var, char *page)
4716{
Masanari Iida176167a2014-04-28 12:38:34 +09004717 return sprintf(page, "%u\n", var);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
4720static ssize_t
4721cfq_var_store(unsigned int *var, const char *page, size_t count)
4722{
4723 char *p = (char *) page;
4724
4725 *var = simple_strtoul(p, &p, 10);
4726 return count;
4727}
4728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004730static ssize_t __FUNC(struct elevator_queue *e, char *page) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004732 struct cfq_data *cfqd = e->elevator_data; \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004733 u64 __data = __VAR; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004735 __data = div_u64(__data, NSEC_PER_MSEC); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 return cfq_var_show(__data, (page)); \
4737}
4738SHOW_FUNCTION(cfq_quantum_show, cfqd->cfq_quantum, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004739SHOW_FUNCTION(cfq_fifo_expire_sync_show, cfqd->cfq_fifo_expire[1], 1);
4740SHOW_FUNCTION(cfq_fifo_expire_async_show, cfqd->cfq_fifo_expire[0], 1);
Al Viroe572ec72006-03-18 22:27:18 -05004741SHOW_FUNCTION(cfq_back_seek_max_show, cfqd->cfq_back_max, 0);
4742SHOW_FUNCTION(cfq_back_seek_penalty_show, cfqd->cfq_back_penalty, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004743SHOW_FUNCTION(cfq_slice_idle_show, cfqd->cfq_slice_idle, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004744SHOW_FUNCTION(cfq_group_idle_show, cfqd->cfq_group_idle, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004745SHOW_FUNCTION(cfq_slice_sync_show, cfqd->cfq_slice[1], 1);
4746SHOW_FUNCTION(cfq_slice_async_show, cfqd->cfq_slice[0], 1);
4747SHOW_FUNCTION(cfq_slice_async_rq_show, cfqd->cfq_slice_async_rq, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004748SHOW_FUNCTION(cfq_low_latency_show, cfqd->cfq_latency, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004749SHOW_FUNCTION(cfq_target_latency_show, cfqd->cfq_target_latency, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750#undef SHOW_FUNCTION
4751
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004752#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
4753static ssize_t __FUNC(struct elevator_queue *e, char *page) \
4754{ \
4755 struct cfq_data *cfqd = e->elevator_data; \
4756 u64 __data = __VAR; \
4757 __data = div_u64(__data, NSEC_PER_USEC); \
4758 return cfq_var_show(__data, (page)); \
4759}
4760USEC_SHOW_FUNCTION(cfq_slice_idle_us_show, cfqd->cfq_slice_idle);
4761USEC_SHOW_FUNCTION(cfq_group_idle_us_show, cfqd->cfq_group_idle);
4762USEC_SHOW_FUNCTION(cfq_slice_sync_us_show, cfqd->cfq_slice[1]);
4763USEC_SHOW_FUNCTION(cfq_slice_async_us_show, cfqd->cfq_slice[0]);
4764USEC_SHOW_FUNCTION(cfq_target_latency_us_show, cfqd->cfq_target_latency);
4765#undef USEC_SHOW_FUNCTION
4766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
Jens Axboeb374d182008-10-31 10:05:07 +01004768static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{ \
Al Viro3d1ab402006-03-18 18:35:43 -05004770 struct cfq_data *cfqd = e->elevator_data; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 unsigned int __data; \
4772 int ret = cfq_var_store(&__data, (page), count); \
4773 if (__data < (MIN)) \
4774 __data = (MIN); \
4775 else if (__data > (MAX)) \
4776 __data = (MAX); \
4777 if (__CONV) \
Jeff Moyer9a7f38c2016-06-08 08:55:34 -06004778 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 else \
4780 *(__PTR) = __data; \
4781 return ret; \
4782}
4783STORE_FUNCTION(cfq_quantum_store, &cfqd->cfq_quantum, 1, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004784STORE_FUNCTION(cfq_fifo_expire_sync_store, &cfqd->cfq_fifo_expire[1], 1,
4785 UINT_MAX, 1);
4786STORE_FUNCTION(cfq_fifo_expire_async_store, &cfqd->cfq_fifo_expire[0], 1,
4787 UINT_MAX, 1);
Al Viroe572ec72006-03-18 22:27:18 -05004788STORE_FUNCTION(cfq_back_seek_max_store, &cfqd->cfq_back_max, 0, UINT_MAX, 0);
Jens Axboefe094d92008-01-31 13:08:54 +01004789STORE_FUNCTION(cfq_back_seek_penalty_store, &cfqd->cfq_back_penalty, 1,
4790 UINT_MAX, 0);
Jens Axboe22e2c502005-06-27 10:55:12 +02004791STORE_FUNCTION(cfq_slice_idle_store, &cfqd->cfq_slice_idle, 0, UINT_MAX, 1);
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004792STORE_FUNCTION(cfq_group_idle_store, &cfqd->cfq_group_idle, 0, UINT_MAX, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02004793STORE_FUNCTION(cfq_slice_sync_store, &cfqd->cfq_slice[1], 1, UINT_MAX, 1);
4794STORE_FUNCTION(cfq_slice_async_store, &cfqd->cfq_slice[0], 1, UINT_MAX, 1);
Jens Axboefe094d92008-01-31 13:08:54 +01004795STORE_FUNCTION(cfq_slice_async_rq_store, &cfqd->cfq_slice_async_rq, 1,
4796 UINT_MAX, 0);
Jens Axboe963b72f2009-10-03 19:42:18 +02004797STORE_FUNCTION(cfq_low_latency_store, &cfqd->cfq_latency, 0, 1, 0);
Tao Ma5bf14c02012-04-01 14:33:39 -07004798STORE_FUNCTION(cfq_target_latency_store, &cfqd->cfq_target_latency, 1, UINT_MAX, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799#undef STORE_FUNCTION
4800
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004801#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
4802static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
4803{ \
4804 struct cfq_data *cfqd = e->elevator_data; \
4805 unsigned int __data; \
4806 int ret = cfq_var_store(&__data, (page), count); \
4807 if (__data < (MIN)) \
4808 __data = (MIN); \
4809 else if (__data > (MAX)) \
4810 __data = (MAX); \
4811 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
4812 return ret; \
4813}
4814USEC_STORE_FUNCTION(cfq_slice_idle_us_store, &cfqd->cfq_slice_idle, 0, UINT_MAX);
4815USEC_STORE_FUNCTION(cfq_group_idle_us_store, &cfqd->cfq_group_idle, 0, UINT_MAX);
4816USEC_STORE_FUNCTION(cfq_slice_sync_us_store, &cfqd->cfq_slice[1], 1, UINT_MAX);
4817USEC_STORE_FUNCTION(cfq_slice_async_us_store, &cfqd->cfq_slice[0], 1, UINT_MAX);
4818USEC_STORE_FUNCTION(cfq_target_latency_us_store, &cfqd->cfq_target_latency, 1, UINT_MAX);
4819#undef USEC_STORE_FUNCTION
4820
Al Viroe572ec72006-03-18 22:27:18 -05004821#define CFQ_ATTR(name) \
4822 __ATTR(name, S_IRUGO|S_IWUSR, cfq_##name##_show, cfq_##name##_store)
Jens Axboe3b181522005-06-27 10:56:24 +02004823
Al Viroe572ec72006-03-18 22:27:18 -05004824static struct elv_fs_entry cfq_attrs[] = {
4825 CFQ_ATTR(quantum),
Al Viroe572ec72006-03-18 22:27:18 -05004826 CFQ_ATTR(fifo_expire_sync),
4827 CFQ_ATTR(fifo_expire_async),
4828 CFQ_ATTR(back_seek_max),
4829 CFQ_ATTR(back_seek_penalty),
4830 CFQ_ATTR(slice_sync),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004831 CFQ_ATTR(slice_sync_us),
Al Viroe572ec72006-03-18 22:27:18 -05004832 CFQ_ATTR(slice_async),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004833 CFQ_ATTR(slice_async_us),
Al Viroe572ec72006-03-18 22:27:18 -05004834 CFQ_ATTR(slice_async_rq),
4835 CFQ_ATTR(slice_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004836 CFQ_ATTR(slice_idle_us),
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004837 CFQ_ATTR(group_idle),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004838 CFQ_ATTR(group_idle_us),
Jens Axboe963b72f2009-10-03 19:42:18 +02004839 CFQ_ATTR(low_latency),
Tao Ma5bf14c02012-04-01 14:33:39 -07004840 CFQ_ATTR(target_latency),
Jeff Moyerd2d481d2016-06-08 15:11:38 +02004841 CFQ_ATTR(target_latency_us),
Al Viroe572ec72006-03-18 22:27:18 -05004842 __ATTR_NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843};
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845static struct elevator_type iosched_cfq = {
Jens Axboec51ca6c2016-12-10 15:13:59 -07004846 .ops.sq = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 .elevator_merge_fn = cfq_merge,
4848 .elevator_merged_fn = cfq_merged_request,
4849 .elevator_merge_req_fn = cfq_merged_requests,
Tahsin Erdogan72ef7992016-07-07 11:48:22 -07004850 .elevator_allow_bio_merge_fn = cfq_allow_bio_merge,
4851 .elevator_allow_rq_merge_fn = cfq_allow_rq_merge,
Divyesh Shah812d4022010-04-08 21:14:23 -07004852 .elevator_bio_merged_fn = cfq_bio_merged,
Jens Axboeb4878f22005-10-20 16:42:29 +02004853 .elevator_dispatch_fn = cfq_dispatch_requests,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 .elevator_add_req_fn = cfq_insert_request,
Jens Axboeb4878f22005-10-20 16:42:29 +02004855 .elevator_activate_req_fn = cfq_activate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 .elevator_deactivate_req_fn = cfq_deactivate_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 .elevator_completed_req_fn = cfq_completed_request,
Jens Axboe21183b02006-07-13 12:33:14 +02004858 .elevator_former_req_fn = elv_rb_former_request,
4859 .elevator_latter_req_fn = elv_rb_latter_request,
Tejun Heo9b84cac2011-12-14 00:33:42 +01004860 .elevator_init_icq_fn = cfq_init_icq,
Tejun Heo7e5a8792011-12-14 00:33:42 +01004861 .elevator_exit_icq_fn = cfq_exit_icq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 .elevator_set_req_fn = cfq_set_request,
4863 .elevator_put_req_fn = cfq_put_request,
4864 .elevator_may_queue_fn = cfq_may_queue,
4865 .elevator_init_fn = cfq_init_queue,
4866 .elevator_exit_fn = cfq_exit_queue,
Jens Axboe0bb97942015-06-10 08:01:20 -06004867 .elevator_registered_fn = cfq_registered_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 },
Tejun Heo3d3c2372011-12-14 00:33:42 +01004869 .icq_size = sizeof(struct cfq_io_cq),
4870 .icq_align = __alignof__(struct cfq_io_cq),
Al Viro3d1ab402006-03-18 18:35:43 -05004871 .elevator_attrs = cfq_attrs,
Tejun Heo3d3c2372011-12-14 00:33:42 +01004872 .elevator_name = "cfq",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 .elevator_owner = THIS_MODULE,
4874};
4875
Vivek Goyal3e252062009-12-04 10:36:42 -05004876#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004877static struct blkcg_policy blkcg_policy_cfq = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07004878 .dfl_cftypes = cfq_blkcg_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07004879 .legacy_cftypes = cfq_blkcg_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004880
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004881 .cpd_alloc_fn = cfq_cpd_alloc,
Arianna Avanzinie48453c2015-06-05 23:38:42 +02004882 .cpd_init_fn = cfq_cpd_init,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004883 .cpd_free_fn = cfq_cpd_free,
Tejun Heo69d7fde2015-08-18 14:55:36 -07004884 .cpd_bind_fn = cfq_cpd_bind,
Tejun Heoe4a9bde2015-08-18 14:55:16 -07004885
Tejun Heo001bea72015-08-18 14:55:11 -07004886 .pd_alloc_fn = cfq_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004887 .pd_init_fn = cfq_pd_init,
Tejun Heo0b399202013-01-09 08:05:13 -08004888 .pd_offline_fn = cfq_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07004889 .pd_free_fn = cfq_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07004890 .pd_reset_stats_fn = cfq_pd_reset_stats,
Vivek Goyal3e252062009-12-04 10:36:42 -05004891};
Vivek Goyal3e252062009-12-04 10:36:42 -05004892#endif
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894static int __init cfq_init(void)
4895{
Tejun Heo3d3c2372011-12-14 00:33:42 +01004896 int ret;
4897
Vivek Goyal80bdf0c2010-08-23 12:24:26 +02004898#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004899 ret = blkcg_policy_register(&blkcg_policy_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004900 if (ret)
4901 return ret;
Tejun Heoffea73f2012-06-04 10:02:29 +02004902#else
4903 cfq_group_idle = 0;
4904#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004905
Tejun Heofd794952012-06-04 10:01:38 +02004906 ret = -ENOMEM;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004907 cfq_pool = KMEM_CACHE(cfq_queue, 0);
4908 if (!cfq_pool)
Tejun Heo8bd435b2012-04-13 13:11:28 -07004909 goto err_pol_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Tejun Heo3d3c2372011-12-14 00:33:42 +01004911 ret = elv_register(&iosched_cfq);
Tejun Heo8bd435b2012-04-13 13:11:28 -07004912 if (ret)
4913 goto err_free_pool;
Tejun Heo3d3c2372011-12-14 00:33:42 +01004914
Adrian Bunk2fdd82b2007-12-12 18:51:56 +01004915 return 0;
Tejun Heo8bd435b2012-04-13 13:11:28 -07004916
4917err_free_pool:
4918 kmem_cache_destroy(cfq_pool);
4919err_pol_unreg:
Tejun Heoffea73f2012-06-04 10:02:29 +02004920#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004921 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004922#endif
Tejun Heo8bd435b2012-04-13 13:11:28 -07004923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
4926static void __exit cfq_exit(void)
4927{
Tejun Heoffea73f2012-06-04 10:02:29 +02004928#ifdef CONFIG_CFQ_GROUP_IOSCHED
Tejun Heo3c798392012-04-16 13:57:25 -07004929 blkcg_policy_unregister(&blkcg_policy_cfq);
Tejun Heoffea73f2012-06-04 10:02:29 +02004930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 elv_unregister(&iosched_cfq);
Tejun Heo3d3c2372011-12-14 00:33:42 +01004932 kmem_cache_destroy(cfq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
4935module_init(cfq_init);
4936module_exit(cfq_exit);
4937
4938MODULE_AUTHOR("Jens Axboe");
4939MODULE_LICENSE("GPL");
4940MODULE_DESCRIPTION("Completely Fair Queueing IO scheduler");