blob: f59efee7a6014c63d45573b357b031ef5a6bf31f [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. */
Paolo Valentea3c92562019-01-29 12:06:35 +0100233#define BFQ_HW_QUEUE_THRESHOLD 3
Paolo Valenteaee69d72017-04-19 08:29:02 -0600234#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/*
Paolo Valentefb53ac62019-03-12 09:59:28 +0100632 * The following function returns false either if every active queue
633 * must receive the same share of the throughput (symmetric scenario),
634 * or, as a special case, if bfqq must receive a share of the
635 * throughput lower than or equal to the share that every other active
636 * queue must receive. If bfqq does sync I/O, then these are the only
637 * two cases where bfqq happens to be guaranteed its share of the
638 * throughput even if I/O dispatching is not plugged when bfqq remains
639 * temporarily empty (for more details, see the comments in the
640 * function bfq_better_to_idle()). For this reason, the return value
641 * of this function is used to check whether I/O-dispatch plugging can
642 * be avoided.
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200643 *
Paolo Valentefb53ac62019-03-12 09:59:28 +0100644 * The above first case (symmetric scenario) occurs when:
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200645 * 1) all active queues have the same weight,
Paolo Valente73d58112019-01-29 12:06:29 +0100646 * 2) all active queues belong to the same I/O-priority class,
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200647 * 3) all active groups at the same level in the groups tree have the same
Paolo Valente73d58112019-01-29 12:06:29 +0100648 * weight,
649 * 4) all active groups at the same level in the groups tree have the same
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200650 * number of children.
651 *
Federico Motta2d29c9f2018-10-12 11:55:57 +0200652 * Unfortunately, keeping the necessary state for evaluating exactly
653 * the last two symmetry sub-conditions above would be quite complex
Paolo Valente73d58112019-01-29 12:06:29 +0100654 * and time consuming. Therefore this function evaluates, instead,
655 * only the following stronger three sub-conditions, for which it is
Federico Motta2d29c9f2018-10-12 11:55:57 +0200656 * much easier to maintain the needed state:
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200657 * 1) all active queues have the same weight,
Paolo Valente73d58112019-01-29 12:06:29 +0100658 * 2) all active queues belong to the same I/O-priority class,
659 * 3) there are no active groups.
Federico Motta2d29c9f2018-10-12 11:55:57 +0200660 * In particular, the last condition is always true if hierarchical
661 * support or the cgroups interface are not enabled, thus no state
662 * needs to be maintained in this case.
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200663 */
Paolo Valentefb53ac62019-03-12 09:59:28 +0100664static bool bfq_asymmetric_scenario(struct bfq_data *bfqd,
665 struct bfq_queue *bfqq)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200666{
Paolo Valentefb53ac62019-03-12 09:59:28 +0100667 bool smallest_weight = bfqq &&
668 bfqq->weight_counter &&
669 bfqq->weight_counter ==
670 container_of(
671 rb_first_cached(&bfqd->queue_weights_tree),
672 struct bfq_weight_counter,
673 weights_node);
674
Paolo Valente73d58112019-01-29 12:06:29 +0100675 /*
676 * For queue weights to differ, queue_weights_tree must contain
677 * at least two nodes.
678 */
Paolo Valentefb53ac62019-03-12 09:59:28 +0100679 bool varied_queue_weights = !smallest_weight &&
680 !RB_EMPTY_ROOT(&bfqd->queue_weights_tree.rb_root) &&
681 (bfqd->queue_weights_tree.rb_root.rb_node->rb_left ||
682 bfqd->queue_weights_tree.rb_root.rb_node->rb_right);
Paolo Valente73d58112019-01-29 12:06:29 +0100683
684 bool multiple_classes_busy =
685 (bfqd->busy_queues[0] && bfqd->busy_queues[1]) ||
686 (bfqd->busy_queues[0] && bfqd->busy_queues[2]) ||
687 (bfqd->busy_queues[1] && bfqd->busy_queues[2]);
688
Paolo Valentefb53ac62019-03-12 09:59:28 +0100689 return varied_queue_weights || multiple_classes_busy
Konstantin Khlebnikov42b1bd32019-03-29 17:01:18 +0300690#ifdef CONFIG_BFQ_GROUP_IOSCHED
Paolo Valente73d58112019-01-29 12:06:29 +0100691 || bfqd->num_groups_with_pending_reqs > 0
692#endif
Paolo Valentefb53ac62019-03-12 09:59:28 +0100693 ;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200694}
695
696/*
697 * If the weight-counter tree passed as input contains no counter for
Federico Motta2d29c9f2018-10-12 11:55:57 +0200698 * the weight of the input queue, then add that counter; otherwise just
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200699 * increment the existing counter.
700 *
701 * Note that weight-counter trees contain few nodes in mostly symmetric
702 * scenarios. For example, if all queues have the same weight, then the
703 * weight-counter tree for the queues may contain at most one node.
704 * This holds even if low_latency is on, because weight-raised queues
705 * are not inserted in the tree.
706 * In most scenarios, the rate at which nodes are created/destroyed
707 * should be low too.
708 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200709void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
Paolo Valentefb53ac62019-03-12 09:59:28 +0100710 struct rb_root_cached *root)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200711{
Federico Motta2d29c9f2018-10-12 11:55:57 +0200712 struct bfq_entity *entity = &bfqq->entity;
Paolo Valentefb53ac62019-03-12 09:59:28 +0100713 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
714 bool leftmost = true;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200715
716 /*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200717 * Do not insert if the queue is already associated with a
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200718 * counter, which happens if:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200719 * 1) a request arrival has caused the queue to become both
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200720 * non-weight-raised, and hence change its weight, and
721 * backlogged; in this respect, each of the two events
722 * causes an invocation of this function,
Federico Motta2d29c9f2018-10-12 11:55:57 +0200723 * 2) this is the invocation of this function caused by the
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200724 * second event. This second invocation is actually useless,
725 * and we handle this fact by exiting immediately. More
726 * efficient or clearer solutions might possibly be adopted.
727 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200728 if (bfqq->weight_counter)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200729 return;
730
731 while (*new) {
732 struct bfq_weight_counter *__counter = container_of(*new,
733 struct bfq_weight_counter,
734 weights_node);
735 parent = *new;
736
737 if (entity->weight == __counter->weight) {
Federico Motta2d29c9f2018-10-12 11:55:57 +0200738 bfqq->weight_counter = __counter;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200739 goto inc_counter;
740 }
741 if (entity->weight < __counter->weight)
742 new = &((*new)->rb_left);
Paolo Valentefb53ac62019-03-12 09:59:28 +0100743 else {
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200744 new = &((*new)->rb_right);
Paolo Valentefb53ac62019-03-12 09:59:28 +0100745 leftmost = false;
746 }
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200747 }
748
Federico Motta2d29c9f2018-10-12 11:55:57 +0200749 bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
750 GFP_ATOMIC);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200751
752 /*
753 * In the unlucky event of an allocation failure, we just
Federico Motta2d29c9f2018-10-12 11:55:57 +0200754 * exit. This will cause the weight of queue to not be
Paolo Valentefb53ac62019-03-12 09:59:28 +0100755 * considered in bfq_asymmetric_scenario, which, in its turn,
Paolo Valente73d58112019-01-29 12:06:29 +0100756 * causes the scenario to be deemed wrongly symmetric in case
757 * bfqq's weight would have been the only weight making the
758 * scenario asymmetric. On the bright side, no unbalance will
759 * however occur when bfqq becomes inactive again (the
760 * invocation of this function is triggered by an activation
761 * of queue). In fact, bfq_weights_tree_remove does nothing
762 * if !bfqq->weight_counter.
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200763 */
Federico Motta2d29c9f2018-10-12 11:55:57 +0200764 if (unlikely(!bfqq->weight_counter))
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200765 return;
766
Federico Motta2d29c9f2018-10-12 11:55:57 +0200767 bfqq->weight_counter->weight = entity->weight;
768 rb_link_node(&bfqq->weight_counter->weights_node, parent, new);
Paolo Valentefb53ac62019-03-12 09:59:28 +0100769 rb_insert_color_cached(&bfqq->weight_counter->weights_node, root,
770 leftmost);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200771
772inc_counter:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200773 bfqq->weight_counter->num_active++;
Paolo Valente9dee8b32019-01-29 12:06:34 +0100774 bfqq->ref++;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200775}
776
777/*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200778 * Decrement the weight counter associated with the queue, and, if the
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200779 * counter reaches 0, remove the counter from the tree.
780 * See the comments to the function bfq_weights_tree_add() for considerations
781 * about overhead.
782 */
Paolo Valente04715592018-06-25 21:55:34 +0200783void __bfq_weights_tree_remove(struct bfq_data *bfqd,
Federico Motta2d29c9f2018-10-12 11:55:57 +0200784 struct bfq_queue *bfqq,
Paolo Valentefb53ac62019-03-12 09:59:28 +0100785 struct rb_root_cached *root)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200786{
Federico Motta2d29c9f2018-10-12 11:55:57 +0200787 if (!bfqq->weight_counter)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200788 return;
789
Federico Motta2d29c9f2018-10-12 11:55:57 +0200790 bfqq->weight_counter->num_active--;
791 if (bfqq->weight_counter->num_active > 0)
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200792 goto reset_entity_pointer;
793
Paolo Valentefb53ac62019-03-12 09:59:28 +0100794 rb_erase_cached(&bfqq->weight_counter->weights_node, root);
Federico Motta2d29c9f2018-10-12 11:55:57 +0200795 kfree(bfqq->weight_counter);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200796
797reset_entity_pointer:
Federico Motta2d29c9f2018-10-12 11:55:57 +0200798 bfqq->weight_counter = NULL;
Paolo Valente9dee8b32019-01-29 12:06:34 +0100799 bfq_put_queue(bfqq);
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +0200800}
801
802/*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200803 * Invoke __bfq_weights_tree_remove on bfqq and decrement the number
804 * of active groups for each queue's inactive parent entity.
Paolo Valente04715592018-06-25 21:55:34 +0200805 */
806void bfq_weights_tree_remove(struct bfq_data *bfqd,
807 struct bfq_queue *bfqq)
808{
809 struct bfq_entity *entity = bfqq->entity.parent;
810
Paolo Valente04715592018-06-25 21:55:34 +0200811 for_each_entity(entity) {
812 struct bfq_sched_data *sd = entity->my_sched_data;
813
814 if (sd->next_in_service || sd->in_service_entity) {
815 /*
816 * entity is still active, because either
817 * next_in_service or in_service_entity is not
818 * NULL (see the comments on the definition of
819 * next_in_service for details on why
820 * in_service_entity must be checked too).
821 *
Federico Motta2d29c9f2018-10-12 11:55:57 +0200822 * As a consequence, its parent entities are
823 * active as well, and thus this loop must
824 * stop here.
Paolo Valente04715592018-06-25 21:55:34 +0200825 */
826 break;
827 }
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100828
829 /*
830 * The decrement of num_groups_with_pending_reqs is
831 * not performed immediately upon the deactivation of
832 * entity, but it is delayed to when it also happens
833 * that the first leaf descendant bfqq of entity gets
834 * all its pending requests completed. The following
835 * instructions perform this delayed decrement, if
836 * needed. See the comments on
837 * num_groups_with_pending_reqs for details.
838 */
839 if (entity->in_groups_with_pending_reqs) {
840 entity->in_groups_with_pending_reqs = false;
841 bfqd->num_groups_with_pending_reqs--;
842 }
Paolo Valente04715592018-06-25 21:55:34 +0200843 }
Paolo Valente9dee8b32019-01-29 12:06:34 +0100844
845 /*
846 * Next function is invoked last, because it causes bfqq to be
847 * freed if the following holds: bfqq is not in service and
848 * has no dispatched request. DO NOT use bfqq after the next
849 * function invocation.
850 */
851 __bfq_weights_tree_remove(bfqd, bfqq,
852 &bfqd->queue_weights_tree);
Paolo Valente04715592018-06-25 21:55:34 +0200853}
854
855/*
Paolo Valenteaee69d72017-04-19 08:29:02 -0600856 * Return expired entry, or NULL to just start from scratch in rbtree.
857 */
858static struct request *bfq_check_fifo(struct bfq_queue *bfqq,
859 struct request *last)
860{
861 struct request *rq;
862
863 if (bfq_bfqq_fifo_expire(bfqq))
864 return NULL;
865
866 bfq_mark_bfqq_fifo_expire(bfqq);
867
868 rq = rq_entry_fifo(bfqq->fifo.next);
869
870 if (rq == last || ktime_get_ns() < rq->fifo_time)
871 return NULL;
872
873 bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq);
874 return rq;
875}
876
877static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
878 struct bfq_queue *bfqq,
879 struct request *last)
880{
881 struct rb_node *rbnext = rb_next(&last->rb_node);
882 struct rb_node *rbprev = rb_prev(&last->rb_node);
883 struct request *next, *prev = NULL;
884
885 /* Follow expired path, else get first next available. */
886 next = bfq_check_fifo(bfqq, last);
887 if (next)
888 return next;
889
890 if (rbprev)
891 prev = rb_entry_rq(rbprev);
892
893 if (rbnext)
894 next = rb_entry_rq(rbnext);
895 else {
896 rbnext = rb_first(&bfqq->sort_list);
897 if (rbnext && rbnext != &last->rb_node)
898 next = rb_entry_rq(rbnext);
899 }
900
901 return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
902}
903
Paolo Valentec074170e2017-04-12 18:23:11 +0200904/* see the definition of bfq_async_charge_factor for details */
Paolo Valenteaee69d72017-04-19 08:29:02 -0600905static unsigned long bfq_serv_to_charge(struct request *rq,
906 struct bfq_queue *bfqq)
907{
Paolo Valente02a6d782019-01-29 12:06:37 +0100908 if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1 ||
Paolo Valentefb53ac62019-03-12 09:59:28 +0100909 bfq_asymmetric_scenario(bfqq->bfqd, bfqq))
Paolo Valentec074170e2017-04-12 18:23:11 +0200910 return blk_rq_sectors(rq);
911
Paolo Valented5801082018-08-16 18:51:17 +0200912 return blk_rq_sectors(rq) * bfq_async_charge_factor;
Paolo Valenteaee69d72017-04-19 08:29:02 -0600913}
914
915/**
916 * bfq_updated_next_req - update the queue after a new next_rq selection.
917 * @bfqd: the device data the queue belongs to.
918 * @bfqq: the queue to update.
919 *
920 * If the first request of a queue changes we make sure that the queue
921 * has enough budget to serve at least its first request (if the
922 * request has grown). We do this because if the queue has not enough
923 * budget for its first request, it has to go through two dispatch
924 * rounds to actually get it dispatched.
925 */
926static void bfq_updated_next_req(struct bfq_data *bfqd,
927 struct bfq_queue *bfqq)
928{
929 struct bfq_entity *entity = &bfqq->entity;
930 struct request *next_rq = bfqq->next_rq;
931 unsigned long new_budget;
932
933 if (!next_rq)
934 return;
935
936 if (bfqq == bfqd->in_service_queue)
937 /*
938 * In order not to break guarantees, budgets cannot be
939 * changed after an entity has been selected.
940 */
941 return;
942
Paolo Valentef3218ad2019-01-29 12:06:27 +0100943 new_budget = max_t(unsigned long,
944 max_t(unsigned long, bfqq->max_budget,
945 bfq_serv_to_charge(next_rq, bfqq)),
946 entity->service);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600947 if (entity->budget != new_budget) {
948 entity->budget = new_budget;
949 bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
950 new_budget);
Paolo Valente80294c32017-08-31 08:46:29 +0200951 bfq_requeue_bfqq(bfqd, bfqq, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -0600952 }
953}
954
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200955static unsigned int bfq_wr_duration(struct bfq_data *bfqd)
956{
957 u64 dur;
958
959 if (bfqd->bfq_wr_max_time > 0)
960 return bfqd->bfq_wr_max_time;
961
Paolo Valentee24f1c22018-05-31 16:45:06 +0200962 dur = bfqd->rate_dur_prod;
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200963 do_div(dur, bfqd->peak_rate);
964
965 /*
Davide Sapienzad4505422018-05-31 16:45:07 +0200966 * Limit duration between 3 and 25 seconds. The upper limit
967 * has been conservatively set after the following worst case:
968 * on a QEMU/KVM virtual machine
969 * - running in a slow PC
970 * - with a virtual disk stacked on a slow low-end 5400rpm HDD
971 * - serving a heavy I/O workload, such as the sequential reading
972 * of several files
973 * mplayer took 23 seconds to start, if constantly weight-raised.
974 *
975 * As for higher values than that accomodating the above bad
976 * scenario, tests show that higher values would often yield
977 * the opposite of the desired result, i.e., would worsen
978 * responsiveness by allowing non-interactive applications to
979 * preserve weight raising for too long.
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200980 *
981 * On the other end, lower values than 3 seconds make it
982 * difficult for most interactive tasks to complete their jobs
983 * before weight-raising finishes.
984 */
Davide Sapienzad4505422018-05-31 16:45:07 +0200985 return clamp_val(dur, msecs_to_jiffies(3000), msecs_to_jiffies(25000));
Paolo Valente3e2bdd62017-09-21 11:04:01 +0200986}
987
988/* switch back from soft real-time to interactive weight raising */
989static void switch_back_to_interactive_wr(struct bfq_queue *bfqq,
990 struct bfq_data *bfqd)
991{
992 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
993 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
994 bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt;
995}
996
Arianna Avanzini36eca892017-04-12 18:23:16 +0200997static void
Paolo Valente13c931b2017-06-27 12:30:47 -0600998bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd,
999 struct bfq_io_cq *bic, bool bfq_already_existing)
Arianna Avanzini36eca892017-04-12 18:23:16 +02001000{
Paolo Valente13c931b2017-06-27 12:30:47 -06001001 unsigned int old_wr_coeff = bfqq->wr_coeff;
1002 bool busy = bfq_already_existing && bfq_bfqq_busy(bfqq);
1003
Paolo Valented5be3fe2017-08-04 07:35:10 +02001004 if (bic->saved_has_short_ttime)
1005 bfq_mark_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02001006 else
Paolo Valented5be3fe2017-08-04 07:35:10 +02001007 bfq_clear_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02001008
1009 if (bic->saved_IO_bound)
1010 bfq_mark_bfqq_IO_bound(bfqq);
1011 else
1012 bfq_clear_bfqq_IO_bound(bfqq);
1013
1014 bfqq->ttime = bic->saved_ttime;
1015 bfqq->wr_coeff = bic->saved_wr_coeff;
1016 bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt;
1017 bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish;
1018 bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time;
1019
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001020 if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) ||
Arianna Avanzini36eca892017-04-12 18:23:16 +02001021 time_is_before_jiffies(bfqq->last_wr_start_finish +
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001022 bfqq->wr_cur_max_time))) {
Paolo Valente3e2bdd62017-09-21 11:04:01 +02001023 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
1024 !bfq_bfqq_in_large_burst(bfqq) &&
1025 time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt +
1026 bfq_wr_duration(bfqd))) {
1027 switch_back_to_interactive_wr(bfqq, bfqd);
1028 } else {
1029 bfqq->wr_coeff = 1;
1030 bfq_log_bfqq(bfqq->bfqd, bfqq,
1031 "resume state: switching off wr");
1032 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02001033 }
1034
1035 /* make sure weight will be updated, however we got here */
1036 bfqq->entity.prio_changed = 1;
Paolo Valente13c931b2017-06-27 12:30:47 -06001037
1038 if (likely(!busy))
1039 return;
1040
1041 if (old_wr_coeff == 1 && bfqq->wr_coeff > 1)
1042 bfqd->wr_busy_queues++;
1043 else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1)
1044 bfqd->wr_busy_queues--;
Arianna Avanzini36eca892017-04-12 18:23:16 +02001045}
1046
1047static int bfqq_process_refs(struct bfq_queue *bfqq)
1048{
Paolo Valente9dee8b32019-01-29 12:06:34 +01001049 return bfqq->ref - bfqq->allocated - bfqq->entity.on_st -
1050 (bfqq->weight_counter != NULL);
Arianna Avanzini36eca892017-04-12 18:23:16 +02001051}
1052
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001053/* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */
1054static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1055{
1056 struct bfq_queue *item;
1057 struct hlist_node *n;
1058
1059 hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node)
1060 hlist_del_init(&item->burst_list_node);
1061 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1062 bfqd->burst_size = 1;
1063 bfqd->burst_parent_entity = bfqq->entity.parent;
1064}
1065
1066/* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
1067static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1068{
1069 /* Increment burst size to take into account also bfqq */
1070 bfqd->burst_size++;
1071
1072 if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
1073 struct bfq_queue *pos, *bfqq_item;
1074 struct hlist_node *n;
1075
1076 /*
1077 * Enough queues have been activated shortly after each
1078 * other to consider this burst as large.
1079 */
1080 bfqd->large_burst = true;
1081
1082 /*
1083 * We can now mark all queues in the burst list as
1084 * belonging to a large burst.
1085 */
1086 hlist_for_each_entry(bfqq_item, &bfqd->burst_list,
1087 burst_list_node)
1088 bfq_mark_bfqq_in_large_burst(bfqq_item);
1089 bfq_mark_bfqq_in_large_burst(bfqq);
1090
1091 /*
1092 * From now on, and until the current burst finishes, any
1093 * new queue being activated shortly after the last queue
1094 * was inserted in the burst can be immediately marked as
1095 * belonging to a large burst. So the burst list is not
1096 * needed any more. Remove it.
1097 */
1098 hlist_for_each_entry_safe(pos, n, &bfqd->burst_list,
1099 burst_list_node)
1100 hlist_del_init(&pos->burst_list_node);
1101 } else /*
1102 * Burst not yet large: add bfqq to the burst list. Do
1103 * not increment the ref counter for bfqq, because bfqq
1104 * is removed from the burst list before freeing bfqq
1105 * in put_queue.
1106 */
1107 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
1108}
1109
1110/*
1111 * If many queues belonging to the same group happen to be created
1112 * shortly after each other, then the processes associated with these
1113 * queues have typically a common goal. In particular, bursts of queue
1114 * creations are usually caused by services or applications that spawn
1115 * many parallel threads/processes. Examples are systemd during boot,
1116 * or git grep. To help these processes get their job done as soon as
1117 * possible, it is usually better to not grant either weight-raising
1118 * or device idling to their queues.
1119 *
1120 * In this comment we describe, firstly, the reasons why this fact
1121 * holds, and, secondly, the next function, which implements the main
1122 * steps needed to properly mark these queues so that they can then be
1123 * treated in a different way.
1124 *
1125 * The above services or applications benefit mostly from a high
1126 * throughput: the quicker the requests of the activated queues are
1127 * cumulatively served, the sooner the target job of these queues gets
1128 * completed. As a consequence, weight-raising any of these queues,
1129 * which also implies idling the device for it, is almost always
1130 * counterproductive. In most cases it just lowers throughput.
1131 *
1132 * On the other hand, a burst of queue creations may be caused also by
1133 * the start of an application that does not consist of a lot of
1134 * parallel I/O-bound threads. In fact, with a complex application,
1135 * several short processes may need to be executed to start-up the
1136 * application. In this respect, to start an application as quickly as
1137 * possible, the best thing to do is in any case to privilege the I/O
1138 * related to the application with respect to all other
1139 * I/O. Therefore, the best strategy to start as quickly as possible
1140 * an application that causes a burst of queue creations is to
1141 * weight-raise all the queues created during the burst. This is the
1142 * exact opposite of the best strategy for the other type of bursts.
1143 *
1144 * In the end, to take the best action for each of the two cases, the
1145 * two types of bursts need to be distinguished. Fortunately, this
1146 * seems relatively easy, by looking at the sizes of the bursts. In
1147 * particular, we found a threshold such that only bursts with a
1148 * larger size than that threshold are apparently caused by
1149 * services or commands such as systemd or git grep. For brevity,
1150 * hereafter we call just 'large' these bursts. BFQ *does not*
1151 * weight-raise queues whose creation occurs in a large burst. In
1152 * addition, for each of these queues BFQ performs or does not perform
1153 * idling depending on which choice boosts the throughput more. The
1154 * exact choice depends on the device and request pattern at
1155 * hand.
1156 *
1157 * Unfortunately, false positives may occur while an interactive task
1158 * is starting (e.g., an application is being started). The
1159 * consequence is that the queues associated with the task do not
1160 * enjoy weight raising as expected. Fortunately these false positives
1161 * are very rare. They typically occur if some service happens to
1162 * start doing I/O exactly when the interactive task starts.
1163 *
1164 * Turning back to the next function, it implements all the steps
1165 * needed to detect the occurrence of a large burst and to properly
1166 * mark all the queues belonging to it (so that they can then be
1167 * treated in a different way). This goal is achieved by maintaining a
1168 * "burst list" that holds, temporarily, the queues that belong to the
1169 * burst in progress. The list is then used to mark these queues as
1170 * belonging to a large burst if the burst does become large. The main
1171 * steps are the following.
1172 *
1173 * . when the very first queue is created, the queue is inserted into the
1174 * list (as it could be the first queue in a possible burst)
1175 *
1176 * . if the current burst has not yet become large, and a queue Q that does
1177 * not yet belong to the burst is activated shortly after the last time
1178 * at which a new queue entered the burst list, then the function appends
1179 * Q to the burst list
1180 *
1181 * . if, as a consequence of the previous step, the burst size reaches
1182 * the large-burst threshold, then
1183 *
1184 * . all the queues in the burst list are marked as belonging to a
1185 * large burst
1186 *
1187 * . the burst list is deleted; in fact, the burst list already served
1188 * its purpose (keeping temporarily track of the queues in a burst,
1189 * so as to be able to mark them as belonging to a large burst in the
1190 * previous sub-step), and now is not needed any more
1191 *
1192 * . the device enters a large-burst mode
1193 *
1194 * . if a queue Q that does not belong to the burst is created while
1195 * the device is in large-burst mode and shortly after the last time
1196 * at which a queue either entered the burst list or was marked as
1197 * belonging to the current large burst, then Q is immediately marked
1198 * as belonging to a large burst.
1199 *
1200 * . if a queue Q that does not belong to the burst is created a while
1201 * later, i.e., not shortly after, than the last time at which a queue
1202 * either entered the burst list or was marked as belonging to the
1203 * current large burst, then the current burst is deemed as finished and:
1204 *
1205 * . the large-burst mode is reset if set
1206 *
1207 * . the burst list is emptied
1208 *
1209 * . Q is inserted in the burst list, as Q may be the first queue
1210 * in a possible new burst (then the burst list contains just Q
1211 * after this step).
1212 */
1213static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
1214{
1215 /*
1216 * If bfqq is already in the burst list or is part of a large
1217 * burst, or finally has just been split, then there is
1218 * nothing else to do.
1219 */
1220 if (!hlist_unhashed(&bfqq->burst_list_node) ||
1221 bfq_bfqq_in_large_burst(bfqq) ||
1222 time_is_after_eq_jiffies(bfqq->split_time +
1223 msecs_to_jiffies(10)))
1224 return;
1225
1226 /*
1227 * If bfqq's creation happens late enough, or bfqq belongs to
1228 * a different group than the burst group, then the current
1229 * burst is finished, and related data structures must be
1230 * reset.
1231 *
1232 * In this respect, consider the special case where bfqq is
1233 * the very first queue created after BFQ is selected for this
1234 * device. In this case, last_ins_in_burst and
1235 * burst_parent_entity are not yet significant when we get
1236 * here. But it is easy to verify that, whether or not the
1237 * following condition is true, bfqq will end up being
1238 * inserted into the burst list. In particular the list will
1239 * happen to contain only bfqq. And this is exactly what has
1240 * to happen, as bfqq may be the first queue of the first
1241 * burst.
1242 */
1243 if (time_is_before_jiffies(bfqd->last_ins_in_burst +
1244 bfqd->bfq_burst_interval) ||
1245 bfqq->entity.parent != bfqd->burst_parent_entity) {
1246 bfqd->large_burst = false;
1247 bfq_reset_burst_list(bfqd, bfqq);
1248 goto end;
1249 }
1250
1251 /*
1252 * If we get here, then bfqq is being activated shortly after the
1253 * last queue. So, if the current burst is also large, we can mark
1254 * bfqq as belonging to this large burst immediately.
1255 */
1256 if (bfqd->large_burst) {
1257 bfq_mark_bfqq_in_large_burst(bfqq);
1258 goto end;
1259 }
1260
1261 /*
1262 * If we get here, then a large-burst state has not yet been
1263 * reached, but bfqq is being activated shortly after the last
1264 * queue. Then we add bfqq to the burst.
1265 */
1266 bfq_add_to_burst(bfqd, bfqq);
1267end:
1268 /*
1269 * At this point, bfqq either has been added to the current
1270 * burst or has caused the current burst to terminate and a
1271 * possible new burst to start. In particular, in the second
1272 * case, bfqq has become the first queue in the possible new
1273 * burst. In both cases last_ins_in_burst needs to be moved
1274 * forward.
1275 */
1276 bfqd->last_ins_in_burst = jiffies;
1277}
1278
Paolo Valenteaee69d72017-04-19 08:29:02 -06001279static int bfq_bfqq_budget_left(struct bfq_queue *bfqq)
1280{
1281 struct bfq_entity *entity = &bfqq->entity;
1282
1283 return entity->budget - entity->service;
1284}
1285
1286/*
1287 * If enough samples have been computed, return the current max budget
1288 * stored in bfqd, which is dynamically updated according to the
1289 * estimated disk peak rate; otherwise return the default max budget
1290 */
1291static int bfq_max_budget(struct bfq_data *bfqd)
1292{
1293 if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1294 return bfq_default_max_budget;
1295 else
1296 return bfqd->bfq_max_budget;
1297}
1298
1299/*
1300 * Return min budget, which is a fraction of the current or default
1301 * max budget (trying with 1/32)
1302 */
1303static int bfq_min_budget(struct bfq_data *bfqd)
1304{
1305 if (bfqd->budgets_assigned < bfq_stats_min_budgets)
1306 return bfq_default_max_budget / 32;
1307 else
1308 return bfqd->bfq_max_budget / 32;
1309}
1310
Paolo Valenteaee69d72017-04-19 08:29:02 -06001311/*
1312 * The next function, invoked after the input queue bfqq switches from
1313 * idle to busy, updates the budget of bfqq. The function also tells
1314 * whether the in-service queue should be expired, by returning
1315 * true. The purpose of expiring the in-service queue is to give bfqq
1316 * the chance to possibly preempt the in-service queue, and the reason
Paolo Valente44e44a12017-04-12 18:23:12 +02001317 * for preempting the in-service queue is to achieve one of the two
1318 * goals below.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001319 *
Paolo Valente44e44a12017-04-12 18:23:12 +02001320 * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has
1321 * expired because it has remained idle. In particular, bfqq may have
1322 * expired for one of the following two reasons:
Paolo Valenteaee69d72017-04-19 08:29:02 -06001323 *
1324 * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
1325 * and did not make it to issue a new request before its last
1326 * request was served;
1327 *
1328 * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
1329 * a new request before the expiration of the idling-time.
1330 *
1331 * Even if bfqq has expired for one of the above reasons, the process
1332 * associated with the queue may be however issuing requests greedily,
1333 * and thus be sensitive to the bandwidth it receives (bfqq may have
1334 * remained idle for other reasons: CPU high load, bfqq not enjoying
1335 * idling, I/O throttling somewhere in the path from the process to
1336 * the I/O scheduler, ...). But if, after every expiration for one of
1337 * the above two reasons, bfqq has to wait for the service of at least
1338 * one full budget of another queue before being served again, then
1339 * bfqq is likely to get a much lower bandwidth or resource time than
1340 * its reserved ones. To address this issue, two countermeasures need
1341 * to be taken.
1342 *
1343 * First, the budget and the timestamps of bfqq need to be updated in
1344 * a special way on bfqq reactivation: they need to be updated as if
1345 * bfqq did not remain idle and did not expire. In fact, if they are
1346 * computed as if bfqq expired and remained idle until reactivation,
1347 * then the process associated with bfqq is treated as if, instead of
1348 * being greedy, it stopped issuing requests when bfqq remained idle,
1349 * and restarts issuing requests only on this reactivation. In other
1350 * words, the scheduler does not help the process recover the "service
1351 * hole" between bfqq expiration and reactivation. As a consequence,
1352 * the process receives a lower bandwidth than its reserved one. In
1353 * contrast, to recover this hole, the budget must be updated as if
1354 * bfqq was not expired at all before this reactivation, i.e., it must
1355 * be set to the value of the remaining budget when bfqq was
1356 * expired. Along the same line, timestamps need to be assigned the
1357 * value they had the last time bfqq was selected for service, i.e.,
1358 * before last expiration. Thus timestamps need to be back-shifted
1359 * with respect to their normal computation (see [1] for more details
1360 * on this tricky aspect).
1361 *
1362 * Secondly, to allow the process to recover the hole, the in-service
1363 * queue must be expired too, to give bfqq the chance to preempt it
1364 * immediately. In fact, if bfqq has to wait for a full budget of the
1365 * in-service queue to be completed, then it may become impossible to
1366 * let the process recover the hole, even if the back-shifted
1367 * timestamps of bfqq are lower than those of the in-service queue. If
1368 * this happens for most or all of the holes, then the process may not
1369 * receive its reserved bandwidth. In this respect, it is worth noting
1370 * that, being the service of outstanding requests unpreemptible, a
1371 * little fraction of the holes may however be unrecoverable, thereby
1372 * causing a little loss of bandwidth.
1373 *
1374 * The last important point is detecting whether bfqq does need this
1375 * bandwidth recovery. In this respect, the next function deems the
1376 * process associated with bfqq greedy, and thus allows it to recover
1377 * the hole, if: 1) the process is waiting for the arrival of a new
1378 * request (which implies that bfqq expired for one of the above two
1379 * reasons), and 2) such a request has arrived soon. The first
1380 * condition is controlled through the flag non_blocking_wait_rq,
1381 * while the second through the flag arrived_in_time. If both
1382 * conditions hold, then the function computes the budget in the
1383 * above-described special way, and signals that the in-service queue
1384 * should be expired. Timestamp back-shifting is done later in
1385 * __bfq_activate_entity.
Paolo Valente44e44a12017-04-12 18:23:12 +02001386 *
1387 * 2. Reduce latency. Even if timestamps are not backshifted to let
1388 * the process associated with bfqq recover a service hole, bfqq may
1389 * however happen to have, after being (re)activated, a lower finish
1390 * timestamp than the in-service queue. That is, the next budget of
1391 * bfqq may have to be completed before the one of the in-service
1392 * queue. If this is the case, then preempting the in-service queue
1393 * allows this goal to be achieved, apart from the unpreemptible,
1394 * outstanding requests mentioned above.
1395 *
1396 * Unfortunately, regardless of which of the above two goals one wants
1397 * to achieve, service trees need first to be updated to know whether
1398 * the in-service queue must be preempted. To have service trees
1399 * correctly updated, the in-service queue must be expired and
1400 * rescheduled, and bfqq must be scheduled too. This is one of the
1401 * most costly operations (in future versions, the scheduling
1402 * mechanism may be re-designed in such a way to make it possible to
1403 * know whether preemption is needed without needing to update service
1404 * trees). In addition, queue preemptions almost always cause random
1405 * I/O, and thus loss of throughput. Because of these facts, the next
1406 * function adopts the following simple scheme to avoid both costly
1407 * operations and too frequent preemptions: it requests the expiration
1408 * of the in-service queue (unconditionally) only for queues that need
1409 * to recover a hole, or that either are weight-raised or deserve to
1410 * be weight-raised.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001411 */
1412static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd,
1413 struct bfq_queue *bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001414 bool arrived_in_time,
1415 bool wr_or_deserves_wr)
Paolo Valenteaee69d72017-04-19 08:29:02 -06001416{
1417 struct bfq_entity *entity = &bfqq->entity;
1418
Paolo Valente218cb892019-01-29 12:06:26 +01001419 /*
1420 * In the next compound condition, we check also whether there
1421 * is some budget left, because otherwise there is no point in
1422 * trying to go on serving bfqq with this same budget: bfqq
1423 * would be expired immediately after being selected for
1424 * service. This would only cause useless overhead.
1425 */
1426 if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time &&
1427 bfq_bfqq_budget_left(bfqq) > 0) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06001428 /*
1429 * We do not clear the flag non_blocking_wait_rq here, as
1430 * the latter is used in bfq_activate_bfqq to signal
1431 * that timestamps need to be back-shifted (and is
1432 * cleared right after).
1433 */
1434
1435 /*
1436 * In next assignment we rely on that either
1437 * entity->service or entity->budget are not updated
1438 * on expiration if bfqq is empty (see
1439 * __bfq_bfqq_recalc_budget). Thus both quantities
1440 * remain unchanged after such an expiration, and the
1441 * following statement therefore assigns to
1442 * entity->budget the remaining budget on such an
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001443 * expiration.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001444 */
1445 entity->budget = min_t(unsigned long,
1446 bfq_bfqq_budget_left(bfqq),
1447 bfqq->max_budget);
1448
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001449 /*
1450 * At this point, we have used entity->service to get
1451 * the budget left (needed for updating
1452 * entity->budget). Thus we finally can, and have to,
1453 * reset entity->service. The latter must be reset
1454 * because bfqq would otherwise be charged again for
1455 * the service it has received during its previous
1456 * service slot(s).
1457 */
1458 entity->service = 0;
1459
Paolo Valenteaee69d72017-04-19 08:29:02 -06001460 return true;
1461 }
1462
Paolo Valente9fae8dd2018-06-25 21:55:36 +02001463 /*
1464 * We can finally complete expiration, by setting service to 0.
1465 */
1466 entity->service = 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001467 entity->budget = max_t(unsigned long, bfqq->max_budget,
1468 bfq_serv_to_charge(bfqq->next_rq, bfqq));
1469 bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
Paolo Valente44e44a12017-04-12 18:23:12 +02001470 return wr_or_deserves_wr;
1471}
1472
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001473/*
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001474 * Return the farthest past time instant according to jiffies
1475 * macros.
1476 */
1477static unsigned long bfq_smallest_from_now(void)
1478{
1479 return jiffies - MAX_JIFFY_OFFSET;
1480}
1481
Paolo Valente44e44a12017-04-12 18:23:12 +02001482static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd,
1483 struct bfq_queue *bfqq,
1484 unsigned int old_wr_coeff,
1485 bool wr_or_deserves_wr,
Paolo Valente77b7dce2017-04-12 18:23:13 +02001486 bool interactive,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001487 bool in_burst,
Paolo Valente77b7dce2017-04-12 18:23:13 +02001488 bool soft_rt)
Paolo Valente44e44a12017-04-12 18:23:12 +02001489{
1490 if (old_wr_coeff == 1 && wr_or_deserves_wr) {
1491 /* start a weight-raising period */
Paolo Valente77b7dce2017-04-12 18:23:13 +02001492 if (interactive) {
Paolo Valente8a8747d2018-01-13 12:05:18 +01001493 bfqq->service_from_wr = 0;
Paolo Valente77b7dce2017-04-12 18:23:13 +02001494 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1495 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1496 } else {
Paolo Valente4baa8bb2017-09-21 11:04:00 +02001497 /*
1498 * No interactive weight raising in progress
1499 * here: assign minus infinity to
1500 * wr_start_at_switch_to_srt, to make sure
1501 * that, at the end of the soft-real-time
1502 * weight raising periods that is starting
1503 * now, no interactive weight-raising period
1504 * may be wrongly considered as still in
1505 * progress (and thus actually started by
1506 * mistake).
1507 */
1508 bfqq->wr_start_at_switch_to_srt =
1509 bfq_smallest_from_now();
Paolo Valente77b7dce2017-04-12 18:23:13 +02001510 bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1511 BFQ_SOFTRT_WEIGHT_FACTOR;
1512 bfqq->wr_cur_max_time =
1513 bfqd->bfq_wr_rt_max_time;
1514 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001515
1516 /*
1517 * If needed, further reduce budget to make sure it is
1518 * close to bfqq's backlog, so as to reduce the
1519 * scheduling-error component due to a too large
1520 * budget. Do not care about throughput consequences,
1521 * but only about latency. Finally, do not assign a
1522 * too small budget either, to avoid increasing
1523 * latency by causing too frequent expirations.
1524 */
1525 bfqq->entity.budget = min_t(unsigned long,
1526 bfqq->entity.budget,
1527 2 * bfq_min_budget(bfqd));
1528 } else if (old_wr_coeff > 1) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02001529 if (interactive) { /* update wr coeff and duration */
1530 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1531 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001532 } else if (in_burst)
1533 bfqq->wr_coeff = 1;
1534 else if (soft_rt) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02001535 /*
1536 * The application is now or still meeting the
1537 * requirements for being deemed soft rt. We
1538 * can then correctly and safely (re)charge
1539 * the weight-raising duration for the
1540 * application with the weight-raising
1541 * duration for soft rt applications.
1542 *
1543 * In particular, doing this recharge now, i.e.,
1544 * before the weight-raising period for the
1545 * application finishes, reduces the probability
1546 * of the following negative scenario:
1547 * 1) the weight of a soft rt application is
1548 * raised at startup (as for any newly
1549 * created application),
1550 * 2) since the application is not interactive,
1551 * at a certain time weight-raising is
1552 * stopped for the application,
1553 * 3) at that time the application happens to
1554 * still have pending requests, and hence
1555 * is destined to not have a chance to be
1556 * deemed soft rt before these requests are
1557 * completed (see the comments to the
1558 * function bfq_bfqq_softrt_next_start()
1559 * for details on soft rt detection),
1560 * 4) these pending requests experience a high
1561 * latency because the application is not
1562 * weight-raised while they are pending.
1563 */
1564 if (bfqq->wr_cur_max_time !=
1565 bfqd->bfq_wr_rt_max_time) {
1566 bfqq->wr_start_at_switch_to_srt =
1567 bfqq->last_wr_start_finish;
1568
1569 bfqq->wr_cur_max_time =
1570 bfqd->bfq_wr_rt_max_time;
1571 bfqq->wr_coeff = bfqd->bfq_wr_coeff *
1572 BFQ_SOFTRT_WEIGHT_FACTOR;
1573 }
1574 bfqq->last_wr_start_finish = jiffies;
1575 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001576 }
1577}
1578
1579static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd,
1580 struct bfq_queue *bfqq)
1581{
1582 return bfqq->dispatched == 0 &&
1583 time_is_before_jiffies(
1584 bfqq->budget_timeout +
1585 bfqd->bfq_wr_min_idle_time);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001586}
1587
1588static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd,
1589 struct bfq_queue *bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001590 int old_wr_coeff,
1591 struct request *rq,
1592 bool *interactive)
Paolo Valenteaee69d72017-04-19 08:29:02 -06001593{
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001594 bool soft_rt, in_burst, wr_or_deserves_wr,
1595 bfqq_wants_to_preempt,
Paolo Valente44e44a12017-04-12 18:23:12 +02001596 idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq),
Paolo Valenteaee69d72017-04-19 08:29:02 -06001597 /*
1598 * See the comments on
1599 * bfq_bfqq_update_budg_for_activation for
1600 * details on the usage of the next variable.
1601 */
1602 arrived_in_time = ktime_get_ns() <=
1603 bfqq->ttime.last_end_request +
1604 bfqd->bfq_slice_idle * 3;
1605
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001606
Paolo Valenteaee69d72017-04-19 08:29:02 -06001607 /*
Paolo Valente44e44a12017-04-12 18:23:12 +02001608 * bfqq deserves to be weight-raised if:
1609 * - it is sync,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001610 * - it does not belong to a large burst,
Arianna Avanzini36eca892017-04-12 18:23:16 +02001611 * - it has been idle for enough time or is soft real-time,
1612 * - is linked to a bfq_io_cq (it is not shared in any sense).
Paolo Valente44e44a12017-04-12 18:23:12 +02001613 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001614 in_burst = bfq_bfqq_in_large_burst(bfqq);
Paolo Valente77b7dce2017-04-12 18:23:13 +02001615 soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001616 !in_burst &&
Davide Sapienzaf6c3ca02018-05-31 16:45:08 +02001617 time_is_before_jiffies(bfqq->soft_rt_next_start) &&
1618 bfqq->dispatched == 0;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001619 *interactive = !in_burst && idle_for_long_time;
Paolo Valente44e44a12017-04-12 18:23:12 +02001620 wr_or_deserves_wr = bfqd->low_latency &&
1621 (bfqq->wr_coeff > 1 ||
Arianna Avanzini36eca892017-04-12 18:23:16 +02001622 (bfq_bfqq_sync(bfqq) &&
1623 bfqq->bic && (*interactive || soft_rt)));
Paolo Valente44e44a12017-04-12 18:23:12 +02001624
1625 /*
1626 * Using the last flag, update budget and check whether bfqq
1627 * may want to preempt the in-service queue.
Paolo Valenteaee69d72017-04-19 08:29:02 -06001628 */
1629 bfqq_wants_to_preempt =
1630 bfq_bfqq_update_budg_for_activation(bfqd, bfqq,
Paolo Valente44e44a12017-04-12 18:23:12 +02001631 arrived_in_time,
1632 wr_or_deserves_wr);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001633
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001634 /*
1635 * If bfqq happened to be activated in a burst, but has been
1636 * idle for much more than an interactive queue, then we
1637 * assume that, in the overall I/O initiated in the burst, the
1638 * I/O associated with bfqq is finished. So bfqq does not need
1639 * to be treated as a queue belonging to a burst
1640 * anymore. Accordingly, we reset bfqq's in_large_burst flag
1641 * if set, and remove bfqq from the burst list if it's
1642 * there. We do not decrement burst_size, because the fact
1643 * that bfqq does not need to belong to the burst list any
1644 * more does not invalidate the fact that bfqq was created in
1645 * a burst.
1646 */
1647 if (likely(!bfq_bfqq_just_created(bfqq)) &&
1648 idle_for_long_time &&
1649 time_is_before_jiffies(
1650 bfqq->budget_timeout +
1651 msecs_to_jiffies(10000))) {
1652 hlist_del_init(&bfqq->burst_list_node);
1653 bfq_clear_bfqq_in_large_burst(bfqq);
1654 }
1655
1656 bfq_clear_bfqq_just_created(bfqq);
1657
1658
Paolo Valenteaee69d72017-04-19 08:29:02 -06001659 if (!bfq_bfqq_IO_bound(bfqq)) {
1660 if (arrived_in_time) {
1661 bfqq->requests_within_timer++;
1662 if (bfqq->requests_within_timer >=
1663 bfqd->bfq_requests_within_timer)
1664 bfq_mark_bfqq_IO_bound(bfqq);
1665 } else
1666 bfqq->requests_within_timer = 0;
1667 }
1668
Paolo Valente44e44a12017-04-12 18:23:12 +02001669 if (bfqd->low_latency) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02001670 if (unlikely(time_is_after_jiffies(bfqq->split_time)))
1671 /* wraparound */
1672 bfqq->split_time =
1673 jiffies - bfqd->bfq_wr_min_idle_time - 1;
Paolo Valente44e44a12017-04-12 18:23:12 +02001674
Arianna Avanzini36eca892017-04-12 18:23:16 +02001675 if (time_is_before_jiffies(bfqq->split_time +
1676 bfqd->bfq_wr_min_idle_time)) {
1677 bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq,
1678 old_wr_coeff,
1679 wr_or_deserves_wr,
1680 *interactive,
Arianna Avanzinie1b23242017-04-12 18:23:20 +02001681 in_burst,
Arianna Avanzini36eca892017-04-12 18:23:16 +02001682 soft_rt);
1683
1684 if (old_wr_coeff != bfqq->wr_coeff)
1685 bfqq->entity.prio_changed = 1;
1686 }
Paolo Valente44e44a12017-04-12 18:23:12 +02001687 }
1688
Paolo Valente77b7dce2017-04-12 18:23:13 +02001689 bfqq->last_idle_bklogged = jiffies;
1690 bfqq->service_from_backlogged = 0;
1691 bfq_clear_bfqq_softrt_update(bfqq);
1692
Paolo Valenteaee69d72017-04-19 08:29:02 -06001693 bfq_add_bfqq_busy(bfqd, bfqq);
1694
1695 /*
1696 * Expire in-service queue only if preemption may be needed
1697 * for guarantees. In this respect, the function
1698 * next_queue_may_preempt just checks a simple, necessary
1699 * condition, and not a sufficient condition based on
1700 * timestamps. In fact, for the latter condition to be
1701 * evaluated, timestamps would need first to be updated, and
1702 * this operation is quite costly (see the comments on the
1703 * function bfq_bfqq_update_budg_for_activation).
1704 */
1705 if (bfqd->in_service_queue && bfqq_wants_to_preempt &&
Paolo Valente77b7dce2017-04-12 18:23:13 +02001706 bfqd->in_service_queue->wr_coeff < bfqq->wr_coeff &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06001707 next_queue_may_preempt(bfqd))
1708 bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
1709 false, BFQQE_PREEMPTED);
1710}
1711
1712static void bfq_add_request(struct request *rq)
1713{
1714 struct bfq_queue *bfqq = RQ_BFQQ(rq);
1715 struct bfq_data *bfqd = bfqq->bfqd;
1716 struct request *next_rq, *prev;
Paolo Valente44e44a12017-04-12 18:23:12 +02001717 unsigned int old_wr_coeff = bfqq->wr_coeff;
1718 bool interactive = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001719
1720 bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
1721 bfqq->queued[rq_is_sync(rq)]++;
1722 bfqd->queued++;
1723
Paolo Valente2341d6622019-03-12 09:59:29 +01001724 if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_bfqq_sync(bfqq)) {
1725 /*
1726 * Periodically reset inject limit, to make sure that
1727 * the latter eventually drops in case workload
1728 * changes, see step (3) in the comments on
1729 * bfq_update_inject_limit().
1730 */
1731 if (time_is_before_eq_jiffies(bfqq->decrease_time_jif +
1732 msecs_to_jiffies(1000))) {
1733 /* invalidate baseline total service time */
1734 bfqq->last_serv_time_ns = 0;
1735
1736 /*
1737 * Reset pointer in case we are waiting for
1738 * some request completion.
1739 */
1740 bfqd->waited_rq = NULL;
1741
1742 /*
1743 * If bfqq has a short think time, then start
1744 * by setting the inject limit to 0
1745 * prudentially, because the service time of
1746 * an injected I/O request may be higher than
1747 * the think time of bfqq, and therefore, if
1748 * one request was injected when bfqq remains
1749 * empty, this injected request might delay
1750 * the service of the next I/O request for
1751 * bfqq significantly. In case bfqq can
1752 * actually tolerate some injection, then the
1753 * adaptive update will however raise the
1754 * limit soon. This lucky circumstance holds
1755 * exactly because bfqq has a short think
1756 * time, and thus, after remaining empty, is
1757 * likely to get new I/O enqueued---and then
1758 * completed---before being expired. This is
1759 * the very pattern that gives the
1760 * limit-update algorithm the chance to
1761 * measure the effect of injection on request
1762 * service times, and then to update the limit
1763 * accordingly.
1764 *
1765 * On the opposite end, if bfqq has a long
1766 * think time, then start directly by 1,
1767 * because:
1768 * a) on the bright side, keeping at most one
1769 * request in service in the drive is unlikely
1770 * to cause any harm to the latency of bfqq's
1771 * requests, as the service time of a single
1772 * request is likely to be lower than the
1773 * think time of bfqq;
1774 * b) on the downside, after becoming empty,
1775 * bfqq is likely to expire before getting its
1776 * next request. With this request arrival
1777 * pattern, it is very hard to sample total
1778 * service times and update the inject limit
1779 * accordingly (see comments on
1780 * bfq_update_inject_limit()). So the limit is
1781 * likely to be never, or at least seldom,
1782 * updated. As a consequence, by setting the
1783 * limit to 1, we avoid that no injection ever
1784 * occurs with bfqq. On the downside, this
1785 * proactive step further reduces chances to
1786 * actually compute the baseline total service
1787 * time. Thus it reduces chances to execute the
1788 * limit-update algorithm and possibly raise the
1789 * limit to more than 1.
1790 */
1791 if (bfq_bfqq_has_short_ttime(bfqq))
1792 bfqq->inject_limit = 0;
1793 else
1794 bfqq->inject_limit = 1;
1795 bfqq->decrease_time_jif = jiffies;
1796 }
1797
1798 /*
1799 * The following conditions must hold to setup a new
1800 * sampling of total service time, and then a new
1801 * update of the inject limit:
1802 * - bfqq is in service, because the total service
1803 * time is evaluated only for the I/O requests of
1804 * the queues in service;
1805 * - this is the right occasion to compute or to
1806 * lower the baseline total service time, because
1807 * there are actually no requests in the drive,
1808 * or
1809 * the baseline total service time is available, and
1810 * this is the right occasion to compute the other
1811 * quantity needed to update the inject limit, i.e.,
1812 * the total service time caused by the amount of
1813 * injection allowed by the current value of the
1814 * limit. It is the right occasion because injection
1815 * has actually been performed during the service
1816 * hole, and there are still in-flight requests,
1817 * which are very likely to be exactly the injected
1818 * requests, or part of them;
1819 * - the minimum interval for sampling the total
1820 * service time and updating the inject limit has
1821 * elapsed.
1822 */
1823 if (bfqq == bfqd->in_service_queue &&
1824 (bfqd->rq_in_driver == 0 ||
1825 (bfqq->last_serv_time_ns > 0 &&
1826 bfqd->rqs_injected && bfqd->rq_in_driver > 0)) &&
1827 time_is_before_eq_jiffies(bfqq->decrease_time_jif +
1828 msecs_to_jiffies(100))) {
1829 bfqd->last_empty_occupied_ns = ktime_get_ns();
1830 /*
1831 * Start the state machine for measuring the
1832 * total service time of rq: setting
1833 * wait_dispatch will cause bfqd->waited_rq to
1834 * be set when rq will be dispatched.
1835 */
1836 bfqd->wait_dispatch = true;
1837 bfqd->rqs_injected = false;
1838 }
1839 }
1840
Paolo Valenteaee69d72017-04-19 08:29:02 -06001841 elv_rb_add(&bfqq->sort_list, rq);
1842
1843 /*
1844 * Check if this request is a better next-serve candidate.
1845 */
1846 prev = bfqq->next_rq;
1847 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
1848 bfqq->next_rq = next_rq;
1849
Arianna Avanzini36eca892017-04-12 18:23:16 +02001850 /*
1851 * Adjust priority tree position, if next_rq changes.
1852 */
1853 if (prev != bfqq->next_rq)
1854 bfq_pos_tree_add_move(bfqd, bfqq);
1855
Paolo Valenteaee69d72017-04-19 08:29:02 -06001856 if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */
Paolo Valente44e44a12017-04-12 18:23:12 +02001857 bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff,
1858 rq, &interactive);
1859 else {
1860 if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
1861 time_is_before_jiffies(
1862 bfqq->last_wr_start_finish +
1863 bfqd->bfq_wr_min_inter_arr_async)) {
1864 bfqq->wr_coeff = bfqd->bfq_wr_coeff;
1865 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
1866
Paolo Valentecfd69712017-04-12 18:23:15 +02001867 bfqd->wr_busy_queues++;
Paolo Valente44e44a12017-04-12 18:23:12 +02001868 bfqq->entity.prio_changed = 1;
1869 }
1870 if (prev != bfqq->next_rq)
1871 bfq_updated_next_req(bfqd, bfqq);
1872 }
1873
1874 /*
1875 * Assign jiffies to last_wr_start_finish in the following
1876 * cases:
1877 *
1878 * . if bfqq is not going to be weight-raised, because, for
1879 * non weight-raised queues, last_wr_start_finish stores the
1880 * arrival time of the last request; as of now, this piece
1881 * of information is used only for deciding whether to
1882 * weight-raise async queues
1883 *
1884 * . if bfqq is not weight-raised, because, if bfqq is now
1885 * switching to weight-raised, then last_wr_start_finish
1886 * stores the time when weight-raising starts
1887 *
1888 * . if bfqq is interactive, because, regardless of whether
1889 * bfqq is currently weight-raised, the weight-raising
1890 * period must start or restart (this case is considered
1891 * separately because it is not detected by the above
1892 * conditions, if bfqq is already weight-raised)
Paolo Valente77b7dce2017-04-12 18:23:13 +02001893 *
1894 * last_wr_start_finish has to be updated also if bfqq is soft
1895 * real-time, because the weight-raising period is constantly
1896 * restarted on idle-to-busy transitions for these queues, but
1897 * this is already done in bfq_bfqq_handle_idle_busy_switch if
1898 * needed.
Paolo Valente44e44a12017-04-12 18:23:12 +02001899 */
1900 if (bfqd->low_latency &&
1901 (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
1902 bfqq->last_wr_start_finish = jiffies;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001903}
1904
1905static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
1906 struct bio *bio,
1907 struct request_queue *q)
1908{
1909 struct bfq_queue *bfqq = bfqd->bio_bfqq;
1910
1911
1912 if (bfqq)
1913 return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio));
1914
1915 return NULL;
1916}
1917
Paolo Valenteab0e43e2017-04-12 18:23:10 +02001918static sector_t get_sdist(sector_t last_pos, struct request *rq)
1919{
1920 if (last_pos)
1921 return abs(blk_rq_pos(rq) - last_pos);
1922
1923 return 0;
1924}
1925
Paolo Valenteaee69d72017-04-19 08:29:02 -06001926#if 0 /* Still not clear if we can do without next two functions */
1927static void bfq_activate_request(struct request_queue *q, struct request *rq)
1928{
1929 struct bfq_data *bfqd = q->elevator->elevator_data;
1930
1931 bfqd->rq_in_driver++;
Paolo Valenteaee69d72017-04-19 08:29:02 -06001932}
1933
1934static void bfq_deactivate_request(struct request_queue *q, struct request *rq)
1935{
1936 struct bfq_data *bfqd = q->elevator->elevator_data;
1937
1938 bfqd->rq_in_driver--;
1939}
1940#endif
1941
1942static void bfq_remove_request(struct request_queue *q,
1943 struct request *rq)
1944{
1945 struct bfq_queue *bfqq = RQ_BFQQ(rq);
1946 struct bfq_data *bfqd = bfqq->bfqd;
1947 const int sync = rq_is_sync(rq);
1948
1949 if (bfqq->next_rq == rq) {
1950 bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
1951 bfq_updated_next_req(bfqd, bfqq);
1952 }
1953
1954 if (rq->queuelist.prev != &rq->queuelist)
1955 list_del_init(&rq->queuelist);
1956 bfqq->queued[sync]--;
1957 bfqd->queued--;
1958 elv_rb_del(&bfqq->sort_list, rq);
1959
1960 elv_rqhash_del(q, rq);
1961 if (q->last_merge == rq)
1962 q->last_merge = NULL;
1963
1964 if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
1965 bfqq->next_rq = NULL;
1966
1967 if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001968 bfq_del_bfqq_busy(bfqd, bfqq, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001969 /*
1970 * bfqq emptied. In normal operation, when
1971 * bfqq is empty, bfqq->entity.service and
1972 * bfqq->entity.budget must contain,
1973 * respectively, the service received and the
1974 * budget used last time bfqq emptied. These
1975 * facts do not hold in this case, as at least
1976 * this last removal occurred while bfqq is
1977 * not in service. To avoid inconsistencies,
1978 * reset both bfqq->entity.service and
1979 * bfqq->entity.budget, if bfqq has still a
1980 * process that may issue I/O requests to it.
1981 */
1982 bfqq->entity.budget = bfqq->entity.service = 0;
1983 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02001984
1985 /*
1986 * Remove queue from request-position tree as it is empty.
1987 */
1988 if (bfqq->pos_root) {
1989 rb_erase(&bfqq->pos_node, bfqq->pos_root);
1990 bfqq->pos_root = NULL;
1991 }
Paolo Valente05e90282017-12-20 12:38:31 +01001992 } else {
1993 bfq_pos_tree_add_move(bfqd, bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06001994 }
1995
1996 if (rq->cmd_flags & REQ_META)
1997 bfqq->meta_pending--;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02001998
Paolo Valenteaee69d72017-04-19 08:29:02 -06001999}
2000
2001static bool bfq_bio_merge(struct blk_mq_hw_ctx *hctx, struct bio *bio)
2002{
2003 struct request_queue *q = hctx->queue;
2004 struct bfq_data *bfqd = q->elevator->elevator_data;
2005 struct request *free = NULL;
2006 /*
2007 * bfq_bic_lookup grabs the queue_lock: invoke it now and
2008 * store its return value for later use, to avoid nesting
2009 * queue_lock inside the bfqd->lock. We assume that the bic
2010 * returned by bfq_bic_lookup does not go away before
2011 * bfqd->lock is taken.
2012 */
2013 struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q);
2014 bool ret;
2015
2016 spin_lock_irq(&bfqd->lock);
2017
2018 if (bic)
2019 bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
2020 else
2021 bfqd->bio_bfqq = NULL;
2022 bfqd->bio_bic = bic;
2023
2024 ret = blk_mq_sched_try_merge(q, bio, &free);
2025
2026 if (free)
2027 blk_mq_free_request(free);
2028 spin_unlock_irq(&bfqd->lock);
2029
2030 return ret;
2031}
2032
2033static int bfq_request_merge(struct request_queue *q, struct request **req,
2034 struct bio *bio)
2035{
2036 struct bfq_data *bfqd = q->elevator->elevator_data;
2037 struct request *__rq;
2038
2039 __rq = bfq_find_rq_fmerge(bfqd, bio, q);
2040 if (__rq && elv_bio_merge_ok(__rq, bio)) {
2041 *req = __rq;
2042 return ELEVATOR_FRONT_MERGE;
2043 }
2044
2045 return ELEVATOR_NO_MERGE;
2046}
2047
Paolo Valente18e5a572018-05-04 19:17:01 +02002048static struct bfq_queue *bfq_init_rq(struct request *rq);
2049
Paolo Valenteaee69d72017-04-19 08:29:02 -06002050static void bfq_request_merged(struct request_queue *q, struct request *req,
2051 enum elv_merge type)
2052{
2053 if (type == ELEVATOR_FRONT_MERGE &&
2054 rb_prev(&req->rb_node) &&
2055 blk_rq_pos(req) <
2056 blk_rq_pos(container_of(rb_prev(&req->rb_node),
2057 struct request, rb_node))) {
Paolo Valente18e5a572018-05-04 19:17:01 +02002058 struct bfq_queue *bfqq = bfq_init_rq(req);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002059 struct bfq_data *bfqd = bfqq->bfqd;
2060 struct request *prev, *next_rq;
2061
2062 /* Reposition request in its sort_list */
2063 elv_rb_del(&bfqq->sort_list, req);
2064 elv_rb_add(&bfqq->sort_list, req);
2065
2066 /* Choose next request to be served for bfqq */
2067 prev = bfqq->next_rq;
2068 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
2069 bfqd->last_position);
2070 bfqq->next_rq = next_rq;
2071 /*
Arianna Avanzini36eca892017-04-12 18:23:16 +02002072 * If next_rq changes, update both the queue's budget to
2073 * fit the new request and the queue's position in its
2074 * rq_pos_tree.
Paolo Valenteaee69d72017-04-19 08:29:02 -06002075 */
Arianna Avanzini36eca892017-04-12 18:23:16 +02002076 if (prev != bfqq->next_rq) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06002077 bfq_updated_next_req(bfqd, bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002078 bfq_pos_tree_add_move(bfqd, bfqq);
2079 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06002080 }
2081}
2082
Paolo Valente8abfa4d2018-05-31 08:48:05 -06002083/*
2084 * This function is called to notify the scheduler that the requests
2085 * rq and 'next' have been merged, with 'next' going away. BFQ
2086 * exploits this hook to address the following issue: if 'next' has a
2087 * fifo_time lower that rq, then the fifo_time of rq must be set to
2088 * the value of 'next', to not forget the greater age of 'next'.
Paolo Valente8abfa4d2018-05-31 08:48:05 -06002089 *
2090 * NOTE: in this function we assume that rq is in a bfq_queue, basing
2091 * on that rq is picked from the hash table q->elevator->hash, which,
2092 * in its turn, is filled only with I/O requests present in
2093 * bfq_queues, while BFQ is in use for the request queue q. In fact,
2094 * the function that fills this hash table (elv_rqhash_add) is called
2095 * only by bfq_insert_request.
2096 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06002097static void bfq_requests_merged(struct request_queue *q, struct request *rq,
2098 struct request *next)
2099{
Paolo Valente18e5a572018-05-04 19:17:01 +02002100 struct bfq_queue *bfqq = bfq_init_rq(rq),
2101 *next_bfqq = bfq_init_rq(next);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002102
Paolo Valenteaee69d72017-04-19 08:29:02 -06002103 /*
2104 * If next and rq belong to the same bfq_queue and next is older
2105 * than rq, then reposition rq in the fifo (by substituting next
2106 * with rq). Otherwise, if next and rq belong to different
2107 * bfq_queues, never reposition rq: in fact, we would have to
2108 * reposition it with respect to next's position in its own fifo,
2109 * which would most certainly be too expensive with respect to
2110 * the benefits.
2111 */
2112 if (bfqq == next_bfqq &&
2113 !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
2114 next->fifo_time < rq->fifo_time) {
2115 list_del_init(&rq->queuelist);
2116 list_replace_init(&next->queuelist, &rq->queuelist);
2117 rq->fifo_time = next->fifo_time;
2118 }
2119
2120 if (bfqq->next_rq == next)
2121 bfqq->next_rq = rq;
2122
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002123 bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002124}
2125
Paolo Valente44e44a12017-04-12 18:23:12 +02002126/* Must be called with bfqq != NULL */
2127static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
2128{
Paolo Valentecfd69712017-04-12 18:23:15 +02002129 if (bfq_bfqq_busy(bfqq))
2130 bfqq->bfqd->wr_busy_queues--;
Paolo Valente44e44a12017-04-12 18:23:12 +02002131 bfqq->wr_coeff = 1;
2132 bfqq->wr_cur_max_time = 0;
Paolo Valente77b7dce2017-04-12 18:23:13 +02002133 bfqq->last_wr_start_finish = jiffies;
Paolo Valente44e44a12017-04-12 18:23:12 +02002134 /*
2135 * Trigger a weight change on the next invocation of
2136 * __bfq_entity_update_weight_prio.
2137 */
2138 bfqq->entity.prio_changed = 1;
2139}
2140
Paolo Valenteea25da42017-04-19 08:48:24 -06002141void bfq_end_wr_async_queues(struct bfq_data *bfqd,
2142 struct bfq_group *bfqg)
Paolo Valente44e44a12017-04-12 18:23:12 +02002143{
2144 int i, j;
2145
2146 for (i = 0; i < 2; i++)
2147 for (j = 0; j < IOPRIO_BE_NR; j++)
2148 if (bfqg->async_bfqq[i][j])
2149 bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
2150 if (bfqg->async_idle_bfqq)
2151 bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
2152}
2153
2154static void bfq_end_wr(struct bfq_data *bfqd)
2155{
2156 struct bfq_queue *bfqq;
2157
2158 spin_lock_irq(&bfqd->lock);
2159
2160 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
2161 bfq_bfqq_end_wr(bfqq);
2162 list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
2163 bfq_bfqq_end_wr(bfqq);
2164 bfq_end_wr_async(bfqd);
2165
2166 spin_unlock_irq(&bfqd->lock);
2167}
2168
Arianna Avanzini36eca892017-04-12 18:23:16 +02002169static sector_t bfq_io_struct_pos(void *io_struct, bool request)
2170{
2171 if (request)
2172 return blk_rq_pos(io_struct);
2173 else
2174 return ((struct bio *)io_struct)->bi_iter.bi_sector;
2175}
2176
2177static int bfq_rq_close_to_sector(void *io_struct, bool request,
2178 sector_t sector)
2179{
2180 return abs(bfq_io_struct_pos(io_struct, request) - sector) <=
2181 BFQQ_CLOSE_THR;
2182}
2183
2184static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd,
2185 struct bfq_queue *bfqq,
2186 sector_t sector)
2187{
2188 struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
2189 struct rb_node *parent, *node;
2190 struct bfq_queue *__bfqq;
2191
2192 if (RB_EMPTY_ROOT(root))
2193 return NULL;
2194
2195 /*
2196 * First, if we find a request starting at the end of the last
2197 * request, choose it.
2198 */
2199 __bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
2200 if (__bfqq)
2201 return __bfqq;
2202
2203 /*
2204 * If the exact sector wasn't found, the parent of the NULL leaf
2205 * will contain the closest sector (rq_pos_tree sorted by
2206 * next_request position).
2207 */
2208 __bfqq = rb_entry(parent, struct bfq_queue, pos_node);
2209 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2210 return __bfqq;
2211
2212 if (blk_rq_pos(__bfqq->next_rq) < sector)
2213 node = rb_next(&__bfqq->pos_node);
2214 else
2215 node = rb_prev(&__bfqq->pos_node);
2216 if (!node)
2217 return NULL;
2218
2219 __bfqq = rb_entry(node, struct bfq_queue, pos_node);
2220 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
2221 return __bfqq;
2222
2223 return NULL;
2224}
2225
2226static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd,
2227 struct bfq_queue *cur_bfqq,
2228 sector_t sector)
2229{
2230 struct bfq_queue *bfqq;
2231
2232 /*
2233 * We shall notice if some of the queues are cooperating,
2234 * e.g., working closely on the same area of the device. In
2235 * that case, we can group them together and: 1) don't waste
2236 * time idling, and 2) serve the union of their requests in
2237 * the best possible order for throughput.
2238 */
2239 bfqq = bfqq_find_close(bfqd, cur_bfqq, sector);
2240 if (!bfqq || bfqq == cur_bfqq)
2241 return NULL;
2242
2243 return bfqq;
2244}
2245
2246static struct bfq_queue *
2247bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2248{
2249 int process_refs, new_process_refs;
2250 struct bfq_queue *__bfqq;
2251
2252 /*
2253 * If there are no process references on the new_bfqq, then it is
2254 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
2255 * may have dropped their last reference (not just their last process
2256 * reference).
2257 */
2258 if (!bfqq_process_refs(new_bfqq))
2259 return NULL;
2260
2261 /* Avoid a circular list and skip interim queue merges. */
2262 while ((__bfqq = new_bfqq->new_bfqq)) {
2263 if (__bfqq == bfqq)
2264 return NULL;
2265 new_bfqq = __bfqq;
2266 }
2267
2268 process_refs = bfqq_process_refs(bfqq);
2269 new_process_refs = bfqq_process_refs(new_bfqq);
2270 /*
2271 * If the process for the bfqq has gone away, there is no
2272 * sense in merging the queues.
2273 */
2274 if (process_refs == 0 || new_process_refs == 0)
2275 return NULL;
2276
2277 bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
2278 new_bfqq->pid);
2279
2280 /*
2281 * Merging is just a redirection: the requests of the process
2282 * owning one of the two queues are redirected to the other queue.
2283 * The latter queue, in its turn, is set as shared if this is the
2284 * first time that the requests of some process are redirected to
2285 * it.
2286 *
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02002287 * We redirect bfqq to new_bfqq and not the opposite, because
2288 * we are in the context of the process owning bfqq, thus we
2289 * have the io_cq of this process. So we can immediately
2290 * configure this io_cq to redirect the requests of the
2291 * process to new_bfqq. In contrast, the io_cq of new_bfqq is
2292 * not available any more (new_bfqq->bic == NULL).
Arianna Avanzini36eca892017-04-12 18:23:16 +02002293 *
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02002294 * Anyway, even in case new_bfqq coincides with the in-service
2295 * queue, redirecting requests the in-service queue is the
2296 * best option, as we feed the in-service queue with new
2297 * requests close to the last request served and, by doing so,
2298 * are likely to increase the throughput.
Arianna Avanzini36eca892017-04-12 18:23:16 +02002299 */
2300 bfqq->new_bfqq = new_bfqq;
2301 new_bfqq->ref += process_refs;
2302 return new_bfqq;
2303}
2304
2305static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq,
2306 struct bfq_queue *new_bfqq)
2307{
Paolo Valente7b8fa3b2017-12-20 12:38:33 +01002308 if (bfq_too_late_for_merging(new_bfqq))
2309 return false;
2310
Arianna Avanzini36eca892017-04-12 18:23:16 +02002311 if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) ||
2312 (bfqq->ioprio_class != new_bfqq->ioprio_class))
2313 return false;
2314
2315 /*
2316 * If either of the queues has already been detected as seeky,
2317 * then merging it with the other queue is unlikely to lead to
2318 * sequential I/O.
2319 */
2320 if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq))
2321 return false;
2322
2323 /*
2324 * Interleaved I/O is known to be done by (some) applications
2325 * only for reads, so it does not make sense to merge async
2326 * queues.
2327 */
2328 if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq))
2329 return false;
2330
2331 return true;
2332}
2333
2334/*
Arianna Avanzini36eca892017-04-12 18:23:16 +02002335 * Attempt to schedule a merge of bfqq with the currently in-service
2336 * queue or with a close queue among the scheduled queues. Return
2337 * NULL if no merge was scheduled, a pointer to the shared bfq_queue
2338 * structure otherwise.
2339 *
2340 * The OOM queue is not allowed to participate to cooperation: in fact, since
2341 * the requests temporarily redirected to the OOM queue could be redirected
2342 * again to dedicated queues at any time, the state needed to correctly
2343 * handle merging with the OOM queue would be quite complex and expensive
2344 * to maintain. Besides, in such a critical condition as an out of memory,
2345 * the benefits of queue merging may be little relevant, or even negligible.
2346 *
Arianna Avanzini36eca892017-04-12 18:23:16 +02002347 * WARNING: queue merging may impair fairness among non-weight raised
2348 * queues, for at least two reasons: 1) the original weight of a
2349 * merged queue may change during the merged state, 2) even being the
2350 * weight the same, a merged queue may be bloated with many more
2351 * requests than the ones produced by its originally-associated
2352 * process.
2353 */
2354static struct bfq_queue *
2355bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
2356 void *io_struct, bool request)
2357{
2358 struct bfq_queue *in_service_bfqq, *new_bfqq;
2359
Paolo Valente7b8fa3b2017-12-20 12:38:33 +01002360 /*
2361 * Prevent bfqq from being merged if it has been created too
2362 * long ago. The idea is that true cooperating processes, and
2363 * thus their associated bfq_queues, are supposed to be
2364 * created shortly after each other. This is the case, e.g.,
2365 * for KVM/QEMU and dump I/O threads. Basing on this
2366 * assumption, the following filtering greatly reduces the
2367 * probability that two non-cooperating processes, which just
2368 * happen to do close I/O for some short time interval, have
2369 * their queues merged by mistake.
2370 */
2371 if (bfq_too_late_for_merging(bfqq))
2372 return NULL;
2373
Arianna Avanzini36eca892017-04-12 18:23:16 +02002374 if (bfqq->new_bfqq)
2375 return bfqq->new_bfqq;
2376
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002377 if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq))
Arianna Avanzini36eca892017-04-12 18:23:16 +02002378 return NULL;
2379
2380 /* If there is only one backlogged queue, don't search. */
Paolo Valente73d58112019-01-29 12:06:29 +01002381 if (bfq_tot_busy_queues(bfqd) == 1)
Arianna Avanzini36eca892017-04-12 18:23:16 +02002382 return NULL;
2383
2384 in_service_bfqq = bfqd->in_service_queue;
2385
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002386 if (in_service_bfqq && in_service_bfqq != bfqq &&
2387 likely(in_service_bfqq != &bfqd->oom_bfqq) &&
Paolo Valente058fdec2019-01-29 12:06:38 +01002388 bfq_rq_close_to_sector(io_struct, request,
2389 bfqd->in_serv_last_pos) &&
Arianna Avanzini36eca892017-04-12 18:23:16 +02002390 bfqq->entity.parent == in_service_bfqq->entity.parent &&
2391 bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) {
2392 new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
2393 if (new_bfqq)
2394 return new_bfqq;
2395 }
2396 /*
2397 * Check whether there is a cooperator among currently scheduled
2398 * queues. The only thing we need is that the bio/request is not
2399 * NULL, as we need it to establish whether a cooperator exists.
2400 */
Arianna Avanzini36eca892017-04-12 18:23:16 +02002401 new_bfqq = bfq_find_close_cooperator(bfqd, bfqq,
2402 bfq_io_struct_pos(io_struct, request));
2403
Angelo Ruocco4403e4e2017-12-20 12:38:34 +01002404 if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) &&
Arianna Avanzini36eca892017-04-12 18:23:16 +02002405 bfq_may_be_close_cooperator(bfqq, new_bfqq))
2406 return bfq_setup_merge(bfqq, new_bfqq);
2407
2408 return NULL;
2409}
2410
2411static void bfq_bfqq_save_state(struct bfq_queue *bfqq)
2412{
2413 struct bfq_io_cq *bic = bfqq->bic;
2414
2415 /*
2416 * If !bfqq->bic, the queue is already shared or its requests
2417 * have already been redirected to a shared queue; both idle window
2418 * and weight raising state have already been saved. Do nothing.
2419 */
2420 if (!bic)
2421 return;
2422
2423 bic->saved_ttime = bfqq->ttime;
Paolo Valented5be3fe2017-08-04 07:35:10 +02002424 bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002425 bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02002426 bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
2427 bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
Paolo Valente894df932017-09-21 11:04:02 +02002428 if (unlikely(bfq_bfqq_just_created(bfqq) &&
Angelo Ruocco1be6e8a2017-12-20 12:38:32 +01002429 !bfq_bfqq_in_large_burst(bfqq) &&
2430 bfqq->bfqd->low_latency)) {
Paolo Valente894df932017-09-21 11:04:02 +02002431 /*
2432 * bfqq being merged right after being created: bfqq
2433 * would have deserved interactive weight raising, but
2434 * did not make it to be set in a weight-raised state,
2435 * because of this early merge. Store directly the
2436 * weight-raising state that would have been assigned
2437 * to bfqq, so that to avoid that bfqq unjustly fails
2438 * to enjoy weight raising if split soon.
2439 */
2440 bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff;
2441 bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd);
2442 bic->saved_last_wr_start_finish = jiffies;
2443 } else {
2444 bic->saved_wr_coeff = bfqq->wr_coeff;
2445 bic->saved_wr_start_at_switch_to_srt =
2446 bfqq->wr_start_at_switch_to_srt;
2447 bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish;
2448 bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time;
2449 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02002450}
2451
Arianna Avanzini36eca892017-04-12 18:23:16 +02002452static void
2453bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
2454 struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
2455{
2456 bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
2457 (unsigned long)new_bfqq->pid);
2458 /* Save weight raising and idle window of the merged queues */
2459 bfq_bfqq_save_state(bfqq);
2460 bfq_bfqq_save_state(new_bfqq);
2461 if (bfq_bfqq_IO_bound(bfqq))
2462 bfq_mark_bfqq_IO_bound(new_bfqq);
2463 bfq_clear_bfqq_IO_bound(bfqq);
2464
2465 /*
2466 * If bfqq is weight-raised, then let new_bfqq inherit
2467 * weight-raising. To reduce false positives, neglect the case
2468 * where bfqq has just been created, but has not yet made it
2469 * to be weight-raised (which may happen because EQM may merge
2470 * bfqq even before bfq_add_request is executed for the first
Arianna Avanzinie1b23242017-04-12 18:23:20 +02002471 * time for bfqq). Handling this case would however be very
2472 * easy, thanks to the flag just_created.
Arianna Avanzini36eca892017-04-12 18:23:16 +02002473 */
2474 if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) {
2475 new_bfqq->wr_coeff = bfqq->wr_coeff;
2476 new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time;
2477 new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish;
2478 new_bfqq->wr_start_at_switch_to_srt =
2479 bfqq->wr_start_at_switch_to_srt;
2480 if (bfq_bfqq_busy(new_bfqq))
2481 bfqd->wr_busy_queues++;
2482 new_bfqq->entity.prio_changed = 1;
2483 }
2484
2485 if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */
2486 bfqq->wr_coeff = 1;
2487 bfqq->entity.prio_changed = 1;
2488 if (bfq_bfqq_busy(bfqq))
2489 bfqd->wr_busy_queues--;
2490 }
2491
2492 bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d",
2493 bfqd->wr_busy_queues);
2494
2495 /*
Arianna Avanzini36eca892017-04-12 18:23:16 +02002496 * Merge queues (that is, let bic redirect its requests to new_bfqq)
2497 */
2498 bic_set_bfqq(bic, new_bfqq, 1);
2499 bfq_mark_bfqq_coop(new_bfqq);
2500 /*
2501 * new_bfqq now belongs to at least two bics (it is a shared queue):
2502 * set new_bfqq->bic to NULL. bfqq either:
2503 * - does not belong to any bic any more, and hence bfqq->bic must
2504 * be set to NULL, or
2505 * - is a queue whose owning bics have already been redirected to a
2506 * different queue, hence the queue is destined to not belong to
2507 * any bic soon and bfqq->bic is already NULL (therefore the next
2508 * assignment causes no harm).
2509 */
2510 new_bfqq->bic = NULL;
2511 bfqq->bic = NULL;
2512 /* release process reference to bfqq */
2513 bfq_put_queue(bfqq);
2514}
2515
Paolo Valenteaee69d72017-04-19 08:29:02 -06002516static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq,
2517 struct bio *bio)
2518{
2519 struct bfq_data *bfqd = q->elevator->elevator_data;
2520 bool is_sync = op_is_sync(bio->bi_opf);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002521 struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06002522
2523 /*
2524 * Disallow merge of a sync bio into an async request.
2525 */
2526 if (is_sync && !rq_is_sync(rq))
2527 return false;
2528
2529 /*
2530 * Lookup the bfqq that this bio will be queued with. Allow
2531 * merge only if rq is queued there.
2532 */
2533 if (!bfqq)
2534 return false;
2535
Arianna Avanzini36eca892017-04-12 18:23:16 +02002536 /*
2537 * We take advantage of this function to perform an early merge
2538 * of the queues of possible cooperating processes.
2539 */
2540 new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
2541 if (new_bfqq) {
2542 /*
2543 * bic still points to bfqq, then it has not yet been
2544 * redirected to some other bfq_queue, and a queue
2545 * merge beween bfqq and new_bfqq can be safely
2546 * fulfillled, i.e., bic can be redirected to new_bfqq
2547 * and bfqq can be put.
2548 */
2549 bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq,
2550 new_bfqq);
2551 /*
2552 * If we get here, bio will be queued into new_queue,
2553 * so use new_bfqq to decide whether bio and rq can be
2554 * merged.
2555 */
2556 bfqq = new_bfqq;
2557
2558 /*
2559 * Change also bqfd->bio_bfqq, as
2560 * bfqd->bio_bic now points to new_bfqq, and
2561 * this function may be invoked again (and then may
2562 * use again bqfd->bio_bfqq).
2563 */
2564 bfqd->bio_bfqq = bfqq;
2565 }
2566
Paolo Valenteaee69d72017-04-19 08:29:02 -06002567 return bfqq == RQ_BFQQ(rq);
2568}
2569
Paolo Valente44e44a12017-04-12 18:23:12 +02002570/*
2571 * Set the maximum time for the in-service queue to consume its
2572 * budget. This prevents seeky processes from lowering the throughput.
2573 * In practice, a time-slice service scheme is used with seeky
2574 * processes.
2575 */
2576static void bfq_set_budget_timeout(struct bfq_data *bfqd,
2577 struct bfq_queue *bfqq)
2578{
Paolo Valente77b7dce2017-04-12 18:23:13 +02002579 unsigned int timeout_coeff;
2580
2581 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
2582 timeout_coeff = 1;
2583 else
2584 timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
2585
Paolo Valente44e44a12017-04-12 18:23:12 +02002586 bfqd->last_budget_start = ktime_get();
2587
2588 bfqq->budget_timeout = jiffies +
Paolo Valente77b7dce2017-04-12 18:23:13 +02002589 bfqd->bfq_timeout * timeout_coeff;
Paolo Valente44e44a12017-04-12 18:23:12 +02002590}
2591
Paolo Valenteaee69d72017-04-19 08:29:02 -06002592static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
2593 struct bfq_queue *bfqq)
2594{
2595 if (bfqq) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06002596 bfq_clear_bfqq_fifo_expire(bfqq);
2597
2598 bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8;
2599
Paolo Valente77b7dce2017-04-12 18:23:13 +02002600 if (time_is_before_jiffies(bfqq->last_wr_start_finish) &&
2601 bfqq->wr_coeff > 1 &&
2602 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
2603 time_is_before_jiffies(bfqq->budget_timeout)) {
2604 /*
2605 * For soft real-time queues, move the start
2606 * of the weight-raising period forward by the
2607 * time the queue has not received any
2608 * service. Otherwise, a relatively long
2609 * service delay is likely to cause the
2610 * weight-raising period of the queue to end,
2611 * because of the short duration of the
2612 * weight-raising period of a soft real-time
2613 * queue. It is worth noting that this move
2614 * is not so dangerous for the other queues,
2615 * because soft real-time queues are not
2616 * greedy.
2617 *
2618 * To not add a further variable, we use the
2619 * overloaded field budget_timeout to
2620 * determine for how long the queue has not
2621 * received service, i.e., how much time has
2622 * elapsed since the queue expired. However,
2623 * this is a little imprecise, because
2624 * budget_timeout is set to jiffies if bfqq
2625 * not only expires, but also remains with no
2626 * request.
2627 */
2628 if (time_after(bfqq->budget_timeout,
2629 bfqq->last_wr_start_finish))
2630 bfqq->last_wr_start_finish +=
2631 jiffies - bfqq->budget_timeout;
2632 else
2633 bfqq->last_wr_start_finish = jiffies;
2634 }
2635
Paolo Valente44e44a12017-04-12 18:23:12 +02002636 bfq_set_budget_timeout(bfqd, bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002637 bfq_log_bfqq(bfqd, bfqq,
2638 "set_in_service_queue, cur-budget = %d",
2639 bfqq->entity.budget);
2640 }
2641
2642 bfqd->in_service_queue = bfqq;
2643}
2644
2645/*
2646 * Get and set a new queue for service.
2647 */
2648static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
2649{
2650 struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
2651
2652 __bfq_set_in_service_queue(bfqd, bfqq);
2653 return bfqq;
2654}
2655
Paolo Valenteaee69d72017-04-19 08:29:02 -06002656static void bfq_arm_slice_timer(struct bfq_data *bfqd)
2657{
2658 struct bfq_queue *bfqq = bfqd->in_service_queue;
Paolo Valenteaee69d72017-04-19 08:29:02 -06002659 u32 sl;
2660
Paolo Valenteaee69d72017-04-19 08:29:02 -06002661 bfq_mark_bfqq_wait_request(bfqq);
2662
2663 /*
2664 * We don't want to idle for seeks, but we do want to allow
2665 * fair distribution of slice time for a process doing back-to-back
2666 * seeks. So allow a little bit of time for him to submit a new rq.
2667 */
2668 sl = bfqd->bfq_slice_idle;
2669 /*
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02002670 * Unless the queue is being weight-raised or the scenario is
2671 * asymmetric, grant only minimum idle time if the queue
2672 * is seeky. A long idling is preserved for a weight-raised
2673 * queue, or, more in general, in an asymmetric scenario,
2674 * because a long idling is needed for guaranteeing to a queue
2675 * its reserved share of the throughput (in particular, it is
2676 * needed if the queue has a higher weight than some other
2677 * queue).
Paolo Valenteaee69d72017-04-19 08:29:02 -06002678 */
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02002679 if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
Paolo Valentefb53ac62019-03-12 09:59:28 +01002680 !bfq_asymmetric_scenario(bfqd, bfqq))
Paolo Valenteaee69d72017-04-19 08:29:02 -06002681 sl = min_t(u64, sl, BFQ_MIN_TT);
Paolo Valente778c02a2019-03-12 09:59:27 +01002682 else if (bfqq->wr_coeff > 1)
2683 sl = max_t(u32, sl, 20ULL * NSEC_PER_MSEC);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002684
2685 bfqd->last_idling_start = ktime_get();
Paolo Valente2341d6622019-03-12 09:59:29 +01002686 bfqd->last_idling_start_jiffies = jiffies;
2687
Paolo Valenteaee69d72017-04-19 08:29:02 -06002688 hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl),
2689 HRTIMER_MODE_REL);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002690 bfqg_stats_set_start_idle_time(bfqq_group(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06002691}
2692
2693/*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002694 * In autotuning mode, max_budget is dynamically recomputed as the
2695 * amount of sectors transferred in timeout at the estimated peak
2696 * rate. This enables BFQ to utilize a full timeslice with a full
2697 * budget, even if the in-service queue is served at peak rate. And
2698 * this maximises throughput with sequential workloads.
2699 */
2700static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd)
2701{
2702 return (u64)bfqd->peak_rate * USEC_PER_MSEC *
2703 jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT;
2704}
2705
Paolo Valente44e44a12017-04-12 18:23:12 +02002706/*
2707 * Update parameters related to throughput and responsiveness, as a
2708 * function of the estimated peak rate. See comments on
Paolo Valentee24f1c22018-05-31 16:45:06 +02002709 * bfq_calc_max_budget(), and on the ref_wr_duration array.
Paolo Valente44e44a12017-04-12 18:23:12 +02002710 */
2711static void update_thr_responsiveness_params(struct bfq_data *bfqd)
2712{
Paolo Valentee24f1c22018-05-31 16:45:06 +02002713 if (bfqd->bfq_user_max_budget == 0) {
Paolo Valente44e44a12017-04-12 18:23:12 +02002714 bfqd->bfq_max_budget =
2715 bfq_calc_max_budget(bfqd);
Paolo Valentee24f1c22018-05-31 16:45:06 +02002716 bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget);
Paolo Valente44e44a12017-04-12 18:23:12 +02002717 }
Paolo Valente44e44a12017-04-12 18:23:12 +02002718}
2719
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002720static void bfq_reset_rate_computation(struct bfq_data *bfqd,
2721 struct request *rq)
2722{
2723 if (rq != NULL) { /* new rq dispatch now, reset accordingly */
2724 bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns();
2725 bfqd->peak_rate_samples = 1;
2726 bfqd->sequential_samples = 0;
2727 bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size =
2728 blk_rq_sectors(rq);
2729 } else /* no new rq dispatched, just reset the number of samples */
2730 bfqd->peak_rate_samples = 0; /* full re-init on next disp. */
2731
2732 bfq_log(bfqd,
2733 "reset_rate_computation at end, sample %u/%u tot_sects %llu",
2734 bfqd->peak_rate_samples, bfqd->sequential_samples,
2735 bfqd->tot_sectors_dispatched);
2736}
2737
2738static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq)
2739{
2740 u32 rate, weight, divisor;
2741
2742 /*
2743 * For the convergence property to hold (see comments on
2744 * bfq_update_peak_rate()) and for the assessment to be
2745 * reliable, a minimum number of samples must be present, and
2746 * a minimum amount of time must have elapsed. If not so, do
2747 * not compute new rate. Just reset parameters, to get ready
2748 * for a new evaluation attempt.
2749 */
2750 if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES ||
2751 bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL)
2752 goto reset_computation;
2753
2754 /*
2755 * If a new request completion has occurred after last
2756 * dispatch, then, to approximate the rate at which requests
2757 * have been served by the device, it is more precise to
2758 * extend the observation interval to the last completion.
2759 */
2760 bfqd->delta_from_first =
2761 max_t(u64, bfqd->delta_from_first,
2762 bfqd->last_completion - bfqd->first_dispatch);
2763
2764 /*
2765 * Rate computed in sects/usec, and not sects/nsec, for
2766 * precision issues.
2767 */
2768 rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT,
2769 div_u64(bfqd->delta_from_first, NSEC_PER_USEC));
2770
2771 /*
2772 * Peak rate not updated if:
2773 * - the percentage of sequential dispatches is below 3/4 of the
2774 * total, and rate is below the current estimated peak rate
2775 * - rate is unreasonably high (> 20M sectors/sec)
2776 */
2777 if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 &&
2778 rate <= bfqd->peak_rate) ||
2779 rate > 20<<BFQ_RATE_SHIFT)
2780 goto reset_computation;
2781
2782 /*
2783 * We have to update the peak rate, at last! To this purpose,
2784 * we use a low-pass filter. We compute the smoothing constant
2785 * of the filter as a function of the 'weight' of the new
2786 * measured rate.
2787 *
2788 * As can be seen in next formulas, we define this weight as a
2789 * quantity proportional to how sequential the workload is,
2790 * and to how long the observation time interval is.
2791 *
2792 * The weight runs from 0 to 8. The maximum value of the
2793 * weight, 8, yields the minimum value for the smoothing
2794 * constant. At this minimum value for the smoothing constant,
2795 * the measured rate contributes for half of the next value of
2796 * the estimated peak rate.
2797 *
2798 * So, the first step is to compute the weight as a function
2799 * of how sequential the workload is. Note that the weight
2800 * cannot reach 9, because bfqd->sequential_samples cannot
2801 * become equal to bfqd->peak_rate_samples, which, in its
2802 * turn, holds true because bfqd->sequential_samples is not
2803 * incremented for the first sample.
2804 */
2805 weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples;
2806
2807 /*
2808 * Second step: further refine the weight as a function of the
2809 * duration of the observation interval.
2810 */
2811 weight = min_t(u32, 8,
2812 div_u64(weight * bfqd->delta_from_first,
2813 BFQ_RATE_REF_INTERVAL));
2814
2815 /*
2816 * Divisor ranging from 10, for minimum weight, to 2, for
2817 * maximum weight.
2818 */
2819 divisor = 10 - weight;
2820
2821 /*
2822 * Finally, update peak rate:
2823 *
2824 * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor
2825 */
2826 bfqd->peak_rate *= divisor-1;
2827 bfqd->peak_rate /= divisor;
2828 rate /= divisor; /* smoothing constant alpha = 1/divisor */
2829
2830 bfqd->peak_rate += rate;
Paolo Valentebc56e2c2018-03-26 16:06:24 +02002831
2832 /*
2833 * For a very slow device, bfqd->peak_rate can reach 0 (see
2834 * the minimum representable values reported in the comments
2835 * on BFQ_RATE_SHIFT). Push to 1 if this happens, to avoid
2836 * divisions by zero where bfqd->peak_rate is used as a
2837 * divisor.
2838 */
2839 bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate);
2840
Paolo Valente44e44a12017-04-12 18:23:12 +02002841 update_thr_responsiveness_params(bfqd);
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002842
2843reset_computation:
2844 bfq_reset_rate_computation(bfqd, rq);
2845}
2846
2847/*
2848 * Update the read/write peak rate (the main quantity used for
2849 * auto-tuning, see update_thr_responsiveness_params()).
2850 *
2851 * It is not trivial to estimate the peak rate (correctly): because of
2852 * the presence of sw and hw queues between the scheduler and the
2853 * device components that finally serve I/O requests, it is hard to
2854 * say exactly when a given dispatched request is served inside the
2855 * device, and for how long. As a consequence, it is hard to know
2856 * precisely at what rate a given set of requests is actually served
2857 * by the device.
2858 *
2859 * On the opposite end, the dispatch time of any request is trivially
2860 * available, and, from this piece of information, the "dispatch rate"
2861 * of requests can be immediately computed. So, the idea in the next
2862 * function is to use what is known, namely request dispatch times
2863 * (plus, when useful, request completion times), to estimate what is
2864 * unknown, namely in-device request service rate.
2865 *
2866 * The main issue is that, because of the above facts, the rate at
2867 * which a certain set of requests is dispatched over a certain time
2868 * interval can vary greatly with respect to the rate at which the
2869 * same requests are then served. But, since the size of any
2870 * intermediate queue is limited, and the service scheme is lossless
2871 * (no request is silently dropped), the following obvious convergence
2872 * property holds: the number of requests dispatched MUST become
2873 * closer and closer to the number of requests completed as the
2874 * observation interval grows. This is the key property used in
2875 * the next function to estimate the peak service rate as a function
2876 * of the observed dispatch rate. The function assumes to be invoked
2877 * on every request dispatch.
2878 */
2879static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq)
2880{
2881 u64 now_ns = ktime_get_ns();
2882
2883 if (bfqd->peak_rate_samples == 0) { /* first dispatch */
2884 bfq_log(bfqd, "update_peak_rate: goto reset, samples %d",
2885 bfqd->peak_rate_samples);
2886 bfq_reset_rate_computation(bfqd, rq);
2887 goto update_last_values; /* will add one sample */
2888 }
2889
2890 /*
2891 * Device idle for very long: the observation interval lasting
2892 * up to this dispatch cannot be a valid observation interval
2893 * for computing a new peak rate (similarly to the late-
2894 * completion event in bfq_completed_request()). Go to
2895 * update_rate_and_reset to have the following three steps
2896 * taken:
2897 * - close the observation interval at the last (previous)
2898 * request dispatch or completion
2899 * - compute rate, if possible, for that observation interval
2900 * - start a new observation interval with this dispatch
2901 */
2902 if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC &&
2903 bfqd->rq_in_driver == 0)
2904 goto update_rate_and_reset;
2905
2906 /* Update sampling information */
2907 bfqd->peak_rate_samples++;
2908
2909 if ((bfqd->rq_in_driver > 0 ||
2910 now_ns - bfqd->last_completion < BFQ_MIN_TT)
Paolo Valented87447d2019-01-29 12:06:33 +01002911 && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq))
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002912 bfqd->sequential_samples++;
2913
2914 bfqd->tot_sectors_dispatched += blk_rq_sectors(rq);
2915
2916 /* Reset max observed rq size every 32 dispatches */
2917 if (likely(bfqd->peak_rate_samples % 32))
2918 bfqd->last_rq_max_size =
2919 max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size);
2920 else
2921 bfqd->last_rq_max_size = blk_rq_sectors(rq);
2922
2923 bfqd->delta_from_first = now_ns - bfqd->first_dispatch;
2924
2925 /* Target observation interval not yet reached, go on sampling */
2926 if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL)
2927 goto update_last_values;
2928
2929update_rate_and_reset:
2930 bfq_update_rate_reset(bfqd, rq);
2931update_last_values:
2932 bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
Paolo Valente058fdec2019-01-29 12:06:38 +01002933 if (RQ_BFQQ(rq) == bfqd->in_service_queue)
2934 bfqd->in_serv_last_pos = bfqd->last_position;
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002935 bfqd->last_dispatch = now_ns;
2936}
2937
2938/*
Paolo Valenteaee69d72017-04-19 08:29:02 -06002939 * Remove request from internal lists.
2940 */
2941static void bfq_dispatch_remove(struct request_queue *q, struct request *rq)
2942{
2943 struct bfq_queue *bfqq = RQ_BFQQ(rq);
2944
2945 /*
2946 * For consistency, the next instruction should have been
2947 * executed after removing the request from the queue and
2948 * dispatching it. We execute instead this instruction before
2949 * bfq_remove_request() (and hence introduce a temporary
2950 * inconsistency), for efficiency. In fact, should this
2951 * dispatch occur for a non in-service bfqq, this anticipated
2952 * increment prevents two counters related to bfqq->dispatched
2953 * from risking to be, first, uselessly decremented, and then
2954 * incremented again when the (new) value of bfqq->dispatched
2955 * happens to be taken into account.
2956 */
2957 bfqq->dispatched++;
Paolo Valenteab0e43e2017-04-12 18:23:10 +02002958 bfq_update_peak_rate(q->elevator->elevator_data, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002959
2960 bfq_remove_request(q, rq);
2961}
2962
2963static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq)
2964{
Arianna Avanzini36eca892017-04-12 18:23:16 +02002965 /*
2966 * If this bfqq is shared between multiple processes, check
2967 * to make sure that those processes are still issuing I/Os
2968 * within the mean seek distance. If not, it may be time to
2969 * break the queues apart again.
2970 */
2971 if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
2972 bfq_mark_bfqq_split_coop(bfqq);
2973
Paolo Valente44e44a12017-04-12 18:23:12 +02002974 if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
2975 if (bfqq->dispatched == 0)
2976 /*
2977 * Overloading budget_timeout field to store
2978 * the time at which the queue remains with no
2979 * backlog and no outstanding request; used by
2980 * the weight-raising mechanism.
2981 */
2982 bfqq->budget_timeout = jiffies;
2983
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002984 bfq_del_bfqq_busy(bfqd, bfqq, true);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002985 } else {
Paolo Valente80294c32017-08-31 08:46:29 +02002986 bfq_requeue_bfqq(bfqd, bfqq, true);
Arianna Avanzini36eca892017-04-12 18:23:16 +02002987 /*
2988 * Resort priority tree of potential close cooperators.
2989 */
2990 bfq_pos_tree_add_move(bfqd, bfqq);
2991 }
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02002992
2993 /*
2994 * All in-service entities must have been properly deactivated
2995 * or requeued before executing the next function, which
2996 * resets all in-service entites as no more in service.
2997 */
2998 __bfq_bfqd_reset_in_service(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06002999}
3000
3001/**
3002 * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
3003 * @bfqd: device data.
3004 * @bfqq: queue to update.
3005 * @reason: reason for expiration.
3006 *
3007 * Handle the feedback on @bfqq budget at queue expiration.
3008 * See the body for detailed comments.
3009 */
3010static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
3011 struct bfq_queue *bfqq,
3012 enum bfqq_expiration reason)
3013{
3014 struct request *next_rq;
3015 int budget, min_budget;
3016
Paolo Valenteaee69d72017-04-19 08:29:02 -06003017 min_budget = bfq_min_budget(bfqd);
3018
Paolo Valente44e44a12017-04-12 18:23:12 +02003019 if (bfqq->wr_coeff == 1)
3020 budget = bfqq->max_budget;
3021 else /*
3022 * Use a constant, low budget for weight-raised queues,
3023 * to help achieve a low latency. Keep it slightly higher
3024 * than the minimum possible budget, to cause a little
3025 * bit fewer expirations.
3026 */
3027 budget = 2 * min_budget;
3028
Paolo Valenteaee69d72017-04-19 08:29:02 -06003029 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d",
3030 bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
3031 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d",
3032 budget, bfq_min_budget(bfqd));
3033 bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
3034 bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
3035
Paolo Valente44e44a12017-04-12 18:23:12 +02003036 if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06003037 switch (reason) {
3038 /*
3039 * Caveat: in all the following cases we trade latency
3040 * for throughput.
3041 */
3042 case BFQQE_TOO_IDLE:
Paolo Valente54b60452017-04-12 18:23:09 +02003043 /*
3044 * This is the only case where we may reduce
3045 * the budget: if there is no request of the
3046 * process still waiting for completion, then
3047 * we assume (tentatively) that the timer has
3048 * expired because the batch of requests of
3049 * the process could have been served with a
3050 * smaller budget. Hence, betting that
3051 * process will behave in the same way when it
3052 * becomes backlogged again, we reduce its
3053 * next budget. As long as we guess right,
3054 * this budget cut reduces the latency
3055 * experienced by the process.
3056 *
3057 * However, if there are still outstanding
3058 * requests, then the process may have not yet
3059 * issued its next request just because it is
3060 * still waiting for the completion of some of
3061 * the still outstanding ones. So in this
3062 * subcase we do not reduce its budget, on the
3063 * contrary we increase it to possibly boost
3064 * the throughput, as discussed in the
3065 * comments to the BUDGET_TIMEOUT case.
3066 */
3067 if (bfqq->dispatched > 0) /* still outstanding reqs */
3068 budget = min(budget * 2, bfqd->bfq_max_budget);
3069 else {
3070 if (budget > 5 * min_budget)
3071 budget -= 4 * min_budget;
3072 else
3073 budget = min_budget;
3074 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06003075 break;
3076 case BFQQE_BUDGET_TIMEOUT:
Paolo Valente54b60452017-04-12 18:23:09 +02003077 /*
3078 * We double the budget here because it gives
3079 * the chance to boost the throughput if this
3080 * is not a seeky process (and has bumped into
3081 * this timeout because of, e.g., ZBR).
3082 */
3083 budget = min(budget * 2, bfqd->bfq_max_budget);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003084 break;
3085 case BFQQE_BUDGET_EXHAUSTED:
3086 /*
3087 * The process still has backlog, and did not
3088 * let either the budget timeout or the disk
3089 * idling timeout expire. Hence it is not
3090 * seeky, has a short thinktime and may be
3091 * happy with a higher budget too. So
3092 * definitely increase the budget of this good
3093 * candidate to boost the disk throughput.
3094 */
Paolo Valente54b60452017-04-12 18:23:09 +02003095 budget = min(budget * 4, bfqd->bfq_max_budget);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003096 break;
3097 case BFQQE_NO_MORE_REQUESTS:
3098 /*
3099 * For queues that expire for this reason, it
3100 * is particularly important to keep the
3101 * budget close to the actual service they
3102 * need. Doing so reduces the timestamp
3103 * misalignment problem described in the
3104 * comments in the body of
3105 * __bfq_activate_entity. In fact, suppose
3106 * that a queue systematically expires for
3107 * BFQQE_NO_MORE_REQUESTS and presents a
3108 * new request in time to enjoy timestamp
3109 * back-shifting. The larger the budget of the
3110 * queue is with respect to the service the
3111 * queue actually requests in each service
3112 * slot, the more times the queue can be
3113 * reactivated with the same virtual finish
3114 * time. It follows that, even if this finish
3115 * time is pushed to the system virtual time
3116 * to reduce the consequent timestamp
3117 * misalignment, the queue unjustly enjoys for
3118 * many re-activations a lower finish time
3119 * than all newly activated queues.
3120 *
3121 * The service needed by bfqq is measured
3122 * quite precisely by bfqq->entity.service.
3123 * Since bfqq does not enjoy device idling,
3124 * bfqq->entity.service is equal to the number
3125 * of sectors that the process associated with
3126 * bfqq requested to read/write before waiting
3127 * for request completions, or blocking for
3128 * other reasons.
3129 */
3130 budget = max_t(int, bfqq->entity.service, min_budget);
3131 break;
3132 default:
3133 return;
3134 }
Paolo Valente44e44a12017-04-12 18:23:12 +02003135 } else if (!bfq_bfqq_sync(bfqq)) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06003136 /*
3137 * Async queues get always the maximum possible
3138 * budget, as for them we do not care about latency
3139 * (in addition, their ability to dispatch is limited
3140 * by the charging factor).
3141 */
3142 budget = bfqd->bfq_max_budget;
3143 }
3144
3145 bfqq->max_budget = budget;
3146
3147 if (bfqd->budgets_assigned >= bfq_stats_min_budgets &&
3148 !bfqd->bfq_user_max_budget)
3149 bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget);
3150
3151 /*
3152 * If there is still backlog, then assign a new budget, making
3153 * sure that it is large enough for the next request. Since
3154 * the finish time of bfqq must be kept in sync with the
3155 * budget, be sure to call __bfq_bfqq_expire() *after* this
3156 * update.
3157 *
3158 * If there is no backlog, then no need to update the budget;
3159 * it will be updated on the arrival of a new request.
3160 */
3161 next_rq = bfqq->next_rq;
3162 if (next_rq)
3163 bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
3164 bfq_serv_to_charge(next_rq, bfqq));
3165
3166 bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d",
3167 next_rq ? blk_rq_sectors(next_rq) : 0,
3168 bfqq->entity.budget);
3169}
3170
Paolo Valenteaee69d72017-04-19 08:29:02 -06003171/*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003172 * Return true if the process associated with bfqq is "slow". The slow
3173 * flag is used, in addition to the budget timeout, to reduce the
3174 * amount of service provided to seeky processes, and thus reduce
3175 * their chances to lower the throughput. More details in the comments
3176 * on the function bfq_bfqq_expire().
3177 *
3178 * An important observation is in order: as discussed in the comments
3179 * on the function bfq_update_peak_rate(), with devices with internal
3180 * queues, it is hard if ever possible to know when and for how long
3181 * an I/O request is processed by the device (apart from the trivial
3182 * I/O pattern where a new request is dispatched only after the
3183 * previous one has been completed). This makes it hard to evaluate
3184 * the real rate at which the I/O requests of each bfq_queue are
3185 * served. In fact, for an I/O scheduler like BFQ, serving a
3186 * bfq_queue means just dispatching its requests during its service
3187 * slot (i.e., until the budget of the queue is exhausted, or the
3188 * queue remains idle, or, finally, a timeout fires). But, during the
3189 * service slot of a bfq_queue, around 100 ms at most, the device may
3190 * be even still processing requests of bfq_queues served in previous
3191 * service slots. On the opposite end, the requests of the in-service
3192 * bfq_queue may be completed after the service slot of the queue
3193 * finishes.
3194 *
3195 * Anyway, unless more sophisticated solutions are used
3196 * (where possible), the sum of the sizes of the requests dispatched
3197 * during the service slot of a bfq_queue is probably the only
3198 * approximation available for the service received by the bfq_queue
3199 * during its service slot. And this sum is the quantity used in this
3200 * function to evaluate the I/O speed of a process.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003201 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003202static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq,
3203 bool compensate, enum bfqq_expiration reason,
3204 unsigned long *delta_ms)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003205{
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003206 ktime_t delta_ktime;
3207 u32 delta_usecs;
3208 bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */
Paolo Valenteaee69d72017-04-19 08:29:02 -06003209
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003210 if (!bfq_bfqq_sync(bfqq))
Paolo Valenteaee69d72017-04-19 08:29:02 -06003211 return false;
3212
3213 if (compensate)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003214 delta_ktime = bfqd->last_idling_start;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003215 else
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003216 delta_ktime = ktime_get();
3217 delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start);
3218 delta_usecs = ktime_to_us(delta_ktime);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003219
3220 /* don't use too short time intervals */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003221 if (delta_usecs < 1000) {
3222 if (blk_queue_nonrot(bfqd->queue))
3223 /*
3224 * give same worst-case guarantees as idling
3225 * for seeky
3226 */
3227 *delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC;
3228 else /* charge at least one seek */
3229 *delta_ms = bfq_slice_idle / NSEC_PER_MSEC;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003230
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003231 return slow;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003232 }
3233
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003234 *delta_ms = delta_usecs / USEC_PER_MSEC;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003235
3236 /*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003237 * Use only long (> 20ms) intervals to filter out excessive
3238 * spikes in service rate estimation.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003239 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003240 if (delta_usecs > 20000) {
3241 /*
3242 * Caveat for rotational devices: processes doing I/O
3243 * in the slower disk zones tend to be slow(er) even
3244 * if not seeky. In this respect, the estimated peak
3245 * rate is likely to be an average over the disk
3246 * surface. Accordingly, to not be too harsh with
3247 * unlucky processes, a process is deemed slow only if
3248 * its rate has been lower than half of the estimated
3249 * peak rate.
3250 */
3251 slow = bfqq->entity.service < bfqd->bfq_max_budget / 2;
3252 }
3253
3254 bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow);
3255
3256 return slow;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003257}
3258
3259/*
Paolo Valente77b7dce2017-04-12 18:23:13 +02003260 * To be deemed as soft real-time, an application must meet two
3261 * requirements. First, the application must not require an average
3262 * bandwidth higher than the approximate bandwidth required to playback or
3263 * record a compressed high-definition video.
3264 * The next function is invoked on the completion of the last request of a
3265 * batch, to compute the next-start time instant, soft_rt_next_start, such
3266 * that, if the next request of the application does not arrive before
3267 * soft_rt_next_start, then the above requirement on the bandwidth is met.
3268 *
3269 * The second requirement is that the request pattern of the application is
3270 * isochronous, i.e., that, after issuing a request or a batch of requests,
3271 * the application stops issuing new requests until all its pending requests
3272 * have been completed. After that, the application may issue a new batch,
3273 * and so on.
3274 * For this reason the next function is invoked to compute
3275 * soft_rt_next_start only for applications that meet this requirement,
3276 * whereas soft_rt_next_start is set to infinity for applications that do
3277 * not.
3278 *
Paolo Valentea34b0242017-12-15 07:23:12 +01003279 * Unfortunately, even a greedy (i.e., I/O-bound) application may
3280 * happen to meet, occasionally or systematically, both the above
3281 * bandwidth and isochrony requirements. This may happen at least in
3282 * the following circumstances. First, if the CPU load is high. The
3283 * application may stop issuing requests while the CPUs are busy
3284 * serving other processes, then restart, then stop again for a while,
3285 * and so on. The other circumstances are related to the storage
3286 * device: the storage device is highly loaded or reaches a low-enough
3287 * throughput with the I/O of the application (e.g., because the I/O
3288 * is random and/or the device is slow). In all these cases, the
3289 * I/O of the application may be simply slowed down enough to meet
3290 * the bandwidth and isochrony requirements. To reduce the probability
3291 * that greedy applications are deemed as soft real-time in these
3292 * corner cases, a further rule is used in the computation of
3293 * soft_rt_next_start: the return value of this function is forced to
3294 * be higher than the maximum between the following two quantities.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003295 *
Paolo Valentea34b0242017-12-15 07:23:12 +01003296 * (a) Current time plus: (1) the maximum time for which the arrival
3297 * of a request is waited for when a sync queue becomes idle,
3298 * namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
3299 * postpone for a moment the reason for adding a few extra
3300 * jiffies; we get back to it after next item (b). Lower-bounding
3301 * the return value of this function with the current time plus
3302 * bfqd->bfq_slice_idle tends to filter out greedy applications,
3303 * because the latter issue their next request as soon as possible
3304 * after the last one has been completed. In contrast, a soft
3305 * real-time application spends some time processing data, after a
3306 * batch of its requests has been completed.
3307 *
3308 * (b) Current value of bfqq->soft_rt_next_start. As pointed out
3309 * above, greedy applications may happen to meet both the
3310 * bandwidth and isochrony requirements under heavy CPU or
3311 * storage-device load. In more detail, in these scenarios, these
3312 * applications happen, only for limited time periods, to do I/O
3313 * slowly enough to meet all the requirements described so far,
3314 * including the filtering in above item (a). These slow-speed
3315 * time intervals are usually interspersed between other time
3316 * intervals during which these applications do I/O at a very high
3317 * speed. Fortunately, exactly because of the high speed of the
3318 * I/O in the high-speed intervals, the values returned by this
3319 * function happen to be so high, near the end of any such
3320 * high-speed interval, to be likely to fall *after* the end of
3321 * the low-speed time interval that follows. These high values are
3322 * stored in bfqq->soft_rt_next_start after each invocation of
3323 * this function. As a consequence, if the last value of
3324 * bfqq->soft_rt_next_start is constantly used to lower-bound the
3325 * next value that this function may return, then, from the very
3326 * beginning of a low-speed interval, bfqq->soft_rt_next_start is
3327 * likely to be constantly kept so high that any I/O request
3328 * issued during the low-speed interval is considered as arriving
3329 * to soon for the application to be deemed as soft
3330 * real-time. Then, in the high-speed interval that follows, the
3331 * application will not be deemed as soft real-time, just because
3332 * it will do I/O at a high speed. And so on.
3333 *
3334 * Getting back to the filtering in item (a), in the following two
3335 * cases this filtering might be easily passed by a greedy
3336 * application, if the reference quantity was just
3337 * bfqd->bfq_slice_idle:
3338 * 1) HZ is so low that the duration of a jiffy is comparable to or
3339 * higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
3340 * devices with HZ=100. The time granularity may be so coarse
3341 * that the approximation, in jiffies, of bfqd->bfq_slice_idle
3342 * is rather lower than the exact value.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003343 * 2) jiffies, instead of increasing at a constant rate, may stop increasing
3344 * for a while, then suddenly 'jump' by several units to recover the lost
3345 * increments. This seems to happen, e.g., inside virtual machines.
Paolo Valentea34b0242017-12-15 07:23:12 +01003346 * To address this issue, in the filtering in (a) we do not use as a
3347 * reference time interval just bfqd->bfq_slice_idle, but
3348 * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
3349 * minimum number of jiffies for which the filter seems to be quite
3350 * precise also in embedded systems and KVM/QEMU virtual machines.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003351 */
3352static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
3353 struct bfq_queue *bfqq)
3354{
Paolo Valentea34b0242017-12-15 07:23:12 +01003355 return max3(bfqq->soft_rt_next_start,
3356 bfqq->last_idle_bklogged +
3357 HZ * bfqq->service_from_backlogged /
3358 bfqd->bfq_wr_max_softrt_rate,
3359 jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4);
Paolo Valente77b7dce2017-04-12 18:23:13 +02003360}
3361
Paolo Valenteaee69d72017-04-19 08:29:02 -06003362/**
3363 * bfq_bfqq_expire - expire a queue.
3364 * @bfqd: device owning the queue.
3365 * @bfqq: the queue to expire.
3366 * @compensate: if true, compensate for the time spent idling.
3367 * @reason: the reason causing the expiration.
3368 *
Paolo Valentec074170e2017-04-12 18:23:11 +02003369 * If the process associated with bfqq does slow I/O (e.g., because it
3370 * issues random requests), we charge bfqq with the time it has been
3371 * in service instead of the service it has received (see
3372 * bfq_bfqq_charge_time for details on how this goal is achieved). As
3373 * a consequence, bfqq will typically get higher timestamps upon
3374 * reactivation, and hence it will be rescheduled as if it had
3375 * received more service than what it has actually received. In the
3376 * end, bfqq receives less service in proportion to how slowly its
3377 * associated process consumes its budgets (and hence how seriously it
3378 * tends to lower the throughput). In addition, this time-charging
3379 * strategy guarantees time fairness among slow processes. In
3380 * contrast, if the process associated with bfqq is not slow, we
3381 * charge bfqq exactly with the service it has received.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003382 *
Paolo Valentec074170e2017-04-12 18:23:11 +02003383 * Charging time to the first type of queues and the exact service to
3384 * the other has the effect of using the WF2Q+ policy to schedule the
3385 * former on a timeslice basis, without violating service domain
3386 * guarantees among the latter.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003387 */
Paolo Valenteea25da42017-04-19 08:48:24 -06003388void bfq_bfqq_expire(struct bfq_data *bfqd,
3389 struct bfq_queue *bfqq,
3390 bool compensate,
3391 enum bfqq_expiration reason)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003392{
3393 bool slow;
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003394 unsigned long delta = 0;
3395 struct bfq_entity *entity = &bfqq->entity;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003396 int ref;
3397
3398 /*
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003399 * Check whether the process is slow (see bfq_bfqq_is_slow).
Paolo Valenteaee69d72017-04-19 08:29:02 -06003400 */
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003401 slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003402
3403 /*
Paolo Valentec074170e2017-04-12 18:23:11 +02003404 * As above explained, charge slow (typically seeky) and
3405 * timed-out queues with the time and not the service
3406 * received, to favor sequential workloads.
3407 *
3408 * Processes doing I/O in the slower disk zones will tend to
3409 * be slow(er) even if not seeky. Therefore, since the
3410 * estimated peak rate is actually an average over the disk
3411 * surface, these processes may timeout just for bad luck. To
3412 * avoid punishing them, do not charge time to processes that
3413 * succeeded in consuming at least 2/3 of their budget. This
3414 * allows BFQ to preserve enough elasticity to still perform
3415 * bandwidth, and not time, distribution with little unlucky
3416 * or quasi-sequential processes.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003417 */
Paolo Valente44e44a12017-04-12 18:23:12 +02003418 if (bfqq->wr_coeff == 1 &&
3419 (slow ||
3420 (reason == BFQQE_BUDGET_TIMEOUT &&
3421 bfq_bfqq_budget_left(bfqq) >= entity->budget / 3)))
Paolo Valentec074170e2017-04-12 18:23:11 +02003422 bfq_bfqq_charge_time(bfqd, bfqq, delta);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003423
3424 if (reason == BFQQE_TOO_IDLE &&
Paolo Valenteab0e43e2017-04-12 18:23:10 +02003425 entity->service <= 2 * entity->budget / 10)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003426 bfq_clear_bfqq_IO_bound(bfqq);
3427
Paolo Valente44e44a12017-04-12 18:23:12 +02003428 if (bfqd->low_latency && bfqq->wr_coeff == 1)
3429 bfqq->last_wr_start_finish = jiffies;
3430
Paolo Valente77b7dce2017-04-12 18:23:13 +02003431 if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
3432 RB_EMPTY_ROOT(&bfqq->sort_list)) {
3433 /*
3434 * If we get here, and there are no outstanding
3435 * requests, then the request pattern is isochronous
3436 * (see the comments on the function
3437 * bfq_bfqq_softrt_next_start()). Thus we can compute
Paolo Valente20cd3242019-01-29 12:06:25 +01003438 * soft_rt_next_start. And we do it, unless bfqq is in
3439 * interactive weight raising. We do not do it in the
3440 * latter subcase, for the following reason. bfqq may
3441 * be conveying the I/O needed to load a soft
3442 * real-time application. Such an application will
3443 * actually exhibit a soft real-time I/O pattern after
3444 * it finally starts doing its job. But, if
3445 * soft_rt_next_start is computed here for an
3446 * interactive bfqq, and bfqq had received a lot of
3447 * service before remaining with no outstanding
3448 * request (likely to happen on a fast device), then
3449 * soft_rt_next_start would be assigned such a high
3450 * value that, for a very long time, bfqq would be
3451 * prevented from being possibly considered as soft
3452 * real time.
3453 *
3454 * If, instead, the queue still has outstanding
3455 * requests, then we have to wait for the completion
3456 * of all the outstanding requests to discover whether
3457 * the request pattern is actually isochronous.
Paolo Valente77b7dce2017-04-12 18:23:13 +02003458 */
Paolo Valente20cd3242019-01-29 12:06:25 +01003459 if (bfqq->dispatched == 0 &&
3460 bfqq->wr_coeff != bfqd->bfq_wr_coeff)
Paolo Valente77b7dce2017-04-12 18:23:13 +02003461 bfqq->soft_rt_next_start =
3462 bfq_bfqq_softrt_next_start(bfqd, bfqq);
Paolo Valente20cd3242019-01-29 12:06:25 +01003463 else if (bfqq->dispatched > 0) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02003464 /*
Paolo Valente77b7dce2017-04-12 18:23:13 +02003465 * Schedule an update of soft_rt_next_start to when
3466 * the task may be discovered to be isochronous.
3467 */
3468 bfq_mark_bfqq_softrt_update(bfqq);
3469 }
3470 }
3471
Paolo Valenteaee69d72017-04-19 08:29:02 -06003472 bfq_log_bfqq(bfqd, bfqq,
Paolo Valented5be3fe2017-08-04 07:35:10 +02003473 "expire (%d, slow %d, num_disp %d, short_ttime %d)", reason,
3474 slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06003475
3476 /*
Paolo Valente2341d6622019-03-12 09:59:29 +01003477 * bfqq expired, so no total service time needs to be computed
3478 * any longer: reset state machine for measuring total service
3479 * times.
3480 */
3481 bfqd->rqs_injected = bfqd->wait_dispatch = false;
3482 bfqd->waited_rq = NULL;
3483
3484 /*
Paolo Valenteaee69d72017-04-19 08:29:02 -06003485 * Increase, decrease or leave budget unchanged according to
3486 * reason.
3487 */
3488 __bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
3489 ref = bfqq->ref;
3490 __bfq_bfqq_expire(bfqd, bfqq);
3491
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003492 if (ref == 1) /* bfqq is gone, no more actions on it */
3493 return;
3494
Paolo Valenteaee69d72017-04-19 08:29:02 -06003495 /* mark bfqq as waiting a request only if a bic still points to it */
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003496 if (!bfq_bfqq_busy(bfqq) &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06003497 reason != BFQQE_BUDGET_TIMEOUT &&
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003498 reason != BFQQE_BUDGET_EXHAUSTED) {
Paolo Valenteaee69d72017-04-19 08:29:02 -06003499 bfq_mark_bfqq_non_blocking_wait_rq(bfqq);
Paolo Valente9fae8dd2018-06-25 21:55:36 +02003500 /*
3501 * Not setting service to 0, because, if the next rq
3502 * arrives in time, the queue will go on receiving
3503 * service with this same budget (as if it never expired)
3504 */
3505 } else
3506 entity->service = 0;
Paolo Valente8a511ba2018-08-16 18:51:15 +02003507
3508 /*
3509 * Reset the received-service counter for every parent entity.
3510 * Differently from what happens with bfqq->entity.service,
3511 * the resetting of this counter never needs to be postponed
3512 * for parent entities. In fact, in case bfqq may have a
3513 * chance to go on being served using the last, partially
3514 * consumed budget, bfqq->entity.service needs to be kept,
3515 * because if bfqq then actually goes on being served using
3516 * the same budget, the last value of bfqq->entity.service is
3517 * needed to properly decrement bfqq->entity.budget by the
3518 * portion already consumed. In contrast, it is not necessary
3519 * to keep entity->service for parent entities too, because
3520 * the bubble up of the new value of bfqq->entity.budget will
3521 * make sure that the budgets of parent entities are correct,
3522 * even in case bfqq and thus parent entities go on receiving
3523 * service with the same budget.
3524 */
3525 entity = entity->parent;
3526 for_each_entity(entity)
3527 entity->service = 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003528}
3529
3530/*
3531 * Budget timeout is not implemented through a dedicated timer, but
3532 * just checked on request arrivals and completions, as well as on
3533 * idle timer expirations.
3534 */
3535static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
3536{
Paolo Valente44e44a12017-04-12 18:23:12 +02003537 return time_is_before_eq_jiffies(bfqq->budget_timeout);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003538}
3539
3540/*
3541 * If we expire a queue that is actively waiting (i.e., with the
3542 * device idled) for the arrival of a new request, then we may incur
3543 * the timestamp misalignment problem described in the body of the
3544 * function __bfq_activate_entity. Hence we return true only if this
3545 * condition does not hold, or if the queue is slow enough to deserve
3546 * only to be kicked off for preserving a high throughput.
3547 */
3548static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
3549{
3550 bfq_log_bfqq(bfqq->bfqd, bfqq,
3551 "may_budget_timeout: wait_request %d left %d timeout %d",
3552 bfq_bfqq_wait_request(bfqq),
3553 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3,
3554 bfq_bfqq_budget_timeout(bfqq));
3555
3556 return (!bfq_bfqq_wait_request(bfqq) ||
3557 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3)
3558 &&
3559 bfq_bfqq_budget_timeout(bfqq);
3560}
3561
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003562static bool idling_boosts_thr_without_issues(struct bfq_data *bfqd,
3563 struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06003564{
Paolo Valenteedaf9422017-08-04 07:35:11 +02003565 bool rot_without_queueing =
3566 !blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag,
3567 bfqq_sequential_and_IO_bound,
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003568 idling_boosts_thr;
Paolo Valented5be3fe2017-08-04 07:35:10 +02003569
Paolo Valenteedaf9422017-08-04 07:35:11 +02003570 bfqq_sequential_and_IO_bound = !BFQQ_SEEKY(bfqq) &&
3571 bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_has_short_ttime(bfqq);
3572
Paolo Valented5be3fe2017-08-04 07:35:10 +02003573 /*
Paolo Valente44e44a12017-04-12 18:23:12 +02003574 * The next variable takes into account the cases where idling
3575 * boosts the throughput.
3576 *
Paolo Valentee01eff02017-04-12 18:23:19 +02003577 * The value of the variable is computed considering, first, that
3578 * idling is virtually always beneficial for the throughput if:
Paolo Valenteedaf9422017-08-04 07:35:11 +02003579 * (a) the device is not NCQ-capable and rotational, or
3580 * (b) regardless of the presence of NCQ, the device is rotational and
3581 * the request pattern for bfqq is I/O-bound and sequential, or
3582 * (c) regardless of whether it is rotational, the device is
3583 * not NCQ-capable and the request pattern for bfqq is
3584 * I/O-bound and sequential.
Paolo Valentebf2b79e2017-04-12 18:23:18 +02003585 *
3586 * Secondly, and in contrast to the above item (b), idling an
3587 * NCQ-capable flash-based device would not boost the
Paolo Valentee01eff02017-04-12 18:23:19 +02003588 * throughput even with sequential I/O; rather it would lower
Paolo Valentebf2b79e2017-04-12 18:23:18 +02003589 * the throughput in proportion to how fast the device
3590 * is. Accordingly, the next variable is true if any of the
Paolo Valenteedaf9422017-08-04 07:35:11 +02003591 * above conditions (a), (b) or (c) is true, and, in
3592 * particular, happens to be false if bfqd is an NCQ-capable
3593 * flash-based device.
Paolo Valenteaee69d72017-04-19 08:29:02 -06003594 */
Paolo Valenteedaf9422017-08-04 07:35:11 +02003595 idling_boosts_thr = rot_without_queueing ||
3596 ((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) &&
3597 bfqq_sequential_and_IO_bound);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003598
3599 /*
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003600 * The return value of this function is equal to that of
Paolo Valentecfd69712017-04-12 18:23:15 +02003601 * idling_boosts_thr, unless a special case holds. In this
3602 * special case, described below, idling may cause problems to
3603 * weight-raised queues.
3604 *
3605 * When the request pool is saturated (e.g., in the presence
3606 * of write hogs), if the processes associated with
3607 * non-weight-raised queues ask for requests at a lower rate,
3608 * then processes associated with weight-raised queues have a
3609 * higher probability to get a request from the pool
3610 * immediately (or at least soon) when they need one. Thus
3611 * they have a higher probability to actually get a fraction
3612 * of the device throughput proportional to their high
3613 * weight. This is especially true with NCQ-capable drives,
3614 * which enqueue several requests in advance, and further
3615 * reorder internally-queued requests.
3616 *
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003617 * For this reason, we force to false the return value if
3618 * there are weight-raised busy queues. In this case, and if
3619 * bfqq is not weight-raised, this guarantees that the device
3620 * is not idled for bfqq (if, instead, bfqq is weight-raised,
3621 * then idling will be guaranteed by another variable, see
3622 * below). Combined with the timestamping rules of BFQ (see
3623 * [1] for details), this behavior causes bfqq, and hence any
3624 * sync non-weight-raised queue, to get a lower number of
3625 * requests served, and thus to ask for a lower number of
3626 * requests from the request pool, before the busy
3627 * weight-raised queues get served again. This often mitigates
3628 * starvation problems in the presence of heavy write
3629 * workloads and NCQ, thereby guaranteeing a higher
3630 * application and system responsiveness in these hostile
3631 * scenarios.
Paolo Valentecfd69712017-04-12 18:23:15 +02003632 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003633 return idling_boosts_thr &&
Paolo Valentecfd69712017-04-12 18:23:15 +02003634 bfqd->wr_busy_queues == 0;
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003635}
Paolo Valentecfd69712017-04-12 18:23:15 +02003636
Paolo Valente530c4cb2019-01-29 12:06:32 +01003637/*
Paolo Valentefb53ac62019-03-12 09:59:28 +01003638 * There is a case where idling does not have to be performed for
3639 * throughput concerns, but to preserve the throughput share of
3640 * the process associated with bfqq.
Paolo Valente530c4cb2019-01-29 12:06:32 +01003641 *
3642 * To introduce this case, we can note that allowing the drive
3643 * to enqueue more than one request at a time, and hence
3644 * delegating de facto final scheduling decisions to the
3645 * drive's internal scheduler, entails loss of control on the
3646 * actual request service order. In particular, the critical
3647 * situation is when requests from different processes happen
3648 * to be present, at the same time, in the internal queue(s)
3649 * of the drive. In such a situation, the drive, by deciding
3650 * the service order of the internally-queued requests, does
3651 * determine also the actual throughput distribution among
3652 * these processes. But the drive typically has no notion or
3653 * concern about per-process throughput distribution, and
3654 * makes its decisions only on a per-request basis. Therefore,
3655 * the service distribution enforced by the drive's internal
Paolo Valentefb53ac62019-03-12 09:59:28 +01003656 * scheduler is likely to coincide with the desired throughput
3657 * distribution only in a completely symmetric, or favorably
3658 * skewed scenario where:
3659 * (i-a) each of these processes must get the same throughput as
3660 * the others,
3661 * (i-b) in case (i-a) does not hold, it holds that the process
3662 * associated with bfqq must receive a lower or equal
3663 * throughput than any of the other processes;
3664 * (ii) the I/O of each process has the same properties, in
3665 * terms of locality (sequential or random), direction
3666 * (reads or writes), request sizes, greediness
3667 * (from I/O-bound to sporadic), and so on;
3668
3669 * In fact, in such a scenario, the drive tends to treat the requests
3670 * of each process in about the same way as the requests of the
3671 * others, and thus to provide each of these processes with about the
3672 * same throughput. This is exactly the desired throughput
3673 * distribution if (i-a) holds, or, if (i-b) holds instead, this is an
3674 * even more convenient distribution for (the process associated with)
3675 * bfqq.
Paolo Valente530c4cb2019-01-29 12:06:32 +01003676 *
Paolo Valentefb53ac62019-03-12 09:59:28 +01003677 * In contrast, in any asymmetric or unfavorable scenario, device
3678 * idling (I/O-dispatch plugging) is certainly needed to guarantee
3679 * that bfqq receives its assigned fraction of the device throughput
3680 * (see [1] for details).
3681 *
3682 * The problem is that idling may significantly reduce throughput with
3683 * certain combinations of types of I/O and devices. An important
3684 * example is sync random I/O on flash storage with command
3685 * queueing. So, unless bfqq falls in cases where idling also boosts
3686 * throughput, it is important to check conditions (i-a), i(-b) and
3687 * (ii) accurately, so as to avoid idling when not strictly needed for
3688 * service guarantees.
3689 *
3690 * Unfortunately, it is extremely difficult to thoroughly check
3691 * condition (ii). And, in case there are active groups, it becomes
3692 * very difficult to check conditions (i-a) and (i-b) too. In fact,
3693 * if there are active groups, then, for conditions (i-a) or (i-b) to
3694 * become false 'indirectly', it is enough that an active group
3695 * contains more active processes or sub-groups than some other active
3696 * group. More precisely, for conditions (i-a) or (i-b) to become
3697 * false because of such a group, it is not even necessary that the
3698 * group is (still) active: it is sufficient that, even if the group
3699 * has become inactive, some of its descendant processes still have
3700 * some request already dispatched but still waiting for
3701 * completion. In fact, requests have still to be guaranteed their
3702 * share of the throughput even after being dispatched. In this
3703 * respect, it is easy to show that, if a group frequently becomes
3704 * inactive while still having in-flight requests, and if, when this
3705 * happens, the group is not considered in the calculation of whether
3706 * the scenario is asymmetric, then the group may fail to be
3707 * guaranteed its fair share of the throughput (basically because
3708 * idling may not be performed for the descendant processes of the
3709 * group, but it had to be). We address this issue with the following
3710 * bi-modal behavior, implemented in the function
3711 * bfq_asymmetric_scenario().
Paolo Valente530c4cb2019-01-29 12:06:32 +01003712 *
3713 * If there are groups with requests waiting for completion
3714 * (as commented above, some of these groups may even be
3715 * already inactive), then the scenario is tagged as
3716 * asymmetric, conservatively, without checking any of the
Paolo Valentefb53ac62019-03-12 09:59:28 +01003717 * conditions (i-a), (i-b) or (ii). So the device is idled for bfqq.
Paolo Valente530c4cb2019-01-29 12:06:32 +01003718 * This behavior matches also the fact that groups are created
3719 * exactly if controlling I/O is a primary concern (to
3720 * preserve bandwidth and latency guarantees).
3721 *
Paolo Valentefb53ac62019-03-12 09:59:28 +01003722 * On the opposite end, if there are no groups with requests waiting
3723 * for completion, then only conditions (i-a) and (i-b) are actually
3724 * controlled, i.e., provided that conditions (i-a) or (i-b) holds,
3725 * idling is not performed, regardless of whether condition (ii)
3726 * holds. In other words, only if conditions (i-a) and (i-b) do not
3727 * hold, then idling is allowed, and the device tends to be prevented
3728 * from queueing many requests, possibly of several processes. Since
3729 * there are no groups with requests waiting for completion, then, to
3730 * control conditions (i-a) and (i-b) it is enough to check just
3731 * whether all the queues with requests waiting for completion also
3732 * have the same weight.
Paolo Valente530c4cb2019-01-29 12:06:32 +01003733 *
3734 * Not checking condition (ii) evidently exposes bfqq to the
3735 * risk of getting less throughput than its fair share.
3736 * However, for queues with the same weight, a further
3737 * mechanism, preemption, mitigates or even eliminates this
3738 * problem. And it does so without consequences on overall
3739 * throughput. This mechanism and its benefits are explained
3740 * in the next three paragraphs.
3741 *
3742 * Even if a queue, say Q, is expired when it remains idle, Q
3743 * can still preempt the new in-service queue if the next
3744 * request of Q arrives soon (see the comments on
3745 * bfq_bfqq_update_budg_for_activation). If all queues and
3746 * groups have the same weight, this form of preemption,
3747 * combined with the hole-recovery heuristic described in the
3748 * comments on function bfq_bfqq_update_budg_for_activation,
3749 * are enough to preserve a correct bandwidth distribution in
3750 * the mid term, even without idling. In fact, even if not
3751 * idling allows the internal queues of the device to contain
3752 * many requests, and thus to reorder requests, we can rather
3753 * safely assume that the internal scheduler still preserves a
3754 * minimum of mid-term fairness.
3755 *
3756 * More precisely, this preemption-based, idleless approach
3757 * provides fairness in terms of IOPS, and not sectors per
3758 * second. This can be seen with a simple example. Suppose
3759 * that there are two queues with the same weight, but that
3760 * the first queue receives requests of 8 sectors, while the
3761 * second queue receives requests of 1024 sectors. In
3762 * addition, suppose that each of the two queues contains at
3763 * most one request at a time, which implies that each queue
3764 * always remains idle after it is served. Finally, after
3765 * remaining idle, each queue receives very quickly a new
3766 * request. It follows that the two queues are served
3767 * alternatively, preempting each other if needed. This
3768 * implies that, although both queues have the same weight,
3769 * the queue with large requests receives a service that is
3770 * 1024/8 times as high as the service received by the other
3771 * queue.
3772 *
3773 * The motivation for using preemption instead of idling (for
3774 * queues with the same weight) is that, by not idling,
3775 * service guarantees are preserved (completely or at least in
3776 * part) without minimally sacrificing throughput. And, if
3777 * there is no active group, then the primary expectation for
3778 * this device is probably a high throughput.
3779 *
3780 * We are now left only with explaining the additional
3781 * compound condition that is checked below for deciding
3782 * whether the scenario is asymmetric. To explain this
3783 * compound condition, we need to add that the function
Paolo Valentefb53ac62019-03-12 09:59:28 +01003784 * bfq_asymmetric_scenario checks the weights of only
Paolo Valente530c4cb2019-01-29 12:06:32 +01003785 * non-weight-raised queues, for efficiency reasons (see
3786 * comments on bfq_weights_tree_add()). Then the fact that
3787 * bfqq is weight-raised is checked explicitly here. More
3788 * precisely, the compound condition below takes into account
3789 * also the fact that, even if bfqq is being weight-raised,
3790 * the scenario is still symmetric if all queues with requests
3791 * waiting for completion happen to be
3792 * weight-raised. Actually, we should be even more precise
3793 * here, and differentiate between interactive weight raising
3794 * and soft real-time weight raising.
3795 *
3796 * As a side note, it is worth considering that the above
3797 * device-idling countermeasures may however fail in the
3798 * following unlucky scenario: if idling is (correctly)
3799 * disabled in a time period during which all symmetry
3800 * sub-conditions hold, and hence the device is allowed to
3801 * enqueue many requests, but at some later point in time some
3802 * sub-condition stops to hold, then it may become impossible
3803 * to let requests be served in the desired order until all
3804 * the requests already queued in the device have been served.
3805 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003806static bool idling_needed_for_service_guarantees(struct bfq_data *bfqd,
3807 struct bfq_queue *bfqq)
3808{
Paolo Valente530c4cb2019-01-29 12:06:32 +01003809 return (bfqq->wr_coeff > 1 &&
3810 bfqd->wr_busy_queues <
3811 bfq_tot_busy_queues(bfqd)) ||
Paolo Valentefb53ac62019-03-12 09:59:28 +01003812 bfq_asymmetric_scenario(bfqd, bfqq);
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003813}
3814
3815/*
3816 * For a queue that becomes empty, device idling is allowed only if
3817 * this function returns true for that queue. As a consequence, since
3818 * device idling plays a critical role for both throughput boosting
3819 * and service guarantees, the return value of this function plays a
3820 * critical role as well.
3821 *
3822 * In a nutshell, this function returns true only if idling is
3823 * beneficial for throughput or, even if detrimental for throughput,
3824 * idling is however necessary to preserve service guarantees (low
3825 * latency, desired throughput distribution, ...). In particular, on
3826 * NCQ-capable devices, this function tries to return false, so as to
3827 * help keep the drives' internal queues full, whenever this helps the
3828 * device boost the throughput without causing any service-guarantee
3829 * issue.
3830 *
3831 * Most of the issues taken into account to get the return value of
3832 * this function are not trivial. We discuss these issues in the two
3833 * functions providing the main pieces of information needed by this
3834 * function.
3835 */
3836static bool bfq_better_to_idle(struct bfq_queue *bfqq)
3837{
3838 struct bfq_data *bfqd = bfqq->bfqd;
3839 bool idling_boosts_thr_with_no_issue, idling_needed_for_service_guar;
3840
3841 if (unlikely(bfqd->strict_guarantees))
3842 return true;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02003843
3844 /*
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003845 * Idling is performed only if slice_idle > 0. In addition, we
3846 * do not idle if
3847 * (a) bfqq is async
3848 * (b) bfqq is in the idle io prio class: in this case we do
3849 * not idle because we want to minimize the bandwidth that
3850 * queues in this class can steal to higher-priority queues
3851 */
3852 if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) ||
3853 bfq_class_idle(bfqq))
3854 return false;
3855
3856 idling_boosts_thr_with_no_issue =
3857 idling_boosts_thr_without_issues(bfqd, bfqq);
3858
3859 idling_needed_for_service_guar =
3860 idling_needed_for_service_guarantees(bfqd, bfqq);
3861
3862 /*
3863 * We have now the two components we need to compute the
Paolo Valented5be3fe2017-08-04 07:35:10 +02003864 * return value of the function, which is true only if idling
3865 * either boosts the throughput (without issues), or is
3866 * necessary to preserve service guarantees.
Paolo Valente44e44a12017-04-12 18:23:12 +02003867 */
Paolo Valente05c2f5c2019-01-29 12:06:30 +01003868 return idling_boosts_thr_with_no_issue ||
3869 idling_needed_for_service_guar;
Paolo Valenteaee69d72017-04-19 08:29:02 -06003870}
3871
3872/*
Paolo Valente277a4a92018-06-25 21:55:37 +02003873 * If the in-service queue is empty but the function bfq_better_to_idle
Paolo Valenteaee69d72017-04-19 08:29:02 -06003874 * returns true, then:
3875 * 1) the queue must remain in service and cannot be expired, and
3876 * 2) the device must be idled to wait for the possible arrival of a new
3877 * request for the queue.
Paolo Valente277a4a92018-06-25 21:55:37 +02003878 * See the comments on the function bfq_better_to_idle for the reasons
Paolo Valenteaee69d72017-04-19 08:29:02 -06003879 * why performing device idling is the best choice to boost the throughput
Paolo Valente277a4a92018-06-25 21:55:37 +02003880 * and preserve service guarantees when bfq_better_to_idle itself
Paolo Valenteaee69d72017-04-19 08:29:02 -06003881 * returns true.
3882 */
3883static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
3884{
Paolo Valente277a4a92018-06-25 21:55:37 +02003885 return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06003886}
3887
Paolo Valente2341d6622019-03-12 09:59:29 +01003888/*
3889 * This function chooses the queue from which to pick the next extra
3890 * I/O request to inject, if it finds a compatible queue. See the
3891 * comments on bfq_update_inject_limit() for details on the injection
3892 * mechanism, and for the definitions of the quantities mentioned
3893 * below.
3894 */
3895static struct bfq_queue *
3896bfq_choose_bfqq_for_injection(struct bfq_data *bfqd)
Paolo Valented0edc242018-09-14 16:23:08 +02003897{
Paolo Valente2341d6622019-03-12 09:59:29 +01003898 struct bfq_queue *bfqq, *in_serv_bfqq = bfqd->in_service_queue;
3899 unsigned int limit = in_serv_bfqq->inject_limit;
3900 /*
3901 * If
3902 * - bfqq is not weight-raised and therefore does not carry
3903 * time-critical I/O,
3904 * or
3905 * - regardless of whether bfqq is weight-raised, bfqq has
3906 * however a long think time, during which it can absorb the
3907 * effect of an appropriate number of extra I/O requests
3908 * from other queues (see bfq_update_inject_limit for
3909 * details on the computation of this number);
3910 * then injection can be performed without restrictions.
3911 */
3912 bool in_serv_always_inject = in_serv_bfqq->wr_coeff == 1 ||
3913 !bfq_bfqq_has_short_ttime(in_serv_bfqq);
Paolo Valented0edc242018-09-14 16:23:08 +02003914
3915 /*
Paolo Valente2341d6622019-03-12 09:59:29 +01003916 * If
3917 * - the baseline total service time could not be sampled yet,
3918 * so the inject limit happens to be still 0, and
3919 * - a lot of time has elapsed since the plugging of I/O
3920 * dispatching started, so drive speed is being wasted
3921 * significantly;
3922 * then temporarily raise inject limit to one request.
3923 */
3924 if (limit == 0 && in_serv_bfqq->last_serv_time_ns == 0 &&
3925 bfq_bfqq_wait_request(in_serv_bfqq) &&
3926 time_is_before_eq_jiffies(bfqd->last_idling_start_jiffies +
3927 bfqd->bfq_slice_idle)
3928 )
3929 limit = 1;
3930
3931 if (bfqd->rq_in_driver >= limit)
3932 return NULL;
3933
3934 /*
3935 * Linear search of the source queue for injection; but, with
3936 * a high probability, very few steps are needed to find a
3937 * candidate queue, i.e., a queue with enough budget left for
3938 * its next request. In fact:
Paolo Valented0edc242018-09-14 16:23:08 +02003939 * - BFQ dynamically updates the budget of every queue so as
3940 * to accommodate the expected backlog of the queue;
3941 * - if a queue gets all its requests dispatched as injected
3942 * service, then the queue is removed from the active list
Paolo Valente2341d6622019-03-12 09:59:29 +01003943 * (and re-added only if it gets new requests, but then it
3944 * is assigned again enough budget for its new backlog).
Paolo Valented0edc242018-09-14 16:23:08 +02003945 */
3946 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
3947 if (!RB_EMPTY_ROOT(&bfqq->sort_list) &&
Paolo Valente2341d6622019-03-12 09:59:29 +01003948 (in_serv_always_inject || bfqq->wr_coeff > 1) &&
Paolo Valented0edc242018-09-14 16:23:08 +02003949 bfq_serv_to_charge(bfqq->next_rq, bfqq) <=
Paolo Valente2341d6622019-03-12 09:59:29 +01003950 bfq_bfqq_budget_left(bfqq)) {
3951 /*
3952 * Allow for only one large in-flight request
3953 * on non-rotational devices, for the
3954 * following reason. On non-rotationl drives,
3955 * large requests take much longer than
3956 * smaller requests to be served. In addition,
3957 * the drive prefers to serve large requests
3958 * w.r.t. to small ones, if it can choose. So,
3959 * having more than one large requests queued
3960 * in the drive may easily make the next first
3961 * request of the in-service queue wait for so
3962 * long to break bfqq's service guarantees. On
3963 * the bright side, large requests let the
3964 * drive reach a very high throughput, even if
3965 * there is only one in-flight large request
3966 * at a time.
3967 */
3968 if (blk_queue_nonrot(bfqd->queue) &&
3969 blk_rq_sectors(bfqq->next_rq) >=
3970 BFQQ_SECT_THR_NONROT)
3971 limit = min_t(unsigned int, 1, limit);
3972 else
3973 limit = in_serv_bfqq->inject_limit;
3974
3975 if (bfqd->rq_in_driver < limit) {
3976 bfqd->rqs_injected = true;
3977 return bfqq;
3978 }
3979 }
Paolo Valented0edc242018-09-14 16:23:08 +02003980
3981 return NULL;
3982}
3983
Paolo Valenteaee69d72017-04-19 08:29:02 -06003984/*
3985 * Select a queue for service. If we have a current queue in service,
3986 * check whether to continue servicing it, or retrieve and set a new one.
3987 */
3988static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
3989{
3990 struct bfq_queue *bfqq;
3991 struct request *next_rq;
3992 enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT;
3993
3994 bfqq = bfqd->in_service_queue;
3995 if (!bfqq)
3996 goto new_queue;
3997
3998 bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
3999
Paolo Valente4420b092018-06-25 21:55:35 +02004000 /*
4001 * Do not expire bfqq for budget timeout if bfqq may be about
4002 * to enjoy device idling. The reason why, in this case, we
4003 * prevent bfqq from expiring is the same as in the comments
4004 * on the case where bfq_bfqq_must_idle() returns true, in
4005 * bfq_completed_request().
4006 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004007 if (bfq_may_expire_for_budg_timeout(bfqq) &&
Paolo Valenteaee69d72017-04-19 08:29:02 -06004008 !bfq_bfqq_must_idle(bfqq))
4009 goto expire;
4010
4011check_queue:
4012 /*
4013 * This loop is rarely executed more than once. Even when it
4014 * happens, it is much more convenient to re-execute this loop
4015 * than to return NULL and trigger a new dispatch to get a
4016 * request served.
4017 */
4018 next_rq = bfqq->next_rq;
4019 /*
4020 * If bfqq has requests queued and it has enough budget left to
4021 * serve them, keep the queue, otherwise expire it.
4022 */
4023 if (next_rq) {
4024 if (bfq_serv_to_charge(next_rq, bfqq) >
4025 bfq_bfqq_budget_left(bfqq)) {
4026 /*
4027 * Expire the queue for budget exhaustion,
4028 * which makes sure that the next budget is
4029 * enough to serve the next request, even if
4030 * it comes from the fifo expired path.
4031 */
4032 reason = BFQQE_BUDGET_EXHAUSTED;
4033 goto expire;
4034 } else {
4035 /*
4036 * The idle timer may be pending because we may
4037 * not disable disk idling even when a new request
4038 * arrives.
4039 */
4040 if (bfq_bfqq_wait_request(bfqq)) {
4041 /*
4042 * If we get here: 1) at least a new request
4043 * has arrived but we have not disabled the
4044 * timer because the request was too small,
4045 * 2) then the block layer has unplugged
4046 * the device, causing the dispatch to be
4047 * invoked.
4048 *
4049 * Since the device is unplugged, now the
4050 * requests are probably large enough to
4051 * provide a reasonable throughput.
4052 * So we disable idling.
4053 */
4054 bfq_clear_bfqq_wait_request(bfqq);
4055 hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
4056 }
4057 goto keep_queue;
4058 }
4059 }
4060
4061 /*
4062 * No requests pending. However, if the in-service queue is idling
4063 * for a new request, or has requests waiting for a completion and
4064 * may idle after their completion, then keep it anyway.
Paolo Valented0edc242018-09-14 16:23:08 +02004065 *
Paolo Valente2341d6622019-03-12 09:59:29 +01004066 * Yet, inject service from other queues if it boosts
4067 * throughput and is possible.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004068 */
4069 if (bfq_bfqq_wait_request(bfqq) ||
Paolo Valente277a4a92018-06-25 21:55:37 +02004070 (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) {
Paolo Valente2341d6622019-03-12 09:59:29 +01004071 struct bfq_queue *async_bfqq =
4072 bfqq->bic && bfqq->bic->bfqq[0] &&
4073 bfq_bfqq_busy(bfqq->bic->bfqq[0]) ?
4074 bfqq->bic->bfqq[0] : NULL;
4075
4076 /*
4077 * If the process associated with bfqq has also async
4078 * I/O pending, then inject it
4079 * unconditionally. Injecting I/O from the same
4080 * process can cause no harm to the process. On the
4081 * contrary, it can only increase bandwidth and reduce
4082 * latency for the process.
4083 */
4084 if (async_bfqq &&
4085 icq_to_bic(async_bfqq->next_rq->elv.icq) == bfqq->bic &&
4086 bfq_serv_to_charge(async_bfqq->next_rq, async_bfqq) <=
4087 bfq_bfqq_budget_left(async_bfqq))
4088 bfqq = bfqq->bic->bfqq[0];
4089 else if (!idling_boosts_thr_without_issues(bfqd, bfqq) &&
4090 (bfqq->wr_coeff == 1 || bfqd->wr_busy_queues > 1 ||
4091 !bfq_bfqq_has_short_ttime(bfqq)))
Paolo Valented0edc242018-09-14 16:23:08 +02004092 bfqq = bfq_choose_bfqq_for_injection(bfqd);
4093 else
4094 bfqq = NULL;
4095
Paolo Valenteaee69d72017-04-19 08:29:02 -06004096 goto keep_queue;
4097 }
4098
4099 reason = BFQQE_NO_MORE_REQUESTS;
4100expire:
4101 bfq_bfqq_expire(bfqd, bfqq, false, reason);
4102new_queue:
4103 bfqq = bfq_set_in_service_queue(bfqd);
4104 if (bfqq) {
4105 bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue");
4106 goto check_queue;
4107 }
4108keep_queue:
4109 if (bfqq)
4110 bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue");
4111 else
4112 bfq_log(bfqd, "select_queue: no queue returned");
4113
4114 return bfqq;
4115}
4116
Paolo Valente44e44a12017-04-12 18:23:12 +02004117static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
4118{
4119 struct bfq_entity *entity = &bfqq->entity;
4120
4121 if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
4122 bfq_log_bfqq(bfqd, bfqq,
4123 "raising period dur %u/%u msec, old coeff %u, w %d(%d)",
4124 jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
4125 jiffies_to_msecs(bfqq->wr_cur_max_time),
4126 bfqq->wr_coeff,
4127 bfqq->entity.weight, bfqq->entity.orig_weight);
4128
4129 if (entity->prio_changed)
4130 bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
4131
4132 /*
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004133 * If the queue was activated in a burst, or too much
4134 * time has elapsed from the beginning of this
4135 * weight-raising period, then end weight raising.
Paolo Valente44e44a12017-04-12 18:23:12 +02004136 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004137 if (bfq_bfqq_in_large_burst(bfqq))
4138 bfq_bfqq_end_wr(bfqq);
4139 else if (time_is_before_jiffies(bfqq->last_wr_start_finish +
4140 bfqq->wr_cur_max_time)) {
Paolo Valente77b7dce2017-04-12 18:23:13 +02004141 if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time ||
4142 time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt +
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004143 bfq_wr_duration(bfqd)))
Paolo Valente77b7dce2017-04-12 18:23:13 +02004144 bfq_bfqq_end_wr(bfqq);
4145 else {
Paolo Valente3e2bdd62017-09-21 11:04:01 +02004146 switch_back_to_interactive_wr(bfqq, bfqd);
Paolo Valente77b7dce2017-04-12 18:23:13 +02004147 bfqq->entity.prio_changed = 1;
4148 }
Paolo Valente44e44a12017-04-12 18:23:12 +02004149 }
Paolo Valente8a8747d2018-01-13 12:05:18 +01004150 if (bfqq->wr_coeff > 1 &&
4151 bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time &&
4152 bfqq->service_from_wr > max_service_from_wr) {
4153 /* see comments on max_service_from_wr */
4154 bfq_bfqq_end_wr(bfqq);
4155 }
Paolo Valente44e44a12017-04-12 18:23:12 +02004156 }
Paolo Valente431b17f2017-07-03 10:00:10 +02004157 /*
4158 * To improve latency (for this or other queues), immediately
4159 * update weight both if it must be raised and if it must be
4160 * lowered. Since, entity may be on some active tree here, and
4161 * might have a pending change of its ioprio class, invoke
4162 * next function with the last parameter unset (see the
4163 * comments on the function).
4164 */
Paolo Valente44e44a12017-04-12 18:23:12 +02004165 if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
Paolo Valente431b17f2017-07-03 10:00:10 +02004166 __bfq_entity_update_weight_prio(bfq_entity_service_tree(entity),
4167 entity, false);
Paolo Valente44e44a12017-04-12 18:23:12 +02004168}
4169
Paolo Valenteaee69d72017-04-19 08:29:02 -06004170/*
4171 * Dispatch next request from bfqq.
4172 */
4173static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd,
4174 struct bfq_queue *bfqq)
4175{
4176 struct request *rq = bfqq->next_rq;
4177 unsigned long service_to_charge;
4178
4179 service_to_charge = bfq_serv_to_charge(rq, bfqq);
4180
4181 bfq_bfqq_served(bfqq, service_to_charge);
4182
Paolo Valente2341d6622019-03-12 09:59:29 +01004183 if (bfqq == bfqd->in_service_queue && bfqd->wait_dispatch) {
4184 bfqd->wait_dispatch = false;
4185 bfqd->waited_rq = rq;
4186 }
4187
Paolo Valenteaee69d72017-04-19 08:29:02 -06004188 bfq_dispatch_remove(bfqd->queue, rq);
4189
Paolo Valente2341d6622019-03-12 09:59:29 +01004190 if (bfqq != bfqd->in_service_queue)
Paolo Valented0edc242018-09-14 16:23:08 +02004191 goto return_rq;
Paolo Valented0edc242018-09-14 16:23:08 +02004192
Paolo Valente44e44a12017-04-12 18:23:12 +02004193 /*
4194 * If weight raising has to terminate for bfqq, then next
4195 * function causes an immediate update of bfqq's weight,
4196 * without waiting for next activation. As a consequence, on
4197 * expiration, bfqq will be timestamped as if has never been
4198 * weight-raised during this service slot, even if it has
4199 * received part or even most of the service as a
4200 * weight-raised queue. This inflates bfqq's timestamps, which
4201 * is beneficial, as bfqq is then more willing to leave the
4202 * device immediately to possible other weight-raised queues.
4203 */
4204 bfq_update_wr_data(bfqd, bfqq);
4205
Paolo Valenteaee69d72017-04-19 08:29:02 -06004206 /*
4207 * Expire bfqq, pretending that its budget expired, if bfqq
4208 * belongs to CLASS_IDLE and other queues are waiting for
4209 * service.
4210 */
Paolo Valente73d58112019-01-29 12:06:29 +01004211 if (!(bfq_tot_busy_queues(bfqd) > 1 && bfq_class_idle(bfqq)))
Paolo Valented0edc242018-09-14 16:23:08 +02004212 goto return_rq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004213
Paolo Valenteaee69d72017-04-19 08:29:02 -06004214 bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED);
Paolo Valented0edc242018-09-14 16:23:08 +02004215
4216return_rq:
Paolo Valenteaee69d72017-04-19 08:29:02 -06004217 return rq;
4218}
4219
4220static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
4221{
4222 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4223
4224 /*
4225 * Avoiding lock: a race on bfqd->busy_queues should cause at
4226 * most a call to dispatch for nothing
4227 */
4228 return !list_empty_careful(&bfqd->dispatch) ||
Paolo Valente73d58112019-01-29 12:06:29 +01004229 bfq_tot_busy_queues(bfqd) > 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004230}
4231
4232static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
4233{
4234 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4235 struct request *rq = NULL;
4236 struct bfq_queue *bfqq = NULL;
4237
4238 if (!list_empty(&bfqd->dispatch)) {
4239 rq = list_first_entry(&bfqd->dispatch, struct request,
4240 queuelist);
4241 list_del_init(&rq->queuelist);
4242
4243 bfqq = RQ_BFQQ(rq);
4244
4245 if (bfqq) {
4246 /*
4247 * Increment counters here, because this
4248 * dispatch does not follow the standard
4249 * dispatch flow (where counters are
4250 * incremented)
4251 */
4252 bfqq->dispatched++;
4253
4254 goto inc_in_driver_start_rq;
4255 }
4256
4257 /*
Paolo Valentea7877392018-02-07 22:19:20 +01004258 * We exploit the bfq_finish_requeue_request hook to
4259 * decrement rq_in_driver, but
4260 * bfq_finish_requeue_request will not be invoked on
4261 * this request. So, to avoid unbalance, just start
4262 * this request, without incrementing rq_in_driver. As
4263 * a negative consequence, rq_in_driver is deceptively
4264 * lower than it should be while this request is in
4265 * service. This may cause bfq_schedule_dispatch to be
4266 * invoked uselessly.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004267 *
4268 * As for implementing an exact solution, the
Paolo Valentea7877392018-02-07 22:19:20 +01004269 * bfq_finish_requeue_request hook, if defined, is
4270 * probably invoked also on this request. So, by
4271 * exploiting this hook, we could 1) increment
4272 * rq_in_driver here, and 2) decrement it in
4273 * bfq_finish_requeue_request. Such a solution would
4274 * let the value of the counter be always accurate,
4275 * but it would entail using an extra interface
4276 * function. This cost seems higher than the benefit,
4277 * being the frequency of non-elevator-private
Paolo Valenteaee69d72017-04-19 08:29:02 -06004278 * requests very low.
4279 */
4280 goto start_rq;
4281 }
4282
Paolo Valente73d58112019-01-29 12:06:29 +01004283 bfq_log(bfqd, "dispatch requests: %d busy queues",
4284 bfq_tot_busy_queues(bfqd));
Paolo Valenteaee69d72017-04-19 08:29:02 -06004285
Paolo Valente73d58112019-01-29 12:06:29 +01004286 if (bfq_tot_busy_queues(bfqd) == 0)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004287 goto exit;
4288
4289 /*
4290 * Force device to serve one request at a time if
4291 * strict_guarantees is true. Forcing this service scheme is
4292 * currently the ONLY way to guarantee that the request
4293 * service order enforced by the scheduler is respected by a
4294 * queueing device. Otherwise the device is free even to make
4295 * some unlucky request wait for as long as the device
4296 * wishes.
4297 *
4298 * Of course, serving one request at at time may cause loss of
4299 * throughput.
4300 */
4301 if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0)
4302 goto exit;
4303
4304 bfqq = bfq_select_queue(bfqd);
4305 if (!bfqq)
4306 goto exit;
4307
4308 rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq);
4309
4310 if (rq) {
4311inc_in_driver_start_rq:
4312 bfqd->rq_in_driver++;
4313start_rq:
4314 rq->rq_flags |= RQF_STARTED;
4315 }
4316exit:
4317 return rq;
4318}
4319
Paolo Valente9b25bd02017-12-04 11:42:05 +01004320#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
4321static void bfq_update_dispatch_stats(struct request_queue *q,
4322 struct request *rq,
4323 struct bfq_queue *in_serv_queue,
4324 bool idle_timer_disabled)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004325{
Paolo Valente9b25bd02017-12-04 11:42:05 +01004326 struct bfq_queue *bfqq = rq ? RQ_BFQQ(rq) : NULL;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004327
Paolo Valente24bfd192017-11-13 07:34:09 +01004328 if (!idle_timer_disabled && !bfqq)
Paolo Valente9b25bd02017-12-04 11:42:05 +01004329 return;
Paolo Valente24bfd192017-11-13 07:34:09 +01004330
4331 /*
4332 * rq and bfqq are guaranteed to exist until this function
4333 * ends, for the following reasons. First, rq can be
4334 * dispatched to the device, and then can be completed and
4335 * freed, only after this function ends. Second, rq cannot be
4336 * merged (and thus freed because of a merge) any longer,
4337 * because it has already started. Thus rq cannot be freed
4338 * before this function ends, and, since rq has a reference to
4339 * bfqq, the same guarantee holds for bfqq too.
4340 *
4341 * In addition, the following queue lock guarantees that
4342 * bfqq_group(bfqq) exists as well.
4343 */
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004344 spin_lock_irq(&q->queue_lock);
Paolo Valente24bfd192017-11-13 07:34:09 +01004345 if (idle_timer_disabled)
4346 /*
4347 * Since the idle timer has been disabled,
4348 * in_serv_queue contained some request when
4349 * __bfq_dispatch_request was invoked above, which
4350 * implies that rq was picked exactly from
4351 * in_serv_queue. Thus in_serv_queue == bfqq, and is
4352 * therefore guaranteed to exist because of the above
4353 * arguments.
4354 */
4355 bfqg_stats_update_idle_time(bfqq_group(in_serv_queue));
4356 if (bfqq) {
4357 struct bfq_group *bfqg = bfqq_group(bfqq);
4358
4359 bfqg_stats_update_avg_queue_size(bfqg);
4360 bfqg_stats_set_start_empty_time(bfqg);
4361 bfqg_stats_update_io_remove(bfqg, rq->cmd_flags);
4362 }
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004363 spin_unlock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004364}
4365#else
4366static inline void bfq_update_dispatch_stats(struct request_queue *q,
4367 struct request *rq,
4368 struct bfq_queue *in_serv_queue,
4369 bool idle_timer_disabled) {}
Paolo Valente24bfd192017-11-13 07:34:09 +01004370#endif
4371
Paolo Valente9b25bd02017-12-04 11:42:05 +01004372static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
4373{
4374 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
4375 struct request *rq;
4376 struct bfq_queue *in_serv_queue;
4377 bool waiting_rq, idle_timer_disabled;
4378
4379 spin_lock_irq(&bfqd->lock);
4380
4381 in_serv_queue = bfqd->in_service_queue;
4382 waiting_rq = in_serv_queue && bfq_bfqq_wait_request(in_serv_queue);
4383
4384 rq = __bfq_dispatch_request(hctx);
4385
4386 idle_timer_disabled =
4387 waiting_rq && !bfq_bfqq_wait_request(in_serv_queue);
4388
4389 spin_unlock_irq(&bfqd->lock);
4390
4391 bfq_update_dispatch_stats(hctx->queue, rq, in_serv_queue,
4392 idle_timer_disabled);
4393
Paolo Valenteaee69d72017-04-19 08:29:02 -06004394 return rq;
4395}
4396
4397/*
4398 * Task holds one reference to the queue, dropped when task exits. Each rq
4399 * in-flight on this queue also holds a reference, dropped when rq is freed.
4400 *
4401 * Scheduler lock must be held here. Recall not to use bfqq after calling
4402 * this function on it.
4403 */
Paolo Valenteea25da42017-04-19 08:48:24 -06004404void bfq_put_queue(struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004405{
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004406#ifdef CONFIG_BFQ_GROUP_IOSCHED
4407 struct bfq_group *bfqg = bfqq_group(bfqq);
4408#endif
4409
Paolo Valenteaee69d72017-04-19 08:29:02 -06004410 if (bfqq->bfqd)
4411 bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
4412 bfqq, bfqq->ref);
4413
4414 bfqq->ref--;
4415 if (bfqq->ref)
4416 return;
4417
Paolo Valente99fead82017-10-09 13:11:23 +02004418 if (!hlist_unhashed(&bfqq->burst_list_node)) {
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004419 hlist_del_init(&bfqq->burst_list_node);
Paolo Valente99fead82017-10-09 13:11:23 +02004420 /*
4421 * Decrement also burst size after the removal, if the
4422 * process associated with bfqq is exiting, and thus
4423 * does not contribute to the burst any longer. This
4424 * decrement helps filter out false positives of large
4425 * bursts, when some short-lived process (often due to
4426 * the execution of commands by some service) happens
4427 * to start and exit while a complex application is
4428 * starting, and thus spawning several processes that
4429 * do I/O (and that *must not* be treated as a large
4430 * burst, see comments on bfq_handle_burst).
4431 *
4432 * In particular, the decrement is performed only if:
4433 * 1) bfqq is not a merged queue, because, if it is,
4434 * then this free of bfqq is not triggered by the exit
4435 * of the process bfqq is associated with, but exactly
4436 * by the fact that bfqq has just been merged.
4437 * 2) burst_size is greater than 0, to handle
4438 * unbalanced decrements. Unbalanced decrements may
4439 * happen in te following case: bfqq is inserted into
4440 * the current burst list--without incrementing
4441 * bust_size--because of a split, but the current
4442 * burst list is not the burst list bfqq belonged to
4443 * (see comments on the case of a split in
4444 * bfq_set_request).
4445 */
4446 if (bfqq->bic && bfqq->bfqd->burst_size > 0)
4447 bfqq->bfqd->burst_size--;
Paolo Valente7cb04002017-09-21 11:04:03 +02004448 }
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004449
Paolo Valenteaee69d72017-04-19 08:29:02 -06004450 kmem_cache_free(bfq_pool, bfqq);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004451#ifdef CONFIG_BFQ_GROUP_IOSCHED
Paolo Valente8f9bebc2017-06-05 10:11:15 +02004452 bfqg_and_blkg_put(bfqg);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004453#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06004454}
4455
Arianna Avanzini36eca892017-04-12 18:23:16 +02004456static void bfq_put_cooperator(struct bfq_queue *bfqq)
4457{
4458 struct bfq_queue *__bfqq, *next;
4459
4460 /*
4461 * If this queue was scheduled to merge with another queue, be
4462 * sure to drop the reference taken on that queue (and others in
4463 * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
4464 */
4465 __bfqq = bfqq->new_bfqq;
4466 while (__bfqq) {
4467 if (__bfqq == bfqq)
4468 break;
4469 next = __bfqq->new_bfqq;
4470 bfq_put_queue(__bfqq);
4471 __bfqq = next;
4472 }
4473}
4474
Paolo Valenteaee69d72017-04-19 08:29:02 -06004475static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
4476{
4477 if (bfqq == bfqd->in_service_queue) {
4478 __bfq_bfqq_expire(bfqd, bfqq);
4479 bfq_schedule_dispatch(bfqd);
4480 }
4481
4482 bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref);
4483
Arianna Avanzini36eca892017-04-12 18:23:16 +02004484 bfq_put_cooperator(bfqq);
4485
Paolo Valenteaee69d72017-04-19 08:29:02 -06004486 bfq_put_queue(bfqq); /* release process reference */
4487}
4488
4489static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync)
4490{
4491 struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
4492 struct bfq_data *bfqd;
4493
4494 if (bfqq)
4495 bfqd = bfqq->bfqd; /* NULL if scheduler already exited */
4496
4497 if (bfqq && bfqd) {
4498 unsigned long flags;
4499
4500 spin_lock_irqsave(&bfqd->lock, flags);
4501 bfq_exit_bfqq(bfqd, bfqq);
4502 bic_set_bfqq(bic, NULL, is_sync);
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02004503 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004504 }
4505}
4506
4507static void bfq_exit_icq(struct io_cq *icq)
4508{
4509 struct bfq_io_cq *bic = icq_to_bic(icq);
4510
4511 bfq_exit_icq_bfqq(bic, true);
4512 bfq_exit_icq_bfqq(bic, false);
4513}
4514
4515/*
4516 * Update the entity prio values; note that the new values will not
4517 * be used until the next (re)activation.
4518 */
4519static void
4520bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
4521{
4522 struct task_struct *tsk = current;
4523 int ioprio_class;
4524 struct bfq_data *bfqd = bfqq->bfqd;
4525
4526 if (!bfqd)
4527 return;
4528
4529 ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
4530 switch (ioprio_class) {
4531 default:
4532 dev_err(bfqq->bfqd->queue->backing_dev_info->dev,
4533 "bfq: bad prio class %d\n", ioprio_class);
Bart Van Asschefa393d12017-08-30 11:42:07 -07004534 /* fall through */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004535 case IOPRIO_CLASS_NONE:
4536 /*
4537 * No prio set, inherit CPU scheduling settings.
4538 */
4539 bfqq->new_ioprio = task_nice_ioprio(tsk);
4540 bfqq->new_ioprio_class = task_nice_ioclass(tsk);
4541 break;
4542 case IOPRIO_CLASS_RT:
4543 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4544 bfqq->new_ioprio_class = IOPRIO_CLASS_RT;
4545 break;
4546 case IOPRIO_CLASS_BE:
4547 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4548 bfqq->new_ioprio_class = IOPRIO_CLASS_BE;
4549 break;
4550 case IOPRIO_CLASS_IDLE:
4551 bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE;
4552 bfqq->new_ioprio = 7;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004553 break;
4554 }
4555
4556 if (bfqq->new_ioprio >= IOPRIO_BE_NR) {
4557 pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n",
4558 bfqq->new_ioprio);
4559 bfqq->new_ioprio = IOPRIO_BE_NR;
4560 }
4561
4562 bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio);
4563 bfqq->entity.prio_changed = 1;
4564}
4565
Paolo Valenteea25da42017-04-19 08:48:24 -06004566static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
4567 struct bio *bio, bool is_sync,
4568 struct bfq_io_cq *bic);
4569
Paolo Valenteaee69d72017-04-19 08:29:02 -06004570static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio)
4571{
4572 struct bfq_data *bfqd = bic_to_bfqd(bic);
4573 struct bfq_queue *bfqq;
4574 int ioprio = bic->icq.ioc->ioprio;
4575
4576 /*
4577 * This condition may trigger on a newly created bic, be sure to
4578 * drop the lock before returning.
4579 */
4580 if (unlikely(!bfqd) || likely(bic->ioprio == ioprio))
4581 return;
4582
4583 bic->ioprio = ioprio;
4584
4585 bfqq = bic_to_bfqq(bic, false);
4586 if (bfqq) {
4587 /* release process reference on this queue */
4588 bfq_put_queue(bfqq);
4589 bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic);
4590 bic_set_bfqq(bic, bfqq, false);
4591 }
4592
4593 bfqq = bic_to_bfqq(bic, true);
4594 if (bfqq)
4595 bfq_set_next_ioprio_data(bfqq, bic);
4596}
4597
4598static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4599 struct bfq_io_cq *bic, pid_t pid, int is_sync)
4600{
4601 RB_CLEAR_NODE(&bfqq->entity.rb_node);
4602 INIT_LIST_HEAD(&bfqq->fifo);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004603 INIT_HLIST_NODE(&bfqq->burst_list_node);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004604
4605 bfqq->ref = 0;
4606 bfqq->bfqd = bfqd;
4607
4608 if (bic)
4609 bfq_set_next_ioprio_data(bfqq, bic);
4610
4611 if (is_sync) {
Paolo Valented5be3fe2017-08-04 07:35:10 +02004612 /*
4613 * No need to mark as has_short_ttime if in
4614 * idle_class, because no device idling is performed
4615 * for queues in idle class
4616 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004617 if (!bfq_class_idle(bfqq))
Paolo Valented5be3fe2017-08-04 07:35:10 +02004618 /* tentatively mark as has_short_ttime */
4619 bfq_mark_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004620 bfq_mark_bfqq_sync(bfqq);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02004621 bfq_mark_bfqq_just_created(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004622 } else
4623 bfq_clear_bfqq_sync(bfqq);
4624
4625 /* set end request to minus infinity from now */
4626 bfqq->ttime.last_end_request = ktime_get_ns() + 1;
4627
4628 bfq_mark_bfqq_IO_bound(bfqq);
4629
4630 bfqq->pid = pid;
4631
4632 /* Tentative initial value to trade off between thr and lat */
Paolo Valente54b60452017-04-12 18:23:09 +02004633 bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004634 bfqq->budget_timeout = bfq_smallest_from_now();
Paolo Valenteaee69d72017-04-19 08:29:02 -06004635
Paolo Valente44e44a12017-04-12 18:23:12 +02004636 bfqq->wr_coeff = 1;
Arianna Avanzini36eca892017-04-12 18:23:16 +02004637 bfqq->last_wr_start_finish = jiffies;
Paolo Valente77b7dce2017-04-12 18:23:13 +02004638 bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now();
Arianna Avanzini36eca892017-04-12 18:23:16 +02004639 bfqq->split_time = bfq_smallest_from_now();
Paolo Valente77b7dce2017-04-12 18:23:13 +02004640
4641 /*
Paolo Valentea34b0242017-12-15 07:23:12 +01004642 * To not forget the possibly high bandwidth consumed by a
4643 * process/queue in the recent past,
4644 * bfq_bfqq_softrt_next_start() returns a value at least equal
4645 * to the current value of bfqq->soft_rt_next_start (see
4646 * comments on bfq_bfqq_softrt_next_start). Set
4647 * soft_rt_next_start to now, to mean that bfqq has consumed
4648 * no bandwidth so far.
Paolo Valente77b7dce2017-04-12 18:23:13 +02004649 */
Paolo Valentea34b0242017-12-15 07:23:12 +01004650 bfqq->soft_rt_next_start = jiffies;
Paolo Valente44e44a12017-04-12 18:23:12 +02004651
Paolo Valenteaee69d72017-04-19 08:29:02 -06004652 /* first request is almost certainly seeky */
4653 bfqq->seek_history = 1;
4654}
4655
4656static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004657 struct bfq_group *bfqg,
Paolo Valenteaee69d72017-04-19 08:29:02 -06004658 int ioprio_class, int ioprio)
4659{
4660 switch (ioprio_class) {
4661 case IOPRIO_CLASS_RT:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004662 return &bfqg->async_bfqq[0][ioprio];
Paolo Valenteaee69d72017-04-19 08:29:02 -06004663 case IOPRIO_CLASS_NONE:
4664 ioprio = IOPRIO_NORM;
4665 /* fall through */
4666 case IOPRIO_CLASS_BE:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004667 return &bfqg->async_bfqq[1][ioprio];
Paolo Valenteaee69d72017-04-19 08:29:02 -06004668 case IOPRIO_CLASS_IDLE:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004669 return &bfqg->async_idle_bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004670 default:
4671 return NULL;
4672 }
4673}
4674
4675static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
4676 struct bio *bio, bool is_sync,
4677 struct bfq_io_cq *bic)
4678{
4679 const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
4680 const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
4681 struct bfq_queue **async_bfqq = NULL;
4682 struct bfq_queue *bfqq;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004683 struct bfq_group *bfqg;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004684
4685 rcu_read_lock();
4686
Dennis Zhou0fe061b2018-12-05 12:10:26 -05004687 bfqg = bfq_find_set_group(bfqd, __bio_blkcg(bio));
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004688 if (!bfqg) {
4689 bfqq = &bfqd->oom_bfqq;
4690 goto out;
4691 }
4692
Paolo Valenteaee69d72017-04-19 08:29:02 -06004693 if (!is_sync) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004694 async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
Paolo Valenteaee69d72017-04-19 08:29:02 -06004695 ioprio);
4696 bfqq = *async_bfqq;
4697 if (bfqq)
4698 goto out;
4699 }
4700
4701 bfqq = kmem_cache_alloc_node(bfq_pool,
4702 GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
4703 bfqd->queue->node);
4704
4705 if (bfqq) {
4706 bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
4707 is_sync);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004708 bfq_init_entity(&bfqq->entity, bfqg);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004709 bfq_log_bfqq(bfqd, bfqq, "allocated");
4710 } else {
4711 bfqq = &bfqd->oom_bfqq;
4712 bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
4713 goto out;
4714 }
4715
4716 /*
4717 * Pin the queue now that it's allocated, scheduler exit will
4718 * prune it.
4719 */
4720 if (async_bfqq) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02004721 bfqq->ref++; /*
4722 * Extra group reference, w.r.t. sync
4723 * queue. This extra reference is removed
4724 * only if bfqq->bfqg disappears, to
4725 * guarantee that this queue is not freed
4726 * until its group goes away.
4727 */
4728 bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
Paolo Valenteaee69d72017-04-19 08:29:02 -06004729 bfqq, bfqq->ref);
4730 *async_bfqq = bfqq;
4731 }
4732
4733out:
4734 bfqq->ref++; /* get a process reference to this queue */
4735 bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref);
4736 rcu_read_unlock();
4737 return bfqq;
4738}
4739
4740static void bfq_update_io_thinktime(struct bfq_data *bfqd,
4741 struct bfq_queue *bfqq)
4742{
4743 struct bfq_ttime *ttime = &bfqq->ttime;
4744 u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request;
4745
4746 elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle);
4747
4748 ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8;
4749 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8);
4750 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
4751 ttime->ttime_samples);
4752}
4753
4754static void
4755bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4756 struct request *rq)
4757{
Paolo Valenteaee69d72017-04-19 08:29:02 -06004758 bfqq->seek_history <<= 1;
Paolo Valented87447d2019-01-29 12:06:33 +01004759 bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004760}
4761
Paolo Valented5be3fe2017-08-04 07:35:10 +02004762static void bfq_update_has_short_ttime(struct bfq_data *bfqd,
4763 struct bfq_queue *bfqq,
4764 struct bfq_io_cq *bic)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004765{
Paolo Valented5be3fe2017-08-04 07:35:10 +02004766 bool has_short_ttime = true;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004767
Paolo Valented5be3fe2017-08-04 07:35:10 +02004768 /*
4769 * No need to update has_short_ttime if bfqq is async or in
4770 * idle io prio class, or if bfq_slice_idle is zero, because
4771 * no device idling is performed for bfqq in this case.
4772 */
4773 if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq) ||
4774 bfqd->bfq_slice_idle == 0)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004775 return;
4776
Arianna Avanzini36eca892017-04-12 18:23:16 +02004777 /* Idle window just restored, statistics are meaningless. */
4778 if (time_is_after_eq_jiffies(bfqq->split_time +
4779 bfqd->bfq_wr_min_idle_time))
4780 return;
4781
Paolo Valented5be3fe2017-08-04 07:35:10 +02004782 /* Think time is infinite if no process is linked to
4783 * bfqq. Otherwise check average think time to
4784 * decide whether to mark as has_short_ttime
4785 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06004786 if (atomic_read(&bic->icq.ioc->active_ref) == 0 ||
Paolo Valented5be3fe2017-08-04 07:35:10 +02004787 (bfq_sample_valid(bfqq->ttime.ttime_samples) &&
4788 bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle))
4789 has_short_ttime = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004790
Paolo Valented5be3fe2017-08-04 07:35:10 +02004791 bfq_log_bfqq(bfqd, bfqq, "update_has_short_ttime: has_short_ttime %d",
4792 has_short_ttime);
4793
4794 if (has_short_ttime)
4795 bfq_mark_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004796 else
Paolo Valented5be3fe2017-08-04 07:35:10 +02004797 bfq_clear_bfqq_has_short_ttime(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004798}
4799
4800/*
4801 * Called when a new fs request (rq) is added to bfqq. Check if there's
4802 * something we should do about it.
4803 */
4804static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
4805 struct request *rq)
4806{
4807 struct bfq_io_cq *bic = RQ_BIC(rq);
4808
4809 if (rq->cmd_flags & REQ_META)
4810 bfqq->meta_pending++;
4811
4812 bfq_update_io_thinktime(bfqd, bfqq);
Paolo Valented5be3fe2017-08-04 07:35:10 +02004813 bfq_update_has_short_ttime(bfqd, bfqq, bic);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004814 bfq_update_io_seektime(bfqd, bfqq, rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004815
4816 bfq_log_bfqq(bfqd, bfqq,
Paolo Valented5be3fe2017-08-04 07:35:10 +02004817 "rq_enqueued: has_short_ttime=%d (seeky %d)",
4818 bfq_bfqq_has_short_ttime(bfqq), BFQQ_SEEKY(bfqq));
Paolo Valenteaee69d72017-04-19 08:29:02 -06004819
4820 bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
4821
4822 if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
4823 bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
4824 blk_rq_sectors(rq) < 32;
4825 bool budget_timeout = bfq_bfqq_budget_timeout(bfqq);
4826
4827 /*
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004828 * There is just this request queued: if
4829 * - the request is small, and
4830 * - we are idling to boost throughput, and
4831 * - the queue is not to be expired,
4832 * then just exit.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004833 *
4834 * In this way, if the device is being idled to wait
4835 * for a new request from the in-service queue, we
4836 * avoid unplugging the device and committing the
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004837 * device to serve just a small request. In contrast
4838 * we wait for the block layer to decide when to
4839 * unplug the device: hopefully, new requests will be
4840 * merged to this one quickly, then the device will be
4841 * unplugged and larger requests will be dispatched.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004842 */
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004843 if (small_req && idling_boosts_thr_without_issues(bfqd, bfqq) &&
4844 !budget_timeout)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004845 return;
4846
4847 /*
Paolo Valenteac8b0cb2019-01-29 12:06:31 +01004848 * A large enough request arrived, or idling is being
4849 * performed to preserve service guarantees, or
4850 * finally the queue is to be expired: in all these
4851 * cases disk idling is to be stopped, so clear
4852 * wait_request flag and reset timer.
Paolo Valenteaee69d72017-04-19 08:29:02 -06004853 */
4854 bfq_clear_bfqq_wait_request(bfqq);
4855 hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
4856
4857 /*
4858 * The queue is not empty, because a new request just
4859 * arrived. Hence we can safely expire the queue, in
4860 * case of budget timeout, without risking that the
4861 * timestamps of the queue are not updated correctly.
4862 * See [1] for more details.
4863 */
4864 if (budget_timeout)
4865 bfq_bfqq_expire(bfqd, bfqq, false,
4866 BFQQE_BUDGET_TIMEOUT);
4867 }
4868}
4869
Paolo Valente24bfd192017-11-13 07:34:09 +01004870/* returns true if it causes the idle timer to be disabled */
4871static bool __bfq_insert_request(struct bfq_data *bfqd, struct request *rq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06004872{
Arianna Avanzini36eca892017-04-12 18:23:16 +02004873 struct bfq_queue *bfqq = RQ_BFQQ(rq),
4874 *new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
Paolo Valente24bfd192017-11-13 07:34:09 +01004875 bool waiting, idle_timer_disabled = false;
Arianna Avanzini36eca892017-04-12 18:23:16 +02004876
4877 if (new_bfqq) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02004878 /*
4879 * Release the request's reference to the old bfqq
4880 * and make sure one is taken to the shared queue.
4881 */
4882 new_bfqq->allocated++;
4883 bfqq->allocated--;
4884 new_bfqq->ref++;
4885 /*
4886 * If the bic associated with the process
4887 * issuing this request still points to bfqq
4888 * (and thus has not been already redirected
4889 * to new_bfqq or even some other bfq_queue),
4890 * then complete the merge and redirect it to
4891 * new_bfqq.
4892 */
4893 if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
4894 bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
4895 bfqq, new_bfqq);
Paolo Valente894df932017-09-21 11:04:02 +02004896
4897 bfq_clear_bfqq_just_created(bfqq);
Arianna Avanzini36eca892017-04-12 18:23:16 +02004898 /*
4899 * rq is about to be enqueued into new_bfqq,
4900 * release rq reference on bfqq
4901 */
4902 bfq_put_queue(bfqq);
4903 rq->elv.priv[1] = new_bfqq;
4904 bfqq = new_bfqq;
4905 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06004906
Paolo Valente24bfd192017-11-13 07:34:09 +01004907 waiting = bfqq && bfq_bfqq_wait_request(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004908 bfq_add_request(rq);
Paolo Valente24bfd192017-11-13 07:34:09 +01004909 idle_timer_disabled = waiting && !bfq_bfqq_wait_request(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004910
4911 rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)];
4912 list_add_tail(&rq->queuelist, &bfqq->fifo);
4913
4914 bfq_rq_enqueued(bfqd, bfqq, rq);
Paolo Valente24bfd192017-11-13 07:34:09 +01004915
4916 return idle_timer_disabled;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004917}
4918
Paolo Valente9b25bd02017-12-04 11:42:05 +01004919#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
4920static void bfq_update_insert_stats(struct request_queue *q,
4921 struct bfq_queue *bfqq,
4922 bool idle_timer_disabled,
4923 unsigned int cmd_flags)
4924{
4925 if (!bfqq)
4926 return;
4927
4928 /*
4929 * bfqq still exists, because it can disappear only after
4930 * either it is merged with another queue, or the process it
4931 * is associated with exits. But both actions must be taken by
4932 * the same process currently executing this flow of
4933 * instructions.
4934 *
4935 * In addition, the following queue lock guarantees that
4936 * bfqq_group(bfqq) exists as well.
4937 */
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004938 spin_lock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004939 bfqg_stats_update_io_add(bfqq_group(bfqq), bfqq, cmd_flags);
4940 if (idle_timer_disabled)
4941 bfqg_stats_update_idle_time(bfqq_group(bfqq));
Christoph Hellwig0d945c12018-11-15 12:17:28 -07004942 spin_unlock_irq(&q->queue_lock);
Paolo Valente9b25bd02017-12-04 11:42:05 +01004943}
4944#else
4945static inline void bfq_update_insert_stats(struct request_queue *q,
4946 struct bfq_queue *bfqq,
4947 bool idle_timer_disabled,
4948 unsigned int cmd_flags) {}
4949#endif
4950
Paolo Valenteaee69d72017-04-19 08:29:02 -06004951static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
4952 bool at_head)
4953{
4954 struct request_queue *q = hctx->queue;
4955 struct bfq_data *bfqd = q->elevator->elevator_data;
Paolo Valente18e5a572018-05-04 19:17:01 +02004956 struct bfq_queue *bfqq;
Paolo Valente24bfd192017-11-13 07:34:09 +01004957 bool idle_timer_disabled = false;
4958 unsigned int cmd_flags;
Paolo Valenteaee69d72017-04-19 08:29:02 -06004959
4960 spin_lock_irq(&bfqd->lock);
4961 if (blk_mq_sched_try_insert_merge(q, rq)) {
4962 spin_unlock_irq(&bfqd->lock);
4963 return;
4964 }
4965
4966 spin_unlock_irq(&bfqd->lock);
4967
4968 blk_mq_sched_request_inserted(rq);
4969
4970 spin_lock_irq(&bfqd->lock);
Paolo Valente18e5a572018-05-04 19:17:01 +02004971 bfqq = bfq_init_rq(rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004972 if (at_head || blk_rq_is_passthrough(rq)) {
4973 if (at_head)
4974 list_add(&rq->queuelist, &bfqd->dispatch);
4975 else
4976 list_add_tail(&rq->queuelist, &bfqd->dispatch);
Paolo Valente18e5a572018-05-04 19:17:01 +02004977 } else { /* bfqq is assumed to be non null here */
Paolo Valente24bfd192017-11-13 07:34:09 +01004978 idle_timer_disabled = __bfq_insert_request(bfqd, rq);
Luca Miccio614822f2017-11-13 07:34:08 +01004979 /*
4980 * Update bfqq, because, if a queue merge has occurred
4981 * in __bfq_insert_request, then rq has been
4982 * redirected into a new queue.
4983 */
4984 bfqq = RQ_BFQQ(rq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06004985
4986 if (rq_mergeable(rq)) {
4987 elv_rqhash_add(q, rq);
4988 if (!q->last_merge)
4989 q->last_merge = rq;
4990 }
4991 }
4992
Paolo Valente24bfd192017-11-13 07:34:09 +01004993 /*
4994 * Cache cmd_flags before releasing scheduler lock, because rq
4995 * may disappear afterwards (for example, because of a request
4996 * merge).
4997 */
4998 cmd_flags = rq->cmd_flags;
Paolo Valente9b25bd02017-12-04 11:42:05 +01004999
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005000 spin_unlock_irq(&bfqd->lock);
Paolo Valente24bfd192017-11-13 07:34:09 +01005001
Paolo Valente9b25bd02017-12-04 11:42:05 +01005002 bfq_update_insert_stats(q, bfqq, idle_timer_disabled,
5003 cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005004}
5005
5006static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
5007 struct list_head *list, bool at_head)
5008{
5009 while (!list_empty(list)) {
5010 struct request *rq;
5011
5012 rq = list_first_entry(list, struct request, queuelist);
5013 list_del_init(&rq->queuelist);
5014 bfq_insert_request(hctx, rq, at_head);
5015 }
5016}
5017
5018static void bfq_update_hw_tag(struct bfq_data *bfqd)
5019{
Paolo Valenteb3c34982019-01-29 12:06:36 +01005020 struct bfq_queue *bfqq = bfqd->in_service_queue;
5021
Paolo Valenteaee69d72017-04-19 08:29:02 -06005022 bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver,
5023 bfqd->rq_in_driver);
5024
5025 if (bfqd->hw_tag == 1)
5026 return;
5027
5028 /*
5029 * This sample is valid if the number of outstanding requests
5030 * is large enough to allow a queueing behavior. Note that the
5031 * sum is not exact, as it's not taking into account deactivated
5032 * requests.
5033 */
Paolo Valentea3c92562019-01-29 12:06:35 +01005034 if (bfqd->rq_in_driver + bfqd->queued <= BFQ_HW_QUEUE_THRESHOLD)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005035 return;
5036
Paolo Valenteb3c34982019-01-29 12:06:36 +01005037 /*
5038 * If active queue hasn't enough requests and can idle, bfq might not
5039 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
5040 * case
5041 */
5042 if (bfqq && bfq_bfqq_has_short_ttime(bfqq) &&
5043 bfqq->dispatched + bfqq->queued[0] + bfqq->queued[1] <
5044 BFQ_HW_QUEUE_THRESHOLD &&
5045 bfqd->rq_in_driver < BFQ_HW_QUEUE_THRESHOLD)
5046 return;
5047
Paolo Valenteaee69d72017-04-19 08:29:02 -06005048 if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
5049 return;
5050
5051 bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
5052 bfqd->max_rq_in_driver = 0;
5053 bfqd->hw_tag_samples = 0;
5054}
5055
5056static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd)
5057{
Paolo Valenteab0e43e2017-04-12 18:23:10 +02005058 u64 now_ns;
5059 u32 delta_us;
5060
Paolo Valenteaee69d72017-04-19 08:29:02 -06005061 bfq_update_hw_tag(bfqd);
5062
5063 bfqd->rq_in_driver--;
5064 bfqq->dispatched--;
5065
Paolo Valente44e44a12017-04-12 18:23:12 +02005066 if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
5067 /*
5068 * Set budget_timeout (which we overload to store the
5069 * time at which the queue remains with no backlog and
5070 * no outstanding request; used by the weight-raising
5071 * mechanism).
5072 */
5073 bfqq->budget_timeout = jiffies;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02005074
Paolo Valente04715592018-06-25 21:55:34 +02005075 bfq_weights_tree_remove(bfqd, bfqq);
Paolo Valente44e44a12017-04-12 18:23:12 +02005076 }
5077
Paolo Valenteab0e43e2017-04-12 18:23:10 +02005078 now_ns = ktime_get_ns();
5079
5080 bfqq->ttime.last_end_request = now_ns;
5081
5082 /*
5083 * Using us instead of ns, to get a reasonable precision in
5084 * computing rate in next check.
5085 */
5086 delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC);
5087
5088 /*
5089 * If the request took rather long to complete, and, according
5090 * to the maximum request size recorded, this completion latency
5091 * implies that the request was certainly served at a very low
5092 * rate (less than 1M sectors/sec), then the whole observation
5093 * interval that lasts up to this time instant cannot be a
5094 * valid time interval for computing a new peak rate. Invoke
5095 * bfq_update_rate_reset to have the following three steps
5096 * taken:
5097 * - close the observation interval at the last (previous)
5098 * request dispatch or completion
5099 * - compute rate, if possible, for that observation interval
5100 * - reset to zero samples, which will trigger a proper
5101 * re-initialization of the observation interval on next
5102 * dispatch
5103 */
5104 if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC &&
5105 (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us <
5106 1UL<<(BFQ_RATE_SHIFT - 10))
5107 bfq_update_rate_reset(bfqd, NULL);
5108 bfqd->last_completion = now_ns;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005109
5110 /*
Paolo Valente77b7dce2017-04-12 18:23:13 +02005111 * If we are waiting to discover whether the request pattern
5112 * of the task associated with the queue is actually
5113 * isochronous, and both requisites for this condition to hold
5114 * are now satisfied, then compute soft_rt_next_start (see the
5115 * comments on the function bfq_bfqq_softrt_next_start()). We
Paolo Valente20cd3242019-01-29 12:06:25 +01005116 * do not compute soft_rt_next_start if bfqq is in interactive
5117 * weight raising (see the comments in bfq_bfqq_expire() for
5118 * an explanation). We schedule this delayed update when bfqq
5119 * expires, if it still has in-flight requests.
Paolo Valente77b7dce2017-04-12 18:23:13 +02005120 */
5121 if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
Paolo Valente20cd3242019-01-29 12:06:25 +01005122 RB_EMPTY_ROOT(&bfqq->sort_list) &&
5123 bfqq->wr_coeff != bfqd->bfq_wr_coeff)
Paolo Valente77b7dce2017-04-12 18:23:13 +02005124 bfqq->soft_rt_next_start =
5125 bfq_bfqq_softrt_next_start(bfqd, bfqq);
5126
5127 /*
Paolo Valenteaee69d72017-04-19 08:29:02 -06005128 * If this is the in-service queue, check if it needs to be expired,
5129 * or if we want to idle in case it has no pending requests.
5130 */
5131 if (bfqd->in_service_queue == bfqq) {
Paolo Valente4420b092018-06-25 21:55:35 +02005132 if (bfq_bfqq_must_idle(bfqq)) {
5133 if (bfqq->dispatched == 0)
5134 bfq_arm_slice_timer(bfqd);
5135 /*
5136 * If we get here, we do not expire bfqq, even
5137 * if bfqq was in budget timeout or had no
5138 * more requests (as controlled in the next
5139 * conditional instructions). The reason for
5140 * not expiring bfqq is as follows.
5141 *
5142 * Here bfqq->dispatched > 0 holds, but
5143 * bfq_bfqq_must_idle() returned true. This
5144 * implies that, even if no request arrives
5145 * for bfqq before bfqq->dispatched reaches 0,
5146 * bfqq will, however, not be expired on the
5147 * completion event that causes bfqq->dispatch
5148 * to reach zero. In contrast, on this event,
5149 * bfqq will start enjoying device idling
5150 * (I/O-dispatch plugging).
5151 *
5152 * But, if we expired bfqq here, bfqq would
5153 * not have the chance to enjoy device idling
5154 * when bfqq->dispatched finally reaches
5155 * zero. This would expose bfqq to violation
5156 * of its reserved service guarantees.
5157 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06005158 return;
5159 } else if (bfq_may_expire_for_budg_timeout(bfqq))
5160 bfq_bfqq_expire(bfqd, bfqq, false,
5161 BFQQE_BUDGET_TIMEOUT);
5162 else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
5163 (bfqq->dispatched == 0 ||
Paolo Valente277a4a92018-06-25 21:55:37 +02005164 !bfq_better_to_idle(bfqq)))
Paolo Valenteaee69d72017-04-19 08:29:02 -06005165 bfq_bfqq_expire(bfqd, bfqq, false,
5166 BFQQE_NO_MORE_REQUESTS);
5167 }
Hou Tao3f7cb4f2017-07-11 21:58:15 +08005168
5169 if (!bfqd->rq_in_driver)
5170 bfq_schedule_dispatch(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005171}
5172
Paolo Valentea7877392018-02-07 22:19:20 +01005173static void bfq_finish_requeue_request_body(struct bfq_queue *bfqq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005174{
5175 bfqq->allocated--;
5176
5177 bfq_put_queue(bfqq);
5178}
5179
Paolo Valentea7877392018-02-07 22:19:20 +01005180/*
Paolo Valente2341d6622019-03-12 09:59:29 +01005181 * The processes associated with bfqq may happen to generate their
5182 * cumulative I/O at a lower rate than the rate at which the device
5183 * could serve the same I/O. This is rather probable, e.g., if only
5184 * one process is associated with bfqq and the device is an SSD. It
5185 * results in bfqq becoming often empty while in service. In this
5186 * respect, if BFQ is allowed to switch to another queue when bfqq
5187 * remains empty, then the device goes on being fed with I/O requests,
5188 * and the throughput is not affected. In contrast, if BFQ is not
5189 * allowed to switch to another queue---because bfqq is sync and
5190 * I/O-dispatch needs to be plugged while bfqq is temporarily
5191 * empty---then, during the service of bfqq, there will be frequent
5192 * "service holes", i.e., time intervals during which bfqq gets empty
5193 * and the device can only consume the I/O already queued in its
5194 * hardware queues. During service holes, the device may even get to
5195 * remaining idle. In the end, during the service of bfqq, the device
5196 * is driven at a lower speed than the one it can reach with the kind
5197 * of I/O flowing through bfqq.
5198 *
5199 * To counter this loss of throughput, BFQ implements a "request
5200 * injection mechanism", which tries to fill the above service holes
5201 * with I/O requests taken from other queues. The hard part in this
5202 * mechanism is finding the right amount of I/O to inject, so as to
5203 * both boost throughput and not break bfqq's bandwidth and latency
5204 * guarantees. In this respect, the mechanism maintains a per-queue
5205 * inject limit, computed as below. While bfqq is empty, the injection
5206 * mechanism dispatches extra I/O requests only until the total number
5207 * of I/O requests in flight---i.e., already dispatched but not yet
5208 * completed---remains lower than this limit.
5209 *
5210 * A first definition comes in handy to introduce the algorithm by
5211 * which the inject limit is computed. We define as first request for
5212 * bfqq, an I/O request for bfqq that arrives while bfqq is in
5213 * service, and causes bfqq to switch from empty to non-empty. The
5214 * algorithm updates the limit as a function of the effect of
5215 * injection on the service times of only the first requests of
5216 * bfqq. The reason for this restriction is that these are the
5217 * requests whose service time is affected most, because they are the
5218 * first to arrive after injection possibly occurred.
5219 *
5220 * To evaluate the effect of injection, the algorithm measures the
5221 * "total service time" of first requests. We define as total service
5222 * time of an I/O request, the time that elapses since when the
5223 * request is enqueued into bfqq, to when it is completed. This
5224 * quantity allows the whole effect of injection to be measured. It is
5225 * easy to see why. Suppose that some requests of other queues are
5226 * actually injected while bfqq is empty, and that a new request R
5227 * then arrives for bfqq. If the device does start to serve all or
5228 * part of the injected requests during the service hole, then,
5229 * because of this extra service, it may delay the next invocation of
5230 * the dispatch hook of BFQ. Then, even after R gets eventually
5231 * dispatched, the device may delay the actual service of R if it is
5232 * still busy serving the extra requests, or if it decides to serve,
5233 * before R, some extra request still present in its queues. As a
5234 * conclusion, the cumulative extra delay caused by injection can be
5235 * easily evaluated by just comparing the total service time of first
5236 * requests with and without injection.
5237 *
5238 * The limit-update algorithm works as follows. On the arrival of a
5239 * first request of bfqq, the algorithm measures the total time of the
5240 * request only if one of the three cases below holds, and, for each
5241 * case, it updates the limit as described below:
5242 *
5243 * (1) If there is no in-flight request. This gives a baseline for the
5244 * total service time of the requests of bfqq. If the baseline has
5245 * not been computed yet, then, after computing it, the limit is
5246 * set to 1, to start boosting throughput, and to prepare the
5247 * ground for the next case. If the baseline has already been
5248 * computed, then it is updated, in case it results to be lower
5249 * than the previous value.
5250 *
5251 * (2) If the limit is higher than 0 and there are in-flight
5252 * requests. By comparing the total service time in this case with
5253 * the above baseline, it is possible to know at which extent the
5254 * current value of the limit is inflating the total service
5255 * time. If the inflation is below a certain threshold, then bfqq
5256 * is assumed to be suffering from no perceivable loss of its
5257 * service guarantees, and the limit is even tentatively
5258 * increased. If the inflation is above the threshold, then the
5259 * limit is decreased. Due to the lack of any hysteresis, this
5260 * logic makes the limit oscillate even in steady workload
5261 * conditions. Yet we opted for it, because it is fast in reaching
5262 * the best value for the limit, as a function of the current I/O
5263 * workload. To reduce oscillations, this step is disabled for a
5264 * short time interval after the limit happens to be decreased.
5265 *
5266 * (3) Periodically, after resetting the limit, to make sure that the
5267 * limit eventually drops in case the workload changes. This is
5268 * needed because, after the limit has gone safely up for a
5269 * certain workload, it is impossible to guess whether the
5270 * baseline total service time may have changed, without measuring
5271 * it again without injection. A more effective version of this
5272 * step might be to just sample the baseline, by interrupting
5273 * injection only once, and then to reset/lower the limit only if
5274 * the total service time with the current limit does happen to be
5275 * too large.
5276 *
5277 * More details on each step are provided in the comments on the
5278 * pieces of code that implement these steps: the branch handling the
5279 * transition from empty to non empty in bfq_add_request(), the branch
5280 * handling injection in bfq_select_queue(), and the function
5281 * bfq_choose_bfqq_for_injection(). These comments also explain some
5282 * exceptions, made by the injection mechanism in some special cases.
5283 */
5284static void bfq_update_inject_limit(struct bfq_data *bfqd,
5285 struct bfq_queue *bfqq)
5286{
5287 u64 tot_time_ns = ktime_get_ns() - bfqd->last_empty_occupied_ns;
5288 unsigned int old_limit = bfqq->inject_limit;
5289
5290 if (bfqq->last_serv_time_ns > 0) {
5291 u64 threshold = (bfqq->last_serv_time_ns * 3)>>1;
5292
5293 if (tot_time_ns >= threshold && old_limit > 0) {
5294 bfqq->inject_limit--;
5295 bfqq->decrease_time_jif = jiffies;
5296 } else if (tot_time_ns < threshold &&
5297 old_limit < bfqd->max_rq_in_driver<<1)
5298 bfqq->inject_limit++;
5299 }
5300
5301 /*
5302 * Either we still have to compute the base value for the
5303 * total service time, and there seem to be the right
5304 * conditions to do it, or we can lower the last base value
5305 * computed.
5306 */
5307 if ((bfqq->last_serv_time_ns == 0 && bfqd->rq_in_driver == 0) ||
5308 tot_time_ns < bfqq->last_serv_time_ns) {
5309 bfqq->last_serv_time_ns = tot_time_ns;
5310 /*
5311 * Now we certainly have a base value: make sure we
5312 * start trying injection.
5313 */
5314 bfqq->inject_limit = max_t(unsigned int, 1, old_limit);
5315 }
5316
5317 /* update complete, not waiting for any request completion any longer */
5318 bfqd->waited_rq = NULL;
5319}
5320
5321/*
Paolo Valentea7877392018-02-07 22:19:20 +01005322 * Handle either a requeue or a finish for rq. The things to do are
5323 * the same in both cases: all references to rq are to be dropped. In
5324 * particular, rq is considered completed from the point of view of
5325 * the scheduler.
5326 */
5327static void bfq_finish_requeue_request(struct request *rq)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005328{
Paolo Valentea7877392018-02-07 22:19:20 +01005329 struct bfq_queue *bfqq = RQ_BFQQ(rq);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005330 struct bfq_data *bfqd;
5331
Paolo Valentea7877392018-02-07 22:19:20 +01005332 /*
5333 * Requeue and finish hooks are invoked in blk-mq without
5334 * checking whether the involved request is actually still
5335 * referenced in the scheduler. To handle this fact, the
5336 * following two checks make this function exit in case of
5337 * spurious invocations, for which there is nothing to do.
5338 *
5339 * First, check whether rq has nothing to do with an elevator.
5340 */
5341 if (unlikely(!(rq->rq_flags & RQF_ELVPRIV)))
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005342 return;
5343
Paolo Valentea7877392018-02-07 22:19:20 +01005344 /*
5345 * rq either is not associated with any icq, or is an already
5346 * requeued request that has not (yet) been re-inserted into
5347 * a bfq_queue.
5348 */
5349 if (!rq->elv.icq || !bfqq)
5350 return;
5351
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005352 bfqd = bfqq->bfqd;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005353
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005354 if (rq->rq_flags & RQF_STARTED)
5355 bfqg_stats_update_completion(bfqq_group(bfqq),
Omar Sandoval522a7772018-05-09 02:08:53 -07005356 rq->start_time_ns,
5357 rq->io_start_time_ns,
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005358 rq->cmd_flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005359
5360 if (likely(rq->rq_flags & RQF_STARTED)) {
5361 unsigned long flags;
5362
5363 spin_lock_irqsave(&bfqd->lock, flags);
5364
Paolo Valente2341d6622019-03-12 09:59:29 +01005365 if (rq == bfqd->waited_rq)
5366 bfq_update_inject_limit(bfqd, bfqq);
5367
Paolo Valenteaee69d72017-04-19 08:29:02 -06005368 bfq_completed_request(bfqq, bfqd);
Paolo Valentea7877392018-02-07 22:19:20 +01005369 bfq_finish_requeue_request_body(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005370
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005371 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005372 } else {
5373 /*
5374 * Request rq may be still/already in the scheduler,
Paolo Valentea7877392018-02-07 22:19:20 +01005375 * in which case we need to remove it (this should
5376 * never happen in case of requeue). And we cannot
Paolo Valenteaee69d72017-04-19 08:29:02 -06005377 * defer such a check and removal, to avoid
5378 * inconsistencies in the time interval from the end
5379 * of this function to the start of the deferred work.
5380 * This situation seems to occur only in process
5381 * context, as a consequence of a merge. In the
5382 * current version of the code, this implies that the
5383 * lock is held.
5384 */
5385
Luca Miccio614822f2017-11-13 07:34:08 +01005386 if (!RB_EMPTY_NODE(&rq->rb_node)) {
Christoph Hellwig7b9e9362017-06-16 18:15:21 +02005387 bfq_remove_request(rq->q, rq);
Luca Miccio614822f2017-11-13 07:34:08 +01005388 bfqg_stats_update_io_remove(bfqq_group(bfqq),
5389 rq->cmd_flags);
5390 }
Paolo Valentea7877392018-02-07 22:19:20 +01005391 bfq_finish_requeue_request_body(bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005392 }
5393
Paolo Valentea7877392018-02-07 22:19:20 +01005394 /*
5395 * Reset private fields. In case of a requeue, this allows
5396 * this function to correctly do nothing if it is spuriously
5397 * invoked again on this same request (see the check at the
5398 * beginning of the function). Probably, a better general
5399 * design would be to prevent blk-mq from invoking the requeue
5400 * or finish hooks of an elevator, for a request that is not
5401 * referred by that elevator.
5402 *
5403 * Resetting the following fields would break the
5404 * request-insertion logic if rq is re-inserted into a bfq
5405 * internal queue, without a re-preparation. Here we assume
5406 * that re-insertions of requeued requests, without
5407 * re-preparation, can happen only for pass_through or at_head
5408 * requests (which are not re-inserted into bfq internal
5409 * queues).
5410 */
Paolo Valenteaee69d72017-04-19 08:29:02 -06005411 rq->elv.priv[0] = NULL;
5412 rq->elv.priv[1] = NULL;
5413}
5414
5415/*
Arianna Avanzini36eca892017-04-12 18:23:16 +02005416 * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
5417 * was the last process referring to that bfqq.
5418 */
5419static struct bfq_queue *
5420bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
5421{
5422 bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
5423
5424 if (bfqq_process_refs(bfqq) == 1) {
5425 bfqq->pid = current->pid;
5426 bfq_clear_bfqq_coop(bfqq);
5427 bfq_clear_bfqq_split_coop(bfqq);
5428 return bfqq;
5429 }
5430
5431 bic_set_bfqq(bic, NULL, 1);
5432
5433 bfq_put_cooperator(bfqq);
5434
5435 bfq_put_queue(bfqq);
5436 return NULL;
5437}
5438
5439static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,
5440 struct bfq_io_cq *bic,
5441 struct bio *bio,
5442 bool split, bool is_sync,
5443 bool *new_queue)
5444{
5445 struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
5446
5447 if (likely(bfqq && bfqq != &bfqd->oom_bfqq))
5448 return bfqq;
5449
5450 if (new_queue)
5451 *new_queue = true;
5452
5453 if (bfqq)
5454 bfq_put_queue(bfqq);
5455 bfqq = bfq_get_queue(bfqd, bio, is_sync, bic);
5456
5457 bic_set_bfqq(bic, bfqq, is_sync);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005458 if (split && is_sync) {
5459 if ((bic->was_in_burst_list && bfqd->large_burst) ||
5460 bic->saved_in_large_burst)
5461 bfq_mark_bfqq_in_large_burst(bfqq);
5462 else {
5463 bfq_clear_bfqq_in_large_burst(bfqq);
5464 if (bic->was_in_burst_list)
Paolo Valente99fead82017-10-09 13:11:23 +02005465 /*
5466 * If bfqq was in the current
5467 * burst list before being
5468 * merged, then we have to add
5469 * it back. And we do not need
5470 * to increase burst_size, as
5471 * we did not decrement
5472 * burst_size when we removed
5473 * bfqq from the burst list as
5474 * a consequence of a merge
5475 * (see comments in
5476 * bfq_put_queue). In this
5477 * respect, it would be rather
5478 * costly to know whether the
5479 * current burst list is still
5480 * the same burst list from
5481 * which bfqq was removed on
5482 * the merge. To avoid this
5483 * cost, if bfqq was in a
5484 * burst list, then we add
5485 * bfqq to the current burst
5486 * list without any further
5487 * check. This can cause
5488 * inappropriate insertions,
5489 * but rarely enough to not
5490 * harm the detection of large
5491 * bursts significantly.
5492 */
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005493 hlist_add_head(&bfqq->burst_list_node,
5494 &bfqd->burst_list);
5495 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02005496 bfqq->split_time = jiffies;
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005497 }
Arianna Avanzini36eca892017-04-12 18:23:16 +02005498
5499 return bfqq;
5500}
5501
5502/*
Paolo Valente18e5a572018-05-04 19:17:01 +02005503 * Only reset private fields. The actual request preparation will be
5504 * performed by bfq_init_rq, when rq is either inserted or merged. See
5505 * comments on bfq_init_rq for the reason behind this delayed
5506 * preparation.
Paolo Valenteaee69d72017-04-19 08:29:02 -06005507 */
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005508static void bfq_prepare_request(struct request *rq, struct bio *bio)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005509{
Paolo Valente18e5a572018-05-04 19:17:01 +02005510 /*
5511 * Regardless of whether we have an icq attached, we have to
5512 * clear the scheduler pointers, as they might point to
5513 * previously allocated bic/bfqq structs.
5514 */
5515 rq->elv.priv[0] = rq->elv.priv[1] = NULL;
5516}
5517
5518/*
5519 * If needed, init rq, allocate bfq data structures associated with
5520 * rq, and increment reference counters in the destination bfq_queue
5521 * for rq. Return the destination bfq_queue for rq, or NULL is rq is
5522 * not associated with any bfq_queue.
5523 *
5524 * This function is invoked by the functions that perform rq insertion
5525 * or merging. One may have expected the above preparation operations
5526 * to be performed in bfq_prepare_request, and not delayed to when rq
5527 * is inserted or merged. The rationale behind this delayed
5528 * preparation is that, after the prepare_request hook is invoked for
5529 * rq, rq may still be transformed into a request with no icq, i.e., a
5530 * request not associated with any queue. No bfq hook is invoked to
5531 * signal this tranformation. As a consequence, should these
5532 * preparation operations be performed when the prepare_request hook
5533 * is invoked, and should rq be transformed one moment later, bfq
5534 * would end up in an inconsistent state, because it would have
5535 * incremented some queue counters for an rq destined to
5536 * transformation, without any chance to correctly lower these
5537 * counters back. In contrast, no transformation can still happen for
5538 * rq after rq has been inserted or merged. So, it is safe to execute
5539 * these preparation operations when rq is finally inserted or merged.
5540 */
5541static struct bfq_queue *bfq_init_rq(struct request *rq)
5542{
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02005543 struct request_queue *q = rq->q;
Paolo Valente18e5a572018-05-04 19:17:01 +02005544 struct bio *bio = rq->bio;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005545 struct bfq_data *bfqd = q->elevator->elevator_data;
Christoph Hellwig9f210732017-06-16 18:15:24 +02005546 struct bfq_io_cq *bic;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005547 const int is_sync = rq_is_sync(rq);
5548 struct bfq_queue *bfqq;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005549 bool new_queue = false;
Paolo Valente13c931b2017-06-27 12:30:47 -06005550 bool bfqq_already_existing = false, split = false;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005551
Paolo Valente18e5a572018-05-04 19:17:01 +02005552 if (unlikely(!rq->elv.icq))
5553 return NULL;
5554
Jens Axboe72961c42018-04-17 17:08:52 -06005555 /*
Paolo Valente18e5a572018-05-04 19:17:01 +02005556 * Assuming that elv.priv[1] is set only if everything is set
5557 * for this rq. This holds true, because this function is
5558 * invoked only for insertion or merging, and, after such
5559 * events, a request cannot be manipulated any longer before
5560 * being removed from bfq.
Jens Axboe72961c42018-04-17 17:08:52 -06005561 */
Paolo Valente18e5a572018-05-04 19:17:01 +02005562 if (rq->elv.priv[1])
5563 return rq->elv.priv[1];
Jens Axboe72961c42018-04-17 17:08:52 -06005564
Christoph Hellwig9f210732017-06-16 18:15:24 +02005565 bic = icq_to_bic(rq->elv.icq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005566
Colin Ian King8c9ff1a2017-04-20 15:07:18 +01005567 bfq_check_ioprio_change(bic, bio);
5568
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005569 bfq_bic_update_cgroup(bic, bio);
5570
Arianna Avanzini36eca892017-04-12 18:23:16 +02005571 bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync,
5572 &new_queue);
5573
5574 if (likely(!new_queue)) {
5575 /* If the queue was seeky for too long, break it apart. */
5576 if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
5577 bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005578
5579 /* Update bic before losing reference to bfqq */
5580 if (bfq_bfqq_in_large_burst(bfqq))
5581 bic->saved_in_large_burst = true;
5582
Arianna Avanzini36eca892017-04-12 18:23:16 +02005583 bfqq = bfq_split_bfqq(bic, bfqq);
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005584 split = true;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005585
5586 if (!bfqq)
5587 bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio,
5588 true, is_sync,
5589 NULL);
Paolo Valente13c931b2017-06-27 12:30:47 -06005590 else
5591 bfqq_already_existing = true;
Arianna Avanzini36eca892017-04-12 18:23:16 +02005592 }
Paolo Valenteaee69d72017-04-19 08:29:02 -06005593 }
5594
5595 bfqq->allocated++;
5596 bfqq->ref++;
5597 bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d",
5598 rq, bfqq, bfqq->ref);
5599
5600 rq->elv.priv[0] = bic;
5601 rq->elv.priv[1] = bfqq;
5602
Arianna Avanzini36eca892017-04-12 18:23:16 +02005603 /*
5604 * If a bfq_queue has only one process reference, it is owned
5605 * by only this bic: we can then set bfqq->bic = bic. in
5606 * addition, if the queue has also just been split, we have to
5607 * resume its state.
5608 */
5609 if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
5610 bfqq->bic = bic;
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005611 if (split) {
Arianna Avanzini36eca892017-04-12 18:23:16 +02005612 /*
5613 * The queue has just been split from a shared
5614 * queue: restore the idle window and the
5615 * possible weight raising period.
5616 */
Paolo Valente13c931b2017-06-27 12:30:47 -06005617 bfq_bfqq_resume_state(bfqq, bfqd, bic,
5618 bfqq_already_existing);
Arianna Avanzini36eca892017-04-12 18:23:16 +02005619 }
5620 }
5621
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005622 if (unlikely(bfq_bfqq_just_created(bfqq)))
5623 bfq_handle_burst(bfqd, bfqq);
5624
Paolo Valente18e5a572018-05-04 19:17:01 +02005625 return bfqq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005626}
5627
5628static void bfq_idle_slice_timer_body(struct bfq_queue *bfqq)
5629{
5630 struct bfq_data *bfqd = bfqq->bfqd;
5631 enum bfqq_expiration reason;
5632 unsigned long flags;
5633
5634 spin_lock_irqsave(&bfqd->lock, flags);
5635 bfq_clear_bfqq_wait_request(bfqq);
5636
5637 if (bfqq != bfqd->in_service_queue) {
5638 spin_unlock_irqrestore(&bfqd->lock, flags);
5639 return;
5640 }
5641
5642 if (bfq_bfqq_budget_timeout(bfqq))
5643 /*
5644 * Also here the queue can be safely expired
5645 * for budget timeout without wasting
5646 * guarantees
5647 */
5648 reason = BFQQE_BUDGET_TIMEOUT;
5649 else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
5650 /*
5651 * The queue may not be empty upon timer expiration,
5652 * because we may not disable the timer when the
5653 * first request of the in-service queue arrives
5654 * during disk idling.
5655 */
5656 reason = BFQQE_TOO_IDLE;
5657 else
5658 goto schedule_dispatch;
5659
5660 bfq_bfqq_expire(bfqd, bfqq, true, reason);
5661
5662schedule_dispatch:
Paolo Valente6fa3e8d2017-04-12 18:23:21 +02005663 spin_unlock_irqrestore(&bfqd->lock, flags);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005664 bfq_schedule_dispatch(bfqd);
5665}
5666
5667/*
5668 * Handler of the expiration of the timer running if the in-service queue
5669 * is idling inside its time slice.
5670 */
5671static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)
5672{
5673 struct bfq_data *bfqd = container_of(timer, struct bfq_data,
5674 idle_slice_timer);
5675 struct bfq_queue *bfqq = bfqd->in_service_queue;
5676
5677 /*
5678 * Theoretical race here: the in-service queue can be NULL or
5679 * different from the queue that was idling if a new request
5680 * arrives for the current queue and there is a full dispatch
5681 * cycle that changes the in-service queue. This can hardly
5682 * happen, but in the worst case we just expire a queue too
5683 * early.
5684 */
5685 if (bfqq)
5686 bfq_idle_slice_timer_body(bfqq);
5687
5688 return HRTIMER_NORESTART;
5689}
5690
5691static void __bfq_put_async_bfqq(struct bfq_data *bfqd,
5692 struct bfq_queue **bfqq_ptr)
5693{
5694 struct bfq_queue *bfqq = *bfqq_ptr;
5695
5696 bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
5697 if (bfqq) {
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005698 bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
5699
Paolo Valenteaee69d72017-04-19 08:29:02 -06005700 bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
5701 bfqq, bfqq->ref);
5702 bfq_put_queue(bfqq);
5703 *bfqq_ptr = NULL;
5704 }
5705}
5706
5707/*
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005708 * Release all the bfqg references to its async queues. If we are
5709 * deallocating the group these queues may still contain requests, so
5710 * we reparent them to the root cgroup (i.e., the only one that will
5711 * exist for sure until all the requests on a device are gone).
Paolo Valenteaee69d72017-04-19 08:29:02 -06005712 */
Paolo Valenteea25da42017-04-19 08:48:24 -06005713void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005714{
5715 int i, j;
5716
5717 for (i = 0; i < 2; i++)
5718 for (j = 0; j < IOPRIO_BE_NR; j++)
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005719 __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005720
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005721 __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005722}
5723
Jens Axboef0635b82018-05-09 13:27:21 -06005724/*
5725 * See the comments on bfq_limit_depth for the purpose of
Jens Axboe483b7bf2018-05-09 15:26:55 -06005726 * the depths set in the function. Return minimum shallow depth we'll use.
Jens Axboef0635b82018-05-09 13:27:21 -06005727 */
Jens Axboe483b7bf2018-05-09 15:26:55 -06005728static unsigned int bfq_update_depths(struct bfq_data *bfqd,
5729 struct sbitmap_queue *bt)
Jens Axboef0635b82018-05-09 13:27:21 -06005730{
Jens Axboe483b7bf2018-05-09 15:26:55 -06005731 unsigned int i, j, min_shallow = UINT_MAX;
5732
Jens Axboef0635b82018-05-09 13:27:21 -06005733 /*
5734 * In-word depths if no bfq_queue is being weight-raised:
5735 * leaving 25% of tags only for sync reads.
5736 *
5737 * In next formulas, right-shift the value
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005738 * (1U<<bt->sb.shift), instead of computing directly
5739 * (1U<<(bt->sb.shift - something)), to be robust against
5740 * any possible value of bt->sb.shift, without having to
Jens Axboef0635b82018-05-09 13:27:21 -06005741 * limit 'something'.
5742 */
5743 /* no more than 50% of tags for async I/O */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005744 bfqd->word_depths[0][0] = max((1U << bt->sb.shift) >> 1, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005745 /*
5746 * no more than 75% of tags for sync writes (25% extra tags
5747 * w.r.t. async I/O, to prevent async I/O from starving sync
5748 * writes)
5749 */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005750 bfqd->word_depths[0][1] = max(((1U << bt->sb.shift) * 3) >> 2, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005751
5752 /*
5753 * In-word depths in case some bfq_queue is being weight-
5754 * raised: leaving ~63% of tags for sync reads. This is the
5755 * highest percentage for which, in our tests, application
5756 * start-up times didn't suffer from any regression due to tag
5757 * shortage.
5758 */
5759 /* no more than ~18% of tags for async I/O */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005760 bfqd->word_depths[1][0] = max(((1U << bt->sb.shift) * 3) >> 4, 1U);
Jens Axboef0635b82018-05-09 13:27:21 -06005761 /* no more than ~37% of tags for sync writes (~20% extra tags) */
Jens Axboebd7d4ef2018-05-09 15:25:22 -06005762 bfqd->word_depths[1][1] = max(((1U << bt->sb.shift) * 6) >> 4, 1U);
Jens Axboe483b7bf2018-05-09 15:26:55 -06005763
5764 for (i = 0; i < 2; i++)
5765 for (j = 0; j < 2; j++)
5766 min_shallow = min(min_shallow, bfqd->word_depths[i][j]);
5767
5768 return min_shallow;
Jens Axboef0635b82018-05-09 13:27:21 -06005769}
5770
5771static int bfq_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int index)
5772{
5773 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
5774 struct blk_mq_tags *tags = hctx->sched_tags;
Jens Axboe483b7bf2018-05-09 15:26:55 -06005775 unsigned int min_shallow;
Jens Axboef0635b82018-05-09 13:27:21 -06005776
Jens Axboe483b7bf2018-05-09 15:26:55 -06005777 min_shallow = bfq_update_depths(bfqd, &tags->bitmap_tags);
5778 sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, min_shallow);
Jens Axboef0635b82018-05-09 13:27:21 -06005779 return 0;
5780}
5781
Paolo Valenteaee69d72017-04-19 08:29:02 -06005782static void bfq_exit_queue(struct elevator_queue *e)
5783{
5784 struct bfq_data *bfqd = e->elevator_data;
5785 struct bfq_queue *bfqq, *n;
5786
5787 hrtimer_cancel(&bfqd->idle_slice_timer);
5788
5789 spin_lock_irq(&bfqd->lock);
5790 list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005791 bfq_deactivate_bfqq(bfqd, bfqq, false, false);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005792 spin_unlock_irq(&bfqd->lock);
5793
5794 hrtimer_cancel(&bfqd->idle_slice_timer);
5795
Jens Axboe8abef102018-01-09 12:20:51 -07005796#ifdef CONFIG_BFQ_GROUP_IOSCHED
Paolo Valente0d52af52018-01-09 10:27:59 +01005797 /* release oom-queue reference to root group */
5798 bfqg_and_blkg_put(bfqd->root_group);
5799
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005800 blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
5801#else
5802 spin_lock_irq(&bfqd->lock);
5803 bfq_put_async_queues(bfqd, bfqd->root_group);
5804 kfree(bfqd->root_group);
5805 spin_unlock_irq(&bfqd->lock);
5806#endif
5807
Paolo Valenteaee69d72017-04-19 08:29:02 -06005808 kfree(bfqd);
5809}
5810
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005811static void bfq_init_root_group(struct bfq_group *root_group,
5812 struct bfq_data *bfqd)
5813{
5814 int i;
5815
5816#ifdef CONFIG_BFQ_GROUP_IOSCHED
5817 root_group->entity.parent = NULL;
5818 root_group->my_entity = NULL;
5819 root_group->bfqd = bfqd;
5820#endif
Arianna Avanzini36eca892017-04-12 18:23:16 +02005821 root_group->rq_pos_tree = RB_ROOT;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005822 for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
5823 root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
5824 root_group->sched_data.bfq_class_idle_last_service = jiffies;
5825}
5826
Paolo Valenteaee69d72017-04-19 08:29:02 -06005827static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
5828{
5829 struct bfq_data *bfqd;
5830 struct elevator_queue *eq;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005831
5832 eq = elevator_alloc(q, e);
5833 if (!eq)
5834 return -ENOMEM;
5835
5836 bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
5837 if (!bfqd) {
5838 kobject_put(&eq->kobj);
5839 return -ENOMEM;
5840 }
5841 eq->elevator_data = bfqd;
5842
Christoph Hellwig0d945c12018-11-15 12:17:28 -07005843 spin_lock_irq(&q->queue_lock);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005844 q->elevator = eq;
Christoph Hellwig0d945c12018-11-15 12:17:28 -07005845 spin_unlock_irq(&q->queue_lock);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005846
Paolo Valenteaee69d72017-04-19 08:29:02 -06005847 /*
5848 * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
5849 * Grab a permanent reference to it, so that the normal code flow
5850 * will not attempt to free it.
5851 */
5852 bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
5853 bfqd->oom_bfqq.ref++;
5854 bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
5855 bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE;
5856 bfqd->oom_bfqq.entity.new_weight =
5857 bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005858
5859 /* oom_bfqq does not participate to bursts */
5860 bfq_clear_bfqq_just_created(&bfqd->oom_bfqq);
5861
Paolo Valenteaee69d72017-04-19 08:29:02 -06005862 /*
5863 * Trigger weight initialization, according to ioprio, at the
5864 * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
5865 * class won't be changed any more.
5866 */
5867 bfqd->oom_bfqq.entity.prio_changed = 1;
5868
5869 bfqd->queue = q;
5870
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005871 INIT_LIST_HEAD(&bfqd->dispatch);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005872
5873 hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC,
5874 HRTIMER_MODE_REL);
5875 bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
5876
Paolo Valentefb53ac62019-03-12 09:59:28 +01005877 bfqd->queue_weights_tree = RB_ROOT_CACHED;
Paolo Valenteba7aeae2018-12-06 19:18:18 +01005878 bfqd->num_groups_with_pending_reqs = 0;
Arianna Avanzini1de0c4c2017-04-12 18:23:17 +02005879
Paolo Valenteaee69d72017-04-19 08:29:02 -06005880 INIT_LIST_HEAD(&bfqd->active_list);
5881 INIT_LIST_HEAD(&bfqd->idle_list);
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005882 INIT_HLIST_HEAD(&bfqd->burst_list);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005883
5884 bfqd->hw_tag = -1;
5885
5886 bfqd->bfq_max_budget = bfq_default_max_budget;
5887
5888 bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
5889 bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
5890 bfqd->bfq_back_max = bfq_back_max;
5891 bfqd->bfq_back_penalty = bfq_back_penalty;
5892 bfqd->bfq_slice_idle = bfq_slice_idle;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005893 bfqd->bfq_timeout = bfq_timeout;
5894
5895 bfqd->bfq_requests_within_timer = 120;
5896
Arianna Avanzinie1b23242017-04-12 18:23:20 +02005897 bfqd->bfq_large_burst_thresh = 8;
5898 bfqd->bfq_burst_interval = msecs_to_jiffies(180);
5899
Paolo Valente44e44a12017-04-12 18:23:12 +02005900 bfqd->low_latency = true;
5901
5902 /*
5903 * Trade-off between responsiveness and fairness.
5904 */
5905 bfqd->bfq_wr_coeff = 30;
Paolo Valente77b7dce2017-04-12 18:23:13 +02005906 bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
Paolo Valente44e44a12017-04-12 18:23:12 +02005907 bfqd->bfq_wr_max_time = 0;
5908 bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
5909 bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
Paolo Valente77b7dce2017-04-12 18:23:13 +02005910 bfqd->bfq_wr_max_softrt_rate = 7000; /*
5911 * Approximate rate required
5912 * to playback or record a
5913 * high-definition compressed
5914 * video.
5915 */
Paolo Valentecfd69712017-04-12 18:23:15 +02005916 bfqd->wr_busy_queues = 0;
Paolo Valente44e44a12017-04-12 18:23:12 +02005917
5918 /*
Paolo Valentee24f1c22018-05-31 16:45:06 +02005919 * Begin by assuming, optimistically, that the device peak
5920 * rate is equal to 2/3 of the highest reference rate.
Paolo Valente44e44a12017-04-12 18:23:12 +02005921 */
Paolo Valentee24f1c22018-05-31 16:45:06 +02005922 bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] *
5923 ref_wr_duration[blk_queue_nonrot(bfqd->queue)];
5924 bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3;
Paolo Valente44e44a12017-04-12 18:23:12 +02005925
Paolo Valenteaee69d72017-04-19 08:29:02 -06005926 spin_lock_init(&bfqd->lock);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005927
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005928 /*
5929 * The invocation of the next bfq_create_group_hierarchy
5930 * function is the head of a chain of function calls
5931 * (bfq_create_group_hierarchy->blkcg_activate_policy->
5932 * blk_mq_freeze_queue) that may lead to the invocation of the
5933 * has_work hook function. For this reason,
5934 * bfq_create_group_hierarchy is invoked only after all
5935 * scheduler data has been initialized, apart from the fields
5936 * that can be initialized only after invoking
5937 * bfq_create_group_hierarchy. This, in particular, enables
5938 * has_work to correctly return false. Of course, to avoid
5939 * other inconsistencies, the blk-mq stack must then refrain
5940 * from invoking further scheduler hooks before this init
5941 * function is finished.
5942 */
5943 bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node);
5944 if (!bfqd->root_group)
5945 goto out_free;
5946 bfq_init_root_group(bfqd->root_group, bfqd);
5947 bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
5948
Luca Micciob5dc5d42017-10-09 16:27:21 +02005949 wbt_disable_default(q);
Paolo Valenteaee69d72017-04-19 08:29:02 -06005950 return 0;
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02005951
5952out_free:
5953 kfree(bfqd);
5954 kobject_put(&eq->kobj);
5955 return -ENOMEM;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005956}
5957
5958static void bfq_slab_kill(void)
5959{
5960 kmem_cache_destroy(bfq_pool);
5961}
5962
5963static int __init bfq_slab_setup(void)
5964{
5965 bfq_pool = KMEM_CACHE(bfq_queue, 0);
5966 if (!bfq_pool)
5967 return -ENOMEM;
5968 return 0;
5969}
5970
5971static ssize_t bfq_var_show(unsigned int var, char *page)
5972{
5973 return sprintf(page, "%u\n", var);
5974}
5975
Bart Van Assche2f791362017-08-30 11:42:09 -07005976static int bfq_var_store(unsigned long *var, const char *page)
Paolo Valenteaee69d72017-04-19 08:29:02 -06005977{
5978 unsigned long new_val;
5979 int ret = kstrtoul(page, 10, &new_val);
5980
Bart Van Assche2f791362017-08-30 11:42:09 -07005981 if (ret)
5982 return ret;
5983 *var = new_val;
5984 return 0;
Paolo Valenteaee69d72017-04-19 08:29:02 -06005985}
5986
5987#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
5988static ssize_t __FUNC(struct elevator_queue *e, char *page) \
5989{ \
5990 struct bfq_data *bfqd = e->elevator_data; \
5991 u64 __data = __VAR; \
5992 if (__CONV == 1) \
5993 __data = jiffies_to_msecs(__data); \
5994 else if (__CONV == 2) \
5995 __data = div_u64(__data, NSEC_PER_MSEC); \
5996 return bfq_var_show(__data, (page)); \
5997}
5998SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
5999SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
6000SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
6001SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
6002SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
6003SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
6004SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
6005SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
Paolo Valente44e44a12017-04-12 18:23:12 +02006006SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
Paolo Valenteaee69d72017-04-19 08:29:02 -06006007#undef SHOW_FUNCTION
6008
6009#define USEC_SHOW_FUNCTION(__FUNC, __VAR) \
6010static ssize_t __FUNC(struct elevator_queue *e, char *page) \
6011{ \
6012 struct bfq_data *bfqd = e->elevator_data; \
6013 u64 __data = __VAR; \
6014 __data = div_u64(__data, NSEC_PER_USEC); \
6015 return bfq_var_show(__data, (page)); \
6016}
6017USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
6018#undef USEC_SHOW_FUNCTION
6019
6020#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
6021static ssize_t \
6022__FUNC(struct elevator_queue *e, const char *page, size_t count) \
6023{ \
6024 struct bfq_data *bfqd = e->elevator_data; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07006025 unsigned long __data, __min = (MIN), __max = (MAX); \
Bart Van Assche2f791362017-08-30 11:42:09 -07006026 int ret; \
6027 \
6028 ret = bfq_var_store(&__data, (page)); \
6029 if (ret) \
6030 return ret; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07006031 if (__data < __min) \
6032 __data = __min; \
6033 else if (__data > __max) \
6034 __data = __max; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06006035 if (__CONV == 1) \
6036 *(__PTR) = msecs_to_jiffies(__data); \
6037 else if (__CONV == 2) \
6038 *(__PTR) = (u64)__data * NSEC_PER_MSEC; \
6039 else \
6040 *(__PTR) = __data; \
weiping zhang235f8da2017-08-25 01:11:33 +08006041 return count; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06006042}
6043STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
6044 INT_MAX, 2);
6045STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
6046 INT_MAX, 2);
6047STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
6048STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
6049 INT_MAX, 0);
6050STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
6051#undef STORE_FUNCTION
6052
6053#define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \
6054static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\
6055{ \
6056 struct bfq_data *bfqd = e->elevator_data; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07006057 unsigned long __data, __min = (MIN), __max = (MAX); \
Bart Van Assche2f791362017-08-30 11:42:09 -07006058 int ret; \
6059 \
6060 ret = bfq_var_store(&__data, (page)); \
6061 if (ret) \
6062 return ret; \
Bart Van Assche1530486c2017-08-30 11:42:10 -07006063 if (__data < __min) \
6064 __data = __min; \
6065 else if (__data > __max) \
6066 __data = __max; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06006067 *(__PTR) = (u64)__data * NSEC_PER_USEC; \
weiping zhang235f8da2017-08-25 01:11:33 +08006068 return count; \
Paolo Valenteaee69d72017-04-19 08:29:02 -06006069}
6070USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
6071 UINT_MAX);
6072#undef USEC_STORE_FUNCTION
6073
Paolo Valenteaee69d72017-04-19 08:29:02 -06006074static ssize_t bfq_max_budget_store(struct elevator_queue *e,
6075 const char *page, size_t count)
6076{
6077 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07006078 unsigned long __data;
6079 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08006080
Bart Van Assche2f791362017-08-30 11:42:09 -07006081 ret = bfq_var_store(&__data, (page));
6082 if (ret)
6083 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006084
6085 if (__data == 0)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02006086 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06006087 else {
6088 if (__data > INT_MAX)
6089 __data = INT_MAX;
6090 bfqd->bfq_max_budget = __data;
6091 }
6092
6093 bfqd->bfq_user_max_budget = __data;
6094
weiping zhang235f8da2017-08-25 01:11:33 +08006095 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006096}
6097
6098/*
6099 * Leaving this name to preserve name compatibility with cfq
6100 * parameters, but this timeout is used for both sync and async.
6101 */
6102static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
6103 const char *page, size_t count)
6104{
6105 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07006106 unsigned long __data;
6107 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08006108
Bart Van Assche2f791362017-08-30 11:42:09 -07006109 ret = bfq_var_store(&__data, (page));
6110 if (ret)
6111 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006112
6113 if (__data < 1)
6114 __data = 1;
6115 else if (__data > INT_MAX)
6116 __data = INT_MAX;
6117
6118 bfqd->bfq_timeout = msecs_to_jiffies(__data);
6119 if (bfqd->bfq_user_max_budget == 0)
Paolo Valenteab0e43e2017-04-12 18:23:10 +02006120 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
Paolo Valenteaee69d72017-04-19 08:29:02 -06006121
weiping zhang235f8da2017-08-25 01:11:33 +08006122 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006123}
6124
6125static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e,
6126 const char *page, size_t count)
6127{
6128 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07006129 unsigned long __data;
6130 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08006131
Bart Van Assche2f791362017-08-30 11:42:09 -07006132 ret = bfq_var_store(&__data, (page));
6133 if (ret)
6134 return ret;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006135
6136 if (__data > 1)
6137 __data = 1;
6138 if (!bfqd->strict_guarantees && __data == 1
6139 && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC)
6140 bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC;
6141
6142 bfqd->strict_guarantees = __data;
6143
weiping zhang235f8da2017-08-25 01:11:33 +08006144 return count;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006145}
6146
Paolo Valente44e44a12017-04-12 18:23:12 +02006147static ssize_t bfq_low_latency_store(struct elevator_queue *e,
6148 const char *page, size_t count)
6149{
6150 struct bfq_data *bfqd = e->elevator_data;
Bart Van Assche2f791362017-08-30 11:42:09 -07006151 unsigned long __data;
6152 int ret;
weiping zhang235f8da2017-08-25 01:11:33 +08006153
Bart Van Assche2f791362017-08-30 11:42:09 -07006154 ret = bfq_var_store(&__data, (page));
6155 if (ret)
6156 return ret;
Paolo Valente44e44a12017-04-12 18:23:12 +02006157
6158 if (__data > 1)
6159 __data = 1;
6160 if (__data == 0 && bfqd->low_latency != 0)
6161 bfq_end_wr(bfqd);
6162 bfqd->low_latency = __data;
6163
weiping zhang235f8da2017-08-25 01:11:33 +08006164 return count;
Paolo Valente44e44a12017-04-12 18:23:12 +02006165}
6166
Paolo Valenteaee69d72017-04-19 08:29:02 -06006167#define BFQ_ATTR(name) \
6168 __ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store)
6169
6170static struct elv_fs_entry bfq_attrs[] = {
6171 BFQ_ATTR(fifo_expire_sync),
6172 BFQ_ATTR(fifo_expire_async),
6173 BFQ_ATTR(back_seek_max),
6174 BFQ_ATTR(back_seek_penalty),
6175 BFQ_ATTR(slice_idle),
6176 BFQ_ATTR(slice_idle_us),
6177 BFQ_ATTR(max_budget),
6178 BFQ_ATTR(timeout_sync),
6179 BFQ_ATTR(strict_guarantees),
Paolo Valente44e44a12017-04-12 18:23:12 +02006180 BFQ_ATTR(low_latency),
Paolo Valenteaee69d72017-04-19 08:29:02 -06006181 __ATTR_NULL
6182};
6183
6184static struct elevator_type iosched_bfq_mq = {
Jens Axboef9cd4bf2018-11-01 16:41:41 -06006185 .ops = {
Paolo Valentea52a69e2018-01-13 12:05:17 +01006186 .limit_depth = bfq_limit_depth,
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +02006187 .prepare_request = bfq_prepare_request,
Paolo Valentea7877392018-02-07 22:19:20 +01006188 .requeue_request = bfq_finish_requeue_request,
6189 .finish_request = bfq_finish_requeue_request,
Paolo Valenteaee69d72017-04-19 08:29:02 -06006190 .exit_icq = bfq_exit_icq,
6191 .insert_requests = bfq_insert_requests,
6192 .dispatch_request = bfq_dispatch_request,
6193 .next_request = elv_rb_latter_request,
6194 .former_request = elv_rb_former_request,
6195 .allow_merge = bfq_allow_bio_merge,
6196 .bio_merge = bfq_bio_merge,
6197 .request_merge = bfq_request_merge,
6198 .requests_merged = bfq_requests_merged,
6199 .request_merged = bfq_request_merged,
6200 .has_work = bfq_has_work,
Jens Axboef0635b82018-05-09 13:27:21 -06006201 .init_hctx = bfq_init_hctx,
Paolo Valenteaee69d72017-04-19 08:29:02 -06006202 .init_sched = bfq_init_queue,
6203 .exit_sched = bfq_exit_queue,
6204 },
6205
Paolo Valenteaee69d72017-04-19 08:29:02 -06006206 .icq_size = sizeof(struct bfq_io_cq),
6207 .icq_align = __alignof__(struct bfq_io_cq),
6208 .elevator_attrs = bfq_attrs,
6209 .elevator_name = "bfq",
6210 .elevator_owner = THIS_MODULE,
6211};
Ben Hutchings26b4cf22017-08-13 18:02:19 +01006212MODULE_ALIAS("bfq-iosched");
Paolo Valenteaee69d72017-04-19 08:29:02 -06006213
6214static int __init bfq_init(void)
6215{
6216 int ret;
6217
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02006218#ifdef CONFIG_BFQ_GROUP_IOSCHED
6219 ret = blkcg_policy_register(&blkcg_policy_bfq);
6220 if (ret)
6221 return ret;
6222#endif
6223
Paolo Valenteaee69d72017-04-19 08:29:02 -06006224 ret = -ENOMEM;
6225 if (bfq_slab_setup())
6226 goto err_pol_unreg;
6227
Paolo Valente44e44a12017-04-12 18:23:12 +02006228 /*
6229 * Times to load large popular applications for the typical
6230 * systems installed on the reference devices (see the
Paolo Valentee24f1c22018-05-31 16:45:06 +02006231 * comments before the definition of the next
6232 * array). Actually, we use slightly lower values, as the
Paolo Valente44e44a12017-04-12 18:23:12 +02006233 * estimated peak rate tends to be smaller than the actual
6234 * peak rate. The reason for this last fact is that estimates
6235 * are computed over much shorter time intervals than the long
6236 * intervals typically used for benchmarking. Why? First, to
6237 * adapt more quickly to variations. Second, because an I/O
6238 * scheduler cannot rely on a peak-rate-evaluation workload to
6239 * be run for a long time.
6240 */
Paolo Valentee24f1c22018-05-31 16:45:06 +02006241 ref_wr_duration[0] = msecs_to_jiffies(7000); /* actually 8 sec */
6242 ref_wr_duration[1] = msecs_to_jiffies(2500); /* actually 3 sec */
Paolo Valente44e44a12017-04-12 18:23:12 +02006243
Paolo Valenteaee69d72017-04-19 08:29:02 -06006244 ret = elv_register(&iosched_bfq_mq);
6245 if (ret)
weiping zhang37dcd652017-08-19 00:37:20 +08006246 goto slab_kill;
Paolo Valenteaee69d72017-04-19 08:29:02 -06006247
6248 return 0;
6249
weiping zhang37dcd652017-08-19 00:37:20 +08006250slab_kill:
6251 bfq_slab_kill();
Paolo Valenteaee69d72017-04-19 08:29:02 -06006252err_pol_unreg:
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02006253#ifdef CONFIG_BFQ_GROUP_IOSCHED
6254 blkcg_policy_unregister(&blkcg_policy_bfq);
6255#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06006256 return ret;
6257}
6258
6259static void __exit bfq_exit(void)
6260{
6261 elv_unregister(&iosched_bfq_mq);
Arianna Avanzinie21b7a02017-04-12 18:23:08 +02006262#ifdef CONFIG_BFQ_GROUP_IOSCHED
6263 blkcg_policy_unregister(&blkcg_policy_bfq);
6264#endif
Paolo Valenteaee69d72017-04-19 08:29:02 -06006265 bfq_slab_kill();
6266}
6267
6268module_init(bfq_init);
6269module_exit(bfq_exit);
6270
6271MODULE_AUTHOR("Paolo Valente");
6272MODULE_LICENSE("GPL");
6273MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler");