blob: e8ef43d3fab3608a9244598bcff3bc9bba0811a5 [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 Heo3c798392012-04-16 13:57:25 -070024static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080025
Vivek Goyal450adcb2011-03-01 13:40:54 -050026/* A workqueue to queue throttle related work */
27static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050028
Tejun Heoc9e03322013-05-14 13:52:32 -070029struct throtl_service_queue {
30 struct rb_root pending_tree; /* RB tree of active tgs */
31 struct rb_node *first_pending; /* first node in the tree */
32 unsigned int nr_pending; /* # queued in the tree */
33 unsigned long first_pending_disptime; /* disptime of the first tg */
Vivek Goyale43473b2010-09-15 17:06:35 -040034};
35
Tejun Heoc9e03322013-05-14 13:52:32 -070036#define THROTL_SERVICE_QUEUE_INITIALIZER \
37 (struct throtl_service_queue){ .pending_tree = RB_ROOT }
Vivek Goyale43473b2010-09-15 17:06:35 -040038
Tejun Heo5b2c16a2013-05-14 13:52:32 -070039enum tg_state_flags {
40 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
41};
42
Vivek Goyale43473b2010-09-15 17:06:35 -040043#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
44
Tejun Heo8a3d2612012-04-01 14:38:44 -070045/* Per-cpu group stats */
46struct tg_stats_cpu {
47 /* total bytes transferred */
48 struct blkg_rwstat service_bytes;
49 /* total IOs serviced, post merge */
50 struct blkg_rwstat serviced;
51};
52
Vivek Goyale43473b2010-09-15 17:06:35 -040053struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070054 /* must be the first member */
55 struct blkg_policy_data pd;
56
Tejun Heoc9e03322013-05-14 13:52:32 -070057 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040058 struct rb_node rb_node;
59
60 /*
61 * Dispatch time in jiffies. This is the estimated time when group
62 * will unthrottle and is ready to dispatch more bio. It is used as
63 * key to sort active groups in service tree.
64 */
65 unsigned long disptime;
66
Vivek Goyale43473b2010-09-15 17:06:35 -040067 unsigned int flags;
68
69 /* Two lists for READ and WRITE */
70 struct bio_list bio_lists[2];
71
72 /* Number of queued bios on READ and WRITE lists */
73 unsigned int nr_queued[2];
74
75 /* bytes per second rate limits */
76 uint64_t bps[2];
77
Vivek Goyal8e89d132010-09-15 17:06:37 -040078 /* IOPS limits */
79 unsigned int iops[2];
80
Vivek Goyale43473b2010-09-15 17:06:35 -040081 /* Number of bytes disptached in current slice */
82 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040083 /* Number of bio's dispatched in current slice */
84 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040085
86 /* When did we start a new slice */
87 unsigned long slice_start[2];
88 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020089
Tejun Heo8a3d2612012-04-01 14:38:44 -070090 /* Per cpu stats pointer */
91 struct tg_stats_cpu __percpu *stats_cpu;
92
93 /* List of tgs waiting for per cpu stats memory to be allocated */
94 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -040095};
96
97struct throtl_data
98{
Vivek Goyale43473b2010-09-15 17:06:35 -040099 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700100 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400101
Vivek Goyale43473b2010-09-15 17:06:35 -0400102 struct request_queue *queue;
103
104 /* Total Number of queued bios on READ and WRITE lists */
105 unsigned int nr_queued[2];
106
107 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200108 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400109 */
110 unsigned int nr_undestroyed_grps;
111
112 /* Work for dispatching throttled bios */
Tejun Heocb761992013-05-14 13:52:31 -0700113 struct delayed_work dispatch_work;
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 Heof95a04a2012-04-16 13:57:26 -0700123static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
124{
125 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
126}
127
Tejun Heo3c798392012-04-16 13:57:25 -0700128static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800129{
Tejun Heof95a04a2012-04-16 13:57:26 -0700130 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800131}
132
Tejun Heo3c798392012-04-16 13:57:25 -0700133static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800134{
Tejun Heof95a04a2012-04-16 13:57:26 -0700135 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800136}
137
Tejun Heo03d8e112012-04-13 13:11:32 -0700138static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
139{
140 return blkg_to_tg(td->queue->root_blkg);
141}
142
Tejun Heo54e7ed12012-04-16 13:57:23 -0700143#define throtl_log_tg(td, tg, fmt, args...) do { \
144 char __pbuf[128]; \
145 \
146 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
147 blk_add_trace_msg((td)->queue, "throtl %s " fmt, __pbuf, ##args); \
148} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400149
150#define throtl_log(td, fmt, args...) \
151 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
152
Tejun Heo8a3d2612012-04-01 14:38:44 -0700153/*
154 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700155 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700156 * allocation.
157 */
158static void tg_stats_alloc_fn(struct work_struct *work)
159{
160 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
161 struct delayed_work *dwork = to_delayed_work(work);
162 bool empty = false;
163
164alloc_stats:
165 if (!stats_cpu) {
166 stats_cpu = alloc_percpu(struct tg_stats_cpu);
167 if (!stats_cpu) {
168 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700169 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700170 return;
171 }
172 }
173
174 spin_lock_irq(&tg_stats_alloc_lock);
175
176 if (!list_empty(&tg_stats_alloc_list)) {
177 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
178 struct throtl_grp,
179 stats_alloc_node);
180 swap(tg->stats_cpu, stats_cpu);
181 list_del_init(&tg->stats_alloc_node);
182 }
183
184 empty = list_empty(&tg_stats_alloc_list);
185 spin_unlock_irq(&tg_stats_alloc_lock);
186 if (!empty)
187 goto alloc_stats;
188}
189
Tejun Heo3c798392012-04-16 13:57:25 -0700190static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400191{
Tejun Heo03814112012-03-05 13:15:14 -0800192 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200193 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800194
Vivek Goyala29a1712011-05-19 15:38:19 -0400195 RB_CLEAR_NODE(&tg->rb_node);
196 bio_list_init(&tg->bio_lists[0]);
197 bio_list_init(&tg->bio_lists[1]);
Vivek Goyala29a1712011-05-19 15:38:19 -0400198
Tejun Heoe56da7e2012-03-05 13:15:07 -0800199 tg->bps[READ] = -1;
200 tg->bps[WRITE] = -1;
201 tg->iops[READ] = -1;
202 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700203
204 /*
205 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
206 * but percpu allocator can't be called from IO path. Queue tg on
207 * tg_stats_alloc_list and allocate from work item.
208 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200209 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700210 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700211 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200212 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700213}
214
Tejun Heo3c798392012-04-16 13:57:25 -0700215static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700216{
217 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200218 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700219
Tejun Heoff26eaa2012-05-23 12:16:21 +0200220 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700221 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200222 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700223
224 free_percpu(tg->stats_cpu);
225}
226
Tejun Heo3c798392012-04-16 13:57:25 -0700227static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700228{
229 struct throtl_grp *tg = blkg_to_tg(blkg);
230 int cpu;
231
232 if (tg->stats_cpu == NULL)
233 return;
234
235 for_each_possible_cpu(cpu) {
236 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
237
238 blkg_rwstat_reset(&sc->service_bytes);
239 blkg_rwstat_reset(&sc->serviced);
240 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400241}
242
Tejun Heo3c798392012-04-16 13:57:25 -0700243static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
244 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400245{
Vivek Goyale43473b2010-09-15 17:06:35 -0400246 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700247 * This is the common case when there are no blkcgs. Avoid lookup
248 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800249 */
Tejun Heo3c798392012-04-16 13:57:25 -0700250 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700251 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400252
Tejun Heoe8989fa2012-03-05 13:15:20 -0800253 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400254}
255
Tejun Heocd1604f2012-03-05 13:15:06 -0800256static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700257 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400258{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400259 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800260 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800261
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400262 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700263 * This is the common case when there are no blkcgs. Avoid lookup
264 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400265 */
Tejun Heo3c798392012-04-16 13:57:25 -0700266 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700267 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800268 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700269 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800270
Tejun Heo3c96cb32012-04-13 13:11:34 -0700271 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800272
273 /* if %NULL and @q is alive, fall back to root_tg */
274 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800275 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100276 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700277 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400278 }
279
Vivek Goyale43473b2010-09-15 17:06:35 -0400280 return tg;
281}
282
Tejun Heoc9e03322013-05-14 13:52:32 -0700283static struct throtl_grp *throtl_rb_first(struct throtl_service_queue *sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400284{
285 /* Service tree is empty */
Tejun Heoc9e03322013-05-14 13:52:32 -0700286 if (!sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400287 return NULL;
288
Tejun Heoc9e03322013-05-14 13:52:32 -0700289 if (!sq->first_pending)
290 sq->first_pending = rb_first(&sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400291
Tejun Heoc9e03322013-05-14 13:52:32 -0700292 if (sq->first_pending)
293 return rb_entry_tg(sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400294
295 return NULL;
296}
297
298static void rb_erase_init(struct rb_node *n, struct rb_root *root)
299{
300 rb_erase(n, root);
301 RB_CLEAR_NODE(n);
302}
303
Tejun Heoc9e03322013-05-14 13:52:32 -0700304static void throtl_rb_erase(struct rb_node *n, struct throtl_service_queue *sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400305{
Tejun Heoc9e03322013-05-14 13:52:32 -0700306 if (sq->first_pending == n)
307 sq->first_pending = NULL;
308 rb_erase_init(n, &sq->pending_tree);
309 --sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400310}
311
Tejun Heoc9e03322013-05-14 13:52:32 -0700312static void update_min_dispatch_time(struct throtl_service_queue *sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400313{
314 struct throtl_grp *tg;
315
Tejun Heoc9e03322013-05-14 13:52:32 -0700316 tg = throtl_rb_first(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400317 if (!tg)
318 return;
319
Tejun Heoc9e03322013-05-14 13:52:32 -0700320 sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400321}
322
Tejun Heoc9e03322013-05-14 13:52:32 -0700323static void tg_service_queue_add(struct throtl_service_queue *sq,
324 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400325{
Tejun Heoc9e03322013-05-14 13:52:32 -0700326 struct rb_node **node = &sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400327 struct rb_node *parent = NULL;
328 struct throtl_grp *__tg;
329 unsigned long key = tg->disptime;
330 int left = 1;
331
332 while (*node != NULL) {
333 parent = *node;
334 __tg = rb_entry_tg(parent);
335
336 if (time_before(key, __tg->disptime))
337 node = &parent->rb_left;
338 else {
339 node = &parent->rb_right;
340 left = 0;
341 }
342 }
343
344 if (left)
Tejun Heoc9e03322013-05-14 13:52:32 -0700345 sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400346
347 rb_link_node(&tg->rb_node, parent, node);
Tejun Heoc9e03322013-05-14 13:52:32 -0700348 rb_insert_color(&tg->rb_node, &sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400349}
350
351static void __throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
352{
Tejun Heoc9e03322013-05-14 13:52:32 -0700353 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400354
Tejun Heoc9e03322013-05-14 13:52:32 -0700355 tg_service_queue_add(sq, tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700356 tg->flags |= THROTL_TG_PENDING;
Tejun Heoc9e03322013-05-14 13:52:32 -0700357 sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400358}
359
360static void throtl_enqueue_tg(struct throtl_data *td, struct throtl_grp *tg)
361{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700362 if (!(tg->flags & THROTL_TG_PENDING))
Vivek Goyale43473b2010-09-15 17:06:35 -0400363 __throtl_enqueue_tg(td, tg);
364}
365
366static void __throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
367{
Tejun Heoc9e03322013-05-14 13:52:32 -0700368 throtl_rb_erase(&tg->rb_node, &td->service_queue);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700369 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400370}
371
372static void throtl_dequeue_tg(struct throtl_data *td, struct throtl_grp *tg)
373{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700374 if (tg->flags & THROTL_TG_PENDING)
Vivek Goyale43473b2010-09-15 17:06:35 -0400375 __throtl_dequeue_tg(td, tg);
376}
377
Tejun Heoa9131a22013-05-14 13:52:31 -0700378/* Call with queue lock held */
379static void throtl_schedule_delayed_work(struct throtl_data *td,
380 unsigned long delay)
381{
382 struct delayed_work *dwork = &td->dispatch_work;
383
Tejun Heo6a525602013-05-14 13:52:32 -0700384 mod_delayed_work(kthrotld_workqueue, dwork, delay);
385 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700386}
387
Vivek Goyale43473b2010-09-15 17:06:35 -0400388static void throtl_schedule_next_dispatch(struct throtl_data *td)
389{
Tejun Heoc9e03322013-05-14 13:52:32 -0700390 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400391
Tejun Heo6a525602013-05-14 13:52:32 -0700392 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700393 if (!sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400394 return;
395
Tejun Heoc9e03322013-05-14 13:52:32 -0700396 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400397
Tejun Heoc9e03322013-05-14 13:52:32 -0700398 if (time_before_eq(sq->first_pending_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500399 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400400 else
Tejun Heoc9e03322013-05-14 13:52:32 -0700401 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400402}
403
404static inline void
405throtl_start_new_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
406{
407 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400408 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400409 tg->slice_start[rw] = jiffies;
410 tg->slice_end[rw] = jiffies + throtl_slice;
411 throtl_log_tg(td, tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
412 rw == READ ? 'R' : 'W', tg->slice_start[rw],
413 tg->slice_end[rw], jiffies);
414}
415
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100416static inline void throtl_set_slice_end(struct throtl_data *td,
417 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
418{
419 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
420}
421
Vivek Goyale43473b2010-09-15 17:06:35 -0400422static inline void throtl_extend_slice(struct throtl_data *td,
423 struct throtl_grp *tg, bool rw, unsigned long jiffy_end)
424{
425 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
426 throtl_log_tg(td, tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
427 rw == READ ? 'R' : 'W', tg->slice_start[rw],
428 tg->slice_end[rw], jiffies);
429}
430
431/* Determine if previously allocated or extended slice is complete or not */
432static bool
433throtl_slice_used(struct throtl_data *td, struct throtl_grp *tg, bool rw)
434{
435 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
436 return 0;
437
438 return 1;
439}
440
441/* Trim the used slices and adjust slice start accordingly */
442static inline void
443throtl_trim_slice(struct throtl_data *td, struct throtl_grp *tg, bool rw)
444{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200445 unsigned long nr_slices, time_elapsed, io_trim;
446 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400447
448 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
449
450 /*
451 * If bps are unlimited (-1), then time slice don't get
452 * renewed. Don't try to trim the slice if slice is used. A new
453 * slice will start when appropriate.
454 */
455 if (throtl_slice_used(td, tg, rw))
456 return;
457
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100458 /*
459 * A bio has been dispatched. Also adjust slice_end. It might happen
460 * that initially cgroup limit was very low resulting in high
461 * slice_end, but later limit was bumped up and bio was dispached
462 * sooner, then we need to reduce slice_end. A high bogus slice_end
463 * is bad because it does not allow new slice to start.
464 */
465
466 throtl_set_slice_end(td, tg, rw, jiffies + throtl_slice);
467
Vivek Goyale43473b2010-09-15 17:06:35 -0400468 time_elapsed = jiffies - tg->slice_start[rw];
469
470 nr_slices = time_elapsed / throtl_slice;
471
472 if (!nr_slices)
473 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200474 tmp = tg->bps[rw] * throtl_slice * nr_slices;
475 do_div(tmp, HZ);
476 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400477
Vivek Goyal8e89d132010-09-15 17:06:37 -0400478 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400479
Vivek Goyal8e89d132010-09-15 17:06:37 -0400480 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400481 return;
482
483 if (tg->bytes_disp[rw] >= bytes_trim)
484 tg->bytes_disp[rw] -= bytes_trim;
485 else
486 tg->bytes_disp[rw] = 0;
487
Vivek Goyal8e89d132010-09-15 17:06:37 -0400488 if (tg->io_disp[rw] >= io_trim)
489 tg->io_disp[rw] -= io_trim;
490 else
491 tg->io_disp[rw] = 0;
492
Vivek Goyale43473b2010-09-15 17:06:35 -0400493 tg->slice_start[rw] += nr_slices * throtl_slice;
494
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200495 throtl_log_tg(td, tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400496 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400497 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400498 tg->slice_start[rw], tg->slice_end[rw], jiffies);
499}
500
Vivek Goyal8e89d132010-09-15 17:06:37 -0400501static bool tg_with_in_iops_limit(struct throtl_data *td, struct throtl_grp *tg,
502 struct bio *bio, unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400503{
504 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400505 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400506 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200507 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400508
Vivek Goyal8e89d132010-09-15 17:06:37 -0400509 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400510
Vivek Goyal8e89d132010-09-15 17:06:37 -0400511 /* Slice has just started. Consider one slice interval */
512 if (!jiffy_elapsed)
513 jiffy_elapsed_rnd = throtl_slice;
514
515 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
516
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200517 /*
518 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
519 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
520 * will allow dispatch after 1 second and after that slice should
521 * have been trimmed.
522 */
523
524 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
525 do_div(tmp, HZ);
526
527 if (tmp > UINT_MAX)
528 io_allowed = UINT_MAX;
529 else
530 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400531
532 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400533 if (wait)
534 *wait = 0;
535 return 1;
536 }
537
Vivek Goyal8e89d132010-09-15 17:06:37 -0400538 /* Calc approx time to dispatch */
539 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
540
541 if (jiffy_wait > jiffy_elapsed)
542 jiffy_wait = jiffy_wait - jiffy_elapsed;
543 else
544 jiffy_wait = 1;
545
546 if (wait)
547 *wait = jiffy_wait;
548 return 0;
549}
550
551static bool tg_with_in_bps_limit(struct throtl_data *td, struct throtl_grp *tg,
552 struct bio *bio, unsigned long *wait)
553{
554 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200555 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400556 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400557
558 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
559
560 /* Slice has just started. Consider one slice interval */
561 if (!jiffy_elapsed)
562 jiffy_elapsed_rnd = throtl_slice;
563
564 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
565
Vivek Goyal5e901a22010-10-01 21:16:38 +0200566 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
567 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200568 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400569
570 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
571 if (wait)
572 *wait = 0;
573 return 1;
574 }
575
576 /* Calc approx time to dispatch */
577 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
578 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
579
580 if (!jiffy_wait)
581 jiffy_wait = 1;
582
583 /*
584 * This wait time is without taking into consideration the rounding
585 * up we did. Add that time also.
586 */
587 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400588 if (wait)
589 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400590 return 0;
591}
Vivek Goyale43473b2010-09-15 17:06:35 -0400592
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400593static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
594 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
595 return 1;
596 return 0;
597}
598
Vivek Goyal8e89d132010-09-15 17:06:37 -0400599/*
600 * Returns whether one can dispatch a bio or not. Also returns approx number
601 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
602 */
603static bool tg_may_dispatch(struct throtl_data *td, struct throtl_grp *tg,
604 struct bio *bio, unsigned long *wait)
605{
606 bool rw = bio_data_dir(bio);
607 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
608
609 /*
610 * Currently whole state machine of group depends on first bio
611 * queued in the group bio list. So one should not be calling
612 * this function with a different bio if there are other bios
613 * queued.
614 */
615 BUG_ON(tg->nr_queued[rw] && bio != bio_list_peek(&tg->bio_lists[rw]));
616
617 /* If tg->bps = -1, then BW is unlimited */
618 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
619 if (wait)
620 *wait = 0;
621 return 1;
622 }
623
624 /*
625 * If previous slice expired, start a new one otherwise renew/extend
626 * existing slice to make sure it is at least throtl_slice interval
627 * long since now.
628 */
629 if (throtl_slice_used(td, tg, rw))
630 throtl_start_new_slice(td, tg, rw);
631 else {
632 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
633 throtl_extend_slice(td, tg, rw, jiffies + throtl_slice);
634 }
635
636 if (tg_with_in_bps_limit(td, tg, bio, &bps_wait)
637 && tg_with_in_iops_limit(td, tg, bio, &iops_wait)) {
638 if (wait)
639 *wait = 0;
640 return 1;
641 }
642
643 max_wait = max(bps_wait, iops_wait);
644
645 if (wait)
646 *wait = max_wait;
647
648 if (time_before(tg->slice_end[rw], jiffies + max_wait))
649 throtl_extend_slice(td, tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400650
651 return 0;
652}
653
Tejun Heo3c798392012-04-16 13:57:25 -0700654static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700655 int rw)
656{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700657 struct throtl_grp *tg = blkg_to_tg(blkg);
658 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700659 unsigned long flags;
660
661 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700662 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700663 return;
664
665 /*
666 * Disabling interrupts to provide mutual exclusion between two
667 * writes on same cpu. It probably is not needed for 64bit. Not
668 * optimizing that case yet.
669 */
670 local_irq_save(flags);
671
Tejun Heo8a3d2612012-04-01 14:38:44 -0700672 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700673
Tejun Heo629ed0b2012-04-01 14:38:44 -0700674 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
675 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
676
677 local_irq_restore(flags);
678}
679
Vivek Goyale43473b2010-09-15 17:06:35 -0400680static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
681{
682 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400683
684 /* Charge the bio to the group */
685 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400686 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400687
Tejun Heo629ed0b2012-04-01 14:38:44 -0700688 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400689}
690
691static void throtl_add_bio_tg(struct throtl_data *td, struct throtl_grp *tg,
692 struct bio *bio)
693{
694 bool rw = bio_data_dir(bio);
695
696 bio_list_add(&tg->bio_lists[rw], bio);
697 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800698 blkg_get(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400699 tg->nr_queued[rw]++;
700 td->nr_queued[rw]++;
701 throtl_enqueue_tg(td, tg);
702}
703
704static void tg_update_disptime(struct throtl_data *td, struct throtl_grp *tg)
705{
706 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
707 struct bio *bio;
708
709 if ((bio = bio_list_peek(&tg->bio_lists[READ])))
710 tg_may_dispatch(td, tg, bio, &read_wait);
711
712 if ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
713 tg_may_dispatch(td, tg, bio, &write_wait);
714
715 min_wait = min(read_wait, write_wait);
716 disptime = jiffies + min_wait;
717
Vivek Goyale43473b2010-09-15 17:06:35 -0400718 /* Update dispatch time */
719 throtl_dequeue_tg(td, tg);
720 tg->disptime = disptime;
721 throtl_enqueue_tg(td, tg);
722}
723
724static void tg_dispatch_one_bio(struct throtl_data *td, struct throtl_grp *tg,
725 bool rw, struct bio_list *bl)
726{
727 struct bio *bio;
728
729 bio = bio_list_pop(&tg->bio_lists[rw]);
730 tg->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800731 /* Drop bio reference on blkg */
732 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400733
734 BUG_ON(td->nr_queued[rw] <= 0);
735 td->nr_queued[rw]--;
736
737 throtl_charge_bio(tg, bio);
738 bio_list_add(bl, bio);
739 bio->bi_rw |= REQ_THROTTLED;
740
741 throtl_trim_slice(td, tg, rw);
742}
743
744static int throtl_dispatch_tg(struct throtl_data *td, struct throtl_grp *tg,
745 struct bio_list *bl)
746{
747 unsigned int nr_reads = 0, nr_writes = 0;
748 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100749 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400750 struct bio *bio;
751
752 /* Try to dispatch 75% READS and 25% WRITES */
753
754 while ((bio = bio_list_peek(&tg->bio_lists[READ]))
755 && tg_may_dispatch(td, tg, bio, NULL)) {
756
757 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
758 nr_reads++;
759
760 if (nr_reads >= max_nr_reads)
761 break;
762 }
763
764 while ((bio = bio_list_peek(&tg->bio_lists[WRITE]))
765 && tg_may_dispatch(td, tg, bio, NULL)) {
766
767 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), bl);
768 nr_writes++;
769
770 if (nr_writes >= max_nr_writes)
771 break;
772 }
773
774 return nr_reads + nr_writes;
775}
776
777static int throtl_select_dispatch(struct throtl_data *td, struct bio_list *bl)
778{
779 unsigned int nr_disp = 0;
780 struct throtl_grp *tg;
Tejun Heoc9e03322013-05-14 13:52:32 -0700781 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400782
783 while (1) {
Tejun Heoc9e03322013-05-14 13:52:32 -0700784 tg = throtl_rb_first(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400785
786 if (!tg)
787 break;
788
789 if (time_before(jiffies, tg->disptime))
790 break;
791
792 throtl_dequeue_tg(td, tg);
793
794 nr_disp += throtl_dispatch_tg(td, tg, bl);
795
Tejun Heo2db63142013-05-14 13:52:31 -0700796 if (tg->nr_queued[0] || tg->nr_queued[1])
Vivek Goyale43473b2010-09-15 17:06:35 -0400797 tg_update_disptime(td, tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400798
799 if (nr_disp >= throtl_quantum)
800 break;
801 }
802
803 return nr_disp;
804}
805
Tejun Heocb761992013-05-14 13:52:31 -0700806/* work function to dispatch throttled bios */
807void blk_throtl_dispatch_work_fn(struct work_struct *work)
Vivek Goyale43473b2010-09-15 17:06:35 -0400808{
Tejun Heocb761992013-05-14 13:52:31 -0700809 struct throtl_data *td = container_of(to_delayed_work(work),
810 struct throtl_data, dispatch_work);
811 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400812 unsigned int nr_disp = 0;
813 struct bio_list bio_list_on_stack;
814 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100815 struct blk_plug plug;
Vivek Goyale43473b2010-09-15 17:06:35 -0400816
817 spin_lock_irq(q->queue_lock);
818
Vivek Goyale43473b2010-09-15 17:06:35 -0400819 bio_list_init(&bio_list_on_stack);
820
Joe Perchesd2f31a52011-06-13 20:19:27 +0200821 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo6a525602013-05-14 13:52:32 -0700822 td->nr_queued[READ] + td->nr_queued[WRITE],
823 td->nr_queued[READ], td->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400824
825 nr_disp = throtl_select_dispatch(td, &bio_list_on_stack);
826
827 if (nr_disp)
828 throtl_log(td, "bios disp=%u", nr_disp);
829
830 throtl_schedule_next_dispatch(td);
Tejun Heo6a525602013-05-14 13:52:32 -0700831
Vivek Goyale43473b2010-09-15 17:06:35 -0400832 spin_unlock_irq(q->queue_lock);
833
834 /*
835 * If we dispatched some requests, unplug the queue to make sure
836 * immediate dispatch
837 */
838 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100839 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400840 while((bio = bio_list_pop(&bio_list_on_stack)))
841 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100842 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400843 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400844}
845
Tejun Heof95a04a2012-04-16 13:57:26 -0700846static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
847 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700848{
Tejun Heof95a04a2012-04-16 13:57:26 -0700849 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -0700850 struct blkg_rwstat rwstat = { }, tmp;
851 int i, cpu;
852
853 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700854 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700855
856 tmp = blkg_rwstat_read((void *)sc + off);
857 for (i = 0; i < BLKG_RWSTAT_NR; i++)
858 rwstat.cnt[i] += tmp.cnt[i];
859 }
860
Tejun Heof95a04a2012-04-16 13:57:26 -0700861 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700862}
863
Tejun Heo8a3d2612012-04-01 14:38:44 -0700864static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
865 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700866{
Tejun Heo3c798392012-04-16 13:57:25 -0700867 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -0700868
Tejun Heo3c798392012-04-16 13:57:25 -0700869 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700870 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700871 return 0;
872}
873
Tejun Heof95a04a2012-04-16 13:57:26 -0700874static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
875 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700876{
Tejun Heof95a04a2012-04-16 13:57:26 -0700877 struct throtl_grp *tg = pd_to_tg(pd);
878 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700879
Tejun Heoaf133ce2012-04-01 14:38:44 -0700880 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700881 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700882 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700883}
884
Tejun Heof95a04a2012-04-16 13:57:26 -0700885static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
886 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700887{
Tejun Heof95a04a2012-04-16 13:57:26 -0700888 struct throtl_grp *tg = pd_to_tg(pd);
889 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700890
891 if (v == -1)
892 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700893 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700894}
895
896static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
897 struct seq_file *sf)
898{
Tejun Heo3c798392012-04-16 13:57:25 -0700899 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
900 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700901 return 0;
902}
903
Tejun Heoaf133ce2012-04-01 14:38:44 -0700904static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
905 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400906{
Tejun Heo3c798392012-04-16 13:57:25 -0700907 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
908 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700909 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400910}
911
Tejun Heoaf133ce2012-04-01 14:38:44 -0700912static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
913 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700914{
Tejun Heo3c798392012-04-16 13:57:25 -0700915 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700916 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700917 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700918 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700919 int ret;
920
Tejun Heo3c798392012-04-16 13:57:25 -0700921 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700922 if (ret)
923 return ret;
924
Tejun Heoaf133ce2012-04-01 14:38:44 -0700925 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700926 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700927
Tejun Heoa2b16932012-04-13 13:11:33 -0700928 if (!ctx.v)
929 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700930
Tejun Heoa2b16932012-04-13 13:11:33 -0700931 if (is_u64)
932 *(u64 *)((void *)tg + cft->private) = ctx.v;
933 else
934 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700935
Tejun Heo632b4492013-05-14 13:52:31 -0700936 throtl_log_tg(td, tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
937 tg->bps[READ], tg->bps[WRITE],
938 tg->iops[READ], tg->iops[WRITE]);
939
940 /*
941 * We're already holding queue_lock and know @tg is valid. Let's
942 * apply the new config directly.
943 *
944 * Restart the slices for both READ and WRITES. It might happen
945 * that a group's limit are dropped suddenly and we don't want to
946 * account recently dispatched IO with new low rate.
947 */
948 throtl_start_new_slice(td, tg, 0);
949 throtl_start_new_slice(td, tg, 1);
950
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700951 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo632b4492013-05-14 13:52:31 -0700952 tg_update_disptime(td, tg);
953 throtl_schedule_next_dispatch(td);
954 }
Tejun Heo60c2bc22012-04-01 14:38:43 -0700955
956 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -0700957 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700958}
959
Tejun Heoaf133ce2012-04-01 14:38:44 -0700960static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
961 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700962{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700963 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700964}
965
Tejun Heoaf133ce2012-04-01 14:38:44 -0700966static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
967 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700968{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700969 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700970}
971
972static struct cftype throtl_files[] = {
973 {
974 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -0700975 .private = offsetof(struct throtl_grp, bps[READ]),
976 .read_seq_string = tg_print_conf_u64,
977 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -0700978 .max_write_len = 256,
979 },
980 {
981 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -0700982 .private = offsetof(struct throtl_grp, bps[WRITE]),
983 .read_seq_string = tg_print_conf_u64,
984 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -0700985 .max_write_len = 256,
986 },
987 {
988 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -0700989 .private = offsetof(struct throtl_grp, iops[READ]),
990 .read_seq_string = tg_print_conf_uint,
991 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -0700992 .max_write_len = 256,
993 },
994 {
995 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -0700996 .private = offsetof(struct throtl_grp, iops[WRITE]),
997 .read_seq_string = tg_print_conf_uint,
998 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -0700999 .max_write_len = 256,
1000 },
1001 {
1002 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001003 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001004 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001005 },
1006 {
1007 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001008 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001009 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001010 },
1011 { } /* terminate */
1012};
1013
Vivek Goyalda527772011-03-02 19:05:33 -05001014static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001015{
1016 struct throtl_data *td = q->td;
1017
Tejun Heocb761992013-05-14 13:52:31 -07001018 cancel_delayed_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001019}
1020
Tejun Heo3c798392012-04-16 13:57:25 -07001021static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001022 .pd_size = sizeof(struct throtl_grp),
1023 .cftypes = throtl_files,
1024
1025 .pd_init_fn = throtl_pd_init,
1026 .pd_exit_fn = throtl_pd_exit,
1027 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001028};
1029
Tejun Heobc16a4f2011-10-19 14:33:01 +02001030bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001031{
1032 struct throtl_data *td = q->td;
1033 struct throtl_grp *tg;
Vivek Goyale43473b2010-09-15 17:06:35 -04001034 bool rw = bio_data_dir(bio), update_disptime = true;
Tejun Heo3c798392012-04-16 13:57:25 -07001035 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001036 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001037
1038 if (bio->bi_rw & REQ_THROTTLED) {
1039 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001040 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001041 }
1042
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001043 /*
1044 * A throtl_grp pointer retrieved under rcu can be used to access
1045 * basic fields like stats and io rates. If a group has no rules,
1046 * just update the dispatch stats in lockless manner and return.
1047 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001048 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001049 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001050 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001051 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001052 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001053 throtl_update_dispatch_stats(tg_to_blkg(tg),
1054 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001055 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001056 }
1057 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001058
1059 /*
1060 * Either group has not been allocated yet or it is not an unlimited
1061 * IO group
1062 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001063 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001064 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001065 if (unlikely(!tg))
1066 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001067
Vivek Goyale43473b2010-09-15 17:06:35 -04001068 if (tg->nr_queued[rw]) {
1069 /*
1070 * There is already another bio queued in same dir. No
1071 * need to update dispatch time.
1072 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001073 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001074 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001075
Vivek Goyale43473b2010-09-15 17:06:35 -04001076 }
1077
1078 /* Bio is with-in rate limit of group */
1079 if (tg_may_dispatch(td, tg, bio, NULL)) {
1080 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001081
1082 /*
1083 * We need to trim slice even when bios are not being queued
1084 * otherwise it might happen that a bio is not queued for
1085 * a long time and slice keeps on extending and trim is not
1086 * called for a long time. Now if limits are reduced suddenly
1087 * we take into account all the IO dispatched so far at new
1088 * low rate and * newly queued IO gets a really long dispatch
1089 * time.
1090 *
1091 * So keep on trimming slice even if bio is not queued.
1092 */
1093 throtl_trim_slice(td, tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001094 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001095 }
1096
1097queue_bio:
Joe Perchesfd16d262011-06-13 10:42:49 +02001098 throtl_log_tg(td, tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001099 " iodisp=%u iops=%u queued=%d/%d",
1100 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001101 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001102 tg->io_disp[rw], tg->iops[rw],
Vivek Goyale43473b2010-09-15 17:06:35 -04001103 tg->nr_queued[READ], tg->nr_queued[WRITE]);
1104
Tejun Heo671058f2012-03-05 13:15:29 -08001105 bio_associate_current(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001106 throtl_add_bio_tg(q->td, tg, bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001107 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001108
1109 if (update_disptime) {
1110 tg_update_disptime(td, tg);
1111 throtl_schedule_next_dispatch(td);
1112 }
1113
Tejun Heobc16a4f2011-10-19 14:33:01 +02001114out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001115 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001116out_unlock_rcu:
1117 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001118out:
1119 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001120}
1121
Tejun Heoc9a929d2011-10-19 14:42:16 +02001122/**
1123 * blk_throtl_drain - drain throttled bios
1124 * @q: request_queue to drain throttled bios for
1125 *
1126 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1127 */
1128void blk_throtl_drain(struct request_queue *q)
1129 __releases(q->queue_lock) __acquires(q->queue_lock)
1130{
1131 struct throtl_data *td = q->td;
Tejun Heoc9e03322013-05-14 13:52:32 -07001132 struct throtl_service_queue *sq = &td->service_queue;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001133 struct throtl_grp *tg;
1134 struct bio_list bl;
1135 struct bio *bio;
1136
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001137 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001138
1139 bio_list_init(&bl);
1140
Tejun Heoc9e03322013-05-14 13:52:32 -07001141 while ((tg = throtl_rb_first(sq))) {
Tejun Heoc9a929d2011-10-19 14:42:16 +02001142 throtl_dequeue_tg(td, tg);
1143
1144 while ((bio = bio_list_peek(&tg->bio_lists[READ])))
1145 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1146 while ((bio = bio_list_peek(&tg->bio_lists[WRITE])))
1147 tg_dispatch_one_bio(td, tg, bio_data_dir(bio), &bl);
1148 }
1149 spin_unlock_irq(q->queue_lock);
1150
1151 while ((bio = bio_list_pop(&bl)))
1152 generic_make_request(bio);
1153
1154 spin_lock_irq(q->queue_lock);
1155}
1156
Vivek Goyale43473b2010-09-15 17:06:35 -04001157int blk_throtl_init(struct request_queue *q)
1158{
1159 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001160 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001161
1162 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1163 if (!td)
1164 return -ENOMEM;
1165
Tejun Heoc9e03322013-05-14 13:52:32 -07001166 td->service_queue = THROTL_SERVICE_QUEUE_INITIALIZER;
Tejun Heocb761992013-05-14 13:52:31 -07001167 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Vivek Goyale43473b2010-09-15 17:06:35 -04001168
Tejun Heocd1604f2012-03-05 13:15:06 -08001169 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001170 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001171
Tejun Heoa2b16932012-04-13 13:11:33 -07001172 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001173 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001174 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001175 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001176 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001177}
1178
1179void blk_throtl_exit(struct request_queue *q)
1180{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001181 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001182 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001183 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001184 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001185}
1186
1187static int __init throtl_init(void)
1188{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001189 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1190 if (!kthrotld_workqueue)
1191 panic("Failed to create kthrotld\n");
1192
Tejun Heo3c798392012-04-16 13:57:25 -07001193 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001194}
1195
1196module_init(throtl_init);