blob: c15d38307e1d196cf690c113ea370c783b40f37e [file] [log] [blame]
Vivek Goyale43473b2010-09-15 17:06:35 -04001/*
2 * Interface for controlling IO bandwidth on a request queue
3 *
4 * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
5 */
6
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"
Tejun Heobc9fcbf2011-10-19 14:31:18 +020013#include "blk.h"
Vivek Goyale43473b2010-09-15 17:06:35 -040014
15/* Max dispatch from a group in 1 round */
16static int throtl_grp_quantum = 8;
17
18/* Total max dispatch from all groups in one round */
19static int throtl_quantum = 32;
20
21/* Throttling is performed over 100ms slice and after that slice is renewed */
22static unsigned long throtl_slice = HZ/10; /* 100 ms */
23
Tejun Heo03814112012-03-05 13:15:14 -080024static struct blkio_policy_type blkio_policy_throtl;
25
Vivek Goyal450adcb2011-03-01 13:40:54 -050026/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
28static void throtl_schedule_delayed_work(struct throtl_data *td,
29 unsigned long delay);
30
Vivek Goyale43473b2010-09-15 17:06:35 -040031struct throtl_rb_root {
32 struct rb_root rb;
33 struct rb_node *left;
34 unsigned int count;
35 unsigned long min_disptime;
36};
37
38#define THROTL_RB_ROOT (struct throtl_rb_root) { .rb = RB_ROOT, .left = NULL, \
39 .count = 0, .min_disptime = 0}
40
41#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
42
43struct throtl_grp {
Vivek Goyale43473b2010-09-15 17:06:35 -040044 /* active throtl group service_tree member */
45 struct rb_node rb_node;
46
47 /*
48 * Dispatch time in jiffies. This is the estimated time when group
49 * will unthrottle and is ready to dispatch more bio. It is used as
50 * key to sort active groups in service tree.
51 */
52 unsigned long disptime;
53
Vivek Goyale43473b2010-09-15 17:06:35 -040054 unsigned int flags;
55
56 /* Two lists for READ and WRITE */
57 struct bio_list bio_lists[2];
58
59 /* Number of queued bios on READ and WRITE lists */
60 unsigned int nr_queued[2];
61
62 /* bytes per second rate limits */
63 uint64_t bps[2];
64
Vivek Goyal8e89d132010-09-15 17:06:37 -040065 /* IOPS limits */
66 unsigned int iops[2];
67
Vivek Goyale43473b2010-09-15 17:06:35 -040068 /* Number of bytes disptached in current slice */
69 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040070 /* Number of bio's dispatched in current slice */
71 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040072
73 /* When did we start a new slice */
74 unsigned long slice_start[2];
75 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020076
77 /* Some throttle limits got updated for the group */
Andreas Schwab6f037932011-03-30 12:21:56 +020078 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -040079};
80
81struct throtl_data
82{
Vivek Goyale43473b2010-09-15 17:06:35 -040083 /* service tree for active throtl groups */
84 struct throtl_rb_root tg_service_tree;
85
Vivek Goyal29b12582011-05-19 15:38:24 -040086 struct throtl_grp *root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -040087 struct request_queue *queue;
88
89 /* Total Number of queued bios on READ and WRITE lists */
90 unsigned int nr_queued[2];
91
92 /*
Vivek Goyal02977e42010-10-01 14:49:48 +020093 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -040094 */
95 unsigned int nr_undestroyed_grps;
96
97 /* Work for dispatching throttled bios */
98 struct delayed_work throtl_work;
Vivek Goyalfe071432010-10-01 14:49:49 +020099
Andreas Schwab6f037932011-03-30 12:21:56 +0200100 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400101};
102
Tejun Heo03814112012-03-05 13:15:14 -0800103static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
104{
105 return blkg_to_pdata(blkg, &blkio_policy_throtl);
106}
107
108static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
109{
110 return pdata_to_blkg(tg, &blkio_policy_throtl);
111}
112
Vivek Goyale43473b2010-09-15 17:06:35 -0400113enum tg_state_flags {
114 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
115};
116
117#define THROTL_TG_FNS(name) \
118static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
119{ \
120 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
121} \
122static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
123{ \
124 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
125} \
126static inline int throtl_tg_##name(const struct throtl_grp *tg) \
127{ \
128 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
129}
130
131THROTL_TG_FNS(on_rr);
132
133#define throtl_log_tg(td, tg, fmt, args...) \
134 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800135 blkg_path(tg_to_blkg(tg)), ##args); \
Vivek Goyale43473b2010-09-15 17:06:35 -0400136
137#define throtl_log(td, fmt, args...) \
138 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
139
Joe Perchesd2f31a52011-06-13 20:19:27 +0200140static inline unsigned int total_nr_queued(struct throtl_data *td)
Vivek Goyale43473b2010-09-15 17:06:35 -0400141{
Joe Perchesd2f31a52011-06-13 20:19:27 +0200142 return td->nr_queued[0] + td->nr_queued[1];
Vivek Goyale43473b2010-09-15 17:06:35 -0400143}
144
Tejun Heo03814112012-03-05 13:15:14 -0800145static void throtl_init_blkio_group(struct blkio_group *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400146{
Tejun Heo03814112012-03-05 13:15:14 -0800147 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800148
Vivek Goyala29a1712011-05-19 15:38:19 -0400149 RB_CLEAR_NODE(&tg->rb_node);
150 bio_list_init(&tg->bio_lists[0]);
151 bio_list_init(&tg->bio_lists[1]);
152 tg->limits_changed = false;
153
Tejun Heoe56da7e2012-03-05 13:15:07 -0800154 tg->bps[READ] = -1;
155 tg->bps[WRITE] = -1;
156 tg->iops[READ] = -1;
157 tg->iops[WRITE] = -1;
Vivek Goyala29a1712011-05-19 15:38:19 -0400158}
159
Tejun Heocd1604f2012-03-05 13:15:06 -0800160static void throtl_link_blkio_group(struct request_queue *q,
161 struct blkio_group *blkg)
Vivek Goyal269f5412011-05-19 15:38:25 -0400162{
Tejun Heo4eef3042012-03-05 13:15:18 -0800163 list_add(&blkg->q_node[BLKIO_POLICY_THROTL],
164 &q->blkg_list[BLKIO_POLICY_THROTL]);
165 q->nr_blkgs[BLKIO_POLICY_THROTL]++;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400166}
167
168static struct
Tejun Heocd1604f2012-03-05 13:15:06 -0800169throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400170{
Vivek Goyale43473b2010-09-15 17:06:35 -0400171 /*
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700172 * This is the common case when there are no blkio cgroups.
Tejun Heocd1604f2012-03-05 13:15:06 -0800173 * Avoid lookup in this case
174 */
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700175 if (blkcg == &blkio_root_cgroup)
Tejun Heo7a4dd282012-03-05 13:15:09 -0800176 return td->root_tg;
Vivek Goyale43473b2010-09-15 17:06:35 -0400177
Tejun Heo03814112012-03-05 13:15:14 -0800178 return blkg_to_tg(blkg_lookup(blkcg, td->queue, BLKIO_POLICY_THROTL));
Vivek Goyale43473b2010-09-15 17:06:35 -0400179}
180
Tejun Heocd1604f2012-03-05 13:15:06 -0800181static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
182 struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400183{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400184 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800185 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800186
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400187 /*
Tejun Heocd1604f2012-03-05 13:15:06 -0800188 * This is the common case when there are no blkio cgroups.
189 * Avoid lookup in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400190 */
Tejun Heocd1604f2012-03-05 13:15:06 -0800191 if (blkcg == &blkio_root_cgroup) {
Vivek Goyal29b12582011-05-19 15:38:24 -0400192 tg = td->root_tg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800193 } else {
194 struct blkio_group *blkg;
195
196 blkg = blkg_lookup_create(blkcg, q, BLKIO_POLICY_THROTL, false);
197
198 /* if %NULL and @q is alive, fall back to root_tg */
199 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800200 tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800201 else if (!blk_queue_dead(q))
202 tg = td->root_tg;
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400203 }
204
Vivek Goyale43473b2010-09-15 17:06:35 -0400205 return tg;
206}
207
208static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
209{
210 /* Service tree is empty */
211 if (!root->count)
212 return NULL;
213
214 if (!root->left)
215 root->left = rb_first(&root->rb);
216
217 if (root->left)
218 return rb_entry_tg(root->left);
219
220 return NULL;
221}
222
223static void rb_erase_init(struct rb_node *n, struct rb_root *root)
224{
225 rb_erase(n, root);
226 RB_CLEAR_NODE(n);
227}
228
229static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
230{
231 if (root->left == n)
232 root->left = NULL;
233 rb_erase_init(n, &root->rb);
234 --root->count;
235}
236
237static void update_min_dispatch_time(struct throtl_rb_root *st)
238{
239 struct throtl_grp *tg;
240
241 tg = throtl_rb_first(st);
242 if (!tg)
243 return;
244
245 st->min_disptime = tg->disptime;
246}
247
248static void
249tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
250{
251 struct rb_node **node = &st->rb.rb_node;
252 struct rb_node *parent = NULL;
253 struct throtl_grp *__tg;
254 unsigned long key = tg->disptime;
255 int left = 1;
256
257 while (*node != NULL) {
258 parent = *node;
259 __tg = rb_entry_tg(parent);
260
261 if (time_before(key, __tg->disptime))
262 node = &parent->rb_left;
263 else {
264 node = &parent->rb_right;
265 left = 0;
266 }
267 }
268
269 if (left)
270 st->left = &tg->rb_node;
271
272 rb_link_node(&tg->rb_node, parent, node);
273 rb_insert_color(&tg->rb_node, &st->rb);
274}
275
276static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
277{
278 struct throtl_rb_root *st = &td->tg_service_tree;
279
280 tg_service_tree_add(st, tg);
281 throtl_mark_tg_on_rr(tg);
282 st->count++;
283}
284
285static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
286{
287 if (!throtl_tg_on_rr(tg))
288 __throtl_enqueue_tg(td, tg);
289}
290
291static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
292{
293 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
294 throtl_clear_tg_on_rr(tg);
295}
296
297static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
298{
299 if (throtl_tg_on_rr(tg))
300 __throtl_dequeue_tg(td, tg);
301}
302
303static void throtl_schedule_next_dispatch(struct throtl_data *td)
304{
305 struct throtl_rb_root *st = &td->tg_service_tree;
306
307 /*
308 * If there are more bios pending, schedule more work.
309 */
310 if (!total_nr_queued(td))
311 return;
312
313 BUG_ON(!st->count);
314
315 update_min_dispatch_time(st);
316
317 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500318 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400319 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500320 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400321}
322
323static inline void
324throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
325{
326 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400327 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400328 tg->slice_start[rw] = jiffies;
329 tg->slice_end[rw] = jiffies + throtl_slice;
330 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
331 rw == READ ? 'R' : 'W', tg->slice_start[rw],
332 tg->slice_end[rw], jiffies);
333}
334
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100335static inline void throtl_set_slice_end(struct throtl_data *td,
336 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
337{
338 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
339}
340
Vivek Goyale43473b2010-09-15 17:06:35 -0400341static inline void throtl_extend_slice(struct throtl_data *td,
342 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
343{
344 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
345 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
346 rw == READ ? 'R' : 'W', tg->slice_start[rw],
347 tg->slice_end[rw], jiffies);
348}
349
350/* Determine if previously allocated or extended slice is complete or not */
351static bool
352throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
353{
354 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
355 return 0;
356
357 return 1;
358}
359
360/* Trim the used slices and adjust slice start accordingly */
361static inline void
362throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
363{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200364 unsigned long nr_slices, time_elapsed, io_trim;
365 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400366
367 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
368
369 /*
370 * If bps are unlimited (-1), then time slice don't get
371 * renewed. Don't try to trim the slice if slice is used. A new
372 * slice will start when appropriate.
373 */
374 if (throtl_slice_used(td, tg, rw))
375 return;
376
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100377 /*
378 * A bio has been dispatched. Also adjust slice_end. It might happen
379 * that initially cgroup limit was very low resulting in high
380 * slice_end, but later limit was bumped up and bio was dispached
381 * sooner, then we need to reduce slice_end. A high bogus slice_end
382 * is bad because it does not allow new slice to start.
383 */
384
385 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
386
Vivek Goyale43473b2010-09-15 17:06:35 -0400387 time_elapsed = jiffies - tg->slice_start[rw];
388
389 nr_slices = time_elapsed / throtl_slice;
390
391 if (!nr_slices)
392 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200393 tmp = tg->bps[rw] * throtl_slice * nr_slices;
394 do_div(tmp, HZ);
395 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400396
Vivek Goyal8e89d132010-09-15 17:06:37 -0400397 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400398
Vivek Goyal8e89d132010-09-15 17:06:37 -0400399 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400400 return;
401
402 if (tg->bytes_disp[rw] >= bytes_trim)
403 tg->bytes_disp[rw] -= bytes_trim;
404 else
405 tg->bytes_disp[rw] = 0;
406
Vivek Goyal8e89d132010-09-15 17:06:37 -0400407 if (tg->io_disp[rw] >= io_trim)
408 tg->io_disp[rw] -= io_trim;
409 else
410 tg->io_disp[rw] = 0;
411
Vivek Goyale43473b2010-09-15 17:06:35 -0400412 tg->slice_start[rw] += nr_slices * throtl_slice;
413
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200414 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400415 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400416 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400417 tg->slice_start[rw], tg->slice_end[rw], jiffies);
418}
419
Vivek Goyal8e89d132010-09-15 17:06:37 -0400420static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
421 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400422{
423 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400424 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400425 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200426 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400427
Vivek Goyal8e89d132010-09-15 17:06:37 -0400428 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400429
Vivek Goyal8e89d132010-09-15 17:06:37 -0400430 /* Slice has just started. Consider one slice interval */
431 if (!jiffy_elapsed)
432 jiffy_elapsed_rnd = throtl_slice;
433
434 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
435
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200436 /*
437 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
438 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
439 * will allow dispatch after 1 second and after that slice should
440 * have been trimmed.
441 */
442
443 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
444 do_div(tmp, HZ);
445
446 if (tmp > UINT_MAX)
447 io_allowed = UINT_MAX;
448 else
449 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400450
451 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400452 if (wait)
453 *wait = 0;
454 return 1;
455 }
456
Vivek Goyal8e89d132010-09-15 17:06:37 -0400457 /* Calc approx time to dispatch */
458 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
459
460 if (jiffy_wait > jiffy_elapsed)
461 jiffy_wait = jiffy_wait - jiffy_elapsed;
462 else
463 jiffy_wait = 1;
464
465 if (wait)
466 *wait = jiffy_wait;
467 return 0;
468}
469
470static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
471 struct bio *bio, unsigned long *wait)
472{
473 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200474 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400475 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400476
477 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
478
479 /* Slice has just started. Consider one slice interval */
480 if (!jiffy_elapsed)
481 jiffy_elapsed_rnd = throtl_slice;
482
483 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
484
Vivek Goyal5e901a22010-10-01 21:16:38 +0200485 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
486 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200487 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400488
489 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
490 if (wait)
491 *wait = 0;
492 return 1;
493 }
494
495 /* Calc approx time to dispatch */
496 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
497 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
498
499 if (!jiffy_wait)
500 jiffy_wait = 1;
501
502 /*
503 * This wait time is without taking into consideration the rounding
504 * up we did. Add that time also.
505 */
506 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400507 if (wait)
508 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400509 return 0;
510}
Vivek Goyale43473b2010-09-15 17:06:35 -0400511
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400512static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
513 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
514 return 1;
515 return 0;
516}
517
Vivek Goyal8e89d132010-09-15 17:06:37 -0400518/*
519 * Returns whether one can dispatch a bio or not. Also returns approx number
520 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
521 */
522static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
523 struct bio *bio, unsigned long *wait)
524{
525 bool rw = bio_data_dir(bio);
526 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
527
528 /*
529 * Currently whole state machine of group depends on first bio
530 * queued in the group bio list. So one should not be calling
531 * this function with a different bio if there are other bios
532 * queued.
533 */
534 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
535
536 /* If tg->bps = -1, then BW is unlimited */
537 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
538 if (wait)
539 *wait = 0;
540 return 1;
541 }
542
543 /*
544 * If previous slice expired, start a new one otherwise renew/extend
545 * existing slice to make sure it is at least throtl_slice interval
546 * long since now.
547 */
548 if (throtl_slice_used(td, tg, rw))
549 throtl_start_new_slice(td, tg, rw);
550 else {
551 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
552 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
553 }
554
555 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
556 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
557 if (wait)
558 *wait = 0;
559 return 1;
560 }
561
562 max_wait = max(bps_wait, iops_wait);
563
564 if (wait)
565 *wait = max_wait;
566
567 if (time_before(tg->slice_end[rw], jiffies + max_wait))
568 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400569
570 return 0;
571}
572
573static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
574{
575 bool rw = bio_data_dir(bio);
Shaohua Lie5a94f52011-08-01 10:31:06 +0200576 bool sync = rw_is_sync(bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400577
578 /* Charge the bio to the group */
579 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400580 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400581
Tejun Heoc1768262012-03-05 13:15:17 -0800582 blkiocg_update_dispatch_stats(tg_to_blkg(tg), &blkio_policy_throtl,
583 bio->bi_size, rw, sync);
Vivek Goyale43473b2010-09-15 17:06:35 -0400584}
585
586static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
587 struct bio *bio)
588{
589 bool rw = bio_data_dir(bio);
590
591 bio_list_add(&tg->bio_lists[rw], bio);
592 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800593 blkg_get(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400594 tg->nr_queued[rw]++;
595 td->nr_queued[rw]++;
596 throtl_enqueue_tg(td, tg);
597}
598
599static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
600{
601 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
602 struct bio *bio;
603
604 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
605 tg_may_dispatch(td, tg, bio, &read_wait);
606
607 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
608 tg_may_dispatch(td, tg, bio, &write_wait);
609
610 min_wait = min(read_wait, write_wait);
611 disptime = jiffies + min_wait;
612
Vivek Goyale43473b2010-09-15 17:06:35 -0400613 /* Update dispatch time */
614 throtl_dequeue_tg(td, tg);
615 tg->disptime = disptime;
616 throtl_enqueue_tg(td, tg);
617}
618
619static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
620 bool rw, struct bio_list *bl)
621{
622 struct bio *bio;
623
624 bio = bio_list_pop(&tg->bio_lists[rw]);
625 tg->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800626 /* Drop bio reference on blkg */
627 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400628
629 BUG_ON(td->nr_queued[rw] <= 0);
630 td->nr_queued[rw]--;
631
632 throtl_charge_bio(tg, bio);
633 bio_list_add(bl, bio);
634 bio->bi_rw |= REQ_THROTTLED;
635
636 throtl_trim_slice(td, tg, rw);
637}
638
639static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
640 struct bio_list *bl)
641{
642 unsigned int nr_reads = 0, nr_writes = 0;
643 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100644 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400645 struct bio *bio;
646
647 /* Try to dispatch 75% READS and 25% WRITES */
648
649 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
650 && tg_may_dispatch(td, tg, bio, NULL)) {
651
652 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
653 nr_reads++;
654
655 if (nr_reads >= max_nr_reads)
656 break;
657 }
658
659 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
660 && tg_may_dispatch(td, tg, bio, NULL)) {
661
662 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
663 nr_writes++;
664
665 if (nr_writes >= max_nr_writes)
666 break;
667 }
668
669 return nr_reads + nr_writes;
670}
671
672static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
673{
674 unsigned int nr_disp = 0;
675 struct throtl_grp *tg;
676 struct throtl_rb_root *st = &td->tg_service_tree;
677
678 while (1) {
679 tg = throtl_rb_first(st);
680
681 if (!tg)
682 break;
683
684 if (time_before(jiffies, tg->disptime))
685 break;
686
687 throtl_dequeue_tg(td, tg);
688
689 nr_disp += throtl_dispatch_tg(td, tg, bl);
690
691 if (tg->nr_queued[0] || tg->nr_queued[1]) {
692 tg_update_disptime(td, tg);
693 throtl_enqueue_tg(td, tg);
694 }
695
696 if (nr_disp >= throtl_quantum)
697 break;
698 }
699
700 return nr_disp;
701}
702
Vivek Goyalfe071432010-10-01 14:49:49 +0200703static void throtl_process_limit_change(struct throtl_data *td)
704{
Tejun Heo4eef3042012-03-05 13:15:18 -0800705 struct request_queue *q = td->queue;
706 struct blkio_group *blkg, *n;
Vivek Goyalfe071432010-10-01 14:49:49 +0200707
Vivek Goyalde701c72011-03-07 21:09:32 +0100708 if (!td->limits_changed)
Vivek Goyalfe071432010-10-01 14:49:49 +0200709 return;
710
Vivek Goyalde701c72011-03-07 21:09:32 +0100711 xchg(&td->limits_changed, false);
Vivek Goyalfe071432010-10-01 14:49:49 +0200712
Vivek Goyalde701c72011-03-07 21:09:32 +0100713 throtl_log(td, "limits changed");
Vivek Goyalfe071432010-10-01 14:49:49 +0200714
Tejun Heo4eef3042012-03-05 13:15:18 -0800715 list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL],
716 q_node[BLKIO_POLICY_THROTL]) {
717 struct throtl_grp *tg = blkg_to_tg(blkg);
718
Vivek Goyalde701c72011-03-07 21:09:32 +0100719 if (!tg->limits_changed)
720 continue;
Vivek Goyalfe071432010-10-01 14:49:49 +0200721
Vivek Goyalde701c72011-03-07 21:09:32 +0100722 if (!xchg(&tg->limits_changed, false))
723 continue;
724
725 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
726 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
727 tg->iops[READ], tg->iops[WRITE]);
728
Vivek Goyal04521db2011-03-22 21:54:29 +0100729 /*
730 * Restart the slices for both READ and WRITES. It
731 * might happen that a group's limit are dropped
732 * suddenly and we don't want to account recently
733 * dispatched IO with new low rate
734 */
735 throtl_start_new_slice(td, tg, 0);
736 throtl_start_new_slice(td, tg, 1);
737
Vivek Goyalde701c72011-03-07 21:09:32 +0100738 if (throtl_tg_on_rr(tg))
739 tg_update_disptime(td, tg);
740 }
Vivek Goyalfe071432010-10-01 14:49:49 +0200741}
742
Vivek Goyale43473b2010-09-15 17:06:35 -0400743/* Dispatch throttled bios. Should be called without queue lock held. */
744static int throtl_dispatch(struct request_queue *q)
745{
746 struct throtl_data *td = q->td;
747 unsigned int nr_disp = 0;
748 struct bio_list bio_list_on_stack;
749 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100750 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400751
752 spin_lock_irq(q->queue_lock);
753
Vivek Goyalfe071432010-10-01 14:49:49 +0200754 throtl_process_limit_change(td);
755
Vivek Goyale43473b2010-09-15 17:06:35 -0400756 if (!total_nr_queued(td))
757 goto out;
758
759 bio_list_init(&bio_list_on_stack);
760
Joe Perchesd2f31a52011-06-13 20:19:27 +0200761 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Vivek Goyale43473b2010-09-15 17:06:35 -0400762 total_nr_queued(td), td->nr_queued[READ],
763 td->nr_queued[WRITE]);
764
765 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
766
767 if (nr_disp)
768 throtl_log(td, "bios disp=%u", nr_disp);
769
770 throtl_schedule_next_dispatch(td);
771out:
772 spin_unlock_irq(q->queue_lock);
773
774 /*
775 * If we dispatched some requests, unplug the queue to make sure
776 * immediate dispatch
777 */
778 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100779 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400780 while((bio = bio_list_pop(&bio_list_on_stack)))
781 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100782 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400783 }
784 return nr_disp;
785}
786
787void blk_throtl_work(struct work_struct *work)
788{
789 struct throtl_data *td = container_of(work, struct throtl_data,
790 throtl_work.work);
791 struct request_queue *q = td->queue;
792
793 throtl_dispatch(q);
794}
795
796/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500797static void
798throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400799{
800
Vivek Goyale43473b2010-09-15 17:06:35 -0400801 struct delayed_work *dwork = &td->throtl_work;
802
Vivek Goyal04521db2011-03-22 21:54:29 +0100803 /* schedule work if limits changed even if no bio is queued */
Joe Perchesd2f31a52011-06-13 20:19:27 +0200804 if (total_nr_queued(td) || td->limits_changed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400805 /*
806 * We might have a work scheduled to be executed in future.
807 * Cancel that and schedule a new one.
808 */
809 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500810 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400811 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
812 delay, jiffies);
813 }
814}
Vivek Goyale43473b2010-09-15 17:06:35 -0400815
816static void
817throtl_destroy_tg(struct throtl_data *td, struct throtl_grp *tg)
818{
Tejun Heo4eef3042012-03-05 13:15:18 -0800819 struct blkio_group *blkg = tg_to_blkg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400820
Tejun Heo4eef3042012-03-05 13:15:18 -0800821 /* Something wrong if we are trying to remove same group twice */
822 WARN_ON_ONCE(list_empty(&blkg->q_node[BLKIO_POLICY_THROTL]));
823
824 list_del_init(&blkg->q_node[BLKIO_POLICY_THROTL]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400825
826 /*
827 * Put the reference taken at the time of creation so that when all
828 * queues are gone, group can be destroyed.
829 */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800830 blkg_put(tg_to_blkg(tg));
Tejun Heo4eef3042012-03-05 13:15:18 -0800831 td->queue->nr_blkgs[BLKIO_POLICY_THROTL]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400832}
833
Tejun Heo72e06c22012-03-05 13:15:00 -0800834static bool throtl_release_tgs(struct throtl_data *td, bool release_root)
Vivek Goyale43473b2010-09-15 17:06:35 -0400835{
Tejun Heo4eef3042012-03-05 13:15:18 -0800836 struct request_queue *q = td->queue;
837 struct blkio_group *blkg, *n;
Tejun Heo72e06c22012-03-05 13:15:00 -0800838 bool empty = true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400839
Tejun Heo4eef3042012-03-05 13:15:18 -0800840 list_for_each_entry_safe(blkg, n, &q->blkg_list[BLKIO_POLICY_THROTL],
841 q_node[BLKIO_POLICY_THROTL]) {
842 struct throtl_grp *tg = blkg_to_tg(blkg);
843
Tejun Heo72e06c22012-03-05 13:15:00 -0800844 /* skip root? */
845 if (!release_root && tg == td->root_tg)
846 continue;
847
Vivek Goyale43473b2010-09-15 17:06:35 -0400848 /*
849 * If cgroup removal path got to blk_group first and removed
850 * it from cgroup list, then it will take care of destroying
851 * cfqg also.
852 */
Tejun Heo4eef3042012-03-05 13:15:18 -0800853 if (!blkiocg_del_blkio_group(blkg))
Vivek Goyale43473b2010-09-15 17:06:35 -0400854 throtl_destroy_tg(td, tg);
Tejun Heo72e06c22012-03-05 13:15:00 -0800855 else
856 empty = false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400857 }
Tejun Heo72e06c22012-03-05 13:15:00 -0800858 return empty;
Vivek Goyale43473b2010-09-15 17:06:35 -0400859}
860
Vivek Goyale43473b2010-09-15 17:06:35 -0400861/*
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.
866 *
Tejun Heoca32aef2012-03-05 13:15:03 -0800867 * 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
869 * are rcu read lock.
Vivek Goyale43473b2010-09-15 17:06:35 -0400870 *
Tejun Heoca32aef2012-03-05 13:15:03 -0800871 * @q was fetched from blkio_group under blkio_cgroup->lock. That means
Vivek Goyale43473b2010-09-15 17:06:35 -0400872 * it should not be NULL as even if queue was going away, cgroup deltion
873 * path got to it first.
874 */
Tejun Heoca32aef2012-03-05 13:15:03 -0800875void throtl_unlink_blkio_group(struct request_queue *q,
876 struct blkio_group *blkg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400877{
878 unsigned long flags;
Vivek Goyale43473b2010-09-15 17:06:35 -0400879
Tejun Heoca32aef2012-03-05 13:15:03 -0800880 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo03814112012-03-05 13:15:14 -0800881 throtl_destroy_tg(q->td, blkg_to_tg(blkg));
Tejun Heoca32aef2012-03-05 13:15:03 -0800882 spin_unlock_irqrestore(q->queue_lock, flags);
Vivek Goyale43473b2010-09-15 17:06:35 -0400883}
884
Tejun Heo72e06c22012-03-05 13:15:00 -0800885static bool throtl_clear_queue(struct request_queue *q)
886{
887 lockdep_assert_held(q->queue_lock);
888
889 /*
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
894 * removed soon.
895 */
896 return throtl_release_tgs(q->td, false);
897}
898
Vivek Goyalde701c72011-03-07 21:09:32 +0100899static void throtl_update_blkio_group_common(struct throtl_data *td,
900 struct throtl_grp *tg)
901{
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);
906}
907
Vivek Goyalfe071432010-10-01 14:49:49 +0200908/*
Tejun Heoca32aef2012-03-05 13:15:03 -0800909 * For all update functions, @q should be a valid pointer because these
Vivek Goyalfe071432010-10-01 14:49:49 +0200910 * update functions are called under blkcg_lock, that means, blkg is
Tejun Heoca32aef2012-03-05 13:15:03 -0800911 * valid and in turn @q is valid. queue exit path can not race because
Vivek Goyalfe071432010-10-01 14:49:49 +0200912 * of blkcg_lock
913 *
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.
916 */
Tejun Heoca32aef2012-03-05 13:15:03 -0800917static void throtl_update_blkio_group_read_bps(struct request_queue *q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200918 struct blkio_group *blkg, u64 read_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400919{
Tejun Heo03814112012-03-05 13:15:14 -0800920 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200921
Vivek Goyalde701c72011-03-07 21:09:32 +0100922 tg->bps[READ] = read_bps;
Tejun Heoca32aef2012-03-05 13:15:03 -0800923 throtl_update_blkio_group_common(q->td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400924}
925
Tejun Heoca32aef2012-03-05 13:15:03 -0800926static void throtl_update_blkio_group_write_bps(struct request_queue *q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200927 struct blkio_group *blkg, u64 write_bps)
Vivek Goyale43473b2010-09-15 17:06:35 -0400928{
Tejun Heo03814112012-03-05 13:15:14 -0800929 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200930
Vivek Goyalde701c72011-03-07 21:09:32 +0100931 tg->bps[WRITE] = write_bps;
Tejun Heoca32aef2012-03-05 13:15:03 -0800932 throtl_update_blkio_group_common(q->td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400933}
934
Tejun Heoca32aef2012-03-05 13:15:03 -0800935static void throtl_update_blkio_group_read_iops(struct request_queue *q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200936 struct blkio_group *blkg, unsigned int read_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400937{
Tejun Heo03814112012-03-05 13:15:14 -0800938 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200939
Vivek Goyalde701c72011-03-07 21:09:32 +0100940 tg->iops[READ] = read_iops;
Tejun Heoca32aef2012-03-05 13:15:03 -0800941 throtl_update_blkio_group_common(q->td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400942}
943
Tejun Heoca32aef2012-03-05 13:15:03 -0800944static void throtl_update_blkio_group_write_iops(struct request_queue *q,
Vivek Goyalfe071432010-10-01 14:49:49 +0200945 struct blkio_group *blkg, unsigned int write_iops)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400946{
Tejun Heo03814112012-03-05 13:15:14 -0800947 struct throtl_grp *tg = blkg_to_tg(blkg);
Vivek Goyalfe071432010-10-01 14:49:49 +0200948
Vivek Goyalde701c72011-03-07 21:09:32 +0100949 tg->iops[WRITE] = write_iops;
Tejun Heoca32aef2012-03-05 13:15:03 -0800950 throtl_update_blkio_group_common(q->td, tg);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400951}
952
Vivek Goyalda527772011-03-02 19:05:33 -0500953static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -0400954{
955 struct throtl_data *td = q->td;
956
957 cancel_delayed_work_sync(&td->throtl_work);
958}
959
960static struct blkio_policy_type blkio_policy_throtl = {
961 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -0800962 .blkio_init_group_fn = throtl_init_blkio_group,
Tejun Heocd1604f2012-03-05 13:15:06 -0800963 .blkio_link_group_fn = throtl_link_blkio_group,
Vivek Goyale43473b2010-09-15 17:06:35 -0400964 .blkio_unlink_group_fn = throtl_unlink_blkio_group,
Tejun Heo72e06c22012-03-05 13:15:00 -0800965 .blkio_clear_queue_fn = throtl_clear_queue,
Vivek Goyale43473b2010-09-15 17:06:35 -0400966 .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,
Vivek Goyal8e89d132010-09-15 17:06:37 -0400970 .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,
Vivek Goyale43473b2010-09-15 17:06:35 -0400974 },
Vivek Goyal8e89d132010-09-15 17:06:37 -0400975 .plid = BLKIO_POLICY_THROTL,
Tejun Heo03814112012-03-05 13:15:14 -0800976 .pdata_size = sizeof(struct throtl_grp),
Vivek Goyale43473b2010-09-15 17:06:35 -0400977};
978
Tejun Heobc16a4f2011-10-19 14:33:01 +0200979bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -0400980{
981 struct throtl_data *td = q->td;
982 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -0400983 bool rw = bio_data_dir(bio), update_disptime = true;
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400984 struct blkio_cgroup *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +0200985 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400986
987 if (bio->bi_rw & REQ_THROTTLED) {
988 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +0200989 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -0400990 }
991
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400992 /*
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.
996 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400997 rcu_read_lock();
998 blkcg = task_blkio_cgroup(current);
Tejun Heocd1604f2012-03-05 13:15:06 -0800999 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001000 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001001 if (tg_no_rule_group(tg, rw)) {
Tejun Heo03814112012-03-05 13:15:14 -08001002 blkiocg_update_dispatch_stats(tg_to_blkg(tg),
Tejun Heoc1768262012-03-05 13:15:17 -08001003 &blkio_policy_throtl,
Tejun Heo03814112012-03-05 13:15:14 -08001004 bio->bi_size, rw,
1005 rw_is_sync(bio->bi_rw));
Tejun Heo2a7f1242012-03-05 13:15:01 -08001006 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001007 }
1008 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001009
1010 /*
1011 * Either group has not been allocated yet or it is not an unlimited
1012 * IO group
1013 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001014 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001015 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001016 if (unlikely(!tg))
1017 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001018
Vivek Goyale43473b2010-09-15 17:06:35 -04001019 if (tg->nr_queued[rw]) {
1020 /*
1021 * There is already another bio queued in same dir. No
1022 * need to update dispatch time.
1023 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001024 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001025 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001026
Vivek Goyale43473b2010-09-15 17:06:35 -04001027 }
1028
1029 /* Bio is with-in rate limit of group */
1030 if (tg_may_dispatch(td, tg, bio, NULL)) {
1031 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001032
1033 /*
1034 * We need to trim slice even when bios are not being queued
1035 * otherwise it might happen that a bio is not queued for
1036 * a long time and slice keeps on extending and trim is not
1037 * called for a long time. Now if limits are reduced suddenly
1038 * we take into account all the IO dispatched so far at new
1039 * low rate and * newly queued IO gets a really long dispatch
1040 * time.
1041 *
1042 * So keep on trimming slice even if bio is not queued.
1043 */
1044 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001045 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001046 }
1047
1048queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001049 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001050 " iodisp=%u iops=%u queued=%d/%d",
1051 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001052 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001053 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001054 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1055
1056 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001057 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001058
1059 if (update_disptime) {
1060 tg_update_disptime(td, tg);
1061 throtl_schedule_next_dispatch(td);
1062 }
1063
Tejun Heobc16a4f2011-10-19 14:33:01 +02001064out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001065 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001066out_unlock_rcu:
1067 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001068out:
1069 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001070}
1071
Tejun Heoc9a929d2011-10-19 14:42:16 +02001072/**
1073 * blk_throtl_drain - drain throttled bios
1074 * @q: request_queue to drain throttled bios for
1075 *
1076 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1077 */
1078void blk_throtl_drain(struct request_queue *q)
1079 __releases(q->queue_lock) __acquires(q->queue_lock)
1080{
1081 struct throtl_data *td = q->td;
1082 struct throtl_rb_root *st = &td->tg_service_tree;
1083 struct throtl_grp *tg;
1084 struct bio_list bl;
1085 struct bio *bio;
1086
Jens Axboe334c2b02011-10-25 15:51:48 +02001087 WARN_ON_ONCE(!queue_is_locked(q));
Tejun Heoc9a929d2011-10-19 14:42:16 +02001088
1089 bio_list_init(&bl);
1090
1091 while ((tg = throtl_rb_first(st))) {
1092 throtl_dequeue_tg(td, tg);
1093
1094 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1095 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1096 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1097 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1098 }
1099 spin_unlock_irq(q->queue_lock);
1100
1101 while ((bio = bio_list_pop(&bl)))
1102 generic_make_request(bio);
1103
1104 spin_lock_irq(q->queue_lock);
1105}
1106
Vivek Goyale43473b2010-09-15 17:06:35 -04001107int blk_throtl_init(struct request_queue *q)
1108{
1109 struct throtl_data *td;
Tejun Heocd1604f2012-03-05 13:15:06 -08001110 struct blkio_group *blkg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001111
1112 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1113 if (!td)
1114 return -ENOMEM;
1115
Vivek Goyale43473b2010-09-15 17:06:35 -04001116 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalde701c72011-03-07 21:09:32 +01001117 td->limits_changed = false;
Vivek Goyala29a1712011-05-19 15:38:19 -04001118 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001119
Tejun Heocd1604f2012-03-05 13:15:06 -08001120 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001121 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001122
Tejun Heocd1604f2012-03-05 13:15:06 -08001123 /* alloc and init root group. */
Tejun Heof51b8022012-03-05 13:15:05 -08001124 rcu_read_lock();
1125 spin_lock_irq(q->queue_lock);
1126
Tejun Heocd1604f2012-03-05 13:15:06 -08001127 blkg = blkg_lookup_create(&blkio_root_cgroup, q, BLKIO_POLICY_THROTL,
1128 true);
1129 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -08001130 td->root_tg = blkg_to_tg(blkg);
Tejun Heof51b8022012-03-05 13:15:05 -08001131
1132 spin_unlock_irq(q->queue_lock);
1133 rcu_read_unlock();
1134
1135 if (!td->root_tg) {
Vivek Goyal29b12582011-05-19 15:38:24 -04001136 kfree(td);
1137 return -ENOMEM;
1138 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001139 return 0;
1140}
1141
1142void blk_throtl_exit(struct request_queue *q)
1143{
1144 struct throtl_data *td = q->td;
Tejun Heo4eef3042012-03-05 13:15:18 -08001145 bool wait;
Vivek Goyale43473b2010-09-15 17:06:35 -04001146
1147 BUG_ON(!td);
1148
Vivek Goyalda527772011-03-02 19:05:33 -05001149 throtl_shutdown_wq(q);
Vivek Goyale43473b2010-09-15 17:06:35 -04001150
1151 spin_lock_irq(q->queue_lock);
Tejun Heo72e06c22012-03-05 13:15:00 -08001152 throtl_release_tgs(td, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001153
1154 /* If there are other groups */
Tejun Heo4eef3042012-03-05 13:15:18 -08001155 wait = q->nr_blkgs[BLKIO_POLICY_THROTL];
Vivek Goyale43473b2010-09-15 17:06:35 -04001156
1157 spin_unlock_irq(q->queue_lock);
1158
1159 /*
Tejun Heo03814112012-03-05 13:15:14 -08001160 * Wait for tg_to_blkg(tg)->q accessors to exit their grace periods.
Vivek Goyale43473b2010-09-15 17:06:35 -04001161 * Do this wait only if there are other undestroyed groups out
1162 * there (other than root group). This can happen if cgroup deletion
1163 * path claimed the responsibility of cleaning up a group before
1164 * queue cleanup code get to the group.
1165 *
1166 * Do not call synchronize_rcu() unconditionally as there are drivers
1167 * which create/delete request queue hundreds of times during scan/boot
1168 * and synchronize_rcu() can take significant time and slow down boot.
1169 */
1170 if (wait)
1171 synchronize_rcu();
Vivek Goyalfe071432010-10-01 14:49:49 +02001172
1173 /*
1174 * Just being safe to make sure after previous flush if some body did
1175 * update limits through cgroup and another work got queued, cancel
1176 * it.
1177 */
Vivek Goyalda527772011-03-02 19:05:33 -05001178 throtl_shutdown_wq(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001179
Tejun Heoc9a929d2011-10-19 14:42:16 +02001180 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001181}
1182
1183static int __init throtl_init(void)
1184{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001185 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1186 if (!kthrotld_workqueue)
1187 panic("Failed to create kthrotld\n");
1188
Vivek Goyale43473b2010-09-15 17:06:35 -04001189 blkio_policy_register(&blkio_policy_throtl);
1190 return 0;
1191}
1192
1193module_init(throtl_init);