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 /* A workqueue to queue throttle related work */
25 static struct workqueue_struct
*kthrotld_workqueue
;
26 static void throtl_schedule_delayed_work(struct throtl_data
*td
,
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)
42 /* List of throtl groups on the request queue*/
43 struct hlist_node tg_node
;
45 /* active throtl group service_tree member */
46 struct rb_node rb_node
;
49 * Dispatch time in jiffies. This is the estimated time when group
50 * will unthrottle and is ready to dispatch more bio. It is used as
51 * key to sort active groups in service tree.
53 unsigned long disptime
;
55 struct blkio_group blkg
;
59 /* Two lists for READ and WRITE */
60 struct bio_list bio_lists
[2];
62 /* Number of queued bios on READ and WRITE lists */
63 unsigned int nr_queued
[2];
65 /* bytes per second rate limits */
71 /* Number of bytes disptached in current slice */
72 uint64_t bytes_disp
[2];
73 /* Number of bio's dispatched in current slice */
74 unsigned int io_disp
[2];
76 /* When did we start a new slice */
77 unsigned long slice_start
[2];
78 unsigned long slice_end
[2];
80 /* Some throttle limits got updated for the group */
83 struct rcu_head rcu_head
;
88 /* List of throtl groups */
89 struct hlist_head tg_list
;
91 /* service tree for active throtl groups */
92 struct throtl_rb_root tg_service_tree
;
94 struct throtl_grp
*root_tg
;
95 struct request_queue
*queue
;
97 /* Total Number of queued bios on READ and WRITE lists */
98 unsigned int nr_queued
[2];
101 * number of total undestroyed groups
103 unsigned int nr_undestroyed_grps
;
105 /* Work for dispatching throttled bios */
106 struct delayed_work throtl_work
;
111 enum tg_state_flags
{
112 THROTL_TG_FLAG_on_rr
= 0, /* on round-robin busy list */
115 #define THROTL_TG_FNS(name) \
116 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
118 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
120 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
122 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
124 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
126 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
129 THROTL_TG_FNS(on_rr
);
131 #define throtl_log_tg(td, tg, fmt, args...) \
132 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
133 blkg_path(&(tg)->blkg), ##args); \
135 #define throtl_log(td, fmt, args...) \
136 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
138 static inline struct throtl_grp
*tg_of_blkg(struct blkio_group
*blkg
)
141 return container_of(blkg
, struct throtl_grp
, blkg
);
146 static inline unsigned int total_nr_queued(struct throtl_data
*td
)
148 return td
->nr_queued
[0] + td
->nr_queued
[1];
151 static inline struct throtl_grp
*throtl_ref_get_tg(struct throtl_grp
*tg
)
153 atomic_inc(&tg
->ref
);
157 static void throtl_free_tg(struct rcu_head
*head
)
159 struct throtl_grp
*tg
;
161 tg
= container_of(head
, struct throtl_grp
, rcu_head
);
162 free_percpu(tg
->blkg
.stats_cpu
);
166 static void throtl_put_tg(struct throtl_grp
*tg
)
168 BUG_ON(atomic_read(&tg
->ref
) <= 0);
169 if (!atomic_dec_and_test(&tg
->ref
))
173 * A group is freed in rcu manner. But having an rcu lock does not
174 * mean that one can access all the fields of blkg and assume these
175 * are valid. For example, don't try to follow throtl_data and
176 * request queue links.
178 * Having a reference to blkg under an rcu allows acess to only
179 * values local to groups like group stats and group rate limits
181 call_rcu(&tg
->rcu_head
, throtl_free_tg
);
184 static void throtl_init_group(struct throtl_grp
*tg
)
186 INIT_HLIST_NODE(&tg
->tg_node
);
187 RB_CLEAR_NODE(&tg
->rb_node
);
188 bio_list_init(&tg
->bio_lists
[0]);
189 bio_list_init(&tg
->bio_lists
[1]);
190 tg
->limits_changed
= false;
192 /* Practically unlimited BW */
193 tg
->bps
[0] = tg
->bps
[1] = -1;
194 tg
->iops
[0] = tg
->iops
[1] = -1;
197 * Take the initial reference that will be released on destroy
198 * This can be thought of a joint reference by cgroup and
199 * request queue which will be dropped by either request queue
200 * exit or cgroup deletion path depending on who is exiting first.
202 atomic_set(&tg
->ref
, 1);
205 /* Should be called with rcu read lock held (needed for blkcg) */
207 throtl_add_group_to_td_list(struct throtl_data
*td
, struct throtl_grp
*tg
)
209 hlist_add_head(&tg
->tg_node
, &td
->tg_list
);
210 td
->nr_undestroyed_grps
++;
214 __throtl_tg_fill_dev_details(struct throtl_data
*td
, struct throtl_grp
*tg
)
216 struct backing_dev_info
*bdi
= &td
->queue
->backing_dev_info
;
217 unsigned int major
, minor
;
219 if (!tg
|| tg
->blkg
.dev
)
223 * Fill in device details for a group which might not have been
224 * filled at group creation time as queue was being instantiated
225 * and driver had not attached a device yet
227 if (bdi
->dev
&& dev_name(bdi
->dev
)) {
228 sscanf(dev_name(bdi
->dev
), "%u:%u", &major
, &minor
);
229 tg
->blkg
.dev
= MKDEV(major
, minor
);
234 * Should be called with without queue lock held. Here queue lock will be
235 * taken rarely. It will be taken only once during life time of a group
239 throtl_tg_fill_dev_details(struct throtl_data
*td
, struct throtl_grp
*tg
)
241 if (!tg
|| tg
->blkg
.dev
)
244 spin_lock_irq(td
->queue
->queue_lock
);
245 __throtl_tg_fill_dev_details(td
, tg
);
246 spin_unlock_irq(td
->queue
->queue_lock
);
249 static void throtl_init_add_tg_lists(struct throtl_data
*td
,
250 struct throtl_grp
*tg
, struct blkio_cgroup
*blkcg
)
252 __throtl_tg_fill_dev_details(td
, tg
);
254 /* Add group onto cgroup list */
255 blkiocg_add_blkio_group(blkcg
, &tg
->blkg
, (void *)td
,
256 tg
->blkg
.dev
, BLKIO_POLICY_THROTL
);
258 tg
->bps
[READ
] = blkcg_get_read_bps(blkcg
, tg
->blkg
.dev
);
259 tg
->bps
[WRITE
] = blkcg_get_write_bps(blkcg
, tg
->blkg
.dev
);
260 tg
->iops
[READ
] = blkcg_get_read_iops(blkcg
, tg
->blkg
.dev
);
261 tg
->iops
[WRITE
] = blkcg_get_write_iops(blkcg
, tg
->blkg
.dev
);
263 throtl_add_group_to_td_list(td
, tg
);
266 /* Should be called without queue lock and outside of rcu period */
267 static struct throtl_grp
*throtl_alloc_tg(struct throtl_data
*td
)
269 struct throtl_grp
*tg
= NULL
;
272 tg
= kzalloc_node(sizeof(*tg
), GFP_ATOMIC
, td
->queue
->node
);
276 ret
= blkio_alloc_blkg_stats(&tg
->blkg
);
283 throtl_init_group(tg
);
288 throtl_grp
*throtl_find_tg(struct throtl_data
*td
, struct blkio_cgroup
*blkcg
)
290 struct throtl_grp
*tg
= NULL
;
294 * This is the common case when there are no blkio cgroups.
295 * Avoid lookup in this case
297 if (blkcg
== &blkio_root_cgroup
)
300 tg
= tg_of_blkg(blkiocg_lookup_group(blkcg
, key
));
302 __throtl_tg_fill_dev_details(td
, tg
);
306 static struct throtl_grp
*throtl_get_tg(struct throtl_data
*td
,
307 struct blkio_cgroup
*blkcg
)
309 struct throtl_grp
*tg
= NULL
, *__tg
= NULL
;
310 struct request_queue
*q
= td
->queue
;
312 /* no throttling for dead queue */
313 if (unlikely(blk_queue_bypass(q
)))
316 tg
= throtl_find_tg(td
, blkcg
);
320 if (!css_tryget(&blkcg
->css
))
324 * Need to allocate a group. Allocation of group also needs allocation
325 * of per cpu stats which in-turn takes a mutex() and can block. Hence
326 * we need to drop rcu lock and queue_lock before we call alloc.
328 spin_unlock_irq(q
->queue_lock
);
331 tg
= throtl_alloc_tg(td
);
333 /* Group allocated and queue is still alive. take the lock */
335 spin_lock_irq(q
->queue_lock
);
336 css_put(&blkcg
->css
);
338 /* Make sure @q is still alive */
339 if (unlikely(blk_queue_bypass(q
))) {
345 * If some other thread already allocated the group while we were
346 * not holding queue lock, free up the group
348 __tg
= throtl_find_tg(td
, blkcg
);
355 /* Group allocation failed. Account the IO to root group */
361 throtl_init_add_tg_lists(td
, tg
, blkcg
);
365 static struct throtl_grp
*throtl_rb_first(struct throtl_rb_root
*root
)
367 /* Service tree is empty */
372 root
->left
= rb_first(&root
->rb
);
375 return rb_entry_tg(root
->left
);
380 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
386 static void throtl_rb_erase(struct rb_node
*n
, struct throtl_rb_root
*root
)
390 rb_erase_init(n
, &root
->rb
);
394 static void update_min_dispatch_time(struct throtl_rb_root
*st
)
396 struct throtl_grp
*tg
;
398 tg
= throtl_rb_first(st
);
402 st
->min_disptime
= tg
->disptime
;
406 tg_service_tree_add(struct throtl_rb_root
*st
, struct throtl_grp
*tg
)
408 struct rb_node
**node
= &st
->rb
.rb_node
;
409 struct rb_node
*parent
= NULL
;
410 struct throtl_grp
*__tg
;
411 unsigned long key
= tg
->disptime
;
414 while (*node
!= NULL
) {
416 __tg
= rb_entry_tg(parent
);
418 if (time_before(key
, __tg
->disptime
))
419 node
= &parent
->rb_left
;
421 node
= &parent
->rb_right
;
427 st
->left
= &tg
->rb_node
;
429 rb_link_node(&tg
->rb_node
, parent
, node
);
430 rb_insert_color(&tg
->rb_node
, &st
->rb
);
433 static void __throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
435 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
437 tg_service_tree_add(st
, tg
);
438 throtl_mark_tg_on_rr(tg
);
442 static void throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
444 if (!throtl_tg_on_rr(tg
))
445 __throtl_enqueue_tg(td
, tg
);
448 static void __throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
450 throtl_rb_erase(&tg
->rb_node
, &td
->tg_service_tree
);
451 throtl_clear_tg_on_rr(tg
);
454 static void throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
456 if (throtl_tg_on_rr(tg
))
457 __throtl_dequeue_tg(td
, tg
);
460 static void throtl_schedule_next_dispatch(struct throtl_data
*td
)
462 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
465 * If there are more bios pending, schedule more work.
467 if (!total_nr_queued(td
))
472 update_min_dispatch_time(st
);
474 if (time_before_eq(st
->min_disptime
, jiffies
))
475 throtl_schedule_delayed_work(td
, 0);
477 throtl_schedule_delayed_work(td
, (st
->min_disptime
- jiffies
));
481 throtl_start_new_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
483 tg
->bytes_disp
[rw
] = 0;
485 tg
->slice_start
[rw
] = jiffies
;
486 tg
->slice_end
[rw
] = jiffies
+ throtl_slice
;
487 throtl_log_tg(td
, tg
, "[%c] new slice start=%lu end=%lu jiffies=%lu",
488 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
489 tg
->slice_end
[rw
], jiffies
);
492 static inline void throtl_set_slice_end(struct throtl_data
*td
,
493 struct throtl_grp
*tg
, bool rw
, unsigned long jiffy_end
)
495 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
498 static inline void throtl_extend_slice(struct throtl_data
*td
,
499 struct throtl_grp
*tg
, bool rw
, unsigned long jiffy_end
)
501 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
502 throtl_log_tg(td
, tg
, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
503 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
504 tg
->slice_end
[rw
], jiffies
);
507 /* Determine if previously allocated or extended slice is complete or not */
509 throtl_slice_used(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
511 if (time_in_range(jiffies
, tg
->slice_start
[rw
], tg
->slice_end
[rw
]))
517 /* Trim the used slices and adjust slice start accordingly */
519 throtl_trim_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
521 unsigned long nr_slices
, time_elapsed
, io_trim
;
524 BUG_ON(time_before(tg
->slice_end
[rw
], tg
->slice_start
[rw
]));
527 * If bps are unlimited (-1), then time slice don't get
528 * renewed. Don't try to trim the slice if slice is used. A new
529 * slice will start when appropriate.
531 if (throtl_slice_used(td
, tg
, rw
))
535 * A bio has been dispatched. Also adjust slice_end. It might happen
536 * that initially cgroup limit was very low resulting in high
537 * slice_end, but later limit was bumped up and bio was dispached
538 * sooner, then we need to reduce slice_end. A high bogus slice_end
539 * is bad because it does not allow new slice to start.
542 throtl_set_slice_end(td
, tg
, rw
, jiffies
+ throtl_slice
);
544 time_elapsed
= jiffies
- tg
->slice_start
[rw
];
546 nr_slices
= time_elapsed
/ throtl_slice
;
550 tmp
= tg
->bps
[rw
] * throtl_slice
* nr_slices
;
554 io_trim
= (tg
->iops
[rw
] * throtl_slice
* nr_slices
)/HZ
;
556 if (!bytes_trim
&& !io_trim
)
559 if (tg
->bytes_disp
[rw
] >= bytes_trim
)
560 tg
->bytes_disp
[rw
] -= bytes_trim
;
562 tg
->bytes_disp
[rw
] = 0;
564 if (tg
->io_disp
[rw
] >= io_trim
)
565 tg
->io_disp
[rw
] -= io_trim
;
569 tg
->slice_start
[rw
] += nr_slices
* throtl_slice
;
571 throtl_log_tg(td
, tg
, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
572 " start=%lu end=%lu jiffies=%lu",
573 rw
== READ
? 'R' : 'W', nr_slices
, bytes_trim
, io_trim
,
574 tg
->slice_start
[rw
], tg
->slice_end
[rw
], jiffies
);
577 static bool tg_with_in_iops_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
578 struct bio
*bio
, unsigned long *wait
)
580 bool rw
= bio_data_dir(bio
);
581 unsigned int io_allowed
;
582 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
585 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
587 /* Slice has just started. Consider one slice interval */
589 jiffy_elapsed_rnd
= throtl_slice
;
591 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
594 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
595 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
596 * will allow dispatch after 1 second and after that slice should
600 tmp
= (u64
)tg
->iops
[rw
] * jiffy_elapsed_rnd
;
604 io_allowed
= UINT_MAX
;
608 if (tg
->io_disp
[rw
] + 1 <= io_allowed
) {
614 /* Calc approx time to dispatch */
615 jiffy_wait
= ((tg
->io_disp
[rw
] + 1) * HZ
)/tg
->iops
[rw
] + 1;
617 if (jiffy_wait
> jiffy_elapsed
)
618 jiffy_wait
= jiffy_wait
- jiffy_elapsed
;
627 static bool tg_with_in_bps_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
628 struct bio
*bio
, unsigned long *wait
)
630 bool rw
= bio_data_dir(bio
);
631 u64 bytes_allowed
, extra_bytes
, tmp
;
632 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
634 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
636 /* Slice has just started. Consider one slice interval */
638 jiffy_elapsed_rnd
= throtl_slice
;
640 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
642 tmp
= tg
->bps
[rw
] * jiffy_elapsed_rnd
;
646 if (tg
->bytes_disp
[rw
] + bio
->bi_size
<= bytes_allowed
) {
652 /* Calc approx time to dispatch */
653 extra_bytes
= tg
->bytes_disp
[rw
] + bio
->bi_size
- bytes_allowed
;
654 jiffy_wait
= div64_u64(extra_bytes
* HZ
, tg
->bps
[rw
]);
660 * This wait time is without taking into consideration the rounding
661 * up we did. Add that time also.
663 jiffy_wait
= jiffy_wait
+ (jiffy_elapsed_rnd
- jiffy_elapsed
);
669 static bool tg_no_rule_group(struct throtl_grp
*tg
, bool rw
) {
670 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1)
676 * Returns whether one can dispatch a bio or not. Also returns approx number
677 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
679 static bool tg_may_dispatch(struct throtl_data
*td
, struct throtl_grp
*tg
,
680 struct bio
*bio
, unsigned long *wait
)
682 bool rw
= bio_data_dir(bio
);
683 unsigned long bps_wait
= 0, iops_wait
= 0, max_wait
= 0;
686 * Currently whole state machine of group depends on first bio
687 * queued in the group bio list. So one should not be calling
688 * this function with a different bio if there are other bios
691 BUG_ON(tg
->nr_queued
[rw
] && bio
!= bio_list_peek(&tg
->bio_lists
[rw
]));
693 /* If tg->bps = -1, then BW is unlimited */
694 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1) {
701 * If previous slice expired, start a new one otherwise renew/extend
702 * existing slice to make sure it is at least throtl_slice interval
705 if (throtl_slice_used(td
, tg
, rw
))
706 throtl_start_new_slice(td
, tg
, rw
);
708 if (time_before(tg
->slice_end
[rw
], jiffies
+ throtl_slice
))
709 throtl_extend_slice(td
, tg
, rw
, jiffies
+ throtl_slice
);
712 if (tg_with_in_bps_limit(td
, tg
, bio
, &bps_wait
)
713 && tg_with_in_iops_limit(td
, tg
, bio
, &iops_wait
)) {
719 max_wait
= max(bps_wait
, iops_wait
);
724 if (time_before(tg
->slice_end
[rw
], jiffies
+ max_wait
))
725 throtl_extend_slice(td
, tg
, rw
, jiffies
+ max_wait
);
730 static void throtl_charge_bio(struct throtl_grp
*tg
, struct bio
*bio
)
732 bool rw
= bio_data_dir(bio
);
733 bool sync
= rw_is_sync(bio
->bi_rw
);
735 /* Charge the bio to the group */
736 tg
->bytes_disp
[rw
] += bio
->bi_size
;
739 blkiocg_update_dispatch_stats(&tg
->blkg
, bio
->bi_size
, rw
, sync
);
742 static void throtl_add_bio_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
745 bool rw
= bio_data_dir(bio
);
747 bio_list_add(&tg
->bio_lists
[rw
], bio
);
748 /* Take a bio reference on tg */
749 throtl_ref_get_tg(tg
);
752 throtl_enqueue_tg(td
, tg
);
755 static void tg_update_disptime(struct throtl_data
*td
, struct throtl_grp
*tg
)
757 unsigned long read_wait
= -1, write_wait
= -1, min_wait
= -1, disptime
;
760 if ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
761 tg_may_dispatch(td
, tg
, bio
, &read_wait
);
763 if ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
764 tg_may_dispatch(td
, tg
, bio
, &write_wait
);
766 min_wait
= min(read_wait
, write_wait
);
767 disptime
= jiffies
+ min_wait
;
769 /* Update dispatch time */
770 throtl_dequeue_tg(td
, tg
);
771 tg
->disptime
= disptime
;
772 throtl_enqueue_tg(td
, tg
);
775 static void tg_dispatch_one_bio(struct throtl_data
*td
, struct throtl_grp
*tg
,
776 bool rw
, struct bio_list
*bl
)
780 bio
= bio_list_pop(&tg
->bio_lists
[rw
]);
782 /* Drop bio reference on tg */
785 BUG_ON(td
->nr_queued
[rw
] <= 0);
788 throtl_charge_bio(tg
, bio
);
789 bio_list_add(bl
, bio
);
790 bio
->bi_rw
|= REQ_THROTTLED
;
792 throtl_trim_slice(td
, tg
, rw
);
795 static int throtl_dispatch_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
798 unsigned int nr_reads
= 0, nr_writes
= 0;
799 unsigned int max_nr_reads
= throtl_grp_quantum
*3/4;
800 unsigned int max_nr_writes
= throtl_grp_quantum
- max_nr_reads
;
803 /* Try to dispatch 75% READS and 25% WRITES */
805 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
]))
806 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
808 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
811 if (nr_reads
>= max_nr_reads
)
815 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
]))
816 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
818 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
821 if (nr_writes
>= max_nr_writes
)
825 return nr_reads
+ nr_writes
;
828 static int throtl_select_dispatch(struct throtl_data
*td
, struct bio_list
*bl
)
830 unsigned int nr_disp
= 0;
831 struct throtl_grp
*tg
;
832 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
835 tg
= throtl_rb_first(st
);
840 if (time_before(jiffies
, tg
->disptime
))
843 throtl_dequeue_tg(td
, tg
);
845 nr_disp
+= throtl_dispatch_tg(td
, tg
, bl
);
847 if (tg
->nr_queued
[0] || tg
->nr_queued
[1]) {
848 tg_update_disptime(td
, tg
);
849 throtl_enqueue_tg(td
, tg
);
852 if (nr_disp
>= throtl_quantum
)
859 static void throtl_process_limit_change(struct throtl_data
*td
)
861 struct throtl_grp
*tg
;
862 struct hlist_node
*pos
, *n
;
864 if (!td
->limits_changed
)
867 xchg(&td
->limits_changed
, false);
869 throtl_log(td
, "limits changed");
871 hlist_for_each_entry_safe(tg
, pos
, n
, &td
->tg_list
, tg_node
) {
872 if (!tg
->limits_changed
)
875 if (!xchg(&tg
->limits_changed
, false))
878 throtl_log_tg(td
, tg
, "limit change rbps=%llu wbps=%llu"
879 " riops=%u wiops=%u", tg
->bps
[READ
], tg
->bps
[WRITE
],
880 tg
->iops
[READ
], tg
->iops
[WRITE
]);
883 * Restart the slices for both READ and WRITES. It
884 * might happen that a group's limit are dropped
885 * suddenly and we don't want to account recently
886 * dispatched IO with new low rate
888 throtl_start_new_slice(td
, tg
, 0);
889 throtl_start_new_slice(td
, tg
, 1);
891 if (throtl_tg_on_rr(tg
))
892 tg_update_disptime(td
, tg
);
896 /* Dispatch throttled bios. Should be called without queue lock held. */
897 static int throtl_dispatch(struct request_queue
*q
)
899 struct throtl_data
*td
= q
->td
;
900 unsigned int nr_disp
= 0;
901 struct bio_list bio_list_on_stack
;
903 struct blk_plug plug
;
905 spin_lock_irq(q
->queue_lock
);
907 throtl_process_limit_change(td
);
909 if (!total_nr_queued(td
))
912 bio_list_init(&bio_list_on_stack
);
914 throtl_log(td
, "dispatch nr_queued=%u read=%u write=%u",
915 total_nr_queued(td
), td
->nr_queued
[READ
],
916 td
->nr_queued
[WRITE
]);
918 nr_disp
= throtl_select_dispatch(td
, &bio_list_on_stack
);
921 throtl_log(td
, "bios disp=%u", nr_disp
);
923 throtl_schedule_next_dispatch(td
);
925 spin_unlock_irq(q
->queue_lock
);
928 * If we dispatched some requests, unplug the queue to make sure
932 blk_start_plug(&plug
);
933 while((bio
= bio_list_pop(&bio_list_on_stack
)))
934 generic_make_request(bio
);
935 blk_finish_plug(&plug
);
940 void blk_throtl_work(struct work_struct
*work
)
942 struct throtl_data
*td
= container_of(work
, struct throtl_data
,
944 struct request_queue
*q
= td
->queue
;
949 /* Call with queue lock held */
951 throtl_schedule_delayed_work(struct throtl_data
*td
, unsigned long delay
)
954 struct delayed_work
*dwork
= &td
->throtl_work
;
956 /* schedule work if limits changed even if no bio is queued */
957 if (total_nr_queued(td
) || td
->limits_changed
) {
959 * We might have a work scheduled to be executed in future.
960 * Cancel that and schedule a new one.
962 __cancel_delayed_work(dwork
);
963 queue_delayed_work(kthrotld_workqueue
, dwork
, delay
);
964 throtl_log(td
, "schedule work. delay=%lu jiffies=%lu",
970 throtl_destroy_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
972 /* Something wrong if we are trying to remove same group twice */
973 BUG_ON(hlist_unhashed(&tg
->tg_node
));
975 hlist_del_init(&tg
->tg_node
);
978 * Put the reference taken at the time of creation so that when all
979 * queues are gone, group can be destroyed.
982 td
->nr_undestroyed_grps
--;
985 static bool throtl_release_tgs(struct throtl_data
*td
, bool release_root
)
987 struct hlist_node
*pos
, *n
;
988 struct throtl_grp
*tg
;
991 hlist_for_each_entry_safe(tg
, pos
, n
, &td
->tg_list
, tg_node
) {
993 if (!release_root
&& tg
== td
->root_tg
)
997 * If cgroup removal path got to blk_group first and removed
998 * it from cgroup list, then it will take care of destroying
1001 if (!blkiocg_del_blkio_group(&tg
->blkg
))
1002 throtl_destroy_tg(td
, tg
);
1010 * Blk cgroup controller notification saying that blkio_group object is being
1011 * delinked as associated cgroup object is going away. That also means that
1012 * no new IO will come in this group. So get rid of this group as soon as
1013 * any pending IO in the group is finished.
1015 * This function is called under rcu_read_lock(). key is the rcu protected
1016 * pointer. That means "key" is a valid throtl_data pointer as long as we are
1019 * "key" was fetched from blkio_group under blkio_cgroup->lock. That means
1020 * it should not be NULL as even if queue was going away, cgroup deltion
1021 * path got to it first.
1023 void throtl_unlink_blkio_group(void *key
, struct blkio_group
*blkg
)
1025 unsigned long flags
;
1026 struct throtl_data
*td
= key
;
1028 spin_lock_irqsave(td
->queue
->queue_lock
, flags
);
1029 throtl_destroy_tg(td
, tg_of_blkg(blkg
));
1030 spin_unlock_irqrestore(td
->queue
->queue_lock
, flags
);
1033 static bool throtl_clear_queue(struct request_queue
*q
)
1035 lockdep_assert_held(q
->queue_lock
);
1038 * Clear tgs but leave the root one alone. This is necessary
1039 * because root_tg is expected to be persistent and safe because
1040 * blk-throtl can never be disabled while @q is alive. This is a
1041 * kludge to prepare for unified blkg. This whole function will be
1044 return throtl_release_tgs(q
->td
, false);
1047 static void throtl_update_blkio_group_common(struct throtl_data
*td
,
1048 struct throtl_grp
*tg
)
1050 xchg(&tg
->limits_changed
, true);
1051 xchg(&td
->limits_changed
, true);
1052 /* Schedule a work now to process the limit change */
1053 throtl_schedule_delayed_work(td
, 0);
1057 * For all update functions, key should be a valid pointer because these
1058 * update functions are called under blkcg_lock, that means, blkg is
1059 * valid and in turn key is valid. queue exit path can not race because
1062 * Can not take queue lock in update functions as queue lock under blkcg_lock
1063 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
1065 static void throtl_update_blkio_group_read_bps(void *key
,
1066 struct blkio_group
*blkg
, u64 read_bps
)
1068 struct throtl_data
*td
= key
;
1069 struct throtl_grp
*tg
= tg_of_blkg(blkg
);
1071 tg
->bps
[READ
] = read_bps
;
1072 throtl_update_blkio_group_common(td
, tg
);
1075 static void throtl_update_blkio_group_write_bps(void *key
,
1076 struct blkio_group
*blkg
, u64 write_bps
)
1078 struct throtl_data
*td
= key
;
1079 struct throtl_grp
*tg
= tg_of_blkg(blkg
);
1081 tg
->bps
[WRITE
] = write_bps
;
1082 throtl_update_blkio_group_common(td
, tg
);
1085 static void throtl_update_blkio_group_read_iops(void *key
,
1086 struct blkio_group
*blkg
, unsigned int read_iops
)
1088 struct throtl_data
*td
= key
;
1089 struct throtl_grp
*tg
= tg_of_blkg(blkg
);
1091 tg
->iops
[READ
] = read_iops
;
1092 throtl_update_blkio_group_common(td
, tg
);
1095 static void throtl_update_blkio_group_write_iops(void *key
,
1096 struct blkio_group
*blkg
, unsigned int write_iops
)
1098 struct throtl_data
*td
= key
;
1099 struct throtl_grp
*tg
= tg_of_blkg(blkg
);
1101 tg
->iops
[WRITE
] = write_iops
;
1102 throtl_update_blkio_group_common(td
, tg
);
1105 static void throtl_shutdown_wq(struct request_queue
*q
)
1107 struct throtl_data
*td
= q
->td
;
1109 cancel_delayed_work_sync(&td
->throtl_work
);
1112 static struct blkio_policy_type blkio_policy_throtl
= {
1114 .blkio_unlink_group_fn
= throtl_unlink_blkio_group
,
1115 .blkio_clear_queue_fn
= throtl_clear_queue
,
1116 .blkio_update_group_read_bps_fn
=
1117 throtl_update_blkio_group_read_bps
,
1118 .blkio_update_group_write_bps_fn
=
1119 throtl_update_blkio_group_write_bps
,
1120 .blkio_update_group_read_iops_fn
=
1121 throtl_update_blkio_group_read_iops
,
1122 .blkio_update_group_write_iops_fn
=
1123 throtl_update_blkio_group_write_iops
,
1125 .plid
= BLKIO_POLICY_THROTL
,
1128 bool blk_throtl_bio(struct request_queue
*q
, struct bio
*bio
)
1130 struct throtl_data
*td
= q
->td
;
1131 struct throtl_grp
*tg
;
1132 bool rw
= bio_data_dir(bio
), update_disptime
= true;
1133 struct blkio_cgroup
*blkcg
;
1134 bool throttled
= false;
1136 if (bio
->bi_rw
& REQ_THROTTLED
) {
1137 bio
->bi_rw
&= ~REQ_THROTTLED
;
1142 * A throtl_grp pointer retrieved under rcu can be used to access
1143 * basic fields like stats and io rates. If a group has no rules,
1144 * just update the dispatch stats in lockless manner and return.
1147 blkcg
= task_blkio_cgroup(current
);
1148 tg
= throtl_find_tg(td
, blkcg
);
1150 throtl_tg_fill_dev_details(td
, tg
);
1152 if (tg_no_rule_group(tg
, rw
)) {
1153 blkiocg_update_dispatch_stats(&tg
->blkg
, bio
->bi_size
,
1154 rw
, rw_is_sync(bio
->bi_rw
));
1155 goto out_unlock_rcu
;
1160 * Either group has not been allocated yet or it is not an unlimited
1163 spin_lock_irq(q
->queue_lock
);
1164 tg
= throtl_get_tg(td
, blkcg
);
1168 if (tg
->nr_queued
[rw
]) {
1170 * There is already another bio queued in same dir. No
1171 * need to update dispatch time.
1173 update_disptime
= false;
1178 /* Bio is with-in rate limit of group */
1179 if (tg_may_dispatch(td
, tg
, bio
, NULL
)) {
1180 throtl_charge_bio(tg
, bio
);
1183 * We need to trim slice even when bios are not being queued
1184 * otherwise it might happen that a bio is not queued for
1185 * a long time and slice keeps on extending and trim is not
1186 * called for a long time. Now if limits are reduced suddenly
1187 * we take into account all the IO dispatched so far at new
1188 * low rate and * newly queued IO gets a really long dispatch
1191 * So keep on trimming slice even if bio is not queued.
1193 throtl_trim_slice(td
, tg
, rw
);
1198 throtl_log_tg(td
, tg
, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1199 " iodisp=%u iops=%u queued=%d/%d",
1200 rw
== READ
? 'R' : 'W',
1201 tg
->bytes_disp
[rw
], bio
->bi_size
, tg
->bps
[rw
],
1202 tg
->io_disp
[rw
], tg
->iops
[rw
],
1203 tg
->nr_queued
[READ
], tg
->nr_queued
[WRITE
]);
1205 throtl_add_bio_tg(q
->td
, tg
, bio
);
1208 if (update_disptime
) {
1209 tg_update_disptime(td
, tg
);
1210 throtl_schedule_next_dispatch(td
);
1214 spin_unlock_irq(q
->queue_lock
);
1222 * blk_throtl_drain - drain throttled bios
1223 * @q: request_queue to drain throttled bios for
1225 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1227 void blk_throtl_drain(struct request_queue
*q
)
1228 __releases(q
->queue_lock
) __acquires(q
->queue_lock
)
1230 struct throtl_data
*td
= q
->td
;
1231 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
1232 struct throtl_grp
*tg
;
1236 WARN_ON_ONCE(!queue_is_locked(q
));
1240 while ((tg
= throtl_rb_first(st
))) {
1241 throtl_dequeue_tg(td
, tg
);
1243 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
1244 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1245 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
1246 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1248 spin_unlock_irq(q
->queue_lock
);
1250 while ((bio
= bio_list_pop(&bl
)))
1251 generic_make_request(bio
);
1253 spin_lock_irq(q
->queue_lock
);
1256 int blk_throtl_init(struct request_queue
*q
)
1258 struct throtl_data
*td
;
1259 struct throtl_grp
*tg
;
1261 td
= kzalloc_node(sizeof(*td
), GFP_KERNEL
, q
->node
);
1265 INIT_HLIST_HEAD(&td
->tg_list
);
1266 td
->tg_service_tree
= THROTL_RB_ROOT
;
1267 td
->limits_changed
= false;
1268 INIT_DELAYED_WORK(&td
->throtl_work
, blk_throtl_work
);
1270 /* alloc and Init root group. */
1272 tg
= throtl_alloc_tg(td
);
1282 throtl_init_add_tg_lists(td
, tg
, &blkio_root_cgroup
);
1285 /* Attach throtl data to request queue */
1290 void blk_throtl_exit(struct request_queue
*q
)
1292 struct throtl_data
*td
= q
->td
;
1297 throtl_shutdown_wq(q
);
1299 spin_lock_irq(q
->queue_lock
);
1300 throtl_release_tgs(td
, true);
1302 /* If there are other groups */
1303 if (td
->nr_undestroyed_grps
> 0)
1306 spin_unlock_irq(q
->queue_lock
);
1309 * Wait for tg->blkg->key accessors to exit their grace periods.
1310 * Do this wait only if there are other undestroyed groups out
1311 * there (other than root group). This can happen if cgroup deletion
1312 * path claimed the responsibility of cleaning up a group before
1313 * queue cleanup code get to the group.
1315 * Do not call synchronize_rcu() unconditionally as there are drivers
1316 * which create/delete request queue hundreds of times during scan/boot
1317 * and synchronize_rcu() can take significant time and slow down boot.
1323 * Just being safe to make sure after previous flush if some body did
1324 * update limits through cgroup and another work got queued, cancel
1327 throtl_shutdown_wq(q
);
1330 void blk_throtl_release(struct request_queue
*q
)
1335 static int __init
throtl_init(void)
1337 kthrotld_workqueue
= alloc_workqueue("kthrotld", WQ_MEM_RECLAIM
, 0);
1338 if (!kthrotld_workqueue
)
1339 panic("Failed to create kthrotld\n");
1341 blkio_policy_register(&blkio_policy_throtl
);
1345 module_init(throtl_init
);