blob: 154bd63719c5c0da4c91ab49eec8b244bf7545c9 [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 */
49};
50
Vivek Goyale43473b2010-09-15 17:06:35 -040051#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
52
Tejun Heo8a3d2612012-04-01 14:38:44 -070053/* Per-cpu group stats */
54struct tg_stats_cpu {
55 /* total bytes transferred */
56 struct blkg_rwstat service_bytes;
57 /* total IOs serviced, post merge */
58 struct blkg_rwstat serviced;
59};
60
Vivek Goyale43473b2010-09-15 17:06:35 -040061struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070062 /* must be the first member */
63 struct blkg_policy_data pd;
64
Tejun Heoc9e03322013-05-14 13:52:32 -070065 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040066 struct rb_node rb_node;
67
Tejun Heo0f3457f2013-05-14 13:52:32 -070068 /* throtl_data this group belongs to */
69 struct throtl_data *td;
70
Tejun Heo49a2f1e2013-05-14 13:52:34 -070071 /* this group's service queue */
72 struct throtl_service_queue service_queue;
73
Vivek Goyale43473b2010-09-15 17:06:35 -040074 /*
75 * Dispatch time in jiffies. This is the estimated time when group
76 * will unthrottle and is ready to dispatch more bio. It is used as
77 * key to sort active groups in service tree.
78 */
79 unsigned long disptime;
80
Vivek Goyale43473b2010-09-15 17:06:35 -040081 unsigned int flags;
82
Vivek Goyale43473b2010-09-15 17:06:35 -040083 /* bytes per second rate limits */
84 uint64_t bps[2];
85
Vivek Goyal8e89d132010-09-15 17:06:37 -040086 /* IOPS limits */
87 unsigned int iops[2];
88
Vivek Goyale43473b2010-09-15 17:06:35 -040089 /* Number of bytes disptached in current slice */
90 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -040091 /* Number of bio's dispatched in current slice */
92 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -040093
94 /* When did we start a new slice */
95 unsigned long slice_start[2];
96 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +020097
Tejun Heo8a3d2612012-04-01 14:38:44 -070098 /* Per cpu stats pointer */
99 struct tg_stats_cpu __percpu *stats_cpu;
100
101 /* List of tgs waiting for per cpu stats memory to be allocated */
102 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400103};
104
105struct throtl_data
106{
Vivek Goyale43473b2010-09-15 17:06:35 -0400107 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700108 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400109
Vivek Goyale43473b2010-09-15 17:06:35 -0400110 struct request_queue *queue;
111
112 /* Total Number of queued bios on READ and WRITE lists */
113 unsigned int nr_queued[2];
114
115 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200116 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400117 */
118 unsigned int nr_undestroyed_grps;
119
120 /* Work for dispatching throttled bios */
Tejun Heocb761992013-05-14 13:52:31 -0700121 struct delayed_work dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400122};
123
Tejun Heo8a3d2612012-04-01 14:38:44 -0700124/* list and work item to allocate percpu group stats */
125static DEFINE_SPINLOCK(tg_stats_alloc_lock);
126static LIST_HEAD(tg_stats_alloc_list);
127
128static void tg_stats_alloc_fn(struct work_struct *);
129static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
130
Tejun Heof95a04a2012-04-16 13:57:26 -0700131static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
132{
133 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
134}
135
Tejun Heo3c798392012-04-16 13:57:25 -0700136static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800137{
Tejun Heof95a04a2012-04-16 13:57:26 -0700138 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800139}
140
Tejun Heo3c798392012-04-16 13:57:25 -0700141static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800142{
Tejun Heof95a04a2012-04-16 13:57:26 -0700143 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800144}
145
Tejun Heo03d8e112012-04-13 13:11:32 -0700146static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
147{
148 return blkg_to_tg(td->queue->root_blkg);
149}
150
Tejun Heo0f3457f2013-05-14 13:52:32 -0700151#define throtl_log_tg(tg, fmt, args...) do { \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700152 char __pbuf[128]; \
153 \
154 blkg_path(tg_to_blkg(tg), __pbuf, sizeof(__pbuf)); \
Tejun Heo0f3457f2013-05-14 13:52:32 -0700155 blk_add_trace_msg((tg)->td->queue, "throtl %s " fmt, __pbuf, ##args); \
Tejun Heo54e7ed12012-04-16 13:57:23 -0700156} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400157
158#define throtl_log(td, fmt, args...) \
159 blk_add_trace_msg((td)->queue, "throtl " fmt, ##args)
160
Tejun Heo8a3d2612012-04-01 14:38:44 -0700161/*
162 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700163 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700164 * allocation.
165 */
166static void tg_stats_alloc_fn(struct work_struct *work)
167{
168 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
169 struct delayed_work *dwork = to_delayed_work(work);
170 bool empty = false;
171
172alloc_stats:
173 if (!stats_cpu) {
174 stats_cpu = alloc_percpu(struct tg_stats_cpu);
175 if (!stats_cpu) {
176 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700177 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700178 return;
179 }
180 }
181
182 spin_lock_irq(&tg_stats_alloc_lock);
183
184 if (!list_empty(&tg_stats_alloc_list)) {
185 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
186 struct throtl_grp,
187 stats_alloc_node);
188 swap(tg->stats_cpu, stats_cpu);
189 list_del_init(&tg->stats_alloc_node);
190 }
191
192 empty = list_empty(&tg_stats_alloc_list);
193 spin_unlock_irq(&tg_stats_alloc_lock);
194 if (!empty)
195 goto alloc_stats;
196}
197
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700198/* init a service_queue, assumes the caller zeroed it */
199static void throtl_service_queue_init(struct throtl_service_queue *sq)
200{
Tejun Heo73f0d492013-05-14 13:52:35 -0700201 bio_list_init(&sq->bio_lists[0]);
202 bio_list_init(&sq->bio_lists[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700203 sq->pending_tree = RB_ROOT;
204}
205
Tejun Heo3c798392012-04-16 13:57:25 -0700206static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400207{
Tejun Heo03814112012-03-05 13:15:14 -0800208 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200209 unsigned long flags;
Tejun Heocd1604f2012-03-05 13:15:06 -0800210
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700211 throtl_service_queue_init(&tg->service_queue);
Vivek Goyala29a1712011-05-19 15:38:19 -0400212 RB_CLEAR_NODE(&tg->rb_node);
Tejun Heo0f3457f2013-05-14 13:52:32 -0700213 tg->td = blkg->q->td;
Vivek Goyala29a1712011-05-19 15:38:19 -0400214
Tejun Heoe56da7e2012-03-05 13:15:07 -0800215 tg->bps[READ] = -1;
216 tg->bps[WRITE] = -1;
217 tg->iops[READ] = -1;
218 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700219
220 /*
221 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
222 * but percpu allocator can't be called from IO path. Queue tg on
223 * tg_stats_alloc_list and allocate from work item.
224 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200225 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700226 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700227 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200228 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700229}
230
Tejun Heo3c798392012-04-16 13:57:25 -0700231static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700232{
233 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200234 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700235
Tejun Heoff26eaa2012-05-23 12:16:21 +0200236 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700237 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200238 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700239
240 free_percpu(tg->stats_cpu);
241}
242
Tejun Heo3c798392012-04-16 13:57:25 -0700243static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700244{
245 struct throtl_grp *tg = blkg_to_tg(blkg);
246 int cpu;
247
248 if (tg->stats_cpu == NULL)
249 return;
250
251 for_each_possible_cpu(cpu) {
252 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
253
254 blkg_rwstat_reset(&sc->service_bytes);
255 blkg_rwstat_reset(&sc->serviced);
256 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400257}
258
Tejun Heo3c798392012-04-16 13:57:25 -0700259static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
260 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400261{
Vivek Goyale43473b2010-09-15 17:06:35 -0400262 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700263 * This is the common case when there are no blkcgs. Avoid lookup
264 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800265 */
Tejun Heo3c798392012-04-16 13:57:25 -0700266 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700267 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400268
Tejun Heoe8989fa2012-03-05 13:15:20 -0800269 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400270}
271
Tejun Heocd1604f2012-03-05 13:15:06 -0800272static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700273 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400274{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400275 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800276 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800277
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400278 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700279 * This is the common case when there are no blkcgs. Avoid lookup
280 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400281 */
Tejun Heo3c798392012-04-16 13:57:25 -0700282 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700283 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800284 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700285 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800286
Tejun Heo3c96cb32012-04-13 13:11:34 -0700287 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800288
289 /* if %NULL and @q is alive, fall back to root_tg */
290 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800291 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100292 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700293 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400294 }
295
Vivek Goyale43473b2010-09-15 17:06:35 -0400296 return tg;
297}
298
Tejun Heo0049af72013-05-14 13:52:33 -0700299static struct throtl_grp *
300throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400301{
302 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700303 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400304 return NULL;
305
Tejun Heo0049af72013-05-14 13:52:33 -0700306 if (!parent_sq->first_pending)
307 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400308
Tejun Heo0049af72013-05-14 13:52:33 -0700309 if (parent_sq->first_pending)
310 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400311
312 return NULL;
313}
314
315static void rb_erase_init(struct rb_node *n, struct rb_root *root)
316{
317 rb_erase(n, root);
318 RB_CLEAR_NODE(n);
319}
320
Tejun Heo0049af72013-05-14 13:52:33 -0700321static void throtl_rb_erase(struct rb_node *n,
322 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400323{
Tejun Heo0049af72013-05-14 13:52:33 -0700324 if (parent_sq->first_pending == n)
325 parent_sq->first_pending = NULL;
326 rb_erase_init(n, &parent_sq->pending_tree);
327 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400328}
329
Tejun Heo0049af72013-05-14 13:52:33 -0700330static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400331{
332 struct throtl_grp *tg;
333
Tejun Heo0049af72013-05-14 13:52:33 -0700334 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400335 if (!tg)
336 return;
337
Tejun Heo0049af72013-05-14 13:52:33 -0700338 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400339}
340
Tejun Heo0049af72013-05-14 13:52:33 -0700341static void tg_service_queue_add(struct throtl_grp *tg,
342 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400343{
Tejun Heo0049af72013-05-14 13:52:33 -0700344 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400345 struct rb_node *parent = NULL;
346 struct throtl_grp *__tg;
347 unsigned long key = tg->disptime;
348 int left = 1;
349
350 while (*node != NULL) {
351 parent = *node;
352 __tg = rb_entry_tg(parent);
353
354 if (time_before(key, __tg->disptime))
355 node = &parent->rb_left;
356 else {
357 node = &parent->rb_right;
358 left = 0;
359 }
360 }
361
362 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700363 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400364
365 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700366 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400367}
368
Tejun Heo0049af72013-05-14 13:52:33 -0700369static void __throtl_enqueue_tg(struct throtl_grp *tg,
370 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400371{
Tejun Heo0049af72013-05-14 13:52:33 -0700372 tg_service_queue_add(tg, parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700373 tg->flags |= THROTL_TG_PENDING;
Tejun Heo0049af72013-05-14 13:52:33 -0700374 parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400375}
376
Tejun Heo0049af72013-05-14 13:52:33 -0700377static void throtl_enqueue_tg(struct throtl_grp *tg,
378 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400379{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700380 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo0049af72013-05-14 13:52:33 -0700381 __throtl_enqueue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400382}
383
Tejun Heo0049af72013-05-14 13:52:33 -0700384static void __throtl_dequeue_tg(struct throtl_grp *tg,
385 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400386{
Tejun Heo0049af72013-05-14 13:52:33 -0700387 throtl_rb_erase(&tg->rb_node, parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700388 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400389}
390
Tejun Heo0049af72013-05-14 13:52:33 -0700391static void throtl_dequeue_tg(struct throtl_grp *tg,
392 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400393{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700394 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo0049af72013-05-14 13:52:33 -0700395 __throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400396}
397
Tejun Heoa9131a22013-05-14 13:52:31 -0700398/* Call with queue lock held */
399static void throtl_schedule_delayed_work(struct throtl_data *td,
400 unsigned long delay)
401{
402 struct delayed_work *dwork = &td->dispatch_work;
403
Tejun Heo6a525602013-05-14 13:52:32 -0700404 mod_delayed_work(kthrotld_workqueue, dwork, delay);
405 throtl_log(td, "schedule work. delay=%lu jiffies=%lu", delay, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700406}
407
Vivek Goyale43473b2010-09-15 17:06:35 -0400408static void throtl_schedule_next_dispatch(struct throtl_data *td)
409{
Tejun Heoc9e03322013-05-14 13:52:32 -0700410 struct throtl_service_queue *sq = &td->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400411
Tejun Heo6a525602013-05-14 13:52:32 -0700412 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700413 if (!sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400414 return;
415
Tejun Heoc9e03322013-05-14 13:52:32 -0700416 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400417
Tejun Heoc9e03322013-05-14 13:52:32 -0700418 if (time_before_eq(sq->first_pending_disptime, jiffies))
Vivek Goyal450adcb2011-03-01 13:40:54 -0500419 throtl_schedule_delayed_work(td, 0);
Vivek Goyale43473b2010-09-15 17:06:35 -0400420 else
Tejun Heoc9e03322013-05-14 13:52:32 -0700421 throtl_schedule_delayed_work(td, sq->first_pending_disptime - jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400422}
423
Tejun Heo0f3457f2013-05-14 13:52:32 -0700424static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400425{
426 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400427 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400428 tg->slice_start[rw] = jiffies;
429 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heo0f3457f2013-05-14 13:52:32 -0700430 throtl_log_tg(tg, "[%c] new slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400431 rw == READ ? 'R' : 'W', tg->slice_start[rw],
432 tg->slice_end[rw], jiffies);
433}
434
Tejun Heo0f3457f2013-05-14 13:52:32 -0700435static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
436 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100437{
438 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
439}
440
Tejun Heo0f3457f2013-05-14 13:52:32 -0700441static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
442 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400443{
444 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heo0f3457f2013-05-14 13:52:32 -0700445 throtl_log_tg(tg, "[%c] extend slice start=%lu end=%lu jiffies=%lu",
Vivek Goyale43473b2010-09-15 17:06:35 -0400446 rw == READ ? 'R' : 'W', tg->slice_start[rw],
447 tg->slice_end[rw], jiffies);
448}
449
450/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700451static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400452{
453 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
454 return 0;
455
456 return 1;
457}
458
459/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700460static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400461{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200462 unsigned long nr_slices, time_elapsed, io_trim;
463 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400464
465 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
466
467 /*
468 * If bps are unlimited (-1), then time slice don't get
469 * renewed. Don't try to trim the slice if slice is used. A new
470 * slice will start when appropriate.
471 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700472 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400473 return;
474
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100475 /*
476 * A bio has been dispatched. Also adjust slice_end. It might happen
477 * that initially cgroup limit was very low resulting in high
478 * slice_end, but later limit was bumped up and bio was dispached
479 * sooner, then we need to reduce slice_end. A high bogus slice_end
480 * is bad because it does not allow new slice to start.
481 */
482
Tejun Heo0f3457f2013-05-14 13:52:32 -0700483 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100484
Vivek Goyale43473b2010-09-15 17:06:35 -0400485 time_elapsed = jiffies - tg->slice_start[rw];
486
487 nr_slices = time_elapsed / throtl_slice;
488
489 if (!nr_slices)
490 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200491 tmp = tg->bps[rw] * throtl_slice * nr_slices;
492 do_div(tmp, HZ);
493 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400494
Vivek Goyal8e89d132010-09-15 17:06:37 -0400495 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400496
Vivek Goyal8e89d132010-09-15 17:06:37 -0400497 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400498 return;
499
500 if (tg->bytes_disp[rw] >= bytes_trim)
501 tg->bytes_disp[rw] -= bytes_trim;
502 else
503 tg->bytes_disp[rw] = 0;
504
Vivek Goyal8e89d132010-09-15 17:06:37 -0400505 if (tg->io_disp[rw] >= io_trim)
506 tg->io_disp[rw] -= io_trim;
507 else
508 tg->io_disp[rw] = 0;
509
Vivek Goyale43473b2010-09-15 17:06:35 -0400510 tg->slice_start[rw] += nr_slices * throtl_slice;
511
Tejun Heo0f3457f2013-05-14 13:52:32 -0700512 throtl_log_tg(tg, "[%c] trim slice nr=%lu bytes=%llu io=%lu"
Vivek Goyale43473b2010-09-15 17:06:35 -0400513 " start=%lu end=%lu jiffies=%lu",
Vivek Goyal8e89d132010-09-15 17:06:37 -0400514 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
Vivek Goyale43473b2010-09-15 17:06:35 -0400515 tg->slice_start[rw], tg->slice_end[rw], jiffies);
516}
517
Tejun Heo0f3457f2013-05-14 13:52:32 -0700518static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
519 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400520{
521 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400522 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400523 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200524 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400525
Vivek Goyal8e89d132010-09-15 17:06:37 -0400526 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400527
Vivek Goyal8e89d132010-09-15 17:06:37 -0400528 /* Slice has just started. Consider one slice interval */
529 if (!jiffy_elapsed)
530 jiffy_elapsed_rnd = throtl_slice;
531
532 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
533
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200534 /*
535 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
536 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
537 * will allow dispatch after 1 second and after that slice should
538 * have been trimmed.
539 */
540
541 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
542 do_div(tmp, HZ);
543
544 if (tmp > UINT_MAX)
545 io_allowed = UINT_MAX;
546 else
547 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400548
549 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400550 if (wait)
551 *wait = 0;
552 return 1;
553 }
554
Vivek Goyal8e89d132010-09-15 17:06:37 -0400555 /* Calc approx time to dispatch */
556 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
557
558 if (jiffy_wait > jiffy_elapsed)
559 jiffy_wait = jiffy_wait - jiffy_elapsed;
560 else
561 jiffy_wait = 1;
562
563 if (wait)
564 *wait = jiffy_wait;
565 return 0;
566}
567
Tejun Heo0f3457f2013-05-14 13:52:32 -0700568static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
569 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400570{
571 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200572 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400573 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400574
575 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
576
577 /* Slice has just started. Consider one slice interval */
578 if (!jiffy_elapsed)
579 jiffy_elapsed_rnd = throtl_slice;
580
581 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
582
Vivek Goyal5e901a22010-10-01 21:16:38 +0200583 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
584 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200585 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400586
587 if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
588 if (wait)
589 *wait = 0;
590 return 1;
591 }
592
593 /* Calc approx time to dispatch */
594 extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
595 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
596
597 if (!jiffy_wait)
598 jiffy_wait = 1;
599
600 /*
601 * This wait time is without taking into consideration the rounding
602 * up we did. Add that time also.
603 */
604 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400605 if (wait)
606 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400607 return 0;
608}
Vivek Goyale43473b2010-09-15 17:06:35 -0400609
Vivek Goyalaf75cd32011-05-19 15:38:31 -0400610static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
611 if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
612 return 1;
613 return 0;
614}
615
Vivek Goyal8e89d132010-09-15 17:06:37 -0400616/*
617 * Returns whether one can dispatch a bio or not. Also returns approx number
618 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
619 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700620static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
621 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400622{
623 bool rw = bio_data_dir(bio);
624 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
625
626 /*
627 * Currently whole state machine of group depends on first bio
628 * queued in the group bio list. So one should not be calling
629 * this function with a different bio if there are other bios
630 * queued.
631 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700632 BUG_ON(tg->service_queue.nr_queued[rw] &&
633 bio != bio_list_peek(&tg->service_queue.bio_lists[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400634
635 /* If tg->bps = -1, then BW is unlimited */
636 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
637 if (wait)
638 *wait = 0;
639 return 1;
640 }
641
642 /*
643 * If previous slice expired, start a new one otherwise renew/extend
644 * existing slice to make sure it is at least throtl_slice interval
645 * long since now.
646 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700647 if (throtl_slice_used(tg, rw))
648 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400649 else {
650 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700651 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400652 }
653
Tejun Heo0f3457f2013-05-14 13:52:32 -0700654 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
655 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400656 if (wait)
657 *wait = 0;
658 return 1;
659 }
660
661 max_wait = max(bps_wait, iops_wait);
662
663 if (wait)
664 *wait = max_wait;
665
666 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700667 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400668
669 return 0;
670}
671
Tejun Heo3c798392012-04-16 13:57:25 -0700672static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700673 int rw)
674{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700675 struct throtl_grp *tg = blkg_to_tg(blkg);
676 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700677 unsigned long flags;
678
679 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700680 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700681 return;
682
683 /*
684 * Disabling interrupts to provide mutual exclusion between two
685 * writes on same cpu. It probably is not needed for 64bit. Not
686 * optimizing that case yet.
687 */
688 local_irq_save(flags);
689
Tejun Heo8a3d2612012-04-01 14:38:44 -0700690 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700691
Tejun Heo629ed0b2012-04-01 14:38:44 -0700692 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
693 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
694
695 local_irq_restore(flags);
696}
697
Vivek Goyale43473b2010-09-15 17:06:35 -0400698static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
699{
700 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400701
702 /* Charge the bio to the group */
703 tg->bytes_disp[rw] += bio->bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400704 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400705
Tejun Heo629ed0b2012-04-01 14:38:44 -0700706 throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size, bio->bi_rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400707}
708
Tejun Heo0049af72013-05-14 13:52:33 -0700709static void throtl_add_bio_tg(struct bio *bio, struct throtl_grp *tg,
710 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400711{
Tejun Heo73f0d492013-05-14 13:52:35 -0700712 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400713 bool rw = bio_data_dir(bio);
714
Tejun Heo73f0d492013-05-14 13:52:35 -0700715 bio_list_add(&sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400716 /* Take a bio reference on tg */
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800717 blkg_get(tg_to_blkg(tg));
Tejun Heo73f0d492013-05-14 13:52:35 -0700718 sq->nr_queued[rw]++;
Tejun Heoe2d57e62013-05-14 13:52:33 -0700719 tg->td->nr_queued[rw]++;
Tejun Heo0049af72013-05-14 13:52:33 -0700720 throtl_enqueue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400721}
722
Tejun Heo0049af72013-05-14 13:52:33 -0700723static void tg_update_disptime(struct throtl_grp *tg,
724 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400725{
Tejun Heo73f0d492013-05-14 13:52:35 -0700726 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400727 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
728 struct bio *bio;
729
Tejun Heo73f0d492013-05-14 13:52:35 -0700730 if ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700731 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400732
Tejun Heo73f0d492013-05-14 13:52:35 -0700733 if ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700734 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400735
736 min_wait = min(read_wait, write_wait);
737 disptime = jiffies + min_wait;
738
Vivek Goyale43473b2010-09-15 17:06:35 -0400739 /* Update dispatch time */
Tejun Heo0049af72013-05-14 13:52:33 -0700740 throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400741 tg->disptime = disptime;
Tejun Heo0049af72013-05-14 13:52:33 -0700742 throtl_enqueue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400743}
744
Tejun Heo0f3457f2013-05-14 13:52:32 -0700745static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw,
Tejun Heo651930b2013-05-14 13:52:35 -0700746 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400747{
Tejun Heo73f0d492013-05-14 13:52:35 -0700748 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400749 struct bio *bio;
750
Tejun Heo73f0d492013-05-14 13:52:35 -0700751 bio = bio_list_pop(&sq->bio_lists[rw]);
752 sq->nr_queued[rw]--;
Tejun Heo1adaf3d2012-03-05 13:15:15 -0800753 /* Drop bio reference on blkg */
754 blkg_put(tg_to_blkg(tg));
Vivek Goyale43473b2010-09-15 17:06:35 -0400755
Tejun Heo0f3457f2013-05-14 13:52:32 -0700756 BUG_ON(tg->td->nr_queued[rw] <= 0);
757 tg->td->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400758
759 throtl_charge_bio(tg, bio);
Tejun Heo651930b2013-05-14 13:52:35 -0700760 bio_list_add(&parent_sq->bio_lists[rw], bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400761 bio->bi_rw |= REQ_THROTTLED;
762
Tejun Heo0f3457f2013-05-14 13:52:32 -0700763 throtl_trim_slice(tg, rw);
Vivek Goyale43473b2010-09-15 17:06:35 -0400764}
765
Tejun Heo651930b2013-05-14 13:52:35 -0700766static int throtl_dispatch_tg(struct throtl_grp *tg,
767 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400768{
Tejun Heo73f0d492013-05-14 13:52:35 -0700769 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400770 unsigned int nr_reads = 0, nr_writes = 0;
771 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +0100772 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -0400773 struct bio *bio;
774
775 /* Try to dispatch 75% READS and 25% WRITES */
776
Tejun Heo73f0d492013-05-14 13:52:35 -0700777 while ((bio = bio_list_peek(&sq->bio_lists[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700778 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400779
Tejun Heo651930b2013-05-14 13:52:35 -0700780 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400781 nr_reads++;
782
783 if (nr_reads >= max_nr_reads)
784 break;
785 }
786
Tejun Heo73f0d492013-05-14 13:52:35 -0700787 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -0700788 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400789
Tejun Heo651930b2013-05-14 13:52:35 -0700790 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400791 nr_writes++;
792
793 if (nr_writes >= max_nr_writes)
794 break;
795 }
796
797 return nr_reads + nr_writes;
798}
799
Tejun Heo651930b2013-05-14 13:52:35 -0700800static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400801{
802 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400803
804 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -0700805 struct throtl_grp *tg = throtl_rb_first(parent_sq);
806 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400807
808 if (!tg)
809 break;
810
811 if (time_before(jiffies, tg->disptime))
812 break;
813
Tejun Heo0049af72013-05-14 13:52:33 -0700814 throtl_dequeue_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400815
Tejun Heo651930b2013-05-14 13:52:35 -0700816 nr_disp += throtl_dispatch_tg(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400817
Tejun Heo73f0d492013-05-14 13:52:35 -0700818 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo0049af72013-05-14 13:52:33 -0700819 tg_update_disptime(tg, parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400820
821 if (nr_disp >= throtl_quantum)
822 break;
823 }
824
825 return nr_disp;
826}
827
Tejun Heocb761992013-05-14 13:52:31 -0700828/* work function to dispatch throttled bios */
829void blk_throtl_dispatch_work_fn(struct work_struct *work)
Vivek Goyale43473b2010-09-15 17:06:35 -0400830{
Tejun Heocb761992013-05-14 13:52:31 -0700831 struct throtl_data *td = container_of(to_delayed_work(work),
832 struct throtl_data, dispatch_work);
Tejun Heo651930b2013-05-14 13:52:35 -0700833 struct throtl_service_queue *sq = &td->service_queue;
Tejun Heocb761992013-05-14 13:52:31 -0700834 struct request_queue *q = td->queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400835 unsigned int nr_disp = 0;
836 struct bio_list bio_list_on_stack;
837 struct bio *bio;
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100838 struct blk_plug plug;
Tejun Heo651930b2013-05-14 13:52:35 -0700839 int rw;
Vivek Goyale43473b2010-09-15 17:06:35 -0400840
841 spin_lock_irq(q->queue_lock);
842
Vivek Goyale43473b2010-09-15 17:06:35 -0400843 bio_list_init(&bio_list_on_stack);
844
Joe Perchesd2f31a52011-06-13 20:19:27 +0200845 throtl_log(td, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo6a525602013-05-14 13:52:32 -0700846 td->nr_queued[READ] + td->nr_queued[WRITE],
847 td->nr_queued[READ], td->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -0400848
Tejun Heo651930b2013-05-14 13:52:35 -0700849 nr_disp = throtl_select_dispatch(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400850
Tejun Heo651930b2013-05-14 13:52:35 -0700851 if (nr_disp) {
852 for (rw = READ; rw <= WRITE; rw++) {
853 bio_list_merge(&bio_list_on_stack, &sq->bio_lists[rw]);
854 bio_list_init(&sq->bio_lists[rw]);
855 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400856 throtl_log(td, "bios disp=%u", nr_disp);
Tejun Heo651930b2013-05-14 13:52:35 -0700857 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400858
859 throtl_schedule_next_dispatch(td);
Tejun Heo6a525602013-05-14 13:52:32 -0700860
Vivek Goyale43473b2010-09-15 17:06:35 -0400861 spin_unlock_irq(q->queue_lock);
862
863 /*
864 * If we dispatched some requests, unplug the queue to make sure
865 * immediate dispatch
866 */
867 if (nr_disp) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100868 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400869 while((bio = bio_list_pop(&bio_list_on_stack)))
870 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +0100871 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -0400872 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400873}
874
Tejun Heof95a04a2012-04-16 13:57:26 -0700875static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
876 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -0700877{
Tejun Heof95a04a2012-04-16 13:57:26 -0700878 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -0700879 struct blkg_rwstat rwstat = { }, tmp;
880 int i, cpu;
881
882 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -0700883 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -0700884
885 tmp = blkg_rwstat_read((void *)sc + off);
886 for (i = 0; i < BLKG_RWSTAT_NR; i++)
887 rwstat.cnt[i] += tmp.cnt[i];
888 }
889
Tejun Heof95a04a2012-04-16 13:57:26 -0700890 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -0700891}
892
Tejun Heo8a3d2612012-04-01 14:38:44 -0700893static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
894 struct seq_file *sf)
Tejun Heo41b38b62012-04-01 14:38:44 -0700895{
Tejun Heo3c798392012-04-16 13:57:25 -0700896 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo41b38b62012-04-01 14:38:44 -0700897
Tejun Heo3c798392012-04-16 13:57:25 -0700898 blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
Tejun Heo5bc4afb12012-04-01 14:38:45 -0700899 cft->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -0700900 return 0;
901}
902
Tejun Heof95a04a2012-04-16 13:57:26 -0700903static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
904 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700905{
Tejun Heof95a04a2012-04-16 13:57:26 -0700906 struct throtl_grp *tg = pd_to_tg(pd);
907 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700908
Tejun Heoaf133ce2012-04-01 14:38:44 -0700909 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700910 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700911 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700912}
913
Tejun Heof95a04a2012-04-16 13:57:26 -0700914static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
915 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700916{
Tejun Heof95a04a2012-04-16 13:57:26 -0700917 struct throtl_grp *tg = pd_to_tg(pd);
918 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700919
920 if (v == -1)
921 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -0700922 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700923}
924
925static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
926 struct seq_file *sf)
927{
Tejun Heo3c798392012-04-16 13:57:25 -0700928 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
929 &blkcg_policy_throtl, cft->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700930 return 0;
931}
932
Tejun Heoaf133ce2012-04-01 14:38:44 -0700933static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
934 struct seq_file *sf)
Vivek Goyale43473b2010-09-15 17:06:35 -0400935{
Tejun Heo3c798392012-04-16 13:57:25 -0700936 blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
937 &blkcg_policy_throtl, cft->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -0700938 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400939}
940
Tejun Heoaf133ce2012-04-01 14:38:44 -0700941static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
942 bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700943{
Tejun Heo3c798392012-04-16 13:57:25 -0700944 struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700945 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700946 struct throtl_grp *tg;
Tejun Heoa2b16932012-04-13 13:11:33 -0700947 struct throtl_data *td;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700948 int ret;
949
Tejun Heo3c798392012-04-16 13:57:25 -0700950 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700951 if (ret)
952 return ret;
953
Tejun Heoaf133ce2012-04-01 14:38:44 -0700954 tg = blkg_to_tg(ctx.blkg);
Tejun Heoa2b16932012-04-13 13:11:33 -0700955 td = ctx.blkg->q->td;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700956
Tejun Heoa2b16932012-04-13 13:11:33 -0700957 if (!ctx.v)
958 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700959
Tejun Heoa2b16932012-04-13 13:11:33 -0700960 if (is_u64)
961 *(u64 *)((void *)tg + cft->private) = ctx.v;
962 else
963 *(unsigned int *)((void *)tg + cft->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -0700964
Tejun Heo0f3457f2013-05-14 13:52:32 -0700965 throtl_log_tg(tg, "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Tejun Heo632b4492013-05-14 13:52:31 -0700966 tg->bps[READ], tg->bps[WRITE],
967 tg->iops[READ], tg->iops[WRITE]);
968
969 /*
970 * We're already holding queue_lock and know @tg is valid. Let's
971 * apply the new config directly.
972 *
973 * Restart the slices for both READ and WRITES. It might happen
974 * that a group's limit are dropped suddenly and we don't want to
975 * account recently dispatched IO with new low rate.
976 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700977 throtl_start_new_slice(tg, 0);
978 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -0700979
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700980 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo0049af72013-05-14 13:52:33 -0700981 tg_update_disptime(tg, &td->service_queue);
Tejun Heo632b4492013-05-14 13:52:31 -0700982 throtl_schedule_next_dispatch(td);
983 }
Tejun Heo60c2bc22012-04-01 14:38:43 -0700984
985 blkg_conf_finish(&ctx);
Tejun Heoa2b16932012-04-13 13:11:33 -0700986 return 0;
Tejun Heo60c2bc22012-04-01 14:38:43 -0700987}
988
Tejun Heoaf133ce2012-04-01 14:38:44 -0700989static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
990 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700991{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700992 return tg_set_conf(cgrp, cft, buf, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700993}
994
Tejun Heoaf133ce2012-04-01 14:38:44 -0700995static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
996 const char *buf)
Tejun Heo60c2bc22012-04-01 14:38:43 -0700997{
Tejun Heoaf133ce2012-04-01 14:38:44 -0700998 return tg_set_conf(cgrp, cft, buf, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -0700999}
1000
1001static struct cftype throtl_files[] = {
1002 {
1003 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001004 .private = offsetof(struct throtl_grp, bps[READ]),
1005 .read_seq_string = tg_print_conf_u64,
1006 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001007 .max_write_len = 256,
1008 },
1009 {
1010 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001011 .private = offsetof(struct throtl_grp, bps[WRITE]),
1012 .read_seq_string = tg_print_conf_u64,
1013 .write_string = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001014 .max_write_len = 256,
1015 },
1016 {
1017 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001018 .private = offsetof(struct throtl_grp, iops[READ]),
1019 .read_seq_string = tg_print_conf_uint,
1020 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001021 .max_write_len = 256,
1022 },
1023 {
1024 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001025 .private = offsetof(struct throtl_grp, iops[WRITE]),
1026 .read_seq_string = tg_print_conf_uint,
1027 .write_string = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001028 .max_write_len = 256,
1029 },
1030 {
1031 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001032 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001033 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001034 },
1035 {
1036 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001037 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo8a3d2612012-04-01 14:38:44 -07001038 .read_seq_string = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001039 },
1040 { } /* terminate */
1041};
1042
Vivek Goyalda527772011-03-02 19:05:33 -05001043static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001044{
1045 struct throtl_data *td = q->td;
1046
Tejun Heocb761992013-05-14 13:52:31 -07001047 cancel_delayed_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001048}
1049
Tejun Heo3c798392012-04-16 13:57:25 -07001050static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001051 .pd_size = sizeof(struct throtl_grp),
1052 .cftypes = throtl_files,
1053
1054 .pd_init_fn = throtl_pd_init,
1055 .pd_exit_fn = throtl_pd_exit,
1056 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001057};
1058
Tejun Heobc16a4f2011-10-19 14:33:01 +02001059bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001060{
1061 struct throtl_data *td = q->td;
1062 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001063 struct throtl_service_queue *sq;
Vivek Goyale43473b2010-09-15 17:06:35 -04001064 bool rw = bio_data_dir(bio), update_disptime = true;
Tejun Heo3c798392012-04-16 13:57:25 -07001065 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001066 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001067
1068 if (bio->bi_rw & REQ_THROTTLED) {
1069 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001070 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001071 }
1072
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001073 /*
1074 * A throtl_grp pointer retrieved under rcu can be used to access
1075 * basic fields like stats and io rates. If a group has no rules,
1076 * just update the dispatch stats in lockless manner and return.
1077 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001078 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001079 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001080 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001081 if (tg) {
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001082 if (tg_no_rule_group(tg, rw)) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001083 throtl_update_dispatch_stats(tg_to_blkg(tg),
1084 bio->bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001085 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001086 }
1087 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001088
1089 /*
1090 * Either group has not been allocated yet or it is not an unlimited
1091 * IO group
1092 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001093 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001094 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001095 if (unlikely(!tg))
1096 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001097
Tejun Heo73f0d492013-05-14 13:52:35 -07001098 sq = &tg->service_queue;
1099
1100 if (sq->nr_queued[rw]) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001101 /*
1102 * There is already another bio queued in same dir. No
1103 * need to update dispatch time.
1104 */
Vivek Goyal231d7042011-03-07 21:05:14 +01001105 update_disptime = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001106 goto queue_bio;
Vivek Goyalde701c72011-03-07 21:09:32 +01001107
Vivek Goyale43473b2010-09-15 17:06:35 -04001108 }
1109
1110 /* Bio is with-in rate limit of group */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001111 if (tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001112 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001113
1114 /*
1115 * We need to trim slice even when bios are not being queued
1116 * otherwise it might happen that a bio is not queued for
1117 * a long time and slice keeps on extending and trim is not
1118 * called for a long time. Now if limits are reduced suddenly
1119 * we take into account all the IO dispatched so far at new
1120 * low rate and * newly queued IO gets a really long dispatch
1121 * time.
1122 *
1123 * So keep on trimming slice even if bio is not queued.
1124 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001125 throtl_trim_slice(tg, rw);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001126 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001127 }
1128
1129queue_bio:
Tejun Heo0f3457f2013-05-14 13:52:32 -07001130 throtl_log_tg(tg, "[%c] bio. bdisp=%llu sz=%u bps=%llu"
Vivek Goyal8e89d132010-09-15 17:06:37 -04001131 " iodisp=%u iops=%u queued=%d/%d",
1132 rw == READ ? 'R' : 'W',
Vivek Goyale43473b2010-09-15 17:06:35 -04001133 tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
Vivek Goyal8e89d132010-09-15 17:06:37 -04001134 tg->io_disp[rw], tg->iops[rw],
Tejun Heo73f0d492013-05-14 13:52:35 -07001135 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001136
Tejun Heo671058f2012-03-05 13:15:29 -08001137 bio_associate_current(bio);
Tejun Heo0049af72013-05-14 13:52:33 -07001138 throtl_add_bio_tg(bio, tg, &q->td->service_queue);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001139 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001140
1141 if (update_disptime) {
Tejun Heo0049af72013-05-14 13:52:33 -07001142 tg_update_disptime(tg, &td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001143 throtl_schedule_next_dispatch(td);
1144 }
1145
Tejun Heobc16a4f2011-10-19 14:33:01 +02001146out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001147 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001148out_unlock_rcu:
1149 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001150out:
1151 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001152}
1153
Tejun Heoc9a929d2011-10-19 14:42:16 +02001154/**
1155 * blk_throtl_drain - drain throttled bios
1156 * @q: request_queue to drain throttled bios for
1157 *
1158 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1159 */
1160void blk_throtl_drain(struct request_queue *q)
1161 __releases(q->queue_lock) __acquires(q->queue_lock)
1162{
1163 struct throtl_data *td = q->td;
Tejun Heo0049af72013-05-14 13:52:33 -07001164 struct throtl_service_queue *parent_sq = &td->service_queue;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001165 struct throtl_grp *tg;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001166 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001167 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001168
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001169 queue_lockdep_assert_held(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001170
Tejun Heo0049af72013-05-14 13:52:33 -07001171 while ((tg = throtl_rb_first(parent_sq))) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001172 struct throtl_service_queue *sq = &tg->service_queue;
1173
Tejun Heo0049af72013-05-14 13:52:33 -07001174 throtl_dequeue_tg(tg, parent_sq);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001175
Tejun Heo73f0d492013-05-14 13:52:35 -07001176 while ((bio = bio_list_peek(&sq->bio_lists[READ])))
Tejun Heo651930b2013-05-14 13:52:35 -07001177 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Tejun Heo73f0d492013-05-14 13:52:35 -07001178 while ((bio = bio_list_peek(&sq->bio_lists[WRITE])))
Tejun Heo651930b2013-05-14 13:52:35 -07001179 tg_dispatch_one_bio(tg, bio_data_dir(bio), parent_sq);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001180 }
1181 spin_unlock_irq(q->queue_lock);
1182
Tejun Heo651930b2013-05-14 13:52:35 -07001183 for (rw = READ; rw <= WRITE; rw++)
1184 while ((bio = bio_list_pop(&parent_sq->bio_lists[rw])))
1185 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001186
1187 spin_lock_irq(q->queue_lock);
1188}
1189
Vivek Goyale43473b2010-09-15 17:06:35 -04001190int blk_throtl_init(struct request_queue *q)
1191{
1192 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001193 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001194
1195 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1196 if (!td)
1197 return -ENOMEM;
1198
Tejun Heocb761992013-05-14 13:52:31 -07001199 INIT_DELAYED_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heo49a2f1e2013-05-14 13:52:34 -07001200 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001201
Tejun Heocd1604f2012-03-05 13:15:06 -08001202 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001203 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001204
Tejun Heoa2b16932012-04-13 13:11:33 -07001205 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001206 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001207 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001208 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001209 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001210}
1211
1212void blk_throtl_exit(struct request_queue *q)
1213{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001214 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001215 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001216 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001217 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001218}
1219
1220static int __init throtl_init(void)
1221{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001222 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1223 if (!kthrotld_workqueue)
1224 panic("Failed to create kthrotld\n");
1225
Tejun Heo3c798392012-04-16 13:57:25 -07001226 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001227}
1228
1229module_init(throtl_init);