blob: 00cfdd05f98f9f4ed98aa6907e36e9993a589b41 [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 Heo77216b02013-05-14 13:52:36 -070030 struct throtl_service_queue *parent_sq; /* the parent service_queue */
31
Tejun Heo73f0d492013-05-14 13:52:35 -070032 /*
33 * Bios queued directly to this service_queue or dispatched from
34 * children throtl_grp's.
35 */
36 struct bio_list bio_lists[2]; /* queued bios [READ/WRITE] */
37 unsigned int nr_queued[2]; /* number of queued bios */
38
39 /*
40 * RB tree of active children throtl_grp's, which are sorted by
41 * their ->disptime.
42 */
Tejun Heoc9e03322013-05-14 13:52:32 -070043 struct rb_root pending_tree; /* RB tree of active tgs */
44 struct rb_node *first_pending; /* first node in the tree */
45 unsigned int nr_pending; /* # queued in the tree */
46 unsigned long first_pending_disptime; /* disptime of the first tg */
Vivek Goyale43473b2010-09-15 17:06:35 -040047};
48
Tejun Heo5b2c16a2013-05-14 13:52:32 -070049enum tg_state_flags {
50 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070051 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070052};
53
Vivek Goyale43473b2010-09-15 17:06:35 -040054#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
55
Tejun Heo8a3d2612012-04-01 14:38:44 -070056/* Per-cpu group stats */
57struct tg_stats_cpu {
58 /* total bytes transferred */
59 struct blkg_rwstat service_bytes;
60 /* total IOs serviced, post merge */
61 struct blkg_rwstat serviced;
62};
63
Vivek Goyale43473b2010-09-15 17:06:35 -040064struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070065 /* must be the first member */
66 struct blkg_policy_data pd;
67
Tejun Heoc9e03322013-05-14 13:52:32 -070068 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040069 struct rb_node rb_node;
70
Tejun Heo0f3457f2013-05-14 13:52:32 -070071 /* throtl_data this group belongs to */
72 struct throtl_data *td;
73
Tejun Heo49a2f1e2013-05-14 13:52:34 -070074 /* this group's service queue */
75 struct throtl_service_queue service_queue;
76
Vivek Goyale43473b2010-09-15 17:06:35 -040077 /*
78 * Dispatch time in jiffies. This is the estimated time when group
79 * will unthrottle and is ready to dispatch more bio. It is used as
80 * key to sort active groups in service tree.
81 */
82 unsigned long disptime;
83
Vivek Goyale43473b2010-09-15 17:06:35 -040084 unsigned int flags;
85
Vivek Goyale43473b2010-09-15 17:06:35 -040086 /* bytes per second rate limits */
87 uint64_t bps[2];
88
Vivek Goyal8e89d132010-09-15 17:06:37 -040089 /* IOPS limits */
90 unsigned int iops[2];
91
Vivek Goyale43473b2010-09-15 17:06:35 -040092 /* Number of bytes disptached in current slice */
93 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040094 /* Number of bio's dispatched in current slice */
95 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040096
97 /* When did we start a new slice */
98 unsigned long slice_start[2];
99 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +0200100
Tejun Heo8a3d2612012-04-01 14:38:44 -0700101 /* Per cpu stats pointer */
102 struct tg_stats_cpu __percpu *stats_cpu;
103
104 /* List of tgs waiting for per cpu stats memory to be allocated */
105 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400106};
107
108struct throtl_data
109{
Vivek Goyale43473b2010-09-15 17:06:35 -0400110 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700111 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400112
Vivek Goyale43473b2010-09-15 17:06:35 -0400113 struct request_queue *queue;
114
115 /* Total Number of queued bios on READ and WRITE lists */
116 unsigned int nr_queued[2];
117
118 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200119 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400120 */
121 unsigned int nr_undestroyed_grps;
122
123 /* Work for dispatching throttled bios */
Tejun Heocb761992013-05-14 13:52:31 -0700124 struct delayed_work dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400125};
126
Tejun Heo8a3d2612012-04-01 14:38:44 -0700127/* list and work item to allocate percpu group stats */
128static DEFINE_SPINLOCK(tg_stats_alloc_lock);
129static LIST_HEAD(tg_stats_alloc_list);
130
131static void tg_stats_alloc_fn(struct work_struct *);
132static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
133
Tejun Heof95a04a2012-04-16 13:57:26 -0700134static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
135{
136 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
137}
138
Tejun Heo3c798392012-04-16 13:57:25 -0700139static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800140{
Tejun Heof95a04a2012-04-16 13:57:26 -0700141 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800142}
143
Tejun Heo3c798392012-04-16 13:57:25 -0700144static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800145{
Tejun Heof95a04a2012-04-16 13:57:26 -0700146 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800147}
148
Tejun Heo03d8e112012-04-13 13:11:32 -0700149static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
150{
151 return blkg_to_tg(td->queue->root_blkg);
152}
153
Tejun Heo0f3457f2013-05-14 13:52:32 -0700154#define throtl_log_tg(tg, fmt, args...) do { \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700155 char __pbuf[128]; \
156 \
157 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
Tejun Heo0f3457f2013-05-14 13:52:32 -0700158 blk_add_trace_msg((tg)->td->queue, "throtl %s " fmt, __pbuf, ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700159} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400160
161#define throtl_log(td, fmt, args...) \
162 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
163
Tejun Heo8a3d2612012-04-01 14:38:44 -0700164/*
165 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700166 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700167 * allocation.
168 */
169static void tg_stats_alloc_fn(struct work_struct *work)
170{
171 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
172 struct delayed_work *dwork = to_delayed_work(work);
173 bool empty = false;
174
175alloc_stats:
176 if (!stats_cpu) {
177 stats_cpu = alloc_percpu(struct tg_stats_cpu);
178 if (!stats_cpu) {
179 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700180 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700181 return;
182 }
183 }
184
185 spin_lock_irq(&tg_stats_alloc_lock);
186
187 if (!list_empty(&tg_stats_alloc_list)) {
188 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
189 struct throtl_grp,
190 stats_alloc_node);
191 swap(tg->stats_cpu, stats_cpu);
192 list_del_init(&tg->stats_alloc_node);
193 }
194
195 empty = list_empty(&tg_stats_alloc_list);
196 spin_unlock_irq(&tg_stats_alloc_lock);
197 if (!empty)
198 goto alloc_stats;
199}
200
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700201/* init a service_queue, assumes the caller zeroed it */
Tejun Heo77216b02013-05-14 13:52:36 -0700202static void throtl_service_queue_init(struct throtl_service_queue *sq,
203 struct throtl_service_queue *parent_sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700204{
Tejun Heo73f0d492013-05-14 13:52:35 -0700205 bio_list_init(&sq->bio_lists[0]);
206 bio_list_init(&sq->bio_lists[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700207 sq->pending_tree = RB_ROOT;
Tejun Heo77216b02013-05-14 13:52:36 -0700208 sq->parent_sq = parent_sq;
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700209}
210
Tejun Heo3c798392012-04-16 13:57:25 -0700211static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400212{
Tejun Heo03814112012-03-05 13:15:14 -0800213 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heo77216b02013-05-14 13:52:36 -0700214 struct throtl_data *td = blkg->q->td;
Tejun Heoff26eaa2012-05-23 12:16:21 +0200215 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800216
Tejun Heo77216b02013-05-14 13:52:36 -0700217 throtl_service_queue_init(&tg->service_queue, &td->service_queue);
Vivek Goyala29a1712011-05-19 15:38:19 -0400218 RB_CLEAR_NODE(&tg->rb_node);
Tejun Heo77216b02013-05-14 13:52:36 -0700219 tg->td = td;
Vivek Goyala29a1712011-05-19 15:38:19 -0400220
Tejun Heoe56da7e2012-03-05 13:15:07 -0800221 tg->bps[READ] = -1;
222 tg->bps[WRITE] = -1;
223 tg->iops[READ] = -1;
224 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700225
226 /*
227 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
228 * but percpu allocator can't be called from IO path. Queue tg on
229 * tg_stats_alloc_list and allocate from work item.
230 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200231 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700232 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700233 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200234 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700235}
236
Tejun Heo3c798392012-04-16 13:57:25 -0700237static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700238{
239 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200240 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700241
Tejun Heoff26eaa2012-05-23 12:16:21 +0200242 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700243 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200244 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700245
246 free_percpu(tg->stats_cpu);
247}
248
Tejun Heo3c798392012-04-16 13:57:25 -0700249static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700250{
251 struct throtl_grp *tg = blkg_to_tg(blkg);
252 int cpu;
253
254 if (tg->stats_cpu == NULL)
255 return;
256
257 for_each_possible_cpu(cpu) {
258 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
259
260 blkg_rwstat_reset(&sc->service_bytes);
261 blkg_rwstat_reset(&sc->serviced);
262 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400263}
264
Tejun Heo3c798392012-04-16 13:57:25 -0700265static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
266 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400267{
Vivek Goyale43473b2010-09-15 17:06:35 -0400268 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700269 * This is the common case when there are no blkcgs. Avoid lookup
270 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800271 */
Tejun Heo3c798392012-04-16 13:57:25 -0700272 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700273 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400274
Tejun Heoe8989fa2012-03-05 13:15:20 -0800275 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400276}
277
Tejun Heocd1604f2012-03-05 13:15:06 -0800278static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700279 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400280{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400281 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800282 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800283
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400284 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700285 * This is the common case when there are no blkcgs. Avoid lookup
286 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400287 */
Tejun Heo3c798392012-04-16 13:57:25 -0700288 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700289 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800290 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700291 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800292
Tejun Heo3c96cb32012-04-13 13:11:34 -0700293 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800294
295 /* if %NULL and @q is alive, fall back to root_tg */
296 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800297 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100298 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700299 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400300 }
301
Vivek Goyale43473b2010-09-15 17:06:35 -0400302 return tg;
303}
304
Tejun Heo0049af72013-05-14 13:52:33 -0700305static struct throtl_grp *
306throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400307{
308 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700309 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400310 return NULL;
311
Tejun Heo0049af72013-05-14 13:52:33 -0700312 if (!parent_sq->first_pending)
313 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400314
Tejun Heo0049af72013-05-14 13:52:33 -0700315 if (parent_sq->first_pending)
316 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400317
318 return NULL;
319}
320
321static void rb_erase_init(struct rb_node *n, struct rb_root *root)
322{
323 rb_erase(n, root);
324 RB_CLEAR_NODE(n);
325}
326
Tejun Heo0049af72013-05-14 13:52:33 -0700327static void throtl_rb_erase(struct rb_node *n,
328 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400329{
Tejun Heo0049af72013-05-14 13:52:33 -0700330 if (parent_sq->first_pending == n)
331 parent_sq->first_pending = NULL;
332 rb_erase_init(n, &parent_sq->pending_tree);
333 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400334}
335
Tejun Heo0049af72013-05-14 13:52:33 -0700336static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400337{
338 struct throtl_grp *tg;
339
Tejun Heo0049af72013-05-14 13:52:33 -0700340 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400341 if (!tg)
342 return;
343
Tejun Heo0049af72013-05-14 13:52:33 -0700344 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400345}
346
Tejun Heo77216b02013-05-14 13:52:36 -0700347static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400348{
Tejun Heo77216b02013-05-14 13:52:36 -0700349 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700350 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400351 struct rb_node *parent = NULL;
352 struct throtl_grp *__tg;
353 unsigned long key = tg->disptime;
354 int left = 1;
355
356 while (*node != NULL) {
357 parent = *node;
358 __tg = rb_entry_tg(parent);
359
360 if (time_before(key, __tg->disptime))
361 node = &parent->rb_left;
362 else {
363 node = &parent->rb_right;
364 left = 0;
365 }
366 }
367
368 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700369 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400370
371 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700372 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400373}
374
Tejun Heo77216b02013-05-14 13:52:36 -0700375static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400376{
Tejun Heo77216b02013-05-14 13:52:36 -0700377 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700378 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700379 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400380}
381
Tejun Heo77216b02013-05-14 13:52:36 -0700382static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400383{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700384 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700385 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400386}
387
Tejun Heo77216b02013-05-14 13:52:36 -0700388static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400389{
Tejun Heo77216b02013-05-14 13:52:36 -0700390 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700391 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400392}
393
Tejun Heo77216b02013-05-14 13:52:36 -0700394static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400395{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700396 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700397 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400398}
399
Tejun Heoa9131a22013-05-14 13:52:31 -0700400/* Call with queue lock held */
401static void throtl_schedule_delayed_work(struct throtl_data *td,
402 unsigned long delay)
403{
404 struct delayed_work *dwork = &td->dispatch_work;
405
Tejun Heo6a525602013-05-14 13:52:32 -0700406 mod_delayed_work(kthrotld_workqueue, dwork, delay);
407 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700408}
409
Vivek Goyale43473b2010-09-15 17:06:35 -0400410static void throtl_schedule_next_dispatch(struct throtl_data *td)
411{
Tejun Heoc9e03322013-05-14 13:52:32 -0700412 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400413
Tejun Heo6a525602013-05-14 13:52:32 -0700414 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700415 if (!sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400416 return;
417
Tejun Heoc9e03322013-05-14 13:52:32 -0700418 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400419
Tejun Heoc9e03322013-05-14 13:52:32 -0700420 if (time_before_eq(sq->first_pending_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500421 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400422 else
Tejun Heoc9e03322013-05-14 13:52:32 -0700423 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400424}
425
Tejun Heo0f3457f2013-05-14 13:52:32 -0700426static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400427{
428 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400429 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400430 tg->slice_start[rw] = jiffies;
431 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heo0f3457f2013-05-14 13:52:32 -0700432 throtl_log_tg(tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400433 rw == READ ? 'R' : 'W', tg->slice_start[rw],
434 tg->slice_end[rw], jiffies);
435}
436
Tejun Heo0f3457f2013-05-14 13:52:32 -0700437static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
438 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100439{
440 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
441}
442
Tejun Heo0f3457f2013-05-14 13:52:32 -0700443static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
444 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400445{
446 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heo0f3457f2013-05-14 13:52:32 -0700447 throtl_log_tg(tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400448 rw == READ ? 'R' : 'W', tg->slice_start[rw],
449 tg->slice_end[rw], jiffies);
450}
451
452/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700453static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400454{
455 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
456 return 0;
457
458 return 1;
459}
460
461/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700462static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400463{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200464 unsigned long nr_slices, time_elapsed, io_trim;
465 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400466
467 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
468
469 /*
470 * If bps are unlimited (-1), then time slice don't get
471 * renewed. Don't try to trim the slice if slice is used. A new
472 * slice will start when appropriate.
473 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700474 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400475 return;
476
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100477 /*
478 * A bio has been dispatched. Also adjust slice_end. It might happen
479 * that initially cgroup limit was very low resulting in high
480 * slice_end, but later limit was bumped up and bio was dispached
481 * sooner, then we need to reduce slice_end. A high bogus slice_end
482 * is bad because it does not allow new slice to start.
483 */
484
Tejun Heo0f3457f2013-05-14 13:52:32 -0700485 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100486
Vivek Goyale43473b2010-09-15 17:06:35 -0400487 time_elapsed = jiffies - tg->slice_start[rw];
488
489 nr_slices = time_elapsed / throtl_slice;
490
491 if (!nr_slices)
492 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200493 tmp = tg->bps[rw] * throtl_slice * nr_slices;
494 do_div(tmp, HZ);
495 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400496
Vivek Goyal8e89d132010-09-15 17:06:37 -0400497 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400498
Vivek Goyal8e89d132010-09-15 17:06:37 -0400499 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400500 return;
501
502 if (tg->bytes_disp[rw] >= bytes_trim)
503 tg->bytes_disp[rw] -= bytes_trim;
504 else
505 tg->bytes_disp[rw] = 0;
506
Vivek Goyal8e89d132010-09-15 17:06:37 -0400507 if (tg->io_disp[rw] >= io_trim)
508 tg->io_disp[rw] -= io_trim;
509 else
510 tg->io_disp[rw] = 0;
511
Vivek Goyale43473b2010-09-15 17:06:35 -0400512 tg->slice_start[rw] += nr_slices * throtl_slice;
513
Tejun Heo0f3457f2013-05-14 13:52:32 -0700514 throtl_log_tg(tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400515 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400516 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400517 tg->slice_start[rw], tg->slice_end[rw], jiffies);
518}
519
Tejun Heo0f3457f2013-05-14 13:52:32 -0700520static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
521 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400522{
523 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400524 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400525 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200526 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400527
Vivek Goyal8e89d132010-09-15 17:06:37 -0400528 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400529
Vivek Goyal8e89d132010-09-15 17:06:37 -0400530 /* Slice has just started. Consider one slice interval */
531 if (!jiffy_elapsed)
532 jiffy_elapsed_rnd = throtl_slice;
533
534 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
535
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200536 /*
537 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
538 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
539 * will allow dispatch after 1 second and after that slice should
540 * have been trimmed.
541 */
542
543 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
544 do_div(tmp, HZ);
545
546 if (tmp > UINT_MAX)
547 io_allowed = UINT_MAX;
548 else
549 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400550
551 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400552 if (wait)
553 *wait = 0;
554 return 1;
555 }
556
Vivek Goyal8e89d132010-09-15 17:06:37 -0400557 /* Calc approx time to dispatch */
558 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
559
560 if (jiffy_wait > jiffy_elapsed)
561 jiffy_wait = jiffy_wait - jiffy_elapsed;
562 else
563 jiffy_wait = 1;
564
565 if (wait)
566 *wait = jiffy_wait;
567 return 0;
568}
569
Tejun Heo0f3457f2013-05-14 13:52:32 -0700570static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
571 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400572{
573 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200574 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400575 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400576
577 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
578
579 /* Slice has just started. Consider one slice interval */
580 if (!jiffy_elapsed)
581 jiffy_elapsed_rnd = throtl_slice;
582
583 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
584
Vivek Goyal5e901a22010-10-01 21:16:38 +0200585 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
586 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200587 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400588
589 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
590 if (wait)
591 *wait = 0;
592 return 1;
593 }
594
595 /* Calc approx time to dispatch */
596 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
597 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
598
599 if (!jiffy_wait)
600 jiffy_wait = 1;
601
602 /*
603 * This wait time is without taking into consideration the rounding
604 * up we did. Add that time also.
605 */
606 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400607 if (wait)
608 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400609 return 0;
610}
Vivek Goyale43473b2010-09-15 17:06:35 -0400611
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400612static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
613 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
614 return 1;
615 return 0;
616}
617
Vivek Goyal8e89d132010-09-15 17:06:37 -0400618/*
619 * Returns whether one can dispatch a bio or not. Also returns approx number
620 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
621 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700622static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
623 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400624{
625 bool rw = bio_data_dir(bio);
626 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
627
628 /*
629 * Currently whole state machine of group depends on first bio
630 * queued in the group bio list. So one should not be calling
631 * this function with a different bio if there are other bios
632 * queued.
633 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700634 BUG_ON(tg->service_queue.nr_queued[rw] &&
635 bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400636
637 /* If tg->bps = -1, then BW is unlimited */
638 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
639 if (wait)
640 *wait = 0;
641 return 1;
642 }
643
644 /*
645 * If previous slice expired, start a new one otherwise renew/extend
646 * existing slice to make sure it is at least throtl_slice interval
647 * long since now.
648 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700649 if (throtl_slice_used(tg, rw))
650 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400651 else {
652 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700653 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400654 }
655
Tejun Heo0f3457f2013-05-14 13:52:32 -0700656 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
657 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400658 if (wait)
659 *wait = 0;
660 return 1;
661 }
662
663 max_wait = max(bps_wait, iops_wait);
664
665 if (wait)
666 *wait = max_wait;
667
668 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700669 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400670
671 return 0;
672}
673
Tejun Heo3c798392012-04-16 13:57:25 -0700674static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700675 int rw)
676{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700677 struct throtl_grp *tg = blkg_to_tg(blkg);
678 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700679 unsigned long flags;
680
681 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700682 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700683 return;
684
685 /*
686 * Disabling interrupts to provide mutual exclusion between two
687 * writes on same cpu. It probably is not needed for 64bit. Not
688 * optimizing that case yet.
689 */
690 local_irq_save(flags);
691
Tejun Heo8a3d2612012-04-01 14:38:44 -0700692 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700693
Tejun Heo629ed0b2012-04-01 14:38:44 -0700694 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
695 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
696
697 local_irq_restore(flags);
698}
699
Vivek Goyale43473b2010-09-15 17:06:35 -0400700static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
701{
702 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400703
704 /* Charge the bio to the group */
705 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400706 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400707
Tejun Heo629ed0b2012-04-01 14:38:44 -0700708 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400709}
710
Tejun Heo77216b02013-05-14 13:52:36 -0700711static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg)
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 Heo77216b02013-05-14 13:52:36 -0700730 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400731}
732
Tejun Heo77216b02013-05-14 13:52:36 -0700733static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400734{
Tejun Heo73f0d492013-05-14 13:52:35 -0700735 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400736 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
737 struct bio *bio;
738
Tejun Heo73f0d492013-05-14 13:52:35 -0700739 if ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700740 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400741
Tejun Heo73f0d492013-05-14 13:52:35 -0700742 if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700743 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400744
745 min_wait = min(read_wait, write_wait);
746 disptime = jiffies + min_wait;
747
Vivek Goyale43473b2010-09-15 17:06:35 -0400748 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700749 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400750 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700751 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700752
753 /* see throtl_add_bio_tg() */
754 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400755}
756
Tejun Heo77216b02013-05-14 13:52:36 -0700757static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400758{
Tejun Heo73f0d492013-05-14 13:52:35 -0700759 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400760 struct bio *bio;
761
Tejun Heo73f0d492013-05-14 13:52:35 -0700762 bio = bio_list_pop(&sq->bio_lists[rw]);
763 sq->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800764 /* Drop bio reference on blkg */
765 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400766
Tejun Heo0f3457f2013-05-14 13:52:32 -0700767 BUG_ON(tg->td->nr_queued[rw] <= 0);
768 tg->td->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400769
770 throtl_charge_bio(tg, bio);
Tejun Heo77216b02013-05-14 13:52:36 -0700771 bio_list_add(&sq->parent_sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400772 bio->bi_rw |= REQ_THROTTLED;
773
Tejun Heo0f3457f2013-05-14 13:52:32 -0700774 throtl_trim_slice(tg, rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400775}
776
Tejun Heo77216b02013-05-14 13:52:36 -0700777static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400778{
Tejun Heo73f0d492013-05-14 13:52:35 -0700779 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400780 unsigned int nr_reads = 0, nr_writes = 0;
781 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100782 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400783 struct bio *bio;
784
785 /* Try to dispatch 75% READS and 25% WRITES */
786
Tejun Heo73f0d492013-05-14 13:52:35 -0700787 while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700788 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400789
Tejun Heo77216b02013-05-14 13:52:36 -0700790 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400791 nr_reads++;
792
793 if (nr_reads >= max_nr_reads)
794 break;
795 }
796
Tejun Heo73f0d492013-05-14 13:52:35 -0700797 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700798 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400799
Tejun Heo77216b02013-05-14 13:52:36 -0700800 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -0400801 nr_writes++;
802
803 if (nr_writes >= max_nr_writes)
804 break;
805 }
806
807 return nr_reads + nr_writes;
808}
809
Tejun Heo651930b2013-05-14 13:52:35 -0700810static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400811{
812 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400813
814 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -0700815 struct throtl_grp *tg = throtl_rb_first(parent_sq);
816 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400817
818 if (!tg)
819 break;
820
821 if (time_before(jiffies, tg->disptime))
822 break;
823
Tejun Heo77216b02013-05-14 13:52:36 -0700824 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400825
Tejun Heo77216b02013-05-14 13:52:36 -0700826 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400827
Tejun Heo73f0d492013-05-14 13:52:35 -0700828 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -0700829 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400830
831 if (nr_disp >= throtl_quantum)
832 break;
833 }
834
835 return nr_disp;
836}
837
Tejun Heocb761992013-05-14 13:52:31 -0700838/* work function to dispatch throttled bios */
839void blk_throtl_dispatch_work_fn(struct work_struct *work)
Vivek Goyale43473b2010-09-15 17:06:35 -0400840{
Tejun Heocb761992013-05-14 13:52:31 -0700841 struct throtl_data *td = container_of(to_delayed_work(work),
842 struct throtl_data, dispatch_work);
Tejun Heo651930b2013-05-14 13:52:35 -0700843 struct throtl_service_queue *sq = &td->service_queue;
Tejun Heocb761992013-05-14 13:52:31 -0700844 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400845 unsigned int nr_disp = 0;
846 struct bio_list bio_list_on_stack;
847 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100848 struct blk_plug plug;
Tejun Heo651930b2013-05-14 13:52:35 -0700849 int rw;
Vivek Goyale43473b2010-09-15 17:06:35 -0400850
851 spin_lock_irq(q->queue_lock);
852
Vivek Goyale43473b2010-09-15 17:06:35 -0400853 bio_list_init(&bio_list_on_stack);
854
Joe Perchesd2f31a52011-06-13 20:19:27 +0200855 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo6a525602013-05-14 13:52:32 -0700856 td->nr_queued[READ] + td->nr_queued[WRITE],
857 td->nr_queued[READ], td->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400858
Tejun Heo651930b2013-05-14 13:52:35 -0700859 nr_disp = throtl_select_dispatch(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400860
Tejun Heo651930b2013-05-14 13:52:35 -0700861 if (nr_disp) {
862 for (rw = READ; rw <= WRITE; rw++) {
863 bio_list_merge(&bio_list_on_stack, &sq->bio_lists[rw]);
864 bio_list_init(&sq->bio_lists[rw]);
865 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400866 throtl_log(td, "bios disp=%u", nr_disp);
Tejun Heo651930b2013-05-14 13:52:35 -0700867 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400868
869 throtl_schedule_next_dispatch(td);
Tejun Heo6a525602013-05-14 13:52:32 -0700870
Vivek Goyale43473b2010-09-15 17:06:35 -0400871 spin_unlock_irq(q->queue_lock);
872
873 /*
874 * If we dispatched some requests, unplug the queue to make sure
875 * immediate dispatch
876 */
877 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100878 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400879 while((bio = bio_list_pop(&bio_list_on_stack)))
880 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100881 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400882 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400883}
884
Tejun Heof95a04a2012-04-16 13:57:26 -0700885static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
886 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700887{
Tejun Heof95a04a2012-04-16 13:57:26 -0700888 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -0700889 struct blkg_rwstat rwstat = { }, tmp;
890 int i, cpu;
891
892 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700893 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700894
895 tmp = blkg_rwstat_read((void *)sc + off);
896 for (i = 0; i < BLKG_RWSTAT_NR; i++)
897 rwstat.cnt[i] += tmp.cnt[i];
898 }
899
Tejun Heof95a04a2012-04-16 13:57:26 -0700900 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700901}
902
Tejun Heo8a3d2612012-04-01 14:38:44 -0700903static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
904 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700905{
Tejun Heo3c798392012-04-16 13:57:25 -0700906 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -0700907
Tejun Heo3c798392012-04-16 13:57:25 -0700908 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700909 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700910 return 0;
911}
912
Tejun Heof95a04a2012-04-16 13:57:26 -0700913static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
914 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700915{
Tejun Heof95a04a2012-04-16 13:57:26 -0700916 struct throtl_grp *tg = pd_to_tg(pd);
917 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700918
Tejun Heoaf133ce2012-04-01 14:38:44 -0700919 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700920 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700921 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700922}
923
Tejun Heof95a04a2012-04-16 13:57:26 -0700924static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
925 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700926{
Tejun Heof95a04a2012-04-16 13:57:26 -0700927 struct throtl_grp *tg = pd_to_tg(pd);
928 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700929
930 if (v == -1)
931 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700932 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700933}
934
935static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
936 struct seq_file *sf)
937{
Tejun Heo3c798392012-04-16 13:57:25 -0700938 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
939 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700940 return 0;
941}
942
Tejun Heoaf133ce2012-04-01 14:38:44 -0700943static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
944 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400945{
Tejun Heo3c798392012-04-16 13:57:25 -0700946 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
947 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700948 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400949}
950
Tejun Heoaf133ce2012-04-01 14:38:44 -0700951static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
952 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700953{
Tejun Heo3c798392012-04-16 13:57:25 -0700954 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700955 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700956 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700957 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700958 int ret;
959
Tejun Heo3c798392012-04-16 13:57:25 -0700960 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700961 if (ret)
962 return ret;
963
Tejun Heoaf133ce2012-04-01 14:38:44 -0700964 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700965 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700966
Tejun Heoa2b16932012-04-13 13:11:33 -0700967 if (!ctx.v)
968 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700969
Tejun Heoa2b16932012-04-13 13:11:33 -0700970 if (is_u64)
971 *(u64 *)((void *)tg + cft->private) = ctx.v;
972 else
973 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700974
Tejun Heo0f3457f2013-05-14 13:52:32 -0700975 throtl_log_tg(tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Tejun Heo632b4492013-05-14 13:52:31 -0700976 tg->bps[READ], tg->bps[WRITE],
977 tg->iops[READ], tg->iops[WRITE]);
978
979 /*
980 * We're already holding queue_lock and know @tg is valid. Let's
981 * apply the new config directly.
982 *
983 * Restart the slices for both READ and WRITES. It might happen
984 * that a group's limit are dropped suddenly and we don't want to
985 * account recently dispatched IO with new low rate.
986 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700987 throtl_start_new_slice(tg, 0);
988 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -0700989
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700990 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -0700991 tg_update_disptime(tg);
Tejun Heo632b4492013-05-14 13:52:31 -0700992 throtl_schedule_next_dispatch(td);
993 }
Tejun Heo60c2bc22012-04-01 14:38:43 -0700994
995 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -0700996 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700997}
998
Tejun Heoaf133ce2012-04-01 14:38:44 -0700999static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
1000 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001001{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001002 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001003}
1004
Tejun Heoaf133ce2012-04-01 14:38:44 -07001005static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1006 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001007{
Tejun Heoaf133ce2012-04-01 14:38:44 -07001008 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001009}
1010
1011static struct cftype throtl_files[] = {
1012 {
1013 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001014 .private = offsetof(struct throtl_grp, bps[READ]),
1015 .read_seq_string = tg_print_conf_u64,
1016 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001017 .max_write_len = 256,
1018 },
1019 {
1020 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001021 .private = offsetof(struct throtl_grp, bps[WRITE]),
1022 .read_seq_string = tg_print_conf_u64,
1023 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001024 .max_write_len = 256,
1025 },
1026 {
1027 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001028 .private = offsetof(struct throtl_grp, iops[READ]),
1029 .read_seq_string = tg_print_conf_uint,
1030 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001031 .max_write_len = 256,
1032 },
1033 {
1034 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001035 .private = offsetof(struct throtl_grp, iops[WRITE]),
1036 .read_seq_string = tg_print_conf_uint,
1037 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001038 .max_write_len = 256,
1039 },
1040 {
1041 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001042 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001043 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001044 },
1045 {
1046 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001047 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001048 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001049 },
1050 { } /* terminate */
1051};
1052
Vivek Goyalda527772011-03-02 19:05:33 -05001053static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001054{
1055 struct throtl_data *td = q->td;
1056
Tejun Heocb761992013-05-14 13:52:31 -07001057 cancel_delayed_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001058}
1059
Tejun Heo3c798392012-04-16 13:57:25 -07001060static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001061 .pd_size = sizeof(struct throtl_grp),
1062 .cftypes = throtl_files,
1063
1064 .pd_init_fn = throtl_pd_init,
1065 .pd_exit_fn = throtl_pd_exit,
1066 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001067};
1068
Tejun Heobc16a4f2011-10-19 14:33:01 +02001069bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001070{
1071 struct throtl_data *td = q->td;
1072 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001073 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001074 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001075 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001076 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001077
1078 if (bio->bi_rw & REQ_THROTTLED) {
1079 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001080 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001081 }
1082
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001083 /*
1084 * A throtl_grp pointer retrieved under rcu can be used to access
1085 * basic fields like stats and io rates. If a group has no rules,
1086 * just update the dispatch stats in lockless manner and return.
1087 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001088 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001089 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001090 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001091 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001092 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001093 throtl_update_dispatch_stats(tg_to_blkg(tg),
1094 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001095 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001096 }
1097 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001098
1099 /*
1100 * Either group has not been allocated yet or it is not an unlimited
1101 * IO group
1102 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001103 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001104 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001105 if (unlikely(!tg))
1106 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001107
Tejun Heo73f0d492013-05-14 13:52:35 -07001108 sq = &tg->service_queue;
1109
Tejun Heo0e9f4162013-05-14 13:52:35 -07001110 /* throtl is FIFO - if other bios are already queued, should queue */
1111 if (sq->nr_queued[rw])
Vivek Goyale43473b2010-09-15 17:06:35 -04001112 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001113
Vivek Goyale43473b2010-09-15 17:06:35 -04001114 /* Bio is with-in rate limit of group */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001115 if (tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001116 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001117
1118 /*
1119 * We need to trim slice even when bios are not being queued
1120 * otherwise it might happen that a bio is not queued for
1121 * a long time and slice keeps on extending and trim is not
1122 * called for a long time. Now if limits are reduced suddenly
1123 * we take into account all the IO dispatched so far at new
1124 * low rate and * newly queued IO gets a really long dispatch
1125 * time.
1126 *
1127 * So keep on trimming slice even if bio is not queued.
1128 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001129 throtl_trim_slice(tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001130 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001131 }
1132
1133queue_bio:
Tejun Heo0f3457f2013-05-14 13:52:32 -07001134 throtl_log_tg(tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001135 " iodisp=%u iops=%u queued=%d/%d",
1136 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001137 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001138 tg->io_disp[rw], tg->iops[rw],
Tejun Heo73f0d492013-05-14 13:52:35 -07001139 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001140
Tejun Heo671058f2012-03-05 13:15:29 -08001141 bio_associate_current(bio);
Tejun Heo77216b02013-05-14 13:52:36 -07001142 throtl_add_bio_tg(bio, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001143 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001144
Tejun Heo0e9f4162013-05-14 13:52:35 -07001145 /* update @tg's dispatch time if @tg was empty before @bio */
1146 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001147 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001148 throtl_schedule_next_dispatch(td);
1149 }
1150
Tejun Heobc16a4f2011-10-19 14:33:01 +02001151out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001152 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001153out_unlock_rcu:
1154 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001155out:
1156 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001157}
1158
Tejun Heoc9a929d2011-10-19 14:42:16 +02001159/**
1160 * blk_throtl_drain - drain throttled bios
1161 * @q: request_queue to drain throttled bios for
1162 *
1163 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1164 */
1165void blk_throtl_drain(struct request_queue *q)
1166 __releases(q->queue_lock) __acquires(q->queue_lock)
1167{
1168 struct throtl_data *td = q->td;
Tejun Heo0049af72013-05-14 13:52:33 -07001169 struct throtl_service_queue *parent_sq = &td->service_queue;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001170 struct throtl_grp *tg;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001171 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001172 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001173
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001174 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001175
Tejun Heo0049af72013-05-14 13:52:33 -07001176 while ((tg = throtl_rb_first(parent_sq))) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001177 struct throtl_service_queue *sq = &tg->service_queue;
1178
Tejun Heo77216b02013-05-14 13:52:36 -07001179 throtl_dequeue_tg(tg);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001180
Tejun Heo73f0d492013-05-14 13:52:35 -07001181 while ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo77216b02013-05-14 13:52:36 -07001182 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heo73f0d492013-05-14 13:52:35 -07001183 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo77216b02013-05-14 13:52:36 -07001184 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc9a929d2011-10-19 14:42:16 +02001185 }
1186 spin_unlock_irq(q->queue_lock);
1187
Tejun Heo651930b2013-05-14 13:52:35 -07001188 for (rw = READ; rw <= WRITE; rw++)
1189 while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
1190 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001191
1192 spin_lock_irq(q->queue_lock);
1193}
1194
Vivek Goyale43473b2010-09-15 17:06:35 -04001195int blk_throtl_init(struct request_queue *q)
1196{
1197 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001198 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001199
1200 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1201 if (!td)
1202 return -ENOMEM;
1203
Tejun Heocb761992013-05-14 13:52:31 -07001204 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heo77216b02013-05-14 13:52:36 -07001205 throtl_service_queue_init(&td->service_queue, NULL);
Vivek Goyale43473b2010-09-15 17:06:35 -04001206
Tejun Heocd1604f2012-03-05 13:15:06 -08001207 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001208 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001209
Tejun Heoa2b16932012-04-13 13:11:33 -07001210 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001211 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001212 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001213 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001214 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001215}
1216
1217void blk_throtl_exit(struct request_queue *q)
1218{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001219 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001220 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001221 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001222 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001223}
1224
1225static int __init throtl_init(void)
1226{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001227 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1228 if (!kthrotld_workqueue)
1229 panic("Failed to create kthrotld\n");
1230
Tejun Heo3c798392012-04-16 13:57:25 -07001231 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001232}
1233
1234module_init(throtl_init);