2
* BFQ-v5 for 3.1.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>
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
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;
49
struct bfq_entity *first_idle;
50
struct bfq_entity *last_idle;
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];
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
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
133
struct rb_node rb_node;
140
struct rb_root *tree;
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;
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.
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;
211
struct list_head fifo;
213
struct bfq_entity entity;
215
unsigned long max_budget;
216
unsigned long budget_timeout;
220
unsigned short org_ioprio;
224
struct list_head bfqq_list;
226
unsigned int seek_samples;
229
sector_t last_request_pos;
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_data - per device data structure.
241
* @queue: request queue for the managed device.
242
* @root_group: root bfq_group for the device.
243
* @rq_pos_tree: rbtree sorted by next_request position,
244
* used when determining if two or more queues
245
* have interleaving requests (see bfq_close_cooperator).
246
* @busy_queues: number of bfq_queues containing requests (including the
247
* queue under service, even if it is idling).
248
* @queued: number of queued requests.
249
* @rq_in_driver: number of requests dispatched and waiting for completion.
250
* @sync_flight: number of sync requests in the driver.
251
* @max_rq_in_driver: max number of reqs in driver in the last @hw_tag_samples
252
* completed requests .
253
* @hw_tag_samples: nr of samples used to calculate hw_tag.
254
* @hw_tag: flag set to one if the driver is showing a queueing behavior.
255
* @budgets_assigned: number of budgets assigned.
256
* @idle_slice_timer: timer set when idling for the next sequential request
257
* from the queue under service.
258
* @unplug_work: delayed work to restart dispatching on the request queue.
259
* @active_queue: bfq_queue under service.
260
* @active_cic: cfq_io_context (cic) associated with the @active_queue.
261
* @last_position: on-disk position of the last served request.
262
* @last_budget_start: beginning of the last budget.
263
* @last_idling_start: beginning of the last idle slice.
264
* @peak_rate: peak transfer rate observed for a budget.
265
* @peak_rate_samples: number of samples used to calculate @peak_rate.
266
* @bfq_max_budget: maximum budget allotted to a bfq_queue before rescheduling.
267
* @cic_index: use small consequent indexes as radix tree keys to reduce depth
268
* @cic_list: list of all the cics active on the bfq_data device.
269
* @group_list: list of all the bfq_groups active on the device.
270
* @active_list: list of all the bfq_queues active on the device.
271
* @idle_list: list of all the bfq_queues idle on the device.
272
* @bfq_quantum: max number of requests dispatched per dispatch round.
273
* @bfq_fifo_expire: timeout for async/sync requests; when it expires
274
* requests are served in fifo order.
275
* @bfq_back_penalty: weight of backward seeks wrt forward ones.
276
* @bfq_back_max: maximum allowed backward seek.
277
* @bfq_slice_idle: maximum idling time.
278
* @bfq_user_max_budget: user-configured max budget value (0 for auto-tuning).
279
* @bfq_max_budget_async_rq: maximum budget (in nr of requests) allotted to
281
* @bfq_timeout: timeout for bfq_queues to consume their budget; used to
282
* to prevent seeky queues to impose long latencies to well
283
* behaved ones (this also implies that seeky queues cannot
284
* receive guarantees in the service domain; after a timeout
285
* they are charged for the whole allocated budget, to try
286
* to preserve a behavior reasonably fair among them, but
287
* without service-domain guarantees).
288
* @bfq_raising_coeff: Maximum factor by which the weight of a boosted
289
* queue is multiplied
290
* @bfq_raising_max_time: maximum duration of a weight-raising period (jiffies)
291
* @bfq_raising_rt_max_time: maximum duration for soft real-time processes
292
* @bfq_raising_min_idle_time: minimum idle period after which weight-raising
293
* may be reactivated for a queue (in jiffies)
294
* @bfq_raising_min_inter_arr_async: minimum period between request arrivals
295
* after which weight-raising may be
296
* reactivated for an already busy queue
298
* @bfq_raising_max_softrt_rate: max service-rate for a soft real-time queue,
299
* sectors per seconds
300
* @RT_prod: cached value of the product R*T used for computing the maximum
301
* duration of the weight raising automatically
302
* @oom_bfqq: fallback dummy bfqq for extreme OOM conditions
304
* All the fields are protected by the @queue lock.
307
struct request_queue *queue;
309
struct bfq_group *root_group;
311
struct rb_root rq_pos_tree;
318
int max_rq_in_driver;
322
int budgets_assigned;
324
struct timer_list idle_slice_timer;
325
struct work_struct unplug_work;
327
struct bfq_queue *active_queue;
328
struct cfq_io_context *active_cic;
330
sector_t last_position;
332
ktime_t last_budget_start;
333
ktime_t last_idling_start;
334
int peak_rate_samples;
336
unsigned long bfq_max_budget;
338
unsigned int cic_index;
339
struct list_head cic_list;
340
struct hlist_head group_list;
341
struct list_head active_list;
342
struct list_head idle_list;
344
unsigned int bfq_quantum;
345
unsigned int bfq_fifo_expire[2];
346
unsigned int bfq_back_penalty;
347
unsigned int bfq_back_max;
348
unsigned int bfq_slice_idle;
349
u64 bfq_class_idle_last_service;
351
unsigned int bfq_user_max_budget;
352
unsigned int bfq_max_budget_async_rq;
353
unsigned int bfq_timeout[2];
357
/* parameters of the low_latency heuristics */
358
unsigned int bfq_raising_coeff;
359
unsigned int bfq_raising_max_time;
360
unsigned int bfq_raising_rt_max_time;
361
unsigned int bfq_raising_min_idle_time;
362
unsigned int bfq_raising_min_inter_arr_async;
363
unsigned int bfq_raising_max_softrt_rate;
366
struct bfq_queue oom_bfqq;
369
enum bfqq_state_flags {
370
BFQ_BFQQ_FLAG_busy = 0, /* has requests or is under service */
371
BFQ_BFQQ_FLAG_wait_request, /* waiting for a request */
372
BFQ_BFQQ_FLAG_must_alloc, /* must be allowed rq alloc */
373
BFQ_BFQQ_FLAG_fifo_expire, /* FIFO checked in this slice */
374
BFQ_BFQQ_FLAG_idle_window, /* slice idling enabled */
375
BFQ_BFQQ_FLAG_prio_changed, /* task priority has changed */
376
BFQ_BFQQ_FLAG_sync, /* synchronous queue */
377
BFQ_BFQQ_FLAG_budget_new, /* no completion with this budget */
378
BFQ_BFQQ_FLAG_coop, /* bfqq is shared */
379
BFQ_BFQQ_FLAG_split_coop, /* shared bfqq will be splitted */
380
BFQ_BFQQ_FLAG_some_coop_idle, /* some cooperator is inactive */
383
#define BFQ_BFQQ_FNS(name) \
384
static inline void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \
386
(bfqq)->flags |= (1 << BFQ_BFQQ_FLAG_##name); \
388
static inline void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \
390
(bfqq)->flags &= ~(1 << BFQ_BFQQ_FLAG_##name); \
392
static inline int bfq_bfqq_##name(const struct bfq_queue *bfqq) \
394
return ((bfqq)->flags & (1 << BFQ_BFQQ_FLAG_##name)) != 0; \
398
BFQ_BFQQ_FNS(wait_request);
399
BFQ_BFQQ_FNS(must_alloc);
400
BFQ_BFQQ_FNS(fifo_expire);
401
BFQ_BFQQ_FNS(idle_window);
402
BFQ_BFQQ_FNS(prio_changed);
404
BFQ_BFQQ_FNS(budget_new);
406
BFQ_BFQQ_FNS(split_coop);
407
BFQ_BFQQ_FNS(some_coop_idle);
410
/* Logging facilities. */
411
#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
412
blk_add_trace_msg((bfqd)->queue, "bfq%d " fmt, (bfqq)->pid, ##args)
414
#define bfq_log(bfqd, fmt, args...) \
415
blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args)
417
/* Expiration reasons. */
418
enum bfqq_expiration {
419
BFQ_BFQQ_TOO_IDLE = 0, /* queue has been idling for too long */
420
BFQ_BFQQ_BUDGET_TIMEOUT, /* budget took too long to be used */
421
BFQ_BFQQ_BUDGET_EXHAUSTED, /* budget consumed */
422
BFQ_BFQQ_NO_MORE_REQUESTS, /* the queue has no more requests */
425
#ifdef CONFIG_CGROUP_BFQIO
427
* struct bfq_group - per (device, cgroup) data structure.
428
* @entity: schedulable entity to insert into the parent group sched_data.
429
* @sched_data: own sched_data, to contain child entities (they may be
430
* both bfq_queues and bfq_groups).
431
* @group_node: node to be inserted into the bfqio_cgroup->group_data
432
* list of the containing cgroup's bfqio_cgroup.
433
* @bfqd_node: node to be inserted into the @bfqd->group_list list
434
* of the groups active on the same device; used for cleanup.
435
* @bfqd: the bfq_data for the device this group acts upon.
436
* @async_bfqq: array of async queues for all the tasks belonging to
437
* the group, one queue per ioprio value per ioprio_class,
438
* except for the idle class that has only one queue.
439
* @async_idle_bfqq: async queue for the idle class (ioprio is ignored).
440
* @my_entity: pointer to @entity, %NULL for the toplevel group; used
441
* to avoid too many special cases during group creation/migration.
443
* Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup
444
* there is a set of bfq_groups, each one collecting the lower-level
445
* entities belonging to the group that are acting on the same device.
447
* Locking works as follows:
448
* o @group_node is protected by the bfqio_cgroup lock, and is accessed
449
* via RCU from its readers.
450
* o @bfqd is protected by the queue lock, RCU is used to access it
452
* o All the other fields are protected by the @bfqd queue lock.
455
struct bfq_entity entity;
456
struct bfq_sched_data sched_data;
458
struct hlist_node group_node;
459
struct hlist_node bfqd_node;
463
struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
464
struct bfq_queue *async_idle_bfqq;
466
struct bfq_entity *my_entity;
470
* struct bfqio_cgroup - bfq cgroup data structure.
471
* @css: subsystem state for bfq in the containing cgroup.
472
* @weight: cgroup weight.
473
* @ioprio: cgroup ioprio.
474
* @ioprio_class: cgroup ioprio_class.
475
* @lock: spinlock that protects @ioprio, @ioprio_class and @group_data.
476
* @group_data: list containing the bfq_group belonging to this cgroup.
478
* @group_data is accessed using RCU, with @lock protecting the updates,
479
* @ioprio and @ioprio_class are protected by @lock.
481
struct bfqio_cgroup {
482
struct cgroup_subsys_state css;
484
unsigned short weight, ioprio, ioprio_class;
487
struct hlist_head group_data;
491
struct bfq_sched_data sched_data;
493
struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR];
494
struct bfq_queue *async_idle_bfqq;
498
static inline struct bfq_service_tree *
499
bfq_entity_service_tree(struct bfq_entity *entity)
501
struct bfq_sched_data *sched_data = entity->sched_data;
502
unsigned int idx = entity->ioprio_class - 1;
504
BUG_ON(idx >= BFQ_IOPRIO_CLASSES);
505
BUG_ON(sched_data == NULL);
507
return sched_data->service_tree + idx;
510
static inline struct bfq_queue *cic_to_bfqq(struct cfq_io_context *cic,
513
return cic->cfqq[!!is_sync];
516
static inline void cic_set_bfqq(struct cfq_io_context *cic,
517
struct bfq_queue *bfqq, int is_sync)
519
cic->cfqq[!!is_sync] = bfqq;
522
static inline void call_for_each_cic(struct io_context *ioc,
523
void (*func)(struct io_context *,
524
struct cfq_io_context *))
526
struct cfq_io_context *cic;
527
struct hlist_node *n;
530
hlist_for_each_entry_rcu(cic, n, &ioc->bfq_cic_list, cic_list)
535
#define CIC_DEAD_KEY 1ul
536
#define CIC_DEAD_INDEX_SHIFT 1
538
static inline void *bfqd_dead_key(struct bfq_data *bfqd)
540
return (void *)(bfqd->cic_index << CIC_DEAD_INDEX_SHIFT | CIC_DEAD_KEY);
544
* bfq_get_bfqd_locked - get a lock to a bfqd using a RCU protected pointer.
545
* @ptr: a pointer to a bfqd.
546
* @flags: storage for the flags to be saved.
548
* This function allows cic->key and bfqg->bfqd to be protected by the
549
* queue lock of the bfqd they reference; the pointer is dereferenced
550
* under RCU, so the storage for bfqd is assured to be safe as long
551
* as the RCU read side critical section does not end. After the
552
* bfqd->queue->queue_lock is taken the pointer is rechecked, to be
553
* sure that no other writer accessed it. If we raced with a writer,
554
* the function returns NULL, with the queue unlocked, otherwise it
555
* returns the dereferenced pointer, with the queue locked.
557
static inline struct bfq_data *bfq_get_bfqd_locked(void **ptr,
558
unsigned long *flags)
560
struct bfq_data *bfqd;
563
bfqd = rcu_dereference(*(struct bfq_data **)ptr);
565
if (bfqd != NULL && !((unsigned long) bfqd & CIC_DEAD_KEY)) {
566
spin_lock_irqsave(bfqd->queue->queue_lock, *flags);
569
spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
578
static inline void bfq_put_bfqd_unlock(struct bfq_data *bfqd,
579
unsigned long *flags)
581
spin_unlock_irqrestore(bfqd->queue->queue_lock, *flags);
584
static void bfq_changed_ioprio(struct io_context *ioc,
585
struct cfq_io_context *cic);
586
static void bfq_put_queue(struct bfq_queue *bfqq);
587
static void bfq_dispatch_insert(struct request_queue *q, struct request *rq);
588
static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd,
589
struct bfq_group *bfqg, int is_sync,
590
struct io_context *ioc, gfp_t gfp_mask);
591
static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg);
592
static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq);