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