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"
14 /* Max dispatch from a group in 1 round */
15 static int throtl_grp_quantum = 8;
17 /* Total max dispatch from all groups in one round */
18 static int throtl_quantum = 32;
20 /* Throttling is performed over 100ms slice and after that slice is renewed */
21 static unsigned long throtl_slice = HZ/10; /* 100 ms */
23 /* A workqueue to queue throttle related work */
24 static struct workqueue_struct *kthrotld_workqueue;
25 static void throtl_schedule_delayed_work(struct throtl_data *td,
28 struct throtl_rb_root {
32 unsigned long min_disptime;
35 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
36 .count = 0, .min_disptime = 0}
38 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
41 /* List of throtl groups on the request queue*/
42 struct hlist_node tg_node;
44 /* active throtl group service_tree member */
45 struct rb_node rb_node;
48 * Dispatch time in jiffies. This is the estimated time when group
49 * will unthrottle and is ready to dispatch more bio. It is used as
50 * key to sort active groups in service tree.
52 unsigned long disptime;
54 struct blkio_group blkg;
58 /* Two lists for READ and WRITE */
59 struct bio_list bio_lists[2];
61 /* Number of queued bios on READ and WRITE lists */
62 unsigned int nr_queued[2];
64 /* bytes per second rate limits */
70 /* Number of bytes disptached in current slice */
71 uint64_t bytes_disp[2];
72 /* Number of bio's dispatched in current slice */
73 unsigned int io_disp[2];
75 /* When did we start a new slice */
76 unsigned long slice_start[2];
77 unsigned long slice_end[2];
79 /* Some throttle limits got updated for the group */
85 /* List of throtl groups */
86 struct hlist_head tg_list;
88 /* service tree for active throtl groups */
89 struct throtl_rb_root tg_service_tree;
91 struct throtl_grp root_tg;
92 struct request_queue *queue;
94 /* Total Number of queued bios on READ and WRITE lists */
95 unsigned int nr_queued[2];
98 * number of total undestroyed groups
100 unsigned int nr_undestroyed_grps;
102 /* Work for dispatching throttled bios */
103 struct delayed_work throtl_work;
108 enum tg_state_flags {
109 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
112 #define THROTL_TG_FNS(name) \
113 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
115 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
117 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
119 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
121 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
123 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
126 THROTL_TG_FNS(on_rr);
128 #define throtl_log_tg(td, tg, fmt, args...) \
129 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
130 blkg_path(&(tg)->blkg), ##args); \
132 #define throtl_log(td, fmt, args...) \
133 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
135 static inline struct throtl_grp *tg_of_blkg(struct blkio_group *blkg)
138 return container_of(blkg, struct throtl_grp, blkg);
143 static inline int total_nr_queued(struct throtl_data *td)
145 return (td->nr_queued[0] + td->nr_queued[1]);
148 static inline struct throtl_grp *throtl_ref_get_tg(struct throtl_grp *tg)
150 atomic_inc(&tg->ref);
154 static void throtl_put_tg(struct throtl_grp *tg)
156 BUG_ON(atomic_read(&tg->ref) <= 0);
157 if (!atomic_dec_and_test(&tg->ref))
162 static struct throtl_grp * throtl_find_alloc_tg(struct throtl_data *td,
163 struct cgroup *cgroup)
165 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgroup);
166 struct throtl_grp *tg = NULL;
168 struct backing_dev_info *bdi = &td->queue->backing_dev_info;
169 unsigned int major, minor;
172 * TODO: Speed up blkiocg_lookup_group() by maintaining a radix
173 * tree of blkg (instead of traversing through hash list all
178 * This is the common case when there are no blkio cgroups.
179 * Avoid lookup in this case
181 if (blkcg == &blkio_root_cgroup)
184 tg = tg_of_blkg(blkiocg_lookup_group(blkcg, key));
186 /* Fill in device details for root group */
187 if (tg && !tg->blkg.dev && bdi->dev && dev_name(bdi->dev)) {
188 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
189 tg->blkg.dev = MKDEV(major, minor);
196 tg = kzalloc_node(sizeof(*tg), GFP_ATOMIC, td->queue->node);
200 INIT_HLIST_NODE(&tg->tg_node);
201 RB_CLEAR_NODE(&tg->rb_node);
202 bio_list_init(&tg->bio_lists[0]);
203 bio_list_init(&tg->bio_lists[1]);
204 td->limits_changed = false;
207 * Take the initial reference that will be released on destroy
208 * This can be thought of a joint reference by cgroup and
209 * request queue which will be dropped by either request queue
210 * exit or cgroup deletion path depending on who is exiting first.
212 atomic_set(&tg->ref, 1);
214 /* Add group onto cgroup list */
215 sscanf(dev_name(bdi->dev), "%u:%u", &major, &minor);
216 blkiocg_add_blkio_group(blkcg, &tg->blkg, (void *)td,
217 MKDEV(major, minor), BLKIO_POLICY_THROTL);
219 tg->bps[READ] = blkcg_get_read_bps(blkcg, tg->blkg.dev);
220 tg->bps[WRITE] = blkcg_get_write_bps(blkcg, tg->blkg.dev);
221 tg->iops[READ] = blkcg_get_read_iops(blkcg, tg->blkg.dev);
222 tg->iops[WRITE] = blkcg_get_write_iops(blkcg, tg->blkg.dev);
224 hlist_add_head(&tg->tg_node, &td->tg_list);
225 td->nr_undestroyed_grps++;
230 static struct throtl_grp * throtl_get_tg(struct throtl_data *td)
232 struct cgroup *cgroup;
233 struct throtl_grp *tg = NULL;
236 cgroup = task_cgroup(current, blkio_subsys_id);
237 tg = throtl_find_alloc_tg(td, cgroup);
244 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
246 /* Service tree is empty */
251 root->left = rb_first(&root->rb);
254 return rb_entry_tg(root->left);
259 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
265 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
269 rb_erase_init(n, &root->rb);
273 static void update_min_dispatch_time(struct throtl_rb_root *st)
275 struct throtl_grp *tg;
277 tg = throtl_rb_first(st);
281 st->min_disptime = tg->disptime;
285 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
287 struct rb_node **node = &st->rb.rb_node;
288 struct rb_node *parent = NULL;
289 struct throtl_grp *__tg;
290 unsigned long key = tg->disptime;
293 while (*node != NULL) {
295 __tg = rb_entry_tg(parent);
297 if (time_before(key, __tg->disptime))
298 node = &parent->rb_left;
300 node = &parent->rb_right;
306 st->left = &tg->rb_node;
308 rb_link_node(&tg->rb_node, parent, node);
309 rb_insert_color(&tg->rb_node, &st->rb);
312 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
314 struct throtl_rb_root *st = &td->tg_service_tree;
316 tg_service_tree_add(st, tg);
317 throtl_mark_tg_on_rr(tg);
321 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
323 if (!throtl_tg_on_rr(tg))
324 __throtl_enqueue_tg(td, tg);
327 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
329 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
330 throtl_clear_tg_on_rr(tg);
333 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
335 if (throtl_tg_on_rr(tg))
336 __throtl_dequeue_tg(td, tg);
339 static void throtl_schedule_next_dispatch(struct throtl_data *td)
341 struct throtl_rb_root *st = &td->tg_service_tree;
344 * If there are more bios pending, schedule more work.
346 if (!total_nr_queued(td))
351 update_min_dispatch_time(st);
353 if (time_before_eq(st->min_disptime, jiffies))
354 throtl_schedule_delayed_work(td, 0);
356 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
360 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
362 tg->bytes_disp[rw] = 0;
364 tg->slice_start[rw] = jiffies;
365 tg->slice_end[rw] = jiffies + throtl_slice;
366 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
367 rw == READ ? 'R' : 'W', tg->slice_start[rw],
368 tg->slice_end[rw], jiffies);
371 static inline void throtl_set_slice_end(struct throtl_data *td,
372 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
374 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
377 static inline void throtl_extend_slice(struct throtl_data *td,
378 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
380 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
381 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
382 rw == READ ? 'R' : 'W', tg->slice_start[rw],
383 tg->slice_end[rw], jiffies);
386 /* Determine if previously allocated or extended slice is complete or not */
388 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
390 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
396 /* Trim the used slices and adjust slice start accordingly */
398 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
400 unsigned long nr_slices, time_elapsed, io_trim;
403 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
406 * If bps are unlimited (-1), then time slice don't get
407 * renewed. Don't try to trim the slice if slice is used. A new
408 * slice will start when appropriate.
410 if (throtl_slice_used(td, tg, rw))
414 * A bio has been dispatched. Also adjust slice_end. It might happen
415 * that initially cgroup limit was very low resulting in high
416 * slice_end, but later limit was bumped up and bio was dispached
417 * sooner, then we need to reduce slice_end. A high bogus slice_end
418 * is bad because it does not allow new slice to start.
421 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
423 time_elapsed = jiffies - tg->slice_start[rw];
425 nr_slices = time_elapsed / throtl_slice;
429 tmp = tg->bps[rw] * throtl_slice * nr_slices;
433 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
435 if (!bytes_trim && !io_trim)
438 if (tg->bytes_disp[rw] >= bytes_trim)
439 tg->bytes_disp[rw] -= bytes_trim;
441 tg->bytes_disp[rw] = 0;
443 if (tg->io_disp[rw] >= io_trim)
444 tg->io_disp[rw] -= io_trim;
448 tg->slice_start[rw] += nr_slices * throtl_slice;
450 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
451 " start=%lu end=%lu jiffies=%lu",
452 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
453 tg->slice_start[rw], tg->slice_end[rw], jiffies);
456 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
457 struct bio *bio, unsigned long *wait)
459 bool rw = bio_data_dir(bio);
460 unsigned int io_allowed;
461 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
464 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
466 /* Slice has just started. Consider one slice interval */
468 jiffy_elapsed_rnd = throtl_slice;
470 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
473 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
474 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
475 * will allow dispatch after 1 second and after that slice should
479 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
483 io_allowed = UINT_MAX;
487 if (tg->io_disp[rw] + 1 <= io_allowed) {
493 /* Calc approx time to dispatch */
494 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
496 if (jiffy_wait > jiffy_elapsed)
497 jiffy_wait = jiffy_wait - jiffy_elapsed;
506 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
507 struct bio *bio, unsigned long *wait)
509 bool rw = bio_data_dir(bio);
510 u64 bytes_allowed, extra_bytes, tmp;
511 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
513 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
515 /* Slice has just started. Consider one slice interval */
517 jiffy_elapsed_rnd = throtl_slice;
519 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
521 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
525 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
531 /* Calc approx time to dispatch */
532 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
533 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
539 * This wait time is without taking into consideration the rounding
540 * up we did. Add that time also.
542 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
549 * Returns whether one can dispatch a bio or not. Also returns approx number
550 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
552 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
553 struct bio *bio, unsigned long *wait)
555 bool rw = bio_data_dir(bio);
556 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
559 * Currently whole state machine of group depends on first bio
560 * queued in the group bio list. So one should not be calling
561 * this function with a different bio if there are other bios
564 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
566 /* If tg->bps = -1, then BW is unlimited */
567 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
574 * If previous slice expired, start a new one otherwise renew/extend
575 * existing slice to make sure it is at least throtl_slice interval
578 if (throtl_slice_used(td, tg, rw))
579 throtl_start_new_slice(td, tg, rw);
581 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
582 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
585 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
586 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
592 max_wait = max(bps_wait, iops_wait);
597 if (time_before(tg->slice_end[rw], jiffies + max_wait))
598 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
603 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
605 bool rw = bio_data_dir(bio);
606 bool sync = bio->bi_rw & REQ_SYNC;
608 /* Charge the bio to the group */
609 tg->bytes_disp[rw] += bio->bi_size;
613 * TODO: This will take blkg->stats_lock. Figure out a way
614 * to avoid this cost.
616 blkiocg_update_dispatch_stats(&tg->blkg, bio->bi_size, rw, sync);
619 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
622 bool rw = bio_data_dir(bio);
624 bio_list_add(&tg->bio_lists[rw], bio);
625 /* Take a bio reference on tg */
626 throtl_ref_get_tg(tg);
629 throtl_enqueue_tg(td, tg);
632 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
634 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
637 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
638 tg_may_dispatch(td, tg, bio, &read_wait);
640 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
641 tg_may_dispatch(td, tg, bio, &write_wait);
643 min_wait = min(read_wait, write_wait);
644 disptime = jiffies + min_wait;
646 /* Update dispatch time */
647 throtl_dequeue_tg(td, tg);
648 tg->disptime = disptime;
649 throtl_enqueue_tg(td, tg);
652 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
653 bool rw, struct bio_list *bl)
657 bio = bio_list_pop(&tg->bio_lists[rw]);
659 /* Drop bio reference on tg */
662 BUG_ON(td->nr_queued[rw] <= 0);
665 throtl_charge_bio(tg, bio);
666 bio_list_add(bl, bio);
667 bio->bi_rw |= REQ_THROTTLED;
669 throtl_trim_slice(td, tg, rw);
672 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
675 unsigned int nr_reads = 0, nr_writes = 0;
676 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
677 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
680 /* Try to dispatch 75% READS and 25% WRITES */
682 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
683 && tg_may_dispatch(td, tg, bio, NULL)) {
685 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
688 if (nr_reads >= max_nr_reads)
692 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
693 && tg_may_dispatch(td, tg, bio, NULL)) {
695 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
698 if (nr_writes >= max_nr_writes)
702 return nr_reads + nr_writes;
705 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
707 unsigned int nr_disp = 0;
708 struct throtl_grp *tg;
709 struct throtl_rb_root *st = &td->tg_service_tree;
712 tg = throtl_rb_first(st);
717 if (time_before(jiffies, tg->disptime))
720 throtl_dequeue_tg(td, tg);
722 nr_disp += throtl_dispatch_tg(td, tg, bl);
724 if (tg->nr_queued[0] || tg->nr_queued[1]) {
725 tg_update_disptime(td, tg);
726 throtl_enqueue_tg(td, tg);
729 if (nr_disp >= throtl_quantum)
736 static void throtl_process_limit_change(struct throtl_data *td)
738 struct throtl_grp *tg;
739 struct hlist_node *pos, *n;
741 if (!td->limits_changed)
744 xchg(&td->limits_changed, false);
746 throtl_log(td, "limits changed");
748 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
749 if (!tg->limits_changed)
752 if (!xchg(&tg->limits_changed, false))
755 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
756 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
757 tg->iops[READ], tg->iops[WRITE]);
759 if (throtl_tg_on_rr(tg))
760 tg_update_disptime(td, tg);
764 /* Dispatch throttled bios. Should be called without queue lock held. */
765 static int throtl_dispatch(struct request_queue *q)
767 struct throtl_data *td = q->td;
768 unsigned int nr_disp = 0;
769 struct bio_list bio_list_on_stack;
772 spin_lock_irq(q->queue_lock);
774 throtl_process_limit_change(td);
776 if (!total_nr_queued(td))
779 bio_list_init(&bio_list_on_stack);
781 throtl_log(td, "dispatch nr_queued=%lu read=%u write=%u",
782 total_nr_queued(td), td->nr_queued[READ],
783 td->nr_queued[WRITE]);
785 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
788 throtl_log(td, "bios disp=%u", nr_disp);
790 throtl_schedule_next_dispatch(td);
792 spin_unlock_irq(q->queue_lock);
795 * If we dispatched some requests, unplug the queue to make sure
799 while((bio = bio_list_pop(&bio_list_on_stack)))
800 generic_make_request(bio);
806 void blk_throtl_work(struct work_struct *work)
808 struct throtl_data *td = container_of(work, struct throtl_data,
810 struct request_queue *q = td->queue;
815 /* Call with queue lock held */
817 throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
820 struct delayed_work *dwork = &td->throtl_work;
822 if (total_nr_queued(td) > 0) {
824 * We might have a work scheduled to be executed in future.
825 * Cancel that and schedule a new one.
827 __cancel_delayed_work(dwork);
828 queue_delayed_work(kthrotld_workqueue, dwork, delay);
829 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
835 throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
837 /* Something wrong if we are trying to remove same group twice */
838 BUG_ON(hlist_unhashed(&tg->tg_node));
840 hlist_del_init(&tg->tg_node);
843 * Put the reference taken at the time of creation so that when all
844 * queues are gone, group can be destroyed.
847 td->nr_undestroyed_grps--;
850 static void throtl_release_tgs(struct throtl_data *td)
852 struct hlist_node *pos, *n;
853 struct throtl_grp *tg;
855 hlist_for_each_entry_safe(tg, pos, n, &td->tg_list, tg_node) {
857 * If cgroup removal path got to blk_group first and removed
858 * it from cgroup list, then it will take care of destroying
861 if (!blkiocg_del_blkio_group(&tg->blkg))
862 throtl_destroy_tg(td, tg);
866 static void throtl_td_free(struct throtl_data *td)
872 * Blk cgroup controller notification saying that blkio_group object is being
873 * delinked as associated cgroup object is going away. That also means that
874 * no new IO will come in this group. So get rid of this group as soon as
875 * any pending IO in the group is finished.
877 * This function is called under rcu_read_lock(). key is the rcu protected
878 * pointer. That means "key" is a valid throtl_data pointer as long as we are
881 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
882 * it should not be NULL as even if queue was going away, cgroup deltion
883 * path got to it first.
885 void throtl_unlink_blkio_group(void *key, struct blkio_group *blkg)
888 struct throtl_data *td = key;
890 spin_lock_irqsave(td->queue->queue_lock, flags);
891 throtl_destroy_tg(td, tg_of_blkg(blkg));
892 spin_unlock_irqrestore(td->queue->queue_lock, flags);
895 static void throtl_update_blkio_group_common(struct throtl_data *td,
896 struct throtl_grp *tg)
898 xchg(&tg->limits_changed, true);
899 xchg(&td->limits_changed, true);
900 /* Schedule a work now to process the limit change */
901 throtl_schedule_delayed_work(td, 0);
905 * For all update functions, key should be a valid pointer because these
906 * update functions are called under blkcg_lock, that means, blkg is
907 * valid and in turn key is valid. queue exit path can not race becuase
910 * Can not take queue lock in update functions as queue lock under blkcg_lock
911 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
913 static void throtl_update_blkio_group_read_bps(void *key,
914 struct blkio_group *blkg, u64 read_bps)
916 struct throtl_data *td = key;
917 struct throtl_grp *tg = tg_of_blkg(blkg);
919 tg->bps[READ] = read_bps;
920 throtl_update_blkio_group_common(td, tg);
923 static void throtl_update_blkio_group_write_bps(void *key,
924 struct blkio_group *blkg, u64 write_bps)
926 struct throtl_data *td = key;
927 struct throtl_grp *tg = tg_of_blkg(blkg);
929 tg->bps[WRITE] = write_bps;
930 throtl_update_blkio_group_common(td, tg);
933 static void throtl_update_blkio_group_read_iops(void *key,
934 struct blkio_group *blkg, unsigned int read_iops)
936 struct throtl_data *td = key;
937 struct throtl_grp *tg = tg_of_blkg(blkg);
939 tg->iops[READ] = read_iops;
940 throtl_update_blkio_group_common(td, tg);
943 static void throtl_update_blkio_group_write_iops(void *key,
944 struct blkio_group *blkg, unsigned int write_iops)
946 struct throtl_data *td = key;
947 struct throtl_grp *tg = tg_of_blkg(blkg);
949 tg->iops[WRITE] = write_iops;
950 throtl_update_blkio_group_common(td, tg);
953 static void throtl_shutdown_wq(struct request_queue *q)
955 struct throtl_data *td = q->td;
957 cancel_delayed_work_sync(&td->throtl_work);
960 static struct blkio_policy_type blkio_policy_throtl = {
962 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
963 .blkio_update_group_read_bps_fn =
964 throtl_update_blkio_group_read_bps,
965 .blkio_update_group_write_bps_fn =
966 throtl_update_blkio_group_write_bps,
967 .blkio_update_group_read_iops_fn =
968 throtl_update_blkio_group_read_iops,
969 .blkio_update_group_write_iops_fn =
970 throtl_update_blkio_group_write_iops,
972 .plid = BLKIO_POLICY_THROTL,
975 int blk_throtl_bio(struct request_queue *q, struct bio **biop)
977 struct throtl_data *td = q->td;
978 struct throtl_grp *tg;
979 struct bio *bio = *biop;
980 bool rw = bio_data_dir(bio), update_disptime = true;
982 if (bio->bi_rw & REQ_THROTTLED) {
983 bio->bi_rw &= ~REQ_THROTTLED;
987 spin_lock_irq(q->queue_lock);
988 tg = throtl_get_tg(td);
990 if (tg->nr_queued[rw]) {
992 * There is already another bio queued in same dir. No
993 * need to update dispatch time.
995 update_disptime = false;
1000 /* Bio is with-in rate limit of group */
1001 if (tg_may_dispatch(td, tg, bio, NULL)) {
1002 throtl_charge_bio(tg, bio);
1007 throtl_log_tg(td, tg, "[%c] bio. bdisp=%u sz=%u bps=%llu"
1008 " iodisp=%u iops=%u queued=%d/%d",
1009 rw == READ ? 'R' : 'W',
1010 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1011 tg->io_disp[rw], tg->iops[rw],
1012 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1014 throtl_add_bio_tg(q->td, tg, bio);
1017 if (update_disptime) {
1018 tg_update_disptime(td, tg);
1019 throtl_schedule_next_dispatch(td);
1023 spin_unlock_irq(q->queue_lock);
1027 int blk_throtl_init(struct request_queue *q)
1029 struct throtl_data *td;
1030 struct throtl_grp *tg;
1032 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1036 INIT_HLIST_HEAD(&td->tg_list);
1037 td->tg_service_tree = THROTL_RB_ROOT;
1038 td->limits_changed = false;
1040 /* Init root group */
1042 INIT_HLIST_NODE(&tg->tg_node);
1043 RB_CLEAR_NODE(&tg->rb_node);
1044 bio_list_init(&tg->bio_lists[0]);
1045 bio_list_init(&tg->bio_lists[1]);
1047 /* Practically unlimited BW */
1048 tg->bps[0] = tg->bps[1] = -1;
1049 tg->iops[0] = tg->iops[1] = -1;
1050 td->limits_changed = false;
1053 * Set root group reference to 2. One reference will be dropped when
1054 * all groups on tg_list are being deleted during queue exit. Other
1055 * reference will remain there as we don't want to delete this group
1056 * as it is statically allocated and gets destroyed when throtl_data
1059 atomic_set(&tg->ref, 2);
1060 hlist_add_head(&tg->tg_node, &td->tg_list);
1061 td->nr_undestroyed_grps++;
1063 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
1066 blkiocg_add_blkio_group(&blkio_root_cgroup, &tg->blkg, (void *)td,
1067 0, BLKIO_POLICY_THROTL);
1070 /* Attach throtl data to request queue */
1076 void blk_throtl_exit(struct request_queue *q)
1078 struct throtl_data *td = q->td;
1083 throtl_shutdown_wq(q);
1085 spin_lock_irq(q->queue_lock);
1086 throtl_release_tgs(td);
1088 /* If there are other groups */
1089 if (td->nr_undestroyed_grps > 0)
1092 spin_unlock_irq(q->queue_lock);
1095 * Wait for tg->blkg->key accessors to exit their grace periods.
1096 * Do this wait only if there are other undestroyed groups out
1097 * there (other than root group). This can happen if cgroup deletion
1098 * path claimed the responsibility of cleaning up a group before
1099 * queue cleanup code get to the group.
1101 * Do not call synchronize_rcu() unconditionally as there are drivers
1102 * which create/delete request queue hundreds of times during scan/boot
1103 * and synchronize_rcu() can take significant time and slow down boot.
1109 * Just being safe to make sure after previous flush if some body did
1110 * update limits through cgroup and another work got queued, cancel
1113 throtl_shutdown_wq(q);
1117 static int __init throtl_init(void)
1119 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1120 if (!kthrotld_workqueue)
1121 panic("Failed to create kthrotld\n");
1123 blkio_policy_register(&blkio_policy_throtl);
1127 module_init(throtl_init);