blob: ec9397f3eb0acf8064a4d4484ea0cafd95eaa6cc [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 {
Tejun Heo73f0d492013-05-14 13:52:35 -070030 /*
31 * Bios queued directly to this service_queue or dispatched from
32 * children throtl_grp's.
33 */
34 struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
35 unsigned int nr_queued[2]; /* number of queued bios */
36
37 /*
38 * RB tree of active children throtl_grp's, which are sorted by
39 * their ->disptime.
40 */
Tejun Heoc9e03322013-05-14 13:52:32 -070041 struct rb_root pending_tree; /* RB tree of active tgs */
42 struct rb_node *first_pending; /* first node in the tree */
43 unsigned int nr_pending; /* # queued in the tree */
44 unsigned long first_pending_disptime; /* disptime of the first tg */
Vivek Goyale43473b2010-09-15 17:06:35 -040045};
46
Tejun Heo5b2c16a2013-05-14 13:52:32 -070047enum tg_state_flags {
48 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070049 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070050};
51
Vivek Goyale43473b2010-09-15 17:06:35 -040052#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
53
Tejun Heo8a3d2612012-04-01 14:38:44 -070054/* Per-cpu group stats */
55struct tg_stats_cpu {
56 /* total bytes transferred */
57 struct blkg_rwstat service_bytes;
58 /* total IOs serviced, post merge */
59 struct blkg_rwstat serviced;
60};
61
Vivek Goyale43473b2010-09-15 17:06:35 -040062struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070063 /* must be the first member */
64 struct blkg_policy_data pd;
65
Tejun Heoc9e03322013-05-14 13:52:32 -070066 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040067 struct rb_node rb_node;
68
Tejun Heo0f3457f2013-05-14 13:52:32 -070069 /* throtl_data this group belongs to */
70 struct throtl_data *td;
71
Tejun Heo49a2f1e2013-05-14 13:52:34 -070072 /* this group's service queue */
73 struct throtl_service_queue service_queue;
74
Vivek Goyale43473b2010-09-15 17:06:35 -040075 /*
76 * Dispatch time in jiffies. This is the estimated time when group
77 * will unthrottle and is ready to dispatch more bio. It is used as
78 * key to sort active groups in service tree.
79 */
80 unsigned long disptime;
81
Vivek Goyale43473b2010-09-15 17:06:35 -040082 unsigned int flags;
83
Vivek Goyale43473b2010-09-15 17:06:35 -040084 /* bytes per second rate limits */
85 uint64_t bps[2];
86
Vivek Goyal8e89d132010-09-15 17:06:37 -040087 /* IOPS limits */
88 unsigned int iops[2];
89
Vivek Goyale43473b2010-09-15 17:06:35 -040090 /* Number of bytes disptached in current slice */
91 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040092 /* Number of bio's dispatched in current slice */
93 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040094
95 /* When did we start a new slice */
96 unsigned long slice_start[2];
97 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020098
Tejun Heo8a3d2612012-04-01 14:38:44 -070099 /* Per cpu stats pointer */
100 struct tg_stats_cpu __percpu *stats_cpu;
101
102 /* List of tgs waiting for per cpu stats memory to be allocated */
103 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400104};
105
106struct throtl_data
107{
Vivek Goyale43473b2010-09-15 17:06:35 -0400108 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700109 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400110
Vivek Goyale43473b2010-09-15 17:06:35 -0400111 struct request_queue *queue;
112
113 /* Total Number of queued bios on READ and WRITE lists */
114 unsigned int nr_queued[2];
115
116 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200117 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400118 */
119 unsigned int nr_undestroyed_grps;
120
121 /* Work for dispatching throttled bios */
Tejun Heocb761992013-05-14 13:52:31 -0700122 struct delayed_work dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400123};
124
Tejun Heo8a3d2612012-04-01 14:38:44 -0700125/* list and work item to allocate percpu group stats */
126static DEFINE_SPINLOCK(tg_stats_alloc_lock);
127static LIST_HEAD(tg_stats_alloc_list);
128
129static void tg_stats_alloc_fn(struct work_struct *);
130static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
131
Tejun Heof95a04a2012-04-16 13:57:26 -0700132static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
133{
134 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
135}
136
Tejun Heo3c798392012-04-16 13:57:25 -0700137static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800138{
Tejun Heof95a04a2012-04-16 13:57:26 -0700139 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800140}
141
Tejun Heo3c798392012-04-16 13:57:25 -0700142static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800143{
Tejun Heof95a04a2012-04-16 13:57:26 -0700144 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800145}
146
Tejun Heo03d8e112012-04-13 13:11:32 -0700147static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
148{
149 return blkg_to_tg(td->queue->root_blkg);
150}
151
Tejun Heo0f3457f2013-05-14 13:52:32 -0700152#define throtl_log_tg(tg, fmt, args...) do { \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700153 char __pbuf[128]; \
154 \
155 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
Tejun Heo0f3457f2013-05-14 13:52:32 -0700156 blk_add_trace_msg((tg)->td->queue, "throtl %s " fmt, __pbuf, ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700157} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400158
159#define throtl_log(td, fmt, args...) \
160 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
161
Tejun Heo8a3d2612012-04-01 14:38:44 -0700162/*
163 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700164 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700165 * allocation.
166 */
167static void tg_stats_alloc_fn(struct work_struct *work)
168{
169 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
170 struct delayed_work *dwork = to_delayed_work(work);
171 bool empty = false;
172
173alloc_stats:
174 if (!stats_cpu) {
175 stats_cpu = alloc_percpu(struct tg_stats_cpu);
176 if (!stats_cpu) {
177 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700178 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700179 return;
180 }
181 }
182
183 spin_lock_irq(&tg_stats_alloc_lock);
184
185 if (!list_empty(&tg_stats_alloc_list)) {
186 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
187 struct throtl_grp,
188 stats_alloc_node);
189 swap(tg->stats_cpu, stats_cpu);
190 list_del_init(&tg->stats_alloc_node);
191 }
192
193 empty = list_empty(&tg_stats_alloc_list);
194 spin_unlock_irq(&tg_stats_alloc_lock);
195 if (!empty)
196 goto alloc_stats;
197}
198
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700199/* init a service_queue, assumes the caller zeroed it */
200static void throtl_service_queue_init(struct throtl_service_queue *sq)
201{
Tejun Heo73f0d492013-05-14 13:52:35 -0700202 bio_list_init(&sq->bio_lists[0]);
203 bio_list_init(&sq->bio_lists[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700204 sq->pending_tree = RB_ROOT;
205}
206
Tejun Heo3c798392012-04-16 13:57:25 -0700207static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400208{
Tejun Heo03814112012-03-05 13:15:14 -0800209 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200210 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800211
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700212 throtl_service_queue_init(&tg->service_queue);
Vivek Goyala29a1712011-05-19 15:38:19 -0400213 RB_CLEAR_NODE(&tg->rb_node);
Tejun Heo0f3457f2013-05-14 13:52:32 -0700214 tg->td = blkg->q->td;
Vivek Goyala29a1712011-05-19 15:38:19 -0400215
Tejun Heoe56da7e2012-03-05 13:15:07 -0800216 tg->bps[READ] = -1;
217 tg->bps[WRITE] = -1;
218 tg->iops[READ] = -1;
219 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700220
221 /*
222 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
223 * but percpu allocator can't be called from IO path. Queue tg on
224 * tg_stats_alloc_list and allocate from work item.
225 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200226 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700227 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700228 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200229 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700230}
231
Tejun Heo3c798392012-04-16 13:57:25 -0700232static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700233{
234 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200235 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700236
Tejun Heoff26eaa2012-05-23 12:16:21 +0200237 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700238 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200239 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700240
241 free_percpu(tg->stats_cpu);
242}
243
Tejun Heo3c798392012-04-16 13:57:25 -0700244static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700245{
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
Tejun Heo3c798392012-04-16 13:57:25 -0700260static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
261 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400262{
Vivek Goyale43473b2010-09-15 17:06:35 -0400263 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700264 * This is the common case when there are no blkcgs. Avoid lookup
265 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800266 */
Tejun Heo3c798392012-04-16 13:57:25 -0700267 if (blkcg == &blkcg_root)
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,
Tejun Heo3c798392012-04-16 13:57:25 -0700274 struct blkcg *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 Heo3c798392012-04-16 13:57:25 -0700280 * This is the common case when there are no blkcgs. Avoid lookup
281 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400282 */
Tejun Heo3c798392012-04-16 13:57:25 -0700283 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700284 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800285 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700286 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800287
Tejun Heo3c96cb32012-04-13 13:11:34 -0700288 blkg = blkg_lookup_create(blkcg, q);
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);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100293 else if (!blk_queue_dying(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
Tejun Heo0049af72013-05-14 13:52:33 -0700300static struct throtl_grp *
301throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400302{
303 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700304 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400305 return NULL;
306
Tejun Heo0049af72013-05-14 13:52:33 -0700307 if (!parent_sq->first_pending)
308 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400309
Tejun Heo0049af72013-05-14 13:52:33 -0700310 if (parent_sq->first_pending)
311 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400312
313 return NULL;
314}
315
316static void rb_erase_init(struct rb_node *n, struct rb_root *root)
317{
318 rb_erase(n, root);
319 RB_CLEAR_NODE(n);
320}
321
Tejun Heo0049af72013-05-14 13:52:33 -0700322static void throtl_rb_erase(struct rb_node *n,
323 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400324{
Tejun Heo0049af72013-05-14 13:52:33 -0700325 if (parent_sq->first_pending == n)
326 parent_sq->first_pending = NULL;
327 rb_erase_init(n, &parent_sq->pending_tree);
328 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400329}
330
Tejun Heo0049af72013-05-14 13:52:33 -0700331static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400332{
333 struct throtl_grp *tg;
334
Tejun Heo0049af72013-05-14 13:52:33 -0700335 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400336 if (!tg)
337 return;
338
Tejun Heo0049af72013-05-14 13:52:33 -0700339 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400340}
341
Tejun Heo0049af72013-05-14 13:52:33 -0700342static void tg_service_queue_add(struct throtl_grp *tg,
343 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400344{
Tejun Heo0049af72013-05-14 13:52:33 -0700345 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400346 struct rb_node *parent = NULL;
347 struct throtl_grp *__tg;
348 unsigned long key = tg->disptime;
349 int left = 1;
350
351 while (*node != NULL) {
352 parent = *node;
353 __tg = rb_entry_tg(parent);
354
355 if (time_before(key, __tg->disptime))
356 node = &parent->rb_left;
357 else {
358 node = &parent->rb_right;
359 left = 0;
360 }
361 }
362
363 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700364 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400365
366 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700367 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400368}
369
Tejun Heo0049af72013-05-14 13:52:33 -0700370static void __throtl_enqueue_tg(struct throtl_grp *tg,
371 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400372{
Tejun Heo0049af72013-05-14 13:52:33 -0700373 tg_service_queue_add(tg, parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700374 tg->flags |= THROTL_TG_PENDING;
Tejun Heo0049af72013-05-14 13:52:33 -0700375 parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400376}
377
Tejun Heo0049af72013-05-14 13:52:33 -0700378static void throtl_enqueue_tg(struct throtl_grp *tg,
379 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400380{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700381 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo0049af72013-05-14 13:52:33 -0700382 __throtl_enqueue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400383}
384
Tejun Heo0049af72013-05-14 13:52:33 -0700385static void __throtl_dequeue_tg(struct throtl_grp *tg,
386 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400387{
Tejun Heo0049af72013-05-14 13:52:33 -0700388 throtl_rb_erase(&tg->rb_node, parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700389 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400390}
391
Tejun Heo0049af72013-05-14 13:52:33 -0700392static void throtl_dequeue_tg(struct throtl_grp *tg,
393 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400394{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700395 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo0049af72013-05-14 13:52:33 -0700396 __throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400397}
398
Tejun Heoa9131a22013-05-14 13:52:31 -0700399/* Call with queue lock held */
400static void throtl_schedule_delayed_work(struct throtl_data *td,
401 unsigned long delay)
402{
403 struct delayed_work *dwork = &td->dispatch_work;
404
Tejun Heo6a525602013-05-14 13:52:32 -0700405 mod_delayed_work(kthrotld_workqueue, dwork, delay);
406 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700407}
408
Vivek Goyale43473b2010-09-15 17:06:35 -0400409static void throtl_schedule_next_dispatch(struct throtl_data *td)
410{
Tejun Heoc9e03322013-05-14 13:52:32 -0700411 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400412
Tejun Heo6a525602013-05-14 13:52:32 -0700413 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700414 if (!sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400415 return;
416
Tejun Heoc9e03322013-05-14 13:52:32 -0700417 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400418
Tejun Heoc9e03322013-05-14 13:52:32 -0700419 if (time_before_eq(sq->first_pending_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500420 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400421 else
Tejun Heoc9e03322013-05-14 13:52:32 -0700422 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400423}
424
Tejun Heo0f3457f2013-05-14 13:52:32 -0700425static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400426{
427 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400428 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400429 tg->slice_start[rw] = jiffies;
430 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heo0f3457f2013-05-14 13:52:32 -0700431 throtl_log_tg(tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400432 rw == READ ? 'R' : 'W', tg->slice_start[rw],
433 tg->slice_end[rw], jiffies);
434}
435
Tejun Heo0f3457f2013-05-14 13:52:32 -0700436static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
437 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100438{
439 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
440}
441
Tejun Heo0f3457f2013-05-14 13:52:32 -0700442static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
443 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400444{
445 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heo0f3457f2013-05-14 13:52:32 -0700446 throtl_log_tg(tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400447 rw == READ ? 'R' : 'W', tg->slice_start[rw],
448 tg->slice_end[rw], jiffies);
449}
450
451/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700452static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400453{
454 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
455 return 0;
456
457 return 1;
458}
459
460/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700461static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400462{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200463 unsigned long nr_slices, time_elapsed, io_trim;
464 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400465
466 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
467
468 /*
469 * If bps are unlimited (-1), then time slice don't get
470 * renewed. Don't try to trim the slice if slice is used. A new
471 * slice will start when appropriate.
472 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700473 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400474 return;
475
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100476 /*
477 * A bio has been dispatched. Also adjust slice_end. It might happen
478 * that initially cgroup limit was very low resulting in high
479 * slice_end, but later limit was bumped up and bio was dispached
480 * sooner, then we need to reduce slice_end. A high bogus slice_end
481 * is bad because it does not allow new slice to start.
482 */
483
Tejun Heo0f3457f2013-05-14 13:52:32 -0700484 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100485
Vivek Goyale43473b2010-09-15 17:06:35 -0400486 time_elapsed = jiffies - tg->slice_start[rw];
487
488 nr_slices = time_elapsed / throtl_slice;
489
490 if (!nr_slices)
491 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200492 tmp = tg->bps[rw] * throtl_slice * nr_slices;
493 do_div(tmp, HZ);
494 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400495
Vivek Goyal8e89d132010-09-15 17:06:37 -0400496 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400497
Vivek Goyal8e89d132010-09-15 17:06:37 -0400498 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400499 return;
500
501 if (tg->bytes_disp[rw] >= bytes_trim)
502 tg->bytes_disp[rw] -= bytes_trim;
503 else
504 tg->bytes_disp[rw] = 0;
505
Vivek Goyal8e89d132010-09-15 17:06:37 -0400506 if (tg->io_disp[rw] >= io_trim)
507 tg->io_disp[rw] -= io_trim;
508 else
509 tg->io_disp[rw] = 0;
510
Vivek Goyale43473b2010-09-15 17:06:35 -0400511 tg->slice_start[rw] += nr_slices * throtl_slice;
512
Tejun Heo0f3457f2013-05-14 13:52:32 -0700513 throtl_log_tg(tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400514 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400515 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400516 tg->slice_start[rw], tg->slice_end[rw], jiffies);
517}
518
Tejun Heo0f3457f2013-05-14 13:52:32 -0700519static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
520 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400521{
522 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400523 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400524 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200525 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400526
Vivek Goyal8e89d132010-09-15 17:06:37 -0400527 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400528
Vivek Goyal8e89d132010-09-15 17:06:37 -0400529 /* Slice has just started. Consider one slice interval */
530 if (!jiffy_elapsed)
531 jiffy_elapsed_rnd = throtl_slice;
532
533 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
534
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200535 /*
536 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
537 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
538 * will allow dispatch after 1 second and after that slice should
539 * have been trimmed.
540 */
541
542 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
543 do_div(tmp, HZ);
544
545 if (tmp > UINT_MAX)
546 io_allowed = UINT_MAX;
547 else
548 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400549
550 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400551 if (wait)
552 *wait = 0;
553 return 1;
554 }
555
Vivek Goyal8e89d132010-09-15 17:06:37 -0400556 /* Calc approx time to dispatch */
557 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
558
559 if (jiffy_wait > jiffy_elapsed)
560 jiffy_wait = jiffy_wait - jiffy_elapsed;
561 else
562 jiffy_wait = 1;
563
564 if (wait)
565 *wait = jiffy_wait;
566 return 0;
567}
568
Tejun Heo0f3457f2013-05-14 13:52:32 -0700569static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
570 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400571{
572 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200573 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400574 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400575
576 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
577
578 /* Slice has just started. Consider one slice interval */
579 if (!jiffy_elapsed)
580 jiffy_elapsed_rnd = throtl_slice;
581
582 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
583
Vivek Goyal5e901a22010-10-01 21:16:38 +0200584 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
585 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200586 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400587
588 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
589 if (wait)
590 *wait = 0;
591 return 1;
592 }
593
594 /* Calc approx time to dispatch */
595 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
596 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
597
598 if (!jiffy_wait)
599 jiffy_wait = 1;
600
601 /*
602 * This wait time is without taking into consideration the rounding
603 * up we did. Add that time also.
604 */
605 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400606 if (wait)
607 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400608 return 0;
609}
Vivek Goyale43473b2010-09-15 17:06:35 -0400610
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400611static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
612 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
613 return 1;
614 return 0;
615}
616
Vivek Goyal8e89d132010-09-15 17:06:37 -0400617/*
618 * Returns whether one can dispatch a bio or not. Also returns approx number
619 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
620 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700621static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
622 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400623{
624 bool rw = bio_data_dir(bio);
625 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
626
627 /*
628 * Currently whole state machine of group depends on first bio
629 * queued in the group bio list. So one should not be calling
630 * this function with a different bio if there are other bios
631 * queued.
632 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700633 BUG_ON(tg->service_queue.nr_queued[rw] &&
634 bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400635
636 /* If tg->bps = -1, then BW is unlimited */
637 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
638 if (wait)
639 *wait = 0;
640 return 1;
641 }
642
643 /*
644 * If previous slice expired, start a new one otherwise renew/extend
645 * existing slice to make sure it is at least throtl_slice interval
646 * long since now.
647 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700648 if (throtl_slice_used(tg, rw))
649 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400650 else {
651 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700652 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400653 }
654
Tejun Heo0f3457f2013-05-14 13:52:32 -0700655 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
656 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400657 if (wait)
658 *wait = 0;
659 return 1;
660 }
661
662 max_wait = max(bps_wait, iops_wait);
663
664 if (wait)
665 *wait = max_wait;
666
667 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700668 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400669
670 return 0;
671}
672
Tejun Heo3c798392012-04-16 13:57:25 -0700673static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700674 int rw)
675{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700676 struct throtl_grp *tg = blkg_to_tg(blkg);
677 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700678 unsigned long flags;
679
680 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700681 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700682 return;
683
684 /*
685 * Disabling interrupts to provide mutual exclusion between two
686 * writes on same cpu. It probably is not needed for 64bit. Not
687 * optimizing that case yet.
688 */
689 local_irq_save(flags);
690
Tejun Heo8a3d2612012-04-01 14:38:44 -0700691 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700692
Tejun Heo629ed0b2012-04-01 14:38:44 -0700693 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
694 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
695
696 local_irq_restore(flags);
697}
698
Vivek Goyale43473b2010-09-15 17:06:35 -0400699static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
700{
701 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400702
703 /* Charge the bio to the group */
704 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400705 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400706
Tejun Heo629ed0b2012-04-01 14:38:44 -0700707 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400708}
709
Tejun Heo0049af72013-05-14 13:52:33 -0700710static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg,
711 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400712{
Tejun Heo73f0d492013-05-14 13:52:35 -0700713 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400714 bool rw = bio_data_dir(bio);
715
Tejun Heo0e9f4162013-05-14 13:52:35 -0700716 /*
717 * If @tg doesn't currently have any bios queued in the same
718 * direction, queueing @bio can change when @tg should be
719 * dispatched. Mark that @tg was empty. This is automatically
720 * cleaered on the next tg_update_disptime().
721 */
722 if (!sq->nr_queued[rw])
723 tg->flags |= THROTL_TG_WAS_EMPTY;
724
Tejun Heo73f0d492013-05-14 13:52:35 -0700725 bio_list_add(&sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400726 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800727 blkg_get(tg_to_blkg(tg));
Tejun Heo73f0d492013-05-14 13:52:35 -0700728 sq->nr_queued[rw]++;
Tejun Heoe2d57e62013-05-14 13:52:33 -0700729 tg->td->nr_queued[rw]++;
Tejun Heo0049af72013-05-14 13:52:33 -0700730 throtl_enqueue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400731}
732
Tejun Heo0049af72013-05-14 13:52:33 -0700733static void tg_update_disptime(struct throtl_grp *tg,
734 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400735{
Tejun Heo73f0d492013-05-14 13:52:35 -0700736 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400737 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
738 struct bio *bio;
739
Tejun Heo73f0d492013-05-14 13:52:35 -0700740 if ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700741 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400742
Tejun Heo73f0d492013-05-14 13:52:35 -0700743 if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700744 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400745
746 min_wait = min(read_wait, write_wait);
747 disptime = jiffies + min_wait;
748
Vivek Goyale43473b2010-09-15 17:06:35 -0400749 /* Update dispatch time */
Tejun Heo0049af72013-05-14 13:52:33 -0700750 throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400751 tg->disptime = disptime;
Tejun Heo0049af72013-05-14 13:52:33 -0700752 throtl_enqueue_tg(tg, parent_sq);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700753
754 /* see throtl_add_bio_tg() */
755 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400756}
757
Tejun Heo0f3457f2013-05-14 13:52:32 -0700758static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw,
Tejun Heo651930b2013-05-14 13:52:35 -0700759 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400760{
Tejun Heo73f0d492013-05-14 13:52:35 -0700761 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400762 struct bio *bio;
763
Tejun Heo73f0d492013-05-14 13:52:35 -0700764 bio = bio_list_pop(&sq->bio_lists[rw]);
765 sq->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800766 /* Drop bio reference on blkg */
767 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400768
Tejun Heo0f3457f2013-05-14 13:52:32 -0700769 BUG_ON(tg->td->nr_queued[rw] <= 0);
770 tg->td->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400771
772 throtl_charge_bio(tg, bio);
Tejun Heo651930b2013-05-14 13:52:35 -0700773 bio_list_add(&parent_sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400774 bio->bi_rw |= REQ_THROTTLED;
775
Tejun Heo0f3457f2013-05-14 13:52:32 -0700776 throtl_trim_slice(tg, rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400777}
778
Tejun Heo651930b2013-05-14 13:52:35 -0700779static int throtl_dispatch_tg(struct throtl_grp *tg,
780 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400781{
Tejun Heo73f0d492013-05-14 13:52:35 -0700782 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400783 unsigned int nr_reads = 0, nr_writes = 0;
784 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100785 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400786 struct bio *bio;
787
788 /* Try to dispatch 75% READS and 25% WRITES */
789
Tejun Heo73f0d492013-05-14 13:52:35 -0700790 while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700791 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400792
Tejun Heo651930b2013-05-14 13:52:35 -0700793 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400794 nr_reads++;
795
796 if (nr_reads >= max_nr_reads)
797 break;
798 }
799
Tejun Heo73f0d492013-05-14 13:52:35 -0700800 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700801 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400802
Tejun Heo651930b2013-05-14 13:52:35 -0700803 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400804 nr_writes++;
805
806 if (nr_writes >= max_nr_writes)
807 break;
808 }
809
810 return nr_reads + nr_writes;
811}
812
Tejun Heo651930b2013-05-14 13:52:35 -0700813static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400814{
815 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400816
817 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -0700818 struct throtl_grp *tg = throtl_rb_first(parent_sq);
819 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400820
821 if (!tg)
822 break;
823
824 if (time_before(jiffies, tg->disptime))
825 break;
826
Tejun Heo0049af72013-05-14 13:52:33 -0700827 throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400828
Tejun Heo651930b2013-05-14 13:52:35 -0700829 nr_disp += throtl_dispatch_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400830
Tejun Heo73f0d492013-05-14 13:52:35 -0700831 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo0049af72013-05-14 13:52:33 -0700832 tg_update_disptime(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400833
834 if (nr_disp >= throtl_quantum)
835 break;
836 }
837
838 return nr_disp;
839}
840
Tejun Heocb761992013-05-14 13:52:31 -0700841/* work function to dispatch throttled bios */
842void blk_throtl_dispatch_work_fn(struct work_struct *work)
Vivek Goyale43473b2010-09-15 17:06:35 -0400843{
Tejun Heocb761992013-05-14 13:52:31 -0700844 struct throtl_data *td = container_of(to_delayed_work(work),
845 struct throtl_data, dispatch_work);
Tejun Heo651930b2013-05-14 13:52:35 -0700846 struct throtl_service_queue *sq = &td->service_queue;
Tejun Heocb761992013-05-14 13:52:31 -0700847 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400848 unsigned int nr_disp = 0;
849 struct bio_list bio_list_on_stack;
850 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100851 struct blk_plug plug;
Tejun Heo651930b2013-05-14 13:52:35 -0700852 int rw;
Vivek Goyale43473b2010-09-15 17:06:35 -0400853
854 spin_lock_irq(q->queue_lock);
855
Vivek Goyale43473b2010-09-15 17:06:35 -0400856 bio_list_init(&bio_list_on_stack);
857
Joe Perchesd2f31a52011-06-13 20:19:27 +0200858 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo6a525602013-05-14 13:52:32 -0700859 td->nr_queued[READ] + td->nr_queued[WRITE],
860 td->nr_queued[READ], td->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400861
Tejun Heo651930b2013-05-14 13:52:35 -0700862 nr_disp = throtl_select_dispatch(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400863
Tejun Heo651930b2013-05-14 13:52:35 -0700864 if (nr_disp) {
865 for (rw = READ; rw <= WRITE; rw++) {
866 bio_list_merge(&bio_list_on_stack, &sq->bio_lists[rw]);
867 bio_list_init(&sq->bio_lists[rw]);
868 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400869 throtl_log(td, "bios disp=%u", nr_disp);
Tejun Heo651930b2013-05-14 13:52:35 -0700870 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400871
872 throtl_schedule_next_dispatch(td);
Tejun Heo6a525602013-05-14 13:52:32 -0700873
Vivek Goyale43473b2010-09-15 17:06:35 -0400874 spin_unlock_irq(q->queue_lock);
875
876 /*
877 * If we dispatched some requests, unplug the queue to make sure
878 * immediate dispatch
879 */
880 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100881 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400882 while((bio = bio_list_pop(&bio_list_on_stack)))
883 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100884 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400885 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400886}
887
Tejun Heof95a04a2012-04-16 13:57:26 -0700888static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
889 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700890{
Tejun Heof95a04a2012-04-16 13:57:26 -0700891 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -0700892 struct blkg_rwstat rwstat = { }, tmp;
893 int i, cpu;
894
895 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700896 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700897
898 tmp = blkg_rwstat_read((void *)sc + off);
899 for (i = 0; i < BLKG_RWSTAT_NR; i++)
900 rwstat.cnt[i] += tmp.cnt[i];
901 }
902
Tejun Heof95a04a2012-04-16 13:57:26 -0700903 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700904}
905
Tejun Heo8a3d2612012-04-01 14:38:44 -0700906static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
907 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700908{
Tejun Heo3c798392012-04-16 13:57:25 -0700909 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -0700910
Tejun Heo3c798392012-04-16 13:57:25 -0700911 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700912 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700913 return 0;
914}
915
Tejun Heof95a04a2012-04-16 13:57:26 -0700916static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
917 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700918{
Tejun Heof95a04a2012-04-16 13:57:26 -0700919 struct throtl_grp *tg = pd_to_tg(pd);
920 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700921
Tejun Heoaf133ce2012-04-01 14:38:44 -0700922 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700923 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700924 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700925}
926
Tejun Heof95a04a2012-04-16 13:57:26 -0700927static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
928 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700929{
Tejun Heof95a04a2012-04-16 13:57:26 -0700930 struct throtl_grp *tg = pd_to_tg(pd);
931 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700932
933 if (v == -1)
934 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700935 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700936}
937
938static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
939 struct seq_file *sf)
940{
Tejun Heo3c798392012-04-16 13:57:25 -0700941 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
942 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700943 return 0;
944}
945
Tejun Heoaf133ce2012-04-01 14:38:44 -0700946static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
947 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400948{
Tejun Heo3c798392012-04-16 13:57:25 -0700949 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
950 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700951 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400952}
953
Tejun Heoaf133ce2012-04-01 14:38:44 -0700954static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
955 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700956{
Tejun Heo3c798392012-04-16 13:57:25 -0700957 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700958 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700959 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700960 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700961 int ret;
962
Tejun Heo3c798392012-04-16 13:57:25 -0700963 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700964 if (ret)
965 return ret;
966
Tejun Heoaf133ce2012-04-01 14:38:44 -0700967 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700968 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700969
Tejun Heoa2b16932012-04-13 13:11:33 -0700970 if (!ctx.v)
971 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700972
Tejun Heoa2b16932012-04-13 13:11:33 -0700973 if (is_u64)
974 *(u64 *)((void *)tg + cft->private) = ctx.v;
975 else
976 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700977
Tejun Heo0f3457f2013-05-14 13:52:32 -0700978 throtl_log_tg(tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Tejun Heo632b4492013-05-14 13:52:31 -0700979 tg->bps[READ], tg->bps[WRITE],
980 tg->iops[READ], tg->iops[WRITE]);
981
982 /*
983 * We're already holding queue_lock and know @tg is valid. Let's
984 * apply the new config directly.
985 *
986 * Restart the slices for both READ and WRITES. It might happen
987 * that a group's limit are dropped suddenly and we don't want to
988 * account recently dispatched IO with new low rate.
989 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700990 throtl_start_new_slice(tg, 0);
991 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -0700992
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700993 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo0049af72013-05-14 13:52:33 -0700994 tg_update_disptime(tg, &td->service_queue);
Tejun Heo632b4492013-05-14 13:52:31 -0700995 throtl_schedule_next_dispatch(td);
996 }
Tejun Heo60c2bc22012-04-01 14:38:43 -0700997
998 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -0700999 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001000}
1001
Tejun Heoaf133ce2012-04-01 14:38:44 -07001002static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1003 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001004{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001005 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001006}
1007
Tejun Heoaf133ce2012-04-01 14:38:44 -07001008static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1009 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001010{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001011 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001012}
1013
1014static struct cftype throtl_files[] = {
1015 {
1016 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001017 .private = offsetof(struct throtl_grp, bps[READ]),
1018 .read_seq_string = tg_print_conf_u64,
1019 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001020 .max_write_len = 256,
1021 },
1022 {
1023 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001024 .private = offsetof(struct throtl_grp, bps[WRITE]),
1025 .read_seq_string = tg_print_conf_u64,
1026 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001027 .max_write_len = 256,
1028 },
1029 {
1030 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001031 .private = offsetof(struct throtl_grp, iops[READ]),
1032 .read_seq_string = tg_print_conf_uint,
1033 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001034 .max_write_len = 256,
1035 },
1036 {
1037 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001038 .private = offsetof(struct throtl_grp, iops[WRITE]),
1039 .read_seq_string = tg_print_conf_uint,
1040 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001041 .max_write_len = 256,
1042 },
1043 {
1044 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001045 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001046 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001047 },
1048 {
1049 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001050 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001051 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001052 },
1053 { } /* terminate */
1054};
1055
Vivek Goyalda527772011-03-02 19:05:33 -05001056static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001057{
1058 struct throtl_data *td = q->td;
1059
Tejun Heocb761992013-05-14 13:52:31 -07001060 cancel_delayed_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001061}
1062
Tejun Heo3c798392012-04-16 13:57:25 -07001063static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001064 .pd_size = sizeof(struct throtl_grp),
1065 .cftypes = throtl_files,
1066
1067 .pd_init_fn = throtl_pd_init,
1068 .pd_exit_fn = throtl_pd_exit,
1069 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001070};
1071
Tejun Heobc16a4f2011-10-19 14:33:01 +02001072bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001073{
1074 struct throtl_data *td = q->td;
1075 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001076 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001077 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001078 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001079 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001080
1081 if (bio->bi_rw & REQ_THROTTLED) {
1082 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001083 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001084 }
1085
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001086 /*
1087 * A throtl_grp pointer retrieved under rcu can be used to access
1088 * basic fields like stats and io rates. If a group has no rules,
1089 * just update the dispatch stats in lockless manner and return.
1090 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001091 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001092 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001093 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001094 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001095 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001096 throtl_update_dispatch_stats(tg_to_blkg(tg),
1097 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001098 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001099 }
1100 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001101
1102 /*
1103 * Either group has not been allocated yet or it is not an unlimited
1104 * IO group
1105 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001106 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001107 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001108 if (unlikely(!tg))
1109 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001110
Tejun Heo73f0d492013-05-14 13:52:35 -07001111 sq = &tg->service_queue;
1112
Tejun Heo0e9f4162013-05-14 13:52:35 -07001113 /* throtl is FIFO - if other bios are already queued, should queue */
1114 if (sq->nr_queued[rw])
Vivek Goyale43473b2010-09-15 17:06:35 -04001115 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001116
Vivek Goyale43473b2010-09-15 17:06:35 -04001117 /* Bio is with-in rate limit of group */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001118 if (tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001119 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001120
1121 /*
1122 * We need to trim slice even when bios are not being queued
1123 * otherwise it might happen that a bio is not queued for
1124 * a long time and slice keeps on extending and trim is not
1125 * called for a long time. Now if limits are reduced suddenly
1126 * we take into account all the IO dispatched so far at new
1127 * low rate and * newly queued IO gets a really long dispatch
1128 * time.
1129 *
1130 * So keep on trimming slice even if bio is not queued.
1131 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001132 throtl_trim_slice(tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001133 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001134 }
1135
1136queue_bio:
Tejun Heo0f3457f2013-05-14 13:52:32 -07001137 throtl_log_tg(tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001138 " iodisp=%u iops=%u queued=%d/%d",
1139 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001140 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001141 tg->io_disp[rw], tg->iops[rw],
Tejun Heo73f0d492013-05-14 13:52:35 -07001142 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001143
Tejun Heo671058f2012-03-05 13:15:29 -08001144 bio_associate_current(bio);
Tejun Heo0049af72013-05-14 13:52:33 -07001145 throtl_add_bio_tg(bio, tg, &q->td->service_queue);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001146 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001147
Tejun Heo0e9f4162013-05-14 13:52:35 -07001148 /* update @tg's dispatch time if @tg was empty before @bio */
1149 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo0049af72013-05-14 13:52:33 -07001150 tg_update_disptime(tg, &td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001151 throtl_schedule_next_dispatch(td);
1152 }
1153
Tejun Heobc16a4f2011-10-19 14:33:01 +02001154out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001155 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001156out_unlock_rcu:
1157 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001158out:
1159 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001160}
1161
Tejun Heoc9a929d2011-10-19 14:42:16 +02001162/**
1163 * blk_throtl_drain - drain throttled bios
1164 * @q: request_queue to drain throttled bios for
1165 *
1166 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1167 */
1168void blk_throtl_drain(struct request_queue *q)
1169 __releases(q->queue_lock) __acquires(q->queue_lock)
1170{
1171 struct throtl_data *td = q->td;
Tejun Heo0049af72013-05-14 13:52:33 -07001172 struct throtl_service_queue *parent_sq = &td->service_queue;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001173 struct throtl_grp *tg;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001174 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001175 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001176
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001177 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001178
Tejun Heo0049af72013-05-14 13:52:33 -07001179 while ((tg = throtl_rb_first(parent_sq))) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001180 struct throtl_service_queue *sq = &tg->service_queue;
1181
Tejun Heo0049af72013-05-14 13:52:33 -07001182 throtl_dequeue_tg(tg, parent_sq);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001183
Tejun Heo73f0d492013-05-14 13:52:35 -07001184 while ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo651930b2013-05-14 13:52:35 -07001185 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Tejun Heo73f0d492013-05-14 13:52:35 -07001186 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo651930b2013-05-14 13:52:35 -07001187 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001188 }
1189 spin_unlock_irq(q->queue_lock);
1190
Tejun Heo651930b2013-05-14 13:52:35 -07001191 for (rw = READ; rw <= WRITE; rw++)
1192 while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
1193 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001194
1195 spin_lock_irq(q->queue_lock);
1196}
1197
Vivek Goyale43473b2010-09-15 17:06:35 -04001198int blk_throtl_init(struct request_queue *q)
1199{
1200 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001201 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001202
1203 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1204 if (!td)
1205 return -ENOMEM;
1206
Tejun Heocb761992013-05-14 13:52:31 -07001207 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heo49a2f1e2013-05-14 13:52:34 -07001208 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001209
Tejun Heocd1604f2012-03-05 13:15:06 -08001210 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001211 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001212
Tejun Heoa2b16932012-04-13 13:11:33 -07001213 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001214 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001215 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001216 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001217 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001218}
1219
1220void blk_throtl_exit(struct request_queue *q)
1221{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001222 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001223 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001224 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001225 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001226}
1227
1228static int __init throtl_init(void)
1229{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001230 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1231 if (!kthrotld_workqueue)
1232 panic("Failed to create kthrotld\n");
1233
Tejun Heo3c798392012-04-16 13:57:25 -07001234 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001235}
1236
1237module_init(throtl_init);