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