Lines Matching refs:tg
92 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) in tg_to_blkg() argument
94 return pd_to_blkg(&tg->pd); in tg_to_blkg()
121 struct throtl_grp *tg = sq_to_tg(sq); in sq_to_td() local
123 if (tg) in sq_to_td()
124 return tg->td; in sq_to_td()
147 static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw) in tg_bps_limit() argument
149 struct blkcg_gq *blkg = tg_to_blkg(tg); in tg_bps_limit()
156 td = tg->td; in tg_bps_limit()
157 ret = tg->bps[rw][td->limit_index]; in tg_bps_limit()
161 tg->iops[rw][td->limit_index]) in tg_bps_limit()
167 if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] && in tg_bps_limit()
168 tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) { in tg_bps_limit()
171 adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td); in tg_bps_limit()
172 ret = min(tg->bps[rw][LIMIT_MAX], adjusted); in tg_bps_limit()
177 static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw) in tg_iops_limit() argument
179 struct blkcg_gq *blkg = tg_to_blkg(tg); in tg_iops_limit()
186 td = tg->td; in tg_iops_limit()
187 ret = tg->iops[rw][td->limit_index]; in tg_iops_limit()
188 if (ret == 0 && tg->td->limit_index == LIMIT_LOW) { in tg_iops_limit()
191 tg->bps[rw][td->limit_index]) in tg_iops_limit()
197 if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] && in tg_iops_limit()
198 tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) { in tg_iops_limit()
201 adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td); in tg_iops_limit()
204 ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted); in tg_iops_limit()
244 static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) in throtl_qnode_init() argument
248 qn->tg = tg; in throtl_qnode_init()
267 blkg_get(tg_to_blkg(qn->tg)); in throtl_qnode_add_bio()
319 *tg_to_put = qn->tg; in throtl_pop_queued()
321 blkg_put(tg_to_blkg(qn->tg)); in throtl_pop_queued()
342 struct throtl_grp *tg; in throtl_pd_alloc() local
345 tg = kzalloc_node(sizeof(*tg), gfp, q->node); in throtl_pd_alloc()
346 if (!tg) in throtl_pd_alloc()
349 if (blkg_rwstat_init(&tg->stat_bytes, gfp)) in throtl_pd_alloc()
352 if (blkg_rwstat_init(&tg->stat_ios, gfp)) in throtl_pd_alloc()
355 throtl_service_queue_init(&tg->service_queue); in throtl_pd_alloc()
358 throtl_qnode_init(&tg->qnode_on_self[rw], tg); in throtl_pd_alloc()
359 throtl_qnode_init(&tg->qnode_on_parent[rw], tg); in throtl_pd_alloc()
362 RB_CLEAR_NODE(&tg->rb_node); in throtl_pd_alloc()
363 tg->bps[READ][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
364 tg->bps[WRITE][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
365 tg->iops[READ][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
366 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
367 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
368 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
369 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
370 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
373 tg->latency_target = DFL_LATENCY_TARGET; in throtl_pd_alloc()
374 tg->latency_target_conf = DFL_LATENCY_TARGET; in throtl_pd_alloc()
375 tg->idletime_threshold = DFL_IDLE_THRESHOLD; in throtl_pd_alloc()
376 tg->idletime_threshold_conf = DFL_IDLE_THRESHOLD; in throtl_pd_alloc()
378 return &tg->pd; in throtl_pd_alloc()
381 blkg_rwstat_exit(&tg->stat_bytes); in throtl_pd_alloc()
383 kfree(tg); in throtl_pd_alloc()
389 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_init() local
390 struct blkcg_gq *blkg = tg_to_blkg(tg); in throtl_pd_init()
392 struct throtl_service_queue *sq = &tg->service_queue; in throtl_pd_init()
410 tg->td = td; in throtl_pd_init()
418 static void tg_update_has_rules(struct throtl_grp *tg) in tg_update_has_rules() argument
420 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); in tg_update_has_rules()
421 struct throtl_data *td = tg->td; in tg_update_has_rules()
425 tg->has_rules_iops[rw] = in tg_update_has_rules()
428 tg_iops_limit(tg, rw) != UINT_MAX); in tg_update_has_rules()
429 tg->has_rules_bps[rw] = in tg_update_has_rules()
432 (tg_bps_limit(tg, rw) != U64_MAX)); in tg_update_has_rules()
438 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_online() local
443 tg_update_has_rules(tg); in throtl_pd_online()
455 struct throtl_grp *tg = blkg_to_tg(blkg); in blk_throtl_update_limit_valid() local
457 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] || in blk_throtl_update_limit_valid()
458 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) { in blk_throtl_update_limit_valid()
476 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_offline() local
478 tg->bps[READ][LIMIT_LOW] = 0; in throtl_pd_offline()
479 tg->bps[WRITE][LIMIT_LOW] = 0; in throtl_pd_offline()
480 tg->iops[READ][LIMIT_LOW] = 0; in throtl_pd_offline()
481 tg->iops[WRITE][LIMIT_LOW] = 0; in throtl_pd_offline()
483 blk_throtl_update_limit_valid(tg->td); in throtl_pd_offline()
485 if (!tg->td->limit_valid[tg->td->limit_index]) in throtl_pd_offline()
486 throtl_upgrade_state(tg->td); in throtl_pd_offline()
491 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_free() local
493 del_timer_sync(&tg->service_queue.pending_timer); in throtl_pd_free()
494 blkg_rwstat_exit(&tg->stat_bytes); in throtl_pd_free()
495 blkg_rwstat_exit(&tg->stat_ios); in throtl_pd_free()
496 kfree(tg); in throtl_pd_free()
520 struct throtl_grp *tg; in update_min_dispatch_time() local
522 tg = throtl_rb_first(parent_sq); in update_min_dispatch_time()
523 if (!tg) in update_min_dispatch_time()
526 parent_sq->first_pending_disptime = tg->disptime; in update_min_dispatch_time()
529 static void tg_service_queue_add(struct throtl_grp *tg) in tg_service_queue_add() argument
531 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; in tg_service_queue_add()
535 unsigned long key = tg->disptime; in tg_service_queue_add()
550 rb_link_node(&tg->rb_node, parent, node); in tg_service_queue_add()
551 rb_insert_color_cached(&tg->rb_node, &parent_sq->pending_tree, in tg_service_queue_add()
555 static void throtl_enqueue_tg(struct throtl_grp *tg) in throtl_enqueue_tg() argument
557 if (!(tg->flags & THROTL_TG_PENDING)) { in throtl_enqueue_tg()
558 tg_service_queue_add(tg); in throtl_enqueue_tg()
559 tg->flags |= THROTL_TG_PENDING; in throtl_enqueue_tg()
560 tg->service_queue.parent_sq->nr_pending++; in throtl_enqueue_tg()
564 static void throtl_dequeue_tg(struct throtl_grp *tg) in throtl_dequeue_tg() argument
566 if (tg->flags & THROTL_TG_PENDING) { in throtl_dequeue_tg()
568 tg->service_queue.parent_sq; in throtl_dequeue_tg()
570 throtl_rb_erase(&tg->rb_node, parent_sq); in throtl_dequeue_tg()
572 tg->flags &= ~THROTL_TG_PENDING; in throtl_dequeue_tg()
633 static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, in throtl_start_new_slice_with_credit() argument
636 tg->bytes_disp[rw] = 0; in throtl_start_new_slice_with_credit()
637 tg->io_disp[rw] = 0; in throtl_start_new_slice_with_credit()
638 tg->carryover_bytes[rw] = 0; in throtl_start_new_slice_with_credit()
639 tg->carryover_ios[rw] = 0; in throtl_start_new_slice_with_credit()
647 if (time_after_eq(start, tg->slice_start[rw])) in throtl_start_new_slice_with_credit()
648 tg->slice_start[rw] = start; in throtl_start_new_slice_with_credit()
650 tg->slice_end[rw] = jiffies + tg->td->throtl_slice; in throtl_start_new_slice_with_credit()
651 throtl_log(&tg->service_queue, in throtl_start_new_slice_with_credit()
653 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_start_new_slice_with_credit()
654 tg->slice_end[rw], jiffies); in throtl_start_new_slice_with_credit()
657 static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw, in throtl_start_new_slice() argument
660 tg->bytes_disp[rw] = 0; in throtl_start_new_slice()
661 tg->io_disp[rw] = 0; in throtl_start_new_slice()
662 tg->slice_start[rw] = jiffies; in throtl_start_new_slice()
663 tg->slice_end[rw] = jiffies + tg->td->throtl_slice; in throtl_start_new_slice()
665 tg->carryover_bytes[rw] = 0; in throtl_start_new_slice()
666 tg->carryover_ios[rw] = 0; in throtl_start_new_slice()
669 throtl_log(&tg->service_queue, in throtl_start_new_slice()
671 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_start_new_slice()
672 tg->slice_end[rw], jiffies); in throtl_start_new_slice()
675 static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, in throtl_set_slice_end() argument
678 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); in throtl_set_slice_end()
681 static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, in throtl_extend_slice() argument
684 throtl_set_slice_end(tg, rw, jiffy_end); in throtl_extend_slice()
685 throtl_log(&tg->service_queue, in throtl_extend_slice()
687 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_extend_slice()
688 tg->slice_end[rw], jiffies); in throtl_extend_slice()
692 static bool throtl_slice_used(struct throtl_grp *tg, bool rw) in throtl_slice_used() argument
694 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) in throtl_slice_used()
701 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) in throtl_trim_slice() argument
706 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); in throtl_trim_slice()
713 if (throtl_slice_used(tg, rw)) in throtl_trim_slice()
724 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice); in throtl_trim_slice()
726 time_elapsed = jiffies - tg->slice_start[rw]; in throtl_trim_slice()
728 nr_slices = time_elapsed / tg->td->throtl_slice; in throtl_trim_slice()
732 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices; in throtl_trim_slice()
736 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) / in throtl_trim_slice()
742 if (tg->bytes_disp[rw] >= bytes_trim) in throtl_trim_slice()
743 tg->bytes_disp[rw] -= bytes_trim; in throtl_trim_slice()
745 tg->bytes_disp[rw] = 0; in throtl_trim_slice()
747 if (tg->io_disp[rw] >= io_trim) in throtl_trim_slice()
748 tg->io_disp[rw] -= io_trim; in throtl_trim_slice()
750 tg->io_disp[rw] = 0; in throtl_trim_slice()
752 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice; in throtl_trim_slice()
754 throtl_log(&tg->service_queue, in throtl_trim_slice()
757 tg->slice_start[rw], tg->slice_end[rw], jiffies); in throtl_trim_slice()
789 static void __tg_update_carryover(struct throtl_grp *tg, bool rw) in __tg_update_carryover() argument
791 unsigned long jiffy_elapsed = jiffies - tg->slice_start[rw]; in __tg_update_carryover()
792 u64 bps_limit = tg_bps_limit(tg, rw); in __tg_update_carryover()
793 u32 iops_limit = tg_iops_limit(tg, rw); in __tg_update_carryover()
802 tg->carryover_bytes[rw] += in __tg_update_carryover()
804 tg->bytes_disp[rw]; in __tg_update_carryover()
806 tg->carryover_ios[rw] += in __tg_update_carryover()
808 tg->io_disp[rw]; in __tg_update_carryover()
811 static void tg_update_carryover(struct throtl_grp *tg) in tg_update_carryover() argument
813 if (tg->service_queue.nr_queued[READ]) in tg_update_carryover()
814 __tg_update_carryover(tg, READ); in tg_update_carryover()
815 if (tg->service_queue.nr_queued[WRITE]) in tg_update_carryover()
816 __tg_update_carryover(tg, WRITE); in tg_update_carryover()
819 throtl_log(&tg->service_queue, "%s: %llu %llu %u %u\n", __func__, in tg_update_carryover()
820 tg->carryover_bytes[READ], tg->carryover_bytes[WRITE], in tg_update_carryover()
821 tg->carryover_ios[READ], tg->carryover_ios[WRITE]); in tg_update_carryover()
824 static bool tg_within_iops_limit(struct throtl_grp *tg, struct bio *bio, in tg_within_iops_limit() argument
837 jiffy_elapsed = jiffies - tg->slice_start[rw]; in tg_within_iops_limit()
840 jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg->td->throtl_slice); in tg_within_iops_limit()
842 tg->carryover_ios[rw]; in tg_within_iops_limit()
843 if (tg->io_disp[rw] + 1 <= io_allowed) { in tg_within_iops_limit()
857 static bool tg_within_bps_limit(struct throtl_grp *tg, struct bio *bio, in tg_within_bps_limit() argument
872 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; in tg_within_bps_limit()
876 jiffy_elapsed_rnd = tg->td->throtl_slice; in tg_within_bps_limit()
878 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); in tg_within_bps_limit()
880 tg->carryover_bytes[rw]; in tg_within_bps_limit()
881 if (tg->bytes_disp[rw] + bio_size <= bytes_allowed) { in tg_within_bps_limit()
888 extra_bytes = tg->bytes_disp[rw] + bio_size - bytes_allowed; in tg_within_bps_limit()
908 static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, in tg_may_dispatch() argument
913 u64 bps_limit = tg_bps_limit(tg, rw); in tg_may_dispatch()
914 u32 iops_limit = tg_iops_limit(tg, rw); in tg_may_dispatch()
922 BUG_ON(tg->service_queue.nr_queued[rw] && in tg_may_dispatch()
923 bio != throtl_peek_queued(&tg->service_queue.queued[rw])); in tg_may_dispatch()
927 tg->flags & THROTL_TG_CANCELING) { in tg_may_dispatch()
940 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw])) in tg_may_dispatch()
941 throtl_start_new_slice(tg, rw, true); in tg_may_dispatch()
943 if (time_before(tg->slice_end[rw], in tg_may_dispatch()
944 jiffies + tg->td->throtl_slice)) in tg_may_dispatch()
945 throtl_extend_slice(tg, rw, in tg_may_dispatch()
946 jiffies + tg->td->throtl_slice); in tg_may_dispatch()
949 if (tg_within_bps_limit(tg, bio, bps_limit, &bps_wait) && in tg_may_dispatch()
950 tg_within_iops_limit(tg, bio, iops_limit, &iops_wait)) { in tg_may_dispatch()
961 if (time_before(tg->slice_end[rw], jiffies + max_wait)) in tg_may_dispatch()
962 throtl_extend_slice(tg, rw, jiffies + max_wait); in tg_may_dispatch()
967 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) in throtl_charge_bio() argument
974 tg->bytes_disp[rw] += bio_size; in throtl_charge_bio()
975 tg->last_bytes_disp[rw] += bio_size; in throtl_charge_bio()
978 tg->io_disp[rw]++; in throtl_charge_bio()
979 tg->last_io_disp[rw]++; in throtl_charge_bio()
992 struct throtl_grp *tg) in throtl_add_bio_tg() argument
994 struct throtl_service_queue *sq = &tg->service_queue; in throtl_add_bio_tg()
998 qn = &tg->qnode_on_self[rw]; in throtl_add_bio_tg()
1007 tg->flags |= THROTL_TG_WAS_EMPTY; in throtl_add_bio_tg()
1012 throtl_enqueue_tg(tg); in throtl_add_bio_tg()
1015 static void tg_update_disptime(struct throtl_grp *tg) in tg_update_disptime() argument
1017 struct throtl_service_queue *sq = &tg->service_queue; in tg_update_disptime()
1023 tg_may_dispatch(tg, bio, &read_wait); in tg_update_disptime()
1027 tg_may_dispatch(tg, bio, &write_wait); in tg_update_disptime()
1033 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); in tg_update_disptime()
1034 tg->disptime = disptime; in tg_update_disptime()
1035 tg_service_queue_add(tg); in tg_update_disptime()
1038 tg->flags &= ~THROTL_TG_WAS_EMPTY; in tg_update_disptime()
1051 static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) in tg_dispatch_one_bio() argument
1053 struct throtl_service_queue *sq = &tg->service_queue; in tg_dispatch_one_bio()
1068 throtl_charge_bio(tg, bio); in tg_dispatch_one_bio()
1079 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); in tg_dispatch_one_bio()
1080 start_parent_slice_with_credit(tg, parent_tg, rw); in tg_dispatch_one_bio()
1082 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], in tg_dispatch_one_bio()
1084 BUG_ON(tg->td->nr_queued[rw] <= 0); in tg_dispatch_one_bio()
1085 tg->td->nr_queued[rw]--; in tg_dispatch_one_bio()
1088 throtl_trim_slice(tg, rw); in tg_dispatch_one_bio()
1094 static int throtl_dispatch_tg(struct throtl_grp *tg) in throtl_dispatch_tg() argument
1096 struct throtl_service_queue *sq = &tg->service_queue; in throtl_dispatch_tg()
1105 tg_may_dispatch(tg, bio, NULL)) { in throtl_dispatch_tg()
1107 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in throtl_dispatch_tg()
1115 tg_may_dispatch(tg, bio, NULL)) { in throtl_dispatch_tg()
1117 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in throtl_dispatch_tg()
1132 struct throtl_grp *tg; in throtl_select_dispatch() local
1138 tg = throtl_rb_first(parent_sq); in throtl_select_dispatch()
1139 if (!tg) in throtl_select_dispatch()
1142 if (time_before(jiffies, tg->disptime)) in throtl_select_dispatch()
1145 nr_disp += throtl_dispatch_tg(tg); in throtl_select_dispatch()
1147 sq = &tg->service_queue; in throtl_select_dispatch()
1149 tg_update_disptime(tg); in throtl_select_dispatch()
1151 throtl_dequeue_tg(tg); in throtl_select_dispatch()
1180 struct throtl_grp *tg = sq_to_tg(sq); in throtl_pending_timer_fn() local
1188 if (tg) in throtl_pending_timer_fn()
1189 q = tg->pd.blkg->q; in throtl_pending_timer_fn()
1230 if (tg->flags & THROTL_TG_WAS_EMPTY) { in throtl_pending_timer_fn()
1231 tg_update_disptime(tg); in throtl_pending_timer_fn()
1235 tg = sq_to_tg(sq); in throtl_pending_timer_fn()
1285 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_conf_u64() local
1286 u64 v = *(u64 *)((void *)tg + off); in tg_prfill_conf_u64()
1296 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_conf_uint() local
1297 unsigned int v = *(unsigned int *)((void *)tg + off); in tg_prfill_conf_uint()
1318 static void tg_conf_updated(struct throtl_grp *tg, bool global) in tg_conf_updated() argument
1320 struct throtl_service_queue *sq = &tg->service_queue; in tg_conf_updated()
1324 throtl_log(&tg->service_queue, in tg_conf_updated()
1326 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE), in tg_conf_updated()
1327 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE)); in tg_conf_updated()
1337 global ? tg->td->queue->root_blkg : tg_to_blkg(tg)) { in tg_conf_updated()
1365 throtl_start_new_slice(tg, READ, false); in tg_conf_updated()
1366 throtl_start_new_slice(tg, WRITE, false); in tg_conf_updated()
1368 if (tg->flags & THROTL_TG_PENDING) { in tg_conf_updated()
1369 tg_update_disptime(tg); in tg_conf_updated()
1379 struct throtl_grp *tg; in tg_set_conf() local
1393 tg = blkg_to_tg(ctx.blkg); in tg_set_conf()
1394 tg_update_carryover(tg); in tg_set_conf()
1397 *(u64 *)((void *)tg + of_cft(of)->private) = v; in tg_set_conf()
1399 *(unsigned int *)((void *)tg + of_cft(of)->private) = v; in tg_set_conf()
1401 tg_conf_updated(tg, false); in tg_set_conf()
1497 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_limit() local
1516 if (tg->bps_conf[READ][off] == bps_dft && in tg_prfill_limit()
1517 tg->bps_conf[WRITE][off] == bps_dft && in tg_prfill_limit()
1518 tg->iops_conf[READ][off] == iops_dft && in tg_prfill_limit()
1519 tg->iops_conf[WRITE][off] == iops_dft && in tg_prfill_limit()
1521 (tg->idletime_threshold_conf == DFL_IDLE_THRESHOLD && in tg_prfill_limit()
1522 tg->latency_target_conf == DFL_LATENCY_TARGET))) in tg_prfill_limit()
1525 if (tg->bps_conf[READ][off] != U64_MAX) in tg_prfill_limit()
1527 tg->bps_conf[READ][off]); in tg_prfill_limit()
1528 if (tg->bps_conf[WRITE][off] != U64_MAX) in tg_prfill_limit()
1530 tg->bps_conf[WRITE][off]); in tg_prfill_limit()
1531 if (tg->iops_conf[READ][off] != UINT_MAX) in tg_prfill_limit()
1533 tg->iops_conf[READ][off]); in tg_prfill_limit()
1534 if (tg->iops_conf[WRITE][off] != UINT_MAX) in tg_prfill_limit()
1536 tg->iops_conf[WRITE][off]); in tg_prfill_limit()
1538 if (tg->idletime_threshold_conf == ULONG_MAX) in tg_prfill_limit()
1542 tg->idletime_threshold_conf); in tg_prfill_limit()
1544 if (tg->latency_target_conf == ULONG_MAX) in tg_prfill_limit()
1548 " latency=%lu", tg->latency_target_conf); in tg_prfill_limit()
1569 struct throtl_grp *tg; in tg_set_limit() local
1580 tg = blkg_to_tg(ctx.blkg); in tg_set_limit()
1581 tg_update_carryover(tg); in tg_set_limit()
1583 v[0] = tg->bps_conf[READ][index]; in tg_set_limit()
1584 v[1] = tg->bps_conf[WRITE][index]; in tg_set_limit()
1585 v[2] = tg->iops_conf[READ][index]; in tg_set_limit()
1586 v[3] = tg->iops_conf[WRITE][index]; in tg_set_limit()
1588 idle_time = tg->idletime_threshold_conf; in tg_set_limit()
1589 latency_time = tg->latency_target_conf; in tg_set_limit()
1629 tg->bps_conf[READ][index] = v[0]; in tg_set_limit()
1630 tg->bps_conf[WRITE][index] = v[1]; in tg_set_limit()
1631 tg->iops_conf[READ][index] = v[2]; in tg_set_limit()
1632 tg->iops_conf[WRITE][index] = v[3]; in tg_set_limit()
1635 tg->bps[READ][index] = v[0]; in tg_set_limit()
1636 tg->bps[WRITE][index] = v[1]; in tg_set_limit()
1637 tg->iops[READ][index] = v[2]; in tg_set_limit()
1638 tg->iops[WRITE][index] = v[3]; in tg_set_limit()
1640 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW], in tg_set_limit()
1641 tg->bps_conf[READ][LIMIT_MAX]); in tg_set_limit()
1642 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW], in tg_set_limit()
1643 tg->bps_conf[WRITE][LIMIT_MAX]); in tg_set_limit()
1644 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW], in tg_set_limit()
1645 tg->iops_conf[READ][LIMIT_MAX]); in tg_set_limit()
1646 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW], in tg_set_limit()
1647 tg->iops_conf[WRITE][LIMIT_MAX]); in tg_set_limit()
1648 tg->idletime_threshold_conf = idle_time; in tg_set_limit()
1649 tg->latency_target_conf = latency_time; in tg_set_limit()
1652 if (!(tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW] || in tg_set_limit()
1653 tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) || in tg_set_limit()
1654 tg->idletime_threshold_conf == DFL_IDLE_THRESHOLD || in tg_set_limit()
1655 tg->latency_target_conf == DFL_LATENCY_TARGET) { in tg_set_limit()
1656 tg->bps[READ][LIMIT_LOW] = 0; in tg_set_limit()
1657 tg->bps[WRITE][LIMIT_LOW] = 0; in tg_set_limit()
1658 tg->iops[READ][LIMIT_LOW] = 0; in tg_set_limit()
1659 tg->iops[WRITE][LIMIT_LOW] = 0; in tg_set_limit()
1660 tg->idletime_threshold = DFL_IDLE_THRESHOLD; in tg_set_limit()
1661 tg->latency_target = DFL_LATENCY_TARGET; in tg_set_limit()
1663 tg->idletime_threshold = tg->idletime_threshold_conf; in tg_set_limit()
1664 tg->latency_target = tg->latency_target_conf; in tg_set_limit()
1667 blk_throtl_update_limit_valid(tg->td); in tg_set_limit()
1668 if (tg->td->limit_valid[LIMIT_LOW]) { in tg_set_limit()
1670 tg->td->limit_index = LIMIT_LOW; in tg_set_limit()
1672 tg->td->limit_index = LIMIT_MAX; in tg_set_limit()
1673 tg_conf_updated(tg, index == LIMIT_LOW && in tg_set_limit()
1674 tg->td->limit_valid[LIMIT_LOW]); in tg_set_limit()
1733 struct throtl_grp *tg = blkg_to_tg(blkg); in blk_throtl_cancel_bios() local
1734 struct throtl_service_queue *sq = &tg->service_queue; in blk_throtl_cancel_bios()
1745 tg_update_disptime(tg); in blk_throtl_cancel_bios()
1754 static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg) in __tg_last_low_overflow_time() argument
1758 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]) in __tg_last_low_overflow_time()
1759 rtime = tg->last_low_overflow_time[READ]; in __tg_last_low_overflow_time()
1760 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) in __tg_last_low_overflow_time()
1761 wtime = tg->last_low_overflow_time[WRITE]; in __tg_last_low_overflow_time()
1766 static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg) in tg_last_low_overflow_time() argument
1769 struct throtl_grp *parent = tg; in tg_last_low_overflow_time()
1770 unsigned long ret = __tg_last_low_overflow_time(tg); in tg_last_low_overflow_time()
1793 static bool throtl_tg_is_idle(struct throtl_grp *tg) in throtl_tg_is_idle() argument
1805 time = min_t(unsigned long, MAX_IDLE_TIME, 4 * tg->idletime_threshold); in throtl_tg_is_idle()
1806 ret = tg->latency_target == DFL_LATENCY_TARGET || in throtl_tg_is_idle()
1807 tg->idletime_threshold == DFL_IDLE_THRESHOLD || in throtl_tg_is_idle()
1808 (ktime_get_ns() >> 10) - tg->last_finish_time > time || in throtl_tg_is_idle()
1809 tg->avg_idletime > tg->idletime_threshold || in throtl_tg_is_idle()
1810 (tg->latency_target && tg->bio_cnt && in throtl_tg_is_idle()
1811 tg->bad_bio_cnt * 5 < tg->bio_cnt); in throtl_tg_is_idle()
1812 throtl_log(&tg->service_queue, in throtl_tg_is_idle()
1814 tg->avg_idletime, tg->idletime_threshold, tg->bad_bio_cnt, in throtl_tg_is_idle()
1815 tg->bio_cnt, ret, tg->td->scale); in throtl_tg_is_idle()
1819 static bool throtl_tg_can_upgrade(struct throtl_grp *tg) in throtl_tg_can_upgrade() argument
1821 struct throtl_service_queue *sq = &tg->service_queue; in throtl_tg_can_upgrade()
1828 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]; in throtl_tg_can_upgrade()
1829 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]; in throtl_tg_can_upgrade()
1840 tg_last_low_overflow_time(tg) + tg->td->throtl_slice) && in throtl_tg_can_upgrade()
1841 throtl_tg_is_idle(tg)) in throtl_tg_can_upgrade()
1846 static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg) in throtl_hierarchy_can_upgrade() argument
1849 if (throtl_tg_can_upgrade(tg)) in throtl_hierarchy_can_upgrade()
1851 tg = sq_to_tg(tg->service_queue.parent_sq); in throtl_hierarchy_can_upgrade()
1852 if (!tg || !tg_to_blkg(tg)->parent) in throtl_hierarchy_can_upgrade()
1872 struct throtl_grp *tg = blkg_to_tg(blkg); in throtl_can_upgrade() local
1874 if (tg == this_tg) in throtl_can_upgrade()
1876 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) in throtl_can_upgrade()
1878 if (!throtl_hierarchy_can_upgrade(tg)) { in throtl_can_upgrade()
1887 static void throtl_upgrade_check(struct throtl_grp *tg) in throtl_upgrade_check() argument
1891 if (tg->td->limit_index != LIMIT_LOW) in throtl_upgrade_check()
1894 if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) in throtl_upgrade_check()
1897 tg->last_check_time = now; in throtl_upgrade_check()
1900 __tg_last_low_overflow_time(tg) + tg->td->throtl_slice)) in throtl_upgrade_check()
1903 if (throtl_can_upgrade(tg->td, NULL)) in throtl_upgrade_check()
1904 throtl_upgrade_state(tg->td); in throtl_upgrade_check()
1918 struct throtl_grp *tg = blkg_to_tg(blkg); in throtl_upgrade_state() local
1919 struct throtl_service_queue *sq = &tg->service_queue; in throtl_upgrade_state()
1921 tg->disptime = jiffies - 1; in throtl_upgrade_state()
1945 static bool throtl_tg_can_downgrade(struct throtl_grp *tg) in throtl_tg_can_downgrade() argument
1947 struct throtl_data *td = tg->td; in throtl_tg_can_downgrade()
1955 time_after_eq(now, tg_last_low_overflow_time(tg) + in throtl_tg_can_downgrade()
1957 (!throtl_tg_is_idle(tg) || in throtl_tg_can_downgrade()
1958 !list_empty(&tg_to_blkg(tg)->blkcg->css.children))) in throtl_tg_can_downgrade()
1963 static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg) in throtl_hierarchy_can_downgrade() argument
1966 if (!throtl_tg_can_downgrade(tg)) in throtl_hierarchy_can_downgrade()
1968 tg = sq_to_tg(tg->service_queue.parent_sq); in throtl_hierarchy_can_downgrade()
1969 if (!tg || !tg_to_blkg(tg)->parent) in throtl_hierarchy_can_downgrade()
1975 static void throtl_downgrade_check(struct throtl_grp *tg) in throtl_downgrade_check() argument
1982 if (tg->td->limit_index != LIMIT_MAX || in throtl_downgrade_check()
1983 !tg->td->limit_valid[LIMIT_LOW]) in throtl_downgrade_check()
1985 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) in throtl_downgrade_check()
1987 if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) in throtl_downgrade_check()
1990 elapsed_time = now - tg->last_check_time; in throtl_downgrade_check()
1991 tg->last_check_time = now; in throtl_downgrade_check()
1993 if (time_before(now, tg_last_low_overflow_time(tg) + in throtl_downgrade_check()
1994 tg->td->throtl_slice)) in throtl_downgrade_check()
1997 if (tg->bps[READ][LIMIT_LOW]) { in throtl_downgrade_check()
1998 bps = tg->last_bytes_disp[READ] * HZ; in throtl_downgrade_check()
2000 if (bps >= tg->bps[READ][LIMIT_LOW]) in throtl_downgrade_check()
2001 tg->last_low_overflow_time[READ] = now; in throtl_downgrade_check()
2004 if (tg->bps[WRITE][LIMIT_LOW]) { in throtl_downgrade_check()
2005 bps = tg->last_bytes_disp[WRITE] * HZ; in throtl_downgrade_check()
2007 if (bps >= tg->bps[WRITE][LIMIT_LOW]) in throtl_downgrade_check()
2008 tg->last_low_overflow_time[WRITE] = now; in throtl_downgrade_check()
2011 if (tg->iops[READ][LIMIT_LOW]) { in throtl_downgrade_check()
2012 iops = tg->last_io_disp[READ] * HZ / elapsed_time; in throtl_downgrade_check()
2013 if (iops >= tg->iops[READ][LIMIT_LOW]) in throtl_downgrade_check()
2014 tg->last_low_overflow_time[READ] = now; in throtl_downgrade_check()
2017 if (tg->iops[WRITE][LIMIT_LOW]) { in throtl_downgrade_check()
2018 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time; in throtl_downgrade_check()
2019 if (iops >= tg->iops[WRITE][LIMIT_LOW]) in throtl_downgrade_check()
2020 tg->last_low_overflow_time[WRITE] = now; in throtl_downgrade_check()
2027 if (throtl_hierarchy_can_downgrade(tg)) in throtl_downgrade_check()
2028 throtl_downgrade_state(tg->td); in throtl_downgrade_check()
2030 tg->last_bytes_disp[READ] = 0; in throtl_downgrade_check()
2031 tg->last_bytes_disp[WRITE] = 0; in throtl_downgrade_check()
2032 tg->last_io_disp[READ] = 0; in throtl_downgrade_check()
2033 tg->last_io_disp[WRITE] = 0; in throtl_downgrade_check()
2036 static void blk_throtl_update_idletime(struct throtl_grp *tg) in blk_throtl_update_idletime() argument
2039 unsigned long last_finish_time = tg->last_finish_time; in blk_throtl_update_idletime()
2046 last_finish_time == tg->checked_last_finish_time) in blk_throtl_update_idletime()
2049 tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3; in blk_throtl_update_idletime()
2050 tg->checked_last_finish_time = last_finish_time; in blk_throtl_update_idletime()
2134 static void blk_throtl_update_idletime(struct throtl_grp *tg) in blk_throtl_update_idletime() argument
2138 static void throtl_downgrade_check(struct throtl_grp *tg) in throtl_downgrade_check() argument
2142 static void throtl_upgrade_check(struct throtl_grp *tg) in throtl_upgrade_check() argument
2162 struct throtl_grp *tg = blkg_to_tg(blkg); in __blk_throtl_bio() local
2166 struct throtl_data *td = tg->td; in __blk_throtl_bio()
2171 blkg_rwstat_add(&tg->stat_bytes, bio->bi_opf, in __blk_throtl_bio()
2173 blkg_rwstat_add(&tg->stat_ios, bio->bi_opf, 1); in __blk_throtl_bio()
2180 blk_throtl_update_idletime(tg); in __blk_throtl_bio()
2182 sq = &tg->service_queue; in __blk_throtl_bio()
2186 if (tg->last_low_overflow_time[rw] == 0) in __blk_throtl_bio()
2187 tg->last_low_overflow_time[rw] = jiffies; in __blk_throtl_bio()
2188 throtl_downgrade_check(tg); in __blk_throtl_bio()
2189 throtl_upgrade_check(tg); in __blk_throtl_bio()
2195 if (!tg_may_dispatch(tg, bio, NULL)) { in __blk_throtl_bio()
2196 tg->last_low_overflow_time[rw] = jiffies; in __blk_throtl_bio()
2197 if (throtl_can_upgrade(td, tg)) { in __blk_throtl_bio()
2205 throtl_charge_bio(tg, bio); in __blk_throtl_bio()
2218 throtl_trim_slice(tg, rw); in __blk_throtl_bio()
2225 qn = &tg->qnode_on_parent[rw]; in __blk_throtl_bio()
2227 tg = sq_to_tg(sq); in __blk_throtl_bio()
2228 if (!tg) { in __blk_throtl_bio()
2237 tg->bytes_disp[rw], bio->bi_iter.bi_size, in __blk_throtl_bio()
2238 tg_bps_limit(tg, rw), in __blk_throtl_bio()
2239 tg->io_disp[rw], tg_iops_limit(tg, rw), in __blk_throtl_bio()
2242 tg->last_low_overflow_time[rw] = jiffies; in __blk_throtl_bio()
2245 throtl_add_bio_tg(bio, qn, tg); in __blk_throtl_bio()
2254 if (tg->flags & THROTL_TG_WAS_EMPTY) { in __blk_throtl_bio()
2255 tg_update_disptime(tg); in __blk_throtl_bio()
2256 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); in __blk_throtl_bio()
2303 struct throtl_grp *tg; in blk_throtl_bio_endio() local
2313 tg = blkg_to_tg(blkg); in blk_throtl_bio_endio()
2314 if (!tg->td->limit_valid[LIMIT_LOW]) in blk_throtl_bio_endio()
2318 tg->last_finish_time = finish_time_ns >> 10; in blk_throtl_bio_endio()
2328 throtl_track_latency(tg->td, bio_issue_size(&bio->bi_issue), in blk_throtl_bio_endio()
2331 if (tg->latency_target && lat >= tg->td->filtered_latency) { in blk_throtl_bio_endio()
2336 threshold = tg->td->avg_buckets[rw][bucket].latency + in blk_throtl_bio_endio()
2337 tg->latency_target; in blk_throtl_bio_endio()
2339 tg->bad_bio_cnt++; in blk_throtl_bio_endio()
2344 tg->bio_cnt++; in blk_throtl_bio_endio()
2347 if (time_after(jiffies, tg->bio_cnt_reset_time) || tg->bio_cnt > 1024) { in blk_throtl_bio_endio()
2348 tg->bio_cnt_reset_time = tg->td->throtl_slice + jiffies; in blk_throtl_bio_endio()
2349 tg->bio_cnt /= 2; in blk_throtl_bio_endio()
2350 tg->bad_bio_cnt /= 2; in blk_throtl_bio_endio()