blob: c82bf9b1fe7224c049aea2248726ee8576d898c6 [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)
Shaohua Li9e234ee2017-03-27 10:51:41 -070025#define DFL_IDLE_THRESHOLD_SSD (1000L) /* 1 ms */
26#define DFL_IDLE_THRESHOLD_HD (100L * 1000) /* 100 ms */
27#define MAX_IDLE_TIME (5L * 1000 * 1000) /* 5 s */
Shaohua Liec809912017-03-27 10:51:44 -070028/* default latency target is 0, eg, guarantee IO latency by default */
29#define DFL_LATENCY_TARGET (0)
Vivek Goyale43473b2010-09-15 17:06:35 -040030
Shaohua Lib9147dd2017-03-27 15:19:42 -070031#define SKIP_LATENCY (((u64)1) << BLK_STAT_RES_SHIFT)
32
Tejun Heo3c798392012-04-16 13:57:25 -070033static struct blkcg_policy blkcg_policy_throtl;
Tejun Heo03814112012-03-05 13:15:14 -080034
Vivek Goyal450adcb2011-03-01 13:40:54 -050035/* A workqueue to queue throttle related work */
36static struct workqueue_struct *kthrotld_workqueue;
Vivek Goyal450adcb2011-03-01 13:40:54 -050037
Tejun Heoc5cc2072013-05-14 13:52:38 -070038/*
39 * To implement hierarchical throttling, throtl_grps form a tree and bios
40 * are dispatched upwards level by level until they reach the top and get
41 * issued. When dispatching bios from the children and local group at each
42 * level, if the bios are dispatched into a single bio_list, there's a risk
43 * of a local or child group which can queue many bios at once filling up
44 * the list starving others.
45 *
46 * To avoid such starvation, dispatched bios are queued separately
47 * according to where they came from. When they are again dispatched to
48 * the parent, they're popped in round-robin order so that no single source
49 * hogs the dispatch window.
50 *
51 * throtl_qnode is used to keep the queued bios separated by their sources.
52 * Bios are queued to throtl_qnode which in turn is queued to
53 * throtl_service_queue and then dispatched in round-robin order.
54 *
55 * It's also used to track the reference counts on blkg's. A qnode always
56 * belongs to a throtl_grp and gets queued on itself or the parent, so
57 * incrementing the reference of the associated throtl_grp when a qnode is
58 * queued and decrementing when dequeued is enough to keep the whole blkg
59 * tree pinned while bios are in flight.
60 */
61struct throtl_qnode {
62 struct list_head node; /* service_queue->queued[] */
63 struct bio_list bios; /* queued bios */
64 struct throtl_grp *tg; /* tg this qnode belongs to */
65};
66
Tejun Heoc9e03322013-05-14 13:52:32 -070067struct throtl_service_queue {
Tejun Heo77216b02013-05-14 13:52:36 -070068 struct throtl_service_queue *parent_sq; /* the parent service_queue */
69
Tejun Heo73f0d492013-05-14 13:52:35 -070070 /*
71 * Bios queued directly to this service_queue or dispatched from
72 * children throtl_grp's.
73 */
Tejun Heoc5cc2072013-05-14 13:52:38 -070074 struct list_head queued[2]; /* throtl_qnode [READ/WRITE] */
Tejun Heo73f0d492013-05-14 13:52:35 -070075 unsigned int nr_queued[2]; /* number of queued bios */
76
77 /*
78 * RB tree of active children throtl_grp's, which are sorted by
79 * their ->disptime.
80 */
Tejun Heoc9e03322013-05-14 13:52:32 -070081 struct rb_root pending_tree; /* RB tree of active tgs */
82 struct rb_node *first_pending; /* first node in the tree */
83 unsigned int nr_pending; /* # queued in the tree */
84 unsigned long first_pending_disptime; /* disptime of the first tg */
Tejun Heo69df0ab2013-05-14 13:52:36 -070085 struct timer_list pending_timer; /* fires on first_pending_disptime */
Vivek Goyale43473b2010-09-15 17:06:35 -040086};
87
Tejun Heo5b2c16a2013-05-14 13:52:32 -070088enum tg_state_flags {
89 THROTL_TG_PENDING = 1 << 0, /* on parent's pending tree */
Tejun Heo0e9f4162013-05-14 13:52:35 -070090 THROTL_TG_WAS_EMPTY = 1 << 1, /* bio_lists[] became non-empty */
Tejun Heo5b2c16a2013-05-14 13:52:32 -070091};
92
Vivek Goyale43473b2010-09-15 17:06:35 -040093#define rb_entry_tg(node) rb_entry((node), struct throtl_grp, rb_node)
94
Shaohua Li9f626e32017-03-27 10:51:30 -070095enum {
Shaohua Licd5ab1b2017-03-27 10:51:32 -070096 LIMIT_LOW,
Shaohua Li9f626e32017-03-27 10:51:30 -070097 LIMIT_MAX,
98 LIMIT_CNT,
99};
100
Vivek Goyale43473b2010-09-15 17:06:35 -0400101struct throtl_grp {
Tejun Heof95a04a2012-04-16 13:57:26 -0700102 /* must be the first member */
103 struct blkg_policy_data pd;
104
Tejun Heoc9e03322013-05-14 13:52:32 -0700105 /* active throtl group service_queue member */
Vivek Goyale43473b2010-09-15 17:06:35 -0400106 struct rb_node rb_node;
107
Tejun Heo0f3457f2013-05-14 13:52:32 -0700108 /* throtl_data this group belongs to */
109 struct throtl_data *td;
110
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700111 /* this group's service queue */
112 struct throtl_service_queue service_queue;
113
Vivek Goyale43473b2010-09-15 17:06:35 -0400114 /*
Tejun Heoc5cc2072013-05-14 13:52:38 -0700115 * qnode_on_self is used when bios are directly queued to this
116 * throtl_grp so that local bios compete fairly with bios
117 * dispatched from children. qnode_on_parent is used when bios are
118 * dispatched from this throtl_grp into its parent and will compete
119 * with the sibling qnode_on_parents and the parent's
120 * qnode_on_self.
121 */
122 struct throtl_qnode qnode_on_self[2];
123 struct throtl_qnode qnode_on_parent[2];
124
125 /*
Vivek Goyale43473b2010-09-15 17:06:35 -0400126 * Dispatch time in jiffies. This is the estimated time when group
127 * will unthrottle and is ready to dispatch more bio. It is used as
128 * key to sort active groups in service tree.
129 */
130 unsigned long disptime;
131
Vivek Goyale43473b2010-09-15 17:06:35 -0400132 unsigned int flags;
133
Tejun Heo693e7512013-05-14 13:52:38 -0700134 /* are there any throtl rules between this group and td? */
135 bool has_rules[2];
136
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700137 /* internally used bytes per second rate limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700138 uint64_t bps[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700139 /* user configured bps limits */
140 uint64_t bps_conf[2][LIMIT_CNT];
Vivek Goyale43473b2010-09-15 17:06:35 -0400141
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700142 /* internally used IOPS limits */
Shaohua Li9f626e32017-03-27 10:51:30 -0700143 unsigned int iops[2][LIMIT_CNT];
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700144 /* user configured IOPS limits */
145 unsigned int iops_conf[2][LIMIT_CNT];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400146
Vivek Goyale43473b2010-09-15 17:06:35 -0400147 /* Number of bytes disptached in current slice */
148 uint64_t bytes_disp[2];
Vivek Goyal8e89d132010-09-15 17:06:37 -0400149 /* Number of bio's dispatched in current slice */
150 unsigned int io_disp[2];
Vivek Goyale43473b2010-09-15 17:06:35 -0400151
Shaohua Li3f0abd82017-03-27 10:51:35 -0700152 unsigned long last_low_overflow_time[2];
153
154 uint64_t last_bytes_disp[2];
155 unsigned int last_io_disp[2];
156
157 unsigned long last_check_time;
158
Shaohua Liec809912017-03-27 10:51:44 -0700159 unsigned long latency_target; /* us */
Vivek Goyale43473b2010-09-15 17:06:35 -0400160 /* When did we start a new slice */
161 unsigned long slice_start[2];
162 unsigned long slice_end[2];
Shaohua Li9e234ee2017-03-27 10:51:41 -0700163
164 unsigned long last_finish_time; /* ns / 1024 */
165 unsigned long checked_last_finish_time; /* ns / 1024 */
166 unsigned long avg_idletime; /* ns / 1024 */
167 unsigned long idletime_threshold; /* us */
Shaohua Li53696b82017-03-27 15:19:43 -0700168
169 unsigned int bio_cnt; /* total bios */
170 unsigned int bad_bio_cnt; /* bios exceeding latency threshold */
171 unsigned long bio_cnt_reset_time;
Vivek Goyale43473b2010-09-15 17:06:35 -0400172};
173
Shaohua Lib9147dd2017-03-27 15:19:42 -0700174/* We measure latency for request size from <= 4k to >= 1M */
175#define LATENCY_BUCKET_SIZE 9
176
177struct latency_bucket {
178 unsigned long total_latency; /* ns / 1024 */
179 int samples;
180};
181
182struct avg_latency_bucket {
183 unsigned long latency; /* ns / 1024 */
184 bool valid;
185};
186
Vivek Goyale43473b2010-09-15 17:06:35 -0400187struct throtl_data
188{
Vivek Goyale43473b2010-09-15 17:06:35 -0400189 /* service tree for active throtl groups */
Tejun Heoc9e03322013-05-14 13:52:32 -0700190 struct throtl_service_queue service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -0400191
Vivek Goyale43473b2010-09-15 17:06:35 -0400192 struct request_queue *queue;
193
194 /* Total Number of queued bios on READ and WRITE lists */
195 unsigned int nr_queued[2];
196
Shaohua Li297e3d82017-03-27 10:51:37 -0700197 unsigned int throtl_slice;
198
Vivek Goyale43473b2010-09-15 17:06:35 -0400199 /* Work for dispatching throttled bios */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700200 struct work_struct dispatch_work;
Shaohua Li9f626e32017-03-27 10:51:30 -0700201 unsigned int limit_index;
202 bool limit_valid[LIMIT_CNT];
Shaohua Li3f0abd82017-03-27 10:51:35 -0700203
Shaohua Liada75b62017-03-27 10:51:42 -0700204 unsigned long dft_idletime_threshold; /* us */
205
Shaohua Li3f0abd82017-03-27 10:51:35 -0700206 unsigned long low_upgrade_time;
207 unsigned long low_downgrade_time;
Shaohua Li7394e312017-03-27 10:51:40 -0700208
209 unsigned int scale;
Shaohua Lib9147dd2017-03-27 15:19:42 -0700210
211 struct latency_bucket tmp_buckets[LATENCY_BUCKET_SIZE];
212 struct avg_latency_bucket avg_buckets[LATENCY_BUCKET_SIZE];
213 struct latency_bucket __percpu *latency_buckets;
214 unsigned long last_calculate_time;
215
216 bool track_bio_latency;
Vivek Goyale43473b2010-09-15 17:06:35 -0400217};
218
Tejun Heo69df0ab2013-05-14 13:52:36 -0700219static void throtl_pending_timer_fn(unsigned long arg);
220
Tejun Heof95a04a2012-04-16 13:57:26 -0700221static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
222{
223 return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
224}
225
Tejun Heo3c798392012-04-16 13:57:25 -0700226static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
Tejun Heo03814112012-03-05 13:15:14 -0800227{
Tejun Heof95a04a2012-04-16 13:57:26 -0700228 return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
Tejun Heo03814112012-03-05 13:15:14 -0800229}
230
Tejun Heo3c798392012-04-16 13:57:25 -0700231static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
Tejun Heo03814112012-03-05 13:15:14 -0800232{
Tejun Heof95a04a2012-04-16 13:57:26 -0700233 return pd_to_blkg(&tg->pd);
Tejun Heo03814112012-03-05 13:15:14 -0800234}
235
Tejun Heofda6f272013-05-14 13:52:36 -0700236/**
237 * sq_to_tg - return the throl_grp the specified service queue belongs to
238 * @sq: the throtl_service_queue of interest
239 *
240 * Return the throtl_grp @sq belongs to. If @sq is the top-level one
241 * embedded in throtl_data, %NULL is returned.
242 */
243static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
244{
245 if (sq && sq->parent_sq)
246 return container_of(sq, struct throtl_grp, service_queue);
247 else
248 return NULL;
249}
Vivek Goyale43473b2010-09-15 17:06:35 -0400250
Tejun Heofda6f272013-05-14 13:52:36 -0700251/**
252 * sq_to_td - return throtl_data the specified service queue belongs to
253 * @sq: the throtl_service_queue of interest
254 *
Masahiro Yamadab43daed2017-02-27 14:29:09 -0800255 * A service_queue can be embedded in either a throtl_grp or throtl_data.
Tejun Heofda6f272013-05-14 13:52:36 -0700256 * Determine the associated throtl_data accordingly and return it.
257 */
258static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
259{
260 struct throtl_grp *tg = sq_to_tg(sq);
261
262 if (tg)
263 return tg->td;
264 else
265 return container_of(sq, struct throtl_data, service_queue);
266}
267
Shaohua Li7394e312017-03-27 10:51:40 -0700268/*
269 * cgroup's limit in LIMIT_MAX is scaled if low limit is set. This scale is to
270 * make the IO dispatch more smooth.
271 * Scale up: linearly scale up according to lapsed time since upgrade. For
272 * every throtl_slice, the limit scales up 1/2 .low limit till the
273 * limit hits .max limit
274 * Scale down: exponentially scale down if a cgroup doesn't hit its .low limit
275 */
276static uint64_t throtl_adjusted_limit(uint64_t low, struct throtl_data *td)
277{
278 /* arbitrary value to avoid too big scale */
279 if (td->scale < 4096 && time_after_eq(jiffies,
280 td->low_upgrade_time + td->scale * td->throtl_slice))
281 td->scale = (jiffies - td->low_upgrade_time) / td->throtl_slice;
282
283 return low + (low >> 1) * td->scale;
284}
285
Shaohua Li9f626e32017-03-27 10:51:30 -0700286static uint64_t tg_bps_limit(struct throtl_grp *tg, int rw)
287{
Shaohua Lib22c4172017-03-27 10:51:33 -0700288 struct blkcg_gq *blkg = tg_to_blkg(tg);
Shaohua Li7394e312017-03-27 10:51:40 -0700289 struct throtl_data *td;
Shaohua Lib22c4172017-03-27 10:51:33 -0700290 uint64_t ret;
291
292 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
293 return U64_MAX;
Shaohua Li7394e312017-03-27 10:51:40 -0700294
295 td = tg->td;
296 ret = tg->bps[rw][td->limit_index];
297 if (ret == 0 && td->limit_index == LIMIT_LOW)
Shaohua Lib22c4172017-03-27 10:51:33 -0700298 return tg->bps[rw][LIMIT_MAX];
Shaohua Li7394e312017-03-27 10:51:40 -0700299
300 if (td->limit_index == LIMIT_MAX && tg->bps[rw][LIMIT_LOW] &&
301 tg->bps[rw][LIMIT_LOW] != tg->bps[rw][LIMIT_MAX]) {
302 uint64_t adjusted;
303
304 adjusted = throtl_adjusted_limit(tg->bps[rw][LIMIT_LOW], td);
305 ret = min(tg->bps[rw][LIMIT_MAX], adjusted);
306 }
Shaohua Lib22c4172017-03-27 10:51:33 -0700307 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700308}
309
310static unsigned int tg_iops_limit(struct throtl_grp *tg, int rw)
311{
Shaohua Lib22c4172017-03-27 10:51:33 -0700312 struct blkcg_gq *blkg = tg_to_blkg(tg);
Shaohua Li7394e312017-03-27 10:51:40 -0700313 struct throtl_data *td;
Shaohua Lib22c4172017-03-27 10:51:33 -0700314 unsigned int ret;
315
316 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && !blkg->parent)
317 return UINT_MAX;
Shaohua Li7394e312017-03-27 10:51:40 -0700318 td = tg->td;
319 ret = tg->iops[rw][td->limit_index];
Shaohua Lib22c4172017-03-27 10:51:33 -0700320 if (ret == 0 && tg->td->limit_index == LIMIT_LOW)
321 return tg->iops[rw][LIMIT_MAX];
Shaohua Li7394e312017-03-27 10:51:40 -0700322
323 if (td->limit_index == LIMIT_MAX && tg->iops[rw][LIMIT_LOW] &&
324 tg->iops[rw][LIMIT_LOW] != tg->iops[rw][LIMIT_MAX]) {
325 uint64_t adjusted;
326
327 adjusted = throtl_adjusted_limit(tg->iops[rw][LIMIT_LOW], td);
328 if (adjusted > UINT_MAX)
329 adjusted = UINT_MAX;
330 ret = min_t(unsigned int, tg->iops[rw][LIMIT_MAX], adjusted);
331 }
Shaohua Lib22c4172017-03-27 10:51:33 -0700332 return ret;
Shaohua Li9f626e32017-03-27 10:51:30 -0700333}
334
Shaohua Lib9147dd2017-03-27 15:19:42 -0700335#define request_bucket_index(sectors) \
336 clamp_t(int, order_base_2(sectors) - 3, 0, LATENCY_BUCKET_SIZE - 1)
337
Tejun Heofda6f272013-05-14 13:52:36 -0700338/**
339 * throtl_log - log debug message via blktrace
340 * @sq: the service_queue being reported
341 * @fmt: printf format string
342 * @args: printf args
343 *
344 * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
345 * throtl_grp; otherwise, just "throtl".
Tejun Heofda6f272013-05-14 13:52:36 -0700346 */
347#define throtl_log(sq, fmt, args...) do { \
348 struct throtl_grp *__tg = sq_to_tg((sq)); \
349 struct throtl_data *__td = sq_to_td((sq)); \
350 \
351 (void)__td; \
Shaohua Li59fa0222016-05-09 17:22:15 -0700352 if (likely(!blk_trace_note_message_enabled(__td->queue))) \
353 break; \
Tejun Heofda6f272013-05-14 13:52:36 -0700354 if ((__tg)) { \
355 char __pbuf[128]; \
356 \
357 blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf)); \
358 blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
359 } else { \
360 blk_add_trace_msg(__td->queue, "throtl " fmt, ##args); \
361 } \
362} while (0)
Vivek Goyale43473b2010-09-15 17:06:35 -0400363
Tejun Heoc5cc2072013-05-14 13:52:38 -0700364static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
365{
366 INIT_LIST_HEAD(&qn->node);
367 bio_list_init(&qn->bios);
368 qn->tg = tg;
369}
370
371/**
372 * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
373 * @bio: bio being added
374 * @qn: qnode to add bio to
375 * @queued: the service_queue->queued[] list @qn belongs to
376 *
377 * Add @bio to @qn and put @qn on @queued if it's not already on.
378 * @qn->tg's reference count is bumped when @qn is activated. See the
379 * comment on top of throtl_qnode definition for details.
380 */
381static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
382 struct list_head *queued)
383{
384 bio_list_add(&qn->bios, bio);
385 if (list_empty(&qn->node)) {
386 list_add_tail(&qn->node, queued);
387 blkg_get(tg_to_blkg(qn->tg));
388 }
389}
390
391/**
392 * throtl_peek_queued - peek the first bio on a qnode list
393 * @queued: the qnode list to peek
394 */
395static struct bio *throtl_peek_queued(struct list_head *queued)
396{
397 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
398 struct bio *bio;
399
400 if (list_empty(queued))
401 return NULL;
402
403 bio = bio_list_peek(&qn->bios);
404 WARN_ON_ONCE(!bio);
405 return bio;
406}
407
408/**
409 * throtl_pop_queued - pop the first bio form a qnode list
410 * @queued: the qnode list to pop a bio from
411 * @tg_to_put: optional out argument for throtl_grp to put
412 *
413 * Pop the first bio from the qnode list @queued. After popping, the first
414 * qnode is removed from @queued if empty or moved to the end of @queued so
415 * that the popping order is round-robin.
416 *
417 * When the first qnode is removed, its associated throtl_grp should be put
418 * too. If @tg_to_put is NULL, this function automatically puts it;
419 * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
420 * responsible for putting it.
421 */
422static struct bio *throtl_pop_queued(struct list_head *queued,
423 struct throtl_grp **tg_to_put)
424{
425 struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
426 struct bio *bio;
427
428 if (list_empty(queued))
429 return NULL;
430
431 bio = bio_list_pop(&qn->bios);
432 WARN_ON_ONCE(!bio);
433
434 if (bio_list_empty(&qn->bios)) {
435 list_del_init(&qn->node);
436 if (tg_to_put)
437 *tg_to_put = qn->tg;
438 else
439 blkg_put(tg_to_blkg(qn->tg));
440 } else {
441 list_move_tail(&qn->node, queued);
442 }
443
444 return bio;
445}
446
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700447/* init a service_queue, assumes the caller zeroed it */
Tejun Heob2ce2642015-08-18 14:55:13 -0700448static void throtl_service_queue_init(struct throtl_service_queue *sq)
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700449{
Tejun Heoc5cc2072013-05-14 13:52:38 -0700450 INIT_LIST_HEAD(&sq->queued[0]);
451 INIT_LIST_HEAD(&sq->queued[1]);
Tejun Heo49a2f1e2013-05-14 13:52:34 -0700452 sq->pending_tree = RB_ROOT;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700453 setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
454 (unsigned long)sq);
455}
456
Tejun Heo001bea72015-08-18 14:55:11 -0700457static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
458{
Tejun Heo4fb72032015-08-18 14:55:12 -0700459 struct throtl_grp *tg;
Tejun Heo24bdb8e2015-08-18 14:55:22 -0700460 int rw;
Tejun Heo4fb72032015-08-18 14:55:12 -0700461
462 tg = kzalloc_node(sizeof(*tg), gfp, node);
463 if (!tg)
Tejun Heo77ea7332015-08-18 14:55:24 -0700464 return NULL;
Tejun Heo4fb72032015-08-18 14:55:12 -0700465
Tejun Heob2ce2642015-08-18 14:55:13 -0700466 throtl_service_queue_init(&tg->service_queue);
467
468 for (rw = READ; rw <= WRITE; rw++) {
469 throtl_qnode_init(&tg->qnode_on_self[rw], tg);
470 throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
471 }
472
473 RB_CLEAR_NODE(&tg->rb_node);
Shaohua Li9f626e32017-03-27 10:51:30 -0700474 tg->bps[READ][LIMIT_MAX] = U64_MAX;
475 tg->bps[WRITE][LIMIT_MAX] = U64_MAX;
476 tg->iops[READ][LIMIT_MAX] = UINT_MAX;
477 tg->iops[WRITE][LIMIT_MAX] = UINT_MAX;
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700478 tg->bps_conf[READ][LIMIT_MAX] = U64_MAX;
479 tg->bps_conf[WRITE][LIMIT_MAX] = U64_MAX;
480 tg->iops_conf[READ][LIMIT_MAX] = UINT_MAX;
481 tg->iops_conf[WRITE][LIMIT_MAX] = UINT_MAX;
482 /* LIMIT_LOW will have default value 0 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700483
Shaohua Liec809912017-03-27 10:51:44 -0700484 tg->latency_target = DFL_LATENCY_TARGET;
485
Tejun Heo4fb72032015-08-18 14:55:12 -0700486 return &tg->pd;
Tejun Heo001bea72015-08-18 14:55:11 -0700487}
488
Tejun Heoa9520cd2015-08-18 14:55:14 -0700489static void throtl_pd_init(struct blkg_policy_data *pd)
Vivek Goyala29a1712011-05-19 15:38:19 -0400490{
Tejun Heoa9520cd2015-08-18 14:55:14 -0700491 struct throtl_grp *tg = pd_to_tg(pd);
492 struct blkcg_gq *blkg = tg_to_blkg(tg);
Tejun Heo77216b02013-05-14 13:52:36 -0700493 struct throtl_data *td = blkg->q->td;
Tejun Heob2ce2642015-08-18 14:55:13 -0700494 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heocd1604f2012-03-05 13:15:06 -0800495
Tejun Heo91381252013-05-14 13:52:38 -0700496 /*
Tejun Heoaa6ec292014-07-09 10:08:08 -0400497 * If on the default hierarchy, we switch to properly hierarchical
Tejun Heo91381252013-05-14 13:52:38 -0700498 * behavior where limits on a given throtl_grp are applied to the
499 * whole subtree rather than just the group itself. e.g. If 16M
500 * read_bps limit is set on the root group, the whole system can't
501 * exceed 16M for the device.
502 *
Tejun Heoaa6ec292014-07-09 10:08:08 -0400503 * If not on the default hierarchy, the broken flat hierarchy
Tejun Heo91381252013-05-14 13:52:38 -0700504 * behavior is retained where all throtl_grps are treated as if
505 * they're all separate root groups right below throtl_data.
506 * Limits of a group don't interact with limits of other groups
507 * regardless of the position of the group in the hierarchy.
508 */
Tejun Heob2ce2642015-08-18 14:55:13 -0700509 sq->parent_sq = &td->service_queue;
Tejun Heo9e10a132015-09-18 11:56:28 -0400510 if (cgroup_subsys_on_dfl(io_cgrp_subsys) && blkg->parent)
Tejun Heob2ce2642015-08-18 14:55:13 -0700511 sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
Tejun Heo77216b02013-05-14 13:52:36 -0700512 tg->td = td;
Shaohua Li9e234ee2017-03-27 10:51:41 -0700513
Shaohua Liada75b62017-03-27 10:51:42 -0700514 tg->idletime_threshold = td->dft_idletime_threshold;
Tejun Heo8a3d2612012-04-01 14:38:44 -0700515}
516
Tejun Heo693e7512013-05-14 13:52:38 -0700517/*
518 * Set has_rules[] if @tg or any of its parents have limits configured.
519 * This doesn't require walking up to the top of the hierarchy as the
520 * parent's has_rules[] is guaranteed to be correct.
521 */
522static void tg_update_has_rules(struct throtl_grp *tg)
523{
524 struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
Shaohua Li9f626e32017-03-27 10:51:30 -0700525 struct throtl_data *td = tg->td;
Tejun Heo693e7512013-05-14 13:52:38 -0700526 int rw;
527
528 for (rw = READ; rw <= WRITE; rw++)
529 tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
Shaohua Li9f626e32017-03-27 10:51:30 -0700530 (td->limit_valid[td->limit_index] &&
531 (tg_bps_limit(tg, rw) != U64_MAX ||
532 tg_iops_limit(tg, rw) != UINT_MAX));
Tejun Heo693e7512013-05-14 13:52:38 -0700533}
534
Tejun Heoa9520cd2015-08-18 14:55:14 -0700535static void throtl_pd_online(struct blkg_policy_data *pd)
Tejun Heo693e7512013-05-14 13:52:38 -0700536{
Shaohua Liaec24242017-03-27 10:51:39 -0700537 struct throtl_grp *tg = pd_to_tg(pd);
Tejun Heo693e7512013-05-14 13:52:38 -0700538 /*
539 * We don't want new groups to escape the limits of its ancestors.
540 * Update has_rules[] after a new group is brought online.
541 */
Shaohua Liaec24242017-03-27 10:51:39 -0700542 tg_update_has_rules(tg);
Tejun Heo693e7512013-05-14 13:52:38 -0700543}
544
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700545static void blk_throtl_update_limit_valid(struct throtl_data *td)
546{
547 struct cgroup_subsys_state *pos_css;
548 struct blkcg_gq *blkg;
549 bool low_valid = false;
550
551 rcu_read_lock();
552 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
553 struct throtl_grp *tg = blkg_to_tg(blkg);
554
555 if (tg->bps[READ][LIMIT_LOW] || tg->bps[WRITE][LIMIT_LOW] ||
556 tg->iops[READ][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
557 low_valid = true;
558 }
559 rcu_read_unlock();
560
561 td->limit_valid[LIMIT_LOW] = low_valid;
562}
563
Shaohua Lic79892c2017-03-27 10:51:34 -0700564static void throtl_upgrade_state(struct throtl_data *td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700565static void throtl_pd_offline(struct blkg_policy_data *pd)
566{
567 struct throtl_grp *tg = pd_to_tg(pd);
568
569 tg->bps[READ][LIMIT_LOW] = 0;
570 tg->bps[WRITE][LIMIT_LOW] = 0;
571 tg->iops[READ][LIMIT_LOW] = 0;
572 tg->iops[WRITE][LIMIT_LOW] = 0;
573
574 blk_throtl_update_limit_valid(tg->td);
575
Shaohua Lic79892c2017-03-27 10:51:34 -0700576 if (!tg->td->limit_valid[tg->td->limit_index])
577 throtl_upgrade_state(tg->td);
Shaohua Licd5ab1b2017-03-27 10:51:32 -0700578}
579
Tejun Heo001bea72015-08-18 14:55:11 -0700580static void throtl_pd_free(struct blkg_policy_data *pd)
581{
Tejun Heo4fb72032015-08-18 14:55:12 -0700582 struct throtl_grp *tg = pd_to_tg(pd);
583
Tejun Heob2ce2642015-08-18 14:55:13 -0700584 del_timer_sync(&tg->service_queue.pending_timer);
Tejun Heo4fb72032015-08-18 14:55:12 -0700585 kfree(tg);
Tejun Heo001bea72015-08-18 14:55:11 -0700586}
587
Tejun Heo0049af72013-05-14 13:52:33 -0700588static struct throtl_grp *
589throtl_rb_first(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400590{
591 /* Service tree is empty */
Tejun Heo0049af72013-05-14 13:52:33 -0700592 if (!parent_sq->nr_pending)
Vivek Goyale43473b2010-09-15 17:06:35 -0400593 return NULL;
594
Tejun Heo0049af72013-05-14 13:52:33 -0700595 if (!parent_sq->first_pending)
596 parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400597
Tejun Heo0049af72013-05-14 13:52:33 -0700598 if (parent_sq->first_pending)
599 return rb_entry_tg(parent_sq->first_pending);
Vivek Goyale43473b2010-09-15 17:06:35 -0400600
601 return NULL;
602}
603
604static void rb_erase_init(struct rb_node *n, struct rb_root *root)
605{
606 rb_erase(n, root);
607 RB_CLEAR_NODE(n);
608}
609
Tejun Heo0049af72013-05-14 13:52:33 -0700610static void throtl_rb_erase(struct rb_node *n,
611 struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400612{
Tejun Heo0049af72013-05-14 13:52:33 -0700613 if (parent_sq->first_pending == n)
614 parent_sq->first_pending = NULL;
615 rb_erase_init(n, &parent_sq->pending_tree);
616 --parent_sq->nr_pending;
Vivek Goyale43473b2010-09-15 17:06:35 -0400617}
618
Tejun Heo0049af72013-05-14 13:52:33 -0700619static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -0400620{
621 struct throtl_grp *tg;
622
Tejun Heo0049af72013-05-14 13:52:33 -0700623 tg = throtl_rb_first(parent_sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400624 if (!tg)
625 return;
626
Tejun Heo0049af72013-05-14 13:52:33 -0700627 parent_sq->first_pending_disptime = tg->disptime;
Vivek Goyale43473b2010-09-15 17:06:35 -0400628}
629
Tejun Heo77216b02013-05-14 13:52:36 -0700630static void tg_service_queue_add(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400631{
Tejun Heo77216b02013-05-14 13:52:36 -0700632 struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
Tejun Heo0049af72013-05-14 13:52:33 -0700633 struct rb_node **node = &parent_sq->pending_tree.rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400634 struct rb_node *parent = NULL;
635 struct throtl_grp *__tg;
636 unsigned long key = tg->disptime;
637 int left = 1;
638
639 while (*node != NULL) {
640 parent = *node;
641 __tg = rb_entry_tg(parent);
642
643 if (time_before(key, __tg->disptime))
644 node = &parent->rb_left;
645 else {
646 node = &parent->rb_right;
647 left = 0;
648 }
649 }
650
651 if (left)
Tejun Heo0049af72013-05-14 13:52:33 -0700652 parent_sq->first_pending = &tg->rb_node;
Vivek Goyale43473b2010-09-15 17:06:35 -0400653
654 rb_link_node(&tg->rb_node, parent, node);
Tejun Heo0049af72013-05-14 13:52:33 -0700655 rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
Vivek Goyale43473b2010-09-15 17:06:35 -0400656}
657
Tejun Heo77216b02013-05-14 13:52:36 -0700658static void __throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400659{
Tejun Heo77216b02013-05-14 13:52:36 -0700660 tg_service_queue_add(tg);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700661 tg->flags |= THROTL_TG_PENDING;
Tejun Heo77216b02013-05-14 13:52:36 -0700662 tg->service_queue.parent_sq->nr_pending++;
Vivek Goyale43473b2010-09-15 17:06:35 -0400663}
664
Tejun Heo77216b02013-05-14 13:52:36 -0700665static void throtl_enqueue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400666{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700667 if (!(tg->flags & THROTL_TG_PENDING))
Tejun Heo77216b02013-05-14 13:52:36 -0700668 __throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400669}
670
Tejun Heo77216b02013-05-14 13:52:36 -0700671static void __throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400672{
Tejun Heo77216b02013-05-14 13:52:36 -0700673 throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700674 tg->flags &= ~THROTL_TG_PENDING;
Vivek Goyale43473b2010-09-15 17:06:35 -0400675}
676
Tejun Heo77216b02013-05-14 13:52:36 -0700677static void throtl_dequeue_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -0400678{
Tejun Heo5b2c16a2013-05-14 13:52:32 -0700679 if (tg->flags & THROTL_TG_PENDING)
Tejun Heo77216b02013-05-14 13:52:36 -0700680 __throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -0400681}
682
Tejun Heoa9131a22013-05-14 13:52:31 -0700683/* Call with queue lock held */
Tejun Heo69df0ab2013-05-14 13:52:36 -0700684static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
685 unsigned long expires)
Tejun Heoa9131a22013-05-14 13:52:31 -0700686{
Shaohua Li297e3d82017-03-27 10:51:37 -0700687 unsigned long max_expire = jiffies + 8 * sq_to_tg(sq)->td->throtl_slice;
Shaohua Li06cceed2017-03-27 10:51:36 -0700688
689 /*
690 * Since we are adjusting the throttle limit dynamically, the sleep
691 * time calculated according to previous limit might be invalid. It's
692 * possible the cgroup sleep time is very long and no other cgroups
693 * have IO running so notify the limit changes. Make sure the cgroup
694 * doesn't sleep too long to avoid the missed notification.
695 */
696 if (time_after(expires, max_expire))
697 expires = max_expire;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700698 mod_timer(&sq->pending_timer, expires);
699 throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
700 expires - jiffies, jiffies);
Tejun Heoa9131a22013-05-14 13:52:31 -0700701}
702
Tejun Heo7f52f982013-05-14 13:52:37 -0700703/**
704 * throtl_schedule_next_dispatch - schedule the next dispatch cycle
705 * @sq: the service_queue to schedule dispatch for
706 * @force: force scheduling
707 *
708 * Arm @sq->pending_timer so that the next dispatch cycle starts on the
709 * dispatch time of the first pending child. Returns %true if either timer
710 * is armed or there's no pending child left. %false if the current
711 * dispatch window is still open and the caller should continue
712 * dispatching.
713 *
714 * If @force is %true, the dispatch timer is always scheduled and this
715 * function is guaranteed to return %true. This is to be used when the
716 * caller can't dispatch itself and needs to invoke pending_timer
717 * unconditionally. Note that forced scheduling is likely to induce short
718 * delay before dispatch starts even if @sq->first_pending_disptime is not
719 * in the future and thus shouldn't be used in hot paths.
720 */
721static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
722 bool force)
Vivek Goyale43473b2010-09-15 17:06:35 -0400723{
Tejun Heo6a525602013-05-14 13:52:32 -0700724 /* any pending children left? */
Tejun Heoc9e03322013-05-14 13:52:32 -0700725 if (!sq->nr_pending)
Tejun Heo7f52f982013-05-14 13:52:37 -0700726 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400727
Tejun Heoc9e03322013-05-14 13:52:32 -0700728 update_min_dispatch_time(sq);
Vivek Goyale43473b2010-09-15 17:06:35 -0400729
Tejun Heo69df0ab2013-05-14 13:52:36 -0700730 /* is the next dispatch time in the future? */
Tejun Heo7f52f982013-05-14 13:52:37 -0700731 if (force || time_after(sq->first_pending_disptime, jiffies)) {
Tejun Heo69df0ab2013-05-14 13:52:36 -0700732 throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
Tejun Heo7f52f982013-05-14 13:52:37 -0700733 return true;
Tejun Heo69df0ab2013-05-14 13:52:36 -0700734 }
735
Tejun Heo7f52f982013-05-14 13:52:37 -0700736 /* tell the caller to continue dispatching */
737 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400738}
739
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700740static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
741 bool rw, unsigned long start)
742{
743 tg->bytes_disp[rw] = 0;
744 tg->io_disp[rw] = 0;
745
746 /*
747 * Previous slice has expired. We must have trimmed it after last
748 * bio dispatch. That means since start of last slice, we never used
749 * that bandwidth. Do try to make use of that bandwidth while giving
750 * credit.
751 */
752 if (time_after_eq(start, tg->slice_start[rw]))
753 tg->slice_start[rw] = start;
754
Shaohua Li297e3d82017-03-27 10:51:37 -0700755 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Vivek Goyal32ee5bc2013-05-14 13:52:38 -0700756 throtl_log(&tg->service_queue,
757 "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
758 rw == READ ? 'R' : 'W', tg->slice_start[rw],
759 tg->slice_end[rw], jiffies);
760}
761
Tejun Heo0f3457f2013-05-14 13:52:32 -0700762static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400763{
764 tg->bytes_disp[rw] = 0;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400765 tg->io_disp[rw] = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -0400766 tg->slice_start[rw] = jiffies;
Shaohua Li297e3d82017-03-27 10:51:37 -0700767 tg->slice_end[rw] = jiffies + tg->td->throtl_slice;
Tejun Heofda6f272013-05-14 13:52:36 -0700768 throtl_log(&tg->service_queue,
769 "[%c] new slice start=%lu end=%lu jiffies=%lu",
770 rw == READ ? 'R' : 'W', tg->slice_start[rw],
771 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400772}
773
Tejun Heo0f3457f2013-05-14 13:52:32 -0700774static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
775 unsigned long jiffy_end)
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100776{
Shaohua Li297e3d82017-03-27 10:51:37 -0700777 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100778}
779
Tejun Heo0f3457f2013-05-14 13:52:32 -0700780static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
781 unsigned long jiffy_end)
Vivek Goyale43473b2010-09-15 17:06:35 -0400782{
Shaohua Li297e3d82017-03-27 10:51:37 -0700783 tg->slice_end[rw] = roundup(jiffy_end, tg->td->throtl_slice);
Tejun Heofda6f272013-05-14 13:52:36 -0700784 throtl_log(&tg->service_queue,
785 "[%c] extend slice start=%lu end=%lu jiffies=%lu",
786 rw == READ ? 'R' : 'W', tg->slice_start[rw],
787 tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400788}
789
790/* Determine if previously allocated or extended slice is complete or not */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700791static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400792{
793 if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200794 return false;
Vivek Goyale43473b2010-09-15 17:06:35 -0400795
796 return 1;
797}
798
799/* Trim the used slices and adjust slice start accordingly */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700800static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -0400801{
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200802 unsigned long nr_slices, time_elapsed, io_trim;
803 u64 bytes_trim, tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400804
805 BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
806
807 /*
808 * If bps are unlimited (-1), then time slice don't get
809 * renewed. Don't try to trim the slice if slice is used. A new
810 * slice will start when appropriate.
811 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700812 if (throtl_slice_used(tg, rw))
Vivek Goyale43473b2010-09-15 17:06:35 -0400813 return;
814
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100815 /*
816 * A bio has been dispatched. Also adjust slice_end. It might happen
817 * that initially cgroup limit was very low resulting in high
818 * slice_end, but later limit was bumped up and bio was dispached
819 * sooner, then we need to reduce slice_end. A high bogus slice_end
820 * is bad because it does not allow new slice to start.
821 */
822
Shaohua Li297e3d82017-03-27 10:51:37 -0700823 throtl_set_slice_end(tg, rw, jiffies + tg->td->throtl_slice);
Vivek Goyald1ae8ff2010-12-01 19:34:46 +0100824
Vivek Goyale43473b2010-09-15 17:06:35 -0400825 time_elapsed = jiffies - tg->slice_start[rw];
826
Shaohua Li297e3d82017-03-27 10:51:37 -0700827 nr_slices = time_elapsed / tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400828
829 if (!nr_slices)
830 return;
Shaohua Li297e3d82017-03-27 10:51:37 -0700831 tmp = tg_bps_limit(tg, rw) * tg->td->throtl_slice * nr_slices;
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200832 do_div(tmp, HZ);
833 bytes_trim = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400834
Shaohua Li297e3d82017-03-27 10:51:37 -0700835 io_trim = (tg_iops_limit(tg, rw) * tg->td->throtl_slice * nr_slices) /
836 HZ;
Vivek Goyale43473b2010-09-15 17:06:35 -0400837
Vivek Goyal8e89d132010-09-15 17:06:37 -0400838 if (!bytes_trim && !io_trim)
Vivek Goyale43473b2010-09-15 17:06:35 -0400839 return;
840
841 if (tg->bytes_disp[rw] >= bytes_trim)
842 tg->bytes_disp[rw] -= bytes_trim;
843 else
844 tg->bytes_disp[rw] = 0;
845
Vivek Goyal8e89d132010-09-15 17:06:37 -0400846 if (tg->io_disp[rw] >= io_trim)
847 tg->io_disp[rw] -= io_trim;
848 else
849 tg->io_disp[rw] = 0;
850
Shaohua Li297e3d82017-03-27 10:51:37 -0700851 tg->slice_start[rw] += nr_slices * tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400852
Tejun Heofda6f272013-05-14 13:52:36 -0700853 throtl_log(&tg->service_queue,
854 "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
855 rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
856 tg->slice_start[rw], tg->slice_end[rw], jiffies);
Vivek Goyale43473b2010-09-15 17:06:35 -0400857}
858
Tejun Heo0f3457f2013-05-14 13:52:32 -0700859static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
860 unsigned long *wait)
Vivek Goyale43473b2010-09-15 17:06:35 -0400861{
862 bool rw = bio_data_dir(bio);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400863 unsigned int io_allowed;
Vivek Goyale43473b2010-09-15 17:06:35 -0400864 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200865 u64 tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400866
Vivek Goyal8e89d132010-09-15 17:06:37 -0400867 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
Vivek Goyale43473b2010-09-15 17:06:35 -0400868
Vivek Goyal8e89d132010-09-15 17:06:37 -0400869 /* Slice has just started. Consider one slice interval */
870 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700871 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400872
Shaohua Li297e3d82017-03-27 10:51:37 -0700873 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400874
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200875 /*
876 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
877 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
878 * will allow dispatch after 1 second and after that slice should
879 * have been trimmed.
880 */
881
Shaohua Li9f626e32017-03-27 10:51:30 -0700882 tmp = (u64)tg_iops_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyalc49c06e2010-10-01 21:16:42 +0200883 do_div(tmp, HZ);
884
885 if (tmp > UINT_MAX)
886 io_allowed = UINT_MAX;
887 else
888 io_allowed = tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400889
890 if (tg->io_disp[rw] + 1 <= io_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400891 if (wait)
892 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200893 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400894 }
895
Vivek Goyal8e89d132010-09-15 17:06:37 -0400896 /* Calc approx time to dispatch */
Shaohua Li9f626e32017-03-27 10:51:30 -0700897 jiffy_wait = ((tg->io_disp[rw] + 1) * HZ) / tg_iops_limit(tg, rw) + 1;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400898
899 if (jiffy_wait > jiffy_elapsed)
900 jiffy_wait = jiffy_wait - jiffy_elapsed;
901 else
902 jiffy_wait = 1;
903
904 if (wait)
905 *wait = jiffy_wait;
906 return 0;
907}
908
Tejun Heo0f3457f2013-05-14 13:52:32 -0700909static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
910 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400911{
912 bool rw = bio_data_dir(bio);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200913 u64 bytes_allowed, extra_bytes, tmp;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400914 unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
Vivek Goyale43473b2010-09-15 17:06:35 -0400915
916 jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
917
918 /* Slice has just started. Consider one slice interval */
919 if (!jiffy_elapsed)
Shaohua Li297e3d82017-03-27 10:51:37 -0700920 jiffy_elapsed_rnd = tg->td->throtl_slice;
Vivek Goyale43473b2010-09-15 17:06:35 -0400921
Shaohua Li297e3d82017-03-27 10:51:37 -0700922 jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, tg->td->throtl_slice);
Vivek Goyale43473b2010-09-15 17:06:35 -0400923
Shaohua Li9f626e32017-03-27 10:51:30 -0700924 tmp = tg_bps_limit(tg, rw) * jiffy_elapsed_rnd;
Vivek Goyal5e901a22010-10-01 21:16:38 +0200925 do_div(tmp, HZ);
Vivek Goyal3aad5d32010-10-01 14:51:14 +0200926 bytes_allowed = tmp;
Vivek Goyale43473b2010-09-15 17:06:35 -0400927
Kent Overstreet4f024f32013-10-11 15:44:27 -0700928 if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
Vivek Goyale43473b2010-09-15 17:06:35 -0400929 if (wait)
930 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200931 return true;
Vivek Goyale43473b2010-09-15 17:06:35 -0400932 }
933
934 /* Calc approx time to dispatch */
Kent Overstreet4f024f32013-10-11 15:44:27 -0700935 extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
Shaohua Li9f626e32017-03-27 10:51:30 -0700936 jiffy_wait = div64_u64(extra_bytes * HZ, tg_bps_limit(tg, rw));
Vivek Goyale43473b2010-09-15 17:06:35 -0400937
938 if (!jiffy_wait)
939 jiffy_wait = 1;
940
941 /*
942 * This wait time is without taking into consideration the rounding
943 * up we did. Add that time also.
944 */
945 jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
Vivek Goyale43473b2010-09-15 17:06:35 -0400946 if (wait)
947 *wait = jiffy_wait;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400948 return 0;
949}
Vivek Goyale43473b2010-09-15 17:06:35 -0400950
Vivek Goyal8e89d132010-09-15 17:06:37 -0400951/*
952 * Returns whether one can dispatch a bio or not. Also returns approx number
953 * of jiffies to wait before this bio is with-in IO rate and can be dispatched
954 */
Tejun Heo0f3457f2013-05-14 13:52:32 -0700955static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
956 unsigned long *wait)
Vivek Goyal8e89d132010-09-15 17:06:37 -0400957{
958 bool rw = bio_data_dir(bio);
959 unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
960
961 /*
962 * Currently whole state machine of group depends on first bio
963 * queued in the group bio list. So one should not be calling
964 * this function with a different bio if there are other bios
965 * queued.
966 */
Tejun Heo73f0d492013-05-14 13:52:35 -0700967 BUG_ON(tg->service_queue.nr_queued[rw] &&
Tejun Heoc5cc2072013-05-14 13:52:38 -0700968 bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
Vivek Goyal8e89d132010-09-15 17:06:37 -0400969
970 /* If tg->bps = -1, then BW is unlimited */
Shaohua Li9f626e32017-03-27 10:51:30 -0700971 if (tg_bps_limit(tg, rw) == U64_MAX &&
972 tg_iops_limit(tg, rw) == UINT_MAX) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400973 if (wait)
974 *wait = 0;
Fabian Frederick5cf8c222014-05-02 18:28:17 +0200975 return true;
Vivek Goyal8e89d132010-09-15 17:06:37 -0400976 }
977
978 /*
979 * If previous slice expired, start a new one otherwise renew/extend
980 * existing slice to make sure it is at least throtl_slice interval
Vivek Goyal164c80e2016-09-19 15:12:41 -0600981 * long since now. New slice is started only for empty throttle group.
982 * If there is queued bio, that means there should be an active
983 * slice and it should be extended instead.
Vivek Goyal8e89d132010-09-15 17:06:37 -0400984 */
Vivek Goyal164c80e2016-09-19 15:12:41 -0600985 if (throtl_slice_used(tg, rw) && !(tg->service_queue.nr_queued[rw]))
Tejun Heo0f3457f2013-05-14 13:52:32 -0700986 throtl_start_new_slice(tg, rw);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400987 else {
Shaohua Li297e3d82017-03-27 10:51:37 -0700988 if (time_before(tg->slice_end[rw],
989 jiffies + tg->td->throtl_slice))
990 throtl_extend_slice(tg, rw,
991 jiffies + tg->td->throtl_slice);
Vivek Goyal8e89d132010-09-15 17:06:37 -0400992 }
993
Tejun Heo0f3457f2013-05-14 13:52:32 -0700994 if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
995 tg_with_in_iops_limit(tg, bio, &iops_wait)) {
Vivek Goyal8e89d132010-09-15 17:06:37 -0400996 if (wait)
997 *wait = 0;
998 return 1;
999 }
1000
1001 max_wait = max(bps_wait, iops_wait);
1002
1003 if (wait)
1004 *wait = max_wait;
1005
1006 if (time_before(tg->slice_end[rw], jiffies + max_wait))
Tejun Heo0f3457f2013-05-14 13:52:32 -07001007 throtl_extend_slice(tg, rw, jiffies + max_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001008
1009 return 0;
1010}
1011
1012static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
1013{
1014 bool rw = bio_data_dir(bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001015
1016 /* Charge the bio to the group */
Kent Overstreet4f024f32013-10-11 15:44:27 -07001017 tg->bytes_disp[rw] += bio->bi_iter.bi_size;
Vivek Goyal8e89d132010-09-15 17:06:37 -04001018 tg->io_disp[rw]++;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001019 tg->last_bytes_disp[rw] += bio->bi_iter.bi_size;
1020 tg->last_io_disp[rw]++;
Vivek Goyale43473b2010-09-15 17:06:35 -04001021
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001022 /*
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001023 * BIO_THROTTLED is used to prevent the same bio to be throttled
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001024 * more than once as a throttled bio will go through blk-throtl the
1025 * second time when it eventually gets issued. Set it when a bio
1026 * is being charged to a tg.
Tejun Heo2a0f61e2013-05-14 13:52:36 -07001027 */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02001028 if (!bio_flagged(bio, BIO_THROTTLED))
1029 bio_set_flag(bio, BIO_THROTTLED);
Vivek Goyale43473b2010-09-15 17:06:35 -04001030}
1031
Tejun Heoc5cc2072013-05-14 13:52:38 -07001032/**
1033 * throtl_add_bio_tg - add a bio to the specified throtl_grp
1034 * @bio: bio to add
1035 * @qn: qnode to use
1036 * @tg: the target throtl_grp
1037 *
1038 * Add @bio to @tg's service_queue using @qn. If @qn is not specified,
1039 * tg->qnode_on_self[] is used.
1040 */
1041static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
1042 struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001043{
Tejun Heo73f0d492013-05-14 13:52:35 -07001044 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001045 bool rw = bio_data_dir(bio);
1046
Tejun Heoc5cc2072013-05-14 13:52:38 -07001047 if (!qn)
1048 qn = &tg->qnode_on_self[rw];
1049
Tejun Heo0e9f4162013-05-14 13:52:35 -07001050 /*
1051 * If @tg doesn't currently have any bios queued in the same
1052 * direction, queueing @bio can change when @tg should be
1053 * dispatched. Mark that @tg was empty. This is automatically
1054 * cleaered on the next tg_update_disptime().
1055 */
1056 if (!sq->nr_queued[rw])
1057 tg->flags |= THROTL_TG_WAS_EMPTY;
1058
Tejun Heoc5cc2072013-05-14 13:52:38 -07001059 throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
1060
Tejun Heo73f0d492013-05-14 13:52:35 -07001061 sq->nr_queued[rw]++;
Tejun Heo77216b02013-05-14 13:52:36 -07001062 throtl_enqueue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001063}
1064
Tejun Heo77216b02013-05-14 13:52:36 -07001065static void tg_update_disptime(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001066{
Tejun Heo73f0d492013-05-14 13:52:35 -07001067 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001068 unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
1069 struct bio *bio;
1070
Markus Elfringd609af32017-01-21 22:15:33 +01001071 bio = throtl_peek_queued(&sq->queued[READ]);
1072 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -07001073 tg_may_dispatch(tg, bio, &read_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001074
Markus Elfringd609af32017-01-21 22:15:33 +01001075 bio = throtl_peek_queued(&sq->queued[WRITE]);
1076 if (bio)
Tejun Heo0f3457f2013-05-14 13:52:32 -07001077 tg_may_dispatch(tg, bio, &write_wait);
Vivek Goyale43473b2010-09-15 17:06:35 -04001078
1079 min_wait = min(read_wait, write_wait);
1080 disptime = jiffies + min_wait;
1081
Vivek Goyale43473b2010-09-15 17:06:35 -04001082 /* Update dispatch time */
Tejun Heo77216b02013-05-14 13:52:36 -07001083 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001084 tg->disptime = disptime;
Tejun Heo77216b02013-05-14 13:52:36 -07001085 throtl_enqueue_tg(tg);
Tejun Heo0e9f4162013-05-14 13:52:35 -07001086
1087 /* see throtl_add_bio_tg() */
1088 tg->flags &= ~THROTL_TG_WAS_EMPTY;
Vivek Goyale43473b2010-09-15 17:06:35 -04001089}
1090
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001091static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1092 struct throtl_grp *parent_tg, bool rw)
1093{
1094 if (throtl_slice_used(parent_tg, rw)) {
1095 throtl_start_new_slice_with_credit(parent_tg, rw,
1096 child_tg->slice_start[rw]);
1097 }
1098
1099}
1100
Tejun Heo77216b02013-05-14 13:52:36 -07001101static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
Vivek Goyale43473b2010-09-15 17:06:35 -04001102{
Tejun Heo73f0d492013-05-14 13:52:35 -07001103 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001104 struct throtl_service_queue *parent_sq = sq->parent_sq;
1105 struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001106 struct throtl_grp *tg_to_put = NULL;
Vivek Goyale43473b2010-09-15 17:06:35 -04001107 struct bio *bio;
1108
Tejun Heoc5cc2072013-05-14 13:52:38 -07001109 /*
1110 * @bio is being transferred from @tg to @parent_sq. Popping a bio
1111 * from @tg may put its reference and @parent_sq might end up
1112 * getting released prematurely. Remember the tg to put and put it
1113 * after @bio is transferred to @parent_sq.
1114 */
1115 bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
Tejun Heo73f0d492013-05-14 13:52:35 -07001116 sq->nr_queued[rw]--;
Vivek Goyale43473b2010-09-15 17:06:35 -04001117
1118 throtl_charge_bio(tg, bio);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001119
1120 /*
1121 * If our parent is another tg, we just need to transfer @bio to
1122 * the parent using throtl_add_bio_tg(). If our parent is
1123 * @td->service_queue, @bio is ready to be issued. Put it on its
1124 * bio_lists[] and decrease total number queued. The caller is
1125 * responsible for issuing these bios.
1126 */
1127 if (parent_tg) {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001128 throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
Vivek Goyal32ee5bc2013-05-14 13:52:38 -07001129 start_parent_slice_with_credit(tg, parent_tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001130 } else {
Tejun Heoc5cc2072013-05-14 13:52:38 -07001131 throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1132 &parent_sq->queued[rw]);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001133 BUG_ON(tg->td->nr_queued[rw] <= 0);
1134 tg->td->nr_queued[rw]--;
1135 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001136
Tejun Heo0f3457f2013-05-14 13:52:32 -07001137 throtl_trim_slice(tg, rw);
Tejun Heo6bc9c2b2013-05-14 13:52:38 -07001138
Tejun Heoc5cc2072013-05-14 13:52:38 -07001139 if (tg_to_put)
1140 blkg_put(tg_to_blkg(tg_to_put));
Vivek Goyale43473b2010-09-15 17:06:35 -04001141}
1142
Tejun Heo77216b02013-05-14 13:52:36 -07001143static int throtl_dispatch_tg(struct throtl_grp *tg)
Vivek Goyale43473b2010-09-15 17:06:35 -04001144{
Tejun Heo73f0d492013-05-14 13:52:35 -07001145 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001146 unsigned int nr_reads = 0, nr_writes = 0;
1147 unsigned int max_nr_reads = throtl_grp_quantum*3/4;
Vivek Goyalc2f68052010-11-15 19:32:42 +01001148 unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
Vivek Goyale43473b2010-09-15 17:06:35 -04001149 struct bio *bio;
1150
1151 /* Try to dispatch 75% READS and 25% WRITES */
1152
Tejun Heoc5cc2072013-05-14 13:52:38 -07001153 while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001154 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001155
Tejun Heo77216b02013-05-14 13:52:36 -07001156 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001157 nr_reads++;
1158
1159 if (nr_reads >= max_nr_reads)
1160 break;
1161 }
1162
Tejun Heoc5cc2072013-05-14 13:52:38 -07001163 while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
Tejun Heo0f3457f2013-05-14 13:52:32 -07001164 tg_may_dispatch(tg, bio, NULL)) {
Vivek Goyale43473b2010-09-15 17:06:35 -04001165
Tejun Heo77216b02013-05-14 13:52:36 -07001166 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Vivek Goyale43473b2010-09-15 17:06:35 -04001167 nr_writes++;
1168
1169 if (nr_writes >= max_nr_writes)
1170 break;
1171 }
1172
1173 return nr_reads + nr_writes;
1174}
1175
Tejun Heo651930b2013-05-14 13:52:35 -07001176static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
Vivek Goyale43473b2010-09-15 17:06:35 -04001177{
1178 unsigned int nr_disp = 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001179
1180 while (1) {
Tejun Heo73f0d492013-05-14 13:52:35 -07001181 struct throtl_grp *tg = throtl_rb_first(parent_sq);
1182 struct throtl_service_queue *sq = &tg->service_queue;
Vivek Goyale43473b2010-09-15 17:06:35 -04001183
1184 if (!tg)
1185 break;
1186
1187 if (time_before(jiffies, tg->disptime))
1188 break;
1189
Tejun Heo77216b02013-05-14 13:52:36 -07001190 throtl_dequeue_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001191
Tejun Heo77216b02013-05-14 13:52:36 -07001192 nr_disp += throtl_dispatch_tg(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001193
Tejun Heo73f0d492013-05-14 13:52:35 -07001194 if (sq->nr_queued[0] || sq->nr_queued[1])
Tejun Heo77216b02013-05-14 13:52:36 -07001195 tg_update_disptime(tg);
Vivek Goyale43473b2010-09-15 17:06:35 -04001196
1197 if (nr_disp >= throtl_quantum)
1198 break;
1199 }
1200
1201 return nr_disp;
1202}
1203
Shaohua Lic79892c2017-03-27 10:51:34 -07001204static bool throtl_can_upgrade(struct throtl_data *td,
1205 struct throtl_grp *this_tg);
Tejun Heo6e1a5702013-05-14 13:52:37 -07001206/**
1207 * throtl_pending_timer_fn - timer function for service_queue->pending_timer
1208 * @arg: the throtl_service_queue being serviced
1209 *
1210 * This timer is armed when a child throtl_grp with active bio's become
1211 * pending and queued on the service_queue's pending_tree and expires when
1212 * the first child throtl_grp should be dispatched. This function
Tejun Heo2e48a532013-05-14 13:52:38 -07001213 * dispatches bio's from the children throtl_grps to the parent
1214 * service_queue.
1215 *
1216 * If the parent's parent is another throtl_grp, dispatching is propagated
1217 * by either arming its pending_timer or repeating dispatch directly. If
1218 * the top-level service_tree is reached, throtl_data->dispatch_work is
1219 * kicked so that the ready bio's are issued.
Tejun Heo6e1a5702013-05-14 13:52:37 -07001220 */
Tejun Heo69df0ab2013-05-14 13:52:36 -07001221static void throtl_pending_timer_fn(unsigned long arg)
1222{
1223 struct throtl_service_queue *sq = (void *)arg;
Tejun Heo2e48a532013-05-14 13:52:38 -07001224 struct throtl_grp *tg = sq_to_tg(sq);
Tejun Heo69df0ab2013-05-14 13:52:36 -07001225 struct throtl_data *td = sq_to_td(sq);
Tejun Heocb761992013-05-14 13:52:31 -07001226 struct request_queue *q = td->queue;
Tejun Heo2e48a532013-05-14 13:52:38 -07001227 struct throtl_service_queue *parent_sq;
1228 bool dispatched;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001229 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04001230
1231 spin_lock_irq(q->queue_lock);
Shaohua Lic79892c2017-03-27 10:51:34 -07001232 if (throtl_can_upgrade(td, NULL))
1233 throtl_upgrade_state(td);
1234
Tejun Heo2e48a532013-05-14 13:52:38 -07001235again:
1236 parent_sq = sq->parent_sq;
1237 dispatched = false;
Vivek Goyale43473b2010-09-15 17:06:35 -04001238
Tejun Heo7f52f982013-05-14 13:52:37 -07001239 while (true) {
1240 throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
Tejun Heo2e48a532013-05-14 13:52:38 -07001241 sq->nr_queued[READ] + sq->nr_queued[WRITE],
1242 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04001243
Tejun Heo7f52f982013-05-14 13:52:37 -07001244 ret = throtl_select_dispatch(sq);
1245 if (ret) {
Tejun Heo7f52f982013-05-14 13:52:37 -07001246 throtl_log(sq, "bios disp=%u", ret);
1247 dispatched = true;
Tejun Heo651930b2013-05-14 13:52:35 -07001248 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001249
Tejun Heo7f52f982013-05-14 13:52:37 -07001250 if (throtl_schedule_next_dispatch(sq, false))
1251 break;
1252
1253 /* this dispatch windows is still open, relax and repeat */
1254 spin_unlock_irq(q->queue_lock);
1255 cpu_relax();
1256 spin_lock_irq(q->queue_lock);
1257 }
Tejun Heo6a525602013-05-14 13:52:32 -07001258
Tejun Heo2e48a532013-05-14 13:52:38 -07001259 if (!dispatched)
1260 goto out_unlock;
Tejun Heo6e1a5702013-05-14 13:52:37 -07001261
Tejun Heo2e48a532013-05-14 13:52:38 -07001262 if (parent_sq) {
1263 /* @parent_sq is another throl_grp, propagate dispatch */
1264 if (tg->flags & THROTL_TG_WAS_EMPTY) {
1265 tg_update_disptime(tg);
1266 if (!throtl_schedule_next_dispatch(parent_sq, false)) {
1267 /* window is already open, repeat dispatching */
1268 sq = parent_sq;
1269 tg = sq_to_tg(sq);
1270 goto again;
1271 }
1272 }
1273 } else {
1274 /* reached the top-level, queue issueing */
1275 queue_work(kthrotld_workqueue, &td->dispatch_work);
1276 }
1277out_unlock:
Tejun Heo6e1a5702013-05-14 13:52:37 -07001278 spin_unlock_irq(q->queue_lock);
1279}
1280
1281/**
1282 * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
1283 * @work: work item being executed
1284 *
1285 * This function is queued for execution when bio's reach the bio_lists[]
1286 * of throtl_data->service_queue. Those bio's are ready and issued by this
1287 * function.
1288 */
Fabian Frederick8876e142014-04-17 21:41:16 +02001289static void blk_throtl_dispatch_work_fn(struct work_struct *work)
Tejun Heo6e1a5702013-05-14 13:52:37 -07001290{
1291 struct throtl_data *td = container_of(work, struct throtl_data,
1292 dispatch_work);
1293 struct throtl_service_queue *td_sq = &td->service_queue;
1294 struct request_queue *q = td->queue;
1295 struct bio_list bio_list_on_stack;
1296 struct bio *bio;
1297 struct blk_plug plug;
1298 int rw;
1299
1300 bio_list_init(&bio_list_on_stack);
1301
1302 spin_lock_irq(q->queue_lock);
Tejun Heoc5cc2072013-05-14 13:52:38 -07001303 for (rw = READ; rw <= WRITE; rw++)
1304 while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1305 bio_list_add(&bio_list_on_stack, bio);
Vivek Goyale43473b2010-09-15 17:06:35 -04001306 spin_unlock_irq(q->queue_lock);
1307
Tejun Heo6e1a5702013-05-14 13:52:37 -07001308 if (!bio_list_empty(&bio_list_on_stack)) {
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001309 blk_start_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001310 while((bio = bio_list_pop(&bio_list_on_stack)))
1311 generic_make_request(bio);
Vivek Goyal69d60eb2011-03-09 08:27:37 +01001312 blk_finish_plug(&plug);
Vivek Goyale43473b2010-09-15 17:06:35 -04001313 }
Vivek Goyale43473b2010-09-15 17:06:35 -04001314}
1315
Tejun Heof95a04a2012-04-16 13:57:26 -07001316static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1317 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001318{
Tejun Heof95a04a2012-04-16 13:57:26 -07001319 struct throtl_grp *tg = pd_to_tg(pd);
1320 u64 v = *(u64 *)((void *)tg + off);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001321
Shaohua Li2ab54922017-03-27 10:51:29 -07001322 if (v == U64_MAX)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001323 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001324 return __blkg_prfill_u64(sf, pd, v);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001325}
1326
Tejun Heof95a04a2012-04-16 13:57:26 -07001327static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1328 int off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001329{
Tejun Heof95a04a2012-04-16 13:57:26 -07001330 struct throtl_grp *tg = pd_to_tg(pd);
1331 unsigned int v = *(unsigned int *)((void *)tg + off);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001332
Shaohua Li2ab54922017-03-27 10:51:29 -07001333 if (v == UINT_MAX)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001334 return 0;
Tejun Heof95a04a2012-04-16 13:57:26 -07001335 return __blkg_prfill_u64(sf, pd, v);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001336}
1337
Tejun Heo2da8ca82013-12-05 12:28:04 -05001338static int tg_print_conf_u64(struct seq_file *sf, void *v)
Tejun Heoaf133ce2012-04-01 14:38:44 -07001339{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001340 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
1341 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001342 return 0;
1343}
1344
Tejun Heo2da8ca82013-12-05 12:28:04 -05001345static int tg_print_conf_uint(struct seq_file *sf, void *v)
Vivek Goyale43473b2010-09-15 17:06:35 -04001346{
Tejun Heo2da8ca82013-12-05 12:28:04 -05001347 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
1348 &blkcg_policy_throtl, seq_cft(sf)->private, false);
Tejun Heoaf133ce2012-04-01 14:38:44 -07001349 return 0;
Vivek Goyale43473b2010-09-15 17:06:35 -04001350}
1351
Tejun Heo69948b02015-08-18 14:55:32 -07001352static void tg_conf_updated(struct throtl_grp *tg)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001353{
Tejun Heo69948b02015-08-18 14:55:32 -07001354 struct throtl_service_queue *sq = &tg->service_queue;
Tejun Heo492eb212013-08-08 20:11:25 -04001355 struct cgroup_subsys_state *pos_css;
Tejun Heo69948b02015-08-18 14:55:32 -07001356 struct blkcg_gq *blkg;
Tejun Heoaf133ce2012-04-01 14:38:44 -07001357
Tejun Heofda6f272013-05-14 13:52:36 -07001358 throtl_log(&tg->service_queue,
1359 "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
Shaohua Li9f626e32017-03-27 10:51:30 -07001360 tg_bps_limit(tg, READ), tg_bps_limit(tg, WRITE),
1361 tg_iops_limit(tg, READ), tg_iops_limit(tg, WRITE));
Tejun Heo632b4492013-05-14 13:52:31 -07001362
1363 /*
Tejun Heo693e7512013-05-14 13:52:38 -07001364 * Update has_rules[] flags for the updated tg's subtree. A tg is
1365 * considered to have rules if either the tg itself or any of its
1366 * ancestors has rules. This identifies groups without any
1367 * restrictions in the whole hierarchy and allows them to bypass
1368 * blk-throttle.
1369 */
Tejun Heo69948b02015-08-18 14:55:32 -07001370 blkg_for_each_descendant_pre(blkg, pos_css, tg_to_blkg(tg))
Tejun Heo693e7512013-05-14 13:52:38 -07001371 tg_update_has_rules(blkg_to_tg(blkg));
1372
1373 /*
Tejun Heo632b4492013-05-14 13:52:31 -07001374 * We're already holding queue_lock and know @tg is valid. Let's
1375 * apply the new config directly.
1376 *
1377 * Restart the slices for both READ and WRITES. It might happen
1378 * that a group's limit are dropped suddenly and we don't want to
1379 * account recently dispatched IO with new low rate.
1380 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07001381 throtl_start_new_slice(tg, 0);
1382 throtl_start_new_slice(tg, 1);
Tejun Heo632b4492013-05-14 13:52:31 -07001383
Tejun Heo5b2c16a2013-05-14 13:52:32 -07001384 if (tg->flags & THROTL_TG_PENDING) {
Tejun Heo77216b02013-05-14 13:52:36 -07001385 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07001386 throtl_schedule_next_dispatch(sq->parent_sq, true);
Tejun Heo632b4492013-05-14 13:52:31 -07001387 }
Tejun Heo69948b02015-08-18 14:55:32 -07001388}
Tejun Heo60c2bc22012-04-01 14:38:43 -07001389
Tejun Heo69948b02015-08-18 14:55:32 -07001390static ssize_t tg_set_conf(struct kernfs_open_file *of,
1391 char *buf, size_t nbytes, loff_t off, bool is_u64)
1392{
1393 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1394 struct blkg_conf_ctx ctx;
1395 struct throtl_grp *tg;
1396 int ret;
1397 u64 v;
1398
1399 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1400 if (ret)
1401 return ret;
1402
1403 ret = -EINVAL;
1404 if (sscanf(ctx.body, "%llu", &v) != 1)
1405 goto out_finish;
1406 if (!v)
Shaohua Li2ab54922017-03-27 10:51:29 -07001407 v = U64_MAX;
Tejun Heo69948b02015-08-18 14:55:32 -07001408
1409 tg = blkg_to_tg(ctx.blkg);
1410
1411 if (is_u64)
1412 *(u64 *)((void *)tg + of_cft(of)->private) = v;
1413 else
1414 *(unsigned int *)((void *)tg + of_cft(of)->private) = v;
1415
1416 tg_conf_updated(tg);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001417 ret = 0;
1418out_finish:
Tejun Heo60c2bc22012-04-01 14:38:43 -07001419 blkg_conf_finish(&ctx);
Tejun Heo36aa9e52015-08-18 14:55:31 -07001420 return ret ?: nbytes;
Tejun Heo60c2bc22012-04-01 14:38:43 -07001421}
1422
Tejun Heo451af502014-05-13 12:16:21 -04001423static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1424 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001425{
Tejun Heo451af502014-05-13 12:16:21 -04001426 return tg_set_conf(of, buf, nbytes, off, true);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001427}
1428
Tejun Heo451af502014-05-13 12:16:21 -04001429static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1430 char *buf, size_t nbytes, loff_t off)
Tejun Heo60c2bc22012-04-01 14:38:43 -07001431{
Tejun Heo451af502014-05-13 12:16:21 -04001432 return tg_set_conf(of, buf, nbytes, off, false);
Tejun Heo60c2bc22012-04-01 14:38:43 -07001433}
1434
Tejun Heo880f50e2015-08-18 14:55:30 -07001435static struct cftype throtl_legacy_files[] = {
Tejun Heo60c2bc22012-04-01 14:38:43 -07001436 {
1437 .name = "throttle.read_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001438 .private = offsetof(struct throtl_grp, bps[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001439 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001440 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001441 },
1442 {
1443 .name = "throttle.write_bps_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001444 .private = offsetof(struct throtl_grp, bps[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001445 .seq_show = tg_print_conf_u64,
Tejun Heo451af502014-05-13 12:16:21 -04001446 .write = tg_set_conf_u64,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001447 },
1448 {
1449 .name = "throttle.read_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001450 .private = offsetof(struct throtl_grp, iops[READ][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001451 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001452 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001453 },
1454 {
1455 .name = "throttle.write_iops_device",
Shaohua Li9f626e32017-03-27 10:51:30 -07001456 .private = offsetof(struct throtl_grp, iops[WRITE][LIMIT_MAX]),
Tejun Heo2da8ca82013-12-05 12:28:04 -05001457 .seq_show = tg_print_conf_uint,
Tejun Heo451af502014-05-13 12:16:21 -04001458 .write = tg_set_conf_uint,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001459 },
1460 {
1461 .name = "throttle.io_service_bytes",
Tejun Heo77ea7332015-08-18 14:55:24 -07001462 .private = (unsigned long)&blkcg_policy_throtl,
1463 .seq_show = blkg_print_stat_bytes,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001464 },
1465 {
1466 .name = "throttle.io_serviced",
Tejun Heo77ea7332015-08-18 14:55:24 -07001467 .private = (unsigned long)&blkcg_policy_throtl,
1468 .seq_show = blkg_print_stat_ios,
Tejun Heo60c2bc22012-04-01 14:38:43 -07001469 },
1470 { } /* terminate */
1471};
1472
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001473static u64 tg_prfill_limit(struct seq_file *sf, struct blkg_policy_data *pd,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001474 int off)
1475{
1476 struct throtl_grp *tg = pd_to_tg(pd);
1477 const char *dname = blkg_dev_name(pd->blkg);
1478 char bufs[4][21] = { "max", "max", "max", "max" };
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001479 u64 bps_dft;
1480 unsigned int iops_dft;
Shaohua Liada75b62017-03-27 10:51:42 -07001481 char idle_time[26] = "";
Shaohua Liec809912017-03-27 10:51:44 -07001482 char latency_time[26] = "";
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001483
1484 if (!dname)
1485 return 0;
Shaohua Li9f626e32017-03-27 10:51:30 -07001486
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001487 if (off == LIMIT_LOW) {
1488 bps_dft = 0;
1489 iops_dft = 0;
1490 } else {
1491 bps_dft = U64_MAX;
1492 iops_dft = UINT_MAX;
1493 }
1494
1495 if (tg->bps_conf[READ][off] == bps_dft &&
1496 tg->bps_conf[WRITE][off] == bps_dft &&
1497 tg->iops_conf[READ][off] == iops_dft &&
Shaohua Liada75b62017-03-27 10:51:42 -07001498 tg->iops_conf[WRITE][off] == iops_dft &&
Shaohua Liec809912017-03-27 10:51:44 -07001499 (off != LIMIT_LOW ||
1500 (tg->idletime_threshold == tg->td->dft_idletime_threshold &&
1501 tg->latency_target == DFL_LATENCY_TARGET)))
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001502 return 0;
1503
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001504 if (tg->bps_conf[READ][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001505 snprintf(bufs[0], sizeof(bufs[0]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001506 tg->bps_conf[READ][off]);
1507 if (tg->bps_conf[WRITE][off] != bps_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001508 snprintf(bufs[1], sizeof(bufs[1]), "%llu",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001509 tg->bps_conf[WRITE][off]);
1510 if (tg->iops_conf[READ][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001511 snprintf(bufs[2], sizeof(bufs[2]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001512 tg->iops_conf[READ][off]);
1513 if (tg->iops_conf[WRITE][off] != iops_dft)
Shaohua Li9f626e32017-03-27 10:51:30 -07001514 snprintf(bufs[3], sizeof(bufs[3]), "%u",
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001515 tg->iops_conf[WRITE][off]);
Shaohua Liada75b62017-03-27 10:51:42 -07001516 if (off == LIMIT_LOW) {
1517 if (tg->idletime_threshold == ULONG_MAX)
1518 strcpy(idle_time, " idle=max");
1519 else
1520 snprintf(idle_time, sizeof(idle_time), " idle=%lu",
1521 tg->idletime_threshold);
Shaohua Liec809912017-03-27 10:51:44 -07001522
1523 if (tg->latency_target == ULONG_MAX)
1524 strcpy(latency_time, " latency=max");
1525 else
1526 snprintf(latency_time, sizeof(latency_time),
1527 " latency=%lu", tg->latency_target);
Shaohua Liada75b62017-03-27 10:51:42 -07001528 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001529
Shaohua Liec809912017-03-27 10:51:44 -07001530 seq_printf(sf, "%s rbps=%s wbps=%s riops=%s wiops=%s%s%s\n",
1531 dname, bufs[0], bufs[1], bufs[2], bufs[3], idle_time,
1532 latency_time);
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001533 return 0;
1534}
1535
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001536static int tg_print_limit(struct seq_file *sf, void *v)
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001537{
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001538 blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_limit,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001539 &blkcg_policy_throtl, seq_cft(sf)->private, false);
1540 return 0;
1541}
1542
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001543static ssize_t tg_set_limit(struct kernfs_open_file *of,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001544 char *buf, size_t nbytes, loff_t off)
1545{
1546 struct blkcg *blkcg = css_to_blkcg(of_css(of));
1547 struct blkg_conf_ctx ctx;
1548 struct throtl_grp *tg;
1549 u64 v[4];
Shaohua Liada75b62017-03-27 10:51:42 -07001550 unsigned long idle_time;
Shaohua Liec809912017-03-27 10:51:44 -07001551 unsigned long latency_time;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001552 int ret;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001553 int index = of_cft(of)->private;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001554
1555 ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
1556 if (ret)
1557 return ret;
1558
1559 tg = blkg_to_tg(ctx.blkg);
1560
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001561 v[0] = tg->bps_conf[READ][index];
1562 v[1] = tg->bps_conf[WRITE][index];
1563 v[2] = tg->iops_conf[READ][index];
1564 v[3] = tg->iops_conf[WRITE][index];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001565
Shaohua Liada75b62017-03-27 10:51:42 -07001566 idle_time = tg->idletime_threshold;
Shaohua Liec809912017-03-27 10:51:44 -07001567 latency_time = tg->latency_target;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001568 while (true) {
1569 char tok[27]; /* wiops=18446744073709551616 */
1570 char *p;
Shaohua Li2ab54922017-03-27 10:51:29 -07001571 u64 val = U64_MAX;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001572 int len;
1573
1574 if (sscanf(ctx.body, "%26s%n", tok, &len) != 1)
1575 break;
1576 if (tok[0] == '\0')
1577 break;
1578 ctx.body += len;
1579
1580 ret = -EINVAL;
1581 p = tok;
1582 strsep(&p, "=");
1583 if (!p || (sscanf(p, "%llu", &val) != 1 && strcmp(p, "max")))
1584 goto out_finish;
1585
1586 ret = -ERANGE;
1587 if (!val)
1588 goto out_finish;
1589
1590 ret = -EINVAL;
1591 if (!strcmp(tok, "rbps"))
1592 v[0] = val;
1593 else if (!strcmp(tok, "wbps"))
1594 v[1] = val;
1595 else if (!strcmp(tok, "riops"))
1596 v[2] = min_t(u64, val, UINT_MAX);
1597 else if (!strcmp(tok, "wiops"))
1598 v[3] = min_t(u64, val, UINT_MAX);
Shaohua Liada75b62017-03-27 10:51:42 -07001599 else if (off == LIMIT_LOW && !strcmp(tok, "idle"))
1600 idle_time = val;
Shaohua Liec809912017-03-27 10:51:44 -07001601 else if (off == LIMIT_LOW && !strcmp(tok, "latency"))
1602 latency_time = val;
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001603 else
1604 goto out_finish;
1605 }
1606
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001607 tg->bps_conf[READ][index] = v[0];
1608 tg->bps_conf[WRITE][index] = v[1];
1609 tg->iops_conf[READ][index] = v[2];
1610 tg->iops_conf[WRITE][index] = v[3];
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001611
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001612 if (index == LIMIT_MAX) {
1613 tg->bps[READ][index] = v[0];
1614 tg->bps[WRITE][index] = v[1];
1615 tg->iops[READ][index] = v[2];
1616 tg->iops[WRITE][index] = v[3];
1617 }
1618 tg->bps[READ][LIMIT_LOW] = min(tg->bps_conf[READ][LIMIT_LOW],
1619 tg->bps_conf[READ][LIMIT_MAX]);
1620 tg->bps[WRITE][LIMIT_LOW] = min(tg->bps_conf[WRITE][LIMIT_LOW],
1621 tg->bps_conf[WRITE][LIMIT_MAX]);
1622 tg->iops[READ][LIMIT_LOW] = min(tg->iops_conf[READ][LIMIT_LOW],
1623 tg->iops_conf[READ][LIMIT_MAX]);
1624 tg->iops[WRITE][LIMIT_LOW] = min(tg->iops_conf[WRITE][LIMIT_LOW],
1625 tg->iops_conf[WRITE][LIMIT_MAX]);
1626
1627 if (index == LIMIT_LOW) {
1628 blk_throtl_update_limit_valid(tg->td);
1629 if (tg->td->limit_valid[LIMIT_LOW])
1630 tg->td->limit_index = LIMIT_LOW;
Shaohua Liada75b62017-03-27 10:51:42 -07001631 tg->idletime_threshold = (idle_time == ULONG_MAX) ?
1632 ULONG_MAX : idle_time;
Shaohua Liec809912017-03-27 10:51:44 -07001633 tg->latency_target = (latency_time == ULONG_MAX) ?
1634 ULONG_MAX : latency_time;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001635 }
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001636 tg_conf_updated(tg);
1637 ret = 0;
1638out_finish:
1639 blkg_conf_finish(&ctx);
1640 return ret ?: nbytes;
1641}
1642
1643static struct cftype throtl_files[] = {
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001644#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1645 {
1646 .name = "low",
1647 .flags = CFTYPE_NOT_ON_ROOT,
1648 .seq_show = tg_print_limit,
1649 .write = tg_set_limit,
1650 .private = LIMIT_LOW,
1651 },
1652#endif
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001653 {
1654 .name = "max",
1655 .flags = CFTYPE_NOT_ON_ROOT,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001656 .seq_show = tg_print_limit,
1657 .write = tg_set_limit,
1658 .private = LIMIT_MAX,
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001659 },
1660 { } /* terminate */
1661};
1662
Vivek Goyalda527772011-03-02 19:05:33 -05001663static void throtl_shutdown_wq(struct request_queue *q)
Vivek Goyale43473b2010-09-15 17:06:35 -04001664{
1665 struct throtl_data *td = q->td;
1666
Tejun Heo69df0ab2013-05-14 13:52:36 -07001667 cancel_work_sync(&td->dispatch_work);
Vivek Goyale43473b2010-09-15 17:06:35 -04001668}
1669
Tejun Heo3c798392012-04-16 13:57:25 -07001670static struct blkcg_policy blkcg_policy_throtl = {
Tejun Heo2ee867dc2015-08-18 14:55:34 -07001671 .dfl_cftypes = throtl_files,
Tejun Heo880f50e2015-08-18 14:55:30 -07001672 .legacy_cftypes = throtl_legacy_files,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001673
Tejun Heo001bea72015-08-18 14:55:11 -07001674 .pd_alloc_fn = throtl_pd_alloc,
Tejun Heof9fcc2d2012-04-16 13:57:27 -07001675 .pd_init_fn = throtl_pd_init,
Tejun Heo693e7512013-05-14 13:52:38 -07001676 .pd_online_fn = throtl_pd_online,
Shaohua Licd5ab1b2017-03-27 10:51:32 -07001677 .pd_offline_fn = throtl_pd_offline,
Tejun Heo001bea72015-08-18 14:55:11 -07001678 .pd_free_fn = throtl_pd_free,
Vivek Goyale43473b2010-09-15 17:06:35 -04001679};
1680
Shaohua Li3f0abd82017-03-27 10:51:35 -07001681static unsigned long __tg_last_low_overflow_time(struct throtl_grp *tg)
1682{
1683 unsigned long rtime = jiffies, wtime = jiffies;
1684
1685 if (tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW])
1686 rtime = tg->last_low_overflow_time[READ];
1687 if (tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW])
1688 wtime = tg->last_low_overflow_time[WRITE];
1689 return min(rtime, wtime);
1690}
1691
1692/* tg should not be an intermediate node */
1693static unsigned long tg_last_low_overflow_time(struct throtl_grp *tg)
1694{
1695 struct throtl_service_queue *parent_sq;
1696 struct throtl_grp *parent = tg;
1697 unsigned long ret = __tg_last_low_overflow_time(tg);
1698
1699 while (true) {
1700 parent_sq = parent->service_queue.parent_sq;
1701 parent = sq_to_tg(parent_sq);
1702 if (!parent)
1703 break;
1704
1705 /*
1706 * The parent doesn't have low limit, it always reaches low
1707 * limit. Its overflow time is useless for children
1708 */
1709 if (!parent->bps[READ][LIMIT_LOW] &&
1710 !parent->iops[READ][LIMIT_LOW] &&
1711 !parent->bps[WRITE][LIMIT_LOW] &&
1712 !parent->iops[WRITE][LIMIT_LOW])
1713 continue;
1714 if (time_after(__tg_last_low_overflow_time(parent), ret))
1715 ret = __tg_last_low_overflow_time(parent);
1716 }
1717 return ret;
1718}
1719
Shaohua Li9e234ee2017-03-27 10:51:41 -07001720static bool throtl_tg_is_idle(struct throtl_grp *tg)
1721{
1722 /*
1723 * cgroup is idle if:
1724 * - single idle is too long, longer than a fixed value (in case user
1725 * configure a too big threshold) or 4 times of slice
1726 * - average think time is more than threshold
Shaohua Li53696b82017-03-27 15:19:43 -07001727 * - IO latency is largely below threshold
Shaohua Li9e234ee2017-03-27 10:51:41 -07001728 */
1729 unsigned long time = jiffies_to_usecs(4 * tg->td->throtl_slice);
1730
1731 time = min_t(unsigned long, MAX_IDLE_TIME, time);
1732 return (ktime_get_ns() >> 10) - tg->last_finish_time > time ||
Shaohua Li53696b82017-03-27 15:19:43 -07001733 tg->avg_idletime > tg->idletime_threshold ||
1734 (tg->latency_target && tg->bio_cnt &&
1735 tg->bad_bio_cnt * 5 < tg->bio_cnt);
Shaohua Li9e234ee2017-03-27 10:51:41 -07001736}
1737
Shaohua Lic79892c2017-03-27 10:51:34 -07001738static bool throtl_tg_can_upgrade(struct throtl_grp *tg)
1739{
1740 struct throtl_service_queue *sq = &tg->service_queue;
1741 bool read_limit, write_limit;
1742
1743 /*
1744 * if cgroup reaches low limit (if low limit is 0, the cgroup always
1745 * reaches), it's ok to upgrade to next limit
1746 */
1747 read_limit = tg->bps[READ][LIMIT_LOW] || tg->iops[READ][LIMIT_LOW];
1748 write_limit = tg->bps[WRITE][LIMIT_LOW] || tg->iops[WRITE][LIMIT_LOW];
1749 if (!read_limit && !write_limit)
1750 return true;
1751 if (read_limit && sq->nr_queued[READ] &&
1752 (!write_limit || sq->nr_queued[WRITE]))
1753 return true;
1754 if (write_limit && sq->nr_queued[WRITE] &&
1755 (!read_limit || sq->nr_queued[READ]))
1756 return true;
Shaohua Liaec24242017-03-27 10:51:39 -07001757
1758 if (time_after_eq(jiffies,
Shaohua Lifa6fb5a2017-03-27 10:51:43 -07001759 tg_last_low_overflow_time(tg) + tg->td->throtl_slice) &&
1760 throtl_tg_is_idle(tg))
Shaohua Liaec24242017-03-27 10:51:39 -07001761 return true;
Shaohua Lic79892c2017-03-27 10:51:34 -07001762 return false;
1763}
1764
1765static bool throtl_hierarchy_can_upgrade(struct throtl_grp *tg)
1766{
1767 while (true) {
1768 if (throtl_tg_can_upgrade(tg))
1769 return true;
1770 tg = sq_to_tg(tg->service_queue.parent_sq);
1771 if (!tg || !tg_to_blkg(tg)->parent)
1772 return false;
1773 }
1774 return false;
1775}
1776
1777static bool throtl_can_upgrade(struct throtl_data *td,
1778 struct throtl_grp *this_tg)
1779{
1780 struct cgroup_subsys_state *pos_css;
1781 struct blkcg_gq *blkg;
1782
1783 if (td->limit_index != LIMIT_LOW)
1784 return false;
1785
Shaohua Li297e3d82017-03-27 10:51:37 -07001786 if (time_before(jiffies, td->low_downgrade_time + td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001787 return false;
1788
Shaohua Lic79892c2017-03-27 10:51:34 -07001789 rcu_read_lock();
1790 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1791 struct throtl_grp *tg = blkg_to_tg(blkg);
1792
1793 if (tg == this_tg)
1794 continue;
1795 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1796 continue;
1797 if (!throtl_hierarchy_can_upgrade(tg)) {
1798 rcu_read_unlock();
1799 return false;
1800 }
1801 }
1802 rcu_read_unlock();
1803 return true;
1804}
1805
Shaohua Lifa6fb5a2017-03-27 10:51:43 -07001806static void throtl_upgrade_check(struct throtl_grp *tg)
1807{
1808 unsigned long now = jiffies;
1809
1810 if (tg->td->limit_index != LIMIT_LOW)
1811 return;
1812
1813 if (time_after(tg->last_check_time + tg->td->throtl_slice, now))
1814 return;
1815
1816 tg->last_check_time = now;
1817
1818 if (!time_after_eq(now,
1819 __tg_last_low_overflow_time(tg) + tg->td->throtl_slice))
1820 return;
1821
1822 if (throtl_can_upgrade(tg->td, NULL))
1823 throtl_upgrade_state(tg->td);
1824}
1825
Shaohua Lic79892c2017-03-27 10:51:34 -07001826static void throtl_upgrade_state(struct throtl_data *td)
1827{
1828 struct cgroup_subsys_state *pos_css;
1829 struct blkcg_gq *blkg;
1830
1831 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07001832 td->low_upgrade_time = jiffies;
Shaohua Li7394e312017-03-27 10:51:40 -07001833 td->scale = 0;
Shaohua Lic79892c2017-03-27 10:51:34 -07001834 rcu_read_lock();
1835 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg) {
1836 struct throtl_grp *tg = blkg_to_tg(blkg);
1837 struct throtl_service_queue *sq = &tg->service_queue;
1838
1839 tg->disptime = jiffies - 1;
1840 throtl_select_dispatch(sq);
1841 throtl_schedule_next_dispatch(sq, false);
1842 }
1843 rcu_read_unlock();
1844 throtl_select_dispatch(&td->service_queue);
1845 throtl_schedule_next_dispatch(&td->service_queue, false);
1846 queue_work(kthrotld_workqueue, &td->dispatch_work);
1847}
1848
Shaohua Li3f0abd82017-03-27 10:51:35 -07001849static void throtl_downgrade_state(struct throtl_data *td, int new)
1850{
Shaohua Li7394e312017-03-27 10:51:40 -07001851 td->scale /= 2;
1852
1853 if (td->scale) {
1854 td->low_upgrade_time = jiffies - td->scale * td->throtl_slice;
1855 return;
1856 }
1857
Shaohua Li3f0abd82017-03-27 10:51:35 -07001858 td->limit_index = new;
1859 td->low_downgrade_time = jiffies;
1860}
1861
1862static bool throtl_tg_can_downgrade(struct throtl_grp *tg)
1863{
1864 struct throtl_data *td = tg->td;
1865 unsigned long now = jiffies;
1866
1867 /*
1868 * If cgroup is below low limit, consider downgrade and throttle other
1869 * cgroups
1870 */
Shaohua Li297e3d82017-03-27 10:51:37 -07001871 if (time_after_eq(now, td->low_upgrade_time + td->throtl_slice) &&
1872 time_after_eq(now, tg_last_low_overflow_time(tg) +
Shaohua Lifa6fb5a2017-03-27 10:51:43 -07001873 td->throtl_slice) &&
1874 (!throtl_tg_is_idle(tg) ||
1875 !list_empty(&tg_to_blkg(tg)->blkcg->css.children)))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001876 return true;
1877 return false;
1878}
1879
1880static bool throtl_hierarchy_can_downgrade(struct throtl_grp *tg)
1881{
1882 while (true) {
1883 if (!throtl_tg_can_downgrade(tg))
1884 return false;
1885 tg = sq_to_tg(tg->service_queue.parent_sq);
1886 if (!tg || !tg_to_blkg(tg)->parent)
1887 break;
1888 }
1889 return true;
1890}
1891
1892static void throtl_downgrade_check(struct throtl_grp *tg)
1893{
1894 uint64_t bps;
1895 unsigned int iops;
1896 unsigned long elapsed_time;
1897 unsigned long now = jiffies;
1898
1899 if (tg->td->limit_index != LIMIT_MAX ||
1900 !tg->td->limit_valid[LIMIT_LOW])
1901 return;
1902 if (!list_empty(&tg_to_blkg(tg)->blkcg->css.children))
1903 return;
Shaohua Li297e3d82017-03-27 10:51:37 -07001904 if (time_after(tg->last_check_time + tg->td->throtl_slice, now))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001905 return;
1906
1907 elapsed_time = now - tg->last_check_time;
1908 tg->last_check_time = now;
1909
Shaohua Li297e3d82017-03-27 10:51:37 -07001910 if (time_before(now, tg_last_low_overflow_time(tg) +
1911 tg->td->throtl_slice))
Shaohua Li3f0abd82017-03-27 10:51:35 -07001912 return;
1913
1914 if (tg->bps[READ][LIMIT_LOW]) {
1915 bps = tg->last_bytes_disp[READ] * HZ;
1916 do_div(bps, elapsed_time);
1917 if (bps >= tg->bps[READ][LIMIT_LOW])
1918 tg->last_low_overflow_time[READ] = now;
1919 }
1920
1921 if (tg->bps[WRITE][LIMIT_LOW]) {
1922 bps = tg->last_bytes_disp[WRITE] * HZ;
1923 do_div(bps, elapsed_time);
1924 if (bps >= tg->bps[WRITE][LIMIT_LOW])
1925 tg->last_low_overflow_time[WRITE] = now;
1926 }
1927
1928 if (tg->iops[READ][LIMIT_LOW]) {
1929 iops = tg->last_io_disp[READ] * HZ / elapsed_time;
1930 if (iops >= tg->iops[READ][LIMIT_LOW])
1931 tg->last_low_overflow_time[READ] = now;
1932 }
1933
1934 if (tg->iops[WRITE][LIMIT_LOW]) {
1935 iops = tg->last_io_disp[WRITE] * HZ / elapsed_time;
1936 if (iops >= tg->iops[WRITE][LIMIT_LOW])
1937 tg->last_low_overflow_time[WRITE] = now;
1938 }
1939
1940 /*
1941 * If cgroup is below low limit, consider downgrade and throttle other
1942 * cgroups
1943 */
1944 if (throtl_hierarchy_can_downgrade(tg))
1945 throtl_downgrade_state(tg->td, LIMIT_LOW);
1946
1947 tg->last_bytes_disp[READ] = 0;
1948 tg->last_bytes_disp[WRITE] = 0;
1949 tg->last_io_disp[READ] = 0;
1950 tg->last_io_disp[WRITE] = 0;
1951}
1952
Shaohua Li9e234ee2017-03-27 10:51:41 -07001953static void blk_throtl_update_idletime(struct throtl_grp *tg)
1954{
1955 unsigned long now = ktime_get_ns() >> 10;
1956 unsigned long last_finish_time = tg->last_finish_time;
1957
1958 if (now <= last_finish_time || last_finish_time == 0 ||
1959 last_finish_time == tg->checked_last_finish_time)
1960 return;
1961
1962 tg->avg_idletime = (tg->avg_idletime * 7 + now - last_finish_time) >> 3;
1963 tg->checked_last_finish_time = last_finish_time;
1964}
1965
Shaohua Lib9147dd2017-03-27 15:19:42 -07001966#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
1967static void throtl_update_latency_buckets(struct throtl_data *td)
1968{
1969 struct avg_latency_bucket avg_latency[LATENCY_BUCKET_SIZE];
1970 int i, cpu;
1971 unsigned long last_latency = 0;
1972 unsigned long latency;
1973
1974 if (!blk_queue_nonrot(td->queue))
1975 return;
1976 if (time_before(jiffies, td->last_calculate_time + HZ))
1977 return;
1978 td->last_calculate_time = jiffies;
1979
1980 memset(avg_latency, 0, sizeof(avg_latency));
1981 for (i = 0; i < LATENCY_BUCKET_SIZE; i++) {
1982 struct latency_bucket *tmp = &td->tmp_buckets[i];
1983
1984 for_each_possible_cpu(cpu) {
1985 struct latency_bucket *bucket;
1986
1987 /* this isn't race free, but ok in practice */
1988 bucket = per_cpu_ptr(td->latency_buckets, cpu);
1989 tmp->total_latency += bucket[i].total_latency;
1990 tmp->samples += bucket[i].samples;
1991 bucket[i].total_latency = 0;
1992 bucket[i].samples = 0;
1993 }
1994
1995 if (tmp->samples >= 32) {
1996 int samples = tmp->samples;
1997
1998 latency = tmp->total_latency;
1999
2000 tmp->total_latency = 0;
2001 tmp->samples = 0;
2002 latency /= samples;
2003 if (latency == 0)
2004 continue;
2005 avg_latency[i].latency = latency;
2006 }
2007 }
2008
2009 for (i = 0; i < LATENCY_BUCKET_SIZE; i++) {
2010 if (!avg_latency[i].latency) {
2011 if (td->avg_buckets[i].latency < last_latency)
2012 td->avg_buckets[i].latency = last_latency;
2013 continue;
2014 }
2015
2016 if (!td->avg_buckets[i].valid)
2017 latency = avg_latency[i].latency;
2018 else
2019 latency = (td->avg_buckets[i].latency * 7 +
2020 avg_latency[i].latency) >> 3;
2021
2022 td->avg_buckets[i].latency = max(latency, last_latency);
2023 td->avg_buckets[i].valid = true;
2024 last_latency = td->avg_buckets[i].latency;
2025 }
2026}
2027#else
2028static inline void throtl_update_latency_buckets(struct throtl_data *td)
2029{
2030}
2031#endif
2032
Tejun Heoae118892015-08-18 14:55:20 -07002033bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
2034 struct bio *bio)
Vivek Goyale43473b2010-09-15 17:06:35 -04002035{
Tejun Heoc5cc2072013-05-14 13:52:38 -07002036 struct throtl_qnode *qn = NULL;
Tejun Heoae118892015-08-18 14:55:20 -07002037 struct throtl_grp *tg = blkg_to_tg(blkg ?: q->root_blkg);
Tejun Heo73f0d492013-05-14 13:52:35 -07002038 struct throtl_service_queue *sq;
Tejun Heo0e9f4162013-05-14 13:52:35 -07002039 bool rw = bio_data_dir(bio);
Tejun Heobc16a4f2011-10-19 14:33:01 +02002040 bool throttled = false;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002041 struct throtl_data *td = tg->td;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002042 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04002043
Tejun Heoae118892015-08-18 14:55:20 -07002044 WARN_ON_ONCE(!rcu_read_lock_held());
2045
Tejun Heo2a0f61e2013-05-14 13:52:36 -07002046 /* see throtl_charge_bio() */
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02002047 if (bio_flagged(bio, BIO_THROTTLED) || !tg->has_rules[rw])
Tejun Heobc16a4f2011-10-19 14:33:01 +02002048 goto out;
Vivek Goyale43473b2010-09-15 17:06:35 -04002049
2050 spin_lock_irq(q->queue_lock);
Tejun Heoc9589f02015-08-18 14:55:19 -07002051
Shaohua Lib9147dd2017-03-27 15:19:42 -07002052 throtl_update_latency_buckets(td);
2053
Tejun Heoc9589f02015-08-18 14:55:19 -07002054 if (unlikely(blk_queue_bypass(q)))
Tejun Heobc16a4f2011-10-19 14:33:01 +02002055 goto out_unlock;
Vivek Goyalf469a7b2011-05-19 15:38:23 -04002056
Shaohua Li9e234ee2017-03-27 10:51:41 -07002057 ret = bio_associate_current(bio);
2058#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2059 if (ret == 0 || ret == -EBUSY)
2060 bio->bi_cg_private = tg;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002061 blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
Shaohua Li9e234ee2017-03-27 10:51:41 -07002062#endif
2063 blk_throtl_update_idletime(tg);
2064
Tejun Heo73f0d492013-05-14 13:52:35 -07002065 sq = &tg->service_queue;
2066
Shaohua Lic79892c2017-03-27 10:51:34 -07002067again:
Tejun Heo9e660ac2013-05-14 13:52:38 -07002068 while (true) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07002069 if (tg->last_low_overflow_time[rw] == 0)
2070 tg->last_low_overflow_time[rw] = jiffies;
2071 throtl_downgrade_check(tg);
Shaohua Lifa6fb5a2017-03-27 10:51:43 -07002072 throtl_upgrade_check(tg);
Tejun Heo9e660ac2013-05-14 13:52:38 -07002073 /* throtl is FIFO - if bios are already queued, should queue */
2074 if (sq->nr_queued[rw])
2075 break;
Vivek Goyalde701c72011-03-07 21:09:32 +01002076
Tejun Heo9e660ac2013-05-14 13:52:38 -07002077 /* if above limits, break to queue */
Shaohua Lic79892c2017-03-27 10:51:34 -07002078 if (!tg_may_dispatch(tg, bio, NULL)) {
Shaohua Li3f0abd82017-03-27 10:51:35 -07002079 tg->last_low_overflow_time[rw] = jiffies;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002080 if (throtl_can_upgrade(td, tg)) {
2081 throtl_upgrade_state(td);
Shaohua Lic79892c2017-03-27 10:51:34 -07002082 goto again;
2083 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07002084 break;
Shaohua Lic79892c2017-03-27 10:51:34 -07002085 }
Tejun Heo9e660ac2013-05-14 13:52:38 -07002086
2087 /* within limits, let's charge and dispatch directly */
Vivek Goyale43473b2010-09-15 17:06:35 -04002088 throtl_charge_bio(tg, bio);
Vivek Goyal04521db2011-03-22 21:54:29 +01002089
2090 /*
2091 * We need to trim slice even when bios are not being queued
2092 * otherwise it might happen that a bio is not queued for
2093 * a long time and slice keeps on extending and trim is not
2094 * called for a long time. Now if limits are reduced suddenly
2095 * we take into account all the IO dispatched so far at new
2096 * low rate and * newly queued IO gets a really long dispatch
2097 * time.
2098 *
2099 * So keep on trimming slice even if bio is not queued.
2100 */
Tejun Heo0f3457f2013-05-14 13:52:32 -07002101 throtl_trim_slice(tg, rw);
Tejun Heo9e660ac2013-05-14 13:52:38 -07002102
2103 /*
2104 * @bio passed through this layer without being throttled.
2105 * Climb up the ladder. If we''re already at the top, it
2106 * can be executed directly.
2107 */
Tejun Heoc5cc2072013-05-14 13:52:38 -07002108 qn = &tg->qnode_on_parent[rw];
Tejun Heo9e660ac2013-05-14 13:52:38 -07002109 sq = sq->parent_sq;
2110 tg = sq_to_tg(sq);
2111 if (!tg)
2112 goto out_unlock;
Vivek Goyale43473b2010-09-15 17:06:35 -04002113 }
2114
Tejun Heo9e660ac2013-05-14 13:52:38 -07002115 /* out-of-limit, queue to @tg */
Tejun Heofda6f272013-05-14 13:52:36 -07002116 throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
2117 rw == READ ? 'R' : 'W',
Shaohua Li9f626e32017-03-27 10:51:30 -07002118 tg->bytes_disp[rw], bio->bi_iter.bi_size,
2119 tg_bps_limit(tg, rw),
2120 tg->io_disp[rw], tg_iops_limit(tg, rw),
Tejun Heofda6f272013-05-14 13:52:36 -07002121 sq->nr_queued[READ], sq->nr_queued[WRITE]);
Vivek Goyale43473b2010-09-15 17:06:35 -04002122
Shaohua Li3f0abd82017-03-27 10:51:35 -07002123 tg->last_low_overflow_time[rw] = jiffies;
2124
Shaohua Lib9147dd2017-03-27 15:19:42 -07002125 td->nr_queued[rw]++;
Tejun Heoc5cc2072013-05-14 13:52:38 -07002126 throtl_add_bio_tg(bio, qn, tg);
Tejun Heobc16a4f2011-10-19 14:33:01 +02002127 throttled = true;
Vivek Goyale43473b2010-09-15 17:06:35 -04002128
Tejun Heo7f52f982013-05-14 13:52:37 -07002129 /*
2130 * Update @tg's dispatch time and force schedule dispatch if @tg
2131 * was empty before @bio. The forced scheduling isn't likely to
2132 * cause undue delay as @bio is likely to be dispatched directly if
2133 * its @tg's disptime is not in the future.
2134 */
Tejun Heo0e9f4162013-05-14 13:52:35 -07002135 if (tg->flags & THROTL_TG_WAS_EMPTY) {
Tejun Heo77216b02013-05-14 13:52:36 -07002136 tg_update_disptime(tg);
Tejun Heo7f52f982013-05-14 13:52:37 -07002137 throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
Vivek Goyale43473b2010-09-15 17:06:35 -04002138 }
2139
Tejun Heobc16a4f2011-10-19 14:33:01 +02002140out_unlock:
Vivek Goyale43473b2010-09-15 17:06:35 -04002141 spin_unlock_irq(q->queue_lock);
Tejun Heobc16a4f2011-10-19 14:33:01 +02002142out:
Tejun Heo2a0f61e2013-05-14 13:52:36 -07002143 /*
2144 * As multiple blk-throtls may stack in the same issue path, we
2145 * don't want bios to leave with the flag set. Clear the flag if
2146 * being issued.
2147 */
2148 if (!throttled)
Christoph Hellwig8d2bbd42016-10-20 15:12:12 +02002149 bio_clear_flag(bio, BIO_THROTTLED);
Shaohua Lib9147dd2017-03-27 15:19:42 -07002150
2151#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2152 if (throttled || !td->track_bio_latency)
2153 bio->bi_issue_stat.stat |= SKIP_LATENCY;
2154#endif
Tejun Heobc16a4f2011-10-19 14:33:01 +02002155 return throttled;
Vivek Goyale43473b2010-09-15 17:06:35 -04002156}
2157
Shaohua Li9e234ee2017-03-27 10:51:41 -07002158#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
Shaohua Lib9147dd2017-03-27 15:19:42 -07002159static void throtl_track_latency(struct throtl_data *td, sector_t size,
2160 int op, unsigned long time)
2161{
2162 struct latency_bucket *latency;
2163 int index;
2164
2165 if (!td || td->limit_index != LIMIT_LOW || op != REQ_OP_READ ||
2166 !blk_queue_nonrot(td->queue))
2167 return;
2168
2169 index = request_bucket_index(size);
2170
2171 latency = get_cpu_ptr(td->latency_buckets);
2172 latency[index].total_latency += time;
2173 latency[index].samples++;
2174 put_cpu_ptr(td->latency_buckets);
2175}
2176
2177void blk_throtl_stat_add(struct request *rq, u64 time_ns)
2178{
2179 struct request_queue *q = rq->q;
2180 struct throtl_data *td = q->td;
2181
2182 throtl_track_latency(td, blk_stat_size(&rq->issue_stat),
2183 req_op(rq), time_ns >> 10);
2184}
2185
Shaohua Li9e234ee2017-03-27 10:51:41 -07002186void blk_throtl_bio_endio(struct bio *bio)
2187{
2188 struct throtl_grp *tg;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002189 u64 finish_time_ns;
2190 unsigned long finish_time;
2191 unsigned long start_time;
2192 unsigned long lat;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002193
2194 tg = bio->bi_cg_private;
2195 if (!tg)
2196 return;
2197 bio->bi_cg_private = NULL;
2198
Shaohua Lib9147dd2017-03-27 15:19:42 -07002199 finish_time_ns = ktime_get_ns();
2200 tg->last_finish_time = finish_time_ns >> 10;
2201
2202 start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
2203 finish_time = __blk_stat_time(finish_time_ns) >> 10;
Shaohua Li53696b82017-03-27 15:19:43 -07002204 if (!start_time || finish_time <= start_time)
2205 return;
2206
2207 lat = finish_time - start_time;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002208 /* this is only for bio based driver */
Shaohua Li53696b82017-03-27 15:19:43 -07002209 if (!(bio->bi_issue_stat.stat & SKIP_LATENCY))
Shaohua Lib9147dd2017-03-27 15:19:42 -07002210 throtl_track_latency(tg->td, blk_stat_size(&bio->bi_issue_stat),
2211 bio_op(bio), lat);
Shaohua Li53696b82017-03-27 15:19:43 -07002212
2213 if (tg->latency_target) {
2214 int bucket;
2215 unsigned int threshold;
2216
2217 bucket = request_bucket_index(
2218 blk_stat_size(&bio->bi_issue_stat));
2219 threshold = tg->td->avg_buckets[bucket].latency +
2220 tg->latency_target;
2221 if (lat > threshold)
2222 tg->bad_bio_cnt++;
2223 /*
2224 * Not race free, could get wrong count, which means cgroups
2225 * will be throttled
2226 */
2227 tg->bio_cnt++;
2228 }
2229
2230 if (time_after(jiffies, tg->bio_cnt_reset_time) || tg->bio_cnt > 1024) {
2231 tg->bio_cnt_reset_time = tg->td->throtl_slice + jiffies;
2232 tg->bio_cnt /= 2;
2233 tg->bad_bio_cnt /= 2;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002234 }
Shaohua Li9e234ee2017-03-27 10:51:41 -07002235}
2236#endif
2237
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002238/*
2239 * Dispatch all bios from all children tg's queued on @parent_sq. On
2240 * return, @parent_sq is guaranteed to not have any active children tg's
2241 * and all bios from previously active tg's are on @parent_sq->bio_lists[].
2242 */
2243static void tg_drain_bios(struct throtl_service_queue *parent_sq)
2244{
2245 struct throtl_grp *tg;
2246
2247 while ((tg = throtl_rb_first(parent_sq))) {
2248 struct throtl_service_queue *sq = &tg->service_queue;
2249 struct bio *bio;
2250
2251 throtl_dequeue_tg(tg);
2252
Tejun Heoc5cc2072013-05-14 13:52:38 -07002253 while ((bio = throtl_peek_queued(&sq->queued[READ])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002254 tg_dispatch_one_bio(tg, bio_data_dir(bio));
Tejun Heoc5cc2072013-05-14 13:52:38 -07002255 while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002256 tg_dispatch_one_bio(tg, bio_data_dir(bio));
2257 }
2258}
2259
Tejun Heoc9a929d2011-10-19 14:42:16 +02002260/**
2261 * blk_throtl_drain - drain throttled bios
2262 * @q: request_queue to drain throttled bios for
2263 *
2264 * Dispatch all currently throttled bios on @q through ->make_request_fn().
2265 */
2266void blk_throtl_drain(struct request_queue *q)
2267 __releases(q->queue_lock) __acquires(q->queue_lock)
2268{
2269 struct throtl_data *td = q->td;
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002270 struct blkcg_gq *blkg;
Tejun Heo492eb212013-08-08 20:11:25 -04002271 struct cgroup_subsys_state *pos_css;
Tejun Heoc9a929d2011-10-19 14:42:16 +02002272 struct bio *bio;
Tejun Heo651930b2013-05-14 13:52:35 -07002273 int rw;
Tejun Heoc9a929d2011-10-19 14:42:16 +02002274
Andi Kleen8bcb6c72012-03-30 12:33:28 +02002275 queue_lockdep_assert_held(q);
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002276 rcu_read_lock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02002277
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002278 /*
2279 * Drain each tg while doing post-order walk on the blkg tree, so
2280 * that all bios are propagated to td->service_queue. It'd be
2281 * better to walk service_queue tree directly but blkg walk is
2282 * easier.
2283 */
Tejun Heo492eb212013-08-08 20:11:25 -04002284 blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002285 tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
Tejun Heo73f0d492013-05-14 13:52:35 -07002286
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002287 /* finally, transfer bios from top-level tg's into the td */
2288 tg_drain_bios(&td->service_queue);
2289
2290 rcu_read_unlock();
Tejun Heoc9a929d2011-10-19 14:42:16 +02002291 spin_unlock_irq(q->queue_lock);
2292
Tejun Heo2a12f0d2013-05-14 13:52:37 -07002293 /* all bios now should be in td->service_queue, issue them */
Tejun Heo651930b2013-05-14 13:52:35 -07002294 for (rw = READ; rw <= WRITE; rw++)
Tejun Heoc5cc2072013-05-14 13:52:38 -07002295 while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
2296 NULL)))
Tejun Heo651930b2013-05-14 13:52:35 -07002297 generic_make_request(bio);
Tejun Heoc9a929d2011-10-19 14:42:16 +02002298
2299 spin_lock_irq(q->queue_lock);
2300}
2301
Vivek Goyale43473b2010-09-15 17:06:35 -04002302int blk_throtl_init(struct request_queue *q)
2303{
2304 struct throtl_data *td;
Tejun Heoa2b16932012-04-13 13:11:33 -07002305 int ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04002306
2307 td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
2308 if (!td)
2309 return -ENOMEM;
Shaohua Lib9147dd2017-03-27 15:19:42 -07002310 td->latency_buckets = __alloc_percpu(sizeof(struct latency_bucket) *
2311 LATENCY_BUCKET_SIZE, __alignof__(u64));
2312 if (!td->latency_buckets) {
2313 kfree(td);
2314 return -ENOMEM;
2315 }
Vivek Goyale43473b2010-09-15 17:06:35 -04002316
Tejun Heo69df0ab2013-05-14 13:52:36 -07002317 INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
Tejun Heob2ce2642015-08-18 14:55:13 -07002318 throtl_service_queue_init(&td->service_queue);
Vivek Goyale43473b2010-09-15 17:06:35 -04002319
Tejun Heocd1604f2012-03-05 13:15:06 -08002320 q->td = td;
Vivek Goyal29b12582011-05-19 15:38:24 -04002321 td->queue = q;
Vivek Goyal02977e42010-10-01 14:49:48 +02002322
Shaohua Li9f626e32017-03-27 10:51:30 -07002323 td->limit_valid[LIMIT_MAX] = true;
Shaohua Licd5ab1b2017-03-27 10:51:32 -07002324 td->limit_index = LIMIT_MAX;
Shaohua Li3f0abd82017-03-27 10:51:35 -07002325 td->low_upgrade_time = jiffies;
2326 td->low_downgrade_time = jiffies;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002327
Tejun Heoa2b16932012-04-13 13:11:33 -07002328 /* activate policy */
Tejun Heo3c798392012-04-16 13:57:25 -07002329 ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
Shaohua Lib9147dd2017-03-27 15:19:42 -07002330 if (ret) {
2331 free_percpu(td->latency_buckets);
Vivek Goyal29b12582011-05-19 15:38:24 -04002332 kfree(td);
Shaohua Lib9147dd2017-03-27 15:19:42 -07002333 }
Tejun Heoa2b16932012-04-13 13:11:33 -07002334 return ret;
Vivek Goyale43473b2010-09-15 17:06:35 -04002335}
2336
2337void blk_throtl_exit(struct request_queue *q)
2338{
Tejun Heoc875f4d2012-03-05 13:15:22 -08002339 BUG_ON(!q->td);
Vivek Goyalda527772011-03-02 19:05:33 -05002340 throtl_shutdown_wq(q);
Tejun Heo3c798392012-04-16 13:57:25 -07002341 blkcg_deactivate_policy(q, &blkcg_policy_throtl);
Shaohua Lib9147dd2017-03-27 15:19:42 -07002342 free_percpu(q->td->latency_buckets);
Tejun Heoc9a929d2011-10-19 14:42:16 +02002343 kfree(q->td);
Vivek Goyale43473b2010-09-15 17:06:35 -04002344}
2345
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002346void blk_throtl_register_queue(struct request_queue *q)
2347{
2348 struct throtl_data *td;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002349 struct cgroup_subsys_state *pos_css;
2350 struct blkcg_gq *blkg;
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002351
2352 td = q->td;
2353 BUG_ON(!td);
2354
Shaohua Liada75b62017-03-27 10:51:42 -07002355 if (blk_queue_nonrot(q)) {
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002356 td->throtl_slice = DFL_THROTL_SLICE_SSD;
Shaohua Liada75b62017-03-27 10:51:42 -07002357 td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_SSD;
2358 } else {
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002359 td->throtl_slice = DFL_THROTL_SLICE_HD;
Shaohua Liada75b62017-03-27 10:51:42 -07002360 td->dft_idletime_threshold = DFL_IDLE_THRESHOLD_HD;
2361 }
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002362#ifndef CONFIG_BLK_DEV_THROTTLING_LOW
2363 /* if no low limit, use previous default */
2364 td->throtl_slice = DFL_THROTL_SLICE_HD;
2365#endif
Shaohua Li9e234ee2017-03-27 10:51:41 -07002366
Shaohua Lib9147dd2017-03-27 15:19:42 -07002367 td->track_bio_latency = !q->mq_ops && !q->request_fn;
2368 if (!td->track_bio_latency)
2369 blk_stat_enable_accounting(q);
2370
Shaohua Li9e234ee2017-03-27 10:51:41 -07002371 /*
2372 * some tg are created before queue is fully initialized, eg, nonrot
2373 * isn't initialized yet
2374 */
2375 rcu_read_lock();
2376 blkg_for_each_descendant_post(blkg, pos_css, q->root_blkg) {
2377 struct throtl_grp *tg = blkg_to_tg(blkg);
2378
Shaohua Liada75b62017-03-27 10:51:42 -07002379 tg->idletime_threshold = td->dft_idletime_threshold;
Shaohua Li9e234ee2017-03-27 10:51:41 -07002380 }
2381 rcu_read_unlock();
Shaohua Lid61fcfa2017-03-27 10:51:38 -07002382}
2383
Shaohua Li297e3d82017-03-27 10:51:37 -07002384#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2385ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page)
2386{
2387 if (!q->td)
2388 return -EINVAL;
2389 return sprintf(page, "%u\n", jiffies_to_msecs(q->td->throtl_slice));
2390}
2391
2392ssize_t blk_throtl_sample_time_store(struct request_queue *q,
2393 const char *page, size_t count)
2394{
2395 unsigned long v;
2396 unsigned long t;
2397
2398 if (!q->td)
2399 return -EINVAL;
2400 if (kstrtoul(page, 10, &v))
2401 return -EINVAL;
2402 t = msecs_to_jiffies(v);
2403 if (t == 0 || t > MAX_THROTL_SLICE)
2404 return -EINVAL;
2405 q->td->throtl_slice = t;
2406 return count;
2407}
2408#endif
2409
Vivek Goyale43473b2010-09-15 17:06:35 -04002410static int __init throtl_init(void)
2411{
Vivek Goyal450adcb2011-03-01 13:40:54 -05002412 kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
2413 if (!kthrotld_workqueue)
2414 panic("Failed to create kthrotld\n");
2415
Tejun Heo3c798392012-04-16 13:57:25 -07002416 return blkcg_policy_register(&blkcg_policy_throtl);
Vivek Goyale43473b2010-09-15 17:06:35 -04002417}
2418
2419module_init(throtl_init);