2 * Interface for controlling IO bandwidth on a request queue
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/bio.h>
11 #include <linux/blktrace_api.h>
12 #include "blk-cgroup.h"
15 /* Max dispatch from a group in 1 round */
16 static int throtl_grp_quantum
= 8;
18 /* Total max dispatch from all groups in one round */
19 static int throtl_quantum
= 32;
21 /* Throttling is performed over 100ms slice and after that slice is renewed */
22 static unsigned long throtl_slice
= HZ
/10; /* 100 ms */
24 static struct blkio_policy_type blkio_policy_throtl
;
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct
*kthrotld_workqueue
;
28 static void throtl_schedule_delayed_work(struct throtl_data
*td
,
31 struct throtl_rb_root
{
35 unsigned long min_disptime
;
38 #define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
39 .count = 0, .min_disptime = 0}
41 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
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 /* active throtl group service_tree member */
53 struct rb_node rb_node
;
56 * Dispatch time in jiffies. This is the estimated time when group
57 * will unthrottle and is ready to dispatch more bio. It is used as
58 * key to sort active groups in service tree.
60 unsigned long disptime
;
64 /* Two lists for READ and WRITE */
65 struct bio_list bio_lists
[2];
67 /* Number of queued bios on READ and WRITE lists */
68 unsigned int nr_queued
[2];
70 /* bytes per second rate limits */
76 /* Number of bytes disptached in current slice */
77 uint64_t bytes_disp
[2];
78 /* Number of bio's dispatched in current slice */
79 unsigned int io_disp
[2];
81 /* When did we start a new slice */
82 unsigned long slice_start
[2];
83 unsigned long slice_end
[2];
85 /* Some throttle limits got updated for the group */
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 throtl_work
;
116 /* list and work item to allocate percpu group stats */
117 static DEFINE_SPINLOCK(tg_stats_alloc_lock
);
118 static LIST_HEAD(tg_stats_alloc_list
);
120 static void tg_stats_alloc_fn(struct work_struct
*);
121 static DECLARE_DELAYED_WORK(tg_stats_alloc_work
, tg_stats_alloc_fn
);
123 static inline struct throtl_grp
*blkg_to_tg(struct blkio_group
*blkg
)
125 return blkg_to_pdata(blkg
, &blkio_policy_throtl
);
128 static inline struct blkio_group
*tg_to_blkg(struct throtl_grp
*tg
)
130 return pdata_to_blkg(tg
);
133 static inline struct throtl_grp
*td_root_tg(struct throtl_data
*td
)
135 return blkg_to_tg(td
->queue
->root_blkg
);
138 enum tg_state_flags
{
139 THROTL_TG_FLAG_on_rr
= 0, /* on round-robin busy list */
142 #define THROTL_TG_FNS(name) \
143 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
145 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
147 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
149 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
151 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
153 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
156 THROTL_TG_FNS(on_rr
);
158 #define throtl_log_tg(td, tg, fmt, args...) \
159 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
160 blkg_path(tg_to_blkg(tg)), ##args); \
162 #define throtl_log(td, fmt, args...) \
163 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
165 static inline unsigned int total_nr_queued(struct throtl_data
*td
)
167 return td
->nr_queued
[0] + td
->nr_queued
[1];
171 * Worker for allocating per cpu stat for tgs. This is scheduled on the
172 * system_nrt_wq once there are some groups on the alloc_list waiting for
175 static void tg_stats_alloc_fn(struct work_struct
*work
)
177 static struct tg_stats_cpu
*stats_cpu
; /* this fn is non-reentrant */
178 struct delayed_work
*dwork
= to_delayed_work(work
);
183 stats_cpu
= alloc_percpu(struct tg_stats_cpu
);
185 /* allocation failed, try again after some time */
186 queue_delayed_work(system_nrt_wq
, dwork
,
187 msecs_to_jiffies(10));
192 spin_lock_irq(&tg_stats_alloc_lock
);
194 if (!list_empty(&tg_stats_alloc_list
)) {
195 struct throtl_grp
*tg
= list_first_entry(&tg_stats_alloc_list
,
198 swap(tg
->stats_cpu
, stats_cpu
);
199 list_del_init(&tg
->stats_alloc_node
);
202 empty
= list_empty(&tg_stats_alloc_list
);
203 spin_unlock_irq(&tg_stats_alloc_lock
);
208 static void throtl_init_blkio_group(struct blkio_group
*blkg
)
210 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
212 RB_CLEAR_NODE(&tg
->rb_node
);
213 bio_list_init(&tg
->bio_lists
[0]);
214 bio_list_init(&tg
->bio_lists
[1]);
215 tg
->limits_changed
= false;
220 tg
->iops
[WRITE
] = -1;
223 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
224 * but percpu allocator can't be called from IO path. Queue tg on
225 * tg_stats_alloc_list and allocate from work item.
227 spin_lock(&tg_stats_alloc_lock
);
228 list_add(&tg
->stats_alloc_node
, &tg_stats_alloc_list
);
229 queue_delayed_work(system_nrt_wq
, &tg_stats_alloc_work
, 0);
230 spin_unlock(&tg_stats_alloc_lock
);
233 static void throtl_exit_blkio_group(struct blkio_group
*blkg
)
235 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
237 spin_lock(&tg_stats_alloc_lock
);
238 list_del_init(&tg
->stats_alloc_node
);
239 spin_unlock(&tg_stats_alloc_lock
);
241 free_percpu(tg
->stats_cpu
);
244 static void throtl_reset_group_stats(struct blkio_group
*blkg
)
246 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
249 if (tg
->stats_cpu
== NULL
)
252 for_each_possible_cpu(cpu
) {
253 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
255 blkg_rwstat_reset(&sc
->service_bytes
);
256 blkg_rwstat_reset(&sc
->serviced
);
261 throtl_grp
*throtl_lookup_tg(struct throtl_data
*td
, struct blkio_cgroup
*blkcg
)
264 * This is the common case when there are no blkio cgroups.
265 * Avoid lookup in this case
267 if (blkcg
== &blkio_root_cgroup
)
268 return td_root_tg(td
);
270 return blkg_to_tg(blkg_lookup(blkcg
, td
->queue
));
273 static struct throtl_grp
*throtl_lookup_create_tg(struct throtl_data
*td
,
274 struct blkio_cgroup
*blkcg
)
276 struct request_queue
*q
= td
->queue
;
277 struct throtl_grp
*tg
= NULL
;
280 * This is the common case when there are no blkio cgroups.
281 * Avoid lookup in this case
283 if (blkcg
== &blkio_root_cgroup
) {
286 struct blkio_group
*blkg
;
288 blkg
= blkg_lookup_create(blkcg
, q
);
290 /* if %NULL and @q is alive, fall back to root_tg */
292 tg
= blkg_to_tg(blkg
);
293 else if (!blk_queue_dead(q
))
300 static struct throtl_grp
*throtl_rb_first(struct throtl_rb_root
*root
)
302 /* Service tree is empty */
307 root
->left
= rb_first(&root
->rb
);
310 return rb_entry_tg(root
->left
);
315 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
321 static void throtl_rb_erase(struct rb_node
*n
, struct throtl_rb_root
*root
)
325 rb_erase_init(n
, &root
->rb
);
329 static void update_min_dispatch_time(struct throtl_rb_root
*st
)
331 struct throtl_grp
*tg
;
333 tg
= throtl_rb_first(st
);
337 st
->min_disptime
= tg
->disptime
;
341 tg_service_tree_add(struct throtl_rb_root
*st
, struct throtl_grp
*tg
)
343 struct rb_node
**node
= &st
->rb
.rb_node
;
344 struct rb_node
*parent
= NULL
;
345 struct throtl_grp
*__tg
;
346 unsigned long key
= tg
->disptime
;
349 while (*node
!= NULL
) {
351 __tg
= rb_entry_tg(parent
);
353 if (time_before(key
, __tg
->disptime
))
354 node
= &parent
->rb_left
;
356 node
= &parent
->rb_right
;
362 st
->left
= &tg
->rb_node
;
364 rb_link_node(&tg
->rb_node
, parent
, node
);
365 rb_insert_color(&tg
->rb_node
, &st
->rb
);
368 static void __throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
370 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
372 tg_service_tree_add(st
, tg
);
373 throtl_mark_tg_on_rr(tg
);
377 static void throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
379 if (!throtl_tg_on_rr(tg
))
380 __throtl_enqueue_tg(td
, tg
);
383 static void __throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
385 throtl_rb_erase(&tg
->rb_node
, &td
->tg_service_tree
);
386 throtl_clear_tg_on_rr(tg
);
389 static void throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
391 if (throtl_tg_on_rr(tg
))
392 __throtl_dequeue_tg(td
, tg
);
395 static void throtl_schedule_next_dispatch(struct throtl_data
*td
)
397 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
400 * If there are more bios pending, schedule more work.
402 if (!total_nr_queued(td
))
407 update_min_dispatch_time(st
);
409 if (time_before_eq(st
->min_disptime
, jiffies
))
410 throtl_schedule_delayed_work(td
, 0);
412 throtl_schedule_delayed_work(td
, (st
->min_disptime
- jiffies
));
416 throtl_start_new_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
418 tg
->bytes_disp
[rw
] = 0;
420 tg
->slice_start
[rw
] = jiffies
;
421 tg
->slice_end
[rw
] = jiffies
+ throtl_slice
;
422 throtl_log_tg(td
, tg
, "[%c] new slice start=%lu end=%lu jiffies=%lu",
423 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
424 tg
->slice_end
[rw
], jiffies
);
427 static inline void throtl_set_slice_end(struct throtl_data
*td
,
428 struct throtl_grp
*tg
, bool rw
, unsigned long jiffy_end
)
430 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
433 static inline void throtl_extend_slice(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
);
437 throtl_log_tg(td
, tg
, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
438 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
439 tg
->slice_end
[rw
], jiffies
);
442 /* Determine if previously allocated or extended slice is complete or not */
444 throtl_slice_used(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
446 if (time_in_range(jiffies
, tg
->slice_start
[rw
], tg
->slice_end
[rw
]))
452 /* Trim the used slices and adjust slice start accordingly */
454 throtl_trim_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
456 unsigned long nr_slices
, time_elapsed
, io_trim
;
459 BUG_ON(time_before(tg
->slice_end
[rw
], tg
->slice_start
[rw
]));
462 * If bps are unlimited (-1), then time slice don't get
463 * renewed. Don't try to trim the slice if slice is used. A new
464 * slice will start when appropriate.
466 if (throtl_slice_used(td
, tg
, rw
))
470 * A bio has been dispatched. Also adjust slice_end. It might happen
471 * that initially cgroup limit was very low resulting in high
472 * slice_end, but later limit was bumped up and bio was dispached
473 * sooner, then we need to reduce slice_end. A high bogus slice_end
474 * is bad because it does not allow new slice to start.
477 throtl_set_slice_end(td
, tg
, rw
, jiffies
+ throtl_slice
);
479 time_elapsed
= jiffies
- tg
->slice_start
[rw
];
481 nr_slices
= time_elapsed
/ throtl_slice
;
485 tmp
= tg
->bps
[rw
] * throtl_slice
* nr_slices
;
489 io_trim
= (tg
->iops
[rw
] * throtl_slice
* nr_slices
)/HZ
;
491 if (!bytes_trim
&& !io_trim
)
494 if (tg
->bytes_disp
[rw
] >= bytes_trim
)
495 tg
->bytes_disp
[rw
] -= bytes_trim
;
497 tg
->bytes_disp
[rw
] = 0;
499 if (tg
->io_disp
[rw
] >= io_trim
)
500 tg
->io_disp
[rw
] -= io_trim
;
504 tg
->slice_start
[rw
] += nr_slices
* throtl_slice
;
506 throtl_log_tg(td
, tg
, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
507 " start=%lu end=%lu jiffies=%lu",
508 rw
== READ
? 'R' : 'W', nr_slices
, bytes_trim
, io_trim
,
509 tg
->slice_start
[rw
], tg
->slice_end
[rw
], jiffies
);
512 static bool tg_with_in_iops_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
513 struct bio
*bio
, unsigned long *wait
)
515 bool rw
= bio_data_dir(bio
);
516 unsigned int io_allowed
;
517 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
520 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
522 /* Slice has just started. Consider one slice interval */
524 jiffy_elapsed_rnd
= throtl_slice
;
526 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
529 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
530 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
531 * will allow dispatch after 1 second and after that slice should
535 tmp
= (u64
)tg
->iops
[rw
] * jiffy_elapsed_rnd
;
539 io_allowed
= UINT_MAX
;
543 if (tg
->io_disp
[rw
] + 1 <= io_allowed
) {
549 /* Calc approx time to dispatch */
550 jiffy_wait
= ((tg
->io_disp
[rw
] + 1) * HZ
)/tg
->iops
[rw
] + 1;
552 if (jiffy_wait
> jiffy_elapsed
)
553 jiffy_wait
= jiffy_wait
- jiffy_elapsed
;
562 static bool tg_with_in_bps_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
563 struct bio
*bio
, unsigned long *wait
)
565 bool rw
= bio_data_dir(bio
);
566 u64 bytes_allowed
, extra_bytes
, tmp
;
567 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
569 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
571 /* Slice has just started. Consider one slice interval */
573 jiffy_elapsed_rnd
= throtl_slice
;
575 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
577 tmp
= tg
->bps
[rw
] * jiffy_elapsed_rnd
;
581 if (tg
->bytes_disp
[rw
] + bio
->bi_size
<= bytes_allowed
) {
587 /* Calc approx time to dispatch */
588 extra_bytes
= tg
->bytes_disp
[rw
] + bio
->bi_size
- bytes_allowed
;
589 jiffy_wait
= div64_u64(extra_bytes
* HZ
, tg
->bps
[rw
]);
595 * This wait time is without taking into consideration the rounding
596 * up we did. Add that time also.
598 jiffy_wait
= jiffy_wait
+ (jiffy_elapsed_rnd
- jiffy_elapsed
);
604 static bool tg_no_rule_group(struct throtl_grp
*tg
, bool rw
) {
605 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1)
611 * Returns whether one can dispatch a bio or not. Also returns approx number
612 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
614 static bool tg_may_dispatch(struct throtl_data
*td
, struct throtl_grp
*tg
,
615 struct bio
*bio
, unsigned long *wait
)
617 bool rw
= bio_data_dir(bio
);
618 unsigned long bps_wait
= 0, iops_wait
= 0, max_wait
= 0;
621 * Currently whole state machine of group depends on first bio
622 * queued in the group bio list. So one should not be calling
623 * this function with a different bio if there are other bios
626 BUG_ON(tg
->nr_queued
[rw
] && bio
!= bio_list_peek(&tg
->bio_lists
[rw
]));
628 /* If tg->bps = -1, then BW is unlimited */
629 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1) {
636 * If previous slice expired, start a new one otherwise renew/extend
637 * existing slice to make sure it is at least throtl_slice interval
640 if (throtl_slice_used(td
, tg
, rw
))
641 throtl_start_new_slice(td
, tg
, rw
);
643 if (time_before(tg
->slice_end
[rw
], jiffies
+ throtl_slice
))
644 throtl_extend_slice(td
, tg
, rw
, jiffies
+ throtl_slice
);
647 if (tg_with_in_bps_limit(td
, tg
, bio
, &bps_wait
)
648 && tg_with_in_iops_limit(td
, tg
, bio
, &iops_wait
)) {
654 max_wait
= max(bps_wait
, iops_wait
);
659 if (time_before(tg
->slice_end
[rw
], jiffies
+ max_wait
))
660 throtl_extend_slice(td
, tg
, rw
, jiffies
+ max_wait
);
665 static void throtl_update_dispatch_stats(struct blkio_group
*blkg
, u64 bytes
,
668 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
669 struct tg_stats_cpu
*stats_cpu
;
672 /* If per cpu stats are not allocated yet, don't do any accounting. */
673 if (tg
->stats_cpu
== NULL
)
677 * Disabling interrupts to provide mutual exclusion between two
678 * writes on same cpu. It probably is not needed for 64bit. Not
679 * optimizing that case yet.
681 local_irq_save(flags
);
683 stats_cpu
= this_cpu_ptr(tg
->stats_cpu
);
685 blkg_rwstat_add(&stats_cpu
->serviced
, rw
, 1);
686 blkg_rwstat_add(&stats_cpu
->service_bytes
, rw
, bytes
);
688 local_irq_restore(flags
);
691 static void throtl_charge_bio(struct throtl_grp
*tg
, struct bio
*bio
)
693 bool rw
= bio_data_dir(bio
);
695 /* Charge the bio to the group */
696 tg
->bytes_disp
[rw
] += bio
->bi_size
;
699 throtl_update_dispatch_stats(tg_to_blkg(tg
), bio
->bi_size
, bio
->bi_rw
);
702 static void throtl_add_bio_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
705 bool rw
= bio_data_dir(bio
);
707 bio_list_add(&tg
->bio_lists
[rw
], bio
);
708 /* Take a bio reference on tg */
709 blkg_get(tg_to_blkg(tg
));
712 throtl_enqueue_tg(td
, tg
);
715 static void tg_update_disptime(struct throtl_data
*td
, struct throtl_grp
*tg
)
717 unsigned long read_wait
= -1, write_wait
= -1, min_wait
= -1, disptime
;
720 if ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
721 tg_may_dispatch(td
, tg
, bio
, &read_wait
);
723 if ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
724 tg_may_dispatch(td
, tg
, bio
, &write_wait
);
726 min_wait
= min(read_wait
, write_wait
);
727 disptime
= jiffies
+ min_wait
;
729 /* Update dispatch time */
730 throtl_dequeue_tg(td
, tg
);
731 tg
->disptime
= disptime
;
732 throtl_enqueue_tg(td
, tg
);
735 static void tg_dispatch_one_bio(struct throtl_data
*td
, struct throtl_grp
*tg
,
736 bool rw
, struct bio_list
*bl
)
740 bio
= bio_list_pop(&tg
->bio_lists
[rw
]);
742 /* Drop bio reference on blkg */
743 blkg_put(tg_to_blkg(tg
));
745 BUG_ON(td
->nr_queued
[rw
] <= 0);
748 throtl_charge_bio(tg
, bio
);
749 bio_list_add(bl
, bio
);
750 bio
->bi_rw
|= REQ_THROTTLED
;
752 throtl_trim_slice(td
, tg
, rw
);
755 static int throtl_dispatch_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
758 unsigned int nr_reads
= 0, nr_writes
= 0;
759 unsigned int max_nr_reads
= throtl_grp_quantum
*3/4;
760 unsigned int max_nr_writes
= throtl_grp_quantum
- max_nr_reads
;
763 /* Try to dispatch 75% READS and 25% WRITES */
765 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
]))
766 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
768 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
771 if (nr_reads
>= max_nr_reads
)
775 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
]))
776 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
778 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
781 if (nr_writes
>= max_nr_writes
)
785 return nr_reads
+ nr_writes
;
788 static int throtl_select_dispatch(struct throtl_data
*td
, struct bio_list
*bl
)
790 unsigned int nr_disp
= 0;
791 struct throtl_grp
*tg
;
792 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
795 tg
= throtl_rb_first(st
);
800 if (time_before(jiffies
, tg
->disptime
))
803 throtl_dequeue_tg(td
, tg
);
805 nr_disp
+= throtl_dispatch_tg(td
, tg
, bl
);
807 if (tg
->nr_queued
[0] || tg
->nr_queued
[1]) {
808 tg_update_disptime(td
, tg
);
809 throtl_enqueue_tg(td
, tg
);
812 if (nr_disp
>= throtl_quantum
)
819 static void throtl_process_limit_change(struct throtl_data
*td
)
821 struct request_queue
*q
= td
->queue
;
822 struct blkio_group
*blkg
, *n
;
824 if (!td
->limits_changed
)
827 xchg(&td
->limits_changed
, false);
829 throtl_log(td
, "limits changed");
831 list_for_each_entry_safe(blkg
, n
, &q
->blkg_list
, q_node
) {
832 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
834 if (!tg
->limits_changed
)
837 if (!xchg(&tg
->limits_changed
, false))
840 throtl_log_tg(td
, tg
, "limit change rbps=%llu wbps=%llu"
841 " riops=%u wiops=%u", tg
->bps
[READ
], tg
->bps
[WRITE
],
842 tg
->iops
[READ
], tg
->iops
[WRITE
]);
845 * Restart the slices for both READ and WRITES. It
846 * might happen that a group's limit are dropped
847 * suddenly and we don't want to account recently
848 * dispatched IO with new low rate
850 throtl_start_new_slice(td
, tg
, 0);
851 throtl_start_new_slice(td
, tg
, 1);
853 if (throtl_tg_on_rr(tg
))
854 tg_update_disptime(td
, tg
);
858 /* Dispatch throttled bios. Should be called without queue lock held. */
859 static int throtl_dispatch(struct request_queue
*q
)
861 struct throtl_data
*td
= q
->td
;
862 unsigned int nr_disp
= 0;
863 struct bio_list bio_list_on_stack
;
865 struct blk_plug plug
;
867 spin_lock_irq(q
->queue_lock
);
869 throtl_process_limit_change(td
);
871 if (!total_nr_queued(td
))
874 bio_list_init(&bio_list_on_stack
);
876 throtl_log(td
, "dispatch nr_queued=%u read=%u write=%u",
877 total_nr_queued(td
), td
->nr_queued
[READ
],
878 td
->nr_queued
[WRITE
]);
880 nr_disp
= throtl_select_dispatch(td
, &bio_list_on_stack
);
883 throtl_log(td
, "bios disp=%u", nr_disp
);
885 throtl_schedule_next_dispatch(td
);
887 spin_unlock_irq(q
->queue_lock
);
890 * If we dispatched some requests, unplug the queue to make sure
894 blk_start_plug(&plug
);
895 while((bio
= bio_list_pop(&bio_list_on_stack
)))
896 generic_make_request(bio
);
897 blk_finish_plug(&plug
);
902 void blk_throtl_work(struct work_struct
*work
)
904 struct throtl_data
*td
= container_of(work
, struct throtl_data
,
906 struct request_queue
*q
= td
->queue
;
911 /* Call with queue lock held */
913 throtl_schedule_delayed_work(struct throtl_data
*td
, unsigned long delay
)
916 struct delayed_work
*dwork
= &td
->throtl_work
;
918 /* schedule work if limits changed even if no bio is queued */
919 if (total_nr_queued(td
) || td
->limits_changed
) {
921 * We might have a work scheduled to be executed in future.
922 * Cancel that and schedule a new one.
924 __cancel_delayed_work(dwork
);
925 queue_delayed_work(kthrotld_workqueue
, dwork
, delay
);
926 throtl_log(td
, "schedule work. delay=%lu jiffies=%lu",
931 static u64
tg_prfill_cpu_rwstat(struct seq_file
*sf
, void *pdata
, int off
)
933 struct throtl_grp
*tg
= pdata
;
934 struct blkg_rwstat rwstat
= { }, tmp
;
937 for_each_possible_cpu(cpu
) {
938 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
940 tmp
= blkg_rwstat_read((void *)sc
+ off
);
941 for (i
= 0; i
< BLKG_RWSTAT_NR
; i
++)
942 rwstat
.cnt
[i
] += tmp
.cnt
[i
];
945 return __blkg_prfill_rwstat(sf
, pdata
, &rwstat
);
948 static int tg_print_cpu_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
951 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgrp
);
953 blkcg_print_blkgs(sf
, blkcg
, tg_prfill_cpu_rwstat
, &blkio_policy_throtl
,
958 static u64
tg_prfill_conf_u64(struct seq_file
*sf
, void *pdata
, int off
)
960 u64 v
= *(u64
*)(pdata
+ off
);
964 return __blkg_prfill_u64(sf
, pdata
, v
);
967 static u64
tg_prfill_conf_uint(struct seq_file
*sf
, void *pdata
, int off
)
969 unsigned int v
= *(unsigned int *)(pdata
+ off
);
973 return __blkg_prfill_u64(sf
, pdata
, v
);
976 static int tg_print_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
979 blkcg_print_blkgs(sf
, cgroup_to_blkio_cgroup(cgrp
), tg_prfill_conf_u64
,
980 &blkio_policy_throtl
, cft
->private, false);
984 static int tg_print_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
987 blkcg_print_blkgs(sf
, cgroup_to_blkio_cgroup(cgrp
), tg_prfill_conf_uint
,
988 &blkio_policy_throtl
, cft
->private, false);
992 static int tg_set_conf(struct cgroup
*cgrp
, struct cftype
*cft
, const char *buf
,
995 struct blkio_cgroup
*blkcg
= cgroup_to_blkio_cgroup(cgrp
);
996 struct blkg_conf_ctx ctx
;
997 struct throtl_grp
*tg
;
998 struct throtl_data
*td
;
1001 ret
= blkg_conf_prep(blkcg
, &blkio_policy_throtl
, buf
, &ctx
);
1005 tg
= blkg_to_tg(ctx
.blkg
);
1006 td
= ctx
.blkg
->q
->td
;
1012 *(u64
*)((void *)tg
+ cft
->private) = ctx
.v
;
1014 *(unsigned int *)((void *)tg
+ cft
->private) = ctx
.v
;
1016 /* XXX: we don't need the following deferred processing */
1017 xchg(&tg
->limits_changed
, true);
1018 xchg(&td
->limits_changed
, true);
1019 throtl_schedule_delayed_work(td
, 0);
1021 blkg_conf_finish(&ctx
);
1025 static int tg_set_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1028 return tg_set_conf(cgrp
, cft
, buf
, true);
1031 static int tg_set_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
1034 return tg_set_conf(cgrp
, cft
, buf
, false);
1037 static struct cftype throtl_files
[] = {
1039 .name
= "throttle.read_bps_device",
1040 .private = offsetof(struct throtl_grp
, bps
[READ
]),
1041 .read_seq_string
= tg_print_conf_u64
,
1042 .write_string
= tg_set_conf_u64
,
1043 .max_write_len
= 256,
1046 .name
= "throttle.write_bps_device",
1047 .private = offsetof(struct throtl_grp
, bps
[WRITE
]),
1048 .read_seq_string
= tg_print_conf_u64
,
1049 .write_string
= tg_set_conf_u64
,
1050 .max_write_len
= 256,
1053 .name
= "throttle.read_iops_device",
1054 .private = offsetof(struct throtl_grp
, iops
[READ
]),
1055 .read_seq_string
= tg_print_conf_uint
,
1056 .write_string
= tg_set_conf_uint
,
1057 .max_write_len
= 256,
1060 .name
= "throttle.write_iops_device",
1061 .private = offsetof(struct throtl_grp
, iops
[WRITE
]),
1062 .read_seq_string
= tg_print_conf_uint
,
1063 .write_string
= tg_set_conf_uint
,
1064 .max_write_len
= 256,
1067 .name
= "throttle.io_service_bytes",
1068 .private = offsetof(struct tg_stats_cpu
, service_bytes
),
1069 .read_seq_string
= tg_print_cpu_rwstat
,
1072 .name
= "throttle.io_serviced",
1073 .private = offsetof(struct tg_stats_cpu
, serviced
),
1074 .read_seq_string
= tg_print_cpu_rwstat
,
1079 static void throtl_shutdown_wq(struct request_queue
*q
)
1081 struct throtl_data
*td
= q
->td
;
1083 cancel_delayed_work_sync(&td
->throtl_work
);
1086 static struct blkio_policy_type blkio_policy_throtl
= {
1088 .blkio_init_group_fn
= throtl_init_blkio_group
,
1089 .blkio_exit_group_fn
= throtl_exit_blkio_group
,
1090 .blkio_reset_group_stats_fn
= throtl_reset_group_stats
,
1092 .pdata_size
= sizeof(struct throtl_grp
),
1093 .cftypes
= throtl_files
,
1096 bool blk_throtl_bio(struct request_queue
*q
, struct bio
*bio
)
1098 struct throtl_data
*td
= q
->td
;
1099 struct throtl_grp
*tg
;
1100 bool rw
= bio_data_dir(bio
), update_disptime
= true;
1101 struct blkio_cgroup
*blkcg
;
1102 bool throttled
= false;
1104 if (bio
->bi_rw
& REQ_THROTTLED
) {
1105 bio
->bi_rw
&= ~REQ_THROTTLED
;
1109 /* bio_associate_current() needs ioc, try creating */
1110 create_io_context(GFP_ATOMIC
, q
->node
);
1113 * A throtl_grp pointer retrieved under rcu can be used to access
1114 * basic fields like stats and io rates. If a group has no rules,
1115 * just update the dispatch stats in lockless manner and return.
1118 blkcg
= bio_blkio_cgroup(bio
);
1119 tg
= throtl_lookup_tg(td
, blkcg
);
1121 if (tg_no_rule_group(tg
, rw
)) {
1122 throtl_update_dispatch_stats(tg_to_blkg(tg
),
1123 bio
->bi_size
, bio
->bi_rw
);
1124 goto out_unlock_rcu
;
1129 * Either group has not been allocated yet or it is not an unlimited
1132 spin_lock_irq(q
->queue_lock
);
1133 tg
= throtl_lookup_create_tg(td
, blkcg
);
1137 if (tg
->nr_queued
[rw
]) {
1139 * There is already another bio queued in same dir. No
1140 * need to update dispatch time.
1142 update_disptime
= false;
1147 /* Bio is with-in rate limit of group */
1148 if (tg_may_dispatch(td
, tg
, bio
, NULL
)) {
1149 throtl_charge_bio(tg
, bio
);
1152 * We need to trim slice even when bios are not being queued
1153 * otherwise it might happen that a bio is not queued for
1154 * a long time and slice keeps on extending and trim is not
1155 * called for a long time. Now if limits are reduced suddenly
1156 * we take into account all the IO dispatched so far at new
1157 * low rate and * newly queued IO gets a really long dispatch
1160 * So keep on trimming slice even if bio is not queued.
1162 throtl_trim_slice(td
, tg
, rw
);
1167 throtl_log_tg(td
, tg
, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1168 " iodisp=%u iops=%u queued=%d/%d",
1169 rw
== READ
? 'R' : 'W',
1170 tg
->bytes_disp
[rw
], bio
->bi_size
, tg
->bps
[rw
],
1171 tg
->io_disp
[rw
], tg
->iops
[rw
],
1172 tg
->nr_queued
[READ
], tg
->nr_queued
[WRITE
]);
1174 bio_associate_current(bio
);
1175 throtl_add_bio_tg(q
->td
, tg
, bio
);
1178 if (update_disptime
) {
1179 tg_update_disptime(td
, tg
);
1180 throtl_schedule_next_dispatch(td
);
1184 spin_unlock_irq(q
->queue_lock
);
1192 * blk_throtl_drain - drain throttled bios
1193 * @q: request_queue to drain throttled bios for
1195 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1197 void blk_throtl_drain(struct request_queue
*q
)
1198 __releases(q
->queue_lock
) __acquires(q
->queue_lock
)
1200 struct throtl_data
*td
= q
->td
;
1201 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
1202 struct throtl_grp
*tg
;
1206 WARN_ON_ONCE(!queue_is_locked(q
));
1210 while ((tg
= throtl_rb_first(st
))) {
1211 throtl_dequeue_tg(td
, tg
);
1213 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
1214 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1215 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
1216 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1218 spin_unlock_irq(q
->queue_lock
);
1220 while ((bio
= bio_list_pop(&bl
)))
1221 generic_make_request(bio
);
1223 spin_lock_irq(q
->queue_lock
);
1226 int blk_throtl_init(struct request_queue
*q
)
1228 struct throtl_data
*td
;
1231 td
= kzalloc_node(sizeof(*td
), GFP_KERNEL
, q
->node
);
1235 td
->tg_service_tree
= THROTL_RB_ROOT
;
1236 td
->limits_changed
= false;
1237 INIT_DELAYED_WORK(&td
->throtl_work
, blk_throtl_work
);
1242 /* activate policy */
1243 ret
= blkcg_activate_policy(q
, &blkio_policy_throtl
);
1249 void blk_throtl_exit(struct request_queue
*q
)
1252 throtl_shutdown_wq(q
);
1253 blkcg_deactivate_policy(q
, &blkio_policy_throtl
);
1257 static int __init
throtl_init(void)
1259 kthrotld_workqueue
= alloc_workqueue("kthrotld", WQ_MEM_RECLAIM
, 0);
1260 if (!kthrotld_workqueue
)
1261 panic("Failed to create kthrotld\n");
1263 return blkio_policy_register(&blkio_policy_throtl
);
1266 module_init(throtl_init
);