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