blob: 26869cfbbfa9584b10faa1a579795ddba739086e [file] [log] [blame]
Paolo Valenteea25da42017-04-19 08:48:24 -06001/*
2 * Header file for the BFQ I/O scheduler: data structures and
3 * prototypes of interface functions among BFQ components.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15#ifndef _BFQ_H
16#define _BFQ_H
17
18#include <linux/blktrace_api.h>
19#include <linux/hrtimer.h>
20#include <linux/blk-cgroup.h>
21
22#define BFQ_IOPRIO_CLASSES 3
23#define BFQ_CL_IDLE_TIMEOUT (HZ/5)
24
25#define BFQ_MIN_WEIGHT 1
26#define BFQ_MAX_WEIGHT 1000
27#define BFQ_WEIGHT_CONVERSION_COEFF 10
28
29#define BFQ_DEFAULT_QUEUE_IOPRIO 4
30
31#define BFQ_WEIGHT_LEGACY_DFL 100
32#define BFQ_DEFAULT_GRP_IOPRIO 0
33#define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
34
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;
Hou Tao38c91402017-07-12 15:25:01 +080055 /* tree for idle entities (i.e., not backlogged, with V < F_i)*/
Paolo Valenteea25da42017-04-19 08:48:24 -060056 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
Paolo Valente46d556e2017-07-29 12:42:56 +020074 * intermediate queue in a hierarchical setup.
Paolo Valenteea25da42017-04-19 08:48:24 -060075 *
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+.
Paolo Valente46d556e2017-07-29 12:42:56 +020081 *
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.
Paolo Valenteea25da42017-04-19 08:48:24 -060097 */
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/**
Federico Motta2d29c9f2018-10-12 11:55:57 +0200111 * struct bfq_weight_counter - counter of the number of all active queues
Paolo Valenteea25da42017-04-19 08:48:24 -0600112 * with a given weight.
113 */
114struct bfq_weight_counter {
Federico Motta2d29c9f2018-10-12 11:55:57 +0200115 unsigned int weight; /* weight of the queues this counter refers to */
116 unsigned int num_active; /* nr of active queues with this weight */
Paolo Valenteea25da42017-04-19 08:48:24 -0600117 /*
Federico Motta2d29c9f2018-10-12 11:55:57 +0200118 * Weights tree member (see bfq_data's @queue_weights_tree)
Paolo Valenteea25da42017-04-19 08:48:24 -0600119 */
120 struct rb_node weights_node;
121};
122
123/**
124 * struct bfq_entity - schedulable entity.
125 *
126 * A bfq_entity is used to represent either a bfq_queue (leaf node in the
127 * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each
128 * entity belongs to the sched_data of the parent group in the cgroup
129 * hierarchy. Non-leaf entities have also their own sched_data, stored
130 * in @my_sched_data.
131 *
132 * Each entity stores independently its priority values; this would
133 * allow different weights on different devices, but this
134 * functionality is not exported to userspace by now. Priorities and
135 * weights are updated lazily, first storing the new values into the
136 * new_* fields, then setting the @prio_changed flag. As soon as
137 * there is a transition in the entity state that allows the priority
138 * update to take place the effective and the requested priority
139 * values are synchronized.
140 *
141 * Unless cgroups are used, the weight value is calculated from the
142 * ioprio to export the same interface as CFQ. When dealing with
143 * ``well-behaved'' queues (i.e., queues that do not spend too much
144 * time to consume their budget and have true sequential behavior, and
145 * when there are no external factors breaking anticipation) the
146 * relative weights at each level of the cgroups hierarchy should be
147 * guaranteed. All the fields are protected by the queue lock of the
148 * containing bfqd.
149 */
150struct bfq_entity {
151 /* service_tree member */
152 struct rb_node rb_node;
Paolo Valenteea25da42017-04-19 08:48:24 -0600153
154 /*
155 * Flag, true if the entity is on a tree (either the active or
156 * the idle one of its service_tree) or is in service.
157 */
158 bool on_st;
159
160 /* B-WF2Q+ start and finish timestamps [sectors/weight] */
161 u64 start, finish;
162
163 /* tree the entity is enqueued into; %NULL if not on a tree */
164 struct rb_root *tree;
165
166 /*
167 * minimum start time of the (active) subtree rooted at this
168 * entity; used for O(log N) lookups into active trees
169 */
170 u64 min_start;
171
172 /* amount of service received during the last service slot */
173 int service;
174
175 /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */
176 int budget;
177
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;
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100199
200 /* flag, set if the entity is counted in groups_with_pending_reqs */
201 bool in_groups_with_pending_reqs;
Paolo Valenteea25da42017-04-19 08:48:24 -0600202};
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
Paolo Valente2341d6622019-03-12 09:59:29 +0100243 /* last total-service-time sample, see bfq_update_inject_limit() */
244 u64 last_serv_time_ns;
245 /* limit for request injection */
246 unsigned int inject_limit;
247 /* last time the inject limit has been decreased, in jiffies */
248 unsigned long decrease_time_jif;
249
Paolo Valenteea25da42017-04-19 08:48:24 -0600250 /*
251 * Shared bfq_queue if queue is cooperating with one or more
252 * other queues.
253 */
254 struct bfq_queue *new_bfqq;
255 /* request-position tree member (see bfq_group's @rq_pos_tree) */
256 struct rb_node pos_node;
257 /* request-position tree root (see bfq_group's @rq_pos_tree) */
258 struct rb_root *pos_root;
259
260 /* sorted list of pending requests */
261 struct rb_root sort_list;
262 /* if fifo isn't expired, next request to serve */
263 struct request *next_rq;
264 /* number of sync and async requests queued */
265 int queued[2];
266 /* number of requests currently allocated */
267 int allocated;
268 /* number of pending metadata requests */
269 int meta_pending;
270 /* fifo list of requests in sort_list */
271 struct list_head fifo;
272
273 /* entity representing this queue in the scheduler */
274 struct bfq_entity entity;
275
Federico Motta2d29c9f2018-10-12 11:55:57 +0200276 /* pointer to the weight counter associated with this entity */
277 struct bfq_weight_counter *weight_counter;
278
Paolo Valenteea25da42017-04-19 08:48:24 -0600279 /* maximum budget allowed from the feedback mechanism */
280 int max_budget;
281 /* budget expiration (in jiffies) */
282 unsigned long budget_timeout;
283
284 /* number of requests on the dispatch list or inside driver */
285 int dispatched;
286
287 /* status flags */
288 unsigned long flags;
289
290 /* node for active/idle bfqq list inside parent bfqd */
291 struct list_head bfqq_list;
292
293 /* associated @bfq_ttime struct */
294 struct bfq_ttime ttime;
295
296 /* bit vector: a 1 for each seeky requests in history */
297 u32 seek_history;
298
299 /* node for the device's burst list */
300 struct hlist_node burst_list_node;
301
302 /* position of the last request enqueued */
303 sector_t last_request_pos;
304
305 /* Number of consecutive pairs of request completion and
306 * arrival, such that the queue becomes idle after the
307 * completion, but the next request arrives within an idle
308 * time slice; used only if the queue's IO_bound flag has been
309 * cleared.
310 */
311 unsigned int requests_within_timer;
312
313 /* pid of the process owning the queue, used for logging purposes */
314 pid_t pid;
315
316 /*
317 * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL
318 * if the queue is shared.
319 */
320 struct bfq_io_cq *bic;
321
322 /* current maximum weight-raising time for this queue */
323 unsigned long wr_cur_max_time;
324 /*
325 * Minimum time instant such that, only if a new request is
326 * enqueued after this time instant in an idle @bfq_queue with
327 * no outstanding requests, then the task associated with the
328 * queue it is deemed as soft real-time (see the comments on
329 * the function bfq_bfqq_softrt_next_start())
330 */
331 unsigned long soft_rt_next_start;
332 /*
333 * Start time of the current weight-raising period if
334 * the @bfq-queue is being weight-raised, otherwise
335 * finish time of the last weight-raising period.
336 */
337 unsigned long last_wr_start_finish;
338 /* factor by which the weight of this queue is multiplied */
339 unsigned int wr_coeff;
340 /*
341 * Time of the last transition of the @bfq_queue from idle to
342 * backlogged.
343 */
344 unsigned long last_idle_bklogged;
345 /*
346 * Cumulative service received from the @bfq_queue since the
347 * last transition from idle to backlogged.
348 */
349 unsigned long service_from_backlogged;
Paolo Valente8a8747d2018-01-13 12:05:18 +0100350 /*
351 * Cumulative service received from the @bfq_queue since its
352 * last transition to weight-raised state.
353 */
354 unsigned long service_from_wr;
Paolo Valenteea25da42017-04-19 08:48:24 -0600355
356 /*
357 * Value of wr start time when switching to soft rt
358 */
359 unsigned long wr_start_at_switch_to_srt;
360
361 unsigned long split_time; /* time of last split */
Paolo Valente7b8fa3b2017-12-20 12:38:33 +0100362
363 unsigned long first_IO_time; /* time of first I/O for this queue */
Paolo Valented0edc242018-09-14 16:23:08 +0200364
365 /* max service rate measured so far */
366 u32 max_service_rate;
Paolo Valenteea25da42017-04-19 08:48:24 -0600367};
368
369/**
370 * struct bfq_io_cq - per (request_queue, io_context) structure.
371 */
372struct bfq_io_cq {
373 /* associated io_cq structure */
374 struct io_cq icq; /* must be the first member */
375 /* array of two process queues, the sync and the async */
376 struct bfq_queue *bfqq[2];
377 /* per (request_queue, blkcg) ioprio */
378 int ioprio;
379#ifdef CONFIG_BFQ_GROUP_IOSCHED
380 uint64_t blkcg_serial_nr; /* the current blkcg serial */
381#endif
382 /*
Paolo Valented5be3fe2017-08-04 07:35:10 +0200383 * Snapshot of the has_short_time flag before merging; taken
384 * to remember its value while the queue is merged, so as to
385 * be able to restore it in case of split.
Paolo Valenteea25da42017-04-19 08:48:24 -0600386 */
Paolo Valented5be3fe2017-08-04 07:35:10 +0200387 bool saved_has_short_ttime;
Paolo Valenteea25da42017-04-19 08:48:24 -0600388 /*
389 * Same purpose as the previous two fields for the I/O bound
390 * classification of a queue.
391 */
392 bool saved_IO_bound;
393
394 /*
395 * Same purpose as the previous fields for the value of the
396 * field keeping the queue's belonging to a large burst
397 */
398 bool saved_in_large_burst;
399 /*
400 * True if the queue belonged to a burst list before its merge
401 * with another cooperating queue.
402 */
403 bool was_in_burst_list;
404
405 /*
406 * Similar to previous fields: save wr information.
407 */
408 unsigned long saved_wr_coeff;
409 unsigned long saved_last_wr_start_finish;
410 unsigned long saved_wr_start_at_switch_to_srt;
411 unsigned int saved_wr_cur_max_time;
412 struct bfq_ttime saved_ttime;
413};
414
Paolo Valenteea25da42017-04-19 08:48:24 -0600415/**
416 * struct bfq_data - per-device data structure.
417 *
418 * All the fields are protected by @lock.
419 */
420struct bfq_data {
421 /* device request queue */
422 struct request_queue *queue;
423 /* dispatch queue */
424 struct list_head dispatch;
425
426 /* root bfq_group for the device */
427 struct bfq_group *root_group;
428
429 /*
430 * rbtree of weight counters of @bfq_queues, sorted by
431 * weight. Used to keep track of whether all @bfq_queues have
432 * the same weight. The tree contains one counter for each
433 * distinct weight associated to some active and not
434 * weight-raised @bfq_queue (see the comments to the functions
435 * bfq_weights_tree_[add|remove] for further details).
436 */
Paolo Valentefb53ac62019-03-12 09:59:28 +0100437 struct rb_root_cached queue_weights_tree;
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100438
Paolo Valenteea25da42017-04-19 08:48:24 -0600439 /*
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100440 * Number of groups with at least one descendant process that
441 * has at least one request waiting for completion. Note that
442 * this accounts for also requests already dispatched, but not
443 * yet completed. Therefore this number of groups may differ
444 * (be larger) than the number of active groups, as a group is
445 * considered active only if its corresponding entity has
446 * descendant queues with at least one request queued. This
447 * number is used to decide whether a scenario is symmetric.
448 * For a detailed explanation see comments on the computation
449 * of the variable asymmetric_scenario in the function
450 * bfq_better_to_idle().
451 *
452 * However, it is hard to compute this number exactly, for
453 * groups with multiple descendant processes. Consider a group
454 * that is inactive, i.e., that has no descendant process with
455 * pending I/O inside BFQ queues. Then suppose that
456 * num_groups_with_pending_reqs is still accounting for this
457 * group, because the group has descendant processes with some
458 * I/O request still in flight. num_groups_with_pending_reqs
459 * should be decremented when the in-flight request of the
460 * last descendant process is finally completed (assuming that
461 * nothing else has changed for the group in the meantime, in
462 * terms of composition of the group and active/inactive state of child
463 * groups and processes). To accomplish this, an additional
464 * pending-request counter must be added to entities, and must
465 * be updated correctly. To avoid this additional field and operations,
466 * we resort to the following tradeoff between simplicity and
467 * accuracy: for an inactive group that is still counted in
468 * num_groups_with_pending_reqs, we decrement
469 * num_groups_with_pending_reqs when the first descendant
470 * process of the group remains with no request waiting for
471 * completion.
472 *
473 * Even this simpler decrement strategy requires a little
474 * carefulness: to avoid multiple decrements, we flag a group,
475 * more precisely an entity representing a group, as still
476 * counted in num_groups_with_pending_reqs when it becomes
477 * inactive. Then, when the first descendant queue of the
478 * entity remains with no request waiting for completion,
479 * num_groups_with_pending_reqs is decremented, and this flag
480 * is reset. After this flag is reset for the entity,
481 * num_groups_with_pending_reqs won't be decremented any
482 * longer in case a new descendant queue of the entity remains
483 * with no request waiting for completion.
Paolo Valenteea25da42017-04-19 08:48:24 -0600484 */
Paolo Valenteba7aeae2018-12-06 19:18:18 +0100485 unsigned int num_groups_with_pending_reqs;
Paolo Valenteea25da42017-04-19 08:48:24 -0600486
487 /*
Paolo Valente73d58112019-01-29 12:06:29 +0100488 * Per-class (RT, BE, IDLE) number of bfq_queues containing
489 * requests (including the queue in service, even if it is
490 * idling).
Paolo Valenteea25da42017-04-19 08:48:24 -0600491 */
Paolo Valente73d58112019-01-29 12:06:29 +0100492 unsigned int busy_queues[3];
Paolo Valenteea25da42017-04-19 08:48:24 -0600493 /* number of weight-raised busy @bfq_queues */
494 int wr_busy_queues;
495 /* number of queued requests */
496 int queued;
497 /* number of requests dispatched and waiting for completion */
498 int rq_in_driver;
499
500 /*
501 * Maximum number of requests in driver in the last
502 * @hw_tag_samples completed requests.
503 */
504 int max_rq_in_driver;
505 /* number of samples used to calculate hw_tag */
506 int hw_tag_samples;
507 /* flag set to one if the driver is showing a queueing behavior */
508 int hw_tag;
509
510 /* number of budgets assigned */
511 int budgets_assigned;
512
513 /*
514 * Timer set when idling (waiting) for the next request from
515 * the queue in service.
516 */
517 struct hrtimer idle_slice_timer;
518
519 /* bfq_queue in service */
520 struct bfq_queue *in_service_queue;
521
522 /* on-disk position of the last served request */
523 sector_t last_position;
524
Paolo Valente058fdec2019-01-29 12:06:38 +0100525 /* position of the last served request for the in-service queue */
526 sector_t in_serv_last_pos;
527
Paolo Valenteea25da42017-04-19 08:48:24 -0600528 /* time of last request completion (ns) */
529 u64 last_completion;
530
Paolo Valente2341d6622019-03-12 09:59:29 +0100531 /* time of last transition from empty to non-empty (ns) */
532 u64 last_empty_occupied_ns;
533
534 /*
535 * Flag set to activate the sampling of the total service time
536 * of a just-arrived first I/O request (see
537 * bfq_update_inject_limit()). This will cause the setting of
538 * waited_rq when the request is finally dispatched.
539 */
540 bool wait_dispatch;
541 /*
542 * If set, then bfq_update_inject_limit() is invoked when
543 * waited_rq is eventually completed.
544 */
545 struct request *waited_rq;
546 /*
547 * True if some request has been injected during the last service hole.
548 */
549 bool rqs_injected;
550
Paolo Valenteea25da42017-04-19 08:48:24 -0600551 /* time of first rq dispatch in current observation interval (ns) */
552 u64 first_dispatch;
553 /* time of last rq dispatch in current observation interval (ns) */
554 u64 last_dispatch;
555
556 /* beginning of the last budget */
557 ktime_t last_budget_start;
558 /* beginning of the last idle slice */
559 ktime_t last_idling_start;
Paolo Valente2341d6622019-03-12 09:59:29 +0100560 unsigned long last_idling_start_jiffies;
Paolo Valenteea25da42017-04-19 08:48:24 -0600561
562 /* number of samples in current observation interval */
563 int peak_rate_samples;
564 /* num of samples of seq dispatches in current observation interval */
565 u32 sequential_samples;
566 /* total num of sectors transferred in current observation interval */
567 u64 tot_sectors_dispatched;
568 /* max rq size seen during current observation interval (sectors) */
569 u32 last_rq_max_size;
570 /* time elapsed from first dispatch in current observ. interval (us) */
571 u64 delta_from_first;
572 /*
573 * Current estimate of the device peak rate, measured in
Paolo Valentebc56e2c2018-03-26 16:06:24 +0200574 * [(sectors/usec) / 2^BFQ_RATE_SHIFT]. The left-shift by
Paolo Valenteea25da42017-04-19 08:48:24 -0600575 * BFQ_RATE_SHIFT is performed to increase precision in
576 * fixed-point calculations.
577 */
578 u32 peak_rate;
579
580 /* maximum budget allotted to a bfq_queue before rescheduling */
581 int bfq_max_budget;
582
583 /* list of all the bfq_queues active on the device */
584 struct list_head active_list;
585 /* list of all the bfq_queues idle on the device */
586 struct list_head idle_list;
587
588 /*
589 * Timeout for async/sync requests; when it fires, requests
590 * are served in fifo order.
591 */
592 u64 bfq_fifo_expire[2];
593 /* weight of backward seeks wrt forward ones */
594 unsigned int bfq_back_penalty;
595 /* maximum allowed backward seek */
596 unsigned int bfq_back_max;
597 /* maximum idling time */
598 u32 bfq_slice_idle;
599
600 /* user-configured max budget value (0 for auto-tuning) */
601 int bfq_user_max_budget;
602 /*
603 * Timeout for bfq_queues to consume their budget; used to
604 * prevent seeky queues from imposing long latencies to
605 * sequential or quasi-sequential ones (this also implies that
606 * seeky queues cannot receive guarantees in the service
607 * domain; after a timeout they are charged for the time they
608 * have been in service, to preserve fairness among them, but
609 * without service-domain guarantees).
610 */
611 unsigned int bfq_timeout;
612
613 /*
614 * Number of consecutive requests that must be issued within
615 * the idle time slice to set again idling to a queue which
616 * was marked as non-I/O-bound (see the definition of the
617 * IO_bound flag for further details).
618 */
619 unsigned int bfq_requests_within_timer;
620
621 /*
622 * Force device idling whenever needed to provide accurate
623 * service guarantees, without caring about throughput
624 * issues. CAVEAT: this may even increase latencies, in case
625 * of useless idling for processes that did stop doing I/O.
626 */
627 bool strict_guarantees;
628
629 /*
630 * Last time at which a queue entered the current burst of
631 * queues being activated shortly after each other; for more
632 * details about this and the following parameters related to
633 * a burst of activations, see the comments on the function
634 * bfq_handle_burst.
635 */
636 unsigned long last_ins_in_burst;
637 /*
638 * Reference time interval used to decide whether a queue has
639 * been activated shortly after @last_ins_in_burst.
640 */
641 unsigned long bfq_burst_interval;
642 /* number of queues in the current burst of queue activations */
643 int burst_size;
644
645 /* common parent entity for the queues in the burst */
646 struct bfq_entity *burst_parent_entity;
647 /* Maximum burst size above which the current queue-activation
648 * burst is deemed as 'large'.
649 */
650 unsigned long bfq_large_burst_thresh;
651 /* true if a large queue-activation burst is in progress */
652 bool large_burst;
653 /*
654 * Head of the burst list (as for the above fields, more
655 * details in the comments on the function bfq_handle_burst).
656 */
657 struct hlist_head burst_list;
658
659 /* if set to true, low-latency heuristics are enabled */
660 bool low_latency;
661 /*
662 * Maximum factor by which the weight of a weight-raised queue
663 * is multiplied.
664 */
665 unsigned int bfq_wr_coeff;
666 /* maximum duration of a weight-raising period (jiffies) */
667 unsigned int bfq_wr_max_time;
668
669 /* Maximum weight-raising duration for soft real-time processes */
670 unsigned int bfq_wr_rt_max_time;
671 /*
672 * Minimum idle period after which weight-raising may be
673 * reactivated for a queue (in jiffies).
674 */
675 unsigned int bfq_wr_min_idle_time;
676 /*
677 * Minimum period between request arrivals after which
678 * weight-raising may be reactivated for an already busy async
679 * queue (in jiffies).
680 */
681 unsigned long bfq_wr_min_inter_arr_async;
682
683 /* Max service-rate for a soft real-time queue, in sectors/sec */
684 unsigned int bfq_wr_max_softrt_rate;
685 /*
Paolo Valentee24f1c22018-05-31 16:45:06 +0200686 * Cached value of the product ref_rate*ref_wr_duration, used
687 * for computing the maximum duration of weight raising
688 * automatically.
Paolo Valenteea25da42017-04-19 08:48:24 -0600689 */
Paolo Valentee24f1c22018-05-31 16:45:06 +0200690 u64 rate_dur_prod;
Paolo Valenteea25da42017-04-19 08:48:24 -0600691
692 /* fallback dummy bfqq for extreme OOM conditions */
693 struct bfq_queue oom_bfqq;
694
695 spinlock_t lock;
696
697 /*
698 * bic associated with the task issuing current bio for
699 * merging. This and the next field are used as a support to
700 * be able to perform the bic lookup, needed by bio-merge
701 * functions, before the scheduler lock is taken, and thus
702 * avoid taking the request-queue lock while the scheduler
703 * lock is being held.
704 */
705 struct bfq_io_cq *bio_bic;
706 /* bfqq associated with the task issuing current bio for merging */
707 struct bfq_queue *bio_bfqq;
Paolo Valentea52a69e2018-01-13 12:05:17 +0100708
709 /*
Paolo Valentea52a69e2018-01-13 12:05:17 +0100710 * Depth limits used in bfq_limit_depth (see comments on the
711 * function)
712 */
713 unsigned int word_depths[2][2];
Paolo Valenteea25da42017-04-19 08:48:24 -0600714};
715
716enum bfqq_state_flags {
717 BFQQF_just_created = 0, /* queue just allocated */
718 BFQQF_busy, /* has requests or is in service */
719 BFQQF_wait_request, /* waiting for a request */
720 BFQQF_non_blocking_wait_rq, /*
721 * waiting for a request
722 * without idling the device
723 */
724 BFQQF_fifo_expire, /* FIFO checked in this slice */
Paolo Valented5be3fe2017-08-04 07:35:10 +0200725 BFQQF_has_short_ttime, /* queue has a short think time */
Paolo Valenteea25da42017-04-19 08:48:24 -0600726 BFQQF_sync, /* synchronous queue */
727 BFQQF_IO_bound, /*
728 * bfqq has timed-out at least once
729 * having consumed at most 2/10 of
730 * its budget
731 */
732 BFQQF_in_large_burst, /*
733 * bfqq activated in a large burst,
734 * see comments to bfq_handle_burst.
735 */
736 BFQQF_softrt_update, /*
737 * may need softrt-next-start
738 * update
739 */
740 BFQQF_coop, /* bfqq is shared */
741 BFQQF_split_coop /* shared bfqq will be split */
742};
743
744#define BFQ_BFQQ_FNS(name) \
745void bfq_mark_bfqq_##name(struct bfq_queue *bfqq); \
746void bfq_clear_bfqq_##name(struct bfq_queue *bfqq); \
747int bfq_bfqq_##name(const struct bfq_queue *bfqq);
748
749BFQ_BFQQ_FNS(just_created);
750BFQ_BFQQ_FNS(busy);
751BFQ_BFQQ_FNS(wait_request);
752BFQ_BFQQ_FNS(non_blocking_wait_rq);
753BFQ_BFQQ_FNS(fifo_expire);
Paolo Valented5be3fe2017-08-04 07:35:10 +0200754BFQ_BFQQ_FNS(has_short_ttime);
Paolo Valenteea25da42017-04-19 08:48:24 -0600755BFQ_BFQQ_FNS(sync);
756BFQ_BFQQ_FNS(IO_bound);
757BFQ_BFQQ_FNS(in_large_burst);
758BFQ_BFQQ_FNS(coop);
759BFQ_BFQQ_FNS(split_coop);
760BFQ_BFQQ_FNS(softrt_update);
761#undef BFQ_BFQQ_FNS
762
763/* Expiration reasons. */
764enum bfqq_expiration {
765 BFQQE_TOO_IDLE = 0, /*
766 * queue has been idling for
767 * too long
768 */
769 BFQQE_BUDGET_TIMEOUT, /* budget took too long to be used */
770 BFQQE_BUDGET_EXHAUSTED, /* budget consumed */
771 BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */
772 BFQQE_PREEMPTED /* preemption in progress */
773};
774
775struct bfqg_stats {
Luca Miccioa33801e2017-11-13 07:34:10 +0100776#if defined(CONFIG_BFQ_GROUP_IOSCHED) && defined(CONFIG_DEBUG_BLK_CGROUP)
Paolo Valenteea25da42017-04-19 08:48:24 -0600777 /* number of ios merged */
778 struct blkg_rwstat merged;
779 /* total time spent on device in ns, may not be accurate w/ queueing */
780 struct blkg_rwstat service_time;
781 /* total time spent waiting in scheduler queue in ns */
782 struct blkg_rwstat wait_time;
783 /* number of IOs queued up */
784 struct blkg_rwstat queued;
785 /* total disk time and nr sectors dispatched by this group */
786 struct blkg_stat time;
787 /* sum of number of ios queued across all samples */
788 struct blkg_stat avg_queue_size_sum;
789 /* count of samples taken for average */
790 struct blkg_stat avg_queue_size_samples;
791 /* how many times this group has been removed from service tree */
792 struct blkg_stat dequeue;
793 /* total time spent waiting for it to be assigned a timeslice. */
794 struct blkg_stat group_wait_time;
795 /* time spent idling for this blkcg_gq */
796 struct blkg_stat idle_time;
797 /* total time with empty current active q with other requests queued */
798 struct blkg_stat empty_time;
799 /* fields after this shouldn't be cleared on stat reset */
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700800 u64 start_group_wait_time;
801 u64 start_idle_time;
802 u64 start_empty_time;
Paolo Valenteea25da42017-04-19 08:48:24 -0600803 uint16_t flags;
Luca Miccioa33801e2017-11-13 07:34:10 +0100804#endif /* CONFIG_BFQ_GROUP_IOSCHED && CONFIG_DEBUG_BLK_CGROUP */
Paolo Valenteea25da42017-04-19 08:48:24 -0600805};
806
807#ifdef CONFIG_BFQ_GROUP_IOSCHED
808
809/*
810 * struct bfq_group_data - per-blkcg storage for the blkio subsystem.
811 *
812 * @ps: @blkcg_policy_storage that this structure inherits
813 * @weight: weight of the bfq_group
814 */
815struct bfq_group_data {
816 /* must be the first member */
817 struct blkcg_policy_data pd;
818
819 unsigned int weight;
820};
821
822/**
823 * struct bfq_group - per (device, cgroup) data structure.
824 * @entity: schedulable entity to insert into the parent group sched_data.
825 * @sched_data: own sched_data, to contain child entities (they may be
826 * both bfq_queues and bfq_groups).
827 * @bfqd: the bfq_data for the device this group acts upon.
828 * @async_bfqq: array of async queues for all the tasks belonging to
829 * the group, one queue per ioprio value per ioprio_class,
830 * except for the idle class that has only one queue.
831 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
832 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
833 * to avoid too many special cases during group creation/
834 * migration.
835 * @stats: stats for this bfqg.
836 * @active_entities: number of active entities belonging to the group;
837 * unused for the root group. Used to know whether there
838 * are groups with more than one active @bfq_entity
839 * (see the comments to the function
840 * bfq_bfqq_may_idle()).
841 * @rq_pos_tree: rbtree sorted by next_request position, used when
842 * determining if two or more queues have interleaving
843 * requests (see bfq_find_close_cooperator()).
844 *
845 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
846 * there is a set of bfq_groups, each one collecting the lower-level
847 * entities belonging to the group that are acting on the same device.
848 *
849 * Locking works as follows:
850 * o @bfqd is protected by the queue lock, RCU is used to access it
851 * from the readers.
852 * o All the other fields are protected by the @bfqd queue lock.
853 */
854struct bfq_group {
855 /* must be the first member */
856 struct blkg_policy_data pd;
857
Paolo Valente8f9bebc2017-06-05 10:11:15 +0200858 /* cached path for this blkg (see comments in bfq_bic_update_cgroup) */
859 char blkg_path[128];
860
861 /* reference counter (see comments in bfq_bic_update_cgroup) */
862 int ref;
863
Paolo Valenteea25da42017-04-19 08:48:24 -0600864 struct bfq_entity entity;
865 struct bfq_sched_data sched_data;
866
867 void *bfqd;
868
869 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
870 struct bfq_queue *async_idle_bfqq;
871
872 struct bfq_entity *my_entity;
873
874 int active_entities;
875
876 struct rb_root rq_pos_tree;
877
878 struct bfqg_stats stats;
879};
880
881#else
882struct bfq_group {
883 struct bfq_sched_data sched_data;
884
885 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
886 struct bfq_queue *async_idle_bfqq;
887
888 struct rb_root rq_pos_tree;
889};
890#endif
891
892struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
893
894/* --------------- main algorithm interface ----------------- */
895
896#define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \
897 { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 })
898
899extern const int bfq_timeout;
900
901struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync);
902void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, bool is_sync);
903struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic);
Paolo Valenteea25da42017-04-19 08:48:24 -0600904void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq);
Federico Motta2d29c9f2018-10-12 11:55:57 +0200905void bfq_weights_tree_add(struct bfq_data *bfqd, struct bfq_queue *bfqq,
Paolo Valentefb53ac62019-03-12 09:59:28 +0100906 struct rb_root_cached *root);
Paolo Valente04715592018-06-25 21:55:34 +0200907void __bfq_weights_tree_remove(struct bfq_data *bfqd,
Federico Motta2d29c9f2018-10-12 11:55:57 +0200908 struct bfq_queue *bfqq,
Paolo Valentefb53ac62019-03-12 09:59:28 +0100909 struct rb_root_cached *root);
Paolo Valente04715592018-06-25 21:55:34 +0200910void bfq_weights_tree_remove(struct bfq_data *bfqd,
911 struct bfq_queue *bfqq);
Paolo Valenteea25da42017-04-19 08:48:24 -0600912void bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq,
913 bool compensate, enum bfqq_expiration reason);
914void bfq_put_queue(struct bfq_queue *bfqq);
915void bfq_end_wr_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
916void bfq_schedule_dispatch(struct bfq_data *bfqd);
917void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
918
919/* ------------ end of main algorithm interface -------------- */
920
921/* ---------------- cgroups-support interface ---------------- */
922
Paolo Valenteea25da42017-04-19 08:48:24 -0600923void bfqg_stats_update_io_add(struct bfq_group *bfqg, struct bfq_queue *bfqq,
924 unsigned int op);
925void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op);
926void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op);
Omar Sandoval84c7afc2018-05-09 02:08:51 -0700927void bfqg_stats_update_completion(struct bfq_group *bfqg, u64 start_time_ns,
928 u64 io_start_time_ns, unsigned int op);
Paolo Valenteea25da42017-04-19 08:48:24 -0600929void bfqg_stats_update_dequeue(struct bfq_group *bfqg);
930void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg);
931void bfqg_stats_update_idle_time(struct bfq_group *bfqg);
932void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg);
933void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg);
934void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq,
935 struct bfq_group *bfqg);
936
937void bfq_init_entity(struct bfq_entity *entity, struct bfq_group *bfqg);
938void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio);
939void bfq_end_wr_async(struct bfq_data *bfqd);
940struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd,
941 struct blkcg *blkcg);
942struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg);
943struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
944struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, int node);
Paolo Valente8f9bebc2017-06-05 10:11:15 +0200945void bfqg_and_blkg_put(struct bfq_group *bfqg);
Paolo Valenteea25da42017-04-19 08:48:24 -0600946
947#ifdef CONFIG_BFQ_GROUP_IOSCHED
Jens Axboe659b3392017-04-20 09:37:05 -0600948extern struct cftype bfq_blkcg_legacy_files[];
949extern struct cftype bfq_blkg_files[];
Paolo Valenteea25da42017-04-19 08:48:24 -0600950extern struct blkcg_policy blkcg_policy_bfq;
951#endif
952
953/* ------------- end of cgroups-support interface ------------- */
954
955/* - interface of the internal hierarchical B-WF2Q+ scheduler - */
956
957#ifdef CONFIG_BFQ_GROUP_IOSCHED
958/* both next loops stop at one of the child entities of the root group */
959#define for_each_entity(entity) \
960 for (; entity ; entity = entity->parent)
961
962/*
963 * For each iteration, compute parent in advance, so as to be safe if
964 * entity is deallocated during the iteration. Such a deallocation may
965 * happen as a consequence of a bfq_put_queue that frees the bfq_queue
966 * containing entity.
967 */
968#define for_each_entity_safe(entity, parent) \
969 for (; entity && ({ parent = entity->parent; 1; }); entity = parent)
970
971#else /* CONFIG_BFQ_GROUP_IOSCHED */
972/*
973 * Next two macros are fake loops when cgroups support is not
974 * enabled. I fact, in such a case, there is only one level to go up
975 * (to reach the root group).
976 */
977#define for_each_entity(entity) \
978 for (; entity ; entity = NULL)
979
980#define for_each_entity_safe(entity, parent) \
981 for (parent = NULL; entity ; entity = parent)
982#endif /* CONFIG_BFQ_GROUP_IOSCHED */
983
984struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq);
985struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity);
Paolo Valente73d58112019-01-29 12:06:29 +0100986unsigned int bfq_tot_busy_queues(struct bfq_data *bfqd);
Paolo Valenteea25da42017-04-19 08:48:24 -0600987struct bfq_service_tree *bfq_entity_service_tree(struct bfq_entity *entity);
988struct bfq_entity *bfq_entity_of(struct rb_node *node);
989unsigned short bfq_ioprio_to_weight(int ioprio);
990void bfq_put_idle_entity(struct bfq_service_tree *st,
991 struct bfq_entity *entity);
992struct bfq_service_tree *
993__bfq_entity_update_weight_prio(struct bfq_service_tree *old_st,
Paolo Valente431b17f2017-07-03 10:00:10 +0200994 struct bfq_entity *entity,
995 bool update_class_too);
Paolo Valenteea25da42017-04-19 08:48:24 -0600996void bfq_bfqq_served(struct bfq_queue *bfqq, int served);
997void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq,
998 unsigned long time_ms);
999bool __bfq_deactivate_entity(struct bfq_entity *entity,
1000 bool ins_into_idle_tree);
1001bool next_queue_may_preempt(struct bfq_data *bfqd);
1002struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd);
1003void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd);
1004void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1005 bool ins_into_idle_tree, bool expiration);
1006void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
Paolo Valente80294c32017-08-31 08:46:29 +02001007void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1008 bool expiration);
Paolo Valenteea25da42017-04-19 08:48:24 -06001009void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq,
1010 bool expiration);
1011void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq);
1012
1013/* --------------- end of interface of B-WF2Q+ ---------------- */
1014
1015/* Logging facilities. */
1016#ifdef CONFIG_BFQ_GROUP_IOSCHED
1017struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
1018
1019#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \
Shaohua Li35fe6d72017-07-12 11:49:56 -07001020 blk_add_cgroup_trace_msg((bfqd)->queue, \
1021 bfqg_to_blkg(bfqq_group(bfqq))->blkcg, \
1022 "bfq%d%c " fmt, (bfqq)->pid, \
1023 bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args); \
Paolo Valenteea25da42017-04-19 08:48:24 -06001024} while (0)
1025
Shaohua Li35fe6d72017-07-12 11:49:56 -07001026#define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do { \
1027 blk_add_cgroup_trace_msg((bfqd)->queue, \
1028 bfqg_to_blkg(bfqg)->blkcg, fmt, ##args); \
1029} while (0)
Paolo Valenteea25da42017-04-19 08:48:24 -06001030
1031#else /* CONFIG_BFQ_GROUP_IOSCHED */
1032
1033#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
1034 blk_add_trace_msg((bfqd)->queue, "bfq%d%c " fmt, (bfqq)->pid, \
1035 bfq_bfqq_sync((bfqq)) ? 'S' : 'A', \
1036 ##args)
1037#define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do {} while (0)
1038
1039#endif /* CONFIG_BFQ_GROUP_IOSCHED */
1040
1041#define bfq_log(bfqd, fmt, args...) \
1042 blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
1043
1044#endif /* _BFQ_H */