Lines Matching +full:embedded +full:- +full:trace +full:- +full:extension

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
126 #include <linux/backing-dev.h>
128 #include <trace/events/block.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) icq_to_bic((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.
448 spin_lock_irqsave(&q->queue_lock, flags); in bfq_bic_lookup()
450 spin_unlock_irqrestore(&q->queue_lock, flags); in bfq_bic_lookup()
464 if (bfqd->queued != 0) { in bfq_schedule_dispatch()
466 blk_mq_run_hw_queues(bfqd->queue, true); in bfq_schedule_dispatch()
470 #define bfq_class_idle(bfqq) ((bfqq)->ioprio_class == IOPRIO_CLASS_IDLE)
475 * Lifted from AS - choose which of rq1 and rq2 that is best served now.
499 if ((rq1->cmd_flags & REQ_META) && !(rq2->cmd_flags & REQ_META)) in bfq_choose_req()
501 else if ((rq2->cmd_flags & REQ_META) && !(rq1->cmd_flags & REQ_META)) in bfq_choose_req()
510 back_max = bfqd->bfq_back_max * 2; in bfq_choose_req()
518 d1 = s1 - last; in bfq_choose_req()
520 d1 = (last - s1) * bfqd->bfq_back_penalty; in bfq_choose_req()
525 d2 = s2 - last; in bfq_choose_req()
527 d2 = (last - s2) * bfqd->bfq_back_penalty; in bfq_choose_req()
535 * check two variables for all permutations: --> faster! in bfq_choose_req()
558 * (--> only *one* back seek required), in bfq_choose_req()
577 struct bfq_data *bfqd = data->q->elevator->elevator_data; in bfq_limit_depth()
582 data->shallow_depth = in bfq_limit_depth()
583 bfqd->word_depths[!!bfqd->wr_busy_queues][op_is_sync(op)]; in bfq_limit_depth()
586 __func__, bfqd->wr_busy_queues, op_is_sync(op), in bfq_limit_depth()
587 data->shallow_depth); in bfq_limit_depth()
599 p = &root->rb_node; in bfq_rq_pos_tree_lookup()
610 if (sector > blk_rq_pos(bfqq->next_rq)) in bfq_rq_pos_tree_lookup()
611 n = &(*p)->rb_right; in bfq_rq_pos_tree_lookup()
612 else if (sector < blk_rq_pos(bfqq->next_rq)) in bfq_rq_pos_tree_lookup()
613 n = &(*p)->rb_left; in bfq_rq_pos_tree_lookup()
626 bfqq ? bfqq->pid : 0); in bfq_rq_pos_tree_lookup()
633 return bfqq->service_from_backlogged > 0 && in bfq_too_late_for_merging()
634 time_is_before_jiffies(bfqq->first_IO_time + in bfq_too_late_for_merging()
652 if (bfqq->pos_root) { in bfq_pos_tree_add_move()
653 rb_erase(&bfqq->pos_node, bfqq->pos_root); in bfq_pos_tree_add_move()
654 bfqq->pos_root = NULL; in bfq_pos_tree_add_move()
658 if (bfqq == &bfqd->oom_bfqq) in bfq_pos_tree_add_move()
671 if (!bfqq->next_rq) in bfq_pos_tree_add_move()
674 bfqq->pos_root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree; in bfq_pos_tree_add_move()
675 __bfqq = bfq_rq_pos_tree_lookup(bfqd, bfqq->pos_root, in bfq_pos_tree_add_move()
676 blk_rq_pos(bfqq->next_rq), &parent, &p); in bfq_pos_tree_add_move()
678 rb_link_node(&bfqq->pos_node, parent, p); in bfq_pos_tree_add_move()
679 rb_insert_color(&bfqq->pos_node, bfqq->pos_root); in bfq_pos_tree_add_move()
681 bfqq->pos_root = NULL; in bfq_pos_tree_add_move()
694 * of this function is used to check whether I/O-dispatch plugging can
699 * 2) all active queues belong to the same I/O-priority class,
706 * the last two symmetry sub-conditions above would be quite complex
708 * only the following stronger three sub-conditions, for which it is
711 * 2) all active queues belong to the same I/O-priority class,
721 bfqq->weight_counter && in bfq_asymmetric_scenario()
722 bfqq->weight_counter == in bfq_asymmetric_scenario()
724 rb_first_cached(&bfqd->queue_weights_tree), in bfq_asymmetric_scenario()
733 !RB_EMPTY_ROOT(&bfqd->queue_weights_tree.rb_root) && in bfq_asymmetric_scenario()
734 (bfqd->queue_weights_tree.rb_root.rb_node->rb_left || in bfq_asymmetric_scenario()
735 bfqd->queue_weights_tree.rb_root.rb_node->rb_right); in bfq_asymmetric_scenario()
738 (bfqd->busy_queues[0] && bfqd->busy_queues[1]) || in bfq_asymmetric_scenario()
739 (bfqd->busy_queues[0] && bfqd->busy_queues[2]) || in bfq_asymmetric_scenario()
740 (bfqd->busy_queues[1] && bfqd->busy_queues[2]); in bfq_asymmetric_scenario()
744 || bfqd->num_groups_with_pending_reqs > 0 in bfq_asymmetric_scenario()
750 * If the weight-counter tree passed as input contains no counter for
754 * Note that weight-counter trees contain few nodes in mostly symmetric
756 * weight-counter tree for the queues may contain at most one node.
757 * This holds even if low_latency is on, because weight-raised queues
765 struct bfq_entity *entity = &bfqq->entity; in bfq_weights_tree_add()
766 struct rb_node **new = &(root->rb_root.rb_node), *parent = NULL; in bfq_weights_tree_add()
773 * non-weight-raised, and hence change its weight, and in bfq_weights_tree_add()
781 if (bfqq->weight_counter) in bfq_weights_tree_add()
790 if (entity->weight == __counter->weight) { in bfq_weights_tree_add()
791 bfqq->weight_counter = __counter; in bfq_weights_tree_add()
794 if (entity->weight < __counter->weight) in bfq_weights_tree_add()
795 new = &((*new)->rb_left); in bfq_weights_tree_add()
797 new = &((*new)->rb_right); in bfq_weights_tree_add()
802 bfqq->weight_counter = kzalloc(sizeof(struct bfq_weight_counter), in bfq_weights_tree_add()
815 * if !bfqq->weight_counter. in bfq_weights_tree_add()
817 if (unlikely(!bfqq->weight_counter)) in bfq_weights_tree_add()
820 bfqq->weight_counter->weight = entity->weight; in bfq_weights_tree_add()
821 rb_link_node(&bfqq->weight_counter->weights_node, parent, new); in bfq_weights_tree_add()
822 rb_insert_color_cached(&bfqq->weight_counter->weights_node, root, in bfq_weights_tree_add()
826 bfqq->weight_counter->num_active++; in bfq_weights_tree_add()
827 bfqq->ref++; in bfq_weights_tree_add()
840 if (!bfqq->weight_counter) in __bfq_weights_tree_remove()
843 bfqq->weight_counter->num_active--; in __bfq_weights_tree_remove()
844 if (bfqq->weight_counter->num_active > 0) in __bfq_weights_tree_remove()
847 rb_erase_cached(&bfqq->weight_counter->weights_node, root); in __bfq_weights_tree_remove()
848 kfree(bfqq->weight_counter); in __bfq_weights_tree_remove()
851 bfqq->weight_counter = NULL; in __bfq_weights_tree_remove()
862 struct bfq_entity *entity = bfqq->entity.parent; in bfq_weights_tree_remove()
865 struct bfq_sched_data *sd = entity->my_sched_data; in bfq_weights_tree_remove()
867 if (sd->next_in_service || sd->in_service_entity) { in bfq_weights_tree_remove()
892 if (entity->in_groups_with_pending_reqs) { in bfq_weights_tree_remove()
893 entity->in_groups_with_pending_reqs = false; in bfq_weights_tree_remove()
894 bfqd->num_groups_with_pending_reqs--; in bfq_weights_tree_remove()
905 &bfqd->queue_weights_tree); in bfq_weights_tree_remove()
921 rq = rq_entry_fifo(bfqq->fifo.next); in bfq_check_fifo()
923 if (rq == last || ktime_get_ns() < rq->fifo_time) in bfq_check_fifo()
926 bfq_log_bfqq(bfqq->bfqd, bfqq, "check_fifo: returned %p", rq); in bfq_check_fifo()
934 struct rb_node *rbnext = rb_next(&last->rb_node); in bfq_find_next_rq()
935 struct rb_node *rbprev = rb_prev(&last->rb_node); in bfq_find_next_rq()
949 rbnext = rb_first(&bfqq->sort_list); in bfq_find_next_rq()
950 if (rbnext && rbnext != &last->rb_node) in bfq_find_next_rq()
961 if (bfq_bfqq_sync(bfqq) || bfqq->wr_coeff > 1 || in bfq_serv_to_charge()
962 bfq_asymmetric_scenario(bfqq->bfqd, bfqq)) in bfq_serv_to_charge()
969 * bfq_updated_next_req - update the queue after a new next_rq selection.
982 struct bfq_entity *entity = &bfqq->entity; in bfq_updated_next_req()
983 struct request *next_rq = bfqq->next_rq; in bfq_updated_next_req()
989 if (bfqq == bfqd->in_service_queue) in bfq_updated_next_req()
997 max_t(unsigned long, bfqq->max_budget, in bfq_updated_next_req()
999 entity->service); in bfq_updated_next_req()
1000 if (entity->budget != new_budget) { in bfq_updated_next_req()
1001 entity->budget = new_budget; in bfq_updated_next_req()
1012 if (bfqd->bfq_wr_max_time > 0) in bfq_wr_duration()
1013 return bfqd->bfq_wr_max_time; in bfq_wr_duration()
1015 dur = bfqd->rate_dur_prod; in bfq_wr_duration()
1016 do_div(dur, bfqd->peak_rate); in bfq_wr_duration()
1022 * - running in a slow PC in bfq_wr_duration()
1023 * - with a virtual disk stacked on a slow low-end 5400rpm HDD in bfq_wr_duration()
1024 * - serving a heavy I/O workload, such as the sequential reading in bfq_wr_duration()
1026 * mplayer took 23 seconds to start, if constantly weight-raised. in bfq_wr_duration()
1031 * responsiveness by allowing non-interactive applications to in bfq_wr_duration()
1036 * before weight-raising finishes. in bfq_wr_duration()
1041 /* switch back from soft real-time to interactive weight raising */
1045 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in switch_back_to_interactive_wr()
1046 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in switch_back_to_interactive_wr()
1047 bfqq->last_wr_start_finish = bfqq->wr_start_at_switch_to_srt; in switch_back_to_interactive_wr()
1057 if (bic->saved_has_short_ttime) in bfq_bfqq_resume_state()
1062 if (bic->saved_IO_bound) in bfq_bfqq_resume_state()
1067 bfqq->last_serv_time_ns = bic->saved_last_serv_time_ns; in bfq_bfqq_resume_state()
1068 bfqq->inject_limit = bic->saved_inject_limit; in bfq_bfqq_resume_state()
1069 bfqq->decrease_time_jif = bic->saved_decrease_time_jif; in bfq_bfqq_resume_state()
1071 bfqq->entity.new_weight = bic->saved_weight; in bfq_bfqq_resume_state()
1072 bfqq->ttime = bic->saved_ttime; in bfq_bfqq_resume_state()
1073 bfqq->io_start_time = bic->saved_io_start_time; in bfq_bfqq_resume_state()
1074 bfqq->tot_idle_time = bic->saved_tot_idle_time; in bfq_bfqq_resume_state()
1078 if (bfqd->low_latency) { in bfq_bfqq_resume_state()
1079 old_wr_coeff = bfqq->wr_coeff; in bfq_bfqq_resume_state()
1080 bfqq->wr_coeff = bic->saved_wr_coeff; in bfq_bfqq_resume_state()
1082 bfqq->service_from_wr = bic->saved_service_from_wr; in bfq_bfqq_resume_state()
1083 bfqq->wr_start_at_switch_to_srt = bic->saved_wr_start_at_switch_to_srt; in bfq_bfqq_resume_state()
1084 bfqq->last_wr_start_finish = bic->saved_last_wr_start_finish; in bfq_bfqq_resume_state()
1085 bfqq->wr_cur_max_time = bic->saved_wr_cur_max_time; in bfq_bfqq_resume_state()
1087 if (bfqq->wr_coeff > 1 && (bfq_bfqq_in_large_burst(bfqq) || in bfq_bfqq_resume_state()
1088 time_is_before_jiffies(bfqq->last_wr_start_finish + in bfq_bfqq_resume_state()
1089 bfqq->wr_cur_max_time))) { in bfq_bfqq_resume_state()
1090 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in bfq_bfqq_resume_state()
1092 time_is_after_eq_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_bfqq_resume_state()
1096 bfqq->wr_coeff = 1; in bfq_bfqq_resume_state()
1097 bfq_log_bfqq(bfqq->bfqd, bfqq, in bfq_bfqq_resume_state()
1103 bfqq->entity.prio_changed = 1; in bfq_bfqq_resume_state()
1108 if (old_wr_coeff == 1 && bfqq->wr_coeff > 1) in bfq_bfqq_resume_state()
1109 bfqd->wr_busy_queues++; in bfq_bfqq_resume_state()
1110 else if (old_wr_coeff > 1 && bfqq->wr_coeff == 1) in bfq_bfqq_resume_state()
1111 bfqd->wr_busy_queues--; in bfq_bfqq_resume_state()
1116 return bfqq->ref - bfqq->allocated - bfqq->entity.on_st_or_in_serv - in bfqq_process_refs()
1117 (bfqq->weight_counter != NULL) - bfqq->stable_ref; in bfqq_process_refs()
1126 hlist_for_each_entry_safe(item, n, &bfqd->burst_list, burst_list_node) in bfq_reset_burst_list()
1127 hlist_del_init(&item->burst_list_node); in bfq_reset_burst_list()
1135 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); in bfq_reset_burst_list()
1136 bfqd->burst_size = 1; in bfq_reset_burst_list()
1138 bfqd->burst_size = 0; in bfq_reset_burst_list()
1140 bfqd->burst_parent_entity = bfqq->entity.parent; in bfq_reset_burst_list()
1147 bfqd->burst_size++; in bfq_add_to_burst()
1149 if (bfqd->burst_size == bfqd->bfq_large_burst_thresh) { in bfq_add_to_burst()
1157 bfqd->large_burst = true; in bfq_add_to_burst()
1163 hlist_for_each_entry(bfqq_item, &bfqd->burst_list, in bfq_add_to_burst()
1175 hlist_for_each_entry_safe(pos, n, &bfqd->burst_list, in bfq_add_to_burst()
1177 hlist_del_init(&pos->burst_list_node); in bfq_add_to_burst()
1184 hlist_add_head(&bfqq->burst_list_node, &bfqd->burst_list); in bfq_add_to_burst()
1194 * possible, it is usually better to not grant either weight-raising
1206 * completed. As a consequence, weight-raising any of these queues,
1210 * weight-raising these new queues just lowers throughput in most
1215 * parallel I/O-bound threads. In fact, with a complex application,
1216 * several short processes may need to be executed to start-up the
1222 * weight-raise all the queues created during the burst. This is the
1232 * weight-raise queues whose creation occurs in a large burst. In
1265 * the large-burst threshold, then
1273 * previous sub-step), and now is not needed any more
1275 * . the device enters a large-burst mode
1278 * the device is in large-burst mode and shortly after the last time
1288 * . the large-burst mode is reset if set
1303 if (!hlist_unhashed(&bfqq->burst_list_node) || in bfq_handle_burst()
1305 time_is_after_eq_jiffies(bfqq->split_time + in bfq_handle_burst()
1326 if (time_is_before_jiffies(bfqd->last_ins_in_burst + in bfq_handle_burst()
1327 bfqd->bfq_burst_interval) || in bfq_handle_burst()
1328 bfqq->entity.parent != bfqd->burst_parent_entity) { in bfq_handle_burst()
1329 bfqd->large_burst = false; in bfq_handle_burst()
1339 if (bfqd->large_burst) { in bfq_handle_burst()
1345 * If we get here, then a large-burst state has not yet been in bfq_handle_burst()
1359 bfqd->last_ins_in_burst = jiffies; in bfq_handle_burst()
1364 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_budget_left()
1366 return entity->budget - entity->service; in bfq_bfqq_budget_left()
1376 if (bfqd->budgets_assigned < bfq_stats_min_budgets) in bfq_max_budget()
1379 return bfqd->bfq_max_budget; in bfq_max_budget()
1388 if (bfqd->budgets_assigned < bfq_stats_min_budgets) in bfq_min_budget()
1391 return bfqd->bfq_max_budget / 32; in bfq_min_budget()
1397 * whether the in-service queue should be expired, by returning
1398 * true. The purpose of expiring the in-service queue is to give bfqq
1399 * the chance to possibly preempt the in-service queue, and the reason
1400 * for preempting the in-service queue is to achieve one of the two
1407 * - BFQQE_NO_MORE_REQUESTS bfqq did not enjoy any device idling
1411 * - BFQQE_TOO_IDLE bfqq did enjoy device idling, but did not issue
1412 * a new request before the expiration of the idling-time.
1441 * before last expiration. Thus timestamps need to be back-shifted
1445 * Secondly, to allow the process to recover the hole, the in-service
1448 * in-service queue to be completed, then it may become impossible to
1449 * let the process recover the hole, even if the back-shifted
1450 * timestamps of bfqq are lower than those of the in-service queue. If
1466 * above-described special way, and signals that the in-service queue
1467 * should be expired. Timestamp back-shifting is done later in
1473 * timestamp than the in-service queue. That is, the next budget of
1474 * bfqq may have to be completed before the one of the in-service
1475 * queue. If this is the case, then preempting the in-service queue
1481 * the in-service queue must be preempted. To have service trees
1482 * correctly updated, the in-service queue must be expired and
1485 * mechanism may be re-designed in such a way to make it possible to
1489 * even be no in-service queue when the next function is invoked (so,
1494 * in-service queue (unconditionally) only for queues that need to
1502 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_update_budg_for_activation()
1516 * that timestamps need to be back-shifted (and is in bfq_bfqq_update_budg_for_activation()
1522 * entity->service or entity->budget are not updated in bfq_bfqq_update_budg_for_activation()
1527 * entity->budget the remaining budget on such an in bfq_bfqq_update_budg_for_activation()
1530 entity->budget = min_t(unsigned long, in bfq_bfqq_update_budg_for_activation()
1532 bfqq->max_budget); in bfq_bfqq_update_budg_for_activation()
1535 * At this point, we have used entity->service to get in bfq_bfqq_update_budg_for_activation()
1537 * entity->budget). Thus we finally can, and have to, in bfq_bfqq_update_budg_for_activation()
1538 * reset entity->service. The latter must be reset in bfq_bfqq_update_budg_for_activation()
1543 entity->service = 0; in bfq_bfqq_update_budg_for_activation()
1551 entity->service = 0; in bfq_bfqq_update_budg_for_activation()
1552 entity->budget = max_t(unsigned long, bfqq->max_budget, in bfq_bfqq_update_budg_for_activation()
1553 bfq_serv_to_charge(bfqq->next_rq, bfqq)); in bfq_bfqq_update_budg_for_activation()
1564 return jiffies - MAX_JIFFY_OFFSET; in bfq_smallest_from_now()
1576 /* start a weight-raising period */ in bfq_update_bfqq_wr_on_rq_arrival()
1578 bfqq->service_from_wr = 0; in bfq_update_bfqq_wr_on_rq_arrival()
1579 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_update_bfqq_wr_on_rq_arrival()
1580 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_update_bfqq_wr_on_rq_arrival()
1586 * that, at the end of the soft-real-time in bfq_update_bfqq_wr_on_rq_arrival()
1588 * now, no interactive weight-raising period in bfq_update_bfqq_wr_on_rq_arrival()
1593 bfqq->wr_start_at_switch_to_srt = in bfq_update_bfqq_wr_on_rq_arrival()
1595 bfqq->wr_coeff = bfqd->bfq_wr_coeff * in bfq_update_bfqq_wr_on_rq_arrival()
1597 bfqq->wr_cur_max_time = in bfq_update_bfqq_wr_on_rq_arrival()
1598 bfqd->bfq_wr_rt_max_time; in bfq_update_bfqq_wr_on_rq_arrival()
1604 * scheduling-error component due to a too large in bfq_update_bfqq_wr_on_rq_arrival()
1610 bfqq->entity.budget = min_t(unsigned long, in bfq_update_bfqq_wr_on_rq_arrival()
1611 bfqq->entity.budget, in bfq_update_bfqq_wr_on_rq_arrival()
1615 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_update_bfqq_wr_on_rq_arrival()
1616 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_update_bfqq_wr_on_rq_arrival()
1618 bfqq->wr_coeff = 1; in bfq_update_bfqq_wr_on_rq_arrival()
1624 * the weight-raising duration for the in bfq_update_bfqq_wr_on_rq_arrival()
1625 * application with the weight-raising in bfq_update_bfqq_wr_on_rq_arrival()
1629 * before the weight-raising period for the in bfq_update_bfqq_wr_on_rq_arrival()
1636 * at a certain time weight-raising is in bfq_update_bfqq_wr_on_rq_arrival()
1647 * weight-raised while they are pending. in bfq_update_bfqq_wr_on_rq_arrival()
1649 if (bfqq->wr_cur_max_time != in bfq_update_bfqq_wr_on_rq_arrival()
1650 bfqd->bfq_wr_rt_max_time) { in bfq_update_bfqq_wr_on_rq_arrival()
1651 bfqq->wr_start_at_switch_to_srt = in bfq_update_bfqq_wr_on_rq_arrival()
1652 bfqq->last_wr_start_finish; in bfq_update_bfqq_wr_on_rq_arrival()
1654 bfqq->wr_cur_max_time = in bfq_update_bfqq_wr_on_rq_arrival()
1655 bfqd->bfq_wr_rt_max_time; in bfq_update_bfqq_wr_on_rq_arrival()
1656 bfqq->wr_coeff = bfqd->bfq_wr_coeff * in bfq_update_bfqq_wr_on_rq_arrival()
1659 bfqq->last_wr_start_finish = jiffies; in bfq_update_bfqq_wr_on_rq_arrival()
1667 return bfqq->dispatched == 0 && in bfq_bfqq_idle_for_long_time()
1669 bfqq->budget_timeout + in bfq_bfqq_idle_for_long_time()
1670 bfqd->bfq_wr_min_idle_time); in bfq_bfqq_idle_for_long_time()
1676 * weight than the in-service queue.
1683 if (bfqq->ioprio_class < in_serv_bfqq->ioprio_class) in bfq_bfqq_higher_class_or_weight()
1686 if (in_serv_bfqq->entity.parent == bfqq->entity.parent) { in bfq_bfqq_higher_class_or_weight()
1687 bfqq_weight = bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1688 in_serv_weight = in_serv_bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1690 if (bfqq->entity.parent) in bfq_bfqq_higher_class_or_weight()
1691 bfqq_weight = bfqq->entity.parent->weight; in bfq_bfqq_higher_class_or_weight()
1693 bfqq_weight = bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1694 if (in_serv_bfqq->entity.parent) in bfq_bfqq_higher_class_or_weight()
1695 in_serv_weight = in_serv_bfqq->entity.parent->weight; in bfq_bfqq_higher_class_or_weight()
1697 in_serv_weight = in_serv_bfqq->entity.weight; in bfq_bfqq_higher_class_or_weight()
1720 bfqq->ttime.last_end_request + in bfq_bfqq_handle_idle_busy_switch()
1721 bfqd->bfq_slice_idle * 3; in bfq_bfqq_handle_idle_busy_switch()
1725 * bfqq deserves to be weight-raised if: in bfq_bfqq_handle_idle_busy_switch()
1726 * - it is sync, in bfq_bfqq_handle_idle_busy_switch()
1727 * - it does not belong to a large burst, in bfq_bfqq_handle_idle_busy_switch()
1728 * - it has been idle for enough time or is soft real-time, in bfq_bfqq_handle_idle_busy_switch()
1729 * - is linked to a bfq_io_cq (it is not shared in any sense), in bfq_bfqq_handle_idle_busy_switch()
1730 * - has a default weight (otherwise we assume the user wanted in bfq_bfqq_handle_idle_busy_switch()
1734 soft_rt = bfqd->bfq_wr_max_softrt_rate > 0 && in bfq_bfqq_handle_idle_busy_switch()
1737 time_is_before_jiffies(bfqq->soft_rt_next_start) && in bfq_bfqq_handle_idle_busy_switch()
1738 bfqq->dispatched == 0 && in bfq_bfqq_handle_idle_busy_switch()
1739 bfqq->entity.new_weight == 40; in bfq_bfqq_handle_idle_busy_switch()
1741 bfqq->entity.new_weight == 40; in bfq_bfqq_handle_idle_busy_switch()
1743 * Merged bfq_queues are kept out of weight-raising in bfq_bfqq_handle_idle_busy_switch()
1744 * (low-latency) mechanisms. The reason is that these queues in bfq_bfqq_handle_idle_busy_switch()
1745 * are usually created for non-interactive and in bfq_bfqq_handle_idle_busy_switch()
1746 * non-soft-real-time tasks. Yet this is not the case for in bfq_bfqq_handle_idle_busy_switch()
1747 * stably-merged queues. These queues are merged just because in bfq_bfqq_handle_idle_busy_switch()
1749 * easily serve the I/O of an interactive or soft-real time in bfq_bfqq_handle_idle_busy_switch()
1751 * processes. So let also stably-merged queued enjoy weight in bfq_bfqq_handle_idle_busy_switch()
1754 wr_or_deserves_wr = bfqd->low_latency && in bfq_bfqq_handle_idle_busy_switch()
1755 (bfqq->wr_coeff > 1 || in bfq_bfqq_handle_idle_busy_switch()
1757 (bfqq->bic || RQ_BIC(rq)->stably_merged) && in bfq_bfqq_handle_idle_busy_switch()
1762 * may want to preempt the in-service queue. in bfq_bfqq_handle_idle_busy_switch()
1784 bfqq->budget_timeout + in bfq_bfqq_handle_idle_busy_switch()
1786 hlist_del_init(&bfqq->burst_list_node); in bfq_bfqq_handle_idle_busy_switch()
1792 if (bfqd->low_latency) { in bfq_bfqq_handle_idle_busy_switch()
1793 if (unlikely(time_is_after_jiffies(bfqq->split_time))) in bfq_bfqq_handle_idle_busy_switch()
1795 bfqq->split_time = in bfq_bfqq_handle_idle_busy_switch()
1796 jiffies - bfqd->bfq_wr_min_idle_time - 1; in bfq_bfqq_handle_idle_busy_switch()
1798 if (time_is_before_jiffies(bfqq->split_time + in bfq_bfqq_handle_idle_busy_switch()
1799 bfqd->bfq_wr_min_idle_time)) { in bfq_bfqq_handle_idle_busy_switch()
1807 if (old_wr_coeff != bfqq->wr_coeff) in bfq_bfqq_handle_idle_busy_switch()
1808 bfqq->entity.prio_changed = 1; in bfq_bfqq_handle_idle_busy_switch()
1812 bfqq->last_idle_bklogged = jiffies; in bfq_bfqq_handle_idle_busy_switch()
1813 bfqq->service_from_backlogged = 0; in bfq_bfqq_handle_idle_busy_switch()
1819 * Expire in-service queue if preemption may be needed for in bfq_bfqq_handle_idle_busy_switch()
1825 * carry time-critical I/O, then bfqq's bandwidth is less in bfq_bfqq_handle_idle_busy_switch()
1826 * important than that of queues that carry time-critical I/O. in bfq_bfqq_handle_idle_busy_switch()
1828 * bfqq is at least as weight-raised, i.e., at least as time in bfq_bfqq_handle_idle_busy_switch()
1829 * critical, as the in-service queue. in bfq_bfqq_handle_idle_busy_switch()
1832 * or has a higher weight than the in-service queue. If this in bfq_bfqq_handle_idle_busy_switch()
1839 * the timestamps of both bfqq and of the in-service queue, in bfq_bfqq_handle_idle_busy_switch()
1846 * timestamps of the in-service queue would need to be in bfq_bfqq_handle_idle_busy_switch()
1858 * request to arrive for the currently in-service queue, but in bfq_bfqq_handle_idle_busy_switch()
1861 if (bfqd->in_service_queue && in bfq_bfqq_handle_idle_busy_switch()
1863 bfqq->wr_coeff >= bfqd->in_service_queue->wr_coeff) || in bfq_bfqq_handle_idle_busy_switch()
1864 bfq_bfqq_higher_class_or_weight(bfqq, bfqd->in_service_queue) || in bfq_bfqq_handle_idle_busy_switch()
1865 !bfq_better_to_idle(bfqd->in_service_queue)) && in bfq_bfqq_handle_idle_busy_switch()
1867 bfq_bfqq_expire(bfqd, bfqd->in_service_queue, in bfq_bfqq_handle_idle_busy_switch()
1875 bfqq->last_serv_time_ns = 0; in bfq_reset_inject_limit()
1881 bfqd->waited_rq = NULL; in bfq_reset_inject_limit()
1894 * get new I/O enqueued---and then completed---before being in bfq_reset_inject_limit()
1896 * limit-update algorithm the chance to measure the effect of in bfq_reset_inject_limit()
1926 * limit-update algorithm and possibly raise the limit to more in bfq_reset_inject_limit()
1930 bfqq->inject_limit = 0; in bfq_reset_inject_limit()
1932 bfqq->inject_limit = 1; in bfq_reset_inject_limit()
1934 bfqq->decrease_time_jif = jiffies; in bfq_reset_inject_limit()
1939 u64 tot_io_time = now_ns - bfqq->io_start_time; in bfq_update_io_intensity()
1941 if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfqq->dispatched == 0) in bfq_update_io_intensity()
1942 bfqq->tot_idle_time += in bfq_update_io_intensity()
1943 now_ns - bfqq->ttime.last_end_request; in bfq_update_io_intensity()
1952 if (bfqq->tot_idle_time * 5 > tot_io_time) in bfq_update_io_intensity()
1962 bfqq->io_start_time = now_ns - (tot_io_time>>1); in bfq_update_io_intensity()
1963 bfqq->tot_idle_time >>= 1; in bfq_update_io_intensity()
1989 * for a waker if it still has some in-flight I/O. In fact, in this
1991 * receive new I/O on the completion of some of the in-flight
1998 * last filter, plus the above three-times requirement, make false
2010 * first I/O-plugging time interval for bfqq. This triggers the first
2013 * during the next I/O-plugging interval for bfqq.
2022 if (!bfqd->last_completed_rq_bfqq || in bfq_check_waker()
2023 bfqd->last_completed_rq_bfqq == bfqq || in bfq_check_waker()
2025 bfqq->dispatched > 0 || in bfq_check_waker()
2026 now_ns - bfqd->last_completion >= 4 * NSEC_PER_MSEC || in bfq_check_waker()
2027 bfqd->last_completed_rq_bfqq == bfqq->waker_bfqq) in bfq_check_waker()
2030 if (bfqd->last_completed_rq_bfqq != in bfq_check_waker()
2031 bfqq->tentative_waker_bfqq) { in bfq_check_waker()
2037 bfqq->tentative_waker_bfqq = in bfq_check_waker()
2038 bfqd->last_completed_rq_bfqq; in bfq_check_waker()
2039 bfqq->num_waker_detections = 1; in bfq_check_waker()
2041 bfqq->num_waker_detections++; in bfq_check_waker()
2043 if (bfqq->num_waker_detections == 3) { in bfq_check_waker()
2044 bfqq->waker_bfqq = bfqd->last_completed_rq_bfqq; in bfq_check_waker()
2045 bfqq->tentative_waker_bfqq = NULL; in bfq_check_waker()
2049 * bfqq->waker_bfqq must be reset. To in bfq_check_waker()
2067 if (!hlist_unhashed(&bfqq->woken_list_node)) in bfq_check_waker()
2068 hlist_del_init(&bfqq->woken_list_node); in bfq_check_waker()
2069 hlist_add_head(&bfqq->woken_list_node, in bfq_check_waker()
2070 &bfqd->last_completed_rq_bfqq->woken_list); in bfq_check_waker()
2077 struct bfq_data *bfqd = bfqq->bfqd; in bfq_add_request()
2079 unsigned int old_wr_coeff = bfqq->wr_coeff; in bfq_add_request()
2084 bfqq->queued[rq_is_sync(rq)]++; in bfq_add_request()
2085 bfqd->queued++; in bfq_add_request()
2087 if (RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_bfqq_sync(bfqq)) { in bfq_add_request()
2096 if (time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_add_request()
2104 * - bfqq is in service, because the total service in bfq_add_request()
2107 * - this is the right occasion to compute or to in bfq_add_request()
2118 * hole, and there are still in-flight requests, in bfq_add_request()
2121 * - the minimum interval for sampling the total in bfq_add_request()
2125 if (bfqq == bfqd->in_service_queue && in bfq_add_request()
2126 (bfqd->rq_in_driver == 0 || in bfq_add_request()
2127 (bfqq->last_serv_time_ns > 0 && in bfq_add_request()
2128 bfqd->rqs_injected && bfqd->rq_in_driver > 0)) && in bfq_add_request()
2129 time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_add_request()
2131 bfqd->last_empty_occupied_ns = ktime_get_ns(); in bfq_add_request()
2135 * wait_dispatch will cause bfqd->waited_rq to in bfq_add_request()
2138 bfqd->wait_dispatch = true; in bfq_add_request()
2152 if (bfqd->rq_in_driver == 0) in bfq_add_request()
2153 bfqd->rqs_injected = false; in bfq_add_request()
2160 elv_rb_add(&bfqq->sort_list, rq); in bfq_add_request()
2163 * Check if this request is a better next-serve candidate. in bfq_add_request()
2165 prev = bfqq->next_rq; in bfq_add_request()
2166 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, rq, bfqd->last_position); in bfq_add_request()
2167 bfqq->next_rq = next_rq; in bfq_add_request()
2173 if (unlikely(!bfqd->nonrot_with_queueing && prev != bfqq->next_rq)) in bfq_add_request()
2180 if (bfqd->low_latency && old_wr_coeff == 1 && !rq_is_sync(rq) && in bfq_add_request()
2182 bfqq->last_wr_start_finish + in bfq_add_request()
2183 bfqd->bfq_wr_min_inter_arr_async)) { in bfq_add_request()
2184 bfqq->wr_coeff = bfqd->bfq_wr_coeff; in bfq_add_request()
2185 bfqq->wr_cur_max_time = bfq_wr_duration(bfqd); in bfq_add_request()
2187 bfqd->wr_busy_queues++; in bfq_add_request()
2188 bfqq->entity.prio_changed = 1; in bfq_add_request()
2190 if (prev != bfqq->next_rq) in bfq_add_request()
2198 * . if bfqq is not going to be weight-raised, because, for in bfq_add_request()
2199 * non weight-raised queues, last_wr_start_finish stores the in bfq_add_request()
2202 * weight-raise async queues in bfq_add_request()
2204 * . if bfqq is not weight-raised, because, if bfqq is now in bfq_add_request()
2205 * switching to weight-raised, then last_wr_start_finish in bfq_add_request()
2206 * stores the time when weight-raising starts in bfq_add_request()
2209 * bfqq is currently weight-raised, the weight-raising in bfq_add_request()
2212 * conditions, if bfqq is already weight-raised) in bfq_add_request()
2215 * real-time, because the weight-raising period is constantly in bfq_add_request()
2216 * restarted on idle-to-busy transitions for these queues, but in bfq_add_request()
2220 if (bfqd->low_latency && in bfq_add_request()
2221 (old_wr_coeff == 1 || bfqq->wr_coeff == 1 || interactive)) in bfq_add_request()
2222 bfqq->last_wr_start_finish = jiffies; in bfq_add_request()
2229 struct bfq_queue *bfqq = bfqd->bio_bfqq; in bfq_find_rq_fmerge()
2233 return elv_rb_find(&bfqq->sort_list, bio_end_sector(bio)); in bfq_find_rq_fmerge()
2241 return abs(blk_rq_pos(rq) - last_pos); in get_sdist()
2249 struct bfq_data *bfqd = q->elevator->elevator_data;
2251 bfqd->rq_in_driver++;
2256 struct bfq_data *bfqd = q->elevator->elevator_data;
2258 bfqd->rq_in_driver--;
2266 struct bfq_data *bfqd = bfqq->bfqd; in bfq_remove_request()
2269 if (bfqq->next_rq == rq) { in bfq_remove_request()
2270 bfqq->next_rq = bfq_find_next_rq(bfqd, bfqq, rq); in bfq_remove_request()
2274 if (rq->queuelist.prev != &rq->queuelist) in bfq_remove_request()
2275 list_del_init(&rq->queuelist); in bfq_remove_request()
2276 bfqq->queued[sync]--; in bfq_remove_request()
2277 bfqd->queued--; in bfq_remove_request()
2278 elv_rb_del(&bfqq->sort_list, rq); in bfq_remove_request()
2281 if (q->last_merge == rq) in bfq_remove_request()
2282 q->last_merge = NULL; in bfq_remove_request()
2284 if (RB_EMPTY_ROOT(&bfqq->sort_list)) { in bfq_remove_request()
2285 bfqq->next_rq = NULL; in bfq_remove_request()
2287 if (bfq_bfqq_busy(bfqq) && bfqq != bfqd->in_service_queue) { in bfq_remove_request()
2291 * bfqq is empty, bfqq->entity.service and in bfq_remove_request()
2292 * bfqq->entity.budget must contain, in bfq_remove_request()
2298 * reset both bfqq->entity.service and in bfq_remove_request()
2299 * bfqq->entity.budget, if bfqq has still a in bfq_remove_request()
2302 bfqq->entity.budget = bfqq->entity.service = 0; in bfq_remove_request()
2306 * Remove queue from request-position tree as it is empty. in bfq_remove_request()
2308 if (bfqq->pos_root) { in bfq_remove_request()
2309 rb_erase(&bfqq->pos_node, bfqq->pos_root); in bfq_remove_request()
2310 bfqq->pos_root = NULL; in bfq_remove_request()
2314 if (unlikely(!bfqd->nonrot_with_queueing)) in bfq_remove_request()
2318 if (rq->cmd_flags & REQ_META) in bfq_remove_request()
2319 bfqq->meta_pending--; in bfq_remove_request()
2326 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_bio_merge()
2331 * queue_lock inside the bfqd->lock. We assume that the bic in bfq_bio_merge()
2333 * bfqd->lock is taken. in bfq_bio_merge()
2335 struct bfq_io_cq *bic = bfq_bic_lookup(bfqd, current->io_context, q); in bfq_bio_merge()
2338 spin_lock_irq(&bfqd->lock); in bfq_bio_merge()
2341 bfqd->bio_bfqq = bic_to_bfqq(bic, op_is_sync(bio->bi_opf)); in bfq_bio_merge()
2343 bfqd->bio_bfqq = NULL; in bfq_bio_merge()
2344 bfqd->bio_bic = bic; in bfq_bio_merge()
2348 spin_unlock_irq(&bfqd->lock); in bfq_bio_merge()
2358 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_request_merge()
2379 rb_prev(&req->rb_node) && in bfq_request_merged()
2381 blk_rq_pos(container_of(rb_prev(&req->rb_node), in bfq_request_merged()
2390 bfqd = bfqq->bfqd; in bfq_request_merged()
2393 elv_rb_del(&bfqq->sort_list, req); in bfq_request_merged()
2394 elv_rb_add(&bfqq->sort_list, req); in bfq_request_merged()
2397 prev = bfqq->next_rq; in bfq_request_merged()
2398 next_rq = bfq_choose_req(bfqd, bfqq->next_rq, req, in bfq_request_merged()
2399 bfqd->last_position); in bfq_request_merged()
2400 bfqq->next_rq = next_rq; in bfq_request_merged()
2406 if (prev != bfqq->next_rq) { in bfq_request_merged()
2412 if (unlikely(!bfqd->nonrot_with_queueing)) in bfq_request_merged()
2426 * on that rq is picked from the hash table q->elevator->hash, which,
2451 !list_empty(&rq->queuelist) && !list_empty(&next->queuelist) && in bfq_requests_merged()
2452 next->fifo_time < rq->fifo_time) { in bfq_requests_merged()
2453 list_del_init(&rq->queuelist); in bfq_requests_merged()
2454 list_replace_init(&next->queuelist, &rq->queuelist); in bfq_requests_merged()
2455 rq->fifo_time = next->fifo_time; in bfq_requests_merged()
2458 if (bfqq->next_rq == next) in bfq_requests_merged()
2459 bfqq->next_rq = rq; in bfq_requests_merged()
2461 bfqg_stats_update_io_merged(bfqq_group(bfqq), next->cmd_flags); in bfq_requests_merged()
2464 if (!RB_EMPTY_NODE(&next->rb_node)) { in bfq_requests_merged()
2465 bfq_remove_request(next->q, next); in bfq_requests_merged()
2468 next->cmd_flags); in bfq_requests_merged()
2476 * If bfqq has been enjoying interactive weight-raising, then in bfq_bfqq_end_wr()
2479 * a soft real-time application. Such an application actually in bfq_bfqq_end_wr()
2480 * exhibits a soft real-time I/O pattern after it finishes in bfq_bfqq_end_wr()
2489 if (bfqq->wr_cur_max_time != in bfq_bfqq_end_wr()
2490 bfqq->bfqd->bfq_wr_rt_max_time) in bfq_bfqq_end_wr()
2491 bfqq->soft_rt_next_start = jiffies; in bfq_bfqq_end_wr()
2494 bfqq->bfqd->wr_busy_queues--; in bfq_bfqq_end_wr()
2495 bfqq->wr_coeff = 1; in bfq_bfqq_end_wr()
2496 bfqq->wr_cur_max_time = 0; in bfq_bfqq_end_wr()
2497 bfqq->last_wr_start_finish = jiffies; in bfq_bfqq_end_wr()
2502 bfqq->entity.prio_changed = 1; in bfq_bfqq_end_wr()
2512 if (bfqg->async_bfqq[i][j]) in bfq_end_wr_async_queues()
2513 bfq_bfqq_end_wr(bfqg->async_bfqq[i][j]); in bfq_end_wr_async_queues()
2514 if (bfqg->async_idle_bfqq) in bfq_end_wr_async_queues()
2515 bfq_bfqq_end_wr(bfqg->async_idle_bfqq); in bfq_end_wr_async_queues()
2522 spin_lock_irq(&bfqd->lock); in bfq_end_wr()
2524 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) in bfq_end_wr()
2526 list_for_each_entry(bfqq, &bfqd->idle_list, bfqq_list) in bfq_end_wr()
2530 spin_unlock_irq(&bfqd->lock); in bfq_end_wr()
2538 return ((struct bio *)io_struct)->bi_iter.bi_sector; in bfq_io_struct_pos()
2544 return abs(bfq_io_struct_pos(io_struct, request) - sector) <= in bfq_rq_close_to_sector()
2552 struct rb_root *root = &bfq_bfqq_to_bfqg(bfqq)->rq_pos_tree; in bfqq_find_close()
2573 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) in bfqq_find_close()
2576 if (blk_rq_pos(__bfqq->next_rq) < sector) in bfqq_find_close()
2577 node = rb_next(&__bfqq->pos_node); in bfqq_find_close()
2579 node = rb_prev(&__bfqq->pos_node); in bfqq_find_close()
2584 if (bfq_rq_close_to_sector(__bfqq->next_rq, true, sector)) in bfqq_find_close()
2618 * unsafe to follow the ->new_bfqq chain as other bfqq's in the chain in bfq_setup_merge()
2626 while ((__bfqq = new_bfqq->new_bfqq)) { in bfq_setup_merge()
2641 bfq_log_bfqq(bfqq->bfqd, bfqq, "scheduling merge with queue %d", in bfq_setup_merge()
2642 new_bfqq->pid); in bfq_setup_merge()
2656 * not available any more (new_bfqq->bic == NULL). in bfq_setup_merge()
2658 * Anyway, even in case new_bfqq coincides with the in-service in bfq_setup_merge()
2659 * queue, redirecting requests the in-service queue is the in bfq_setup_merge()
2660 * best option, as we feed the in-service queue with new in bfq_setup_merge()
2664 bfqq->new_bfqq = new_bfqq; in bfq_setup_merge()
2665 new_bfqq->ref += process_refs; in bfq_setup_merge()
2676 (bfqq->ioprio_class != new_bfqq->ioprio_class)) in bfq_may_be_close_cooperator()
2702 * Attempt to schedule a merge of bfqq with the currently in-service
2714 * WARNING: queue merging may impair fairness among non-weight raised
2718 * requests than the ones produced by its originally-associated
2728 * Check delayed stable merge for rotational or non-queueing in bfq_setup_cooperator()
2730 * currently merged with some other queue (i.e., bfqq->bic in bfq_setup_cooperator()
2733 * merged with bic->stable_merge_bfqq. But this would be in bfq_setup_cooperator()
2736 if (unlikely(!bfqd->nonrot_with_queueing)) { in bfq_setup_cooperator()
2739 * bic->stable_merge_bfqq may point to some queue (for in bfq_setup_cooperator()
2743 if (bfq_bfqq_sync(bfqq) && bic->stable_merge_bfqq && in bfq_setup_cooperator()
2745 time_is_before_jiffies(bfqq->split_time + in bfq_setup_cooperator()
2747 time_is_before_jiffies(bfqq->creation_time + in bfq_setup_cooperator()
2750 bic->stable_merge_bfqq; in bfq_setup_cooperator()
2757 bic->stable_merge_bfqq = NULL; in bfq_setup_cooperator()
2765 bic->stably_merged = true; in bfq_setup_cooperator()
2766 if (new_bfqq && new_bfqq->bic) in bfq_setup_cooperator()
2767 new_bfqq->bic->stably_merged = true; in bfq_setup_cooperator()
2792 * non-merged queues. This may accentuate workload in bfq_setup_cooperator()
2811 if (likely(bfqd->nonrot_with_queueing)) in bfq_setup_cooperator()
2821 * probability that two non-cooperating processes, which just in bfq_setup_cooperator()
2828 if (bfqq->new_bfqq) in bfq_setup_cooperator()
2829 return bfqq->new_bfqq; in bfq_setup_cooperator()
2831 if (!io_struct || unlikely(bfqq == &bfqd->oom_bfqq)) in bfq_setup_cooperator()
2838 in_service_bfqq = bfqd->in_service_queue; in bfq_setup_cooperator()
2841 likely(in_service_bfqq != &bfqd->oom_bfqq) && in bfq_setup_cooperator()
2843 bfqd->in_serv_last_pos) && in bfq_setup_cooperator()
2844 bfqq->entity.parent == in_service_bfqq->entity.parent && in bfq_setup_cooperator()
2858 if (new_bfqq && likely(new_bfqq != &bfqd->oom_bfqq) && in bfq_setup_cooperator()
2867 struct bfq_io_cq *bic = bfqq->bic; in bfq_bfqq_save_state()
2870 * If !bfqq->bic, the queue is already shared or its requests in bfq_bfqq_save_state()
2877 bic->saved_last_serv_time_ns = bfqq->last_serv_time_ns; in bfq_bfqq_save_state()
2878 bic->saved_inject_limit = bfqq->inject_limit; in bfq_bfqq_save_state()
2879 bic->saved_decrease_time_jif = bfqq->decrease_time_jif; in bfq_bfqq_save_state()
2881 bic->saved_weight = bfqq->entity.orig_weight; in bfq_bfqq_save_state()
2882 bic->saved_ttime = bfqq->ttime; in bfq_bfqq_save_state()
2883 bic->saved_has_short_ttime = bfq_bfqq_has_short_ttime(bfqq); in bfq_bfqq_save_state()
2884 bic->saved_IO_bound = bfq_bfqq_IO_bound(bfqq); in bfq_bfqq_save_state()
2885 bic->saved_io_start_time = bfqq->io_start_time; in bfq_bfqq_save_state()
2886 bic->saved_tot_idle_time = bfqq->tot_idle_time; in bfq_bfqq_save_state()
2887 bic->saved_in_large_burst = bfq_bfqq_in_large_burst(bfqq); in bfq_bfqq_save_state()
2888 bic->was_in_burst_list = !hlist_unhashed(&bfqq->burst_list_node); in bfq_bfqq_save_state()
2891 bfqq->bfqd->low_latency)) { in bfq_bfqq_save_state()
2895 * did not make it to be set in a weight-raised state, in bfq_bfqq_save_state()
2897 * weight-raising state that would have been assigned in bfq_bfqq_save_state()
2901 bic->saved_wr_coeff = bfqq->bfqd->bfq_wr_coeff; in bfq_bfqq_save_state()
2902 bic->saved_wr_start_at_switch_to_srt = bfq_smallest_from_now(); in bfq_bfqq_save_state()
2903 bic->saved_wr_cur_max_time = bfq_wr_duration(bfqq->bfqd); in bfq_bfqq_save_state()
2904 bic->saved_last_wr_start_finish = jiffies; in bfq_bfqq_save_state()
2906 bic->saved_wr_coeff = bfqq->wr_coeff; in bfq_bfqq_save_state()
2907 bic->saved_wr_start_at_switch_to_srt = in bfq_bfqq_save_state()
2908 bfqq->wr_start_at_switch_to_srt; in bfq_bfqq_save_state()
2909 bic->saved_service_from_wr = bfqq->service_from_wr; in bfq_bfqq_save_state()
2910 bic->saved_last_wr_start_finish = bfqq->last_wr_start_finish; in bfq_bfqq_save_state()
2911 bic->saved_wr_cur_max_time = bfqq->wr_cur_max_time; in bfq_bfqq_save_state()
2919 if (cur_bfqq->entity.parent && in bfq_reassign_last_bfqq()
2920 cur_bfqq->entity.parent->last_bfqq_created == cur_bfqq) in bfq_reassign_last_bfqq()
2921 cur_bfqq->entity.parent->last_bfqq_created = new_bfqq; in bfq_reassign_last_bfqq()
2922 else if (cur_bfqq->bfqd && cur_bfqq->bfqd->last_bfqq_created == cur_bfqq) in bfq_reassign_last_bfqq()
2923 cur_bfqq->bfqd->last_bfqq_created = new_bfqq; in bfq_reassign_last_bfqq()
2939 if (bfq_bfqq_busy(bfqq) && RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_release_process_ref()
2940 bfqq != bfqd->in_service_queue) in bfq_release_process_ref()
2953 (unsigned long)new_bfqq->pid); in bfq_merge_bfqqs()
2968 if (bfqq->waker_bfqq && !new_bfqq->waker_bfqq && in bfq_merge_bfqqs()
2969 bfqq->waker_bfqq != new_bfqq) { in bfq_merge_bfqqs()
2970 new_bfqq->waker_bfqq = bfqq->waker_bfqq; in bfq_merge_bfqqs()
2971 new_bfqq->tentative_waker_bfqq = NULL; in bfq_merge_bfqqs()
2975 * new_bfqq->waker_bfqq must be reset. So insert in bfq_merge_bfqqs()
2979 hlist_add_head(&new_bfqq->woken_list_node, in bfq_merge_bfqqs()
2980 &new_bfqq->waker_bfqq->woken_list); in bfq_merge_bfqqs()
2985 * If bfqq is weight-raised, then let new_bfqq inherit in bfq_merge_bfqqs()
2986 * weight-raising. To reduce false positives, neglect the case in bfq_merge_bfqqs()
2988 * to be weight-raised (which may happen because EQM may merge in bfq_merge_bfqqs()
2993 if (new_bfqq->wr_coeff == 1 && bfqq->wr_coeff > 1) { in bfq_merge_bfqqs()
2994 new_bfqq->wr_coeff = bfqq->wr_coeff; in bfq_merge_bfqqs()
2995 new_bfqq->wr_cur_max_time = bfqq->wr_cur_max_time; in bfq_merge_bfqqs()
2996 new_bfqq->last_wr_start_finish = bfqq->last_wr_start_finish; in bfq_merge_bfqqs()
2997 new_bfqq->wr_start_at_switch_to_srt = in bfq_merge_bfqqs()
2998 bfqq->wr_start_at_switch_to_srt; in bfq_merge_bfqqs()
3000 bfqd->wr_busy_queues++; in bfq_merge_bfqqs()
3001 new_bfqq->entity.prio_changed = 1; in bfq_merge_bfqqs()
3004 if (bfqq->wr_coeff > 1) { /* bfqq has given its wr to new_bfqq */ in bfq_merge_bfqqs()
3005 bfqq->wr_coeff = 1; in bfq_merge_bfqqs()
3006 bfqq->entity.prio_changed = 1; in bfq_merge_bfqqs()
3008 bfqd->wr_busy_queues--; in bfq_merge_bfqqs()
3012 bfqd->wr_busy_queues); in bfq_merge_bfqqs()
3021 * set new_bfqq->bic to NULL. bfqq either: in bfq_merge_bfqqs()
3022 * - does not belong to any bic any more, and hence bfqq->bic must in bfq_merge_bfqqs()
3024 * - is a queue whose owning bics have already been redirected to a in bfq_merge_bfqqs()
3026 * any bic soon and bfqq->bic is already NULL (therefore the next in bfq_merge_bfqqs()
3029 new_bfqq->bic = NULL; in bfq_merge_bfqqs()
3036 * We mark such a queue with a pid -1, and then print SHARED instead of in bfq_merge_bfqqs()
3039 new_bfqq->pid = -1; in bfq_merge_bfqqs()
3040 bfqq->bic = NULL; in bfq_merge_bfqqs()
3050 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_allow_bio_merge()
3051 bool is_sync = op_is_sync(bio->bi_opf); in bfq_allow_bio_merge()
3052 struct bfq_queue *bfqq = bfqd->bio_bfqq, *new_bfqq; in bfq_allow_bio_merge()
3071 new_bfqq = bfq_setup_cooperator(bfqd, bfqq, bio, false, bfqd->bio_bic); in bfq_allow_bio_merge()
3080 bfq_merge_bfqqs(bfqd, bfqd->bio_bic, bfqq, in bfq_allow_bio_merge()
3090 * Change also bqfd->bio_bfqq, as in bfq_allow_bio_merge()
3091 * bfqd->bio_bic now points to new_bfqq, and in bfq_allow_bio_merge()
3093 * use again bqfd->bio_bfqq). in bfq_allow_bio_merge()
3095 bfqd->bio_bfqq = bfqq; in bfq_allow_bio_merge()
3102 * Set the maximum time for the in-service queue to consume its
3104 * In practice, a time-slice service scheme is used with seeky
3112 if (bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time) in bfq_set_budget_timeout()
3115 timeout_coeff = bfqq->entity.weight / bfqq->entity.orig_weight; in bfq_set_budget_timeout()
3117 bfqd->last_budget_start = ktime_get(); in bfq_set_budget_timeout()
3119 bfqq->budget_timeout = jiffies + in bfq_set_budget_timeout()
3120 bfqd->bfq_timeout * timeout_coeff; in bfq_set_budget_timeout()
3129 bfqd->budgets_assigned = (bfqd->budgets_assigned * 7 + 256) / 8; in __bfq_set_in_service_queue()
3131 if (time_is_before_jiffies(bfqq->last_wr_start_finish) && in __bfq_set_in_service_queue()
3132 bfqq->wr_coeff > 1 && in __bfq_set_in_service_queue()
3133 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in __bfq_set_in_service_queue()
3134 time_is_before_jiffies(bfqq->budget_timeout)) { in __bfq_set_in_service_queue()
3136 * For soft real-time queues, move the start in __bfq_set_in_service_queue()
3137 * of the weight-raising period forward by the in __bfq_set_in_service_queue()
3141 * weight-raising period of the queue to end, in __bfq_set_in_service_queue()
3143 * weight-raising period of a soft real-time in __bfq_set_in_service_queue()
3146 * because soft real-time queues are not in __bfq_set_in_service_queue()
3159 if (time_after(bfqq->budget_timeout, in __bfq_set_in_service_queue()
3160 bfqq->last_wr_start_finish)) in __bfq_set_in_service_queue()
3161 bfqq->last_wr_start_finish += in __bfq_set_in_service_queue()
3162 jiffies - bfqq->budget_timeout; in __bfq_set_in_service_queue()
3164 bfqq->last_wr_start_finish = jiffies; in __bfq_set_in_service_queue()
3169 "set_in_service_queue, cur-budget = %d", in __bfq_set_in_service_queue()
3170 bfqq->entity.budget); in __bfq_set_in_service_queue()
3173 bfqd->in_service_queue = bfqq; in __bfq_set_in_service_queue()
3174 bfqd->in_serv_last_pos = 0; in __bfq_set_in_service_queue()
3190 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_arm_slice_timer()
3197 * fair distribution of slice time for a process doing back-to-back in bfq_arm_slice_timer()
3200 sl = bfqd->bfq_slice_idle; in bfq_arm_slice_timer()
3202 * Unless the queue is being weight-raised or the scenario is in bfq_arm_slice_timer()
3204 * is seeky. A long idling is preserved for a weight-raised in bfq_arm_slice_timer()
3211 if (BFQQ_SEEKY(bfqq) && bfqq->wr_coeff == 1 && in bfq_arm_slice_timer()
3214 else if (bfqq->wr_coeff > 1) in bfq_arm_slice_timer()
3217 bfqd->last_idling_start = ktime_get(); in bfq_arm_slice_timer()
3218 bfqd->last_idling_start_jiffies = jiffies; in bfq_arm_slice_timer()
3220 hrtimer_start(&bfqd->idle_slice_timer, ns_to_ktime(sl), in bfq_arm_slice_timer()
3229 * budget, even if the in-service queue is served at peak rate. And
3234 return (u64)bfqd->peak_rate * USEC_PER_MSEC * in bfq_calc_max_budget()
3235 jiffies_to_msecs(bfqd->bfq_timeout)>>BFQ_RATE_SHIFT; in bfq_calc_max_budget()
3245 if (bfqd->bfq_user_max_budget == 0) { in update_thr_responsiveness_params()
3246 bfqd->bfq_max_budget = in update_thr_responsiveness_params()
3248 bfq_log(bfqd, "new max_budget = %d", bfqd->bfq_max_budget); in update_thr_responsiveness_params()
3256 bfqd->last_dispatch = bfqd->first_dispatch = ktime_get_ns(); in bfq_reset_rate_computation()
3257 bfqd->peak_rate_samples = 1; in bfq_reset_rate_computation()
3258 bfqd->sequential_samples = 0; in bfq_reset_rate_computation()
3259 bfqd->tot_sectors_dispatched = bfqd->last_rq_max_size = in bfq_reset_rate_computation()
3262 bfqd->peak_rate_samples = 0; /* full re-init on next disp. */ in bfq_reset_rate_computation()
3266 bfqd->peak_rate_samples, bfqd->sequential_samples, in bfq_reset_rate_computation()
3267 bfqd->tot_sectors_dispatched); in bfq_reset_rate_computation()
3282 if (bfqd->peak_rate_samples < BFQ_RATE_MIN_SAMPLES || in bfq_update_rate_reset()
3283 bfqd->delta_from_first < BFQ_RATE_MIN_INTERVAL) in bfq_update_rate_reset()
3292 bfqd->delta_from_first = in bfq_update_rate_reset()
3293 max_t(u64, bfqd->delta_from_first, in bfq_update_rate_reset()
3294 bfqd->last_completion - bfqd->first_dispatch); in bfq_update_rate_reset()
3300 rate = div64_ul(bfqd->tot_sectors_dispatched<<BFQ_RATE_SHIFT, in bfq_update_rate_reset()
3301 div_u64(bfqd->delta_from_first, NSEC_PER_USEC)); in bfq_update_rate_reset()
3305 * - the percentage of sequential dispatches is below 3/4 of the in bfq_update_rate_reset()
3307 * - rate is unreasonably high (> 20M sectors/sec) in bfq_update_rate_reset()
3309 if ((bfqd->sequential_samples < (3 * bfqd->peak_rate_samples)>>2 && in bfq_update_rate_reset()
3310 rate <= bfqd->peak_rate) || in bfq_update_rate_reset()
3316 * we use a low-pass filter. We compute the smoothing constant in bfq_update_rate_reset()
3332 * cannot reach 9, because bfqd->sequential_samples cannot in bfq_update_rate_reset()
3333 * become equal to bfqd->peak_rate_samples, which, in its in bfq_update_rate_reset()
3334 * turn, holds true because bfqd->sequential_samples is not in bfq_update_rate_reset()
3337 weight = (9 * bfqd->sequential_samples) / bfqd->peak_rate_samples; in bfq_update_rate_reset()
3344 div_u64(weight * bfqd->delta_from_first, in bfq_update_rate_reset()
3351 divisor = 10 - weight; in bfq_update_rate_reset()
3356 * peak_rate = peak_rate * (divisor-1) / divisor + rate / divisor in bfq_update_rate_reset()
3358 bfqd->peak_rate *= divisor-1; in bfq_update_rate_reset()
3359 bfqd->peak_rate /= divisor; in bfq_update_rate_reset()
3362 bfqd->peak_rate += rate; in bfq_update_rate_reset()
3365 * For a very slow device, bfqd->peak_rate can reach 0 (see in bfq_update_rate_reset()
3368 * divisions by zero where bfqd->peak_rate is used as a in bfq_update_rate_reset()
3371 bfqd->peak_rate = max_t(u32, 1, bfqd->peak_rate); in bfq_update_rate_reset()
3381 * auto-tuning, see update_thr_responsiveness_params()).
3396 * unknown, namely in-device request service rate.
3415 if (bfqd->peak_rate_samples == 0) { /* first dispatch */ in bfq_update_peak_rate()
3417 bfqd->peak_rate_samples); in bfq_update_peak_rate()
3425 * for computing a new peak rate (similarly to the late- in bfq_update_peak_rate()
3429 * - close the observation interval at the last (previous) in bfq_update_peak_rate()
3431 * - compute rate, if possible, for that observation interval in bfq_update_peak_rate()
3432 * - start a new observation interval with this dispatch in bfq_update_peak_rate()
3434 if (now_ns - bfqd->last_dispatch > 100*NSEC_PER_MSEC && in bfq_update_peak_rate()
3435 bfqd->rq_in_driver == 0) in bfq_update_peak_rate()
3439 bfqd->peak_rate_samples++; in bfq_update_peak_rate()
3441 if ((bfqd->rq_in_driver > 0 || in bfq_update_peak_rate()
3442 now_ns - bfqd->last_completion < BFQ_MIN_TT) in bfq_update_peak_rate()
3443 && !BFQ_RQ_SEEKY(bfqd, bfqd->last_position, rq)) in bfq_update_peak_rate()
3444 bfqd->sequential_samples++; in bfq_update_peak_rate()
3446 bfqd->tot_sectors_dispatched += blk_rq_sectors(rq); in bfq_update_peak_rate()
3449 if (likely(bfqd->peak_rate_samples % 32)) in bfq_update_peak_rate()
3450 bfqd->last_rq_max_size = in bfq_update_peak_rate()
3451 max_t(u32, blk_rq_sectors(rq), bfqd->last_rq_max_size); in bfq_update_peak_rate()
3453 bfqd->last_rq_max_size = blk_rq_sectors(rq); in bfq_update_peak_rate()
3455 bfqd->delta_from_first = now_ns - bfqd->first_dispatch; in bfq_update_peak_rate()
3458 if (bfqd->delta_from_first < BFQ_RATE_REF_INTERVAL) in bfq_update_peak_rate()
3464 bfqd->last_position = blk_rq_pos(rq) + blk_rq_sectors(rq); in bfq_update_peak_rate()
3465 if (RQ_BFQQ(rq) == bfqd->in_service_queue) in bfq_update_peak_rate()
3466 bfqd->in_serv_last_pos = bfqd->last_position; in bfq_update_peak_rate()
3467 bfqd->last_dispatch = now_ns; in bfq_update_peak_rate()
3483 * dispatch occur for a non in-service bfqq, this anticipated in bfq_dispatch_remove()
3484 * increment prevents two counters related to bfqq->dispatched in bfq_dispatch_remove()
3486 * incremented again when the (new) value of bfqq->dispatched in bfq_dispatch_remove()
3489 bfqq->dispatched++; in bfq_dispatch_remove()
3490 bfq_update_peak_rate(q->elevator->elevator_data, rq); in bfq_dispatch_remove()
3508 * the service order of the internally-queued requests, does
3511 * concern about per-process throughput distribution, and
3512 * makes its decisions only on a per-request basis. Therefore,
3517 * (i-a) each of these processes must get the same throughput as
3519 * (i-b) in case (i-a) does not hold, it holds that the process
3525 * (from I/O-bound to sporadic), and so on;
3531 * distribution if (i-a) holds, or, if (i-b) holds instead, this is an
3536 * idling (I/O-dispatch plugging) is certainly needed to guarantee
3544 * throughput, it is important to check conditions (i-a), i(-b) and
3550 * very difficult to check conditions (i-a) and (i-b) too. In fact,
3551 * if there are active groups, then, for conditions (i-a) or (i-b) to
3553 * contains more active processes or sub-groups than some other active
3554 * group. More precisely, for conditions (i-a) or (i-b) to become
3562 * inactive while still having in-flight requests, and if, when this
3568 * bi-modal behavior, implemented in the function
3575 * conditions (i-a), (i-b) or (ii). So the device is idled for bfqq.
3581 * for completion, then only conditions (i-a) and (i-b) are actually
3582 * controlled, i.e., provided that conditions (i-a) or (i-b) holds,
3584 * holds. In other words, only if conditions (i-a) and (i-b) do not
3588 * control conditions (i-a) and (i-b) it is enough to check just
3601 * can still preempt the new in-service queue if the next
3605 * combined with the hole-recovery heuristic described in the
3612 * minimum of mid-term fairness.
3614 * More precisely, this preemption-based, idleless approach
3638 * We are now left only with explaining the two sub-conditions in the
3641 * sub-condition, we need to add that the function
3643 * non-weight-raised queues, for efficiency reasons (see comments on
3644 * bfq_weights_tree_add()). Then the fact that bfqq is weight-raised
3647 * weight-raised, the scenario is still symmetric if all queues with
3649 * weight-raised. Actually, we should be even more precise here, and
3650 * differentiate between interactive weight raising and soft real-time
3653 * The second sub-condition checked in the compound condition is
3654 * whether there is a fair amount of already in-flight I/O not
3656 * following reason. The drive may decide to serve in-flight
3657 * non-bfqq's I/O requests before bfqq's ones, thereby delaying the
3659 * I/O-dispatching is not plugged, then, while bfqq remains empty, a
3667 * in-flight I/O, and enables bfqq to recover the bandwidth it may
3671 * device-idling countermeasures may however fail in the following
3672 * unlucky scenario: if I/O-dispatch plugging is (correctly) disabled
3673 * in a time period during which all symmetry sub-conditions hold, and
3675 * some later point in time some sub-condition stops to hold, then it
3678 * served. The last sub-condition commented above somewhat mitigates
3679 * this problem for weight-raised queues.
3705 return (bfqq->wr_coeff > 1 && in idling_needed_for_service_guarantees()
3706 (bfqd->wr_busy_queues < in idling_needed_for_service_guarantees()
3708 bfqd->rq_in_driver >= in idling_needed_for_service_guarantees()
3709 bfqq->dispatched + 4)) || in idling_needed_for_service_guarantees()
3734 * not re-scheduled. To prevent this from happening, re-queue in __bfq_bfqq_expire()
3735 * bfqq if it needs I/O-dispatch plugging, even if it is in __bfq_bfqq_expire()
3739 if (RB_EMPTY_ROOT(&bfqq->sort_list) && in __bfq_bfqq_expire()
3742 if (bfqq->dispatched == 0) in __bfq_bfqq_expire()
3747 * the weight-raising mechanism. in __bfq_bfqq_expire()
3749 bfqq->budget_timeout = jiffies; in __bfq_bfqq_expire()
3758 if (unlikely(!bfqd->nonrot_with_queueing && in __bfq_bfqq_expire()
3759 !RB_EMPTY_ROOT(&bfqq->sort_list))) in __bfq_bfqq_expire()
3764 * All in-service entities must have been properly deactivated in __bfq_bfqq_expire()
3766 * resets all in-service entities as no more in service. This in __bfq_bfqq_expire()
3774 * __bfq_bfqq_recalc_budget - try to adapt the budget to the @bfqq behavior.
3791 if (bfqq->wr_coeff == 1) in __bfq_bfqq_recalc_budget()
3792 budget = bfqq->max_budget; in __bfq_bfqq_recalc_budget()
3794 * Use a constant, low budget for weight-raised queues, in __bfq_bfqq_recalc_budget()
3802 bfqq->entity.budget, bfq_bfqq_budget_left(bfqq)); in __bfq_bfqq_recalc_budget()
3806 bfq_bfqq_sync(bfqq), BFQQ_SEEKY(bfqd->in_service_queue)); in __bfq_bfqq_recalc_budget()
3808 if (bfq_bfqq_sync(bfqq) && bfqq->wr_coeff == 1) { in __bfq_bfqq_recalc_budget()
3839 if (bfqq->dispatched > 0) /* still outstanding reqs */ in __bfq_bfqq_recalc_budget()
3840 budget = min(budget * 2, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
3843 budget -= 4 * min_budget; in __bfq_bfqq_recalc_budget()
3855 budget = min(budget * 2, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
3867 budget = min(budget * 4, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
3881 * back-shifting. The larger the budget of the in __bfq_bfqq_recalc_budget()
3890 * many re-activations a lower finish time in __bfq_bfqq_recalc_budget()
3894 * quite precisely by bfqq->entity.service. in __bfq_bfqq_recalc_budget()
3896 * bfqq->entity.service is equal to the number in __bfq_bfqq_recalc_budget()
3902 budget = max_t(int, bfqq->entity.service, min_budget); in __bfq_bfqq_recalc_budget()
3914 budget = bfqd->bfq_max_budget; in __bfq_bfqq_recalc_budget()
3917 bfqq->max_budget = budget; in __bfq_bfqq_recalc_budget()
3919 if (bfqd->budgets_assigned >= bfq_stats_min_budgets && in __bfq_bfqq_recalc_budget()
3920 !bfqd->bfq_user_max_budget) in __bfq_bfqq_recalc_budget()
3921 bfqq->max_budget = min(bfqq->max_budget, bfqd->bfq_max_budget); in __bfq_bfqq_recalc_budget()
3933 next_rq = bfqq->next_rq; in __bfq_bfqq_recalc_budget()
3935 bfqq->entity.budget = max_t(unsigned long, bfqq->max_budget, in __bfq_bfqq_recalc_budget()
3940 bfqq->entity.budget); in __bfq_bfqq_recalc_budget()
3963 * service slots. On the opposite end, the requests of the in-service
3986 delta_ktime = bfqd->last_idling_start; in bfq_bfqq_is_slow()
3989 delta_ktime = ktime_sub(delta_ktime, bfqd->last_budget_start); in bfq_bfqq_is_slow()
3994 if (blk_queue_nonrot(bfqd->queue)) in bfq_bfqq_is_slow()
3996 * give same worst-case guarantees as idling in bfq_bfqq_is_slow()
4023 slow = bfqq->entity.service < bfqd->bfq_max_budget / 2; in bfq_bfqq_is_slow()
4032 * To be deemed as soft real-time, an application must meet two
4035 * record a compressed high-definition video.
4037 * batch, to compute the next-start time instant, soft_rt_next_start, such
4051 * Unfortunately, even a greedy (i.e., I/O-bound) application may
4058 * device: the storage device is highly loaded or reaches a low-enough
4063 * that greedy applications are deemed as soft real-time in these
4070 * namely bfqd->bfq_slice_idle, and (2) a few extra jiffies. We
4072 * jiffies; we get back to it after next item (b). Lower-bounding
4074 * bfqd->bfq_slice_idle tends to filter out greedy applications,
4077 * real-time application spends some time processing data, after a
4080 * (b) Current value of bfqq->soft_rt_next_start. As pointed out
4083 * storage-device load. In more detail, in these scenarios, these
4086 * including the filtering in above item (a). These slow-speed
4090 * I/O in the high-speed intervals, the values returned by this
4092 * high-speed interval, to be likely to fall *after* the end of
4093 * the low-speed time interval that follows. These high values are
4094 * stored in bfqq->soft_rt_next_start after each invocation of
4096 * bfqq->soft_rt_next_start is constantly used to lower-bound the
4098 * beginning of a low-speed interval, bfqq->soft_rt_next_start is
4100 * issued during the low-speed interval is considered as arriving
4102 * real-time. Then, in the high-speed interval that follows, the
4103 * application will not be deemed as soft real-time, just because
4109 * bfqd->bfq_slice_idle:
4111 * higher than bfqd->bfq_slice_idle. This happens, e.g., on slow
4113 * that the approximation, in jiffies, of bfqd->bfq_slice_idle
4119 * reference time interval just bfqd->bfq_slice_idle, but
4120 * bfqd->bfq_slice_idle plus a few jiffies. In particular, we add the
4122 * precise also in embedded systems and KVM/QEMU virtual machines.
4127 return max3(bfqq->soft_rt_next_start, in bfq_bfqq_softrt_next_start()
4128 bfqq->last_idle_bklogged + in bfq_bfqq_softrt_next_start()
4129 HZ * bfqq->service_from_backlogged / in bfq_bfqq_softrt_next_start()
4130 bfqd->bfq_wr_max_softrt_rate, in bfq_bfqq_softrt_next_start()
4131 jiffies + nsecs_to_jiffies(bfqq->bfqd->bfq_slice_idle) + 4); in bfq_bfqq_softrt_next_start()
4135 * bfq_bfqq_expire - expire a queue.
4150 * tends to lower the throughput). In addition, this time-charging
4167 struct bfq_entity *entity = &bfqq->entity; in bfq_bfqq_expire()
4176 * timed-out queues with the time and not the service in bfq_bfqq_expire()
4187 * or quasi-sequential processes. in bfq_bfqq_expire()
4189 if (bfqq->wr_coeff == 1 && in bfq_bfqq_expire()
4192 bfq_bfqq_budget_left(bfqq) >= entity->budget / 3))) in bfq_bfqq_expire()
4195 if (bfqd->low_latency && bfqq->wr_coeff == 1) in bfq_bfqq_expire()
4196 bfqq->last_wr_start_finish = jiffies; in bfq_bfqq_expire()
4198 if (bfqd->low_latency && bfqd->bfq_wr_max_softrt_rate > 0 && in bfq_bfqq_expire()
4199 RB_EMPTY_ROOT(&bfqq->sort_list)) { in bfq_bfqq_expire()
4212 if (bfqq->dispatched == 0) in bfq_bfqq_expire()
4213 bfqq->soft_rt_next_start = in bfq_bfqq_expire()
4215 else if (bfqq->dispatched > 0) { in bfq_bfqq_expire()
4226 slow, bfqq->dispatched, bfq_bfqq_has_short_ttime(bfqq)); in bfq_bfqq_expire()
4233 bfqd->rqs_injected = bfqd->wait_dispatch = false; in bfq_bfqq_expire()
4234 bfqd->waited_rq = NULL; in bfq_bfqq_expire()
4256 entity->service = 0; in bfq_bfqq_expire()
4259 * Reset the received-service counter for every parent entity. in bfq_bfqq_expire()
4260 * Differently from what happens with bfqq->entity.service, in bfq_bfqq_expire()
4264 * consumed budget, bfqq->entity.service needs to be kept, in bfq_bfqq_expire()
4266 * the same budget, the last value of bfqq->entity.service is in bfq_bfqq_expire()
4267 * needed to properly decrement bfqq->entity.budget by the in bfq_bfqq_expire()
4269 * to keep entity->service for parent entities too, because in bfq_bfqq_expire()
4270 * the bubble up of the new value of bfqq->entity.budget will in bfq_bfqq_expire()
4275 entity = entity->parent; in bfq_bfqq_expire()
4277 entity->service = 0; in bfq_bfqq_expire()
4287 return time_is_before_eq_jiffies(bfqq->budget_timeout); in bfq_bfqq_budget_timeout()
4300 bfq_log_bfqq(bfqq->bfqd, bfqq, in bfq_may_expire_for_budg_timeout()
4303 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3, in bfq_may_expire_for_budg_timeout()
4307 bfq_bfqq_budget_left(bfqq) >= bfqq->entity.budget / 3) in bfq_may_expire_for_budg_timeout()
4316 !blk_queue_nonrot(bfqd->queue) && !bfqd->hw_tag, in idling_boosts_thr_without_issues()
4333 * (a) the device is not NCQ-capable and rotational, or in idling_boosts_thr_without_issues()
4335 * the request pattern for bfqq is I/O-bound and sequential, or in idling_boosts_thr_without_issues()
4337 * not NCQ-capable and the request pattern for bfqq is in idling_boosts_thr_without_issues()
4338 * I/O-bound and sequential. in idling_boosts_thr_without_issues()
4341 * NCQ-capable flash-based device would not boost the in idling_boosts_thr_without_issues()
4346 * particular, happens to be false if bfqd is an NCQ-capable in idling_boosts_thr_without_issues()
4347 * flash-based device. in idling_boosts_thr_without_issues()
4350 ((!blk_queue_nonrot(bfqd->queue) || !bfqd->hw_tag) && in idling_boosts_thr_without_issues()
4357 * weight-raised queues. in idling_boosts_thr_without_issues()
4361 * non-weight-raised queues ask for requests at a lower rate, in idling_boosts_thr_without_issues()
4362 * then processes associated with weight-raised queues have a in idling_boosts_thr_without_issues()
4367 * weight. This is especially true with NCQ-capable drives, in idling_boosts_thr_without_issues()
4369 * reorder internally-queued requests. in idling_boosts_thr_without_issues()
4372 * there are weight-raised busy queues. In this case, and if in idling_boosts_thr_without_issues()
4373 * bfqq is not weight-raised, this guarantees that the device in idling_boosts_thr_without_issues()
4374 * is not idled for bfqq (if, instead, bfqq is weight-raised, in idling_boosts_thr_without_issues()
4378 * sync non-weight-raised queue, to get a lower number of in idling_boosts_thr_without_issues()
4381 * weight-raised queues get served again. This often mitigates in idling_boosts_thr_without_issues()
4388 bfqd->wr_busy_queues == 0; in idling_boosts_thr_without_issues()
4402 * NCQ-capable devices, this function tries to return false, so as to
4404 * device boost the throughput without causing any service-guarantee
4414 struct bfq_data *bfqd = bfqq->bfqd; in bfq_better_to_idle()
4421 if (unlikely(bfqd->strict_guarantees)) in bfq_better_to_idle()
4430 * queues in this class can steal to higher-priority queues in bfq_better_to_idle()
4432 if (bfqd->bfq_slice_idle == 0 || !bfq_bfqq_sync(bfqq) || in bfq_better_to_idle()
4453 * If the in-service queue is empty but the function bfq_better_to_idle
4465 return RB_EMPTY_ROOT(&bfqq->sort_list) && bfq_better_to_idle(bfqq); in bfq_bfqq_must_idle()
4478 struct bfq_queue *bfqq, *in_serv_bfqq = bfqd->in_service_queue; in bfq_choose_bfqq_for_injection()
4479 unsigned int limit = in_serv_bfqq->inject_limit; in bfq_choose_bfqq_for_injection()
4482 * - bfqq is not weight-raised and therefore does not carry in bfq_choose_bfqq_for_injection()
4483 * time-critical I/O, in bfq_choose_bfqq_for_injection()
4485 * - regardless of whether bfqq is weight-raised, bfqq has in bfq_choose_bfqq_for_injection()
4492 bool in_serv_always_inject = in_serv_bfqq->wr_coeff == 1 || in bfq_choose_bfqq_for_injection()
4497 * - the baseline total service time could not be sampled yet, in bfq_choose_bfqq_for_injection()
4499 * - a lot of time has elapsed since the plugging of I/O in bfq_choose_bfqq_for_injection()
4504 if (limit == 0 && in_serv_bfqq->last_serv_time_ns == 0 && in bfq_choose_bfqq_for_injection()
4506 time_is_before_eq_jiffies(bfqd->last_idling_start_jiffies + in bfq_choose_bfqq_for_injection()
4507 bfqd->bfq_slice_idle) in bfq_choose_bfqq_for_injection()
4511 if (bfqd->rq_in_driver >= limit) in bfq_choose_bfqq_for_injection()
4519 * - BFQ dynamically updates the budget of every queue so as in bfq_choose_bfqq_for_injection()
4521 * - if a queue gets all its requests dispatched as injected in bfq_choose_bfqq_for_injection()
4523 * (and re-added only if it gets new requests, but then it in bfq_choose_bfqq_for_injection()
4526 list_for_each_entry(bfqq, &bfqd->active_list, bfqq_list) in bfq_choose_bfqq_for_injection()
4527 if (!RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_choose_bfqq_for_injection()
4528 (in_serv_always_inject || bfqq->wr_coeff > 1) && in bfq_choose_bfqq_for_injection()
4529 bfq_serv_to_charge(bfqq->next_rq, bfqq) <= in bfq_choose_bfqq_for_injection()
4532 * Allow for only one large in-flight request in bfq_choose_bfqq_for_injection()
4533 * on non-rotational devices, for the in bfq_choose_bfqq_for_injection()
4534 * following reason. On non-rotationl drives, in bfq_choose_bfqq_for_injection()
4541 * request of the in-service queue wait for so in bfq_choose_bfqq_for_injection()
4545 * there is only one in-flight large request in bfq_choose_bfqq_for_injection()
4548 if (blk_queue_nonrot(bfqd->queue) && in bfq_choose_bfqq_for_injection()
4549 blk_rq_sectors(bfqq->next_rq) >= in bfq_choose_bfqq_for_injection()
4553 limit = in_serv_bfqq->inject_limit; in bfq_choose_bfqq_for_injection()
4555 if (bfqd->rq_in_driver < limit) { in bfq_choose_bfqq_for_injection()
4556 bfqd->rqs_injected = true; in bfq_choose_bfqq_for_injection()
4574 bfqq = bfqd->in_service_queue; in bfq_select_queue()
4578 bfq_log_bfqq(bfqd, bfqq, "select_queue: already in-service queue"); in bfq_select_queue()
4594 * happens, it is much more convenient to re-execute this loop in bfq_select_queue()
4598 next_rq = bfqq->next_rq; in bfq_select_queue()
4635 hrtimer_try_to_cancel(&bfqd->idle_slice_timer); in bfq_select_queue()
4642 * No requests pending. However, if the in-service queue is idling in bfq_select_queue()
4650 (bfqq->dispatched != 0 && bfq_better_to_idle(bfqq))) { in bfq_select_queue()
4652 bfqq->bic && bfqq->bic->bfqq[0] && in bfq_select_queue()
4653 bfq_bfqq_busy(bfqq->bic->bfqq[0]) && in bfq_select_queue()
4654 bfqq->bic->bfqq[0]->next_rq ? in bfq_select_queue()
4655 bfqq->bic->bfqq[0] : NULL; in bfq_select_queue()
4657 !hlist_empty(&bfqq->woken_list) ? in bfq_select_queue()
4658 container_of(bfqq->woken_list.first, in bfq_select_queue()
4664 * The next four mutually-exclusive ifs decide in bfq_select_queue()
4676 * non-empty waker queue for bfqq, i.e., a queue whose in bfq_select_queue()
4727 * I/O-plugging timeout fires. So one may deem the in bfq_select_queue()
4744 icq_to_bic(async_bfqq->next_rq->elv.icq) == bfqq->bic && in bfq_select_queue()
4745 bfq_serv_to_charge(async_bfqq->next_rq, async_bfqq) <= in bfq_select_queue()
4747 bfqq = bfqq->bic->bfqq[0]; in bfq_select_queue()
4748 else if (bfqq->waker_bfqq && in bfq_select_queue()
4749 bfq_bfqq_busy(bfqq->waker_bfqq) && in bfq_select_queue()
4750 bfqq->waker_bfqq->next_rq && in bfq_select_queue()
4751 bfq_serv_to_charge(bfqq->waker_bfqq->next_rq, in bfq_select_queue()
4752 bfqq->waker_bfqq) <= in bfq_select_queue()
4753 bfq_bfqq_budget_left(bfqq->waker_bfqq) in bfq_select_queue()
4755 bfqq = bfqq->waker_bfqq; in bfq_select_queue()
4758 blocked_bfqq->next_rq && in bfq_select_queue()
4759 bfq_serv_to_charge(blocked_bfqq->next_rq, in bfq_select_queue()
4765 (bfqq->wr_coeff == 1 || bfqd->wr_busy_queues > 1 || in bfq_select_queue()
4794 struct bfq_entity *entity = &bfqq->entity; in bfq_update_wr_data()
4796 if (bfqq->wr_coeff > 1) { /* queue is being weight-raised */ in bfq_update_wr_data()
4799 jiffies_to_msecs(jiffies - bfqq->last_wr_start_finish), in bfq_update_wr_data()
4800 jiffies_to_msecs(bfqq->wr_cur_max_time), in bfq_update_wr_data()
4801 bfqq->wr_coeff, in bfq_update_wr_data()
4802 bfqq->entity.weight, bfqq->entity.orig_weight); in bfq_update_wr_data()
4804 if (entity->prio_changed) in bfq_update_wr_data()
4810 * weight-raising period, then end weight raising. in bfq_update_wr_data()
4814 else if (time_is_before_jiffies(bfqq->last_wr_start_finish + in bfq_update_wr_data()
4815 bfqq->wr_cur_max_time)) { in bfq_update_wr_data()
4816 if (bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time || in bfq_update_wr_data()
4817 time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_update_wr_data()
4823 * interactive-weight-raising period in bfq_update_wr_data()
4834 bfqq->entity.prio_changed = 1; in bfq_update_wr_data()
4837 if (bfqq->wr_coeff > 1 && in bfq_update_wr_data()
4838 bfqq->wr_cur_max_time != bfqd->bfq_wr_rt_max_time && in bfq_update_wr_data()
4839 bfqq->service_from_wr > max_service_from_wr) { in bfq_update_wr_data()
4852 if ((entity->weight > entity->orig_weight) != (bfqq->wr_coeff > 1)) in bfq_update_wr_data()
4863 struct request *rq = bfqq->next_rq; in bfq_dispatch_rq_from_bfqq()
4870 if (bfqq == bfqd->in_service_queue && bfqd->wait_dispatch) { in bfq_dispatch_rq_from_bfqq()
4871 bfqd->wait_dispatch = false; in bfq_dispatch_rq_from_bfqq()
4872 bfqd->waited_rq = rq; in bfq_dispatch_rq_from_bfqq()
4875 bfq_dispatch_remove(bfqd->queue, rq); in bfq_dispatch_rq_from_bfqq()
4877 if (bfqq != bfqd->in_service_queue) in bfq_dispatch_rq_from_bfqq()
4885 * weight-raised during this service slot, even if it has in bfq_dispatch_rq_from_bfqq()
4887 * weight-raised queue. This inflates bfqq's timestamps, which in bfq_dispatch_rq_from_bfqq()
4889 * device immediately to possible other weight-raised queues. in bfq_dispatch_rq_from_bfqq()
4909 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_has_work()
4912 * Avoiding lock: a race on bfqd->busy_queues should cause at in bfq_has_work()
4915 return !list_empty_careful(&bfqd->dispatch) || in bfq_has_work()
4921 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in __bfq_dispatch_request()
4925 if (!list_empty(&bfqd->dispatch)) { in __bfq_dispatch_request()
4926 rq = list_first_entry(&bfqd->dispatch, struct request, in __bfq_dispatch_request()
4928 list_del_init(&rq->queuelist); in __bfq_dispatch_request()
4939 bfqq->dispatched++; in __bfq_dispatch_request()
4964 * being the frequency of non-elevator-private in __bfq_dispatch_request()
4988 if (bfqd->strict_guarantees && bfqd->rq_in_driver > 0) in __bfq_dispatch_request()
4999 bfqd->rq_in_driver++; in __bfq_dispatch_request()
5001 rq->rq_flags |= RQF_STARTED; in __bfq_dispatch_request()
5031 spin_lock_irq(&q->queue_lock); in bfq_update_dispatch_stats()
5048 bfqg_stats_update_io_remove(bfqg, rq->cmd_flags); in bfq_update_dispatch_stats()
5050 spin_unlock_irq(&q->queue_lock); in bfq_update_dispatch_stats()
5061 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_dispatch_request()
5066 spin_lock_irq(&bfqd->lock); in bfq_dispatch_request()
5068 in_serv_queue = bfqd->in_service_queue; in bfq_dispatch_request()
5076 spin_unlock_irq(&bfqd->lock); in bfq_dispatch_request()
5078 bfq_update_dispatch_stats(hctx->queue, rq, in_serv_queue, in bfq_dispatch_request()
5086 * in-flight on this queue also holds a reference, dropped when rq is freed.
5097 if (bfqq->bfqd) in bfq_put_queue()
5098 bfq_log_bfqq(bfqq->bfqd, bfqq, "put_queue: %p %d", in bfq_put_queue()
5099 bfqq, bfqq->ref); in bfq_put_queue()
5101 bfqq->ref--; in bfq_put_queue()
5102 if (bfqq->ref) in bfq_put_queue()
5105 if (!hlist_unhashed(&bfqq->burst_list_node)) { in bfq_put_queue()
5106 hlist_del_init(&bfqq->burst_list_node); in bfq_put_queue()
5112 * bursts, when some short-lived process (often due to in bfq_put_queue()
5127 * the current burst list--without incrementing in bfq_put_queue()
5128 * bust_size--because of a split, but the current in bfq_put_queue()
5133 if (bfqq->bic && bfqq->bfqd->burst_size > 0) in bfq_put_queue()
5134 bfqq->bfqd->burst_size--; in bfq_put_queue()
5153 if (!hlist_unhashed(&bfqq->woken_list_node)) in bfq_put_queue()
5154 hlist_del_init(&bfqq->woken_list_node); in bfq_put_queue()
5157 hlist_for_each_entry_safe(item, n, &bfqq->woken_list, in bfq_put_queue()
5159 item->waker_bfqq = NULL; in bfq_put_queue()
5160 hlist_del_init(&item->woken_list_node); in bfq_put_queue()
5163 if (bfqq->bfqd && bfqq->bfqd->last_completed_rq_bfqq == bfqq) in bfq_put_queue()
5164 bfqq->bfqd->last_completed_rq_bfqq = NULL; in bfq_put_queue()
5172 bfqq->stable_ref--; in bfq_put_stable_ref()
5185 __bfqq = bfqq->new_bfqq; in bfq_put_cooperator()
5189 next = __bfqq->new_bfqq; in bfq_put_cooperator()
5197 if (bfqq == bfqd->in_service_queue) { in bfq_exit_bfqq()
5202 bfq_log_bfqq(bfqd, bfqq, "exit_bfqq: %p, %d", bfqq, bfqq->ref); in bfq_exit_bfqq()
5215 bfqd = bfqq->bfqd; /* NULL if scheduler already exited */ in bfq_exit_icq_bfqq()
5220 spin_lock_irqsave(&bfqd->lock, flags); in bfq_exit_icq_bfqq()
5221 bfqq->bic = NULL; in bfq_exit_icq_bfqq()
5224 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_exit_icq_bfqq()
5232 if (bic->stable_merge_bfqq) { in bfq_exit_icq()
5233 struct bfq_data *bfqd = bic->stable_merge_bfqq->bfqd; in bfq_exit_icq()
5242 spin_lock_irqsave(&bfqd->lock, flags); in bfq_exit_icq()
5243 bfq_put_stable_ref(bic->stable_merge_bfqq); in bfq_exit_icq()
5244 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_exit_icq()
5246 bfq_put_stable_ref(bic->stable_merge_bfqq); in bfq_exit_icq()
5263 struct bfq_data *bfqd = bfqq->bfqd; in bfq_set_next_ioprio_data()
5268 ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); in bfq_set_next_ioprio_data()
5272 bdi_dev_name(bfqq->bfqd->queue->disk->bdi), in bfq_set_next_ioprio_data()
5279 bfqq->new_ioprio = task_nice_ioprio(tsk); in bfq_set_next_ioprio_data()
5280 bfqq->new_ioprio_class = task_nice_ioclass(tsk); in bfq_set_next_ioprio_data()
5283 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_set_next_ioprio_data()
5284 bfqq->new_ioprio_class = IOPRIO_CLASS_RT; in bfq_set_next_ioprio_data()
5287 bfqq->new_ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_set_next_ioprio_data()
5288 bfqq->new_ioprio_class = IOPRIO_CLASS_BE; in bfq_set_next_ioprio_data()
5291 bfqq->new_ioprio_class = IOPRIO_CLASS_IDLE; in bfq_set_next_ioprio_data()
5292 bfqq->new_ioprio = 7; in bfq_set_next_ioprio_data()
5296 if (bfqq->new_ioprio >= IOPRIO_NR_LEVELS) { in bfq_set_next_ioprio_data()
5298 bfqq->new_ioprio); in bfq_set_next_ioprio_data()
5299 bfqq->new_ioprio = IOPRIO_NR_LEVELS - 1; in bfq_set_next_ioprio_data()
5302 bfqq->entity.new_weight = bfq_ioprio_to_weight(bfqq->new_ioprio); in bfq_set_next_ioprio_data()
5304 bfqq->new_ioprio, bfqq->entity.new_weight); in bfq_set_next_ioprio_data()
5305 bfqq->entity.prio_changed = 1; in bfq_set_next_ioprio_data()
5317 int ioprio = bic->icq.ioc->ioprio; in bfq_check_ioprio_change()
5323 if (unlikely(!bfqd) || likely(bic->ioprio == ioprio)) in bfq_check_ioprio_change()
5326 bic->ioprio = ioprio; in bfq_check_ioprio_change()
5345 RB_CLEAR_NODE(&bfqq->entity.rb_node); in bfq_init_bfqq()
5346 INIT_LIST_HEAD(&bfqq->fifo); in bfq_init_bfqq()
5347 INIT_HLIST_NODE(&bfqq->burst_list_node); in bfq_init_bfqq()
5348 INIT_HLIST_NODE(&bfqq->woken_list_node); in bfq_init_bfqq()
5349 INIT_HLIST_HEAD(&bfqq->woken_list); in bfq_init_bfqq()
5351 bfqq->ref = 0; in bfq_init_bfqq()
5352 bfqq->bfqd = bfqd; in bfq_init_bfqq()
5372 bfqq->ttime.last_end_request = now_ns + 1; in bfq_init_bfqq()
5374 bfqq->creation_time = jiffies; in bfq_init_bfqq()
5376 bfqq->io_start_time = now_ns; in bfq_init_bfqq()
5380 bfqq->pid = pid; in bfq_init_bfqq()
5383 bfqq->max_budget = (2 * bfq_max_budget(bfqd)) / 3; in bfq_init_bfqq()
5384 bfqq->budget_timeout = bfq_smallest_from_now(); in bfq_init_bfqq()
5386 bfqq->wr_coeff = 1; in bfq_init_bfqq()
5387 bfqq->last_wr_start_finish = jiffies; in bfq_init_bfqq()
5388 bfqq->wr_start_at_switch_to_srt = bfq_smallest_from_now(); in bfq_init_bfqq()
5389 bfqq->split_time = bfq_smallest_from_now(); in bfq_init_bfqq()
5395 * to the current value of bfqq->soft_rt_next_start (see in bfq_init_bfqq()
5400 bfqq->soft_rt_next_start = jiffies; in bfq_init_bfqq()
5403 bfqq->seek_history = 1; in bfq_init_bfqq()
5412 return &bfqg->async_bfqq[0][ioprio]; in bfq_async_queue_prio()
5417 return &bfqg->async_bfqq[1][ioprio]; in bfq_async_queue_prio()
5419 return &bfqg->async_idle_bfqq; in bfq_async_queue_prio()
5436 if (new_bfqq->bic) in bfq_do_early_stable_merge()
5437 new_bfqq->bic->stably_merged = true; in bfq_do_early_stable_merge()
5438 bic->stably_merged = true; in bfq_do_early_stable_merge()
5442 * bfqq->bic must be set too, for in bfq_do_early_stable_merge()
5446 bfqq->bic = bic; in bfq_do_early_stable_merge()
5453 * Many throughput-sensitive workloads are made of several parallel
5460 * To avoid this plugging, BFQ has been using a burst-handling
5469 * throughput of the flows and task-wide I/O latency. In particular,
5485 * - very little time has elapsed since when Q1 was created
5486 * - Q2 has the same ioprio as Q1
5487 * - Q2 belongs to the same group as Q1
5492 * the total per-request processing time, the above throughput boost
5496 * burst-handling heuristics. We keep those heuristics for the moment.
5502 struct bfq_queue **source_bfqq = bfqq->entity.parent ? in bfq_do_or_sched_stable_merge()
5503 &bfqq->entity.parent->last_bfqq_created : in bfq_do_or_sched_stable_merge()
5504 &bfqd->last_bfqq_created; in bfq_do_or_sched_stable_merge()
5519 * throughput-beneficial if not merged. Currently this is in bfq_do_or_sched_stable_merge()
5528 time_before(last_bfqq_created->creation_time + in bfq_do_or_sched_stable_merge()
5530 bfqq->creation_time) || in bfq_do_or_sched_stable_merge()
5531 bfqq->entity.parent != last_bfqq_created->entity.parent || in bfq_do_or_sched_stable_merge()
5532 bfqq->ioprio != last_bfqq_created->ioprio || in bfq_do_or_sched_stable_merge()
5533 bfqq->ioprio_class != last_bfqq_created->ioprio_class) in bfq_do_or_sched_stable_merge()
5535 else if (time_after_eq(last_bfqq_created->creation_time + in bfq_do_or_sched_stable_merge()
5536 bfqd->bfq_burst_interval, in bfq_do_or_sched_stable_merge()
5537 bfqq->creation_time)) { in bfq_do_or_sched_stable_merge()
5538 if (likely(bfqd->nonrot_with_queueing)) in bfq_do_or_sched_stable_merge()
5554 last_bfqq_created->ref++; in bfq_do_or_sched_stable_merge()
5559 last_bfqq_created->stable_ref++; in bfq_do_or_sched_stable_merge()
5563 bic->stable_merge_bfqq = last_bfqq_created; in bfq_do_or_sched_stable_merge()
5576 const int ioprio = IOPRIO_PRIO_DATA(bic->ioprio); in bfq_get_queue()
5577 const int ioprio_class = IOPRIO_PRIO_CLASS(bic->ioprio); in bfq_get_queue()
5586 bfqq = &bfqd->oom_bfqq; in bfq_get_queue()
5600 bfqd->queue->node); in bfq_get_queue()
5603 bfq_init_bfqq(bfqd, bfqq, bic, current->pid, in bfq_get_queue()
5605 bfq_init_entity(&bfqq->entity, bfqg); in bfq_get_queue()
5608 bfqq = &bfqd->oom_bfqq; in bfq_get_queue()
5618 bfqq->ref++; /* in bfq_get_queue()
5621 * only if bfqq->bfqg disappears, to in bfq_get_queue()
5626 bfqq, bfqq->ref); in bfq_get_queue()
5631 bfqq->ref++; /* get a process reference to this queue */ in bfq_get_queue()
5633 if (bfqq != &bfqd->oom_bfqq && is_sync && !respawn) in bfq_get_queue()
5643 struct bfq_ttime *ttime = &bfqq->ttime; in bfq_update_io_thinktime()
5651 if (bfqq->dispatched || bfq_bfqq_busy(bfqq)) in bfq_update_io_thinktime()
5653 elapsed = ktime_get_ns() - bfqq->ttime.last_end_request; in bfq_update_io_thinktime()
5654 elapsed = min_t(u64, elapsed, 2ULL * bfqd->bfq_slice_idle); in bfq_update_io_thinktime()
5656 ttime->ttime_samples = (7*ttime->ttime_samples + 256) / 8; in bfq_update_io_thinktime()
5657 ttime->ttime_total = div_u64(7*ttime->ttime_total + 256*elapsed, 8); in bfq_update_io_thinktime()
5658 ttime->ttime_mean = div64_ul(ttime->ttime_total + 128, in bfq_update_io_thinktime()
5659 ttime->ttime_samples); in bfq_update_io_thinktime()
5666 bfqq->seek_history <<= 1; in bfq_update_io_seektime()
5667 bfqq->seek_history |= BFQ_RQ_SEEKY(bfqd, bfqq->last_request_pos, rq); in bfq_update_io_seektime()
5669 if (bfqq->wr_coeff > 1 && in bfq_update_io_seektime()
5670 bfqq->wr_cur_max_time == bfqd->bfq_wr_rt_max_time && in bfq_update_io_seektime()
5672 if (time_is_before_jiffies(bfqq->wr_start_at_switch_to_srt + in bfq_update_io_seektime()
5676 * interactive-weight-raising period in bfq_update_io_seektime()
5688 bfqq->entity.prio_changed = 1; in bfq_update_io_seektime()
5705 bfqd->bfq_slice_idle == 0) in bfq_update_has_short_ttime()
5709 if (time_is_after_eq_jiffies(bfqq->split_time + in bfq_update_has_short_ttime()
5710 bfqd->bfq_wr_min_idle_time)) in bfq_update_has_short_ttime()
5716 * think time with half the I/O-plugging timeout. in bfq_update_has_short_ttime()
5718 if (atomic_read(&bic->icq.ioc->active_ref) == 0 || in bfq_update_has_short_ttime()
5719 (bfq_sample_valid(bfqq->ttime.ttime_samples) && in bfq_update_has_short_ttime()
5720 bfqq->ttime.ttime_mean > bfqd->bfq_slice_idle>>1)) in bfq_update_has_short_ttime()
5733 * the think-time state (short|long). In particular, the limit in bfq_update_has_short_ttime()
5737 * instructions reset the inject limit if the think-time state in bfq_update_has_short_ttime()
5756 * I/O-dispatch-plugging, then bfqq remains empty, and no I/O in bfq_update_has_short_ttime()
5761 * On the opposite end, a non-zero inject limit may allow the in bfq_update_has_short_ttime()
5766 * next think-time sample for bfqq may be very low. This in in bfq_update_has_short_ttime()
5774 * of such a steady oscillation between the two think-time in bfq_update_has_short_ttime()
5800 * more frequently than once per I/O-plugging timeout, makes in bfq_update_has_short_ttime()
5814 if (state_changed && bfqq->last_serv_time_ns == 0 && in bfq_update_has_short_ttime()
5815 (time_is_before_eq_jiffies(bfqq->decrease_time_jif + in bfq_update_has_short_ttime()
5828 if (rq->cmd_flags & REQ_META) in bfq_rq_enqueued()
5829 bfqq->meta_pending++; in bfq_rq_enqueued()
5831 bfqq->last_request_pos = blk_rq_pos(rq) + blk_rq_sectors(rq); in bfq_rq_enqueued()
5833 if (bfqq == bfqd->in_service_queue && bfq_bfqq_wait_request(bfqq)) { in bfq_rq_enqueued()
5834 bool small_req = bfqq->queued[rq_is_sync(rq)] == 1 && in bfq_rq_enqueued()
5840 * - the request is small, and in bfq_rq_enqueued()
5841 * - we are idling to boost throughput, and in bfq_rq_enqueued()
5842 * - the queue is not to be expired, in bfq_rq_enqueued()
5846 * for a new request from the in-service queue, we in bfq_rq_enqueued()
5866 hrtimer_try_to_cancel(&bfqd->idle_slice_timer); in bfq_rq_enqueued()
5894 new_bfqq->allocated++; in __bfq_insert_request()
5895 bfqq->allocated--; in __bfq_insert_request()
5896 new_bfqq->ref++; in __bfq_insert_request()
5915 rq->elv.priv[1] = new_bfqq; in __bfq_insert_request()
5927 rq->fifo_time = ktime_get_ns() + bfqd->bfq_fifo_expire[rq_is_sync(rq)]; in __bfq_insert_request()
5928 list_add_tail(&rq->queuelist, &bfqq->fifo); in __bfq_insert_request()
5954 spin_lock_irq(&q->queue_lock); in bfq_update_insert_stats()
5958 spin_unlock_irq(&q->queue_lock); in bfq_update_insert_stats()
5970 struct request_queue *q = hctx->queue; in bfq_insert_request()
5971 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_insert_request()
5978 if (!cgroup_subsys_on_dfl(io_cgrp_subsys) && rq->bio) in bfq_insert_request()
5981 spin_lock_irq(&bfqd->lock); in bfq_insert_request()
5983 spin_unlock_irq(&bfqd->lock); in bfq_insert_request()
5988 spin_unlock_irq(&bfqd->lock); in bfq_insert_request()
5992 spin_lock_irq(&bfqd->lock); in bfq_insert_request()
6002 * - consider a bfq_queue, say Q1, detected as a waker of in bfq_insert_request()
6004 * - by definition of a waker, Q1 blocks the I/O of Q2, i.e., in bfq_insert_request()
6007 * - so, Q1 and Q2 are in any respect the queues of two in bfq_insert_request()
6014 * - as a consequence, if some I/O of Q1/Q2 arrives while in bfq_insert_request()
6018 * - so, when the above condition holds, the best option is to in bfq_insert_request()
6020 * - the most effective and efficient way to attain the above in bfq_insert_request()
6023 * - as an additional restriction, Q1 and Q2 must be the only in bfq_insert_request()
6031 (bfqq != bfqd->in_service_queue && in bfq_insert_request()
6032 bfqd->in_service_queue != NULL && in bfq_insert_request()
6034 (bfqq->waker_bfqq == bfqd->in_service_queue || in bfq_insert_request()
6035 bfqd->in_service_queue->waker_bfqq == bfqq)) || at_head) { in bfq_insert_request()
6037 list_add(&rq->queuelist, &bfqd->dispatch); in bfq_insert_request()
6039 list_add_tail(&rq->queuelist, &bfqd->dispatch); in bfq_insert_request()
6051 if (!q->last_merge) in bfq_insert_request()
6052 q->last_merge = rq; in bfq_insert_request()
6061 cmd_flags = rq->cmd_flags; in bfq_insert_request()
6063 spin_unlock_irq(&bfqd->lock); in bfq_insert_request()
6076 list_del_init(&rq->queuelist); in bfq_insert_requests()
6083 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_update_hw_tag()
6085 bfqd->max_rq_in_driver = max_t(int, bfqd->max_rq_in_driver, in bfq_update_hw_tag()
6086 bfqd->rq_in_driver); in bfq_update_hw_tag()
6088 if (bfqd->hw_tag == 1) in bfq_update_hw_tag()
6097 if (bfqd->rq_in_driver + bfqd->queued <= BFQ_HW_QUEUE_THRESHOLD) in bfq_update_hw_tag()
6106 bfqq->dispatched + bfqq->queued[0] + bfqq->queued[1] < in bfq_update_hw_tag()
6108 bfqd->rq_in_driver < BFQ_HW_QUEUE_THRESHOLD) in bfq_update_hw_tag()
6111 if (bfqd->hw_tag_samples++ < BFQ_HW_QUEUE_SAMPLES) in bfq_update_hw_tag()
6114 bfqd->hw_tag = bfqd->max_rq_in_driver > BFQ_HW_QUEUE_THRESHOLD; in bfq_update_hw_tag()
6115 bfqd->max_rq_in_driver = 0; in bfq_update_hw_tag()
6116 bfqd->hw_tag_samples = 0; in bfq_update_hw_tag()
6118 bfqd->nonrot_with_queueing = in bfq_update_hw_tag()
6119 blk_queue_nonrot(bfqd->queue) && bfqd->hw_tag; in bfq_update_hw_tag()
6129 bfqd->rq_in_driver--; in bfq_completed_request()
6130 bfqq->dispatched--; in bfq_completed_request()
6132 if (!bfqq->dispatched && !bfq_bfqq_busy(bfqq)) { in bfq_completed_request()
6136 * no outstanding request; used by the weight-raising in bfq_completed_request()
6139 bfqq->budget_timeout = jiffies; in bfq_completed_request()
6146 bfqq->ttime.last_end_request = now_ns; in bfq_completed_request()
6152 delta_us = div_u64(now_ns - bfqd->last_completion, NSEC_PER_USEC); in bfq_completed_request()
6163 * - close the observation interval at the last (previous) in bfq_completed_request()
6165 * - compute rate, if possible, for that observation interval in bfq_completed_request()
6166 * - reset to zero samples, which will trigger a proper in bfq_completed_request()
6167 * re-initialization of the observation interval on next in bfq_completed_request()
6171 (bfqd->last_rq_max_size<<BFQ_RATE_SHIFT)/delta_us < in bfq_completed_request()
6172 1UL<<(BFQ_RATE_SHIFT - 10)) in bfq_completed_request()
6174 bfqd->last_completion = now_ns; in bfq_completed_request()
6185 bfqd->last_completed_rq_bfqq = bfqq; in bfq_completed_request()
6187 bfqd->last_completed_rq_bfqq = NULL; in bfq_completed_request()
6198 * expires, if it still has in-flight requests. in bfq_completed_request()
6200 if (bfq_bfqq_softrt_update(bfqq) && bfqq->dispatched == 0 && in bfq_completed_request()
6201 RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_completed_request()
6202 bfqq->wr_coeff != bfqd->bfq_wr_coeff) in bfq_completed_request()
6203 bfqq->soft_rt_next_start = in bfq_completed_request()
6207 * If this is the in-service queue, check if it needs to be expired, in bfq_completed_request()
6210 if (bfqd->in_service_queue == bfqq) { in bfq_completed_request()
6212 if (bfqq->dispatched == 0) in bfq_completed_request()
6221 * Here bfqq->dispatched > 0 holds, but in bfq_completed_request()
6224 * for bfqq before bfqq->dispatched reaches 0, in bfq_completed_request()
6226 * completion event that causes bfqq->dispatch in bfq_completed_request()
6229 * (I/O-dispatch plugging). in bfq_completed_request()
6233 * when bfqq->dispatched finally reaches in bfq_completed_request()
6241 else if (RB_EMPTY_ROOT(&bfqq->sort_list) && in bfq_completed_request()
6242 (bfqq->dispatched == 0 || in bfq_completed_request()
6248 if (!bfqd->rq_in_driver) in bfq_completed_request()
6254 bfqq->allocated--; in bfq_finish_requeue_request_body()
6268 * allowed to switch to another queue---because bfqq is sync and
6269 * I/O-dispatch needs to be plugged while bfqq is temporarily
6270 * empty---then, during the service of bfqq, there will be frequent
6283 * guarantees. In this respect, the mechanism maintains a per-queue
6286 * of I/O requests in flight---i.e., already dispatched but not yet
6287 * completed---remains lower than this limit.
6292 * service, and causes bfqq to switch from empty to non-empty. The
6317 * The limit-update algorithm works as follows. On the arrival of a
6322 * (1) If there is no in-flight request. This gives a baseline for the
6330 * (2) If the limit is higher than 0 and there are in-flight
6366 u64 tot_time_ns = ktime_get_ns() - bfqd->last_empty_occupied_ns; in bfq_update_inject_limit()
6367 unsigned int old_limit = bfqq->inject_limit; in bfq_update_inject_limit()
6369 if (bfqq->last_serv_time_ns > 0 && bfqd->rqs_injected) { in bfq_update_inject_limit()
6370 u64 threshold = (bfqq->last_serv_time_ns * 3)>>1; in bfq_update_inject_limit()
6373 bfqq->inject_limit--; in bfq_update_inject_limit()
6374 bfqq->decrease_time_jif = jiffies; in bfq_update_inject_limit()
6376 old_limit <= bfqd->max_rq_in_driver) in bfq_update_inject_limit()
6377 bfqq->inject_limit++; in bfq_update_inject_limit()
6386 * NOTE: (bfqd->rq_in_driver == 1) means that there is no I/O in bfq_update_inject_limit()
6390 * bfqd->rq_in_driver is decremented in such a code path. in bfq_update_inject_limit()
6392 if ((bfqq->last_serv_time_ns == 0 && bfqd->rq_in_driver == 1) || in bfq_update_inject_limit()
6393 tot_time_ns < bfqq->last_serv_time_ns) { in bfq_update_inject_limit()
6394 if (bfqq->last_serv_time_ns == 0) { in bfq_update_inject_limit()
6399 bfqq->inject_limit = max_t(unsigned int, 1, old_limit); in bfq_update_inject_limit()
6401 bfqq->last_serv_time_ns = tot_time_ns; in bfq_update_inject_limit()
6402 } else if (!bfqd->rqs_injected && bfqd->rq_in_driver == 1) in bfq_update_inject_limit()
6412 bfqq->last_serv_time_ns = tot_time_ns; in bfq_update_inject_limit()
6416 bfqd->waited_rq = NULL; in bfq_update_inject_limit()
6417 bfqd->rqs_injected = false; in bfq_update_inject_limit()
6434 * requeued request that has not (yet) been re-inserted into in bfq_finish_requeue_request()
6437 if (!rq->elv.icq || !bfqq) in bfq_finish_requeue_request()
6440 bfqd = bfqq->bfqd; in bfq_finish_requeue_request()
6442 if (rq->rq_flags & RQF_STARTED) in bfq_finish_requeue_request()
6444 rq->start_time_ns, in bfq_finish_requeue_request()
6445 rq->io_start_time_ns, in bfq_finish_requeue_request()
6446 rq->cmd_flags); in bfq_finish_requeue_request()
6448 spin_lock_irqsave(&bfqd->lock, flags); in bfq_finish_requeue_request()
6449 if (likely(rq->rq_flags & RQF_STARTED)) { in bfq_finish_requeue_request()
6450 if (rq == bfqd->waited_rq) in bfq_finish_requeue_request()
6456 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_finish_requeue_request()
6463 * design would be to prevent blk-mq from invoking the requeue in bfq_finish_requeue_request()
6468 * request-insertion logic if rq is re-inserted into a bfq in bfq_finish_requeue_request()
6469 * internal queue, without a re-preparation. Here we assume in bfq_finish_requeue_request()
6470 * that re-insertions of requeued requests, without in bfq_finish_requeue_request()
6471 * re-preparation, can happen only for pass_through or at_head in bfq_finish_requeue_request()
6472 * requests (which are not re-inserted into bfq internal in bfq_finish_requeue_request()
6475 rq->elv.priv[0] = NULL; in bfq_finish_requeue_request()
6476 rq->elv.priv[1] = NULL; in bfq_finish_requeue_request()
6488 bfq_log_bfqq(bfqq->bfqd, bfqq, "splitting queue"); in bfq_split_bfqq()
6491 bfqq->pid = current->pid; in bfq_split_bfqq()
6501 bfq_release_process_ref(bfqq->bfqd, bfqq); in bfq_split_bfqq()
6513 if (likely(bfqq && bfqq != &bfqd->oom_bfqq)) in bfq_get_bfqq_handle_split()
6525 if ((bic->was_in_burst_list && bfqd->large_burst) || in bfq_get_bfqq_handle_split()
6526 bic->saved_in_large_burst) in bfq_get_bfqq_handle_split()
6530 if (bic->was_in_burst_list) in bfq_get_bfqq_handle_split()
6559 hlist_add_head(&bfqq->burst_list_node, in bfq_get_bfqq_handle_split()
6560 &bfqd->burst_list); in bfq_get_bfqq_handle_split()
6562 bfqq->split_time = jiffies; in bfq_get_bfqq_handle_split()
6581 rq->elv.priv[0] = rq->elv.priv[1] = NULL; in bfq_prepare_request()
6609 struct request_queue *q = rq->q; in bfq_init_rq()
6610 struct bio *bio = rq->bio; in bfq_init_rq()
6611 struct bfq_data *bfqd = q->elevator->elevator_data; in bfq_init_rq()
6618 if (unlikely(!rq->elv.icq)) in bfq_init_rq()
6628 if (rq->elv.priv[1]) in bfq_init_rq()
6629 return rq->elv.priv[1]; in bfq_init_rq()
6631 bic = icq_to_bic(rq->elv.icq); in bfq_init_rq()
6643 !bic->stably_merged) { in bfq_init_rq()
6648 bic->saved_in_large_burst = true; in bfq_init_rq()
6657 bfqq->waker_bfqq = old_bfqq->waker_bfqq; in bfq_init_rq()
6658 bfqq->tentative_waker_bfqq = NULL; in bfq_init_rq()
6662 * new_bfqq->waker_bfqq must be in bfq_init_rq()
6667 if (bfqq->waker_bfqq) in bfq_init_rq()
6668 hlist_add_head(&bfqq->woken_list_node, in bfq_init_rq()
6669 &bfqq->waker_bfqq->woken_list); in bfq_init_rq()
6675 bfqq->allocated++; in bfq_init_rq()
6676 bfqq->ref++; in bfq_init_rq()
6678 rq, bfqq, bfqq->ref); in bfq_init_rq()
6680 rq->elv.priv[0] = bic; in bfq_init_rq()
6681 rq->elv.priv[1] = bfqq; in bfq_init_rq()
6685 * by only this bic: we can then set bfqq->bic = bic. in in bfq_init_rq()
6689 if (likely(bfqq != &bfqd->oom_bfqq) && bfqq_process_refs(bfqq) == 1) { in bfq_init_rq()
6690 bfqq->bic = bic; in bfq_init_rq()
6705 * 1) A burst is actually happening (bfqd->burst_size > 0) in bfq_init_rq()
6711 * therefore in not weight-raising bfqq. See comments on in bfq_init_rq()
6723 (bfqd->burst_size > 0 || in bfq_init_rq()
6736 spin_lock_irqsave(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6745 if (bfqq != bfqd->in_service_queue) { in bfq_idle_slice_timer_body()
6746 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6759 else if (bfqq->queued[0] == 0 && bfqq->queued[1] == 0) in bfq_idle_slice_timer_body()
6763 * first request of the in-service queue arrives in bfq_idle_slice_timer_body()
6773 spin_unlock_irqrestore(&bfqd->lock, flags); in bfq_idle_slice_timer_body()
6778 * Handler of the expiration of the timer running if the in-service queue
6785 struct bfq_queue *bfqq = bfqd->in_service_queue; in bfq_idle_slice_timer()
6788 * Theoretical race here: the in-service queue can be NULL or in bfq_idle_slice_timer()
6791 * cycle that changes the in-service queue. This can hardly in bfq_idle_slice_timer()
6808 bfq_bfqq_move(bfqd, bfqq, bfqd->root_group); in __bfq_put_async_bfqq()
6811 bfqq, bfqq->ref); in __bfq_put_async_bfqq()
6829 __bfq_put_async_bfqq(bfqd, &bfqg->async_bfqq[i][j]); in bfq_put_async_queues()
6831 __bfq_put_async_bfqq(bfqd, &bfqg->async_idle_bfqq); in bfq_put_async_queues()
6844 * In-word depths if no bfq_queue is being weight-raised: in bfq_update_depths()
6847 * In next formulas, right-shift the value in bfq_update_depths()
6848 * (1U<<bt->sb.shift), instead of computing directly in bfq_update_depths()
6849 * (1U<<(bt->sb.shift - something)), to be robust against in bfq_update_depths()
6850 * any possible value of bt->sb.shift, without having to in bfq_update_depths()
6854 bfqd->word_depths[0][0] = max((1U << bt->sb.shift) >> 1, 1U); in bfq_update_depths()
6860 bfqd->word_depths[0][1] = max(((1U << bt->sb.shift) * 3) >> 2, 1U); in bfq_update_depths()
6863 * In-word depths in case some bfq_queue is being weight- in bfq_update_depths()
6866 * start-up times didn't suffer from any regression due to tag in bfq_update_depths()
6870 bfqd->word_depths[1][0] = max(((1U << bt->sb.shift) * 3) >> 4, 1U); in bfq_update_depths()
6872 bfqd->word_depths[1][1] = max(((1U << bt->sb.shift) * 6) >> 4, 1U); in bfq_update_depths()
6876 min_shallow = min(min_shallow, bfqd->word_depths[i][j]); in bfq_update_depths()
6883 struct bfq_data *bfqd = hctx->queue->elevator->elevator_data; in bfq_depth_updated()
6884 struct blk_mq_tags *tags = hctx->sched_tags; in bfq_depth_updated()
6887 min_shallow = bfq_update_depths(bfqd, tags->bitmap_tags); in bfq_depth_updated()
6888 sbitmap_queue_min_shallow_depth(tags->bitmap_tags, min_shallow); in bfq_depth_updated()
6899 struct bfq_data *bfqd = e->elevator_data; in bfq_exit_queue()
6902 hrtimer_cancel(&bfqd->idle_slice_timer); in bfq_exit_queue()
6904 spin_lock_irq(&bfqd->lock); in bfq_exit_queue()
6905 list_for_each_entry_safe(bfqq, n, &bfqd->idle_list, bfqq_list) in bfq_exit_queue()
6907 spin_unlock_irq(&bfqd->lock); in bfq_exit_queue()
6909 hrtimer_cancel(&bfqd->idle_slice_timer); in bfq_exit_queue()
6911 /* release oom-queue reference to root group */ in bfq_exit_queue()
6912 bfqg_and_blkg_put(bfqd->root_group); in bfq_exit_queue()
6915 blkcg_deactivate_policy(bfqd->queue, &blkcg_policy_bfq); in bfq_exit_queue()
6917 spin_lock_irq(&bfqd->lock); in bfq_exit_queue()
6918 bfq_put_async_queues(bfqd, bfqd->root_group); in bfq_exit_queue()
6919 kfree(bfqd->root_group); in bfq_exit_queue()
6920 spin_unlock_irq(&bfqd->lock); in bfq_exit_queue()
6932 root_group->entity.parent = NULL; in bfq_init_root_group()
6933 root_group->my_entity = NULL; in bfq_init_root_group()
6934 root_group->bfqd = bfqd; in bfq_init_root_group()
6936 root_group->rq_pos_tree = RB_ROOT; in bfq_init_root_group()
6938 root_group->sched_data.service_tree[i] = BFQ_SERVICE_TREE_INIT; in bfq_init_root_group()
6939 root_group->sched_data.bfq_class_idle_last_service = jiffies; in bfq_init_root_group()
6949 return -ENOMEM; in bfq_init_queue()
6951 bfqd = kzalloc_node(sizeof(*bfqd), GFP_KERNEL, q->node); in bfq_init_queue()
6953 kobject_put(&eq->kobj); in bfq_init_queue()
6954 return -ENOMEM; in bfq_init_queue()
6956 eq->elevator_data = bfqd; in bfq_init_queue()
6958 spin_lock_irq(&q->queue_lock); in bfq_init_queue()
6959 q->elevator = eq; in bfq_init_queue()
6960 spin_unlock_irq(&q->queue_lock); in bfq_init_queue()
6967 bfq_init_bfqq(bfqd, &bfqd->oom_bfqq, NULL, 1, 0); in bfq_init_queue()
6968 bfqd->oom_bfqq.ref++; in bfq_init_queue()
6969 bfqd->oom_bfqq.new_ioprio = BFQ_DEFAULT_QUEUE_IOPRIO; in bfq_init_queue()
6970 bfqd->oom_bfqq.new_ioprio_class = IOPRIO_CLASS_BE; in bfq_init_queue()
6971 bfqd->oom_bfqq.entity.new_weight = in bfq_init_queue()
6972 bfq_ioprio_to_weight(bfqd->oom_bfqq.new_ioprio); in bfq_init_queue()
6975 bfq_clear_bfqq_just_created(&bfqd->oom_bfqq); in bfq_init_queue()
6982 bfqd->oom_bfqq.entity.prio_changed = 1; in bfq_init_queue()
6984 bfqd->queue = q; in bfq_init_queue()
6986 INIT_LIST_HEAD(&bfqd->dispatch); in bfq_init_queue()
6988 hrtimer_init(&bfqd->idle_slice_timer, CLOCK_MONOTONIC, in bfq_init_queue()
6990 bfqd->idle_slice_timer.function = bfq_idle_slice_timer; in bfq_init_queue()
6992 bfqd->queue_weights_tree = RB_ROOT_CACHED; in bfq_init_queue()
6993 bfqd->num_groups_with_pending_reqs = 0; in bfq_init_queue()
6995 INIT_LIST_HEAD(&bfqd->active_list); in bfq_init_queue()
6996 INIT_LIST_HEAD(&bfqd->idle_list); in bfq_init_queue()
6997 INIT_HLIST_HEAD(&bfqd->burst_list); in bfq_init_queue()
6999 bfqd->hw_tag = -1; in bfq_init_queue()
7000 bfqd->nonrot_with_queueing = blk_queue_nonrot(bfqd->queue); in bfq_init_queue()
7002 bfqd->bfq_max_budget = bfq_default_max_budget; in bfq_init_queue()
7004 bfqd->bfq_fifo_expire[0] = bfq_fifo_expire[0]; in bfq_init_queue()
7005 bfqd->bfq_fifo_expire[1] = bfq_fifo_expire[1]; in bfq_init_queue()
7006 bfqd->bfq_back_max = bfq_back_max; in bfq_init_queue()
7007 bfqd->bfq_back_penalty = bfq_back_penalty; in bfq_init_queue()
7008 bfqd->bfq_slice_idle = bfq_slice_idle; in bfq_init_queue()
7009 bfqd->bfq_timeout = bfq_timeout; in bfq_init_queue()
7011 bfqd->bfq_large_burst_thresh = 8; in bfq_init_queue()
7012 bfqd->bfq_burst_interval = msecs_to_jiffies(180); in bfq_init_queue()
7014 bfqd->low_latency = true; in bfq_init_queue()
7017 * Trade-off between responsiveness and fairness. in bfq_init_queue()
7019 bfqd->bfq_wr_coeff = 30; in bfq_init_queue()
7020 bfqd->bfq_wr_rt_max_time = msecs_to_jiffies(300); in bfq_init_queue()
7021 bfqd->bfq_wr_max_time = 0; in bfq_init_queue()
7022 bfqd->bfq_wr_min_idle_time = msecs_to_jiffies(2000); in bfq_init_queue()
7023 bfqd->bfq_wr_min_inter_arr_async = msecs_to_jiffies(500); in bfq_init_queue()
7024 bfqd->bfq_wr_max_softrt_rate = 7000; /* in bfq_init_queue()
7027 * high-definition compressed in bfq_init_queue()
7030 bfqd->wr_busy_queues = 0; in bfq_init_queue()
7036 bfqd->rate_dur_prod = ref_rate[blk_queue_nonrot(bfqd->queue)] * in bfq_init_queue()
7037 ref_wr_duration[blk_queue_nonrot(bfqd->queue)]; in bfq_init_queue()
7038 bfqd->peak_rate = ref_rate[blk_queue_nonrot(bfqd->queue)] * 2 / 3; in bfq_init_queue()
7040 spin_lock_init(&bfqd->lock); in bfq_init_queue()
7045 * (bfq_create_group_hierarchy->blkcg_activate_policy-> in bfq_init_queue()
7053 * other inconsistencies, the blk-mq stack must then refrain in bfq_init_queue()
7057 bfqd->root_group = bfq_create_group_hierarchy(bfqd, q->node); in bfq_init_queue()
7058 if (!bfqd->root_group) in bfq_init_queue()
7060 bfq_init_root_group(bfqd->root_group, bfqd); in bfq_init_queue()
7061 bfq_init_entity(&bfqd->oom_bfqq.entity, bfqd->root_group); in bfq_init_queue()
7068 kobject_put(&eq->kobj); in bfq_init_queue()
7069 return -ENOMEM; in bfq_init_queue()
7081 return -ENOMEM; in bfq_slab_setup()
7104 struct bfq_data *bfqd = e->elevator_data; \
7112 SHOW_FUNCTION(bfq_fifo_expire_sync_show, bfqd->bfq_fifo_expire[1], 2);
7113 SHOW_FUNCTION(bfq_fifo_expire_async_show, bfqd->bfq_fifo_expire[0], 2);
7114 SHOW_FUNCTION(bfq_back_seek_max_show, bfqd->bfq_back_max, 0);
7115 SHOW_FUNCTION(bfq_back_seek_penalty_show, bfqd->bfq_back_penalty, 0);
7116 SHOW_FUNCTION(bfq_slice_idle_show, bfqd->bfq_slice_idle, 2);
7117 SHOW_FUNCTION(bfq_max_budget_show, bfqd->bfq_user_max_budget, 0);
7118 SHOW_FUNCTION(bfq_timeout_sync_show, bfqd->bfq_timeout, 1);
7119 SHOW_FUNCTION(bfq_strict_guarantees_show, bfqd->strict_guarantees, 0);
7120 SHOW_FUNCTION(bfq_low_latency_show, bfqd->low_latency, 0);
7126 struct bfq_data *bfqd = e->elevator_data; \
7131 USEC_SHOW_FUNCTION(bfq_slice_idle_us_show, bfqd->bfq_slice_idle);
7138 struct bfq_data *bfqd = e->elevator_data; \
7157 STORE_FUNCTION(bfq_fifo_expire_sync_store, &bfqd->bfq_fifo_expire[1], 1,
7159 STORE_FUNCTION(bfq_fifo_expire_async_store, &bfqd->bfq_fifo_expire[0], 1,
7161 STORE_FUNCTION(bfq_back_seek_max_store, &bfqd->bfq_back_max, 0, INT_MAX, 0);
7162 STORE_FUNCTION(bfq_back_seek_penalty_store, &bfqd->bfq_back_penalty, 1,
7164 STORE_FUNCTION(bfq_slice_idle_store, &bfqd->bfq_slice_idle, 0, INT_MAX, 2);
7170 struct bfq_data *bfqd = e->elevator_data; \
7184 USEC_STORE_FUNCTION(bfq_slice_idle_us_store, &bfqd->bfq_slice_idle, 0,
7191 struct bfq_data *bfqd = e->elevator_data; in bfq_max_budget_store()
7200 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); in bfq_max_budget_store()
7204 bfqd->bfq_max_budget = __data; in bfq_max_budget_store()
7207 bfqd->bfq_user_max_budget = __data; in bfq_max_budget_store()
7219 struct bfq_data *bfqd = e->elevator_data; in bfq_timeout_sync_store()
7232 bfqd->bfq_timeout = msecs_to_jiffies(__data); in bfq_timeout_sync_store()
7233 if (bfqd->bfq_user_max_budget == 0) in bfq_timeout_sync_store()
7234 bfqd->bfq_max_budget = bfq_calc_max_budget(bfqd); in bfq_timeout_sync_store()
7242 struct bfq_data *bfqd = e->elevator_data; in bfq_strict_guarantees_store()
7252 if (!bfqd->strict_guarantees && __data == 1 in bfq_strict_guarantees_store()
7253 && bfqd->bfq_slice_idle < 8 * NSEC_PER_MSEC) in bfq_strict_guarantees_store()
7254 bfqd->bfq_slice_idle = 8 * NSEC_PER_MSEC; in bfq_strict_guarantees_store()
7256 bfqd->strict_guarantees = __data; in bfq_strict_guarantees_store()
7264 struct bfq_data *bfqd = e->elevator_data; in bfq_low_latency_store()
7274 if (__data == 0 && bfqd->low_latency != 0) in bfq_low_latency_store()
7276 bfqd->low_latency = __data; in bfq_low_latency_store()
7327 MODULE_ALIAS("bfq-iosched");
7339 ret = -ENOMEM; in bfq_init()
7353 * scheduler cannot rely on a peak-rate-evaluation workload to in bfq_init()