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_service_queue {
30 struct rb_root pending_tree; /* RB tree of active tgs */
31 struct rb_node *first_pending; /* first node in the tree */
32 unsigned int nr_pending; /* # queued in the tree */
33 unsigned long first_pending_disptime; /* disptime of the first tg */
37 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
40 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
42 /* Per-cpu group stats */
44 /* total bytes transferred */
45 struct blkg_rwstat service_bytes;
46 /* total IOs serviced, post merge */
47 struct blkg_rwstat serviced;
51 /* must be the first member */
52 struct blkg_policy_data pd;
54 /* active throtl group service_queue member */
55 struct rb_node rb_node;
57 /* throtl_data this group belongs to */
58 struct throtl_data *td;
60 /* this group's service queue */
61 struct throtl_service_queue service_queue;
64 * Dispatch time in jiffies. This is the estimated time when group
65 * will unthrottle and is ready to dispatch more bio. It is used as
66 * key to sort active groups in service tree.
68 unsigned long disptime;
72 /* Two lists for READ and WRITE */
73 struct bio_list bio_lists[2];
75 /* Number of queued bios on READ and WRITE lists */
76 unsigned int nr_queued[2];
78 /* bytes per second rate limits */
84 /* Number of bytes disptached in current slice */
85 uint64_t bytes_disp[2];
86 /* Number of bio's dispatched in current slice */
87 unsigned int io_disp[2];
89 /* When did we start a new slice */
90 unsigned long slice_start[2];
91 unsigned long slice_end[2];
93 /* Per cpu stats pointer */
94 struct tg_stats_cpu __percpu *stats_cpu;
96 /* List of tgs waiting for per cpu stats memory to be allocated */
97 struct list_head stats_alloc_node;
102 /* service tree for active throtl groups */
103 struct throtl_service_queue service_queue;
105 struct request_queue *queue;
107 /* Total Number of queued bios on READ and WRITE lists */
108 unsigned int nr_queued[2];
111 * number of total undestroyed groups
113 unsigned int nr_undestroyed_grps;
115 /* Work for dispatching throttled bios */
116 struct delayed_work dispatch_work;
119 /* list and work item to allocate percpu group stats */
120 static DEFINE_SPINLOCK(tg_stats_alloc_lock);
121 static LIST_HEAD(tg_stats_alloc_list);
123 static void tg_stats_alloc_fn(struct work_struct *);
124 static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
126 static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
128 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
131 static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
133 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
136 static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
138 return pd_to_blkg(&tg->pd);
141 static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
143 return blkg_to_tg(td->queue->root_blkg);
146 #define throtl_log_tg(tg, fmt, args...) do { \
149 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
150 blk_add_trace_msg((tg)->td->queue, "throtl %s " fmt, __pbuf, ##args); \
153 #define throtl_log(td, fmt, args...) \
154 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
157 * Worker for allocating per cpu stat for tgs. This is scheduled on the
158 * system_wq once there are some groups on the alloc_list waiting for
161 static void tg_stats_alloc_fn(struct work_struct *work)
163 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
164 struct delayed_work *dwork = to_delayed_work(work);
169 stats_cpu = alloc_percpu(struct tg_stats_cpu);
171 /* allocation failed, try again after some time */
172 schedule_delayed_work(dwork, msecs_to_jiffies(10));
177 spin_lock_irq(&tg_stats_alloc_lock);
179 if (!list_empty(&tg_stats_alloc_list)) {
180 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
183 swap(tg->stats_cpu, stats_cpu);
184 list_del_init(&tg->stats_alloc_node);
187 empty = list_empty(&tg_stats_alloc_list);
188 spin_unlock_irq(&tg_stats_alloc_lock);
193 /* init a service_queue, assumes the caller zeroed it */
194 static void throtl_service_queue_init(struct throtl_service_queue *sq)
196 sq->pending_tree = RB_ROOT;
199 static void throtl_pd_init(struct blkcg_gq *blkg)
201 struct throtl_grp *tg = blkg_to_tg(blkg);
204 throtl_service_queue_init(&tg->service_queue);
205 RB_CLEAR_NODE(&tg->rb_node);
206 tg->td = blkg->q->td;
207 bio_list_init(&tg->bio_lists[0]);
208 bio_list_init(&tg->bio_lists[1]);
213 tg->iops[WRITE] = -1;
216 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
217 * but percpu allocator can't be called from IO path. Queue tg on
218 * tg_stats_alloc_list and allocate from work item.
220 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
221 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
222 schedule_delayed_work(&tg_stats_alloc_work, 0);
223 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
226 static void throtl_pd_exit(struct blkcg_gq *blkg)
228 struct throtl_grp *tg = blkg_to_tg(blkg);
231 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
232 list_del_init(&tg->stats_alloc_node);
233 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
235 free_percpu(tg->stats_cpu);
238 static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
240 struct throtl_grp *tg = blkg_to_tg(blkg);
243 if (tg->stats_cpu == NULL)
246 for_each_possible_cpu(cpu) {
247 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
249 blkg_rwstat_reset(&sc->service_bytes);
250 blkg_rwstat_reset(&sc->serviced);
254 static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
258 * This is the common case when there are no blkcgs. Avoid lookup
261 if (blkcg == &blkcg_root)
262 return td_root_tg(td);
264 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
267 static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
270 struct request_queue *q = td->queue;
271 struct throtl_grp *tg = NULL;
274 * This is the common case when there are no blkcgs. Avoid lookup
277 if (blkcg == &blkcg_root) {
280 struct blkcg_gq *blkg;
282 blkg = blkg_lookup_create(blkcg, q);
284 /* if %NULL and @q is alive, fall back to root_tg */
286 tg = blkg_to_tg(blkg);
287 else if (!blk_queue_dying(q))
294 static struct throtl_grp *
295 throtl_rb_first(struct throtl_service_queue *parent_sq)
297 /* Service tree is empty */
298 if (!parent_sq->nr_pending)
301 if (!parent_sq->first_pending)
302 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
304 if (parent_sq->first_pending)
305 return rb_entry_tg(parent_sq->first_pending);
310 static void rb_erase_init(struct rb_node *n, struct rb_root *root)
316 static void throtl_rb_erase(struct rb_node *n,
317 struct throtl_service_queue *parent_sq)
319 if (parent_sq->first_pending == n)
320 parent_sq->first_pending = NULL;
321 rb_erase_init(n, &parent_sq->pending_tree);
322 --parent_sq->nr_pending;
325 static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
327 struct throtl_grp *tg;
329 tg = throtl_rb_first(parent_sq);
333 parent_sq->first_pending_disptime = tg->disptime;
336 static void tg_service_queue_add(struct throtl_grp *tg,
337 struct throtl_service_queue *parent_sq)
339 struct rb_node **node = &parent_sq->pending_tree.rb_node;
340 struct rb_node *parent = NULL;
341 struct throtl_grp *__tg;
342 unsigned long key = tg->disptime;
345 while (*node != NULL) {
347 __tg = rb_entry_tg(parent);
349 if (time_before(key, __tg->disptime))
350 node = &parent->rb_left;
352 node = &parent->rb_right;
358 parent_sq->first_pending = &tg->rb_node;
360 rb_link_node(&tg->rb_node, parent, node);
361 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
364 static void __throtl_enqueue_tg(struct throtl_grp *tg,
365 struct throtl_service_queue *parent_sq)
367 tg_service_queue_add(tg, parent_sq);
368 tg->flags |= THROTL_TG_PENDING;
369 parent_sq->nr_pending++;
372 static void throtl_enqueue_tg(struct throtl_grp *tg,
373 struct throtl_service_queue *parent_sq)
375 if (!(tg->flags & THROTL_TG_PENDING))
376 __throtl_enqueue_tg(tg, parent_sq);
379 static void __throtl_dequeue_tg(struct throtl_grp *tg,
380 struct throtl_service_queue *parent_sq)
382 throtl_rb_erase(&tg->rb_node, parent_sq);
383 tg->flags &= ~THROTL_TG_PENDING;
386 static void throtl_dequeue_tg(struct throtl_grp *tg,
387 struct throtl_service_queue *parent_sq)
389 if (tg->flags & THROTL_TG_PENDING)
390 __throtl_dequeue_tg(tg, parent_sq);
393 /* Call with queue lock held */
394 static void throtl_schedule_delayed_work(struct throtl_data *td,
397 struct delayed_work *dwork = &td->dispatch_work;
399 mod_delayed_work(kthrotld_workqueue, dwork, delay);
400 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
403 static void throtl_schedule_next_dispatch(struct throtl_data *td)
405 struct throtl_service_queue *sq = &td->service_queue;
407 /* any pending children left? */
411 update_min_dispatch_time(sq);
413 if (time_before_eq(sq->first_pending_disptime, jiffies))
414 throtl_schedule_delayed_work(td, 0);
416 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
419 static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
421 tg->bytes_disp[rw] = 0;
423 tg->slice_start[rw] = jiffies;
424 tg->slice_end[rw] = jiffies + throtl_slice;
425 throtl_log_tg(tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
426 rw == READ ? 'R' : 'W', tg->slice_start[rw],
427 tg->slice_end[rw], jiffies);
430 static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
431 unsigned long jiffy_end)
433 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
436 static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
437 unsigned long jiffy_end)
439 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
440 throtl_log_tg(tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
441 rw == READ ? 'R' : 'W', tg->slice_start[rw],
442 tg->slice_end[rw], jiffies);
445 /* Determine if previously allocated or extended slice is complete or not */
446 static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
448 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
454 /* Trim the used slices and adjust slice start accordingly */
455 static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
457 unsigned long nr_slices, time_elapsed, io_trim;
460 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
463 * If bps are unlimited (-1), then time slice don't get
464 * renewed. Don't try to trim the slice if slice is used. A new
465 * slice will start when appropriate.
467 if (throtl_slice_used(tg, rw))
471 * A bio has been dispatched. Also adjust slice_end. It might happen
472 * that initially cgroup limit was very low resulting in high
473 * slice_end, but later limit was bumped up and bio was dispached
474 * sooner, then we need to reduce slice_end. A high bogus slice_end
475 * is bad because it does not allow new slice to start.
478 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
480 time_elapsed = jiffies - tg->slice_start[rw];
482 nr_slices = time_elapsed / throtl_slice;
486 tmp = tg->bps[rw] * throtl_slice * nr_slices;
490 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
492 if (!bytes_trim && !io_trim)
495 if (tg->bytes_disp[rw] >= bytes_trim)
496 tg->bytes_disp[rw] -= bytes_trim;
498 tg->bytes_disp[rw] = 0;
500 if (tg->io_disp[rw] >= io_trim)
501 tg->io_disp[rw] -= io_trim;
505 tg->slice_start[rw] += nr_slices * throtl_slice;
507 throtl_log_tg(tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
508 " start=%lu end=%lu jiffies=%lu",
509 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
510 tg->slice_start[rw], tg->slice_end[rw], jiffies);
513 static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
516 bool rw = bio_data_dir(bio);
517 unsigned int io_allowed;
518 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
521 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
523 /* Slice has just started. Consider one slice interval */
525 jiffy_elapsed_rnd = throtl_slice;
527 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
530 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
531 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
532 * will allow dispatch after 1 second and after that slice should
536 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
540 io_allowed = UINT_MAX;
544 if (tg->io_disp[rw] + 1 <= io_allowed) {
550 /* Calc approx time to dispatch */
551 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
553 if (jiffy_wait > jiffy_elapsed)
554 jiffy_wait = jiffy_wait - jiffy_elapsed;
563 static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
566 bool rw = bio_data_dir(bio);
567 u64 bytes_allowed, extra_bytes, tmp;
568 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
570 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
572 /* Slice has just started. Consider one slice interval */
574 jiffy_elapsed_rnd = throtl_slice;
576 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
578 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
582 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
588 /* Calc approx time to dispatch */
589 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
590 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
596 * This wait time is without taking into consideration the rounding
597 * up we did. Add that time also.
599 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
605 static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
606 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
612 * Returns whether one can dispatch a bio or not. Also returns approx number
613 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
615 static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
618 bool rw = bio_data_dir(bio);
619 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
622 * Currently whole state machine of group depends on first bio
623 * queued in the group bio list. So one should not be calling
624 * this function with a different bio if there are other bios
627 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
629 /* If tg->bps = -1, then BW is unlimited */
630 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
637 * If previous slice expired, start a new one otherwise renew/extend
638 * existing slice to make sure it is at least throtl_slice interval
641 if (throtl_slice_used(tg, rw))
642 throtl_start_new_slice(tg, rw);
644 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
645 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
648 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
649 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
655 max_wait = max(bps_wait, iops_wait);
660 if (time_before(tg->slice_end[rw], jiffies + max_wait))
661 throtl_extend_slice(tg, rw, jiffies + max_wait);
666 static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
669 struct throtl_grp *tg = blkg_to_tg(blkg);
670 struct tg_stats_cpu *stats_cpu;
673 /* If per cpu stats are not allocated yet, don't do any accounting. */
674 if (tg->stats_cpu == NULL)
678 * Disabling interrupts to provide mutual exclusion between two
679 * writes on same cpu. It probably is not needed for 64bit. Not
680 * optimizing that case yet.
682 local_irq_save(flags);
684 stats_cpu = this_cpu_ptr(tg->stats_cpu);
686 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
687 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
689 local_irq_restore(flags);
692 static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
694 bool rw = bio_data_dir(bio);
696 /* Charge the bio to the group */
697 tg->bytes_disp[rw] += bio->bi_size;
700 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
703 static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg,
704 struct throtl_service_queue *parent_sq)
706 bool rw = bio_data_dir(bio);
708 bio_list_add(&tg->bio_lists[rw], bio);
709 /* Take a bio reference on tg */
710 blkg_get(tg_to_blkg(tg));
712 tg->td->nr_queued[rw]++;
713 throtl_enqueue_tg(tg, parent_sq);
716 static void tg_update_disptime(struct throtl_grp *tg,
717 struct throtl_service_queue *parent_sq)
719 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
722 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
723 tg_may_dispatch(tg, bio, &read_wait);
725 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
726 tg_may_dispatch(tg, bio, &write_wait);
728 min_wait = min(read_wait, write_wait);
729 disptime = jiffies + min_wait;
731 /* Update dispatch time */
732 throtl_dequeue_tg(tg, parent_sq);
733 tg->disptime = disptime;
734 throtl_enqueue_tg(tg, parent_sq);
737 static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw,
742 bio = bio_list_pop(&tg->bio_lists[rw]);
744 /* Drop bio reference on blkg */
745 blkg_put(tg_to_blkg(tg));
747 BUG_ON(tg->td->nr_queued[rw] <= 0);
748 tg->td->nr_queued[rw]--;
750 throtl_charge_bio(tg, bio);
751 bio_list_add(bl, bio);
752 bio->bi_rw |= REQ_THROTTLED;
754 throtl_trim_slice(tg, rw);
757 static int throtl_dispatch_tg(struct throtl_grp *tg, struct bio_list *bl)
759 unsigned int nr_reads = 0, nr_writes = 0;
760 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
761 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
764 /* Try to dispatch 75% READS and 25% WRITES */
766 while ((bio = bio_list_peek(&tg->bio_lists[READ])) &&
767 tg_may_dispatch(tg, bio, NULL)) {
769 tg_dispatch_one_bio(tg, bio_data_dir(bio), bl);
772 if (nr_reads >= max_nr_reads)
776 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])) &&
777 tg_may_dispatch(tg, bio, NULL)) {
779 tg_dispatch_one_bio(tg, bio_data_dir(bio), bl);
782 if (nr_writes >= max_nr_writes)
786 return nr_reads + nr_writes;
789 static int throtl_select_dispatch(struct throtl_service_queue *parent_sq,
792 unsigned int nr_disp = 0;
793 struct throtl_grp *tg;
796 tg = throtl_rb_first(parent_sq);
801 if (time_before(jiffies, tg->disptime))
804 throtl_dequeue_tg(tg, parent_sq);
806 nr_disp += throtl_dispatch_tg(tg, bl);
808 if (tg->nr_queued[0] || tg->nr_queued[1])
809 tg_update_disptime(tg, parent_sq);
811 if (nr_disp >= throtl_quantum)
818 /* work function to dispatch throttled bios */
819 void blk_throtl_dispatch_work_fn(struct work_struct *work)
821 struct throtl_data *td = container_of(to_delayed_work(work),
822 struct throtl_data, dispatch_work);
823 struct request_queue *q = td->queue;
824 unsigned int nr_disp = 0;
825 struct bio_list bio_list_on_stack;
827 struct blk_plug plug;
829 spin_lock_irq(q->queue_lock);
831 bio_list_init(&bio_list_on_stack);
833 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
834 td->nr_queued[READ] + td->nr_queued[WRITE],
835 td->nr_queued[READ], td->nr_queued[WRITE]);
837 nr_disp = throtl_select_dispatch(&td->service_queue, &bio_list_on_stack);
840 throtl_log(td, "bios disp=%u", nr_disp);
842 throtl_schedule_next_dispatch(td);
844 spin_unlock_irq(q->queue_lock);
847 * If we dispatched some requests, unplug the queue to make sure
851 blk_start_plug(&plug);
852 while((bio = bio_list_pop(&bio_list_on_stack)))
853 generic_make_request(bio);
854 blk_finish_plug(&plug);
858 static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
859 struct blkg_policy_data *pd, int off)
861 struct throtl_grp *tg = pd_to_tg(pd);
862 struct blkg_rwstat rwstat = { }, tmp;
865 for_each_possible_cpu(cpu) {
866 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
868 tmp = blkg_rwstat_read((void *)sc + off);
869 for (i = 0; i < BLKG_RWSTAT_NR; i++)
870 rwstat.cnt[i] += tmp.cnt[i];
873 return __blkg_prfill_rwstat(sf, pd, &rwstat);
876 static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
879 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
881 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
886 static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
889 struct throtl_grp *tg = pd_to_tg(pd);
890 u64 v = *(u64 *)((void *)tg + off);
894 return __blkg_prfill_u64(sf, pd, v);
897 static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
900 struct throtl_grp *tg = pd_to_tg(pd);
901 unsigned int v = *(unsigned int *)((void *)tg + off);
905 return __blkg_prfill_u64(sf, pd, v);
908 static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
911 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
912 &blkcg_policy_throtl, cft->private, false);
916 static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
919 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
920 &blkcg_policy_throtl, cft->private, false);
924 static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
927 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
928 struct blkg_conf_ctx ctx;
929 struct throtl_grp *tg;
930 struct throtl_data *td;
933 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
937 tg = blkg_to_tg(ctx.blkg);
938 td = ctx.blkg->q->td;
944 *(u64 *)((void *)tg + cft->private) = ctx.v;
946 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
948 throtl_log_tg(tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
949 tg->bps[READ], tg->bps[WRITE],
950 tg->iops[READ], tg->iops[WRITE]);
953 * We're already holding queue_lock and know @tg is valid. Let's
954 * apply the new config directly.
956 * Restart the slices for both READ and WRITES. It might happen
957 * that a group's limit are dropped suddenly and we don't want to
958 * account recently dispatched IO with new low rate.
960 throtl_start_new_slice(tg, 0);
961 throtl_start_new_slice(tg, 1);
963 if (tg->flags & THROTL_TG_PENDING) {
964 tg_update_disptime(tg, &td->service_queue);
965 throtl_schedule_next_dispatch(td);
968 blkg_conf_finish(&ctx);
972 static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
975 return tg_set_conf(cgrp, cft, buf, true);
978 static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
981 return tg_set_conf(cgrp, cft, buf, false);
984 static struct cftype throtl_files[] = {
986 .name = "throttle.read_bps_device",
987 .private = offsetof(struct throtl_grp, bps[READ]),
988 .read_seq_string = tg_print_conf_u64,
989 .write_string = tg_set_conf_u64,
990 .max_write_len = 256,
993 .name = "throttle.write_bps_device",
994 .private = offsetof(struct throtl_grp, bps[WRITE]),
995 .read_seq_string = tg_print_conf_u64,
996 .write_string = tg_set_conf_u64,
997 .max_write_len = 256,
1000 .name = "throttle.read_iops_device",
1001 .private = offsetof(struct throtl_grp, iops[READ]),
1002 .read_seq_string = tg_print_conf_uint,
1003 .write_string = tg_set_conf_uint,
1004 .max_write_len = 256,
1007 .name = "throttle.write_iops_device",
1008 .private = offsetof(struct throtl_grp, iops[WRITE]),
1009 .read_seq_string = tg_print_conf_uint,
1010 .write_string = tg_set_conf_uint,
1011 .max_write_len = 256,
1014 .name = "throttle.io_service_bytes",
1015 .private = offsetof(struct tg_stats_cpu, service_bytes),
1016 .read_seq_string = tg_print_cpu_rwstat,
1019 .name = "throttle.io_serviced",
1020 .private = offsetof(struct tg_stats_cpu, serviced),
1021 .read_seq_string = tg_print_cpu_rwstat,
1026 static void throtl_shutdown_wq(struct request_queue *q)
1028 struct throtl_data *td = q->td;
1030 cancel_delayed_work_sync(&td->dispatch_work);
1033 static struct blkcg_policy blkcg_policy_throtl = {
1034 .pd_size = sizeof(struct throtl_grp),
1035 .cftypes = throtl_files,
1037 .pd_init_fn = throtl_pd_init,
1038 .pd_exit_fn = throtl_pd_exit,
1039 .pd_reset_stats_fn = throtl_pd_reset_stats,
1042 bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1044 struct throtl_data *td = q->td;
1045 struct throtl_grp *tg;
1046 bool rw = bio_data_dir(bio), update_disptime = true;
1047 struct blkcg *blkcg;
1048 bool throttled = false;
1050 if (bio->bi_rw & REQ_THROTTLED) {
1051 bio->bi_rw &= ~REQ_THROTTLED;
1056 * A throtl_grp pointer retrieved under rcu can be used to access
1057 * basic fields like stats and io rates. If a group has no rules,
1058 * just update the dispatch stats in lockless manner and return.
1061 blkcg = bio_blkcg(bio);
1062 tg = throtl_lookup_tg(td, blkcg);
1064 if (tg_no_rule_group(tg, rw)) {
1065 throtl_update_dispatch_stats(tg_to_blkg(tg),
1066 bio->bi_size, bio->bi_rw);
1067 goto out_unlock_rcu;
1072 * Either group has not been allocated yet or it is not an unlimited
1075 spin_lock_irq(q->queue_lock);
1076 tg = throtl_lookup_create_tg(td, blkcg);
1080 if (tg->nr_queued[rw]) {
1082 * There is already another bio queued in same dir. No
1083 * need to update dispatch time.
1085 update_disptime = false;
1090 /* Bio is with-in rate limit of group */
1091 if (tg_may_dispatch(tg, bio, NULL)) {
1092 throtl_charge_bio(tg, bio);
1095 * We need to trim slice even when bios are not being queued
1096 * otherwise it might happen that a bio is not queued for
1097 * a long time and slice keeps on extending and trim is not
1098 * called for a long time. Now if limits are reduced suddenly
1099 * we take into account all the IO dispatched so far at new
1100 * low rate and * newly queued IO gets a really long dispatch
1103 * So keep on trimming slice even if bio is not queued.
1105 throtl_trim_slice(tg, rw);
1110 throtl_log_tg(tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1111 " iodisp=%u iops=%u queued=%d/%d",
1112 rw == READ ? 'R' : 'W',
1113 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
1114 tg->io_disp[rw], tg->iops[rw],
1115 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1117 bio_associate_current(bio);
1118 throtl_add_bio_tg(bio, tg, &q->td->service_queue);
1121 if (update_disptime) {
1122 tg_update_disptime(tg, &td->service_queue);
1123 throtl_schedule_next_dispatch(td);
1127 spin_unlock_irq(q->queue_lock);
1135 * blk_throtl_drain - drain throttled bios
1136 * @q: request_queue to drain throttled bios for
1138 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1140 void blk_throtl_drain(struct request_queue *q)
1141 __releases(q->queue_lock) __acquires(q->queue_lock)
1143 struct throtl_data *td = q->td;
1144 struct throtl_service_queue *parent_sq = &td->service_queue;
1145 struct throtl_grp *tg;
1149 queue_lockdep_assert_held(q);
1153 while ((tg = throtl_rb_first(parent_sq))) {
1154 throtl_dequeue_tg(tg, parent_sq);
1156 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1157 tg_dispatch_one_bio(tg, bio_data_dir(bio), &bl);
1158 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1159 tg_dispatch_one_bio(tg, bio_data_dir(bio), &bl);
1161 spin_unlock_irq(q->queue_lock);
1163 while ((bio = bio_list_pop(&bl)))
1164 generic_make_request(bio);
1166 spin_lock_irq(q->queue_lock);
1169 int blk_throtl_init(struct request_queue *q)
1171 struct throtl_data *td;
1174 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1178 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1179 throtl_service_queue_init(&td->service_queue);
1184 /* activate policy */
1185 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1191 void blk_throtl_exit(struct request_queue *q)
1194 throtl_shutdown_wq(q);
1195 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1199 static int __init throtl_init(void)
1201 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1202 if (!kthrotld_workqueue)
1203 panic("Failed to create kthrotld\n");
1205 return blkcg_policy_register(&blkcg_policy_throtl);
1208 module_init(throtl_init);