Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Budget Fair Queueing (BFQ) I/O scheduler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Based on ideas and code from CFQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *		      Paolo Valente <paolo.valente@unimore.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *                    Arianna Avanzini <avanzini@google.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * Copyright (C) 2017 Paolo Valente <paolo.valente@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * BFQ is a proportional-share I/O scheduler, with some extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * low-latency capabilities. BFQ also supports full hierarchical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * scheduling through cgroups. Next paragraphs provide an introduction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * on BFQ inner workings. Details on BFQ benefits, usage and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * limitations can be found in Documentation/block/bfq-iosched.rst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * BFQ is a proportional-share storage-I/O scheduling algorithm based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * on the slice-by-slice service scheme of CFQ. But BFQ assigns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * budgets, measured in number of sectors, to processes instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * time slices. The device is not granted to the in-service process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * for a given time slice, but until it has exhausted its assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * budget. This change from the time to the service domain enables BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * to distribute the device throughput among processes as desired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * without any distortion due to throughput fluctuations, or to device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * internal queueing. BFQ uses an ad hoc internal scheduler, called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * B-WF2Q+, to schedule processes according to their budgets. More
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * precisely, BFQ schedules queues associated with processes. Each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * process/queue is assigned a user-configurable weight, and B-WF2Q+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * guarantees that each queue receives a fraction of the throughput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * proportional to its weight. Thanks to the accurate policy of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * processes issuing sequential requests (to boost the throughput),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  * and yet guarantee a low latency to interactive and soft real-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * In particular, to provide these low-latency guarantees, BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * explicitly privileges the I/O of two classes of time-sensitive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * applications: interactive and soft real-time. In more detail, BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  * behaves this way if the low_latency parameter is set (default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45)  * configuration). This feature enables BFQ to provide applications in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46)  * these classes with a very low latency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * To implement this feature, BFQ constantly tries to detect whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * the I/O requests in a bfq_queue come from an interactive or a soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  * real-time application. For brevity, in these cases, the queue is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  * said to be interactive or soft real-time. In both cases, BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52)  * privileges the service of the queue, over that of non-interactive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53)  * and non-soft-real-time queues. This privileging is performed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * mainly, by raising the weight of the queue. So, for brevity, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  * call just weight-raising periods the time periods during which a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56)  * queue is privileged, because deemed interactive or soft real-time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * The detection of soft real-time queues/applications is described in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * detail in the comments on the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * bfq_bfqq_softrt_next_start. On the other hand, the detection of an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  * interactive queue works as follows: a queue is deemed interactive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62)  * if it is constantly non empty only for a limited time interval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * after which it does become empty. The queue may be deemed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * interactive again (for a limited time), if it restarts being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * constantly non empty, provided that this happens only after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * queue has remained empty for a given minimum idle time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68)  * By default, BFQ computes automatically the above maximum time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * interval, i.e., the time interval after which a constantly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * non-empty queue stops being deemed interactive. Since a queue is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * weight-raised while it is deemed interactive, this maximum time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * interval happens to coincide with the (maximum) duration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * weight-raising for interactive queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * Finally, BFQ also features additional heuristics for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * preserving both a low latency and a high throughput on NCQ-capable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * rotational or flash-based devices, and to get the job done quickly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * for applications consisting in many I/O-bound processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * NOTE: if the main or only goal, with a given device, is to achieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * the maximum-possible throughput at all times, then do switch off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * all low-latency heuristics for that device, by setting low_latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * BFQ is described in [1], where also a reference to the initial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * more theoretical paper on BFQ can be found. The interested reader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * can find in the latter paper full details on the main algorithm, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  * well as formulas of the guarantees and formal proofs of all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * properties.  With respect to the version of BFQ presented in these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * papers, this implementation adds a few more heuristics, such as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * ones that guarantee a low latency to interactive and soft real-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * applications, and a hierarchical extension based on H-WF2Q+.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * B-WF2Q+ is based on WF2Q+, which is described in [2], together with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * with O(log N) complexity derives from the one introduced with EEVDF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * in [3].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  *     Scheduler", Proceedings of the First Workshop on Mobile System
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  *     Technologies (MST-2015), May 2015.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102)  *     http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104)  * [2] Jon C.R. Bennett and H. Zhang, "Hierarchical Packet Fair Queueing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105)  *     Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106)  *     Oct 1997.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  *     First: A Flexible and Accurate Mechanism for Proportional Share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  *     Resource Allocation", technical report.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114)  * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #include <linux/cgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #include <linux/elevator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #include <linux/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #include <linux/ioprio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) #include <linux/sbitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #include "blk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #include "blk-mq.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #include "blk-mq-tag.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) #include "blk-mq-sched.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) #include "bfq-iosched.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #include "blk-wbt.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) #define BFQ_BFQQ_FNS(name)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) void bfq_mark_bfqq_##name(struct bfq_queue *bfqq)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	__set_bit(BFQQF_##name, &(bfqq)->flags);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) void bfq_clear_bfqq_##name(struct bfq_queue *bfqq)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	__clear_bit(BFQQF_##name, &(bfqq)->flags);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) }									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) int bfq_bfqq_##name(const struct bfq_queue *bfqq)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	return test_bit(BFQQF_##name, &(bfqq)->flags);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) BFQ_BFQQ_FNS(just_created);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) BFQ_BFQQ_FNS(busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) BFQ_BFQQ_FNS(wait_request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) BFQ_BFQQ_FNS(non_blocking_wait_rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) BFQ_BFQQ_FNS(fifo_expire);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) BFQ_BFQQ_FNS(has_short_ttime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) BFQ_BFQQ_FNS(sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) BFQ_BFQQ_FNS(IO_bound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) BFQ_BFQQ_FNS(in_large_burst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) BFQ_BFQQ_FNS(coop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) BFQ_BFQQ_FNS(split_coop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) BFQ_BFQQ_FNS(softrt_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) BFQ_BFQQ_FNS(has_waker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) #undef BFQ_BFQQ_FNS						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) /* Expiration time of sync (0) and async (1) requests, in ns. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static const u64 bfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) /* Maximum backwards seek (magic number lifted from CFQ), in KiB. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) static const int bfq_back_max = 16 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) /* Penalty of a backwards seek, in number of sectors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) static const int bfq_back_penalty = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) /* Idling period duration, in ns. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static u64 bfq_slice_idle = NSEC_PER_SEC / 125;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) /* Minimum number of assigned budgets for which stats are safe to compute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) static const int bfq_stats_min_budgets = 194;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /* Default maximum budget values, in sectors and number of requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) static const int bfq_default_max_budget = 16 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  * When a sync request is dispatched, the queue that contains that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  * request, and all the ancestor entities of that queue, are charged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185)  * with the number of sectors of the request. In contrast, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186)  * request is async, then the queue and its ancestor entities are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187)  * charged with the number of sectors of the request, multiplied by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188)  * the factor below. This throttles the bandwidth for async I/O,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189)  * w.r.t. to sync I/O, and it is done to counter the tendency of async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190)  * writes to steal I/O throughput to reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192)  * The current value of this parameter is the result of a tuning with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193)  * several hardware and software configurations. We tried to find the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194)  * lowest value for which writes do not cause noticeable problems to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  * reads. In fact, the lower this parameter, the stabler I/O control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  * in the following respect.  The lower this parameter is, the less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  * the bandwidth enjoyed by a group decreases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * - when the group does writes, w.r.t. to when it does reads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * - when other groups do reads, w.r.t. to when they do writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) static const int bfq_async_charge_factor = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) /* Default timeout values, in jiffies, approximating CFQ defaults. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) const int bfq_timeout = HZ / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * Time limit for merging (see comments in bfq_setup_cooperator). Set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  * to the slowest value that, in our tests, proved to be effective in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  * removing false positives, while not causing true positives to miss
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * queue merging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * As can be deduced from the low time limit below, queue merging, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * successful, happens at the very beginning of the I/O of the involved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * cooperating processes, as a consequence of the arrival of the very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * first requests from each cooperator.  After that, there is very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  * little chance to find cooperators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) static const unsigned long bfq_merge_time_limit = HZ/10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) static struct kmem_cache *bfq_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) /* Below this threshold (in ns), we consider thinktime immediate. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) #define BFQ_MIN_TT		(2 * NSEC_PER_MSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) /* hw_tag detection: parallel requests threshold and min samples needed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) #define BFQ_HW_QUEUE_THRESHOLD	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) #define BFQ_HW_QUEUE_SAMPLES	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) #define BFQQ_SEEK_THR		(sector_t)(8 * 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #define BFQQ_SECT_THR_NONROT	(sector_t)(2 * 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) #define BFQ_RQ_SEEKY(bfqd, last_pos, rq) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	(get_sdist(last_pos, rq) >			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	 BFQQ_SEEK_THR &&				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	 (!blk_queue_nonrot(bfqd->queue) ||		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	  blk_rq_sectors(rq) < BFQQ_SECT_THR_NONROT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) #define BFQQ_CLOSE_THR		(sector_t)(8 * 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) #define BFQQ_SEEKY(bfqq)	(hweight32(bfqq->seek_history) > 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239)  * Sync random I/O is likely to be confused with soft real-time I/O,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240)  * because it is characterized by limited throughput and apparently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241)  * isochronous arrival pattern. To avoid false positives, queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242)  * containing only random (seeky) I/O are prevented from being tagged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243)  * as soft real-time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) #define BFQQ_TOTALLY_SEEKY(bfqq)	(bfqq->seek_history == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) /* Min number of samples required to perform peak-rate update */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) #define BFQ_RATE_MIN_SAMPLES	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) /* Min observation time interval required to perform a peak-rate update (ns) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) #define BFQ_RATE_MIN_INTERVAL	(300*NSEC_PER_MSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) /* Target observation time interval for a peak-rate update (ns) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) #define BFQ_RATE_REF_INTERVAL	NSEC_PER_SEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * Shift used for peak-rate fixed precision calculations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * With
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  * - the current shift: 16 positions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * - the current type used to store rate: u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  * - the current unit of measure for rate: [sectors/usec], or, more precisely,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260)  *   [(sectors/usec) / 2^BFQ_RATE_SHIFT] to take into account the shift,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261)  * the range of rates that can be stored is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262)  * [1 / 2^BFQ_RATE_SHIFT, 2^(32 - BFQ_RATE_SHIFT)] sectors/usec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263)  * [1 / 2^16, 2^16] sectors/usec = [15e-6, 65536] sectors/usec =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264)  * [15, 65G] sectors/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * Which, assuming a sector size of 512B, corresponds to a range of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * [7.5K, 33T] B/sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) #define BFQ_RATE_SHIFT		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * When configured for computing the duration of the weight-raising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * for interactive queues automatically (see the comments at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * beginning of this file), BFQ does it using the following formula:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  * duration = (ref_rate / r) * ref_wr_duration,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  * where r is the peak rate of the device, and ref_rate and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * ref_wr_duration are two reference parameters.  In particular,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  * ref_rate is the peak rate of the reference storage device (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * below), and ref_wr_duration is about the maximum time needed, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  * BFQ and while reading two files in parallel, to load typical large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * applications on the reference device (see the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  * max_service_from_wr below, for more details on how ref_wr_duration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282)  * is obtained).  In practice, the slower/faster the device at hand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283)  * is, the more/less it takes to load applications with respect to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284)  * reference device.  Accordingly, the longer/shorter BFQ grants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285)  * weight raising to interactive applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  * BFQ uses two different reference pairs (ref_rate, ref_wr_duration),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  * depending on whether the device is rotational or non-rotational.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290)  * In the following definitions, ref_rate[0] and ref_wr_duration[0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291)  * are the reference values for a rotational device, whereas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * ref_rate[1] and ref_wr_duration[1] are the reference values for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * non-rotational device. The reference rates are not the actual peak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  * rates of the devices used as a reference, but slightly lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295)  * values. The reason for using slightly lower values is that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296)  * peak-rate estimator tends to yield slightly lower values than the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297)  * actual peak rate (it can yield the actual peak rate only if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298)  * is only one process doing I/O, and the process does sequential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299)  * I/O).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  * The reference peak rates are measured in sectors/usec, left-shifted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302)  * by BFQ_RATE_SHIFT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) static int ref_rate[2] = {14000, 33000};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * To improve readability, a conversion function is used to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  * the following array, which entails that the array can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  * initialized only in a function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) static int ref_wr_duration[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  * BFQ uses the above-detailed, time-based weight-raising mechanism to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  * privilege interactive tasks. This mechanism is vulnerable to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * following false positives: I/O-bound applications that will go on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  * doing I/O for much longer than the duration of weight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  * raising. These applications have basically no benefit from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318)  * weight-raised at the beginning of their I/O. On the opposite end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319)  * while being weight-raised, these applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320)  * a) unjustly steal throughput to applications that may actually need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321)  * low latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322)  * b) make BFQ uselessly perform device idling; device idling results
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323)  * in loss of device throughput with most flash-based storage, and may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324)  * increase latencies when used purposelessly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326)  * BFQ tries to reduce these problems, by adopting the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327)  * countermeasure. To introduce this countermeasure, we need first to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328)  * finish explaining how the duration of weight-raising for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329)  * interactive tasks is computed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331)  * For a bfq_queue deemed as interactive, the duration of weight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332)  * raising is dynamically adjusted, as a function of the estimated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333)  * peak rate of the device, so as to be equal to the time needed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334)  * execute the 'largest' interactive task we benchmarked so far. By
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335)  * largest task, we mean the task for which each involved process has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * to do more I/O than for any of the other tasks we benchmarked. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * reference interactive task is the start-up of LibreOffice Writer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * and in this task each process/bfq_queue needs to have at most ~110K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * sectors transferred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * This last piece of information enables BFQ to reduce the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * duration of weight-raising for at least one class of I/O-bound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * applications: those doing sequential or quasi-sequential I/O. An
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  * example is file copy. In fact, once started, the main I/O-bound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345)  * processes of these applications usually consume the above 110K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346)  * sectors in much less time than the processes of an application that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347)  * is starting, because these I/O-bound processes will greedily devote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348)  * almost all their CPU cycles only to their target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349)  * throughput-friendly I/O operations. This is even more true if BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350)  * happens to be underestimating the device peak rate, and thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351)  * overestimating the duration of weight raising. But, according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352)  * our measurements, once transferred 110K sectors, these processes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * have no right to be weight-raised any longer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  * Basing on the last consideration, BFQ ends weight-raising for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356)  * bfq_queue if the latter happens to have received an amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357)  * service at least equal to the following constant. The constant is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358)  * set to slightly more than 110K, to have a minimum safety margin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360)  * This early ending of weight-raising reduces the amount of time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361)  * during which interactive false positives cause the two problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  * described at the beginning of these comments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) static const unsigned long max_service_from_wr = 120000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) #define RQ_BIC(rq)		icq_to_bic((rq)->elv.priv[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) #define RQ_BFQQ(rq)		((rq)->elv.priv[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	return bic->bfqq[is_sync];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	bic->bfqq[is_sync] = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	return bic->icq.q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385)  * icq_to_bic - convert iocontext queue structure to bfq_io_cq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386)  * @icq: the iocontext queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) static struct bfq_io_cq *icq_to_bic(struct io_cq *icq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	/* bic->icq is the first member, %NULL will convert to %NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	return container_of(icq, struct bfq_io_cq, icq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395)  * bfq_bic_lookup - search into @ioc a bic associated to @bfqd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396)  * @bfqd: the lookup key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397)  * @ioc: the io_context of the process doing I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * @q: the request queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) static struct bfq_io_cq *bfq_bic_lookup(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 					struct io_context *ioc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 					struct request_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	if (ioc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		struct bfq_io_cq *icq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		spin_lock_irqsave(&q->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		icq = icq_to_bic(ioc_lookup_icq(ioc, q));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		spin_unlock_irqrestore(&q->queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		return icq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * Scheduler run of queue, if there are requests pending and no one in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  * driver that will restart queueing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) void bfq_schedule_dispatch(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (bfqd->queued != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		bfq_log(bfqd, "schedule dispatch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		blk_mq_run_hw_queues(bfqd->queue, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) #define bfq_class_idle(bfqq)	((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) #define bfq_sample_valid(samples)	((samples) > 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * Lifted from AS - choose which of rq1 and rq2 that is best served now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * We choose the request that is closer to the head right now.  Distance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  * behind the head is penalized and only allowed to a certain extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) static struct request *bfq_choose_req(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				      struct request *rq1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 				      struct request *rq2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 				      sector_t last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	sector_t s1, s2, d1 = 0, d2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	unsigned long back_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) #define BFQ_RQ1_WRAP	0x01 /* request 1 wraps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) #define BFQ_RQ2_WRAP	0x02 /* request 2 wraps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	unsigned int wrap = 0; /* bit mask: requests behind the disk head? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	if (!rq1 || rq1 == rq2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (!rq2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (rq_is_sync(rq1) && !rq_is_sync(rq2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	else if (rq_is_sync(rq2) && !rq_is_sync(rq1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	s1 = blk_rq_pos(rq1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	s2 = blk_rq_pos(rq2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	 * By definition, 1KiB is 2 sectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	back_max = bfqd->bfq_back_max * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	 * Strict one way elevator _except_ in the case where we allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	 * short backward seeks which are biased as twice the cost of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	 * similar forward seek.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (s1 >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		d1 = s1 - last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	else if (s1 + back_max >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		d1 = (last - s1) * bfqd->bfq_back_penalty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		wrap |= BFQ_RQ1_WRAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	if (s2 >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		d2 = s2 - last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	else if (s2 + back_max >= last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		d2 = (last - s2) * bfqd->bfq_back_penalty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 		wrap |= BFQ_RQ2_WRAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	/* Found required data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	 * By doing switch() on the bit mask "wrap" we avoid having to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	 * check two variables for all permutations: --> faster!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	switch (wrap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	case 0: /* common case for CFQ: rq1 and rq2 not wrapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		if (d1 < d2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		else if (d2 < d1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		if (s1 >= s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	case BFQ_RQ2_WRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	case BFQ_RQ1_WRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	case BFQ_RQ1_WRAP|BFQ_RQ2_WRAP: /* both rqs wrapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		 * Since both rqs are wrapped,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		 * start with the one that's further behind head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		 * (--> only *one* back seek required),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		 * since back seek takes more time than forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		if (s1 <= s2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			return rq1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			return rq2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  * Async I/O can easily starve sync I/O (both sync reads and sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  * writes), by consuming all tags. Similarly, storms of sync writes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  * such as those that sync(2) may trigger, can starve sync reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  * Limit depths of async I/O and sync writes so as to counter both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  * problems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static void bfq_limit_depth(unsigned int op, struct blk_mq_alloc_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	struct bfq_data *bfqd = data->q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (op_is_sync(op) && !op_is_write(op))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	data->shallow_depth =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		bfqd->word_depths[!!bfqd->wr_busy_queues][op_is_sync(op)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	bfq_log(bfqd, "[%s] wr_busy %d sync %d depth %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			__func__, bfqd->wr_busy_queues, op_is_sync(op),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 			data->shallow_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) static struct bfq_queue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		     sector_t sector, struct rb_node **ret_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		     struct rb_node ***rb_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	struct rb_node **p, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	struct bfq_queue *bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	p = &root->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		struct rb_node **n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		bfqq = rb_entry(parent, struct bfq_queue, pos_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		 * Sort strictly based on sector. Smallest to the left,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		 * largest to the right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if (sector > blk_rq_pos(bfqq->next_rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			n = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		else if (sector < blk_rq_pos(bfqq->next_rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 			n = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		p = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	*ret_parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	if (rb_link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		*rb_link = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		(unsigned long long)sector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		bfqq ? bfqq->pid : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static bool bfq_too_late_for_merging(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	return bfqq->service_from_backlogged > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		time_is_before_jiffies(bfqq->first_IO_time +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 				       bfq_merge_time_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * The following function is not marked as __cold because it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * actually cold, but for the same performance goal described in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * comments on the likely() at the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  * bfq_setup_cooperator(). Unexpectedly, to reach an even lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603)  * execution time for the case where this function is not invoked, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * had to add an unlikely() in each involved if().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) void __cold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct rb_node **p, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	struct bfq_queue *__bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (bfqq->pos_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		rb_erase(&bfqq->pos_node, bfqq->pos_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		bfqq->pos_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	/* oom_bfqq does not participate in queue merging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if (bfqq == &bfqd->oom_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	 * bfqq cannot be merged any longer (see comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	 * bfq_setup_cooperator): no point in adding bfqq into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	 * position tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	if (bfq_too_late_for_merging(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (bfq_class_idle(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	if (!bfqq->next_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	__bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			blk_rq_pos(bfqq->next_rq), &parent, &p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (!__bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		rb_link_node(&bfqq->pos_node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		rb_insert_color(&bfqq->pos_node, bfqq->pos_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		bfqq->pos_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645)  * The following function returns false either if every active queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * must receive the same share of the throughput (symmetric scenario),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  * or, as a special case, if bfqq must receive a share of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * throughput lower than or equal to the share that every other active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  * queue must receive.  If bfqq does sync I/O, then these are the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  * two cases where bfqq happens to be guaranteed its share of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651)  * throughput even if I/O dispatching is not plugged when bfqq remains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652)  * temporarily empty (for more details, see the comments in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653)  * function bfq_better_to_idle()). For this reason, the return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654)  * of this function is used to check whether I/O-dispatch plugging can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655)  * be avoided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657)  * The above first case (symmetric scenario) occurs when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658)  * 1) all active queues have the same weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659)  * 2) all active queues belong to the same I/O-priority class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660)  * 3) all active groups at the same level in the groups tree have the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661)  *    weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * 4) all active groups at the same level in the groups tree have the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  *    number of children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * Unfortunately, keeping the necessary state for evaluating exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  * the last two symmetry sub-conditions above would be quite complex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * and time consuming. Therefore this function evaluates, instead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  * only the following stronger three sub-conditions, for which it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669)  * much easier to maintain the needed state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670)  * 1) all active queues have the same weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671)  * 2) all active queues belong to the same I/O-priority class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672)  * 3) there are no active groups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673)  * In particular, the last condition is always true if hierarchical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674)  * support or the cgroups interface are not enabled, thus no state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675)  * needs to be maintained in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) static bool bfq_asymmetric_scenario(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 				   struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	bool smallest_weight = bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		bfqq->weight_counter &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		bfqq->weight_counter ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			rb_first_cached(&bfqd->queue_weights_tree),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			struct bfq_weight_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			weights_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	 * For queue weights to differ, queue_weights_tree must contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 * at least two nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	bool varied_queue_weights = !smallest_weight &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		!RB_EMPTY_ROOT(&bfqd->queue_weights_tree.rb_root) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		(bfqd->queue_weights_tree.rb_root.rb_node->rb_left ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 bfqd->queue_weights_tree.rb_root.rb_node->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	bool multiple_classes_busy =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		(bfqd->busy_queues[0] && bfqd->busy_queues[1]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		(bfqd->busy_queues[0] && bfqd->busy_queues[2]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		(bfqd->busy_queues[1] && bfqd->busy_queues[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	return varied_queue_weights || multiple_classes_busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	       || bfqd->num_groups_with_pending_reqs > 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710)  * If the weight-counter tree passed as input contains no counter for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711)  * the weight of the input queue, then add that counter; otherwise just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712)  * increment the existing counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714)  * Note that weight-counter trees contain few nodes in mostly symmetric
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * scenarios. For example, if all queues have the same weight, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  * weight-counter tree for the queues may contain at most one node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717)  * This holds even if low_latency is on, because weight-raised queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718)  * are not inserted in the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719)  * In most scenarios, the rate at which nodes are created/destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720)  * should be low too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			  struct rb_root_cached *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	bool leftmost = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	 * Do not insert if the queue is already associated with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	 * counter, which happens if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	 *   1) a request arrival has caused the queue to become both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	 *      non-weight-raised, and hence change its weight, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	 *      backlogged; in this respect, each of the two events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	 *      causes an invocation of this function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	 *   2) this is the invocation of this function caused by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	 *      second event. This second invocation is actually useless,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	 *      and we handle this fact by exiting immediately. More
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	 *      efficient or clearer solutions might possibly be adopted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (bfqq->weight_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	while (*new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		struct bfq_weight_counter *__counter = container_of(*new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 						struct bfq_weight_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 						weights_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		parent = *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		if (entity->weight == __counter->weight) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			bfqq->weight_counter = __counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			goto inc_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		if (entity->weight < __counter->weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 			new = &((*new)->rb_left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			new = &((*new)->rb_right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			leftmost = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 				       GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	 * In the unlucky event of an allocation failure, we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	 * exit. This will cause the weight of queue to not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	 * considered in bfq_asymmetric_scenario, which, in its turn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	 * causes the scenario to be deemed wrongly symmetric in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	 * bfqq's weight would have been the only weight making the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	 * scenario asymmetric.  On the bright side, no unbalance will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	 * however occur when bfqq becomes inactive again (the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	 * invocation of this function is triggered by an activation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 * of queue).  In fact, bfq_weights_tree_remove does nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 * if !bfqq->weight_counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	if (unlikely(!bfqq->weight_counter))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	bfqq->weight_counter->weight = entity->weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	rb_link_node(&bfqq->weight_counter->weights_node, parent, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	rb_insert_color_cached(&bfqq->weight_counter->weights_node, root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 				leftmost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) inc_counter:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	bfqq->weight_counter->num_active++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	bfqq->ref++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791)  * Decrement the weight counter associated with the queue, and, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792)  * counter reaches 0, remove the counter from the tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793)  * See the comments to the function bfq_weights_tree_add() for considerations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794)  * about overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) void __bfq_weights_tree_remove(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 			       struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			       struct rb_root_cached *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (!bfqq->weight_counter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	bfqq->weight_counter->num_active--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (bfqq->weight_counter->num_active > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		goto reset_entity_pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	rb_erase_cached(&bfqq->weight_counter->weights_node, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	kfree(bfqq->weight_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) reset_entity_pointer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	bfqq->weight_counter = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816)  * Invoke __bfq_weights_tree_remove on bfqq and decrement the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * of active groups for each queue's inactive parent entity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) void bfq_weights_tree_remove(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			     struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct bfq_entity *entity = bfqq->entity.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	for_each_entity(entity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		struct bfq_sched_data *sd = entity->my_sched_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		if (sd->next_in_service || sd->in_service_entity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			 * entity is still active, because either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			 * next_in_service or in_service_entity is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			 * NULL (see the comments on the definition of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			 * next_in_service for details on why
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			 * in_service_entity must be checked too).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			 * As a consequence, its parent entities are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			 * active as well, and thus this loop must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 			 * stop here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		 * The decrement of num_groups_with_pending_reqs is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		 * not performed immediately upon the deactivation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		 * entity, but it is delayed to when it also happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		 * that the first leaf descendant bfqq of entity gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		 * all its pending requests completed. The following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		 * instructions perform this delayed decrement, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		 * needed. See the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		 * num_groups_with_pending_reqs for details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		if (entity->in_groups_with_pending_reqs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			entity->in_groups_with_pending_reqs = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			bfqd->num_groups_with_pending_reqs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	 * Next function is invoked last, because it causes bfqq to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	 * freed if the following holds: bfqq is not in service and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	 * has no dispatched request. DO NOT use bfqq after the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	 * function invocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	__bfq_weights_tree_remove(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 				  &bfqd->queue_weights_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869)  * Return expired entry, or NULL to just start from scratch in rbtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) static struct request *bfq_check_fifo(struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 				      struct request *last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (bfq_bfqq_fifo_expire(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	bfq_mark_bfqq_fifo_expire(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	rq = rq_entry_fifo(bfqq->fifo.next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	if (rq == last || ktime_get_ns() < rq->fifo_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	return rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) static struct request *bfq_find_next_rq(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 					struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 					struct request *last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	struct rb_node *rbnext = rb_next(&last->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	struct rb_node *rbprev = rb_prev(&last->rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	struct request *next, *prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/* Follow expired path, else get first next available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	next = bfq_check_fifo(bfqq, last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	if (next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		return next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	if (rbprev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		prev = rb_entry_rq(rbprev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (rbnext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		next = rb_entry_rq(rbnext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		rbnext = rb_first(&bfqq->sort_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (rbnext && rbnext != &last->rb_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			next = rb_entry_rq(rbnext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) /* see the definition of bfq_async_charge_factor for details */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) static unsigned long bfq_serv_to_charge(struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 					struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	    bfq_asymmetric_scenario(bfqq->bfqd, bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	return blk_rq_sectors(rq) * bfq_async_charge_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929)  * bfq_updated_next_req - update the queue after a new next_rq selection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930)  * @bfqd: the device data the queue belongs to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931)  * @bfqq: the queue to update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933)  * If the first request of a queue changes we make sure that the queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934)  * has enough budget to serve at least its first request (if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935)  * request has grown).  We do this because if the queue has not enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936)  * budget for its first request, it has to go through two dispatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937)  * rounds to actually get it dispatched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) static void bfq_updated_next_req(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 				 struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	struct request *next_rq = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	unsigned long new_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (!next_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (bfqq == bfqd->in_service_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		 * In order not to break guarantees, budgets cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		 * changed after an entity has been selected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	new_budget = max_t(unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			   max_t(unsigned long, bfqq->max_budget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 				 bfq_serv_to_charge(next_rq, bfqq)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			   entity->service);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	if (entity->budget != new_budget) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		entity->budget = new_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 					 new_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		bfq_requeue_bfqq(bfqd, bfqq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) static unsigned int bfq_wr_duration(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	u64 dur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	if (bfqd->bfq_wr_max_time > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		return bfqd->bfq_wr_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	dur = bfqd->rate_dur_prod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	do_div(dur, bfqd->peak_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 * Limit duration between 3 and 25 seconds. The upper limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	 * has been conservatively set after the following worst case:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	 * on a QEMU/KVM virtual machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	 * - running in a slow PC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	 * - with a virtual disk stacked on a slow low-end 5400rpm HDD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	 * - serving a heavy I/O workload, such as the sequential reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 *   of several files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * mplayer took 23 seconds to start, if constantly weight-raised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * As for higher values than that accommodating the above bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 * scenario, tests show that higher values would often yield
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	 * the opposite of the desired result, i.e., would worsen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	 * responsiveness by allowing non-interactive applications to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	 * preserve weight raising for too long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	 * On the other end, lower values than 3 seconds make it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	 * difficult for most interactive tasks to complete their jobs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	 * before weight-raising finishes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	return clamp_val(dur, msecs_to_jiffies(3000), msecs_to_jiffies(25000));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) /* switch back from soft real-time to interactive weight raising */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) static void switch_back_to_interactive_wr(struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 					  struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	bfqq->wr_coeff = bfqd->bfq_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		      struct bfq_io_cq *bic, bool bfq_already_existing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	unsigned int old_wr_coeff = bfqq->wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	bool busy = bfq_already_existing && bfq_bfqq_busy(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	if (bic->saved_has_short_ttime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		bfq_mark_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		bfq_clear_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (bic->saved_IO_bound)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		bfq_mark_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		bfq_clear_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	bfqq->entity.new_weight = bic->saved_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	bfqq->ttime = bic->saved_ttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	bfqq->wr_coeff = bic->saved_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	    time_is_before_jiffies(bfqq->last_wr_start_finish +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 				   bfqq->wr_cur_max_time))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		    !bfq_bfqq_in_large_burst(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		    time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 					     bfq_wr_duration(bfqd))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			switch_back_to_interactive_wr(bfqq, bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 			bfqq->wr_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			bfq_log_bfqq(bfqq->bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				     "resume state: switching off wr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	/* make sure weight will be updated, however we got here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	if (likely(!busy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (old_wr_coeff == 1 && bfqq->wr_coeff > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		bfqd->wr_busy_queues++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		bfqd->wr_busy_queues--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) static int bfqq_process_refs(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	return bfqq->ref - bfqq->allocated - bfqq->entity.on_st_or_in_serv -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		(bfqq->weight_counter != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) /* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	struct bfq_queue *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	struct hlist_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		hlist_del_init(&item->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	 * Start the creation of a new burst list only if there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	 * active queue. See comments on the conditional invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	 * bfq_handle_burst().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (bfq_tot_busy_queues(bfqd) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		bfqd->burst_size = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		bfqd->burst_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	bfqd->burst_parent_entity = bfqq->entity.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) /* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	/* Increment burst size to take into account also bfqq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	bfqd->burst_size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		struct bfq_queue *pos, *bfqq_item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		struct hlist_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		 * Enough queues have been activated shortly after each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		 * other to consider this burst as large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		bfqd->large_burst = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		 * We can now mark all queues in the burst list as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		 * belonging to a large burst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		hlist_for_each_entry(bfqq_item, &bfqd->burst_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 				     burst_list_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			bfq_mark_bfqq_in_large_burst(bfqq_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		bfq_mark_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		 * From now on, and until the current burst finishes, any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		 * new queue being activated shortly after the last queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		 * was inserted in the burst can be immediately marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		 * belonging to a large burst. So the burst list is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 		 * needed any more. Remove it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		hlist_for_each_entry_safe(pos, n, &bfqd->burst_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 					  burst_list_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 			hlist_del_init(&pos->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	} else /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		* Burst not yet large: add bfqq to the burst list. Do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		* not increment the ref counter for bfqq, because bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		* is removed from the burst list before freeing bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		* in put_queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 		hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)  * If many queues belonging to the same group happen to be created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)  * shortly after each other, then the processes associated with these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)  * queues have typically a common goal. In particular, bursts of queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)  * creations are usually caused by services or applications that spawn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)  * many parallel threads/processes. Examples are systemd during boot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)  * or git grep. To help these processes get their job done as soon as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)  * possible, it is usually better to not grant either weight-raising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142)  * or device idling to their queues, unless these queues must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)  * protected from the I/O flowing through other active queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)  * In this comment we describe, firstly, the reasons why this fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)  * holds, and, secondly, the next function, which implements the main
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)  * steps needed to properly mark these queues so that they can then be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)  * treated in a different way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150)  * The above services or applications benefit mostly from a high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)  * throughput: the quicker the requests of the activated queues are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)  * cumulatively served, the sooner the target job of these queues gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)  * completed. As a consequence, weight-raising any of these queues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)  * which also implies idling the device for it, is almost always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * counterproductive, unless there are other active queues to isolate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  * these new queues from. If there no other active queues, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  * weight-raising these new queues just lowers throughput in most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)  * cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)  * On the other hand, a burst of queue creations may be caused also by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)  * the start of an application that does not consist of a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)  * parallel I/O-bound threads. In fact, with a complex application,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163)  * several short processes may need to be executed to start-up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)  * application. In this respect, to start an application as quickly as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)  * possible, the best thing to do is in any case to privilege the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)  * related to the application with respect to all other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)  * I/O. Therefore, the best strategy to start as quickly as possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)  * an application that causes a burst of queue creations is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)  * weight-raise all the queues created during the burst. This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170)  * exact opposite of the best strategy for the other type of bursts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)  * In the end, to take the best action for each of the two cases, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173)  * two types of bursts need to be distinguished. Fortunately, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174)  * seems relatively easy, by looking at the sizes of the bursts. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)  * particular, we found a threshold such that only bursts with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)  * larger size than that threshold are apparently caused by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)  * services or commands such as systemd or git grep. For brevity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)  * hereafter we call just 'large' these bursts. BFQ *does not*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * weight-raise queues whose creation occurs in a large burst. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * addition, for each of these queues BFQ performs or does not perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * idling depending on which choice boosts the throughput more. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  * exact choice depends on the device and request pattern at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  * hand.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  * Unfortunately, false positives may occur while an interactive task
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  * is starting (e.g., an application is being started). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  * consequence is that the queues associated with the task do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  * enjoy weight raising as expected. Fortunately these false positives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  * are very rare. They typically occur if some service happens to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)  * start doing I/O exactly when the interactive task starts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)  * Turning back to the next function, it is invoked only if there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)  * no active queues (apart from active queues that would belong to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  * same, possible burst bfqq would belong to), and it implements all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)  * the steps needed to detect the occurrence of a large burst and to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  * properly mark all the queues belonging to it (so that they can then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)  * be treated in a different way). This goal is achieved by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)  * maintaining a "burst list" that holds, temporarily, the queues that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)  * belong to the burst in progress. The list is then used to mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)  * these queues as belonging to a large burst if the burst does become
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201)  * large. The main steps are the following.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)  * . when the very first queue is created, the queue is inserted into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)  *   list (as it could be the first queue in a possible burst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)  * . if the current burst has not yet become large, and a queue Q that does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)  *   not yet belong to the burst is activated shortly after the last time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  *   at which a new queue entered the burst list, then the function appends
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  *   Q to the burst list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  * . if, as a consequence of the previous step, the burst size reaches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  *   the large-burst threshold, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)  *     . all the queues in the burst list are marked as belonging to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)  *       large burst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)  *     . the burst list is deleted; in fact, the burst list already served
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)  *       its purpose (keeping temporarily track of the queues in a burst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219)  *       so as to be able to mark them as belonging to a large burst in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)  *       previous sub-step), and now is not needed any more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)  *     . the device enters a large-burst mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)  * . if a queue Q that does not belong to the burst is created while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)  *   the device is in large-burst mode and shortly after the last time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)  *   at which a queue either entered the burst list or was marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)  *   belonging to the current large burst, then Q is immediately marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)  *   as belonging to a large burst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230)  * . if a queue Q that does not belong to the burst is created a while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)  *   later, i.e., not shortly after, than the last time at which a queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)  *   either entered the burst list or was marked as belonging to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233)  *   current large burst, then the current burst is deemed as finished and:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  *        . the large-burst mode is reset if set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  *        . the burst list is emptied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  *        . Q is inserted in the burst list, as Q may be the first queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240)  *          in a possible new burst (then the burst list contains just Q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)  *          after this step).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	 * If bfqq is already in the burst list or is part of a large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	 * burst, or finally has just been split, then there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	 * nothing else to do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	if (!hlist_unhashed(&bfqq->burst_list_node) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	    bfq_bfqq_in_large_burst(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	    time_is_after_eq_jiffies(bfqq->split_time +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 				     msecs_to_jiffies(10)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	 * If bfqq's creation happens late enough, or bfqq belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	 * a different group than the burst group, then the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	 * burst is finished, and related data structures must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	 * reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	 * In this respect, consider the special case where bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	 * the very first queue created after BFQ is selected for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	 * device. In this case, last_ins_in_burst and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	 * burst_parent_entity are not yet significant when we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	 * here. But it is easy to verify that, whether or not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	 * following condition is true, bfqq will end up being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	 * inserted into the burst list. In particular the list will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	 * happen to contain only bfqq. And this is exactly what has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	 * to happen, as bfqq may be the first queue of the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	 * burst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	if (time_is_before_jiffies(bfqd->last_ins_in_burst +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	    bfqd->bfq_burst_interval) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	    bfqq->entity.parent != bfqd->burst_parent_entity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		bfqd->large_burst = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		bfq_reset_burst_list(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	 * If we get here, then bfqq is being activated shortly after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	 * last queue. So, if the current burst is also large, we can mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	 * bfqq as belonging to this large burst immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	if (bfqd->large_burst) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		bfq_mark_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	 * If we get here, then a large-burst state has not yet been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	 * reached, but bfqq is being activated shortly after the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	 * queue. Then we add bfqq to the burst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	bfq_add_to_burst(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	 * At this point, bfqq either has been added to the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	 * burst or has caused the current burst to terminate and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	 * possible new burst to start. In particular, in the second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	 * case, bfqq has become the first queue in the possible new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	 * burst.  In both cases last_ins_in_burst needs to be moved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	 * forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	bfqd->last_ins_in_burst = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) static int bfq_bfqq_budget_left(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	return entity->budget - entity->service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  * If enough samples have been computed, return the current max budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)  * stored in bfqd, which is dynamically updated according to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)  * estimated disk peak rate; otherwise return the default max budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static int bfq_max_budget(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (bfqd->budgets_assigned < bfq_stats_min_budgets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		return bfq_default_max_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		return bfqd->bfq_max_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)  * Return min budget, which is a fraction of the current or default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331)  * max budget (trying with 1/32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static int bfq_min_budget(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	if (bfqd->budgets_assigned < bfq_stats_min_budgets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		return bfq_default_max_budget / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		return bfqd->bfq_max_budget / 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)  * The next function, invoked after the input queue bfqq switches from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)  * idle to busy, updates the budget of bfqq. The function also tells
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)  * whether the in-service queue should be expired, by returning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)  * true. The purpose of expiring the in-service queue is to give bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346)  * the chance to possibly preempt the in-service queue, and the reason
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)  * for preempting the in-service queue is to achieve one of the two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)  * goals below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350)  * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351)  * expired because it has remained idle. In particular, bfqq may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352)  * expired for one of the following two reasons:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)  * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)  *   and did not make it to issue a new request before its last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)  *   request was served;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)  * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)  *   a new request before the expiration of the idling-time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)  * Even if bfqq has expired for one of the above reasons, the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362)  * associated with the queue may be however issuing requests greedily,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)  * and thus be sensitive to the bandwidth it receives (bfqq may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)  * remained idle for other reasons: CPU high load, bfqq not enjoying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)  * idling, I/O throttling somewhere in the path from the process to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * the I/O scheduler, ...). But if, after every expiration for one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * the above two reasons, bfqq has to wait for the service of at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * one full budget of another queue before being served again, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  * bfqq is likely to get a much lower bandwidth or resource time than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)  * its reserved ones. To address this issue, two countermeasures need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)  * to be taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373)  * First, the budget and the timestamps of bfqq need to be updated in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)  * a special way on bfqq reactivation: they need to be updated as if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375)  * bfqq did not remain idle and did not expire. In fact, if they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)  * computed as if bfqq expired and remained idle until reactivation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377)  * then the process associated with bfqq is treated as if, instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)  * being greedy, it stopped issuing requests when bfqq remained idle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)  * and restarts issuing requests only on this reactivation. In other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)  * words, the scheduler does not help the process recover the "service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)  * hole" between bfqq expiration and reactivation. As a consequence,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)  * the process receives a lower bandwidth than its reserved one. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)  * contrast, to recover this hole, the budget must be updated as if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)  * bfqq was not expired at all before this reactivation, i.e., it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385)  * be set to the value of the remaining budget when bfqq was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)  * expired. Along the same line, timestamps need to be assigned the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)  * value they had the last time bfqq was selected for service, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)  * before last expiration. Thus timestamps need to be back-shifted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)  * with respect to their normal computation (see [1] for more details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390)  * on this tricky aspect).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)  * Secondly, to allow the process to recover the hole, the in-service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393)  * queue must be expired too, to give bfqq the chance to preempt it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)  * immediately. In fact, if bfqq has to wait for a full budget of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395)  * in-service queue to be completed, then it may become impossible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)  * let the process recover the hole, even if the back-shifted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)  * timestamps of bfqq are lower than those of the in-service queue. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)  * this happens for most or all of the holes, then the process may not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)  * receive its reserved bandwidth. In this respect, it is worth noting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  * that, being the service of outstanding requests unpreemptible, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)  * little fraction of the holes may however be unrecoverable, thereby
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)  * causing a little loss of bandwidth.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)  * The last important point is detecting whether bfqq does need this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405)  * bandwidth recovery. In this respect, the next function deems the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)  * process associated with bfqq greedy, and thus allows it to recover
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)  * the hole, if: 1) the process is waiting for the arrival of a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)  * request (which implies that bfqq expired for one of the above two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)  * reasons), and 2) such a request has arrived soon. The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)  * condition is controlled through the flag non_blocking_wait_rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)  * while the second through the flag arrived_in_time. If both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  * conditions hold, then the function computes the budget in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)  * above-described special way, and signals that the in-service queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)  * should be expired. Timestamp back-shifting is done later in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)  * __bfq_activate_entity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)  * 2. Reduce latency. Even if timestamps are not backshifted to let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)  * the process associated with bfqq recover a service hole, bfqq may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)  * however happen to have, after being (re)activated, a lower finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420)  * timestamp than the in-service queue.	 That is, the next budget of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)  * bfqq may have to be completed before the one of the in-service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422)  * queue. If this is the case, then preempting the in-service queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)  * allows this goal to be achieved, apart from the unpreemptible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)  * outstanding requests mentioned above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426)  * Unfortunately, regardless of which of the above two goals one wants
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)  * to achieve, service trees need first to be updated to know whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)  * the in-service queue must be preempted. To have service trees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)  * correctly updated, the in-service queue must be expired and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)  * rescheduled, and bfqq must be scheduled too. This is one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)  * most costly operations (in future versions, the scheduling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432)  * mechanism may be re-designed in such a way to make it possible to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433)  * know whether preemption is needed without needing to update service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434)  * trees). In addition, queue preemptions almost always cause random
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435)  * I/O, which may in turn cause loss of throughput. Finally, there may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)  * even be no in-service queue when the next function is invoked (so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)  * no queue to compare timestamps with). Because of these facts, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438)  * next function adopts the following simple scheme to avoid costly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)  * operations, too frequent preemptions and too many dependencies on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)  * the state of the scheduler: it requests the expiration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441)  * in-service queue (unconditionally) only for queues that need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442)  * recover a hole. Then it delegates to other parts of the code the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443)  * responsibility of handling the above case 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 						struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 						bool arrived_in_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 	 * In the next compound condition, we check also whether there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 	 * is some budget left, because otherwise there is no point in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	 * trying to go on serving bfqq with this same budget: bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	 * would be expired immediately after being selected for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	 * service. This would only cause useless overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	    bfq_bfqq_budget_left(bfqq) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 		 * We do not clear the flag non_blocking_wait_rq here, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		 * the latter is used in bfq_activate_bfqq to signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		 * that timestamps need to be back-shifted (and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		 * cleared right after).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		 * In next assignment we rely on that either
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		 * entity->service or entity->budget are not updated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		 * on expiration if bfqq is empty (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		 * __bfq_bfqq_recalc_budget). Thus both quantities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		 * remain unchanged after such an expiration, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		 * following statement therefore assigns to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		 * entity->budget the remaining budget on such an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		 * expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		entity->budget = min_t(unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 				       bfq_bfqq_budget_left(bfqq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 				       bfqq->max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 		 * At this point, we have used entity->service to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		 * the budget left (needed for updating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		 * entity->budget). Thus we finally can, and have to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		 * reset entity->service. The latter must be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 		 * because bfqq would otherwise be charged again for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		 * the service it has received during its previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		 * service slot(s).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 		entity->service = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	 * We can finally complete expiration, by setting service to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	entity->service = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	entity->budget = max_t(unsigned long, bfqq->max_budget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 			       bfq_serv_to_charge(bfqq->next_rq, bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	bfq_clear_bfqq_non_blocking_wait_rq(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)  * Return the farthest past time instant according to jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507)  * macros.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) static unsigned long bfq_smallest_from_now(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	return jiffies - MAX_JIFFY_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 					     struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 					     unsigned int old_wr_coeff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 					     bool wr_or_deserves_wr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 					     bool interactive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 					     bool in_burst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 					     bool soft_rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	if (old_wr_coeff == 1 && wr_or_deserves_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		/* start a weight-raising period */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		if (interactive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 			bfqq->service_from_wr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 			 * No interactive weight raising in progress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 			 * here: assign minus infinity to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			 * wr_start_at_switch_to_srt, to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			 * that, at the end of the soft-real-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			 * weight raising periods that is starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			 * now, no interactive weight-raising period
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			 * may be wrongly considered as still in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			 * progress (and thus actually started by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 			 * mistake).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 			bfqq->wr_start_at_switch_to_srt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 				bfq_smallest_from_now();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			bfqq->wr_coeff = bfqd->bfq_wr_coeff *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 				BFQ_SOFTRT_WEIGHT_FACTOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 			bfqq->wr_cur_max_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 				bfqd->bfq_wr_rt_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		 * If needed, further reduce budget to make sure it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 		 * close to bfqq's backlog, so as to reduce the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		 * scheduling-error component due to a too large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 * budget. Do not care about throughput consequences,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		 * but only about latency. Finally, do not assign a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		 * too small budget either, to avoid increasing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		 * latency by causing too frequent expirations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		bfqq->entity.budget = min_t(unsigned long,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 					    bfqq->entity.budget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 					    2 * bfq_min_budget(bfqd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	} else if (old_wr_coeff > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		if (interactive) { /* update wr coeff and duration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		} else if (in_burst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			bfqq->wr_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 		else if (soft_rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			 * The application is now or still meeting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			 * requirements for being deemed soft rt.  We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 			 * can then correctly and safely (re)charge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			 * the weight-raising duration for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 			 * application with the weight-raising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 			 * duration for soft rt applications.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 			 * In particular, doing this recharge now, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 			 * before the weight-raising period for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 			 * application finishes, reduces the probability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 			 * of the following negative scenario:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			 * 1) the weight of a soft rt application is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			 *    raised at startup (as for any newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			 *    created application),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 			 * 2) since the application is not interactive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 			 *    at a certain time weight-raising is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			 *    stopped for the application,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			 * 3) at that time the application happens to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			 *    still have pending requests, and hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			 *    is destined to not have a chance to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 			 *    deemed soft rt before these requests are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 			 *    completed (see the comments to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			 *    function bfq_bfqq_softrt_next_start()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			 *    for details on soft rt detection),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			 * 4) these pending requests experience a high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 			 *    latency because the application is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 			 *    weight-raised while they are pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 			if (bfqq->wr_cur_max_time !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 				bfqd->bfq_wr_rt_max_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 				bfqq->wr_start_at_switch_to_srt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 					bfqq->last_wr_start_finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 				bfqq->wr_cur_max_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 					bfqd->bfq_wr_rt_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 				bfqq->wr_coeff = bfqd->bfq_wr_coeff *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 					BFQ_SOFTRT_WEIGHT_FACTOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 					struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	return bfqq->dispatched == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		time_is_before_jiffies(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 			bfqq->budget_timeout +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			bfqd->bfq_wr_min_idle_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622)  * Return true if bfqq is in a higher priority class, or has a higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)  * weight than the in-service queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) static bool bfq_bfqq_higher_class_or_weight(struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 					    struct bfq_queue *in_serv_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	int bfqq_weight, in_serv_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	if (bfqq->ioprio_class < in_serv_bfqq->ioprio_class)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 	if (in_serv_bfqq->entity.parent == bfqq->entity.parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		bfqq_weight = bfqq->entity.weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		in_serv_weight = in_serv_bfqq->entity.weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 		if (bfqq->entity.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			bfqq_weight = bfqq->entity.parent->weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			bfqq_weight = bfqq->entity.weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 		if (in_serv_bfqq->entity.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			in_serv_weight = in_serv_bfqq->entity.parent->weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 			in_serv_weight = in_serv_bfqq->entity.weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 	return bfqq_weight > in_serv_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 					     struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 					     int old_wr_coeff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 					     struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 					     bool *interactive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	bool soft_rt, in_burst,	wr_or_deserves_wr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		bfqq_wants_to_preempt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		 * See the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 		 * bfq_bfqq_update_budg_for_activation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		 * details on the usage of the next variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		arrived_in_time =  ktime_get_ns() <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 			bfqq->ttime.last_end_request +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			bfqd->bfq_slice_idle * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	 * bfqq deserves to be weight-raised if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	 * - it is sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	 * - it does not belong to a large burst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	 * - it has been idle for enough time or is soft real-time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	 * - is linked to a bfq_io_cq (it is not shared in any sense).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	in_burst = bfq_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		!BFQQ_TOTALLY_SEEKY(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		!in_burst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		time_is_before_jiffies(bfqq->soft_rt_next_start) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		bfqq->dispatched == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	*interactive = !in_burst && idle_for_long_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	wr_or_deserves_wr = bfqd->low_latency &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		(bfqq->wr_coeff > 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		 (bfq_bfqq_sync(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		  bfqq->bic && (*interactive || soft_rt)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	 * Using the last flag, update budget and check whether bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	 * may want to preempt the in-service queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	bfqq_wants_to_preempt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 		bfq_bfqq_update_budg_for_activation(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 						    arrived_in_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	 * If bfqq happened to be activated in a burst, but has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	 * idle for much more than an interactive queue, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	 * assume that, in the overall I/O initiated in the burst, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	 * I/O associated with bfqq is finished. So bfqq does not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	 * to be treated as a queue belonging to a burst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	 * anymore. Accordingly, we reset bfqq's in_large_burst flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	 * if set, and remove bfqq from the burst list if it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	 * there. We do not decrement burst_size, because the fact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 * that bfqq does not need to belong to the burst list any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	 * more does not invalidate the fact that bfqq was created in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	 * a burst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 	if (likely(!bfq_bfqq_just_created(bfqq)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	    idle_for_long_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	    time_is_before_jiffies(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		    bfqq->budget_timeout +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		    msecs_to_jiffies(10000))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		hlist_del_init(&bfqq->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		bfq_clear_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	bfq_clear_bfqq_just_created(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	if (!bfq_bfqq_IO_bound(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		if (arrived_in_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 			bfqq->requests_within_timer++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 			if (bfqq->requests_within_timer >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 			    bfqd->bfq_requests_within_timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 				bfq_mark_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 			bfqq->requests_within_timer = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (bfqd->low_latency) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		if (unlikely(time_is_after_jiffies(bfqq->split_time)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 			/* wraparound */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			bfqq->split_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 				jiffies - bfqd->bfq_wr_min_idle_time - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (time_is_before_jiffies(bfqq->split_time +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 					   bfqd->bfq_wr_min_idle_time)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 							 old_wr_coeff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 							 wr_or_deserves_wr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 							 *interactive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 							 in_burst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 							 soft_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 			if (old_wr_coeff != bfqq->wr_coeff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 				bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	bfqq->last_idle_bklogged = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	bfqq->service_from_backlogged = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	bfq_clear_bfqq_softrt_update(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	bfq_add_bfqq_busy(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	 * Expire in-service queue only if preemption may be needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	 * for guarantees. In particular, we care only about two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	 * cases. The first is that bfqq has to recover a service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	 * hole, as explained in the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	 * bfq_bfqq_update_budg_for_activation(), i.e., that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	 * bfqq_wants_to_preempt is true. However, if bfqq does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	 * carry time-critical I/O, then bfqq's bandwidth is less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	 * important than that of queues that carry time-critical I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	 * So, as a further constraint, we consider this case only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	 * bfqq is at least as weight-raised, i.e., at least as time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	 * critical, as the in-service queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	 * The second case is that bfqq is in a higher priority class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	 * or has a higher weight than the in-service queue. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	 * condition does not hold, we don't care because, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	 * bfqq does not start to be served immediately, the resulting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	 * delay for bfqq's I/O is however lower or much lower than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	 * the ideal completion time to be guaranteed to bfqq's I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	 * In both cases, preemption is needed only if, according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	 * the timestamps of both bfqq and of the in-service queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	 * bfqq actually is the next queue to serve. So, to reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	 * useless preemptions, the return value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	 * next_queue_may_preempt() is considered in the next compound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	 * condition too. Yet next_queue_may_preempt() just checks a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 	 * simple, necessary condition for bfqq to be the next queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	 * to serve. In fact, to evaluate a sufficient condition, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	 * timestamps of the in-service queue would need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	 * updated, and this operation is quite costly (see the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	 * comments on bfq_bfqq_update_budg_for_activation()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	if (bfqd->in_service_queue &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	    ((bfqq_wants_to_preempt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	      bfqq->wr_coeff >= bfqd->in_service_queue->wr_coeff) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	     bfq_bfqq_higher_class_or_weight(bfqq, bfqd->in_service_queue)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 	    next_queue_may_preempt(bfqd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		bfq_bfqq_expire(bfqd, bfqd->in_service_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 				false, BFQQE_PREEMPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) static void bfq_reset_inject_limit(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 				   struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	/* invalidate baseline total service time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	bfqq->last_serv_time_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	 * Reset pointer in case we are waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	 * some request completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 	bfqd->waited_rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 	 * If bfqq has a short think time, then start by setting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	 * inject limit to 0 prudentially, because the service time of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 	 * an injected I/O request may be higher than the think time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 	 * of bfqq, and therefore, if one request was injected when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 	 * bfqq remains empty, this injected request might delay the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 	 * service of the next I/O request for bfqq significantly. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	 * case bfqq can actually tolerate some injection, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 	 * adaptive update will however raise the limit soon. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	 * lucky circumstance holds exactly because bfqq has a short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	 * think time, and thus, after remaining empty, is likely to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	 * get new I/O enqueued---and then completed---before being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 	 * expired. This is the very pattern that gives the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	 * limit-update algorithm the chance to measure the effect of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	 * injection on request service times, and then to update the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	 * limit accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	 * However, in the following special case, the inject limit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	 * left to 1 even if the think time is short: bfqq's I/O is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	 * synchronized with that of some other queue, i.e., bfqq may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 	 * receive new I/O only after the I/O of the other queue is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 	 * completed. Keeping the inject limit to 1 allows the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 	 * blocking I/O to be served while bfqq is in service. And
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 	 * this is very convenient both for bfqq and for overall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	 * throughput, as explained in detail in the comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	 * bfq_update_has_short_ttime().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	 * On the opposite end, if bfqq has a long think time, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	 * start directly by 1, because:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 	 * a) on the bright side, keeping at most one request in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	 * service in the drive is unlikely to cause any harm to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	 * latency of bfqq's requests, as the service time of a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 	 * request is likely to be lower than the think time of bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	 * b) on the downside, after becoming empty, bfqq is likely to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	 * expire before getting its next request. With this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	 * arrival pattern, it is very hard to sample total service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	 * times and update the inject limit accordingly (see comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	 * on bfq_update_inject_limit()). So the limit is likely to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	 * never, or at least seldom, updated.  As a consequence, by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	 * setting the limit to 1, we avoid that no injection ever
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	 * occurs with bfqq. On the downside, this proactive step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	 * further reduces chances to actually compute the baseline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	 * total service time. Thus it reduces chances to execute the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	 * limit-update algorithm and possibly raise the limit to more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	 * than 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	if (bfq_bfqq_has_short_ttime(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		bfqq->inject_limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		bfqq->inject_limit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	bfqq->decrease_time_jif = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) static void bfq_add_request(struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	struct bfq_data *bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	struct request *next_rq, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	unsigned int old_wr_coeff = bfqq->wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	bool interactive = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 	bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	bfqq->queued[rq_is_sync(rq)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	bfqd->queued++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_bfqq_sync(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		 * Detect whether bfqq's I/O seems synchronized with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		 * that of some other queue, i.e., whether bfqq, after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 		 * remaining empty, happens to receive new I/O only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 		 * right after some I/O request of the other queue has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		 * been completed. We call waker queue the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		 * queue, and we assume, for simplicity, that bfqq may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 		 * have at most one waker queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 		 * A remarkable throughput boost can be reached by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 		 * unconditionally injecting the I/O of the waker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 		 * queue, every time a new bfq_dispatch_request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 		 * happens to be invoked while I/O is being plugged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 		 * for bfqq.  In addition to boosting throughput, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 		 * unblocks bfqq's I/O, thereby improving bandwidth
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		 * and latency for bfqq. Note that these same results
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		 * may be achieved with the general injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		 * mechanism, but less effectively. For details on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		 * this aspect, see the comments on the choice of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 		 * queue for injection in bfq_select_queue().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 		 * Turning back to the detection of a waker queue, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 		 * queue Q is deemed as a waker queue for bfqq if, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 		 * two consecutive times, bfqq happens to become non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		 * empty right after a request of Q has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		 * completed. In particular, on the first time, Q is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		 * tentatively set as a candidate waker queue, while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		 * on the second time, the flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		 * bfq_bfqq_has_waker(bfqq) is set to confirm that Q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		 * is a waker queue for bfqq. These detection steps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		 * are performed only if bfqq has a long think time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 		 * so as to make it more likely that bfqq's I/O is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		 * actually being blocked by a synchronization. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		 * last filter, plus the above two-times requirement,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		 * make false positives less likely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		 * NOTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 		 * The sooner a waker queue is detected, the sooner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		 * throughput can be boosted by injecting I/O from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 		 * waker queue. Fortunately, detection is likely to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 		 * actually fast, for the following reasons. While
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		 * blocked by synchronization, bfqq has a long think
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 		 * time. This implies that bfqq's inject limit is at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 		 * least equal to 1 (see the comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		 * bfq_update_inject_limit()). So, thanks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 		 * injection, the waker queue is likely to be served
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 		 * during the very first I/O-plugging time interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 		 * for bfqq. This triggers the first step of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 		 * detection mechanism. Thanks again to injection, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		 * candidate waker queue is then likely to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		 * confirmed no later than during the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		 * I/O-plugging interval for bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		if (bfqd->last_completed_rq_bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 		    !bfq_bfqq_has_short_ttime(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		    ktime_get_ns() - bfqd->last_completion <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		    200 * NSEC_PER_USEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 			if (bfqd->last_completed_rq_bfqq != bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 			    bfqd->last_completed_rq_bfqq !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 			    bfqq->waker_bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 				 * First synchronization detected with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 				 * a candidate waker queue, or with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 				 * different candidate waker queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 				 * from the current one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 				bfqq->waker_bfqq = bfqd->last_completed_rq_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 				 * If the waker queue disappears, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 				 * bfqq->waker_bfqq must be reset. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 				 * this goal, we maintain in each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 				 * waker queue a list, woken_list, of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 				 * all the queues that reference the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 				 * waker queue through their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 				 * waker_bfqq pointer. When the waker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 				 * queue exits, the waker_bfqq pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 				 * of all the queues in the woken_list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 				 * is reset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 				 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 				 * In addition, if bfqq is already in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 				 * the woken_list of a waker queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 				 * then, before being inserted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 				 * the woken_list of a new waker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 				 * queue, bfqq must be removed from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 				 * the woken_list of the old waker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 				 * queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 				if (!hlist_unhashed(&bfqq->woken_list_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 					hlist_del_init(&bfqq->woken_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 				hlist_add_head(&bfqq->woken_list_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 				    &bfqd->last_completed_rq_bfqq->woken_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 				bfq_clear_bfqq_has_waker(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 			} else if (bfqd->last_completed_rq_bfqq ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 				   bfqq->waker_bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 				   !bfq_bfqq_has_waker(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 				 * synchronization with waker_bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 				 * seen for the second time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 				bfq_mark_bfqq_has_waker(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		 * Periodically reset inject limit, to make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 		 * the latter eventually drops in case workload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		 * changes, see step (3) in the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		 * bfq_update_inject_limit().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 		if (time_is_before_eq_jiffies(bfqq->decrease_time_jif +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 					     msecs_to_jiffies(1000)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 			bfq_reset_inject_limit(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 		 * The following conditions must hold to setup a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		 * sampling of total service time, and then a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		 * update of the inject limit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		 * - bfqq is in service, because the total service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		 *   time is evaluated only for the I/O requests of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		 *   the queues in service;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		 * - this is the right occasion to compute or to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		 *   lower the baseline total service time, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		 *   there are actually no requests in the drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		 *   or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		 *   the baseline total service time is available, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		 *   this is the right occasion to compute the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 		 *   quantity needed to update the inject limit, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		 *   the total service time caused by the amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		 *   injection allowed by the current value of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 		 *   limit. It is the right occasion because injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		 *   has actually been performed during the service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 		 *   hole, and there are still in-flight requests,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 		 *   which are very likely to be exactly the injected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		 *   requests, or part of them;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		 * - the minimum interval for sampling the total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 		 *   service time and updating the inject limit has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		 *   elapsed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		if (bfqq == bfqd->in_service_queue &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 		    (bfqd->rq_in_driver == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		     (bfqq->last_serv_time_ns > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 		      bfqd->rqs_injected && bfqd->rq_in_driver > 0)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		    time_is_before_eq_jiffies(bfqq->decrease_time_jif +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 					      msecs_to_jiffies(10))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 			bfqd->last_empty_occupied_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 			 * Start the state machine for measuring the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 			 * total service time of rq: setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 			 * wait_dispatch will cause bfqd->waited_rq to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 			 * be set when rq will be dispatched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 			bfqd->wait_dispatch = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 			 * If there is no I/O in service in the drive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			 * then possible injection occurred before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 			 * arrival of rq will not affect the total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 			 * service time of rq. So the injection limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 			 * must not be updated as a function of such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 			 * total service time, unless new injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 			 * occurs before rq is completed. To have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 			 * injection limit updated only in the latter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			 * case, reset rqs_injected here (rqs_injected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 			 * will be set in case injection is performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 			 * on bfqq before rq is completed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			if (bfqd->rq_in_driver == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 				bfqd->rqs_injected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	elv_rb_add(&bfqq->sort_list, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	 * Check if this request is a better next-serve candidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	prev = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	bfqq->next_rq = next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	 * Adjust priority tree position, if next_rq changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	 * See comments on bfq_pos_tree_add_move() for the unlikely().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	if (unlikely(!bfqd->nonrot_with_queueing && prev != bfqq->next_rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		bfq_pos_tree_add_move(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 		bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 						 rq, &interactive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 		if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 		    time_is_before_jiffies(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 				bfqq->last_wr_start_finish +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 				bfqd->bfq_wr_min_inter_arr_async)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 			bfqq->wr_coeff = bfqd->bfq_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			bfqq->wr_cur_max_time = bfq_wr_duration(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 			bfqd->wr_busy_queues++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		if (prev != bfqq->next_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 			bfq_updated_next_req(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 	 * Assign jiffies to last_wr_start_finish in the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 	 * cases:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 	 * . if bfqq is not going to be weight-raised, because, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 	 *   non weight-raised queues, last_wr_start_finish stores the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 	 *   arrival time of the last request; as of now, this piece
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	 *   of information is used only for deciding whether to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	 *   weight-raise async queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	 * . if bfqq is not weight-raised, because, if bfqq is now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	 *   switching to weight-raised, then last_wr_start_finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	 *   stores the time when weight-raising starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	 * . if bfqq is interactive, because, regardless of whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	 *   bfqq is currently weight-raised, the weight-raising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	 *   period must start or restart (this case is considered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	 *   separately because it is not detected by the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	 *   conditions, if bfqq is already weight-raised)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	 * last_wr_start_finish has to be updated also if bfqq is soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 	 * real-time, because the weight-raising period is constantly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	 * restarted on idle-to-busy transitions for these queues, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	 * this is already done in bfq_bfqq_handle_idle_busy_switch if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	 * needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	if (bfqd->low_latency &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 		(old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 					  struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 					  struct request_queue *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	struct bfq_queue *bfqq = bfqd->bio_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) static sector_t get_sdist(sector_t last_pos, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	if (last_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		return abs(blk_rq_pos(rq) - last_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) #if 0 /* Still not clear if we can do without next two functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) static void bfq_activate_request(struct request_queue *q, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	bfqd->rq_in_driver++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) static void bfq_deactivate_request(struct request_queue *q, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	bfqd->rq_in_driver--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) static void bfq_remove_request(struct request_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 			       struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	struct bfq_data *bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	const int sync = rq_is_sync(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	if (bfqq->next_rq == rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 		bfq_updated_next_req(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	if (rq->queuelist.prev != &rq->queuelist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		list_del_init(&rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	bfqq->queued[sync]--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	bfqd->queued--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	elv_rb_del(&bfqq->sort_list, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	elv_rqhash_del(q, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 	if (q->last_merge == rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		q->last_merge = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 	if (RB_EMPTY_ROOT(&bfqq->sort_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 		bfqq->next_rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			bfq_del_bfqq_busy(bfqd, bfqq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 			 * bfqq emptied. In normal operation, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 			 * bfqq is empty, bfqq->entity.service and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 			 * bfqq->entity.budget must contain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			 * respectively, the service received and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 			 * budget used last time bfqq emptied. These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 			 * facts do not hold in this case, as at least
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 			 * this last removal occurred while bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 			 * not in service. To avoid inconsistencies,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 			 * reset both bfqq->entity.service and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 			 * bfqq->entity.budget, if bfqq has still a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 			 * process that may issue I/O requests to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			bfqq->entity.budget = bfqq->entity.service = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		 * Remove queue from request-position tree as it is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		if (bfqq->pos_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 			rb_erase(&bfqq->pos_node, bfqq->pos_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			bfqq->pos_root = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		/* see comments on bfq_pos_tree_add_move() for the unlikely() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 		if (unlikely(!bfqd->nonrot_with_queueing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 			bfq_pos_tree_add_move(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) 	if (rq->cmd_flags & REQ_META)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 		bfqq->meta_pending--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) static bool bfq_bio_merge(struct request_queue *q, struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		unsigned int nr_segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	struct request *free = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	 * bfq_bic_lookup grabs the queue_lock: invoke it now and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	 * store its return value for later use, to avoid nesting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 	 * queue_lock inside the bfqd->lock. We assume that the bic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	 * returned by bfq_bic_lookup does not go away before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	 * bfqd->lock is taken.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	if (bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		bfqd->bio_bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 	bfqd->bio_bic = bic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	ret = blk_mq_sched_try_merge(q, bio, nr_segs, &free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	if (free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 		blk_mq_free_request(free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) static int bfq_request_merge(struct request_queue *q, struct request **req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 			     struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	struct request *__rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 	__rq = bfq_find_rq_fmerge(bfqd, bio, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 	if (__rq && elv_bio_merge_ok(__rq, bio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 		*req = __rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		if (blk_discard_mergable(__rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 			return ELEVATOR_DISCARD_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		return ELEVATOR_FRONT_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	return ELEVATOR_NO_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) static struct bfq_queue *bfq_init_rq(struct request *rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) static void bfq_request_merged(struct request_queue *q, struct request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 			       enum elv_merge type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	if (type == ELEVATOR_FRONT_MERGE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	    rb_prev(&req->rb_node) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 	    blk_rq_pos(req) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 	    blk_rq_pos(container_of(rb_prev(&req->rb_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 				    struct request, rb_node))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 		struct bfq_queue *bfqq = bfq_init_rq(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 		struct bfq_data *bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		struct request *prev, *next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 		/* Reposition request in its sort_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		elv_rb_del(&bfqq->sort_list, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 		elv_rb_add(&bfqq->sort_list, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 		/* Choose next request to be served for bfqq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 		prev = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 					 bfqd->last_position);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		bfqq->next_rq = next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 		 * If next_rq changes, update both the queue's budget to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 		 * fit the new request and the queue's position in its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		 * rq_pos_tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		if (prev != bfqq->next_rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 			bfq_updated_next_req(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			 * See comments on bfq_pos_tree_add_move() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			 * the unlikely().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 			if (unlikely(!bfqd->nonrot_with_queueing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 				bfq_pos_tree_add_move(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309)  * This function is called to notify the scheduler that the requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310)  * rq and 'next' have been merged, with 'next' going away.  BFQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311)  * exploits this hook to address the following issue: if 'next' has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)  * fifo_time lower that rq, then the fifo_time of rq must be set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)  * the value of 'next', to not forget the greater age of 'next'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315)  * NOTE: in this function we assume that rq is in a bfq_queue, basing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)  * on that rq is picked from the hash table q->elevator->hash, which,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317)  * in its turn, is filled only with I/O requests present in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318)  * bfq_queues, while BFQ is in use for the request queue q. In fact,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319)  * the function that fills this hash table (elv_rqhash_add) is called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320)  * only by bfq_insert_request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) static void bfq_requests_merged(struct request_queue *q, struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 				struct request *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	struct bfq_queue *bfqq = bfq_init_rq(rq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 		*next_bfqq = bfq_init_rq(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	 * If next and rq belong to the same bfq_queue and next is older
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	 * than rq, then reposition rq in the fifo (by substituting next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	 * with rq). Otherwise, if next and rq belong to different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 	 * bfq_queues, never reposition rq: in fact, we would have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 	 * reposition it with respect to next's position in its own fifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	 * which would most certainly be too expensive with respect to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	 * the benefits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	if (bfqq == next_bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	    !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	    next->fifo_time < rq->fifo_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 		list_del_init(&rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		list_replace_init(&next->queuelist, &rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 		rq->fifo_time = next->fifo_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	if (bfqq->next_rq == next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		bfqq->next_rq = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) /* Must be called with bfqq != NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) static void bfq_bfqq_end_wr(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	if (bfq_bfqq_busy(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		bfqq->bfqd->wr_busy_queues--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	bfqq->wr_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 	bfqq->wr_cur_max_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 	 * Trigger a weight change on the next invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	 * __bfq_entity_update_weight_prio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 	bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) void bfq_end_wr_async_queues(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			     struct bfq_group *bfqg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		for (j = 0; j < IOPRIO_BE_NR; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 			if (bfqg->async_bfqq[i][j])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 				bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 	if (bfqg->async_idle_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 		bfq_bfqq_end_wr(bfqg->async_idle_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) static void bfq_end_wr(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	bfq_end_wr_async(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) static sector_t bfq_io_struct_pos(void *io_struct, bool request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	if (request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		return blk_rq_pos(io_struct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		return ((struct bio *)io_struct)->bi_iter.bi_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) static int bfq_rq_close_to_sector(void *io_struct, bool request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 				  sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	return abs(bfq_io_struct_pos(io_struct, request) - sector) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	       BFQQ_CLOSE_THR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 					 struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 					 sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	struct rb_node *parent, *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	struct bfq_queue *__bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	if (RB_EMPTY_ROOT(root))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	 * First, if we find a request starting at the end of the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	 * request, choose it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	__bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	if (__bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		return __bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	 * If the exact sector wasn't found, the parent of the NULL leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 	 * will contain the closest sector (rq_pos_tree sorted by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 	 * next_request position).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 	__bfqq = rb_entry(parent, struct bfq_queue, pos_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 		return __bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	if (blk_rq_pos(__bfqq->next_rq) < sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		node = rb_next(&__bfqq->pos_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 		node = rb_prev(&__bfqq->pos_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	__bfqq = rb_entry(node, struct bfq_queue, pos_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		return __bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 						   struct bfq_queue *cur_bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 						   sector_t sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 	 * We shall notice if some of the queues are cooperating,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 	 * e.g., working closely on the same area of the device. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	 * that case, we can group them together and: 1) don't waste
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	 * time idling, and 2) serve the union of their requests in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	 * the best possible order for throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	bfqq = bfqq_find_close(bfqd, cur_bfqq, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	if (!bfqq || bfqq == cur_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) static struct bfq_queue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 	int process_refs, new_process_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	struct bfq_queue *__bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 	 * If there are no process references on the new_bfqq, then it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	 * may have dropped their last reference (not just their last process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	 * reference).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	if (!bfqq_process_refs(new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	/* Avoid a circular list and skip interim queue merges. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	while ((__bfqq = new_bfqq->new_bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		if (__bfqq == bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		new_bfqq = __bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	process_refs = bfqq_process_refs(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	new_process_refs = bfqq_process_refs(new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	 * If the process for the bfqq has gone away, there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	 * sense in merging the queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 	if (process_refs == 0 || new_process_refs == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		new_bfqq->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	 * Merging is just a redirection: the requests of the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 	 * owning one of the two queues are redirected to the other queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	 * The latter queue, in its turn, is set as shared if this is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	 * first time that the requests of some process are redirected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	 * it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	 * We redirect bfqq to new_bfqq and not the opposite, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	 * we are in the context of the process owning bfqq, thus we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	 * have the io_cq of this process. So we can immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	 * configure this io_cq to redirect the requests of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 	 * process to new_bfqq. In contrast, the io_cq of new_bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 	 * not available any more (new_bfqq->bic == NULL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	 * Anyway, even in case new_bfqq coincides with the in-service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	 * queue, redirecting requests the in-service queue is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	 * best option, as we feed the in-service queue with new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	 * requests close to the last request served and, by doing so,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	 * are likely to increase the throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	bfqq->new_bfqq = new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	 * The above assignment schedules the following redirections:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	 * each time some I/O for bfqq arrives, the process that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	 * generated that I/O is disassociated from bfqq and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	 * associated with new_bfqq. Here we increases new_bfqq->ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	 * in advance, adding the number of processes that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	 * expected to be associated with new_bfqq as they happen to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	 * issue I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 	new_bfqq->ref += process_refs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	return new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 					struct bfq_queue *new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	if (bfq_too_late_for_merging(new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	    (bfqq->ioprio_class != new_bfqq->ioprio_class))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 	 * If either of the queues has already been detected as seeky,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	 * then merging it with the other queue is unlikely to lead to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 	 * sequential I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 	if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 	 * Interleaved I/O is known to be done by (some) applications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	 * only for reads, so it does not make sense to merge async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 	 * queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 	if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572)  * Attempt to schedule a merge of bfqq with the currently in-service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)  * queue or with a close queue among the scheduled queues.  Return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574)  * NULL if no merge was scheduled, a pointer to the shared bfq_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)  * structure otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577)  * The OOM queue is not allowed to participate to cooperation: in fact, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)  * the requests temporarily redirected to the OOM queue could be redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579)  * again to dedicated queues at any time, the state needed to correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580)  * handle merging with the OOM queue would be quite complex and expensive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581)  * to maintain. Besides, in such a critical condition as an out of memory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)  * the benefits of queue merging may be little relevant, or even negligible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584)  * WARNING: queue merging may impair fairness among non-weight raised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585)  * queues, for at least two reasons: 1) the original weight of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586)  * merged queue may change during the merged state, 2) even being the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587)  * weight the same, a merged queue may be bloated with many more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588)  * requests than the ones produced by its originally-associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589)  * process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) static struct bfq_queue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 		     void *io_struct, bool request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	struct bfq_queue *in_service_bfqq, *new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	/* if a merge has already been setup, then proceed with that first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	if (bfqq->new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 		return bfqq->new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	 * Do not perform queue merging if the device is non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	 * rotational and performs internal queueing. In fact, such a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 	 * device reaches a high speed through internal parallelism
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	 * and pipelining. This means that, to reach a high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	 * throughput, it must have many requests enqueued at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	 * time. But, in this configuration, the internal scheduling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	 * algorithm of the device does exactly the job of queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 	 * merging: it reorders requests so as to obtain as much as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	 * possible a sequential I/O pattern. As a consequence, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 	 * the workload generated by processes doing interleaved I/O,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	 * the throughput reached by the device is likely to be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 	 * same, with and without queue merging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 	 * Disabling merging also provides a remarkable benefit in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	 * terms of throughput. Merging tends to make many workloads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	 * artificially more uneven, because of shared queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	 * remaining non empty for incomparably more time than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	 * non-merged queues. This may accentuate workload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 	 * asymmetries. For example, if one of the queues in a set of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	 * merged queues has a higher weight than a normal queue, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 	 * the shared queue may inherit such a high weight and, by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	 * staying almost always active, may force BFQ to perform I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	 * plugging most of the time. This evidently makes it harder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 	 * for BFQ to let the device reach a high throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	 * Finally, the likely() macro below is not used because one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 	 * of the two branches is more likely than the other, but to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 	 * have the code path after the following if() executed as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 	 * fast as possible for the case of a non rotational device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 	 * with queueing. We want it because this is the fastest kind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	 * of device. On the opposite end, the likely() may lengthen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	 * the execution time of BFQ for the case of slower devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	 * (rotational or at least without queueing). But in this case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	 * the execution time of BFQ matters very little, if not at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	 * all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	if (likely(bfqd->nonrot_with_queueing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 	 * Prevent bfqq from being merged if it has been created too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	 * long ago. The idea is that true cooperating processes, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	 * thus their associated bfq_queues, are supposed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	 * created shortly after each other. This is the case, e.g.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	 * for KVM/QEMU and dump I/O threads. Basing on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	 * assumption, the following filtering greatly reduces the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	 * probability that two non-cooperating processes, which just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	 * happen to do close I/O for some short time interval, have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	 * their queues merged by mistake.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	if (bfq_too_late_for_merging(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 	if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	/* If there is only one backlogged queue, don't search. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 	if (bfq_tot_busy_queues(bfqd) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 	in_service_bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	if (in_service_bfqq && in_service_bfqq != bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 	    likely(in_service_bfqq != &bfqd->oom_bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 	    bfq_rq_close_to_sector(io_struct, request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 				   bfqd->in_serv_last_pos) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	    bfqq->entity.parent == in_service_bfqq->entity.parent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 	    bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 		new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 		if (new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 			return new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	 * Check whether there is a cooperator among currently scheduled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	 * queues. The only thing we need is that the bio/request is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 	 * NULL, as we need it to establish whether a cooperator exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 	new_bfqq = bfq_find_close_cooperator(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 			bfq_io_struct_pos(io_struct, request));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	    bfq_may_be_close_cooperator(bfqq, new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 		return bfq_setup_merge(bfqq, new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) static void bfq_bfqq_save_state(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	struct bfq_io_cq *bic = bfqq->bic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	 * If !bfqq->bic, the queue is already shared or its requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	 * have already been redirected to a shared queue; both idle window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	 * and weight raising state have already been saved. Do nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	if (!bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	bic->saved_weight = bfqq->entity.orig_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	bic->saved_ttime = bfqq->ttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 	bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 	if (unlikely(bfq_bfqq_just_created(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 		     !bfq_bfqq_in_large_burst(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 		     bfqq->bfqd->low_latency)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 		 * bfqq being merged right after being created: bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 		 * would have deserved interactive weight raising, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		 * did not make it to be set in a weight-raised state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 		 * because of this early merge.	Store directly the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 		 * weight-raising state that would have been assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 		 * to bfqq, so that to avoid that bfqq unjustly fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 		 * to enjoy weight raising if split soon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 		bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 		bic->saved_wr_start_at_switch_to_srt = bfq_smallest_from_now();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 		bic->saved_last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 		bic->saved_wr_coeff = bfqq->wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 		bic->saved_wr_start_at_switch_to_srt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 			bfqq->wr_start_at_switch_to_srt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 		bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 		bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	 * To prevent bfqq's service guarantees from being violated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 	 * bfqq may be left busy, i.e., queued for service, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	 * empty (see comments in __bfq_bfqq_expire() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	 * details). But, if no process will send requests to bfqq any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 	 * longer, then there is no point in keeping bfqq queued for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	 * service. In addition, keeping bfqq queued for service, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	 * with no process ref any longer, may have caused bfqq to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	 * freed when dequeued from service. But this is assumed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	 * never happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	if (bfq_bfqq_busy(bfqq) && RB_EMPTY_ROOT(&bfqq->sort_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 	    bfqq != bfqd->in_service_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		bfq_del_bfqq_busy(bfqd, bfqq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 		struct bfq_queue *bfqq, struct bfq_queue *new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 		(unsigned long)new_bfqq->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	/* Save weight raising and idle window of the merged queues */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	bfq_bfqq_save_state(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	bfq_bfqq_save_state(new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 	if (bfq_bfqq_IO_bound(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 		bfq_mark_bfqq_IO_bound(new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	bfq_clear_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	 * If bfqq is weight-raised, then let new_bfqq inherit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	 * weight-raising. To reduce false positives, neglect the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	 * where bfqq has just been created, but has not yet made it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	 * to be weight-raised (which may happen because EQM may merge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	 * bfqq even before bfq_add_request is executed for the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	 * time for bfqq). Handling this case would however be very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 	 * easy, thanks to the flag just_created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		new_bfqq->wr_coeff = bfqq->wr_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 		new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		new_bfqq->wr_start_at_switch_to_srt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 			bfqq->wr_start_at_switch_to_srt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 		if (bfq_bfqq_busy(new_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 			bfqd->wr_busy_queues++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 		new_bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 		bfqq->wr_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 		bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 		if (bfq_bfqq_busy(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 			bfqd->wr_busy_queues--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 		     bfqd->wr_busy_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	 * Merge queues (that is, let bic redirect its requests to new_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	bic_set_bfqq(bic, new_bfqq, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	bfq_mark_bfqq_coop(new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	 * new_bfqq now belongs to at least two bics (it is a shared queue):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	 * set new_bfqq->bic to NULL. bfqq either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	 * - does not belong to any bic any more, and hence bfqq->bic must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	 *   be set to NULL, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	 * - is a queue whose owning bics have already been redirected to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	 *   different queue, hence the queue is destined to not belong to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	 *   any bic soon and bfqq->bic is already NULL (therefore the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	 *   assignment causes no harm).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	new_bfqq->bic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	 * If the queue is shared, the pid is the pid of one of the associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 	 * processes. Which pid depends on the exact sequence of merge events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	 * the queue underwent. So printing such a pid is useless and confusing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	 * because it reports a random pid between those of the associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	 * processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	 * We mark such a queue with a pid -1, and then print SHARED instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	 * a pid in logging messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	new_bfqq->pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	bfqq->bic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 	bfq_release_process_ref(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 				struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	bool is_sync = op_is_sync(bio->bi_opf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 	 * Disallow merge of a sync bio into an async request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 	if (is_sync && !rq_is_sync(rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 	 * Lookup the bfqq that this bio will be queued with. Allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 	 * merge only if rq is queued there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 	if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	 * We take advantage of this function to perform an early merge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 	 * of the queues of possible cooperating processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 	if (new_bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 		 * bic still points to bfqq, then it has not yet been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 		 * redirected to some other bfq_queue, and a queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 		 * merge between bfqq and new_bfqq can be safely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		 * fulfilled, i.e., bic can be redirected to new_bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		 * and bfqq can be put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 		bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 				new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 		 * If we get here, bio will be queued into new_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 		 * so use new_bfqq to decide whether bio and rq can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 		 * merged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 		bfqq = new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		 * Change also bqfd->bio_bfqq, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 		 * bfqd->bio_bic now points to new_bfqq, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 		 * this function may be invoked again (and then may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		 * use again bqfd->bio_bfqq).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 		bfqd->bio_bfqq = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	return bfqq == RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880)  * Set the maximum time for the in-service queue to consume its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881)  * budget. This prevents seeky processes from lowering the throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882)  * In practice, a time-slice service scheme is used with seeky
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883)  * processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) static void bfq_set_budget_timeout(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 				   struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 	unsigned int timeout_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 		timeout_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 		timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 	bfqd->last_budget_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	bfqq->budget_timeout = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) 		bfqd->bfq_timeout * timeout_coeff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) static void __bfq_set_in_service_queue(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 				       struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 		bfq_clear_bfqq_fifo_expire(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 		bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		if (time_is_before_jiffies(bfqq->last_wr_start_finish) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		    bfqq->wr_coeff > 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		    bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		    time_is_before_jiffies(bfqq->budget_timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 			 * For soft real-time queues, move the start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 			 * of the weight-raising period forward by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 			 * time the queue has not received any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 			 * service. Otherwise, a relatively long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 			 * service delay is likely to cause the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 			 * weight-raising period of the queue to end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 			 * because of the short duration of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 			 * weight-raising period of a soft real-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 			 * queue.  It is worth noting that this move
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 			 * is not so dangerous for the other queues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 			 * because soft real-time queues are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 			 * greedy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 			 * To not add a further variable, we use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 			 * overloaded field budget_timeout to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 			 * determine for how long the queue has not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 			 * received service, i.e., how much time has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 			 * elapsed since the queue expired. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 			 * this is a little imprecise, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 			 * budget_timeout is set to jiffies if bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 			 * not only expires, but also remains with no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 			 * request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 			if (time_after(bfqq->budget_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 				       bfqq->last_wr_start_finish))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 				bfqq->last_wr_start_finish +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 					jiffies - bfqq->budget_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 				bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 		bfq_set_budget_timeout(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 		bfq_log_bfqq(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 			     "set_in_service_queue, cur-budget = %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 			     bfqq->entity.budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	bfqd->in_service_queue = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	bfqd->in_serv_last_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956)  * Get and set a new queue for service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	struct bfq_queue *bfqq = bfq_get_next_queue(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	__bfq_set_in_service_queue(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) static void bfq_arm_slice_timer(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 	struct bfq_queue *bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 	u32 sl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 	bfq_mark_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	 * We don't want to idle for seeks, but we do want to allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 	 * fair distribution of slice time for a process doing back-to-back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 	 * seeks. So allow a little bit of time for him to submit a new rq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 	sl = bfqd->bfq_slice_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	 * Unless the queue is being weight-raised or the scenario is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 	 * asymmetric, grant only minimum idle time if the queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 	 * is seeky. A long idling is preserved for a weight-raised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	 * queue, or, more in general, in an asymmetric scenario,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 	 * because a long idling is needed for guaranteeing to a queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 	 * its reserved share of the throughput (in particular, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	 * needed if the queue has a higher weight than some other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	 * queue).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 	if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 	    !bfq_asymmetric_scenario(bfqd, bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 		sl = min_t(u64, sl, BFQ_MIN_TT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 	else if (bfqq->wr_coeff > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 		sl = max_t(u32, sl, 20ULL * NSEC_PER_MSEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	bfqd->last_idling_start = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 	bfqd->last_idling_start_jiffies = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 	hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 		      HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	bfqg_stats_set_start_idle_time(bfqq_group(bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004)  * In autotuning mode, max_budget is dynamically recomputed as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005)  * amount of sectors transferred in timeout at the estimated peak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006)  * rate. This enables BFQ to utilize a full timeslice with a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007)  * budget, even if the in-service queue is served at peak rate. And
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008)  * this maximises throughput with sequential workloads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	return (u64)bfqd->peak_rate * USEC_PER_MSEC *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017)  * Update parameters related to throughput and responsiveness, as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018)  * function of the estimated peak rate. See comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019)  * bfq_calc_max_budget(), and on the ref_wr_duration array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) static void update_thr_responsiveness_params(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 	if (bfqd->bfq_user_max_budget == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 		bfqd->bfq_max_budget =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 			bfq_calc_max_budget(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 		bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) static void bfq_reset_rate_computation(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 				       struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 	if (rq != NULL) { /* new rq dispatch now, reset accordingly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 		bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 		bfqd->peak_rate_samples = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 		bfqd->sequential_samples = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 		bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 			blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	} else /* no new rq dispatched, just reset the number of samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 		bfqd->peak_rate_samples = 0; /* full re-init on next disp. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	bfq_log(bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 		"reset_rate_computation at end, sample %u/%u tot_sects %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 		bfqd->peak_rate_samples, bfqd->sequential_samples,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 		bfqd->tot_sectors_dispatched);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	u32 rate, weight, divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	 * For the convergence property to hold (see comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	 * bfq_update_peak_rate()) and for the assessment to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	 * reliable, a minimum number of samples must be present, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	 * a minimum amount of time must have elapsed. If not so, do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 	 * not compute new rate. Just reset parameters, to get ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 	 * for a new evaluation attempt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 	if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	    bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 		goto reset_computation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	 * If a new request completion has occurred after last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 	 * dispatch, then, to approximate the rate at which requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 	 * have been served by the device, it is more precise to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	 * extend the observation interval to the last completion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	bfqd->delta_from_first =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 		max_t(u64, bfqd->delta_from_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 		      bfqd->last_completion - bfqd->first_dispatch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	 * Rate computed in sects/usec, and not sects/nsec, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	 * precision issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 			div_u64(bfqd->delta_from_first, NSEC_PER_USEC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	 * Peak rate not updated if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 	 * - the percentage of sequential dispatches is below 3/4 of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	 *   total, and rate is below the current estimated peak rate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 	 * - rate is unreasonably high (> 20M sectors/sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 	if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 	     rate <= bfqd->peak_rate) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		rate > 20<<BFQ_RATE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		goto reset_computation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 	 * We have to update the peak rate, at last! To this purpose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 	 * we use a low-pass filter. We compute the smoothing constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	 * of the filter as a function of the 'weight' of the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 	 * measured rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	 * As can be seen in next formulas, we define this weight as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 	 * quantity proportional to how sequential the workload is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 	 * and to how long the observation time interval is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 	 * The weight runs from 0 to 8. The maximum value of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 	 * weight, 8, yields the minimum value for the smoothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 	 * constant. At this minimum value for the smoothing constant,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 	 * the measured rate contributes for half of the next value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 	 * the estimated peak rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	 * So, the first step is to compute the weight as a function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	 * of how sequential the workload is. Note that the weight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	 * cannot reach 9, because bfqd->sequential_samples cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	 * become equal to bfqd->peak_rate_samples, which, in its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 	 * turn, holds true because bfqd->sequential_samples is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	 * incremented for the first sample.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 	weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 	 * Second step: further refine the weight as a function of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	 * duration of the observation interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	weight = min_t(u32, 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 		       div_u64(weight * bfqd->delta_from_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 			       BFQ_RATE_REF_INTERVAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 	 * Divisor ranging from 10, for minimum weight, to 2, for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 	 * maximum weight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	divisor = 10 - weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	 * Finally, update peak rate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	 * peak_rate = peak_rate * (divisor-1) / divisor  +  rate / divisor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	bfqd->peak_rate *= divisor-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	bfqd->peak_rate /= divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 	rate /= divisor; /* smoothing constant alpha = 1/divisor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	bfqd->peak_rate += rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	 * For a very slow device, bfqd->peak_rate can reach 0 (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	 * the minimum representable values reported in the comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	 * on BFQ_RATE_SHIFT). Push to 1 if this happens, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	 * divisions by zero where bfqd->peak_rate is used as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	 * divisor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	update_thr_responsiveness_params(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) reset_computation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	bfq_reset_rate_computation(bfqd, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158)  * Update the read/write peak rate (the main quantity used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159)  * auto-tuning, see update_thr_responsiveness_params()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161)  * It is not trivial to estimate the peak rate (correctly): because of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162)  * the presence of sw and hw queues between the scheduler and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163)  * device components that finally serve I/O requests, it is hard to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164)  * say exactly when a given dispatched request is served inside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165)  * device, and for how long. As a consequence, it is hard to know
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166)  * precisely at what rate a given set of requests is actually served
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167)  * by the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169)  * On the opposite end, the dispatch time of any request is trivially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170)  * available, and, from this piece of information, the "dispatch rate"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171)  * of requests can be immediately computed. So, the idea in the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172)  * function is to use what is known, namely request dispatch times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173)  * (plus, when useful, request completion times), to estimate what is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)  * unknown, namely in-device request service rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176)  * The main issue is that, because of the above facts, the rate at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177)  * which a certain set of requests is dispatched over a certain time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178)  * interval can vary greatly with respect to the rate at which the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179)  * same requests are then served. But, since the size of any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180)  * intermediate queue is limited, and the service scheme is lossless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181)  * (no request is silently dropped), the following obvious convergence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182)  * property holds: the number of requests dispatched MUST become
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183)  * closer and closer to the number of requests completed as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184)  * observation interval grows. This is the key property used in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)  * the next function to estimate the peak service rate as a function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186)  * of the observed dispatch rate. The function assumes to be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187)  * on every request dispatch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	u64 now_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 	if (bfqd->peak_rate_samples == 0) { /* first dispatch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 		bfq_log(bfqd, "update_peak_rate: goto reset, samples %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 			bfqd->peak_rate_samples);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 		bfq_reset_rate_computation(bfqd, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 		goto update_last_values; /* will add one sample */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	 * Device idle for very long: the observation interval lasting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	 * up to this dispatch cannot be a valid observation interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	 * for computing a new peak rate (similarly to the late-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	 * completion event in bfq_completed_request()). Go to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	 * update_rate_and_reset to have the following three steps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	 * taken:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	 * - close the observation interval at the last (previous)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	 *   request dispatch or completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 	 * - compute rate, if possible, for that observation interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	 * - start a new observation interval with this dispatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	    bfqd->rq_in_driver == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 		goto update_rate_and_reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	/* Update sampling information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	bfqd->peak_rate_samples++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	if ((bfqd->rq_in_driver > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 		now_ns - bfqd->last_completion < BFQ_MIN_TT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	    && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 		bfqd->sequential_samples++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	bfqd->tot_sectors_dispatched += blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	/* Reset max observed rq size every 32 dispatches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 	if (likely(bfqd->peak_rate_samples % 32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 		bfqd->last_rq_max_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 			max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 		bfqd->last_rq_max_size = blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 	bfqd->delta_from_first = now_ns - bfqd->first_dispatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 	/* Target observation interval not yet reached, go on sampling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 		goto update_last_values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) update_rate_and_reset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 	bfq_update_rate_reset(bfqd, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) update_last_values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) 	if (RQ_BFQQ(rq) == bfqd->in_service_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 		bfqd->in_serv_last_pos = bfqd->last_position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	bfqd->last_dispatch = now_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249)  * Remove request from internal lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) static void bfq_dispatch_remove(struct request_queue *q, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 	 * For consistency, the next instruction should have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 	 * executed after removing the request from the queue and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	 * dispatching it.  We execute instead this instruction before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	 * bfq_remove_request() (and hence introduce a temporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	 * inconsistency), for efficiency.  In fact, should this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	 * dispatch occur for a non in-service bfqq, this anticipated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	 * increment prevents two counters related to bfqq->dispatched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	 * from risking to be, first, uselessly decremented, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	 * incremented again when the (new) value of bfqq->dispatched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	 * happens to be taken into account.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 	bfqq->dispatched++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	bfq_update_peak_rate(q->elevator->elevator_data, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	bfq_remove_request(q, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274)  * There is a case where idling does not have to be performed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275)  * throughput concerns, but to preserve the throughput share of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276)  * the process associated with bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278)  * To introduce this case, we can note that allowing the drive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279)  * to enqueue more than one request at a time, and hence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280)  * delegating de facto final scheduling decisions to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281)  * drive's internal scheduler, entails loss of control on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282)  * actual request service order. In particular, the critical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283)  * situation is when requests from different processes happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284)  * to be present, at the same time, in the internal queue(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285)  * of the drive. In such a situation, the drive, by deciding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286)  * the service order of the internally-queued requests, does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287)  * determine also the actual throughput distribution among
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288)  * these processes. But the drive typically has no notion or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289)  * concern about per-process throughput distribution, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290)  * makes its decisions only on a per-request basis. Therefore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291)  * the service distribution enforced by the drive's internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292)  * scheduler is likely to coincide with the desired throughput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293)  * distribution only in a completely symmetric, or favorably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294)  * skewed scenario where:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295)  * (i-a) each of these processes must get the same throughput as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296)  *	 the others,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297)  * (i-b) in case (i-a) does not hold, it holds that the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298)  *       associated with bfqq must receive a lower or equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299)  *	 throughput than any of the other processes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300)  * (ii)  the I/O of each process has the same properties, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301)  *       terms of locality (sequential or random), direction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302)  *       (reads or writes), request sizes, greediness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303)  *       (from I/O-bound to sporadic), and so on;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305)  * In fact, in such a scenario, the drive tends to treat the requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306)  * of each process in about the same way as the requests of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307)  * others, and thus to provide each of these processes with about the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308)  * same throughput.  This is exactly the desired throughput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309)  * distribution if (i-a) holds, or, if (i-b) holds instead, this is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310)  * even more convenient distribution for (the process associated with)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311)  * bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313)  * In contrast, in any asymmetric or unfavorable scenario, device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314)  * idling (I/O-dispatch plugging) is certainly needed to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315)  * that bfqq receives its assigned fraction of the device throughput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316)  * (see [1] for details).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318)  * The problem is that idling may significantly reduce throughput with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319)  * certain combinations of types of I/O and devices. An important
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320)  * example is sync random I/O on flash storage with command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321)  * queueing. So, unless bfqq falls in cases where idling also boosts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322)  * throughput, it is important to check conditions (i-a), i(-b) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323)  * (ii) accurately, so as to avoid idling when not strictly needed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324)  * service guarantees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326)  * Unfortunately, it is extremely difficult to thoroughly check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327)  * condition (ii). And, in case there are active groups, it becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328)  * very difficult to check conditions (i-a) and (i-b) too.  In fact,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329)  * if there are active groups, then, for conditions (i-a) or (i-b) to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330)  * become false 'indirectly', it is enough that an active group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331)  * contains more active processes or sub-groups than some other active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332)  * group. More precisely, for conditions (i-a) or (i-b) to become
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333)  * false because of such a group, it is not even necessary that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334)  * group is (still) active: it is sufficient that, even if the group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335)  * has become inactive, some of its descendant processes still have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336)  * some request already dispatched but still waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337)  * completion. In fact, requests have still to be guaranteed their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338)  * share of the throughput even after being dispatched. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339)  * respect, it is easy to show that, if a group frequently becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340)  * inactive while still having in-flight requests, and if, when this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341)  * happens, the group is not considered in the calculation of whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342)  * the scenario is asymmetric, then the group may fail to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343)  * guaranteed its fair share of the throughput (basically because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344)  * idling may not be performed for the descendant processes of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345)  * group, but it had to be).  We address this issue with the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346)  * bi-modal behavior, implemented in the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347)  * bfq_asymmetric_scenario().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349)  * If there are groups with requests waiting for completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350)  * (as commented above, some of these groups may even be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351)  * already inactive), then the scenario is tagged as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352)  * asymmetric, conservatively, without checking any of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353)  * conditions (i-a), (i-b) or (ii). So the device is idled for bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354)  * This behavior matches also the fact that groups are created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355)  * exactly if controlling I/O is a primary concern (to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356)  * preserve bandwidth and latency guarantees).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358)  * On the opposite end, if there are no groups with requests waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359)  * for completion, then only conditions (i-a) and (i-b) are actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360)  * controlled, i.e., provided that conditions (i-a) or (i-b) holds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361)  * idling is not performed, regardless of whether condition (ii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362)  * holds.  In other words, only if conditions (i-a) and (i-b) do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363)  * hold, then idling is allowed, and the device tends to be prevented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364)  * from queueing many requests, possibly of several processes. Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365)  * there are no groups with requests waiting for completion, then, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366)  * control conditions (i-a) and (i-b) it is enough to check just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367)  * whether all the queues with requests waiting for completion also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368)  * have the same weight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370)  * Not checking condition (ii) evidently exposes bfqq to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371)  * risk of getting less throughput than its fair share.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372)  * However, for queues with the same weight, a further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373)  * mechanism, preemption, mitigates or even eliminates this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374)  * problem. And it does so without consequences on overall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375)  * throughput. This mechanism and its benefits are explained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376)  * in the next three paragraphs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378)  * Even if a queue, say Q, is expired when it remains idle, Q
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379)  * can still preempt the new in-service queue if the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380)  * request of Q arrives soon (see the comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381)  * bfq_bfqq_update_budg_for_activation). If all queues and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382)  * groups have the same weight, this form of preemption,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383)  * combined with the hole-recovery heuristic described in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384)  * comments on function bfq_bfqq_update_budg_for_activation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385)  * are enough to preserve a correct bandwidth distribution in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386)  * the mid term, even without idling. In fact, even if not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387)  * idling allows the internal queues of the device to contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388)  * many requests, and thus to reorder requests, we can rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389)  * safely assume that the internal scheduler still preserves a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390)  * minimum of mid-term fairness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392)  * More precisely, this preemption-based, idleless approach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393)  * provides fairness in terms of IOPS, and not sectors per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394)  * second. This can be seen with a simple example. Suppose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395)  * that there are two queues with the same weight, but that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396)  * the first queue receives requests of 8 sectors, while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397)  * second queue receives requests of 1024 sectors. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)  * addition, suppose that each of the two queues contains at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399)  * most one request at a time, which implies that each queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400)  * always remains idle after it is served. Finally, after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401)  * remaining idle, each queue receives very quickly a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402)  * request. It follows that the two queues are served
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403)  * alternatively, preempting each other if needed. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404)  * implies that, although both queues have the same weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)  * the queue with large requests receives a service that is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406)  * 1024/8 times as high as the service received by the other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407)  * queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409)  * The motivation for using preemption instead of idling (for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410)  * queues with the same weight) is that, by not idling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411)  * service guarantees are preserved (completely or at least in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412)  * part) without minimally sacrificing throughput. And, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413)  * there is no active group, then the primary expectation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414)  * this device is probably a high throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416)  * We are now left only with explaining the two sub-conditions in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417)  * additional compound condition that is checked below for deciding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418)  * whether the scenario is asymmetric. To explain the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419)  * sub-condition, we need to add that the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420)  * bfq_asymmetric_scenario checks the weights of only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421)  * non-weight-raised queues, for efficiency reasons (see comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422)  * bfq_weights_tree_add()). Then the fact that bfqq is weight-raised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423)  * is checked explicitly here. More precisely, the compound condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424)  * below takes into account also the fact that, even if bfqq is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425)  * weight-raised, the scenario is still symmetric if all queues with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426)  * requests waiting for completion happen to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427)  * weight-raised. Actually, we should be even more precise here, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428)  * differentiate between interactive weight raising and soft real-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429)  * weight raising.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431)  * The second sub-condition checked in the compound condition is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432)  * whether there is a fair amount of already in-flight I/O not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433)  * belonging to bfqq. If so, I/O dispatching is to be plugged, for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434)  * following reason. The drive may decide to serve in-flight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435)  * non-bfqq's I/O requests before bfqq's ones, thereby delaying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436)  * arrival of new I/O requests for bfqq (recall that bfqq is sync). If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437)  * I/O-dispatching is not plugged, then, while bfqq remains empty, a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438)  * basically uncontrolled amount of I/O from other queues may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439)  * dispatched too, possibly causing the service of bfqq's I/O to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440)  * delayed even longer in the drive. This problem gets more and more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441)  * serious as the speed and the queue depth of the drive grow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442)  * because, as these two quantities grow, the probability to find no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443)  * queue busy but many requests in flight grows too. By contrast,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444)  * plugging I/O dispatching minimizes the delay induced by already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445)  * in-flight I/O, and enables bfqq to recover the bandwidth it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446)  * lose because of this delay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448)  * As a side note, it is worth considering that the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449)  * device-idling countermeasures may however fail in the following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450)  * unlucky scenario: if I/O-dispatch plugging is (correctly) disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451)  * in a time period during which all symmetry sub-conditions hold, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452)  * therefore the device is allowed to enqueue many requests, but at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453)  * some later point in time some sub-condition stops to hold, then it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454)  * may become impossible to make requests be served in the desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455)  * order until all the requests already queued in the device have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456)  * served. The last sub-condition commented above somewhat mitigates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457)  * this problem for weight-raised queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) static bool idling_needed_for_service_guarantees(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 						 struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	/* No point in idling for bfqq if it won't get requests any longer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	if (unlikely(!bfqq_process_refs(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 	return (bfqq->wr_coeff > 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		(bfqd->wr_busy_queues <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 		 bfq_tot_busy_queues(bfqd) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 		 bfqd->rq_in_driver >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 		 bfqq->dispatched + 4)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 		bfq_asymmetric_scenario(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) static bool __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 			      enum bfqq_expiration reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	 * If this bfqq is shared between multiple processes, check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 	 * to make sure that those processes are still issuing I/Os
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	 * within the mean seek distance. If not, it may be time to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 	 * break the queues apart again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 	if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 		bfq_mark_bfqq_split_coop(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	 * Consider queues with a higher finish virtual time than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	 * bfqq. If idling_needed_for_service_guarantees(bfqq) returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	 * true, then bfqq's bandwidth would be violated if an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	 * uncontrolled amount of I/O from these queues were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	 * dispatched while bfqq is waiting for its new I/O to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	 * arrive. This is exactly what may happen if this is a forced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	 * expiration caused by a preemption attempt, and if bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	 * not re-scheduled. To prevent this from happening, re-queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 	 * bfqq if it needs I/O-dispatch plugging, even if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 	 * empty. By doing so, bfqq is granted to be served before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 	 * above queues (provided that bfqq is of course eligible).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 	if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 	    !(reason == BFQQE_PREEMPTED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 	      idling_needed_for_service_guarantees(bfqd, bfqq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 		if (bfqq->dispatched == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 			 * Overloading budget_timeout field to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 			 * the time at which the queue remains with no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 			 * backlog and no outstanding request; used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 			 * the weight-raising mechanism.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 			bfqq->budget_timeout = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 		bfq_del_bfqq_busy(bfqd, bfqq, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		bfq_requeue_bfqq(bfqd, bfqq, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 		 * Resort priority tree of potential close cooperators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 		 * See comments on bfq_pos_tree_add_move() for the unlikely().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 		if (unlikely(!bfqd->nonrot_with_queueing &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 			     !RB_EMPTY_ROOT(&bfqq->sort_list)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 			bfq_pos_tree_add_move(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 	 * All in-service entities must have been properly deactivated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 	 * or requeued before executing the next function, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 	 * resets all in-service entities as no more in service. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 	 * may cause bfqq to be freed. If this happens, the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 	 * function returns true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 	return __bfq_bfqd_reset_in_service(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)  * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535)  * @bfqd: device data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536)  * @bfqq: queue to update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537)  * @reason: reason for expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539)  * Handle the feedback on @bfqq budget at queue expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540)  * See the body for detailed comments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 				     struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 				     enum bfqq_expiration reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	struct request *next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	int budget, min_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 	min_budget = bfq_min_budget(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	if (bfqq->wr_coeff == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 		budget = bfqq->max_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	else /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 	      * Use a constant, low budget for weight-raised queues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	      * to help achieve a low latency. Keep it slightly higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 	      * than the minimum possible budget, to cause a little
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 	      * bit fewer expirations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) 	      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 		budget = 2 * min_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 		bfqq->entity.budget, bfq_bfqq_budget_left(bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 		budget, bfq_min_budget(bfqd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 		bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 	if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 		switch (reason) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 		 * Caveat: in all the following cases we trade latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 		 * for throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 		case BFQQE_TOO_IDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 			 * This is the only case where we may reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 			 * the budget: if there is no request of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 			 * process still waiting for completion, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 			 * we assume (tentatively) that the timer has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 			 * expired because the batch of requests of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 			 * the process could have been served with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 			 * smaller budget.  Hence, betting that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 			 * process will behave in the same way when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 			 * becomes backlogged again, we reduce its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 			 * next budget.  As long as we guess right,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 			 * this budget cut reduces the latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 			 * experienced by the process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 			 * However, if there are still outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 			 * requests, then the process may have not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 			 * issued its next request just because it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 			 * still waiting for the completion of some of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 			 * the still outstanding ones.  So in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 			 * subcase we do not reduce its budget, on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 			 * contrary we increase it to possibly boost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 			 * the throughput, as discussed in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 			 * comments to the BUDGET_TIMEOUT case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 			if (bfqq->dispatched > 0) /* still outstanding reqs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) 				budget = min(budget * 2, bfqd->bfq_max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 				if (budget > 5 * min_budget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 					budget -= 4 * min_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 					budget = min_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 		case BFQQE_BUDGET_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 			 * We double the budget here because it gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 			 * the chance to boost the throughput if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 			 * is not a seeky process (and has bumped into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 			 * this timeout because of, e.g., ZBR).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 			budget = min(budget * 2, bfqd->bfq_max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 		case BFQQE_BUDGET_EXHAUSTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 			 * The process still has backlog, and did not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 			 * let either the budget timeout or the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 			 * idling timeout expire. Hence it is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 			 * seeky, has a short thinktime and may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 			 * happy with a higher budget too. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 			 * definitely increase the budget of this good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 			 * candidate to boost the disk throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 			budget = min(budget * 4, bfqd->bfq_max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		case BFQQE_NO_MORE_REQUESTS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 			 * For queues that expire for this reason, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 			 * is particularly important to keep the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 			 * budget close to the actual service they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 			 * need. Doing so reduces the timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 			 * misalignment problem described in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 			 * comments in the body of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 			 * __bfq_activate_entity. In fact, suppose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 			 * that a queue systematically expires for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 			 * BFQQE_NO_MORE_REQUESTS and presents a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 			 * new request in time to enjoy timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 			 * back-shifting. The larger the budget of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 			 * queue is with respect to the service the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 			 * queue actually requests in each service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 			 * slot, the more times the queue can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 			 * reactivated with the same virtual finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 			 * time. It follows that, even if this finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 			 * time is pushed to the system virtual time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 			 * to reduce the consequent timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 			 * misalignment, the queue unjustly enjoys for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 			 * many re-activations a lower finish time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 			 * than all newly activated queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 			 * The service needed by bfqq is measured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 			 * quite precisely by bfqq->entity.service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 			 * Since bfqq does not enjoy device idling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 			 * bfqq->entity.service is equal to the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 			 * of sectors that the process associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 			 * bfqq requested to read/write before waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 			 * for request completions, or blocking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 			 * other reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 			budget = max_t(int, bfqq->entity.service, min_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 	} else if (!bfq_bfqq_sync(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 		 * Async queues get always the maximum possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		 * budget, as for them we do not care about latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 		 * (in addition, their ability to dispatch is limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 		 * by the charging factor).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 		budget = bfqd->bfq_max_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 	bfqq->max_budget = budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 	if (bfqd->budgets_assigned >= bfq_stats_min_budgets &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 	    !bfqd->bfq_user_max_budget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 		bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 	 * If there is still backlog, then assign a new budget, making
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 	 * sure that it is large enough for the next request.  Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 	 * the finish time of bfqq must be kept in sync with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 	 * budget, be sure to call __bfq_bfqq_expire() *after* this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 	 * update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 	 * If there is no backlog, then no need to update the budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 	 * it will be updated on the arrival of a new request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 	next_rq = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 	if (next_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 		bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 					    bfq_serv_to_charge(next_rq, bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 	bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 			next_rq ? blk_rq_sectors(next_rq) : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 			bfqq->entity.budget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704)  * Return true if the process associated with bfqq is "slow". The slow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705)  * flag is used, in addition to the budget timeout, to reduce the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706)  * amount of service provided to seeky processes, and thus reduce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707)  * their chances to lower the throughput. More details in the comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708)  * on the function bfq_bfqq_expire().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710)  * An important observation is in order: as discussed in the comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711)  * on the function bfq_update_peak_rate(), with devices with internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)  * queues, it is hard if ever possible to know when and for how long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713)  * an I/O request is processed by the device (apart from the trivial
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714)  * I/O pattern where a new request is dispatched only after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715)  * previous one has been completed). This makes it hard to evaluate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716)  * the real rate at which the I/O requests of each bfq_queue are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717)  * served.  In fact, for an I/O scheduler like BFQ, serving a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718)  * bfq_queue means just dispatching its requests during its service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719)  * slot (i.e., until the budget of the queue is exhausted, or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720)  * queue remains idle, or, finally, a timeout fires). But, during the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721)  * service slot of a bfq_queue, around 100 ms at most, the device may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722)  * be even still processing requests of bfq_queues served in previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723)  * service slots. On the opposite end, the requests of the in-service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724)  * bfq_queue may be completed after the service slot of the queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725)  * finishes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727)  * Anyway, unless more sophisticated solutions are used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728)  * (where possible), the sum of the sizes of the requests dispatched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729)  * during the service slot of a bfq_queue is probably the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730)  * approximation available for the service received by the bfq_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731)  * during its service slot. And this sum is the quantity used in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732)  * function to evaluate the I/O speed of a process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 				 bool compensate, enum bfqq_expiration reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 				 unsigned long *delta_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 	ktime_t delta_ktime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	u32 delta_usecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 	bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 	if (!bfq_bfqq_sync(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	if (compensate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 		delta_ktime = bfqd->last_idling_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 		delta_ktime = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 	delta_usecs = ktime_to_us(delta_ktime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 	/* don't use too short time intervals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	if (delta_usecs < 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 		if (blk_queue_nonrot(bfqd->queue))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 			 /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 			  * give same worst-case guarantees as idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 			  * for seeky
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 			  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 			*delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 		else /* charge at least one seek */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 			*delta_ms = bfq_slice_idle / NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 		return slow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	*delta_ms = delta_usecs / USEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 	 * Use only long (> 20ms) intervals to filter out excessive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) 	 * spikes in service rate estimation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	if (delta_usecs > 20000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 		 * Caveat for rotational devices: processes doing I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 		 * in the slower disk zones tend to be slow(er) even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 		 * if not seeky. In this respect, the estimated peak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 		 * rate is likely to be an average over the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 		 * surface. Accordingly, to not be too harsh with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 		 * unlucky processes, a process is deemed slow only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 		 * its rate has been lower than half of the estimated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 		 * peak rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 		slow = bfqq->entity.service < bfqd->bfq_max_budget / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	return slow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792)  * To be deemed as soft real-time, an application must meet two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793)  * requirements. First, the application must not require an average
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794)  * bandwidth higher than the approximate bandwidth required to playback or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795)  * record a compressed high-definition video.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796)  * The next function is invoked on the completion of the last request of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797)  * batch, to compute the next-start time instant, soft_rt_next_start, such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798)  * that, if the next request of the application does not arrive before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799)  * soft_rt_next_start, then the above requirement on the bandwidth is met.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801)  * The second requirement is that the request pattern of the application is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802)  * isochronous, i.e., that, after issuing a request or a batch of requests,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803)  * the application stops issuing new requests until all its pending requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804)  * have been completed. After that, the application may issue a new batch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805)  * and so on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806)  * For this reason the next function is invoked to compute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807)  * soft_rt_next_start only for applications that meet this requirement,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808)  * whereas soft_rt_next_start is set to infinity for applications that do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809)  * not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811)  * Unfortunately, even a greedy (i.e., I/O-bound) application may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812)  * happen to meet, occasionally or systematically, both the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813)  * bandwidth and isochrony requirements. This may happen at least in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814)  * the following circumstances. First, if the CPU load is high. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815)  * application may stop issuing requests while the CPUs are busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816)  * serving other processes, then restart, then stop again for a while,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817)  * and so on. The other circumstances are related to the storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818)  * device: the storage device is highly loaded or reaches a low-enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819)  * throughput with the I/O of the application (e.g., because the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820)  * is random and/or the device is slow). In all these cases, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821)  * I/O of the application may be simply slowed down enough to meet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822)  * the bandwidth and isochrony requirements. To reduce the probability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823)  * that greedy applications are deemed as soft real-time in these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824)  * corner cases, a further rule is used in the computation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825)  * soft_rt_next_start: the return value of this function is forced to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826)  * be higher than the maximum between the following two quantities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828)  * (a) Current time plus: (1) the maximum time for which the arrival
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829)  *     of a request is waited for when a sync queue becomes idle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830)  *     namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831)  *     postpone for a moment the reason for adding a few extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832)  *     jiffies; we get back to it after next item (b).  Lower-bounding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833)  *     the return value of this function with the current time plus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834)  *     bfqd->bfq_slice_idle tends to filter out greedy applications,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835)  *     because the latter issue their next request as soon as possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836)  *     after the last one has been completed. In contrast, a soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837)  *     real-time application spends some time processing data, after a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838)  *     batch of its requests has been completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)  * (b) Current value of bfqq->soft_rt_next_start. As pointed out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841)  *     above, greedy applications may happen to meet both the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842)  *     bandwidth and isochrony requirements under heavy CPU or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843)  *     storage-device load. In more detail, in these scenarios, these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844)  *     applications happen, only for limited time periods, to do I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845)  *     slowly enough to meet all the requirements described so far,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846)  *     including the filtering in above item (a). These slow-speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847)  *     time intervals are usually interspersed between other time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848)  *     intervals during which these applications do I/O at a very high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849)  *     speed. Fortunately, exactly because of the high speed of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850)  *     I/O in the high-speed intervals, the values returned by this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851)  *     function happen to be so high, near the end of any such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852)  *     high-speed interval, to be likely to fall *after* the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853)  *     the low-speed time interval that follows. These high values are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854)  *     stored in bfqq->soft_rt_next_start after each invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855)  *     this function. As a consequence, if the last value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856)  *     bfqq->soft_rt_next_start is constantly used to lower-bound the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857)  *     next value that this function may return, then, from the very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858)  *     beginning of a low-speed interval, bfqq->soft_rt_next_start is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859)  *     likely to be constantly kept so high that any I/O request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860)  *     issued during the low-speed interval is considered as arriving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861)  *     to soon for the application to be deemed as soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862)  *     real-time. Then, in the high-speed interval that follows, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863)  *     application will not be deemed as soft real-time, just because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864)  *     it will do I/O at a high speed. And so on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866)  * Getting back to the filtering in item (a), in the following two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867)  * cases this filtering might be easily passed by a greedy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868)  * application, if the reference quantity was just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869)  * bfqd->bfq_slice_idle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870)  * 1) HZ is so low that the duration of a jiffy is comparable to or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871)  *    higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872)  *    devices with HZ=100. The time granularity may be so coarse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873)  *    that the approximation, in jiffies, of bfqd->bfq_slice_idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874)  *    is rather lower than the exact value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875)  * 2) jiffies, instead of increasing at a constant rate, may stop increasing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876)  *    for a while, then suddenly 'jump' by several units to recover the lost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877)  *    increments. This seems to happen, e.g., inside virtual machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878)  * To address this issue, in the filtering in (a) we do not use as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879)  * reference time interval just bfqd->bfq_slice_idle, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880)  * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881)  * minimum number of jiffies for which the filter seems to be quite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882)  * precise also in embedded systems and KVM/QEMU virtual machines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 						struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 	return max3(bfqq->soft_rt_next_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 		    bfqq->last_idle_bklogged +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 		    HZ * bfqq->service_from_backlogged /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 		    bfqd->bfq_wr_max_softrt_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 		    jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)  * bfq_bfqq_expire - expire a queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896)  * @bfqd: device owning the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897)  * @bfqq: the queue to expire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898)  * @compensate: if true, compensate for the time spent idling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899)  * @reason: the reason causing the expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901)  * If the process associated with bfqq does slow I/O (e.g., because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902)  * issues random requests), we charge bfqq with the time it has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903)  * in service instead of the service it has received (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904)  * bfq_bfqq_charge_time for details on how this goal is achieved). As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905)  * a consequence, bfqq will typically get higher timestamps upon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906)  * reactivation, and hence it will be rescheduled as if it had
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907)  * received more service than what it has actually received. In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908)  * end, bfqq receives less service in proportion to how slowly its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909)  * associated process consumes its budgets (and hence how seriously it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910)  * tends to lower the throughput). In addition, this time-charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911)  * strategy guarantees time fairness among slow processes. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912)  * contrast, if the process associated with bfqq is not slow, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913)  * charge bfqq exactly with the service it has received.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915)  * Charging time to the first type of queues and the exact service to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916)  * the other has the effect of using the WF2Q+ policy to schedule the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917)  * former on a timeslice basis, without violating service domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918)  * guarantees among the latter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) void bfq_bfqq_expire(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 		     struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 		     bool compensate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 		     enum bfqq_expiration reason)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 	bool slow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 	unsigned long delta = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 	 * Check whether the process is slow (see bfq_bfqq_is_slow).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 	 * As above explained, charge slow (typically seeky) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	 * timed-out queues with the time and not the service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	 * received, to favor sequential workloads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	 * Processes doing I/O in the slower disk zones will tend to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 	 * be slow(er) even if not seeky. Therefore, since the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 	 * estimated peak rate is actually an average over the disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	 * surface, these processes may timeout just for bad luck. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 	 * avoid punishing them, do not charge time to processes that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 	 * succeeded in consuming at least 2/3 of their budget. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	 * allows BFQ to preserve enough elasticity to still perform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	 * bandwidth, and not time, distribution with little unlucky
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 	 * or quasi-sequential processes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	if (bfqq->wr_coeff == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 	    (slow ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 	     (reason == BFQQE_BUDGET_TIMEOUT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	      bfq_bfqq_budget_left(bfqq) >=  entity->budget / 3)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 		bfq_bfqq_charge_time(bfqd, bfqq, delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	if (reason == BFQQE_TOO_IDLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	    entity->service <= 2 * entity->budget / 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 		bfq_clear_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	if (bfqd->low_latency && bfqq->wr_coeff == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 		bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 	if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 	    RB_EMPTY_ROOT(&bfqq->sort_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 		 * If we get here, and there are no outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 		 * requests, then the request pattern is isochronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		 * (see the comments on the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 		 * bfq_bfqq_softrt_next_start()). Thus we can compute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		 * soft_rt_next_start. And we do it, unless bfqq is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 		 * interactive weight raising. We do not do it in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		 * latter subcase, for the following reason. bfqq may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 		 * be conveying the I/O needed to load a soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 		 * real-time application. Such an application will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 		 * actually exhibit a soft real-time I/O pattern after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 		 * it finally starts doing its job. But, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 		 * soft_rt_next_start is computed here for an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 		 * interactive bfqq, and bfqq had received a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) 		 * service before remaining with no outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 		 * request (likely to happen on a fast device), then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) 		 * soft_rt_next_start would be assigned such a high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 		 * value that, for a very long time, bfqq would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 		 * prevented from being possibly considered as soft
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 		 * real time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 		 * If, instead, the queue still has outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 		 * requests, then we have to wait for the completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 		 * of all the outstanding requests to discover whether
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 		 * the request pattern is actually isochronous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 		if (bfqq->dispatched == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 		    bfqq->wr_coeff != bfqd->bfq_wr_coeff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 			bfqq->soft_rt_next_start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 				bfq_bfqq_softrt_next_start(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 		else if (bfqq->dispatched > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 			 * Schedule an update of soft_rt_next_start to when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 			 * the task may be discovered to be isochronous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 			bfq_mark_bfqq_softrt_update(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	bfq_log_bfqq(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 		"expire (%d, slow %d, num_disp %d, short_ttime %d)", reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 		slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 	 * bfqq expired, so no total service time needs to be computed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 	 * any longer: reset state machine for measuring total service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 	 * times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	bfqd->rqs_injected = bfqd->wait_dispatch = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 	bfqd->waited_rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	 * Increase, decrease or leave budget unchanged according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	 * reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 	__bfq_bfqq_recalc_budget(bfqd, bfqq, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 	if (__bfq_bfqq_expire(bfqd, bfqq, reason))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 		/* bfqq is gone, no more actions on it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 	/* mark bfqq as waiting a request only if a bic still points to it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	if (!bfq_bfqq_busy(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 	    reason != BFQQE_BUDGET_TIMEOUT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) 	    reason != BFQQE_BUDGET_EXHAUSTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 		bfq_mark_bfqq_non_blocking_wait_rq(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 		 * Not setting service to 0, because, if the next rq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 		 * arrives in time, the queue will go on receiving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 		 * service with this same budget (as if it never expired)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 		entity->service = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 	 * Reset the received-service counter for every parent entity.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 	 * Differently from what happens with bfqq->entity.service,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 	 * the resetting of this counter never needs to be postponed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	 * for parent entities. In fact, in case bfqq may have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	 * chance to go on being served using the last, partially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 	 * consumed budget, bfqq->entity.service needs to be kept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 	 * because if bfqq then actually goes on being served using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	 * the same budget, the last value of bfqq->entity.service is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 	 * needed to properly decrement bfqq->entity.budget by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	 * portion already consumed. In contrast, it is not necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 	 * to keep entity->service for parent entities too, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	 * the bubble up of the new value of bfqq->entity.budget will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 	 * make sure that the budgets of parent entities are correct,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 	 * even in case bfqq and thus parent entities go on receiving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 	 * service with the same budget.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 	entity = entity->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 	for_each_entity(entity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 		entity->service = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060)  * Budget timeout is not implemented through a dedicated timer, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061)  * just checked on request arrivals and completions, as well as on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062)  * idle timer expirations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 	return time_is_before_eq_jiffies(bfqq->budget_timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070)  * If we expire a queue that is actively waiting (i.e., with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071)  * device idled) for the arrival of a new request, then we may incur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072)  * the timestamp misalignment problem described in the body of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073)  * function __bfq_activate_entity. Hence we return true only if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074)  * condition does not hold, or if the queue is slow enough to deserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075)  * only to be kicked off for preserving a high throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 	bfq_log_bfqq(bfqq->bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 		"may_budget_timeout: wait_request %d left %d timeout %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 		bfq_bfqq_wait_request(bfqq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 			bfq_bfqq_budget_left(bfqq) >=  bfqq->entity.budget / 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 		bfq_bfqq_budget_timeout(bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 	return (!bfq_bfqq_wait_request(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 		bfq_bfqq_budget_left(bfqq) >=  bfqq->entity.budget / 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 		&&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 		bfq_bfqq_budget_timeout(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) static bool idling_boosts_thr_without_issues(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 					     struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 	bool rot_without_queueing =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 		!blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 		bfqq_sequential_and_IO_bound,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 		idling_boosts_thr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 	/* No point in idling for bfqq if it won't get requests any longer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 	if (unlikely(!bfqq_process_refs(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	bfqq_sequential_and_IO_bound = !BFQQ_SEEKY(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 		bfq_bfqq_IO_bound(bfqq) && bfq_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 	 * The next variable takes into account the cases where idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 	 * boosts the throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 	 * The value of the variable is computed considering, first, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 	 * idling is virtually always beneficial for the throughput if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 	 * (a) the device is not NCQ-capable and rotational, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 	 * (b) regardless of the presence of NCQ, the device is rotational and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 	 *     the request pattern for bfqq is I/O-bound and sequential, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 	 * (c) regardless of whether it is rotational, the device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 	 *     not NCQ-capable and the request pattern for bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 	 *     I/O-bound and sequential.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 	 * Secondly, and in contrast to the above item (b), idling an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 	 * NCQ-capable flash-based device would not boost the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	 * throughput even with sequential I/O; rather it would lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 	 * the throughput in proportion to how fast the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 	 * is. Accordingly, the next variable is true if any of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	 * above conditions (a), (b) or (c) is true, and, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 	 * particular, happens to be false if bfqd is an NCQ-capable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	 * flash-based device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	idling_boosts_thr = rot_without_queueing ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 		((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 		 bfqq_sequential_and_IO_bound);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	 * The return value of this function is equal to that of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 	 * idling_boosts_thr, unless a special case holds. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	 * special case, described below, idling may cause problems to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 	 * weight-raised queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 	 * When the request pool is saturated (e.g., in the presence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 	 * of write hogs), if the processes associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 	 * non-weight-raised queues ask for requests at a lower rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 	 * then processes associated with weight-raised queues have a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 	 * higher probability to get a request from the pool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	 * immediately (or at least soon) when they need one. Thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 	 * they have a higher probability to actually get a fraction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	 * of the device throughput proportional to their high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 	 * weight. This is especially true with NCQ-capable drives,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 	 * which enqueue several requests in advance, and further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 	 * reorder internally-queued requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	 * For this reason, we force to false the return value if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 	 * there are weight-raised busy queues. In this case, and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 	 * bfqq is not weight-raised, this guarantees that the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 	 * is not idled for bfqq (if, instead, bfqq is weight-raised,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 	 * then idling will be guaranteed by another variable, see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 	 * below). Combined with the timestamping rules of BFQ (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 	 * [1] for details), this behavior causes bfqq, and hence any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 	 * sync non-weight-raised queue, to get a lower number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	 * requests served, and thus to ask for a lower number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	 * requests from the request pool, before the busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 	 * weight-raised queues get served again. This often mitigates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	 * starvation problems in the presence of heavy write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 	 * workloads and NCQ, thereby guaranteeing a higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 	 * application and system responsiveness in these hostile
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	 * scenarios.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 	return idling_boosts_thr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 		bfqd->wr_busy_queues == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171)  * For a queue that becomes empty, device idling is allowed only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172)  * this function returns true for that queue. As a consequence, since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173)  * device idling plays a critical role for both throughput boosting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174)  * and service guarantees, the return value of this function plays a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175)  * critical role as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177)  * In a nutshell, this function returns true only if idling is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178)  * beneficial for throughput or, even if detrimental for throughput,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179)  * idling is however necessary to preserve service guarantees (low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180)  * latency, desired throughput distribution, ...). In particular, on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181)  * NCQ-capable devices, this function tries to return false, so as to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182)  * help keep the drives' internal queues full, whenever this helps the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183)  * device boost the throughput without causing any service-guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184)  * issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186)  * Most of the issues taken into account to get the return value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187)  * this function are not trivial. We discuss these issues in the two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188)  * functions providing the main pieces of information needed by this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189)  * function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) static bool bfq_better_to_idle(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 	struct bfq_data *bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	bool idling_boosts_thr_with_no_issue, idling_needed_for_service_guar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	/* No point in idling for bfqq if it won't get requests any longer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 	if (unlikely(!bfqq_process_refs(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 	if (unlikely(bfqd->strict_guarantees))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	 * Idling is performed only if slice_idle > 0. In addition, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	 * do not idle if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 	 * (a) bfqq is async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 	 * (b) bfqq is in the idle io prio class: in this case we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 	 * not idle because we want to minimize the bandwidth that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) 	 * queues in this class can steal to higher-priority queues
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 	   bfq_class_idle(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 	idling_boosts_thr_with_no_issue =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 		idling_boosts_thr_without_issues(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	idling_needed_for_service_guar =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 		idling_needed_for_service_guarantees(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 	 * We have now the two components we need to compute the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 	 * return value of the function, which is true only if idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 	 * either boosts the throughput (without issues), or is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 	 * necessary to preserve service guarantees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	return idling_boosts_thr_with_no_issue ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 		idling_needed_for_service_guar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232)  * If the in-service queue is empty but the function bfq_better_to_idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233)  * returns true, then:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234)  * 1) the queue must remain in service and cannot be expired, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235)  * 2) the device must be idled to wait for the possible arrival of a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236)  *    request for the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237)  * See the comments on the function bfq_better_to_idle for the reasons
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238)  * why performing device idling is the best choice to boost the throughput
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239)  * and preserve service guarantees when bfq_better_to_idle itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240)  * returns true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 	return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)  * This function chooses the queue from which to pick the next extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249)  * I/O request to inject, if it finds a compatible queue. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250)  * comments on bfq_update_inject_limit() for details on the injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251)  * mechanism, and for the definitions of the quantities mentioned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252)  * below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) static struct bfq_queue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) bfq_choose_bfqq_for_injection(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 	struct bfq_queue *bfqq, *in_serv_bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 	unsigned int limit = in_serv_bfqq->inject_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 	 * If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 	 * - bfqq is not weight-raised and therefore does not carry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 	 *   time-critical I/O,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 	 * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	 * - regardless of whether bfqq is weight-raised, bfqq has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	 *   however a long think time, during which it can absorb the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	 *   effect of an appropriate number of extra I/O requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	 *   from other queues (see bfq_update_inject_limit for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	 *   details on the computation of this number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 	 * then injection can be performed without restrictions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	bool in_serv_always_inject = in_serv_bfqq->wr_coeff == 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 		!bfq_bfqq_has_short_ttime(in_serv_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	 * If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 	 * - the baseline total service time could not be sampled yet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	 *   so the inject limit happens to be still 0, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 	 * - a lot of time has elapsed since the plugging of I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 	 *   dispatching started, so drive speed is being wasted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 	 *   significantly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 	 * then temporarily raise inject limit to one request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 	if (limit == 0 && in_serv_bfqq->last_serv_time_ns == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 	    bfq_bfqq_wait_request(in_serv_bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 	    time_is_before_eq_jiffies(bfqd->last_idling_start_jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 				      bfqd->bfq_slice_idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 		)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 		limit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 	if (bfqd->rq_in_driver >= limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	 * Linear search of the source queue for injection; but, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 	 * a high probability, very few steps are needed to find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 	 * candidate queue, i.e., a queue with enough budget left for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	 * its next request. In fact:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	 * - BFQ dynamically updates the budget of every queue so as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 	 *   to accommodate the expected backlog of the queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 	 * - if a queue gets all its requests dispatched as injected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	 *   service, then the queue is removed from the active list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 	 *   (and re-added only if it gets new requests, but then it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 	 *   is assigned again enough budget for its new backlog).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 	list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 		if (!RB_EMPTY_ROOT(&bfqq->sort_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 		    (in_serv_always_inject || bfqq->wr_coeff > 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 		    bfq_serv_to_charge(bfqq->next_rq, bfqq) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 		    bfq_bfqq_budget_left(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 			 * Allow for only one large in-flight request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 			 * on non-rotational devices, for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 			 * following reason. On non-rotationl drives,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 			 * large requests take much longer than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 			 * smaller requests to be served. In addition,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 			 * the drive prefers to serve large requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 			 * w.r.t. to small ones, if it can choose. So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 			 * having more than one large requests queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 			 * in the drive may easily make the next first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 			 * request of the in-service queue wait for so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 			 * long to break bfqq's service guarantees. On
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 			 * the bright side, large requests let the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 			 * drive reach a very high throughput, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 			 * there is only one in-flight large request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 			 * at a time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 			if (blk_queue_nonrot(bfqd->queue) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 			    blk_rq_sectors(bfqq->next_rq) >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 			    BFQQ_SECT_THR_NONROT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 				limit = min_t(unsigned int, 1, limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 				limit = in_serv_bfqq->inject_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 			if (bfqd->rq_in_driver < limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 				bfqd->rqs_injected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 				return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344)  * Select a queue for service.  If we have a current queue in service,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345)  * check whether to continue servicing it, or retrieve and set a new one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 	struct request *next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 	enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 	bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 	if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 		goto new_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 	bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 	 * Do not expire bfqq for budget timeout if bfqq may be about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 	 * to enjoy device idling. The reason why, in this case, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 	 * prevent bfqq from expiring is the same as in the comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 	 * on the case where bfq_bfqq_must_idle() returns true, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 	 * bfq_completed_request().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 	if (bfq_may_expire_for_budg_timeout(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) 	    !bfq_bfqq_must_idle(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) 		goto expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) check_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 	 * This loop is rarely executed more than once. Even when it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 	 * happens, it is much more convenient to re-execute this loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 	 * than to return NULL and trigger a new dispatch to get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 	 * request served.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 	next_rq = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 	 * If bfqq has requests queued and it has enough budget left to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 	 * serve them, keep the queue, otherwise expire it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 	if (next_rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 		if (bfq_serv_to_charge(next_rq, bfqq) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 			bfq_bfqq_budget_left(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 			 * Expire the queue for budget exhaustion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 			 * which makes sure that the next budget is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 			 * enough to serve the next request, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 			 * it comes from the fifo expired path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 			reason = BFQQE_BUDGET_EXHAUSTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 			goto expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 			 * The idle timer may be pending because we may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 			 * not disable disk idling even when a new request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 			 * arrives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 			if (bfq_bfqq_wait_request(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 				 * If we get here: 1) at least a new request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 				 * has arrived but we have not disabled the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 				 * timer because the request was too small,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 				 * 2) then the block layer has unplugged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 				 * the device, causing the dispatch to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 				 * invoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 				 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 				 * Since the device is unplugged, now the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 				 * requests are probably large enough to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 				 * provide a reasonable throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 				 * So we disable idling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 				bfq_clear_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 				hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 			goto keep_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 	 * No requests pending. However, if the in-service queue is idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 	 * for a new request, or has requests waiting for a completion and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 	 * may idle after their completion, then keep it anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 	 * Yet, inject service from other queues if it boosts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 	 * throughput and is possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 	if (bfq_bfqq_wait_request(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 	    (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 		struct bfq_queue *async_bfqq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 			bfqq->bic && bfqq->bic->bfqq[0] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 			bfq_bfqq_busy(bfqq->bic->bfqq[0]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 			bfqq->bic->bfqq[0]->next_rq ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 			bfqq->bic->bfqq[0] : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 		 * The next three mutually-exclusive ifs decide
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 		 * whether to try injection, and choose the queue to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 		 * pick an I/O request from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 		 * The first if checks whether the process associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 		 * with bfqq has also async I/O pending. If so, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 		 * injects such I/O unconditionally. Injecting async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 		 * I/O from the same process can cause no harm to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 		 * process. On the contrary, it can only increase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 		 * bandwidth and reduce latency for the process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 		 * The second if checks whether there happens to be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 		 * non-empty waker queue for bfqq, i.e., a queue whose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 		 * I/O needs to be completed for bfqq to receive new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 		 * I/O. This happens, e.g., if bfqq is associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 		 * a process that does some sync. A sync generates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 		 * extra blocking I/O, which must be completed before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 		 * the process associated with bfqq can go on with its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 		 * I/O. If the I/O of the waker queue is not served,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 		 * then bfqq remains empty, and no I/O is dispatched,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 		 * until the idle timeout fires for bfqq. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 		 * likely to result in lower bandwidth and higher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 		 * latencies for bfqq, and in a severe loss of total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 		 * throughput. The best action to take is therefore to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 		 * serve the waker queue as soon as possible. So do it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 		 * (without relying on the third alternative below for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 		 * eventually serving waker_bfqq's I/O; see the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 		 * paragraph for further details). This systematic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 		 * injection of I/O from the waker queue does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 		 * cause any delay to bfqq's I/O. On the contrary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 		 * next bfqq's I/O is brought forward dramatically,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 		 * for it is not blocked for milliseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 		 * The third if checks whether bfqq is a queue for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 		 * which it is better to avoid injection. It is so if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 		 * bfqq delivers more throughput when served without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 		 * any further I/O from other queues in the middle, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 		 * if the service times of bfqq's I/O requests both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 		 * count more than overall throughput, and may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 		 * easily increased by injection (this happens if bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 		 * has a short think time). If none of these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 		 * conditions holds, then a candidate queue for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 		 * injection is looked for through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 		 * bfq_choose_bfqq_for_injection(). Note that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 		 * latter may return NULL (for example if the inject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 		 * limit for bfqq is currently 0).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 		 * NOTE: motivation for the second alternative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 		 * Thanks to the way the inject limit is updated in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 		 * bfq_update_has_short_ttime(), it is rather likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 		 * that, if I/O is being plugged for bfqq and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 		 * waker queue has pending I/O requests that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 		 * blocking bfqq's I/O, then the third alternative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 		 * above lets the waker queue get served before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 		 * I/O-plugging timeout fires. So one may deem the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 		 * second alternative superfluous. It is not, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 		 * the third alternative may be way less effective in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 		 * case of a synchronization. For two main
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 		 * reasons. First, throughput may be low because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 		 * inject limit may be too low to guarantee the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 		 * amount of injected I/O, from the waker queue or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 		 * other queues, that the second alternative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 		 * guarantees (the second alternative unconditionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 		 * injects a pending I/O request of the waker queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 		 * for each bfq_dispatch_request()). Second, with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 		 * third alternative, the duration of the plugging,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 		 * i.e., the time before bfqq finally receives new I/O,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 		 * may not be minimized, because the waker queue may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 		 * happen to be served only after other queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 		if (async_bfqq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 		    icq_to_bic(async_bfqq->next_rq->elv.icq) == bfqq->bic &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 		    bfq_serv_to_charge(async_bfqq->next_rq, async_bfqq) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 		    bfq_bfqq_budget_left(async_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 			bfqq = bfqq->bic->bfqq[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 		else if (bfq_bfqq_has_waker(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 			   bfq_bfqq_busy(bfqq->waker_bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 			   bfqq->waker_bfqq->next_rq &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 			   bfq_serv_to_charge(bfqq->waker_bfqq->next_rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 					      bfqq->waker_bfqq) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 			   bfq_bfqq_budget_left(bfqq->waker_bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 			)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 			bfqq = bfqq->waker_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 		else if (!idling_boosts_thr_without_issues(bfqd, bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 			 (bfqq->wr_coeff == 1 || bfqd->wr_busy_queues > 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 			  !bfq_bfqq_has_short_ttime(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 			bfqq = bfq_choose_bfqq_for_injection(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 			bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 		goto keep_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	reason = BFQQE_NO_MORE_REQUESTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) expire:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 	bfq_bfqq_expire(bfqd, bfqq, false, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) new_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 	bfqq = bfq_set_in_service_queue(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 		bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 		goto check_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) keep_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) 		bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 		bfq_log(bfqd, "select_queue: no queue returned");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 	struct bfq_entity *entity = &bfqq->entity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 	if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 		bfq_log_bfqq(bfqd, bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 			"raising period dur %u/%u msec, old coeff %u, w %d(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 			jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 			jiffies_to_msecs(bfqq->wr_cur_max_time),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 			bfqq->wr_coeff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 			bfqq->entity.weight, bfqq->entity.orig_weight);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 		if (entity->prio_changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 			bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 		 * If the queue was activated in a burst, or too much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) 		 * time has elapsed from the beginning of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 		 * weight-raising period, then end weight raising.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 		if (bfq_bfqq_in_large_burst(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 			bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 		else if (time_is_before_jiffies(bfqq->last_wr_start_finish +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 						bfqq->wr_cur_max_time)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 			if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 			time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) 					       bfq_wr_duration(bfqd)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 				bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) 				switch_back_to_interactive_wr(bfqq, bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 				bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 		if (bfqq->wr_coeff > 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 		    bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) 		    bfqq->service_from_wr > max_service_from_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 			/* see comments on max_service_from_wr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 			bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 	 * To improve latency (for this or other queues), immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 	 * update weight both if it must be raised and if it must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	 * lowered. Since, entity may be on some active tree here, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 	 * might have a pending change of its ioprio class, invoke
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	 * next function with the last parameter unset (see the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 	 * comments on the function).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 	if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 		__bfq_entity_update_weight_prio(bfq_entity_service_tree(entity),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 						entity, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603)  * Dispatch next request from bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 						 struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 	struct request *rq = bfqq->next_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 	unsigned long service_to_charge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 	service_to_charge = bfq_serv_to_charge(rq, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) 	bfq_bfqq_served(bfqq, service_to_charge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) 	if (bfqq == bfqd->in_service_queue && bfqd->wait_dispatch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) 		bfqd->wait_dispatch = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 		bfqd->waited_rq = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 	bfq_dispatch_remove(bfqd->queue, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 	if (bfqq != bfqd->in_service_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) 		goto return_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) 	 * If weight raising has to terminate for bfqq, then next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 	 * function causes an immediate update of bfqq's weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 	 * without waiting for next activation. As a consequence, on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 	 * expiration, bfqq will be timestamped as if has never been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 	 * weight-raised during this service slot, even if it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 	 * received part or even most of the service as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	 * weight-raised queue. This inflates bfqq's timestamps, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 	 * is beneficial, as bfqq is then more willing to leave the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 	 * device immediately to possible other weight-raised queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 	bfq_update_wr_data(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 	 * Expire bfqq, pretending that its budget expired, if bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) 	 * belongs to CLASS_IDLE and other queues are waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 	 * service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 	if (!(bfq_tot_busy_queues(bfqd) > 1 && bfq_class_idle(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 		goto return_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 	bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) return_rq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 	return rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) static bool bfq_has_work(struct blk_mq_hw_ctx *hctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 	 * Avoiding lock: a race on bfqd->busy_queues should cause at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 	 * most a call to dispatch for nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) 	return !list_empty_careful(&bfqd->dispatch) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) 		bfq_tot_busy_queues(bfqd) > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 	struct request *rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 	struct bfq_queue *bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 	if (!list_empty(&bfqd->dispatch)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 		rq = list_first_entry(&bfqd->dispatch, struct request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 				      queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 		list_del_init(&rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 		bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 		if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 			 * Increment counters here, because this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 			 * dispatch does not follow the standard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 			 * dispatch flow (where counters are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 			 * incremented)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 			bfqq->dispatched++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 			goto inc_in_driver_start_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) 		 * We exploit the bfq_finish_requeue_request hook to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 		 * decrement rq_in_driver, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 		 * bfq_finish_requeue_request will not be invoked on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 		 * this request. So, to avoid unbalance, just start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 		 * this request, without incrementing rq_in_driver. As
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 		 * a negative consequence, rq_in_driver is deceptively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) 		 * lower than it should be while this request is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 		 * service. This may cause bfq_schedule_dispatch to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 		 * invoked uselessly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) 		 * As for implementing an exact solution, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) 		 * bfq_finish_requeue_request hook, if defined, is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 		 * probably invoked also on this request. So, by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 		 * exploiting this hook, we could 1) increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) 		 * rq_in_driver here, and 2) decrement it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 		 * bfq_finish_requeue_request. Such a solution would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 		 * let the value of the counter be always accurate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) 		 * but it would entail using an extra interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 		 * function. This cost seems higher than the benefit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) 		 * being the frequency of non-elevator-private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) 		 * requests very low.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) 		goto start_rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) 	bfq_log(bfqd, "dispatch requests: %d busy queues",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) 		bfq_tot_busy_queues(bfqd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 	if (bfq_tot_busy_queues(bfqd) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 	 * Force device to serve one request at a time if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) 	 * strict_guarantees is true. Forcing this service scheme is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 	 * currently the ONLY way to guarantee that the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) 	 * service order enforced by the scheduler is respected by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 	 * queueing device. Otherwise the device is free even to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) 	 * some unlucky request wait for as long as the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 	 * wishes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 	 * Of course, serving one request at a time may cause loss of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 	 * throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 	if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) 	bfqq = bfq_select_queue(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 	if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 	rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 	if (rq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) inc_in_driver_start_rq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 		bfqd->rq_in_driver++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) start_rq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 		rq->rq_flags |= RQF_STARTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 	return rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) #ifdef CONFIG_BFQ_CGROUP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) static void bfq_update_dispatch_stats(struct request_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 				      struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) 				      struct bfq_queue *in_serv_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 				      bool idle_timer_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) 	struct bfq_queue *bfqq = rq ? RQ_BFQQ(rq) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 	if (!idle_timer_disabled && !bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) 	 * rq and bfqq are guaranteed to exist until this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 	 * ends, for the following reasons. First, rq can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) 	 * dispatched to the device, and then can be completed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) 	 * freed, only after this function ends. Second, rq cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 	 * merged (and thus freed because of a merge) any longer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 	 * because it has already started. Thus rq cannot be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) 	 * before this function ends, and, since rq has a reference to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 	 * bfqq, the same guarantee holds for bfqq too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 	 * In addition, the following queue lock guarantees that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 	 * bfqq_group(bfqq) exists as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 	spin_lock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 	if (idle_timer_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 		 * Since the idle timer has been disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 		 * in_serv_queue contained some request when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 		 * __bfq_dispatch_request was invoked above, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 		 * implies that rq was picked exactly from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 		 * in_serv_queue. Thus in_serv_queue == bfqq, and is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 		 * therefore guaranteed to exist because of the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 		 * arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 		bfqg_stats_update_idle_time(bfqq_group(in_serv_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 		struct bfq_group *bfqg = bfqq_group(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) 		bfqg_stats_update_avg_queue_size(bfqg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 		bfqg_stats_set_start_empty_time(bfqg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 		bfqg_stats_update_io_remove(bfqg, rq->cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 	spin_unlock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) static inline void bfq_update_dispatch_stats(struct request_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 					     struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) 					     struct bfq_queue *in_serv_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) 					     bool idle_timer_disabled) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) #endif /* CONFIG_BFQ_CGROUP_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 	struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) 	struct bfq_queue *in_serv_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 	bool waiting_rq, idle_timer_disabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 	in_serv_queue = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	waiting_rq = in_serv_queue && bfq_bfqq_wait_request(in_serv_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 	rq = __bfq_dispatch_request(hctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) 	if (in_serv_queue == bfqd->in_service_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) 		idle_timer_disabled =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) 			waiting_rq && !bfq_bfqq_wait_request(in_serv_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) 	bfq_update_dispatch_stats(hctx->queue, rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) 			idle_timer_disabled ? in_serv_queue : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) 				idle_timer_disabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) 	return rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831)  * Task holds one reference to the queue, dropped when task exits.  Each rq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832)  * in-flight on this queue also holds a reference, dropped when rq is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834)  * Scheduler lock must be held here. Recall not to use bfqq after calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835)  * this function on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) void bfq_put_queue(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	struct bfq_queue *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	struct hlist_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 	struct bfq_group *bfqg = bfqq_group(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 	if (bfqq->bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 		bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 			     bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 	bfqq->ref--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 	if (bfqq->ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 	if (!hlist_unhashed(&bfqq->burst_list_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 		hlist_del_init(&bfqq->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 		 * Decrement also burst size after the removal, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 		 * process associated with bfqq is exiting, and thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 		 * does not contribute to the burst any longer. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 		 * decrement helps filter out false positives of large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 		 * bursts, when some short-lived process (often due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 		 * the execution of commands by some service) happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 		 * to start and exit while a complex application is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) 		 * starting, and thus spawning several processes that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 		 * do I/O (and that *must not* be treated as a large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 		 * burst, see comments on bfq_handle_burst).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 		 * In particular, the decrement is performed only if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 		 * 1) bfqq is not a merged queue, because, if it is,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 		 * then this free of bfqq is not triggered by the exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 		 * of the process bfqq is associated with, but exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 		 * by the fact that bfqq has just been merged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 		 * 2) burst_size is greater than 0, to handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 		 * unbalanced decrements. Unbalanced decrements may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 		 * happen in te following case: bfqq is inserted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 		 * the current burst list--without incrementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 		 * bust_size--because of a split, but the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 		 * burst list is not the burst list bfqq belonged to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 		 * (see comments on the case of a split in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 		 * bfq_set_request).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 		if (bfqq->bic && bfqq->bfqd->burst_size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 			bfqq->bfqd->burst_size--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 	 * bfqq does not exist any longer, so it cannot be woken by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 	 * any other queue, and cannot wake any other queue. Then bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 	 * must be removed from the woken list of its possible waker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 	 * queue, and all queues in the woken list of bfqq must stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 	 * having a waker queue. Strictly speaking, these updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 	 * should be performed when bfqq remains with no I/O source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 	 * attached to it, which happens before bfqq gets freed. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 	 * particular, this happens when the last process associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 	 * with bfqq exits or gets associated with a different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 	 * queue. However, both events lead to bfqq being freed soon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 	 * and dangling references would come out only after bfqq gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 	 * freed. So these updates are done here, as a simple and safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 	 * way to handle all cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) 	/* remove bfqq from woken list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 	if (!hlist_unhashed(&bfqq->woken_list_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 		hlist_del_init(&bfqq->woken_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 	/* reset waker for all queues in woken list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 	hlist_for_each_entry_safe(item, n, &bfqq->woken_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 				  woken_list_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 		item->waker_bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 		bfq_clear_bfqq_has_waker(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 		hlist_del_init(&item->woken_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) 	if (bfqq->bfqd && bfqq->bfqd->last_completed_rq_bfqq == bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 		bfqq->bfqd->last_completed_rq_bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 	kmem_cache_free(bfq_pool, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) 	bfqg_and_blkg_put(bfqg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) static void bfq_put_cooperator(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	struct bfq_queue *__bfqq, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 	 * If this queue was scheduled to merge with another queue, be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 	 * sure to drop the reference taken on that queue (and others in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 	 * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 	__bfqq = bfqq->new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) 	while (__bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 		if (__bfqq == bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) 		next = __bfqq->new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) 		bfq_put_queue(__bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) 		__bfqq = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 	if (bfqq == bfqd->in_service_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 		__bfq_bfqq_expire(bfqd, bfqq, BFQQE_BUDGET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 		bfq_schedule_dispatch(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 	bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 	bfq_put_cooperator(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 	bfq_release_process_ref(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 	struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 	struct bfq_data *bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) 		bfqd = bfqq->bfqd; /* NULL if scheduler already exited */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 	if (bfqq && bfqd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) 		spin_lock_irqsave(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) 		bfqq->bic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) 		bfq_exit_bfqq(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) 		bic_set_bfqq(bic, NULL, is_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) 		spin_unlock_irqrestore(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) static void bfq_exit_icq(struct io_cq *icq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 	struct bfq_io_cq *bic = icq_to_bic(icq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 	bfq_exit_icq_bfqq(bic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 	bfq_exit_icq_bfqq(bic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978)  * Update the entity prio values; note that the new values will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979)  * be used until the next (re)activation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 	struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) 	int ioprio_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) 	struct bfq_data *bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) 	if (!bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) 	ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) 	switch (ioprio_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) 		pr_err("bdi %s: bfq: bad prio class %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) 				bdi_dev_name(bfqq->bfqd->queue->backing_dev_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) 				ioprio_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) 	case IOPRIO_CLASS_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) 		 * No prio set, inherit CPU scheduling settings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 		bfqq->new_ioprio = task_nice_ioprio(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) 		bfqq->new_ioprio_class = task_nice_ioclass(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 	case IOPRIO_CLASS_RT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) 		bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 		bfqq->new_ioprio_class = IOPRIO_CLASS_RT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) 	case IOPRIO_CLASS_BE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) 		bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 		bfqq->new_ioprio_class = IOPRIO_CLASS_BE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 	case IOPRIO_CLASS_IDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 		bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 		bfqq->new_ioprio = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) 	if (bfqq->new_ioprio >= IOPRIO_BE_NR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) 		pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) 			bfqq->new_ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 		bfqq->new_ioprio = IOPRIO_BE_NR - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) 	bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) 	bfqq->entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) 				       struct bio *bio, bool is_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) 				       struct bfq_io_cq *bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) 	struct bfq_data *bfqd = bic_to_bfqd(bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) 	int ioprio = bic->icq.ioc->ioprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 	 * This condition may trigger on a newly created bic, be sure to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 	 * drop the lock before returning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 	if (unlikely(!bfqd) || likely(bic->ioprio == ioprio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) 	bic->ioprio = ioprio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) 	bfqq = bic_to_bfqq(bic, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) 		bfq_release_process_ref(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) 		bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 		bic_set_bfqq(bic, bfqq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) 	bfqq = bic_to_bfqq(bic, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 		bfq_set_next_ioprio_data(bfqq, bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060) static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061) 			  struct bfq_io_cq *bic, pid_t pid, int is_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) 	RB_CLEAR_NODE(&bfqq->entity.rb_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) 	INIT_LIST_HEAD(&bfqq->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 	INIT_HLIST_NODE(&bfqq->burst_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 	INIT_HLIST_NODE(&bfqq->woken_list_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 	INIT_HLIST_HEAD(&bfqq->woken_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) 	bfqq->ref = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) 	bfqq->bfqd = bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) 	if (bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) 		bfq_set_next_ioprio_data(bfqq, bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) 	if (is_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) 		 * No need to mark as has_short_ttime if in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) 		 * idle_class, because no device idling is performed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) 		 * for queues in idle class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) 		if (!bfq_class_idle(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) 			/* tentatively mark as has_short_ttime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) 			bfq_mark_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) 		bfq_mark_bfqq_sync(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) 		bfq_mark_bfqq_just_created(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087) 		bfq_clear_bfqq_sync(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089) 	/* set end request to minus infinity from now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090) 	bfqq->ttime.last_end_request = ktime_get_ns() + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) 	bfq_mark_bfqq_IO_bound(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) 	bfqq->pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) 	/* Tentative initial value to trade off between thr and lat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) 	bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) 	bfqq->budget_timeout = bfq_smallest_from_now();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) 	bfqq->wr_coeff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) 	bfqq->last_wr_start_finish = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) 	bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) 	bfqq->split_time = bfq_smallest_from_now();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) 	 * To not forget the possibly high bandwidth consumed by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) 	 * process/queue in the recent past,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) 	 * bfq_bfqq_softrt_next_start() returns a value at least equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) 	 * to the current value of bfqq->soft_rt_next_start (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) 	 * comments on bfq_bfqq_softrt_next_start).  Set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) 	 * soft_rt_next_start to now, to mean that bfqq has consumed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) 	 * no bandwidth so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) 	bfqq->soft_rt_next_start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) 	/* first request is almost certainly seeky */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) 	bfqq->seek_history = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) 					       struct bfq_group *bfqg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) 					       int ioprio_class, int ioprio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) 	switch (ioprio_class) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 	case IOPRIO_CLASS_RT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) 		return &bfqg->async_bfqq[0][ioprio];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127) 	case IOPRIO_CLASS_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128) 		ioprio = IOPRIO_NORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) 	case IOPRIO_CLASS_BE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) 		return &bfqg->async_bfqq[1][ioprio];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) 	case IOPRIO_CLASS_IDLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) 		return &bfqg->async_idle_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) 				       struct bio *bio, bool is_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) 				       struct bfq_io_cq *bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) 	const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) 	const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) 	struct bfq_queue **async_bfqq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) 	struct bfq_group *bfqg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) 	bfqg = bfq_find_set_group(bfqd, __bio_blkcg(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) 	if (!bfqg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) 		bfqq = &bfqd->oom_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) 	if (!is_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) 		async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) 						  ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) 		bfqq = *async_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) 		if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) 	bfqq = kmem_cache_alloc_node(bfq_pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) 				     GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) 				     bfqd->queue->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) 		bfq_init_bfqq(bfqd, bfqq, bic, current->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) 			      is_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) 		bfq_init_entity(&bfqq->entity, bfqg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) 		bfq_log_bfqq(bfqd, bfqq, "allocated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) 		bfqq = &bfqd->oom_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) 		bfq_log_bfqq(bfqd, bfqq, "using oom bfqq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) 	 * Pin the queue now that it's allocated, scheduler exit will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) 	 * prune it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) 	if (async_bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) 		bfqq->ref++; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) 			      * Extra group reference, w.r.t. sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) 			      * queue. This extra reference is removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) 			      * only if bfqq->bfqg disappears, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) 			      * guarantee that this queue is not freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) 			      * until its group goes away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) 			      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) 		bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) 			     bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) 		*async_bfqq = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) 	bfqq->ref++; /* get a process reference to this queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) 	bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) static void bfq_update_io_thinktime(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) 				    struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) 	struct bfq_ttime *ttime = &bfqq->ttime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) 	u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) 	elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) 	ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) 	ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed,  8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) 	ttime->ttime_mean = div64_ul(ttime->ttime_total + 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) 				     ttime->ttime_samples);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) 		       struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) 	bfqq->seek_history <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) 	bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) 	if (bfqq->wr_coeff > 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) 	    bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) 	    BFQQ_TOTALLY_SEEKY(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) 		bfq_bfqq_end_wr(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) static void bfq_update_has_short_ttime(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) 				       struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233) 				       struct bfq_io_cq *bic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235) 	bool has_short_ttime = true, state_changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238) 	 * No need to update has_short_ttime if bfqq is async or in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239) 	 * idle io prio class, or if bfq_slice_idle is zero, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240) 	 * no device idling is performed for bfqq in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242) 	if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243) 	    bfqd->bfq_slice_idle == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246) 	/* Idle window just restored, statistics are meaningless. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247) 	if (time_is_after_eq_jiffies(bfqq->split_time +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248) 				     bfqd->bfq_wr_min_idle_time))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251) 	/* Think time is infinite if no process is linked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252) 	 * bfqq. Otherwise check average think time to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253) 	 * decide whether to mark as has_short_ttime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255) 	if (atomic_read(&bic->icq.ioc->active_ref) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256) 	    (bfq_sample_valid(bfqq->ttime.ttime_samples) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257) 	     bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258) 		has_short_ttime = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260) 	state_changed = has_short_ttime != bfq_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262) 	if (has_short_ttime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263) 		bfq_mark_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265) 		bfq_clear_bfqq_has_short_ttime(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) 	 * Until the base value for the total service time gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) 	 * finally computed for bfqq, the inject limit does depend on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) 	 * the think-time state (short|long). In particular, the limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) 	 * is 0 or 1 if the think time is deemed, respectively, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) 	 * short or long (details in the comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) 	 * bfq_update_inject_limit()). Accordingly, the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) 	 * instructions reset the inject limit if the think-time state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) 	 * has changed and the above base value is still to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) 	 * computed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) 	 * However, the reset is performed only if more than 100 ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) 	 * have elapsed since the last update of the inject limit, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) 	 * (inclusive) if the change is from short to long think
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) 	 * time. The reason for this waiting is as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) 	 * bfqq may have a long think time because of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) 	 * synchronization with some other queue, i.e., because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) 	 * I/O of some other queue may need to be completed for bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) 	 * to receive new I/O. Details in the comments on the choice
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) 	 * of the queue for injection in bfq_select_queue().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) 	 * As stressed in those comments, if such a synchronization is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) 	 * actually in place, then, without injection on bfqq, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) 	 * blocking I/O cannot happen to served while bfqq is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) 	 * service. As a consequence, if bfqq is granted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) 	 * I/O-dispatch-plugging, then bfqq remains empty, and no I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) 	 * is dispatched, until the idle timeout fires. This is likely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) 	 * to result in lower bandwidth and higher latencies for bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) 	 * and in a severe loss of total throughput.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) 	 * On the opposite end, a non-zero inject limit may allow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) 	 * I/O that blocks bfqq to be executed soon, and therefore
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) 	 * bfqq to receive new I/O soon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) 	 * But, if the blocking gets actually eliminated, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) 	 * next think-time sample for bfqq may be very low. This in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) 	 * turn may cause bfqq's think time to be deemed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) 	 * short. Without the 100 ms barrier, this new state change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) 	 * would cause the body of the next if to be executed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) 	 * immediately. But this would set to 0 the inject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) 	 * limit. Without injection, the blocking I/O would cause the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) 	 * think time of bfqq to become long again, and therefore the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) 	 * inject limit to be raised again, and so on. The only effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) 	 * of such a steady oscillation between the two think-time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) 	 * states would be to prevent effective injection on bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) 	 * In contrast, if the inject limit is not reset during such a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) 	 * long time interval as 100 ms, then the number of short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) 	 * think time samples can grow significantly before the reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317) 	 * is performed. As a consequence, the think time state can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318) 	 * become stable before the reset. Therefore there will be no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319) 	 * state change when the 100 ms elapse, and no reset of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320) 	 * inject limit. The inject limit remains steadily equal to 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) 	 * both during and after the 100 ms. So injection can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) 	 * performed at all times, and throughput gets boosted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) 	 * An inject limit equal to 1 is however in conflict, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) 	 * general, with the fact that the think time of bfqq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) 	 * short, because injection may be likely to delay bfqq's I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) 	 * (as explained in the comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) 	 * bfq_update_inject_limit()). But this does not happen in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) 	 * this special case, because bfqq's low think time is due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) 	 * an effective handling of a synchronization, through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) 	 * injection. In this special case, bfqq's I/O does not get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) 	 * delayed by injection; on the contrary, bfqq's I/O is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) 	 * brought forward, because it is not blocked for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) 	 * milliseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) 	 * In addition, serving the blocking I/O much sooner, and much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) 	 * more frequently than once per I/O-plugging timeout, makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) 	 * it much quicker to detect a waker queue (the concept of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) 	 * waker queue is defined in the comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) 	 * bfq_add_request()). This makes it possible to start sooner
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) 	 * to boost throughput more effectively, by injecting the I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) 	 * of the waker queue unconditionally on every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) 	 * bfq_dispatch_request().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) 	 * One last, important benefit of not resetting the inject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) 	 * limit before 100 ms is that, during this time interval, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) 	 * base value for the total service time is likely to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) 	 * finally computed for bfqq, freeing the inject limit from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) 	 * its relation with the think time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) 	if (state_changed && bfqq->last_serv_time_ns == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) 	    (time_is_before_eq_jiffies(bfqq->decrease_time_jif +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) 				      msecs_to_jiffies(100)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) 	     !has_short_ttime))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) 		bfq_reset_inject_limit(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359)  * Called when a new fs request (rq) is added to bfqq.  Check if there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360)  * something we should do about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363) 			    struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365) 	if (rq->cmd_flags & REQ_META)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366) 		bfqq->meta_pending++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368) 	bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370) 	if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371) 		bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372) 				 blk_rq_sectors(rq) < 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373) 		bool budget_timeout = bfq_bfqq_budget_timeout(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376) 		 * There is just this request queued: if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377) 		 * - the request is small, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378) 		 * - we are idling to boost throughput, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379) 		 * - the queue is not to be expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380) 		 * then just exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382) 		 * In this way, if the device is being idled to wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383) 		 * for a new request from the in-service queue, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384) 		 * avoid unplugging the device and committing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385) 		 * device to serve just a small request. In contrast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) 		 * we wait for the block layer to decide when to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) 		 * unplug the device: hopefully, new requests will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) 		 * merged to this one quickly, then the device will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) 		 * unplugged and larger requests will be dispatched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) 		if (small_req && idling_boosts_thr_without_issues(bfqd, bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) 		    !budget_timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) 		 * A large enough request arrived, or idling is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) 		 * performed to preserve service guarantees, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) 		 * finally the queue is to be expired: in all these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) 		 * cases disk idling is to be stopped, so clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) 		 * wait_request flag and reset timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) 		bfq_clear_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) 		hrtimer_try_to_cancel(&bfqd->idle_slice_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) 		 * The queue is not empty, because a new request just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) 		 * arrived. Hence we can safely expire the queue, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) 		 * case of budget timeout, without risking that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) 		 * timestamps of the queue are not updated correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) 		 * See [1] for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) 		if (budget_timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) 			bfq_bfqq_expire(bfqd, bfqq, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) 					BFQQE_BUDGET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) /* returns true if it causes the idle timer to be disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) static bool __bfq_insert_request(struct bfq_data *bfqd, struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) 	struct bfq_queue *bfqq = RQ_BFQQ(rq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) 		*new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) 	bool waiting, idle_timer_disabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) 	if (new_bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) 		 * Release the request's reference to the old bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) 		 * and make sure one is taken to the shared queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) 		new_bfqq->allocated++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) 		bfqq->allocated--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) 		new_bfqq->ref++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) 		 * If the bic associated with the process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) 		 * issuing this request still points to bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) 		 * (and thus has not been already redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) 		 * to new_bfqq or even some other bfq_queue),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) 		 * then complete the merge and redirect it to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) 		 * new_bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) 		if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) 			bfq_merge_bfqqs(bfqd, RQ_BIC(rq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) 					bfqq, new_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) 		bfq_clear_bfqq_just_created(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) 		 * rq is about to be enqueued into new_bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) 		 * release rq reference on bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) 		bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) 		rq->elv.priv[1] = new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) 		bfqq = new_bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) 	bfq_update_io_thinktime(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) 	bfq_update_has_short_ttime(bfqd, bfqq, RQ_BIC(rq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) 	bfq_update_io_seektime(bfqd, bfqq, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) 	waiting = bfqq && bfq_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) 	bfq_add_request(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) 	idle_timer_disabled = waiting && !bfq_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) 	rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) 	list_add_tail(&rq->queuelist, &bfqq->fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) 	bfq_rq_enqueued(bfqd, bfqq, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) 	return idle_timer_disabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) #ifdef CONFIG_BFQ_CGROUP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) static void bfq_update_insert_stats(struct request_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) 				    struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) 				    bool idle_timer_disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) 				    unsigned int cmd_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) 	if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) 	 * bfqq still exists, because it can disappear only after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) 	 * either it is merged with another queue, or the process it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) 	 * is associated with exits. But both actions must be taken by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) 	 * the same process currently executing this flow of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) 	 * instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) 	 * In addition, the following queue lock guarantees that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) 	 * bfqq_group(bfqq) exists as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) 	spin_lock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) 	bfqg_stats_update_io_add(bfqq_group(bfqq), bfqq, cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) 	if (idle_timer_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) 		bfqg_stats_update_idle_time(bfqq_group(bfqq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) 	spin_unlock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) static inline void bfq_update_insert_stats(struct request_queue *q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) 					   struct bfq_queue *bfqq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) 					   bool idle_timer_disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) 					   unsigned int cmd_flags) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) #endif /* CONFIG_BFQ_CGROUP_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) 			       bool at_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) 	struct request_queue *q = hctx->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) 	bool idle_timer_disabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) 	unsigned int cmd_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) 	if (!cgroup_subsys_on_dfl(io_cgrp_subsys) && rq->bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) 		bfqg_stats_update_legacy_io(q, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) 	if (blk_mq_sched_try_insert_merge(q, rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) 		spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) 	blk_mq_sched_request_inserted(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) 	bfqq = bfq_init_rq(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) 	if (!bfqq || at_head || blk_rq_is_passthrough(rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) 		if (at_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) 			list_add(&rq->queuelist, &bfqd->dispatch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) 			list_add_tail(&rq->queuelist, &bfqd->dispatch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) 		idle_timer_disabled = __bfq_insert_request(bfqd, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) 		 * Update bfqq, because, if a queue merge has occurred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) 		 * in __bfq_insert_request, then rq has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) 		 * redirected into a new queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) 		bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) 		if (rq_mergeable(rq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) 			elv_rqhash_add(q, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) 			if (!q->last_merge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) 				q->last_merge = rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) 	 * Cache cmd_flags before releasing scheduler lock, because rq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) 	 * may disappear afterwards (for example, because of a request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) 	 * merge).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) 	cmd_flags = rq->cmd_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) 	bfq_update_insert_stats(q, bfqq, idle_timer_disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) 				cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) 				struct list_head *list, bool at_head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) 	while (!list_empty(list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) 		struct request *rq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) 		rq = list_first_entry(list, struct request, queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) 		list_del_init(&rq->queuelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) 		bfq_insert_request(hctx, rq, at_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) static void bfq_update_hw_tag(struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) 	struct bfq_queue *bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) 	bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) 				       bfqd->rq_in_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) 	if (bfqd->hw_tag == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) 	 * This sample is valid if the number of outstanding requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) 	 * is large enough to allow a queueing behavior.  Note that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) 	 * sum is not exact, as it's not taking into account deactivated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) 	 * requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) 	if (bfqd->rq_in_driver + bfqd->queued <= BFQ_HW_QUEUE_THRESHOLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) 	 * If active queue hasn't enough requests and can idle, bfq might not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) 	 * dispatch sufficient requests to hardware. Don't zero hw_tag in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) 	 * case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) 	if (bfqq && bfq_bfqq_has_short_ttime(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) 	    bfqq->dispatched + bfqq->queued[0] + bfqq->queued[1] <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) 	    BFQ_HW_QUEUE_THRESHOLD &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) 	    bfqd->rq_in_driver < BFQ_HW_QUEUE_THRESHOLD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) 	if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) 	bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) 	bfqd->max_rq_in_driver = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) 	bfqd->hw_tag_samples = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) 	bfqd->nonrot_with_queueing =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) 		blk_queue_nonrot(bfqd->queue) && bfqd->hw_tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) 	u64 now_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) 	u32 delta_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) 	bfq_update_hw_tag(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) 	bfqd->rq_in_driver--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) 	bfqq->dispatched--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) 	if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) 		 * Set budget_timeout (which we overload to store the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) 		 * time at which the queue remains with no backlog and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) 		 * no outstanding request; used by the weight-raising
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) 		 * mechanism).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) 		bfqq->budget_timeout = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) 		bfq_weights_tree_remove(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) 	now_ns = ktime_get_ns();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) 	bfqq->ttime.last_end_request = now_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) 	 * Using us instead of ns, to get a reasonable precision in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) 	 * computing rate in next check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) 	delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) 	 * If the request took rather long to complete, and, according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) 	 * to the maximum request size recorded, this completion latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) 	 * implies that the request was certainly served at a very low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) 	 * rate (less than 1M sectors/sec), then the whole observation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) 	 * interval that lasts up to this time instant cannot be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) 	 * valid time interval for computing a new peak rate.  Invoke
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) 	 * bfq_update_rate_reset to have the following three steps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) 	 * taken:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) 	 * - close the observation interval at the last (previous)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) 	 *   request dispatch or completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) 	 * - compute rate, if possible, for that observation interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) 	 * - reset to zero samples, which will trigger a proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) 	 *   re-initialization of the observation interval on next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) 	 *   dispatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) 	if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) 	   (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) 			1UL<<(BFQ_RATE_SHIFT - 10))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) 		bfq_update_rate_reset(bfqd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) 	bfqd->last_completion = now_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) 	bfqd->last_completed_rq_bfqq = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) 	 * If we are waiting to discover whether the request pattern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) 	 * of the task associated with the queue is actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) 	 * isochronous, and both requisites for this condition to hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) 	 * are now satisfied, then compute soft_rt_next_start (see the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) 	 * comments on the function bfq_bfqq_softrt_next_start()). We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) 	 * do not compute soft_rt_next_start if bfqq is in interactive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) 	 * weight raising (see the comments in bfq_bfqq_expire() for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) 	 * an explanation). We schedule this delayed update when bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) 	 * expires, if it still has in-flight requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) 	if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) 	    RB_EMPTY_ROOT(&bfqq->sort_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) 	    bfqq->wr_coeff != bfqd->bfq_wr_coeff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) 		bfqq->soft_rt_next_start =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685) 			bfq_bfqq_softrt_next_start(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688) 	 * If this is the in-service queue, check if it needs to be expired,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689) 	 * or if we want to idle in case it has no pending requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691) 	if (bfqd->in_service_queue == bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692) 		if (bfq_bfqq_must_idle(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693) 			if (bfqq->dispatched == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694) 				bfq_arm_slice_timer(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) 			 * If we get here, we do not expire bfqq, even
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) 			 * if bfqq was in budget timeout or had no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) 			 * more requests (as controlled in the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) 			 * conditional instructions). The reason for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) 			 * not expiring bfqq is as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) 			 * Here bfqq->dispatched > 0 holds, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) 			 * bfq_bfqq_must_idle() returned true. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) 			 * implies that, even if no request arrives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) 			 * for bfqq before bfqq->dispatched reaches 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) 			 * bfqq will, however, not be expired on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) 			 * completion event that causes bfqq->dispatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) 			 * to reach zero. In contrast, on this event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) 			 * bfqq will start enjoying device idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) 			 * (I/O-dispatch plugging).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) 			 * But, if we expired bfqq here, bfqq would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) 			 * not have the chance to enjoy device idling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) 			 * when bfqq->dispatched finally reaches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) 			 * zero. This would expose bfqq to violation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) 			 * of its reserved service guarantees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) 		} else if (bfq_may_expire_for_budg_timeout(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) 			bfq_bfqq_expire(bfqd, bfqq, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) 					BFQQE_BUDGET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) 		else if (RB_EMPTY_ROOT(&bfqq->sort_list) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) 			 (bfqq->dispatched == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) 			  !bfq_better_to_idle(bfqq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) 			bfq_bfqq_expire(bfqd, bfqq, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) 					BFQQE_NO_MORE_REQUESTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) 	if (!bfqd->rq_in_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) 		bfq_schedule_dispatch(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733) static void bfq_finish_requeue_request_body(struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735) 	bfqq->allocated--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737) 	bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741)  * The processes associated with bfqq may happen to generate their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742)  * cumulative I/O at a lower rate than the rate at which the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743)  * could serve the same I/O. This is rather probable, e.g., if only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744)  * one process is associated with bfqq and the device is an SSD. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745)  * results in bfqq becoming often empty while in service. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746)  * respect, if BFQ is allowed to switch to another queue when bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747)  * remains empty, then the device goes on being fed with I/O requests,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748)  * and the throughput is not affected. In contrast, if BFQ is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749)  * allowed to switch to another queue---because bfqq is sync and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750)  * I/O-dispatch needs to be plugged while bfqq is temporarily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751)  * empty---then, during the service of bfqq, there will be frequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752)  * "service holes", i.e., time intervals during which bfqq gets empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753)  * and the device can only consume the I/O already queued in its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754)  * hardware queues. During service holes, the device may even get to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755)  * remaining idle. In the end, during the service of bfqq, the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756)  * is driven at a lower speed than the one it can reach with the kind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757)  * of I/O flowing through bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759)  * To counter this loss of throughput, BFQ implements a "request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760)  * injection mechanism", which tries to fill the above service holes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761)  * with I/O requests taken from other queues. The hard part in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762)  * mechanism is finding the right amount of I/O to inject, so as to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763)  * both boost throughput and not break bfqq's bandwidth and latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764)  * guarantees. In this respect, the mechanism maintains a per-queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765)  * inject limit, computed as below. While bfqq is empty, the injection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766)  * mechanism dispatches extra I/O requests only until the total number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767)  * of I/O requests in flight---i.e., already dispatched but not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768)  * completed---remains lower than this limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770)  * A first definition comes in handy to introduce the algorithm by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771)  * which the inject limit is computed.  We define as first request for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772)  * bfqq, an I/O request for bfqq that arrives while bfqq is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773)  * service, and causes bfqq to switch from empty to non-empty. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774)  * algorithm updates the limit as a function of the effect of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775)  * injection on the service times of only the first requests of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776)  * bfqq. The reason for this restriction is that these are the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777)  * requests whose service time is affected most, because they are the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778)  * first to arrive after injection possibly occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780)  * To evaluate the effect of injection, the algorithm measures the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781)  * "total service time" of first requests. We define as total service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782)  * time of an I/O request, the time that elapses since when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783)  * request is enqueued into bfqq, to when it is completed. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784)  * quantity allows the whole effect of injection to be measured. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785)  * easy to see why. Suppose that some requests of other queues are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786)  * actually injected while bfqq is empty, and that a new request R
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787)  * then arrives for bfqq. If the device does start to serve all or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788)  * part of the injected requests during the service hole, then,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789)  * because of this extra service, it may delay the next invocation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790)  * the dispatch hook of BFQ. Then, even after R gets eventually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791)  * dispatched, the device may delay the actual service of R if it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792)  * still busy serving the extra requests, or if it decides to serve,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793)  * before R, some extra request still present in its queues. As a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794)  * conclusion, the cumulative extra delay caused by injection can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795)  * easily evaluated by just comparing the total service time of first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796)  * requests with and without injection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798)  * The limit-update algorithm works as follows. On the arrival of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799)  * first request of bfqq, the algorithm measures the total time of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800)  * request only if one of the three cases below holds, and, for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801)  * case, it updates the limit as described below:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803)  * (1) If there is no in-flight request. This gives a baseline for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804)  *     total service time of the requests of bfqq. If the baseline has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805)  *     not been computed yet, then, after computing it, the limit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806)  *     set to 1, to start boosting throughput, and to prepare the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807)  *     ground for the next case. If the baseline has already been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808)  *     computed, then it is updated, in case it results to be lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809)  *     than the previous value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811)  * (2) If the limit is higher than 0 and there are in-flight
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812)  *     requests. By comparing the total service time in this case with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813)  *     the above baseline, it is possible to know at which extent the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814)  *     current value of the limit is inflating the total service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815)  *     time. If the inflation is below a certain threshold, then bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816)  *     is assumed to be suffering from no perceivable loss of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817)  *     service guarantees, and the limit is even tentatively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818)  *     increased. If the inflation is above the threshold, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819)  *     limit is decreased. Due to the lack of any hysteresis, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820)  *     logic makes the limit oscillate even in steady workload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821)  *     conditions. Yet we opted for it, because it is fast in reaching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822)  *     the best value for the limit, as a function of the current I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823)  *     workload. To reduce oscillations, this step is disabled for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824)  *     short time interval after the limit happens to be decreased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826)  * (3) Periodically, after resetting the limit, to make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827)  *     limit eventually drops in case the workload changes. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828)  *     needed because, after the limit has gone safely up for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829)  *     certain workload, it is impossible to guess whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830)  *     baseline total service time may have changed, without measuring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831)  *     it again without injection. A more effective version of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832)  *     step might be to just sample the baseline, by interrupting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833)  *     injection only once, and then to reset/lower the limit only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834)  *     the total service time with the current limit does happen to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835)  *     too large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837)  * More details on each step are provided in the comments on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838)  * pieces of code that implement these steps: the branch handling the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839)  * transition from empty to non empty in bfq_add_request(), the branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840)  * handling injection in bfq_select_queue(), and the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841)  * bfq_choose_bfqq_for_injection(). These comments also explain some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842)  * exceptions, made by the injection mechanism in some special cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) static void bfq_update_inject_limit(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) 				    struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) 	u64 tot_time_ns = ktime_get_ns() - bfqd->last_empty_occupied_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) 	unsigned int old_limit = bfqq->inject_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) 	if (bfqq->last_serv_time_ns > 0 && bfqd->rqs_injected) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) 		u64 threshold = (bfqq->last_serv_time_ns * 3)>>1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) 		if (tot_time_ns >= threshold && old_limit > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) 			bfqq->inject_limit--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) 			bfqq->decrease_time_jif = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) 		} else if (tot_time_ns < threshold &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) 			   old_limit <= bfqd->max_rq_in_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) 			bfqq->inject_limit++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) 	 * Either we still have to compute the base value for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) 	 * total service time, and there seem to be the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) 	 * conditions to do it, or we can lower the last base value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) 	 * computed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) 	 * NOTE: (bfqd->rq_in_driver == 1) means that there is no I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) 	 * request in flight, because this function is in the code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) 	 * path that handles the completion of a request of bfqq, and,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) 	 * in particular, this function is executed before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) 	 * bfqd->rq_in_driver is decremented in such a code path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) 	if ((bfqq->last_serv_time_ns == 0 && bfqd->rq_in_driver == 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874) 	    tot_time_ns < bfqq->last_serv_time_ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875) 		if (bfqq->last_serv_time_ns == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) 			 * Now we certainly have a base value: make sure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) 			 * start trying injection.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) 			bfqq->inject_limit = max_t(unsigned int, 1, old_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) 		bfqq->last_serv_time_ns = tot_time_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) 	} else if (!bfqd->rqs_injected && bfqd->rq_in_driver == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) 		 * No I/O injected and no request still in service in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) 		 * the drive: these are the exact conditions for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) 		 * computing the base value of the total service time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) 		 * for bfqq. So let's update this value, because it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) 		 * rather variable. For example, it varies if the size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) 		 * or the spatial locality of the I/O requests in bfqq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) 		 * change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) 		bfqq->last_serv_time_ns = tot_time_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) 	/* update complete, not waiting for any request completion any longer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) 	bfqd->waited_rq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) 	bfqd->rqs_injected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902)  * Handle either a requeue or a finish for rq. The things to do are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903)  * the same in both cases: all references to rq are to be dropped. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904)  * particular, rq is considered completed from the point of view of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905)  * the scheduler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) static void bfq_finish_requeue_request(struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) 	struct bfq_queue *bfqq = RQ_BFQQ(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910) 	struct bfq_data *bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) 	 * rq either is not associated with any icq, or is an already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) 	 * requeued request that has not (yet) been re-inserted into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) 	 * a bfq_queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) 	if (!rq->elv.icq || !bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) 	bfqd = bfqq->bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) 	if (rq->rq_flags & RQF_STARTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) 		bfqg_stats_update_completion(bfqq_group(bfqq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) 					     rq->start_time_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) 					     rq->io_start_time_ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) 					     rq->cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) 	if (likely(rq->rq_flags & RQF_STARTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) 		unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) 		spin_lock_irqsave(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) 		if (rq == bfqd->waited_rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) 			bfq_update_inject_limit(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936) 		bfq_completed_request(bfqq, bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) 		bfq_finish_requeue_request_body(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) 		spin_unlock_irqrestore(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) 		 * Request rq may be still/already in the scheduler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) 		 * in which case we need to remove it (this should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) 		 * never happen in case of requeue). And we cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) 		 * defer such a check and removal, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) 		 * inconsistencies in the time interval from the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) 		 * of this function to the start of the deferred work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948) 		 * This situation seems to occur only in process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) 		 * context, as a consequence of a merge. In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) 		 * current version of the code, this implies that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) 		 * lock is held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) 		if (!RB_EMPTY_NODE(&rq->rb_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955) 			bfq_remove_request(rq->q, rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956) 			bfqg_stats_update_io_remove(bfqq_group(bfqq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957) 						    rq->cmd_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959) 		bfq_finish_requeue_request_body(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963) 	 * Reset private fields. In case of a requeue, this allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964) 	 * this function to correctly do nothing if it is spuriously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965) 	 * invoked again on this same request (see the check at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966) 	 * beginning of the function). Probably, a better general
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967) 	 * design would be to prevent blk-mq from invoking the requeue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) 	 * or finish hooks of an elevator, for a request that is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) 	 * referred by that elevator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) 	 * Resetting the following fields would break the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972) 	 * request-insertion logic if rq is re-inserted into a bfq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) 	 * internal queue, without a re-preparation. Here we assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) 	 * that re-insertions of requeued requests, without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) 	 * re-preparation, can happen only for pass_through or at_head
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) 	 * requests (which are not re-inserted into bfq internal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) 	 * queues).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) 	rq->elv.priv[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) 	rq->elv.priv[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984)  * Removes the association between the current task and bfqq, assuming
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985)  * that bic points to the bfq iocontext of the task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986)  * Returns NULL if a new bfqq should be allocated, or the old bfqq if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987)  * was the last process referring to that bfqq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) static struct bfq_queue *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) 	bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994) 	if (bfqq_process_refs(bfqq) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995) 		bfqq->pid = current->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996) 		bfq_clear_bfqq_coop(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997) 		bfq_clear_bfqq_split_coop(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998) 		return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001) 	bic_set_bfqq(bic, NULL, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003) 	bfq_put_cooperator(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005) 	bfq_release_process_ref(bfqq->bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009) static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) 						   struct bfq_io_cq *bic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) 						   struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012) 						   bool split, bool is_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) 						   bool *new_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) 	struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) 	if (likely(bfqq && bfqq != &bfqd->oom_bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) 		return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) 	if (new_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) 		*new_queue = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) 		bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) 	bfqq = bfq_get_queue(bfqd, bio, is_sync, bic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) 	bic_set_bfqq(bic, bfqq, is_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) 	if (split && is_sync) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) 		if ((bic->was_in_burst_list && bfqd->large_burst) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) 		    bic->saved_in_large_burst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) 			bfq_mark_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) 			bfq_clear_bfqq_in_large_burst(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) 			if (bic->was_in_burst_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036) 				 * If bfqq was in the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) 				 * burst list before being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) 				 * merged, then we have to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039) 				 * it back. And we do not need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) 				 * to increase burst_size, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) 				 * we did not decrement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) 				 * burst_size when we removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) 				 * bfqq from the burst list as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) 				 * a consequence of a merge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) 				 * (see comments in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) 				 * bfq_put_queue). In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) 				 * respect, it would be rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) 				 * costly to know whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) 				 * current burst list is still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) 				 * the same burst list from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) 				 * which bfqq was removed on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) 				 * the merge. To avoid this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) 				 * cost, if bfqq was in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) 				 * burst list, then we add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) 				 * bfqq to the current burst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) 				 * list without any further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) 				 * check. This can cause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) 				 * inappropriate insertions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) 				 * but rarely enough to not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) 				 * harm the detection of large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) 				 * bursts significantly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) 				hlist_add_head(&bfqq->burst_list_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) 					       &bfqd->burst_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) 		bfqq->split_time = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073)  * Only reset private fields. The actual request preparation will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074)  * performed by bfq_init_rq, when rq is either inserted or merged. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075)  * comments on bfq_init_rq for the reason behind this delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076)  * preparation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) static void bfq_prepare_request(struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) 	 * Regardless of whether we have an icq attached, we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) 	 * clear the scheduler pointers, as they might point to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) 	 * previously allocated bic/bfqq structs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) 	rq->elv.priv[0] = rq->elv.priv[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089)  * If needed, init rq, allocate bfq data structures associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090)  * rq, and increment reference counters in the destination bfq_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091)  * for rq. Return the destination bfq_queue for rq, or NULL is rq is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092)  * not associated with any bfq_queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094)  * This function is invoked by the functions that perform rq insertion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095)  * or merging. One may have expected the above preparation operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096)  * to be performed in bfq_prepare_request, and not delayed to when rq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097)  * is inserted or merged. The rationale behind this delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098)  * preparation is that, after the prepare_request hook is invoked for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099)  * rq, rq may still be transformed into a request with no icq, i.e., a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100)  * request not associated with any queue. No bfq hook is invoked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101)  * signal this transformation. As a consequence, should these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102)  * preparation operations be performed when the prepare_request hook
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103)  * is invoked, and should rq be transformed one moment later, bfq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104)  * would end up in an inconsistent state, because it would have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105)  * incremented some queue counters for an rq destined to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106)  * transformation, without any chance to correctly lower these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107)  * counters back. In contrast, no transformation can still happen for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108)  * rq after rq has been inserted or merged. So, it is safe to execute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109)  * these preparation operations when rq is finally inserted or merged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) static struct bfq_queue *bfq_init_rq(struct request *rq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) 	struct request_queue *q = rq->q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) 	struct bio *bio = rq->bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) 	struct bfq_data *bfqd = q->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) 	struct bfq_io_cq *bic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) 	const int is_sync = rq_is_sync(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118) 	struct bfq_queue *bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) 	bool new_queue = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) 	bool bfqq_already_existing = false, split = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) 	if (unlikely(!rq->elv.icq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) 	 * Assuming that elv.priv[1] is set only if everything is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) 	 * for this rq. This holds true, because this function is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) 	 * invoked only for insertion or merging, and, after such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) 	 * events, a request cannot be manipulated any longer before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) 	 * being removed from bfq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) 	if (rq->elv.priv[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) 		return rq->elv.priv[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135) 	bic = icq_to_bic(rq->elv.icq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137) 	bfq_check_ioprio_change(bic, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139) 	bfq_bic_update_cgroup(bic, bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) 	bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) 					 &new_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) 	if (likely(!new_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) 		/* If the queue was seeky for too long, break it apart. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) 		if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147) 			bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) 			/* Update bic before losing reference to bfqq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) 			if (bfq_bfqq_in_large_burst(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) 				bic->saved_in_large_burst = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) 			bfqq = bfq_split_bfqq(bic, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) 			split = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156) 			if (!bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) 				bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) 								 true, is_sync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159) 								 NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) 				bfqq_already_existing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) 	bfqq->allocated++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166) 	bfqq->ref++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167) 	bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168) 		     rq, bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) 	rq->elv.priv[0] = bic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) 	rq->elv.priv[1] = bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) 	 * If a bfq_queue has only one process reference, it is owned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) 	 * by only this bic: we can then set bfqq->bic = bic. in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) 	 * addition, if the queue has also just been split, we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) 	 * resume its state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) 	if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) 		bfqq->bic = bic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) 		if (split) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) 			 * The queue has just been split from a shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) 			 * queue: restore the idle window and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) 			 * possible weight raising period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) 			bfq_bfqq_resume_state(bfqq, bfqd, bic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) 					      bfqq_already_existing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) 	 * Consider bfqq as possibly belonging to a burst of newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) 	 * created queues only if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) 	 * 1) A burst is actually happening (bfqd->burst_size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) 	 * or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) 	 * 2) There is no other active queue. In fact, if, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) 	 *    contrast, there are active queues not belonging to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) 	 *    possible burst bfqq may belong to, then there is no gain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) 	 *    in considering bfqq as belonging to a burst, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) 	 *    therefore in not weight-raising bfqq. See comments on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) 	 *    bfq_handle_burst().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) 	 * This filtering also helps eliminating false positives,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) 	 * occurring when bfqq does not belong to an actual large
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206) 	 * burst, but some background task (e.g., a service) happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) 	 * to trigger the creation of new queues very close to when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) 	 * bfqq and its possible companion queues are created. See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209) 	 * comments on bfq_handle_burst() for further details also on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210) 	 * this issue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) 	if (unlikely(bfq_bfqq_just_created(bfqq) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) 		     (bfqd->burst_size > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) 		      bfq_tot_busy_queues(bfqd) == 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) 		bfq_handle_burst(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217) 	return bfqq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221) bfq_idle_slice_timer_body(struct bfq_data *bfqd, struct bfq_queue *bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) 	enum bfqq_expiration reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) 	spin_lock_irqsave(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) 	 * Considering that bfqq may be in race, we should firstly check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230) 	 * whether bfqq is in service before doing something on it. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) 	 * the bfqq in race is not in service, it has already been expired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) 	 * through __bfq_bfqq_expire func and its wait_request flags has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) 	 * been cleared in __bfq_bfqd_reset_in_service func.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) 	if (bfqq != bfqd->in_service_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) 		spin_unlock_irqrestore(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240) 	bfq_clear_bfqq_wait_request(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) 	if (bfq_bfqq_budget_timeout(bfqq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244) 		 * Also here the queue can be safely expired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) 		 * for budget timeout without wasting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) 		 * guarantees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248) 		reason = BFQQE_BUDGET_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249) 	else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) 		 * The queue may not be empty upon timer expiration,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252) 		 * because we may not disable the timer when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) 		 * first request of the in-service queue arrives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) 		 * during disk idling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) 		reason = BFQQE_TOO_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) 		goto schedule_dispatch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) 	bfq_bfqq_expire(bfqd, bfqq, true, reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) schedule_dispatch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) 	spin_unlock_irqrestore(&bfqd->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) 	bfq_schedule_dispatch(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268)  * Handler of the expiration of the timer running if the in-service queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269)  * is idling inside its time slice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271) static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) 	struct bfq_data *bfqd = container_of(timer, struct bfq_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) 					     idle_slice_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6275) 	struct bfq_queue *bfqq = bfqd->in_service_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6277) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6278) 	 * Theoretical race here: the in-service queue can be NULL or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6279) 	 * different from the queue that was idling if a new request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6280) 	 * arrives for the current queue and there is a full dispatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6281) 	 * cycle that changes the in-service queue.  This can hardly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6282) 	 * happen, but in the worst case we just expire a queue too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6283) 	 * early.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6284) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6285) 	if (bfqq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6286) 		bfq_idle_slice_timer_body(bfqd, bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6288) 	return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6291) static void __bfq_put_async_bfqq(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6292) 				 struct bfq_queue **bfqq_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6294) 	struct bfq_queue *bfqq = *bfqq_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6296) 	bfq_log(bfqd, "put_async_bfqq: %p", bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6297) 	if (bfqq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6298) 		bfq_bfqq_move(bfqd, bfqq, bfqd->root_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6300) 		bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6301) 			     bfqq, bfqq->ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6302) 		bfq_put_queue(bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6303) 		*bfqq_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6308)  * Release all the bfqg references to its async queues.  If we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6309)  * deallocating the group these queues may still contain requests, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6310)  * we reparent them to the root cgroup (i.e., the only one that will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6311)  * exist for sure until all the requests on a device are gone).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6312)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6313) void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6315) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6317) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6318) 		for (j = 0; j < IOPRIO_BE_NR; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6319) 			__bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6321) 	__bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6324) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6325)  * See the comments on bfq_limit_depth for the purpose of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6326)  * the depths set in the function. Return minimum shallow depth we'll use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6327)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6328) static unsigned int bfq_update_depths(struct bfq_data *bfqd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6329) 				      struct sbitmap_queue *bt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6331) 	unsigned int i, j, min_shallow = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6334) 	 * In-word depths if no bfq_queue is being weight-raised:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6335) 	 * leaving 25% of tags only for sync reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6336) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6337) 	 * In next formulas, right-shift the value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6338) 	 * (1U<<bt->sb.shift), instead of computing directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6339) 	 * (1U<<(bt->sb.shift - something)), to be robust against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6340) 	 * any possible value of bt->sb.shift, without having to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6341) 	 * limit 'something'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6342) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6343) 	/* no more than 50% of tags for async I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6344) 	bfqd->word_depths[0][0] = max((1U << bt->sb.shift) >> 1, 1U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6345) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6346) 	 * no more than 75% of tags for sync writes (25% extra tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6347) 	 * w.r.t. async I/O, to prevent async I/O from starving sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6348) 	 * writes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6349) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6350) 	bfqd->word_depths[0][1] = max(((1U << bt->sb.shift) * 3) >> 2, 1U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6352) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6353) 	 * In-word depths in case some bfq_queue is being weight-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6354) 	 * raised: leaving ~63% of tags for sync reads. This is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6355) 	 * highest percentage for which, in our tests, application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6356) 	 * start-up times didn't suffer from any regression due to tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6357) 	 * shortage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6358) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6359) 	/* no more than ~18% of tags for async I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6360) 	bfqd->word_depths[1][0] = max(((1U << bt->sb.shift) * 3) >> 4, 1U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6361) 	/* no more than ~37% of tags for sync writes (~20% extra tags) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6362) 	bfqd->word_depths[1][1] = max(((1U << bt->sb.shift) * 6) >> 4, 1U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6364) 	for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6365) 		for (j = 0; j < 2; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6366) 			min_shallow = min(min_shallow, bfqd->word_depths[i][j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6368) 	return min_shallow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6371) static void bfq_depth_updated(struct blk_mq_hw_ctx *hctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6373) 	struct bfq_data *bfqd = hctx->queue->elevator->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6374) 	struct blk_mq_tags *tags = hctx->sched_tags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6375) 	unsigned int min_shallow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6377) 	min_shallow = bfq_update_depths(bfqd, tags->bitmap_tags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6378) 	sbitmap_queue_min_shallow_depth(tags->bitmap_tags, min_shallow);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6381) static int bfq_init_hctx(struct blk_mq_hw_ctx *hctx, unsigned int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6383) 	bfq_depth_updated(hctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6384) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6387) static void bfq_exit_queue(struct elevator_queue *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6389) 	struct bfq_data *bfqd = e->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6390) 	struct bfq_queue *bfqq, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6392) 	hrtimer_cancel(&bfqd->idle_slice_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6394) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6395) 	list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6396) 		bfq_deactivate_bfqq(bfqd, bfqq, false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6397) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6399) 	hrtimer_cancel(&bfqd->idle_slice_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6401) 	/* release oom-queue reference to root group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6402) 	bfqg_and_blkg_put(bfqd->root_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6404) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6405) 	blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6406) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6407) 	spin_lock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6408) 	bfq_put_async_queues(bfqd, bfqd->root_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6409) 	kfree(bfqd->root_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6410) 	spin_unlock_irq(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6411) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6413) 	wbt_enable_default(bfqd->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6415) 	kfree(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6418) static void bfq_init_root_group(struct bfq_group *root_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6419) 				struct bfq_data *bfqd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6421) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6423) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6424) 	root_group->entity.parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6425) 	root_group->my_entity = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6426) 	root_group->bfqd = bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6427) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6428) 	root_group->rq_pos_tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6429) 	for (i = 0; i < BFQ_IOPRIO_CLASSES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6430) 		root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6431) 	root_group->sched_data.bfq_class_idle_last_service = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6434) static int bfq_init_queue(struct request_queue *q, struct elevator_type *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6436) 	struct bfq_data *bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6437) 	struct elevator_queue *eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6439) 	eq = elevator_alloc(q, e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6440) 	if (!eq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6441) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6443) 	bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6444) 	if (!bfqd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6445) 		kobject_put(&eq->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6446) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6448) 	eq->elevator_data = bfqd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6450) 	spin_lock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6451) 	q->elevator = eq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6452) 	spin_unlock_irq(&q->queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6454) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6455) 	 * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6456) 	 * Grab a permanent reference to it, so that the normal code flow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6457) 	 * will not attempt to free it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6458) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6459) 	bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6460) 	bfqd->oom_bfqq.ref++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6461) 	bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6462) 	bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6463) 	bfqd->oom_bfqq.entity.new_weight =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6464) 		bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6466) 	/* oom_bfqq does not participate to bursts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6467) 	bfq_clear_bfqq_just_created(&bfqd->oom_bfqq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6469) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6470) 	 * Trigger weight initialization, according to ioprio, at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6471) 	 * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6472) 	 * class won't be changed any more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6474) 	bfqd->oom_bfqq.entity.prio_changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6476) 	bfqd->queue = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6478) 	INIT_LIST_HEAD(&bfqd->dispatch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6480) 	hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6481) 		     HRTIMER_MODE_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6482) 	bfqd->idle_slice_timer.function = bfq_idle_slice_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6484) 	bfqd->queue_weights_tree = RB_ROOT_CACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6485) 	bfqd->num_groups_with_pending_reqs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6487) 	INIT_LIST_HEAD(&bfqd->active_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6488) 	INIT_LIST_HEAD(&bfqd->idle_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6489) 	INIT_HLIST_HEAD(&bfqd->burst_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6491) 	bfqd->hw_tag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6492) 	bfqd->nonrot_with_queueing = blk_queue_nonrot(bfqd->queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6494) 	bfqd->bfq_max_budget = bfq_default_max_budget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6496) 	bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6497) 	bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6498) 	bfqd->bfq_back_max = bfq_back_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6499) 	bfqd->bfq_back_penalty = bfq_back_penalty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6500) 	bfqd->bfq_slice_idle = bfq_slice_idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6501) 	bfqd->bfq_timeout = bfq_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6503) 	bfqd->bfq_requests_within_timer = 120;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6505) 	bfqd->bfq_large_burst_thresh = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6506) 	bfqd->bfq_burst_interval = msecs_to_jiffies(180);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6508) 	bfqd->low_latency = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6510) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6511) 	 * Trade-off between responsiveness and fairness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6512) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6513) 	bfqd->bfq_wr_coeff = 30;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6514) 	bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6515) 	bfqd->bfq_wr_max_time = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6516) 	bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6517) 	bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6518) 	bfqd->bfq_wr_max_softrt_rate = 7000; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6519) 					      * Approximate rate required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6520) 					      * to playback or record a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6521) 					      * high-definition compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6522) 					      * video.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6523) 					      */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6524) 	bfqd->wr_busy_queues = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6526) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6527) 	 * Begin by assuming, optimistically, that the device peak
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6528) 	 * rate is equal to 2/3 of the highest reference rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6529) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6530) 	bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6531) 		ref_wr_duration[blk_queue_nonrot(bfqd->queue)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6532) 	bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6534) 	spin_lock_init(&bfqd->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6536) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6537) 	 * The invocation of the next bfq_create_group_hierarchy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6538) 	 * function is the head of a chain of function calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6539) 	 * (bfq_create_group_hierarchy->blkcg_activate_policy->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6540) 	 * blk_mq_freeze_queue) that may lead to the invocation of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6541) 	 * has_work hook function. For this reason,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6542) 	 * bfq_create_group_hierarchy is invoked only after all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6543) 	 * scheduler data has been initialized, apart from the fields
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6544) 	 * that can be initialized only after invoking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6545) 	 * bfq_create_group_hierarchy. This, in particular, enables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6546) 	 * has_work to correctly return false. Of course, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6547) 	 * other inconsistencies, the blk-mq stack must then refrain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6548) 	 * from invoking further scheduler hooks before this init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6549) 	 * function is finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6551) 	bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6552) 	if (!bfqd->root_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6553) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6554) 	bfq_init_root_group(bfqd->root_group, bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6555) 	bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6557) 	wbt_disable_default(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6558) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6560) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6561) 	kfree(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6562) 	kobject_put(&eq->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6563) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6566) static void bfq_slab_kill(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6568) 	kmem_cache_destroy(bfq_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6571) static int __init bfq_slab_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6573) 	bfq_pool = KMEM_CACHE(bfq_queue, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6574) 	if (!bfq_pool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6575) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6576) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6579) static ssize_t bfq_var_show(unsigned int var, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6581) 	return sprintf(page, "%u\n", var);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6584) static int bfq_var_store(unsigned long *var, const char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6586) 	unsigned long new_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6587) 	int ret = kstrtoul(page, 10, &new_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6589) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6590) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6591) 	*var = new_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6592) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6595) #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6596) static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6597) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6598) 	struct bfq_data *bfqd = e->elevator_data;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6599) 	u64 __data = __VAR;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6600) 	if (__CONV == 1)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6601) 		__data = jiffies_to_msecs(__data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6602) 	else if (__CONV == 2)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6603) 		__data = div_u64(__data, NSEC_PER_MSEC);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6604) 	return bfq_var_show(__data, (page));				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6606) SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6607) SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6608) SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6609) SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6610) SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6611) SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6612) SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6613) SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6614) SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6615) #undef SHOW_FUNCTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6617) #define USEC_SHOW_FUNCTION(__FUNC, __VAR)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6618) static ssize_t __FUNC(struct elevator_queue *e, char *page)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6619) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6620) 	struct bfq_data *bfqd = e->elevator_data;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6621) 	u64 __data = __VAR;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6622) 	__data = div_u64(__data, NSEC_PER_USEC);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6623) 	return bfq_var_show(__data, (page));				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6625) USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6626) #undef USEC_SHOW_FUNCTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6628) #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6629) static ssize_t								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6630) __FUNC(struct elevator_queue *e, const char *page, size_t count)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6631) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6632) 	struct bfq_data *bfqd = e->elevator_data;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6633) 	unsigned long __data, __min = (MIN), __max = (MAX);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6634) 	int ret;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6635) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6636) 	ret = bfq_var_store(&__data, (page));				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6637) 	if (ret)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6638) 		return ret;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6639) 	if (__data < __min)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6640) 		__data = __min;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6641) 	else if (__data > __max)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6642) 		__data = __max;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6643) 	if (__CONV == 1)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6644) 		*(__PTR) = msecs_to_jiffies(__data);			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6645) 	else if (__CONV == 2)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6646) 		*(__PTR) = (u64)__data * NSEC_PER_MSEC;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6647) 	else								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6648) 		*(__PTR) = __data;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6649) 	return count;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6651) STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6652) 		INT_MAX, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6653) STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6654) 		INT_MAX, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6655) STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6656) STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6657) 		INT_MAX, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6658) STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6659) #undef STORE_FUNCTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6661) #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6662) static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6663) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6664) 	struct bfq_data *bfqd = e->elevator_data;			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6665) 	unsigned long __data, __min = (MIN), __max = (MAX);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6666) 	int ret;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6667) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6668) 	ret = bfq_var_store(&__data, (page));				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6669) 	if (ret)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6670) 		return ret;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6671) 	if (__data < __min)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6672) 		__data = __min;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6673) 	else if (__data > __max)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6674) 		__data = __max;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6675) 	*(__PTR) = (u64)__data * NSEC_PER_USEC;				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6676) 	return count;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6678) USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6679) 		    UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6680) #undef USEC_STORE_FUNCTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6682) static ssize_t bfq_max_budget_store(struct elevator_queue *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6683) 				    const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6685) 	struct bfq_data *bfqd = e->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6686) 	unsigned long __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6687) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6689) 	ret = bfq_var_store(&__data, (page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6690) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6691) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6693) 	if (__data == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6694) 		bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6695) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6696) 		if (__data > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6697) 			__data = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6698) 		bfqd->bfq_max_budget = __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6701) 	bfqd->bfq_user_max_budget = __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6703) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6706) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6707)  * Leaving this name to preserve name compatibility with cfq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6708)  * parameters, but this timeout is used for both sync and async.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6709)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6710) static ssize_t bfq_timeout_sync_store(struct elevator_queue *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6711) 				      const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6713) 	struct bfq_data *bfqd = e->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6714) 	unsigned long __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6715) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6717) 	ret = bfq_var_store(&__data, (page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6718) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6719) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6721) 	if (__data < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6722) 		__data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6723) 	else if (__data > INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6724) 		__data = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6726) 	bfqd->bfq_timeout = msecs_to_jiffies(__data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6727) 	if (bfqd->bfq_user_max_budget == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6728) 		bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6730) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6733) static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6734) 				     const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6736) 	struct bfq_data *bfqd = e->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6737) 	unsigned long __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6738) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6740) 	ret = bfq_var_store(&__data, (page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6741) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6742) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6744) 	if (__data > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6745) 		__data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6746) 	if (!bfqd->strict_guarantees && __data == 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6747) 	    && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6748) 		bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6750) 	bfqd->strict_guarantees = __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6752) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6755) static ssize_t bfq_low_latency_store(struct elevator_queue *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6756) 				     const char *page, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6758) 	struct bfq_data *bfqd = e->elevator_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6759) 	unsigned long __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6760) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6762) 	ret = bfq_var_store(&__data, (page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6763) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6764) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6766) 	if (__data > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6767) 		__data = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6768) 	if (__data == 0 && bfqd->low_latency != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6769) 		bfq_end_wr(bfqd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6770) 	bfqd->low_latency = __data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6772) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6775) #define BFQ_ATTR(name) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6776) 	__ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6778) static struct elv_fs_entry bfq_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6779) 	BFQ_ATTR(fifo_expire_sync),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6780) 	BFQ_ATTR(fifo_expire_async),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6781) 	BFQ_ATTR(back_seek_max),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6782) 	BFQ_ATTR(back_seek_penalty),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6783) 	BFQ_ATTR(slice_idle),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6784) 	BFQ_ATTR(slice_idle_us),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6785) 	BFQ_ATTR(max_budget),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6786) 	BFQ_ATTR(timeout_sync),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6787) 	BFQ_ATTR(strict_guarantees),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6788) 	BFQ_ATTR(low_latency),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6789) 	__ATTR_NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6790) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6792) static struct elevator_type iosched_bfq_mq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6793) 	.ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6794) 		.limit_depth		= bfq_limit_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6795) 		.prepare_request	= bfq_prepare_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6796) 		.requeue_request        = bfq_finish_requeue_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6797) 		.finish_request		= bfq_finish_requeue_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6798) 		.exit_icq		= bfq_exit_icq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6799) 		.insert_requests	= bfq_insert_requests,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6800) 		.dispatch_request	= bfq_dispatch_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6801) 		.next_request		= elv_rb_latter_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6802) 		.former_request		= elv_rb_former_request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6803) 		.allow_merge		= bfq_allow_bio_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6804) 		.bio_merge		= bfq_bio_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6805) 		.request_merge		= bfq_request_merge,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6806) 		.requests_merged	= bfq_requests_merged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6807) 		.request_merged		= bfq_request_merged,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6808) 		.has_work		= bfq_has_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6809) 		.depth_updated		= bfq_depth_updated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6810) 		.init_hctx		= bfq_init_hctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6811) 		.init_sched		= bfq_init_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6812) 		.exit_sched		= bfq_exit_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6813) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6815) 	.icq_size =		sizeof(struct bfq_io_cq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6816) 	.icq_align =		__alignof__(struct bfq_io_cq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6817) 	.elevator_attrs =	bfq_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6818) 	.elevator_name =	"bfq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6819) 	.elevator_owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6820) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6821) MODULE_ALIAS("bfq-iosched");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6823) static int __init bfq_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6825) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6827) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6828) 	ret = blkcg_policy_register(&blkcg_policy_bfq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6829) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6830) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6831) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6833) 	ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6834) 	if (bfq_slab_setup())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6835) 		goto err_pol_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6837) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6838) 	 * Times to load large popular applications for the typical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6839) 	 * systems installed on the reference devices (see the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6840) 	 * comments before the definition of the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6841) 	 * array). Actually, we use slightly lower values, as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6842) 	 * estimated peak rate tends to be smaller than the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6843) 	 * peak rate.  The reason for this last fact is that estimates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6844) 	 * are computed over much shorter time intervals than the long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6845) 	 * intervals typically used for benchmarking. Why? First, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6846) 	 * adapt more quickly to variations. Second, because an I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6847) 	 * scheduler cannot rely on a peak-rate-evaluation workload to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6848) 	 * be run for a long time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6849) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6850) 	ref_wr_duration[0] = msecs_to_jiffies(7000); /* actually 8 sec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6851) 	ref_wr_duration[1] = msecs_to_jiffies(2500); /* actually 3 sec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6853) 	ret = elv_register(&iosched_bfq_mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6854) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6855) 		goto slab_kill;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6857) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6859) slab_kill:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6860) 	bfq_slab_kill();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6861) err_pol_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6862) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6863) 	blkcg_policy_unregister(&blkcg_policy_bfq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6864) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6865) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6868) static void __exit bfq_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6870) 	elv_unregister(&iosched_bfq_mq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6871) #ifdef CONFIG_BFQ_GROUP_IOSCHED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6872) 	blkcg_policy_unregister(&blkcg_policy_bfq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6873) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6874) 	bfq_slab_kill();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6877) module_init(bfq_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6878) module_exit(bfq_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6880) MODULE_AUTHOR("Paolo Valente");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6881) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6882) MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler");