]>
Commit | Line | Data |
---|---|---|
aee69d78 PV |
1 | /* |
2 | * Budget Fair Queueing (BFQ) I/O scheduler. | |
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 | * Arianna Avanzini <avanzini@google.com> | |
12 | * | |
13 | * Copyright (C) 2017 Paolo Valente <paolo.valente@linaro.org> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License as | |
17 | * published by the Free Software Foundation; either version 2 of the | |
18 | * License, or (at your option) any later version. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
23 | * General Public License for more details. | |
24 | * | |
25 | * BFQ is a proportional-share I/O scheduler, with some extra | |
26 | * low-latency capabilities. BFQ also supports full hierarchical | |
27 | * scheduling through cgroups. Next paragraphs provide an introduction | |
28 | * on BFQ inner workings. Details on BFQ benefits, usage and | |
29 | * limitations can be found in Documentation/block/bfq-iosched.txt. | |
30 | * | |
31 | * BFQ is a proportional-share storage-I/O scheduling algorithm based | |
32 | * on the slice-by-slice service scheme of CFQ. But BFQ assigns | |
33 | * budgets, measured in number of sectors, to processes instead of | |
34 | * time slices. The device is not granted to the in-service process | |
35 | * for a given time slice, but until it has exhausted its assigned | |
36 | * budget. This change from the time to the service domain enables BFQ | |
37 | * to distribute the device throughput among processes as desired, | |
38 | * without any distortion due to throughput fluctuations, or to device | |
39 | * internal queueing. BFQ uses an ad hoc internal scheduler, called | |
40 | * B-WF2Q+, to schedule processes according to their budgets. More | |
41 | * precisely, BFQ schedules queues associated with processes. Each | |
42 | * process/queue is assigned a user-configurable weight, and B-WF2Q+ | |
43 | * guarantees that each queue receives a fraction of the throughput | |
44 | * proportional to its weight. Thanks to the accurate policy of | |
45 | * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound | |
46 | * processes issuing sequential requests (to boost the throughput), | |
47 | * and yet guarantee a low latency to interactive and soft real-time | |
48 | * applications. | |
49 | * | |
50 | * In particular, to provide these low-latency guarantees, BFQ | |
51 | * explicitly privileges the I/O of two classes of time-sensitive | |
52 | * applications: interactive and soft real-time. This feature enables | |
53 | * BFQ to provide applications in these classes with a very low | |
54 | * latency. Finally, BFQ also features additional heuristics for | |
55 | * preserving both a low latency and a high throughput on NCQ-capable, | |
56 | * rotational or flash-based devices, and to get the job done quickly | |
57 | * for applications consisting in many I/O-bound processes. | |
58 | * | |
59 | * BFQ is described in [1], where also a reference to the initial, more | |
60 | * theoretical paper on BFQ can be found. The interested reader can find | |
61 | * in the latter paper full details on the main algorithm, as well as | |
62 | * formulas of the guarantees and formal proofs of all the properties. | |
63 | * With respect to the version of BFQ presented in these papers, this | |
64 | * implementation adds a few more heuristics, such as the one that | |
65 | * guarantees a low latency to soft real-time applications, and a | |
66 | * hierarchical extension based on H-WF2Q+. | |
67 | * | |
68 | * B-WF2Q+ is based on WF2Q+, which is described in [2], together with | |
69 | * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+ | |
70 | * with O(log N) complexity derives from the one introduced with EEVDF | |
71 | * in [3]. | |
72 | * | |
73 | * [1] P. Valente, A. Avanzini, "Evolution of the BFQ Storage I/O | |
74 | * Scheduler", Proceedings of the First Workshop on Mobile System | |
75 | * Technologies (MST-2015), May 2015. | |
76 | * http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf | |
77 | * | |
78 | * [2] Jon C.R. Bennett and H. Zhang, "Hierarchical Packet Fair Queueing | |
79 | * Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689, | |
80 | * Oct 1997. | |
81 | * | |
82 | * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz | |
83 | * | |
84 | * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline | |
85 | * First: A Flexible and Accurate Mechanism for Proportional Share | |
86 | * Resource Allocation", technical report. | |
87 | * | |
88 | * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf | |
89 | */ | |
90 | #include <linux/module.h> | |
91 | #include <linux/slab.h> | |
92 | #include <linux/blkdev.h> | |
e21b7a0b | 93 | #include <linux/cgroup.h> |
aee69d78 PV |
94 | #include <linux/elevator.h> |
95 | #include <linux/ktime.h> | |
96 | #include <linux/rbtree.h> | |
97 | #include <linux/ioprio.h> | |
98 | #include <linux/sbitmap.h> | |
99 | #include <linux/delay.h> | |
100 | ||
101 | #include "blk.h" | |
102 | #include "blk-mq.h" | |
103 | #include "blk-mq-tag.h" | |
104 | #include "blk-mq-sched.h" | |
105 | #include <linux/blktrace_api.h> | |
106 | #include <linux/hrtimer.h> | |
107 | #include <linux/blk-cgroup.h> | |
108 | ||
109 | #define BFQ_IOPRIO_CLASSES 3 | |
110 | #define BFQ_CL_IDLE_TIMEOUT (HZ/5) | |
111 | ||
112 | #define BFQ_MIN_WEIGHT 1 | |
113 | #define BFQ_MAX_WEIGHT 1000 | |
114 | #define BFQ_WEIGHT_CONVERSION_COEFF 10 | |
115 | ||
116 | #define BFQ_DEFAULT_QUEUE_IOPRIO 4 | |
117 | ||
e21b7a0b | 118 | #define BFQ_WEIGHT_LEGACY_DFL 100 |
aee69d78 PV |
119 | #define BFQ_DEFAULT_GRP_IOPRIO 0 |
120 | #define BFQ_DEFAULT_GRP_CLASS IOPRIO_CLASS_BE | |
121 | ||
77b7dcea PV |
122 | /* |
123 | * Soft real-time applications are extremely more latency sensitive | |
124 | * than interactive ones. Over-raise the weight of the former to | |
125 | * privilege them against the latter. | |
126 | */ | |
127 | #define BFQ_SOFTRT_WEIGHT_FACTOR 100 | |
128 | ||
aee69d78 PV |
129 | struct bfq_entity; |
130 | ||
131 | /** | |
132 | * struct bfq_service_tree - per ioprio_class service tree. | |
133 | * | |
134 | * Each service tree represents a B-WF2Q+ scheduler on its own. Each | |
135 | * ioprio_class has its own independent scheduler, and so its own | |
136 | * bfq_service_tree. All the fields are protected by the queue lock | |
137 | * of the containing bfqd. | |
138 | */ | |
139 | struct bfq_service_tree { | |
140 | /* tree for active entities (i.e., those backlogged) */ | |
141 | struct rb_root active; | |
142 | /* tree for idle entities (i.e., not backlogged, with V <= F_i)*/ | |
143 | struct rb_root idle; | |
144 | ||
145 | /* idle entity with minimum F_i */ | |
146 | struct bfq_entity *first_idle; | |
147 | /* idle entity with maximum F_i */ | |
148 | struct bfq_entity *last_idle; | |
149 | ||
150 | /* scheduler virtual time */ | |
151 | u64 vtime; | |
152 | /* scheduler weight sum; active and idle entities contribute to it */ | |
153 | unsigned long wsum; | |
154 | }; | |
155 | ||
156 | /** | |
157 | * struct bfq_sched_data - multi-class scheduler. | |
158 | * | |
159 | * bfq_sched_data is the basic scheduler queue. It supports three | |
e21b7a0b AA |
160 | * ioprio_classes, and can be used either as a toplevel queue or as an |
161 | * intermediate queue on a hierarchical setup. @next_in_service | |
162 | * points to the active entity of the sched_data service trees that | |
163 | * will be scheduled next. It is used to reduce the number of steps | |
164 | * needed for each hierarchical-schedule update. | |
aee69d78 PV |
165 | * |
166 | * The supported ioprio_classes are the same as in CFQ, in descending | |
167 | * priority order, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE. | |
168 | * Requests from higher priority queues are served before all the | |
169 | * requests from lower priority queues; among requests of the same | |
170 | * queue requests are served according to B-WF2Q+. | |
171 | * All the fields are protected by the queue lock of the containing bfqd. | |
172 | */ | |
173 | struct bfq_sched_data { | |
174 | /* entity in service */ | |
175 | struct bfq_entity *in_service_entity; | |
e21b7a0b | 176 | /* head-of-line entity (see comments above) */ |
aee69d78 PV |
177 | struct bfq_entity *next_in_service; |
178 | /* array of service trees, one per ioprio_class */ | |
179 | struct bfq_service_tree service_tree[BFQ_IOPRIO_CLASSES]; | |
e21b7a0b AA |
180 | /* last time CLASS_IDLE was served */ |
181 | unsigned long bfq_class_idle_last_service; | |
182 | ||
aee69d78 PV |
183 | }; |
184 | ||
1de0c4cd AA |
185 | /** |
186 | * struct bfq_weight_counter - counter of the number of all active entities | |
187 | * with a given weight. | |
188 | */ | |
189 | struct bfq_weight_counter { | |
190 | unsigned int weight; /* weight of the entities this counter refers to */ | |
191 | unsigned int num_active; /* nr of active entities with this weight */ | |
192 | /* | |
193 | * Weights tree member (see bfq_data's @queue_weights_tree and | |
194 | * @group_weights_tree) | |
195 | */ | |
196 | struct rb_node weights_node; | |
197 | }; | |
198 | ||
aee69d78 PV |
199 | /** |
200 | * struct bfq_entity - schedulable entity. | |
201 | * | |
e21b7a0b AA |
202 | * A bfq_entity is used to represent either a bfq_queue (leaf node in the |
203 | * cgroup hierarchy) or a bfq_group into the upper level scheduler. Each | |
204 | * entity belongs to the sched_data of the parent group in the cgroup | |
205 | * hierarchy. Non-leaf entities have also their own sched_data, stored | |
206 | * in @my_sched_data. | |
aee69d78 PV |
207 | * |
208 | * Each entity stores independently its priority values; this would | |
209 | * allow different weights on different devices, but this | |
210 | * functionality is not exported to userspace by now. Priorities and | |
211 | * weights are updated lazily, first storing the new values into the | |
212 | * new_* fields, then setting the @prio_changed flag. As soon as | |
213 | * there is a transition in the entity state that allows the priority | |
214 | * update to take place the effective and the requested priority | |
215 | * values are synchronized. | |
216 | * | |
e21b7a0b AA |
217 | * Unless cgroups are used, the weight value is calculated from the |
218 | * ioprio to export the same interface as CFQ. When dealing with | |
219 | * ``well-behaved'' queues (i.e., queues that do not spend too much | |
220 | * time to consume their budget and have true sequential behavior, and | |
221 | * when there are no external factors breaking anticipation) the | |
222 | * relative weights at each level of the cgroups hierarchy should be | |
223 | * guaranteed. All the fields are protected by the queue lock of the | |
224 | * containing bfqd. | |
aee69d78 PV |
225 | */ |
226 | struct bfq_entity { | |
227 | /* service_tree member */ | |
228 | struct rb_node rb_node; | |
1de0c4cd AA |
229 | /* pointer to the weight counter associated with this entity */ |
230 | struct bfq_weight_counter *weight_counter; | |
aee69d78 PV |
231 | |
232 | /* | |
e21b7a0b AA |
233 | * Flag, true if the entity is on a tree (either the active or |
234 | * the idle one of its service_tree) or is in service. | |
aee69d78 | 235 | */ |
e21b7a0b | 236 | bool on_st; |
aee69d78 PV |
237 | |
238 | /* B-WF2Q+ start and finish timestamps [sectors/weight] */ | |
239 | u64 start, finish; | |
240 | ||
241 | /* tree the entity is enqueued into; %NULL if not on a tree */ | |
242 | struct rb_root *tree; | |
243 | ||
244 | /* | |
245 | * minimum start time of the (active) subtree rooted at this | |
246 | * entity; used for O(log N) lookups into active trees | |
247 | */ | |
248 | u64 min_start; | |
249 | ||
250 | /* amount of service received during the last service slot */ | |
251 | int service; | |
252 | ||
253 | /* budget, used also to calculate F_i: F_i = S_i + @budget / @weight */ | |
254 | int budget; | |
255 | ||
256 | /* weight of the queue */ | |
257 | int weight; | |
258 | /* next weight if a change is in progress */ | |
259 | int new_weight; | |
260 | ||
261 | /* original weight, used to implement weight boosting */ | |
262 | int orig_weight; | |
263 | ||
264 | /* parent entity, for hierarchical scheduling */ | |
265 | struct bfq_entity *parent; | |
266 | ||
267 | /* | |
268 | * For non-leaf nodes in the hierarchy, the associated | |
269 | * scheduler queue, %NULL on leaf nodes. | |
270 | */ | |
271 | struct bfq_sched_data *my_sched_data; | |
272 | /* the scheduler queue this entity belongs to */ | |
273 | struct bfq_sched_data *sched_data; | |
274 | ||
275 | /* flag, set to request a weight, ioprio or ioprio_class change */ | |
276 | int prio_changed; | |
277 | }; | |
278 | ||
e21b7a0b AA |
279 | struct bfq_group; |
280 | ||
aee69d78 PV |
281 | /** |
282 | * struct bfq_ttime - per process thinktime stats. | |
283 | */ | |
284 | struct bfq_ttime { | |
285 | /* completion time of the last request */ | |
286 | u64 last_end_request; | |
287 | ||
288 | /* total process thinktime */ | |
289 | u64 ttime_total; | |
290 | /* number of thinktime samples */ | |
291 | unsigned long ttime_samples; | |
292 | /* average process thinktime */ | |
293 | u64 ttime_mean; | |
294 | }; | |
295 | ||
296 | /** | |
297 | * struct bfq_queue - leaf schedulable entity. | |
298 | * | |
299 | * A bfq_queue is a leaf request queue; it can be associated with an | |
36eca894 AA |
300 | * io_context or more, if it is async or shared between cooperating |
301 | * processes. @cgroup holds a reference to the cgroup, to be sure that it | |
302 | * does not disappear while a bfqq still references it (mostly to avoid | |
303 | * races between request issuing and task migration followed by cgroup | |
304 | * destruction). | |
305 | * All the fields are protected by the queue lock of the containing bfqd. | |
aee69d78 PV |
306 | */ |
307 | struct bfq_queue { | |
308 | /* reference counter */ | |
309 | int ref; | |
310 | /* parent bfq_data */ | |
311 | struct bfq_data *bfqd; | |
312 | ||
313 | /* current ioprio and ioprio class */ | |
314 | unsigned short ioprio, ioprio_class; | |
315 | /* next ioprio and ioprio class if a change is in progress */ | |
316 | unsigned short new_ioprio, new_ioprio_class; | |
317 | ||
36eca894 AA |
318 | /* |
319 | * Shared bfq_queue if queue is cooperating with one or more | |
320 | * other queues. | |
321 | */ | |
322 | struct bfq_queue *new_bfqq; | |
323 | /* request-position tree member (see bfq_group's @rq_pos_tree) */ | |
324 | struct rb_node pos_node; | |
325 | /* request-position tree root (see bfq_group's @rq_pos_tree) */ | |
326 | struct rb_root *pos_root; | |
327 | ||
aee69d78 PV |
328 | /* sorted list of pending requests */ |
329 | struct rb_root sort_list; | |
330 | /* if fifo isn't expired, next request to serve */ | |
331 | struct request *next_rq; | |
332 | /* number of sync and async requests queued */ | |
333 | int queued[2]; | |
334 | /* number of requests currently allocated */ | |
335 | int allocated; | |
336 | /* number of pending metadata requests */ | |
337 | int meta_pending; | |
338 | /* fifo list of requests in sort_list */ | |
339 | struct list_head fifo; | |
340 | ||
341 | /* entity representing this queue in the scheduler */ | |
342 | struct bfq_entity entity; | |
343 | ||
344 | /* maximum budget allowed from the feedback mechanism */ | |
345 | int max_budget; | |
346 | /* budget expiration (in jiffies) */ | |
347 | unsigned long budget_timeout; | |
348 | ||
349 | /* number of requests on the dispatch list or inside driver */ | |
350 | int dispatched; | |
351 | ||
352 | /* status flags */ | |
353 | unsigned long flags; | |
354 | ||
355 | /* node for active/idle bfqq list inside parent bfqd */ | |
356 | struct list_head bfqq_list; | |
357 | ||
358 | /* associated @bfq_ttime struct */ | |
359 | struct bfq_ttime ttime; | |
360 | ||
361 | /* bit vector: a 1 for each seeky requests in history */ | |
362 | u32 seek_history; | |
e1b2324d AA |
363 | |
364 | /* node for the device's burst list */ | |
365 | struct hlist_node burst_list_node; | |
366 | ||
aee69d78 PV |
367 | /* position of the last request enqueued */ |
368 | sector_t last_request_pos; | |
369 | ||
370 | /* Number of consecutive pairs of request completion and | |
371 | * arrival, such that the queue becomes idle after the | |
372 | * completion, but the next request arrives within an idle | |
373 | * time slice; used only if the queue's IO_bound flag has been | |
374 | * cleared. | |
375 | */ | |
376 | unsigned int requests_within_timer; | |
377 | ||
378 | /* pid of the process owning the queue, used for logging purposes */ | |
379 | pid_t pid; | |
44e44a1b | 380 | |
36eca894 AA |
381 | /* |
382 | * Pointer to the bfq_io_cq owning the bfq_queue, set to %NULL | |
383 | * if the queue is shared. | |
384 | */ | |
385 | struct bfq_io_cq *bic; | |
386 | ||
44e44a1b PV |
387 | /* current maximum weight-raising time for this queue */ |
388 | unsigned long wr_cur_max_time; | |
77b7dcea PV |
389 | /* |
390 | * Minimum time instant such that, only if a new request is | |
391 | * enqueued after this time instant in an idle @bfq_queue with | |
392 | * no outstanding requests, then the task associated with the | |
393 | * queue it is deemed as soft real-time (see the comments on | |
394 | * the function bfq_bfqq_softrt_next_start()) | |
395 | */ | |
396 | unsigned long soft_rt_next_start; | |
44e44a1b PV |
397 | /* |
398 | * Start time of the current weight-raising period if | |
399 | * the @bfq-queue is being weight-raised, otherwise | |
400 | * finish time of the last weight-raising period. | |
401 | */ | |
402 | unsigned long last_wr_start_finish; | |
403 | /* factor by which the weight of this queue is multiplied */ | |
404 | unsigned int wr_coeff; | |
77b7dcea PV |
405 | /* |
406 | * Time of the last transition of the @bfq_queue from idle to | |
407 | * backlogged. | |
408 | */ | |
409 | unsigned long last_idle_bklogged; | |
410 | /* | |
411 | * Cumulative service received from the @bfq_queue since the | |
412 | * last transition from idle to backlogged. | |
413 | */ | |
414 | unsigned long service_from_backlogged; | |
36eca894 | 415 | |
77b7dcea PV |
416 | /* |
417 | * Value of wr start time when switching to soft rt | |
418 | */ | |
419 | unsigned long wr_start_at_switch_to_srt; | |
36eca894 AA |
420 | |
421 | unsigned long split_time; /* time of last split */ | |
aee69d78 PV |
422 | }; |
423 | ||
424 | /** | |
425 | * struct bfq_io_cq - per (request_queue, io_context) structure. | |
426 | */ | |
427 | struct bfq_io_cq { | |
428 | /* associated io_cq structure */ | |
429 | struct io_cq icq; /* must be the first member */ | |
430 | /* array of two process queues, the sync and the async */ | |
431 | struct bfq_queue *bfqq[2]; | |
432 | /* per (request_queue, blkcg) ioprio */ | |
433 | int ioprio; | |
e21b7a0b AA |
434 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
435 | uint64_t blkcg_serial_nr; /* the current blkcg serial */ | |
436 | #endif | |
36eca894 AA |
437 | /* |
438 | * Snapshot of the idle window before merging; taken to | |
439 | * remember this value while the queue is merged, so as to be | |
440 | * able to restore it in case of split. | |
441 | */ | |
442 | bool saved_idle_window; | |
443 | /* | |
444 | * Same purpose as the previous two fields for the I/O bound | |
445 | * classification of a queue. | |
446 | */ | |
447 | bool saved_IO_bound; | |
448 | ||
e1b2324d AA |
449 | /* |
450 | * Same purpose as the previous fields for the value of the | |
451 | * field keeping the queue's belonging to a large burst | |
452 | */ | |
453 | bool saved_in_large_burst; | |
454 | /* | |
455 | * True if the queue belonged to a burst list before its merge | |
456 | * with another cooperating queue. | |
457 | */ | |
458 | bool was_in_burst_list; | |
459 | ||
36eca894 AA |
460 | /* |
461 | * Similar to previous fields: save wr information. | |
462 | */ | |
463 | unsigned long saved_wr_coeff; | |
464 | unsigned long saved_last_wr_start_finish; | |
465 | unsigned long saved_wr_start_at_switch_to_srt; | |
466 | unsigned int saved_wr_cur_max_time; | |
467 | struct bfq_ttime saved_ttime; | |
aee69d78 PV |
468 | }; |
469 | ||
44e44a1b PV |
470 | enum bfq_device_speed { |
471 | BFQ_BFQD_FAST, | |
472 | BFQ_BFQD_SLOW, | |
473 | }; | |
474 | ||
aee69d78 PV |
475 | /** |
476 | * struct bfq_data - per-device data structure. | |
477 | * | |
478 | * All the fields are protected by @lock. | |
479 | */ | |
480 | struct bfq_data { | |
481 | /* device request queue */ | |
482 | struct request_queue *queue; | |
483 | /* dispatch queue */ | |
484 | struct list_head dispatch; | |
485 | ||
e21b7a0b AA |
486 | /* root bfq_group for the device */ |
487 | struct bfq_group *root_group; | |
aee69d78 | 488 | |
1de0c4cd AA |
489 | /* |
490 | * rbtree of weight counters of @bfq_queues, sorted by | |
491 | * weight. Used to keep track of whether all @bfq_queues have | |
492 | * the same weight. The tree contains one counter for each | |
493 | * distinct weight associated to some active and not | |
494 | * weight-raised @bfq_queue (see the comments to the functions | |
495 | * bfq_weights_tree_[add|remove] for further details). | |
496 | */ | |
497 | struct rb_root queue_weights_tree; | |
498 | /* | |
499 | * rbtree of non-queue @bfq_entity weight counters, sorted by | |
500 | * weight. Used to keep track of whether all @bfq_groups have | |
501 | * the same weight. The tree contains one counter for each | |
502 | * distinct weight associated to some active @bfq_group (see | |
503 | * the comments to the functions bfq_weights_tree_[add|remove] | |
504 | * for further details). | |
505 | */ | |
506 | struct rb_root group_weights_tree; | |
507 | ||
aee69d78 PV |
508 | /* |
509 | * Number of bfq_queues containing requests (including the | |
510 | * queue in service, even if it is idling). | |
511 | */ | |
512 | int busy_queues; | |
cfd69712 PV |
513 | /* number of weight-raised busy @bfq_queues */ |
514 | int wr_busy_queues; | |
aee69d78 PV |
515 | /* number of queued requests */ |
516 | int queued; | |
517 | /* number of requests dispatched and waiting for completion */ | |
518 | int rq_in_driver; | |
519 | ||
520 | /* | |
521 | * Maximum number of requests in driver in the last | |
522 | * @hw_tag_samples completed requests. | |
523 | */ | |
524 | int max_rq_in_driver; | |
525 | /* number of samples used to calculate hw_tag */ | |
526 | int hw_tag_samples; | |
527 | /* flag set to one if the driver is showing a queueing behavior */ | |
528 | int hw_tag; | |
529 | ||
530 | /* number of budgets assigned */ | |
531 | int budgets_assigned; | |
532 | ||
533 | /* | |
534 | * Timer set when idling (waiting) for the next request from | |
535 | * the queue in service. | |
536 | */ | |
537 | struct hrtimer idle_slice_timer; | |
538 | ||
539 | /* bfq_queue in service */ | |
540 | struct bfq_queue *in_service_queue; | |
aee69d78 PV |
541 | |
542 | /* on-disk position of the last served request */ | |
543 | sector_t last_position; | |
544 | ||
ab0e43e9 PV |
545 | /* time of last request completion (ns) */ |
546 | u64 last_completion; | |
547 | ||
548 | /* time of first rq dispatch in current observation interval (ns) */ | |
549 | u64 first_dispatch; | |
550 | /* time of last rq dispatch in current observation interval (ns) */ | |
551 | u64 last_dispatch; | |
552 | ||
aee69d78 PV |
553 | /* beginning of the last budget */ |
554 | ktime_t last_budget_start; | |
555 | /* beginning of the last idle slice */ | |
556 | ktime_t last_idling_start; | |
ab0e43e9 PV |
557 | |
558 | /* number of samples in current observation interval */ | |
aee69d78 | 559 | int peak_rate_samples; |
ab0e43e9 PV |
560 | /* num of samples of seq dispatches in current observation interval */ |
561 | u32 sequential_samples; | |
562 | /* total num of sectors transferred in current observation interval */ | |
563 | u64 tot_sectors_dispatched; | |
564 | /* max rq size seen during current observation interval (sectors) */ | |
565 | u32 last_rq_max_size; | |
566 | /* time elapsed from first dispatch in current observ. interval (us) */ | |
567 | u64 delta_from_first; | |
aee69d78 | 568 | /* |
ab0e43e9 PV |
569 | * Current estimate of the device peak rate, measured in |
570 | * [BFQ_RATE_SHIFT * sectors/usec]. The left-shift by | |
571 | * BFQ_RATE_SHIFT is performed to increase precision in | |
aee69d78 PV |
572 | * fixed-point calculations. |
573 | */ | |
ab0e43e9 PV |
574 | u32 peak_rate; |
575 | ||
aee69d78 PV |
576 | /* maximum budget allotted to a bfq_queue before rescheduling */ |
577 | int bfq_max_budget; | |
578 | ||
579 | /* list of all the bfq_queues active on the device */ | |
580 | struct list_head active_list; | |
581 | /* list of all the bfq_queues idle on the device */ | |
582 | struct list_head idle_list; | |
583 | ||
584 | /* | |
585 | * Timeout for async/sync requests; when it fires, requests | |
586 | * are served in fifo order. | |
587 | */ | |
588 | u64 bfq_fifo_expire[2]; | |
589 | /* weight of backward seeks wrt forward ones */ | |
590 | unsigned int bfq_back_penalty; | |
591 | /* maximum allowed backward seek */ | |
592 | unsigned int bfq_back_max; | |
593 | /* maximum idling time */ | |
594 | u32 bfq_slice_idle; | |
aee69d78 PV |
595 | |
596 | /* user-configured max budget value (0 for auto-tuning) */ | |
597 | int bfq_user_max_budget; | |
598 | /* | |
599 | * Timeout for bfq_queues to consume their budget; used to | |
600 | * prevent seeky queues from imposing long latencies to | |
601 | * sequential or quasi-sequential ones (this also implies that | |
602 | * seeky queues cannot receive guarantees in the service | |
603 | * domain; after a timeout they are charged for the time they | |
604 | * have been in service, to preserve fairness among them, but | |
605 | * without service-domain guarantees). | |
606 | */ | |
607 | unsigned int bfq_timeout; | |
608 | ||
609 | /* | |
610 | * Number of consecutive requests that must be issued within | |
611 | * the idle time slice to set again idling to a queue which | |
612 | * was marked as non-I/O-bound (see the definition of the | |
613 | * IO_bound flag for further details). | |
614 | */ | |
615 | unsigned int bfq_requests_within_timer; | |
616 | ||
617 | /* | |
618 | * Force device idling whenever needed to provide accurate | |
619 | * service guarantees, without caring about throughput | |
620 | * issues. CAVEAT: this may even increase latencies, in case | |
621 | * of useless idling for processes that did stop doing I/O. | |
622 | */ | |
623 | bool strict_guarantees; | |
624 | ||
e1b2324d AA |
625 | /* |
626 | * Last time at which a queue entered the current burst of | |
627 | * queues being activated shortly after each other; for more | |
628 | * details about this and the following parameters related to | |
629 | * a burst of activations, see the comments on the function | |
630 | * bfq_handle_burst. | |
631 | */ | |
632 | unsigned long last_ins_in_burst; | |
633 | /* | |
634 | * Reference time interval used to decide whether a queue has | |
635 | * been activated shortly after @last_ins_in_burst. | |
636 | */ | |
637 | unsigned long bfq_burst_interval; | |
638 | /* number of queues in the current burst of queue activations */ | |
639 | int burst_size; | |
640 | ||
641 | /* common parent entity for the queues in the burst */ | |
642 | struct bfq_entity *burst_parent_entity; | |
643 | /* Maximum burst size above which the current queue-activation | |
644 | * burst is deemed as 'large'. | |
645 | */ | |
646 | unsigned long bfq_large_burst_thresh; | |
647 | /* true if a large queue-activation burst is in progress */ | |
648 | bool large_burst; | |
649 | /* | |
650 | * Head of the burst list (as for the above fields, more | |
651 | * details in the comments on the function bfq_handle_burst). | |
652 | */ | |
653 | struct hlist_head burst_list; | |
654 | ||
44e44a1b PV |
655 | /* if set to true, low-latency heuristics are enabled */ |
656 | bool low_latency; | |
657 | /* | |
658 | * Maximum factor by which the weight of a weight-raised queue | |
659 | * is multiplied. | |
660 | */ | |
661 | unsigned int bfq_wr_coeff; | |
662 | /* maximum duration of a weight-raising period (jiffies) */ | |
663 | unsigned int bfq_wr_max_time; | |
77b7dcea PV |
664 | |
665 | /* Maximum weight-raising duration for soft real-time processes */ | |
666 | unsigned int bfq_wr_rt_max_time; | |
44e44a1b PV |
667 | /* |
668 | * Minimum idle period after which weight-raising may be | |
669 | * reactivated for a queue (in jiffies). | |
670 | */ | |
671 | unsigned int bfq_wr_min_idle_time; | |
672 | /* | |
673 | * Minimum period between request arrivals after which | |
674 | * weight-raising may be reactivated for an already busy async | |
675 | * queue (in jiffies). | |
676 | */ | |
677 | unsigned long bfq_wr_min_inter_arr_async; | |
77b7dcea PV |
678 | |
679 | /* Max service-rate for a soft real-time queue, in sectors/sec */ | |
680 | unsigned int bfq_wr_max_softrt_rate; | |
44e44a1b PV |
681 | /* |
682 | * Cached value of the product R*T, used for computing the | |
683 | * maximum duration of weight raising automatically. | |
684 | */ | |
685 | u64 RT_prod; | |
686 | /* device-speed class for the low-latency heuristic */ | |
687 | enum bfq_device_speed device_speed; | |
688 | ||
aee69d78 PV |
689 | /* fallback dummy bfqq for extreme OOM conditions */ |
690 | struct bfq_queue oom_bfqq; | |
691 | ||
692 | spinlock_t lock; | |
693 | ||
694 | /* | |
695 | * bic associated with the task issuing current bio for | |
696 | * merging. This and the next field are used as a support to | |
697 | * be able to perform the bic lookup, needed by bio-merge | |
698 | * functions, before the scheduler lock is taken, and thus | |
699 | * avoid taking the request-queue lock while the scheduler | |
700 | * lock is being held. | |
701 | */ | |
702 | struct bfq_io_cq *bio_bic; | |
703 | /* bfqq associated with the task issuing current bio for merging */ | |
704 | struct bfq_queue *bio_bfqq; | |
705 | }; | |
706 | ||
707 | enum bfqq_state_flags { | |
e1b2324d AA |
708 | BFQQF_just_created = 0, /* queue just allocated */ |
709 | BFQQF_busy, /* has requests or is in service */ | |
aee69d78 PV |
710 | BFQQF_wait_request, /* waiting for a request */ |
711 | BFQQF_non_blocking_wait_rq, /* | |
712 | * waiting for a request | |
713 | * without idling the device | |
714 | */ | |
715 | BFQQF_fifo_expire, /* FIFO checked in this slice */ | |
716 | BFQQF_idle_window, /* slice idling enabled */ | |
717 | BFQQF_sync, /* synchronous queue */ | |
aee69d78 PV |
718 | BFQQF_IO_bound, /* |
719 | * bfqq has timed-out at least once | |
720 | * having consumed at most 2/10 of | |
721 | * its budget | |
722 | */ | |
e1b2324d AA |
723 | BFQQF_in_large_burst, /* |
724 | * bfqq activated in a large burst, | |
725 | * see comments to bfq_handle_burst. | |
726 | */ | |
77b7dcea PV |
727 | BFQQF_softrt_update, /* |
728 | * may need softrt-next-start | |
729 | * update | |
730 | */ | |
36eca894 AA |
731 | BFQQF_coop, /* bfqq is shared */ |
732 | BFQQF_split_coop /* shared bfqq will be split */ | |
aee69d78 PV |
733 | }; |
734 | ||
735 | #define BFQ_BFQQ_FNS(name) \ | |
736 | static void bfq_mark_bfqq_##name(struct bfq_queue *bfqq) \ | |
737 | { \ | |
738 | __set_bit(BFQQF_##name, &(bfqq)->flags); \ | |
739 | } \ | |
740 | static void bfq_clear_bfqq_##name(struct bfq_queue *bfqq) \ | |
741 | { \ | |
742 | __clear_bit(BFQQF_##name, &(bfqq)->flags); \ | |
743 | } \ | |
744 | static int bfq_bfqq_##name(const struct bfq_queue *bfqq) \ | |
745 | { \ | |
746 | return test_bit(BFQQF_##name, &(bfqq)->flags); \ | |
747 | } | |
748 | ||
e1b2324d | 749 | BFQ_BFQQ_FNS(just_created); |
aee69d78 PV |
750 | BFQ_BFQQ_FNS(busy); |
751 | BFQ_BFQQ_FNS(wait_request); | |
752 | BFQ_BFQQ_FNS(non_blocking_wait_rq); | |
753 | BFQ_BFQQ_FNS(fifo_expire); | |
754 | BFQ_BFQQ_FNS(idle_window); | |
755 | BFQ_BFQQ_FNS(sync); | |
aee69d78 | 756 | BFQ_BFQQ_FNS(IO_bound); |
e1b2324d | 757 | BFQ_BFQQ_FNS(in_large_burst); |
36eca894 AA |
758 | BFQ_BFQQ_FNS(coop); |
759 | BFQ_BFQQ_FNS(split_coop); | |
77b7dcea | 760 | BFQ_BFQQ_FNS(softrt_update); |
aee69d78 PV |
761 | #undef BFQ_BFQQ_FNS |
762 | ||
763 | /* Logging facilities. */ | |
e21b7a0b AA |
764 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
765 | static struct bfq_group *bfqq_group(struct bfq_queue *bfqq); | |
766 | static struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg); | |
767 | ||
768 | #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do { \ | |
769 | char __pbuf[128]; \ | |
770 | \ | |
771 | blkg_path(bfqg_to_blkg(bfqq_group(bfqq)), __pbuf, sizeof(__pbuf)); \ | |
772 | blk_add_trace_msg((bfqd)->queue, "bfq%d%c %s " fmt, (bfqq)->pid, \ | |
773 | bfq_bfqq_sync((bfqq)) ? 'S' : 'A', \ | |
774 | __pbuf, ##args); \ | |
775 | } while (0) | |
776 | ||
777 | #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do { \ | |
778 | char __pbuf[128]; \ | |
779 | \ | |
780 | blkg_path(bfqg_to_blkg(bfqg), __pbuf, sizeof(__pbuf)); \ | |
781 | blk_add_trace_msg((bfqd)->queue, "%s " fmt, __pbuf, ##args); \ | |
782 | } while (0) | |
783 | ||
784 | #else /* CONFIG_BFQ_GROUP_IOSCHED */ | |
785 | ||
786 | #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \ | |
787 | blk_add_trace_msg((bfqd)->queue, "bfq%d%c " fmt, (bfqq)->pid, \ | |
788 | bfq_bfqq_sync((bfqq)) ? 'S' : 'A', \ | |
789 | ##args) | |
790 | #define bfq_log_bfqg(bfqd, bfqg, fmt, args...) do {} while (0) | |
791 | ||
792 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ | |
aee69d78 PV |
793 | |
794 | #define bfq_log(bfqd, fmt, args...) \ | |
795 | blk_add_trace_msg((bfqd)->queue, "bfq " fmt, ##args) | |
796 | ||
797 | /* Expiration reasons. */ | |
798 | enum bfqq_expiration { | |
799 | BFQQE_TOO_IDLE = 0, /* | |
800 | * queue has been idling for | |
801 | * too long | |
802 | */ | |
803 | BFQQE_BUDGET_TIMEOUT, /* budget took too long to be used */ | |
804 | BFQQE_BUDGET_EXHAUSTED, /* budget consumed */ | |
805 | BFQQE_NO_MORE_REQUESTS, /* the queue has no more requests */ | |
806 | BFQQE_PREEMPTED /* preemption in progress */ | |
807 | }; | |
808 | ||
e21b7a0b AA |
809 | struct bfqg_stats { |
810 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
811 | /* number of ios merged */ | |
812 | struct blkg_rwstat merged; | |
813 | /* total time spent on device in ns, may not be accurate w/ queueing */ | |
814 | struct blkg_rwstat service_time; | |
815 | /* total time spent waiting in scheduler queue in ns */ | |
816 | struct blkg_rwstat wait_time; | |
817 | /* number of IOs queued up */ | |
818 | struct blkg_rwstat queued; | |
819 | /* total disk time and nr sectors dispatched by this group */ | |
820 | struct blkg_stat time; | |
821 | /* sum of number of ios queued across all samples */ | |
822 | struct blkg_stat avg_queue_size_sum; | |
823 | /* count of samples taken for average */ | |
824 | struct blkg_stat avg_queue_size_samples; | |
825 | /* how many times this group has been removed from service tree */ | |
826 | struct blkg_stat dequeue; | |
827 | /* total time spent waiting for it to be assigned a timeslice. */ | |
828 | struct blkg_stat group_wait_time; | |
829 | /* time spent idling for this blkcg_gq */ | |
830 | struct blkg_stat idle_time; | |
831 | /* total time with empty current active q with other requests queued */ | |
832 | struct blkg_stat empty_time; | |
833 | /* fields after this shouldn't be cleared on stat reset */ | |
834 | uint64_t start_group_wait_time; | |
835 | uint64_t start_idle_time; | |
836 | uint64_t start_empty_time; | |
837 | uint16_t flags; | |
838 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ | |
839 | }; | |
840 | ||
841 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
842 | ||
843 | /* | |
844 | * struct bfq_group_data - per-blkcg storage for the blkio subsystem. | |
845 | * | |
846 | * @ps: @blkcg_policy_storage that this structure inherits | |
847 | * @weight: weight of the bfq_group | |
848 | */ | |
849 | struct bfq_group_data { | |
850 | /* must be the first member */ | |
851 | struct blkcg_policy_data pd; | |
852 | ||
44e44a1b | 853 | unsigned int weight; |
e21b7a0b AA |
854 | }; |
855 | ||
856 | /** | |
857 | * struct bfq_group - per (device, cgroup) data structure. | |
858 | * @entity: schedulable entity to insert into the parent group sched_data. | |
859 | * @sched_data: own sched_data, to contain child entities (they may be | |
860 | * both bfq_queues and bfq_groups). | |
861 | * @bfqd: the bfq_data for the device this group acts upon. | |
862 | * @async_bfqq: array of async queues for all the tasks belonging to | |
863 | * the group, one queue per ioprio value per ioprio_class, | |
864 | * except for the idle class that has only one queue. | |
865 | * @async_idle_bfqq: async queue for the idle class (ioprio is ignored). | |
866 | * @my_entity: pointer to @entity, %NULL for the toplevel group; used | |
867 | * to avoid too many special cases during group creation/ | |
868 | * migration. | |
869 | * @stats: stats for this bfqg. | |
1de0c4cd AA |
870 | * @active_entities: number of active entities belonging to the group; |
871 | * unused for the root group. Used to know whether there | |
872 | * are groups with more than one active @bfq_entity | |
873 | * (see the comments to the function | |
874 | * bfq_bfqq_may_idle()). | |
36eca894 AA |
875 | * @rq_pos_tree: rbtree sorted by next_request position, used when |
876 | * determining if two or more queues have interleaving | |
877 | * requests (see bfq_find_close_cooperator()). | |
e21b7a0b AA |
878 | * |
879 | * Each (device, cgroup) pair has its own bfq_group, i.e., for each cgroup | |
880 | * there is a set of bfq_groups, each one collecting the lower-level | |
881 | * entities belonging to the group that are acting on the same device. | |
882 | * | |
883 | * Locking works as follows: | |
884 | * o @bfqd is protected by the queue lock, RCU is used to access it | |
885 | * from the readers. | |
886 | * o All the other fields are protected by the @bfqd queue lock. | |
887 | */ | |
888 | struct bfq_group { | |
889 | /* must be the first member */ | |
890 | struct blkg_policy_data pd; | |
891 | ||
892 | struct bfq_entity entity; | |
893 | struct bfq_sched_data sched_data; | |
894 | ||
895 | void *bfqd; | |
896 | ||
897 | struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR]; | |
898 | struct bfq_queue *async_idle_bfqq; | |
899 | ||
900 | struct bfq_entity *my_entity; | |
901 | ||
1de0c4cd AA |
902 | int active_entities; |
903 | ||
36eca894 AA |
904 | struct rb_root rq_pos_tree; |
905 | ||
e21b7a0b AA |
906 | struct bfqg_stats stats; |
907 | }; | |
908 | ||
909 | #else | |
910 | struct bfq_group { | |
911 | struct bfq_sched_data sched_data; | |
912 | ||
913 | struct bfq_queue *async_bfqq[2][IOPRIO_BE_NR]; | |
914 | struct bfq_queue *async_idle_bfqq; | |
915 | ||
916 | struct rb_root rq_pos_tree; | |
917 | }; | |
918 | #endif | |
919 | ||
aee69d78 PV |
920 | static struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity); |
921 | ||
e21b7a0b AA |
922 | static unsigned int bfq_class_idx(struct bfq_entity *entity) |
923 | { | |
924 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
925 | ||
926 | return bfqq ? bfqq->ioprio_class - 1 : | |
927 | BFQ_DEFAULT_GRP_CLASS - 1; | |
928 | } | |
929 | ||
aee69d78 PV |
930 | static struct bfq_service_tree * |
931 | bfq_entity_service_tree(struct bfq_entity *entity) | |
932 | { | |
933 | struct bfq_sched_data *sched_data = entity->sched_data; | |
e21b7a0b | 934 | unsigned int idx = bfq_class_idx(entity); |
aee69d78 PV |
935 | |
936 | return sched_data->service_tree + idx; | |
937 | } | |
938 | ||
939 | static struct bfq_queue *bic_to_bfqq(struct bfq_io_cq *bic, bool is_sync) | |
940 | { | |
941 | return bic->bfqq[is_sync]; | |
942 | } | |
943 | ||
944 | static void bic_set_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq, | |
945 | bool is_sync) | |
946 | { | |
947 | bic->bfqq[is_sync] = bfqq; | |
948 | } | |
949 | ||
950 | static struct bfq_data *bic_to_bfqd(struct bfq_io_cq *bic) | |
951 | { | |
952 | return bic->icq.q->elevator->elevator_data; | |
953 | } | |
954 | ||
36eca894 AA |
955 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
956 | ||
957 | static struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq) | |
958 | { | |
959 | struct bfq_entity *group_entity = bfqq->entity.parent; | |
960 | ||
961 | if (!group_entity) | |
962 | group_entity = &bfqq->bfqd->root_group->entity; | |
963 | ||
964 | return container_of(group_entity, struct bfq_group, entity); | |
965 | } | |
966 | ||
967 | #else | |
968 | ||
969 | static struct bfq_group *bfq_bfqq_to_bfqg(struct bfq_queue *bfqq) | |
970 | { | |
971 | return bfqq->bfqd->root_group; | |
972 | } | |
973 | ||
974 | #endif | |
975 | ||
aee69d78 PV |
976 | static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio); |
977 | static void bfq_put_queue(struct bfq_queue *bfqq); | |
978 | static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd, | |
979 | struct bio *bio, bool is_sync, | |
980 | struct bfq_io_cq *bic); | |
44e44a1b PV |
981 | static void bfq_end_wr_async_queues(struct bfq_data *bfqd, |
982 | struct bfq_group *bfqg); | |
e21b7a0b | 983 | static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg); |
aee69d78 PV |
984 | static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq); |
985 | ||
aee69d78 PV |
986 | /* Expiration time of sync (0) and async (1) requests, in ns. */ |
987 | static const u64 bfq_fifo_expire[2] = { NSEC_PER_SEC / 4, NSEC_PER_SEC / 8 }; | |
988 | ||
989 | /* Maximum backwards seek (magic number lifted from CFQ), in KiB. */ | |
990 | static const int bfq_back_max = 16 * 1024; | |
991 | ||
992 | /* Penalty of a backwards seek, in number of sectors. */ | |
993 | static const int bfq_back_penalty = 2; | |
994 | ||
995 | /* Idling period duration, in ns. */ | |
996 | static u64 bfq_slice_idle = NSEC_PER_SEC / 125; | |
997 | ||
998 | /* Minimum number of assigned budgets for which stats are safe to compute. */ | |
999 | static const int bfq_stats_min_budgets = 194; | |
1000 | ||
1001 | /* Default maximum budget values, in sectors and number of requests. */ | |
1002 | static const int bfq_default_max_budget = 16 * 1024; | |
1003 | ||
c074170e PV |
1004 | /* |
1005 | * Async to sync throughput distribution is controlled as follows: | |
1006 | * when an async request is served, the entity is charged the number | |
1007 | * of sectors of the request, multiplied by the factor below | |
1008 | */ | |
1009 | static const int bfq_async_charge_factor = 10; | |
1010 | ||
aee69d78 PV |
1011 | /* Default timeout values, in jiffies, approximating CFQ defaults. */ |
1012 | static const int bfq_timeout = HZ / 8; | |
1013 | ||
1014 | static struct kmem_cache *bfq_pool; | |
1015 | ||
ab0e43e9 | 1016 | /* Below this threshold (in ns), we consider thinktime immediate. */ |
aee69d78 PV |
1017 | #define BFQ_MIN_TT (2 * NSEC_PER_MSEC) |
1018 | ||
1019 | /* hw_tag detection: parallel requests threshold and min samples needed. */ | |
1020 | #define BFQ_HW_QUEUE_THRESHOLD 4 | |
1021 | #define BFQ_HW_QUEUE_SAMPLES 32 | |
1022 | ||
1023 | #define BFQQ_SEEK_THR (sector_t)(8 * 100) | |
1024 | #define BFQQ_SECT_THR_NONROT (sector_t)(2 * 32) | |
1025 | #define BFQQ_CLOSE_THR (sector_t)(8 * 1024) | |
1026 | #define BFQQ_SEEKY(bfqq) (hweight32(bfqq->seek_history) > 32/8) | |
1027 | ||
ab0e43e9 PV |
1028 | /* Min number of samples required to perform peak-rate update */ |
1029 | #define BFQ_RATE_MIN_SAMPLES 32 | |
1030 | /* Min observation time interval required to perform a peak-rate update (ns) */ | |
1031 | #define BFQ_RATE_MIN_INTERVAL (300*NSEC_PER_MSEC) | |
1032 | /* Target observation time interval for a peak-rate update (ns) */ | |
1033 | #define BFQ_RATE_REF_INTERVAL NSEC_PER_SEC | |
aee69d78 PV |
1034 | |
1035 | /* Shift used for peak rate fixed precision calculations. */ | |
1036 | #define BFQ_RATE_SHIFT 16 | |
1037 | ||
44e44a1b PV |
1038 | /* |
1039 | * By default, BFQ computes the duration of the weight raising for | |
1040 | * interactive applications automatically, using the following formula: | |
1041 | * duration = (R / r) * T, where r is the peak rate of the device, and | |
1042 | * R and T are two reference parameters. | |
1043 | * In particular, R is the peak rate of the reference device (see below), | |
1044 | * and T is a reference time: given the systems that are likely to be | |
1045 | * installed on the reference device according to its speed class, T is | |
1046 | * about the maximum time needed, under BFQ and while reading two files in | |
1047 | * parallel, to load typical large applications on these systems. | |
1048 | * In practice, the slower/faster the device at hand is, the more/less it | |
1049 | * takes to load applications with respect to the reference device. | |
1050 | * Accordingly, the longer/shorter BFQ grants weight raising to interactive | |
1051 | * applications. | |
1052 | * | |
1053 | * BFQ uses four different reference pairs (R, T), depending on: | |
1054 | * . whether the device is rotational or non-rotational; | |
1055 | * . whether the device is slow, such as old or portable HDDs, as well as | |
1056 | * SD cards, or fast, such as newer HDDs and SSDs. | |
1057 | * | |
1058 | * The device's speed class is dynamically (re)detected in | |
1059 | * bfq_update_peak_rate() every time the estimated peak rate is updated. | |
1060 | * | |
1061 | * In the following definitions, R_slow[0]/R_fast[0] and | |
1062 | * T_slow[0]/T_fast[0] are the reference values for a slow/fast | |
1063 | * rotational device, whereas R_slow[1]/R_fast[1] and | |
1064 | * T_slow[1]/T_fast[1] are the reference values for a slow/fast | |
1065 | * non-rotational device. Finally, device_speed_thresh are the | |
1066 | * thresholds used to switch between speed classes. The reference | |
1067 | * rates are not the actual peak rates of the devices used as a | |
1068 | * reference, but slightly lower values. The reason for using these | |
1069 | * slightly lower values is that the peak-rate estimator tends to | |
1070 | * yield slightly lower values than the actual peak rate (it can yield | |
1071 | * the actual peak rate only if there is only one process doing I/O, | |
1072 | * and the process does sequential I/O). | |
1073 | * | |
1074 | * Both the reference peak rates and the thresholds are measured in | |
1075 | * sectors/usec, left-shifted by BFQ_RATE_SHIFT. | |
1076 | */ | |
1077 | static int R_slow[2] = {1000, 10700}; | |
1078 | static int R_fast[2] = {14000, 33000}; | |
1079 | /* | |
1080 | * To improve readability, a conversion function is used to initialize the | |
1081 | * following arrays, which entails that they can be initialized only in a | |
1082 | * function. | |
1083 | */ | |
1084 | static int T_slow[2]; | |
1085 | static int T_fast[2]; | |
1086 | static int device_speed_thresh[2]; | |
1087 | ||
aee69d78 PV |
1088 | #define BFQ_SERVICE_TREE_INIT ((struct bfq_service_tree) \ |
1089 | { RB_ROOT, RB_ROOT, NULL, NULL, 0, 0 }) | |
1090 | ||
1091 | #define RQ_BIC(rq) ((struct bfq_io_cq *) (rq)->elv.priv[0]) | |
1092 | #define RQ_BFQQ(rq) ((rq)->elv.priv[1]) | |
1093 | ||
1094 | /** | |
1095 | * icq_to_bic - convert iocontext queue structure to bfq_io_cq. | |
1096 | * @icq: the iocontext queue. | |
1097 | */ | |
1098 | static struct bfq_io_cq *icq_to_bic(struct io_cq *icq) | |
1099 | { | |
1100 | /* bic->icq is the first member, %NULL will convert to %NULL */ | |
1101 | return container_of(icq, struct bfq_io_cq, icq); | |
1102 | } | |
1103 | ||
1104 | /** | |
1105 | * bfq_bic_lookup - search into @ioc a bic associated to @bfqd. | |
1106 | * @bfqd: the lookup key. | |
1107 | * @ioc: the io_context of the process doing I/O. | |
1108 | * @q: the request queue. | |
1109 | */ | |
1110 | static struct bfq_io_cq *bfq_bic_lookup(struct bfq_data *bfqd, | |
1111 | struct io_context *ioc, | |
1112 | struct request_queue *q) | |
1113 | { | |
1114 | if (ioc) { | |
1115 | unsigned long flags; | |
1116 | struct bfq_io_cq *icq; | |
1117 | ||
1118 | spin_lock_irqsave(q->queue_lock, flags); | |
1119 | icq = icq_to_bic(ioc_lookup_icq(ioc, q)); | |
1120 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1121 | ||
1122 | return icq; | |
1123 | } | |
1124 | ||
1125 | return NULL; | |
1126 | } | |
1127 | ||
1128 | /* | |
e21b7a0b AA |
1129 | * Scheduler run of queue, if there are requests pending and no one in the |
1130 | * driver that will restart queueing. | |
1131 | */ | |
1132 | static void bfq_schedule_dispatch(struct bfq_data *bfqd) | |
1133 | { | |
1134 | if (bfqd->queued != 0) { | |
1135 | bfq_log(bfqd, "schedule dispatch"); | |
1136 | blk_mq_run_hw_queues(bfqd->queue, true); | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | /** | |
1141 | * bfq_gt - compare two timestamps. | |
1142 | * @a: first ts. | |
1143 | * @b: second ts. | |
1144 | * | |
1145 | * Return @a > @b, dealing with wrapping correctly. | |
1146 | */ | |
1147 | static int bfq_gt(u64 a, u64 b) | |
1148 | { | |
1149 | return (s64)(a - b) > 0; | |
1150 | } | |
1151 | ||
1152 | static struct bfq_entity *bfq_root_active_entity(struct rb_root *tree) | |
1153 | { | |
1154 | struct rb_node *node = tree->rb_node; | |
1155 | ||
1156 | return rb_entry(node, struct bfq_entity, rb_node); | |
1157 | } | |
1158 | ||
1159 | static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd); | |
1160 | ||
1161 | static bool bfq_update_parent_budget(struct bfq_entity *next_in_service); | |
1162 | ||
1163 | /** | |
1164 | * bfq_update_next_in_service - update sd->next_in_service | |
1165 | * @sd: sched_data for which to perform the update. | |
1166 | * @new_entity: if not NULL, pointer to the entity whose activation, | |
1167 | * requeueing or repositionig triggered the invocation of | |
1168 | * this function. | |
1169 | * | |
1170 | * This function is called to update sd->next_in_service, which, in | |
1171 | * its turn, may change as a consequence of the insertion or | |
1172 | * extraction of an entity into/from one of the active trees of | |
1173 | * sd. These insertions/extractions occur as a consequence of | |
1174 | * activations/deactivations of entities, with some activations being | |
1175 | * 'true' activations, and other activations being requeueings (i.e., | |
1176 | * implementing the second, requeueing phase of the mechanism used to | |
1177 | * reposition an entity in its active tree; see comments on | |
1178 | * __bfq_activate_entity and __bfq_requeue_entity for details). In | |
1179 | * both the last two activation sub-cases, new_entity points to the | |
1180 | * just activated or requeued entity. | |
1181 | * | |
1182 | * Returns true if sd->next_in_service changes in such a way that | |
1183 | * entity->parent may become the next_in_service for its parent | |
1184 | * entity. | |
aee69d78 | 1185 | */ |
e21b7a0b AA |
1186 | static bool bfq_update_next_in_service(struct bfq_sched_data *sd, |
1187 | struct bfq_entity *new_entity) | |
1188 | { | |
1189 | struct bfq_entity *next_in_service = sd->next_in_service; | |
1190 | bool parent_sched_may_change = false; | |
1191 | ||
1192 | /* | |
1193 | * If this update is triggered by the activation, requeueing | |
1194 | * or repositiong of an entity that does not coincide with | |
1195 | * sd->next_in_service, then a full lookup in the active tree | |
1196 | * can be avoided. In fact, it is enough to check whether the | |
1197 | * just-modified entity has a higher priority than | |
1198 | * sd->next_in_service, or, even if it has the same priority | |
1199 | * as sd->next_in_service, is eligible and has a lower virtual | |
1200 | * finish time than sd->next_in_service. If this compound | |
1201 | * condition holds, then the new entity becomes the new | |
1202 | * next_in_service. Otherwise no change is needed. | |
1203 | */ | |
1204 | if (new_entity && new_entity != sd->next_in_service) { | |
1205 | /* | |
1206 | * Flag used to decide whether to replace | |
1207 | * sd->next_in_service with new_entity. Tentatively | |
1208 | * set to true, and left as true if | |
1209 | * sd->next_in_service is NULL. | |
1210 | */ | |
1211 | bool replace_next = true; | |
1212 | ||
1213 | /* | |
1214 | * If there is already a next_in_service candidate | |
1215 | * entity, then compare class priorities or timestamps | |
1216 | * to decide whether to replace sd->service_tree with | |
1217 | * new_entity. | |
1218 | */ | |
1219 | if (next_in_service) { | |
1220 | unsigned int new_entity_class_idx = | |
1221 | bfq_class_idx(new_entity); | |
1222 | struct bfq_service_tree *st = | |
1223 | sd->service_tree + new_entity_class_idx; | |
1224 | ||
1225 | /* | |
1226 | * For efficiency, evaluate the most likely | |
1227 | * sub-condition first. | |
1228 | */ | |
1229 | replace_next = | |
1230 | (new_entity_class_idx == | |
1231 | bfq_class_idx(next_in_service) | |
1232 | && | |
1233 | !bfq_gt(new_entity->start, st->vtime) | |
1234 | && | |
1235 | bfq_gt(next_in_service->finish, | |
1236 | new_entity->finish)) | |
1237 | || | |
1238 | new_entity_class_idx < | |
1239 | bfq_class_idx(next_in_service); | |
1240 | } | |
1241 | ||
1242 | if (replace_next) | |
1243 | next_in_service = new_entity; | |
1244 | } else /* invoked because of a deactivation: lookup needed */ | |
1245 | next_in_service = bfq_lookup_next_entity(sd); | |
1246 | ||
1247 | if (next_in_service) { | |
1248 | parent_sched_may_change = !sd->next_in_service || | |
1249 | bfq_update_parent_budget(next_in_service); | |
1250 | } | |
1251 | ||
1252 | sd->next_in_service = next_in_service; | |
1253 | ||
1254 | if (!next_in_service) | |
1255 | return parent_sched_may_change; | |
1256 | ||
1257 | return parent_sched_may_change; | |
1258 | } | |
1259 | ||
1260 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
1261 | /* both next loops stop at one of the child entities of the root group */ | |
aee69d78 | 1262 | #define for_each_entity(entity) \ |
e21b7a0b | 1263 | for (; entity ; entity = entity->parent) |
aee69d78 | 1264 | |
e21b7a0b AA |
1265 | /* |
1266 | * For each iteration, compute parent in advance, so as to be safe if | |
1267 | * entity is deallocated during the iteration. Such a deallocation may | |
1268 | * happen as a consequence of a bfq_put_queue that frees the bfq_queue | |
1269 | * containing entity. | |
1270 | */ | |
aee69d78 | 1271 | #define for_each_entity_safe(entity, parent) \ |
e21b7a0b | 1272 | for (; entity && ({ parent = entity->parent; 1; }); entity = parent) |
aee69d78 | 1273 | |
e21b7a0b AA |
1274 | /* |
1275 | * Returns true if this budget changes may let next_in_service->parent | |
1276 | * become the next_in_service entity for its parent entity. | |
1277 | */ | |
1278 | static bool bfq_update_parent_budget(struct bfq_entity *next_in_service) | |
aee69d78 | 1279 | { |
e21b7a0b AA |
1280 | struct bfq_entity *bfqg_entity; |
1281 | struct bfq_group *bfqg; | |
1282 | struct bfq_sched_data *group_sd; | |
1283 | bool ret = false; | |
1284 | ||
1285 | group_sd = next_in_service->sched_data; | |
1286 | ||
1287 | bfqg = container_of(group_sd, struct bfq_group, sched_data); | |
1288 | /* | |
1289 | * bfq_group's my_entity field is not NULL only if the group | |
1290 | * is not the root group. We must not touch the root entity | |
1291 | * as it must never become an in-service entity. | |
1292 | */ | |
1293 | bfqg_entity = bfqg->my_entity; | |
1294 | if (bfqg_entity) { | |
1295 | if (bfqg_entity->budget > next_in_service->budget) | |
1296 | ret = true; | |
1297 | bfqg_entity->budget = next_in_service->budget; | |
1298 | } | |
1299 | ||
1300 | return ret; | |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * This function tells whether entity stops being a candidate for next | |
1305 | * service, according to the following logic. | |
1306 | * | |
1307 | * This function is invoked for an entity that is about to be set in | |
1308 | * service. If such an entity is a queue, then the entity is no longer | |
1309 | * a candidate for next service (i.e, a candidate entity to serve | |
1310 | * after the in-service entity is expired). The function then returns | |
1311 | * true. | |
1de0c4cd AA |
1312 | * |
1313 | * In contrast, the entity could stil be a candidate for next service | |
1314 | * if it is not a queue, and has more than one child. In fact, even if | |
1315 | * one of its children is about to be set in service, other children | |
1316 | * may still be the next to serve. As a consequence, a non-queue | |
1317 | * entity is not a candidate for next-service only if it has only one | |
1318 | * child. And only if this condition holds, then the function returns | |
1319 | * true for a non-queue entity. | |
e21b7a0b AA |
1320 | */ |
1321 | static bool bfq_no_longer_next_in_service(struct bfq_entity *entity) | |
1322 | { | |
1de0c4cd AA |
1323 | struct bfq_group *bfqg; |
1324 | ||
e21b7a0b AA |
1325 | if (bfq_entity_to_bfqq(entity)) |
1326 | return true; | |
1327 | ||
1de0c4cd AA |
1328 | bfqg = container_of(entity, struct bfq_group, entity); |
1329 | ||
1330 | if (bfqg->active_entities == 1) | |
1331 | return true; | |
1332 | ||
e21b7a0b | 1333 | return false; |
aee69d78 PV |
1334 | } |
1335 | ||
e21b7a0b AA |
1336 | #else /* CONFIG_BFQ_GROUP_IOSCHED */ |
1337 | /* | |
1338 | * Next two macros are fake loops when cgroups support is not | |
1339 | * enabled. I fact, in such a case, there is only one level to go up | |
1340 | * (to reach the root group). | |
1341 | */ | |
1342 | #define for_each_entity(entity) \ | |
1343 | for (; entity ; entity = NULL) | |
1344 | ||
1345 | #define for_each_entity_safe(entity, parent) \ | |
1346 | for (parent = NULL; entity ; entity = parent) | |
1347 | ||
1348 | static bool bfq_update_parent_budget(struct bfq_entity *next_in_service) | |
aee69d78 | 1349 | { |
e21b7a0b | 1350 | return false; |
aee69d78 PV |
1351 | } |
1352 | ||
e21b7a0b | 1353 | static bool bfq_no_longer_next_in_service(struct bfq_entity *entity) |
aee69d78 | 1354 | { |
e21b7a0b | 1355 | return true; |
aee69d78 PV |
1356 | } |
1357 | ||
e21b7a0b AA |
1358 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ |
1359 | ||
aee69d78 PV |
1360 | /* |
1361 | * Shift for timestamp calculations. This actually limits the maximum | |
1362 | * service allowed in one timestamp delta (small shift values increase it), | |
1363 | * the maximum total weight that can be used for the queues in the system | |
1364 | * (big shift values increase it), and the period of virtual time | |
1365 | * wraparounds. | |
1366 | */ | |
1367 | #define WFQ_SERVICE_SHIFT 22 | |
1368 | ||
aee69d78 PV |
1369 | static struct bfq_queue *bfq_entity_to_bfqq(struct bfq_entity *entity) |
1370 | { | |
1371 | struct bfq_queue *bfqq = NULL; | |
1372 | ||
1373 | if (!entity->my_sched_data) | |
1374 | bfqq = container_of(entity, struct bfq_queue, entity); | |
1375 | ||
1376 | return bfqq; | |
1377 | } | |
1378 | ||
1379 | ||
1380 | /** | |
1381 | * bfq_delta - map service into the virtual time domain. | |
1382 | * @service: amount of service. | |
1383 | * @weight: scale factor (weight of an entity or weight sum). | |
1384 | */ | |
1385 | static u64 bfq_delta(unsigned long service, unsigned long weight) | |
1386 | { | |
1387 | u64 d = (u64)service << WFQ_SERVICE_SHIFT; | |
1388 | ||
1389 | do_div(d, weight); | |
1390 | return d; | |
1391 | } | |
1392 | ||
1393 | /** | |
1394 | * bfq_calc_finish - assign the finish time to an entity. | |
1395 | * @entity: the entity to act upon. | |
1396 | * @service: the service to be charged to the entity. | |
1397 | */ | |
1398 | static void bfq_calc_finish(struct bfq_entity *entity, unsigned long service) | |
1399 | { | |
1400 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1401 | ||
1402 | entity->finish = entity->start + | |
1403 | bfq_delta(service, entity->weight); | |
1404 | ||
1405 | if (bfqq) { | |
1406 | bfq_log_bfqq(bfqq->bfqd, bfqq, | |
1407 | "calc_finish: serv %lu, w %d", | |
1408 | service, entity->weight); | |
1409 | bfq_log_bfqq(bfqq->bfqd, bfqq, | |
1410 | "calc_finish: start %llu, finish %llu, delta %llu", | |
1411 | entity->start, entity->finish, | |
1412 | bfq_delta(service, entity->weight)); | |
1413 | } | |
1414 | } | |
1415 | ||
1416 | /** | |
1417 | * bfq_entity_of - get an entity from a node. | |
1418 | * @node: the node field of the entity. | |
1419 | * | |
1420 | * Convert a node pointer to the relative entity. This is used only | |
1421 | * to simplify the logic of some functions and not as the generic | |
1422 | * conversion mechanism because, e.g., in the tree walking functions, | |
1423 | * the check for a %NULL value would be redundant. | |
1424 | */ | |
1425 | static struct bfq_entity *bfq_entity_of(struct rb_node *node) | |
1426 | { | |
1427 | struct bfq_entity *entity = NULL; | |
1428 | ||
1429 | if (node) | |
1430 | entity = rb_entry(node, struct bfq_entity, rb_node); | |
1431 | ||
1432 | return entity; | |
1433 | } | |
1434 | ||
1435 | /** | |
1436 | * bfq_extract - remove an entity from a tree. | |
1437 | * @root: the tree root. | |
1438 | * @entity: the entity to remove. | |
1439 | */ | |
1440 | static void bfq_extract(struct rb_root *root, struct bfq_entity *entity) | |
1441 | { | |
1442 | entity->tree = NULL; | |
1443 | rb_erase(&entity->rb_node, root); | |
1444 | } | |
1445 | ||
1446 | /** | |
1447 | * bfq_idle_extract - extract an entity from the idle tree. | |
1448 | * @st: the service tree of the owning @entity. | |
1449 | * @entity: the entity being removed. | |
1450 | */ | |
1451 | static void bfq_idle_extract(struct bfq_service_tree *st, | |
1452 | struct bfq_entity *entity) | |
1453 | { | |
1454 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1455 | struct rb_node *next; | |
1456 | ||
1457 | if (entity == st->first_idle) { | |
1458 | next = rb_next(&entity->rb_node); | |
1459 | st->first_idle = bfq_entity_of(next); | |
1460 | } | |
1461 | ||
1462 | if (entity == st->last_idle) { | |
1463 | next = rb_prev(&entity->rb_node); | |
1464 | st->last_idle = bfq_entity_of(next); | |
1465 | } | |
1466 | ||
1467 | bfq_extract(&st->idle, entity); | |
1468 | ||
1469 | if (bfqq) | |
1470 | list_del(&bfqq->bfqq_list); | |
1471 | } | |
1472 | ||
1473 | /** | |
1474 | * bfq_insert - generic tree insertion. | |
1475 | * @root: tree root. | |
1476 | * @entity: entity to insert. | |
1477 | * | |
1478 | * This is used for the idle and the active tree, since they are both | |
1479 | * ordered by finish time. | |
1480 | */ | |
1481 | static void bfq_insert(struct rb_root *root, struct bfq_entity *entity) | |
1482 | { | |
1483 | struct bfq_entity *entry; | |
1484 | struct rb_node **node = &root->rb_node; | |
1485 | struct rb_node *parent = NULL; | |
1486 | ||
1487 | while (*node) { | |
1488 | parent = *node; | |
1489 | entry = rb_entry(parent, struct bfq_entity, rb_node); | |
1490 | ||
1491 | if (bfq_gt(entry->finish, entity->finish)) | |
1492 | node = &parent->rb_left; | |
1493 | else | |
1494 | node = &parent->rb_right; | |
1495 | } | |
1496 | ||
1497 | rb_link_node(&entity->rb_node, parent, node); | |
1498 | rb_insert_color(&entity->rb_node, root); | |
1499 | ||
1500 | entity->tree = root; | |
1501 | } | |
1502 | ||
1503 | /** | |
1504 | * bfq_update_min - update the min_start field of a entity. | |
1505 | * @entity: the entity to update. | |
1506 | * @node: one of its children. | |
1507 | * | |
1508 | * This function is called when @entity may store an invalid value for | |
1509 | * min_start due to updates to the active tree. The function assumes | |
1510 | * that the subtree rooted at @node (which may be its left or its right | |
1511 | * child) has a valid min_start value. | |
1512 | */ | |
1513 | static void bfq_update_min(struct bfq_entity *entity, struct rb_node *node) | |
1514 | { | |
1515 | struct bfq_entity *child; | |
1516 | ||
1517 | if (node) { | |
1518 | child = rb_entry(node, struct bfq_entity, rb_node); | |
1519 | if (bfq_gt(entity->min_start, child->min_start)) | |
1520 | entity->min_start = child->min_start; | |
1521 | } | |
1522 | } | |
1523 | ||
1524 | /** | |
1525 | * bfq_update_active_node - recalculate min_start. | |
1526 | * @node: the node to update. | |
1527 | * | |
1528 | * @node may have changed position or one of its children may have moved, | |
1529 | * this function updates its min_start value. The left and right subtrees | |
1530 | * are assumed to hold a correct min_start value. | |
1531 | */ | |
1532 | static void bfq_update_active_node(struct rb_node *node) | |
1533 | { | |
1534 | struct bfq_entity *entity = rb_entry(node, struct bfq_entity, rb_node); | |
1535 | ||
1536 | entity->min_start = entity->start; | |
1537 | bfq_update_min(entity, node->rb_right); | |
1538 | bfq_update_min(entity, node->rb_left); | |
1539 | } | |
1540 | ||
1541 | /** | |
1542 | * bfq_update_active_tree - update min_start for the whole active tree. | |
1543 | * @node: the starting node. | |
1544 | * | |
1545 | * @node must be the deepest modified node after an update. This function | |
1546 | * updates its min_start using the values held by its children, assuming | |
1547 | * that they did not change, and then updates all the nodes that may have | |
1548 | * changed in the path to the root. The only nodes that may have changed | |
1549 | * are the ones in the path or their siblings. | |
1550 | */ | |
1551 | static void bfq_update_active_tree(struct rb_node *node) | |
1552 | { | |
1553 | struct rb_node *parent; | |
1554 | ||
1555 | up: | |
1556 | bfq_update_active_node(node); | |
1557 | ||
1558 | parent = rb_parent(node); | |
1559 | if (!parent) | |
1560 | return; | |
1561 | ||
1562 | if (node == parent->rb_left && parent->rb_right) | |
1563 | bfq_update_active_node(parent->rb_right); | |
1564 | else if (parent->rb_left) | |
1565 | bfq_update_active_node(parent->rb_left); | |
1566 | ||
1567 | node = parent; | |
1568 | goto up; | |
1569 | } | |
1570 | ||
1de0c4cd AA |
1571 | static void bfq_weights_tree_add(struct bfq_data *bfqd, |
1572 | struct bfq_entity *entity, | |
1573 | struct rb_root *root); | |
1574 | ||
1575 | static void bfq_weights_tree_remove(struct bfq_data *bfqd, | |
1576 | struct bfq_entity *entity, | |
1577 | struct rb_root *root); | |
1578 | ||
1579 | ||
aee69d78 PV |
1580 | /** |
1581 | * bfq_active_insert - insert an entity in the active tree of its | |
1582 | * group/device. | |
1583 | * @st: the service tree of the entity. | |
1584 | * @entity: the entity being inserted. | |
1585 | * | |
1586 | * The active tree is ordered by finish time, but an extra key is kept | |
1587 | * per each node, containing the minimum value for the start times of | |
1588 | * its children (and the node itself), so it's possible to search for | |
1589 | * the eligible node with the lowest finish time in logarithmic time. | |
1590 | */ | |
1591 | static void bfq_active_insert(struct bfq_service_tree *st, | |
1592 | struct bfq_entity *entity) | |
1593 | { | |
1594 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1595 | struct rb_node *node = &entity->rb_node; | |
e21b7a0b AA |
1596 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1597 | struct bfq_sched_data *sd = NULL; | |
1598 | struct bfq_group *bfqg = NULL; | |
1599 | struct bfq_data *bfqd = NULL; | |
1600 | #endif | |
aee69d78 PV |
1601 | |
1602 | bfq_insert(&st->active, entity); | |
1603 | ||
1604 | if (node->rb_left) | |
1605 | node = node->rb_left; | |
1606 | else if (node->rb_right) | |
1607 | node = node->rb_right; | |
1608 | ||
1609 | bfq_update_active_tree(node); | |
1610 | ||
e21b7a0b AA |
1611 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1612 | sd = entity->sched_data; | |
1613 | bfqg = container_of(sd, struct bfq_group, sched_data); | |
1614 | bfqd = (struct bfq_data *)bfqg->bfqd; | |
1615 | #endif | |
aee69d78 PV |
1616 | if (bfqq) |
1617 | list_add(&bfqq->bfqq_list, &bfqq->bfqd->active_list); | |
1de0c4cd AA |
1618 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1619 | else /* bfq_group */ | |
1620 | bfq_weights_tree_add(bfqd, entity, &bfqd->group_weights_tree); | |
1621 | ||
1622 | if (bfqg != bfqd->root_group) | |
1623 | bfqg->active_entities++; | |
1624 | #endif | |
aee69d78 PV |
1625 | } |
1626 | ||
1627 | /** | |
1628 | * bfq_ioprio_to_weight - calc a weight from an ioprio. | |
1629 | * @ioprio: the ioprio value to convert. | |
1630 | */ | |
1631 | static unsigned short bfq_ioprio_to_weight(int ioprio) | |
1632 | { | |
1633 | return (IOPRIO_BE_NR - ioprio) * BFQ_WEIGHT_CONVERSION_COEFF; | |
1634 | } | |
1635 | ||
1636 | /** | |
1637 | * bfq_weight_to_ioprio - calc an ioprio from a weight. | |
1638 | * @weight: the weight value to convert. | |
1639 | * | |
1640 | * To preserve as much as possible the old only-ioprio user interface, | |
1641 | * 0 is used as an escape ioprio value for weights (numerically) equal or | |
1642 | * larger than IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF. | |
1643 | */ | |
1644 | static unsigned short bfq_weight_to_ioprio(int weight) | |
1645 | { | |
1646 | return max_t(int, 0, | |
1647 | IOPRIO_BE_NR * BFQ_WEIGHT_CONVERSION_COEFF - weight); | |
1648 | } | |
1649 | ||
1650 | static void bfq_get_entity(struct bfq_entity *entity) | |
1651 | { | |
1652 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1653 | ||
1654 | if (bfqq) { | |
1655 | bfqq->ref++; | |
1656 | bfq_log_bfqq(bfqq->bfqd, bfqq, "get_entity: %p %d", | |
1657 | bfqq, bfqq->ref); | |
1658 | } | |
1659 | } | |
1660 | ||
1661 | /** | |
1662 | * bfq_find_deepest - find the deepest node that an extraction can modify. | |
1663 | * @node: the node being removed. | |
1664 | * | |
1665 | * Do the first step of an extraction in an rb tree, looking for the | |
1666 | * node that will replace @node, and returning the deepest node that | |
1667 | * the following modifications to the tree can touch. If @node is the | |
1668 | * last node in the tree return %NULL. | |
1669 | */ | |
1670 | static struct rb_node *bfq_find_deepest(struct rb_node *node) | |
1671 | { | |
1672 | struct rb_node *deepest; | |
1673 | ||
1674 | if (!node->rb_right && !node->rb_left) | |
1675 | deepest = rb_parent(node); | |
1676 | else if (!node->rb_right) | |
1677 | deepest = node->rb_left; | |
1678 | else if (!node->rb_left) | |
1679 | deepest = node->rb_right; | |
1680 | else { | |
1681 | deepest = rb_next(node); | |
1682 | if (deepest->rb_right) | |
1683 | deepest = deepest->rb_right; | |
1684 | else if (rb_parent(deepest) != node) | |
1685 | deepest = rb_parent(deepest); | |
1686 | } | |
1687 | ||
1688 | return deepest; | |
1689 | } | |
1690 | ||
1691 | /** | |
1692 | * bfq_active_extract - remove an entity from the active tree. | |
1693 | * @st: the service_tree containing the tree. | |
1694 | * @entity: the entity being removed. | |
1695 | */ | |
1696 | static void bfq_active_extract(struct bfq_service_tree *st, | |
1697 | struct bfq_entity *entity) | |
1698 | { | |
1699 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1700 | struct rb_node *node; | |
e21b7a0b AA |
1701 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1702 | struct bfq_sched_data *sd = NULL; | |
1703 | struct bfq_group *bfqg = NULL; | |
1704 | struct bfq_data *bfqd = NULL; | |
1705 | #endif | |
aee69d78 PV |
1706 | |
1707 | node = bfq_find_deepest(&entity->rb_node); | |
1708 | bfq_extract(&st->active, entity); | |
1709 | ||
1710 | if (node) | |
1711 | bfq_update_active_tree(node); | |
1712 | ||
e21b7a0b AA |
1713 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1714 | sd = entity->sched_data; | |
1715 | bfqg = container_of(sd, struct bfq_group, sched_data); | |
1716 | bfqd = (struct bfq_data *)bfqg->bfqd; | |
1717 | #endif | |
aee69d78 PV |
1718 | if (bfqq) |
1719 | list_del(&bfqq->bfqq_list); | |
1de0c4cd AA |
1720 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1721 | else /* bfq_group */ | |
1722 | bfq_weights_tree_remove(bfqd, entity, | |
1723 | &bfqd->group_weights_tree); | |
1724 | ||
1725 | if (bfqg != bfqd->root_group) | |
1726 | bfqg->active_entities--; | |
1727 | #endif | |
aee69d78 PV |
1728 | } |
1729 | ||
1730 | /** | |
1731 | * bfq_idle_insert - insert an entity into the idle tree. | |
1732 | * @st: the service tree containing the tree. | |
1733 | * @entity: the entity to insert. | |
1734 | */ | |
1735 | static void bfq_idle_insert(struct bfq_service_tree *st, | |
1736 | struct bfq_entity *entity) | |
1737 | { | |
1738 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1739 | struct bfq_entity *first_idle = st->first_idle; | |
1740 | struct bfq_entity *last_idle = st->last_idle; | |
1741 | ||
1742 | if (!first_idle || bfq_gt(first_idle->finish, entity->finish)) | |
1743 | st->first_idle = entity; | |
1744 | if (!last_idle || bfq_gt(entity->finish, last_idle->finish)) | |
1745 | st->last_idle = entity; | |
1746 | ||
1747 | bfq_insert(&st->idle, entity); | |
1748 | ||
1749 | if (bfqq) | |
1750 | list_add(&bfqq->bfqq_list, &bfqq->bfqd->idle_list); | |
1751 | } | |
1752 | ||
1753 | /** | |
1754 | * bfq_forget_entity - do not consider entity any longer for scheduling | |
1755 | * @st: the service tree. | |
1756 | * @entity: the entity being removed. | |
1757 | * @is_in_service: true if entity is currently the in-service entity. | |
1758 | * | |
1759 | * Forget everything about @entity. In addition, if entity represents | |
1760 | * a queue, and the latter is not in service, then release the service | |
1761 | * reference to the queue (the one taken through bfq_get_entity). In | |
1762 | * fact, in this case, there is really no more service reference to | |
1763 | * the queue, as the latter is also outside any service tree. If, | |
1764 | * instead, the queue is in service, then __bfq_bfqd_reset_in_service | |
1765 | * will take care of putting the reference when the queue finally | |
1766 | * stops being served. | |
1767 | */ | |
1768 | static void bfq_forget_entity(struct bfq_service_tree *st, | |
1769 | struct bfq_entity *entity, | |
1770 | bool is_in_service) | |
1771 | { | |
1772 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
1773 | ||
e21b7a0b | 1774 | entity->on_st = false; |
aee69d78 PV |
1775 | st->wsum -= entity->weight; |
1776 | if (bfqq && !is_in_service) | |
1777 | bfq_put_queue(bfqq); | |
1778 | } | |
1779 | ||
1780 | /** | |
1781 | * bfq_put_idle_entity - release the idle tree ref of an entity. | |
1782 | * @st: service tree for the entity. | |
1783 | * @entity: the entity being released. | |
1784 | */ | |
1785 | static void bfq_put_idle_entity(struct bfq_service_tree *st, | |
1786 | struct bfq_entity *entity) | |
1787 | { | |
1788 | bfq_idle_extract(st, entity); | |
1789 | bfq_forget_entity(st, entity, | |
1790 | entity == entity->sched_data->in_service_entity); | |
1791 | } | |
1792 | ||
1793 | /** | |
1794 | * bfq_forget_idle - update the idle tree if necessary. | |
1795 | * @st: the service tree to act upon. | |
1796 | * | |
1797 | * To preserve the global O(log N) complexity we only remove one entry here; | |
1798 | * as the idle tree will not grow indefinitely this can be done safely. | |
1799 | */ | |
1800 | static void bfq_forget_idle(struct bfq_service_tree *st) | |
1801 | { | |
1802 | struct bfq_entity *first_idle = st->first_idle; | |
1803 | struct bfq_entity *last_idle = st->last_idle; | |
1804 | ||
1805 | if (RB_EMPTY_ROOT(&st->active) && last_idle && | |
1806 | !bfq_gt(last_idle->finish, st->vtime)) { | |
1807 | /* | |
1808 | * Forget the whole idle tree, increasing the vtime past | |
1809 | * the last finish time of idle entities. | |
1810 | */ | |
1811 | st->vtime = last_idle->finish; | |
1812 | } | |
1813 | ||
1814 | if (first_idle && !bfq_gt(first_idle->finish, st->vtime)) | |
1815 | bfq_put_idle_entity(st, first_idle); | |
1816 | } | |
1817 | ||
1818 | static struct bfq_service_tree * | |
1819 | __bfq_entity_update_weight_prio(struct bfq_service_tree *old_st, | |
e21b7a0b | 1820 | struct bfq_entity *entity) |
aee69d78 PV |
1821 | { |
1822 | struct bfq_service_tree *new_st = old_st; | |
1823 | ||
1824 | if (entity->prio_changed) { | |
1825 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
44e44a1b | 1826 | unsigned int prev_weight, new_weight; |
aee69d78 | 1827 | struct bfq_data *bfqd = NULL; |
1de0c4cd | 1828 | struct rb_root *root; |
e21b7a0b AA |
1829 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1830 | struct bfq_sched_data *sd; | |
1831 | struct bfq_group *bfqg; | |
1832 | #endif | |
aee69d78 PV |
1833 | |
1834 | if (bfqq) | |
1835 | bfqd = bfqq->bfqd; | |
e21b7a0b AA |
1836 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
1837 | else { | |
1838 | sd = entity->my_sched_data; | |
1839 | bfqg = container_of(sd, struct bfq_group, sched_data); | |
1840 | bfqd = (struct bfq_data *)bfqg->bfqd; | |
1841 | } | |
1842 | #endif | |
aee69d78 PV |
1843 | |
1844 | old_st->wsum -= entity->weight; | |
1845 | ||
1846 | if (entity->new_weight != entity->orig_weight) { | |
1847 | if (entity->new_weight < BFQ_MIN_WEIGHT || | |
1848 | entity->new_weight > BFQ_MAX_WEIGHT) { | |
1849 | pr_crit("update_weight_prio: new_weight %d\n", | |
1850 | entity->new_weight); | |
1851 | if (entity->new_weight < BFQ_MIN_WEIGHT) | |
1852 | entity->new_weight = BFQ_MIN_WEIGHT; | |
1853 | else | |
1854 | entity->new_weight = BFQ_MAX_WEIGHT; | |
1855 | } | |
1856 | entity->orig_weight = entity->new_weight; | |
1857 | if (bfqq) | |
1858 | bfqq->ioprio = | |
1859 | bfq_weight_to_ioprio(entity->orig_weight); | |
1860 | } | |
1861 | ||
1862 | if (bfqq) | |
1863 | bfqq->ioprio_class = bfqq->new_ioprio_class; | |
1864 | entity->prio_changed = 0; | |
1865 | ||
1866 | /* | |
1867 | * NOTE: here we may be changing the weight too early, | |
1868 | * this will cause unfairness. The correct approach | |
1869 | * would have required additional complexity to defer | |
1870 | * weight changes to the proper time instants (i.e., | |
1871 | * when entity->finish <= old_st->vtime). | |
1872 | */ | |
1873 | new_st = bfq_entity_service_tree(entity); | |
1874 | ||
1875 | prev_weight = entity->weight; | |
44e44a1b PV |
1876 | new_weight = entity->orig_weight * |
1877 | (bfqq ? bfqq->wr_coeff : 1); | |
1de0c4cd AA |
1878 | /* |
1879 | * If the weight of the entity changes, remove the entity | |
1880 | * from its old weight counter (if there is a counter | |
1881 | * associated with the entity), and add it to the counter | |
1882 | * associated with its new weight. | |
1883 | */ | |
1884 | if (prev_weight != new_weight) { | |
1885 | root = bfqq ? &bfqd->queue_weights_tree : | |
1886 | &bfqd->group_weights_tree; | |
1887 | bfq_weights_tree_remove(bfqd, entity, root); | |
1888 | } | |
aee69d78 | 1889 | entity->weight = new_weight; |
1de0c4cd AA |
1890 | /* |
1891 | * Add the entity to its weights tree only if it is | |
1892 | * not associated with a weight-raised queue. | |
1893 | */ | |
1894 | if (prev_weight != new_weight && | |
1895 | (bfqq ? bfqq->wr_coeff == 1 : 1)) | |
1896 | /* If we get here, root has been initialized. */ | |
1897 | bfq_weights_tree_add(bfqd, entity, root); | |
aee69d78 PV |
1898 | |
1899 | new_st->wsum += entity->weight; | |
1900 | ||
1901 | if (new_st != old_st) | |
1902 | entity->start = new_st->vtime; | |
1903 | } | |
1904 | ||
1905 | return new_st; | |
1906 | } | |
1907 | ||
e21b7a0b AA |
1908 | static void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg); |
1909 | static struct bfq_group *bfqq_group(struct bfq_queue *bfqq); | |
1910 | ||
aee69d78 PV |
1911 | /** |
1912 | * bfq_bfqq_served - update the scheduler status after selection for | |
1913 | * service. | |
1914 | * @bfqq: the queue being served. | |
1915 | * @served: bytes to transfer. | |
1916 | * | |
1917 | * NOTE: this can be optimized, as the timestamps of upper level entities | |
1918 | * are synchronized every time a new bfqq is selected for service. By now, | |
1919 | * we keep it to better check consistency. | |
1920 | */ | |
1921 | static void bfq_bfqq_served(struct bfq_queue *bfqq, int served) | |
1922 | { | |
1923 | struct bfq_entity *entity = &bfqq->entity; | |
1924 | struct bfq_service_tree *st; | |
1925 | ||
1926 | for_each_entity(entity) { | |
1927 | st = bfq_entity_service_tree(entity); | |
1928 | ||
1929 | entity->service += served; | |
1930 | ||
1931 | st->vtime += bfq_delta(served, st->wsum); | |
1932 | bfq_forget_idle(st); | |
1933 | } | |
e21b7a0b | 1934 | bfqg_stats_set_start_empty_time(bfqq_group(bfqq)); |
aee69d78 PV |
1935 | bfq_log_bfqq(bfqq->bfqd, bfqq, "bfqq_served %d secs", served); |
1936 | } | |
1937 | ||
1938 | /** | |
c074170e PV |
1939 | * bfq_bfqq_charge_time - charge an amount of service equivalent to the length |
1940 | * of the time interval during which bfqq has been in | |
1941 | * service. | |
1942 | * @bfqd: the device | |
aee69d78 | 1943 | * @bfqq: the queue that needs a service update. |
c074170e | 1944 | * @time_ms: the amount of time during which the queue has received service |
aee69d78 | 1945 | * |
c074170e PV |
1946 | * If a queue does not consume its budget fast enough, then providing |
1947 | * the queue with service fairness may impair throughput, more or less | |
1948 | * severely. For this reason, queues that consume their budget slowly | |
1949 | * are provided with time fairness instead of service fairness. This | |
1950 | * goal is achieved through the BFQ scheduling engine, even if such an | |
1951 | * engine works in the service, and not in the time domain. The trick | |
1952 | * is charging these queues with an inflated amount of service, equal | |
1953 | * to the amount of service that they would have received during their | |
1954 | * service slot if they had been fast, i.e., if their requests had | |
1955 | * been dispatched at a rate equal to the estimated peak rate. | |
1956 | * | |
1957 | * It is worth noting that time fairness can cause important | |
1958 | * distortions in terms of bandwidth distribution, on devices with | |
1959 | * internal queueing. The reason is that I/O requests dispatched | |
1960 | * during the service slot of a queue may be served after that service | |
1961 | * slot is finished, and may have a total processing time loosely | |
1962 | * correlated with the duration of the service slot. This is | |
1963 | * especially true for short service slots. | |
aee69d78 | 1964 | */ |
c074170e PV |
1965 | static void bfq_bfqq_charge_time(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
1966 | unsigned long time_ms) | |
aee69d78 PV |
1967 | { |
1968 | struct bfq_entity *entity = &bfqq->entity; | |
c074170e PV |
1969 | int tot_serv_to_charge = entity->service; |
1970 | unsigned int timeout_ms = jiffies_to_msecs(bfq_timeout); | |
1971 | ||
1972 | if (time_ms > 0 && time_ms < timeout_ms) | |
1973 | tot_serv_to_charge = | |
1974 | (bfqd->bfq_max_budget * time_ms) / timeout_ms; | |
aee69d78 | 1975 | |
c074170e PV |
1976 | if (tot_serv_to_charge < entity->service) |
1977 | tot_serv_to_charge = entity->service; | |
aee69d78 | 1978 | |
c074170e PV |
1979 | /* Increase budget to avoid inconsistencies */ |
1980 | if (tot_serv_to_charge > entity->budget) | |
1981 | entity->budget = tot_serv_to_charge; | |
1982 | ||
1983 | bfq_bfqq_served(bfqq, | |
1984 | max_t(int, 0, tot_serv_to_charge - entity->service)); | |
aee69d78 PV |
1985 | } |
1986 | ||
e21b7a0b AA |
1987 | static void bfq_update_fin_time_enqueue(struct bfq_entity *entity, |
1988 | struct bfq_service_tree *st, | |
1989 | bool backshifted) | |
aee69d78 | 1990 | { |
44e44a1b PV |
1991 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); |
1992 | ||
aee69d78 PV |
1993 | st = __bfq_entity_update_weight_prio(st, entity); |
1994 | bfq_calc_finish(entity, entity->budget); | |
1995 | ||
1996 | /* | |
1997 | * If some queues enjoy backshifting for a while, then their | |
1998 | * (virtual) finish timestamps may happen to become lower and | |
1999 | * lower than the system virtual time. In particular, if | |
2000 | * these queues often happen to be idle for short time | |
2001 | * periods, and during such time periods other queues with | |
2002 | * higher timestamps happen to be busy, then the backshifted | |
2003 | * timestamps of the former queues can become much lower than | |
2004 | * the system virtual time. In fact, to serve the queues with | |
2005 | * higher timestamps while the ones with lower timestamps are | |
2006 | * idle, the system virtual time may be pushed-up to much | |
2007 | * higher values than the finish timestamps of the idle | |
2008 | * queues. As a consequence, the finish timestamps of all new | |
2009 | * or newly activated queues may end up being much larger than | |
2010 | * those of lucky queues with backshifted timestamps. The | |
2011 | * latter queues may then monopolize the device for a lot of | |
2012 | * time. This would simply break service guarantees. | |
2013 | * | |
2014 | * To reduce this problem, push up a little bit the | |
2015 | * backshifted timestamps of the queue associated with this | |
2016 | * entity (only a queue can happen to have the backshifted | |
2017 | * flag set): just enough to let the finish timestamp of the | |
2018 | * queue be equal to the current value of the system virtual | |
2019 | * time. This may introduce a little unfairness among queues | |
2020 | * with backshifted timestamps, but it does not break | |
2021 | * worst-case fairness guarantees. | |
44e44a1b PV |
2022 | * |
2023 | * As a special case, if bfqq is weight-raised, push up | |
2024 | * timestamps much less, to keep very low the probability that | |
2025 | * this push up causes the backshifted finish timestamps of | |
2026 | * weight-raised queues to become higher than the backshifted | |
2027 | * finish timestamps of non weight-raised queues. | |
aee69d78 PV |
2028 | */ |
2029 | if (backshifted && bfq_gt(st->vtime, entity->finish)) { | |
2030 | unsigned long delta = st->vtime - entity->finish; | |
2031 | ||
44e44a1b PV |
2032 | if (bfqq) |
2033 | delta /= bfqq->wr_coeff; | |
2034 | ||
aee69d78 PV |
2035 | entity->start += delta; |
2036 | entity->finish += delta; | |
2037 | } | |
2038 | ||
2039 | bfq_active_insert(st, entity); | |
2040 | } | |
2041 | ||
2042 | /** | |
e21b7a0b AA |
2043 | * __bfq_activate_entity - handle activation of entity. |
2044 | * @entity: the entity being activated. | |
2045 | * @non_blocking_wait_rq: true if entity was waiting for a request | |
2046 | * | |
2047 | * Called for a 'true' activation, i.e., if entity is not active and | |
2048 | * one of its children receives a new request. | |
2049 | * | |
2050 | * Basically, this function updates the timestamps of entity and | |
2051 | * inserts entity into its active tree, ater possible extracting it | |
2052 | * from its idle tree. | |
2053 | */ | |
2054 | static void __bfq_activate_entity(struct bfq_entity *entity, | |
2055 | bool non_blocking_wait_rq) | |
2056 | { | |
2057 | struct bfq_service_tree *st = bfq_entity_service_tree(entity); | |
2058 | bool backshifted = false; | |
2059 | unsigned long long min_vstart; | |
2060 | ||
2061 | /* See comments on bfq_fqq_update_budg_for_activation */ | |
2062 | if (non_blocking_wait_rq && bfq_gt(st->vtime, entity->finish)) { | |
2063 | backshifted = true; | |
2064 | min_vstart = entity->finish; | |
2065 | } else | |
2066 | min_vstart = st->vtime; | |
2067 | ||
2068 | if (entity->tree == &st->idle) { | |
2069 | /* | |
2070 | * Must be on the idle tree, bfq_idle_extract() will | |
2071 | * check for that. | |
2072 | */ | |
2073 | bfq_idle_extract(st, entity); | |
2074 | entity->start = bfq_gt(min_vstart, entity->finish) ? | |
2075 | min_vstart : entity->finish; | |
2076 | } else { | |
2077 | /* | |
2078 | * The finish time of the entity may be invalid, and | |
2079 | * it is in the past for sure, otherwise the queue | |
2080 | * would have been on the idle tree. | |
2081 | */ | |
2082 | entity->start = min_vstart; | |
2083 | st->wsum += entity->weight; | |
2084 | /* | |
2085 | * entity is about to be inserted into a service tree, | |
2086 | * and then set in service: get a reference to make | |
2087 | * sure entity does not disappear until it is no | |
2088 | * longer in service or scheduled for service. | |
2089 | */ | |
2090 | bfq_get_entity(entity); | |
2091 | ||
2092 | entity->on_st = true; | |
2093 | } | |
2094 | ||
2095 | bfq_update_fin_time_enqueue(entity, st, backshifted); | |
2096 | } | |
2097 | ||
2098 | /** | |
2099 | * __bfq_requeue_entity - handle requeueing or repositioning of an entity. | |
2100 | * @entity: the entity being requeued or repositioned. | |
2101 | * | |
2102 | * Requeueing is needed if this entity stops being served, which | |
2103 | * happens if a leaf descendant entity has expired. On the other hand, | |
2104 | * repositioning is needed if the next_inservice_entity for the child | |
2105 | * entity has changed. See the comments inside the function for | |
2106 | * details. | |
2107 | * | |
2108 | * Basically, this function: 1) removes entity from its active tree if | |
2109 | * present there, 2) updates the timestamps of entity and 3) inserts | |
2110 | * entity back into its active tree (in the new, right position for | |
2111 | * the new values of the timestamps). | |
2112 | */ | |
2113 | static void __bfq_requeue_entity(struct bfq_entity *entity) | |
2114 | { | |
2115 | struct bfq_sched_data *sd = entity->sched_data; | |
2116 | struct bfq_service_tree *st = bfq_entity_service_tree(entity); | |
2117 | ||
2118 | if (entity == sd->in_service_entity) { | |
2119 | /* | |
2120 | * We are requeueing the current in-service entity, | |
2121 | * which may have to be done for one of the following | |
2122 | * reasons: | |
2123 | * - entity represents the in-service queue, and the | |
2124 | * in-service queue is being requeued after an | |
2125 | * expiration; | |
2126 | * - entity represents a group, and its budget has | |
2127 | * changed because one of its child entities has | |
2128 | * just been either activated or requeued for some | |
2129 | * reason; the timestamps of the entity need then to | |
2130 | * be updated, and the entity needs to be enqueued | |
2131 | * or repositioned accordingly. | |
2132 | * | |
2133 | * In particular, before requeueing, the start time of | |
2134 | * the entity must be moved forward to account for the | |
2135 | * service that the entity has received while in | |
2136 | * service. This is done by the next instructions. The | |
2137 | * finish time will then be updated according to this | |
2138 | * new value of the start time, and to the budget of | |
2139 | * the entity. | |
2140 | */ | |
2141 | bfq_calc_finish(entity, entity->service); | |
2142 | entity->start = entity->finish; | |
2143 | /* | |
2144 | * In addition, if the entity had more than one child | |
2145 | * when set in service, then was not extracted from | |
2146 | * the active tree. This implies that the position of | |
2147 | * the entity in the active tree may need to be | |
2148 | * changed now, because we have just updated the start | |
2149 | * time of the entity, and we will update its finish | |
2150 | * time in a moment (the requeueing is then, more | |
2151 | * precisely, a repositioning in this case). To | |
2152 | * implement this repositioning, we: 1) dequeue the | |
2153 | * entity here, 2) update the finish time and | |
2154 | * requeue the entity according to the new | |
2155 | * timestamps below. | |
2156 | */ | |
2157 | if (entity->tree) | |
2158 | bfq_active_extract(st, entity); | |
2159 | } else { /* The entity is already active, and not in service */ | |
2160 | /* | |
2161 | * In this case, this function gets called only if the | |
2162 | * next_in_service entity below this entity has | |
2163 | * changed, and this change has caused the budget of | |
2164 | * this entity to change, which, finally implies that | |
2165 | * the finish time of this entity must be | |
2166 | * updated. Such an update may cause the scheduling, | |
2167 | * i.e., the position in the active tree, of this | |
2168 | * entity to change. We handle this change by: 1) | |
2169 | * dequeueing the entity here, 2) updating the finish | |
2170 | * time and requeueing the entity according to the new | |
2171 | * timestamps below. This is the same approach as the | |
2172 | * non-extracted-entity sub-case above. | |
2173 | */ | |
2174 | bfq_active_extract(st, entity); | |
2175 | } | |
2176 | ||
2177 | bfq_update_fin_time_enqueue(entity, st, false); | |
2178 | } | |
2179 | ||
2180 | static void __bfq_activate_requeue_entity(struct bfq_entity *entity, | |
2181 | struct bfq_sched_data *sd, | |
2182 | bool non_blocking_wait_rq) | |
2183 | { | |
2184 | struct bfq_service_tree *st = bfq_entity_service_tree(entity); | |
2185 | ||
2186 | if (sd->in_service_entity == entity || entity->tree == &st->active) | |
2187 | /* | |
2188 | * in service or already queued on the active tree, | |
2189 | * requeue or reposition | |
2190 | */ | |
2191 | __bfq_requeue_entity(entity); | |
2192 | else | |
2193 | /* | |
2194 | * Not in service and not queued on its active tree: | |
2195 | * the activity is idle and this is a true activation. | |
2196 | */ | |
2197 | __bfq_activate_entity(entity, non_blocking_wait_rq); | |
2198 | } | |
2199 | ||
2200 | ||
2201 | /** | |
2202 | * bfq_activate_entity - activate or requeue an entity representing a bfq_queue, | |
2203 | * and activate, requeue or reposition all ancestors | |
2204 | * for which such an update becomes necessary. | |
aee69d78 PV |
2205 | * @entity: the entity to activate. |
2206 | * @non_blocking_wait_rq: true if this entity was waiting for a request | |
e21b7a0b AA |
2207 | * @requeue: true if this is a requeue, which implies that bfqq is |
2208 | * being expired; thus ALL its ancestors stop being served and must | |
2209 | * therefore be requeued | |
aee69d78 | 2210 | */ |
e21b7a0b AA |
2211 | static void bfq_activate_requeue_entity(struct bfq_entity *entity, |
2212 | bool non_blocking_wait_rq, | |
2213 | bool requeue) | |
aee69d78 PV |
2214 | { |
2215 | struct bfq_sched_data *sd; | |
2216 | ||
2217 | for_each_entity(entity) { | |
aee69d78 | 2218 | sd = entity->sched_data; |
e21b7a0b AA |
2219 | __bfq_activate_requeue_entity(entity, sd, non_blocking_wait_rq); |
2220 | ||
2221 | if (!bfq_update_next_in_service(sd, entity) && !requeue) | |
aee69d78 PV |
2222 | break; |
2223 | } | |
2224 | } | |
2225 | ||
2226 | /** | |
2227 | * __bfq_deactivate_entity - deactivate an entity from its service tree. | |
2228 | * @entity: the entity to deactivate. | |
e21b7a0b AA |
2229 | * @ins_into_idle_tree: if false, the entity will not be put into the |
2230 | * idle tree. | |
aee69d78 | 2231 | * |
e21b7a0b AA |
2232 | * Deactivates an entity, independently from its previous state. Must |
2233 | * be invoked only if entity is on a service tree. Extracts the entity | |
2234 | * from that tree, and if necessary and allowed, puts it on the idle | |
2235 | * tree. | |
aee69d78 | 2236 | */ |
e21b7a0b AA |
2237 | static bool __bfq_deactivate_entity(struct bfq_entity *entity, |
2238 | bool ins_into_idle_tree) | |
aee69d78 PV |
2239 | { |
2240 | struct bfq_sched_data *sd = entity->sched_data; | |
2241 | struct bfq_service_tree *st = bfq_entity_service_tree(entity); | |
2242 | int is_in_service = entity == sd->in_service_entity; | |
aee69d78 | 2243 | |
e21b7a0b AA |
2244 | if (!entity->on_st) /* entity never activated, or already inactive */ |
2245 | return false; | |
aee69d78 | 2246 | |
e21b7a0b | 2247 | if (is_in_service) |
aee69d78 | 2248 | bfq_calc_finish(entity, entity->service); |
e21b7a0b AA |
2249 | |
2250 | if (entity->tree == &st->active) | |
aee69d78 | 2251 | bfq_active_extract(st, entity); |
e21b7a0b | 2252 | else if (!is_in_service && entity->tree == &st->idle) |
aee69d78 PV |
2253 | bfq_idle_extract(st, entity); |
2254 | ||
e21b7a0b | 2255 | if (!ins_into_idle_tree || !bfq_gt(entity->finish, st->vtime)) |
aee69d78 PV |
2256 | bfq_forget_entity(st, entity, is_in_service); |
2257 | else | |
2258 | bfq_idle_insert(st, entity); | |
2259 | ||
e21b7a0b | 2260 | return true; |
aee69d78 PV |
2261 | } |
2262 | ||
2263 | /** | |
e21b7a0b | 2264 | * bfq_deactivate_entity - deactivate an entity representing a bfq_queue. |
aee69d78 | 2265 | * @entity: the entity to deactivate. |
e21b7a0b | 2266 | * @ins_into_idle_tree: true if the entity can be put on the idle tree |
aee69d78 | 2267 | */ |
e21b7a0b AA |
2268 | static void bfq_deactivate_entity(struct bfq_entity *entity, |
2269 | bool ins_into_idle_tree, | |
2270 | bool expiration) | |
aee69d78 PV |
2271 | { |
2272 | struct bfq_sched_data *sd; | |
2273 | struct bfq_entity *parent = NULL; | |
2274 | ||
2275 | for_each_entity_safe(entity, parent) { | |
2276 | sd = entity->sched_data; | |
2277 | ||
e21b7a0b | 2278 | if (!__bfq_deactivate_entity(entity, ins_into_idle_tree)) { |
aee69d78 | 2279 | /* |
e21b7a0b AA |
2280 | * entity is not in any tree any more, so |
2281 | * this deactivation is a no-op, and there is | |
2282 | * nothing to change for upper-level entities | |
2283 | * (in case of expiration, this can never | |
2284 | * happen). | |
aee69d78 | 2285 | */ |
e21b7a0b AA |
2286 | return; |
2287 | } | |
2288 | ||
2289 | if (sd->next_in_service == entity) | |
2290 | /* | |
2291 | * entity was the next_in_service entity, | |
2292 | * then, since entity has just been | |
2293 | * deactivated, a new one must be found. | |
2294 | */ | |
2295 | bfq_update_next_in_service(sd, NULL); | |
aee69d78 PV |
2296 | |
2297 | if (sd->next_in_service) | |
2298 | /* | |
e21b7a0b AA |
2299 | * The parent entity is still backlogged, |
2300 | * because next_in_service is not NULL. So, no | |
2301 | * further upwards deactivation must be | |
2302 | * performed. Yet, next_in_service has | |
2303 | * changed. Then the schedule does need to be | |
2304 | * updated upwards. | |
aee69d78 | 2305 | */ |
e21b7a0b | 2306 | break; |
aee69d78 PV |
2307 | |
2308 | /* | |
e21b7a0b AA |
2309 | * If we get here, then the parent is no more |
2310 | * backlogged and we need to propagate the | |
2311 | * deactivation upwards. Thus let the loop go on. | |
aee69d78 | 2312 | */ |
aee69d78 | 2313 | |
e21b7a0b AA |
2314 | /* |
2315 | * Also let parent be queued into the idle tree on | |
2316 | * deactivation, to preserve service guarantees, and | |
2317 | * assuming that who invoked this function does not | |
2318 | * need parent entities too to be removed completely. | |
2319 | */ | |
2320 | ins_into_idle_tree = true; | |
2321 | } | |
aee69d78 | 2322 | |
e21b7a0b AA |
2323 | /* |
2324 | * If the deactivation loop is fully executed, then there are | |
2325 | * no more entities to touch and next loop is not executed at | |
2326 | * all. Otherwise, requeue remaining entities if they are | |
2327 | * about to stop receiving service, or reposition them if this | |
2328 | * is not the case. | |
2329 | */ | |
aee69d78 PV |
2330 | entity = parent; |
2331 | for_each_entity(entity) { | |
e21b7a0b AA |
2332 | /* |
2333 | * Invoke __bfq_requeue_entity on entity, even if | |
2334 | * already active, to requeue/reposition it in the | |
2335 | * active tree (because sd->next_in_service has | |
2336 | * changed) | |
2337 | */ | |
2338 | __bfq_requeue_entity(entity); | |
aee69d78 PV |
2339 | |
2340 | sd = entity->sched_data; | |
e21b7a0b AA |
2341 | if (!bfq_update_next_in_service(sd, entity) && |
2342 | !expiration) | |
2343 | /* | |
2344 | * next_in_service unchanged or not causing | |
2345 | * any change in entity->parent->sd, and no | |
2346 | * requeueing needed for expiration: stop | |
2347 | * here. | |
2348 | */ | |
aee69d78 PV |
2349 | break; |
2350 | } | |
2351 | } | |
2352 | ||
2353 | /** | |
e21b7a0b AA |
2354 | * bfq_calc_vtime_jump - compute the value to which the vtime should jump, |
2355 | * if needed, to have at least one entity eligible. | |
aee69d78 PV |
2356 | * @st: the service tree to act upon. |
2357 | * | |
e21b7a0b | 2358 | * Assumes that st is not empty. |
aee69d78 | 2359 | */ |
e21b7a0b | 2360 | static u64 bfq_calc_vtime_jump(struct bfq_service_tree *st) |
aee69d78 | 2361 | { |
e21b7a0b AA |
2362 | struct bfq_entity *root_entity = bfq_root_active_entity(&st->active); |
2363 | ||
2364 | if (bfq_gt(root_entity->min_start, st->vtime)) | |
2365 | return root_entity->min_start; | |
2366 | ||
2367 | return st->vtime; | |
2368 | } | |
aee69d78 | 2369 | |
e21b7a0b AA |
2370 | static void bfq_update_vtime(struct bfq_service_tree *st, u64 new_value) |
2371 | { | |
2372 | if (new_value > st->vtime) { | |
2373 | st->vtime = new_value; | |
aee69d78 PV |
2374 | bfq_forget_idle(st); |
2375 | } | |
2376 | } | |
2377 | ||
2378 | /** | |
2379 | * bfq_first_active_entity - find the eligible entity with | |
2380 | * the smallest finish time | |
2381 | * @st: the service tree to select from. | |
e21b7a0b | 2382 | * @vtime: the system virtual to use as a reference for eligibility |
aee69d78 PV |
2383 | * |
2384 | * This function searches the first schedulable entity, starting from the | |
2385 | * root of the tree and going on the left every time on this side there is | |
2386 | * a subtree with at least one eligible (start >= vtime) entity. The path on | |
2387 | * the right is followed only if a) the left subtree contains no eligible | |
2388 | * entities and b) no eligible entity has been found yet. | |
2389 | */ | |
e21b7a0b AA |
2390 | static struct bfq_entity *bfq_first_active_entity(struct bfq_service_tree *st, |
2391 | u64 vtime) | |
aee69d78 PV |
2392 | { |
2393 | struct bfq_entity *entry, *first = NULL; | |
2394 | struct rb_node *node = st->active.rb_node; | |
2395 | ||
2396 | while (node) { | |
2397 | entry = rb_entry(node, struct bfq_entity, rb_node); | |
2398 | left: | |
e21b7a0b | 2399 | if (!bfq_gt(entry->start, vtime)) |
aee69d78 PV |
2400 | first = entry; |
2401 | ||
2402 | if (node->rb_left) { | |
2403 | entry = rb_entry(node->rb_left, | |
2404 | struct bfq_entity, rb_node); | |
e21b7a0b | 2405 | if (!bfq_gt(entry->min_start, vtime)) { |
aee69d78 PV |
2406 | node = node->rb_left; |
2407 | goto left; | |
2408 | } | |
2409 | } | |
2410 | if (first) | |
2411 | break; | |
2412 | node = node->rb_right; | |
2413 | } | |
2414 | ||
e21b7a0b AA |
2415 | return first; |
2416 | } | |
2417 | ||
2418 | /** | |
2419 | * __bfq_lookup_next_entity - return the first eligible entity in @st. | |
2420 | * @st: the service tree. | |
2421 | * | |
2422 | * If there is no in-service entity for the sched_data st belongs to, | |
2423 | * then return the entity that will be set in service if: | |
2424 | * 1) the parent entity this st belongs to is set in service; | |
2425 | * 2) no entity belonging to such parent entity undergoes a state change | |
2426 | * that would influence the timestamps of the entity (e.g., becomes idle, | |
2427 | * becomes backlogged, changes its budget, ...). | |
2428 | * | |
2429 | * In this first case, update the virtual time in @st too (see the | |
2430 | * comments on this update inside the function). | |
2431 | * | |
2432 | * In constrast, if there is an in-service entity, then return the | |
2433 | * entity that would be set in service if not only the above | |
2434 | * conditions, but also the next one held true: the currently | |
2435 | * in-service entity, on expiration, | |
2436 | * 1) gets a finish time equal to the current one, or | |
2437 | * 2) is not eligible any more, or | |
2438 | * 3) is idle. | |
2439 | */ | |
2440 | static struct bfq_entity * | |
2441 | __bfq_lookup_next_entity(struct bfq_service_tree *st, bool in_service) | |
2442 | { | |
2443 | struct bfq_entity *entity; | |
2444 | u64 new_vtime; | |
2445 | ||
2446 | if (RB_EMPTY_ROOT(&st->active)) | |
2447 | return NULL; | |
2448 | ||
2449 | /* | |
2450 | * Get the value of the system virtual time for which at | |
2451 | * least one entity is eligible. | |
2452 | */ | |
2453 | new_vtime = bfq_calc_vtime_jump(st); | |
2454 | ||
2455 | /* | |
2456 | * If there is no in-service entity for the sched_data this | |
2457 | * active tree belongs to, then push the system virtual time | |
2458 | * up to the value that guarantees that at least one entity is | |
2459 | * eligible. If, instead, there is an in-service entity, then | |
2460 | * do not make any such update, because there is already an | |
2461 | * eligible entity, namely the in-service one (even if the | |
2462 | * entity is not on st, because it was extracted when set in | |
2463 | * service). | |
2464 | */ | |
2465 | if (!in_service) | |
2466 | bfq_update_vtime(st, new_vtime); | |
2467 | ||
2468 | entity = bfq_first_active_entity(st, new_vtime); | |
2469 | ||
2470 | return entity; | |
2471 | } | |
2472 | ||
2473 | /** | |
2474 | * bfq_lookup_next_entity - return the first eligible entity in @sd. | |
2475 | * @sd: the sched_data. | |
2476 | * | |
2477 | * This function is invoked when there has been a change in the trees | |
2478 | * for sd, and we need know what is the new next entity after this | |
2479 | * change. | |
2480 | */ | |
2481 | static struct bfq_entity *bfq_lookup_next_entity(struct bfq_sched_data *sd) | |
2482 | { | |
2483 | struct bfq_service_tree *st = sd->service_tree; | |
2484 | struct bfq_service_tree *idle_class_st = st + (BFQ_IOPRIO_CLASSES - 1); | |
2485 | struct bfq_entity *entity = NULL; | |
2486 | int class_idx = 0; | |
2487 | ||
2488 | /* | |
2489 | * Choose from idle class, if needed to guarantee a minimum | |
2490 | * bandwidth to this class (and if there is some active entity | |
2491 | * in idle class). This should also mitigate | |
2492 | * priority-inversion problems in case a low priority task is | |
2493 | * holding file system resources. | |
2494 | */ | |
2495 | if (time_is_before_jiffies(sd->bfq_class_idle_last_service + | |
2496 | BFQ_CL_IDLE_TIMEOUT)) { | |
2497 | if (!RB_EMPTY_ROOT(&idle_class_st->active)) | |
2498 | class_idx = BFQ_IOPRIO_CLASSES - 1; | |
2499 | /* About to be served if backlogged, or not yet backlogged */ | |
2500 | sd->bfq_class_idle_last_service = jiffies; | |
2501 | } | |
2502 | ||
2503 | /* | |
2504 | * Find the next entity to serve for the highest-priority | |
2505 | * class, unless the idle class needs to be served. | |
2506 | */ | |
2507 | for (; class_idx < BFQ_IOPRIO_CLASSES; class_idx++) { | |
2508 | entity = __bfq_lookup_next_entity(st + class_idx, | |
2509 | sd->in_service_entity); | |
2510 | ||
2511 | if (entity) | |
2512 | break; | |
2513 | } | |
2514 | ||
2515 | if (!entity) | |
2516 | return NULL; | |
2517 | ||
2518 | return entity; | |
2519 | } | |
2520 | ||
2521 | static bool next_queue_may_preempt(struct bfq_data *bfqd) | |
2522 | { | |
2523 | struct bfq_sched_data *sd = &bfqd->root_group->sched_data; | |
2524 | ||
2525 | return sd->next_in_service != sd->in_service_entity; | |
2526 | } | |
2527 | ||
2528 | /* | |
2529 | * Get next queue for service. | |
2530 | */ | |
2531 | static struct bfq_queue *bfq_get_next_queue(struct bfq_data *bfqd) | |
2532 | { | |
2533 | struct bfq_entity *entity = NULL; | |
2534 | struct bfq_sched_data *sd; | |
2535 | struct bfq_queue *bfqq; | |
2536 | ||
2537 | if (bfqd->busy_queues == 0) | |
2538 | return NULL; | |
2539 | ||
2540 | /* | |
2541 | * Traverse the path from the root to the leaf entity to | |
2542 | * serve. Set in service all the entities visited along the | |
2543 | * way. | |
2544 | */ | |
2545 | sd = &bfqd->root_group->sched_data; | |
2546 | for (; sd ; sd = entity->my_sched_data) { | |
2547 | /* | |
2548 | * WARNING. We are about to set the in-service entity | |
2549 | * to sd->next_in_service, i.e., to the (cached) value | |
2550 | * returned by bfq_lookup_next_entity(sd) the last | |
2551 | * time it was invoked, i.e., the last time when the | |
2552 | * service order in sd changed as a consequence of the | |
2553 | * activation or deactivation of an entity. In this | |
2554 | * respect, if we execute bfq_lookup_next_entity(sd) | |
2555 | * in this very moment, it may, although with low | |
2556 | * probability, yield a different entity than that | |
2557 | * pointed to by sd->next_in_service. This rare event | |
2558 | * happens in case there was no CLASS_IDLE entity to | |
2559 | * serve for sd when bfq_lookup_next_entity(sd) was | |
2560 | * invoked for the last time, while there is now one | |
2561 | * such entity. | |
2562 | * | |
2563 | * If the above event happens, then the scheduling of | |
2564 | * such entity in CLASS_IDLE is postponed until the | |
2565 | * service of the sd->next_in_service entity | |
2566 | * finishes. In fact, when the latter is expired, | |
2567 | * bfq_lookup_next_entity(sd) gets called again, | |
2568 | * exactly to update sd->next_in_service. | |
2569 | */ | |
2570 | ||
2571 | /* Make next_in_service entity become in_service_entity */ | |
2572 | entity = sd->next_in_service; | |
2573 | sd->in_service_entity = entity; | |
2574 | ||
2575 | /* | |
2576 | * Reset the accumulator of the amount of service that | |
2577 | * the entity is about to receive. | |
2578 | */ | |
2579 | entity->service = 0; | |
2580 | ||
2581 | /* | |
2582 | * If entity is no longer a candidate for next | |
2583 | * service, then we extract it from its active tree, | |
2584 | * for the following reason. To further boost the | |
2585 | * throughput in some special case, BFQ needs to know | |
2586 | * which is the next candidate entity to serve, while | |
2587 | * there is already an entity in service. In this | |
2588 | * respect, to make it easy to compute/update the next | |
2589 | * candidate entity to serve after the current | |
2590 | * candidate has been set in service, there is a case | |
2591 | * where it is necessary to extract the current | |
2592 | * candidate from its service tree. Such a case is | |
2593 | * when the entity just set in service cannot be also | |
2594 | * a candidate for next service. Details about when | |
2595 | * this conditions holds are reported in the comments | |
2596 | * on the function bfq_no_longer_next_in_service() | |
2597 | * invoked below. | |
2598 | */ | |
2599 | if (bfq_no_longer_next_in_service(entity)) | |
2600 | bfq_active_extract(bfq_entity_service_tree(entity), | |
2601 | entity); | |
2602 | ||
2603 | /* | |
2604 | * For the same reason why we may have just extracted | |
2605 | * entity from its active tree, we may need to update | |
2606 | * next_in_service for the sched_data of entity too, | |
2607 | * regardless of whether entity has been extracted. | |
2608 | * In fact, even if entity has not been extracted, a | |
2609 | * descendant entity may get extracted. Such an event | |
2610 | * would cause a change in next_in_service for the | |
2611 | * level of the descendant entity, and thus possibly | |
2612 | * back to upper levels. | |
2613 | * | |
2614 | * We cannot perform the resulting needed update | |
2615 | * before the end of this loop, because, to know which | |
2616 | * is the correct next-to-serve candidate entity for | |
2617 | * each level, we need first to find the leaf entity | |
2618 | * to set in service. In fact, only after we know | |
2619 | * which is the next-to-serve leaf entity, we can | |
2620 | * discover whether the parent entity of the leaf | |
2621 | * entity becomes the next-to-serve, and so on. | |
2622 | */ | |
2623 | ||
2624 | } | |
2625 | ||
2626 | bfqq = bfq_entity_to_bfqq(entity); | |
2627 | ||
2628 | /* | |
2629 | * We can finally update all next-to-serve entities along the | |
2630 | * path from the leaf entity just set in service to the root. | |
2631 | */ | |
2632 | for_each_entity(entity) { | |
2633 | struct bfq_sched_data *sd = entity->sched_data; | |
2634 | ||
2635 | if (!bfq_update_next_in_service(sd, NULL)) | |
2636 | break; | |
2637 | } | |
2638 | ||
2639 | return bfqq; | |
2640 | } | |
2641 | ||
2642 | static void __bfq_bfqd_reset_in_service(struct bfq_data *bfqd) | |
2643 | { | |
2644 | struct bfq_queue *in_serv_bfqq = bfqd->in_service_queue; | |
2645 | struct bfq_entity *in_serv_entity = &in_serv_bfqq->entity; | |
2646 | struct bfq_entity *entity = in_serv_entity; | |
2647 | ||
e21b7a0b AA |
2648 | bfq_clear_bfqq_wait_request(in_serv_bfqq); |
2649 | hrtimer_try_to_cancel(&bfqd->idle_slice_timer); | |
2650 | bfqd->in_service_queue = NULL; | |
2651 | ||
2652 | /* | |
2653 | * When this function is called, all in-service entities have | |
2654 | * been properly deactivated or requeued, so we can safely | |
2655 | * execute the final step: reset in_service_entity along the | |
2656 | * path from entity to the root. | |
2657 | */ | |
2658 | for_each_entity(entity) | |
2659 | entity->sched_data->in_service_entity = NULL; | |
2660 | ||
2661 | /* | |
2662 | * in_serv_entity is no longer in service, so, if it is in no | |
2663 | * service tree either, then release the service reference to | |
2664 | * the queue it represents (taken with bfq_get_entity). | |
2665 | */ | |
2666 | if (!in_serv_entity->on_st) | |
2667 | bfq_put_queue(in_serv_bfqq); | |
2668 | } | |
2669 | ||
2670 | static void bfq_deactivate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
2671 | bool ins_into_idle_tree, bool expiration) | |
2672 | { | |
2673 | struct bfq_entity *entity = &bfqq->entity; | |
2674 | ||
2675 | bfq_deactivate_entity(entity, ins_into_idle_tree, expiration); | |
2676 | } | |
2677 | ||
2678 | static void bfq_activate_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
2679 | { | |
2680 | struct bfq_entity *entity = &bfqq->entity; | |
2681 | ||
2682 | bfq_activate_requeue_entity(entity, bfq_bfqq_non_blocking_wait_rq(bfqq), | |
2683 | false); | |
2684 | bfq_clear_bfqq_non_blocking_wait_rq(bfqq); | |
2685 | } | |
2686 | ||
2687 | static void bfq_requeue_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
2688 | { | |
2689 | struct bfq_entity *entity = &bfqq->entity; | |
2690 | ||
2691 | bfq_activate_requeue_entity(entity, false, | |
2692 | bfqq == bfqd->in_service_queue); | |
2693 | } | |
2694 | ||
2695 | static void bfqg_stats_update_dequeue(struct bfq_group *bfqg); | |
2696 | ||
2697 | /* | |
2698 | * Called when the bfqq no longer has requests pending, remove it from | |
2699 | * the service tree. As a special case, it can be invoked during an | |
2700 | * expiration. | |
2701 | */ | |
2702 | static void bfq_del_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
2703 | bool expiration) | |
2704 | { | |
2705 | bfq_log_bfqq(bfqd, bfqq, "del from busy"); | |
2706 | ||
2707 | bfq_clear_bfqq_busy(bfqq); | |
2708 | ||
2709 | bfqd->busy_queues--; | |
2710 | ||
1de0c4cd AA |
2711 | if (!bfqq->dispatched) |
2712 | bfq_weights_tree_remove(bfqd, &bfqq->entity, | |
2713 | &bfqd->queue_weights_tree); | |
2714 | ||
cfd69712 PV |
2715 | if (bfqq->wr_coeff > 1) |
2716 | bfqd->wr_busy_queues--; | |
2717 | ||
e21b7a0b AA |
2718 | bfqg_stats_update_dequeue(bfqq_group(bfqq)); |
2719 | ||
2720 | bfq_deactivate_bfqq(bfqd, bfqq, true, expiration); | |
2721 | } | |
2722 | ||
2723 | /* | |
2724 | * Called when an inactive queue receives a new request. | |
2725 | */ | |
2726 | static void bfq_add_bfqq_busy(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
2727 | { | |
2728 | bfq_log_bfqq(bfqd, bfqq, "add to busy"); | |
2729 | ||
2730 | bfq_activate_bfqq(bfqd, bfqq); | |
2731 | ||
2732 | bfq_mark_bfqq_busy(bfqq); | |
2733 | bfqd->busy_queues++; | |
cfd69712 | 2734 | |
1de0c4cd AA |
2735 | if (!bfqq->dispatched) |
2736 | if (bfqq->wr_coeff == 1) | |
2737 | bfq_weights_tree_add(bfqd, &bfqq->entity, | |
2738 | &bfqd->queue_weights_tree); | |
2739 | ||
cfd69712 PV |
2740 | if (bfqq->wr_coeff > 1) |
2741 | bfqd->wr_busy_queues++; | |
e21b7a0b AA |
2742 | } |
2743 | ||
2744 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
2745 | ||
2746 | /* bfqg stats flags */ | |
2747 | enum bfqg_stats_flags { | |
2748 | BFQG_stats_waiting = 0, | |
2749 | BFQG_stats_idling, | |
2750 | BFQG_stats_empty, | |
2751 | }; | |
2752 | ||
2753 | #define BFQG_FLAG_FNS(name) \ | |
2754 | static void bfqg_stats_mark_##name(struct bfqg_stats *stats) \ | |
2755 | { \ | |
2756 | stats->flags |= (1 << BFQG_stats_##name); \ | |
2757 | } \ | |
2758 | static void bfqg_stats_clear_##name(struct bfqg_stats *stats) \ | |
2759 | { \ | |
2760 | stats->flags &= ~(1 << BFQG_stats_##name); \ | |
2761 | } \ | |
2762 | static int bfqg_stats_##name(struct bfqg_stats *stats) \ | |
2763 | { \ | |
2764 | return (stats->flags & (1 << BFQG_stats_##name)) != 0; \ | |
2765 | } \ | |
2766 | ||
2767 | BFQG_FLAG_FNS(waiting) | |
2768 | BFQG_FLAG_FNS(idling) | |
2769 | BFQG_FLAG_FNS(empty) | |
2770 | #undef BFQG_FLAG_FNS | |
2771 | ||
2772 | /* This should be called with the queue_lock held. */ | |
2773 | static void bfqg_stats_update_group_wait_time(struct bfqg_stats *stats) | |
2774 | { | |
2775 | unsigned long long now; | |
2776 | ||
2777 | if (!bfqg_stats_waiting(stats)) | |
2778 | return; | |
2779 | ||
2780 | now = sched_clock(); | |
2781 | if (time_after64(now, stats->start_group_wait_time)) | |
2782 | blkg_stat_add(&stats->group_wait_time, | |
2783 | now - stats->start_group_wait_time); | |
2784 | bfqg_stats_clear_waiting(stats); | |
2785 | } | |
2786 | ||
2787 | /* This should be called with the queue_lock held. */ | |
2788 | static void bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg, | |
2789 | struct bfq_group *curr_bfqg) | |
2790 | { | |
2791 | struct bfqg_stats *stats = &bfqg->stats; | |
2792 | ||
2793 | if (bfqg_stats_waiting(stats)) | |
2794 | return; | |
2795 | if (bfqg == curr_bfqg) | |
2796 | return; | |
2797 | stats->start_group_wait_time = sched_clock(); | |
2798 | bfqg_stats_mark_waiting(stats); | |
2799 | } | |
2800 | ||
2801 | /* This should be called with the queue_lock held. */ | |
2802 | static void bfqg_stats_end_empty_time(struct bfqg_stats *stats) | |
2803 | { | |
2804 | unsigned long long now; | |
2805 | ||
2806 | if (!bfqg_stats_empty(stats)) | |
2807 | return; | |
2808 | ||
2809 | now = sched_clock(); | |
2810 | if (time_after64(now, stats->start_empty_time)) | |
2811 | blkg_stat_add(&stats->empty_time, | |
2812 | now - stats->start_empty_time); | |
2813 | bfqg_stats_clear_empty(stats); | |
2814 | } | |
2815 | ||
2816 | static void bfqg_stats_update_dequeue(struct bfq_group *bfqg) | |
2817 | { | |
2818 | blkg_stat_add(&bfqg->stats.dequeue, 1); | |
2819 | } | |
2820 | ||
2821 | static void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) | |
2822 | { | |
2823 | struct bfqg_stats *stats = &bfqg->stats; | |
2824 | ||
2825 | if (blkg_rwstat_total(&stats->queued)) | |
2826 | return; | |
2827 | ||
2828 | /* | |
2829 | * group is already marked empty. This can happen if bfqq got new | |
2830 | * request in parent group and moved to this group while being added | |
2831 | * to service tree. Just ignore the event and move on. | |
2832 | */ | |
2833 | if (bfqg_stats_empty(stats)) | |
2834 | return; | |
2835 | ||
2836 | stats->start_empty_time = sched_clock(); | |
2837 | bfqg_stats_mark_empty(stats); | |
2838 | } | |
2839 | ||
2840 | static void bfqg_stats_update_idle_time(struct bfq_group *bfqg) | |
2841 | { | |
2842 | struct bfqg_stats *stats = &bfqg->stats; | |
2843 | ||
2844 | if (bfqg_stats_idling(stats)) { | |
2845 | unsigned long long now = sched_clock(); | |
2846 | ||
2847 | if (time_after64(now, stats->start_idle_time)) | |
2848 | blkg_stat_add(&stats->idle_time, | |
2849 | now - stats->start_idle_time); | |
2850 | bfqg_stats_clear_idling(stats); | |
2851 | } | |
2852 | } | |
2853 | ||
2854 | static void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) | |
2855 | { | |
2856 | struct bfqg_stats *stats = &bfqg->stats; | |
2857 | ||
2858 | stats->start_idle_time = sched_clock(); | |
2859 | bfqg_stats_mark_idling(stats); | |
2860 | } | |
2861 | ||
2862 | static void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) | |
2863 | { | |
2864 | struct bfqg_stats *stats = &bfqg->stats; | |
2865 | ||
2866 | blkg_stat_add(&stats->avg_queue_size_sum, | |
2867 | blkg_rwstat_total(&stats->queued)); | |
2868 | blkg_stat_add(&stats->avg_queue_size_samples, 1); | |
2869 | bfqg_stats_update_group_wait_time(stats); | |
2870 | } | |
2871 | ||
2872 | /* | |
2873 | * blk-cgroup policy-related handlers | |
2874 | * The following functions help in converting between blk-cgroup | |
2875 | * internal structures and BFQ-specific structures. | |
2876 | */ | |
2877 | ||
2878 | static struct bfq_group *pd_to_bfqg(struct blkg_policy_data *pd) | |
2879 | { | |
2880 | return pd ? container_of(pd, struct bfq_group, pd) : NULL; | |
2881 | } | |
2882 | ||
2883 | static struct blkcg_gq *bfqg_to_blkg(struct bfq_group *bfqg) | |
2884 | { | |
2885 | return pd_to_blkg(&bfqg->pd); | |
2886 | } | |
2887 | ||
2888 | static struct blkcg_policy blkcg_policy_bfq; | |
2889 | ||
2890 | static struct bfq_group *blkg_to_bfqg(struct blkcg_gq *blkg) | |
2891 | { | |
2892 | return pd_to_bfqg(blkg_to_pd(blkg, &blkcg_policy_bfq)); | |
2893 | } | |
2894 | ||
2895 | /* | |
2896 | * bfq_group handlers | |
2897 | * The following functions help in navigating the bfq_group hierarchy | |
2898 | * by allowing to find the parent of a bfq_group or the bfq_group | |
2899 | * associated to a bfq_queue. | |
2900 | */ | |
2901 | ||
2902 | static struct bfq_group *bfqg_parent(struct bfq_group *bfqg) | |
2903 | { | |
2904 | struct blkcg_gq *pblkg = bfqg_to_blkg(bfqg)->parent; | |
2905 | ||
2906 | return pblkg ? blkg_to_bfqg(pblkg) : NULL; | |
2907 | } | |
2908 | ||
2909 | static struct bfq_group *bfqq_group(struct bfq_queue *bfqq) | |
2910 | { | |
2911 | struct bfq_entity *group_entity = bfqq->entity.parent; | |
2912 | ||
2913 | return group_entity ? container_of(group_entity, struct bfq_group, | |
2914 | entity) : | |
2915 | bfqq->bfqd->root_group; | |
2916 | } | |
2917 | ||
2918 | /* | |
2919 | * The following two functions handle get and put of a bfq_group by | |
2920 | * wrapping the related blk-cgroup hooks. | |
2921 | */ | |
2922 | ||
2923 | static void bfqg_get(struct bfq_group *bfqg) | |
2924 | { | |
2925 | return blkg_get(bfqg_to_blkg(bfqg)); | |
2926 | } | |
2927 | ||
2928 | static void bfqg_put(struct bfq_group *bfqg) | |
2929 | { | |
2930 | return blkg_put(bfqg_to_blkg(bfqg)); | |
2931 | } | |
2932 | ||
2933 | static void bfqg_stats_update_io_add(struct bfq_group *bfqg, | |
2934 | struct bfq_queue *bfqq, | |
2935 | unsigned int op) | |
2936 | { | |
2937 | blkg_rwstat_add(&bfqg->stats.queued, op, 1); | |
2938 | bfqg_stats_end_empty_time(&bfqg->stats); | |
2939 | if (!(bfqq == ((struct bfq_data *)bfqg->bfqd)->in_service_queue)) | |
2940 | bfqg_stats_set_start_group_wait_time(bfqg, bfqq_group(bfqq)); | |
2941 | } | |
2942 | ||
2943 | static void bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) | |
2944 | { | |
2945 | blkg_rwstat_add(&bfqg->stats.queued, op, -1); | |
2946 | } | |
2947 | ||
2948 | static void bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) | |
2949 | { | |
2950 | blkg_rwstat_add(&bfqg->stats.merged, op, 1); | |
2951 | } | |
2952 | ||
2953 | static void bfqg_stats_update_completion(struct bfq_group *bfqg, | |
2954 | uint64_t start_time, uint64_t io_start_time, | |
2955 | unsigned int op) | |
2956 | { | |
2957 | struct bfqg_stats *stats = &bfqg->stats; | |
2958 | unsigned long long now = sched_clock(); | |
2959 | ||
2960 | if (time_after64(now, io_start_time)) | |
2961 | blkg_rwstat_add(&stats->service_time, op, | |
2962 | now - io_start_time); | |
2963 | if (time_after64(io_start_time, start_time)) | |
2964 | blkg_rwstat_add(&stats->wait_time, op, | |
2965 | io_start_time - start_time); | |
2966 | } | |
2967 | ||
2968 | /* @stats = 0 */ | |
2969 | static void bfqg_stats_reset(struct bfqg_stats *stats) | |
2970 | { | |
2971 | /* queued stats shouldn't be cleared */ | |
2972 | blkg_rwstat_reset(&stats->merged); | |
2973 | blkg_rwstat_reset(&stats->service_time); | |
2974 | blkg_rwstat_reset(&stats->wait_time); | |
2975 | blkg_stat_reset(&stats->time); | |
2976 | blkg_stat_reset(&stats->avg_queue_size_sum); | |
2977 | blkg_stat_reset(&stats->avg_queue_size_samples); | |
2978 | blkg_stat_reset(&stats->dequeue); | |
2979 | blkg_stat_reset(&stats->group_wait_time); | |
2980 | blkg_stat_reset(&stats->idle_time); | |
2981 | blkg_stat_reset(&stats->empty_time); | |
2982 | } | |
2983 | ||
2984 | /* @to += @from */ | |
2985 | static void bfqg_stats_add_aux(struct bfqg_stats *to, struct bfqg_stats *from) | |
2986 | { | |
2987 | if (!to || !from) | |
2988 | return; | |
2989 | ||
2990 | /* queued stats shouldn't be cleared */ | |
2991 | blkg_rwstat_add_aux(&to->merged, &from->merged); | |
2992 | blkg_rwstat_add_aux(&to->service_time, &from->service_time); | |
2993 | blkg_rwstat_add_aux(&to->wait_time, &from->wait_time); | |
2994 | blkg_stat_add_aux(&from->time, &from->time); | |
2995 | blkg_stat_add_aux(&to->avg_queue_size_sum, &from->avg_queue_size_sum); | |
2996 | blkg_stat_add_aux(&to->avg_queue_size_samples, | |
2997 | &from->avg_queue_size_samples); | |
2998 | blkg_stat_add_aux(&to->dequeue, &from->dequeue); | |
2999 | blkg_stat_add_aux(&to->group_wait_time, &from->group_wait_time); | |
3000 | blkg_stat_add_aux(&to->idle_time, &from->idle_time); | |
3001 | blkg_stat_add_aux(&to->empty_time, &from->empty_time); | |
3002 | } | |
3003 | ||
3004 | /* | |
3005 | * Transfer @bfqg's stats to its parent's aux counts so that the ancestors' | |
3006 | * recursive stats can still account for the amount used by this bfqg after | |
3007 | * it's gone. | |
3008 | */ | |
3009 | static void bfqg_stats_xfer_dead(struct bfq_group *bfqg) | |
3010 | { | |
3011 | struct bfq_group *parent; | |
3012 | ||
3013 | if (!bfqg) /* root_group */ | |
3014 | return; | |
3015 | ||
3016 | parent = bfqg_parent(bfqg); | |
3017 | ||
3018 | lockdep_assert_held(bfqg_to_blkg(bfqg)->q->queue_lock); | |
3019 | ||
3020 | if (unlikely(!parent)) | |
3021 | return; | |
3022 | ||
3023 | bfqg_stats_add_aux(&parent->stats, &bfqg->stats); | |
3024 | bfqg_stats_reset(&bfqg->stats); | |
3025 | } | |
3026 | ||
3027 | static void bfq_init_entity(struct bfq_entity *entity, | |
3028 | struct bfq_group *bfqg) | |
3029 | { | |
3030 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
3031 | ||
3032 | entity->weight = entity->new_weight; | |
3033 | entity->orig_weight = entity->new_weight; | |
3034 | if (bfqq) { | |
3035 | bfqq->ioprio = bfqq->new_ioprio; | |
3036 | bfqq->ioprio_class = bfqq->new_ioprio_class; | |
3037 | bfqg_get(bfqg); | |
3038 | } | |
3039 | entity->parent = bfqg->my_entity; /* NULL for root group */ | |
3040 | entity->sched_data = &bfqg->sched_data; | |
3041 | } | |
3042 | ||
3043 | static void bfqg_stats_exit(struct bfqg_stats *stats) | |
3044 | { | |
3045 | blkg_rwstat_exit(&stats->merged); | |
3046 | blkg_rwstat_exit(&stats->service_time); | |
3047 | blkg_rwstat_exit(&stats->wait_time); | |
3048 | blkg_rwstat_exit(&stats->queued); | |
3049 | blkg_stat_exit(&stats->time); | |
3050 | blkg_stat_exit(&stats->avg_queue_size_sum); | |
3051 | blkg_stat_exit(&stats->avg_queue_size_samples); | |
3052 | blkg_stat_exit(&stats->dequeue); | |
3053 | blkg_stat_exit(&stats->group_wait_time); | |
3054 | blkg_stat_exit(&stats->idle_time); | |
3055 | blkg_stat_exit(&stats->empty_time); | |
3056 | } | |
3057 | ||
3058 | static int bfqg_stats_init(struct bfqg_stats *stats, gfp_t gfp) | |
3059 | { | |
3060 | if (blkg_rwstat_init(&stats->merged, gfp) || | |
3061 | blkg_rwstat_init(&stats->service_time, gfp) || | |
3062 | blkg_rwstat_init(&stats->wait_time, gfp) || | |
3063 | blkg_rwstat_init(&stats->queued, gfp) || | |
3064 | blkg_stat_init(&stats->time, gfp) || | |
3065 | blkg_stat_init(&stats->avg_queue_size_sum, gfp) || | |
3066 | blkg_stat_init(&stats->avg_queue_size_samples, gfp) || | |
3067 | blkg_stat_init(&stats->dequeue, gfp) || | |
3068 | blkg_stat_init(&stats->group_wait_time, gfp) || | |
3069 | blkg_stat_init(&stats->idle_time, gfp) || | |
3070 | blkg_stat_init(&stats->empty_time, gfp)) { | |
3071 | bfqg_stats_exit(stats); | |
3072 | return -ENOMEM; | |
3073 | } | |
3074 | ||
3075 | return 0; | |
3076 | } | |
3077 | ||
3078 | static struct bfq_group_data *cpd_to_bfqgd(struct blkcg_policy_data *cpd) | |
3079 | { | |
3080 | return cpd ? container_of(cpd, struct bfq_group_data, pd) : NULL; | |
3081 | } | |
3082 | ||
3083 | static struct bfq_group_data *blkcg_to_bfqgd(struct blkcg *blkcg) | |
3084 | { | |
3085 | return cpd_to_bfqgd(blkcg_to_cpd(blkcg, &blkcg_policy_bfq)); | |
3086 | } | |
3087 | ||
3088 | static struct blkcg_policy_data *bfq_cpd_alloc(gfp_t gfp) | |
3089 | { | |
3090 | struct bfq_group_data *bgd; | |
3091 | ||
3092 | bgd = kzalloc(sizeof(*bgd), gfp); | |
3093 | if (!bgd) | |
3094 | return NULL; | |
3095 | return &bgd->pd; | |
3096 | } | |
3097 | ||
3098 | static void bfq_cpd_init(struct blkcg_policy_data *cpd) | |
3099 | { | |
3100 | struct bfq_group_data *d = cpd_to_bfqgd(cpd); | |
3101 | ||
3102 | d->weight = cgroup_subsys_on_dfl(io_cgrp_subsys) ? | |
3103 | CGROUP_WEIGHT_DFL : BFQ_WEIGHT_LEGACY_DFL; | |
3104 | } | |
3105 | ||
3106 | static void bfq_cpd_free(struct blkcg_policy_data *cpd) | |
3107 | { | |
3108 | kfree(cpd_to_bfqgd(cpd)); | |
3109 | } | |
3110 | ||
3111 | static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, int node) | |
3112 | { | |
3113 | struct bfq_group *bfqg; | |
3114 | ||
3115 | bfqg = kzalloc_node(sizeof(*bfqg), gfp, node); | |
3116 | if (!bfqg) | |
3117 | return NULL; | |
3118 | ||
3119 | if (bfqg_stats_init(&bfqg->stats, gfp)) { | |
3120 | kfree(bfqg); | |
3121 | return NULL; | |
3122 | } | |
3123 | ||
3124 | return &bfqg->pd; | |
3125 | } | |
3126 | ||
3127 | static void bfq_pd_init(struct blkg_policy_data *pd) | |
3128 | { | |
3129 | struct blkcg_gq *blkg = pd_to_blkg(pd); | |
3130 | struct bfq_group *bfqg = blkg_to_bfqg(blkg); | |
3131 | struct bfq_data *bfqd = blkg->q->elevator->elevator_data; | |
3132 | struct bfq_entity *entity = &bfqg->entity; | |
3133 | struct bfq_group_data *d = blkcg_to_bfqgd(blkg->blkcg); | |
3134 | ||
3135 | entity->orig_weight = entity->weight = entity->new_weight = d->weight; | |
3136 | entity->my_sched_data = &bfqg->sched_data; | |
3137 | bfqg->my_entity = entity; /* | |
3138 | * the root_group's will be set to NULL | |
3139 | * in bfq_init_queue() | |
3140 | */ | |
3141 | bfqg->bfqd = bfqd; | |
1de0c4cd | 3142 | bfqg->active_entities = 0; |
36eca894 | 3143 | bfqg->rq_pos_tree = RB_ROOT; |
e21b7a0b AA |
3144 | } |
3145 | ||
3146 | static void bfq_pd_free(struct blkg_policy_data *pd) | |
3147 | { | |
3148 | struct bfq_group *bfqg = pd_to_bfqg(pd); | |
3149 | ||
3150 | bfqg_stats_exit(&bfqg->stats); | |
3151 | return kfree(bfqg); | |
3152 | } | |
3153 | ||
3154 | static void bfq_pd_reset_stats(struct blkg_policy_data *pd) | |
3155 | { | |
3156 | struct bfq_group *bfqg = pd_to_bfqg(pd); | |
3157 | ||
3158 | bfqg_stats_reset(&bfqg->stats); | |
3159 | } | |
3160 | ||
3161 | static void bfq_group_set_parent(struct bfq_group *bfqg, | |
3162 | struct bfq_group *parent) | |
3163 | { | |
3164 | struct bfq_entity *entity; | |
3165 | ||
3166 | entity = &bfqg->entity; | |
3167 | entity->parent = parent->my_entity; | |
3168 | entity->sched_data = &parent->sched_data; | |
3169 | } | |
3170 | ||
3171 | static struct bfq_group *bfq_lookup_bfqg(struct bfq_data *bfqd, | |
3172 | struct blkcg *blkcg) | |
3173 | { | |
3174 | struct blkcg_gq *blkg; | |
3175 | ||
3176 | blkg = blkg_lookup(blkcg, bfqd->queue); | |
3177 | if (likely(blkg)) | |
3178 | return blkg_to_bfqg(blkg); | |
3179 | return NULL; | |
3180 | } | |
3181 | ||
3182 | static struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, | |
3183 | struct blkcg *blkcg) | |
3184 | { | |
3185 | struct bfq_group *bfqg, *parent; | |
3186 | struct bfq_entity *entity; | |
3187 | ||
3188 | bfqg = bfq_lookup_bfqg(bfqd, blkcg); | |
3189 | ||
3190 | if (unlikely(!bfqg)) | |
3191 | return NULL; | |
3192 | ||
3193 | /* | |
3194 | * Update chain of bfq_groups as we might be handling a leaf group | |
3195 | * which, along with some of its relatives, has not been hooked yet | |
3196 | * to the private hierarchy of BFQ. | |
3197 | */ | |
3198 | entity = &bfqg->entity; | |
3199 | for_each_entity(entity) { | |
3200 | bfqg = container_of(entity, struct bfq_group, entity); | |
3201 | if (bfqg != bfqd->root_group) { | |
3202 | parent = bfqg_parent(bfqg); | |
3203 | if (!parent) | |
3204 | parent = bfqd->root_group; | |
3205 | bfq_group_set_parent(bfqg, parent); | |
3206 | } | |
3207 | } | |
3208 | ||
3209 | return bfqg; | |
3210 | } | |
3211 | ||
36eca894 AA |
3212 | static void bfq_pos_tree_add_move(struct bfq_data *bfqd, |
3213 | struct bfq_queue *bfqq); | |
e21b7a0b AA |
3214 | static void bfq_bfqq_expire(struct bfq_data *bfqd, |
3215 | struct bfq_queue *bfqq, | |
3216 | bool compensate, | |
3217 | enum bfqq_expiration reason); | |
3218 | ||
3219 | /** | |
3220 | * bfq_bfqq_move - migrate @bfqq to @bfqg. | |
3221 | * @bfqd: queue descriptor. | |
3222 | * @bfqq: the queue to move. | |
3223 | * @bfqg: the group to move to. | |
3224 | * | |
3225 | * Move @bfqq to @bfqg, deactivating it from its old group and reactivating | |
3226 | * it on the new one. Avoid putting the entity on the old group idle tree. | |
3227 | * | |
3228 | * Must be called under the queue lock; the cgroup owning @bfqg must | |
3229 | * not disappear (by now this just means that we are called under | |
3230 | * rcu_read_lock()). | |
3231 | */ | |
3232 | static void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
3233 | struct bfq_group *bfqg) | |
3234 | { | |
3235 | struct bfq_entity *entity = &bfqq->entity; | |
3236 | ||
3237 | /* If bfqq is empty, then bfq_bfqq_expire also invokes | |
3238 | * bfq_del_bfqq_busy, thereby removing bfqq and its entity | |
3239 | * from data structures related to current group. Otherwise we | |
3240 | * need to remove bfqq explicitly with bfq_deactivate_bfqq, as | |
3241 | * we do below. | |
3242 | */ | |
3243 | if (bfqq == bfqd->in_service_queue) | |
3244 | bfq_bfqq_expire(bfqd, bfqd->in_service_queue, | |
3245 | false, BFQQE_PREEMPTED); | |
3246 | ||
3247 | if (bfq_bfqq_busy(bfqq)) | |
3248 | bfq_deactivate_bfqq(bfqd, bfqq, false, false); | |
3249 | else if (entity->on_st) | |
3250 | bfq_put_idle_entity(bfq_entity_service_tree(entity), entity); | |
3251 | bfqg_put(bfqq_group(bfqq)); | |
3252 | ||
3253 | /* | |
3254 | * Here we use a reference to bfqg. We don't need a refcounter | |
3255 | * as the cgroup reference will not be dropped, so that its | |
3256 | * destroy() callback will not be invoked. | |
3257 | */ | |
3258 | entity->parent = bfqg->my_entity; | |
3259 | entity->sched_data = &bfqg->sched_data; | |
3260 | bfqg_get(bfqg); | |
3261 | ||
36eca894 AA |
3262 | if (bfq_bfqq_busy(bfqq)) { |
3263 | bfq_pos_tree_add_move(bfqd, bfqq); | |
e21b7a0b | 3264 | bfq_activate_bfqq(bfqd, bfqq); |
36eca894 | 3265 | } |
e21b7a0b AA |
3266 | |
3267 | if (!bfqd->in_service_queue && !bfqd->rq_in_driver) | |
3268 | bfq_schedule_dispatch(bfqd); | |
3269 | } | |
3270 | ||
3271 | /** | |
3272 | * __bfq_bic_change_cgroup - move @bic to @cgroup. | |
3273 | * @bfqd: the queue descriptor. | |
3274 | * @bic: the bic to move. | |
3275 | * @blkcg: the blk-cgroup to move to. | |
3276 | * | |
3277 | * Move bic to blkcg, assuming that bfqd->queue is locked; the caller | |
3278 | * has to make sure that the reference to cgroup is valid across the call. | |
3279 | * | |
3280 | * NOTE: an alternative approach might have been to store the current | |
3281 | * cgroup in bfqq and getting a reference to it, reducing the lookup | |
3282 | * time here, at the price of slightly more complex code. | |
3283 | */ | |
3284 | static struct bfq_group *__bfq_bic_change_cgroup(struct bfq_data *bfqd, | |
3285 | struct bfq_io_cq *bic, | |
3286 | struct blkcg *blkcg) | |
3287 | { | |
3288 | struct bfq_queue *async_bfqq = bic_to_bfqq(bic, 0); | |
3289 | struct bfq_queue *sync_bfqq = bic_to_bfqq(bic, 1); | |
3290 | struct bfq_group *bfqg; | |
3291 | struct bfq_entity *entity; | |
3292 | ||
3293 | bfqg = bfq_find_set_group(bfqd, blkcg); | |
3294 | ||
3295 | if (unlikely(!bfqg)) | |
3296 | bfqg = bfqd->root_group; | |
3297 | ||
3298 | if (async_bfqq) { | |
3299 | entity = &async_bfqq->entity; | |
3300 | ||
3301 | if (entity->sched_data != &bfqg->sched_data) { | |
3302 | bic_set_bfqq(bic, NULL, 0); | |
3303 | bfq_log_bfqq(bfqd, async_bfqq, | |
3304 | "bic_change_group: %p %d", | |
36eca894 | 3305 | async_bfqq, async_bfqq->ref); |
e21b7a0b AA |
3306 | bfq_put_queue(async_bfqq); |
3307 | } | |
3308 | } | |
3309 | ||
3310 | if (sync_bfqq) { | |
3311 | entity = &sync_bfqq->entity; | |
3312 | if (entity->sched_data != &bfqg->sched_data) | |
3313 | bfq_bfqq_move(bfqd, sync_bfqq, bfqg); | |
3314 | } | |
3315 | ||
3316 | return bfqg; | |
3317 | } | |
3318 | ||
3319 | static void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) | |
3320 | { | |
3321 | struct bfq_data *bfqd = bic_to_bfqd(bic); | |
3322 | struct bfq_group *bfqg = NULL; | |
3323 | uint64_t serial_nr; | |
3324 | ||
3325 | rcu_read_lock(); | |
3326 | serial_nr = bio_blkcg(bio)->css.serial_nr; | |
3327 | ||
3328 | /* | |
3329 | * Check whether blkcg has changed. The condition may trigger | |
3330 | * spuriously on a newly created cic but there's no harm. | |
3331 | */ | |
3332 | if (unlikely(!bfqd) || likely(bic->blkcg_serial_nr == serial_nr)) | |
3333 | goto out; | |
3334 | ||
3335 | bfqg = __bfq_bic_change_cgroup(bfqd, bic, bio_blkcg(bio)); | |
3336 | bic->blkcg_serial_nr = serial_nr; | |
3337 | out: | |
3338 | rcu_read_unlock(); | |
3339 | } | |
3340 | ||
3341 | /** | |
3342 | * bfq_flush_idle_tree - deactivate any entity on the idle tree of @st. | |
3343 | * @st: the service tree being flushed. | |
3344 | */ | |
3345 | static void bfq_flush_idle_tree(struct bfq_service_tree *st) | |
3346 | { | |
3347 | struct bfq_entity *entity = st->first_idle; | |
3348 | ||
3349 | for (; entity ; entity = st->first_idle) | |
3350 | __bfq_deactivate_entity(entity, false); | |
3351 | } | |
3352 | ||
3353 | /** | |
3354 | * bfq_reparent_leaf_entity - move leaf entity to the root_group. | |
3355 | * @bfqd: the device data structure with the root group. | |
3356 | * @entity: the entity to move. | |
3357 | */ | |
3358 | static void bfq_reparent_leaf_entity(struct bfq_data *bfqd, | |
3359 | struct bfq_entity *entity) | |
3360 | { | |
3361 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
3362 | ||
3363 | bfq_bfqq_move(bfqd, bfqq, bfqd->root_group); | |
aee69d78 PV |
3364 | } |
3365 | ||
3366 | /** | |
e21b7a0b AA |
3367 | * bfq_reparent_active_entities - move to the root group all active |
3368 | * entities. | |
3369 | * @bfqd: the device data structure with the root group. | |
3370 | * @bfqg: the group to move from. | |
3371 | * @st: the service tree with the entities. | |
aee69d78 | 3372 | * |
e21b7a0b | 3373 | * Needs queue_lock to be taken and reference to be valid over the call. |
aee69d78 | 3374 | */ |
e21b7a0b AA |
3375 | static void bfq_reparent_active_entities(struct bfq_data *bfqd, |
3376 | struct bfq_group *bfqg, | |
3377 | struct bfq_service_tree *st) | |
aee69d78 | 3378 | { |
e21b7a0b AA |
3379 | struct rb_root *active = &st->active; |
3380 | struct bfq_entity *entity = NULL; | |
aee69d78 | 3381 | |
e21b7a0b AA |
3382 | if (!RB_EMPTY_ROOT(&st->active)) |
3383 | entity = bfq_entity_of(rb_first(active)); | |
aee69d78 | 3384 | |
e21b7a0b AA |
3385 | for (; entity ; entity = bfq_entity_of(rb_first(active))) |
3386 | bfq_reparent_leaf_entity(bfqd, entity); | |
aee69d78 | 3387 | |
e21b7a0b AA |
3388 | if (bfqg->sched_data.in_service_entity) |
3389 | bfq_reparent_leaf_entity(bfqd, | |
3390 | bfqg->sched_data.in_service_entity); | |
aee69d78 PV |
3391 | } |
3392 | ||
3393 | /** | |
e21b7a0b AA |
3394 | * bfq_pd_offline - deactivate the entity associated with @pd, |
3395 | * and reparent its children entities. | |
3396 | * @pd: descriptor of the policy going offline. | |
aee69d78 | 3397 | * |
e21b7a0b AA |
3398 | * blkio already grabs the queue_lock for us, so no need to use |
3399 | * RCU-based magic | |
aee69d78 | 3400 | */ |
e21b7a0b | 3401 | static void bfq_pd_offline(struct blkg_policy_data *pd) |
aee69d78 | 3402 | { |
e21b7a0b AA |
3403 | struct bfq_service_tree *st; |
3404 | struct bfq_group *bfqg = pd_to_bfqg(pd); | |
3405 | struct bfq_data *bfqd = bfqg->bfqd; | |
3406 | struct bfq_entity *entity = bfqg->my_entity; | |
3407 | unsigned long flags; | |
3408 | int i; | |
aee69d78 | 3409 | |
e21b7a0b AA |
3410 | if (!entity) /* root group */ |
3411 | return; | |
3412 | ||
3413 | spin_lock_irqsave(&bfqd->lock, flags); | |
aee69d78 | 3414 | /* |
e21b7a0b AA |
3415 | * Empty all service_trees belonging to this group before |
3416 | * deactivating the group itself. | |
aee69d78 | 3417 | */ |
e21b7a0b AA |
3418 | for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) { |
3419 | st = bfqg->sched_data.service_tree + i; | |
3420 | ||
3421 | /* | |
3422 | * The idle tree may still contain bfq_queues belonging | |
3423 | * to exited task because they never migrated to a different | |
3424 | * cgroup from the one being destroyed now. No one else | |
3425 | * can access them so it's safe to act without any lock. | |
3426 | */ | |
3427 | bfq_flush_idle_tree(st); | |
3428 | ||
3429 | /* | |
3430 | * It may happen that some queues are still active | |
3431 | * (busy) upon group destruction (if the corresponding | |
3432 | * processes have been forced to terminate). We move | |
3433 | * all the leaf entities corresponding to these queues | |
3434 | * to the root_group. | |
3435 | * Also, it may happen that the group has an entity | |
3436 | * in service, which is disconnected from the active | |
3437 | * tree: it must be moved, too. | |
3438 | * There is no need to put the sync queues, as the | |
3439 | * scheduler has taken no reference. | |
3440 | */ | |
3441 | bfq_reparent_active_entities(bfqd, bfqg, st); | |
aee69d78 PV |
3442 | } |
3443 | ||
e21b7a0b AA |
3444 | __bfq_deactivate_entity(entity, false); |
3445 | bfq_put_async_queues(bfqd, bfqg); | |
3446 | ||
6fa3e8d3 | 3447 | spin_unlock_irqrestore(&bfqd->lock, flags); |
e21b7a0b AA |
3448 | /* |
3449 | * @blkg is going offline and will be ignored by | |
3450 | * blkg_[rw]stat_recursive_sum(). Transfer stats to the parent so | |
3451 | * that they don't get lost. If IOs complete after this point, the | |
3452 | * stats for them will be lost. Oh well... | |
3453 | */ | |
3454 | bfqg_stats_xfer_dead(bfqg); | |
aee69d78 PV |
3455 | } |
3456 | ||
44e44a1b PV |
3457 | static void bfq_end_wr_async(struct bfq_data *bfqd) |
3458 | { | |
3459 | struct blkcg_gq *blkg; | |
3460 | ||
3461 | list_for_each_entry(blkg, &bfqd->queue->blkg_list, q_node) { | |
3462 | struct bfq_group *bfqg = blkg_to_bfqg(blkg); | |
3463 | ||
3464 | bfq_end_wr_async_queues(bfqd, bfqg); | |
3465 | } | |
3466 | bfq_end_wr_async_queues(bfqd, bfqd->root_group); | |
3467 | } | |
3468 | ||
e21b7a0b | 3469 | static int bfq_io_show_weight(struct seq_file *sf, void *v) |
aee69d78 | 3470 | { |
e21b7a0b AA |
3471 | struct blkcg *blkcg = css_to_blkcg(seq_css(sf)); |
3472 | struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg); | |
3473 | unsigned int val = 0; | |
aee69d78 | 3474 | |
e21b7a0b AA |
3475 | if (bfqgd) |
3476 | val = bfqgd->weight; | |
aee69d78 | 3477 | |
e21b7a0b | 3478 | seq_printf(sf, "%u\n", val); |
aee69d78 | 3479 | |
e21b7a0b AA |
3480 | return 0; |
3481 | } | |
3482 | ||
3483 | static int bfq_io_set_weight_legacy(struct cgroup_subsys_state *css, | |
3484 | struct cftype *cftype, | |
3485 | u64 val) | |
aee69d78 | 3486 | { |
e21b7a0b AA |
3487 | struct blkcg *blkcg = css_to_blkcg(css); |
3488 | struct bfq_group_data *bfqgd = blkcg_to_bfqgd(blkcg); | |
3489 | struct blkcg_gq *blkg; | |
3490 | int ret = -ERANGE; | |
aee69d78 | 3491 | |
e21b7a0b AA |
3492 | if (val < BFQ_MIN_WEIGHT || val > BFQ_MAX_WEIGHT) |
3493 | return ret; | |
aee69d78 | 3494 | |
e21b7a0b AA |
3495 | ret = 0; |
3496 | spin_lock_irq(&blkcg->lock); | |
3497 | bfqgd->weight = (unsigned short)val; | |
3498 | hlist_for_each_entry(blkg, &blkcg->blkg_list, blkcg_node) { | |
3499 | struct bfq_group *bfqg = blkg_to_bfqg(blkg); | |
3500 | ||
3501 | if (!bfqg) | |
3502 | continue; | |
3503 | /* | |
3504 | * Setting the prio_changed flag of the entity | |
3505 | * to 1 with new_weight == weight would re-set | |
3506 | * the value of the weight to its ioprio mapping. | |
3507 | * Set the flag only if necessary. | |
3508 | */ | |
3509 | if ((unsigned short)val != bfqg->entity.new_weight) { | |
3510 | bfqg->entity.new_weight = (unsigned short)val; | |
3511 | /* | |
3512 | * Make sure that the above new value has been | |
3513 | * stored in bfqg->entity.new_weight before | |
3514 | * setting the prio_changed flag. In fact, | |
3515 | * this flag may be read asynchronously (in | |
3516 | * critical sections protected by a different | |
3517 | * lock than that held here), and finding this | |
3518 | * flag set may cause the execution of the code | |
3519 | * for updating parameters whose value may | |
3520 | * depend also on bfqg->entity.new_weight (in | |
3521 | * __bfq_entity_update_weight_prio). | |
3522 | * This barrier makes sure that the new value | |
3523 | * of bfqg->entity.new_weight is correctly | |
3524 | * seen in that code. | |
3525 | */ | |
3526 | smp_wmb(); | |
3527 | bfqg->entity.prio_changed = 1; | |
3528 | } | |
aee69d78 | 3529 | } |
e21b7a0b | 3530 | spin_unlock_irq(&blkcg->lock); |
aee69d78 | 3531 | |
e21b7a0b AA |
3532 | return ret; |
3533 | } | |
aee69d78 | 3534 | |
e21b7a0b AA |
3535 | static ssize_t bfq_io_set_weight(struct kernfs_open_file *of, |
3536 | char *buf, size_t nbytes, | |
3537 | loff_t off) | |
3538 | { | |
3539 | u64 weight; | |
3540 | /* First unsigned long found in the file is used */ | |
3541 | int ret = kstrtoull(strim(buf), 0, &weight); | |
3542 | ||
3543 | if (ret) | |
3544 | return ret; | |
3545 | ||
3546 | return bfq_io_set_weight_legacy(of_css(of), NULL, weight); | |
aee69d78 PV |
3547 | } |
3548 | ||
e21b7a0b | 3549 | static int bfqg_print_stat(struct seq_file *sf, void *v) |
aee69d78 | 3550 | { |
e21b7a0b AA |
3551 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_stat, |
3552 | &blkcg_policy_bfq, seq_cft(sf)->private, false); | |
3553 | return 0; | |
3554 | } | |
aee69d78 | 3555 | |
e21b7a0b AA |
3556 | static int bfqg_print_rwstat(struct seq_file *sf, void *v) |
3557 | { | |
3558 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), blkg_prfill_rwstat, | |
3559 | &blkcg_policy_bfq, seq_cft(sf)->private, true); | |
3560 | return 0; | |
3561 | } | |
aee69d78 | 3562 | |
e21b7a0b AA |
3563 | static u64 bfqg_prfill_stat_recursive(struct seq_file *sf, |
3564 | struct blkg_policy_data *pd, int off) | |
3565 | { | |
3566 | u64 sum = blkg_stat_recursive_sum(pd_to_blkg(pd), | |
3567 | &blkcg_policy_bfq, off); | |
3568 | return __blkg_prfill_u64(sf, pd, sum); | |
3569 | } | |
aee69d78 | 3570 | |
e21b7a0b AA |
3571 | static u64 bfqg_prfill_rwstat_recursive(struct seq_file *sf, |
3572 | struct blkg_policy_data *pd, int off) | |
3573 | { | |
3574 | struct blkg_rwstat sum = blkg_rwstat_recursive_sum(pd_to_blkg(pd), | |
3575 | &blkcg_policy_bfq, | |
3576 | off); | |
3577 | return __blkg_prfill_rwstat(sf, pd, &sum); | |
aee69d78 PV |
3578 | } |
3579 | ||
e21b7a0b | 3580 | static int bfqg_print_stat_recursive(struct seq_file *sf, void *v) |
aee69d78 | 3581 | { |
e21b7a0b AA |
3582 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
3583 | bfqg_prfill_stat_recursive, &blkcg_policy_bfq, | |
3584 | seq_cft(sf)->private, false); | |
3585 | return 0; | |
3586 | } | |
aee69d78 | 3587 | |
e21b7a0b AA |
3588 | static int bfqg_print_rwstat_recursive(struct seq_file *sf, void *v) |
3589 | { | |
3590 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), | |
3591 | bfqg_prfill_rwstat_recursive, &blkcg_policy_bfq, | |
3592 | seq_cft(sf)->private, true); | |
3593 | return 0; | |
aee69d78 PV |
3594 | } |
3595 | ||
e21b7a0b AA |
3596 | static u64 bfqg_prfill_sectors(struct seq_file *sf, struct blkg_policy_data *pd, |
3597 | int off) | |
aee69d78 | 3598 | { |
e21b7a0b | 3599 | u64 sum = blkg_rwstat_total(&pd->blkg->stat_bytes); |
aee69d78 | 3600 | |
e21b7a0b | 3601 | return __blkg_prfill_u64(sf, pd, sum >> 9); |
aee69d78 PV |
3602 | } |
3603 | ||
e21b7a0b | 3604 | static int bfqg_print_stat_sectors(struct seq_file *sf, void *v) |
aee69d78 | 3605 | { |
e21b7a0b AA |
3606 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), |
3607 | bfqg_prfill_sectors, &blkcg_policy_bfq, 0, false); | |
3608 | return 0; | |
3609 | } | |
aee69d78 | 3610 | |
e21b7a0b AA |
3611 | static u64 bfqg_prfill_sectors_recursive(struct seq_file *sf, |
3612 | struct blkg_policy_data *pd, int off) | |
3613 | { | |
3614 | struct blkg_rwstat tmp = blkg_rwstat_recursive_sum(pd->blkg, NULL, | |
3615 | offsetof(struct blkcg_gq, stat_bytes)); | |
3616 | u64 sum = atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) + | |
3617 | atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]); | |
aee69d78 | 3618 | |
e21b7a0b AA |
3619 | return __blkg_prfill_u64(sf, pd, sum >> 9); |
3620 | } | |
aee69d78 | 3621 | |
e21b7a0b AA |
3622 | static int bfqg_print_stat_sectors_recursive(struct seq_file *sf, void *v) |
3623 | { | |
3624 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), | |
3625 | bfqg_prfill_sectors_recursive, &blkcg_policy_bfq, 0, | |
3626 | false); | |
3627 | return 0; | |
aee69d78 PV |
3628 | } |
3629 | ||
e21b7a0b AA |
3630 | static u64 bfqg_prfill_avg_queue_size(struct seq_file *sf, |
3631 | struct blkg_policy_data *pd, int off) | |
aee69d78 | 3632 | { |
e21b7a0b AA |
3633 | struct bfq_group *bfqg = pd_to_bfqg(pd); |
3634 | u64 samples = blkg_stat_read(&bfqg->stats.avg_queue_size_samples); | |
3635 | u64 v = 0; | |
aee69d78 | 3636 | |
e21b7a0b AA |
3637 | if (samples) { |
3638 | v = blkg_stat_read(&bfqg->stats.avg_queue_size_sum); | |
3639 | v = div64_u64(v, samples); | |
3640 | } | |
3641 | __blkg_prfill_u64(sf, pd, v); | |
3642 | return 0; | |
3643 | } | |
aee69d78 | 3644 | |
e21b7a0b AA |
3645 | /* print avg_queue_size */ |
3646 | static int bfqg_print_avg_queue_size(struct seq_file *sf, void *v) | |
3647 | { | |
3648 | blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), | |
3649 | bfqg_prfill_avg_queue_size, &blkcg_policy_bfq, | |
3650 | 0, false); | |
3651 | return 0; | |
3652 | } | |
3653 | ||
3654 | static struct bfq_group * | |
3655 | bfq_create_group_hierarchy(struct bfq_data *bfqd, int node) | |
3656 | { | |
3657 | int ret; | |
3658 | ||
3659 | ret = blkcg_activate_policy(bfqd->queue, &blkcg_policy_bfq); | |
3660 | if (ret) | |
3661 | return NULL; | |
3662 | ||
3663 | return blkg_to_bfqg(bfqd->queue->root_blkg); | |
aee69d78 PV |
3664 | } |
3665 | ||
e21b7a0b AA |
3666 | static struct cftype bfq_blkcg_legacy_files[] = { |
3667 | { | |
3668 | .name = "bfq.weight", | |
3669 | .flags = CFTYPE_NOT_ON_ROOT, | |
3670 | .seq_show = bfq_io_show_weight, | |
3671 | .write_u64 = bfq_io_set_weight_legacy, | |
3672 | }, | |
3673 | ||
3674 | /* statistics, covers only the tasks in the bfqg */ | |
3675 | { | |
3676 | .name = "bfq.time", | |
3677 | .private = offsetof(struct bfq_group, stats.time), | |
3678 | .seq_show = bfqg_print_stat, | |
3679 | }, | |
3680 | { | |
3681 | .name = "bfq.sectors", | |
3682 | .seq_show = bfqg_print_stat_sectors, | |
3683 | }, | |
3684 | { | |
3685 | .name = "bfq.io_service_bytes", | |
3686 | .private = (unsigned long)&blkcg_policy_bfq, | |
3687 | .seq_show = blkg_print_stat_bytes, | |
3688 | }, | |
3689 | { | |
3690 | .name = "bfq.io_serviced", | |
3691 | .private = (unsigned long)&blkcg_policy_bfq, | |
3692 | .seq_show = blkg_print_stat_ios, | |
3693 | }, | |
3694 | { | |
3695 | .name = "bfq.io_service_time", | |
3696 | .private = offsetof(struct bfq_group, stats.service_time), | |
3697 | .seq_show = bfqg_print_rwstat, | |
3698 | }, | |
3699 | { | |
3700 | .name = "bfq.io_wait_time", | |
3701 | .private = offsetof(struct bfq_group, stats.wait_time), | |
3702 | .seq_show = bfqg_print_rwstat, | |
3703 | }, | |
3704 | { | |
3705 | .name = "bfq.io_merged", | |
3706 | .private = offsetof(struct bfq_group, stats.merged), | |
3707 | .seq_show = bfqg_print_rwstat, | |
3708 | }, | |
3709 | { | |
3710 | .name = "bfq.io_queued", | |
3711 | .private = offsetof(struct bfq_group, stats.queued), | |
3712 | .seq_show = bfqg_print_rwstat, | |
3713 | }, | |
3714 | ||
3715 | /* the same statictics which cover the bfqg and its descendants */ | |
3716 | { | |
3717 | .name = "bfq.time_recursive", | |
3718 | .private = offsetof(struct bfq_group, stats.time), | |
3719 | .seq_show = bfqg_print_stat_recursive, | |
3720 | }, | |
3721 | { | |
3722 | .name = "bfq.sectors_recursive", | |
3723 | .seq_show = bfqg_print_stat_sectors_recursive, | |
3724 | }, | |
3725 | { | |
3726 | .name = "bfq.io_service_bytes_recursive", | |
3727 | .private = (unsigned long)&blkcg_policy_bfq, | |
3728 | .seq_show = blkg_print_stat_bytes_recursive, | |
3729 | }, | |
3730 | { | |
3731 | .name = "bfq.io_serviced_recursive", | |
3732 | .private = (unsigned long)&blkcg_policy_bfq, | |
3733 | .seq_show = blkg_print_stat_ios_recursive, | |
3734 | }, | |
3735 | { | |
3736 | .name = "bfq.io_service_time_recursive", | |
3737 | .private = offsetof(struct bfq_group, stats.service_time), | |
3738 | .seq_show = bfqg_print_rwstat_recursive, | |
3739 | }, | |
3740 | { | |
3741 | .name = "bfq.io_wait_time_recursive", | |
3742 | .private = offsetof(struct bfq_group, stats.wait_time), | |
3743 | .seq_show = bfqg_print_rwstat_recursive, | |
3744 | }, | |
3745 | { | |
3746 | .name = "bfq.io_merged_recursive", | |
3747 | .private = offsetof(struct bfq_group, stats.merged), | |
3748 | .seq_show = bfqg_print_rwstat_recursive, | |
3749 | }, | |
3750 | { | |
3751 | .name = "bfq.io_queued_recursive", | |
3752 | .private = offsetof(struct bfq_group, stats.queued), | |
3753 | .seq_show = bfqg_print_rwstat_recursive, | |
3754 | }, | |
3755 | { | |
3756 | .name = "bfq.avg_queue_size", | |
3757 | .seq_show = bfqg_print_avg_queue_size, | |
3758 | }, | |
3759 | { | |
3760 | .name = "bfq.group_wait_time", | |
3761 | .private = offsetof(struct bfq_group, stats.group_wait_time), | |
3762 | .seq_show = bfqg_print_stat, | |
3763 | }, | |
3764 | { | |
3765 | .name = "bfq.idle_time", | |
3766 | .private = offsetof(struct bfq_group, stats.idle_time), | |
3767 | .seq_show = bfqg_print_stat, | |
3768 | }, | |
3769 | { | |
3770 | .name = "bfq.empty_time", | |
3771 | .private = offsetof(struct bfq_group, stats.empty_time), | |
3772 | .seq_show = bfqg_print_stat, | |
3773 | }, | |
3774 | { | |
3775 | .name = "bfq.dequeue", | |
3776 | .private = offsetof(struct bfq_group, stats.dequeue), | |
3777 | .seq_show = bfqg_print_stat, | |
3778 | }, | |
3779 | { } /* terminate */ | |
3780 | }; | |
3781 | ||
3782 | static struct cftype bfq_blkg_files[] = { | |
3783 | { | |
3784 | .name = "bfq.weight", | |
3785 | .flags = CFTYPE_NOT_ON_ROOT, | |
3786 | .seq_show = bfq_io_show_weight, | |
3787 | .write = bfq_io_set_weight, | |
3788 | }, | |
3789 | {} /* terminate */ | |
3790 | }; | |
3791 | ||
3792 | #else /* CONFIG_BFQ_GROUP_IOSCHED */ | |
3793 | ||
3794 | static inline void bfqg_stats_update_io_add(struct bfq_group *bfqg, | |
3795 | struct bfq_queue *bfqq, unsigned int op) { } | |
3796 | static inline void | |
3797 | bfqg_stats_update_io_remove(struct bfq_group *bfqg, unsigned int op) { } | |
3798 | static inline void | |
3799 | bfqg_stats_update_io_merged(struct bfq_group *bfqg, unsigned int op) { } | |
3800 | static inline void bfqg_stats_update_completion(struct bfq_group *bfqg, | |
3801 | uint64_t start_time, uint64_t io_start_time, | |
3802 | unsigned int op) { } | |
3803 | static inline void | |
3804 | bfqg_stats_set_start_group_wait_time(struct bfq_group *bfqg, | |
3805 | struct bfq_group *curr_bfqg) { } | |
3806 | static inline void bfqg_stats_end_empty_time(struct bfqg_stats *stats) { } | |
3807 | static inline void bfqg_stats_update_dequeue(struct bfq_group *bfqg) { } | |
3808 | static inline void bfqg_stats_set_start_empty_time(struct bfq_group *bfqg) { } | |
3809 | static inline void bfqg_stats_update_idle_time(struct bfq_group *bfqg) { } | |
3810 | static inline void bfqg_stats_set_start_idle_time(struct bfq_group *bfqg) { } | |
3811 | static inline void bfqg_stats_update_avg_queue_size(struct bfq_group *bfqg) { } | |
3812 | ||
3813 | static void bfq_bfqq_move(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
3814 | struct bfq_group *bfqg) {} | |
3815 | ||
3816 | static void bfq_init_entity(struct bfq_entity *entity, | |
3817 | struct bfq_group *bfqg) | |
aee69d78 PV |
3818 | { |
3819 | struct bfq_queue *bfqq = bfq_entity_to_bfqq(entity); | |
3820 | ||
3821 | entity->weight = entity->new_weight; | |
3822 | entity->orig_weight = entity->new_weight; | |
e21b7a0b AA |
3823 | if (bfqq) { |
3824 | bfqq->ioprio = bfqq->new_ioprio; | |
3825 | bfqq->ioprio_class = bfqq->new_ioprio_class; | |
3826 | } | |
3827 | entity->sched_data = &bfqg->sched_data; | |
3828 | } | |
3829 | ||
3830 | static void bfq_bic_update_cgroup(struct bfq_io_cq *bic, struct bio *bio) {} | |
3831 | ||
44e44a1b PV |
3832 | static void bfq_end_wr_async(struct bfq_data *bfqd) |
3833 | { | |
3834 | bfq_end_wr_async_queues(bfqd, bfqd->root_group); | |
3835 | } | |
3836 | ||
e21b7a0b AA |
3837 | static struct bfq_group *bfq_find_set_group(struct bfq_data *bfqd, |
3838 | struct blkcg *blkcg) | |
3839 | { | |
3840 | return bfqd->root_group; | |
3841 | } | |
3842 | ||
3843 | static struct bfq_group *bfqq_group(struct bfq_queue *bfqq) | |
3844 | { | |
3845 | return bfqq->bfqd->root_group; | |
3846 | } | |
aee69d78 | 3847 | |
e21b7a0b AA |
3848 | static struct bfq_group *bfq_create_group_hierarchy(struct bfq_data *bfqd, |
3849 | int node) | |
3850 | { | |
3851 | struct bfq_group *bfqg; | |
3852 | int i; | |
3853 | ||
3854 | bfqg = kmalloc_node(sizeof(*bfqg), GFP_KERNEL | __GFP_ZERO, node); | |
3855 | if (!bfqg) | |
3856 | return NULL; | |
aee69d78 | 3857 | |
e21b7a0b AA |
3858 | for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) |
3859 | bfqg->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT; | |
3860 | ||
3861 | return bfqg; | |
aee69d78 | 3862 | } |
e21b7a0b | 3863 | #endif /* CONFIG_BFQ_GROUP_IOSCHED */ |
aee69d78 PV |
3864 | |
3865 | #define bfq_class_idle(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE) | |
3866 | #define bfq_class_rt(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_RT) | |
3867 | ||
3868 | #define bfq_sample_valid(samples) ((samples) > 80) | |
3869 | ||
aee69d78 PV |
3870 | /* |
3871 | * Lifted from AS - choose which of rq1 and rq2 that is best served now. | |
3872 | * We choose the request that is closesr to the head right now. Distance | |
3873 | * behind the head is penalized and only allowed to a certain extent. | |
3874 | */ | |
3875 | static struct request *bfq_choose_req(struct bfq_data *bfqd, | |
3876 | struct request *rq1, | |
3877 | struct request *rq2, | |
3878 | sector_t last) | |
3879 | { | |
3880 | sector_t s1, s2, d1 = 0, d2 = 0; | |
3881 | unsigned long back_max; | |
3882 | #define BFQ_RQ1_WRAP 0x01 /* request 1 wraps */ | |
3883 | #define BFQ_RQ2_WRAP 0x02 /* request 2 wraps */ | |
3884 | unsigned int wrap = 0; /* bit mask: requests behind the disk head? */ | |
3885 | ||
3886 | if (!rq1 || rq1 == rq2) | |
3887 | return rq2; | |
3888 | if (!rq2) | |
3889 | return rq1; | |
3890 | ||
3891 | if (rq_is_sync(rq1) && !rq_is_sync(rq2)) | |
3892 | return rq1; | |
3893 | else if (rq_is_sync(rq2) && !rq_is_sync(rq1)) | |
3894 | return rq2; | |
3895 | if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META)) | |
3896 | return rq1; | |
3897 | else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META)) | |
3898 | return rq2; | |
3899 | ||
3900 | s1 = blk_rq_pos(rq1); | |
3901 | s2 = blk_rq_pos(rq2); | |
3902 | ||
3903 | /* | |
3904 | * By definition, 1KiB is 2 sectors. | |
3905 | */ | |
3906 | back_max = bfqd->bfq_back_max * 2; | |
3907 | ||
3908 | /* | |
3909 | * Strict one way elevator _except_ in the case where we allow | |
3910 | * short backward seeks which are biased as twice the cost of a | |
3911 | * similar forward seek. | |
3912 | */ | |
3913 | if (s1 >= last) | |
3914 | d1 = s1 - last; | |
3915 | else if (s1 + back_max >= last) | |
3916 | d1 = (last - s1) * bfqd->bfq_back_penalty; | |
3917 | else | |
3918 | wrap |= BFQ_RQ1_WRAP; | |
3919 | ||
3920 | if (s2 >= last) | |
3921 | d2 = s2 - last; | |
3922 | else if (s2 + back_max >= last) | |
3923 | d2 = (last - s2) * bfqd->bfq_back_penalty; | |
3924 | else | |
3925 | wrap |= BFQ_RQ2_WRAP; | |
3926 | ||
3927 | /* Found required data */ | |
3928 | ||
3929 | /* | |
3930 | * By doing switch() on the bit mask "wrap" we avoid having to | |
3931 | * check two variables for all permutations: --> faster! | |
3932 | */ | |
3933 | switch (wrap) { | |
3934 | case 0: /* common case for CFQ: rq1 and rq2 not wrapped */ | |
3935 | if (d1 < d2) | |
3936 | return rq1; | |
3937 | else if (d2 < d1) | |
3938 | return rq2; | |
3939 | ||
3940 | if (s1 >= s2) | |
3941 | return rq1; | |
3942 | else | |
3943 | return rq2; | |
3944 | ||
3945 | case BFQ_RQ2_WRAP: | |
3946 | return rq1; | |
3947 | case BFQ_RQ1_WRAP: | |
3948 | return rq2; | |
3949 | case BFQ_RQ1_WRAP|BFQ_RQ2_WRAP: /* both rqs wrapped */ | |
3950 | default: | |
3951 | /* | |
3952 | * Since both rqs are wrapped, | |
3953 | * start with the one that's further behind head | |
3954 | * (--> only *one* back seek required), | |
3955 | * since back seek takes more time than forward. | |
3956 | */ | |
3957 | if (s1 <= s2) | |
3958 | return rq1; | |
3959 | else | |
3960 | return rq2; | |
3961 | } | |
3962 | } | |
3963 | ||
36eca894 AA |
3964 | static struct bfq_queue * |
3965 | bfq_rq_pos_tree_lookup(struct bfq_data *bfqd, struct rb_root *root, | |
3966 | sector_t sector, struct rb_node **ret_parent, | |
3967 | struct rb_node ***rb_link) | |
3968 | { | |
3969 | struct rb_node **p, *parent; | |
3970 | struct bfq_queue *bfqq = NULL; | |
3971 | ||
3972 | parent = NULL; | |
3973 | p = &root->rb_node; | |
3974 | while (*p) { | |
3975 | struct rb_node **n; | |
3976 | ||
3977 | parent = *p; | |
3978 | bfqq = rb_entry(parent, struct bfq_queue, pos_node); | |
3979 | ||
3980 | /* | |
3981 | * Sort strictly based on sector. Smallest to the left, | |
3982 | * largest to the right. | |
3983 | */ | |
3984 | if (sector > blk_rq_pos(bfqq->next_rq)) | |
3985 | n = &(*p)->rb_right; | |
3986 | else if (sector < blk_rq_pos(bfqq->next_rq)) | |
3987 | n = &(*p)->rb_left; | |
3988 | else | |
3989 | break; | |
3990 | p = n; | |
3991 | bfqq = NULL; | |
3992 | } | |
3993 | ||
3994 | *ret_parent = parent; | |
3995 | if (rb_link) | |
3996 | *rb_link = p; | |
3997 | ||
3998 | bfq_log(bfqd, "rq_pos_tree_lookup %llu: returning %d", | |
3999 | (unsigned long long)sector, | |
4000 | bfqq ? bfqq->pid : 0); | |
4001 | ||
4002 | return bfqq; | |
4003 | } | |
4004 | ||
4005 | static void bfq_pos_tree_add_move(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
4006 | { | |
4007 | struct rb_node **p, *parent; | |
4008 | struct bfq_queue *__bfqq; | |
4009 | ||
4010 | if (bfqq->pos_root) { | |
4011 | rb_erase(&bfqq->pos_node, bfqq->pos_root); | |
4012 | bfqq->pos_root = NULL; | |
4013 | } | |
4014 | ||
4015 | if (bfq_class_idle(bfqq)) | |
4016 | return; | |
4017 | if (!bfqq->next_rq) | |
4018 | return; | |
4019 | ||
4020 | bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree; | |
4021 | __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root, | |
4022 | blk_rq_pos(bfqq->next_rq), &parent, &p); | |
4023 | if (!__bfqq) { | |
4024 | rb_link_node(&bfqq->pos_node, parent, p); | |
4025 | rb_insert_color(&bfqq->pos_node, bfqq->pos_root); | |
4026 | } else | |
4027 | bfqq->pos_root = NULL; | |
4028 | } | |
4029 | ||
1de0c4cd AA |
4030 | /* |
4031 | * Tell whether there are active queues or groups with differentiated weights. | |
4032 | */ | |
4033 | static bool bfq_differentiated_weights(struct bfq_data *bfqd) | |
4034 | { | |
4035 | /* | |
4036 | * For weights to differ, at least one of the trees must contain | |
4037 | * at least two nodes. | |
4038 | */ | |
4039 | return (!RB_EMPTY_ROOT(&bfqd->queue_weights_tree) && | |
4040 | (bfqd->queue_weights_tree.rb_node->rb_left || | |
4041 | bfqd->queue_weights_tree.rb_node->rb_right) | |
4042 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
4043 | ) || | |
4044 | (!RB_EMPTY_ROOT(&bfqd->group_weights_tree) && | |
4045 | (bfqd->group_weights_tree.rb_node->rb_left || | |
4046 | bfqd->group_weights_tree.rb_node->rb_right) | |
4047 | #endif | |
4048 | ); | |
4049 | } | |
4050 | ||
4051 | /* | |
4052 | * The following function returns true if every queue must receive the | |
4053 | * same share of the throughput (this condition is used when deciding | |
4054 | * whether idling may be disabled, see the comments in the function | |
4055 | * bfq_bfqq_may_idle()). | |
4056 | * | |
4057 | * Such a scenario occurs when: | |
4058 | * 1) all active queues have the same weight, | |
4059 | * 2) all active groups at the same level in the groups tree have the same | |
4060 | * weight, | |
4061 | * 3) all active groups at the same level in the groups tree have the same | |
4062 | * number of children. | |
4063 | * | |
4064 | * Unfortunately, keeping the necessary state for evaluating exactly the | |
4065 | * above symmetry conditions would be quite complex and time-consuming. | |
4066 | * Therefore this function evaluates, instead, the following stronger | |
4067 | * sub-conditions, for which it is much easier to maintain the needed | |
4068 | * state: | |
4069 | * 1) all active queues have the same weight, | |
4070 | * 2) all active groups have the same weight, | |
4071 | * 3) all active groups have at most one active child each. | |
4072 | * In particular, the last two conditions are always true if hierarchical | |
4073 | * support and the cgroups interface are not enabled, thus no state needs | |
4074 | * to be maintained in this case. | |
4075 | */ | |
4076 | static bool bfq_symmetric_scenario(struct bfq_data *bfqd) | |
4077 | { | |
4078 | return !bfq_differentiated_weights(bfqd); | |
4079 | } | |
4080 | ||
4081 | /* | |
4082 | * If the weight-counter tree passed as input contains no counter for | |
4083 | * the weight of the input entity, then add that counter; otherwise just | |
4084 | * increment the existing counter. | |
4085 | * | |
4086 | * Note that weight-counter trees contain few nodes in mostly symmetric | |
4087 | * scenarios. For example, if all queues have the same weight, then the | |
4088 | * weight-counter tree for the queues may contain at most one node. | |
4089 | * This holds even if low_latency is on, because weight-raised queues | |
4090 | * are not inserted in the tree. | |
4091 | * In most scenarios, the rate at which nodes are created/destroyed | |
4092 | * should be low too. | |
4093 | */ | |
4094 | static void bfq_weights_tree_add(struct bfq_data *bfqd, | |
4095 | struct bfq_entity *entity, | |
4096 | struct rb_root *root) | |
4097 | { | |
4098 | struct rb_node **new = &(root->rb_node), *parent = NULL; | |
4099 | ||
4100 | /* | |
4101 | * Do not insert if the entity is already associated with a | |
4102 | * counter, which happens if: | |
4103 | * 1) the entity is associated with a queue, | |
4104 | * 2) a request arrival has caused the queue to become both | |
4105 | * non-weight-raised, and hence change its weight, and | |
4106 | * backlogged; in this respect, each of the two events | |
4107 | * causes an invocation of this function, | |
4108 | * 3) this is the invocation of this function caused by the | |
4109 | * second event. This second invocation is actually useless, | |
4110 | * and we handle this fact by exiting immediately. More | |
4111 | * efficient or clearer solutions might possibly be adopted. | |
4112 | */ | |
4113 | if (entity->weight_counter) | |
4114 | return; | |
4115 | ||
4116 | while (*new) { | |
4117 | struct bfq_weight_counter *__counter = container_of(*new, | |
4118 | struct bfq_weight_counter, | |
4119 | weights_node); | |
4120 | parent = *new; | |
4121 | ||
4122 | if (entity->weight == __counter->weight) { | |
4123 | entity->weight_counter = __counter; | |
4124 | goto inc_counter; | |
4125 | } | |
4126 | if (entity->weight < __counter->weight) | |
4127 | new = &((*new)->rb_left); | |
4128 | else | |
4129 | new = &((*new)->rb_right); | |
4130 | } | |
4131 | ||
4132 | entity->weight_counter = kzalloc(sizeof(struct bfq_weight_counter), | |
4133 | GFP_ATOMIC); | |
4134 | ||
4135 | /* | |
4136 | * In the unlucky event of an allocation failure, we just | |
4137 | * exit. This will cause the weight of entity to not be | |
4138 | * considered in bfq_differentiated_weights, which, in its | |
4139 | * turn, causes the scenario to be deemed wrongly symmetric in | |
4140 | * case entity's weight would have been the only weight making | |
4141 | * the scenario asymmetric. On the bright side, no unbalance | |
4142 | * will however occur when entity becomes inactive again (the | |
4143 | * invocation of this function is triggered by an activation | |
4144 | * of entity). In fact, bfq_weights_tree_remove does nothing | |
4145 | * if !entity->weight_counter. | |
4146 | */ | |
4147 | if (unlikely(!entity->weight_counter)) | |
4148 | return; | |
4149 | ||
4150 | entity->weight_counter->weight = entity->weight; | |
4151 | rb_link_node(&entity->weight_counter->weights_node, parent, new); | |
4152 | rb_insert_color(&entity->weight_counter->weights_node, root); | |
4153 | ||
4154 | inc_counter: | |
4155 | entity->weight_counter->num_active++; | |
4156 | } | |
4157 | ||
4158 | /* | |
4159 | * Decrement the weight counter associated with the entity, and, if the | |
4160 | * counter reaches 0, remove the counter from the tree. | |
4161 | * See the comments to the function bfq_weights_tree_add() for considerations | |
4162 | * about overhead. | |
4163 | */ | |
4164 | static void bfq_weights_tree_remove(struct bfq_data *bfqd, | |
4165 | struct bfq_entity *entity, | |
4166 | struct rb_root *root) | |
4167 | { | |
4168 | if (!entity->weight_counter) | |
4169 | return; | |
4170 | ||
4171 | entity->weight_counter->num_active--; | |
4172 | if (entity->weight_counter->num_active > 0) | |
4173 | goto reset_entity_pointer; | |
4174 | ||
4175 | rb_erase(&entity->weight_counter->weights_node, root); | |
4176 | kfree(entity->weight_counter); | |
4177 | ||
4178 | reset_entity_pointer: | |
4179 | entity->weight_counter = NULL; | |
4180 | } | |
4181 | ||
aee69d78 PV |
4182 | /* |
4183 | * Return expired entry, or NULL to just start from scratch in rbtree. | |
4184 | */ | |
4185 | static struct request *bfq_check_fifo(struct bfq_queue *bfqq, | |
4186 | struct request *last) | |
4187 | { | |
4188 | struct request *rq; | |
4189 | ||
4190 | if (bfq_bfqq_fifo_expire(bfqq)) | |
4191 | return NULL; | |
4192 | ||
4193 | bfq_mark_bfqq_fifo_expire(bfqq); | |
4194 | ||
4195 | rq = rq_entry_fifo(bfqq->fifo.next); | |
4196 | ||
4197 | if (rq == last || ktime_get_ns() < rq->fifo_time) | |
4198 | return NULL; | |
4199 | ||
4200 | bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq); | |
4201 | return rq; | |
4202 | } | |
4203 | ||
4204 | static struct request *bfq_find_next_rq(struct bfq_data *bfqd, | |
4205 | struct bfq_queue *bfqq, | |
4206 | struct request *last) | |
4207 | { | |
4208 | struct rb_node *rbnext = rb_next(&last->rb_node); | |
4209 | struct rb_node *rbprev = rb_prev(&last->rb_node); | |
4210 | struct request *next, *prev = NULL; | |
4211 | ||
4212 | /* Follow expired path, else get first next available. */ | |
4213 | next = bfq_check_fifo(bfqq, last); | |
4214 | if (next) | |
4215 | return next; | |
4216 | ||
4217 | if (rbprev) | |
4218 | prev = rb_entry_rq(rbprev); | |
4219 | ||
4220 | if (rbnext) | |
4221 | next = rb_entry_rq(rbnext); | |
4222 | else { | |
4223 | rbnext = rb_first(&bfqq->sort_list); | |
4224 | if (rbnext && rbnext != &last->rb_node) | |
4225 | next = rb_entry_rq(rbnext); | |
4226 | } | |
4227 | ||
4228 | return bfq_choose_req(bfqd, next, prev, blk_rq_pos(last)); | |
4229 | } | |
4230 | ||
c074170e | 4231 | /* see the definition of bfq_async_charge_factor for details */ |
aee69d78 PV |
4232 | static unsigned long bfq_serv_to_charge(struct request *rq, |
4233 | struct bfq_queue *bfqq) | |
4234 | { | |
44e44a1b | 4235 | if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1) |
c074170e PV |
4236 | return blk_rq_sectors(rq); |
4237 | ||
cfd69712 PV |
4238 | /* |
4239 | * If there are no weight-raised queues, then amplify service | |
4240 | * by just the async charge factor; otherwise amplify service | |
4241 | * by twice the async charge factor, to further reduce latency | |
4242 | * for weight-raised queues. | |
4243 | */ | |
4244 | if (bfqq->bfqd->wr_busy_queues == 0) | |
4245 | return blk_rq_sectors(rq) * bfq_async_charge_factor; | |
4246 | ||
4247 | return blk_rq_sectors(rq) * 2 * bfq_async_charge_factor; | |
aee69d78 PV |
4248 | } |
4249 | ||
4250 | /** | |
4251 | * bfq_updated_next_req - update the queue after a new next_rq selection. | |
4252 | * @bfqd: the device data the queue belongs to. | |
4253 | * @bfqq: the queue to update. | |
4254 | * | |
4255 | * If the first request of a queue changes we make sure that the queue | |
4256 | * has enough budget to serve at least its first request (if the | |
4257 | * request has grown). We do this because if the queue has not enough | |
4258 | * budget for its first request, it has to go through two dispatch | |
4259 | * rounds to actually get it dispatched. | |
4260 | */ | |
4261 | static void bfq_updated_next_req(struct bfq_data *bfqd, | |
4262 | struct bfq_queue *bfqq) | |
4263 | { | |
4264 | struct bfq_entity *entity = &bfqq->entity; | |
4265 | struct request *next_rq = bfqq->next_rq; | |
4266 | unsigned long new_budget; | |
4267 | ||
4268 | if (!next_rq) | |
4269 | return; | |
4270 | ||
4271 | if (bfqq == bfqd->in_service_queue) | |
4272 | /* | |
4273 | * In order not to break guarantees, budgets cannot be | |
4274 | * changed after an entity has been selected. | |
4275 | */ | |
4276 | return; | |
4277 | ||
4278 | new_budget = max_t(unsigned long, bfqq->max_budget, | |
4279 | bfq_serv_to_charge(next_rq, bfqq)); | |
4280 | if (entity->budget != new_budget) { | |
4281 | entity->budget = new_budget; | |
4282 | bfq_log_bfqq(bfqd, bfqq, "updated next rq: new budget %lu", | |
4283 | new_budget); | |
e21b7a0b | 4284 | bfq_requeue_bfqq(bfqd, bfqq); |
aee69d78 PV |
4285 | } |
4286 | } | |
4287 | ||
36eca894 AA |
4288 | static void |
4289 | bfq_bfqq_resume_state(struct bfq_queue *bfqq, struct bfq_io_cq *bic) | |
4290 | { | |
4291 | if (bic->saved_idle_window) | |
4292 | bfq_mark_bfqq_idle_window(bfqq); | |
4293 | else | |
4294 | bfq_clear_bfqq_idle_window(bfqq); | |
4295 | ||
4296 | if (bic->saved_IO_bound) | |
4297 | bfq_mark_bfqq_IO_bound(bfqq); | |
4298 | else | |
4299 | bfq_clear_bfqq_IO_bound(bfqq); | |
4300 | ||
4301 | bfqq->ttime = bic->saved_ttime; | |
4302 | bfqq->wr_coeff = bic->saved_wr_coeff; | |
4303 | bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt; | |
4304 | bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish; | |
4305 | bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time; | |
4306 | ||
e1b2324d | 4307 | if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) || |
36eca894 | 4308 | time_is_before_jiffies(bfqq->last_wr_start_finish + |
e1b2324d | 4309 | bfqq->wr_cur_max_time))) { |
36eca894 AA |
4310 | bfq_log_bfqq(bfqq->bfqd, bfqq, |
4311 | "resume state: switching off wr"); | |
4312 | ||
4313 | bfqq->wr_coeff = 1; | |
4314 | } | |
4315 | ||
4316 | /* make sure weight will be updated, however we got here */ | |
4317 | bfqq->entity.prio_changed = 1; | |
4318 | } | |
4319 | ||
4320 | static int bfqq_process_refs(struct bfq_queue *bfqq) | |
4321 | { | |
4322 | return bfqq->ref - bfqq->allocated - bfqq->entity.on_st; | |
4323 | } | |
4324 | ||
e1b2324d AA |
4325 | /* Empty burst list and add just bfqq (see comments on bfq_handle_burst) */ |
4326 | static void bfq_reset_burst_list(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
4327 | { | |
4328 | struct bfq_queue *item; | |
4329 | struct hlist_node *n; | |
4330 | ||
4331 | hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node) | |
4332 | hlist_del_init(&item->burst_list_node); | |
4333 | hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); | |
4334 | bfqd->burst_size = 1; | |
4335 | bfqd->burst_parent_entity = bfqq->entity.parent; | |
4336 | } | |
4337 | ||
4338 | /* Add bfqq to the list of queues in current burst (see bfq_handle_burst) */ | |
4339 | static void bfq_add_to_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
4340 | { | |
4341 | /* Increment burst size to take into account also bfqq */ | |
4342 | bfqd->burst_size++; | |
4343 | ||
4344 | if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) { | |
4345 | struct bfq_queue *pos, *bfqq_item; | |
4346 | struct hlist_node *n; | |
4347 | ||
4348 | /* | |
4349 | * Enough queues have been activated shortly after each | |
4350 | * other to consider this burst as large. | |
4351 | */ | |
4352 | bfqd->large_burst = true; | |
4353 | ||
4354 | /* | |
4355 | * We can now mark all queues in the burst list as | |
4356 | * belonging to a large burst. | |
4357 | */ | |
4358 | hlist_for_each_entry(bfqq_item, &bfqd->burst_list, | |
4359 | burst_list_node) | |
4360 | bfq_mark_bfqq_in_large_burst(bfqq_item); | |
4361 | bfq_mark_bfqq_in_large_burst(bfqq); | |
4362 | ||
4363 | /* | |
4364 | * From now on, and until the current burst finishes, any | |
4365 | * new queue being activated shortly after the last queue | |
4366 | * was inserted in the burst can be immediately marked as | |
4367 | * belonging to a large burst. So the burst list is not | |
4368 | * needed any more. Remove it. | |
4369 | */ | |
4370 | hlist_for_each_entry_safe(pos, n, &bfqd->burst_list, | |
4371 | burst_list_node) | |
4372 | hlist_del_init(&pos->burst_list_node); | |
4373 | } else /* | |
4374 | * Burst not yet large: add bfqq to the burst list. Do | |
4375 | * not increment the ref counter for bfqq, because bfqq | |
4376 | * is removed from the burst list before freeing bfqq | |
4377 | * in put_queue. | |
4378 | */ | |
4379 | hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); | |
4380 | } | |
4381 | ||
4382 | /* | |
4383 | * If many queues belonging to the same group happen to be created | |
4384 | * shortly after each other, then the processes associated with these | |
4385 | * queues have typically a common goal. In particular, bursts of queue | |
4386 | * creations are usually caused by services or applications that spawn | |
4387 | * many parallel threads/processes. Examples are systemd during boot, | |
4388 | * or git grep. To help these processes get their job done as soon as | |
4389 | * possible, it is usually better to not grant either weight-raising | |
4390 | * or device idling to their queues. | |
4391 | * | |
4392 | * In this comment we describe, firstly, the reasons why this fact | |
4393 | * holds, and, secondly, the next function, which implements the main | |
4394 | * steps needed to properly mark these queues so that they can then be | |
4395 | * treated in a different way. | |
4396 | * | |
4397 | * The above services or applications benefit mostly from a high | |
4398 | * throughput: the quicker the requests of the activated queues are | |
4399 | * cumulatively served, the sooner the target job of these queues gets | |
4400 | * completed. As a consequence, weight-raising any of these queues, | |
4401 | * which also implies idling the device for it, is almost always | |
4402 | * counterproductive. In most cases it just lowers throughput. | |
4403 | * | |
4404 | * On the other hand, a burst of queue creations may be caused also by | |
4405 | * the start of an application that does not consist of a lot of | |
4406 | * parallel I/O-bound threads. In fact, with a complex application, | |
4407 | * several short processes may need to be executed to start-up the | |
4408 | * application. In this respect, to start an application as quickly as | |
4409 | * possible, the best thing to do is in any case to privilege the I/O | |
4410 | * related to the application with respect to all other | |
4411 | * I/O. Therefore, the best strategy to start as quickly as possible | |
4412 | * an application that causes a burst of queue creations is to | |
4413 | * weight-raise all the queues created during the burst. This is the | |
4414 | * exact opposite of the best strategy for the other type of bursts. | |
4415 | * | |
4416 | * In the end, to take the best action for each of the two cases, the | |
4417 | * two types of bursts need to be distinguished. Fortunately, this | |
4418 | * seems relatively easy, by looking at the sizes of the bursts. In | |
4419 | * particular, we found a threshold such that only bursts with a | |
4420 | * larger size than that threshold are apparently caused by | |
4421 | * services or commands such as systemd or git grep. For brevity, | |
4422 | * hereafter we call just 'large' these bursts. BFQ *does not* | |
4423 | * weight-raise queues whose creation occurs in a large burst. In | |
4424 | * addition, for each of these queues BFQ performs or does not perform | |
4425 | * idling depending on which choice boosts the throughput more. The | |
4426 | * exact choice depends on the device and request pattern at | |
4427 | * hand. | |
4428 | * | |
4429 | * Unfortunately, false positives may occur while an interactive task | |
4430 | * is starting (e.g., an application is being started). The | |
4431 | * consequence is that the queues associated with the task do not | |
4432 | * enjoy weight raising as expected. Fortunately these false positives | |
4433 | * are very rare. They typically occur if some service happens to | |
4434 | * start doing I/O exactly when the interactive task starts. | |
4435 | * | |
4436 | * Turning back to the next function, it implements all the steps | |
4437 | * needed to detect the occurrence of a large burst and to properly | |
4438 | * mark all the queues belonging to it (so that they can then be | |
4439 | * treated in a different way). This goal is achieved by maintaining a | |
4440 | * "burst list" that holds, temporarily, the queues that belong to the | |
4441 | * burst in progress. The list is then used to mark these queues as | |
4442 | * belonging to a large burst if the burst does become large. The main | |
4443 | * steps are the following. | |
4444 | * | |
4445 | * . when the very first queue is created, the queue is inserted into the | |
4446 | * list (as it could be the first queue in a possible burst) | |
4447 | * | |
4448 | * . if the current burst has not yet become large, and a queue Q that does | |
4449 | * not yet belong to the burst is activated shortly after the last time | |
4450 | * at which a new queue entered the burst list, then the function appends | |
4451 | * Q to the burst list | |
4452 | * | |
4453 | * . if, as a consequence of the previous step, the burst size reaches | |
4454 | * the large-burst threshold, then | |
4455 | * | |
4456 | * . all the queues in the burst list are marked as belonging to a | |
4457 | * large burst | |
4458 | * | |
4459 | * . the burst list is deleted; in fact, the burst list already served | |
4460 | * its purpose (keeping temporarily track of the queues in a burst, | |
4461 | * so as to be able to mark them as belonging to a large burst in the | |
4462 | * previous sub-step), and now is not needed any more | |
4463 | * | |
4464 | * . the device enters a large-burst mode | |
4465 | * | |
4466 | * . if a queue Q that does not belong to the burst is created while | |
4467 | * the device is in large-burst mode and shortly after the last time | |
4468 | * at which a queue either entered the burst list or was marked as | |
4469 | * belonging to the current large burst, then Q is immediately marked | |
4470 | * as belonging to a large burst. | |
4471 | * | |
4472 | * . if a queue Q that does not belong to the burst is created a while | |
4473 | * later, i.e., not shortly after, than the last time at which a queue | |
4474 | * either entered the burst list or was marked as belonging to the | |
4475 | * current large burst, then the current burst is deemed as finished and: | |
4476 | * | |
4477 | * . the large-burst mode is reset if set | |
4478 | * | |
4479 | * . the burst list is emptied | |
4480 | * | |
4481 | * . Q is inserted in the burst list, as Q may be the first queue | |
4482 | * in a possible new burst (then the burst list contains just Q | |
4483 | * after this step). | |
4484 | */ | |
4485 | static void bfq_handle_burst(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
4486 | { | |
4487 | /* | |
4488 | * If bfqq is already in the burst list or is part of a large | |
4489 | * burst, or finally has just been split, then there is | |
4490 | * nothing else to do. | |
4491 | */ | |
4492 | if (!hlist_unhashed(&bfqq->burst_list_node) || | |
4493 | bfq_bfqq_in_large_burst(bfqq) || | |
4494 | time_is_after_eq_jiffies(bfqq->split_time + | |
4495 | msecs_to_jiffies(10))) | |
4496 | return; | |
4497 | ||
4498 | /* | |
4499 | * If bfqq's creation happens late enough, or bfqq belongs to | |
4500 | * a different group than the burst group, then the current | |
4501 | * burst is finished, and related data structures must be | |
4502 | * reset. | |
4503 | * | |
4504 | * In this respect, consider the special case where bfqq is | |
4505 | * the very first queue created after BFQ is selected for this | |
4506 | * device. In this case, last_ins_in_burst and | |
4507 | * burst_parent_entity are not yet significant when we get | |
4508 | * here. But it is easy to verify that, whether or not the | |
4509 | * following condition is true, bfqq will end up being | |
4510 | * inserted into the burst list. In particular the list will | |
4511 | * happen to contain only bfqq. And this is exactly what has | |
4512 | * to happen, as bfqq may be the first queue of the first | |
4513 | * burst. | |
4514 | */ | |
4515 | if (time_is_before_jiffies(bfqd->last_ins_in_burst + | |
4516 | bfqd->bfq_burst_interval) || | |
4517 | bfqq->entity.parent != bfqd->burst_parent_entity) { | |
4518 | bfqd->large_burst = false; | |
4519 | bfq_reset_burst_list(bfqd, bfqq); | |
4520 | goto end; | |
4521 | } | |
4522 | ||
4523 | /* | |
4524 | * If we get here, then bfqq is being activated shortly after the | |
4525 | * last queue. So, if the current burst is also large, we can mark | |
4526 | * bfqq as belonging to this large burst immediately. | |
4527 | */ | |
4528 | if (bfqd->large_burst) { | |
4529 | bfq_mark_bfqq_in_large_burst(bfqq); | |
4530 | goto end; | |
4531 | } | |
4532 | ||
4533 | /* | |
4534 | * If we get here, then a large-burst state has not yet been | |
4535 | * reached, but bfqq is being activated shortly after the last | |
4536 | * queue. Then we add bfqq to the burst. | |
4537 | */ | |
4538 | bfq_add_to_burst(bfqd, bfqq); | |
4539 | end: | |
4540 | /* | |
4541 | * At this point, bfqq either has been added to the current | |
4542 | * burst or has caused the current burst to terminate and a | |
4543 | * possible new burst to start. In particular, in the second | |
4544 | * case, bfqq has become the first queue in the possible new | |
4545 | * burst. In both cases last_ins_in_burst needs to be moved | |
4546 | * forward. | |
4547 | */ | |
4548 | bfqd->last_ins_in_burst = jiffies; | |
4549 | } | |
4550 | ||
aee69d78 PV |
4551 | static int bfq_bfqq_budget_left(struct bfq_queue *bfqq) |
4552 | { | |
4553 | struct bfq_entity *entity = &bfqq->entity; | |
4554 | ||
4555 | return entity->budget - entity->service; | |
4556 | } | |
4557 | ||
4558 | /* | |
4559 | * If enough samples have been computed, return the current max budget | |
4560 | * stored in bfqd, which is dynamically updated according to the | |
4561 | * estimated disk peak rate; otherwise return the default max budget | |
4562 | */ | |
4563 | static int bfq_max_budget(struct bfq_data *bfqd) | |
4564 | { | |
4565 | if (bfqd->budgets_assigned < bfq_stats_min_budgets) | |
4566 | return bfq_default_max_budget; | |
4567 | else | |
4568 | return bfqd->bfq_max_budget; | |
4569 | } | |
4570 | ||
4571 | /* | |
4572 | * Return min budget, which is a fraction of the current or default | |
4573 | * max budget (trying with 1/32) | |
4574 | */ | |
4575 | static int bfq_min_budget(struct bfq_data *bfqd) | |
4576 | { | |
4577 | if (bfqd->budgets_assigned < bfq_stats_min_budgets) | |
4578 | return bfq_default_max_budget / 32; | |
4579 | else | |
4580 | return bfqd->bfq_max_budget / 32; | |
4581 | } | |
4582 | ||
4583 | static void bfq_bfqq_expire(struct bfq_data *bfqd, | |
4584 | struct bfq_queue *bfqq, | |
4585 | bool compensate, | |
4586 | enum bfqq_expiration reason); | |
4587 | ||
4588 | /* | |
4589 | * The next function, invoked after the input queue bfqq switches from | |
4590 | * idle to busy, updates the budget of bfqq. The function also tells | |
4591 | * whether the in-service queue should be expired, by returning | |
4592 | * true. The purpose of expiring the in-service queue is to give bfqq | |
4593 | * the chance to possibly preempt the in-service queue, and the reason | |
44e44a1b PV |
4594 | * for preempting the in-service queue is to achieve one of the two |
4595 | * goals below. | |
aee69d78 | 4596 | * |
44e44a1b PV |
4597 | * 1. Guarantee to bfqq its reserved bandwidth even if bfqq has |
4598 | * expired because it has remained idle. In particular, bfqq may have | |
4599 | * expired for one of the following two reasons: | |
aee69d78 PV |
4600 | * |
4601 | * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling | |
4602 | * and did not make it to issue a new request before its last | |
4603 | * request was served; | |
4604 | * | |
4605 | * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue | |
4606 | * a new request before the expiration of the idling-time. | |
4607 | * | |
4608 | * Even if bfqq has expired for one of the above reasons, the process | |
4609 | * associated with the queue may be however issuing requests greedily, | |
4610 | * and thus be sensitive to the bandwidth it receives (bfqq may have | |
4611 | * remained idle for other reasons: CPU high load, bfqq not enjoying | |
4612 | * idling, I/O throttling somewhere in the path from the process to | |
4613 | * the I/O scheduler, ...). But if, after every expiration for one of | |
4614 | * the above two reasons, bfqq has to wait for the service of at least | |
4615 | * one full budget of another queue before being served again, then | |
4616 | * bfqq is likely to get a much lower bandwidth or resource time than | |
4617 | * its reserved ones. To address this issue, two countermeasures need | |
4618 | * to be taken. | |
4619 | * | |
4620 | * First, the budget and the timestamps of bfqq need to be updated in | |
4621 | * a special way on bfqq reactivation: they need to be updated as if | |
4622 | * bfqq did not remain idle and did not expire. In fact, if they are | |
4623 | * computed as if bfqq expired and remained idle until reactivation, | |
4624 | * then the process associated with bfqq is treated as if, instead of | |
4625 | * being greedy, it stopped issuing requests when bfqq remained idle, | |
4626 | * and restarts issuing requests only on this reactivation. In other | |
4627 | * words, the scheduler does not help the process recover the "service | |
4628 | * hole" between bfqq expiration and reactivation. As a consequence, | |
4629 | * the process receives a lower bandwidth than its reserved one. In | |
4630 | * contrast, to recover this hole, the budget must be updated as if | |
4631 | * bfqq was not expired at all before this reactivation, i.e., it must | |
4632 | * be set to the value of the remaining budget when bfqq was | |
4633 | * expired. Along the same line, timestamps need to be assigned the | |
4634 | * value they had the last time bfqq was selected for service, i.e., | |
4635 | * before last expiration. Thus timestamps need to be back-shifted | |
4636 | * with respect to their normal computation (see [1] for more details | |
4637 | * on this tricky aspect). | |
4638 | * | |
4639 | * Secondly, to allow the process to recover the hole, the in-service | |
4640 | * queue must be expired too, to give bfqq the chance to preempt it | |
4641 | * immediately. In fact, if bfqq has to wait for a full budget of the | |
4642 | * in-service queue to be completed, then it may become impossible to | |
4643 | * let the process recover the hole, even if the back-shifted | |
4644 | * timestamps of bfqq are lower than those of the in-service queue. If | |
4645 | * this happens for most or all of the holes, then the process may not | |
4646 | * receive its reserved bandwidth. In this respect, it is worth noting | |
4647 | * that, being the service of outstanding requests unpreemptible, a | |
4648 | * little fraction of the holes may however be unrecoverable, thereby | |
4649 | * causing a little loss of bandwidth. | |
4650 | * | |
4651 | * The last important point is detecting whether bfqq does need this | |
4652 | * bandwidth recovery. In this respect, the next function deems the | |
4653 | * process associated with bfqq greedy, and thus allows it to recover | |
4654 | * the hole, if: 1) the process is waiting for the arrival of a new | |
4655 | * request (which implies that bfqq expired for one of the above two | |
4656 | * reasons), and 2) such a request has arrived soon. The first | |
4657 | * condition is controlled through the flag non_blocking_wait_rq, | |
4658 | * while the second through the flag arrived_in_time. If both | |
4659 | * conditions hold, then the function computes the budget in the | |
4660 | * above-described special way, and signals that the in-service queue | |
4661 | * should be expired. Timestamp back-shifting is done later in | |
4662 | * __bfq_activate_entity. | |
44e44a1b PV |
4663 | * |
4664 | * 2. Reduce latency. Even if timestamps are not backshifted to let | |
4665 | * the process associated with bfqq recover a service hole, bfqq may | |
4666 | * however happen to have, after being (re)activated, a lower finish | |
4667 | * timestamp than the in-service queue. That is, the next budget of | |
4668 | * bfqq may have to be completed before the one of the in-service | |
4669 | * queue. If this is the case, then preempting the in-service queue | |
4670 | * allows this goal to be achieved, apart from the unpreemptible, | |
4671 | * outstanding requests mentioned above. | |
4672 | * | |
4673 | * Unfortunately, regardless of which of the above two goals one wants | |
4674 | * to achieve, service trees need first to be updated to know whether | |
4675 | * the in-service queue must be preempted. To have service trees | |
4676 | * correctly updated, the in-service queue must be expired and | |
4677 | * rescheduled, and bfqq must be scheduled too. This is one of the | |
4678 | * most costly operations (in future versions, the scheduling | |
4679 | * mechanism may be re-designed in such a way to make it possible to | |
4680 | * know whether preemption is needed without needing to update service | |
4681 | * trees). In addition, queue preemptions almost always cause random | |
4682 | * I/O, and thus loss of throughput. Because of these facts, the next | |
4683 | * function adopts the following simple scheme to avoid both costly | |
4684 | * operations and too frequent preemptions: it requests the expiration | |
4685 | * of the in-service queue (unconditionally) only for queues that need | |
4686 | * to recover a hole, or that either are weight-raised or deserve to | |
4687 | * be weight-raised. | |
aee69d78 PV |
4688 | */ |
4689 | static bool bfq_bfqq_update_budg_for_activation(struct bfq_data *bfqd, | |
4690 | struct bfq_queue *bfqq, | |
44e44a1b PV |
4691 | bool arrived_in_time, |
4692 | bool wr_or_deserves_wr) | |
aee69d78 PV |
4693 | { |
4694 | struct bfq_entity *entity = &bfqq->entity; | |
4695 | ||
4696 | if (bfq_bfqq_non_blocking_wait_rq(bfqq) && arrived_in_time) { | |
4697 | /* | |
4698 | * We do not clear the flag non_blocking_wait_rq here, as | |
4699 | * the latter is used in bfq_activate_bfqq to signal | |
4700 | * that timestamps need to be back-shifted (and is | |
4701 | * cleared right after). | |
4702 | */ | |
4703 | ||
4704 | /* | |
4705 | * In next assignment we rely on that either | |
4706 | * entity->service or entity->budget are not updated | |
4707 | * on expiration if bfqq is empty (see | |
4708 | * __bfq_bfqq_recalc_budget). Thus both quantities | |
4709 | * remain unchanged after such an expiration, and the | |
4710 | * following statement therefore assigns to | |
4711 | * entity->budget the remaining budget on such an | |
4712 | * expiration. For clarity, entity->service is not | |
4713 | * updated on expiration in any case, and, in normal | |
4714 | * operation, is reset only when bfqq is selected for | |
4715 | * service (see bfq_get_next_queue). | |
4716 | */ | |
4717 | entity->budget = min_t(unsigned long, | |
4718 | bfq_bfqq_budget_left(bfqq), | |
4719 | bfqq->max_budget); | |
4720 | ||
4721 | return true; | |
4722 | } | |
4723 | ||
4724 | entity->budget = max_t(unsigned long, bfqq->max_budget, | |
4725 | bfq_serv_to_charge(bfqq->next_rq, bfqq)); | |
4726 | bfq_clear_bfqq_non_blocking_wait_rq(bfqq); | |
44e44a1b PV |
4727 | return wr_or_deserves_wr; |
4728 | } | |
4729 | ||
4730 | static unsigned int bfq_wr_duration(struct bfq_data *bfqd) | |
4731 | { | |
4732 | u64 dur; | |
4733 | ||
4734 | if (bfqd->bfq_wr_max_time > 0) | |
4735 | return bfqd->bfq_wr_max_time; | |
4736 | ||
4737 | dur = bfqd->RT_prod; | |
4738 | do_div(dur, bfqd->peak_rate); | |
4739 | ||
4740 | /* | |
4741 | * Limit duration between 3 and 13 seconds. Tests show that | |
4742 | * higher values than 13 seconds often yield the opposite of | |
4743 | * the desired result, i.e., worsen responsiveness by letting | |
4744 | * non-interactive and non-soft-real-time applications | |
4745 | * preserve weight raising for a too long time interval. | |
4746 | * | |
4747 | * On the other end, lower values than 3 seconds make it | |
4748 | * difficult for most interactive tasks to complete their jobs | |
4749 | * before weight-raising finishes. | |
4750 | */ | |
4751 | if (dur > msecs_to_jiffies(13000)) | |
4752 | dur = msecs_to_jiffies(13000); | |
4753 | else if (dur < msecs_to_jiffies(3000)) | |
4754 | dur = msecs_to_jiffies(3000); | |
4755 | ||
4756 | return dur; | |
4757 | } | |
4758 | ||
4759 | static void bfq_update_bfqq_wr_on_rq_arrival(struct bfq_data *bfqd, | |
4760 | struct bfq_queue *bfqq, | |
4761 | unsigned int old_wr_coeff, | |
4762 | bool wr_or_deserves_wr, | |
77b7dcea | 4763 | bool interactive, |
e1b2324d | 4764 | bool in_burst, |
77b7dcea | 4765 | bool soft_rt) |
44e44a1b PV |
4766 | { |
4767 | if (old_wr_coeff == 1 && wr_or_deserves_wr) { | |
4768 | /* start a weight-raising period */ | |
77b7dcea PV |
4769 | if (interactive) { |
4770 | bfqq->wr_coeff = bfqd->bfq_wr_coeff; | |
4771 | bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); | |
4772 | } else { | |
4773 | bfqq->wr_start_at_switch_to_srt = jiffies; | |
4774 | bfqq->wr_coeff = bfqd->bfq_wr_coeff * | |
4775 | BFQ_SOFTRT_WEIGHT_FACTOR; | |
4776 | bfqq->wr_cur_max_time = | |
4777 | bfqd->bfq_wr_rt_max_time; | |
4778 | } | |
44e44a1b PV |
4779 | |
4780 | /* | |
4781 | * If needed, further reduce budget to make sure it is | |
4782 | * close to bfqq's backlog, so as to reduce the | |
4783 | * scheduling-error component due to a too large | |
4784 | * budget. Do not care about throughput consequences, | |
4785 | * but only about latency. Finally, do not assign a | |
4786 | * too small budget either, to avoid increasing | |
4787 | * latency by causing too frequent expirations. | |
4788 | */ | |
4789 | bfqq->entity.budget = min_t(unsigned long, | |
4790 | bfqq->entity.budget, | |
4791 | 2 * bfq_min_budget(bfqd)); | |
4792 | } else if (old_wr_coeff > 1) { | |
77b7dcea PV |
4793 | if (interactive) { /* update wr coeff and duration */ |
4794 | bfqq->wr_coeff = bfqd->bfq_wr_coeff; | |
4795 | bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); | |
e1b2324d AA |
4796 | } else if (in_burst) |
4797 | bfqq->wr_coeff = 1; | |
4798 | else if (soft_rt) { | |
77b7dcea PV |
4799 | /* |
4800 | * The application is now or still meeting the | |
4801 | * requirements for being deemed soft rt. We | |
4802 | * can then correctly and safely (re)charge | |
4803 | * the weight-raising duration for the | |
4804 | * application with the weight-raising | |
4805 | * duration for soft rt applications. | |
4806 | * | |
4807 | * In particular, doing this recharge now, i.e., | |
4808 | * before the weight-raising period for the | |
4809 | * application finishes, reduces the probability | |
4810 | * of the following negative scenario: | |
4811 | * 1) the weight of a soft rt application is | |
4812 | * raised at startup (as for any newly | |
4813 | * created application), | |
4814 | * 2) since the application is not interactive, | |
4815 | * at a certain time weight-raising is | |
4816 | * stopped for the application, | |
4817 | * 3) at that time the application happens to | |
4818 | * still have pending requests, and hence | |
4819 | * is destined to not have a chance to be | |
4820 | * deemed soft rt before these requests are | |
4821 | * completed (see the comments to the | |
4822 | * function bfq_bfqq_softrt_next_start() | |
4823 | * for details on soft rt detection), | |
4824 | * 4) these pending requests experience a high | |
4825 | * latency because the application is not | |
4826 | * weight-raised while they are pending. | |
4827 | */ | |
4828 | if (bfqq->wr_cur_max_time != | |
4829 | bfqd->bfq_wr_rt_max_time) { | |
4830 | bfqq->wr_start_at_switch_to_srt = | |
4831 | bfqq->last_wr_start_finish; | |
4832 | ||
4833 | bfqq->wr_cur_max_time = | |
4834 | bfqd->bfq_wr_rt_max_time; | |
4835 | bfqq->wr_coeff = bfqd->bfq_wr_coeff * | |
4836 | BFQ_SOFTRT_WEIGHT_FACTOR; | |
4837 | } | |
4838 | bfqq->last_wr_start_finish = jiffies; | |
4839 | } | |
44e44a1b PV |
4840 | } |
4841 | } | |
4842 | ||
4843 | static bool bfq_bfqq_idle_for_long_time(struct bfq_data *bfqd, | |
4844 | struct bfq_queue *bfqq) | |
4845 | { | |
4846 | return bfqq->dispatched == 0 && | |
4847 | time_is_before_jiffies( | |
4848 | bfqq->budget_timeout + | |
4849 | bfqd->bfq_wr_min_idle_time); | |
aee69d78 PV |
4850 | } |
4851 | ||
4852 | static void bfq_bfqq_handle_idle_busy_switch(struct bfq_data *bfqd, | |
4853 | struct bfq_queue *bfqq, | |
44e44a1b PV |
4854 | int old_wr_coeff, |
4855 | struct request *rq, | |
4856 | bool *interactive) | |
aee69d78 | 4857 | { |
e1b2324d AA |
4858 | bool soft_rt, in_burst, wr_or_deserves_wr, |
4859 | bfqq_wants_to_preempt, | |
44e44a1b | 4860 | idle_for_long_time = bfq_bfqq_idle_for_long_time(bfqd, bfqq), |
aee69d78 PV |
4861 | /* |
4862 | * See the comments on | |
4863 | * bfq_bfqq_update_budg_for_activation for | |
4864 | * details on the usage of the next variable. | |
4865 | */ | |
4866 | arrived_in_time = ktime_get_ns() <= | |
4867 | bfqq->ttime.last_end_request + | |
4868 | bfqd->bfq_slice_idle * 3; | |
4869 | ||
e21b7a0b AA |
4870 | bfqg_stats_update_io_add(bfqq_group(RQ_BFQQ(rq)), bfqq, rq->cmd_flags); |
4871 | ||
aee69d78 | 4872 | /* |
44e44a1b PV |
4873 | * bfqq deserves to be weight-raised if: |
4874 | * - it is sync, | |
e1b2324d | 4875 | * - it does not belong to a large burst, |
36eca894 AA |
4876 | * - it has been idle for enough time or is soft real-time, |
4877 | * - is linked to a bfq_io_cq (it is not shared in any sense). | |
44e44a1b | 4878 | */ |
e1b2324d | 4879 | in_burst = bfq_bfqq_in_large_burst(bfqq); |
77b7dcea | 4880 | soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 && |
e1b2324d | 4881 | !in_burst && |
77b7dcea | 4882 | time_is_before_jiffies(bfqq->soft_rt_next_start); |
e1b2324d | 4883 | *interactive = !in_burst && idle_for_long_time; |
44e44a1b PV |
4884 | wr_or_deserves_wr = bfqd->low_latency && |
4885 | (bfqq->wr_coeff > 1 || | |
36eca894 AA |
4886 | (bfq_bfqq_sync(bfqq) && |
4887 | bfqq->bic && (*interactive || soft_rt))); | |
44e44a1b PV |
4888 | |
4889 | /* | |
4890 | * Using the last flag, update budget and check whether bfqq | |
4891 | * may want to preempt the in-service queue. | |
aee69d78 PV |
4892 | */ |
4893 | bfqq_wants_to_preempt = | |
4894 | bfq_bfqq_update_budg_for_activation(bfqd, bfqq, | |
44e44a1b PV |
4895 | arrived_in_time, |
4896 | wr_or_deserves_wr); | |
aee69d78 | 4897 | |
e1b2324d AA |
4898 | /* |
4899 | * If bfqq happened to be activated in a burst, but has been | |
4900 | * idle for much more than an interactive queue, then we | |
4901 | * assume that, in the overall I/O initiated in the burst, the | |
4902 | * I/O associated with bfqq is finished. So bfqq does not need | |
4903 | * to be treated as a queue belonging to a burst | |
4904 | * anymore. Accordingly, we reset bfqq's in_large_burst flag | |
4905 | * if set, and remove bfqq from the burst list if it's | |
4906 | * there. We do not decrement burst_size, because the fact | |
4907 | * that bfqq does not need to belong to the burst list any | |
4908 | * more does not invalidate the fact that bfqq was created in | |
4909 | * a burst. | |
4910 | */ | |
4911 | if (likely(!bfq_bfqq_just_created(bfqq)) && | |
4912 | idle_for_long_time && | |
4913 | time_is_before_jiffies( | |
4914 | bfqq->budget_timeout + | |
4915 | msecs_to_jiffies(10000))) { | |
4916 | hlist_del_init(&bfqq->burst_list_node); | |
4917 | bfq_clear_bfqq_in_large_burst(bfqq); | |
4918 | } | |
4919 | ||
4920 | bfq_clear_bfqq_just_created(bfqq); | |
4921 | ||
4922 | ||
aee69d78 PV |
4923 | if (!bfq_bfqq_IO_bound(bfqq)) { |
4924 | if (arrived_in_time) { | |
4925 | bfqq->requests_within_timer++; | |
4926 | if (bfqq->requests_within_timer >= | |
4927 | bfqd->bfq_requests_within_timer) | |
4928 | bfq_mark_bfqq_IO_bound(bfqq); | |
4929 | } else | |
4930 | bfqq->requests_within_timer = 0; | |
4931 | } | |
4932 | ||
44e44a1b | 4933 | if (bfqd->low_latency) { |
36eca894 AA |
4934 | if (unlikely(time_is_after_jiffies(bfqq->split_time))) |
4935 | /* wraparound */ | |
4936 | bfqq->split_time = | |
4937 | jiffies - bfqd->bfq_wr_min_idle_time - 1; | |
4938 | ||
4939 | if (time_is_before_jiffies(bfqq->split_time + | |
4940 | bfqd->bfq_wr_min_idle_time)) { | |
4941 | bfq_update_bfqq_wr_on_rq_arrival(bfqd, bfqq, | |
4942 | old_wr_coeff, | |
4943 | wr_or_deserves_wr, | |
4944 | *interactive, | |
e1b2324d | 4945 | in_burst, |
36eca894 AA |
4946 | soft_rt); |
4947 | ||
4948 | if (old_wr_coeff != bfqq->wr_coeff) | |
4949 | bfqq->entity.prio_changed = 1; | |
4950 | } | |
44e44a1b PV |
4951 | } |
4952 | ||
77b7dcea PV |
4953 | bfqq->last_idle_bklogged = jiffies; |
4954 | bfqq->service_from_backlogged = 0; | |
4955 | bfq_clear_bfqq_softrt_update(bfqq); | |
4956 | ||
aee69d78 PV |
4957 | bfq_add_bfqq_busy(bfqd, bfqq); |
4958 | ||
4959 | /* | |
4960 | * Expire in-service queue only if preemption may be needed | |
4961 | * for guarantees. In this respect, the function | |
4962 | * next_queue_may_preempt just checks a simple, necessary | |
4963 | * condition, and not a sufficient condition based on | |
4964 | * timestamps. In fact, for the latter condition to be | |
4965 | * evaluated, timestamps would need first to be updated, and | |
4966 | * this operation is quite costly (see the comments on the | |
4967 | * function bfq_bfqq_update_budg_for_activation). | |
4968 | */ | |
4969 | if (bfqd->in_service_queue && bfqq_wants_to_preempt && | |
77b7dcea | 4970 | bfqd->in_service_queue->wr_coeff < bfqq->wr_coeff && |
aee69d78 PV |
4971 | next_queue_may_preempt(bfqd)) |
4972 | bfq_bfqq_expire(bfqd, bfqd->in_service_queue, | |
4973 | false, BFQQE_PREEMPTED); | |
4974 | } | |
4975 | ||
4976 | static void bfq_add_request(struct request *rq) | |
4977 | { | |
4978 | struct bfq_queue *bfqq = RQ_BFQQ(rq); | |
4979 | struct bfq_data *bfqd = bfqq->bfqd; | |
4980 | struct request *next_rq, *prev; | |
44e44a1b PV |
4981 | unsigned int old_wr_coeff = bfqq->wr_coeff; |
4982 | bool interactive = false; | |
aee69d78 PV |
4983 | |
4984 | bfq_log_bfqq(bfqd, bfqq, "add_request %d", rq_is_sync(rq)); | |
4985 | bfqq->queued[rq_is_sync(rq)]++; | |
4986 | bfqd->queued++; | |
4987 | ||
4988 | elv_rb_add(&bfqq->sort_list, rq); | |
4989 | ||
4990 | /* | |
4991 | * Check if this request is a better next-serve candidate. | |
4992 | */ | |
4993 | prev = bfqq->next_rq; | |
4994 | next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position); | |
4995 | bfqq->next_rq = next_rq; | |
4996 | ||
36eca894 AA |
4997 | /* |
4998 | * Adjust priority tree position, if next_rq changes. | |
4999 | */ | |
5000 | if (prev != bfqq->next_rq) | |
5001 | bfq_pos_tree_add_move(bfqd, bfqq); | |
5002 | ||
aee69d78 | 5003 | if (!bfq_bfqq_busy(bfqq)) /* switching to busy ... */ |
44e44a1b PV |
5004 | bfq_bfqq_handle_idle_busy_switch(bfqd, bfqq, old_wr_coeff, |
5005 | rq, &interactive); | |
5006 | else { | |
5007 | if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) && | |
5008 | time_is_before_jiffies( | |
5009 | bfqq->last_wr_start_finish + | |
5010 | bfqd->bfq_wr_min_inter_arr_async)) { | |
5011 | bfqq->wr_coeff = bfqd->bfq_wr_coeff; | |
5012 | bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); | |
5013 | ||
cfd69712 | 5014 | bfqd->wr_busy_queues++; |
44e44a1b PV |
5015 | bfqq->entity.prio_changed = 1; |
5016 | } | |
5017 | if (prev != bfqq->next_rq) | |
5018 | bfq_updated_next_req(bfqd, bfqq); | |
5019 | } | |
5020 | ||
5021 | /* | |
5022 | * Assign jiffies to last_wr_start_finish in the following | |
5023 | * cases: | |
5024 | * | |
5025 | * . if bfqq is not going to be weight-raised, because, for | |
5026 | * non weight-raised queues, last_wr_start_finish stores the | |
5027 | * arrival time of the last request; as of now, this piece | |
5028 | * of information is used only for deciding whether to | |
5029 | * weight-raise async queues | |
5030 | * | |
5031 | * . if bfqq is not weight-raised, because, if bfqq is now | |
5032 | * switching to weight-raised, then last_wr_start_finish | |
5033 | * stores the time when weight-raising starts | |
5034 | * | |
5035 | * . if bfqq is interactive, because, regardless of whether | |
5036 | * bfqq is currently weight-raised, the weight-raising | |
5037 | * period must start or restart (this case is considered | |
5038 | * separately because it is not detected by the above | |
5039 | * conditions, if bfqq is already weight-raised) | |
77b7dcea PV |
5040 | * |
5041 | * last_wr_start_finish has to be updated also if bfqq is soft | |
5042 | * real-time, because the weight-raising period is constantly | |
5043 | * restarted on idle-to-busy transitions for these queues, but | |
5044 | * this is already done in bfq_bfqq_handle_idle_busy_switch if | |
5045 | * needed. | |
44e44a1b PV |
5046 | */ |
5047 | if (bfqd->low_latency && | |
5048 | (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive)) | |
5049 | bfqq->last_wr_start_finish = jiffies; | |
aee69d78 PV |
5050 | } |
5051 | ||
5052 | static struct request *bfq_find_rq_fmerge(struct bfq_data *bfqd, | |
5053 | struct bio *bio, | |
5054 | struct request_queue *q) | |
5055 | { | |
5056 | struct bfq_queue *bfqq = bfqd->bio_bfqq; | |
5057 | ||
5058 | ||
5059 | if (bfqq) | |
5060 | return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio)); | |
5061 | ||
5062 | return NULL; | |
5063 | } | |
5064 | ||
ab0e43e9 PV |
5065 | static sector_t get_sdist(sector_t last_pos, struct request *rq) |
5066 | { | |
5067 | if (last_pos) | |
5068 | return abs(blk_rq_pos(rq) - last_pos); | |
5069 | ||
5070 | return 0; | |
5071 | } | |
5072 | ||
aee69d78 PV |
5073 | #if 0 /* Still not clear if we can do without next two functions */ |
5074 | static void bfq_activate_request(struct request_queue *q, struct request *rq) | |
5075 | { | |
5076 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
5077 | ||
5078 | bfqd->rq_in_driver++; | |
aee69d78 PV |
5079 | } |
5080 | ||
5081 | static void bfq_deactivate_request(struct request_queue *q, struct request *rq) | |
5082 | { | |
5083 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
5084 | ||
5085 | bfqd->rq_in_driver--; | |
5086 | } | |
5087 | #endif | |
5088 | ||
5089 | static void bfq_remove_request(struct request_queue *q, | |
5090 | struct request *rq) | |
5091 | { | |
5092 | struct bfq_queue *bfqq = RQ_BFQQ(rq); | |
5093 | struct bfq_data *bfqd = bfqq->bfqd; | |
5094 | const int sync = rq_is_sync(rq); | |
5095 | ||
5096 | if (bfqq->next_rq == rq) { | |
5097 | bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq); | |
5098 | bfq_updated_next_req(bfqd, bfqq); | |
5099 | } | |
5100 | ||
5101 | if (rq->queuelist.prev != &rq->queuelist) | |
5102 | list_del_init(&rq->queuelist); | |
5103 | bfqq->queued[sync]--; | |
5104 | bfqd->queued--; | |
5105 | elv_rb_del(&bfqq->sort_list, rq); | |
5106 | ||
5107 | elv_rqhash_del(q, rq); | |
5108 | if (q->last_merge == rq) | |
5109 | q->last_merge = NULL; | |
5110 | ||
5111 | if (RB_EMPTY_ROOT(&bfqq->sort_list)) { | |
5112 | bfqq->next_rq = NULL; | |
5113 | ||
5114 | if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) { | |
e21b7a0b | 5115 | bfq_del_bfqq_busy(bfqd, bfqq, false); |
aee69d78 PV |
5116 | /* |
5117 | * bfqq emptied. In normal operation, when | |
5118 | * bfqq is empty, bfqq->entity.service and | |
5119 | * bfqq->entity.budget must contain, | |
5120 | * respectively, the service received and the | |
5121 | * budget used last time bfqq emptied. These | |
5122 | * facts do not hold in this case, as at least | |
5123 | * this last removal occurred while bfqq is | |
5124 | * not in service. To avoid inconsistencies, | |
5125 | * reset both bfqq->entity.service and | |
5126 | * bfqq->entity.budget, if bfqq has still a | |
5127 | * process that may issue I/O requests to it. | |
5128 | */ | |
5129 | bfqq->entity.budget = bfqq->entity.service = 0; | |
5130 | } | |
36eca894 AA |
5131 | |
5132 | /* | |
5133 | * Remove queue from request-position tree as it is empty. | |
5134 | */ | |
5135 | if (bfqq->pos_root) { | |
5136 | rb_erase(&bfqq->pos_node, bfqq->pos_root); | |
5137 | bfqq->pos_root = NULL; | |
5138 | } | |
aee69d78 PV |
5139 | } |
5140 | ||
5141 | if (rq->cmd_flags & REQ_META) | |
5142 | bfqq->meta_pending--; | |
e21b7a0b AA |
5143 | |
5144 | bfqg_stats_update_io_remove(bfqq_group(bfqq), rq->cmd_flags); | |
aee69d78 PV |
5145 | } |
5146 | ||
5147 | static bool bfq_bio_merge(struct blk_mq_hw_ctx *hctx, struct bio *bio) | |
5148 | { | |
5149 | struct request_queue *q = hctx->queue; | |
5150 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
5151 | struct request *free = NULL; | |
5152 | /* | |
5153 | * bfq_bic_lookup grabs the queue_lock: invoke it now and | |
5154 | * store its return value for later use, to avoid nesting | |
5155 | * queue_lock inside the bfqd->lock. We assume that the bic | |
5156 | * returned by bfq_bic_lookup does not go away before | |
5157 | * bfqd->lock is taken. | |
5158 | */ | |
5159 | struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q); | |
5160 | bool ret; | |
5161 | ||
5162 | spin_lock_irq(&bfqd->lock); | |
5163 | ||
5164 | if (bic) | |
5165 | bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf)); | |
5166 | else | |
5167 | bfqd->bio_bfqq = NULL; | |
5168 | bfqd->bio_bic = bic; | |
5169 | ||
5170 | ret = blk_mq_sched_try_merge(q, bio, &free); | |
5171 | ||
5172 | if (free) | |
5173 | blk_mq_free_request(free); | |
5174 | spin_unlock_irq(&bfqd->lock); | |
5175 | ||
5176 | return ret; | |
5177 | } | |
5178 | ||
5179 | static int bfq_request_merge(struct request_queue *q, struct request **req, | |
5180 | struct bio *bio) | |
5181 | { | |
5182 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
5183 | struct request *__rq; | |
5184 | ||
5185 | __rq = bfq_find_rq_fmerge(bfqd, bio, q); | |
5186 | if (__rq && elv_bio_merge_ok(__rq, bio)) { | |
5187 | *req = __rq; | |
5188 | return ELEVATOR_FRONT_MERGE; | |
5189 | } | |
5190 | ||
5191 | return ELEVATOR_NO_MERGE; | |
5192 | } | |
5193 | ||
5194 | static void bfq_request_merged(struct request_queue *q, struct request *req, | |
5195 | enum elv_merge type) | |
5196 | { | |
5197 | if (type == ELEVATOR_FRONT_MERGE && | |
5198 | rb_prev(&req->rb_node) && | |
5199 | blk_rq_pos(req) < | |
5200 | blk_rq_pos(container_of(rb_prev(&req->rb_node), | |
5201 | struct request, rb_node))) { | |
5202 | struct bfq_queue *bfqq = RQ_BFQQ(req); | |
5203 | struct bfq_data *bfqd = bfqq->bfqd; | |
5204 | struct request *prev, *next_rq; | |
5205 | ||
5206 | /* Reposition request in its sort_list */ | |
5207 | elv_rb_del(&bfqq->sort_list, req); | |
5208 | elv_rb_add(&bfqq->sort_list, req); | |
5209 | ||
5210 | /* Choose next request to be served for bfqq */ | |
5211 | prev = bfqq->next_rq; | |
5212 | next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req, | |
5213 | bfqd->last_position); | |
5214 | bfqq->next_rq = next_rq; | |
5215 | /* | |
36eca894 AA |
5216 | * If next_rq changes, update both the queue's budget to |
5217 | * fit the new request and the queue's position in its | |
5218 | * rq_pos_tree. | |
aee69d78 | 5219 | */ |
36eca894 | 5220 | if (prev != bfqq->next_rq) { |
aee69d78 | 5221 | bfq_updated_next_req(bfqd, bfqq); |
36eca894 AA |
5222 | bfq_pos_tree_add_move(bfqd, bfqq); |
5223 | } | |
aee69d78 PV |
5224 | } |
5225 | } | |
5226 | ||
5227 | static void bfq_requests_merged(struct request_queue *q, struct request *rq, | |
5228 | struct request *next) | |
5229 | { | |
5230 | struct bfq_queue *bfqq = RQ_BFQQ(rq), *next_bfqq = RQ_BFQQ(next); | |
5231 | ||
5232 | if (!RB_EMPTY_NODE(&rq->rb_node)) | |
e21b7a0b | 5233 | goto end; |
aee69d78 PV |
5234 | spin_lock_irq(&bfqq->bfqd->lock); |
5235 | ||
5236 | /* | |
5237 | * If next and rq belong to the same bfq_queue and next is older | |
5238 | * than rq, then reposition rq in the fifo (by substituting next | |
5239 | * with rq). Otherwise, if next and rq belong to different | |
5240 | * bfq_queues, never reposition rq: in fact, we would have to | |
5241 | * reposition it with respect to next's position in its own fifo, | |
5242 | * which would most certainly be too expensive with respect to | |
5243 | * the benefits. | |
5244 | */ | |
5245 | if (bfqq == next_bfqq && | |
5246 | !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && | |
5247 | next->fifo_time < rq->fifo_time) { | |
5248 | list_del_init(&rq->queuelist); | |
5249 | list_replace_init(&next->queuelist, &rq->queuelist); | |
5250 | rq->fifo_time = next->fifo_time; | |
5251 | } | |
5252 | ||
5253 | if (bfqq->next_rq == next) | |
5254 | bfqq->next_rq = rq; | |
5255 | ||
5256 | bfq_remove_request(q, next); | |
5257 | ||
5258 | spin_unlock_irq(&bfqq->bfqd->lock); | |
e21b7a0b AA |
5259 | end: |
5260 | bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags); | |
aee69d78 PV |
5261 | } |
5262 | ||
44e44a1b PV |
5263 | /* Must be called with bfqq != NULL */ |
5264 | static void bfq_bfqq_end_wr(struct bfq_queue *bfqq) | |
5265 | { | |
cfd69712 PV |
5266 | if (bfq_bfqq_busy(bfqq)) |
5267 | bfqq->bfqd->wr_busy_queues--; | |
44e44a1b PV |
5268 | bfqq->wr_coeff = 1; |
5269 | bfqq->wr_cur_max_time = 0; | |
77b7dcea | 5270 | bfqq->last_wr_start_finish = jiffies; |
44e44a1b PV |
5271 | /* |
5272 | * Trigger a weight change on the next invocation of | |
5273 | * __bfq_entity_update_weight_prio. | |
5274 | */ | |
5275 | bfqq->entity.prio_changed = 1; | |
5276 | } | |
5277 | ||
5278 | static void bfq_end_wr_async_queues(struct bfq_data *bfqd, | |
5279 | struct bfq_group *bfqg) | |
5280 | { | |
5281 | int i, j; | |
5282 | ||
5283 | for (i = 0; i < 2; i++) | |
5284 | for (j = 0; j < IOPRIO_BE_NR; j++) | |
5285 | if (bfqg->async_bfqq[i][j]) | |
5286 | bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]); | |
5287 | if (bfqg->async_idle_bfqq) | |
5288 | bfq_bfqq_end_wr(bfqg->async_idle_bfqq); | |
5289 | } | |
5290 | ||
5291 | static void bfq_end_wr(struct bfq_data *bfqd) | |
5292 | { | |
5293 | struct bfq_queue *bfqq; | |
5294 | ||
5295 | spin_lock_irq(&bfqd->lock); | |
5296 | ||
5297 | list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) | |
5298 | bfq_bfqq_end_wr(bfqq); | |
5299 | list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list) | |
5300 | bfq_bfqq_end_wr(bfqq); | |
5301 | bfq_end_wr_async(bfqd); | |
5302 | ||
5303 | spin_unlock_irq(&bfqd->lock); | |
5304 | } | |
5305 | ||
36eca894 AA |
5306 | static sector_t bfq_io_struct_pos(void *io_struct, bool request) |
5307 | { | |
5308 | if (request) | |
5309 | return blk_rq_pos(io_struct); | |
5310 | else | |
5311 | return ((struct bio *)io_struct)->bi_iter.bi_sector; | |
5312 | } | |
5313 | ||
5314 | static int bfq_rq_close_to_sector(void *io_struct, bool request, | |
5315 | sector_t sector) | |
5316 | { | |
5317 | return abs(bfq_io_struct_pos(io_struct, request) - sector) <= | |
5318 | BFQQ_CLOSE_THR; | |
5319 | } | |
5320 | ||
5321 | static struct bfq_queue *bfqq_find_close(struct bfq_data *bfqd, | |
5322 | struct bfq_queue *bfqq, | |
5323 | sector_t sector) | |
5324 | { | |
5325 | struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree; | |
5326 | struct rb_node *parent, *node; | |
5327 | struct bfq_queue *__bfqq; | |
5328 | ||
5329 | if (RB_EMPTY_ROOT(root)) | |
5330 | return NULL; | |
5331 | ||
5332 | /* | |
5333 | * First, if we find a request starting at the end of the last | |
5334 | * request, choose it. | |
5335 | */ | |
5336 | __bfqq = bfq_rq_pos_tree_lookup(bfqd, root, sector, &parent, NULL); | |
5337 | if (__bfqq) | |
5338 | return __bfqq; | |
5339 | ||
5340 | /* | |
5341 | * If the exact sector wasn't found, the parent of the NULL leaf | |
5342 | * will contain the closest sector (rq_pos_tree sorted by | |
5343 | * next_request position). | |
5344 | */ | |
5345 | __bfqq = rb_entry(parent, struct bfq_queue, pos_node); | |
5346 | if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) | |
5347 | return __bfqq; | |
5348 | ||
5349 | if (blk_rq_pos(__bfqq->next_rq) < sector) | |
5350 | node = rb_next(&__bfqq->pos_node); | |
5351 | else | |
5352 | node = rb_prev(&__bfqq->pos_node); | |
5353 | if (!node) | |
5354 | return NULL; | |
5355 | ||
5356 | __bfqq = rb_entry(node, struct bfq_queue, pos_node); | |
5357 | if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) | |
5358 | return __bfqq; | |
5359 | ||
5360 | return NULL; | |
5361 | } | |
5362 | ||
5363 | static struct bfq_queue *bfq_find_close_cooperator(struct bfq_data *bfqd, | |
5364 | struct bfq_queue *cur_bfqq, | |
5365 | sector_t sector) | |
5366 | { | |
5367 | struct bfq_queue *bfqq; | |
5368 | ||
5369 | /* | |
5370 | * We shall notice if some of the queues are cooperating, | |
5371 | * e.g., working closely on the same area of the device. In | |
5372 | * that case, we can group them together and: 1) don't waste | |
5373 | * time idling, and 2) serve the union of their requests in | |
5374 | * the best possible order for throughput. | |
5375 | */ | |
5376 | bfqq = bfqq_find_close(bfqd, cur_bfqq, sector); | |
5377 | if (!bfqq || bfqq == cur_bfqq) | |
5378 | return NULL; | |
5379 | ||
5380 | return bfqq; | |
5381 | } | |
5382 | ||
5383 | static struct bfq_queue * | |
5384 | bfq_setup_merge(struct bfq_queue *bfqq, struct bfq_queue *new_bfqq) | |
5385 | { | |
5386 | int process_refs, new_process_refs; | |
5387 | struct bfq_queue *__bfqq; | |
5388 | ||
5389 | /* | |
5390 | * If there are no process references on the new_bfqq, then it is | |
5391 | * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain | |
5392 | * may have dropped their last reference (not just their last process | |
5393 | * reference). | |
5394 | */ | |
5395 | if (!bfqq_process_refs(new_bfqq)) | |
5396 | return NULL; | |
5397 | ||
5398 | /* Avoid a circular list and skip interim queue merges. */ | |
5399 | while ((__bfqq = new_bfqq->new_bfqq)) { | |
5400 | if (__bfqq == bfqq) | |
5401 | return NULL; | |
5402 | new_bfqq = __bfqq; | |
5403 | } | |
5404 | ||
5405 | process_refs = bfqq_process_refs(bfqq); | |
5406 | new_process_refs = bfqq_process_refs(new_bfqq); | |
5407 | /* | |
5408 | * If the process for the bfqq has gone away, there is no | |
5409 | * sense in merging the queues. | |
5410 | */ | |
5411 | if (process_refs == 0 || new_process_refs == 0) | |
5412 | return NULL; | |
5413 | ||
5414 | bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d", | |
5415 | new_bfqq->pid); | |
5416 | ||
5417 | /* | |
5418 | * Merging is just a redirection: the requests of the process | |
5419 | * owning one of the two queues are redirected to the other queue. | |
5420 | * The latter queue, in its turn, is set as shared if this is the | |
5421 | * first time that the requests of some process are redirected to | |
5422 | * it. | |
5423 | * | |
6fa3e8d3 PV |
5424 | * We redirect bfqq to new_bfqq and not the opposite, because |
5425 | * we are in the context of the process owning bfqq, thus we | |
5426 | * have the io_cq of this process. So we can immediately | |
5427 | * configure this io_cq to redirect the requests of the | |
5428 | * process to new_bfqq. In contrast, the io_cq of new_bfqq is | |
5429 | * not available any more (new_bfqq->bic == NULL). | |
36eca894 | 5430 | * |
6fa3e8d3 PV |
5431 | * Anyway, even in case new_bfqq coincides with the in-service |
5432 | * queue, redirecting requests the in-service queue is the | |
5433 | * best option, as we feed the in-service queue with new | |
5434 | * requests close to the last request served and, by doing so, | |
5435 | * are likely to increase the throughput. | |
36eca894 AA |
5436 | */ |
5437 | bfqq->new_bfqq = new_bfqq; | |
5438 | new_bfqq->ref += process_refs; | |
5439 | return new_bfqq; | |
5440 | } | |
5441 | ||
5442 | static bool bfq_may_be_close_cooperator(struct bfq_queue *bfqq, | |
5443 | struct bfq_queue *new_bfqq) | |
5444 | { | |
5445 | if (bfq_class_idle(bfqq) || bfq_class_idle(new_bfqq) || | |
5446 | (bfqq->ioprio_class != new_bfqq->ioprio_class)) | |
5447 | return false; | |
5448 | ||
5449 | /* | |
5450 | * If either of the queues has already been detected as seeky, | |
5451 | * then merging it with the other queue is unlikely to lead to | |
5452 | * sequential I/O. | |
5453 | */ | |
5454 | if (BFQQ_SEEKY(bfqq) || BFQQ_SEEKY(new_bfqq)) | |
5455 | return false; | |
5456 | ||
5457 | /* | |
5458 | * Interleaved I/O is known to be done by (some) applications | |
5459 | * only for reads, so it does not make sense to merge async | |
5460 | * queues. | |
5461 | */ | |
5462 | if (!bfq_bfqq_sync(bfqq) || !bfq_bfqq_sync(new_bfqq)) | |
5463 | return false; | |
5464 | ||
5465 | return true; | |
5466 | } | |
5467 | ||
5468 | /* | |
5469 | * If this function returns true, then bfqq cannot be merged. The idea | |
5470 | * is that true cooperation happens very early after processes start | |
5471 | * to do I/O. Usually, late cooperations are just accidental false | |
5472 | * positives. In case bfqq is weight-raised, such false positives | |
5473 | * would evidently degrade latency guarantees for bfqq. | |
5474 | */ | |
5475 | static bool wr_from_too_long(struct bfq_queue *bfqq) | |
5476 | { | |
5477 | return bfqq->wr_coeff > 1 && | |
5478 | time_is_before_jiffies(bfqq->last_wr_start_finish + | |
5479 | msecs_to_jiffies(100)); | |
5480 | } | |
5481 | ||
5482 | /* | |
5483 | * Attempt to schedule a merge of bfqq with the currently in-service | |
5484 | * queue or with a close queue among the scheduled queues. Return | |
5485 | * NULL if no merge was scheduled, a pointer to the shared bfq_queue | |
5486 | * structure otherwise. | |
5487 | * | |
5488 | * The OOM queue is not allowed to participate to cooperation: in fact, since | |
5489 | * the requests temporarily redirected to the OOM queue could be redirected | |
5490 | * again to dedicated queues at any time, the state needed to correctly | |
5491 | * handle merging with the OOM queue would be quite complex and expensive | |
5492 | * to maintain. Besides, in such a critical condition as an out of memory, | |
5493 | * the benefits of queue merging may be little relevant, or even negligible. | |
5494 | * | |
5495 | * Weight-raised queues can be merged only if their weight-raising | |
5496 | * period has just started. In fact cooperating processes are usually | |
5497 | * started together. Thus, with this filter we avoid false positives | |
5498 | * that would jeopardize low-latency guarantees. | |
5499 | * | |
5500 | * WARNING: queue merging may impair fairness among non-weight raised | |
5501 | * queues, for at least two reasons: 1) the original weight of a | |
5502 | * merged queue may change during the merged state, 2) even being the | |
5503 | * weight the same, a merged queue may be bloated with many more | |
5504 | * requests than the ones produced by its originally-associated | |
5505 | * process. | |
5506 | */ | |
5507 | static struct bfq_queue * | |
5508 | bfq_setup_cooperator(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
5509 | void *io_struct, bool request) | |
5510 | { | |
5511 | struct bfq_queue *in_service_bfqq, *new_bfqq; | |
5512 | ||
5513 | if (bfqq->new_bfqq) | |
5514 | return bfqq->new_bfqq; | |
5515 | ||
5516 | if (!io_struct || | |
5517 | wr_from_too_long(bfqq) || | |
5518 | unlikely(bfqq == &bfqd->oom_bfqq)) | |
5519 | return NULL; | |
5520 | ||
5521 | /* If there is only one backlogged queue, don't search. */ | |
5522 | if (bfqd->busy_queues == 1) | |
5523 | return NULL; | |
5524 | ||
5525 | in_service_bfqq = bfqd->in_service_queue; | |
5526 | ||
6fa3e8d3 PV |
5527 | if (!in_service_bfqq || in_service_bfqq == bfqq |
5528 | || wr_from_too_long(in_service_bfqq) || | |
36eca894 AA |
5529 | unlikely(in_service_bfqq == &bfqd->oom_bfqq)) |
5530 | goto check_scheduled; | |
5531 | ||
5532 | if (bfq_rq_close_to_sector(io_struct, request, bfqd->last_position) && | |
5533 | bfqq->entity.parent == in_service_bfqq->entity.parent && | |
5534 | bfq_may_be_close_cooperator(bfqq, in_service_bfqq)) { | |
5535 | new_bfqq = bfq_setup_merge(bfqq, in_service_bfqq); | |
5536 | if (new_bfqq) | |
5537 | return new_bfqq; | |
5538 | } | |
5539 | /* | |
5540 | * Check whether there is a cooperator among currently scheduled | |
5541 | * queues. The only thing we need is that the bio/request is not | |
5542 | * NULL, as we need it to establish whether a cooperator exists. | |
5543 | */ | |
5544 | check_scheduled: | |
5545 | new_bfqq = bfq_find_close_cooperator(bfqd, bfqq, | |
5546 | bfq_io_struct_pos(io_struct, request)); | |
5547 | ||
5548 | if (new_bfqq && !wr_from_too_long(new_bfqq) && | |
5549 | likely(new_bfqq != &bfqd->oom_bfqq) && | |
5550 | bfq_may_be_close_cooperator(bfqq, new_bfqq)) | |
5551 | return bfq_setup_merge(bfqq, new_bfqq); | |
5552 | ||
5553 | return NULL; | |
5554 | } | |
5555 | ||
5556 | static void bfq_bfqq_save_state(struct bfq_queue *bfqq) | |
5557 | { | |
5558 | struct bfq_io_cq *bic = bfqq->bic; | |
5559 | ||
5560 | /* | |
5561 | * If !bfqq->bic, the queue is already shared or its requests | |
5562 | * have already been redirected to a shared queue; both idle window | |
5563 | * and weight raising state have already been saved. Do nothing. | |
5564 | */ | |
5565 | if (!bic) | |
5566 | return; | |
5567 | ||
5568 | bic->saved_ttime = bfqq->ttime; | |
5569 | bic->saved_idle_window = bfq_bfqq_idle_window(bfqq); | |
5570 | bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq); | |
e1b2324d AA |
5571 | bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq); |
5572 | bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node); | |
36eca894 AA |
5573 | bic->saved_wr_coeff = bfqq->wr_coeff; |
5574 | bic->saved_wr_start_at_switch_to_srt = bfqq->wr_start_at_switch_to_srt; | |
5575 | bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish; | |
5576 | bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time; | |
5577 | } | |
5578 | ||
36eca894 AA |
5579 | static void |
5580 | bfq_merge_bfqqs(struct bfq_data *bfqd, struct bfq_io_cq *bic, | |
5581 | struct bfq_queue *bfqq, struct bfq_queue *new_bfqq) | |
5582 | { | |
5583 | bfq_log_bfqq(bfqd, bfqq, "merging with queue %lu", | |
5584 | (unsigned long)new_bfqq->pid); | |
5585 | /* Save weight raising and idle window of the merged queues */ | |
5586 | bfq_bfqq_save_state(bfqq); | |
5587 | bfq_bfqq_save_state(new_bfqq); | |
5588 | if (bfq_bfqq_IO_bound(bfqq)) | |
5589 | bfq_mark_bfqq_IO_bound(new_bfqq); | |
5590 | bfq_clear_bfqq_IO_bound(bfqq); | |
5591 | ||
5592 | /* | |
5593 | * If bfqq is weight-raised, then let new_bfqq inherit | |
5594 | * weight-raising. To reduce false positives, neglect the case | |
5595 | * where bfqq has just been created, but has not yet made it | |
5596 | * to be weight-raised (which may happen because EQM may merge | |
5597 | * bfqq even before bfq_add_request is executed for the first | |
e1b2324d AA |
5598 | * time for bfqq). Handling this case would however be very |
5599 | * easy, thanks to the flag just_created. | |
36eca894 AA |
5600 | */ |
5601 | if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) { | |
5602 | new_bfqq->wr_coeff = bfqq->wr_coeff; | |
5603 | new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time; | |
5604 | new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish; | |
5605 | new_bfqq->wr_start_at_switch_to_srt = | |
5606 | bfqq->wr_start_at_switch_to_srt; | |
5607 | if (bfq_bfqq_busy(new_bfqq)) | |
5608 | bfqd->wr_busy_queues++; | |
5609 | new_bfqq->entity.prio_changed = 1; | |
5610 | } | |
5611 | ||
5612 | if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */ | |
5613 | bfqq->wr_coeff = 1; | |
5614 | bfqq->entity.prio_changed = 1; | |
5615 | if (bfq_bfqq_busy(bfqq)) | |
5616 | bfqd->wr_busy_queues--; | |
5617 | } | |
5618 | ||
5619 | bfq_log_bfqq(bfqd, new_bfqq, "merge_bfqqs: wr_busy %d", | |
5620 | bfqd->wr_busy_queues); | |
5621 | ||
36eca894 AA |
5622 | /* |
5623 | * Merge queues (that is, let bic redirect its requests to new_bfqq) | |
5624 | */ | |
5625 | bic_set_bfqq(bic, new_bfqq, 1); | |
5626 | bfq_mark_bfqq_coop(new_bfqq); | |
5627 | /* | |
5628 | * new_bfqq now belongs to at least two bics (it is a shared queue): | |
5629 | * set new_bfqq->bic to NULL. bfqq either: | |
5630 | * - does not belong to any bic any more, and hence bfqq->bic must | |
5631 | * be set to NULL, or | |
5632 | * - is a queue whose owning bics have already been redirected to a | |
5633 | * different queue, hence the queue is destined to not belong to | |
5634 | * any bic soon and bfqq->bic is already NULL (therefore the next | |
5635 | * assignment causes no harm). | |
5636 | */ | |
5637 | new_bfqq->bic = NULL; | |
5638 | bfqq->bic = NULL; | |
5639 | /* release process reference to bfqq */ | |
5640 | bfq_put_queue(bfqq); | |
5641 | } | |
5642 | ||
aee69d78 PV |
5643 | static bool bfq_allow_bio_merge(struct request_queue *q, struct request *rq, |
5644 | struct bio *bio) | |
5645 | { | |
5646 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
5647 | bool is_sync = op_is_sync(bio->bi_opf); | |
36eca894 | 5648 | struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq; |
aee69d78 PV |
5649 | |
5650 | /* | |
5651 | * Disallow merge of a sync bio into an async request. | |
5652 | */ | |
5653 | if (is_sync && !rq_is_sync(rq)) | |
5654 | return false; | |
5655 | ||
5656 | /* | |
5657 | * Lookup the bfqq that this bio will be queued with. Allow | |
5658 | * merge only if rq is queued there. | |
5659 | */ | |
5660 | if (!bfqq) | |
5661 | return false; | |
5662 | ||
36eca894 AA |
5663 | /* |
5664 | * We take advantage of this function to perform an early merge | |
5665 | * of the queues of possible cooperating processes. | |
5666 | */ | |
5667 | new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false); | |
5668 | if (new_bfqq) { | |
5669 | /* | |
5670 | * bic still points to bfqq, then it has not yet been | |
5671 | * redirected to some other bfq_queue, and a queue | |
5672 | * merge beween bfqq and new_bfqq can be safely | |
5673 | * fulfillled, i.e., bic can be redirected to new_bfqq | |
5674 | * and bfqq can be put. | |
5675 | */ | |
5676 | bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq, | |
5677 | new_bfqq); | |
5678 | /* | |
5679 | * If we get here, bio will be queued into new_queue, | |
5680 | * so use new_bfqq to decide whether bio and rq can be | |
5681 | * merged. | |
5682 | */ | |
5683 | bfqq = new_bfqq; | |
5684 | ||
5685 | /* | |
5686 | * Change also bqfd->bio_bfqq, as | |
5687 | * bfqd->bio_bic now points to new_bfqq, and | |
5688 | * this function may be invoked again (and then may | |
5689 | * use again bqfd->bio_bfqq). | |
5690 | */ | |
5691 | bfqd->bio_bfqq = bfqq; | |
5692 | } | |
5693 | ||
aee69d78 PV |
5694 | return bfqq == RQ_BFQQ(rq); |
5695 | } | |
5696 | ||
44e44a1b PV |
5697 | /* |
5698 | * Set the maximum time for the in-service queue to consume its | |
5699 | * budget. This prevents seeky processes from lowering the throughput. | |
5700 | * In practice, a time-slice service scheme is used with seeky | |
5701 | * processes. | |
5702 | */ | |
5703 | static void bfq_set_budget_timeout(struct bfq_data *bfqd, | |
5704 | struct bfq_queue *bfqq) | |
5705 | { | |
77b7dcea PV |
5706 | unsigned int timeout_coeff; |
5707 | ||
5708 | if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time) | |
5709 | timeout_coeff = 1; | |
5710 | else | |
5711 | timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight; | |
5712 | ||
44e44a1b PV |
5713 | bfqd->last_budget_start = ktime_get(); |
5714 | ||
5715 | bfqq->budget_timeout = jiffies + | |
77b7dcea | 5716 | bfqd->bfq_timeout * timeout_coeff; |
44e44a1b PV |
5717 | } |
5718 | ||
aee69d78 PV |
5719 | static void __bfq_set_in_service_queue(struct bfq_data *bfqd, |
5720 | struct bfq_queue *bfqq) | |
5721 | { | |
5722 | if (bfqq) { | |
e21b7a0b | 5723 | bfqg_stats_update_avg_queue_size(bfqq_group(bfqq)); |
aee69d78 PV |
5724 | bfq_clear_bfqq_fifo_expire(bfqq); |
5725 | ||
5726 | bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8; | |
5727 | ||
77b7dcea PV |
5728 | if (time_is_before_jiffies(bfqq->last_wr_start_finish) && |
5729 | bfqq->wr_coeff > 1 && | |
5730 | bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && | |
5731 | time_is_before_jiffies(bfqq->budget_timeout)) { | |
5732 | /* | |
5733 | * For soft real-time queues, move the start | |
5734 | * of the weight-raising period forward by the | |
5735 | * time the queue has not received any | |
5736 | * service. Otherwise, a relatively long | |
5737 | * service delay is likely to cause the | |
5738 | * weight-raising period of the queue to end, | |
5739 | * because of the short duration of the | |
5740 | * weight-raising period of a soft real-time | |
5741 | * queue. It is worth noting that this move | |
5742 | * is not so dangerous for the other queues, | |
5743 | * because soft real-time queues are not | |
5744 | * greedy. | |
5745 | * | |
5746 | * To not add a further variable, we use the | |
5747 | * overloaded field budget_timeout to | |
5748 | * determine for how long the queue has not | |
5749 | * received service, i.e., how much time has | |
5750 | * elapsed since the queue expired. However, | |
5751 | * this is a little imprecise, because | |
5752 | * budget_timeout is set to jiffies if bfqq | |
5753 | * not only expires, but also remains with no | |
5754 | * request. | |
5755 | */ | |
5756 | if (time_after(bfqq->budget_timeout, | |
5757 | bfqq->last_wr_start_finish)) | |
5758 | bfqq->last_wr_start_finish += | |
5759 | jiffies - bfqq->budget_timeout; | |
5760 | else | |
5761 | bfqq->last_wr_start_finish = jiffies; | |
5762 | } | |
5763 | ||
44e44a1b | 5764 | bfq_set_budget_timeout(bfqd, bfqq); |
aee69d78 PV |
5765 | bfq_log_bfqq(bfqd, bfqq, |
5766 | "set_in_service_queue, cur-budget = %d", | |
5767 | bfqq->entity.budget); | |
5768 | } | |
5769 | ||
5770 | bfqd->in_service_queue = bfqq; | |
5771 | } | |
5772 | ||
5773 | /* | |
5774 | * Get and set a new queue for service. | |
5775 | */ | |
5776 | static struct bfq_queue *bfq_set_in_service_queue(struct bfq_data *bfqd) | |
5777 | { | |
5778 | struct bfq_queue *bfqq = bfq_get_next_queue(bfqd); | |
5779 | ||
5780 | __bfq_set_in_service_queue(bfqd, bfqq); | |
5781 | return bfqq; | |
5782 | } | |
5783 | ||
aee69d78 PV |
5784 | static void bfq_arm_slice_timer(struct bfq_data *bfqd) |
5785 | { | |
5786 | struct bfq_queue *bfqq = bfqd->in_service_queue; | |
aee69d78 PV |
5787 | u32 sl; |
5788 | ||
aee69d78 PV |
5789 | bfq_mark_bfqq_wait_request(bfqq); |
5790 | ||
5791 | /* | |
5792 | * We don't want to idle for seeks, but we do want to allow | |
5793 | * fair distribution of slice time for a process doing back-to-back | |
5794 | * seeks. So allow a little bit of time for him to submit a new rq. | |
5795 | */ | |
5796 | sl = bfqd->bfq_slice_idle; | |
5797 | /* | |
1de0c4cd AA |
5798 | * Unless the queue is being weight-raised or the scenario is |
5799 | * asymmetric, grant only minimum idle time if the queue | |
5800 | * is seeky. A long idling is preserved for a weight-raised | |
5801 | * queue, or, more in general, in an asymmetric scenario, | |
5802 | * because a long idling is needed for guaranteeing to a queue | |
5803 | * its reserved share of the throughput (in particular, it is | |
5804 | * needed if the queue has a higher weight than some other | |
5805 | * queue). | |
aee69d78 | 5806 | */ |
1de0c4cd AA |
5807 | if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 && |
5808 | bfq_symmetric_scenario(bfqd)) | |
aee69d78 PV |
5809 | sl = min_t(u64, sl, BFQ_MIN_TT); |
5810 | ||
5811 | bfqd->last_idling_start = ktime_get(); | |
5812 | hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl), | |
5813 | HRTIMER_MODE_REL); | |
e21b7a0b | 5814 | bfqg_stats_set_start_idle_time(bfqq_group(bfqq)); |
aee69d78 PV |
5815 | } |
5816 | ||
ab0e43e9 PV |
5817 | /* |
5818 | * In autotuning mode, max_budget is dynamically recomputed as the | |
5819 | * amount of sectors transferred in timeout at the estimated peak | |
5820 | * rate. This enables BFQ to utilize a full timeslice with a full | |
5821 | * budget, even if the in-service queue is served at peak rate. And | |
5822 | * this maximises throughput with sequential workloads. | |
5823 | */ | |
5824 | static unsigned long bfq_calc_max_budget(struct bfq_data *bfqd) | |
5825 | { | |
5826 | return (u64)bfqd->peak_rate * USEC_PER_MSEC * | |
5827 | jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT; | |
5828 | } | |
5829 | ||
44e44a1b PV |
5830 | /* |
5831 | * Update parameters related to throughput and responsiveness, as a | |
5832 | * function of the estimated peak rate. See comments on | |
5833 | * bfq_calc_max_budget(), and on T_slow and T_fast arrays. | |
5834 | */ | |
5835 | static void update_thr_responsiveness_params(struct bfq_data *bfqd) | |
5836 | { | |
5837 | int dev_type = blk_queue_nonrot(bfqd->queue); | |
5838 | ||
5839 | if (bfqd->bfq_user_max_budget == 0) | |
5840 | bfqd->bfq_max_budget = | |
5841 | bfq_calc_max_budget(bfqd); | |
5842 | ||
5843 | if (bfqd->device_speed == BFQ_BFQD_FAST && | |
5844 | bfqd->peak_rate < device_speed_thresh[dev_type]) { | |
5845 | bfqd->device_speed = BFQ_BFQD_SLOW; | |
5846 | bfqd->RT_prod = R_slow[dev_type] * | |
5847 | T_slow[dev_type]; | |
5848 | } else if (bfqd->device_speed == BFQ_BFQD_SLOW && | |
5849 | bfqd->peak_rate > device_speed_thresh[dev_type]) { | |
5850 | bfqd->device_speed = BFQ_BFQD_FAST; | |
5851 | bfqd->RT_prod = R_fast[dev_type] * | |
5852 | T_fast[dev_type]; | |
5853 | } | |
5854 | ||
5855 | bfq_log(bfqd, | |
5856 | "dev_type %s dev_speed_class = %s (%llu sects/sec), thresh %llu setcs/sec", | |
5857 | dev_type == 0 ? "ROT" : "NONROT", | |
5858 | bfqd->device_speed == BFQ_BFQD_FAST ? "FAST" : "SLOW", | |
5859 | bfqd->device_speed == BFQ_BFQD_FAST ? | |
5860 | (USEC_PER_SEC*(u64)R_fast[dev_type])>>BFQ_RATE_SHIFT : | |
5861 | (USEC_PER_SEC*(u64)R_slow[dev_type])>>BFQ_RATE_SHIFT, | |
5862 | (USEC_PER_SEC*(u64)device_speed_thresh[dev_type])>> | |
5863 | BFQ_RATE_SHIFT); | |
5864 | } | |
5865 | ||
ab0e43e9 PV |
5866 | static void bfq_reset_rate_computation(struct bfq_data *bfqd, |
5867 | struct request *rq) | |
5868 | { | |
5869 | if (rq != NULL) { /* new rq dispatch now, reset accordingly */ | |
5870 | bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns(); | |
5871 | bfqd->peak_rate_samples = 1; | |
5872 | bfqd->sequential_samples = 0; | |
5873 | bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size = | |
5874 | blk_rq_sectors(rq); | |
5875 | } else /* no new rq dispatched, just reset the number of samples */ | |
5876 | bfqd->peak_rate_samples = 0; /* full re-init on next disp. */ | |
5877 | ||
5878 | bfq_log(bfqd, | |
5879 | "reset_rate_computation at end, sample %u/%u tot_sects %llu", | |
5880 | bfqd->peak_rate_samples, bfqd->sequential_samples, | |
5881 | bfqd->tot_sectors_dispatched); | |
5882 | } | |
5883 | ||
5884 | static void bfq_update_rate_reset(struct bfq_data *bfqd, struct request *rq) | |
5885 | { | |
5886 | u32 rate, weight, divisor; | |
5887 | ||
5888 | /* | |
5889 | * For the convergence property to hold (see comments on | |
5890 | * bfq_update_peak_rate()) and for the assessment to be | |
5891 | * reliable, a minimum number of samples must be present, and | |
5892 | * a minimum amount of time must have elapsed. If not so, do | |
5893 | * not compute new rate. Just reset parameters, to get ready | |
5894 | * for a new evaluation attempt. | |
5895 | */ | |
5896 | if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES || | |
5897 | bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL) | |
5898 | goto reset_computation; | |
5899 | ||
5900 | /* | |
5901 | * If a new request completion has occurred after last | |
5902 | * dispatch, then, to approximate the rate at which requests | |
5903 | * have been served by the device, it is more precise to | |
5904 | * extend the observation interval to the last completion. | |
5905 | */ | |
5906 | bfqd->delta_from_first = | |
5907 | max_t(u64, bfqd->delta_from_first, | |
5908 | bfqd->last_completion - bfqd->first_dispatch); | |
5909 | ||
5910 | /* | |
5911 | * Rate computed in sects/usec, and not sects/nsec, for | |
5912 | * precision issues. | |
5913 | */ | |
5914 | rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT, | |
5915 | div_u64(bfqd->delta_from_first, NSEC_PER_USEC)); | |
5916 | ||
5917 | /* | |
5918 | * Peak rate not updated if: | |
5919 | * - the percentage of sequential dispatches is below 3/4 of the | |
5920 | * total, and rate is below the current estimated peak rate | |
5921 | * - rate is unreasonably high (> 20M sectors/sec) | |
5922 | */ | |
5923 | if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 && | |
5924 | rate <= bfqd->peak_rate) || | |
5925 | rate > 20<<BFQ_RATE_SHIFT) | |
5926 | goto reset_computation; | |
5927 | ||
5928 | /* | |
5929 | * We have to update the peak rate, at last! To this purpose, | |
5930 | * we use a low-pass filter. We compute the smoothing constant | |
5931 | * of the filter as a function of the 'weight' of the new | |
5932 | * measured rate. | |
5933 | * | |
5934 | * As can be seen in next formulas, we define this weight as a | |
5935 | * quantity proportional to how sequential the workload is, | |
5936 | * and to how long the observation time interval is. | |
5937 | * | |
5938 | * The weight runs from 0 to 8. The maximum value of the | |
5939 | * weight, 8, yields the minimum value for the smoothing | |
5940 | * constant. At this minimum value for the smoothing constant, | |
5941 | * the measured rate contributes for half of the next value of | |
5942 | * the estimated peak rate. | |
5943 | * | |
5944 | * So, the first step is to compute the weight as a function | |
5945 | * of how sequential the workload is. Note that the weight | |
5946 | * cannot reach 9, because bfqd->sequential_samples cannot | |
5947 | * become equal to bfqd->peak_rate_samples, which, in its | |
5948 | * turn, holds true because bfqd->sequential_samples is not | |
5949 | * incremented for the first sample. | |
5950 | */ | |
5951 | weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples; | |
5952 | ||
5953 | /* | |
5954 | * Second step: further refine the weight as a function of the | |
5955 | * duration of the observation interval. | |
5956 | */ | |
5957 | weight = min_t(u32, 8, | |
5958 | div_u64(weight * bfqd->delta_from_first, | |
5959 | BFQ_RATE_REF_INTERVAL)); | |
5960 | ||
5961 | /* | |
5962 | * Divisor ranging from 10, for minimum weight, to 2, for | |
5963 | * maximum weight. | |
5964 | */ | |
5965 | divisor = 10 - weight; | |
5966 | ||
5967 | /* | |
5968 | * Finally, update peak rate: | |
5969 | * | |
5970 | * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor | |
5971 | */ | |
5972 | bfqd->peak_rate *= divisor-1; | |
5973 | bfqd->peak_rate /= divisor; | |
5974 | rate /= divisor; /* smoothing constant alpha = 1/divisor */ | |
5975 | ||
5976 | bfqd->peak_rate += rate; | |
44e44a1b | 5977 | update_thr_responsiveness_params(bfqd); |
ab0e43e9 PV |
5978 | |
5979 | reset_computation: | |
5980 | bfq_reset_rate_computation(bfqd, rq); | |
5981 | } | |
5982 | ||
5983 | /* | |
5984 | * Update the read/write peak rate (the main quantity used for | |
5985 | * auto-tuning, see update_thr_responsiveness_params()). | |
5986 | * | |
5987 | * It is not trivial to estimate the peak rate (correctly): because of | |
5988 | * the presence of sw and hw queues between the scheduler and the | |
5989 | * device components that finally serve I/O requests, it is hard to | |
5990 | * say exactly when a given dispatched request is served inside the | |
5991 | * device, and for how long. As a consequence, it is hard to know | |
5992 | * precisely at what rate a given set of requests is actually served | |
5993 | * by the device. | |
5994 | * | |
5995 | * On the opposite end, the dispatch time of any request is trivially | |
5996 | * available, and, from this piece of information, the "dispatch rate" | |
5997 | * of requests can be immediately computed. So, the idea in the next | |
5998 | * function is to use what is known, namely request dispatch times | |
5999 | * (plus, when useful, request completion times), to estimate what is | |
6000 | * unknown, namely in-device request service rate. | |
6001 | * | |
6002 | * The main issue is that, because of the above facts, the rate at | |
6003 | * which a certain set of requests is dispatched over a certain time | |
6004 | * interval can vary greatly with respect to the rate at which the | |
6005 | * same requests are then served. But, since the size of any | |
6006 | * intermediate queue is limited, and the service scheme is lossless | |
6007 | * (no request is silently dropped), the following obvious convergence | |
6008 | * property holds: the number of requests dispatched MUST become | |
6009 | * closer and closer to the number of requests completed as the | |
6010 | * observation interval grows. This is the key property used in | |
6011 | * the next function to estimate the peak service rate as a function | |
6012 | * of the observed dispatch rate. The function assumes to be invoked | |
6013 | * on every request dispatch. | |
6014 | */ | |
6015 | static void bfq_update_peak_rate(struct bfq_data *bfqd, struct request *rq) | |
6016 | { | |
6017 | u64 now_ns = ktime_get_ns(); | |
6018 | ||
6019 | if (bfqd->peak_rate_samples == 0) { /* first dispatch */ | |
6020 | bfq_log(bfqd, "update_peak_rate: goto reset, samples %d", | |
6021 | bfqd->peak_rate_samples); | |
6022 | bfq_reset_rate_computation(bfqd, rq); | |
6023 | goto update_last_values; /* will add one sample */ | |
6024 | } | |
6025 | ||
6026 | /* | |
6027 | * Device idle for very long: the observation interval lasting | |
6028 | * up to this dispatch cannot be a valid observation interval | |
6029 | * for computing a new peak rate (similarly to the late- | |
6030 | * completion event in bfq_completed_request()). Go to | |
6031 | * update_rate_and_reset to have the following three steps | |
6032 | * taken: | |
6033 | * - close the observation interval at the last (previous) | |
6034 | * request dispatch or completion | |
6035 | * - compute rate, if possible, for that observation interval | |
6036 | * - start a new observation interval with this dispatch | |
6037 | */ | |
6038 | if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC && | |
6039 | bfqd->rq_in_driver == 0) | |
6040 | goto update_rate_and_reset; | |
6041 | ||
6042 | /* Update sampling information */ | |
6043 | bfqd->peak_rate_samples++; | |
6044 | ||
6045 | if ((bfqd->rq_in_driver > 0 || | |
6046 | now_ns - bfqd->last_completion < BFQ_MIN_TT) | |
6047 | && get_sdist(bfqd->last_position, rq) < BFQQ_SEEK_THR) | |
6048 | bfqd->sequential_samples++; | |
6049 | ||
6050 | bfqd->tot_sectors_dispatched += blk_rq_sectors(rq); | |
6051 | ||
6052 | /* Reset max observed rq size every 32 dispatches */ | |
6053 | if (likely(bfqd->peak_rate_samples % 32)) | |
6054 | bfqd->last_rq_max_size = | |
6055 | max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size); | |
6056 | else | |
6057 | bfqd->last_rq_max_size = blk_rq_sectors(rq); | |
6058 | ||
6059 | bfqd->delta_from_first = now_ns - bfqd->first_dispatch; | |
6060 | ||
6061 | /* Target observation interval not yet reached, go on sampling */ | |
6062 | if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL) | |
6063 | goto update_last_values; | |
6064 | ||
6065 | update_rate_and_reset: | |
6066 | bfq_update_rate_reset(bfqd, rq); | |
6067 | update_last_values: | |
6068 | bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); | |
6069 | bfqd->last_dispatch = now_ns; | |
6070 | } | |
6071 | ||
aee69d78 PV |
6072 | /* |
6073 | * Remove request from internal lists. | |
6074 | */ | |
6075 | static void bfq_dispatch_remove(struct request_queue *q, struct request *rq) | |
6076 | { | |
6077 | struct bfq_queue *bfqq = RQ_BFQQ(rq); | |
6078 | ||
6079 | /* | |
6080 | * For consistency, the next instruction should have been | |
6081 | * executed after removing the request from the queue and | |
6082 | * dispatching it. We execute instead this instruction before | |
6083 | * bfq_remove_request() (and hence introduce a temporary | |
6084 | * inconsistency), for efficiency. In fact, should this | |
6085 | * dispatch occur for a non in-service bfqq, this anticipated | |
6086 | * increment prevents two counters related to bfqq->dispatched | |
6087 | * from risking to be, first, uselessly decremented, and then | |
6088 | * incremented again when the (new) value of bfqq->dispatched | |
6089 | * happens to be taken into account. | |
6090 | */ | |
6091 | bfqq->dispatched++; | |
ab0e43e9 | 6092 | bfq_update_peak_rate(q->elevator->elevator_data, rq); |
aee69d78 PV |
6093 | |
6094 | bfq_remove_request(q, rq); | |
6095 | } | |
6096 | ||
6097 | static void __bfq_bfqq_expire(struct bfq_data *bfqd, struct bfq_queue *bfqq) | |
6098 | { | |
36eca894 AA |
6099 | /* |
6100 | * If this bfqq is shared between multiple processes, check | |
6101 | * to make sure that those processes are still issuing I/Os | |
6102 | * within the mean seek distance. If not, it may be time to | |
6103 | * break the queues apart again. | |
6104 | */ | |
6105 | if (bfq_bfqq_coop(bfqq) && BFQQ_SEEKY(bfqq)) | |
6106 | bfq_mark_bfqq_split_coop(bfqq); | |
6107 | ||
44e44a1b PV |
6108 | if (RB_EMPTY_ROOT(&bfqq->sort_list)) { |
6109 | if (bfqq->dispatched == 0) | |
6110 | /* | |
6111 | * Overloading budget_timeout field to store | |
6112 | * the time at which the queue remains with no | |
6113 | * backlog and no outstanding request; used by | |
6114 | * the weight-raising mechanism. | |
6115 | */ | |
6116 | bfqq->budget_timeout = jiffies; | |
6117 | ||
e21b7a0b | 6118 | bfq_del_bfqq_busy(bfqd, bfqq, true); |
36eca894 | 6119 | } else { |
e21b7a0b | 6120 | bfq_requeue_bfqq(bfqd, bfqq); |
36eca894 AA |
6121 | /* |
6122 | * Resort priority tree of potential close cooperators. | |
6123 | */ | |
6124 | bfq_pos_tree_add_move(bfqd, bfqq); | |
6125 | } | |
e21b7a0b AA |
6126 | |
6127 | /* | |
6128 | * All in-service entities must have been properly deactivated | |
6129 | * or requeued before executing the next function, which | |
6130 | * resets all in-service entites as no more in service. | |
6131 | */ | |
6132 | __bfq_bfqd_reset_in_service(bfqd); | |
aee69d78 PV |
6133 | } |
6134 | ||
6135 | /** | |
6136 | * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior. | |
6137 | * @bfqd: device data. | |
6138 | * @bfqq: queue to update. | |
6139 | * @reason: reason for expiration. | |
6140 | * | |
6141 | * Handle the feedback on @bfqq budget at queue expiration. | |
6142 | * See the body for detailed comments. | |
6143 | */ | |
6144 | static void __bfq_bfqq_recalc_budget(struct bfq_data *bfqd, | |
6145 | struct bfq_queue *bfqq, | |
6146 | enum bfqq_expiration reason) | |
6147 | { | |
6148 | struct request *next_rq; | |
6149 | int budget, min_budget; | |
6150 | ||
aee69d78 PV |
6151 | min_budget = bfq_min_budget(bfqd); |
6152 | ||
44e44a1b PV |
6153 | if (bfqq->wr_coeff == 1) |
6154 | budget = bfqq->max_budget; | |
6155 | else /* | |
6156 | * Use a constant, low budget for weight-raised queues, | |
6157 | * to help achieve a low latency. Keep it slightly higher | |
6158 | * than the minimum possible budget, to cause a little | |
6159 | * bit fewer expirations. | |
6160 | */ | |
6161 | budget = 2 * min_budget; | |
6162 | ||
aee69d78 PV |
6163 | bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last budg %d, budg left %d", |
6164 | bfqq->entity.budget, bfq_bfqq_budget_left(bfqq)); | |
6165 | bfq_log_bfqq(bfqd, bfqq, "recalc_budg: last max_budg %d, min budg %d", | |
6166 | budget, bfq_min_budget(bfqd)); | |
6167 | bfq_log_bfqq(bfqd, bfqq, "recalc_budg: sync %d, seeky %d", | |
6168 | bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue)); | |
6169 | ||
44e44a1b | 6170 | if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) { |
aee69d78 PV |
6171 | switch (reason) { |
6172 | /* | |
6173 | * Caveat: in all the following cases we trade latency | |
6174 | * for throughput. | |
6175 | */ | |
6176 | case BFQQE_TOO_IDLE: | |
54b60456 PV |
6177 | /* |
6178 | * This is the only case where we may reduce | |
6179 | * the budget: if there is no request of the | |
6180 | * process still waiting for completion, then | |
6181 | * we assume (tentatively) that the timer has | |
6182 | * expired because the batch of requests of | |
6183 | * the process could have been served with a | |
6184 | * smaller budget. Hence, betting that | |
6185 | * process will behave in the same way when it | |
6186 | * becomes backlogged again, we reduce its | |
6187 | * next budget. As long as we guess right, | |
6188 | * this budget cut reduces the latency | |
6189 | * experienced by the process. | |
6190 | * | |
6191 | * However, if there are still outstanding | |
6192 | * requests, then the process may have not yet | |
6193 | * issued its next request just because it is | |
6194 | * still waiting for the completion of some of | |
6195 | * the still outstanding ones. So in this | |
6196 | * subcase we do not reduce its budget, on the | |
6197 | * contrary we increase it to possibly boost | |
6198 | * the throughput, as discussed in the | |
6199 | * comments to the BUDGET_TIMEOUT case. | |
6200 | */ | |
6201 | if (bfqq->dispatched > 0) /* still outstanding reqs */ | |
6202 | budget = min(budget * 2, bfqd->bfq_max_budget); | |
6203 | else { | |
6204 | if (budget > 5 * min_budget) | |
6205 | budget -= 4 * min_budget; | |
6206 | else | |
6207 | budget = min_budget; | |
6208 | } | |
aee69d78 PV |
6209 | break; |
6210 | case BFQQE_BUDGET_TIMEOUT: | |
54b60456 PV |
6211 | /* |
6212 | * We double the budget here because it gives | |
6213 | * the chance to boost the throughput if this | |
6214 | * is not a seeky process (and has bumped into | |
6215 | * this timeout because of, e.g., ZBR). | |
6216 | */ | |
6217 | budget = min(budget * 2, bfqd->bfq_max_budget); | |
aee69d78 PV |
6218 | break; |
6219 | case BFQQE_BUDGET_EXHAUSTED: | |
6220 | /* | |
6221 | * The process still has backlog, and did not | |
6222 | * let either the budget timeout or the disk | |
6223 | * idling timeout expire. Hence it is not | |
6224 | * seeky, has a short thinktime and may be | |
6225 | * happy with a higher budget too. So | |
6226 | * definitely increase the budget of this good | |
6227 | * candidate to boost the disk throughput. | |
6228 | */ | |
54b60456 | 6229 | budget = min(budget * 4, bfqd->bfq_max_budget); |
aee69d78 PV |
6230 | break; |
6231 | case BFQQE_NO_MORE_REQUESTS: | |
6232 | /* | |
6233 | * For queues that expire for this reason, it | |
6234 | * is particularly important to keep the | |
6235 | * budget close to the actual service they | |
6236 | * need. Doing so reduces the timestamp | |
6237 | * misalignment problem described in the | |
6238 | * comments in the body of | |
6239 | * __bfq_activate_entity. In fact, suppose | |
6240 | * that a queue systematically expires for | |
6241 | * BFQQE_NO_MORE_REQUESTS and presents a | |
6242 | * new request in time to enjoy timestamp | |
6243 | * back-shifting. The larger the budget of the | |
6244 | * queue is with respect to the service the | |
6245 | * queue actually requests in each service | |
6246 | * slot, the more times the queue can be | |
6247 | * reactivated with the same virtual finish | |
6248 | * time. It follows that, even if this finish | |
6249 | * time is pushed to the system virtual time | |
6250 | * to reduce the consequent timestamp | |
6251 | * misalignment, the queue unjustly enjoys for | |
6252 | * many re-activations a lower finish time | |
6253 | * than all newly activated queues. | |
6254 | * | |
6255 | * The service needed by bfqq is measured | |
6256 | * quite precisely by bfqq->entity.service. | |
6257 | * Since bfqq does not enjoy device idling, | |
6258 | * bfqq->entity.service is equal to the number | |
6259 | * of sectors that the process associated with | |
6260 | * bfqq requested to read/write before waiting | |
6261 | * for request completions, or blocking for | |
6262 | * other reasons. | |
6263 | */ | |
6264 | budget = max_t(int, bfqq->entity.service, min_budget); | |
6265 | break; | |
6266 | default: | |
6267 | return; | |
6268 | } | |
44e44a1b | 6269 | } else if (!bfq_bfqq_sync(bfqq)) { |
aee69d78 PV |
6270 | /* |
6271 | * Async queues get always the maximum possible | |
6272 | * budget, as for them we do not care about latency | |
6273 | * (in addition, their ability to dispatch is limited | |
6274 | * by the charging factor). | |
6275 | */ | |
6276 | budget = bfqd->bfq_max_budget; | |
6277 | } | |
6278 | ||
6279 | bfqq->max_budget = budget; | |
6280 | ||
6281 | if (bfqd->budgets_assigned >= bfq_stats_min_budgets && | |
6282 | !bfqd->bfq_user_max_budget) | |
6283 | bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget); | |
6284 | ||
6285 | /* | |
6286 | * If there is still backlog, then assign a new budget, making | |
6287 | * sure that it is large enough for the next request. Since | |
6288 | * the finish time of bfqq must be kept in sync with the | |
6289 | * budget, be sure to call __bfq_bfqq_expire() *after* this | |
6290 | * update. | |
6291 | * | |
6292 | * If there is no backlog, then no need to update the budget; | |
6293 | * it will be updated on the arrival of a new request. | |
6294 | */ | |
6295 | next_rq = bfqq->next_rq; | |
6296 | if (next_rq) | |
6297 | bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget, | |
6298 | bfq_serv_to_charge(next_rq, bfqq)); | |
6299 | ||
6300 | bfq_log_bfqq(bfqd, bfqq, "head sect: %u, new budget %d", | |
6301 | next_rq ? blk_rq_sectors(next_rq) : 0, | |
6302 | bfqq->entity.budget); | |
6303 | } | |
6304 | ||
aee69d78 | 6305 | /* |
ab0e43e9 PV |
6306 | * Return true if the process associated with bfqq is "slow". The slow |
6307 | * flag is used, in addition to the budget timeout, to reduce the | |
6308 | * amount of service provided to seeky processes, and thus reduce | |
6309 | * their chances to lower the throughput. More details in the comments | |
6310 | * on the function bfq_bfqq_expire(). | |
6311 | * | |
6312 | * An important observation is in order: as discussed in the comments | |
6313 | * on the function bfq_update_peak_rate(), with devices with internal | |
6314 | * queues, it is hard if ever possible to know when and for how long | |
6315 | * an I/O request is processed by the device (apart from the trivial | |
6316 | * I/O pattern where a new request is dispatched only after the | |
6317 | * previous one has been completed). This makes it hard to evaluate | |
6318 | * the real rate at which the I/O requests of each bfq_queue are | |
6319 | * served. In fact, for an I/O scheduler like BFQ, serving a | |
6320 | * bfq_queue means just dispatching its requests during its service | |
6321 | * slot (i.e., until the budget of the queue is exhausted, or the | |
6322 | * queue remains idle, or, finally, a timeout fires). But, during the | |
6323 | * service slot of a bfq_queue, around 100 ms at most, the device may | |
6324 | * be even still processing requests of bfq_queues served in previous | |
6325 | * service slots. On the opposite end, the requests of the in-service | |
6326 | * bfq_queue may be completed after the service slot of the queue | |
6327 | * finishes. | |
6328 | * | |
6329 | * Anyway, unless more sophisticated solutions are used | |
6330 | * (where possible), the sum of the sizes of the requests dispatched | |
6331 | * during the service slot of a bfq_queue is probably the only | |
6332 | * approximation available for the service received by the bfq_queue | |
6333 | * during its service slot. And this sum is the quantity used in this | |
6334 | * function to evaluate the I/O speed of a process. | |
aee69d78 | 6335 | */ |
ab0e43e9 PV |
6336 | static bool bfq_bfqq_is_slow(struct bfq_data *bfqd, struct bfq_queue *bfqq, |
6337 | bool compensate, enum bfqq_expiration reason, | |
6338 | unsigned long *delta_ms) | |
aee69d78 | 6339 | { |
ab0e43e9 PV |
6340 | ktime_t delta_ktime; |
6341 | u32 delta_usecs; | |
6342 | bool slow = BFQQ_SEEKY(bfqq); /* if delta too short, use seekyness */ | |
aee69d78 | 6343 | |
ab0e43e9 | 6344 | if (!bfq_bfqq_sync(bfqq)) |
aee69d78 PV |
6345 | return false; |
6346 | ||
6347 | if (compensate) | |
ab0e43e9 | 6348 | delta_ktime = bfqd->last_idling_start; |
aee69d78 | 6349 | else |
ab0e43e9 PV |
6350 | delta_ktime = ktime_get(); |
6351 | delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start); | |
6352 | delta_usecs = ktime_to_us(delta_ktime); | |
aee69d78 PV |
6353 | |
6354 | /* don't use too short time intervals */ | |
ab0e43e9 PV |
6355 | if (delta_usecs < 1000) { |
6356 | if (blk_queue_nonrot(bfqd->queue)) | |
6357 | /* | |
6358 | * give same worst-case guarantees as idling | |
6359 | * for seeky | |
6360 | */ | |
6361 | *delta_ms = BFQ_MIN_TT / NSEC_PER_MSEC; | |
6362 | else /* charge at least one seek */ | |
6363 | *delta_ms = bfq_slice_idle / NSEC_PER_MSEC; | |
6364 | ||
6365 | return slow; | |
6366 | } | |
aee69d78 | 6367 | |
ab0e43e9 | 6368 | *delta_ms = delta_usecs / USEC_PER_MSEC; |
aee69d78 PV |
6369 | |
6370 | /* | |
ab0e43e9 PV |
6371 | * Use only long (> 20ms) intervals to filter out excessive |
6372 | * spikes in service rate estimation. | |
aee69d78 | 6373 | */ |
ab0e43e9 PV |
6374 | if (delta_usecs > 20000) { |
6375 | /* | |
6376 | * Caveat for rotational devices: processes doing I/O | |
6377 | * in the slower disk zones tend to be slow(er) even | |
6378 | * if not seeky. In this respect, the estimated peak | |
6379 | * rate is likely to be an average over the disk | |
6380 | * surface. Accordingly, to not be too harsh with | |
6381 | * unlucky processes, a process is deemed slow only if | |
6382 | * its rate has been lower than half of the estimated | |
6383 | * peak rate. | |
6384 | */ | |
6385 | slow = bfqq->entity.service < bfqd->bfq_max_budget / 2; | |
aee69d78 PV |
6386 | } |
6387 | ||
ab0e43e9 | 6388 | bfq_log_bfqq(bfqd, bfqq, "bfq_bfqq_is_slow: slow %d", slow); |
aee69d78 | 6389 | |
ab0e43e9 | 6390 | return slow; |
aee69d78 PV |
6391 | } |
6392 | ||
77b7dcea PV |
6393 | /* |
6394 | * To be deemed as soft real-time, an application must meet two | |
6395 | * requirements. First, the application must not require an average | |
6396 | * bandwidth higher than the approximate bandwidth required to playback or | |
6397 | * record a compressed high-definition video. | |
6398 | * The next function is invoked on the completion of the last request of a | |
6399 | * batch, to compute the next-start time instant, soft_rt_next_start, such | |
6400 | * that, if the next request of the application does not arrive before | |
6401 | * soft_rt_next_start, then the above requirement on the bandwidth is met. | |
6402 | * | |
6403 | * The second requirement is that the request pattern of the application is | |
6404 | * isochronous, i.e., that, after issuing a request or a batch of requests, | |
6405 | * the application stops issuing new requests until all its pending requests | |
6406 | * have been completed. After that, the application may issue a new batch, | |
6407 | * and so on. | |
6408 | * For this reason the next function is invoked to compute | |
6409 | * soft_rt_next_start only for applications that meet this requirement, | |
6410 | * whereas soft_rt_next_start is set to infinity for applications that do | |
6411 | * not. | |
6412 | * | |
6413 | * Unfortunately, even a greedy application may happen to behave in an | |
6414 | * isochronous way if the CPU load is high. In fact, the application may | |
6415 | * stop issuing requests while the CPUs are busy serving other processes, | |
6416 | * then restart, then stop again for a while, and so on. In addition, if | |
6417 | * the disk achieves a low enough throughput with the request pattern | |
6418 | * issued by the application (e.g., because the request pattern is random | |
6419 | * and/or the device is slow), then the application may meet the above | |
6420 | * bandwidth requirement too. To prevent such a greedy application to be | |
6421 | * deemed as soft real-time, a further rule is used in the computation of | |
6422 | * soft_rt_next_start: soft_rt_next_start must be higher than the current | |
6423 | * time plus the maximum time for which the arrival of a request is waited | |
6424 | * for when a sync queue becomes idle, namely bfqd->bfq_slice_idle. | |
6425 | * This filters out greedy applications, as the latter issue instead their | |
6426 | * next request as soon as possible after the last one has been completed | |
6427 | * (in contrast, when a batch of requests is completed, a soft real-time | |
6428 | * application spends some time processing data). | |
6429 | * | |
6430 | * Unfortunately, the last filter may easily generate false positives if | |
6431 | * only bfqd->bfq_slice_idle is used as a reference time interval and one | |
6432 | * or both the following cases occur: | |
6433 | * 1) HZ is so low that the duration of a jiffy is comparable to or higher | |
6434 | * than bfqd->bfq_slice_idle. This happens, e.g., on slow devices with | |
6435 | * HZ=100. | |
6436 | * 2) jiffies, instead of increasing at a constant rate, may stop increasing | |
6437 | * for a while, then suddenly 'jump' by several units to recover the lost | |
6438 | * increments. This seems to happen, e.g., inside virtual machines. | |
6439 | * To address this issue, we do not use as a reference time interval just | |
6440 | * bfqd->bfq_slice_idle, but bfqd->bfq_slice_idle plus a few jiffies. In | |
6441 | * particular we add the minimum number of jiffies for which the filter | |
6442 | * seems to be quite precise also in embedded systems and KVM/QEMU virtual | |
6443 | * machines. | |
6444 | */ | |
6445 | static unsigned long bfq_bfqq_softrt_next_start(struct bfq_data *bfqd, | |
6446 | struct bfq_queue *bfqq) | |
6447 | { | |
6448 | return max(bfqq->last_idle_bklogged + | |
6449 | HZ * bfqq->service_from_backlogged / | |
6450 | bfqd->bfq_wr_max_softrt_rate, | |
6451 | jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4); | |
6452 | } | |
6453 | ||
6454 | /* | |
6455 | * Return the farthest future time instant according to jiffies | |
6456 | * macros. | |
6457 | */ | |
6458 | static unsigned long bfq_greatest_from_now(void) | |
6459 | { | |
6460 | return jiffies + MAX_JIFFY_OFFSET; | |
6461 | } | |
6462 | ||
aee69d78 PV |
6463 | /* |
6464 | * Return the farthest past time instant according to jiffies | |
6465 | * macros. | |
6466 | */ | |
6467 | static unsigned long bfq_smallest_from_now(void) | |
6468 | { | |
6469 | return jiffies - MAX_JIFFY_OFFSET; | |
6470 | } | |
6471 | ||
6472 | /** | |
6473 | * bfq_bfqq_expire - expire a queue. | |
6474 | * @bfqd: device owning the queue. | |
6475 | * @bfqq: the queue to expire. | |
6476 | * @compensate: if true, compensate for the time spent idling. | |
6477 | * @reason: the reason causing the expiration. | |
6478 | * | |
c074170e PV |
6479 | * If the process associated with bfqq does slow I/O (e.g., because it |
6480 | * issues random requests), we charge bfqq with the time it has been | |
6481 | * in service instead of the service it has received (see | |
6482 | * bfq_bfqq_charge_time for details on how this goal is achieved). As | |
6483 | * a consequence, bfqq will typically get higher timestamps upon | |
6484 | * reactivation, and hence it will be rescheduled as if it had | |
6485 | * received more service than what it has actually received. In the | |
6486 | * end, bfqq receives less service in proportion to how slowly its | |
6487 | * associated process consumes its budgets (and hence how seriously it | |
6488 | * tends to lower the throughput). In addition, this time-charging | |
6489 | * strategy guarantees time fairness among slow processes. In | |
6490 | * contrast, if the process associated with bfqq is not slow, we | |
6491 | * charge bfqq exactly with the service it has received. | |
aee69d78 | 6492 | * |
c074170e PV |
6493 | * Charging time to the first type of queues and the exact service to |
6494 | * the other has the effect of using the WF2Q+ policy to schedule the | |
6495 | * former on a timeslice basis, without violating service domain | |
6496 | * guarantees among the latter. | |
aee69d78 PV |
6497 | */ |
6498 | static void bfq_bfqq_expire(struct bfq_data *bfqd, | |
6499 | struct bfq_queue *bfqq, | |
6500 | bool compensate, | |
6501 | enum bfqq_expiration reason) | |
6502 | { | |
6503 | bool slow; | |
ab0e43e9 PV |
6504 | unsigned long delta = 0; |
6505 | struct bfq_entity *entity = &bfqq->entity; | |
aee69d78 PV |
6506 | int ref; |
6507 | ||
6508 | /* | |
ab0e43e9 | 6509 | * Check whether the process is slow (see bfq_bfqq_is_slow). |
aee69d78 | 6510 | */ |
ab0e43e9 | 6511 | slow = bfq_bfqq_is_slow(bfqd, bfqq, compensate, reason, &delta); |
aee69d78 | 6512 | |
77b7dcea PV |
6513 | /* |
6514 | * Increase service_from_backlogged before next statement, | |
6515 | * because the possible next invocation of | |
6516 | * bfq_bfqq_charge_time would likely inflate | |
6517 | * entity->service. In contrast, service_from_backlogged must | |
6518 | * contain real service, to enable the soft real-time | |
6519 | * heuristic to correctly compute the bandwidth consumed by | |
6520 | * bfqq. | |
6521 | */ | |
6522 | bfqq->service_from_backlogged += entity->service; | |
6523 | ||
aee69d78 | 6524 | /* |
c074170e PV |
6525 | * As above explained, charge slow (typically seeky) and |
6526 | * timed-out queues with the time and not the service | |
6527 | * received, to favor sequential workloads. | |
6528 | * | |
6529 | * Processes doing I/O in the slower disk zones will tend to | |
6530 | * be slow(er) even if not seeky. Therefore, since the | |
6531 | * estimated peak rate is actually an average over the disk | |
6532 | * surface, these processes may timeout just for bad luck. To | |
6533 | * avoid punishing them, do not charge time to processes that | |
6534 | * succeeded in consuming at least 2/3 of their budget. This | |
6535 | * allows BFQ to preserve enough elasticity to still perform | |
6536 | * bandwidth, and not time, distribution with little unlucky | |
6537 | * or quasi-sequential processes. | |
aee69d78 | 6538 | */ |
44e44a1b PV |
6539 | if (bfqq->wr_coeff == 1 && |
6540 | (slow || | |
6541 | (reason == BFQQE_BUDGET_TIMEOUT && | |
6542 | bfq_bfqq_budget_left(bfqq) >= entity->budget / 3))) | |
c074170e | 6543 | bfq_bfqq_charge_time(bfqd, bfqq, delta); |
aee69d78 PV |
6544 | |
6545 | if (reason == BFQQE_TOO_IDLE && | |
ab0e43e9 | 6546 | entity->service <= 2 * entity->budget / 10) |
aee69d78 PV |
6547 | bfq_clear_bfqq_IO_bound(bfqq); |
6548 | ||
44e44a1b PV |
6549 | if (bfqd->low_latency && bfqq->wr_coeff == 1) |
6550 | bfqq->last_wr_start_finish = jiffies; | |
6551 | ||
77b7dcea PV |
6552 | if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 && |
6553 | RB_EMPTY_ROOT(&bfqq->sort_list)) { | |
6554 | /* | |
6555 | * If we get here, and there are no outstanding | |
6556 | * requests, then the request pattern is isochronous | |
6557 | * (see the comments on the function | |
6558 | * bfq_bfqq_softrt_next_start()). Thus we can compute | |
6559 | * soft_rt_next_start. If, instead, the queue still | |
6560 | * has outstanding requests, then we have to wait for | |
6561 | * the completion of all the outstanding requests to | |
6562 | * discover whether the request pattern is actually | |
6563 | * isochronous. | |
6564 | */ | |
6565 | if (bfqq->dispatched == 0) | |
6566 | bfqq->soft_rt_next_start = | |
6567 | bfq_bfqq_softrt_next_start(bfqd, bfqq); | |
6568 | else { | |
6569 | /* | |
6570 | * The application is still waiting for the | |
6571 | * completion of one or more requests: | |
6572 | * prevent it from possibly being incorrectly | |
6573 | * deemed as soft real-time by setting its | |
6574 | * soft_rt_next_start to infinity. In fact, | |
6575 | * without this assignment, the application | |
6576 | * would be incorrectly deemed as soft | |
6577 | * real-time if: | |
6578 | * 1) it issued a new request before the | |
6579 | * completion of all its in-flight | |
6580 | * requests, and | |
6581 | * 2) at that time, its soft_rt_next_start | |
6582 | * happened to be in the past. | |
6583 | */ | |
6584 | bfqq->soft_rt_next_start = | |
6585 | bfq_greatest_from_now(); | |
6586 | /* | |
6587 | * Schedule an update of soft_rt_next_start to when | |
6588 | * the task may be discovered to be isochronous. | |
6589 | */ | |
6590 | bfq_mark_bfqq_softrt_update(bfqq); | |
6591 | } | |
6592 | } | |
6593 | ||
aee69d78 PV |
6594 | bfq_log_bfqq(bfqd, bfqq, |
6595 | "expire (%d, slow %d, num_disp %d, idle_win %d)", reason, | |
6596 | slow, bfqq->dispatched, bfq_bfqq_idle_window(bfqq)); | |
6597 | ||
6598 | /* | |
6599 | * Increase, decrease or leave budget unchanged according to | |
6600 | * reason. | |
6601 | */ | |
6602 | __bfq_bfqq_recalc_budget(bfqd, bfqq, reason); | |
6603 | ref = bfqq->ref; | |
6604 | __bfq_bfqq_expire(bfqd, bfqq); | |
6605 | ||
6606 | /* mark bfqq as waiting a request only if a bic still points to it */ | |
6607 | if (ref > 1 && !bfq_bfqq_busy(bfqq) && | |
6608 | reason != BFQQE_BUDGET_TIMEOUT && | |
6609 | reason != BFQQE_BUDGET_EXHAUSTED) | |
6610 | bfq_mark_bfqq_non_blocking_wait_rq(bfqq); | |
6611 | } | |
6612 | ||
6613 | /* | |
6614 | * Budget timeout is not implemented through a dedicated timer, but | |
6615 | * just checked on request arrivals and completions, as well as on | |
6616 | * idle timer expirations. | |
6617 | */ | |
6618 | static bool bfq_bfqq_budget_timeout(struct bfq_queue *bfqq) | |
6619 | { | |
44e44a1b | 6620 | return time_is_before_eq_jiffies(bfqq->budget_timeout); |
aee69d78 PV |
6621 | } |
6622 | ||
6623 | /* | |
6624 | * If we expire a queue that is actively waiting (i.e., with the | |
6625 | * device idled) for the arrival of a new request, then we may incur | |
6626 | * the timestamp misalignment problem described in the body of the | |
6627 | * function __bfq_activate_entity. Hence we return true only if this | |
6628 | * condition does not hold, or if the queue is slow enough to deserve | |
6629 | * only to be kicked off for preserving a high throughput. | |
6630 | */ | |
6631 | static bool bfq_may_expire_for_budg_timeout(struct bfq_queue *bfqq) | |
6632 | { | |
6633 | bfq_log_bfqq(bfqq->bfqd, bfqq, | |
6634 | "may_budget_timeout: wait_request %d left %d timeout %d", | |
6635 | bfq_bfqq_wait_request(bfqq), | |
6636 | bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3, | |
6637 | bfq_bfqq_budget_timeout(bfqq)); | |
6638 | ||
6639 | return (!bfq_bfqq_wait_request(bfqq) || | |
6640 | bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3) | |
6641 | && | |
6642 | bfq_bfqq_budget_timeout(bfqq); | |
6643 | } | |
6644 | ||
6645 | /* | |
6646 | * For a queue that becomes empty, device idling is allowed only if | |
44e44a1b PV |
6647 | * this function returns true for the queue. As a consequence, since |
6648 | * device idling plays a critical role in both throughput boosting and | |
6649 | * service guarantees, the return value of this function plays a | |
6650 | * critical role in both these aspects as well. | |
6651 | * | |
6652 | * In a nutshell, this function returns true only if idling is | |
6653 | * beneficial for throughput or, even if detrimental for throughput, | |
6654 | * idling is however necessary to preserve service guarantees (low | |
6655 | * latency, desired throughput distribution, ...). In particular, on | |
6656 | * NCQ-capable devices, this function tries to return false, so as to | |
6657 | * help keep the drives' internal queues full, whenever this helps the | |
6658 | * device boost the throughput without causing any service-guarantee | |
6659 | * issue. | |
6660 | * | |
6661 | * In more detail, the return value of this function is obtained by, | |
6662 | * first, computing a number of boolean variables that take into | |
6663 | * account throughput and service-guarantee issues, and, then, | |
6664 | * combining these variables in a logical expression. Most of the | |
6665 | * issues taken into account are not trivial. We discuss these issues | |
6666 | * individually while introducing the variables. | |
aee69d78 PV |
6667 | */ |
6668 | static bool bfq_bfqq_may_idle(struct bfq_queue *bfqq) | |
6669 | { | |
6670 | struct bfq_data *bfqd = bfqq->bfqd; | |
cfd69712 | 6671 | bool idling_boosts_thr, idling_boosts_thr_without_issues, |
e1b2324d | 6672 | idling_needed_for_service_guarantees, |
cfd69712 | 6673 | asymmetric_scenario; |
aee69d78 PV |
6674 | |
6675 | if (bfqd->strict_guarantees) | |
6676 | return true; | |
6677 | ||
6678 | /* | |
44e44a1b PV |
6679 | * The next variable takes into account the cases where idling |
6680 | * boosts the throughput. | |
6681 | * | |
e01eff01 PV |
6682 | * The value of the variable is computed considering, first, that |
6683 | * idling is virtually always beneficial for the throughput if: | |
aee69d78 | 6684 | * (a) the device is not NCQ-capable, or |
bf2b79e7 | 6685 | * (b) regardless of the presence of NCQ, the device is rotational |
e01eff01 | 6686 | * and the request pattern for bfqq is I/O-bound and sequential. |
bf2b79e7 PV |
6687 | * |
6688 | * Secondly, and in contrast to the above item (b), idling an | |
6689 | * NCQ-capable flash-based device would not boost the | |
e01eff01 | 6690 | * throughput even with sequential I/O; rather it would lower |
bf2b79e7 PV |
6691 | * the throughput in proportion to how fast the device |
6692 | * is. Accordingly, the next variable is true if any of the | |
6693 | * above conditions (a) and (b) is true, and, in particular, | |
6694 | * happens to be false if bfqd is an NCQ-capable flash-based | |
6695 | * device. | |
aee69d78 | 6696 | */ |
bf2b79e7 | 6697 | idling_boosts_thr = !bfqd->hw_tag || |
e01eff01 PV |
6698 | (!blk_queue_nonrot(bfqd->queue) && bfq_bfqq_IO_bound(bfqq) && |
6699 | bfq_bfqq_idle_window(bfqq)); | |
aee69d78 | 6700 | |
cfd69712 PV |
6701 | /* |
6702 | * The value of the next variable, | |
6703 | * idling_boosts_thr_without_issues, is equal to that of | |
6704 | * idling_boosts_thr, unless a special case holds. In this | |
6705 | * special case, described below, idling may cause problems to | |
6706 | * weight-raised queues. | |
6707 | * | |
6708 | * When the request pool is saturated (e.g., in the presence | |
6709 | * of write hogs), if the processes associated with | |
6710 | * non-weight-raised queues ask for requests at a lower rate, | |
6711 | * then processes associated with weight-raised queues have a | |
6712 | * higher probability to get a request from the pool | |
6713 | * immediately (or at least soon) when they need one. Thus | |
6714 | * they have a higher probability to actually get a fraction | |
6715 | * of the device throughput proportional to their high | |
6716 | * weight. This is especially true with NCQ-capable drives, | |
6717 | * which enqueue several requests in advance, and further | |
6718 | * reorder internally-queued requests. | |
6719 | * | |
6720 | * For this reason, we force to false the value of | |
6721 | * idling_boosts_thr_without_issues if there are weight-raised | |
6722 | * busy queues. In this case, and if bfqq is not weight-raised, | |
6723 | * this guarantees that the device is not idled for bfqq (if, | |
6724 | * instead, bfqq is weight-raised, then idling will be | |
6725 | * guaranteed by another variable, see below). Combined with | |
6726 | * the timestamping rules of BFQ (see [1] for details), this | |
6727 | * behavior causes bfqq, and hence any sync non-weight-raised | |
6728 | * queue, to get a lower number of requests served, and thus | |
6729 | * to ask for a lower number of requests from the request | |
6730 | * pool, before the busy weight-raised queues get served | |
6731 | * again. This often mitigates starvation problems in the | |
6732 | * presence of heavy write workloads and NCQ, thereby | |
6733 | * guaranteeing a higher application and system responsiveness | |
6734 | * in these hostile scenarios. | |
6735 | */ | |
6736 | idling_boosts_thr_without_issues = idling_boosts_thr && | |
6737 | bfqd->wr_busy_queues == 0; | |
6738 | ||
aee69d78 | 6739 | /* |
bf2b79e7 PV |
6740 | * There is then a case where idling must be performed not |
6741 | * for throughput concerns, but to preserve service | |
6742 | * guarantees. | |
6743 | * | |
6744 | * To introduce this case, we can note that allowing the drive | |
6745 | * to enqueue more than one request at a time, and hence | |
44e44a1b | 6746 | * delegating de facto final scheduling decisions to the |
bf2b79e7 | 6747 | * drive's internal scheduler, entails loss of control on the |
44e44a1b | 6748 | * actual request service order. In particular, the critical |
bf2b79e7 | 6749 | * situation is when requests from different processes happen |
44e44a1b PV |
6750 | * to be present, at the same time, in the internal queue(s) |
6751 | * of the drive. In such a situation, the drive, by deciding | |
6752 | * the service order of the internally-queued requests, does | |
6753 | * determine also the actual throughput distribution among | |
6754 | * these processes. But the drive typically has no notion or | |
6755 | * concern about per-process throughput distribution, and | |
6756 | * makes its decisions only on a per-request basis. Therefore, | |
6757 | * the service distribution enforced by the drive's internal | |
6758 | * scheduler is likely to coincide with the desired | |
6759 | * device-throughput distribution only in a completely | |
bf2b79e7 PV |
6760 | * symmetric scenario where: |
6761 | * (i) each of these processes must get the same throughput as | |
6762 | * the others; | |
6763 | * (ii) all these processes have the same I/O pattern | |
6764 | (either sequential or random). | |
6765 | * In fact, in such a scenario, the drive will tend to treat | |
6766 | * the requests of each of these processes in about the same | |
6767 | * way as the requests of the others, and thus to provide | |
6768 | * each of these processes with about the same throughput | |
6769 | * (which is exactly the desired throughput distribution). In | |
6770 | * contrast, in any asymmetric scenario, device idling is | |
6771 | * certainly needed to guarantee that bfqq receives its | |
6772 | * assigned fraction of the device throughput (see [1] for | |
6773 | * details). | |
6774 | * | |
6775 | * We address this issue by controlling, actually, only the | |
6776 | * symmetry sub-condition (i), i.e., provided that | |
6777 | * sub-condition (i) holds, idling is not performed, | |
6778 | * regardless of whether sub-condition (ii) holds. In other | |
6779 | * words, only if sub-condition (i) holds, then idling is | |
6780 | * allowed, and the device tends to be prevented from queueing | |
6781 | * many requests, possibly of several processes. The reason | |
6782 | * for not controlling also sub-condition (ii) is that we | |
6783 | * exploit preemption to preserve guarantees in case of | |
6784 | * symmetric scenarios, even if (ii) does not hold, as | |
6785 | * explained in the next two paragraphs. | |
6786 | * | |
6787 | * Even if a queue, say Q, is expired when it remains idle, Q | |
6788 | * can still preempt the new in-service queue if the next | |
6789 | * request of Q arrives soon (see the comments on | |
6790 | * bfq_bfqq_update_budg_for_activation). If all queues and | |
6791 | * groups have the same weight, this form of preemption, | |
6792 | * combined with the hole-recovery heuristic described in the | |
6793 | * comments on function bfq_bfqq_update_budg_for_activation, | |
6794 | * are enough to preserve a correct bandwidth distribution in | |
6795 | * the mid term, even without idling. In fact, even if not | |
6796 | * idling allows the internal queues of the device to contain | |
6797 | * many requests, and thus to reorder requests, we can rather | |
6798 | * safely assume that the internal scheduler still preserves a | |
6799 | * minimum of mid-term fairness. The motivation for using | |
6800 | * preemption instead of idling is that, by not idling, | |
6801 | * service guarantees are preserved without minimally | |
6802 | * sacrificing throughput. In other words, both a high | |
6803 | * throughput and its desired distribution are obtained. | |
6804 | * | |
6805 | * More precisely, this preemption-based, idleless approach | |
6806 | * provides fairness in terms of IOPS, and not sectors per | |
6807 | * second. This can be seen with a simple example. Suppose | |
6808 | * that there are two queues with the same weight, but that | |
6809 | * the first queue receives requests of 8 sectors, while the | |
6810 | * second queue receives requests of 1024 sectors. In | |
6811 | * addition, suppose that each of the two queues contains at | |
6812 | * most one request at a time, which implies that each queue | |
6813 | * always remains idle after it is served. Finally, after | |
6814 | * remaining idle, each queue receives very quickly a new | |
6815 | * request. It follows that the two queues are served | |
6816 | * alternatively, preempting each other if needed. This | |
6817 | * implies that, although both queues have the same weight, | |
6818 | * the queue with large requests receives a service that is | |
6819 | * 1024/8 times as high as the service received by the other | |
6820 | * queue. | |
44e44a1b | 6821 | * |
bf2b79e7 PV |
6822 | * On the other hand, device idling is performed, and thus |
6823 | * pure sector-domain guarantees are provided, for the | |
6824 | * following queues, which are likely to need stronger | |
6825 | * throughput guarantees: weight-raised queues, and queues | |
6826 | * with a higher weight than other queues. When such queues | |
6827 | * are active, sub-condition (i) is false, which triggers | |
6828 | * device idling. | |
44e44a1b | 6829 | * |
bf2b79e7 PV |
6830 | * According to the above considerations, the next variable is |
6831 | * true (only) if sub-condition (i) holds. To compute the | |
6832 | * value of this variable, we not only use the return value of | |
6833 | * the function bfq_symmetric_scenario(), but also check | |
6834 | * whether bfqq is being weight-raised, because | |
6835 | * bfq_symmetric_scenario() does not take into account also | |
6836 | * weight-raised queues (see comments on | |
6837 | * bfq_weights_tree_add()). | |
44e44a1b PV |
6838 | * |
6839 | * As a side note, it is worth considering that the above | |
6840 | * device-idling countermeasures may however fail in the | |
6841 | * following unlucky scenario: if idling is (correctly) | |
bf2b79e7 PV |
6842 | * disabled in a time period during which all symmetry |
6843 | * sub-conditions hold, and hence the device is allowed to | |
44e44a1b PV |
6844 | * enqueue many requests, but at some later point in time some |
6845 | * sub-condition stops to hold, then it may become impossible | |
6846 | * to let requests be served in the desired order until all | |
6847 | * the requests already queued in the device have been served. | |
6848 | */ | |
bf2b79e7 PV |
6849 | asymmetric_scenario = bfqq->wr_coeff > 1 || |
6850 | !bfq_symmetric_scenario(bfqd); | |
44e44a1b | 6851 | |
e1b2324d AA |
6852 | /* |
6853 | * Finally, there is a case where maximizing throughput is the | |
6854 | * best choice even if it may cause unfairness toward | |
6855 | * bfqq. Such a case is when bfqq became active in a burst of | |
6856 | * queue activations. Queues that became active during a large | |
6857 | * burst benefit only from throughput, as discussed in the | |
6858 | * comments on bfq_handle_burst. Thus, if bfqq became active | |
6859 | * in a burst and not idling the device maximizes throughput, | |
6860 | * then the device must no be idled, because not idling the | |
6861 | * device provides bfqq and all other queues in the burst with | |
6862 | * maximum benefit. Combining this and the above case, we can | |
6863 | * now establish when idling is actually needed to preserve | |
6864 | * service guarantees. | |
6865 | */ | |
6866 | idling_needed_for_service_guarantees = | |
6867 | asymmetric_scenario && !bfq_bfqq_in_large_burst(bfqq); | |
6868 | ||
44e44a1b PV |
6869 | /* |
6870 | * We have now all the components we need to compute the return | |
6871 | * value of the function, which is true only if both the following | |
6872 | * conditions hold: | |
aee69d78 | 6873 | * 1) bfqq is sync, because idling make sense only for sync queues; |
44e44a1b PV |
6874 | * 2) idling either boosts the throughput (without issues), or |
6875 | * is necessary to preserve service guarantees. | |
aee69d78 | 6876 | */ |
44e44a1b | 6877 | return bfq_bfqq_sync(bfqq) && |
e1b2324d AA |
6878 | (idling_boosts_thr_without_issues || |
6879 | idling_needed_for_service_guarantees); | |
aee69d78 PV |
6880 | } |
6881 | ||
6882 | /* | |
6883 | * If the in-service queue is empty but the function bfq_bfqq_may_idle | |
6884 | * returns true, then: | |
6885 | * 1) the queue must remain in service and cannot be expired, and | |
6886 | * 2) the device must be idled to wait for the possible arrival of a new | |
6887 | * request for the queue. | |
6888 | * See the comments on the function bfq_bfqq_may_idle for the reasons | |
6889 | * why performing device idling is the best choice to boost the throughput | |
6890 | * and preserve service guarantees when bfq_bfqq_may_idle itself | |
6891 | * returns true. | |
6892 | */ | |
6893 | static bool bfq_bfqq_must_idle(struct bfq_queue *bfqq) | |
6894 | { | |
6895 | struct bfq_data *bfqd = bfqq->bfqd; | |
6896 | ||
6897 | return RB_EMPTY_ROOT(&bfqq->sort_list) && bfqd->bfq_slice_idle != 0 && | |
6898 | bfq_bfqq_may_idle(bfqq); | |
6899 | } | |
6900 | ||
6901 | /* | |
6902 | * Select a queue for service. If we have a current queue in service, | |
6903 | * check whether to continue servicing it, or retrieve and set a new one. | |
6904 | */ | |
6905 | static struct bfq_queue *bfq_select_queue(struct bfq_data *bfqd) | |
6906 | { | |
6907 | struct bfq_queue *bfqq; | |
6908 | struct request *next_rq; | |
6909 | enum bfqq_expiration reason = BFQQE_BUDGET_TIMEOUT; | |
6910 | ||
6911 | bfqq = bfqd->in_service_queue; | |
6912 | if (!bfqq) | |
6913 | goto new_queue; | |
6914 | ||
6915 | bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue"); | |
6916 | ||
6917 | if (bfq_may_expire_for_budg_timeout(bfqq) && | |
6918 | !bfq_bfqq_wait_request(bfqq) && | |
6919 | !bfq_bfqq_must_idle(bfqq)) | |
6920 | goto expire; | |
6921 | ||
6922 | check_queue: | |
6923 | /* | |
6924 | * This loop is rarely executed more than once. Even when it | |
6925 | * happens, it is much more convenient to re-execute this loop | |
6926 | * than to return NULL and trigger a new dispatch to get a | |
6927 | * request served. | |
6928 | */ | |
6929 | next_rq = bfqq->next_rq; | |
6930 | /* | |
6931 | * If bfqq has requests queued and it has enough budget left to | |
6932 | * serve them, keep the queue, otherwise expire it. | |
6933 | */ | |
6934 | if (next_rq) { | |
6935 | if (bfq_serv_to_charge(next_rq, bfqq) > | |
6936 | bfq_bfqq_budget_left(bfqq)) { | |
6937 | /* | |
6938 | * Expire the queue for budget exhaustion, | |
6939 | * which makes sure that the next budget is | |
6940 | * enough to serve the next request, even if | |
6941 | * it comes from the fifo expired path. | |
6942 | */ | |
6943 | reason = BFQQE_BUDGET_EXHAUSTED; | |
6944 | goto expire; | |
6945 | } else { | |
6946 | /* | |
6947 | * The idle timer may be pending because we may | |
6948 | * not disable disk idling even when a new request | |
6949 | * arrives. | |
6950 | */ | |
6951 | if (bfq_bfqq_wait_request(bfqq)) { | |
6952 | /* | |
6953 | * If we get here: 1) at least a new request | |
6954 | * has arrived but we have not disabled the | |
6955 | * timer because the request was too small, | |
6956 | * 2) then the block layer has unplugged | |
6957 | * the device, causing the dispatch to be | |
6958 | * invoked. | |
6959 | * | |
6960 | * Since the device is unplugged, now the | |
6961 | * requests are probably large enough to | |
6962 | * provide a reasonable throughput. | |
6963 | * So we disable idling. | |
6964 | */ | |
6965 | bfq_clear_bfqq_wait_request(bfqq); | |
6966 | hrtimer_try_to_cancel(&bfqd->idle_slice_timer); | |
e21b7a0b | 6967 | bfqg_stats_update_idle_time(bfqq_group(bfqq)); |
aee69d78 PV |
6968 | } |
6969 | goto keep_queue; | |
6970 | } | |
6971 | } | |
6972 | ||
6973 | /* | |
6974 | * No requests pending. However, if the in-service queue is idling | |
6975 | * for a new request, or has requests waiting for a completion and | |
6976 | * may idle after their completion, then keep it anyway. | |
6977 | */ | |
6978 | if (bfq_bfqq_wait_request(bfqq) || | |
6979 | (bfqq->dispatched != 0 && bfq_bfqq_may_idle(bfqq))) { | |
6980 | bfqq = NULL; | |
6981 | goto keep_queue; | |
6982 | } | |
6983 | ||
6984 | reason = BFQQE_NO_MORE_REQUESTS; | |
6985 | expire: | |
6986 | bfq_bfqq_expire(bfqd, bfqq, false, reason); | |
6987 | new_queue: | |
6988 | bfqq = bfq_set_in_service_queue(bfqd); | |
6989 | if (bfqq) { | |
6990 | bfq_log_bfqq(bfqd, bfqq, "select_queue: checking new queue"); | |
6991 | goto check_queue; | |
6992 | } | |
6993 | keep_queue: | |
6994 | if (bfqq) | |
6995 | bfq_log_bfqq(bfqd, bfqq, "select_queue: returned this queue"); | |
6996 | else | |
6997 | bfq_log(bfqd, "select_queue: no queue returned"); | |
6998 | ||
6999 | return bfqq; | |
7000 | } | |
7001 | ||
44e44a1b PV |
7002 | static void bfq_update_wr_data(struct bfq_data *bfqd, struct bfq_queue *bfqq) |
7003 | { | |
7004 | struct bfq_entity *entity = &bfqq->entity; | |
7005 | ||
7006 | if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */ | |
7007 | bfq_log_bfqq(bfqd, bfqq, | |
7008 | "raising period dur %u/%u msec, old coeff %u, w %d(%d)", | |
7009 | jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish), | |
7010 | jiffies_to_msecs(bfqq->wr_cur_max_time), | |
7011 | bfqq->wr_coeff, | |
7012 | bfqq->entity.weight, bfqq->entity.orig_weight); | |
7013 | ||
7014 | if (entity->prio_changed) | |
7015 | bfq_log_bfqq(bfqd, bfqq, "WARN: pending prio change"); | |
7016 | ||
7017 | /* | |
e1b2324d AA |
7018 | * If the queue was activated in a burst, or too much |
7019 | * time has elapsed from the beginning of this | |
7020 | * weight-raising period, then end weight raising. | |
44e44a1b | 7021 | */ |
e1b2324d AA |
7022 | if (bfq_bfqq_in_large_burst(bfqq)) |
7023 | bfq_bfqq_end_wr(bfqq); | |
7024 | else if (time_is_before_jiffies(bfqq->last_wr_start_finish + | |
7025 | bfqq->wr_cur_max_time)) { | |
77b7dcea PV |
7026 | if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time || |
7027 | time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt + | |
e1b2324d | 7028 | bfq_wr_duration(bfqd))) |
77b7dcea PV |
7029 | bfq_bfqq_end_wr(bfqq); |
7030 | else { | |
7031 | /* switch back to interactive wr */ | |
7032 | bfqq->wr_coeff = bfqd->bfq_wr_coeff; | |
7033 | bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); | |
7034 | bfqq->last_wr_start_finish = | |
7035 | bfqq->wr_start_at_switch_to_srt; | |
7036 | bfqq->entity.prio_changed = 1; | |
7037 | } | |
44e44a1b PV |
7038 | } |
7039 | } | |
7040 | /* Update weight both if it must be raised and if it must be lowered */ | |
7041 | if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1)) | |
7042 | __bfq_entity_update_weight_prio( | |
7043 | bfq_entity_service_tree(entity), | |
7044 | entity); | |
7045 | } | |
7046 | ||
aee69d78 PV |
7047 | /* |
7048 | * Dispatch next request from bfqq. | |
7049 | */ | |
7050 | static struct request *bfq_dispatch_rq_from_bfqq(struct bfq_data *bfqd, | |
7051 | struct bfq_queue *bfqq) | |
7052 | { | |
7053 | struct request *rq = bfqq->next_rq; | |
7054 | unsigned long service_to_charge; | |
7055 | ||
7056 | service_to_charge = bfq_serv_to_charge(rq, bfqq); | |
7057 | ||
7058 | bfq_bfqq_served(bfqq, service_to_charge); | |
7059 | ||
7060 | bfq_dispatch_remove(bfqd->queue, rq); | |
7061 | ||
44e44a1b PV |
7062 | /* |
7063 | * If weight raising has to terminate for bfqq, then next | |
7064 | * function causes an immediate update of bfqq's weight, | |
7065 | * without waiting for next activation. As a consequence, on | |
7066 | * expiration, bfqq will be timestamped as if has never been | |
7067 | * weight-raised during this service slot, even if it has | |
7068 | * received part or even most of the service as a | |
7069 | * weight-raised queue. This inflates bfqq's timestamps, which | |
7070 | * is beneficial, as bfqq is then more willing to leave the | |
7071 | * device immediately to possible other weight-raised queues. | |
7072 | */ | |
7073 | bfq_update_wr_data(bfqd, bfqq); | |
7074 | ||
aee69d78 PV |
7075 | /* |
7076 | * Expire bfqq, pretending that its budget expired, if bfqq | |
7077 | * belongs to CLASS_IDLE and other queues are waiting for | |
7078 | * service. | |
7079 | */ | |
7080 | if (bfqd->busy_queues > 1 && bfq_class_idle(bfqq)) | |
7081 | goto expire; | |
7082 | ||
7083 | return rq; | |
7084 | ||
7085 | expire: | |
7086 | bfq_bfqq_expire(bfqd, bfqq, false, BFQQE_BUDGET_EXHAUSTED); | |
7087 | return rq; | |
7088 | } | |
7089 | ||
7090 | static bool bfq_has_work(struct blk_mq_hw_ctx *hctx) | |
7091 | { | |
7092 | struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; | |
7093 | ||
7094 | /* | |
7095 | * Avoiding lock: a race on bfqd->busy_queues should cause at | |
7096 | * most a call to dispatch for nothing | |
7097 | */ | |
7098 | return !list_empty_careful(&bfqd->dispatch) || | |
7099 | bfqd->busy_queues > 0; | |
7100 | } | |
7101 | ||
7102 | static struct request *__bfq_dispatch_request(struct blk_mq_hw_ctx *hctx) | |
7103 | { | |
7104 | struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; | |
7105 | struct request *rq = NULL; | |
7106 | struct bfq_queue *bfqq = NULL; | |
7107 | ||
7108 | if (!list_empty(&bfqd->dispatch)) { | |
7109 | rq = list_first_entry(&bfqd->dispatch, struct request, | |
7110 | queuelist); | |
7111 | list_del_init(&rq->queuelist); | |
7112 | ||
7113 | bfqq = RQ_BFQQ(rq); | |
7114 | ||
7115 | if (bfqq) { | |
7116 | /* | |
7117 | * Increment counters here, because this | |
7118 | * dispatch does not follow the standard | |
7119 | * dispatch flow (where counters are | |
7120 | * incremented) | |
7121 | */ | |
7122 | bfqq->dispatched++; | |
7123 | ||
7124 | goto inc_in_driver_start_rq; | |
7125 | } | |
7126 | ||
7127 | /* | |
7128 | * We exploit the put_rq_private hook to decrement | |
7129 | * rq_in_driver, but put_rq_private will not be | |
7130 | * invoked on this request. So, to avoid unbalance, | |
7131 | * just start this request, without incrementing | |
7132 | * rq_in_driver. As a negative consequence, | |
7133 | * rq_in_driver is deceptively lower than it should be | |
7134 | * while this request is in service. This may cause | |
7135 | * bfq_schedule_dispatch to be invoked uselessly. | |
7136 | * | |
7137 | * As for implementing an exact solution, the | |
7138 | * put_request hook, if defined, is probably invoked | |
7139 | * also on this request. So, by exploiting this hook, | |
7140 | * we could 1) increment rq_in_driver here, and 2) | |
7141 | * decrement it in put_request. Such a solution would | |
7142 | * let the value of the counter be always accurate, | |
7143 | * but it would entail using an extra interface | |
7144 | * function. This cost seems higher than the benefit, | |
7145 | * being the frequency of non-elevator-private | |
7146 | * requests very low. | |
7147 | */ | |
7148 | goto start_rq; | |
7149 | } | |
7150 | ||
7151 | bfq_log(bfqd, "dispatch requests: %d busy queues", bfqd->busy_queues); | |
7152 | ||
7153 | if (bfqd->busy_queues == 0) | |
7154 | goto exit; | |
7155 | ||
7156 | /* | |
7157 | * Force device to serve one request at a time if | |
7158 | * strict_guarantees is true. Forcing this service scheme is | |
7159 | * currently the ONLY way to guarantee that the request | |
7160 | * service order enforced by the scheduler is respected by a | |
7161 | * queueing device. Otherwise the device is free even to make | |
7162 | * some unlucky request wait for as long as the device | |
7163 | * wishes. | |
7164 | * | |
7165 | * Of course, serving one request at at time may cause loss of | |
7166 | * throughput. | |
7167 | */ | |
7168 | if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0) | |
7169 | goto exit; | |
7170 | ||
7171 | bfqq = bfq_select_queue(bfqd); | |
7172 | if (!bfqq) | |
7173 | goto exit; | |
7174 | ||
7175 | rq = bfq_dispatch_rq_from_bfqq(bfqd, bfqq); | |
7176 | ||
7177 | if (rq) { | |
7178 | inc_in_driver_start_rq: | |
7179 | bfqd->rq_in_driver++; | |
7180 | start_rq: | |
7181 | rq->rq_flags |= RQF_STARTED; | |
7182 | } | |
7183 | exit: | |
7184 | return rq; | |
7185 | } | |
7186 | ||
7187 | static struct request *bfq_dispatch_request(struct blk_mq_hw_ctx *hctx) | |
7188 | { | |
7189 | struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; | |
7190 | struct request *rq; | |
7191 | ||
7192 | spin_lock_irq(&bfqd->lock); | |
36eca894 | 7193 | |
aee69d78 | 7194 | rq = __bfq_dispatch_request(hctx); |
6fa3e8d3 | 7195 | spin_unlock_irq(&bfqd->lock); |
aee69d78 PV |
7196 | |
7197 | return rq; | |
7198 | } | |
7199 | ||
7200 | /* | |
7201 | * Task holds one reference to the queue, dropped when task exits. Each rq | |
7202 | * in-flight on this queue also holds a reference, dropped when rq is freed. | |
7203 | * | |
7204 | * Scheduler lock must be held here. Recall not to use bfqq after calling | |
7205 | * this function on it. | |
7206 | */ | |
7207 | static void bfq_put_queue(struct bfq_queue *bfqq) | |
7208 | { | |
e21b7a0b AA |
7209 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
7210 | struct bfq_group *bfqg = bfqq_group(bfqq); | |
7211 | #endif | |
7212 | ||
aee69d78 PV |
7213 | if (bfqq->bfqd) |
7214 | bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d", | |
7215 | bfqq, bfqq->ref); | |
7216 | ||
7217 | bfqq->ref--; | |
7218 | if (bfqq->ref) | |
7219 | return; | |
7220 | ||
e1b2324d AA |
7221 | if (bfq_bfqq_sync(bfqq)) |
7222 | /* | |
7223 | * The fact that this queue is being destroyed does not | |
7224 | * invalidate the fact that this queue may have been | |
7225 | * activated during the current burst. As a consequence, | |
7226 | * although the queue does not exist anymore, and hence | |
7227 | * needs to be removed from the burst list if there, | |
7228 | * the burst size has not to be decremented. | |
7229 | */ | |
7230 | hlist_del_init(&bfqq->burst_list_node); | |
e21b7a0b | 7231 | |
aee69d78 | 7232 | kmem_cache_free(bfq_pool, bfqq); |
e21b7a0b AA |
7233 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
7234 | bfqg_put(bfqg); | |
7235 | #endif | |
aee69d78 PV |
7236 | } |
7237 | ||
36eca894 AA |
7238 | static void bfq_put_cooperator(struct bfq_queue *bfqq) |
7239 | { | |
7240 | struct bfq_queue *__bfqq, *next; | |
7241 | ||
7242 | /* | |
7243 | * If this queue was scheduled to merge with another queue, be | |
7244 | * sure to drop the reference taken on that queue (and others in | |
7245 | * the merge chain). See bfq_setup_merge and bfq_merge_bfqqs. | |
7246 | */ | |
7247 | __bfqq = bfqq->new_bfqq; | |
7248 | while (__bfqq) { | |
7249 | if (__bfqq == bfqq) | |
7250 | break; | |
7251 | next = __bfqq->new_bfqq; | |
7252 | bfq_put_queue(__bfqq); | |
7253 | __bfqq = next; | |
7254 | } | |
7255 | } | |
7256 | ||
aee69d78 PV |
7257 | static void bfq_exit_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq) |
7258 | { | |
7259 | if (bfqq == bfqd->in_service_queue) { | |
7260 | __bfq_bfqq_expire(bfqd, bfqq); | |
7261 | bfq_schedule_dispatch(bfqd); | |
7262 | } | |
7263 | ||
7264 | bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref); | |
7265 | ||
36eca894 AA |
7266 | bfq_put_cooperator(bfqq); |
7267 | ||
aee69d78 PV |
7268 | bfq_put_queue(bfqq); /* release process reference */ |
7269 | } | |
7270 | ||
7271 | static void bfq_exit_icq_bfqq(struct bfq_io_cq *bic, bool is_sync) | |
7272 | { | |
7273 | struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync); | |
7274 | struct bfq_data *bfqd; | |
7275 | ||
7276 | if (bfqq) | |
7277 | bfqd = bfqq->bfqd; /* NULL if scheduler already exited */ | |
7278 | ||
7279 | if (bfqq && bfqd) { | |
7280 | unsigned long flags; | |
7281 | ||
7282 | spin_lock_irqsave(&bfqd->lock, flags); | |
7283 | bfq_exit_bfqq(bfqd, bfqq); | |
7284 | bic_set_bfqq(bic, NULL, is_sync); | |
6fa3e8d3 | 7285 | spin_unlock_irqrestore(&bfqd->lock, flags); |
aee69d78 PV |
7286 | } |
7287 | } | |
7288 | ||
7289 | static void bfq_exit_icq(struct io_cq *icq) | |
7290 | { | |
7291 | struct bfq_io_cq *bic = icq_to_bic(icq); | |
7292 | ||
7293 | bfq_exit_icq_bfqq(bic, true); | |
7294 | bfq_exit_icq_bfqq(bic, false); | |
7295 | } | |
7296 | ||
7297 | /* | |
7298 | * Update the entity prio values; note that the new values will not | |
7299 | * be used until the next (re)activation. | |
7300 | */ | |
7301 | static void | |
7302 | bfq_set_next_ioprio_data(struct bfq_queue *bfqq, struct bfq_io_cq *bic) | |
7303 | { | |
7304 | struct task_struct *tsk = current; | |
7305 | int ioprio_class; | |
7306 | struct bfq_data *bfqd = bfqq->bfqd; | |
7307 | ||
7308 | if (!bfqd) | |
7309 | return; | |
7310 | ||
7311 | ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); | |
7312 | switch (ioprio_class) { | |
7313 | default: | |
7314 | dev_err(bfqq->bfqd->queue->backing_dev_info->dev, | |
7315 | "bfq: bad prio class %d\n", ioprio_class); | |
7316 | case IOPRIO_CLASS_NONE: | |
7317 | /* | |
7318 | * No prio set, inherit CPU scheduling settings. | |
7319 | */ | |
7320 | bfqq->new_ioprio = task_nice_ioprio(tsk); | |
7321 | bfqq->new_ioprio_class = task_nice_ioclass(tsk); | |
7322 | break; | |
7323 | case IOPRIO_CLASS_RT: | |
7324 | bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); | |
7325 | bfqq->new_ioprio_class = IOPRIO_CLASS_RT; | |
7326 | break; | |
7327 | case IOPRIO_CLASS_BE: | |
7328 | bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); | |
7329 | bfqq->new_ioprio_class = IOPRIO_CLASS_BE; | |
7330 | break; | |
7331 | case IOPRIO_CLASS_IDLE: | |
7332 | bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE; | |
7333 | bfqq->new_ioprio = 7; | |
7334 | bfq_clear_bfqq_idle_window(bfqq); | |
7335 | break; | |
7336 | } | |
7337 | ||
7338 | if (bfqq->new_ioprio >= IOPRIO_BE_NR) { | |
7339 | pr_crit("bfq_set_next_ioprio_data: new_ioprio %d\n", | |
7340 | bfqq->new_ioprio); | |
7341 | bfqq->new_ioprio = IOPRIO_BE_NR; | |
7342 | } | |
7343 | ||
7344 | bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio); | |
7345 | bfqq->entity.prio_changed = 1; | |
7346 | } | |
7347 | ||
7348 | static void bfq_check_ioprio_change(struct bfq_io_cq *bic, struct bio *bio) | |
7349 | { | |
7350 | struct bfq_data *bfqd = bic_to_bfqd(bic); | |
7351 | struct bfq_queue *bfqq; | |
7352 | int ioprio = bic->icq.ioc->ioprio; | |
7353 | ||
7354 | /* | |
7355 | * This condition may trigger on a newly created bic, be sure to | |
7356 | * drop the lock before returning. | |
7357 | */ | |
7358 | if (unlikely(!bfqd) || likely(bic->ioprio == ioprio)) | |
7359 | return; | |
7360 | ||
7361 | bic->ioprio = ioprio; | |
7362 | ||
7363 | bfqq = bic_to_bfqq(bic, false); | |
7364 | if (bfqq) { | |
7365 | /* release process reference on this queue */ | |
7366 | bfq_put_queue(bfqq); | |
7367 | bfqq = bfq_get_queue(bfqd, bio, BLK_RW_ASYNC, bic); | |
7368 | bic_set_bfqq(bic, bfqq, false); | |
7369 | } | |
7370 | ||
7371 | bfqq = bic_to_bfqq(bic, true); | |
7372 | if (bfqq) | |
7373 | bfq_set_next_ioprio_data(bfqq, bic); | |
7374 | } | |
7375 | ||
7376 | static void bfq_init_bfqq(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
7377 | struct bfq_io_cq *bic, pid_t pid, int is_sync) | |
7378 | { | |
7379 | RB_CLEAR_NODE(&bfqq->entity.rb_node); | |
7380 | INIT_LIST_HEAD(&bfqq->fifo); | |
e1b2324d | 7381 | INIT_HLIST_NODE(&bfqq->burst_list_node); |
aee69d78 PV |
7382 | |
7383 | bfqq->ref = 0; | |
7384 | bfqq->bfqd = bfqd; | |
7385 | ||
7386 | if (bic) | |
7387 | bfq_set_next_ioprio_data(bfqq, bic); | |
7388 | ||
7389 | if (is_sync) { | |
7390 | if (!bfq_class_idle(bfqq)) | |
7391 | bfq_mark_bfqq_idle_window(bfqq); | |
7392 | bfq_mark_bfqq_sync(bfqq); | |
e1b2324d | 7393 | bfq_mark_bfqq_just_created(bfqq); |
aee69d78 PV |
7394 | } else |
7395 | bfq_clear_bfqq_sync(bfqq); | |
7396 | ||
7397 | /* set end request to minus infinity from now */ | |
7398 | bfqq->ttime.last_end_request = ktime_get_ns() + 1; | |
7399 | ||
7400 | bfq_mark_bfqq_IO_bound(bfqq); | |
7401 | ||
7402 | bfqq->pid = pid; | |
7403 | ||
7404 | /* Tentative initial value to trade off between thr and lat */ | |
54b60456 | 7405 | bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3; |
aee69d78 | 7406 | bfqq->budget_timeout = bfq_smallest_from_now(); |
aee69d78 | 7407 | |
44e44a1b | 7408 | bfqq->wr_coeff = 1; |
36eca894 | 7409 | bfqq->last_wr_start_finish = jiffies; |
77b7dcea | 7410 | bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now(); |
36eca894 | 7411 | bfqq->split_time = bfq_smallest_from_now(); |
77b7dcea PV |
7412 | |
7413 | /* | |
7414 | * Set to the value for which bfqq will not be deemed as | |
7415 | * soft rt when it becomes backlogged. | |
7416 | */ | |
7417 | bfqq->soft_rt_next_start = bfq_greatest_from_now(); | |
44e44a1b | 7418 | |
aee69d78 PV |
7419 | /* first request is almost certainly seeky */ |
7420 | bfqq->seek_history = 1; | |
7421 | } | |
7422 | ||
7423 | static struct bfq_queue **bfq_async_queue_prio(struct bfq_data *bfqd, | |
e21b7a0b | 7424 | struct bfq_group *bfqg, |
aee69d78 PV |
7425 | int ioprio_class, int ioprio) |
7426 | { | |
7427 | switch (ioprio_class) { | |
7428 | case IOPRIO_CLASS_RT: | |
e21b7a0b | 7429 | return &bfqg->async_bfqq[0][ioprio]; |
aee69d78 PV |
7430 | case IOPRIO_CLASS_NONE: |
7431 | ioprio = IOPRIO_NORM; | |
7432 | /* fall through */ | |
7433 | case IOPRIO_CLASS_BE: | |
e21b7a0b | 7434 | return &bfqg->async_bfqq[1][ioprio]; |
aee69d78 | 7435 | case IOPRIO_CLASS_IDLE: |
e21b7a0b | 7436 | return &bfqg->async_idle_bfqq; |
aee69d78 PV |
7437 | default: |
7438 | return NULL; | |
7439 | } | |
7440 | } | |
7441 | ||
7442 | static struct bfq_queue *bfq_get_queue(struct bfq_data *bfqd, | |
7443 | struct bio *bio, bool is_sync, | |
7444 | struct bfq_io_cq *bic) | |
7445 | { | |
7446 | const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio); | |
7447 | const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); | |
7448 | struct bfq_queue **async_bfqq = NULL; | |
7449 | struct bfq_queue *bfqq; | |
e21b7a0b | 7450 | struct bfq_group *bfqg; |
aee69d78 PV |
7451 | |
7452 | rcu_read_lock(); | |
7453 | ||
e21b7a0b AA |
7454 | bfqg = bfq_find_set_group(bfqd, bio_blkcg(bio)); |
7455 | if (!bfqg) { | |
7456 | bfqq = &bfqd->oom_bfqq; | |
7457 | goto out; | |
7458 | } | |
7459 | ||
aee69d78 | 7460 | if (!is_sync) { |
e21b7a0b | 7461 | async_bfqq = bfq_async_queue_prio(bfqd, bfqg, ioprio_class, |
aee69d78 PV |
7462 | ioprio); |
7463 | bfqq = *async_bfqq; | |
7464 | if (bfqq) | |
7465 | goto out; | |
7466 | } | |
7467 | ||
7468 | bfqq = kmem_cache_alloc_node(bfq_pool, | |
7469 | GFP_NOWAIT | __GFP_ZERO | __GFP_NOWARN, | |
7470 | bfqd->queue->node); | |
7471 | ||
7472 | if (bfqq) { | |
7473 | bfq_init_bfqq(bfqd, bfqq, bic, current->pid, | |
7474 | is_sync); | |
e21b7a0b | 7475 | bfq_init_entity(&bfqq->entity, bfqg); |
aee69d78 PV |
7476 | bfq_log_bfqq(bfqd, bfqq, "allocated"); |
7477 | } else { | |
7478 | bfqq = &bfqd->oom_bfqq; | |
7479 | bfq_log_bfqq(bfqd, bfqq, "using oom bfqq"); | |
7480 | goto out; | |
7481 | } | |
7482 | ||
7483 | /* | |
7484 | * Pin the queue now that it's allocated, scheduler exit will | |
7485 | * prune it. | |
7486 | */ | |
7487 | if (async_bfqq) { | |
e21b7a0b AA |
7488 | bfqq->ref++; /* |
7489 | * Extra group reference, w.r.t. sync | |
7490 | * queue. This extra reference is removed | |
7491 | * only if bfqq->bfqg disappears, to | |
7492 | * guarantee that this queue is not freed | |
7493 | * until its group goes away. | |
7494 | */ | |
7495 | bfq_log_bfqq(bfqd, bfqq, "get_queue, bfqq not in async: %p, %d", | |
aee69d78 PV |
7496 | bfqq, bfqq->ref); |
7497 | *async_bfqq = bfqq; | |
7498 | } | |
7499 | ||
7500 | out: | |
7501 | bfqq->ref++; /* get a process reference to this queue */ | |
7502 | bfq_log_bfqq(bfqd, bfqq, "get_queue, at end: %p, %d", bfqq, bfqq->ref); | |
7503 | rcu_read_unlock(); | |
7504 | return bfqq; | |
7505 | } | |
7506 | ||
7507 | static void bfq_update_io_thinktime(struct bfq_data *bfqd, | |
7508 | struct bfq_queue *bfqq) | |
7509 | { | |
7510 | struct bfq_ttime *ttime = &bfqq->ttime; | |
7511 | u64 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request; | |
7512 | ||
7513 | elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle); | |
7514 | ||
7515 | ttime->ttime_samples = (7*bfqq->ttime.ttime_samples + 256) / 8; | |
7516 | ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8); | |
7517 | ttime->ttime_mean = div64_ul(ttime->ttime_total + 128, | |
7518 | ttime->ttime_samples); | |
7519 | } | |
7520 | ||
7521 | static void | |
7522 | bfq_update_io_seektime(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
7523 | struct request *rq) | |
7524 | { | |
aee69d78 | 7525 | bfqq->seek_history <<= 1; |
ab0e43e9 PV |
7526 | bfqq->seek_history |= |
7527 | get_sdist(bfqq->last_request_pos, rq) > BFQQ_SEEK_THR && | |
aee69d78 PV |
7528 | (!blk_queue_nonrot(bfqd->queue) || |
7529 | blk_rq_sectors(rq) < BFQQ_SECT_THR_NONROT); | |
7530 | } | |
7531 | ||
7532 | /* | |
7533 | * Disable idle window if the process thinks too long or seeks so much that | |
7534 | * it doesn't matter. | |
7535 | */ | |
7536 | static void bfq_update_idle_window(struct bfq_data *bfqd, | |
7537 | struct bfq_queue *bfqq, | |
7538 | struct bfq_io_cq *bic) | |
7539 | { | |
7540 | int enable_idle; | |
7541 | ||
7542 | /* Don't idle for async or idle io prio class. */ | |
7543 | if (!bfq_bfqq_sync(bfqq) || bfq_class_idle(bfqq)) | |
7544 | return; | |
7545 | ||
36eca894 AA |
7546 | /* Idle window just restored, statistics are meaningless. */ |
7547 | if (time_is_after_eq_jiffies(bfqq->split_time + | |
7548 | bfqd->bfq_wr_min_idle_time)) | |
7549 | return; | |
7550 | ||
aee69d78 PV |
7551 | enable_idle = bfq_bfqq_idle_window(bfqq); |
7552 | ||
7553 | if (atomic_read(&bic->icq.ioc->active_ref) == 0 || | |
7554 | bfqd->bfq_slice_idle == 0 || | |
bcd56426 PV |
7555 | (bfqd->hw_tag && BFQQ_SEEKY(bfqq) && |
7556 | bfqq->wr_coeff == 1)) | |
aee69d78 PV |
7557 | enable_idle = 0; |
7558 | else if (bfq_sample_valid(bfqq->ttime.ttime_samples)) { | |
44e44a1b PV |
7559 | if (bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle && |
7560 | bfqq->wr_coeff == 1) | |
aee69d78 PV |
7561 | enable_idle = 0; |
7562 | else | |
7563 | enable_idle = 1; | |
7564 | } | |
7565 | bfq_log_bfqq(bfqd, bfqq, "update_idle_window: enable_idle %d", | |
7566 | enable_idle); | |
7567 | ||
7568 | if (enable_idle) | |
7569 | bfq_mark_bfqq_idle_window(bfqq); | |
7570 | else | |
7571 | bfq_clear_bfqq_idle_window(bfqq); | |
7572 | } | |
7573 | ||
7574 | /* | |
7575 | * Called when a new fs request (rq) is added to bfqq. Check if there's | |
7576 | * something we should do about it. | |
7577 | */ | |
7578 | static void bfq_rq_enqueued(struct bfq_data *bfqd, struct bfq_queue *bfqq, | |
7579 | struct request *rq) | |
7580 | { | |
7581 | struct bfq_io_cq *bic = RQ_BIC(rq); | |
7582 | ||
7583 | if (rq->cmd_flags & REQ_META) | |
7584 | bfqq->meta_pending++; | |
7585 | ||
7586 | bfq_update_io_thinktime(bfqd, bfqq); | |
7587 | bfq_update_io_seektime(bfqd, bfqq, rq); | |
7588 | if (bfqq->entity.service > bfq_max_budget(bfqd) / 8 || | |
7589 | !BFQQ_SEEKY(bfqq)) | |
7590 | bfq_update_idle_window(bfqd, bfqq, bic); | |
7591 | ||
7592 | bfq_log_bfqq(bfqd, bfqq, | |
7593 | "rq_enqueued: idle_window=%d (seeky %d)", | |
7594 | bfq_bfqq_idle_window(bfqq), BFQQ_SEEKY(bfqq)); | |
7595 | ||
7596 | bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); | |
7597 | ||
7598 | if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) { | |
7599 | bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 && | |
7600 | blk_rq_sectors(rq) < 32; | |
7601 | bool budget_timeout = bfq_bfqq_budget_timeout(bfqq); | |
7602 | ||
7603 | /* | |
7604 | * There is just this request queued: if the request | |
7605 | * is small and the queue is not to be expired, then | |
7606 | * just exit. | |
7607 | * | |
7608 | * In this way, if the device is being idled to wait | |
7609 | * for a new request from the in-service queue, we | |
7610 | * avoid unplugging the device and committing the | |
7611 | * device to serve just a small request. On the | |
7612 | * contrary, we wait for the block layer to decide | |
7613 | * when to unplug the device: hopefully, new requests | |
7614 | * will be merged to this one quickly, then the device | |
7615 | * will be unplugged and larger requests will be | |
7616 | * dispatched. | |
7617 | */ | |
7618 | if (small_req && !budget_timeout) | |
7619 | return; | |
7620 | ||
7621 | /* | |
7622 | * A large enough request arrived, or the queue is to | |
7623 | * be expired: in both cases disk idling is to be | |
7624 | * stopped, so clear wait_request flag and reset | |
7625 | * timer. | |
7626 | */ | |
7627 | bfq_clear_bfqq_wait_request(bfqq); | |
7628 | hrtimer_try_to_cancel(&bfqd->idle_slice_timer); | |
e21b7a0b | 7629 | bfqg_stats_update_idle_time(bfqq_group(bfqq)); |
aee69d78 PV |
7630 | |
7631 | /* | |
7632 | * The queue is not empty, because a new request just | |
7633 | * arrived. Hence we can safely expire the queue, in | |
7634 | * case of budget timeout, without risking that the | |
7635 | * timestamps of the queue are not updated correctly. | |
7636 | * See [1] for more details. | |
7637 | */ | |
7638 | if (budget_timeout) | |
7639 | bfq_bfqq_expire(bfqd, bfqq, false, | |
7640 | BFQQE_BUDGET_TIMEOUT); | |
7641 | } | |
7642 | } | |
7643 | ||
7644 | static void __bfq_insert_request(struct bfq_data *bfqd, struct request *rq) | |
7645 | { | |
36eca894 AA |
7646 | struct bfq_queue *bfqq = RQ_BFQQ(rq), |
7647 | *new_bfqq = bfq_setup_cooperator(bfqd, bfqq, rq, true); | |
7648 | ||
7649 | if (new_bfqq) { | |
7650 | if (bic_to_bfqq(RQ_BIC(rq), 1) != bfqq) | |
7651 | new_bfqq = bic_to_bfqq(RQ_BIC(rq), 1); | |
7652 | /* | |
7653 | * Release the request's reference to the old bfqq | |
7654 | * and make sure one is taken to the shared queue. | |
7655 | */ | |
7656 | new_bfqq->allocated++; | |
7657 | bfqq->allocated--; | |
7658 | new_bfqq->ref++; | |
e1b2324d | 7659 | bfq_clear_bfqq_just_created(bfqq); |
36eca894 AA |
7660 | /* |
7661 | * If the bic associated with the process | |
7662 | * issuing this request still points to bfqq | |
7663 | * (and thus has not been already redirected | |
7664 | * to new_bfqq or even some other bfq_queue), | |
7665 | * then complete the merge and redirect it to | |
7666 | * new_bfqq. | |
7667 | */ | |
7668 | if (bic_to_bfqq(RQ_BIC(rq), 1) == bfqq) | |
7669 | bfq_merge_bfqqs(bfqd, RQ_BIC(rq), | |
7670 | bfqq, new_bfqq); | |
7671 | /* | |
7672 | * rq is about to be enqueued into new_bfqq, | |
7673 | * release rq reference on bfqq | |
7674 | */ | |
7675 | bfq_put_queue(bfqq); | |
7676 | rq->elv.priv[1] = new_bfqq; | |
7677 | bfqq = new_bfqq; | |
7678 | } | |
aee69d78 PV |
7679 | |
7680 | bfq_add_request(rq); | |
7681 | ||
7682 | rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)]; | |
7683 | list_add_tail(&rq->queuelist, &bfqq->fifo); | |
7684 | ||
7685 | bfq_rq_enqueued(bfqd, bfqq, rq); | |
7686 | } | |
7687 | ||
7688 | static void bfq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq, | |
7689 | bool at_head) | |
7690 | { | |
7691 | struct request_queue *q = hctx->queue; | |
7692 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
7693 | ||
7694 | spin_lock_irq(&bfqd->lock); | |
7695 | if (blk_mq_sched_try_insert_merge(q, rq)) { | |
7696 | spin_unlock_irq(&bfqd->lock); | |
7697 | return; | |
7698 | } | |
7699 | ||
7700 | spin_unlock_irq(&bfqd->lock); | |
7701 | ||
7702 | blk_mq_sched_request_inserted(rq); | |
7703 | ||
7704 | spin_lock_irq(&bfqd->lock); | |
7705 | if (at_head || blk_rq_is_passthrough(rq)) { | |
7706 | if (at_head) | |
7707 | list_add(&rq->queuelist, &bfqd->dispatch); | |
7708 | else | |
7709 | list_add_tail(&rq->queuelist, &bfqd->dispatch); | |
7710 | } else { | |
7711 | __bfq_insert_request(bfqd, rq); | |
7712 | ||
7713 | if (rq_mergeable(rq)) { | |
7714 | elv_rqhash_add(q, rq); | |
7715 | if (!q->last_merge) | |
7716 | q->last_merge = rq; | |
7717 | } | |
7718 | } | |
7719 | ||
6fa3e8d3 | 7720 | spin_unlock_irq(&bfqd->lock); |
aee69d78 PV |
7721 | } |
7722 | ||
7723 | static void bfq_insert_requests(struct blk_mq_hw_ctx *hctx, | |
7724 | struct list_head *list, bool at_head) | |
7725 | { | |
7726 | while (!list_empty(list)) { | |
7727 | struct request *rq; | |
7728 | ||
7729 | rq = list_first_entry(list, struct request, queuelist); | |
7730 | list_del_init(&rq->queuelist); | |
7731 | bfq_insert_request(hctx, rq, at_head); | |
7732 | } | |
7733 | } | |
7734 | ||
7735 | static void bfq_update_hw_tag(struct bfq_data *bfqd) | |
7736 | { | |
7737 | bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver, | |
7738 | bfqd->rq_in_driver); | |
7739 | ||
7740 | if (bfqd->hw_tag == 1) | |
7741 | return; | |
7742 | ||
7743 | /* | |
7744 | * This sample is valid if the number of outstanding requests | |
7745 | * is large enough to allow a queueing behavior. Note that the | |
7746 | * sum is not exact, as it's not taking into account deactivated | |
7747 | * requests. | |
7748 | */ | |
7749 | if (bfqd->rq_in_driver + bfqd->queued < BFQ_HW_QUEUE_THRESHOLD) | |
7750 | return; | |
7751 | ||
7752 | if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES) | |
7753 | return; | |
7754 | ||
7755 | bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD; | |
7756 | bfqd->max_rq_in_driver = 0; | |
7757 | bfqd->hw_tag_samples = 0; | |
7758 | } | |
7759 | ||
7760 | static void bfq_completed_request(struct bfq_queue *bfqq, struct bfq_data *bfqd) | |
7761 | { | |
ab0e43e9 PV |
7762 | u64 now_ns; |
7763 | u32 delta_us; | |
7764 | ||
aee69d78 PV |
7765 | bfq_update_hw_tag(bfqd); |
7766 | ||
7767 | bfqd->rq_in_driver--; | |
7768 | bfqq->dispatched--; | |
7769 | ||
44e44a1b PV |
7770 | if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) { |
7771 | /* | |
7772 | * Set budget_timeout (which we overload to store the | |
7773 | * time at which the queue remains with no backlog and | |
7774 | * no outstanding request; used by the weight-raising | |
7775 | * mechanism). | |
7776 | */ | |
7777 | bfqq->budget_timeout = jiffies; | |
1de0c4cd AA |
7778 | |
7779 | bfq_weights_tree_remove(bfqd, &bfqq->entity, | |
7780 | &bfqd->queue_weights_tree); | |
44e44a1b PV |
7781 | } |
7782 | ||
ab0e43e9 PV |
7783 | now_ns = ktime_get_ns(); |
7784 | ||
7785 | bfqq->ttime.last_end_request = now_ns; | |
7786 | ||
7787 | /* | |
7788 | * Using us instead of ns, to get a reasonable precision in | |
7789 | * computing rate in next check. | |
7790 | */ | |
7791 | delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC); | |
7792 | ||
7793 | /* | |
7794 | * If the request took rather long to complete, and, according | |
7795 | * to the maximum request size recorded, this completion latency | |
7796 | * implies that the request was certainly served at a very low | |
7797 | * rate (less than 1M sectors/sec), then the whole observation | |
7798 | * interval that lasts up to this time instant cannot be a | |
7799 | * valid time interval for computing a new peak rate. Invoke | |
7800 | * bfq_update_rate_reset to have the following three steps | |
7801 | * taken: | |
7802 | * - close the observation interval at the last (previous) | |
7803 | * request dispatch or completion | |
7804 | * - compute rate, if possible, for that observation interval | |
7805 | * - reset to zero samples, which will trigger a proper | |
7806 | * re-initialization of the observation interval on next | |
7807 | * dispatch | |
7808 | */ | |
7809 | if (delta_us > BFQ_MIN_TT/NSEC_PER_USEC && | |
7810 | (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us < | |
7811 | 1UL<<(BFQ_RATE_SHIFT - 10)) | |
7812 | bfq_update_rate_reset(bfqd, NULL); | |
7813 | bfqd->last_completion = now_ns; | |
aee69d78 | 7814 | |
77b7dcea PV |
7815 | /* |
7816 | * If we are waiting to discover whether the request pattern | |
7817 | * of the task associated with the queue is actually | |
7818 | * isochronous, and both requisites for this condition to hold | |
7819 | * are now satisfied, then compute soft_rt_next_start (see the | |
7820 | * comments on the function bfq_bfqq_softrt_next_start()). We | |
7821 | * schedule this delayed check when bfqq expires, if it still | |
7822 | * has in-flight requests. | |
7823 | */ | |
7824 | if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 && | |
7825 | RB_EMPTY_ROOT(&bfqq->sort_list)) | |
7826 | bfqq->soft_rt_next_start = | |
7827 | bfq_bfqq_softrt_next_start(bfqd, bfqq); | |
7828 | ||
aee69d78 PV |
7829 | /* |
7830 | * If this is the in-service queue, check if it needs to be expired, | |
7831 | * or if we want to idle in case it has no pending requests. | |
7832 | */ | |
7833 | if (bfqd->in_service_queue == bfqq) { | |
44e44a1b | 7834 | if (bfqq->dispatched == 0 && bfq_bfqq_must_idle(bfqq)) { |
aee69d78 PV |
7835 | bfq_arm_slice_timer(bfqd); |
7836 | return; | |
7837 | } else if (bfq_may_expire_for_budg_timeout(bfqq)) | |
7838 | bfq_bfqq_expire(bfqd, bfqq, false, | |
7839 | BFQQE_BUDGET_TIMEOUT); | |
7840 | else if (RB_EMPTY_ROOT(&bfqq->sort_list) && | |
7841 | (bfqq->dispatched == 0 || | |
7842 | !bfq_bfqq_may_idle(bfqq))) | |
7843 | bfq_bfqq_expire(bfqd, bfqq, false, | |
7844 | BFQQE_NO_MORE_REQUESTS); | |
7845 | } | |
7846 | } | |
7847 | ||
7848 | static void bfq_put_rq_priv_body(struct bfq_queue *bfqq) | |
7849 | { | |
7850 | bfqq->allocated--; | |
7851 | ||
7852 | bfq_put_queue(bfqq); | |
7853 | } | |
7854 | ||
7855 | static void bfq_put_rq_private(struct request_queue *q, struct request *rq) | |
7856 | { | |
7857 | struct bfq_queue *bfqq = RQ_BFQQ(rq); | |
7858 | struct bfq_data *bfqd = bfqq->bfqd; | |
7859 | ||
e21b7a0b AA |
7860 | if (rq->rq_flags & RQF_STARTED) |
7861 | bfqg_stats_update_completion(bfqq_group(bfqq), | |
7862 | rq_start_time_ns(rq), | |
7863 | rq_io_start_time_ns(rq), | |
7864 | rq->cmd_flags); | |
aee69d78 PV |
7865 | |
7866 | if (likely(rq->rq_flags & RQF_STARTED)) { | |
7867 | unsigned long flags; | |
7868 | ||
7869 | spin_lock_irqsave(&bfqd->lock, flags); | |
7870 | ||
7871 | bfq_completed_request(bfqq, bfqd); | |
7872 | bfq_put_rq_priv_body(bfqq); | |
7873 | ||
6fa3e8d3 | 7874 | spin_unlock_irqrestore(&bfqd->lock, flags); |
aee69d78 PV |
7875 | } else { |
7876 | /* | |
7877 | * Request rq may be still/already in the scheduler, | |
7878 | * in which case we need to remove it. And we cannot | |
7879 | * defer such a check and removal, to avoid | |
7880 | * inconsistencies in the time interval from the end | |
7881 | * of this function to the start of the deferred work. | |
7882 | * This situation seems to occur only in process | |
7883 | * context, as a consequence of a merge. In the | |
7884 | * current version of the code, this implies that the | |
7885 | * lock is held. | |
7886 | */ | |
7887 | ||
7888 | if (!RB_EMPTY_NODE(&rq->rb_node)) | |
7889 | bfq_remove_request(q, rq); | |
7890 | bfq_put_rq_priv_body(bfqq); | |
7891 | } | |
7892 | ||
7893 | rq->elv.priv[0] = NULL; | |
7894 | rq->elv.priv[1] = NULL; | |
7895 | } | |
7896 | ||
36eca894 AA |
7897 | /* |
7898 | * Returns NULL if a new bfqq should be allocated, or the old bfqq if this | |
7899 | * was the last process referring to that bfqq. | |
7900 | */ | |
7901 | static struct bfq_queue * | |
7902 | bfq_split_bfqq(struct bfq_io_cq *bic, struct bfq_queue *bfqq) | |
7903 | { | |
7904 | bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue"); | |
7905 | ||
7906 | if (bfqq_process_refs(bfqq) == 1) { | |
7907 | bfqq->pid = current->pid; | |
7908 | bfq_clear_bfqq_coop(bfqq); | |
7909 | bfq_clear_bfqq_split_coop(bfqq); | |
7910 | return bfqq; | |
7911 | } | |
7912 | ||
7913 | bic_set_bfqq(bic, NULL, 1); | |
7914 | ||
7915 | bfq_put_cooperator(bfqq); | |
7916 | ||
7917 | bfq_put_queue(bfqq); | |
7918 | return NULL; | |
7919 | } | |
7920 | ||
7921 | static struct bfq_queue *bfq_get_bfqq_handle_split(struct bfq_data *bfqd, | |
7922 | struct bfq_io_cq *bic, | |
7923 | struct bio *bio, | |
7924 | bool split, bool is_sync, | |
7925 | bool *new_queue) | |
7926 | { | |
7927 | struct bfq_queue *bfqq = bic_to_bfqq(bic, is_sync); | |
7928 | ||
7929 | if (likely(bfqq && bfqq != &bfqd->oom_bfqq)) | |
7930 | return bfqq; | |
7931 | ||
7932 | if (new_queue) | |
7933 | *new_queue = true; | |
7934 | ||
7935 | if (bfqq) | |
7936 | bfq_put_queue(bfqq); | |
7937 | bfqq = bfq_get_queue(bfqd, bio, is_sync, bic); | |
7938 | ||
7939 | bic_set_bfqq(bic, bfqq, is_sync); | |
e1b2324d AA |
7940 | if (split && is_sync) { |
7941 | if ((bic->was_in_burst_list && bfqd->large_burst) || | |
7942 | bic->saved_in_large_burst) | |
7943 | bfq_mark_bfqq_in_large_burst(bfqq); | |
7944 | else { | |
7945 | bfq_clear_bfqq_in_large_burst(bfqq); | |
7946 | if (bic->was_in_burst_list) | |
7947 | hlist_add_head(&bfqq->burst_list_node, | |
7948 | &bfqd->burst_list); | |
7949 | } | |
36eca894 | 7950 | bfqq->split_time = jiffies; |
e1b2324d | 7951 | } |
36eca894 AA |
7952 | |
7953 | return bfqq; | |
7954 | } | |
7955 | ||
aee69d78 PV |
7956 | /* |
7957 | * Allocate bfq data structures associated with this request. | |
7958 | */ | |
7959 | static int bfq_get_rq_private(struct request_queue *q, struct request *rq, | |
7960 | struct bio *bio) | |
7961 | { | |
7962 | struct bfq_data *bfqd = q->elevator->elevator_data; | |
7963 | struct bfq_io_cq *bic = icq_to_bic(rq->elv.icq); | |
7964 | const int is_sync = rq_is_sync(rq); | |
7965 | struct bfq_queue *bfqq; | |
36eca894 | 7966 | bool new_queue = false; |
6fa3e8d3 | 7967 | bool split = false; |
aee69d78 PV |
7968 | |
7969 | spin_lock_irq(&bfqd->lock); | |
7970 | ||
7971 | bfq_check_ioprio_change(bic, bio); | |
7972 | ||
7973 | if (!bic) | |
7974 | goto queue_fail; | |
7975 | ||
e21b7a0b AA |
7976 | bfq_bic_update_cgroup(bic, bio); |
7977 | ||
36eca894 AA |
7978 | bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, false, is_sync, |
7979 | &new_queue); | |
7980 | ||
7981 | if (likely(!new_queue)) { | |
7982 | /* If the queue was seeky for too long, break it apart. */ | |
7983 | if (bfq_bfqq_coop(bfqq) && bfq_bfqq_split_coop(bfqq)) { | |
7984 | bfq_log_bfqq(bfqd, bfqq, "breaking apart bfqq"); | |
e1b2324d AA |
7985 | |
7986 | /* Update bic before losing reference to bfqq */ | |
7987 | if (bfq_bfqq_in_large_burst(bfqq)) | |
7988 | bic->saved_in_large_burst = true; | |
7989 | ||
36eca894 | 7990 | bfqq = bfq_split_bfqq(bic, bfqq); |
6fa3e8d3 | 7991 | split = true; |
36eca894 AA |
7992 | |
7993 | if (!bfqq) | |
7994 | bfqq = bfq_get_bfqq_handle_split(bfqd, bic, bio, | |
7995 | true, is_sync, | |
7996 | NULL); | |
7997 | } | |
aee69d78 PV |
7998 | } |
7999 | ||
8000 | bfqq->allocated++; | |
8001 | bfqq->ref++; | |
8002 | bfq_log_bfqq(bfqd, bfqq, "get_request %p: bfqq %p, %d", | |
8003 | rq, bfqq, bfqq->ref); | |
8004 | ||
8005 | rq->elv.priv[0] = bic; | |
8006 | rq->elv.priv[1] = bfqq; | |
8007 | ||
36eca894 AA |
8008 | /* |
8009 | * If a bfq_queue has only one process reference, it is owned | |
8010 | * by only this bic: we can then set bfqq->bic = bic. in | |
8011 | * addition, if the queue has also just been split, we have to | |
8012 | * resume its state. | |
8013 | */ | |
8014 | if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) { | |
8015 | bfqq->bic = bic; | |
6fa3e8d3 | 8016 | if (split) { |
36eca894 AA |
8017 | /* |
8018 | * The queue has just been split from a shared | |
8019 | * queue: restore the idle window and the | |
8020 | * possible weight raising period. | |
8021 | */ | |
8022 | bfq_bfqq_resume_state(bfqq, bic); | |
8023 | } | |
8024 | } | |
8025 | ||
e1b2324d AA |
8026 | if (unlikely(bfq_bfqq_just_created(bfqq))) |
8027 | bfq_handle_burst(bfqd, bfqq); | |
8028 | ||
6fa3e8d3 | 8029 | spin_unlock_irq(&bfqd->lock); |
aee69d78 PV |
8030 | |
8031 | return 0; | |
8032 | ||
8033 | queue_fail: | |
8034 | spin_unlock_irq(&bfqd->lock); | |
8035 | ||
8036 | return 1; | |
8037 | } | |
8038 | ||
8039 | static void bfq_idle_slice_timer_body(struct bfq_queue *bfqq) | |
8040 | { | |
8041 | struct bfq_data *bfqd = bfqq->bfqd; | |
8042 | enum bfqq_expiration reason; | |
8043 | unsigned long flags; | |
8044 | ||
8045 | spin_lock_irqsave(&bfqd->lock, flags); | |
8046 | bfq_clear_bfqq_wait_request(bfqq); | |
8047 | ||
8048 | if (bfqq != bfqd->in_service_queue) { | |
8049 | spin_unlock_irqrestore(&bfqd->lock, flags); | |
8050 | return; | |
8051 | } | |
8052 | ||
8053 | if (bfq_bfqq_budget_timeout(bfqq)) | |
8054 | /* | |
8055 | * Also here the queue can be safely expired | |
8056 | * for budget timeout without wasting | |
8057 | * guarantees | |
8058 | */ | |
8059 | reason = BFQQE_BUDGET_TIMEOUT; | |
8060 | else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0) | |
8061 | /* | |
8062 | * The queue may not be empty upon timer expiration, | |
8063 | * because we may not disable the timer when the | |
8064 | * first request of the in-service queue arrives | |
8065 | * during disk idling. | |
8066 | */ | |
8067 | reason = BFQQE_TOO_IDLE; | |
8068 | else | |
8069 | goto schedule_dispatch; | |
8070 | ||
8071 | bfq_bfqq_expire(bfqd, bfqq, true, reason); | |
8072 | ||
8073 | schedule_dispatch: | |
6fa3e8d3 | 8074 | spin_unlock_irqrestore(&bfqd->lock, flags); |
aee69d78 PV |
8075 | bfq_schedule_dispatch(bfqd); |
8076 | } | |
8077 | ||
8078 | /* | |
8079 | * Handler of the expiration of the timer running if the in-service queue | |
8080 | * is idling inside its time slice. | |
8081 | */ | |
8082 | static enum hrtimer_restart bfq_idle_slice_timer(struct hrtimer *timer) | |
8083 | { | |
8084 | struct bfq_data *bfqd = container_of(timer, struct bfq_data, | |
8085 | idle_slice_timer); | |
8086 | struct bfq_queue *bfqq = bfqd->in_service_queue; | |
8087 | ||
8088 | /* | |
8089 | * Theoretical race here: the in-service queue can be NULL or | |
8090 | * different from the queue that was idling if a new request | |
8091 | * arrives for the current queue and there is a full dispatch | |
8092 | * cycle that changes the in-service queue. This can hardly | |
8093 | * happen, but in the worst case we just expire a queue too | |
8094 | * early. | |
8095 | */ | |
8096 | if (bfqq) | |
8097 | bfq_idle_slice_timer_body(bfqq); | |
8098 | ||
8099 | return HRTIMER_NORESTART; | |
8100 | } | |
8101 | ||
8102 | static void __bfq_put_async_bfqq(struct bfq_data *bfqd, | |
8103 | struct bfq_queue **bfqq_ptr) | |
8104 | { | |
8105 | struct bfq_queue *bfqq = *bfqq_ptr; | |
8106 | ||
8107 | bfq_log(bfqd, "put_async_bfqq: %p", bfqq); | |
8108 | if (bfqq) { | |
e21b7a0b AA |
8109 | bfq_bfqq_move(bfqd, bfqq, bfqd->root_group); |
8110 | ||
aee69d78 PV |
8111 | bfq_log_bfqq(bfqd, bfqq, "put_async_bfqq: putting %p, %d", |
8112 | bfqq, bfqq->ref); | |
8113 | bfq_put_queue(bfqq); | |
8114 | *bfqq_ptr = NULL; | |
8115 | } | |
8116 | } | |
8117 | ||
8118 | /* | |
e21b7a0b AA |
8119 | * Release all the bfqg references to its async queues. If we are |
8120 | * deallocating the group these queues may still contain requests, so | |
8121 | * we reparent them to the root cgroup (i.e., the only one that will | |
8122 | * exist for sure until all the requests on a device are gone). | |
aee69d78 | 8123 | */ |
e21b7a0b | 8124 | static void bfq_put_async_queues(struct bfq_data *bfqd, struct bfq_group *bfqg) |
aee69d78 PV |
8125 | { |
8126 | int i, j; | |
8127 | ||
8128 | for (i = 0; i < 2; i++) | |
8129 | for (j = 0; j < IOPRIO_BE_NR; j++) | |
e21b7a0b | 8130 | __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]); |
aee69d78 | 8131 | |
e21b7a0b | 8132 | __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq); |
aee69d78 PV |
8133 | } |
8134 | ||
8135 | static void bfq_exit_queue(struct elevator_queue *e) | |
8136 | { | |
8137 | struct bfq_data *bfqd = e->elevator_data; | |
8138 | struct bfq_queue *bfqq, *n; | |
8139 | ||
8140 | hrtimer_cancel(&bfqd->idle_slice_timer); | |
8141 | ||
8142 | spin_lock_irq(&bfqd->lock); | |
8143 | list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list) | |
e21b7a0b | 8144 | bfq_deactivate_bfqq(bfqd, bfqq, false, false); |
aee69d78 PV |
8145 | spin_unlock_irq(&bfqd->lock); |
8146 | ||
8147 | hrtimer_cancel(&bfqd->idle_slice_timer); | |
8148 | ||
e21b7a0b AA |
8149 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
8150 | blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq); | |
8151 | #else | |
8152 | spin_lock_irq(&bfqd->lock); | |
8153 | bfq_put_async_queues(bfqd, bfqd->root_group); | |
8154 | kfree(bfqd->root_group); | |
8155 | spin_unlock_irq(&bfqd->lock); | |
8156 | #endif | |
8157 | ||
aee69d78 PV |
8158 | kfree(bfqd); |
8159 | } | |
8160 | ||
e21b7a0b AA |
8161 | static void bfq_init_root_group(struct bfq_group *root_group, |
8162 | struct bfq_data *bfqd) | |
8163 | { | |
8164 | int i; | |
8165 | ||
8166 | #ifdef CONFIG_BFQ_GROUP_IOSCHED | |
8167 | root_group->entity.parent = NULL; | |
8168 | root_group->my_entity = NULL; | |
8169 | root_group->bfqd = bfqd; | |
8170 | #endif | |
36eca894 | 8171 | root_group->rq_pos_tree = RB_ROOT; |
e21b7a0b AA |
8172 | for (i = 0; i < BFQ_IOPRIO_CLASSES; i++) |
8173 | root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT; | |
8174 | root_group->sched_data.bfq_class_idle_last_service = jiffies; | |
8175 | } | |
8176 | ||
aee69d78 PV |
8177 | static int bfq_init_queue(struct request_queue *q, struct elevator_type *e) |
8178 | { | |
8179 | struct bfq_data *bfqd; | |
8180 | struct elevator_queue *eq; | |
aee69d78 PV |
8181 | |
8182 | eq = elevator_alloc(q, e); | |
8183 | if (!eq) | |
8184 | return -ENOMEM; | |
8185 | ||
8186 | bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node); | |
8187 | if (!bfqd) { | |
8188 | kobject_put(&eq->kobj); | |
8189 | return -ENOMEM; | |
8190 | } | |
8191 | eq->elevator_data = bfqd; | |
8192 | ||
e21b7a0b AA |
8193 | spin_lock_irq(q->queue_lock); |
8194 | q->elevator = eq; | |
8195 | spin_unlock_irq(q->queue_lock); | |
8196 | ||
aee69d78 PV |
8197 | /* |
8198 | * Our fallback bfqq if bfq_find_alloc_queue() runs into OOM issues. | |
8199 | * Grab a permanent reference to it, so that the normal code flow | |
8200 | * will not attempt to free it. | |
8201 | */ | |
8202 | bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0); | |
8203 | bfqd->oom_bfqq.ref++; | |
8204 | bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO; | |
8205 | bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE; | |
8206 | bfqd->oom_bfqq.entity.new_weight = | |
8207 | bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio); | |
e1b2324d AA |
8208 | |
8209 | /* oom_bfqq does not participate to bursts */ | |
8210 | bfq_clear_bfqq_just_created(&bfqd->oom_bfqq); | |
8211 | ||
aee69d78 PV |
8212 | /* |
8213 | * Trigger weight initialization, according to ioprio, at the | |
8214 | * oom_bfqq's first activation. The oom_bfqq's ioprio and ioprio | |
8215 | * class won't be changed any more. | |
8216 | */ | |
8217 | bfqd->oom_bfqq.entity.prio_changed = 1; | |
8218 | ||
8219 | bfqd->queue = q; | |
8220 | ||
e21b7a0b | 8221 | INIT_LIST_HEAD(&bfqd->dispatch); |
aee69d78 PV |
8222 | |
8223 | hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC, | |
8224 | HRTIMER_MODE_REL); | |
8225 | bfqd->idle_slice_timer.function = bfq_idle_slice_timer; | |
8226 | ||
1de0c4cd AA |
8227 | bfqd->queue_weights_tree = RB_ROOT; |
8228 | bfqd->group_weights_tree = RB_ROOT; | |
8229 | ||
aee69d78 PV |
8230 | INIT_LIST_HEAD(&bfqd->active_list); |
8231 | INIT_LIST_HEAD(&bfqd->idle_list); | |
e1b2324d | 8232 | INIT_HLIST_HEAD(&bfqd->burst_list); |
aee69d78 PV |
8233 | |
8234 | bfqd->hw_tag = -1; | |
8235 | ||
8236 | bfqd->bfq_max_budget = bfq_default_max_budget; | |
8237 | ||
8238 | bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0]; | |
8239 | bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1]; | |
8240 | bfqd->bfq_back_max = bfq_back_max; | |
8241 | bfqd->bfq_back_penalty = bfq_back_penalty; | |
8242 | bfqd->bfq_slice_idle = bfq_slice_idle; | |
aee69d78 PV |
8243 | bfqd->bfq_timeout = bfq_timeout; |
8244 | ||
8245 | bfqd->bfq_requests_within_timer = 120; | |
8246 | ||
e1b2324d AA |
8247 | bfqd->bfq_large_burst_thresh = 8; |
8248 | bfqd->bfq_burst_interval = msecs_to_jiffies(180); | |
8249 | ||
44e44a1b PV |
8250 | bfqd->low_latency = true; |
8251 | ||
8252 | /* | |
8253 | * Trade-off between responsiveness and fairness. | |
8254 | */ | |
8255 | bfqd->bfq_wr_coeff = 30; | |
77b7dcea | 8256 | bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300); |
44e44a1b PV |
8257 | bfqd->bfq_wr_max_time = 0; |
8258 | bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000); | |
8259 | bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500); | |
77b7dcea PV |
8260 | bfqd->bfq_wr_max_softrt_rate = 7000; /* |
8261 | * Approximate rate required | |
8262 | * to playback or record a | |
8263 | * high-definition compressed | |
8264 | * video. | |
8265 | */ | |
cfd69712 | 8266 | bfqd->wr_busy_queues = 0; |
44e44a1b PV |
8267 | |
8268 | /* | |
8269 | * Begin by assuming, optimistically, that the device is a | |
8270 | * high-speed one, and that its peak rate is equal to 2/3 of | |
8271 | * the highest reference rate. | |
8272 | */ | |
8273 | bfqd->RT_prod = R_fast[blk_queue_nonrot(bfqd->queue)] * | |
8274 | T_fast[blk_queue_nonrot(bfqd->queue)]; | |
8275 | bfqd->peak_rate = R_fast[blk_queue_nonrot(bfqd->queue)] * 2 / 3; | |
8276 | bfqd->device_speed = BFQ_BFQD_FAST; | |
8277 | ||
aee69d78 | 8278 | spin_lock_init(&bfqd->lock); |
aee69d78 | 8279 | |
e21b7a0b AA |
8280 | /* |
8281 | * The invocation of the next bfq_create_group_hierarchy | |
8282 | * function is the head of a chain of function calls | |
8283 | * (bfq_create_group_hierarchy->blkcg_activate_policy-> | |
8284 | * blk_mq_freeze_queue) that may lead to the invocation of the | |
8285 | * has_work hook function. For this reason, | |
8286 | * bfq_create_group_hierarchy is invoked only after all | |
8287 | * scheduler data has been initialized, apart from the fields | |
8288 | * that can be initialized only after invoking | |
8289 | * bfq_create_group_hierarchy. This, in particular, enables | |
8290 | * has_work to correctly return false. Of course, to avoid | |
8291 | * other inconsistencies, the blk-mq stack must then refrain | |
8292 | * from invoking further scheduler hooks before this init | |
8293 | * function is finished. | |
8294 | */ | |
8295 | bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node); | |
8296 | if (!bfqd->root_group) | |
8297 | goto out_free; | |
8298 | bfq_init_root_group(bfqd->root_group, bfqd); | |
8299 | bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group); | |
8300 | ||
aee69d78 PV |
8301 | |
8302 | return 0; | |
e21b7a0b AA |
8303 | |
8304 | out_free: | |
8305 | kfree(bfqd); | |
8306 | kobject_put(&eq->kobj); | |
8307 | return -ENOMEM; | |
aee69d78 PV |
8308 | } |
8309 | ||
8310 | static void bfq_slab_kill(void) | |
8311 | { | |
8312 | kmem_cache_destroy(bfq_pool); | |
8313 | } | |
8314 | ||
8315 | static int __init bfq_slab_setup(void) | |
8316 | { | |
8317 | bfq_pool = KMEM_CACHE(bfq_queue, 0); | |
8318 | if (!bfq_pool) | |
8319 | return -ENOMEM; | |
8320 | return 0; | |
8321 | } | |
8322 | ||
8323 | static ssize_t bfq_var_show(unsigned int var, char *page) | |
8324 | { | |
8325 | return sprintf(page, "%u\n", var); | |
8326 | } | |
8327 | ||
8328 | static ssize_t bfq_var_store(unsigned long *var, const char *page, | |
8329 | size_t count) | |
8330 | { | |
8331 | unsigned long new_val; | |
8332 | int ret = kstrtoul(page, 10, &new_val); | |
8333 | ||
8334 | if (ret == 0) | |
8335 | *var = new_val; | |
8336 | ||
8337 | return count; | |
8338 | } | |
8339 | ||
8340 | #define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \ | |
8341 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ | |
8342 | { \ | |
8343 | struct bfq_data *bfqd = e->elevator_data; \ | |
8344 | u64 __data = __VAR; \ | |
8345 | if (__CONV == 1) \ | |
8346 | __data = jiffies_to_msecs(__data); \ | |
8347 | else if (__CONV == 2) \ | |
8348 | __data = div_u64(__data, NSEC_PER_MSEC); \ | |
8349 | return bfq_var_show(__data, (page)); \ | |
8350 | } | |
8351 | SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2); | |
8352 | SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2); | |
8353 | SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0); | |
8354 | SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0); | |
8355 | SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2); | |
8356 | SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0); | |
8357 | SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1); | |
8358 | SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0); | |
44e44a1b | 8359 | SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0); |
aee69d78 PV |
8360 | #undef SHOW_FUNCTION |
8361 | ||
8362 | #define USEC_SHOW_FUNCTION(__FUNC, __VAR) \ | |
8363 | static ssize_t __FUNC(struct elevator_queue *e, char *page) \ | |
8364 | { \ | |
8365 | struct bfq_data *bfqd = e->elevator_data; \ | |
8366 | u64 __data = __VAR; \ | |
8367 | __data = div_u64(__data, NSEC_PER_USEC); \ | |
8368 | return bfq_var_show(__data, (page)); \ | |
8369 | } | |
8370 | USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle); | |
8371 | #undef USEC_SHOW_FUNCTION | |
8372 | ||
8373 | #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \ | |
8374 | static ssize_t \ | |
8375 | __FUNC(struct elevator_queue *e, const char *page, size_t count) \ | |
8376 | { \ | |
8377 | struct bfq_data *bfqd = e->elevator_data; \ | |
8378 | unsigned long uninitialized_var(__data); \ | |
8379 | int ret = bfq_var_store(&__data, (page), count); \ | |
8380 | if (__data < (MIN)) \ | |
8381 | __data = (MIN); \ | |
8382 | else if (__data > (MAX)) \ | |
8383 | __data = (MAX); \ | |
8384 | if (__CONV == 1) \ | |
8385 | *(__PTR) = msecs_to_jiffies(__data); \ | |
8386 | else if (__CONV == 2) \ | |
8387 | *(__PTR) = (u64)__data * NSEC_PER_MSEC; \ | |
8388 | else \ | |
8389 | *(__PTR) = __data; \ | |
8390 | return ret; \ | |
8391 | } | |
8392 | STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1, | |
8393 | INT_MAX, 2); | |
8394 | STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1, | |
8395 | INT_MAX, 2); | |
8396 | STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0); | |
8397 | STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1, | |
8398 | INT_MAX, 0); | |
8399 | STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2); | |
8400 | #undef STORE_FUNCTION | |
8401 | ||
8402 | #define USEC_STORE_FUNCTION(__FUNC, __PTR, MIN, MAX) \ | |
8403 | static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count)\ | |
8404 | { \ | |
8405 | struct bfq_data *bfqd = e->elevator_data; \ | |
8406 | unsigned long uninitialized_var(__data); \ | |
8407 | int ret = bfq_var_store(&__data, (page), count); \ | |
8408 | if (__data < (MIN)) \ | |
8409 | __data = (MIN); \ | |
8410 | else if (__data > (MAX)) \ | |
8411 | __data = (MAX); \ | |
8412 | *(__PTR) = (u64)__data * NSEC_PER_USEC; \ | |
8413 | return ret; \ | |
8414 | } | |
8415 | USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0, | |
8416 | UINT_MAX); | |
8417 | #undef USEC_STORE_FUNCTION | |
8418 | ||
aee69d78 PV |
8419 | static ssize_t bfq_max_budget_store(struct elevator_queue *e, |
8420 | const char *page, size_t count) | |
8421 | { | |
8422 | struct bfq_data *bfqd = e->elevator_data; | |
8423 | unsigned long uninitialized_var(__data); | |
8424 | int ret = bfq_var_store(&__data, (page), count); | |
8425 | ||
8426 | if (__data == 0) | |
ab0e43e9 | 8427 | bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); |
aee69d78 PV |
8428 | else { |
8429 | if (__data > INT_MAX) | |
8430 | __data = INT_MAX; | |
8431 | bfqd->bfq_max_budget = __data; | |
8432 | } | |
8433 | ||
8434 | bfqd->bfq_user_max_budget = __data; | |
8435 | ||
8436 | return ret; | |
8437 | } | |
8438 | ||
8439 | /* | |
8440 | * Leaving this name to preserve name compatibility with cfq | |
8441 | * parameters, but this timeout is used for both sync and async. | |
8442 | */ | |
8443 | static ssize_t bfq_timeout_sync_store(struct elevator_queue *e, | |
8444 | const char *page, size_t count) | |
8445 | { | |
8446 | struct bfq_data *bfqd = e->elevator_data; | |
8447 | unsigned long uninitialized_var(__data); | |
8448 | int ret = bfq_var_store(&__data, (page), count); | |
8449 | ||
8450 | if (__data < 1) | |
8451 | __data = 1; | |
8452 | else if (__data > INT_MAX) | |
8453 | __data = INT_MAX; | |
8454 | ||
8455 | bfqd->bfq_timeout = msecs_to_jiffies(__data); | |
8456 | if (bfqd->bfq_user_max_budget == 0) | |
ab0e43e9 | 8457 | bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); |
aee69d78 PV |
8458 | |
8459 | return ret; | |
8460 | } | |
8461 | ||
8462 | static ssize_t bfq_strict_guarantees_store(struct elevator_queue *e, | |
8463 | const char *page, size_t count) | |
8464 | { | |
8465 | struct bfq_data *bfqd = e->elevator_data; | |
8466 | unsigned long uninitialized_var(__data); | |
8467 | int ret = bfq_var_store(&__data, (page), count); | |
8468 | ||
8469 | if (__data > 1) | |
8470 | __data = 1; | |
8471 | if (!bfqd->strict_guarantees && __data == 1 | |
8472 | && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC) | |
8473 | bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC; | |
8474 | ||
8475 | bfqd->strict_guarantees = __data; | |
8476 | ||
8477 | return ret; | |
8478 | } | |
8479 | ||
44e44a1b PV |
8480 | static ssize_t bfq_low_latency_store(struct elevator_queue *e, |
8481 | const char *page, size_t count) | |
8482 | { | |
8483 | struct bfq_data *bfqd = e->elevator_data; | |
8484 | unsigned long uninitialized_var(__data); | |
8485 | int ret = bfq_var_store(&__data, (page), count); | |
8486 | ||
8487 | if (__data > 1) | |
8488 | __data = 1; | |
8489 | if (__data == 0 && bfqd->low_latency != 0) | |
8490 | bfq_end_wr(bfqd); | |
8491 | bfqd->low_latency = __data; | |
8492 | ||
8493 | return ret; | |
8494 | } | |
8495 | ||
aee69d78 PV |
8496 | #define BFQ_ATTR(name) \ |
8497 | __ATTR(name, 0644, bfq_##name##_show, bfq_##name##_store) | |
8498 | ||
8499 | static struct elv_fs_entry bfq_attrs[] = { | |
8500 | BFQ_ATTR(fifo_expire_sync), | |
8501 | BFQ_ATTR(fifo_expire_async), | |
8502 | BFQ_ATTR(back_seek_max), | |
8503 | BFQ_ATTR(back_seek_penalty), | |
8504 | BFQ_ATTR(slice_idle), | |
8505 | BFQ_ATTR(slice_idle_us), | |
8506 | BFQ_ATTR(max_budget), | |
8507 | BFQ_ATTR(timeout_sync), | |
8508 | BFQ_ATTR(strict_guarantees), | |
44e44a1b | 8509 | BFQ_ATTR(low_latency), |
aee69d78 PV |
8510 | __ATTR_NULL |
8511 | }; | |
8512 | ||
8513 | static struct elevator_type iosched_bfq_mq = { | |
8514 | .ops.mq = { | |
8515 | .get_rq_priv = bfq_get_rq_private, | |
8516 | .put_rq_priv = bfq_put_rq_private, | |
8517 | .exit_icq = bfq_exit_icq, | |
8518 | .insert_requests = bfq_insert_requests, | |
8519 | .dispatch_request = bfq_dispatch_request, | |
8520 | .next_request = elv_rb_latter_request, | |
8521 | .former_request = elv_rb_former_request, | |
8522 | .allow_merge = bfq_allow_bio_merge, | |
8523 | .bio_merge = bfq_bio_merge, | |
8524 | .request_merge = bfq_request_merge, | |
8525 | .requests_merged = bfq_requests_merged, | |
8526 | .request_merged = bfq_request_merged, | |
8527 | .has_work = bfq_has_work, | |
8528 | .init_sched = bfq_init_queue, | |
8529 | .exit_sched = bfq_exit_queue, | |
8530 | }, | |
8531 | ||
8532 | .uses_mq = true, | |
8533 | .icq_size = sizeof(struct bfq_io_cq), | |
8534 | .icq_align = __alignof__(struct bfq_io_cq), | |
8535 | .elevator_attrs = bfq_attrs, | |
8536 | .elevator_name = "bfq", | |
8537 | .elevator_owner = THIS_MODULE, | |
8538 | }; | |
8539 | ||
e21b7a0b AA |
8540 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
8541 | static struct blkcg_policy blkcg_policy_bfq = { | |
8542 | .dfl_cftypes = bfq_blkg_files, | |
8543 | .legacy_cftypes = bfq_blkcg_legacy_files, | |
8544 | ||
8545 | .cpd_alloc_fn = bfq_cpd_alloc, | |
8546 | .cpd_init_fn = bfq_cpd_init, | |
8547 | .cpd_bind_fn = bfq_cpd_init, | |
8548 | .cpd_free_fn = bfq_cpd_free, | |
8549 | ||
8550 | .pd_alloc_fn = bfq_pd_alloc, | |
8551 | .pd_init_fn = bfq_pd_init, | |
8552 | .pd_offline_fn = bfq_pd_offline, | |
8553 | .pd_free_fn = bfq_pd_free, | |
8554 | .pd_reset_stats_fn = bfq_pd_reset_stats, | |
8555 | }; | |
8556 | #endif | |
8557 | ||
aee69d78 PV |
8558 | static int __init bfq_init(void) |
8559 | { | |
8560 | int ret; | |
8561 | ||
e21b7a0b AA |
8562 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
8563 | ret = blkcg_policy_register(&blkcg_policy_bfq); | |
8564 | if (ret) | |
8565 | return ret; | |
8566 | #endif | |
8567 | ||
aee69d78 PV |
8568 | ret = -ENOMEM; |
8569 | if (bfq_slab_setup()) | |
8570 | goto err_pol_unreg; | |
8571 | ||
44e44a1b PV |
8572 | /* |
8573 | * Times to load large popular applications for the typical | |
8574 | * systems installed on the reference devices (see the | |
8575 | * comments before the definitions of the next two | |
8576 | * arrays). Actually, we use slightly slower values, as the | |
8577 | * estimated peak rate tends to be smaller than the actual | |
8578 | * peak rate. The reason for this last fact is that estimates | |
8579 | * are computed over much shorter time intervals than the long | |
8580 | * intervals typically used for benchmarking. Why? First, to | |
8581 | * adapt more quickly to variations. Second, because an I/O | |
8582 | * scheduler cannot rely on a peak-rate-evaluation workload to | |
8583 | * be run for a long time. | |
8584 | */ | |
8585 | T_slow[0] = msecs_to_jiffies(3500); /* actually 4 sec */ | |
8586 | T_slow[1] = msecs_to_jiffies(6000); /* actually 6.5 sec */ | |
8587 | T_fast[0] = msecs_to_jiffies(7000); /* actually 8 sec */ | |
8588 | T_fast[1] = msecs_to_jiffies(2500); /* actually 3 sec */ | |
8589 | ||
8590 | /* | |
8591 | * Thresholds that determine the switch between speed classes | |
8592 | * (see the comments before the definition of the array | |
8593 | * device_speed_thresh). These thresholds are biased towards | |
8594 | * transitions to the fast class. This is safer than the | |
8595 | * opposite bias. In fact, a wrong transition to the slow | |
8596 | * class results in short weight-raising periods, because the | |
8597 | * speed of the device then tends to be higher that the | |
8598 | * reference peak rate. On the opposite end, a wrong | |
8599 | * transition to the fast class tends to increase | |
8600 | * weight-raising periods, because of the opposite reason. | |
8601 | */ | |
8602 | device_speed_thresh[0] = (4 * R_slow[0]) / 3; | |
8603 | device_speed_thresh[1] = (4 * R_slow[1]) / 3; | |
8604 | ||
aee69d78 PV |
8605 | ret = elv_register(&iosched_bfq_mq); |
8606 | if (ret) | |
8607 | goto err_pol_unreg; | |
8608 | ||
8609 | return 0; | |
8610 | ||
8611 | err_pol_unreg: | |
e21b7a0b AA |
8612 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
8613 | blkcg_policy_unregister(&blkcg_policy_bfq); | |
8614 | #endif | |
aee69d78 PV |
8615 | return ret; |
8616 | } | |
8617 | ||
8618 | static void __exit bfq_exit(void) | |
8619 | { | |
8620 | elv_unregister(&iosched_bfq_mq); | |
e21b7a0b AA |
8621 | #ifdef CONFIG_BFQ_GROUP_IOSCHED |
8622 | blkcg_policy_unregister(&blkcg_policy_bfq); | |
8623 | #endif | |
aee69d78 PV |
8624 | bfq_slab_kill(); |
8625 | } | |
8626 | ||
8627 | module_init(bfq_init); | |
8628 | module_exit(bfq_exit); | |
8629 | ||
8630 | MODULE_AUTHOR("Paolo Valente"); | |
8631 | MODULE_LICENSE("GPL"); | |
8632 | MODULE_DESCRIPTION("MQ Budget Fair Queueing I/O Scheduler"); |