Lines Matching refs:tg

70 	struct throtl_grp	*tg;		/* tg this qnode belongs to */  member
237 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg) in tg_to_blkg() argument
239 return pd_to_blkg(&tg->pd); in tg_to_blkg()
266 struct throtl_grp *tg = sq_to_tg(sq); in sq_to_td() local
268 if (tg) in sq_to_td()
269 return tg->td; in sq_to_td()
292 static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw) in tg_bps_limit() argument
294 struct blkcg_gq *blkg = tg_to_blkg(tg); in tg_bps_limit()
301 td = tg->td; in tg_bps_limit()
302 ret = tg->bps[rw][td->limit_index]; in tg_bps_limit()
306 tg->iops[rw][td->limit_index]) in tg_bps_limit()
312 if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] && in tg_bps_limit()
313 tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) { in tg_bps_limit()
316 adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td); in tg_bps_limit()
317 ret = min(tg->bps[rw][LIMIT_MAX], adjusted); in tg_bps_limit()
322 static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw) in tg_iops_limit() argument
324 struct blkcg_gq *blkg = tg_to_blkg(tg); in tg_iops_limit()
331 td = tg->td; in tg_iops_limit()
332 ret = tg->iops[rw][td->limit_index]; in tg_iops_limit()
333 if (ret == 0 && tg->td->limit_index == LIMIT_LOW) { in tg_iops_limit()
336 tg->bps[rw][td->limit_index]) in tg_iops_limit()
342 if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] && in tg_iops_limit()
343 tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) { in tg_iops_limit()
346 adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td); in tg_iops_limit()
349 ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted); in tg_iops_limit()
389 static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg) in throtl_qnode_init() argument
393 qn->tg = tg; in throtl_qnode_init()
412 blkg_get(tg_to_blkg(qn->tg)); in throtl_qnode_add_bio()
462 *tg_to_put = qn->tg; in throtl_pop_queued()
464 blkg_put(tg_to_blkg(qn->tg)); in throtl_pop_queued()
485 struct throtl_grp *tg; in throtl_pd_alloc() local
488 tg = kzalloc_node(sizeof(*tg), gfp, q->node); in throtl_pd_alloc()
489 if (!tg) in throtl_pd_alloc()
492 throtl_service_queue_init(&tg->service_queue); in throtl_pd_alloc()
495 throtl_qnode_init(&tg->qnode_on_self[rw], tg); in throtl_pd_alloc()
496 throtl_qnode_init(&tg->qnode_on_parent[rw], tg); in throtl_pd_alloc()
499 RB_CLEAR_NODE(&tg->rb_node); in throtl_pd_alloc()
500 tg->bps[READ][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
501 tg->bps[WRITE][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
502 tg->iops[READ][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
503 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
504 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
505 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX; in throtl_pd_alloc()
506 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
507 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX; in throtl_pd_alloc()
510 tg->latency_target = DFL_LATENCY_TARGET; in throtl_pd_alloc()
511 tg->latency_target_conf = DFL_LATENCY_TARGET; in throtl_pd_alloc()
512 tg->idletime_threshold = DFL_IDLE_THRESHOLD; in throtl_pd_alloc()
513 tg->idletime_threshold_conf = DFL_IDLE_THRESHOLD; in throtl_pd_alloc()
515 return &tg->pd; in throtl_pd_alloc()
520 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_init() local
521 struct blkcg_gq *blkg = tg_to_blkg(tg); in throtl_pd_init()
523 struct throtl_service_queue *sq = &tg->service_queue; in throtl_pd_init()
541 tg->td = td; in throtl_pd_init()
549 static void tg_update_has_rules(struct throtl_grp *tg) in tg_update_has_rules() argument
551 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq); in tg_update_has_rules()
552 struct throtl_data *td = tg->td; in tg_update_has_rules()
556 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) || in tg_update_has_rules()
558 (tg_bps_limit(tg, rw) != U64_MAX || in tg_update_has_rules()
559 tg_iops_limit(tg, rw) != UINT_MAX)); in tg_update_has_rules()
564 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_online() local
569 tg_update_has_rules(tg); in throtl_pd_online()
580 struct throtl_grp *tg = blkg_to_tg(blkg); in blk_throtl_update_limit_valid() local
582 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] || in blk_throtl_update_limit_valid()
583 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) { in blk_throtl_update_limit_valid()
596 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_offline() local
598 tg->bps[READ][LIMIT_LOW] = 0; in throtl_pd_offline()
599 tg->bps[WRITE][LIMIT_LOW] = 0; in throtl_pd_offline()
600 tg->iops[READ][LIMIT_LOW] = 0; in throtl_pd_offline()
601 tg->iops[WRITE][LIMIT_LOW] = 0; in throtl_pd_offline()
603 blk_throtl_update_limit_valid(tg->td); in throtl_pd_offline()
605 if (!tg->td->limit_valid[tg->td->limit_index]) in throtl_pd_offline()
606 throtl_upgrade_state(tg->td); in throtl_pd_offline()
611 struct throtl_grp *tg = pd_to_tg(pd); in throtl_pd_free() local
613 del_timer_sync(&tg->service_queue.pending_timer); in throtl_pd_free()
614 kfree(tg); in throtl_pd_free()
642 struct throtl_grp *tg; in update_min_dispatch_time() local
644 tg = throtl_rb_first(parent_sq); in update_min_dispatch_time()
645 if (!tg) in update_min_dispatch_time()
648 parent_sq->first_pending_disptime = tg->disptime; in update_min_dispatch_time()
651 static void tg_service_queue_add(struct throtl_grp *tg) in tg_service_queue_add() argument
653 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq; in tg_service_queue_add()
657 unsigned long key = tg->disptime; in tg_service_queue_add()
672 rb_link_node(&tg->rb_node, parent, node); in tg_service_queue_add()
673 rb_insert_color_cached(&tg->rb_node, &parent_sq->pending_tree, in tg_service_queue_add()
677 static void __throtl_enqueue_tg(struct throtl_grp *tg) in __throtl_enqueue_tg() argument
679 tg_service_queue_add(tg); in __throtl_enqueue_tg()
680 tg->flags |= THROTL_TG_PENDING; in __throtl_enqueue_tg()
681 tg->service_queue.parent_sq->nr_pending++; in __throtl_enqueue_tg()
684 static void throtl_enqueue_tg(struct throtl_grp *tg) in throtl_enqueue_tg() argument
686 if (!(tg->flags & THROTL_TG_PENDING)) in throtl_enqueue_tg()
687 __throtl_enqueue_tg(tg); in throtl_enqueue_tg()
690 static void __throtl_dequeue_tg(struct throtl_grp *tg) in __throtl_dequeue_tg() argument
692 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq); in __throtl_dequeue_tg()
693 tg->flags &= ~THROTL_TG_PENDING; in __throtl_dequeue_tg()
696 static void throtl_dequeue_tg(struct throtl_grp *tg) in throtl_dequeue_tg() argument
698 if (tg->flags & THROTL_TG_PENDING) in throtl_dequeue_tg()
699 __throtl_dequeue_tg(tg); in throtl_dequeue_tg()
759 static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg, in throtl_start_new_slice_with_credit() argument
762 tg->bytes_disp[rw] = 0; in throtl_start_new_slice_with_credit()
763 tg->io_disp[rw] = 0; in throtl_start_new_slice_with_credit()
771 if (time_after_eq(start, tg->slice_start[rw])) in throtl_start_new_slice_with_credit()
772 tg->slice_start[rw] = start; in throtl_start_new_slice_with_credit()
774 tg->slice_end[rw] = jiffies + tg->td->throtl_slice; in throtl_start_new_slice_with_credit()
775 throtl_log(&tg->service_queue, in throtl_start_new_slice_with_credit()
777 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_start_new_slice_with_credit()
778 tg->slice_end[rw], jiffies); in throtl_start_new_slice_with_credit()
781 static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw) in throtl_start_new_slice() argument
783 tg->bytes_disp[rw] = 0; in throtl_start_new_slice()
784 tg->io_disp[rw] = 0; in throtl_start_new_slice()
785 tg->slice_start[rw] = jiffies; in throtl_start_new_slice()
786 tg->slice_end[rw] = jiffies + tg->td->throtl_slice; in throtl_start_new_slice()
787 throtl_log(&tg->service_queue, in throtl_start_new_slice()
789 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_start_new_slice()
790 tg->slice_end[rw], jiffies); in throtl_start_new_slice()
793 static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw, in throtl_set_slice_end() argument
796 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); in throtl_set_slice_end()
799 static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw, in throtl_extend_slice() argument
802 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice); in throtl_extend_slice()
803 throtl_log(&tg->service_queue, in throtl_extend_slice()
805 rw == READ ? 'R' : 'W', tg->slice_start[rw], in throtl_extend_slice()
806 tg->slice_end[rw], jiffies); in throtl_extend_slice()
810 static bool throtl_slice_used(struct throtl_grp *tg, bool rw) in throtl_slice_used() argument
812 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw])) in throtl_slice_used()
819 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw) in throtl_trim_slice() argument
824 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw])); in throtl_trim_slice()
831 if (throtl_slice_used(tg, rw)) in throtl_trim_slice()
842 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice); in throtl_trim_slice()
844 time_elapsed = jiffies - tg->slice_start[rw]; in throtl_trim_slice()
846 nr_slices = time_elapsed / tg->td->throtl_slice; in throtl_trim_slice()
850 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices; in throtl_trim_slice()
854 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) / in throtl_trim_slice()
860 if (tg->bytes_disp[rw] >= bytes_trim) in throtl_trim_slice()
861 tg->bytes_disp[rw] -= bytes_trim; in throtl_trim_slice()
863 tg->bytes_disp[rw] = 0; in throtl_trim_slice()
865 if (tg->io_disp[rw] >= io_trim) in throtl_trim_slice()
866 tg->io_disp[rw] -= io_trim; in throtl_trim_slice()
868 tg->io_disp[rw] = 0; in throtl_trim_slice()
870 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice; in throtl_trim_slice()
872 throtl_log(&tg->service_queue, in throtl_trim_slice()
875 tg->slice_start[rw], tg->slice_end[rw], jiffies); in throtl_trim_slice()
878 static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio, in tg_with_in_iops_limit() argument
886 jiffy_elapsed = jiffies - tg->slice_start[rw]; in tg_with_in_iops_limit()
889 jiffy_elapsed_rnd = roundup(jiffy_elapsed + 1, tg->td->throtl_slice); in tg_with_in_iops_limit()
898 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd; in tg_with_in_iops_limit()
906 if (tg->io_disp[rw] + 1 <= io_allowed) { in tg_with_in_iops_limit()
920 static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio, in tg_with_in_bps_limit() argument
928 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw]; in tg_with_in_bps_limit()
932 jiffy_elapsed_rnd = tg->td->throtl_slice; in tg_with_in_bps_limit()
934 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice); in tg_with_in_bps_limit()
936 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd; in tg_with_in_bps_limit()
940 if (tg->bytes_disp[rw] + bio_size <= bytes_allowed) { in tg_with_in_bps_limit()
947 extra_bytes = tg->bytes_disp[rw] + bio_size - bytes_allowed; in tg_with_in_bps_limit()
948 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw)); in tg_with_in_bps_limit()
967 static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio, in tg_may_dispatch() argument
979 BUG_ON(tg->service_queue.nr_queued[rw] && in tg_may_dispatch()
980 bio != throtl_peek_queued(&tg->service_queue.queued[rw])); in tg_may_dispatch()
983 if (tg_bps_limit(tg, rw) == U64_MAX && in tg_may_dispatch()
984 tg_iops_limit(tg, rw) == UINT_MAX) { in tg_may_dispatch()
997 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw])) in tg_may_dispatch()
998 throtl_start_new_slice(tg, rw); in tg_may_dispatch()
1000 if (time_before(tg->slice_end[rw], in tg_may_dispatch()
1001 jiffies + tg->td->throtl_slice)) in tg_may_dispatch()
1002 throtl_extend_slice(tg, rw, in tg_may_dispatch()
1003 jiffies + tg->td->throtl_slice); in tg_may_dispatch()
1006 if (tg_with_in_bps_limit(tg, bio, &bps_wait) && in tg_may_dispatch()
1007 tg_with_in_iops_limit(tg, bio, &iops_wait)) { in tg_may_dispatch()
1018 if (time_before(tg->slice_end[rw], jiffies + max_wait)) in tg_may_dispatch()
1019 throtl_extend_slice(tg, rw, jiffies + max_wait); in tg_may_dispatch()
1024 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio) in throtl_charge_bio() argument
1030 tg->bytes_disp[rw] += bio_size; in throtl_charge_bio()
1031 tg->io_disp[rw]++; in throtl_charge_bio()
1032 tg->last_bytes_disp[rw] += bio_size; in throtl_charge_bio()
1033 tg->last_io_disp[rw]++; in throtl_charge_bio()
1055 struct throtl_grp *tg) in throtl_add_bio_tg() argument
1057 struct throtl_service_queue *sq = &tg->service_queue; in throtl_add_bio_tg()
1061 qn = &tg->qnode_on_self[rw]; in throtl_add_bio_tg()
1070 tg->flags |= THROTL_TG_WAS_EMPTY; in throtl_add_bio_tg()
1075 throtl_enqueue_tg(tg); in throtl_add_bio_tg()
1078 static void tg_update_disptime(struct throtl_grp *tg) in tg_update_disptime() argument
1080 struct throtl_service_queue *sq = &tg->service_queue; in tg_update_disptime()
1086 tg_may_dispatch(tg, bio, &read_wait); in tg_update_disptime()
1090 tg_may_dispatch(tg, bio, &write_wait); in tg_update_disptime()
1096 throtl_dequeue_tg(tg); in tg_update_disptime()
1097 tg->disptime = disptime; in tg_update_disptime()
1098 throtl_enqueue_tg(tg); in tg_update_disptime()
1101 tg->flags &= ~THROTL_TG_WAS_EMPTY; in tg_update_disptime()
1114 static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw) in tg_dispatch_one_bio() argument
1116 struct throtl_service_queue *sq = &tg->service_queue; in tg_dispatch_one_bio()
1131 throtl_charge_bio(tg, bio); in tg_dispatch_one_bio()
1141 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg); in tg_dispatch_one_bio()
1142 start_parent_slice_with_credit(tg, parent_tg, rw); in tg_dispatch_one_bio()
1144 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw], in tg_dispatch_one_bio()
1146 BUG_ON(tg->td->nr_queued[rw] <= 0); in tg_dispatch_one_bio()
1147 tg->td->nr_queued[rw]--; in tg_dispatch_one_bio()
1150 throtl_trim_slice(tg, rw); in tg_dispatch_one_bio()
1156 static int throtl_dispatch_tg(struct throtl_grp *tg) in throtl_dispatch_tg() argument
1158 struct throtl_service_queue *sq = &tg->service_queue; in throtl_dispatch_tg()
1167 tg_may_dispatch(tg, bio, NULL)) { in throtl_dispatch_tg()
1169 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in throtl_dispatch_tg()
1177 tg_may_dispatch(tg, bio, NULL)) { in throtl_dispatch_tg()
1179 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in throtl_dispatch_tg()
1194 struct throtl_grp *tg = throtl_rb_first(parent_sq); in throtl_select_dispatch() local
1197 if (!tg) in throtl_select_dispatch()
1200 if (time_before(jiffies, tg->disptime)) in throtl_select_dispatch()
1203 throtl_dequeue_tg(tg); in throtl_select_dispatch()
1205 nr_disp += throtl_dispatch_tg(tg); in throtl_select_dispatch()
1207 sq = &tg->service_queue; in throtl_select_dispatch()
1209 tg_update_disptime(tg); in throtl_select_dispatch()
1238 struct throtl_grp *tg = sq_to_tg(sq); in throtl_pending_timer_fn() local
1278 if (tg->flags & THROTL_TG_WAS_EMPTY) { in throtl_pending_timer_fn()
1279 tg_update_disptime(tg); in throtl_pending_timer_fn()
1283 tg = sq_to_tg(sq); in throtl_pending_timer_fn()
1333 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_conf_u64() local
1334 u64 v = *(u64 *)((void *)tg + off); in tg_prfill_conf_u64()
1344 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_conf_uint() local
1345 unsigned int v = *(unsigned int *)((void *)tg + off); in tg_prfill_conf_uint()
1366 static void tg_conf_updated(struct throtl_grp *tg, bool global) in tg_conf_updated() argument
1368 struct throtl_service_queue *sq = &tg->service_queue; in tg_conf_updated()
1372 throtl_log(&tg->service_queue, in tg_conf_updated()
1374 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE), in tg_conf_updated()
1375 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE)); in tg_conf_updated()
1385 global ? tg->td->queue->root_blkg : tg_to_blkg(tg)) { in tg_conf_updated()
1413 throtl_start_new_slice(tg, 0); in tg_conf_updated()
1414 throtl_start_new_slice(tg, 1); in tg_conf_updated()
1416 if (tg->flags & THROTL_TG_PENDING) { in tg_conf_updated()
1417 tg_update_disptime(tg); in tg_conf_updated()
1427 struct throtl_grp *tg; in tg_set_conf() local
1441 tg = blkg_to_tg(ctx.blkg); in tg_set_conf()
1444 *(u64 *)((void *)tg + of_cft(of)->private) = v; in tg_set_conf()
1446 *(unsigned int *)((void *)tg + of_cft(of)->private) = v; in tg_set_conf()
1448 tg_conf_updated(tg, false); in tg_set_conf()
1518 struct throtl_grp *tg = pd_to_tg(pd); in tg_prfill_limit() local
1537 if (tg->bps_conf[READ][off] == bps_dft && in tg_prfill_limit()
1538 tg->bps_conf[WRITE][off] == bps_dft && in tg_prfill_limit()
1539 tg->iops_conf[READ][off] == iops_dft && in tg_prfill_limit()
1540 tg->iops_conf[WRITE][off] == iops_dft && in tg_prfill_limit()
1542 (tg->idletime_threshold_conf == DFL_IDLE_THRESHOLD && in tg_prfill_limit()
1543 tg->latency_target_conf == DFL_LATENCY_TARGET))) in tg_prfill_limit()
1546 if (tg->bps_conf[READ][off] != U64_MAX) in tg_prfill_limit()
1548 tg->bps_conf[READ][off]); in tg_prfill_limit()
1549 if (tg->bps_conf[WRITE][off] != U64_MAX) in tg_prfill_limit()
1551 tg->bps_conf[WRITE][off]); in tg_prfill_limit()
1552 if (tg->iops_conf[READ][off] != UINT_MAX) in tg_prfill_limit()
1554 tg->iops_conf[READ][off]); in tg_prfill_limit()
1555 if (tg->iops_conf[WRITE][off] != UINT_MAX) in tg_prfill_limit()
1557 tg->iops_conf[WRITE][off]); in tg_prfill_limit()
1559 if (tg->idletime_threshold_conf == ULONG_MAX) in tg_prfill_limit()
1563 tg->idletime_threshold_conf); in tg_prfill_limit()
1565 if (tg->latency_target_conf == ULONG_MAX) in tg_prfill_limit()
1569 " latency=%lu", tg->latency_target_conf); in tg_prfill_limit()
1590 struct throtl_grp *tg; in tg_set_limit() local
1601 tg = blkg_to_tg(ctx.blkg); in tg_set_limit()
1603 v[0] = tg->bps_conf[READ][index]; in tg_set_limit()
1604 v[1] = tg->bps_conf[WRITE][index]; in tg_set_limit()
1605 v[2] = tg->iops_conf[READ][index]; in tg_set_limit()
1606 v[3] = tg->iops_conf[WRITE][index]; in tg_set_limit()
1608 idle_time = tg->idletime_threshold_conf; in tg_set_limit()
1609 latency_time = tg->latency_target_conf; in tg_set_limit()
1649 tg->bps_conf[READ][index] = v[0]; in tg_set_limit()
1650 tg->bps_conf[WRITE][index] = v[1]; in tg_set_limit()
1651 tg->iops_conf[READ][index] = v[2]; in tg_set_limit()
1652 tg->iops_conf[WRITE][index] = v[3]; in tg_set_limit()
1655 tg->bps[READ][index] = v[0]; in tg_set_limit()
1656 tg->bps[WRITE][index] = v[1]; in tg_set_limit()
1657 tg->iops[READ][index] = v[2]; in tg_set_limit()
1658 tg->iops[WRITE][index] = v[3]; in tg_set_limit()
1660 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW], in tg_set_limit()
1661 tg->bps_conf[READ][LIMIT_MAX]); in tg_set_limit()
1662 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW], in tg_set_limit()
1663 tg->bps_conf[WRITE][LIMIT_MAX]); in tg_set_limit()
1664 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW], in tg_set_limit()
1665 tg->iops_conf[READ][LIMIT_MAX]); in tg_set_limit()
1666 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW], in tg_set_limit()
1667 tg->iops_conf[WRITE][LIMIT_MAX]); in tg_set_limit()
1668 tg->idletime_threshold_conf = idle_time; in tg_set_limit()
1669 tg->latency_target_conf = latency_time; in tg_set_limit()
1672 if (!(tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW] || in tg_set_limit()
1673 tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) || in tg_set_limit()
1674 tg->idletime_threshold_conf == DFL_IDLE_THRESHOLD || in tg_set_limit()
1675 tg->latency_target_conf == DFL_LATENCY_TARGET) { in tg_set_limit()
1676 tg->bps[READ][LIMIT_LOW] = 0; in tg_set_limit()
1677 tg->bps[WRITE][LIMIT_LOW] = 0; in tg_set_limit()
1678 tg->iops[READ][LIMIT_LOW] = 0; in tg_set_limit()
1679 tg->iops[WRITE][LIMIT_LOW] = 0; in tg_set_limit()
1680 tg->idletime_threshold = DFL_IDLE_THRESHOLD; in tg_set_limit()
1681 tg->latency_target = DFL_LATENCY_TARGET; in tg_set_limit()
1683 tg->idletime_threshold = tg->idletime_threshold_conf; in tg_set_limit()
1684 tg->latency_target = tg->latency_target_conf; in tg_set_limit()
1687 blk_throtl_update_limit_valid(tg->td); in tg_set_limit()
1688 if (tg->td->limit_valid[LIMIT_LOW]) { in tg_set_limit()
1690 tg->td->limit_index = LIMIT_LOW; in tg_set_limit()
1692 tg->td->limit_index = LIMIT_MAX; in tg_set_limit()
1693 tg_conf_updated(tg, index == LIMIT_LOW && in tg_set_limit()
1694 tg->td->limit_valid[LIMIT_LOW]); in tg_set_limit()
1739 static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg) in __tg_last_low_overflow_time() argument
1743 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]) in __tg_last_low_overflow_time()
1744 rtime = tg->last_low_overflow_time[READ]; in __tg_last_low_overflow_time()
1745 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]) in __tg_last_low_overflow_time()
1746 wtime = tg->last_low_overflow_time[WRITE]; in __tg_last_low_overflow_time()
1751 static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg) in tg_last_low_overflow_time() argument
1754 struct throtl_grp *parent = tg; in tg_last_low_overflow_time()
1755 unsigned long ret = __tg_last_low_overflow_time(tg); in tg_last_low_overflow_time()
1778 static bool throtl_tg_is_idle(struct throtl_grp *tg) in throtl_tg_is_idle() argument
1790 time = min_t(unsigned long, MAX_IDLE_TIME, 4 * tg->idletime_threshold); in throtl_tg_is_idle()
1791 ret = tg->latency_target == DFL_LATENCY_TARGET || in throtl_tg_is_idle()
1792 tg->idletime_threshold == DFL_IDLE_THRESHOLD || in throtl_tg_is_idle()
1793 (ktime_get_ns() >> 10) - tg->last_finish_time > time || in throtl_tg_is_idle()
1794 tg->avg_idletime > tg->idletime_threshold || in throtl_tg_is_idle()
1795 (tg->latency_target && tg->bio_cnt && in throtl_tg_is_idle()
1796 tg->bad_bio_cnt * 5 < tg->bio_cnt); in throtl_tg_is_idle()
1797 throtl_log(&tg->service_queue, in throtl_tg_is_idle()
1799 tg->avg_idletime, tg->idletime_threshold, tg->bad_bio_cnt, in throtl_tg_is_idle()
1800 tg->bio_cnt, ret, tg->td->scale); in throtl_tg_is_idle()
1804 static bool throtl_tg_can_upgrade(struct throtl_grp *tg) in throtl_tg_can_upgrade() argument
1806 struct throtl_service_queue *sq = &tg->service_queue; in throtl_tg_can_upgrade()
1813 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW]; in throtl_tg_can_upgrade()
1814 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW]; in throtl_tg_can_upgrade()
1825 tg_last_low_overflow_time(tg) + tg->td->throtl_slice) && in throtl_tg_can_upgrade()
1826 throtl_tg_is_idle(tg)) in throtl_tg_can_upgrade()
1831 static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg) in throtl_hierarchy_can_upgrade() argument
1834 if (throtl_tg_can_upgrade(tg)) in throtl_hierarchy_can_upgrade()
1836 tg = sq_to_tg(tg->service_queue.parent_sq); in throtl_hierarchy_can_upgrade()
1837 if (!tg || !tg_to_blkg(tg)->parent) in throtl_hierarchy_can_upgrade()
1857 struct throtl_grp *tg = blkg_to_tg(blkg); in throtl_can_upgrade() local
1859 if (tg == this_tg) in throtl_can_upgrade()
1861 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) in throtl_can_upgrade()
1863 if (!throtl_hierarchy_can_upgrade(tg)) { in throtl_can_upgrade()
1872 static void throtl_upgrade_check(struct throtl_grp *tg) in throtl_upgrade_check() argument
1876 if (tg->td->limit_index != LIMIT_LOW) in throtl_upgrade_check()
1879 if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) in throtl_upgrade_check()
1882 tg->last_check_time = now; in throtl_upgrade_check()
1885 __tg_last_low_overflow_time(tg) + tg->td->throtl_slice)) in throtl_upgrade_check()
1888 if (throtl_can_upgrade(tg->td, NULL)) in throtl_upgrade_check()
1889 throtl_upgrade_state(tg->td); in throtl_upgrade_check()
1903 struct throtl_grp *tg = blkg_to_tg(blkg); in throtl_upgrade_state() local
1904 struct throtl_service_queue *sq = &tg->service_queue; in throtl_upgrade_state()
1906 tg->disptime = jiffies - 1; in throtl_upgrade_state()
1930 static bool throtl_tg_can_downgrade(struct throtl_grp *tg) in throtl_tg_can_downgrade() argument
1932 struct throtl_data *td = tg->td; in throtl_tg_can_downgrade()
1940 time_after_eq(now, tg_last_low_overflow_time(tg) + in throtl_tg_can_downgrade()
1942 (!throtl_tg_is_idle(tg) || in throtl_tg_can_downgrade()
1943 !list_empty(&tg_to_blkg(tg)->blkcg->css.children))) in throtl_tg_can_downgrade()
1948 static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg) in throtl_hierarchy_can_downgrade() argument
1951 if (!throtl_tg_can_downgrade(tg)) in throtl_hierarchy_can_downgrade()
1953 tg = sq_to_tg(tg->service_queue.parent_sq); in throtl_hierarchy_can_downgrade()
1954 if (!tg || !tg_to_blkg(tg)->parent) in throtl_hierarchy_can_downgrade()
1960 static void throtl_downgrade_check(struct throtl_grp *tg) in throtl_downgrade_check() argument
1967 if (tg->td->limit_index != LIMIT_MAX || in throtl_downgrade_check()
1968 !tg->td->limit_valid[LIMIT_LOW]) in throtl_downgrade_check()
1970 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children)) in throtl_downgrade_check()
1972 if (time_after(tg->last_check_time + tg->td->throtl_slice, now)) in throtl_downgrade_check()
1975 elapsed_time = now - tg->last_check_time; in throtl_downgrade_check()
1976 tg->last_check_time = now; in throtl_downgrade_check()
1978 if (time_before(now, tg_last_low_overflow_time(tg) + in throtl_downgrade_check()
1979 tg->td->throtl_slice)) in throtl_downgrade_check()
1982 if (tg->bps[READ][LIMIT_LOW]) { in throtl_downgrade_check()
1983 bps = tg->last_bytes_disp[READ] * HZ; in throtl_downgrade_check()
1985 if (bps >= tg->bps[READ][LIMIT_LOW]) in throtl_downgrade_check()
1986 tg->last_low_overflow_time[READ] = now; in throtl_downgrade_check()
1989 if (tg->bps[WRITE][LIMIT_LOW]) { in throtl_downgrade_check()
1990 bps = tg->last_bytes_disp[WRITE] * HZ; in throtl_downgrade_check()
1992 if (bps >= tg->bps[WRITE][LIMIT_LOW]) in throtl_downgrade_check()
1993 tg->last_low_overflow_time[WRITE] = now; in throtl_downgrade_check()
1996 if (tg->iops[READ][LIMIT_LOW]) { in throtl_downgrade_check()
1997 iops = tg->last_io_disp[READ] * HZ / elapsed_time; in throtl_downgrade_check()
1998 if (iops >= tg->iops[READ][LIMIT_LOW]) in throtl_downgrade_check()
1999 tg->last_low_overflow_time[READ] = now; in throtl_downgrade_check()
2002 if (tg->iops[WRITE][LIMIT_LOW]) { in throtl_downgrade_check()
2003 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time; in throtl_downgrade_check()
2004 if (iops >= tg->iops[WRITE][LIMIT_LOW]) in throtl_downgrade_check()
2005 tg->last_low_overflow_time[WRITE] = now; in throtl_downgrade_check()
2012 if (throtl_hierarchy_can_downgrade(tg)) in throtl_downgrade_check()
2013 throtl_downgrade_state(tg->td, LIMIT_LOW); in throtl_downgrade_check()
2015 tg->last_bytes_disp[READ] = 0; in throtl_downgrade_check()
2016 tg->last_bytes_disp[WRITE] = 0; in throtl_downgrade_check()
2017 tg->last_io_disp[READ] = 0; in throtl_downgrade_check()
2018 tg->last_io_disp[WRITE] = 0; in throtl_downgrade_check()
2021 static void blk_throtl_update_idletime(struct throtl_grp *tg) in blk_throtl_update_idletime() argument
2024 unsigned long last_finish_time = tg->last_finish_time; in blk_throtl_update_idletime()
2027 last_finish_time == tg->checked_last_finish_time) in blk_throtl_update_idletime()
2030 tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3; in blk_throtl_update_idletime()
2031 tg->checked_last_finish_time = last_finish_time; in blk_throtl_update_idletime()
2121 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg); in blk_throtl_bio() local
2125 struct throtl_data *td = tg->td; in blk_throtl_bio()
2130 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw]) in blk_throtl_bio()
2137 blk_throtl_update_idletime(tg); in blk_throtl_bio()
2139 sq = &tg->service_queue; in blk_throtl_bio()
2143 if (tg->last_low_overflow_time[rw] == 0) in blk_throtl_bio()
2144 tg->last_low_overflow_time[rw] = jiffies; in blk_throtl_bio()
2145 throtl_downgrade_check(tg); in blk_throtl_bio()
2146 throtl_upgrade_check(tg); in blk_throtl_bio()
2152 if (!tg_may_dispatch(tg, bio, NULL)) { in blk_throtl_bio()
2153 tg->last_low_overflow_time[rw] = jiffies; in blk_throtl_bio()
2154 if (throtl_can_upgrade(td, tg)) { in blk_throtl_bio()
2162 throtl_charge_bio(tg, bio); in blk_throtl_bio()
2175 throtl_trim_slice(tg, rw); in blk_throtl_bio()
2182 qn = &tg->qnode_on_parent[rw]; in blk_throtl_bio()
2184 tg = sq_to_tg(sq); in blk_throtl_bio()
2185 if (!tg) in blk_throtl_bio()
2192 tg->bytes_disp[rw], bio->bi_iter.bi_size, in blk_throtl_bio()
2193 tg_bps_limit(tg, rw), in blk_throtl_bio()
2194 tg->io_disp[rw], tg_iops_limit(tg, rw), in blk_throtl_bio()
2197 tg->last_low_overflow_time[rw] = jiffies; in blk_throtl_bio()
2200 throtl_add_bio_tg(bio, qn, tg); in blk_throtl_bio()
2209 if (tg->flags & THROTL_TG_WAS_EMPTY) { in blk_throtl_bio()
2210 tg_update_disptime(tg); in blk_throtl_bio()
2211 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true); in blk_throtl_bio()
2258 struct throtl_grp *tg; in blk_throtl_bio_endio() local
2268 tg = blkg_to_tg(blkg); in blk_throtl_bio_endio()
2271 tg->last_finish_time = finish_time_ns >> 10; in blk_throtl_bio_endio()
2281 throtl_track_latency(tg->td, bio_issue_size(&bio->bi_issue), in blk_throtl_bio_endio()
2284 if (tg->latency_target && lat >= tg->td->filtered_latency) { in blk_throtl_bio_endio()
2289 threshold = tg->td->avg_buckets[rw][bucket].latency + in blk_throtl_bio_endio()
2290 tg->latency_target; in blk_throtl_bio_endio()
2292 tg->bad_bio_cnt++; in blk_throtl_bio_endio()
2297 tg->bio_cnt++; in blk_throtl_bio_endio()
2300 if (time_after(jiffies, tg->bio_cnt_reset_time) || tg->bio_cnt > 1024) { in blk_throtl_bio_endio()
2301 tg->bio_cnt_reset_time = tg->td->throtl_slice + jiffies; in blk_throtl_bio_endio()
2302 tg->bio_cnt /= 2; in blk_throtl_bio_endio()
2303 tg->bad_bio_cnt /= 2; in blk_throtl_bio_endio()
2315 struct throtl_grp *tg; in tg_drain_bios() local
2317 while ((tg = throtl_rb_first(parent_sq))) { in tg_drain_bios()
2318 struct throtl_service_queue *sq = &tg->service_queue; in tg_drain_bios()
2321 throtl_dequeue_tg(tg); in tg_drain_bios()
2324 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in tg_drain_bios()
2326 tg_dispatch_one_bio(tg, bio_data_dir(bio)); in tg_drain_bios()