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 "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 blkio_policy_type blkio_policy_throtl;
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct *kthrotld_workqueue;
28 static void throtl_schedule_delayed_work(struct throtl_data *td,
31 struct throtl_rb_root {
35 unsigned long min_disptime;
38 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
39 .count = 0, .min_disptime = 0}
41 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
44 /* List of throtl groups on the request queue*/
45 struct hlist_node tg_node;
47 /* active throtl group service_tree member */
48 struct rb_node rb_node;
51 * Dispatch time in jiffies. This is the estimated time when group
52 * will unthrottle and is ready to dispatch more bio. It is used as
53 * key to sort active groups in service tree.
55 unsigned long disptime;
60 /* Two lists for READ and WRITE */
61 struct bio_list bio_lists[2];
63 /* Number of queued bios on READ and WRITE lists */
64 unsigned int nr_queued[2];
66 /* bytes per second rate limits */
72 /* Number of bytes disptached in current slice */
73 uint64_t bytes_disp[2];
74 /* Number of bio's dispatched in current slice */
75 unsigned int io_disp[2];
77 /* When did we start a new slice */
78 unsigned long slice_start[2];
79 unsigned long slice_end[2];
81 /* Some throttle limits got updated for the group */
84 struct rcu_head rcu_head;
89 /* List of throtl groups */
90 struct hlist_head tg_list;
92 /* service tree for active throtl groups */
93 struct throtl_rb_root tg_service_tree;
95 struct throtl_grp *root_tg;
96 struct request_queue *queue;
98 /* Total Number of queued bios on READ and WRITE lists */
99 unsigned int nr_queued[2];
102 * number of total undestroyed groups
104 unsigned int nr_undestroyed_grps;
106 /* Work for dispatching throttled bios */
107 struct delayed_work throtl_work;
112 static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
114 return blkg_to_pdata(blkg, &blkio_policy_throtl);
117 static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
119 return pdata_to_blkg(tg, &blkio_policy_throtl);
122 enum tg_state_flags {
123 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
126 #define THROTL_TG_FNS(name) \
127 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
129 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
131 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
133 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
135 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
137 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
140 THROTL_TG_FNS(on_rr);
142 #define throtl_log_tg(td, tg, fmt, args...) \
143 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
144 blkg_path(tg_to_blkg(tg)), ##args); \
146 #define throtl_log(td, fmt, args...) \
147 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
149 static inline unsigned int total_nr_queued(struct throtl_data *td)
151 return td->nr_queued[0] + td->nr_queued[1];
154 static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
156 atomic_inc(&tg->ref);
160 static void throtl_free_tg(struct rcu_head *head)
162 struct throtl_grp *tg = container_of(head, struct throtl_grp, rcu_head);
163 struct blkio_group *blkg = tg_to_blkg(tg);
165 free_percpu(blkg->stats_cpu);
170 static void throtl_put_tg(struct throtl_grp *tg)
172 struct blkio_group *blkg = tg_to_blkg(tg);
174 BUG_ON(atomic_read(&tg->ref) <= 0);
175 if (!atomic_dec_and_test(&tg->ref))
178 /* release the extra blkcg reference this blkg has been holding */
179 css_put(&blkg->blkcg->css);
182 * A group is freed in rcu manner. But having an rcu lock does not
183 * mean that one can access all the fields of blkg and assume these
184 * are valid. For example, don't try to follow throtl_data and
185 * request queue links.
187 * Having a reference to blkg under an rcu allows acess to only
188 * values local to groups like group stats and group rate limits
190 call_rcu(&tg->rcu_head, throtl_free_tg);
193 static void throtl_init_blkio_group(struct blkio_group *blkg)
195 struct throtl_grp *tg = blkg_to_tg(blkg);
197 INIT_HLIST_NODE(&tg->tg_node);
198 RB_CLEAR_NODE(&tg->rb_node);
199 bio_list_init(&tg->bio_lists[0]);
200 bio_list_init(&tg->bio_lists[1]);
201 tg->limits_changed = false;
206 tg->iops[WRITE] = -1;
209 * Take the initial reference that will be released on destroy
210 * This can be thought of a joint reference by cgroup and
211 * request queue which will be dropped by either request queue
212 * exit or cgroup deletion path depending on who is exiting first.
214 atomic_set(&tg->ref, 1);
217 static void throtl_link_blkio_group(struct request_queue *q,
218 struct blkio_group *blkg)
220 struct throtl_data *td = q->td;
221 struct throtl_grp *tg = blkg_to_tg(blkg);
223 hlist_add_head(&tg->tg_node, &td->tg_list);
224 td->nr_undestroyed_grps++;
228 throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
231 * This is the common case when there are no blkio cgroups.
232 * Avoid lookup in this case
234 if (blkcg == &blkio_root_cgroup)
237 return blkg_to_tg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL));
240 static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
241 struct blkio_cgroup *blkcg)
243 struct request_queue *q = td->queue;
244 struct throtl_grp *tg = NULL;
247 * This is the common case when there are no blkio cgroups.
248 * Avoid lookup in this case
250 if (blkcg == &blkio_root_cgroup) {
253 struct blkio_group *blkg;
255 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false);
257 /* if %NULL and @q is alive, fall back to root_tg */
259 tg = blkg_to_tg(blkg);
260 else if (!blk_queue_dead(q))
267 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
269 /* Service tree is empty */
274 root->left = rb_first(&root->rb);
277 return rb_entry_tg(root->left);
282 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
288 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
292 rb_erase_init(n, &root->rb);
296 static void update_min_dispatch_time(struct throtl_rb_root *st)
298 struct throtl_grp *tg;
300 tg = throtl_rb_first(st);
304 st->min_disptime = tg->disptime;
308 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
310 struct rb_node **node = &st->rb.rb_node;
311 struct rb_node *parent = NULL;
312 struct throtl_grp *__tg;
313 unsigned long key = tg->disptime;
316 while (*node != NULL) {
318 __tg = rb_entry_tg(parent);
320 if (time_before(key, __tg->disptime))
321 node = &parent->rb_left;
323 node = &parent->rb_right;
329 st->left = &tg->rb_node;
331 rb_link_node(&tg->rb_node, parent, node);
332 rb_insert_color(&tg->rb_node, &st->rb);
335 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
337 struct throtl_rb_root *st = &td->tg_service_tree;
339 tg_service_tree_add(st, tg);
340 throtl_mark_tg_on_rr(tg);
344 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
346 if (!throtl_tg_on_rr(tg))
347 __throtl_enqueue_tg(td, tg);
350 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
352 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
353 throtl_clear_tg_on_rr(tg);
356 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
358 if (throtl_tg_on_rr(tg))
359 __throtl_dequeue_tg(td, tg);
362 static void throtl_schedule_next_dispatch(struct throtl_data *td)
364 struct throtl_rb_root *st = &td->tg_service_tree;
367 * If there are more bios pending, schedule more work.
369 if (!total_nr_queued(td))
374 update_min_dispatch_time(st);
376 if (time_before_eq(st->min_disptime, jiffies))
377 throtl_schedule_delayed_work(td, 0);
379 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
383 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
385 tg->bytes_disp[rw] = 0;
387 tg->slice_start[rw] = jiffies;
388 tg->slice_end[rw] = jiffies + throtl_slice;
389 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
390 rw == READ ? 'R' : 'W', tg->slice_start[rw],
391 tg->slice_end[rw], jiffies);
394 static inline void throtl_set_slice_end(struct throtl_data *td,
395 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
397 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
400 static inline void throtl_extend_slice(struct throtl_data *td,
401 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
403 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
404 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
405 rw == READ ? 'R' : 'W', tg->slice_start[rw],
406 tg->slice_end[rw], jiffies);
409 /* Determine if previously allocated or extended slice is complete or not */
411 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
413 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
419 /* Trim the used slices and adjust slice start accordingly */
421 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
423 unsigned long nr_slices, time_elapsed, io_trim;
426 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
429 * If bps are unlimited (-1), then time slice don't get
430 * renewed. Don't try to trim the slice if slice is used. A new
431 * slice will start when appropriate.
433 if (throtl_slice_used(td, tg, rw))
437 * A bio has been dispatched. Also adjust slice_end. It might happen
438 * that initially cgroup limit was very low resulting in high
439 * slice_end, but later limit was bumped up and bio was dispached
440 * sooner, then we need to reduce slice_end. A high bogus slice_end
441 * is bad because it does not allow new slice to start.
444 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
446 time_elapsed = jiffies - tg->slice_start[rw];
448 nr_slices = time_elapsed / throtl_slice;
452 tmp = tg->bps[rw] * throtl_slice * nr_slices;
456 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
458 if (!bytes_trim && !io_trim)
461 if (tg->bytes_disp[rw] >= bytes_trim)
462 tg->bytes_disp[rw] -= bytes_trim;
464 tg->bytes_disp[rw] = 0;
466 if (tg->io_disp[rw] >= io_trim)
467 tg->io_disp[rw] -= io_trim;
471 tg->slice_start[rw] += nr_slices * throtl_slice;
473 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
474 " start=%lu end=%lu jiffies=%lu",
475 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
476 tg->slice_start[rw], tg->slice_end[rw], jiffies);
479 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
480 struct bio *bio, unsigned long *wait)
482 bool rw = bio_data_dir(bio);
483 unsigned int io_allowed;
484 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
487 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
489 /* Slice has just started. Consider one slice interval */
491 jiffy_elapsed_rnd = throtl_slice;
493 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
496 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
497 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
498 * will allow dispatch after 1 second and after that slice should
502 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
506 io_allowed = UINT_MAX;
510 if (tg->io_disp[rw] + 1 <= io_allowed) {
516 /* Calc approx time to dispatch */
517 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
519 if (jiffy_wait > jiffy_elapsed)
520 jiffy_wait = jiffy_wait - jiffy_elapsed;
529 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
530 struct bio *bio, unsigned long *wait)
532 bool rw = bio_data_dir(bio);
533 u64 bytes_allowed, extra_bytes, tmp;
534 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
536 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
538 /* Slice has just started. Consider one slice interval */
540 jiffy_elapsed_rnd = throtl_slice;
542 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
544 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
548 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
554 /* Calc approx time to dispatch */
555 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
556 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
562 * This wait time is without taking into consideration the rounding
563 * up we did. Add that time also.
565 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
571 static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
572 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
578 * Returns whether one can dispatch a bio or not. Also returns approx number
579 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
581 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
582 struct bio *bio, unsigned long *wait)
584 bool rw = bio_data_dir(bio);
585 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
588 * Currently whole state machine of group depends on first bio
589 * queued in the group bio list. So one should not be calling
590 * this function with a different bio if there are other bios
593 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
595 /* If tg->bps = -1, then BW is unlimited */
596 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
603 * If previous slice expired, start a new one otherwise renew/extend
604 * existing slice to make sure it is at least throtl_slice interval
607 if (throtl_slice_used(td, tg, rw))
608 throtl_start_new_slice(td, tg, rw);
610 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
611 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
614 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
615 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
621 max_wait = max(bps_wait, iops_wait);
626 if (time_before(tg->slice_end[rw], jiffies + max_wait))
627 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
632 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
634 bool rw = bio_data_dir(bio);
635 bool sync = rw_is_sync(bio->bi_rw);
637 /* Charge the bio to the group */
638 tg->bytes_disp[rw] += bio->bi_size;
641 blkiocg_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, rw, sync);
644 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
647 bool rw = bio_data_dir(bio);
649 bio_list_add(&tg->bio_lists[rw], bio);
650 /* Take a bio reference on tg */
651 throtl_ref_get_tg(tg);
654 throtl_enqueue_tg(td, tg);
657 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
659 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
662 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
663 tg_may_dispatch(td, tg, bio, &read_wait);
665 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
666 tg_may_dispatch(td, tg, bio, &write_wait);
668 min_wait = min(read_wait, write_wait);
669 disptime = jiffies + min_wait;
671 /* Update dispatch time */
672 throtl_dequeue_tg(td, tg);
673 tg->disptime = disptime;
674 throtl_enqueue_tg(td, tg);
677 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
678 bool rw, struct bio_list *bl)
682 bio = bio_list_pop(&tg->bio_lists[rw]);
684 /* Drop bio reference on tg */
687 BUG_ON(td->nr_queued[rw] <= 0);
690 throtl_charge_bio(tg, bio);
691 bio_list_add(bl, bio);
692 bio->bi_rw |= REQ_THROTTLED;
694 throtl_trim_slice(td, tg, rw);
697 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
700 unsigned int nr_reads = 0, nr_writes = 0;
701 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
702 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
705 /* Try to dispatch 75% READS and 25% WRITES */
707 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
708 && tg_may_dispatch(td, tg, bio, NULL)) {
710 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
713 if (nr_reads >= max_nr_reads)
717 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
718 && tg_may_dispatch(td, tg, bio, NULL)) {
720 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
723 if (nr_writes >= max_nr_writes)
727 return nr_reads + nr_writes;
730 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
732 unsigned int nr_disp = 0;
733 struct throtl_grp *tg;
734 struct throtl_rb_root *st = &td->tg_service_tree;
737 tg = throtl_rb_first(st);
742 if (time_before(jiffies, tg->disptime))
745 throtl_dequeue_tg(td, tg);
747 nr_disp += throtl_dispatch_tg(td, tg, bl);
749 if (tg->nr_queued[0] || tg->nr_queued[1]) {
750 tg_update_disptime(td, tg);
751 throtl_enqueue_tg(td, tg);
754 if (nr_disp >= throtl_quantum)
761 static void throtl_process_limit_change(struct throtl_data *td)
763 struct throtl_grp *tg;
764 struct hlist_node *pos, *n;
766 if (!td->limits_changed)
769 xchg(&td->limits_changed, false);
771 throtl_log(td, "limits changed");
773 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
774 if (!tg->limits_changed)
777 if (!xchg(&tg->limits_changed, false))
780 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
781 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
782 tg->iops[READ], tg->iops[WRITE]);
785 * Restart the slices for both READ and WRITES. It
786 * might happen that a group's limit are dropped
787 * suddenly and we don't want to account recently
788 * dispatched IO with new low rate
790 throtl_start_new_slice(td, tg, 0);
791 throtl_start_new_slice(td, tg, 1);
793 if (throtl_tg_on_rr(tg))
794 tg_update_disptime(td, tg);
798 /* Dispatch throttled bios. Should be called without queue lock held. */
799 static int throtl_dispatch(struct request_queue *q)
801 struct throtl_data *td = q->td;
802 unsigned int nr_disp = 0;
803 struct bio_list bio_list_on_stack;
805 struct blk_plug plug;
807 spin_lock_irq(q->queue_lock);
809 throtl_process_limit_change(td);
811 if (!total_nr_queued(td))
814 bio_list_init(&bio_list_on_stack);
816 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
817 total_nr_queued(td), td->nr_queued[READ],
818 td->nr_queued[WRITE]);
820 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
823 throtl_log(td, "bios disp=%u", nr_disp);
825 throtl_schedule_next_dispatch(td);
827 spin_unlock_irq(q->queue_lock);
830 * If we dispatched some requests, unplug the queue to make sure
834 blk_start_plug(&plug);
835 while((bio = bio_list_pop(&bio_list_on_stack)))
836 generic_make_request(bio);
837 blk_finish_plug(&plug);
842 void blk_throtl_work(struct work_struct *work)
844 struct throtl_data *td = container_of(work, struct throtl_data,
846 struct request_queue *q = td->queue;
851 /* Call with queue lock held */
853 throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
856 struct delayed_work *dwork = &td->throtl_work;
858 /* schedule work if limits changed even if no bio is queued */
859 if (total_nr_queued(td) || td->limits_changed) {
861 * We might have a work scheduled to be executed in future.
862 * Cancel that and schedule a new one.
864 __cancel_delayed_work(dwork);
865 queue_delayed_work(kthrotld_workqueue, dwork, delay);
866 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
872 throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
874 /* Something wrong if we are trying to remove same group twice */
875 BUG_ON(hlist_unhashed(&tg->tg_node));
877 hlist_del_init(&tg->tg_node);
880 * Put the reference taken at the time of creation so that when all
881 * queues are gone, group can be destroyed.
884 td->nr_undestroyed_grps--;
887 static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
889 struct hlist_node *pos, *n;
890 struct throtl_grp *tg;
893 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
895 if (!release_root && tg == td->root_tg)
899 * If cgroup removal path got to blk_group first and removed
900 * it from cgroup list, then it will take care of destroying
903 if (!blkiocg_del_blkio_group(tg_to_blkg(tg)))
904 throtl_destroy_tg(td, tg);
912 * Blk cgroup controller notification saying that blkio_group object is being
913 * delinked as associated cgroup object is going away. That also means that
914 * no new IO will come in this group. So get rid of this group as soon as
915 * any pending IO in the group is finished.
917 * This function is called under rcu_read_lock(). @q is the rcu protected
918 * pointer. That means @q is a valid request_queue pointer as long as we
921 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
922 * it should not be NULL as even if queue was going away, cgroup deltion
923 * path got to it first.
925 void throtl_unlink_blkio_group(struct request_queue *q,
926 struct blkio_group *blkg)
930 spin_lock_irqsave(q->queue_lock, flags);
931 throtl_destroy_tg(q->td, blkg_to_tg(blkg));
932 spin_unlock_irqrestore(q->queue_lock, flags);
935 static bool throtl_clear_queue(struct request_queue *q)
937 lockdep_assert_held(q->queue_lock);
940 * Clear tgs but leave the root one alone. This is necessary
941 * because root_tg is expected to be persistent and safe because
942 * blk-throtl can never be disabled while @q is alive. This is a
943 * kludge to prepare for unified blkg. This whole function will be
946 return throtl_release_tgs(q->td, false);
949 static void throtl_update_blkio_group_common(struct throtl_data *td,
950 struct throtl_grp *tg)
952 xchg(&tg->limits_changed, true);
953 xchg(&td->limits_changed, true);
954 /* Schedule a work now to process the limit change */
955 throtl_schedule_delayed_work(td, 0);
959 * For all update functions, @q should be a valid pointer because these
960 * update functions are called under blkcg_lock, that means, blkg is
961 * valid and in turn @q is valid. queue exit path can not race because
964 * Can not take queue lock in update functions as queue lock under blkcg_lock
965 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
967 static void throtl_update_blkio_group_read_bps(struct request_queue *q,
968 struct blkio_group *blkg, u64 read_bps)
970 struct throtl_grp *tg = blkg_to_tg(blkg);
972 tg->bps[READ] = read_bps;
973 throtl_update_blkio_group_common(q->td, tg);
976 static void throtl_update_blkio_group_write_bps(struct request_queue *q,
977 struct blkio_group *blkg, u64 write_bps)
979 struct throtl_grp *tg = blkg_to_tg(blkg);
981 tg->bps[WRITE] = write_bps;
982 throtl_update_blkio_group_common(q->td, tg);
985 static void throtl_update_blkio_group_read_iops(struct request_queue *q,
986 struct blkio_group *blkg, unsigned int read_iops)
988 struct throtl_grp *tg = blkg_to_tg(blkg);
990 tg->iops[READ] = read_iops;
991 throtl_update_blkio_group_common(q->td, tg);
994 static void throtl_update_blkio_group_write_iops(struct request_queue *q,
995 struct blkio_group *blkg, unsigned int write_iops)
997 struct throtl_grp *tg = blkg_to_tg(blkg);
999 tg->iops[WRITE] = write_iops;
1000 throtl_update_blkio_group_common(q->td, tg);
1003 static void throtl_shutdown_wq(struct request_queue *q)
1005 struct throtl_data *td = q->td;
1007 cancel_delayed_work_sync(&td->throtl_work);
1010 static struct blkio_policy_type blkio_policy_throtl = {
1012 .blkio_init_group_fn = throtl_init_blkio_group,
1013 .blkio_link_group_fn = throtl_link_blkio_group,
1014 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
1015 .blkio_clear_queue_fn = throtl_clear_queue,
1016 .blkio_update_group_read_bps_fn =
1017 throtl_update_blkio_group_read_bps,
1018 .blkio_update_group_write_bps_fn =
1019 throtl_update_blkio_group_write_bps,
1020 .blkio_update_group_read_iops_fn =
1021 throtl_update_blkio_group_read_iops,
1022 .blkio_update_group_write_iops_fn =
1023 throtl_update_blkio_group_write_iops,
1025 .plid = BLKIO_POLICY_THROTL,
1026 .pdata_size = sizeof(struct throtl_grp),
1029 bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1031 struct throtl_data *td = q->td;
1032 struct throtl_grp *tg;
1033 bool rw = bio_data_dir(bio), update_disptime = true;
1034 struct blkio_cgroup *blkcg;
1035 bool throttled = false;
1037 if (bio->bi_rw & REQ_THROTTLED) {
1038 bio->bi_rw &= ~REQ_THROTTLED;
1043 * A throtl_grp pointer retrieved under rcu can be used to access
1044 * basic fields like stats and io rates. If a group has no rules,
1045 * just update the dispatch stats in lockless manner and return.
1048 blkcg = task_blkio_cgroup(current);
1049 tg = throtl_lookup_tg(td, blkcg);
1051 if (tg_no_rule_group(tg, rw)) {
1052 blkiocg_update_dispatch_stats(tg_to_blkg(tg),
1054 rw_is_sync(bio->bi_rw));
1055 goto out_unlock_rcu;
1060 * Either group has not been allocated yet or it is not an unlimited
1063 spin_lock_irq(q->queue_lock);
1064 tg = throtl_lookup_create_tg(td, blkcg);
1068 if (tg->nr_queued[rw]) {
1070 * There is already another bio queued in same dir. No
1071 * need to update dispatch time.
1073 update_disptime = false;
1078 /* Bio is with-in rate limit of group */
1079 if (tg_may_dispatch(td, tg, bio, NULL)) {
1080 throtl_charge_bio(tg, bio);
1083 * We need to trim slice even when bios are not being queued
1084 * otherwise it might happen that a bio is not queued for
1085 * a long time and slice keeps on extending and trim is not
1086 * called for a long time. Now if limits are reduced suddenly
1087 * we take into account all the IO dispatched so far at new
1088 * low rate and * newly queued IO gets a really long dispatch
1091 * So keep on trimming slice even if bio is not queued.
1093 throtl_trim_slice(td, tg, rw);
1098 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1099 " iodisp=%u iops=%u queued=%d/%d",
1100 rw == READ ? 'R' : 'W',
1101 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1102 tg->io_disp[rw], tg->iops[rw],
1103 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1105 throtl_add_bio_tg(q->td, tg, bio);
1108 if (update_disptime) {
1109 tg_update_disptime(td, tg);
1110 throtl_schedule_next_dispatch(td);
1114 spin_unlock_irq(q->queue_lock);
1122 * blk_throtl_drain - drain throttled bios
1123 * @q: request_queue to drain throttled bios for
1125 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1127 void blk_throtl_drain(struct request_queue *q)
1128 __releases(q->queue_lock) __acquires(q->queue_lock)
1130 struct throtl_data *td = q->td;
1131 struct throtl_rb_root *st = &td->tg_service_tree;
1132 struct throtl_grp *tg;
1136 WARN_ON_ONCE(!queue_is_locked(q));
1140 while ((tg = throtl_rb_first(st))) {
1141 throtl_dequeue_tg(td, tg);
1143 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1144 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1145 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1146 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1148 spin_unlock_irq(q->queue_lock);
1150 while ((bio = bio_list_pop(&bl)))
1151 generic_make_request(bio);
1153 spin_lock_irq(q->queue_lock);
1156 int blk_throtl_init(struct request_queue *q)
1158 struct throtl_data *td;
1159 struct blkio_group *blkg;
1161 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1165 INIT_HLIST_HEAD(&td->tg_list);
1166 td->tg_service_tree = THROTL_RB_ROOT;
1167 td->limits_changed = false;
1168 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1173 /* alloc and init root group. */
1175 spin_lock_irq(q->queue_lock);
1177 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL,
1180 td->root_tg = blkg_to_tg(blkg);
1182 spin_unlock_irq(q->queue_lock);
1192 void blk_throtl_exit(struct request_queue *q)
1194 struct throtl_data *td = q->td;
1199 throtl_shutdown_wq(q);
1201 spin_lock_irq(q->queue_lock);
1202 throtl_release_tgs(td, true);
1204 /* If there are other groups */
1205 if (td->nr_undestroyed_grps > 0)
1208 spin_unlock_irq(q->queue_lock);
1211 * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods.
1212 * Do this wait only if there are other undestroyed groups out
1213 * there (other than root group). This can happen if cgroup deletion
1214 * path claimed the responsibility of cleaning up a group before
1215 * queue cleanup code get to the group.
1217 * Do not call synchronize_rcu() unconditionally as there are drivers
1218 * which create/delete request queue hundreds of times during scan/boot
1219 * and synchronize_rcu() can take significant time and slow down boot.
1225 * Just being safe to make sure after previous flush if some body did
1226 * update limits through cgroup and another work got queued, cancel
1229 throtl_shutdown_wq(q);
1234 static int __init throtl_init(void)
1236 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1237 if (!kthrotld_workqueue)
1238 panic("Failed to create kthrotld\n");
1240 blkio_policy_register(&blkio_policy_throtl);
1244 module_init(throtl_init);