blob: e6f1a88f4dd607380789cf38be46879d7d18d976 [file] [log] [blame]
Paolo Valente70f28712013-05-09 19:10:02 +02001/*
2 * BFQ-v7r8 for 3.4.0: data structures and common functions prototypes.
3 *
4 * Based on ideas and code from CFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
6 *
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 *
10 * Copyright (C) 2010 Paolo Valente <paolo.valente@unimore.it>
11 */
12
13#ifndef _BFQ_H
14#define _BFQ_H
15
16#include <linux/blktrace_api.h>
17#include <linux/hrtimer.h>
18#include <linux/ioprio.h>
19#include <linux/rbtree.h>
20
21#define BFQ_IOPRIO_CLASSES 3
22#define BFQ_CL_IDLE_TIMEOUT (HZ/5)
23
24#define BFQ_MIN_WEIGHT 1
25#define BFQ_MAX_WEIGHT 1000
26
27#define BFQ_DEFAULT_QUEUE_IOPRIO 4
28
29#define BFQ_DEFAULT_GRP_WEIGHT 10
30#define BFQ_DEFAULT_GRP_IOPRIO 0
31#define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
32
33struct bfq_entity;
34
35/**
36 * struct bfq_service_tree - per ioprio_class service tree.
37 * @active: tree for active entities (i.e., those backlogged).
38 * @idle: tree for idle entities (i.e., those not backlogged, with V <= F_i).
39 * @first_idle: idle entity with minimum F_i.
40 * @last_idle: idle entity with maximum F_i.
41 * @vtime: scheduler virtual time.
42 * @wsum: scheduler weight sum; active and idle entities contribute to it.
43 *
44 * Each service tree represents a B-WF2Q+ scheduler on its own. Each
45 * ioprio_class has its own independent scheduler, and so its own
46 * bfq_service_tree. All the fields are protected by the queue lock
47 * of the containing bfqd.
48 */
49struct bfq_service_tree {
50 struct rb_root active;
51 struct rb_root idle;
52
53 struct bfq_entity *first_idle;
54 struct bfq_entity *last_idle;
55
56 u64 vtime;
57 unsigned long wsum;
58};
59
60/**
61 * struct bfq_sched_data - multi-class scheduler.
62 * @in_service_entity: entity in service.
63 * @next_in_service: head-of-the-line entity in the scheduler.
64 * @service_tree: array of service trees, one per ioprio_class.
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
68 * an intermediate queue on a hierarchical setup.
69 * @next_in_service points to the active entity of the sched_data
70 * service trees that will be scheduled next.
71 *
72 * The supported ioprio_classes are the same as in CFQ, in descending
73 * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
74 * Requests from higher priority queues are served before all the
75 * requests from lower priority queues; among requests of the same
76 * queue requests are served according to B-WF2Q+.
77 * All the fields are protected by the queue lock of the containing bfqd.
78 */
79struct bfq_sched_data {
80 struct bfq_entity *in_service_entity;
81 struct bfq_entity *next_in_service;
82 struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
83};
84
85/**
86 * struct bfq_weight_counter - counter of the number of all active entities
87 * with a given weight.
88 * @weight: weight of the entities that this counter refers to.
89 * @num_active: number of active entities with this weight.
90 * @weights_node: weights tree member (see bfq_data's @queue_weights_tree
91 * and @group_weights_tree).
92 */
93struct bfq_weight_counter {
94 short int weight;
95 unsigned int num_active;
96 struct rb_node weights_node;
97};
98
99/**
100 * struct bfq_entity - schedulable entity.
101 * @rb_node: service_tree member.
102 * @weight_counter: pointer to the weight counter associated with this entity.
103 * @on_st: flag, true if the entity is on a tree (either the active or
104 * the idle one of its service_tree).
105 * @finish: B-WF2Q+ finish timestamp (aka F_i).
106 * @start: B-WF2Q+ start timestamp (aka S_i).
107 * @tree: tree the entity is enqueued into; %NULL if not on a tree.
108 * @min_start: minimum start time of the (active) subtree rooted at
109 * this entity; used for O(log N) lookups into active trees.
110 * @service: service received during the last round of service.
111 * @budget: budget used to calculate F_i; F_i = S_i + @budget / @weight.
112 * @weight: weight of the queue
113 * @parent: parent entity, for hierarchical scheduling.
114 * @my_sched_data: for non-leaf nodes in the cgroup hierarchy, the
115 * associated scheduler queue, %NULL on leaf nodes.
116 * @sched_data: the scheduler queue this entity belongs to.
117 * @ioprio: the ioprio in use.
118 * @new_weight: when a weight change is requested, the new weight value.
119 * @orig_weight: original weight, used to implement weight boosting
120 * @new_ioprio: when an ioprio change is requested, the new ioprio value.
121 * @ioprio_class: the ioprio_class in use.
122 * @new_ioprio_class: when an ioprio_class change is requested, the new
123 * ioprio_class value.
124 * @ioprio_changed: flag, true when the user requested a weight, ioprio or
125 * ioprio_class change.
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 @ioprio_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 struct rb_node rb_node;
153 struct bfq_weight_counter *weight_counter;
154
155 int on_st;
156
157 u64 finish;
158 u64 start;
159
160 struct rb_root *tree;
161
162 u64 min_start;
163
164 unsigned long service, budget;
165 unsigned short weight, new_weight;
166 unsigned short orig_weight;
167
168 struct bfq_entity *parent;
169
170 struct bfq_sched_data *my_sched_data;
171 struct bfq_sched_data *sched_data;
172
173 unsigned short ioprio, new_ioprio;
174 unsigned short ioprio_class, new_ioprio_class;
175
176 int ioprio_changed;
177};
178
179struct bfq_group;
180
181/**
182 * struct bfq_queue - leaf schedulable entity.
183 * @ref: reference counter.
184 * @bfqd: parent bfq_data.
185 * @new_bfqq: shared bfq_queue if queue is cooperating with
186 * one or more other queues.
187 * @pos_node: request-position tree member (see bfq_data's @rq_pos_tree).
188 * @pos_root: request-position tree root (see bfq_data's @rq_pos_tree).
189 * @sort_list: sorted list of pending requests.
190 * @next_rq: if fifo isn't expired, next request to serve.
191 * @queued: nr of requests queued in @sort_list.
192 * @allocated: currently allocated requests.
193 * @meta_pending: pending metadata requests.
194 * @fifo: fifo list of requests in sort_list.
195 * @entity: entity representing this queue in the scheduler.
196 * @max_budget: maximum budget allowed from the feedback mechanism.
197 * @budget_timeout: budget expiration (in jiffies).
198 * @dispatched: number of requests on the dispatch list or inside driver.
199 * @flags: status flags.
200 * @bfqq_list: node for active/idle bfqq list inside our bfqd.
201 * @burst_list_node: node for the device's burst list.
202 * @seek_samples: number of seeks sampled
203 * @seek_total: sum of the distances of the seeks sampled
204 * @seek_mean: mean seek distance
205 * @last_request_pos: position of the last request enqueued
206 * @requests_within_timer: number of consecutive pairs of request completion
207 * and arrival, such that the queue becomes idle
208 * after the completion, but the next request arrives
209 * within an idle time slice; used only if the queue's
210 * IO_bound has been cleared.
211 * @pid: pid of the process owning the queue, used for logging purposes.
212 * @last_wr_start_finish: start time of the current weight-raising period if
213 * the @bfq-queue is being weight-raised, otherwise
214 * finish time of the last weight-raising period
215 * @wr_cur_max_time: current max raising time for this queue
216 * @soft_rt_next_start: minimum time instant such that, only if a new
217 * request is enqueued after this time instant in an
218 * idle @bfq_queue with no outstanding requests, then
219 * the task associated with the queue it is deemed as
220 * soft real-time (see the comments to the function
Mauro Andreolini0b335882015-06-07 02:34:22 +0200221 * bfq_bfqq_softrt_next_start())
Paolo Valente70f28712013-05-09 19:10:02 +0200222 * @last_idle_bklogged: time of the last transition of the @bfq_queue from
223 * idle to backlogged
224 * @service_from_backlogged: cumulative service received from the @bfq_queue
225 * since the last transition from idle to
226 * backlogged
Mauro Andreolini0b335882015-06-07 02:34:22 +0200227 * @bic: pointer to the bfq_io_cq owning the bfq_queue, set to %NULL if the
228 * queue is shared
Paolo Valente70f28712013-05-09 19:10:02 +0200229 *
Mauro Andreolini0b335882015-06-07 02:34:22 +0200230 * A bfq_queue is a leaf request queue; it can be associated with an
231 * io_context or more, if it is async or shared between cooperating
232 * processes. @cgroup holds a reference to the cgroup, to be sure that it
233 * does not disappear while a bfqq still references it (mostly to avoid
234 * races between request issuing and task migration followed by cgroup
235 * destruction).
Paolo Valente70f28712013-05-09 19:10:02 +0200236 * All the fields are protected by the queue lock of the containing bfqd.
237 */
238struct bfq_queue {
239 atomic_t ref;
240 struct bfq_data *bfqd;
241
242 /* fields for cooperating queues handling */
243 struct bfq_queue *new_bfqq;
244 struct rb_node pos_node;
245 struct rb_root *pos_root;
246
247 struct rb_root sort_list;
248 struct request *next_rq;
249 int queued[2];
250 int allocated[2];
251 int meta_pending;
252 struct list_head fifo;
253
254 struct bfq_entity entity;
255
256 unsigned long max_budget;
257 unsigned long budget_timeout;
258
259 int dispatched;
260
261 unsigned int flags;
262
263 struct list_head bfqq_list;
264
265 struct hlist_node burst_list_node;
266
267 unsigned int seek_samples;
268 u64 seek_total;
269 sector_t seek_mean;
270 sector_t last_request_pos;
271
272 unsigned int requests_within_timer;
273
274 pid_t pid;
Mauro Andreolini0b335882015-06-07 02:34:22 +0200275 struct bfq_io_cq *bic;
Paolo Valente70f28712013-05-09 19:10:02 +0200276
277 /* weight-raising fields */
278 unsigned long wr_cur_max_time;
279 unsigned long soft_rt_next_start;
280 unsigned long last_wr_start_finish;
281 unsigned int wr_coeff;
282 unsigned long last_idle_bklogged;
283 unsigned long service_from_backlogged;
284};
285
286/**
287 * struct bfq_ttime - per process thinktime stats.
288 * @ttime_total: total process thinktime
289 * @ttime_samples: number of thinktime samples
290 * @ttime_mean: average process thinktime
291 */
292struct bfq_ttime {
293 unsigned long last_end_request;
294
295 unsigned long ttime_total;
296 unsigned long ttime_samples;
297 unsigned long ttime_mean;
298};
299
300/**
301 * struct bfq_io_cq - per (request_queue, io_context) structure.
302 * @icq: associated io_cq structure
303 * @bfqq: array of two process queues, the sync and the async
304 * @ttime: associated @bfq_ttime struct
Mauro Andreolini0b335882015-06-07 02:34:22 +0200305 * @wr_time_left: snapshot of the time left before weight raising ends
306 * for the sync queue associated to this process; this
307 * snapshot is taken to remember this value while the weight
308 * raising is suspended because the queue is merged with a
309 * shared queue, and is used to set @raising_cur_max_time
310 * when the queue is split from the shared queue and its
311 * weight is raised again
312 * @saved_idle_window: same purpose as the previous field for the idle
313 * window
314 * @saved_IO_bound: same purpose as the previous two fields for the I/O
315 * bound classification of a queue
316 * @saved_in_large_burst: same purpose as the previous fields for the
317 * value of the field keeping the queue's belonging
318 * to a large burst
319 * @was_in_burst_list: true if the queue belonged to a burst list
320 * before its merge with another cooperating queue
321 * @cooperations: counter of consecutive successful queue merges underwent
322 * by any of the process' @bfq_queues
323 * @failed_cooperations: counter of consecutive failed queue merges of any
324 * of the process' @bfq_queues
Paolo Valente70f28712013-05-09 19:10:02 +0200325 */
326struct bfq_io_cq {
327 struct io_cq icq; /* must be the first member */
328 struct bfq_queue *bfqq[2];
329 struct bfq_ttime ttime;
330 int ioprio;
Mauro Andreolini0b335882015-06-07 02:34:22 +0200331
332 unsigned int wr_time_left;
333 bool saved_idle_window;
334 bool saved_IO_bound;
335
336 bool saved_in_large_burst;
337 bool was_in_burst_list;
338
339 unsigned int cooperations;
340 unsigned int failed_cooperations;
Paolo Valente70f28712013-05-09 19:10:02 +0200341};
342
343enum bfq_device_speed {
344 BFQ_BFQD_FAST,
345 BFQ_BFQD_SLOW,
346};
347
348/**
349 * struct bfq_data - per device data structure.
350 * @queue: request queue for the managed device.
351 * @root_group: root bfq_group for the device.
352 * @rq_pos_tree: rbtree sorted by next_request position, used when
353 * determining if two or more queues have interleaving
354 * requests (see bfq_close_cooperator()).
355 * @active_numerous_groups: number of bfq_groups containing more than one
356 * active @bfq_entity.
357 * @queue_weights_tree: rbtree of weight counters of @bfq_queues, sorted by
358 * weight. Used to keep track of whether all @bfq_queues
359 * have the same weight. The tree contains one counter
360 * for each distinct weight associated to some active
361 * and not weight-raised @bfq_queue (see the comments to
362 * the functions bfq_weights_tree_[add|remove] for
363 * further details).
364 * @group_weights_tree: rbtree of non-queue @bfq_entity weight counters, sorted
365 * by weight. Used to keep track of whether all
366 * @bfq_groups have the same weight. The tree contains
367 * one counter for each distinct weight associated to
368 * some active @bfq_group (see the comments to the
369 * functions bfq_weights_tree_[add|remove] for further
370 * details).
371 * @busy_queues: number of bfq_queues containing requests (including the
372 * queue in service, even if it is idling).
373 * @busy_in_flight_queues: number of @bfq_queues containing pending or
374 * in-flight requests, plus the @bfq_queue in
375 * service, even if idle but waiting for the
376 * possible arrival of its next sync request. This
377 * field is updated only if the device is rotational,
378 * but used only if the device is also NCQ-capable.
379 * The reason why the field is updated also for non-
380 * NCQ-capable rotational devices is related to the
381 * fact that the value of @hw_tag may be set also
382 * later than when busy_in_flight_queues may need to
383 * be incremented for the first time(s). Taking also
384 * this possibility into account, to avoid unbalanced
385 * increments/decrements, would imply more overhead
386 * than just updating busy_in_flight_queues
387 * regardless of the value of @hw_tag.
388 * @const_seeky_busy_in_flight_queues: number of constantly-seeky @bfq_queues
389 * (that is, seeky queues that expired
390 * for budget timeout at least once)
391 * containing pending or in-flight
392 * requests, including the in-service
393 * @bfq_queue if constantly seeky. This
394 * field is updated only if the device
395 * is rotational, but used only if the
396 * device is also NCQ-capable (see the
397 * comments to @busy_in_flight_queues).
398 * @wr_busy_queues: number of weight-raised busy @bfq_queues.
399 * @queued: number of queued requests.
400 * @rq_in_driver: number of requests dispatched and waiting for completion.
401 * @sync_flight: number of sync requests in the driver.
402 * @max_rq_in_driver: max number of reqs in driver in the last
403 * @hw_tag_samples completed requests.
404 * @hw_tag_samples: nr of samples used to calculate hw_tag.
405 * @hw_tag: flag set to one if the driver is showing a queueing behavior.
406 * @budgets_assigned: number of budgets assigned.
407 * @idle_slice_timer: timer set when idling for the next sequential request
408 * from the queue in service.
409 * @unplug_work: delayed work to restart dispatching on the request queue.
410 * @in_service_queue: bfq_queue in service.
411 * @in_service_bic: bfq_io_cq (bic) associated with the @in_service_queue.
412 * @last_position: on-disk position of the last served request.
413 * @last_budget_start: beginning of the last budget.
414 * @last_idling_start: beginning of the last idle slice.
415 * @peak_rate: peak transfer rate observed for a budget.
416 * @peak_rate_samples: number of samples used to calculate @peak_rate.
417 * @bfq_max_budget: maximum budget allotted to a bfq_queue before
418 * rescheduling.
419 * @group_list: list of all the bfq_groups active on the device.
420 * @active_list: list of all the bfq_queues active on the device.
421 * @idle_list: list of all the bfq_queues idle on the device.
422 * @bfq_fifo_expire: timeout for async/sync requests; when it expires
423 * requests are served in fifo order.
424 * @bfq_back_penalty: weight of backward seeks wrt forward ones.
425 * @bfq_back_max: maximum allowed backward seek.
426 * @bfq_slice_idle: maximum idling time.
427 * @bfq_user_max_budget: user-configured max budget value
428 * (0 for auto-tuning).
429 * @bfq_max_budget_async_rq: maximum budget (in nr of requests) allotted to
430 * async queues.
431 * @bfq_timeout: timeout for bfq_queues to consume their budget; used to
432 * to prevent seeky queues to impose long latencies to well
433 * behaved ones (this also implies that seeky queues cannot
434 * receive guarantees in the service domain; after a timeout
435 * they are charged for the whole allocated budget, to try
436 * to preserve a behavior reasonably fair among them, but
437 * without service-domain guarantees).
438 * @bfq_coop_thresh: number of queue merges after which a @bfq_queue is
439 * no more granted any weight-raising.
440 * @bfq_failed_cooperations: number of consecutive failed cooperation
441 * chances after which weight-raising is restored
442 * to a queue subject to more than bfq_coop_thresh
443 * queue merges.
444 * @bfq_requests_within_timer: number of consecutive requests that must be
445 * issued within the idle time slice to set
446 * again idling to a queue which was marked as
447 * non-I/O-bound (see the definition of the
448 * IO_bound flag for further details).
449 * @last_ins_in_burst: last time at which a queue entered the current
450 * burst of queues being activated shortly after
451 * each other; for more details about this and the
452 * following parameters related to a burst of
453 * activations, see the comments to the function
454 * @bfq_handle_burst.
455 * @bfq_burst_interval: reference time interval used to decide whether a
456 * queue has been activated shortly after
457 * @last_ins_in_burst.
458 * @burst_size: number of queues in the current burst of queue activations.
459 * @bfq_large_burst_thresh: maximum burst size above which the current
460 * queue-activation burst is deemed as 'large'.
461 * @large_burst: true if a large queue-activation burst is in progress.
462 * @burst_list: head of the burst list (as for the above fields, more details
463 * in the comments to the function bfq_handle_burst).
464 * @low_latency: if set to true, low-latency heuristics are enabled.
465 * @bfq_wr_coeff: maximum factor by which the weight of a weight-raised
466 * queue is multiplied.
467 * @bfq_wr_max_time: maximum duration of a weight-raising period (jiffies).
468 * @bfq_wr_rt_max_time: maximum duration for soft real-time processes.
469 * @bfq_wr_min_idle_time: minimum idle period after which weight-raising
470 * may be reactivated for a queue (in jiffies).
471 * @bfq_wr_min_inter_arr_async: minimum period between request arrivals
472 * after which weight-raising may be
473 * reactivated for an already busy queue
474 * (in jiffies).
475 * @bfq_wr_max_softrt_rate: max service-rate for a soft real-time queue,
476 * sectors per seconds.
477 * @RT_prod: cached value of the product R*T used for computing the maximum
478 * duration of the weight raising automatically.
479 * @device_speed: device-speed class for the low-latency heuristic.
480 * @oom_bfqq: fallback dummy bfqq for extreme OOM conditions.
481 *
482 * All the fields are protected by the @queue lock.
483 */
484struct bfq_data {
485 struct request_queue *queue;
486
487 struct bfq_group *root_group;
488 struct rb_root rq_pos_tree;
489
490#ifdef CONFIG_CGROUP_BFQIO
491 int active_numerous_groups;
492#endif
493
494 struct rb_root queue_weights_tree;
495 struct rb_root group_weights_tree;
496
497 int busy_queues;
498 int busy_in_flight_queues;
499 int const_seeky_busy_in_flight_queues;
500 int wr_busy_queues;
501 int queued;
502 int rq_in_driver;
503 int sync_flight;
504
505 int max_rq_in_driver;
506 int hw_tag_samples;
507 int hw_tag;
508
509 int budgets_assigned;
510
511 struct timer_list idle_slice_timer;
512 struct work_struct unplug_work;
513
514 struct bfq_queue *in_service_queue;
515 struct bfq_io_cq *in_service_bic;
516
517 sector_t last_position;
518
519 ktime_t last_budget_start;
520 ktime_t last_idling_start;
521 int peak_rate_samples;
522 u64 peak_rate;
523 unsigned long bfq_max_budget;
524
525 struct hlist_head group_list;
526 struct list_head active_list;
527 struct list_head idle_list;
528
529 unsigned int bfq_fifo_expire[2];
530 unsigned int bfq_back_penalty;
531 unsigned int bfq_back_max;
532 unsigned int bfq_slice_idle;
533 u64 bfq_class_idle_last_service;
534
535 unsigned int bfq_user_max_budget;
536 unsigned int bfq_max_budget_async_rq;
537 unsigned int bfq_timeout[2];
538
539 unsigned int bfq_coop_thresh;
540 unsigned int bfq_failed_cooperations;
541 unsigned int bfq_requests_within_timer;
542
543 unsigned long last_ins_in_burst;
544 unsigned long bfq_burst_interval;
545 int burst_size;
546 unsigned long bfq_large_burst_thresh;
547 bool large_burst;
548 struct hlist_head burst_list;
549
550 bool low_latency;
551
552 /* parameters of the low_latency heuristics */
553 unsigned int bfq_wr_coeff;
554 unsigned int bfq_wr_max_time;
555 unsigned int bfq_wr_rt_max_time;
556 unsigned int bfq_wr_min_idle_time;
557 unsigned long bfq_wr_min_inter_arr_async;
558 unsigned int bfq_wr_max_softrt_rate;
559 u64 RT_prod;
560 enum bfq_device_speed device_speed;
561
562 struct bfq_queue oom_bfqq;
563};
564
565enum bfqq_state_flags {
566 BFQ_BFQQ_FLAG_busy = 0, /* has requests or is in service */
567 BFQ_BFQQ_FLAG_wait_request, /* waiting for a request */
568 BFQ_BFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
569 BFQ_BFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
570 BFQ_BFQQ_FLAG_idle_window, /* slice idling enabled */
571 BFQ_BFQQ_FLAG_sync, /* synchronous queue */
572 BFQ_BFQQ_FLAG_budget_new, /* no completion with this budget */
Mauro Andreolini0b335882015-06-07 02:34:22 +0200573 BFQ_BFQQ_FLAG_IO_bound, /*
Paolo Valente70f28712013-05-09 19:10:02 +0200574 * bfqq has timed-out at least once
575 * having consumed at most 2/10 of
576 * its budget
577 */
578 BFQ_BFQQ_FLAG_in_large_burst, /*
579 * bfqq activated in a large burst,
580 * see comments to bfq_handle_burst.
581 */
582 BFQ_BFQQ_FLAG_constantly_seeky, /*
583 * bfqq has proved to be slow and
584 * seeky until budget timeout
585 */
Mauro Andreolini0b335882015-06-07 02:34:22 +0200586 BFQ_BFQQ_FLAG_softrt_update, /*
Paolo Valente70f28712013-05-09 19:10:02 +0200587 * may need softrt-next-start
588 * update
589 */
590 BFQ_BFQQ_FLAG_coop, /* bfqq is shared */
Mauro Andreolini0b335882015-06-07 02:34:22 +0200591 BFQ_BFQQ_FLAG_split_coop, /* shared bfqq will be split */
592 BFQ_BFQQ_FLAG_just_split, /* queue has just been split */
Paolo Valente70f28712013-05-09 19:10:02 +0200593};
594
595#define BFQ_BFQQ_FNS(name) \
596static inline void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
597{ \
598 (bfqq)->flags |= (1 << BFQ_BFQQ_FLAG_##name); \
599} \
600static inline void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
601{ \
602 (bfqq)->flags &= ~(1 << BFQ_BFQQ_FLAG_##name); \
603} \
604static inline int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
605{ \
606 return ((bfqq)->flags & (1 << BFQ_BFQQ_FLAG_##name)) != 0; \
607}
608
609BFQ_BFQQ_FNS(busy);
610BFQ_BFQQ_FNS(wait_request);
611BFQ_BFQQ_FNS(must_alloc);
612BFQ_BFQQ_FNS(fifo_expire);
613BFQ_BFQQ_FNS(idle_window);
614BFQ_BFQQ_FNS(sync);
615BFQ_BFQQ_FNS(budget_new);
616BFQ_BFQQ_FNS(IO_bound);
617BFQ_BFQQ_FNS(in_large_burst);
618BFQ_BFQQ_FNS(constantly_seeky);
619BFQ_BFQQ_FNS(coop);
620BFQ_BFQQ_FNS(split_coop);
Mauro Andreolini0b335882015-06-07 02:34:22 +0200621BFQ_BFQQ_FNS(just_split);
Paolo Valente70f28712013-05-09 19:10:02 +0200622BFQ_BFQQ_FNS(softrt_update);
623#undef BFQ_BFQQ_FNS
624
625/* Logging facilities. */
626#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
627 blk_add_trace_msg((bfqd)->queue, "bfq%d " fmt, (bfqq)->pid, ##args)
628
629#define bfq_log(bfqd, fmt, args...) \
630 blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
631
632/* Expiration reasons. */
633enum bfqq_expiration {
634 BFQ_BFQQ_TOO_IDLE = 0, /*
635 * queue has been idling for
636 * too long
637 */
638 BFQ_BFQQ_BUDGET_TIMEOUT, /* budget took too long to be used */
639 BFQ_BFQQ_BUDGET_EXHAUSTED, /* budget consumed */
640 BFQ_BFQQ_NO_MORE_REQUESTS, /* the queue has no more requests */
641};
642
643#ifdef CONFIG_CGROUP_BFQIO
644/**
645 * struct bfq_group - per (device, cgroup) data structure.
646 * @entity: schedulable entity to insert into the parent group sched_data.
647 * @sched_data: own sched_data, to contain child entities (they may be
648 * both bfq_queues and bfq_groups).
649 * @group_node: node to be inserted into the bfqio_cgroup->group_data
650 * list of the containing cgroup's bfqio_cgroup.
651 * @bfqd_node: node to be inserted into the @bfqd->group_list list
652 * of the groups active on the same device; used for cleanup.
653 * @bfqd: the bfq_data for the device this group acts upon.
654 * @async_bfqq: array of async queues for all the tasks belonging to
655 * the group, one queue per ioprio value per ioprio_class,
656 * except for the idle class that has only one queue.
657 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
658 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
659 * to avoid too many special cases during group creation/
660 * migration.
661 * @active_entities: number of active entities belonging to the group;
662 * unused for the root group. Used to know whether there
663 * are groups with more than one active @bfq_entity
664 * (see the comments to the function
665 * bfq_bfqq_must_not_expire()).
666 *
667 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
668 * there is a set of bfq_groups, each one collecting the lower-level
669 * entities belonging to the group that are acting on the same device.
670 *
671 * Locking works as follows:
672 * o @group_node is protected by the bfqio_cgroup lock, and is accessed
673 * via RCU from its readers.
674 * o @bfqd is protected by the queue lock, RCU is used to access it
675 * from the readers.
676 * o All the other fields are protected by the @bfqd queue lock.
677 */
678struct bfq_group {
679 struct bfq_entity entity;
680 struct bfq_sched_data sched_data;
681
682 struct hlist_node group_node;
683 struct hlist_node bfqd_node;
684
685 void *bfqd;
686
687 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
688 struct bfq_queue *async_idle_bfqq;
689
690 struct bfq_entity *my_entity;
691
692 int active_entities;
693};
694
695/**
696 * struct bfqio_cgroup - bfq cgroup data structure.
697 * @css: subsystem state for bfq in the containing cgroup.
698 * @weight: cgroup weight.
699 * @ioprio: cgroup ioprio.
700 * @ioprio_class: cgroup ioprio_class.
701 * @lock: spinlock that protects @ioprio, @ioprio_class and @group_data.
702 * @group_data: list containing the bfq_group belonging to this cgroup.
703 *
704 * @group_data is accessed using RCU, with @lock protecting the updates,
705 * @ioprio and @ioprio_class are protected by @lock.
706 */
707struct bfqio_cgroup {
708 struct cgroup_subsys_state css;
709
710 unsigned short weight, ioprio, ioprio_class;
711
712 spinlock_t lock;
713 struct hlist_head group_data;
714};
715#else
716struct bfq_group {
717 struct bfq_sched_data sched_data;
718
719 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
720 struct bfq_queue *async_idle_bfqq;
721};
722#endif
723
724static inline struct bfq_service_tree *
725bfq_entity_service_tree(struct bfq_entity *entity)
726{
727 struct bfq_sched_data *sched_data = entity->sched_data;
728 unsigned int idx = entity->ioprio_class - 1;
729
730 BUG_ON(idx >= BFQ_IOPRIO_CLASSES);
731 BUG_ON(sched_data == NULL);
732
733 return sched_data->service_tree + idx;
734}
735
736static inline struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic,
737 bool is_sync)
738{
739 return bic->bfqq[is_sync];
740}
741
742static inline void bic_set_bfqq(struct bfq_io_cq *bic,
743 struct bfq_queue *bfqq, bool is_sync)
744{
745 bic->bfqq[is_sync] = bfqq;
746}
747
748static inline struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
749{
750 return bic->icq.q->elevator->elevator_data;
751}
752
753/**
754 * bfq_get_bfqd_locked - get a lock to a bfqd using a RCU protected pointer.
755 * @ptr: a pointer to a bfqd.
756 * @flags: storage for the flags to be saved.
757 *
758 * This function allows bfqg->bfqd to be protected by the
759 * queue lock of the bfqd they reference; the pointer is dereferenced
760 * under RCU, so the storage for bfqd is assured to be safe as long
761 * as the RCU read side critical section does not end. After the
762 * bfqd->queue->queue_lock is taken the pointer is rechecked, to be
763 * sure that no other writer accessed it. If we raced with a writer,
764 * the function returns NULL, with the queue unlocked, otherwise it
765 * returns the dereferenced pointer, with the queue locked.
766 */
767static inline struct bfq_data *bfq_get_bfqd_locked(void **ptr,
768 unsigned long *flags)
769{
770 struct bfq_data *bfqd;
771
772 rcu_read_lock();
773 bfqd = rcu_dereference(*(struct bfq_data **)ptr);
774
775 if (bfqd != NULL) {
776 spin_lock_irqsave(bfqd->queue->queue_lock, *flags);
777 if (*ptr == bfqd)
778 goto out;
779 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
780 }
781
782 bfqd = NULL;
783out:
784 rcu_read_unlock();
785 return bfqd;
786}
787
788static inline void bfq_put_bfqd_unlock(struct bfq_data *bfqd,
789 unsigned long *flags)
790{
791 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
792}
793
794static void bfq_check_ioprio_change(struct io_context *ioc,
795 struct bfq_io_cq *bic);
796static void bfq_put_queue(struct bfq_queue *bfqq);
797static void bfq_dispatch_insert(struct request_queue *q, struct request *rq);
798static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
799 struct bfq_group *bfqg, int is_sync,
800 struct io_context *ioc, gfp_t gfp_mask);
801static void bfq_end_wr_async_queues(struct bfq_data *bfqd,
802 struct bfq_group *bfqg);
803static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
804static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
805
806#endif /* _BFQ_H */