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 throtl_service_queue
*parent_sq
; /* the parent service_queue */
33 * Bios queued directly to this service_queue or dispatched from
34 * children throtl_grp's.
36 struct bio_list bio_lists
[2]; /* queued bios [READ/WRITE] */
37 unsigned int nr_queued
[2]; /* number of queued bios */
40 * RB tree of active children throtl_grp's, which are sorted by
43 struct rb_root pending_tree
; /* RB tree of active tgs */
44 struct rb_node
*first_pending
; /* first node in the tree */
45 unsigned int nr_pending
; /* # queued in the tree */
46 unsigned long first_pending_disptime
; /* disptime of the first tg */
50 THROTL_TG_PENDING
= 1 << 0, /* on parent's pending tree */
51 THROTL_TG_WAS_EMPTY
= 1 << 1, /* bio_lists[] became non-empty */
54 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
56 /* Per-cpu group stats */
58 /* total bytes transferred */
59 struct blkg_rwstat service_bytes
;
60 /* total IOs serviced, post merge */
61 struct blkg_rwstat serviced
;
65 /* must be the first member */
66 struct blkg_policy_data pd
;
68 /* active throtl group service_queue member */
69 struct rb_node rb_node
;
71 /* throtl_data this group belongs to */
72 struct throtl_data
*td
;
74 /* this group's service queue */
75 struct throtl_service_queue service_queue
;
78 * Dispatch time in jiffies. This is the estimated time when group
79 * will unthrottle and is ready to dispatch more bio. It is used as
80 * key to sort active groups in service tree.
82 unsigned long disptime
;
86 /* bytes per second rate limits */
92 /* Number of bytes disptached in current slice */
93 uint64_t bytes_disp
[2];
94 /* Number of bio's dispatched in current slice */
95 unsigned int io_disp
[2];
97 /* When did we start a new slice */
98 unsigned long slice_start
[2];
99 unsigned long slice_end
[2];
101 /* Per cpu stats pointer */
102 struct tg_stats_cpu __percpu
*stats_cpu
;
104 /* List of tgs waiting for per cpu stats memory to be allocated */
105 struct list_head stats_alloc_node
;
110 /* service tree for active throtl groups */
111 struct throtl_service_queue service_queue
;
113 struct request_queue
*queue
;
115 /* Total Number of queued bios on READ and WRITE lists */
116 unsigned int nr_queued
[2];
119 * number of total undestroyed groups
121 unsigned int nr_undestroyed_grps
;
123 /* Work for dispatching throttled bios */
124 struct delayed_work dispatch_work
;
127 /* list and work item to allocate percpu group stats */
128 static DEFINE_SPINLOCK(tg_stats_alloc_lock
);
129 static LIST_HEAD(tg_stats_alloc_list
);
131 static void tg_stats_alloc_fn(struct work_struct
*);
132 static DECLARE_DELAYED_WORK(tg_stats_alloc_work
, tg_stats_alloc_fn
);
134 static inline struct throtl_grp
*pd_to_tg(struct blkg_policy_data
*pd
)
136 return pd
? container_of(pd
, struct throtl_grp
, pd
) : NULL
;
139 static inline struct throtl_grp
*blkg_to_tg(struct blkcg_gq
*blkg
)
141 return pd_to_tg(blkg_to_pd(blkg
, &blkcg_policy_throtl
));
144 static inline struct blkcg_gq
*tg_to_blkg(struct throtl_grp
*tg
)
146 return pd_to_blkg(&tg
->pd
);
149 static inline struct throtl_grp
*td_root_tg(struct throtl_data
*td
)
151 return blkg_to_tg(td
->queue
->root_blkg
);
155 * sq_to_tg - return the throl_grp the specified service queue belongs to
156 * @sq: the throtl_service_queue of interest
158 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
159 * embedded in throtl_data, %NULL is returned.
161 static struct throtl_grp
*sq_to_tg(struct throtl_service_queue
*sq
)
163 if (sq
&& sq
->parent_sq
)
164 return container_of(sq
, struct throtl_grp
, service_queue
);
170 * sq_to_td - return throtl_data the specified service queue belongs to
171 * @sq: the throtl_service_queue of interest
173 * A service_queue can be embeded in either a throtl_grp or throtl_data.
174 * Determine the associated throtl_data accordingly and return it.
176 static struct throtl_data
*sq_to_td(struct throtl_service_queue
*sq
)
178 struct throtl_grp
*tg
= sq_to_tg(sq
);
183 return container_of(sq
, struct throtl_data
, service_queue
);
187 * throtl_log - log debug message via blktrace
188 * @sq: the service_queue being reported
189 * @fmt: printf format string
192 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
193 * throtl_grp; otherwise, just "throtl".
195 * TODO: this should be made a function and name formatting should happen
196 * after testing whether blktrace is enabled.
198 #define throtl_log(sq, fmt, args...) do { \
199 struct throtl_grp *__tg = sq_to_tg((sq)); \
200 struct throtl_data *__td = sq_to_td((sq)); \
206 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
207 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
209 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
214 * Worker for allocating per cpu stat for tgs. This is scheduled on the
215 * system_wq once there are some groups on the alloc_list waiting for
218 static void tg_stats_alloc_fn(struct work_struct
*work
)
220 static struct tg_stats_cpu
*stats_cpu
; /* this fn is non-reentrant */
221 struct delayed_work
*dwork
= to_delayed_work(work
);
226 stats_cpu
= alloc_percpu(struct tg_stats_cpu
);
228 /* allocation failed, try again after some time */
229 schedule_delayed_work(dwork
, msecs_to_jiffies(10));
234 spin_lock_irq(&tg_stats_alloc_lock
);
236 if (!list_empty(&tg_stats_alloc_list
)) {
237 struct throtl_grp
*tg
= list_first_entry(&tg_stats_alloc_list
,
240 swap(tg
->stats_cpu
, stats_cpu
);
241 list_del_init(&tg
->stats_alloc_node
);
244 empty
= list_empty(&tg_stats_alloc_list
);
245 spin_unlock_irq(&tg_stats_alloc_lock
);
250 /* init a service_queue, assumes the caller zeroed it */
251 static void throtl_service_queue_init(struct throtl_service_queue
*sq
,
252 struct throtl_service_queue
*parent_sq
)
254 bio_list_init(&sq
->bio_lists
[0]);
255 bio_list_init(&sq
->bio_lists
[1]);
256 sq
->pending_tree
= RB_ROOT
;
257 sq
->parent_sq
= parent_sq
;
260 static void throtl_pd_init(struct blkcg_gq
*blkg
)
262 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
263 struct throtl_data
*td
= blkg
->q
->td
;
266 throtl_service_queue_init(&tg
->service_queue
, &td
->service_queue
);
267 RB_CLEAR_NODE(&tg
->rb_node
);
273 tg
->iops
[WRITE
] = -1;
276 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
277 * but percpu allocator can't be called from IO path. Queue tg on
278 * tg_stats_alloc_list and allocate from work item.
280 spin_lock_irqsave(&tg_stats_alloc_lock
, flags
);
281 list_add(&tg
->stats_alloc_node
, &tg_stats_alloc_list
);
282 schedule_delayed_work(&tg_stats_alloc_work
, 0);
283 spin_unlock_irqrestore(&tg_stats_alloc_lock
, flags
);
286 static void throtl_pd_exit(struct blkcg_gq
*blkg
)
288 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
291 spin_lock_irqsave(&tg_stats_alloc_lock
, flags
);
292 list_del_init(&tg
->stats_alloc_node
);
293 spin_unlock_irqrestore(&tg_stats_alloc_lock
, flags
);
295 free_percpu(tg
->stats_cpu
);
298 static void throtl_pd_reset_stats(struct blkcg_gq
*blkg
)
300 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
303 if (tg
->stats_cpu
== NULL
)
306 for_each_possible_cpu(cpu
) {
307 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
309 blkg_rwstat_reset(&sc
->service_bytes
);
310 blkg_rwstat_reset(&sc
->serviced
);
314 static struct throtl_grp
*throtl_lookup_tg(struct throtl_data
*td
,
318 * This is the common case when there are no blkcgs. Avoid lookup
321 if (blkcg
== &blkcg_root
)
322 return td_root_tg(td
);
324 return blkg_to_tg(blkg_lookup(blkcg
, td
->queue
));
327 static struct throtl_grp
*throtl_lookup_create_tg(struct throtl_data
*td
,
330 struct request_queue
*q
= td
->queue
;
331 struct throtl_grp
*tg
= NULL
;
334 * This is the common case when there are no blkcgs. Avoid lookup
337 if (blkcg
== &blkcg_root
) {
340 struct blkcg_gq
*blkg
;
342 blkg
= blkg_lookup_create(blkcg
, q
);
344 /* if %NULL and @q is alive, fall back to root_tg */
346 tg
= blkg_to_tg(blkg
);
347 else if (!blk_queue_dying(q
))
354 static struct throtl_grp
*
355 throtl_rb_first(struct throtl_service_queue
*parent_sq
)
357 /* Service tree is empty */
358 if (!parent_sq
->nr_pending
)
361 if (!parent_sq
->first_pending
)
362 parent_sq
->first_pending
= rb_first(&parent_sq
->pending_tree
);
364 if (parent_sq
->first_pending
)
365 return rb_entry_tg(parent_sq
->first_pending
);
370 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
376 static void throtl_rb_erase(struct rb_node
*n
,
377 struct throtl_service_queue
*parent_sq
)
379 if (parent_sq
->first_pending
== n
)
380 parent_sq
->first_pending
= NULL
;
381 rb_erase_init(n
, &parent_sq
->pending_tree
);
382 --parent_sq
->nr_pending
;
385 static void update_min_dispatch_time(struct throtl_service_queue
*parent_sq
)
387 struct throtl_grp
*tg
;
389 tg
= throtl_rb_first(parent_sq
);
393 parent_sq
->first_pending_disptime
= tg
->disptime
;
396 static void tg_service_queue_add(struct throtl_grp
*tg
)
398 struct throtl_service_queue
*parent_sq
= tg
->service_queue
.parent_sq
;
399 struct rb_node
**node
= &parent_sq
->pending_tree
.rb_node
;
400 struct rb_node
*parent
= NULL
;
401 struct throtl_grp
*__tg
;
402 unsigned long key
= tg
->disptime
;
405 while (*node
!= NULL
) {
407 __tg
= rb_entry_tg(parent
);
409 if (time_before(key
, __tg
->disptime
))
410 node
= &parent
->rb_left
;
412 node
= &parent
->rb_right
;
418 parent_sq
->first_pending
= &tg
->rb_node
;
420 rb_link_node(&tg
->rb_node
, parent
, node
);
421 rb_insert_color(&tg
->rb_node
, &parent_sq
->pending_tree
);
424 static void __throtl_enqueue_tg(struct throtl_grp
*tg
)
426 tg_service_queue_add(tg
);
427 tg
->flags
|= THROTL_TG_PENDING
;
428 tg
->service_queue
.parent_sq
->nr_pending
++;
431 static void throtl_enqueue_tg(struct throtl_grp
*tg
)
433 if (!(tg
->flags
& THROTL_TG_PENDING
))
434 __throtl_enqueue_tg(tg
);
437 static void __throtl_dequeue_tg(struct throtl_grp
*tg
)
439 throtl_rb_erase(&tg
->rb_node
, tg
->service_queue
.parent_sq
);
440 tg
->flags
&= ~THROTL_TG_PENDING
;
443 static void throtl_dequeue_tg(struct throtl_grp
*tg
)
445 if (tg
->flags
& THROTL_TG_PENDING
)
446 __throtl_dequeue_tg(tg
);
449 /* Call with queue lock held */
450 static void throtl_schedule_delayed_work(struct throtl_data
*td
,
453 struct delayed_work
*dwork
= &td
->dispatch_work
;
454 struct throtl_service_queue
*sq
= &td
->service_queue
;
456 mod_delayed_work(kthrotld_workqueue
, dwork
, delay
);
457 throtl_log(sq
, "schedule work. delay=%lu jiffies=%lu", delay
, jiffies
);
460 static void throtl_schedule_next_dispatch(struct throtl_data
*td
)
462 struct throtl_service_queue
*sq
= &td
->service_queue
;
464 /* any pending children left? */
468 update_min_dispatch_time(sq
);
470 if (time_before_eq(sq
->first_pending_disptime
, jiffies
))
471 throtl_schedule_delayed_work(td
, 0);
473 throtl_schedule_delayed_work(td
, sq
->first_pending_disptime
- jiffies
);
476 static inline void throtl_start_new_slice(struct throtl_grp
*tg
, bool rw
)
478 tg
->bytes_disp
[rw
] = 0;
480 tg
->slice_start
[rw
] = jiffies
;
481 tg
->slice_end
[rw
] = jiffies
+ throtl_slice
;
482 throtl_log(&tg
->service_queue
,
483 "[%c] new slice start=%lu end=%lu jiffies=%lu",
484 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
485 tg
->slice_end
[rw
], jiffies
);
488 static inline void throtl_set_slice_end(struct throtl_grp
*tg
, bool rw
,
489 unsigned long jiffy_end
)
491 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
494 static inline void throtl_extend_slice(struct throtl_grp
*tg
, bool rw
,
495 unsigned long jiffy_end
)
497 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
498 throtl_log(&tg
->service_queue
,
499 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
500 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
501 tg
->slice_end
[rw
], jiffies
);
504 /* Determine if previously allocated or extended slice is complete or not */
505 static bool throtl_slice_used(struct throtl_grp
*tg
, bool rw
)
507 if (time_in_range(jiffies
, tg
->slice_start
[rw
], tg
->slice_end
[rw
]))
513 /* Trim the used slices and adjust slice start accordingly */
514 static inline void throtl_trim_slice(struct throtl_grp
*tg
, bool rw
)
516 unsigned long nr_slices
, time_elapsed
, io_trim
;
519 BUG_ON(time_before(tg
->slice_end
[rw
], tg
->slice_start
[rw
]));
522 * If bps are unlimited (-1), then time slice don't get
523 * renewed. Don't try to trim the slice if slice is used. A new
524 * slice will start when appropriate.
526 if (throtl_slice_used(tg
, rw
))
530 * A bio has been dispatched. Also adjust slice_end. It might happen
531 * that initially cgroup limit was very low resulting in high
532 * slice_end, but later limit was bumped up and bio was dispached
533 * sooner, then we need to reduce slice_end. A high bogus slice_end
534 * is bad because it does not allow new slice to start.
537 throtl_set_slice_end(tg
, rw
, jiffies
+ throtl_slice
);
539 time_elapsed
= jiffies
- tg
->slice_start
[rw
];
541 nr_slices
= time_elapsed
/ throtl_slice
;
545 tmp
= tg
->bps
[rw
] * throtl_slice
* nr_slices
;
549 io_trim
= (tg
->iops
[rw
] * throtl_slice
* nr_slices
)/HZ
;
551 if (!bytes_trim
&& !io_trim
)
554 if (tg
->bytes_disp
[rw
] >= bytes_trim
)
555 tg
->bytes_disp
[rw
] -= bytes_trim
;
557 tg
->bytes_disp
[rw
] = 0;
559 if (tg
->io_disp
[rw
] >= io_trim
)
560 tg
->io_disp
[rw
] -= io_trim
;
564 tg
->slice_start
[rw
] += nr_slices
* throtl_slice
;
566 throtl_log(&tg
->service_queue
,
567 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
568 rw
== READ
? 'R' : 'W', nr_slices
, bytes_trim
, io_trim
,
569 tg
->slice_start
[rw
], tg
->slice_end
[rw
], jiffies
);
572 static bool tg_with_in_iops_limit(struct throtl_grp
*tg
, struct bio
*bio
,
575 bool rw
= bio_data_dir(bio
);
576 unsigned int io_allowed
;
577 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
580 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
582 /* Slice has just started. Consider one slice interval */
584 jiffy_elapsed_rnd
= throtl_slice
;
586 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
589 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
590 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
591 * will allow dispatch after 1 second and after that slice should
595 tmp
= (u64
)tg
->iops
[rw
] * jiffy_elapsed_rnd
;
599 io_allowed
= UINT_MAX
;
603 if (tg
->io_disp
[rw
] + 1 <= io_allowed
) {
609 /* Calc approx time to dispatch */
610 jiffy_wait
= ((tg
->io_disp
[rw
] + 1) * HZ
)/tg
->iops
[rw
] + 1;
612 if (jiffy_wait
> jiffy_elapsed
)
613 jiffy_wait
= jiffy_wait
- jiffy_elapsed
;
622 static bool tg_with_in_bps_limit(struct throtl_grp
*tg
, struct bio
*bio
,
625 bool rw
= bio_data_dir(bio
);
626 u64 bytes_allowed
, extra_bytes
, tmp
;
627 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
629 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
631 /* Slice has just started. Consider one slice interval */
633 jiffy_elapsed_rnd
= throtl_slice
;
635 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
637 tmp
= tg
->bps
[rw
] * jiffy_elapsed_rnd
;
641 if (tg
->bytes_disp
[rw
] + bio
->bi_size
<= bytes_allowed
) {
647 /* Calc approx time to dispatch */
648 extra_bytes
= tg
->bytes_disp
[rw
] + bio
->bi_size
- bytes_allowed
;
649 jiffy_wait
= div64_u64(extra_bytes
* HZ
, tg
->bps
[rw
]);
655 * This wait time is without taking into consideration the rounding
656 * up we did. Add that time also.
658 jiffy_wait
= jiffy_wait
+ (jiffy_elapsed_rnd
- jiffy_elapsed
);
664 static bool tg_no_rule_group(struct throtl_grp
*tg
, bool rw
) {
665 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1)
671 * Returns whether one can dispatch a bio or not. Also returns approx number
672 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
674 static bool tg_may_dispatch(struct throtl_grp
*tg
, struct bio
*bio
,
677 bool rw
= bio_data_dir(bio
);
678 unsigned long bps_wait
= 0, iops_wait
= 0, max_wait
= 0;
681 * Currently whole state machine of group depends on first bio
682 * queued in the group bio list. So one should not be calling
683 * this function with a different bio if there are other bios
686 BUG_ON(tg
->service_queue
.nr_queued
[rw
] &&
687 bio
!= bio_list_peek(&tg
->service_queue
.bio_lists
[rw
]));
689 /* If tg->bps = -1, then BW is unlimited */
690 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1) {
697 * If previous slice expired, start a new one otherwise renew/extend
698 * existing slice to make sure it is at least throtl_slice interval
701 if (throtl_slice_used(tg
, rw
))
702 throtl_start_new_slice(tg
, rw
);
704 if (time_before(tg
->slice_end
[rw
], jiffies
+ throtl_slice
))
705 throtl_extend_slice(tg
, rw
, jiffies
+ throtl_slice
);
708 if (tg_with_in_bps_limit(tg
, bio
, &bps_wait
) &&
709 tg_with_in_iops_limit(tg
, bio
, &iops_wait
)) {
715 max_wait
= max(bps_wait
, iops_wait
);
720 if (time_before(tg
->slice_end
[rw
], jiffies
+ max_wait
))
721 throtl_extend_slice(tg
, rw
, jiffies
+ max_wait
);
726 static void throtl_update_dispatch_stats(struct blkcg_gq
*blkg
, u64 bytes
,
729 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
730 struct tg_stats_cpu
*stats_cpu
;
733 /* If per cpu stats are not allocated yet, don't do any accounting. */
734 if (tg
->stats_cpu
== NULL
)
738 * Disabling interrupts to provide mutual exclusion between two
739 * writes on same cpu. It probably is not needed for 64bit. Not
740 * optimizing that case yet.
742 local_irq_save(flags
);
744 stats_cpu
= this_cpu_ptr(tg
->stats_cpu
);
746 blkg_rwstat_add(&stats_cpu
->serviced
, rw
, 1);
747 blkg_rwstat_add(&stats_cpu
->service_bytes
, rw
, bytes
);
749 local_irq_restore(flags
);
752 static void throtl_charge_bio(struct throtl_grp
*tg
, struct bio
*bio
)
754 bool rw
= bio_data_dir(bio
);
756 /* Charge the bio to the group */
757 tg
->bytes_disp
[rw
] += bio
->bi_size
;
761 * REQ_THROTTLED is used to prevent the same bio to be throttled
762 * more than once as a throttled bio will go through blk-throtl the
763 * second time when it eventually gets issued. Set it when a bio
764 * is being charged to a tg.
766 * Dispatch stats aren't recursive and each @bio should only be
767 * accounted by the @tg it was originally associated with. Let's
768 * update the stats when setting REQ_THROTTLED for the first time
769 * which is guaranteed to be for the @bio's original tg.
771 if (!(bio
->bi_rw
& REQ_THROTTLED
)) {
772 bio
->bi_rw
|= REQ_THROTTLED
;
773 throtl_update_dispatch_stats(tg_to_blkg(tg
), bio
->bi_size
,
778 static void throtl_add_bio_tg(struct bio
*bio
, struct throtl_grp
*tg
)
780 struct throtl_service_queue
*sq
= &tg
->service_queue
;
781 bool rw
= bio_data_dir(bio
);
784 * If @tg doesn't currently have any bios queued in the same
785 * direction, queueing @bio can change when @tg should be
786 * dispatched. Mark that @tg was empty. This is automatically
787 * cleaered on the next tg_update_disptime().
789 if (!sq
->nr_queued
[rw
])
790 tg
->flags
|= THROTL_TG_WAS_EMPTY
;
792 bio_list_add(&sq
->bio_lists
[rw
], bio
);
793 /* Take a bio reference on tg */
794 blkg_get(tg_to_blkg(tg
));
796 tg
->td
->nr_queued
[rw
]++;
797 throtl_enqueue_tg(tg
);
800 static void tg_update_disptime(struct throtl_grp
*tg
)
802 struct throtl_service_queue
*sq
= &tg
->service_queue
;
803 unsigned long read_wait
= -1, write_wait
= -1, min_wait
= -1, disptime
;
806 if ((bio
= bio_list_peek(&sq
->bio_lists
[READ
])))
807 tg_may_dispatch(tg
, bio
, &read_wait
);
809 if ((bio
= bio_list_peek(&sq
->bio_lists
[WRITE
])))
810 tg_may_dispatch(tg
, bio
, &write_wait
);
812 min_wait
= min(read_wait
, write_wait
);
813 disptime
= jiffies
+ min_wait
;
815 /* Update dispatch time */
816 throtl_dequeue_tg(tg
);
817 tg
->disptime
= disptime
;
818 throtl_enqueue_tg(tg
);
820 /* see throtl_add_bio_tg() */
821 tg
->flags
&= ~THROTL_TG_WAS_EMPTY
;
824 static void tg_dispatch_one_bio(struct throtl_grp
*tg
, bool rw
)
826 struct throtl_service_queue
*sq
= &tg
->service_queue
;
829 bio
= bio_list_pop(&sq
->bio_lists
[rw
]);
831 /* Drop bio reference on blkg */
832 blkg_put(tg_to_blkg(tg
));
834 BUG_ON(tg
->td
->nr_queued
[rw
] <= 0);
835 tg
->td
->nr_queued
[rw
]--;
837 throtl_charge_bio(tg
, bio
);
838 bio_list_add(&sq
->parent_sq
->bio_lists
[rw
], bio
);
840 throtl_trim_slice(tg
, rw
);
843 static int throtl_dispatch_tg(struct throtl_grp
*tg
)
845 struct throtl_service_queue
*sq
= &tg
->service_queue
;
846 unsigned int nr_reads
= 0, nr_writes
= 0;
847 unsigned int max_nr_reads
= throtl_grp_quantum
*3/4;
848 unsigned int max_nr_writes
= throtl_grp_quantum
- max_nr_reads
;
851 /* Try to dispatch 75% READS and 25% WRITES */
853 while ((bio
= bio_list_peek(&sq
->bio_lists
[READ
])) &&
854 tg_may_dispatch(tg
, bio
, NULL
)) {
856 tg_dispatch_one_bio(tg
, bio_data_dir(bio
));
859 if (nr_reads
>= max_nr_reads
)
863 while ((bio
= bio_list_peek(&sq
->bio_lists
[WRITE
])) &&
864 tg_may_dispatch(tg
, bio
, NULL
)) {
866 tg_dispatch_one_bio(tg
, bio_data_dir(bio
));
869 if (nr_writes
>= max_nr_writes
)
873 return nr_reads
+ nr_writes
;
876 static int throtl_select_dispatch(struct throtl_service_queue
*parent_sq
)
878 unsigned int nr_disp
= 0;
881 struct throtl_grp
*tg
= throtl_rb_first(parent_sq
);
882 struct throtl_service_queue
*sq
= &tg
->service_queue
;
887 if (time_before(jiffies
, tg
->disptime
))
890 throtl_dequeue_tg(tg
);
892 nr_disp
+= throtl_dispatch_tg(tg
);
894 if (sq
->nr_queued
[0] || sq
->nr_queued
[1])
895 tg_update_disptime(tg
);
897 if (nr_disp
>= throtl_quantum
)
904 /* work function to dispatch throttled bios */
905 void blk_throtl_dispatch_work_fn(struct work_struct
*work
)
907 struct throtl_data
*td
= container_of(to_delayed_work(work
),
908 struct throtl_data
, dispatch_work
);
909 struct throtl_service_queue
*sq
= &td
->service_queue
;
910 struct request_queue
*q
= td
->queue
;
911 unsigned int nr_disp
= 0;
912 struct bio_list bio_list_on_stack
;
914 struct blk_plug plug
;
917 spin_lock_irq(q
->queue_lock
);
919 bio_list_init(&bio_list_on_stack
);
921 throtl_log(sq
, "dispatch nr_queued=%u read=%u write=%u",
922 td
->nr_queued
[READ
] + td
->nr_queued
[WRITE
],
923 td
->nr_queued
[READ
], td
->nr_queued
[WRITE
]);
925 nr_disp
= throtl_select_dispatch(sq
);
928 for (rw
= READ
; rw
<= WRITE
; rw
++) {
929 bio_list_merge(&bio_list_on_stack
, &sq
->bio_lists
[rw
]);
930 bio_list_init(&sq
->bio_lists
[rw
]);
932 throtl_log(sq
, "bios disp=%u", nr_disp
);
935 throtl_schedule_next_dispatch(td
);
937 spin_unlock_irq(q
->queue_lock
);
940 * If we dispatched some requests, unplug the queue to make sure
944 blk_start_plug(&plug
);
945 while((bio
= bio_list_pop(&bio_list_on_stack
)))
946 generic_make_request(bio
);
947 blk_finish_plug(&plug
);
951 static u64
tg_prfill_cpu_rwstat(struct seq_file
*sf
,
952 struct blkg_policy_data
*pd
, int off
)
954 struct throtl_grp
*tg
= pd_to_tg(pd
);
955 struct blkg_rwstat rwstat
= { }, tmp
;
958 for_each_possible_cpu(cpu
) {
959 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
961 tmp
= blkg_rwstat_read((void *)sc
+ off
);
962 for (i
= 0; i
< BLKG_RWSTAT_NR
; i
++)
963 rwstat
.cnt
[i
] += tmp
.cnt
[i
];
966 return __blkg_prfill_rwstat(sf
, pd
, &rwstat
);
969 static int tg_print_cpu_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
972 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
974 blkcg_print_blkgs(sf
, blkcg
, tg_prfill_cpu_rwstat
, &blkcg_policy_throtl
,
979 static u64
tg_prfill_conf_u64(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
982 struct throtl_grp
*tg
= pd_to_tg(pd
);
983 u64 v
= *(u64
*)((void *)tg
+ off
);
987 return __blkg_prfill_u64(sf
, pd
, v
);
990 static u64
tg_prfill_conf_uint(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
993 struct throtl_grp
*tg
= pd_to_tg(pd
);
994 unsigned int v
= *(unsigned int *)((void *)tg
+ off
);
998 return __blkg_prfill_u64(sf
, pd
, v
);
1001 static int tg_print_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1002 struct seq_file
*sf
)
1004 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
), tg_prfill_conf_u64
,
1005 &blkcg_policy_throtl
, cft
->private, false);
1009 static int tg_print_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
1010 struct seq_file
*sf
)
1012 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
), tg_prfill_conf_uint
,
1013 &blkcg_policy_throtl
, cft
->private, false);
1017 static int tg_set_conf(struct cgroup
*cgrp
, struct cftype
*cft
, const char *buf
,
1020 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
1021 struct blkg_conf_ctx ctx
;
1022 struct throtl_grp
*tg
;
1023 struct throtl_data
*td
;
1026 ret
= blkg_conf_prep(blkcg
, &blkcg_policy_throtl
, buf
, &ctx
);
1030 tg
= blkg_to_tg(ctx
.blkg
);
1031 td
= ctx
.blkg
->q
->td
;
1037 *(u64
*)((void *)tg
+ cft
->private) = ctx
.v
;
1039 *(unsigned int *)((void *)tg
+ cft
->private) = ctx
.v
;
1041 throtl_log(&tg
->service_queue
,
1042 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1043 tg
->bps
[READ
], tg
->bps
[WRITE
],
1044 tg
->iops
[READ
], tg
->iops
[WRITE
]);
1047 * We're already holding queue_lock and know @tg is valid. Let's
1048 * apply the new config directly.
1050 * Restart the slices for both READ and WRITES. It might happen
1051 * that a group's limit are dropped suddenly and we don't want to
1052 * account recently dispatched IO with new low rate.
1054 throtl_start_new_slice(tg
, 0);
1055 throtl_start_new_slice(tg
, 1);
1057 if (tg
->flags
& THROTL_TG_PENDING
) {
1058 tg_update_disptime(tg
);
1059 throtl_schedule_next_dispatch(td
);
1062 blkg_conf_finish(&ctx
);
1066 static int tg_set_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
1069 return tg_set_conf(cgrp
, cft
, buf
, true);
1072 static int tg_set_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
1075 return tg_set_conf(cgrp
, cft
, buf
, false);
1078 static struct cftype throtl_files
[] = {
1080 .name
= "throttle.read_bps_device",
1081 .private = offsetof(struct throtl_grp
, bps
[READ
]),
1082 .read_seq_string
= tg_print_conf_u64
,
1083 .write_string
= tg_set_conf_u64
,
1084 .max_write_len
= 256,
1087 .name
= "throttle.write_bps_device",
1088 .private = offsetof(struct throtl_grp
, bps
[WRITE
]),
1089 .read_seq_string
= tg_print_conf_u64
,
1090 .write_string
= tg_set_conf_u64
,
1091 .max_write_len
= 256,
1094 .name
= "throttle.read_iops_device",
1095 .private = offsetof(struct throtl_grp
, iops
[READ
]),
1096 .read_seq_string
= tg_print_conf_uint
,
1097 .write_string
= tg_set_conf_uint
,
1098 .max_write_len
= 256,
1101 .name
= "throttle.write_iops_device",
1102 .private = offsetof(struct throtl_grp
, iops
[WRITE
]),
1103 .read_seq_string
= tg_print_conf_uint
,
1104 .write_string
= tg_set_conf_uint
,
1105 .max_write_len
= 256,
1108 .name
= "throttle.io_service_bytes",
1109 .private = offsetof(struct tg_stats_cpu
, service_bytes
),
1110 .read_seq_string
= tg_print_cpu_rwstat
,
1113 .name
= "throttle.io_serviced",
1114 .private = offsetof(struct tg_stats_cpu
, serviced
),
1115 .read_seq_string
= tg_print_cpu_rwstat
,
1120 static void throtl_shutdown_wq(struct request_queue
*q
)
1122 struct throtl_data
*td
= q
->td
;
1124 cancel_delayed_work_sync(&td
->dispatch_work
);
1127 static struct blkcg_policy blkcg_policy_throtl
= {
1128 .pd_size
= sizeof(struct throtl_grp
),
1129 .cftypes
= throtl_files
,
1131 .pd_init_fn
= throtl_pd_init
,
1132 .pd_exit_fn
= throtl_pd_exit
,
1133 .pd_reset_stats_fn
= throtl_pd_reset_stats
,
1136 bool blk_throtl_bio(struct request_queue
*q
, struct bio
*bio
)
1138 struct throtl_data
*td
= q
->td
;
1139 struct throtl_grp
*tg
;
1140 struct throtl_service_queue
*sq
;
1141 bool rw
= bio_data_dir(bio
);
1142 struct blkcg
*blkcg
;
1143 bool throttled
= false;
1145 /* see throtl_charge_bio() */
1146 if (bio
->bi_rw
& REQ_THROTTLED
)
1150 * A throtl_grp pointer retrieved under rcu can be used to access
1151 * basic fields like stats and io rates. If a group has no rules,
1152 * just update the dispatch stats in lockless manner and return.
1155 blkcg
= bio_blkcg(bio
);
1156 tg
= throtl_lookup_tg(td
, blkcg
);
1158 if (tg_no_rule_group(tg
, rw
)) {
1159 throtl_update_dispatch_stats(tg_to_blkg(tg
),
1160 bio
->bi_size
, bio
->bi_rw
);
1161 goto out_unlock_rcu
;
1166 * Either group has not been allocated yet or it is not an unlimited
1169 spin_lock_irq(q
->queue_lock
);
1170 tg
= throtl_lookup_create_tg(td
, blkcg
);
1174 sq
= &tg
->service_queue
;
1176 /* throtl is FIFO - if other bios are already queued, should queue */
1177 if (sq
->nr_queued
[rw
])
1180 /* Bio is with-in rate limit of group */
1181 if (tg_may_dispatch(tg
, bio
, NULL
)) {
1182 throtl_charge_bio(tg
, bio
);
1185 * We need to trim slice even when bios are not being queued
1186 * otherwise it might happen that a bio is not queued for
1187 * a long time and slice keeps on extending and trim is not
1188 * called for a long time. Now if limits are reduced suddenly
1189 * we take into account all the IO dispatched so far at new
1190 * low rate and * newly queued IO gets a really long dispatch
1193 * So keep on trimming slice even if bio is not queued.
1195 throtl_trim_slice(tg
, rw
);
1200 throtl_log(sq
, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1201 rw
== READ
? 'R' : 'W',
1202 tg
->bytes_disp
[rw
], bio
->bi_size
, tg
->bps
[rw
],
1203 tg
->io_disp
[rw
], tg
->iops
[rw
],
1204 sq
->nr_queued
[READ
], sq
->nr_queued
[WRITE
]);
1206 bio_associate_current(bio
);
1207 throtl_add_bio_tg(bio
, tg
);
1210 /* update @tg's dispatch time if @tg was empty before @bio */
1211 if (tg
->flags
& THROTL_TG_WAS_EMPTY
) {
1212 tg_update_disptime(tg
);
1213 throtl_schedule_next_dispatch(td
);
1217 spin_unlock_irq(q
->queue_lock
);
1222 * As multiple blk-throtls may stack in the same issue path, we
1223 * don't want bios to leave with the flag set. Clear the flag if
1227 bio
->bi_rw
&= ~REQ_THROTTLED
;
1232 * blk_throtl_drain - drain throttled bios
1233 * @q: request_queue to drain throttled bios for
1235 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1237 void blk_throtl_drain(struct request_queue
*q
)
1238 __releases(q
->queue_lock
) __acquires(q
->queue_lock
)
1240 struct throtl_data
*td
= q
->td
;
1241 struct throtl_service_queue
*parent_sq
= &td
->service_queue
;
1242 struct throtl_grp
*tg
;
1246 queue_lockdep_assert_held(q
);
1248 while ((tg
= throtl_rb_first(parent_sq
))) {
1249 struct throtl_service_queue
*sq
= &tg
->service_queue
;
1251 throtl_dequeue_tg(tg
);
1253 while ((bio
= bio_list_peek(&sq
->bio_lists
[READ
])))
1254 tg_dispatch_one_bio(tg
, bio_data_dir(bio
));
1255 while ((bio
= bio_list_peek(&sq
->bio_lists
[WRITE
])))
1256 tg_dispatch_one_bio(tg
, bio_data_dir(bio
));
1258 spin_unlock_irq(q
->queue_lock
);
1260 for (rw
= READ
; rw
<= WRITE
; rw
++)
1261 while ((bio
= bio_list_pop(&parent_sq
->bio_lists
[rw
])))
1262 generic_make_request(bio
);
1264 spin_lock_irq(q
->queue_lock
);
1267 int blk_throtl_init(struct request_queue
*q
)
1269 struct throtl_data
*td
;
1272 td
= kzalloc_node(sizeof(*td
), GFP_KERNEL
, q
->node
);
1276 INIT_DELAYED_WORK(&td
->dispatch_work
, blk_throtl_dispatch_work_fn
);
1277 throtl_service_queue_init(&td
->service_queue
, NULL
);
1282 /* activate policy */
1283 ret
= blkcg_activate_policy(q
, &blkcg_policy_throtl
);
1289 void blk_throtl_exit(struct request_queue
*q
)
1292 throtl_shutdown_wq(q
);
1293 blkcg_deactivate_policy(q
, &blkcg_policy_throtl
);
1297 static int __init
throtl_init(void)
1299 kthrotld_workqueue
= alloc_workqueue("kthrotld", WQ_MEM_RECLAIM
, 0);
1300 if (!kthrotld_workqueue
)
1301 panic("Failed to create kthrotld\n");
1303 return blkcg_policy_register(&blkcg_policy_throtl
);
1306 module_init(throtl_init
);