blob: 2fc964e06ea427c760d59bbd6a9aff8738411e25 [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
Tejun Heo8a3d2612012-04-01 14:38:44 -070043/* Per-cpu group stats */
44struct tg_stats_cpu {
45 /* total bytes transferred */
46 struct blkg_rwstat service_bytes;
47 /* total IOs serviced, post merge */
48 struct blkg_rwstat serviced;
49};
50
Vivek Goyale43473b2010-09-15 17:06:35 -040051struct throtl_grp {
Vivek Goyale43473b2010-09-15 17:06:35 -040052 /* active throtl group service_tree member */
53 struct rb_node rb_node;
54
55 /*
56 * Dispatch time in jiffies. This is the estimated time when group
57 * will unthrottle and is ready to dispatch more bio. It is used as
58 * key to sort active groups in service tree.
59 */
60 unsigned long disptime;
61
Vivek Goyale43473b2010-09-15 17:06:35 -040062 unsigned int flags;
63
64 /* Two lists for READ and WRITE */
65 struct bio_list bio_lists[2];
66
67 /* Number of queued bios on READ and WRITE lists */
68 unsigned int nr_queued[2];
69
70 /* bytes per second rate limits */
71 uint64_t bps[2];
72
Vivek Goyal8e89d132010-09-15 17:06:37 -040073 /* IOPS limits */
74 unsigned int iops[2];
75
Vivek Goyale43473b2010-09-15 17:06:35 -040076 /* Number of bytes disptached in current slice */
77 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040078 /* Number of bio's dispatched in current slice */
79 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040080
81 /* When did we start a new slice */
82 unsigned long slice_start[2];
83 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020084
85 /* Some throttle limits got updated for the group */
Andreas Schwab6f037932011-03-30 12:21:56 +020086 int limits_changed;
Tejun Heo8a3d2612012-04-01 14:38:44 -070087
88 /* Per cpu stats pointer */
89 struct tg_stats_cpu __percpu *stats_cpu;
90
91 /* List of tgs waiting for per cpu stats memory to be allocated */
92 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -040093};
94
95struct throtl_data
96{
Vivek Goyale43473b2010-09-15 17:06:35 -040097 /* service tree for active throtl groups */
98 struct throtl_rb_root tg_service_tree;
99
Vivek Goyale43473b2010-09-15 17:06:35 -0400100 struct request_queue *queue;
101
102 /* Total Number of queued bios on READ and WRITE lists */
103 unsigned int nr_queued[2];
104
105 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200106 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400107 */
108 unsigned int nr_undestroyed_grps;
109
110 /* Work for dispatching throttled bios */
111 struct delayed_work throtl_work;
Vivek Goyalfe071432010-10-01 14:49:49 +0200112
Andreas Schwab6f037932011-03-30 12:21:56 +0200113 int limits_changed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400114};
115
Tejun Heo8a3d2612012-04-01 14:38:44 -0700116/* list and work item to allocate percpu group stats */
117static DEFINE_SPINLOCK(tg_stats_alloc_lock);
118static LIST_HEAD(tg_stats_alloc_list);
119
120static void tg_stats_alloc_fn(struct work_struct *);
121static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
122
Tejun Heo03814112012-03-05 13:15:14 -0800123static inline struct throtl_grp *blkg_to_tg(struct blkio_group *blkg)
124{
125 return blkg_to_pdata(blkg, &blkio_policy_throtl);
126}
127
128static inline struct blkio_group *tg_to_blkg(struct throtl_grp *tg)
129{
Tejun Heoaaec55a2012-04-01 14:38:42 -0700130 return pdata_to_blkg(tg);
Tejun Heo03814112012-03-05 13:15:14 -0800131}
132
Tejun Heo03d8e112012-04-13 13:11:32 -0700133static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
134{
135 return blkg_to_tg(td->queue->root_blkg);
136}
137
Vivek Goyale43473b2010-09-15 17:06:35 -0400138enum tg_state_flags {
139 THROTL_TG_FLAG_on_rr = 0, /* on round-robin busy list */
140};
141
142#define THROTL_TG_FNS(name) \
143static inline void throtl_mark_tg_##name(struct throtl_grp *tg) \
144{ \
145 (tg)->flags |= (1 << THROTL_TG_FLAG_##name); \
146} \
147static inline void throtl_clear_tg_##name(struct throtl_grp *tg) \
148{ \
149 (tg)->flags &= ~(1 << THROTL_TG_FLAG_##name); \
150} \
151static inline int throtl_tg_##name(const struct throtl_grp *tg) \
152{ \
153 return ((tg)->flags & (1 << THROTL_TG_FLAG_##name)) != 0; \
154}
155
156THROTL_TG_FNS(on_rr);
157
158#define throtl_log_tg(td, tg, fmt, args...) \
159 blk_add_trace_msg((td)->queue, "throtl %s " fmt, \
Tejun Heo03814112012-03-05 13:15:14 -0800160 blkg_path(tg_to_blkg(tg)), ##args); \
Vivek Goyale43473b2010-09-15 17:06:35 -0400161
162#define throtl_log(td, fmt, args...) \
163 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
164
Joe Perchesd2f31a52011-06-13 20:19:27 +0200165static inline unsigned int total_nr_queued(struct throtl_data *td)
Vivek Goyale43473b2010-09-15 17:06:35 -0400166{
Joe Perchesd2f31a52011-06-13 20:19:27 +0200167 return td->nr_queued[0] + td->nr_queued[1];
Vivek Goyale43473b2010-09-15 17:06:35 -0400168}
169
Tejun Heo8a3d2612012-04-01 14:38:44 -0700170/*
171 * Worker for allocating per cpu stat for tgs. This is scheduled on the
172 * system_nrt_wq once there are some groups on the alloc_list waiting for
173 * allocation.
174 */
175static void tg_stats_alloc_fn(struct work_struct *work)
176{
177 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
178 struct delayed_work *dwork = to_delayed_work(work);
179 bool empty = false;
180
181alloc_stats:
182 if (!stats_cpu) {
183 stats_cpu = alloc_percpu(struct tg_stats_cpu);
184 if (!stats_cpu) {
185 /* allocation failed, try again after some time */
186 queue_delayed_work(system_nrt_wq, dwork,
187 msecs_to_jiffies(10));
188 return;
189 }
190 }
191
192 spin_lock_irq(&tg_stats_alloc_lock);
193
194 if (!list_empty(&tg_stats_alloc_list)) {
195 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
196 struct throtl_grp,
197 stats_alloc_node);
198 swap(tg->stats_cpu, stats_cpu);
199 list_del_init(&tg->stats_alloc_node);
200 }
201
202 empty = list_empty(&tg_stats_alloc_list);
203 spin_unlock_irq(&tg_stats_alloc_lock);
204 if (!empty)
205 goto alloc_stats;
206}
207
Tejun Heo03814112012-03-05 13:15:14 -0800208static void throtl_init_blkio_group(struct blkio_group *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400209{
Tejun Heo03814112012-03-05 13:15:14 -0800210 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800211
Vivek Goyala29a1712011-05-19 15:38:19 -0400212 RB_CLEAR_NODE(&tg->rb_node);
213 bio_list_init(&tg->bio_lists[0]);
214 bio_list_init(&tg->bio_lists[1]);
215 tg->limits_changed = false;
216
Tejun Heoe56da7e2012-03-05 13:15:07 -0800217 tg->bps[READ] = -1;
218 tg->bps[WRITE] = -1;
219 tg->iops[READ] = -1;
220 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700221
222 /*
223 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
224 * but percpu allocator can't be called from IO path. Queue tg on
225 * tg_stats_alloc_list and allocate from work item.
226 */
227 spin_lock(&tg_stats_alloc_lock);
228 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
229 queue_delayed_work(system_nrt_wq, &tg_stats_alloc_work, 0);
230 spin_unlock(&tg_stats_alloc_lock);
231}
232
233static void throtl_exit_blkio_group(struct blkio_group *blkg)
234{
235 struct throtl_grp *tg = blkg_to_tg(blkg);
236
237 spin_lock(&tg_stats_alloc_lock);
238 list_del_init(&tg->stats_alloc_node);
239 spin_unlock(&tg_stats_alloc_lock);
240
241 free_percpu(tg->stats_cpu);
242}
243
244static void throtl_reset_group_stats(struct blkio_group *blkg)
245{
246 struct throtl_grp *tg = blkg_to_tg(blkg);
247 int cpu;
248
249 if (tg->stats_cpu == NULL)
250 return;
251
252 for_each_possible_cpu(cpu) {
253 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
254
255 blkg_rwstat_reset(&sc->service_bytes);
256 blkg_rwstat_reset(&sc->serviced);
257 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400258}
259
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400260static struct
Tejun Heocd1604f2012-03-05 13:15:06 -0800261throtl_grp *throtl_lookup_tg(struct throtl_data *td, struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400262{
Vivek Goyale43473b2010-09-15 17:06:35 -0400263 /*
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700264 * This is the common case when there are no blkio cgroups.
Tejun Heocd1604f2012-03-05 13:15:06 -0800265 * Avoid lookup in this case
266 */
Vivek Goyalbe2c6b12011-01-19 08:25:02 -0700267 if (blkcg == &blkio_root_cgroup)
Tejun Heo03d8e112012-04-13 13:11:32 -0700268 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400269
Tejun Heoe8989fa2012-03-05 13:15:20 -0800270 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400271}
272
Tejun Heocd1604f2012-03-05 13:15:06 -0800273static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
274 struct blkio_cgroup *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400275{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400276 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800277 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800278
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400279 /*
Tejun Heocd1604f2012-03-05 13:15:06 -0800280 * This is the common case when there are no blkio cgroups.
281 * Avoid lookup in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400282 */
Tejun Heocd1604f2012-03-05 13:15:06 -0800283 if (blkcg == &blkio_root_cgroup) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700284 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800285 } else {
286 struct blkio_group *blkg;
287
Tejun Heoaaec55a2012-04-01 14:38:42 -0700288 blkg = blkg_lookup_create(blkcg, q, false);
Tejun Heocd1604f2012-03-05 13:15:06 -0800289
290 /* if %NULL and @q is alive, fall back to root_tg */
291 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800292 tg = blkg_to_tg(blkg);
Tejun Heocd1604f2012-03-05 13:15:06 -0800293 else if (!blk_queue_dead(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700294 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400295 }
296
Vivek Goyale43473b2010-09-15 17:06:35 -0400297 return tg;
298}
299
300static struct throtl_grp *throtl_rb_first(struct throtl_rb_root *root)
301{
302 /* Service tree is empty */
303 if (!root->count)
304 return NULL;
305
306 if (!root->left)
307 root->left = rb_first(&root->rb);
308
309 if (root->left)
310 return rb_entry_tg(root->left);
311
312 return NULL;
313}
314
315static void rb_erase_init(struct rb_node *n, struct rb_root *root)
316{
317 rb_erase(n, root);
318 RB_CLEAR_NODE(n);
319}
320
321static void throtl_rb_erase(struct rb_node *n, struct throtl_rb_root *root)
322{
323 if (root->left == n)
324 root->left = NULL;
325 rb_erase_init(n, &root->rb);
326 --root->count;
327}
328
329static void update_min_dispatch_time(struct throtl_rb_root *st)
330{
331 struct throtl_grp *tg;
332
333 tg = throtl_rb_first(st);
334 if (!tg)
335 return;
336
337 st->min_disptime = tg->disptime;
338}
339
340static void
341tg_service_tree_add(struct throtl_rb_root *st, struct throtl_grp *tg)
342{
343 struct rb_node **node = &st->rb.rb_node;
344 struct rb_node *parent = NULL;
345 struct throtl_grp *__tg;
346 unsigned long key = tg->disptime;
347 int left = 1;
348
349 while (*node != NULL) {
350 parent = *node;
351 __tg = rb_entry_tg(parent);
352
353 if (time_before(key, __tg->disptime))
354 node = &parent->rb_left;
355 else {
356 node = &parent->rb_right;
357 left = 0;
358 }
359 }
360
361 if (left)
362 st->left = &tg->rb_node;
363
364 rb_link_node(&tg->rb_node, parent, node);
365 rb_insert_color(&tg->rb_node, &st->rb);
366}
367
368static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
369{
370 struct throtl_rb_root *st = &td->tg_service_tree;
371
372 tg_service_tree_add(st, tg);
373 throtl_mark_tg_on_rr(tg);
374 st->count++;
375}
376
377static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
378{
379 if (!throtl_tg_on_rr(tg))
380 __throtl_enqueue_tg(td, tg);
381}
382
383static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
384{
385 throtl_rb_erase(&tg->rb_node, &td->tg_service_tree);
386 throtl_clear_tg_on_rr(tg);
387}
388
389static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
390{
391 if (throtl_tg_on_rr(tg))
392 __throtl_dequeue_tg(td, tg);
393}
394
395static void throtl_schedule_next_dispatch(struct throtl_data *td)
396{
397 struct throtl_rb_root *st = &td->tg_service_tree;
398
399 /*
400 * If there are more bios pending, schedule more work.
401 */
402 if (!total_nr_queued(td))
403 return;
404
405 BUG_ON(!st->count);
406
407 update_min_dispatch_time(st);
408
409 if (time_before_eq(st->min_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500410 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400411 else
Vivek Goyal450adcb2011-03-01 13:40:54 -0500412 throtl_schedule_delayed_work(td, (st->min_disptime - jiffies));
Vivek Goyale43473b2010-09-15 17:06:35 -0400413}
414
415static inline void
416throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
417{
418 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400419 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400420 tg->slice_start[rw] = jiffies;
421 tg->slice_end[rw] = jiffies + throtl_slice;
422 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
423 rw == READ ? 'R' : 'W', tg->slice_start[rw],
424 tg->slice_end[rw], jiffies);
425}
426
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100427static inline void throtl_set_slice_end(struct throtl_data *td,
428 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
429{
430 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
431}
432
Vivek Goyale43473b2010-09-15 17:06:35 -0400433static inline void throtl_extend_slice(struct throtl_data *td,
434 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
435{
436 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
437 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
438 rw == READ ? 'R' : 'W', tg->slice_start[rw],
439 tg->slice_end[rw], jiffies);
440}
441
442/* Determine if previously allocated or extended slice is complete or not */
443static bool
444throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
445{
446 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
447 return 0;
448
449 return 1;
450}
451
452/* Trim the used slices and adjust slice start accordingly */
453static inline void
454throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
455{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200456 unsigned long nr_slices, time_elapsed, io_trim;
457 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400458
459 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
460
461 /*
462 * If bps are unlimited (-1), then time slice don't get
463 * renewed. Don't try to trim the slice if slice is used. A new
464 * slice will start when appropriate.
465 */
466 if (throtl_slice_used(td, tg, rw))
467 return;
468
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100469 /*
470 * A bio has been dispatched. Also adjust slice_end. It might happen
471 * that initially cgroup limit was very low resulting in high
472 * slice_end, but later limit was bumped up and bio was dispached
473 * sooner, then we need to reduce slice_end. A high bogus slice_end
474 * is bad because it does not allow new slice to start.
475 */
476
477 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
478
Vivek Goyale43473b2010-09-15 17:06:35 -0400479 time_elapsed = jiffies - tg->slice_start[rw];
480
481 nr_slices = time_elapsed / throtl_slice;
482
483 if (!nr_slices)
484 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200485 tmp = tg->bps[rw] * throtl_slice * nr_slices;
486 do_div(tmp, HZ);
487 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400488
Vivek Goyal8e89d132010-09-15 17:06:37 -0400489 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400490
Vivek Goyal8e89d132010-09-15 17:06:37 -0400491 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400492 return;
493
494 if (tg->bytes_disp[rw] >= bytes_trim)
495 tg->bytes_disp[rw] -= bytes_trim;
496 else
497 tg->bytes_disp[rw] = 0;
498
Vivek Goyal8e89d132010-09-15 17:06:37 -0400499 if (tg->io_disp[rw] >= io_trim)
500 tg->io_disp[rw] -= io_trim;
501 else
502 tg->io_disp[rw] = 0;
503
Vivek Goyale43473b2010-09-15 17:06:35 -0400504 tg->slice_start[rw] += nr_slices * throtl_slice;
505
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200506 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400507 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400508 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400509 tg->slice_start[rw], tg->slice_end[rw], jiffies);
510}
511
Vivek Goyal8e89d132010-09-15 17:06:37 -0400512static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
513 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400514{
515 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400516 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400517 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200518 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400519
Vivek Goyal8e89d132010-09-15 17:06:37 -0400520 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400521
Vivek Goyal8e89d132010-09-15 17:06:37 -0400522 /* Slice has just started. Consider one slice interval */
523 if (!jiffy_elapsed)
524 jiffy_elapsed_rnd = throtl_slice;
525
526 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
527
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200528 /*
529 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
530 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
531 * will allow dispatch after 1 second and after that slice should
532 * have been trimmed.
533 */
534
535 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
536 do_div(tmp, HZ);
537
538 if (tmp > UINT_MAX)
539 io_allowed = UINT_MAX;
540 else
541 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400542
543 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400544 if (wait)
545 *wait = 0;
546 return 1;
547 }
548
Vivek Goyal8e89d132010-09-15 17:06:37 -0400549 /* Calc approx time to dispatch */
550 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
551
552 if (jiffy_wait > jiffy_elapsed)
553 jiffy_wait = jiffy_wait - jiffy_elapsed;
554 else
555 jiffy_wait = 1;
556
557 if (wait)
558 *wait = jiffy_wait;
559 return 0;
560}
561
562static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
563 struct bio *bio, unsigned long *wait)
564{
565 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200566 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400567 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400568
569 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
570
571 /* Slice has just started. Consider one slice interval */
572 if (!jiffy_elapsed)
573 jiffy_elapsed_rnd = throtl_slice;
574
575 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
576
Vivek Goyal5e901a22010-10-01 21:16:38 +0200577 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
578 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200579 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400580
581 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
582 if (wait)
583 *wait = 0;
584 return 1;
585 }
586
587 /* Calc approx time to dispatch */
588 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
589 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
590
591 if (!jiffy_wait)
592 jiffy_wait = 1;
593
594 /*
595 * This wait time is without taking into consideration the rounding
596 * up we did. Add that time also.
597 */
598 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400599 if (wait)
600 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400601 return 0;
602}
Vivek Goyale43473b2010-09-15 17:06:35 -0400603
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400604static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
605 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
606 return 1;
607 return 0;
608}
609
Vivek Goyal8e89d132010-09-15 17:06:37 -0400610/*
611 * Returns whether one can dispatch a bio or not. Also returns approx number
612 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
613 */
614static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
615 struct bio *bio, unsigned long *wait)
616{
617 bool rw = bio_data_dir(bio);
618 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
619
620 /*
621 * Currently whole state machine of group depends on first bio
622 * queued in the group bio list. So one should not be calling
623 * this function with a different bio if there are other bios
624 * queued.
625 */
626 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
627
628 /* If tg->bps = -1, then BW is unlimited */
629 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
630 if (wait)
631 *wait = 0;
632 return 1;
633 }
634
635 /*
636 * If previous slice expired, start a new one otherwise renew/extend
637 * existing slice to make sure it is at least throtl_slice interval
638 * long since now.
639 */
640 if (throtl_slice_used(td, tg, rw))
641 throtl_start_new_slice(td, tg, rw);
642 else {
643 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
644 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
645 }
646
647 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
648 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
649 if (wait)
650 *wait = 0;
651 return 1;
652 }
653
654 max_wait = max(bps_wait, iops_wait);
655
656 if (wait)
657 *wait = max_wait;
658
659 if (time_before(tg->slice_end[rw], jiffies + max_wait))
660 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400661
662 return 0;
663}
664
Tejun Heo629ed0b2012-04-01 14:38:44 -0700665static void throtl_update_dispatch_stats(struct blkio_group *blkg, u64 bytes,
666 int rw)
667{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700668 struct throtl_grp *tg = blkg_to_tg(blkg);
669 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700670 unsigned long flags;
671
672 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700673 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700674 return;
675
676 /*
677 * Disabling interrupts to provide mutual exclusion between two
678 * writes on same cpu. It probably is not needed for 64bit. Not
679 * optimizing that case yet.
680 */
681 local_irq_save(flags);
682
Tejun Heo8a3d2612012-04-01 14:38:44 -0700683 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700684
Tejun Heo629ed0b2012-04-01 14:38:44 -0700685 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
686 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
687
688 local_irq_restore(flags);
689}
690
Vivek Goyale43473b2010-09-15 17:06:35 -0400691static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
692{
693 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400694
695 /* Charge the bio to the group */
696 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400697 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400698
Tejun Heo629ed0b2012-04-01 14:38:44 -0700699 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400700}
701
702static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
703 struct bio *bio)
704{
705 bool rw = bio_data_dir(bio);
706
707 bio_list_add(&tg->bio_lists[rw], bio);
708 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800709 blkg_get(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400710 tg->nr_queued[rw]++;
711 td->nr_queued[rw]++;
712 throtl_enqueue_tg(td, tg);
713}
714
715static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
716{
717 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
718 struct bio *bio;
719
720 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
721 tg_may_dispatch(td, tg, bio, &read_wait);
722
723 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
724 tg_may_dispatch(td, tg, bio, &write_wait);
725
726 min_wait = min(read_wait, write_wait);
727 disptime = jiffies + min_wait;
728
Vivek Goyale43473b2010-09-15 17:06:35 -0400729 /* Update dispatch time */
730 throtl_dequeue_tg(td, tg);
731 tg->disptime = disptime;
732 throtl_enqueue_tg(td, tg);
733}
734
735static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
736 bool rw, struct bio_list *bl)
737{
738 struct bio *bio;
739
740 bio = bio_list_pop(&tg->bio_lists[rw]);
741 tg->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800742 /* Drop bio reference on blkg */
743 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400744
745 BUG_ON(td->nr_queued[rw] <= 0);
746 td->nr_queued[rw]--;
747
748 throtl_charge_bio(tg, bio);
749 bio_list_add(bl, bio);
750 bio->bi_rw |= REQ_THROTTLED;
751
752 throtl_trim_slice(td, tg, rw);
753}
754
755static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
756 struct bio_list *bl)
757{
758 unsigned int nr_reads = 0, nr_writes = 0;
759 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100760 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400761 struct bio *bio;
762
763 /* Try to dispatch 75% READS and 25% WRITES */
764
765 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
766 && tg_may_dispatch(td, tg, bio, NULL)) {
767
768 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
769 nr_reads++;
770
771 if (nr_reads >= max_nr_reads)
772 break;
773 }
774
775 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
776 && tg_may_dispatch(td, tg, bio, NULL)) {
777
778 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
779 nr_writes++;
780
781 if (nr_writes >= max_nr_writes)
782 break;
783 }
784
785 return nr_reads + nr_writes;
786}
787
788static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
789{
790 unsigned int nr_disp = 0;
791 struct throtl_grp *tg;
792 struct throtl_rb_root *st = &td->tg_service_tree;
793
794 while (1) {
795 tg = throtl_rb_first(st);
796
797 if (!tg)
798 break;
799
800 if (time_before(jiffies, tg->disptime))
801 break;
802
803 throtl_dequeue_tg(td, tg);
804
805 nr_disp += throtl_dispatch_tg(td, tg, bl);
806
807 if (tg->nr_queued[0] || tg->nr_queued[1]) {
808 tg_update_disptime(td, tg);
809 throtl_enqueue_tg(td, tg);
810 }
811
812 if (nr_disp >= throtl_quantum)
813 break;
814 }
815
816 return nr_disp;
817}
818
Vivek Goyalfe071432010-10-01 14:49:49 +0200819static void throtl_process_limit_change(struct throtl_data *td)
820{
Tejun Heo4eef3042012-03-05 13:15:18 -0800821 struct request_queue *q = td->queue;
822 struct blkio_group *blkg, *n;
Vivek Goyalfe071432010-10-01 14:49:49 +0200823
Vivek Goyalde701c72011-03-07 21:09:32 +0100824 if (!td->limits_changed)
Vivek Goyalfe071432010-10-01 14:49:49 +0200825 return;
826
Vivek Goyalde701c72011-03-07 21:09:32 +0100827 xchg(&td->limits_changed, false);
Vivek Goyalfe071432010-10-01 14:49:49 +0200828
Vivek Goyalde701c72011-03-07 21:09:32 +0100829 throtl_log(td, "limits changed");
Vivek Goyalfe071432010-10-01 14:49:49 +0200830
Tejun Heoe8989fa2012-03-05 13:15:20 -0800831 list_for_each_entry_safe(blkg, n, &q->blkg_list, q_node) {
Tejun Heo4eef3042012-03-05 13:15:18 -0800832 struct throtl_grp *tg = blkg_to_tg(blkg);
833
Vivek Goyalde701c72011-03-07 21:09:32 +0100834 if (!tg->limits_changed)
835 continue;
Vivek Goyalfe071432010-10-01 14:49:49 +0200836
Vivek Goyalde701c72011-03-07 21:09:32 +0100837 if (!xchg(&tg->limits_changed, false))
838 continue;
839
840 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu"
841 " riops=%u wiops=%u", tg->bps[READ], tg->bps[WRITE],
842 tg->iops[READ], tg->iops[WRITE]);
843
Vivek Goyal04521db2011-03-22 21:54:29 +0100844 /*
845 * Restart the slices for both READ and WRITES. It
846 * might happen that a group's limit are dropped
847 * suddenly and we don't want to account recently
848 * dispatched IO with new low rate
849 */
850 throtl_start_new_slice(td, tg, 0);
851 throtl_start_new_slice(td, tg, 1);
852
Vivek Goyalde701c72011-03-07 21:09:32 +0100853 if (throtl_tg_on_rr(tg))
854 tg_update_disptime(td, tg);
855 }
Vivek Goyalfe071432010-10-01 14:49:49 +0200856}
857
Vivek Goyale43473b2010-09-15 17:06:35 -0400858/* Dispatch throttled bios. Should be called without queue lock held. */
859static int throtl_dispatch(struct request_queue *q)
860{
861 struct throtl_data *td = q->td;
862 unsigned int nr_disp = 0;
863 struct bio_list bio_list_on_stack;
864 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100865 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400866
867 spin_lock_irq(q->queue_lock);
868
Vivek Goyalfe071432010-10-01 14:49:49 +0200869 throtl_process_limit_change(td);
870
Vivek Goyale43473b2010-09-15 17:06:35 -0400871 if (!total_nr_queued(td))
872 goto out;
873
874 bio_list_init(&bio_list_on_stack);
875
Joe Perchesd2f31a52011-06-13 20:19:27 +0200876 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Vivek Goyale43473b2010-09-15 17:06:35 -0400877 total_nr_queued(td), td->nr_queued[READ],
878 td->nr_queued[WRITE]);
879
880 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
881
882 if (nr_disp)
883 throtl_log(td, "bios disp=%u", nr_disp);
884
885 throtl_schedule_next_dispatch(td);
886out:
887 spin_unlock_irq(q->queue_lock);
888
889 /*
890 * If we dispatched some requests, unplug the queue to make sure
891 * immediate dispatch
892 */
893 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100894 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400895 while((bio = bio_list_pop(&bio_list_on_stack)))
896 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100897 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400898 }
899 return nr_disp;
900}
901
902void blk_throtl_work(struct work_struct *work)
903{
904 struct throtl_data *td = container_of(work, struct throtl_data,
905 throtl_work.work);
906 struct request_queue *q = td->queue;
907
908 throtl_dispatch(q);
909}
910
911/* Call with queue lock held */
Vivek Goyal450adcb2011-03-01 13:40:54 -0500912static void
913throtl_schedule_delayed_work(struct throtl_data *td, unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -0400914{
915
Vivek Goyale43473b2010-09-15 17:06:35 -0400916 struct delayed_work *dwork = &td->throtl_work;
917
Vivek Goyal04521db2011-03-22 21:54:29 +0100918 /* schedule work if limits changed even if no bio is queued */
Joe Perchesd2f31a52011-06-13 20:19:27 +0200919 if (total_nr_queued(td) || td->limits_changed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400920 /*
921 * We might have a work scheduled to be executed in future.
922 * Cancel that and schedule a new one.
923 */
924 __cancel_delayed_work(dwork);
Vivek Goyal450adcb2011-03-01 13:40:54 -0500925 queue_delayed_work(kthrotld_workqueue, dwork, delay);
Vivek Goyale43473b2010-09-15 17:06:35 -0400926 throtl_log(td, "schedule work. delay=%lu jiffies=%lu",
927 delay, jiffies);
928 }
929}
Vivek Goyale43473b2010-09-15 17:06:35 -0400930
Tejun Heod366e7e2012-04-01 14:38:44 -0700931static u64 tg_prfill_cpu_rwstat(struct seq_file *sf, void *pdata, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700932{
Tejun Heod366e7e2012-04-01 14:38:44 -0700933 struct throtl_grp *tg = pdata;
Tejun Heo41b38b62012-04-01 14:38:44 -0700934 struct blkg_rwstat rwstat = { }, tmp;
935 int i, cpu;
936
937 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700938 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700939
940 tmp = blkg_rwstat_read((void *)sc + off);
941 for (i = 0; i < BLKG_RWSTAT_NR; i++)
942 rwstat.cnt[i] += tmp.cnt[i];
943 }
944
Tejun Heod366e7e2012-04-01 14:38:44 -0700945 return __blkg_prfill_rwstat(sf, pdata, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700946}
947
Tejun Heo8a3d2612012-04-01 14:38:44 -0700948static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
949 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700950{
951 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
952
Tejun Heoec399342012-04-13 13:11:27 -0700953 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkio_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700954 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700955 return 0;
956}
957
Tejun Heod366e7e2012-04-01 14:38:44 -0700958static u64 tg_prfill_conf_u64(struct seq_file *sf, void *pdata, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700959{
Tejun Heod366e7e2012-04-01 14:38:44 -0700960 u64 v = *(u64 *)(pdata + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700961
Tejun Heoaf133ce2012-04-01 14:38:44 -0700962 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700963 return 0;
Tejun Heod366e7e2012-04-01 14:38:44 -0700964 return __blkg_prfill_u64(sf, pdata, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700965}
966
Tejun Heod366e7e2012-04-01 14:38:44 -0700967static u64 tg_prfill_conf_uint(struct seq_file *sf, void *pdata, int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700968{
Tejun Heod366e7e2012-04-01 14:38:44 -0700969 unsigned int v = *(unsigned int *)(pdata + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700970
971 if (v == -1)
972 return 0;
Tejun Heod366e7e2012-04-01 14:38:44 -0700973 return __blkg_prfill_u64(sf, pdata, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700974}
975
976static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
977 struct seq_file *sf)
978{
979 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_u64,
Tejun Heoec399342012-04-13 13:11:27 -0700980 &blkio_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700981 return 0;
982}
983
Tejun Heoaf133ce2012-04-01 14:38:44 -0700984static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
985 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400986{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700987 blkcg_print_blkgs(sf, cgroup_to_blkio_cgroup(cgrp), tg_prfill_conf_uint,
Tejun Heoec399342012-04-13 13:11:27 -0700988 &blkio_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700989 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400990}
991
Tejun Heoaf133ce2012-04-01 14:38:44 -0700992static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
993 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700994{
995 struct blkio_cgroup *blkcg = cgroup_to_blkio_cgroup(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700996 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700997 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700998 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700999 int ret;
1000
Tejun Heoda8b0662012-04-13 13:11:29 -07001001 ret = blkg_conf_prep(blkcg, &blkio_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001002 if (ret)
1003 return ret;
1004
Tejun Heoaf133ce2012-04-01 14:38:44 -07001005 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -07001006 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001007
Tejun Heoa2b16932012-04-13 13:11:33 -07001008 if (!ctx.v)
1009 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001010
Tejun Heoa2b16932012-04-13 13:11:33 -07001011 if (is_u64)
1012 *(u64 *)((void *)tg + cft->private) = ctx.v;
1013 else
1014 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001015
Tejun Heoa2b16932012-04-13 13:11:33 -07001016 /* XXX: we don't need the following deferred processing */
1017 xchg(&tg->limits_changed, true);
1018 xchg(&td->limits_changed, true);
1019 throtl_schedule_delayed_work(td, 0);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001020
1021 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -07001022 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001023}
1024
Tejun Heoaf133ce2012-04-01 14:38:44 -07001025static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1026 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001027{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001028 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001029}
1030
Tejun Heoaf133ce2012-04-01 14:38:44 -07001031static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1032 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001033{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001034 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001035}
1036
1037static struct cftype throtl_files[] = {
1038 {
1039 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001040 .private = offsetof(struct throtl_grp, bps[READ]),
1041 .read_seq_string = tg_print_conf_u64,
1042 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001043 .max_write_len = 256,
1044 },
1045 {
1046 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001047 .private = offsetof(struct throtl_grp, bps[WRITE]),
1048 .read_seq_string = tg_print_conf_u64,
1049 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001050 .max_write_len = 256,
1051 },
1052 {
1053 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001054 .private = offsetof(struct throtl_grp, iops[READ]),
1055 .read_seq_string = tg_print_conf_uint,
1056 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001057 .max_write_len = 256,
1058 },
1059 {
1060 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001061 .private = offsetof(struct throtl_grp, iops[WRITE]),
1062 .read_seq_string = tg_print_conf_uint,
1063 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001064 .max_write_len = 256,
1065 },
1066 {
1067 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001068 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001069 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001070 },
1071 {
1072 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001073 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001074 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001075 },
1076 { } /* terminate */
1077};
1078
Vivek Goyalda527772011-03-02 19:05:33 -05001079static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001080{
1081 struct throtl_data *td = q->td;
1082
1083 cancel_delayed_work_sync(&td->throtl_work);
1084}
1085
1086static struct blkio_policy_type blkio_policy_throtl = {
1087 .ops = {
Tejun Heo03814112012-03-05 13:15:14 -08001088 .blkio_init_group_fn = throtl_init_blkio_group,
Tejun Heo8a3d2612012-04-01 14:38:44 -07001089 .blkio_exit_group_fn = throtl_exit_blkio_group,
1090 .blkio_reset_group_stats_fn = throtl_reset_group_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001091 },
Tejun Heo03814112012-03-05 13:15:14 -08001092 .pdata_size = sizeof(struct throtl_grp),
Tejun Heo60c2bc22012-04-01 14:38:43 -07001093 .cftypes = throtl_files,
Vivek Goyale43473b2010-09-15 17:06:35 -04001094};
1095
Tejun Heobc16a4f2011-10-19 14:33:01 +02001096bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001097{
1098 struct throtl_data *td = q->td;
1099 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001100 bool rw = bio_data_dir(bio), update_disptime = true;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001101 struct blkio_cgroup *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001102 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001103
1104 if (bio->bi_rw & REQ_THROTTLED) {
1105 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001106 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001107 }
1108
Tejun Heo671058f2012-03-05 13:15:29 -08001109 /* bio_associate_current() needs ioc, try creating */
1110 create_io_context(GFP_ATOMIC, q->node);
1111
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001112 /*
1113 * A throtl_grp pointer retrieved under rcu can be used to access
1114 * basic fields like stats and io rates. If a group has no rules,
1115 * just update the dispatch stats in lockless manner and return.
1116 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001117 rcu_read_lock();
Tejun Heo4f85cb92012-03-05 13:15:28 -08001118 blkcg = bio_blkio_cgroup(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001119 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001120 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001121 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001122 throtl_update_dispatch_stats(tg_to_blkg(tg),
1123 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001124 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001125 }
1126 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001127
1128 /*
1129 * Either group has not been allocated yet or it is not an unlimited
1130 * IO group
1131 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001132 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001133 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001134 if (unlikely(!tg))
1135 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001136
Vivek Goyale43473b2010-09-15 17:06:35 -04001137 if (tg->nr_queued[rw]) {
1138 /*
1139 * There is already another bio queued in same dir. No
1140 * need to update dispatch time.
1141 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001142 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001143 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001144
Vivek Goyale43473b2010-09-15 17:06:35 -04001145 }
1146
1147 /* Bio is with-in rate limit of group */
1148 if (tg_may_dispatch(td, tg, bio, NULL)) {
1149 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001150
1151 /*
1152 * We need to trim slice even when bios are not being queued
1153 * otherwise it might happen that a bio is not queued for
1154 * a long time and slice keeps on extending and trim is not
1155 * called for a long time. Now if limits are reduced suddenly
1156 * we take into account all the IO dispatched so far at new
1157 * low rate and * newly queued IO gets a really long dispatch
1158 * time.
1159 *
1160 * So keep on trimming slice even if bio is not queued.
1161 */
1162 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001163 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001164 }
1165
1166queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001167 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001168 " iodisp=%u iops=%u queued=%d/%d",
1169 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001170 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001171 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001172 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1173
Tejun Heo671058f2012-03-05 13:15:29 -08001174 bio_associate_current(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001175 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001176 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001177
1178 if (update_disptime) {
1179 tg_update_disptime(td, tg);
1180 throtl_schedule_next_dispatch(td);
1181 }
1182
Tejun Heobc16a4f2011-10-19 14:33:01 +02001183out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001184 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001185out_unlock_rcu:
1186 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001187out:
1188 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001189}
1190
Tejun Heoc9a929d2011-10-19 14:42:16 +02001191/**
1192 * blk_throtl_drain - drain throttled bios
1193 * @q: request_queue to drain throttled bios for
1194 *
1195 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1196 */
1197void blk_throtl_drain(struct request_queue *q)
1198 __releases(q->queue_lock) __acquires(q->queue_lock)
1199{
1200 struct throtl_data *td = q->td;
1201 struct throtl_rb_root *st = &td->tg_service_tree;
1202 struct throtl_grp *tg;
1203 struct bio_list bl;
1204 struct bio *bio;
1205
Jens Axboe334c2b02011-10-25 15:51:48 +02001206 WARN_ON_ONCE(!queue_is_locked(q));
Tejun Heoc9a929d2011-10-19 14:42:16 +02001207
1208 bio_list_init(&bl);
1209
1210 while ((tg = throtl_rb_first(st))) {
1211 throtl_dequeue_tg(td, tg);
1212
1213 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1214 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1215 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1216 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1217 }
1218 spin_unlock_irq(q->queue_lock);
1219
1220 while ((bio = bio_list_pop(&bl)))
1221 generic_make_request(bio);
1222
1223 spin_lock_irq(q->queue_lock);
1224}
1225
Vivek Goyale43473b2010-09-15 17:06:35 -04001226int blk_throtl_init(struct request_queue *q)
1227{
1228 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001229 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001230
1231 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1232 if (!td)
1233 return -ENOMEM;
1234
Vivek Goyale43473b2010-09-15 17:06:35 -04001235 td->tg_service_tree = THROTL_RB_ROOT;
Vivek Goyalde701c72011-03-07 21:09:32 +01001236 td->limits_changed = false;
Vivek Goyala29a1712011-05-19 15:38:19 -04001237 INIT_DELAYED_WORK(&td->throtl_work, blk_throtl_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001238
Tejun Heocd1604f2012-03-05 13:15:06 -08001239 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001240 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001241
Tejun Heoa2b16932012-04-13 13:11:33 -07001242 /* activate policy */
1243 ret = blkcg_activate_policy(q, &blkio_policy_throtl);
1244 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001245 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001246 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001247}
1248
1249void blk_throtl_exit(struct request_queue *q)
1250{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001251 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001252 throtl_shutdown_wq(q);
Tejun Heoa2b16932012-04-13 13:11:33 -07001253 blkcg_deactivate_policy(q, &blkio_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001254 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001255}
1256
1257static int __init throtl_init(void)
1258{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001259 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1260 if (!kthrotld_workqueue)
1261 panic("Failed to create kthrotld\n");
1262
Tejun Heo8bd435b2012-04-13 13:11:28 -07001263 return blkio_policy_register(&blkio_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001264}
1265
1266module_init(throtl_init);