2 * Interface for controlling IO bandwidth on a request queue
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/bio.h>
11 #include <linux/blktrace_api.h>
12 #include <linux/blk-cgroup.h>
15 /* Max dispatch from a group in 1 round */
16 static int throtl_grp_quantum = 8;
18 /* Total max dispatch from all groups in one round */
19 static int throtl_quantum = 32;
21 /* Throttling is performed over 100ms slice and after that slice is renewed */
22 static unsigned long throtl_slice = HZ/10; /* 100 ms */
24 static struct blkcg_policy blkcg_policy_throtl;
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct *kthrotld_workqueue;
30 * To implement hierarchical throttling, throtl_grps form a tree and bios
31 * are dispatched upwards level by level until they reach the top and get
32 * issued. When dispatching bios from the children and local group at each
33 * level, if the bios are dispatched into a single bio_list, there's a risk
34 * of a local or child group which can queue many bios at once filling up
35 * the list starving others.
37 * To avoid such starvation, dispatched bios are queued separately
38 * according to where they came from. When they are again dispatched to
39 * the parent, they're popped in round-robin order so that no single source
40 * hogs the dispatch window.
42 * throtl_qnode is used to keep the queued bios separated by their sources.
43 * Bios are queued to throtl_qnode which in turn is queued to
44 * throtl_service_queue and then dispatched in round-robin order.
46 * It's also used to track the reference counts on blkg's. A qnode always
47 * belongs to a throtl_grp and gets queued on itself or the parent, so
48 * incrementing the reference of the associated throtl_grp when a qnode is
49 * queued and decrementing when dequeued is enough to keep the whole blkg
50 * tree pinned while bios are in flight.
53 struct list_head node; /* service_queue->queued[] */
54 struct bio_list bios; /* queued bios */
55 struct throtl_grp *tg; /* tg this qnode belongs to */
58 struct throtl_service_queue {
59 struct throtl_service_queue *parent_sq; /* the parent service_queue */
62 * Bios queued directly to this service_queue or dispatched from
63 * children throtl_grp's.
65 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
66 unsigned int nr_queued[2]; /* number of queued bios */
69 * RB tree of active children throtl_grp's, which are sorted by
72 struct rb_root pending_tree; /* RB tree of active tgs */
73 struct rb_node *first_pending; /* first node in the tree */
74 unsigned int nr_pending; /* # queued in the tree */
75 unsigned long first_pending_disptime; /* disptime of the first tg */
76 struct timer_list pending_timer; /* fires on first_pending_disptime */
80 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
81 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
84 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
93 /* must be the first member */
94 struct blkg_policy_data pd;
96 /* active throtl group service_queue member */
97 struct rb_node rb_node;
99 /* throtl_data this group belongs to */
100 struct throtl_data *td;
102 /* this group's service queue */
103 struct throtl_service_queue service_queue;
106 * qnode_on_self is used when bios are directly queued to this
107 * throtl_grp so that local bios compete fairly with bios
108 * dispatched from children. qnode_on_parent is used when bios are
109 * dispatched from this throtl_grp into its parent and will compete
110 * with the sibling qnode_on_parents and the parent's
113 struct throtl_qnode qnode_on_self[2];
114 struct throtl_qnode qnode_on_parent[2];
117 * Dispatch time in jiffies. This is the estimated time when group
118 * will unthrottle and is ready to dispatch more bio. It is used as
119 * key to sort active groups in service tree.
121 unsigned long disptime;
125 /* are there any throtl rules between this group and td? */
128 /* internally used bytes per second rate limits */
129 uint64_t bps[2][LIMIT_CNT];
130 /* user configured bps limits */
131 uint64_t bps_conf[2][LIMIT_CNT];
133 /* internally used IOPS limits */
134 unsigned int iops[2][LIMIT_CNT];
135 /* user configured IOPS limits */
136 unsigned int iops_conf[2][LIMIT_CNT];
138 /* Number of bytes disptached in current slice */
139 uint64_t bytes_disp[2];
140 /* Number of bio's dispatched in current slice */
141 unsigned int io_disp[2];
143 unsigned long last_low_overflow_time[2];
145 uint64_t last_bytes_disp[2];
146 unsigned int last_io_disp[2];
148 unsigned long last_check_time;
150 /* When did we start a new slice */
151 unsigned long slice_start[2];
152 unsigned long slice_end[2];
157 /* service tree for active throtl groups */
158 struct throtl_service_queue service_queue;
160 struct request_queue *queue;
162 /* Total Number of queued bios on READ and WRITE lists */
163 unsigned int nr_queued[2];
165 /* Work for dispatching throttled bios */
166 struct work_struct dispatch_work;
167 unsigned int limit_index;
168 bool limit_valid[LIMIT_CNT];
170 unsigned long low_upgrade_time;
171 unsigned long low_downgrade_time;
174 static void throtl_pending_timer_fn(unsigned long arg);
176 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
178 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
181 static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
183 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
186 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
188 return pd_to_blkg(&tg->pd);
192 * sq_to_tg - return the throl_grp the specified service queue belongs to
193 * @sq: the throtl_service_queue of interest
195 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
196 * embedded in throtl_data, %NULL is returned.
198 static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
200 if (sq && sq->parent_sq)
201 return container_of(sq, struct throtl_grp, service_queue);
207 * sq_to_td - return throtl_data the specified service queue belongs to
208 * @sq: the throtl_service_queue of interest
210 * A service_queue can be embedded in either a throtl_grp or throtl_data.
211 * Determine the associated throtl_data accordingly and return it.
213 static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
215 struct throtl_grp *tg = sq_to_tg(sq);
220 return container_of(sq, struct throtl_data, service_queue);
223 static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
225 struct blkcg_gq *blkg = tg_to_blkg(tg);
228 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
230 ret = tg->bps[rw][tg->td->limit_index];
231 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
232 return tg->bps[rw][LIMIT_MAX];
236 static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
238 struct blkcg_gq *blkg = tg_to_blkg(tg);
241 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
243 ret = tg->iops[rw][tg->td->limit_index];
244 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
245 return tg->iops[rw][LIMIT_MAX];
250 * throtl_log - log debug message via blktrace
251 * @sq: the service_queue being reported
252 * @fmt: printf format string
255 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
256 * throtl_grp; otherwise, just "throtl".
258 #define throtl_log(sq, fmt, args...) do { \
259 struct throtl_grp *__tg = sq_to_tg((sq)); \
260 struct throtl_data *__td = sq_to_td((sq)); \
263 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
268 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
269 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
271 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
275 static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
277 INIT_LIST_HEAD(&qn->node);
278 bio_list_init(&qn->bios);
283 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
284 * @bio: bio being added
285 * @qn: qnode to add bio to
286 * @queued: the service_queue->queued[] list @qn belongs to
288 * Add @bio to @qn and put @qn on @queued if it's not already on.
289 * @qn->tg's reference count is bumped when @qn is activated. See the
290 * comment on top of throtl_qnode definition for details.
292 static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
293 struct list_head *queued)
295 bio_list_add(&qn->bios, bio);
296 if (list_empty(&qn->node)) {
297 list_add_tail(&qn->node, queued);
298 blkg_get(tg_to_blkg(qn->tg));
303 * throtl_peek_queued - peek the first bio on a qnode list
304 * @queued: the qnode list to peek
306 static struct bio *throtl_peek_queued(struct list_head *queued)
308 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
311 if (list_empty(queued))
314 bio = bio_list_peek(&qn->bios);
320 * throtl_pop_queued - pop the first bio form a qnode list
321 * @queued: the qnode list to pop a bio from
322 * @tg_to_put: optional out argument for throtl_grp to put
324 * Pop the first bio from the qnode list @queued. After popping, the first
325 * qnode is removed from @queued if empty or moved to the end of @queued so
326 * that the popping order is round-robin.
328 * When the first qnode is removed, its associated throtl_grp should be put
329 * too. If @tg_to_put is NULL, this function automatically puts it;
330 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
331 * responsible for putting it.
333 static struct bio *throtl_pop_queued(struct list_head *queued,
334 struct throtl_grp **tg_to_put)
336 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
339 if (list_empty(queued))
342 bio = bio_list_pop(&qn->bios);
345 if (bio_list_empty(&qn->bios)) {
346 list_del_init(&qn->node);
350 blkg_put(tg_to_blkg(qn->tg));
352 list_move_tail(&qn->node, queued);
358 /* init a service_queue, assumes the caller zeroed it */
359 static void throtl_service_queue_init(struct throtl_service_queue *sq)
361 INIT_LIST_HEAD(&sq->queued[0]);
362 INIT_LIST_HEAD(&sq->queued[1]);
363 sq->pending_tree = RB_ROOT;
364 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
368 static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
370 struct throtl_grp *tg;
373 tg = kzalloc_node(sizeof(*tg), gfp, node);
377 throtl_service_queue_init(&tg->service_queue);
379 for (rw = READ; rw <= WRITE; rw++) {
380 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
381 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
384 RB_CLEAR_NODE(&tg->rb_node);
385 tg->bps[READ][LIMIT_MAX] = U64_MAX;
386 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
387 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
388 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
389 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
390 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
391 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
392 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
393 /* LIMIT_LOW will have default value 0 */
398 static void throtl_pd_init(struct blkg_policy_data *pd)
400 struct throtl_grp *tg = pd_to_tg(pd);
401 struct blkcg_gq *blkg = tg_to_blkg(tg);
402 struct throtl_data *td = blkg->q->td;
403 struct throtl_service_queue *sq = &tg->service_queue;
406 * If on the default hierarchy, we switch to properly hierarchical
407 * behavior where limits on a given throtl_grp are applied to the
408 * whole subtree rather than just the group itself. e.g. If 16M
409 * read_bps limit is set on the root group, the whole system can't
410 * exceed 16M for the device.
412 * If not on the default hierarchy, the broken flat hierarchy
413 * behavior is retained where all throtl_grps are treated as if
414 * they're all separate root groups right below throtl_data.
415 * Limits of a group don't interact with limits of other groups
416 * regardless of the position of the group in the hierarchy.
418 sq->parent_sq = &td->service_queue;
419 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
420 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
425 * Set has_rules[] if @tg or any of its parents have limits configured.
426 * This doesn't require walking up to the top of the hierarchy as the
427 * parent's has_rules[] is guaranteed to be correct.
429 static void tg_update_has_rules(struct throtl_grp *tg)
431 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
432 struct throtl_data *td = tg->td;
435 for (rw = READ; rw <= WRITE; rw++)
436 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
437 (td->limit_valid[td->limit_index] &&
438 (tg_bps_limit(tg, rw) != U64_MAX ||
439 tg_iops_limit(tg, rw) != UINT_MAX));
442 static void throtl_pd_online(struct blkg_policy_data *pd)
445 * We don't want new groups to escape the limits of its ancestors.
446 * Update has_rules[] after a new group is brought online.
448 tg_update_has_rules(pd_to_tg(pd));
451 static void blk_throtl_update_limit_valid(struct throtl_data *td)
453 struct cgroup_subsys_state *pos_css;
454 struct blkcg_gq *blkg;
455 bool low_valid = false;
458 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
459 struct throtl_grp *tg = blkg_to_tg(blkg);
461 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
462 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
467 td->limit_valid[LIMIT_LOW] = low_valid;
470 static void throtl_upgrade_state(struct throtl_data *td);
471 static void throtl_pd_offline(struct blkg_policy_data *pd)
473 struct throtl_grp *tg = pd_to_tg(pd);
475 tg->bps[READ][LIMIT_LOW] = 0;
476 tg->bps[WRITE][LIMIT_LOW] = 0;
477 tg->iops[READ][LIMIT_LOW] = 0;
478 tg->iops[WRITE][LIMIT_LOW] = 0;
480 blk_throtl_update_limit_valid(tg->td);
482 if (!tg->td->limit_valid[tg->td->limit_index])
483 throtl_upgrade_state(tg->td);
486 static void throtl_pd_free(struct blkg_policy_data *pd)
488 struct throtl_grp *tg = pd_to_tg(pd);
490 del_timer_sync(&tg->service_queue.pending_timer);
494 static struct throtl_grp *
495 throtl_rb_first(struct throtl_service_queue *parent_sq)
497 /* Service tree is empty */
498 if (!parent_sq->nr_pending)
501 if (!parent_sq->first_pending)
502 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
504 if (parent_sq->first_pending)
505 return rb_entry_tg(parent_sq->first_pending);
510 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
516 static void throtl_rb_erase(struct rb_node *n,
517 struct throtl_service_queue *parent_sq)
519 if (parent_sq->first_pending == n)
520 parent_sq->first_pending = NULL;
521 rb_erase_init(n, &parent_sq->pending_tree);
522 --parent_sq->nr_pending;
525 static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
527 struct throtl_grp *tg;
529 tg = throtl_rb_first(parent_sq);
533 parent_sq->first_pending_disptime = tg->disptime;
536 static void tg_service_queue_add(struct throtl_grp *tg)
538 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
539 struct rb_node **node = &parent_sq->pending_tree.rb_node;
540 struct rb_node *parent = NULL;
541 struct throtl_grp *__tg;
542 unsigned long key = tg->disptime;
545 while (*node != NULL) {
547 __tg = rb_entry_tg(parent);
549 if (time_before(key, __tg->disptime))
550 node = &parent->rb_left;
552 node = &parent->rb_right;
558 parent_sq->first_pending = &tg->rb_node;
560 rb_link_node(&tg->rb_node, parent, node);
561 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
564 static void __throtl_enqueue_tg(struct throtl_grp *tg)
566 tg_service_queue_add(tg);
567 tg->flags |= THROTL_TG_PENDING;
568 tg->service_queue.parent_sq->nr_pending++;
571 static void throtl_enqueue_tg(struct throtl_grp *tg)
573 if (!(tg->flags & THROTL_TG_PENDING))
574 __throtl_enqueue_tg(tg);
577 static void __throtl_dequeue_tg(struct throtl_grp *tg)
579 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
580 tg->flags &= ~THROTL_TG_PENDING;
583 static void throtl_dequeue_tg(struct throtl_grp *tg)
585 if (tg->flags & THROTL_TG_PENDING)
586 __throtl_dequeue_tg(tg);
589 /* Call with queue lock held */
590 static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
591 unsigned long expires)
593 unsigned long max_expire = jiffies + 8 * throtl_slice;
596 * Since we are adjusting the throttle limit dynamically, the sleep
597 * time calculated according to previous limit might be invalid. It's
598 * possible the cgroup sleep time is very long and no other cgroups
599 * have IO running so notify the limit changes. Make sure the cgroup
600 * doesn't sleep too long to avoid the missed notification.
602 if (time_after(expires, max_expire))
603 expires = max_expire;
604 mod_timer(&sq->pending_timer, expires);
605 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
606 expires - jiffies, jiffies);
610 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
611 * @sq: the service_queue to schedule dispatch for
612 * @force: force scheduling
614 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
615 * dispatch time of the first pending child. Returns %true if either timer
616 * is armed or there's no pending child left. %false if the current
617 * dispatch window is still open and the caller should continue
620 * If @force is %true, the dispatch timer is always scheduled and this
621 * function is guaranteed to return %true. This is to be used when the
622 * caller can't dispatch itself and needs to invoke pending_timer
623 * unconditionally. Note that forced scheduling is likely to induce short
624 * delay before dispatch starts even if @sq->first_pending_disptime is not
625 * in the future and thus shouldn't be used in hot paths.
627 static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
630 /* any pending children left? */
634 update_min_dispatch_time(sq);
636 /* is the next dispatch time in the future? */
637 if (force || time_after(sq->first_pending_disptime, jiffies)) {
638 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
642 /* tell the caller to continue dispatching */
646 static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
647 bool rw, unsigned long start)
649 tg->bytes_disp[rw] = 0;
653 * Previous slice has expired. We must have trimmed it after last
654 * bio dispatch. That means since start of last slice, we never used
655 * that bandwidth. Do try to make use of that bandwidth while giving
658 if (time_after_eq(start, tg->slice_start[rw]))
659 tg->slice_start[rw] = start;
661 tg->slice_end[rw] = jiffies + throtl_slice;
662 throtl_log(&tg->service_queue,
663 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
664 rw == READ ? 'R' : 'W', tg->slice_start[rw],
665 tg->slice_end[rw], jiffies);
668 static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
670 tg->bytes_disp[rw] = 0;
672 tg->slice_start[rw] = jiffies;
673 tg->slice_end[rw] = jiffies + throtl_slice;
674 throtl_log(&tg->service_queue,
675 "[%c] new slice start=%lu end=%lu jiffies=%lu",
676 rw == READ ? 'R' : 'W', tg->slice_start[rw],
677 tg->slice_end[rw], jiffies);
680 static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
681 unsigned long jiffy_end)
683 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
686 static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
687 unsigned long jiffy_end)
689 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
690 throtl_log(&tg->service_queue,
691 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
692 rw == READ ? 'R' : 'W', tg->slice_start[rw],
693 tg->slice_end[rw], jiffies);
696 /* Determine if previously allocated or extended slice is complete or not */
697 static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
699 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
705 /* Trim the used slices and adjust slice start accordingly */
706 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
708 unsigned long nr_slices, time_elapsed, io_trim;
711 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
714 * If bps are unlimited (-1), then time slice don't get
715 * renewed. Don't try to trim the slice if slice is used. A new
716 * slice will start when appropriate.
718 if (throtl_slice_used(tg, rw))
722 * A bio has been dispatched. Also adjust slice_end. It might happen
723 * that initially cgroup limit was very low resulting in high
724 * slice_end, but later limit was bumped up and bio was dispached
725 * sooner, then we need to reduce slice_end. A high bogus slice_end
726 * is bad because it does not allow new slice to start.
729 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
731 time_elapsed = jiffies - tg->slice_start[rw];
733 nr_slices = time_elapsed / throtl_slice;
737 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
741 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
743 if (!bytes_trim && !io_trim)
746 if (tg->bytes_disp[rw] >= bytes_trim)
747 tg->bytes_disp[rw] -= bytes_trim;
749 tg->bytes_disp[rw] = 0;
751 if (tg->io_disp[rw] >= io_trim)
752 tg->io_disp[rw] -= io_trim;
756 tg->slice_start[rw] += nr_slices * throtl_slice;
758 throtl_log(&tg->service_queue,
759 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
760 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
761 tg->slice_start[rw], tg->slice_end[rw], jiffies);
764 static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
767 bool rw = bio_data_dir(bio);
768 unsigned int io_allowed;
769 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
772 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
774 /* Slice has just started. Consider one slice interval */
776 jiffy_elapsed_rnd = throtl_slice;
778 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
781 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
782 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
783 * will allow dispatch after 1 second and after that slice should
787 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
791 io_allowed = UINT_MAX;
795 if (tg->io_disp[rw] + 1 <= io_allowed) {
801 /* Calc approx time to dispatch */
802 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
804 if (jiffy_wait > jiffy_elapsed)
805 jiffy_wait = jiffy_wait - jiffy_elapsed;
814 static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
817 bool rw = bio_data_dir(bio);
818 u64 bytes_allowed, extra_bytes, tmp;
819 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
821 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
823 /* Slice has just started. Consider one slice interval */
825 jiffy_elapsed_rnd = throtl_slice;
827 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
829 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
833 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
839 /* Calc approx time to dispatch */
840 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
841 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
847 * This wait time is without taking into consideration the rounding
848 * up we did. Add that time also.
850 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
857 * Returns whether one can dispatch a bio or not. Also returns approx number
858 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
860 static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
863 bool rw = bio_data_dir(bio);
864 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
867 * Currently whole state machine of group depends on first bio
868 * queued in the group bio list. So one should not be calling
869 * this function with a different bio if there are other bios
872 BUG_ON(tg->service_queue.nr_queued[rw] &&
873 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
875 /* If tg->bps = -1, then BW is unlimited */
876 if (tg_bps_limit(tg, rw) == U64_MAX &&
877 tg_iops_limit(tg, rw) == UINT_MAX) {
884 * If previous slice expired, start a new one otherwise renew/extend
885 * existing slice to make sure it is at least throtl_slice interval
886 * long since now. New slice is started only for empty throttle group.
887 * If there is queued bio, that means there should be an active
888 * slice and it should be extended instead.
890 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
891 throtl_start_new_slice(tg, rw);
893 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
894 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
897 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
898 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
904 max_wait = max(bps_wait, iops_wait);
909 if (time_before(tg->slice_end[rw], jiffies + max_wait))
910 throtl_extend_slice(tg, rw, jiffies + max_wait);
915 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
917 bool rw = bio_data_dir(bio);
919 /* Charge the bio to the group */
920 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
922 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
923 tg->last_io_disp[rw]++;
926 * BIO_THROTTLED is used to prevent the same bio to be throttled
927 * more than once as a throttled bio will go through blk-throtl the
928 * second time when it eventually gets issued. Set it when a bio
929 * is being charged to a tg.
931 if (!bio_flagged(bio, BIO_THROTTLED))
932 bio_set_flag(bio, BIO_THROTTLED);
936 * throtl_add_bio_tg - add a bio to the specified throtl_grp
939 * @tg: the target throtl_grp
941 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
942 * tg->qnode_on_self[] is used.
944 static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
945 struct throtl_grp *tg)
947 struct throtl_service_queue *sq = &tg->service_queue;
948 bool rw = bio_data_dir(bio);
951 qn = &tg->qnode_on_self[rw];
954 * If @tg doesn't currently have any bios queued in the same
955 * direction, queueing @bio can change when @tg should be
956 * dispatched. Mark that @tg was empty. This is automatically
957 * cleaered on the next tg_update_disptime().
959 if (!sq->nr_queued[rw])
960 tg->flags |= THROTL_TG_WAS_EMPTY;
962 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
965 throtl_enqueue_tg(tg);
968 static void tg_update_disptime(struct throtl_grp *tg)
970 struct throtl_service_queue *sq = &tg->service_queue;
971 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
974 bio = throtl_peek_queued(&sq->queued[READ]);
976 tg_may_dispatch(tg, bio, &read_wait);
978 bio = throtl_peek_queued(&sq->queued[WRITE]);
980 tg_may_dispatch(tg, bio, &write_wait);
982 min_wait = min(read_wait, write_wait);
983 disptime = jiffies + min_wait;
985 /* Update dispatch time */
986 throtl_dequeue_tg(tg);
987 tg->disptime = disptime;
988 throtl_enqueue_tg(tg);
990 /* see throtl_add_bio_tg() */
991 tg->flags &= ~THROTL_TG_WAS_EMPTY;
994 static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
995 struct throtl_grp *parent_tg, bool rw)
997 if (throtl_slice_used(parent_tg, rw)) {
998 throtl_start_new_slice_with_credit(parent_tg, rw,
999 child_tg->slice_start[rw]);
1004 static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
1006 struct throtl_service_queue *sq = &tg->service_queue;
1007 struct throtl_service_queue *parent_sq = sq->parent_sq;
1008 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
1009 struct throtl_grp *tg_to_put = NULL;
1013 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1014 * from @tg may put its reference and @parent_sq might end up
1015 * getting released prematurely. Remember the tg to put and put it
1016 * after @bio is transferred to @parent_sq.
1018 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
1019 sq->nr_queued[rw]--;
1021 throtl_charge_bio(tg, bio);
1024 * If our parent is another tg, we just need to transfer @bio to
1025 * the parent using throtl_add_bio_tg(). If our parent is
1026 * @td->service_queue, @bio is ready to be issued. Put it on its
1027 * bio_lists[] and decrease total number queued. The caller is
1028 * responsible for issuing these bios.
1031 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
1032 start_parent_slice_with_credit(tg, parent_tg, rw);
1034 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1035 &parent_sq->queued[rw]);
1036 BUG_ON(tg->td->nr_queued[rw] <= 0);
1037 tg->td->nr_queued[rw]--;
1040 throtl_trim_slice(tg, rw);
1043 blkg_put(tg_to_blkg(tg_to_put));
1046 static int throtl_dispatch_tg(struct throtl_grp *tg)
1048 struct throtl_service_queue *sq = &tg->service_queue;
1049 unsigned int nr_reads = 0, nr_writes = 0;
1050 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
1051 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
1054 /* Try to dispatch 75% READS and 25% WRITES */
1056 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
1057 tg_may_dispatch(tg, bio, NULL)) {
1059 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1062 if (nr_reads >= max_nr_reads)
1066 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
1067 tg_may_dispatch(tg, bio, NULL)) {
1069 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1072 if (nr_writes >= max_nr_writes)
1076 return nr_reads + nr_writes;
1079 static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
1081 unsigned int nr_disp = 0;
1084 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1085 struct throtl_service_queue *sq = &tg->service_queue;
1090 if (time_before(jiffies, tg->disptime))
1093 throtl_dequeue_tg(tg);
1095 nr_disp += throtl_dispatch_tg(tg);
1097 if (sq->nr_queued[0] || sq->nr_queued[1])
1098 tg_update_disptime(tg);
1100 if (nr_disp >= throtl_quantum)
1107 static bool throtl_can_upgrade(struct throtl_data *td,
1108 struct throtl_grp *this_tg);
1110 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1111 * @arg: the throtl_service_queue being serviced
1113 * This timer is armed when a child throtl_grp with active bio's become
1114 * pending and queued on the service_queue's pending_tree and expires when
1115 * the first child throtl_grp should be dispatched. This function
1116 * dispatches bio's from the children throtl_grps to the parent
1119 * If the parent's parent is another throtl_grp, dispatching is propagated
1120 * by either arming its pending_timer or repeating dispatch directly. If
1121 * the top-level service_tree is reached, throtl_data->dispatch_work is
1122 * kicked so that the ready bio's are issued.
1124 static void throtl_pending_timer_fn(unsigned long arg)
1126 struct throtl_service_queue *sq = (void *)arg;
1127 struct throtl_grp *tg = sq_to_tg(sq);
1128 struct throtl_data *td = sq_to_td(sq);
1129 struct request_queue *q = td->queue;
1130 struct throtl_service_queue *parent_sq;
1134 spin_lock_irq(q->queue_lock);
1135 if (throtl_can_upgrade(td, NULL))
1136 throtl_upgrade_state(td);
1139 parent_sq = sq->parent_sq;
1143 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
1144 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1145 sq->nr_queued[READ], sq->nr_queued[WRITE]);
1147 ret = throtl_select_dispatch(sq);
1149 throtl_log(sq, "bios disp=%u", ret);
1153 if (throtl_schedule_next_dispatch(sq, false))
1156 /* this dispatch windows is still open, relax and repeat */
1157 spin_unlock_irq(q->queue_lock);
1159 spin_lock_irq(q->queue_lock);
1166 /* @parent_sq is another throl_grp, propagate dispatch */
1167 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1168 tg_update_disptime(tg);
1169 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1170 /* window is already open, repeat dispatching */
1177 /* reached the top-level, queue issueing */
1178 queue_work(kthrotld_workqueue, &td->dispatch_work);
1181 spin_unlock_irq(q->queue_lock);
1185 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1186 * @work: work item being executed
1188 * This function is queued for execution when bio's reach the bio_lists[]
1189 * of throtl_data->service_queue. Those bio's are ready and issued by this
1192 static void blk_throtl_dispatch_work_fn(struct work_struct *work)
1194 struct throtl_data *td = container_of(work, struct throtl_data,
1196 struct throtl_service_queue *td_sq = &td->service_queue;
1197 struct request_queue *q = td->queue;
1198 struct bio_list bio_list_on_stack;
1200 struct blk_plug plug;
1203 bio_list_init(&bio_list_on_stack);
1205 spin_lock_irq(q->queue_lock);
1206 for (rw = READ; rw <= WRITE; rw++)
1207 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1208 bio_list_add(&bio_list_on_stack, bio);
1209 spin_unlock_irq(q->queue_lock);
1211 if (!bio_list_empty(&bio_list_on_stack)) {
1212 blk_start_plug(&plug);
1213 while((bio = bio_list_pop(&bio_list_on_stack)))
1214 generic_make_request(bio);
1215 blk_finish_plug(&plug);
1219 static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1222 struct throtl_grp *tg = pd_to_tg(pd);
1223 u64 v = *(u64 *)((void *)tg + off);
1227 return __blkg_prfill_u64(sf, pd, v);
1230 static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1233 struct throtl_grp *tg = pd_to_tg(pd);
1234 unsigned int v = *(unsigned int *)((void *)tg + off);
1238 return __blkg_prfill_u64(sf, pd, v);
1241 static int tg_print_conf_u64(struct seq_file *sf, void *v)
1243 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1244 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1248 static int tg_print_conf_uint(struct seq_file *sf, void *v)
1250 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1251 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1255 static void tg_conf_updated(struct throtl_grp *tg)
1257 struct throtl_service_queue *sq = &tg->service_queue;
1258 struct cgroup_subsys_state *pos_css;
1259 struct blkcg_gq *blkg;
1261 throtl_log(&tg->service_queue,
1262 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1263 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1264 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
1267 * Update has_rules[] flags for the updated tg's subtree. A tg is
1268 * considered to have rules if either the tg itself or any of its
1269 * ancestors has rules. This identifies groups without any
1270 * restrictions in the whole hierarchy and allows them to bypass
1273 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
1274 tg_update_has_rules(blkg_to_tg(blkg));
1277 * We're already holding queue_lock and know @tg is valid. Let's
1278 * apply the new config directly.
1280 * Restart the slices for both READ and WRITES. It might happen
1281 * that a group's limit are dropped suddenly and we don't want to
1282 * account recently dispatched IO with new low rate.
1284 throtl_start_new_slice(tg, 0);
1285 throtl_start_new_slice(tg, 1);
1287 if (tg->flags & THROTL_TG_PENDING) {
1288 tg_update_disptime(tg);
1289 throtl_schedule_next_dispatch(sq->parent_sq, true);
1293 static ssize_t tg_set_conf(struct kernfs_open_file *of,
1294 char *buf, size_t nbytes, loff_t off, bool is_u64)
1296 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1297 struct blkg_conf_ctx ctx;
1298 struct throtl_grp *tg;
1302 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1307 if (sscanf(ctx.body, "%llu", &v) != 1)
1312 tg = blkg_to_tg(ctx.blkg);
1315 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1317 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1319 tg_conf_updated(tg);
1322 blkg_conf_finish(&ctx);
1323 return ret ?: nbytes;
1326 static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1327 char *buf, size_t nbytes, loff_t off)
1329 return tg_set_conf(of, buf, nbytes, off, true);
1332 static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1333 char *buf, size_t nbytes, loff_t off)
1335 return tg_set_conf(of, buf, nbytes, off, false);
1338 static struct cftype throtl_legacy_files[] = {
1340 .name = "throttle.read_bps_device",
1341 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
1342 .seq_show = tg_print_conf_u64,
1343 .write = tg_set_conf_u64,
1346 .name = "throttle.write_bps_device",
1347 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
1348 .seq_show = tg_print_conf_u64,
1349 .write = tg_set_conf_u64,
1352 .name = "throttle.read_iops_device",
1353 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
1354 .seq_show = tg_print_conf_uint,
1355 .write = tg_set_conf_uint,
1358 .name = "throttle.write_iops_device",
1359 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
1360 .seq_show = tg_print_conf_uint,
1361 .write = tg_set_conf_uint,
1364 .name = "throttle.io_service_bytes",
1365 .private = (unsigned long)&blkcg_policy_throtl,
1366 .seq_show = blkg_print_stat_bytes,
1369 .name = "throttle.io_serviced",
1370 .private = (unsigned long)&blkcg_policy_throtl,
1371 .seq_show = blkg_print_stat_ios,
1376 static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
1379 struct throtl_grp *tg = pd_to_tg(pd);
1380 const char *dname = blkg_dev_name(pd->blkg);
1381 char bufs[4][21] = { "max", "max", "max", "max" };
1383 unsigned int iops_dft;
1388 if (off == LIMIT_LOW) {
1393 iops_dft = UINT_MAX;
1396 if (tg->bps_conf[READ][off] == bps_dft &&
1397 tg->bps_conf[WRITE][off] == bps_dft &&
1398 tg->iops_conf[READ][off] == iops_dft &&
1399 tg->iops_conf[WRITE][off] == iops_dft)
1402 if (tg->bps_conf[READ][off] != bps_dft)
1403 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
1404 tg->bps_conf[READ][off]);
1405 if (tg->bps_conf[WRITE][off] != bps_dft)
1406 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
1407 tg->bps_conf[WRITE][off]);
1408 if (tg->iops_conf[READ][off] != iops_dft)
1409 snprintf(bufs[2], sizeof(bufs[2]), "%u",
1410 tg->iops_conf[READ][off]);
1411 if (tg->iops_conf[WRITE][off] != iops_dft)
1412 snprintf(bufs[3], sizeof(bufs[3]), "%u",
1413 tg->iops_conf[WRITE][off]);
1415 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1416 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1420 static int tg_print_limit(struct seq_file *sf, void *v)
1422 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
1423 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1427 static ssize_t tg_set_limit(struct kernfs_open_file *of,
1428 char *buf, size_t nbytes, loff_t off)
1430 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1431 struct blkg_conf_ctx ctx;
1432 struct throtl_grp *tg;
1435 int index = of_cft(of)->private;
1437 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1441 tg = blkg_to_tg(ctx.blkg);
1443 v[0] = tg->bps_conf[READ][index];
1444 v[1] = tg->bps_conf[WRITE][index];
1445 v[2] = tg->iops_conf[READ][index];
1446 v[3] = tg->iops_conf[WRITE][index];
1449 char tok[27]; /* wiops=18446744073709551616 */
1454 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1463 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1471 if (!strcmp(tok, "rbps"))
1473 else if (!strcmp(tok, "wbps"))
1475 else if (!strcmp(tok, "riops"))
1476 v[2] = min_t(u64, val, UINT_MAX);
1477 else if (!strcmp(tok, "wiops"))
1478 v[3] = min_t(u64, val, UINT_MAX);
1483 tg->bps_conf[READ][index] = v[0];
1484 tg->bps_conf[WRITE][index] = v[1];
1485 tg->iops_conf[READ][index] = v[2];
1486 tg->iops_conf[WRITE][index] = v[3];
1488 if (index == LIMIT_MAX) {
1489 tg->bps[READ][index] = v[0];
1490 tg->bps[WRITE][index] = v[1];
1491 tg->iops[READ][index] = v[2];
1492 tg->iops[WRITE][index] = v[3];
1494 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1495 tg->bps_conf[READ][LIMIT_MAX]);
1496 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1497 tg->bps_conf[WRITE][LIMIT_MAX]);
1498 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1499 tg->iops_conf[READ][LIMIT_MAX]);
1500 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1501 tg->iops_conf[WRITE][LIMIT_MAX]);
1503 if (index == LIMIT_LOW) {
1504 blk_throtl_update_limit_valid(tg->td);
1505 if (tg->td->limit_valid[LIMIT_LOW])
1506 tg->td->limit_index = LIMIT_LOW;
1508 tg_conf_updated(tg);
1511 blkg_conf_finish(&ctx);
1512 return ret ?: nbytes;
1515 static struct cftype throtl_files[] = {
1516 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1519 .flags = CFTYPE_NOT_ON_ROOT,
1520 .seq_show = tg_print_limit,
1521 .write = tg_set_limit,
1522 .private = LIMIT_LOW,
1527 .flags = CFTYPE_NOT_ON_ROOT,
1528 .seq_show = tg_print_limit,
1529 .write = tg_set_limit,
1530 .private = LIMIT_MAX,
1535 static void throtl_shutdown_wq(struct request_queue *q)
1537 struct throtl_data *td = q->td;
1539 cancel_work_sync(&td->dispatch_work);
1542 static struct blkcg_policy blkcg_policy_throtl = {
1543 .dfl_cftypes = throtl_files,
1544 .legacy_cftypes = throtl_legacy_files,
1546 .pd_alloc_fn = throtl_pd_alloc,
1547 .pd_init_fn = throtl_pd_init,
1548 .pd_online_fn = throtl_pd_online,
1549 .pd_offline_fn = throtl_pd_offline,
1550 .pd_free_fn = throtl_pd_free,
1553 static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1555 unsigned long rtime = jiffies, wtime = jiffies;
1557 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1558 rtime = tg->last_low_overflow_time[READ];
1559 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1560 wtime = tg->last_low_overflow_time[WRITE];
1561 return min(rtime, wtime);
1564 /* tg should not be an intermediate node */
1565 static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1567 struct throtl_service_queue *parent_sq;
1568 struct throtl_grp *parent = tg;
1569 unsigned long ret = __tg_last_low_overflow_time(tg);
1572 parent_sq = parent->service_queue.parent_sq;
1573 parent = sq_to_tg(parent_sq);
1578 * The parent doesn't have low limit, it always reaches low
1579 * limit. Its overflow time is useless for children
1581 if (!parent->bps[READ][LIMIT_LOW] &&
1582 !parent->iops[READ][LIMIT_LOW] &&
1583 !parent->bps[WRITE][LIMIT_LOW] &&
1584 !parent->iops[WRITE][LIMIT_LOW])
1586 if (time_after(__tg_last_low_overflow_time(parent), ret))
1587 ret = __tg_last_low_overflow_time(parent);
1592 static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1594 struct throtl_service_queue *sq = &tg->service_queue;
1595 bool read_limit, write_limit;
1598 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1599 * reaches), it's ok to upgrade to next limit
1601 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1602 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1603 if (!read_limit && !write_limit)
1605 if (read_limit && sq->nr_queued[READ] &&
1606 (!write_limit || sq->nr_queued[WRITE]))
1608 if (write_limit && sq->nr_queued[WRITE] &&
1609 (!read_limit || sq->nr_queued[READ]))
1614 static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1617 if (throtl_tg_can_upgrade(tg))
1619 tg = sq_to_tg(tg->service_queue.parent_sq);
1620 if (!tg || !tg_to_blkg(tg)->parent)
1626 static bool throtl_can_upgrade(struct throtl_data *td,
1627 struct throtl_grp *this_tg)
1629 struct cgroup_subsys_state *pos_css;
1630 struct blkcg_gq *blkg;
1632 if (td->limit_index != LIMIT_LOW)
1635 if (time_before(jiffies, td->low_downgrade_time + throtl_slice))
1639 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1640 struct throtl_grp *tg = blkg_to_tg(blkg);
1644 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1646 if (!throtl_hierarchy_can_upgrade(tg)) {
1655 static void throtl_upgrade_state(struct throtl_data *td)
1657 struct cgroup_subsys_state *pos_css;
1658 struct blkcg_gq *blkg;
1660 td->limit_index = LIMIT_MAX;
1661 td->low_upgrade_time = jiffies;
1663 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1664 struct throtl_grp *tg = blkg_to_tg(blkg);
1665 struct throtl_service_queue *sq = &tg->service_queue;
1667 tg->disptime = jiffies - 1;
1668 throtl_select_dispatch(sq);
1669 throtl_schedule_next_dispatch(sq, false);
1672 throtl_select_dispatch(&td->service_queue);
1673 throtl_schedule_next_dispatch(&td->service_queue, false);
1674 queue_work(kthrotld_workqueue, &td->dispatch_work);
1677 static void throtl_downgrade_state(struct throtl_data *td, int new)
1679 td->limit_index = new;
1680 td->low_downgrade_time = jiffies;
1683 static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1685 struct throtl_data *td = tg->td;
1686 unsigned long now = jiffies;
1689 * If cgroup is below low limit, consider downgrade and throttle other
1692 if (time_after_eq(now, td->low_upgrade_time + throtl_slice) &&
1693 time_after_eq(now, tg_last_low_overflow_time(tg) + throtl_slice))
1698 static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1701 if (!throtl_tg_can_downgrade(tg))
1703 tg = sq_to_tg(tg->service_queue.parent_sq);
1704 if (!tg || !tg_to_blkg(tg)->parent)
1710 static void throtl_downgrade_check(struct throtl_grp *tg)
1714 unsigned long elapsed_time;
1715 unsigned long now = jiffies;
1717 if (tg->td->limit_index != LIMIT_MAX ||
1718 !tg->td->limit_valid[LIMIT_LOW])
1720 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1722 if (time_after(tg->last_check_time + throtl_slice, now))
1725 elapsed_time = now - tg->last_check_time;
1726 tg->last_check_time = now;
1728 if (time_before(now, tg_last_low_overflow_time(tg) + throtl_slice))
1731 if (tg->bps[READ][LIMIT_LOW]) {
1732 bps = tg->last_bytes_disp[READ] * HZ;
1733 do_div(bps, elapsed_time);
1734 if (bps >= tg->bps[READ][LIMIT_LOW])
1735 tg->last_low_overflow_time[READ] = now;
1738 if (tg->bps[WRITE][LIMIT_LOW]) {
1739 bps = tg->last_bytes_disp[WRITE] * HZ;
1740 do_div(bps, elapsed_time);
1741 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1742 tg->last_low_overflow_time[WRITE] = now;
1745 if (tg->iops[READ][LIMIT_LOW]) {
1746 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1747 if (iops >= tg->iops[READ][LIMIT_LOW])
1748 tg->last_low_overflow_time[READ] = now;
1751 if (tg->iops[WRITE][LIMIT_LOW]) {
1752 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1753 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1754 tg->last_low_overflow_time[WRITE] = now;
1758 * If cgroup is below low limit, consider downgrade and throttle other
1761 if (throtl_hierarchy_can_downgrade(tg))
1762 throtl_downgrade_state(tg->td, LIMIT_LOW);
1764 tg->last_bytes_disp[READ] = 0;
1765 tg->last_bytes_disp[WRITE] = 0;
1766 tg->last_io_disp[READ] = 0;
1767 tg->last_io_disp[WRITE] = 0;
1770 bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1773 struct throtl_qnode *qn = NULL;
1774 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
1775 struct throtl_service_queue *sq;
1776 bool rw = bio_data_dir(bio);
1777 bool throttled = false;
1779 WARN_ON_ONCE(!rcu_read_lock_held());
1781 /* see throtl_charge_bio() */
1782 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
1785 spin_lock_irq(q->queue_lock);
1787 if (unlikely(blk_queue_bypass(q)))
1790 sq = &tg->service_queue;
1794 if (tg->last_low_overflow_time[rw] == 0)
1795 tg->last_low_overflow_time[rw] = jiffies;
1796 throtl_downgrade_check(tg);
1797 /* throtl is FIFO - if bios are already queued, should queue */
1798 if (sq->nr_queued[rw])
1801 /* if above limits, break to queue */
1802 if (!tg_may_dispatch(tg, bio, NULL)) {
1803 tg->last_low_overflow_time[rw] = jiffies;
1804 if (throtl_can_upgrade(tg->td, tg)) {
1805 throtl_upgrade_state(tg->td);
1811 /* within limits, let's charge and dispatch directly */
1812 throtl_charge_bio(tg, bio);
1815 * We need to trim slice even when bios are not being queued
1816 * otherwise it might happen that a bio is not queued for
1817 * a long time and slice keeps on extending and trim is not
1818 * called for a long time. Now if limits are reduced suddenly
1819 * we take into account all the IO dispatched so far at new
1820 * low rate and * newly queued IO gets a really long dispatch
1823 * So keep on trimming slice even if bio is not queued.
1825 throtl_trim_slice(tg, rw);
1828 * @bio passed through this layer without being throttled.
1829 * Climb up the ladder. If we''re already at the top, it
1830 * can be executed directly.
1832 qn = &tg->qnode_on_parent[rw];
1839 /* out-of-limit, queue to @tg */
1840 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1841 rw == READ ? 'R' : 'W',
1842 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1843 tg_bps_limit(tg, rw),
1844 tg->io_disp[rw], tg_iops_limit(tg, rw),
1845 sq->nr_queued[READ], sq->nr_queued[WRITE]);
1847 tg->last_low_overflow_time[rw] = jiffies;
1849 bio_associate_current(bio);
1850 tg->td->nr_queued[rw]++;
1851 throtl_add_bio_tg(bio, qn, tg);
1855 * Update @tg's dispatch time and force schedule dispatch if @tg
1856 * was empty before @bio. The forced scheduling isn't likely to
1857 * cause undue delay as @bio is likely to be dispatched directly if
1858 * its @tg's disptime is not in the future.
1860 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1861 tg_update_disptime(tg);
1862 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
1866 spin_unlock_irq(q->queue_lock);
1869 * As multiple blk-throtls may stack in the same issue path, we
1870 * don't want bios to leave with the flag set. Clear the flag if
1874 bio_clear_flag(bio, BIO_THROTTLED);
1879 * Dispatch all bios from all children tg's queued on @parent_sq. On
1880 * return, @parent_sq is guaranteed to not have any active children tg's
1881 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1883 static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1885 struct throtl_grp *tg;
1887 while ((tg = throtl_rb_first(parent_sq))) {
1888 struct throtl_service_queue *sq = &tg->service_queue;
1891 throtl_dequeue_tg(tg);
1893 while ((bio = throtl_peek_queued(&sq->queued[READ])))
1894 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1895 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
1896 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1901 * blk_throtl_drain - drain throttled bios
1902 * @q: request_queue to drain throttled bios for
1904 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1906 void blk_throtl_drain(struct request_queue *q)
1907 __releases(q->queue_lock) __acquires(q->queue_lock)
1909 struct throtl_data *td = q->td;
1910 struct blkcg_gq *blkg;
1911 struct cgroup_subsys_state *pos_css;
1915 queue_lockdep_assert_held(q);
1919 * Drain each tg while doing post-order walk on the blkg tree, so
1920 * that all bios are propagated to td->service_queue. It'd be
1921 * better to walk service_queue tree directly but blkg walk is
1924 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
1925 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
1927 /* finally, transfer bios from top-level tg's into the td */
1928 tg_drain_bios(&td->service_queue);
1931 spin_unlock_irq(q->queue_lock);
1933 /* all bios now should be in td->service_queue, issue them */
1934 for (rw = READ; rw <= WRITE; rw++)
1935 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1937 generic_make_request(bio);
1939 spin_lock_irq(q->queue_lock);
1942 int blk_throtl_init(struct request_queue *q)
1944 struct throtl_data *td;
1947 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1951 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1952 throtl_service_queue_init(&td->service_queue);
1957 td->limit_valid[LIMIT_MAX] = true;
1958 td->limit_index = LIMIT_MAX;
1959 td->low_upgrade_time = jiffies;
1960 td->low_downgrade_time = jiffies;
1961 /* activate policy */
1962 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1968 void blk_throtl_exit(struct request_queue *q)
1971 throtl_shutdown_wq(q);
1972 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1976 static int __init throtl_init(void)
1978 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1979 if (!kthrotld_workqueue)
1980 panic("Failed to create kthrotld\n");
1982 return blkcg_policy_register(&blkcg_policy_throtl);
1985 module_init(throtl_init);