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 blkcg_policy blkcg_policy_throtl;
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct *kthrotld_workqueue;
29 struct throtl_rb_root {
33 unsigned long min_disptime;
36 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
37 .count = 0, .min_disptime = 0}
39 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
41 /* Per-cpu group stats */
43 /* total bytes transferred */
44 struct blkg_rwstat service_bytes;
45 /* total IOs serviced, post merge */
46 struct blkg_rwstat serviced;
50 /* must be the first member */
51 struct blkg_policy_data pd;
53 /* active throtl group service_tree member */
54 struct rb_node rb_node;
57 * Dispatch time in jiffies. This is the estimated time when group
58 * will unthrottle and is ready to dispatch more bio. It is used as
59 * key to sort active groups in service tree.
61 unsigned long disptime;
65 /* Two lists for READ and WRITE */
66 struct bio_list bio_lists[2];
68 /* Number of queued bios on READ and WRITE lists */
69 unsigned int nr_queued[2];
71 /* bytes per second rate limits */
77 /* Number of bytes disptached in current slice */
78 uint64_t bytes_disp[2];
79 /* Number of bio's dispatched in current slice */
80 unsigned int io_disp[2];
82 /* When did we start a new slice */
83 unsigned long slice_start[2];
84 unsigned long slice_end[2];
86 /* Per cpu stats pointer */
87 struct tg_stats_cpu __percpu *stats_cpu;
89 /* List of tgs waiting for per cpu stats memory to be allocated */
90 struct list_head stats_alloc_node;
95 /* service tree for active throtl groups */
96 struct throtl_rb_root tg_service_tree;
98 struct request_queue *queue;
100 /* Total Number of queued bios on READ and WRITE lists */
101 unsigned int nr_queued[2];
104 * number of total undestroyed groups
106 unsigned int nr_undestroyed_grps;
108 /* Work for dispatching throttled bios */
109 struct delayed_work dispatch_work;
112 /* list and work item to allocate percpu group stats */
113 static DEFINE_SPINLOCK(tg_stats_alloc_lock);
114 static LIST_HEAD(tg_stats_alloc_list);
116 static void tg_stats_alloc_fn(struct work_struct *);
117 static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
119 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
121 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
124 static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
126 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
129 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
131 return pd_to_blkg(&tg->pd);
134 static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
136 return blkg_to_tg(td->queue->root_blkg);
139 enum tg_state_flags {
140 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
143 #define THROTL_TG_FNS(name) \
144 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
146 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
148 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
150 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
152 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
154 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
157 THROTL_TG_FNS(on_rr);
159 #define throtl_log_tg(td, tg, fmt, args...) do { \
162 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
163 blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
166 #define throtl_log(td, fmt, args...) \
167 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
169 static inline unsigned int total_nr_queued(struct throtl_data *td)
171 return td->nr_queued[0] + td->nr_queued[1];
175 * Worker for allocating per cpu stat for tgs. This is scheduled on the
176 * system_wq once there are some groups on the alloc_list waiting for
179 static void tg_stats_alloc_fn(struct work_struct *work)
181 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
182 struct delayed_work *dwork = to_delayed_work(work);
187 stats_cpu = alloc_percpu(struct tg_stats_cpu);
189 /* allocation failed, try again after some time */
190 schedule_delayed_work(dwork, msecs_to_jiffies(10));
195 spin_lock_irq(&tg_stats_alloc_lock);
197 if (!list_empty(&tg_stats_alloc_list)) {
198 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
201 swap(tg->stats_cpu, stats_cpu);
202 list_del_init(&tg->stats_alloc_node);
205 empty = list_empty(&tg_stats_alloc_list);
206 spin_unlock_irq(&tg_stats_alloc_lock);
211 static void throtl_pd_init(struct blkcg_gq *blkg)
213 struct throtl_grp *tg = blkg_to_tg(blkg);
216 RB_CLEAR_NODE(&tg->rb_node);
217 bio_list_init(&tg->bio_lists[0]);
218 bio_list_init(&tg->bio_lists[1]);
223 tg->iops[WRITE] = -1;
226 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
227 * but percpu allocator can't be called from IO path. Queue tg on
228 * tg_stats_alloc_list and allocate from work item.
230 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
231 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
232 schedule_delayed_work(&tg_stats_alloc_work, 0);
233 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
236 static void throtl_pd_exit(struct blkcg_gq *blkg)
238 struct throtl_grp *tg = blkg_to_tg(blkg);
241 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
242 list_del_init(&tg->stats_alloc_node);
243 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
245 free_percpu(tg->stats_cpu);
248 static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
250 struct throtl_grp *tg = blkg_to_tg(blkg);
253 if (tg->stats_cpu == NULL)
256 for_each_possible_cpu(cpu) {
257 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
259 blkg_rwstat_reset(&sc->service_bytes);
260 blkg_rwstat_reset(&sc->serviced);
264 static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
268 * This is the common case when there are no blkcgs. Avoid lookup
271 if (blkcg == &blkcg_root)
272 return td_root_tg(td);
274 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
277 static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
280 struct request_queue *q = td->queue;
281 struct throtl_grp *tg = NULL;
284 * This is the common case when there are no blkcgs. Avoid lookup
287 if (blkcg == &blkcg_root) {
290 struct blkcg_gq *blkg;
292 blkg = blkg_lookup_create(blkcg, q);
294 /* if %NULL and @q is alive, fall back to root_tg */
296 tg = blkg_to_tg(blkg);
297 else if (!blk_queue_dying(q))
304 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
306 /* Service tree is empty */
311 root->left = rb_first(&root->rb);
314 return rb_entry_tg(root->left);
319 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
325 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
329 rb_erase_init(n, &root->rb);
333 static void update_min_dispatch_time(struct throtl_rb_root *st)
335 struct throtl_grp *tg;
337 tg = throtl_rb_first(st);
341 st->min_disptime = tg->disptime;
345 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
347 struct rb_node **node = &st->rb.rb_node;
348 struct rb_node *parent = NULL;
349 struct throtl_grp *__tg;
350 unsigned long key = tg->disptime;
353 while (*node != NULL) {
355 __tg = rb_entry_tg(parent);
357 if (time_before(key, __tg->disptime))
358 node = &parent->rb_left;
360 node = &parent->rb_right;
366 st->left = &tg->rb_node;
368 rb_link_node(&tg->rb_node, parent, node);
369 rb_insert_color(&tg->rb_node, &st->rb);
372 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
374 struct throtl_rb_root *st = &td->tg_service_tree;
376 tg_service_tree_add(st, tg);
377 throtl_mark_tg_on_rr(tg);
381 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
383 if (!throtl_tg_on_rr(tg))
384 __throtl_enqueue_tg(td, tg);
387 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
389 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
390 throtl_clear_tg_on_rr(tg);
393 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
395 if (throtl_tg_on_rr(tg))
396 __throtl_dequeue_tg(td, tg);
399 /* Call with queue lock held */
400 static void throtl_schedule_delayed_work(struct throtl_data *td,
403 struct delayed_work *dwork = &td->dispatch_work;
405 if (total_nr_queued(td)) {
406 mod_delayed_work(kthrotld_workqueue, dwork, delay);
407 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
412 static void throtl_schedule_next_dispatch(struct throtl_data *td)
414 struct throtl_rb_root *st = &td->tg_service_tree;
417 * If there are more bios pending, schedule more work.
419 if (!total_nr_queued(td))
424 update_min_dispatch_time(st);
426 if (time_before_eq(st->min_disptime, jiffies))
427 throtl_schedule_delayed_work(td, 0);
429 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
433 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
435 tg->bytes_disp[rw] = 0;
437 tg->slice_start[rw] = jiffies;
438 tg->slice_end[rw] = jiffies + throtl_slice;
439 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
440 rw == READ ? 'R' : 'W', tg->slice_start[rw],
441 tg->slice_end[rw], jiffies);
444 static inline void throtl_set_slice_end(struct throtl_data *td,
445 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
447 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
450 static inline void throtl_extend_slice(struct throtl_data *td,
451 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
453 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
454 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
455 rw == READ ? 'R' : 'W', tg->slice_start[rw],
456 tg->slice_end[rw], jiffies);
459 /* Determine if previously allocated or extended slice is complete or not */
461 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
463 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
469 /* Trim the used slices and adjust slice start accordingly */
471 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
473 unsigned long nr_slices, time_elapsed, io_trim;
476 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
479 * If bps are unlimited (-1), then time slice don't get
480 * renewed. Don't try to trim the slice if slice is used. A new
481 * slice will start when appropriate.
483 if (throtl_slice_used(td, tg, rw))
487 * A bio has been dispatched. Also adjust slice_end. It might happen
488 * that initially cgroup limit was very low resulting in high
489 * slice_end, but later limit was bumped up and bio was dispached
490 * sooner, then we need to reduce slice_end. A high bogus slice_end
491 * is bad because it does not allow new slice to start.
494 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
496 time_elapsed = jiffies - tg->slice_start[rw];
498 nr_slices = time_elapsed / throtl_slice;
502 tmp = tg->bps[rw] * throtl_slice * nr_slices;
506 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
508 if (!bytes_trim && !io_trim)
511 if (tg->bytes_disp[rw] >= bytes_trim)
512 tg->bytes_disp[rw] -= bytes_trim;
514 tg->bytes_disp[rw] = 0;
516 if (tg->io_disp[rw] >= io_trim)
517 tg->io_disp[rw] -= io_trim;
521 tg->slice_start[rw] += nr_slices * throtl_slice;
523 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
524 " start=%lu end=%lu jiffies=%lu",
525 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
526 tg->slice_start[rw], tg->slice_end[rw], jiffies);
529 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
530 struct bio *bio, unsigned long *wait)
532 bool rw = bio_data_dir(bio);
533 unsigned int io_allowed;
534 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
537 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
539 /* Slice has just started. Consider one slice interval */
541 jiffy_elapsed_rnd = throtl_slice;
543 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
546 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
547 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
548 * will allow dispatch after 1 second and after that slice should
552 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
556 io_allowed = UINT_MAX;
560 if (tg->io_disp[rw] + 1 <= io_allowed) {
566 /* Calc approx time to dispatch */
567 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
569 if (jiffy_wait > jiffy_elapsed)
570 jiffy_wait = jiffy_wait - jiffy_elapsed;
579 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
580 struct bio *bio, unsigned long *wait)
582 bool rw = bio_data_dir(bio);
583 u64 bytes_allowed, extra_bytes, tmp;
584 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
586 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
588 /* Slice has just started. Consider one slice interval */
590 jiffy_elapsed_rnd = throtl_slice;
592 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
594 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
598 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
604 /* Calc approx time to dispatch */
605 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
606 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
612 * This wait time is without taking into consideration the rounding
613 * up we did. Add that time also.
615 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
621 static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
622 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
628 * Returns whether one can dispatch a bio or not. Also returns approx number
629 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
631 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
632 struct bio *bio, unsigned long *wait)
634 bool rw = bio_data_dir(bio);
635 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
638 * Currently whole state machine of group depends on first bio
639 * queued in the group bio list. So one should not be calling
640 * this function with a different bio if there are other bios
643 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
645 /* If tg->bps = -1, then BW is unlimited */
646 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
653 * If previous slice expired, start a new one otherwise renew/extend
654 * existing slice to make sure it is at least throtl_slice interval
657 if (throtl_slice_used(td, tg, rw))
658 throtl_start_new_slice(td, tg, rw);
660 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
661 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
664 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
665 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
671 max_wait = max(bps_wait, iops_wait);
676 if (time_before(tg->slice_end[rw], jiffies + max_wait))
677 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
682 static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
685 struct throtl_grp *tg = blkg_to_tg(blkg);
686 struct tg_stats_cpu *stats_cpu;
689 /* If per cpu stats are not allocated yet, don't do any accounting. */
690 if (tg->stats_cpu == NULL)
694 * Disabling interrupts to provide mutual exclusion between two
695 * writes on same cpu. It probably is not needed for 64bit. Not
696 * optimizing that case yet.
698 local_irq_save(flags);
700 stats_cpu = this_cpu_ptr(tg->stats_cpu);
702 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
703 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
705 local_irq_restore(flags);
708 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
710 bool rw = bio_data_dir(bio);
712 /* Charge the bio to the group */
713 tg->bytes_disp[rw] += bio->bi_size;
716 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
719 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
722 bool rw = bio_data_dir(bio);
724 bio_list_add(&tg->bio_lists[rw], bio);
725 /* Take a bio reference on tg */
726 blkg_get(tg_to_blkg(tg));
729 throtl_enqueue_tg(td, tg);
732 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
734 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
737 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
738 tg_may_dispatch(td, tg, bio, &read_wait);
740 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
741 tg_may_dispatch(td, tg, bio, &write_wait);
743 min_wait = min(read_wait, write_wait);
744 disptime = jiffies + min_wait;
746 /* Update dispatch time */
747 throtl_dequeue_tg(td, tg);
748 tg->disptime = disptime;
749 throtl_enqueue_tg(td, tg);
752 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
753 bool rw, struct bio_list *bl)
757 bio = bio_list_pop(&tg->bio_lists[rw]);
759 /* Drop bio reference on blkg */
760 blkg_put(tg_to_blkg(tg));
762 BUG_ON(td->nr_queued[rw] <= 0);
765 throtl_charge_bio(tg, bio);
766 bio_list_add(bl, bio);
767 bio->bi_rw |= REQ_THROTTLED;
769 throtl_trim_slice(td, tg, rw);
772 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
775 unsigned int nr_reads = 0, nr_writes = 0;
776 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
777 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
780 /* Try to dispatch 75% READS and 25% WRITES */
782 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
783 && tg_may_dispatch(td, tg, bio, NULL)) {
785 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
788 if (nr_reads >= max_nr_reads)
792 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
793 && tg_may_dispatch(td, tg, bio, NULL)) {
795 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
798 if (nr_writes >= max_nr_writes)
802 return nr_reads + nr_writes;
805 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
807 unsigned int nr_disp = 0;
808 struct throtl_grp *tg;
809 struct throtl_rb_root *st = &td->tg_service_tree;
812 tg = throtl_rb_first(st);
817 if (time_before(jiffies, tg->disptime))
820 throtl_dequeue_tg(td, tg);
822 nr_disp += throtl_dispatch_tg(td, tg, bl);
824 if (tg->nr_queued[0] || tg->nr_queued[1])
825 tg_update_disptime(td, tg);
827 if (nr_disp >= throtl_quantum)
834 /* work function to dispatch throttled bios */
835 void blk_throtl_dispatch_work_fn(struct work_struct *work)
837 struct throtl_data *td = container_of(to_delayed_work(work),
838 struct throtl_data, dispatch_work);
839 struct request_queue *q = td->queue;
840 unsigned int nr_disp = 0;
841 struct bio_list bio_list_on_stack;
843 struct blk_plug plug;
845 spin_lock_irq(q->queue_lock);
847 if (!total_nr_queued(td))
850 bio_list_init(&bio_list_on_stack);
852 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
853 total_nr_queued(td), td->nr_queued[READ],
854 td->nr_queued[WRITE]);
856 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
859 throtl_log(td, "bios disp=%u", nr_disp);
861 throtl_schedule_next_dispatch(td);
863 spin_unlock_irq(q->queue_lock);
866 * If we dispatched some requests, unplug the queue to make sure
870 blk_start_plug(&plug);
871 while((bio = bio_list_pop(&bio_list_on_stack)))
872 generic_make_request(bio);
873 blk_finish_plug(&plug);
877 static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
878 struct blkg_policy_data *pd, int off)
880 struct throtl_grp *tg = pd_to_tg(pd);
881 struct blkg_rwstat rwstat = { }, tmp;
884 for_each_possible_cpu(cpu) {
885 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
887 tmp = blkg_rwstat_read((void *)sc + off);
888 for (i = 0; i < BLKG_RWSTAT_NR; i++)
889 rwstat.cnt[i] += tmp.cnt[i];
892 return __blkg_prfill_rwstat(sf, pd, &rwstat);
895 static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
898 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
900 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
905 static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
908 struct throtl_grp *tg = pd_to_tg(pd);
909 u64 v = *(u64 *)((void *)tg + off);
913 return __blkg_prfill_u64(sf, pd, v);
916 static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
919 struct throtl_grp *tg = pd_to_tg(pd);
920 unsigned int v = *(unsigned int *)((void *)tg + off);
924 return __blkg_prfill_u64(sf, pd, v);
927 static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
930 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
931 &blkcg_policy_throtl, cft->private, false);
935 static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
938 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
939 &blkcg_policy_throtl, cft->private, false);
943 static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
946 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
947 struct blkg_conf_ctx ctx;
948 struct throtl_grp *tg;
949 struct throtl_data *td;
952 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
956 tg = blkg_to_tg(ctx.blkg);
957 td = ctx.blkg->q->td;
963 *(u64 *)((void *)tg + cft->private) = ctx.v;
965 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
967 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
968 tg->bps[READ], tg->bps[WRITE],
969 tg->iops[READ], tg->iops[WRITE]);
972 * We're already holding queue_lock and know @tg is valid. Let's
973 * apply the new config directly.
975 * Restart the slices for both READ and WRITES. It might happen
976 * that a group's limit are dropped suddenly and we don't want to
977 * account recently dispatched IO with new low rate.
979 throtl_start_new_slice(td, tg, 0);
980 throtl_start_new_slice(td, tg, 1);
982 if (throtl_tg_on_rr(tg)) {
983 tg_update_disptime(td, tg);
984 throtl_schedule_next_dispatch(td);
987 blkg_conf_finish(&ctx);
991 static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
994 return tg_set_conf(cgrp, cft, buf, true);
997 static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1000 return tg_set_conf(cgrp, cft, buf, false);
1003 static struct cftype throtl_files[] = {
1005 .name = "throttle.read_bps_device",
1006 .private = offsetof(struct throtl_grp, bps[READ]),
1007 .read_seq_string = tg_print_conf_u64,
1008 .write_string = tg_set_conf_u64,
1009 .max_write_len = 256,
1012 .name = "throttle.write_bps_device",
1013 .private = offsetof(struct throtl_grp, bps[WRITE]),
1014 .read_seq_string = tg_print_conf_u64,
1015 .write_string = tg_set_conf_u64,
1016 .max_write_len = 256,
1019 .name = "throttle.read_iops_device",
1020 .private = offsetof(struct throtl_grp, iops[READ]),
1021 .read_seq_string = tg_print_conf_uint,
1022 .write_string = tg_set_conf_uint,
1023 .max_write_len = 256,
1026 .name = "throttle.write_iops_device",
1027 .private = offsetof(struct throtl_grp, iops[WRITE]),
1028 .read_seq_string = tg_print_conf_uint,
1029 .write_string = tg_set_conf_uint,
1030 .max_write_len = 256,
1033 .name = "throttle.io_service_bytes",
1034 .private = offsetof(struct tg_stats_cpu, service_bytes),
1035 .read_seq_string = tg_print_cpu_rwstat,
1038 .name = "throttle.io_serviced",
1039 .private = offsetof(struct tg_stats_cpu, serviced),
1040 .read_seq_string = tg_print_cpu_rwstat,
1045 static void throtl_shutdown_wq(struct request_queue *q)
1047 struct throtl_data *td = q->td;
1049 cancel_delayed_work_sync(&td->dispatch_work);
1052 static struct blkcg_policy blkcg_policy_throtl = {
1053 .pd_size = sizeof(struct throtl_grp),
1054 .cftypes = throtl_files,
1056 .pd_init_fn = throtl_pd_init,
1057 .pd_exit_fn = throtl_pd_exit,
1058 .pd_reset_stats_fn = throtl_pd_reset_stats,
1061 bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1063 struct throtl_data *td = q->td;
1064 struct throtl_grp *tg;
1065 bool rw = bio_data_dir(bio), update_disptime = true;
1066 struct blkcg *blkcg;
1067 bool throttled = false;
1069 if (bio->bi_rw & REQ_THROTTLED) {
1070 bio->bi_rw &= ~REQ_THROTTLED;
1075 * A throtl_grp pointer retrieved under rcu can be used to access
1076 * basic fields like stats and io rates. If a group has no rules,
1077 * just update the dispatch stats in lockless manner and return.
1080 blkcg = bio_blkcg(bio);
1081 tg = throtl_lookup_tg(td, blkcg);
1083 if (tg_no_rule_group(tg, rw)) {
1084 throtl_update_dispatch_stats(tg_to_blkg(tg),
1085 bio->bi_size, bio->bi_rw);
1086 goto out_unlock_rcu;
1091 * Either group has not been allocated yet or it is not an unlimited
1094 spin_lock_irq(q->queue_lock);
1095 tg = throtl_lookup_create_tg(td, blkcg);
1099 if (tg->nr_queued[rw]) {
1101 * There is already another bio queued in same dir. No
1102 * need to update dispatch time.
1104 update_disptime = false;
1109 /* Bio is with-in rate limit of group */
1110 if (tg_may_dispatch(td, tg, bio, NULL)) {
1111 throtl_charge_bio(tg, bio);
1114 * We need to trim slice even when bios are not being queued
1115 * otherwise it might happen that a bio is not queued for
1116 * a long time and slice keeps on extending and trim is not
1117 * called for a long time. Now if limits are reduced suddenly
1118 * we take into account all the IO dispatched so far at new
1119 * low rate and * newly queued IO gets a really long dispatch
1122 * So keep on trimming slice even if bio is not queued.
1124 throtl_trim_slice(td, tg, rw);
1129 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1130 " iodisp=%u iops=%u queued=%d/%d",
1131 rw == READ ? 'R' : 'W',
1132 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1133 tg->io_disp[rw], tg->iops[rw],
1134 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1136 bio_associate_current(bio);
1137 throtl_add_bio_tg(q->td, tg, bio);
1140 if (update_disptime) {
1141 tg_update_disptime(td, tg);
1142 throtl_schedule_next_dispatch(td);
1146 spin_unlock_irq(q->queue_lock);
1154 * blk_throtl_drain - drain throttled bios
1155 * @q: request_queue to drain throttled bios for
1157 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1159 void blk_throtl_drain(struct request_queue *q)
1160 __releases(q->queue_lock) __acquires(q->queue_lock)
1162 struct throtl_data *td = q->td;
1163 struct throtl_rb_root *st = &td->tg_service_tree;
1164 struct throtl_grp *tg;
1168 queue_lockdep_assert_held(q);
1172 while ((tg = throtl_rb_first(st))) {
1173 throtl_dequeue_tg(td, tg);
1175 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1176 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1177 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1178 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1180 spin_unlock_irq(q->queue_lock);
1182 while ((bio = bio_list_pop(&bl)))
1183 generic_make_request(bio);
1185 spin_lock_irq(q->queue_lock);
1188 int blk_throtl_init(struct request_queue *q)
1190 struct throtl_data *td;
1193 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1197 td->tg_service_tree = THROTL_RB_ROOT;
1198 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1203 /* activate policy */
1204 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1210 void blk_throtl_exit(struct request_queue *q)
1213 throtl_shutdown_wq(q);
1214 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1218 static int __init throtl_init(void)
1220 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1221 if (!kthrotld_workqueue)
1222 panic("Failed to create kthrotld\n");
1224 return blkcg_policy_register(&blkcg_policy_throtl);
1227 module_init(throtl_init);