blob: 2073b4839ba53a7d26f72ff4e33ed5c1a193ab07 [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
Shaohua Li9f626e32017-03-27 10:51:30 -070086enum {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070087 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070088 LIMIT_MAX,
89 LIMIT_CNT,
90};
91
Vivek Goyale43473b2010-09-15 17:06:35 -040092struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -070093 /* must be the first member */
94 struct blkg_policy_data pd;
95
Tejun Heoc9e03322013-05-14 13:52:32 -070096 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -040097 struct rb_node rb_node;
98
Tejun Heo0f3457f2013-05-14 13:52:32 -070099 /* throtl_data this group belongs to */
100 struct throtl_data *td;
101
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700102 /* this group's service queue */
103 struct throtl_service_queue service_queue;
104
Vivek Goyale43473b2010-09-15 17:06:35 -0400105 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700106 * qnode_on_self is used when bios are directly queued to this
107 * throtl_grp so that local bios compete fairly with bios
108 * dispatched from children. qnode_on_parent is used when bios are
109 * dispatched from this throtl_grp into its parent and will compete
110 * with the sibling qnode_on_parents and the parent's
111 * qnode_on_self.
112 */
113 struct throtl_qnode qnode_on_self[2];
114 struct throtl_qnode qnode_on_parent[2];
115
116 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400117 * Dispatch time in jiffies. This is the estimated time when group
118 * will unthrottle and is ready to dispatch more bio. It is used as
119 * key to sort active groups in service tree.
120 */
121 unsigned long disptime;
122
Vivek Goyale43473b2010-09-15 17:06:35 -0400123 unsigned int flags;
124
Tejun Heo693e7512013-05-14 13:52:38 -0700125 /* are there any throtl rules between this group and td? */
126 bool has_rules[2];
127
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700128 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700129 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700130 /* user configured bps limits */
131 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400132
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700133 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700134 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700135 /* user configured IOPS limits */
136 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400137
Vivek Goyale43473b2010-09-15 17:06:35 -0400138 /* Number of bytes disptached in current slice */
139 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400140 /* Number of bio's dispatched in current slice */
141 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400142
Shaohua Li3f0abd82017-03-27 10:51:35 -0700143 unsigned long last_low_overflow_time[2];
144
145 uint64_t last_bytes_disp[2];
146 unsigned int last_io_disp[2];
147
148 unsigned long last_check_time;
149
Vivek Goyale43473b2010-09-15 17:06:35 -0400150 /* When did we start a new slice */
151 unsigned long slice_start[2];
152 unsigned long slice_end[2];
153};
154
155struct throtl_data
156{
Vivek Goyale43473b2010-09-15 17:06:35 -0400157 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700158 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400159
Vivek Goyale43473b2010-09-15 17:06:35 -0400160 struct request_queue *queue;
161
162 /* Total Number of queued bios on READ and WRITE lists */
163 unsigned int nr_queued[2];
164
Vivek Goyale43473b2010-09-15 17:06:35 -0400165 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700166 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700167 unsigned int limit_index;
168 bool limit_valid[LIMIT_CNT];
Shaohua Li3f0abd82017-03-27 10:51:35 -0700169
170 unsigned long low_upgrade_time;
171 unsigned long low_downgrade_time;
Vivek Goyale43473b2010-09-15 17:06:35 -0400172};
173
Tejun Heo69df0ab2013-05-14 13:52:36 -0700174static void throtl_pending_timer_fn(unsigned long arg);
175
Tejun Heof95a04a2012-04-16 13:57:26 -0700176static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
177{
178 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
179}
180
Tejun Heo3c798392012-04-16 13:57:25 -0700181static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800182{
Tejun Heof95a04a2012-04-16 13:57:26 -0700183 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800184}
185
Tejun Heo3c798392012-04-16 13:57:25 -0700186static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800187{
Tejun Heof95a04a2012-04-16 13:57:26 -0700188 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800189}
190
Tejun Heofda6f272013-05-14 13:52:36 -0700191/**
192 * sq_to_tg - return the throl_grp the specified service queue belongs to
193 * @sq: the throtl_service_queue of interest
194 *
195 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
196 * embedded in throtl_data, %NULL is returned.
197 */
198static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
199{
200 if (sq && sq->parent_sq)
201 return container_of(sq, struct throtl_grp, service_queue);
202 else
203 return NULL;
204}
Vivek Goyale43473b2010-09-15 17:06:35 -0400205
Tejun Heofda6f272013-05-14 13:52:36 -0700206/**
207 * sq_to_td - return throtl_data the specified service queue belongs to
208 * @sq: the throtl_service_queue of interest
209 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800210 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700211 * Determine the associated throtl_data accordingly and return it.
212 */
213static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
214{
215 struct throtl_grp *tg = sq_to_tg(sq);
216
217 if (tg)
218 return tg->td;
219 else
220 return container_of(sq, struct throtl_data, service_queue);
221}
222
Shaohua Li9f626e32017-03-27 10:51:30 -0700223static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
224{
Shaohua Lib22c4172017-03-27 10:51:33 -0700225 struct blkcg_gq *blkg = tg_to_blkg(tg);
226 uint64_t ret;
227
228 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
229 return U64_MAX;
230 ret = tg->bps[rw][tg->td->limit_index];
231 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
232 return tg->bps[rw][LIMIT_MAX];
233 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700234}
235
236static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
237{
Shaohua Lib22c4172017-03-27 10:51:33 -0700238 struct blkcg_gq *blkg = tg_to_blkg(tg);
239 unsigned int ret;
240
241 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
242 return UINT_MAX;
243 ret = tg->iops[rw][tg->td->limit_index];
244 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
245 return tg->iops[rw][LIMIT_MAX];
246 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700247}
248
Tejun Heofda6f272013-05-14 13:52:36 -0700249/**
250 * throtl_log - log debug message via blktrace
251 * @sq: the service_queue being reported
252 * @fmt: printf format string
253 * @args: printf args
254 *
255 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
256 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700257 */
258#define throtl_log(sq, fmt, args...) do { \
259 struct throtl_grp *__tg = sq_to_tg((sq)); \
260 struct throtl_data *__td = sq_to_td((sq)); \
261 \
262 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700263 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
264 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700265 if ((__tg)) { \
266 char __pbuf[128]; \
267 \
268 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
269 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
270 } else { \
271 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
272 } \
273} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400274
Tejun Heoc5cc2072013-05-14 13:52:38 -0700275static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
276{
277 INIT_LIST_HEAD(&qn->node);
278 bio_list_init(&qn->bios);
279 qn->tg = tg;
280}
281
282/**
283 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
284 * @bio: bio being added
285 * @qn: qnode to add bio to
286 * @queued: the service_queue->queued[] list @qn belongs to
287 *
288 * Add @bio to @qn and put @qn on @queued if it's not already on.
289 * @qn->tg's reference count is bumped when @qn is activated. See the
290 * comment on top of throtl_qnode definition for details.
291 */
292static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
293 struct list_head *queued)
294{
295 bio_list_add(&qn->bios, bio);
296 if (list_empty(&qn->node)) {
297 list_add_tail(&qn->node, queued);
298 blkg_get(tg_to_blkg(qn->tg));
299 }
300}
301
302/**
303 * throtl_peek_queued - peek the first bio on a qnode list
304 * @queued: the qnode list to peek
305 */
306static struct bio *throtl_peek_queued(struct list_head *queued)
307{
308 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
309 struct bio *bio;
310
311 if (list_empty(queued))
312 return NULL;
313
314 bio = bio_list_peek(&qn->bios);
315 WARN_ON_ONCE(!bio);
316 return bio;
317}
318
319/**
320 * throtl_pop_queued - pop the first bio form a qnode list
321 * @queued: the qnode list to pop a bio from
322 * @tg_to_put: optional out argument for throtl_grp to put
323 *
324 * Pop the first bio from the qnode list @queued. After popping, the first
325 * qnode is removed from @queued if empty or moved to the end of @queued so
326 * that the popping order is round-robin.
327 *
328 * When the first qnode is removed, its associated throtl_grp should be put
329 * too. If @tg_to_put is NULL, this function automatically puts it;
330 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
331 * responsible for putting it.
332 */
333static struct bio *throtl_pop_queued(struct list_head *queued,
334 struct throtl_grp **tg_to_put)
335{
336 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
337 struct bio *bio;
338
339 if (list_empty(queued))
340 return NULL;
341
342 bio = bio_list_pop(&qn->bios);
343 WARN_ON_ONCE(!bio);
344
345 if (bio_list_empty(&qn->bios)) {
346 list_del_init(&qn->node);
347 if (tg_to_put)
348 *tg_to_put = qn->tg;
349 else
350 blkg_put(tg_to_blkg(qn->tg));
351 } else {
352 list_move_tail(&qn->node, queued);
353 }
354
355 return bio;
356}
357
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700358/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700359static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700360{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700361 INIT_LIST_HEAD(&sq->queued[0]);
362 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700363 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700364 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
365 (unsigned long)sq);
366}
367
Tejun Heo001bea72015-08-18 14:55:11 -0700368static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
369{
Tejun Heo4fb72032015-08-18 14:55:12 -0700370 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700371 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700372
373 tg = kzalloc_node(sizeof(*tg), gfp, node);
374 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700375 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700376
Tejun Heob2ce2642015-08-18 14:55:13 -0700377 throtl_service_queue_init(&tg->service_queue);
378
379 for (rw = READ; rw <= WRITE; rw++) {
380 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
381 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
382 }
383
384 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700385 tg->bps[READ][LIMIT_MAX] = U64_MAX;
386 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
387 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
388 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700389 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
390 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
391 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
392 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
393 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700394
Tejun Heo4fb72032015-08-18 14:55:12 -0700395 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700396}
397
Tejun Heoa9520cd2015-08-18 14:55:14 -0700398static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400399{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700400 struct throtl_grp *tg = pd_to_tg(pd);
401 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700402 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700403 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800404
Tejun Heo91381252013-05-14 13:52:38 -0700405 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400406 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700407 * behavior where limits on a given throtl_grp are applied to the
408 * whole subtree rather than just the group itself. e.g. If 16M
409 * read_bps limit is set on the root group, the whole system can't
410 * exceed 16M for the device.
411 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400412 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700413 * behavior is retained where all throtl_grps are treated as if
414 * they're all separate root groups right below throtl_data.
415 * Limits of a group don't interact with limits of other groups
416 * regardless of the position of the group in the hierarchy.
417 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700418 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400419 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700420 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700421 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700422}
423
Tejun Heo693e7512013-05-14 13:52:38 -0700424/*
425 * Set has_rules[] if @tg or any of its parents have limits configured.
426 * This doesn't require walking up to the top of the hierarchy as the
427 * parent's has_rules[] is guaranteed to be correct.
428 */
429static void tg_update_has_rules(struct throtl_grp *tg)
430{
431 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700432 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700433 int rw;
434
435 for (rw = READ; rw <= WRITE; rw++)
436 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700437 (td->limit_valid[td->limit_index] &&
438 (tg_bps_limit(tg, rw) != U64_MAX ||
439 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700440}
441
Tejun Heoa9520cd2015-08-18 14:55:14 -0700442static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700443{
444 /*
445 * We don't want new groups to escape the limits of its ancestors.
446 * Update has_rules[] after a new group is brought online.
447 */
Tejun Heoa9520cd2015-08-18 14:55:14 -0700448 tg_update_has_rules(pd_to_tg(pd));
Tejun Heo693e7512013-05-14 13:52:38 -0700449}
450
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700451static void blk_throtl_update_limit_valid(struct throtl_data *td)
452{
453 struct cgroup_subsys_state *pos_css;
454 struct blkcg_gq *blkg;
455 bool low_valid = false;
456
457 rcu_read_lock();
458 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
459 struct throtl_grp *tg = blkg_to_tg(blkg);
460
461 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
462 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
463 low_valid = true;
464 }
465 rcu_read_unlock();
466
467 td->limit_valid[LIMIT_LOW] = low_valid;
468}
469
Shaohua Lic79892c2017-03-27 10:51:34 -0700470static void throtl_upgrade_state(struct throtl_data *td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700471static void throtl_pd_offline(struct blkg_policy_data *pd)
472{
473 struct throtl_grp *tg = pd_to_tg(pd);
474
475 tg->bps[READ][LIMIT_LOW] = 0;
476 tg->bps[WRITE][LIMIT_LOW] = 0;
477 tg->iops[READ][LIMIT_LOW] = 0;
478 tg->iops[WRITE][LIMIT_LOW] = 0;
479
480 blk_throtl_update_limit_valid(tg->td);
481
Shaohua Lic79892c2017-03-27 10:51:34 -0700482 if (!tg->td->limit_valid[tg->td->limit_index])
483 throtl_upgrade_state(tg->td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700484}
485
Tejun Heo001bea72015-08-18 14:55:11 -0700486static void throtl_pd_free(struct blkg_policy_data *pd)
487{
Tejun Heo4fb72032015-08-18 14:55:12 -0700488 struct throtl_grp *tg = pd_to_tg(pd);
489
Tejun Heob2ce2642015-08-18 14:55:13 -0700490 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700491 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700492}
493
Tejun Heo0049af72013-05-14 13:52:33 -0700494static struct throtl_grp *
495throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400496{
497 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700498 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400499 return NULL;
500
Tejun Heo0049af72013-05-14 13:52:33 -0700501 if (!parent_sq->first_pending)
502 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400503
Tejun Heo0049af72013-05-14 13:52:33 -0700504 if (parent_sq->first_pending)
505 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400506
507 return NULL;
508}
509
510static void rb_erase_init(struct rb_node *n, struct rb_root *root)
511{
512 rb_erase(n, root);
513 RB_CLEAR_NODE(n);
514}
515
Tejun Heo0049af72013-05-14 13:52:33 -0700516static void throtl_rb_erase(struct rb_node *n,
517 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400518{
Tejun Heo0049af72013-05-14 13:52:33 -0700519 if (parent_sq->first_pending == n)
520 parent_sq->first_pending = NULL;
521 rb_erase_init(n, &parent_sq->pending_tree);
522 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400523}
524
Tejun Heo0049af72013-05-14 13:52:33 -0700525static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400526{
527 struct throtl_grp *tg;
528
Tejun Heo0049af72013-05-14 13:52:33 -0700529 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400530 if (!tg)
531 return;
532
Tejun Heo0049af72013-05-14 13:52:33 -0700533 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400534}
535
Tejun Heo77216b02013-05-14 13:52:36 -0700536static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400537{
Tejun Heo77216b02013-05-14 13:52:36 -0700538 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700539 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400540 struct rb_node *parent = NULL;
541 struct throtl_grp *__tg;
542 unsigned long key = tg->disptime;
543 int left = 1;
544
545 while (*node != NULL) {
546 parent = *node;
547 __tg = rb_entry_tg(parent);
548
549 if (time_before(key, __tg->disptime))
550 node = &parent->rb_left;
551 else {
552 node = &parent->rb_right;
553 left = 0;
554 }
555 }
556
557 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700558 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400559
560 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700561 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400562}
563
Tejun Heo77216b02013-05-14 13:52:36 -0700564static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400565{
Tejun Heo77216b02013-05-14 13:52:36 -0700566 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700567 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700568 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400569}
570
Tejun Heo77216b02013-05-14 13:52:36 -0700571static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400572{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700573 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700574 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400575}
576
Tejun Heo77216b02013-05-14 13:52:36 -0700577static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400578{
Tejun Heo77216b02013-05-14 13:52:36 -0700579 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700580 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400581}
582
Tejun Heo77216b02013-05-14 13:52:36 -0700583static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400584{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700585 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700586 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400587}
588
Tejun Heoa9131a22013-05-14 13:52:31 -0700589/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700590static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
591 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700592{
Shaohua Li06cceed2017-03-27 10:51:36 -0700593 unsigned long max_expire = jiffies + 8 * throtl_slice;
594
595 /*
596 * Since we are adjusting the throttle limit dynamically, the sleep
597 * time calculated according to previous limit might be invalid. It's
598 * possible the cgroup sleep time is very long and no other cgroups
599 * have IO running so notify the limit changes. Make sure the cgroup
600 * doesn't sleep too long to avoid the missed notification.
601 */
602 if (time_after(expires, max_expire))
603 expires = max_expire;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700604 mod_timer(&sq->pending_timer, expires);
605 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
606 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700607}
608
Tejun Heo7f52f982013-05-14 13:52:37 -0700609/**
610 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
611 * @sq: the service_queue to schedule dispatch for
612 * @force: force scheduling
613 *
614 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
615 * dispatch time of the first pending child. Returns %true if either timer
616 * is armed or there's no pending child left. %false if the current
617 * dispatch window is still open and the caller should continue
618 * dispatching.
619 *
620 * If @force is %true, the dispatch timer is always scheduled and this
621 * function is guaranteed to return %true. This is to be used when the
622 * caller can't dispatch itself and needs to invoke pending_timer
623 * unconditionally. Note that forced scheduling is likely to induce short
624 * delay before dispatch starts even if @sq->first_pending_disptime is not
625 * in the future and thus shouldn't be used in hot paths.
626 */
627static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
628 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400629{
Tejun Heo6a525602013-05-14 13:52:32 -0700630 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700631 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700632 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400633
Tejun Heoc9e03322013-05-14 13:52:32 -0700634 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400635
Tejun Heo69df0ab2013-05-14 13:52:36 -0700636 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700637 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700638 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700639 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700640 }
641
Tejun Heo7f52f982013-05-14 13:52:37 -0700642 /* tell the caller to continue dispatching */
643 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400644}
645
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700646static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
647 bool rw, unsigned long start)
648{
649 tg->bytes_disp[rw] = 0;
650 tg->io_disp[rw] = 0;
651
652 /*
653 * Previous slice has expired. We must have trimmed it after last
654 * bio dispatch. That means since start of last slice, we never used
655 * that bandwidth. Do try to make use of that bandwidth while giving
656 * credit.
657 */
658 if (time_after_eq(start, tg->slice_start[rw]))
659 tg->slice_start[rw] = start;
660
661 tg->slice_end[rw] = jiffies + throtl_slice;
662 throtl_log(&tg->service_queue,
663 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
664 rw == READ ? 'R' : 'W', tg->slice_start[rw],
665 tg->slice_end[rw], jiffies);
666}
667
Tejun Heo0f3457f2013-05-14 13:52:32 -0700668static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400669{
670 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400671 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400672 tg->slice_start[rw] = jiffies;
673 tg->slice_end[rw] = jiffies + throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700674 throtl_log(&tg->service_queue,
675 "[%c] new slice start=%lu end=%lu jiffies=%lu",
676 rw == READ ? 'R' : 'W', tg->slice_start[rw],
677 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400678}
679
Tejun Heo0f3457f2013-05-14 13:52:32 -0700680static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
681 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100682{
683 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
684}
685
Tejun Heo0f3457f2013-05-14 13:52:32 -0700686static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
687 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400688{
689 tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700690 throtl_log(&tg->service_queue,
691 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
692 rw == READ ? 'R' : 'W', tg->slice_start[rw],
693 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400694}
695
696/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700697static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400698{
699 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200700 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400701
702 return 1;
703}
704
705/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700706static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400707{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200708 unsigned long nr_slices, time_elapsed, io_trim;
709 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400710
711 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
712
713 /*
714 * If bps are unlimited (-1), then time slice don't get
715 * renewed. Don't try to trim the slice if slice is used. A new
716 * slice will start when appropriate.
717 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700718 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400719 return;
720
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100721 /*
722 * A bio has been dispatched. Also adjust slice_end. It might happen
723 * that initially cgroup limit was very low resulting in high
724 * slice_end, but later limit was bumped up and bio was dispached
725 * sooner, then we need to reduce slice_end. A high bogus slice_end
726 * is bad because it does not allow new slice to start.
727 */
728
Tejun Heo0f3457f2013-05-14 13:52:32 -0700729 throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100730
Vivek Goyale43473b2010-09-15 17:06:35 -0400731 time_elapsed = jiffies - tg->slice_start[rw];
732
733 nr_slices = time_elapsed / throtl_slice;
734
735 if (!nr_slices)
736 return;
Shaohua Li9f626e32017-03-27 10:51:30 -0700737 tmp = tg_bps_limit(tg, rw) * throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200738 do_div(tmp, HZ);
739 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400740
Shaohua Li9f626e32017-03-27 10:51:30 -0700741 io_trim = (tg_iops_limit(tg, rw) * throtl_slice * nr_slices) / HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400742
Vivek Goyal8e89d132010-09-15 17:06:37 -0400743 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400744 return;
745
746 if (tg->bytes_disp[rw] >= bytes_trim)
747 tg->bytes_disp[rw] -= bytes_trim;
748 else
749 tg->bytes_disp[rw] = 0;
750
Vivek Goyal8e89d132010-09-15 17:06:37 -0400751 if (tg->io_disp[rw] >= io_trim)
752 tg->io_disp[rw] -= io_trim;
753 else
754 tg->io_disp[rw] = 0;
755
Vivek Goyale43473b2010-09-15 17:06:35 -0400756 tg->slice_start[rw] += nr_slices * throtl_slice;
757
Tejun Heofda6f272013-05-14 13:52:36 -0700758 throtl_log(&tg->service_queue,
759 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
760 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
761 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400762}
763
Tejun Heo0f3457f2013-05-14 13:52:32 -0700764static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
765 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400766{
767 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400768 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400769 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200770 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400771
Vivek Goyal8e89d132010-09-15 17:06:37 -0400772 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400773
Vivek Goyal8e89d132010-09-15 17:06:37 -0400774 /* Slice has just started. Consider one slice interval */
775 if (!jiffy_elapsed)
776 jiffy_elapsed_rnd = throtl_slice;
777
778 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
779
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200780 /*
781 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
782 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
783 * will allow dispatch after 1 second and after that slice should
784 * have been trimmed.
785 */
786
Shaohua Li9f626e32017-03-27 10:51:30 -0700787 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200788 do_div(tmp, HZ);
789
790 if (tmp > UINT_MAX)
791 io_allowed = UINT_MAX;
792 else
793 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400794
795 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400796 if (wait)
797 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200798 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400799 }
800
Vivek Goyal8e89d132010-09-15 17:06:37 -0400801 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700802 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400803
804 if (jiffy_wait > jiffy_elapsed)
805 jiffy_wait = jiffy_wait - jiffy_elapsed;
806 else
807 jiffy_wait = 1;
808
809 if (wait)
810 *wait = jiffy_wait;
811 return 0;
812}
813
Tejun Heo0f3457f2013-05-14 13:52:32 -0700814static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
815 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400816{
817 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200818 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400819 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400820
821 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
822
823 /* Slice has just started. Consider one slice interval */
824 if (!jiffy_elapsed)
825 jiffy_elapsed_rnd = throtl_slice;
826
827 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
828
Shaohua Li9f626e32017-03-27 10:51:30 -0700829 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200830 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200831 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400832
Kent Overstreet4f024f32013-10-11 15:44:27 -0700833 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400834 if (wait)
835 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200836 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400837 }
838
839 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700840 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700841 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400842
843 if (!jiffy_wait)
844 jiffy_wait = 1;
845
846 /*
847 * This wait time is without taking into consideration the rounding
848 * up we did. Add that time also.
849 */
850 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400851 if (wait)
852 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400853 return 0;
854}
Vivek Goyale43473b2010-09-15 17:06:35 -0400855
Vivek Goyal8e89d132010-09-15 17:06:37 -0400856/*
857 * Returns whether one can dispatch a bio or not. Also returns approx number
858 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
859 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700860static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
861 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400862{
863 bool rw = bio_data_dir(bio);
864 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
865
866 /*
867 * Currently whole state machine of group depends on first bio
868 * queued in the group bio list. So one should not be calling
869 * this function with a different bio if there are other bios
870 * queued.
871 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700872 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700873 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400874
875 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700876 if (tg_bps_limit(tg, rw) == U64_MAX &&
877 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400878 if (wait)
879 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200880 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400881 }
882
883 /*
884 * If previous slice expired, start a new one otherwise renew/extend
885 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600886 * long since now. New slice is started only for empty throttle group.
887 * If there is queued bio, that means there should be an active
888 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400889 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600890 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700891 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400892 else {
893 if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700894 throtl_extend_slice(tg, rw, jiffies + throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400895 }
896
Tejun Heo0f3457f2013-05-14 13:52:32 -0700897 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
898 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400899 if (wait)
900 *wait = 0;
901 return 1;
902 }
903
904 max_wait = max(bps_wait, iops_wait);
905
906 if (wait)
907 *wait = max_wait;
908
909 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700910 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400911
912 return 0;
913}
914
915static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
916{
917 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400918
919 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700920 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400921 tg->io_disp[rw]++;
Shaohua Li3f0abd82017-03-27 10:51:35 -0700922 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
923 tg->last_io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400924
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700925 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200926 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700927 * more than once as a throttled bio will go through blk-throtl the
928 * second time when it eventually gets issued. Set it when a bio
929 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700930 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200931 if (!bio_flagged(bio, BIO_THROTTLED))
932 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400933}
934
Tejun Heoc5cc2072013-05-14 13:52:38 -0700935/**
936 * throtl_add_bio_tg - add a bio to the specified throtl_grp
937 * @bio: bio to add
938 * @qn: qnode to use
939 * @tg: the target throtl_grp
940 *
941 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
942 * tg->qnode_on_self[] is used.
943 */
944static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
945 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400946{
Tejun Heo73f0d492013-05-14 13:52:35 -0700947 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400948 bool rw = bio_data_dir(bio);
949
Tejun Heoc5cc2072013-05-14 13:52:38 -0700950 if (!qn)
951 qn = &tg->qnode_on_self[rw];
952
Tejun Heo0e9f4162013-05-14 13:52:35 -0700953 /*
954 * If @tg doesn't currently have any bios queued in the same
955 * direction, queueing @bio can change when @tg should be
956 * dispatched. Mark that @tg was empty. This is automatically
957 * cleaered on the next tg_update_disptime().
958 */
959 if (!sq->nr_queued[rw])
960 tg->flags |= THROTL_TG_WAS_EMPTY;
961
Tejun Heoc5cc2072013-05-14 13:52:38 -0700962 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
963
Tejun Heo73f0d492013-05-14 13:52:35 -0700964 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700965 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400966}
967
Tejun Heo77216b02013-05-14 13:52:36 -0700968static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400969{
Tejun Heo73f0d492013-05-14 13:52:35 -0700970 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400971 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
972 struct bio *bio;
973
Markus Elfringd609af32017-01-21 22:15:33 +0100974 bio = throtl_peek_queued(&sq->queued[READ]);
975 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700976 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400977
Markus Elfringd609af32017-01-21 22:15:33 +0100978 bio = throtl_peek_queued(&sq->queued[WRITE]);
979 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700980 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400981
982 min_wait = min(read_wait, write_wait);
983 disptime = jiffies + min_wait;
984
Vivek Goyale43473b2010-09-15 17:06:35 -0400985 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700986 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400987 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -0700988 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -0700989
990 /* see throtl_add_bio_tg() */
991 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -0400992}
993
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700994static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
995 struct throtl_grp *parent_tg, bool rw)
996{
997 if (throtl_slice_used(parent_tg, rw)) {
998 throtl_start_new_slice_with_credit(parent_tg, rw,
999 child_tg->slice_start[rw]);
1000 }
1001
1002}
1003
Tejun Heo77216b02013-05-14 13:52:36 -07001004static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001005{
Tejun Heo73f0d492013-05-14 13:52:35 -07001006 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001007 struct throtl_service_queue *parent_sq = sq->parent_sq;
1008 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001009 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001010 struct bio *bio;
1011
Tejun Heoc5cc2072013-05-14 13:52:38 -07001012 /*
1013 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1014 * from @tg may put its reference and @parent_sq might end up
1015 * getting released prematurely. Remember the tg to put and put it
1016 * after @bio is transferred to @parent_sq.
1017 */
1018 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001019 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001020
1021 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001022
1023 /*
1024 * If our parent is another tg, we just need to transfer @bio to
1025 * the parent using throtl_add_bio_tg(). If our parent is
1026 * @td->service_queue, @bio is ready to be issued. Put it on its
1027 * bio_lists[] and decrease total number queued. The caller is
1028 * responsible for issuing these bios.
1029 */
1030 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001031 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001032 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001033 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001034 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1035 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001036 BUG_ON(tg->td->nr_queued[rw] <= 0);
1037 tg->td->nr_queued[rw]--;
1038 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001039
Tejun Heo0f3457f2013-05-14 13:52:32 -07001040 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001041
Tejun Heoc5cc2072013-05-14 13:52:38 -07001042 if (tg_to_put)
1043 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001044}
1045
Tejun Heo77216b02013-05-14 13:52:36 -07001046static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001047{
Tejun Heo73f0d492013-05-14 13:52:35 -07001048 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001049 unsigned int nr_reads = 0, nr_writes = 0;
1050 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001051 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001052 struct bio *bio;
1053
1054 /* Try to dispatch 75% READS and 25% WRITES */
1055
Tejun Heoc5cc2072013-05-14 13:52:38 -07001056 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001057 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001058
Tejun Heo77216b02013-05-14 13:52:36 -07001059 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001060 nr_reads++;
1061
1062 if (nr_reads >= max_nr_reads)
1063 break;
1064 }
1065
Tejun Heoc5cc2072013-05-14 13:52:38 -07001066 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001067 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001068
Tejun Heo77216b02013-05-14 13:52:36 -07001069 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001070 nr_writes++;
1071
1072 if (nr_writes >= max_nr_writes)
1073 break;
1074 }
1075
1076 return nr_reads + nr_writes;
1077}
1078
Tejun Heo651930b2013-05-14 13:52:35 -07001079static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001080{
1081 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001082
1083 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001084 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1085 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001086
1087 if (!tg)
1088 break;
1089
1090 if (time_before(jiffies, tg->disptime))
1091 break;
1092
Tejun Heo77216b02013-05-14 13:52:36 -07001093 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001094
Tejun Heo77216b02013-05-14 13:52:36 -07001095 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001096
Tejun Heo73f0d492013-05-14 13:52:35 -07001097 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001098 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001099
1100 if (nr_disp >= throtl_quantum)
1101 break;
1102 }
1103
1104 return nr_disp;
1105}
1106
Shaohua Lic79892c2017-03-27 10:51:34 -07001107static bool throtl_can_upgrade(struct throtl_data *td,
1108 struct throtl_grp *this_tg);
Tejun Heo6e1a5702013-05-14 13:52:37 -07001109/**
1110 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1111 * @arg: the throtl_service_queue being serviced
1112 *
1113 * This timer is armed when a child throtl_grp with active bio's become
1114 * pending and queued on the service_queue's pending_tree and expires when
1115 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001116 * dispatches bio's from the children throtl_grps to the parent
1117 * service_queue.
1118 *
1119 * If the parent's parent is another throtl_grp, dispatching is propagated
1120 * by either arming its pending_timer or repeating dispatch directly. If
1121 * the top-level service_tree is reached, throtl_data->dispatch_work is
1122 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001123 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001124static void throtl_pending_timer_fn(unsigned long arg)
1125{
1126 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001127 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001128 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001129 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001130 struct throtl_service_queue *parent_sq;
1131 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001132 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001133
1134 spin_lock_irq(q->queue_lock);
Shaohua Lic79892c2017-03-27 10:51:34 -07001135 if (throtl_can_upgrade(td, NULL))
1136 throtl_upgrade_state(td);
1137
Tejun Heo2e48a532013-05-14 13:52:38 -07001138again:
1139 parent_sq = sq->parent_sq;
1140 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001141
Tejun Heo7f52f982013-05-14 13:52:37 -07001142 while (true) {
1143 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001144 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1145 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001146
Tejun Heo7f52f982013-05-14 13:52:37 -07001147 ret = throtl_select_dispatch(sq);
1148 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001149 throtl_log(sq, "bios disp=%u", ret);
1150 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001151 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001152
Tejun Heo7f52f982013-05-14 13:52:37 -07001153 if (throtl_schedule_next_dispatch(sq, false))
1154 break;
1155
1156 /* this dispatch windows is still open, relax and repeat */
1157 spin_unlock_irq(q->queue_lock);
1158 cpu_relax();
1159 spin_lock_irq(q->queue_lock);
1160 }
Tejun Heo6a525602013-05-14 13:52:32 -07001161
Tejun Heo2e48a532013-05-14 13:52:38 -07001162 if (!dispatched)
1163 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001164
Tejun Heo2e48a532013-05-14 13:52:38 -07001165 if (parent_sq) {
1166 /* @parent_sq is another throl_grp, propagate dispatch */
1167 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1168 tg_update_disptime(tg);
1169 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1170 /* window is already open, repeat dispatching */
1171 sq = parent_sq;
1172 tg = sq_to_tg(sq);
1173 goto again;
1174 }
1175 }
1176 } else {
1177 /* reached the top-level, queue issueing */
1178 queue_work(kthrotld_workqueue, &td->dispatch_work);
1179 }
1180out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001181 spin_unlock_irq(q->queue_lock);
1182}
1183
1184/**
1185 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1186 * @work: work item being executed
1187 *
1188 * This function is queued for execution when bio's reach the bio_lists[]
1189 * of throtl_data->service_queue. Those bio's are ready and issued by this
1190 * function.
1191 */
Fabian Frederick8876e1402014-04-17 21:41:16 +02001192static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001193{
1194 struct throtl_data *td = container_of(work, struct throtl_data,
1195 dispatch_work);
1196 struct throtl_service_queue *td_sq = &td->service_queue;
1197 struct request_queue *q = td->queue;
1198 struct bio_list bio_list_on_stack;
1199 struct bio *bio;
1200 struct blk_plug plug;
1201 int rw;
1202
1203 bio_list_init(&bio_list_on_stack);
1204
1205 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001206 for (rw = READ; rw <= WRITE; rw++)
1207 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1208 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001209 spin_unlock_irq(q->queue_lock);
1210
Tejun Heo6e1a5702013-05-14 13:52:37 -07001211 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001212 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001213 while((bio = bio_list_pop(&bio_list_on_stack)))
1214 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001215 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001216 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001217}
1218
Tejun Heof95a04a2012-04-16 13:57:26 -07001219static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1220 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001221{
Tejun Heof95a04a2012-04-16 13:57:26 -07001222 struct throtl_grp *tg = pd_to_tg(pd);
1223 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001224
Shaohua Li2ab54922017-03-27 10:51:29 -07001225 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001226 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001227 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001228}
1229
Tejun Heof95a04a2012-04-16 13:57:26 -07001230static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1231 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001232{
Tejun Heof95a04a2012-04-16 13:57:26 -07001233 struct throtl_grp *tg = pd_to_tg(pd);
1234 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001235
Shaohua Li2ab54922017-03-27 10:51:29 -07001236 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001237 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001238 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001239}
1240
Tejun Heo2da8ca82013-12-05 12:28:04 -05001241static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001242{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001243 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1244 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001245 return 0;
1246}
1247
Tejun Heo2da8ca82013-12-05 12:28:04 -05001248static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001249{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001250 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1251 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001252 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001253}
1254
Tejun Heo69948b02015-08-18 14:55:32 -07001255static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001256{
Tejun Heo69948b02015-08-18 14:55:32 -07001257 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001258 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001259 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001260
Tejun Heofda6f272013-05-14 13:52:36 -07001261 throtl_log(&tg->service_queue,
1262 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001263 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1264 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001265
1266 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001267 * Update has_rules[] flags for the updated tg's subtree. A tg is
1268 * considered to have rules if either the tg itself or any of its
1269 * ancestors has rules. This identifies groups without any
1270 * restrictions in the whole hierarchy and allows them to bypass
1271 * blk-throttle.
1272 */
Tejun Heo69948b02015-08-18 14:55:32 -07001273 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001274 tg_update_has_rules(blkg_to_tg(blkg));
1275
1276 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001277 * We're already holding queue_lock and know @tg is valid. Let's
1278 * apply the new config directly.
1279 *
1280 * Restart the slices for both READ and WRITES. It might happen
1281 * that a group's limit are dropped suddenly and we don't want to
1282 * account recently dispatched IO with new low rate.
1283 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001284 throtl_start_new_slice(tg, 0);
1285 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001286
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001287 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001288 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001289 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001290 }
Tejun Heo69948b02015-08-18 14:55:32 -07001291}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001292
Tejun Heo69948b02015-08-18 14:55:32 -07001293static ssize_t tg_set_conf(struct kernfs_open_file *of,
1294 char *buf, size_t nbytes, loff_t off, bool is_u64)
1295{
1296 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1297 struct blkg_conf_ctx ctx;
1298 struct throtl_grp *tg;
1299 int ret;
1300 u64 v;
1301
1302 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1303 if (ret)
1304 return ret;
1305
1306 ret = -EINVAL;
1307 if (sscanf(ctx.body, "%llu", &v) != 1)
1308 goto out_finish;
1309 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001310 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001311
1312 tg = blkg_to_tg(ctx.blkg);
1313
1314 if (is_u64)
1315 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1316 else
1317 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1318
1319 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001320 ret = 0;
1321out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001322 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001323 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001324}
1325
Tejun Heo451af502014-05-13 12:16:21 -04001326static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1327 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001328{
Tejun Heo451af502014-05-13 12:16:21 -04001329 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001330}
1331
Tejun Heo451af502014-05-13 12:16:21 -04001332static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1333 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334{
Tejun Heo451af502014-05-13 12:16:21 -04001335 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001336}
1337
Tejun Heo880f50e2015-08-18 14:55:30 -07001338static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001339 {
1340 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001341 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001342 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001343 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001344 },
1345 {
1346 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001347 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001348 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001349 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001350 },
1351 {
1352 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001353 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001354 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001355 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001356 },
1357 {
1358 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001359 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001360 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001361 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001362 },
1363 {
1364 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001365 .private = (unsigned long)&blkcg_policy_throtl,
1366 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001367 },
1368 {
1369 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001370 .private = (unsigned long)&blkcg_policy_throtl,
1371 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001372 },
1373 { } /* terminate */
1374};
1375
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001376static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001377 int off)
1378{
1379 struct throtl_grp *tg = pd_to_tg(pd);
1380 const char *dname = blkg_dev_name(pd->blkg);
1381 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001382 u64 bps_dft;
1383 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001384
1385 if (!dname)
1386 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001387
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001388 if (off == LIMIT_LOW) {
1389 bps_dft = 0;
1390 iops_dft = 0;
1391 } else {
1392 bps_dft = U64_MAX;
1393 iops_dft = UINT_MAX;
1394 }
1395
1396 if (tg->bps_conf[READ][off] == bps_dft &&
1397 tg->bps_conf[WRITE][off] == bps_dft &&
1398 tg->iops_conf[READ][off] == iops_dft &&
1399 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001400 return 0;
1401
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001402 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001403 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001404 tg->bps_conf[READ][off]);
1405 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001406 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001407 tg->bps_conf[WRITE][off]);
1408 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001409 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001410 tg->iops_conf[READ][off]);
1411 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001412 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001413 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001414
1415 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1416 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1417 return 0;
1418}
1419
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001420static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001421{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001422 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001423 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1424 return 0;
1425}
1426
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001427static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001428 char *buf, size_t nbytes, loff_t off)
1429{
1430 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1431 struct blkg_conf_ctx ctx;
1432 struct throtl_grp *tg;
1433 u64 v[4];
1434 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001435 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001436
1437 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1438 if (ret)
1439 return ret;
1440
1441 tg = blkg_to_tg(ctx.blkg);
1442
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001443 v[0] = tg->bps_conf[READ][index];
1444 v[1] = tg->bps_conf[WRITE][index];
1445 v[2] = tg->iops_conf[READ][index];
1446 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001447
1448 while (true) {
1449 char tok[27]; /* wiops=18446744073709551616 */
1450 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001451 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001452 int len;
1453
1454 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1455 break;
1456 if (tok[0] == '\0')
1457 break;
1458 ctx.body += len;
1459
1460 ret = -EINVAL;
1461 p = tok;
1462 strsep(&p, "=");
1463 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1464 goto out_finish;
1465
1466 ret = -ERANGE;
1467 if (!val)
1468 goto out_finish;
1469
1470 ret = -EINVAL;
1471 if (!strcmp(tok, "rbps"))
1472 v[0] = val;
1473 else if (!strcmp(tok, "wbps"))
1474 v[1] = val;
1475 else if (!strcmp(tok, "riops"))
1476 v[2] = min_t(u64, val, UINT_MAX);
1477 else if (!strcmp(tok, "wiops"))
1478 v[3] = min_t(u64, val, UINT_MAX);
1479 else
1480 goto out_finish;
1481 }
1482
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001483 tg->bps_conf[READ][index] = v[0];
1484 tg->bps_conf[WRITE][index] = v[1];
1485 tg->iops_conf[READ][index] = v[2];
1486 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001487
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001488 if (index == LIMIT_MAX) {
1489 tg->bps[READ][index] = v[0];
1490 tg->bps[WRITE][index] = v[1];
1491 tg->iops[READ][index] = v[2];
1492 tg->iops[WRITE][index] = v[3];
1493 }
1494 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1495 tg->bps_conf[READ][LIMIT_MAX]);
1496 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1497 tg->bps_conf[WRITE][LIMIT_MAX]);
1498 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1499 tg->iops_conf[READ][LIMIT_MAX]);
1500 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1501 tg->iops_conf[WRITE][LIMIT_MAX]);
1502
1503 if (index == LIMIT_LOW) {
1504 blk_throtl_update_limit_valid(tg->td);
1505 if (tg->td->limit_valid[LIMIT_LOW])
1506 tg->td->limit_index = LIMIT_LOW;
1507 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001508 tg_conf_updated(tg);
1509 ret = 0;
1510out_finish:
1511 blkg_conf_finish(&ctx);
1512 return ret ?: nbytes;
1513}
1514
1515static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001516#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1517 {
1518 .name = "low",
1519 .flags = CFTYPE_NOT_ON_ROOT,
1520 .seq_show = tg_print_limit,
1521 .write = tg_set_limit,
1522 .private = LIMIT_LOW,
1523 },
1524#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001525 {
1526 .name = "max",
1527 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001528 .seq_show = tg_print_limit,
1529 .write = tg_set_limit,
1530 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001531 },
1532 { } /* terminate */
1533};
1534
Vivek Goyalda527772011-03-02 19:05:33 -05001535static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001536{
1537 struct throtl_data *td = q->td;
1538
Tejun Heo69df0ab2013-05-14 13:52:36 -07001539 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001540}
1541
Tejun Heo3c798392012-04-16 13:57:25 -07001542static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001543 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001544 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001545
Tejun Heo001bea72015-08-18 14:55:11 -07001546 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001547 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001548 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001549 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001550 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001551};
1552
Shaohua Li3f0abd82017-03-27 10:51:35 -07001553static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1554{
1555 unsigned long rtime = jiffies, wtime = jiffies;
1556
1557 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1558 rtime = tg->last_low_overflow_time[READ];
1559 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1560 wtime = tg->last_low_overflow_time[WRITE];
1561 return min(rtime, wtime);
1562}
1563
1564/* tg should not be an intermediate node */
1565static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1566{
1567 struct throtl_service_queue *parent_sq;
1568 struct throtl_grp *parent = tg;
1569 unsigned long ret = __tg_last_low_overflow_time(tg);
1570
1571 while (true) {
1572 parent_sq = parent->service_queue.parent_sq;
1573 parent = sq_to_tg(parent_sq);
1574 if (!parent)
1575 break;
1576
1577 /*
1578 * The parent doesn't have low limit, it always reaches low
1579 * limit. Its overflow time is useless for children
1580 */
1581 if (!parent->bps[READ][LIMIT_LOW] &&
1582 !parent->iops[READ][LIMIT_LOW] &&
1583 !parent->bps[WRITE][LIMIT_LOW] &&
1584 !parent->iops[WRITE][LIMIT_LOW])
1585 continue;
1586 if (time_after(__tg_last_low_overflow_time(parent), ret))
1587 ret = __tg_last_low_overflow_time(parent);
1588 }
1589 return ret;
1590}
1591
Shaohua Lic79892c2017-03-27 10:51:34 -07001592static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1593{
1594 struct throtl_service_queue *sq = &tg->service_queue;
1595 bool read_limit, write_limit;
1596
1597 /*
1598 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1599 * reaches), it's ok to upgrade to next limit
1600 */
1601 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1602 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1603 if (!read_limit && !write_limit)
1604 return true;
1605 if (read_limit && sq->nr_queued[READ] &&
1606 (!write_limit || sq->nr_queued[WRITE]))
1607 return true;
1608 if (write_limit && sq->nr_queued[WRITE] &&
1609 (!read_limit || sq->nr_queued[READ]))
1610 return true;
1611 return false;
1612}
1613
1614static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1615{
1616 while (true) {
1617 if (throtl_tg_can_upgrade(tg))
1618 return true;
1619 tg = sq_to_tg(tg->service_queue.parent_sq);
1620 if (!tg || !tg_to_blkg(tg)->parent)
1621 return false;
1622 }
1623 return false;
1624}
1625
1626static bool throtl_can_upgrade(struct throtl_data *td,
1627 struct throtl_grp *this_tg)
1628{
1629 struct cgroup_subsys_state *pos_css;
1630 struct blkcg_gq *blkg;
1631
1632 if (td->limit_index != LIMIT_LOW)
1633 return false;
1634
Shaohua Li3f0abd82017-03-27 10:51:35 -07001635 if (time_before(jiffies, td->low_downgrade_time + throtl_slice))
1636 return false;
1637
Shaohua Lic79892c2017-03-27 10:51:34 -07001638 rcu_read_lock();
1639 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1640 struct throtl_grp *tg = blkg_to_tg(blkg);
1641
1642 if (tg == this_tg)
1643 continue;
1644 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1645 continue;
1646 if (!throtl_hierarchy_can_upgrade(tg)) {
1647 rcu_read_unlock();
1648 return false;
1649 }
1650 }
1651 rcu_read_unlock();
1652 return true;
1653}
1654
1655static void throtl_upgrade_state(struct throtl_data *td)
1656{
1657 struct cgroup_subsys_state *pos_css;
1658 struct blkcg_gq *blkg;
1659
1660 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001661 td->low_upgrade_time = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001662 rcu_read_lock();
1663 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1664 struct throtl_grp *tg = blkg_to_tg(blkg);
1665 struct throtl_service_queue *sq = &tg->service_queue;
1666
1667 tg->disptime = jiffies - 1;
1668 throtl_select_dispatch(sq);
1669 throtl_schedule_next_dispatch(sq, false);
1670 }
1671 rcu_read_unlock();
1672 throtl_select_dispatch(&td->service_queue);
1673 throtl_schedule_next_dispatch(&td->service_queue, false);
1674 queue_work(kthrotld_workqueue, &td->dispatch_work);
1675}
1676
Shaohua Li3f0abd82017-03-27 10:51:35 -07001677static void throtl_downgrade_state(struct throtl_data *td, int new)
1678{
1679 td->limit_index = new;
1680 td->low_downgrade_time = jiffies;
1681}
1682
1683static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1684{
1685 struct throtl_data *td = tg->td;
1686 unsigned long now = jiffies;
1687
1688 /*
1689 * If cgroup is below low limit, consider downgrade and throttle other
1690 * cgroups
1691 */
1692 if (time_after_eq(now, td->low_upgrade_time + throtl_slice) &&
1693 time_after_eq(now, tg_last_low_overflow_time(tg) + throtl_slice))
1694 return true;
1695 return false;
1696}
1697
1698static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1699{
1700 while (true) {
1701 if (!throtl_tg_can_downgrade(tg))
1702 return false;
1703 tg = sq_to_tg(tg->service_queue.parent_sq);
1704 if (!tg || !tg_to_blkg(tg)->parent)
1705 break;
1706 }
1707 return true;
1708}
1709
1710static void throtl_downgrade_check(struct throtl_grp *tg)
1711{
1712 uint64_t bps;
1713 unsigned int iops;
1714 unsigned long elapsed_time;
1715 unsigned long now = jiffies;
1716
1717 if (tg->td->limit_index != LIMIT_MAX ||
1718 !tg->td->limit_valid[LIMIT_LOW])
1719 return;
1720 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1721 return;
1722 if (time_after(tg->last_check_time + throtl_slice, now))
1723 return;
1724
1725 elapsed_time = now - tg->last_check_time;
1726 tg->last_check_time = now;
1727
1728 if (time_before(now, tg_last_low_overflow_time(tg) + throtl_slice))
1729 return;
1730
1731 if (tg->bps[READ][LIMIT_LOW]) {
1732 bps = tg->last_bytes_disp[READ] * HZ;
1733 do_div(bps, elapsed_time);
1734 if (bps >= tg->bps[READ][LIMIT_LOW])
1735 tg->last_low_overflow_time[READ] = now;
1736 }
1737
1738 if (tg->bps[WRITE][LIMIT_LOW]) {
1739 bps = tg->last_bytes_disp[WRITE] * HZ;
1740 do_div(bps, elapsed_time);
1741 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1742 tg->last_low_overflow_time[WRITE] = now;
1743 }
1744
1745 if (tg->iops[READ][LIMIT_LOW]) {
1746 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1747 if (iops >= tg->iops[READ][LIMIT_LOW])
1748 tg->last_low_overflow_time[READ] = now;
1749 }
1750
1751 if (tg->iops[WRITE][LIMIT_LOW]) {
1752 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1753 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1754 tg->last_low_overflow_time[WRITE] = now;
1755 }
1756
1757 /*
1758 * If cgroup is below low limit, consider downgrade and throttle other
1759 * cgroups
1760 */
1761 if (throtl_hierarchy_can_downgrade(tg))
1762 throtl_downgrade_state(tg->td, LIMIT_LOW);
1763
1764 tg->last_bytes_disp[READ] = 0;
1765 tg->last_bytes_disp[WRITE] = 0;
1766 tg->last_io_disp[READ] = 0;
1767 tg->last_io_disp[WRITE] = 0;
1768}
1769
Tejun Heoae118892015-08-18 14:55:20 -07001770bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1771 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001772{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001773 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001774 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001775 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001776 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001777 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001778
Tejun Heoae118892015-08-18 14:55:20 -07001779 WARN_ON_ONCE(!rcu_read_lock_held());
1780
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001781 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001782 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001783 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001784
1785 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001786
1787 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001788 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001789
Tejun Heo73f0d492013-05-14 13:52:35 -07001790 sq = &tg->service_queue;
1791
Shaohua Lic79892c2017-03-27 10:51:34 -07001792again:
Tejun Heo9e660ac2013-05-14 13:52:38 -07001793 while (true) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001794 if (tg->last_low_overflow_time[rw] == 0)
1795 tg->last_low_overflow_time[rw] = jiffies;
1796 throtl_downgrade_check(tg);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001797 /* throtl is FIFO - if bios are already queued, should queue */
1798 if (sq->nr_queued[rw])
1799 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001800
Tejun Heo9e660ac2013-05-14 13:52:38 -07001801 /* if above limits, break to queue */
Shaohua Lic79892c2017-03-27 10:51:34 -07001802 if (!tg_may_dispatch(tg, bio, NULL)) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001803 tg->last_low_overflow_time[rw] = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001804 if (throtl_can_upgrade(tg->td, tg)) {
1805 throtl_upgrade_state(tg->td);
1806 goto again;
1807 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001808 break;
Shaohua Lic79892c2017-03-27 10:51:34 -07001809 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001810
1811 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001812 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001813
1814 /*
1815 * We need to trim slice even when bios are not being queued
1816 * otherwise it might happen that a bio is not queued for
1817 * a long time and slice keeps on extending and trim is not
1818 * called for a long time. Now if limits are reduced suddenly
1819 * we take into account all the IO dispatched so far at new
1820 * low rate and * newly queued IO gets a really long dispatch
1821 * time.
1822 *
1823 * So keep on trimming slice even if bio is not queued.
1824 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001825 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001826
1827 /*
1828 * @bio passed through this layer without being throttled.
1829 * Climb up the ladder. If we''re already at the top, it
1830 * can be executed directly.
1831 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001832 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001833 sq = sq->parent_sq;
1834 tg = sq_to_tg(sq);
1835 if (!tg)
1836 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001837 }
1838
Tejun Heo9e660ac2013-05-14 13:52:38 -07001839 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001840 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1841 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001842 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1843 tg_bps_limit(tg, rw),
1844 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001845 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001846
Shaohua Li3f0abd82017-03-27 10:51:35 -07001847 tg->last_low_overflow_time[rw] = jiffies;
1848
Tejun Heo671058f2012-03-05 13:15:29 -08001849 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001850 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001851 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001852 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001853
Tejun Heo7f52f982013-05-14 13:52:37 -07001854 /*
1855 * Update @tg's dispatch time and force schedule dispatch if @tg
1856 * was empty before @bio. The forced scheduling isn't likely to
1857 * cause undue delay as @bio is likely to be dispatched directly if
1858 * its @tg's disptime is not in the future.
1859 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001860 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001861 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001862 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001863 }
1864
Tejun Heobc16a4f2011-10-19 14:33:01 +02001865out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001866 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001867out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001868 /*
1869 * As multiple blk-throtls may stack in the same issue path, we
1870 * don't want bios to leave with the flag set. Clear the flag if
1871 * being issued.
1872 */
1873 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001874 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001875 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001876}
1877
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001878/*
1879 * Dispatch all bios from all children tg's queued on @parent_sq. On
1880 * return, @parent_sq is guaranteed to not have any active children tg's
1881 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1882 */
1883static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1884{
1885 struct throtl_grp *tg;
1886
1887 while ((tg = throtl_rb_first(parent_sq))) {
1888 struct throtl_service_queue *sq = &tg->service_queue;
1889 struct bio *bio;
1890
1891 throtl_dequeue_tg(tg);
1892
Tejun Heoc5cc2072013-05-14 13:52:38 -07001893 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001894 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001895 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001896 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1897 }
1898}
1899
Tejun Heoc9a929d2011-10-19 14:42:16 +02001900/**
1901 * blk_throtl_drain - drain throttled bios
1902 * @q: request_queue to drain throttled bios for
1903 *
1904 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1905 */
1906void blk_throtl_drain(struct request_queue *q)
1907 __releases(q->queue_lock) __acquires(q->queue_lock)
1908{
1909 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001910 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001911 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001912 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001913 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001914
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001915 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001916 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001917
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001918 /*
1919 * Drain each tg while doing post-order walk on the blkg tree, so
1920 * that all bios are propagated to td->service_queue. It'd be
1921 * better to walk service_queue tree directly but blkg walk is
1922 * easier.
1923 */
Tejun Heo492eb212013-08-08 20:11:25 -04001924 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001925 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001926
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001927 /* finally, transfer bios from top-level tg's into the td */
1928 tg_drain_bios(&td->service_queue);
1929
1930 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001931 spin_unlock_irq(q->queue_lock);
1932
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001933 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001934 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001935 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1936 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001937 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001938
1939 spin_lock_irq(q->queue_lock);
1940}
1941
Vivek Goyale43473b2010-09-15 17:06:35 -04001942int blk_throtl_init(struct request_queue *q)
1943{
1944 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001945 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001946
1947 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1948 if (!td)
1949 return -ENOMEM;
1950
Tejun Heo69df0ab2013-05-14 13:52:36 -07001951 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001952 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001953
Tejun Heocd1604f2012-03-05 13:15:06 -08001954 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001955 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001956
Shaohua Li9f626e32017-03-27 10:51:30 -07001957 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001958 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001959 td->low_upgrade_time = jiffies;
1960 td->low_downgrade_time = jiffies;
Tejun Heoa2b16932012-04-13 13:11:33 -07001961 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001962 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001963 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001964 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001965 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001966}
1967
1968void blk_throtl_exit(struct request_queue *q)
1969{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001970 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001971 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001972 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001973 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04001974}
1975
1976static int __init throtl_init(void)
1977{
Vivek Goyal450adcb2011-03-01 13:40:54 -05001978 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1979 if (!kthrotld_workqueue)
1980 panic("Failed to create kthrotld\n");
1981
Tejun Heo3c798392012-04-16 13:57:25 -07001982 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04001983}
1984
1985module_init(throtl_init);