1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Header file for the BFQ I/O scheduler: data structures and
4 * prototypes of interface functions among BFQ components.
9 #include <linux/blktrace_api.h>
10 #include <linux/hrtimer.h>
11 #include <linux/blk-cgroup.h>
13 #include "blk-cgroup-rwstat.h"
15 #define BFQ_IOPRIO_CLASSES 3
16 #define BFQ_CL_IDLE_TIMEOUT (HZ/5)
18 #define BFQ_MIN_WEIGHT 1
19 #define BFQ_MAX_WEIGHT 1000
20 #define BFQ_WEIGHT_CONVERSION_COEFF 10
22 #define BFQ_DEFAULT_QUEUE_IOPRIO 4
24 #define BFQ_WEIGHT_LEGACY_DFL 100
25 #define BFQ_DEFAULT_GRP_IOPRIO 0
26 #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
28 #define MAX_PID_STR_LENGTH 12
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.
35 #define BFQ_SOFTRT_WEIGHT_FACTOR 100
40 * struct bfq_service_tree - per ioprio_class service tree.
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.
47 struct bfq_service_tree {
48 /* tree for active entities (i.e., those backlogged) */
49 struct rb_root active;
50 /* tree for idle entities (i.e., not backlogged, with V < F_i)*/
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;
58 /* scheduler virtual time */
60 /* scheduler weight sum; active and idle entities contribute to it */
65 * struct bfq_sched_data - multi-class scheduler.
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
69 * intermediate queue in a hierarchical setup.
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+.
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
87 * service among the other active entities in the same parent
88 * entity. We can then quickly compare the timestamps of the
89 * in-service entity with those of such best candidate.
91 * All fields are protected by the lock of the containing bfqd.
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;
106 * struct bfq_weight_counter - counter of the number of all active queues
107 * with a given weight.
109 struct bfq_weight_counter {
110 unsigned int weight; /* weight of the queues this counter refers to */
111 unsigned int num_active; /* nr of active queues with this weight */
113 * Weights tree member (see bfq_data's @queue_weights_tree)
115 struct rb_node weights_node;
119 * struct bfq_entity - schedulable entity.
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
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.
136 * Unless cgroups are used, the weight value is calculated from the
137 * ioprio to export the same interface as CFQ. When dealing with
138 * "well-behaved" queues (i.e., queues that do not spend too much
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
146 /* service_tree member */
147 struct rb_node rb_node;
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.
153 bool on_st_or_in_serv;
155 /* B-WF2Q+ start and finish timestamps [sectors/weight] */
158 /* tree the entity is enqueued into; %NULL if not on a tree */
159 struct rb_root *tree;
162 * minimum start time of the (active) subtree rooted at this
163 * entity; used for O(log N) lookups into active trees
167 /* amount of service received during the last service slot */
170 /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
173 /* device weight, if non-zero, it overrides the default weight of
176 /* weight of the queue */
178 /* next weight if a change is in progress */
181 /* original weight, used to implement weight boosting */
184 /* parent entity, for hierarchical scheduling */
185 struct bfq_entity *parent;
188 * For non-leaf nodes in the hierarchy, the associated
189 * scheduler queue, %NULL on leaf nodes.
191 struct bfq_sched_data *my_sched_data;
192 /* the scheduler queue this entity belongs to */
193 struct bfq_sched_data *sched_data;
195 /* flag, set to request a weight, ioprio or ioprio_class change */
198 /* flag, set if the entity is counted in groups_with_pending_reqs */
199 bool in_groups_with_pending_reqs;
205 * struct bfq_ttime - per process thinktime stats.
208 /* completion time of the last request */
209 u64 last_end_request;
211 /* total process thinktime */
213 /* number of thinktime samples */
214 unsigned long ttime_samples;
215 /* average process thinktime */
220 * struct bfq_queue - leaf schedulable entity.
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
228 * All the fields are protected by the queue lock of the containing bfqd.
231 /* reference counter */
233 /* parent bfq_data */
234 struct bfq_data *bfqd;
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;
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;
249 * Shared bfq_queue if queue is cooperating with one or more
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;
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 */
264 /* number of requests currently allocated */
266 /* number of pending metadata requests */
268 /* fifo list of requests in sort_list */
269 struct list_head fifo;
271 /* entity representing this queue in the scheduler */
272 struct bfq_entity entity;
274 /* pointer to the weight counter associated with this entity */
275 struct bfq_weight_counter *weight_counter;
277 /* maximum budget allowed from the feedback mechanism */
279 /* budget expiration (in jiffies) */
280 unsigned long budget_timeout;
282 /* number of requests on the dispatch list or inside driver */
288 /* node for active/idle bfqq list inside parent bfqd */
289 struct list_head bfqq_list;
291 /* associated @bfq_ttime struct */
292 struct bfq_ttime ttime;
294 /* when bfqq started to do I/O within the last observation window */
296 /* how long bfqq has remained empty during the last observ. window */
299 /* bit vector: a 1 for each seeky requests in history */
302 /* node for the device's burst list */
303 struct hlist_node burst_list_node;
305 /* position of the last request enqueued */
306 sector_t last_request_pos;
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
314 unsigned int requests_within_timer;
316 /* pid of the process owning the queue, used for logging purposes */
320 * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
321 * if the queue is shared.
323 struct bfq_io_cq *bic;
325 /* current maximum weight-raising time for this queue */
326 unsigned long wr_cur_max_time;
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())
334 unsigned long soft_rt_next_start;
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.
340 unsigned long last_wr_start_finish;
341 /* factor by which the weight of this queue is multiplied */
342 unsigned int wr_coeff;
344 * Time of the last transition of the @bfq_queue from idle to
347 unsigned long last_idle_bklogged;
349 * Cumulative service received from the @bfq_queue since the
350 * last transition from idle to backlogged.
352 unsigned long service_from_backlogged;
354 * Cumulative service received from the @bfq_queue since its
355 * last transition to weight-raised state.
357 unsigned long service_from_wr;
360 * Value of wr start time when switching to soft rt
362 unsigned long wr_start_at_switch_to_srt;
364 unsigned long split_time; /* time of last split */
366 unsigned long first_IO_time; /* time of first I/O for this queue */
368 /* max service rate measured so far */
369 u32 max_service_rate;
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().
378 struct bfq_queue *waker_bfqq;
379 /* pointer to the curr. tentative waker queue, see bfq_check_waker() */
380 struct bfq_queue *tentative_waker_bfqq;
381 /* number of times the same tentative waker has been detected */
382 unsigned int num_waker_detections;
384 /* node for woken_list, see below */
385 struct hlist_node woken_list_node;
387 * Head of the list of the woken queues for this queue, i.e.,
388 * of the list of the queues for which this queue is a waker
389 * queue. This list is used to reset the waker_bfqq pointer in
390 * the woken queues when this queue exits.
392 struct hlist_head woken_list;
396 * struct bfq_io_cq - per (request_queue, io_context) structure.
399 /* associated io_cq structure */
400 struct io_cq icq; /* must be the first member */
401 /* array of two process queues, the sync and the async */
402 struct bfq_queue *bfqq[2];
403 /* per (request_queue, blkcg) ioprio */
405 #ifdef CONFIG_BFQ_GROUP_IOSCHED
406 uint64_t blkcg_serial_nr; /* the current blkcg serial */
409 * Snapshot of the has_short_time flag before merging; taken
410 * to remember its value while the queue is merged, so as to
411 * be able to restore it in case of split.
413 bool saved_has_short_ttime;
415 * Same purpose as the previous two fields for the I/O bound
416 * classification of a queue.
420 u64 saved_io_start_time;
421 u64 saved_tot_idle_time;
424 * Same purpose as the previous fields for the value of the
425 * field keeping the queue's belonging to a large burst
427 bool saved_in_large_burst;
429 * True if the queue belonged to a burst list before its merge
430 * with another cooperating queue.
432 bool was_in_burst_list;
435 * Save the weight when a merge occurs, to be able
436 * to restore it in case of split. If the weight is not
437 * correctly resumed when the queue is recycled,
438 * then the weight of the recycled queue could differ
439 * from the weight of the original queue.
441 unsigned int saved_weight;
444 * Similar to previous fields: save wr information.
446 unsigned long saved_wr_coeff;
447 unsigned long saved_last_wr_start_finish;
448 unsigned long saved_service_from_wr;
449 unsigned long saved_wr_start_at_switch_to_srt;
450 unsigned int saved_wr_cur_max_time;
451 struct bfq_ttime saved_ttime;
453 /* Save also injection state */
454 u64 saved_last_serv_time_ns;
455 unsigned int saved_inject_limit;
456 unsigned long saved_decrease_time_jif;
460 * struct bfq_data - per-device data structure.
462 * All the fields are protected by @lock.
465 /* device request queue */
466 struct request_queue *queue;
468 struct list_head dispatch;
470 /* root bfq_group for the device */
471 struct bfq_group *root_group;
474 * rbtree of weight counters of @bfq_queues, sorted by
475 * weight. Used to keep track of whether all @bfq_queues have
476 * the same weight. The tree contains one counter for each
477 * distinct weight associated to some active and not
478 * weight-raised @bfq_queue (see the comments to the functions
479 * bfq_weights_tree_[add|remove] for further details).
481 struct rb_root_cached queue_weights_tree;
484 * Number of groups with at least one descendant process that
485 * has at least one request waiting for completion. Note that
486 * this accounts for also requests already dispatched, but not
487 * yet completed. Therefore this number of groups may differ
488 * (be larger) than the number of active groups, as a group is
489 * considered active only if its corresponding entity has
490 * descendant queues with at least one request queued. This
491 * number is used to decide whether a scenario is symmetric.
492 * For a detailed explanation see comments on the computation
493 * of the variable asymmetric_scenario in the function
494 * bfq_better_to_idle().
496 * However, it is hard to compute this number exactly, for
497 * groups with multiple descendant processes. Consider a group
498 * that is inactive, i.e., that has no descendant process with
499 * pending I/O inside BFQ queues. Then suppose that
500 * num_groups_with_pending_reqs is still accounting for this
501 * group, because the group has descendant processes with some
502 * I/O request still in flight. num_groups_with_pending_reqs
503 * should be decremented when the in-flight request of the
504 * last descendant process is finally completed (assuming that
505 * nothing else has changed for the group in the meantime, in
506 * terms of composition of the group and active/inactive state of child
507 * groups and processes). To accomplish this, an additional
508 * pending-request counter must be added to entities, and must
509 * be updated correctly. To avoid this additional field and operations,
510 * we resort to the following tradeoff between simplicity and
511 * accuracy: for an inactive group that is still counted in
512 * num_groups_with_pending_reqs, we decrement
513 * num_groups_with_pending_reqs when the first descendant
514 * process of the group remains with no request waiting for
517 * Even this simpler decrement strategy requires a little
518 * carefulness: to avoid multiple decrements, we flag a group,
519 * more precisely an entity representing a group, as still
520 * counted in num_groups_with_pending_reqs when it becomes
521 * inactive. Then, when the first descendant queue of the
522 * entity remains with no request waiting for completion,
523 * num_groups_with_pending_reqs is decremented, and this flag
524 * is reset. After this flag is reset for the entity,
525 * num_groups_with_pending_reqs won't be decremented any
526 * longer in case a new descendant queue of the entity remains
527 * with no request waiting for completion.
529 unsigned int num_groups_with_pending_reqs;
532 * Per-class (RT, BE, IDLE) number of bfq_queues containing
533 * requests (including the queue in service, even if it is
536 unsigned int busy_queues[3];
537 /* number of weight-raised busy @bfq_queues */
539 /* number of queued requests */
541 /* number of requests dispatched and waiting for completion */
544 /* true if the device is non rotational and performs queueing */
545 bool nonrot_with_queueing;
548 * Maximum number of requests in driver in the last
549 * @hw_tag_samples completed requests.
551 int max_rq_in_driver;
552 /* number of samples used to calculate hw_tag */
554 /* flag set to one if the driver is showing a queueing behavior */
557 /* number of budgets assigned */
558 int budgets_assigned;
561 * Timer set when idling (waiting) for the next request from
562 * the queue in service.
564 struct hrtimer idle_slice_timer;
566 /* bfq_queue in service */
567 struct bfq_queue *in_service_queue;
569 /* on-disk position of the last served request */
570 sector_t last_position;
572 /* position of the last served request for the in-service queue */
573 sector_t in_serv_last_pos;
575 /* time of last request completion (ns) */
578 /* bfqq owning the last completed rq */
579 struct bfq_queue *last_completed_rq_bfqq;
581 /* time of last transition from empty to non-empty (ns) */
582 u64 last_empty_occupied_ns;
585 * Flag set to activate the sampling of the total service time
586 * of a just-arrived first I/O request (see
587 * bfq_update_inject_limit()). This will cause the setting of
588 * waited_rq when the request is finally dispatched.
592 * If set, then bfq_update_inject_limit() is invoked when
593 * waited_rq is eventually completed.
595 struct request *waited_rq;
597 * True if some request has been injected during the last service hole.
601 /* time of first rq dispatch in current observation interval (ns) */
603 /* time of last rq dispatch in current observation interval (ns) */
606 /* beginning of the last budget */
607 ktime_t last_budget_start;
608 /* beginning of the last idle slice */
609 ktime_t last_idling_start;
610 unsigned long last_idling_start_jiffies;
612 /* number of samples in current observation interval */
613 int peak_rate_samples;
614 /* num of samples of seq dispatches in current observation interval */
615 u32 sequential_samples;
616 /* total num of sectors transferred in current observation interval */
617 u64 tot_sectors_dispatched;
618 /* max rq size seen during current observation interval (sectors) */
619 u32 last_rq_max_size;
620 /* time elapsed from first dispatch in current observ. interval (us) */
621 u64 delta_from_first;
623 * Current estimate of the device peak rate, measured in
624 * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
625 * BFQ_RATE_SHIFT is performed to increase precision in
626 * fixed-point calculations.
630 /* maximum budget allotted to a bfq_queue before rescheduling */
633 /* list of all the bfq_queues active on the device */
634 struct list_head active_list;
635 /* list of all the bfq_queues idle on the device */
636 struct list_head idle_list;
639 * Timeout for async/sync requests; when it fires, requests
640 * are served in fifo order.
642 u64 bfq_fifo_expire[2];
643 /* weight of backward seeks wrt forward ones */
644 unsigned int bfq_back_penalty;
645 /* maximum allowed backward seek */
646 unsigned int bfq_back_max;
647 /* maximum idling time */
650 /* user-configured max budget value (0 for auto-tuning) */
651 int bfq_user_max_budget;
653 * Timeout for bfq_queues to consume their budget; used to
654 * prevent seeky queues from imposing long latencies to
655 * sequential or quasi-sequential ones (this also implies that
656 * seeky queues cannot receive guarantees in the service
657 * domain; after a timeout they are charged for the time they
658 * have been in service, to preserve fairness among them, but
659 * without service-domain guarantees).
661 unsigned int bfq_timeout;
664 * Force device idling whenever needed to provide accurate
665 * service guarantees, without caring about throughput
666 * issues. CAVEAT: this may even increase latencies, in case
667 * of useless idling for processes that did stop doing I/O.
669 bool strict_guarantees;
672 * Last time at which a queue entered the current burst of
673 * queues being activated shortly after each other; for more
674 * details about this and the following parameters related to
675 * a burst of activations, see the comments on the function
678 unsigned long last_ins_in_burst;
680 * Reference time interval used to decide whether a queue has
681 * been activated shortly after @last_ins_in_burst.
683 unsigned long bfq_burst_interval;
684 /* number of queues in the current burst of queue activations */
687 /* common parent entity for the queues in the burst */
688 struct bfq_entity *burst_parent_entity;
689 /* Maximum burst size above which the current queue-activation
690 * burst is deemed as 'large'.
692 unsigned long bfq_large_burst_thresh;
693 /* true if a large queue-activation burst is in progress */
696 * Head of the burst list (as for the above fields, more
697 * details in the comments on the function bfq_handle_burst).
699 struct hlist_head burst_list;
701 /* if set to true, low-latency heuristics are enabled */
704 * Maximum factor by which the weight of a weight-raised queue
707 unsigned int bfq_wr_coeff;
708 /* maximum duration of a weight-raising period (jiffies) */
709 unsigned int bfq_wr_max_time;
711 /* Maximum weight-raising duration for soft real-time processes */
712 unsigned int bfq_wr_rt_max_time;
714 * Minimum idle period after which weight-raising may be
715 * reactivated for a queue (in jiffies).
717 unsigned int bfq_wr_min_idle_time;
719 * Minimum period between request arrivals after which
720 * weight-raising may be reactivated for an already busy async
721 * queue (in jiffies).
723 unsigned long bfq_wr_min_inter_arr_async;
725 /* Max service-rate for a soft real-time queue, in sectors/sec */
726 unsigned int bfq_wr_max_softrt_rate;
728 * Cached value of the product ref_rate*ref_wr_duration, used
729 * for computing the maximum duration of weight raising
734 /* fallback dummy bfqq for extreme OOM conditions */
735 struct bfq_queue oom_bfqq;
740 * bic associated with the task issuing current bio for
741 * merging. This and the next field are used as a support to
742 * be able to perform the bic lookup, needed by bio-merge
743 * functions, before the scheduler lock is taken, and thus
744 * avoid taking the request-queue lock while the scheduler
745 * lock is being held.
747 struct bfq_io_cq *bio_bic;
748 /* bfqq associated with the task issuing current bio for merging */
749 struct bfq_queue *bio_bfqq;
752 * Depth limits used in bfq_limit_depth (see comments on the
755 unsigned int word_depths[2][2];
758 enum bfqq_state_flags {
759 BFQQF_just_created = 0, /* queue just allocated */
760 BFQQF_busy, /* has requests or is in service */
761 BFQQF_wait_request, /* waiting for a request */
762 BFQQF_non_blocking_wait_rq, /*
763 * waiting for a request
764 * without idling the device
766 BFQQF_fifo_expire, /* FIFO checked in this slice */
767 BFQQF_has_short_ttime, /* queue has a short think time */
768 BFQQF_sync, /* synchronous queue */
770 * bfqq has timed-out at least once
771 * having consumed at most 2/10 of
774 BFQQF_in_large_burst, /*
775 * bfqq activated in a large burst,
776 * see comments to bfq_handle_burst.
778 BFQQF_softrt_update, /*
779 * may need softrt-next-start
782 BFQQF_coop, /* bfqq is shared */
783 BFQQF_split_coop, /* shared bfqq will be split */
786 #define BFQ_BFQQ_FNS(name) \
787 void bfq_mark_bfqq_##name(struct bfq_queue *bfqq); \
788 void bfq_clear_bfqq_##name(struct bfq_queue *bfqq); \
789 int bfq_bfqq_##name(const struct bfq_queue *bfqq);
791 BFQ_BFQQ_FNS(just_created);
793 BFQ_BFQQ_FNS(wait_request);
794 BFQ_BFQQ_FNS(non_blocking_wait_rq);
795 BFQ_BFQQ_FNS(fifo_expire);
796 BFQ_BFQQ_FNS(has_short_ttime);
798 BFQ_BFQQ_FNS(IO_bound);
799 BFQ_BFQQ_FNS(in_large_burst);
801 BFQ_BFQQ_FNS(split_coop);
802 BFQ_BFQQ_FNS(softrt_update);
805 /* Expiration reasons. */
806 enum bfqq_expiration {
807 BFQQE_TOO_IDLE = 0, /*
808 * queue has been idling for
811 BFQQE_BUDGET_TIMEOUT, /* budget took too long to be used */
812 BFQQE_BUDGET_EXHAUSTED, /* budget consumed */
813 BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */
814 BFQQE_PREEMPTED /* preemption in progress */
818 struct percpu_counter cpu_cnt;
824 struct blkg_rwstat bytes;
825 struct blkg_rwstat ios;
826 #ifdef CONFIG_BFQ_CGROUP_DEBUG
827 /* number of ios merged */
828 struct blkg_rwstat merged;
829 /* total time spent on device in ns, may not be accurate w/ queueing */
830 struct blkg_rwstat service_time;
831 /* total time spent waiting in scheduler queue in ns */
832 struct blkg_rwstat wait_time;
833 /* number of IOs queued up */
834 struct blkg_rwstat queued;
835 /* total disk time and nr sectors dispatched by this group */
836 struct bfq_stat time;
837 /* sum of number of ios queued across all samples */
838 struct bfq_stat avg_queue_size_sum;
839 /* count of samples taken for average */
840 struct bfq_stat avg_queue_size_samples;
841 /* how many times this group has been removed from service tree */
842 struct bfq_stat dequeue;
843 /* total time spent waiting for it to be assigned a timeslice. */
844 struct bfq_stat group_wait_time;
845 /* time spent idling for this blkcg_gq */
846 struct bfq_stat idle_time;
847 /* total time with empty current active q with other requests queued */
848 struct bfq_stat empty_time;
849 /* fields after this shouldn't be cleared on stat reset */
850 u64 start_group_wait_time;
852 u64 start_empty_time;
854 #endif /* CONFIG_BFQ_CGROUP_DEBUG */
857 #ifdef CONFIG_BFQ_GROUP_IOSCHED
860 * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
862 * @ps: @blkcg_policy_storage that this structure inherits
863 * @weight: weight of the bfq_group
865 struct bfq_group_data {
866 /* must be the first member */
867 struct blkcg_policy_data pd;
873 * struct bfq_group - per (device, cgroup) data structure.
874 * @entity: schedulable entity to insert into the parent group sched_data.
875 * @sched_data: own sched_data, to contain child entities (they may be
876 * both bfq_queues and bfq_groups).
877 * @bfqd: the bfq_data for the device this group acts upon.
878 * @async_bfqq: array of async queues for all the tasks belonging to
879 * the group, one queue per ioprio value per ioprio_class,
880 * except for the idle class that has only one queue.
881 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
882 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
883 * to avoid too many special cases during group creation/
885 * @stats: stats for this bfqg.
886 * @active_entities: number of active entities belonging to the group;
887 * unused for the root group. Used to know whether there
888 * are groups with more than one active @bfq_entity
889 * (see the comments to the function
890 * bfq_bfqq_may_idle()).
891 * @rq_pos_tree: rbtree sorted by next_request position, used when
892 * determining if two or more queues have interleaving
893 * requests (see bfq_find_close_cooperator()).
895 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
896 * there is a set of bfq_groups, each one collecting the lower-level
897 * entities belonging to the group that are acting on the same device.
899 * Locking works as follows:
900 * o @bfqd is protected by the queue lock, RCU is used to access it
902 * o All the other fields are protected by the @bfqd queue lock.
905 /* must be the first member */
906 struct blkg_policy_data pd;
908 /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
911 /* reference counter (see comments in bfq_bic_update_cgroup) */
914 struct bfq_entity entity;
915 struct bfq_sched_data sched_data;
919 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
920 struct bfq_queue *async_idle_bfqq;
922 struct bfq_entity *my_entity;
926 struct rb_root rq_pos_tree;
928 struct bfqg_stats stats;
933 struct bfq_entity entity;
934 struct bfq_sched_data sched_data;
936 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
937 struct bfq_queue *async_idle_bfqq;
939 struct rb_root rq_pos_tree;
943 struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
945 /* --------------- main algorithm interface ----------------- */
947 #define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \
948 { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
950 extern const int bfq_timeout;
952 struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
953 void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
954 struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
955 void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
956 void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
957 struct rb_root_cached *root);
958 void __bfq_weights_tree_remove(struct bfq_data *bfqd,
959 struct bfq_queue *bfqq,
960 struct rb_root_cached *root);
961 void bfq_weights_tree_remove(struct bfq_data *bfqd,
962 struct bfq_queue *bfqq);
963 void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
964 bool compensate, enum bfqq_expiration reason);
965 void bfq_put_queue(struct bfq_queue *bfqq);
966 void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
967 void bfq_release_process_ref(struct bfq_data *bfqd, struct bfq_queue *bfqq);
968 void bfq_schedule_dispatch(struct bfq_data *bfqd);
969 void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
971 /* ------------ end of main algorithm interface -------------- */
973 /* ---------------- cgroups-support interface ---------------- */
975 void bfqg_stats_update_legacy_io(struct request_queue *q, struct request *rq);
976 void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
978 void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op);
979 void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op);
980 void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
981 u64 io_start_time_ns, unsigned int op);
982 void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
983 void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
984 void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
985 void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
986 void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
987 void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
988 struct bfq_group *bfqg);
990 void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
991 void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
992 void bfq_end_wr_async(struct bfq_data *bfqd);
993 struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
994 struct blkcg *blkcg);
995 struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
996 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
997 struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
998 void bfqg_and_blkg_put(struct bfq_group *bfqg);
1000 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1001 extern struct cftype bfq_blkcg_legacy_files[];
1002 extern struct cftype bfq_blkg_files[];
1003 extern struct blkcg_policy blkcg_policy_bfq;
1006 /* ------------- end of cgroups-support interface ------------- */
1008 /* - interface of the internal hierarchical B-WF2Q+ scheduler - */
1010 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1011 /* both next loops stop at one of the child entities of the root group */
1012 #define for_each_entity(entity) \
1013 for (; entity ; entity = entity->parent)
1016 * For each iteration, compute parent in advance, so as to be safe if
1017 * entity is deallocated during the iteration. Such a deallocation may
1018 * happen as a consequence of a bfq_put_queue that frees the bfq_queue
1019 * containing entity.
1021 #define for_each_entity_safe(entity, parent) \
1022 for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
1024 #else /* CONFIG_BFQ_GROUP_IOSCHED */
1026 * Next two macros are fake loops when cgroups support is not
1027 * enabled. I fact, in such a case, there is only one level to go up
1028 * (to reach the root group).
1030 #define for_each_entity(entity) \
1031 for (; entity ; entity = NULL)
1033 #define for_each_entity_safe(entity, parent) \
1034 for (parent = NULL; entity ; entity = parent)
1035 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1037 struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq);
1038 struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
1039 unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
1040 struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
1041 struct bfq_entity *bfq_entity_of(struct rb_node *node);
1042 unsigned short bfq_ioprio_to_weight(int ioprio);
1043 void bfq_put_idle_entity(struct bfq_service_tree *st,
1044 struct bfq_entity *entity);
1045 struct bfq_service_tree *
1046 __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
1047 struct bfq_entity *entity,
1048 bool update_class_too);
1049 void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
1050 void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1051 unsigned long time_ms);
1052 bool __bfq_deactivate_entity(struct bfq_entity *entity,
1053 bool ins_into_idle_tree);
1054 bool next_queue_may_preempt(struct bfq_data *bfqd);
1055 struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
1056 bool __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
1057 void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1058 bool ins_into_idle_tree, bool expiration);
1059 void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1060 void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1062 void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1064 void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1066 /* --------------- end of interface of B-WF2Q+ ---------------- */
1068 /* Logging facilities. */
1069 static inline void bfq_pid_to_str(int pid, char *str, int len)
1072 snprintf(str, len, "%d", pid);
1074 snprintf(str, len, "SHARED-");
1077 #ifdef CONFIG_BFQ_GROUP_IOSCHED
1078 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1080 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
1081 char pid_str[MAX_PID_STR_LENGTH]; \
1082 if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
1084 bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH); \
1085 blk_add_cgroup_trace_msg((bfqd)->queue, \
1086 bfqg_to_blkg(bfqq_group(bfqq))->blkcg, \
1087 "bfq%s%c " fmt, pid_str, \
1088 bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args); \
1091 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do { \
1092 blk_add_cgroup_trace_msg((bfqd)->queue, \
1093 bfqg_to_blkg(bfqg)->blkcg, fmt, ##args); \
1096 #else /* CONFIG_BFQ_GROUP_IOSCHED */
1098 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
1099 char pid_str[MAX_PID_STR_LENGTH]; \
1100 if (likely(!blk_trace_note_message_enabled((bfqd)->queue))) \
1102 bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH); \
1103 blk_add_trace_msg((bfqd)->queue, "bfq%s%c " fmt, pid_str, \
1104 bfq_bfqq_sync((bfqq)) ? 'S' : 'A', \
1107 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do {} while (0)
1109 #endif /* CONFIG_BFQ_GROUP_IOSCHED */
1111 #define bfq_log(bfqd, fmt, args...) \
1112 blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)