Christoph Hellwig | a497ee3 | 2019-04-30 14:42:40 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Header file for the BFQ I/O scheduler: data structures and |
| 4 | * prototypes of interface functions among BFQ components. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 5 | */ |
| 6 | #ifndef _BFQ_H |
| 7 | #define _BFQ_H |
| 8 | |
| 9 | #include <linux/blktrace_api.h> |
| 10 | #include <linux/hrtimer.h> |
| 11 | #include <linux/blk-cgroup.h> |
| 12 | |
Tejun Heo | 1d156646 | 2019-11-07 11:18:04 -0800 | [diff] [blame] | 13 | #include "blk-cgroup-rwstat.h" |
| 14 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 15 | #define BFQ_IOPRIO_CLASSES 3 |
| 16 | #define BFQ_CL_IDLE_TIMEOUT (HZ/5) |
| 17 | |
| 18 | #define BFQ_MIN_WEIGHT 1 |
| 19 | #define BFQ_MAX_WEIGHT 1000 |
| 20 | #define BFQ_WEIGHT_CONVERSION_COEFF 10 |
| 21 | |
| 22 | #define BFQ_DEFAULT_QUEUE_IOPRIO 4 |
| 23 | |
| 24 | #define BFQ_WEIGHT_LEGACY_DFL 100 |
| 25 | #define BFQ_DEFAULT_GRP_IOPRIO 0 |
| 26 | #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE |
| 27 | |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 28 | #define MAX_PID_STR_LENGTH 12 |
| 29 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 30 | /* |
| 31 | * Soft real-time applications are extremely more latency sensitive |
| 32 | * than interactive ones. Over-raise the weight of the former to |
| 33 | * privilege them against the latter. |
| 34 | */ |
| 35 | #define BFQ_SOFTRT_WEIGHT_FACTOR 100 |
| 36 | |
| 37 | struct bfq_entity; |
| 38 | |
| 39 | /** |
| 40 | * struct bfq_service_tree - per ioprio_class service tree. |
| 41 | * |
| 42 | * Each service tree represents a B-WF2Q+ scheduler on its own. Each |
| 43 | * ioprio_class has its own independent scheduler, and so its own |
| 44 | * bfq_service_tree. All the fields are protected by the queue lock |
| 45 | * of the containing bfqd. |
| 46 | */ |
| 47 | struct bfq_service_tree { |
| 48 | /* tree for active entities (i.e., those backlogged) */ |
| 49 | struct rb_root active; |
Hou Tao | 38c9140 | 2017-07-12 15:25:01 +0800 | [diff] [blame] | 50 | /* tree for idle entities (i.e., not backlogged, with V < F_i)*/ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 51 | struct rb_root idle; |
| 52 | |
| 53 | /* idle entity with minimum F_i */ |
| 54 | struct bfq_entity *first_idle; |
| 55 | /* idle entity with maximum F_i */ |
| 56 | struct bfq_entity *last_idle; |
| 57 | |
| 58 | /* scheduler virtual time */ |
| 59 | u64 vtime; |
| 60 | /* scheduler weight sum; active and idle entities contribute to it */ |
| 61 | unsigned long wsum; |
| 62 | }; |
| 63 | |
| 64 | /** |
| 65 | * struct bfq_sched_data - multi-class scheduler. |
| 66 | * |
| 67 | * bfq_sched_data is the basic scheduler queue. It supports three |
| 68 | * ioprio_classes, and can be used either as a toplevel queue or as an |
Paolo Valente | 46d556e | 2017-07-29 12:42:56 +0200 | [diff] [blame] | 69 | * intermediate queue in a hierarchical setup. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 70 | * |
| 71 | * The supported ioprio_classes are the same as in CFQ, in descending |
| 72 | * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE. |
| 73 | * Requests from higher priority queues are served before all the |
| 74 | * requests from lower priority queues; among requests of the same |
| 75 | * queue requests are served according to B-WF2Q+. |
Paolo Valente | 46d556e | 2017-07-29 12:42:56 +0200 | [diff] [blame] | 76 | * |
| 77 | * The schedule is implemented by the service trees, plus the field |
| 78 | * @next_in_service, which points to the entity on the active trees |
| 79 | * that will be served next, if 1) no changes in the schedule occurs |
| 80 | * before the current in-service entity is expired, 2) the in-service |
| 81 | * queue becomes idle when it expires, and 3) if the entity pointed by |
| 82 | * in_service_entity is not a queue, then the in-service child entity |
| 83 | * of the entity pointed by in_service_entity becomes idle on |
| 84 | * expiration. This peculiar definition allows for the following |
| 85 | * optimization, not yet exploited: while a given entity is still in |
| 86 | * service, we already know which is the best candidate for next |
Angelo Ruocco | 636b8fe | 2019-04-08 17:35:34 +0200 | [diff] [blame] | 87 | * service among the other active entities in the same parent |
Paolo Valente | 46d556e | 2017-07-29 12:42:56 +0200 | [diff] [blame] | 88 | * entity. We can then quickly compare the timestamps of the |
| 89 | * in-service entity with those of such best candidate. |
| 90 | * |
| 91 | * All fields are protected by the lock of the containing bfqd. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 92 | */ |
| 93 | struct bfq_sched_data { |
| 94 | /* entity in service */ |
| 95 | struct bfq_entity *in_service_entity; |
| 96 | /* head-of-line entity (see comments above) */ |
| 97 | struct bfq_entity *next_in_service; |
| 98 | /* array of service trees, one per ioprio_class */ |
| 99 | struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES]; |
| 100 | /* last time CLASS_IDLE was served */ |
| 101 | unsigned long bfq_class_idle_last_service; |
| 102 | |
| 103 | }; |
| 104 | |
| 105 | /** |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 106 | * struct bfq_weight_counter - counter of the number of all active queues |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 107 | * with a given weight. |
| 108 | */ |
| 109 | struct bfq_weight_counter { |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 110 | unsigned int weight; /* weight of the queues this counter refers to */ |
| 111 | unsigned int num_active; /* nr of active queues with this weight */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 112 | /* |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 113 | * Weights tree member (see bfq_data's @queue_weights_tree) |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 114 | */ |
| 115 | struct rb_node weights_node; |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * struct bfq_entity - schedulable entity. |
| 120 | * |
| 121 | * A bfq_entity is used to represent either a bfq_queue (leaf node in the |
| 122 | * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each |
| 123 | * entity belongs to the sched_data of the parent group in the cgroup |
| 124 | * hierarchy. Non-leaf entities have also their own sched_data, stored |
| 125 | * in @my_sched_data. |
| 126 | * |
| 127 | * Each entity stores independently its priority values; this would |
| 128 | * allow different weights on different devices, but this |
| 129 | * functionality is not exported to userspace by now. Priorities and |
| 130 | * weights are updated lazily, first storing the new values into the |
| 131 | * new_* fields, then setting the @prio_changed flag. As soon as |
| 132 | * there is a transition in the entity state that allows the priority |
| 133 | * update to take place the effective and the requested priority |
| 134 | * values are synchronized. |
| 135 | * |
| 136 | * Unless cgroups are used, the weight value is calculated from the |
| 137 | * ioprio to export the same interface as CFQ. When dealing with |
Angelo Ruocco | 636b8fe | 2019-04-08 17:35:34 +0200 | [diff] [blame] | 138 | * "well-behaved" queues (i.e., queues that do not spend too much |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 139 | * time to consume their budget and have true sequential behavior, and |
| 140 | * when there are no external factors breaking anticipation) the |
| 141 | * relative weights at each level of the cgroups hierarchy should be |
| 142 | * guaranteed. All the fields are protected by the queue lock of the |
| 143 | * containing bfqd. |
| 144 | */ |
| 145 | struct bfq_entity { |
| 146 | /* service_tree member */ |
| 147 | struct rb_node rb_node; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * Flag, true if the entity is on a tree (either the active or |
| 151 | * the idle one of its service_tree) or is in service. |
| 152 | */ |
Paolo Valente | 33a16a9 | 2020-02-03 11:40:57 +0100 | [diff] [blame] | 153 | bool on_st_or_in_serv; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 154 | |
| 155 | /* B-WF2Q+ start and finish timestamps [sectors/weight] */ |
| 156 | u64 start, finish; |
| 157 | |
| 158 | /* tree the entity is enqueued into; %NULL if not on a tree */ |
| 159 | struct rb_root *tree; |
| 160 | |
| 161 | /* |
| 162 | * minimum start time of the (active) subtree rooted at this |
| 163 | * entity; used for O(log N) lookups into active trees |
| 164 | */ |
| 165 | u64 min_start; |
| 166 | |
| 167 | /* amount of service received during the last service slot */ |
| 168 | int service; |
| 169 | |
| 170 | /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */ |
| 171 | int budget; |
| 172 | |
Fam Zheng | 795fe54 | 2019-08-28 11:54:53 +0800 | [diff] [blame] | 173 | /* device weight, if non-zero, it overrides the default weight of |
| 174 | * bfq_group_data */ |
| 175 | int dev_weight; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 176 | /* weight of the queue */ |
| 177 | int weight; |
| 178 | /* next weight if a change is in progress */ |
| 179 | int new_weight; |
| 180 | |
| 181 | /* original weight, used to implement weight boosting */ |
| 182 | int orig_weight; |
| 183 | |
| 184 | /* parent entity, for hierarchical scheduling */ |
| 185 | struct bfq_entity *parent; |
| 186 | |
| 187 | /* |
| 188 | * For non-leaf nodes in the hierarchy, the associated |
| 189 | * scheduler queue, %NULL on leaf nodes. |
| 190 | */ |
| 191 | struct bfq_sched_data *my_sched_data; |
| 192 | /* the scheduler queue this entity belongs to */ |
| 193 | struct bfq_sched_data *sched_data; |
| 194 | |
| 195 | /* flag, set to request a weight, ioprio or ioprio_class change */ |
| 196 | int prio_changed; |
Paolo Valente | ba7aeae | 2018-12-06 19:18:18 +0100 | [diff] [blame] | 197 | |
| 198 | /* flag, set if the entity is counted in groups_with_pending_reqs */ |
| 199 | bool in_groups_with_pending_reqs; |
Paolo Valente | 430a67f | 2021-03-04 18:46:27 +0100 | [diff] [blame] | 200 | |
| 201 | /* last child queue of entity created (for non-leaf entities) */ |
| 202 | struct bfq_queue *last_bfqq_created; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | struct bfq_group; |
| 206 | |
| 207 | /** |
| 208 | * struct bfq_ttime - per process thinktime stats. |
| 209 | */ |
| 210 | struct bfq_ttime { |
| 211 | /* completion time of the last request */ |
| 212 | u64 last_end_request; |
| 213 | |
| 214 | /* total process thinktime */ |
| 215 | u64 ttime_total; |
| 216 | /* number of thinktime samples */ |
| 217 | unsigned long ttime_samples; |
| 218 | /* average process thinktime */ |
| 219 | u64 ttime_mean; |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * struct bfq_queue - leaf schedulable entity. |
| 224 | * |
| 225 | * A bfq_queue is a leaf request queue; it can be associated with an |
| 226 | * io_context or more, if it is async or shared between cooperating |
| 227 | * processes. @cgroup holds a reference to the cgroup, to be sure that it |
| 228 | * does not disappear while a bfqq still references it (mostly to avoid |
| 229 | * races between request issuing and task migration followed by cgroup |
| 230 | * destruction). |
| 231 | * All the fields are protected by the queue lock of the containing bfqd. |
| 232 | */ |
| 233 | struct bfq_queue { |
| 234 | /* reference counter */ |
| 235 | int ref; |
Paolo Valente | 430a67f | 2021-03-04 18:46:27 +0100 | [diff] [blame] | 236 | /* counter of references from other queues for delayed stable merge */ |
| 237 | int stable_ref; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 238 | /* parent bfq_data */ |
| 239 | struct bfq_data *bfqd; |
| 240 | |
| 241 | /* current ioprio and ioprio class */ |
| 242 | unsigned short ioprio, ioprio_class; |
| 243 | /* next ioprio and ioprio class if a change is in progress */ |
| 244 | unsigned short new_ioprio, new_ioprio_class; |
| 245 | |
Paolo Valente | 2341d662 | 2019-03-12 09:59:29 +0100 | [diff] [blame] | 246 | /* last total-service-time sample, see bfq_update_inject_limit() */ |
| 247 | u64 last_serv_time_ns; |
| 248 | /* limit for request injection */ |
| 249 | unsigned int inject_limit; |
| 250 | /* last time the inject limit has been decreased, in jiffies */ |
| 251 | unsigned long decrease_time_jif; |
| 252 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 253 | /* |
| 254 | * Shared bfq_queue if queue is cooperating with one or more |
| 255 | * other queues. |
| 256 | */ |
| 257 | struct bfq_queue *new_bfqq; |
| 258 | /* request-position tree member (see bfq_group's @rq_pos_tree) */ |
| 259 | struct rb_node pos_node; |
| 260 | /* request-position tree root (see bfq_group's @rq_pos_tree) */ |
| 261 | struct rb_root *pos_root; |
| 262 | |
| 263 | /* sorted list of pending requests */ |
| 264 | struct rb_root sort_list; |
| 265 | /* if fifo isn't expired, next request to serve */ |
| 266 | struct request *next_rq; |
| 267 | /* number of sync and async requests queued */ |
| 268 | int queued[2]; |
| 269 | /* number of requests currently allocated */ |
| 270 | int allocated; |
| 271 | /* number of pending metadata requests */ |
| 272 | int meta_pending; |
| 273 | /* fifo list of requests in sort_list */ |
| 274 | struct list_head fifo; |
| 275 | |
| 276 | /* entity representing this queue in the scheduler */ |
| 277 | struct bfq_entity entity; |
| 278 | |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 279 | /* pointer to the weight counter associated with this entity */ |
| 280 | struct bfq_weight_counter *weight_counter; |
| 281 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 282 | /* maximum budget allowed from the feedback mechanism */ |
| 283 | int max_budget; |
| 284 | /* budget expiration (in jiffies) */ |
| 285 | unsigned long budget_timeout; |
| 286 | |
| 287 | /* number of requests on the dispatch list or inside driver */ |
| 288 | int dispatched; |
| 289 | |
| 290 | /* status flags */ |
| 291 | unsigned long flags; |
| 292 | |
| 293 | /* node for active/idle bfqq list inside parent bfqd */ |
| 294 | struct list_head bfqq_list; |
| 295 | |
| 296 | /* associated @bfq_ttime struct */ |
| 297 | struct bfq_ttime ttime; |
| 298 | |
Paolo Valente | eb2fd80 | 2021-01-25 20:02:43 +0100 | [diff] [blame] | 299 | /* when bfqq started to do I/O within the last observation window */ |
| 300 | u64 io_start_time; |
| 301 | /* how long bfqq has remained empty during the last observ. window */ |
| 302 | u64 tot_idle_time; |
| 303 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 304 | /* bit vector: a 1 for each seeky requests in history */ |
| 305 | u32 seek_history; |
| 306 | |
| 307 | /* node for the device's burst list */ |
| 308 | struct hlist_node burst_list_node; |
| 309 | |
| 310 | /* position of the last request enqueued */ |
| 311 | sector_t last_request_pos; |
| 312 | |
| 313 | /* Number of consecutive pairs of request completion and |
| 314 | * arrival, such that the queue becomes idle after the |
| 315 | * completion, but the next request arrives within an idle |
| 316 | * time slice; used only if the queue's IO_bound flag has been |
| 317 | * cleared. |
| 318 | */ |
| 319 | unsigned int requests_within_timer; |
| 320 | |
| 321 | /* pid of the process owning the queue, used for logging purposes */ |
| 322 | pid_t pid; |
| 323 | |
| 324 | /* |
| 325 | * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL |
| 326 | * if the queue is shared. |
| 327 | */ |
| 328 | struct bfq_io_cq *bic; |
| 329 | |
| 330 | /* current maximum weight-raising time for this queue */ |
| 331 | unsigned long wr_cur_max_time; |
| 332 | /* |
| 333 | * Minimum time instant such that, only if a new request is |
| 334 | * enqueued after this time instant in an idle @bfq_queue with |
| 335 | * no outstanding requests, then the task associated with the |
| 336 | * queue it is deemed as soft real-time (see the comments on |
| 337 | * the function bfq_bfqq_softrt_next_start()) |
| 338 | */ |
| 339 | unsigned long soft_rt_next_start; |
| 340 | /* |
| 341 | * Start time of the current weight-raising period if |
| 342 | * the @bfq-queue is being weight-raised, otherwise |
| 343 | * finish time of the last weight-raising period. |
| 344 | */ |
| 345 | unsigned long last_wr_start_finish; |
| 346 | /* factor by which the weight of this queue is multiplied */ |
| 347 | unsigned int wr_coeff; |
| 348 | /* |
| 349 | * Time of the last transition of the @bfq_queue from idle to |
| 350 | * backlogged. |
| 351 | */ |
| 352 | unsigned long last_idle_bklogged; |
| 353 | /* |
| 354 | * Cumulative service received from the @bfq_queue since the |
| 355 | * last transition from idle to backlogged. |
| 356 | */ |
| 357 | unsigned long service_from_backlogged; |
Paolo Valente | 8a8747d | 2018-01-13 12:05:18 +0100 | [diff] [blame] | 358 | /* |
| 359 | * Cumulative service received from the @bfq_queue since its |
| 360 | * last transition to weight-raised state. |
| 361 | */ |
| 362 | unsigned long service_from_wr; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * Value of wr start time when switching to soft rt |
| 366 | */ |
| 367 | unsigned long wr_start_at_switch_to_srt; |
| 368 | |
| 369 | unsigned long split_time; /* time of last split */ |
Paolo Valente | 7b8fa3b | 2017-12-20 12:38:33 +0100 | [diff] [blame] | 370 | |
| 371 | unsigned long first_IO_time; /* time of first I/O for this queue */ |
Paolo Valente | d0edc24 | 2018-09-14 16:23:08 +0200 | [diff] [blame] | 372 | |
Paolo Valente | 430a67f | 2021-03-04 18:46:27 +0100 | [diff] [blame] | 373 | unsigned long creation_time; /* when this queue is created */ |
| 374 | |
Paolo Valente | d0edc24 | 2018-09-14 16:23:08 +0200 | [diff] [blame] | 375 | /* max service rate measured so far */ |
| 376 | u32 max_service_rate; |
Paolo Valente | 13a857a | 2019-06-25 07:12:47 +0200 | [diff] [blame] | 377 | |
| 378 | /* |
| 379 | * Pointer to the waker queue for this queue, i.e., to the |
| 380 | * queue Q such that this queue happens to get new I/O right |
| 381 | * after some I/O request of Q is completed. For details, see |
| 382 | * the comments on the choice of the queue for injection in |
| 383 | * bfq_select_queue(). |
| 384 | */ |
| 385 | struct bfq_queue *waker_bfqq; |
Paolo Valente | 71217df | 2021-01-25 20:02:48 +0100 | [diff] [blame] | 386 | /* pointer to the curr. tentative waker queue, see bfq_check_waker() */ |
| 387 | struct bfq_queue *tentative_waker_bfqq; |
| 388 | /* number of times the same tentative waker has been detected */ |
| 389 | unsigned int num_waker_detections; |
| 390 | |
Paolo Valente | 13a857a | 2019-06-25 07:12:47 +0200 | [diff] [blame] | 391 | /* node for woken_list, see below */ |
| 392 | struct hlist_node woken_list_node; |
| 393 | /* |
| 394 | * Head of the list of the woken queues for this queue, i.e., |
| 395 | * of the list of the queues for which this queue is a waker |
| 396 | * queue. This list is used to reset the waker_bfqq pointer in |
| 397 | * the woken queues when this queue exits. |
| 398 | */ |
| 399 | struct hlist_head woken_list; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | /** |
| 403 | * struct bfq_io_cq - per (request_queue, io_context) structure. |
| 404 | */ |
| 405 | struct bfq_io_cq { |
| 406 | /* associated io_cq structure */ |
| 407 | struct io_cq icq; /* must be the first member */ |
| 408 | /* array of two process queues, the sync and the async */ |
| 409 | struct bfq_queue *bfqq[2]; |
| 410 | /* per (request_queue, blkcg) ioprio */ |
| 411 | int ioprio; |
| 412 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
| 413 | uint64_t blkcg_serial_nr; /* the current blkcg serial */ |
| 414 | #endif |
| 415 | /* |
Paolo Valente | d5be3fe | 2017-08-04 07:35:10 +0200 | [diff] [blame] | 416 | * Snapshot of the has_short_time flag before merging; taken |
| 417 | * to remember its value while the queue is merged, so as to |
| 418 | * be able to restore it in case of split. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 419 | */ |
Paolo Valente | d5be3fe | 2017-08-04 07:35:10 +0200 | [diff] [blame] | 420 | bool saved_has_short_ttime; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 421 | /* |
| 422 | * Same purpose as the previous two fields for the I/O bound |
| 423 | * classification of a queue. |
| 424 | */ |
| 425 | bool saved_IO_bound; |
| 426 | |
Paolo Valente | eb2fd80 | 2021-01-25 20:02:43 +0100 | [diff] [blame] | 427 | u64 saved_io_start_time; |
| 428 | u64 saved_tot_idle_time; |
| 429 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 430 | /* |
| 431 | * Same purpose as the previous fields for the value of the |
| 432 | * field keeping the queue's belonging to a large burst |
| 433 | */ |
| 434 | bool saved_in_large_burst; |
| 435 | /* |
| 436 | * True if the queue belonged to a burst list before its merge |
| 437 | * with another cooperating queue. |
| 438 | */ |
| 439 | bool was_in_burst_list; |
| 440 | |
| 441 | /* |
Francesco Pollicino | fffca08 | 2019-03-12 09:59:34 +0100 | [diff] [blame] | 442 | * Save the weight when a merge occurs, to be able |
| 443 | * to restore it in case of split. If the weight is not |
| 444 | * correctly resumed when the queue is recycled, |
| 445 | * then the weight of the recycled queue could differ |
| 446 | * from the weight of the original queue. |
| 447 | */ |
| 448 | unsigned int saved_weight; |
| 449 | |
| 450 | /* |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 451 | * Similar to previous fields: save wr information. |
| 452 | */ |
| 453 | unsigned long saved_wr_coeff; |
| 454 | unsigned long saved_last_wr_start_finish; |
Paolo Valente | e673914 | 2021-01-25 20:02:46 +0100 | [diff] [blame] | 455 | unsigned long saved_service_from_wr; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 456 | unsigned long saved_wr_start_at_switch_to_srt; |
| 457 | unsigned int saved_wr_cur_max_time; |
| 458 | struct bfq_ttime saved_ttime; |
Paolo Valente | 5a5436b | 2021-01-25 20:02:47 +0100 | [diff] [blame] | 459 | |
| 460 | /* Save also injection state */ |
| 461 | u64 saved_last_serv_time_ns; |
| 462 | unsigned int saved_inject_limit; |
| 463 | unsigned long saved_decrease_time_jif; |
Paolo Valente | 430a67f | 2021-03-04 18:46:27 +0100 | [diff] [blame] | 464 | |
| 465 | /* candidate queue for a stable merge (due to close creation time) */ |
| 466 | struct bfq_queue *stable_merge_bfqq; |
| 467 | |
| 468 | bool stably_merged; /* non splittable if true */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 469 | }; |
| 470 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 471 | /** |
| 472 | * struct bfq_data - per-device data structure. |
| 473 | * |
| 474 | * All the fields are protected by @lock. |
| 475 | */ |
| 476 | struct bfq_data { |
| 477 | /* device request queue */ |
| 478 | struct request_queue *queue; |
| 479 | /* dispatch queue */ |
| 480 | struct list_head dispatch; |
| 481 | |
| 482 | /* root bfq_group for the device */ |
| 483 | struct bfq_group *root_group; |
| 484 | |
| 485 | /* |
| 486 | * rbtree of weight counters of @bfq_queues, sorted by |
| 487 | * weight. Used to keep track of whether all @bfq_queues have |
| 488 | * the same weight. The tree contains one counter for each |
| 489 | * distinct weight associated to some active and not |
| 490 | * weight-raised @bfq_queue (see the comments to the functions |
| 491 | * bfq_weights_tree_[add|remove] for further details). |
| 492 | */ |
Paolo Valente | fb53ac6 | 2019-03-12 09:59:28 +0100 | [diff] [blame] | 493 | struct rb_root_cached queue_weights_tree; |
Paolo Valente | ba7aeae | 2018-12-06 19:18:18 +0100 | [diff] [blame] | 494 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 495 | /* |
Paolo Valente | ba7aeae | 2018-12-06 19:18:18 +0100 | [diff] [blame] | 496 | * Number of groups with at least one descendant process that |
| 497 | * has at least one request waiting for completion. Note that |
| 498 | * this accounts for also requests already dispatched, but not |
| 499 | * yet completed. Therefore this number of groups may differ |
| 500 | * (be larger) than the number of active groups, as a group is |
| 501 | * considered active only if its corresponding entity has |
| 502 | * descendant queues with at least one request queued. This |
| 503 | * number is used to decide whether a scenario is symmetric. |
| 504 | * For a detailed explanation see comments on the computation |
| 505 | * of the variable asymmetric_scenario in the function |
| 506 | * bfq_better_to_idle(). |
| 507 | * |
| 508 | * However, it is hard to compute this number exactly, for |
| 509 | * groups with multiple descendant processes. Consider a group |
| 510 | * that is inactive, i.e., that has no descendant process with |
| 511 | * pending I/O inside BFQ queues. Then suppose that |
| 512 | * num_groups_with_pending_reqs is still accounting for this |
| 513 | * group, because the group has descendant processes with some |
| 514 | * I/O request still in flight. num_groups_with_pending_reqs |
| 515 | * should be decremented when the in-flight request of the |
| 516 | * last descendant process is finally completed (assuming that |
| 517 | * nothing else has changed for the group in the meantime, in |
| 518 | * terms of composition of the group and active/inactive state of child |
| 519 | * groups and processes). To accomplish this, an additional |
| 520 | * pending-request counter must be added to entities, and must |
| 521 | * be updated correctly. To avoid this additional field and operations, |
| 522 | * we resort to the following tradeoff between simplicity and |
| 523 | * accuracy: for an inactive group that is still counted in |
| 524 | * num_groups_with_pending_reqs, we decrement |
| 525 | * num_groups_with_pending_reqs when the first descendant |
| 526 | * process of the group remains with no request waiting for |
| 527 | * completion. |
| 528 | * |
| 529 | * Even this simpler decrement strategy requires a little |
| 530 | * carefulness: to avoid multiple decrements, we flag a group, |
| 531 | * more precisely an entity representing a group, as still |
| 532 | * counted in num_groups_with_pending_reqs when it becomes |
| 533 | * inactive. Then, when the first descendant queue of the |
| 534 | * entity remains with no request waiting for completion, |
| 535 | * num_groups_with_pending_reqs is decremented, and this flag |
| 536 | * is reset. After this flag is reset for the entity, |
| 537 | * num_groups_with_pending_reqs won't be decremented any |
| 538 | * longer in case a new descendant queue of the entity remains |
| 539 | * with no request waiting for completion. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 540 | */ |
Paolo Valente | ba7aeae | 2018-12-06 19:18:18 +0100 | [diff] [blame] | 541 | unsigned int num_groups_with_pending_reqs; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 542 | |
| 543 | /* |
Paolo Valente | 73d5811 | 2019-01-29 12:06:29 +0100 | [diff] [blame] | 544 | * Per-class (RT, BE, IDLE) number of bfq_queues containing |
| 545 | * requests (including the queue in service, even if it is |
| 546 | * idling). |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 547 | */ |
Paolo Valente | 73d5811 | 2019-01-29 12:06:29 +0100 | [diff] [blame] | 548 | unsigned int busy_queues[3]; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 549 | /* number of weight-raised busy @bfq_queues */ |
| 550 | int wr_busy_queues; |
| 551 | /* number of queued requests */ |
| 552 | int queued; |
| 553 | /* number of requests dispatched and waiting for completion */ |
| 554 | int rq_in_driver; |
| 555 | |
Paolo Valente | 8cacc5a | 2019-03-12 09:59:30 +0100 | [diff] [blame] | 556 | /* true if the device is non rotational and performs queueing */ |
| 557 | bool nonrot_with_queueing; |
| 558 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 559 | /* |
| 560 | * Maximum number of requests in driver in the last |
| 561 | * @hw_tag_samples completed requests. |
| 562 | */ |
| 563 | int max_rq_in_driver; |
| 564 | /* number of samples used to calculate hw_tag */ |
| 565 | int hw_tag_samples; |
| 566 | /* flag set to one if the driver is showing a queueing behavior */ |
| 567 | int hw_tag; |
| 568 | |
| 569 | /* number of budgets assigned */ |
| 570 | int budgets_assigned; |
| 571 | |
| 572 | /* |
| 573 | * Timer set when idling (waiting) for the next request from |
| 574 | * the queue in service. |
| 575 | */ |
| 576 | struct hrtimer idle_slice_timer; |
| 577 | |
| 578 | /* bfq_queue in service */ |
| 579 | struct bfq_queue *in_service_queue; |
| 580 | |
| 581 | /* on-disk position of the last served request */ |
| 582 | sector_t last_position; |
| 583 | |
Paolo Valente | 058fdec | 2019-01-29 12:06:38 +0100 | [diff] [blame] | 584 | /* position of the last served request for the in-service queue */ |
| 585 | sector_t in_serv_last_pos; |
| 586 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 587 | /* time of last request completion (ns) */ |
| 588 | u64 last_completion; |
| 589 | |
Paolo Valente | 13a857a | 2019-06-25 07:12:47 +0200 | [diff] [blame] | 590 | /* bfqq owning the last completed rq */ |
| 591 | struct bfq_queue *last_completed_rq_bfqq; |
| 592 | |
Paolo Valente | 430a67f | 2021-03-04 18:46:27 +0100 | [diff] [blame] | 593 | /* last bfqq created, among those in the root group */ |
| 594 | struct bfq_queue *last_bfqq_created; |
| 595 | |
Paolo Valente | 2341d662 | 2019-03-12 09:59:29 +0100 | [diff] [blame] | 596 | /* time of last transition from empty to non-empty (ns) */ |
| 597 | u64 last_empty_occupied_ns; |
| 598 | |
| 599 | /* |
| 600 | * Flag set to activate the sampling of the total service time |
| 601 | * of a just-arrived first I/O request (see |
| 602 | * bfq_update_inject_limit()). This will cause the setting of |
| 603 | * waited_rq when the request is finally dispatched. |
| 604 | */ |
| 605 | bool wait_dispatch; |
| 606 | /* |
| 607 | * If set, then bfq_update_inject_limit() is invoked when |
| 608 | * waited_rq is eventually completed. |
| 609 | */ |
| 610 | struct request *waited_rq; |
| 611 | /* |
| 612 | * True if some request has been injected during the last service hole. |
| 613 | */ |
| 614 | bool rqs_injected; |
| 615 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 616 | /* time of first rq dispatch in current observation interval (ns) */ |
| 617 | u64 first_dispatch; |
| 618 | /* time of last rq dispatch in current observation interval (ns) */ |
| 619 | u64 last_dispatch; |
| 620 | |
| 621 | /* beginning of the last budget */ |
| 622 | ktime_t last_budget_start; |
| 623 | /* beginning of the last idle slice */ |
| 624 | ktime_t last_idling_start; |
Paolo Valente | 2341d662 | 2019-03-12 09:59:29 +0100 | [diff] [blame] | 625 | unsigned long last_idling_start_jiffies; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 626 | |
| 627 | /* number of samples in current observation interval */ |
| 628 | int peak_rate_samples; |
| 629 | /* num of samples of seq dispatches in current observation interval */ |
| 630 | u32 sequential_samples; |
| 631 | /* total num of sectors transferred in current observation interval */ |
| 632 | u64 tot_sectors_dispatched; |
| 633 | /* max rq size seen during current observation interval (sectors) */ |
| 634 | u32 last_rq_max_size; |
| 635 | /* time elapsed from first dispatch in current observ. interval (us) */ |
| 636 | u64 delta_from_first; |
| 637 | /* |
| 638 | * Current estimate of the device peak rate, measured in |
Paolo Valente | bc56e2c | 2018-03-26 16:06:24 +0200 | [diff] [blame] | 639 | * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 640 | * BFQ_RATE_SHIFT is performed to increase precision in |
| 641 | * fixed-point calculations. |
| 642 | */ |
| 643 | u32 peak_rate; |
| 644 | |
| 645 | /* maximum budget allotted to a bfq_queue before rescheduling */ |
| 646 | int bfq_max_budget; |
| 647 | |
| 648 | /* list of all the bfq_queues active on the device */ |
| 649 | struct list_head active_list; |
| 650 | /* list of all the bfq_queues idle on the device */ |
| 651 | struct list_head idle_list; |
| 652 | |
| 653 | /* |
| 654 | * Timeout for async/sync requests; when it fires, requests |
| 655 | * are served in fifo order. |
| 656 | */ |
| 657 | u64 bfq_fifo_expire[2]; |
| 658 | /* weight of backward seeks wrt forward ones */ |
| 659 | unsigned int bfq_back_penalty; |
| 660 | /* maximum allowed backward seek */ |
| 661 | unsigned int bfq_back_max; |
| 662 | /* maximum idling time */ |
| 663 | u32 bfq_slice_idle; |
| 664 | |
| 665 | /* user-configured max budget value (0 for auto-tuning) */ |
| 666 | int bfq_user_max_budget; |
| 667 | /* |
| 668 | * Timeout for bfq_queues to consume their budget; used to |
| 669 | * prevent seeky queues from imposing long latencies to |
| 670 | * sequential or quasi-sequential ones (this also implies that |
| 671 | * seeky queues cannot receive guarantees in the service |
| 672 | * domain; after a timeout they are charged for the time they |
| 673 | * have been in service, to preserve fairness among them, but |
| 674 | * without service-domain guarantees). |
| 675 | */ |
| 676 | unsigned int bfq_timeout; |
| 677 | |
| 678 | /* |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 679 | * Force device idling whenever needed to provide accurate |
| 680 | * service guarantees, without caring about throughput |
| 681 | * issues. CAVEAT: this may even increase latencies, in case |
| 682 | * of useless idling for processes that did stop doing I/O. |
| 683 | */ |
| 684 | bool strict_guarantees; |
| 685 | |
| 686 | /* |
| 687 | * Last time at which a queue entered the current burst of |
| 688 | * queues being activated shortly after each other; for more |
| 689 | * details about this and the following parameters related to |
| 690 | * a burst of activations, see the comments on the function |
| 691 | * bfq_handle_burst. |
| 692 | */ |
| 693 | unsigned long last_ins_in_burst; |
| 694 | /* |
| 695 | * Reference time interval used to decide whether a queue has |
| 696 | * been activated shortly after @last_ins_in_burst. |
| 697 | */ |
| 698 | unsigned long bfq_burst_interval; |
| 699 | /* number of queues in the current burst of queue activations */ |
| 700 | int burst_size; |
| 701 | |
| 702 | /* common parent entity for the queues in the burst */ |
| 703 | struct bfq_entity *burst_parent_entity; |
| 704 | /* Maximum burst size above which the current queue-activation |
| 705 | * burst is deemed as 'large'. |
| 706 | */ |
| 707 | unsigned long bfq_large_burst_thresh; |
| 708 | /* true if a large queue-activation burst is in progress */ |
| 709 | bool large_burst; |
| 710 | /* |
| 711 | * Head of the burst list (as for the above fields, more |
| 712 | * details in the comments on the function bfq_handle_burst). |
| 713 | */ |
| 714 | struct hlist_head burst_list; |
| 715 | |
| 716 | /* if set to true, low-latency heuristics are enabled */ |
| 717 | bool low_latency; |
| 718 | /* |
| 719 | * Maximum factor by which the weight of a weight-raised queue |
| 720 | * is multiplied. |
| 721 | */ |
| 722 | unsigned int bfq_wr_coeff; |
| 723 | /* maximum duration of a weight-raising period (jiffies) */ |
| 724 | unsigned int bfq_wr_max_time; |
| 725 | |
| 726 | /* Maximum weight-raising duration for soft real-time processes */ |
| 727 | unsigned int bfq_wr_rt_max_time; |
| 728 | /* |
| 729 | * Minimum idle period after which weight-raising may be |
| 730 | * reactivated for a queue (in jiffies). |
| 731 | */ |
| 732 | unsigned int bfq_wr_min_idle_time; |
| 733 | /* |
| 734 | * Minimum period between request arrivals after which |
| 735 | * weight-raising may be reactivated for an already busy async |
| 736 | * queue (in jiffies). |
| 737 | */ |
| 738 | unsigned long bfq_wr_min_inter_arr_async; |
| 739 | |
| 740 | /* Max service-rate for a soft real-time queue, in sectors/sec */ |
| 741 | unsigned int bfq_wr_max_softrt_rate; |
| 742 | /* |
Paolo Valente | e24f1c2 | 2018-05-31 16:45:06 +0200 | [diff] [blame] | 743 | * Cached value of the product ref_rate*ref_wr_duration, used |
| 744 | * for computing the maximum duration of weight raising |
| 745 | * automatically. |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 746 | */ |
Paolo Valente | e24f1c2 | 2018-05-31 16:45:06 +0200 | [diff] [blame] | 747 | u64 rate_dur_prod; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 748 | |
| 749 | /* fallback dummy bfqq for extreme OOM conditions */ |
| 750 | struct bfq_queue oom_bfqq; |
| 751 | |
| 752 | spinlock_t lock; |
| 753 | |
| 754 | /* |
| 755 | * bic associated with the task issuing current bio for |
| 756 | * merging. This and the next field are used as a support to |
| 757 | * be able to perform the bic lookup, needed by bio-merge |
| 758 | * functions, before the scheduler lock is taken, and thus |
| 759 | * avoid taking the request-queue lock while the scheduler |
| 760 | * lock is being held. |
| 761 | */ |
| 762 | struct bfq_io_cq *bio_bic; |
| 763 | /* bfqq associated with the task issuing current bio for merging */ |
| 764 | struct bfq_queue *bio_bfqq; |
Paolo Valente | a52a69e | 2018-01-13 12:05:17 +0100 | [diff] [blame] | 765 | |
| 766 | /* |
Paolo Valente | a52a69e | 2018-01-13 12:05:17 +0100 | [diff] [blame] | 767 | * Depth limits used in bfq_limit_depth (see comments on the |
| 768 | * function) |
| 769 | */ |
| 770 | unsigned int word_depths[2][2]; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | enum bfqq_state_flags { |
| 774 | BFQQF_just_created = 0, /* queue just allocated */ |
| 775 | BFQQF_busy, /* has requests or is in service */ |
| 776 | BFQQF_wait_request, /* waiting for a request */ |
| 777 | BFQQF_non_blocking_wait_rq, /* |
| 778 | * waiting for a request |
| 779 | * without idling the device |
| 780 | */ |
| 781 | BFQQF_fifo_expire, /* FIFO checked in this slice */ |
Paolo Valente | d5be3fe | 2017-08-04 07:35:10 +0200 | [diff] [blame] | 782 | BFQQF_has_short_ttime, /* queue has a short think time */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 783 | BFQQF_sync, /* synchronous queue */ |
| 784 | BFQQF_IO_bound, /* |
| 785 | * bfqq has timed-out at least once |
| 786 | * having consumed at most 2/10 of |
| 787 | * its budget |
| 788 | */ |
| 789 | BFQQF_in_large_burst, /* |
| 790 | * bfqq activated in a large burst, |
| 791 | * see comments to bfq_handle_burst. |
| 792 | */ |
| 793 | BFQQF_softrt_update, /* |
| 794 | * may need softrt-next-start |
| 795 | * update |
| 796 | */ |
| 797 | BFQQF_coop, /* bfqq is shared */ |
Paolo Valente | 13a857a | 2019-06-25 07:12:47 +0200 | [diff] [blame] | 798 | BFQQF_split_coop, /* shared bfqq will be split */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | #define BFQ_BFQQ_FNS(name) \ |
| 802 | void bfq_mark_bfqq_##name(struct bfq_queue *bfqq); \ |
| 803 | void bfq_clear_bfqq_##name(struct bfq_queue *bfqq); \ |
| 804 | int bfq_bfqq_##name(const struct bfq_queue *bfqq); |
| 805 | |
| 806 | BFQ_BFQQ_FNS(just_created); |
| 807 | BFQ_BFQQ_FNS(busy); |
| 808 | BFQ_BFQQ_FNS(wait_request); |
| 809 | BFQ_BFQQ_FNS(non_blocking_wait_rq); |
| 810 | BFQ_BFQQ_FNS(fifo_expire); |
Paolo Valente | d5be3fe | 2017-08-04 07:35:10 +0200 | [diff] [blame] | 811 | BFQ_BFQQ_FNS(has_short_ttime); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 812 | BFQ_BFQQ_FNS(sync); |
| 813 | BFQ_BFQQ_FNS(IO_bound); |
| 814 | BFQ_BFQQ_FNS(in_large_burst); |
| 815 | BFQ_BFQQ_FNS(coop); |
| 816 | BFQ_BFQQ_FNS(split_coop); |
| 817 | BFQ_BFQQ_FNS(softrt_update); |
| 818 | #undef BFQ_BFQQ_FNS |
| 819 | |
| 820 | /* Expiration reasons. */ |
| 821 | enum bfqq_expiration { |
| 822 | BFQQE_TOO_IDLE = 0, /* |
| 823 | * queue has been idling for |
| 824 | * too long |
| 825 | */ |
| 826 | BFQQE_BUDGET_TIMEOUT, /* budget took too long to be used */ |
| 827 | BFQQE_BUDGET_EXHAUSTED, /* budget consumed */ |
| 828 | BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */ |
| 829 | BFQQE_PREEMPTED /* preemption in progress */ |
| 830 | }; |
| 831 | |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 832 | struct bfq_stat { |
| 833 | struct percpu_counter cpu_cnt; |
| 834 | atomic64_t aux_cnt; |
| 835 | }; |
| 836 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 837 | struct bfqg_stats { |
Tejun Heo | fd41e60 | 2019-11-07 11:18:00 -0800 | [diff] [blame] | 838 | /* basic stats */ |
| 839 | struct blkg_rwstat bytes; |
| 840 | struct blkg_rwstat ios; |
Christoph Hellwig | 8060c47 | 2019-06-06 12:26:24 +0200 | [diff] [blame] | 841 | #ifdef CONFIG_BFQ_CGROUP_DEBUG |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 842 | /* number of ios merged */ |
| 843 | struct blkg_rwstat merged; |
| 844 | /* total time spent on device in ns, may not be accurate w/ queueing */ |
| 845 | struct blkg_rwstat service_time; |
| 846 | /* total time spent waiting in scheduler queue in ns */ |
| 847 | struct blkg_rwstat wait_time; |
| 848 | /* number of IOs queued up */ |
| 849 | struct blkg_rwstat queued; |
| 850 | /* total disk time and nr sectors dispatched by this group */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 851 | struct bfq_stat time; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 852 | /* sum of number of ios queued across all samples */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 853 | struct bfq_stat avg_queue_size_sum; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 854 | /* count of samples taken for average */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 855 | struct bfq_stat avg_queue_size_samples; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 856 | /* how many times this group has been removed from service tree */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 857 | struct bfq_stat dequeue; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 858 | /* total time spent waiting for it to be assigned a timeslice. */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 859 | struct bfq_stat group_wait_time; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 860 | /* time spent idling for this blkcg_gq */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 861 | struct bfq_stat idle_time; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 862 | /* total time with empty current active q with other requests queued */ |
Christoph Hellwig | c0ce79dc | 2019-06-06 12:26:22 +0200 | [diff] [blame] | 863 | struct bfq_stat empty_time; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 864 | /* fields after this shouldn't be cleared on stat reset */ |
Omar Sandoval | 84c7afc | 2018-05-09 02:08:51 -0700 | [diff] [blame] | 865 | u64 start_group_wait_time; |
| 866 | u64 start_idle_time; |
| 867 | u64 start_empty_time; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 868 | uint16_t flags; |
Christoph Hellwig | 8060c47 | 2019-06-06 12:26:24 +0200 | [diff] [blame] | 869 | #endif /* CONFIG_BFQ_CGROUP_DEBUG */ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
| 873 | |
| 874 | /* |
| 875 | * struct bfq_group_data - per-blkcg storage for the blkio subsystem. |
| 876 | * |
| 877 | * @ps: @blkcg_policy_storage that this structure inherits |
| 878 | * @weight: weight of the bfq_group |
| 879 | */ |
| 880 | struct bfq_group_data { |
| 881 | /* must be the first member */ |
| 882 | struct blkcg_policy_data pd; |
| 883 | |
| 884 | unsigned int weight; |
| 885 | }; |
| 886 | |
| 887 | /** |
| 888 | * struct bfq_group - per (device, cgroup) data structure. |
| 889 | * @entity: schedulable entity to insert into the parent group sched_data. |
| 890 | * @sched_data: own sched_data, to contain child entities (they may be |
| 891 | * both bfq_queues and bfq_groups). |
| 892 | * @bfqd: the bfq_data for the device this group acts upon. |
| 893 | * @async_bfqq: array of async queues for all the tasks belonging to |
| 894 | * the group, one queue per ioprio value per ioprio_class, |
| 895 | * except for the idle class that has only one queue. |
| 896 | * @async_idle_bfqq: async queue for the idle class (ioprio is ignored). |
| 897 | * @my_entity: pointer to @entity, %NULL for the toplevel group; used |
| 898 | * to avoid too many special cases during group creation/ |
| 899 | * migration. |
| 900 | * @stats: stats for this bfqg. |
| 901 | * @active_entities: number of active entities belonging to the group; |
| 902 | * unused for the root group. Used to know whether there |
| 903 | * are groups with more than one active @bfq_entity |
| 904 | * (see the comments to the function |
| 905 | * bfq_bfqq_may_idle()). |
| 906 | * @rq_pos_tree: rbtree sorted by next_request position, used when |
| 907 | * determining if two or more queues have interleaving |
| 908 | * requests (see bfq_find_close_cooperator()). |
| 909 | * |
| 910 | * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup |
| 911 | * there is a set of bfq_groups, each one collecting the lower-level |
| 912 | * entities belonging to the group that are acting on the same device. |
| 913 | * |
| 914 | * Locking works as follows: |
| 915 | * o @bfqd is protected by the queue lock, RCU is used to access it |
| 916 | * from the readers. |
| 917 | * o All the other fields are protected by the @bfqd queue lock. |
| 918 | */ |
| 919 | struct bfq_group { |
| 920 | /* must be the first member */ |
| 921 | struct blkg_policy_data pd; |
| 922 | |
Paolo Valente | 8f9bebc | 2017-06-05 10:11:15 +0200 | [diff] [blame] | 923 | /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */ |
| 924 | char blkg_path[128]; |
| 925 | |
| 926 | /* reference counter (see comments in bfq_bic_update_cgroup) */ |
| 927 | int ref; |
| 928 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 929 | struct bfq_entity entity; |
| 930 | struct bfq_sched_data sched_data; |
| 931 | |
| 932 | void *bfqd; |
| 933 | |
Damien Le Moal | 202bc94 | 2021-08-11 12:37:01 +0900 | [diff] [blame] | 934 | struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS]; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 935 | struct bfq_queue *async_idle_bfqq; |
| 936 | |
| 937 | struct bfq_entity *my_entity; |
| 938 | |
| 939 | int active_entities; |
| 940 | |
| 941 | struct rb_root rq_pos_tree; |
| 942 | |
| 943 | struct bfqg_stats stats; |
| 944 | }; |
| 945 | |
| 946 | #else |
| 947 | struct bfq_group { |
Paolo Valente | 4d8340d | 2020-02-03 11:40:58 +0100 | [diff] [blame] | 948 | struct bfq_entity entity; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 949 | struct bfq_sched_data sched_data; |
| 950 | |
Damien Le Moal | 202bc94 | 2021-08-11 12:37:01 +0900 | [diff] [blame] | 951 | struct bfq_queue *async_bfqq[2][IOPRIO_NR_LEVELS]; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 952 | struct bfq_queue *async_idle_bfqq; |
| 953 | |
| 954 | struct rb_root rq_pos_tree; |
| 955 | }; |
| 956 | #endif |
| 957 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 958 | /* --------------- main algorithm interface ----------------- */ |
| 959 | |
| 960 | #define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \ |
| 961 | { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 }) |
| 962 | |
| 963 | extern const int bfq_timeout; |
| 964 | |
| 965 | struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync); |
| 966 | void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync); |
| 967 | struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 968 | void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq); |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 969 | void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
Paolo Valente | fb53ac6 | 2019-03-12 09:59:28 +0100 | [diff] [blame] | 970 | struct rb_root_cached *root); |
Paolo Valente | 0471559 | 2018-06-25 21:55:34 +0200 | [diff] [blame] | 971 | void __bfq_weights_tree_remove(struct bfq_data *bfqd, |
Federico Motta | 2d29c9f | 2018-10-12 11:55:57 +0200 | [diff] [blame] | 972 | struct bfq_queue *bfqq, |
Paolo Valente | fb53ac6 | 2019-03-12 09:59:28 +0100 | [diff] [blame] | 973 | struct rb_root_cached *root); |
Paolo Valente | 0471559 | 2018-06-25 21:55:34 +0200 | [diff] [blame] | 974 | void bfq_weights_tree_remove(struct bfq_data *bfqd, |
| 975 | struct bfq_queue *bfqq); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 976 | void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 977 | bool compensate, enum bfqq_expiration reason); |
| 978 | void bfq_put_queue(struct bfq_queue *bfqq); |
| 979 | void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg); |
Paolo Valente | c899773 | 2020-03-21 10:45:19 +0100 | [diff] [blame] | 980 | void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 981 | void bfq_schedule_dispatch(struct bfq_data *bfqd); |
| 982 | void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg); |
| 983 | |
| 984 | /* ------------ end of main algorithm interface -------------- */ |
| 985 | |
| 986 | /* ---------------- cgroups-support interface ---------------- */ |
| 987 | |
Tejun Heo | fd41e60 | 2019-11-07 11:18:00 -0800 | [diff] [blame] | 988 | void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 989 | void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq, |
| 990 | unsigned int op); |
| 991 | void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op); |
| 992 | void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op); |
Omar Sandoval | 84c7afc | 2018-05-09 02:08:51 -0700 | [diff] [blame] | 993 | void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns, |
| 994 | u64 io_start_time_ns, unsigned int op); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 995 | void bfqg_stats_update_dequeue(struct bfq_group *bfqg); |
| 996 | void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg); |
| 997 | void bfqg_stats_update_idle_time(struct bfq_group *bfqg); |
| 998 | void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg); |
| 999 | void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg); |
| 1000 | void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 1001 | struct bfq_group *bfqg); |
| 1002 | |
| 1003 | void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg); |
| 1004 | void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio); |
| 1005 | void bfq_end_wr_async(struct bfq_data *bfqd); |
| 1006 | struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, |
| 1007 | struct blkcg *blkcg); |
| 1008 | struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg); |
| 1009 | struct bfq_group *bfqq_group(struct bfq_queue *bfqq); |
| 1010 | struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node); |
Paolo Valente | 8f9bebc | 2017-06-05 10:11:15 +0200 | [diff] [blame] | 1011 | void bfqg_and_blkg_put(struct bfq_group *bfqg); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1012 | |
| 1013 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
Jens Axboe | 659b339 | 2017-04-20 09:37:05 -0600 | [diff] [blame] | 1014 | extern struct cftype bfq_blkcg_legacy_files[]; |
| 1015 | extern struct cftype bfq_blkg_files[]; |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1016 | extern struct blkcg_policy blkcg_policy_bfq; |
| 1017 | #endif |
| 1018 | |
| 1019 | /* ------------- end of cgroups-support interface ------------- */ |
| 1020 | |
| 1021 | /* - interface of the internal hierarchical B-WF2Q+ scheduler - */ |
| 1022 | |
| 1023 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
| 1024 | /* both next loops stop at one of the child entities of the root group */ |
| 1025 | #define for_each_entity(entity) \ |
| 1026 | for (; entity ; entity = entity->parent) |
| 1027 | |
| 1028 | /* |
| 1029 | * For each iteration, compute parent in advance, so as to be safe if |
| 1030 | * entity is deallocated during the iteration. Such a deallocation may |
| 1031 | * happen as a consequence of a bfq_put_queue that frees the bfq_queue |
| 1032 | * containing entity. |
| 1033 | */ |
| 1034 | #define for_each_entity_safe(entity, parent) \ |
| 1035 | for (; entity && ({ parent = entity->parent; 1; }); entity = parent) |
| 1036 | |
| 1037 | #else /* CONFIG_BFQ_GROUP_IOSCHED */ |
| 1038 | /* |
| 1039 | * Next two macros are fake loops when cgroups support is not |
| 1040 | * enabled. I fact, in such a case, there is only one level to go up |
| 1041 | * (to reach the root group). |
| 1042 | */ |
| 1043 | #define for_each_entity(entity) \ |
| 1044 | for (; entity ; entity = NULL) |
| 1045 | |
| 1046 | #define for_each_entity_safe(entity, parent) \ |
| 1047 | for (parent = NULL; entity ; entity = parent) |
| 1048 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ |
| 1049 | |
| 1050 | struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq); |
| 1051 | struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity); |
Paolo Valente | 73d5811 | 2019-01-29 12:06:29 +0100 | [diff] [blame] | 1052 | unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1053 | struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity); |
| 1054 | struct bfq_entity *bfq_entity_of(struct rb_node *node); |
| 1055 | unsigned short bfq_ioprio_to_weight(int ioprio); |
| 1056 | void bfq_put_idle_entity(struct bfq_service_tree *st, |
| 1057 | struct bfq_entity *entity); |
| 1058 | struct bfq_service_tree * |
| 1059 | __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st, |
Paolo Valente | 431b17f | 2017-07-03 10:00:10 +0200 | [diff] [blame] | 1060 | struct bfq_entity *entity, |
| 1061 | bool update_class_too); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1062 | void bfq_bfqq_served(struct bfq_queue *bfqq, int served); |
| 1063 | void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 1064 | unsigned long time_ms); |
| 1065 | bool __bfq_deactivate_entity(struct bfq_entity *entity, |
| 1066 | bool ins_into_idle_tree); |
| 1067 | bool next_queue_may_preempt(struct bfq_data *bfqd); |
| 1068 | struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd); |
Paolo Valente | eed47d1 | 2019-04-10 10:38:33 +0200 | [diff] [blame] | 1069 | bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1070 | void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 1071 | bool ins_into_idle_tree, bool expiration); |
| 1072 | void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq); |
Paolo Valente | 80294c3 | 2017-08-31 08:46:29 +0200 | [diff] [blame] | 1073 | void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 1074 | bool expiration); |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1075 | void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
| 1076 | bool expiration); |
| 1077 | void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq); |
| 1078 | |
| 1079 | /* --------------- end of interface of B-WF2Q+ ---------------- */ |
| 1080 | |
| 1081 | /* Logging facilities. */ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1082 | static inline void bfq_pid_to_str(int pid, char *str, int len) |
| 1083 | { |
| 1084 | if (pid != -1) |
| 1085 | snprintf(str, len, "%d", pid); |
| 1086 | else |
| 1087 | snprintf(str, len, "SHARED-"); |
| 1088 | } |
| 1089 | |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1090 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
| 1091 | struct bfq_group *bfqq_group(struct bfq_queue *bfqq); |
| 1092 | |
| 1093 | #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1094 | char pid_str[MAX_PID_STR_LENGTH]; \ |
Dmitry Monakhov | 40d47c1 | 2019-11-01 13:11:10 +0000 | [diff] [blame] | 1095 | if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \ |
| 1096 | break; \ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1097 | bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH); \ |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 1098 | blk_add_cgroup_trace_msg((bfqd)->queue, \ |
| 1099 | bfqg_to_blkg(bfqq_group(bfqq))->blkcg, \ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1100 | "bfq%s%c " fmt, pid_str, \ |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 1101 | bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args); \ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1102 | } while (0) |
| 1103 | |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 1104 | #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do { \ |
| 1105 | blk_add_cgroup_trace_msg((bfqd)->queue, \ |
| 1106 | bfqg_to_blkg(bfqg)->blkcg, fmt, ##args); \ |
| 1107 | } while (0) |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1108 | |
| 1109 | #else /* CONFIG_BFQ_GROUP_IOSCHED */ |
| 1110 | |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1111 | #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \ |
| 1112 | char pid_str[MAX_PID_STR_LENGTH]; \ |
Dmitry Monakhov | 40d47c1 | 2019-11-01 13:11:10 +0000 | [diff] [blame] | 1113 | if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \ |
| 1114 | break; \ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1115 | bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH); \ |
| 1116 | blk_add_trace_msg((bfqd)->queue, "bfq%s%c " fmt, pid_str, \ |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1117 | bfq_bfqq_sync((bfqq)) ? 'S' : 'A', \ |
Francesco Pollicino | 1e66413 | 2019-03-12 09:59:33 +0100 | [diff] [blame] | 1118 | ##args); \ |
| 1119 | } while (0) |
Paolo Valente | ea25da4 | 2017-04-19 08:48:24 -0600 | [diff] [blame] | 1120 | #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do {} while (0) |
| 1121 | |
| 1122 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ |
| 1123 | |
| 1124 | #define bfq_log(bfqd, fmt, args...) \ |
| 1125 | blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args) |
| 1126 | |
| 1127 | #endif /* _BFQ_H */ |