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;
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)
43 /* Per-cpu group stats */
45 /* total bytes transferred */
46 struct blkg_rwstat service_bytes;
47 /* total IOs serviced, post merge */
48 struct blkg_rwstat serviced;
52 /* must be the first member */
53 struct blkg_policy_data pd;
55 /* active throtl group service_tree member */
56 struct rb_node rb_node;
59 * Dispatch time in jiffies. This is the estimated time when group
60 * will unthrottle and is ready to dispatch more bio. It is used as
61 * key to sort active groups in service tree.
63 unsigned long disptime;
67 /* Two lists for READ and WRITE */
68 struct bio_list bio_lists[2];
70 /* Number of queued bios on READ and WRITE lists */
71 unsigned int nr_queued[2];
73 /* bytes per second rate limits */
79 /* Number of bytes disptached in current slice */
80 uint64_t bytes_disp[2];
81 /* Number of bio's dispatched in current slice */
82 unsigned int io_disp[2];
84 /* When did we start a new slice */
85 unsigned long slice_start[2];
86 unsigned long slice_end[2];
88 /* Per cpu stats pointer */
89 struct tg_stats_cpu __percpu *stats_cpu;
91 /* List of tgs waiting for per cpu stats memory to be allocated */
92 struct list_head stats_alloc_node;
97 /* service tree for active throtl groups */
98 struct throtl_rb_root tg_service_tree;
100 struct request_queue *queue;
102 /* Total Number of queued bios on READ and WRITE lists */
103 unsigned int nr_queued[2];
106 * number of total undestroyed groups
108 unsigned int nr_undestroyed_grps;
110 /* Work for dispatching throttled bios */
111 struct delayed_work dispatch_work;
114 /* list and work item to allocate percpu group stats */
115 static DEFINE_SPINLOCK(tg_stats_alloc_lock);
116 static LIST_HEAD(tg_stats_alloc_list);
118 static void tg_stats_alloc_fn(struct work_struct *);
119 static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
121 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
123 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
126 static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
128 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
131 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
133 return pd_to_blkg(&tg->pd);
136 static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
138 return blkg_to_tg(td->queue->root_blkg);
141 enum tg_state_flags {
142 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
145 #define THROTL_TG_FNS(name) \
146 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
148 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
150 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
152 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
154 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
156 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
159 THROTL_TG_FNS(on_rr);
161 #define throtl_log_tg(td, tg, fmt, args...) do { \
164 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
165 blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
168 #define throtl_log(td, fmt, args...) \
169 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
171 static inline unsigned int total_nr_queued(struct throtl_data *td)
173 return td->nr_queued[0] + td->nr_queued[1];
177 * Worker for allocating per cpu stat for tgs. This is scheduled on the
178 * system_wq once there are some groups on the alloc_list waiting for
181 static void tg_stats_alloc_fn(struct work_struct *work)
183 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
184 struct delayed_work *dwork = to_delayed_work(work);
189 stats_cpu = alloc_percpu(struct tg_stats_cpu);
191 /* allocation failed, try again after some time */
192 schedule_delayed_work(dwork, msecs_to_jiffies(10));
197 spin_lock_irq(&tg_stats_alloc_lock);
199 if (!list_empty(&tg_stats_alloc_list)) {
200 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
203 swap(tg->stats_cpu, stats_cpu);
204 list_del_init(&tg->stats_alloc_node);
207 empty = list_empty(&tg_stats_alloc_list);
208 spin_unlock_irq(&tg_stats_alloc_lock);
213 static void throtl_pd_init(struct blkcg_gq *blkg)
215 struct throtl_grp *tg = blkg_to_tg(blkg);
218 RB_CLEAR_NODE(&tg->rb_node);
219 bio_list_init(&tg->bio_lists[0]);
220 bio_list_init(&tg->bio_lists[1]);
225 tg->iops[WRITE] = -1;
228 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
229 * but percpu allocator can't be called from IO path. Queue tg on
230 * tg_stats_alloc_list and allocate from work item.
232 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
233 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
234 schedule_delayed_work(&tg_stats_alloc_work, 0);
235 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
238 static void throtl_pd_exit(struct blkcg_gq *blkg)
240 struct throtl_grp *tg = blkg_to_tg(blkg);
243 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
244 list_del_init(&tg->stats_alloc_node);
245 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
247 free_percpu(tg->stats_cpu);
250 static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
252 struct throtl_grp *tg = blkg_to_tg(blkg);
255 if (tg->stats_cpu == NULL)
258 for_each_possible_cpu(cpu) {
259 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
261 blkg_rwstat_reset(&sc->service_bytes);
262 blkg_rwstat_reset(&sc->serviced);
266 static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
270 * This is the common case when there are no blkcgs. Avoid lookup
273 if (blkcg == &blkcg_root)
274 return td_root_tg(td);
276 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
279 static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
282 struct request_queue *q = td->queue;
283 struct throtl_grp *tg = NULL;
286 * This is the common case when there are no blkcgs. Avoid lookup
289 if (blkcg == &blkcg_root) {
292 struct blkcg_gq *blkg;
294 blkg = blkg_lookup_create(blkcg, q);
296 /* if %NULL and @q is alive, fall back to root_tg */
298 tg = blkg_to_tg(blkg);
299 else if (!blk_queue_dying(q))
306 static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
308 /* Service tree is empty */
313 root->left = rb_first(&root->rb);
316 return rb_entry_tg(root->left);
321 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
327 static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
331 rb_erase_init(n, &root->rb);
335 static void update_min_dispatch_time(struct throtl_rb_root *st)
337 struct throtl_grp *tg;
339 tg = throtl_rb_first(st);
343 st->min_disptime = tg->disptime;
347 tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
349 struct rb_node **node = &st->rb.rb_node;
350 struct rb_node *parent = NULL;
351 struct throtl_grp *__tg;
352 unsigned long key = tg->disptime;
355 while (*node != NULL) {
357 __tg = rb_entry_tg(parent);
359 if (time_before(key, __tg->disptime))
360 node = &parent->rb_left;
362 node = &parent->rb_right;
368 st->left = &tg->rb_node;
370 rb_link_node(&tg->rb_node, parent, node);
371 rb_insert_color(&tg->rb_node, &st->rb);
374 static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
376 struct throtl_rb_root *st = &td->tg_service_tree;
378 tg_service_tree_add(st, tg);
379 throtl_mark_tg_on_rr(tg);
383 static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
385 if (!throtl_tg_on_rr(tg))
386 __throtl_enqueue_tg(td, tg);
389 static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
391 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
392 throtl_clear_tg_on_rr(tg);
395 static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
397 if (throtl_tg_on_rr(tg))
398 __throtl_dequeue_tg(td, tg);
401 static void throtl_schedule_next_dispatch(struct throtl_data *td)
403 struct throtl_rb_root *st = &td->tg_service_tree;
406 * If there are more bios pending, schedule more work.
408 if (!total_nr_queued(td))
413 update_min_dispatch_time(st);
415 if (time_before_eq(st->min_disptime, jiffies))
416 throtl_schedule_delayed_work(td, 0);
418 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
422 throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
424 tg->bytes_disp[rw] = 0;
426 tg->slice_start[rw] = jiffies;
427 tg->slice_end[rw] = jiffies + throtl_slice;
428 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
429 rw == READ ? 'R' : 'W', tg->slice_start[rw],
430 tg->slice_end[rw], jiffies);
433 static inline void throtl_set_slice_end(struct throtl_data *td,
434 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
436 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
439 static inline void throtl_extend_slice(struct throtl_data *td,
440 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
442 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
443 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
444 rw == READ ? 'R' : 'W', tg->slice_start[rw],
445 tg->slice_end[rw], jiffies);
448 /* Determine if previously allocated or extended slice is complete or not */
450 throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
452 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
458 /* Trim the used slices and adjust slice start accordingly */
460 throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
462 unsigned long nr_slices, time_elapsed, io_trim;
465 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
468 * If bps are unlimited (-1), then time slice don't get
469 * renewed. Don't try to trim the slice if slice is used. A new
470 * slice will start when appropriate.
472 if (throtl_slice_used(td, tg, rw))
476 * A bio has been dispatched. Also adjust slice_end. It might happen
477 * that initially cgroup limit was very low resulting in high
478 * slice_end, but later limit was bumped up and bio was dispached
479 * sooner, then we need to reduce slice_end. A high bogus slice_end
480 * is bad because it does not allow new slice to start.
483 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
485 time_elapsed = jiffies - tg->slice_start[rw];
487 nr_slices = time_elapsed / throtl_slice;
491 tmp = tg->bps[rw] * throtl_slice * nr_slices;
495 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
497 if (!bytes_trim && !io_trim)
500 if (tg->bytes_disp[rw] >= bytes_trim)
501 tg->bytes_disp[rw] -= bytes_trim;
503 tg->bytes_disp[rw] = 0;
505 if (tg->io_disp[rw] >= io_trim)
506 tg->io_disp[rw] -= io_trim;
510 tg->slice_start[rw] += nr_slices * throtl_slice;
512 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
513 " start=%lu end=%lu jiffies=%lu",
514 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
515 tg->slice_start[rw], tg->slice_end[rw], jiffies);
518 static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
519 struct bio *bio, unsigned long *wait)
521 bool rw = bio_data_dir(bio);
522 unsigned int io_allowed;
523 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
526 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
528 /* Slice has just started. Consider one slice interval */
530 jiffy_elapsed_rnd = throtl_slice;
532 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
535 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
536 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
537 * will allow dispatch after 1 second and after that slice should
541 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
545 io_allowed = UINT_MAX;
549 if (tg->io_disp[rw] + 1 <= io_allowed) {
555 /* Calc approx time to dispatch */
556 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
558 if (jiffy_wait > jiffy_elapsed)
559 jiffy_wait = jiffy_wait - jiffy_elapsed;
568 static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
569 struct bio *bio, unsigned long *wait)
571 bool rw = bio_data_dir(bio);
572 u64 bytes_allowed, extra_bytes, tmp;
573 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
575 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
577 /* Slice has just started. Consider one slice interval */
579 jiffy_elapsed_rnd = throtl_slice;
581 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
583 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
587 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
593 /* Calc approx time to dispatch */
594 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
595 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
601 * This wait time is without taking into consideration the rounding
602 * up we did. Add that time also.
604 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
610 static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
611 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
617 * Returns whether one can dispatch a bio or not. Also returns approx number
618 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
620 static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
621 struct bio *bio, unsigned long *wait)
623 bool rw = bio_data_dir(bio);
624 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
627 * Currently whole state machine of group depends on first bio
628 * queued in the group bio list. So one should not be calling
629 * this function with a different bio if there are other bios
632 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
634 /* If tg->bps = -1, then BW is unlimited */
635 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
642 * If previous slice expired, start a new one otherwise renew/extend
643 * existing slice to make sure it is at least throtl_slice interval
646 if (throtl_slice_used(td, tg, rw))
647 throtl_start_new_slice(td, tg, rw);
649 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
650 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
653 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
654 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
660 max_wait = max(bps_wait, iops_wait);
665 if (time_before(tg->slice_end[rw], jiffies + max_wait))
666 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
671 static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
674 struct throtl_grp *tg = blkg_to_tg(blkg);
675 struct tg_stats_cpu *stats_cpu;
678 /* If per cpu stats are not allocated yet, don't do any accounting. */
679 if (tg->stats_cpu == NULL)
683 * Disabling interrupts to provide mutual exclusion between two
684 * writes on same cpu. It probably is not needed for 64bit. Not
685 * optimizing that case yet.
687 local_irq_save(flags);
689 stats_cpu = this_cpu_ptr(tg->stats_cpu);
691 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
692 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
694 local_irq_restore(flags);
697 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
699 bool rw = bio_data_dir(bio);
701 /* Charge the bio to the group */
702 tg->bytes_disp[rw] += bio->bi_size;
705 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
708 static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
711 bool rw = bio_data_dir(bio);
713 bio_list_add(&tg->bio_lists[rw], bio);
714 /* Take a bio reference on tg */
715 blkg_get(tg_to_blkg(tg));
718 throtl_enqueue_tg(td, tg);
721 static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
723 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
726 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
727 tg_may_dispatch(td, tg, bio, &read_wait);
729 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
730 tg_may_dispatch(td, tg, bio, &write_wait);
732 min_wait = min(read_wait, write_wait);
733 disptime = jiffies + min_wait;
735 /* Update dispatch time */
736 throtl_dequeue_tg(td, tg);
737 tg->disptime = disptime;
738 throtl_enqueue_tg(td, tg);
741 static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
742 bool rw, struct bio_list *bl)
746 bio = bio_list_pop(&tg->bio_lists[rw]);
748 /* Drop bio reference on blkg */
749 blkg_put(tg_to_blkg(tg));
751 BUG_ON(td->nr_queued[rw] <= 0);
754 throtl_charge_bio(tg, bio);
755 bio_list_add(bl, bio);
756 bio->bi_rw |= REQ_THROTTLED;
758 throtl_trim_slice(td, tg, rw);
761 static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
764 unsigned int nr_reads = 0, nr_writes = 0;
765 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
766 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
769 /* Try to dispatch 75% READS and 25% WRITES */
771 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
772 && tg_may_dispatch(td, tg, bio, NULL)) {
774 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
777 if (nr_reads >= max_nr_reads)
781 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
782 && tg_may_dispatch(td, tg, bio, NULL)) {
784 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
787 if (nr_writes >= max_nr_writes)
791 return nr_reads + nr_writes;
794 static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
796 unsigned int nr_disp = 0;
797 struct throtl_grp *tg;
798 struct throtl_rb_root *st = &td->tg_service_tree;
801 tg = throtl_rb_first(st);
806 if (time_before(jiffies, tg->disptime))
809 throtl_dequeue_tg(td, tg);
811 nr_disp += throtl_dispatch_tg(td, tg, bl);
813 if (tg->nr_queued[0] || tg->nr_queued[1])
814 tg_update_disptime(td, tg);
816 if (nr_disp >= throtl_quantum)
823 /* work function to dispatch throttled bios */
824 void blk_throtl_dispatch_work_fn(struct work_struct *work)
826 struct throtl_data *td = container_of(to_delayed_work(work),
827 struct throtl_data, dispatch_work);
828 struct request_queue *q = td->queue;
829 unsigned int nr_disp = 0;
830 struct bio_list bio_list_on_stack;
832 struct blk_plug plug;
834 spin_lock_irq(q->queue_lock);
836 if (!total_nr_queued(td))
839 bio_list_init(&bio_list_on_stack);
841 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
842 total_nr_queued(td), td->nr_queued[READ],
843 td->nr_queued[WRITE]);
845 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
848 throtl_log(td, "bios disp=%u", nr_disp);
850 throtl_schedule_next_dispatch(td);
852 spin_unlock_irq(q->queue_lock);
855 * If we dispatched some requests, unplug the queue to make sure
859 blk_start_plug(&plug);
860 while((bio = bio_list_pop(&bio_list_on_stack)))
861 generic_make_request(bio);
862 blk_finish_plug(&plug);
866 /* Call with queue lock held */
868 throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
871 struct delayed_work *dwork = &td->dispatch_work;
873 if (total_nr_queued(td)) {
874 mod_delayed_work(kthrotld_workqueue, dwork, delay);
875 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
880 static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
881 struct blkg_policy_data *pd, int off)
883 struct throtl_grp *tg = pd_to_tg(pd);
884 struct blkg_rwstat rwstat = { }, tmp;
887 for_each_possible_cpu(cpu) {
888 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
890 tmp = blkg_rwstat_read((void *)sc + off);
891 for (i = 0; i < BLKG_RWSTAT_NR; i++)
892 rwstat.cnt[i] += tmp.cnt[i];
895 return __blkg_prfill_rwstat(sf, pd, &rwstat);
898 static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
901 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
903 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
908 static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
911 struct throtl_grp *tg = pd_to_tg(pd);
912 u64 v = *(u64 *)((void *)tg + off);
916 return __blkg_prfill_u64(sf, pd, v);
919 static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
922 struct throtl_grp *tg = pd_to_tg(pd);
923 unsigned int v = *(unsigned int *)((void *)tg + off);
927 return __blkg_prfill_u64(sf, pd, v);
930 static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
933 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
934 &blkcg_policy_throtl, cft->private, false);
938 static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
941 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
942 &blkcg_policy_throtl, cft->private, false);
946 static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
949 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
950 struct blkg_conf_ctx ctx;
951 struct throtl_grp *tg;
952 struct throtl_data *td;
955 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
959 tg = blkg_to_tg(ctx.blkg);
960 td = ctx.blkg->q->td;
966 *(u64 *)((void *)tg + cft->private) = ctx.v;
968 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
970 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
971 tg->bps[READ], tg->bps[WRITE],
972 tg->iops[READ], tg->iops[WRITE]);
975 * We're already holding queue_lock and know @tg is valid. Let's
976 * apply the new config directly.
978 * Restart the slices for both READ and WRITES. It might happen
979 * that a group's limit are dropped suddenly and we don't want to
980 * account recently dispatched IO with new low rate.
982 throtl_start_new_slice(td, tg, 0);
983 throtl_start_new_slice(td, tg, 1);
985 if (throtl_tg_on_rr(tg)) {
986 tg_update_disptime(td, tg);
987 throtl_schedule_next_dispatch(td);
990 blkg_conf_finish(&ctx);
994 static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
997 return tg_set_conf(cgrp, cft, buf, true);
1000 static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1003 return tg_set_conf(cgrp, cft, buf, false);
1006 static struct cftype throtl_files[] = {
1008 .name = "throttle.read_bps_device",
1009 .private = offsetof(struct throtl_grp, bps[READ]),
1010 .read_seq_string = tg_print_conf_u64,
1011 .write_string = tg_set_conf_u64,
1012 .max_write_len = 256,
1015 .name = "throttle.write_bps_device",
1016 .private = offsetof(struct throtl_grp, bps[WRITE]),
1017 .read_seq_string = tg_print_conf_u64,
1018 .write_string = tg_set_conf_u64,
1019 .max_write_len = 256,
1022 .name = "throttle.read_iops_device",
1023 .private = offsetof(struct throtl_grp, iops[READ]),
1024 .read_seq_string = tg_print_conf_uint,
1025 .write_string = tg_set_conf_uint,
1026 .max_write_len = 256,
1029 .name = "throttle.write_iops_device",
1030 .private = offsetof(struct throtl_grp, iops[WRITE]),
1031 .read_seq_string = tg_print_conf_uint,
1032 .write_string = tg_set_conf_uint,
1033 .max_write_len = 256,
1036 .name = "throttle.io_service_bytes",
1037 .private = offsetof(struct tg_stats_cpu, service_bytes),
1038 .read_seq_string = tg_print_cpu_rwstat,
1041 .name = "throttle.io_serviced",
1042 .private = offsetof(struct tg_stats_cpu, serviced),
1043 .read_seq_string = tg_print_cpu_rwstat,
1048 static void throtl_shutdown_wq(struct request_queue *q)
1050 struct throtl_data *td = q->td;
1052 cancel_delayed_work_sync(&td->dispatch_work);
1055 static struct blkcg_policy blkcg_policy_throtl = {
1056 .pd_size = sizeof(struct throtl_grp),
1057 .cftypes = throtl_files,
1059 .pd_init_fn = throtl_pd_init,
1060 .pd_exit_fn = throtl_pd_exit,
1061 .pd_reset_stats_fn = throtl_pd_reset_stats,
1064 bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1066 struct throtl_data *td = q->td;
1067 struct throtl_grp *tg;
1068 bool rw = bio_data_dir(bio), update_disptime = true;
1069 struct blkcg *blkcg;
1070 bool throttled = false;
1072 if (bio->bi_rw & REQ_THROTTLED) {
1073 bio->bi_rw &= ~REQ_THROTTLED;
1078 * A throtl_grp pointer retrieved under rcu can be used to access
1079 * basic fields like stats and io rates. If a group has no rules,
1080 * just update the dispatch stats in lockless manner and return.
1083 blkcg = bio_blkcg(bio);
1084 tg = throtl_lookup_tg(td, blkcg);
1086 if (tg_no_rule_group(tg, rw)) {
1087 throtl_update_dispatch_stats(tg_to_blkg(tg),
1088 bio->bi_size, bio->bi_rw);
1089 goto out_unlock_rcu;
1094 * Either group has not been allocated yet or it is not an unlimited
1097 spin_lock_irq(q->queue_lock);
1098 tg = throtl_lookup_create_tg(td, blkcg);
1102 if (tg->nr_queued[rw]) {
1104 * There is already another bio queued in same dir. No
1105 * need to update dispatch time.
1107 update_disptime = false;
1112 /* Bio is with-in rate limit of group */
1113 if (tg_may_dispatch(td, tg, bio, NULL)) {
1114 throtl_charge_bio(tg, bio);
1117 * We need to trim slice even when bios are not being queued
1118 * otherwise it might happen that a bio is not queued for
1119 * a long time and slice keeps on extending and trim is not
1120 * called for a long time. Now if limits are reduced suddenly
1121 * we take into account all the IO dispatched so far at new
1122 * low rate and * newly queued IO gets a really long dispatch
1125 * So keep on trimming slice even if bio is not queued.
1127 throtl_trim_slice(td, tg, rw);
1132 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1133 " iodisp=%u iops=%u queued=%d/%d",
1134 rw == READ ? 'R' : 'W',
1135 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1136 tg->io_disp[rw], tg->iops[rw],
1137 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1139 bio_associate_current(bio);
1140 throtl_add_bio_tg(q->td, tg, bio);
1143 if (update_disptime) {
1144 tg_update_disptime(td, tg);
1145 throtl_schedule_next_dispatch(td);
1149 spin_unlock_irq(q->queue_lock);
1157 * blk_throtl_drain - drain throttled bios
1158 * @q: request_queue to drain throttled bios for
1160 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1162 void blk_throtl_drain(struct request_queue *q)
1163 __releases(q->queue_lock) __acquires(q->queue_lock)
1165 struct throtl_data *td = q->td;
1166 struct throtl_rb_root *st = &td->tg_service_tree;
1167 struct throtl_grp *tg;
1171 queue_lockdep_assert_held(q);
1175 while ((tg = throtl_rb_first(st))) {
1176 throtl_dequeue_tg(td, tg);
1178 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1179 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1180 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1181 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1183 spin_unlock_irq(q->queue_lock);
1185 while ((bio = bio_list_pop(&bl)))
1186 generic_make_request(bio);
1188 spin_lock_irq(q->queue_lock);
1191 int blk_throtl_init(struct request_queue *q)
1193 struct throtl_data *td;
1196 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1200 td->tg_service_tree = THROTL_RB_ROOT;
1201 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1206 /* activate policy */
1207 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1213 void blk_throtl_exit(struct request_queue *q)
1216 throtl_shutdown_wq(q);
1217 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1221 static int __init throtl_init(void)
1223 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1224 if (!kthrotld_workqueue)
1225 panic("Failed to create kthrotld\n");
1227 return blkcg_policy_register(&blkcg_policy_throtl);
1230 module_init(throtl_init);