blob: 014b2e96a423da2e4526b78f2b537595d613c8e2 [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
Shaohua Lid61fcfa2017-03-27 10:51:38 -070021/* Throttling is performed over a slice and after that slice is renewed */
22#define DFL_THROTL_SLICE_HD (HZ / 10)
23#define DFL_THROTL_SLICE_SSD (HZ / 50)
Shaohua Li297e3d82017-03-27 10:51:37 -070024#define MAX_THROTL_SLICE (HZ)
Vivek Goyale43473b2010-09-15 17:06:35 -040025
Tejun Heo3c798392012-04-16 13:57:25 -070026static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080027
Vivek Goyal450adcb2011-03-01 13:40:54 -050028/* A workqueue to queue throttle related work */
29static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050030
Tejun Heoc5cc2072013-05-14 13:52:38 -070031/*
32 * To implement hierarchical throttling, throtl_grps form a tree and bios
33 * are dispatched upwards level by level until they reach the top and get
34 * issued. When dispatching bios from the children and local group at each
35 * level, if the bios are dispatched into a single bio_list, there's a risk
36 * of a local or child group which can queue many bios at once filling up
37 * the list starving others.
38 *
39 * To avoid such starvation, dispatched bios are queued separately
40 * according to where they came from. When they are again dispatched to
41 * the parent, they're popped in round-robin order so that no single source
42 * hogs the dispatch window.
43 *
44 * throtl_qnode is used to keep the queued bios separated by their sources.
45 * Bios are queued to throtl_qnode which in turn is queued to
46 * throtl_service_queue and then dispatched in round-robin order.
47 *
48 * It's also used to track the reference counts on blkg's. A qnode always
49 * belongs to a throtl_grp and gets queued on itself or the parent, so
50 * incrementing the reference of the associated throtl_grp when a qnode is
51 * queued and decrementing when dequeued is enough to keep the whole blkg
52 * tree pinned while bios are in flight.
53 */
54struct throtl_qnode {
55 struct list_head node; /* service_queue->queued[] */
56 struct bio_list bios; /* queued bios */
57 struct throtl_grp *tg; /* tg this qnode belongs to */
58};
59
Tejun Heoc9e03322013-05-14 13:52:32 -070060struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070061 struct throtl_service_queue *parent_sq; /* the parent service_queue */
62
Tejun Heo73f0d492013-05-14 13:52:35 -070063 /*
64 * Bios queued directly to this service_queue or dispatched from
65 * children throtl_grp's.
66 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070067 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070068 unsigned int nr_queued[2]; /* number of queued bios */
69
70 /*
71 * RB tree of active children throtl_grp's, which are sorted by
72 * their ->disptime.
73 */
Tejun Heoc9e03322013-05-14 13:52:32 -070074 struct rb_root pending_tree; /* RB tree of active tgs */
75 struct rb_node *first_pending; /* first node in the tree */
76 unsigned int nr_pending; /* # queued in the tree */
77 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070078 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040079};
80
Tejun Heo5b2c16a2013-05-14 13:52:32 -070081enum tg_state_flags {
82 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070083 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070084};
85
Vivek Goyale43473b2010-09-15 17:06:35 -040086#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
87
Shaohua Li9f626e32017-03-27 10:51:30 -070088enum {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070089 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070090 LIMIT_MAX,
91 LIMIT_CNT,
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
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700130 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700131 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700132 /* user configured bps limits */
133 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400134
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700135 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700136 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700137 /* user configured IOPS limits */
138 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400139
Vivek Goyale43473b2010-09-15 17:06:35 -0400140 /* Number of bytes disptached in current slice */
141 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400142 /* Number of bio's dispatched in current slice */
143 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400144
Shaohua Li3f0abd82017-03-27 10:51:35 -0700145 unsigned long last_low_overflow_time[2];
146
147 uint64_t last_bytes_disp[2];
148 unsigned int last_io_disp[2];
149
150 unsigned long last_check_time;
151
Shaohua Liaec24242017-03-27 10:51:39 -0700152 unsigned long last_dispatch_time[2];
153
Vivek Goyale43473b2010-09-15 17:06:35 -0400154 /* When did we start a new slice */
155 unsigned long slice_start[2];
156 unsigned long slice_end[2];
157};
158
159struct throtl_data
160{
Vivek Goyale43473b2010-09-15 17:06:35 -0400161 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700162 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400163
Vivek Goyale43473b2010-09-15 17:06:35 -0400164 struct request_queue *queue;
165
166 /* Total Number of queued bios on READ and WRITE lists */
167 unsigned int nr_queued[2];
168
Shaohua Li297e3d82017-03-27 10:51:37 -0700169 unsigned int throtl_slice;
170
Vivek Goyale43473b2010-09-15 17:06:35 -0400171 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700172 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700173 unsigned int limit_index;
174 bool limit_valid[LIMIT_CNT];
Shaohua Li3f0abd82017-03-27 10:51:35 -0700175
176 unsigned long low_upgrade_time;
177 unsigned long low_downgrade_time;
Vivek Goyale43473b2010-09-15 17:06:35 -0400178};
179
Tejun Heo69df0ab2013-05-14 13:52:36 -0700180static void throtl_pending_timer_fn(unsigned long arg);
181
Tejun Heof95a04a2012-04-16 13:57:26 -0700182static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
183{
184 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
185}
186
Tejun Heo3c798392012-04-16 13:57:25 -0700187static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800188{
Tejun Heof95a04a2012-04-16 13:57:26 -0700189 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800190}
191
Tejun Heo3c798392012-04-16 13:57:25 -0700192static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800193{
Tejun Heof95a04a2012-04-16 13:57:26 -0700194 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800195}
196
Tejun Heofda6f272013-05-14 13:52:36 -0700197/**
198 * sq_to_tg - return the throl_grp the specified service queue belongs to
199 * @sq: the throtl_service_queue of interest
200 *
201 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
202 * embedded in throtl_data, %NULL is returned.
203 */
204static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
205{
206 if (sq && sq->parent_sq)
207 return container_of(sq, struct throtl_grp, service_queue);
208 else
209 return NULL;
210}
Vivek Goyale43473b2010-09-15 17:06:35 -0400211
Tejun Heofda6f272013-05-14 13:52:36 -0700212/**
213 * sq_to_td - return throtl_data the specified service queue belongs to
214 * @sq: the throtl_service_queue of interest
215 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800216 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700217 * Determine the associated throtl_data accordingly and return it.
218 */
219static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
220{
221 struct throtl_grp *tg = sq_to_tg(sq);
222
223 if (tg)
224 return tg->td;
225 else
226 return container_of(sq, struct throtl_data, service_queue);
227}
228
Shaohua Li9f626e32017-03-27 10:51:30 -0700229static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
230{
Shaohua Lib22c4172017-03-27 10:51:33 -0700231 struct blkcg_gq *blkg = tg_to_blkg(tg);
232 uint64_t ret;
233
234 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
235 return U64_MAX;
236 ret = tg->bps[rw][tg->td->limit_index];
237 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
238 return tg->bps[rw][LIMIT_MAX];
239 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700240}
241
242static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
243{
Shaohua Lib22c4172017-03-27 10:51:33 -0700244 struct blkcg_gq *blkg = tg_to_blkg(tg);
245 unsigned int ret;
246
247 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
248 return UINT_MAX;
249 ret = tg->iops[rw][tg->td->limit_index];
250 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
251 return tg->iops[rw][LIMIT_MAX];
252 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700253}
254
Tejun Heofda6f272013-05-14 13:52:36 -0700255/**
256 * throtl_log - log debug message via blktrace
257 * @sq: the service_queue being reported
258 * @fmt: printf format string
259 * @args: printf args
260 *
261 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
262 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700263 */
264#define throtl_log(sq, fmt, args...) do { \
265 struct throtl_grp *__tg = sq_to_tg((sq)); \
266 struct throtl_data *__td = sq_to_td((sq)); \
267 \
268 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700269 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
270 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700271 if ((__tg)) { \
272 char __pbuf[128]; \
273 \
274 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
275 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
276 } else { \
277 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
278 } \
279} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400280
Tejun Heoc5cc2072013-05-14 13:52:38 -0700281static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
282{
283 INIT_LIST_HEAD(&qn->node);
284 bio_list_init(&qn->bios);
285 qn->tg = tg;
286}
287
288/**
289 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
290 * @bio: bio being added
291 * @qn: qnode to add bio to
292 * @queued: the service_queue->queued[] list @qn belongs to
293 *
294 * Add @bio to @qn and put @qn on @queued if it's not already on.
295 * @qn->tg's reference count is bumped when @qn is activated. See the
296 * comment on top of throtl_qnode definition for details.
297 */
298static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
299 struct list_head *queued)
300{
301 bio_list_add(&qn->bios, bio);
302 if (list_empty(&qn->node)) {
303 list_add_tail(&qn->node, queued);
304 blkg_get(tg_to_blkg(qn->tg));
305 }
306}
307
308/**
309 * throtl_peek_queued - peek the first bio on a qnode list
310 * @queued: the qnode list to peek
311 */
312static struct bio *throtl_peek_queued(struct list_head *queued)
313{
314 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
315 struct bio *bio;
316
317 if (list_empty(queued))
318 return NULL;
319
320 bio = bio_list_peek(&qn->bios);
321 WARN_ON_ONCE(!bio);
322 return bio;
323}
324
325/**
326 * throtl_pop_queued - pop the first bio form a qnode list
327 * @queued: the qnode list to pop a bio from
328 * @tg_to_put: optional out argument for throtl_grp to put
329 *
330 * Pop the first bio from the qnode list @queued. After popping, the first
331 * qnode is removed from @queued if empty or moved to the end of @queued so
332 * that the popping order is round-robin.
333 *
334 * When the first qnode is removed, its associated throtl_grp should be put
335 * too. If @tg_to_put is NULL, this function automatically puts it;
336 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
337 * responsible for putting it.
338 */
339static struct bio *throtl_pop_queued(struct list_head *queued,
340 struct throtl_grp **tg_to_put)
341{
342 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
343 struct bio *bio;
344
345 if (list_empty(queued))
346 return NULL;
347
348 bio = bio_list_pop(&qn->bios);
349 WARN_ON_ONCE(!bio);
350
351 if (bio_list_empty(&qn->bios)) {
352 list_del_init(&qn->node);
353 if (tg_to_put)
354 *tg_to_put = qn->tg;
355 else
356 blkg_put(tg_to_blkg(qn->tg));
357 } else {
358 list_move_tail(&qn->node, queued);
359 }
360
361 return bio;
362}
363
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700364/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700365static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700366{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700367 INIT_LIST_HEAD(&sq->queued[0]);
368 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700369 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700370 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
371 (unsigned long)sq);
372}
373
Tejun Heo001bea72015-08-18 14:55:11 -0700374static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
375{
Tejun Heo4fb72032015-08-18 14:55:12 -0700376 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700377 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700378
379 tg = kzalloc_node(sizeof(*tg), gfp, node);
380 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700381 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700382
Tejun Heob2ce2642015-08-18 14:55:13 -0700383 throtl_service_queue_init(&tg->service_queue);
384
385 for (rw = READ; rw <= WRITE; rw++) {
386 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
387 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
388 }
389
390 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700391 tg->bps[READ][LIMIT_MAX] = U64_MAX;
392 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
393 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
394 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700395 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
396 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
397 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
398 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
399 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700400
Tejun Heo4fb72032015-08-18 14:55:12 -0700401 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700402}
403
Tejun Heoa9520cd2015-08-18 14:55:14 -0700404static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400405{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700406 struct throtl_grp *tg = pd_to_tg(pd);
407 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700408 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700409 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800410
Tejun Heo91381252013-05-14 13:52:38 -0700411 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400412 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700413 * behavior where limits on a given throtl_grp are applied to the
414 * whole subtree rather than just the group itself. e.g. If 16M
415 * read_bps limit is set on the root group, the whole system can't
416 * exceed 16M for the device.
417 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400418 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700419 * behavior is retained where all throtl_grps are treated as if
420 * they're all separate root groups right below throtl_data.
421 * Limits of a group don't interact with limits of other groups
422 * regardless of the position of the group in the hierarchy.
423 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700424 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400425 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700426 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700427 tg->td = td;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700428}
429
Tejun Heo693e7512013-05-14 13:52:38 -0700430/*
431 * Set has_rules[] if @tg or any of its parents have limits configured.
432 * This doesn't require walking up to the top of the hierarchy as the
433 * parent's has_rules[] is guaranteed to be correct.
434 */
435static void tg_update_has_rules(struct throtl_grp *tg)
436{
437 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700438 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700439 int rw;
440
441 for (rw = READ; rw <= WRITE; rw++)
442 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700443 (td->limit_valid[td->limit_index] &&
444 (tg_bps_limit(tg, rw) != U64_MAX ||
445 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700446}
447
Tejun Heoa9520cd2015-08-18 14:55:14 -0700448static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700449{
Shaohua Liaec24242017-03-27 10:51:39 -0700450 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo693e7512013-05-14 13:52:38 -0700451 /*
452 * We don't want new groups to escape the limits of its ancestors.
453 * Update has_rules[] after a new group is brought online.
454 */
Shaohua Liaec24242017-03-27 10:51:39 -0700455 tg_update_has_rules(tg);
456 tg->last_dispatch_time[READ] = jiffies;
457 tg->last_dispatch_time[WRITE] = jiffies;
Tejun Heo693e7512013-05-14 13:52:38 -0700458}
459
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700460static void blk_throtl_update_limit_valid(struct throtl_data *td)
461{
462 struct cgroup_subsys_state *pos_css;
463 struct blkcg_gq *blkg;
464 bool low_valid = false;
465
466 rcu_read_lock();
467 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
468 struct throtl_grp *tg = blkg_to_tg(blkg);
469
470 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
471 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
472 low_valid = true;
473 }
474 rcu_read_unlock();
475
476 td->limit_valid[LIMIT_LOW] = low_valid;
477}
478
Shaohua Lic79892c2017-03-27 10:51:34 -0700479static void throtl_upgrade_state(struct throtl_data *td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700480static void throtl_pd_offline(struct blkg_policy_data *pd)
481{
482 struct throtl_grp *tg = pd_to_tg(pd);
483
484 tg->bps[READ][LIMIT_LOW] = 0;
485 tg->bps[WRITE][LIMIT_LOW] = 0;
486 tg->iops[READ][LIMIT_LOW] = 0;
487 tg->iops[WRITE][LIMIT_LOW] = 0;
488
489 blk_throtl_update_limit_valid(tg->td);
490
Shaohua Lic79892c2017-03-27 10:51:34 -0700491 if (!tg->td->limit_valid[tg->td->limit_index])
492 throtl_upgrade_state(tg->td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700493}
494
Tejun Heo001bea72015-08-18 14:55:11 -0700495static void throtl_pd_free(struct blkg_policy_data *pd)
496{
Tejun Heo4fb72032015-08-18 14:55:12 -0700497 struct throtl_grp *tg = pd_to_tg(pd);
498
Tejun Heob2ce2642015-08-18 14:55:13 -0700499 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700500 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700501}
502
Tejun Heo0049af72013-05-14 13:52:33 -0700503static struct throtl_grp *
504throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400505{
506 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700507 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400508 return NULL;
509
Tejun Heo0049af72013-05-14 13:52:33 -0700510 if (!parent_sq->first_pending)
511 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400512
Tejun Heo0049af72013-05-14 13:52:33 -0700513 if (parent_sq->first_pending)
514 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400515
516 return NULL;
517}
518
519static void rb_erase_init(struct rb_node *n, struct rb_root *root)
520{
521 rb_erase(n, root);
522 RB_CLEAR_NODE(n);
523}
524
Tejun Heo0049af72013-05-14 13:52:33 -0700525static void throtl_rb_erase(struct rb_node *n,
526 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400527{
Tejun Heo0049af72013-05-14 13:52:33 -0700528 if (parent_sq->first_pending == n)
529 parent_sq->first_pending = NULL;
530 rb_erase_init(n, &parent_sq->pending_tree);
531 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400532}
533
Tejun Heo0049af72013-05-14 13:52:33 -0700534static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400535{
536 struct throtl_grp *tg;
537
Tejun Heo0049af72013-05-14 13:52:33 -0700538 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400539 if (!tg)
540 return;
541
Tejun Heo0049af72013-05-14 13:52:33 -0700542 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400543}
544
Tejun Heo77216b02013-05-14 13:52:36 -0700545static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400546{
Tejun Heo77216b02013-05-14 13:52:36 -0700547 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700548 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400549 struct rb_node *parent = NULL;
550 struct throtl_grp *__tg;
551 unsigned long key = tg->disptime;
552 int left = 1;
553
554 while (*node != NULL) {
555 parent = *node;
556 __tg = rb_entry_tg(parent);
557
558 if (time_before(key, __tg->disptime))
559 node = &parent->rb_left;
560 else {
561 node = &parent->rb_right;
562 left = 0;
563 }
564 }
565
566 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700567 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400568
569 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700570 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400571}
572
Tejun Heo77216b02013-05-14 13:52:36 -0700573static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400574{
Tejun Heo77216b02013-05-14 13:52:36 -0700575 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700576 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700577 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400578}
579
Tejun Heo77216b02013-05-14 13:52:36 -0700580static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400581{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700582 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700583 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400584}
585
Tejun Heo77216b02013-05-14 13:52:36 -0700586static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400587{
Tejun Heo77216b02013-05-14 13:52:36 -0700588 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700589 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400590}
591
Tejun Heo77216b02013-05-14 13:52:36 -0700592static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400593{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700594 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700595 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400596}
597
Tejun Heoa9131a22013-05-14 13:52:31 -0700598/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700599static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
600 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700601{
Shaohua Li297e3d82017-03-27 10:51:37 -0700602 unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice;
Shaohua Li06cceed2017-03-27 10:51:36 -0700603
604 /*
605 * Since we are adjusting the throttle limit dynamically, the sleep
606 * time calculated according to previous limit might be invalid. It's
607 * possible the cgroup sleep time is very long and no other cgroups
608 * have IO running so notify the limit changes. Make sure the cgroup
609 * doesn't sleep too long to avoid the missed notification.
610 */
611 if (time_after(expires, max_expire))
612 expires = max_expire;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700613 mod_timer(&sq->pending_timer, expires);
614 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
615 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700616}
617
Tejun Heo7f52f982013-05-14 13:52:37 -0700618/**
619 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
620 * @sq: the service_queue to schedule dispatch for
621 * @force: force scheduling
622 *
623 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
624 * dispatch time of the first pending child. Returns %true if either timer
625 * is armed or there's no pending child left. %false if the current
626 * dispatch window is still open and the caller should continue
627 * dispatching.
628 *
629 * If @force is %true, the dispatch timer is always scheduled and this
630 * function is guaranteed to return %true. This is to be used when the
631 * caller can't dispatch itself and needs to invoke pending_timer
632 * unconditionally. Note that forced scheduling is likely to induce short
633 * delay before dispatch starts even if @sq->first_pending_disptime is not
634 * in the future and thus shouldn't be used in hot paths.
635 */
636static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
637 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400638{
Tejun Heo6a525602013-05-14 13:52:32 -0700639 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700640 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700641 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400642
Tejun Heoc9e03322013-05-14 13:52:32 -0700643 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400644
Tejun Heo69df0ab2013-05-14 13:52:36 -0700645 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700646 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700647 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700648 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700649 }
650
Tejun Heo7f52f982013-05-14 13:52:37 -0700651 /* tell the caller to continue dispatching */
652 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400653}
654
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700655static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
656 bool rw, unsigned long start)
657{
658 tg->bytes_disp[rw] = 0;
659 tg->io_disp[rw] = 0;
660
661 /*
662 * Previous slice has expired. We must have trimmed it after last
663 * bio dispatch. That means since start of last slice, we never used
664 * that bandwidth. Do try to make use of that bandwidth while giving
665 * credit.
666 */
667 if (time_after_eq(start, tg->slice_start[rw]))
668 tg->slice_start[rw] = start;
669
Shaohua Li297e3d82017-03-27 10:51:37 -0700670 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700671 throtl_log(&tg->service_queue,
672 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
673 rw == READ ? 'R' : 'W', tg->slice_start[rw],
674 tg->slice_end[rw], jiffies);
675}
676
Tejun Heo0f3457f2013-05-14 13:52:32 -0700677static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400678{
679 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400680 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400681 tg->slice_start[rw] = jiffies;
Shaohua Li297e3d82017-03-27 10:51:37 -0700682 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700683 throtl_log(&tg->service_queue,
684 "[%c] new slice start=%lu end=%lu jiffies=%lu",
685 rw == READ ? 'R' : 'W', tg->slice_start[rw],
686 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400687}
688
Tejun Heo0f3457f2013-05-14 13:52:32 -0700689static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
690 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100691{
Shaohua Li297e3d82017-03-27 10:51:37 -0700692 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100693}
694
Tejun Heo0f3457f2013-05-14 13:52:32 -0700695static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
696 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400697{
Shaohua Li297e3d82017-03-27 10:51:37 -0700698 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700699 throtl_log(&tg->service_queue,
700 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
701 rw == READ ? 'R' : 'W', tg->slice_start[rw],
702 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400703}
704
705/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700706static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400707{
708 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200709 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400710
711 return 1;
712}
713
714/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700715static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400716{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200717 unsigned long nr_slices, time_elapsed, io_trim;
718 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400719
720 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
721
722 /*
723 * If bps are unlimited (-1), then time slice don't get
724 * renewed. Don't try to trim the slice if slice is used. A new
725 * slice will start when appropriate.
726 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700727 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400728 return;
729
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100730 /*
731 * A bio has been dispatched. Also adjust slice_end. It might happen
732 * that initially cgroup limit was very low resulting in high
733 * slice_end, but later limit was bumped up and bio was dispached
734 * sooner, then we need to reduce slice_end. A high bogus slice_end
735 * is bad because it does not allow new slice to start.
736 */
737
Shaohua Li297e3d82017-03-27 10:51:37 -0700738 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100739
Vivek Goyale43473b2010-09-15 17:06:35 -0400740 time_elapsed = jiffies - tg->slice_start[rw];
741
Shaohua Li297e3d82017-03-27 10:51:37 -0700742 nr_slices = time_elapsed / tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400743
744 if (!nr_slices)
745 return;
Shaohua Li297e3d82017-03-27 10:51:37 -0700746 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200747 do_div(tmp, HZ);
748 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400749
Shaohua Li297e3d82017-03-27 10:51:37 -0700750 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) /
751 HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400752
Vivek Goyal8e89d132010-09-15 17:06:37 -0400753 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400754 return;
755
756 if (tg->bytes_disp[rw] >= bytes_trim)
757 tg->bytes_disp[rw] -= bytes_trim;
758 else
759 tg->bytes_disp[rw] = 0;
760
Vivek Goyal8e89d132010-09-15 17:06:37 -0400761 if (tg->io_disp[rw] >= io_trim)
762 tg->io_disp[rw] -= io_trim;
763 else
764 tg->io_disp[rw] = 0;
765
Shaohua Li297e3d82017-03-27 10:51:37 -0700766 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400767
Tejun Heofda6f272013-05-14 13:52:36 -0700768 throtl_log(&tg->service_queue,
769 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
770 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
771 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400772}
773
Tejun Heo0f3457f2013-05-14 13:52:32 -0700774static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
775 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400776{
777 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400778 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400779 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200780 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400781
Vivek Goyal8e89d132010-09-15 17:06:37 -0400782 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400783
Vivek Goyal8e89d132010-09-15 17:06:37 -0400784 /* Slice has just started. Consider one slice interval */
785 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700786 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400787
Shaohua Li297e3d82017-03-27 10:51:37 -0700788 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400789
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200790 /*
791 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
792 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
793 * will allow dispatch after 1 second and after that slice should
794 * have been trimmed.
795 */
796
Shaohua Li9f626e32017-03-27 10:51:30 -0700797 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200798 do_div(tmp, HZ);
799
800 if (tmp > UINT_MAX)
801 io_allowed = UINT_MAX;
802 else
803 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400804
805 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400806 if (wait)
807 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200808 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400809 }
810
Vivek Goyal8e89d132010-09-15 17:06:37 -0400811 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700812 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400813
814 if (jiffy_wait > jiffy_elapsed)
815 jiffy_wait = jiffy_wait - jiffy_elapsed;
816 else
817 jiffy_wait = 1;
818
819 if (wait)
820 *wait = jiffy_wait;
821 return 0;
822}
823
Tejun Heo0f3457f2013-05-14 13:52:32 -0700824static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
825 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400826{
827 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200828 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400829 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400830
831 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
832
833 /* Slice has just started. Consider one slice interval */
834 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700835 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400836
Shaohua Li297e3d82017-03-27 10:51:37 -0700837 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyale43473b2010-09-15 17:06:35 -0400838
Shaohua Li9f626e32017-03-27 10:51:30 -0700839 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200840 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200841 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400842
Kent Overstreet4f024f32013-10-11 15:44:27 -0700843 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400844 if (wait)
845 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200846 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400847 }
848
849 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700850 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700851 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400852
853 if (!jiffy_wait)
854 jiffy_wait = 1;
855
856 /*
857 * This wait time is without taking into consideration the rounding
858 * up we did. Add that time also.
859 */
860 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400861 if (wait)
862 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400863 return 0;
864}
Vivek Goyale43473b2010-09-15 17:06:35 -0400865
Vivek Goyal8e89d132010-09-15 17:06:37 -0400866/*
867 * Returns whether one can dispatch a bio or not. Also returns approx number
868 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
869 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700870static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
871 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400872{
873 bool rw = bio_data_dir(bio);
874 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
875
876 /*
877 * Currently whole state machine of group depends on first bio
878 * queued in the group bio list. So one should not be calling
879 * this function with a different bio if there are other bios
880 * queued.
881 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700882 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700883 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400884
885 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700886 if (tg_bps_limit(tg, rw) == U64_MAX &&
887 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400888 if (wait)
889 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200890 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400891 }
892
893 /*
894 * If previous slice expired, start a new one otherwise renew/extend
895 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600896 * long since now. New slice is started only for empty throttle group.
897 * If there is queued bio, that means there should be an active
898 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400899 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600900 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700901 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400902 else {
Shaohua Li297e3d82017-03-27 10:51:37 -0700903 if (time_before(tg->slice_end[rw],
904 jiffies + tg->td->throtl_slice))
905 throtl_extend_slice(tg, rw,
906 jiffies + tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400907 }
908
Tejun Heo0f3457f2013-05-14 13:52:32 -0700909 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
910 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400911 if (wait)
912 *wait = 0;
913 return 1;
914 }
915
916 max_wait = max(bps_wait, iops_wait);
917
918 if (wait)
919 *wait = max_wait;
920
921 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700922 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400923
924 return 0;
925}
926
927static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
928{
929 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -0400930
931 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700932 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400933 tg->io_disp[rw]++;
Shaohua Li3f0abd82017-03-27 10:51:35 -0700934 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
935 tg->last_io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400936
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700937 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200938 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700939 * more than once as a throttled bio will go through blk-throtl the
940 * second time when it eventually gets issued. Set it when a bio
941 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -0700942 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +0200943 if (!bio_flagged(bio, BIO_THROTTLED))
944 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -0400945}
946
Tejun Heoc5cc2072013-05-14 13:52:38 -0700947/**
948 * throtl_add_bio_tg - add a bio to the specified throtl_grp
949 * @bio: bio to add
950 * @qn: qnode to use
951 * @tg: the target throtl_grp
952 *
953 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
954 * tg->qnode_on_self[] is used.
955 */
956static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
957 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400958{
Tejun Heo73f0d492013-05-14 13:52:35 -0700959 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400960 bool rw = bio_data_dir(bio);
961
Tejun Heoc5cc2072013-05-14 13:52:38 -0700962 if (!qn)
963 qn = &tg->qnode_on_self[rw];
964
Tejun Heo0e9f4162013-05-14 13:52:35 -0700965 /*
966 * If @tg doesn't currently have any bios queued in the same
967 * direction, queueing @bio can change when @tg should be
968 * dispatched. Mark that @tg was empty. This is automatically
969 * cleaered on the next tg_update_disptime().
970 */
971 if (!sq->nr_queued[rw])
972 tg->flags |= THROTL_TG_WAS_EMPTY;
973
Tejun Heoc5cc2072013-05-14 13:52:38 -0700974 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
975
Tejun Heo73f0d492013-05-14 13:52:35 -0700976 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -0700977 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400978}
979
Tejun Heo77216b02013-05-14 13:52:36 -0700980static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400981{
Tejun Heo73f0d492013-05-14 13:52:35 -0700982 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400983 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
984 struct bio *bio;
985
Markus Elfringd609af32017-01-21 22:15:33 +0100986 bio = throtl_peek_queued(&sq->queued[READ]);
987 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700988 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400989
Markus Elfringd609af32017-01-21 22:15:33 +0100990 bio = throtl_peek_queued(&sq->queued[WRITE]);
991 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -0700992 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -0400993
994 min_wait = min(read_wait, write_wait);
995 disptime = jiffies + min_wait;
996
Vivek Goyale43473b2010-09-15 17:06:35 -0400997 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -0700998 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400999 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -07001000 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -07001001
1002 /* see throtl_add_bio_tg() */
1003 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -04001004}
1005
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001006static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1007 struct throtl_grp *parent_tg, bool rw)
1008{
1009 if (throtl_slice_used(parent_tg, rw)) {
1010 throtl_start_new_slice_with_credit(parent_tg, rw,
1011 child_tg->slice_start[rw]);
1012 }
1013
1014}
1015
Tejun Heo77216b02013-05-14 13:52:36 -07001016static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001017{
Tejun Heo73f0d492013-05-14 13:52:35 -07001018 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001019 struct throtl_service_queue *parent_sq = sq->parent_sq;
1020 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001021 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001022 struct bio *bio;
1023
Tejun Heoc5cc2072013-05-14 13:52:38 -07001024 /*
1025 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1026 * from @tg may put its reference and @parent_sq might end up
1027 * getting released prematurely. Remember the tg to put and put it
1028 * after @bio is transferred to @parent_sq.
1029 */
1030 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001031 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001032
1033 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001034
1035 /*
1036 * If our parent is another tg, we just need to transfer @bio to
1037 * the parent using throtl_add_bio_tg(). If our parent is
1038 * @td->service_queue, @bio is ready to be issued. Put it on its
1039 * bio_lists[] and decrease total number queued. The caller is
1040 * responsible for issuing these bios.
1041 */
1042 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001043 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001044 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001045 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001046 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1047 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001048 BUG_ON(tg->td->nr_queued[rw] <= 0);
1049 tg->td->nr_queued[rw]--;
1050 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001051
Tejun Heo0f3457f2013-05-14 13:52:32 -07001052 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001053
Tejun Heoc5cc2072013-05-14 13:52:38 -07001054 if (tg_to_put)
1055 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001056}
1057
Tejun Heo77216b02013-05-14 13:52:36 -07001058static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001059{
Tejun Heo73f0d492013-05-14 13:52:35 -07001060 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001061 unsigned int nr_reads = 0, nr_writes = 0;
1062 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001063 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001064 struct bio *bio;
1065
1066 /* Try to dispatch 75% READS and 25% WRITES */
1067
Tejun Heoc5cc2072013-05-14 13:52:38 -07001068 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001069 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001070
Tejun Heo77216b02013-05-14 13:52:36 -07001071 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001072 nr_reads++;
1073
1074 if (nr_reads >= max_nr_reads)
1075 break;
1076 }
1077
Tejun Heoc5cc2072013-05-14 13:52:38 -07001078 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001079 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001080
Tejun Heo77216b02013-05-14 13:52:36 -07001081 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001082 nr_writes++;
1083
1084 if (nr_writes >= max_nr_writes)
1085 break;
1086 }
1087
1088 return nr_reads + nr_writes;
1089}
1090
Tejun Heo651930b2013-05-14 13:52:35 -07001091static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001092{
1093 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001094
1095 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001096 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1097 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001098
1099 if (!tg)
1100 break;
1101
1102 if (time_before(jiffies, tg->disptime))
1103 break;
1104
Tejun Heo77216b02013-05-14 13:52:36 -07001105 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001106
Tejun Heo77216b02013-05-14 13:52:36 -07001107 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001108
Tejun Heo73f0d492013-05-14 13:52:35 -07001109 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001110 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001111
1112 if (nr_disp >= throtl_quantum)
1113 break;
1114 }
1115
1116 return nr_disp;
1117}
1118
Shaohua Lic79892c2017-03-27 10:51:34 -07001119static bool throtl_can_upgrade(struct throtl_data *td,
1120 struct throtl_grp *this_tg);
Tejun Heo6e1a5702013-05-14 13:52:37 -07001121/**
1122 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1123 * @arg: the throtl_service_queue being serviced
1124 *
1125 * This timer is armed when a child throtl_grp with active bio's become
1126 * pending and queued on the service_queue's pending_tree and expires when
1127 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001128 * dispatches bio's from the children throtl_grps to the parent
1129 * service_queue.
1130 *
1131 * If the parent's parent is another throtl_grp, dispatching is propagated
1132 * by either arming its pending_timer or repeating dispatch directly. If
1133 * the top-level service_tree is reached, throtl_data->dispatch_work is
1134 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001135 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001136static void throtl_pending_timer_fn(unsigned long arg)
1137{
1138 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001139 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001140 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001141 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001142 struct throtl_service_queue *parent_sq;
1143 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001144 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001145
1146 spin_lock_irq(q->queue_lock);
Shaohua Lic79892c2017-03-27 10:51:34 -07001147 if (throtl_can_upgrade(td, NULL))
1148 throtl_upgrade_state(td);
1149
Tejun Heo2e48a532013-05-14 13:52:38 -07001150again:
1151 parent_sq = sq->parent_sq;
1152 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001153
Tejun Heo7f52f982013-05-14 13:52:37 -07001154 while (true) {
1155 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001156 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1157 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001158
Tejun Heo7f52f982013-05-14 13:52:37 -07001159 ret = throtl_select_dispatch(sq);
1160 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001161 throtl_log(sq, "bios disp=%u", ret);
1162 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001163 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001164
Tejun Heo7f52f982013-05-14 13:52:37 -07001165 if (throtl_schedule_next_dispatch(sq, false))
1166 break;
1167
1168 /* this dispatch windows is still open, relax and repeat */
1169 spin_unlock_irq(q->queue_lock);
1170 cpu_relax();
1171 spin_lock_irq(q->queue_lock);
1172 }
Tejun Heo6a525602013-05-14 13:52:32 -07001173
Tejun Heo2e48a532013-05-14 13:52:38 -07001174 if (!dispatched)
1175 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001176
Tejun Heo2e48a532013-05-14 13:52:38 -07001177 if (parent_sq) {
1178 /* @parent_sq is another throl_grp, propagate dispatch */
1179 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1180 tg_update_disptime(tg);
1181 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1182 /* window is already open, repeat dispatching */
1183 sq = parent_sq;
1184 tg = sq_to_tg(sq);
1185 goto again;
1186 }
1187 }
1188 } else {
1189 /* reached the top-level, queue issueing */
1190 queue_work(kthrotld_workqueue, &td->dispatch_work);
1191 }
1192out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001193 spin_unlock_irq(q->queue_lock);
1194}
1195
1196/**
1197 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1198 * @work: work item being executed
1199 *
1200 * This function is queued for execution when bio's reach the bio_lists[]
1201 * of throtl_data->service_queue. Those bio's are ready and issued by this
1202 * function.
1203 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001204static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001205{
1206 struct throtl_data *td = container_of(work, struct throtl_data,
1207 dispatch_work);
1208 struct throtl_service_queue *td_sq = &td->service_queue;
1209 struct request_queue *q = td->queue;
1210 struct bio_list bio_list_on_stack;
1211 struct bio *bio;
1212 struct blk_plug plug;
1213 int rw;
1214
1215 bio_list_init(&bio_list_on_stack);
1216
1217 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001218 for (rw = READ; rw <= WRITE; rw++)
1219 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1220 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001221 spin_unlock_irq(q->queue_lock);
1222
Tejun Heo6e1a5702013-05-14 13:52:37 -07001223 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001224 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001225 while((bio = bio_list_pop(&bio_list_on_stack)))
1226 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001227 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001228 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001229}
1230
Tejun Heof95a04a2012-04-16 13:57:26 -07001231static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1232 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001233{
Tejun Heof95a04a2012-04-16 13:57:26 -07001234 struct throtl_grp *tg = pd_to_tg(pd);
1235 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001236
Shaohua Li2ab54922017-03-27 10:51:29 -07001237 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001238 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001239 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001240}
1241
Tejun Heof95a04a2012-04-16 13:57:26 -07001242static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1243 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001244{
Tejun Heof95a04a2012-04-16 13:57:26 -07001245 struct throtl_grp *tg = pd_to_tg(pd);
1246 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001247
Shaohua Li2ab54922017-03-27 10:51:29 -07001248 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001249 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001250 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001251}
1252
Tejun Heo2da8ca82013-12-05 12:28:04 -05001253static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001254{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001255 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1256 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001257 return 0;
1258}
1259
Tejun Heo2da8ca82013-12-05 12:28:04 -05001260static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001261{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001262 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1263 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001264 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001265}
1266
Tejun Heo69948b02015-08-18 14:55:32 -07001267static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001268{
Tejun Heo69948b02015-08-18 14:55:32 -07001269 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001270 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001271 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001272
Tejun Heofda6f272013-05-14 13:52:36 -07001273 throtl_log(&tg->service_queue,
1274 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001275 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1276 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001277
1278 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001279 * Update has_rules[] flags for the updated tg's subtree. A tg is
1280 * considered to have rules if either the tg itself or any of its
1281 * ancestors has rules. This identifies groups without any
1282 * restrictions in the whole hierarchy and allows them to bypass
1283 * blk-throttle.
1284 */
Tejun Heo69948b02015-08-18 14:55:32 -07001285 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001286 tg_update_has_rules(blkg_to_tg(blkg));
1287
1288 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001289 * We're already holding queue_lock and know @tg is valid. Let's
1290 * apply the new config directly.
1291 *
1292 * Restart the slices for both READ and WRITES. It might happen
1293 * that a group's limit are dropped suddenly and we don't want to
1294 * account recently dispatched IO with new low rate.
1295 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001296 throtl_start_new_slice(tg, 0);
1297 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001298
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001299 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001300 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001301 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001302 }
Tejun Heo69948b02015-08-18 14:55:32 -07001303}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001304
Tejun Heo69948b02015-08-18 14:55:32 -07001305static ssize_t tg_set_conf(struct kernfs_open_file *of,
1306 char *buf, size_t nbytes, loff_t off, bool is_u64)
1307{
1308 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1309 struct blkg_conf_ctx ctx;
1310 struct throtl_grp *tg;
1311 int ret;
1312 u64 v;
1313
1314 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1315 if (ret)
1316 return ret;
1317
1318 ret = -EINVAL;
1319 if (sscanf(ctx.body, "%llu", &v) != 1)
1320 goto out_finish;
1321 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001322 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001323
1324 tg = blkg_to_tg(ctx.blkg);
1325
1326 if (is_u64)
1327 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1328 else
1329 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1330
1331 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001332 ret = 0;
1333out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001334 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001335 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001336}
1337
Tejun Heo451af502014-05-13 12:16:21 -04001338static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1339 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001340{
Tejun Heo451af502014-05-13 12:16:21 -04001341 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001342}
1343
Tejun Heo451af502014-05-13 12:16:21 -04001344static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1345 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001346{
Tejun Heo451af502014-05-13 12:16:21 -04001347 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001348}
1349
Tejun Heo880f50e2015-08-18 14:55:30 -07001350static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001351 {
1352 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001353 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001354 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001355 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001356 },
1357 {
1358 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001359 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001360 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001361 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001362 },
1363 {
1364 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001365 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001366 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001367 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001368 },
1369 {
1370 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001371 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001372 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001373 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001374 },
1375 {
1376 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001377 .private = (unsigned long)&blkcg_policy_throtl,
1378 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001379 },
1380 {
1381 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001382 .private = (unsigned long)&blkcg_policy_throtl,
1383 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001384 },
1385 { } /* terminate */
1386};
1387
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001388static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001389 int off)
1390{
1391 struct throtl_grp *tg = pd_to_tg(pd);
1392 const char *dname = blkg_dev_name(pd->blkg);
1393 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001394 u64 bps_dft;
1395 unsigned int iops_dft;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001396
1397 if (!dname)
1398 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001399
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001400 if (off == LIMIT_LOW) {
1401 bps_dft = 0;
1402 iops_dft = 0;
1403 } else {
1404 bps_dft = U64_MAX;
1405 iops_dft = UINT_MAX;
1406 }
1407
1408 if (tg->bps_conf[READ][off] == bps_dft &&
1409 tg->bps_conf[WRITE][off] == bps_dft &&
1410 tg->iops_conf[READ][off] == iops_dft &&
1411 tg->iops_conf[WRITE][off] == iops_dft)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001412 return 0;
1413
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001414 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001415 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001416 tg->bps_conf[READ][off]);
1417 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001418 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001419 tg->bps_conf[WRITE][off]);
1420 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001421 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001422 tg->iops_conf[READ][off]);
1423 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001424 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001425 tg->iops_conf[WRITE][off]);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001426
1427 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s\n",
1428 dname, bufs[0], bufs[1], bufs[2], bufs[3]);
1429 return 0;
1430}
1431
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001432static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001433{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001434 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001435 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1436 return 0;
1437}
1438
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001439static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001440 char *buf, size_t nbytes, loff_t off)
1441{
1442 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1443 struct blkg_conf_ctx ctx;
1444 struct throtl_grp *tg;
1445 u64 v[4];
1446 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001447 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001448
1449 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1450 if (ret)
1451 return ret;
1452
1453 tg = blkg_to_tg(ctx.blkg);
1454
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001455 v[0] = tg->bps_conf[READ][index];
1456 v[1] = tg->bps_conf[WRITE][index];
1457 v[2] = tg->iops_conf[READ][index];
1458 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001459
1460 while (true) {
1461 char tok[27]; /* wiops=18446744073709551616 */
1462 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001463 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001464 int len;
1465
1466 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1467 break;
1468 if (tok[0] == '\0')
1469 break;
1470 ctx.body += len;
1471
1472 ret = -EINVAL;
1473 p = tok;
1474 strsep(&p, "=");
1475 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1476 goto out_finish;
1477
1478 ret = -ERANGE;
1479 if (!val)
1480 goto out_finish;
1481
1482 ret = -EINVAL;
1483 if (!strcmp(tok, "rbps"))
1484 v[0] = val;
1485 else if (!strcmp(tok, "wbps"))
1486 v[1] = val;
1487 else if (!strcmp(tok, "riops"))
1488 v[2] = min_t(u64, val, UINT_MAX);
1489 else if (!strcmp(tok, "wiops"))
1490 v[3] = min_t(u64, val, UINT_MAX);
1491 else
1492 goto out_finish;
1493 }
1494
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001495 tg->bps_conf[READ][index] = v[0];
1496 tg->bps_conf[WRITE][index] = v[1];
1497 tg->iops_conf[READ][index] = v[2];
1498 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001499
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001500 if (index == LIMIT_MAX) {
1501 tg->bps[READ][index] = v[0];
1502 tg->bps[WRITE][index] = v[1];
1503 tg->iops[READ][index] = v[2];
1504 tg->iops[WRITE][index] = v[3];
1505 }
1506 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1507 tg->bps_conf[READ][LIMIT_MAX]);
1508 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1509 tg->bps_conf[WRITE][LIMIT_MAX]);
1510 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1511 tg->iops_conf[READ][LIMIT_MAX]);
1512 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1513 tg->iops_conf[WRITE][LIMIT_MAX]);
1514
1515 if (index == LIMIT_LOW) {
1516 blk_throtl_update_limit_valid(tg->td);
1517 if (tg->td->limit_valid[LIMIT_LOW])
1518 tg->td->limit_index = LIMIT_LOW;
1519 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001520 tg_conf_updated(tg);
1521 ret = 0;
1522out_finish:
1523 blkg_conf_finish(&ctx);
1524 return ret ?: nbytes;
1525}
1526
1527static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001528#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1529 {
1530 .name = "low",
1531 .flags = CFTYPE_NOT_ON_ROOT,
1532 .seq_show = tg_print_limit,
1533 .write = tg_set_limit,
1534 .private = LIMIT_LOW,
1535 },
1536#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001537 {
1538 .name = "max",
1539 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001540 .seq_show = tg_print_limit,
1541 .write = tg_set_limit,
1542 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001543 },
1544 { } /* terminate */
1545};
1546
Vivek Goyalda527772011-03-02 19:05:33 -05001547static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001548{
1549 struct throtl_data *td = q->td;
1550
Tejun Heo69df0ab2013-05-14 13:52:36 -07001551 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001552}
1553
Tejun Heo3c798392012-04-16 13:57:25 -07001554static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001555 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001556 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001557
Tejun Heo001bea72015-08-18 14:55:11 -07001558 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001559 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001560 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001561 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001562 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001563};
1564
Shaohua Li3f0abd82017-03-27 10:51:35 -07001565static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1566{
1567 unsigned long rtime = jiffies, wtime = jiffies;
1568
1569 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1570 rtime = tg->last_low_overflow_time[READ];
1571 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1572 wtime = tg->last_low_overflow_time[WRITE];
1573 return min(rtime, wtime);
1574}
1575
1576/* tg should not be an intermediate node */
1577static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1578{
1579 struct throtl_service_queue *parent_sq;
1580 struct throtl_grp *parent = tg;
1581 unsigned long ret = __tg_last_low_overflow_time(tg);
1582
1583 while (true) {
1584 parent_sq = parent->service_queue.parent_sq;
1585 parent = sq_to_tg(parent_sq);
1586 if (!parent)
1587 break;
1588
1589 /*
1590 * The parent doesn't have low limit, it always reaches low
1591 * limit. Its overflow time is useless for children
1592 */
1593 if (!parent->bps[READ][LIMIT_LOW] &&
1594 !parent->iops[READ][LIMIT_LOW] &&
1595 !parent->bps[WRITE][LIMIT_LOW] &&
1596 !parent->iops[WRITE][LIMIT_LOW])
1597 continue;
1598 if (time_after(__tg_last_low_overflow_time(parent), ret))
1599 ret = __tg_last_low_overflow_time(parent);
1600 }
1601 return ret;
1602}
1603
Shaohua Lic79892c2017-03-27 10:51:34 -07001604static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1605{
1606 struct throtl_service_queue *sq = &tg->service_queue;
1607 bool read_limit, write_limit;
1608
1609 /*
1610 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1611 * reaches), it's ok to upgrade to next limit
1612 */
1613 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1614 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1615 if (!read_limit && !write_limit)
1616 return true;
1617 if (read_limit && sq->nr_queued[READ] &&
1618 (!write_limit || sq->nr_queued[WRITE]))
1619 return true;
1620 if (write_limit && sq->nr_queued[WRITE] &&
1621 (!read_limit || sq->nr_queued[READ]))
1622 return true;
Shaohua Liaec24242017-03-27 10:51:39 -07001623
1624 if (time_after_eq(jiffies,
1625 tg->last_dispatch_time[READ] + tg->td->throtl_slice) &&
1626 time_after_eq(jiffies,
1627 tg->last_dispatch_time[WRITE] + tg->td->throtl_slice))
1628 return true;
Shaohua Lic79892c2017-03-27 10:51:34 -07001629 return false;
1630}
1631
1632static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1633{
1634 while (true) {
1635 if (throtl_tg_can_upgrade(tg))
1636 return true;
1637 tg = sq_to_tg(tg->service_queue.parent_sq);
1638 if (!tg || !tg_to_blkg(tg)->parent)
1639 return false;
1640 }
1641 return false;
1642}
1643
1644static bool throtl_can_upgrade(struct throtl_data *td,
1645 struct throtl_grp *this_tg)
1646{
1647 struct cgroup_subsys_state *pos_css;
1648 struct blkcg_gq *blkg;
1649
1650 if (td->limit_index != LIMIT_LOW)
1651 return false;
1652
Shaohua Li297e3d82017-03-27 10:51:37 -07001653 if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001654 return false;
1655
Shaohua Lic79892c2017-03-27 10:51:34 -07001656 rcu_read_lock();
1657 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1658 struct throtl_grp *tg = blkg_to_tg(blkg);
1659
1660 if (tg == this_tg)
1661 continue;
1662 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1663 continue;
1664 if (!throtl_hierarchy_can_upgrade(tg)) {
1665 rcu_read_unlock();
1666 return false;
1667 }
1668 }
1669 rcu_read_unlock();
1670 return true;
1671}
1672
1673static void throtl_upgrade_state(struct throtl_data *td)
1674{
1675 struct cgroup_subsys_state *pos_css;
1676 struct blkcg_gq *blkg;
1677
1678 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001679 td->low_upgrade_time = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001680 rcu_read_lock();
1681 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1682 struct throtl_grp *tg = blkg_to_tg(blkg);
1683 struct throtl_service_queue *sq = &tg->service_queue;
1684
1685 tg->disptime = jiffies - 1;
1686 throtl_select_dispatch(sq);
1687 throtl_schedule_next_dispatch(sq, false);
1688 }
1689 rcu_read_unlock();
1690 throtl_select_dispatch(&td->service_queue);
1691 throtl_schedule_next_dispatch(&td->service_queue, false);
1692 queue_work(kthrotld_workqueue, &td->dispatch_work);
1693}
1694
Shaohua Li3f0abd82017-03-27 10:51:35 -07001695static void throtl_downgrade_state(struct throtl_data *td, int new)
1696{
1697 td->limit_index = new;
1698 td->low_downgrade_time = jiffies;
1699}
1700
1701static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1702{
1703 struct throtl_data *td = tg->td;
1704 unsigned long now = jiffies;
1705
Shaohua Liaec24242017-03-27 10:51:39 -07001706 if (time_after_eq(now, tg->last_dispatch_time[READ] +
1707 td->throtl_slice) &&
1708 time_after_eq(now, tg->last_dispatch_time[WRITE] +
1709 td->throtl_slice))
1710 return false;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001711 /*
1712 * If cgroup is below low limit, consider downgrade and throttle other
1713 * cgroups
1714 */
Shaohua Li297e3d82017-03-27 10:51:37 -07001715 if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) &&
1716 time_after_eq(now, tg_last_low_overflow_time(tg) +
1717 td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001718 return true;
1719 return false;
1720}
1721
1722static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1723{
1724 while (true) {
1725 if (!throtl_tg_can_downgrade(tg))
1726 return false;
1727 tg = sq_to_tg(tg->service_queue.parent_sq);
1728 if (!tg || !tg_to_blkg(tg)->parent)
1729 break;
1730 }
1731 return true;
1732}
1733
1734static void throtl_downgrade_check(struct throtl_grp *tg)
1735{
1736 uint64_t bps;
1737 unsigned int iops;
1738 unsigned long elapsed_time;
1739 unsigned long now = jiffies;
1740
1741 if (tg->td->limit_index != LIMIT_MAX ||
1742 !tg->td->limit_valid[LIMIT_LOW])
1743 return;
1744 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1745 return;
Shaohua Li297e3d82017-03-27 10:51:37 -07001746 if (time_after(tg->last_check_time + tg->td->throtl_slice, now))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001747 return;
1748
1749 elapsed_time = now - tg->last_check_time;
1750 tg->last_check_time = now;
1751
Shaohua Li297e3d82017-03-27 10:51:37 -07001752 if (time_before(now, tg_last_low_overflow_time(tg) +
1753 tg->td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001754 return;
1755
1756 if (tg->bps[READ][LIMIT_LOW]) {
1757 bps = tg->last_bytes_disp[READ] * HZ;
1758 do_div(bps, elapsed_time);
1759 if (bps >= tg->bps[READ][LIMIT_LOW])
1760 tg->last_low_overflow_time[READ] = now;
1761 }
1762
1763 if (tg->bps[WRITE][LIMIT_LOW]) {
1764 bps = tg->last_bytes_disp[WRITE] * HZ;
1765 do_div(bps, elapsed_time);
1766 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1767 tg->last_low_overflow_time[WRITE] = now;
1768 }
1769
1770 if (tg->iops[READ][LIMIT_LOW]) {
1771 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1772 if (iops >= tg->iops[READ][LIMIT_LOW])
1773 tg->last_low_overflow_time[READ] = now;
1774 }
1775
1776 if (tg->iops[WRITE][LIMIT_LOW]) {
1777 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1778 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1779 tg->last_low_overflow_time[WRITE] = now;
1780 }
1781
1782 /*
1783 * If cgroup is below low limit, consider downgrade and throttle other
1784 * cgroups
1785 */
1786 if (throtl_hierarchy_can_downgrade(tg))
1787 throtl_downgrade_state(tg->td, LIMIT_LOW);
1788
1789 tg->last_bytes_disp[READ] = 0;
1790 tg->last_bytes_disp[WRITE] = 0;
1791 tg->last_io_disp[READ] = 0;
1792 tg->last_io_disp[WRITE] = 0;
1793}
1794
Tejun Heoae118892015-08-18 14:55:20 -07001795bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
1796 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04001797{
Tejun Heoc5cc2072013-05-14 13:52:38 -07001798 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07001799 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07001800 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07001801 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001802 bool throttled = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001803
Tejun Heoae118892015-08-18 14:55:20 -07001804 WARN_ON_ONCE(!rcu_read_lock_held());
1805
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001806 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001807 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02001808 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04001809
1810 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07001811
1812 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02001813 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04001814
Tejun Heo73f0d492013-05-14 13:52:35 -07001815 sq = &tg->service_queue;
1816
Shaohua Lic79892c2017-03-27 10:51:34 -07001817again:
Tejun Heo9e660ac2013-05-14 13:52:38 -07001818 while (true) {
Shaohua Liaec24242017-03-27 10:51:39 -07001819 tg->last_dispatch_time[rw] = jiffies;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001820 if (tg->last_low_overflow_time[rw] == 0)
1821 tg->last_low_overflow_time[rw] = jiffies;
1822 throtl_downgrade_check(tg);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001823 /* throtl is FIFO - if bios are already queued, should queue */
1824 if (sq->nr_queued[rw])
1825 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01001826
Tejun Heo9e660ac2013-05-14 13:52:38 -07001827 /* if above limits, break to queue */
Shaohua Lic79892c2017-03-27 10:51:34 -07001828 if (!tg_may_dispatch(tg, bio, NULL)) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07001829 tg->last_low_overflow_time[rw] = jiffies;
Shaohua Lic79892c2017-03-27 10:51:34 -07001830 if (throtl_can_upgrade(tg->td, tg)) {
1831 throtl_upgrade_state(tg->td);
1832 goto again;
1833 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001834 break;
Shaohua Lic79892c2017-03-27 10:51:34 -07001835 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07001836
1837 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04001838 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01001839
1840 /*
1841 * We need to trim slice even when bios are not being queued
1842 * otherwise it might happen that a bio is not queued for
1843 * a long time and slice keeps on extending and trim is not
1844 * called for a long time. Now if limits are reduced suddenly
1845 * we take into account all the IO dispatched so far at new
1846 * low rate and * newly queued IO gets a really long dispatch
1847 * time.
1848 *
1849 * So keep on trimming slice even if bio is not queued.
1850 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001851 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07001852
1853 /*
1854 * @bio passed through this layer without being throttled.
1855 * Climb up the ladder. If we''re already at the top, it
1856 * can be executed directly.
1857 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07001858 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07001859 sq = sq->parent_sq;
1860 tg = sq_to_tg(sq);
1861 if (!tg)
1862 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04001863 }
1864
Tejun Heo9e660ac2013-05-14 13:52:38 -07001865 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07001866 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
1867 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07001868 tg->bytes_disp[rw], bio->bi_iter.bi_size,
1869 tg_bps_limit(tg, rw),
1870 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07001871 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001872
Shaohua Li3f0abd82017-03-27 10:51:35 -07001873 tg->last_low_overflow_time[rw] = jiffies;
1874
Tejun Heo671058f2012-03-05 13:15:29 -08001875 bio_associate_current(bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001876 tg->td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07001877 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001878 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04001879
Tejun Heo7f52f982013-05-14 13:52:37 -07001880 /*
1881 * Update @tg's dispatch time and force schedule dispatch if @tg
1882 * was empty before @bio. The forced scheduling isn't likely to
1883 * cause undue delay as @bio is likely to be dispatched directly if
1884 * its @tg's disptime is not in the future.
1885 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07001886 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07001887 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001888 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04001889 }
1890
Tejun Heobc16a4f2011-10-19 14:33:01 +02001891out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04001892 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001893out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001894 /*
1895 * As multiple blk-throtls may stack in the same issue path, we
1896 * don't want bios to leave with the flag set. Clear the flag if
1897 * being issued.
1898 */
1899 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001900 bio_clear_flag(bio, BIO_THROTTLED);
Tejun Heobc16a4f2011-10-19 14:33:01 +02001901 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04001902}
1903
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001904/*
1905 * Dispatch all bios from all children tg's queued on @parent_sq. On
1906 * return, @parent_sq is guaranteed to not have any active children tg's
1907 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
1908 */
1909static void tg_drain_bios(struct throtl_service_queue *parent_sq)
1910{
1911 struct throtl_grp *tg;
1912
1913 while ((tg = throtl_rb_first(parent_sq))) {
1914 struct throtl_service_queue *sq = &tg->service_queue;
1915 struct bio *bio;
1916
1917 throtl_dequeue_tg(tg);
1918
Tejun Heoc5cc2072013-05-14 13:52:38 -07001919 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001920 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07001921 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001922 tg_dispatch_one_bio(tg, bio_data_dir(bio));
1923 }
1924}
1925
Tejun Heoc9a929d2011-10-19 14:42:16 +02001926/**
1927 * blk_throtl_drain - drain throttled bios
1928 * @q: request_queue to drain throttled bios for
1929 *
1930 * Dispatch all currently throttled bios on @q through ->make_request_fn().
1931 */
1932void blk_throtl_drain(struct request_queue *q)
1933 __releases(q->queue_lock) __acquires(q->queue_lock)
1934{
1935 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001936 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04001937 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001938 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07001939 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02001940
Andi Kleen8bcb6c72012-03-30 12:33:28 +02001941 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001942 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001943
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001944 /*
1945 * Drain each tg while doing post-order walk on the blkg tree, so
1946 * that all bios are propagated to td->service_queue. It'd be
1947 * better to walk service_queue tree directly but blkg walk is
1948 * easier.
1949 */
Tejun Heo492eb212013-08-08 20:11:25 -04001950 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001951 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07001952
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001953 /* finally, transfer bios from top-level tg's into the td */
1954 tg_drain_bios(&td->service_queue);
1955
1956 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02001957 spin_unlock_irq(q->queue_lock);
1958
Tejun Heo2a12f0d2013-05-14 13:52:37 -07001959 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07001960 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07001961 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1962 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07001963 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001964
1965 spin_lock_irq(q->queue_lock);
1966}
1967
Vivek Goyale43473b2010-09-15 17:06:35 -04001968int blk_throtl_init(struct request_queue *q)
1969{
1970 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07001971 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001972
1973 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1974 if (!td)
1975 return -ENOMEM;
1976
Tejun Heo69df0ab2013-05-14 13:52:36 -07001977 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07001978 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04001979
Tejun Heocd1604f2012-03-05 13:15:06 -08001980 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04001981 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02001982
Shaohua Li9f626e32017-03-27 10:51:30 -07001983 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001984 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001985 td->low_upgrade_time = jiffies;
1986 td->low_downgrade_time = jiffies;
Tejun Heoa2b16932012-04-13 13:11:33 -07001987 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07001988 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Tejun Heoa2b16932012-04-13 13:11:33 -07001989 if (ret)
Vivek Goyal29b12582011-05-19 15:38:24 -04001990 kfree(td);
Tejun Heoa2b16932012-04-13 13:11:33 -07001991 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001992}
1993
1994void blk_throtl_exit(struct request_queue *q)
1995{
Tejun Heoc875f4d2012-03-05 13:15:22 -08001996 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05001997 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07001998 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Tejun Heoc9a929d2011-10-19 14:42:16 +02001999 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04002000}
2001
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002002void blk_throtl_register_queue(struct request_queue *q)
2003{
2004 struct throtl_data *td;
2005
2006 td = q->td;
2007 BUG_ON(!td);
2008
2009 if (blk_queue_nonrot(q))
2010 td->throtl_slice = DFL_THROTL_SLICE_SSD;
2011 else
2012 td->throtl_slice = DFL_THROTL_SLICE_HD;
2013#ifndef CONFIG_BLK_DEV_THROTTLING_LOW
2014 /* if no low limit, use previous default */
2015 td->throtl_slice = DFL_THROTL_SLICE_HD;
2016#endif
2017}
2018
Shaohua Li297e3d82017-03-27 10:51:37 -07002019#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2020ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page)
2021{
2022 if (!q->td)
2023 return -EINVAL;
2024 return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice));
2025}
2026
2027ssize_t blk_throtl_sample_time_store(struct request_queue *q,
2028 const char *page, size_t count)
2029{
2030 unsigned long v;
2031 unsigned long t;
2032
2033 if (!q->td)
2034 return -EINVAL;
2035 if (kstrtoul(page, 10, &v))
2036 return -EINVAL;
2037 t = msecs_to_jiffies(v);
2038 if (t == 0 || t > MAX_THROTL_SLICE)
2039 return -EINVAL;
2040 q->td->throtl_slice = t;
2041 return count;
2042}
2043#endif
2044
Vivek Goyale43473b2010-09-15 17:06:35 -04002045static int __init throtl_init(void)
2046{
Vivek Goyal450adcb2011-03-01 13:40:54 -05002047 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
2048 if (!kthrotld_workqueue)
2049 panic("Failed to create kthrotld\n");
2050
Tejun Heo3c798392012-04-16 13:57:25 -07002051 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04002052}
2053
2054module_init(throtl_init);