blob: 12228af161982f5b32d7148d3b6ab9f5c1078d7e [file] [log] [blame]
Paolo Valenteaee69d72017-04-19 08:29:02 -06001/*
2 * Budget Fair Queueing (BFQ) I/O scheduler.
3 *
4 * Based on ideas and code from CFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
11 * Arianna Avanzini <avanzini@google.com>
12 *
13 * Copyright (C) 2017 Paolo Valente <paolo.valente@linaro.org>
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * BFQ is a proportional-share I/O scheduler, with some extra
26 * low-latency capabilities. BFQ also supports full hierarchical
27 * scheduling through cgroups. Next paragraphs provide an introduction
28 * on BFQ inner workings. Details on BFQ benefits, usage and
29 * limitations can be found in Documentation/block/bfq-iosched.txt.
30 *
31 * BFQ is a proportional-share storage-I/O scheduling algorithm based
32 * on the slice-by-slice service scheme of CFQ. But BFQ assigns
33 * budgets, measured in number of sectors, to processes instead of
34 * time slices. The device is not granted to the in-service process
35 * for a given time slice, but until it has exhausted its assigned
36 * budget. This change from the time to the service domain enables BFQ
37 * to distribute the device throughput among processes as desired,
38 * without any distortion due to throughput fluctuations, or to device
39 * internal queueing. BFQ uses an ad hoc internal scheduler, called
40 * B-WF2Q+, to schedule processes according to their budgets. More
41 * precisely, BFQ schedules queues associated with processes. Each
42 * process/queue is assigned a user-configurable weight, and B-WF2Q+
43 * guarantees that each queue receives a fraction of the throughput
44 * proportional to its weight. Thanks to the accurate policy of
45 * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound
46 * processes issuing sequential requests (to boost the throughput),
47 * and yet guarantee a low latency to interactive and soft real-time
48 * applications.
49 *
50 * In particular, to provide these low-latency guarantees, BFQ
51 * explicitly privileges the I/O of two classes of time-sensitive
Paolo Valente4029eef2018-05-31 16:45:05 +020052 * applications: interactive and soft real-time. In more detail, BFQ
53 * behaves this way if the low_latency parameter is set (default
54 * configuration). This feature enables BFQ to provide applications in
55 * these classes with a very low latency.
56 *
57 * To implement this feature, BFQ constantly tries to detect whether
58 * the I/O requests in a bfq_queue come from an interactive or a soft
59 * real-time application. For brevity, in these cases, the queue is
60 * said to be interactive or soft real-time. In both cases, BFQ
61 * privileges the service of the queue, over that of non-interactive
62 * and non-soft-real-time queues. This privileging is performed,
63 * mainly, by raising the weight of the queue. So, for brevity, we
64 * call just weight-raising periods the time periods during which a
65 * queue is privileged, because deemed interactive or soft real-time.
66 *
67 * The detection of soft real-time queues/applications is described in
68 * detail in the comments on the function
69 * bfq_bfqq_softrt_next_start. On the other hand, the detection of an
70 * interactive queue works as follows: a queue is deemed interactive
71 * if it is constantly non empty only for a limited time interval,
72 * after which it does become empty. The queue may be deemed
73 * interactive again (for a limited time), if it restarts being
74 * constantly non empty, provided that this happens only after the
75 * queue has remained empty for a given minimum idle time.
76 *
77 * By default, BFQ computes automatically the above maximum time
78 * interval, i.e., the time interval after which a constantly
79 * non-empty queue stops being deemed interactive. Since a queue is
80 * weight-raised while it is deemed interactive, this maximum time
81 * interval happens to coincide with the (maximum) duration of the
82 * weight-raising for interactive queues.
83 *
84 * Finally, BFQ also features additional heuristics for
Paolo Valenteaee69d72017-04-19 08:29:02 -060085 * preserving both a low latency and a high throughput on NCQ-capable,
86 * rotational or flash-based devices, and to get the job done quickly
87 * for applications consisting in many I/O-bound processes.
88 *
Paolo Valente43c1b3d2017-05-09 12:54:23 +020089 * NOTE: if the main or only goal, with a given device, is to achieve
90 * the maximum-possible throughput at all times, then do switch off
91 * all low-latency heuristics for that device, by setting low_latency
92 * to 0.
93 *
Paolo Valente4029eef2018-05-31 16:45:05 +020094 * BFQ is described in [1], where also a reference to the initial,
95 * more theoretical paper on BFQ can be found. The interested reader
96 * can find in the latter paper full details on the main algorithm, as
97 * well as formulas of the guarantees and formal proofs of all the
98 * properties. With respect to the version of BFQ presented in these
99 * papers, this implementation adds a few more heuristics, such as the
100 * ones that guarantee a low latency to interactive and soft real-time
101 * applications, and a hierarchical extension based on H-WF2Q+.
Paolo Valenteaee69d72017-04-19 08:29:02 -0600102 *
103 * B-WF2Q+ is based on WF2Q+, which is described in [2], together with
104 * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+
105 * with O(log N) complexity derives from the one introduced with EEVDF
106 * in [3].
107 *
108 * [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O
109 * Scheduler", Proceedings of the First Workshop on Mobile System
110 * Technologies (MST-2015), May 2015.
111 * http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
112 *
113 * [2] Jon C.R. Bennett and H. Zhang, "Hierarchical Packet Fair Queueing
114 * Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689,
115 * Oct 1997.
116 *
117 * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
118 *
119 * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline
120 * First: A Flexible and Accurate Mechanism for Proportional Share
121 * Resource Allocation", technical report.
122 *
123 * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
124 */
125#include <linux/module.h>
126#include <linux/slab.h>
127#include <linux/blkdev.h>
Arianna Avanzinie21b7a02017-04-12 18:23:08 +0200128#include <linux/cgroup.h>
Paolo Valenteaee69d72017-04-19 08:29:02 -0600129#include <linux/elevator.h>
130#include <linux/ktime.h>
131#include <linux/rbtree.h>
132#include <linux/ioprio.h>
133#include <linux/sbitmap.h>
134#include <linux/delay.h>
135
136#include "blk.h"
137#include "blk-mq.h"
138#include "blk-mq-tag.h"
139#include "blk-mq-sched.h"
Paolo Valenteea25da42017-04-19 08:48:24 -0600140#include "bfq-iosched.h"
Luca Micciob5dc5d42017-10-09 16:27:21 +0200141#include "blk-wbt.h"
Paolo Valenteaee69d72017-04-19 08:29:02 -0600142
143#define BFQ_BFQQ_FNS(name) \
Paolo Valenteea25da42017-04-19 08:48:24 -0600144void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
Paolo Valenteaee69d72017-04-19 08:29:02 -0600145{ \
146 __set_bit(BFQQF_##name, &(bfqq)->flags); \
147} \
Paolo Valenteea25da42017-04-19 08:48:24 -0600148void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
Paolo Valenteaee69d72017-04-19 08:29:02 -0600149{ \
150 __clear_bit(BFQQF_##name, &(bfqq)->flags); \
151} \
Paolo Valenteea25da42017-04-19 08:48:24 -0600152int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
Paolo Valenteaee69d72017-04-19 08:29:02 -0600153{ \
154 return test_bit(BFQQF_##name, &(bfqq)->flags); \
155}
156
Arianna Avanzinie1b23242017-04-12 18:23:20 +0200157BFQ_BFQQ_FNS(just_created);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600158BFQ_BFQQ_FNS(busy);
159BFQ_BFQQ_FNS(wait_request);
160BFQ_BFQQ_FNS(non_blocking_wait_rq);
161BFQ_BFQQ_FNS(fifo_expire);
Paolo Valented5be3fe2017-08-04 07:35:10 +0200162BFQ_BFQQ_FNS(has_short_ttime);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600163BFQ_BFQQ_FNS(sync);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600164BFQ_BFQQ_FNS(IO_bound);
Arianna Avanzinie1b23242017-04-12 18:23:20 +0200165BFQ_BFQQ_FNS(in_large_burst);
Arianna Avanzini36eca892017-04-12 18:23:16 +0200166BFQ_BFQQ_FNS(coop);
167BFQ_BFQQ_FNS(split_coop);
Paolo Valente77b7dce2017-04-12 18:23:13 +0200168BFQ_BFQQ_FNS(softrt_update);
Paolo Valenteea25da42017-04-19 08:48:24 -0600169#undef BFQ_BFQQ_FNS \
Paolo Valenteaee69d72017-04-19 08:29:02 -0600170
Paolo Valenteaee69d72017-04-19 08:29:02 -0600171/* Expiration time of sync (0) and async (1) requests, in ns. */
172static const u64 bfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
173
174/* Maximum backwards seek (magic number lifted from CFQ), in KiB. */
175static const int bfq_back_max = 16 * 1024;
176
177/* Penalty of a backwards seek, in number of sectors. */
178static const int bfq_back_penalty = 2;
179
180/* Idling period duration, in ns. */
181static u64 bfq_slice_idle = NSEC_PER_SEC / 125;
182
183/* Minimum number of assigned budgets for which stats are safe to compute. */
184static const int bfq_stats_min_budgets = 194;
185
186/* Default maximum budget values, in sectors and number of requests. */
187static const int bfq_default_max_budget = 16 * 1024;
188
Paolo Valentec074170e2017-04-12 18:23:11 +0200189/*
Paolo Valented5801082018-08-16 18:51:17 +0200190 * When a sync request is dispatched, the queue that contains that
191 * request, and all the ancestor entities of that queue, are charged
192 * with the number of sectors of the request. In constrast, if the
193 * request is async, then the queue and its ancestor entities are
194 * charged with the number of sectors of the request, multiplied by
195 * the factor below. This throttles the bandwidth for async I/O,
196 * w.r.t. to sync I/O, and it is done to counter the tendency of async
197 * writes to steal I/O throughput to reads.
198 *
199 * The current value of this parameter is the result of a tuning with
200 * several hardware and software configurations. We tried to find the
201 * lowest value for which writes do not cause noticeable problems to
202 * reads. In fact, the lower this parameter, the stabler I/O control,
203 * in the following respect. The lower this parameter is, the less
204 * the bandwidth enjoyed by a group decreases
205 * - when the group does writes, w.r.t. to when it does reads;
206 * - when other groups do reads, w.r.t. to when they do writes.
Paolo Valentec074170e2017-04-12 18:23:11 +0200207 */
Paolo Valented5801082018-08-16 18:51:17 +0200208static const int bfq_async_charge_factor = 3;
Paolo Valentec074170e2017-04-12 18:23:11 +0200209
Paolo Valenteaee69d72017-04-19 08:29:02 -0600210/* Default timeout values, in jiffies, approximating CFQ defaults. */
Paolo Valenteea25da42017-04-19 08:48:24 -0600211const int bfq_timeout = HZ / 8;
Paolo Valenteaee69d72017-04-19 08:29:02 -0600212
Paolo Valente7b8fa3b2017-12-20 12:38:33 +0100213/*
214 * Time limit for merging (see comments in bfq_setup_cooperator). Set
215 * to the slowest value that, in our tests, proved to be effective in
216 * removing false positives, while not causing true positives to miss
217 * queue merging.
218 *
219 * As can be deduced from the low time limit below, queue merging, if
220 * successful, happens at the very beggining of the I/O of the involved
221 * cooperating processes, as a consequence of the arrival of the very
222 * first requests from each cooperator. After that, there is very
223 * little chance to find cooperators.
224 */
225static const unsigned long bfq_merge_time_limit = HZ/10;
226
Paolo Valenteaee69d72017-04-19 08:29:02 -0600227static struct kmem_cache *bfq_pool;
228
Paolo Valenteab0e43e2017-04-12 18:23:10 +0200229/* Below this threshold (in ns), we consider thinktime immediate. */
Paolo Valenteaee69d72017-04-19 08:29:02 -0600230#define BFQ_MIN_TT (2 * NSEC_PER_MSEC)
231
232/* hw_tag detection: parallel requests threshold and min samples needed. */
233#define BFQ_HW_QUEUE_THRESHOLD 4
234#define BFQ_HW_QUEUE_SAMPLES 32
235
236#define BFQQ_SEEK_THR (sector_t)(8 * 100)
237#define BFQQ_SECT_THR_NONROT (sector_t)(2 * 32)
Paolo Valented87447d2019-01-29 12:06:33 +0100238#define BFQ_RQ_SEEKY(bfqd, last_pos, rq) \
239 (get_sdist(last_pos, rq) > \
240 BFQQ_SEEK_THR && \
241 (!blk_queue_nonrot(bfqd->queue) || \
242 blk_rq_sectors(rq) < BFQQ_SECT_THR_NONROT))
Paolo Valenteaee69d72017-04-19 08:29:02 -0600243#define BFQQ_CLOSE_THR (sector_t)(8 * 1024)
Paolo Valentef0ba5ea2017-12-20 17:27:36 +0100244#define BFQQ_SEEKY(bfqq) (hweight32(bfqq->seek_history) > 19)
Paolo Valenteaee69d72017-04-19 08:29:02 -0600245
Paolo Valenteab0e43e2017-04-12 18:23:10 +0200246/* Min number of samples required to perform peak-rate update */
247#define BFQ_RATE_MIN_SAMPLES 32
248/* Min observation time interval required to perform a peak-rate update (ns) */
249#define BFQ_RATE_MIN_INTERVAL (300*NSEC_PER_MSEC)
250/* Target observation time interval for a peak-rate update (ns) */
251#define BFQ_RATE_REF_INTERVAL NSEC_PER_SEC
Paolo Valenteaee69d72017-04-19 08:29:02 -0600252
Paolo Valentebc56e2c2018-03-26 16:06:24 +0200253/*
254 * Shift used for peak-rate fixed precision calculations.
255 * With
256 * - the current shift: 16 positions
257 * - the current type used to store rate: u32
258 * - the current unit of measure for rate: [sectors/usec], or, more precisely,
259 * [(sectors/usec) / 2^BFQ_RATE_SHIFT] to take into account the shift,
260 * the range of rates that can be stored is
261 * [1 / 2^BFQ_RATE_SHIFT, 2^(32 - BFQ_RATE_SHIFT)] sectors/usec =
262 * [1 / 2^16, 2^16] sectors/usec = [15e-6, 65536] sectors/usec =
263 * [15, 65G] sectors/sec
264 * Which, assuming a sector size of 512B, corresponds to a range of
265 * [7.5K, 33T] B/sec
266 */
Paolo Valenteaee69d72017-04-19 08:29:02 -0600267#define BFQ_RATE_SHIFT 16
268
Paolo Valente44e44a12017-04-12 18:23:12 +0200269/*
Paolo Valente4029eef2018-05-31 16:45:05 +0200270 * When configured for computing the duration of the weight-raising
271 * for interactive queues automatically (see the comments at the
272 * beginning of this file), BFQ does it using the following formula:
Paolo Valentee24f1c22018-05-31 16:45:06 +0200273 * duration = (ref_rate / r) * ref_wr_duration,
274 * where r is the peak rate of the device, and ref_rate and
275 * ref_wr_duration are two reference parameters. In particular,
276 * ref_rate is the peak rate of the reference storage device (see
277 * below), and ref_wr_duration is about the maximum time needed, with
278 * BFQ and while reading two files in parallel, to load typical large
279 * applications on the reference device (see the comments on
280 * max_service_from_wr below, for more details on how ref_wr_duration
281 * is obtained). In practice, the slower/faster the device at hand
282 * is, the more/less it takes to load applications with respect to the
Paolo Valente4029eef2018-05-31 16:45:05 +0200283 * reference device. Accordingly, the longer/shorter BFQ grants
284 * weight raising to interactive applications.
Paolo Valente44e44a12017-04-12 18:23:12 +0200285 *
Paolo Valentee24f1c22018-05-31 16:45:06 +0200286 * BFQ uses two different reference pairs (ref_rate, ref_wr_duration),
287 * depending on whether the device is rotational or non-rotational.
Paolo Valente44e44a12017-04-12 18:23:12 +0200288 *
Paolo Valentee24f1c22018-05-31 16:45:06 +0200289 * In the following definitions, ref_rate[0] and ref_wr_duration[0]
290 * are the reference values for a rotational device, whereas
291 * ref_rate[1] and ref_wr_duration[1] are the reference values for a
292 * non-rotational device. The reference rates are not the actual peak
293 * rates of the devices used as a reference, but slightly lower
294 * values. The reason for using slightly lower values is that the
295 * peak-rate estimator tends to yield slightly lower values than the
296 * actual peak rate (it can yield the actual peak rate only if there
297 * is only one process doing I/O, and the process does sequential
298 * I/O).
Paolo Valente44e44a12017-04-12 18:23:12 +0200299 *
Paolo Valentee24f1c22018-05-31 16:45:06 +0200300 * The reference peak rates are measured in sectors/usec, left-shifted
301 * by BFQ_RATE_SHIFT.
Paolo Valente44e44a12017-04-12 18:23:12 +0200302 */
Paolo Valentee24f1c22018-05-31 16:45:06 +0200303static int ref_rate[2] = {14000, 33000};
Paolo Valente44e44a12017-04-12 18:23:12 +0200304/*
Paolo Valentee24f1c22018-05-31 16:45:06 +0200305 * To improve readability, a conversion function is used to initialize
306 * the following array, which entails that the array can be
307 * initialized only in a function.
Paolo Valente44e44a12017-04-12 18:23:12 +0200308 */
Paolo Valentee24f1c22018-05-31 16:45:06 +0200309static int ref_wr_duration[2];
Paolo Valente44e44a12017-04-12 18:23:12 +0200310
Paolo Valente8a8747d2018-01-13 12:05:18 +0100311/*
312 * BFQ uses the above-detailed, time-based weight-raising mechanism to
313 * privilege interactive tasks. This mechanism is vulnerable to the
314 * following false positives: I/O-bound applications that will go on
315 * doing I/O for much longer than the duration of weight
316 * raising. These applications have basically no benefit from being
317 * weight-raised at the beginning of their I/O. On the opposite end,
318 * while being weight-raised, these applications
319 * a) unjustly steal throughput to applications that may actually need
320 * low latency;
321 * b) make BFQ uselessly perform device idling; device idling results
322 * in loss of device throughput with most flash-based storage, and may
323 * increase latencies when used purposelessly.
324 *
325 * BFQ tries to reduce these problems, by adopting the following
326 * countermeasure. To introduce this countermeasure, we need first to
327 * finish explaining how the duration of weight-raising for
328 * interactive tasks is computed.
329 *
330 * For a bfq_queue deemed as interactive, the duration of weight
331 * raising is dynamically adjusted, as a function of the estimated
332 * peak rate of the device, so as to be equal to the time needed to
333 * execute the 'largest' interactive task we benchmarked so far. By
334 * largest task, we mean the task for which each involved process has
335 * to do more I/O than for any of the other tasks we benchmarked. This
336 * reference interactive task is the start-up of LibreOffice Writer,
337 * and in this task each process/bfq_queue needs to have at most ~110K
338 * sectors transferred.
339 *
340 * This last piece of information enables BFQ to reduce the actual
341 * duration of weight-raising for at least one class of I/O-bound
342 * applications: those doing sequential or quasi-sequential I/O. An
343 * example is file copy. In fact, once started, the main I/O-bound
344 * processes of these applications usually consume the above 110K
345 * sectors in much less time than the processes of an application that
346 * is starting, because these I/O-bound processes will greedily devote
347 * almost all their CPU cycles only to their target,
348 * throughput-friendly I/O operations. This is even more true if BFQ
349 * happens to be underestimating the device peak rate, and thus
350 * overestimating the duration of weight raising. But, according to
351 * our measurements, once transferred 110K sectors, these processes
352 * have no right to be weight-raised any longer.
353 *
354 * Basing on the last consideration, BFQ ends weight-raising for a
355 * bfq_queue if the latter happens to have received an amount of
356 * service at least equal to the following constant. The constant is
357 * set to slightly more than 110K, to have a minimum safety margin.
358 *
359 * This early ending of weight-raising reduces the amount of time
360 * during which interactive false positives cause the two problems
361 * described at the beginning of these comments.
362 */
363static const unsigned long max_service_from_wr = 120000;
364
Bart Van Assche12cd3a22017-08-30 11:42:11 -0700365#define RQ_BIC(rq) icq_to_bic((rq)->elv.priv[0])
Paolo Valenteaee69d72017-04-19 08:29:02 -0600366#define RQ_BFQQ(rq) ((rq)->elv.priv[1])
367
Paolo Valenteea25da42017-04-19 08:48:24 -0600368struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync)
369{
370 return bic->bfqq[is_sync];
371}
372
373void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync)
374{
375 bic->bfqq[is_sync] = bfqq;
376}
377
378struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
379{
380 return bic->icq.q->elevator->elevator_data;
381}
382
Paolo Valenteaee69d72017-04-19 08:29:02 -0600383/**
384 * icq_to_bic - convert iocontext queue structure to bfq_io_cq.
385 * @icq: the iocontext queue.
386 */
387static struct bfq_io_cq *icq_to_bic(struct io_cq *icq)
388{
389 /* bic->icq is the first member, %NULL will convert to %NULL */
390 return container_of(icq, struct bfq_io_cq, icq);
391}
392
393/**
394 * bfq_bic_lookup - search into @ioc a bic associated to @bfqd.
395 * @bfqd: the lookup key.
396 * @ioc: the io_context of the process doing I/O.
397 * @q: the request queue.
398 */
399static struct bfq_io_cq *bfq_bic_lookup(struct bfq_data *bfqd,
400 struct io_context *ioc,
401 struct request_queue *q)
402{
403 if (ioc) {
404 unsigned long flags;
405 struct bfq_io_cq *icq;
406
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700407 spin_lock_irqsave(&q->queue_lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600408 icq = icq_to_bic(ioc_lookup_icq(ioc, q));
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700409 spin_unlock_irqrestore(&q->queue_lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600410
411 return icq;
412 }
413
414 return NULL;
415}
416
417/*
Arianna Avanzinie21b7a02017-04-12 18:23:08 +0200418 * Scheduler run of queue, if there are requests pending and no one in the
419 * driver that will restart queueing.
Paolo Valenteaee69d72017-04-19 08:29:02 -0600420 */
Paolo Valenteea25da42017-04-19 08:48:24 -0600421void bfq_schedule_dispatch(struct bfq_data *bfqd)
Paolo Valenteaee69d72017-04-19 08:29:02 -0600422{
Arianna Avanzinie21b7a02017-04-12 18:23:08 +0200423 if (bfqd->queued != 0) {
424 bfq_log(bfqd, "schedule dispatch");
425 blk_mq_run_hw_queues(bfqd->queue, true);
426 }
Paolo Valenteaee69d72017-04-19 08:29:02 -0600427}
428
Paolo Valenteaee69d72017-04-19 08:29:02 -0600429#define bfq_class_idle(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
430#define bfq_class_rt(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_RT)
431
432#define bfq_sample_valid(samples) ((samples) > 80)
433
434/*
Paolo Valenteaee69d72017-04-19 08:29:02 -0600435 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
436 * We choose the request that is closesr to the head right now. Distance
437 * behind the head is penalized and only allowed to a certain extent.
438 */
439static struct request *bfq_choose_req(struct bfq_data *bfqd,
440 struct request *rq1,
441 struct request *rq2,
442 sector_t last)
443{
444 sector_t s1, s2, d1 = 0, d2 = 0;
445 unsigned long back_max;
446#define BFQ_RQ1_WRAP 0x01 /* request 1 wraps */
447#define BFQ_RQ2_WRAP 0x02 /* request 2 wraps */
448 unsigned int wrap = 0; /* bit mask: requests behind the disk head? */
449
450 if (!rq1 || rq1 == rq2)
451 return rq2;
452 if (!rq2)
453 return rq1;
454
455 if (rq_is_sync(rq1) && !rq_is_sync(rq2))
456 return rq1;
457 else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
458 return rq2;
459 if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
460 return rq1;
461 else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META))
462 return rq2;
463
464 s1 = blk_rq_pos(rq1);
465 s2 = blk_rq_pos(rq2);
466
467 /*
468 * By definition, 1KiB is 2 sectors.
469 */
470 back_max = bfqd->bfq_back_max * 2;
471
472 /*
473 * Strict one way elevator _except_ in the case where we allow
474 * short backward seeks which are biased as twice the cost of a
475 * similar forward seek.
476 */
477 if (s1 >= last)
478 d1 = s1 - last;
479 else if (s1 + back_max >= last)
480 d1 = (last - s1) * bfqd->bfq_back_penalty;
481 else
482 wrap |= BFQ_RQ1_WRAP;
483
484 if (s2 >= last)
485 d2 = s2 - last;
486 else if (s2 + back_max >= last)
487 d2 = (last - s2) * bfqd->bfq_back_penalty;
488 else
489 wrap |= BFQ_RQ2_WRAP;
490
491 /* Found required data */
492
493 /*
494 * By doing switch() on the bit mask "wrap" we avoid having to
495 * check two variables for all permutations: --> faster!
496 */
497 switch (wrap) {
498 case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
499 if (d1 < d2)
500 return rq1;
501 else if (d2 < d1)
502 return rq2;
503
504 if (s1 >= s2)
505 return rq1;
506 else
507 return rq2;
508
509 case BFQ_RQ2_WRAP:
510 return rq1;
511 case BFQ_RQ1_WRAP:
512 return rq2;
513 case BFQ_RQ1_WRAP|BFQ_RQ2_WRAP: /* both rqs wrapped */
514 default:
515 /*
516 * Since both rqs are wrapped,
517 * start with the one that's further behind head
518 * (--> only *one* back seek required),
519 * since back seek takes more time than forward.
520 */
521 if (s1 <= s2)
522 return rq1;
523 else
524 return rq2;
525 }
526}
527
Paolo Valentea52a69e2018-01-13 12:05:17 +0100528/*
Paolo Valentea52a69e2018-01-13 12:05:17 +0100529 * Async I/O can easily starve sync I/O (both sync reads and sync
530 * writes), by consuming all tags. Similarly, storms of sync writes,
531 * such as those that sync(2) may trigger, can starve sync reads.
532 * Limit depths of async I/O and sync writes so as to counter both
533 * problems.
534 */
535static void bfq_limit_depth(unsigned int op, struct blk_mq_alloc_data *data)
536{
Paolo Valentea52a69e2018-01-13 12:05:17 +0100537 struct bfq_data *bfqd = data->q->elevator->elevator_data;
Paolo Valentea52a69e2018-01-13 12:05:17 +0100538
539 if (op_is_sync(op) && !op_is_write(op))
540 return;
541
Paolo Valentea52a69e2018-01-13 12:05:17 +0100542 data->shallow_depth =
543 bfqd->word_depths[!!bfqd->wr_busy_queues][op_is_sync(op)];
544
545 bfq_log(bfqd, "[%s] wr_busy %d sync %d depth %u",
546 __func__, bfqd->wr_busy_queues, op_is_sync(op),
547 data->shallow_depth);
548}
549
Arianna Avanzini36eca892017-04-12 18:23:16 +0200550static struct bfq_queue *
551bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root,
552 sector_t sector, struct rb_node **ret_parent,
553 struct rb_node ***rb_link)
554{
555 struct rb_node **p, *parent;
556 struct bfq_queue *bfqq = NULL;
557
558 parent = NULL;
559 p = &root->rb_node;
560 while (*p) {
561 struct rb_node **n;
562
563 parent = *p;
564 bfqq = rb_entry(parent, struct bfq_queue, pos_node);
565
566 /*
567 * Sort strictly based on sector. Smallest to the left,
568 * largest to the right.
569 */
570 if (sector > blk_rq_pos(bfqq->next_rq))
571 n = &(*p)->rb_right;
572 else if (sector < blk_rq_pos(bfqq->next_rq))
573 n = &(*p)->rb_left;
574 else
575 break;
576 p = n;
577 bfqq = NULL;
578 }
579
580 *ret_parent = parent;
581 if (rb_link)
582 *rb_link = p;
583
584 bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d",
585 (unsigned long long)sector,
586 bfqq ? bfqq->pid : 0);
587
588 return bfqq;
589}
590
Paolo Valente7b8fa3b2017-12-20 12:38:33 +0100591static bool bfq_too_late_for_merging(struct bfq_queue *bfqq)
592{
593 return bfqq->service_from_backlogged > 0 &&
594 time_is_before_jiffies(bfqq->first_IO_time +
595 bfq_merge_time_limit);
596}
597
Paolo Valenteea25da42017-04-19 08:48:24 -0600598void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq)
Arianna Avanzini36eca892017-04-12 18:23:16 +0200599{
600 struct rb_node **p, *parent;
601 struct bfq_queue *__bfqq;
602
603 if (bfqq->pos_root) {
604 rb_erase(&bfqq->pos_node, bfqq->pos_root);
605 bfqq->pos_root = NULL;
606 }
607
Paolo Valente7b8fa3b2017-12-20 12:38:33 +0100608 /*
609 * bfqq cannot be merged any longer (see comments in
610 * bfq_setup_cooperator): no point in adding bfqq into the
611 * position tree.
612 */
613 if (bfq_too_late_for_merging(bfqq))
614 return;
615
Arianna Avanzini36eca892017-04-12 18:23:16 +0200616 if (bfq_class_idle(bfqq))
617 return;
618 if (!bfqq->next_rq)
619 return;
620
621 bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
622 __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root,
623 blk_rq_pos(bfqq->next_rq), &parent, &p);
624 if (!__bfqq) {
625 rb_link_node(&bfqq->pos_node, parent, p);
626 rb_insert_color(&bfqq->pos_node, bfqq->pos_root);
627 } else
628 bfqq->pos_root = NULL;
629}
630
Paolo Valenteaee69d72017-04-19 08:29:02 -0600631/*
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200632 * The following function returns true if every queue must receive the
633 * same share of the throughput (this condition is used when deciding
634 * whether idling may be disabled, see the comments in the function
Paolo Valente277a4a92018-06-25 21:55:37 +0200635 * bfq_better_to_idle()).
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200636 *
637 * Such a scenario occurs when:
638 * 1) all active queues have the same weight,
Paolo Valente73d58112019-01-29 12:06:29 +0100639 * 2) all active queues belong to the same I/O-priority class,
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200640 * 3) all active groups at the same level in the groups tree have the same
Paolo Valente73d58112019-01-29 12:06:29 +0100641 * weight,
642 * 4) all active groups at the same level in the groups tree have the same
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200643 * number of children.
644 *
Federico Motta2d29c9f2018-10-12 11:55:57 +0200645 * Unfortunately, keeping the necessary state for evaluating exactly
646 * the last two symmetry sub-conditions above would be quite complex
Paolo Valente73d58112019-01-29 12:06:29 +0100647 * and time consuming. Therefore this function evaluates, instead,
648 * only the following stronger three sub-conditions, for which it is
Federico Motta2d29c9f2018-10-12 11:55:57 +0200649 * much easier to maintain the needed state:
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200650 * 1) all active queues have the same weight,
Paolo Valente73d58112019-01-29 12:06:29 +0100651 * 2) all active queues belong to the same I/O-priority class,
652 * 3) there are no active groups.
Federico Motta2d29c9f2018-10-12 11:55:57 +0200653 * In particular, the last condition is always true if hierarchical
654 * support or the cgroups interface are not enabled, thus no state
655 * needs to be maintained in this case.
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200656 */
657static bool bfq_symmetric_scenario(struct bfq_data *bfqd)
658{
Paolo Valente73d58112019-01-29 12:06:29 +0100659 /*
660 * For queue weights to differ, queue_weights_tree must contain
661 * at least two nodes.
662 */
663 bool varied_queue_weights = !RB_EMPTY_ROOT(&bfqd->queue_weights_tree) &&
664 (bfqd->queue_weights_tree.rb_node->rb_left ||
665 bfqd->queue_weights_tree.rb_node->rb_right);
666
667 bool multiple_classes_busy =
668 (bfqd->busy_queues[0] && bfqd->busy_queues[1]) ||
669 (bfqd->busy_queues[0] && bfqd->busy_queues[2]) ||
670 (bfqd->busy_queues[1] && bfqd->busy_queues[2]);
671
672 /*
673 * For queue weights to differ, queue_weights_tree must contain
674 * at least two nodes.
675 */
676 return !(varied_queue_weights || multiple_classes_busy
677#ifdef BFQ_GROUP_IOSCHED_ENABLED
678 || bfqd->num_groups_with_pending_reqs > 0
679#endif
680 );
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200681}
682
683/*
684 * If the weight-counter tree passed as input contains no counter for
Federico Motta2d29c9f2018-10-12 11:55:57 +0200685 * the weight of the input queue, then add that counter; otherwise just
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200686 * increment the existing counter.
687 *
688 * Note that weight-counter trees contain few nodes in mostly symmetric
689 * scenarios. For example, if all queues have the same weight, then the
690 * weight-counter tree for the queues may contain at most one node.
691 * This holds even if low_latency is on, because weight-raised queues
692 * are not inserted in the tree.
693 * In most scenarios, the rate at which nodes are created/destroyed
694 * should be low too.
695 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200696void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
Paolo Valenteea25da42017-04-19 08:48:24 -0600697 struct rb_root *root)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200698{
Federico Motta2d29c9f2018-10-12 11:55:57 +0200699 struct bfq_entity *entity = &bfqq->entity;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200700 struct rb_node **new = &(root->rb_node), *parent = NULL;
701
702 /*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200703 * Do not insert if the queue is already associated with a
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200704 * counter, which happens if:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200705 * 1) a request arrival has caused the queue to become both
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200706 * non-weight-raised, and hence change its weight, and
707 * backlogged; in this respect, each of the two events
708 * causes an invocation of this function,
Federico Motta2d29c9f2018-10-12 11:55:57 +0200709 * 2) this is the invocation of this function caused by the
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200710 * second event. This second invocation is actually useless,
711 * and we handle this fact by exiting immediately. More
712 * efficient or clearer solutions might possibly be adopted.
713 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200714 if (bfqq->weight_counter)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200715 return;
716
717 while (*new) {
718 struct bfq_weight_counter *__counter = container_of(*new,
719 struct bfq_weight_counter,
720 weights_node);
721 parent = *new;
722
723 if (entity->weight == __counter->weight) {
Federico Motta2d29c9f2018-10-12 11:55:57 +0200724 bfqq->weight_counter = __counter;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200725 goto inc_counter;
726 }
727 if (entity->weight < __counter->weight)
728 new = &((*new)->rb_left);
729 else
730 new = &((*new)->rb_right);
731 }
732
Federico Motta2d29c9f2018-10-12 11:55:57 +0200733 bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
734 GFP_ATOMIC);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200735
736 /*
737 * In the unlucky event of an allocation failure, we just
Federico Motta2d29c9f2018-10-12 11:55:57 +0200738 * exit. This will cause the weight of queue to not be
Paolo Valente73d58112019-01-29 12:06:29 +0100739 * considered in bfq_symmetric_scenario, which, in its turn,
740 * causes the scenario to be deemed wrongly symmetric in case
741 * bfqq's weight would have been the only weight making the
742 * scenario asymmetric. On the bright side, no unbalance will
743 * however occur when bfqq becomes inactive again (the
744 * invocation of this function is triggered by an activation
745 * of queue). In fact, bfq_weights_tree_remove does nothing
746 * if !bfqq->weight_counter.
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200747 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200748 if (unlikely(!bfqq->weight_counter))
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200749 return;
750
Federico Motta2d29c9f2018-10-12 11:55:57 +0200751 bfqq->weight_counter->weight = entity->weight;
752 rb_link_node(&bfqq->weight_counter->weights_node, parent, new);
753 rb_insert_color(&bfqq->weight_counter->weights_node, root);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200754
755inc_counter:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200756 bfqq->weight_counter->num_active++;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200757}
758
759/*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200760 * Decrement the weight counter associated with the queue, and, if the
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200761 * counter reaches 0, remove the counter from the tree.
762 * See the comments to the function bfq_weights_tree_add() for considerations
763 * about overhead.
764 */
Paolo Valente04715592018-06-25 21:55:34 +0200765void __bfq_weights_tree_remove(struct bfq_data *bfqd,
Federico Motta2d29c9f2018-10-12 11:55:57 +0200766 struct bfq_queue *bfqq,
Paolo Valente04715592018-06-25 21:55:34 +0200767 struct rb_root *root)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200768{
Federico Motta2d29c9f2018-10-12 11:55:57 +0200769 if (!bfqq->weight_counter)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200770 return;
771
Federico Motta2d29c9f2018-10-12 11:55:57 +0200772 bfqq->weight_counter->num_active--;
773 if (bfqq->weight_counter->num_active > 0)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200774 goto reset_entity_pointer;
775
Federico Motta2d29c9f2018-10-12 11:55:57 +0200776 rb_erase(&bfqq->weight_counter->weights_node, root);
777 kfree(bfqq->weight_counter);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200778
779reset_entity_pointer:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200780 bfqq->weight_counter = NULL;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200781}
782
783/*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200784 * Invoke __bfq_weights_tree_remove on bfqq and decrement the number
785 * of active groups for each queue's inactive parent entity.
Paolo Valente04715592018-06-25 21:55:34 +0200786 */
787void bfq_weights_tree_remove(struct bfq_data *bfqd,
788 struct bfq_queue *bfqq)
789{
790 struct bfq_entity *entity = bfqq->entity.parent;
791
Federico Motta2d29c9f2018-10-12 11:55:57 +0200792 __bfq_weights_tree_remove(bfqd, bfqq,
Paolo Valente04715592018-06-25 21:55:34 +0200793 &bfqd->queue_weights_tree);
794
795 for_each_entity(entity) {
796 struct bfq_sched_data *sd = entity->my_sched_data;
797
798 if (sd->next_in_service || sd->in_service_entity) {
799 /*
800 * entity is still active, because either
801 * next_in_service or in_service_entity is not
802 * NULL (see the comments on the definition of
803 * next_in_service for details on why
804 * in_service_entity must be checked too).
805 *
Federico Motta2d29c9f2018-10-12 11:55:57 +0200806 * As a consequence, its parent entities are
807 * active as well, and thus this loop must
808 * stop here.
Paolo Valente04715592018-06-25 21:55:34 +0200809 */
810 break;
811 }
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100812
813 /*
814 * The decrement of num_groups_with_pending_reqs is
815 * not performed immediately upon the deactivation of
816 * entity, but it is delayed to when it also happens
817 * that the first leaf descendant bfqq of entity gets
818 * all its pending requests completed. The following
819 * instructions perform this delayed decrement, if
820 * needed. See the comments on
821 * num_groups_with_pending_reqs for details.
822 */
823 if (entity->in_groups_with_pending_reqs) {
824 entity->in_groups_with_pending_reqs = false;
825 bfqd->num_groups_with_pending_reqs--;
826 }
Paolo Valente04715592018-06-25 21:55:34 +0200827 }
828}
829
830/*
Paolo Valenteaee69d72017-04-19 08:29:02 -0600831 * Return expired entry, or NULL to just start from scratch in rbtree.
832 */
833static struct request *bfq_check_fifo(struct bfq_queue *bfqq,
834 struct request *last)
835{
836 struct request *rq;
837
838 if (bfq_bfqq_fifo_expire(bfqq))
839 return NULL;
840
841 bfq_mark_bfqq_fifo_expire(bfqq);
842
843 rq = rq_entry_fifo(bfqq->fifo.next);
844
845 if (rq == last || ktime_get_ns() < rq->fifo_time)
846 return NULL;
847
848 bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq);
849 return rq;
850}
851
852static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
853 struct bfq_queue *bfqq,
854 struct request *last)
855{
856 struct rb_node *rbnext = rb_next(&last->rb_node);
857 struct rb_node *rbprev = rb_prev(&last->rb_node);
858 struct request *next, *prev = NULL;
859
860 /* Follow expired path, else get first next available. */
861 next = bfq_check_fifo(bfqq, last);
862 if (next)
863 return next;
864
865 if (rbprev)
866 prev = rb_entry_rq(rbprev);
867
868 if (rbnext)
869 next = rb_entry_rq(rbnext);
870 else {
871 rbnext = rb_first(&bfqq->sort_list);
872 if (rbnext && rbnext != &last->rb_node)
873 next = rb_entry_rq(rbnext);
874 }
875
876 return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
877}
878
Paolo Valentec074170e2017-04-12 18:23:11 +0200879/* see the definition of bfq_async_charge_factor for details */
Paolo Valenteaee69d72017-04-19 08:29:02 -0600880static unsigned long bfq_serv_to_charge(struct request *rq,
881 struct bfq_queue *bfqq)
882{
Paolo Valente44e44a12017-04-12 18:23:12 +0200883 if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1)
Paolo Valentec074170e2017-04-12 18:23:11 +0200884 return blk_rq_sectors(rq);
885
Paolo Valented5801082018-08-16 18:51:17 +0200886 return blk_rq_sectors(rq) * bfq_async_charge_factor;
Paolo Valenteaee69d72017-04-19 08:29:02 -0600887}
888
889/**
890 * bfq_updated_next_req - update the queue after a new next_rq selection.
891 * @bfqd: the device data the queue belongs to.
892 * @bfqq: the queue to update.
893 *
894 * If the first request of a queue changes we make sure that the queue
895 * has enough budget to serve at least its first request (if the
896 * request has grown). We do this because if the queue has not enough
897 * budget for its first request, it has to go through two dispatch
898 * rounds to actually get it dispatched.
899 */
900static void bfq_updated_next_req(struct bfq_data *bfqd,
901 struct bfq_queue *bfqq)
902{
903 struct bfq_entity *entity = &bfqq->entity;
904 struct request *next_rq = bfqq->next_rq;
905 unsigned long new_budget;
906
907 if (!next_rq)
908 return;
909
910 if (bfqq == bfqd->in_service_queue)
911 /*
912 * In order not to break guarantees, budgets cannot be
913 * changed after an entity has been selected.
914 */
915 return;
916
Paolo Valentef3218ad2019-01-29 12:06:27 +0100917 new_budget = max_t(unsigned long,
918 max_t(unsigned long, bfqq->max_budget,
919 bfq_serv_to_charge(next_rq, bfqq)),
920 entity->service);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600921 if (entity->budget != new_budget) {
922 entity->budget = new_budget;
923 bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
924 new_budget);
Paolo Valente80294c32017-08-31 08:46:29 +0200925 bfq_requeue_bfqq(bfqd, bfqq, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600926 }
927}
928
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200929static unsigned int bfq_wr_duration(struct bfq_data *bfqd)
930{
931 u64 dur;
932
933 if (bfqd->bfq_wr_max_time > 0)
934 return bfqd->bfq_wr_max_time;
935
Paolo Valentee24f1c22018-05-31 16:45:06 +0200936 dur = bfqd->rate_dur_prod;
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200937 do_div(dur, bfqd->peak_rate);
938
939 /*
Davide Sapienzad4505422018-05-31 16:45:07 +0200940 * Limit duration between 3 and 25 seconds. The upper limit
941 * has been conservatively set after the following worst case:
942 * on a QEMU/KVM virtual machine
943 * - running in a slow PC
944 * - with a virtual disk stacked on a slow low-end 5400rpm HDD
945 * - serving a heavy I/O workload, such as the sequential reading
946 * of several files
947 * mplayer took 23 seconds to start, if constantly weight-raised.
948 *
949 * As for higher values than that accomodating the above bad
950 * scenario, tests show that higher values would often yield
951 * the opposite of the desired result, i.e., would worsen
952 * responsiveness by allowing non-interactive applications to
953 * preserve weight raising for too long.
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200954 *
955 * On the other end, lower values than 3 seconds make it
956 * difficult for most interactive tasks to complete their jobs
957 * before weight-raising finishes.
958 */
Davide Sapienzad4505422018-05-31 16:45:07 +0200959 return clamp_val(dur, msecs_to_jiffies(3000), msecs_to_jiffies(25000));
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200960}
961
962/* switch back from soft real-time to interactive weight raising */
963static void switch_back_to_interactive_wr(struct bfq_queue *bfqq,
964 struct bfq_data *bfqd)
965{
966 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
967 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
968 bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt;
969}
970
Arianna Avanzini36eca892017-04-12 18:23:16 +0200971static void
Paolo Valente13c931b2017-06-27 12:30:47 -0600972bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd,
973 struct bfq_io_cq *bic, bool bfq_already_existing)
Arianna Avanzini36eca892017-04-12 18:23:16 +0200974{
Paolo Valente13c931b2017-06-27 12:30:47 -0600975 unsigned int old_wr_coeff = bfqq->wr_coeff;
976 bool busy = bfq_already_existing && bfq_bfqq_busy(bfqq);
977
Paolo Valented5be3fe2017-08-04 07:35:10 +0200978 if (bic->saved_has_short_ttime)
979 bfq_mark_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +0200980 else
Paolo Valented5be3fe2017-08-04 07:35:10 +0200981 bfq_clear_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +0200982
983 if (bic->saved_IO_bound)
984 bfq_mark_bfqq_IO_bound(bfqq);
985 else
986 bfq_clear_bfqq_IO_bound(bfqq);
987
988 bfqq->ttime = bic->saved_ttime;
989 bfqq->wr_coeff = bic->saved_wr_coeff;
990 bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt;
991 bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish;
992 bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time;
993
Arianna Avanzinie1b23242017-04-12 18:23:20 +0200994 if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) ||
Arianna Avanzini36eca892017-04-12 18:23:16 +0200995 time_is_before_jiffies(bfqq->last_wr_start_finish +
Arianna Avanzinie1b23242017-04-12 18:23:20 +0200996 bfqq->wr_cur_max_time))) {
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200997 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
998 !bfq_bfqq_in_large_burst(bfqq) &&
999 time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt +
1000 bfq_wr_duration(bfqd))) {
1001 switch_back_to_interactive_wr(bfqq, bfqd);
1002 } else {
1003 bfqq->wr_coeff = 1;
1004 bfq_log_bfqq(bfqq->bfqd, bfqq,
1005 "resume state: switching off wr");
1006 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02001007 }
1008
1009 /* make sure weight will be updated, however we got here */
1010 bfqq->entity.prio_changed = 1;
Paolo Valente13c931b2017-06-27 12:30:47 -06001011
1012 if (likely(!busy))
1013 return;
1014
1015 if (old_wr_coeff == 1 && bfqq->wr_coeff > 1)
1016 bfqd->wr_busy_queues++;
1017 else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1)
1018 bfqd->wr_busy_queues--;
Arianna Avanzini36eca892017-04-12 18:23:16 +02001019}
1020
1021static int bfqq_process_refs(struct bfq_queue *bfqq)
1022{
1023 return bfqq->ref - bfqq->allocated - bfqq->entity.on_st;
1024}
1025
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001026/* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */
1027static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1028{
1029 struct bfq_queue *item;
1030 struct hlist_node *n;
1031
1032 hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node)
1033 hlist_del_init(&item->burst_list_node);
1034 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1035 bfqd->burst_size = 1;
1036 bfqd->burst_parent_entity = bfqq->entity.parent;
1037}
1038
1039/* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
1040static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1041{
1042 /* Increment burst size to take into account also bfqq */
1043 bfqd->burst_size++;
1044
1045 if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
1046 struct bfq_queue *pos, *bfqq_item;
1047 struct hlist_node *n;
1048
1049 /*
1050 * Enough queues have been activated shortly after each
1051 * other to consider this burst as large.
1052 */
1053 bfqd->large_burst = true;
1054
1055 /*
1056 * We can now mark all queues in the burst list as
1057 * belonging to a large burst.
1058 */
1059 hlist_for_each_entry(bfqq_item, &bfqd->burst_list,
1060 burst_list_node)
1061 bfq_mark_bfqq_in_large_burst(bfqq_item);
1062 bfq_mark_bfqq_in_large_burst(bfqq);
1063
1064 /*
1065 * From now on, and until the current burst finishes, any
1066 * new queue being activated shortly after the last queue
1067 * was inserted in the burst can be immediately marked as
1068 * belonging to a large burst. So the burst list is not
1069 * needed any more. Remove it.
1070 */
1071 hlist_for_each_entry_safe(pos, n, &bfqd->burst_list,
1072 burst_list_node)
1073 hlist_del_init(&pos->burst_list_node);
1074 } else /*
1075 * Burst not yet large: add bfqq to the burst list. Do
1076 * not increment the ref counter for bfqq, because bfqq
1077 * is removed from the burst list before freeing bfqq
1078 * in put_queue.
1079 */
1080 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1081}
1082
1083/*
1084 * If many queues belonging to the same group happen to be created
1085 * shortly after each other, then the processes associated with these
1086 * queues have typically a common goal. In particular, bursts of queue
1087 * creations are usually caused by services or applications that spawn
1088 * many parallel threads/processes. Examples are systemd during boot,
1089 * or git grep. To help these processes get their job done as soon as
1090 * possible, it is usually better to not grant either weight-raising
1091 * or device idling to their queues.
1092 *
1093 * In this comment we describe, firstly, the reasons why this fact
1094 * holds, and, secondly, the next function, which implements the main
1095 * steps needed to properly mark these queues so that they can then be
1096 * treated in a different way.
1097 *
1098 * The above services or applications benefit mostly from a high
1099 * throughput: the quicker the requests of the activated queues are
1100 * cumulatively served, the sooner the target job of these queues gets
1101 * completed. As a consequence, weight-raising any of these queues,
1102 * which also implies idling the device for it, is almost always
1103 * counterproductive. In most cases it just lowers throughput.
1104 *
1105 * On the other hand, a burst of queue creations may be caused also by
1106 * the start of an application that does not consist of a lot of
1107 * parallel I/O-bound threads. In fact, with a complex application,
1108 * several short processes may need to be executed to start-up the
1109 * application. In this respect, to start an application as quickly as
1110 * possible, the best thing to do is in any case to privilege the I/O
1111 * related to the application with respect to all other
1112 * I/O. Therefore, the best strategy to start as quickly as possible
1113 * an application that causes a burst of queue creations is to
1114 * weight-raise all the queues created during the burst. This is the
1115 * exact opposite of the best strategy for the other type of bursts.
1116 *
1117 * In the end, to take the best action for each of the two cases, the
1118 * two types of bursts need to be distinguished. Fortunately, this
1119 * seems relatively easy, by looking at the sizes of the bursts. In
1120 * particular, we found a threshold such that only bursts with a
1121 * larger size than that threshold are apparently caused by
1122 * services or commands such as systemd or git grep. For brevity,
1123 * hereafter we call just 'large' these bursts. BFQ *does not*
1124 * weight-raise queues whose creation occurs in a large burst. In
1125 * addition, for each of these queues BFQ performs or does not perform
1126 * idling depending on which choice boosts the throughput more. The
1127 * exact choice depends on the device and request pattern at
1128 * hand.
1129 *
1130 * Unfortunately, false positives may occur while an interactive task
1131 * is starting (e.g., an application is being started). The
1132 * consequence is that the queues associated with the task do not
1133 * enjoy weight raising as expected. Fortunately these false positives
1134 * are very rare. They typically occur if some service happens to
1135 * start doing I/O exactly when the interactive task starts.
1136 *
1137 * Turning back to the next function, it implements all the steps
1138 * needed to detect the occurrence of a large burst and to properly
1139 * mark all the queues belonging to it (so that they can then be
1140 * treated in a different way). This goal is achieved by maintaining a
1141 * "burst list" that holds, temporarily, the queues that belong to the
1142 * burst in progress. The list is then used to mark these queues as
1143 * belonging to a large burst if the burst does become large. The main
1144 * steps are the following.
1145 *
1146 * . when the very first queue is created, the queue is inserted into the
1147 * list (as it could be the first queue in a possible burst)
1148 *
1149 * . if the current burst has not yet become large, and a queue Q that does
1150 * not yet belong to the burst is activated shortly after the last time
1151 * at which a new queue entered the burst list, then the function appends
1152 * Q to the burst list
1153 *
1154 * . if, as a consequence of the previous step, the burst size reaches
1155 * the large-burst threshold, then
1156 *
1157 * . all the queues in the burst list are marked as belonging to a
1158 * large burst
1159 *
1160 * . the burst list is deleted; in fact, the burst list already served
1161 * its purpose (keeping temporarily track of the queues in a burst,
1162 * so as to be able to mark them as belonging to a large burst in the
1163 * previous sub-step), and now is not needed any more
1164 *
1165 * . the device enters a large-burst mode
1166 *
1167 * . if a queue Q that does not belong to the burst is created while
1168 * the device is in large-burst mode and shortly after the last time
1169 * at which a queue either entered the burst list or was marked as
1170 * belonging to the current large burst, then Q is immediately marked
1171 * as belonging to a large burst.
1172 *
1173 * . if a queue Q that does not belong to the burst is created a while
1174 * later, i.e., not shortly after, than the last time at which a queue
1175 * either entered the burst list or was marked as belonging to the
1176 * current large burst, then the current burst is deemed as finished and:
1177 *
1178 * . the large-burst mode is reset if set
1179 *
1180 * . the burst list is emptied
1181 *
1182 * . Q is inserted in the burst list, as Q may be the first queue
1183 * in a possible new burst (then the burst list contains just Q
1184 * after this step).
1185 */
1186static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1187{
1188 /*
1189 * If bfqq is already in the burst list or is part of a large
1190 * burst, or finally has just been split, then there is
1191 * nothing else to do.
1192 */
1193 if (!hlist_unhashed(&bfqq->burst_list_node) ||
1194 bfq_bfqq_in_large_burst(bfqq) ||
1195 time_is_after_eq_jiffies(bfqq->split_time +
1196 msecs_to_jiffies(10)))
1197 return;
1198
1199 /*
1200 * If bfqq's creation happens late enough, or bfqq belongs to
1201 * a different group than the burst group, then the current
1202 * burst is finished, and related data structures must be
1203 * reset.
1204 *
1205 * In this respect, consider the special case where bfqq is
1206 * the very first queue created after BFQ is selected for this
1207 * device. In this case, last_ins_in_burst and
1208 * burst_parent_entity are not yet significant when we get
1209 * here. But it is easy to verify that, whether or not the
1210 * following condition is true, bfqq will end up being
1211 * inserted into the burst list. In particular the list will
1212 * happen to contain only bfqq. And this is exactly what has
1213 * to happen, as bfqq may be the first queue of the first
1214 * burst.
1215 */
1216 if (time_is_before_jiffies(bfqd->last_ins_in_burst +
1217 bfqd->bfq_burst_interval) ||
1218 bfqq->entity.parent != bfqd->burst_parent_entity) {
1219 bfqd->large_burst = false;
1220 bfq_reset_burst_list(bfqd, bfqq);
1221 goto end;
1222 }
1223
1224 /*
1225 * If we get here, then bfqq is being activated shortly after the
1226 * last queue. So, if the current burst is also large, we can mark
1227 * bfqq as belonging to this large burst immediately.
1228 */
1229 if (bfqd->large_burst) {
1230 bfq_mark_bfqq_in_large_burst(bfqq);
1231 goto end;
1232 }
1233
1234 /*
1235 * If we get here, then a large-burst state has not yet been
1236 * reached, but bfqq is being activated shortly after the last
1237 * queue. Then we add bfqq to the burst.
1238 */
1239 bfq_add_to_burst(bfqd, bfqq);
1240end:
1241 /*
1242 * At this point, bfqq either has been added to the current
1243 * burst or has caused the current burst to terminate and a
1244 * possible new burst to start. In particular, in the second
1245 * case, bfqq has become the first queue in the possible new
1246 * burst. In both cases last_ins_in_burst needs to be moved
1247 * forward.
1248 */
1249 bfqd->last_ins_in_burst = jiffies;
1250}
1251
Paolo Valenteaee69d72017-04-19 08:29:02 -06001252static int bfq_bfqq_budget_left(struct bfq_queue *bfqq)
1253{
1254 struct bfq_entity *entity = &bfqq->entity;
1255
1256 return entity->budget - entity->service;
1257}
1258
1259/*
1260 * If enough samples have been computed, return the current max budget
1261 * stored in bfqd, which is dynamically updated according to the
1262 * estimated disk peak rate; otherwise return the default max budget
1263 */
1264static int bfq_max_budget(struct bfq_data *bfqd)
1265{
1266 if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1267 return bfq_default_max_budget;
1268 else
1269 return bfqd->bfq_max_budget;
1270}
1271
1272/*
1273 * Return min budget, which is a fraction of the current or default
1274 * max budget (trying with 1/32)
1275 */
1276static int bfq_min_budget(struct bfq_data *bfqd)
1277{
1278 if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1279 return bfq_default_max_budget / 32;
1280 else
1281 return bfqd->bfq_max_budget / 32;
1282}
1283
Paolo Valenteaee69d72017-04-19 08:29:02 -06001284/*
1285 * The next function, invoked after the input queue bfqq switches from
1286 * idle to busy, updates the budget of bfqq. The function also tells
1287 * whether the in-service queue should be expired, by returning
1288 * true. The purpose of expiring the in-service queue is to give bfqq
1289 * the chance to possibly preempt the in-service queue, and the reason
Paolo Valente44e44a12017-04-12 18:23:12 +02001290 * for preempting the in-service queue is to achieve one of the two
1291 * goals below.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001292 *
Paolo Valente44e44a12017-04-12 18:23:12 +02001293 * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has
1294 * expired because it has remained idle. In particular, bfqq may have
1295 * expired for one of the following two reasons:
Paolo Valenteaee69d72017-04-19 08:29:02 -06001296 *
1297 * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
1298 * and did not make it to issue a new request before its last
1299 * request was served;
1300 *
1301 * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
1302 * a new request before the expiration of the idling-time.
1303 *
1304 * Even if bfqq has expired for one of the above reasons, the process
1305 * associated with the queue may be however issuing requests greedily,
1306 * and thus be sensitive to the bandwidth it receives (bfqq may have
1307 * remained idle for other reasons: CPU high load, bfqq not enjoying
1308 * idling, I/O throttling somewhere in the path from the process to
1309 * the I/O scheduler, ...). But if, after every expiration for one of
1310 * the above two reasons, bfqq has to wait for the service of at least
1311 * one full budget of another queue before being served again, then
1312 * bfqq is likely to get a much lower bandwidth or resource time than
1313 * its reserved ones. To address this issue, two countermeasures need
1314 * to be taken.
1315 *
1316 * First, the budget and the timestamps of bfqq need to be updated in
1317 * a special way on bfqq reactivation: they need to be updated as if
1318 * bfqq did not remain idle and did not expire. In fact, if they are
1319 * computed as if bfqq expired and remained idle until reactivation,
1320 * then the process associated with bfqq is treated as if, instead of
1321 * being greedy, it stopped issuing requests when bfqq remained idle,
1322 * and restarts issuing requests only on this reactivation. In other
1323 * words, the scheduler does not help the process recover the "service
1324 * hole" between bfqq expiration and reactivation. As a consequence,
1325 * the process receives a lower bandwidth than its reserved one. In
1326 * contrast, to recover this hole, the budget must be updated as if
1327 * bfqq was not expired at all before this reactivation, i.e., it must
1328 * be set to the value of the remaining budget when bfqq was
1329 * expired. Along the same line, timestamps need to be assigned the
1330 * value they had the last time bfqq was selected for service, i.e.,
1331 * before last expiration. Thus timestamps need to be back-shifted
1332 * with respect to their normal computation (see [1] for more details
1333 * on this tricky aspect).
1334 *
1335 * Secondly, to allow the process to recover the hole, the in-service
1336 * queue must be expired too, to give bfqq the chance to preempt it
1337 * immediately. In fact, if bfqq has to wait for a full budget of the
1338 * in-service queue to be completed, then it may become impossible to
1339 * let the process recover the hole, even if the back-shifted
1340 * timestamps of bfqq are lower than those of the in-service queue. If
1341 * this happens for most or all of the holes, then the process may not
1342 * receive its reserved bandwidth. In this respect, it is worth noting
1343 * that, being the service of outstanding requests unpreemptible, a
1344 * little fraction of the holes may however be unrecoverable, thereby
1345 * causing a little loss of bandwidth.
1346 *
1347 * The last important point is detecting whether bfqq does need this
1348 * bandwidth recovery. In this respect, the next function deems the
1349 * process associated with bfqq greedy, and thus allows it to recover
1350 * the hole, if: 1) the process is waiting for the arrival of a new
1351 * request (which implies that bfqq expired for one of the above two
1352 * reasons), and 2) such a request has arrived soon. The first
1353 * condition is controlled through the flag non_blocking_wait_rq,
1354 * while the second through the flag arrived_in_time. If both
1355 * conditions hold, then the function computes the budget in the
1356 * above-described special way, and signals that the in-service queue
1357 * should be expired. Timestamp back-shifting is done later in
1358 * __bfq_activate_entity.
Paolo Valente44e44a12017-04-12 18:23:12 +02001359 *
1360 * 2. Reduce latency. Even if timestamps are not backshifted to let
1361 * the process associated with bfqq recover a service hole, bfqq may
1362 * however happen to have, after being (re)activated, a lower finish
1363 * timestamp than the in-service queue. That is, the next budget of
1364 * bfqq may have to be completed before the one of the in-service
1365 * queue. If this is the case, then preempting the in-service queue
1366 * allows this goal to be achieved, apart from the unpreemptible,
1367 * outstanding requests mentioned above.
1368 *
1369 * Unfortunately, regardless of which of the above two goals one wants
1370 * to achieve, service trees need first to be updated to know whether
1371 * the in-service queue must be preempted. To have service trees
1372 * correctly updated, the in-service queue must be expired and
1373 * rescheduled, and bfqq must be scheduled too. This is one of the
1374 * most costly operations (in future versions, the scheduling
1375 * mechanism may be re-designed in such a way to make it possible to
1376 * know whether preemption is needed without needing to update service
1377 * trees). In addition, queue preemptions almost always cause random
1378 * I/O, and thus loss of throughput. Because of these facts, the next
1379 * function adopts the following simple scheme to avoid both costly
1380 * operations and too frequent preemptions: it requests the expiration
1381 * of the in-service queue (unconditionally) only for queues that need
1382 * to recover a hole, or that either are weight-raised or deserve to
1383 * be weight-raised.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001384 */
1385static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd,
1386 struct bfq_queue *bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001387 bool arrived_in_time,
1388 bool wr_or_deserves_wr)
Paolo Valenteaee69d72017-04-19 08:29:02 -06001389{
1390 struct bfq_entity *entity = &bfqq->entity;
1391
Paolo Valente218cb892019-01-29 12:06:26 +01001392 /*
1393 * In the next compound condition, we check also whether there
1394 * is some budget left, because otherwise there is no point in
1395 * trying to go on serving bfqq with this same budget: bfqq
1396 * would be expired immediately after being selected for
1397 * service. This would only cause useless overhead.
1398 */
1399 if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time &&
1400 bfq_bfqq_budget_left(bfqq) > 0) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06001401 /*
1402 * We do not clear the flag non_blocking_wait_rq here, as
1403 * the latter is used in bfq_activate_bfqq to signal
1404 * that timestamps need to be back-shifted (and is
1405 * cleared right after).
1406 */
1407
1408 /*
1409 * In next assignment we rely on that either
1410 * entity->service or entity->budget are not updated
1411 * on expiration if bfqq is empty (see
1412 * __bfq_bfqq_recalc_budget). Thus both quantities
1413 * remain unchanged after such an expiration, and the
1414 * following statement therefore assigns to
1415 * entity->budget the remaining budget on such an
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001416 * expiration.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001417 */
1418 entity->budget = min_t(unsigned long,
1419 bfq_bfqq_budget_left(bfqq),
1420 bfqq->max_budget);
1421
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001422 /*
1423 * At this point, we have used entity->service to get
1424 * the budget left (needed for updating
1425 * entity->budget). Thus we finally can, and have to,
1426 * reset entity->service. The latter must be reset
1427 * because bfqq would otherwise be charged again for
1428 * the service it has received during its previous
1429 * service slot(s).
1430 */
1431 entity->service = 0;
1432
Paolo Valenteaee69d72017-04-19 08:29:02 -06001433 return true;
1434 }
1435
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001436 /*
1437 * We can finally complete expiration, by setting service to 0.
1438 */
1439 entity->service = 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001440 entity->budget = max_t(unsigned long, bfqq->max_budget,
1441 bfq_serv_to_charge(bfqq->next_rq, bfqq));
1442 bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
Paolo Valente44e44a12017-04-12 18:23:12 +02001443 return wr_or_deserves_wr;
1444}
1445
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001446/*
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001447 * Return the farthest past time instant according to jiffies
1448 * macros.
1449 */
1450static unsigned long bfq_smallest_from_now(void)
1451{
1452 return jiffies - MAX_JIFFY_OFFSET;
1453}
1454
Paolo Valente44e44a12017-04-12 18:23:12 +02001455static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd,
1456 struct bfq_queue *bfqq,
1457 unsigned int old_wr_coeff,
1458 bool wr_or_deserves_wr,
Paolo Valente77b7dce2017-04-12 18:23:13 +02001459 bool interactive,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001460 bool in_burst,
Paolo Valente77b7dce2017-04-12 18:23:13 +02001461 bool soft_rt)
Paolo Valente44e44a12017-04-12 18:23:12 +02001462{
1463 if (old_wr_coeff == 1 && wr_or_deserves_wr) {
1464 /* start a weight-raising period */
Paolo Valente77b7dce2017-04-12 18:23:13 +02001465 if (interactive) {
Paolo Valente8a8747d2018-01-13 12:05:18 +01001466 bfqq->service_from_wr = 0;
Paolo Valente77b7dce2017-04-12 18:23:13 +02001467 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1468 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1469 } else {
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001470 /*
1471 * No interactive weight raising in progress
1472 * here: assign minus infinity to
1473 * wr_start_at_switch_to_srt, to make sure
1474 * that, at the end of the soft-real-time
1475 * weight raising periods that is starting
1476 * now, no interactive weight-raising period
1477 * may be wrongly considered as still in
1478 * progress (and thus actually started by
1479 * mistake).
1480 */
1481 bfqq->wr_start_at_switch_to_srt =
1482 bfq_smallest_from_now();
Paolo Valente77b7dce2017-04-12 18:23:13 +02001483 bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1484 BFQ_SOFTRT_WEIGHT_FACTOR;
1485 bfqq->wr_cur_max_time =
1486 bfqd->bfq_wr_rt_max_time;
1487 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001488
1489 /*
1490 * If needed, further reduce budget to make sure it is
1491 * close to bfqq's backlog, so as to reduce the
1492 * scheduling-error component due to a too large
1493 * budget. Do not care about throughput consequences,
1494 * but only about latency. Finally, do not assign a
1495 * too small budget either, to avoid increasing
1496 * latency by causing too frequent expirations.
1497 */
1498 bfqq->entity.budget = min_t(unsigned long,
1499 bfqq->entity.budget,
1500 2 * bfq_min_budget(bfqd));
1501 } else if (old_wr_coeff > 1) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02001502 if (interactive) { /* update wr coeff and duration */
1503 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1504 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001505 } else if (in_burst)
1506 bfqq->wr_coeff = 1;
1507 else if (soft_rt) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02001508 /*
1509 * The application is now or still meeting the
1510 * requirements for being deemed soft rt. We
1511 * can then correctly and safely (re)charge
1512 * the weight-raising duration for the
1513 * application with the weight-raising
1514 * duration for soft rt applications.
1515 *
1516 * In particular, doing this recharge now, i.e.,
1517 * before the weight-raising period for the
1518 * application finishes, reduces the probability
1519 * of the following negative scenario:
1520 * 1) the weight of a soft rt application is
1521 * raised at startup (as for any newly
1522 * created application),
1523 * 2) since the application is not interactive,
1524 * at a certain time weight-raising is
1525 * stopped for the application,
1526 * 3) at that time the application happens to
1527 * still have pending requests, and hence
1528 * is destined to not have a chance to be
1529 * deemed soft rt before these requests are
1530 * completed (see the comments to the
1531 * function bfq_bfqq_softrt_next_start()
1532 * for details on soft rt detection),
1533 * 4) these pending requests experience a high
1534 * latency because the application is not
1535 * weight-raised while they are pending.
1536 */
1537 if (bfqq->wr_cur_max_time !=
1538 bfqd->bfq_wr_rt_max_time) {
1539 bfqq->wr_start_at_switch_to_srt =
1540 bfqq->last_wr_start_finish;
1541
1542 bfqq->wr_cur_max_time =
1543 bfqd->bfq_wr_rt_max_time;
1544 bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1545 BFQ_SOFTRT_WEIGHT_FACTOR;
1546 }
1547 bfqq->last_wr_start_finish = jiffies;
1548 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001549 }
1550}
1551
1552static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd,
1553 struct bfq_queue *bfqq)
1554{
1555 return bfqq->dispatched == 0 &&
1556 time_is_before_jiffies(
1557 bfqq->budget_timeout +
1558 bfqd->bfq_wr_min_idle_time);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001559}
1560
1561static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd,
1562 struct bfq_queue *bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001563 int old_wr_coeff,
1564 struct request *rq,
1565 bool *interactive)
Paolo Valenteaee69d72017-04-19 08:29:02 -06001566{
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001567 bool soft_rt, in_burst, wr_or_deserves_wr,
1568 bfqq_wants_to_preempt,
Paolo Valente44e44a12017-04-12 18:23:12 +02001569 idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq),
Paolo Valenteaee69d72017-04-19 08:29:02 -06001570 /*
1571 * See the comments on
1572 * bfq_bfqq_update_budg_for_activation for
1573 * details on the usage of the next variable.
1574 */
1575 arrived_in_time = ktime_get_ns() <=
1576 bfqq->ttime.last_end_request +
1577 bfqd->bfq_slice_idle * 3;
1578
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001579
Paolo Valenteaee69d72017-04-19 08:29:02 -06001580 /*
Paolo Valente44e44a12017-04-12 18:23:12 +02001581 * bfqq deserves to be weight-raised if:
1582 * - it is sync,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001583 * - it does not belong to a large burst,
Arianna Avanzini36eca892017-04-12 18:23:16 +02001584 * - it has been idle for enough time or is soft real-time,
1585 * - is linked to a bfq_io_cq (it is not shared in any sense).
Paolo Valente44e44a12017-04-12 18:23:12 +02001586 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001587 in_burst = bfq_bfqq_in_large_burst(bfqq);
Paolo Valente77b7dce2017-04-12 18:23:13 +02001588 soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001589 !in_burst &&
Davide Sapienzaf6c3ca02018-05-31 16:45:08 +02001590 time_is_before_jiffies(bfqq->soft_rt_next_start) &&
1591 bfqq->dispatched == 0;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001592 *interactive = !in_burst && idle_for_long_time;
Paolo Valente44e44a12017-04-12 18:23:12 +02001593 wr_or_deserves_wr = bfqd->low_latency &&
1594 (bfqq->wr_coeff > 1 ||
Arianna Avanzini36eca892017-04-12 18:23:16 +02001595 (bfq_bfqq_sync(bfqq) &&
1596 bfqq->bic && (*interactive || soft_rt)));
Paolo Valente44e44a12017-04-12 18:23:12 +02001597
1598 /*
1599 * Using the last flag, update budget and check whether bfqq
1600 * may want to preempt the in-service queue.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001601 */
1602 bfqq_wants_to_preempt =
1603 bfq_bfqq_update_budg_for_activation(bfqd, bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001604 arrived_in_time,
1605 wr_or_deserves_wr);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001606
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001607 /*
1608 * If bfqq happened to be activated in a burst, but has been
1609 * idle for much more than an interactive queue, then we
1610 * assume that, in the overall I/O initiated in the burst, the
1611 * I/O associated with bfqq is finished. So bfqq does not need
1612 * to be treated as a queue belonging to a burst
1613 * anymore. Accordingly, we reset bfqq's in_large_burst flag
1614 * if set, and remove bfqq from the burst list if it's
1615 * there. We do not decrement burst_size, because the fact
1616 * that bfqq does not need to belong to the burst list any
1617 * more does not invalidate the fact that bfqq was created in
1618 * a burst.
1619 */
1620 if (likely(!bfq_bfqq_just_created(bfqq)) &&
1621 idle_for_long_time &&
1622 time_is_before_jiffies(
1623 bfqq->budget_timeout +
1624 msecs_to_jiffies(10000))) {
1625 hlist_del_init(&bfqq->burst_list_node);
1626 bfq_clear_bfqq_in_large_burst(bfqq);
1627 }
1628
1629 bfq_clear_bfqq_just_created(bfqq);
1630
1631
Paolo Valenteaee69d72017-04-19 08:29:02 -06001632 if (!bfq_bfqq_IO_bound(bfqq)) {
1633 if (arrived_in_time) {
1634 bfqq->requests_within_timer++;
1635 if (bfqq->requests_within_timer >=
1636 bfqd->bfq_requests_within_timer)
1637 bfq_mark_bfqq_IO_bound(bfqq);
1638 } else
1639 bfqq->requests_within_timer = 0;
1640 }
1641
Paolo Valente44e44a12017-04-12 18:23:12 +02001642 if (bfqd->low_latency) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02001643 if (unlikely(time_is_after_jiffies(bfqq->split_time)))
1644 /* wraparound */
1645 bfqq->split_time =
1646 jiffies - bfqd->bfq_wr_min_idle_time - 1;
Paolo Valente44e44a12017-04-12 18:23:12 +02001647
Arianna Avanzini36eca892017-04-12 18:23:16 +02001648 if (time_is_before_jiffies(bfqq->split_time +
1649 bfqd->bfq_wr_min_idle_time)) {
1650 bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq,
1651 old_wr_coeff,
1652 wr_or_deserves_wr,
1653 *interactive,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001654 in_burst,
Arianna Avanzini36eca892017-04-12 18:23:16 +02001655 soft_rt);
1656
1657 if (old_wr_coeff != bfqq->wr_coeff)
1658 bfqq->entity.prio_changed = 1;
1659 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001660 }
1661
Paolo Valente77b7dce2017-04-12 18:23:13 +02001662 bfqq->last_idle_bklogged = jiffies;
1663 bfqq->service_from_backlogged = 0;
1664 bfq_clear_bfqq_softrt_update(bfqq);
1665
Paolo Valenteaee69d72017-04-19 08:29:02 -06001666 bfq_add_bfqq_busy(bfqd, bfqq);
1667
1668 /*
1669 * Expire in-service queue only if preemption may be needed
1670 * for guarantees. In this respect, the function
1671 * next_queue_may_preempt just checks a simple, necessary
1672 * condition, and not a sufficient condition based on
1673 * timestamps. In fact, for the latter condition to be
1674 * evaluated, timestamps would need first to be updated, and
1675 * this operation is quite costly (see the comments on the
1676 * function bfq_bfqq_update_budg_for_activation).
1677 */
1678 if (bfqd->in_service_queue && bfqq_wants_to_preempt &&
Paolo Valente77b7dce2017-04-12 18:23:13 +02001679 bfqd->in_service_queue->wr_coeff < bfqq->wr_coeff &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06001680 next_queue_may_preempt(bfqd))
1681 bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
1682 false, BFQQE_PREEMPTED);
1683}
1684
1685static void bfq_add_request(struct request *rq)
1686{
1687 struct bfq_queue *bfqq = RQ_BFQQ(rq);
1688 struct bfq_data *bfqd = bfqq->bfqd;
1689 struct request *next_rq, *prev;
Paolo Valente44e44a12017-04-12 18:23:12 +02001690 unsigned int old_wr_coeff = bfqq->wr_coeff;
1691 bool interactive = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001692
1693 bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
1694 bfqq->queued[rq_is_sync(rq)]++;
1695 bfqd->queued++;
1696
1697 elv_rb_add(&bfqq->sort_list, rq);
1698
1699 /*
1700 * Check if this request is a better next-serve candidate.
1701 */
1702 prev = bfqq->next_rq;
1703 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
1704 bfqq->next_rq = next_rq;
1705
Arianna Avanzini36eca892017-04-12 18:23:16 +02001706 /*
1707 * Adjust priority tree position, if next_rq changes.
1708 */
1709 if (prev != bfqq->next_rq)
1710 bfq_pos_tree_add_move(bfqd, bfqq);
1711
Paolo Valenteaee69d72017-04-19 08:29:02 -06001712 if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */
Paolo Valente44e44a12017-04-12 18:23:12 +02001713 bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff,
1714 rq, &interactive);
1715 else {
1716 if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
1717 time_is_before_jiffies(
1718 bfqq->last_wr_start_finish +
1719 bfqd->bfq_wr_min_inter_arr_async)) {
1720 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1721 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1722
Paolo Valentecfd69712017-04-12 18:23:15 +02001723 bfqd->wr_busy_queues++;
Paolo Valente44e44a12017-04-12 18:23:12 +02001724 bfqq->entity.prio_changed = 1;
1725 }
1726 if (prev != bfqq->next_rq)
1727 bfq_updated_next_req(bfqd, bfqq);
1728 }
1729
1730 /*
1731 * Assign jiffies to last_wr_start_finish in the following
1732 * cases:
1733 *
1734 * . if bfqq is not going to be weight-raised, because, for
1735 * non weight-raised queues, last_wr_start_finish stores the
1736 * arrival time of the last request; as of now, this piece
1737 * of information is used only for deciding whether to
1738 * weight-raise async queues
1739 *
1740 * . if bfqq is not weight-raised, because, if bfqq is now
1741 * switching to weight-raised, then last_wr_start_finish
1742 * stores the time when weight-raising starts
1743 *
1744 * . if bfqq is interactive, because, regardless of whether
1745 * bfqq is currently weight-raised, the weight-raising
1746 * period must start or restart (this case is considered
1747 * separately because it is not detected by the above
1748 * conditions, if bfqq is already weight-raised)
Paolo Valente77b7dce2017-04-12 18:23:13 +02001749 *
1750 * last_wr_start_finish has to be updated also if bfqq is soft
1751 * real-time, because the weight-raising period is constantly
1752 * restarted on idle-to-busy transitions for these queues, but
1753 * this is already done in bfq_bfqq_handle_idle_busy_switch if
1754 * needed.
Paolo Valente44e44a12017-04-12 18:23:12 +02001755 */
1756 if (bfqd->low_latency &&
1757 (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
1758 bfqq->last_wr_start_finish = jiffies;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001759}
1760
1761static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
1762 struct bio *bio,
1763 struct request_queue *q)
1764{
1765 struct bfq_queue *bfqq = bfqd->bio_bfqq;
1766
1767
1768 if (bfqq)
1769 return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio));
1770
1771 return NULL;
1772}
1773
Paolo Valenteab0e43e2017-04-12 18:23:10 +02001774static sector_t get_sdist(sector_t last_pos, struct request *rq)
1775{
1776 if (last_pos)
1777 return abs(blk_rq_pos(rq) - last_pos);
1778
1779 return 0;
1780}
1781
Paolo Valenteaee69d72017-04-19 08:29:02 -06001782#if 0 /* Still not clear if we can do without next two functions */
1783static void bfq_activate_request(struct request_queue *q, struct request *rq)
1784{
1785 struct bfq_data *bfqd = q->elevator->elevator_data;
1786
1787 bfqd->rq_in_driver++;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001788}
1789
1790static void bfq_deactivate_request(struct request_queue *q, struct request *rq)
1791{
1792 struct bfq_data *bfqd = q->elevator->elevator_data;
1793
1794 bfqd->rq_in_driver--;
1795}
1796#endif
1797
1798static void bfq_remove_request(struct request_queue *q,
1799 struct request *rq)
1800{
1801 struct bfq_queue *bfqq = RQ_BFQQ(rq);
1802 struct bfq_data *bfqd = bfqq->bfqd;
1803 const int sync = rq_is_sync(rq);
1804
1805 if (bfqq->next_rq == rq) {
1806 bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
1807 bfq_updated_next_req(bfqd, bfqq);
1808 }
1809
1810 if (rq->queuelist.prev != &rq->queuelist)
1811 list_del_init(&rq->queuelist);
1812 bfqq->queued[sync]--;
1813 bfqd->queued--;
1814 elv_rb_del(&bfqq->sort_list, rq);
1815
1816 elv_rqhash_del(q, rq);
1817 if (q->last_merge == rq)
1818 q->last_merge = NULL;
1819
1820 if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
1821 bfqq->next_rq = NULL;
1822
1823 if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001824 bfq_del_bfqq_busy(bfqd, bfqq, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001825 /*
1826 * bfqq emptied. In normal operation, when
1827 * bfqq is empty, bfqq->entity.service and
1828 * bfqq->entity.budget must contain,
1829 * respectively, the service received and the
1830 * budget used last time bfqq emptied. These
1831 * facts do not hold in this case, as at least
1832 * this last removal occurred while bfqq is
1833 * not in service. To avoid inconsistencies,
1834 * reset both bfqq->entity.service and
1835 * bfqq->entity.budget, if bfqq has still a
1836 * process that may issue I/O requests to it.
1837 */
1838 bfqq->entity.budget = bfqq->entity.service = 0;
1839 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02001840
1841 /*
1842 * Remove queue from request-position tree as it is empty.
1843 */
1844 if (bfqq->pos_root) {
1845 rb_erase(&bfqq->pos_node, bfqq->pos_root);
1846 bfqq->pos_root = NULL;
1847 }
Paolo Valente05e90282017-12-20 12:38:31 +01001848 } else {
1849 bfq_pos_tree_add_move(bfqd, bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001850 }
1851
1852 if (rq->cmd_flags & REQ_META)
1853 bfqq->meta_pending--;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001854
Paolo Valenteaee69d72017-04-19 08:29:02 -06001855}
1856
1857static bool bfq_bio_merge(struct blk_mq_hw_ctx *hctx, struct bio *bio)
1858{
1859 struct request_queue *q = hctx->queue;
1860 struct bfq_data *bfqd = q->elevator->elevator_data;
1861 struct request *free = NULL;
1862 /*
1863 * bfq_bic_lookup grabs the queue_lock: invoke it now and
1864 * store its return value for later use, to avoid nesting
1865 * queue_lock inside the bfqd->lock. We assume that the bic
1866 * returned by bfq_bic_lookup does not go away before
1867 * bfqd->lock is taken.
1868 */
1869 struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q);
1870 bool ret;
1871
1872 spin_lock_irq(&bfqd->lock);
1873
1874 if (bic)
1875 bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
1876 else
1877 bfqd->bio_bfqq = NULL;
1878 bfqd->bio_bic = bic;
1879
1880 ret = blk_mq_sched_try_merge(q, bio, &free);
1881
1882 if (free)
1883 blk_mq_free_request(free);
1884 spin_unlock_irq(&bfqd->lock);
1885
1886 return ret;
1887}
1888
1889static int bfq_request_merge(struct request_queue *q, struct request **req,
1890 struct bio *bio)
1891{
1892 struct bfq_data *bfqd = q->elevator->elevator_data;
1893 struct request *__rq;
1894
1895 __rq = bfq_find_rq_fmerge(bfqd, bio, q);
1896 if (__rq && elv_bio_merge_ok(__rq, bio)) {
1897 *req = __rq;
1898 return ELEVATOR_FRONT_MERGE;
1899 }
1900
1901 return ELEVATOR_NO_MERGE;
1902}
1903
Paolo Valente18e5a572018-05-04 19:17:01 +02001904static struct bfq_queue *bfq_init_rq(struct request *rq);
1905
Paolo Valenteaee69d72017-04-19 08:29:02 -06001906static void bfq_request_merged(struct request_queue *q, struct request *req,
1907 enum elv_merge type)
1908{
1909 if (type == ELEVATOR_FRONT_MERGE &&
1910 rb_prev(&req->rb_node) &&
1911 blk_rq_pos(req) <
1912 blk_rq_pos(container_of(rb_prev(&req->rb_node),
1913 struct request, rb_node))) {
Paolo Valente18e5a572018-05-04 19:17:01 +02001914 struct bfq_queue *bfqq = bfq_init_rq(req);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001915 struct bfq_data *bfqd = bfqq->bfqd;
1916 struct request *prev, *next_rq;
1917
1918 /* Reposition request in its sort_list */
1919 elv_rb_del(&bfqq->sort_list, req);
1920 elv_rb_add(&bfqq->sort_list, req);
1921
1922 /* Choose next request to be served for bfqq */
1923 prev = bfqq->next_rq;
1924 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
1925 bfqd->last_position);
1926 bfqq->next_rq = next_rq;
1927 /*
Arianna Avanzini36eca892017-04-12 18:23:16 +02001928 * If next_rq changes, update both the queue's budget to
1929 * fit the new request and the queue's position in its
1930 * rq_pos_tree.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001931 */
Arianna Avanzini36eca892017-04-12 18:23:16 +02001932 if (prev != bfqq->next_rq) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06001933 bfq_updated_next_req(bfqd, bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02001934 bfq_pos_tree_add_move(bfqd, bfqq);
1935 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06001936 }
1937}
1938
Paolo Valente8abfa4d2018-05-31 08:48:05 -06001939/*
1940 * This function is called to notify the scheduler that the requests
1941 * rq and 'next' have been merged, with 'next' going away. BFQ
1942 * exploits this hook to address the following issue: if 'next' has a
1943 * fifo_time lower that rq, then the fifo_time of rq must be set to
1944 * the value of 'next', to not forget the greater age of 'next'.
Paolo Valente8abfa4d2018-05-31 08:48:05 -06001945 *
1946 * NOTE: in this function we assume that rq is in a bfq_queue, basing
1947 * on that rq is picked from the hash table q->elevator->hash, which,
1948 * in its turn, is filled only with I/O requests present in
1949 * bfq_queues, while BFQ is in use for the request queue q. In fact,
1950 * the function that fills this hash table (elv_rqhash_add) is called
1951 * only by bfq_insert_request.
1952 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06001953static void bfq_requests_merged(struct request_queue *q, struct request *rq,
1954 struct request *next)
1955{
Paolo Valente18e5a572018-05-04 19:17:01 +02001956 struct bfq_queue *bfqq = bfq_init_rq(rq),
1957 *next_bfqq = bfq_init_rq(next);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001958
Paolo Valenteaee69d72017-04-19 08:29:02 -06001959 /*
1960 * If next and rq belong to the same bfq_queue and next is older
1961 * than rq, then reposition rq in the fifo (by substituting next
1962 * with rq). Otherwise, if next and rq belong to different
1963 * bfq_queues, never reposition rq: in fact, we would have to
1964 * reposition it with respect to next's position in its own fifo,
1965 * which would most certainly be too expensive with respect to
1966 * the benefits.
1967 */
1968 if (bfqq == next_bfqq &&
1969 !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
1970 next->fifo_time < rq->fifo_time) {
1971 list_del_init(&rq->queuelist);
1972 list_replace_init(&next->queuelist, &rq->queuelist);
1973 rq->fifo_time = next->fifo_time;
1974 }
1975
1976 if (bfqq->next_rq == next)
1977 bfqq->next_rq = rq;
1978
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001979 bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001980}
1981
Paolo Valente44e44a12017-04-12 18:23:12 +02001982/* Must be called with bfqq != NULL */
1983static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
1984{
Paolo Valentecfd69712017-04-12 18:23:15 +02001985 if (bfq_bfqq_busy(bfqq))
1986 bfqq->bfqd->wr_busy_queues--;
Paolo Valente44e44a12017-04-12 18:23:12 +02001987 bfqq->wr_coeff = 1;
1988 bfqq->wr_cur_max_time = 0;
Paolo Valente77b7dce2017-04-12 18:23:13 +02001989 bfqq->last_wr_start_finish = jiffies;
Paolo Valente44e44a12017-04-12 18:23:12 +02001990 /*
1991 * Trigger a weight change on the next invocation of
1992 * __bfq_entity_update_weight_prio.
1993 */
1994 bfqq->entity.prio_changed = 1;
1995}
1996
Paolo Valenteea25da42017-04-19 08:48:24 -06001997void bfq_end_wr_async_queues(struct bfq_data *bfqd,
1998 struct bfq_group *bfqg)
Paolo Valente44e44a12017-04-12 18:23:12 +02001999{
2000 int i, j;
2001
2002 for (i = 0; i < 2; i++)
2003 for (j = 0; j < IOPRIO_BE_NR; j++)
2004 if (bfqg->async_bfqq[i][j])
2005 bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
2006 if (bfqg->async_idle_bfqq)
2007 bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
2008}
2009
2010static void bfq_end_wr(struct bfq_data *bfqd)
2011{
2012 struct bfq_queue *bfqq;
2013
2014 spin_lock_irq(&bfqd->lock);
2015
2016 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
2017 bfq_bfqq_end_wr(bfqq);
2018 list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
2019 bfq_bfqq_end_wr(bfqq);
2020 bfq_end_wr_async(bfqd);
2021
2022 spin_unlock_irq(&bfqd->lock);
2023}
2024
Arianna Avanzini36eca892017-04-12 18:23:16 +02002025static sector_t bfq_io_struct_pos(void *io_struct, bool request)
2026{
2027 if (request)
2028 return blk_rq_pos(io_struct);
2029 else
2030 return ((struct bio *)io_struct)->bi_iter.bi_sector;
2031}
2032
2033static int bfq_rq_close_to_sector(void *io_struct, bool request,
2034 sector_t sector)
2035{
2036 return abs(bfq_io_struct_pos(io_struct, request) - sector) <=
2037 BFQQ_CLOSE_THR;
2038}
2039
2040static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd,
2041 struct bfq_queue *bfqq,
2042 sector_t sector)
2043{
2044 struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
2045 struct rb_node *parent, *node;
2046 struct bfq_queue *__bfqq;
2047
2048 if (RB_EMPTY_ROOT(root))
2049 return NULL;
2050
2051 /*
2052 * First, if we find a request starting at the end of the last
2053 * request, choose it.
2054 */
2055 __bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
2056 if (__bfqq)
2057 return __bfqq;
2058
2059 /*
2060 * If the exact sector wasn't found, the parent of the NULL leaf
2061 * will contain the closest sector (rq_pos_tree sorted by
2062 * next_request position).
2063 */
2064 __bfqq = rb_entry(parent, struct bfq_queue, pos_node);
2065 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2066 return __bfqq;
2067
2068 if (blk_rq_pos(__bfqq->next_rq) < sector)
2069 node = rb_next(&__bfqq->pos_node);
2070 else
2071 node = rb_prev(&__bfqq->pos_node);
2072 if (!node)
2073 return NULL;
2074
2075 __bfqq = rb_entry(node, struct bfq_queue, pos_node);
2076 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2077 return __bfqq;
2078
2079 return NULL;
2080}
2081
2082static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd,
2083 struct bfq_queue *cur_bfqq,
2084 sector_t sector)
2085{
2086 struct bfq_queue *bfqq;
2087
2088 /*
2089 * We shall notice if some of the queues are cooperating,
2090 * e.g., working closely on the same area of the device. In
2091 * that case, we can group them together and: 1) don't waste
2092 * time idling, and 2) serve the union of their requests in
2093 * the best possible order for throughput.
2094 */
2095 bfqq = bfqq_find_close(bfqd, cur_bfqq, sector);
2096 if (!bfqq || bfqq == cur_bfqq)
2097 return NULL;
2098
2099 return bfqq;
2100}
2101
2102static struct bfq_queue *
2103bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2104{
2105 int process_refs, new_process_refs;
2106 struct bfq_queue *__bfqq;
2107
2108 /*
2109 * If there are no process references on the new_bfqq, then it is
2110 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
2111 * may have dropped their last reference (not just their last process
2112 * reference).
2113 */
2114 if (!bfqq_process_refs(new_bfqq))
2115 return NULL;
2116
2117 /* Avoid a circular list and skip interim queue merges. */
2118 while ((__bfqq = new_bfqq->new_bfqq)) {
2119 if (__bfqq == bfqq)
2120 return NULL;
2121 new_bfqq = __bfqq;
2122 }
2123
2124 process_refs = bfqq_process_refs(bfqq);
2125 new_process_refs = bfqq_process_refs(new_bfqq);
2126 /*
2127 * If the process for the bfqq has gone away, there is no
2128 * sense in merging the queues.
2129 */
2130 if (process_refs == 0 || new_process_refs == 0)
2131 return NULL;
2132
2133 bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
2134 new_bfqq->pid);
2135
2136 /*
2137 * Merging is just a redirection: the requests of the process
2138 * owning one of the two queues are redirected to the other queue.
2139 * The latter queue, in its turn, is set as shared if this is the
2140 * first time that the requests of some process are redirected to
2141 * it.
2142 *
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02002143 * We redirect bfqq to new_bfqq and not the opposite, because
2144 * we are in the context of the process owning bfqq, thus we
2145 * have the io_cq of this process. So we can immediately
2146 * configure this io_cq to redirect the requests of the
2147 * process to new_bfqq. In contrast, the io_cq of new_bfqq is
2148 * not available any more (new_bfqq->bic == NULL).
Arianna Avanzini36eca892017-04-12 18:23:16 +02002149 *
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02002150 * Anyway, even in case new_bfqq coincides with the in-service
2151 * queue, redirecting requests the in-service queue is the
2152 * best option, as we feed the in-service queue with new
2153 * requests close to the last request served and, by doing so,
2154 * are likely to increase the throughput.
Arianna Avanzini36eca892017-04-12 18:23:16 +02002155 */
2156 bfqq->new_bfqq = new_bfqq;
2157 new_bfqq->ref += process_refs;
2158 return new_bfqq;
2159}
2160
2161static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq,
2162 struct bfq_queue *new_bfqq)
2163{
Paolo Valente7b8fa3b2017-12-20 12:38:33 +01002164 if (bfq_too_late_for_merging(new_bfqq))
2165 return false;
2166
Arianna Avanzini36eca892017-04-12 18:23:16 +02002167 if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) ||
2168 (bfqq->ioprio_class != new_bfqq->ioprio_class))
2169 return false;
2170
2171 /*
2172 * If either of the queues has already been detected as seeky,
2173 * then merging it with the other queue is unlikely to lead to
2174 * sequential I/O.
2175 */
2176 if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq))
2177 return false;
2178
2179 /*
2180 * Interleaved I/O is known to be done by (some) applications
2181 * only for reads, so it does not make sense to merge async
2182 * queues.
2183 */
2184 if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq))
2185 return false;
2186
2187 return true;
2188}
2189
2190/*
Arianna Avanzini36eca892017-04-12 18:23:16 +02002191 * Attempt to schedule a merge of bfqq with the currently in-service
2192 * queue or with a close queue among the scheduled queues. Return
2193 * NULL if no merge was scheduled, a pointer to the shared bfq_queue
2194 * structure otherwise.
2195 *
2196 * The OOM queue is not allowed to participate to cooperation: in fact, since
2197 * the requests temporarily redirected to the OOM queue could be redirected
2198 * again to dedicated queues at any time, the state needed to correctly
2199 * handle merging with the OOM queue would be quite complex and expensive
2200 * to maintain. Besides, in such a critical condition as an out of memory,
2201 * the benefits of queue merging may be little relevant, or even negligible.
2202 *
Arianna Avanzini36eca892017-04-12 18:23:16 +02002203 * WARNING: queue merging may impair fairness among non-weight raised
2204 * queues, for at least two reasons: 1) the original weight of a
2205 * merged queue may change during the merged state, 2) even being the
2206 * weight the same, a merged queue may be bloated with many more
2207 * requests than the ones produced by its originally-associated
2208 * process.
2209 */
2210static struct bfq_queue *
2211bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
2212 void *io_struct, bool request)
2213{
2214 struct bfq_queue *in_service_bfqq, *new_bfqq;
2215
Paolo Valente7b8fa3b2017-12-20 12:38:33 +01002216 /*
2217 * Prevent bfqq from being merged if it has been created too
2218 * long ago. The idea is that true cooperating processes, and
2219 * thus their associated bfq_queues, are supposed to be
2220 * created shortly after each other. This is the case, e.g.,
2221 * for KVM/QEMU and dump I/O threads. Basing on this
2222 * assumption, the following filtering greatly reduces the
2223 * probability that two non-cooperating processes, which just
2224 * happen to do close I/O for some short time interval, have
2225 * their queues merged by mistake.
2226 */
2227 if (bfq_too_late_for_merging(bfqq))
2228 return NULL;
2229
Arianna Avanzini36eca892017-04-12 18:23:16 +02002230 if (bfqq->new_bfqq)
2231 return bfqq->new_bfqq;
2232
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002233 if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq))
Arianna Avanzini36eca892017-04-12 18:23:16 +02002234 return NULL;
2235
2236 /* If there is only one backlogged queue, don't search. */
Paolo Valente73d58112019-01-29 12:06:29 +01002237 if (bfq_tot_busy_queues(bfqd) == 1)
Arianna Avanzini36eca892017-04-12 18:23:16 +02002238 return NULL;
2239
2240 in_service_bfqq = bfqd->in_service_queue;
2241
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002242 if (in_service_bfqq && in_service_bfqq != bfqq &&
2243 likely(in_service_bfqq != &bfqd->oom_bfqq) &&
2244 bfq_rq_close_to_sector(io_struct, request, bfqd->last_position) &&
Arianna Avanzini36eca892017-04-12 18:23:16 +02002245 bfqq->entity.parent == in_service_bfqq->entity.parent &&
2246 bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) {
2247 new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
2248 if (new_bfqq)
2249 return new_bfqq;
2250 }
2251 /*
2252 * Check whether there is a cooperator among currently scheduled
2253 * queues. The only thing we need is that the bio/request is not
2254 * NULL, as we need it to establish whether a cooperator exists.
2255 */
Arianna Avanzini36eca892017-04-12 18:23:16 +02002256 new_bfqq = bfq_find_close_cooperator(bfqd, bfqq,
2257 bfq_io_struct_pos(io_struct, request));
2258
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002259 if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) &&
Arianna Avanzini36eca892017-04-12 18:23:16 +02002260 bfq_may_be_close_cooperator(bfqq, new_bfqq))
2261 return bfq_setup_merge(bfqq, new_bfqq);
2262
2263 return NULL;
2264}
2265
2266static void bfq_bfqq_save_state(struct bfq_queue *bfqq)
2267{
2268 struct bfq_io_cq *bic = bfqq->bic;
2269
2270 /*
2271 * If !bfqq->bic, the queue is already shared or its requests
2272 * have already been redirected to a shared queue; both idle window
2273 * and weight raising state have already been saved. Do nothing.
2274 */
2275 if (!bic)
2276 return;
2277
2278 bic->saved_ttime = bfqq->ttime;
Paolo Valented5be3fe2017-08-04 07:35:10 +02002279 bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002280 bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02002281 bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
2282 bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
Paolo Valente894df932017-09-21 11:04:02 +02002283 if (unlikely(bfq_bfqq_just_created(bfqq) &&
Angelo Ruocco1be6e8a2017-12-20 12:38:32 +01002284 !bfq_bfqq_in_large_burst(bfqq) &&
2285 bfqq->bfqd->low_latency)) {
Paolo Valente894df932017-09-21 11:04:02 +02002286 /*
2287 * bfqq being merged right after being created: bfqq
2288 * would have deserved interactive weight raising, but
2289 * did not make it to be set in a weight-raised state,
2290 * because of this early merge. Store directly the
2291 * weight-raising state that would have been assigned
2292 * to bfqq, so that to avoid that bfqq unjustly fails
2293 * to enjoy weight raising if split soon.
2294 */
2295 bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff;
2296 bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd);
2297 bic->saved_last_wr_start_finish = jiffies;
2298 } else {
2299 bic->saved_wr_coeff = bfqq->wr_coeff;
2300 bic->saved_wr_start_at_switch_to_srt =
2301 bfqq->wr_start_at_switch_to_srt;
2302 bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish;
2303 bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time;
2304 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02002305}
2306
Arianna Avanzini36eca892017-04-12 18:23:16 +02002307static void
2308bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
2309 struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2310{
2311 bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
2312 (unsigned long)new_bfqq->pid);
2313 /* Save weight raising and idle window of the merged queues */
2314 bfq_bfqq_save_state(bfqq);
2315 bfq_bfqq_save_state(new_bfqq);
2316 if (bfq_bfqq_IO_bound(bfqq))
2317 bfq_mark_bfqq_IO_bound(new_bfqq);
2318 bfq_clear_bfqq_IO_bound(bfqq);
2319
2320 /*
2321 * If bfqq is weight-raised, then let new_bfqq inherit
2322 * weight-raising. To reduce false positives, neglect the case
2323 * where bfqq has just been created, but has not yet made it
2324 * to be weight-raised (which may happen because EQM may merge
2325 * bfqq even before bfq_add_request is executed for the first
Arianna Avanzinie1b23242017-04-12 18:23:20 +02002326 * time for bfqq). Handling this case would however be very
2327 * easy, thanks to the flag just_created.
Arianna Avanzini36eca892017-04-12 18:23:16 +02002328 */
2329 if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) {
2330 new_bfqq->wr_coeff = bfqq->wr_coeff;
2331 new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time;
2332 new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish;
2333 new_bfqq->wr_start_at_switch_to_srt =
2334 bfqq->wr_start_at_switch_to_srt;
2335 if (bfq_bfqq_busy(new_bfqq))
2336 bfqd->wr_busy_queues++;
2337 new_bfqq->entity.prio_changed = 1;
2338 }
2339
2340 if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */
2341 bfqq->wr_coeff = 1;
2342 bfqq->entity.prio_changed = 1;
2343 if (bfq_bfqq_busy(bfqq))
2344 bfqd->wr_busy_queues--;
2345 }
2346
2347 bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d",
2348 bfqd->wr_busy_queues);
2349
2350 /*
Arianna Avanzini36eca892017-04-12 18:23:16 +02002351 * Merge queues (that is, let bic redirect its requests to new_bfqq)
2352 */
2353 bic_set_bfqq(bic, new_bfqq, 1);
2354 bfq_mark_bfqq_coop(new_bfqq);
2355 /*
2356 * new_bfqq now belongs to at least two bics (it is a shared queue):
2357 * set new_bfqq->bic to NULL. bfqq either:
2358 * - does not belong to any bic any more, and hence bfqq->bic must
2359 * be set to NULL, or
2360 * - is a queue whose owning bics have already been redirected to a
2361 * different queue, hence the queue is destined to not belong to
2362 * any bic soon and bfqq->bic is already NULL (therefore the next
2363 * assignment causes no harm).
2364 */
2365 new_bfqq->bic = NULL;
2366 bfqq->bic = NULL;
2367 /* release process reference to bfqq */
2368 bfq_put_queue(bfqq);
2369}
2370
Paolo Valenteaee69d72017-04-19 08:29:02 -06002371static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2372 struct bio *bio)
2373{
2374 struct bfq_data *bfqd = q->elevator->elevator_data;
2375 bool is_sync = op_is_sync(bio->bi_opf);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002376 struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06002377
2378 /*
2379 * Disallow merge of a sync bio into an async request.
2380 */
2381 if (is_sync && !rq_is_sync(rq))
2382 return false;
2383
2384 /*
2385 * Lookup the bfqq that this bio will be queued with. Allow
2386 * merge only if rq is queued there.
2387 */
2388 if (!bfqq)
2389 return false;
2390
Arianna Avanzini36eca892017-04-12 18:23:16 +02002391 /*
2392 * We take advantage of this function to perform an early merge
2393 * of the queues of possible cooperating processes.
2394 */
2395 new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
2396 if (new_bfqq) {
2397 /*
2398 * bic still points to bfqq, then it has not yet been
2399 * redirected to some other bfq_queue, and a queue
2400 * merge beween bfqq and new_bfqq can be safely
2401 * fulfillled, i.e., bic can be redirected to new_bfqq
2402 * and bfqq can be put.
2403 */
2404 bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq,
2405 new_bfqq);
2406 /*
2407 * If we get here, bio will be queued into new_queue,
2408 * so use new_bfqq to decide whether bio and rq can be
2409 * merged.
2410 */
2411 bfqq = new_bfqq;
2412
2413 /*
2414 * Change also bqfd->bio_bfqq, as
2415 * bfqd->bio_bic now points to new_bfqq, and
2416 * this function may be invoked again (and then may
2417 * use again bqfd->bio_bfqq).
2418 */
2419 bfqd->bio_bfqq = bfqq;
2420 }
2421
Paolo Valenteaee69d72017-04-19 08:29:02 -06002422 return bfqq == RQ_BFQQ(rq);
2423}
2424
Paolo Valente44e44a12017-04-12 18:23:12 +02002425/*
2426 * Set the maximum time for the in-service queue to consume its
2427 * budget. This prevents seeky processes from lowering the throughput.
2428 * In practice, a time-slice service scheme is used with seeky
2429 * processes.
2430 */
2431static void bfq_set_budget_timeout(struct bfq_data *bfqd,
2432 struct bfq_queue *bfqq)
2433{
Paolo Valente77b7dce2017-04-12 18:23:13 +02002434 unsigned int timeout_coeff;
2435
2436 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
2437 timeout_coeff = 1;
2438 else
2439 timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
2440
Paolo Valente44e44a12017-04-12 18:23:12 +02002441 bfqd->last_budget_start = ktime_get();
2442
2443 bfqq->budget_timeout = jiffies +
Paolo Valente77b7dce2017-04-12 18:23:13 +02002444 bfqd->bfq_timeout * timeout_coeff;
Paolo Valente44e44a12017-04-12 18:23:12 +02002445}
2446
Paolo Valenteaee69d72017-04-19 08:29:02 -06002447static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
2448 struct bfq_queue *bfqq)
2449{
2450 if (bfqq) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06002451 bfq_clear_bfqq_fifo_expire(bfqq);
2452
2453 bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8;
2454
Paolo Valente77b7dce2017-04-12 18:23:13 +02002455 if (time_is_before_jiffies(bfqq->last_wr_start_finish) &&
2456 bfqq->wr_coeff > 1 &&
2457 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
2458 time_is_before_jiffies(bfqq->budget_timeout)) {
2459 /*
2460 * For soft real-time queues, move the start
2461 * of the weight-raising period forward by the
2462 * time the queue has not received any
2463 * service. Otherwise, a relatively long
2464 * service delay is likely to cause the
2465 * weight-raising period of the queue to end,
2466 * because of the short duration of the
2467 * weight-raising period of a soft real-time
2468 * queue. It is worth noting that this move
2469 * is not so dangerous for the other queues,
2470 * because soft real-time queues are not
2471 * greedy.
2472 *
2473 * To not add a further variable, we use the
2474 * overloaded field budget_timeout to
2475 * determine for how long the queue has not
2476 * received service, i.e., how much time has
2477 * elapsed since the queue expired. However,
2478 * this is a little imprecise, because
2479 * budget_timeout is set to jiffies if bfqq
2480 * not only expires, but also remains with no
2481 * request.
2482 */
2483 if (time_after(bfqq->budget_timeout,
2484 bfqq->last_wr_start_finish))
2485 bfqq->last_wr_start_finish +=
2486 jiffies - bfqq->budget_timeout;
2487 else
2488 bfqq->last_wr_start_finish = jiffies;
2489 }
2490
Paolo Valente44e44a12017-04-12 18:23:12 +02002491 bfq_set_budget_timeout(bfqd, bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002492 bfq_log_bfqq(bfqd, bfqq,
2493 "set_in_service_queue, cur-budget = %d",
2494 bfqq->entity.budget);
2495 }
2496
2497 bfqd->in_service_queue = bfqq;
2498}
2499
2500/*
2501 * Get and set a new queue for service.
2502 */
2503static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
2504{
2505 struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
2506
2507 __bfq_set_in_service_queue(bfqd, bfqq);
2508 return bfqq;
2509}
2510
Paolo Valenteaee69d72017-04-19 08:29:02 -06002511static void bfq_arm_slice_timer(struct bfq_data *bfqd)
2512{
2513 struct bfq_queue *bfqq = bfqd->in_service_queue;
Paolo Valenteaee69d72017-04-19 08:29:02 -06002514 u32 sl;
2515
Paolo Valenteaee69d72017-04-19 08:29:02 -06002516 bfq_mark_bfqq_wait_request(bfqq);
2517
2518 /*
2519 * We don't want to idle for seeks, but we do want to allow
2520 * fair distribution of slice time for a process doing back-to-back
2521 * seeks. So allow a little bit of time for him to submit a new rq.
2522 */
2523 sl = bfqd->bfq_slice_idle;
2524 /*
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02002525 * Unless the queue is being weight-raised or the scenario is
2526 * asymmetric, grant only minimum idle time if the queue
2527 * is seeky. A long idling is preserved for a weight-raised
2528 * queue, or, more in general, in an asymmetric scenario,
2529 * because a long idling is needed for guaranteeing to a queue
2530 * its reserved share of the throughput (in particular, it is
2531 * needed if the queue has a higher weight than some other
2532 * queue).
Paolo Valenteaee69d72017-04-19 08:29:02 -06002533 */
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02002534 if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
2535 bfq_symmetric_scenario(bfqd))
Paolo Valenteaee69d72017-04-19 08:29:02 -06002536 sl = min_t(u64, sl, BFQ_MIN_TT);
2537
2538 bfqd->last_idling_start = ktime_get();
2539 hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl),
2540 HRTIMER_MODE_REL);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002541 bfqg_stats_set_start_idle_time(bfqq_group(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06002542}
2543
2544/*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002545 * In autotuning mode, max_budget is dynamically recomputed as the
2546 * amount of sectors transferred in timeout at the estimated peak
2547 * rate. This enables BFQ to utilize a full timeslice with a full
2548 * budget, even if the in-service queue is served at peak rate. And
2549 * this maximises throughput with sequential workloads.
2550 */
2551static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd)
2552{
2553 return (u64)bfqd->peak_rate * USEC_PER_MSEC *
2554 jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT;
2555}
2556
Paolo Valente44e44a12017-04-12 18:23:12 +02002557/*
2558 * Update parameters related to throughput and responsiveness, as a
2559 * function of the estimated peak rate. See comments on
Paolo Valentee24f1c22018-05-31 16:45:06 +02002560 * bfq_calc_max_budget(), and on the ref_wr_duration array.
Paolo Valente44e44a12017-04-12 18:23:12 +02002561 */
2562static void update_thr_responsiveness_params(struct bfq_data *bfqd)
2563{
Paolo Valentee24f1c22018-05-31 16:45:06 +02002564 if (bfqd->bfq_user_max_budget == 0) {
Paolo Valente44e44a12017-04-12 18:23:12 +02002565 bfqd->bfq_max_budget =
2566 bfq_calc_max_budget(bfqd);
Paolo Valentee24f1c22018-05-31 16:45:06 +02002567 bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget);
Paolo Valente44e44a12017-04-12 18:23:12 +02002568 }
Paolo Valente44e44a12017-04-12 18:23:12 +02002569}
2570
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002571static void bfq_reset_rate_computation(struct bfq_data *bfqd,
2572 struct request *rq)
2573{
2574 if (rq != NULL) { /* new rq dispatch now, reset accordingly */
2575 bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns();
2576 bfqd->peak_rate_samples = 1;
2577 bfqd->sequential_samples = 0;
2578 bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size =
2579 blk_rq_sectors(rq);
2580 } else /* no new rq dispatched, just reset the number of samples */
2581 bfqd->peak_rate_samples = 0; /* full re-init on next disp. */
2582
2583 bfq_log(bfqd,
2584 "reset_rate_computation at end, sample %u/%u tot_sects %llu",
2585 bfqd->peak_rate_samples, bfqd->sequential_samples,
2586 bfqd->tot_sectors_dispatched);
2587}
2588
2589static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq)
2590{
2591 u32 rate, weight, divisor;
2592
2593 /*
2594 * For the convergence property to hold (see comments on
2595 * bfq_update_peak_rate()) and for the assessment to be
2596 * reliable, a minimum number of samples must be present, and
2597 * a minimum amount of time must have elapsed. If not so, do
2598 * not compute new rate. Just reset parameters, to get ready
2599 * for a new evaluation attempt.
2600 */
2601 if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES ||
2602 bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL)
2603 goto reset_computation;
2604
2605 /*
2606 * If a new request completion has occurred after last
2607 * dispatch, then, to approximate the rate at which requests
2608 * have been served by the device, it is more precise to
2609 * extend the observation interval to the last completion.
2610 */
2611 bfqd->delta_from_first =
2612 max_t(u64, bfqd->delta_from_first,
2613 bfqd->last_completion - bfqd->first_dispatch);
2614
2615 /*
2616 * Rate computed in sects/usec, and not sects/nsec, for
2617 * precision issues.
2618 */
2619 rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT,
2620 div_u64(bfqd->delta_from_first, NSEC_PER_USEC));
2621
2622 /*
2623 * Peak rate not updated if:
2624 * - the percentage of sequential dispatches is below 3/4 of the
2625 * total, and rate is below the current estimated peak rate
2626 * - rate is unreasonably high (> 20M sectors/sec)
2627 */
2628 if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 &&
2629 rate <= bfqd->peak_rate) ||
2630 rate > 20<<BFQ_RATE_SHIFT)
2631 goto reset_computation;
2632
2633 /*
2634 * We have to update the peak rate, at last! To this purpose,
2635 * we use a low-pass filter. We compute the smoothing constant
2636 * of the filter as a function of the 'weight' of the new
2637 * measured rate.
2638 *
2639 * As can be seen in next formulas, we define this weight as a
2640 * quantity proportional to how sequential the workload is,
2641 * and to how long the observation time interval is.
2642 *
2643 * The weight runs from 0 to 8. The maximum value of the
2644 * weight, 8, yields the minimum value for the smoothing
2645 * constant. At this minimum value for the smoothing constant,
2646 * the measured rate contributes for half of the next value of
2647 * the estimated peak rate.
2648 *
2649 * So, the first step is to compute the weight as a function
2650 * of how sequential the workload is. Note that the weight
2651 * cannot reach 9, because bfqd->sequential_samples cannot
2652 * become equal to bfqd->peak_rate_samples, which, in its
2653 * turn, holds true because bfqd->sequential_samples is not
2654 * incremented for the first sample.
2655 */
2656 weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples;
2657
2658 /*
2659 * Second step: further refine the weight as a function of the
2660 * duration of the observation interval.
2661 */
2662 weight = min_t(u32, 8,
2663 div_u64(weight * bfqd->delta_from_first,
2664 BFQ_RATE_REF_INTERVAL));
2665
2666 /*
2667 * Divisor ranging from 10, for minimum weight, to 2, for
2668 * maximum weight.
2669 */
2670 divisor = 10 - weight;
2671
2672 /*
2673 * Finally, update peak rate:
2674 *
2675 * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor
2676 */
2677 bfqd->peak_rate *= divisor-1;
2678 bfqd->peak_rate /= divisor;
2679 rate /= divisor; /* smoothing constant alpha = 1/divisor */
2680
2681 bfqd->peak_rate += rate;
Paolo Valentebc56e2c2018-03-26 16:06:24 +02002682
2683 /*
2684 * For a very slow device, bfqd->peak_rate can reach 0 (see
2685 * the minimum representable values reported in the comments
2686 * on BFQ_RATE_SHIFT). Push to 1 if this happens, to avoid
2687 * divisions by zero where bfqd->peak_rate is used as a
2688 * divisor.
2689 */
2690 bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate);
2691
Paolo Valente44e44a12017-04-12 18:23:12 +02002692 update_thr_responsiveness_params(bfqd);
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002693
2694reset_computation:
2695 bfq_reset_rate_computation(bfqd, rq);
2696}
2697
2698/*
2699 * Update the read/write peak rate (the main quantity used for
2700 * auto-tuning, see update_thr_responsiveness_params()).
2701 *
2702 * It is not trivial to estimate the peak rate (correctly): because of
2703 * the presence of sw and hw queues between the scheduler and the
2704 * device components that finally serve I/O requests, it is hard to
2705 * say exactly when a given dispatched request is served inside the
2706 * device, and for how long. As a consequence, it is hard to know
2707 * precisely at what rate a given set of requests is actually served
2708 * by the device.
2709 *
2710 * On the opposite end, the dispatch time of any request is trivially
2711 * available, and, from this piece of information, the "dispatch rate"
2712 * of requests can be immediately computed. So, the idea in the next
2713 * function is to use what is known, namely request dispatch times
2714 * (plus, when useful, request completion times), to estimate what is
2715 * unknown, namely in-device request service rate.
2716 *
2717 * The main issue is that, because of the above facts, the rate at
2718 * which a certain set of requests is dispatched over a certain time
2719 * interval can vary greatly with respect to the rate at which the
2720 * same requests are then served. But, since the size of any
2721 * intermediate queue is limited, and the service scheme is lossless
2722 * (no request is silently dropped), the following obvious convergence
2723 * property holds: the number of requests dispatched MUST become
2724 * closer and closer to the number of requests completed as the
2725 * observation interval grows. This is the key property used in
2726 * the next function to estimate the peak service rate as a function
2727 * of the observed dispatch rate. The function assumes to be invoked
2728 * on every request dispatch.
2729 */
2730static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq)
2731{
2732 u64 now_ns = ktime_get_ns();
2733
2734 if (bfqd->peak_rate_samples == 0) { /* first dispatch */
2735 bfq_log(bfqd, "update_peak_rate: goto reset, samples %d",
2736 bfqd->peak_rate_samples);
2737 bfq_reset_rate_computation(bfqd, rq);
2738 goto update_last_values; /* will add one sample */
2739 }
2740
2741 /*
2742 * Device idle for very long: the observation interval lasting
2743 * up to this dispatch cannot be a valid observation interval
2744 * for computing a new peak rate (similarly to the late-
2745 * completion event in bfq_completed_request()). Go to
2746 * update_rate_and_reset to have the following three steps
2747 * taken:
2748 * - close the observation interval at the last (previous)
2749 * request dispatch or completion
2750 * - compute rate, if possible, for that observation interval
2751 * - start a new observation interval with this dispatch
2752 */
2753 if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC &&
2754 bfqd->rq_in_driver == 0)
2755 goto update_rate_and_reset;
2756
2757 /* Update sampling information */
2758 bfqd->peak_rate_samples++;
2759
2760 if ((bfqd->rq_in_driver > 0 ||
2761 now_ns - bfqd->last_completion < BFQ_MIN_TT)
Paolo Valented87447d2019-01-29 12:06:33 +01002762 && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq))
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002763 bfqd->sequential_samples++;
2764
2765 bfqd->tot_sectors_dispatched += blk_rq_sectors(rq);
2766
2767 /* Reset max observed rq size every 32 dispatches */
2768 if (likely(bfqd->peak_rate_samples % 32))
2769 bfqd->last_rq_max_size =
2770 max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size);
2771 else
2772 bfqd->last_rq_max_size = blk_rq_sectors(rq);
2773
2774 bfqd->delta_from_first = now_ns - bfqd->first_dispatch;
2775
2776 /* Target observation interval not yet reached, go on sampling */
2777 if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL)
2778 goto update_last_values;
2779
2780update_rate_and_reset:
2781 bfq_update_rate_reset(bfqd, rq);
2782update_last_values:
2783 bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
2784 bfqd->last_dispatch = now_ns;
2785}
2786
2787/*
Paolo Valenteaee69d72017-04-19 08:29:02 -06002788 * Remove request from internal lists.
2789 */
2790static void bfq_dispatch_remove(struct request_queue *q, struct request *rq)
2791{
2792 struct bfq_queue *bfqq = RQ_BFQQ(rq);
2793
2794 /*
2795 * For consistency, the next instruction should have been
2796 * executed after removing the request from the queue and
2797 * dispatching it. We execute instead this instruction before
2798 * bfq_remove_request() (and hence introduce a temporary
2799 * inconsistency), for efficiency. In fact, should this
2800 * dispatch occur for a non in-service bfqq, this anticipated
2801 * increment prevents two counters related to bfqq->dispatched
2802 * from risking to be, first, uselessly decremented, and then
2803 * incremented again when the (new) value of bfqq->dispatched
2804 * happens to be taken into account.
2805 */
2806 bfqq->dispatched++;
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002807 bfq_update_peak_rate(q->elevator->elevator_data, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002808
2809 bfq_remove_request(q, rq);
2810}
2811
2812static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq)
2813{
Arianna Avanzini36eca892017-04-12 18:23:16 +02002814 /*
2815 * If this bfqq is shared between multiple processes, check
2816 * to make sure that those processes are still issuing I/Os
2817 * within the mean seek distance. If not, it may be time to
2818 * break the queues apart again.
2819 */
2820 if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
2821 bfq_mark_bfqq_split_coop(bfqq);
2822
Paolo Valente44e44a12017-04-12 18:23:12 +02002823 if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
2824 if (bfqq->dispatched == 0)
2825 /*
2826 * Overloading budget_timeout field to store
2827 * the time at which the queue remains with no
2828 * backlog and no outstanding request; used by
2829 * the weight-raising mechanism.
2830 */
2831 bfqq->budget_timeout = jiffies;
2832
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002833 bfq_del_bfqq_busy(bfqd, bfqq, true);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002834 } else {
Paolo Valente80294c32017-08-31 08:46:29 +02002835 bfq_requeue_bfqq(bfqd, bfqq, true);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002836 /*
2837 * Resort priority tree of potential close cooperators.
2838 */
2839 bfq_pos_tree_add_move(bfqd, bfqq);
2840 }
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002841
2842 /*
2843 * All in-service entities must have been properly deactivated
2844 * or requeued before executing the next function, which
2845 * resets all in-service entites as no more in service.
2846 */
2847 __bfq_bfqd_reset_in_service(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002848}
2849
2850/**
2851 * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
2852 * @bfqd: device data.
2853 * @bfqq: queue to update.
2854 * @reason: reason for expiration.
2855 *
2856 * Handle the feedback on @bfqq budget at queue expiration.
2857 * See the body for detailed comments.
2858 */
2859static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
2860 struct bfq_queue *bfqq,
2861 enum bfqq_expiration reason)
2862{
2863 struct request *next_rq;
2864 int budget, min_budget;
2865
Paolo Valenteaee69d72017-04-19 08:29:02 -06002866 min_budget = bfq_min_budget(bfqd);
2867
Paolo Valente44e44a12017-04-12 18:23:12 +02002868 if (bfqq->wr_coeff == 1)
2869 budget = bfqq->max_budget;
2870 else /*
2871 * Use a constant, low budget for weight-raised queues,
2872 * to help achieve a low latency. Keep it slightly higher
2873 * than the minimum possible budget, to cause a little
2874 * bit fewer expirations.
2875 */
2876 budget = 2 * min_budget;
2877
Paolo Valenteaee69d72017-04-19 08:29:02 -06002878 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d",
2879 bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
2880 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d",
2881 budget, bfq_min_budget(bfqd));
2882 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
2883 bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
2884
Paolo Valente44e44a12017-04-12 18:23:12 +02002885 if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06002886 switch (reason) {
2887 /*
2888 * Caveat: in all the following cases we trade latency
2889 * for throughput.
2890 */
2891 case BFQQE_TOO_IDLE:
Paolo Valente54b60452017-04-12 18:23:09 +02002892 /*
2893 * This is the only case where we may reduce
2894 * the budget: if there is no request of the
2895 * process still waiting for completion, then
2896 * we assume (tentatively) that the timer has
2897 * expired because the batch of requests of
2898 * the process could have been served with a
2899 * smaller budget. Hence, betting that
2900 * process will behave in the same way when it
2901 * becomes backlogged again, we reduce its
2902 * next budget. As long as we guess right,
2903 * this budget cut reduces the latency
2904 * experienced by the process.
2905 *
2906 * However, if there are still outstanding
2907 * requests, then the process may have not yet
2908 * issued its next request just because it is
2909 * still waiting for the completion of some of
2910 * the still outstanding ones. So in this
2911 * subcase we do not reduce its budget, on the
2912 * contrary we increase it to possibly boost
2913 * the throughput, as discussed in the
2914 * comments to the BUDGET_TIMEOUT case.
2915 */
2916 if (bfqq->dispatched > 0) /* still outstanding reqs */
2917 budget = min(budget * 2, bfqd->bfq_max_budget);
2918 else {
2919 if (budget > 5 * min_budget)
2920 budget -= 4 * min_budget;
2921 else
2922 budget = min_budget;
2923 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06002924 break;
2925 case BFQQE_BUDGET_TIMEOUT:
Paolo Valente54b60452017-04-12 18:23:09 +02002926 /*
2927 * We double the budget here because it gives
2928 * the chance to boost the throughput if this
2929 * is not a seeky process (and has bumped into
2930 * this timeout because of, e.g., ZBR).
2931 */
2932 budget = min(budget * 2, bfqd->bfq_max_budget);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002933 break;
2934 case BFQQE_BUDGET_EXHAUSTED:
2935 /*
2936 * The process still has backlog, and did not
2937 * let either the budget timeout or the disk
2938 * idling timeout expire. Hence it is not
2939 * seeky, has a short thinktime and may be
2940 * happy with a higher budget too. So
2941 * definitely increase the budget of this good
2942 * candidate to boost the disk throughput.
2943 */
Paolo Valente54b60452017-04-12 18:23:09 +02002944 budget = min(budget * 4, bfqd->bfq_max_budget);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002945 break;
2946 case BFQQE_NO_MORE_REQUESTS:
2947 /*
2948 * For queues that expire for this reason, it
2949 * is particularly important to keep the
2950 * budget close to the actual service they
2951 * need. Doing so reduces the timestamp
2952 * misalignment problem described in the
2953 * comments in the body of
2954 * __bfq_activate_entity. In fact, suppose
2955 * that a queue systematically expires for
2956 * BFQQE_NO_MORE_REQUESTS and presents a
2957 * new request in time to enjoy timestamp
2958 * back-shifting. The larger the budget of the
2959 * queue is with respect to the service the
2960 * queue actually requests in each service
2961 * slot, the more times the queue can be
2962 * reactivated with the same virtual finish
2963 * time. It follows that, even if this finish
2964 * time is pushed to the system virtual time
2965 * to reduce the consequent timestamp
2966 * misalignment, the queue unjustly enjoys for
2967 * many re-activations a lower finish time
2968 * than all newly activated queues.
2969 *
2970 * The service needed by bfqq is measured
2971 * quite precisely by bfqq->entity.service.
2972 * Since bfqq does not enjoy device idling,
2973 * bfqq->entity.service is equal to the number
2974 * of sectors that the process associated with
2975 * bfqq requested to read/write before waiting
2976 * for request completions, or blocking for
2977 * other reasons.
2978 */
2979 budget = max_t(int, bfqq->entity.service, min_budget);
2980 break;
2981 default:
2982 return;
2983 }
Paolo Valente44e44a12017-04-12 18:23:12 +02002984 } else if (!bfq_bfqq_sync(bfqq)) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06002985 /*
2986 * Async queues get always the maximum possible
2987 * budget, as for them we do not care about latency
2988 * (in addition, their ability to dispatch is limited
2989 * by the charging factor).
2990 */
2991 budget = bfqd->bfq_max_budget;
2992 }
2993
2994 bfqq->max_budget = budget;
2995
2996 if (bfqd->budgets_assigned >= bfq_stats_min_budgets &&
2997 !bfqd->bfq_user_max_budget)
2998 bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget);
2999
3000 /*
3001 * If there is still backlog, then assign a new budget, making
3002 * sure that it is large enough for the next request. Since
3003 * the finish time of bfqq must be kept in sync with the
3004 * budget, be sure to call __bfq_bfqq_expire() *after* this
3005 * update.
3006 *
3007 * If there is no backlog, then no need to update the budget;
3008 * it will be updated on the arrival of a new request.
3009 */
3010 next_rq = bfqq->next_rq;
3011 if (next_rq)
3012 bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
3013 bfq_serv_to_charge(next_rq, bfqq));
3014
3015 bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d",
3016 next_rq ? blk_rq_sectors(next_rq) : 0,
3017 bfqq->entity.budget);
3018}
3019
Paolo Valenteaee69d72017-04-19 08:29:02 -06003020/*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003021 * Return true if the process associated with bfqq is "slow". The slow
3022 * flag is used, in addition to the budget timeout, to reduce the
3023 * amount of service provided to seeky processes, and thus reduce
3024 * their chances to lower the throughput. More details in the comments
3025 * on the function bfq_bfqq_expire().
3026 *
3027 * An important observation is in order: as discussed in the comments
3028 * on the function bfq_update_peak_rate(), with devices with internal
3029 * queues, it is hard if ever possible to know when and for how long
3030 * an I/O request is processed by the device (apart from the trivial
3031 * I/O pattern where a new request is dispatched only after the
3032 * previous one has been completed). This makes it hard to evaluate
3033 * the real rate at which the I/O requests of each bfq_queue are
3034 * served. In fact, for an I/O scheduler like BFQ, serving a
3035 * bfq_queue means just dispatching its requests during its service
3036 * slot (i.e., until the budget of the queue is exhausted, or the
3037 * queue remains idle, or, finally, a timeout fires). But, during the
3038 * service slot of a bfq_queue, around 100 ms at most, the device may
3039 * be even still processing requests of bfq_queues served in previous
3040 * service slots. On the opposite end, the requests of the in-service
3041 * bfq_queue may be completed after the service slot of the queue
3042 * finishes.
3043 *
3044 * Anyway, unless more sophisticated solutions are used
3045 * (where possible), the sum of the sizes of the requests dispatched
3046 * during the service slot of a bfq_queue is probably the only
3047 * approximation available for the service received by the bfq_queue
3048 * during its service slot. And this sum is the quantity used in this
3049 * function to evaluate the I/O speed of a process.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003050 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003051static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq,
3052 bool compensate, enum bfqq_expiration reason,
3053 unsigned long *delta_ms)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003054{
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003055 ktime_t delta_ktime;
3056 u32 delta_usecs;
3057 bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */
Paolo Valenteaee69d72017-04-19 08:29:02 -06003058
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003059 if (!bfq_bfqq_sync(bfqq))
Paolo Valenteaee69d72017-04-19 08:29:02 -06003060 return false;
3061
3062 if (compensate)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003063 delta_ktime = bfqd->last_idling_start;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003064 else
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003065 delta_ktime = ktime_get();
3066 delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start);
3067 delta_usecs = ktime_to_us(delta_ktime);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003068
3069 /* don't use too short time intervals */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003070 if (delta_usecs < 1000) {
3071 if (blk_queue_nonrot(bfqd->queue))
3072 /*
3073 * give same worst-case guarantees as idling
3074 * for seeky
3075 */
3076 *delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC;
3077 else /* charge at least one seek */
3078 *delta_ms = bfq_slice_idle / NSEC_PER_MSEC;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003079
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003080 return slow;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003081 }
3082
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003083 *delta_ms = delta_usecs / USEC_PER_MSEC;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003084
3085 /*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003086 * Use only long (> 20ms) intervals to filter out excessive
3087 * spikes in service rate estimation.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003088 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003089 if (delta_usecs > 20000) {
3090 /*
3091 * Caveat for rotational devices: processes doing I/O
3092 * in the slower disk zones tend to be slow(er) even
3093 * if not seeky. In this respect, the estimated peak
3094 * rate is likely to be an average over the disk
3095 * surface. Accordingly, to not be too harsh with
3096 * unlucky processes, a process is deemed slow only if
3097 * its rate has been lower than half of the estimated
3098 * peak rate.
3099 */
3100 slow = bfqq->entity.service < bfqd->bfq_max_budget / 2;
3101 }
3102
3103 bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow);
3104
3105 return slow;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003106}
3107
3108/*
Paolo Valente77b7dce2017-04-12 18:23:13 +02003109 * To be deemed as soft real-time, an application must meet two
3110 * requirements. First, the application must not require an average
3111 * bandwidth higher than the approximate bandwidth required to playback or
3112 * record a compressed high-definition video.
3113 * The next function is invoked on the completion of the last request of a
3114 * batch, to compute the next-start time instant, soft_rt_next_start, such
3115 * that, if the next request of the application does not arrive before
3116 * soft_rt_next_start, then the above requirement on the bandwidth is met.
3117 *
3118 * The second requirement is that the request pattern of the application is
3119 * isochronous, i.e., that, after issuing a request or a batch of requests,
3120 * the application stops issuing new requests until all its pending requests
3121 * have been completed. After that, the application may issue a new batch,
3122 * and so on.
3123 * For this reason the next function is invoked to compute
3124 * soft_rt_next_start only for applications that meet this requirement,
3125 * whereas soft_rt_next_start is set to infinity for applications that do
3126 * not.
3127 *
Paolo Valentea34b0242017-12-15 07:23:12 +01003128 * Unfortunately, even a greedy (i.e., I/O-bound) application may
3129 * happen to meet, occasionally or systematically, both the above
3130 * bandwidth and isochrony requirements. This may happen at least in
3131 * the following circumstances. First, if the CPU load is high. The
3132 * application may stop issuing requests while the CPUs are busy
3133 * serving other processes, then restart, then stop again for a while,
3134 * and so on. The other circumstances are related to the storage
3135 * device: the storage device is highly loaded or reaches a low-enough
3136 * throughput with the I/O of the application (e.g., because the I/O
3137 * is random and/or the device is slow). In all these cases, the
3138 * I/O of the application may be simply slowed down enough to meet
3139 * the bandwidth and isochrony requirements. To reduce the probability
3140 * that greedy applications are deemed as soft real-time in these
3141 * corner cases, a further rule is used in the computation of
3142 * soft_rt_next_start: the return value of this function is forced to
3143 * be higher than the maximum between the following two quantities.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003144 *
Paolo Valentea34b0242017-12-15 07:23:12 +01003145 * (a) Current time plus: (1) the maximum time for which the arrival
3146 * of a request is waited for when a sync queue becomes idle,
3147 * namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
3148 * postpone for a moment the reason for adding a few extra
3149 * jiffies; we get back to it after next item (b). Lower-bounding
3150 * the return value of this function with the current time plus
3151 * bfqd->bfq_slice_idle tends to filter out greedy applications,
3152 * because the latter issue their next request as soon as possible
3153 * after the last one has been completed. In contrast, a soft
3154 * real-time application spends some time processing data, after a
3155 * batch of its requests has been completed.
3156 *
3157 * (b) Current value of bfqq->soft_rt_next_start. As pointed out
3158 * above, greedy applications may happen to meet both the
3159 * bandwidth and isochrony requirements under heavy CPU or
3160 * storage-device load. In more detail, in these scenarios, these
3161 * applications happen, only for limited time periods, to do I/O
3162 * slowly enough to meet all the requirements described so far,
3163 * including the filtering in above item (a). These slow-speed
3164 * time intervals are usually interspersed between other time
3165 * intervals during which these applications do I/O at a very high
3166 * speed. Fortunately, exactly because of the high speed of the
3167 * I/O in the high-speed intervals, the values returned by this
3168 * function happen to be so high, near the end of any such
3169 * high-speed interval, to be likely to fall *after* the end of
3170 * the low-speed time interval that follows. These high values are
3171 * stored in bfqq->soft_rt_next_start after each invocation of
3172 * this function. As a consequence, if the last value of
3173 * bfqq->soft_rt_next_start is constantly used to lower-bound the
3174 * next value that this function may return, then, from the very
3175 * beginning of a low-speed interval, bfqq->soft_rt_next_start is
3176 * likely to be constantly kept so high that any I/O request
3177 * issued during the low-speed interval is considered as arriving
3178 * to soon for the application to be deemed as soft
3179 * real-time. Then, in the high-speed interval that follows, the
3180 * application will not be deemed as soft real-time, just because
3181 * it will do I/O at a high speed. And so on.
3182 *
3183 * Getting back to the filtering in item (a), in the following two
3184 * cases this filtering might be easily passed by a greedy
3185 * application, if the reference quantity was just
3186 * bfqd->bfq_slice_idle:
3187 * 1) HZ is so low that the duration of a jiffy is comparable to or
3188 * higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
3189 * devices with HZ=100. The time granularity may be so coarse
3190 * that the approximation, in jiffies, of bfqd->bfq_slice_idle
3191 * is rather lower than the exact value.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003192 * 2) jiffies, instead of increasing at a constant rate, may stop increasing
3193 * for a while, then suddenly 'jump' by several units to recover the lost
3194 * increments. This seems to happen, e.g., inside virtual machines.
Paolo Valentea34b0242017-12-15 07:23:12 +01003195 * To address this issue, in the filtering in (a) we do not use as a
3196 * reference time interval just bfqd->bfq_slice_idle, but
3197 * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
3198 * minimum number of jiffies for which the filter seems to be quite
3199 * precise also in embedded systems and KVM/QEMU virtual machines.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003200 */
3201static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
3202 struct bfq_queue *bfqq)
3203{
Paolo Valentea34b0242017-12-15 07:23:12 +01003204 return max3(bfqq->soft_rt_next_start,
3205 bfqq->last_idle_bklogged +
3206 HZ * bfqq->service_from_backlogged /
3207 bfqd->bfq_wr_max_softrt_rate,
3208 jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4);
Paolo Valente77b7dce2017-04-12 18:23:13 +02003209}
3210
Paolo Valented0edc242018-09-14 16:23:08 +02003211static bool bfq_bfqq_injectable(struct bfq_queue *bfqq)
3212{
3213 return BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
3214 blk_queue_nonrot(bfqq->bfqd->queue) &&
3215 bfqq->bfqd->hw_tag;
3216}
3217
Paolo Valenteaee69d72017-04-19 08:29:02 -06003218/**
3219 * bfq_bfqq_expire - expire a queue.
3220 * @bfqd: device owning the queue.
3221 * @bfqq: the queue to expire.
3222 * @compensate: if true, compensate for the time spent idling.
3223 * @reason: the reason causing the expiration.
3224 *
Paolo Valentec074170e2017-04-12 18:23:11 +02003225 * If the process associated with bfqq does slow I/O (e.g., because it
3226 * issues random requests), we charge bfqq with the time it has been
3227 * in service instead of the service it has received (see
3228 * bfq_bfqq_charge_time for details on how this goal is achieved). As
3229 * a consequence, bfqq will typically get higher timestamps upon
3230 * reactivation, and hence it will be rescheduled as if it had
3231 * received more service than what it has actually received. In the
3232 * end, bfqq receives less service in proportion to how slowly its
3233 * associated process consumes its budgets (and hence how seriously it
3234 * tends to lower the throughput). In addition, this time-charging
3235 * strategy guarantees time fairness among slow processes. In
3236 * contrast, if the process associated with bfqq is not slow, we
3237 * charge bfqq exactly with the service it has received.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003238 *
Paolo Valentec074170e2017-04-12 18:23:11 +02003239 * Charging time to the first type of queues and the exact service to
3240 * the other has the effect of using the WF2Q+ policy to schedule the
3241 * former on a timeslice basis, without violating service domain
3242 * guarantees among the latter.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003243 */
Paolo Valenteea25da42017-04-19 08:48:24 -06003244void bfq_bfqq_expire(struct bfq_data *bfqd,
3245 struct bfq_queue *bfqq,
3246 bool compensate,
3247 enum bfqq_expiration reason)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003248{
3249 bool slow;
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003250 unsigned long delta = 0;
3251 struct bfq_entity *entity = &bfqq->entity;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003252 int ref;
3253
3254 /*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003255 * Check whether the process is slow (see bfq_bfqq_is_slow).
Paolo Valenteaee69d72017-04-19 08:29:02 -06003256 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003257 slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003258
3259 /*
Paolo Valentec074170e2017-04-12 18:23:11 +02003260 * As above explained, charge slow (typically seeky) and
3261 * timed-out queues with the time and not the service
3262 * received, to favor sequential workloads.
3263 *
3264 * Processes doing I/O in the slower disk zones will tend to
3265 * be slow(er) even if not seeky. Therefore, since the
3266 * estimated peak rate is actually an average over the disk
3267 * surface, these processes may timeout just for bad luck. To
3268 * avoid punishing them, do not charge time to processes that
3269 * succeeded in consuming at least 2/3 of their budget. This
3270 * allows BFQ to preserve enough elasticity to still perform
3271 * bandwidth, and not time, distribution with little unlucky
3272 * or quasi-sequential processes.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003273 */
Paolo Valente44e44a12017-04-12 18:23:12 +02003274 if (bfqq->wr_coeff == 1 &&
3275 (slow ||
3276 (reason == BFQQE_BUDGET_TIMEOUT &&
3277 bfq_bfqq_budget_left(bfqq) >= entity->budget / 3)))
Paolo Valentec074170e2017-04-12 18:23:11 +02003278 bfq_bfqq_charge_time(bfqd, bfqq, delta);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003279
3280 if (reason == BFQQE_TOO_IDLE &&
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003281 entity->service <= 2 * entity->budget / 10)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003282 bfq_clear_bfqq_IO_bound(bfqq);
3283
Paolo Valente44e44a12017-04-12 18:23:12 +02003284 if (bfqd->low_latency && bfqq->wr_coeff == 1)
3285 bfqq->last_wr_start_finish = jiffies;
3286
Paolo Valente77b7dce2017-04-12 18:23:13 +02003287 if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
3288 RB_EMPTY_ROOT(&bfqq->sort_list)) {
3289 /*
3290 * If we get here, and there are no outstanding
3291 * requests, then the request pattern is isochronous
3292 * (see the comments on the function
3293 * bfq_bfqq_softrt_next_start()). Thus we can compute
Paolo Valente20cd3242019-01-29 12:06:25 +01003294 * soft_rt_next_start. And we do it, unless bfqq is in
3295 * interactive weight raising. We do not do it in the
3296 * latter subcase, for the following reason. bfqq may
3297 * be conveying the I/O needed to load a soft
3298 * real-time application. Such an application will
3299 * actually exhibit a soft real-time I/O pattern after
3300 * it finally starts doing its job. But, if
3301 * soft_rt_next_start is computed here for an
3302 * interactive bfqq, and bfqq had received a lot of
3303 * service before remaining with no outstanding
3304 * request (likely to happen on a fast device), then
3305 * soft_rt_next_start would be assigned such a high
3306 * value that, for a very long time, bfqq would be
3307 * prevented from being possibly considered as soft
3308 * real time.
3309 *
3310 * If, instead, the queue still has outstanding
3311 * requests, then we have to wait for the completion
3312 * of all the outstanding requests to discover whether
3313 * the request pattern is actually isochronous.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003314 */
Paolo Valente20cd3242019-01-29 12:06:25 +01003315 if (bfqq->dispatched == 0 &&
3316 bfqq->wr_coeff != bfqd->bfq_wr_coeff)
Paolo Valente77b7dce2017-04-12 18:23:13 +02003317 bfqq->soft_rt_next_start =
3318 bfq_bfqq_softrt_next_start(bfqd, bfqq);
Paolo Valente20cd3242019-01-29 12:06:25 +01003319 else if (bfqq->dispatched > 0) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02003320 /*
Paolo Valente77b7dce2017-04-12 18:23:13 +02003321 * Schedule an update of soft_rt_next_start to when
3322 * the task may be discovered to be isochronous.
3323 */
3324 bfq_mark_bfqq_softrt_update(bfqq);
3325 }
3326 }
3327
Paolo Valenteaee69d72017-04-19 08:29:02 -06003328 bfq_log_bfqq(bfqd, bfqq,
Paolo Valented5be3fe2017-08-04 07:35:10 +02003329 "expire (%d, slow %d, num_disp %d, short_ttime %d)", reason,
3330 slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06003331
3332 /*
3333 * Increase, decrease or leave budget unchanged according to
3334 * reason.
3335 */
3336 __bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
3337 ref = bfqq->ref;
3338 __bfq_bfqq_expire(bfqd, bfqq);
3339
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003340 if (ref == 1) /* bfqq is gone, no more actions on it */
3341 return;
3342
Paolo Valented0edc242018-09-14 16:23:08 +02003343 bfqq->injected_service = 0;
3344
Paolo Valenteaee69d72017-04-19 08:29:02 -06003345 /* mark bfqq as waiting a request only if a bic still points to it */
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003346 if (!bfq_bfqq_busy(bfqq) &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06003347 reason != BFQQE_BUDGET_TIMEOUT &&
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003348 reason != BFQQE_BUDGET_EXHAUSTED) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06003349 bfq_mark_bfqq_non_blocking_wait_rq(bfqq);
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003350 /*
3351 * Not setting service to 0, because, if the next rq
3352 * arrives in time, the queue will go on receiving
3353 * service with this same budget (as if it never expired)
3354 */
3355 } else
3356 entity->service = 0;
Paolo Valente8a511ba2018-08-16 18:51:15 +02003357
3358 /*
3359 * Reset the received-service counter for every parent entity.
3360 * Differently from what happens with bfqq->entity.service,
3361 * the resetting of this counter never needs to be postponed
3362 * for parent entities. In fact, in case bfqq may have a
3363 * chance to go on being served using the last, partially
3364 * consumed budget, bfqq->entity.service needs to be kept,
3365 * because if bfqq then actually goes on being served using
3366 * the same budget, the last value of bfqq->entity.service is
3367 * needed to properly decrement bfqq->entity.budget by the
3368 * portion already consumed. In contrast, it is not necessary
3369 * to keep entity->service for parent entities too, because
3370 * the bubble up of the new value of bfqq->entity.budget will
3371 * make sure that the budgets of parent entities are correct,
3372 * even in case bfqq and thus parent entities go on receiving
3373 * service with the same budget.
3374 */
3375 entity = entity->parent;
3376 for_each_entity(entity)
3377 entity->service = 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003378}
3379
3380/*
3381 * Budget timeout is not implemented through a dedicated timer, but
3382 * just checked on request arrivals and completions, as well as on
3383 * idle timer expirations.
3384 */
3385static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
3386{
Paolo Valente44e44a12017-04-12 18:23:12 +02003387 return time_is_before_eq_jiffies(bfqq->budget_timeout);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003388}
3389
3390/*
3391 * If we expire a queue that is actively waiting (i.e., with the
3392 * device idled) for the arrival of a new request, then we may incur
3393 * the timestamp misalignment problem described in the body of the
3394 * function __bfq_activate_entity. Hence we return true only if this
3395 * condition does not hold, or if the queue is slow enough to deserve
3396 * only to be kicked off for preserving a high throughput.
3397 */
3398static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
3399{
3400 bfq_log_bfqq(bfqq->bfqd, bfqq,
3401 "may_budget_timeout: wait_request %d left %d timeout %d",
3402 bfq_bfqq_wait_request(bfqq),
3403 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3,
3404 bfq_bfqq_budget_timeout(bfqq));
3405
3406 return (!bfq_bfqq_wait_request(bfqq) ||
3407 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3)
3408 &&
3409 bfq_bfqq_budget_timeout(bfqq);
3410}
3411
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003412static bool idling_boosts_thr_without_issues(struct bfq_data *bfqd,
3413 struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003414{
Paolo Valenteedaf9422017-08-04 07:35:11 +02003415 bool rot_without_queueing =
3416 !blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag,
3417 bfqq_sequential_and_IO_bound,
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003418 idling_boosts_thr;
Paolo Valented5be3fe2017-08-04 07:35:10 +02003419
Paolo Valenteedaf9422017-08-04 07:35:11 +02003420 bfqq_sequential_and_IO_bound = !BFQQ_SEEKY(bfqq) &&
3421 bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_has_short_ttime(bfqq);
3422
Paolo Valented5be3fe2017-08-04 07:35:10 +02003423 /*
Paolo Valente44e44a12017-04-12 18:23:12 +02003424 * The next variable takes into account the cases where idling
3425 * boosts the throughput.
3426 *
Paolo Valentee01eff02017-04-12 18:23:19 +02003427 * The value of the variable is computed considering, first, that
3428 * idling is virtually always beneficial for the throughput if:
Paolo Valenteedaf9422017-08-04 07:35:11 +02003429 * (a) the device is not NCQ-capable and rotational, or
3430 * (b) regardless of the presence of NCQ, the device is rotational and
3431 * the request pattern for bfqq is I/O-bound and sequential, or
3432 * (c) regardless of whether it is rotational, the device is
3433 * not NCQ-capable and the request pattern for bfqq is
3434 * I/O-bound and sequential.
Paolo Valentebf2b79e2017-04-12 18:23:18 +02003435 *
3436 * Secondly, and in contrast to the above item (b), idling an
3437 * NCQ-capable flash-based device would not boost the
Paolo Valentee01eff02017-04-12 18:23:19 +02003438 * throughput even with sequential I/O; rather it would lower
Paolo Valentebf2b79e2017-04-12 18:23:18 +02003439 * the throughput in proportion to how fast the device
3440 * is. Accordingly, the next variable is true if any of the
Paolo Valenteedaf9422017-08-04 07:35:11 +02003441 * above conditions (a), (b) or (c) is true, and, in
3442 * particular, happens to be false if bfqd is an NCQ-capable
3443 * flash-based device.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003444 */
Paolo Valenteedaf9422017-08-04 07:35:11 +02003445 idling_boosts_thr = rot_without_queueing ||
3446 ((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) &&
3447 bfqq_sequential_and_IO_bound);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003448
3449 /*
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003450 * The return value of this function is equal to that of
Paolo Valentecfd69712017-04-12 18:23:15 +02003451 * idling_boosts_thr, unless a special case holds. In this
3452 * special case, described below, idling may cause problems to
3453 * weight-raised queues.
3454 *
3455 * When the request pool is saturated (e.g., in the presence
3456 * of write hogs), if the processes associated with
3457 * non-weight-raised queues ask for requests at a lower rate,
3458 * then processes associated with weight-raised queues have a
3459 * higher probability to get a request from the pool
3460 * immediately (or at least soon) when they need one. Thus
3461 * they have a higher probability to actually get a fraction
3462 * of the device throughput proportional to their high
3463 * weight. This is especially true with NCQ-capable drives,
3464 * which enqueue several requests in advance, and further
3465 * reorder internally-queued requests.
3466 *
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003467 * For this reason, we force to false the return value if
3468 * there are weight-raised busy queues. In this case, and if
3469 * bfqq is not weight-raised, this guarantees that the device
3470 * is not idled for bfqq (if, instead, bfqq is weight-raised,
3471 * then idling will be guaranteed by another variable, see
3472 * below). Combined with the timestamping rules of BFQ (see
3473 * [1] for details), this behavior causes bfqq, and hence any
3474 * sync non-weight-raised queue, to get a lower number of
3475 * requests served, and thus to ask for a lower number of
3476 * requests from the request pool, before the busy
3477 * weight-raised queues get served again. This often mitigates
3478 * starvation problems in the presence of heavy write
3479 * workloads and NCQ, thereby guaranteeing a higher
3480 * application and system responsiveness in these hostile
3481 * scenarios.
Paolo Valentecfd69712017-04-12 18:23:15 +02003482 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003483 return idling_boosts_thr &&
Paolo Valentecfd69712017-04-12 18:23:15 +02003484 bfqd->wr_busy_queues == 0;
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003485}
Paolo Valentecfd69712017-04-12 18:23:15 +02003486
Paolo Valente530c4cb2019-01-29 12:06:32 +01003487/*
3488 * There is a case where idling must be performed not for
3489 * throughput concerns, but to preserve service guarantees.
3490 *
3491 * To introduce this case, we can note that allowing the drive
3492 * to enqueue more than one request at a time, and hence
3493 * delegating de facto final scheduling decisions to the
3494 * drive's internal scheduler, entails loss of control on the
3495 * actual request service order. In particular, the critical
3496 * situation is when requests from different processes happen
3497 * to be present, at the same time, in the internal queue(s)
3498 * of the drive. In such a situation, the drive, by deciding
3499 * the service order of the internally-queued requests, does
3500 * determine also the actual throughput distribution among
3501 * these processes. But the drive typically has no notion or
3502 * concern about per-process throughput distribution, and
3503 * makes its decisions only on a per-request basis. Therefore,
3504 * the service distribution enforced by the drive's internal
3505 * scheduler is likely to coincide with the desired
3506 * device-throughput distribution only in a completely
3507 * symmetric scenario where:
3508 * (i) each of these processes must get the same throughput as
3509 * the others;
3510 * (ii) the I/O of each process has the same properties, in
3511 * terms of locality (sequential or random), direction
3512 * (reads or writes), request sizes, greediness
3513 * (from I/O-bound to sporadic), and so on.
3514 * In fact, in such a scenario, the drive tends to treat
3515 * the requests of each of these processes in about the same
3516 * way as the requests of the others, and thus to provide
3517 * each of these processes with about the same throughput
3518 * (which is exactly the desired throughput distribution). In
3519 * contrast, in any asymmetric scenario, device idling is
3520 * certainly needed to guarantee that bfqq receives its
3521 * assigned fraction of the device throughput (see [1] for
3522 * details).
3523 * The problem is that idling may significantly reduce
3524 * throughput with certain combinations of types of I/O and
3525 * devices. An important example is sync random I/O, on flash
3526 * storage with command queueing. So, unless bfqq falls in the
3527 * above cases where idling also boosts throughput, it would
3528 * be important to check conditions (i) and (ii) accurately,
3529 * so as to avoid idling when not strictly needed for service
3530 * guarantees.
3531 *
3532 * Unfortunately, it is extremely difficult to thoroughly
3533 * check condition (ii). And, in case there are active groups,
3534 * it becomes very difficult to check condition (i) too. In
3535 * fact, if there are active groups, then, for condition (i)
3536 * to become false, it is enough that an active group contains
3537 * more active processes or sub-groups than some other active
3538 * group. More precisely, for condition (i) to hold because of
3539 * such a group, it is not even necessary that the group is
3540 * (still) active: it is sufficient that, even if the group
3541 * has become inactive, some of its descendant processes still
3542 * have some request already dispatched but still waiting for
3543 * completion. In fact, requests have still to be guaranteed
3544 * their share of the throughput even after being
3545 * dispatched. In this respect, it is easy to show that, if a
3546 * group frequently becomes inactive while still having
3547 * in-flight requests, and if, when this happens, the group is
3548 * not considered in the calculation of whether the scenario
3549 * is asymmetric, then the group may fail to be guaranteed its
3550 * fair share of the throughput (basically because idling may
3551 * not be performed for the descendant processes of the group,
3552 * but it had to be). We address this issue with the
3553 * following bi-modal behavior, implemented in the function
3554 * bfq_symmetric_scenario().
3555 *
3556 * If there are groups with requests waiting for completion
3557 * (as commented above, some of these groups may even be
3558 * already inactive), then the scenario is tagged as
3559 * asymmetric, conservatively, without checking any of the
3560 * conditions (i) and (ii). So the device is idled for bfqq.
3561 * This behavior matches also the fact that groups are created
3562 * exactly if controlling I/O is a primary concern (to
3563 * preserve bandwidth and latency guarantees).
3564 *
3565 * On the opposite end, if there are no groups with requests
3566 * waiting for completion, then only condition (i) is actually
3567 * controlled, i.e., provided that condition (i) holds, idling
3568 * is not performed, regardless of whether condition (ii)
3569 * holds. In other words, only if condition (i) does not hold,
3570 * then idling is allowed, and the device tends to be
3571 * prevented from queueing many requests, possibly of several
3572 * processes. Since there are no groups with requests waiting
3573 * for completion, then, to control condition (i) it is enough
3574 * to check just whether all the queues with requests waiting
3575 * for completion also have the same weight.
3576 *
3577 * Not checking condition (ii) evidently exposes bfqq to the
3578 * risk of getting less throughput than its fair share.
3579 * However, for queues with the same weight, a further
3580 * mechanism, preemption, mitigates or even eliminates this
3581 * problem. And it does so without consequences on overall
3582 * throughput. This mechanism and its benefits are explained
3583 * in the next three paragraphs.
3584 *
3585 * Even if a queue, say Q, is expired when it remains idle, Q
3586 * can still preempt the new in-service queue if the next
3587 * request of Q arrives soon (see the comments on
3588 * bfq_bfqq_update_budg_for_activation). If all queues and
3589 * groups have the same weight, this form of preemption,
3590 * combined with the hole-recovery heuristic described in the
3591 * comments on function bfq_bfqq_update_budg_for_activation,
3592 * are enough to preserve a correct bandwidth distribution in
3593 * the mid term, even without idling. In fact, even if not
3594 * idling allows the internal queues of the device to contain
3595 * many requests, and thus to reorder requests, we can rather
3596 * safely assume that the internal scheduler still preserves a
3597 * minimum of mid-term fairness.
3598 *
3599 * More precisely, this preemption-based, idleless approach
3600 * provides fairness in terms of IOPS, and not sectors per
3601 * second. This can be seen with a simple example. Suppose
3602 * that there are two queues with the same weight, but that
3603 * the first queue receives requests of 8 sectors, while the
3604 * second queue receives requests of 1024 sectors. In
3605 * addition, suppose that each of the two queues contains at
3606 * most one request at a time, which implies that each queue
3607 * always remains idle after it is served. Finally, after
3608 * remaining idle, each queue receives very quickly a new
3609 * request. It follows that the two queues are served
3610 * alternatively, preempting each other if needed. This
3611 * implies that, although both queues have the same weight,
3612 * the queue with large requests receives a service that is
3613 * 1024/8 times as high as the service received by the other
3614 * queue.
3615 *
3616 * The motivation for using preemption instead of idling (for
3617 * queues with the same weight) is that, by not idling,
3618 * service guarantees are preserved (completely or at least in
3619 * part) without minimally sacrificing throughput. And, if
3620 * there is no active group, then the primary expectation for
3621 * this device is probably a high throughput.
3622 *
3623 * We are now left only with explaining the additional
3624 * compound condition that is checked below for deciding
3625 * whether the scenario is asymmetric. To explain this
3626 * compound condition, we need to add that the function
3627 * bfq_symmetric_scenario checks the weights of only
3628 * non-weight-raised queues, for efficiency reasons (see
3629 * comments on bfq_weights_tree_add()). Then the fact that
3630 * bfqq is weight-raised is checked explicitly here. More
3631 * precisely, the compound condition below takes into account
3632 * also the fact that, even if bfqq is being weight-raised,
3633 * the scenario is still symmetric if all queues with requests
3634 * waiting for completion happen to be
3635 * weight-raised. Actually, we should be even more precise
3636 * here, and differentiate between interactive weight raising
3637 * and soft real-time weight raising.
3638 *
3639 * As a side note, it is worth considering that the above
3640 * device-idling countermeasures may however fail in the
3641 * following unlucky scenario: if idling is (correctly)
3642 * disabled in a time period during which all symmetry
3643 * sub-conditions hold, and hence the device is allowed to
3644 * enqueue many requests, but at some later point in time some
3645 * sub-condition stops to hold, then it may become impossible
3646 * to let requests be served in the desired order until all
3647 * the requests already queued in the device have been served.
3648 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003649static bool idling_needed_for_service_guarantees(struct bfq_data *bfqd,
3650 struct bfq_queue *bfqq)
3651{
Paolo Valente530c4cb2019-01-29 12:06:32 +01003652 return (bfqq->wr_coeff > 1 &&
3653 bfqd->wr_busy_queues <
3654 bfq_tot_busy_queues(bfqd)) ||
Paolo Valentebf2b79e2017-04-12 18:23:18 +02003655 !bfq_symmetric_scenario(bfqd);
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003656}
3657
3658/*
3659 * For a queue that becomes empty, device idling is allowed only if
3660 * this function returns true for that queue. As a consequence, since
3661 * device idling plays a critical role for both throughput boosting
3662 * and service guarantees, the return value of this function plays a
3663 * critical role as well.
3664 *
3665 * In a nutshell, this function returns true only if idling is
3666 * beneficial for throughput or, even if detrimental for throughput,
3667 * idling is however necessary to preserve service guarantees (low
3668 * latency, desired throughput distribution, ...). In particular, on
3669 * NCQ-capable devices, this function tries to return false, so as to
3670 * help keep the drives' internal queues full, whenever this helps the
3671 * device boost the throughput without causing any service-guarantee
3672 * issue.
3673 *
3674 * Most of the issues taken into account to get the return value of
3675 * this function are not trivial. We discuss these issues in the two
3676 * functions providing the main pieces of information needed by this
3677 * function.
3678 */
3679static bool bfq_better_to_idle(struct bfq_queue *bfqq)
3680{
3681 struct bfq_data *bfqd = bfqq->bfqd;
3682 bool idling_boosts_thr_with_no_issue, idling_needed_for_service_guar;
3683
3684 if (unlikely(bfqd->strict_guarantees))
3685 return true;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02003686
3687 /*
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003688 * Idling is performed only if slice_idle > 0. In addition, we
3689 * do not idle if
3690 * (a) bfqq is async
3691 * (b) bfqq is in the idle io prio class: in this case we do
3692 * not idle because we want to minimize the bandwidth that
3693 * queues in this class can steal to higher-priority queues
3694 */
3695 if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) ||
3696 bfq_class_idle(bfqq))
3697 return false;
3698
3699 idling_boosts_thr_with_no_issue =
3700 idling_boosts_thr_without_issues(bfqd, bfqq);
3701
3702 idling_needed_for_service_guar =
3703 idling_needed_for_service_guarantees(bfqd, bfqq);
3704
3705 /*
3706 * We have now the two components we need to compute the
Paolo Valented5be3fe2017-08-04 07:35:10 +02003707 * return value of the function, which is true only if idling
3708 * either boosts the throughput (without issues), or is
3709 * necessary to preserve service guarantees.
Paolo Valente44e44a12017-04-12 18:23:12 +02003710 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003711 return idling_boosts_thr_with_no_issue ||
3712 idling_needed_for_service_guar;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003713}
3714
3715/*
Paolo Valente277a4a92018-06-25 21:55:37 +02003716 * If the in-service queue is empty but the function bfq_better_to_idle
Paolo Valenteaee69d72017-04-19 08:29:02 -06003717 * returns true, then:
3718 * 1) the queue must remain in service and cannot be expired, and
3719 * 2) the device must be idled to wait for the possible arrival of a new
3720 * request for the queue.
Paolo Valente277a4a92018-06-25 21:55:37 +02003721 * See the comments on the function bfq_better_to_idle for the reasons
Paolo Valenteaee69d72017-04-19 08:29:02 -06003722 * why performing device idling is the best choice to boost the throughput
Paolo Valente277a4a92018-06-25 21:55:37 +02003723 * and preserve service guarantees when bfq_better_to_idle itself
Paolo Valenteaee69d72017-04-19 08:29:02 -06003724 * returns true.
3725 */
3726static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
3727{
Paolo Valente277a4a92018-06-25 21:55:37 +02003728 return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003729}
3730
Paolo Valented0edc242018-09-14 16:23:08 +02003731static struct bfq_queue *bfq_choose_bfqq_for_injection(struct bfq_data *bfqd)
3732{
3733 struct bfq_queue *bfqq;
3734
3735 /*
3736 * A linear search; but, with a high probability, very few
3737 * steps are needed to find a candidate queue, i.e., a queue
3738 * with enough budget left for its next request. In fact:
3739 * - BFQ dynamically updates the budget of every queue so as
3740 * to accommodate the expected backlog of the queue;
3741 * - if a queue gets all its requests dispatched as injected
3742 * service, then the queue is removed from the active list
3743 * (and re-added only if it gets new requests, but with
3744 * enough budget for its new backlog).
3745 */
3746 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
3747 if (!RB_EMPTY_ROOT(&bfqq->sort_list) &&
3748 bfq_serv_to_charge(bfqq->next_rq, bfqq) <=
3749 bfq_bfqq_budget_left(bfqq))
3750 return bfqq;
3751
3752 return NULL;
3753}
3754
Paolo Valenteaee69d72017-04-19 08:29:02 -06003755/*
3756 * Select a queue for service. If we have a current queue in service,
3757 * check whether to continue servicing it, or retrieve and set a new one.
3758 */
3759static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
3760{
3761 struct bfq_queue *bfqq;
3762 struct request *next_rq;
3763 enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT;
3764
3765 bfqq = bfqd->in_service_queue;
3766 if (!bfqq)
3767 goto new_queue;
3768
3769 bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
3770
Paolo Valente4420b092018-06-25 21:55:35 +02003771 /*
3772 * Do not expire bfqq for budget timeout if bfqq may be about
3773 * to enjoy device idling. The reason why, in this case, we
3774 * prevent bfqq from expiring is the same as in the comments
3775 * on the case where bfq_bfqq_must_idle() returns true, in
3776 * bfq_completed_request().
3777 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06003778 if (bfq_may_expire_for_budg_timeout(bfqq) &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06003779 !bfq_bfqq_must_idle(bfqq))
3780 goto expire;
3781
3782check_queue:
3783 /*
3784 * This loop is rarely executed more than once. Even when it
3785 * happens, it is much more convenient to re-execute this loop
3786 * than to return NULL and trigger a new dispatch to get a
3787 * request served.
3788 */
3789 next_rq = bfqq->next_rq;
3790 /*
3791 * If bfqq has requests queued and it has enough budget left to
3792 * serve them, keep the queue, otherwise expire it.
3793 */
3794 if (next_rq) {
3795 if (bfq_serv_to_charge(next_rq, bfqq) >
3796 bfq_bfqq_budget_left(bfqq)) {
3797 /*
3798 * Expire the queue for budget exhaustion,
3799 * which makes sure that the next budget is
3800 * enough to serve the next request, even if
3801 * it comes from the fifo expired path.
3802 */
3803 reason = BFQQE_BUDGET_EXHAUSTED;
3804 goto expire;
3805 } else {
3806 /*
3807 * The idle timer may be pending because we may
3808 * not disable disk idling even when a new request
3809 * arrives.
3810 */
3811 if (bfq_bfqq_wait_request(bfqq)) {
3812 /*
3813 * If we get here: 1) at least a new request
3814 * has arrived but we have not disabled the
3815 * timer because the request was too small,
3816 * 2) then the block layer has unplugged
3817 * the device, causing the dispatch to be
3818 * invoked.
3819 *
3820 * Since the device is unplugged, now the
3821 * requests are probably large enough to
3822 * provide a reasonable throughput.
3823 * So we disable idling.
3824 */
3825 bfq_clear_bfqq_wait_request(bfqq);
3826 hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
3827 }
3828 goto keep_queue;
3829 }
3830 }
3831
3832 /*
3833 * No requests pending. However, if the in-service queue is idling
3834 * for a new request, or has requests waiting for a completion and
3835 * may idle after their completion, then keep it anyway.
Paolo Valented0edc242018-09-14 16:23:08 +02003836 *
3837 * Yet, to boost throughput, inject service from other queues if
3838 * possible.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003839 */
3840 if (bfq_bfqq_wait_request(bfqq) ||
Paolo Valente277a4a92018-06-25 21:55:37 +02003841 (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) {
Paolo Valented0edc242018-09-14 16:23:08 +02003842 if (bfq_bfqq_injectable(bfqq) &&
3843 bfqq->injected_service * bfqq->inject_coeff <
3844 bfqq->entity.service * 10)
3845 bfqq = bfq_choose_bfqq_for_injection(bfqd);
3846 else
3847 bfqq = NULL;
3848
Paolo Valenteaee69d72017-04-19 08:29:02 -06003849 goto keep_queue;
3850 }
3851
3852 reason = BFQQE_NO_MORE_REQUESTS;
3853expire:
3854 bfq_bfqq_expire(bfqd, bfqq, false, reason);
3855new_queue:
3856 bfqq = bfq_set_in_service_queue(bfqd);
3857 if (bfqq) {
3858 bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue");
3859 goto check_queue;
3860 }
3861keep_queue:
3862 if (bfqq)
3863 bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue");
3864 else
3865 bfq_log(bfqd, "select_queue: no queue returned");
3866
3867 return bfqq;
3868}
3869
Paolo Valente44e44a12017-04-12 18:23:12 +02003870static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
3871{
3872 struct bfq_entity *entity = &bfqq->entity;
3873
3874 if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
3875 bfq_log_bfqq(bfqd, bfqq,
3876 "raising period dur %u/%u msec, old coeff %u, w %d(%d)",
3877 jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
3878 jiffies_to_msecs(bfqq->wr_cur_max_time),
3879 bfqq->wr_coeff,
3880 bfqq->entity.weight, bfqq->entity.orig_weight);
3881
3882 if (entity->prio_changed)
3883 bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
3884
3885 /*
Arianna Avanzinie1b23242017-04-12 18:23:20 +02003886 * If the queue was activated in a burst, or too much
3887 * time has elapsed from the beginning of this
3888 * weight-raising period, then end weight raising.
Paolo Valente44e44a12017-04-12 18:23:12 +02003889 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02003890 if (bfq_bfqq_in_large_burst(bfqq))
3891 bfq_bfqq_end_wr(bfqq);
3892 else if (time_is_before_jiffies(bfqq->last_wr_start_finish +
3893 bfqq->wr_cur_max_time)) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02003894 if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time ||
3895 time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt +
Arianna Avanzinie1b23242017-04-12 18:23:20 +02003896 bfq_wr_duration(bfqd)))
Paolo Valente77b7dce2017-04-12 18:23:13 +02003897 bfq_bfqq_end_wr(bfqq);
3898 else {
Paolo Valente3e2bdd62017-09-21 11:04:01 +02003899 switch_back_to_interactive_wr(bfqq, bfqd);
Paolo Valente77b7dce2017-04-12 18:23:13 +02003900 bfqq->entity.prio_changed = 1;
3901 }
Paolo Valente44e44a12017-04-12 18:23:12 +02003902 }
Paolo Valente8a8747d2018-01-13 12:05:18 +01003903 if (bfqq->wr_coeff > 1 &&
3904 bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time &&
3905 bfqq->service_from_wr > max_service_from_wr) {
3906 /* see comments on max_service_from_wr */
3907 bfq_bfqq_end_wr(bfqq);
3908 }
Paolo Valente44e44a12017-04-12 18:23:12 +02003909 }
Paolo Valente431b17f2017-07-03 10:00:10 +02003910 /*
3911 * To improve latency (for this or other queues), immediately
3912 * update weight both if it must be raised and if it must be
3913 * lowered. Since, entity may be on some active tree here, and
3914 * might have a pending change of its ioprio class, invoke
3915 * next function with the last parameter unset (see the
3916 * comments on the function).
3917 */
Paolo Valente44e44a12017-04-12 18:23:12 +02003918 if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
Paolo Valente431b17f2017-07-03 10:00:10 +02003919 __bfq_entity_update_weight_prio(bfq_entity_service_tree(entity),
3920 entity, false);
Paolo Valente44e44a12017-04-12 18:23:12 +02003921}
3922
Paolo Valenteaee69d72017-04-19 08:29:02 -06003923/*
3924 * Dispatch next request from bfqq.
3925 */
3926static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd,
3927 struct bfq_queue *bfqq)
3928{
3929 struct request *rq = bfqq->next_rq;
3930 unsigned long service_to_charge;
3931
3932 service_to_charge = bfq_serv_to_charge(rq, bfqq);
3933
3934 bfq_bfqq_served(bfqq, service_to_charge);
3935
3936 bfq_dispatch_remove(bfqd->queue, rq);
3937
Paolo Valented0edc242018-09-14 16:23:08 +02003938 if (bfqq != bfqd->in_service_queue) {
3939 if (likely(bfqd->in_service_queue))
3940 bfqd->in_service_queue->injected_service +=
3941 bfq_serv_to_charge(rq, bfqq);
3942
3943 goto return_rq;
3944 }
3945
Paolo Valente44e44a12017-04-12 18:23:12 +02003946 /*
3947 * If weight raising has to terminate for bfqq, then next
3948 * function causes an immediate update of bfqq's weight,
3949 * without waiting for next activation. As a consequence, on
3950 * expiration, bfqq will be timestamped as if has never been
3951 * weight-raised during this service slot, even if it has
3952 * received part or even most of the service as a
3953 * weight-raised queue. This inflates bfqq's timestamps, which
3954 * is beneficial, as bfqq is then more willing to leave the
3955 * device immediately to possible other weight-raised queues.
3956 */
3957 bfq_update_wr_data(bfqd, bfqq);
3958
Paolo Valenteaee69d72017-04-19 08:29:02 -06003959 /*
3960 * Expire bfqq, pretending that its budget expired, if bfqq
3961 * belongs to CLASS_IDLE and other queues are waiting for
3962 * service.
3963 */
Paolo Valente73d58112019-01-29 12:06:29 +01003964 if (!(bfq_tot_busy_queues(bfqd) > 1 && bfq_class_idle(bfqq)))
Paolo Valented0edc242018-09-14 16:23:08 +02003965 goto return_rq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003966
Paolo Valenteaee69d72017-04-19 08:29:02 -06003967 bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED);
Paolo Valented0edc242018-09-14 16:23:08 +02003968
3969return_rq:
Paolo Valenteaee69d72017-04-19 08:29:02 -06003970 return rq;
3971}
3972
3973static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
3974{
3975 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
3976
3977 /*
3978 * Avoiding lock: a race on bfqd->busy_queues should cause at
3979 * most a call to dispatch for nothing
3980 */
3981 return !list_empty_careful(&bfqd->dispatch) ||
Paolo Valente73d58112019-01-29 12:06:29 +01003982 bfq_tot_busy_queues(bfqd) > 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003983}
3984
3985static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
3986{
3987 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
3988 struct request *rq = NULL;
3989 struct bfq_queue *bfqq = NULL;
3990
3991 if (!list_empty(&bfqd->dispatch)) {
3992 rq = list_first_entry(&bfqd->dispatch, struct request,
3993 queuelist);
3994 list_del_init(&rq->queuelist);
3995
3996 bfqq = RQ_BFQQ(rq);
3997
3998 if (bfqq) {
3999 /*
4000 * Increment counters here, because this
4001 * dispatch does not follow the standard
4002 * dispatch flow (where counters are
4003 * incremented)
4004 */
4005 bfqq->dispatched++;
4006
4007 goto inc_in_driver_start_rq;
4008 }
4009
4010 /*
Paolo Valentea7877392018-02-07 22:19:20 +01004011 * We exploit the bfq_finish_requeue_request hook to
4012 * decrement rq_in_driver, but
4013 * bfq_finish_requeue_request will not be invoked on
4014 * this request. So, to avoid unbalance, just start
4015 * this request, without incrementing rq_in_driver. As
4016 * a negative consequence, rq_in_driver is deceptively
4017 * lower than it should be while this request is in
4018 * service. This may cause bfq_schedule_dispatch to be
4019 * invoked uselessly.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004020 *
4021 * As for implementing an exact solution, the
Paolo Valentea7877392018-02-07 22:19:20 +01004022 * bfq_finish_requeue_request hook, if defined, is
4023 * probably invoked also on this request. So, by
4024 * exploiting this hook, we could 1) increment
4025 * rq_in_driver here, and 2) decrement it in
4026 * bfq_finish_requeue_request. Such a solution would
4027 * let the value of the counter be always accurate,
4028 * but it would entail using an extra interface
4029 * function. This cost seems higher than the benefit,
4030 * being the frequency of non-elevator-private
Paolo Valenteaee69d72017-04-19 08:29:02 -06004031 * requests very low.
4032 */
4033 goto start_rq;
4034 }
4035
Paolo Valente73d58112019-01-29 12:06:29 +01004036 bfq_log(bfqd, "dispatch requests: %d busy queues",
4037 bfq_tot_busy_queues(bfqd));
Paolo Valenteaee69d72017-04-19 08:29:02 -06004038
Paolo Valente73d58112019-01-29 12:06:29 +01004039 if (bfq_tot_busy_queues(bfqd) == 0)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004040 goto exit;
4041
4042 /*
4043 * Force device to serve one request at a time if
4044 * strict_guarantees is true. Forcing this service scheme is
4045 * currently the ONLY way to guarantee that the request
4046 * service order enforced by the scheduler is respected by a
4047 * queueing device. Otherwise the device is free even to make
4048 * some unlucky request wait for as long as the device
4049 * wishes.
4050 *
4051 * Of course, serving one request at at time may cause loss of
4052 * throughput.
4053 */
4054 if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0)
4055 goto exit;
4056
4057 bfqq = bfq_select_queue(bfqd);
4058 if (!bfqq)
4059 goto exit;
4060
4061 rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq);
4062
4063 if (rq) {
4064inc_in_driver_start_rq:
4065 bfqd->rq_in_driver++;
4066start_rq:
4067 rq->rq_flags |= RQF_STARTED;
4068 }
4069exit:
4070 return rq;
4071}
4072
Paolo Valente9b25bd02017-12-04 11:42:05 +01004073#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
4074static void bfq_update_dispatch_stats(struct request_queue *q,
4075 struct request *rq,
4076 struct bfq_queue *in_serv_queue,
4077 bool idle_timer_disabled)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004078{
Paolo Valente9b25bd02017-12-04 11:42:05 +01004079 struct bfq_queue *bfqq = rq ? RQ_BFQQ(rq) : NULL;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004080
Paolo Valente24bfd192017-11-13 07:34:09 +01004081 if (!idle_timer_disabled && !bfqq)
Paolo Valente9b25bd02017-12-04 11:42:05 +01004082 return;
Paolo Valente24bfd192017-11-13 07:34:09 +01004083
4084 /*
4085 * rq and bfqq are guaranteed to exist until this function
4086 * ends, for the following reasons. First, rq can be
4087 * dispatched to the device, and then can be completed and
4088 * freed, only after this function ends. Second, rq cannot be
4089 * merged (and thus freed because of a merge) any longer,
4090 * because it has already started. Thus rq cannot be freed
4091 * before this function ends, and, since rq has a reference to
4092 * bfqq, the same guarantee holds for bfqq too.
4093 *
4094 * In addition, the following queue lock guarantees that
4095 * bfqq_group(bfqq) exists as well.
4096 */
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004097 spin_lock_irq(&q->queue_lock);
Paolo Valente24bfd192017-11-13 07:34:09 +01004098 if (idle_timer_disabled)
4099 /*
4100 * Since the idle timer has been disabled,
4101 * in_serv_queue contained some request when
4102 * __bfq_dispatch_request was invoked above, which
4103 * implies that rq was picked exactly from
4104 * in_serv_queue. Thus in_serv_queue == bfqq, and is
4105 * therefore guaranteed to exist because of the above
4106 * arguments.
4107 */
4108 bfqg_stats_update_idle_time(bfqq_group(in_serv_queue));
4109 if (bfqq) {
4110 struct bfq_group *bfqg = bfqq_group(bfqq);
4111
4112 bfqg_stats_update_avg_queue_size(bfqg);
4113 bfqg_stats_set_start_empty_time(bfqg);
4114 bfqg_stats_update_io_remove(bfqg, rq->cmd_flags);
4115 }
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004116 spin_unlock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004117}
4118#else
4119static inline void bfq_update_dispatch_stats(struct request_queue *q,
4120 struct request *rq,
4121 struct bfq_queue *in_serv_queue,
4122 bool idle_timer_disabled) {}
Paolo Valente24bfd192017-11-13 07:34:09 +01004123#endif
4124
Paolo Valente9b25bd02017-12-04 11:42:05 +01004125static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
4126{
4127 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4128 struct request *rq;
4129 struct bfq_queue *in_serv_queue;
4130 bool waiting_rq, idle_timer_disabled;
4131
4132 spin_lock_irq(&bfqd->lock);
4133
4134 in_serv_queue = bfqd->in_service_queue;
4135 waiting_rq = in_serv_queue && bfq_bfqq_wait_request(in_serv_queue);
4136
4137 rq = __bfq_dispatch_request(hctx);
4138
4139 idle_timer_disabled =
4140 waiting_rq && !bfq_bfqq_wait_request(in_serv_queue);
4141
4142 spin_unlock_irq(&bfqd->lock);
4143
4144 bfq_update_dispatch_stats(hctx->queue, rq, in_serv_queue,
4145 idle_timer_disabled);
4146
Paolo Valenteaee69d72017-04-19 08:29:02 -06004147 return rq;
4148}
4149
4150/*
4151 * Task holds one reference to the queue, dropped when task exits. Each rq
4152 * in-flight on this queue also holds a reference, dropped when rq is freed.
4153 *
4154 * Scheduler lock must be held here. Recall not to use bfqq after calling
4155 * this function on it.
4156 */
Paolo Valenteea25da42017-04-19 08:48:24 -06004157void bfq_put_queue(struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004158{
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004159#ifdef CONFIG_BFQ_GROUP_IOSCHED
4160 struct bfq_group *bfqg = bfqq_group(bfqq);
4161#endif
4162
Paolo Valenteaee69d72017-04-19 08:29:02 -06004163 if (bfqq->bfqd)
4164 bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
4165 bfqq, bfqq->ref);
4166
4167 bfqq->ref--;
4168 if (bfqq->ref)
4169 return;
4170
Paolo Valente99fead82017-10-09 13:11:23 +02004171 if (!hlist_unhashed(&bfqq->burst_list_node)) {
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004172 hlist_del_init(&bfqq->burst_list_node);
Paolo Valente99fead82017-10-09 13:11:23 +02004173 /*
4174 * Decrement also burst size after the removal, if the
4175 * process associated with bfqq is exiting, and thus
4176 * does not contribute to the burst any longer. This
4177 * decrement helps filter out false positives of large
4178 * bursts, when some short-lived process (often due to
4179 * the execution of commands by some service) happens
4180 * to start and exit while a complex application is
4181 * starting, and thus spawning several processes that
4182 * do I/O (and that *must not* be treated as a large
4183 * burst, see comments on bfq_handle_burst).
4184 *
4185 * In particular, the decrement is performed only if:
4186 * 1) bfqq is not a merged queue, because, if it is,
4187 * then this free of bfqq is not triggered by the exit
4188 * of the process bfqq is associated with, but exactly
4189 * by the fact that bfqq has just been merged.
4190 * 2) burst_size is greater than 0, to handle
4191 * unbalanced decrements. Unbalanced decrements may
4192 * happen in te following case: bfqq is inserted into
4193 * the current burst list--without incrementing
4194 * bust_size--because of a split, but the current
4195 * burst list is not the burst list bfqq belonged to
4196 * (see comments on the case of a split in
4197 * bfq_set_request).
4198 */
4199 if (bfqq->bic && bfqq->bfqd->burst_size > 0)
4200 bfqq->bfqd->burst_size--;
Paolo Valente7cb04002017-09-21 11:04:03 +02004201 }
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004202
Paolo Valenteaee69d72017-04-19 08:29:02 -06004203 kmem_cache_free(bfq_pool, bfqq);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004204#ifdef CONFIG_BFQ_GROUP_IOSCHED
Paolo Valente8f9bebc2017-06-05 10:11:15 +02004205 bfqg_and_blkg_put(bfqg);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004206#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06004207}
4208
Arianna Avanzini36eca892017-04-12 18:23:16 +02004209static void bfq_put_cooperator(struct bfq_queue *bfqq)
4210{
4211 struct bfq_queue *__bfqq, *next;
4212
4213 /*
4214 * If this queue was scheduled to merge with another queue, be
4215 * sure to drop the reference taken on that queue (and others in
4216 * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
4217 */
4218 __bfqq = bfqq->new_bfqq;
4219 while (__bfqq) {
4220 if (__bfqq == bfqq)
4221 break;
4222 next = __bfqq->new_bfqq;
4223 bfq_put_queue(__bfqq);
4224 __bfqq = next;
4225 }
4226}
4227
Paolo Valenteaee69d72017-04-19 08:29:02 -06004228static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
4229{
4230 if (bfqq == bfqd->in_service_queue) {
4231 __bfq_bfqq_expire(bfqd, bfqq);
4232 bfq_schedule_dispatch(bfqd);
4233 }
4234
4235 bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref);
4236
Arianna Avanzini36eca892017-04-12 18:23:16 +02004237 bfq_put_cooperator(bfqq);
4238
Paolo Valenteaee69d72017-04-19 08:29:02 -06004239 bfq_put_queue(bfqq); /* release process reference */
4240}
4241
4242static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync)
4243{
4244 struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
4245 struct bfq_data *bfqd;
4246
4247 if (bfqq)
4248 bfqd = bfqq->bfqd; /* NULL if scheduler already exited */
4249
4250 if (bfqq && bfqd) {
4251 unsigned long flags;
4252
4253 spin_lock_irqsave(&bfqd->lock, flags);
4254 bfq_exit_bfqq(bfqd, bfqq);
4255 bic_set_bfqq(bic, NULL, is_sync);
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02004256 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004257 }
4258}
4259
4260static void bfq_exit_icq(struct io_cq *icq)
4261{
4262 struct bfq_io_cq *bic = icq_to_bic(icq);
4263
4264 bfq_exit_icq_bfqq(bic, true);
4265 bfq_exit_icq_bfqq(bic, false);
4266}
4267
4268/*
4269 * Update the entity prio values; note that the new values will not
4270 * be used until the next (re)activation.
4271 */
4272static void
4273bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
4274{
4275 struct task_struct *tsk = current;
4276 int ioprio_class;
4277 struct bfq_data *bfqd = bfqq->bfqd;
4278
4279 if (!bfqd)
4280 return;
4281
4282 ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
4283 switch (ioprio_class) {
4284 default:
4285 dev_err(bfqq->bfqd->queue->backing_dev_info->dev,
4286 "bfq: bad prio class %d\n", ioprio_class);
Bart Van Asschefa393d12017-08-30 11:42:07 -07004287 /* fall through */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004288 case IOPRIO_CLASS_NONE:
4289 /*
4290 * No prio set, inherit CPU scheduling settings.
4291 */
4292 bfqq->new_ioprio = task_nice_ioprio(tsk);
4293 bfqq->new_ioprio_class = task_nice_ioclass(tsk);
4294 break;
4295 case IOPRIO_CLASS_RT:
4296 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4297 bfqq->new_ioprio_class = IOPRIO_CLASS_RT;
4298 break;
4299 case IOPRIO_CLASS_BE:
4300 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4301 bfqq->new_ioprio_class = IOPRIO_CLASS_BE;
4302 break;
4303 case IOPRIO_CLASS_IDLE:
4304 bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE;
4305 bfqq->new_ioprio = 7;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004306 break;
4307 }
4308
4309 if (bfqq->new_ioprio >= IOPRIO_BE_NR) {
4310 pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n",
4311 bfqq->new_ioprio);
4312 bfqq->new_ioprio = IOPRIO_BE_NR;
4313 }
4314
4315 bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio);
4316 bfqq->entity.prio_changed = 1;
4317}
4318
Paolo Valenteea25da42017-04-19 08:48:24 -06004319static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
4320 struct bio *bio, bool is_sync,
4321 struct bfq_io_cq *bic);
4322
Paolo Valenteaee69d72017-04-19 08:29:02 -06004323static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio)
4324{
4325 struct bfq_data *bfqd = bic_to_bfqd(bic);
4326 struct bfq_queue *bfqq;
4327 int ioprio = bic->icq.ioc->ioprio;
4328
4329 /*
4330 * This condition may trigger on a newly created bic, be sure to
4331 * drop the lock before returning.
4332 */
4333 if (unlikely(!bfqd) || likely(bic->ioprio == ioprio))
4334 return;
4335
4336 bic->ioprio = ioprio;
4337
4338 bfqq = bic_to_bfqq(bic, false);
4339 if (bfqq) {
4340 /* release process reference on this queue */
4341 bfq_put_queue(bfqq);
4342 bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic);
4343 bic_set_bfqq(bic, bfqq, false);
4344 }
4345
4346 bfqq = bic_to_bfqq(bic, true);
4347 if (bfqq)
4348 bfq_set_next_ioprio_data(bfqq, bic);
4349}
4350
4351static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4352 struct bfq_io_cq *bic, pid_t pid, int is_sync)
4353{
4354 RB_CLEAR_NODE(&bfqq->entity.rb_node);
4355 INIT_LIST_HEAD(&bfqq->fifo);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004356 INIT_HLIST_NODE(&bfqq->burst_list_node);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004357
4358 bfqq->ref = 0;
4359 bfqq->bfqd = bfqd;
4360
4361 if (bic)
4362 bfq_set_next_ioprio_data(bfqq, bic);
4363
4364 if (is_sync) {
Paolo Valented5be3fe2017-08-04 07:35:10 +02004365 /*
4366 * No need to mark as has_short_ttime if in
4367 * idle_class, because no device idling is performed
4368 * for queues in idle class
4369 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004370 if (!bfq_class_idle(bfqq))
Paolo Valented5be3fe2017-08-04 07:35:10 +02004371 /* tentatively mark as has_short_ttime */
4372 bfq_mark_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004373 bfq_mark_bfqq_sync(bfqq);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004374 bfq_mark_bfqq_just_created(bfqq);
Paolo Valented0edc242018-09-14 16:23:08 +02004375 /*
4376 * Aggressively inject a lot of service: up to 90%.
4377 * This coefficient remains constant during bfqq life,
4378 * but this behavior might be changed, after enough
4379 * testing and tuning.
4380 */
4381 bfqq->inject_coeff = 1;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004382 } else
4383 bfq_clear_bfqq_sync(bfqq);
4384
4385 /* set end request to minus infinity from now */
4386 bfqq->ttime.last_end_request = ktime_get_ns() + 1;
4387
4388 bfq_mark_bfqq_IO_bound(bfqq);
4389
4390 bfqq->pid = pid;
4391
4392 /* Tentative initial value to trade off between thr and lat */
Paolo Valente54b60452017-04-12 18:23:09 +02004393 bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004394 bfqq->budget_timeout = bfq_smallest_from_now();
Paolo Valenteaee69d72017-04-19 08:29:02 -06004395
Paolo Valente44e44a12017-04-12 18:23:12 +02004396 bfqq->wr_coeff = 1;
Arianna Avanzini36eca892017-04-12 18:23:16 +02004397 bfqq->last_wr_start_finish = jiffies;
Paolo Valente77b7dce2017-04-12 18:23:13 +02004398 bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now();
Arianna Avanzini36eca892017-04-12 18:23:16 +02004399 bfqq->split_time = bfq_smallest_from_now();
Paolo Valente77b7dce2017-04-12 18:23:13 +02004400
4401 /*
Paolo Valentea34b0242017-12-15 07:23:12 +01004402 * To not forget the possibly high bandwidth consumed by a
4403 * process/queue in the recent past,
4404 * bfq_bfqq_softrt_next_start() returns a value at least equal
4405 * to the current value of bfqq->soft_rt_next_start (see
4406 * comments on bfq_bfqq_softrt_next_start). Set
4407 * soft_rt_next_start to now, to mean that bfqq has consumed
4408 * no bandwidth so far.
Paolo Valente77b7dce2017-04-12 18:23:13 +02004409 */
Paolo Valentea34b0242017-12-15 07:23:12 +01004410 bfqq->soft_rt_next_start = jiffies;
Paolo Valente44e44a12017-04-12 18:23:12 +02004411
Paolo Valenteaee69d72017-04-19 08:29:02 -06004412 /* first request is almost certainly seeky */
4413 bfqq->seek_history = 1;
4414}
4415
4416static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004417 struct bfq_group *bfqg,
Paolo Valenteaee69d72017-04-19 08:29:02 -06004418 int ioprio_class, int ioprio)
4419{
4420 switch (ioprio_class) {
4421 case IOPRIO_CLASS_RT:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004422 return &bfqg->async_bfqq[0][ioprio];
Paolo Valenteaee69d72017-04-19 08:29:02 -06004423 case IOPRIO_CLASS_NONE:
4424 ioprio = IOPRIO_NORM;
4425 /* fall through */
4426 case IOPRIO_CLASS_BE:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004427 return &bfqg->async_bfqq[1][ioprio];
Paolo Valenteaee69d72017-04-19 08:29:02 -06004428 case IOPRIO_CLASS_IDLE:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004429 return &bfqg->async_idle_bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004430 default:
4431 return NULL;
4432 }
4433}
4434
4435static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
4436 struct bio *bio, bool is_sync,
4437 struct bfq_io_cq *bic)
4438{
4439 const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4440 const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
4441 struct bfq_queue **async_bfqq = NULL;
4442 struct bfq_queue *bfqq;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004443 struct bfq_group *bfqg;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004444
4445 rcu_read_lock();
4446
Dennis Zhou0fe061b2018-12-05 12:10:26 -05004447 bfqg = bfq_find_set_group(bfqd, __bio_blkcg(bio));
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004448 if (!bfqg) {
4449 bfqq = &bfqd->oom_bfqq;
4450 goto out;
4451 }
4452
Paolo Valenteaee69d72017-04-19 08:29:02 -06004453 if (!is_sync) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004454 async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
Paolo Valenteaee69d72017-04-19 08:29:02 -06004455 ioprio);
4456 bfqq = *async_bfqq;
4457 if (bfqq)
4458 goto out;
4459 }
4460
4461 bfqq = kmem_cache_alloc_node(bfq_pool,
4462 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
4463 bfqd->queue->node);
4464
4465 if (bfqq) {
4466 bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
4467 is_sync);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004468 bfq_init_entity(&bfqq->entity, bfqg);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004469 bfq_log_bfqq(bfqd, bfqq, "allocated");
4470 } else {
4471 bfqq = &bfqd->oom_bfqq;
4472 bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
4473 goto out;
4474 }
4475
4476 /*
4477 * Pin the queue now that it's allocated, scheduler exit will
4478 * prune it.
4479 */
4480 if (async_bfqq) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004481 bfqq->ref++; /*
4482 * Extra group reference, w.r.t. sync
4483 * queue. This extra reference is removed
4484 * only if bfqq->bfqg disappears, to
4485 * guarantee that this queue is not freed
4486 * until its group goes away.
4487 */
4488 bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
Paolo Valenteaee69d72017-04-19 08:29:02 -06004489 bfqq, bfqq->ref);
4490 *async_bfqq = bfqq;
4491 }
4492
4493out:
4494 bfqq->ref++; /* get a process reference to this queue */
4495 bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref);
4496 rcu_read_unlock();
4497 return bfqq;
4498}
4499
4500static void bfq_update_io_thinktime(struct bfq_data *bfqd,
4501 struct bfq_queue *bfqq)
4502{
4503 struct bfq_ttime *ttime = &bfqq->ttime;
4504 u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request;
4505
4506 elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle);
4507
4508 ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8;
4509 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
4510 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
4511 ttime->ttime_samples);
4512}
4513
4514static void
4515bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4516 struct request *rq)
4517{
Paolo Valenteaee69d72017-04-19 08:29:02 -06004518 bfqq->seek_history <<= 1;
Paolo Valented87447d2019-01-29 12:06:33 +01004519 bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004520}
4521
Paolo Valented5be3fe2017-08-04 07:35:10 +02004522static void bfq_update_has_short_ttime(struct bfq_data *bfqd,
4523 struct bfq_queue *bfqq,
4524 struct bfq_io_cq *bic)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004525{
Paolo Valented5be3fe2017-08-04 07:35:10 +02004526 bool has_short_ttime = true;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004527
Paolo Valented5be3fe2017-08-04 07:35:10 +02004528 /*
4529 * No need to update has_short_ttime if bfqq is async or in
4530 * idle io prio class, or if bfq_slice_idle is zero, because
4531 * no device idling is performed for bfqq in this case.
4532 */
4533 if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq) ||
4534 bfqd->bfq_slice_idle == 0)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004535 return;
4536
Arianna Avanzini36eca892017-04-12 18:23:16 +02004537 /* Idle window just restored, statistics are meaningless. */
4538 if (time_is_after_eq_jiffies(bfqq->split_time +
4539 bfqd->bfq_wr_min_idle_time))
4540 return;
4541
Paolo Valented5be3fe2017-08-04 07:35:10 +02004542 /* Think time is infinite if no process is linked to
4543 * bfqq. Otherwise check average think time to
4544 * decide whether to mark as has_short_ttime
4545 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004546 if (atomic_read(&bic->icq.ioc->active_ref) == 0 ||
Paolo Valented5be3fe2017-08-04 07:35:10 +02004547 (bfq_sample_valid(bfqq->ttime.ttime_samples) &&
4548 bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle))
4549 has_short_ttime = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004550
Paolo Valented5be3fe2017-08-04 07:35:10 +02004551 bfq_log_bfqq(bfqd, bfqq, "update_has_short_ttime: has_short_ttime %d",
4552 has_short_ttime);
4553
4554 if (has_short_ttime)
4555 bfq_mark_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004556 else
Paolo Valented5be3fe2017-08-04 07:35:10 +02004557 bfq_clear_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004558}
4559
4560/*
4561 * Called when a new fs request (rq) is added to bfqq. Check if there's
4562 * something we should do about it.
4563 */
4564static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4565 struct request *rq)
4566{
4567 struct bfq_io_cq *bic = RQ_BIC(rq);
4568
4569 if (rq->cmd_flags & REQ_META)
4570 bfqq->meta_pending++;
4571
4572 bfq_update_io_thinktime(bfqd, bfqq);
Paolo Valented5be3fe2017-08-04 07:35:10 +02004573 bfq_update_has_short_ttime(bfqd, bfqq, bic);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004574 bfq_update_io_seektime(bfqd, bfqq, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004575
4576 bfq_log_bfqq(bfqd, bfqq,
Paolo Valented5be3fe2017-08-04 07:35:10 +02004577 "rq_enqueued: has_short_ttime=%d (seeky %d)",
4578 bfq_bfqq_has_short_ttime(bfqq), BFQQ_SEEKY(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06004579
4580 bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
4581
4582 if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
4583 bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
4584 blk_rq_sectors(rq) < 32;
4585 bool budget_timeout = bfq_bfqq_budget_timeout(bfqq);
4586
4587 /*
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004588 * There is just this request queued: if
4589 * - the request is small, and
4590 * - we are idling to boost throughput, and
4591 * - the queue is not to be expired,
4592 * then just exit.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004593 *
4594 * In this way, if the device is being idled to wait
4595 * for a new request from the in-service queue, we
4596 * avoid unplugging the device and committing the
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004597 * device to serve just a small request. In contrast
4598 * we wait for the block layer to decide when to
4599 * unplug the device: hopefully, new requests will be
4600 * merged to this one quickly, then the device will be
4601 * unplugged and larger requests will be dispatched.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004602 */
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004603 if (small_req && idling_boosts_thr_without_issues(bfqd, bfqq) &&
4604 !budget_timeout)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004605 return;
4606
4607 /*
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004608 * A large enough request arrived, or idling is being
4609 * performed to preserve service guarantees, or
4610 * finally the queue is to be expired: in all these
4611 * cases disk idling is to be stopped, so clear
4612 * wait_request flag and reset timer.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004613 */
4614 bfq_clear_bfqq_wait_request(bfqq);
4615 hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
4616
4617 /*
4618 * The queue is not empty, because a new request just
4619 * arrived. Hence we can safely expire the queue, in
4620 * case of budget timeout, without risking that the
4621 * timestamps of the queue are not updated correctly.
4622 * See [1] for more details.
4623 */
4624 if (budget_timeout)
4625 bfq_bfqq_expire(bfqd, bfqq, false,
4626 BFQQE_BUDGET_TIMEOUT);
4627 }
4628}
4629
Paolo Valente24bfd192017-11-13 07:34:09 +01004630/* returns true if it causes the idle timer to be disabled */
4631static bool __bfq_insert_request(struct bfq_data *bfqd, struct request *rq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004632{
Arianna Avanzini36eca892017-04-12 18:23:16 +02004633 struct bfq_queue *bfqq = RQ_BFQQ(rq),
4634 *new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
Paolo Valente24bfd192017-11-13 07:34:09 +01004635 bool waiting, idle_timer_disabled = false;
Arianna Avanzini36eca892017-04-12 18:23:16 +02004636
4637 if (new_bfqq) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02004638 /*
4639 * Release the request's reference to the old bfqq
4640 * and make sure one is taken to the shared queue.
4641 */
4642 new_bfqq->allocated++;
4643 bfqq->allocated--;
4644 new_bfqq->ref++;
4645 /*
4646 * If the bic associated with the process
4647 * issuing this request still points to bfqq
4648 * (and thus has not been already redirected
4649 * to new_bfqq or even some other bfq_queue),
4650 * then complete the merge and redirect it to
4651 * new_bfqq.
4652 */
4653 if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
4654 bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
4655 bfqq, new_bfqq);
Paolo Valente894df932017-09-21 11:04:02 +02004656
4657 bfq_clear_bfqq_just_created(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02004658 /*
4659 * rq is about to be enqueued into new_bfqq,
4660 * release rq reference on bfqq
4661 */
4662 bfq_put_queue(bfqq);
4663 rq->elv.priv[1] = new_bfqq;
4664 bfqq = new_bfqq;
4665 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06004666
Paolo Valente24bfd192017-11-13 07:34:09 +01004667 waiting = bfqq && bfq_bfqq_wait_request(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004668 bfq_add_request(rq);
Paolo Valente24bfd192017-11-13 07:34:09 +01004669 idle_timer_disabled = waiting && !bfq_bfqq_wait_request(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004670
4671 rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)];
4672 list_add_tail(&rq->queuelist, &bfqq->fifo);
4673
4674 bfq_rq_enqueued(bfqd, bfqq, rq);
Paolo Valente24bfd192017-11-13 07:34:09 +01004675
4676 return idle_timer_disabled;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004677}
4678
Paolo Valente9b25bd02017-12-04 11:42:05 +01004679#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
4680static void bfq_update_insert_stats(struct request_queue *q,
4681 struct bfq_queue *bfqq,
4682 bool idle_timer_disabled,
4683 unsigned int cmd_flags)
4684{
4685 if (!bfqq)
4686 return;
4687
4688 /*
4689 * bfqq still exists, because it can disappear only after
4690 * either it is merged with another queue, or the process it
4691 * is associated with exits. But both actions must be taken by
4692 * the same process currently executing this flow of
4693 * instructions.
4694 *
4695 * In addition, the following queue lock guarantees that
4696 * bfqq_group(bfqq) exists as well.
4697 */
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004698 spin_lock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004699 bfqg_stats_update_io_add(bfqq_group(bfqq), bfqq, cmd_flags);
4700 if (idle_timer_disabled)
4701 bfqg_stats_update_idle_time(bfqq_group(bfqq));
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004702 spin_unlock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004703}
4704#else
4705static inline void bfq_update_insert_stats(struct request_queue *q,
4706 struct bfq_queue *bfqq,
4707 bool idle_timer_disabled,
4708 unsigned int cmd_flags) {}
4709#endif
4710
Paolo Valenteaee69d72017-04-19 08:29:02 -06004711static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
4712 bool at_head)
4713{
4714 struct request_queue *q = hctx->queue;
4715 struct bfq_data *bfqd = q->elevator->elevator_data;
Paolo Valente18e5a572018-05-04 19:17:01 +02004716 struct bfq_queue *bfqq;
Paolo Valente24bfd192017-11-13 07:34:09 +01004717 bool idle_timer_disabled = false;
4718 unsigned int cmd_flags;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004719
4720 spin_lock_irq(&bfqd->lock);
4721 if (blk_mq_sched_try_insert_merge(q, rq)) {
4722 spin_unlock_irq(&bfqd->lock);
4723 return;
4724 }
4725
4726 spin_unlock_irq(&bfqd->lock);
4727
4728 blk_mq_sched_request_inserted(rq);
4729
4730 spin_lock_irq(&bfqd->lock);
Paolo Valente18e5a572018-05-04 19:17:01 +02004731 bfqq = bfq_init_rq(rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004732 if (at_head || blk_rq_is_passthrough(rq)) {
4733 if (at_head)
4734 list_add(&rq->queuelist, &bfqd->dispatch);
4735 else
4736 list_add_tail(&rq->queuelist, &bfqd->dispatch);
Paolo Valente18e5a572018-05-04 19:17:01 +02004737 } else { /* bfqq is assumed to be non null here */
Paolo Valente24bfd192017-11-13 07:34:09 +01004738 idle_timer_disabled = __bfq_insert_request(bfqd, rq);
Luca Miccio614822f2017-11-13 07:34:08 +01004739 /*
4740 * Update bfqq, because, if a queue merge has occurred
4741 * in __bfq_insert_request, then rq has been
4742 * redirected into a new queue.
4743 */
4744 bfqq = RQ_BFQQ(rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004745
4746 if (rq_mergeable(rq)) {
4747 elv_rqhash_add(q, rq);
4748 if (!q->last_merge)
4749 q->last_merge = rq;
4750 }
4751 }
4752
Paolo Valente24bfd192017-11-13 07:34:09 +01004753 /*
4754 * Cache cmd_flags before releasing scheduler lock, because rq
4755 * may disappear afterwards (for example, because of a request
4756 * merge).
4757 */
4758 cmd_flags = rq->cmd_flags;
Paolo Valente9b25bd02017-12-04 11:42:05 +01004759
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02004760 spin_unlock_irq(&bfqd->lock);
Paolo Valente24bfd192017-11-13 07:34:09 +01004761
Paolo Valente9b25bd02017-12-04 11:42:05 +01004762 bfq_update_insert_stats(q, bfqq, idle_timer_disabled,
4763 cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004764}
4765
4766static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
4767 struct list_head *list, bool at_head)
4768{
4769 while (!list_empty(list)) {
4770 struct request *rq;
4771
4772 rq = list_first_entry(list, struct request, queuelist);
4773 list_del_init(&rq->queuelist);
4774 bfq_insert_request(hctx, rq, at_head);
4775 }
4776}
4777
4778static void bfq_update_hw_tag(struct bfq_data *bfqd)
4779{
4780 bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver,
4781 bfqd->rq_in_driver);
4782
4783 if (bfqd->hw_tag == 1)
4784 return;
4785
4786 /*
4787 * This sample is valid if the number of outstanding requests
4788 * is large enough to allow a queueing behavior. Note that the
4789 * sum is not exact, as it's not taking into account deactivated
4790 * requests.
4791 */
4792 if (bfqd->rq_in_driver + bfqd->queued < BFQ_HW_QUEUE_THRESHOLD)
4793 return;
4794
4795 if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
4796 return;
4797
4798 bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
4799 bfqd->max_rq_in_driver = 0;
4800 bfqd->hw_tag_samples = 0;
4801}
4802
4803static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd)
4804{
Paolo Valenteab0e43e2017-04-12 18:23:10 +02004805 u64 now_ns;
4806 u32 delta_us;
4807
Paolo Valenteaee69d72017-04-19 08:29:02 -06004808 bfq_update_hw_tag(bfqd);
4809
4810 bfqd->rq_in_driver--;
4811 bfqq->dispatched--;
4812
Paolo Valente44e44a12017-04-12 18:23:12 +02004813 if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
4814 /*
4815 * Set budget_timeout (which we overload to store the
4816 * time at which the queue remains with no backlog and
4817 * no outstanding request; used by the weight-raising
4818 * mechanism).
4819 */
4820 bfqq->budget_timeout = jiffies;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02004821
Paolo Valente04715592018-06-25 21:55:34 +02004822 bfq_weights_tree_remove(bfqd, bfqq);
Paolo Valente44e44a12017-04-12 18:23:12 +02004823 }
4824
Paolo Valenteab0e43e2017-04-12 18:23:10 +02004825 now_ns = ktime_get_ns();
4826
4827 bfqq->ttime.last_end_request = now_ns;
4828
4829 /*
4830 * Using us instead of ns, to get a reasonable precision in
4831 * computing rate in next check.
4832 */
4833 delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC);
4834
4835 /*
4836 * If the request took rather long to complete, and, according
4837 * to the maximum request size recorded, this completion latency
4838 * implies that the request was certainly served at a very low
4839 * rate (less than 1M sectors/sec), then the whole observation
4840 * interval that lasts up to this time instant cannot be a
4841 * valid time interval for computing a new peak rate. Invoke
4842 * bfq_update_rate_reset to have the following three steps
4843 * taken:
4844 * - close the observation interval at the last (previous)
4845 * request dispatch or completion
4846 * - compute rate, if possible, for that observation interval
4847 * - reset to zero samples, which will trigger a proper
4848 * re-initialization of the observation interval on next
4849 * dispatch
4850 */
4851 if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC &&
4852 (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us <
4853 1UL<<(BFQ_RATE_SHIFT - 10))
4854 bfq_update_rate_reset(bfqd, NULL);
4855 bfqd->last_completion = now_ns;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004856
4857 /*
Paolo Valente77b7dce2017-04-12 18:23:13 +02004858 * If we are waiting to discover whether the request pattern
4859 * of the task associated with the queue is actually
4860 * isochronous, and both requisites for this condition to hold
4861 * are now satisfied, then compute soft_rt_next_start (see the
4862 * comments on the function bfq_bfqq_softrt_next_start()). We
Paolo Valente20cd3242019-01-29 12:06:25 +01004863 * do not compute soft_rt_next_start if bfqq is in interactive
4864 * weight raising (see the comments in bfq_bfqq_expire() for
4865 * an explanation). We schedule this delayed update when bfqq
4866 * expires, if it still has in-flight requests.
Paolo Valente77b7dce2017-04-12 18:23:13 +02004867 */
4868 if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
Paolo Valente20cd3242019-01-29 12:06:25 +01004869 RB_EMPTY_ROOT(&bfqq->sort_list) &&
4870 bfqq->wr_coeff != bfqd->bfq_wr_coeff)
Paolo Valente77b7dce2017-04-12 18:23:13 +02004871 bfqq->soft_rt_next_start =
4872 bfq_bfqq_softrt_next_start(bfqd, bfqq);
4873
4874 /*
Paolo Valenteaee69d72017-04-19 08:29:02 -06004875 * If this is the in-service queue, check if it needs to be expired,
4876 * or if we want to idle in case it has no pending requests.
4877 */
4878 if (bfqd->in_service_queue == bfqq) {
Paolo Valente4420b092018-06-25 21:55:35 +02004879 if (bfq_bfqq_must_idle(bfqq)) {
4880 if (bfqq->dispatched == 0)
4881 bfq_arm_slice_timer(bfqd);
4882 /*
4883 * If we get here, we do not expire bfqq, even
4884 * if bfqq was in budget timeout or had no
4885 * more requests (as controlled in the next
4886 * conditional instructions). The reason for
4887 * not expiring bfqq is as follows.
4888 *
4889 * Here bfqq->dispatched > 0 holds, but
4890 * bfq_bfqq_must_idle() returned true. This
4891 * implies that, even if no request arrives
4892 * for bfqq before bfqq->dispatched reaches 0,
4893 * bfqq will, however, not be expired on the
4894 * completion event that causes bfqq->dispatch
4895 * to reach zero. In contrast, on this event,
4896 * bfqq will start enjoying device idling
4897 * (I/O-dispatch plugging).
4898 *
4899 * But, if we expired bfqq here, bfqq would
4900 * not have the chance to enjoy device idling
4901 * when bfqq->dispatched finally reaches
4902 * zero. This would expose bfqq to violation
4903 * of its reserved service guarantees.
4904 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004905 return;
4906 } else if (bfq_may_expire_for_budg_timeout(bfqq))
4907 bfq_bfqq_expire(bfqd, bfqq, false,
4908 BFQQE_BUDGET_TIMEOUT);
4909 else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
4910 (bfqq->dispatched == 0 ||
Paolo Valente277a4a92018-06-25 21:55:37 +02004911 !bfq_better_to_idle(bfqq)))
Paolo Valenteaee69d72017-04-19 08:29:02 -06004912 bfq_bfqq_expire(bfqd, bfqq, false,
4913 BFQQE_NO_MORE_REQUESTS);
4914 }
Hou Tao3f7cb4f2017-07-11 21:58:15 +08004915
4916 if (!bfqd->rq_in_driver)
4917 bfq_schedule_dispatch(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004918}
4919
Paolo Valentea7877392018-02-07 22:19:20 +01004920static void bfq_finish_requeue_request_body(struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004921{
4922 bfqq->allocated--;
4923
4924 bfq_put_queue(bfqq);
4925}
4926
Paolo Valentea7877392018-02-07 22:19:20 +01004927/*
4928 * Handle either a requeue or a finish for rq. The things to do are
4929 * the same in both cases: all references to rq are to be dropped. In
4930 * particular, rq is considered completed from the point of view of
4931 * the scheduler.
4932 */
4933static void bfq_finish_requeue_request(struct request *rq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004934{
Paolo Valentea7877392018-02-07 22:19:20 +01004935 struct bfq_queue *bfqq = RQ_BFQQ(rq);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02004936 struct bfq_data *bfqd;
4937
Paolo Valentea7877392018-02-07 22:19:20 +01004938 /*
4939 * Requeue and finish hooks are invoked in blk-mq without
4940 * checking whether the involved request is actually still
4941 * referenced in the scheduler. To handle this fact, the
4942 * following two checks make this function exit in case of
4943 * spurious invocations, for which there is nothing to do.
4944 *
4945 * First, check whether rq has nothing to do with an elevator.
4946 */
4947 if (unlikely(!(rq->rq_flags & RQF_ELVPRIV)))
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02004948 return;
4949
Paolo Valentea7877392018-02-07 22:19:20 +01004950 /*
4951 * rq either is not associated with any icq, or is an already
4952 * requeued request that has not (yet) been re-inserted into
4953 * a bfq_queue.
4954 */
4955 if (!rq->elv.icq || !bfqq)
4956 return;
4957
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02004958 bfqd = bfqq->bfqd;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004959
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004960 if (rq->rq_flags & RQF_STARTED)
4961 bfqg_stats_update_completion(bfqq_group(bfqq),
Omar Sandoval522a7772018-05-09 02:08:53 -07004962 rq->start_time_ns,
4963 rq->io_start_time_ns,
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004964 rq->cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004965
4966 if (likely(rq->rq_flags & RQF_STARTED)) {
4967 unsigned long flags;
4968
4969 spin_lock_irqsave(&bfqd->lock, flags);
4970
4971 bfq_completed_request(bfqq, bfqd);
Paolo Valentea7877392018-02-07 22:19:20 +01004972 bfq_finish_requeue_request_body(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004973
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02004974 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004975 } else {
4976 /*
4977 * Request rq may be still/already in the scheduler,
Paolo Valentea7877392018-02-07 22:19:20 +01004978 * in which case we need to remove it (this should
4979 * never happen in case of requeue). And we cannot
Paolo Valenteaee69d72017-04-19 08:29:02 -06004980 * defer such a check and removal, to avoid
4981 * inconsistencies in the time interval from the end
4982 * of this function to the start of the deferred work.
4983 * This situation seems to occur only in process
4984 * context, as a consequence of a merge. In the
4985 * current version of the code, this implies that the
4986 * lock is held.
4987 */
4988
Luca Miccio614822f2017-11-13 07:34:08 +01004989 if (!RB_EMPTY_NODE(&rq->rb_node)) {
Christoph Hellwig7b9e9362017-06-16 18:15:21 +02004990 bfq_remove_request(rq->q, rq);
Luca Miccio614822f2017-11-13 07:34:08 +01004991 bfqg_stats_update_io_remove(bfqq_group(bfqq),
4992 rq->cmd_flags);
4993 }
Paolo Valentea7877392018-02-07 22:19:20 +01004994 bfq_finish_requeue_request_body(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004995 }
4996
Paolo Valentea7877392018-02-07 22:19:20 +01004997 /*
4998 * Reset private fields. In case of a requeue, this allows
4999 * this function to correctly do nothing if it is spuriously
5000 * invoked again on this same request (see the check at the
5001 * beginning of the function). Probably, a better general
5002 * design would be to prevent blk-mq from invoking the requeue
5003 * or finish hooks of an elevator, for a request that is not
5004 * referred by that elevator.
5005 *
5006 * Resetting the following fields would break the
5007 * request-insertion logic if rq is re-inserted into a bfq
5008 * internal queue, without a re-preparation. Here we assume
5009 * that re-insertions of requeued requests, without
5010 * re-preparation, can happen only for pass_through or at_head
5011 * requests (which are not re-inserted into bfq internal
5012 * queues).
5013 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06005014 rq->elv.priv[0] = NULL;
5015 rq->elv.priv[1] = NULL;
5016}
5017
5018/*
Arianna Avanzini36eca892017-04-12 18:23:16 +02005019 * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
5020 * was the last process referring to that bfqq.
5021 */
5022static struct bfq_queue *
5023bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
5024{
5025 bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
5026
5027 if (bfqq_process_refs(bfqq) == 1) {
5028 bfqq->pid = current->pid;
5029 bfq_clear_bfqq_coop(bfqq);
5030 bfq_clear_bfqq_split_coop(bfqq);
5031 return bfqq;
5032 }
5033
5034 bic_set_bfqq(bic, NULL, 1);
5035
5036 bfq_put_cooperator(bfqq);
5037
5038 bfq_put_queue(bfqq);
5039 return NULL;
5040}
5041
5042static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,
5043 struct bfq_io_cq *bic,
5044 struct bio *bio,
5045 bool split, bool is_sync,
5046 bool *new_queue)
5047{
5048 struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
5049
5050 if (likely(bfqq && bfqq != &bfqd->oom_bfqq))
5051 return bfqq;
5052
5053 if (new_queue)
5054 *new_queue = true;
5055
5056 if (bfqq)
5057 bfq_put_queue(bfqq);
5058 bfqq = bfq_get_queue(bfqd, bio, is_sync, bic);
5059
5060 bic_set_bfqq(bic, bfqq, is_sync);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005061 if (split && is_sync) {
5062 if ((bic->was_in_burst_list && bfqd->large_burst) ||
5063 bic->saved_in_large_burst)
5064 bfq_mark_bfqq_in_large_burst(bfqq);
5065 else {
5066 bfq_clear_bfqq_in_large_burst(bfqq);
5067 if (bic->was_in_burst_list)
Paolo Valente99fead82017-10-09 13:11:23 +02005068 /*
5069 * If bfqq was in the current
5070 * burst list before being
5071 * merged, then we have to add
5072 * it back. And we do not need
5073 * to increase burst_size, as
5074 * we did not decrement
5075 * burst_size when we removed
5076 * bfqq from the burst list as
5077 * a consequence of a merge
5078 * (see comments in
5079 * bfq_put_queue). In this
5080 * respect, it would be rather
5081 * costly to know whether the
5082 * current burst list is still
5083 * the same burst list from
5084 * which bfqq was removed on
5085 * the merge. To avoid this
5086 * cost, if bfqq was in a
5087 * burst list, then we add
5088 * bfqq to the current burst
5089 * list without any further
5090 * check. This can cause
5091 * inappropriate insertions,
5092 * but rarely enough to not
5093 * harm the detection of large
5094 * bursts significantly.
5095 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005096 hlist_add_head(&bfqq->burst_list_node,
5097 &bfqd->burst_list);
5098 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02005099 bfqq->split_time = jiffies;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005100 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02005101
5102 return bfqq;
5103}
5104
5105/*
Paolo Valente18e5a572018-05-04 19:17:01 +02005106 * Only reset private fields. The actual request preparation will be
5107 * performed by bfq_init_rq, when rq is either inserted or merged. See
5108 * comments on bfq_init_rq for the reason behind this delayed
5109 * preparation.
Paolo Valenteaee69d72017-04-19 08:29:02 -06005110 */
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005111static void bfq_prepare_request(struct request *rq, struct bio *bio)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005112{
Paolo Valente18e5a572018-05-04 19:17:01 +02005113 /*
5114 * Regardless of whether we have an icq attached, we have to
5115 * clear the scheduler pointers, as they might point to
5116 * previously allocated bic/bfqq structs.
5117 */
5118 rq->elv.priv[0] = rq->elv.priv[1] = NULL;
5119}
5120
5121/*
5122 * If needed, init rq, allocate bfq data structures associated with
5123 * rq, and increment reference counters in the destination bfq_queue
5124 * for rq. Return the destination bfq_queue for rq, or NULL is rq is
5125 * not associated with any bfq_queue.
5126 *
5127 * This function is invoked by the functions that perform rq insertion
5128 * or merging. One may have expected the above preparation operations
5129 * to be performed in bfq_prepare_request, and not delayed to when rq
5130 * is inserted or merged. The rationale behind this delayed
5131 * preparation is that, after the prepare_request hook is invoked for
5132 * rq, rq may still be transformed into a request with no icq, i.e., a
5133 * request not associated with any queue. No bfq hook is invoked to
5134 * signal this tranformation. As a consequence, should these
5135 * preparation operations be performed when the prepare_request hook
5136 * is invoked, and should rq be transformed one moment later, bfq
5137 * would end up in an inconsistent state, because it would have
5138 * incremented some queue counters for an rq destined to
5139 * transformation, without any chance to correctly lower these
5140 * counters back. In contrast, no transformation can still happen for
5141 * rq after rq has been inserted or merged. So, it is safe to execute
5142 * these preparation operations when rq is finally inserted or merged.
5143 */
5144static struct bfq_queue *bfq_init_rq(struct request *rq)
5145{
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005146 struct request_queue *q = rq->q;
Paolo Valente18e5a572018-05-04 19:17:01 +02005147 struct bio *bio = rq->bio;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005148 struct bfq_data *bfqd = q->elevator->elevator_data;
Christoph Hellwig9f210732017-06-16 18:15:24 +02005149 struct bfq_io_cq *bic;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005150 const int is_sync = rq_is_sync(rq);
5151 struct bfq_queue *bfqq;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005152 bool new_queue = false;
Paolo Valente13c931b2017-06-27 12:30:47 -06005153 bool bfqq_already_existing = false, split = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005154
Paolo Valente18e5a572018-05-04 19:17:01 +02005155 if (unlikely(!rq->elv.icq))
5156 return NULL;
5157
Jens Axboe72961c42018-04-17 17:08:52 -06005158 /*
Paolo Valente18e5a572018-05-04 19:17:01 +02005159 * Assuming that elv.priv[1] is set only if everything is set
5160 * for this rq. This holds true, because this function is
5161 * invoked only for insertion or merging, and, after such
5162 * events, a request cannot be manipulated any longer before
5163 * being removed from bfq.
Jens Axboe72961c42018-04-17 17:08:52 -06005164 */
Paolo Valente18e5a572018-05-04 19:17:01 +02005165 if (rq->elv.priv[1])
5166 return rq->elv.priv[1];
Jens Axboe72961c42018-04-17 17:08:52 -06005167
Christoph Hellwig9f210732017-06-16 18:15:24 +02005168 bic = icq_to_bic(rq->elv.icq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005169
Colin Ian King8c9ff1a2017-04-20 15:07:18 +01005170 bfq_check_ioprio_change(bic, bio);
5171
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005172 bfq_bic_update_cgroup(bic, bio);
5173
Arianna Avanzini36eca892017-04-12 18:23:16 +02005174 bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync,
5175 &new_queue);
5176
5177 if (likely(!new_queue)) {
5178 /* If the queue was seeky for too long, break it apart. */
5179 if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
5180 bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005181
5182 /* Update bic before losing reference to bfqq */
5183 if (bfq_bfqq_in_large_burst(bfqq))
5184 bic->saved_in_large_burst = true;
5185
Arianna Avanzini36eca892017-04-12 18:23:16 +02005186 bfqq = bfq_split_bfqq(bic, bfqq);
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005187 split = true;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005188
5189 if (!bfqq)
5190 bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio,
5191 true, is_sync,
5192 NULL);
Paolo Valente13c931b2017-06-27 12:30:47 -06005193 else
5194 bfqq_already_existing = true;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005195 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06005196 }
5197
5198 bfqq->allocated++;
5199 bfqq->ref++;
5200 bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d",
5201 rq, bfqq, bfqq->ref);
5202
5203 rq->elv.priv[0] = bic;
5204 rq->elv.priv[1] = bfqq;
5205
Arianna Avanzini36eca892017-04-12 18:23:16 +02005206 /*
5207 * If a bfq_queue has only one process reference, it is owned
5208 * by only this bic: we can then set bfqq->bic = bic. in
5209 * addition, if the queue has also just been split, we have to
5210 * resume its state.
5211 */
5212 if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
5213 bfqq->bic = bic;
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005214 if (split) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02005215 /*
5216 * The queue has just been split from a shared
5217 * queue: restore the idle window and the
5218 * possible weight raising period.
5219 */
Paolo Valente13c931b2017-06-27 12:30:47 -06005220 bfq_bfqq_resume_state(bfqq, bfqd, bic,
5221 bfqq_already_existing);
Arianna Avanzini36eca892017-04-12 18:23:16 +02005222 }
5223 }
5224
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005225 if (unlikely(bfq_bfqq_just_created(bfqq)))
5226 bfq_handle_burst(bfqd, bfqq);
5227
Paolo Valente18e5a572018-05-04 19:17:01 +02005228 return bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005229}
5230
5231static void bfq_idle_slice_timer_body(struct bfq_queue *bfqq)
5232{
5233 struct bfq_data *bfqd = bfqq->bfqd;
5234 enum bfqq_expiration reason;
5235 unsigned long flags;
5236
5237 spin_lock_irqsave(&bfqd->lock, flags);
5238 bfq_clear_bfqq_wait_request(bfqq);
5239
5240 if (bfqq != bfqd->in_service_queue) {
5241 spin_unlock_irqrestore(&bfqd->lock, flags);
5242 return;
5243 }
5244
5245 if (bfq_bfqq_budget_timeout(bfqq))
5246 /*
5247 * Also here the queue can be safely expired
5248 * for budget timeout without wasting
5249 * guarantees
5250 */
5251 reason = BFQQE_BUDGET_TIMEOUT;
5252 else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
5253 /*
5254 * The queue may not be empty upon timer expiration,
5255 * because we may not disable the timer when the
5256 * first request of the in-service queue arrives
5257 * during disk idling.
5258 */
5259 reason = BFQQE_TOO_IDLE;
5260 else
5261 goto schedule_dispatch;
5262
5263 bfq_bfqq_expire(bfqd, bfqq, true, reason);
5264
5265schedule_dispatch:
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005266 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005267 bfq_schedule_dispatch(bfqd);
5268}
5269
5270/*
5271 * Handler of the expiration of the timer running if the in-service queue
5272 * is idling inside its time slice.
5273 */
5274static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)
5275{
5276 struct bfq_data *bfqd = container_of(timer, struct bfq_data,
5277 idle_slice_timer);
5278 struct bfq_queue *bfqq = bfqd->in_service_queue;
5279
5280 /*
5281 * Theoretical race here: the in-service queue can be NULL or
5282 * different from the queue that was idling if a new request
5283 * arrives for the current queue and there is a full dispatch
5284 * cycle that changes the in-service queue. This can hardly
5285 * happen, but in the worst case we just expire a queue too
5286 * early.
5287 */
5288 if (bfqq)
5289 bfq_idle_slice_timer_body(bfqq);
5290
5291 return HRTIMER_NORESTART;
5292}
5293
5294static void __bfq_put_async_bfqq(struct bfq_data *bfqd,
5295 struct bfq_queue **bfqq_ptr)
5296{
5297 struct bfq_queue *bfqq = *bfqq_ptr;
5298
5299 bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
5300 if (bfqq) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005301 bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
5302
Paolo Valenteaee69d72017-04-19 08:29:02 -06005303 bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
5304 bfqq, bfqq->ref);
5305 bfq_put_queue(bfqq);
5306 *bfqq_ptr = NULL;
5307 }
5308}
5309
5310/*
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005311 * Release all the bfqg references to its async queues. If we are
5312 * deallocating the group these queues may still contain requests, so
5313 * we reparent them to the root cgroup (i.e., the only one that will
5314 * exist for sure until all the requests on a device are gone).
Paolo Valenteaee69d72017-04-19 08:29:02 -06005315 */
Paolo Valenteea25da42017-04-19 08:48:24 -06005316void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005317{
5318 int i, j;
5319
5320 for (i = 0; i < 2; i++)
5321 for (j = 0; j < IOPRIO_BE_NR; j++)
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005322 __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005323
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005324 __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005325}
5326
Jens Axboef0635b82018-05-09 13:27:21 -06005327/*
5328 * See the comments on bfq_limit_depth for the purpose of
Jens Axboe483b7bf2018-05-09 15:26:55 -06005329 * the depths set in the function. Return minimum shallow depth we'll use.
Jens Axboef0635b82018-05-09 13:27:21 -06005330 */
Jens Axboe483b7bf2018-05-09 15:26:55 -06005331static unsigned int bfq_update_depths(struct bfq_data *bfqd,
5332 struct sbitmap_queue *bt)
Jens Axboef0635b82018-05-09 13:27:21 -06005333{
Jens Axboe483b7bf2018-05-09 15:26:55 -06005334 unsigned int i, j, min_shallow = UINT_MAX;
5335
Jens Axboef0635b82018-05-09 13:27:21 -06005336 /*
5337 * In-word depths if no bfq_queue is being weight-raised:
5338 * leaving 25% of tags only for sync reads.
5339 *
5340 * In next formulas, right-shift the value
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005341 * (1U<<bt->sb.shift), instead of computing directly
5342 * (1U<<(bt->sb.shift - something)), to be robust against
5343 * any possible value of bt->sb.shift, without having to
Jens Axboef0635b82018-05-09 13:27:21 -06005344 * limit 'something'.
5345 */
5346 /* no more than 50% of tags for async I/O */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005347 bfqd->word_depths[0][0] = max((1U << bt->sb.shift) >> 1, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005348 /*
5349 * no more than 75% of tags for sync writes (25% extra tags
5350 * w.r.t. async I/O, to prevent async I/O from starving sync
5351 * writes)
5352 */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005353 bfqd->word_depths[0][1] = max(((1U << bt->sb.shift) * 3) >> 2, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005354
5355 /*
5356 * In-word depths in case some bfq_queue is being weight-
5357 * raised: leaving ~63% of tags for sync reads. This is the
5358 * highest percentage for which, in our tests, application
5359 * start-up times didn't suffer from any regression due to tag
5360 * shortage.
5361 */
5362 /* no more than ~18% of tags for async I/O */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005363 bfqd->word_depths[1][0] = max(((1U << bt->sb.shift) * 3) >> 4, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005364 /* no more than ~37% of tags for sync writes (~20% extra tags) */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005365 bfqd->word_depths[1][1] = max(((1U << bt->sb.shift) * 6) >> 4, 1U);
Jens Axboe483b7bf2018-05-09 15:26:55 -06005366
5367 for (i = 0; i < 2; i++)
5368 for (j = 0; j < 2; j++)
5369 min_shallow = min(min_shallow, bfqd->word_depths[i][j]);
5370
5371 return min_shallow;
Jens Axboef0635b82018-05-09 13:27:21 -06005372}
5373
5374static int bfq_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int index)
5375{
5376 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
5377 struct blk_mq_tags *tags = hctx->sched_tags;
Jens Axboe483b7bf2018-05-09 15:26:55 -06005378 unsigned int min_shallow;
Jens Axboef0635b82018-05-09 13:27:21 -06005379
Jens Axboe483b7bf2018-05-09 15:26:55 -06005380 min_shallow = bfq_update_depths(bfqd, &tags->bitmap_tags);
5381 sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, min_shallow);
Jens Axboef0635b82018-05-09 13:27:21 -06005382 return 0;
5383}
5384
Paolo Valenteaee69d72017-04-19 08:29:02 -06005385static void bfq_exit_queue(struct elevator_queue *e)
5386{
5387 struct bfq_data *bfqd = e->elevator_data;
5388 struct bfq_queue *bfqq, *n;
5389
5390 hrtimer_cancel(&bfqd->idle_slice_timer);
5391
5392 spin_lock_irq(&bfqd->lock);
5393 list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005394 bfq_deactivate_bfqq(bfqd, bfqq, false, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005395 spin_unlock_irq(&bfqd->lock);
5396
5397 hrtimer_cancel(&bfqd->idle_slice_timer);
5398
Jens Axboe8abef102018-01-09 12:20:51 -07005399#ifdef CONFIG_BFQ_GROUP_IOSCHED
Paolo Valente0d52af52018-01-09 10:27:59 +01005400 /* release oom-queue reference to root group */
5401 bfqg_and_blkg_put(bfqd->root_group);
5402
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005403 blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
5404#else
5405 spin_lock_irq(&bfqd->lock);
5406 bfq_put_async_queues(bfqd, bfqd->root_group);
5407 kfree(bfqd->root_group);
5408 spin_unlock_irq(&bfqd->lock);
5409#endif
5410
Paolo Valenteaee69d72017-04-19 08:29:02 -06005411 kfree(bfqd);
5412}
5413
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005414static void bfq_init_root_group(struct bfq_group *root_group,
5415 struct bfq_data *bfqd)
5416{
5417 int i;
5418
5419#ifdef CONFIG_BFQ_GROUP_IOSCHED
5420 root_group->entity.parent = NULL;
5421 root_group->my_entity = NULL;
5422 root_group->bfqd = bfqd;
5423#endif
Arianna Avanzini36eca892017-04-12 18:23:16 +02005424 root_group->rq_pos_tree = RB_ROOT;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005425 for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
5426 root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
5427 root_group->sched_data.bfq_class_idle_last_service = jiffies;
5428}
5429
Paolo Valenteaee69d72017-04-19 08:29:02 -06005430static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
5431{
5432 struct bfq_data *bfqd;
5433 struct elevator_queue *eq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005434
5435 eq = elevator_alloc(q, e);
5436 if (!eq)
5437 return -ENOMEM;
5438
5439 bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
5440 if (!bfqd) {
5441 kobject_put(&eq->kobj);
5442 return -ENOMEM;
5443 }
5444 eq->elevator_data = bfqd;
5445
Christoph Hellwig0d945c12018-11-15 12:17:28 -07005446 spin_lock_irq(&q->queue_lock);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005447 q->elevator = eq;
Christoph Hellwig0d945c12018-11-15 12:17:28 -07005448 spin_unlock_irq(&q->queue_lock);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005449
Paolo Valenteaee69d72017-04-19 08:29:02 -06005450 /*
5451 * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
5452 * Grab a permanent reference to it, so that the normal code flow
5453 * will not attempt to free it.
5454 */
5455 bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
5456 bfqd->oom_bfqq.ref++;
5457 bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
5458 bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE;
5459 bfqd->oom_bfqq.entity.new_weight =
5460 bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005461
5462 /* oom_bfqq does not participate to bursts */
5463 bfq_clear_bfqq_just_created(&bfqd->oom_bfqq);
5464
Paolo Valenteaee69d72017-04-19 08:29:02 -06005465 /*
5466 * Trigger weight initialization, according to ioprio, at the
5467 * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
5468 * class won't be changed any more.
5469 */
5470 bfqd->oom_bfqq.entity.prio_changed = 1;
5471
5472 bfqd->queue = q;
5473
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005474 INIT_LIST_HEAD(&bfqd->dispatch);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005475
5476 hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC,
5477 HRTIMER_MODE_REL);
5478 bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
5479
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02005480 bfqd->queue_weights_tree = RB_ROOT;
Paolo Valenteba7aeae2018-12-06 19:18:18 +01005481 bfqd->num_groups_with_pending_reqs = 0;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02005482
Paolo Valenteaee69d72017-04-19 08:29:02 -06005483 INIT_LIST_HEAD(&bfqd->active_list);
5484 INIT_LIST_HEAD(&bfqd->idle_list);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005485 INIT_HLIST_HEAD(&bfqd->burst_list);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005486
5487 bfqd->hw_tag = -1;
5488
5489 bfqd->bfq_max_budget = bfq_default_max_budget;
5490
5491 bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
5492 bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
5493 bfqd->bfq_back_max = bfq_back_max;
5494 bfqd->bfq_back_penalty = bfq_back_penalty;
5495 bfqd->bfq_slice_idle = bfq_slice_idle;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005496 bfqd->bfq_timeout = bfq_timeout;
5497
5498 bfqd->bfq_requests_within_timer = 120;
5499
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005500 bfqd->bfq_large_burst_thresh = 8;
5501 bfqd->bfq_burst_interval = msecs_to_jiffies(180);
5502
Paolo Valente44e44a12017-04-12 18:23:12 +02005503 bfqd->low_latency = true;
5504
5505 /*
5506 * Trade-off between responsiveness and fairness.
5507 */
5508 bfqd->bfq_wr_coeff = 30;
Paolo Valente77b7dce2017-04-12 18:23:13 +02005509 bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
Paolo Valente44e44a12017-04-12 18:23:12 +02005510 bfqd->bfq_wr_max_time = 0;
5511 bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
5512 bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
Paolo Valente77b7dce2017-04-12 18:23:13 +02005513 bfqd->bfq_wr_max_softrt_rate = 7000; /*
5514 * Approximate rate required
5515 * to playback or record a
5516 * high-definition compressed
5517 * video.
5518 */
Paolo Valentecfd69712017-04-12 18:23:15 +02005519 bfqd->wr_busy_queues = 0;
Paolo Valente44e44a12017-04-12 18:23:12 +02005520
5521 /*
Paolo Valentee24f1c22018-05-31 16:45:06 +02005522 * Begin by assuming, optimistically, that the device peak
5523 * rate is equal to 2/3 of the highest reference rate.
Paolo Valente44e44a12017-04-12 18:23:12 +02005524 */
Paolo Valentee24f1c22018-05-31 16:45:06 +02005525 bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] *
5526 ref_wr_duration[blk_queue_nonrot(bfqd->queue)];
5527 bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3;
Paolo Valente44e44a12017-04-12 18:23:12 +02005528
Paolo Valenteaee69d72017-04-19 08:29:02 -06005529 spin_lock_init(&bfqd->lock);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005530
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005531 /*
5532 * The invocation of the next bfq_create_group_hierarchy
5533 * function is the head of a chain of function calls
5534 * (bfq_create_group_hierarchy->blkcg_activate_policy->
5535 * blk_mq_freeze_queue) that may lead to the invocation of the
5536 * has_work hook function. For this reason,
5537 * bfq_create_group_hierarchy is invoked only after all
5538 * scheduler data has been initialized, apart from the fields
5539 * that can be initialized only after invoking
5540 * bfq_create_group_hierarchy. This, in particular, enables
5541 * has_work to correctly return false. Of course, to avoid
5542 * other inconsistencies, the blk-mq stack must then refrain
5543 * from invoking further scheduler hooks before this init
5544 * function is finished.
5545 */
5546 bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node);
5547 if (!bfqd->root_group)
5548 goto out_free;
5549 bfq_init_root_group(bfqd->root_group, bfqd);
5550 bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
5551
Luca Micciob5dc5d42017-10-09 16:27:21 +02005552 wbt_disable_default(q);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005553 return 0;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005554
5555out_free:
5556 kfree(bfqd);
5557 kobject_put(&eq->kobj);
5558 return -ENOMEM;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005559}
5560
5561static void bfq_slab_kill(void)
5562{
5563 kmem_cache_destroy(bfq_pool);
5564}
5565
5566static int __init bfq_slab_setup(void)
5567{
5568 bfq_pool = KMEM_CACHE(bfq_queue, 0);
5569 if (!bfq_pool)
5570 return -ENOMEM;
5571 return 0;
5572}
5573
5574static ssize_t bfq_var_show(unsigned int var, char *page)
5575{
5576 return sprintf(page, "%u\n", var);
5577}
5578
Bart Van Assche2f791362017-08-30 11:42:09 -07005579static int bfq_var_store(unsigned long *var, const char *page)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005580{
5581 unsigned long new_val;
5582 int ret = kstrtoul(page, 10, &new_val);
5583
Bart Van Assche2f791362017-08-30 11:42:09 -07005584 if (ret)
5585 return ret;
5586 *var = new_val;
5587 return 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005588}
5589
5590#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
5591static ssize_t __FUNC(struct elevator_queue *e, char *page) \
5592{ \
5593 struct bfq_data *bfqd = e->elevator_data; \
5594 u64 __data = __VAR; \
5595 if (__CONV == 1) \
5596 __data = jiffies_to_msecs(__data); \
5597 else if (__CONV == 2) \
5598 __data = div_u64(__data, NSEC_PER_MSEC); \
5599 return bfq_var_show(__data, (page)); \
5600}
5601SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
5602SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
5603SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
5604SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
5605SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
5606SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
5607SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
5608SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
Paolo Valente44e44a12017-04-12 18:23:12 +02005609SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005610#undef SHOW_FUNCTION
5611
5612#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
5613static ssize_t __FUNC(struct elevator_queue *e, char *page) \
5614{ \
5615 struct bfq_data *bfqd = e->elevator_data; \
5616 u64 __data = __VAR; \
5617 __data = div_u64(__data, NSEC_PER_USEC); \
5618 return bfq_var_show(__data, (page)); \
5619}
5620USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
5621#undef USEC_SHOW_FUNCTION
5622
5623#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
5624static ssize_t \
5625__FUNC(struct elevator_queue *e, const char *page, size_t count) \
5626{ \
5627 struct bfq_data *bfqd = e->elevator_data; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07005628 unsigned long __data, __min = (MIN), __max = (MAX); \
Bart Van Assche2f791362017-08-30 11:42:09 -07005629 int ret; \
5630 \
5631 ret = bfq_var_store(&__data, (page)); \
5632 if (ret) \
5633 return ret; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07005634 if (__data < __min) \
5635 __data = __min; \
5636 else if (__data > __max) \
5637 __data = __max; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06005638 if (__CONV == 1) \
5639 *(__PTR) = msecs_to_jiffies(__data); \
5640 else if (__CONV == 2) \
5641 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
5642 else \
5643 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08005644 return count; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06005645}
5646STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
5647 INT_MAX, 2);
5648STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
5649 INT_MAX, 2);
5650STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
5651STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
5652 INT_MAX, 0);
5653STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
5654#undef STORE_FUNCTION
5655
5656#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
5657static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\
5658{ \
5659 struct bfq_data *bfqd = e->elevator_data; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07005660 unsigned long __data, __min = (MIN), __max = (MAX); \
Bart Van Assche2f791362017-08-30 11:42:09 -07005661 int ret; \
5662 \
5663 ret = bfq_var_store(&__data, (page)); \
5664 if (ret) \
5665 return ret; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07005666 if (__data < __min) \
5667 __data = __min; \
5668 else if (__data > __max) \
5669 __data = __max; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06005670 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
weiping zhang235f8da2017-08-25 01:11:33 +08005671 return count; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06005672}
5673USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
5674 UINT_MAX);
5675#undef USEC_STORE_FUNCTION
5676
Paolo Valenteaee69d72017-04-19 08:29:02 -06005677static ssize_t bfq_max_budget_store(struct elevator_queue *e,
5678 const char *page, size_t count)
5679{
5680 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07005681 unsigned long __data;
5682 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08005683
Bart Van Assche2f791362017-08-30 11:42:09 -07005684 ret = bfq_var_store(&__data, (page));
5685 if (ret)
5686 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005687
5688 if (__data == 0)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02005689 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005690 else {
5691 if (__data > INT_MAX)
5692 __data = INT_MAX;
5693 bfqd->bfq_max_budget = __data;
5694 }
5695
5696 bfqd->bfq_user_max_budget = __data;
5697
weiping zhang235f8da2017-08-25 01:11:33 +08005698 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005699}
5700
5701/*
5702 * Leaving this name to preserve name compatibility with cfq
5703 * parameters, but this timeout is used for both sync and async.
5704 */
5705static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
5706 const char *page, size_t count)
5707{
5708 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07005709 unsigned long __data;
5710 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08005711
Bart Van Assche2f791362017-08-30 11:42:09 -07005712 ret = bfq_var_store(&__data, (page));
5713 if (ret)
5714 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005715
5716 if (__data < 1)
5717 __data = 1;
5718 else if (__data > INT_MAX)
5719 __data = INT_MAX;
5720
5721 bfqd->bfq_timeout = msecs_to_jiffies(__data);
5722 if (bfqd->bfq_user_max_budget == 0)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02005723 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005724
weiping zhang235f8da2017-08-25 01:11:33 +08005725 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005726}
5727
5728static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e,
5729 const char *page, size_t count)
5730{
5731 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07005732 unsigned long __data;
5733 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08005734
Bart Van Assche2f791362017-08-30 11:42:09 -07005735 ret = bfq_var_store(&__data, (page));
5736 if (ret)
5737 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005738
5739 if (__data > 1)
5740 __data = 1;
5741 if (!bfqd->strict_guarantees && __data == 1
5742 && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC)
5743 bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC;
5744
5745 bfqd->strict_guarantees = __data;
5746
weiping zhang235f8da2017-08-25 01:11:33 +08005747 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005748}
5749
Paolo Valente44e44a12017-04-12 18:23:12 +02005750static ssize_t bfq_low_latency_store(struct elevator_queue *e,
5751 const char *page, size_t count)
5752{
5753 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07005754 unsigned long __data;
5755 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08005756
Bart Van Assche2f791362017-08-30 11:42:09 -07005757 ret = bfq_var_store(&__data, (page));
5758 if (ret)
5759 return ret;
Paolo Valente44e44a12017-04-12 18:23:12 +02005760
5761 if (__data > 1)
5762 __data = 1;
5763 if (__data == 0 && bfqd->low_latency != 0)
5764 bfq_end_wr(bfqd);
5765 bfqd->low_latency = __data;
5766
weiping zhang235f8da2017-08-25 01:11:33 +08005767 return count;
Paolo Valente44e44a12017-04-12 18:23:12 +02005768}
5769
Paolo Valenteaee69d72017-04-19 08:29:02 -06005770#define BFQ_ATTR(name) \
5771 __ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store)
5772
5773static struct elv_fs_entry bfq_attrs[] = {
5774 BFQ_ATTR(fifo_expire_sync),
5775 BFQ_ATTR(fifo_expire_async),
5776 BFQ_ATTR(back_seek_max),
5777 BFQ_ATTR(back_seek_penalty),
5778 BFQ_ATTR(slice_idle),
5779 BFQ_ATTR(slice_idle_us),
5780 BFQ_ATTR(max_budget),
5781 BFQ_ATTR(timeout_sync),
5782 BFQ_ATTR(strict_guarantees),
Paolo Valente44e44a12017-04-12 18:23:12 +02005783 BFQ_ATTR(low_latency),
Paolo Valenteaee69d72017-04-19 08:29:02 -06005784 __ATTR_NULL
5785};
5786
5787static struct elevator_type iosched_bfq_mq = {
Jens Axboef9cd4bf2018-11-01 16:41:41 -06005788 .ops = {
Paolo Valentea52a69e2018-01-13 12:05:17 +01005789 .limit_depth = bfq_limit_depth,
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005790 .prepare_request = bfq_prepare_request,
Paolo Valentea7877392018-02-07 22:19:20 +01005791 .requeue_request = bfq_finish_requeue_request,
5792 .finish_request = bfq_finish_requeue_request,
Paolo Valenteaee69d72017-04-19 08:29:02 -06005793 .exit_icq = bfq_exit_icq,
5794 .insert_requests = bfq_insert_requests,
5795 .dispatch_request = bfq_dispatch_request,
5796 .next_request = elv_rb_latter_request,
5797 .former_request = elv_rb_former_request,
5798 .allow_merge = bfq_allow_bio_merge,
5799 .bio_merge = bfq_bio_merge,
5800 .request_merge = bfq_request_merge,
5801 .requests_merged = bfq_requests_merged,
5802 .request_merged = bfq_request_merged,
5803 .has_work = bfq_has_work,
Jens Axboef0635b82018-05-09 13:27:21 -06005804 .init_hctx = bfq_init_hctx,
Paolo Valenteaee69d72017-04-19 08:29:02 -06005805 .init_sched = bfq_init_queue,
5806 .exit_sched = bfq_exit_queue,
5807 },
5808
Paolo Valenteaee69d72017-04-19 08:29:02 -06005809 .icq_size = sizeof(struct bfq_io_cq),
5810 .icq_align = __alignof__(struct bfq_io_cq),
5811 .elevator_attrs = bfq_attrs,
5812 .elevator_name = "bfq",
5813 .elevator_owner = THIS_MODULE,
5814};
Ben Hutchings26b4cf22017-08-13 18:02:19 +01005815MODULE_ALIAS("bfq-iosched");
Paolo Valenteaee69d72017-04-19 08:29:02 -06005816
5817static int __init bfq_init(void)
5818{
5819 int ret;
5820
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005821#ifdef CONFIG_BFQ_GROUP_IOSCHED
5822 ret = blkcg_policy_register(&blkcg_policy_bfq);
5823 if (ret)
5824 return ret;
5825#endif
5826
Paolo Valenteaee69d72017-04-19 08:29:02 -06005827 ret = -ENOMEM;
5828 if (bfq_slab_setup())
5829 goto err_pol_unreg;
5830
Paolo Valente44e44a12017-04-12 18:23:12 +02005831 /*
5832 * Times to load large popular applications for the typical
5833 * systems installed on the reference devices (see the
Paolo Valentee24f1c22018-05-31 16:45:06 +02005834 * comments before the definition of the next
5835 * array). Actually, we use slightly lower values, as the
Paolo Valente44e44a12017-04-12 18:23:12 +02005836 * estimated peak rate tends to be smaller than the actual
5837 * peak rate. The reason for this last fact is that estimates
5838 * are computed over much shorter time intervals than the long
5839 * intervals typically used for benchmarking. Why? First, to
5840 * adapt more quickly to variations. Second, because an I/O
5841 * scheduler cannot rely on a peak-rate-evaluation workload to
5842 * be run for a long time.
5843 */
Paolo Valentee24f1c22018-05-31 16:45:06 +02005844 ref_wr_duration[0] = msecs_to_jiffies(7000); /* actually 8 sec */
5845 ref_wr_duration[1] = msecs_to_jiffies(2500); /* actually 3 sec */
Paolo Valente44e44a12017-04-12 18:23:12 +02005846
Paolo Valenteaee69d72017-04-19 08:29:02 -06005847 ret = elv_register(&iosched_bfq_mq);
5848 if (ret)
weiping zhang37dcd652017-08-19 00:37:20 +08005849 goto slab_kill;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005850
5851 return 0;
5852
weiping zhang37dcd652017-08-19 00:37:20 +08005853slab_kill:
5854 bfq_slab_kill();
Paolo Valenteaee69d72017-04-19 08:29:02 -06005855err_pol_unreg:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005856#ifdef CONFIG_BFQ_GROUP_IOSCHED
5857 blkcg_policy_unregister(&blkcg_policy_bfq);
5858#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06005859 return ret;
5860}
5861
5862static void __exit bfq_exit(void)
5863{
5864 elv_unregister(&iosched_bfq_mq);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005865#ifdef CONFIG_BFQ_GROUP_IOSCHED
5866 blkcg_policy_unregister(&blkcg_policy_bfq);
5867#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06005868 bfq_slab_kill();
5869}
5870
5871module_init(bfq_init);
5872module_exit(bfq_exit);
5873
5874MODULE_AUTHOR("Paolo Valente");
5875MODULE_LICENSE("GPL");
5876MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler");