blob: 29c22ed4b0730c90bf3923ff09e20484ba1f1a0a [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;
Vivek Goyale43473b2010-09-15 17:06:35 -0400147};
148
149struct throtl_data
150{
Vivek Goyale43473b2010-09-15 17:06:35 -0400151 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700152 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400153
Vivek Goyale43473b2010-09-15 17:06:35 -0400154 struct request_queue *queue;
155
156 /* Total Number of queued bios on READ and WRITE lists */
157 unsigned int nr_queued[2];
158
159 /*
Vivek Goyal02977e42010-10-01 14:49:48 +0200160 * number of total undestroyed groups
Vivek Goyale43473b2010-09-15 17:06:35 -0400161 */
162 unsigned int nr_undestroyed_grps;
163
164 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700165 struct work_struct dispatch_work;
Vivek Goyale43473b2010-09-15 17:06:35 -0400166};
167
Tejun Heo69df0ab2013-05-14 13:52:36 -0700168static void throtl_pending_timer_fn(unsigned long arg);
169
Tejun Heof95a04a2012-04-16 13:57:26 -0700170static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
171{
172 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
173}
174
Tejun Heo3c798392012-04-16 13:57:25 -0700175static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800176{
Tejun Heof95a04a2012-04-16 13:57:26 -0700177 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800178}
179
Tejun Heo3c798392012-04-16 13:57:25 -0700180static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800181{
Tejun Heof95a04a2012-04-16 13:57:26 -0700182 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800183}
184
Tejun Heofda6f272013-05-14 13:52:36 -0700185/**
186 * sq_to_tg - return the throl_grp the specified service queue belongs to
187 * @sq: the throtl_service_queue of interest
188 *
189 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
190 * embedded in throtl_data, %NULL is returned.
191 */
192static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
193{
194 if (sq && sq->parent_sq)
195 return container_of(sq, struct throtl_grp, service_queue);
196 else
197 return NULL;
198}
Vivek Goyale43473b2010-09-15 17:06:35 -0400199
Tejun Heofda6f272013-05-14 13:52:36 -0700200/**
201 * sq_to_td - return throtl_data the specified service queue belongs to
202 * @sq: the throtl_service_queue of interest
203 *
204 * A service_queue can be embeded in either a throtl_grp or throtl_data.
205 * Determine the associated throtl_data accordingly and return it.
206 */
207static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
208{
209 struct throtl_grp *tg = sq_to_tg(sq);
210
211 if (tg)
212 return tg->td;
213 else
214 return container_of(sq, struct throtl_data, service_queue);
215}
216
217/**
218 * throtl_log - log debug message via blktrace
219 * @sq: the service_queue being reported
220 * @fmt: printf format string
221 * @args: printf args
222 *
223 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
224 * throtl_grp; otherwise, just "throtl".
225 *
226 * TODO: this should be made a function and name formatting should happen
227 * after testing whether blktrace is enabled.
228 */
229#define throtl_log(sq, fmt, args...) do { \
230 struct throtl_grp *__tg = sq_to_tg((sq)); \
231 struct throtl_data *__td = sq_to_td((sq)); \
232 \
233 (void)__td; \
234 if ((__tg)) { \
235 char __pbuf[128]; \
236 \
237 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
238 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
239 } else { \
240 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
241 } \
242} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400243
Tejun Heoc5cc2072013-05-14 13:52:38 -0700244static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
245{
246 INIT_LIST_HEAD(&qn->node);
247 bio_list_init(&qn->bios);
248 qn->tg = tg;
249}
250
251/**
252 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
253 * @bio: bio being added
254 * @qn: qnode to add bio to
255 * @queued: the service_queue->queued[] list @qn belongs to
256 *
257 * Add @bio to @qn and put @qn on @queued if it's not already on.
258 * @qn->tg's reference count is bumped when @qn is activated. See the
259 * comment on top of throtl_qnode definition for details.
260 */
261static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
262 struct list_head *queued)
263{
264 bio_list_add(&qn->bios, bio);
265 if (list_empty(&qn->node)) {
266 list_add_tail(&qn->node, queued);
267 blkg_get(tg_to_blkg(qn->tg));
268 }
269}
270
271/**
272 * throtl_peek_queued - peek the first bio on a qnode list
273 * @queued: the qnode list to peek
274 */
275static struct bio *throtl_peek_queued(struct list_head *queued)
276{
277 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
278 struct bio *bio;
279
280 if (list_empty(queued))
281 return NULL;
282
283 bio = bio_list_peek(&qn->bios);
284 WARN_ON_ONCE(!bio);
285 return bio;
286}
287
288/**
289 * throtl_pop_queued - pop the first bio form a qnode list
290 * @queued: the qnode list to pop a bio from
291 * @tg_to_put: optional out argument for throtl_grp to put
292 *
293 * Pop the first bio from the qnode list @queued. After popping, the first
294 * qnode is removed from @queued if empty or moved to the end of @queued so
295 * that the popping order is round-robin.
296 *
297 * When the first qnode is removed, its associated throtl_grp should be put
298 * too. If @tg_to_put is NULL, this function automatically puts it;
299 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
300 * responsible for putting it.
301 */
302static struct bio *throtl_pop_queued(struct list_head *queued,
303 struct throtl_grp **tg_to_put)
304{
305 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
306 struct bio *bio;
307
308 if (list_empty(queued))
309 return NULL;
310
311 bio = bio_list_pop(&qn->bios);
312 WARN_ON_ONCE(!bio);
313
314 if (bio_list_empty(&qn->bios)) {
315 list_del_init(&qn->node);
316 if (tg_to_put)
317 *tg_to_put = qn->tg;
318 else
319 blkg_put(tg_to_blkg(qn->tg));
320 } else {
321 list_move_tail(&qn->node, queued);
322 }
323
324 return bio;
325}
326
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700327/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700328static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700329{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700330 INIT_LIST_HEAD(&sq->queued[0]);
331 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700332 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700333 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
334 (unsigned long)sq);
335}
336
Tejun Heo001bea72015-08-18 14:55:11 -0700337static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
338{
Tejun Heo4fb72032015-08-18 14:55:12 -0700339 struct throtl_grp *tg;
Tejun Heob2ce2642015-08-18 14:55:13 -0700340 int rw, cpu;
Tejun Heo4fb72032015-08-18 14:55:12 -0700341
342 tg = kzalloc_node(sizeof(*tg), gfp, node);
343 if (!tg)
344 return NULL;
345
346 tg->stats_cpu = alloc_percpu_gfp(struct tg_stats_cpu, gfp);
347 if (!tg->stats_cpu) {
348 kfree(tg);
349 return NULL;
350 }
351
Tejun Heob2ce2642015-08-18 14:55:13 -0700352 throtl_service_queue_init(&tg->service_queue);
353
354 for (rw = READ; rw <= WRITE; rw++) {
355 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
356 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
357 }
358
359 RB_CLEAR_NODE(&tg->rb_node);
360 tg->bps[READ] = -1;
361 tg->bps[WRITE] = -1;
362 tg->iops[READ] = -1;
363 tg->iops[WRITE] = -1;
364
Tejun Heo4fb72032015-08-18 14:55:12 -0700365 for_each_possible_cpu(cpu) {
366 struct tg_stats_cpu *stats_cpu = per_cpu_ptr(tg->stats_cpu, cpu);
367
368 blkg_rwstat_init(&stats_cpu->service_bytes);
369 blkg_rwstat_init(&stats_cpu->serviced);
370 }
371
372 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700373}
374
Tejun Heoa9520cd2015-08-18 14:55:14 -0700375static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400376{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700377 struct throtl_grp *tg = pd_to_tg(pd);
378 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700379 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700380 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800381
Tejun Heo91381252013-05-14 13:52:38 -0700382 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400383 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700384 * behavior where limits on a given throtl_grp are applied to the
385 * whole subtree rather than just the group itself. e.g. If 16M
386 * read_bps limit is set on the root group, the whole system can't
387 * exceed 16M for the device.
388 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400389 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700390 * behavior is retained where all throtl_grps are treated as if
391 * they're all separate root groups right below throtl_data.
392 * Limits of a group don't interact with limits of other groups
393 * regardless of the position of the group in the hierarchy.
394 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700395 sq->parent_sq = &td->service_queue;
Tejun Heoaa6ec292014-07-09 10:08:08 -0400396 if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700397 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700398 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700399}
400
Tejun Heo693e7512013-05-14 13:52:38 -0700401/*
402 * Set has_rules[] if @tg or any of its parents have limits configured.
403 * This doesn't require walking up to the top of the hierarchy as the
404 * parent's has_rules[] is guaranteed to be correct.
405 */
406static void tg_update_has_rules(struct throtl_grp *tg)
407{
408 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
409 int rw;
410
411 for (rw = READ; rw <= WRITE; rw++)
412 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
413 (tg->bps[rw] != -1 || tg->iops[rw] != -1);
414}
415
Tejun Heoa9520cd2015-08-18 14:55:14 -0700416static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700417{
418 /*
419 * We don't want new groups to escape the limits of its ancestors.
420 * Update has_rules[] after a new group is brought online.
421 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700422 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700423}
424
Tejun Heo001bea72015-08-18 14:55:11 -0700425static void throtl_pd_free(struct blkg_policy_data *pd)
426{
Tejun Heo4fb72032015-08-18 14:55:12 -0700427 struct throtl_grp *tg = pd_to_tg(pd);
428
Tejun Heob2ce2642015-08-18 14:55:13 -0700429 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700430 free_percpu(tg->stats_cpu);
431 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700432}
433
Tejun Heoa9520cd2015-08-18 14:55:14 -0700434static void throtl_pd_reset_stats(struct blkg_policy_data *pd)
Tejun Heo8a3d2612012-04-01 14:38:44 -0700435{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700436 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo8a3d2612012-04-01 14:38:44 -0700437 int cpu;
438
Tejun Heo8a3d2612012-04-01 14:38:44 -0700439 for_each_possible_cpu(cpu) {
440 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
441
442 blkg_rwstat_reset(&sc->service_bytes);
443 blkg_rwstat_reset(&sc->serviced);
444 }
Vivek Goyala29a1712011-05-19 15:38:19 -0400445}
446
Tejun Heo0049af72013-05-14 13:52:33 -0700447static struct throtl_grp *
448throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400449{
450 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700451 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400452 return NULL;
453
Tejun Heo0049af72013-05-14 13:52:33 -0700454 if (!parent_sq->first_pending)
455 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400456
Tejun Heo0049af72013-05-14 13:52:33 -0700457 if (parent_sq->first_pending)
458 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400459
460 return NULL;
461}
462
463static void rb_erase_init(struct rb_node *n, struct rb_root *root)
464{
465 rb_erase(n, root);
466 RB_CLEAR_NODE(n);
467}
468
Tejun Heo0049af72013-05-14 13:52:33 -0700469static void throtl_rb_erase(struct rb_node *n,
470 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400471{
Tejun Heo0049af72013-05-14 13:52:33 -0700472 if (parent_sq->first_pending == n)
473 parent_sq->first_pending = NULL;
474 rb_erase_init(n, &parent_sq->pending_tree);
475 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400476}
477
Tejun Heo0049af72013-05-14 13:52:33 -0700478static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400479{
480 struct throtl_grp *tg;
481
Tejun Heo0049af72013-05-14 13:52:33 -0700482 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400483 if (!tg)
484 return;
485
Tejun Heo0049af72013-05-14 13:52:33 -0700486 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400487}
488
Tejun Heo77216b02013-05-14 13:52:36 -0700489static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400490{
Tejun Heo77216b02013-05-14 13:52:36 -0700491 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700492 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400493 struct rb_node *parent = NULL;
494 struct throtl_grp *__tg;
495 unsigned long key = tg->disptime;
496 int left = 1;
497
498 while (*node != NULL) {
499 parent = *node;
500 __tg = rb_entry_tg(parent);
501
502 if (time_before(key, __tg->disptime))
503 node = &parent->rb_left;
504 else {
505 node = &parent->rb_right;
506 left = 0;
507 }
508 }
509
510 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700511 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400512
513 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700514 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400515}
516
Tejun Heo77216b02013-05-14 13:52:36 -0700517static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400518{
Tejun Heo77216b02013-05-14 13:52:36 -0700519 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700520 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700521 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400522}
523
Tejun Heo77216b02013-05-14 13:52:36 -0700524static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400525{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700526 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700527 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400528}
529
Tejun Heo77216b02013-05-14 13:52:36 -0700530static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400531{
Tejun Heo77216b02013-05-14 13:52:36 -0700532 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700533 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400534}
535
Tejun Heo77216b02013-05-14 13:52:36 -0700536static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400537{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700538 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700539 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400540}
541
Tejun Heoa9131a22013-05-14 13:52:31 -0700542/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700543static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
544 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700545{
Tejun Heo69df0ab2013-05-14 13:52:36 -0700546 mod_timer(&sq->pending_timer, expires);
547 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
548 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700549}
550
Tejun Heo7f52f982013-05-14 13:52:37 -0700551/**
552 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
553 * @sq: the service_queue to schedule dispatch for
554 * @force: force scheduling
555 *
556 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
557 * dispatch time of the first pending child. Returns %true if either timer
558 * is armed or there's no pending child left. %false if the current
559 * dispatch window is still open and the caller should continue
560 * dispatching.
561 *
562 * If @force is %true, the dispatch timer is always scheduled and this
563 * function is guaranteed to return %true. This is to be used when the
564 * caller can't dispatch itself and needs to invoke pending_timer
565 * unconditionally. Note that forced scheduling is likely to induce short
566 * delay before dispatch starts even if @sq->first_pending_disptime is not
567 * in the future and thus shouldn't be used in hot paths.
568 */
569static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
570 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400571{
Tejun Heo6a525602013-05-14 13:52:32 -0700572 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700573 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700574 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400575
Tejun Heoc9e03322013-05-14 13:52:32 -0700576 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400577
Tejun Heo69df0ab2013-05-14 13:52:36 -0700578 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700579 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700580 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700581 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700582 }
583
Tejun Heo7f52f982013-05-14 13:52:37 -0700584 /* tell the caller to continue dispatching */
585 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400586}
587
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700588static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
589 bool rw, unsigned long start)
590{
591 tg->bytes_disp[rw] = 0;
592 tg->io_disp[rw] = 0;
593
594 /*
595 * Previous slice has expired. We must have trimmed it after last
596 * bio dispatch. That means since start of last slice, we never used
597 * that bandwidth. Do try to make use of that bandwidth while giving
598 * credit.
599 */
600 if (time_after_eq(start, tg->slice_start[rw]))
601 tg->slice_start[rw] = start;
602
603 tg->slice_end[rw] = jiffies + throtl_slice;
604 throtl_log(&tg->service_queue,
605 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
606 rw == READ ? 'R' : 'W', tg->slice_start[rw],
607 tg->slice_end[rw], jiffies);
608}
609
Tejun Heo0f3457f2013-05-14 13:52:32 -0700610static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400611{
612 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400613 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400614 tg->slice_start[rw] = jiffies;
615 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700616 throtl_log(&tg->service_queue,
617 "[%c] new slice start=%lu end=%lu jiffies=%lu",
618 rw == READ ? 'R' : 'W', tg->slice_start[rw],
619 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400620}
621
Tejun Heo0f3457f2013-05-14 13:52:32 -0700622static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
623 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100624{
625 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
626}
627
Tejun Heo0f3457f2013-05-14 13:52:32 -0700628static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
629 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400630{
631 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700632 throtl_log(&tg->service_queue,
633 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
634 rw == READ ? 'R' : 'W', tg->slice_start[rw],
635 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400636}
637
638/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700639static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400640{
641 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200642 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400643
644 return 1;
645}
646
647/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700648static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400649{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200650 unsigned long nr_slices, time_elapsed, io_trim;
651 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400652
653 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
654
655 /*
656 * If bps are unlimited (-1), then time slice don't get
657 * renewed. Don't try to trim the slice if slice is used. A new
658 * slice will start when appropriate.
659 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700660 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400661 return;
662
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100663 /*
664 * A bio has been dispatched. Also adjust slice_end. It might happen
665 * that initially cgroup limit was very low resulting in high
666 * slice_end, but later limit was bumped up and bio was dispached
667 * sooner, then we need to reduce slice_end. A high bogus slice_end
668 * is bad because it does not allow new slice to start.
669 */
670
Tejun Heo0f3457f2013-05-14 13:52:32 -0700671 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100672
Vivek Goyale43473b2010-09-15 17:06:35 -0400673 time_elapsed = jiffies - tg->slice_start[rw];
674
675 nr_slices = time_elapsed / throtl_slice;
676
677 if (!nr_slices)
678 return;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200679 tmp = tg->bps[rw] * throtl_slice * nr_slices;
680 do_div(tmp, HZ);
681 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400682
Vivek Goyal8e89d132010-09-15 17:06:37 -0400683 io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400684
Vivek Goyal8e89d132010-09-15 17:06:37 -0400685 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400686 return;
687
688 if (tg->bytes_disp[rw] >= bytes_trim)
689 tg->bytes_disp[rw] -= bytes_trim;
690 else
691 tg->bytes_disp[rw] = 0;
692
Vivek Goyal8e89d132010-09-15 17:06:37 -0400693 if (tg->io_disp[rw] >= io_trim)
694 tg->io_disp[rw] -= io_trim;
695 else
696 tg->io_disp[rw] = 0;
697
Vivek Goyale43473b2010-09-15 17:06:35 -0400698 tg->slice_start[rw] += nr_slices * throtl_slice;
699
Tejun Heofda6f272013-05-14 13:52:36 -0700700 throtl_log(&tg->service_queue,
701 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
702 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
703 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400704}
705
Tejun Heo0f3457f2013-05-14 13:52:32 -0700706static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
707 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400708{
709 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400710 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400711 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200712 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400713
Vivek Goyal8e89d132010-09-15 17:06:37 -0400714 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400715
Vivek Goyal8e89d132010-09-15 17:06:37 -0400716 /* Slice has just started. Consider one slice interval */
717 if (!jiffy_elapsed)
718 jiffy_elapsed_rnd = throtl_slice;
719
720 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
721
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200722 /*
723 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
724 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
725 * will allow dispatch after 1 second and after that slice should
726 * have been trimmed.
727 */
728
729 tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
730 do_div(tmp, HZ);
731
732 if (tmp > UINT_MAX)
733 io_allowed = UINT_MAX;
734 else
735 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400736
737 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400738 if (wait)
739 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200740 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400741 }
742
Vivek Goyal8e89d132010-09-15 17:06:37 -0400743 /* Calc approx time to dispatch */
744 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
745
746 if (jiffy_wait > jiffy_elapsed)
747 jiffy_wait = jiffy_wait - jiffy_elapsed;
748 else
749 jiffy_wait = 1;
750
751 if (wait)
752 *wait = jiffy_wait;
753 return 0;
754}
755
Tejun Heo0f3457f2013-05-14 13:52:32 -0700756static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
757 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400758{
759 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200760 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400761 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400762
763 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
764
765 /* Slice has just started. Consider one slice interval */
766 if (!jiffy_elapsed)
767 jiffy_elapsed_rnd = throtl_slice;
768
769 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
770
Vivek Goyal5e901a22010-10-01 21:16:38 +0200771 tmp = tg->bps[rw] * jiffy_elapsed_rnd;
772 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200773 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400774
Kent Overstreet4f024f32013-10-11 15:44:27 -0700775 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400776 if (wait)
777 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200778 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400779 }
780
781 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700782 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400783 jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
784
785 if (!jiffy_wait)
786 jiffy_wait = 1;
787
788 /*
789 * This wait time is without taking into consideration the rounding
790 * up we did. Add that time also.
791 */
792 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400793 if (wait)
794 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400795 return 0;
796}
Vivek Goyale43473b2010-09-15 17:06:35 -0400797
Vivek Goyal8e89d132010-09-15 17:06:37 -0400798/*
799 * Returns whether one can dispatch a bio or not. Also returns approx number
800 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
801 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700802static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
803 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400804{
805 bool rw = bio_data_dir(bio);
806 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
807
808 /*
809 * Currently whole state machine of group depends on first bio
810 * queued in the group bio list. So one should not be calling
811 * this function with a different bio if there are other bios
812 * queued.
813 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700814 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700815 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400816
817 /* If tg->bps = -1, then BW is unlimited */
818 if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
819 if (wait)
820 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200821 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400822 }
823
824 /*
825 * If previous slice expired, start a new one otherwise renew/extend
826 * existing slice to make sure it is at least throtl_slice interval
827 * long since now.
828 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700829 if (throtl_slice_used(tg, rw))
830 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400831 else {
832 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700833 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400834 }
835
Tejun Heo0f3457f2013-05-14 13:52:32 -0700836 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
837 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400838 if (wait)
839 *wait = 0;
840 return 1;
841 }
842
843 max_wait = max(bps_wait, iops_wait);
844
845 if (wait)
846 *wait = max_wait;
847
848 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700849 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400850
851 return 0;
852}
853
Tejun Heo3c798392012-04-16 13:57:25 -0700854static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
Tejun Heo629ed0b2012-04-01 14:38:44 -0700855 int rw)
856{
Tejun Heo8a3d2612012-04-01 14:38:44 -0700857 struct throtl_grp *tg = blkg_to_tg(blkg);
858 struct tg_stats_cpu *stats_cpu;
Tejun Heo629ed0b2012-04-01 14:38:44 -0700859 unsigned long flags;
860
Tejun Heo629ed0b2012-04-01 14:38:44 -0700861 /*
862 * Disabling interrupts to provide mutual exclusion between two
863 * writes on same cpu. It probably is not needed for 64bit. Not
864 * optimizing that case yet.
865 */
866 local_irq_save(flags);
867
Tejun Heo8a3d2612012-04-01 14:38:44 -0700868 stats_cpu = this_cpu_ptr(tg->stats_cpu);
Tejun Heo629ed0b2012-04-01 14:38:44 -0700869
Tejun Heo629ed0b2012-04-01 14:38:44 -0700870 blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
871 blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
872
873 local_irq_restore(flags);
874}
875
Vivek Goyale43473b2010-09-15 17:06:35 -0400876static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
877{
878 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400879
880 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700881 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400882 tg->io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400883
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700884 /*
885 * REQ_THROTTLED is used to prevent the same bio to be throttled
886 * more than once as a throttled bio will go through blk-throtl the
887 * second time when it eventually gets issued. Set it when a bio
888 * is being charged to a tg.
889 *
890 * Dispatch stats aren't recursive and each @bio should only be
891 * accounted by the @tg it was originally associated with. Let's
892 * update the stats when setting REQ_THROTTLED for the first time
893 * which is guaranteed to be for the @bio's original tg.
894 */
895 if (!(bio->bi_rw & REQ_THROTTLED)) {
896 bio->bi_rw |= REQ_THROTTLED;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700897 throtl_update_dispatch_stats(tg_to_blkg(tg),
898 bio->bi_iter.bi_size, bio->bi_rw);
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700899 }
Vivek Goyale43473b2010-09-15 17:06:35 -0400900}
901
Tejun Heoc5cc2072013-05-14 13:52:38 -0700902/**
903 * throtl_add_bio_tg - add a bio to the specified throtl_grp
904 * @bio: bio to add
905 * @qn: qnode to use
906 * @tg: the target throtl_grp
907 *
908 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
909 * tg->qnode_on_self[] is used.
910 */
911static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
912 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400913{
Tejun Heo73f0d492013-05-14 13:52:35 -0700914 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400915 bool rw = bio_data_dir(bio);
916
Tejun Heoc5cc2072013-05-14 13:52:38 -0700917 if (!qn)
918 qn = &tg->qnode_on_self[rw];
919
Tejun Heo0e9f4162013-05-14 13:52:35 -0700920 /*
921 * If @tg doesn't currently have any bios queued in the same
922 * direction, queueing @bio can change when @tg should be
923 * dispatched. Mark that @tg was empty. This is automatically
924 * cleaered on the next tg_update_disptime().
925 */
926 if (!sq->nr_queued[rw])
927 tg->flags |= THROTL_TG_WAS_EMPTY;
928
Tejun Heoc5cc2072013-05-14 13:52:38 -0700929 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
930
Tejun Heo73f0d492013-05-14 13:52:35 -0700931 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700932 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400933}
934
Tejun Heo77216b02013-05-14 13:52:36 -0700935static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400936{
Tejun Heo73f0d492013-05-14 13:52:35 -0700937 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400938 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
939 struct bio *bio;
940
Tejun Heoc5cc2072013-05-14 13:52:38 -0700941 if ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700942 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400943
Tejun Heoc5cc2072013-05-14 13:52:38 -0700944 if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700945 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400946
947 min_wait = min(read_wait, write_wait);
948 disptime = jiffies + min_wait;
949
Vivek Goyale43473b2010-09-15 17:06:35 -0400950 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700951 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400952 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700953 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700954
955 /* see throtl_add_bio_tg() */
956 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400957}
958
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700959static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
960 struct throtl_grp *parent_tg, bool rw)
961{
962 if (throtl_slice_used(parent_tg, rw)) {
963 throtl_start_new_slice_with_credit(parent_tg, rw,
964 child_tg->slice_start[rw]);
965 }
966
967}
968
Tejun Heo77216b02013-05-14 13:52:36 -0700969static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400970{
Tejun Heo73f0d492013-05-14 13:52:35 -0700971 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700972 struct throtl_service_queue *parent_sq = sq->parent_sq;
973 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -0700974 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -0400975 struct bio *bio;
976
Tejun Heoc5cc2072013-05-14 13:52:38 -0700977 /*
978 * @bio is being transferred from @tg to @parent_sq. Popping a bio
979 * from @tg may put its reference and @parent_sq might end up
980 * getting released prematurely. Remember the tg to put and put it
981 * after @bio is transferred to @parent_sq.
982 */
983 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -0700984 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -0400985
986 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700987
988 /*
989 * If our parent is another tg, we just need to transfer @bio to
990 * the parent using throtl_add_bio_tg(). If our parent is
991 * @td->service_queue, @bio is ready to be issued. Put it on its
992 * bio_lists[] and decrease total number queued. The caller is
993 * responsible for issuing these bios.
994 */
995 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700996 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700997 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -0700998 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -0700999 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1000 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001001 BUG_ON(tg->td->nr_queued[rw] <= 0);
1002 tg->td->nr_queued[rw]--;
1003 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001004
Tejun Heo0f3457f2013-05-14 13:52:32 -07001005 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001006
Tejun Heoc5cc2072013-05-14 13:52:38 -07001007 if (tg_to_put)
1008 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001009}
1010
Tejun Heo77216b02013-05-14 13:52:36 -07001011static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001012{
Tejun Heo73f0d492013-05-14 13:52:35 -07001013 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001014 unsigned int nr_reads = 0, nr_writes = 0;
1015 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001016 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001017 struct bio *bio;
1018
1019 /* Try to dispatch 75% READS and 25% WRITES */
1020
Tejun Heoc5cc2072013-05-14 13:52:38 -07001021 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001022 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001023
Tejun Heo77216b02013-05-14 13:52:36 -07001024 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001025 nr_reads++;
1026
1027 if (nr_reads >= max_nr_reads)
1028 break;
1029 }
1030
Tejun Heoc5cc2072013-05-14 13:52:38 -07001031 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001032 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001033
Tejun Heo77216b02013-05-14 13:52:36 -07001034 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001035 nr_writes++;
1036
1037 if (nr_writes >= max_nr_writes)
1038 break;
1039 }
1040
1041 return nr_reads + nr_writes;
1042}
1043
Tejun Heo651930b2013-05-14 13:52:35 -07001044static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001045{
1046 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001047
1048 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001049 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1050 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001051
1052 if (!tg)
1053 break;
1054
1055 if (time_before(jiffies, tg->disptime))
1056 break;
1057
Tejun Heo77216b02013-05-14 13:52:36 -07001058 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001059
Tejun Heo77216b02013-05-14 13:52:36 -07001060 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001061
Tejun Heo73f0d492013-05-14 13:52:35 -07001062 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001063 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001064
1065 if (nr_disp >= throtl_quantum)
1066 break;
1067 }
1068
1069 return nr_disp;
1070}
1071
Tejun Heo6e1a5702013-05-14 13:52:37 -07001072/**
1073 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1074 * @arg: the throtl_service_queue being serviced
1075 *
1076 * This timer is armed when a child throtl_grp with active bio's become
1077 * pending and queued on the service_queue's pending_tree and expires when
1078 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001079 * dispatches bio's from the children throtl_grps to the parent
1080 * service_queue.
1081 *
1082 * If the parent's parent is another throtl_grp, dispatching is propagated
1083 * by either arming its pending_timer or repeating dispatch directly. If
1084 * the top-level service_tree is reached, throtl_data->dispatch_work is
1085 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001086 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001087static void throtl_pending_timer_fn(unsigned long arg)
1088{
1089 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001090 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001091 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001092 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001093 struct throtl_service_queue *parent_sq;
1094 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001095 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001096
1097 spin_lock_irq(q->queue_lock);
Tejun Heo2e48a532013-05-14 13:52:38 -07001098again:
1099 parent_sq = sq->parent_sq;
1100 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001101
Tejun Heo7f52f982013-05-14 13:52:37 -07001102 while (true) {
1103 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001104 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1105 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001106
Tejun Heo7f52f982013-05-14 13:52:37 -07001107 ret = throtl_select_dispatch(sq);
1108 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001109 throtl_log(sq, "bios disp=%u", ret);
1110 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001111 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001112
Tejun Heo7f52f982013-05-14 13:52:37 -07001113 if (throtl_schedule_next_dispatch(sq, false))
1114 break;
1115
1116 /* this dispatch windows is still open, relax and repeat */
1117 spin_unlock_irq(q->queue_lock);
1118 cpu_relax();
1119 spin_lock_irq(q->queue_lock);
1120 }
Tejun Heo6a525602013-05-14 13:52:32 -07001121
Tejun Heo2e48a532013-05-14 13:52:38 -07001122 if (!dispatched)
1123 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001124
Tejun Heo2e48a532013-05-14 13:52:38 -07001125 if (parent_sq) {
1126 /* @parent_sq is another throl_grp, propagate dispatch */
1127 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1128 tg_update_disptime(tg);
1129 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1130 /* window is already open, repeat dispatching */
1131 sq = parent_sq;
1132 tg = sq_to_tg(sq);
1133 goto again;
1134 }
1135 }
1136 } else {
1137 /* reached the top-level, queue issueing */
1138 queue_work(kthrotld_workqueue, &td->dispatch_work);
1139 }
1140out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001141 spin_unlock_irq(q->queue_lock);
1142}
1143
1144/**
1145 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1146 * @work: work item being executed
1147 *
1148 * This function is queued for execution when bio's reach the bio_lists[]
1149 * of throtl_data->service_queue. Those bio's are ready and issued by this
1150 * function.
1151 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001152static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001153{
1154 struct throtl_data *td = container_of(work, struct throtl_data,
1155 dispatch_work);
1156 struct throtl_service_queue *td_sq = &td->service_queue;
1157 struct request_queue *q = td->queue;
1158 struct bio_list bio_list_on_stack;
1159 struct bio *bio;
1160 struct blk_plug plug;
1161 int rw;
1162
1163 bio_list_init(&bio_list_on_stack);
1164
1165 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001166 for (rw = READ; rw <= WRITE; rw++)
1167 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1168 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001169 spin_unlock_irq(q->queue_lock);
1170
Tejun Heo6e1a5702013-05-14 13:52:37 -07001171 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001172 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001173 while((bio = bio_list_pop(&bio_list_on_stack)))
1174 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001175 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001176 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001177}
1178
Tejun Heof95a04a2012-04-16 13:57:26 -07001179static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1180 struct blkg_policy_data *pd, int off)
Tejun Heo41b38b62012-04-01 14:38:44 -07001181{
Tejun Heof95a04a2012-04-16 13:57:26 -07001182 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo41b38b62012-04-01 14:38:44 -07001183 struct blkg_rwstat rwstat = { }, tmp;
1184 int i, cpu;
1185
1186 for_each_possible_cpu(cpu) {
Tejun Heo8a3d2612012-04-01 14:38:44 -07001187 struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
Tejun Heo41b38b62012-04-01 14:38:44 -07001188
1189 tmp = blkg_rwstat_read((void *)sc + off);
1190 for (i = 0; i < BLKG_RWSTAT_NR; i++)
1191 rwstat.cnt[i] += tmp.cnt[i];
1192 }
1193
Tejun Heof95a04a2012-04-16 13:57:26 -07001194 return __blkg_prfill_rwstat(sf, pd, &rwstat);
Tejun Heo41b38b62012-04-01 14:38:44 -07001195}
1196
Tejun Heo2da8ca82013-12-05 12:28:04 -05001197static int tg_print_cpu_rwstat(struct seq_file *sf, void *v)
Tejun Heo41b38b62012-04-01 14:38:44 -07001198{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001199 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat,
1200 &blkcg_policy_throtl, seq_cft(sf)->private, true);
Tejun Heo41b38b62012-04-01 14:38:44 -07001201 return 0;
1202}
1203
Tejun Heof95a04a2012-04-16 13:57:26 -07001204static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1205 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001206{
Tejun Heof95a04a2012-04-16 13:57:26 -07001207 struct throtl_grp *tg = pd_to_tg(pd);
1208 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001209
Tejun Heoaf133ce2012-04-01 14:38:44 -07001210 if (v == -1)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001211 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001212 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001213}
1214
Tejun Heof95a04a2012-04-16 13:57:26 -07001215static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1216 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001217{
Tejun Heof95a04a2012-04-16 13:57:26 -07001218 struct throtl_grp *tg = pd_to_tg(pd);
1219 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001220
1221 if (v == -1)
1222 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001223 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001224}
1225
Tejun Heo2da8ca82013-12-05 12:28:04 -05001226static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001227{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001228 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1229 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001230 return 0;
1231}
1232
Tejun Heo2da8ca82013-12-05 12:28:04 -05001233static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001234{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001235 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1236 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001237 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001238}
1239
Tejun Heo451af502014-05-13 12:16:21 -04001240static ssize_t tg_set_conf(struct kernfs_open_file *of,
1241 char *buf, size_t nbytes, loff_t off, bool is_u64)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001242{
Tejun Heo451af502014-05-13 12:16:21 -04001243 struct blkcg *blkcg = css_to_blkcg(of_css(of));
Tejun Heo60c2bc22012-04-01 14:38:43 -07001244 struct blkg_conf_ctx ctx;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001245 struct throtl_grp *tg;
Tejun Heo69df0ab2013-05-14 13:52:36 -07001246 struct throtl_service_queue *sq;
Tejun Heo693e7512013-05-14 13:52:38 -07001247 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001248 struct cgroup_subsys_state *pos_css;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001249 int ret;
1250
Tejun Heo3c798392012-04-16 13:57:25 -07001251 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001252 if (ret)
1253 return ret;
1254
Tejun Heoaf133ce2012-04-01 14:38:44 -07001255 tg = blkg_to_tg(ctx.blkg);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001256 sq = &tg->service_queue;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001257
Tejun Heoa2b16932012-04-13 13:11:33 -07001258 if (!ctx.v)
1259 ctx.v = -1;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001260
Tejun Heoa2b16932012-04-13 13:11:33 -07001261 if (is_u64)
Tejun Heo451af502014-05-13 12:16:21 -04001262 *(u64 *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoa2b16932012-04-13 13:11:33 -07001263 else
Tejun Heo451af502014-05-13 12:16:21 -04001264 *(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001265
Tejun Heofda6f272013-05-14 13:52:36 -07001266 throtl_log(&tg->service_queue,
1267 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1268 tg->bps[READ], tg->bps[WRITE],
1269 tg->iops[READ], tg->iops[WRITE]);
Tejun Heo632b4492013-05-14 13:52:31 -07001270
1271 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001272 * Update has_rules[] flags for the updated tg's subtree. A tg is
1273 * considered to have rules if either the tg itself or any of its
1274 * ancestors has rules. This identifies groups without any
1275 * restrictions in the whole hierarchy and allows them to bypass
1276 * blk-throttle.
1277 */
Tejun Heo492eb212013-08-08 20:11:25 -04001278 blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg)
Tejun Heo693e7512013-05-14 13:52:38 -07001279 tg_update_has_rules(blkg_to_tg(blkg));
1280
1281 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001282 * We're already holding queue_lock and know @tg is valid. Let's
1283 * apply the new config directly.
1284 *
1285 * Restart the slices for both READ and WRITES. It might happen
1286 * that a group's limit are dropped suddenly and we don't want to
1287 * account recently dispatched IO with new low rate.
1288 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001289 throtl_start_new_slice(tg, 0);
1290 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001291
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001292 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001293 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001294 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001295 }
Tejun Heo60c2bc22012-04-01 14:38:43 -07001296
1297 blkg_conf_finish(&ctx);
Tejun Heo451af502014-05-13 12:16:21 -04001298 return nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001299}
1300
Tejun Heo451af502014-05-13 12:16:21 -04001301static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1302 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001303{
Tejun Heo451af502014-05-13 12:16:21 -04001304 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001305}
1306
Tejun Heo451af502014-05-13 12:16:21 -04001307static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1308 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001309{
Tejun Heo451af502014-05-13 12:16:21 -04001310 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001311}
1312
1313static struct cftype throtl_files[] = {
1314 {
1315 .name = "throttle.read_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001316 .private = offsetof(struct throtl_grp, bps[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001317 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001318 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001319 },
1320 {
1321 .name = "throttle.write_bps_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001322 .private = offsetof(struct throtl_grp, bps[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001323 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001324 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001325 },
1326 {
1327 .name = "throttle.read_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001328 .private = offsetof(struct throtl_grp, iops[READ]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001329 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001330 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001331 },
1332 {
1333 .name = "throttle.write_iops_device",
Tejun Heoaf133ce2012-04-01 14:38:44 -07001334 .private = offsetof(struct throtl_grp, iops[WRITE]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001335 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001336 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001337 },
1338 {
1339 .name = "throttle.io_service_bytes",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001340 .private = offsetof(struct tg_stats_cpu, service_bytes),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001341 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001342 },
1343 {
1344 .name = "throttle.io_serviced",
Tejun Heo5bc4afb12012-04-01 14:38:45 -07001345 .private = offsetof(struct tg_stats_cpu, serviced),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001346 .seq_show = tg_print_cpu_rwstat,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001347 },
1348 { } /* terminate */
1349};
1350
Vivek Goyalda527772011-03-02 19:05:33 -05001351static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001352{
1353 struct throtl_data *td = q->td;
1354
Tejun Heo69df0ab2013-05-14 13:52:36 -07001355 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001356}
1357
Tejun Heo3c798392012-04-16 13:57:25 -07001358static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001359 .cftypes = throtl_files,
1360
Tejun Heo001bea72015-08-18 14:55:11 -07001361 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001362 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001363 .pd_online_fn = throtl_pd_online,
Tejun Heo001bea72015-08-18 14:55:11 -07001364 .pd_free_fn = throtl_pd_free,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001365 .pd_reset_stats_fn = throtl_pd_reset_stats,
Vivek Goyale43473b2010-09-15 17:06:35 -04001366};
1367
Tejun Heoae118892015-08-18 14:55:20 -07001368bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1369 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001370{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001371 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001372 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001373 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001374 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001375 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001376
Tejun Heoae118892015-08-18 14:55:20 -07001377 WARN_ON_ONCE(!rcu_read_lock_held());
1378
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001379 /* see throtl_charge_bio() */
Tejun Heoae118892015-08-18 14:55:20 -07001380 if ((bio->bi_rw & REQ_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001381 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001382
1383 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001384
1385 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001386 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001387
Tejun Heo73f0d492013-05-14 13:52:35 -07001388 sq = &tg->service_queue;
1389
Tejun Heo9e660ac2013-05-14 13:52:38 -07001390 while (true) {
1391 /* throtl is FIFO - if bios are already queued, should queue */
1392 if (sq->nr_queued[rw])
1393 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001394
Tejun Heo9e660ac2013-05-14 13:52:38 -07001395 /* if above limits, break to queue */
1396 if (!tg_may_dispatch(tg, bio, NULL))
1397 break;
1398
1399 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001400 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001401
1402 /*
1403 * We need to trim slice even when bios are not being queued
1404 * otherwise it might happen that a bio is not queued for
1405 * a long time and slice keeps on extending and trim is not
1406 * called for a long time. Now if limits are reduced suddenly
1407 * we take into account all the IO dispatched so far at new
1408 * low rate and * newly queued IO gets a really long dispatch
1409 * time.
1410 *
1411 * So keep on trimming slice even if bio is not queued.
1412 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001413 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001414
1415 /*
1416 * @bio passed through this layer without being throttled.
1417 * Climb up the ladder. If we''re already at the top, it
1418 * can be executed directly.
1419 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001420 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001421 sq = sq->parent_sq;
1422 tg = sq_to_tg(sq);
1423 if (!tg)
1424 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001425 }
1426
Tejun Heo9e660ac2013-05-14 13:52:38 -07001427 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001428 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1429 rw == READ ? 'R' : 'W',
Kent Overstreet4f024f32013-10-11 15:44:27 -07001430 tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
Tejun Heofda6f272013-05-14 13:52:36 -07001431 tg->io_disp[rw], tg->iops[rw],
1432 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001433
Tejun Heo671058f2012-03-05 13:15:29 -08001434 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001435 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001436 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001437 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001438
Tejun Heo7f52f982013-05-14 13:52:37 -07001439 /*
1440 * Update @tg's dispatch time and force schedule dispatch if @tg
1441 * was empty before @bio. The forced scheduling isn't likely to
1442 * cause undue delay as @bio is likely to be dispatched directly if
1443 * its @tg's disptime is not in the future.
1444 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001445 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001446 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001447 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001448 }
1449
Tejun Heobc16a4f2011-10-19 14:33:01 +02001450out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001451 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001452out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001453 /*
1454 * As multiple blk-throtls may stack in the same issue path, we
1455 * don't want bios to leave with the flag set. Clear the flag if
1456 * being issued.
1457 */
1458 if (!throttled)
1459 bio->bi_rw &= ~REQ_THROTTLED;
Tejun Heobc16a4f2011-10-19 14:33:01 +02001460 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001461}
1462
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001463/*
1464 * Dispatch all bios from all children tg's queued on @parent_sq. On
1465 * return, @parent_sq is guaranteed to not have any active children tg's
1466 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1467 */
1468static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1469{
1470 struct throtl_grp *tg;
1471
1472 while ((tg = throtl_rb_first(parent_sq))) {
1473 struct throtl_service_queue *sq = &tg->service_queue;
1474 struct bio *bio;
1475
1476 throtl_dequeue_tg(tg);
1477
Tejun Heoc5cc2072013-05-14 13:52:38 -07001478 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001479 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001480 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001481 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1482 }
1483}
1484
Tejun Heoc9a929d2011-10-19 14:42:16 +02001485/**
1486 * blk_throtl_drain - drain throttled bios
1487 * @q: request_queue to drain throttled bios for
1488 *
1489 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1490 */
1491void blk_throtl_drain(struct request_queue *q)
1492 __releases(q->queue_lock) __acquires(q->queue_lock)
1493{
1494 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001495 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001496 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001497 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001498 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001499
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001500 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001501 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001502
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001503 /*
1504 * Drain each tg while doing post-order walk on the blkg tree, so
1505 * that all bios are propagated to td->service_queue. It'd be
1506 * better to walk service_queue tree directly but blkg walk is
1507 * easier.
1508 */
Tejun Heo492eb212013-08-08 20:11:25 -04001509 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001510 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001511
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001512 /* finally, transfer bios from top-level tg's into the td */
1513 tg_drain_bios(&td->service_queue);
1514
1515 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001516 spin_unlock_irq(q->queue_lock);
1517
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001518 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001519 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001520 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1521 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001522 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001523
1524 spin_lock_irq(q->queue_lock);
1525}
1526
Vivek Goyale43473b2010-09-15 17:06:35 -04001527int blk_throtl_init(struct request_queue *q)
1528{
1529 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001530 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001531
1532 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1533 if (!td)
1534 return -ENOMEM;
1535
Tejun Heo69df0ab2013-05-14 13:52:36 -07001536 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001537 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001538
Tejun Heocd1604f2012-03-05 13:15:06 -08001539 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001540 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001541
Tejun Heoa2b16932012-04-13 13:11:33 -07001542 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001543 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001544 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001545 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001546 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001547}
1548
1549void blk_throtl_exit(struct request_queue *q)
1550{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001551 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001552 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001553 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001554 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001555}
1556
1557static int __init throtl_init(void)
1558{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001559 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1560 if (!kthrotld_workqueue)
1561 panic("Failed to create kthrotld\n");
1562
Tejun Heo3c798392012-04-16 13:57:25 -07001563 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001564}
1565
1566module_init(throtl_init);