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