xref: /OK3568_Linux_fs/kernel/block/bfq-iosched.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Budget Fair Queueing (BFQ) I/O scheduler.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Based on ideas and code from CFQ:
6*4882a593Smuzhiyun  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
9*4882a593Smuzhiyun  *		      Paolo Valente <paolo.valente@unimore.it>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
12*4882a593Smuzhiyun  *                    Arianna Avanzini <avanzini@google.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Copyright (C) 2017 Paolo Valente <paolo.valente@linaro.org>
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * BFQ is a proportional-share I/O scheduler, with some extra
17*4882a593Smuzhiyun  * low-latency capabilities. BFQ also supports full hierarchical
18*4882a593Smuzhiyun  * scheduling through cgroups. Next paragraphs provide an introduction
19*4882a593Smuzhiyun  * on BFQ inner workings. Details on BFQ benefits, usage and
20*4882a593Smuzhiyun  * limitations can be found in Documentation/block/bfq-iosched.rst.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * BFQ is a proportional-share storage-I/O scheduling algorithm based
23*4882a593Smuzhiyun  * on the slice-by-slice service scheme of CFQ. But BFQ assigns
24*4882a593Smuzhiyun  * budgets, measured in number of sectors, to processes instead of
25*4882a593Smuzhiyun  * time slices. The device is not granted to the in-service process
26*4882a593Smuzhiyun  * for a given time slice, but until it has exhausted its assigned
27*4882a593Smuzhiyun  * budget. This change from the time to the service domain enables BFQ
28*4882a593Smuzhiyun  * to distribute the device throughput among processes as desired,
29*4882a593Smuzhiyun  * without any distortion due to throughput fluctuations, or to device
30*4882a593Smuzhiyun  * internal queueing. BFQ uses an ad hoc internal scheduler, called
31*4882a593Smuzhiyun  * B-WF2Q+, to schedule processes according to their budgets. More
32*4882a593Smuzhiyun  * precisely, BFQ schedules queues associated with processes. Each
33*4882a593Smuzhiyun  * process/queue is assigned a user-configurable weight, and B-WF2Q+
34*4882a593Smuzhiyun  * guarantees that each queue receives a fraction of the throughput
35*4882a593Smuzhiyun  * proportional to its weight. Thanks to the accurate policy of
36*4882a593Smuzhiyun  * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound
37*4882a593Smuzhiyun  * processes issuing sequential requests (to boost the throughput),
38*4882a593Smuzhiyun  * and yet guarantee a low latency to interactive and soft real-time
39*4882a593Smuzhiyun  * applications.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * In particular, to provide these low-latency guarantees, BFQ
42*4882a593Smuzhiyun  * explicitly privileges the I/O of two classes of time-sensitive
43*4882a593Smuzhiyun  * applications: interactive and soft real-time. In more detail, BFQ
44*4882a593Smuzhiyun  * behaves this way if the low_latency parameter is set (default
45*4882a593Smuzhiyun  * configuration). This feature enables BFQ to provide applications in
46*4882a593Smuzhiyun  * these classes with a very low latency.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * To implement this feature, BFQ constantly tries to detect whether
49*4882a593Smuzhiyun  * the I/O requests in a bfq_queue come from an interactive or a soft
50*4882a593Smuzhiyun  * real-time application. For brevity, in these cases, the queue is
51*4882a593Smuzhiyun  * said to be interactive or soft real-time. In both cases, BFQ
52*4882a593Smuzhiyun  * privileges the service of the queue, over that of non-interactive
53*4882a593Smuzhiyun  * and non-soft-real-time queues. This privileging is performed,
54*4882a593Smuzhiyun  * mainly, by raising the weight of the queue. So, for brevity, we
55*4882a593Smuzhiyun  * call just weight-raising periods the time periods during which a
56*4882a593Smuzhiyun  * queue is privileged, because deemed interactive or soft real-time.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * The detection of soft real-time queues/applications is described in
59*4882a593Smuzhiyun  * detail in the comments on the function
60*4882a593Smuzhiyun  * bfq_bfqq_softrt_next_start. On the other hand, the detection of an
61*4882a593Smuzhiyun  * interactive queue works as follows: a queue is deemed interactive
62*4882a593Smuzhiyun  * if it is constantly non empty only for a limited time interval,
63*4882a593Smuzhiyun  * after which it does become empty. The queue may be deemed
64*4882a593Smuzhiyun  * interactive again (for a limited time), if it restarts being
65*4882a593Smuzhiyun  * constantly non empty, provided that this happens only after the
66*4882a593Smuzhiyun  * queue has remained empty for a given minimum idle time.
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * By default, BFQ computes automatically the above maximum time
69*4882a593Smuzhiyun  * interval, i.e., the time interval after which a constantly
70*4882a593Smuzhiyun  * non-empty queue stops being deemed interactive. Since a queue is
71*4882a593Smuzhiyun  * weight-raised while it is deemed interactive, this maximum time
72*4882a593Smuzhiyun  * interval happens to coincide with the (maximum) duration of the
73*4882a593Smuzhiyun  * weight-raising for interactive queues.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * Finally, BFQ also features additional heuristics for
76*4882a593Smuzhiyun  * preserving both a low latency and a high throughput on NCQ-capable,
77*4882a593Smuzhiyun  * rotational or flash-based devices, and to get the job done quickly
78*4882a593Smuzhiyun  * for applications consisting in many I/O-bound processes.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * NOTE: if the main or only goal, with a given device, is to achieve
81*4882a593Smuzhiyun  * the maximum-possible throughput at all times, then do switch off
82*4882a593Smuzhiyun  * all low-latency heuristics for that device, by setting low_latency
83*4882a593Smuzhiyun  * to 0.
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * BFQ is described in [1], where also a reference to the initial,
86*4882a593Smuzhiyun  * more theoretical paper on BFQ can be found. The interested reader
87*4882a593Smuzhiyun  * can find in the latter paper full details on the main algorithm, as
88*4882a593Smuzhiyun  * well as formulas of the guarantees and formal proofs of all the
89*4882a593Smuzhiyun  * properties.  With respect to the version of BFQ presented in these
90*4882a593Smuzhiyun  * papers, this implementation adds a few more heuristics, such as the
91*4882a593Smuzhiyun  * ones that guarantee a low latency to interactive and soft real-time
92*4882a593Smuzhiyun  * applications, and a hierarchical extension based on H-WF2Q+.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * B-WF2Q+ is based on WF2Q+, which is described in [2], together with
95*4882a593Smuzhiyun  * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+
96*4882a593Smuzhiyun  * with O(log N) complexity derives from the one introduced with EEVDF
97*4882a593Smuzhiyun  * in [3].
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O
100*4882a593Smuzhiyun  *     Scheduler", Proceedings of the First Workshop on Mobile System
101*4882a593Smuzhiyun  *     Technologies (MST-2015), May 2015.
102*4882a593Smuzhiyun  *     http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * [2] Jon C.R. Bennett and H. Zhang, "Hierarchical Packet Fair Queueing
105*4882a593Smuzhiyun  *     Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689,
106*4882a593Smuzhiyun  *     Oct 1997.
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline
111*4882a593Smuzhiyun  *     First: A Flexible and Accurate Mechanism for Proportional Share
112*4882a593Smuzhiyun  *     Resource Allocation", technical report.
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun #include <linux/module.h>
117*4882a593Smuzhiyun #include <linux/slab.h>
118*4882a593Smuzhiyun #include <linux/blkdev.h>
119*4882a593Smuzhiyun #include <linux/cgroup.h>
120*4882a593Smuzhiyun #include <linux/elevator.h>
121*4882a593Smuzhiyun #include <linux/ktime.h>
122*4882a593Smuzhiyun #include <linux/rbtree.h>
123*4882a593Smuzhiyun #include <linux/ioprio.h>
124*4882a593Smuzhiyun #include <linux/sbitmap.h>
125*4882a593Smuzhiyun #include <linux/delay.h>
126*4882a593Smuzhiyun #include <linux/backing-dev.h>
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #include "blk.h"
129*4882a593Smuzhiyun #include "blk-mq.h"
130*4882a593Smuzhiyun #include "blk-mq-tag.h"
131*4882a593Smuzhiyun #include "blk-mq-sched.h"
132*4882a593Smuzhiyun #include "bfq-iosched.h"
133*4882a593Smuzhiyun #include "blk-wbt.h"
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #define BFQ_BFQQ_FNS(name)						\
136*4882a593Smuzhiyun void bfq_mark_bfqq_##name(struct bfq_queue *bfqq)			\
137*4882a593Smuzhiyun {									\
138*4882a593Smuzhiyun 	__set_bit(BFQQF_##name, &(bfqq)->flags);			\
139*4882a593Smuzhiyun }									\
140*4882a593Smuzhiyun void bfq_clear_bfqq_##name(struct bfq_queue *bfqq)			\
141*4882a593Smuzhiyun {									\
142*4882a593Smuzhiyun 	__clear_bit(BFQQF_##name, &(bfqq)->flags);		\
143*4882a593Smuzhiyun }									\
144*4882a593Smuzhiyun int bfq_bfqq_##name(const struct bfq_queue *bfqq)			\
145*4882a593Smuzhiyun {									\
146*4882a593Smuzhiyun 	return test_bit(BFQQF_##name, &(bfqq)->flags);		\
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun BFQ_BFQQ_FNS(just_created);
150*4882a593Smuzhiyun BFQ_BFQQ_FNS(busy);
151*4882a593Smuzhiyun BFQ_BFQQ_FNS(wait_request);
152*4882a593Smuzhiyun BFQ_BFQQ_FNS(non_blocking_wait_rq);
153*4882a593Smuzhiyun BFQ_BFQQ_FNS(fifo_expire);
154*4882a593Smuzhiyun BFQ_BFQQ_FNS(has_short_ttime);
155*4882a593Smuzhiyun BFQ_BFQQ_FNS(sync);
156*4882a593Smuzhiyun BFQ_BFQQ_FNS(IO_bound);
157*4882a593Smuzhiyun BFQ_BFQQ_FNS(in_large_burst);
158*4882a593Smuzhiyun BFQ_BFQQ_FNS(coop);
159*4882a593Smuzhiyun BFQ_BFQQ_FNS(split_coop);
160*4882a593Smuzhiyun BFQ_BFQQ_FNS(softrt_update);
161*4882a593Smuzhiyun BFQ_BFQQ_FNS(has_waker);
162*4882a593Smuzhiyun #undef BFQ_BFQQ_FNS						\
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /* Expiration time of sync (0) and async (1) requests, in ns. */
165*4882a593Smuzhiyun static const u64 bfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /* Maximum backwards seek (magic number lifted from CFQ), in KiB. */
168*4882a593Smuzhiyun static const int bfq_back_max = 16 * 1024;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* Penalty of a backwards seek, in number of sectors. */
171*4882a593Smuzhiyun static const int bfq_back_penalty = 2;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /* Idling period duration, in ns. */
174*4882a593Smuzhiyun static u64 bfq_slice_idle = NSEC_PER_SEC / 125;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /* Minimum number of assigned budgets for which stats are safe to compute. */
177*4882a593Smuzhiyun static const int bfq_stats_min_budgets = 194;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* Default maximum budget values, in sectors and number of requests. */
180*4882a593Smuzhiyun static const int bfq_default_max_budget = 16 * 1024;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun  * When a sync request is dispatched, the queue that contains that
184*4882a593Smuzhiyun  * request, and all the ancestor entities of that queue, are charged
185*4882a593Smuzhiyun  * with the number of sectors of the request. In contrast, if the
186*4882a593Smuzhiyun  * request is async, then the queue and its ancestor entities are
187*4882a593Smuzhiyun  * charged with the number of sectors of the request, multiplied by
188*4882a593Smuzhiyun  * the factor below. This throttles the bandwidth for async I/O,
189*4882a593Smuzhiyun  * w.r.t. to sync I/O, and it is done to counter the tendency of async
190*4882a593Smuzhiyun  * writes to steal I/O throughput to reads.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * The current value of this parameter is the result of a tuning with
193*4882a593Smuzhiyun  * several hardware and software configurations. We tried to find the
194*4882a593Smuzhiyun  * lowest value for which writes do not cause noticeable problems to
195*4882a593Smuzhiyun  * reads. In fact, the lower this parameter, the stabler I/O control,
196*4882a593Smuzhiyun  * in the following respect.  The lower this parameter is, the less
197*4882a593Smuzhiyun  * the bandwidth enjoyed by a group decreases
198*4882a593Smuzhiyun  * - when the group does writes, w.r.t. to when it does reads;
199*4882a593Smuzhiyun  * - when other groups do reads, w.r.t. to when they do writes.
200*4882a593Smuzhiyun  */
201*4882a593Smuzhiyun static const int bfq_async_charge_factor = 3;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /* Default timeout values, in jiffies, approximating CFQ defaults. */
204*4882a593Smuzhiyun const int bfq_timeout = HZ / 8;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /*
207*4882a593Smuzhiyun  * Time limit for merging (see comments in bfq_setup_cooperator). Set
208*4882a593Smuzhiyun  * to the slowest value that, in our tests, proved to be effective in
209*4882a593Smuzhiyun  * removing false positives, while not causing true positives to miss
210*4882a593Smuzhiyun  * queue merging.
211*4882a593Smuzhiyun  *
212*4882a593Smuzhiyun  * As can be deduced from the low time limit below, queue merging, if
213*4882a593Smuzhiyun  * successful, happens at the very beginning of the I/O of the involved
214*4882a593Smuzhiyun  * cooperating processes, as a consequence of the arrival of the very
215*4882a593Smuzhiyun  * first requests from each cooperator.  After that, there is very
216*4882a593Smuzhiyun  * little chance to find cooperators.
217*4882a593Smuzhiyun  */
218*4882a593Smuzhiyun static const unsigned long bfq_merge_time_limit = HZ/10;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun static struct kmem_cache *bfq_pool;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun /* Below this threshold (in ns), we consider thinktime immediate. */
223*4882a593Smuzhiyun #define BFQ_MIN_TT		(2 * NSEC_PER_MSEC)
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun /* hw_tag detection: parallel requests threshold and min samples needed. */
226*4882a593Smuzhiyun #define BFQ_HW_QUEUE_THRESHOLD	3
227*4882a593Smuzhiyun #define BFQ_HW_QUEUE_SAMPLES	32
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun #define BFQQ_SEEK_THR		(sector_t)(8 * 100)
230*4882a593Smuzhiyun #define BFQQ_SECT_THR_NONROT	(sector_t)(2 * 32)
231*4882a593Smuzhiyun #define BFQ_RQ_SEEKY(bfqd, last_pos, rq) \
232*4882a593Smuzhiyun 	(get_sdist(last_pos, rq) >			\
233*4882a593Smuzhiyun 	 BFQQ_SEEK_THR &&				\
234*4882a593Smuzhiyun 	 (!blk_queue_nonrot(bfqd->queue) ||		\
235*4882a593Smuzhiyun 	  blk_rq_sectors(rq) < BFQQ_SECT_THR_NONROT))
236*4882a593Smuzhiyun #define BFQQ_CLOSE_THR		(sector_t)(8 * 1024)
237*4882a593Smuzhiyun #define BFQQ_SEEKY(bfqq)	(hweight32(bfqq->seek_history) > 19)
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun  * Sync random I/O is likely to be confused with soft real-time I/O,
240*4882a593Smuzhiyun  * because it is characterized by limited throughput and apparently
241*4882a593Smuzhiyun  * isochronous arrival pattern. To avoid false positives, queues
242*4882a593Smuzhiyun  * containing only random (seeky) I/O are prevented from being tagged
243*4882a593Smuzhiyun  * as soft real-time.
244*4882a593Smuzhiyun  */
245*4882a593Smuzhiyun #define BFQQ_TOTALLY_SEEKY(bfqq)	(bfqq->seek_history == -1)
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /* Min number of samples required to perform peak-rate update */
248*4882a593Smuzhiyun #define BFQ_RATE_MIN_SAMPLES	32
249*4882a593Smuzhiyun /* Min observation time interval required to perform a peak-rate update (ns) */
250*4882a593Smuzhiyun #define BFQ_RATE_MIN_INTERVAL	(300*NSEC_PER_MSEC)
251*4882a593Smuzhiyun /* Target observation time interval for a peak-rate update (ns) */
252*4882a593Smuzhiyun #define BFQ_RATE_REF_INTERVAL	NSEC_PER_SEC
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun /*
255*4882a593Smuzhiyun  * Shift used for peak-rate fixed precision calculations.
256*4882a593Smuzhiyun  * With
257*4882a593Smuzhiyun  * - the current shift: 16 positions
258*4882a593Smuzhiyun  * - the current type used to store rate: u32
259*4882a593Smuzhiyun  * - the current unit of measure for rate: [sectors/usec], or, more precisely,
260*4882a593Smuzhiyun  *   [(sectors/usec) / 2^BFQ_RATE_SHIFT] to take into account the shift,
261*4882a593Smuzhiyun  * the range of rates that can be stored is
262*4882a593Smuzhiyun  * [1 / 2^BFQ_RATE_SHIFT, 2^(32 - BFQ_RATE_SHIFT)] sectors/usec =
263*4882a593Smuzhiyun  * [1 / 2^16, 2^16] sectors/usec = [15e-6, 65536] sectors/usec =
264*4882a593Smuzhiyun  * [15, 65G] sectors/sec
265*4882a593Smuzhiyun  * Which, assuming a sector size of 512B, corresponds to a range of
266*4882a593Smuzhiyun  * [7.5K, 33T] B/sec
267*4882a593Smuzhiyun  */
268*4882a593Smuzhiyun #define BFQ_RATE_SHIFT		16
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /*
271*4882a593Smuzhiyun  * When configured for computing the duration of the weight-raising
272*4882a593Smuzhiyun  * for interactive queues automatically (see the comments at the
273*4882a593Smuzhiyun  * beginning of this file), BFQ does it using the following formula:
274*4882a593Smuzhiyun  * duration = (ref_rate / r) * ref_wr_duration,
275*4882a593Smuzhiyun  * where r is the peak rate of the device, and ref_rate and
276*4882a593Smuzhiyun  * ref_wr_duration are two reference parameters.  In particular,
277*4882a593Smuzhiyun  * ref_rate is the peak rate of the reference storage device (see
278*4882a593Smuzhiyun  * below), and ref_wr_duration is about the maximum time needed, with
279*4882a593Smuzhiyun  * BFQ and while reading two files in parallel, to load typical large
280*4882a593Smuzhiyun  * applications on the reference device (see the comments on
281*4882a593Smuzhiyun  * max_service_from_wr below, for more details on how ref_wr_duration
282*4882a593Smuzhiyun  * is obtained).  In practice, the slower/faster the device at hand
283*4882a593Smuzhiyun  * is, the more/less it takes to load applications with respect to the
284*4882a593Smuzhiyun  * reference device.  Accordingly, the longer/shorter BFQ grants
285*4882a593Smuzhiyun  * weight raising to interactive applications.
286*4882a593Smuzhiyun  *
287*4882a593Smuzhiyun  * BFQ uses two different reference pairs (ref_rate, ref_wr_duration),
288*4882a593Smuzhiyun  * depending on whether the device is rotational or non-rotational.
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  * In the following definitions, ref_rate[0] and ref_wr_duration[0]
291*4882a593Smuzhiyun  * are the reference values for a rotational device, whereas
292*4882a593Smuzhiyun  * ref_rate[1] and ref_wr_duration[1] are the reference values for a
293*4882a593Smuzhiyun  * non-rotational device. The reference rates are not the actual peak
294*4882a593Smuzhiyun  * rates of the devices used as a reference, but slightly lower
295*4882a593Smuzhiyun  * values. The reason for using slightly lower values is that the
296*4882a593Smuzhiyun  * peak-rate estimator tends to yield slightly lower values than the
297*4882a593Smuzhiyun  * actual peak rate (it can yield the actual peak rate only if there
298*4882a593Smuzhiyun  * is only one process doing I/O, and the process does sequential
299*4882a593Smuzhiyun  * I/O).
300*4882a593Smuzhiyun  *
301*4882a593Smuzhiyun  * The reference peak rates are measured in sectors/usec, left-shifted
302*4882a593Smuzhiyun  * by BFQ_RATE_SHIFT.
303*4882a593Smuzhiyun  */
304*4882a593Smuzhiyun static int ref_rate[2] = {14000, 33000};
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * To improve readability, a conversion function is used to initialize
307*4882a593Smuzhiyun  * the following array, which entails that the array can be
308*4882a593Smuzhiyun  * initialized only in a function.
309*4882a593Smuzhiyun  */
310*4882a593Smuzhiyun static int ref_wr_duration[2];
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun  * BFQ uses the above-detailed, time-based weight-raising mechanism to
314*4882a593Smuzhiyun  * privilege interactive tasks. This mechanism is vulnerable to the
315*4882a593Smuzhiyun  * following false positives: I/O-bound applications that will go on
316*4882a593Smuzhiyun  * doing I/O for much longer than the duration of weight
317*4882a593Smuzhiyun  * raising. These applications have basically no benefit from being
318*4882a593Smuzhiyun  * weight-raised at the beginning of their I/O. On the opposite end,
319*4882a593Smuzhiyun  * while being weight-raised, these applications
320*4882a593Smuzhiyun  * a) unjustly steal throughput to applications that may actually need
321*4882a593Smuzhiyun  * low latency;
322*4882a593Smuzhiyun  * b) make BFQ uselessly perform device idling; device idling results
323*4882a593Smuzhiyun  * in loss of device throughput with most flash-based storage, and may
324*4882a593Smuzhiyun  * increase latencies when used purposelessly.
325*4882a593Smuzhiyun  *
326*4882a593Smuzhiyun  * BFQ tries to reduce these problems, by adopting the following
327*4882a593Smuzhiyun  * countermeasure. To introduce this countermeasure, we need first to
328*4882a593Smuzhiyun  * finish explaining how the duration of weight-raising for
329*4882a593Smuzhiyun  * interactive tasks is computed.
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * For a bfq_queue deemed as interactive, the duration of weight
332*4882a593Smuzhiyun  * raising is dynamically adjusted, as a function of the estimated
333*4882a593Smuzhiyun  * peak rate of the device, so as to be equal to the time needed to
334*4882a593Smuzhiyun  * execute the 'largest' interactive task we benchmarked so far. By
335*4882a593Smuzhiyun  * largest task, we mean the task for which each involved process has
336*4882a593Smuzhiyun  * to do more I/O than for any of the other tasks we benchmarked. This
337*4882a593Smuzhiyun  * reference interactive task is the start-up of LibreOffice Writer,
338*4882a593Smuzhiyun  * and in this task each process/bfq_queue needs to have at most ~110K
339*4882a593Smuzhiyun  * sectors transferred.
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * This last piece of information enables BFQ to reduce the actual
342*4882a593Smuzhiyun  * duration of weight-raising for at least one class of I/O-bound
343*4882a593Smuzhiyun  * applications: those doing sequential or quasi-sequential I/O. An
344*4882a593Smuzhiyun  * example is file copy. In fact, once started, the main I/O-bound
345*4882a593Smuzhiyun  * processes of these applications usually consume the above 110K
346*4882a593Smuzhiyun  * sectors in much less time than the processes of an application that
347*4882a593Smuzhiyun  * is starting, because these I/O-bound processes will greedily devote
348*4882a593Smuzhiyun  * almost all their CPU cycles only to their target,
349*4882a593Smuzhiyun  * throughput-friendly I/O operations. This is even more true if BFQ
350*4882a593Smuzhiyun  * happens to be underestimating the device peak rate, and thus
351*4882a593Smuzhiyun  * overestimating the duration of weight raising. But, according to
352*4882a593Smuzhiyun  * our measurements, once transferred 110K sectors, these processes
353*4882a593Smuzhiyun  * have no right to be weight-raised any longer.
354*4882a593Smuzhiyun  *
355*4882a593Smuzhiyun  * Basing on the last consideration, BFQ ends weight-raising for a
356*4882a593Smuzhiyun  * bfq_queue if the latter happens to have received an amount of
357*4882a593Smuzhiyun  * service at least equal to the following constant. The constant is
358*4882a593Smuzhiyun  * set to slightly more than 110K, to have a minimum safety margin.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * This early ending of weight-raising reduces the amount of time
361*4882a593Smuzhiyun  * during which interactive false positives cause the two problems
362*4882a593Smuzhiyun  * described at the beginning of these comments.
363*4882a593Smuzhiyun  */
364*4882a593Smuzhiyun static const unsigned long max_service_from_wr = 120000;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun #define RQ_BIC(rq)		icq_to_bic((rq)->elv.priv[0])
367*4882a593Smuzhiyun #define RQ_BFQQ(rq)		((rq)->elv.priv[1])
368*4882a593Smuzhiyun 
bic_to_bfqq(struct bfq_io_cq * bic,bool is_sync)369*4882a593Smuzhiyun struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	return bic->bfqq[is_sync];
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
bic_set_bfqq(struct bfq_io_cq * bic,struct bfq_queue * bfqq,bool is_sync)374*4882a593Smuzhiyun void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	bic->bfqq[is_sync] = bfqq;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
bic_to_bfqd(struct bfq_io_cq * bic)379*4882a593Smuzhiyun struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	return bic->icq.q->elevator->elevator_data;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun  * icq_to_bic - convert iocontext queue structure to bfq_io_cq.
386*4882a593Smuzhiyun  * @icq: the iocontext queue.
387*4882a593Smuzhiyun  */
icq_to_bic(struct io_cq * icq)388*4882a593Smuzhiyun static struct bfq_io_cq *icq_to_bic(struct io_cq *icq)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	/* bic->icq is the first member, %NULL will convert to %NULL */
391*4882a593Smuzhiyun 	return container_of(icq, struct bfq_io_cq, icq);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun /**
395*4882a593Smuzhiyun  * bfq_bic_lookup - search into @ioc a bic associated to @bfqd.
396*4882a593Smuzhiyun  * @bfqd: the lookup key.
397*4882a593Smuzhiyun  * @ioc: the io_context of the process doing I/O.
398*4882a593Smuzhiyun  * @q: the request queue.
399*4882a593Smuzhiyun  */
bfq_bic_lookup(struct bfq_data * bfqd,struct io_context * ioc,struct request_queue * q)400*4882a593Smuzhiyun static struct bfq_io_cq *bfq_bic_lookup(struct bfq_data *bfqd,
401*4882a593Smuzhiyun 					struct io_context *ioc,
402*4882a593Smuzhiyun 					struct request_queue *q)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	if (ioc) {
405*4882a593Smuzhiyun 		unsigned long flags;
406*4882a593Smuzhiyun 		struct bfq_io_cq *icq;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 		spin_lock_irqsave(&q->queue_lock, flags);
409*4882a593Smuzhiyun 		icq = icq_to_bic(ioc_lookup_icq(ioc, q));
410*4882a593Smuzhiyun 		spin_unlock_irqrestore(&q->queue_lock, flags);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 		return icq;
413*4882a593Smuzhiyun 	}
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	return NULL;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun /*
419*4882a593Smuzhiyun  * Scheduler run of queue, if there are requests pending and no one in the
420*4882a593Smuzhiyun  * driver that will restart queueing.
421*4882a593Smuzhiyun  */
bfq_schedule_dispatch(struct bfq_data * bfqd)422*4882a593Smuzhiyun void bfq_schedule_dispatch(struct bfq_data *bfqd)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	lockdep_assert_held(&bfqd->lock);
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	if (bfqd->queued != 0) {
427*4882a593Smuzhiyun 		bfq_log(bfqd, "schedule dispatch");
428*4882a593Smuzhiyun 		blk_mq_run_hw_queues(bfqd->queue, true);
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun #define bfq_class_idle(bfqq)	((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun #define bfq_sample_valid(samples)	((samples) > 80)
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun /*
437*4882a593Smuzhiyun  * Lifted from AS - choose which of rq1 and rq2 that is best served now.
438*4882a593Smuzhiyun  * We choose the request that is closer to the head right now.  Distance
439*4882a593Smuzhiyun  * behind the head is penalized and only allowed to a certain extent.
440*4882a593Smuzhiyun  */
bfq_choose_req(struct bfq_data * bfqd,struct request * rq1,struct request * rq2,sector_t last)441*4882a593Smuzhiyun static struct request *bfq_choose_req(struct bfq_data *bfqd,
442*4882a593Smuzhiyun 				      struct request *rq1,
443*4882a593Smuzhiyun 				      struct request *rq2,
444*4882a593Smuzhiyun 				      sector_t last)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	sector_t s1, s2, d1 = 0, d2 = 0;
447*4882a593Smuzhiyun 	unsigned long back_max;
448*4882a593Smuzhiyun #define BFQ_RQ1_WRAP	0x01 /* request 1 wraps */
449*4882a593Smuzhiyun #define BFQ_RQ2_WRAP	0x02 /* request 2 wraps */
450*4882a593Smuzhiyun 	unsigned int wrap = 0; /* bit mask: requests behind the disk head? */
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (!rq1 || rq1 == rq2)
453*4882a593Smuzhiyun 		return rq2;
454*4882a593Smuzhiyun 	if (!rq2)
455*4882a593Smuzhiyun 		return rq1;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (rq_is_sync(rq1) && !rq_is_sync(rq2))
458*4882a593Smuzhiyun 		return rq1;
459*4882a593Smuzhiyun 	else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
460*4882a593Smuzhiyun 		return rq2;
461*4882a593Smuzhiyun 	if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
462*4882a593Smuzhiyun 		return rq1;
463*4882a593Smuzhiyun 	else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META))
464*4882a593Smuzhiyun 		return rq2;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	s1 = blk_rq_pos(rq1);
467*4882a593Smuzhiyun 	s2 = blk_rq_pos(rq2);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 	/*
470*4882a593Smuzhiyun 	 * By definition, 1KiB is 2 sectors.
471*4882a593Smuzhiyun 	 */
472*4882a593Smuzhiyun 	back_max = bfqd->bfq_back_max * 2;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	/*
475*4882a593Smuzhiyun 	 * Strict one way elevator _except_ in the case where we allow
476*4882a593Smuzhiyun 	 * short backward seeks which are biased as twice the cost of a
477*4882a593Smuzhiyun 	 * similar forward seek.
478*4882a593Smuzhiyun 	 */
479*4882a593Smuzhiyun 	if (s1 >= last)
480*4882a593Smuzhiyun 		d1 = s1 - last;
481*4882a593Smuzhiyun 	else if (s1 + back_max >= last)
482*4882a593Smuzhiyun 		d1 = (last - s1) * bfqd->bfq_back_penalty;
483*4882a593Smuzhiyun 	else
484*4882a593Smuzhiyun 		wrap |= BFQ_RQ1_WRAP;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (s2 >= last)
487*4882a593Smuzhiyun 		d2 = s2 - last;
488*4882a593Smuzhiyun 	else if (s2 + back_max >= last)
489*4882a593Smuzhiyun 		d2 = (last - s2) * bfqd->bfq_back_penalty;
490*4882a593Smuzhiyun 	else
491*4882a593Smuzhiyun 		wrap |= BFQ_RQ2_WRAP;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	/* Found required data */
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	/*
496*4882a593Smuzhiyun 	 * By doing switch() on the bit mask "wrap" we avoid having to
497*4882a593Smuzhiyun 	 * check two variables for all permutations: --> faster!
498*4882a593Smuzhiyun 	 */
499*4882a593Smuzhiyun 	switch (wrap) {
500*4882a593Smuzhiyun 	case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
501*4882a593Smuzhiyun 		if (d1 < d2)
502*4882a593Smuzhiyun 			return rq1;
503*4882a593Smuzhiyun 		else if (d2 < d1)
504*4882a593Smuzhiyun 			return rq2;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 		if (s1 >= s2)
507*4882a593Smuzhiyun 			return rq1;
508*4882a593Smuzhiyun 		else
509*4882a593Smuzhiyun 			return rq2;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	case BFQ_RQ2_WRAP:
512*4882a593Smuzhiyun 		return rq1;
513*4882a593Smuzhiyun 	case BFQ_RQ1_WRAP:
514*4882a593Smuzhiyun 		return rq2;
515*4882a593Smuzhiyun 	case BFQ_RQ1_WRAP|BFQ_RQ2_WRAP: /* both rqs wrapped */
516*4882a593Smuzhiyun 	default:
517*4882a593Smuzhiyun 		/*
518*4882a593Smuzhiyun 		 * Since both rqs are wrapped,
519*4882a593Smuzhiyun 		 * start with the one that's further behind head
520*4882a593Smuzhiyun 		 * (--> only *one* back seek required),
521*4882a593Smuzhiyun 		 * since back seek takes more time than forward.
522*4882a593Smuzhiyun 		 */
523*4882a593Smuzhiyun 		if (s1 <= s2)
524*4882a593Smuzhiyun 			return rq1;
525*4882a593Smuzhiyun 		else
526*4882a593Smuzhiyun 			return rq2;
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /*
531*4882a593Smuzhiyun  * Async I/O can easily starve sync I/O (both sync reads and sync
532*4882a593Smuzhiyun  * writes), by consuming all tags. Similarly, storms of sync writes,
533*4882a593Smuzhiyun  * such as those that sync(2) may trigger, can starve sync reads.
534*4882a593Smuzhiyun  * Limit depths of async I/O and sync writes so as to counter both
535*4882a593Smuzhiyun  * problems.
536*4882a593Smuzhiyun  */
bfq_limit_depth(unsigned int op,struct blk_mq_alloc_data * data)537*4882a593Smuzhiyun static void bfq_limit_depth(unsigned int op, struct blk_mq_alloc_data *data)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct bfq_data *bfqd = data->q->elevator->elevator_data;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (op_is_sync(op) && !op_is_write(op))
542*4882a593Smuzhiyun 		return;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	data->shallow_depth =
545*4882a593Smuzhiyun 		bfqd->word_depths[!!bfqd->wr_busy_queues][op_is_sync(op)];
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	bfq_log(bfqd, "[%s] wr_busy %d sync %d depth %u",
548*4882a593Smuzhiyun 			__func__, bfqd->wr_busy_queues, op_is_sync(op),
549*4882a593Smuzhiyun 			data->shallow_depth);
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun static struct bfq_queue *
bfq_rq_pos_tree_lookup(struct bfq_data * bfqd,struct rb_root * root,sector_t sector,struct rb_node ** ret_parent,struct rb_node *** rb_link)553*4882a593Smuzhiyun bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root,
554*4882a593Smuzhiyun 		     sector_t sector, struct rb_node **ret_parent,
555*4882a593Smuzhiyun 		     struct rb_node ***rb_link)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun 	struct rb_node **p, *parent;
558*4882a593Smuzhiyun 	struct bfq_queue *bfqq = NULL;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	parent = NULL;
561*4882a593Smuzhiyun 	p = &root->rb_node;
562*4882a593Smuzhiyun 	while (*p) {
563*4882a593Smuzhiyun 		struct rb_node **n;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		parent = *p;
566*4882a593Smuzhiyun 		bfqq = rb_entry(parent, struct bfq_queue, pos_node);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		/*
569*4882a593Smuzhiyun 		 * Sort strictly based on sector. Smallest to the left,
570*4882a593Smuzhiyun 		 * largest to the right.
571*4882a593Smuzhiyun 		 */
572*4882a593Smuzhiyun 		if (sector > blk_rq_pos(bfqq->next_rq))
573*4882a593Smuzhiyun 			n = &(*p)->rb_right;
574*4882a593Smuzhiyun 		else if (sector < blk_rq_pos(bfqq->next_rq))
575*4882a593Smuzhiyun 			n = &(*p)->rb_left;
576*4882a593Smuzhiyun 		else
577*4882a593Smuzhiyun 			break;
578*4882a593Smuzhiyun 		p = n;
579*4882a593Smuzhiyun 		bfqq = NULL;
580*4882a593Smuzhiyun 	}
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	*ret_parent = parent;
583*4882a593Smuzhiyun 	if (rb_link)
584*4882a593Smuzhiyun 		*rb_link = p;
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d",
587*4882a593Smuzhiyun 		(unsigned long long)sector,
588*4882a593Smuzhiyun 		bfqq ? bfqq->pid : 0);
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return bfqq;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
bfq_too_late_for_merging(struct bfq_queue * bfqq)593*4882a593Smuzhiyun static bool bfq_too_late_for_merging(struct bfq_queue *bfqq)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	return bfqq->service_from_backlogged > 0 &&
596*4882a593Smuzhiyun 		time_is_before_jiffies(bfqq->first_IO_time +
597*4882a593Smuzhiyun 				       bfq_merge_time_limit);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun /*
601*4882a593Smuzhiyun  * The following function is not marked as __cold because it is
602*4882a593Smuzhiyun  * actually cold, but for the same performance goal described in the
603*4882a593Smuzhiyun  * comments on the likely() at the beginning of
604*4882a593Smuzhiyun  * bfq_setup_cooperator(). Unexpectedly, to reach an even lower
605*4882a593Smuzhiyun  * execution time for the case where this function is not invoked, we
606*4882a593Smuzhiyun  * had to add an unlikely() in each involved if().
607*4882a593Smuzhiyun  */
608*4882a593Smuzhiyun void __cold
bfq_pos_tree_add_move(struct bfq_data * bfqd,struct bfq_queue * bfqq)609*4882a593Smuzhiyun bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct rb_node **p, *parent;
612*4882a593Smuzhiyun 	struct bfq_queue *__bfqq;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	if (bfqq->pos_root) {
615*4882a593Smuzhiyun 		rb_erase(&bfqq->pos_node, bfqq->pos_root);
616*4882a593Smuzhiyun 		bfqq->pos_root = NULL;
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	/* oom_bfqq does not participate in queue merging */
620*4882a593Smuzhiyun 	if (bfqq == &bfqd->oom_bfqq)
621*4882a593Smuzhiyun 		return;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	/*
624*4882a593Smuzhiyun 	 * bfqq cannot be merged any longer (see comments in
625*4882a593Smuzhiyun 	 * bfq_setup_cooperator): no point in adding bfqq into the
626*4882a593Smuzhiyun 	 * position tree.
627*4882a593Smuzhiyun 	 */
628*4882a593Smuzhiyun 	if (bfq_too_late_for_merging(bfqq))
629*4882a593Smuzhiyun 		return;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (bfq_class_idle(bfqq))
632*4882a593Smuzhiyun 		return;
633*4882a593Smuzhiyun 	if (!bfqq->next_rq)
634*4882a593Smuzhiyun 		return;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
637*4882a593Smuzhiyun 	__bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root,
638*4882a593Smuzhiyun 			blk_rq_pos(bfqq->next_rq), &parent, &p);
639*4882a593Smuzhiyun 	if (!__bfqq) {
640*4882a593Smuzhiyun 		rb_link_node(&bfqq->pos_node, parent, p);
641*4882a593Smuzhiyun 		rb_insert_color(&bfqq->pos_node, bfqq->pos_root);
642*4882a593Smuzhiyun 	} else
643*4882a593Smuzhiyun 		bfqq->pos_root = NULL;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun  * The following function returns false either if every active queue
648*4882a593Smuzhiyun  * must receive the same share of the throughput (symmetric scenario),
649*4882a593Smuzhiyun  * or, as a special case, if bfqq must receive a share of the
650*4882a593Smuzhiyun  * throughput lower than or equal to the share that every other active
651*4882a593Smuzhiyun  * queue must receive.  If bfqq does sync I/O, then these are the only
652*4882a593Smuzhiyun  * two cases where bfqq happens to be guaranteed its share of the
653*4882a593Smuzhiyun  * throughput even if I/O dispatching is not plugged when bfqq remains
654*4882a593Smuzhiyun  * temporarily empty (for more details, see the comments in the
655*4882a593Smuzhiyun  * function bfq_better_to_idle()). For this reason, the return value
656*4882a593Smuzhiyun  * of this function is used to check whether I/O-dispatch plugging can
657*4882a593Smuzhiyun  * be avoided.
658*4882a593Smuzhiyun  *
659*4882a593Smuzhiyun  * The above first case (symmetric scenario) occurs when:
660*4882a593Smuzhiyun  * 1) all active queues have the same weight,
661*4882a593Smuzhiyun  * 2) all active queues belong to the same I/O-priority class,
662*4882a593Smuzhiyun  * 3) all active groups at the same level in the groups tree have the same
663*4882a593Smuzhiyun  *    weight,
664*4882a593Smuzhiyun  * 4) all active groups at the same level in the groups tree have the same
665*4882a593Smuzhiyun  *    number of children.
666*4882a593Smuzhiyun  *
667*4882a593Smuzhiyun  * Unfortunately, keeping the necessary state for evaluating exactly
668*4882a593Smuzhiyun  * the last two symmetry sub-conditions above would be quite complex
669*4882a593Smuzhiyun  * and time consuming. Therefore this function evaluates, instead,
670*4882a593Smuzhiyun  * only the following stronger three sub-conditions, for which it is
671*4882a593Smuzhiyun  * much easier to maintain the needed state:
672*4882a593Smuzhiyun  * 1) all active queues have the same weight,
673*4882a593Smuzhiyun  * 2) all active queues belong to the same I/O-priority class,
674*4882a593Smuzhiyun  * 3) there are no active groups.
675*4882a593Smuzhiyun  * In particular, the last condition is always true if hierarchical
676*4882a593Smuzhiyun  * support or the cgroups interface are not enabled, thus no state
677*4882a593Smuzhiyun  * needs to be maintained in this case.
678*4882a593Smuzhiyun  */
bfq_asymmetric_scenario(struct bfq_data * bfqd,struct bfq_queue * bfqq)679*4882a593Smuzhiyun static bool bfq_asymmetric_scenario(struct bfq_data *bfqd,
680*4882a593Smuzhiyun 				   struct bfq_queue *bfqq)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun 	bool smallest_weight = bfqq &&
683*4882a593Smuzhiyun 		bfqq->weight_counter &&
684*4882a593Smuzhiyun 		bfqq->weight_counter ==
685*4882a593Smuzhiyun 		container_of(
686*4882a593Smuzhiyun 			rb_first_cached(&bfqd->queue_weights_tree),
687*4882a593Smuzhiyun 			struct bfq_weight_counter,
688*4882a593Smuzhiyun 			weights_node);
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	/*
691*4882a593Smuzhiyun 	 * For queue weights to differ, queue_weights_tree must contain
692*4882a593Smuzhiyun 	 * at least two nodes.
693*4882a593Smuzhiyun 	 */
694*4882a593Smuzhiyun 	bool varied_queue_weights = !smallest_weight &&
695*4882a593Smuzhiyun 		!RB_EMPTY_ROOT(&bfqd->queue_weights_tree.rb_root) &&
696*4882a593Smuzhiyun 		(bfqd->queue_weights_tree.rb_root.rb_node->rb_left ||
697*4882a593Smuzhiyun 		 bfqd->queue_weights_tree.rb_root.rb_node->rb_right);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	bool multiple_classes_busy =
700*4882a593Smuzhiyun 		(bfqd->busy_queues[0] && bfqd->busy_queues[1]) ||
701*4882a593Smuzhiyun 		(bfqd->busy_queues[0] && bfqd->busy_queues[2]) ||
702*4882a593Smuzhiyun 		(bfqd->busy_queues[1] && bfqd->busy_queues[2]);
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	return varied_queue_weights || multiple_classes_busy
705*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
706*4882a593Smuzhiyun 	       || bfqd->num_groups_with_pending_reqs > 0
707*4882a593Smuzhiyun #endif
708*4882a593Smuzhiyun 		;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun /*
712*4882a593Smuzhiyun  * If the weight-counter tree passed as input contains no counter for
713*4882a593Smuzhiyun  * the weight of the input queue, then add that counter; otherwise just
714*4882a593Smuzhiyun  * increment the existing counter.
715*4882a593Smuzhiyun  *
716*4882a593Smuzhiyun  * Note that weight-counter trees contain few nodes in mostly symmetric
717*4882a593Smuzhiyun  * scenarios. For example, if all queues have the same weight, then the
718*4882a593Smuzhiyun  * weight-counter tree for the queues may contain at most one node.
719*4882a593Smuzhiyun  * This holds even if low_latency is on, because weight-raised queues
720*4882a593Smuzhiyun  * are not inserted in the tree.
721*4882a593Smuzhiyun  * In most scenarios, the rate at which nodes are created/destroyed
722*4882a593Smuzhiyun  * should be low too.
723*4882a593Smuzhiyun  */
bfq_weights_tree_add(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct rb_root_cached * root)724*4882a593Smuzhiyun void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
725*4882a593Smuzhiyun 			  struct rb_root_cached *root)
726*4882a593Smuzhiyun {
727*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
728*4882a593Smuzhiyun 	struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
729*4882a593Smuzhiyun 	bool leftmost = true;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	/*
732*4882a593Smuzhiyun 	 * Do not insert if the queue is already associated with a
733*4882a593Smuzhiyun 	 * counter, which happens if:
734*4882a593Smuzhiyun 	 *   1) a request arrival has caused the queue to become both
735*4882a593Smuzhiyun 	 *      non-weight-raised, and hence change its weight, and
736*4882a593Smuzhiyun 	 *      backlogged; in this respect, each of the two events
737*4882a593Smuzhiyun 	 *      causes an invocation of this function,
738*4882a593Smuzhiyun 	 *   2) this is the invocation of this function caused by the
739*4882a593Smuzhiyun 	 *      second event. This second invocation is actually useless,
740*4882a593Smuzhiyun 	 *      and we handle this fact by exiting immediately. More
741*4882a593Smuzhiyun 	 *      efficient or clearer solutions might possibly be adopted.
742*4882a593Smuzhiyun 	 */
743*4882a593Smuzhiyun 	if (bfqq->weight_counter)
744*4882a593Smuzhiyun 		return;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	while (*new) {
747*4882a593Smuzhiyun 		struct bfq_weight_counter *__counter = container_of(*new,
748*4882a593Smuzhiyun 						struct bfq_weight_counter,
749*4882a593Smuzhiyun 						weights_node);
750*4882a593Smuzhiyun 		parent = *new;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 		if (entity->weight == __counter->weight) {
753*4882a593Smuzhiyun 			bfqq->weight_counter = __counter;
754*4882a593Smuzhiyun 			goto inc_counter;
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 		if (entity->weight < __counter->weight)
757*4882a593Smuzhiyun 			new = &((*new)->rb_left);
758*4882a593Smuzhiyun 		else {
759*4882a593Smuzhiyun 			new = &((*new)->rb_right);
760*4882a593Smuzhiyun 			leftmost = false;
761*4882a593Smuzhiyun 		}
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
765*4882a593Smuzhiyun 				       GFP_ATOMIC);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	/*
768*4882a593Smuzhiyun 	 * In the unlucky event of an allocation failure, we just
769*4882a593Smuzhiyun 	 * exit. This will cause the weight of queue to not be
770*4882a593Smuzhiyun 	 * considered in bfq_asymmetric_scenario, which, in its turn,
771*4882a593Smuzhiyun 	 * causes the scenario to be deemed wrongly symmetric in case
772*4882a593Smuzhiyun 	 * bfqq's weight would have been the only weight making the
773*4882a593Smuzhiyun 	 * scenario asymmetric.  On the bright side, no unbalance will
774*4882a593Smuzhiyun 	 * however occur when bfqq becomes inactive again (the
775*4882a593Smuzhiyun 	 * invocation of this function is triggered by an activation
776*4882a593Smuzhiyun 	 * of queue).  In fact, bfq_weights_tree_remove does nothing
777*4882a593Smuzhiyun 	 * if !bfqq->weight_counter.
778*4882a593Smuzhiyun 	 */
779*4882a593Smuzhiyun 	if (unlikely(!bfqq->weight_counter))
780*4882a593Smuzhiyun 		return;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	bfqq->weight_counter->weight = entity->weight;
783*4882a593Smuzhiyun 	rb_link_node(&bfqq->weight_counter->weights_node, parent, new);
784*4882a593Smuzhiyun 	rb_insert_color_cached(&bfqq->weight_counter->weights_node, root,
785*4882a593Smuzhiyun 				leftmost);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun inc_counter:
788*4882a593Smuzhiyun 	bfqq->weight_counter->num_active++;
789*4882a593Smuzhiyun 	bfqq->ref++;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /*
793*4882a593Smuzhiyun  * Decrement the weight counter associated with the queue, and, if the
794*4882a593Smuzhiyun  * counter reaches 0, remove the counter from the tree.
795*4882a593Smuzhiyun  * See the comments to the function bfq_weights_tree_add() for considerations
796*4882a593Smuzhiyun  * about overhead.
797*4882a593Smuzhiyun  */
__bfq_weights_tree_remove(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct rb_root_cached * root)798*4882a593Smuzhiyun void __bfq_weights_tree_remove(struct bfq_data *bfqd,
799*4882a593Smuzhiyun 			       struct bfq_queue *bfqq,
800*4882a593Smuzhiyun 			       struct rb_root_cached *root)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	if (!bfqq->weight_counter)
803*4882a593Smuzhiyun 		return;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	bfqq->weight_counter->num_active--;
806*4882a593Smuzhiyun 	if (bfqq->weight_counter->num_active > 0)
807*4882a593Smuzhiyun 		goto reset_entity_pointer;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	rb_erase_cached(&bfqq->weight_counter->weights_node, root);
810*4882a593Smuzhiyun 	kfree(bfqq->weight_counter);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun reset_entity_pointer:
813*4882a593Smuzhiyun 	bfqq->weight_counter = NULL;
814*4882a593Smuzhiyun 	bfq_put_queue(bfqq);
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun /*
818*4882a593Smuzhiyun  * Invoke __bfq_weights_tree_remove on bfqq and decrement the number
819*4882a593Smuzhiyun  * of active groups for each queue's inactive parent entity.
820*4882a593Smuzhiyun  */
bfq_weights_tree_remove(struct bfq_data * bfqd,struct bfq_queue * bfqq)821*4882a593Smuzhiyun void bfq_weights_tree_remove(struct bfq_data *bfqd,
822*4882a593Smuzhiyun 			     struct bfq_queue *bfqq)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	struct bfq_entity *entity = bfqq->entity.parent;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	for_each_entity(entity) {
827*4882a593Smuzhiyun 		struct bfq_sched_data *sd = entity->my_sched_data;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 		if (sd->next_in_service || sd->in_service_entity) {
830*4882a593Smuzhiyun 			/*
831*4882a593Smuzhiyun 			 * entity is still active, because either
832*4882a593Smuzhiyun 			 * next_in_service or in_service_entity is not
833*4882a593Smuzhiyun 			 * NULL (see the comments on the definition of
834*4882a593Smuzhiyun 			 * next_in_service for details on why
835*4882a593Smuzhiyun 			 * in_service_entity must be checked too).
836*4882a593Smuzhiyun 			 *
837*4882a593Smuzhiyun 			 * As a consequence, its parent entities are
838*4882a593Smuzhiyun 			 * active as well, and thus this loop must
839*4882a593Smuzhiyun 			 * stop here.
840*4882a593Smuzhiyun 			 */
841*4882a593Smuzhiyun 			break;
842*4882a593Smuzhiyun 		}
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 		/*
845*4882a593Smuzhiyun 		 * The decrement of num_groups_with_pending_reqs is
846*4882a593Smuzhiyun 		 * not performed immediately upon the deactivation of
847*4882a593Smuzhiyun 		 * entity, but it is delayed to when it also happens
848*4882a593Smuzhiyun 		 * that the first leaf descendant bfqq of entity gets
849*4882a593Smuzhiyun 		 * all its pending requests completed. The following
850*4882a593Smuzhiyun 		 * instructions perform this delayed decrement, if
851*4882a593Smuzhiyun 		 * needed. See the comments on
852*4882a593Smuzhiyun 		 * num_groups_with_pending_reqs for details.
853*4882a593Smuzhiyun 		 */
854*4882a593Smuzhiyun 		if (entity->in_groups_with_pending_reqs) {
855*4882a593Smuzhiyun 			entity->in_groups_with_pending_reqs = false;
856*4882a593Smuzhiyun 			bfqd->num_groups_with_pending_reqs--;
857*4882a593Smuzhiyun 		}
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	/*
861*4882a593Smuzhiyun 	 * Next function is invoked last, because it causes bfqq to be
862*4882a593Smuzhiyun 	 * freed if the following holds: bfqq is not in service and
863*4882a593Smuzhiyun 	 * has no dispatched request. DO NOT use bfqq after the next
864*4882a593Smuzhiyun 	 * function invocation.
865*4882a593Smuzhiyun 	 */
866*4882a593Smuzhiyun 	__bfq_weights_tree_remove(bfqd, bfqq,
867*4882a593Smuzhiyun 				  &bfqd->queue_weights_tree);
868*4882a593Smuzhiyun }
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun /*
871*4882a593Smuzhiyun  * Return expired entry, or NULL to just start from scratch in rbtree.
872*4882a593Smuzhiyun  */
bfq_check_fifo(struct bfq_queue * bfqq,struct request * last)873*4882a593Smuzhiyun static struct request *bfq_check_fifo(struct bfq_queue *bfqq,
874*4882a593Smuzhiyun 				      struct request *last)
875*4882a593Smuzhiyun {
876*4882a593Smuzhiyun 	struct request *rq;
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	if (bfq_bfqq_fifo_expire(bfqq))
879*4882a593Smuzhiyun 		return NULL;
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun 	bfq_mark_bfqq_fifo_expire(bfqq);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	rq = rq_entry_fifo(bfqq->fifo.next);
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	if (rq == last || ktime_get_ns() < rq->fifo_time)
886*4882a593Smuzhiyun 		return NULL;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq);
889*4882a593Smuzhiyun 	return rq;
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
bfq_find_next_rq(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct request * last)892*4882a593Smuzhiyun static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
893*4882a593Smuzhiyun 					struct bfq_queue *bfqq,
894*4882a593Smuzhiyun 					struct request *last)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	struct rb_node *rbnext = rb_next(&last->rb_node);
897*4882a593Smuzhiyun 	struct rb_node *rbprev = rb_prev(&last->rb_node);
898*4882a593Smuzhiyun 	struct request *next, *prev = NULL;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	/* Follow expired path, else get first next available. */
901*4882a593Smuzhiyun 	next = bfq_check_fifo(bfqq, last);
902*4882a593Smuzhiyun 	if (next)
903*4882a593Smuzhiyun 		return next;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	if (rbprev)
906*4882a593Smuzhiyun 		prev = rb_entry_rq(rbprev);
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	if (rbnext)
909*4882a593Smuzhiyun 		next = rb_entry_rq(rbnext);
910*4882a593Smuzhiyun 	else {
911*4882a593Smuzhiyun 		rbnext = rb_first(&bfqq->sort_list);
912*4882a593Smuzhiyun 		if (rbnext && rbnext != &last->rb_node)
913*4882a593Smuzhiyun 			next = rb_entry_rq(rbnext);
914*4882a593Smuzhiyun 	}
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun /* see the definition of bfq_async_charge_factor for details */
bfq_serv_to_charge(struct request * rq,struct bfq_queue * bfqq)920*4882a593Smuzhiyun static unsigned long bfq_serv_to_charge(struct request *rq,
921*4882a593Smuzhiyun 					struct bfq_queue *bfqq)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1 ||
924*4882a593Smuzhiyun 	    bfq_asymmetric_scenario(bfqq->bfqd, bfqq))
925*4882a593Smuzhiyun 		return blk_rq_sectors(rq);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	return blk_rq_sectors(rq) * bfq_async_charge_factor;
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun /**
931*4882a593Smuzhiyun  * bfq_updated_next_req - update the queue after a new next_rq selection.
932*4882a593Smuzhiyun  * @bfqd: the device data the queue belongs to.
933*4882a593Smuzhiyun  * @bfqq: the queue to update.
934*4882a593Smuzhiyun  *
935*4882a593Smuzhiyun  * If the first request of a queue changes we make sure that the queue
936*4882a593Smuzhiyun  * has enough budget to serve at least its first request (if the
937*4882a593Smuzhiyun  * request has grown).  We do this because if the queue has not enough
938*4882a593Smuzhiyun  * budget for its first request, it has to go through two dispatch
939*4882a593Smuzhiyun  * rounds to actually get it dispatched.
940*4882a593Smuzhiyun  */
bfq_updated_next_req(struct bfq_data * bfqd,struct bfq_queue * bfqq)941*4882a593Smuzhiyun static void bfq_updated_next_req(struct bfq_data *bfqd,
942*4882a593Smuzhiyun 				 struct bfq_queue *bfqq)
943*4882a593Smuzhiyun {
944*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
945*4882a593Smuzhiyun 	struct request *next_rq = bfqq->next_rq;
946*4882a593Smuzhiyun 	unsigned long new_budget;
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	if (!next_rq)
949*4882a593Smuzhiyun 		return;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	if (bfqq == bfqd->in_service_queue)
952*4882a593Smuzhiyun 		/*
953*4882a593Smuzhiyun 		 * In order not to break guarantees, budgets cannot be
954*4882a593Smuzhiyun 		 * changed after an entity has been selected.
955*4882a593Smuzhiyun 		 */
956*4882a593Smuzhiyun 		return;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	new_budget = max_t(unsigned long,
959*4882a593Smuzhiyun 			   max_t(unsigned long, bfqq->max_budget,
960*4882a593Smuzhiyun 				 bfq_serv_to_charge(next_rq, bfqq)),
961*4882a593Smuzhiyun 			   entity->service);
962*4882a593Smuzhiyun 	if (entity->budget != new_budget) {
963*4882a593Smuzhiyun 		entity->budget = new_budget;
964*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
965*4882a593Smuzhiyun 					 new_budget);
966*4882a593Smuzhiyun 		bfq_requeue_bfqq(bfqd, bfqq, false);
967*4882a593Smuzhiyun 	}
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
bfq_wr_duration(struct bfq_data * bfqd)970*4882a593Smuzhiyun static unsigned int bfq_wr_duration(struct bfq_data *bfqd)
971*4882a593Smuzhiyun {
972*4882a593Smuzhiyun 	u64 dur;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	if (bfqd->bfq_wr_max_time > 0)
975*4882a593Smuzhiyun 		return bfqd->bfq_wr_max_time;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	dur = bfqd->rate_dur_prod;
978*4882a593Smuzhiyun 	do_div(dur, bfqd->peak_rate);
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/*
981*4882a593Smuzhiyun 	 * Limit duration between 3 and 25 seconds. The upper limit
982*4882a593Smuzhiyun 	 * has been conservatively set after the following worst case:
983*4882a593Smuzhiyun 	 * on a QEMU/KVM virtual machine
984*4882a593Smuzhiyun 	 * - running in a slow PC
985*4882a593Smuzhiyun 	 * - with a virtual disk stacked on a slow low-end 5400rpm HDD
986*4882a593Smuzhiyun 	 * - serving a heavy I/O workload, such as the sequential reading
987*4882a593Smuzhiyun 	 *   of several files
988*4882a593Smuzhiyun 	 * mplayer took 23 seconds to start, if constantly weight-raised.
989*4882a593Smuzhiyun 	 *
990*4882a593Smuzhiyun 	 * As for higher values than that accommodating the above bad
991*4882a593Smuzhiyun 	 * scenario, tests show that higher values would often yield
992*4882a593Smuzhiyun 	 * the opposite of the desired result, i.e., would worsen
993*4882a593Smuzhiyun 	 * responsiveness by allowing non-interactive applications to
994*4882a593Smuzhiyun 	 * preserve weight raising for too long.
995*4882a593Smuzhiyun 	 *
996*4882a593Smuzhiyun 	 * On the other end, lower values than 3 seconds make it
997*4882a593Smuzhiyun 	 * difficult for most interactive tasks to complete their jobs
998*4882a593Smuzhiyun 	 * before weight-raising finishes.
999*4882a593Smuzhiyun 	 */
1000*4882a593Smuzhiyun 	return clamp_val(dur, msecs_to_jiffies(3000), msecs_to_jiffies(25000));
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun /* switch back from soft real-time to interactive weight raising */
switch_back_to_interactive_wr(struct bfq_queue * bfqq,struct bfq_data * bfqd)1004*4882a593Smuzhiyun static void switch_back_to_interactive_wr(struct bfq_queue *bfqq,
1005*4882a593Smuzhiyun 					  struct bfq_data *bfqd)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun 	bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1008*4882a593Smuzhiyun 	bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1009*4882a593Smuzhiyun 	bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun static void
bfq_bfqq_resume_state(struct bfq_queue * bfqq,struct bfq_data * bfqd,struct bfq_io_cq * bic,bool bfq_already_existing)1013*4882a593Smuzhiyun bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd,
1014*4882a593Smuzhiyun 		      struct bfq_io_cq *bic, bool bfq_already_existing)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	unsigned int old_wr_coeff = bfqq->wr_coeff;
1017*4882a593Smuzhiyun 	bool busy = bfq_already_existing && bfq_bfqq_busy(bfqq);
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	if (bic->saved_has_short_ttime)
1020*4882a593Smuzhiyun 		bfq_mark_bfqq_has_short_ttime(bfqq);
1021*4882a593Smuzhiyun 	else
1022*4882a593Smuzhiyun 		bfq_clear_bfqq_has_short_ttime(bfqq);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	if (bic->saved_IO_bound)
1025*4882a593Smuzhiyun 		bfq_mark_bfqq_IO_bound(bfqq);
1026*4882a593Smuzhiyun 	else
1027*4882a593Smuzhiyun 		bfq_clear_bfqq_IO_bound(bfqq);
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun 	bfqq->entity.new_weight = bic->saved_weight;
1030*4882a593Smuzhiyun 	bfqq->ttime = bic->saved_ttime;
1031*4882a593Smuzhiyun 	bfqq->wr_coeff = bic->saved_wr_coeff;
1032*4882a593Smuzhiyun 	bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt;
1033*4882a593Smuzhiyun 	bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish;
1034*4882a593Smuzhiyun 	bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time;
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) ||
1037*4882a593Smuzhiyun 	    time_is_before_jiffies(bfqq->last_wr_start_finish +
1038*4882a593Smuzhiyun 				   bfqq->wr_cur_max_time))) {
1039*4882a593Smuzhiyun 		if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
1040*4882a593Smuzhiyun 		    !bfq_bfqq_in_large_burst(bfqq) &&
1041*4882a593Smuzhiyun 		    time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt +
1042*4882a593Smuzhiyun 					     bfq_wr_duration(bfqd))) {
1043*4882a593Smuzhiyun 			switch_back_to_interactive_wr(bfqq, bfqd);
1044*4882a593Smuzhiyun 		} else {
1045*4882a593Smuzhiyun 			bfqq->wr_coeff = 1;
1046*4882a593Smuzhiyun 			bfq_log_bfqq(bfqq->bfqd, bfqq,
1047*4882a593Smuzhiyun 				     "resume state: switching off wr");
1048*4882a593Smuzhiyun 		}
1049*4882a593Smuzhiyun 	}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	/* make sure weight will be updated, however we got here */
1052*4882a593Smuzhiyun 	bfqq->entity.prio_changed = 1;
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	if (likely(!busy))
1055*4882a593Smuzhiyun 		return;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	if (old_wr_coeff == 1 && bfqq->wr_coeff > 1)
1058*4882a593Smuzhiyun 		bfqd->wr_busy_queues++;
1059*4882a593Smuzhiyun 	else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1)
1060*4882a593Smuzhiyun 		bfqd->wr_busy_queues--;
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
bfqq_process_refs(struct bfq_queue * bfqq)1063*4882a593Smuzhiyun static int bfqq_process_refs(struct bfq_queue *bfqq)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun 	return bfqq->ref - bfqq->allocated - bfqq->entity.on_st_or_in_serv -
1066*4882a593Smuzhiyun 		(bfqq->weight_counter != NULL);
1067*4882a593Smuzhiyun }
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun /* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */
bfq_reset_burst_list(struct bfq_data * bfqd,struct bfq_queue * bfqq)1070*4882a593Smuzhiyun static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun 	struct bfq_queue *item;
1073*4882a593Smuzhiyun 	struct hlist_node *n;
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun 	hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node)
1076*4882a593Smuzhiyun 		hlist_del_init(&item->burst_list_node);
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 	/*
1079*4882a593Smuzhiyun 	 * Start the creation of a new burst list only if there is no
1080*4882a593Smuzhiyun 	 * active queue. See comments on the conditional invocation of
1081*4882a593Smuzhiyun 	 * bfq_handle_burst().
1082*4882a593Smuzhiyun 	 */
1083*4882a593Smuzhiyun 	if (bfq_tot_busy_queues(bfqd) == 0) {
1084*4882a593Smuzhiyun 		hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1085*4882a593Smuzhiyun 		bfqd->burst_size = 1;
1086*4882a593Smuzhiyun 	} else
1087*4882a593Smuzhiyun 		bfqd->burst_size = 0;
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	bfqd->burst_parent_entity = bfqq->entity.parent;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun /* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
bfq_add_to_burst(struct bfq_data * bfqd,struct bfq_queue * bfqq)1093*4882a593Smuzhiyun static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	/* Increment burst size to take into account also bfqq */
1096*4882a593Smuzhiyun 	bfqd->burst_size++;
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
1099*4882a593Smuzhiyun 		struct bfq_queue *pos, *bfqq_item;
1100*4882a593Smuzhiyun 		struct hlist_node *n;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 		/*
1103*4882a593Smuzhiyun 		 * Enough queues have been activated shortly after each
1104*4882a593Smuzhiyun 		 * other to consider this burst as large.
1105*4882a593Smuzhiyun 		 */
1106*4882a593Smuzhiyun 		bfqd->large_burst = true;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 		/*
1109*4882a593Smuzhiyun 		 * We can now mark all queues in the burst list as
1110*4882a593Smuzhiyun 		 * belonging to a large burst.
1111*4882a593Smuzhiyun 		 */
1112*4882a593Smuzhiyun 		hlist_for_each_entry(bfqq_item, &bfqd->burst_list,
1113*4882a593Smuzhiyun 				     burst_list_node)
1114*4882a593Smuzhiyun 			bfq_mark_bfqq_in_large_burst(bfqq_item);
1115*4882a593Smuzhiyun 		bfq_mark_bfqq_in_large_burst(bfqq);
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 		/*
1118*4882a593Smuzhiyun 		 * From now on, and until the current burst finishes, any
1119*4882a593Smuzhiyun 		 * new queue being activated shortly after the last queue
1120*4882a593Smuzhiyun 		 * was inserted in the burst can be immediately marked as
1121*4882a593Smuzhiyun 		 * belonging to a large burst. So the burst list is not
1122*4882a593Smuzhiyun 		 * needed any more. Remove it.
1123*4882a593Smuzhiyun 		 */
1124*4882a593Smuzhiyun 		hlist_for_each_entry_safe(pos, n, &bfqd->burst_list,
1125*4882a593Smuzhiyun 					  burst_list_node)
1126*4882a593Smuzhiyun 			hlist_del_init(&pos->burst_list_node);
1127*4882a593Smuzhiyun 	} else /*
1128*4882a593Smuzhiyun 		* Burst not yet large: add bfqq to the burst list. Do
1129*4882a593Smuzhiyun 		* not increment the ref counter for bfqq, because bfqq
1130*4882a593Smuzhiyun 		* is removed from the burst list before freeing bfqq
1131*4882a593Smuzhiyun 		* in put_queue.
1132*4882a593Smuzhiyun 		*/
1133*4882a593Smuzhiyun 		hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun /*
1137*4882a593Smuzhiyun  * If many queues belonging to the same group happen to be created
1138*4882a593Smuzhiyun  * shortly after each other, then the processes associated with these
1139*4882a593Smuzhiyun  * queues have typically a common goal. In particular, bursts of queue
1140*4882a593Smuzhiyun  * creations are usually caused by services or applications that spawn
1141*4882a593Smuzhiyun  * many parallel threads/processes. Examples are systemd during boot,
1142*4882a593Smuzhiyun  * or git grep. To help these processes get their job done as soon as
1143*4882a593Smuzhiyun  * possible, it is usually better to not grant either weight-raising
1144*4882a593Smuzhiyun  * or device idling to their queues, unless these queues must be
1145*4882a593Smuzhiyun  * protected from the I/O flowing through other active queues.
1146*4882a593Smuzhiyun  *
1147*4882a593Smuzhiyun  * In this comment we describe, firstly, the reasons why this fact
1148*4882a593Smuzhiyun  * holds, and, secondly, the next function, which implements the main
1149*4882a593Smuzhiyun  * steps needed to properly mark these queues so that they can then be
1150*4882a593Smuzhiyun  * treated in a different way.
1151*4882a593Smuzhiyun  *
1152*4882a593Smuzhiyun  * The above services or applications benefit mostly from a high
1153*4882a593Smuzhiyun  * throughput: the quicker the requests of the activated queues are
1154*4882a593Smuzhiyun  * cumulatively served, the sooner the target job of these queues gets
1155*4882a593Smuzhiyun  * completed. As a consequence, weight-raising any of these queues,
1156*4882a593Smuzhiyun  * which also implies idling the device for it, is almost always
1157*4882a593Smuzhiyun  * counterproductive, unless there are other active queues to isolate
1158*4882a593Smuzhiyun  * these new queues from. If there no other active queues, then
1159*4882a593Smuzhiyun  * weight-raising these new queues just lowers throughput in most
1160*4882a593Smuzhiyun  * cases.
1161*4882a593Smuzhiyun  *
1162*4882a593Smuzhiyun  * On the other hand, a burst of queue creations may be caused also by
1163*4882a593Smuzhiyun  * the start of an application that does not consist of a lot of
1164*4882a593Smuzhiyun  * parallel I/O-bound threads. In fact, with a complex application,
1165*4882a593Smuzhiyun  * several short processes may need to be executed to start-up the
1166*4882a593Smuzhiyun  * application. In this respect, to start an application as quickly as
1167*4882a593Smuzhiyun  * possible, the best thing to do is in any case to privilege the I/O
1168*4882a593Smuzhiyun  * related to the application with respect to all other
1169*4882a593Smuzhiyun  * I/O. Therefore, the best strategy to start as quickly as possible
1170*4882a593Smuzhiyun  * an application that causes a burst of queue creations is to
1171*4882a593Smuzhiyun  * weight-raise all the queues created during the burst. This is the
1172*4882a593Smuzhiyun  * exact opposite of the best strategy for the other type of bursts.
1173*4882a593Smuzhiyun  *
1174*4882a593Smuzhiyun  * In the end, to take the best action for each of the two cases, the
1175*4882a593Smuzhiyun  * two types of bursts need to be distinguished. Fortunately, this
1176*4882a593Smuzhiyun  * seems relatively easy, by looking at the sizes of the bursts. In
1177*4882a593Smuzhiyun  * particular, we found a threshold such that only bursts with a
1178*4882a593Smuzhiyun  * larger size than that threshold are apparently caused by
1179*4882a593Smuzhiyun  * services or commands such as systemd or git grep. For brevity,
1180*4882a593Smuzhiyun  * hereafter we call just 'large' these bursts. BFQ *does not*
1181*4882a593Smuzhiyun  * weight-raise queues whose creation occurs in a large burst. In
1182*4882a593Smuzhiyun  * addition, for each of these queues BFQ performs or does not perform
1183*4882a593Smuzhiyun  * idling depending on which choice boosts the throughput more. The
1184*4882a593Smuzhiyun  * exact choice depends on the device and request pattern at
1185*4882a593Smuzhiyun  * hand.
1186*4882a593Smuzhiyun  *
1187*4882a593Smuzhiyun  * Unfortunately, false positives may occur while an interactive task
1188*4882a593Smuzhiyun  * is starting (e.g., an application is being started). The
1189*4882a593Smuzhiyun  * consequence is that the queues associated with the task do not
1190*4882a593Smuzhiyun  * enjoy weight raising as expected. Fortunately these false positives
1191*4882a593Smuzhiyun  * are very rare. They typically occur if some service happens to
1192*4882a593Smuzhiyun  * start doing I/O exactly when the interactive task starts.
1193*4882a593Smuzhiyun  *
1194*4882a593Smuzhiyun  * Turning back to the next function, it is invoked only if there are
1195*4882a593Smuzhiyun  * no active queues (apart from active queues that would belong to the
1196*4882a593Smuzhiyun  * same, possible burst bfqq would belong to), and it implements all
1197*4882a593Smuzhiyun  * the steps needed to detect the occurrence of a large burst and to
1198*4882a593Smuzhiyun  * properly mark all the queues belonging to it (so that they can then
1199*4882a593Smuzhiyun  * be treated in a different way). This goal is achieved by
1200*4882a593Smuzhiyun  * maintaining a "burst list" that holds, temporarily, the queues that
1201*4882a593Smuzhiyun  * belong to the burst in progress. The list is then used to mark
1202*4882a593Smuzhiyun  * these queues as belonging to a large burst if the burst does become
1203*4882a593Smuzhiyun  * large. The main steps are the following.
1204*4882a593Smuzhiyun  *
1205*4882a593Smuzhiyun  * . when the very first queue is created, the queue is inserted into the
1206*4882a593Smuzhiyun  *   list (as it could be the first queue in a possible burst)
1207*4882a593Smuzhiyun  *
1208*4882a593Smuzhiyun  * . if the current burst has not yet become large, and a queue Q that does
1209*4882a593Smuzhiyun  *   not yet belong to the burst is activated shortly after the last time
1210*4882a593Smuzhiyun  *   at which a new queue entered the burst list, then the function appends
1211*4882a593Smuzhiyun  *   Q to the burst list
1212*4882a593Smuzhiyun  *
1213*4882a593Smuzhiyun  * . if, as a consequence of the previous step, the burst size reaches
1214*4882a593Smuzhiyun  *   the large-burst threshold, then
1215*4882a593Smuzhiyun  *
1216*4882a593Smuzhiyun  *     . all the queues in the burst list are marked as belonging to a
1217*4882a593Smuzhiyun  *       large burst
1218*4882a593Smuzhiyun  *
1219*4882a593Smuzhiyun  *     . the burst list is deleted; in fact, the burst list already served
1220*4882a593Smuzhiyun  *       its purpose (keeping temporarily track of the queues in a burst,
1221*4882a593Smuzhiyun  *       so as to be able to mark them as belonging to a large burst in the
1222*4882a593Smuzhiyun  *       previous sub-step), and now is not needed any more
1223*4882a593Smuzhiyun  *
1224*4882a593Smuzhiyun  *     . the device enters a large-burst mode
1225*4882a593Smuzhiyun  *
1226*4882a593Smuzhiyun  * . if a queue Q that does not belong to the burst is created while
1227*4882a593Smuzhiyun  *   the device is in large-burst mode and shortly after the last time
1228*4882a593Smuzhiyun  *   at which a queue either entered the burst list or was marked as
1229*4882a593Smuzhiyun  *   belonging to the current large burst, then Q is immediately marked
1230*4882a593Smuzhiyun  *   as belonging to a large burst.
1231*4882a593Smuzhiyun  *
1232*4882a593Smuzhiyun  * . if a queue Q that does not belong to the burst is created a while
1233*4882a593Smuzhiyun  *   later, i.e., not shortly after, than the last time at which a queue
1234*4882a593Smuzhiyun  *   either entered the burst list or was marked as belonging to the
1235*4882a593Smuzhiyun  *   current large burst, then the current burst is deemed as finished and:
1236*4882a593Smuzhiyun  *
1237*4882a593Smuzhiyun  *        . the large-burst mode is reset if set
1238*4882a593Smuzhiyun  *
1239*4882a593Smuzhiyun  *        . the burst list is emptied
1240*4882a593Smuzhiyun  *
1241*4882a593Smuzhiyun  *        . Q is inserted in the burst list, as Q may be the first queue
1242*4882a593Smuzhiyun  *          in a possible new burst (then the burst list contains just Q
1243*4882a593Smuzhiyun  *          after this step).
1244*4882a593Smuzhiyun  */
bfq_handle_burst(struct bfq_data * bfqd,struct bfq_queue * bfqq)1245*4882a593Smuzhiyun static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	/*
1248*4882a593Smuzhiyun 	 * If bfqq is already in the burst list or is part of a large
1249*4882a593Smuzhiyun 	 * burst, or finally has just been split, then there is
1250*4882a593Smuzhiyun 	 * nothing else to do.
1251*4882a593Smuzhiyun 	 */
1252*4882a593Smuzhiyun 	if (!hlist_unhashed(&bfqq->burst_list_node) ||
1253*4882a593Smuzhiyun 	    bfq_bfqq_in_large_burst(bfqq) ||
1254*4882a593Smuzhiyun 	    time_is_after_eq_jiffies(bfqq->split_time +
1255*4882a593Smuzhiyun 				     msecs_to_jiffies(10)))
1256*4882a593Smuzhiyun 		return;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	/*
1259*4882a593Smuzhiyun 	 * If bfqq's creation happens late enough, or bfqq belongs to
1260*4882a593Smuzhiyun 	 * a different group than the burst group, then the current
1261*4882a593Smuzhiyun 	 * burst is finished, and related data structures must be
1262*4882a593Smuzhiyun 	 * reset.
1263*4882a593Smuzhiyun 	 *
1264*4882a593Smuzhiyun 	 * In this respect, consider the special case where bfqq is
1265*4882a593Smuzhiyun 	 * the very first queue created after BFQ is selected for this
1266*4882a593Smuzhiyun 	 * device. In this case, last_ins_in_burst and
1267*4882a593Smuzhiyun 	 * burst_parent_entity are not yet significant when we get
1268*4882a593Smuzhiyun 	 * here. But it is easy to verify that, whether or not the
1269*4882a593Smuzhiyun 	 * following condition is true, bfqq will end up being
1270*4882a593Smuzhiyun 	 * inserted into the burst list. In particular the list will
1271*4882a593Smuzhiyun 	 * happen to contain only bfqq. And this is exactly what has
1272*4882a593Smuzhiyun 	 * to happen, as bfqq may be the first queue of the first
1273*4882a593Smuzhiyun 	 * burst.
1274*4882a593Smuzhiyun 	 */
1275*4882a593Smuzhiyun 	if (time_is_before_jiffies(bfqd->last_ins_in_burst +
1276*4882a593Smuzhiyun 	    bfqd->bfq_burst_interval) ||
1277*4882a593Smuzhiyun 	    bfqq->entity.parent != bfqd->burst_parent_entity) {
1278*4882a593Smuzhiyun 		bfqd->large_burst = false;
1279*4882a593Smuzhiyun 		bfq_reset_burst_list(bfqd, bfqq);
1280*4882a593Smuzhiyun 		goto end;
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	/*
1284*4882a593Smuzhiyun 	 * If we get here, then bfqq is being activated shortly after the
1285*4882a593Smuzhiyun 	 * last queue. So, if the current burst is also large, we can mark
1286*4882a593Smuzhiyun 	 * bfqq as belonging to this large burst immediately.
1287*4882a593Smuzhiyun 	 */
1288*4882a593Smuzhiyun 	if (bfqd->large_burst) {
1289*4882a593Smuzhiyun 		bfq_mark_bfqq_in_large_burst(bfqq);
1290*4882a593Smuzhiyun 		goto end;
1291*4882a593Smuzhiyun 	}
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	/*
1294*4882a593Smuzhiyun 	 * If we get here, then a large-burst state has not yet been
1295*4882a593Smuzhiyun 	 * reached, but bfqq is being activated shortly after the last
1296*4882a593Smuzhiyun 	 * queue. Then we add bfqq to the burst.
1297*4882a593Smuzhiyun 	 */
1298*4882a593Smuzhiyun 	bfq_add_to_burst(bfqd, bfqq);
1299*4882a593Smuzhiyun end:
1300*4882a593Smuzhiyun 	/*
1301*4882a593Smuzhiyun 	 * At this point, bfqq either has been added to the current
1302*4882a593Smuzhiyun 	 * burst or has caused the current burst to terminate and a
1303*4882a593Smuzhiyun 	 * possible new burst to start. In particular, in the second
1304*4882a593Smuzhiyun 	 * case, bfqq has become the first queue in the possible new
1305*4882a593Smuzhiyun 	 * burst.  In both cases last_ins_in_burst needs to be moved
1306*4882a593Smuzhiyun 	 * forward.
1307*4882a593Smuzhiyun 	 */
1308*4882a593Smuzhiyun 	bfqd->last_ins_in_burst = jiffies;
1309*4882a593Smuzhiyun }
1310*4882a593Smuzhiyun 
bfq_bfqq_budget_left(struct bfq_queue * bfqq)1311*4882a593Smuzhiyun static int bfq_bfqq_budget_left(struct bfq_queue *bfqq)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	return entity->budget - entity->service;
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun /*
1319*4882a593Smuzhiyun  * If enough samples have been computed, return the current max budget
1320*4882a593Smuzhiyun  * stored in bfqd, which is dynamically updated according to the
1321*4882a593Smuzhiyun  * estimated disk peak rate; otherwise return the default max budget
1322*4882a593Smuzhiyun  */
bfq_max_budget(struct bfq_data * bfqd)1323*4882a593Smuzhiyun static int bfq_max_budget(struct bfq_data *bfqd)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun 	if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1326*4882a593Smuzhiyun 		return bfq_default_max_budget;
1327*4882a593Smuzhiyun 	else
1328*4882a593Smuzhiyun 		return bfqd->bfq_max_budget;
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun /*
1332*4882a593Smuzhiyun  * Return min budget, which is a fraction of the current or default
1333*4882a593Smuzhiyun  * max budget (trying with 1/32)
1334*4882a593Smuzhiyun  */
bfq_min_budget(struct bfq_data * bfqd)1335*4882a593Smuzhiyun static int bfq_min_budget(struct bfq_data *bfqd)
1336*4882a593Smuzhiyun {
1337*4882a593Smuzhiyun 	if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1338*4882a593Smuzhiyun 		return bfq_default_max_budget / 32;
1339*4882a593Smuzhiyun 	else
1340*4882a593Smuzhiyun 		return bfqd->bfq_max_budget / 32;
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun /*
1344*4882a593Smuzhiyun  * The next function, invoked after the input queue bfqq switches from
1345*4882a593Smuzhiyun  * idle to busy, updates the budget of bfqq. The function also tells
1346*4882a593Smuzhiyun  * whether the in-service queue should be expired, by returning
1347*4882a593Smuzhiyun  * true. The purpose of expiring the in-service queue is to give bfqq
1348*4882a593Smuzhiyun  * the chance to possibly preempt the in-service queue, and the reason
1349*4882a593Smuzhiyun  * for preempting the in-service queue is to achieve one of the two
1350*4882a593Smuzhiyun  * goals below.
1351*4882a593Smuzhiyun  *
1352*4882a593Smuzhiyun  * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has
1353*4882a593Smuzhiyun  * expired because it has remained idle. In particular, bfqq may have
1354*4882a593Smuzhiyun  * expired for one of the following two reasons:
1355*4882a593Smuzhiyun  *
1356*4882a593Smuzhiyun  * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
1357*4882a593Smuzhiyun  *   and did not make it to issue a new request before its last
1358*4882a593Smuzhiyun  *   request was served;
1359*4882a593Smuzhiyun  *
1360*4882a593Smuzhiyun  * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
1361*4882a593Smuzhiyun  *   a new request before the expiration of the idling-time.
1362*4882a593Smuzhiyun  *
1363*4882a593Smuzhiyun  * Even if bfqq has expired for one of the above reasons, the process
1364*4882a593Smuzhiyun  * associated with the queue may be however issuing requests greedily,
1365*4882a593Smuzhiyun  * and thus be sensitive to the bandwidth it receives (bfqq may have
1366*4882a593Smuzhiyun  * remained idle for other reasons: CPU high load, bfqq not enjoying
1367*4882a593Smuzhiyun  * idling, I/O throttling somewhere in the path from the process to
1368*4882a593Smuzhiyun  * the I/O scheduler, ...). But if, after every expiration for one of
1369*4882a593Smuzhiyun  * the above two reasons, bfqq has to wait for the service of at least
1370*4882a593Smuzhiyun  * one full budget of another queue before being served again, then
1371*4882a593Smuzhiyun  * bfqq is likely to get a much lower bandwidth or resource time than
1372*4882a593Smuzhiyun  * its reserved ones. To address this issue, two countermeasures need
1373*4882a593Smuzhiyun  * to be taken.
1374*4882a593Smuzhiyun  *
1375*4882a593Smuzhiyun  * First, the budget and the timestamps of bfqq need to be updated in
1376*4882a593Smuzhiyun  * a special way on bfqq reactivation: they need to be updated as if
1377*4882a593Smuzhiyun  * bfqq did not remain idle and did not expire. In fact, if they are
1378*4882a593Smuzhiyun  * computed as if bfqq expired and remained idle until reactivation,
1379*4882a593Smuzhiyun  * then the process associated with bfqq is treated as if, instead of
1380*4882a593Smuzhiyun  * being greedy, it stopped issuing requests when bfqq remained idle,
1381*4882a593Smuzhiyun  * and restarts issuing requests only on this reactivation. In other
1382*4882a593Smuzhiyun  * words, the scheduler does not help the process recover the "service
1383*4882a593Smuzhiyun  * hole" between bfqq expiration and reactivation. As a consequence,
1384*4882a593Smuzhiyun  * the process receives a lower bandwidth than its reserved one. In
1385*4882a593Smuzhiyun  * contrast, to recover this hole, the budget must be updated as if
1386*4882a593Smuzhiyun  * bfqq was not expired at all before this reactivation, i.e., it must
1387*4882a593Smuzhiyun  * be set to the value of the remaining budget when bfqq was
1388*4882a593Smuzhiyun  * expired. Along the same line, timestamps need to be assigned the
1389*4882a593Smuzhiyun  * value they had the last time bfqq was selected for service, i.e.,
1390*4882a593Smuzhiyun  * before last expiration. Thus timestamps need to be back-shifted
1391*4882a593Smuzhiyun  * with respect to their normal computation (see [1] for more details
1392*4882a593Smuzhiyun  * on this tricky aspect).
1393*4882a593Smuzhiyun  *
1394*4882a593Smuzhiyun  * Secondly, to allow the process to recover the hole, the in-service
1395*4882a593Smuzhiyun  * queue must be expired too, to give bfqq the chance to preempt it
1396*4882a593Smuzhiyun  * immediately. In fact, if bfqq has to wait for a full budget of the
1397*4882a593Smuzhiyun  * in-service queue to be completed, then it may become impossible to
1398*4882a593Smuzhiyun  * let the process recover the hole, even if the back-shifted
1399*4882a593Smuzhiyun  * timestamps of bfqq are lower than those of the in-service queue. If
1400*4882a593Smuzhiyun  * this happens for most or all of the holes, then the process may not
1401*4882a593Smuzhiyun  * receive its reserved bandwidth. In this respect, it is worth noting
1402*4882a593Smuzhiyun  * that, being the service of outstanding requests unpreemptible, a
1403*4882a593Smuzhiyun  * little fraction of the holes may however be unrecoverable, thereby
1404*4882a593Smuzhiyun  * causing a little loss of bandwidth.
1405*4882a593Smuzhiyun  *
1406*4882a593Smuzhiyun  * The last important point is detecting whether bfqq does need this
1407*4882a593Smuzhiyun  * bandwidth recovery. In this respect, the next function deems the
1408*4882a593Smuzhiyun  * process associated with bfqq greedy, and thus allows it to recover
1409*4882a593Smuzhiyun  * the hole, if: 1) the process is waiting for the arrival of a new
1410*4882a593Smuzhiyun  * request (which implies that bfqq expired for one of the above two
1411*4882a593Smuzhiyun  * reasons), and 2) such a request has arrived soon. The first
1412*4882a593Smuzhiyun  * condition is controlled through the flag non_blocking_wait_rq,
1413*4882a593Smuzhiyun  * while the second through the flag arrived_in_time. If both
1414*4882a593Smuzhiyun  * conditions hold, then the function computes the budget in the
1415*4882a593Smuzhiyun  * above-described special way, and signals that the in-service queue
1416*4882a593Smuzhiyun  * should be expired. Timestamp back-shifting is done later in
1417*4882a593Smuzhiyun  * __bfq_activate_entity.
1418*4882a593Smuzhiyun  *
1419*4882a593Smuzhiyun  * 2. Reduce latency. Even if timestamps are not backshifted to let
1420*4882a593Smuzhiyun  * the process associated with bfqq recover a service hole, bfqq may
1421*4882a593Smuzhiyun  * however happen to have, after being (re)activated, a lower finish
1422*4882a593Smuzhiyun  * timestamp than the in-service queue.	 That is, the next budget of
1423*4882a593Smuzhiyun  * bfqq may have to be completed before the one of the in-service
1424*4882a593Smuzhiyun  * queue. If this is the case, then preempting the in-service queue
1425*4882a593Smuzhiyun  * allows this goal to be achieved, apart from the unpreemptible,
1426*4882a593Smuzhiyun  * outstanding requests mentioned above.
1427*4882a593Smuzhiyun  *
1428*4882a593Smuzhiyun  * Unfortunately, regardless of which of the above two goals one wants
1429*4882a593Smuzhiyun  * to achieve, service trees need first to be updated to know whether
1430*4882a593Smuzhiyun  * the in-service queue must be preempted. To have service trees
1431*4882a593Smuzhiyun  * correctly updated, the in-service queue must be expired and
1432*4882a593Smuzhiyun  * rescheduled, and bfqq must be scheduled too. This is one of the
1433*4882a593Smuzhiyun  * most costly operations (in future versions, the scheduling
1434*4882a593Smuzhiyun  * mechanism may be re-designed in such a way to make it possible to
1435*4882a593Smuzhiyun  * know whether preemption is needed without needing to update service
1436*4882a593Smuzhiyun  * trees). In addition, queue preemptions almost always cause random
1437*4882a593Smuzhiyun  * I/O, which may in turn cause loss of throughput. Finally, there may
1438*4882a593Smuzhiyun  * even be no in-service queue when the next function is invoked (so,
1439*4882a593Smuzhiyun  * no queue to compare timestamps with). Because of these facts, the
1440*4882a593Smuzhiyun  * next function adopts the following simple scheme to avoid costly
1441*4882a593Smuzhiyun  * operations, too frequent preemptions and too many dependencies on
1442*4882a593Smuzhiyun  * the state of the scheduler: it requests the expiration of the
1443*4882a593Smuzhiyun  * in-service queue (unconditionally) only for queues that need to
1444*4882a593Smuzhiyun  * recover a hole. Then it delegates to other parts of the code the
1445*4882a593Smuzhiyun  * responsibility of handling the above case 2.
1446*4882a593Smuzhiyun  */
bfq_bfqq_update_budg_for_activation(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool arrived_in_time)1447*4882a593Smuzhiyun static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd,
1448*4882a593Smuzhiyun 						struct bfq_queue *bfqq,
1449*4882a593Smuzhiyun 						bool arrived_in_time)
1450*4882a593Smuzhiyun {
1451*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	/*
1454*4882a593Smuzhiyun 	 * In the next compound condition, we check also whether there
1455*4882a593Smuzhiyun 	 * is some budget left, because otherwise there is no point in
1456*4882a593Smuzhiyun 	 * trying to go on serving bfqq with this same budget: bfqq
1457*4882a593Smuzhiyun 	 * would be expired immediately after being selected for
1458*4882a593Smuzhiyun 	 * service. This would only cause useless overhead.
1459*4882a593Smuzhiyun 	 */
1460*4882a593Smuzhiyun 	if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time &&
1461*4882a593Smuzhiyun 	    bfq_bfqq_budget_left(bfqq) > 0) {
1462*4882a593Smuzhiyun 		/*
1463*4882a593Smuzhiyun 		 * We do not clear the flag non_blocking_wait_rq here, as
1464*4882a593Smuzhiyun 		 * the latter is used in bfq_activate_bfqq to signal
1465*4882a593Smuzhiyun 		 * that timestamps need to be back-shifted (and is
1466*4882a593Smuzhiyun 		 * cleared right after).
1467*4882a593Smuzhiyun 		 */
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun 		/*
1470*4882a593Smuzhiyun 		 * In next assignment we rely on that either
1471*4882a593Smuzhiyun 		 * entity->service or entity->budget are not updated
1472*4882a593Smuzhiyun 		 * on expiration if bfqq is empty (see
1473*4882a593Smuzhiyun 		 * __bfq_bfqq_recalc_budget). Thus both quantities
1474*4882a593Smuzhiyun 		 * remain unchanged after such an expiration, and the
1475*4882a593Smuzhiyun 		 * following statement therefore assigns to
1476*4882a593Smuzhiyun 		 * entity->budget the remaining budget on such an
1477*4882a593Smuzhiyun 		 * expiration.
1478*4882a593Smuzhiyun 		 */
1479*4882a593Smuzhiyun 		entity->budget = min_t(unsigned long,
1480*4882a593Smuzhiyun 				       bfq_bfqq_budget_left(bfqq),
1481*4882a593Smuzhiyun 				       bfqq->max_budget);
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 		/*
1484*4882a593Smuzhiyun 		 * At this point, we have used entity->service to get
1485*4882a593Smuzhiyun 		 * the budget left (needed for updating
1486*4882a593Smuzhiyun 		 * entity->budget). Thus we finally can, and have to,
1487*4882a593Smuzhiyun 		 * reset entity->service. The latter must be reset
1488*4882a593Smuzhiyun 		 * because bfqq would otherwise be charged again for
1489*4882a593Smuzhiyun 		 * the service it has received during its previous
1490*4882a593Smuzhiyun 		 * service slot(s).
1491*4882a593Smuzhiyun 		 */
1492*4882a593Smuzhiyun 		entity->service = 0;
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 		return true;
1495*4882a593Smuzhiyun 	}
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	/*
1498*4882a593Smuzhiyun 	 * We can finally complete expiration, by setting service to 0.
1499*4882a593Smuzhiyun 	 */
1500*4882a593Smuzhiyun 	entity->service = 0;
1501*4882a593Smuzhiyun 	entity->budget = max_t(unsigned long, bfqq->max_budget,
1502*4882a593Smuzhiyun 			       bfq_serv_to_charge(bfqq->next_rq, bfqq));
1503*4882a593Smuzhiyun 	bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
1504*4882a593Smuzhiyun 	return false;
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun /*
1508*4882a593Smuzhiyun  * Return the farthest past time instant according to jiffies
1509*4882a593Smuzhiyun  * macros.
1510*4882a593Smuzhiyun  */
bfq_smallest_from_now(void)1511*4882a593Smuzhiyun static unsigned long bfq_smallest_from_now(void)
1512*4882a593Smuzhiyun {
1513*4882a593Smuzhiyun 	return jiffies - MAX_JIFFY_OFFSET;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun 
bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data * bfqd,struct bfq_queue * bfqq,unsigned int old_wr_coeff,bool wr_or_deserves_wr,bool interactive,bool in_burst,bool soft_rt)1516*4882a593Smuzhiyun static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd,
1517*4882a593Smuzhiyun 					     struct bfq_queue *bfqq,
1518*4882a593Smuzhiyun 					     unsigned int old_wr_coeff,
1519*4882a593Smuzhiyun 					     bool wr_or_deserves_wr,
1520*4882a593Smuzhiyun 					     bool interactive,
1521*4882a593Smuzhiyun 					     bool in_burst,
1522*4882a593Smuzhiyun 					     bool soft_rt)
1523*4882a593Smuzhiyun {
1524*4882a593Smuzhiyun 	if (old_wr_coeff == 1 && wr_or_deserves_wr) {
1525*4882a593Smuzhiyun 		/* start a weight-raising period */
1526*4882a593Smuzhiyun 		if (interactive) {
1527*4882a593Smuzhiyun 			bfqq->service_from_wr = 0;
1528*4882a593Smuzhiyun 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1529*4882a593Smuzhiyun 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1530*4882a593Smuzhiyun 		} else {
1531*4882a593Smuzhiyun 			/*
1532*4882a593Smuzhiyun 			 * No interactive weight raising in progress
1533*4882a593Smuzhiyun 			 * here: assign minus infinity to
1534*4882a593Smuzhiyun 			 * wr_start_at_switch_to_srt, to make sure
1535*4882a593Smuzhiyun 			 * that, at the end of the soft-real-time
1536*4882a593Smuzhiyun 			 * weight raising periods that is starting
1537*4882a593Smuzhiyun 			 * now, no interactive weight-raising period
1538*4882a593Smuzhiyun 			 * may be wrongly considered as still in
1539*4882a593Smuzhiyun 			 * progress (and thus actually started by
1540*4882a593Smuzhiyun 			 * mistake).
1541*4882a593Smuzhiyun 			 */
1542*4882a593Smuzhiyun 			bfqq->wr_start_at_switch_to_srt =
1543*4882a593Smuzhiyun 				bfq_smallest_from_now();
1544*4882a593Smuzhiyun 			bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1545*4882a593Smuzhiyun 				BFQ_SOFTRT_WEIGHT_FACTOR;
1546*4882a593Smuzhiyun 			bfqq->wr_cur_max_time =
1547*4882a593Smuzhiyun 				bfqd->bfq_wr_rt_max_time;
1548*4882a593Smuzhiyun 		}
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 		/*
1551*4882a593Smuzhiyun 		 * If needed, further reduce budget to make sure it is
1552*4882a593Smuzhiyun 		 * close to bfqq's backlog, so as to reduce the
1553*4882a593Smuzhiyun 		 * scheduling-error component due to a too large
1554*4882a593Smuzhiyun 		 * budget. Do not care about throughput consequences,
1555*4882a593Smuzhiyun 		 * but only about latency. Finally, do not assign a
1556*4882a593Smuzhiyun 		 * too small budget either, to avoid increasing
1557*4882a593Smuzhiyun 		 * latency by causing too frequent expirations.
1558*4882a593Smuzhiyun 		 */
1559*4882a593Smuzhiyun 		bfqq->entity.budget = min_t(unsigned long,
1560*4882a593Smuzhiyun 					    bfqq->entity.budget,
1561*4882a593Smuzhiyun 					    2 * bfq_min_budget(bfqd));
1562*4882a593Smuzhiyun 	} else if (old_wr_coeff > 1) {
1563*4882a593Smuzhiyun 		if (interactive) { /* update wr coeff and duration */
1564*4882a593Smuzhiyun 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1565*4882a593Smuzhiyun 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1566*4882a593Smuzhiyun 		} else if (in_burst)
1567*4882a593Smuzhiyun 			bfqq->wr_coeff = 1;
1568*4882a593Smuzhiyun 		else if (soft_rt) {
1569*4882a593Smuzhiyun 			/*
1570*4882a593Smuzhiyun 			 * The application is now or still meeting the
1571*4882a593Smuzhiyun 			 * requirements for being deemed soft rt.  We
1572*4882a593Smuzhiyun 			 * can then correctly and safely (re)charge
1573*4882a593Smuzhiyun 			 * the weight-raising duration for the
1574*4882a593Smuzhiyun 			 * application with the weight-raising
1575*4882a593Smuzhiyun 			 * duration for soft rt applications.
1576*4882a593Smuzhiyun 			 *
1577*4882a593Smuzhiyun 			 * In particular, doing this recharge now, i.e.,
1578*4882a593Smuzhiyun 			 * before the weight-raising period for the
1579*4882a593Smuzhiyun 			 * application finishes, reduces the probability
1580*4882a593Smuzhiyun 			 * of the following negative scenario:
1581*4882a593Smuzhiyun 			 * 1) the weight of a soft rt application is
1582*4882a593Smuzhiyun 			 *    raised at startup (as for any newly
1583*4882a593Smuzhiyun 			 *    created application),
1584*4882a593Smuzhiyun 			 * 2) since the application is not interactive,
1585*4882a593Smuzhiyun 			 *    at a certain time weight-raising is
1586*4882a593Smuzhiyun 			 *    stopped for the application,
1587*4882a593Smuzhiyun 			 * 3) at that time the application happens to
1588*4882a593Smuzhiyun 			 *    still have pending requests, and hence
1589*4882a593Smuzhiyun 			 *    is destined to not have a chance to be
1590*4882a593Smuzhiyun 			 *    deemed soft rt before these requests are
1591*4882a593Smuzhiyun 			 *    completed (see the comments to the
1592*4882a593Smuzhiyun 			 *    function bfq_bfqq_softrt_next_start()
1593*4882a593Smuzhiyun 			 *    for details on soft rt detection),
1594*4882a593Smuzhiyun 			 * 4) these pending requests experience a high
1595*4882a593Smuzhiyun 			 *    latency because the application is not
1596*4882a593Smuzhiyun 			 *    weight-raised while they are pending.
1597*4882a593Smuzhiyun 			 */
1598*4882a593Smuzhiyun 			if (bfqq->wr_cur_max_time !=
1599*4882a593Smuzhiyun 				bfqd->bfq_wr_rt_max_time) {
1600*4882a593Smuzhiyun 				bfqq->wr_start_at_switch_to_srt =
1601*4882a593Smuzhiyun 					bfqq->last_wr_start_finish;
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 				bfqq->wr_cur_max_time =
1604*4882a593Smuzhiyun 					bfqd->bfq_wr_rt_max_time;
1605*4882a593Smuzhiyun 				bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1606*4882a593Smuzhiyun 					BFQ_SOFTRT_WEIGHT_FACTOR;
1607*4882a593Smuzhiyun 			}
1608*4882a593Smuzhiyun 			bfqq->last_wr_start_finish = jiffies;
1609*4882a593Smuzhiyun 		}
1610*4882a593Smuzhiyun 	}
1611*4882a593Smuzhiyun }
1612*4882a593Smuzhiyun 
bfq_bfqq_idle_for_long_time(struct bfq_data * bfqd,struct bfq_queue * bfqq)1613*4882a593Smuzhiyun static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd,
1614*4882a593Smuzhiyun 					struct bfq_queue *bfqq)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun 	return bfqq->dispatched == 0 &&
1617*4882a593Smuzhiyun 		time_is_before_jiffies(
1618*4882a593Smuzhiyun 			bfqq->budget_timeout +
1619*4882a593Smuzhiyun 			bfqd->bfq_wr_min_idle_time);
1620*4882a593Smuzhiyun }
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun /*
1624*4882a593Smuzhiyun  * Return true if bfqq is in a higher priority class, or has a higher
1625*4882a593Smuzhiyun  * weight than the in-service queue.
1626*4882a593Smuzhiyun  */
bfq_bfqq_higher_class_or_weight(struct bfq_queue * bfqq,struct bfq_queue * in_serv_bfqq)1627*4882a593Smuzhiyun static bool bfq_bfqq_higher_class_or_weight(struct bfq_queue *bfqq,
1628*4882a593Smuzhiyun 					    struct bfq_queue *in_serv_bfqq)
1629*4882a593Smuzhiyun {
1630*4882a593Smuzhiyun 	int bfqq_weight, in_serv_weight;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	if (bfqq->ioprio_class < in_serv_bfqq->ioprio_class)
1633*4882a593Smuzhiyun 		return true;
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	if (in_serv_bfqq->entity.parent == bfqq->entity.parent) {
1636*4882a593Smuzhiyun 		bfqq_weight = bfqq->entity.weight;
1637*4882a593Smuzhiyun 		in_serv_weight = in_serv_bfqq->entity.weight;
1638*4882a593Smuzhiyun 	} else {
1639*4882a593Smuzhiyun 		if (bfqq->entity.parent)
1640*4882a593Smuzhiyun 			bfqq_weight = bfqq->entity.parent->weight;
1641*4882a593Smuzhiyun 		else
1642*4882a593Smuzhiyun 			bfqq_weight = bfqq->entity.weight;
1643*4882a593Smuzhiyun 		if (in_serv_bfqq->entity.parent)
1644*4882a593Smuzhiyun 			in_serv_weight = in_serv_bfqq->entity.parent->weight;
1645*4882a593Smuzhiyun 		else
1646*4882a593Smuzhiyun 			in_serv_weight = in_serv_bfqq->entity.weight;
1647*4882a593Smuzhiyun 	}
1648*4882a593Smuzhiyun 
1649*4882a593Smuzhiyun 	return bfqq_weight > in_serv_weight;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
bfq_bfqq_handle_idle_busy_switch(struct bfq_data * bfqd,struct bfq_queue * bfqq,int old_wr_coeff,struct request * rq,bool * interactive)1652*4882a593Smuzhiyun static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd,
1653*4882a593Smuzhiyun 					     struct bfq_queue *bfqq,
1654*4882a593Smuzhiyun 					     int old_wr_coeff,
1655*4882a593Smuzhiyun 					     struct request *rq,
1656*4882a593Smuzhiyun 					     bool *interactive)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun 	bool soft_rt, in_burst,	wr_or_deserves_wr,
1659*4882a593Smuzhiyun 		bfqq_wants_to_preempt,
1660*4882a593Smuzhiyun 		idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq),
1661*4882a593Smuzhiyun 		/*
1662*4882a593Smuzhiyun 		 * See the comments on
1663*4882a593Smuzhiyun 		 * bfq_bfqq_update_budg_for_activation for
1664*4882a593Smuzhiyun 		 * details on the usage of the next variable.
1665*4882a593Smuzhiyun 		 */
1666*4882a593Smuzhiyun 		arrived_in_time =  ktime_get_ns() <=
1667*4882a593Smuzhiyun 			bfqq->ttime.last_end_request +
1668*4882a593Smuzhiyun 			bfqd->bfq_slice_idle * 3;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	/*
1672*4882a593Smuzhiyun 	 * bfqq deserves to be weight-raised if:
1673*4882a593Smuzhiyun 	 * - it is sync,
1674*4882a593Smuzhiyun 	 * - it does not belong to a large burst,
1675*4882a593Smuzhiyun 	 * - it has been idle for enough time or is soft real-time,
1676*4882a593Smuzhiyun 	 * - is linked to a bfq_io_cq (it is not shared in any sense).
1677*4882a593Smuzhiyun 	 */
1678*4882a593Smuzhiyun 	in_burst = bfq_bfqq_in_large_burst(bfqq);
1679*4882a593Smuzhiyun 	soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
1680*4882a593Smuzhiyun 		!BFQQ_TOTALLY_SEEKY(bfqq) &&
1681*4882a593Smuzhiyun 		!in_burst &&
1682*4882a593Smuzhiyun 		time_is_before_jiffies(bfqq->soft_rt_next_start) &&
1683*4882a593Smuzhiyun 		bfqq->dispatched == 0;
1684*4882a593Smuzhiyun 	*interactive = !in_burst && idle_for_long_time;
1685*4882a593Smuzhiyun 	wr_or_deserves_wr = bfqd->low_latency &&
1686*4882a593Smuzhiyun 		(bfqq->wr_coeff > 1 ||
1687*4882a593Smuzhiyun 		 (bfq_bfqq_sync(bfqq) &&
1688*4882a593Smuzhiyun 		  bfqq->bic && (*interactive || soft_rt)));
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	/*
1691*4882a593Smuzhiyun 	 * Using the last flag, update budget and check whether bfqq
1692*4882a593Smuzhiyun 	 * may want to preempt the in-service queue.
1693*4882a593Smuzhiyun 	 */
1694*4882a593Smuzhiyun 	bfqq_wants_to_preempt =
1695*4882a593Smuzhiyun 		bfq_bfqq_update_budg_for_activation(bfqd, bfqq,
1696*4882a593Smuzhiyun 						    arrived_in_time);
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun 	/*
1699*4882a593Smuzhiyun 	 * If bfqq happened to be activated in a burst, but has been
1700*4882a593Smuzhiyun 	 * idle for much more than an interactive queue, then we
1701*4882a593Smuzhiyun 	 * assume that, in the overall I/O initiated in the burst, the
1702*4882a593Smuzhiyun 	 * I/O associated with bfqq is finished. So bfqq does not need
1703*4882a593Smuzhiyun 	 * to be treated as a queue belonging to a burst
1704*4882a593Smuzhiyun 	 * anymore. Accordingly, we reset bfqq's in_large_burst flag
1705*4882a593Smuzhiyun 	 * if set, and remove bfqq from the burst list if it's
1706*4882a593Smuzhiyun 	 * there. We do not decrement burst_size, because the fact
1707*4882a593Smuzhiyun 	 * that bfqq does not need to belong to the burst list any
1708*4882a593Smuzhiyun 	 * more does not invalidate the fact that bfqq was created in
1709*4882a593Smuzhiyun 	 * a burst.
1710*4882a593Smuzhiyun 	 */
1711*4882a593Smuzhiyun 	if (likely(!bfq_bfqq_just_created(bfqq)) &&
1712*4882a593Smuzhiyun 	    idle_for_long_time &&
1713*4882a593Smuzhiyun 	    time_is_before_jiffies(
1714*4882a593Smuzhiyun 		    bfqq->budget_timeout +
1715*4882a593Smuzhiyun 		    msecs_to_jiffies(10000))) {
1716*4882a593Smuzhiyun 		hlist_del_init(&bfqq->burst_list_node);
1717*4882a593Smuzhiyun 		bfq_clear_bfqq_in_large_burst(bfqq);
1718*4882a593Smuzhiyun 	}
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 	bfq_clear_bfqq_just_created(bfqq);
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun 	if (!bfq_bfqq_IO_bound(bfqq)) {
1724*4882a593Smuzhiyun 		if (arrived_in_time) {
1725*4882a593Smuzhiyun 			bfqq->requests_within_timer++;
1726*4882a593Smuzhiyun 			if (bfqq->requests_within_timer >=
1727*4882a593Smuzhiyun 			    bfqd->bfq_requests_within_timer)
1728*4882a593Smuzhiyun 				bfq_mark_bfqq_IO_bound(bfqq);
1729*4882a593Smuzhiyun 		} else
1730*4882a593Smuzhiyun 			bfqq->requests_within_timer = 0;
1731*4882a593Smuzhiyun 	}
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	if (bfqd->low_latency) {
1734*4882a593Smuzhiyun 		if (unlikely(time_is_after_jiffies(bfqq->split_time)))
1735*4882a593Smuzhiyun 			/* wraparound */
1736*4882a593Smuzhiyun 			bfqq->split_time =
1737*4882a593Smuzhiyun 				jiffies - bfqd->bfq_wr_min_idle_time - 1;
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 		if (time_is_before_jiffies(bfqq->split_time +
1740*4882a593Smuzhiyun 					   bfqd->bfq_wr_min_idle_time)) {
1741*4882a593Smuzhiyun 			bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq,
1742*4882a593Smuzhiyun 							 old_wr_coeff,
1743*4882a593Smuzhiyun 							 wr_or_deserves_wr,
1744*4882a593Smuzhiyun 							 *interactive,
1745*4882a593Smuzhiyun 							 in_burst,
1746*4882a593Smuzhiyun 							 soft_rt);
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 			if (old_wr_coeff != bfqq->wr_coeff)
1749*4882a593Smuzhiyun 				bfqq->entity.prio_changed = 1;
1750*4882a593Smuzhiyun 		}
1751*4882a593Smuzhiyun 	}
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 	bfqq->last_idle_bklogged = jiffies;
1754*4882a593Smuzhiyun 	bfqq->service_from_backlogged = 0;
1755*4882a593Smuzhiyun 	bfq_clear_bfqq_softrt_update(bfqq);
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	bfq_add_bfqq_busy(bfqd, bfqq);
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	/*
1760*4882a593Smuzhiyun 	 * Expire in-service queue only if preemption may be needed
1761*4882a593Smuzhiyun 	 * for guarantees. In particular, we care only about two
1762*4882a593Smuzhiyun 	 * cases. The first is that bfqq has to recover a service
1763*4882a593Smuzhiyun 	 * hole, as explained in the comments on
1764*4882a593Smuzhiyun 	 * bfq_bfqq_update_budg_for_activation(), i.e., that
1765*4882a593Smuzhiyun 	 * bfqq_wants_to_preempt is true. However, if bfqq does not
1766*4882a593Smuzhiyun 	 * carry time-critical I/O, then bfqq's bandwidth is less
1767*4882a593Smuzhiyun 	 * important than that of queues that carry time-critical I/O.
1768*4882a593Smuzhiyun 	 * So, as a further constraint, we consider this case only if
1769*4882a593Smuzhiyun 	 * bfqq is at least as weight-raised, i.e., at least as time
1770*4882a593Smuzhiyun 	 * critical, as the in-service queue.
1771*4882a593Smuzhiyun 	 *
1772*4882a593Smuzhiyun 	 * The second case is that bfqq is in a higher priority class,
1773*4882a593Smuzhiyun 	 * or has a higher weight than the in-service queue. If this
1774*4882a593Smuzhiyun 	 * condition does not hold, we don't care because, even if
1775*4882a593Smuzhiyun 	 * bfqq does not start to be served immediately, the resulting
1776*4882a593Smuzhiyun 	 * delay for bfqq's I/O is however lower or much lower than
1777*4882a593Smuzhiyun 	 * the ideal completion time to be guaranteed to bfqq's I/O.
1778*4882a593Smuzhiyun 	 *
1779*4882a593Smuzhiyun 	 * In both cases, preemption is needed only if, according to
1780*4882a593Smuzhiyun 	 * the timestamps of both bfqq and of the in-service queue,
1781*4882a593Smuzhiyun 	 * bfqq actually is the next queue to serve. So, to reduce
1782*4882a593Smuzhiyun 	 * useless preemptions, the return value of
1783*4882a593Smuzhiyun 	 * next_queue_may_preempt() is considered in the next compound
1784*4882a593Smuzhiyun 	 * condition too. Yet next_queue_may_preempt() just checks a
1785*4882a593Smuzhiyun 	 * simple, necessary condition for bfqq to be the next queue
1786*4882a593Smuzhiyun 	 * to serve. In fact, to evaluate a sufficient condition, the
1787*4882a593Smuzhiyun 	 * timestamps of the in-service queue would need to be
1788*4882a593Smuzhiyun 	 * updated, and this operation is quite costly (see the
1789*4882a593Smuzhiyun 	 * comments on bfq_bfqq_update_budg_for_activation()).
1790*4882a593Smuzhiyun 	 */
1791*4882a593Smuzhiyun 	if (bfqd->in_service_queue &&
1792*4882a593Smuzhiyun 	    ((bfqq_wants_to_preempt &&
1793*4882a593Smuzhiyun 	      bfqq->wr_coeff >= bfqd->in_service_queue->wr_coeff) ||
1794*4882a593Smuzhiyun 	     bfq_bfqq_higher_class_or_weight(bfqq, bfqd->in_service_queue)) &&
1795*4882a593Smuzhiyun 	    next_queue_may_preempt(bfqd))
1796*4882a593Smuzhiyun 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
1797*4882a593Smuzhiyun 				false, BFQQE_PREEMPTED);
1798*4882a593Smuzhiyun }
1799*4882a593Smuzhiyun 
bfq_reset_inject_limit(struct bfq_data * bfqd,struct bfq_queue * bfqq)1800*4882a593Smuzhiyun static void bfq_reset_inject_limit(struct bfq_data *bfqd,
1801*4882a593Smuzhiyun 				   struct bfq_queue *bfqq)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun 	/* invalidate baseline total service time */
1804*4882a593Smuzhiyun 	bfqq->last_serv_time_ns = 0;
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	/*
1807*4882a593Smuzhiyun 	 * Reset pointer in case we are waiting for
1808*4882a593Smuzhiyun 	 * some request completion.
1809*4882a593Smuzhiyun 	 */
1810*4882a593Smuzhiyun 	bfqd->waited_rq = NULL;
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	/*
1813*4882a593Smuzhiyun 	 * If bfqq has a short think time, then start by setting the
1814*4882a593Smuzhiyun 	 * inject limit to 0 prudentially, because the service time of
1815*4882a593Smuzhiyun 	 * an injected I/O request may be higher than the think time
1816*4882a593Smuzhiyun 	 * of bfqq, and therefore, if one request was injected when
1817*4882a593Smuzhiyun 	 * bfqq remains empty, this injected request might delay the
1818*4882a593Smuzhiyun 	 * service of the next I/O request for bfqq significantly. In
1819*4882a593Smuzhiyun 	 * case bfqq can actually tolerate some injection, then the
1820*4882a593Smuzhiyun 	 * adaptive update will however raise the limit soon. This
1821*4882a593Smuzhiyun 	 * lucky circumstance holds exactly because bfqq has a short
1822*4882a593Smuzhiyun 	 * think time, and thus, after remaining empty, is likely to
1823*4882a593Smuzhiyun 	 * get new I/O enqueued---and then completed---before being
1824*4882a593Smuzhiyun 	 * expired. This is the very pattern that gives the
1825*4882a593Smuzhiyun 	 * limit-update algorithm the chance to measure the effect of
1826*4882a593Smuzhiyun 	 * injection on request service times, and then to update the
1827*4882a593Smuzhiyun 	 * limit accordingly.
1828*4882a593Smuzhiyun 	 *
1829*4882a593Smuzhiyun 	 * However, in the following special case, the inject limit is
1830*4882a593Smuzhiyun 	 * left to 1 even if the think time is short: bfqq's I/O is
1831*4882a593Smuzhiyun 	 * synchronized with that of some other queue, i.e., bfqq may
1832*4882a593Smuzhiyun 	 * receive new I/O only after the I/O of the other queue is
1833*4882a593Smuzhiyun 	 * completed. Keeping the inject limit to 1 allows the
1834*4882a593Smuzhiyun 	 * blocking I/O to be served while bfqq is in service. And
1835*4882a593Smuzhiyun 	 * this is very convenient both for bfqq and for overall
1836*4882a593Smuzhiyun 	 * throughput, as explained in detail in the comments in
1837*4882a593Smuzhiyun 	 * bfq_update_has_short_ttime().
1838*4882a593Smuzhiyun 	 *
1839*4882a593Smuzhiyun 	 * On the opposite end, if bfqq has a long think time, then
1840*4882a593Smuzhiyun 	 * start directly by 1, because:
1841*4882a593Smuzhiyun 	 * a) on the bright side, keeping at most one request in
1842*4882a593Smuzhiyun 	 * service in the drive is unlikely to cause any harm to the
1843*4882a593Smuzhiyun 	 * latency of bfqq's requests, as the service time of a single
1844*4882a593Smuzhiyun 	 * request is likely to be lower than the think time of bfqq;
1845*4882a593Smuzhiyun 	 * b) on the downside, after becoming empty, bfqq is likely to
1846*4882a593Smuzhiyun 	 * expire before getting its next request. With this request
1847*4882a593Smuzhiyun 	 * arrival pattern, it is very hard to sample total service
1848*4882a593Smuzhiyun 	 * times and update the inject limit accordingly (see comments
1849*4882a593Smuzhiyun 	 * on bfq_update_inject_limit()). So the limit is likely to be
1850*4882a593Smuzhiyun 	 * never, or at least seldom, updated.  As a consequence, by
1851*4882a593Smuzhiyun 	 * setting the limit to 1, we avoid that no injection ever
1852*4882a593Smuzhiyun 	 * occurs with bfqq. On the downside, this proactive step
1853*4882a593Smuzhiyun 	 * further reduces chances to actually compute the baseline
1854*4882a593Smuzhiyun 	 * total service time. Thus it reduces chances to execute the
1855*4882a593Smuzhiyun 	 * limit-update algorithm and possibly raise the limit to more
1856*4882a593Smuzhiyun 	 * than 1.
1857*4882a593Smuzhiyun 	 */
1858*4882a593Smuzhiyun 	if (bfq_bfqq_has_short_ttime(bfqq))
1859*4882a593Smuzhiyun 		bfqq->inject_limit = 0;
1860*4882a593Smuzhiyun 	else
1861*4882a593Smuzhiyun 		bfqq->inject_limit = 1;
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun 	bfqq->decrease_time_jif = jiffies;
1864*4882a593Smuzhiyun }
1865*4882a593Smuzhiyun 
bfq_add_request(struct request * rq)1866*4882a593Smuzhiyun static void bfq_add_request(struct request *rq)
1867*4882a593Smuzhiyun {
1868*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
1869*4882a593Smuzhiyun 	struct bfq_data *bfqd = bfqq->bfqd;
1870*4882a593Smuzhiyun 	struct request *next_rq, *prev;
1871*4882a593Smuzhiyun 	unsigned int old_wr_coeff = bfqq->wr_coeff;
1872*4882a593Smuzhiyun 	bool interactive = false;
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
1875*4882a593Smuzhiyun 	bfqq->queued[rq_is_sync(rq)]++;
1876*4882a593Smuzhiyun 	bfqd->queued++;
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_bfqq_sync(bfqq)) {
1879*4882a593Smuzhiyun 		/*
1880*4882a593Smuzhiyun 		 * Detect whether bfqq's I/O seems synchronized with
1881*4882a593Smuzhiyun 		 * that of some other queue, i.e., whether bfqq, after
1882*4882a593Smuzhiyun 		 * remaining empty, happens to receive new I/O only
1883*4882a593Smuzhiyun 		 * right after some I/O request of the other queue has
1884*4882a593Smuzhiyun 		 * been completed. We call waker queue the other
1885*4882a593Smuzhiyun 		 * queue, and we assume, for simplicity, that bfqq may
1886*4882a593Smuzhiyun 		 * have at most one waker queue.
1887*4882a593Smuzhiyun 		 *
1888*4882a593Smuzhiyun 		 * A remarkable throughput boost can be reached by
1889*4882a593Smuzhiyun 		 * unconditionally injecting the I/O of the waker
1890*4882a593Smuzhiyun 		 * queue, every time a new bfq_dispatch_request
1891*4882a593Smuzhiyun 		 * happens to be invoked while I/O is being plugged
1892*4882a593Smuzhiyun 		 * for bfqq.  In addition to boosting throughput, this
1893*4882a593Smuzhiyun 		 * unblocks bfqq's I/O, thereby improving bandwidth
1894*4882a593Smuzhiyun 		 * and latency for bfqq. Note that these same results
1895*4882a593Smuzhiyun 		 * may be achieved with the general injection
1896*4882a593Smuzhiyun 		 * mechanism, but less effectively. For details on
1897*4882a593Smuzhiyun 		 * this aspect, see the comments on the choice of the
1898*4882a593Smuzhiyun 		 * queue for injection in bfq_select_queue().
1899*4882a593Smuzhiyun 		 *
1900*4882a593Smuzhiyun 		 * Turning back to the detection of a waker queue, a
1901*4882a593Smuzhiyun 		 * queue Q is deemed as a waker queue for bfqq if, for
1902*4882a593Smuzhiyun 		 * two consecutive times, bfqq happens to become non
1903*4882a593Smuzhiyun 		 * empty right after a request of Q has been
1904*4882a593Smuzhiyun 		 * completed. In particular, on the first time, Q is
1905*4882a593Smuzhiyun 		 * tentatively set as a candidate waker queue, while
1906*4882a593Smuzhiyun 		 * on the second time, the flag
1907*4882a593Smuzhiyun 		 * bfq_bfqq_has_waker(bfqq) is set to confirm that Q
1908*4882a593Smuzhiyun 		 * is a waker queue for bfqq. These detection steps
1909*4882a593Smuzhiyun 		 * are performed only if bfqq has a long think time,
1910*4882a593Smuzhiyun 		 * so as to make it more likely that bfqq's I/O is
1911*4882a593Smuzhiyun 		 * actually being blocked by a synchronization. This
1912*4882a593Smuzhiyun 		 * last filter, plus the above two-times requirement,
1913*4882a593Smuzhiyun 		 * make false positives less likely.
1914*4882a593Smuzhiyun 		 *
1915*4882a593Smuzhiyun 		 * NOTE
1916*4882a593Smuzhiyun 		 *
1917*4882a593Smuzhiyun 		 * The sooner a waker queue is detected, the sooner
1918*4882a593Smuzhiyun 		 * throughput can be boosted by injecting I/O from the
1919*4882a593Smuzhiyun 		 * waker queue. Fortunately, detection is likely to be
1920*4882a593Smuzhiyun 		 * actually fast, for the following reasons. While
1921*4882a593Smuzhiyun 		 * blocked by synchronization, bfqq has a long think
1922*4882a593Smuzhiyun 		 * time. This implies that bfqq's inject limit is at
1923*4882a593Smuzhiyun 		 * least equal to 1 (see the comments in
1924*4882a593Smuzhiyun 		 * bfq_update_inject_limit()). So, thanks to
1925*4882a593Smuzhiyun 		 * injection, the waker queue is likely to be served
1926*4882a593Smuzhiyun 		 * during the very first I/O-plugging time interval
1927*4882a593Smuzhiyun 		 * for bfqq. This triggers the first step of the
1928*4882a593Smuzhiyun 		 * detection mechanism. Thanks again to injection, the
1929*4882a593Smuzhiyun 		 * candidate waker queue is then likely to be
1930*4882a593Smuzhiyun 		 * confirmed no later than during the next
1931*4882a593Smuzhiyun 		 * I/O-plugging interval for bfqq.
1932*4882a593Smuzhiyun 		 */
1933*4882a593Smuzhiyun 		if (bfqd->last_completed_rq_bfqq &&
1934*4882a593Smuzhiyun 		    !bfq_bfqq_has_short_ttime(bfqq) &&
1935*4882a593Smuzhiyun 		    ktime_get_ns() - bfqd->last_completion <
1936*4882a593Smuzhiyun 		    200 * NSEC_PER_USEC) {
1937*4882a593Smuzhiyun 			if (bfqd->last_completed_rq_bfqq != bfqq &&
1938*4882a593Smuzhiyun 			    bfqd->last_completed_rq_bfqq !=
1939*4882a593Smuzhiyun 			    bfqq->waker_bfqq) {
1940*4882a593Smuzhiyun 				/*
1941*4882a593Smuzhiyun 				 * First synchronization detected with
1942*4882a593Smuzhiyun 				 * a candidate waker queue, or with a
1943*4882a593Smuzhiyun 				 * different candidate waker queue
1944*4882a593Smuzhiyun 				 * from the current one.
1945*4882a593Smuzhiyun 				 */
1946*4882a593Smuzhiyun 				bfqq->waker_bfqq = bfqd->last_completed_rq_bfqq;
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 				/*
1949*4882a593Smuzhiyun 				 * If the waker queue disappears, then
1950*4882a593Smuzhiyun 				 * bfqq->waker_bfqq must be reset. To
1951*4882a593Smuzhiyun 				 * this goal, we maintain in each
1952*4882a593Smuzhiyun 				 * waker queue a list, woken_list, of
1953*4882a593Smuzhiyun 				 * all the queues that reference the
1954*4882a593Smuzhiyun 				 * waker queue through their
1955*4882a593Smuzhiyun 				 * waker_bfqq pointer. When the waker
1956*4882a593Smuzhiyun 				 * queue exits, the waker_bfqq pointer
1957*4882a593Smuzhiyun 				 * of all the queues in the woken_list
1958*4882a593Smuzhiyun 				 * is reset.
1959*4882a593Smuzhiyun 				 *
1960*4882a593Smuzhiyun 				 * In addition, if bfqq is already in
1961*4882a593Smuzhiyun 				 * the woken_list of a waker queue,
1962*4882a593Smuzhiyun 				 * then, before being inserted into
1963*4882a593Smuzhiyun 				 * the woken_list of a new waker
1964*4882a593Smuzhiyun 				 * queue, bfqq must be removed from
1965*4882a593Smuzhiyun 				 * the woken_list of the old waker
1966*4882a593Smuzhiyun 				 * queue.
1967*4882a593Smuzhiyun 				 */
1968*4882a593Smuzhiyun 				if (!hlist_unhashed(&bfqq->woken_list_node))
1969*4882a593Smuzhiyun 					hlist_del_init(&bfqq->woken_list_node);
1970*4882a593Smuzhiyun 				hlist_add_head(&bfqq->woken_list_node,
1971*4882a593Smuzhiyun 				    &bfqd->last_completed_rq_bfqq->woken_list);
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 				bfq_clear_bfqq_has_waker(bfqq);
1974*4882a593Smuzhiyun 			} else if (bfqd->last_completed_rq_bfqq ==
1975*4882a593Smuzhiyun 				   bfqq->waker_bfqq &&
1976*4882a593Smuzhiyun 				   !bfq_bfqq_has_waker(bfqq)) {
1977*4882a593Smuzhiyun 				/*
1978*4882a593Smuzhiyun 				 * synchronization with waker_bfqq
1979*4882a593Smuzhiyun 				 * seen for the second time
1980*4882a593Smuzhiyun 				 */
1981*4882a593Smuzhiyun 				bfq_mark_bfqq_has_waker(bfqq);
1982*4882a593Smuzhiyun 			}
1983*4882a593Smuzhiyun 		}
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 		/*
1986*4882a593Smuzhiyun 		 * Periodically reset inject limit, to make sure that
1987*4882a593Smuzhiyun 		 * the latter eventually drops in case workload
1988*4882a593Smuzhiyun 		 * changes, see step (3) in the comments on
1989*4882a593Smuzhiyun 		 * bfq_update_inject_limit().
1990*4882a593Smuzhiyun 		 */
1991*4882a593Smuzhiyun 		if (time_is_before_eq_jiffies(bfqq->decrease_time_jif +
1992*4882a593Smuzhiyun 					     msecs_to_jiffies(1000)))
1993*4882a593Smuzhiyun 			bfq_reset_inject_limit(bfqd, bfqq);
1994*4882a593Smuzhiyun 
1995*4882a593Smuzhiyun 		/*
1996*4882a593Smuzhiyun 		 * The following conditions must hold to setup a new
1997*4882a593Smuzhiyun 		 * sampling of total service time, and then a new
1998*4882a593Smuzhiyun 		 * update of the inject limit:
1999*4882a593Smuzhiyun 		 * - bfqq is in service, because the total service
2000*4882a593Smuzhiyun 		 *   time is evaluated only for the I/O requests of
2001*4882a593Smuzhiyun 		 *   the queues in service;
2002*4882a593Smuzhiyun 		 * - this is the right occasion to compute or to
2003*4882a593Smuzhiyun 		 *   lower the baseline total service time, because
2004*4882a593Smuzhiyun 		 *   there are actually no requests in the drive,
2005*4882a593Smuzhiyun 		 *   or
2006*4882a593Smuzhiyun 		 *   the baseline total service time is available, and
2007*4882a593Smuzhiyun 		 *   this is the right occasion to compute the other
2008*4882a593Smuzhiyun 		 *   quantity needed to update the inject limit, i.e.,
2009*4882a593Smuzhiyun 		 *   the total service time caused by the amount of
2010*4882a593Smuzhiyun 		 *   injection allowed by the current value of the
2011*4882a593Smuzhiyun 		 *   limit. It is the right occasion because injection
2012*4882a593Smuzhiyun 		 *   has actually been performed during the service
2013*4882a593Smuzhiyun 		 *   hole, and there are still in-flight requests,
2014*4882a593Smuzhiyun 		 *   which are very likely to be exactly the injected
2015*4882a593Smuzhiyun 		 *   requests, or part of them;
2016*4882a593Smuzhiyun 		 * - the minimum interval for sampling the total
2017*4882a593Smuzhiyun 		 *   service time and updating the inject limit has
2018*4882a593Smuzhiyun 		 *   elapsed.
2019*4882a593Smuzhiyun 		 */
2020*4882a593Smuzhiyun 		if (bfqq == bfqd->in_service_queue &&
2021*4882a593Smuzhiyun 		    (bfqd->rq_in_driver == 0 ||
2022*4882a593Smuzhiyun 		     (bfqq->last_serv_time_ns > 0 &&
2023*4882a593Smuzhiyun 		      bfqd->rqs_injected && bfqd->rq_in_driver > 0)) &&
2024*4882a593Smuzhiyun 		    time_is_before_eq_jiffies(bfqq->decrease_time_jif +
2025*4882a593Smuzhiyun 					      msecs_to_jiffies(10))) {
2026*4882a593Smuzhiyun 			bfqd->last_empty_occupied_ns = ktime_get_ns();
2027*4882a593Smuzhiyun 			/*
2028*4882a593Smuzhiyun 			 * Start the state machine for measuring the
2029*4882a593Smuzhiyun 			 * total service time of rq: setting
2030*4882a593Smuzhiyun 			 * wait_dispatch will cause bfqd->waited_rq to
2031*4882a593Smuzhiyun 			 * be set when rq will be dispatched.
2032*4882a593Smuzhiyun 			 */
2033*4882a593Smuzhiyun 			bfqd->wait_dispatch = true;
2034*4882a593Smuzhiyun 			/*
2035*4882a593Smuzhiyun 			 * If there is no I/O in service in the drive,
2036*4882a593Smuzhiyun 			 * then possible injection occurred before the
2037*4882a593Smuzhiyun 			 * arrival of rq will not affect the total
2038*4882a593Smuzhiyun 			 * service time of rq. So the injection limit
2039*4882a593Smuzhiyun 			 * must not be updated as a function of such
2040*4882a593Smuzhiyun 			 * total service time, unless new injection
2041*4882a593Smuzhiyun 			 * occurs before rq is completed. To have the
2042*4882a593Smuzhiyun 			 * injection limit updated only in the latter
2043*4882a593Smuzhiyun 			 * case, reset rqs_injected here (rqs_injected
2044*4882a593Smuzhiyun 			 * will be set in case injection is performed
2045*4882a593Smuzhiyun 			 * on bfqq before rq is completed).
2046*4882a593Smuzhiyun 			 */
2047*4882a593Smuzhiyun 			if (bfqd->rq_in_driver == 0)
2048*4882a593Smuzhiyun 				bfqd->rqs_injected = false;
2049*4882a593Smuzhiyun 		}
2050*4882a593Smuzhiyun 	}
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	elv_rb_add(&bfqq->sort_list, rq);
2053*4882a593Smuzhiyun 
2054*4882a593Smuzhiyun 	/*
2055*4882a593Smuzhiyun 	 * Check if this request is a better next-serve candidate.
2056*4882a593Smuzhiyun 	 */
2057*4882a593Smuzhiyun 	prev = bfqq->next_rq;
2058*4882a593Smuzhiyun 	next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
2059*4882a593Smuzhiyun 	bfqq->next_rq = next_rq;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	/*
2062*4882a593Smuzhiyun 	 * Adjust priority tree position, if next_rq changes.
2063*4882a593Smuzhiyun 	 * See comments on bfq_pos_tree_add_move() for the unlikely().
2064*4882a593Smuzhiyun 	 */
2065*4882a593Smuzhiyun 	if (unlikely(!bfqd->nonrot_with_queueing && prev != bfqq->next_rq))
2066*4882a593Smuzhiyun 		bfq_pos_tree_add_move(bfqd, bfqq);
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 	if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */
2069*4882a593Smuzhiyun 		bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff,
2070*4882a593Smuzhiyun 						 rq, &interactive);
2071*4882a593Smuzhiyun 	else {
2072*4882a593Smuzhiyun 		if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
2073*4882a593Smuzhiyun 		    time_is_before_jiffies(
2074*4882a593Smuzhiyun 				bfqq->last_wr_start_finish +
2075*4882a593Smuzhiyun 				bfqd->bfq_wr_min_inter_arr_async)) {
2076*4882a593Smuzhiyun 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
2077*4882a593Smuzhiyun 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 			bfqd->wr_busy_queues++;
2080*4882a593Smuzhiyun 			bfqq->entity.prio_changed = 1;
2081*4882a593Smuzhiyun 		}
2082*4882a593Smuzhiyun 		if (prev != bfqq->next_rq)
2083*4882a593Smuzhiyun 			bfq_updated_next_req(bfqd, bfqq);
2084*4882a593Smuzhiyun 	}
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	/*
2087*4882a593Smuzhiyun 	 * Assign jiffies to last_wr_start_finish in the following
2088*4882a593Smuzhiyun 	 * cases:
2089*4882a593Smuzhiyun 	 *
2090*4882a593Smuzhiyun 	 * . if bfqq is not going to be weight-raised, because, for
2091*4882a593Smuzhiyun 	 *   non weight-raised queues, last_wr_start_finish stores the
2092*4882a593Smuzhiyun 	 *   arrival time of the last request; as of now, this piece
2093*4882a593Smuzhiyun 	 *   of information is used only for deciding whether to
2094*4882a593Smuzhiyun 	 *   weight-raise async queues
2095*4882a593Smuzhiyun 	 *
2096*4882a593Smuzhiyun 	 * . if bfqq is not weight-raised, because, if bfqq is now
2097*4882a593Smuzhiyun 	 *   switching to weight-raised, then last_wr_start_finish
2098*4882a593Smuzhiyun 	 *   stores the time when weight-raising starts
2099*4882a593Smuzhiyun 	 *
2100*4882a593Smuzhiyun 	 * . if bfqq is interactive, because, regardless of whether
2101*4882a593Smuzhiyun 	 *   bfqq is currently weight-raised, the weight-raising
2102*4882a593Smuzhiyun 	 *   period must start or restart (this case is considered
2103*4882a593Smuzhiyun 	 *   separately because it is not detected by the above
2104*4882a593Smuzhiyun 	 *   conditions, if bfqq is already weight-raised)
2105*4882a593Smuzhiyun 	 *
2106*4882a593Smuzhiyun 	 * last_wr_start_finish has to be updated also if bfqq is soft
2107*4882a593Smuzhiyun 	 * real-time, because the weight-raising period is constantly
2108*4882a593Smuzhiyun 	 * restarted on idle-to-busy transitions for these queues, but
2109*4882a593Smuzhiyun 	 * this is already done in bfq_bfqq_handle_idle_busy_switch if
2110*4882a593Smuzhiyun 	 * needed.
2111*4882a593Smuzhiyun 	 */
2112*4882a593Smuzhiyun 	if (bfqd->low_latency &&
2113*4882a593Smuzhiyun 		(old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
2114*4882a593Smuzhiyun 		bfqq->last_wr_start_finish = jiffies;
2115*4882a593Smuzhiyun }
2116*4882a593Smuzhiyun 
bfq_find_rq_fmerge(struct bfq_data * bfqd,struct bio * bio,struct request_queue * q)2117*4882a593Smuzhiyun static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
2118*4882a593Smuzhiyun 					  struct bio *bio,
2119*4882a593Smuzhiyun 					  struct request_queue *q)
2120*4882a593Smuzhiyun {
2121*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfqd->bio_bfqq;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	if (bfqq)
2125*4882a593Smuzhiyun 		return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio));
2126*4882a593Smuzhiyun 
2127*4882a593Smuzhiyun 	return NULL;
2128*4882a593Smuzhiyun }
2129*4882a593Smuzhiyun 
get_sdist(sector_t last_pos,struct request * rq)2130*4882a593Smuzhiyun static sector_t get_sdist(sector_t last_pos, struct request *rq)
2131*4882a593Smuzhiyun {
2132*4882a593Smuzhiyun 	if (last_pos)
2133*4882a593Smuzhiyun 		return abs(blk_rq_pos(rq) - last_pos);
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun 	return 0;
2136*4882a593Smuzhiyun }
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun #if 0 /* Still not clear if we can do without next two functions */
2139*4882a593Smuzhiyun static void bfq_activate_request(struct request_queue *q, struct request *rq)
2140*4882a593Smuzhiyun {
2141*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 	bfqd->rq_in_driver++;
2144*4882a593Smuzhiyun }
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun static void bfq_deactivate_request(struct request_queue *q, struct request *rq)
2147*4882a593Smuzhiyun {
2148*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	bfqd->rq_in_driver--;
2151*4882a593Smuzhiyun }
2152*4882a593Smuzhiyun #endif
2153*4882a593Smuzhiyun 
bfq_remove_request(struct request_queue * q,struct request * rq)2154*4882a593Smuzhiyun static void bfq_remove_request(struct request_queue *q,
2155*4882a593Smuzhiyun 			       struct request *rq)
2156*4882a593Smuzhiyun {
2157*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
2158*4882a593Smuzhiyun 	struct bfq_data *bfqd = bfqq->bfqd;
2159*4882a593Smuzhiyun 	const int sync = rq_is_sync(rq);
2160*4882a593Smuzhiyun 
2161*4882a593Smuzhiyun 	if (bfqq->next_rq == rq) {
2162*4882a593Smuzhiyun 		bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
2163*4882a593Smuzhiyun 		bfq_updated_next_req(bfqd, bfqq);
2164*4882a593Smuzhiyun 	}
2165*4882a593Smuzhiyun 
2166*4882a593Smuzhiyun 	if (rq->queuelist.prev != &rq->queuelist)
2167*4882a593Smuzhiyun 		list_del_init(&rq->queuelist);
2168*4882a593Smuzhiyun 	bfqq->queued[sync]--;
2169*4882a593Smuzhiyun 	bfqd->queued--;
2170*4882a593Smuzhiyun 	elv_rb_del(&bfqq->sort_list, rq);
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun 	elv_rqhash_del(q, rq);
2173*4882a593Smuzhiyun 	if (q->last_merge == rq)
2174*4882a593Smuzhiyun 		q->last_merge = NULL;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
2177*4882a593Smuzhiyun 		bfqq->next_rq = NULL;
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun 		if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) {
2180*4882a593Smuzhiyun 			bfq_del_bfqq_busy(bfqd, bfqq, false);
2181*4882a593Smuzhiyun 			/*
2182*4882a593Smuzhiyun 			 * bfqq emptied. In normal operation, when
2183*4882a593Smuzhiyun 			 * bfqq is empty, bfqq->entity.service and
2184*4882a593Smuzhiyun 			 * bfqq->entity.budget must contain,
2185*4882a593Smuzhiyun 			 * respectively, the service received and the
2186*4882a593Smuzhiyun 			 * budget used last time bfqq emptied. These
2187*4882a593Smuzhiyun 			 * facts do not hold in this case, as at least
2188*4882a593Smuzhiyun 			 * this last removal occurred while bfqq is
2189*4882a593Smuzhiyun 			 * not in service. To avoid inconsistencies,
2190*4882a593Smuzhiyun 			 * reset both bfqq->entity.service and
2191*4882a593Smuzhiyun 			 * bfqq->entity.budget, if bfqq has still a
2192*4882a593Smuzhiyun 			 * process that may issue I/O requests to it.
2193*4882a593Smuzhiyun 			 */
2194*4882a593Smuzhiyun 			bfqq->entity.budget = bfqq->entity.service = 0;
2195*4882a593Smuzhiyun 		}
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 		/*
2198*4882a593Smuzhiyun 		 * Remove queue from request-position tree as it is empty.
2199*4882a593Smuzhiyun 		 */
2200*4882a593Smuzhiyun 		if (bfqq->pos_root) {
2201*4882a593Smuzhiyun 			rb_erase(&bfqq->pos_node, bfqq->pos_root);
2202*4882a593Smuzhiyun 			bfqq->pos_root = NULL;
2203*4882a593Smuzhiyun 		}
2204*4882a593Smuzhiyun 	} else {
2205*4882a593Smuzhiyun 		/* see comments on bfq_pos_tree_add_move() for the unlikely() */
2206*4882a593Smuzhiyun 		if (unlikely(!bfqd->nonrot_with_queueing))
2207*4882a593Smuzhiyun 			bfq_pos_tree_add_move(bfqd, bfqq);
2208*4882a593Smuzhiyun 	}
2209*4882a593Smuzhiyun 
2210*4882a593Smuzhiyun 	if (rq->cmd_flags & REQ_META)
2211*4882a593Smuzhiyun 		bfqq->meta_pending--;
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun 
bfq_bio_merge(struct request_queue * q,struct bio * bio,unsigned int nr_segs)2215*4882a593Smuzhiyun static bool bfq_bio_merge(struct request_queue *q, struct bio *bio,
2216*4882a593Smuzhiyun 		unsigned int nr_segs)
2217*4882a593Smuzhiyun {
2218*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
2219*4882a593Smuzhiyun 	struct request *free = NULL;
2220*4882a593Smuzhiyun 	/*
2221*4882a593Smuzhiyun 	 * bfq_bic_lookup grabs the queue_lock: invoke it now and
2222*4882a593Smuzhiyun 	 * store its return value for later use, to avoid nesting
2223*4882a593Smuzhiyun 	 * queue_lock inside the bfqd->lock. We assume that the bic
2224*4882a593Smuzhiyun 	 * returned by bfq_bic_lookup does not go away before
2225*4882a593Smuzhiyun 	 * bfqd->lock is taken.
2226*4882a593Smuzhiyun 	 */
2227*4882a593Smuzhiyun 	struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q);
2228*4882a593Smuzhiyun 	bool ret;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun 	if (bic) {
2233*4882a593Smuzhiyun 		/*
2234*4882a593Smuzhiyun 		 * Make sure cgroup info is uptodate for current process before
2235*4882a593Smuzhiyun 		 * considering the merge.
2236*4882a593Smuzhiyun 		 */
2237*4882a593Smuzhiyun 		bfq_bic_update_cgroup(bic, bio);
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 		bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
2240*4882a593Smuzhiyun 	} else {
2241*4882a593Smuzhiyun 		bfqd->bio_bfqq = NULL;
2242*4882a593Smuzhiyun 	}
2243*4882a593Smuzhiyun 	bfqd->bio_bic = bic;
2244*4882a593Smuzhiyun 
2245*4882a593Smuzhiyun 	ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun 	if (free)
2248*4882a593Smuzhiyun 		blk_mq_free_request(free);
2249*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 	return ret;
2252*4882a593Smuzhiyun }
2253*4882a593Smuzhiyun 
bfq_request_merge(struct request_queue * q,struct request ** req,struct bio * bio)2254*4882a593Smuzhiyun static int bfq_request_merge(struct request_queue *q, struct request **req,
2255*4882a593Smuzhiyun 			     struct bio *bio)
2256*4882a593Smuzhiyun {
2257*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
2258*4882a593Smuzhiyun 	struct request *__rq;
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 	__rq = bfq_find_rq_fmerge(bfqd, bio, q);
2261*4882a593Smuzhiyun 	if (__rq && elv_bio_merge_ok(__rq, bio)) {
2262*4882a593Smuzhiyun 		*req = __rq;
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 		if (blk_discard_mergable(__rq))
2265*4882a593Smuzhiyun 			return ELEVATOR_DISCARD_MERGE;
2266*4882a593Smuzhiyun 		return ELEVATOR_FRONT_MERGE;
2267*4882a593Smuzhiyun 	}
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 	return ELEVATOR_NO_MERGE;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun 
bfq_request_merged(struct request_queue * q,struct request * req,enum elv_merge type)2272*4882a593Smuzhiyun static void bfq_request_merged(struct request_queue *q, struct request *req,
2273*4882a593Smuzhiyun 			       enum elv_merge type)
2274*4882a593Smuzhiyun {
2275*4882a593Smuzhiyun 	if (type == ELEVATOR_FRONT_MERGE &&
2276*4882a593Smuzhiyun 	    rb_prev(&req->rb_node) &&
2277*4882a593Smuzhiyun 	    blk_rq_pos(req) <
2278*4882a593Smuzhiyun 	    blk_rq_pos(container_of(rb_prev(&req->rb_node),
2279*4882a593Smuzhiyun 				    struct request, rb_node))) {
2280*4882a593Smuzhiyun 		struct bfq_queue *bfqq = RQ_BFQQ(req);
2281*4882a593Smuzhiyun 		struct bfq_data *bfqd;
2282*4882a593Smuzhiyun 		struct request *prev, *next_rq;
2283*4882a593Smuzhiyun 
2284*4882a593Smuzhiyun 		if (!bfqq)
2285*4882a593Smuzhiyun 			return;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 		bfqd = bfqq->bfqd;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 		/* Reposition request in its sort_list */
2290*4882a593Smuzhiyun 		elv_rb_del(&bfqq->sort_list, req);
2291*4882a593Smuzhiyun 		elv_rb_add(&bfqq->sort_list, req);
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 		/* Choose next request to be served for bfqq */
2294*4882a593Smuzhiyun 		prev = bfqq->next_rq;
2295*4882a593Smuzhiyun 		next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
2296*4882a593Smuzhiyun 					 bfqd->last_position);
2297*4882a593Smuzhiyun 		bfqq->next_rq = next_rq;
2298*4882a593Smuzhiyun 		/*
2299*4882a593Smuzhiyun 		 * If next_rq changes, update both the queue's budget to
2300*4882a593Smuzhiyun 		 * fit the new request and the queue's position in its
2301*4882a593Smuzhiyun 		 * rq_pos_tree.
2302*4882a593Smuzhiyun 		 */
2303*4882a593Smuzhiyun 		if (prev != bfqq->next_rq) {
2304*4882a593Smuzhiyun 			bfq_updated_next_req(bfqd, bfqq);
2305*4882a593Smuzhiyun 			/*
2306*4882a593Smuzhiyun 			 * See comments on bfq_pos_tree_add_move() for
2307*4882a593Smuzhiyun 			 * the unlikely().
2308*4882a593Smuzhiyun 			 */
2309*4882a593Smuzhiyun 			if (unlikely(!bfqd->nonrot_with_queueing))
2310*4882a593Smuzhiyun 				bfq_pos_tree_add_move(bfqd, bfqq);
2311*4882a593Smuzhiyun 		}
2312*4882a593Smuzhiyun 	}
2313*4882a593Smuzhiyun }
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun /*
2316*4882a593Smuzhiyun  * This function is called to notify the scheduler that the requests
2317*4882a593Smuzhiyun  * rq and 'next' have been merged, with 'next' going away.  BFQ
2318*4882a593Smuzhiyun  * exploits this hook to address the following issue: if 'next' has a
2319*4882a593Smuzhiyun  * fifo_time lower that rq, then the fifo_time of rq must be set to
2320*4882a593Smuzhiyun  * the value of 'next', to not forget the greater age of 'next'.
2321*4882a593Smuzhiyun  *
2322*4882a593Smuzhiyun  * NOTE: in this function we assume that rq is in a bfq_queue, basing
2323*4882a593Smuzhiyun  * on that rq is picked from the hash table q->elevator->hash, which,
2324*4882a593Smuzhiyun  * in its turn, is filled only with I/O requests present in
2325*4882a593Smuzhiyun  * bfq_queues, while BFQ is in use for the request queue q. In fact,
2326*4882a593Smuzhiyun  * the function that fills this hash table (elv_rqhash_add) is called
2327*4882a593Smuzhiyun  * only by bfq_insert_request.
2328*4882a593Smuzhiyun  */
bfq_requests_merged(struct request_queue * q,struct request * rq,struct request * next)2329*4882a593Smuzhiyun static void bfq_requests_merged(struct request_queue *q, struct request *rq,
2330*4882a593Smuzhiyun 				struct request *next)
2331*4882a593Smuzhiyun {
2332*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq),
2333*4882a593Smuzhiyun 		*next_bfqq = RQ_BFQQ(next);
2334*4882a593Smuzhiyun 
2335*4882a593Smuzhiyun 	if (!bfqq)
2336*4882a593Smuzhiyun 		return;
2337*4882a593Smuzhiyun 
2338*4882a593Smuzhiyun 	/*
2339*4882a593Smuzhiyun 	 * If next and rq belong to the same bfq_queue and next is older
2340*4882a593Smuzhiyun 	 * than rq, then reposition rq in the fifo (by substituting next
2341*4882a593Smuzhiyun 	 * with rq). Otherwise, if next and rq belong to different
2342*4882a593Smuzhiyun 	 * bfq_queues, never reposition rq: in fact, we would have to
2343*4882a593Smuzhiyun 	 * reposition it with respect to next's position in its own fifo,
2344*4882a593Smuzhiyun 	 * which would most certainly be too expensive with respect to
2345*4882a593Smuzhiyun 	 * the benefits.
2346*4882a593Smuzhiyun 	 */
2347*4882a593Smuzhiyun 	if (bfqq == next_bfqq &&
2348*4882a593Smuzhiyun 	    !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
2349*4882a593Smuzhiyun 	    next->fifo_time < rq->fifo_time) {
2350*4882a593Smuzhiyun 		list_del_init(&rq->queuelist);
2351*4882a593Smuzhiyun 		list_replace_init(&next->queuelist, &rq->queuelist);
2352*4882a593Smuzhiyun 		rq->fifo_time = next->fifo_time;
2353*4882a593Smuzhiyun 	}
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	if (bfqq->next_rq == next)
2356*4882a593Smuzhiyun 		bfqq->next_rq = rq;
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 	bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
2359*4882a593Smuzhiyun }
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun /* Must be called with bfqq != NULL */
bfq_bfqq_end_wr(struct bfq_queue * bfqq)2362*4882a593Smuzhiyun static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
2363*4882a593Smuzhiyun {
2364*4882a593Smuzhiyun 	if (bfq_bfqq_busy(bfqq))
2365*4882a593Smuzhiyun 		bfqq->bfqd->wr_busy_queues--;
2366*4882a593Smuzhiyun 	bfqq->wr_coeff = 1;
2367*4882a593Smuzhiyun 	bfqq->wr_cur_max_time = 0;
2368*4882a593Smuzhiyun 	bfqq->last_wr_start_finish = jiffies;
2369*4882a593Smuzhiyun 	/*
2370*4882a593Smuzhiyun 	 * Trigger a weight change on the next invocation of
2371*4882a593Smuzhiyun 	 * __bfq_entity_update_weight_prio.
2372*4882a593Smuzhiyun 	 */
2373*4882a593Smuzhiyun 	bfqq->entity.prio_changed = 1;
2374*4882a593Smuzhiyun }
2375*4882a593Smuzhiyun 
bfq_end_wr_async_queues(struct bfq_data * bfqd,struct bfq_group * bfqg)2376*4882a593Smuzhiyun void bfq_end_wr_async_queues(struct bfq_data *bfqd,
2377*4882a593Smuzhiyun 			     struct bfq_group *bfqg)
2378*4882a593Smuzhiyun {
2379*4882a593Smuzhiyun 	int i, j;
2380*4882a593Smuzhiyun 
2381*4882a593Smuzhiyun 	for (i = 0; i < 2; i++)
2382*4882a593Smuzhiyun 		for (j = 0; j < IOPRIO_BE_NR; j++)
2383*4882a593Smuzhiyun 			if (bfqg->async_bfqq[i][j])
2384*4882a593Smuzhiyun 				bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
2385*4882a593Smuzhiyun 	if (bfqg->async_idle_bfqq)
2386*4882a593Smuzhiyun 		bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
2387*4882a593Smuzhiyun }
2388*4882a593Smuzhiyun 
bfq_end_wr(struct bfq_data * bfqd)2389*4882a593Smuzhiyun static void bfq_end_wr(struct bfq_data *bfqd)
2390*4882a593Smuzhiyun {
2391*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 	list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
2396*4882a593Smuzhiyun 		bfq_bfqq_end_wr(bfqq);
2397*4882a593Smuzhiyun 	list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
2398*4882a593Smuzhiyun 		bfq_bfqq_end_wr(bfqq);
2399*4882a593Smuzhiyun 	bfq_end_wr_async(bfqd);
2400*4882a593Smuzhiyun 
2401*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
2402*4882a593Smuzhiyun }
2403*4882a593Smuzhiyun 
bfq_io_struct_pos(void * io_struct,bool request)2404*4882a593Smuzhiyun static sector_t bfq_io_struct_pos(void *io_struct, bool request)
2405*4882a593Smuzhiyun {
2406*4882a593Smuzhiyun 	if (request)
2407*4882a593Smuzhiyun 		return blk_rq_pos(io_struct);
2408*4882a593Smuzhiyun 	else
2409*4882a593Smuzhiyun 		return ((struct bio *)io_struct)->bi_iter.bi_sector;
2410*4882a593Smuzhiyun }
2411*4882a593Smuzhiyun 
bfq_rq_close_to_sector(void * io_struct,bool request,sector_t sector)2412*4882a593Smuzhiyun static int bfq_rq_close_to_sector(void *io_struct, bool request,
2413*4882a593Smuzhiyun 				  sector_t sector)
2414*4882a593Smuzhiyun {
2415*4882a593Smuzhiyun 	return abs(bfq_io_struct_pos(io_struct, request) - sector) <=
2416*4882a593Smuzhiyun 	       BFQQ_CLOSE_THR;
2417*4882a593Smuzhiyun }
2418*4882a593Smuzhiyun 
bfqq_find_close(struct bfq_data * bfqd,struct bfq_queue * bfqq,sector_t sector)2419*4882a593Smuzhiyun static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd,
2420*4882a593Smuzhiyun 					 struct bfq_queue *bfqq,
2421*4882a593Smuzhiyun 					 sector_t sector)
2422*4882a593Smuzhiyun {
2423*4882a593Smuzhiyun 	struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
2424*4882a593Smuzhiyun 	struct rb_node *parent, *node;
2425*4882a593Smuzhiyun 	struct bfq_queue *__bfqq;
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(root))
2428*4882a593Smuzhiyun 		return NULL;
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun 	/*
2431*4882a593Smuzhiyun 	 * First, if we find a request starting at the end of the last
2432*4882a593Smuzhiyun 	 * request, choose it.
2433*4882a593Smuzhiyun 	 */
2434*4882a593Smuzhiyun 	__bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
2435*4882a593Smuzhiyun 	if (__bfqq)
2436*4882a593Smuzhiyun 		return __bfqq;
2437*4882a593Smuzhiyun 
2438*4882a593Smuzhiyun 	/*
2439*4882a593Smuzhiyun 	 * If the exact sector wasn't found, the parent of the NULL leaf
2440*4882a593Smuzhiyun 	 * will contain the closest sector (rq_pos_tree sorted by
2441*4882a593Smuzhiyun 	 * next_request position).
2442*4882a593Smuzhiyun 	 */
2443*4882a593Smuzhiyun 	__bfqq = rb_entry(parent, struct bfq_queue, pos_node);
2444*4882a593Smuzhiyun 	if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2445*4882a593Smuzhiyun 		return __bfqq;
2446*4882a593Smuzhiyun 
2447*4882a593Smuzhiyun 	if (blk_rq_pos(__bfqq->next_rq) < sector)
2448*4882a593Smuzhiyun 		node = rb_next(&__bfqq->pos_node);
2449*4882a593Smuzhiyun 	else
2450*4882a593Smuzhiyun 		node = rb_prev(&__bfqq->pos_node);
2451*4882a593Smuzhiyun 	if (!node)
2452*4882a593Smuzhiyun 		return NULL;
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun 	__bfqq = rb_entry(node, struct bfq_queue, pos_node);
2455*4882a593Smuzhiyun 	if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2456*4882a593Smuzhiyun 		return __bfqq;
2457*4882a593Smuzhiyun 
2458*4882a593Smuzhiyun 	return NULL;
2459*4882a593Smuzhiyun }
2460*4882a593Smuzhiyun 
bfq_find_close_cooperator(struct bfq_data * bfqd,struct bfq_queue * cur_bfqq,sector_t sector)2461*4882a593Smuzhiyun static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd,
2462*4882a593Smuzhiyun 						   struct bfq_queue *cur_bfqq,
2463*4882a593Smuzhiyun 						   sector_t sector)
2464*4882a593Smuzhiyun {
2465*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	/*
2468*4882a593Smuzhiyun 	 * We shall notice if some of the queues are cooperating,
2469*4882a593Smuzhiyun 	 * e.g., working closely on the same area of the device. In
2470*4882a593Smuzhiyun 	 * that case, we can group them together and: 1) don't waste
2471*4882a593Smuzhiyun 	 * time idling, and 2) serve the union of their requests in
2472*4882a593Smuzhiyun 	 * the best possible order for throughput.
2473*4882a593Smuzhiyun 	 */
2474*4882a593Smuzhiyun 	bfqq = bfqq_find_close(bfqd, cur_bfqq, sector);
2475*4882a593Smuzhiyun 	if (!bfqq || bfqq == cur_bfqq)
2476*4882a593Smuzhiyun 		return NULL;
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun 	return bfqq;
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun static struct bfq_queue *
bfq_setup_merge(struct bfq_queue * bfqq,struct bfq_queue * new_bfqq)2482*4882a593Smuzhiyun bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2483*4882a593Smuzhiyun {
2484*4882a593Smuzhiyun 	int process_refs, new_process_refs;
2485*4882a593Smuzhiyun 	struct bfq_queue *__bfqq;
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun 	/*
2488*4882a593Smuzhiyun 	 * If there are no process references on the new_bfqq, then it is
2489*4882a593Smuzhiyun 	 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
2490*4882a593Smuzhiyun 	 * may have dropped their last reference (not just their last process
2491*4882a593Smuzhiyun 	 * reference).
2492*4882a593Smuzhiyun 	 */
2493*4882a593Smuzhiyun 	if (!bfqq_process_refs(new_bfqq))
2494*4882a593Smuzhiyun 		return NULL;
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun 	/* Avoid a circular list and skip interim queue merges. */
2497*4882a593Smuzhiyun 	while ((__bfqq = new_bfqq->new_bfqq)) {
2498*4882a593Smuzhiyun 		if (__bfqq == bfqq)
2499*4882a593Smuzhiyun 			return NULL;
2500*4882a593Smuzhiyun 		new_bfqq = __bfqq;
2501*4882a593Smuzhiyun 	}
2502*4882a593Smuzhiyun 
2503*4882a593Smuzhiyun 	process_refs = bfqq_process_refs(bfqq);
2504*4882a593Smuzhiyun 	new_process_refs = bfqq_process_refs(new_bfqq);
2505*4882a593Smuzhiyun 	/*
2506*4882a593Smuzhiyun 	 * If the process for the bfqq has gone away, there is no
2507*4882a593Smuzhiyun 	 * sense in merging the queues.
2508*4882a593Smuzhiyun 	 */
2509*4882a593Smuzhiyun 	if (process_refs == 0 || new_process_refs == 0)
2510*4882a593Smuzhiyun 		return NULL;
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun 	/*
2513*4882a593Smuzhiyun 	 * Make sure merged queues belong to the same parent. Parents could
2514*4882a593Smuzhiyun 	 * have changed since the time we decided the two queues are suitable
2515*4882a593Smuzhiyun 	 * for merging.
2516*4882a593Smuzhiyun 	 */
2517*4882a593Smuzhiyun 	if (new_bfqq->entity.parent != bfqq->entity.parent)
2518*4882a593Smuzhiyun 		return NULL;
2519*4882a593Smuzhiyun 
2520*4882a593Smuzhiyun 	bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
2521*4882a593Smuzhiyun 		new_bfqq->pid);
2522*4882a593Smuzhiyun 
2523*4882a593Smuzhiyun 	/*
2524*4882a593Smuzhiyun 	 * Merging is just a redirection: the requests of the process
2525*4882a593Smuzhiyun 	 * owning one of the two queues are redirected to the other queue.
2526*4882a593Smuzhiyun 	 * The latter queue, in its turn, is set as shared if this is the
2527*4882a593Smuzhiyun 	 * first time that the requests of some process are redirected to
2528*4882a593Smuzhiyun 	 * it.
2529*4882a593Smuzhiyun 	 *
2530*4882a593Smuzhiyun 	 * We redirect bfqq to new_bfqq and not the opposite, because
2531*4882a593Smuzhiyun 	 * we are in the context of the process owning bfqq, thus we
2532*4882a593Smuzhiyun 	 * have the io_cq of this process. So we can immediately
2533*4882a593Smuzhiyun 	 * configure this io_cq to redirect the requests of the
2534*4882a593Smuzhiyun 	 * process to new_bfqq. In contrast, the io_cq of new_bfqq is
2535*4882a593Smuzhiyun 	 * not available any more (new_bfqq->bic == NULL).
2536*4882a593Smuzhiyun 	 *
2537*4882a593Smuzhiyun 	 * Anyway, even in case new_bfqq coincides with the in-service
2538*4882a593Smuzhiyun 	 * queue, redirecting requests the in-service queue is the
2539*4882a593Smuzhiyun 	 * best option, as we feed the in-service queue with new
2540*4882a593Smuzhiyun 	 * requests close to the last request served and, by doing so,
2541*4882a593Smuzhiyun 	 * are likely to increase the throughput.
2542*4882a593Smuzhiyun 	 */
2543*4882a593Smuzhiyun 	bfqq->new_bfqq = new_bfqq;
2544*4882a593Smuzhiyun 	/*
2545*4882a593Smuzhiyun 	 * The above assignment schedules the following redirections:
2546*4882a593Smuzhiyun 	 * each time some I/O for bfqq arrives, the process that
2547*4882a593Smuzhiyun 	 * generated that I/O is disassociated from bfqq and
2548*4882a593Smuzhiyun 	 * associated with new_bfqq. Here we increases new_bfqq->ref
2549*4882a593Smuzhiyun 	 * in advance, adding the number of processes that are
2550*4882a593Smuzhiyun 	 * expected to be associated with new_bfqq as they happen to
2551*4882a593Smuzhiyun 	 * issue I/O.
2552*4882a593Smuzhiyun 	 */
2553*4882a593Smuzhiyun 	new_bfqq->ref += process_refs;
2554*4882a593Smuzhiyun 	return new_bfqq;
2555*4882a593Smuzhiyun }
2556*4882a593Smuzhiyun 
bfq_may_be_close_cooperator(struct bfq_queue * bfqq,struct bfq_queue * new_bfqq)2557*4882a593Smuzhiyun static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq,
2558*4882a593Smuzhiyun 					struct bfq_queue *new_bfqq)
2559*4882a593Smuzhiyun {
2560*4882a593Smuzhiyun 	if (bfq_too_late_for_merging(new_bfqq))
2561*4882a593Smuzhiyun 		return false;
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun 	if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) ||
2564*4882a593Smuzhiyun 	    (bfqq->ioprio_class != new_bfqq->ioprio_class))
2565*4882a593Smuzhiyun 		return false;
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	/*
2568*4882a593Smuzhiyun 	 * If either of the queues has already been detected as seeky,
2569*4882a593Smuzhiyun 	 * then merging it with the other queue is unlikely to lead to
2570*4882a593Smuzhiyun 	 * sequential I/O.
2571*4882a593Smuzhiyun 	 */
2572*4882a593Smuzhiyun 	if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq))
2573*4882a593Smuzhiyun 		return false;
2574*4882a593Smuzhiyun 
2575*4882a593Smuzhiyun 	/*
2576*4882a593Smuzhiyun 	 * Interleaved I/O is known to be done by (some) applications
2577*4882a593Smuzhiyun 	 * only for reads, so it does not make sense to merge async
2578*4882a593Smuzhiyun 	 * queues.
2579*4882a593Smuzhiyun 	 */
2580*4882a593Smuzhiyun 	if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq))
2581*4882a593Smuzhiyun 		return false;
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 	return true;
2584*4882a593Smuzhiyun }
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun /*
2587*4882a593Smuzhiyun  * Attempt to schedule a merge of bfqq with the currently in-service
2588*4882a593Smuzhiyun  * queue or with a close queue among the scheduled queues.  Return
2589*4882a593Smuzhiyun  * NULL if no merge was scheduled, a pointer to the shared bfq_queue
2590*4882a593Smuzhiyun  * structure otherwise.
2591*4882a593Smuzhiyun  *
2592*4882a593Smuzhiyun  * The OOM queue is not allowed to participate to cooperation: in fact, since
2593*4882a593Smuzhiyun  * the requests temporarily redirected to the OOM queue could be redirected
2594*4882a593Smuzhiyun  * again to dedicated queues at any time, the state needed to correctly
2595*4882a593Smuzhiyun  * handle merging with the OOM queue would be quite complex and expensive
2596*4882a593Smuzhiyun  * to maintain. Besides, in such a critical condition as an out of memory,
2597*4882a593Smuzhiyun  * the benefits of queue merging may be little relevant, or even negligible.
2598*4882a593Smuzhiyun  *
2599*4882a593Smuzhiyun  * WARNING: queue merging may impair fairness among non-weight raised
2600*4882a593Smuzhiyun  * queues, for at least two reasons: 1) the original weight of a
2601*4882a593Smuzhiyun  * merged queue may change during the merged state, 2) even being the
2602*4882a593Smuzhiyun  * weight the same, a merged queue may be bloated with many more
2603*4882a593Smuzhiyun  * requests than the ones produced by its originally-associated
2604*4882a593Smuzhiyun  * process.
2605*4882a593Smuzhiyun  */
2606*4882a593Smuzhiyun static struct bfq_queue *
bfq_setup_cooperator(struct bfq_data * bfqd,struct bfq_queue * bfqq,void * io_struct,bool request)2607*4882a593Smuzhiyun bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
2608*4882a593Smuzhiyun 		     void *io_struct, bool request)
2609*4882a593Smuzhiyun {
2610*4882a593Smuzhiyun 	struct bfq_queue *in_service_bfqq, *new_bfqq;
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun 	/* if a merge has already been setup, then proceed with that first */
2613*4882a593Smuzhiyun 	if (bfqq->new_bfqq)
2614*4882a593Smuzhiyun 		return bfqq->new_bfqq;
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	/*
2617*4882a593Smuzhiyun 	 * Do not perform queue merging if the device is non
2618*4882a593Smuzhiyun 	 * rotational and performs internal queueing. In fact, such a
2619*4882a593Smuzhiyun 	 * device reaches a high speed through internal parallelism
2620*4882a593Smuzhiyun 	 * and pipelining. This means that, to reach a high
2621*4882a593Smuzhiyun 	 * throughput, it must have many requests enqueued at the same
2622*4882a593Smuzhiyun 	 * time. But, in this configuration, the internal scheduling
2623*4882a593Smuzhiyun 	 * algorithm of the device does exactly the job of queue
2624*4882a593Smuzhiyun 	 * merging: it reorders requests so as to obtain as much as
2625*4882a593Smuzhiyun 	 * possible a sequential I/O pattern. As a consequence, with
2626*4882a593Smuzhiyun 	 * the workload generated by processes doing interleaved I/O,
2627*4882a593Smuzhiyun 	 * the throughput reached by the device is likely to be the
2628*4882a593Smuzhiyun 	 * same, with and without queue merging.
2629*4882a593Smuzhiyun 	 *
2630*4882a593Smuzhiyun 	 * Disabling merging also provides a remarkable benefit in
2631*4882a593Smuzhiyun 	 * terms of throughput. Merging tends to make many workloads
2632*4882a593Smuzhiyun 	 * artificially more uneven, because of shared queues
2633*4882a593Smuzhiyun 	 * remaining non empty for incomparably more time than
2634*4882a593Smuzhiyun 	 * non-merged queues. This may accentuate workload
2635*4882a593Smuzhiyun 	 * asymmetries. For example, if one of the queues in a set of
2636*4882a593Smuzhiyun 	 * merged queues has a higher weight than a normal queue, then
2637*4882a593Smuzhiyun 	 * the shared queue may inherit such a high weight and, by
2638*4882a593Smuzhiyun 	 * staying almost always active, may force BFQ to perform I/O
2639*4882a593Smuzhiyun 	 * plugging most of the time. This evidently makes it harder
2640*4882a593Smuzhiyun 	 * for BFQ to let the device reach a high throughput.
2641*4882a593Smuzhiyun 	 *
2642*4882a593Smuzhiyun 	 * Finally, the likely() macro below is not used because one
2643*4882a593Smuzhiyun 	 * of the two branches is more likely than the other, but to
2644*4882a593Smuzhiyun 	 * have the code path after the following if() executed as
2645*4882a593Smuzhiyun 	 * fast as possible for the case of a non rotational device
2646*4882a593Smuzhiyun 	 * with queueing. We want it because this is the fastest kind
2647*4882a593Smuzhiyun 	 * of device. On the opposite end, the likely() may lengthen
2648*4882a593Smuzhiyun 	 * the execution time of BFQ for the case of slower devices
2649*4882a593Smuzhiyun 	 * (rotational or at least without queueing). But in this case
2650*4882a593Smuzhiyun 	 * the execution time of BFQ matters very little, if not at
2651*4882a593Smuzhiyun 	 * all.
2652*4882a593Smuzhiyun 	 */
2653*4882a593Smuzhiyun 	if (likely(bfqd->nonrot_with_queueing))
2654*4882a593Smuzhiyun 		return NULL;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	/*
2657*4882a593Smuzhiyun 	 * Prevent bfqq from being merged if it has been created too
2658*4882a593Smuzhiyun 	 * long ago. The idea is that true cooperating processes, and
2659*4882a593Smuzhiyun 	 * thus their associated bfq_queues, are supposed to be
2660*4882a593Smuzhiyun 	 * created shortly after each other. This is the case, e.g.,
2661*4882a593Smuzhiyun 	 * for KVM/QEMU and dump I/O threads. Basing on this
2662*4882a593Smuzhiyun 	 * assumption, the following filtering greatly reduces the
2663*4882a593Smuzhiyun 	 * probability that two non-cooperating processes, which just
2664*4882a593Smuzhiyun 	 * happen to do close I/O for some short time interval, have
2665*4882a593Smuzhiyun 	 * their queues merged by mistake.
2666*4882a593Smuzhiyun 	 */
2667*4882a593Smuzhiyun 	if (bfq_too_late_for_merging(bfqq))
2668*4882a593Smuzhiyun 		return NULL;
2669*4882a593Smuzhiyun 
2670*4882a593Smuzhiyun 	if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq))
2671*4882a593Smuzhiyun 		return NULL;
2672*4882a593Smuzhiyun 
2673*4882a593Smuzhiyun 	/* If there is only one backlogged queue, don't search. */
2674*4882a593Smuzhiyun 	if (bfq_tot_busy_queues(bfqd) == 1)
2675*4882a593Smuzhiyun 		return NULL;
2676*4882a593Smuzhiyun 
2677*4882a593Smuzhiyun 	in_service_bfqq = bfqd->in_service_queue;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	if (in_service_bfqq && in_service_bfqq != bfqq &&
2680*4882a593Smuzhiyun 	    likely(in_service_bfqq != &bfqd->oom_bfqq) &&
2681*4882a593Smuzhiyun 	    bfq_rq_close_to_sector(io_struct, request,
2682*4882a593Smuzhiyun 				   bfqd->in_serv_last_pos) &&
2683*4882a593Smuzhiyun 	    bfqq->entity.parent == in_service_bfqq->entity.parent &&
2684*4882a593Smuzhiyun 	    bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) {
2685*4882a593Smuzhiyun 		new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
2686*4882a593Smuzhiyun 		if (new_bfqq)
2687*4882a593Smuzhiyun 			return new_bfqq;
2688*4882a593Smuzhiyun 	}
2689*4882a593Smuzhiyun 	/*
2690*4882a593Smuzhiyun 	 * Check whether there is a cooperator among currently scheduled
2691*4882a593Smuzhiyun 	 * queues. The only thing we need is that the bio/request is not
2692*4882a593Smuzhiyun 	 * NULL, as we need it to establish whether a cooperator exists.
2693*4882a593Smuzhiyun 	 */
2694*4882a593Smuzhiyun 	new_bfqq = bfq_find_close_cooperator(bfqd, bfqq,
2695*4882a593Smuzhiyun 			bfq_io_struct_pos(io_struct, request));
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun 	if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) &&
2698*4882a593Smuzhiyun 	    bfq_may_be_close_cooperator(bfqq, new_bfqq))
2699*4882a593Smuzhiyun 		return bfq_setup_merge(bfqq, new_bfqq);
2700*4882a593Smuzhiyun 
2701*4882a593Smuzhiyun 	return NULL;
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun 
bfq_bfqq_save_state(struct bfq_queue * bfqq)2704*4882a593Smuzhiyun static void bfq_bfqq_save_state(struct bfq_queue *bfqq)
2705*4882a593Smuzhiyun {
2706*4882a593Smuzhiyun 	struct bfq_io_cq *bic = bfqq->bic;
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun 	/*
2709*4882a593Smuzhiyun 	 * If !bfqq->bic, the queue is already shared or its requests
2710*4882a593Smuzhiyun 	 * have already been redirected to a shared queue; both idle window
2711*4882a593Smuzhiyun 	 * and weight raising state have already been saved. Do nothing.
2712*4882a593Smuzhiyun 	 */
2713*4882a593Smuzhiyun 	if (!bic)
2714*4882a593Smuzhiyun 		return;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 	bic->saved_weight = bfqq->entity.orig_weight;
2717*4882a593Smuzhiyun 	bic->saved_ttime = bfqq->ttime;
2718*4882a593Smuzhiyun 	bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq);
2719*4882a593Smuzhiyun 	bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
2720*4882a593Smuzhiyun 	bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
2721*4882a593Smuzhiyun 	bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
2722*4882a593Smuzhiyun 	if (unlikely(bfq_bfqq_just_created(bfqq) &&
2723*4882a593Smuzhiyun 		     !bfq_bfqq_in_large_burst(bfqq) &&
2724*4882a593Smuzhiyun 		     bfqq->bfqd->low_latency)) {
2725*4882a593Smuzhiyun 		/*
2726*4882a593Smuzhiyun 		 * bfqq being merged right after being created: bfqq
2727*4882a593Smuzhiyun 		 * would have deserved interactive weight raising, but
2728*4882a593Smuzhiyun 		 * did not make it to be set in a weight-raised state,
2729*4882a593Smuzhiyun 		 * because of this early merge.	Store directly the
2730*4882a593Smuzhiyun 		 * weight-raising state that would have been assigned
2731*4882a593Smuzhiyun 		 * to bfqq, so that to avoid that bfqq unjustly fails
2732*4882a593Smuzhiyun 		 * to enjoy weight raising if split soon.
2733*4882a593Smuzhiyun 		 */
2734*4882a593Smuzhiyun 		bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff;
2735*4882a593Smuzhiyun 		bic->saved_wr_start_at_switch_to_srt = bfq_smallest_from_now();
2736*4882a593Smuzhiyun 		bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd);
2737*4882a593Smuzhiyun 		bic->saved_last_wr_start_finish = jiffies;
2738*4882a593Smuzhiyun 	} else {
2739*4882a593Smuzhiyun 		bic->saved_wr_coeff = bfqq->wr_coeff;
2740*4882a593Smuzhiyun 		bic->saved_wr_start_at_switch_to_srt =
2741*4882a593Smuzhiyun 			bfqq->wr_start_at_switch_to_srt;
2742*4882a593Smuzhiyun 		bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish;
2743*4882a593Smuzhiyun 		bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time;
2744*4882a593Smuzhiyun 	}
2745*4882a593Smuzhiyun }
2746*4882a593Smuzhiyun 
bfq_release_process_ref(struct bfq_data * bfqd,struct bfq_queue * bfqq)2747*4882a593Smuzhiyun void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq)
2748*4882a593Smuzhiyun {
2749*4882a593Smuzhiyun 	/*
2750*4882a593Smuzhiyun 	 * To prevent bfqq's service guarantees from being violated,
2751*4882a593Smuzhiyun 	 * bfqq may be left busy, i.e., queued for service, even if
2752*4882a593Smuzhiyun 	 * empty (see comments in __bfq_bfqq_expire() for
2753*4882a593Smuzhiyun 	 * details). But, if no process will send requests to bfqq any
2754*4882a593Smuzhiyun 	 * longer, then there is no point in keeping bfqq queued for
2755*4882a593Smuzhiyun 	 * service. In addition, keeping bfqq queued for service, but
2756*4882a593Smuzhiyun 	 * with no process ref any longer, may have caused bfqq to be
2757*4882a593Smuzhiyun 	 * freed when dequeued from service. But this is assumed to
2758*4882a593Smuzhiyun 	 * never happen.
2759*4882a593Smuzhiyun 	 */
2760*4882a593Smuzhiyun 	if (bfq_bfqq_busy(bfqq) && RB_EMPTY_ROOT(&bfqq->sort_list) &&
2761*4882a593Smuzhiyun 	    bfqq != bfqd->in_service_queue)
2762*4882a593Smuzhiyun 		bfq_del_bfqq_busy(bfqd, bfqq, false);
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 	bfq_put_queue(bfqq);
2765*4882a593Smuzhiyun }
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun static void
bfq_merge_bfqqs(struct bfq_data * bfqd,struct bfq_io_cq * bic,struct bfq_queue * bfqq,struct bfq_queue * new_bfqq)2768*4882a593Smuzhiyun bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
2769*4882a593Smuzhiyun 		struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2770*4882a593Smuzhiyun {
2771*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
2772*4882a593Smuzhiyun 		(unsigned long)new_bfqq->pid);
2773*4882a593Smuzhiyun 	/* Save weight raising and idle window of the merged queues */
2774*4882a593Smuzhiyun 	bfq_bfqq_save_state(bfqq);
2775*4882a593Smuzhiyun 	bfq_bfqq_save_state(new_bfqq);
2776*4882a593Smuzhiyun 	if (bfq_bfqq_IO_bound(bfqq))
2777*4882a593Smuzhiyun 		bfq_mark_bfqq_IO_bound(new_bfqq);
2778*4882a593Smuzhiyun 	bfq_clear_bfqq_IO_bound(bfqq);
2779*4882a593Smuzhiyun 
2780*4882a593Smuzhiyun 	/*
2781*4882a593Smuzhiyun 	 * If bfqq is weight-raised, then let new_bfqq inherit
2782*4882a593Smuzhiyun 	 * weight-raising. To reduce false positives, neglect the case
2783*4882a593Smuzhiyun 	 * where bfqq has just been created, but has not yet made it
2784*4882a593Smuzhiyun 	 * to be weight-raised (which may happen because EQM may merge
2785*4882a593Smuzhiyun 	 * bfqq even before bfq_add_request is executed for the first
2786*4882a593Smuzhiyun 	 * time for bfqq). Handling this case would however be very
2787*4882a593Smuzhiyun 	 * easy, thanks to the flag just_created.
2788*4882a593Smuzhiyun 	 */
2789*4882a593Smuzhiyun 	if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) {
2790*4882a593Smuzhiyun 		new_bfqq->wr_coeff = bfqq->wr_coeff;
2791*4882a593Smuzhiyun 		new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time;
2792*4882a593Smuzhiyun 		new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish;
2793*4882a593Smuzhiyun 		new_bfqq->wr_start_at_switch_to_srt =
2794*4882a593Smuzhiyun 			bfqq->wr_start_at_switch_to_srt;
2795*4882a593Smuzhiyun 		if (bfq_bfqq_busy(new_bfqq))
2796*4882a593Smuzhiyun 			bfqd->wr_busy_queues++;
2797*4882a593Smuzhiyun 		new_bfqq->entity.prio_changed = 1;
2798*4882a593Smuzhiyun 	}
2799*4882a593Smuzhiyun 
2800*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */
2801*4882a593Smuzhiyun 		bfqq->wr_coeff = 1;
2802*4882a593Smuzhiyun 		bfqq->entity.prio_changed = 1;
2803*4882a593Smuzhiyun 		if (bfq_bfqq_busy(bfqq))
2804*4882a593Smuzhiyun 			bfqd->wr_busy_queues--;
2805*4882a593Smuzhiyun 	}
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d",
2808*4882a593Smuzhiyun 		     bfqd->wr_busy_queues);
2809*4882a593Smuzhiyun 
2810*4882a593Smuzhiyun 	/*
2811*4882a593Smuzhiyun 	 * Merge queues (that is, let bic redirect its requests to new_bfqq)
2812*4882a593Smuzhiyun 	 */
2813*4882a593Smuzhiyun 	bic_set_bfqq(bic, new_bfqq, 1);
2814*4882a593Smuzhiyun 	bfq_mark_bfqq_coop(new_bfqq);
2815*4882a593Smuzhiyun 	/*
2816*4882a593Smuzhiyun 	 * new_bfqq now belongs to at least two bics (it is a shared queue):
2817*4882a593Smuzhiyun 	 * set new_bfqq->bic to NULL. bfqq either:
2818*4882a593Smuzhiyun 	 * - does not belong to any bic any more, and hence bfqq->bic must
2819*4882a593Smuzhiyun 	 *   be set to NULL, or
2820*4882a593Smuzhiyun 	 * - is a queue whose owning bics have already been redirected to a
2821*4882a593Smuzhiyun 	 *   different queue, hence the queue is destined to not belong to
2822*4882a593Smuzhiyun 	 *   any bic soon and bfqq->bic is already NULL (therefore the next
2823*4882a593Smuzhiyun 	 *   assignment causes no harm).
2824*4882a593Smuzhiyun 	 */
2825*4882a593Smuzhiyun 	new_bfqq->bic = NULL;
2826*4882a593Smuzhiyun 	/*
2827*4882a593Smuzhiyun 	 * If the queue is shared, the pid is the pid of one of the associated
2828*4882a593Smuzhiyun 	 * processes. Which pid depends on the exact sequence of merge events
2829*4882a593Smuzhiyun 	 * the queue underwent. So printing such a pid is useless and confusing
2830*4882a593Smuzhiyun 	 * because it reports a random pid between those of the associated
2831*4882a593Smuzhiyun 	 * processes.
2832*4882a593Smuzhiyun 	 * We mark such a queue with a pid -1, and then print SHARED instead of
2833*4882a593Smuzhiyun 	 * a pid in logging messages.
2834*4882a593Smuzhiyun 	 */
2835*4882a593Smuzhiyun 	new_bfqq->pid = -1;
2836*4882a593Smuzhiyun 	bfqq->bic = NULL;
2837*4882a593Smuzhiyun 	bfq_release_process_ref(bfqd, bfqq);
2838*4882a593Smuzhiyun }
2839*4882a593Smuzhiyun 
bfq_allow_bio_merge(struct request_queue * q,struct request * rq,struct bio * bio)2840*4882a593Smuzhiyun static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2841*4882a593Smuzhiyun 				struct bio *bio)
2842*4882a593Smuzhiyun {
2843*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
2844*4882a593Smuzhiyun 	bool is_sync = op_is_sync(bio->bi_opf);
2845*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq;
2846*4882a593Smuzhiyun 
2847*4882a593Smuzhiyun 	/*
2848*4882a593Smuzhiyun 	 * Disallow merge of a sync bio into an async request.
2849*4882a593Smuzhiyun 	 */
2850*4882a593Smuzhiyun 	if (is_sync && !rq_is_sync(rq))
2851*4882a593Smuzhiyun 		return false;
2852*4882a593Smuzhiyun 
2853*4882a593Smuzhiyun 	/*
2854*4882a593Smuzhiyun 	 * Lookup the bfqq that this bio will be queued with. Allow
2855*4882a593Smuzhiyun 	 * merge only if rq is queued there.
2856*4882a593Smuzhiyun 	 */
2857*4882a593Smuzhiyun 	if (!bfqq)
2858*4882a593Smuzhiyun 		return false;
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	/*
2861*4882a593Smuzhiyun 	 * We take advantage of this function to perform an early merge
2862*4882a593Smuzhiyun 	 * of the queues of possible cooperating processes.
2863*4882a593Smuzhiyun 	 */
2864*4882a593Smuzhiyun 	new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
2865*4882a593Smuzhiyun 	if (new_bfqq) {
2866*4882a593Smuzhiyun 		/*
2867*4882a593Smuzhiyun 		 * bic still points to bfqq, then it has not yet been
2868*4882a593Smuzhiyun 		 * redirected to some other bfq_queue, and a queue
2869*4882a593Smuzhiyun 		 * merge between bfqq and new_bfqq can be safely
2870*4882a593Smuzhiyun 		 * fulfilled, i.e., bic can be redirected to new_bfqq
2871*4882a593Smuzhiyun 		 * and bfqq can be put.
2872*4882a593Smuzhiyun 		 */
2873*4882a593Smuzhiyun 		bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq,
2874*4882a593Smuzhiyun 				new_bfqq);
2875*4882a593Smuzhiyun 		/*
2876*4882a593Smuzhiyun 		 * If we get here, bio will be queued into new_queue,
2877*4882a593Smuzhiyun 		 * so use new_bfqq to decide whether bio and rq can be
2878*4882a593Smuzhiyun 		 * merged.
2879*4882a593Smuzhiyun 		 */
2880*4882a593Smuzhiyun 		bfqq = new_bfqq;
2881*4882a593Smuzhiyun 
2882*4882a593Smuzhiyun 		/*
2883*4882a593Smuzhiyun 		 * Change also bqfd->bio_bfqq, as
2884*4882a593Smuzhiyun 		 * bfqd->bio_bic now points to new_bfqq, and
2885*4882a593Smuzhiyun 		 * this function may be invoked again (and then may
2886*4882a593Smuzhiyun 		 * use again bqfd->bio_bfqq).
2887*4882a593Smuzhiyun 		 */
2888*4882a593Smuzhiyun 		bfqd->bio_bfqq = bfqq;
2889*4882a593Smuzhiyun 	}
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun 	return bfqq == RQ_BFQQ(rq);
2892*4882a593Smuzhiyun }
2893*4882a593Smuzhiyun 
2894*4882a593Smuzhiyun /*
2895*4882a593Smuzhiyun  * Set the maximum time for the in-service queue to consume its
2896*4882a593Smuzhiyun  * budget. This prevents seeky processes from lowering the throughput.
2897*4882a593Smuzhiyun  * In practice, a time-slice service scheme is used with seeky
2898*4882a593Smuzhiyun  * processes.
2899*4882a593Smuzhiyun  */
bfq_set_budget_timeout(struct bfq_data * bfqd,struct bfq_queue * bfqq)2900*4882a593Smuzhiyun static void bfq_set_budget_timeout(struct bfq_data *bfqd,
2901*4882a593Smuzhiyun 				   struct bfq_queue *bfqq)
2902*4882a593Smuzhiyun {
2903*4882a593Smuzhiyun 	unsigned int timeout_coeff;
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun 	if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
2906*4882a593Smuzhiyun 		timeout_coeff = 1;
2907*4882a593Smuzhiyun 	else
2908*4882a593Smuzhiyun 		timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun 	bfqd->last_budget_start = ktime_get();
2911*4882a593Smuzhiyun 
2912*4882a593Smuzhiyun 	bfqq->budget_timeout = jiffies +
2913*4882a593Smuzhiyun 		bfqd->bfq_timeout * timeout_coeff;
2914*4882a593Smuzhiyun }
2915*4882a593Smuzhiyun 
__bfq_set_in_service_queue(struct bfq_data * bfqd,struct bfq_queue * bfqq)2916*4882a593Smuzhiyun static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
2917*4882a593Smuzhiyun 				       struct bfq_queue *bfqq)
2918*4882a593Smuzhiyun {
2919*4882a593Smuzhiyun 	if (bfqq) {
2920*4882a593Smuzhiyun 		bfq_clear_bfqq_fifo_expire(bfqq);
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun 		bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8;
2923*4882a593Smuzhiyun 
2924*4882a593Smuzhiyun 		if (time_is_before_jiffies(bfqq->last_wr_start_finish) &&
2925*4882a593Smuzhiyun 		    bfqq->wr_coeff > 1 &&
2926*4882a593Smuzhiyun 		    bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
2927*4882a593Smuzhiyun 		    time_is_before_jiffies(bfqq->budget_timeout)) {
2928*4882a593Smuzhiyun 			/*
2929*4882a593Smuzhiyun 			 * For soft real-time queues, move the start
2930*4882a593Smuzhiyun 			 * of the weight-raising period forward by the
2931*4882a593Smuzhiyun 			 * time the queue has not received any
2932*4882a593Smuzhiyun 			 * service. Otherwise, a relatively long
2933*4882a593Smuzhiyun 			 * service delay is likely to cause the
2934*4882a593Smuzhiyun 			 * weight-raising period of the queue to end,
2935*4882a593Smuzhiyun 			 * because of the short duration of the
2936*4882a593Smuzhiyun 			 * weight-raising period of a soft real-time
2937*4882a593Smuzhiyun 			 * queue.  It is worth noting that this move
2938*4882a593Smuzhiyun 			 * is not so dangerous for the other queues,
2939*4882a593Smuzhiyun 			 * because soft real-time queues are not
2940*4882a593Smuzhiyun 			 * greedy.
2941*4882a593Smuzhiyun 			 *
2942*4882a593Smuzhiyun 			 * To not add a further variable, we use the
2943*4882a593Smuzhiyun 			 * overloaded field budget_timeout to
2944*4882a593Smuzhiyun 			 * determine for how long the queue has not
2945*4882a593Smuzhiyun 			 * received service, i.e., how much time has
2946*4882a593Smuzhiyun 			 * elapsed since the queue expired. However,
2947*4882a593Smuzhiyun 			 * this is a little imprecise, because
2948*4882a593Smuzhiyun 			 * budget_timeout is set to jiffies if bfqq
2949*4882a593Smuzhiyun 			 * not only expires, but also remains with no
2950*4882a593Smuzhiyun 			 * request.
2951*4882a593Smuzhiyun 			 */
2952*4882a593Smuzhiyun 			if (time_after(bfqq->budget_timeout,
2953*4882a593Smuzhiyun 				       bfqq->last_wr_start_finish))
2954*4882a593Smuzhiyun 				bfqq->last_wr_start_finish +=
2955*4882a593Smuzhiyun 					jiffies - bfqq->budget_timeout;
2956*4882a593Smuzhiyun 			else
2957*4882a593Smuzhiyun 				bfqq->last_wr_start_finish = jiffies;
2958*4882a593Smuzhiyun 		}
2959*4882a593Smuzhiyun 
2960*4882a593Smuzhiyun 		bfq_set_budget_timeout(bfqd, bfqq);
2961*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq,
2962*4882a593Smuzhiyun 			     "set_in_service_queue, cur-budget = %d",
2963*4882a593Smuzhiyun 			     bfqq->entity.budget);
2964*4882a593Smuzhiyun 	}
2965*4882a593Smuzhiyun 
2966*4882a593Smuzhiyun 	bfqd->in_service_queue = bfqq;
2967*4882a593Smuzhiyun 	bfqd->in_serv_last_pos = 0;
2968*4882a593Smuzhiyun }
2969*4882a593Smuzhiyun 
2970*4882a593Smuzhiyun /*
2971*4882a593Smuzhiyun  * Get and set a new queue for service.
2972*4882a593Smuzhiyun  */
bfq_set_in_service_queue(struct bfq_data * bfqd)2973*4882a593Smuzhiyun static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
2974*4882a593Smuzhiyun {
2975*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
2976*4882a593Smuzhiyun 
2977*4882a593Smuzhiyun 	__bfq_set_in_service_queue(bfqd, bfqq);
2978*4882a593Smuzhiyun 	return bfqq;
2979*4882a593Smuzhiyun }
2980*4882a593Smuzhiyun 
bfq_arm_slice_timer(struct bfq_data * bfqd)2981*4882a593Smuzhiyun static void bfq_arm_slice_timer(struct bfq_data *bfqd)
2982*4882a593Smuzhiyun {
2983*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfqd->in_service_queue;
2984*4882a593Smuzhiyun 	u32 sl;
2985*4882a593Smuzhiyun 
2986*4882a593Smuzhiyun 	bfq_mark_bfqq_wait_request(bfqq);
2987*4882a593Smuzhiyun 
2988*4882a593Smuzhiyun 	/*
2989*4882a593Smuzhiyun 	 * We don't want to idle for seeks, but we do want to allow
2990*4882a593Smuzhiyun 	 * fair distribution of slice time for a process doing back-to-back
2991*4882a593Smuzhiyun 	 * seeks. So allow a little bit of time for him to submit a new rq.
2992*4882a593Smuzhiyun 	 */
2993*4882a593Smuzhiyun 	sl = bfqd->bfq_slice_idle;
2994*4882a593Smuzhiyun 	/*
2995*4882a593Smuzhiyun 	 * Unless the queue is being weight-raised or the scenario is
2996*4882a593Smuzhiyun 	 * asymmetric, grant only minimum idle time if the queue
2997*4882a593Smuzhiyun 	 * is seeky. A long idling is preserved for a weight-raised
2998*4882a593Smuzhiyun 	 * queue, or, more in general, in an asymmetric scenario,
2999*4882a593Smuzhiyun 	 * because a long idling is needed for guaranteeing to a queue
3000*4882a593Smuzhiyun 	 * its reserved share of the throughput (in particular, it is
3001*4882a593Smuzhiyun 	 * needed if the queue has a higher weight than some other
3002*4882a593Smuzhiyun 	 * queue).
3003*4882a593Smuzhiyun 	 */
3004*4882a593Smuzhiyun 	if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
3005*4882a593Smuzhiyun 	    !bfq_asymmetric_scenario(bfqd, bfqq))
3006*4882a593Smuzhiyun 		sl = min_t(u64, sl, BFQ_MIN_TT);
3007*4882a593Smuzhiyun 	else if (bfqq->wr_coeff > 1)
3008*4882a593Smuzhiyun 		sl = max_t(u32, sl, 20ULL * NSEC_PER_MSEC);
3009*4882a593Smuzhiyun 
3010*4882a593Smuzhiyun 	bfqd->last_idling_start = ktime_get();
3011*4882a593Smuzhiyun 	bfqd->last_idling_start_jiffies = jiffies;
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun 	hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl),
3014*4882a593Smuzhiyun 		      HRTIMER_MODE_REL);
3015*4882a593Smuzhiyun 	bfqg_stats_set_start_idle_time(bfqq_group(bfqq));
3016*4882a593Smuzhiyun }
3017*4882a593Smuzhiyun 
3018*4882a593Smuzhiyun /*
3019*4882a593Smuzhiyun  * In autotuning mode, max_budget is dynamically recomputed as the
3020*4882a593Smuzhiyun  * amount of sectors transferred in timeout at the estimated peak
3021*4882a593Smuzhiyun  * rate. This enables BFQ to utilize a full timeslice with a full
3022*4882a593Smuzhiyun  * budget, even if the in-service queue is served at peak rate. And
3023*4882a593Smuzhiyun  * this maximises throughput with sequential workloads.
3024*4882a593Smuzhiyun  */
bfq_calc_max_budget(struct bfq_data * bfqd)3025*4882a593Smuzhiyun static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd)
3026*4882a593Smuzhiyun {
3027*4882a593Smuzhiyun 	return (u64)bfqd->peak_rate * USEC_PER_MSEC *
3028*4882a593Smuzhiyun 		jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT;
3029*4882a593Smuzhiyun }
3030*4882a593Smuzhiyun 
3031*4882a593Smuzhiyun /*
3032*4882a593Smuzhiyun  * Update parameters related to throughput and responsiveness, as a
3033*4882a593Smuzhiyun  * function of the estimated peak rate. See comments on
3034*4882a593Smuzhiyun  * bfq_calc_max_budget(), and on the ref_wr_duration array.
3035*4882a593Smuzhiyun  */
update_thr_responsiveness_params(struct bfq_data * bfqd)3036*4882a593Smuzhiyun static void update_thr_responsiveness_params(struct bfq_data *bfqd)
3037*4882a593Smuzhiyun {
3038*4882a593Smuzhiyun 	if (bfqd->bfq_user_max_budget == 0) {
3039*4882a593Smuzhiyun 		bfqd->bfq_max_budget =
3040*4882a593Smuzhiyun 			bfq_calc_max_budget(bfqd);
3041*4882a593Smuzhiyun 		bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget);
3042*4882a593Smuzhiyun 	}
3043*4882a593Smuzhiyun }
3044*4882a593Smuzhiyun 
bfq_reset_rate_computation(struct bfq_data * bfqd,struct request * rq)3045*4882a593Smuzhiyun static void bfq_reset_rate_computation(struct bfq_data *bfqd,
3046*4882a593Smuzhiyun 				       struct request *rq)
3047*4882a593Smuzhiyun {
3048*4882a593Smuzhiyun 	if (rq != NULL) { /* new rq dispatch now, reset accordingly */
3049*4882a593Smuzhiyun 		bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns();
3050*4882a593Smuzhiyun 		bfqd->peak_rate_samples = 1;
3051*4882a593Smuzhiyun 		bfqd->sequential_samples = 0;
3052*4882a593Smuzhiyun 		bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size =
3053*4882a593Smuzhiyun 			blk_rq_sectors(rq);
3054*4882a593Smuzhiyun 	} else /* no new rq dispatched, just reset the number of samples */
3055*4882a593Smuzhiyun 		bfqd->peak_rate_samples = 0; /* full re-init on next disp. */
3056*4882a593Smuzhiyun 
3057*4882a593Smuzhiyun 	bfq_log(bfqd,
3058*4882a593Smuzhiyun 		"reset_rate_computation at end, sample %u/%u tot_sects %llu",
3059*4882a593Smuzhiyun 		bfqd->peak_rate_samples, bfqd->sequential_samples,
3060*4882a593Smuzhiyun 		bfqd->tot_sectors_dispatched);
3061*4882a593Smuzhiyun }
3062*4882a593Smuzhiyun 
bfq_update_rate_reset(struct bfq_data * bfqd,struct request * rq)3063*4882a593Smuzhiyun static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq)
3064*4882a593Smuzhiyun {
3065*4882a593Smuzhiyun 	u32 rate, weight, divisor;
3066*4882a593Smuzhiyun 
3067*4882a593Smuzhiyun 	/*
3068*4882a593Smuzhiyun 	 * For the convergence property to hold (see comments on
3069*4882a593Smuzhiyun 	 * bfq_update_peak_rate()) and for the assessment to be
3070*4882a593Smuzhiyun 	 * reliable, a minimum number of samples must be present, and
3071*4882a593Smuzhiyun 	 * a minimum amount of time must have elapsed. If not so, do
3072*4882a593Smuzhiyun 	 * not compute new rate. Just reset parameters, to get ready
3073*4882a593Smuzhiyun 	 * for a new evaluation attempt.
3074*4882a593Smuzhiyun 	 */
3075*4882a593Smuzhiyun 	if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES ||
3076*4882a593Smuzhiyun 	    bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL)
3077*4882a593Smuzhiyun 		goto reset_computation;
3078*4882a593Smuzhiyun 
3079*4882a593Smuzhiyun 	/*
3080*4882a593Smuzhiyun 	 * If a new request completion has occurred after last
3081*4882a593Smuzhiyun 	 * dispatch, then, to approximate the rate at which requests
3082*4882a593Smuzhiyun 	 * have been served by the device, it is more precise to
3083*4882a593Smuzhiyun 	 * extend the observation interval to the last completion.
3084*4882a593Smuzhiyun 	 */
3085*4882a593Smuzhiyun 	bfqd->delta_from_first =
3086*4882a593Smuzhiyun 		max_t(u64, bfqd->delta_from_first,
3087*4882a593Smuzhiyun 		      bfqd->last_completion - bfqd->first_dispatch);
3088*4882a593Smuzhiyun 
3089*4882a593Smuzhiyun 	/*
3090*4882a593Smuzhiyun 	 * Rate computed in sects/usec, and not sects/nsec, for
3091*4882a593Smuzhiyun 	 * precision issues.
3092*4882a593Smuzhiyun 	 */
3093*4882a593Smuzhiyun 	rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT,
3094*4882a593Smuzhiyun 			div_u64(bfqd->delta_from_first, NSEC_PER_USEC));
3095*4882a593Smuzhiyun 
3096*4882a593Smuzhiyun 	/*
3097*4882a593Smuzhiyun 	 * Peak rate not updated if:
3098*4882a593Smuzhiyun 	 * - the percentage of sequential dispatches is below 3/4 of the
3099*4882a593Smuzhiyun 	 *   total, and rate is below the current estimated peak rate
3100*4882a593Smuzhiyun 	 * - rate is unreasonably high (> 20M sectors/sec)
3101*4882a593Smuzhiyun 	 */
3102*4882a593Smuzhiyun 	if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 &&
3103*4882a593Smuzhiyun 	     rate <= bfqd->peak_rate) ||
3104*4882a593Smuzhiyun 		rate > 20<<BFQ_RATE_SHIFT)
3105*4882a593Smuzhiyun 		goto reset_computation;
3106*4882a593Smuzhiyun 
3107*4882a593Smuzhiyun 	/*
3108*4882a593Smuzhiyun 	 * We have to update the peak rate, at last! To this purpose,
3109*4882a593Smuzhiyun 	 * we use a low-pass filter. We compute the smoothing constant
3110*4882a593Smuzhiyun 	 * of the filter as a function of the 'weight' of the new
3111*4882a593Smuzhiyun 	 * measured rate.
3112*4882a593Smuzhiyun 	 *
3113*4882a593Smuzhiyun 	 * As can be seen in next formulas, we define this weight as a
3114*4882a593Smuzhiyun 	 * quantity proportional to how sequential the workload is,
3115*4882a593Smuzhiyun 	 * and to how long the observation time interval is.
3116*4882a593Smuzhiyun 	 *
3117*4882a593Smuzhiyun 	 * The weight runs from 0 to 8. The maximum value of the
3118*4882a593Smuzhiyun 	 * weight, 8, yields the minimum value for the smoothing
3119*4882a593Smuzhiyun 	 * constant. At this minimum value for the smoothing constant,
3120*4882a593Smuzhiyun 	 * the measured rate contributes for half of the next value of
3121*4882a593Smuzhiyun 	 * the estimated peak rate.
3122*4882a593Smuzhiyun 	 *
3123*4882a593Smuzhiyun 	 * So, the first step is to compute the weight as a function
3124*4882a593Smuzhiyun 	 * of how sequential the workload is. Note that the weight
3125*4882a593Smuzhiyun 	 * cannot reach 9, because bfqd->sequential_samples cannot
3126*4882a593Smuzhiyun 	 * become equal to bfqd->peak_rate_samples, which, in its
3127*4882a593Smuzhiyun 	 * turn, holds true because bfqd->sequential_samples is not
3128*4882a593Smuzhiyun 	 * incremented for the first sample.
3129*4882a593Smuzhiyun 	 */
3130*4882a593Smuzhiyun 	weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples;
3131*4882a593Smuzhiyun 
3132*4882a593Smuzhiyun 	/*
3133*4882a593Smuzhiyun 	 * Second step: further refine the weight as a function of the
3134*4882a593Smuzhiyun 	 * duration of the observation interval.
3135*4882a593Smuzhiyun 	 */
3136*4882a593Smuzhiyun 	weight = min_t(u32, 8,
3137*4882a593Smuzhiyun 		       div_u64(weight * bfqd->delta_from_first,
3138*4882a593Smuzhiyun 			       BFQ_RATE_REF_INTERVAL));
3139*4882a593Smuzhiyun 
3140*4882a593Smuzhiyun 	/*
3141*4882a593Smuzhiyun 	 * Divisor ranging from 10, for minimum weight, to 2, for
3142*4882a593Smuzhiyun 	 * maximum weight.
3143*4882a593Smuzhiyun 	 */
3144*4882a593Smuzhiyun 	divisor = 10 - weight;
3145*4882a593Smuzhiyun 
3146*4882a593Smuzhiyun 	/*
3147*4882a593Smuzhiyun 	 * Finally, update peak rate:
3148*4882a593Smuzhiyun 	 *
3149*4882a593Smuzhiyun 	 * peak_rate = peak_rate * (divisor-1) / divisor  +  rate / divisor
3150*4882a593Smuzhiyun 	 */
3151*4882a593Smuzhiyun 	bfqd->peak_rate *= divisor-1;
3152*4882a593Smuzhiyun 	bfqd->peak_rate /= divisor;
3153*4882a593Smuzhiyun 	rate /= divisor; /* smoothing constant alpha = 1/divisor */
3154*4882a593Smuzhiyun 
3155*4882a593Smuzhiyun 	bfqd->peak_rate += rate;
3156*4882a593Smuzhiyun 
3157*4882a593Smuzhiyun 	/*
3158*4882a593Smuzhiyun 	 * For a very slow device, bfqd->peak_rate can reach 0 (see
3159*4882a593Smuzhiyun 	 * the minimum representable values reported in the comments
3160*4882a593Smuzhiyun 	 * on BFQ_RATE_SHIFT). Push to 1 if this happens, to avoid
3161*4882a593Smuzhiyun 	 * divisions by zero where bfqd->peak_rate is used as a
3162*4882a593Smuzhiyun 	 * divisor.
3163*4882a593Smuzhiyun 	 */
3164*4882a593Smuzhiyun 	bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate);
3165*4882a593Smuzhiyun 
3166*4882a593Smuzhiyun 	update_thr_responsiveness_params(bfqd);
3167*4882a593Smuzhiyun 
3168*4882a593Smuzhiyun reset_computation:
3169*4882a593Smuzhiyun 	bfq_reset_rate_computation(bfqd, rq);
3170*4882a593Smuzhiyun }
3171*4882a593Smuzhiyun 
3172*4882a593Smuzhiyun /*
3173*4882a593Smuzhiyun  * Update the read/write peak rate (the main quantity used for
3174*4882a593Smuzhiyun  * auto-tuning, see update_thr_responsiveness_params()).
3175*4882a593Smuzhiyun  *
3176*4882a593Smuzhiyun  * It is not trivial to estimate the peak rate (correctly): because of
3177*4882a593Smuzhiyun  * the presence of sw and hw queues between the scheduler and the
3178*4882a593Smuzhiyun  * device components that finally serve I/O requests, it is hard to
3179*4882a593Smuzhiyun  * say exactly when a given dispatched request is served inside the
3180*4882a593Smuzhiyun  * device, and for how long. As a consequence, it is hard to know
3181*4882a593Smuzhiyun  * precisely at what rate a given set of requests is actually served
3182*4882a593Smuzhiyun  * by the device.
3183*4882a593Smuzhiyun  *
3184*4882a593Smuzhiyun  * On the opposite end, the dispatch time of any request is trivially
3185*4882a593Smuzhiyun  * available, and, from this piece of information, the "dispatch rate"
3186*4882a593Smuzhiyun  * of requests can be immediately computed. So, the idea in the next
3187*4882a593Smuzhiyun  * function is to use what is known, namely request dispatch times
3188*4882a593Smuzhiyun  * (plus, when useful, request completion times), to estimate what is
3189*4882a593Smuzhiyun  * unknown, namely in-device request service rate.
3190*4882a593Smuzhiyun  *
3191*4882a593Smuzhiyun  * The main issue is that, because of the above facts, the rate at
3192*4882a593Smuzhiyun  * which a certain set of requests is dispatched over a certain time
3193*4882a593Smuzhiyun  * interval can vary greatly with respect to the rate at which the
3194*4882a593Smuzhiyun  * same requests are then served. But, since the size of any
3195*4882a593Smuzhiyun  * intermediate queue is limited, and the service scheme is lossless
3196*4882a593Smuzhiyun  * (no request is silently dropped), the following obvious convergence
3197*4882a593Smuzhiyun  * property holds: the number of requests dispatched MUST become
3198*4882a593Smuzhiyun  * closer and closer to the number of requests completed as the
3199*4882a593Smuzhiyun  * observation interval grows. This is the key property used in
3200*4882a593Smuzhiyun  * the next function to estimate the peak service rate as a function
3201*4882a593Smuzhiyun  * of the observed dispatch rate. The function assumes to be invoked
3202*4882a593Smuzhiyun  * on every request dispatch.
3203*4882a593Smuzhiyun  */
bfq_update_peak_rate(struct bfq_data * bfqd,struct request * rq)3204*4882a593Smuzhiyun static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq)
3205*4882a593Smuzhiyun {
3206*4882a593Smuzhiyun 	u64 now_ns = ktime_get_ns();
3207*4882a593Smuzhiyun 
3208*4882a593Smuzhiyun 	if (bfqd->peak_rate_samples == 0) { /* first dispatch */
3209*4882a593Smuzhiyun 		bfq_log(bfqd, "update_peak_rate: goto reset, samples %d",
3210*4882a593Smuzhiyun 			bfqd->peak_rate_samples);
3211*4882a593Smuzhiyun 		bfq_reset_rate_computation(bfqd, rq);
3212*4882a593Smuzhiyun 		goto update_last_values; /* will add one sample */
3213*4882a593Smuzhiyun 	}
3214*4882a593Smuzhiyun 
3215*4882a593Smuzhiyun 	/*
3216*4882a593Smuzhiyun 	 * Device idle for very long: the observation interval lasting
3217*4882a593Smuzhiyun 	 * up to this dispatch cannot be a valid observation interval
3218*4882a593Smuzhiyun 	 * for computing a new peak rate (similarly to the late-
3219*4882a593Smuzhiyun 	 * completion event in bfq_completed_request()). Go to
3220*4882a593Smuzhiyun 	 * update_rate_and_reset to have the following three steps
3221*4882a593Smuzhiyun 	 * taken:
3222*4882a593Smuzhiyun 	 * - close the observation interval at the last (previous)
3223*4882a593Smuzhiyun 	 *   request dispatch or completion
3224*4882a593Smuzhiyun 	 * - compute rate, if possible, for that observation interval
3225*4882a593Smuzhiyun 	 * - start a new observation interval with this dispatch
3226*4882a593Smuzhiyun 	 */
3227*4882a593Smuzhiyun 	if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC &&
3228*4882a593Smuzhiyun 	    bfqd->rq_in_driver == 0)
3229*4882a593Smuzhiyun 		goto update_rate_and_reset;
3230*4882a593Smuzhiyun 
3231*4882a593Smuzhiyun 	/* Update sampling information */
3232*4882a593Smuzhiyun 	bfqd->peak_rate_samples++;
3233*4882a593Smuzhiyun 
3234*4882a593Smuzhiyun 	if ((bfqd->rq_in_driver > 0 ||
3235*4882a593Smuzhiyun 		now_ns - bfqd->last_completion < BFQ_MIN_TT)
3236*4882a593Smuzhiyun 	    && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq))
3237*4882a593Smuzhiyun 		bfqd->sequential_samples++;
3238*4882a593Smuzhiyun 
3239*4882a593Smuzhiyun 	bfqd->tot_sectors_dispatched += blk_rq_sectors(rq);
3240*4882a593Smuzhiyun 
3241*4882a593Smuzhiyun 	/* Reset max observed rq size every 32 dispatches */
3242*4882a593Smuzhiyun 	if (likely(bfqd->peak_rate_samples % 32))
3243*4882a593Smuzhiyun 		bfqd->last_rq_max_size =
3244*4882a593Smuzhiyun 			max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size);
3245*4882a593Smuzhiyun 	else
3246*4882a593Smuzhiyun 		bfqd->last_rq_max_size = blk_rq_sectors(rq);
3247*4882a593Smuzhiyun 
3248*4882a593Smuzhiyun 	bfqd->delta_from_first = now_ns - bfqd->first_dispatch;
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	/* Target observation interval not yet reached, go on sampling */
3251*4882a593Smuzhiyun 	if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL)
3252*4882a593Smuzhiyun 		goto update_last_values;
3253*4882a593Smuzhiyun 
3254*4882a593Smuzhiyun update_rate_and_reset:
3255*4882a593Smuzhiyun 	bfq_update_rate_reset(bfqd, rq);
3256*4882a593Smuzhiyun update_last_values:
3257*4882a593Smuzhiyun 	bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
3258*4882a593Smuzhiyun 	if (RQ_BFQQ(rq) == bfqd->in_service_queue)
3259*4882a593Smuzhiyun 		bfqd->in_serv_last_pos = bfqd->last_position;
3260*4882a593Smuzhiyun 	bfqd->last_dispatch = now_ns;
3261*4882a593Smuzhiyun }
3262*4882a593Smuzhiyun 
3263*4882a593Smuzhiyun /*
3264*4882a593Smuzhiyun  * Remove request from internal lists.
3265*4882a593Smuzhiyun  */
bfq_dispatch_remove(struct request_queue * q,struct request * rq)3266*4882a593Smuzhiyun static void bfq_dispatch_remove(struct request_queue *q, struct request *rq)
3267*4882a593Smuzhiyun {
3268*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
3269*4882a593Smuzhiyun 
3270*4882a593Smuzhiyun 	/*
3271*4882a593Smuzhiyun 	 * For consistency, the next instruction should have been
3272*4882a593Smuzhiyun 	 * executed after removing the request from the queue and
3273*4882a593Smuzhiyun 	 * dispatching it.  We execute instead this instruction before
3274*4882a593Smuzhiyun 	 * bfq_remove_request() (and hence introduce a temporary
3275*4882a593Smuzhiyun 	 * inconsistency), for efficiency.  In fact, should this
3276*4882a593Smuzhiyun 	 * dispatch occur for a non in-service bfqq, this anticipated
3277*4882a593Smuzhiyun 	 * increment prevents two counters related to bfqq->dispatched
3278*4882a593Smuzhiyun 	 * from risking to be, first, uselessly decremented, and then
3279*4882a593Smuzhiyun 	 * incremented again when the (new) value of bfqq->dispatched
3280*4882a593Smuzhiyun 	 * happens to be taken into account.
3281*4882a593Smuzhiyun 	 */
3282*4882a593Smuzhiyun 	bfqq->dispatched++;
3283*4882a593Smuzhiyun 	bfq_update_peak_rate(q->elevator->elevator_data, rq);
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 	bfq_remove_request(q, rq);
3286*4882a593Smuzhiyun }
3287*4882a593Smuzhiyun 
3288*4882a593Smuzhiyun /*
3289*4882a593Smuzhiyun  * There is a case where idling does not have to be performed for
3290*4882a593Smuzhiyun  * throughput concerns, but to preserve the throughput share of
3291*4882a593Smuzhiyun  * the process associated with bfqq.
3292*4882a593Smuzhiyun  *
3293*4882a593Smuzhiyun  * To introduce this case, we can note that allowing the drive
3294*4882a593Smuzhiyun  * to enqueue more than one request at a time, and hence
3295*4882a593Smuzhiyun  * delegating de facto final scheduling decisions to the
3296*4882a593Smuzhiyun  * drive's internal scheduler, entails loss of control on the
3297*4882a593Smuzhiyun  * actual request service order. In particular, the critical
3298*4882a593Smuzhiyun  * situation is when requests from different processes happen
3299*4882a593Smuzhiyun  * to be present, at the same time, in the internal queue(s)
3300*4882a593Smuzhiyun  * of the drive. In such a situation, the drive, by deciding
3301*4882a593Smuzhiyun  * the service order of the internally-queued requests, does
3302*4882a593Smuzhiyun  * determine also the actual throughput distribution among
3303*4882a593Smuzhiyun  * these processes. But the drive typically has no notion or
3304*4882a593Smuzhiyun  * concern about per-process throughput distribution, and
3305*4882a593Smuzhiyun  * makes its decisions only on a per-request basis. Therefore,
3306*4882a593Smuzhiyun  * the service distribution enforced by the drive's internal
3307*4882a593Smuzhiyun  * scheduler is likely to coincide with the desired throughput
3308*4882a593Smuzhiyun  * distribution only in a completely symmetric, or favorably
3309*4882a593Smuzhiyun  * skewed scenario where:
3310*4882a593Smuzhiyun  * (i-a) each of these processes must get the same throughput as
3311*4882a593Smuzhiyun  *	 the others,
3312*4882a593Smuzhiyun  * (i-b) in case (i-a) does not hold, it holds that the process
3313*4882a593Smuzhiyun  *       associated with bfqq must receive a lower or equal
3314*4882a593Smuzhiyun  *	 throughput than any of the other processes;
3315*4882a593Smuzhiyun  * (ii)  the I/O of each process has the same properties, in
3316*4882a593Smuzhiyun  *       terms of locality (sequential or random), direction
3317*4882a593Smuzhiyun  *       (reads or writes), request sizes, greediness
3318*4882a593Smuzhiyun  *       (from I/O-bound to sporadic), and so on;
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun  * In fact, in such a scenario, the drive tends to treat the requests
3321*4882a593Smuzhiyun  * of each process in about the same way as the requests of the
3322*4882a593Smuzhiyun  * others, and thus to provide each of these processes with about the
3323*4882a593Smuzhiyun  * same throughput.  This is exactly the desired throughput
3324*4882a593Smuzhiyun  * distribution if (i-a) holds, or, if (i-b) holds instead, this is an
3325*4882a593Smuzhiyun  * even more convenient distribution for (the process associated with)
3326*4882a593Smuzhiyun  * bfqq.
3327*4882a593Smuzhiyun  *
3328*4882a593Smuzhiyun  * In contrast, in any asymmetric or unfavorable scenario, device
3329*4882a593Smuzhiyun  * idling (I/O-dispatch plugging) is certainly needed to guarantee
3330*4882a593Smuzhiyun  * that bfqq receives its assigned fraction of the device throughput
3331*4882a593Smuzhiyun  * (see [1] for details).
3332*4882a593Smuzhiyun  *
3333*4882a593Smuzhiyun  * The problem is that idling may significantly reduce throughput with
3334*4882a593Smuzhiyun  * certain combinations of types of I/O and devices. An important
3335*4882a593Smuzhiyun  * example is sync random I/O on flash storage with command
3336*4882a593Smuzhiyun  * queueing. So, unless bfqq falls in cases where idling also boosts
3337*4882a593Smuzhiyun  * throughput, it is important to check conditions (i-a), i(-b) and
3338*4882a593Smuzhiyun  * (ii) accurately, so as to avoid idling when not strictly needed for
3339*4882a593Smuzhiyun  * service guarantees.
3340*4882a593Smuzhiyun  *
3341*4882a593Smuzhiyun  * Unfortunately, it is extremely difficult to thoroughly check
3342*4882a593Smuzhiyun  * condition (ii). And, in case there are active groups, it becomes
3343*4882a593Smuzhiyun  * very difficult to check conditions (i-a) and (i-b) too.  In fact,
3344*4882a593Smuzhiyun  * if there are active groups, then, for conditions (i-a) or (i-b) to
3345*4882a593Smuzhiyun  * become false 'indirectly', it is enough that an active group
3346*4882a593Smuzhiyun  * contains more active processes or sub-groups than some other active
3347*4882a593Smuzhiyun  * group. More precisely, for conditions (i-a) or (i-b) to become
3348*4882a593Smuzhiyun  * false because of such a group, it is not even necessary that the
3349*4882a593Smuzhiyun  * group is (still) active: it is sufficient that, even if the group
3350*4882a593Smuzhiyun  * has become inactive, some of its descendant processes still have
3351*4882a593Smuzhiyun  * some request already dispatched but still waiting for
3352*4882a593Smuzhiyun  * completion. In fact, requests have still to be guaranteed their
3353*4882a593Smuzhiyun  * share of the throughput even after being dispatched. In this
3354*4882a593Smuzhiyun  * respect, it is easy to show that, if a group frequently becomes
3355*4882a593Smuzhiyun  * inactive while still having in-flight requests, and if, when this
3356*4882a593Smuzhiyun  * happens, the group is not considered in the calculation of whether
3357*4882a593Smuzhiyun  * the scenario is asymmetric, then the group may fail to be
3358*4882a593Smuzhiyun  * guaranteed its fair share of the throughput (basically because
3359*4882a593Smuzhiyun  * idling may not be performed for the descendant processes of the
3360*4882a593Smuzhiyun  * group, but it had to be).  We address this issue with the following
3361*4882a593Smuzhiyun  * bi-modal behavior, implemented in the function
3362*4882a593Smuzhiyun  * bfq_asymmetric_scenario().
3363*4882a593Smuzhiyun  *
3364*4882a593Smuzhiyun  * If there are groups with requests waiting for completion
3365*4882a593Smuzhiyun  * (as commented above, some of these groups may even be
3366*4882a593Smuzhiyun  * already inactive), then the scenario is tagged as
3367*4882a593Smuzhiyun  * asymmetric, conservatively, without checking any of the
3368*4882a593Smuzhiyun  * conditions (i-a), (i-b) or (ii). So the device is idled for bfqq.
3369*4882a593Smuzhiyun  * This behavior matches also the fact that groups are created
3370*4882a593Smuzhiyun  * exactly if controlling I/O is a primary concern (to
3371*4882a593Smuzhiyun  * preserve bandwidth and latency guarantees).
3372*4882a593Smuzhiyun  *
3373*4882a593Smuzhiyun  * On the opposite end, if there are no groups with requests waiting
3374*4882a593Smuzhiyun  * for completion, then only conditions (i-a) and (i-b) are actually
3375*4882a593Smuzhiyun  * controlled, i.e., provided that conditions (i-a) or (i-b) holds,
3376*4882a593Smuzhiyun  * idling is not performed, regardless of whether condition (ii)
3377*4882a593Smuzhiyun  * holds.  In other words, only if conditions (i-a) and (i-b) do not
3378*4882a593Smuzhiyun  * hold, then idling is allowed, and the device tends to be prevented
3379*4882a593Smuzhiyun  * from queueing many requests, possibly of several processes. Since
3380*4882a593Smuzhiyun  * there are no groups with requests waiting for completion, then, to
3381*4882a593Smuzhiyun  * control conditions (i-a) and (i-b) it is enough to check just
3382*4882a593Smuzhiyun  * whether all the queues with requests waiting for completion also
3383*4882a593Smuzhiyun  * have the same weight.
3384*4882a593Smuzhiyun  *
3385*4882a593Smuzhiyun  * Not checking condition (ii) evidently exposes bfqq to the
3386*4882a593Smuzhiyun  * risk of getting less throughput than its fair share.
3387*4882a593Smuzhiyun  * However, for queues with the same weight, a further
3388*4882a593Smuzhiyun  * mechanism, preemption, mitigates or even eliminates this
3389*4882a593Smuzhiyun  * problem. And it does so without consequences on overall
3390*4882a593Smuzhiyun  * throughput. This mechanism and its benefits are explained
3391*4882a593Smuzhiyun  * in the next three paragraphs.
3392*4882a593Smuzhiyun  *
3393*4882a593Smuzhiyun  * Even if a queue, say Q, is expired when it remains idle, Q
3394*4882a593Smuzhiyun  * can still preempt the new in-service queue if the next
3395*4882a593Smuzhiyun  * request of Q arrives soon (see the comments on
3396*4882a593Smuzhiyun  * bfq_bfqq_update_budg_for_activation). If all queues and
3397*4882a593Smuzhiyun  * groups have the same weight, this form of preemption,
3398*4882a593Smuzhiyun  * combined with the hole-recovery heuristic described in the
3399*4882a593Smuzhiyun  * comments on function bfq_bfqq_update_budg_for_activation,
3400*4882a593Smuzhiyun  * are enough to preserve a correct bandwidth distribution in
3401*4882a593Smuzhiyun  * the mid term, even without idling. In fact, even if not
3402*4882a593Smuzhiyun  * idling allows the internal queues of the device to contain
3403*4882a593Smuzhiyun  * many requests, and thus to reorder requests, we can rather
3404*4882a593Smuzhiyun  * safely assume that the internal scheduler still preserves a
3405*4882a593Smuzhiyun  * minimum of mid-term fairness.
3406*4882a593Smuzhiyun  *
3407*4882a593Smuzhiyun  * More precisely, this preemption-based, idleless approach
3408*4882a593Smuzhiyun  * provides fairness in terms of IOPS, and not sectors per
3409*4882a593Smuzhiyun  * second. This can be seen with a simple example. Suppose
3410*4882a593Smuzhiyun  * that there are two queues with the same weight, but that
3411*4882a593Smuzhiyun  * the first queue receives requests of 8 sectors, while the
3412*4882a593Smuzhiyun  * second queue receives requests of 1024 sectors. In
3413*4882a593Smuzhiyun  * addition, suppose that each of the two queues contains at
3414*4882a593Smuzhiyun  * most one request at a time, which implies that each queue
3415*4882a593Smuzhiyun  * always remains idle after it is served. Finally, after
3416*4882a593Smuzhiyun  * remaining idle, each queue receives very quickly a new
3417*4882a593Smuzhiyun  * request. It follows that the two queues are served
3418*4882a593Smuzhiyun  * alternatively, preempting each other if needed. This
3419*4882a593Smuzhiyun  * implies that, although both queues have the same weight,
3420*4882a593Smuzhiyun  * the queue with large requests receives a service that is
3421*4882a593Smuzhiyun  * 1024/8 times as high as the service received by the other
3422*4882a593Smuzhiyun  * queue.
3423*4882a593Smuzhiyun  *
3424*4882a593Smuzhiyun  * The motivation for using preemption instead of idling (for
3425*4882a593Smuzhiyun  * queues with the same weight) is that, by not idling,
3426*4882a593Smuzhiyun  * service guarantees are preserved (completely or at least in
3427*4882a593Smuzhiyun  * part) without minimally sacrificing throughput. And, if
3428*4882a593Smuzhiyun  * there is no active group, then the primary expectation for
3429*4882a593Smuzhiyun  * this device is probably a high throughput.
3430*4882a593Smuzhiyun  *
3431*4882a593Smuzhiyun  * We are now left only with explaining the two sub-conditions in the
3432*4882a593Smuzhiyun  * additional compound condition that is checked below for deciding
3433*4882a593Smuzhiyun  * whether the scenario is asymmetric. To explain the first
3434*4882a593Smuzhiyun  * sub-condition, we need to add that the function
3435*4882a593Smuzhiyun  * bfq_asymmetric_scenario checks the weights of only
3436*4882a593Smuzhiyun  * non-weight-raised queues, for efficiency reasons (see comments on
3437*4882a593Smuzhiyun  * bfq_weights_tree_add()). Then the fact that bfqq is weight-raised
3438*4882a593Smuzhiyun  * is checked explicitly here. More precisely, the compound condition
3439*4882a593Smuzhiyun  * below takes into account also the fact that, even if bfqq is being
3440*4882a593Smuzhiyun  * weight-raised, the scenario is still symmetric if all queues with
3441*4882a593Smuzhiyun  * requests waiting for completion happen to be
3442*4882a593Smuzhiyun  * weight-raised. Actually, we should be even more precise here, and
3443*4882a593Smuzhiyun  * differentiate between interactive weight raising and soft real-time
3444*4882a593Smuzhiyun  * weight raising.
3445*4882a593Smuzhiyun  *
3446*4882a593Smuzhiyun  * The second sub-condition checked in the compound condition is
3447*4882a593Smuzhiyun  * whether there is a fair amount of already in-flight I/O not
3448*4882a593Smuzhiyun  * belonging to bfqq. If so, I/O dispatching is to be plugged, for the
3449*4882a593Smuzhiyun  * following reason. The drive may decide to serve in-flight
3450*4882a593Smuzhiyun  * non-bfqq's I/O requests before bfqq's ones, thereby delaying the
3451*4882a593Smuzhiyun  * arrival of new I/O requests for bfqq (recall that bfqq is sync). If
3452*4882a593Smuzhiyun  * I/O-dispatching is not plugged, then, while bfqq remains empty, a
3453*4882a593Smuzhiyun  * basically uncontrolled amount of I/O from other queues may be
3454*4882a593Smuzhiyun  * dispatched too, possibly causing the service of bfqq's I/O to be
3455*4882a593Smuzhiyun  * delayed even longer in the drive. This problem gets more and more
3456*4882a593Smuzhiyun  * serious as the speed and the queue depth of the drive grow,
3457*4882a593Smuzhiyun  * because, as these two quantities grow, the probability to find no
3458*4882a593Smuzhiyun  * queue busy but many requests in flight grows too. By contrast,
3459*4882a593Smuzhiyun  * plugging I/O dispatching minimizes the delay induced by already
3460*4882a593Smuzhiyun  * in-flight I/O, and enables bfqq to recover the bandwidth it may
3461*4882a593Smuzhiyun  * lose because of this delay.
3462*4882a593Smuzhiyun  *
3463*4882a593Smuzhiyun  * As a side note, it is worth considering that the above
3464*4882a593Smuzhiyun  * device-idling countermeasures may however fail in the following
3465*4882a593Smuzhiyun  * unlucky scenario: if I/O-dispatch plugging is (correctly) disabled
3466*4882a593Smuzhiyun  * in a time period during which all symmetry sub-conditions hold, and
3467*4882a593Smuzhiyun  * therefore the device is allowed to enqueue many requests, but at
3468*4882a593Smuzhiyun  * some later point in time some sub-condition stops to hold, then it
3469*4882a593Smuzhiyun  * may become impossible to make requests be served in the desired
3470*4882a593Smuzhiyun  * order until all the requests already queued in the device have been
3471*4882a593Smuzhiyun  * served. The last sub-condition commented above somewhat mitigates
3472*4882a593Smuzhiyun  * this problem for weight-raised queues.
3473*4882a593Smuzhiyun  */
idling_needed_for_service_guarantees(struct bfq_data * bfqd,struct bfq_queue * bfqq)3474*4882a593Smuzhiyun static bool idling_needed_for_service_guarantees(struct bfq_data *bfqd,
3475*4882a593Smuzhiyun 						 struct bfq_queue *bfqq)
3476*4882a593Smuzhiyun {
3477*4882a593Smuzhiyun 	/* No point in idling for bfqq if it won't get requests any longer */
3478*4882a593Smuzhiyun 	if (unlikely(!bfqq_process_refs(bfqq)))
3479*4882a593Smuzhiyun 		return false;
3480*4882a593Smuzhiyun 
3481*4882a593Smuzhiyun 	return (bfqq->wr_coeff > 1 &&
3482*4882a593Smuzhiyun 		(bfqd->wr_busy_queues <
3483*4882a593Smuzhiyun 		 bfq_tot_busy_queues(bfqd) ||
3484*4882a593Smuzhiyun 		 bfqd->rq_in_driver >=
3485*4882a593Smuzhiyun 		 bfqq->dispatched + 4)) ||
3486*4882a593Smuzhiyun 		bfq_asymmetric_scenario(bfqd, bfqq);
3487*4882a593Smuzhiyun }
3488*4882a593Smuzhiyun 
__bfq_bfqq_expire(struct bfq_data * bfqd,struct bfq_queue * bfqq,enum bfqq_expiration reason)3489*4882a593Smuzhiyun static bool __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
3490*4882a593Smuzhiyun 			      enum bfqq_expiration reason)
3491*4882a593Smuzhiyun {
3492*4882a593Smuzhiyun 	/*
3493*4882a593Smuzhiyun 	 * If this bfqq is shared between multiple processes, check
3494*4882a593Smuzhiyun 	 * to make sure that those processes are still issuing I/Os
3495*4882a593Smuzhiyun 	 * within the mean seek distance. If not, it may be time to
3496*4882a593Smuzhiyun 	 * break the queues apart again.
3497*4882a593Smuzhiyun 	 */
3498*4882a593Smuzhiyun 	if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
3499*4882a593Smuzhiyun 		bfq_mark_bfqq_split_coop(bfqq);
3500*4882a593Smuzhiyun 
3501*4882a593Smuzhiyun 	/*
3502*4882a593Smuzhiyun 	 * Consider queues with a higher finish virtual time than
3503*4882a593Smuzhiyun 	 * bfqq. If idling_needed_for_service_guarantees(bfqq) returns
3504*4882a593Smuzhiyun 	 * true, then bfqq's bandwidth would be violated if an
3505*4882a593Smuzhiyun 	 * uncontrolled amount of I/O from these queues were
3506*4882a593Smuzhiyun 	 * dispatched while bfqq is waiting for its new I/O to
3507*4882a593Smuzhiyun 	 * arrive. This is exactly what may happen if this is a forced
3508*4882a593Smuzhiyun 	 * expiration caused by a preemption attempt, and if bfqq is
3509*4882a593Smuzhiyun 	 * not re-scheduled. To prevent this from happening, re-queue
3510*4882a593Smuzhiyun 	 * bfqq if it needs I/O-dispatch plugging, even if it is
3511*4882a593Smuzhiyun 	 * empty. By doing so, bfqq is granted to be served before the
3512*4882a593Smuzhiyun 	 * above queues (provided that bfqq is of course eligible).
3513*4882a593Smuzhiyun 	 */
3514*4882a593Smuzhiyun 	if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
3515*4882a593Smuzhiyun 	    !(reason == BFQQE_PREEMPTED &&
3516*4882a593Smuzhiyun 	      idling_needed_for_service_guarantees(bfqd, bfqq))) {
3517*4882a593Smuzhiyun 		if (bfqq->dispatched == 0)
3518*4882a593Smuzhiyun 			/*
3519*4882a593Smuzhiyun 			 * Overloading budget_timeout field to store
3520*4882a593Smuzhiyun 			 * the time at which the queue remains with no
3521*4882a593Smuzhiyun 			 * backlog and no outstanding request; used by
3522*4882a593Smuzhiyun 			 * the weight-raising mechanism.
3523*4882a593Smuzhiyun 			 */
3524*4882a593Smuzhiyun 			bfqq->budget_timeout = jiffies;
3525*4882a593Smuzhiyun 
3526*4882a593Smuzhiyun 		bfq_del_bfqq_busy(bfqd, bfqq, true);
3527*4882a593Smuzhiyun 	} else {
3528*4882a593Smuzhiyun 		bfq_requeue_bfqq(bfqd, bfqq, true);
3529*4882a593Smuzhiyun 		/*
3530*4882a593Smuzhiyun 		 * Resort priority tree of potential close cooperators.
3531*4882a593Smuzhiyun 		 * See comments on bfq_pos_tree_add_move() for the unlikely().
3532*4882a593Smuzhiyun 		 */
3533*4882a593Smuzhiyun 		if (unlikely(!bfqd->nonrot_with_queueing &&
3534*4882a593Smuzhiyun 			     !RB_EMPTY_ROOT(&bfqq->sort_list)))
3535*4882a593Smuzhiyun 			bfq_pos_tree_add_move(bfqd, bfqq);
3536*4882a593Smuzhiyun 	}
3537*4882a593Smuzhiyun 
3538*4882a593Smuzhiyun 	/*
3539*4882a593Smuzhiyun 	 * All in-service entities must have been properly deactivated
3540*4882a593Smuzhiyun 	 * or requeued before executing the next function, which
3541*4882a593Smuzhiyun 	 * resets all in-service entities as no more in service. This
3542*4882a593Smuzhiyun 	 * may cause bfqq to be freed. If this happens, the next
3543*4882a593Smuzhiyun 	 * function returns true.
3544*4882a593Smuzhiyun 	 */
3545*4882a593Smuzhiyun 	return __bfq_bfqd_reset_in_service(bfqd);
3546*4882a593Smuzhiyun }
3547*4882a593Smuzhiyun 
3548*4882a593Smuzhiyun /**
3549*4882a593Smuzhiyun  * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
3550*4882a593Smuzhiyun  * @bfqd: device data.
3551*4882a593Smuzhiyun  * @bfqq: queue to update.
3552*4882a593Smuzhiyun  * @reason: reason for expiration.
3553*4882a593Smuzhiyun  *
3554*4882a593Smuzhiyun  * Handle the feedback on @bfqq budget at queue expiration.
3555*4882a593Smuzhiyun  * See the body for detailed comments.
3556*4882a593Smuzhiyun  */
__bfq_bfqq_recalc_budget(struct bfq_data * bfqd,struct bfq_queue * bfqq,enum bfqq_expiration reason)3557*4882a593Smuzhiyun static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
3558*4882a593Smuzhiyun 				     struct bfq_queue *bfqq,
3559*4882a593Smuzhiyun 				     enum bfqq_expiration reason)
3560*4882a593Smuzhiyun {
3561*4882a593Smuzhiyun 	struct request *next_rq;
3562*4882a593Smuzhiyun 	int budget, min_budget;
3563*4882a593Smuzhiyun 
3564*4882a593Smuzhiyun 	min_budget = bfq_min_budget(bfqd);
3565*4882a593Smuzhiyun 
3566*4882a593Smuzhiyun 	if (bfqq->wr_coeff == 1)
3567*4882a593Smuzhiyun 		budget = bfqq->max_budget;
3568*4882a593Smuzhiyun 	else /*
3569*4882a593Smuzhiyun 	      * Use a constant, low budget for weight-raised queues,
3570*4882a593Smuzhiyun 	      * to help achieve a low latency. Keep it slightly higher
3571*4882a593Smuzhiyun 	      * than the minimum possible budget, to cause a little
3572*4882a593Smuzhiyun 	      * bit fewer expirations.
3573*4882a593Smuzhiyun 	      */
3574*4882a593Smuzhiyun 		budget = 2 * min_budget;
3575*4882a593Smuzhiyun 
3576*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d",
3577*4882a593Smuzhiyun 		bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
3578*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d",
3579*4882a593Smuzhiyun 		budget, bfq_min_budget(bfqd));
3580*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
3581*4882a593Smuzhiyun 		bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
3582*4882a593Smuzhiyun 
3583*4882a593Smuzhiyun 	if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) {
3584*4882a593Smuzhiyun 		switch (reason) {
3585*4882a593Smuzhiyun 		/*
3586*4882a593Smuzhiyun 		 * Caveat: in all the following cases we trade latency
3587*4882a593Smuzhiyun 		 * for throughput.
3588*4882a593Smuzhiyun 		 */
3589*4882a593Smuzhiyun 		case BFQQE_TOO_IDLE:
3590*4882a593Smuzhiyun 			/*
3591*4882a593Smuzhiyun 			 * This is the only case where we may reduce
3592*4882a593Smuzhiyun 			 * the budget: if there is no request of the
3593*4882a593Smuzhiyun 			 * process still waiting for completion, then
3594*4882a593Smuzhiyun 			 * we assume (tentatively) that the timer has
3595*4882a593Smuzhiyun 			 * expired because the batch of requests of
3596*4882a593Smuzhiyun 			 * the process could have been served with a
3597*4882a593Smuzhiyun 			 * smaller budget.  Hence, betting that
3598*4882a593Smuzhiyun 			 * process will behave in the same way when it
3599*4882a593Smuzhiyun 			 * becomes backlogged again, we reduce its
3600*4882a593Smuzhiyun 			 * next budget.  As long as we guess right,
3601*4882a593Smuzhiyun 			 * this budget cut reduces the latency
3602*4882a593Smuzhiyun 			 * experienced by the process.
3603*4882a593Smuzhiyun 			 *
3604*4882a593Smuzhiyun 			 * However, if there are still outstanding
3605*4882a593Smuzhiyun 			 * requests, then the process may have not yet
3606*4882a593Smuzhiyun 			 * issued its next request just because it is
3607*4882a593Smuzhiyun 			 * still waiting for the completion of some of
3608*4882a593Smuzhiyun 			 * the still outstanding ones.  So in this
3609*4882a593Smuzhiyun 			 * subcase we do not reduce its budget, on the
3610*4882a593Smuzhiyun 			 * contrary we increase it to possibly boost
3611*4882a593Smuzhiyun 			 * the throughput, as discussed in the
3612*4882a593Smuzhiyun 			 * comments to the BUDGET_TIMEOUT case.
3613*4882a593Smuzhiyun 			 */
3614*4882a593Smuzhiyun 			if (bfqq->dispatched > 0) /* still outstanding reqs */
3615*4882a593Smuzhiyun 				budget = min(budget * 2, bfqd->bfq_max_budget);
3616*4882a593Smuzhiyun 			else {
3617*4882a593Smuzhiyun 				if (budget > 5 * min_budget)
3618*4882a593Smuzhiyun 					budget -= 4 * min_budget;
3619*4882a593Smuzhiyun 				else
3620*4882a593Smuzhiyun 					budget = min_budget;
3621*4882a593Smuzhiyun 			}
3622*4882a593Smuzhiyun 			break;
3623*4882a593Smuzhiyun 		case BFQQE_BUDGET_TIMEOUT:
3624*4882a593Smuzhiyun 			/*
3625*4882a593Smuzhiyun 			 * We double the budget here because it gives
3626*4882a593Smuzhiyun 			 * the chance to boost the throughput if this
3627*4882a593Smuzhiyun 			 * is not a seeky process (and has bumped into
3628*4882a593Smuzhiyun 			 * this timeout because of, e.g., ZBR).
3629*4882a593Smuzhiyun 			 */
3630*4882a593Smuzhiyun 			budget = min(budget * 2, bfqd->bfq_max_budget);
3631*4882a593Smuzhiyun 			break;
3632*4882a593Smuzhiyun 		case BFQQE_BUDGET_EXHAUSTED:
3633*4882a593Smuzhiyun 			/*
3634*4882a593Smuzhiyun 			 * The process still has backlog, and did not
3635*4882a593Smuzhiyun 			 * let either the budget timeout or the disk
3636*4882a593Smuzhiyun 			 * idling timeout expire. Hence it is not
3637*4882a593Smuzhiyun 			 * seeky, has a short thinktime and may be
3638*4882a593Smuzhiyun 			 * happy with a higher budget too. So
3639*4882a593Smuzhiyun 			 * definitely increase the budget of this good
3640*4882a593Smuzhiyun 			 * candidate to boost the disk throughput.
3641*4882a593Smuzhiyun 			 */
3642*4882a593Smuzhiyun 			budget = min(budget * 4, bfqd->bfq_max_budget);
3643*4882a593Smuzhiyun 			break;
3644*4882a593Smuzhiyun 		case BFQQE_NO_MORE_REQUESTS:
3645*4882a593Smuzhiyun 			/*
3646*4882a593Smuzhiyun 			 * For queues that expire for this reason, it
3647*4882a593Smuzhiyun 			 * is particularly important to keep the
3648*4882a593Smuzhiyun 			 * budget close to the actual service they
3649*4882a593Smuzhiyun 			 * need. Doing so reduces the timestamp
3650*4882a593Smuzhiyun 			 * misalignment problem described in the
3651*4882a593Smuzhiyun 			 * comments in the body of
3652*4882a593Smuzhiyun 			 * __bfq_activate_entity. In fact, suppose
3653*4882a593Smuzhiyun 			 * that a queue systematically expires for
3654*4882a593Smuzhiyun 			 * BFQQE_NO_MORE_REQUESTS and presents a
3655*4882a593Smuzhiyun 			 * new request in time to enjoy timestamp
3656*4882a593Smuzhiyun 			 * back-shifting. The larger the budget of the
3657*4882a593Smuzhiyun 			 * queue is with respect to the service the
3658*4882a593Smuzhiyun 			 * queue actually requests in each service
3659*4882a593Smuzhiyun 			 * slot, the more times the queue can be
3660*4882a593Smuzhiyun 			 * reactivated with the same virtual finish
3661*4882a593Smuzhiyun 			 * time. It follows that, even if this finish
3662*4882a593Smuzhiyun 			 * time is pushed to the system virtual time
3663*4882a593Smuzhiyun 			 * to reduce the consequent timestamp
3664*4882a593Smuzhiyun 			 * misalignment, the queue unjustly enjoys for
3665*4882a593Smuzhiyun 			 * many re-activations a lower finish time
3666*4882a593Smuzhiyun 			 * than all newly activated queues.
3667*4882a593Smuzhiyun 			 *
3668*4882a593Smuzhiyun 			 * The service needed by bfqq is measured
3669*4882a593Smuzhiyun 			 * quite precisely by bfqq->entity.service.
3670*4882a593Smuzhiyun 			 * Since bfqq does not enjoy device idling,
3671*4882a593Smuzhiyun 			 * bfqq->entity.service is equal to the number
3672*4882a593Smuzhiyun 			 * of sectors that the process associated with
3673*4882a593Smuzhiyun 			 * bfqq requested to read/write before waiting
3674*4882a593Smuzhiyun 			 * for request completions, or blocking for
3675*4882a593Smuzhiyun 			 * other reasons.
3676*4882a593Smuzhiyun 			 */
3677*4882a593Smuzhiyun 			budget = max_t(int, bfqq->entity.service, min_budget);
3678*4882a593Smuzhiyun 			break;
3679*4882a593Smuzhiyun 		default:
3680*4882a593Smuzhiyun 			return;
3681*4882a593Smuzhiyun 		}
3682*4882a593Smuzhiyun 	} else if (!bfq_bfqq_sync(bfqq)) {
3683*4882a593Smuzhiyun 		/*
3684*4882a593Smuzhiyun 		 * Async queues get always the maximum possible
3685*4882a593Smuzhiyun 		 * budget, as for them we do not care about latency
3686*4882a593Smuzhiyun 		 * (in addition, their ability to dispatch is limited
3687*4882a593Smuzhiyun 		 * by the charging factor).
3688*4882a593Smuzhiyun 		 */
3689*4882a593Smuzhiyun 		budget = bfqd->bfq_max_budget;
3690*4882a593Smuzhiyun 	}
3691*4882a593Smuzhiyun 
3692*4882a593Smuzhiyun 	bfqq->max_budget = budget;
3693*4882a593Smuzhiyun 
3694*4882a593Smuzhiyun 	if (bfqd->budgets_assigned >= bfq_stats_min_budgets &&
3695*4882a593Smuzhiyun 	    !bfqd->bfq_user_max_budget)
3696*4882a593Smuzhiyun 		bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget);
3697*4882a593Smuzhiyun 
3698*4882a593Smuzhiyun 	/*
3699*4882a593Smuzhiyun 	 * If there is still backlog, then assign a new budget, making
3700*4882a593Smuzhiyun 	 * sure that it is large enough for the next request.  Since
3701*4882a593Smuzhiyun 	 * the finish time of bfqq must be kept in sync with the
3702*4882a593Smuzhiyun 	 * budget, be sure to call __bfq_bfqq_expire() *after* this
3703*4882a593Smuzhiyun 	 * update.
3704*4882a593Smuzhiyun 	 *
3705*4882a593Smuzhiyun 	 * If there is no backlog, then no need to update the budget;
3706*4882a593Smuzhiyun 	 * it will be updated on the arrival of a new request.
3707*4882a593Smuzhiyun 	 */
3708*4882a593Smuzhiyun 	next_rq = bfqq->next_rq;
3709*4882a593Smuzhiyun 	if (next_rq)
3710*4882a593Smuzhiyun 		bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
3711*4882a593Smuzhiyun 					    bfq_serv_to_charge(next_rq, bfqq));
3712*4882a593Smuzhiyun 
3713*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d",
3714*4882a593Smuzhiyun 			next_rq ? blk_rq_sectors(next_rq) : 0,
3715*4882a593Smuzhiyun 			bfqq->entity.budget);
3716*4882a593Smuzhiyun }
3717*4882a593Smuzhiyun 
3718*4882a593Smuzhiyun /*
3719*4882a593Smuzhiyun  * Return true if the process associated with bfqq is "slow". The slow
3720*4882a593Smuzhiyun  * flag is used, in addition to the budget timeout, to reduce the
3721*4882a593Smuzhiyun  * amount of service provided to seeky processes, and thus reduce
3722*4882a593Smuzhiyun  * their chances to lower the throughput. More details in the comments
3723*4882a593Smuzhiyun  * on the function bfq_bfqq_expire().
3724*4882a593Smuzhiyun  *
3725*4882a593Smuzhiyun  * An important observation is in order: as discussed in the comments
3726*4882a593Smuzhiyun  * on the function bfq_update_peak_rate(), with devices with internal
3727*4882a593Smuzhiyun  * queues, it is hard if ever possible to know when and for how long
3728*4882a593Smuzhiyun  * an I/O request is processed by the device (apart from the trivial
3729*4882a593Smuzhiyun  * I/O pattern where a new request is dispatched only after the
3730*4882a593Smuzhiyun  * previous one has been completed). This makes it hard to evaluate
3731*4882a593Smuzhiyun  * the real rate at which the I/O requests of each bfq_queue are
3732*4882a593Smuzhiyun  * served.  In fact, for an I/O scheduler like BFQ, serving a
3733*4882a593Smuzhiyun  * bfq_queue means just dispatching its requests during its service
3734*4882a593Smuzhiyun  * slot (i.e., until the budget of the queue is exhausted, or the
3735*4882a593Smuzhiyun  * queue remains idle, or, finally, a timeout fires). But, during the
3736*4882a593Smuzhiyun  * service slot of a bfq_queue, around 100 ms at most, the device may
3737*4882a593Smuzhiyun  * be even still processing requests of bfq_queues served in previous
3738*4882a593Smuzhiyun  * service slots. On the opposite end, the requests of the in-service
3739*4882a593Smuzhiyun  * bfq_queue may be completed after the service slot of the queue
3740*4882a593Smuzhiyun  * finishes.
3741*4882a593Smuzhiyun  *
3742*4882a593Smuzhiyun  * Anyway, unless more sophisticated solutions are used
3743*4882a593Smuzhiyun  * (where possible), the sum of the sizes of the requests dispatched
3744*4882a593Smuzhiyun  * during the service slot of a bfq_queue is probably the only
3745*4882a593Smuzhiyun  * approximation available for the service received by the bfq_queue
3746*4882a593Smuzhiyun  * during its service slot. And this sum is the quantity used in this
3747*4882a593Smuzhiyun  * function to evaluate the I/O speed of a process.
3748*4882a593Smuzhiyun  */
bfq_bfqq_is_slow(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool compensate,enum bfqq_expiration reason,unsigned long * delta_ms)3749*4882a593Smuzhiyun static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq,
3750*4882a593Smuzhiyun 				 bool compensate, enum bfqq_expiration reason,
3751*4882a593Smuzhiyun 				 unsigned long *delta_ms)
3752*4882a593Smuzhiyun {
3753*4882a593Smuzhiyun 	ktime_t delta_ktime;
3754*4882a593Smuzhiyun 	u32 delta_usecs;
3755*4882a593Smuzhiyun 	bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */
3756*4882a593Smuzhiyun 
3757*4882a593Smuzhiyun 	if (!bfq_bfqq_sync(bfqq))
3758*4882a593Smuzhiyun 		return false;
3759*4882a593Smuzhiyun 
3760*4882a593Smuzhiyun 	if (compensate)
3761*4882a593Smuzhiyun 		delta_ktime = bfqd->last_idling_start;
3762*4882a593Smuzhiyun 	else
3763*4882a593Smuzhiyun 		delta_ktime = ktime_get();
3764*4882a593Smuzhiyun 	delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start);
3765*4882a593Smuzhiyun 	delta_usecs = ktime_to_us(delta_ktime);
3766*4882a593Smuzhiyun 
3767*4882a593Smuzhiyun 	/* don't use too short time intervals */
3768*4882a593Smuzhiyun 	if (delta_usecs < 1000) {
3769*4882a593Smuzhiyun 		if (blk_queue_nonrot(bfqd->queue))
3770*4882a593Smuzhiyun 			 /*
3771*4882a593Smuzhiyun 			  * give same worst-case guarantees as idling
3772*4882a593Smuzhiyun 			  * for seeky
3773*4882a593Smuzhiyun 			  */
3774*4882a593Smuzhiyun 			*delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC;
3775*4882a593Smuzhiyun 		else /* charge at least one seek */
3776*4882a593Smuzhiyun 			*delta_ms = bfq_slice_idle / NSEC_PER_MSEC;
3777*4882a593Smuzhiyun 
3778*4882a593Smuzhiyun 		return slow;
3779*4882a593Smuzhiyun 	}
3780*4882a593Smuzhiyun 
3781*4882a593Smuzhiyun 	*delta_ms = delta_usecs / USEC_PER_MSEC;
3782*4882a593Smuzhiyun 
3783*4882a593Smuzhiyun 	/*
3784*4882a593Smuzhiyun 	 * Use only long (> 20ms) intervals to filter out excessive
3785*4882a593Smuzhiyun 	 * spikes in service rate estimation.
3786*4882a593Smuzhiyun 	 */
3787*4882a593Smuzhiyun 	if (delta_usecs > 20000) {
3788*4882a593Smuzhiyun 		/*
3789*4882a593Smuzhiyun 		 * Caveat for rotational devices: processes doing I/O
3790*4882a593Smuzhiyun 		 * in the slower disk zones tend to be slow(er) even
3791*4882a593Smuzhiyun 		 * if not seeky. In this respect, the estimated peak
3792*4882a593Smuzhiyun 		 * rate is likely to be an average over the disk
3793*4882a593Smuzhiyun 		 * surface. Accordingly, to not be too harsh with
3794*4882a593Smuzhiyun 		 * unlucky processes, a process is deemed slow only if
3795*4882a593Smuzhiyun 		 * its rate has been lower than half of the estimated
3796*4882a593Smuzhiyun 		 * peak rate.
3797*4882a593Smuzhiyun 		 */
3798*4882a593Smuzhiyun 		slow = bfqq->entity.service < bfqd->bfq_max_budget / 2;
3799*4882a593Smuzhiyun 	}
3800*4882a593Smuzhiyun 
3801*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow);
3802*4882a593Smuzhiyun 
3803*4882a593Smuzhiyun 	return slow;
3804*4882a593Smuzhiyun }
3805*4882a593Smuzhiyun 
3806*4882a593Smuzhiyun /*
3807*4882a593Smuzhiyun  * To be deemed as soft real-time, an application must meet two
3808*4882a593Smuzhiyun  * requirements. First, the application must not require an average
3809*4882a593Smuzhiyun  * bandwidth higher than the approximate bandwidth required to playback or
3810*4882a593Smuzhiyun  * record a compressed high-definition video.
3811*4882a593Smuzhiyun  * The next function is invoked on the completion of the last request of a
3812*4882a593Smuzhiyun  * batch, to compute the next-start time instant, soft_rt_next_start, such
3813*4882a593Smuzhiyun  * that, if the next request of the application does not arrive before
3814*4882a593Smuzhiyun  * soft_rt_next_start, then the above requirement on the bandwidth is met.
3815*4882a593Smuzhiyun  *
3816*4882a593Smuzhiyun  * The second requirement is that the request pattern of the application is
3817*4882a593Smuzhiyun  * isochronous, i.e., that, after issuing a request or a batch of requests,
3818*4882a593Smuzhiyun  * the application stops issuing new requests until all its pending requests
3819*4882a593Smuzhiyun  * have been completed. After that, the application may issue a new batch,
3820*4882a593Smuzhiyun  * and so on.
3821*4882a593Smuzhiyun  * For this reason the next function is invoked to compute
3822*4882a593Smuzhiyun  * soft_rt_next_start only for applications that meet this requirement,
3823*4882a593Smuzhiyun  * whereas soft_rt_next_start is set to infinity for applications that do
3824*4882a593Smuzhiyun  * not.
3825*4882a593Smuzhiyun  *
3826*4882a593Smuzhiyun  * Unfortunately, even a greedy (i.e., I/O-bound) application may
3827*4882a593Smuzhiyun  * happen to meet, occasionally or systematically, both the above
3828*4882a593Smuzhiyun  * bandwidth and isochrony requirements. This may happen at least in
3829*4882a593Smuzhiyun  * the following circumstances. First, if the CPU load is high. The
3830*4882a593Smuzhiyun  * application may stop issuing requests while the CPUs are busy
3831*4882a593Smuzhiyun  * serving other processes, then restart, then stop again for a while,
3832*4882a593Smuzhiyun  * and so on. The other circumstances are related to the storage
3833*4882a593Smuzhiyun  * device: the storage device is highly loaded or reaches a low-enough
3834*4882a593Smuzhiyun  * throughput with the I/O of the application (e.g., because the I/O
3835*4882a593Smuzhiyun  * is random and/or the device is slow). In all these cases, the
3836*4882a593Smuzhiyun  * I/O of the application may be simply slowed down enough to meet
3837*4882a593Smuzhiyun  * the bandwidth and isochrony requirements. To reduce the probability
3838*4882a593Smuzhiyun  * that greedy applications are deemed as soft real-time in these
3839*4882a593Smuzhiyun  * corner cases, a further rule is used in the computation of
3840*4882a593Smuzhiyun  * soft_rt_next_start: the return value of this function is forced to
3841*4882a593Smuzhiyun  * be higher than the maximum between the following two quantities.
3842*4882a593Smuzhiyun  *
3843*4882a593Smuzhiyun  * (a) Current time plus: (1) the maximum time for which the arrival
3844*4882a593Smuzhiyun  *     of a request is waited for when a sync queue becomes idle,
3845*4882a593Smuzhiyun  *     namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
3846*4882a593Smuzhiyun  *     postpone for a moment the reason for adding a few extra
3847*4882a593Smuzhiyun  *     jiffies; we get back to it after next item (b).  Lower-bounding
3848*4882a593Smuzhiyun  *     the return value of this function with the current time plus
3849*4882a593Smuzhiyun  *     bfqd->bfq_slice_idle tends to filter out greedy applications,
3850*4882a593Smuzhiyun  *     because the latter issue their next request as soon as possible
3851*4882a593Smuzhiyun  *     after the last one has been completed. In contrast, a soft
3852*4882a593Smuzhiyun  *     real-time application spends some time processing data, after a
3853*4882a593Smuzhiyun  *     batch of its requests has been completed.
3854*4882a593Smuzhiyun  *
3855*4882a593Smuzhiyun  * (b) Current value of bfqq->soft_rt_next_start. As pointed out
3856*4882a593Smuzhiyun  *     above, greedy applications may happen to meet both the
3857*4882a593Smuzhiyun  *     bandwidth and isochrony requirements under heavy CPU or
3858*4882a593Smuzhiyun  *     storage-device load. In more detail, in these scenarios, these
3859*4882a593Smuzhiyun  *     applications happen, only for limited time periods, to do I/O
3860*4882a593Smuzhiyun  *     slowly enough to meet all the requirements described so far,
3861*4882a593Smuzhiyun  *     including the filtering in above item (a). These slow-speed
3862*4882a593Smuzhiyun  *     time intervals are usually interspersed between other time
3863*4882a593Smuzhiyun  *     intervals during which these applications do I/O at a very high
3864*4882a593Smuzhiyun  *     speed. Fortunately, exactly because of the high speed of the
3865*4882a593Smuzhiyun  *     I/O in the high-speed intervals, the values returned by this
3866*4882a593Smuzhiyun  *     function happen to be so high, near the end of any such
3867*4882a593Smuzhiyun  *     high-speed interval, to be likely to fall *after* the end of
3868*4882a593Smuzhiyun  *     the low-speed time interval that follows. These high values are
3869*4882a593Smuzhiyun  *     stored in bfqq->soft_rt_next_start after each invocation of
3870*4882a593Smuzhiyun  *     this function. As a consequence, if the last value of
3871*4882a593Smuzhiyun  *     bfqq->soft_rt_next_start is constantly used to lower-bound the
3872*4882a593Smuzhiyun  *     next value that this function may return, then, from the very
3873*4882a593Smuzhiyun  *     beginning of a low-speed interval, bfqq->soft_rt_next_start is
3874*4882a593Smuzhiyun  *     likely to be constantly kept so high that any I/O request
3875*4882a593Smuzhiyun  *     issued during the low-speed interval is considered as arriving
3876*4882a593Smuzhiyun  *     to soon for the application to be deemed as soft
3877*4882a593Smuzhiyun  *     real-time. Then, in the high-speed interval that follows, the
3878*4882a593Smuzhiyun  *     application will not be deemed as soft real-time, just because
3879*4882a593Smuzhiyun  *     it will do I/O at a high speed. And so on.
3880*4882a593Smuzhiyun  *
3881*4882a593Smuzhiyun  * Getting back to the filtering in item (a), in the following two
3882*4882a593Smuzhiyun  * cases this filtering might be easily passed by a greedy
3883*4882a593Smuzhiyun  * application, if the reference quantity was just
3884*4882a593Smuzhiyun  * bfqd->bfq_slice_idle:
3885*4882a593Smuzhiyun  * 1) HZ is so low that the duration of a jiffy is comparable to or
3886*4882a593Smuzhiyun  *    higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
3887*4882a593Smuzhiyun  *    devices with HZ=100. The time granularity may be so coarse
3888*4882a593Smuzhiyun  *    that the approximation, in jiffies, of bfqd->bfq_slice_idle
3889*4882a593Smuzhiyun  *    is rather lower than the exact value.
3890*4882a593Smuzhiyun  * 2) jiffies, instead of increasing at a constant rate, may stop increasing
3891*4882a593Smuzhiyun  *    for a while, then suddenly 'jump' by several units to recover the lost
3892*4882a593Smuzhiyun  *    increments. This seems to happen, e.g., inside virtual machines.
3893*4882a593Smuzhiyun  * To address this issue, in the filtering in (a) we do not use as a
3894*4882a593Smuzhiyun  * reference time interval just bfqd->bfq_slice_idle, but
3895*4882a593Smuzhiyun  * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
3896*4882a593Smuzhiyun  * minimum number of jiffies for which the filter seems to be quite
3897*4882a593Smuzhiyun  * precise also in embedded systems and KVM/QEMU virtual machines.
3898*4882a593Smuzhiyun  */
bfq_bfqq_softrt_next_start(struct bfq_data * bfqd,struct bfq_queue * bfqq)3899*4882a593Smuzhiyun static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
3900*4882a593Smuzhiyun 						struct bfq_queue *bfqq)
3901*4882a593Smuzhiyun {
3902*4882a593Smuzhiyun 	return max3(bfqq->soft_rt_next_start,
3903*4882a593Smuzhiyun 		    bfqq->last_idle_bklogged +
3904*4882a593Smuzhiyun 		    HZ * bfqq->service_from_backlogged /
3905*4882a593Smuzhiyun 		    bfqd->bfq_wr_max_softrt_rate,
3906*4882a593Smuzhiyun 		    jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4);
3907*4882a593Smuzhiyun }
3908*4882a593Smuzhiyun 
3909*4882a593Smuzhiyun /**
3910*4882a593Smuzhiyun  * bfq_bfqq_expire - expire a queue.
3911*4882a593Smuzhiyun  * @bfqd: device owning the queue.
3912*4882a593Smuzhiyun  * @bfqq: the queue to expire.
3913*4882a593Smuzhiyun  * @compensate: if true, compensate for the time spent idling.
3914*4882a593Smuzhiyun  * @reason: the reason causing the expiration.
3915*4882a593Smuzhiyun  *
3916*4882a593Smuzhiyun  * If the process associated with bfqq does slow I/O (e.g., because it
3917*4882a593Smuzhiyun  * issues random requests), we charge bfqq with the time it has been
3918*4882a593Smuzhiyun  * in service instead of the service it has received (see
3919*4882a593Smuzhiyun  * bfq_bfqq_charge_time for details on how this goal is achieved). As
3920*4882a593Smuzhiyun  * a consequence, bfqq will typically get higher timestamps upon
3921*4882a593Smuzhiyun  * reactivation, and hence it will be rescheduled as if it had
3922*4882a593Smuzhiyun  * received more service than what it has actually received. In the
3923*4882a593Smuzhiyun  * end, bfqq receives less service in proportion to how slowly its
3924*4882a593Smuzhiyun  * associated process consumes its budgets (and hence how seriously it
3925*4882a593Smuzhiyun  * tends to lower the throughput). In addition, this time-charging
3926*4882a593Smuzhiyun  * strategy guarantees time fairness among slow processes. In
3927*4882a593Smuzhiyun  * contrast, if the process associated with bfqq is not slow, we
3928*4882a593Smuzhiyun  * charge bfqq exactly with the service it has received.
3929*4882a593Smuzhiyun  *
3930*4882a593Smuzhiyun  * Charging time to the first type of queues and the exact service to
3931*4882a593Smuzhiyun  * the other has the effect of using the WF2Q+ policy to schedule the
3932*4882a593Smuzhiyun  * former on a timeslice basis, without violating service domain
3933*4882a593Smuzhiyun  * guarantees among the latter.
3934*4882a593Smuzhiyun  */
bfq_bfqq_expire(struct bfq_data * bfqd,struct bfq_queue * bfqq,bool compensate,enum bfqq_expiration reason)3935*4882a593Smuzhiyun void bfq_bfqq_expire(struct bfq_data *bfqd,
3936*4882a593Smuzhiyun 		     struct bfq_queue *bfqq,
3937*4882a593Smuzhiyun 		     bool compensate,
3938*4882a593Smuzhiyun 		     enum bfqq_expiration reason)
3939*4882a593Smuzhiyun {
3940*4882a593Smuzhiyun 	bool slow;
3941*4882a593Smuzhiyun 	unsigned long delta = 0;
3942*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
3943*4882a593Smuzhiyun 
3944*4882a593Smuzhiyun 	/*
3945*4882a593Smuzhiyun 	 * Check whether the process is slow (see bfq_bfqq_is_slow).
3946*4882a593Smuzhiyun 	 */
3947*4882a593Smuzhiyun 	slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta);
3948*4882a593Smuzhiyun 
3949*4882a593Smuzhiyun 	/*
3950*4882a593Smuzhiyun 	 * As above explained, charge slow (typically seeky) and
3951*4882a593Smuzhiyun 	 * timed-out queues with the time and not the service
3952*4882a593Smuzhiyun 	 * received, to favor sequential workloads.
3953*4882a593Smuzhiyun 	 *
3954*4882a593Smuzhiyun 	 * Processes doing I/O in the slower disk zones will tend to
3955*4882a593Smuzhiyun 	 * be slow(er) even if not seeky. Therefore, since the
3956*4882a593Smuzhiyun 	 * estimated peak rate is actually an average over the disk
3957*4882a593Smuzhiyun 	 * surface, these processes may timeout just for bad luck. To
3958*4882a593Smuzhiyun 	 * avoid punishing them, do not charge time to processes that
3959*4882a593Smuzhiyun 	 * succeeded in consuming at least 2/3 of their budget. This
3960*4882a593Smuzhiyun 	 * allows BFQ to preserve enough elasticity to still perform
3961*4882a593Smuzhiyun 	 * bandwidth, and not time, distribution with little unlucky
3962*4882a593Smuzhiyun 	 * or quasi-sequential processes.
3963*4882a593Smuzhiyun 	 */
3964*4882a593Smuzhiyun 	if (bfqq->wr_coeff == 1 &&
3965*4882a593Smuzhiyun 	    (slow ||
3966*4882a593Smuzhiyun 	     (reason == BFQQE_BUDGET_TIMEOUT &&
3967*4882a593Smuzhiyun 	      bfq_bfqq_budget_left(bfqq) >=  entity->budget / 3)))
3968*4882a593Smuzhiyun 		bfq_bfqq_charge_time(bfqd, bfqq, delta);
3969*4882a593Smuzhiyun 
3970*4882a593Smuzhiyun 	if (reason == BFQQE_TOO_IDLE &&
3971*4882a593Smuzhiyun 	    entity->service <= 2 * entity->budget / 10)
3972*4882a593Smuzhiyun 		bfq_clear_bfqq_IO_bound(bfqq);
3973*4882a593Smuzhiyun 
3974*4882a593Smuzhiyun 	if (bfqd->low_latency && bfqq->wr_coeff == 1)
3975*4882a593Smuzhiyun 		bfqq->last_wr_start_finish = jiffies;
3976*4882a593Smuzhiyun 
3977*4882a593Smuzhiyun 	if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
3978*4882a593Smuzhiyun 	    RB_EMPTY_ROOT(&bfqq->sort_list)) {
3979*4882a593Smuzhiyun 		/*
3980*4882a593Smuzhiyun 		 * If we get here, and there are no outstanding
3981*4882a593Smuzhiyun 		 * requests, then the request pattern is isochronous
3982*4882a593Smuzhiyun 		 * (see the comments on the function
3983*4882a593Smuzhiyun 		 * bfq_bfqq_softrt_next_start()). Thus we can compute
3984*4882a593Smuzhiyun 		 * soft_rt_next_start. And we do it, unless bfqq is in
3985*4882a593Smuzhiyun 		 * interactive weight raising. We do not do it in the
3986*4882a593Smuzhiyun 		 * latter subcase, for the following reason. bfqq may
3987*4882a593Smuzhiyun 		 * be conveying the I/O needed to load a soft
3988*4882a593Smuzhiyun 		 * real-time application. Such an application will
3989*4882a593Smuzhiyun 		 * actually exhibit a soft real-time I/O pattern after
3990*4882a593Smuzhiyun 		 * it finally starts doing its job. But, if
3991*4882a593Smuzhiyun 		 * soft_rt_next_start is computed here for an
3992*4882a593Smuzhiyun 		 * interactive bfqq, and bfqq had received a lot of
3993*4882a593Smuzhiyun 		 * service before remaining with no outstanding
3994*4882a593Smuzhiyun 		 * request (likely to happen on a fast device), then
3995*4882a593Smuzhiyun 		 * soft_rt_next_start would be assigned such a high
3996*4882a593Smuzhiyun 		 * value that, for a very long time, bfqq would be
3997*4882a593Smuzhiyun 		 * prevented from being possibly considered as soft
3998*4882a593Smuzhiyun 		 * real time.
3999*4882a593Smuzhiyun 		 *
4000*4882a593Smuzhiyun 		 * If, instead, the queue still has outstanding
4001*4882a593Smuzhiyun 		 * requests, then we have to wait for the completion
4002*4882a593Smuzhiyun 		 * of all the outstanding requests to discover whether
4003*4882a593Smuzhiyun 		 * the request pattern is actually isochronous.
4004*4882a593Smuzhiyun 		 */
4005*4882a593Smuzhiyun 		if (bfqq->dispatched == 0 &&
4006*4882a593Smuzhiyun 		    bfqq->wr_coeff != bfqd->bfq_wr_coeff)
4007*4882a593Smuzhiyun 			bfqq->soft_rt_next_start =
4008*4882a593Smuzhiyun 				bfq_bfqq_softrt_next_start(bfqd, bfqq);
4009*4882a593Smuzhiyun 		else if (bfqq->dispatched > 0) {
4010*4882a593Smuzhiyun 			/*
4011*4882a593Smuzhiyun 			 * Schedule an update of soft_rt_next_start to when
4012*4882a593Smuzhiyun 			 * the task may be discovered to be isochronous.
4013*4882a593Smuzhiyun 			 */
4014*4882a593Smuzhiyun 			bfq_mark_bfqq_softrt_update(bfqq);
4015*4882a593Smuzhiyun 		}
4016*4882a593Smuzhiyun 	}
4017*4882a593Smuzhiyun 
4018*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq,
4019*4882a593Smuzhiyun 		"expire (%d, slow %d, num_disp %d, short_ttime %d)", reason,
4020*4882a593Smuzhiyun 		slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq));
4021*4882a593Smuzhiyun 
4022*4882a593Smuzhiyun 	/*
4023*4882a593Smuzhiyun 	 * bfqq expired, so no total service time needs to be computed
4024*4882a593Smuzhiyun 	 * any longer: reset state machine for measuring total service
4025*4882a593Smuzhiyun 	 * times.
4026*4882a593Smuzhiyun 	 */
4027*4882a593Smuzhiyun 	bfqd->rqs_injected = bfqd->wait_dispatch = false;
4028*4882a593Smuzhiyun 	bfqd->waited_rq = NULL;
4029*4882a593Smuzhiyun 
4030*4882a593Smuzhiyun 	/*
4031*4882a593Smuzhiyun 	 * Increase, decrease or leave budget unchanged according to
4032*4882a593Smuzhiyun 	 * reason.
4033*4882a593Smuzhiyun 	 */
4034*4882a593Smuzhiyun 	__bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
4035*4882a593Smuzhiyun 	if (__bfq_bfqq_expire(bfqd, bfqq, reason))
4036*4882a593Smuzhiyun 		/* bfqq is gone, no more actions on it */
4037*4882a593Smuzhiyun 		return;
4038*4882a593Smuzhiyun 
4039*4882a593Smuzhiyun 	/* mark bfqq as waiting a request only if a bic still points to it */
4040*4882a593Smuzhiyun 	if (!bfq_bfqq_busy(bfqq) &&
4041*4882a593Smuzhiyun 	    reason != BFQQE_BUDGET_TIMEOUT &&
4042*4882a593Smuzhiyun 	    reason != BFQQE_BUDGET_EXHAUSTED) {
4043*4882a593Smuzhiyun 		bfq_mark_bfqq_non_blocking_wait_rq(bfqq);
4044*4882a593Smuzhiyun 		/*
4045*4882a593Smuzhiyun 		 * Not setting service to 0, because, if the next rq
4046*4882a593Smuzhiyun 		 * arrives in time, the queue will go on receiving
4047*4882a593Smuzhiyun 		 * service with this same budget (as if it never expired)
4048*4882a593Smuzhiyun 		 */
4049*4882a593Smuzhiyun 	} else
4050*4882a593Smuzhiyun 		entity->service = 0;
4051*4882a593Smuzhiyun 
4052*4882a593Smuzhiyun 	/*
4053*4882a593Smuzhiyun 	 * Reset the received-service counter for every parent entity.
4054*4882a593Smuzhiyun 	 * Differently from what happens with bfqq->entity.service,
4055*4882a593Smuzhiyun 	 * the resetting of this counter never needs to be postponed
4056*4882a593Smuzhiyun 	 * for parent entities. In fact, in case bfqq may have a
4057*4882a593Smuzhiyun 	 * chance to go on being served using the last, partially
4058*4882a593Smuzhiyun 	 * consumed budget, bfqq->entity.service needs to be kept,
4059*4882a593Smuzhiyun 	 * because if bfqq then actually goes on being served using
4060*4882a593Smuzhiyun 	 * the same budget, the last value of bfqq->entity.service is
4061*4882a593Smuzhiyun 	 * needed to properly decrement bfqq->entity.budget by the
4062*4882a593Smuzhiyun 	 * portion already consumed. In contrast, it is not necessary
4063*4882a593Smuzhiyun 	 * to keep entity->service for parent entities too, because
4064*4882a593Smuzhiyun 	 * the bubble up of the new value of bfqq->entity.budget will
4065*4882a593Smuzhiyun 	 * make sure that the budgets of parent entities are correct,
4066*4882a593Smuzhiyun 	 * even in case bfqq and thus parent entities go on receiving
4067*4882a593Smuzhiyun 	 * service with the same budget.
4068*4882a593Smuzhiyun 	 */
4069*4882a593Smuzhiyun 	entity = entity->parent;
4070*4882a593Smuzhiyun 	for_each_entity(entity)
4071*4882a593Smuzhiyun 		entity->service = 0;
4072*4882a593Smuzhiyun }
4073*4882a593Smuzhiyun 
4074*4882a593Smuzhiyun /*
4075*4882a593Smuzhiyun  * Budget timeout is not implemented through a dedicated timer, but
4076*4882a593Smuzhiyun  * just checked on request arrivals and completions, as well as on
4077*4882a593Smuzhiyun  * idle timer expirations.
4078*4882a593Smuzhiyun  */
bfq_bfqq_budget_timeout(struct bfq_queue * bfqq)4079*4882a593Smuzhiyun static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
4080*4882a593Smuzhiyun {
4081*4882a593Smuzhiyun 	return time_is_before_eq_jiffies(bfqq->budget_timeout);
4082*4882a593Smuzhiyun }
4083*4882a593Smuzhiyun 
4084*4882a593Smuzhiyun /*
4085*4882a593Smuzhiyun  * If we expire a queue that is actively waiting (i.e., with the
4086*4882a593Smuzhiyun  * device idled) for the arrival of a new request, then we may incur
4087*4882a593Smuzhiyun  * the timestamp misalignment problem described in the body of the
4088*4882a593Smuzhiyun  * function __bfq_activate_entity. Hence we return true only if this
4089*4882a593Smuzhiyun  * condition does not hold, or if the queue is slow enough to deserve
4090*4882a593Smuzhiyun  * only to be kicked off for preserving a high throughput.
4091*4882a593Smuzhiyun  */
bfq_may_expire_for_budg_timeout(struct bfq_queue * bfqq)4092*4882a593Smuzhiyun static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
4093*4882a593Smuzhiyun {
4094*4882a593Smuzhiyun 	bfq_log_bfqq(bfqq->bfqd, bfqq,
4095*4882a593Smuzhiyun 		"may_budget_timeout: wait_request %d left %d timeout %d",
4096*4882a593Smuzhiyun 		bfq_bfqq_wait_request(bfqq),
4097*4882a593Smuzhiyun 			bfq_bfqq_budget_left(bfqq) >=  bfqq->entity.budget / 3,
4098*4882a593Smuzhiyun 		bfq_bfqq_budget_timeout(bfqq));
4099*4882a593Smuzhiyun 
4100*4882a593Smuzhiyun 	return (!bfq_bfqq_wait_request(bfqq) ||
4101*4882a593Smuzhiyun 		bfq_bfqq_budget_left(bfqq) >=  bfqq->entity.budget / 3)
4102*4882a593Smuzhiyun 		&&
4103*4882a593Smuzhiyun 		bfq_bfqq_budget_timeout(bfqq);
4104*4882a593Smuzhiyun }
4105*4882a593Smuzhiyun 
idling_boosts_thr_without_issues(struct bfq_data * bfqd,struct bfq_queue * bfqq)4106*4882a593Smuzhiyun static bool idling_boosts_thr_without_issues(struct bfq_data *bfqd,
4107*4882a593Smuzhiyun 					     struct bfq_queue *bfqq)
4108*4882a593Smuzhiyun {
4109*4882a593Smuzhiyun 	bool rot_without_queueing =
4110*4882a593Smuzhiyun 		!blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag,
4111*4882a593Smuzhiyun 		bfqq_sequential_and_IO_bound,
4112*4882a593Smuzhiyun 		idling_boosts_thr;
4113*4882a593Smuzhiyun 
4114*4882a593Smuzhiyun 	/* No point in idling for bfqq if it won't get requests any longer */
4115*4882a593Smuzhiyun 	if (unlikely(!bfqq_process_refs(bfqq)))
4116*4882a593Smuzhiyun 		return false;
4117*4882a593Smuzhiyun 
4118*4882a593Smuzhiyun 	bfqq_sequential_and_IO_bound = !BFQQ_SEEKY(bfqq) &&
4119*4882a593Smuzhiyun 		bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_has_short_ttime(bfqq);
4120*4882a593Smuzhiyun 
4121*4882a593Smuzhiyun 	/*
4122*4882a593Smuzhiyun 	 * The next variable takes into account the cases where idling
4123*4882a593Smuzhiyun 	 * boosts the throughput.
4124*4882a593Smuzhiyun 	 *
4125*4882a593Smuzhiyun 	 * The value of the variable is computed considering, first, that
4126*4882a593Smuzhiyun 	 * idling is virtually always beneficial for the throughput if:
4127*4882a593Smuzhiyun 	 * (a) the device is not NCQ-capable and rotational, or
4128*4882a593Smuzhiyun 	 * (b) regardless of the presence of NCQ, the device is rotational and
4129*4882a593Smuzhiyun 	 *     the request pattern for bfqq is I/O-bound and sequential, or
4130*4882a593Smuzhiyun 	 * (c) regardless of whether it is rotational, the device is
4131*4882a593Smuzhiyun 	 *     not NCQ-capable and the request pattern for bfqq is
4132*4882a593Smuzhiyun 	 *     I/O-bound and sequential.
4133*4882a593Smuzhiyun 	 *
4134*4882a593Smuzhiyun 	 * Secondly, and in contrast to the above item (b), idling an
4135*4882a593Smuzhiyun 	 * NCQ-capable flash-based device would not boost the
4136*4882a593Smuzhiyun 	 * throughput even with sequential I/O; rather it would lower
4137*4882a593Smuzhiyun 	 * the throughput in proportion to how fast the device
4138*4882a593Smuzhiyun 	 * is. Accordingly, the next variable is true if any of the
4139*4882a593Smuzhiyun 	 * above conditions (a), (b) or (c) is true, and, in
4140*4882a593Smuzhiyun 	 * particular, happens to be false if bfqd is an NCQ-capable
4141*4882a593Smuzhiyun 	 * flash-based device.
4142*4882a593Smuzhiyun 	 */
4143*4882a593Smuzhiyun 	idling_boosts_thr = rot_without_queueing ||
4144*4882a593Smuzhiyun 		((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) &&
4145*4882a593Smuzhiyun 		 bfqq_sequential_and_IO_bound);
4146*4882a593Smuzhiyun 
4147*4882a593Smuzhiyun 	/*
4148*4882a593Smuzhiyun 	 * The return value of this function is equal to that of
4149*4882a593Smuzhiyun 	 * idling_boosts_thr, unless a special case holds. In this
4150*4882a593Smuzhiyun 	 * special case, described below, idling may cause problems to
4151*4882a593Smuzhiyun 	 * weight-raised queues.
4152*4882a593Smuzhiyun 	 *
4153*4882a593Smuzhiyun 	 * When the request pool is saturated (e.g., in the presence
4154*4882a593Smuzhiyun 	 * of write hogs), if the processes associated with
4155*4882a593Smuzhiyun 	 * non-weight-raised queues ask for requests at a lower rate,
4156*4882a593Smuzhiyun 	 * then processes associated with weight-raised queues have a
4157*4882a593Smuzhiyun 	 * higher probability to get a request from the pool
4158*4882a593Smuzhiyun 	 * immediately (or at least soon) when they need one. Thus
4159*4882a593Smuzhiyun 	 * they have a higher probability to actually get a fraction
4160*4882a593Smuzhiyun 	 * of the device throughput proportional to their high
4161*4882a593Smuzhiyun 	 * weight. This is especially true with NCQ-capable drives,
4162*4882a593Smuzhiyun 	 * which enqueue several requests in advance, and further
4163*4882a593Smuzhiyun 	 * reorder internally-queued requests.
4164*4882a593Smuzhiyun 	 *
4165*4882a593Smuzhiyun 	 * For this reason, we force to false the return value if
4166*4882a593Smuzhiyun 	 * there are weight-raised busy queues. In this case, and if
4167*4882a593Smuzhiyun 	 * bfqq is not weight-raised, this guarantees that the device
4168*4882a593Smuzhiyun 	 * is not idled for bfqq (if, instead, bfqq is weight-raised,
4169*4882a593Smuzhiyun 	 * then idling will be guaranteed by another variable, see
4170*4882a593Smuzhiyun 	 * below). Combined with the timestamping rules of BFQ (see
4171*4882a593Smuzhiyun 	 * [1] for details), this behavior causes bfqq, and hence any
4172*4882a593Smuzhiyun 	 * sync non-weight-raised queue, to get a lower number of
4173*4882a593Smuzhiyun 	 * requests served, and thus to ask for a lower number of
4174*4882a593Smuzhiyun 	 * requests from the request pool, before the busy
4175*4882a593Smuzhiyun 	 * weight-raised queues get served again. This often mitigates
4176*4882a593Smuzhiyun 	 * starvation problems in the presence of heavy write
4177*4882a593Smuzhiyun 	 * workloads and NCQ, thereby guaranteeing a higher
4178*4882a593Smuzhiyun 	 * application and system responsiveness in these hostile
4179*4882a593Smuzhiyun 	 * scenarios.
4180*4882a593Smuzhiyun 	 */
4181*4882a593Smuzhiyun 	return idling_boosts_thr &&
4182*4882a593Smuzhiyun 		bfqd->wr_busy_queues == 0;
4183*4882a593Smuzhiyun }
4184*4882a593Smuzhiyun 
4185*4882a593Smuzhiyun /*
4186*4882a593Smuzhiyun  * For a queue that becomes empty, device idling is allowed only if
4187*4882a593Smuzhiyun  * this function returns true for that queue. As a consequence, since
4188*4882a593Smuzhiyun  * device idling plays a critical role for both throughput boosting
4189*4882a593Smuzhiyun  * and service guarantees, the return value of this function plays a
4190*4882a593Smuzhiyun  * critical role as well.
4191*4882a593Smuzhiyun  *
4192*4882a593Smuzhiyun  * In a nutshell, this function returns true only if idling is
4193*4882a593Smuzhiyun  * beneficial for throughput or, even if detrimental for throughput,
4194*4882a593Smuzhiyun  * idling is however necessary to preserve service guarantees (low
4195*4882a593Smuzhiyun  * latency, desired throughput distribution, ...). In particular, on
4196*4882a593Smuzhiyun  * NCQ-capable devices, this function tries to return false, so as to
4197*4882a593Smuzhiyun  * help keep the drives' internal queues full, whenever this helps the
4198*4882a593Smuzhiyun  * device boost the throughput without causing any service-guarantee
4199*4882a593Smuzhiyun  * issue.
4200*4882a593Smuzhiyun  *
4201*4882a593Smuzhiyun  * Most of the issues taken into account to get the return value of
4202*4882a593Smuzhiyun  * this function are not trivial. We discuss these issues in the two
4203*4882a593Smuzhiyun  * functions providing the main pieces of information needed by this
4204*4882a593Smuzhiyun  * function.
4205*4882a593Smuzhiyun  */
bfq_better_to_idle(struct bfq_queue * bfqq)4206*4882a593Smuzhiyun static bool bfq_better_to_idle(struct bfq_queue *bfqq)
4207*4882a593Smuzhiyun {
4208*4882a593Smuzhiyun 	struct bfq_data *bfqd = bfqq->bfqd;
4209*4882a593Smuzhiyun 	bool idling_boosts_thr_with_no_issue, idling_needed_for_service_guar;
4210*4882a593Smuzhiyun 
4211*4882a593Smuzhiyun 	/* No point in idling for bfqq if it won't get requests any longer */
4212*4882a593Smuzhiyun 	if (unlikely(!bfqq_process_refs(bfqq)))
4213*4882a593Smuzhiyun 		return false;
4214*4882a593Smuzhiyun 
4215*4882a593Smuzhiyun 	if (unlikely(bfqd->strict_guarantees))
4216*4882a593Smuzhiyun 		return true;
4217*4882a593Smuzhiyun 
4218*4882a593Smuzhiyun 	/*
4219*4882a593Smuzhiyun 	 * Idling is performed only if slice_idle > 0. In addition, we
4220*4882a593Smuzhiyun 	 * do not idle if
4221*4882a593Smuzhiyun 	 * (a) bfqq is async
4222*4882a593Smuzhiyun 	 * (b) bfqq is in the idle io prio class: in this case we do
4223*4882a593Smuzhiyun 	 * not idle because we want to minimize the bandwidth that
4224*4882a593Smuzhiyun 	 * queues in this class can steal to higher-priority queues
4225*4882a593Smuzhiyun 	 */
4226*4882a593Smuzhiyun 	if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) ||
4227*4882a593Smuzhiyun 	   bfq_class_idle(bfqq))
4228*4882a593Smuzhiyun 		return false;
4229*4882a593Smuzhiyun 
4230*4882a593Smuzhiyun 	idling_boosts_thr_with_no_issue =
4231*4882a593Smuzhiyun 		idling_boosts_thr_without_issues(bfqd, bfqq);
4232*4882a593Smuzhiyun 
4233*4882a593Smuzhiyun 	idling_needed_for_service_guar =
4234*4882a593Smuzhiyun 		idling_needed_for_service_guarantees(bfqd, bfqq);
4235*4882a593Smuzhiyun 
4236*4882a593Smuzhiyun 	/*
4237*4882a593Smuzhiyun 	 * We have now the two components we need to compute the
4238*4882a593Smuzhiyun 	 * return value of the function, which is true only if idling
4239*4882a593Smuzhiyun 	 * either boosts the throughput (without issues), or is
4240*4882a593Smuzhiyun 	 * necessary to preserve service guarantees.
4241*4882a593Smuzhiyun 	 */
4242*4882a593Smuzhiyun 	return idling_boosts_thr_with_no_issue ||
4243*4882a593Smuzhiyun 		idling_needed_for_service_guar;
4244*4882a593Smuzhiyun }
4245*4882a593Smuzhiyun 
4246*4882a593Smuzhiyun /*
4247*4882a593Smuzhiyun  * If the in-service queue is empty but the function bfq_better_to_idle
4248*4882a593Smuzhiyun  * returns true, then:
4249*4882a593Smuzhiyun  * 1) the queue must remain in service and cannot be expired, and
4250*4882a593Smuzhiyun  * 2) the device must be idled to wait for the possible arrival of a new
4251*4882a593Smuzhiyun  *    request for the queue.
4252*4882a593Smuzhiyun  * See the comments on the function bfq_better_to_idle for the reasons
4253*4882a593Smuzhiyun  * why performing device idling is the best choice to boost the throughput
4254*4882a593Smuzhiyun  * and preserve service guarantees when bfq_better_to_idle itself
4255*4882a593Smuzhiyun  * returns true.
4256*4882a593Smuzhiyun  */
bfq_bfqq_must_idle(struct bfq_queue * bfqq)4257*4882a593Smuzhiyun static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
4258*4882a593Smuzhiyun {
4259*4882a593Smuzhiyun 	return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq);
4260*4882a593Smuzhiyun }
4261*4882a593Smuzhiyun 
4262*4882a593Smuzhiyun /*
4263*4882a593Smuzhiyun  * This function chooses the queue from which to pick the next extra
4264*4882a593Smuzhiyun  * I/O request to inject, if it finds a compatible queue. See the
4265*4882a593Smuzhiyun  * comments on bfq_update_inject_limit() for details on the injection
4266*4882a593Smuzhiyun  * mechanism, and for the definitions of the quantities mentioned
4267*4882a593Smuzhiyun  * below.
4268*4882a593Smuzhiyun  */
4269*4882a593Smuzhiyun static struct bfq_queue *
bfq_choose_bfqq_for_injection(struct bfq_data * bfqd)4270*4882a593Smuzhiyun bfq_choose_bfqq_for_injection(struct bfq_data *bfqd)
4271*4882a593Smuzhiyun {
4272*4882a593Smuzhiyun 	struct bfq_queue *bfqq, *in_serv_bfqq = bfqd->in_service_queue;
4273*4882a593Smuzhiyun 	unsigned int limit = in_serv_bfqq->inject_limit;
4274*4882a593Smuzhiyun 	/*
4275*4882a593Smuzhiyun 	 * If
4276*4882a593Smuzhiyun 	 * - bfqq is not weight-raised and therefore does not carry
4277*4882a593Smuzhiyun 	 *   time-critical I/O,
4278*4882a593Smuzhiyun 	 * or
4279*4882a593Smuzhiyun 	 * - regardless of whether bfqq is weight-raised, bfqq has
4280*4882a593Smuzhiyun 	 *   however a long think time, during which it can absorb the
4281*4882a593Smuzhiyun 	 *   effect of an appropriate number of extra I/O requests
4282*4882a593Smuzhiyun 	 *   from other queues (see bfq_update_inject_limit for
4283*4882a593Smuzhiyun 	 *   details on the computation of this number);
4284*4882a593Smuzhiyun 	 * then injection can be performed without restrictions.
4285*4882a593Smuzhiyun 	 */
4286*4882a593Smuzhiyun 	bool in_serv_always_inject = in_serv_bfqq->wr_coeff == 1 ||
4287*4882a593Smuzhiyun 		!bfq_bfqq_has_short_ttime(in_serv_bfqq);
4288*4882a593Smuzhiyun 
4289*4882a593Smuzhiyun 	/*
4290*4882a593Smuzhiyun 	 * If
4291*4882a593Smuzhiyun 	 * - the baseline total service time could not be sampled yet,
4292*4882a593Smuzhiyun 	 *   so the inject limit happens to be still 0, and
4293*4882a593Smuzhiyun 	 * - a lot of time has elapsed since the plugging of I/O
4294*4882a593Smuzhiyun 	 *   dispatching started, so drive speed is being wasted
4295*4882a593Smuzhiyun 	 *   significantly;
4296*4882a593Smuzhiyun 	 * then temporarily raise inject limit to one request.
4297*4882a593Smuzhiyun 	 */
4298*4882a593Smuzhiyun 	if (limit == 0 && in_serv_bfqq->last_serv_time_ns == 0 &&
4299*4882a593Smuzhiyun 	    bfq_bfqq_wait_request(in_serv_bfqq) &&
4300*4882a593Smuzhiyun 	    time_is_before_eq_jiffies(bfqd->last_idling_start_jiffies +
4301*4882a593Smuzhiyun 				      bfqd->bfq_slice_idle)
4302*4882a593Smuzhiyun 		)
4303*4882a593Smuzhiyun 		limit = 1;
4304*4882a593Smuzhiyun 
4305*4882a593Smuzhiyun 	if (bfqd->rq_in_driver >= limit)
4306*4882a593Smuzhiyun 		return NULL;
4307*4882a593Smuzhiyun 
4308*4882a593Smuzhiyun 	/*
4309*4882a593Smuzhiyun 	 * Linear search of the source queue for injection; but, with
4310*4882a593Smuzhiyun 	 * a high probability, very few steps are needed to find a
4311*4882a593Smuzhiyun 	 * candidate queue, i.e., a queue with enough budget left for
4312*4882a593Smuzhiyun 	 * its next request. In fact:
4313*4882a593Smuzhiyun 	 * - BFQ dynamically updates the budget of every queue so as
4314*4882a593Smuzhiyun 	 *   to accommodate the expected backlog of the queue;
4315*4882a593Smuzhiyun 	 * - if a queue gets all its requests dispatched as injected
4316*4882a593Smuzhiyun 	 *   service, then the queue is removed from the active list
4317*4882a593Smuzhiyun 	 *   (and re-added only if it gets new requests, but then it
4318*4882a593Smuzhiyun 	 *   is assigned again enough budget for its new backlog).
4319*4882a593Smuzhiyun 	 */
4320*4882a593Smuzhiyun 	list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
4321*4882a593Smuzhiyun 		if (!RB_EMPTY_ROOT(&bfqq->sort_list) &&
4322*4882a593Smuzhiyun 		    (in_serv_always_inject || bfqq->wr_coeff > 1) &&
4323*4882a593Smuzhiyun 		    bfq_serv_to_charge(bfqq->next_rq, bfqq) <=
4324*4882a593Smuzhiyun 		    bfq_bfqq_budget_left(bfqq)) {
4325*4882a593Smuzhiyun 			/*
4326*4882a593Smuzhiyun 			 * Allow for only one large in-flight request
4327*4882a593Smuzhiyun 			 * on non-rotational devices, for the
4328*4882a593Smuzhiyun 			 * following reason. On non-rotationl drives,
4329*4882a593Smuzhiyun 			 * large requests take much longer than
4330*4882a593Smuzhiyun 			 * smaller requests to be served. In addition,
4331*4882a593Smuzhiyun 			 * the drive prefers to serve large requests
4332*4882a593Smuzhiyun 			 * w.r.t. to small ones, if it can choose. So,
4333*4882a593Smuzhiyun 			 * having more than one large requests queued
4334*4882a593Smuzhiyun 			 * in the drive may easily make the next first
4335*4882a593Smuzhiyun 			 * request of the in-service queue wait for so
4336*4882a593Smuzhiyun 			 * long to break bfqq's service guarantees. On
4337*4882a593Smuzhiyun 			 * the bright side, large requests let the
4338*4882a593Smuzhiyun 			 * drive reach a very high throughput, even if
4339*4882a593Smuzhiyun 			 * there is only one in-flight large request
4340*4882a593Smuzhiyun 			 * at a time.
4341*4882a593Smuzhiyun 			 */
4342*4882a593Smuzhiyun 			if (blk_queue_nonrot(bfqd->queue) &&
4343*4882a593Smuzhiyun 			    blk_rq_sectors(bfqq->next_rq) >=
4344*4882a593Smuzhiyun 			    BFQQ_SECT_THR_NONROT)
4345*4882a593Smuzhiyun 				limit = min_t(unsigned int, 1, limit);
4346*4882a593Smuzhiyun 			else
4347*4882a593Smuzhiyun 				limit = in_serv_bfqq->inject_limit;
4348*4882a593Smuzhiyun 
4349*4882a593Smuzhiyun 			if (bfqd->rq_in_driver < limit) {
4350*4882a593Smuzhiyun 				bfqd->rqs_injected = true;
4351*4882a593Smuzhiyun 				return bfqq;
4352*4882a593Smuzhiyun 			}
4353*4882a593Smuzhiyun 		}
4354*4882a593Smuzhiyun 
4355*4882a593Smuzhiyun 	return NULL;
4356*4882a593Smuzhiyun }
4357*4882a593Smuzhiyun 
4358*4882a593Smuzhiyun /*
4359*4882a593Smuzhiyun  * Select a queue for service.  If we have a current queue in service,
4360*4882a593Smuzhiyun  * check whether to continue servicing it, or retrieve and set a new one.
4361*4882a593Smuzhiyun  */
bfq_select_queue(struct bfq_data * bfqd)4362*4882a593Smuzhiyun static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
4363*4882a593Smuzhiyun {
4364*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
4365*4882a593Smuzhiyun 	struct request *next_rq;
4366*4882a593Smuzhiyun 	enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT;
4367*4882a593Smuzhiyun 
4368*4882a593Smuzhiyun 	bfqq = bfqd->in_service_queue;
4369*4882a593Smuzhiyun 	if (!bfqq)
4370*4882a593Smuzhiyun 		goto new_queue;
4371*4882a593Smuzhiyun 
4372*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
4373*4882a593Smuzhiyun 
4374*4882a593Smuzhiyun 	/*
4375*4882a593Smuzhiyun 	 * Do not expire bfqq for budget timeout if bfqq may be about
4376*4882a593Smuzhiyun 	 * to enjoy device idling. The reason why, in this case, we
4377*4882a593Smuzhiyun 	 * prevent bfqq from expiring is the same as in the comments
4378*4882a593Smuzhiyun 	 * on the case where bfq_bfqq_must_idle() returns true, in
4379*4882a593Smuzhiyun 	 * bfq_completed_request().
4380*4882a593Smuzhiyun 	 */
4381*4882a593Smuzhiyun 	if (bfq_may_expire_for_budg_timeout(bfqq) &&
4382*4882a593Smuzhiyun 	    !bfq_bfqq_must_idle(bfqq))
4383*4882a593Smuzhiyun 		goto expire;
4384*4882a593Smuzhiyun 
4385*4882a593Smuzhiyun check_queue:
4386*4882a593Smuzhiyun 	/*
4387*4882a593Smuzhiyun 	 * This loop is rarely executed more than once. Even when it
4388*4882a593Smuzhiyun 	 * happens, it is much more convenient to re-execute this loop
4389*4882a593Smuzhiyun 	 * than to return NULL and trigger a new dispatch to get a
4390*4882a593Smuzhiyun 	 * request served.
4391*4882a593Smuzhiyun 	 */
4392*4882a593Smuzhiyun 	next_rq = bfqq->next_rq;
4393*4882a593Smuzhiyun 	/*
4394*4882a593Smuzhiyun 	 * If bfqq has requests queued and it has enough budget left to
4395*4882a593Smuzhiyun 	 * serve them, keep the queue, otherwise expire it.
4396*4882a593Smuzhiyun 	 */
4397*4882a593Smuzhiyun 	if (next_rq) {
4398*4882a593Smuzhiyun 		if (bfq_serv_to_charge(next_rq, bfqq) >
4399*4882a593Smuzhiyun 			bfq_bfqq_budget_left(bfqq)) {
4400*4882a593Smuzhiyun 			/*
4401*4882a593Smuzhiyun 			 * Expire the queue for budget exhaustion,
4402*4882a593Smuzhiyun 			 * which makes sure that the next budget is
4403*4882a593Smuzhiyun 			 * enough to serve the next request, even if
4404*4882a593Smuzhiyun 			 * it comes from the fifo expired path.
4405*4882a593Smuzhiyun 			 */
4406*4882a593Smuzhiyun 			reason = BFQQE_BUDGET_EXHAUSTED;
4407*4882a593Smuzhiyun 			goto expire;
4408*4882a593Smuzhiyun 		} else {
4409*4882a593Smuzhiyun 			/*
4410*4882a593Smuzhiyun 			 * The idle timer may be pending because we may
4411*4882a593Smuzhiyun 			 * not disable disk idling even when a new request
4412*4882a593Smuzhiyun 			 * arrives.
4413*4882a593Smuzhiyun 			 */
4414*4882a593Smuzhiyun 			if (bfq_bfqq_wait_request(bfqq)) {
4415*4882a593Smuzhiyun 				/*
4416*4882a593Smuzhiyun 				 * If we get here: 1) at least a new request
4417*4882a593Smuzhiyun 				 * has arrived but we have not disabled the
4418*4882a593Smuzhiyun 				 * timer because the request was too small,
4419*4882a593Smuzhiyun 				 * 2) then the block layer has unplugged
4420*4882a593Smuzhiyun 				 * the device, causing the dispatch to be
4421*4882a593Smuzhiyun 				 * invoked.
4422*4882a593Smuzhiyun 				 *
4423*4882a593Smuzhiyun 				 * Since the device is unplugged, now the
4424*4882a593Smuzhiyun 				 * requests are probably large enough to
4425*4882a593Smuzhiyun 				 * provide a reasonable throughput.
4426*4882a593Smuzhiyun 				 * So we disable idling.
4427*4882a593Smuzhiyun 				 */
4428*4882a593Smuzhiyun 				bfq_clear_bfqq_wait_request(bfqq);
4429*4882a593Smuzhiyun 				hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
4430*4882a593Smuzhiyun 			}
4431*4882a593Smuzhiyun 			goto keep_queue;
4432*4882a593Smuzhiyun 		}
4433*4882a593Smuzhiyun 	}
4434*4882a593Smuzhiyun 
4435*4882a593Smuzhiyun 	/*
4436*4882a593Smuzhiyun 	 * No requests pending. However, if the in-service queue is idling
4437*4882a593Smuzhiyun 	 * for a new request, or has requests waiting for a completion and
4438*4882a593Smuzhiyun 	 * may idle after their completion, then keep it anyway.
4439*4882a593Smuzhiyun 	 *
4440*4882a593Smuzhiyun 	 * Yet, inject service from other queues if it boosts
4441*4882a593Smuzhiyun 	 * throughput and is possible.
4442*4882a593Smuzhiyun 	 */
4443*4882a593Smuzhiyun 	if (bfq_bfqq_wait_request(bfqq) ||
4444*4882a593Smuzhiyun 	    (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) {
4445*4882a593Smuzhiyun 		struct bfq_queue *async_bfqq =
4446*4882a593Smuzhiyun 			bfqq->bic && bfqq->bic->bfqq[0] &&
4447*4882a593Smuzhiyun 			bfq_bfqq_busy(bfqq->bic->bfqq[0]) &&
4448*4882a593Smuzhiyun 			bfqq->bic->bfqq[0]->next_rq ?
4449*4882a593Smuzhiyun 			bfqq->bic->bfqq[0] : NULL;
4450*4882a593Smuzhiyun 
4451*4882a593Smuzhiyun 		/*
4452*4882a593Smuzhiyun 		 * The next three mutually-exclusive ifs decide
4453*4882a593Smuzhiyun 		 * whether to try injection, and choose the queue to
4454*4882a593Smuzhiyun 		 * pick an I/O request from.
4455*4882a593Smuzhiyun 		 *
4456*4882a593Smuzhiyun 		 * The first if checks whether the process associated
4457*4882a593Smuzhiyun 		 * with bfqq has also async I/O pending. If so, it
4458*4882a593Smuzhiyun 		 * injects such I/O unconditionally. Injecting async
4459*4882a593Smuzhiyun 		 * I/O from the same process can cause no harm to the
4460*4882a593Smuzhiyun 		 * process. On the contrary, it can only increase
4461*4882a593Smuzhiyun 		 * bandwidth and reduce latency for the process.
4462*4882a593Smuzhiyun 		 *
4463*4882a593Smuzhiyun 		 * The second if checks whether there happens to be a
4464*4882a593Smuzhiyun 		 * non-empty waker queue for bfqq, i.e., a queue whose
4465*4882a593Smuzhiyun 		 * I/O needs to be completed for bfqq to receive new
4466*4882a593Smuzhiyun 		 * I/O. This happens, e.g., if bfqq is associated with
4467*4882a593Smuzhiyun 		 * a process that does some sync. A sync generates
4468*4882a593Smuzhiyun 		 * extra blocking I/O, which must be completed before
4469*4882a593Smuzhiyun 		 * the process associated with bfqq can go on with its
4470*4882a593Smuzhiyun 		 * I/O. If the I/O of the waker queue is not served,
4471*4882a593Smuzhiyun 		 * then bfqq remains empty, and no I/O is dispatched,
4472*4882a593Smuzhiyun 		 * until the idle timeout fires for bfqq. This is
4473*4882a593Smuzhiyun 		 * likely to result in lower bandwidth and higher
4474*4882a593Smuzhiyun 		 * latencies for bfqq, and in a severe loss of total
4475*4882a593Smuzhiyun 		 * throughput. The best action to take is therefore to
4476*4882a593Smuzhiyun 		 * serve the waker queue as soon as possible. So do it
4477*4882a593Smuzhiyun 		 * (without relying on the third alternative below for
4478*4882a593Smuzhiyun 		 * eventually serving waker_bfqq's I/O; see the last
4479*4882a593Smuzhiyun 		 * paragraph for further details). This systematic
4480*4882a593Smuzhiyun 		 * injection of I/O from the waker queue does not
4481*4882a593Smuzhiyun 		 * cause any delay to bfqq's I/O. On the contrary,
4482*4882a593Smuzhiyun 		 * next bfqq's I/O is brought forward dramatically,
4483*4882a593Smuzhiyun 		 * for it is not blocked for milliseconds.
4484*4882a593Smuzhiyun 		 *
4485*4882a593Smuzhiyun 		 * The third if checks whether bfqq is a queue for
4486*4882a593Smuzhiyun 		 * which it is better to avoid injection. It is so if
4487*4882a593Smuzhiyun 		 * bfqq delivers more throughput when served without
4488*4882a593Smuzhiyun 		 * any further I/O from other queues in the middle, or
4489*4882a593Smuzhiyun 		 * if the service times of bfqq's I/O requests both
4490*4882a593Smuzhiyun 		 * count more than overall throughput, and may be
4491*4882a593Smuzhiyun 		 * easily increased by injection (this happens if bfqq
4492*4882a593Smuzhiyun 		 * has a short think time). If none of these
4493*4882a593Smuzhiyun 		 * conditions holds, then a candidate queue for
4494*4882a593Smuzhiyun 		 * injection is looked for through
4495*4882a593Smuzhiyun 		 * bfq_choose_bfqq_for_injection(). Note that the
4496*4882a593Smuzhiyun 		 * latter may return NULL (for example if the inject
4497*4882a593Smuzhiyun 		 * limit for bfqq is currently 0).
4498*4882a593Smuzhiyun 		 *
4499*4882a593Smuzhiyun 		 * NOTE: motivation for the second alternative
4500*4882a593Smuzhiyun 		 *
4501*4882a593Smuzhiyun 		 * Thanks to the way the inject limit is updated in
4502*4882a593Smuzhiyun 		 * bfq_update_has_short_ttime(), it is rather likely
4503*4882a593Smuzhiyun 		 * that, if I/O is being plugged for bfqq and the
4504*4882a593Smuzhiyun 		 * waker queue has pending I/O requests that are
4505*4882a593Smuzhiyun 		 * blocking bfqq's I/O, then the third alternative
4506*4882a593Smuzhiyun 		 * above lets the waker queue get served before the
4507*4882a593Smuzhiyun 		 * I/O-plugging timeout fires. So one may deem the
4508*4882a593Smuzhiyun 		 * second alternative superfluous. It is not, because
4509*4882a593Smuzhiyun 		 * the third alternative may be way less effective in
4510*4882a593Smuzhiyun 		 * case of a synchronization. For two main
4511*4882a593Smuzhiyun 		 * reasons. First, throughput may be low because the
4512*4882a593Smuzhiyun 		 * inject limit may be too low to guarantee the same
4513*4882a593Smuzhiyun 		 * amount of injected I/O, from the waker queue or
4514*4882a593Smuzhiyun 		 * other queues, that the second alternative
4515*4882a593Smuzhiyun 		 * guarantees (the second alternative unconditionally
4516*4882a593Smuzhiyun 		 * injects a pending I/O request of the waker queue
4517*4882a593Smuzhiyun 		 * for each bfq_dispatch_request()). Second, with the
4518*4882a593Smuzhiyun 		 * third alternative, the duration of the plugging,
4519*4882a593Smuzhiyun 		 * i.e., the time before bfqq finally receives new I/O,
4520*4882a593Smuzhiyun 		 * may not be minimized, because the waker queue may
4521*4882a593Smuzhiyun 		 * happen to be served only after other queues.
4522*4882a593Smuzhiyun 		 */
4523*4882a593Smuzhiyun 		if (async_bfqq &&
4524*4882a593Smuzhiyun 		    icq_to_bic(async_bfqq->next_rq->elv.icq) == bfqq->bic &&
4525*4882a593Smuzhiyun 		    bfq_serv_to_charge(async_bfqq->next_rq, async_bfqq) <=
4526*4882a593Smuzhiyun 		    bfq_bfqq_budget_left(async_bfqq))
4527*4882a593Smuzhiyun 			bfqq = bfqq->bic->bfqq[0];
4528*4882a593Smuzhiyun 		else if (bfq_bfqq_has_waker(bfqq) &&
4529*4882a593Smuzhiyun 			   bfq_bfqq_busy(bfqq->waker_bfqq) &&
4530*4882a593Smuzhiyun 			   bfqq->waker_bfqq->next_rq &&
4531*4882a593Smuzhiyun 			   bfq_serv_to_charge(bfqq->waker_bfqq->next_rq,
4532*4882a593Smuzhiyun 					      bfqq->waker_bfqq) <=
4533*4882a593Smuzhiyun 			   bfq_bfqq_budget_left(bfqq->waker_bfqq)
4534*4882a593Smuzhiyun 			)
4535*4882a593Smuzhiyun 			bfqq = bfqq->waker_bfqq;
4536*4882a593Smuzhiyun 		else if (!idling_boosts_thr_without_issues(bfqd, bfqq) &&
4537*4882a593Smuzhiyun 			 (bfqq->wr_coeff == 1 || bfqd->wr_busy_queues > 1 ||
4538*4882a593Smuzhiyun 			  !bfq_bfqq_has_short_ttime(bfqq)))
4539*4882a593Smuzhiyun 			bfqq = bfq_choose_bfqq_for_injection(bfqd);
4540*4882a593Smuzhiyun 		else
4541*4882a593Smuzhiyun 			bfqq = NULL;
4542*4882a593Smuzhiyun 
4543*4882a593Smuzhiyun 		goto keep_queue;
4544*4882a593Smuzhiyun 	}
4545*4882a593Smuzhiyun 
4546*4882a593Smuzhiyun 	reason = BFQQE_NO_MORE_REQUESTS;
4547*4882a593Smuzhiyun expire:
4548*4882a593Smuzhiyun 	bfq_bfqq_expire(bfqd, bfqq, false, reason);
4549*4882a593Smuzhiyun new_queue:
4550*4882a593Smuzhiyun 	bfqq = bfq_set_in_service_queue(bfqd);
4551*4882a593Smuzhiyun 	if (bfqq) {
4552*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue");
4553*4882a593Smuzhiyun 		goto check_queue;
4554*4882a593Smuzhiyun 	}
4555*4882a593Smuzhiyun keep_queue:
4556*4882a593Smuzhiyun 	if (bfqq)
4557*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue");
4558*4882a593Smuzhiyun 	else
4559*4882a593Smuzhiyun 		bfq_log(bfqd, "select_queue: no queue returned");
4560*4882a593Smuzhiyun 
4561*4882a593Smuzhiyun 	return bfqq;
4562*4882a593Smuzhiyun }
4563*4882a593Smuzhiyun 
bfq_update_wr_data(struct bfq_data * bfqd,struct bfq_queue * bfqq)4564*4882a593Smuzhiyun static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
4565*4882a593Smuzhiyun {
4566*4882a593Smuzhiyun 	struct bfq_entity *entity = &bfqq->entity;
4567*4882a593Smuzhiyun 
4568*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
4569*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq,
4570*4882a593Smuzhiyun 			"raising period dur %u/%u msec, old coeff %u, w %d(%d)",
4571*4882a593Smuzhiyun 			jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
4572*4882a593Smuzhiyun 			jiffies_to_msecs(bfqq->wr_cur_max_time),
4573*4882a593Smuzhiyun 			bfqq->wr_coeff,
4574*4882a593Smuzhiyun 			bfqq->entity.weight, bfqq->entity.orig_weight);
4575*4882a593Smuzhiyun 
4576*4882a593Smuzhiyun 		if (entity->prio_changed)
4577*4882a593Smuzhiyun 			bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
4578*4882a593Smuzhiyun 
4579*4882a593Smuzhiyun 		/*
4580*4882a593Smuzhiyun 		 * If the queue was activated in a burst, or too much
4581*4882a593Smuzhiyun 		 * time has elapsed from the beginning of this
4582*4882a593Smuzhiyun 		 * weight-raising period, then end weight raising.
4583*4882a593Smuzhiyun 		 */
4584*4882a593Smuzhiyun 		if (bfq_bfqq_in_large_burst(bfqq))
4585*4882a593Smuzhiyun 			bfq_bfqq_end_wr(bfqq);
4586*4882a593Smuzhiyun 		else if (time_is_before_jiffies(bfqq->last_wr_start_finish +
4587*4882a593Smuzhiyun 						bfqq->wr_cur_max_time)) {
4588*4882a593Smuzhiyun 			if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time ||
4589*4882a593Smuzhiyun 			time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt +
4590*4882a593Smuzhiyun 					       bfq_wr_duration(bfqd)))
4591*4882a593Smuzhiyun 				bfq_bfqq_end_wr(bfqq);
4592*4882a593Smuzhiyun 			else {
4593*4882a593Smuzhiyun 				switch_back_to_interactive_wr(bfqq, bfqd);
4594*4882a593Smuzhiyun 				bfqq->entity.prio_changed = 1;
4595*4882a593Smuzhiyun 			}
4596*4882a593Smuzhiyun 		}
4597*4882a593Smuzhiyun 		if (bfqq->wr_coeff > 1 &&
4598*4882a593Smuzhiyun 		    bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time &&
4599*4882a593Smuzhiyun 		    bfqq->service_from_wr > max_service_from_wr) {
4600*4882a593Smuzhiyun 			/* see comments on max_service_from_wr */
4601*4882a593Smuzhiyun 			bfq_bfqq_end_wr(bfqq);
4602*4882a593Smuzhiyun 		}
4603*4882a593Smuzhiyun 	}
4604*4882a593Smuzhiyun 	/*
4605*4882a593Smuzhiyun 	 * To improve latency (for this or other queues), immediately
4606*4882a593Smuzhiyun 	 * update weight both if it must be raised and if it must be
4607*4882a593Smuzhiyun 	 * lowered. Since, entity may be on some active tree here, and
4608*4882a593Smuzhiyun 	 * might have a pending change of its ioprio class, invoke
4609*4882a593Smuzhiyun 	 * next function with the last parameter unset (see the
4610*4882a593Smuzhiyun 	 * comments on the function).
4611*4882a593Smuzhiyun 	 */
4612*4882a593Smuzhiyun 	if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
4613*4882a593Smuzhiyun 		__bfq_entity_update_weight_prio(bfq_entity_service_tree(entity),
4614*4882a593Smuzhiyun 						entity, false);
4615*4882a593Smuzhiyun }
4616*4882a593Smuzhiyun 
4617*4882a593Smuzhiyun /*
4618*4882a593Smuzhiyun  * Dispatch next request from bfqq.
4619*4882a593Smuzhiyun  */
bfq_dispatch_rq_from_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq)4620*4882a593Smuzhiyun static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd,
4621*4882a593Smuzhiyun 						 struct bfq_queue *bfqq)
4622*4882a593Smuzhiyun {
4623*4882a593Smuzhiyun 	struct request *rq = bfqq->next_rq;
4624*4882a593Smuzhiyun 	unsigned long service_to_charge;
4625*4882a593Smuzhiyun 
4626*4882a593Smuzhiyun 	service_to_charge = bfq_serv_to_charge(rq, bfqq);
4627*4882a593Smuzhiyun 
4628*4882a593Smuzhiyun 	bfq_bfqq_served(bfqq, service_to_charge);
4629*4882a593Smuzhiyun 
4630*4882a593Smuzhiyun 	if (bfqq == bfqd->in_service_queue && bfqd->wait_dispatch) {
4631*4882a593Smuzhiyun 		bfqd->wait_dispatch = false;
4632*4882a593Smuzhiyun 		bfqd->waited_rq = rq;
4633*4882a593Smuzhiyun 	}
4634*4882a593Smuzhiyun 
4635*4882a593Smuzhiyun 	bfq_dispatch_remove(bfqd->queue, rq);
4636*4882a593Smuzhiyun 
4637*4882a593Smuzhiyun 	if (bfqq != bfqd->in_service_queue)
4638*4882a593Smuzhiyun 		goto return_rq;
4639*4882a593Smuzhiyun 
4640*4882a593Smuzhiyun 	/*
4641*4882a593Smuzhiyun 	 * If weight raising has to terminate for bfqq, then next
4642*4882a593Smuzhiyun 	 * function causes an immediate update of bfqq's weight,
4643*4882a593Smuzhiyun 	 * without waiting for next activation. As a consequence, on
4644*4882a593Smuzhiyun 	 * expiration, bfqq will be timestamped as if has never been
4645*4882a593Smuzhiyun 	 * weight-raised during this service slot, even if it has
4646*4882a593Smuzhiyun 	 * received part or even most of the service as a
4647*4882a593Smuzhiyun 	 * weight-raised queue. This inflates bfqq's timestamps, which
4648*4882a593Smuzhiyun 	 * is beneficial, as bfqq is then more willing to leave the
4649*4882a593Smuzhiyun 	 * device immediately to possible other weight-raised queues.
4650*4882a593Smuzhiyun 	 */
4651*4882a593Smuzhiyun 	bfq_update_wr_data(bfqd, bfqq);
4652*4882a593Smuzhiyun 
4653*4882a593Smuzhiyun 	/*
4654*4882a593Smuzhiyun 	 * Expire bfqq, pretending that its budget expired, if bfqq
4655*4882a593Smuzhiyun 	 * belongs to CLASS_IDLE and other queues are waiting for
4656*4882a593Smuzhiyun 	 * service.
4657*4882a593Smuzhiyun 	 */
4658*4882a593Smuzhiyun 	if (!(bfq_tot_busy_queues(bfqd) > 1 && bfq_class_idle(bfqq)))
4659*4882a593Smuzhiyun 		goto return_rq;
4660*4882a593Smuzhiyun 
4661*4882a593Smuzhiyun 	bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED);
4662*4882a593Smuzhiyun 
4663*4882a593Smuzhiyun return_rq:
4664*4882a593Smuzhiyun 	return rq;
4665*4882a593Smuzhiyun }
4666*4882a593Smuzhiyun 
bfq_has_work(struct blk_mq_hw_ctx * hctx)4667*4882a593Smuzhiyun static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
4668*4882a593Smuzhiyun {
4669*4882a593Smuzhiyun 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4670*4882a593Smuzhiyun 
4671*4882a593Smuzhiyun 	/*
4672*4882a593Smuzhiyun 	 * Avoiding lock: a race on bfqd->busy_queues should cause at
4673*4882a593Smuzhiyun 	 * most a call to dispatch for nothing
4674*4882a593Smuzhiyun 	 */
4675*4882a593Smuzhiyun 	return !list_empty_careful(&bfqd->dispatch) ||
4676*4882a593Smuzhiyun 		bfq_tot_busy_queues(bfqd) > 0;
4677*4882a593Smuzhiyun }
4678*4882a593Smuzhiyun 
__bfq_dispatch_request(struct blk_mq_hw_ctx * hctx)4679*4882a593Smuzhiyun static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
4680*4882a593Smuzhiyun {
4681*4882a593Smuzhiyun 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4682*4882a593Smuzhiyun 	struct request *rq = NULL;
4683*4882a593Smuzhiyun 	struct bfq_queue *bfqq = NULL;
4684*4882a593Smuzhiyun 
4685*4882a593Smuzhiyun 	if (!list_empty(&bfqd->dispatch)) {
4686*4882a593Smuzhiyun 		rq = list_first_entry(&bfqd->dispatch, struct request,
4687*4882a593Smuzhiyun 				      queuelist);
4688*4882a593Smuzhiyun 		list_del_init(&rq->queuelist);
4689*4882a593Smuzhiyun 
4690*4882a593Smuzhiyun 		bfqq = RQ_BFQQ(rq);
4691*4882a593Smuzhiyun 
4692*4882a593Smuzhiyun 		if (bfqq) {
4693*4882a593Smuzhiyun 			/*
4694*4882a593Smuzhiyun 			 * Increment counters here, because this
4695*4882a593Smuzhiyun 			 * dispatch does not follow the standard
4696*4882a593Smuzhiyun 			 * dispatch flow (where counters are
4697*4882a593Smuzhiyun 			 * incremented)
4698*4882a593Smuzhiyun 			 */
4699*4882a593Smuzhiyun 			bfqq->dispatched++;
4700*4882a593Smuzhiyun 
4701*4882a593Smuzhiyun 			goto inc_in_driver_start_rq;
4702*4882a593Smuzhiyun 		}
4703*4882a593Smuzhiyun 
4704*4882a593Smuzhiyun 		/*
4705*4882a593Smuzhiyun 		 * We exploit the bfq_finish_requeue_request hook to
4706*4882a593Smuzhiyun 		 * decrement rq_in_driver, but
4707*4882a593Smuzhiyun 		 * bfq_finish_requeue_request will not be invoked on
4708*4882a593Smuzhiyun 		 * this request. So, to avoid unbalance, just start
4709*4882a593Smuzhiyun 		 * this request, without incrementing rq_in_driver. As
4710*4882a593Smuzhiyun 		 * a negative consequence, rq_in_driver is deceptively
4711*4882a593Smuzhiyun 		 * lower than it should be while this request is in
4712*4882a593Smuzhiyun 		 * service. This may cause bfq_schedule_dispatch to be
4713*4882a593Smuzhiyun 		 * invoked uselessly.
4714*4882a593Smuzhiyun 		 *
4715*4882a593Smuzhiyun 		 * As for implementing an exact solution, the
4716*4882a593Smuzhiyun 		 * bfq_finish_requeue_request hook, if defined, is
4717*4882a593Smuzhiyun 		 * probably invoked also on this request. So, by
4718*4882a593Smuzhiyun 		 * exploiting this hook, we could 1) increment
4719*4882a593Smuzhiyun 		 * rq_in_driver here, and 2) decrement it in
4720*4882a593Smuzhiyun 		 * bfq_finish_requeue_request. Such a solution would
4721*4882a593Smuzhiyun 		 * let the value of the counter be always accurate,
4722*4882a593Smuzhiyun 		 * but it would entail using an extra interface
4723*4882a593Smuzhiyun 		 * function. This cost seems higher than the benefit,
4724*4882a593Smuzhiyun 		 * being the frequency of non-elevator-private
4725*4882a593Smuzhiyun 		 * requests very low.
4726*4882a593Smuzhiyun 		 */
4727*4882a593Smuzhiyun 		goto start_rq;
4728*4882a593Smuzhiyun 	}
4729*4882a593Smuzhiyun 
4730*4882a593Smuzhiyun 	bfq_log(bfqd, "dispatch requests: %d busy queues",
4731*4882a593Smuzhiyun 		bfq_tot_busy_queues(bfqd));
4732*4882a593Smuzhiyun 
4733*4882a593Smuzhiyun 	if (bfq_tot_busy_queues(bfqd) == 0)
4734*4882a593Smuzhiyun 		goto exit;
4735*4882a593Smuzhiyun 
4736*4882a593Smuzhiyun 	/*
4737*4882a593Smuzhiyun 	 * Force device to serve one request at a time if
4738*4882a593Smuzhiyun 	 * strict_guarantees is true. Forcing this service scheme is
4739*4882a593Smuzhiyun 	 * currently the ONLY way to guarantee that the request
4740*4882a593Smuzhiyun 	 * service order enforced by the scheduler is respected by a
4741*4882a593Smuzhiyun 	 * queueing device. Otherwise the device is free even to make
4742*4882a593Smuzhiyun 	 * some unlucky request wait for as long as the device
4743*4882a593Smuzhiyun 	 * wishes.
4744*4882a593Smuzhiyun 	 *
4745*4882a593Smuzhiyun 	 * Of course, serving one request at a time may cause loss of
4746*4882a593Smuzhiyun 	 * throughput.
4747*4882a593Smuzhiyun 	 */
4748*4882a593Smuzhiyun 	if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0)
4749*4882a593Smuzhiyun 		goto exit;
4750*4882a593Smuzhiyun 
4751*4882a593Smuzhiyun 	bfqq = bfq_select_queue(bfqd);
4752*4882a593Smuzhiyun 	if (!bfqq)
4753*4882a593Smuzhiyun 		goto exit;
4754*4882a593Smuzhiyun 
4755*4882a593Smuzhiyun 	rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq);
4756*4882a593Smuzhiyun 
4757*4882a593Smuzhiyun 	if (rq) {
4758*4882a593Smuzhiyun inc_in_driver_start_rq:
4759*4882a593Smuzhiyun 		bfqd->rq_in_driver++;
4760*4882a593Smuzhiyun start_rq:
4761*4882a593Smuzhiyun 		rq->rq_flags |= RQF_STARTED;
4762*4882a593Smuzhiyun 	}
4763*4882a593Smuzhiyun exit:
4764*4882a593Smuzhiyun 	return rq;
4765*4882a593Smuzhiyun }
4766*4882a593Smuzhiyun 
4767*4882a593Smuzhiyun #ifdef CONFIG_BFQ_CGROUP_DEBUG
bfq_update_dispatch_stats(struct request_queue * q,struct request * rq,struct bfq_queue * in_serv_queue,bool idle_timer_disabled)4768*4882a593Smuzhiyun static void bfq_update_dispatch_stats(struct request_queue *q,
4769*4882a593Smuzhiyun 				      struct request *rq,
4770*4882a593Smuzhiyun 				      struct bfq_queue *in_serv_queue,
4771*4882a593Smuzhiyun 				      bool idle_timer_disabled)
4772*4882a593Smuzhiyun {
4773*4882a593Smuzhiyun 	struct bfq_queue *bfqq = rq ? RQ_BFQQ(rq) : NULL;
4774*4882a593Smuzhiyun 
4775*4882a593Smuzhiyun 	if (!idle_timer_disabled && !bfqq)
4776*4882a593Smuzhiyun 		return;
4777*4882a593Smuzhiyun 
4778*4882a593Smuzhiyun 	/*
4779*4882a593Smuzhiyun 	 * rq and bfqq are guaranteed to exist until this function
4780*4882a593Smuzhiyun 	 * ends, for the following reasons. First, rq can be
4781*4882a593Smuzhiyun 	 * dispatched to the device, and then can be completed and
4782*4882a593Smuzhiyun 	 * freed, only after this function ends. Second, rq cannot be
4783*4882a593Smuzhiyun 	 * merged (and thus freed because of a merge) any longer,
4784*4882a593Smuzhiyun 	 * because it has already started. Thus rq cannot be freed
4785*4882a593Smuzhiyun 	 * before this function ends, and, since rq has a reference to
4786*4882a593Smuzhiyun 	 * bfqq, the same guarantee holds for bfqq too.
4787*4882a593Smuzhiyun 	 *
4788*4882a593Smuzhiyun 	 * In addition, the following queue lock guarantees that
4789*4882a593Smuzhiyun 	 * bfqq_group(bfqq) exists as well.
4790*4882a593Smuzhiyun 	 */
4791*4882a593Smuzhiyun 	spin_lock_irq(&q->queue_lock);
4792*4882a593Smuzhiyun 	if (idle_timer_disabled)
4793*4882a593Smuzhiyun 		/*
4794*4882a593Smuzhiyun 		 * Since the idle timer has been disabled,
4795*4882a593Smuzhiyun 		 * in_serv_queue contained some request when
4796*4882a593Smuzhiyun 		 * __bfq_dispatch_request was invoked above, which
4797*4882a593Smuzhiyun 		 * implies that rq was picked exactly from
4798*4882a593Smuzhiyun 		 * in_serv_queue. Thus in_serv_queue == bfqq, and is
4799*4882a593Smuzhiyun 		 * therefore guaranteed to exist because of the above
4800*4882a593Smuzhiyun 		 * arguments.
4801*4882a593Smuzhiyun 		 */
4802*4882a593Smuzhiyun 		bfqg_stats_update_idle_time(bfqq_group(in_serv_queue));
4803*4882a593Smuzhiyun 	if (bfqq) {
4804*4882a593Smuzhiyun 		struct bfq_group *bfqg = bfqq_group(bfqq);
4805*4882a593Smuzhiyun 
4806*4882a593Smuzhiyun 		bfqg_stats_update_avg_queue_size(bfqg);
4807*4882a593Smuzhiyun 		bfqg_stats_set_start_empty_time(bfqg);
4808*4882a593Smuzhiyun 		bfqg_stats_update_io_remove(bfqg, rq->cmd_flags);
4809*4882a593Smuzhiyun 	}
4810*4882a593Smuzhiyun 	spin_unlock_irq(&q->queue_lock);
4811*4882a593Smuzhiyun }
4812*4882a593Smuzhiyun #else
bfq_update_dispatch_stats(struct request_queue * q,struct request * rq,struct bfq_queue * in_serv_queue,bool idle_timer_disabled)4813*4882a593Smuzhiyun static inline void bfq_update_dispatch_stats(struct request_queue *q,
4814*4882a593Smuzhiyun 					     struct request *rq,
4815*4882a593Smuzhiyun 					     struct bfq_queue *in_serv_queue,
4816*4882a593Smuzhiyun 					     bool idle_timer_disabled) {}
4817*4882a593Smuzhiyun #endif /* CONFIG_BFQ_CGROUP_DEBUG */
4818*4882a593Smuzhiyun 
bfq_dispatch_request(struct blk_mq_hw_ctx * hctx)4819*4882a593Smuzhiyun static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
4820*4882a593Smuzhiyun {
4821*4882a593Smuzhiyun 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4822*4882a593Smuzhiyun 	struct request *rq;
4823*4882a593Smuzhiyun 	struct bfq_queue *in_serv_queue;
4824*4882a593Smuzhiyun 	bool waiting_rq, idle_timer_disabled = false;
4825*4882a593Smuzhiyun 
4826*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
4827*4882a593Smuzhiyun 
4828*4882a593Smuzhiyun 	in_serv_queue = bfqd->in_service_queue;
4829*4882a593Smuzhiyun 	waiting_rq = in_serv_queue && bfq_bfqq_wait_request(in_serv_queue);
4830*4882a593Smuzhiyun 
4831*4882a593Smuzhiyun 	rq = __bfq_dispatch_request(hctx);
4832*4882a593Smuzhiyun 	if (in_serv_queue == bfqd->in_service_queue) {
4833*4882a593Smuzhiyun 		idle_timer_disabled =
4834*4882a593Smuzhiyun 			waiting_rq && !bfq_bfqq_wait_request(in_serv_queue);
4835*4882a593Smuzhiyun 	}
4836*4882a593Smuzhiyun 
4837*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
4838*4882a593Smuzhiyun 	bfq_update_dispatch_stats(hctx->queue, rq,
4839*4882a593Smuzhiyun 			idle_timer_disabled ? in_serv_queue : NULL,
4840*4882a593Smuzhiyun 				idle_timer_disabled);
4841*4882a593Smuzhiyun 
4842*4882a593Smuzhiyun 	return rq;
4843*4882a593Smuzhiyun }
4844*4882a593Smuzhiyun 
4845*4882a593Smuzhiyun /*
4846*4882a593Smuzhiyun  * Task holds one reference to the queue, dropped when task exits.  Each rq
4847*4882a593Smuzhiyun  * in-flight on this queue also holds a reference, dropped when rq is freed.
4848*4882a593Smuzhiyun  *
4849*4882a593Smuzhiyun  * Scheduler lock must be held here. Recall not to use bfqq after calling
4850*4882a593Smuzhiyun  * this function on it.
4851*4882a593Smuzhiyun  */
bfq_put_queue(struct bfq_queue * bfqq)4852*4882a593Smuzhiyun void bfq_put_queue(struct bfq_queue *bfqq)
4853*4882a593Smuzhiyun {
4854*4882a593Smuzhiyun 	struct bfq_queue *item;
4855*4882a593Smuzhiyun 	struct hlist_node *n;
4856*4882a593Smuzhiyun 	struct bfq_group *bfqg = bfqq_group(bfqq);
4857*4882a593Smuzhiyun 
4858*4882a593Smuzhiyun 	if (bfqq->bfqd)
4859*4882a593Smuzhiyun 		bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
4860*4882a593Smuzhiyun 			     bfqq, bfqq->ref);
4861*4882a593Smuzhiyun 
4862*4882a593Smuzhiyun 	bfqq->ref--;
4863*4882a593Smuzhiyun 	if (bfqq->ref)
4864*4882a593Smuzhiyun 		return;
4865*4882a593Smuzhiyun 
4866*4882a593Smuzhiyun 	if (!hlist_unhashed(&bfqq->burst_list_node)) {
4867*4882a593Smuzhiyun 		hlist_del_init(&bfqq->burst_list_node);
4868*4882a593Smuzhiyun 		/*
4869*4882a593Smuzhiyun 		 * Decrement also burst size after the removal, if the
4870*4882a593Smuzhiyun 		 * process associated with bfqq is exiting, and thus
4871*4882a593Smuzhiyun 		 * does not contribute to the burst any longer. This
4872*4882a593Smuzhiyun 		 * decrement helps filter out false positives of large
4873*4882a593Smuzhiyun 		 * bursts, when some short-lived process (often due to
4874*4882a593Smuzhiyun 		 * the execution of commands by some service) happens
4875*4882a593Smuzhiyun 		 * to start and exit while a complex application is
4876*4882a593Smuzhiyun 		 * starting, and thus spawning several processes that
4877*4882a593Smuzhiyun 		 * do I/O (and that *must not* be treated as a large
4878*4882a593Smuzhiyun 		 * burst, see comments on bfq_handle_burst).
4879*4882a593Smuzhiyun 		 *
4880*4882a593Smuzhiyun 		 * In particular, the decrement is performed only if:
4881*4882a593Smuzhiyun 		 * 1) bfqq is not a merged queue, because, if it is,
4882*4882a593Smuzhiyun 		 * then this free of bfqq is not triggered by the exit
4883*4882a593Smuzhiyun 		 * of the process bfqq is associated with, but exactly
4884*4882a593Smuzhiyun 		 * by the fact that bfqq has just been merged.
4885*4882a593Smuzhiyun 		 * 2) burst_size is greater than 0, to handle
4886*4882a593Smuzhiyun 		 * unbalanced decrements. Unbalanced decrements may
4887*4882a593Smuzhiyun 		 * happen in te following case: bfqq is inserted into
4888*4882a593Smuzhiyun 		 * the current burst list--without incrementing
4889*4882a593Smuzhiyun 		 * bust_size--because of a split, but the current
4890*4882a593Smuzhiyun 		 * burst list is not the burst list bfqq belonged to
4891*4882a593Smuzhiyun 		 * (see comments on the case of a split in
4892*4882a593Smuzhiyun 		 * bfq_set_request).
4893*4882a593Smuzhiyun 		 */
4894*4882a593Smuzhiyun 		if (bfqq->bic && bfqq->bfqd->burst_size > 0)
4895*4882a593Smuzhiyun 			bfqq->bfqd->burst_size--;
4896*4882a593Smuzhiyun 	}
4897*4882a593Smuzhiyun 
4898*4882a593Smuzhiyun 	/*
4899*4882a593Smuzhiyun 	 * bfqq does not exist any longer, so it cannot be woken by
4900*4882a593Smuzhiyun 	 * any other queue, and cannot wake any other queue. Then bfqq
4901*4882a593Smuzhiyun 	 * must be removed from the woken list of its possible waker
4902*4882a593Smuzhiyun 	 * queue, and all queues in the woken list of bfqq must stop
4903*4882a593Smuzhiyun 	 * having a waker queue. Strictly speaking, these updates
4904*4882a593Smuzhiyun 	 * should be performed when bfqq remains with no I/O source
4905*4882a593Smuzhiyun 	 * attached to it, which happens before bfqq gets freed. In
4906*4882a593Smuzhiyun 	 * particular, this happens when the last process associated
4907*4882a593Smuzhiyun 	 * with bfqq exits or gets associated with a different
4908*4882a593Smuzhiyun 	 * queue. However, both events lead to bfqq being freed soon,
4909*4882a593Smuzhiyun 	 * and dangling references would come out only after bfqq gets
4910*4882a593Smuzhiyun 	 * freed. So these updates are done here, as a simple and safe
4911*4882a593Smuzhiyun 	 * way to handle all cases.
4912*4882a593Smuzhiyun 	 */
4913*4882a593Smuzhiyun 	/* remove bfqq from woken list */
4914*4882a593Smuzhiyun 	if (!hlist_unhashed(&bfqq->woken_list_node))
4915*4882a593Smuzhiyun 		hlist_del_init(&bfqq->woken_list_node);
4916*4882a593Smuzhiyun 
4917*4882a593Smuzhiyun 	/* reset waker for all queues in woken list */
4918*4882a593Smuzhiyun 	hlist_for_each_entry_safe(item, n, &bfqq->woken_list,
4919*4882a593Smuzhiyun 				  woken_list_node) {
4920*4882a593Smuzhiyun 		item->waker_bfqq = NULL;
4921*4882a593Smuzhiyun 		bfq_clear_bfqq_has_waker(item);
4922*4882a593Smuzhiyun 		hlist_del_init(&item->woken_list_node);
4923*4882a593Smuzhiyun 	}
4924*4882a593Smuzhiyun 
4925*4882a593Smuzhiyun 	if (bfqq->bfqd && bfqq->bfqd->last_completed_rq_bfqq == bfqq)
4926*4882a593Smuzhiyun 		bfqq->bfqd->last_completed_rq_bfqq = NULL;
4927*4882a593Smuzhiyun 
4928*4882a593Smuzhiyun 	kmem_cache_free(bfq_pool, bfqq);
4929*4882a593Smuzhiyun 	bfqg_and_blkg_put(bfqg);
4930*4882a593Smuzhiyun }
4931*4882a593Smuzhiyun 
bfq_put_cooperator(struct bfq_queue * bfqq)4932*4882a593Smuzhiyun void bfq_put_cooperator(struct bfq_queue *bfqq)
4933*4882a593Smuzhiyun {
4934*4882a593Smuzhiyun 	struct bfq_queue *__bfqq, *next;
4935*4882a593Smuzhiyun 
4936*4882a593Smuzhiyun 	/*
4937*4882a593Smuzhiyun 	 * If this queue was scheduled to merge with another queue, be
4938*4882a593Smuzhiyun 	 * sure to drop the reference taken on that queue (and others in
4939*4882a593Smuzhiyun 	 * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
4940*4882a593Smuzhiyun 	 */
4941*4882a593Smuzhiyun 	__bfqq = bfqq->new_bfqq;
4942*4882a593Smuzhiyun 	while (__bfqq) {
4943*4882a593Smuzhiyun 		if (__bfqq == bfqq)
4944*4882a593Smuzhiyun 			break;
4945*4882a593Smuzhiyun 		next = __bfqq->new_bfqq;
4946*4882a593Smuzhiyun 		bfq_put_queue(__bfqq);
4947*4882a593Smuzhiyun 		__bfqq = next;
4948*4882a593Smuzhiyun 	}
4949*4882a593Smuzhiyun }
4950*4882a593Smuzhiyun 
bfq_exit_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq)4951*4882a593Smuzhiyun static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
4952*4882a593Smuzhiyun {
4953*4882a593Smuzhiyun 	if (bfqq == bfqd->in_service_queue) {
4954*4882a593Smuzhiyun 		__bfq_bfqq_expire(bfqd, bfqq, BFQQE_BUDGET_TIMEOUT);
4955*4882a593Smuzhiyun 		bfq_schedule_dispatch(bfqd);
4956*4882a593Smuzhiyun 	}
4957*4882a593Smuzhiyun 
4958*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref);
4959*4882a593Smuzhiyun 
4960*4882a593Smuzhiyun 	bfq_put_cooperator(bfqq);
4961*4882a593Smuzhiyun 
4962*4882a593Smuzhiyun 	bfq_release_process_ref(bfqd, bfqq);
4963*4882a593Smuzhiyun }
4964*4882a593Smuzhiyun 
bfq_exit_icq_bfqq(struct bfq_io_cq * bic,bool is_sync)4965*4882a593Smuzhiyun static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync)
4966*4882a593Smuzhiyun {
4967*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
4968*4882a593Smuzhiyun 	struct bfq_data *bfqd;
4969*4882a593Smuzhiyun 
4970*4882a593Smuzhiyun 	if (bfqq)
4971*4882a593Smuzhiyun 		bfqd = bfqq->bfqd; /* NULL if scheduler already exited */
4972*4882a593Smuzhiyun 
4973*4882a593Smuzhiyun 	if (bfqq && bfqd) {
4974*4882a593Smuzhiyun 		unsigned long flags;
4975*4882a593Smuzhiyun 
4976*4882a593Smuzhiyun 		spin_lock_irqsave(&bfqd->lock, flags);
4977*4882a593Smuzhiyun 		bfqq->bic = NULL;
4978*4882a593Smuzhiyun 		bfq_exit_bfqq(bfqd, bfqq);
4979*4882a593Smuzhiyun 		bic_set_bfqq(bic, NULL, is_sync);
4980*4882a593Smuzhiyun 		spin_unlock_irqrestore(&bfqd->lock, flags);
4981*4882a593Smuzhiyun 	}
4982*4882a593Smuzhiyun }
4983*4882a593Smuzhiyun 
bfq_exit_icq(struct io_cq * icq)4984*4882a593Smuzhiyun static void bfq_exit_icq(struct io_cq *icq)
4985*4882a593Smuzhiyun {
4986*4882a593Smuzhiyun 	struct bfq_io_cq *bic = icq_to_bic(icq);
4987*4882a593Smuzhiyun 
4988*4882a593Smuzhiyun 	bfq_exit_icq_bfqq(bic, true);
4989*4882a593Smuzhiyun 	bfq_exit_icq_bfqq(bic, false);
4990*4882a593Smuzhiyun }
4991*4882a593Smuzhiyun 
4992*4882a593Smuzhiyun /*
4993*4882a593Smuzhiyun  * Update the entity prio values; note that the new values will not
4994*4882a593Smuzhiyun  * be used until the next (re)activation.
4995*4882a593Smuzhiyun  */
4996*4882a593Smuzhiyun static void
bfq_set_next_ioprio_data(struct bfq_queue * bfqq,struct bfq_io_cq * bic)4997*4882a593Smuzhiyun bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
4998*4882a593Smuzhiyun {
4999*4882a593Smuzhiyun 	struct task_struct *tsk = current;
5000*4882a593Smuzhiyun 	int ioprio_class;
5001*4882a593Smuzhiyun 	struct bfq_data *bfqd = bfqq->bfqd;
5002*4882a593Smuzhiyun 
5003*4882a593Smuzhiyun 	if (!bfqd)
5004*4882a593Smuzhiyun 		return;
5005*4882a593Smuzhiyun 
5006*4882a593Smuzhiyun 	ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
5007*4882a593Smuzhiyun 	switch (ioprio_class) {
5008*4882a593Smuzhiyun 	default:
5009*4882a593Smuzhiyun 		pr_err("bdi %s: bfq: bad prio class %d\n",
5010*4882a593Smuzhiyun 				bdi_dev_name(bfqq->bfqd->queue->backing_dev_info),
5011*4882a593Smuzhiyun 				ioprio_class);
5012*4882a593Smuzhiyun 		fallthrough;
5013*4882a593Smuzhiyun 	case IOPRIO_CLASS_NONE:
5014*4882a593Smuzhiyun 		/*
5015*4882a593Smuzhiyun 		 * No prio set, inherit CPU scheduling settings.
5016*4882a593Smuzhiyun 		 */
5017*4882a593Smuzhiyun 		bfqq->new_ioprio = task_nice_ioprio(tsk);
5018*4882a593Smuzhiyun 		bfqq->new_ioprio_class = task_nice_ioclass(tsk);
5019*4882a593Smuzhiyun 		break;
5020*4882a593Smuzhiyun 	case IOPRIO_CLASS_RT:
5021*4882a593Smuzhiyun 		bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
5022*4882a593Smuzhiyun 		bfqq->new_ioprio_class = IOPRIO_CLASS_RT;
5023*4882a593Smuzhiyun 		break;
5024*4882a593Smuzhiyun 	case IOPRIO_CLASS_BE:
5025*4882a593Smuzhiyun 		bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
5026*4882a593Smuzhiyun 		bfqq->new_ioprio_class = IOPRIO_CLASS_BE;
5027*4882a593Smuzhiyun 		break;
5028*4882a593Smuzhiyun 	case IOPRIO_CLASS_IDLE:
5029*4882a593Smuzhiyun 		bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE;
5030*4882a593Smuzhiyun 		bfqq->new_ioprio = 7;
5031*4882a593Smuzhiyun 		break;
5032*4882a593Smuzhiyun 	}
5033*4882a593Smuzhiyun 
5034*4882a593Smuzhiyun 	if (bfqq->new_ioprio >= IOPRIO_BE_NR) {
5035*4882a593Smuzhiyun 		pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n",
5036*4882a593Smuzhiyun 			bfqq->new_ioprio);
5037*4882a593Smuzhiyun 		bfqq->new_ioprio = IOPRIO_BE_NR - 1;
5038*4882a593Smuzhiyun 	}
5039*4882a593Smuzhiyun 
5040*4882a593Smuzhiyun 	bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio);
5041*4882a593Smuzhiyun 	bfqq->entity.prio_changed = 1;
5042*4882a593Smuzhiyun }
5043*4882a593Smuzhiyun 
5044*4882a593Smuzhiyun static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
5045*4882a593Smuzhiyun 				       struct bio *bio, bool is_sync,
5046*4882a593Smuzhiyun 				       struct bfq_io_cq *bic);
5047*4882a593Smuzhiyun 
bfq_check_ioprio_change(struct bfq_io_cq * bic,struct bio * bio)5048*4882a593Smuzhiyun static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio)
5049*4882a593Smuzhiyun {
5050*4882a593Smuzhiyun 	struct bfq_data *bfqd = bic_to_bfqd(bic);
5051*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
5052*4882a593Smuzhiyun 	int ioprio = bic->icq.ioc->ioprio;
5053*4882a593Smuzhiyun 
5054*4882a593Smuzhiyun 	/*
5055*4882a593Smuzhiyun 	 * This condition may trigger on a newly created bic, be sure to
5056*4882a593Smuzhiyun 	 * drop the lock before returning.
5057*4882a593Smuzhiyun 	 */
5058*4882a593Smuzhiyun 	if (unlikely(!bfqd) || likely(bic->ioprio == ioprio))
5059*4882a593Smuzhiyun 		return;
5060*4882a593Smuzhiyun 
5061*4882a593Smuzhiyun 	bic->ioprio = ioprio;
5062*4882a593Smuzhiyun 
5063*4882a593Smuzhiyun 	bfqq = bic_to_bfqq(bic, false);
5064*4882a593Smuzhiyun 	if (bfqq) {
5065*4882a593Smuzhiyun 		bfq_release_process_ref(bfqd, bfqq);
5066*4882a593Smuzhiyun 		bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic);
5067*4882a593Smuzhiyun 		bic_set_bfqq(bic, bfqq, false);
5068*4882a593Smuzhiyun 	}
5069*4882a593Smuzhiyun 
5070*4882a593Smuzhiyun 	bfqq = bic_to_bfqq(bic, true);
5071*4882a593Smuzhiyun 	if (bfqq)
5072*4882a593Smuzhiyun 		bfq_set_next_ioprio_data(bfqq, bic);
5073*4882a593Smuzhiyun }
5074*4882a593Smuzhiyun 
bfq_init_bfqq(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct bfq_io_cq * bic,pid_t pid,int is_sync)5075*4882a593Smuzhiyun static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
5076*4882a593Smuzhiyun 			  struct bfq_io_cq *bic, pid_t pid, int is_sync)
5077*4882a593Smuzhiyun {
5078*4882a593Smuzhiyun 	RB_CLEAR_NODE(&bfqq->entity.rb_node);
5079*4882a593Smuzhiyun 	INIT_LIST_HEAD(&bfqq->fifo);
5080*4882a593Smuzhiyun 	INIT_HLIST_NODE(&bfqq->burst_list_node);
5081*4882a593Smuzhiyun 	INIT_HLIST_NODE(&bfqq->woken_list_node);
5082*4882a593Smuzhiyun 	INIT_HLIST_HEAD(&bfqq->woken_list);
5083*4882a593Smuzhiyun 
5084*4882a593Smuzhiyun 	bfqq->ref = 0;
5085*4882a593Smuzhiyun 	bfqq->bfqd = bfqd;
5086*4882a593Smuzhiyun 
5087*4882a593Smuzhiyun 	if (bic)
5088*4882a593Smuzhiyun 		bfq_set_next_ioprio_data(bfqq, bic);
5089*4882a593Smuzhiyun 
5090*4882a593Smuzhiyun 	if (is_sync) {
5091*4882a593Smuzhiyun 		/*
5092*4882a593Smuzhiyun 		 * No need to mark as has_short_ttime if in
5093*4882a593Smuzhiyun 		 * idle_class, because no device idling is performed
5094*4882a593Smuzhiyun 		 * for queues in idle class
5095*4882a593Smuzhiyun 		 */
5096*4882a593Smuzhiyun 		if (!bfq_class_idle(bfqq))
5097*4882a593Smuzhiyun 			/* tentatively mark as has_short_ttime */
5098*4882a593Smuzhiyun 			bfq_mark_bfqq_has_short_ttime(bfqq);
5099*4882a593Smuzhiyun 		bfq_mark_bfqq_sync(bfqq);
5100*4882a593Smuzhiyun 		bfq_mark_bfqq_just_created(bfqq);
5101*4882a593Smuzhiyun 	} else
5102*4882a593Smuzhiyun 		bfq_clear_bfqq_sync(bfqq);
5103*4882a593Smuzhiyun 
5104*4882a593Smuzhiyun 	/* set end request to minus infinity from now */
5105*4882a593Smuzhiyun 	bfqq->ttime.last_end_request = ktime_get_ns() + 1;
5106*4882a593Smuzhiyun 
5107*4882a593Smuzhiyun 	bfq_mark_bfqq_IO_bound(bfqq);
5108*4882a593Smuzhiyun 
5109*4882a593Smuzhiyun 	bfqq->pid = pid;
5110*4882a593Smuzhiyun 
5111*4882a593Smuzhiyun 	/* Tentative initial value to trade off between thr and lat */
5112*4882a593Smuzhiyun 	bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
5113*4882a593Smuzhiyun 	bfqq->budget_timeout = bfq_smallest_from_now();
5114*4882a593Smuzhiyun 
5115*4882a593Smuzhiyun 	bfqq->wr_coeff = 1;
5116*4882a593Smuzhiyun 	bfqq->last_wr_start_finish = jiffies;
5117*4882a593Smuzhiyun 	bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now();
5118*4882a593Smuzhiyun 	bfqq->split_time = bfq_smallest_from_now();
5119*4882a593Smuzhiyun 
5120*4882a593Smuzhiyun 	/*
5121*4882a593Smuzhiyun 	 * To not forget the possibly high bandwidth consumed by a
5122*4882a593Smuzhiyun 	 * process/queue in the recent past,
5123*4882a593Smuzhiyun 	 * bfq_bfqq_softrt_next_start() returns a value at least equal
5124*4882a593Smuzhiyun 	 * to the current value of bfqq->soft_rt_next_start (see
5125*4882a593Smuzhiyun 	 * comments on bfq_bfqq_softrt_next_start).  Set
5126*4882a593Smuzhiyun 	 * soft_rt_next_start to now, to mean that bfqq has consumed
5127*4882a593Smuzhiyun 	 * no bandwidth so far.
5128*4882a593Smuzhiyun 	 */
5129*4882a593Smuzhiyun 	bfqq->soft_rt_next_start = jiffies;
5130*4882a593Smuzhiyun 
5131*4882a593Smuzhiyun 	/* first request is almost certainly seeky */
5132*4882a593Smuzhiyun 	bfqq->seek_history = 1;
5133*4882a593Smuzhiyun }
5134*4882a593Smuzhiyun 
bfq_async_queue_prio(struct bfq_data * bfqd,struct bfq_group * bfqg,int ioprio_class,int ioprio)5135*4882a593Smuzhiyun static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
5136*4882a593Smuzhiyun 					       struct bfq_group *bfqg,
5137*4882a593Smuzhiyun 					       int ioprio_class, int ioprio)
5138*4882a593Smuzhiyun {
5139*4882a593Smuzhiyun 	switch (ioprio_class) {
5140*4882a593Smuzhiyun 	case IOPRIO_CLASS_RT:
5141*4882a593Smuzhiyun 		return &bfqg->async_bfqq[0][ioprio];
5142*4882a593Smuzhiyun 	case IOPRIO_CLASS_NONE:
5143*4882a593Smuzhiyun 		ioprio = IOPRIO_NORM;
5144*4882a593Smuzhiyun 		fallthrough;
5145*4882a593Smuzhiyun 	case IOPRIO_CLASS_BE:
5146*4882a593Smuzhiyun 		return &bfqg->async_bfqq[1][ioprio];
5147*4882a593Smuzhiyun 	case IOPRIO_CLASS_IDLE:
5148*4882a593Smuzhiyun 		return &bfqg->async_idle_bfqq;
5149*4882a593Smuzhiyun 	default:
5150*4882a593Smuzhiyun 		return NULL;
5151*4882a593Smuzhiyun 	}
5152*4882a593Smuzhiyun }
5153*4882a593Smuzhiyun 
bfq_get_queue(struct bfq_data * bfqd,struct bio * bio,bool is_sync,struct bfq_io_cq * bic)5154*4882a593Smuzhiyun static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
5155*4882a593Smuzhiyun 				       struct bio *bio, bool is_sync,
5156*4882a593Smuzhiyun 				       struct bfq_io_cq *bic)
5157*4882a593Smuzhiyun {
5158*4882a593Smuzhiyun 	const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
5159*4882a593Smuzhiyun 	const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
5160*4882a593Smuzhiyun 	struct bfq_queue **async_bfqq = NULL;
5161*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
5162*4882a593Smuzhiyun 	struct bfq_group *bfqg;
5163*4882a593Smuzhiyun 
5164*4882a593Smuzhiyun 	bfqg = bfq_bio_bfqg(bfqd, bio);
5165*4882a593Smuzhiyun 	if (!is_sync) {
5166*4882a593Smuzhiyun 		async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
5167*4882a593Smuzhiyun 						  ioprio);
5168*4882a593Smuzhiyun 		bfqq = *async_bfqq;
5169*4882a593Smuzhiyun 		if (bfqq)
5170*4882a593Smuzhiyun 			goto out;
5171*4882a593Smuzhiyun 	}
5172*4882a593Smuzhiyun 
5173*4882a593Smuzhiyun 	bfqq = kmem_cache_alloc_node(bfq_pool,
5174*4882a593Smuzhiyun 				     GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
5175*4882a593Smuzhiyun 				     bfqd->queue->node);
5176*4882a593Smuzhiyun 
5177*4882a593Smuzhiyun 	if (bfqq) {
5178*4882a593Smuzhiyun 		bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
5179*4882a593Smuzhiyun 			      is_sync);
5180*4882a593Smuzhiyun 		bfq_init_entity(&bfqq->entity, bfqg);
5181*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "allocated");
5182*4882a593Smuzhiyun 	} else {
5183*4882a593Smuzhiyun 		bfqq = &bfqd->oom_bfqq;
5184*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
5185*4882a593Smuzhiyun 		goto out;
5186*4882a593Smuzhiyun 	}
5187*4882a593Smuzhiyun 
5188*4882a593Smuzhiyun 	/*
5189*4882a593Smuzhiyun 	 * Pin the queue now that it's allocated, scheduler exit will
5190*4882a593Smuzhiyun 	 * prune it.
5191*4882a593Smuzhiyun 	 */
5192*4882a593Smuzhiyun 	if (async_bfqq) {
5193*4882a593Smuzhiyun 		bfqq->ref++; /*
5194*4882a593Smuzhiyun 			      * Extra group reference, w.r.t. sync
5195*4882a593Smuzhiyun 			      * queue. This extra reference is removed
5196*4882a593Smuzhiyun 			      * only if bfqq->bfqg disappears, to
5197*4882a593Smuzhiyun 			      * guarantee that this queue is not freed
5198*4882a593Smuzhiyun 			      * until its group goes away.
5199*4882a593Smuzhiyun 			      */
5200*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
5201*4882a593Smuzhiyun 			     bfqq, bfqq->ref);
5202*4882a593Smuzhiyun 		*async_bfqq = bfqq;
5203*4882a593Smuzhiyun 	}
5204*4882a593Smuzhiyun 
5205*4882a593Smuzhiyun out:
5206*4882a593Smuzhiyun 	bfqq->ref++; /* get a process reference to this queue */
5207*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref);
5208*4882a593Smuzhiyun 	return bfqq;
5209*4882a593Smuzhiyun }
5210*4882a593Smuzhiyun 
bfq_update_io_thinktime(struct bfq_data * bfqd,struct bfq_queue * bfqq)5211*4882a593Smuzhiyun static void bfq_update_io_thinktime(struct bfq_data *bfqd,
5212*4882a593Smuzhiyun 				    struct bfq_queue *bfqq)
5213*4882a593Smuzhiyun {
5214*4882a593Smuzhiyun 	struct bfq_ttime *ttime = &bfqq->ttime;
5215*4882a593Smuzhiyun 	u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request;
5216*4882a593Smuzhiyun 
5217*4882a593Smuzhiyun 	elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle);
5218*4882a593Smuzhiyun 
5219*4882a593Smuzhiyun 	ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8;
5220*4882a593Smuzhiyun 	ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed,  8);
5221*4882a593Smuzhiyun 	ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
5222*4882a593Smuzhiyun 				     ttime->ttime_samples);
5223*4882a593Smuzhiyun }
5224*4882a593Smuzhiyun 
5225*4882a593Smuzhiyun static void
bfq_update_io_seektime(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct request * rq)5226*4882a593Smuzhiyun bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq,
5227*4882a593Smuzhiyun 		       struct request *rq)
5228*4882a593Smuzhiyun {
5229*4882a593Smuzhiyun 	bfqq->seek_history <<= 1;
5230*4882a593Smuzhiyun 	bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq);
5231*4882a593Smuzhiyun 
5232*4882a593Smuzhiyun 	if (bfqq->wr_coeff > 1 &&
5233*4882a593Smuzhiyun 	    bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
5234*4882a593Smuzhiyun 	    BFQQ_TOTALLY_SEEKY(bfqq))
5235*4882a593Smuzhiyun 		bfq_bfqq_end_wr(bfqq);
5236*4882a593Smuzhiyun }
5237*4882a593Smuzhiyun 
bfq_update_has_short_ttime(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct bfq_io_cq * bic)5238*4882a593Smuzhiyun static void bfq_update_has_short_ttime(struct bfq_data *bfqd,
5239*4882a593Smuzhiyun 				       struct bfq_queue *bfqq,
5240*4882a593Smuzhiyun 				       struct bfq_io_cq *bic)
5241*4882a593Smuzhiyun {
5242*4882a593Smuzhiyun 	bool has_short_ttime = true, state_changed;
5243*4882a593Smuzhiyun 
5244*4882a593Smuzhiyun 	/*
5245*4882a593Smuzhiyun 	 * No need to update has_short_ttime if bfqq is async or in
5246*4882a593Smuzhiyun 	 * idle io prio class, or if bfq_slice_idle is zero, because
5247*4882a593Smuzhiyun 	 * no device idling is performed for bfqq in this case.
5248*4882a593Smuzhiyun 	 */
5249*4882a593Smuzhiyun 	if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq) ||
5250*4882a593Smuzhiyun 	    bfqd->bfq_slice_idle == 0)
5251*4882a593Smuzhiyun 		return;
5252*4882a593Smuzhiyun 
5253*4882a593Smuzhiyun 	/* Idle window just restored, statistics are meaningless. */
5254*4882a593Smuzhiyun 	if (time_is_after_eq_jiffies(bfqq->split_time +
5255*4882a593Smuzhiyun 				     bfqd->bfq_wr_min_idle_time))
5256*4882a593Smuzhiyun 		return;
5257*4882a593Smuzhiyun 
5258*4882a593Smuzhiyun 	/* Think time is infinite if no process is linked to
5259*4882a593Smuzhiyun 	 * bfqq. Otherwise check average think time to
5260*4882a593Smuzhiyun 	 * decide whether to mark as has_short_ttime
5261*4882a593Smuzhiyun 	 */
5262*4882a593Smuzhiyun 	if (atomic_read(&bic->icq.ioc->active_ref) == 0 ||
5263*4882a593Smuzhiyun 	    (bfq_sample_valid(bfqq->ttime.ttime_samples) &&
5264*4882a593Smuzhiyun 	     bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle))
5265*4882a593Smuzhiyun 		has_short_ttime = false;
5266*4882a593Smuzhiyun 
5267*4882a593Smuzhiyun 	state_changed = has_short_ttime != bfq_bfqq_has_short_ttime(bfqq);
5268*4882a593Smuzhiyun 
5269*4882a593Smuzhiyun 	if (has_short_ttime)
5270*4882a593Smuzhiyun 		bfq_mark_bfqq_has_short_ttime(bfqq);
5271*4882a593Smuzhiyun 	else
5272*4882a593Smuzhiyun 		bfq_clear_bfqq_has_short_ttime(bfqq);
5273*4882a593Smuzhiyun 
5274*4882a593Smuzhiyun 	/*
5275*4882a593Smuzhiyun 	 * Until the base value for the total service time gets
5276*4882a593Smuzhiyun 	 * finally computed for bfqq, the inject limit does depend on
5277*4882a593Smuzhiyun 	 * the think-time state (short|long). In particular, the limit
5278*4882a593Smuzhiyun 	 * is 0 or 1 if the think time is deemed, respectively, as
5279*4882a593Smuzhiyun 	 * short or long (details in the comments in
5280*4882a593Smuzhiyun 	 * bfq_update_inject_limit()). Accordingly, the next
5281*4882a593Smuzhiyun 	 * instructions reset the inject limit if the think-time state
5282*4882a593Smuzhiyun 	 * has changed and the above base value is still to be
5283*4882a593Smuzhiyun 	 * computed.
5284*4882a593Smuzhiyun 	 *
5285*4882a593Smuzhiyun 	 * However, the reset is performed only if more than 100 ms
5286*4882a593Smuzhiyun 	 * have elapsed since the last update of the inject limit, or
5287*4882a593Smuzhiyun 	 * (inclusive) if the change is from short to long think
5288*4882a593Smuzhiyun 	 * time. The reason for this waiting is as follows.
5289*4882a593Smuzhiyun 	 *
5290*4882a593Smuzhiyun 	 * bfqq may have a long think time because of a
5291*4882a593Smuzhiyun 	 * synchronization with some other queue, i.e., because the
5292*4882a593Smuzhiyun 	 * I/O of some other queue may need to be completed for bfqq
5293*4882a593Smuzhiyun 	 * to receive new I/O. Details in the comments on the choice
5294*4882a593Smuzhiyun 	 * of the queue for injection in bfq_select_queue().
5295*4882a593Smuzhiyun 	 *
5296*4882a593Smuzhiyun 	 * As stressed in those comments, if such a synchronization is
5297*4882a593Smuzhiyun 	 * actually in place, then, without injection on bfqq, the
5298*4882a593Smuzhiyun 	 * blocking I/O cannot happen to served while bfqq is in
5299*4882a593Smuzhiyun 	 * service. As a consequence, if bfqq is granted
5300*4882a593Smuzhiyun 	 * I/O-dispatch-plugging, then bfqq remains empty, and no I/O
5301*4882a593Smuzhiyun 	 * is dispatched, until the idle timeout fires. This is likely
5302*4882a593Smuzhiyun 	 * to result in lower bandwidth and higher latencies for bfqq,
5303*4882a593Smuzhiyun 	 * and in a severe loss of total throughput.
5304*4882a593Smuzhiyun 	 *
5305*4882a593Smuzhiyun 	 * On the opposite end, a non-zero inject limit may allow the
5306*4882a593Smuzhiyun 	 * I/O that blocks bfqq to be executed soon, and therefore
5307*4882a593Smuzhiyun 	 * bfqq to receive new I/O soon.
5308*4882a593Smuzhiyun 	 *
5309*4882a593Smuzhiyun 	 * But, if the blocking gets actually eliminated, then the
5310*4882a593Smuzhiyun 	 * next think-time sample for bfqq may be very low. This in
5311*4882a593Smuzhiyun 	 * turn may cause bfqq's think time to be deemed
5312*4882a593Smuzhiyun 	 * short. Without the 100 ms barrier, this new state change
5313*4882a593Smuzhiyun 	 * would cause the body of the next if to be executed
5314*4882a593Smuzhiyun 	 * immediately. But this would set to 0 the inject
5315*4882a593Smuzhiyun 	 * limit. Without injection, the blocking I/O would cause the
5316*4882a593Smuzhiyun 	 * think time of bfqq to become long again, and therefore the
5317*4882a593Smuzhiyun 	 * inject limit to be raised again, and so on. The only effect
5318*4882a593Smuzhiyun 	 * of such a steady oscillation between the two think-time
5319*4882a593Smuzhiyun 	 * states would be to prevent effective injection on bfqq.
5320*4882a593Smuzhiyun 	 *
5321*4882a593Smuzhiyun 	 * In contrast, if the inject limit is not reset during such a
5322*4882a593Smuzhiyun 	 * long time interval as 100 ms, then the number of short
5323*4882a593Smuzhiyun 	 * think time samples can grow significantly before the reset
5324*4882a593Smuzhiyun 	 * is performed. As a consequence, the think time state can
5325*4882a593Smuzhiyun 	 * become stable before the reset. Therefore there will be no
5326*4882a593Smuzhiyun 	 * state change when the 100 ms elapse, and no reset of the
5327*4882a593Smuzhiyun 	 * inject limit. The inject limit remains steadily equal to 1
5328*4882a593Smuzhiyun 	 * both during and after the 100 ms. So injection can be
5329*4882a593Smuzhiyun 	 * performed at all times, and throughput gets boosted.
5330*4882a593Smuzhiyun 	 *
5331*4882a593Smuzhiyun 	 * An inject limit equal to 1 is however in conflict, in
5332*4882a593Smuzhiyun 	 * general, with the fact that the think time of bfqq is
5333*4882a593Smuzhiyun 	 * short, because injection may be likely to delay bfqq's I/O
5334*4882a593Smuzhiyun 	 * (as explained in the comments in
5335*4882a593Smuzhiyun 	 * bfq_update_inject_limit()). But this does not happen in
5336*4882a593Smuzhiyun 	 * this special case, because bfqq's low think time is due to
5337*4882a593Smuzhiyun 	 * an effective handling of a synchronization, through
5338*4882a593Smuzhiyun 	 * injection. In this special case, bfqq's I/O does not get
5339*4882a593Smuzhiyun 	 * delayed by injection; on the contrary, bfqq's I/O is
5340*4882a593Smuzhiyun 	 * brought forward, because it is not blocked for
5341*4882a593Smuzhiyun 	 * milliseconds.
5342*4882a593Smuzhiyun 	 *
5343*4882a593Smuzhiyun 	 * In addition, serving the blocking I/O much sooner, and much
5344*4882a593Smuzhiyun 	 * more frequently than once per I/O-plugging timeout, makes
5345*4882a593Smuzhiyun 	 * it much quicker to detect a waker queue (the concept of
5346*4882a593Smuzhiyun 	 * waker queue is defined in the comments in
5347*4882a593Smuzhiyun 	 * bfq_add_request()). This makes it possible to start sooner
5348*4882a593Smuzhiyun 	 * to boost throughput more effectively, by injecting the I/O
5349*4882a593Smuzhiyun 	 * of the waker queue unconditionally on every
5350*4882a593Smuzhiyun 	 * bfq_dispatch_request().
5351*4882a593Smuzhiyun 	 *
5352*4882a593Smuzhiyun 	 * One last, important benefit of not resetting the inject
5353*4882a593Smuzhiyun 	 * limit before 100 ms is that, during this time interval, the
5354*4882a593Smuzhiyun 	 * base value for the total service time is likely to get
5355*4882a593Smuzhiyun 	 * finally computed for bfqq, freeing the inject limit from
5356*4882a593Smuzhiyun 	 * its relation with the think time.
5357*4882a593Smuzhiyun 	 */
5358*4882a593Smuzhiyun 	if (state_changed && bfqq->last_serv_time_ns == 0 &&
5359*4882a593Smuzhiyun 	    (time_is_before_eq_jiffies(bfqq->decrease_time_jif +
5360*4882a593Smuzhiyun 				      msecs_to_jiffies(100)) ||
5361*4882a593Smuzhiyun 	     !has_short_ttime))
5362*4882a593Smuzhiyun 		bfq_reset_inject_limit(bfqd, bfqq);
5363*4882a593Smuzhiyun }
5364*4882a593Smuzhiyun 
5365*4882a593Smuzhiyun /*
5366*4882a593Smuzhiyun  * Called when a new fs request (rq) is added to bfqq.  Check if there's
5367*4882a593Smuzhiyun  * something we should do about it.
5368*4882a593Smuzhiyun  */
bfq_rq_enqueued(struct bfq_data * bfqd,struct bfq_queue * bfqq,struct request * rq)5369*4882a593Smuzhiyun static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
5370*4882a593Smuzhiyun 			    struct request *rq)
5371*4882a593Smuzhiyun {
5372*4882a593Smuzhiyun 	if (rq->cmd_flags & REQ_META)
5373*4882a593Smuzhiyun 		bfqq->meta_pending++;
5374*4882a593Smuzhiyun 
5375*4882a593Smuzhiyun 	bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
5376*4882a593Smuzhiyun 
5377*4882a593Smuzhiyun 	if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
5378*4882a593Smuzhiyun 		bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
5379*4882a593Smuzhiyun 				 blk_rq_sectors(rq) < 32;
5380*4882a593Smuzhiyun 		bool budget_timeout = bfq_bfqq_budget_timeout(bfqq);
5381*4882a593Smuzhiyun 
5382*4882a593Smuzhiyun 		/*
5383*4882a593Smuzhiyun 		 * There is just this request queued: if
5384*4882a593Smuzhiyun 		 * - the request is small, and
5385*4882a593Smuzhiyun 		 * - we are idling to boost throughput, and
5386*4882a593Smuzhiyun 		 * - the queue is not to be expired,
5387*4882a593Smuzhiyun 		 * then just exit.
5388*4882a593Smuzhiyun 		 *
5389*4882a593Smuzhiyun 		 * In this way, if the device is being idled to wait
5390*4882a593Smuzhiyun 		 * for a new request from the in-service queue, we
5391*4882a593Smuzhiyun 		 * avoid unplugging the device and committing the
5392*4882a593Smuzhiyun 		 * device to serve just a small request. In contrast
5393*4882a593Smuzhiyun 		 * we wait for the block layer to decide when to
5394*4882a593Smuzhiyun 		 * unplug the device: hopefully, new requests will be
5395*4882a593Smuzhiyun 		 * merged to this one quickly, then the device will be
5396*4882a593Smuzhiyun 		 * unplugged and larger requests will be dispatched.
5397*4882a593Smuzhiyun 		 */
5398*4882a593Smuzhiyun 		if (small_req && idling_boosts_thr_without_issues(bfqd, bfqq) &&
5399*4882a593Smuzhiyun 		    !budget_timeout)
5400*4882a593Smuzhiyun 			return;
5401*4882a593Smuzhiyun 
5402*4882a593Smuzhiyun 		/*
5403*4882a593Smuzhiyun 		 * A large enough request arrived, or idling is being
5404*4882a593Smuzhiyun 		 * performed to preserve service guarantees, or
5405*4882a593Smuzhiyun 		 * finally the queue is to be expired: in all these
5406*4882a593Smuzhiyun 		 * cases disk idling is to be stopped, so clear
5407*4882a593Smuzhiyun 		 * wait_request flag and reset timer.
5408*4882a593Smuzhiyun 		 */
5409*4882a593Smuzhiyun 		bfq_clear_bfqq_wait_request(bfqq);
5410*4882a593Smuzhiyun 		hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
5411*4882a593Smuzhiyun 
5412*4882a593Smuzhiyun 		/*
5413*4882a593Smuzhiyun 		 * The queue is not empty, because a new request just
5414*4882a593Smuzhiyun 		 * arrived. Hence we can safely expire the queue, in
5415*4882a593Smuzhiyun 		 * case of budget timeout, without risking that the
5416*4882a593Smuzhiyun 		 * timestamps of the queue are not updated correctly.
5417*4882a593Smuzhiyun 		 * See [1] for more details.
5418*4882a593Smuzhiyun 		 */
5419*4882a593Smuzhiyun 		if (budget_timeout)
5420*4882a593Smuzhiyun 			bfq_bfqq_expire(bfqd, bfqq, false,
5421*4882a593Smuzhiyun 					BFQQE_BUDGET_TIMEOUT);
5422*4882a593Smuzhiyun 	}
5423*4882a593Smuzhiyun }
5424*4882a593Smuzhiyun 
5425*4882a593Smuzhiyun /* returns true if it causes the idle timer to be disabled */
__bfq_insert_request(struct bfq_data * bfqd,struct request * rq)5426*4882a593Smuzhiyun static bool __bfq_insert_request(struct bfq_data *bfqd, struct request *rq)
5427*4882a593Smuzhiyun {
5428*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq),
5429*4882a593Smuzhiyun 		*new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
5430*4882a593Smuzhiyun 	bool waiting, idle_timer_disabled = false;
5431*4882a593Smuzhiyun 
5432*4882a593Smuzhiyun 	if (new_bfqq) {
5433*4882a593Smuzhiyun 		/*
5434*4882a593Smuzhiyun 		 * Release the request's reference to the old bfqq
5435*4882a593Smuzhiyun 		 * and make sure one is taken to the shared queue.
5436*4882a593Smuzhiyun 		 */
5437*4882a593Smuzhiyun 		new_bfqq->allocated++;
5438*4882a593Smuzhiyun 		bfqq->allocated--;
5439*4882a593Smuzhiyun 		new_bfqq->ref++;
5440*4882a593Smuzhiyun 		/*
5441*4882a593Smuzhiyun 		 * If the bic associated with the process
5442*4882a593Smuzhiyun 		 * issuing this request still points to bfqq
5443*4882a593Smuzhiyun 		 * (and thus has not been already redirected
5444*4882a593Smuzhiyun 		 * to new_bfqq or even some other bfq_queue),
5445*4882a593Smuzhiyun 		 * then complete the merge and redirect it to
5446*4882a593Smuzhiyun 		 * new_bfqq.
5447*4882a593Smuzhiyun 		 */
5448*4882a593Smuzhiyun 		if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
5449*4882a593Smuzhiyun 			bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
5450*4882a593Smuzhiyun 					bfqq, new_bfqq);
5451*4882a593Smuzhiyun 
5452*4882a593Smuzhiyun 		bfq_clear_bfqq_just_created(bfqq);
5453*4882a593Smuzhiyun 		/*
5454*4882a593Smuzhiyun 		 * rq is about to be enqueued into new_bfqq,
5455*4882a593Smuzhiyun 		 * release rq reference on bfqq
5456*4882a593Smuzhiyun 		 */
5457*4882a593Smuzhiyun 		bfq_put_queue(bfqq);
5458*4882a593Smuzhiyun 		rq->elv.priv[1] = new_bfqq;
5459*4882a593Smuzhiyun 		bfqq = new_bfqq;
5460*4882a593Smuzhiyun 	}
5461*4882a593Smuzhiyun 
5462*4882a593Smuzhiyun 	bfq_update_io_thinktime(bfqd, bfqq);
5463*4882a593Smuzhiyun 	bfq_update_has_short_ttime(bfqd, bfqq, RQ_BIC(rq));
5464*4882a593Smuzhiyun 	bfq_update_io_seektime(bfqd, bfqq, rq);
5465*4882a593Smuzhiyun 
5466*4882a593Smuzhiyun 	waiting = bfqq && bfq_bfqq_wait_request(bfqq);
5467*4882a593Smuzhiyun 	bfq_add_request(rq);
5468*4882a593Smuzhiyun 	idle_timer_disabled = waiting && !bfq_bfqq_wait_request(bfqq);
5469*4882a593Smuzhiyun 
5470*4882a593Smuzhiyun 	rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)];
5471*4882a593Smuzhiyun 	list_add_tail(&rq->queuelist, &bfqq->fifo);
5472*4882a593Smuzhiyun 
5473*4882a593Smuzhiyun 	bfq_rq_enqueued(bfqd, bfqq, rq);
5474*4882a593Smuzhiyun 
5475*4882a593Smuzhiyun 	return idle_timer_disabled;
5476*4882a593Smuzhiyun }
5477*4882a593Smuzhiyun 
5478*4882a593Smuzhiyun #ifdef CONFIG_BFQ_CGROUP_DEBUG
bfq_update_insert_stats(struct request_queue * q,struct bfq_queue * bfqq,bool idle_timer_disabled,unsigned int cmd_flags)5479*4882a593Smuzhiyun static void bfq_update_insert_stats(struct request_queue *q,
5480*4882a593Smuzhiyun 				    struct bfq_queue *bfqq,
5481*4882a593Smuzhiyun 				    bool idle_timer_disabled,
5482*4882a593Smuzhiyun 				    unsigned int cmd_flags)
5483*4882a593Smuzhiyun {
5484*4882a593Smuzhiyun 	if (!bfqq)
5485*4882a593Smuzhiyun 		return;
5486*4882a593Smuzhiyun 
5487*4882a593Smuzhiyun 	/*
5488*4882a593Smuzhiyun 	 * bfqq still exists, because it can disappear only after
5489*4882a593Smuzhiyun 	 * either it is merged with another queue, or the process it
5490*4882a593Smuzhiyun 	 * is associated with exits. But both actions must be taken by
5491*4882a593Smuzhiyun 	 * the same process currently executing this flow of
5492*4882a593Smuzhiyun 	 * instructions.
5493*4882a593Smuzhiyun 	 *
5494*4882a593Smuzhiyun 	 * In addition, the following queue lock guarantees that
5495*4882a593Smuzhiyun 	 * bfqq_group(bfqq) exists as well.
5496*4882a593Smuzhiyun 	 */
5497*4882a593Smuzhiyun 	spin_lock_irq(&q->queue_lock);
5498*4882a593Smuzhiyun 	bfqg_stats_update_io_add(bfqq_group(bfqq), bfqq, cmd_flags);
5499*4882a593Smuzhiyun 	if (idle_timer_disabled)
5500*4882a593Smuzhiyun 		bfqg_stats_update_idle_time(bfqq_group(bfqq));
5501*4882a593Smuzhiyun 	spin_unlock_irq(&q->queue_lock);
5502*4882a593Smuzhiyun }
5503*4882a593Smuzhiyun #else
bfq_update_insert_stats(struct request_queue * q,struct bfq_queue * bfqq,bool idle_timer_disabled,unsigned int cmd_flags)5504*4882a593Smuzhiyun static inline void bfq_update_insert_stats(struct request_queue *q,
5505*4882a593Smuzhiyun 					   struct bfq_queue *bfqq,
5506*4882a593Smuzhiyun 					   bool idle_timer_disabled,
5507*4882a593Smuzhiyun 					   unsigned int cmd_flags) {}
5508*4882a593Smuzhiyun #endif /* CONFIG_BFQ_CGROUP_DEBUG */
5509*4882a593Smuzhiyun 
5510*4882a593Smuzhiyun static struct bfq_queue *bfq_init_rq(struct request *rq);
5511*4882a593Smuzhiyun 
bfq_insert_request(struct blk_mq_hw_ctx * hctx,struct request * rq,bool at_head)5512*4882a593Smuzhiyun static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
5513*4882a593Smuzhiyun 			       bool at_head)
5514*4882a593Smuzhiyun {
5515*4882a593Smuzhiyun 	struct request_queue *q = hctx->queue;
5516*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
5517*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
5518*4882a593Smuzhiyun 	bool idle_timer_disabled = false;
5519*4882a593Smuzhiyun 	unsigned int cmd_flags;
5520*4882a593Smuzhiyun 
5521*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
5522*4882a593Smuzhiyun 	if (!cgroup_subsys_on_dfl(io_cgrp_subsys) && rq->bio)
5523*4882a593Smuzhiyun 		bfqg_stats_update_legacy_io(q, rq);
5524*4882a593Smuzhiyun #endif
5525*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
5526*4882a593Smuzhiyun 	bfqq = bfq_init_rq(rq);
5527*4882a593Smuzhiyun 	if (blk_mq_sched_try_insert_merge(q, rq)) {
5528*4882a593Smuzhiyun 		spin_unlock_irq(&bfqd->lock);
5529*4882a593Smuzhiyun 		return;
5530*4882a593Smuzhiyun 	}
5531*4882a593Smuzhiyun 
5532*4882a593Smuzhiyun 	blk_mq_sched_request_inserted(rq);
5533*4882a593Smuzhiyun 
5534*4882a593Smuzhiyun 	if (!bfqq || at_head || blk_rq_is_passthrough(rq)) {
5535*4882a593Smuzhiyun 		if (at_head)
5536*4882a593Smuzhiyun 			list_add(&rq->queuelist, &bfqd->dispatch);
5537*4882a593Smuzhiyun 		else
5538*4882a593Smuzhiyun 			list_add_tail(&rq->queuelist, &bfqd->dispatch);
5539*4882a593Smuzhiyun 	} else {
5540*4882a593Smuzhiyun 		idle_timer_disabled = __bfq_insert_request(bfqd, rq);
5541*4882a593Smuzhiyun 		/*
5542*4882a593Smuzhiyun 		 * Update bfqq, because, if a queue merge has occurred
5543*4882a593Smuzhiyun 		 * in __bfq_insert_request, then rq has been
5544*4882a593Smuzhiyun 		 * redirected into a new queue.
5545*4882a593Smuzhiyun 		 */
5546*4882a593Smuzhiyun 		bfqq = RQ_BFQQ(rq);
5547*4882a593Smuzhiyun 
5548*4882a593Smuzhiyun 		if (rq_mergeable(rq)) {
5549*4882a593Smuzhiyun 			elv_rqhash_add(q, rq);
5550*4882a593Smuzhiyun 			if (!q->last_merge)
5551*4882a593Smuzhiyun 				q->last_merge = rq;
5552*4882a593Smuzhiyun 		}
5553*4882a593Smuzhiyun 	}
5554*4882a593Smuzhiyun 
5555*4882a593Smuzhiyun 	/*
5556*4882a593Smuzhiyun 	 * Cache cmd_flags before releasing scheduler lock, because rq
5557*4882a593Smuzhiyun 	 * may disappear afterwards (for example, because of a request
5558*4882a593Smuzhiyun 	 * merge).
5559*4882a593Smuzhiyun 	 */
5560*4882a593Smuzhiyun 	cmd_flags = rq->cmd_flags;
5561*4882a593Smuzhiyun 
5562*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
5563*4882a593Smuzhiyun 
5564*4882a593Smuzhiyun 	bfq_update_insert_stats(q, bfqq, idle_timer_disabled,
5565*4882a593Smuzhiyun 				cmd_flags);
5566*4882a593Smuzhiyun }
5567*4882a593Smuzhiyun 
bfq_insert_requests(struct blk_mq_hw_ctx * hctx,struct list_head * list,bool at_head)5568*4882a593Smuzhiyun static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
5569*4882a593Smuzhiyun 				struct list_head *list, bool at_head)
5570*4882a593Smuzhiyun {
5571*4882a593Smuzhiyun 	while (!list_empty(list)) {
5572*4882a593Smuzhiyun 		struct request *rq;
5573*4882a593Smuzhiyun 
5574*4882a593Smuzhiyun 		rq = list_first_entry(list, struct request, queuelist);
5575*4882a593Smuzhiyun 		list_del_init(&rq->queuelist);
5576*4882a593Smuzhiyun 		bfq_insert_request(hctx, rq, at_head);
5577*4882a593Smuzhiyun 	}
5578*4882a593Smuzhiyun }
5579*4882a593Smuzhiyun 
bfq_update_hw_tag(struct bfq_data * bfqd)5580*4882a593Smuzhiyun static void bfq_update_hw_tag(struct bfq_data *bfqd)
5581*4882a593Smuzhiyun {
5582*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfqd->in_service_queue;
5583*4882a593Smuzhiyun 
5584*4882a593Smuzhiyun 	bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver,
5585*4882a593Smuzhiyun 				       bfqd->rq_in_driver);
5586*4882a593Smuzhiyun 
5587*4882a593Smuzhiyun 	if (bfqd->hw_tag == 1)
5588*4882a593Smuzhiyun 		return;
5589*4882a593Smuzhiyun 
5590*4882a593Smuzhiyun 	/*
5591*4882a593Smuzhiyun 	 * This sample is valid if the number of outstanding requests
5592*4882a593Smuzhiyun 	 * is large enough to allow a queueing behavior.  Note that the
5593*4882a593Smuzhiyun 	 * sum is not exact, as it's not taking into account deactivated
5594*4882a593Smuzhiyun 	 * requests.
5595*4882a593Smuzhiyun 	 */
5596*4882a593Smuzhiyun 	if (bfqd->rq_in_driver + bfqd->queued <= BFQ_HW_QUEUE_THRESHOLD)
5597*4882a593Smuzhiyun 		return;
5598*4882a593Smuzhiyun 
5599*4882a593Smuzhiyun 	/*
5600*4882a593Smuzhiyun 	 * If active queue hasn't enough requests and can idle, bfq might not
5601*4882a593Smuzhiyun 	 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
5602*4882a593Smuzhiyun 	 * case
5603*4882a593Smuzhiyun 	 */
5604*4882a593Smuzhiyun 	if (bfqq && bfq_bfqq_has_short_ttime(bfqq) &&
5605*4882a593Smuzhiyun 	    bfqq->dispatched + bfqq->queued[0] + bfqq->queued[1] <
5606*4882a593Smuzhiyun 	    BFQ_HW_QUEUE_THRESHOLD &&
5607*4882a593Smuzhiyun 	    bfqd->rq_in_driver < BFQ_HW_QUEUE_THRESHOLD)
5608*4882a593Smuzhiyun 		return;
5609*4882a593Smuzhiyun 
5610*4882a593Smuzhiyun 	if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
5611*4882a593Smuzhiyun 		return;
5612*4882a593Smuzhiyun 
5613*4882a593Smuzhiyun 	bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
5614*4882a593Smuzhiyun 	bfqd->max_rq_in_driver = 0;
5615*4882a593Smuzhiyun 	bfqd->hw_tag_samples = 0;
5616*4882a593Smuzhiyun 
5617*4882a593Smuzhiyun 	bfqd->nonrot_with_queueing =
5618*4882a593Smuzhiyun 		blk_queue_nonrot(bfqd->queue) && bfqd->hw_tag;
5619*4882a593Smuzhiyun }
5620*4882a593Smuzhiyun 
bfq_completed_request(struct bfq_queue * bfqq,struct bfq_data * bfqd)5621*4882a593Smuzhiyun static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd)
5622*4882a593Smuzhiyun {
5623*4882a593Smuzhiyun 	u64 now_ns;
5624*4882a593Smuzhiyun 	u32 delta_us;
5625*4882a593Smuzhiyun 
5626*4882a593Smuzhiyun 	bfq_update_hw_tag(bfqd);
5627*4882a593Smuzhiyun 
5628*4882a593Smuzhiyun 	bfqd->rq_in_driver--;
5629*4882a593Smuzhiyun 	bfqq->dispatched--;
5630*4882a593Smuzhiyun 
5631*4882a593Smuzhiyun 	if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
5632*4882a593Smuzhiyun 		/*
5633*4882a593Smuzhiyun 		 * Set budget_timeout (which we overload to store the
5634*4882a593Smuzhiyun 		 * time at which the queue remains with no backlog and
5635*4882a593Smuzhiyun 		 * no outstanding request; used by the weight-raising
5636*4882a593Smuzhiyun 		 * mechanism).
5637*4882a593Smuzhiyun 		 */
5638*4882a593Smuzhiyun 		bfqq->budget_timeout = jiffies;
5639*4882a593Smuzhiyun 
5640*4882a593Smuzhiyun 		bfq_weights_tree_remove(bfqd, bfqq);
5641*4882a593Smuzhiyun 	}
5642*4882a593Smuzhiyun 
5643*4882a593Smuzhiyun 	now_ns = ktime_get_ns();
5644*4882a593Smuzhiyun 
5645*4882a593Smuzhiyun 	bfqq->ttime.last_end_request = now_ns;
5646*4882a593Smuzhiyun 
5647*4882a593Smuzhiyun 	/*
5648*4882a593Smuzhiyun 	 * Using us instead of ns, to get a reasonable precision in
5649*4882a593Smuzhiyun 	 * computing rate in next check.
5650*4882a593Smuzhiyun 	 */
5651*4882a593Smuzhiyun 	delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC);
5652*4882a593Smuzhiyun 
5653*4882a593Smuzhiyun 	/*
5654*4882a593Smuzhiyun 	 * If the request took rather long to complete, and, according
5655*4882a593Smuzhiyun 	 * to the maximum request size recorded, this completion latency
5656*4882a593Smuzhiyun 	 * implies that the request was certainly served at a very low
5657*4882a593Smuzhiyun 	 * rate (less than 1M sectors/sec), then the whole observation
5658*4882a593Smuzhiyun 	 * interval that lasts up to this time instant cannot be a
5659*4882a593Smuzhiyun 	 * valid time interval for computing a new peak rate.  Invoke
5660*4882a593Smuzhiyun 	 * bfq_update_rate_reset to have the following three steps
5661*4882a593Smuzhiyun 	 * taken:
5662*4882a593Smuzhiyun 	 * - close the observation interval at the last (previous)
5663*4882a593Smuzhiyun 	 *   request dispatch or completion
5664*4882a593Smuzhiyun 	 * - compute rate, if possible, for that observation interval
5665*4882a593Smuzhiyun 	 * - reset to zero samples, which will trigger a proper
5666*4882a593Smuzhiyun 	 *   re-initialization of the observation interval on next
5667*4882a593Smuzhiyun 	 *   dispatch
5668*4882a593Smuzhiyun 	 */
5669*4882a593Smuzhiyun 	if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC &&
5670*4882a593Smuzhiyun 	   (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us <
5671*4882a593Smuzhiyun 			1UL<<(BFQ_RATE_SHIFT - 10))
5672*4882a593Smuzhiyun 		bfq_update_rate_reset(bfqd, NULL);
5673*4882a593Smuzhiyun 	bfqd->last_completion = now_ns;
5674*4882a593Smuzhiyun 	bfqd->last_completed_rq_bfqq = bfqq;
5675*4882a593Smuzhiyun 
5676*4882a593Smuzhiyun 	/*
5677*4882a593Smuzhiyun 	 * If we are waiting to discover whether the request pattern
5678*4882a593Smuzhiyun 	 * of the task associated with the queue is actually
5679*4882a593Smuzhiyun 	 * isochronous, and both requisites for this condition to hold
5680*4882a593Smuzhiyun 	 * are now satisfied, then compute soft_rt_next_start (see the
5681*4882a593Smuzhiyun 	 * comments on the function bfq_bfqq_softrt_next_start()). We
5682*4882a593Smuzhiyun 	 * do not compute soft_rt_next_start if bfqq is in interactive
5683*4882a593Smuzhiyun 	 * weight raising (see the comments in bfq_bfqq_expire() for
5684*4882a593Smuzhiyun 	 * an explanation). We schedule this delayed update when bfqq
5685*4882a593Smuzhiyun 	 * expires, if it still has in-flight requests.
5686*4882a593Smuzhiyun 	 */
5687*4882a593Smuzhiyun 	if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
5688*4882a593Smuzhiyun 	    RB_EMPTY_ROOT(&bfqq->sort_list) &&
5689*4882a593Smuzhiyun 	    bfqq->wr_coeff != bfqd->bfq_wr_coeff)
5690*4882a593Smuzhiyun 		bfqq->soft_rt_next_start =
5691*4882a593Smuzhiyun 			bfq_bfqq_softrt_next_start(bfqd, bfqq);
5692*4882a593Smuzhiyun 
5693*4882a593Smuzhiyun 	/*
5694*4882a593Smuzhiyun 	 * If this is the in-service queue, check if it needs to be expired,
5695*4882a593Smuzhiyun 	 * or if we want to idle in case it has no pending requests.
5696*4882a593Smuzhiyun 	 */
5697*4882a593Smuzhiyun 	if (bfqd->in_service_queue == bfqq) {
5698*4882a593Smuzhiyun 		if (bfq_bfqq_must_idle(bfqq)) {
5699*4882a593Smuzhiyun 			if (bfqq->dispatched == 0)
5700*4882a593Smuzhiyun 				bfq_arm_slice_timer(bfqd);
5701*4882a593Smuzhiyun 			/*
5702*4882a593Smuzhiyun 			 * If we get here, we do not expire bfqq, even
5703*4882a593Smuzhiyun 			 * if bfqq was in budget timeout or had no
5704*4882a593Smuzhiyun 			 * more requests (as controlled in the next
5705*4882a593Smuzhiyun 			 * conditional instructions). The reason for
5706*4882a593Smuzhiyun 			 * not expiring bfqq is as follows.
5707*4882a593Smuzhiyun 			 *
5708*4882a593Smuzhiyun 			 * Here bfqq->dispatched > 0 holds, but
5709*4882a593Smuzhiyun 			 * bfq_bfqq_must_idle() returned true. This
5710*4882a593Smuzhiyun 			 * implies that, even if no request arrives
5711*4882a593Smuzhiyun 			 * for bfqq before bfqq->dispatched reaches 0,
5712*4882a593Smuzhiyun 			 * bfqq will, however, not be expired on the
5713*4882a593Smuzhiyun 			 * completion event that causes bfqq->dispatch
5714*4882a593Smuzhiyun 			 * to reach zero. In contrast, on this event,
5715*4882a593Smuzhiyun 			 * bfqq will start enjoying device idling
5716*4882a593Smuzhiyun 			 * (I/O-dispatch plugging).
5717*4882a593Smuzhiyun 			 *
5718*4882a593Smuzhiyun 			 * But, if we expired bfqq here, bfqq would
5719*4882a593Smuzhiyun 			 * not have the chance to enjoy device idling
5720*4882a593Smuzhiyun 			 * when bfqq->dispatched finally reaches
5721*4882a593Smuzhiyun 			 * zero. This would expose bfqq to violation
5722*4882a593Smuzhiyun 			 * of its reserved service guarantees.
5723*4882a593Smuzhiyun 			 */
5724*4882a593Smuzhiyun 			return;
5725*4882a593Smuzhiyun 		} else if (bfq_may_expire_for_budg_timeout(bfqq))
5726*4882a593Smuzhiyun 			bfq_bfqq_expire(bfqd, bfqq, false,
5727*4882a593Smuzhiyun 					BFQQE_BUDGET_TIMEOUT);
5728*4882a593Smuzhiyun 		else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
5729*4882a593Smuzhiyun 			 (bfqq->dispatched == 0 ||
5730*4882a593Smuzhiyun 			  !bfq_better_to_idle(bfqq)))
5731*4882a593Smuzhiyun 			bfq_bfqq_expire(bfqd, bfqq, false,
5732*4882a593Smuzhiyun 					BFQQE_NO_MORE_REQUESTS);
5733*4882a593Smuzhiyun 	}
5734*4882a593Smuzhiyun 
5735*4882a593Smuzhiyun 	if (!bfqd->rq_in_driver)
5736*4882a593Smuzhiyun 		bfq_schedule_dispatch(bfqd);
5737*4882a593Smuzhiyun }
5738*4882a593Smuzhiyun 
bfq_finish_requeue_request_body(struct bfq_queue * bfqq)5739*4882a593Smuzhiyun static void bfq_finish_requeue_request_body(struct bfq_queue *bfqq)
5740*4882a593Smuzhiyun {
5741*4882a593Smuzhiyun 	bfqq->allocated--;
5742*4882a593Smuzhiyun 
5743*4882a593Smuzhiyun 	bfq_put_queue(bfqq);
5744*4882a593Smuzhiyun }
5745*4882a593Smuzhiyun 
5746*4882a593Smuzhiyun /*
5747*4882a593Smuzhiyun  * The processes associated with bfqq may happen to generate their
5748*4882a593Smuzhiyun  * cumulative I/O at a lower rate than the rate at which the device
5749*4882a593Smuzhiyun  * could serve the same I/O. This is rather probable, e.g., if only
5750*4882a593Smuzhiyun  * one process is associated with bfqq and the device is an SSD. It
5751*4882a593Smuzhiyun  * results in bfqq becoming often empty while in service. In this
5752*4882a593Smuzhiyun  * respect, if BFQ is allowed to switch to another queue when bfqq
5753*4882a593Smuzhiyun  * remains empty, then the device goes on being fed with I/O requests,
5754*4882a593Smuzhiyun  * and the throughput is not affected. In contrast, if BFQ is not
5755*4882a593Smuzhiyun  * allowed to switch to another queue---because bfqq is sync and
5756*4882a593Smuzhiyun  * I/O-dispatch needs to be plugged while bfqq is temporarily
5757*4882a593Smuzhiyun  * empty---then, during the service of bfqq, there will be frequent
5758*4882a593Smuzhiyun  * "service holes", i.e., time intervals during which bfqq gets empty
5759*4882a593Smuzhiyun  * and the device can only consume the I/O already queued in its
5760*4882a593Smuzhiyun  * hardware queues. During service holes, the device may even get to
5761*4882a593Smuzhiyun  * remaining idle. In the end, during the service of bfqq, the device
5762*4882a593Smuzhiyun  * is driven at a lower speed than the one it can reach with the kind
5763*4882a593Smuzhiyun  * of I/O flowing through bfqq.
5764*4882a593Smuzhiyun  *
5765*4882a593Smuzhiyun  * To counter this loss of throughput, BFQ implements a "request
5766*4882a593Smuzhiyun  * injection mechanism", which tries to fill the above service holes
5767*4882a593Smuzhiyun  * with I/O requests taken from other queues. The hard part in this
5768*4882a593Smuzhiyun  * mechanism is finding the right amount of I/O to inject, so as to
5769*4882a593Smuzhiyun  * both boost throughput and not break bfqq's bandwidth and latency
5770*4882a593Smuzhiyun  * guarantees. In this respect, the mechanism maintains a per-queue
5771*4882a593Smuzhiyun  * inject limit, computed as below. While bfqq is empty, the injection
5772*4882a593Smuzhiyun  * mechanism dispatches extra I/O requests only until the total number
5773*4882a593Smuzhiyun  * of I/O requests in flight---i.e., already dispatched but not yet
5774*4882a593Smuzhiyun  * completed---remains lower than this limit.
5775*4882a593Smuzhiyun  *
5776*4882a593Smuzhiyun  * A first definition comes in handy to introduce the algorithm by
5777*4882a593Smuzhiyun  * which the inject limit is computed.  We define as first request for
5778*4882a593Smuzhiyun  * bfqq, an I/O request for bfqq that arrives while bfqq is in
5779*4882a593Smuzhiyun  * service, and causes bfqq to switch from empty to non-empty. The
5780*4882a593Smuzhiyun  * algorithm updates the limit as a function of the effect of
5781*4882a593Smuzhiyun  * injection on the service times of only the first requests of
5782*4882a593Smuzhiyun  * bfqq. The reason for this restriction is that these are the
5783*4882a593Smuzhiyun  * requests whose service time is affected most, because they are the
5784*4882a593Smuzhiyun  * first to arrive after injection possibly occurred.
5785*4882a593Smuzhiyun  *
5786*4882a593Smuzhiyun  * To evaluate the effect of injection, the algorithm measures the
5787*4882a593Smuzhiyun  * "total service time" of first requests. We define as total service
5788*4882a593Smuzhiyun  * time of an I/O request, the time that elapses since when the
5789*4882a593Smuzhiyun  * request is enqueued into bfqq, to when it is completed. This
5790*4882a593Smuzhiyun  * quantity allows the whole effect of injection to be measured. It is
5791*4882a593Smuzhiyun  * easy to see why. Suppose that some requests of other queues are
5792*4882a593Smuzhiyun  * actually injected while bfqq is empty, and that a new request R
5793*4882a593Smuzhiyun  * then arrives for bfqq. If the device does start to serve all or
5794*4882a593Smuzhiyun  * part of the injected requests during the service hole, then,
5795*4882a593Smuzhiyun  * because of this extra service, it may delay the next invocation of
5796*4882a593Smuzhiyun  * the dispatch hook of BFQ. Then, even after R gets eventually
5797*4882a593Smuzhiyun  * dispatched, the device may delay the actual service of R if it is
5798*4882a593Smuzhiyun  * still busy serving the extra requests, or if it decides to serve,
5799*4882a593Smuzhiyun  * before R, some extra request still present in its queues. As a
5800*4882a593Smuzhiyun  * conclusion, the cumulative extra delay caused by injection can be
5801*4882a593Smuzhiyun  * easily evaluated by just comparing the total service time of first
5802*4882a593Smuzhiyun  * requests with and without injection.
5803*4882a593Smuzhiyun  *
5804*4882a593Smuzhiyun  * The limit-update algorithm works as follows. On the arrival of a
5805*4882a593Smuzhiyun  * first request of bfqq, the algorithm measures the total time of the
5806*4882a593Smuzhiyun  * request only if one of the three cases below holds, and, for each
5807*4882a593Smuzhiyun  * case, it updates the limit as described below:
5808*4882a593Smuzhiyun  *
5809*4882a593Smuzhiyun  * (1) If there is no in-flight request. This gives a baseline for the
5810*4882a593Smuzhiyun  *     total service time of the requests of bfqq. If the baseline has
5811*4882a593Smuzhiyun  *     not been computed yet, then, after computing it, the limit is
5812*4882a593Smuzhiyun  *     set to 1, to start boosting throughput, and to prepare the
5813*4882a593Smuzhiyun  *     ground for the next case. If the baseline has already been
5814*4882a593Smuzhiyun  *     computed, then it is updated, in case it results to be lower
5815*4882a593Smuzhiyun  *     than the previous value.
5816*4882a593Smuzhiyun  *
5817*4882a593Smuzhiyun  * (2) If the limit is higher than 0 and there are in-flight
5818*4882a593Smuzhiyun  *     requests. By comparing the total service time in this case with
5819*4882a593Smuzhiyun  *     the above baseline, it is possible to know at which extent the
5820*4882a593Smuzhiyun  *     current value of the limit is inflating the total service
5821*4882a593Smuzhiyun  *     time. If the inflation is below a certain threshold, then bfqq
5822*4882a593Smuzhiyun  *     is assumed to be suffering from no perceivable loss of its
5823*4882a593Smuzhiyun  *     service guarantees, and the limit is even tentatively
5824*4882a593Smuzhiyun  *     increased. If the inflation is above the threshold, then the
5825*4882a593Smuzhiyun  *     limit is decreased. Due to the lack of any hysteresis, this
5826*4882a593Smuzhiyun  *     logic makes the limit oscillate even in steady workload
5827*4882a593Smuzhiyun  *     conditions. Yet we opted for it, because it is fast in reaching
5828*4882a593Smuzhiyun  *     the best value for the limit, as a function of the current I/O
5829*4882a593Smuzhiyun  *     workload. To reduce oscillations, this step is disabled for a
5830*4882a593Smuzhiyun  *     short time interval after the limit happens to be decreased.
5831*4882a593Smuzhiyun  *
5832*4882a593Smuzhiyun  * (3) Periodically, after resetting the limit, to make sure that the
5833*4882a593Smuzhiyun  *     limit eventually drops in case the workload changes. This is
5834*4882a593Smuzhiyun  *     needed because, after the limit has gone safely up for a
5835*4882a593Smuzhiyun  *     certain workload, it is impossible to guess whether the
5836*4882a593Smuzhiyun  *     baseline total service time may have changed, without measuring
5837*4882a593Smuzhiyun  *     it again without injection. A more effective version of this
5838*4882a593Smuzhiyun  *     step might be to just sample the baseline, by interrupting
5839*4882a593Smuzhiyun  *     injection only once, and then to reset/lower the limit only if
5840*4882a593Smuzhiyun  *     the total service time with the current limit does happen to be
5841*4882a593Smuzhiyun  *     too large.
5842*4882a593Smuzhiyun  *
5843*4882a593Smuzhiyun  * More details on each step are provided in the comments on the
5844*4882a593Smuzhiyun  * pieces of code that implement these steps: the branch handling the
5845*4882a593Smuzhiyun  * transition from empty to non empty in bfq_add_request(), the branch
5846*4882a593Smuzhiyun  * handling injection in bfq_select_queue(), and the function
5847*4882a593Smuzhiyun  * bfq_choose_bfqq_for_injection(). These comments also explain some
5848*4882a593Smuzhiyun  * exceptions, made by the injection mechanism in some special cases.
5849*4882a593Smuzhiyun  */
bfq_update_inject_limit(struct bfq_data * bfqd,struct bfq_queue * bfqq)5850*4882a593Smuzhiyun static void bfq_update_inject_limit(struct bfq_data *bfqd,
5851*4882a593Smuzhiyun 				    struct bfq_queue *bfqq)
5852*4882a593Smuzhiyun {
5853*4882a593Smuzhiyun 	u64 tot_time_ns = ktime_get_ns() - bfqd->last_empty_occupied_ns;
5854*4882a593Smuzhiyun 	unsigned int old_limit = bfqq->inject_limit;
5855*4882a593Smuzhiyun 
5856*4882a593Smuzhiyun 	if (bfqq->last_serv_time_ns > 0 && bfqd->rqs_injected) {
5857*4882a593Smuzhiyun 		u64 threshold = (bfqq->last_serv_time_ns * 3)>>1;
5858*4882a593Smuzhiyun 
5859*4882a593Smuzhiyun 		if (tot_time_ns >= threshold && old_limit > 0) {
5860*4882a593Smuzhiyun 			bfqq->inject_limit--;
5861*4882a593Smuzhiyun 			bfqq->decrease_time_jif = jiffies;
5862*4882a593Smuzhiyun 		} else if (tot_time_ns < threshold &&
5863*4882a593Smuzhiyun 			   old_limit <= bfqd->max_rq_in_driver)
5864*4882a593Smuzhiyun 			bfqq->inject_limit++;
5865*4882a593Smuzhiyun 	}
5866*4882a593Smuzhiyun 
5867*4882a593Smuzhiyun 	/*
5868*4882a593Smuzhiyun 	 * Either we still have to compute the base value for the
5869*4882a593Smuzhiyun 	 * total service time, and there seem to be the right
5870*4882a593Smuzhiyun 	 * conditions to do it, or we can lower the last base value
5871*4882a593Smuzhiyun 	 * computed.
5872*4882a593Smuzhiyun 	 *
5873*4882a593Smuzhiyun 	 * NOTE: (bfqd->rq_in_driver == 1) means that there is no I/O
5874*4882a593Smuzhiyun 	 * request in flight, because this function is in the code
5875*4882a593Smuzhiyun 	 * path that handles the completion of a request of bfqq, and,
5876*4882a593Smuzhiyun 	 * in particular, this function is executed before
5877*4882a593Smuzhiyun 	 * bfqd->rq_in_driver is decremented in such a code path.
5878*4882a593Smuzhiyun 	 */
5879*4882a593Smuzhiyun 	if ((bfqq->last_serv_time_ns == 0 && bfqd->rq_in_driver == 1) ||
5880*4882a593Smuzhiyun 	    tot_time_ns < bfqq->last_serv_time_ns) {
5881*4882a593Smuzhiyun 		if (bfqq->last_serv_time_ns == 0) {
5882*4882a593Smuzhiyun 			/*
5883*4882a593Smuzhiyun 			 * Now we certainly have a base value: make sure we
5884*4882a593Smuzhiyun 			 * start trying injection.
5885*4882a593Smuzhiyun 			 */
5886*4882a593Smuzhiyun 			bfqq->inject_limit = max_t(unsigned int, 1, old_limit);
5887*4882a593Smuzhiyun 		}
5888*4882a593Smuzhiyun 		bfqq->last_serv_time_ns = tot_time_ns;
5889*4882a593Smuzhiyun 	} else if (!bfqd->rqs_injected && bfqd->rq_in_driver == 1)
5890*4882a593Smuzhiyun 		/*
5891*4882a593Smuzhiyun 		 * No I/O injected and no request still in service in
5892*4882a593Smuzhiyun 		 * the drive: these are the exact conditions for
5893*4882a593Smuzhiyun 		 * computing the base value of the total service time
5894*4882a593Smuzhiyun 		 * for bfqq. So let's update this value, because it is
5895*4882a593Smuzhiyun 		 * rather variable. For example, it varies if the size
5896*4882a593Smuzhiyun 		 * or the spatial locality of the I/O requests in bfqq
5897*4882a593Smuzhiyun 		 * change.
5898*4882a593Smuzhiyun 		 */
5899*4882a593Smuzhiyun 		bfqq->last_serv_time_ns = tot_time_ns;
5900*4882a593Smuzhiyun 
5901*4882a593Smuzhiyun 
5902*4882a593Smuzhiyun 	/* update complete, not waiting for any request completion any longer */
5903*4882a593Smuzhiyun 	bfqd->waited_rq = NULL;
5904*4882a593Smuzhiyun 	bfqd->rqs_injected = false;
5905*4882a593Smuzhiyun }
5906*4882a593Smuzhiyun 
5907*4882a593Smuzhiyun /*
5908*4882a593Smuzhiyun  * Handle either a requeue or a finish for rq. The things to do are
5909*4882a593Smuzhiyun  * the same in both cases: all references to rq are to be dropped. In
5910*4882a593Smuzhiyun  * particular, rq is considered completed from the point of view of
5911*4882a593Smuzhiyun  * the scheduler.
5912*4882a593Smuzhiyun  */
bfq_finish_requeue_request(struct request * rq)5913*4882a593Smuzhiyun static void bfq_finish_requeue_request(struct request *rq)
5914*4882a593Smuzhiyun {
5915*4882a593Smuzhiyun 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
5916*4882a593Smuzhiyun 	struct bfq_data *bfqd;
5917*4882a593Smuzhiyun 
5918*4882a593Smuzhiyun 	/*
5919*4882a593Smuzhiyun 	 * rq either is not associated with any icq, or is an already
5920*4882a593Smuzhiyun 	 * requeued request that has not (yet) been re-inserted into
5921*4882a593Smuzhiyun 	 * a bfq_queue.
5922*4882a593Smuzhiyun 	 */
5923*4882a593Smuzhiyun 	if (!rq->elv.icq || !bfqq)
5924*4882a593Smuzhiyun 		return;
5925*4882a593Smuzhiyun 
5926*4882a593Smuzhiyun 	bfqd = bfqq->bfqd;
5927*4882a593Smuzhiyun 
5928*4882a593Smuzhiyun 	if (rq->rq_flags & RQF_STARTED)
5929*4882a593Smuzhiyun 		bfqg_stats_update_completion(bfqq_group(bfqq),
5930*4882a593Smuzhiyun 					     rq->start_time_ns,
5931*4882a593Smuzhiyun 					     rq->io_start_time_ns,
5932*4882a593Smuzhiyun 					     rq->cmd_flags);
5933*4882a593Smuzhiyun 
5934*4882a593Smuzhiyun 	if (likely(rq->rq_flags & RQF_STARTED)) {
5935*4882a593Smuzhiyun 		unsigned long flags;
5936*4882a593Smuzhiyun 
5937*4882a593Smuzhiyun 		spin_lock_irqsave(&bfqd->lock, flags);
5938*4882a593Smuzhiyun 
5939*4882a593Smuzhiyun 		if (rq == bfqd->waited_rq)
5940*4882a593Smuzhiyun 			bfq_update_inject_limit(bfqd, bfqq);
5941*4882a593Smuzhiyun 
5942*4882a593Smuzhiyun 		bfq_completed_request(bfqq, bfqd);
5943*4882a593Smuzhiyun 		bfq_finish_requeue_request_body(bfqq);
5944*4882a593Smuzhiyun 
5945*4882a593Smuzhiyun 		spin_unlock_irqrestore(&bfqd->lock, flags);
5946*4882a593Smuzhiyun 	} else {
5947*4882a593Smuzhiyun 		/*
5948*4882a593Smuzhiyun 		 * Request rq may be still/already in the scheduler,
5949*4882a593Smuzhiyun 		 * in which case we need to remove it (this should
5950*4882a593Smuzhiyun 		 * never happen in case of requeue). And we cannot
5951*4882a593Smuzhiyun 		 * defer such a check and removal, to avoid
5952*4882a593Smuzhiyun 		 * inconsistencies in the time interval from the end
5953*4882a593Smuzhiyun 		 * of this function to the start of the deferred work.
5954*4882a593Smuzhiyun 		 * This situation seems to occur only in process
5955*4882a593Smuzhiyun 		 * context, as a consequence of a merge. In the
5956*4882a593Smuzhiyun 		 * current version of the code, this implies that the
5957*4882a593Smuzhiyun 		 * lock is held.
5958*4882a593Smuzhiyun 		 */
5959*4882a593Smuzhiyun 
5960*4882a593Smuzhiyun 		if (!RB_EMPTY_NODE(&rq->rb_node)) {
5961*4882a593Smuzhiyun 			bfq_remove_request(rq->q, rq);
5962*4882a593Smuzhiyun 			bfqg_stats_update_io_remove(bfqq_group(bfqq),
5963*4882a593Smuzhiyun 						    rq->cmd_flags);
5964*4882a593Smuzhiyun 		}
5965*4882a593Smuzhiyun 		bfq_finish_requeue_request_body(bfqq);
5966*4882a593Smuzhiyun 	}
5967*4882a593Smuzhiyun 
5968*4882a593Smuzhiyun 	/*
5969*4882a593Smuzhiyun 	 * Reset private fields. In case of a requeue, this allows
5970*4882a593Smuzhiyun 	 * this function to correctly do nothing if it is spuriously
5971*4882a593Smuzhiyun 	 * invoked again on this same request (see the check at the
5972*4882a593Smuzhiyun 	 * beginning of the function). Probably, a better general
5973*4882a593Smuzhiyun 	 * design would be to prevent blk-mq from invoking the requeue
5974*4882a593Smuzhiyun 	 * or finish hooks of an elevator, for a request that is not
5975*4882a593Smuzhiyun 	 * referred by that elevator.
5976*4882a593Smuzhiyun 	 *
5977*4882a593Smuzhiyun 	 * Resetting the following fields would break the
5978*4882a593Smuzhiyun 	 * request-insertion logic if rq is re-inserted into a bfq
5979*4882a593Smuzhiyun 	 * internal queue, without a re-preparation. Here we assume
5980*4882a593Smuzhiyun 	 * that re-insertions of requeued requests, without
5981*4882a593Smuzhiyun 	 * re-preparation, can happen only for pass_through or at_head
5982*4882a593Smuzhiyun 	 * requests (which are not re-inserted into bfq internal
5983*4882a593Smuzhiyun 	 * queues).
5984*4882a593Smuzhiyun 	 */
5985*4882a593Smuzhiyun 	rq->elv.priv[0] = NULL;
5986*4882a593Smuzhiyun 	rq->elv.priv[1] = NULL;
5987*4882a593Smuzhiyun }
5988*4882a593Smuzhiyun 
5989*4882a593Smuzhiyun /*
5990*4882a593Smuzhiyun  * Removes the association between the current task and bfqq, assuming
5991*4882a593Smuzhiyun  * that bic points to the bfq iocontext of the task.
5992*4882a593Smuzhiyun  * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
5993*4882a593Smuzhiyun  * was the last process referring to that bfqq.
5994*4882a593Smuzhiyun  */
5995*4882a593Smuzhiyun static struct bfq_queue *
bfq_split_bfqq(struct bfq_io_cq * bic,struct bfq_queue * bfqq)5996*4882a593Smuzhiyun bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
5997*4882a593Smuzhiyun {
5998*4882a593Smuzhiyun 	bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
5999*4882a593Smuzhiyun 
6000*4882a593Smuzhiyun 	if (bfqq_process_refs(bfqq) == 1) {
6001*4882a593Smuzhiyun 		bfqq->pid = current->pid;
6002*4882a593Smuzhiyun 		bfq_clear_bfqq_coop(bfqq);
6003*4882a593Smuzhiyun 		bfq_clear_bfqq_split_coop(bfqq);
6004*4882a593Smuzhiyun 		return bfqq;
6005*4882a593Smuzhiyun 	}
6006*4882a593Smuzhiyun 
6007*4882a593Smuzhiyun 	bic_set_bfqq(bic, NULL, 1);
6008*4882a593Smuzhiyun 
6009*4882a593Smuzhiyun 	bfq_put_cooperator(bfqq);
6010*4882a593Smuzhiyun 
6011*4882a593Smuzhiyun 	bfq_release_process_ref(bfqq->bfqd, bfqq);
6012*4882a593Smuzhiyun 	return NULL;
6013*4882a593Smuzhiyun }
6014*4882a593Smuzhiyun 
bfq_get_bfqq_handle_split(struct bfq_data * bfqd,struct bfq_io_cq * bic,struct bio * bio,bool split,bool is_sync,bool * new_queue)6015*4882a593Smuzhiyun static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,
6016*4882a593Smuzhiyun 						   struct bfq_io_cq *bic,
6017*4882a593Smuzhiyun 						   struct bio *bio,
6018*4882a593Smuzhiyun 						   bool split, bool is_sync,
6019*4882a593Smuzhiyun 						   bool *new_queue)
6020*4882a593Smuzhiyun {
6021*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
6022*4882a593Smuzhiyun 
6023*4882a593Smuzhiyun 	if (likely(bfqq && bfqq != &bfqd->oom_bfqq))
6024*4882a593Smuzhiyun 		return bfqq;
6025*4882a593Smuzhiyun 
6026*4882a593Smuzhiyun 	if (new_queue)
6027*4882a593Smuzhiyun 		*new_queue = true;
6028*4882a593Smuzhiyun 
6029*4882a593Smuzhiyun 	if (bfqq)
6030*4882a593Smuzhiyun 		bfq_put_queue(bfqq);
6031*4882a593Smuzhiyun 	bfqq = bfq_get_queue(bfqd, bio, is_sync, bic);
6032*4882a593Smuzhiyun 
6033*4882a593Smuzhiyun 	bic_set_bfqq(bic, bfqq, is_sync);
6034*4882a593Smuzhiyun 	if (split && is_sync) {
6035*4882a593Smuzhiyun 		if ((bic->was_in_burst_list && bfqd->large_burst) ||
6036*4882a593Smuzhiyun 		    bic->saved_in_large_burst)
6037*4882a593Smuzhiyun 			bfq_mark_bfqq_in_large_burst(bfqq);
6038*4882a593Smuzhiyun 		else {
6039*4882a593Smuzhiyun 			bfq_clear_bfqq_in_large_burst(bfqq);
6040*4882a593Smuzhiyun 			if (bic->was_in_burst_list)
6041*4882a593Smuzhiyun 				/*
6042*4882a593Smuzhiyun 				 * If bfqq was in the current
6043*4882a593Smuzhiyun 				 * burst list before being
6044*4882a593Smuzhiyun 				 * merged, then we have to add
6045*4882a593Smuzhiyun 				 * it back. And we do not need
6046*4882a593Smuzhiyun 				 * to increase burst_size, as
6047*4882a593Smuzhiyun 				 * we did not decrement
6048*4882a593Smuzhiyun 				 * burst_size when we removed
6049*4882a593Smuzhiyun 				 * bfqq from the burst list as
6050*4882a593Smuzhiyun 				 * a consequence of a merge
6051*4882a593Smuzhiyun 				 * (see comments in
6052*4882a593Smuzhiyun 				 * bfq_put_queue). In this
6053*4882a593Smuzhiyun 				 * respect, it would be rather
6054*4882a593Smuzhiyun 				 * costly to know whether the
6055*4882a593Smuzhiyun 				 * current burst list is still
6056*4882a593Smuzhiyun 				 * the same burst list from
6057*4882a593Smuzhiyun 				 * which bfqq was removed on
6058*4882a593Smuzhiyun 				 * the merge. To avoid this
6059*4882a593Smuzhiyun 				 * cost, if bfqq was in a
6060*4882a593Smuzhiyun 				 * burst list, then we add
6061*4882a593Smuzhiyun 				 * bfqq to the current burst
6062*4882a593Smuzhiyun 				 * list without any further
6063*4882a593Smuzhiyun 				 * check. This can cause
6064*4882a593Smuzhiyun 				 * inappropriate insertions,
6065*4882a593Smuzhiyun 				 * but rarely enough to not
6066*4882a593Smuzhiyun 				 * harm the detection of large
6067*4882a593Smuzhiyun 				 * bursts significantly.
6068*4882a593Smuzhiyun 				 */
6069*4882a593Smuzhiyun 				hlist_add_head(&bfqq->burst_list_node,
6070*4882a593Smuzhiyun 					       &bfqd->burst_list);
6071*4882a593Smuzhiyun 		}
6072*4882a593Smuzhiyun 		bfqq->split_time = jiffies;
6073*4882a593Smuzhiyun 	}
6074*4882a593Smuzhiyun 
6075*4882a593Smuzhiyun 	return bfqq;
6076*4882a593Smuzhiyun }
6077*4882a593Smuzhiyun 
6078*4882a593Smuzhiyun /*
6079*4882a593Smuzhiyun  * Only reset private fields. The actual request preparation will be
6080*4882a593Smuzhiyun  * performed by bfq_init_rq, when rq is either inserted or merged. See
6081*4882a593Smuzhiyun  * comments on bfq_init_rq for the reason behind this delayed
6082*4882a593Smuzhiyun  * preparation.
6083*4882a593Smuzhiyun  */
bfq_prepare_request(struct request * rq)6084*4882a593Smuzhiyun static void bfq_prepare_request(struct request *rq)
6085*4882a593Smuzhiyun {
6086*4882a593Smuzhiyun 	/*
6087*4882a593Smuzhiyun 	 * Regardless of whether we have an icq attached, we have to
6088*4882a593Smuzhiyun 	 * clear the scheduler pointers, as they might point to
6089*4882a593Smuzhiyun 	 * previously allocated bic/bfqq structs.
6090*4882a593Smuzhiyun 	 */
6091*4882a593Smuzhiyun 	rq->elv.priv[0] = rq->elv.priv[1] = NULL;
6092*4882a593Smuzhiyun }
6093*4882a593Smuzhiyun 
6094*4882a593Smuzhiyun /*
6095*4882a593Smuzhiyun  * If needed, init rq, allocate bfq data structures associated with
6096*4882a593Smuzhiyun  * rq, and increment reference counters in the destination bfq_queue
6097*4882a593Smuzhiyun  * for rq. Return the destination bfq_queue for rq, or NULL is rq is
6098*4882a593Smuzhiyun  * not associated with any bfq_queue.
6099*4882a593Smuzhiyun  *
6100*4882a593Smuzhiyun  * This function is invoked by the functions that perform rq insertion
6101*4882a593Smuzhiyun  * or merging. One may have expected the above preparation operations
6102*4882a593Smuzhiyun  * to be performed in bfq_prepare_request, and not delayed to when rq
6103*4882a593Smuzhiyun  * is inserted or merged. The rationale behind this delayed
6104*4882a593Smuzhiyun  * preparation is that, after the prepare_request hook is invoked for
6105*4882a593Smuzhiyun  * rq, rq may still be transformed into a request with no icq, i.e., a
6106*4882a593Smuzhiyun  * request not associated with any queue. No bfq hook is invoked to
6107*4882a593Smuzhiyun  * signal this transformation. As a consequence, should these
6108*4882a593Smuzhiyun  * preparation operations be performed when the prepare_request hook
6109*4882a593Smuzhiyun  * is invoked, and should rq be transformed one moment later, bfq
6110*4882a593Smuzhiyun  * would end up in an inconsistent state, because it would have
6111*4882a593Smuzhiyun  * incremented some queue counters for an rq destined to
6112*4882a593Smuzhiyun  * transformation, without any chance to correctly lower these
6113*4882a593Smuzhiyun  * counters back. In contrast, no transformation can still happen for
6114*4882a593Smuzhiyun  * rq after rq has been inserted or merged. So, it is safe to execute
6115*4882a593Smuzhiyun  * these preparation operations when rq is finally inserted or merged.
6116*4882a593Smuzhiyun  */
bfq_init_rq(struct request * rq)6117*4882a593Smuzhiyun static struct bfq_queue *bfq_init_rq(struct request *rq)
6118*4882a593Smuzhiyun {
6119*4882a593Smuzhiyun 	struct request_queue *q = rq->q;
6120*4882a593Smuzhiyun 	struct bio *bio = rq->bio;
6121*4882a593Smuzhiyun 	struct bfq_data *bfqd = q->elevator->elevator_data;
6122*4882a593Smuzhiyun 	struct bfq_io_cq *bic;
6123*4882a593Smuzhiyun 	const int is_sync = rq_is_sync(rq);
6124*4882a593Smuzhiyun 	struct bfq_queue *bfqq;
6125*4882a593Smuzhiyun 	bool new_queue = false;
6126*4882a593Smuzhiyun 	bool bfqq_already_existing = false, split = false;
6127*4882a593Smuzhiyun 
6128*4882a593Smuzhiyun 	if (unlikely(!rq->elv.icq))
6129*4882a593Smuzhiyun 		return NULL;
6130*4882a593Smuzhiyun 
6131*4882a593Smuzhiyun 	/*
6132*4882a593Smuzhiyun 	 * Assuming that elv.priv[1] is set only if everything is set
6133*4882a593Smuzhiyun 	 * for this rq. This holds true, because this function is
6134*4882a593Smuzhiyun 	 * invoked only for insertion or merging, and, after such
6135*4882a593Smuzhiyun 	 * events, a request cannot be manipulated any longer before
6136*4882a593Smuzhiyun 	 * being removed from bfq.
6137*4882a593Smuzhiyun 	 */
6138*4882a593Smuzhiyun 	if (rq->elv.priv[1])
6139*4882a593Smuzhiyun 		return rq->elv.priv[1];
6140*4882a593Smuzhiyun 
6141*4882a593Smuzhiyun 	bic = icq_to_bic(rq->elv.icq);
6142*4882a593Smuzhiyun 
6143*4882a593Smuzhiyun 	bfq_check_ioprio_change(bic, bio);
6144*4882a593Smuzhiyun 
6145*4882a593Smuzhiyun 	bfq_bic_update_cgroup(bic, bio);
6146*4882a593Smuzhiyun 
6147*4882a593Smuzhiyun 	bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync,
6148*4882a593Smuzhiyun 					 &new_queue);
6149*4882a593Smuzhiyun 
6150*4882a593Smuzhiyun 	if (likely(!new_queue)) {
6151*4882a593Smuzhiyun 		/* If the queue was seeky for too long, break it apart. */
6152*4882a593Smuzhiyun 		if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
6153*4882a593Smuzhiyun 			bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
6154*4882a593Smuzhiyun 
6155*4882a593Smuzhiyun 			/* Update bic before losing reference to bfqq */
6156*4882a593Smuzhiyun 			if (bfq_bfqq_in_large_burst(bfqq))
6157*4882a593Smuzhiyun 				bic->saved_in_large_burst = true;
6158*4882a593Smuzhiyun 
6159*4882a593Smuzhiyun 			bfqq = bfq_split_bfqq(bic, bfqq);
6160*4882a593Smuzhiyun 			split = true;
6161*4882a593Smuzhiyun 
6162*4882a593Smuzhiyun 			if (!bfqq)
6163*4882a593Smuzhiyun 				bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio,
6164*4882a593Smuzhiyun 								 true, is_sync,
6165*4882a593Smuzhiyun 								 NULL);
6166*4882a593Smuzhiyun 			else
6167*4882a593Smuzhiyun 				bfqq_already_existing = true;
6168*4882a593Smuzhiyun 		}
6169*4882a593Smuzhiyun 	}
6170*4882a593Smuzhiyun 
6171*4882a593Smuzhiyun 	bfqq->allocated++;
6172*4882a593Smuzhiyun 	bfqq->ref++;
6173*4882a593Smuzhiyun 	bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d",
6174*4882a593Smuzhiyun 		     rq, bfqq, bfqq->ref);
6175*4882a593Smuzhiyun 
6176*4882a593Smuzhiyun 	rq->elv.priv[0] = bic;
6177*4882a593Smuzhiyun 	rq->elv.priv[1] = bfqq;
6178*4882a593Smuzhiyun 
6179*4882a593Smuzhiyun 	/*
6180*4882a593Smuzhiyun 	 * If a bfq_queue has only one process reference, it is owned
6181*4882a593Smuzhiyun 	 * by only this bic: we can then set bfqq->bic = bic. in
6182*4882a593Smuzhiyun 	 * addition, if the queue has also just been split, we have to
6183*4882a593Smuzhiyun 	 * resume its state.
6184*4882a593Smuzhiyun 	 */
6185*4882a593Smuzhiyun 	if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
6186*4882a593Smuzhiyun 		bfqq->bic = bic;
6187*4882a593Smuzhiyun 		if (split) {
6188*4882a593Smuzhiyun 			/*
6189*4882a593Smuzhiyun 			 * The queue has just been split from a shared
6190*4882a593Smuzhiyun 			 * queue: restore the idle window and the
6191*4882a593Smuzhiyun 			 * possible weight raising period.
6192*4882a593Smuzhiyun 			 */
6193*4882a593Smuzhiyun 			bfq_bfqq_resume_state(bfqq, bfqd, bic,
6194*4882a593Smuzhiyun 					      bfqq_already_existing);
6195*4882a593Smuzhiyun 		}
6196*4882a593Smuzhiyun 	}
6197*4882a593Smuzhiyun 
6198*4882a593Smuzhiyun 	/*
6199*4882a593Smuzhiyun 	 * Consider bfqq as possibly belonging to a burst of newly
6200*4882a593Smuzhiyun 	 * created queues only if:
6201*4882a593Smuzhiyun 	 * 1) A burst is actually happening (bfqd->burst_size > 0)
6202*4882a593Smuzhiyun 	 * or
6203*4882a593Smuzhiyun 	 * 2) There is no other active queue. In fact, if, in
6204*4882a593Smuzhiyun 	 *    contrast, there are active queues not belonging to the
6205*4882a593Smuzhiyun 	 *    possible burst bfqq may belong to, then there is no gain
6206*4882a593Smuzhiyun 	 *    in considering bfqq as belonging to a burst, and
6207*4882a593Smuzhiyun 	 *    therefore in not weight-raising bfqq. See comments on
6208*4882a593Smuzhiyun 	 *    bfq_handle_burst().
6209*4882a593Smuzhiyun 	 *
6210*4882a593Smuzhiyun 	 * This filtering also helps eliminating false positives,
6211*4882a593Smuzhiyun 	 * occurring when bfqq does not belong to an actual large
6212*4882a593Smuzhiyun 	 * burst, but some background task (e.g., a service) happens
6213*4882a593Smuzhiyun 	 * to trigger the creation of new queues very close to when
6214*4882a593Smuzhiyun 	 * bfqq and its possible companion queues are created. See
6215*4882a593Smuzhiyun 	 * comments on bfq_handle_burst() for further details also on
6216*4882a593Smuzhiyun 	 * this issue.
6217*4882a593Smuzhiyun 	 */
6218*4882a593Smuzhiyun 	if (unlikely(bfq_bfqq_just_created(bfqq) &&
6219*4882a593Smuzhiyun 		     (bfqd->burst_size > 0 ||
6220*4882a593Smuzhiyun 		      bfq_tot_busy_queues(bfqd) == 0)))
6221*4882a593Smuzhiyun 		bfq_handle_burst(bfqd, bfqq);
6222*4882a593Smuzhiyun 
6223*4882a593Smuzhiyun 	return bfqq;
6224*4882a593Smuzhiyun }
6225*4882a593Smuzhiyun 
6226*4882a593Smuzhiyun static void
bfq_idle_slice_timer_body(struct bfq_data * bfqd,struct bfq_queue * bfqq)6227*4882a593Smuzhiyun bfq_idle_slice_timer_body(struct bfq_data *bfqd, struct bfq_queue *bfqq)
6228*4882a593Smuzhiyun {
6229*4882a593Smuzhiyun 	enum bfqq_expiration reason;
6230*4882a593Smuzhiyun 	unsigned long flags;
6231*4882a593Smuzhiyun 
6232*4882a593Smuzhiyun 	spin_lock_irqsave(&bfqd->lock, flags);
6233*4882a593Smuzhiyun 
6234*4882a593Smuzhiyun 	/*
6235*4882a593Smuzhiyun 	 * Considering that bfqq may be in race, we should firstly check
6236*4882a593Smuzhiyun 	 * whether bfqq is in service before doing something on it. If
6237*4882a593Smuzhiyun 	 * the bfqq in race is not in service, it has already been expired
6238*4882a593Smuzhiyun 	 * through __bfq_bfqq_expire func and its wait_request flags has
6239*4882a593Smuzhiyun 	 * been cleared in __bfq_bfqd_reset_in_service func.
6240*4882a593Smuzhiyun 	 */
6241*4882a593Smuzhiyun 	if (bfqq != bfqd->in_service_queue) {
6242*4882a593Smuzhiyun 		spin_unlock_irqrestore(&bfqd->lock, flags);
6243*4882a593Smuzhiyun 		return;
6244*4882a593Smuzhiyun 	}
6245*4882a593Smuzhiyun 
6246*4882a593Smuzhiyun 	bfq_clear_bfqq_wait_request(bfqq);
6247*4882a593Smuzhiyun 
6248*4882a593Smuzhiyun 	if (bfq_bfqq_budget_timeout(bfqq))
6249*4882a593Smuzhiyun 		/*
6250*4882a593Smuzhiyun 		 * Also here the queue can be safely expired
6251*4882a593Smuzhiyun 		 * for budget timeout without wasting
6252*4882a593Smuzhiyun 		 * guarantees
6253*4882a593Smuzhiyun 		 */
6254*4882a593Smuzhiyun 		reason = BFQQE_BUDGET_TIMEOUT;
6255*4882a593Smuzhiyun 	else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
6256*4882a593Smuzhiyun 		/*
6257*4882a593Smuzhiyun 		 * The queue may not be empty upon timer expiration,
6258*4882a593Smuzhiyun 		 * because we may not disable the timer when the
6259*4882a593Smuzhiyun 		 * first request of the in-service queue arrives
6260*4882a593Smuzhiyun 		 * during disk idling.
6261*4882a593Smuzhiyun 		 */
6262*4882a593Smuzhiyun 		reason = BFQQE_TOO_IDLE;
6263*4882a593Smuzhiyun 	else
6264*4882a593Smuzhiyun 		goto schedule_dispatch;
6265*4882a593Smuzhiyun 
6266*4882a593Smuzhiyun 	bfq_bfqq_expire(bfqd, bfqq, true, reason);
6267*4882a593Smuzhiyun 
6268*4882a593Smuzhiyun schedule_dispatch:
6269*4882a593Smuzhiyun 	bfq_schedule_dispatch(bfqd);
6270*4882a593Smuzhiyun 	spin_unlock_irqrestore(&bfqd->lock, flags);
6271*4882a593Smuzhiyun }
6272*4882a593Smuzhiyun 
6273*4882a593Smuzhiyun /*
6274*4882a593Smuzhiyun  * Handler of the expiration of the timer running if the in-service queue
6275*4882a593Smuzhiyun  * is idling inside its time slice.
6276*4882a593Smuzhiyun  */
bfq_idle_slice_timer(struct hrtimer * timer)6277*4882a593Smuzhiyun static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)
6278*4882a593Smuzhiyun {
6279*4882a593Smuzhiyun 	struct bfq_data *bfqd = container_of(timer, struct bfq_data,
6280*4882a593Smuzhiyun 					     idle_slice_timer);
6281*4882a593Smuzhiyun 	struct bfq_queue *bfqq = bfqd->in_service_queue;
6282*4882a593Smuzhiyun 
6283*4882a593Smuzhiyun 	/*
6284*4882a593Smuzhiyun 	 * Theoretical race here: the in-service queue can be NULL or
6285*4882a593Smuzhiyun 	 * different from the queue that was idling if a new request
6286*4882a593Smuzhiyun 	 * arrives for the current queue and there is a full dispatch
6287*4882a593Smuzhiyun 	 * cycle that changes the in-service queue.  This can hardly
6288*4882a593Smuzhiyun 	 * happen, but in the worst case we just expire a queue too
6289*4882a593Smuzhiyun 	 * early.
6290*4882a593Smuzhiyun 	 */
6291*4882a593Smuzhiyun 	if (bfqq)
6292*4882a593Smuzhiyun 		bfq_idle_slice_timer_body(bfqd, bfqq);
6293*4882a593Smuzhiyun 
6294*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
6295*4882a593Smuzhiyun }
6296*4882a593Smuzhiyun 
__bfq_put_async_bfqq(struct bfq_data * bfqd,struct bfq_queue ** bfqq_ptr)6297*4882a593Smuzhiyun static void __bfq_put_async_bfqq(struct bfq_data *bfqd,
6298*4882a593Smuzhiyun 				 struct bfq_queue **bfqq_ptr)
6299*4882a593Smuzhiyun {
6300*4882a593Smuzhiyun 	struct bfq_queue *bfqq = *bfqq_ptr;
6301*4882a593Smuzhiyun 
6302*4882a593Smuzhiyun 	bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
6303*4882a593Smuzhiyun 	if (bfqq) {
6304*4882a593Smuzhiyun 		bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
6305*4882a593Smuzhiyun 
6306*4882a593Smuzhiyun 		bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
6307*4882a593Smuzhiyun 			     bfqq, bfqq->ref);
6308*4882a593Smuzhiyun 		bfq_put_queue(bfqq);
6309*4882a593Smuzhiyun 		*bfqq_ptr = NULL;
6310*4882a593Smuzhiyun 	}
6311*4882a593Smuzhiyun }
6312*4882a593Smuzhiyun 
6313*4882a593Smuzhiyun /*
6314*4882a593Smuzhiyun  * Release all the bfqg references to its async queues.  If we are
6315*4882a593Smuzhiyun  * deallocating the group these queues may still contain requests, so
6316*4882a593Smuzhiyun  * we reparent them to the root cgroup (i.e., the only one that will
6317*4882a593Smuzhiyun  * exist for sure until all the requests on a device are gone).
6318*4882a593Smuzhiyun  */
bfq_put_async_queues(struct bfq_data * bfqd,struct bfq_group * bfqg)6319*4882a593Smuzhiyun void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
6320*4882a593Smuzhiyun {
6321*4882a593Smuzhiyun 	int i, j;
6322*4882a593Smuzhiyun 
6323*4882a593Smuzhiyun 	for (i = 0; i < 2; i++)
6324*4882a593Smuzhiyun 		for (j = 0; j < IOPRIO_BE_NR; j++)
6325*4882a593Smuzhiyun 			__bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
6326*4882a593Smuzhiyun 
6327*4882a593Smuzhiyun 	__bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
6328*4882a593Smuzhiyun }
6329*4882a593Smuzhiyun 
6330*4882a593Smuzhiyun /*
6331*4882a593Smuzhiyun  * See the comments on bfq_limit_depth for the purpose of
6332*4882a593Smuzhiyun  * the depths set in the function. Return minimum shallow depth we'll use.
6333*4882a593Smuzhiyun  */
bfq_update_depths(struct bfq_data * bfqd,struct sbitmap_queue * bt)6334*4882a593Smuzhiyun static unsigned int bfq_update_depths(struct bfq_data *bfqd,
6335*4882a593Smuzhiyun 				      struct sbitmap_queue *bt)
6336*4882a593Smuzhiyun {
6337*4882a593Smuzhiyun 	unsigned int i, j, min_shallow = UINT_MAX;
6338*4882a593Smuzhiyun 
6339*4882a593Smuzhiyun 	/*
6340*4882a593Smuzhiyun 	 * In-word depths if no bfq_queue is being weight-raised:
6341*4882a593Smuzhiyun 	 * leaving 25% of tags only for sync reads.
6342*4882a593Smuzhiyun 	 *
6343*4882a593Smuzhiyun 	 * In next formulas, right-shift the value
6344*4882a593Smuzhiyun 	 * (1U<<bt->sb.shift), instead of computing directly
6345*4882a593Smuzhiyun 	 * (1U<<(bt->sb.shift - something)), to be robust against
6346*4882a593Smuzhiyun 	 * any possible value of bt->sb.shift, without having to
6347*4882a593Smuzhiyun 	 * limit 'something'.
6348*4882a593Smuzhiyun 	 */
6349*4882a593Smuzhiyun 	/* no more than 50% of tags for async I/O */
6350*4882a593Smuzhiyun 	bfqd->word_depths[0][0] = max((1U << bt->sb.shift) >> 1, 1U);
6351*4882a593Smuzhiyun 	/*
6352*4882a593Smuzhiyun 	 * no more than 75% of tags for sync writes (25% extra tags
6353*4882a593Smuzhiyun 	 * w.r.t. async I/O, to prevent async I/O from starving sync
6354*4882a593Smuzhiyun 	 * writes)
6355*4882a593Smuzhiyun 	 */
6356*4882a593Smuzhiyun 	bfqd->word_depths[0][1] = max(((1U << bt->sb.shift) * 3) >> 2, 1U);
6357*4882a593Smuzhiyun 
6358*4882a593Smuzhiyun 	/*
6359*4882a593Smuzhiyun 	 * In-word depths in case some bfq_queue is being weight-
6360*4882a593Smuzhiyun 	 * raised: leaving ~63% of tags for sync reads. This is the
6361*4882a593Smuzhiyun 	 * highest percentage for which, in our tests, application
6362*4882a593Smuzhiyun 	 * start-up times didn't suffer from any regression due to tag
6363*4882a593Smuzhiyun 	 * shortage.
6364*4882a593Smuzhiyun 	 */
6365*4882a593Smuzhiyun 	/* no more than ~18% of tags for async I/O */
6366*4882a593Smuzhiyun 	bfqd->word_depths[1][0] = max(((1U << bt->sb.shift) * 3) >> 4, 1U);
6367*4882a593Smuzhiyun 	/* no more than ~37% of tags for sync writes (~20% extra tags) */
6368*4882a593Smuzhiyun 	bfqd->word_depths[1][1] = max(((1U << bt->sb.shift) * 6) >> 4, 1U);
6369*4882a593Smuzhiyun 
6370*4882a593Smuzhiyun 	for (i = 0; i < 2; i++)
6371*4882a593Smuzhiyun 		for (j = 0; j < 2; j++)
6372*4882a593Smuzhiyun 			min_shallow = min(min_shallow, bfqd->word_depths[i][j]);
6373*4882a593Smuzhiyun 
6374*4882a593Smuzhiyun 	return min_shallow;
6375*4882a593Smuzhiyun }
6376*4882a593Smuzhiyun 
bfq_depth_updated(struct blk_mq_hw_ctx * hctx)6377*4882a593Smuzhiyun static void bfq_depth_updated(struct blk_mq_hw_ctx *hctx)
6378*4882a593Smuzhiyun {
6379*4882a593Smuzhiyun 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
6380*4882a593Smuzhiyun 	struct blk_mq_tags *tags = hctx->sched_tags;
6381*4882a593Smuzhiyun 	unsigned int min_shallow;
6382*4882a593Smuzhiyun 
6383*4882a593Smuzhiyun 	min_shallow = bfq_update_depths(bfqd, tags->bitmap_tags);
6384*4882a593Smuzhiyun 	sbitmap_queue_min_shallow_depth(tags->bitmap_tags, min_shallow);
6385*4882a593Smuzhiyun }
6386*4882a593Smuzhiyun 
bfq_init_hctx(struct blk_mq_hw_ctx * hctx,unsigned int index)6387*4882a593Smuzhiyun static int bfq_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int index)
6388*4882a593Smuzhiyun {
6389*4882a593Smuzhiyun 	bfq_depth_updated(hctx);
6390*4882a593Smuzhiyun 	return 0;
6391*4882a593Smuzhiyun }
6392*4882a593Smuzhiyun 
bfq_exit_queue(struct elevator_queue * e)6393*4882a593Smuzhiyun static void bfq_exit_queue(struct elevator_queue *e)
6394*4882a593Smuzhiyun {
6395*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;
6396*4882a593Smuzhiyun 	struct bfq_queue *bfqq, *n;
6397*4882a593Smuzhiyun 
6398*4882a593Smuzhiyun 	hrtimer_cancel(&bfqd->idle_slice_timer);
6399*4882a593Smuzhiyun 
6400*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
6401*4882a593Smuzhiyun 	list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
6402*4882a593Smuzhiyun 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
6403*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
6404*4882a593Smuzhiyun 
6405*4882a593Smuzhiyun 	hrtimer_cancel(&bfqd->idle_slice_timer);
6406*4882a593Smuzhiyun 
6407*4882a593Smuzhiyun 	/* release oom-queue reference to root group */
6408*4882a593Smuzhiyun 	bfqg_and_blkg_put(bfqd->root_group);
6409*4882a593Smuzhiyun 
6410*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
6411*4882a593Smuzhiyun 	blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
6412*4882a593Smuzhiyun #else
6413*4882a593Smuzhiyun 	spin_lock_irq(&bfqd->lock);
6414*4882a593Smuzhiyun 	bfq_put_async_queues(bfqd, bfqd->root_group);
6415*4882a593Smuzhiyun 	kfree(bfqd->root_group);
6416*4882a593Smuzhiyun 	spin_unlock_irq(&bfqd->lock);
6417*4882a593Smuzhiyun #endif
6418*4882a593Smuzhiyun 
6419*4882a593Smuzhiyun 	wbt_enable_default(bfqd->queue);
6420*4882a593Smuzhiyun 
6421*4882a593Smuzhiyun 	kfree(bfqd);
6422*4882a593Smuzhiyun }
6423*4882a593Smuzhiyun 
bfq_init_root_group(struct bfq_group * root_group,struct bfq_data * bfqd)6424*4882a593Smuzhiyun static void bfq_init_root_group(struct bfq_group *root_group,
6425*4882a593Smuzhiyun 				struct bfq_data *bfqd)
6426*4882a593Smuzhiyun {
6427*4882a593Smuzhiyun 	int i;
6428*4882a593Smuzhiyun 
6429*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
6430*4882a593Smuzhiyun 	root_group->entity.parent = NULL;
6431*4882a593Smuzhiyun 	root_group->my_entity = NULL;
6432*4882a593Smuzhiyun 	root_group->bfqd = bfqd;
6433*4882a593Smuzhiyun #endif
6434*4882a593Smuzhiyun 	root_group->rq_pos_tree = RB_ROOT;
6435*4882a593Smuzhiyun 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
6436*4882a593Smuzhiyun 		root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
6437*4882a593Smuzhiyun 	root_group->sched_data.bfq_class_idle_last_service = jiffies;
6438*4882a593Smuzhiyun }
6439*4882a593Smuzhiyun 
bfq_init_queue(struct request_queue * q,struct elevator_type * e)6440*4882a593Smuzhiyun static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
6441*4882a593Smuzhiyun {
6442*4882a593Smuzhiyun 	struct bfq_data *bfqd;
6443*4882a593Smuzhiyun 	struct elevator_queue *eq;
6444*4882a593Smuzhiyun 
6445*4882a593Smuzhiyun 	eq = elevator_alloc(q, e);
6446*4882a593Smuzhiyun 	if (!eq)
6447*4882a593Smuzhiyun 		return -ENOMEM;
6448*4882a593Smuzhiyun 
6449*4882a593Smuzhiyun 	bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
6450*4882a593Smuzhiyun 	if (!bfqd) {
6451*4882a593Smuzhiyun 		kobject_put(&eq->kobj);
6452*4882a593Smuzhiyun 		return -ENOMEM;
6453*4882a593Smuzhiyun 	}
6454*4882a593Smuzhiyun 	eq->elevator_data = bfqd;
6455*4882a593Smuzhiyun 
6456*4882a593Smuzhiyun 	spin_lock_irq(&q->queue_lock);
6457*4882a593Smuzhiyun 	q->elevator = eq;
6458*4882a593Smuzhiyun 	spin_unlock_irq(&q->queue_lock);
6459*4882a593Smuzhiyun 
6460*4882a593Smuzhiyun 	/*
6461*4882a593Smuzhiyun 	 * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
6462*4882a593Smuzhiyun 	 * Grab a permanent reference to it, so that the normal code flow
6463*4882a593Smuzhiyun 	 * will not attempt to free it.
6464*4882a593Smuzhiyun 	 */
6465*4882a593Smuzhiyun 	bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
6466*4882a593Smuzhiyun 	bfqd->oom_bfqq.ref++;
6467*4882a593Smuzhiyun 	bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
6468*4882a593Smuzhiyun 	bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE;
6469*4882a593Smuzhiyun 	bfqd->oom_bfqq.entity.new_weight =
6470*4882a593Smuzhiyun 		bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio);
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun 	/* oom_bfqq does not participate to bursts */
6473*4882a593Smuzhiyun 	bfq_clear_bfqq_just_created(&bfqd->oom_bfqq);
6474*4882a593Smuzhiyun 
6475*4882a593Smuzhiyun 	/*
6476*4882a593Smuzhiyun 	 * Trigger weight initialization, according to ioprio, at the
6477*4882a593Smuzhiyun 	 * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
6478*4882a593Smuzhiyun 	 * class won't be changed any more.
6479*4882a593Smuzhiyun 	 */
6480*4882a593Smuzhiyun 	bfqd->oom_bfqq.entity.prio_changed = 1;
6481*4882a593Smuzhiyun 
6482*4882a593Smuzhiyun 	bfqd->queue = q;
6483*4882a593Smuzhiyun 
6484*4882a593Smuzhiyun 	INIT_LIST_HEAD(&bfqd->dispatch);
6485*4882a593Smuzhiyun 
6486*4882a593Smuzhiyun 	hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC,
6487*4882a593Smuzhiyun 		     HRTIMER_MODE_REL);
6488*4882a593Smuzhiyun 	bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
6489*4882a593Smuzhiyun 
6490*4882a593Smuzhiyun 	bfqd->queue_weights_tree = RB_ROOT_CACHED;
6491*4882a593Smuzhiyun 	bfqd->num_groups_with_pending_reqs = 0;
6492*4882a593Smuzhiyun 
6493*4882a593Smuzhiyun 	INIT_LIST_HEAD(&bfqd->active_list);
6494*4882a593Smuzhiyun 	INIT_LIST_HEAD(&bfqd->idle_list);
6495*4882a593Smuzhiyun 	INIT_HLIST_HEAD(&bfqd->burst_list);
6496*4882a593Smuzhiyun 
6497*4882a593Smuzhiyun 	bfqd->hw_tag = -1;
6498*4882a593Smuzhiyun 	bfqd->nonrot_with_queueing = blk_queue_nonrot(bfqd->queue);
6499*4882a593Smuzhiyun 
6500*4882a593Smuzhiyun 	bfqd->bfq_max_budget = bfq_default_max_budget;
6501*4882a593Smuzhiyun 
6502*4882a593Smuzhiyun 	bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
6503*4882a593Smuzhiyun 	bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
6504*4882a593Smuzhiyun 	bfqd->bfq_back_max = bfq_back_max;
6505*4882a593Smuzhiyun 	bfqd->bfq_back_penalty = bfq_back_penalty;
6506*4882a593Smuzhiyun 	bfqd->bfq_slice_idle = bfq_slice_idle;
6507*4882a593Smuzhiyun 	bfqd->bfq_timeout = bfq_timeout;
6508*4882a593Smuzhiyun 
6509*4882a593Smuzhiyun 	bfqd->bfq_requests_within_timer = 120;
6510*4882a593Smuzhiyun 
6511*4882a593Smuzhiyun 	bfqd->bfq_large_burst_thresh = 8;
6512*4882a593Smuzhiyun 	bfqd->bfq_burst_interval = msecs_to_jiffies(180);
6513*4882a593Smuzhiyun 
6514*4882a593Smuzhiyun 	bfqd->low_latency = true;
6515*4882a593Smuzhiyun 
6516*4882a593Smuzhiyun 	/*
6517*4882a593Smuzhiyun 	 * Trade-off between responsiveness and fairness.
6518*4882a593Smuzhiyun 	 */
6519*4882a593Smuzhiyun 	bfqd->bfq_wr_coeff = 30;
6520*4882a593Smuzhiyun 	bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
6521*4882a593Smuzhiyun 	bfqd->bfq_wr_max_time = 0;
6522*4882a593Smuzhiyun 	bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
6523*4882a593Smuzhiyun 	bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
6524*4882a593Smuzhiyun 	bfqd->bfq_wr_max_softrt_rate = 7000; /*
6525*4882a593Smuzhiyun 					      * Approximate rate required
6526*4882a593Smuzhiyun 					      * to playback or record a
6527*4882a593Smuzhiyun 					      * high-definition compressed
6528*4882a593Smuzhiyun 					      * video.
6529*4882a593Smuzhiyun 					      */
6530*4882a593Smuzhiyun 	bfqd->wr_busy_queues = 0;
6531*4882a593Smuzhiyun 
6532*4882a593Smuzhiyun 	/*
6533*4882a593Smuzhiyun 	 * Begin by assuming, optimistically, that the device peak
6534*4882a593Smuzhiyun 	 * rate is equal to 2/3 of the highest reference rate.
6535*4882a593Smuzhiyun 	 */
6536*4882a593Smuzhiyun 	bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] *
6537*4882a593Smuzhiyun 		ref_wr_duration[blk_queue_nonrot(bfqd->queue)];
6538*4882a593Smuzhiyun 	bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3;
6539*4882a593Smuzhiyun 
6540*4882a593Smuzhiyun 	spin_lock_init(&bfqd->lock);
6541*4882a593Smuzhiyun 
6542*4882a593Smuzhiyun 	/*
6543*4882a593Smuzhiyun 	 * The invocation of the next bfq_create_group_hierarchy
6544*4882a593Smuzhiyun 	 * function is the head of a chain of function calls
6545*4882a593Smuzhiyun 	 * (bfq_create_group_hierarchy->blkcg_activate_policy->
6546*4882a593Smuzhiyun 	 * blk_mq_freeze_queue) that may lead to the invocation of the
6547*4882a593Smuzhiyun 	 * has_work hook function. For this reason,
6548*4882a593Smuzhiyun 	 * bfq_create_group_hierarchy is invoked only after all
6549*4882a593Smuzhiyun 	 * scheduler data has been initialized, apart from the fields
6550*4882a593Smuzhiyun 	 * that can be initialized only after invoking
6551*4882a593Smuzhiyun 	 * bfq_create_group_hierarchy. This, in particular, enables
6552*4882a593Smuzhiyun 	 * has_work to correctly return false. Of course, to avoid
6553*4882a593Smuzhiyun 	 * other inconsistencies, the blk-mq stack must then refrain
6554*4882a593Smuzhiyun 	 * from invoking further scheduler hooks before this init
6555*4882a593Smuzhiyun 	 * function is finished.
6556*4882a593Smuzhiyun 	 */
6557*4882a593Smuzhiyun 	bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node);
6558*4882a593Smuzhiyun 	if (!bfqd->root_group)
6559*4882a593Smuzhiyun 		goto out_free;
6560*4882a593Smuzhiyun 	bfq_init_root_group(bfqd->root_group, bfqd);
6561*4882a593Smuzhiyun 	bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
6562*4882a593Smuzhiyun 
6563*4882a593Smuzhiyun 	wbt_disable_default(q);
6564*4882a593Smuzhiyun 	return 0;
6565*4882a593Smuzhiyun 
6566*4882a593Smuzhiyun out_free:
6567*4882a593Smuzhiyun 	kfree(bfqd);
6568*4882a593Smuzhiyun 	kobject_put(&eq->kobj);
6569*4882a593Smuzhiyun 	return -ENOMEM;
6570*4882a593Smuzhiyun }
6571*4882a593Smuzhiyun 
bfq_slab_kill(void)6572*4882a593Smuzhiyun static void bfq_slab_kill(void)
6573*4882a593Smuzhiyun {
6574*4882a593Smuzhiyun 	kmem_cache_destroy(bfq_pool);
6575*4882a593Smuzhiyun }
6576*4882a593Smuzhiyun 
bfq_slab_setup(void)6577*4882a593Smuzhiyun static int __init bfq_slab_setup(void)
6578*4882a593Smuzhiyun {
6579*4882a593Smuzhiyun 	bfq_pool = KMEM_CACHE(bfq_queue, 0);
6580*4882a593Smuzhiyun 	if (!bfq_pool)
6581*4882a593Smuzhiyun 		return -ENOMEM;
6582*4882a593Smuzhiyun 	return 0;
6583*4882a593Smuzhiyun }
6584*4882a593Smuzhiyun 
bfq_var_show(unsigned int var,char * page)6585*4882a593Smuzhiyun static ssize_t bfq_var_show(unsigned int var, char *page)
6586*4882a593Smuzhiyun {
6587*4882a593Smuzhiyun 	return sprintf(page, "%u\n", var);
6588*4882a593Smuzhiyun }
6589*4882a593Smuzhiyun 
bfq_var_store(unsigned long * var,const char * page)6590*4882a593Smuzhiyun static int bfq_var_store(unsigned long *var, const char *page)
6591*4882a593Smuzhiyun {
6592*4882a593Smuzhiyun 	unsigned long new_val;
6593*4882a593Smuzhiyun 	int ret = kstrtoul(page, 10, &new_val);
6594*4882a593Smuzhiyun 
6595*4882a593Smuzhiyun 	if (ret)
6596*4882a593Smuzhiyun 		return ret;
6597*4882a593Smuzhiyun 	*var = new_val;
6598*4882a593Smuzhiyun 	return 0;
6599*4882a593Smuzhiyun }
6600*4882a593Smuzhiyun 
6601*4882a593Smuzhiyun #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)				\
6602*4882a593Smuzhiyun static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
6603*4882a593Smuzhiyun {									\
6604*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;			\
6605*4882a593Smuzhiyun 	u64 __data = __VAR;						\
6606*4882a593Smuzhiyun 	if (__CONV == 1)						\
6607*4882a593Smuzhiyun 		__data = jiffies_to_msecs(__data);			\
6608*4882a593Smuzhiyun 	else if (__CONV == 2)						\
6609*4882a593Smuzhiyun 		__data = div_u64(__data, NSEC_PER_MSEC);		\
6610*4882a593Smuzhiyun 	return bfq_var_show(__data, (page));				\
6611*4882a593Smuzhiyun }
6612*4882a593Smuzhiyun SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
6613*4882a593Smuzhiyun SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
6614*4882a593Smuzhiyun SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
6615*4882a593Smuzhiyun SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
6616*4882a593Smuzhiyun SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
6617*4882a593Smuzhiyun SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
6618*4882a593Smuzhiyun SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
6619*4882a593Smuzhiyun SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
6620*4882a593Smuzhiyun SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
6621*4882a593Smuzhiyun #undef SHOW_FUNCTION
6622*4882a593Smuzhiyun 
6623*4882a593Smuzhiyun #define USEC_SHOW_FUNCTION(__FUNC, __VAR)				\
6624*4882a593Smuzhiyun static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
6625*4882a593Smuzhiyun {									\
6626*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;			\
6627*4882a593Smuzhiyun 	u64 __data = __VAR;						\
6628*4882a593Smuzhiyun 	__data = div_u64(__data, NSEC_PER_USEC);			\
6629*4882a593Smuzhiyun 	return bfq_var_show(__data, (page));				\
6630*4882a593Smuzhiyun }
6631*4882a593Smuzhiyun USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
6632*4882a593Smuzhiyun #undef USEC_SHOW_FUNCTION
6633*4882a593Smuzhiyun 
6634*4882a593Smuzhiyun #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)			\
6635*4882a593Smuzhiyun static ssize_t								\
6636*4882a593Smuzhiyun __FUNC(struct elevator_queue *e, const char *page, size_t count)	\
6637*4882a593Smuzhiyun {									\
6638*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;			\
6639*4882a593Smuzhiyun 	unsigned long __data, __min = (MIN), __max = (MAX);		\
6640*4882a593Smuzhiyun 	int ret;							\
6641*4882a593Smuzhiyun 									\
6642*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));				\
6643*4882a593Smuzhiyun 	if (ret)							\
6644*4882a593Smuzhiyun 		return ret;						\
6645*4882a593Smuzhiyun 	if (__data < __min)						\
6646*4882a593Smuzhiyun 		__data = __min;						\
6647*4882a593Smuzhiyun 	else if (__data > __max)					\
6648*4882a593Smuzhiyun 		__data = __max;						\
6649*4882a593Smuzhiyun 	if (__CONV == 1)						\
6650*4882a593Smuzhiyun 		*(__PTR) = msecs_to_jiffies(__data);			\
6651*4882a593Smuzhiyun 	else if (__CONV == 2)						\
6652*4882a593Smuzhiyun 		*(__PTR) = (u64)__data * NSEC_PER_MSEC;			\
6653*4882a593Smuzhiyun 	else								\
6654*4882a593Smuzhiyun 		*(__PTR) = __data;					\
6655*4882a593Smuzhiyun 	return count;							\
6656*4882a593Smuzhiyun }
6657*4882a593Smuzhiyun STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
6658*4882a593Smuzhiyun 		INT_MAX, 2);
6659*4882a593Smuzhiyun STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
6660*4882a593Smuzhiyun 		INT_MAX, 2);
6661*4882a593Smuzhiyun STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
6662*4882a593Smuzhiyun STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
6663*4882a593Smuzhiyun 		INT_MAX, 0);
6664*4882a593Smuzhiyun STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
6665*4882a593Smuzhiyun #undef STORE_FUNCTION
6666*4882a593Smuzhiyun 
6667*4882a593Smuzhiyun #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX)			\
6668*4882a593Smuzhiyun static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\
6669*4882a593Smuzhiyun {									\
6670*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;			\
6671*4882a593Smuzhiyun 	unsigned long __data, __min = (MIN), __max = (MAX);		\
6672*4882a593Smuzhiyun 	int ret;							\
6673*4882a593Smuzhiyun 									\
6674*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));				\
6675*4882a593Smuzhiyun 	if (ret)							\
6676*4882a593Smuzhiyun 		return ret;						\
6677*4882a593Smuzhiyun 	if (__data < __min)						\
6678*4882a593Smuzhiyun 		__data = __min;						\
6679*4882a593Smuzhiyun 	else if (__data > __max)					\
6680*4882a593Smuzhiyun 		__data = __max;						\
6681*4882a593Smuzhiyun 	*(__PTR) = (u64)__data * NSEC_PER_USEC;				\
6682*4882a593Smuzhiyun 	return count;							\
6683*4882a593Smuzhiyun }
6684*4882a593Smuzhiyun USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
6685*4882a593Smuzhiyun 		    UINT_MAX);
6686*4882a593Smuzhiyun #undef USEC_STORE_FUNCTION
6687*4882a593Smuzhiyun 
bfq_max_budget_store(struct elevator_queue * e,const char * page,size_t count)6688*4882a593Smuzhiyun static ssize_t bfq_max_budget_store(struct elevator_queue *e,
6689*4882a593Smuzhiyun 				    const char *page, size_t count)
6690*4882a593Smuzhiyun {
6691*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;
6692*4882a593Smuzhiyun 	unsigned long __data;
6693*4882a593Smuzhiyun 	int ret;
6694*4882a593Smuzhiyun 
6695*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));
6696*4882a593Smuzhiyun 	if (ret)
6697*4882a593Smuzhiyun 		return ret;
6698*4882a593Smuzhiyun 
6699*4882a593Smuzhiyun 	if (__data == 0)
6700*4882a593Smuzhiyun 		bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
6701*4882a593Smuzhiyun 	else {
6702*4882a593Smuzhiyun 		if (__data > INT_MAX)
6703*4882a593Smuzhiyun 			__data = INT_MAX;
6704*4882a593Smuzhiyun 		bfqd->bfq_max_budget = __data;
6705*4882a593Smuzhiyun 	}
6706*4882a593Smuzhiyun 
6707*4882a593Smuzhiyun 	bfqd->bfq_user_max_budget = __data;
6708*4882a593Smuzhiyun 
6709*4882a593Smuzhiyun 	return count;
6710*4882a593Smuzhiyun }
6711*4882a593Smuzhiyun 
6712*4882a593Smuzhiyun /*
6713*4882a593Smuzhiyun  * Leaving this name to preserve name compatibility with cfq
6714*4882a593Smuzhiyun  * parameters, but this timeout is used for both sync and async.
6715*4882a593Smuzhiyun  */
bfq_timeout_sync_store(struct elevator_queue * e,const char * page,size_t count)6716*4882a593Smuzhiyun static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
6717*4882a593Smuzhiyun 				      const char *page, size_t count)
6718*4882a593Smuzhiyun {
6719*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;
6720*4882a593Smuzhiyun 	unsigned long __data;
6721*4882a593Smuzhiyun 	int ret;
6722*4882a593Smuzhiyun 
6723*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));
6724*4882a593Smuzhiyun 	if (ret)
6725*4882a593Smuzhiyun 		return ret;
6726*4882a593Smuzhiyun 
6727*4882a593Smuzhiyun 	if (__data < 1)
6728*4882a593Smuzhiyun 		__data = 1;
6729*4882a593Smuzhiyun 	else if (__data > INT_MAX)
6730*4882a593Smuzhiyun 		__data = INT_MAX;
6731*4882a593Smuzhiyun 
6732*4882a593Smuzhiyun 	bfqd->bfq_timeout = msecs_to_jiffies(__data);
6733*4882a593Smuzhiyun 	if (bfqd->bfq_user_max_budget == 0)
6734*4882a593Smuzhiyun 		bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
6735*4882a593Smuzhiyun 
6736*4882a593Smuzhiyun 	return count;
6737*4882a593Smuzhiyun }
6738*4882a593Smuzhiyun 
bfq_strict_guarantees_store(struct elevator_queue * e,const char * page,size_t count)6739*4882a593Smuzhiyun static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e,
6740*4882a593Smuzhiyun 				     const char *page, size_t count)
6741*4882a593Smuzhiyun {
6742*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;
6743*4882a593Smuzhiyun 	unsigned long __data;
6744*4882a593Smuzhiyun 	int ret;
6745*4882a593Smuzhiyun 
6746*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));
6747*4882a593Smuzhiyun 	if (ret)
6748*4882a593Smuzhiyun 		return ret;
6749*4882a593Smuzhiyun 
6750*4882a593Smuzhiyun 	if (__data > 1)
6751*4882a593Smuzhiyun 		__data = 1;
6752*4882a593Smuzhiyun 	if (!bfqd->strict_guarantees && __data == 1
6753*4882a593Smuzhiyun 	    && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC)
6754*4882a593Smuzhiyun 		bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC;
6755*4882a593Smuzhiyun 
6756*4882a593Smuzhiyun 	bfqd->strict_guarantees = __data;
6757*4882a593Smuzhiyun 
6758*4882a593Smuzhiyun 	return count;
6759*4882a593Smuzhiyun }
6760*4882a593Smuzhiyun 
bfq_low_latency_store(struct elevator_queue * e,const char * page,size_t count)6761*4882a593Smuzhiyun static ssize_t bfq_low_latency_store(struct elevator_queue *e,
6762*4882a593Smuzhiyun 				     const char *page, size_t count)
6763*4882a593Smuzhiyun {
6764*4882a593Smuzhiyun 	struct bfq_data *bfqd = e->elevator_data;
6765*4882a593Smuzhiyun 	unsigned long __data;
6766*4882a593Smuzhiyun 	int ret;
6767*4882a593Smuzhiyun 
6768*4882a593Smuzhiyun 	ret = bfq_var_store(&__data, (page));
6769*4882a593Smuzhiyun 	if (ret)
6770*4882a593Smuzhiyun 		return ret;
6771*4882a593Smuzhiyun 
6772*4882a593Smuzhiyun 	if (__data > 1)
6773*4882a593Smuzhiyun 		__data = 1;
6774*4882a593Smuzhiyun 	if (__data == 0 && bfqd->low_latency != 0)
6775*4882a593Smuzhiyun 		bfq_end_wr(bfqd);
6776*4882a593Smuzhiyun 	bfqd->low_latency = __data;
6777*4882a593Smuzhiyun 
6778*4882a593Smuzhiyun 	return count;
6779*4882a593Smuzhiyun }
6780*4882a593Smuzhiyun 
6781*4882a593Smuzhiyun #define BFQ_ATTR(name) \
6782*4882a593Smuzhiyun 	__ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store)
6783*4882a593Smuzhiyun 
6784*4882a593Smuzhiyun static struct elv_fs_entry bfq_attrs[] = {
6785*4882a593Smuzhiyun 	BFQ_ATTR(fifo_expire_sync),
6786*4882a593Smuzhiyun 	BFQ_ATTR(fifo_expire_async),
6787*4882a593Smuzhiyun 	BFQ_ATTR(back_seek_max),
6788*4882a593Smuzhiyun 	BFQ_ATTR(back_seek_penalty),
6789*4882a593Smuzhiyun 	BFQ_ATTR(slice_idle),
6790*4882a593Smuzhiyun 	BFQ_ATTR(slice_idle_us),
6791*4882a593Smuzhiyun 	BFQ_ATTR(max_budget),
6792*4882a593Smuzhiyun 	BFQ_ATTR(timeout_sync),
6793*4882a593Smuzhiyun 	BFQ_ATTR(strict_guarantees),
6794*4882a593Smuzhiyun 	BFQ_ATTR(low_latency),
6795*4882a593Smuzhiyun 	__ATTR_NULL
6796*4882a593Smuzhiyun };
6797*4882a593Smuzhiyun 
6798*4882a593Smuzhiyun static struct elevator_type iosched_bfq_mq = {
6799*4882a593Smuzhiyun 	.ops = {
6800*4882a593Smuzhiyun 		.limit_depth		= bfq_limit_depth,
6801*4882a593Smuzhiyun 		.prepare_request	= bfq_prepare_request,
6802*4882a593Smuzhiyun 		.requeue_request        = bfq_finish_requeue_request,
6803*4882a593Smuzhiyun 		.finish_request		= bfq_finish_requeue_request,
6804*4882a593Smuzhiyun 		.exit_icq		= bfq_exit_icq,
6805*4882a593Smuzhiyun 		.insert_requests	= bfq_insert_requests,
6806*4882a593Smuzhiyun 		.dispatch_request	= bfq_dispatch_request,
6807*4882a593Smuzhiyun 		.next_request		= elv_rb_latter_request,
6808*4882a593Smuzhiyun 		.former_request		= elv_rb_former_request,
6809*4882a593Smuzhiyun 		.allow_merge		= bfq_allow_bio_merge,
6810*4882a593Smuzhiyun 		.bio_merge		= bfq_bio_merge,
6811*4882a593Smuzhiyun 		.request_merge		= bfq_request_merge,
6812*4882a593Smuzhiyun 		.requests_merged	= bfq_requests_merged,
6813*4882a593Smuzhiyun 		.request_merged		= bfq_request_merged,
6814*4882a593Smuzhiyun 		.has_work		= bfq_has_work,
6815*4882a593Smuzhiyun 		.depth_updated		= bfq_depth_updated,
6816*4882a593Smuzhiyun 		.init_hctx		= bfq_init_hctx,
6817*4882a593Smuzhiyun 		.init_sched		= bfq_init_queue,
6818*4882a593Smuzhiyun 		.exit_sched		= bfq_exit_queue,
6819*4882a593Smuzhiyun 	},
6820*4882a593Smuzhiyun 
6821*4882a593Smuzhiyun 	.icq_size =		sizeof(struct bfq_io_cq),
6822*4882a593Smuzhiyun 	.icq_align =		__alignof__(struct bfq_io_cq),
6823*4882a593Smuzhiyun 	.elevator_attrs =	bfq_attrs,
6824*4882a593Smuzhiyun 	.elevator_name =	"bfq",
6825*4882a593Smuzhiyun 	.elevator_owner =	THIS_MODULE,
6826*4882a593Smuzhiyun };
6827*4882a593Smuzhiyun MODULE_ALIAS("bfq-iosched");
6828*4882a593Smuzhiyun 
bfq_init(void)6829*4882a593Smuzhiyun static int __init bfq_init(void)
6830*4882a593Smuzhiyun {
6831*4882a593Smuzhiyun 	int ret;
6832*4882a593Smuzhiyun 
6833*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
6834*4882a593Smuzhiyun 	ret = blkcg_policy_register(&blkcg_policy_bfq);
6835*4882a593Smuzhiyun 	if (ret)
6836*4882a593Smuzhiyun 		return ret;
6837*4882a593Smuzhiyun #endif
6838*4882a593Smuzhiyun 
6839*4882a593Smuzhiyun 	ret = -ENOMEM;
6840*4882a593Smuzhiyun 	if (bfq_slab_setup())
6841*4882a593Smuzhiyun 		goto err_pol_unreg;
6842*4882a593Smuzhiyun 
6843*4882a593Smuzhiyun 	/*
6844*4882a593Smuzhiyun 	 * Times to load large popular applications for the typical
6845*4882a593Smuzhiyun 	 * systems installed on the reference devices (see the
6846*4882a593Smuzhiyun 	 * comments before the definition of the next
6847*4882a593Smuzhiyun 	 * array). Actually, we use slightly lower values, as the
6848*4882a593Smuzhiyun 	 * estimated peak rate tends to be smaller than the actual
6849*4882a593Smuzhiyun 	 * peak rate.  The reason for this last fact is that estimates
6850*4882a593Smuzhiyun 	 * are computed over much shorter time intervals than the long
6851*4882a593Smuzhiyun 	 * intervals typically used for benchmarking. Why? First, to
6852*4882a593Smuzhiyun 	 * adapt more quickly to variations. Second, because an I/O
6853*4882a593Smuzhiyun 	 * scheduler cannot rely on a peak-rate-evaluation workload to
6854*4882a593Smuzhiyun 	 * be run for a long time.
6855*4882a593Smuzhiyun 	 */
6856*4882a593Smuzhiyun 	ref_wr_duration[0] = msecs_to_jiffies(7000); /* actually 8 sec */
6857*4882a593Smuzhiyun 	ref_wr_duration[1] = msecs_to_jiffies(2500); /* actually 3 sec */
6858*4882a593Smuzhiyun 
6859*4882a593Smuzhiyun 	ret = elv_register(&iosched_bfq_mq);
6860*4882a593Smuzhiyun 	if (ret)
6861*4882a593Smuzhiyun 		goto slab_kill;
6862*4882a593Smuzhiyun 
6863*4882a593Smuzhiyun 	return 0;
6864*4882a593Smuzhiyun 
6865*4882a593Smuzhiyun slab_kill:
6866*4882a593Smuzhiyun 	bfq_slab_kill();
6867*4882a593Smuzhiyun err_pol_unreg:
6868*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
6869*4882a593Smuzhiyun 	blkcg_policy_unregister(&blkcg_policy_bfq);
6870*4882a593Smuzhiyun #endif
6871*4882a593Smuzhiyun 	return ret;
6872*4882a593Smuzhiyun }
6873*4882a593Smuzhiyun 
bfq_exit(void)6874*4882a593Smuzhiyun static void __exit bfq_exit(void)
6875*4882a593Smuzhiyun {
6876*4882a593Smuzhiyun 	elv_unregister(&iosched_bfq_mq);
6877*4882a593Smuzhiyun #ifdef CONFIG_BFQ_GROUP_IOSCHED
6878*4882a593Smuzhiyun 	blkcg_policy_unregister(&blkcg_policy_bfq);
6879*4882a593Smuzhiyun #endif
6880*4882a593Smuzhiyun 	bfq_slab_kill();
6881*4882a593Smuzhiyun }
6882*4882a593Smuzhiyun 
6883*4882a593Smuzhiyun module_init(bfq_init);
6884*4882a593Smuzhiyun module_exit(bfq_exit);
6885*4882a593Smuzhiyun 
6886*4882a593Smuzhiyun MODULE_AUTHOR("Paolo Valente");
6887*4882a593Smuzhiyun MODULE_LICENSE("GPL");
6888*4882a593Smuzhiyun MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler");
6889