blob: f1dd691c53593ca91dce47fbd42474b13887fabe [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>
Tejun Heoeea8f412015-05-22 17:13:17 -040012#include <linux/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 Heoc5cc2072013-05-14 13:52:38 -070029/*
30 * To implement hierarchical throttling, throtl_grps form a tree and bios
31 * are dispatched upwards level by level until they reach the top and get
32 * issued. When dispatching bios from the children and local group at each
33 * level, if the bios are dispatched into a single bio_list, there's a risk
34 * of a local or child group which can queue many bios at once filling up
35 * the list starving others.
36 *
37 * To avoid such starvation, dispatched bios are queued separately
38 * according to where they came from. When they are again dispatched to
39 * the parent, they're popped in round-robin order so that no single source
40 * hogs the dispatch window.
41 *
42 * throtl_qnode is used to keep the queued bios separated by their sources.
43 * Bios are queued to throtl_qnode which in turn is queued to
44 * throtl_service_queue and then dispatched in round-robin order.
45 *
46 * It's also used to track the reference counts on blkg's. A qnode always
47 * belongs to a throtl_grp and gets queued on itself or the parent, so
48 * incrementing the reference of the associated throtl_grp when a qnode is
49 * queued and decrementing when dequeued is enough to keep the whole blkg
50 * tree pinned while bios are in flight.
51 */
52struct throtl_qnode {
53 struct list_head node; /* service_queue->queued[] */
54 struct bio_list bios; /* queued bios */
55 struct throtl_grp *tg; /* tg this qnode belongs to */
56};
57
Tejun Heoc9e03322013-05-14 13:52:32 -070058struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070059 struct throtl_service_queue *parent_sq; /* the parent service_queue */
60
Tejun Heo73f0d492013-05-14 13:52:35 -070061 /*
62 * Bios queued directly to this service_queue or dispatched from
63 * children throtl_grp's.
64 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070065 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070066 unsigned int nr_queued[2]; /* number of queued bios */
67
68 /*
69 * RB tree of active children throtl_grp's, which are sorted by
70 * their ->disptime.
71 */
Tejun Heoc9e03322013-05-14 13:52:32 -070072 struct rb_root pending_tree; /* RB tree of active tgs */
73 struct rb_node *first_pending; /* first node in the tree */
74 unsigned int nr_pending; /* # queued in the tree */
75 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070076 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040077};
78
Tejun Heo5b2c16a2013-05-14 13:52:32 -070079enum tg_state_flags {
80 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070081 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070082};
83
Vivek Goyale43473b2010-09-15 17:06:35 -040084#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
85
Tejun Heo8a3d2612012-04-01 14:38:44 -070086/* Per-cpu group stats */
87struct tg_stats_cpu {
88 /* total bytes transferred */
89 struct blkg_rwstat service_bytes;
90 /* total IOs serviced, post merge */
91 struct blkg_rwstat serviced;
92};
93
Vivek Goyale43473b2010-09-15 17:06:35 -040094struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070095 /* must be the first member */
96 struct blkg_policy_data pd;
97
Tejun Heoc9e03322013-05-14 13:52:32 -070098 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040099 struct rb_node rb_node;
100
Tejun Heo0f3457f2013-05-14 13:52:32 -0700101 /* throtl_data this group belongs to */
102 struct throtl_data *td;
103
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700104 /* this group's service queue */
105 struct throtl_service_queue service_queue;
106
Vivek Goyale43473b2010-09-15 17:06:35 -0400107 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700108 * qnode_on_self is used when bios are directly queued to this
109 * throtl_grp so that local bios compete fairly with bios
110 * dispatched from children. qnode_on_parent is used when bios are
111 * dispatched from this throtl_grp into its parent and will compete
112 * with the sibling qnode_on_parents and the parent's
113 * qnode_on_self.
114 */
115 struct throtl_qnode qnode_on_self[2];
116 struct throtl_qnode qnode_on_parent[2];
117
118 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400119 * Dispatch time in jiffies. This is the estimated time when group
120 * will unthrottle and is ready to dispatch more bio. It is used as
121 * key to sort active groups in service tree.
122 */
123 unsigned long disptime;
124
Vivek Goyale43473b2010-09-15 17:06:35 -0400125 unsigned int flags;
126
Tejun Heo693e7512013-05-14 13:52:38 -0700127 /* are there any throtl rules between this group and td? */
128 bool has_rules[2];
129
Vivek Goyale43473b2010-09-15 17:06:35 -0400130 /* bytes per second rate limits */
131 uint64_t bps[2];
132
Vivek Goyal8e89d132010-09-15 17:06:37 -0400133 /* IOPS limits */
134 unsigned int iops[2];
135
Vivek Goyale43473b2010-09-15 17:06:35 -0400136 /* Number of bytes disptached in current slice */
137 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400138 /* Number of bio's dispatched in current slice */
139 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400140
141 /* When did we start a new slice */
142 unsigned long slice_start[2];
143 unsigned long slice_end[2];
Vivek Goyalfe071432010-10-01 14:49:49 +0200144
Tejun Heo8a3d2612012-04-01 14:38:44 -0700145 /* Per cpu stats pointer */
146 struct tg_stats_cpu __percpu *stats_cpu;
147
148 /* List of tgs waiting for per cpu stats memory to be allocated */
149 struct list_head stats_alloc_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400150};
151
152struct throtl_data
153{
Vivek Goyale43473b2010-09-15 17:06:35 -0400154 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700155 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400156
Vivek Goyale43473b2010-09-15 17:06:35 -0400157 struct request_queue *queue;
158
159 /* Total Number of queued bios on READ and WRITE lists */
160 unsigned int nr_queued[2];
161
162 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200163 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400164 */
165 unsigned int nr_undestroyed_grps;
166
167 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700168 struct work_struct dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400169};
170
Tejun Heo8a3d2612012-04-01 14:38:44 -0700171/* list and work item to allocate percpu group stats */
172static DEFINE_SPINLOCK(tg_stats_alloc_lock);
173static LIST_HEAD(tg_stats_alloc_list);
174
175static void tg_stats_alloc_fn(struct work_struct *);
176static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
177
Tejun Heo69df0ab2013-05-14 13:52:36 -0700178static void throtl_pending_timer_fn(unsigned long arg);
179
Tejun Heof95a04a2012-04-16 13:57:26 -0700180static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
181{
182 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
183}
184
Tejun Heo3c798392012-04-16 13:57:25 -0700185static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800186{
Tejun Heof95a04a2012-04-16 13:57:26 -0700187 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800188}
189
Tejun Heo3c798392012-04-16 13:57:25 -0700190static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800191{
Tejun Heof95a04a2012-04-16 13:57:26 -0700192 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800193}
194
Tejun Heo03d8e112012-04-13 13:11:32 -0700195static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
196{
197 return blkg_to_tg(td->queue->root_blkg);
198}
199
Tejun Heofda6f272013-05-14 13:52:36 -0700200/**
201 * sq_to_tg - return the throl_grp the specified service queue belongs to
202 * @sq: the throtl_service_queue of interest
203 *
204 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
205 * embedded in throtl_data, %NULL is returned.
206 */
207static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
208{
209 if (sq && sq->parent_sq)
210 return container_of(sq, struct throtl_grp, service_queue);
211 else
212 return NULL;
213}
Vivek Goyale43473b2010-09-15 17:06:35 -0400214
Tejun Heofda6f272013-05-14 13:52:36 -0700215/**
216 * sq_to_td - return throtl_data the specified service queue belongs to
217 * @sq: the throtl_service_queue of interest
218 *
219 * A service_queue can be embeded in either a throtl_grp or throtl_data.
220 * Determine the associated throtl_data accordingly and return it.
221 */
222static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
223{
224 struct throtl_grp *tg = sq_to_tg(sq);
225
226 if (tg)
227 return tg->td;
228 else
229 return container_of(sq, struct throtl_data, service_queue);
230}
231
232/**
233 * throtl_log - log debug message via blktrace
234 * @sq: the service_queue being reported
235 * @fmt: printf format string
236 * @args: printf args
237 *
238 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
239 * throtl_grp; otherwise, just "throtl".
240 *
241 * TODO: this should be made a function and name formatting should happen
242 * after testing whether blktrace is enabled.
243 */
244#define throtl_log(sq, fmt, args...) do { \
245 struct throtl_grp *__tg = sq_to_tg((sq)); \
246 struct throtl_data *__td = sq_to_td((sq)); \
247 \
248 (void)__td; \
249 if ((__tg)) { \
250 char __pbuf[128]; \
251 \
252 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
253 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
254 } else { \
255 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
256 } \
257} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400258
Peter Zijlstra90d38392013-11-12 19:42:14 -0800259static void tg_stats_init(struct tg_stats_cpu *tg_stats)
260{
261 blkg_rwstat_init(&tg_stats->service_bytes);
262 blkg_rwstat_init(&tg_stats->serviced);
263}
264
Tejun Heo8a3d2612012-04-01 14:38:44 -0700265/*
266 * Worker for allocating per cpu stat for tgs. This is scheduled on the
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700267 * system_wq once there are some groups on the alloc_list waiting for
Tejun Heo8a3d2612012-04-01 14:38:44 -0700268 * allocation.
269 */
270static void tg_stats_alloc_fn(struct work_struct *work)
271{
272 static struct tg_stats_cpu *stats_cpu; /* this fn is non-reentrant */
273 struct delayed_work *dwork = to_delayed_work(work);
274 bool empty = false;
275
276alloc_stats:
277 if (!stats_cpu) {
Peter Zijlstra90d38392013-11-12 19:42:14 -0800278 int cpu;
279
Tejun Heo8a3d2612012-04-01 14:38:44 -0700280 stats_cpu = alloc_percpu(struct tg_stats_cpu);
281 if (!stats_cpu) {
282 /* allocation failed, try again after some time */
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700283 schedule_delayed_work(dwork, msecs_to_jiffies(10));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700284 return;
285 }
Peter Zijlstra90d38392013-11-12 19:42:14 -0800286 for_each_possible_cpu(cpu)
287 tg_stats_init(per_cpu_ptr(stats_cpu, cpu));
Tejun Heo8a3d2612012-04-01 14:38:44 -0700288 }
289
290 spin_lock_irq(&tg_stats_alloc_lock);
291
292 if (!list_empty(&tg_stats_alloc_list)) {
293 struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
294 struct throtl_grp,
295 stats_alloc_node);
296 swap(tg->stats_cpu, stats_cpu);
297 list_del_init(&tg->stats_alloc_node);
298 }
299
300 empty = list_empty(&tg_stats_alloc_list);
301 spin_unlock_irq(&tg_stats_alloc_lock);
302 if (!empty)
303 goto alloc_stats;
304}
305
Tejun Heoc5cc2072013-05-14 13:52:38 -0700306static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
307{
308 INIT_LIST_HEAD(&qn->node);
309 bio_list_init(&qn->bios);
310 qn->tg = tg;
311}
312
313/**
314 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
315 * @bio: bio being added
316 * @qn: qnode to add bio to
317 * @queued: the service_queue->queued[] list @qn belongs to
318 *
319 * Add @bio to @qn and put @qn on @queued if it's not already on.
320 * @qn->tg's reference count is bumped when @qn is activated. See the
321 * comment on top of throtl_qnode definition for details.
322 */
323static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
324 struct list_head *queued)
325{
326 bio_list_add(&qn->bios, bio);
327 if (list_empty(&qn->node)) {
328 list_add_tail(&qn->node, queued);
329 blkg_get(tg_to_blkg(qn->tg));
330 }
331}
332
333/**
334 * throtl_peek_queued - peek the first bio on a qnode list
335 * @queued: the qnode list to peek
336 */
337static struct bio *throtl_peek_queued(struct list_head *queued)
338{
339 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
340 struct bio *bio;
341
342 if (list_empty(queued))
343 return NULL;
344
345 bio = bio_list_peek(&qn->bios);
346 WARN_ON_ONCE(!bio);
347 return bio;
348}
349
350/**
351 * throtl_pop_queued - pop the first bio form a qnode list
352 * @queued: the qnode list to pop a bio from
353 * @tg_to_put: optional out argument for throtl_grp to put
354 *
355 * Pop the first bio from the qnode list @queued. After popping, the first
356 * qnode is removed from @queued if empty or moved to the end of @queued so
357 * that the popping order is round-robin.
358 *
359 * When the first qnode is removed, its associated throtl_grp should be put
360 * too. If @tg_to_put is NULL, this function automatically puts it;
361 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
362 * responsible for putting it.
363 */
364static struct bio *throtl_pop_queued(struct list_head *queued,
365 struct throtl_grp **tg_to_put)
366{
367 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
368 struct bio *bio;
369
370 if (list_empty(queued))
371 return NULL;
372
373 bio = bio_list_pop(&qn->bios);
374 WARN_ON_ONCE(!bio);
375
376 if (bio_list_empty(&qn->bios)) {
377 list_del_init(&qn->node);
378 if (tg_to_put)
379 *tg_to_put = qn->tg;
380 else
381 blkg_put(tg_to_blkg(qn->tg));
382 } else {
383 list_move_tail(&qn->node, queued);
384 }
385
386 return bio;
387}
388
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700389/* init a service_queue, assumes the caller zeroed it */
Tejun Heo77216b02013-05-14 13:52:36 -0700390static void throtl_service_queue_init(struct throtl_service_queue *sq,
391 struct throtl_service_queue *parent_sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700392{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700393 INIT_LIST_HEAD(&sq->queued[0]);
394 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700395 sq->pending_tree = RB_ROOT;
Tejun Heo77216b02013-05-14 13:52:36 -0700396 sq->parent_sq = parent_sq;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700397 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
398 (unsigned long)sq);
399}
400
401static void throtl_service_queue_exit(struct throtl_service_queue *sq)
402{
403 del_timer_sync(&sq->pending_timer);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700404}
405
Tejun Heo001bea72015-08-18 14:55:11 -0700406static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
407{
408 return kzalloc_node(sizeof(struct throtl_grp), gfp, node);
409}
410
Tejun Heo3c798392012-04-16 13:57:25 -0700411static void throtl_pd_init(struct blkcg_gq *blkg)
Vivek Goyala29a1712011-05-19 15:38:19 -0400412{
Tejun Heo03814112012-03-05 13:15:14 -0800413 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heo77216b02013-05-14 13:52:36 -0700414 struct throtl_data *td = blkg->q->td;
Tejun Heo91381252013-05-14 13:52:38 -0700415 struct throtl_service_queue *parent_sq;
Tejun Heoff26eaa2012-05-23 12:16:21 +0200416 unsigned long flags;
Tejun Heoc5cc2072013-05-14 13:52:38 -0700417 int rw;
Tejun Heocd1604f2012-03-05 13:15:06 -0800418
Tejun Heo91381252013-05-14 13:52:38 -0700419 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400420 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700421 * behavior where limits on a given throtl_grp are applied to the
422 * whole subtree rather than just the group itself. e.g. If 16M
423 * read_bps limit is set on the root group, the whole system can't
424 * exceed 16M for the device.
425 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400426 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700427 * behavior is retained where all throtl_grps are treated as if
428 * they're all separate root groups right below throtl_data.
429 * Limits of a group don't interact with limits of other groups
430 * regardless of the position of the group in the hierarchy.
431 */
432 parent_sq = &td->service_queue;
433
Tejun Heoaa6ec292014-07-09 10:08:08 -0400434 if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
Tejun Heo91381252013-05-14 13:52:38 -0700435 parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
436
437 throtl_service_queue_init(&tg->service_queue, parent_sq);
438
Tejun Heoc5cc2072013-05-14 13:52:38 -0700439 for (rw = READ; rw <= WRITE; rw++) {
440 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
441 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
442 }
443
Vivek Goyala29a1712011-05-19 15:38:19 -0400444 RB_CLEAR_NODE(&tg->rb_node);
Tejun Heo77216b02013-05-14 13:52:36 -0700445 tg->td = td;
Vivek Goyala29a1712011-05-19 15:38:19 -0400446
Tejun Heoe56da7e2012-03-05 13:15:07 -0800447 tg->bps[READ] = -1;
448 tg->bps[WRITE] = -1;
449 tg->iops[READ] = -1;
450 tg->iops[WRITE] = -1;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700451
452 /*
453 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
454 * but percpu allocator can't be called from IO path. Queue tg on
455 * tg_stats_alloc_list and allocate from work item.
456 */
Tejun Heoff26eaa2012-05-23 12:16:21 +0200457 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700458 list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
Tejun Heo3b07e9c2012-08-20 14:51:24 -0700459 schedule_delayed_work(&tg_stats_alloc_work, 0);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200460 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700461}
462
Tejun Heo693e7512013-05-14 13:52:38 -0700463/*
464 * Set has_rules[] if @tg or any of its parents have limits configured.
465 * This doesn't require walking up to the top of the hierarchy as the
466 * parent's has_rules[] is guaranteed to be correct.
467 */
468static void tg_update_has_rules(struct throtl_grp *tg)
469{
470 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
471 int rw;
472
473 for (rw = READ; rw <= WRITE; rw++)
474 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
475 (tg->bps[rw] != -1 || tg->iops[rw] != -1);
476}
477
478static void throtl_pd_online(struct blkcg_gq *blkg)
479{
480 /*
481 * We don't want new groups to escape the limits of its ancestors.
482 * Update has_rules[] after a new group is brought online.
483 */
484 tg_update_has_rules(blkg_to_tg(blkg));
485}
486
Tejun Heo3c798392012-04-16 13:57:25 -0700487static void throtl_pd_exit(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700488{
489 struct throtl_grp *tg = blkg_to_tg(blkg);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200490 unsigned long flags;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700491
Tejun Heoff26eaa2012-05-23 12:16:21 +0200492 spin_lock_irqsave(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700493 list_del_init(&tg->stats_alloc_node);
Tejun Heoff26eaa2012-05-23 12:16:21 +0200494 spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700495
496 free_percpu(tg->stats_cpu);
Tejun Heo69df0ab2013-05-14 13:52:36 -0700497
498 throtl_service_queue_exit(&tg->service_queue);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700499}
500
Tejun Heo001bea72015-08-18 14:55:11 -0700501static void throtl_pd_free(struct blkg_policy_data *pd)
502{
503 kfree(pd);
504}
505
Tejun Heo3c798392012-04-16 13:57:25 -0700506static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700507{
508 struct throtl_grp *tg = blkg_to_tg(blkg);
509 int cpu;
510
511 if (tg->stats_cpu == NULL)
512 return;
513
514 for_each_possible_cpu(cpu) {
515 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
516
517 blkg_rwstat_reset(&sc->service_bytes);
518 blkg_rwstat_reset(&sc->serviced);
519 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400520}
521
Tejun Heo3c798392012-04-16 13:57:25 -0700522static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
523 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400524{
Vivek Goyale43473b2010-09-15 17:06:35 -0400525 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700526 * This is the common case when there are no blkcgs. Avoid lookup
527 * in this case
Tejun Heocd1604f2012-03-05 13:15:06 -0800528 */
Tejun Heo3c798392012-04-16 13:57:25 -0700529 if (blkcg == &blkcg_root)
Tejun Heo03d8e112012-04-13 13:11:32 -0700530 return td_root_tg(td);
Vivek Goyale43473b2010-09-15 17:06:35 -0400531
Tejun Heoe8989fa2012-03-05 13:15:20 -0800532 return blkg_to_tg(blkg_lookup(blkcg, td->queue));
Vivek Goyale43473b2010-09-15 17:06:35 -0400533}
534
Tejun Heocd1604f2012-03-05 13:15:06 -0800535static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
Tejun Heo3c798392012-04-16 13:57:25 -0700536 struct blkcg *blkcg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400537{
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400538 struct request_queue *q = td->queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800539 struct throtl_grp *tg = NULL;
Tejun Heo0a5a7d02012-03-05 13:15:02 -0800540
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400541 /*
Tejun Heo3c798392012-04-16 13:57:25 -0700542 * This is the common case when there are no blkcgs. Avoid lookup
543 * in this case
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400544 */
Tejun Heo3c798392012-04-16 13:57:25 -0700545 if (blkcg == &blkcg_root) {
Tejun Heo03d8e112012-04-13 13:11:32 -0700546 tg = td_root_tg(td);
Tejun Heocd1604f2012-03-05 13:15:06 -0800547 } else {
Tejun Heo3c798392012-04-16 13:57:25 -0700548 struct blkcg_gq *blkg;
Tejun Heocd1604f2012-03-05 13:15:06 -0800549
Tejun Heo3c96cb32012-04-13 13:11:34 -0700550 blkg = blkg_lookup_create(blkcg, q);
Tejun Heocd1604f2012-03-05 13:15:06 -0800551
552 /* if %NULL and @q is alive, fall back to root_tg */
553 if (!IS_ERR(blkg))
Tejun Heo03814112012-03-05 13:15:14 -0800554 tg = blkg_to_tg(blkg);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100555 else if (!blk_queue_dying(q))
Tejun Heo03d8e112012-04-13 13:11:32 -0700556 tg = td_root_tg(td);
Vivek Goyalf469a7b2011-05-19 15:38:23 -0400557 }
558
Vivek Goyale43473b2010-09-15 17:06:35 -0400559 return tg;
560}
561
Tejun Heo0049af72013-05-14 13:52:33 -0700562static struct throtl_grp *
563throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400564{
565 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700566 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400567 return NULL;
568
Tejun Heo0049af72013-05-14 13:52:33 -0700569 if (!parent_sq->first_pending)
570 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400571
Tejun Heo0049af72013-05-14 13:52:33 -0700572 if (parent_sq->first_pending)
573 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400574
575 return NULL;
576}
577
578static void rb_erase_init(struct rb_node *n, struct rb_root *root)
579{
580 rb_erase(n, root);
581 RB_CLEAR_NODE(n);
582}
583
Tejun Heo0049af72013-05-14 13:52:33 -0700584static void throtl_rb_erase(struct rb_node *n,
585 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400586{
Tejun Heo0049af72013-05-14 13:52:33 -0700587 if (parent_sq->first_pending == n)
588 parent_sq->first_pending = NULL;
589 rb_erase_init(n, &parent_sq->pending_tree);
590 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400591}
592
Tejun Heo0049af72013-05-14 13:52:33 -0700593static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400594{
595 struct throtl_grp *tg;
596
Tejun Heo0049af72013-05-14 13:52:33 -0700597 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400598 if (!tg)
599 return;
600
Tejun Heo0049af72013-05-14 13:52:33 -0700601 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400602}
603
Tejun Heo77216b02013-05-14 13:52:36 -0700604static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400605{
Tejun Heo77216b02013-05-14 13:52:36 -0700606 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700607 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400608 struct rb_node *parent = NULL;
609 struct throtl_grp *__tg;
610 unsigned long key = tg->disptime;
611 int left = 1;
612
613 while (*node != NULL) {
614 parent = *node;
615 __tg = rb_entry_tg(parent);
616
617 if (time_before(key, __tg->disptime))
618 node = &parent->rb_left;
619 else {
620 node = &parent->rb_right;
621 left = 0;
622 }
623 }
624
625 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700626 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400627
628 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700629 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400630}
631
Tejun Heo77216b02013-05-14 13:52:36 -0700632static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400633{
Tejun Heo77216b02013-05-14 13:52:36 -0700634 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700635 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700636 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400637}
638
Tejun Heo77216b02013-05-14 13:52:36 -0700639static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400640{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700641 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700642 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400643}
644
Tejun Heo77216b02013-05-14 13:52:36 -0700645static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400646{
Tejun Heo77216b02013-05-14 13:52:36 -0700647 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700648 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400649}
650
Tejun Heo77216b02013-05-14 13:52:36 -0700651static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400652{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700653 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700654 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400655}
656
Tejun Heoa9131a22013-05-14 13:52:31 -0700657/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700658static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
659 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700660{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700661 mod_timer(&sq->pending_timer, expires);
662 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
663 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700664}
665
Tejun Heo7f52f982013-05-14 13:52:37 -0700666/**
667 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
668 * @sq: the service_queue to schedule dispatch for
669 * @force: force scheduling
670 *
671 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
672 * dispatch time of the first pending child. Returns %true if either timer
673 * is armed or there's no pending child left. %false if the current
674 * dispatch window is still open and the caller should continue
675 * dispatching.
676 *
677 * If @force is %true, the dispatch timer is always scheduled and this
678 * function is guaranteed to return %true. This is to be used when the
679 * caller can't dispatch itself and needs to invoke pending_timer
680 * unconditionally. Note that forced scheduling is likely to induce short
681 * delay before dispatch starts even if @sq->first_pending_disptime is not
682 * in the future and thus shouldn't be used in hot paths.
683 */
684static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
685 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400686{
Tejun Heo6a525602013-05-14 13:52:32 -0700687 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700688 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700689 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400690
Tejun Heoc9e03322013-05-14 13:52:32 -0700691 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400692
Tejun Heo69df0ab2013-05-14 13:52:36 -0700693 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700694 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700695 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700696 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700697 }
698
Tejun Heo7f52f982013-05-14 13:52:37 -0700699 /* tell the caller to continue dispatching */
700 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400701}
702
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700703static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
704 bool rw, unsigned long start)
705{
706 tg->bytes_disp[rw] = 0;
707 tg->io_disp[rw] = 0;
708
709 /*
710 * Previous slice has expired. We must have trimmed it after last
711 * bio dispatch. That means since start of last slice, we never used
712 * that bandwidth. Do try to make use of that bandwidth while giving
713 * credit.
714 */
715 if (time_after_eq(start, tg->slice_start[rw]))
716 tg->slice_start[rw] = start;
717
718 tg->slice_end[rw] = jiffies + throtl_slice;
719 throtl_log(&tg->service_queue,
720 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
721 rw == READ ? 'R' : 'W', tg->slice_start[rw],
722 tg->slice_end[rw], jiffies);
723}
724
Tejun Heo0f3457f2013-05-14 13:52:32 -0700725static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400726{
727 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400728 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400729 tg->slice_start[rw] = jiffies;
730 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700731 throtl_log(&tg->service_queue,
732 "[%c] new slice start=%lu end=%lu jiffies=%lu",
733 rw == READ ? 'R' : 'W', tg->slice_start[rw],
734 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400735}
736
Tejun Heo0f3457f2013-05-14 13:52:32 -0700737static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
738 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100739{
740 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
741}
742
Tejun Heo0f3457f2013-05-14 13:52:32 -0700743static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
744 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400745{
746 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700747 throtl_log(&tg->service_queue,
748 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
749 rw == READ ? 'R' : 'W', tg->slice_start[rw],
750 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400751}
752
753/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700754static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400755{
756 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200757 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400758
759 return 1;
760}
761
762/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700763static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400764{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200765 unsigned long nr_slices, time_elapsed, io_trim;
766 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400767
768 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
769
770 /*
771 * If bps are unlimited (-1), then time slice don't get
772 * renewed. Don't try to trim the slice if slice is used. A new
773 * slice will start when appropriate.
774 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700775 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400776 return;
777
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100778 /*
779 * A bio has been dispatched. Also adjust slice_end. It might happen
780 * that initially cgroup limit was very low resulting in high
781 * slice_end, but later limit was bumped up and bio was dispached
782 * sooner, then we need to reduce slice_end. A high bogus slice_end
783 * is bad because it does not allow new slice to start.
784 */
785
Tejun Heo0f3457f2013-05-14 13:52:32 -0700786 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100787
Vivek Goyale43473b2010-09-15 17:06:35 -0400788 time_elapsed = jiffies - tg->slice_start[rw];
789
790 nr_slices = time_elapsed / throtl_slice;
791
792 if (!nr_slices)
793 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200794 tmp = tg->bps[rw] * throtl_slice * nr_slices;
795 do_div(tmp, HZ);
796 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400797
Vivek Goyal8e89d132010-09-15 17:06:37 -0400798 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400799
Vivek Goyal8e89d132010-09-15 17:06:37 -0400800 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400801 return;
802
803 if (tg->bytes_disp[rw] >= bytes_trim)
804 tg->bytes_disp[rw] -= bytes_trim;
805 else
806 tg->bytes_disp[rw] = 0;
807
Vivek Goyal8e89d132010-09-15 17:06:37 -0400808 if (tg->io_disp[rw] >= io_trim)
809 tg->io_disp[rw] -= io_trim;
810 else
811 tg->io_disp[rw] = 0;
812
Vivek Goyale43473b2010-09-15 17:06:35 -0400813 tg->slice_start[rw] += nr_slices * throtl_slice;
814
Tejun Heofda6f272013-05-14 13:52:36 -0700815 throtl_log(&tg->service_queue,
816 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
817 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
818 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400819}
820
Tejun Heo0f3457f2013-05-14 13:52:32 -0700821static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
822 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400823{
824 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400825 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400826 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200827 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400828
Vivek Goyal8e89d132010-09-15 17:06:37 -0400829 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400830
Vivek Goyal8e89d132010-09-15 17:06:37 -0400831 /* Slice has just started. Consider one slice interval */
832 if (!jiffy_elapsed)
833 jiffy_elapsed_rnd = throtl_slice;
834
835 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
836
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200837 /*
838 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
839 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
840 * will allow dispatch after 1 second and after that slice should
841 * have been trimmed.
842 */
843
844 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
845 do_div(tmp, HZ);
846
847 if (tmp > UINT_MAX)
848 io_allowed = UINT_MAX;
849 else
850 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400851
852 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400853 if (wait)
854 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200855 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400856 }
857
Vivek Goyal8e89d132010-09-15 17:06:37 -0400858 /* Calc approx time to dispatch */
859 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
860
861 if (jiffy_wait > jiffy_elapsed)
862 jiffy_wait = jiffy_wait - jiffy_elapsed;
863 else
864 jiffy_wait = 1;
865
866 if (wait)
867 *wait = jiffy_wait;
868 return 0;
869}
870
Tejun Heo0f3457f2013-05-14 13:52:32 -0700871static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
872 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400873{
874 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200875 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400876 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400877
878 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
879
880 /* Slice has just started. Consider one slice interval */
881 if (!jiffy_elapsed)
882 jiffy_elapsed_rnd = throtl_slice;
883
884 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
885
Vivek Goyal5e901a22010-10-01 21:16:38 +0200886 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
887 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200888 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400889
Kent Overstreet4f024f32013-10-11 15:44:27 -0700890 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400891 if (wait)
892 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200893 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400894 }
895
896 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700897 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400898 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
899
900 if (!jiffy_wait)
901 jiffy_wait = 1;
902
903 /*
904 * This wait time is without taking into consideration the rounding
905 * up we did. Add that time also.
906 */
907 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400908 if (wait)
909 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400910 return 0;
911}
Vivek Goyale43473b2010-09-15 17:06:35 -0400912
Vivek Goyal8e89d132010-09-15 17:06:37 -0400913/*
914 * Returns whether one can dispatch a bio or not. Also returns approx number
915 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
916 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700917static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
918 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400919{
920 bool rw = bio_data_dir(bio);
921 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
922
923 /*
924 * Currently whole state machine of group depends on first bio
925 * queued in the group bio list. So one should not be calling
926 * this function with a different bio if there are other bios
927 * queued.
928 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700929 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700930 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400931
932 /* If tg->bps = -1, then BW is unlimited */
933 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
934 if (wait)
935 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200936 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400937 }
938
939 /*
940 * If previous slice expired, start a new one otherwise renew/extend
941 * existing slice to make sure it is at least throtl_slice interval
942 * long since now.
943 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700944 if (throtl_slice_used(tg, rw))
945 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400946 else {
947 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700948 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400949 }
950
Tejun Heo0f3457f2013-05-14 13:52:32 -0700951 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
952 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400953 if (wait)
954 *wait = 0;
955 return 1;
956 }
957
958 max_wait = max(bps_wait, iops_wait);
959
960 if (wait)
961 *wait = max_wait;
962
963 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700964 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400965
966 return 0;
967}
968
Tejun Heo3c798392012-04-16 13:57:25 -0700969static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700970 int rw)
971{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700972 struct throtl_grp *tg = blkg_to_tg(blkg);
973 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700974 unsigned long flags;
975
976 /* If per cpu stats are not allocated yet, don't do any accounting. */
Tejun Heo8a3d2612012-04-01 14:38:44 -0700977 if (tg->stats_cpu == NULL)
Tejun Heo629ed0b2012-04-01 14:38:44 -0700978 return;
979
980 /*
981 * Disabling interrupts to provide mutual exclusion between two
982 * writes on same cpu. It probably is not needed for 64bit. Not
983 * optimizing that case yet.
984 */
985 local_irq_save(flags);
986
Tejun Heo8a3d2612012-04-01 14:38:44 -0700987 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700988
Tejun Heo629ed0b2012-04-01 14:38:44 -0700989 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
990 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
991
992 local_irq_restore(flags);
993}
994
Vivek Goyale43473b2010-09-15 17:06:35 -0400995static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
996{
997 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400998
999 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001000 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -04001001 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -04001002
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001003 /*
1004 * REQ_THROTTLED is used to prevent the same bio to be throttled
1005 * more than once as a throttled bio will go through blk-throtl the
1006 * second time when it eventually gets issued. Set it when a bio
1007 * is being charged to a tg.
1008 *
1009 * Dispatch stats aren't recursive and each @bio should only be
1010 * accounted by the @tg it was originally associated with. Let's
1011 * update the stats when setting REQ_THROTTLED for the first time
1012 * which is guaranteed to be for the @bio's original tg.
1013 */
1014 if (!(bio->bi_rw & REQ_THROTTLED)) {
1015 bio->bi_rw |= REQ_THROTTLED;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001016 throtl_update_dispatch_stats(tg_to_blkg(tg),
1017 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001018 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001019}
1020
Tejun Heoc5cc2072013-05-14 13:52:38 -07001021/**
1022 * throtl_add_bio_tg - add a bio to the specified throtl_grp
1023 * @bio: bio to add
1024 * @qn: qnode to use
1025 * @tg: the target throtl_grp
1026 *
1027 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
1028 * tg->qnode_on_self[] is used.
1029 */
1030static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
1031 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001032{
Tejun Heo73f0d492013-05-14 13:52:35 -07001033 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001034 bool rw = bio_data_dir(bio);
1035
Tejun Heoc5cc2072013-05-14 13:52:38 -07001036 if (!qn)
1037 qn = &tg->qnode_on_self[rw];
1038
Tejun Heo0e9f4162013-05-14 13:52:35 -07001039 /*
1040 * If @tg doesn't currently have any bios queued in the same
1041 * direction, queueing @bio can change when @tg should be
1042 * dispatched. Mark that @tg was empty. This is automatically
1043 * cleaered on the next tg_update_disptime().
1044 */
1045 if (!sq->nr_queued[rw])
1046 tg->flags |= THROTL_TG_WAS_EMPTY;
1047
Tejun Heoc5cc2072013-05-14 13:52:38 -07001048 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
1049
Tejun Heo73f0d492013-05-14 13:52:35 -07001050 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -07001051 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001052}
1053
Tejun Heo77216b02013-05-14 13:52:36 -07001054static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001055{
Tejun Heo73f0d492013-05-14 13:52:35 -07001056 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001057 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
1058 struct bio *bio;
1059
Tejun Heoc5cc2072013-05-14 13:52:38 -07001060 if ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -07001061 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001062
Tejun Heoc5cc2072013-05-14 13:52:38 -07001063 if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -07001064 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001065
1066 min_wait = min(read_wait, write_wait);
1067 disptime = jiffies + min_wait;
1068
Vivek Goyale43473b2010-09-15 17:06:35 -04001069 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -07001070 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001071 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -07001072 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -07001073
1074 /* see throtl_add_bio_tg() */
1075 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -04001076}
1077
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001078static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1079 struct throtl_grp *parent_tg, bool rw)
1080{
1081 if (throtl_slice_used(parent_tg, rw)) {
1082 throtl_start_new_slice_with_credit(parent_tg, rw,
1083 child_tg->slice_start[rw]);
1084 }
1085
1086}
1087
Tejun Heo77216b02013-05-14 13:52:36 -07001088static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001089{
Tejun Heo73f0d492013-05-14 13:52:35 -07001090 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001091 struct throtl_service_queue *parent_sq = sq->parent_sq;
1092 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001093 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001094 struct bio *bio;
1095
Tejun Heoc5cc2072013-05-14 13:52:38 -07001096 /*
1097 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1098 * from @tg may put its reference and @parent_sq might end up
1099 * getting released prematurely. Remember the tg to put and put it
1100 * after @bio is transferred to @parent_sq.
1101 */
1102 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001103 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001104
1105 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001106
1107 /*
1108 * If our parent is another tg, we just need to transfer @bio to
1109 * the parent using throtl_add_bio_tg(). If our parent is
1110 * @td->service_queue, @bio is ready to be issued. Put it on its
1111 * bio_lists[] and decrease total number queued. The caller is
1112 * responsible for issuing these bios.
1113 */
1114 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001115 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001116 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001117 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001118 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1119 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001120 BUG_ON(tg->td->nr_queued[rw] <= 0);
1121 tg->td->nr_queued[rw]--;
1122 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001123
Tejun Heo0f3457f2013-05-14 13:52:32 -07001124 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001125
Tejun Heoc5cc2072013-05-14 13:52:38 -07001126 if (tg_to_put)
1127 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001128}
1129
Tejun Heo77216b02013-05-14 13:52:36 -07001130static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001131{
Tejun Heo73f0d492013-05-14 13:52:35 -07001132 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001133 unsigned int nr_reads = 0, nr_writes = 0;
1134 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001135 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001136 struct bio *bio;
1137
1138 /* Try to dispatch 75% READS and 25% WRITES */
1139
Tejun Heoc5cc2072013-05-14 13:52:38 -07001140 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001141 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001142
Tejun Heo77216b02013-05-14 13:52:36 -07001143 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001144 nr_reads++;
1145
1146 if (nr_reads >= max_nr_reads)
1147 break;
1148 }
1149
Tejun Heoc5cc2072013-05-14 13:52:38 -07001150 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001151 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001152
Tejun Heo77216b02013-05-14 13:52:36 -07001153 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001154 nr_writes++;
1155
1156 if (nr_writes >= max_nr_writes)
1157 break;
1158 }
1159
1160 return nr_reads + nr_writes;
1161}
1162
Tejun Heo651930b2013-05-14 13:52:35 -07001163static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001164{
1165 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001166
1167 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001168 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1169 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001170
1171 if (!tg)
1172 break;
1173
1174 if (time_before(jiffies, tg->disptime))
1175 break;
1176
Tejun Heo77216b02013-05-14 13:52:36 -07001177 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001178
Tejun Heo77216b02013-05-14 13:52:36 -07001179 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001180
Tejun Heo73f0d492013-05-14 13:52:35 -07001181 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001182 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001183
1184 if (nr_disp >= throtl_quantum)
1185 break;
1186 }
1187
1188 return nr_disp;
1189}
1190
Tejun Heo6e1a5702013-05-14 13:52:37 -07001191/**
1192 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1193 * @arg: the throtl_service_queue being serviced
1194 *
1195 * This timer is armed when a child throtl_grp with active bio's become
1196 * pending and queued on the service_queue's pending_tree and expires when
1197 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001198 * dispatches bio's from the children throtl_grps to the parent
1199 * service_queue.
1200 *
1201 * If the parent's parent is another throtl_grp, dispatching is propagated
1202 * by either arming its pending_timer or repeating dispatch directly. If
1203 * the top-level service_tree is reached, throtl_data->dispatch_work is
1204 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001205 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001206static void throtl_pending_timer_fn(unsigned long arg)
1207{
1208 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001209 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001210 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001211 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001212 struct throtl_service_queue *parent_sq;
1213 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001214 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001215
1216 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001217again:
1218 parent_sq = sq->parent_sq;
1219 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001220
Tejun Heo7f52f982013-05-14 13:52:37 -07001221 while (true) {
1222 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001223 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1224 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001225
Tejun Heo7f52f982013-05-14 13:52:37 -07001226 ret = throtl_select_dispatch(sq);
1227 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001228 throtl_log(sq, "bios disp=%u", ret);
1229 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001230 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001231
Tejun Heo7f52f982013-05-14 13:52:37 -07001232 if (throtl_schedule_next_dispatch(sq, false))
1233 break;
1234
1235 /* this dispatch windows is still open, relax and repeat */
1236 spin_unlock_irq(q->queue_lock);
1237 cpu_relax();
1238 spin_lock_irq(q->queue_lock);
1239 }
Tejun Heo6a525602013-05-14 13:52:32 -07001240
Tejun Heo2e48a532013-05-14 13:52:38 -07001241 if (!dispatched)
1242 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001243
Tejun Heo2e48a532013-05-14 13:52:38 -07001244 if (parent_sq) {
1245 /* @parent_sq is another throl_grp, propagate dispatch */
1246 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1247 tg_update_disptime(tg);
1248 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1249 /* window is already open, repeat dispatching */
1250 sq = parent_sq;
1251 tg = sq_to_tg(sq);
1252 goto again;
1253 }
1254 }
1255 } else {
1256 /* reached the top-level, queue issueing */
1257 queue_work(kthrotld_workqueue, &td->dispatch_work);
1258 }
1259out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001260 spin_unlock_irq(q->queue_lock);
1261}
1262
1263/**
1264 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1265 * @work: work item being executed
1266 *
1267 * This function is queued for execution when bio's reach the bio_lists[]
1268 * of throtl_data->service_queue. Those bio's are ready and issued by this
1269 * function.
1270 */
Fabian Frederick8876e1402014-04-17 21:41:16 +02001271static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001272{
1273 struct throtl_data *td = container_of(work, struct throtl_data,
1274 dispatch_work);
1275 struct throtl_service_queue *td_sq = &td->service_queue;
1276 struct request_queue *q = td->queue;
1277 struct bio_list bio_list_on_stack;
1278 struct bio *bio;
1279 struct blk_plug plug;
1280 int rw;
1281
1282 bio_list_init(&bio_list_on_stack);
1283
1284 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001285 for (rw = READ; rw <= WRITE; rw++)
1286 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1287 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001288 spin_unlock_irq(q->queue_lock);
1289
Tejun Heo6e1a5702013-05-14 13:52:37 -07001290 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001291 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001292 while((bio = bio_list_pop(&bio_list_on_stack)))
1293 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001294 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001295 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001296}
1297
Tejun Heof95a04a2012-04-16 13:57:26 -07001298static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1299 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -07001300{
Tejun Heof95a04a2012-04-16 13:57:26 -07001301 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -07001302 struct blkg_rwstat rwstat = { }, tmp;
1303 int i, cpu;
1304
Thadeu Lima de Souza Cascardo045c47c2015-02-16 17:16:45 -02001305 if (tg->stats_cpu == NULL)
1306 return 0;
1307
Tejun Heo41b38b62012-04-01 14:38:44 -07001308 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -07001309 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -07001310
1311 tmp = blkg_rwstat_read((void *)sc + off);
1312 for (i = 0; i < BLKG_RWSTAT_NR; i++)
1313 rwstat.cnt[i] += tmp.cnt[i];
1314 }
1315
Tejun Heof95a04a2012-04-16 13:57:26 -07001316 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -07001317}
1318
Tejun Heo2da8ca82013-12-05 12:28:04 -05001319static int tg_print_cpu_rwstat(struct seq_file *sf, void *v)
Tejun Heo41b38b62012-04-01 14:38:44 -07001320{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001321 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat,
1322 &blkcg_policy_throtl, seq_cft(sf)->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -07001323 return 0;
1324}
1325
Tejun Heof95a04a2012-04-16 13:57:26 -07001326static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1327 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001328{
Tejun Heof95a04a2012-04-16 13:57:26 -07001329 struct throtl_grp *tg = pd_to_tg(pd);
1330 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001331
Tejun Heoaf133ce2012-04-01 14:38:44 -07001332 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001333 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001334 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001335}
1336
Tejun Heof95a04a2012-04-16 13:57:26 -07001337static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1338 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001339{
Tejun Heof95a04a2012-04-16 13:57:26 -07001340 struct throtl_grp *tg = pd_to_tg(pd);
1341 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001342
1343 if (v == -1)
1344 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001345 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001346}
1347
Tejun Heo2da8ca82013-12-05 12:28:04 -05001348static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001349{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001350 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1351 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001352 return 0;
1353}
1354
Tejun Heo2da8ca82013-12-05 12:28:04 -05001355static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001356{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001357 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1358 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001359 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001360}
1361
Tejun Heo451af502014-05-13 12:16:21 -04001362static ssize_t tg_set_conf(struct kernfs_open_file *of,
1363 char *buf, size_t nbytes, loff_t off, bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001364{
Tejun Heo451af502014-05-13 12:16:21 -04001365 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001366 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001367 struct throtl_grp *tg;
Tejun Heo69df0ab2013-05-14 13:52:36 -07001368 struct throtl_service_queue *sq;
Tejun Heo693e7512013-05-14 13:52:38 -07001369 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001370 struct cgroup_subsys_state *pos_css;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001371 int ret;
1372
Tejun Heo3c798392012-04-16 13:57:25 -07001373 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001374 if (ret)
1375 return ret;
1376
Tejun Heoaf133ce2012-04-01 14:38:44 -07001377 tg = blkg_to_tg(ctx.blkg);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001378 sq = &tg->service_queue;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001379
Tejun Heoa2b16932012-04-13 13:11:33 -07001380 if (!ctx.v)
1381 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001382
Tejun Heoa2b16932012-04-13 13:11:33 -07001383 if (is_u64)
Tejun Heo451af502014-05-13 12:16:21 -04001384 *(u64 *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoa2b16932012-04-13 13:11:33 -07001385 else
Tejun Heo451af502014-05-13 12:16:21 -04001386 *(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001387
Tejun Heofda6f272013-05-14 13:52:36 -07001388 throtl_log(&tg->service_queue,
1389 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1390 tg->bps[READ], tg->bps[WRITE],
1391 tg->iops[READ], tg->iops[WRITE]);
Tejun Heo632b4492013-05-14 13:52:31 -07001392
1393 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001394 * Update has_rules[] flags for the updated tg's subtree. A tg is
1395 * considered to have rules if either the tg itself or any of its
1396 * ancestors has rules. This identifies groups without any
1397 * restrictions in the whole hierarchy and allows them to bypass
1398 * blk-throttle.
1399 */
Tejun Heo492eb212013-08-08 20:11:25 -04001400 blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg)
Tejun Heo693e7512013-05-14 13:52:38 -07001401 tg_update_has_rules(blkg_to_tg(blkg));
1402
1403 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001404 * We're already holding queue_lock and know @tg is valid. Let's
1405 * apply the new config directly.
1406 *
1407 * Restart the slices for both READ and WRITES. It might happen
1408 * that a group's limit are dropped suddenly and we don't want to
1409 * account recently dispatched IO with new low rate.
1410 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001411 throtl_start_new_slice(tg, 0);
1412 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001413
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001414 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001415 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001416 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001417 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001418
1419 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001420 return nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001421}
1422
Tejun Heo451af502014-05-13 12:16:21 -04001423static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1424 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001425{
Tejun Heo451af502014-05-13 12:16:21 -04001426 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001427}
1428
Tejun Heo451af502014-05-13 12:16:21 -04001429static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1430 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001431{
Tejun Heo451af502014-05-13 12:16:21 -04001432 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001433}
1434
1435static struct cftype throtl_files[] = {
1436 {
1437 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001438 .private = offsetof(struct throtl_grp, bps[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001439 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001440 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001441 },
1442 {
1443 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001444 .private = offsetof(struct throtl_grp, bps[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001445 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001446 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001447 },
1448 {
1449 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001450 .private = offsetof(struct throtl_grp, iops[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001451 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001452 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001453 },
1454 {
1455 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001456 .private = offsetof(struct throtl_grp, iops[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001457 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001458 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001459 },
1460 {
1461 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001462 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001463 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001464 },
1465 {
1466 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001467 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001468 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001469 },
1470 { } /* terminate */
1471};
1472
Vivek Goyalda527772011-03-02 19:05:33 -05001473static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001474{
1475 struct throtl_data *td = q->td;
1476
Tejun Heo69df0ab2013-05-14 13:52:36 -07001477 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001478}
1479
Tejun Heo3c798392012-04-16 13:57:25 -07001480static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001481 .cftypes = throtl_files,
1482
Tejun Heo001bea72015-08-18 14:55:11 -07001483 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001484 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001485 .pd_online_fn = throtl_pd_online,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001486 .pd_exit_fn = throtl_pd_exit,
Tejun Heo001bea72015-08-18 14:55:11 -07001487 .pd_free_fn = throtl_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001488 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001489};
1490
Tejun Heobc16a4f2011-10-19 14:33:01 +02001491bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001492{
1493 struct throtl_data *td = q->td;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001494 struct throtl_qnode *qn = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001495 struct throtl_grp *tg;
Tejun Heo73f0d492013-05-14 13:52:35 -07001496 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001497 bool rw = bio_data_dir(bio);
Tejun Heo3c798392012-04-16 13:57:25 -07001498 struct blkcg *blkcg;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001499 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001500
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001501 /* see throtl_charge_bio() */
1502 if (bio->bi_rw & REQ_THROTTLED)
Tejun Heobc16a4f2011-10-19 14:33:01 +02001503 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001504
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001505 /*
1506 * A throtl_grp pointer retrieved under rcu can be used to access
1507 * basic fields like stats and io rates. If a group has no rules,
1508 * just update the dispatch stats in lockless manner and return.
1509 */
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001510 rcu_read_lock();
Tejun Heo3c798392012-04-16 13:57:25 -07001511 blkcg = bio_blkcg(bio);
Tejun Heocd1604f2012-03-05 13:15:06 -08001512 tg = throtl_lookup_tg(td, blkcg);
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001513 if (tg) {
Tejun Heo693e7512013-05-14 13:52:38 -07001514 if (!tg->has_rules[rw]) {
Tejun Heo629ed0b2012-04-01 14:38:44 -07001515 throtl_update_dispatch_stats(tg_to_blkg(tg),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001516 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001517 goto out_unlock_rcu;
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001518 }
1519 }
Vivek Goyalaf75cd32011-05-19 15:38:31 -04001520
1521 /*
1522 * Either group has not been allocated yet or it is not an unlimited
1523 * IO group
1524 */
Vivek Goyale43473b2010-09-15 17:06:35 -04001525 spin_lock_irq(q->queue_lock);
Tejun Heocd1604f2012-03-05 13:15:06 -08001526 tg = throtl_lookup_create_tg(td, blkcg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001527 if (unlikely(!tg))
1528 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001529
Tejun Heo73f0d492013-05-14 13:52:35 -07001530 sq = &tg->service_queue;
1531
Tejun Heo9e660ac2013-05-14 13:52:38 -07001532 while (true) {
1533 /* throtl is FIFO - if bios are already queued, should queue */
1534 if (sq->nr_queued[rw])
1535 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001536
Tejun Heo9e660ac2013-05-14 13:52:38 -07001537 /* if above limits, break to queue */
1538 if (!tg_may_dispatch(tg, bio, NULL))
1539 break;
1540
1541 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001542 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001543
1544 /*
1545 * We need to trim slice even when bios are not being queued
1546 * otherwise it might happen that a bio is not queued for
1547 * a long time and slice keeps on extending and trim is not
1548 * called for a long time. Now if limits are reduced suddenly
1549 * we take into account all the IO dispatched so far at new
1550 * low rate and * newly queued IO gets a really long dispatch
1551 * time.
1552 *
1553 * So keep on trimming slice even if bio is not queued.
1554 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001555 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001556
1557 /*
1558 * @bio passed through this layer without being throttled.
1559 * Climb up the ladder. If we''re already at the top, it
1560 * can be executed directly.
1561 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001562 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001563 sq = sq->parent_sq;
1564 tg = sq_to_tg(sq);
1565 if (!tg)
1566 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001567 }
1568
Tejun Heo9e660ac2013-05-14 13:52:38 -07001569 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001570 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1571 rw == READ ? 'R' : 'W',
Kent Overstreet4f024f32013-10-11 15:44:27 -07001572 tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
Tejun Heofda6f272013-05-14 13:52:36 -07001573 tg->io_disp[rw], tg->iops[rw],
1574 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001575
Tejun Heo671058f2012-03-05 13:15:29 -08001576 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001577 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001578 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001579 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001580
Tejun Heo7f52f982013-05-14 13:52:37 -07001581 /*
1582 * Update @tg's dispatch time and force schedule dispatch if @tg
1583 * was empty before @bio. The forced scheduling isn't likely to
1584 * cause undue delay as @bio is likely to be dispatched directly if
1585 * its @tg's disptime is not in the future.
1586 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001587 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001588 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001589 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001590 }
1591
Tejun Heobc16a4f2011-10-19 14:33:01 +02001592out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001593 spin_unlock_irq(q->queue_lock);
Tejun Heo2a7f1242012-03-05 13:15:01 -08001594out_unlock_rcu:
1595 rcu_read_unlock();
Tejun Heobc16a4f2011-10-19 14:33:01 +02001596out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001597 /*
1598 * As multiple blk-throtls may stack in the same issue path, we
1599 * don't want bios to leave with the flag set. Clear the flag if
1600 * being issued.
1601 */
1602 if (!throttled)
1603 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001604 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001605}
1606
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001607/*
1608 * Dispatch all bios from all children tg's queued on @parent_sq. On
1609 * return, @parent_sq is guaranteed to not have any active children tg's
1610 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1611 */
1612static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1613{
1614 struct throtl_grp *tg;
1615
1616 while ((tg = throtl_rb_first(parent_sq))) {
1617 struct throtl_service_queue *sq = &tg->service_queue;
1618 struct bio *bio;
1619
1620 throtl_dequeue_tg(tg);
1621
Tejun Heoc5cc2072013-05-14 13:52:38 -07001622 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001623 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001624 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001625 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1626 }
1627}
1628
Tejun Heoc9a929d2011-10-19 14:42:16 +02001629/**
1630 * blk_throtl_drain - drain throttled bios
1631 * @q: request_queue to drain throttled bios for
1632 *
1633 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1634 */
1635void blk_throtl_drain(struct request_queue *q)
1636 __releases(q->queue_lock) __acquires(q->queue_lock)
1637{
1638 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001639 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001640 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001641 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001642 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001643
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001644 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001645 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001646
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001647 /*
1648 * Drain each tg while doing post-order walk on the blkg tree, so
1649 * that all bios are propagated to td->service_queue. It'd be
1650 * better to walk service_queue tree directly but blkg walk is
1651 * easier.
1652 */
Tejun Heo492eb212013-08-08 20:11:25 -04001653 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001654 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001655
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001656 /* finally, transfer bios from top-level tg's into the td */
1657 tg_drain_bios(&td->service_queue);
1658
1659 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001660 spin_unlock_irq(q->queue_lock);
1661
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001662 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001663 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001664 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1665 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001666 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001667
1668 spin_lock_irq(q->queue_lock);
1669}
1670
Vivek Goyale43473b2010-09-15 17:06:35 -04001671int blk_throtl_init(struct request_queue *q)
1672{
1673 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001674 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001675
1676 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1677 if (!td)
1678 return -ENOMEM;
1679
Tejun Heo69df0ab2013-05-14 13:52:36 -07001680 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heo77216b02013-05-14 13:52:36 -07001681 throtl_service_queue_init(&td->service_queue, NULL);
Vivek Goyale43473b2010-09-15 17:06:35 -04001682
Tejun Heocd1604f2012-03-05 13:15:06 -08001683 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001684 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001685
Tejun Heoa2b16932012-04-13 13:11:33 -07001686 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001687 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001688 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001689 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001690 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001691}
1692
1693void blk_throtl_exit(struct request_queue *q)
1694{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001695 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001696 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001697 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001698 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001699}
1700
1701static int __init throtl_init(void)
1702{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001703 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1704 if (!kthrotld_workqueue)
1705 panic("Failed to create kthrotld\n");
1706
Tejun Heo3c798392012-04-16 13:57:25 -07001707 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001708}
1709
1710module_init(throtl_init);