Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / block / bfq.h
blobd5b82f0231fed55dcfc68a75841d23c69df00058
1 /*
2 * BFQ-v3r3 for 3.3.0: data structures and common functions prototypes.
4 * Based on ideas and code from CFQ:
5 * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
7 * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
8 * Paolo Valente <paolo.valente@unimore.it>
9 */
11 #ifndef _BFQ_H
12 #define _BFQ_H
14 #include <linux/blktrace_api.h>
15 #include <linux/hrtimer.h>
16 #include <linux/ioprio.h>
17 #include <linux/rbtree.h>
19 #define BFQ_IOPRIO_CLASSES 3
20 #define BFQ_CL_IDLE_TIMEOUT HZ/5
22 #define BFQ_MIN_WEIGHT 1
23 #define BFQ_MAX_WEIGHT 1000
25 #define BFQ_DEFAULT_GRP_WEIGHT 10
26 #define BFQ_DEFAULT_GRP_IOPRIO 0
27 #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE
29 struct bfq_entity;
31 /**
32 * struct bfq_service_tree - per ioprio_class service tree.
33 * @active: tree for active entities (i.e., those backlogged).
34 * @idle: tree for idle entities (i.e., those not backlogged, with V <= F_i).
35 * @first_idle: idle entity with minimum F_i.
36 * @last_idle: idle entity with maximum F_i.
37 * @vtime: scheduler virtual time.
38 * @wsum: scheduler weight sum; active and idle entities contribute to it.
40 * Each service tree represents a B-WF2Q+ scheduler on its own. Each
41 * ioprio_class has its own independent scheduler, and so its own
42 * bfq_service_tree. All the fields are protected by the queue lock
43 * of the containing bfqd.
45 struct bfq_service_tree {
46 struct rb_root active;
47 struct rb_root idle;
49 struct bfq_entity *first_idle;
50 struct bfq_entity *last_idle;
52 u64 vtime;
53 unsigned long wsum;
56 /**
57 * struct bfq_sched_data - multi-class scheduler.
58 * @active_entity: entity under service.
59 * @next_active: head-of-the-line entity in the scheduler.
60 * @service_tree: array of service trees, one per ioprio_class.
62 * bfq_sched_data is the basic scheduler queue. It supports three
63 * ioprio_classes, and can be used either as a toplevel queue or as
64 * an intermediate queue on a hierarchical setup.
65 * @next_active points to the active entity of the sched_data service
66 * trees that will be scheduled next.
68 * The supported ioprio_classes are the same as in CFQ, in descending
69 * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE.
70 * Requests from higher priority queues are served before all the
71 * requests from lower priority queues; among requests of the same
72 * queue requests are served according to B-WF2Q+.
73 * All the fields are protected by the queue lock of the containing bfqd.
75 struct bfq_sched_data {
76 struct bfq_entity *active_entity;
77 struct bfq_entity *next_active;
78 struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES];
81 /**
82 * struct bfq_entity - schedulable entity.
83 * @rb_node: service_tree member.
84 * @on_st: flag, true if the entity is on a tree (either the active or
85 * the idle one of its service_tree).
86 * @finish: B-WF2Q+ finish timestamp (aka F_i).
87 * @start: B-WF2Q+ start timestamp (aka S_i).
88 * @tree: tree the entity is enqueued into; %NULL if not on a tree.
89 * @min_start: minimum start time of the (active) subtree rooted at
90 * this entity; used for O(log N) lookups into active trees.
91 * @service: service received during the last round of service.
92 * @budget: budget used to calculate F_i; F_i = S_i + @budget / @weight.
93 * @weight: weight of the queue
94 * @parent: parent entity, for hierarchical scheduling.
95 * @my_sched_data: for non-leaf nodes in the cgroup hierarchy, the
96 * associated scheduler queue, %NULL on leaf nodes.
97 * @sched_data: the scheduler queue this entity belongs to.
98 * @ioprio: the ioprio in use.
99 * @new_weight: when a weight change is requested, the new weight value.
100 * @orig_weight: original weight, used to implement weight boosting
101 * @new_ioprio: when an ioprio change is requested, the new ioprio value.
102 * @ioprio_class: the ioprio_class in use.
103 * @new_ioprio_class: when an ioprio_class change is requested, the new
104 * ioprio_class value.
105 * @ioprio_changed: flag, true when the user requested a weight, ioprio or
106 * ioprio_class change.
108 * A bfq_entity is used to represent either a bfq_queue (leaf node in the
109 * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each
110 * entity belongs to the sched_data of the parent group in the cgroup
111 * hierarchy. Non-leaf entities have also their own sched_data, stored
112 * in @my_sched_data.
114 * Each entity stores independently its priority values; this would
115 * allow different weights on different devices, but this
116 * functionality is not exported to userspace by now. Priorities and
117 * weights are updated lazily, first storing the new values into the
118 * new_* fields, then setting the @ioprio_changed flag. As soon as
119 * there is a transition in the entity state that allows the priority
120 * update to take place the effective and the requested priority
121 * values are synchronized.
123 * Unless cgroups are used, the weight value is calculated from the
124 * ioprio to export the same interface as CFQ. When dealing with
125 * ``well-behaved'' queues (i.e., queues that do not spend too much
126 * time to consume their budget and have true sequential behavior, and
127 * when there are no external factors breaking anticipation) the
128 * relative weights at each level of the cgroups hierarchy should be
129 * guaranteed. All the fields are protected by the queue lock of the
130 * containing bfqd.
132 struct bfq_entity {
133 struct rb_node rb_node;
135 int on_st;
137 u64 finish;
138 u64 start;
140 struct rb_root *tree;
142 u64 min_start;
144 unsigned long service, budget;
145 unsigned short weight, new_weight;
146 unsigned short orig_weight;
148 struct bfq_entity *parent;
150 struct bfq_sched_data *my_sched_data;
151 struct bfq_sched_data *sched_data;
153 unsigned short ioprio, new_ioprio;
154 unsigned short ioprio_class, new_ioprio_class;
156 int ioprio_changed;
159 struct bfq_group;
162 * struct bfq_queue - leaf schedulable entity.
163 * @ref: reference counter.
164 * @bfqd: parent bfq_data.
165 * @new_bfqq: shared bfq_queue if queue is cooperating with
166 * one or more other queues.
167 * @pos_node: request-position tree member (see bfq_data's @rq_pos_tree).
168 * @pos_root: request-position tree root (see bfq_data's @rq_pos_tree).
169 * @sort_list: sorted list of pending requests.
170 * @next_rq: if fifo isn't expired, next request to serve.
171 * @queued: nr of requests queued in @sort_list.
172 * @allocated: currently allocated requests.
173 * @meta_pending: pending metadata requests.
174 * @fifo: fifo list of requests in sort_list.
175 * @entity: entity representing this queue in the scheduler.
176 * @max_budget: maximum budget allowed from the feedback mechanism.
177 * @budget_timeout: budget expiration (in jiffies).
178 * @dispatched: number of requests on the dispatch list or inside driver.
179 * @org_ioprio: saved ioprio during boosted periods.
180 * @flags: status flags.
181 * @bfqq_list: node for active/idle bfqq list inside our bfqd.
182 * @seek_samples: number of seeks sampled
183 * @seek_total: sum of the distances of the seeks sampled
184 * @seek_mean: mean seek distance
185 * @last_request_pos: position of the last request enqueued
186 * @pid: pid of the process owning the queue, used for logging purposes.
187 * @last_rais_start_time: last (idle -> weight-raised) transition attempt
188 * @raising_cur_max_time: current max raising time for this queue
190 * A bfq_queue is a leaf request queue; it can be associated to an io_context
191 * or more (if it is an async one). @cgroup holds a reference to the
192 * cgroup, to be sure that it does not disappear while a bfqq still
193 * references it (mostly to avoid races between request issuing and task
194 * migration followed by cgroup distruction).
195 * All the fields are protected by the queue lock of the containing bfqd.
197 struct bfq_queue {
198 atomic_t ref;
199 struct bfq_data *bfqd;
201 /* fields for cooperating queues handling */
202 struct bfq_queue *new_bfqq;
203 struct rb_node pos_node;
204 struct rb_root *pos_root;
206 struct rb_root sort_list;
207 struct request *next_rq;
208 int queued[2];
209 int allocated[2];
210 int meta_pending;
211 struct list_head fifo;
213 struct bfq_entity entity;
215 unsigned long max_budget;
216 unsigned long budget_timeout;
218 int dispatched;
220 unsigned short org_ioprio;
222 unsigned int flags;
224 struct list_head bfqq_list;
226 unsigned int seek_samples;
227 u64 seek_total;
228 sector_t seek_mean;
229 sector_t last_request_pos;
231 pid_t pid;
233 /* weight-raising fields */
234 unsigned int raising_cur_max_time;
235 u64 last_rais_start_finish, soft_rt_next_start;
236 unsigned int raising_coeff;
240 * struct bfq_ttime - per process thinktime stats.
241 * @ttime_total: total process thinktime
242 * @ttime_samples: number of thinktime samples
243 * @ttime_mean: average process thinktime
245 struct bfq_ttime {
246 unsigned long last_end_request;
248 unsigned long ttime_total;
249 unsigned long ttime_samples;
250 unsigned long ttime_mean;
254 * struct bfq_io_cq - per (request_queue, io_context) structure.
255 * @icq: associated io_cq structure
256 * @bfqq: array of two process queues, the sync and the async
257 * @ttime: associated @bfq_ttime struct
259 struct bfq_io_cq {
260 struct io_cq icq; /* must be the first member */
261 struct bfq_queue *bfqq[2];
262 struct bfq_ttime ttime;
266 * struct bfq_data - per device data structure.
267 * @queue: request queue for the managed device.
268 * @root_group: root bfq_group for the device.
269 * @rq_pos_tree: rbtree sorted by next_request position,
270 * used when determining if two or more queues
271 * have interleaving requests (see bfq_close_cooperator).
272 * @busy_queues: number of bfq_queues containing requests (including the
273 * queue under service, even if it is idling).
274 * @queued: number of queued requests.
275 * @rq_in_driver: number of requests dispatched and waiting for completion.
276 * @sync_flight: number of sync requests in the driver.
277 * @max_rq_in_driver: max number of reqs in driver in the last @hw_tag_samples
278 * completed requests .
279 * @hw_tag_samples: nr of samples used to calculate hw_tag.
280 * @hw_tag: flag set to one if the driver is showing a queueing behavior.
281 * @budgets_assigned: number of budgets assigned.
282 * @idle_slice_timer: timer set when idling for the next sequential request
283 * from the queue under service.
284 * @unplug_work: delayed work to restart dispatching on the request queue.
285 * @active_queue: bfq_queue under service.
286 * @active_bic: bfq_io_cq (bic) associated with the @active_queue.
287 * @last_position: on-disk position of the last served request.
288 * @last_budget_start: beginning of the last budget.
289 * @last_idling_start: beginning of the last idle slice.
290 * @peak_rate: peak transfer rate observed for a budget.
291 * @peak_rate_samples: number of samples used to calculate @peak_rate.
292 * @bfq_max_budget: maximum budget allotted to a bfq_queue before rescheduling.
293 * @group_list: list of all the bfq_groups active on the device.
294 * @active_list: list of all the bfq_queues active on the device.
295 * @idle_list: list of all the bfq_queues idle on the device.
296 * @bfq_quantum: max number of requests dispatched per dispatch round.
297 * @bfq_fifo_expire: timeout for async/sync requests; when it expires
298 * requests are served in fifo order.
299 * @bfq_back_penalty: weight of backward seeks wrt forward ones.
300 * @bfq_back_max: maximum allowed backward seek.
301 * @bfq_slice_idle: maximum idling time.
302 * @bfq_user_max_budget: user-configured max budget value (0 for auto-tuning).
303 * @bfq_max_budget_async_rq: maximum budget (in nr of requests) allotted to
304 * async queues.
305 * @bfq_timeout: timeout for bfq_queues to consume their budget; used to
306 * to prevent seeky queues to impose long latencies to well
307 * behaved ones (this also implies that seeky queues cannot
308 * receive guarantees in the service domain; after a timeout
309 * they are charged for the whole allocated budget, to try
310 * to preserve a behavior reasonably fair among them, but
311 * without service-domain guarantees).
312 * @bfq_raising_coeff: Maximum factor by which the weight of a boosted
313 * queue is multiplied
314 * @bfq_raising_max_time: maximum duration of a weight-raising period (jiffies)
315 * @bfq_raising_rt_max_time: maximum duration for soft real-time processes
316 * @bfq_raising_min_idle_time: minimum idle period after which weight-raising
317 * may be reactivated for a queue (in jiffies)
318 * @bfq_raising_min_inter_arr_async: minimum period between request arrivals
319 * after which weight-raising may be
320 * reactivated for an already busy queue
321 * (in jiffies)
322 * @bfq_raising_max_softrt_rate: max service-rate for a soft real-time queue,
323 * sectors per seconds
324 * @oom_bfqq: fallback dummy bfqq for extreme OOM conditions
326 * All the fields are protected by the @queue lock.
328 struct bfq_data {
329 struct request_queue *queue;
331 struct bfq_group *root_group;
333 struct rb_root rq_pos_tree;
335 int busy_queues;
336 int queued;
337 int rq_in_driver;
338 int sync_flight;
340 int max_rq_in_driver;
341 int hw_tag_samples;
342 int hw_tag;
344 int budgets_assigned;
346 struct timer_list idle_slice_timer;
347 struct work_struct unplug_work;
349 struct bfq_queue *active_queue;
350 struct bfq_io_cq *active_bic;
352 sector_t last_position;
354 ktime_t last_budget_start;
355 ktime_t last_idling_start;
356 int peak_rate_samples;
357 u64 peak_rate;
358 unsigned long bfq_max_budget;
360 struct hlist_head group_list;
361 struct list_head active_list;
362 struct list_head idle_list;
364 unsigned int bfq_quantum;
365 unsigned int bfq_fifo_expire[2];
366 unsigned int bfq_back_penalty;
367 unsigned int bfq_back_max;
368 unsigned int bfq_slice_idle;
369 u64 bfq_class_idle_last_service;
371 unsigned int bfq_user_max_budget;
372 unsigned int bfq_max_budget_async_rq;
373 unsigned int bfq_timeout[2];
375 bool low_latency;
377 /* parameters of the low_latency heuristics */
378 unsigned int bfq_raising_coeff;
379 unsigned int bfq_raising_max_time;
380 unsigned int bfq_raising_rt_max_time;
381 unsigned int bfq_raising_min_idle_time;
382 unsigned int bfq_raising_min_inter_arr_async;
383 unsigned int bfq_raising_max_softrt_rate;
385 struct bfq_queue oom_bfqq;
388 enum bfqq_state_flags {
389 BFQ_BFQQ_FLAG_busy = 0, /* has requests or is under service */
390 BFQ_BFQQ_FLAG_wait_request, /* waiting for a request */
391 BFQ_BFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
392 BFQ_BFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
393 BFQ_BFQQ_FLAG_idle_window, /* slice idling enabled */
394 BFQ_BFQQ_FLAG_prio_changed, /* task priority has changed */
395 BFQ_BFQQ_FLAG_sync, /* synchronous queue */
396 BFQ_BFQQ_FLAG_budget_new, /* no completion with this budget */
397 BFQ_BFQQ_FLAG_coop, /* bfqq is shared */
398 BFQ_BFQQ_FLAG_split_coop, /* shared bfqq will be splitted */
399 BFQ_BFQQ_FLAG_some_coop_idle, /* some cooperator is inactive */
402 #define BFQ_BFQQ_FNS(name) \
403 static inline void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
405 (bfqq)->flags |= (1 << BFQ_BFQQ_FLAG_##name); \
407 static inline void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
409 (bfqq)->flags &= ~(1 << BFQ_BFQQ_FLAG_##name); \
411 static inline int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
413 return ((bfqq)->flags & (1 << BFQ_BFQQ_FLAG_##name)) != 0; \
416 BFQ_BFQQ_FNS(busy);
417 BFQ_BFQQ_FNS(wait_request);
418 BFQ_BFQQ_FNS(must_alloc);
419 BFQ_BFQQ_FNS(fifo_expire);
420 BFQ_BFQQ_FNS(idle_window);
421 BFQ_BFQQ_FNS(prio_changed);
422 BFQ_BFQQ_FNS(sync);
423 BFQ_BFQQ_FNS(budget_new);
424 BFQ_BFQQ_FNS(coop);
425 BFQ_BFQQ_FNS(split_coop);
426 BFQ_BFQQ_FNS(some_coop_idle);
427 #undef BFQ_BFQQ_FNS
429 /* Logging facilities. */
430 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
431 blk_add_trace_msg((bfqd)->queue, "bfq%d " fmt, (bfqq)->pid, ##args)
433 #define bfq_log(bfqd, fmt, args...) \
434 blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
436 /* Expiration reasons. */
437 enum bfqq_expiration {
438 BFQ_BFQQ_TOO_IDLE = 0, /* queue has been idling for too long */
439 BFQ_BFQQ_BUDGET_TIMEOUT, /* budget took too long to be used */
440 BFQ_BFQQ_BUDGET_EXHAUSTED, /* budget consumed */
441 BFQ_BFQQ_NO_MORE_REQUESTS, /* the queue has no more requests */
444 #ifdef CONFIG_CGROUP_BFQIO
446 * struct bfq_group - per (device, cgroup) data structure.
447 * @entity: schedulable entity to insert into the parent group sched_data.
448 * @sched_data: own sched_data, to contain child entities (they may be
449 * both bfq_queues and bfq_groups).
450 * @group_node: node to be inserted into the bfqio_cgroup->group_data
451 * list of the containing cgroup's bfqio_cgroup.
452 * @bfqd_node: node to be inserted into the @bfqd->group_list list
453 * of the groups active on the same device; used for cleanup.
454 * @bfqd: the bfq_data for the device this group acts upon.
455 * @async_bfqq: array of async queues for all the tasks belonging to
456 * the group, one queue per ioprio value per ioprio_class,
457 * except for the idle class that has only one queue.
458 * @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
459 * @my_entity: pointer to @entity, %NULL for the toplevel group; used
460 * to avoid too many special cases during group creation/migration.
462 * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
463 * there is a set of bfq_groups, each one collecting the lower-level
464 * entities belonging to the group that are acting on the same device.
466 * Locking works as follows:
467 * o @group_node is protected by the bfqio_cgroup lock, and is accessed
468 * via RCU from its readers.
469 * o @bfqd is protected by the queue lock, RCU is used to access it
470 * from the readers.
471 * o All the other fields are protected by the @bfqd queue lock.
473 struct bfq_group {
474 struct bfq_entity entity;
475 struct bfq_sched_data sched_data;
477 struct hlist_node group_node;
478 struct hlist_node bfqd_node;
480 void *bfqd;
482 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
483 struct bfq_queue *async_idle_bfqq;
485 struct bfq_entity *my_entity;
489 * struct bfqio_cgroup - bfq cgroup data structure.
490 * @css: subsystem state for bfq in the containing cgroup.
491 * @weight: cgroup weight.
492 * @ioprio: cgroup ioprio.
493 * @ioprio_class: cgroup ioprio_class.
494 * @lock: spinlock that protects @ioprio, @ioprio_class and @group_data.
495 * @group_data: list containing the bfq_group belonging to this cgroup.
497 * @group_data is accessed using RCU, with @lock protecting the updates,
498 * @ioprio and @ioprio_class are protected by @lock.
500 struct bfqio_cgroup {
501 struct cgroup_subsys_state css;
503 unsigned short weight, ioprio, ioprio_class;
505 spinlock_t lock;
506 struct hlist_head group_data;
508 #else
509 struct bfq_group {
510 struct bfq_sched_data sched_data;
512 struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
513 struct bfq_queue *async_idle_bfqq;
515 #endif
517 static inline struct bfq_service_tree *
518 bfq_entity_service_tree(struct bfq_entity *entity)
520 struct bfq_sched_data *sched_data = entity->sched_data;
521 unsigned int idx = entity->ioprio_class - 1;
523 BUG_ON(idx >= BFQ_IOPRIO_CLASSES);
524 BUG_ON(sched_data == NULL);
526 return sched_data->service_tree + idx;
529 static inline struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic,
530 int is_sync)
532 return bic->bfqq[!!is_sync];
535 static inline void bic_set_bfqq(struct bfq_io_cq *bic,
536 struct bfq_queue *bfqq, int is_sync)
538 bic->bfqq[!!is_sync] = bfqq;
541 static inline struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic)
543 return bic->icq.q->elevator->elevator_data;
547 * bfq_get_bfqd_locked - get a lock to a bfqd using a RCU protected pointer.
548 * @ptr: a pointer to a bfqd.
549 * @flags: storage for the flags to be saved.
551 * This function allows bfqg->bfqd to be protected by the
552 * queue lock of the bfqd they reference; the pointer is dereferenced
553 * under RCU, so the storage for bfqd is assured to be safe as long
554 * as the RCU read side critical section does not end. After the
555 * bfqd->queue->queue_lock is taken the pointer is rechecked, to be
556 * sure that no other writer accessed it. If we raced with a writer,
557 * the function returns NULL, with the queue unlocked, otherwise it
558 * returns the dereferenced pointer, with the queue locked.
560 static inline struct bfq_data *bfq_get_bfqd_locked(void **ptr,
561 unsigned long *flags)
563 struct bfq_data *bfqd;
565 rcu_read_lock();
566 bfqd = rcu_dereference(*(struct bfq_data **)ptr);
568 if (bfqd != NULL) {
569 spin_lock_irqsave(bfqd->queue->queue_lock, *flags);
570 if (*ptr == bfqd)
571 goto out;
572 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
575 bfqd = NULL;
576 out:
577 rcu_read_unlock();
578 return bfqd;
581 static inline void bfq_put_bfqd_unlock(struct bfq_data *bfqd,
582 unsigned long *flags)
584 spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
587 static void bfq_changed_ioprio(struct io_context *ioc,
588 struct bfq_io_cq *bic);
589 static void bfq_put_queue(struct bfq_queue *bfqq);
590 static void bfq_dispatch_insert(struct request_queue *q, struct request *rq);
591 static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
592 struct bfq_group *bfqg, int is_sync,
593 struct io_context *ioc, gfp_t gfp_mask);
594 static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
595 static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);
596 #endif