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)
44 /* List of throtl groups on the request queue*/
45 struct hlist_node tg_node
;
47 /* active throtl group service_tree member */
48 struct rb_node rb_node
;
51 * Dispatch time in jiffies. This is the estimated time when group
52 * will unthrottle and is ready to dispatch more bio. It is used as
53 * key to sort active groups in service tree.
55 unsigned long disptime
;
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 */
86 /* List of throtl groups */
87 struct hlist_head tg_list
;
89 /* service tree for active throtl groups */
90 struct throtl_rb_root tg_service_tree
;
92 struct throtl_grp
*root_tg
;
93 struct request_queue
*queue
;
95 /* Total Number of queued bios on READ and WRITE lists */
96 unsigned int nr_queued
[2];
99 * number of total undestroyed groups
101 unsigned int nr_undestroyed_grps
;
103 /* Work for dispatching throttled bios */
104 struct delayed_work throtl_work
;
109 static inline struct throtl_grp
*blkg_to_tg(struct blkio_group
*blkg
)
111 return blkg_to_pdata(blkg
, &blkio_policy_throtl
);
114 static inline struct blkio_group
*tg_to_blkg(struct throtl_grp
*tg
)
116 return pdata_to_blkg(tg
, &blkio_policy_throtl
);
119 enum tg_state_flags
{
120 THROTL_TG_FLAG_on_rr
= 0, /* on round-robin busy list */
123 #define THROTL_TG_FNS(name) \
124 static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
126 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
128 static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
130 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
132 static inline int throtl_tg_##name(const struct throtl_grp *tg) \
134 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
137 THROTL_TG_FNS(on_rr
);
139 #define throtl_log_tg(td, tg, fmt, args...) \
140 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
141 blkg_path(tg_to_blkg(tg)), ##args); \
143 #define throtl_log(td, fmt, args...) \
144 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
146 static inline unsigned int total_nr_queued(struct throtl_data
*td
)
148 return td
->nr_queued
[0] + td
->nr_queued
[1];
151 static void throtl_init_blkio_group(struct blkio_group
*blkg
)
153 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
155 INIT_HLIST_NODE(&tg
->tg_node
);
156 RB_CLEAR_NODE(&tg
->rb_node
);
157 bio_list_init(&tg
->bio_lists
[0]);
158 bio_list_init(&tg
->bio_lists
[1]);
159 tg
->limits_changed
= false;
164 tg
->iops
[WRITE
] = -1;
167 static void throtl_link_blkio_group(struct request_queue
*q
,
168 struct blkio_group
*blkg
)
170 struct throtl_data
*td
= q
->td
;
171 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
173 hlist_add_head(&tg
->tg_node
, &td
->tg_list
);
174 td
->nr_undestroyed_grps
++;
178 throtl_grp
*throtl_lookup_tg(struct throtl_data
*td
, struct blkio_cgroup
*blkcg
)
181 * This is the common case when there are no blkio cgroups.
182 * Avoid lookup in this case
184 if (blkcg
== &blkio_root_cgroup
)
187 return blkg_to_tg(blkg_lookup(blkcg
, td
->queue
, BLKIO_POLICY_THROTL
));
190 static struct throtl_grp
*throtl_lookup_create_tg(struct throtl_data
*td
,
191 struct blkio_cgroup
*blkcg
)
193 struct request_queue
*q
= td
->queue
;
194 struct throtl_grp
*tg
= NULL
;
197 * This is the common case when there are no blkio cgroups.
198 * Avoid lookup in this case
200 if (blkcg
== &blkio_root_cgroup
) {
203 struct blkio_group
*blkg
;
205 blkg
= blkg_lookup_create(blkcg
, q
, BLKIO_POLICY_THROTL
, false);
207 /* if %NULL and @q is alive, fall back to root_tg */
209 tg
= blkg_to_tg(blkg
);
210 else if (!blk_queue_dead(q
))
217 static struct throtl_grp
*throtl_rb_first(struct throtl_rb_root
*root
)
219 /* Service tree is empty */
224 root
->left
= rb_first(&root
->rb
);
227 return rb_entry_tg(root
->left
);
232 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
238 static void throtl_rb_erase(struct rb_node
*n
, struct throtl_rb_root
*root
)
242 rb_erase_init(n
, &root
->rb
);
246 static void update_min_dispatch_time(struct throtl_rb_root
*st
)
248 struct throtl_grp
*tg
;
250 tg
= throtl_rb_first(st
);
254 st
->min_disptime
= tg
->disptime
;
258 tg_service_tree_add(struct throtl_rb_root
*st
, struct throtl_grp
*tg
)
260 struct rb_node
**node
= &st
->rb
.rb_node
;
261 struct rb_node
*parent
= NULL
;
262 struct throtl_grp
*__tg
;
263 unsigned long key
= tg
->disptime
;
266 while (*node
!= NULL
) {
268 __tg
= rb_entry_tg(parent
);
270 if (time_before(key
, __tg
->disptime
))
271 node
= &parent
->rb_left
;
273 node
= &parent
->rb_right
;
279 st
->left
= &tg
->rb_node
;
281 rb_link_node(&tg
->rb_node
, parent
, node
);
282 rb_insert_color(&tg
->rb_node
, &st
->rb
);
285 static void __throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
287 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
289 tg_service_tree_add(st
, tg
);
290 throtl_mark_tg_on_rr(tg
);
294 static void throtl_enqueue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
296 if (!throtl_tg_on_rr(tg
))
297 __throtl_enqueue_tg(td
, tg
);
300 static void __throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
302 throtl_rb_erase(&tg
->rb_node
, &td
->tg_service_tree
);
303 throtl_clear_tg_on_rr(tg
);
306 static void throtl_dequeue_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
308 if (throtl_tg_on_rr(tg
))
309 __throtl_dequeue_tg(td
, tg
);
312 static void throtl_schedule_next_dispatch(struct throtl_data
*td
)
314 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
317 * If there are more bios pending, schedule more work.
319 if (!total_nr_queued(td
))
324 update_min_dispatch_time(st
);
326 if (time_before_eq(st
->min_disptime
, jiffies
))
327 throtl_schedule_delayed_work(td
, 0);
329 throtl_schedule_delayed_work(td
, (st
->min_disptime
- jiffies
));
333 throtl_start_new_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
335 tg
->bytes_disp
[rw
] = 0;
337 tg
->slice_start
[rw
] = jiffies
;
338 tg
->slice_end
[rw
] = jiffies
+ throtl_slice
;
339 throtl_log_tg(td
, tg
, "[%c] new slice start=%lu end=%lu jiffies=%lu",
340 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
341 tg
->slice_end
[rw
], jiffies
);
344 static inline void throtl_set_slice_end(struct throtl_data
*td
,
345 struct throtl_grp
*tg
, bool rw
, unsigned long jiffy_end
)
347 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
350 static inline void throtl_extend_slice(struct throtl_data
*td
,
351 struct throtl_grp
*tg
, bool rw
, unsigned long jiffy_end
)
353 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
354 throtl_log_tg(td
, tg
, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
355 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
356 tg
->slice_end
[rw
], jiffies
);
359 /* Determine if previously allocated or extended slice is complete or not */
361 throtl_slice_used(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
363 if (time_in_range(jiffies
, tg
->slice_start
[rw
], tg
->slice_end
[rw
]))
369 /* Trim the used slices and adjust slice start accordingly */
371 throtl_trim_slice(struct throtl_data
*td
, struct throtl_grp
*tg
, bool rw
)
373 unsigned long nr_slices
, time_elapsed
, io_trim
;
376 BUG_ON(time_before(tg
->slice_end
[rw
], tg
->slice_start
[rw
]));
379 * If bps are unlimited (-1), then time slice don't get
380 * renewed. Don't try to trim the slice if slice is used. A new
381 * slice will start when appropriate.
383 if (throtl_slice_used(td
, tg
, rw
))
387 * A bio has been dispatched. Also adjust slice_end. It might happen
388 * that initially cgroup limit was very low resulting in high
389 * slice_end, but later limit was bumped up and bio was dispached
390 * sooner, then we need to reduce slice_end. A high bogus slice_end
391 * is bad because it does not allow new slice to start.
394 throtl_set_slice_end(td
, tg
, rw
, jiffies
+ throtl_slice
);
396 time_elapsed
= jiffies
- tg
->slice_start
[rw
];
398 nr_slices
= time_elapsed
/ throtl_slice
;
402 tmp
= tg
->bps
[rw
] * throtl_slice
* nr_slices
;
406 io_trim
= (tg
->iops
[rw
] * throtl_slice
* nr_slices
)/HZ
;
408 if (!bytes_trim
&& !io_trim
)
411 if (tg
->bytes_disp
[rw
] >= bytes_trim
)
412 tg
->bytes_disp
[rw
] -= bytes_trim
;
414 tg
->bytes_disp
[rw
] = 0;
416 if (tg
->io_disp
[rw
] >= io_trim
)
417 tg
->io_disp
[rw
] -= io_trim
;
421 tg
->slice_start
[rw
] += nr_slices
* throtl_slice
;
423 throtl_log_tg(td
, tg
, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
424 " start=%lu end=%lu jiffies=%lu",
425 rw
== READ
? 'R' : 'W', nr_slices
, bytes_trim
, io_trim
,
426 tg
->slice_start
[rw
], tg
->slice_end
[rw
], jiffies
);
429 static bool tg_with_in_iops_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
430 struct bio
*bio
, unsigned long *wait
)
432 bool rw
= bio_data_dir(bio
);
433 unsigned int io_allowed
;
434 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
437 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
439 /* Slice has just started. Consider one slice interval */
441 jiffy_elapsed_rnd
= throtl_slice
;
443 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
446 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
447 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
448 * will allow dispatch after 1 second and after that slice should
452 tmp
= (u64
)tg
->iops
[rw
] * jiffy_elapsed_rnd
;
456 io_allowed
= UINT_MAX
;
460 if (tg
->io_disp
[rw
] + 1 <= io_allowed
) {
466 /* Calc approx time to dispatch */
467 jiffy_wait
= ((tg
->io_disp
[rw
] + 1) * HZ
)/tg
->iops
[rw
] + 1;
469 if (jiffy_wait
> jiffy_elapsed
)
470 jiffy_wait
= jiffy_wait
- jiffy_elapsed
;
479 static bool tg_with_in_bps_limit(struct throtl_data
*td
, struct throtl_grp
*tg
,
480 struct bio
*bio
, unsigned long *wait
)
482 bool rw
= bio_data_dir(bio
);
483 u64 bytes_allowed
, extra_bytes
, tmp
;
484 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
486 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
488 /* Slice has just started. Consider one slice interval */
490 jiffy_elapsed_rnd
= throtl_slice
;
492 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
494 tmp
= tg
->bps
[rw
] * jiffy_elapsed_rnd
;
498 if (tg
->bytes_disp
[rw
] + bio
->bi_size
<= bytes_allowed
) {
504 /* Calc approx time to dispatch */
505 extra_bytes
= tg
->bytes_disp
[rw
] + bio
->bi_size
- bytes_allowed
;
506 jiffy_wait
= div64_u64(extra_bytes
* HZ
, tg
->bps
[rw
]);
512 * This wait time is without taking into consideration the rounding
513 * up we did. Add that time also.
515 jiffy_wait
= jiffy_wait
+ (jiffy_elapsed_rnd
- jiffy_elapsed
);
521 static bool tg_no_rule_group(struct throtl_grp
*tg
, bool rw
) {
522 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1)
528 * Returns whether one can dispatch a bio or not. Also returns approx number
529 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
531 static bool tg_may_dispatch(struct throtl_data
*td
, struct throtl_grp
*tg
,
532 struct bio
*bio
, unsigned long *wait
)
534 bool rw
= bio_data_dir(bio
);
535 unsigned long bps_wait
= 0, iops_wait
= 0, max_wait
= 0;
538 * Currently whole state machine of group depends on first bio
539 * queued in the group bio list. So one should not be calling
540 * this function with a different bio if there are other bios
543 BUG_ON(tg
->nr_queued
[rw
] && bio
!= bio_list_peek(&tg
->bio_lists
[rw
]));
545 /* If tg->bps = -1, then BW is unlimited */
546 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1) {
553 * If previous slice expired, start a new one otherwise renew/extend
554 * existing slice to make sure it is at least throtl_slice interval
557 if (throtl_slice_used(td
, tg
, rw
))
558 throtl_start_new_slice(td
, tg
, rw
);
560 if (time_before(tg
->slice_end
[rw
], jiffies
+ throtl_slice
))
561 throtl_extend_slice(td
, tg
, rw
, jiffies
+ throtl_slice
);
564 if (tg_with_in_bps_limit(td
, tg
, bio
, &bps_wait
)
565 && tg_with_in_iops_limit(td
, tg
, bio
, &iops_wait
)) {
571 max_wait
= max(bps_wait
, iops_wait
);
576 if (time_before(tg
->slice_end
[rw
], jiffies
+ max_wait
))
577 throtl_extend_slice(td
, tg
, rw
, jiffies
+ max_wait
);
582 static void throtl_charge_bio(struct throtl_grp
*tg
, struct bio
*bio
)
584 bool rw
= bio_data_dir(bio
);
585 bool sync
= rw_is_sync(bio
->bi_rw
);
587 /* Charge the bio to the group */
588 tg
->bytes_disp
[rw
] += bio
->bi_size
;
591 blkiocg_update_dispatch_stats(tg_to_blkg(tg
), bio
->bi_size
, rw
, sync
);
594 static void throtl_add_bio_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
597 bool rw
= bio_data_dir(bio
);
599 bio_list_add(&tg
->bio_lists
[rw
], bio
);
600 /* Take a bio reference on tg */
601 blkg_get(tg_to_blkg(tg
));
604 throtl_enqueue_tg(td
, tg
);
607 static void tg_update_disptime(struct throtl_data
*td
, struct throtl_grp
*tg
)
609 unsigned long read_wait
= -1, write_wait
= -1, min_wait
= -1, disptime
;
612 if ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
613 tg_may_dispatch(td
, tg
, bio
, &read_wait
);
615 if ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
616 tg_may_dispatch(td
, tg
, bio
, &write_wait
);
618 min_wait
= min(read_wait
, write_wait
);
619 disptime
= jiffies
+ min_wait
;
621 /* Update dispatch time */
622 throtl_dequeue_tg(td
, tg
);
623 tg
->disptime
= disptime
;
624 throtl_enqueue_tg(td
, tg
);
627 static void tg_dispatch_one_bio(struct throtl_data
*td
, struct throtl_grp
*tg
,
628 bool rw
, struct bio_list
*bl
)
632 bio
= bio_list_pop(&tg
->bio_lists
[rw
]);
634 /* Drop bio reference on blkg */
635 blkg_put(tg_to_blkg(tg
));
637 BUG_ON(td
->nr_queued
[rw
] <= 0);
640 throtl_charge_bio(tg
, bio
);
641 bio_list_add(bl
, bio
);
642 bio
->bi_rw
|= REQ_THROTTLED
;
644 throtl_trim_slice(td
, tg
, rw
);
647 static int throtl_dispatch_tg(struct throtl_data
*td
, struct throtl_grp
*tg
,
650 unsigned int nr_reads
= 0, nr_writes
= 0;
651 unsigned int max_nr_reads
= throtl_grp_quantum
*3/4;
652 unsigned int max_nr_writes
= throtl_grp_quantum
- max_nr_reads
;
655 /* Try to dispatch 75% READS and 25% WRITES */
657 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
]))
658 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
660 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
663 if (nr_reads
>= max_nr_reads
)
667 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
]))
668 && tg_may_dispatch(td
, tg
, bio
, NULL
)) {
670 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), bl
);
673 if (nr_writes
>= max_nr_writes
)
677 return nr_reads
+ nr_writes
;
680 static int throtl_select_dispatch(struct throtl_data
*td
, struct bio_list
*bl
)
682 unsigned int nr_disp
= 0;
683 struct throtl_grp
*tg
;
684 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
687 tg
= throtl_rb_first(st
);
692 if (time_before(jiffies
, tg
->disptime
))
695 throtl_dequeue_tg(td
, tg
);
697 nr_disp
+= throtl_dispatch_tg(td
, tg
, bl
);
699 if (tg
->nr_queued
[0] || tg
->nr_queued
[1]) {
700 tg_update_disptime(td
, tg
);
701 throtl_enqueue_tg(td
, tg
);
704 if (nr_disp
>= throtl_quantum
)
711 static void throtl_process_limit_change(struct throtl_data
*td
)
713 struct throtl_grp
*tg
;
714 struct hlist_node
*pos
, *n
;
716 if (!td
->limits_changed
)
719 xchg(&td
->limits_changed
, false);
721 throtl_log(td
, "limits changed");
723 hlist_for_each_entry_safe(tg
, pos
, n
, &td
->tg_list
, tg_node
) {
724 if (!tg
->limits_changed
)
727 if (!xchg(&tg
->limits_changed
, false))
730 throtl_log_tg(td
, tg
, "limit change rbps=%llu wbps=%llu"
731 " riops=%u wiops=%u", tg
->bps
[READ
], tg
->bps
[WRITE
],
732 tg
->iops
[READ
], tg
->iops
[WRITE
]);
735 * Restart the slices for both READ and WRITES. It
736 * might happen that a group's limit are dropped
737 * suddenly and we don't want to account recently
738 * dispatched IO with new low rate
740 throtl_start_new_slice(td
, tg
, 0);
741 throtl_start_new_slice(td
, tg
, 1);
743 if (throtl_tg_on_rr(tg
))
744 tg_update_disptime(td
, tg
);
748 /* Dispatch throttled bios. Should be called without queue lock held. */
749 static int throtl_dispatch(struct request_queue
*q
)
751 struct throtl_data
*td
= q
->td
;
752 unsigned int nr_disp
= 0;
753 struct bio_list bio_list_on_stack
;
755 struct blk_plug plug
;
757 spin_lock_irq(q
->queue_lock
);
759 throtl_process_limit_change(td
);
761 if (!total_nr_queued(td
))
764 bio_list_init(&bio_list_on_stack
);
766 throtl_log(td
, "dispatch nr_queued=%u read=%u write=%u",
767 total_nr_queued(td
), td
->nr_queued
[READ
],
768 td
->nr_queued
[WRITE
]);
770 nr_disp
= throtl_select_dispatch(td
, &bio_list_on_stack
);
773 throtl_log(td
, "bios disp=%u", nr_disp
);
775 throtl_schedule_next_dispatch(td
);
777 spin_unlock_irq(q
->queue_lock
);
780 * If we dispatched some requests, unplug the queue to make sure
784 blk_start_plug(&plug
);
785 while((bio
= bio_list_pop(&bio_list_on_stack
)))
786 generic_make_request(bio
);
787 blk_finish_plug(&plug
);
792 void blk_throtl_work(struct work_struct
*work
)
794 struct throtl_data
*td
= container_of(work
, struct throtl_data
,
796 struct request_queue
*q
= td
->queue
;
801 /* Call with queue lock held */
803 throtl_schedule_delayed_work(struct throtl_data
*td
, unsigned long delay
)
806 struct delayed_work
*dwork
= &td
->throtl_work
;
808 /* schedule work if limits changed even if no bio is queued */
809 if (total_nr_queued(td
) || td
->limits_changed
) {
811 * We might have a work scheduled to be executed in future.
812 * Cancel that and schedule a new one.
814 __cancel_delayed_work(dwork
);
815 queue_delayed_work(kthrotld_workqueue
, dwork
, delay
);
816 throtl_log(td
, "schedule work. delay=%lu jiffies=%lu",
822 throtl_destroy_tg(struct throtl_data
*td
, struct throtl_grp
*tg
)
824 /* Something wrong if we are trying to remove same group twice */
825 BUG_ON(hlist_unhashed(&tg
->tg_node
));
827 hlist_del_init(&tg
->tg_node
);
830 * Put the reference taken at the time of creation so that when all
831 * queues are gone, group can be destroyed.
833 blkg_put(tg_to_blkg(tg
));
834 td
->nr_undestroyed_grps
--;
837 static bool throtl_release_tgs(struct throtl_data
*td
, bool release_root
)
839 struct hlist_node
*pos
, *n
;
840 struct throtl_grp
*tg
;
843 hlist_for_each_entry_safe(tg
, pos
, n
, &td
->tg_list
, tg_node
) {
845 if (!release_root
&& tg
== td
->root_tg
)
849 * If cgroup removal path got to blk_group first and removed
850 * it from cgroup list, then it will take care of destroying
853 if (!blkiocg_del_blkio_group(tg_to_blkg(tg
)))
854 throtl_destroy_tg(td
, tg
);
862 * Blk cgroup controller notification saying that blkio_group object is being
863 * delinked as associated cgroup object is going away. That also means that
864 * no new IO will come in this group. So get rid of this group as soon as
865 * any pending IO in the group is finished.
867 * This function is called under rcu_read_lock(). @q is the rcu protected
868 * pointer. That means @q is a valid request_queue pointer as long as we
871 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
872 * it should not be NULL as even if queue was going away, cgroup deltion
873 * path got to it first.
875 void throtl_unlink_blkio_group(struct request_queue
*q
,
876 struct blkio_group
*blkg
)
880 spin_lock_irqsave(q
->queue_lock
, flags
);
881 throtl_destroy_tg(q
->td
, blkg_to_tg(blkg
));
882 spin_unlock_irqrestore(q
->queue_lock
, flags
);
885 static bool throtl_clear_queue(struct request_queue
*q
)
887 lockdep_assert_held(q
->queue_lock
);
890 * Clear tgs but leave the root one alone. This is necessary
891 * because root_tg is expected to be persistent and safe because
892 * blk-throtl can never be disabled while @q is alive. This is a
893 * kludge to prepare for unified blkg. This whole function will be
896 return throtl_release_tgs(q
->td
, false);
899 static void throtl_update_blkio_group_common(struct throtl_data
*td
,
900 struct throtl_grp
*tg
)
902 xchg(&tg
->limits_changed
, true);
903 xchg(&td
->limits_changed
, true);
904 /* Schedule a work now to process the limit change */
905 throtl_schedule_delayed_work(td
, 0);
909 * For all update functions, @q should be a valid pointer because these
910 * update functions are called under blkcg_lock, that means, blkg is
911 * valid and in turn @q is valid. queue exit path can not race because
914 * Can not take queue lock in update functions as queue lock under blkcg_lock
915 * is not allowed. Under other paths we take blkcg_lock under queue_lock.
917 static void throtl_update_blkio_group_read_bps(struct request_queue
*q
,
918 struct blkio_group
*blkg
, u64 read_bps
)
920 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
922 tg
->bps
[READ
] = read_bps
;
923 throtl_update_blkio_group_common(q
->td
, tg
);
926 static void throtl_update_blkio_group_write_bps(struct request_queue
*q
,
927 struct blkio_group
*blkg
, u64 write_bps
)
929 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
931 tg
->bps
[WRITE
] = write_bps
;
932 throtl_update_blkio_group_common(q
->td
, tg
);
935 static void throtl_update_blkio_group_read_iops(struct request_queue
*q
,
936 struct blkio_group
*blkg
, unsigned int read_iops
)
938 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
940 tg
->iops
[READ
] = read_iops
;
941 throtl_update_blkio_group_common(q
->td
, tg
);
944 static void throtl_update_blkio_group_write_iops(struct request_queue
*q
,
945 struct blkio_group
*blkg
, unsigned int write_iops
)
947 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
949 tg
->iops
[WRITE
] = write_iops
;
950 throtl_update_blkio_group_common(q
->td
, tg
);
953 static void throtl_shutdown_wq(struct request_queue
*q
)
955 struct throtl_data
*td
= q
->td
;
957 cancel_delayed_work_sync(&td
->throtl_work
);
960 static struct blkio_policy_type blkio_policy_throtl
= {
962 .blkio_init_group_fn
= throtl_init_blkio_group
,
963 .blkio_link_group_fn
= throtl_link_blkio_group
,
964 .blkio_unlink_group_fn
= throtl_unlink_blkio_group
,
965 .blkio_clear_queue_fn
= throtl_clear_queue
,
966 .blkio_update_group_read_bps_fn
=
967 throtl_update_blkio_group_read_bps
,
968 .blkio_update_group_write_bps_fn
=
969 throtl_update_blkio_group_write_bps
,
970 .blkio_update_group_read_iops_fn
=
971 throtl_update_blkio_group_read_iops
,
972 .blkio_update_group_write_iops_fn
=
973 throtl_update_blkio_group_write_iops
,
975 .plid
= BLKIO_POLICY_THROTL
,
976 .pdata_size
= sizeof(struct throtl_grp
),
979 bool blk_throtl_bio(struct request_queue
*q
, struct bio
*bio
)
981 struct throtl_data
*td
= q
->td
;
982 struct throtl_grp
*tg
;
983 bool rw
= bio_data_dir(bio
), update_disptime
= true;
984 struct blkio_cgroup
*blkcg
;
985 bool throttled
= false;
987 if (bio
->bi_rw
& REQ_THROTTLED
) {
988 bio
->bi_rw
&= ~REQ_THROTTLED
;
993 * A throtl_grp pointer retrieved under rcu can be used to access
994 * basic fields like stats and io rates. If a group has no rules,
995 * just update the dispatch stats in lockless manner and return.
998 blkcg
= task_blkio_cgroup(current
);
999 tg
= throtl_lookup_tg(td
, blkcg
);
1001 if (tg_no_rule_group(tg
, rw
)) {
1002 blkiocg_update_dispatch_stats(tg_to_blkg(tg
),
1004 rw_is_sync(bio
->bi_rw
));
1005 goto out_unlock_rcu
;
1010 * Either group has not been allocated yet or it is not an unlimited
1013 spin_lock_irq(q
->queue_lock
);
1014 tg
= throtl_lookup_create_tg(td
, blkcg
);
1018 if (tg
->nr_queued
[rw
]) {
1020 * There is already another bio queued in same dir. No
1021 * need to update dispatch time.
1023 update_disptime
= false;
1028 /* Bio is with-in rate limit of group */
1029 if (tg_may_dispatch(td
, tg
, bio
, NULL
)) {
1030 throtl_charge_bio(tg
, bio
);
1033 * We need to trim slice even when bios are not being queued
1034 * otherwise it might happen that a bio is not queued for
1035 * a long time and slice keeps on extending and trim is not
1036 * called for a long time. Now if limits are reduced suddenly
1037 * we take into account all the IO dispatched so far at new
1038 * low rate and * newly queued IO gets a really long dispatch
1041 * So keep on trimming slice even if bio is not queued.
1043 throtl_trim_slice(td
, tg
, rw
);
1048 throtl_log_tg(td
, tg
, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1049 " iodisp=%u iops=%u queued=%d/%d",
1050 rw
== READ
? 'R' : 'W',
1051 tg
->bytes_disp
[rw
], bio
->bi_size
, tg
->bps
[rw
],
1052 tg
->io_disp
[rw
], tg
->iops
[rw
],
1053 tg
->nr_queued
[READ
], tg
->nr_queued
[WRITE
]);
1055 throtl_add_bio_tg(q
->td
, tg
, bio
);
1058 if (update_disptime
) {
1059 tg_update_disptime(td
, tg
);
1060 throtl_schedule_next_dispatch(td
);
1064 spin_unlock_irq(q
->queue_lock
);
1072 * blk_throtl_drain - drain throttled bios
1073 * @q: request_queue to drain throttled bios for
1075 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1077 void blk_throtl_drain(struct request_queue
*q
)
1078 __releases(q
->queue_lock
) __acquires(q
->queue_lock
)
1080 struct throtl_data
*td
= q
->td
;
1081 struct throtl_rb_root
*st
= &td
->tg_service_tree
;
1082 struct throtl_grp
*tg
;
1086 WARN_ON_ONCE(!queue_is_locked(q
));
1090 while ((tg
= throtl_rb_first(st
))) {
1091 throtl_dequeue_tg(td
, tg
);
1093 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
1094 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1095 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
1096 tg_dispatch_one_bio(td
, tg
, bio_data_dir(bio
), &bl
);
1098 spin_unlock_irq(q
->queue_lock
);
1100 while ((bio
= bio_list_pop(&bl
)))
1101 generic_make_request(bio
);
1103 spin_lock_irq(q
->queue_lock
);
1106 int blk_throtl_init(struct request_queue
*q
)
1108 struct throtl_data
*td
;
1109 struct blkio_group
*blkg
;
1111 td
= kzalloc_node(sizeof(*td
), GFP_KERNEL
, q
->node
);
1115 INIT_HLIST_HEAD(&td
->tg_list
);
1116 td
->tg_service_tree
= THROTL_RB_ROOT
;
1117 td
->limits_changed
= false;
1118 INIT_DELAYED_WORK(&td
->throtl_work
, blk_throtl_work
);
1123 /* alloc and init root group. */
1125 spin_lock_irq(q
->queue_lock
);
1127 blkg
= blkg_lookup_create(&blkio_root_cgroup
, q
, BLKIO_POLICY_THROTL
,
1130 td
->root_tg
= blkg_to_tg(blkg
);
1132 spin_unlock_irq(q
->queue_lock
);
1142 void blk_throtl_exit(struct request_queue
*q
)
1144 struct throtl_data
*td
= q
->td
;
1149 throtl_shutdown_wq(q
);
1151 spin_lock_irq(q
->queue_lock
);
1152 throtl_release_tgs(td
, true);
1154 /* If there are other groups */
1155 if (td
->nr_undestroyed_grps
> 0)
1158 spin_unlock_irq(q
->queue_lock
);
1161 * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods.
1162 * Do this wait only if there are other undestroyed groups out
1163 * there (other than root group). This can happen if cgroup deletion
1164 * path claimed the responsibility of cleaning up a group before
1165 * queue cleanup code get to the group.
1167 * Do not call synchronize_rcu() unconditionally as there are drivers
1168 * which create/delete request queue hundreds of times during scan/boot
1169 * and synchronize_rcu() can take significant time and slow down boot.
1175 * Just being safe to make sure after previous flush if some body did
1176 * update limits through cgroup and another work got queued, cancel
1179 throtl_shutdown_wq(q
);
1184 static int __init
throtl_init(void)
1186 kthrotld_workqueue
= alloc_workqueue("kthrotld", WQ_MEM_RECLAIM
, 0);
1187 if (!kthrotld_workqueue
)
1188 panic("Failed to create kthrotld\n");
1190 blkio_policy_register(&blkio_policy_throtl
);
1194 module_init(throtl_init
);