blob: 89078fc0e112b3f03953d1cfbc16086b8b0800e3 [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
221 * bfq_bfqq_softrt_next_start()).
222 * @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
227 *
228 * A bfq_queue is a leaf request queue; it can be associated with an io_context
229 * or more, if it is async or shared between cooperating processes. @cgroup
230 * holds a reference to the cgroup, to be sure that it does not disappear while
231 * a bfqq still references it (mostly to avoid races between request issuing and
232 * task migration followed by cgroup destruction).
233 * All the fields are protected by the queue lock of the containing bfqd.
234 */
235struct bfq_queue {
236 atomic_t ref;
237 struct bfq_data *bfqd;
238
239 /* fields for cooperating queues handling */
240 struct bfq_queue *new_bfqq;
241 struct rb_node pos_node;
242 struct rb_root *pos_root;
243
244 struct rb_root sort_list;
245 struct request *next_rq;
246 int queued[2];
247 int allocated[2];
248 int meta_pending;
249 struct list_head fifo;
250
251 struct bfq_entity entity;
252
253 unsigned long max_budget;
254 unsigned long budget_timeout;
255
256 int dispatched;
257
258 unsigned int flags;
259
260 struct list_head bfqq_list;
261
262 struct hlist_node burst_list_node;
263
264 unsigned int seek_samples;
265 u64 seek_total;
266 sector_t seek_mean;
267 sector_t last_request_pos;
268
269 unsigned int requests_within_timer;
270
271 pid_t pid;
272
273 /* weight-raising fields */
274 unsigned long wr_cur_max_time;
275 unsigned long soft_rt_next_start;
276 unsigned long last_wr_start_finish;
277 unsigned int wr_coeff;
278 unsigned long last_idle_bklogged;
279 unsigned long service_from_backlogged;
280};
281
282/**
283 * struct bfq_ttime - per process thinktime stats.
284 * @ttime_total: total process thinktime
285 * @ttime_samples: number of thinktime samples
286 * @ttime_mean: average process thinktime
287 */
288struct bfq_ttime {
289 unsigned long last_end_request;
290
291 unsigned long ttime_total;
292 unsigned long ttime_samples;
293 unsigned long ttime_mean;
294};
295
296/**
297 * struct bfq_io_cq - per (request_queue, io_context) structure.
298 * @icq: associated io_cq structure
299 * @bfqq: array of two process queues, the sync and the async
300 * @ttime: associated @bfq_ttime struct
301 */
302struct bfq_io_cq {
303 struct io_cq icq; /* must be the first member */
304 struct bfq_queue *bfqq[2];
305 struct bfq_ttime ttime;
306 int ioprio;
307};
308
309enum bfq_device_speed {
310 BFQ_BFQD_FAST,
311 BFQ_BFQD_SLOW,
312};
313
314/**
315 * struct bfq_data - per device data structure.
316 * @queue: request queue for the managed device.
317 * @root_group: root bfq_group for the device.
318 * @rq_pos_tree: rbtree sorted by next_request position, used when
319 * determining if two or more queues have interleaving
320 * requests (see bfq_close_cooperator()).
321 * @active_numerous_groups: number of bfq_groups containing more than one
322 * active @bfq_entity.
323 * @queue_weights_tree: rbtree of weight counters of @bfq_queues, sorted by
324 * weight. Used to keep track of whether all @bfq_queues
325 * have the same weight. The tree contains one counter
326 * for each distinct weight associated to some active
327 * and not weight-raised @bfq_queue (see the comments to
328 * the functions bfq_weights_tree_[add|remove] for
329 * further details).
330 * @group_weights_tree: rbtree of non-queue @bfq_entity weight counters, sorted
331 * by weight. Used to keep track of whether all
332 * @bfq_groups have the same weight. The tree contains
333 * one counter for each distinct weight associated to
334 * some active @bfq_group (see the comments to the
335 * functions bfq_weights_tree_[add|remove] for further
336 * details).
337 * @busy_queues: number of bfq_queues containing requests (including the
338 * queue in service, even if it is idling).
339 * @busy_in_flight_queues: number of @bfq_queues containing pending or
340 * in-flight requests, plus the @bfq_queue in
341 * service, even if idle but waiting for the
342 * possible arrival of its next sync request. This
343 * field is updated only if the device is rotational,
344 * but used only if the device is also NCQ-capable.
345 * The reason why the field is updated also for non-
346 * NCQ-capable rotational devices is related to the
347 * fact that the value of @hw_tag may be set also
348 * later than when busy_in_flight_queues may need to
349 * be incremented for the first time(s). Taking also
350 * this possibility into account, to avoid unbalanced
351 * increments/decrements, would imply more overhead
352 * than just updating busy_in_flight_queues
353 * regardless of the value of @hw_tag.
354 * @const_seeky_busy_in_flight_queues: number of constantly-seeky @bfq_queues
355 * (that is, seeky queues that expired
356 * for budget timeout at least once)
357 * containing pending or in-flight
358 * requests, including the in-service
359 * @bfq_queue if constantly seeky. This
360 * field is updated only if the device
361 * is rotational, but used only if the
362 * device is also NCQ-capable (see the
363 * comments to @busy_in_flight_queues).
364 * @wr_busy_queues: number of weight-raised busy @bfq_queues.
365 * @queued: number of queued requests.
366 * @rq_in_driver: number of requests dispatched and waiting for completion.
367 * @sync_flight: number of sync requests in the driver.
368 * @max_rq_in_driver: max number of reqs in driver in the last
369 * @hw_tag_samples completed requests.
370 * @hw_tag_samples: nr of samples used to calculate hw_tag.
371 * @hw_tag: flag set to one if the driver is showing a queueing behavior.
372 * @budgets_assigned: number of budgets assigned.
373 * @idle_slice_timer: timer set when idling for the next sequential request
374 * from the queue in service.
375 * @unplug_work: delayed work to restart dispatching on the request queue.
376 * @in_service_queue: bfq_queue in service.
377 * @in_service_bic: bfq_io_cq (bic) associated with the @in_service_queue.
378 * @last_position: on-disk position of the last served request.
379 * @last_budget_start: beginning of the last budget.
380 * @last_idling_start: beginning of the last idle slice.
381 * @peak_rate: peak transfer rate observed for a budget.
382 * @peak_rate_samples: number of samples used to calculate @peak_rate.
383 * @bfq_max_budget: maximum budget allotted to a bfq_queue before
384 * rescheduling.
385 * @group_list: list of all the bfq_groups active on the device.
386 * @active_list: list of all the bfq_queues active on the device.
387 * @idle_list: list of all the bfq_queues idle on the device.
388 * @bfq_fifo_expire: timeout for async/sync requests; when it expires
389 * requests are served in fifo order.
390 * @bfq_back_penalty: weight of backward seeks wrt forward ones.
391 * @bfq_back_max: maximum allowed backward seek.
392 * @bfq_slice_idle: maximum idling time.
393 * @bfq_user_max_budget: user-configured max budget value
394 * (0 for auto-tuning).
395 * @bfq_max_budget_async_rq: maximum budget (in nr of requests) allotted to
396 * async queues.
397 * @bfq_timeout: timeout for bfq_queues to consume their budget; used to
398 * to prevent seeky queues to impose long latencies to well
399 * behaved ones (this also implies that seeky queues cannot
400 * receive guarantees in the service domain; after a timeout
401 * they are charged for the whole allocated budget, to try
402 * to preserve a behavior reasonably fair among them, but
403 * without service-domain guarantees).
404 * @bfq_coop_thresh: number of queue merges after which a @bfq_queue is
405 * no more granted any weight-raising.
406 * @bfq_failed_cooperations: number of consecutive failed cooperation
407 * chances after which weight-raising is restored
408 * to a queue subject to more than bfq_coop_thresh
409 * queue merges.
410 * @bfq_requests_within_timer: number of consecutive requests that must be
411 * issued within the idle time slice to set
412 * again idling to a queue which was marked as
413 * non-I/O-bound (see the definition of the
414 * IO_bound flag for further details).
415 * @last_ins_in_burst: last time at which a queue entered the current
416 * burst of queues being activated shortly after
417 * each other; for more details about this and the
418 * following parameters related to a burst of
419 * activations, see the comments to the function
420 * @bfq_handle_burst.
421 * @bfq_burst_interval: reference time interval used to decide whether a
422 * queue has been activated shortly after
423 * @last_ins_in_burst.
424 * @burst_size: number of queues in the current burst of queue activations.
425 * @bfq_large_burst_thresh: maximum burst size above which the current
426 * queue-activation burst is deemed as 'large'.
427 * @large_burst: true if a large queue-activation burst is in progress.
428 * @burst_list: head of the burst list (as for the above fields, more details
429 * in the comments to the function bfq_handle_burst).
430 * @low_latency: if set to true, low-latency heuristics are enabled.
431 * @bfq_wr_coeff: maximum factor by which the weight of a weight-raised
432 * queue is multiplied.
433 * @bfq_wr_max_time: maximum duration of a weight-raising period (jiffies).
434 * @bfq_wr_rt_max_time: maximum duration for soft real-time processes.
435 * @bfq_wr_min_idle_time: minimum idle period after which weight-raising
436 * may be reactivated for a queue (in jiffies).
437 * @bfq_wr_min_inter_arr_async: minimum period between request arrivals
438 * after which weight-raising may be
439 * reactivated for an already busy queue
440 * (in jiffies).
441 * @bfq_wr_max_softrt_rate: max service-rate for a soft real-time queue,
442 * sectors per seconds.
443 * @RT_prod: cached value of the product R*T used for computing the maximum
444 * duration of the weight raising automatically.
445 * @device_speed: device-speed class for the low-latency heuristic.
446 * @oom_bfqq: fallback dummy bfqq for extreme OOM conditions.
447 *
448 * All the fields are protected by the @queue lock.
449 */
450struct bfq_data {
451 struct request_queue *queue;
452
453 struct bfq_group *root_group;
454 struct rb_root rq_pos_tree;
455
456#ifdef CONFIG_CGROUP_BFQIO
457 int active_numerous_groups;
458#endif
459
460 struct rb_root queue_weights_tree;
461 struct rb_root group_weights_tree;
462
463 int busy_queues;
464 int busy_in_flight_queues;
465 int const_seeky_busy_in_flight_queues;
466 int wr_busy_queues;
467 int queued;
468 int rq_in_driver;
469 int sync_flight;
470
471 int max_rq_in_driver;
472 int hw_tag_samples;
473 int hw_tag;
474
475 int budgets_assigned;
476
477 struct timer_list idle_slice_timer;
478 struct work_struct unplug_work;
479
480 struct bfq_queue *in_service_queue;
481 struct bfq_io_cq *in_service_bic;
482
483 sector_t last_position;
484
485 ktime_t last_budget_start;
486 ktime_t last_idling_start;
487 int peak_rate_samples;
488 u64 peak_rate;
489 unsigned long bfq_max_budget;
490
491 struct hlist_head group_list;
492 struct list_head active_list;
493 struct list_head idle_list;
494
495 unsigned int bfq_fifo_expire[2];
496 unsigned int bfq_back_penalty;
497 unsigned int bfq_back_max;
498 unsigned int bfq_slice_idle;
499 u64 bfq_class_idle_last_service;
500
501 unsigned int bfq_user_max_budget;
502 unsigned int bfq_max_budget_async_rq;
503 unsigned int bfq_timeout[2];
504
505 unsigned int bfq_coop_thresh;
506 unsigned int bfq_failed_cooperations;
507 unsigned int bfq_requests_within_timer;
508
509 unsigned long last_ins_in_burst;
510 unsigned long bfq_burst_interval;
511 int burst_size;
512 unsigned long bfq_large_burst_thresh;
513 bool large_burst;
514 struct hlist_head burst_list;
515
516 bool low_latency;
517
518 /* parameters of the low_latency heuristics */
519 unsigned int bfq_wr_coeff;
520 unsigned int bfq_wr_max_time;
521 unsigned int bfq_wr_rt_max_time;
522 unsigned int bfq_wr_min_idle_time;
523 unsigned long bfq_wr_min_inter_arr_async;
524 unsigned int bfq_wr_max_softrt_rate;
525 u64 RT_prod;
526 enum bfq_device_speed device_speed;
527
528 struct bfq_queue oom_bfqq;
529};
530
531enum bfqq_state_flags {
532 BFQ_BFQQ_FLAG_busy = 0, /* has requests or is in service */
533 BFQ_BFQQ_FLAG_wait_request, /* waiting for a request */
534 BFQ_BFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
535 BFQ_BFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
536 BFQ_BFQQ_FLAG_idle_window, /* slice idling enabled */
537 BFQ_BFQQ_FLAG_sync, /* synchronous queue */
538 BFQ_BFQQ_FLAG_budget_new, /* no completion with this budget */
539 BFQ_BFQQ_FLAG_IO_bound, /*
540 * bfqq has timed-out at least once
541 * having consumed at most 2/10 of
542 * its budget
543 */
544 BFQ_BFQQ_FLAG_in_large_burst, /*
545 * bfqq activated in a large burst,
546 * see comments to bfq_handle_burst.
547 */
548 BFQ_BFQQ_FLAG_constantly_seeky, /*
549 * bfqq has proved to be slow and
550 * seeky until budget timeout
551 */
552 BFQ_BFQQ_FLAG_softrt_update, /*
553 * may need softrt-next-start
554 * update
555 */
556 BFQ_BFQQ_FLAG_coop, /* bfqq is shared */
557 BFQ_BFQQ_FLAG_split_coop, /* shared bfqq will be splitted */
558};
559
560#define BFQ_BFQQ_FNS(name) \
561static inline void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
562{ \
563 (bfqq)->flags |= (1 << BFQ_BFQQ_FLAG_##name); \
564} \
565static inline void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
566{ \
567 (bfqq)->flags &= ~(1 << BFQ_BFQQ_FLAG_##name); \
568} \
569static inline int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
570{ \
571 return ((bfqq)->flags & (1 << BFQ_BFQQ_FLAG_##name)) != 0; \
572}
573
574BFQ_BFQQ_FNS(busy);
575BFQ_BFQQ_FNS(wait_request);
576BFQ_BFQQ_FNS(must_alloc);
577BFQ_BFQQ_FNS(fifo_expire);
578BFQ_BFQQ_FNS(idle_window);
579BFQ_BFQQ_FNS(sync);
580BFQ_BFQQ_FNS(budget_new);
581BFQ_BFQQ_FNS(IO_bound);
582BFQ_BFQQ_FNS(in_large_burst);
583BFQ_BFQQ_FNS(constantly_seeky);
584BFQ_BFQQ_FNS(coop);
585BFQ_BFQQ_FNS(split_coop);
586BFQ_BFQQ_FNS(softrt_update);
587#undef BFQ_BFQQ_FNS
588
589/* Logging facilities. */
590#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
591 blk_add_trace_msg((bfqd)->queue, "bfq%d " fmt, (bfqq)->pid, ##args)
592
593#define bfq_log(bfqd, fmt, args...) \
594 blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
595
596/* Expiration reasons. */
597enum bfqq_expiration {
598 BFQ_BFQQ_TOO_IDLE = 0, /*
599 * queue has been idling for
600 * too long
601 */
602 BFQ_BFQQ_BUDGET_TIMEOUT, /* budget took too long to be used */
603 BFQ_BFQQ_BUDGET_EXHAUSTED, /* budget consumed */
604 BFQ_BFQQ_NO_MORE_REQUESTS, /* the queue has no more requests */
605};
606
607#ifdef CONFIG_CGROUP_BFQIO
608/**
609 * struct bfq_group - per (device, cgroup) data structure.
610 * @entity: schedulable entity to insert into the parent group sched_data.
611 * @sched_data: own sched_data, to contain child entities (they may be
612 * both bfq_queues and bfq_groups).
613 * @group_node: node to be inserted into the bfqio_cgroup->group_data
614 * list of the containing cgroup's bfqio_cgroup.
615 * @bfqd_node: node to be inserted into the @bfqd->group_list list
616 * of the groups active on the same device; used for cleanup.
617 * @bfqd: the bfq_data for the device this group acts upon.
618 * @async_bfqq: array of async queues for all the tasks belonging to
619 * the group, one queue per ioprio value per ioprio_class,
620 * except for the idle class that has only one queue.
621 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
622 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
623 * to avoid too many special cases during group creation/
624 * migration.
625 * @active_entities: number of active entities belonging to the group;
626 * unused for the root group. Used to know whether there
627 * are groups with more than one active @bfq_entity
628 * (see the comments to the function
629 * bfq_bfqq_must_not_expire()).
630 *
631 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
632 * there is a set of bfq_groups, each one collecting the lower-level
633 * entities belonging to the group that are acting on the same device.
634 *
635 * Locking works as follows:
636 * o @group_node is protected by the bfqio_cgroup lock, and is accessed
637 * via RCU from its readers.
638 * o @bfqd is protected by the queue lock, RCU is used to access it
639 * from the readers.
640 * o All the other fields are protected by the @bfqd queue lock.
641 */
642struct bfq_group {
643 struct bfq_entity entity;
644 struct bfq_sched_data sched_data;
645
646 struct hlist_node group_node;
647 struct hlist_node bfqd_node;
648
649 void *bfqd;
650
651 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
652 struct bfq_queue *async_idle_bfqq;
653
654 struct bfq_entity *my_entity;
655
656 int active_entities;
657};
658
659/**
660 * struct bfqio_cgroup - bfq cgroup data structure.
661 * @css: subsystem state for bfq in the containing cgroup.
662 * @weight: cgroup weight.
663 * @ioprio: cgroup ioprio.
664 * @ioprio_class: cgroup ioprio_class.
665 * @lock: spinlock that protects @ioprio, @ioprio_class and @group_data.
666 * @group_data: list containing the bfq_group belonging to this cgroup.
667 *
668 * @group_data is accessed using RCU, with @lock protecting the updates,
669 * @ioprio and @ioprio_class are protected by @lock.
670 */
671struct bfqio_cgroup {
672 struct cgroup_subsys_state css;
673
674 unsigned short weight, ioprio, ioprio_class;
675
676 spinlock_t lock;
677 struct hlist_head group_data;
678};
679#else
680struct bfq_group {
681 struct bfq_sched_data sched_data;
682
683 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
684 struct bfq_queue *async_idle_bfqq;
685};
686#endif
687
688static inline struct bfq_service_tree *
689bfq_entity_service_tree(struct bfq_entity *entity)
690{
691 struct bfq_sched_data *sched_data = entity->sched_data;
692 unsigned int idx = entity->ioprio_class - 1;
693
694 BUG_ON(idx >= BFQ_IOPRIO_CLASSES);
695 BUG_ON(sched_data == NULL);
696
697 return sched_data->service_tree + idx;
698}
699
700static inline struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic,
701 bool is_sync)
702{
703 return bic->bfqq[is_sync];
704}
705
706static inline void bic_set_bfqq(struct bfq_io_cq *bic,
707 struct bfq_queue *bfqq, bool is_sync)
708{
709 bic->bfqq[is_sync] = bfqq;
710}
711
712static inline struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
713{
714 return bic->icq.q->elevator->elevator_data;
715}
716
717/**
718 * bfq_get_bfqd_locked - get a lock to a bfqd using a RCU protected pointer.
719 * @ptr: a pointer to a bfqd.
720 * @flags: storage for the flags to be saved.
721 *
722 * This function allows bfqg->bfqd to be protected by the
723 * queue lock of the bfqd they reference; the pointer is dereferenced
724 * under RCU, so the storage for bfqd is assured to be safe as long
725 * as the RCU read side critical section does not end. After the
726 * bfqd->queue->queue_lock is taken the pointer is rechecked, to be
727 * sure that no other writer accessed it. If we raced with a writer,
728 * the function returns NULL, with the queue unlocked, otherwise it
729 * returns the dereferenced pointer, with the queue locked.
730 */
731static inline struct bfq_data *bfq_get_bfqd_locked(void **ptr,
732 unsigned long *flags)
733{
734 struct bfq_data *bfqd;
735
736 rcu_read_lock();
737 bfqd = rcu_dereference(*(struct bfq_data **)ptr);
738
739 if (bfqd != NULL) {
740 spin_lock_irqsave(bfqd->queue->queue_lock, *flags);
741 if (*ptr == bfqd)
742 goto out;
743 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
744 }
745
746 bfqd = NULL;
747out:
748 rcu_read_unlock();
749 return bfqd;
750}
751
752static inline void bfq_put_bfqd_unlock(struct bfq_data *bfqd,
753 unsigned long *flags)
754{
755 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
756}
757
758static void bfq_check_ioprio_change(struct io_context *ioc,
759 struct bfq_io_cq *bic);
760static void bfq_put_queue(struct bfq_queue *bfqq);
761static void bfq_dispatch_insert(struct request_queue *q, struct request *rq);
762static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
763 struct bfq_group *bfqg, int is_sync,
764 struct io_context *ioc, gfp_t gfp_mask);
765static void bfq_end_wr_async_queues(struct bfq_data *bfqd,
766 struct bfq_group *bfqg);
767static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
768static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
769
770#endif /* _BFQ_H */