Lines Matching +full:sub +full:- +full:sampled
1 // SPDX-License-Identifier: GPL-2.0-or-later
16 * BFQ is a proportional-share I/O scheduler, with some extra
17 * low-latency capabilities. BFQ also supports full hierarchical
20 * limitations can be found in Documentation/block/bfq-iosched.rst.
22 * BFQ is a proportional-share storage-I/O scheduling algorithm based
23 * on the slice-by-slice service scheme of CFQ. But BFQ assigns
25 * time slices. The device is not granted to the in-service process
31 * B-WF2Q+, to schedule processes according to their budgets. More
33 * process/queue is assigned a user-configurable weight, and B-WF2Q+
36 * B-WF2Q+, BFQ can afford to assign high budgets to I/O-bound
38 * and yet guarantee a low latency to interactive and soft real-time
41 * In particular, to provide these low-latency guarantees, BFQ
42 * explicitly privileges the I/O of two classes of time-sensitive
43 * applications: interactive and soft real-time. In more detail, BFQ
50 * real-time application. For brevity, in these cases, the queue is
51 * said to be interactive or soft real-time. In both cases, BFQ
52 * privileges the service of the queue, over that of non-interactive
53 * and non-soft-real-time queues. This privileging is performed,
55 * call just weight-raising periods the time periods during which a
56 * queue is privileged, because deemed interactive or soft real-time.
58 * The detection of soft real-time queues/applications is described in
70 * non-empty queue stops being deemed interactive. Since a queue is
71 * weight-raised while it is deemed interactive, this maximum time
73 * weight-raising for interactive queues.
76 * preserving both a low latency and a high throughput on NCQ-capable,
77 * rotational or flash-based devices, and to get the job done quickly
78 * for applications consisting in many I/O-bound processes.
81 * the maximum-possible throughput at all times, then do switch off
82 * all low-latency heuristics for that device, by setting low_latency
91 * ones that guarantee a low latency to interactive and soft real-time
92 * applications, and a hierarchical extension based on H-WF2Q+.
94 * B-WF2Q+ is based on WF2Q+, which is described in [2], together with
95 * H-WF2Q+, while the augmented tree used here to implement B-WF2Q+
101 * Technologies (MST-2015), May 2015.
102 * http://algogroup.unimore.it/people/paolo/disk_sched/mst-2015.pdf
105 * Algorithms", IEEE/ACM Transactions on Networking, 5(5):675-689,
108 * http://www.cs.cmu.edu/~hzhang/papers/TON-97-Oct.ps.gz
110 * [3] I. Stoica and H. Abdel-Wahab, "Earliest Eligible Virtual Deadline
114 * http://www.cs.berkeley.edu/~istoica/papers/eevdf-tr-95.pdf
125 #include <linux/backing-dev.h>
131 #include "blk-mq.h"
132 #include "blk-mq-tag.h"
133 #include "blk-mq-sched.h"
134 #include "bfq-iosched.h"
135 #include "blk-wbt.h"
140 __set_bit(BFQQF_##name, &(bfqq)->flags); \
144 __clear_bit(BFQQF_##name, &(bfqq)->flags); \
148 return test_bit(BFQQF_##name, &(bfqq)->flags); \
199 * - when the group does writes, w.r.t. to when it does reads;
200 * - when other groups do reads, w.r.t. to when they do writes.
235 (!blk_queue_nonrot(bfqd->queue) || \
238 #define BFQQ_SEEKY(bfqq) (hweight32(bfqq->seek_history) > 19)
240 * Sync random I/O is likely to be confused with soft real-time I/O,
244 * as soft real-time.
246 #define BFQQ_TOTALLY_SEEKY(bfqq) (bfqq->seek_history == -1)
248 /* Min number of samples required to perform peak-rate update */
250 /* Min observation time interval required to perform a peak-rate update (ns) */
252 /* Target observation time interval for a peak-rate update (ns) */
256 * Shift used for peak-rate fixed precision calculations.
258 * - the current shift: 16 positions
259 * - the current type used to store rate: u32
260 * - the current unit of measure for rate: [sectors/usec], or, more precisely,
263 * [1 / 2^BFQ_RATE_SHIFT, 2^(32 - BFQ_RATE_SHIFT)] sectors/usec =
264 * [1 / 2^16, 2^16] sectors/usec = [15e-6, 65536] sectors/usec =
272 * When configured for computing the duration of the weight-raising
289 * depending on whether the device is rotational or non-rotational.
294 * non-rotational device. The reference rates are not the actual peak
297 * peak-rate estimator tends to yield slightly lower values than the
302 * The reference peak rates are measured in sectors/usec, left-shifted
314 * BFQ uses the above-detailed, time-based weight-raising mechanism to
316 * following false positives: I/O-bound applications that will go on
319 * weight-raised at the beginning of their I/O. On the opposite end,
320 * while being weight-raised, these applications
324 * in loss of device throughput with most flash-based storage, and may
329 * finish explaining how the duration of weight-raising for
338 * reference interactive task is the start-up of LibreOffice Writer,
343 * duration of weight-raising for at least one class of I/O-bound
344 * applications: those doing sequential or quasi-sequential I/O. An
345 * example is file copy. In fact, once started, the main I/O-bound
348 * is starting, because these I/O-bound processes will greedily devote
350 * throughput-friendly I/O operations. This is even more true if BFQ
354 * have no right to be weight-raised any longer.
356 * Basing on the last consideration, BFQ ends weight-raising for a
361 * This early ending of weight-raising reduces the amount of time
377 #define RQ_BIC(rq) ((struct bfq_io_cq *)((rq)->elv.priv[0]))
378 #define RQ_BFQQ(rq) ((rq)->elv.priv[1])
382 return bic->bfqq[is_sync]; in bic_to_bfqq()
390 * If bfqq != NULL, then a non-stable queue merge between in bic_set_bfqq()
391 * bic->bfqq and bfqq is happening here. This causes troubles in bic_set_bfqq()
392 * in the following case: bic->bfqq has also been scheduled in bic_set_bfqq()
393 * for a possible stable merge with bic->stable_merge_bfqq, in bic_set_bfqq()
394 * and bic->stable_merge_bfqq == bfqq happens to in bic_set_bfqq()
397 * bic->stable_merge_bfqq points exactly to bfqq, then bfqq in bic_set_bfqq()
400 * bic->stable_merge_bfqq == bfqq. in bic_set_bfqq()
402 bic->bfqq[is_sync] = bfqq; in bic_set_bfqq()
404 if (bfqq && bic->stable_merge_bfqq == bfqq) { in bic_set_bfqq()
413 bfq_put_stable_ref(bic->stable_merge_bfqq); in bic_set_bfqq()
415 bic->stable_merge_bfqq = NULL; in bic_set_bfqq()
421 return bic->icq.q->elevator->elevator_data; in bic_to_bfqd()
425 * icq_to_bic - convert iocontext queue structure to bfq_io_cq.
430 /* bic->icq is the first member, %NULL will convert to %NULL */ in icq_to_bic()
435 * bfq_bic_lookup - search into @ioc a bic associated to @bfqd.
443 if (!current->io_context) in bfq_bic_lookup()
446 spin_lock_irqsave(&q->queue_lock, flags); in bfq_bic_lookup()
448 spin_unlock_irqrestore(&q->queue_lock, flags); in bfq_bic_lookup()
459 lockdep_assert_held(&bfqd->lock); in bfq_schedule_dispatch()
461 if (bfqd->queued != 0) { in bfq_schedule_dispatch()
463 blk_mq_run_hw_queues(bfqd->queue, true); in bfq_schedule_dispatch()
467 #define bfq_class_idle(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
472 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
496 if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META)) in bfq_choose_req()
498 else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META)) in bfq_choose_req()
507 back_max = bfqd->bfq_back_max * 2; in bfq_choose_req()
515 d1 = s1 - last; in bfq_choose_req()
517 d1 = (last - s1) * bfqd->bfq_back_penalty; in bfq_choose_req()
522 d2 = s2 - last; in bfq_choose_req()
524 d2 = (last - s2) * bfqd->bfq_back_penalty; in bfq_choose_req()
532 * check two variables for all permutations: --> faster! in bfq_choose_req()
555 * (--> only *one* back seek required), in bfq_choose_req()
570 struct bfq_data *bfqd = bfqq->bfqd; in bfqq_request_over_limit()
571 struct bfq_entity *entity = &bfqq->entity; in bfqq_request_over_limit()
575 int class_idx = bfqq->ioprio_class - 1; in bfqq_request_over_limit()
580 if (!entity->on_st_or_in_serv) in bfqq_request_over_limit()
584 spin_lock_irq(&bfqd->lock); in bfqq_request_over_limit()
586 depth = bfqg_to_blkg(bfqq_group(bfqq))->blkcg->css.cgroup->level + 1; in bfqq_request_over_limit()
588 spin_unlock_irq(&bfqd->lock); in bfqq_request_over_limit()
598 sched_data = entity->sched_data; in bfqq_request_over_limit()
607 if (!entity->on_st_or_in_serv) in bfqq_request_over_limit()
615 for (level--; level >= 0; level--) { in bfqq_request_over_limit()
618 wsum = bfq_entity_service_tree(entity)->wsum; in bfqq_request_over_limit()
631 sched_data->service_tree[i].wsum; in bfqq_request_over_limit()
634 limit = DIV_ROUND_CLOSEST(limit * entity->weight, wsum); in bfqq_request_over_limit()
635 if (entity->allocated >= limit) { in bfqq_request_over_limit()
636 bfq_log_bfqq(bfqq->bfqd, bfqq, in bfqq_request_over_limit()
638 entity->allocated, limit, level); in bfqq_request_over_limit()
644 spin_unlock_irq(&bfqd->lock); in bfqq_request_over_limit()
673 struct bfq_data *bfqd = data->q->elevator->elevator_data; in bfq_limit_depth()
674 struct bfq_io_cq *bic = bfq_bic_lookup(data->q); in bfq_limit_depth()
677 unsigned limit = data->q->nr_requests; in bfq_limit_depth()
683 depth = bfqd->word_depths[!!bfqd->wr_busy_queues][op_is_sync(opf)]; in bfq_limit_depth()
684 limit = (limit * depth) >> bfqd->full_depth_shift; in bfq_limit_depth()
696 __func__, bfqd->wr_busy_queues, op_is_sync(opf), depth); in bfq_limit_depth()
698 data->shallow_depth = depth; in bfq_limit_depth()
710 p = &root->rb_node; in bfq_rq_pos_tree_lookup()
721 if (sector > blk_rq_pos(bfqq->next_rq)) in bfq_rq_pos_tree_lookup()
722 n = &(*p)->rb_right; in bfq_rq_pos_tree_lookup()
723 else if (sector < blk_rq_pos(bfqq->next_rq)) in bfq_rq_pos_tree_lookup()
724 n = &(*p)->rb_left; in bfq_rq_pos_tree_lookup()
737 bfqq ? bfqq->pid : 0); in bfq_rq_pos_tree_lookup()
744 return bfqq->service_from_backlogged > 0 && in bfq_too_late_for_merging()
745 time_is_before_jiffies(bfqq->first_IO_time + in bfq_too_late_for_merging()
763 if (bfqq->pos_root) { in bfq_pos_tree_add_move()
764 rb_erase(&bfqq->pos_node, bfqq->pos_root); in bfq_pos_tree_add_move()
765 bfqq->pos_root = NULL; in bfq_pos_tree_add_move()
769 if (bfqq == &bfqd->oom_bfqq) in bfq_pos_tree_add_move()
782 if (!bfqq->next_rq) in bfq_pos_tree_add_move()
785 bfqq->pos_root = &bfqq_group(bfqq)->rq_pos_tree; in bfq_pos_tree_add_move()
786 __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root, in bfq_pos_tree_add_move()
787 blk_rq_pos(bfqq->next_rq), &parent, &p); in bfq_pos_tree_add_move()
789 rb_link_node(&bfqq->pos_node, parent, p); in bfq_pos_tree_add_move()
790 rb_insert_color(&bfqq->pos_node, bfqq->pos_root); in bfq_pos_tree_add_move()
792 bfqq->pos_root = NULL; in bfq_pos_tree_add_move()
805 * of this function is used to check whether I/O-dispatch plugging can
810 * 2) all active queues belong to the same I/O-priority class,
817 * the last two symmetry sub-conditions above would be quite complex
819 * only the following stronger three sub-conditions, for which it is
822 * 2) all active queues belong to the same I/O-priority class,
832 bfqq->weight_counter && in bfq_asymmetric_scenario()
833 bfqq->weight_counter == in bfq_asymmetric_scenario()
835 rb_first_cached(&bfqd->queue_weights_tree), in bfq_asymmetric_scenario()
844 !RB_EMPTY_ROOT(&bfqd->queue_weights_tree.rb_root) && in bfq_asymmetric_scenario()
845 (bfqd->queue_weights_tree.rb_root.rb_node->rb_left || in bfq_asymmetric_scenario()
846 bfqd->queue_weights_tree.rb_root.rb_node->rb_right); in bfq_asymmetric_scenario()
849 (bfqd->busy_queues[0] && bfqd->busy_queues[1]) || in bfq_asymmetric_scenario()
850 (bfqd->busy_queues[0] && bfqd->busy_queues[2]) || in bfq_asymmetric_scenario()
851 (bfqd->busy_queues[1] && bfqd->busy_queues[2]); in bfq_asymmetric_scenario()
855 || bfqd->num_groups_with_pending_reqs > 0 in bfq_asymmetric_scenario()
861 * If the weight-counter tree passed as input contains no counter for
865 * Note that weight-counter trees contain few nodes in mostly symmetric
867 * weight-counter tree for the queues may contain at most one node.
868 * This holds even if low_latency is on, because weight-raised queues
876 struct bfq_entity *entity = &bfqq->entity; in bfq_weights_tree_add()
877 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in bfq_weights_tree_add()
884 * non-weight-raised, and hence change its weight, and in bfq_weights_tree_add()
892 if (bfqq->weight_counter) in bfq_weights_tree_add()
901 if (entity->weight == __counter->weight) { in bfq_weights_tree_add()
902 bfqq->weight_counter = __counter; in bfq_weights_tree_add()
905 if (entity->weight < __counter->weight) in bfq_weights_tree_add()
906 new = &((*new)->rb_left); in bfq_weights_tree_add()
908 new = &((*new)->rb_right); in bfq_weights_tree_add()
913 bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter), in bfq_weights_tree_add()
926 * if !bfqq->weight_counter. in bfq_weights_tree_add()
928 if (unlikely(!bfqq->weight_counter)) in bfq_weights_tree_add()
931 bfqq->weight_counter->weight = entity->weight; in bfq_weights_tree_add()
932 rb_link_node(&bfqq->weight_counter->weights_node, parent, new); in bfq_weights_tree_add()
933 rb_insert_color_cached(&bfqq->weight_counter->weights_node, root, in bfq_weights_tree_add()
937 bfqq->weight_counter->num_active++; in bfq_weights_tree_add()
938 bfqq->ref++; in bfq_weights_tree_add()
951 if (!bfqq->weight_counter) in __bfq_weights_tree_remove()
954 bfqq->weight_counter->num_active--; in __bfq_weights_tree_remove()
955 if (bfqq->weight_counter->num_active > 0) in __bfq_weights_tree_remove()
958 rb_erase_cached(&bfqq->weight_counter->weights_node, root); in __bfq_weights_tree_remove()
959 kfree(bfqq->weight_counter); in __bfq_weights_tree_remove()
962 bfqq->weight_counter = NULL; in __bfq_weights_tree_remove()
973 struct bfq_entity *entity = bfqq->entity.parent; in bfq_weights_tree_remove()
976 struct bfq_sched_data *sd = entity->my_sched_data; in bfq_weights_tree_remove()
978 if (sd->next_in_service || sd->in_service_entity) { in bfq_weights_tree_remove()
1003 if (entity->in_groups_with_pending_reqs) { in bfq_weights_tree_remove()
1004 entity->in_groups_with_pending_reqs = false; in bfq_weights_tree_remove()
1005 bfqd->num_groups_with_pending_reqs--; in bfq_weights_tree_remove()
1016 &bfqd->queue_weights_tree); in bfq_weights_tree_remove()
1032 rq = rq_entry_fifo(bfqq->fifo.next); in bfq_check_fifo()
1034 if (rq == last || ktime_get_ns() < rq->fifo_time) in bfq_check_fifo()
1037 bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq); in bfq_check_fifo()
1045 struct rb_node *rbnext = rb_next(&last->rb_node); in bfq_find_next_rq()
1046 struct rb_node *rbprev = rb_prev(&last->rb_node); in bfq_find_next_rq()
1060 rbnext = rb_first(&bfqq->sort_list); in bfq_find_next_rq()
1061 if (rbnext && rbnext != &last->rb_node) in bfq_find_next_rq()
1072 if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1 || in bfq_serv_to_charge()
1073 bfq_asymmetric_scenario(bfqq->bfqd, bfqq)) in bfq_serv_to_charge()
1080 * bfq_updated_next_req - update the queue after a new next_rq selection.
1093 struct bfq_entity *entity = &bfqq->entity; in bfq_updated_next_req()
1094 struct request *next_rq = bfqq->next_rq; in bfq_updated_next_req()
1100 if (bfqq == bfqd->in_service_queue) in bfq_updated_next_req()
1108 max_t(unsigned long, bfqq->max_budget, in bfq_updated_next_req()
1110 entity->service); in bfq_updated_next_req()
1111 if (entity->budget != new_budget) { in bfq_updated_next_req()
1112 entity->budget = new_budget; in bfq_updated_next_req()
1123 if (bfqd->bfq_wr_max_time > 0) in bfq_wr_duration()
1124 return bfqd->bfq_wr_max_time; in bfq_wr_duration()
1126 dur = bfqd->rate_dur_prod; in bfq_wr_duration()
1127 do_div(dur, bfqd->peak_rate); in bfq_wr_duration()
1133 * - running in a slow PC in bfq_wr_duration()
1134 * - with a virtual disk stacked on a slow low-end 5400rpm HDD in bfq_wr_duration()
1135 * - serving a heavy I/O workload, such as the sequential reading in bfq_wr_duration()
1137 * mplayer took 23 seconds to start, if constantly weight-raised. in bfq_wr_duration()
1142 * responsiveness by allowing non-interactive applications to in bfq_wr_duration()
1147 * before weight-raising finishes. in bfq_wr_duration()
1152 /* switch back from soft real-time to interactive weight raising */
1156 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in switch_back_to_interactive_wr()
1157 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in switch_back_to_interactive_wr()
1158 bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt; in switch_back_to_interactive_wr()
1168 if (bic->saved_has_short_ttime) in bfq_bfqq_resume_state()
1173 if (bic->saved_IO_bound) in bfq_bfqq_resume_state()
1178 bfqq->last_serv_time_ns = bic->saved_last_serv_time_ns; in bfq_bfqq_resume_state()
1179 bfqq->inject_limit = bic->saved_inject_limit; in bfq_bfqq_resume_state()
1180 bfqq->decrease_time_jif = bic->saved_decrease_time_jif; in bfq_bfqq_resume_state()
1182 bfqq->entity.new_weight = bic->saved_weight; in bfq_bfqq_resume_state()
1183 bfqq->ttime = bic->saved_ttime; in bfq_bfqq_resume_state()
1184 bfqq->io_start_time = bic->saved_io_start_time; in bfq_bfqq_resume_state()
1185 bfqq->tot_idle_time = bic->saved_tot_idle_time; in bfq_bfqq_resume_state()
1189 if (bfqd->low_latency) { in bfq_bfqq_resume_state()
1190 old_wr_coeff = bfqq->wr_coeff; in bfq_bfqq_resume_state()
1191 bfqq->wr_coeff = bic->saved_wr_coeff; in bfq_bfqq_resume_state()
1193 bfqq->service_from_wr = bic->saved_service_from_wr; in bfq_bfqq_resume_state()
1194 bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt; in bfq_bfqq_resume_state()
1195 bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish; in bfq_bfqq_resume_state()
1196 bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time; in bfq_bfqq_resume_state()
1198 if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) || in bfq_bfqq_resume_state()
1199 time_is_before_jiffies(bfqq->last_wr_start_finish + in bfq_bfqq_resume_state()
1200 bfqq->wr_cur_max_time))) { in bfq_bfqq_resume_state()
1201 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in bfq_bfqq_resume_state()
1203 time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_bfqq_resume_state()
1207 bfqq->wr_coeff = 1; in bfq_bfqq_resume_state()
1208 bfq_log_bfqq(bfqq->bfqd, bfqq, in bfq_bfqq_resume_state()
1214 bfqq->entity.prio_changed = 1; in bfq_bfqq_resume_state()
1219 if (old_wr_coeff == 1 && bfqq->wr_coeff > 1) in bfq_bfqq_resume_state()
1220 bfqd->wr_busy_queues++; in bfq_bfqq_resume_state()
1221 else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1) in bfq_bfqq_resume_state()
1222 bfqd->wr_busy_queues--; in bfq_bfqq_resume_state()
1227 return bfqq->ref - bfqq->entity.allocated - in bfqq_process_refs()
1228 bfqq->entity.on_st_or_in_serv - in bfqq_process_refs()
1229 (bfqq->weight_counter != NULL) - bfqq->stable_ref; in bfqq_process_refs()
1238 hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node) in bfq_reset_burst_list()
1239 hlist_del_init(&item->burst_list_node); in bfq_reset_burst_list()
1247 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); in bfq_reset_burst_list()
1248 bfqd->burst_size = 1; in bfq_reset_burst_list()
1250 bfqd->burst_size = 0; in bfq_reset_burst_list()
1252 bfqd->burst_parent_entity = bfqq->entity.parent; in bfq_reset_burst_list()
1259 bfqd->burst_size++; in bfq_add_to_burst()
1261 if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) { in bfq_add_to_burst()
1269 bfqd->large_burst = true; in bfq_add_to_burst()
1275 hlist_for_each_entry(bfqq_item, &bfqd->burst_list, in bfq_add_to_burst()
1287 hlist_for_each_entry_safe(pos, n, &bfqd->burst_list, in bfq_add_to_burst()
1289 hlist_del_init(&pos->burst_list_node); in bfq_add_to_burst()
1296 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); in bfq_add_to_burst()
1306 * possible, it is usually better to not grant either weight-raising
1318 * completed. As a consequence, weight-raising any of these queues,
1322 * weight-raising these new queues just lowers throughput in most
1327 * parallel I/O-bound threads. In fact, with a complex application,
1328 * several short processes may need to be executed to start-up the
1334 * weight-raise all the queues created during the burst. This is the
1344 * weight-raise queues whose creation occurs in a large burst. In
1377 * the large-burst threshold, then
1385 * previous sub-step), and now is not needed any more
1387 * . the device enters a large-burst mode
1390 * the device is in large-burst mode and shortly after the last time
1400 * . the large-burst mode is reset if set
1415 if (!hlist_unhashed(&bfqq->burst_list_node) || in bfq_handle_burst()
1417 time_is_after_eq_jiffies(bfqq->split_time + in bfq_handle_burst()
1438 if (time_is_before_jiffies(bfqd->last_ins_in_burst + in bfq_handle_burst()
1439 bfqd->bfq_burst_interval) || in bfq_handle_burst()
1440 bfqq->entity.parent != bfqd->burst_parent_entity) { in bfq_handle_burst()
1441 bfqd->large_burst = false; in bfq_handle_burst()
1451 if (bfqd->large_burst) { in bfq_handle_burst()
1457 * If we get here, then a large-burst state has not yet been in bfq_handle_burst()
1471 bfqd->last_ins_in_burst = jiffies; in bfq_handle_burst()
1476 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_budget_left()
1478 return entity->budget - entity->service; in bfq_bfqq_budget_left()
1488 if (bfqd->budgets_assigned < bfq_stats_min_budgets) in bfq_max_budget()
1491 return bfqd->bfq_max_budget; in bfq_max_budget()
1500 if (bfqd->budgets_assigned < bfq_stats_min_budgets) in bfq_min_budget()
1503 return bfqd->bfq_max_budget / 32; in bfq_min_budget()
1509 * whether the in-service queue should be expired, by returning
1510 * true. The purpose of expiring the in-service queue is to give bfqq
1511 * the chance to possibly preempt the in-service queue, and the reason
1512 * for preempting the in-service queue is to achieve one of the two
1519 * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
1523 * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
1524 * a new request before the expiration of the idling-time.
1553 * before last expiration. Thus timestamps need to be back-shifted
1557 * Secondly, to allow the process to recover the hole, the in-service
1560 * in-service queue to be completed, then it may become impossible to
1561 * let the process recover the hole, even if the back-shifted
1562 * timestamps of bfqq are lower than those of the in-service queue. If
1578 * above-described special way, and signals that the in-service queue
1579 * should be expired. Timestamp back-shifting is done later in
1585 * timestamp than the in-service queue. That is, the next budget of
1586 * bfqq may have to be completed before the one of the in-service
1587 * queue. If this is the case, then preempting the in-service queue
1593 * the in-service queue must be preempted. To have service trees
1594 * correctly updated, the in-service queue must be expired and
1597 * mechanism may be re-designed in such a way to make it possible to
1601 * even be no in-service queue when the next function is invoked (so,
1606 * in-service queue (unconditionally) only for queues that need to
1614 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_update_budg_for_activation()
1628 * that timestamps need to be back-shifted (and is in bfq_bfqq_update_budg_for_activation()
1634 * entity->service or entity->budget are not updated in bfq_bfqq_update_budg_for_activation()
1639 * entity->budget the remaining budget on such an in bfq_bfqq_update_budg_for_activation()
1642 entity->budget = min_t(unsigned long, in bfq_bfqq_update_budg_for_activation()
1644 bfqq->max_budget); in bfq_bfqq_update_budg_for_activation()
1647 * At this point, we have used entity->service to get in bfq_bfqq_update_budg_for_activation()
1649 * entity->budget). Thus we finally can, and have to, in bfq_bfqq_update_budg_for_activation()
1650 * reset entity->service. The latter must be reset in bfq_bfqq_update_budg_for_activation()
1655 entity->service = 0; in bfq_bfqq_update_budg_for_activation()
1663 entity->service = 0; in bfq_bfqq_update_budg_for_activation()
1664 entity->budget = max_t(unsigned long, bfqq->max_budget, in bfq_bfqq_update_budg_for_activation()
1665 bfq_serv_to_charge(bfqq->next_rq, bfqq)); in bfq_bfqq_update_budg_for_activation()
1676 return jiffies - MAX_JIFFY_OFFSET; in bfq_smallest_from_now()
1688 /* start a weight-raising period */ in bfq_update_bfqq_wr_on_rq_arrival()
1690 bfqq->service_from_wr = 0; in bfq_update_bfqq_wr_on_rq_arrival()
1691 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_update_bfqq_wr_on_rq_arrival()
1692 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_update_bfqq_wr_on_rq_arrival()
1698 * that, at the end of the soft-real-time in bfq_update_bfqq_wr_on_rq_arrival()
1700 * now, no interactive weight-raising period in bfq_update_bfqq_wr_on_rq_arrival()
1705 bfqq->wr_start_at_switch_to_srt = in bfq_update_bfqq_wr_on_rq_arrival()
1707 bfqq->wr_coeff = bfqd->bfq_wr_coeff * in bfq_update_bfqq_wr_on_rq_arrival()
1709 bfqq->wr_cur_max_time = in bfq_update_bfqq_wr_on_rq_arrival()
1710 bfqd->bfq_wr_rt_max_time; in bfq_update_bfqq_wr_on_rq_arrival()
1716 * scheduling-error component due to a too large in bfq_update_bfqq_wr_on_rq_arrival()
1722 bfqq->entity.budget = min_t(unsigned long, in bfq_update_bfqq_wr_on_rq_arrival()
1723 bfqq->entity.budget, in bfq_update_bfqq_wr_on_rq_arrival()
1727 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_update_bfqq_wr_on_rq_arrival()
1728 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_update_bfqq_wr_on_rq_arrival()
1730 bfqq->wr_coeff = 1; in bfq_update_bfqq_wr_on_rq_arrival()
1736 * the weight-raising duration for the in bfq_update_bfqq_wr_on_rq_arrival()
1737 * application with the weight-raising in bfq_update_bfqq_wr_on_rq_arrival()
1741 * before the weight-raising period for the in bfq_update_bfqq_wr_on_rq_arrival()
1748 * at a certain time weight-raising is in bfq_update_bfqq_wr_on_rq_arrival()
1759 * weight-raised while they are pending. in bfq_update_bfqq_wr_on_rq_arrival()
1761 if (bfqq->wr_cur_max_time != in bfq_update_bfqq_wr_on_rq_arrival()
1762 bfqd->bfq_wr_rt_max_time) { in bfq_update_bfqq_wr_on_rq_arrival()
1763 bfqq->wr_start_at_switch_to_srt = in bfq_update_bfqq_wr_on_rq_arrival()
1764 bfqq->last_wr_start_finish; in bfq_update_bfqq_wr_on_rq_arrival()
1766 bfqq->wr_cur_max_time = in bfq_update_bfqq_wr_on_rq_arrival()
1767 bfqd->bfq_wr_rt_max_time; in bfq_update_bfqq_wr_on_rq_arrival()
1768 bfqq->wr_coeff = bfqd->bfq_wr_coeff * in bfq_update_bfqq_wr_on_rq_arrival()
1771 bfqq->last_wr_start_finish = jiffies; in bfq_update_bfqq_wr_on_rq_arrival()
1779 return bfqq->dispatched == 0 && in bfq_bfqq_idle_for_long_time()
1781 bfqq->budget_timeout + in bfq_bfqq_idle_for_long_time()
1782 bfqd->bfq_wr_min_idle_time); in bfq_bfqq_idle_for_long_time()
1788 * weight than the in-service queue.
1795 if (bfqq->ioprio_class < in_serv_bfqq->ioprio_class) in bfq_bfqq_higher_class_or_weight()
1798 if (in_serv_bfqq->entity.parent == bfqq->entity.parent) { in bfq_bfqq_higher_class_or_weight()
1799 bfqq_weight = bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1800 in_serv_weight = in_serv_bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1802 if (bfqq->entity.parent) in bfq_bfqq_higher_class_or_weight()
1803 bfqq_weight = bfqq->entity.parent->weight; in bfq_bfqq_higher_class_or_weight()
1805 bfqq_weight = bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1806 if (in_serv_bfqq->entity.parent) in bfq_bfqq_higher_class_or_weight()
1807 in_serv_weight = in_serv_bfqq->entity.parent->weight; in bfq_bfqq_higher_class_or_weight()
1809 in_serv_weight = in_serv_bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1832 bfqq->ttime.last_end_request + in bfq_bfqq_handle_idle_busy_switch()
1833 bfqd->bfq_slice_idle * 3; in bfq_bfqq_handle_idle_busy_switch()
1837 * bfqq deserves to be weight-raised if: in bfq_bfqq_handle_idle_busy_switch()
1838 * - it is sync, in bfq_bfqq_handle_idle_busy_switch()
1839 * - it does not belong to a large burst, in bfq_bfqq_handle_idle_busy_switch()
1840 * - it has been idle for enough time or is soft real-time, in bfq_bfqq_handle_idle_busy_switch()
1841 * - is linked to a bfq_io_cq (it is not shared in any sense), in bfq_bfqq_handle_idle_busy_switch()
1842 * - has a default weight (otherwise we assume the user wanted in bfq_bfqq_handle_idle_busy_switch()
1846 soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 && in bfq_bfqq_handle_idle_busy_switch()
1849 time_is_before_jiffies(bfqq->soft_rt_next_start) && in bfq_bfqq_handle_idle_busy_switch()
1850 bfqq->dispatched == 0 && in bfq_bfqq_handle_idle_busy_switch()
1851 bfqq->entity.new_weight == 40; in bfq_bfqq_handle_idle_busy_switch()
1853 bfqq->entity.new_weight == 40; in bfq_bfqq_handle_idle_busy_switch()
1855 * Merged bfq_queues are kept out of weight-raising in bfq_bfqq_handle_idle_busy_switch()
1856 * (low-latency) mechanisms. The reason is that these queues in bfq_bfqq_handle_idle_busy_switch()
1857 * are usually created for non-interactive and in bfq_bfqq_handle_idle_busy_switch()
1858 * non-soft-real-time tasks. Yet this is not the case for in bfq_bfqq_handle_idle_busy_switch()
1859 * stably-merged queues. These queues are merged just because in bfq_bfqq_handle_idle_busy_switch()
1861 * easily serve the I/O of an interactive or soft-real time in bfq_bfqq_handle_idle_busy_switch()
1863 * processes. So let also stably-merged queued enjoy weight in bfq_bfqq_handle_idle_busy_switch()
1866 wr_or_deserves_wr = bfqd->low_latency && in bfq_bfqq_handle_idle_busy_switch()
1867 (bfqq->wr_coeff > 1 || in bfq_bfqq_handle_idle_busy_switch()
1869 (bfqq->bic || RQ_BIC(rq)->stably_merged) && in bfq_bfqq_handle_idle_busy_switch()
1874 * may want to preempt the in-service queue. in bfq_bfqq_handle_idle_busy_switch()
1896 bfqq->budget_timeout + in bfq_bfqq_handle_idle_busy_switch()
1898 hlist_del_init(&bfqq->burst_list_node); in bfq_bfqq_handle_idle_busy_switch()
1904 if (bfqd->low_latency) { in bfq_bfqq_handle_idle_busy_switch()
1905 if (unlikely(time_is_after_jiffies(bfqq->split_time))) in bfq_bfqq_handle_idle_busy_switch()
1907 bfqq->split_time = in bfq_bfqq_handle_idle_busy_switch()
1908 jiffies - bfqd->bfq_wr_min_idle_time - 1; in bfq_bfqq_handle_idle_busy_switch()
1910 if (time_is_before_jiffies(bfqq->split_time + in bfq_bfqq_handle_idle_busy_switch()
1911 bfqd->bfq_wr_min_idle_time)) { in bfq_bfqq_handle_idle_busy_switch()
1919 if (old_wr_coeff != bfqq->wr_coeff) in bfq_bfqq_handle_idle_busy_switch()
1920 bfqq->entity.prio_changed = 1; in bfq_bfqq_handle_idle_busy_switch()
1924 bfqq->last_idle_bklogged = jiffies; in bfq_bfqq_handle_idle_busy_switch()
1925 bfqq->service_from_backlogged = 0; in bfq_bfqq_handle_idle_busy_switch()
1931 * Expire in-service queue if preemption may be needed for in bfq_bfqq_handle_idle_busy_switch()
1937 * carry time-critical I/O, then bfqq's bandwidth is less in bfq_bfqq_handle_idle_busy_switch()
1938 * important than that of queues that carry time-critical I/O. in bfq_bfqq_handle_idle_busy_switch()
1940 * bfqq is at least as weight-raised, i.e., at least as time in bfq_bfqq_handle_idle_busy_switch()
1941 * critical, as the in-service queue. in bfq_bfqq_handle_idle_busy_switch()
1944 * or has a higher weight than the in-service queue. If this in bfq_bfqq_handle_idle_busy_switch()
1951 * the timestamps of both bfqq and of the in-service queue, in bfq_bfqq_handle_idle_busy_switch()
1958 * timestamps of the in-service queue would need to be in bfq_bfqq_handle_idle_busy_switch()
1970 * request to arrive for the currently in-service queue, but in bfq_bfqq_handle_idle_busy_switch()
1973 if (bfqd->in_service_queue && in bfq_bfqq_handle_idle_busy_switch()
1975 bfqq->wr_coeff >= bfqd->in_service_queue->wr_coeff) || in bfq_bfqq_handle_idle_busy_switch()
1976 bfq_bfqq_higher_class_or_weight(bfqq, bfqd->in_service_queue) || in bfq_bfqq_handle_idle_busy_switch()
1977 !bfq_better_to_idle(bfqd->in_service_queue)) && in bfq_bfqq_handle_idle_busy_switch()
1979 bfq_bfqq_expire(bfqd, bfqd->in_service_queue, in bfq_bfqq_handle_idle_busy_switch()
1987 bfqq->last_serv_time_ns = 0; in bfq_reset_inject_limit()
1993 bfqd->waited_rq = NULL; in bfq_reset_inject_limit()
2006 * get new I/O enqueued---and then completed---before being in bfq_reset_inject_limit()
2008 * limit-update algorithm the chance to measure the effect of in bfq_reset_inject_limit()
2038 * limit-update algorithm and possibly raise the limit to more in bfq_reset_inject_limit()
2042 bfqq->inject_limit = 0; in bfq_reset_inject_limit()
2044 bfqq->inject_limit = 1; in bfq_reset_inject_limit()
2046 bfqq->decrease_time_jif = jiffies; in bfq_reset_inject_limit()
2051 u64 tot_io_time = now_ns - bfqq->io_start_time; in bfq_update_io_intensity()
2053 if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfqq->dispatched == 0) in bfq_update_io_intensity()
2054 bfqq->tot_idle_time += in bfq_update_io_intensity()
2055 now_ns - bfqq->ttime.last_end_request; in bfq_update_io_intensity()
2064 if (bfqq->tot_idle_time * 5 > tot_io_time) in bfq_update_io_intensity()
2074 bfqq->io_start_time = now_ns - (tot_io_time>>1); in bfq_update_io_intensity()
2075 bfqq->tot_idle_time >>= 1; in bfq_update_io_intensity()
2101 * if it still has some in-flight I/O. In fact, in this case bfqq is actually
2103 * of some of the in-flight requests. In particular, on the first time, Q is
2109 * above three-times requirement and time limit for detection, make false
2121 * first I/O-plugging time interval for bfqq. This triggers the first
2124 * during the next I/O-plugging interval for bfqq.
2135 if (!bfqd->last_completed_rq_bfqq || in bfq_check_waker()
2136 bfqd->last_completed_rq_bfqq == bfqq || in bfq_check_waker()
2138 now_ns - bfqd->last_completion >= 4 * NSEC_PER_MSEC) in bfq_check_waker()
2147 if (bfqd->last_completed_rq_bfqq != in bfq_check_waker()
2148 bfqq->tentative_waker_bfqq || in bfq_check_waker()
2149 now_ns > bfqq->waker_detection_started + in bfq_check_waker()
2150 128 * (u64)bfqd->bfq_slice_idle) { in bfq_check_waker()
2156 bfqq->tentative_waker_bfqq = in bfq_check_waker()
2157 bfqd->last_completed_rq_bfqq; in bfq_check_waker()
2158 bfqq->num_waker_detections = 1; in bfq_check_waker()
2159 bfqq->waker_detection_started = now_ns; in bfq_check_waker()
2160 bfq_bfqq_name(bfqq->tentative_waker_bfqq, waker_name, in bfq_check_waker()
2164 bfqq->num_waker_detections++; in bfq_check_waker()
2166 if (bfqq->num_waker_detections == 3) { in bfq_check_waker()
2167 bfqq->waker_bfqq = bfqd->last_completed_rq_bfqq; in bfq_check_waker()
2168 bfqq->tentative_waker_bfqq = NULL; in bfq_check_waker()
2169 bfq_bfqq_name(bfqq->waker_bfqq, waker_name, in bfq_check_waker()
2175 * bfqq->waker_bfqq must be reset. To in bfq_check_waker()
2193 if (!hlist_unhashed(&bfqq->woken_list_node)) in bfq_check_waker()
2194 hlist_del_init(&bfqq->woken_list_node); in bfq_check_waker()
2195 hlist_add_head(&bfqq->woken_list_node, in bfq_check_waker()
2196 &bfqd->last_completed_rq_bfqq->woken_list); in bfq_check_waker()
2203 struct bfq_data *bfqd = bfqq->bfqd; in bfq_add_request()
2205 unsigned int old_wr_coeff = bfqq->wr_coeff; in bfq_add_request()
2210 bfqq->queued[rq_is_sync(rq)]++; in bfq_add_request()
2212 * Updating of 'bfqd->queued' is protected by 'bfqd->lock', however, it in bfq_add_request()
2215 WRITE_ONCE(bfqd->queued, bfqd->queued + 1); in bfq_add_request()
2217 if (bfq_bfqq_sync(bfqq) && RQ_BIC(rq)->requests <= 1) { in bfq_add_request()
2226 if (time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_add_request()
2234 * - bfqq is in service, because the total service in bfq_add_request()
2237 * - this is the right occasion to compute or to in bfq_add_request()
2248 * hole, and there are still in-flight requests, in bfq_add_request()
2251 * - the minimum interval for sampling the total in bfq_add_request()
2255 if (bfqq == bfqd->in_service_queue && in bfq_add_request()
2256 (bfqd->rq_in_driver == 0 || in bfq_add_request()
2257 (bfqq->last_serv_time_ns > 0 && in bfq_add_request()
2258 bfqd->rqs_injected && bfqd->rq_in_driver > 0)) && in bfq_add_request()
2259 time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_add_request()
2261 bfqd->last_empty_occupied_ns = ktime_get_ns(); in bfq_add_request()
2265 * wait_dispatch will cause bfqd->waited_rq to in bfq_add_request()
2268 bfqd->wait_dispatch = true; in bfq_add_request()
2282 if (bfqd->rq_in_driver == 0) in bfq_add_request()
2283 bfqd->rqs_injected = false; in bfq_add_request()
2290 elv_rb_add(&bfqq->sort_list, rq); in bfq_add_request()
2293 * Check if this request is a better next-serve candidate. in bfq_add_request()
2295 prev = bfqq->next_rq; in bfq_add_request()
2296 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position); in bfq_add_request()
2297 bfqq->next_rq = next_rq; in bfq_add_request()
2303 if (unlikely(!bfqd->nonrot_with_queueing && prev != bfqq->next_rq)) in bfq_add_request()
2310 if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) && in bfq_add_request()
2312 bfqq->last_wr_start_finish + in bfq_add_request()
2313 bfqd->bfq_wr_min_inter_arr_async)) { in bfq_add_request()
2314 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_add_request()
2315 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_add_request()
2317 bfqd->wr_busy_queues++; in bfq_add_request()
2318 bfqq->entity.prio_changed = 1; in bfq_add_request()
2320 if (prev != bfqq->next_rq) in bfq_add_request()
2328 * . if bfqq is not going to be weight-raised, because, for in bfq_add_request()
2329 * non weight-raised queues, last_wr_start_finish stores the in bfq_add_request()
2332 * weight-raise async queues in bfq_add_request()
2334 * . if bfqq is not weight-raised, because, if bfqq is now in bfq_add_request()
2335 * switching to weight-raised, then last_wr_start_finish in bfq_add_request()
2336 * stores the time when weight-raising starts in bfq_add_request()
2339 * bfqq is currently weight-raised, the weight-raising in bfq_add_request()
2342 * conditions, if bfqq is already weight-raised) in bfq_add_request()
2345 * real-time, because the weight-raising period is constantly in bfq_add_request()
2346 * restarted on idle-to-busy transitions for these queues, but in bfq_add_request()
2350 if (bfqd->low_latency && in bfq_add_request()
2351 (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive)) in bfq_add_request()
2352 bfqq->last_wr_start_finish = jiffies; in bfq_add_request()
2359 struct bfq_queue *bfqq = bfqd->bio_bfqq; in bfq_find_rq_fmerge()
2363 return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio)); in bfq_find_rq_fmerge()
2371 return abs(blk_rq_pos(rq) - last_pos); in get_sdist()
2379 struct bfq_data *bfqd = q->elevator->elevator_data;
2381 bfqd->rq_in_driver++;
2386 struct bfq_data *bfqd = q->elevator->elevator_data;
2388 bfqd->rq_in_driver--;
2396 struct bfq_data *bfqd = bfqq->bfqd; in bfq_remove_request()
2399 if (bfqq->next_rq == rq) { in bfq_remove_request()
2400 bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq); in bfq_remove_request()
2404 if (rq->queuelist.prev != &rq->queuelist) in bfq_remove_request()
2405 list_del_init(&rq->queuelist); in bfq_remove_request()
2406 bfqq->queued[sync]--; in bfq_remove_request()
2408 * Updating of 'bfqd->queued' is protected by 'bfqd->lock', however, it in bfq_remove_request()
2411 WRITE_ONCE(bfqd->queued, bfqd->queued - 1); in bfq_remove_request()
2412 elv_rb_del(&bfqq->sort_list, rq); in bfq_remove_request()
2415 if (q->last_merge == rq) in bfq_remove_request()
2416 q->last_merge = NULL; in bfq_remove_request()
2418 if (RB_EMPTY_ROOT(&bfqq->sort_list)) { in bfq_remove_request()
2419 bfqq->next_rq = NULL; in bfq_remove_request()
2421 if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) { in bfq_remove_request()
2425 * bfqq is empty, bfqq->entity.service and in bfq_remove_request()
2426 * bfqq->entity.budget must contain, in bfq_remove_request()
2432 * reset both bfqq->entity.service and in bfq_remove_request()
2433 * bfqq->entity.budget, if bfqq has still a in bfq_remove_request()
2436 bfqq->entity.budget = bfqq->entity.service = 0; in bfq_remove_request()
2440 * Remove queue from request-position tree as it is empty. in bfq_remove_request()
2442 if (bfqq->pos_root) { in bfq_remove_request()
2443 rb_erase(&bfqq->pos_node, bfqq->pos_root); in bfq_remove_request()
2444 bfqq->pos_root = NULL; in bfq_remove_request()
2448 if (unlikely(!bfqd->nonrot_with_queueing)) in bfq_remove_request()
2452 if (rq->cmd_flags & REQ_META) in bfq_remove_request()
2453 bfqq->meta_pending--; in bfq_remove_request()
2460 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_bio_merge()
2465 * queue_lock inside the bfqd->lock. We assume that the bic in bfq_bio_merge()
2467 * bfqd->lock is taken. in bfq_bio_merge()
2472 spin_lock_irq(&bfqd->lock); in bfq_bio_merge()
2481 bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf)); in bfq_bio_merge()
2483 bfqd->bio_bfqq = NULL; in bfq_bio_merge()
2485 bfqd->bio_bic = bic; in bfq_bio_merge()
2489 spin_unlock_irq(&bfqd->lock); in bfq_bio_merge()
2499 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_request_merge()
2518 rb_prev(&req->rb_node) && in bfq_request_merged()
2520 blk_rq_pos(container_of(rb_prev(&req->rb_node), in bfq_request_merged()
2529 bfqd = bfqq->bfqd; in bfq_request_merged()
2532 elv_rb_del(&bfqq->sort_list, req); in bfq_request_merged()
2533 elv_rb_add(&bfqq->sort_list, req); in bfq_request_merged()
2536 prev = bfqq->next_rq; in bfq_request_merged()
2537 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req, in bfq_request_merged()
2538 bfqd->last_position); in bfq_request_merged()
2539 bfqq->next_rq = next_rq; in bfq_request_merged()
2545 if (prev != bfqq->next_rq) { in bfq_request_merged()
2551 if (unlikely(!bfqd->nonrot_with_queueing)) in bfq_request_merged()
2565 * on that rq is picked from the hash table q->elevator->hash, which,
2590 !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && in bfq_requests_merged()
2591 next->fifo_time < rq->fifo_time) { in bfq_requests_merged()
2592 list_del_init(&rq->queuelist); in bfq_requests_merged()
2593 list_replace_init(&next->queuelist, &rq->queuelist); in bfq_requests_merged()
2594 rq->fifo_time = next->fifo_time; in bfq_requests_merged()
2597 if (bfqq->next_rq == next) in bfq_requests_merged()
2598 bfqq->next_rq = rq; in bfq_requests_merged()
2600 bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags); in bfq_requests_merged()
2603 if (!RB_EMPTY_NODE(&next->rb_node)) { in bfq_requests_merged()
2604 bfq_remove_request(next->q, next); in bfq_requests_merged()
2607 next->cmd_flags); in bfq_requests_merged()
2615 * If bfqq has been enjoying interactive weight-raising, then in bfq_bfqq_end_wr()
2618 * a soft real-time application. Such an application actually in bfq_bfqq_end_wr()
2619 * exhibits a soft real-time I/O pattern after it finishes in bfq_bfqq_end_wr()
2628 if (bfqq->wr_cur_max_time != in bfq_bfqq_end_wr()
2629 bfqq->bfqd->bfq_wr_rt_max_time) in bfq_bfqq_end_wr()
2630 bfqq->soft_rt_next_start = jiffies; in bfq_bfqq_end_wr()
2633 bfqq->bfqd->wr_busy_queues--; in bfq_bfqq_end_wr()
2634 bfqq->wr_coeff = 1; in bfq_bfqq_end_wr()
2635 bfqq->wr_cur_max_time = 0; in bfq_bfqq_end_wr()
2636 bfqq->last_wr_start_finish = jiffies; in bfq_bfqq_end_wr()
2641 bfqq->entity.prio_changed = 1; in bfq_bfqq_end_wr()
2651 if (bfqg->async_bfqq[i][j]) in bfq_end_wr_async_queues()
2652 bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]); in bfq_end_wr_async_queues()
2653 if (bfqg->async_idle_bfqq) in bfq_end_wr_async_queues()
2654 bfq_bfqq_end_wr(bfqg->async_idle_bfqq); in bfq_end_wr_async_queues()
2661 spin_lock_irq(&bfqd->lock); in bfq_end_wr()
2663 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) in bfq_end_wr()
2665 list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list) in bfq_end_wr()
2669 spin_unlock_irq(&bfqd->lock); in bfq_end_wr()
2677 return ((struct bio *)io_struct)->bi_iter.bi_sector; in bfq_io_struct_pos()
2683 return abs(bfq_io_struct_pos(io_struct, request) - sector) <= in bfq_rq_close_to_sector()
2691 struct rb_root *root = &bfqq_group(bfqq)->rq_pos_tree; in bfqq_find_close()
2712 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) in bfqq_find_close()
2715 if (blk_rq_pos(__bfqq->next_rq) < sector) in bfqq_find_close()
2716 node = rb_next(&__bfqq->pos_node); in bfqq_find_close()
2718 node = rb_prev(&__bfqq->pos_node); in bfqq_find_close()
2723 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) in bfqq_find_close()
2757 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain in bfq_setup_merge()
2765 while ((__bfqq = new_bfqq->new_bfqq)) { in bfq_setup_merge()
2785 if (new_bfqq->entity.parent != bfqq->entity.parent) in bfq_setup_merge()
2788 bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d", in bfq_setup_merge()
2789 new_bfqq->pid); in bfq_setup_merge()
2803 * not available any more (new_bfqq->bic == NULL). in bfq_setup_merge()
2805 * Anyway, even in case new_bfqq coincides with the in-service in bfq_setup_merge()
2806 * queue, redirecting requests the in-service queue is the in bfq_setup_merge()
2807 * best option, as we feed the in-service queue with new in bfq_setup_merge()
2811 bfqq->new_bfqq = new_bfqq; in bfq_setup_merge()
2816 * associated with new_bfqq. Here we increases new_bfqq->ref in bfq_setup_merge()
2821 new_bfqq->ref += process_refs; in bfq_setup_merge()
2832 (bfqq->ioprio_class != new_bfqq->ioprio_class)) in bfq_may_be_close_cooperator()
2858 * Attempt to schedule a merge of bfqq with the currently in-service
2870 * WARNING: queue merging may impair fairness among non-weight raised
2874 * requests than the ones produced by its originally-associated
2884 if (bfqq->new_bfqq) in bfq_setup_cooperator()
2885 return bfqq->new_bfqq; in bfq_setup_cooperator()
2888 * Check delayed stable merge for rotational or non-queueing in bfq_setup_cooperator()
2890 * currently merged with some other queue (i.e., bfqq->bic in bfq_setup_cooperator()
2893 * merged with bic->stable_merge_bfqq. But this would be in bfq_setup_cooperator()
2896 if (unlikely(!bfqd->nonrot_with_queueing)) { in bfq_setup_cooperator()
2899 * bic->stable_merge_bfqq may point to some queue (for in bfq_setup_cooperator()
2903 if (bfq_bfqq_sync(bfqq) && bic->stable_merge_bfqq && in bfq_setup_cooperator()
2905 time_is_before_jiffies(bfqq->split_time + in bfq_setup_cooperator()
2907 time_is_before_jiffies(bfqq->creation_time + in bfq_setup_cooperator()
2910 bic->stable_merge_bfqq; in bfq_setup_cooperator()
2917 bic->stable_merge_bfqq = NULL; in bfq_setup_cooperator()
2926 bic->stably_merged = true; in bfq_setup_cooperator()
2927 if (new_bfqq->bic) in bfq_setup_cooperator()
2928 new_bfqq->bic->stably_merged = in bfq_setup_cooperator()
2955 * non-merged queues. This may accentuate workload in bfq_setup_cooperator()
2974 if (likely(bfqd->nonrot_with_queueing)) in bfq_setup_cooperator()
2984 * probability that two non-cooperating processes, which just in bfq_setup_cooperator()
2991 if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq)) in bfq_setup_cooperator()
2998 in_service_bfqq = bfqd->in_service_queue; in bfq_setup_cooperator()
3001 likely(in_service_bfqq != &bfqd->oom_bfqq) && in bfq_setup_cooperator()
3003 bfqd->in_serv_last_pos) && in bfq_setup_cooperator()
3004 bfqq->entity.parent == in_service_bfqq->entity.parent && in bfq_setup_cooperator()
3018 if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) && in bfq_setup_cooperator()
3027 struct bfq_io_cq *bic = bfqq->bic; in bfq_bfqq_save_state()
3030 * If !bfqq->bic, the queue is already shared or its requests in bfq_bfqq_save_state()
3037 bic->saved_last_serv_time_ns = bfqq->last_serv_time_ns; in bfq_bfqq_save_state()
3038 bic->saved_inject_limit = bfqq->inject_limit; in bfq_bfqq_save_state()
3039 bic->saved_decrease_time_jif = bfqq->decrease_time_jif; in bfq_bfqq_save_state()
3041 bic->saved_weight = bfqq->entity.orig_weight; in bfq_bfqq_save_state()
3042 bic->saved_ttime = bfqq->ttime; in bfq_bfqq_save_state()
3043 bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq); in bfq_bfqq_save_state()
3044 bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq); in bfq_bfqq_save_state()
3045 bic->saved_io_start_time = bfqq->io_start_time; in bfq_bfqq_save_state()
3046 bic->saved_tot_idle_time = bfqq->tot_idle_time; in bfq_bfqq_save_state()
3047 bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq); in bfq_bfqq_save_state()
3048 bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node); in bfq_bfqq_save_state()
3051 bfqq->bfqd->low_latency)) { in bfq_bfqq_save_state()
3055 * did not make it to be set in a weight-raised state, in bfq_bfqq_save_state()
3057 * weight-raising state that would have been assigned in bfq_bfqq_save_state()
3061 bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff; in bfq_bfqq_save_state()
3062 bic->saved_wr_start_at_switch_to_srt = bfq_smallest_from_now(); in bfq_bfqq_save_state()
3063 bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd); in bfq_bfqq_save_state()
3064 bic->saved_last_wr_start_finish = jiffies; in bfq_bfqq_save_state()
3066 bic->saved_wr_coeff = bfqq->wr_coeff; in bfq_bfqq_save_state()
3067 bic->saved_wr_start_at_switch_to_srt = in bfq_bfqq_save_state()
3068 bfqq->wr_start_at_switch_to_srt; in bfq_bfqq_save_state()
3069 bic->saved_service_from_wr = bfqq->service_from_wr; in bfq_bfqq_save_state()
3070 bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish; in bfq_bfqq_save_state()
3071 bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time; in bfq_bfqq_save_state()
3079 if (cur_bfqq->entity.parent && in bfq_reassign_last_bfqq()
3080 cur_bfqq->entity.parent->last_bfqq_created == cur_bfqq) in bfq_reassign_last_bfqq()
3081 cur_bfqq->entity.parent->last_bfqq_created = new_bfqq; in bfq_reassign_last_bfqq()
3082 else if (cur_bfqq->bfqd && cur_bfqq->bfqd->last_bfqq_created == cur_bfqq) in bfq_reassign_last_bfqq()
3083 cur_bfqq->bfqd->last_bfqq_created = new_bfqq; in bfq_reassign_last_bfqq()
3099 if (bfq_bfqq_busy(bfqq) && RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_release_process_ref()
3100 bfqq != bfqd->in_service_queue) in bfq_release_process_ref()
3113 (unsigned long)new_bfqq->pid); in bfq_merge_bfqqs()
3128 if (bfqq->waker_bfqq && !new_bfqq->waker_bfqq && in bfq_merge_bfqqs()
3129 bfqq->waker_bfqq != new_bfqq) { in bfq_merge_bfqqs()
3130 new_bfqq->waker_bfqq = bfqq->waker_bfqq; in bfq_merge_bfqqs()
3131 new_bfqq->tentative_waker_bfqq = NULL; in bfq_merge_bfqqs()
3135 * new_bfqq->waker_bfqq must be reset. So insert in bfq_merge_bfqqs()
3139 hlist_add_head(&new_bfqq->woken_list_node, in bfq_merge_bfqqs()
3140 &new_bfqq->waker_bfqq->woken_list); in bfq_merge_bfqqs()
3145 * If bfqq is weight-raised, then let new_bfqq inherit in bfq_merge_bfqqs()
3146 * weight-raising. To reduce false positives, neglect the case in bfq_merge_bfqqs()
3148 * to be weight-raised (which may happen because EQM may merge in bfq_merge_bfqqs()
3153 if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) { in bfq_merge_bfqqs()
3154 new_bfqq->wr_coeff = bfqq->wr_coeff; in bfq_merge_bfqqs()
3155 new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time; in bfq_merge_bfqqs()
3156 new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish; in bfq_merge_bfqqs()
3157 new_bfqq->wr_start_at_switch_to_srt = in bfq_merge_bfqqs()
3158 bfqq->wr_start_at_switch_to_srt; in bfq_merge_bfqqs()
3160 bfqd->wr_busy_queues++; in bfq_merge_bfqqs()
3161 new_bfqq->entity.prio_changed = 1; in bfq_merge_bfqqs()
3164 if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */ in bfq_merge_bfqqs()
3165 bfqq->wr_coeff = 1; in bfq_merge_bfqqs()
3166 bfqq->entity.prio_changed = 1; in bfq_merge_bfqqs()
3168 bfqd->wr_busy_queues--; in bfq_merge_bfqqs()
3172 bfqd->wr_busy_queues); in bfq_merge_bfqqs()
3181 * set new_bfqq->bic to NULL. bfqq either: in bfq_merge_bfqqs()
3182 * - does not belong to any bic any more, and hence bfqq->bic must in bfq_merge_bfqqs()
3184 * - is a queue whose owning bics have already been redirected to a in bfq_merge_bfqqs()
3186 * any bic soon and bfqq->bic is already NULL (therefore the next in bfq_merge_bfqqs()
3189 new_bfqq->bic = NULL; in bfq_merge_bfqqs()
3196 * We mark such a queue with a pid -1, and then print SHARED instead of in bfq_merge_bfqqs()
3199 new_bfqq->pid = -1; in bfq_merge_bfqqs()
3200 bfqq->bic = NULL; in bfq_merge_bfqqs()
3210 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_allow_bio_merge()
3211 bool is_sync = op_is_sync(bio->bi_opf); in bfq_allow_bio_merge()
3212 struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq; in bfq_allow_bio_merge()
3231 new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false, bfqd->bio_bic); in bfq_allow_bio_merge()
3240 bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq, in bfq_allow_bio_merge()
3250 * Change also bqfd->bio_bfqq, as in bfq_allow_bio_merge()
3251 * bfqd->bio_bic now points to new_bfqq, and in bfq_allow_bio_merge()
3253 * use again bqfd->bio_bfqq). in bfq_allow_bio_merge()
3255 bfqd->bio_bfqq = bfqq; in bfq_allow_bio_merge()
3262 * Set the maximum time for the in-service queue to consume its
3264 * In practice, a time-slice service scheme is used with seeky
3272 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time) in bfq_set_budget_timeout()
3275 timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight; in bfq_set_budget_timeout()
3277 bfqd->last_budget_start = ktime_get(); in bfq_set_budget_timeout()
3279 bfqq->budget_timeout = jiffies + in bfq_set_budget_timeout()
3280 bfqd->bfq_timeout * timeout_coeff; in bfq_set_budget_timeout()
3289 bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8; in __bfq_set_in_service_queue()
3291 if (time_is_before_jiffies(bfqq->last_wr_start_finish) && in __bfq_set_in_service_queue()
3292 bfqq->wr_coeff > 1 && in __bfq_set_in_service_queue()
3293 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in __bfq_set_in_service_queue()
3294 time_is_before_jiffies(bfqq->budget_timeout)) { in __bfq_set_in_service_queue()
3296 * For soft real-time queues, move the start in __bfq_set_in_service_queue()
3297 * of the weight-raising period forward by the in __bfq_set_in_service_queue()
3301 * weight-raising period of the queue to end, in __bfq_set_in_service_queue()
3303 * weight-raising period of a soft real-time in __bfq_set_in_service_queue()
3306 * because soft real-time queues are not in __bfq_set_in_service_queue()
3319 if (time_after(bfqq->budget_timeout, in __bfq_set_in_service_queue()
3320 bfqq->last_wr_start_finish)) in __bfq_set_in_service_queue()
3321 bfqq->last_wr_start_finish += in __bfq_set_in_service_queue()
3322 jiffies - bfqq->budget_timeout; in __bfq_set_in_service_queue()
3324 bfqq->last_wr_start_finish = jiffies; in __bfq_set_in_service_queue()
3329 "set_in_service_queue, cur-budget = %d", in __bfq_set_in_service_queue()
3330 bfqq->entity.budget); in __bfq_set_in_service_queue()
3333 bfqd->in_service_queue = bfqq; in __bfq_set_in_service_queue()
3334 bfqd->in_serv_last_pos = 0; in __bfq_set_in_service_queue()
3350 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_arm_slice_timer()
3357 * fair distribution of slice time for a process doing back-to-back in bfq_arm_slice_timer()
3360 sl = bfqd->bfq_slice_idle; in bfq_arm_slice_timer()
3362 * Unless the queue is being weight-raised or the scenario is in bfq_arm_slice_timer()
3364 * is seeky. A long idling is preserved for a weight-raised in bfq_arm_slice_timer()
3371 if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 && in bfq_arm_slice_timer()
3374 else if (bfqq->wr_coeff > 1) in bfq_arm_slice_timer()
3377 bfqd->last_idling_start = ktime_get(); in bfq_arm_slice_timer()
3378 bfqd->last_idling_start_jiffies = jiffies; in bfq_arm_slice_timer()
3380 hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl), in bfq_arm_slice_timer()
3389 * budget, even if the in-service queue is served at peak rate. And
3394 return (u64)bfqd->peak_rate * USEC_PER_MSEC * in bfq_calc_max_budget()
3395 jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT; in bfq_calc_max_budget()
3405 if (bfqd->bfq_user_max_budget == 0) { in update_thr_responsiveness_params()
3406 bfqd->bfq_max_budget = in update_thr_responsiveness_params()
3408 bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget); in update_thr_responsiveness_params()
3416 bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns(); in bfq_reset_rate_computation()
3417 bfqd->peak_rate_samples = 1; in bfq_reset_rate_computation()
3418 bfqd->sequential_samples = 0; in bfq_reset_rate_computation()
3419 bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size = in bfq_reset_rate_computation()
3422 bfqd->peak_rate_samples = 0; /* full re-init on next disp. */ in bfq_reset_rate_computation()
3426 bfqd->peak_rate_samples, bfqd->sequential_samples, in bfq_reset_rate_computation()
3427 bfqd->tot_sectors_dispatched); in bfq_reset_rate_computation()
3442 if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES || in bfq_update_rate_reset()
3443 bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL) in bfq_update_rate_reset()
3452 bfqd->delta_from_first = in bfq_update_rate_reset()
3453 max_t(u64, bfqd->delta_from_first, in bfq_update_rate_reset()
3454 bfqd->last_completion - bfqd->first_dispatch); in bfq_update_rate_reset()
3460 rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT, in bfq_update_rate_reset()
3461 div_u64(bfqd->delta_from_first, NSEC_PER_USEC)); in bfq_update_rate_reset()
3465 * - the percentage of sequential dispatches is below 3/4 of the in bfq_update_rate_reset()
3467 * - rate is unreasonably high (> 20M sectors/sec) in bfq_update_rate_reset()
3469 if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 && in bfq_update_rate_reset()
3470 rate <= bfqd->peak_rate) || in bfq_update_rate_reset()
3476 * we use a low-pass filter. We compute the smoothing constant in bfq_update_rate_reset()
3492 * cannot reach 9, because bfqd->sequential_samples cannot in bfq_update_rate_reset()
3493 * become equal to bfqd->peak_rate_samples, which, in its in bfq_update_rate_reset()
3494 * turn, holds true because bfqd->sequential_samples is not in bfq_update_rate_reset()
3497 weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples; in bfq_update_rate_reset()
3504 div_u64(weight * bfqd->delta_from_first, in bfq_update_rate_reset()
3511 divisor = 10 - weight; in bfq_update_rate_reset()
3516 * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor in bfq_update_rate_reset()
3518 bfqd->peak_rate *= divisor-1; in bfq_update_rate_reset()
3519 bfqd->peak_rate /= divisor; in bfq_update_rate_reset()
3522 bfqd->peak_rate += rate; in bfq_update_rate_reset()
3525 * For a very slow device, bfqd->peak_rate can reach 0 (see in bfq_update_rate_reset()
3528 * divisions by zero where bfqd->peak_rate is used as a in bfq_update_rate_reset()
3531 bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate); in bfq_update_rate_reset()
3541 * auto-tuning, see update_thr_responsiveness_params()).
3556 * unknown, namely in-device request service rate.
3575 if (bfqd->peak_rate_samples == 0) { /* first dispatch */ in bfq_update_peak_rate()
3577 bfqd->peak_rate_samples); in bfq_update_peak_rate()
3585 * for computing a new peak rate (similarly to the late- in bfq_update_peak_rate()
3589 * - close the observation interval at the last (previous) in bfq_update_peak_rate()
3591 * - compute rate, if possible, for that observation interval in bfq_update_peak_rate()
3592 * - start a new observation interval with this dispatch in bfq_update_peak_rate()
3594 if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC && in bfq_update_peak_rate()
3595 bfqd->rq_in_driver == 0) in bfq_update_peak_rate()
3599 bfqd->peak_rate_samples++; in bfq_update_peak_rate()
3601 if ((bfqd->rq_in_driver > 0 || in bfq_update_peak_rate()
3602 now_ns - bfqd->last_completion < BFQ_MIN_TT) in bfq_update_peak_rate()
3603 && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq)) in bfq_update_peak_rate()
3604 bfqd->sequential_samples++; in bfq_update_peak_rate()
3606 bfqd->tot_sectors_dispatched += blk_rq_sectors(rq); in bfq_update_peak_rate()
3609 if (likely(bfqd->peak_rate_samples % 32)) in bfq_update_peak_rate()
3610 bfqd->last_rq_max_size = in bfq_update_peak_rate()
3611 max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size); in bfq_update_peak_rate()
3613 bfqd->last_rq_max_size = blk_rq_sectors(rq); in bfq_update_peak_rate()
3615 bfqd->delta_from_first = now_ns - bfqd->first_dispatch; in bfq_update_peak_rate()
3618 if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL) in bfq_update_peak_rate()
3624 bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); in bfq_update_peak_rate()
3625 if (RQ_BFQQ(rq) == bfqd->in_service_queue) in bfq_update_peak_rate()
3626 bfqd->in_serv_last_pos = bfqd->last_position; in bfq_update_peak_rate()
3627 bfqd->last_dispatch = now_ns; in bfq_update_peak_rate()
3643 * dispatch occur for a non in-service bfqq, this anticipated in bfq_dispatch_remove()
3644 * increment prevents two counters related to bfqq->dispatched in bfq_dispatch_remove()
3646 * incremented again when the (new) value of bfqq->dispatched in bfq_dispatch_remove()
3649 bfqq->dispatched++; in bfq_dispatch_remove()
3650 bfq_update_peak_rate(q->elevator->elevator_data, rq); in bfq_dispatch_remove()
3668 * the service order of the internally-queued requests, does
3671 * concern about per-process throughput distribution, and
3672 * makes its decisions only on a per-request basis. Therefore,
3677 * (i-a) each of these processes must get the same throughput as
3679 * (i-b) in case (i-a) does not hold, it holds that the process
3685 * (from I/O-bound to sporadic), and so on;
3691 * distribution if (i-a) holds, or, if (i-b) holds instead, this is an
3696 * idling (I/O-dispatch plugging) is certainly needed to guarantee
3704 * throughput, it is important to check conditions (i-a), i(-b) and
3710 * very difficult to check conditions (i-a) and (i-b) too. In fact,
3711 * if there are active groups, then, for conditions (i-a) or (i-b) to
3713 * contains more active processes or sub-groups than some other active
3714 * group. More precisely, for conditions (i-a) or (i-b) to become
3722 * inactive while still having in-flight requests, and if, when this
3728 * bi-modal behavior, implemented in the function
3735 * conditions (i-a), (i-b) or (ii). So the device is idled for bfqq.
3741 * for completion, then only conditions (i-a) and (i-b) are actually
3742 * controlled, i.e., provided that conditions (i-a) or (i-b) holds,
3744 * holds. In other words, only if conditions (i-a) and (i-b) do not
3748 * control conditions (i-a) and (i-b) it is enough to check just
3761 * can still preempt the new in-service queue if the next
3765 * combined with the hole-recovery heuristic described in the
3772 * minimum of mid-term fairness.
3774 * More precisely, this preemption-based, idleless approach
3798 * We are now left only with explaining the two sub-conditions in the
3801 * sub-condition, we need to add that the function
3803 * non-weight-raised queues, for efficiency reasons (see comments on
3804 * bfq_weights_tree_add()). Then the fact that bfqq is weight-raised
3807 * weight-raised, the scenario is still symmetric if all queues with
3809 * weight-raised. Actually, we should be even more precise here, and
3810 * differentiate between interactive weight raising and soft real-time
3813 * The second sub-condition checked in the compound condition is
3814 * whether there is a fair amount of already in-flight I/O not
3816 * following reason. The drive may decide to serve in-flight
3817 * non-bfqq's I/O requests before bfqq's ones, thereby delaying the
3819 * I/O-dispatching is not plugged, then, while bfqq remains empty, a
3827 * in-flight I/O, and enables bfqq to recover the bandwidth it may
3831 * device-idling countermeasures may however fail in the following
3832 * unlucky scenario: if I/O-dispatch plugging is (correctly) disabled
3833 * in a time period during which all symmetry sub-conditions hold, and
3835 * some later point in time some sub-condition stops to hold, then it
3838 * served. The last sub-condition commented above somewhat mitigates
3839 * this problem for weight-raised queues.
3865 return (bfqq->wr_coeff > 1 && in idling_needed_for_service_guarantees()
3866 (bfqd->wr_busy_queues < in idling_needed_for_service_guarantees()
3868 bfqd->rq_in_driver >= in idling_needed_for_service_guarantees()
3869 bfqq->dispatched + 4)) || in idling_needed_for_service_guarantees()
3894 * not re-scheduled. To prevent this from happening, re-queue in __bfq_bfqq_expire()
3895 * bfqq if it needs I/O-dispatch plugging, even if it is in __bfq_bfqq_expire()
3899 if (RB_EMPTY_ROOT(&bfqq->sort_list) && in __bfq_bfqq_expire()
3902 if (bfqq->dispatched == 0) in __bfq_bfqq_expire()
3907 * the weight-raising mechanism. in __bfq_bfqq_expire()
3909 bfqq->budget_timeout = jiffies; in __bfq_bfqq_expire()
3918 if (unlikely(!bfqd->nonrot_with_queueing && in __bfq_bfqq_expire()
3919 !RB_EMPTY_ROOT(&bfqq->sort_list))) in __bfq_bfqq_expire()
3924 * All in-service entities must have been properly deactivated in __bfq_bfqq_expire()
3926 * resets all in-service entities as no more in service. This in __bfq_bfqq_expire()
3934 * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
3951 if (bfqq->wr_coeff == 1) in __bfq_bfqq_recalc_budget()
3952 budget = bfqq->max_budget; in __bfq_bfqq_recalc_budget()
3954 * Use a constant, low budget for weight-raised queues, in __bfq_bfqq_recalc_budget()
3962 bfqq->entity.budget, bfq_bfqq_budget_left(bfqq)); in __bfq_bfqq_recalc_budget()
3966 bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue)); in __bfq_bfqq_recalc_budget()
3968 if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) { in __bfq_bfqq_recalc_budget()
3999 if (bfqq->dispatched > 0) /* still outstanding reqs */ in __bfq_bfqq_recalc_budget()
4000 budget = min(budget * 2, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
4003 budget -= 4 * min_budget; in __bfq_bfqq_recalc_budget()
4015 budget = min(budget * 2, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
4027 budget = min(budget * 4, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
4041 * back-shifting. The larger the budget of the in __bfq_bfqq_recalc_budget()
4050 * many re-activations a lower finish time in __bfq_bfqq_recalc_budget()
4054 * quite precisely by bfqq->entity.service. in __bfq_bfqq_recalc_budget()
4056 * bfqq->entity.service is equal to the number in __bfq_bfqq_recalc_budget()
4062 budget = max_t(int, bfqq->entity.service, min_budget); in __bfq_bfqq_recalc_budget()
4074 budget = bfqd->bfq_max_budget; in __bfq_bfqq_recalc_budget()
4077 bfqq->max_budget = budget; in __bfq_bfqq_recalc_budget()
4079 if (bfqd->budgets_assigned >= bfq_stats_min_budgets && in __bfq_bfqq_recalc_budget()
4080 !bfqd->bfq_user_max_budget) in __bfq_bfqq_recalc_budget()
4081 bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
4093 next_rq = bfqq->next_rq; in __bfq_bfqq_recalc_budget()
4095 bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget, in __bfq_bfqq_recalc_budget()
4100 bfqq->entity.budget); in __bfq_bfqq_recalc_budget()
4123 * service slots. On the opposite end, the requests of the in-service
4146 delta_ktime = bfqd->last_idling_start; in bfq_bfqq_is_slow()
4149 delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start); in bfq_bfqq_is_slow()
4154 if (blk_queue_nonrot(bfqd->queue)) in bfq_bfqq_is_slow()
4156 * give same worst-case guarantees as idling in bfq_bfqq_is_slow()
4183 slow = bfqq->entity.service < bfqd->bfq_max_budget / 2; in bfq_bfqq_is_slow()
4192 * To be deemed as soft real-time, an application must meet two
4195 * record a compressed high-definition video.
4197 * batch, to compute the next-start time instant, soft_rt_next_start, such
4211 * Unfortunately, even a greedy (i.e., I/O-bound) application may
4218 * device: the storage device is highly loaded or reaches a low-enough
4223 * that greedy applications are deemed as soft real-time in these
4230 * namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
4232 * jiffies; we get back to it after next item (b). Lower-bounding
4234 * bfqd->bfq_slice_idle tends to filter out greedy applications,
4237 * real-time application spends some time processing data, after a
4240 * (b) Current value of bfqq->soft_rt_next_start. As pointed out
4243 * storage-device load. In more detail, in these scenarios, these
4246 * including the filtering in above item (a). These slow-speed
4250 * I/O in the high-speed intervals, the values returned by this
4252 * high-speed interval, to be likely to fall *after* the end of
4253 * the low-speed time interval that follows. These high values are
4254 * stored in bfqq->soft_rt_next_start after each invocation of
4256 * bfqq->soft_rt_next_start is constantly used to lower-bound the
4258 * beginning of a low-speed interval, bfqq->soft_rt_next_start is
4260 * issued during the low-speed interval is considered as arriving
4262 * real-time. Then, in the high-speed interval that follows, the
4263 * application will not be deemed as soft real-time, just because
4269 * bfqd->bfq_slice_idle:
4271 * higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
4273 * that the approximation, in jiffies, of bfqd->bfq_slice_idle
4279 * reference time interval just bfqd->bfq_slice_idle, but
4280 * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
4287 return max3(bfqq->soft_rt_next_start, in bfq_bfqq_softrt_next_start()
4288 bfqq->last_idle_bklogged + in bfq_bfqq_softrt_next_start()
4289 HZ * bfqq->service_from_backlogged / in bfq_bfqq_softrt_next_start()
4290 bfqd->bfq_wr_max_softrt_rate, in bfq_bfqq_softrt_next_start()
4291 jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4); in bfq_bfqq_softrt_next_start()
4295 * bfq_bfqq_expire - expire a queue.
4310 * tends to lower the throughput). In addition, this time-charging
4327 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_expire()
4336 * timed-out queues with the time and not the service in bfq_bfqq_expire()
4347 * or quasi-sequential processes. in bfq_bfqq_expire()
4349 if (bfqq->wr_coeff == 1 && in bfq_bfqq_expire()
4352 bfq_bfqq_budget_left(bfqq) >= entity->budget / 3))) in bfq_bfqq_expire()
4355 if (bfqd->low_latency && bfqq->wr_coeff == 1) in bfq_bfqq_expire()
4356 bfqq->last_wr_start_finish = jiffies; in bfq_bfqq_expire()
4358 if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 && in bfq_bfqq_expire()
4359 RB_EMPTY_ROOT(&bfqq->sort_list)) { in bfq_bfqq_expire()
4372 if (bfqq->dispatched == 0) in bfq_bfqq_expire()
4373 bfqq->soft_rt_next_start = in bfq_bfqq_expire()
4375 else if (bfqq->dispatched > 0) { in bfq_bfqq_expire()
4386 slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq)); in bfq_bfqq_expire()
4393 bfqd->rqs_injected = bfqd->wait_dispatch = false; in bfq_bfqq_expire()
4394 bfqd->waited_rq = NULL; in bfq_bfqq_expire()
4416 entity->service = 0; in bfq_bfqq_expire()
4419 * Reset the received-service counter for every parent entity. in bfq_bfqq_expire()
4420 * Differently from what happens with bfqq->entity.service, in bfq_bfqq_expire()
4424 * consumed budget, bfqq->entity.service needs to be kept, in bfq_bfqq_expire()
4426 * the same budget, the last value of bfqq->entity.service is in bfq_bfqq_expire()
4427 * needed to properly decrement bfqq->entity.budget by the in bfq_bfqq_expire()
4429 * to keep entity->service for parent entities too, because in bfq_bfqq_expire()
4430 * the bubble up of the new value of bfqq->entity.budget will in bfq_bfqq_expire()
4435 entity = entity->parent; in bfq_bfqq_expire()
4437 entity->service = 0; in bfq_bfqq_expire()
4447 return time_is_before_eq_jiffies(bfqq->budget_timeout); in bfq_bfqq_budget_timeout()
4460 bfq_log_bfqq(bfqq->bfqd, bfqq, in bfq_may_expire_for_budg_timeout()
4463 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3, in bfq_may_expire_for_budg_timeout()
4467 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3) in bfq_may_expire_for_budg_timeout()
4476 !blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag, in idling_boosts_thr_without_issues()
4493 * (a) the device is not NCQ-capable and rotational, or in idling_boosts_thr_without_issues()
4495 * the request pattern for bfqq is I/O-bound and sequential, or in idling_boosts_thr_without_issues()
4497 * not NCQ-capable and the request pattern for bfqq is in idling_boosts_thr_without_issues()
4498 * I/O-bound and sequential. in idling_boosts_thr_without_issues()
4501 * NCQ-capable flash-based device would not boost the in idling_boosts_thr_without_issues()
4506 * particular, happens to be false if bfqd is an NCQ-capable in idling_boosts_thr_without_issues()
4507 * flash-based device. in idling_boosts_thr_without_issues()
4510 ((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) && in idling_boosts_thr_without_issues()
4517 * weight-raised queues. in idling_boosts_thr_without_issues()
4521 * non-weight-raised queues ask for requests at a lower rate, in idling_boosts_thr_without_issues()
4522 * then processes associated with weight-raised queues have a in idling_boosts_thr_without_issues()
4527 * weight. This is especially true with NCQ-capable drives, in idling_boosts_thr_without_issues()
4529 * reorder internally-queued requests. in idling_boosts_thr_without_issues()
4532 * there are weight-raised busy queues. In this case, and if in idling_boosts_thr_without_issues()
4533 * bfqq is not weight-raised, this guarantees that the device in idling_boosts_thr_without_issues()
4534 * is not idled for bfqq (if, instead, bfqq is weight-raised, in idling_boosts_thr_without_issues()
4538 * sync non-weight-raised queue, to get a lower number of in idling_boosts_thr_without_issues()
4541 * weight-raised queues get served again. This often mitigates in idling_boosts_thr_without_issues()
4548 bfqd->wr_busy_queues == 0; in idling_boosts_thr_without_issues()
4562 * NCQ-capable devices, this function tries to return false, so as to
4564 * device boost the throughput without causing any service-guarantee
4574 struct bfq_data *bfqd = bfqq->bfqd; in bfq_better_to_idle()
4581 if (unlikely(bfqd->strict_guarantees)) in bfq_better_to_idle()
4590 * queues in this class can steal to higher-priority queues in bfq_better_to_idle()
4592 if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) || in bfq_better_to_idle()
4613 * If the in-service queue is empty but the function bfq_better_to_idle
4625 return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq); in bfq_bfqq_must_idle()
4638 struct bfq_queue *bfqq, *in_serv_bfqq = bfqd->in_service_queue; in bfq_choose_bfqq_for_injection()
4639 unsigned int limit = in_serv_bfqq->inject_limit; in bfq_choose_bfqq_for_injection()
4642 * - bfqq is not weight-raised and therefore does not carry in bfq_choose_bfqq_for_injection()
4643 * time-critical I/O, in bfq_choose_bfqq_for_injection()
4645 * - regardless of whether bfqq is weight-raised, bfqq has in bfq_choose_bfqq_for_injection()
4652 bool in_serv_always_inject = in_serv_bfqq->wr_coeff == 1 || in bfq_choose_bfqq_for_injection()
4657 * - the baseline total service time could not be sampled yet, in bfq_choose_bfqq_for_injection()
4659 * - a lot of time has elapsed since the plugging of I/O in bfq_choose_bfqq_for_injection()
4664 if (limit == 0 && in_serv_bfqq->last_serv_time_ns == 0 && in bfq_choose_bfqq_for_injection()
4666 time_is_before_eq_jiffies(bfqd->last_idling_start_jiffies + in bfq_choose_bfqq_for_injection()
4667 bfqd->bfq_slice_idle) in bfq_choose_bfqq_for_injection()
4671 if (bfqd->rq_in_driver >= limit) in bfq_choose_bfqq_for_injection()
4679 * - BFQ dynamically updates the budget of every queue so as in bfq_choose_bfqq_for_injection()
4681 * - if a queue gets all its requests dispatched as injected in bfq_choose_bfqq_for_injection()
4683 * (and re-added only if it gets new requests, but then it in bfq_choose_bfqq_for_injection()
4686 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) in bfq_choose_bfqq_for_injection()
4687 if (!RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_choose_bfqq_for_injection()
4688 (in_serv_always_inject || bfqq->wr_coeff > 1) && in bfq_choose_bfqq_for_injection()
4689 bfq_serv_to_charge(bfqq->next_rq, bfqq) <= in bfq_choose_bfqq_for_injection()
4692 * Allow for only one large in-flight request in bfq_choose_bfqq_for_injection()
4693 * on non-rotational devices, for the in bfq_choose_bfqq_for_injection()
4694 * following reason. On non-rotationl drives, in bfq_choose_bfqq_for_injection()
4701 * request of the in-service queue wait for so in bfq_choose_bfqq_for_injection()
4705 * there is only one in-flight large request in bfq_choose_bfqq_for_injection()
4708 if (blk_queue_nonrot(bfqd->queue) && in bfq_choose_bfqq_for_injection()
4709 blk_rq_sectors(bfqq->next_rq) >= in bfq_choose_bfqq_for_injection()
4713 limit = in_serv_bfqq->inject_limit; in bfq_choose_bfqq_for_injection()
4715 if (bfqd->rq_in_driver < limit) { in bfq_choose_bfqq_for_injection()
4716 bfqd->rqs_injected = true; in bfq_choose_bfqq_for_injection()
4734 bfqq = bfqd->in_service_queue; in bfq_select_queue()
4738 bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue"); in bfq_select_queue()
4754 * happens, it is much more convenient to re-execute this loop in bfq_select_queue()
4758 next_rq = bfqq->next_rq; in bfq_select_queue()
4795 hrtimer_try_to_cancel(&bfqd->idle_slice_timer); in bfq_select_queue()
4802 * No requests pending. However, if the in-service queue is idling in bfq_select_queue()
4810 (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) { in bfq_select_queue()
4812 bfqq->bic && bfqq->bic->bfqq[0] && in bfq_select_queue()
4813 bfq_bfqq_busy(bfqq->bic->bfqq[0]) && in bfq_select_queue()
4814 bfqq->bic->bfqq[0]->next_rq ? in bfq_select_queue()
4815 bfqq->bic->bfqq[0] : NULL; in bfq_select_queue()
4817 !hlist_empty(&bfqq->woken_list) ? in bfq_select_queue()
4818 container_of(bfqq->woken_list.first, in bfq_select_queue()
4824 * The next four mutually-exclusive ifs decide in bfq_select_queue()
4836 * non-empty waker queue for bfqq, i.e., a queue whose in bfq_select_queue()
4887 * I/O-plugging timeout fires. So one may deem the in bfq_select_queue()
4904 icq_to_bic(async_bfqq->next_rq->elv.icq) == bfqq->bic && in bfq_select_queue()
4905 bfq_serv_to_charge(async_bfqq->next_rq, async_bfqq) <= in bfq_select_queue()
4907 bfqq = bfqq->bic->bfqq[0]; in bfq_select_queue()
4908 else if (bfqq->waker_bfqq && in bfq_select_queue()
4909 bfq_bfqq_busy(bfqq->waker_bfqq) && in bfq_select_queue()
4910 bfqq->waker_bfqq->next_rq && in bfq_select_queue()
4911 bfq_serv_to_charge(bfqq->waker_bfqq->next_rq, in bfq_select_queue()
4912 bfqq->waker_bfqq) <= in bfq_select_queue()
4913 bfq_bfqq_budget_left(bfqq->waker_bfqq) in bfq_select_queue()
4915 bfqq = bfqq->waker_bfqq; in bfq_select_queue()
4918 blocked_bfqq->next_rq && in bfq_select_queue()
4919 bfq_serv_to_charge(blocked_bfqq->next_rq, in bfq_select_queue()
4925 (bfqq->wr_coeff == 1 || bfqd->wr_busy_queues > 1 || in bfq_select_queue()
4954 struct bfq_entity *entity = &bfqq->entity; in bfq_update_wr_data()
4956 if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */ in bfq_update_wr_data()
4959 jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish), in bfq_update_wr_data()
4960 jiffies_to_msecs(bfqq->wr_cur_max_time), in bfq_update_wr_data()
4961 bfqq->wr_coeff, in bfq_update_wr_data()
4962 bfqq->entity.weight, bfqq->entity.orig_weight); in bfq_update_wr_data()
4964 if (entity->prio_changed) in bfq_update_wr_data()
4970 * weight-raising period, then end weight raising. in bfq_update_wr_data()
4974 else if (time_is_before_jiffies(bfqq->last_wr_start_finish + in bfq_update_wr_data()
4975 bfqq->wr_cur_max_time)) { in bfq_update_wr_data()
4976 if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time || in bfq_update_wr_data()
4977 time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_update_wr_data()
4983 * interactive-weight-raising period in bfq_update_wr_data()
4994 bfqq->entity.prio_changed = 1; in bfq_update_wr_data()
4997 if (bfqq->wr_coeff > 1 && in bfq_update_wr_data()
4998 bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time && in bfq_update_wr_data()
4999 bfqq->service_from_wr > max_service_from_wr) { in bfq_update_wr_data()
5012 if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1)) in bfq_update_wr_data()
5023 struct request *rq = bfqq->next_rq; in bfq_dispatch_rq_from_bfqq()
5030 if (bfqq == bfqd->in_service_queue && bfqd->wait_dispatch) { in bfq_dispatch_rq_from_bfqq()
5031 bfqd->wait_dispatch = false; in bfq_dispatch_rq_from_bfqq()
5032 bfqd->waited_rq = rq; in bfq_dispatch_rq_from_bfqq()
5035 bfq_dispatch_remove(bfqd->queue, rq); in bfq_dispatch_rq_from_bfqq()
5037 if (bfqq != bfqd->in_service_queue) in bfq_dispatch_rq_from_bfqq()
5045 * weight-raised during this service slot, even if it has in bfq_dispatch_rq_from_bfqq()
5047 * weight-raised queue. This inflates bfqq's timestamps, which in bfq_dispatch_rq_from_bfqq()
5049 * device immediately to possible other weight-raised queues. in bfq_dispatch_rq_from_bfqq()
5069 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_has_work()
5072 * Avoiding lock: a race on bfqd->queued should cause at in bfq_has_work()
5075 return !list_empty_careful(&bfqd->dispatch) || in bfq_has_work()
5076 READ_ONCE(bfqd->queued); in bfq_has_work()
5081 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in __bfq_dispatch_request()
5085 if (!list_empty(&bfqd->dispatch)) { in __bfq_dispatch_request()
5086 rq = list_first_entry(&bfqd->dispatch, struct request, in __bfq_dispatch_request()
5088 list_del_init(&rq->queuelist); in __bfq_dispatch_request()
5099 bfqq->dispatched++; in __bfq_dispatch_request()
5124 * being the frequency of non-elevator-private in __bfq_dispatch_request()
5148 if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0) in __bfq_dispatch_request()
5159 bfqd->rq_in_driver++; in __bfq_dispatch_request()
5161 rq->rq_flags |= RQF_STARTED; in __bfq_dispatch_request()
5191 spin_lock_irq(&q->queue_lock); in bfq_update_dispatch_stats()
5208 bfqg_stats_update_io_remove(bfqg, rq->cmd_flags); in bfq_update_dispatch_stats()
5210 spin_unlock_irq(&q->queue_lock); in bfq_update_dispatch_stats()
5221 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_dispatch_request()
5226 spin_lock_irq(&bfqd->lock); in bfq_dispatch_request()
5228 in_serv_queue = bfqd->in_service_queue; in bfq_dispatch_request()
5232 if (in_serv_queue == bfqd->in_service_queue) { in bfq_dispatch_request()
5237 spin_unlock_irq(&bfqd->lock); in bfq_dispatch_request()
5238 bfq_update_dispatch_stats(hctx->queue, rq, in bfq_dispatch_request()
5247 * in-flight on this queue also holds a reference, dropped when rq is freed.
5258 bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d", bfqq, bfqq->ref); in bfq_put_queue()
5260 bfqq->ref--; in bfq_put_queue()
5261 if (bfqq->ref) in bfq_put_queue()
5264 if (!hlist_unhashed(&bfqq->burst_list_node)) { in bfq_put_queue()
5265 hlist_del_init(&bfqq->burst_list_node); in bfq_put_queue()
5271 * bursts, when some short-lived process (often due to in bfq_put_queue()
5286 * the current burst list--without incrementing in bfq_put_queue()
5287 * bust_size--because of a split, but the current in bfq_put_queue()
5292 if (bfqq->bic && bfqq->bfqd->burst_size > 0) in bfq_put_queue()
5293 bfqq->bfqd->burst_size--; in bfq_put_queue()
5312 if (!hlist_unhashed(&bfqq->woken_list_node)) in bfq_put_queue()
5313 hlist_del_init(&bfqq->woken_list_node); in bfq_put_queue()
5316 hlist_for_each_entry_safe(item, n, &bfqq->woken_list, in bfq_put_queue()
5318 item->waker_bfqq = NULL; in bfq_put_queue()
5319 hlist_del_init(&item->woken_list_node); in bfq_put_queue()
5322 if (bfqq->bfqd->last_completed_rq_bfqq == bfqq) in bfq_put_queue()
5323 bfqq->bfqd->last_completed_rq_bfqq = NULL; in bfq_put_queue()
5331 bfqq->stable_ref--; in bfq_put_stable_ref()
5344 __bfqq = bfqq->new_bfqq; in bfq_put_cooperator()
5348 next = __bfqq->new_bfqq; in bfq_put_cooperator()
5356 if (bfqq == bfqd->in_service_queue) { in bfq_exit_bfqq()
5361 bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref); in bfq_exit_bfqq()
5374 bfqd = bfqq->bfqd; /* NULL if scheduler already exited */ in bfq_exit_icq_bfqq()
5379 spin_lock_irqsave(&bfqd->lock, flags); in bfq_exit_icq_bfqq()
5380 bfqq->bic = NULL; in bfq_exit_icq_bfqq()
5383 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_exit_icq_bfqq()
5391 if (bic->stable_merge_bfqq) { in bfq_exit_icq()
5392 struct bfq_data *bfqd = bic->stable_merge_bfqq->bfqd; in bfq_exit_icq()
5401 spin_lock_irqsave(&bfqd->lock, flags); in bfq_exit_icq()
5402 bfq_put_stable_ref(bic->stable_merge_bfqq); in bfq_exit_icq()
5403 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_exit_icq()
5405 bfq_put_stable_ref(bic->stable_merge_bfqq); in bfq_exit_icq()
5422 struct bfq_data *bfqd = bfqq->bfqd; in bfq_set_next_ioprio_data()
5427 ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); in bfq_set_next_ioprio_data()
5431 bdi_dev_name(bfqq->bfqd->queue->disk->bdi), in bfq_set_next_ioprio_data()
5438 bfqq->new_ioprio = task_nice_ioprio(tsk); in bfq_set_next_ioprio_data()
5439 bfqq->new_ioprio_class = task_nice_ioclass(tsk); in bfq_set_next_ioprio_data()
5442 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_set_next_ioprio_data()
5443 bfqq->new_ioprio_class = IOPRIO_CLASS_RT; in bfq_set_next_ioprio_data()
5446 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_set_next_ioprio_data()
5447 bfqq->new_ioprio_class = IOPRIO_CLASS_BE; in bfq_set_next_ioprio_data()
5450 bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE; in bfq_set_next_ioprio_data()
5451 bfqq->new_ioprio = 7; in bfq_set_next_ioprio_data()
5455 if (bfqq->new_ioprio >= IOPRIO_NR_LEVELS) { in bfq_set_next_ioprio_data()
5457 bfqq->new_ioprio); in bfq_set_next_ioprio_data()
5458 bfqq->new_ioprio = IOPRIO_NR_LEVELS - 1; in bfq_set_next_ioprio_data()
5461 bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio); in bfq_set_next_ioprio_data()
5463 bfqq->new_ioprio, bfqq->entity.new_weight); in bfq_set_next_ioprio_data()
5464 bfqq->entity.prio_changed = 1; in bfq_set_next_ioprio_data()
5476 int ioprio = bic->icq.ioc->ioprio; in bfq_check_ioprio_change()
5482 if (unlikely(!bfqd) || likely(bic->ioprio == ioprio)) in bfq_check_ioprio_change()
5485 bic->ioprio = ioprio; in bfq_check_ioprio_change()
5504 RB_CLEAR_NODE(&bfqq->entity.rb_node); in bfq_init_bfqq()
5505 INIT_LIST_HEAD(&bfqq->fifo); in bfq_init_bfqq()
5506 INIT_HLIST_NODE(&bfqq->burst_list_node); in bfq_init_bfqq()
5507 INIT_HLIST_NODE(&bfqq->woken_list_node); in bfq_init_bfqq()
5508 INIT_HLIST_HEAD(&bfqq->woken_list); in bfq_init_bfqq()
5510 bfqq->ref = 0; in bfq_init_bfqq()
5511 bfqq->bfqd = bfqd; in bfq_init_bfqq()
5531 bfqq->ttime.last_end_request = now_ns + 1; in bfq_init_bfqq()
5533 bfqq->creation_time = jiffies; in bfq_init_bfqq()
5535 bfqq->io_start_time = now_ns; in bfq_init_bfqq()
5539 bfqq->pid = pid; in bfq_init_bfqq()
5542 bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3; in bfq_init_bfqq()
5543 bfqq->budget_timeout = bfq_smallest_from_now(); in bfq_init_bfqq()
5545 bfqq->wr_coeff = 1; in bfq_init_bfqq()
5546 bfqq->last_wr_start_finish = jiffies; in bfq_init_bfqq()
5547 bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now(); in bfq_init_bfqq()
5548 bfqq->split_time = bfq_smallest_from_now(); in bfq_init_bfqq()
5554 * to the current value of bfqq->soft_rt_next_start (see in bfq_init_bfqq()
5559 bfqq->soft_rt_next_start = jiffies; in bfq_init_bfqq()
5562 bfqq->seek_history = 1; in bfq_init_bfqq()
5571 return &bfqg->async_bfqq[0][ioprio]; in bfq_async_queue_prio()
5576 return &bfqg->async_bfqq[1][ioprio]; in bfq_async_queue_prio()
5578 return &bfqg->async_idle_bfqq; in bfq_async_queue_prio()
5595 if (new_bfqq->bic) in bfq_do_early_stable_merge()
5596 new_bfqq->bic->stably_merged = true; in bfq_do_early_stable_merge()
5597 bic->stably_merged = true; in bfq_do_early_stable_merge()
5601 * bfqq->bic must be set too, for in bfq_do_early_stable_merge()
5605 bfqq->bic = bic; in bfq_do_early_stable_merge()
5612 * Many throughput-sensitive workloads are made of several parallel
5619 * To avoid this plugging, BFQ has been using a burst-handling
5628 * throughput of the flows and task-wide I/O latency. In particular,
5644 * - very little time has elapsed since when Q1 was created
5645 * - Q2 has the same ioprio as Q1
5646 * - Q2 belongs to the same group as Q1
5651 * the total per-request processing time, the above throughput boost
5655 * burst-handling heuristics. We keep those heuristics for the moment.
5661 struct bfq_queue **source_bfqq = bfqq->entity.parent ? in bfq_do_or_sched_stable_merge()
5662 &bfqq->entity.parent->last_bfqq_created : in bfq_do_or_sched_stable_merge()
5663 &bfqd->last_bfqq_created; in bfq_do_or_sched_stable_merge()
5678 * throughput-beneficial if not merged. Currently this is in bfq_do_or_sched_stable_merge()
5687 time_before(last_bfqq_created->creation_time + in bfq_do_or_sched_stable_merge()
5689 bfqq->creation_time) || in bfq_do_or_sched_stable_merge()
5690 bfqq->entity.parent != last_bfqq_created->entity.parent || in bfq_do_or_sched_stable_merge()
5691 bfqq->ioprio != last_bfqq_created->ioprio || in bfq_do_or_sched_stable_merge()
5692 bfqq->ioprio_class != last_bfqq_created->ioprio_class) in bfq_do_or_sched_stable_merge()
5694 else if (time_after_eq(last_bfqq_created->creation_time + in bfq_do_or_sched_stable_merge()
5695 bfqd->bfq_burst_interval, in bfq_do_or_sched_stable_merge()
5696 bfqq->creation_time)) { in bfq_do_or_sched_stable_merge()
5697 if (likely(bfqd->nonrot_with_queueing)) in bfq_do_or_sched_stable_merge()
5713 last_bfqq_created->ref++; in bfq_do_or_sched_stable_merge()
5718 last_bfqq_created->stable_ref++; in bfq_do_or_sched_stable_merge()
5722 bic->stable_merge_bfqq = last_bfqq_created; in bfq_do_or_sched_stable_merge()
5735 const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_get_queue()
5736 const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); in bfq_get_queue()
5752 bfqd->queue->node); in bfq_get_queue()
5755 bfq_init_bfqq(bfqd, bfqq, bic, current->pid, in bfq_get_queue()
5757 bfq_init_entity(&bfqq->entity, bfqg); in bfq_get_queue()
5760 bfqq = &bfqd->oom_bfqq; in bfq_get_queue()
5770 bfqq->ref++; /* in bfq_get_queue()
5773 * only if bfqq->bfqg disappears, to in bfq_get_queue()
5778 bfqq, bfqq->ref); in bfq_get_queue()
5783 bfqq->ref++; /* get a process reference to this queue */ in bfq_get_queue()
5785 if (bfqq != &bfqd->oom_bfqq && is_sync && !respawn) in bfq_get_queue()
5793 struct bfq_ttime *ttime = &bfqq->ttime; in bfq_update_io_thinktime()
5801 if (bfqq->dispatched || bfq_bfqq_busy(bfqq)) in bfq_update_io_thinktime()
5803 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request; in bfq_update_io_thinktime()
5804 elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle); in bfq_update_io_thinktime()
5806 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; in bfq_update_io_thinktime()
5807 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8); in bfq_update_io_thinktime()
5808 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128, in bfq_update_io_thinktime()
5809 ttime->ttime_samples); in bfq_update_io_thinktime()
5816 bfqq->seek_history <<= 1; in bfq_update_io_seektime()
5817 bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq); in bfq_update_io_seektime()
5819 if (bfqq->wr_coeff > 1 && in bfq_update_io_seektime()
5820 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in bfq_update_io_seektime()
5822 if (time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_update_io_seektime()
5826 * interactive-weight-raising period in bfq_update_io_seektime()
5838 bfqq->entity.prio_changed = 1; in bfq_update_io_seektime()
5855 bfqd->bfq_slice_idle == 0) in bfq_update_has_short_ttime()
5859 if (time_is_after_eq_jiffies(bfqq->split_time + in bfq_update_has_short_ttime()
5860 bfqd->bfq_wr_min_idle_time)) in bfq_update_has_short_ttime()
5866 * think time with half the I/O-plugging timeout. in bfq_update_has_short_ttime()
5868 if (atomic_read(&bic->icq.ioc->active_ref) == 0 || in bfq_update_has_short_ttime()
5869 (bfq_sample_valid(bfqq->ttime.ttime_samples) && in bfq_update_has_short_ttime()
5870 bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle>>1)) in bfq_update_has_short_ttime()
5883 * the think-time state (short|long). In particular, the limit in bfq_update_has_short_ttime()
5887 * instructions reset the inject limit if the think-time state in bfq_update_has_short_ttime()
5906 * I/O-dispatch-plugging, then bfqq remains empty, and no I/O in bfq_update_has_short_ttime()
5911 * On the opposite end, a non-zero inject limit may allow the in bfq_update_has_short_ttime()
5916 * next think-time sample for bfqq may be very low. This in in bfq_update_has_short_ttime()
5924 * of such a steady oscillation between the two think-time in bfq_update_has_short_ttime()
5950 * more frequently than once per I/O-plugging timeout, makes in bfq_update_has_short_ttime()
5964 if (state_changed && bfqq->last_serv_time_ns == 0 && in bfq_update_has_short_ttime()
5965 (time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_update_has_short_ttime()
5978 if (rq->cmd_flags & REQ_META) in bfq_rq_enqueued()
5979 bfqq->meta_pending++; in bfq_rq_enqueued()
5981 bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); in bfq_rq_enqueued()
5983 if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) { in bfq_rq_enqueued()
5984 bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 && in bfq_rq_enqueued()
5990 * - the request is small, and in bfq_rq_enqueued()
5991 * - we are idling to boost throughput, and in bfq_rq_enqueued()
5992 * - the queue is not to be expired, in bfq_rq_enqueued()
5996 * for a new request from the in-service queue, we in bfq_rq_enqueued()
6016 hrtimer_try_to_cancel(&bfqd->idle_slice_timer); in bfq_rq_enqueued()
6033 struct bfq_entity *entity = &bfqq->entity; in bfqq_request_allocated()
6036 entity->allocated++; in bfqq_request_allocated()
6041 struct bfq_entity *entity = &bfqq->entity; in bfqq_request_freed()
6044 entity->allocated--; in bfqq_request_freed()
6062 new_bfqq->ref++; in __bfq_insert_request()
6081 rq->elv.priv[1] = new_bfqq; in __bfq_insert_request()
6093 rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)]; in __bfq_insert_request()
6094 list_add_tail(&rq->queuelist, &bfqq->fifo); in __bfq_insert_request()
6120 spin_lock_irq(&q->queue_lock); in bfq_update_insert_stats()
6124 spin_unlock_irq(&q->queue_lock); in bfq_update_insert_stats()
6138 struct request_queue *q = hctx->queue; in bfq_insert_request()
6139 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_insert_request()
6146 if (!cgroup_subsys_on_dfl(io_cgrp_subsys) && rq->bio) in bfq_insert_request()
6149 spin_lock_irq(&bfqd->lock); in bfq_insert_request()
6152 spin_unlock_irq(&bfqd->lock); in bfq_insert_request()
6161 list_add(&rq->queuelist, &bfqd->dispatch); in bfq_insert_request()
6163 list_add_tail(&rq->queuelist, &bfqd->dispatch); in bfq_insert_request()
6175 if (!q->last_merge) in bfq_insert_request()
6176 q->last_merge = rq; in bfq_insert_request()
6185 cmd_flags = rq->cmd_flags; in bfq_insert_request()
6186 spin_unlock_irq(&bfqd->lock); in bfq_insert_request()
6199 list_del_init(&rq->queuelist); in bfq_insert_requests()
6206 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_update_hw_tag()
6208 bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver, in bfq_update_hw_tag()
6209 bfqd->rq_in_driver); in bfq_update_hw_tag()
6211 if (bfqd->hw_tag == 1) in bfq_update_hw_tag()
6220 if (bfqd->rq_in_driver + bfqd->queued <= BFQ_HW_QUEUE_THRESHOLD) in bfq_update_hw_tag()
6229 bfqq->dispatched + bfqq->queued[0] + bfqq->queued[1] < in bfq_update_hw_tag()
6231 bfqd->rq_in_driver < BFQ_HW_QUEUE_THRESHOLD) in bfq_update_hw_tag()
6234 if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES) in bfq_update_hw_tag()
6237 bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD; in bfq_update_hw_tag()
6238 bfqd->max_rq_in_driver = 0; in bfq_update_hw_tag()
6239 bfqd->hw_tag_samples = 0; in bfq_update_hw_tag()
6241 bfqd->nonrot_with_queueing = in bfq_update_hw_tag()
6242 blk_queue_nonrot(bfqd->queue) && bfqd->hw_tag; in bfq_update_hw_tag()
6252 bfqd->rq_in_driver--; in bfq_completed_request()
6253 bfqq->dispatched--; in bfq_completed_request()
6255 if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) { in bfq_completed_request()
6259 * no outstanding request; used by the weight-raising in bfq_completed_request()
6262 bfqq->budget_timeout = jiffies; in bfq_completed_request()
6269 bfqq->ttime.last_end_request = now_ns; in bfq_completed_request()
6275 delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC); in bfq_completed_request()
6286 * - close the observation interval at the last (previous) in bfq_completed_request()
6288 * - compute rate, if possible, for that observation interval in bfq_completed_request()
6289 * - reset to zero samples, which will trigger a proper in bfq_completed_request()
6290 * re-initialization of the observation interval on next in bfq_completed_request()
6294 (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us < in bfq_completed_request()
6295 1UL<<(BFQ_RATE_SHIFT - 10)) in bfq_completed_request()
6297 bfqd->last_completion = now_ns; in bfq_completed_request()
6308 bfqd->last_completed_rq_bfqq = bfqq; in bfq_completed_request()
6310 bfqd->last_completed_rq_bfqq = NULL; in bfq_completed_request()
6321 * expires, if it still has in-flight requests. in bfq_completed_request()
6323 if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 && in bfq_completed_request()
6324 RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_completed_request()
6325 bfqq->wr_coeff != bfqd->bfq_wr_coeff) in bfq_completed_request()
6326 bfqq->soft_rt_next_start = in bfq_completed_request()
6330 * If this is the in-service queue, check if it needs to be expired, in bfq_completed_request()
6333 if (bfqd->in_service_queue == bfqq) { in bfq_completed_request()
6335 if (bfqq->dispatched == 0) in bfq_completed_request()
6344 * Here bfqq->dispatched > 0 holds, but in bfq_completed_request()
6347 * for bfqq before bfqq->dispatched reaches 0, in bfq_completed_request()
6349 * completion event that causes bfqq->dispatch in bfq_completed_request()
6352 * (I/O-dispatch plugging). in bfq_completed_request()
6356 * when bfqq->dispatched finally reaches in bfq_completed_request()
6364 else if (RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_completed_request()
6365 (bfqq->dispatched == 0 || in bfq_completed_request()
6371 if (!bfqd->rq_in_driver) in bfq_completed_request()
6384 * allowed to switch to another queue---because bfqq is sync and
6385 * I/O-dispatch needs to be plugged while bfqq is temporarily
6386 * empty---then, during the service of bfqq, there will be frequent
6399 * guarantees. In this respect, the mechanism maintains a per-queue
6402 * of I/O requests in flight---i.e., already dispatched but not yet
6403 * completed---remains lower than this limit.
6408 * service, and causes bfqq to switch from empty to non-empty. The
6433 * The limit-update algorithm works as follows. On the arrival of a
6438 * (1) If there is no in-flight request. This gives a baseline for the
6446 * (2) If the limit is higher than 0 and there are in-flight
6482 u64 tot_time_ns = ktime_get_ns() - bfqd->last_empty_occupied_ns; in bfq_update_inject_limit()
6483 unsigned int old_limit = bfqq->inject_limit; in bfq_update_inject_limit()
6485 if (bfqq->last_serv_time_ns > 0 && bfqd->rqs_injected) { in bfq_update_inject_limit()
6486 u64 threshold = (bfqq->last_serv_time_ns * 3)>>1; in bfq_update_inject_limit()
6489 bfqq->inject_limit--; in bfq_update_inject_limit()
6490 bfqq->decrease_time_jif = jiffies; in bfq_update_inject_limit()
6492 old_limit <= bfqd->max_rq_in_driver) in bfq_update_inject_limit()
6493 bfqq->inject_limit++; in bfq_update_inject_limit()
6502 * NOTE: (bfqd->rq_in_driver == 1) means that there is no I/O in bfq_update_inject_limit()
6506 * bfqd->rq_in_driver is decremented in such a code path. in bfq_update_inject_limit()
6508 if ((bfqq->last_serv_time_ns == 0 && bfqd->rq_in_driver == 1) || in bfq_update_inject_limit()
6509 tot_time_ns < bfqq->last_serv_time_ns) { in bfq_update_inject_limit()
6510 if (bfqq->last_serv_time_ns == 0) { in bfq_update_inject_limit()
6515 bfqq->inject_limit = max_t(unsigned int, 1, old_limit); in bfq_update_inject_limit()
6517 bfqq->last_serv_time_ns = tot_time_ns; in bfq_update_inject_limit()
6518 } else if (!bfqd->rqs_injected && bfqd->rq_in_driver == 1) in bfq_update_inject_limit()
6528 bfqq->last_serv_time_ns = tot_time_ns; in bfq_update_inject_limit()
6532 bfqd->waited_rq = NULL; in bfq_update_inject_limit()
6533 bfqd->rqs_injected = false; in bfq_update_inject_limit()
6550 * requeued request that has not (yet) been re-inserted into in bfq_finish_requeue_request()
6553 if (!rq->elv.icq || !bfqq) in bfq_finish_requeue_request()
6556 bfqd = bfqq->bfqd; in bfq_finish_requeue_request()
6558 if (rq->rq_flags & RQF_STARTED) in bfq_finish_requeue_request()
6560 rq->start_time_ns, in bfq_finish_requeue_request()
6561 rq->io_start_time_ns, in bfq_finish_requeue_request()
6562 rq->cmd_flags); in bfq_finish_requeue_request()
6564 spin_lock_irqsave(&bfqd->lock, flags); in bfq_finish_requeue_request()
6565 if (likely(rq->rq_flags & RQF_STARTED)) { in bfq_finish_requeue_request()
6566 if (rq == bfqd->waited_rq) in bfq_finish_requeue_request()
6573 RQ_BIC(rq)->requests--; in bfq_finish_requeue_request()
6574 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_finish_requeue_request()
6581 * design would be to prevent blk-mq from invoking the requeue in bfq_finish_requeue_request()
6586 * request-insertion logic if rq is re-inserted into a bfq in bfq_finish_requeue_request()
6587 * internal queue, without a re-preparation. Here we assume in bfq_finish_requeue_request()
6588 * that re-insertions of requeued requests, without in bfq_finish_requeue_request()
6589 * re-preparation, can happen only for pass_through or at_head in bfq_finish_requeue_request()
6590 * requests (which are not re-inserted into bfq internal in bfq_finish_requeue_request()
6593 rq->elv.priv[0] = NULL; in bfq_finish_requeue_request()
6594 rq->elv.priv[1] = NULL; in bfq_finish_requeue_request()
6601 if (rq->elv.icq) { in bfq_finish_request()
6602 put_io_context(rq->elv.icq->ioc); in bfq_finish_request()
6603 rq->elv.icq = NULL; in bfq_finish_request()
6616 bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue"); in bfq_split_bfqq()
6619 bfqq->pid = current->pid; in bfq_split_bfqq()
6629 bfq_release_process_ref(bfqq->bfqd, bfqq); in bfq_split_bfqq()
6641 if (likely(bfqq && bfqq != &bfqd->oom_bfqq)) in bfq_get_bfqq_handle_split()
6653 if ((bic->was_in_burst_list && bfqd->large_burst) || in bfq_get_bfqq_handle_split()
6654 bic->saved_in_large_burst) in bfq_get_bfqq_handle_split()
6658 if (bic->was_in_burst_list) in bfq_get_bfqq_handle_split()
6687 hlist_add_head(&bfqq->burst_list_node, in bfq_get_bfqq_handle_split()
6688 &bfqd->burst_list); in bfq_get_bfqq_handle_split()
6690 bfqq->split_time = jiffies; in bfq_get_bfqq_handle_split()
6704 rq->elv.icq = ioc_find_get_icq(rq->q); in bfq_prepare_request()
6711 rq->elv.priv[0] = rq->elv.priv[1] = NULL; in bfq_prepare_request()
6739 struct request_queue *q = rq->q; in bfq_init_rq()
6740 struct bio *bio = rq->bio; in bfq_init_rq()
6741 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_init_rq()
6748 if (unlikely(!rq->elv.icq)) in bfq_init_rq()
6758 if (rq->elv.priv[1]) in bfq_init_rq()
6759 return rq->elv.priv[1]; in bfq_init_rq()
6761 bic = icq_to_bic(rq->elv.icq); in bfq_init_rq()
6773 !bic->stably_merged) { in bfq_init_rq()
6778 bic->saved_in_large_burst = true; in bfq_init_rq()
6787 bfqq->waker_bfqq = old_bfqq->waker_bfqq; in bfq_init_rq()
6788 bfqq->tentative_waker_bfqq = NULL; in bfq_init_rq()
6792 * new_bfqq->waker_bfqq must be in bfq_init_rq()
6797 if (bfqq->waker_bfqq) in bfq_init_rq()
6798 hlist_add_head(&bfqq->woken_list_node, in bfq_init_rq()
6799 &bfqq->waker_bfqq->woken_list); in bfq_init_rq()
6806 bfqq->ref++; in bfq_init_rq()
6807 bic->requests++; in bfq_init_rq()
6809 rq, bfqq, bfqq->ref); in bfq_init_rq()
6811 rq->elv.priv[0] = bic; in bfq_init_rq()
6812 rq->elv.priv[1] = bfqq; in bfq_init_rq()
6816 * by only this bic: we can then set bfqq->bic = bic. in in bfq_init_rq()
6820 if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) { in bfq_init_rq()
6821 bfqq->bic = bic; in bfq_init_rq()
6836 * 1) A burst is actually happening (bfqd->burst_size > 0) in bfq_init_rq()
6842 * therefore in not weight-raising bfqq. See comments on in bfq_init_rq()
6854 (bfqd->burst_size > 0 || in bfq_init_rq()
6867 spin_lock_irqsave(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6876 if (bfqq != bfqd->in_service_queue) { in bfq_idle_slice_timer_body()
6877 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6890 else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0) in bfq_idle_slice_timer_body()
6894 * first request of the in-service queue arrives in bfq_idle_slice_timer_body()
6905 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6909 * Handler of the expiration of the timer running if the in-service queue
6916 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_idle_slice_timer()
6919 * Theoretical race here: the in-service queue can be NULL or in bfq_idle_slice_timer()
6922 * cycle that changes the in-service queue. This can hardly in bfq_idle_slice_timer()
6939 bfq_bfqq_move(bfqd, bfqq, bfqd->root_group); in __bfq_put_async_bfqq()
6942 bfqq, bfqq->ref); in __bfq_put_async_bfqq()
6960 __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]); in bfq_put_async_queues()
6962 __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq); in bfq_put_async_queues()
6971 unsigned int depth = 1U << bt->sb.shift; in bfq_update_depths()
6973 bfqd->full_depth_shift = bt->sb.shift; in bfq_update_depths()
6975 * In-word depths if no bfq_queue is being weight-raised: in bfq_update_depths()
6978 * In next formulas, right-shift the value in bfq_update_depths()
6979 * (1U<<bt->sb.shift), instead of computing directly in bfq_update_depths()
6980 * (1U<<(bt->sb.shift - something)), to be robust against in bfq_update_depths()
6981 * any possible value of bt->sb.shift, without having to in bfq_update_depths()
6985 bfqd->word_depths[0][0] = max(depth >> 1, 1U); in bfq_update_depths()
6991 bfqd->word_depths[0][1] = max((depth * 3) >> 2, 1U); in bfq_update_depths()
6994 * In-word depths in case some bfq_queue is being weight- in bfq_update_depths()
6997 * start-up times didn't suffer from any regression due to tag in bfq_update_depths()
7001 bfqd->word_depths[1][0] = max((depth * 3) >> 4, 1U); in bfq_update_depths()
7003 bfqd->word_depths[1][1] = max((depth * 6) >> 4, 1U); in bfq_update_depths()
7008 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_depth_updated()
7009 struct blk_mq_tags *tags = hctx->sched_tags; in bfq_depth_updated()
7011 bfq_update_depths(bfqd, &tags->bitmap_tags); in bfq_depth_updated()
7012 sbitmap_queue_min_shallow_depth(&tags->bitmap_tags, 1); in bfq_depth_updated()
7023 struct bfq_data *bfqd = e->elevator_data; in bfq_exit_queue()
7026 hrtimer_cancel(&bfqd->idle_slice_timer); in bfq_exit_queue()
7028 spin_lock_irq(&bfqd->lock); in bfq_exit_queue()
7029 list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list) in bfq_exit_queue()
7031 spin_unlock_irq(&bfqd->lock); in bfq_exit_queue()
7033 hrtimer_cancel(&bfqd->idle_slice_timer); in bfq_exit_queue()
7035 /* release oom-queue reference to root group */ in bfq_exit_queue()
7036 bfqg_and_blkg_put(bfqd->root_group); in bfq_exit_queue()
7039 blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq); in bfq_exit_queue()
7041 spin_lock_irq(&bfqd->lock); in bfq_exit_queue()
7042 bfq_put_async_queues(bfqd, bfqd->root_group); in bfq_exit_queue()
7043 kfree(bfqd->root_group); in bfq_exit_queue()
7044 spin_unlock_irq(&bfqd->lock); in bfq_exit_queue()
7047 blk_stat_disable_accounting(bfqd->queue); in bfq_exit_queue()
7048 wbt_enable_default(bfqd->queue); in bfq_exit_queue()
7059 root_group->entity.parent = NULL; in bfq_init_root_group()
7060 root_group->my_entity = NULL; in bfq_init_root_group()
7061 root_group->bfqd = bfqd; in bfq_init_root_group()
7063 root_group->rq_pos_tree = RB_ROOT; in bfq_init_root_group()
7065 root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT; in bfq_init_root_group()
7066 root_group->sched_data.bfq_class_idle_last_service = jiffies; in bfq_init_root_group()
7076 return -ENOMEM; in bfq_init_queue()
7078 bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node); in bfq_init_queue()
7080 kobject_put(&eq->kobj); in bfq_init_queue()
7081 return -ENOMEM; in bfq_init_queue()
7083 eq->elevator_data = bfqd; in bfq_init_queue()
7085 spin_lock_irq(&q->queue_lock); in bfq_init_queue()
7086 q->elevator = eq; in bfq_init_queue()
7087 spin_unlock_irq(&q->queue_lock); in bfq_init_queue()
7094 bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0); in bfq_init_queue()
7095 bfqd->oom_bfqq.ref++; in bfq_init_queue()
7096 bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO; in bfq_init_queue()
7097 bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE; in bfq_init_queue()
7098 bfqd->oom_bfqq.entity.new_weight = in bfq_init_queue()
7099 bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio); in bfq_init_queue()
7102 bfq_clear_bfqq_just_created(&bfqd->oom_bfqq); in bfq_init_queue()
7109 bfqd->oom_bfqq.entity.prio_changed = 1; in bfq_init_queue()
7111 bfqd->queue = q; in bfq_init_queue()
7113 INIT_LIST_HEAD(&bfqd->dispatch); in bfq_init_queue()
7115 hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC, in bfq_init_queue()
7117 bfqd->idle_slice_timer.function = bfq_idle_slice_timer; in bfq_init_queue()
7119 bfqd->queue_weights_tree = RB_ROOT_CACHED; in bfq_init_queue()
7120 bfqd->num_groups_with_pending_reqs = 0; in bfq_init_queue()
7122 INIT_LIST_HEAD(&bfqd->active_list); in bfq_init_queue()
7123 INIT_LIST_HEAD(&bfqd->idle_list); in bfq_init_queue()
7124 INIT_HLIST_HEAD(&bfqd->burst_list); in bfq_init_queue()
7126 bfqd->hw_tag = -1; in bfq_init_queue()
7127 bfqd->nonrot_with_queueing = blk_queue_nonrot(bfqd->queue); in bfq_init_queue()
7129 bfqd->bfq_max_budget = bfq_default_max_budget; in bfq_init_queue()
7131 bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0]; in bfq_init_queue()
7132 bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1]; in bfq_init_queue()
7133 bfqd->bfq_back_max = bfq_back_max; in bfq_init_queue()
7134 bfqd->bfq_back_penalty = bfq_back_penalty; in bfq_init_queue()
7135 bfqd->bfq_slice_idle = bfq_slice_idle; in bfq_init_queue()
7136 bfqd->bfq_timeout = bfq_timeout; in bfq_init_queue()
7138 bfqd->bfq_large_burst_thresh = 8; in bfq_init_queue()
7139 bfqd->bfq_burst_interval = msecs_to_jiffies(180); in bfq_init_queue()
7141 bfqd->low_latency = true; in bfq_init_queue()
7144 * Trade-off between responsiveness and fairness. in bfq_init_queue()
7146 bfqd->bfq_wr_coeff = 30; in bfq_init_queue()
7147 bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300); in bfq_init_queue()
7148 bfqd->bfq_wr_max_time = 0; in bfq_init_queue()
7149 bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000); in bfq_init_queue()
7150 bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500); in bfq_init_queue()
7151 bfqd->bfq_wr_max_softrt_rate = 7000; /* in bfq_init_queue()
7154 * high-definition compressed in bfq_init_queue()
7157 bfqd->wr_busy_queues = 0; in bfq_init_queue()
7163 bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] * in bfq_init_queue()
7164 ref_wr_duration[blk_queue_nonrot(bfqd->queue)]; in bfq_init_queue()
7165 bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3; in bfq_init_queue()
7167 spin_lock_init(&bfqd->lock); in bfq_init_queue()
7172 * (bfq_create_group_hierarchy->blkcg_activate_policy-> in bfq_init_queue()
7180 * other inconsistencies, the blk-mq stack must then refrain in bfq_init_queue()
7184 bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node); in bfq_init_queue()
7185 if (!bfqd->root_group) in bfq_init_queue()
7187 bfq_init_root_group(bfqd->root_group, bfqd); in bfq_init_queue()
7188 bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group); in bfq_init_queue()
7200 kobject_put(&eq->kobj); in bfq_init_queue()
7201 return -ENOMEM; in bfq_init_queue()
7213 return -ENOMEM; in bfq_slab_setup()
7236 struct bfq_data *bfqd = e->elevator_data; \
7244 SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
7245 SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
7246 SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
7247 SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
7248 SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
7249 SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
7250 SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
7251 SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
7252 SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
7258 struct bfq_data *bfqd = e->elevator_data; \
7263 USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
7270 struct bfq_data *bfqd = e->elevator_data; \
7289 STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
7291 STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
7293 STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
7294 STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
7296 STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
7302 struct bfq_data *bfqd = e->elevator_data; \
7316 USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
7323 struct bfq_data *bfqd = e->elevator_data; in bfq_max_budget_store()
7332 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); in bfq_max_budget_store()
7336 bfqd->bfq_max_budget = __data; in bfq_max_budget_store()
7339 bfqd->bfq_user_max_budget = __data; in bfq_max_budget_store()
7351 struct bfq_data *bfqd = e->elevator_data; in bfq_timeout_sync_store()
7364 bfqd->bfq_timeout = msecs_to_jiffies(__data); in bfq_timeout_sync_store()
7365 if (bfqd->bfq_user_max_budget == 0) in bfq_timeout_sync_store()
7366 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); in bfq_timeout_sync_store()
7374 struct bfq_data *bfqd = e->elevator_data; in bfq_strict_guarantees_store()
7384 if (!bfqd->strict_guarantees && __data == 1 in bfq_strict_guarantees_store()
7385 && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC) in bfq_strict_guarantees_store()
7386 bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC; in bfq_strict_guarantees_store()
7388 bfqd->strict_guarantees = __data; in bfq_strict_guarantees_store()
7396 struct bfq_data *bfqd = e->elevator_data; in bfq_low_latency_store()
7406 if (__data == 0 && bfqd->low_latency != 0) in bfq_low_latency_store()
7408 bfqd->low_latency = __data; in bfq_low_latency_store()
7459 MODULE_ALIAS("bfq-iosched");
7471 ret = -ENOMEM; in bfq_init()
7485 * scheduler cannot rely on a peak-rate-evaluation workload to in bfq_init()