2 * Interface for controlling IO bandwidth on a request queue
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/blkdev.h>
10 #include <linux/bio.h>
11 #include <linux/blktrace_api.h>
12 #include "blk-cgroup.h"
15 /* Max dispatch from a group in 1 round */
16 static int throtl_grp_quantum
= 8;
18 /* Total max dispatch from all groups in one round */
19 static int throtl_quantum
= 32;
21 /* Throttling is performed over 100ms slice and after that slice is renewed */
22 static unsigned long throtl_slice
= HZ
/10; /* 100 ms */
24 static struct blkcg_policy blkcg_policy_throtl
;
26 /* A workqueue to queue throttle related work */
27 static struct workqueue_struct
*kthrotld_workqueue
;
29 struct throtl_service_queue
{
30 struct rb_root pending_tree
; /* RB tree of active tgs */
31 struct rb_node
*first_pending
; /* first node in the tree */
32 unsigned int nr_pending
; /* # queued in the tree */
33 unsigned long first_pending_disptime
; /* disptime of the first tg */
36 #define THROTL_SERVICE_QUEUE_INITIALIZER \
37 (struct throtl_service_queue){ .pending_tree = RB_ROOT }
40 THROTL_TG_PENDING
= 1 << 0, /* on parent's pending tree */
43 #define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
45 /* Per-cpu group stats */
47 /* total bytes transferred */
48 struct blkg_rwstat service_bytes
;
49 /* total IOs serviced, post merge */
50 struct blkg_rwstat serviced
;
54 /* must be the first member */
55 struct blkg_policy_data pd
;
57 /* active throtl group service_queue member */
58 struct rb_node rb_node
;
60 /* throtl_data this group belongs to */
61 struct throtl_data
*td
;
64 * Dispatch time in jiffies. This is the estimated time when group
65 * will unthrottle and is ready to dispatch more bio. It is used as
66 * key to sort active groups in service tree.
68 unsigned long disptime
;
72 /* Two lists for READ and WRITE */
73 struct bio_list bio_lists
[2];
75 /* Number of queued bios on READ and WRITE lists */
76 unsigned int nr_queued
[2];
78 /* bytes per second rate limits */
84 /* Number of bytes disptached in current slice */
85 uint64_t bytes_disp
[2];
86 /* Number of bio's dispatched in current slice */
87 unsigned int io_disp
[2];
89 /* When did we start a new slice */
90 unsigned long slice_start
[2];
91 unsigned long slice_end
[2];
93 /* Per cpu stats pointer */
94 struct tg_stats_cpu __percpu
*stats_cpu
;
96 /* List of tgs waiting for per cpu stats memory to be allocated */
97 struct list_head stats_alloc_node
;
102 /* service tree for active throtl groups */
103 struct throtl_service_queue service_queue
;
105 struct request_queue
*queue
;
107 /* Total Number of queued bios on READ and WRITE lists */
108 unsigned int nr_queued
[2];
111 * number of total undestroyed groups
113 unsigned int nr_undestroyed_grps
;
115 /* Work for dispatching throttled bios */
116 struct delayed_work dispatch_work
;
119 /* list and work item to allocate percpu group stats */
120 static DEFINE_SPINLOCK(tg_stats_alloc_lock
);
121 static LIST_HEAD(tg_stats_alloc_list
);
123 static void tg_stats_alloc_fn(struct work_struct
*);
124 static DECLARE_DELAYED_WORK(tg_stats_alloc_work
, tg_stats_alloc_fn
);
126 static inline struct throtl_grp
*pd_to_tg(struct blkg_policy_data
*pd
)
128 return pd
? container_of(pd
, struct throtl_grp
, pd
) : NULL
;
131 static inline struct throtl_grp
*blkg_to_tg(struct blkcg_gq
*blkg
)
133 return pd_to_tg(blkg_to_pd(blkg
, &blkcg_policy_throtl
));
136 static inline struct blkcg_gq
*tg_to_blkg(struct throtl_grp
*tg
)
138 return pd_to_blkg(&tg
->pd
);
141 static inline struct throtl_grp
*td_root_tg(struct throtl_data
*td
)
143 return blkg_to_tg(td
->queue
->root_blkg
);
146 #define throtl_log_tg(tg, fmt, args...) do { \
149 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
150 blk_add_trace_msg((tg)->td->queue, "throtl %s " fmt, __pbuf, ##args); \
153 #define throtl_log(td, fmt, args...) \
154 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
157 * Worker for allocating per cpu stat for tgs. This is scheduled on the
158 * system_wq once there are some groups on the alloc_list waiting for
161 static void tg_stats_alloc_fn(struct work_struct
*work
)
163 static struct tg_stats_cpu
*stats_cpu
; /* this fn is non-reentrant */
164 struct delayed_work
*dwork
= to_delayed_work(work
);
169 stats_cpu
= alloc_percpu(struct tg_stats_cpu
);
171 /* allocation failed, try again after some time */
172 schedule_delayed_work(dwork
, msecs_to_jiffies(10));
177 spin_lock_irq(&tg_stats_alloc_lock
);
179 if (!list_empty(&tg_stats_alloc_list
)) {
180 struct throtl_grp
*tg
= list_first_entry(&tg_stats_alloc_list
,
183 swap(tg
->stats_cpu
, stats_cpu
);
184 list_del_init(&tg
->stats_alloc_node
);
187 empty
= list_empty(&tg_stats_alloc_list
);
188 spin_unlock_irq(&tg_stats_alloc_lock
);
193 static void throtl_pd_init(struct blkcg_gq
*blkg
)
195 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
198 RB_CLEAR_NODE(&tg
->rb_node
);
199 tg
->td
= blkg
->q
->td
;
200 bio_list_init(&tg
->bio_lists
[0]);
201 bio_list_init(&tg
->bio_lists
[1]);
206 tg
->iops
[WRITE
] = -1;
209 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
210 * but percpu allocator can't be called from IO path. Queue tg on
211 * tg_stats_alloc_list and allocate from work item.
213 spin_lock_irqsave(&tg_stats_alloc_lock
, flags
);
214 list_add(&tg
->stats_alloc_node
, &tg_stats_alloc_list
);
215 schedule_delayed_work(&tg_stats_alloc_work
, 0);
216 spin_unlock_irqrestore(&tg_stats_alloc_lock
, flags
);
219 static void throtl_pd_exit(struct blkcg_gq
*blkg
)
221 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
224 spin_lock_irqsave(&tg_stats_alloc_lock
, flags
);
225 list_del_init(&tg
->stats_alloc_node
);
226 spin_unlock_irqrestore(&tg_stats_alloc_lock
, flags
);
228 free_percpu(tg
->stats_cpu
);
231 static void throtl_pd_reset_stats(struct blkcg_gq
*blkg
)
233 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
236 if (tg
->stats_cpu
== NULL
)
239 for_each_possible_cpu(cpu
) {
240 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
242 blkg_rwstat_reset(&sc
->service_bytes
);
243 blkg_rwstat_reset(&sc
->serviced
);
247 static struct throtl_grp
*throtl_lookup_tg(struct throtl_data
*td
,
251 * This is the common case when there are no blkcgs. Avoid lookup
254 if (blkcg
== &blkcg_root
)
255 return td_root_tg(td
);
257 return blkg_to_tg(blkg_lookup(blkcg
, td
->queue
));
260 static struct throtl_grp
*throtl_lookup_create_tg(struct throtl_data
*td
,
263 struct request_queue
*q
= td
->queue
;
264 struct throtl_grp
*tg
= NULL
;
267 * This is the common case when there are no blkcgs. Avoid lookup
270 if (blkcg
== &blkcg_root
) {
273 struct blkcg_gq
*blkg
;
275 blkg
= blkg_lookup_create(blkcg
, q
);
277 /* if %NULL and @q is alive, fall back to root_tg */
279 tg
= blkg_to_tg(blkg
);
280 else if (!blk_queue_dying(q
))
287 static struct throtl_grp
*
288 throtl_rb_first(struct throtl_service_queue
*parent_sq
)
290 /* Service tree is empty */
291 if (!parent_sq
->nr_pending
)
294 if (!parent_sq
->first_pending
)
295 parent_sq
->first_pending
= rb_first(&parent_sq
->pending_tree
);
297 if (parent_sq
->first_pending
)
298 return rb_entry_tg(parent_sq
->first_pending
);
303 static void rb_erase_init(struct rb_node
*n
, struct rb_root
*root
)
309 static void throtl_rb_erase(struct rb_node
*n
,
310 struct throtl_service_queue
*parent_sq
)
312 if (parent_sq
->first_pending
== n
)
313 parent_sq
->first_pending
= NULL
;
314 rb_erase_init(n
, &parent_sq
->pending_tree
);
315 --parent_sq
->nr_pending
;
318 static void update_min_dispatch_time(struct throtl_service_queue
*parent_sq
)
320 struct throtl_grp
*tg
;
322 tg
= throtl_rb_first(parent_sq
);
326 parent_sq
->first_pending_disptime
= tg
->disptime
;
329 static void tg_service_queue_add(struct throtl_grp
*tg
,
330 struct throtl_service_queue
*parent_sq
)
332 struct rb_node
**node
= &parent_sq
->pending_tree
.rb_node
;
333 struct rb_node
*parent
= NULL
;
334 struct throtl_grp
*__tg
;
335 unsigned long key
= tg
->disptime
;
338 while (*node
!= NULL
) {
340 __tg
= rb_entry_tg(parent
);
342 if (time_before(key
, __tg
->disptime
))
343 node
= &parent
->rb_left
;
345 node
= &parent
->rb_right
;
351 parent_sq
->first_pending
= &tg
->rb_node
;
353 rb_link_node(&tg
->rb_node
, parent
, node
);
354 rb_insert_color(&tg
->rb_node
, &parent_sq
->pending_tree
);
357 static void __throtl_enqueue_tg(struct throtl_grp
*tg
,
358 struct throtl_service_queue
*parent_sq
)
360 tg_service_queue_add(tg
, parent_sq
);
361 tg
->flags
|= THROTL_TG_PENDING
;
362 parent_sq
->nr_pending
++;
365 static void throtl_enqueue_tg(struct throtl_grp
*tg
,
366 struct throtl_service_queue
*parent_sq
)
368 if (!(tg
->flags
& THROTL_TG_PENDING
))
369 __throtl_enqueue_tg(tg
, parent_sq
);
372 static void __throtl_dequeue_tg(struct throtl_grp
*tg
,
373 struct throtl_service_queue
*parent_sq
)
375 throtl_rb_erase(&tg
->rb_node
, parent_sq
);
376 tg
->flags
&= ~THROTL_TG_PENDING
;
379 static void throtl_dequeue_tg(struct throtl_grp
*tg
,
380 struct throtl_service_queue
*parent_sq
)
382 if (tg
->flags
& THROTL_TG_PENDING
)
383 __throtl_dequeue_tg(tg
, parent_sq
);
386 /* Call with queue lock held */
387 static void throtl_schedule_delayed_work(struct throtl_data
*td
,
390 struct delayed_work
*dwork
= &td
->dispatch_work
;
392 mod_delayed_work(kthrotld_workqueue
, dwork
, delay
);
393 throtl_log(td
, "schedule work. delay=%lu jiffies=%lu", delay
, jiffies
);
396 static void throtl_schedule_next_dispatch(struct throtl_data
*td
)
398 struct throtl_service_queue
*sq
= &td
->service_queue
;
400 /* any pending children left? */
404 update_min_dispatch_time(sq
);
406 if (time_before_eq(sq
->first_pending_disptime
, jiffies
))
407 throtl_schedule_delayed_work(td
, 0);
409 throtl_schedule_delayed_work(td
, sq
->first_pending_disptime
- jiffies
);
412 static inline void throtl_start_new_slice(struct throtl_grp
*tg
, bool rw
)
414 tg
->bytes_disp
[rw
] = 0;
416 tg
->slice_start
[rw
] = jiffies
;
417 tg
->slice_end
[rw
] = jiffies
+ throtl_slice
;
418 throtl_log_tg(tg
, "[%c] new slice start=%lu end=%lu jiffies=%lu",
419 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
420 tg
->slice_end
[rw
], jiffies
);
423 static inline void throtl_set_slice_end(struct throtl_grp
*tg
, bool rw
,
424 unsigned long jiffy_end
)
426 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
429 static inline void throtl_extend_slice(struct throtl_grp
*tg
, bool rw
,
430 unsigned long jiffy_end
)
432 tg
->slice_end
[rw
] = roundup(jiffy_end
, throtl_slice
);
433 throtl_log_tg(tg
, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
434 rw
== READ
? 'R' : 'W', tg
->slice_start
[rw
],
435 tg
->slice_end
[rw
], jiffies
);
438 /* Determine if previously allocated or extended slice is complete or not */
439 static bool throtl_slice_used(struct throtl_grp
*tg
, bool rw
)
441 if (time_in_range(jiffies
, tg
->slice_start
[rw
], tg
->slice_end
[rw
]))
447 /* Trim the used slices and adjust slice start accordingly */
448 static inline void throtl_trim_slice(struct throtl_grp
*tg
, bool rw
)
450 unsigned long nr_slices
, time_elapsed
, io_trim
;
453 BUG_ON(time_before(tg
->slice_end
[rw
], tg
->slice_start
[rw
]));
456 * If bps are unlimited (-1), then time slice don't get
457 * renewed. Don't try to trim the slice if slice is used. A new
458 * slice will start when appropriate.
460 if (throtl_slice_used(tg
, rw
))
464 * A bio has been dispatched. Also adjust slice_end. It might happen
465 * that initially cgroup limit was very low resulting in high
466 * slice_end, but later limit was bumped up and bio was dispached
467 * sooner, then we need to reduce slice_end. A high bogus slice_end
468 * is bad because it does not allow new slice to start.
471 throtl_set_slice_end(tg
, rw
, jiffies
+ throtl_slice
);
473 time_elapsed
= jiffies
- tg
->slice_start
[rw
];
475 nr_slices
= time_elapsed
/ throtl_slice
;
479 tmp
= tg
->bps
[rw
] * throtl_slice
* nr_slices
;
483 io_trim
= (tg
->iops
[rw
] * throtl_slice
* nr_slices
)/HZ
;
485 if (!bytes_trim
&& !io_trim
)
488 if (tg
->bytes_disp
[rw
] >= bytes_trim
)
489 tg
->bytes_disp
[rw
] -= bytes_trim
;
491 tg
->bytes_disp
[rw
] = 0;
493 if (tg
->io_disp
[rw
] >= io_trim
)
494 tg
->io_disp
[rw
] -= io_trim
;
498 tg
->slice_start
[rw
] += nr_slices
* throtl_slice
;
500 throtl_log_tg(tg
, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
501 " start=%lu end=%lu jiffies=%lu",
502 rw
== READ
? 'R' : 'W', nr_slices
, bytes_trim
, io_trim
,
503 tg
->slice_start
[rw
], tg
->slice_end
[rw
], jiffies
);
506 static bool tg_with_in_iops_limit(struct throtl_grp
*tg
, struct bio
*bio
,
509 bool rw
= bio_data_dir(bio
);
510 unsigned int io_allowed
;
511 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
514 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
516 /* Slice has just started. Consider one slice interval */
518 jiffy_elapsed_rnd
= throtl_slice
;
520 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
523 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
524 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
525 * will allow dispatch after 1 second and after that slice should
529 tmp
= (u64
)tg
->iops
[rw
] * jiffy_elapsed_rnd
;
533 io_allowed
= UINT_MAX
;
537 if (tg
->io_disp
[rw
] + 1 <= io_allowed
) {
543 /* Calc approx time to dispatch */
544 jiffy_wait
= ((tg
->io_disp
[rw
] + 1) * HZ
)/tg
->iops
[rw
] + 1;
546 if (jiffy_wait
> jiffy_elapsed
)
547 jiffy_wait
= jiffy_wait
- jiffy_elapsed
;
556 static bool tg_with_in_bps_limit(struct throtl_grp
*tg
, struct bio
*bio
,
559 bool rw
= bio_data_dir(bio
);
560 u64 bytes_allowed
, extra_bytes
, tmp
;
561 unsigned long jiffy_elapsed
, jiffy_wait
, jiffy_elapsed_rnd
;
563 jiffy_elapsed
= jiffy_elapsed_rnd
= jiffies
- tg
->slice_start
[rw
];
565 /* Slice has just started. Consider one slice interval */
567 jiffy_elapsed_rnd
= throtl_slice
;
569 jiffy_elapsed_rnd
= roundup(jiffy_elapsed_rnd
, throtl_slice
);
571 tmp
= tg
->bps
[rw
] * jiffy_elapsed_rnd
;
575 if (tg
->bytes_disp
[rw
] + bio
->bi_size
<= bytes_allowed
) {
581 /* Calc approx time to dispatch */
582 extra_bytes
= tg
->bytes_disp
[rw
] + bio
->bi_size
- bytes_allowed
;
583 jiffy_wait
= div64_u64(extra_bytes
* HZ
, tg
->bps
[rw
]);
589 * This wait time is without taking into consideration the rounding
590 * up we did. Add that time also.
592 jiffy_wait
= jiffy_wait
+ (jiffy_elapsed_rnd
- jiffy_elapsed
);
598 static bool tg_no_rule_group(struct throtl_grp
*tg
, bool rw
) {
599 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1)
605 * Returns whether one can dispatch a bio or not. Also returns approx number
606 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
608 static bool tg_may_dispatch(struct throtl_grp
*tg
, struct bio
*bio
,
611 bool rw
= bio_data_dir(bio
);
612 unsigned long bps_wait
= 0, iops_wait
= 0, max_wait
= 0;
615 * Currently whole state machine of group depends on first bio
616 * queued in the group bio list. So one should not be calling
617 * this function with a different bio if there are other bios
620 BUG_ON(tg
->nr_queued
[rw
] && bio
!= bio_list_peek(&tg
->bio_lists
[rw
]));
622 /* If tg->bps = -1, then BW is unlimited */
623 if (tg
->bps
[rw
] == -1 && tg
->iops
[rw
] == -1) {
630 * If previous slice expired, start a new one otherwise renew/extend
631 * existing slice to make sure it is at least throtl_slice interval
634 if (throtl_slice_used(tg
, rw
))
635 throtl_start_new_slice(tg
, rw
);
637 if (time_before(tg
->slice_end
[rw
], jiffies
+ throtl_slice
))
638 throtl_extend_slice(tg
, rw
, jiffies
+ throtl_slice
);
641 if (tg_with_in_bps_limit(tg
, bio
, &bps_wait
) &&
642 tg_with_in_iops_limit(tg
, bio
, &iops_wait
)) {
648 max_wait
= max(bps_wait
, iops_wait
);
653 if (time_before(tg
->slice_end
[rw
], jiffies
+ max_wait
))
654 throtl_extend_slice(tg
, rw
, jiffies
+ max_wait
);
659 static void throtl_update_dispatch_stats(struct blkcg_gq
*blkg
, u64 bytes
,
662 struct throtl_grp
*tg
= blkg_to_tg(blkg
);
663 struct tg_stats_cpu
*stats_cpu
;
666 /* If per cpu stats are not allocated yet, don't do any accounting. */
667 if (tg
->stats_cpu
== NULL
)
671 * Disabling interrupts to provide mutual exclusion between two
672 * writes on same cpu. It probably is not needed for 64bit. Not
673 * optimizing that case yet.
675 local_irq_save(flags
);
677 stats_cpu
= this_cpu_ptr(tg
->stats_cpu
);
679 blkg_rwstat_add(&stats_cpu
->serviced
, rw
, 1);
680 blkg_rwstat_add(&stats_cpu
->service_bytes
, rw
, bytes
);
682 local_irq_restore(flags
);
685 static void throtl_charge_bio(struct throtl_grp
*tg
, struct bio
*bio
)
687 bool rw
= bio_data_dir(bio
);
689 /* Charge the bio to the group */
690 tg
->bytes_disp
[rw
] += bio
->bi_size
;
693 throtl_update_dispatch_stats(tg_to_blkg(tg
), bio
->bi_size
, bio
->bi_rw
);
696 static void throtl_add_bio_tg(struct bio
*bio
, struct throtl_grp
*tg
,
697 struct throtl_service_queue
*parent_sq
)
699 bool rw
= bio_data_dir(bio
);
701 bio_list_add(&tg
->bio_lists
[rw
], bio
);
702 /* Take a bio reference on tg */
703 blkg_get(tg_to_blkg(tg
));
705 tg
->td
->nr_queued
[rw
]++;
706 throtl_enqueue_tg(tg
, parent_sq
);
709 static void tg_update_disptime(struct throtl_grp
*tg
,
710 struct throtl_service_queue
*parent_sq
)
712 unsigned long read_wait
= -1, write_wait
= -1, min_wait
= -1, disptime
;
715 if ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
716 tg_may_dispatch(tg
, bio
, &read_wait
);
718 if ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
719 tg_may_dispatch(tg
, bio
, &write_wait
);
721 min_wait
= min(read_wait
, write_wait
);
722 disptime
= jiffies
+ min_wait
;
724 /* Update dispatch time */
725 throtl_dequeue_tg(tg
, parent_sq
);
726 tg
->disptime
= disptime
;
727 throtl_enqueue_tg(tg
, parent_sq
);
730 static void tg_dispatch_one_bio(struct throtl_grp
*tg
, bool rw
,
735 bio
= bio_list_pop(&tg
->bio_lists
[rw
]);
737 /* Drop bio reference on blkg */
738 blkg_put(tg_to_blkg(tg
));
740 BUG_ON(tg
->td
->nr_queued
[rw
] <= 0);
741 tg
->td
->nr_queued
[rw
]--;
743 throtl_charge_bio(tg
, bio
);
744 bio_list_add(bl
, bio
);
745 bio
->bi_rw
|= REQ_THROTTLED
;
747 throtl_trim_slice(tg
, rw
);
750 static int throtl_dispatch_tg(struct throtl_grp
*tg
, struct bio_list
*bl
)
752 unsigned int nr_reads
= 0, nr_writes
= 0;
753 unsigned int max_nr_reads
= throtl_grp_quantum
*3/4;
754 unsigned int max_nr_writes
= throtl_grp_quantum
- max_nr_reads
;
757 /* Try to dispatch 75% READS and 25% WRITES */
759 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])) &&
760 tg_may_dispatch(tg
, bio
, NULL
)) {
762 tg_dispatch_one_bio(tg
, bio_data_dir(bio
), bl
);
765 if (nr_reads
>= max_nr_reads
)
769 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])) &&
770 tg_may_dispatch(tg
, bio
, NULL
)) {
772 tg_dispatch_one_bio(tg
, bio_data_dir(bio
), bl
);
775 if (nr_writes
>= max_nr_writes
)
779 return nr_reads
+ nr_writes
;
782 static int throtl_select_dispatch(struct throtl_service_queue
*parent_sq
,
785 unsigned int nr_disp
= 0;
786 struct throtl_grp
*tg
;
789 tg
= throtl_rb_first(parent_sq
);
794 if (time_before(jiffies
, tg
->disptime
))
797 throtl_dequeue_tg(tg
, parent_sq
);
799 nr_disp
+= throtl_dispatch_tg(tg
, bl
);
801 if (tg
->nr_queued
[0] || tg
->nr_queued
[1])
802 tg_update_disptime(tg
, parent_sq
);
804 if (nr_disp
>= throtl_quantum
)
811 /* work function to dispatch throttled bios */
812 void blk_throtl_dispatch_work_fn(struct work_struct
*work
)
814 struct throtl_data
*td
= container_of(to_delayed_work(work
),
815 struct throtl_data
, dispatch_work
);
816 struct request_queue
*q
= td
->queue
;
817 unsigned int nr_disp
= 0;
818 struct bio_list bio_list_on_stack
;
820 struct blk_plug plug
;
822 spin_lock_irq(q
->queue_lock
);
824 bio_list_init(&bio_list_on_stack
);
826 throtl_log(td
, "dispatch nr_queued=%u read=%u write=%u",
827 td
->nr_queued
[READ
] + td
->nr_queued
[WRITE
],
828 td
->nr_queued
[READ
], td
->nr_queued
[WRITE
]);
830 nr_disp
= throtl_select_dispatch(&td
->service_queue
, &bio_list_on_stack
);
833 throtl_log(td
, "bios disp=%u", nr_disp
);
835 throtl_schedule_next_dispatch(td
);
837 spin_unlock_irq(q
->queue_lock
);
840 * If we dispatched some requests, unplug the queue to make sure
844 blk_start_plug(&plug
);
845 while((bio
= bio_list_pop(&bio_list_on_stack
)))
846 generic_make_request(bio
);
847 blk_finish_plug(&plug
);
851 static u64
tg_prfill_cpu_rwstat(struct seq_file
*sf
,
852 struct blkg_policy_data
*pd
, int off
)
854 struct throtl_grp
*tg
= pd_to_tg(pd
);
855 struct blkg_rwstat rwstat
= { }, tmp
;
858 for_each_possible_cpu(cpu
) {
859 struct tg_stats_cpu
*sc
= per_cpu_ptr(tg
->stats_cpu
, cpu
);
861 tmp
= blkg_rwstat_read((void *)sc
+ off
);
862 for (i
= 0; i
< BLKG_RWSTAT_NR
; i
++)
863 rwstat
.cnt
[i
] += tmp
.cnt
[i
];
866 return __blkg_prfill_rwstat(sf
, pd
, &rwstat
);
869 static int tg_print_cpu_rwstat(struct cgroup
*cgrp
, struct cftype
*cft
,
872 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
874 blkcg_print_blkgs(sf
, blkcg
, tg_prfill_cpu_rwstat
, &blkcg_policy_throtl
,
879 static u64
tg_prfill_conf_u64(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
882 struct throtl_grp
*tg
= pd_to_tg(pd
);
883 u64 v
= *(u64
*)((void *)tg
+ off
);
887 return __blkg_prfill_u64(sf
, pd
, v
);
890 static u64
tg_prfill_conf_uint(struct seq_file
*sf
, struct blkg_policy_data
*pd
,
893 struct throtl_grp
*tg
= pd_to_tg(pd
);
894 unsigned int v
= *(unsigned int *)((void *)tg
+ off
);
898 return __blkg_prfill_u64(sf
, pd
, v
);
901 static int tg_print_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
904 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
), tg_prfill_conf_u64
,
905 &blkcg_policy_throtl
, cft
->private, false);
909 static int tg_print_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
912 blkcg_print_blkgs(sf
, cgroup_to_blkcg(cgrp
), tg_prfill_conf_uint
,
913 &blkcg_policy_throtl
, cft
->private, false);
917 static int tg_set_conf(struct cgroup
*cgrp
, struct cftype
*cft
, const char *buf
,
920 struct blkcg
*blkcg
= cgroup_to_blkcg(cgrp
);
921 struct blkg_conf_ctx ctx
;
922 struct throtl_grp
*tg
;
923 struct throtl_data
*td
;
926 ret
= blkg_conf_prep(blkcg
, &blkcg_policy_throtl
, buf
, &ctx
);
930 tg
= blkg_to_tg(ctx
.blkg
);
931 td
= ctx
.blkg
->q
->td
;
937 *(u64
*)((void *)tg
+ cft
->private) = ctx
.v
;
939 *(unsigned int *)((void *)tg
+ cft
->private) = ctx
.v
;
941 throtl_log_tg(tg
, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
942 tg
->bps
[READ
], tg
->bps
[WRITE
],
943 tg
->iops
[READ
], tg
->iops
[WRITE
]);
946 * We're already holding queue_lock and know @tg is valid. Let's
947 * apply the new config directly.
949 * Restart the slices for both READ and WRITES. It might happen
950 * that a group's limit are dropped suddenly and we don't want to
951 * account recently dispatched IO with new low rate.
953 throtl_start_new_slice(tg
, 0);
954 throtl_start_new_slice(tg
, 1);
956 if (tg
->flags
& THROTL_TG_PENDING
) {
957 tg_update_disptime(tg
, &td
->service_queue
);
958 throtl_schedule_next_dispatch(td
);
961 blkg_conf_finish(&ctx
);
965 static int tg_set_conf_u64(struct cgroup
*cgrp
, struct cftype
*cft
,
968 return tg_set_conf(cgrp
, cft
, buf
, true);
971 static int tg_set_conf_uint(struct cgroup
*cgrp
, struct cftype
*cft
,
974 return tg_set_conf(cgrp
, cft
, buf
, false);
977 static struct cftype throtl_files
[] = {
979 .name
= "throttle.read_bps_device",
980 .private = offsetof(struct throtl_grp
, bps
[READ
]),
981 .read_seq_string
= tg_print_conf_u64
,
982 .write_string
= tg_set_conf_u64
,
983 .max_write_len
= 256,
986 .name
= "throttle.write_bps_device",
987 .private = offsetof(struct throtl_grp
, bps
[WRITE
]),
988 .read_seq_string
= tg_print_conf_u64
,
989 .write_string
= tg_set_conf_u64
,
990 .max_write_len
= 256,
993 .name
= "throttle.read_iops_device",
994 .private = offsetof(struct throtl_grp
, iops
[READ
]),
995 .read_seq_string
= tg_print_conf_uint
,
996 .write_string
= tg_set_conf_uint
,
997 .max_write_len
= 256,
1000 .name
= "throttle.write_iops_device",
1001 .private = offsetof(struct throtl_grp
, iops
[WRITE
]),
1002 .read_seq_string
= tg_print_conf_uint
,
1003 .write_string
= tg_set_conf_uint
,
1004 .max_write_len
= 256,
1007 .name
= "throttle.io_service_bytes",
1008 .private = offsetof(struct tg_stats_cpu
, service_bytes
),
1009 .read_seq_string
= tg_print_cpu_rwstat
,
1012 .name
= "throttle.io_serviced",
1013 .private = offsetof(struct tg_stats_cpu
, serviced
),
1014 .read_seq_string
= tg_print_cpu_rwstat
,
1019 static void throtl_shutdown_wq(struct request_queue
*q
)
1021 struct throtl_data
*td
= q
->td
;
1023 cancel_delayed_work_sync(&td
->dispatch_work
);
1026 static struct blkcg_policy blkcg_policy_throtl
= {
1027 .pd_size
= sizeof(struct throtl_grp
),
1028 .cftypes
= throtl_files
,
1030 .pd_init_fn
= throtl_pd_init
,
1031 .pd_exit_fn
= throtl_pd_exit
,
1032 .pd_reset_stats_fn
= throtl_pd_reset_stats
,
1035 bool blk_throtl_bio(struct request_queue
*q
, struct bio
*bio
)
1037 struct throtl_data
*td
= q
->td
;
1038 struct throtl_grp
*tg
;
1039 bool rw
= bio_data_dir(bio
), update_disptime
= true;
1040 struct blkcg
*blkcg
;
1041 bool throttled
= false;
1043 if (bio
->bi_rw
& REQ_THROTTLED
) {
1044 bio
->bi_rw
&= ~REQ_THROTTLED
;
1049 * A throtl_grp pointer retrieved under rcu can be used to access
1050 * basic fields like stats and io rates. If a group has no rules,
1051 * just update the dispatch stats in lockless manner and return.
1054 blkcg
= bio_blkcg(bio
);
1055 tg
= throtl_lookup_tg(td
, blkcg
);
1057 if (tg_no_rule_group(tg
, rw
)) {
1058 throtl_update_dispatch_stats(tg_to_blkg(tg
),
1059 bio
->bi_size
, bio
->bi_rw
);
1060 goto out_unlock_rcu
;
1065 * Either group has not been allocated yet or it is not an unlimited
1068 spin_lock_irq(q
->queue_lock
);
1069 tg
= throtl_lookup_create_tg(td
, blkcg
);
1073 if (tg
->nr_queued
[rw
]) {
1075 * There is already another bio queued in same dir. No
1076 * need to update dispatch time.
1078 update_disptime
= false;
1083 /* Bio is with-in rate limit of group */
1084 if (tg_may_dispatch(tg
, bio
, NULL
)) {
1085 throtl_charge_bio(tg
, bio
);
1088 * We need to trim slice even when bios are not being queued
1089 * otherwise it might happen that a bio is not queued for
1090 * a long time and slice keeps on extending and trim is not
1091 * called for a long time. Now if limits are reduced suddenly
1092 * we take into account all the IO dispatched so far at new
1093 * low rate and * newly queued IO gets a really long dispatch
1096 * So keep on trimming slice even if bio is not queued.
1098 throtl_trim_slice(tg
, rw
);
1103 throtl_log_tg(tg
, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
1104 " iodisp=%u iops=%u queued=%d/%d",
1105 rw
== READ
? 'R' : 'W',
1106 tg
->bytes_disp
[rw
], bio
->bi_size
, tg
->bps
[rw
],
1107 tg
->io_disp
[rw
], tg
->iops
[rw
],
1108 tg
->nr_queued
[READ
], tg
->nr_queued
[WRITE
]);
1110 bio_associate_current(bio
);
1111 throtl_add_bio_tg(bio
, tg
, &q
->td
->service_queue
);
1114 if (update_disptime
) {
1115 tg_update_disptime(tg
, &td
->service_queue
);
1116 throtl_schedule_next_dispatch(td
);
1120 spin_unlock_irq(q
->queue_lock
);
1128 * blk_throtl_drain - drain throttled bios
1129 * @q: request_queue to drain throttled bios for
1131 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1133 void blk_throtl_drain(struct request_queue
*q
)
1134 __releases(q
->queue_lock
) __acquires(q
->queue_lock
)
1136 struct throtl_data
*td
= q
->td
;
1137 struct throtl_service_queue
*parent_sq
= &td
->service_queue
;
1138 struct throtl_grp
*tg
;
1142 queue_lockdep_assert_held(q
);
1146 while ((tg
= throtl_rb_first(parent_sq
))) {
1147 throtl_dequeue_tg(tg
, parent_sq
);
1149 while ((bio
= bio_list_peek(&tg
->bio_lists
[READ
])))
1150 tg_dispatch_one_bio(tg
, bio_data_dir(bio
), &bl
);
1151 while ((bio
= bio_list_peek(&tg
->bio_lists
[WRITE
])))
1152 tg_dispatch_one_bio(tg
, bio_data_dir(bio
), &bl
);
1154 spin_unlock_irq(q
->queue_lock
);
1156 while ((bio
= bio_list_pop(&bl
)))
1157 generic_make_request(bio
);
1159 spin_lock_irq(q
->queue_lock
);
1162 int blk_throtl_init(struct request_queue
*q
)
1164 struct throtl_data
*td
;
1167 td
= kzalloc_node(sizeof(*td
), GFP_KERNEL
, q
->node
);
1171 td
->service_queue
= THROTL_SERVICE_QUEUE_INITIALIZER
;
1172 INIT_DELAYED_WORK(&td
->dispatch_work
, blk_throtl_dispatch_work_fn
);
1177 /* activate policy */
1178 ret
= blkcg_activate_policy(q
, &blkcg_policy_throtl
);
1184 void blk_throtl_exit(struct request_queue
*q
)
1187 throtl_shutdown_wq(q
);
1188 blkcg_deactivate_policy(q
, &blkcg_policy_throtl
);
1192 static int __init
throtl_init(void)
1194 kthrotld_workqueue
= alloc_workqueue("kthrotld", WQ_MEM_RECLAIM
, 0);
1195 if (!kthrotld_workqueue
)
1196 panic("Failed to create kthrotld\n");
1198 return blkcg_policy_register(&blkcg_policy_throtl
);
1201 module_init(throtl_init
);