xref: /openbmc/linux/block/blk-iolatency.c (revision ff4cee08)
1d7067512SJosef Bacik /*
2d7067512SJosef Bacik  * Block rq-qos base io controller
3d7067512SJosef Bacik  *
4d7067512SJosef Bacik  * This works similar to wbt with a few exceptions
5d7067512SJosef Bacik  *
6d7067512SJosef Bacik  * - It's bio based, so the latency covers the whole block layer in addition to
7d7067512SJosef Bacik  *   the actual io.
8d7067512SJosef Bacik  * - We will throttle all IO that comes in here if we need to.
9d7067512SJosef Bacik  * - We use the mean latency over the 100ms window.  This is because writes can
10d7067512SJosef Bacik  *   be particularly fast, which could give us a false sense of the impact of
11d7067512SJosef Bacik  *   other workloads on our protected workload.
12a284390bSJosef Bacik  * - By default there's no throttling, we set the queue_depth to UINT_MAX so
13a284390bSJosef Bacik  *   that we can have as many outstanding bio's as we're allowed to.  Only at
14d7067512SJosef Bacik  *   throttle time do we pay attention to the actual queue depth.
15d7067512SJosef Bacik  *
16d7067512SJosef Bacik  * The hierarchy works like the cpu controller does, we track the latency at
17d7067512SJosef Bacik  * every configured node, and each configured node has it's own independent
18d7067512SJosef Bacik  * queue depth.  This means that we only care about our latency targets at the
19d7067512SJosef Bacik  * peer level.  Some group at the bottom of the hierarchy isn't going to affect
20d7067512SJosef Bacik  * a group at the end of some other path if we're only configred at leaf level.
21d7067512SJosef Bacik  *
22d7067512SJosef Bacik  * Consider the following
23d7067512SJosef Bacik  *
24d7067512SJosef Bacik  *                   root blkg
25d7067512SJosef Bacik  *             /                     \
26d7067512SJosef Bacik  *        fast (target=5ms)     slow (target=10ms)
27d7067512SJosef Bacik  *         /     \                  /        \
28d7067512SJosef Bacik  *       a        b          normal(15ms)   unloved
29d7067512SJosef Bacik  *
30d7067512SJosef Bacik  * "a" and "b" have no target, but their combined io under "fast" cannot exceed
31d7067512SJosef Bacik  * an average latency of 5ms.  If it does then we will throttle the "slow"
32d7067512SJosef Bacik  * group.  In the case of "normal", if it exceeds its 15ms target, we will
33d7067512SJosef Bacik  * throttle "unloved", but nobody else.
34d7067512SJosef Bacik  *
35d7067512SJosef Bacik  * In this example "fast", "slow", and "normal" will be the only groups actually
36d7067512SJosef Bacik  * accounting their io latencies.  We have to walk up the heirarchy to the root
37d7067512SJosef Bacik  * on every submit and complete so we can do the appropriate stat recording and
38d7067512SJosef Bacik  * adjust the queue depth of ourselves if needed.
39d7067512SJosef Bacik  *
40d7067512SJosef Bacik  * There are 2 ways we throttle IO.
41d7067512SJosef Bacik  *
42d7067512SJosef Bacik  * 1) Queue depth throttling.  As we throttle down we will adjust the maximum
43d7067512SJosef Bacik  * number of IO's we're allowed to have in flight.  This starts at (u64)-1 down
44d7067512SJosef Bacik  * to 1.  If the group is only ever submitting IO for itself then this is the
45d7067512SJosef Bacik  * only way we throttle.
46d7067512SJosef Bacik  *
47d7067512SJosef Bacik  * 2) Induced delay throttling.  This is for the case that a group is generating
48d7067512SJosef Bacik  * IO that has to be issued by the root cg to avoid priority inversion. So think
49d7067512SJosef Bacik  * REQ_META or REQ_SWAP.  If we are already at qd == 1 and we're getting a lot
50d7067512SJosef Bacik  * of work done for us on behalf of the root cg and are being asked to scale
51d7067512SJosef Bacik  * down more then we induce a latency at userspace return.  We accumulate the
52d7067512SJosef Bacik  * total amount of time we need to be punished by doing
53d7067512SJosef Bacik  *
54d7067512SJosef Bacik  * total_time += min_lat_nsec - actual_io_completion
55d7067512SJosef Bacik  *
56d7067512SJosef Bacik  * and then at throttle time will do
57d7067512SJosef Bacik  *
58d7067512SJosef Bacik  * throttle_time = min(total_time, NSEC_PER_SEC)
59d7067512SJosef Bacik  *
60d7067512SJosef Bacik  * This induced delay will throttle back the activity that is generating the
61d7067512SJosef Bacik  * root cg issued io's, wethere that's some metadata intensive operation or the
62d7067512SJosef Bacik  * group is using so much memory that it is pushing us into swap.
63d7067512SJosef Bacik  *
64d7067512SJosef Bacik  * Copyright (C) 2018 Josef Bacik
65d7067512SJosef Bacik  */
66d7067512SJosef Bacik #include <linux/kernel.h>
67d7067512SJosef Bacik #include <linux/blk_types.h>
68d7067512SJosef Bacik #include <linux/backing-dev.h>
69d7067512SJosef Bacik #include <linux/module.h>
70d7067512SJosef Bacik #include <linux/timer.h>
71d7067512SJosef Bacik #include <linux/memcontrol.h>
72c480bcf9SDennis Zhou (Facebook) #include <linux/sched/loadavg.h>
73d7067512SJosef Bacik #include <linux/sched/signal.h>
74d7067512SJosef Bacik #include <trace/events/block.h>
75d7067512SJosef Bacik #include "blk-rq-qos.h"
76d7067512SJosef Bacik #include "blk-stat.h"
77d7067512SJosef Bacik 
78d7067512SJosef Bacik #define DEFAULT_SCALE_COOKIE 1000000U
79d7067512SJosef Bacik 
80d7067512SJosef Bacik static struct blkcg_policy blkcg_policy_iolatency;
81d7067512SJosef Bacik struct iolatency_grp;
82d7067512SJosef Bacik 
83d7067512SJosef Bacik struct blk_iolatency {
84d7067512SJosef Bacik 	struct rq_qos rqos;
85d7067512SJosef Bacik 	struct timer_list timer;
86d7067512SJosef Bacik 	atomic_t enabled;
87d7067512SJosef Bacik };
88d7067512SJosef Bacik 
89d7067512SJosef Bacik static inline struct blk_iolatency *BLKIOLATENCY(struct rq_qos *rqos)
90d7067512SJosef Bacik {
91d7067512SJosef Bacik 	return container_of(rqos, struct blk_iolatency, rqos);
92d7067512SJosef Bacik }
93d7067512SJosef Bacik 
94d7067512SJosef Bacik static inline bool blk_iolatency_enabled(struct blk_iolatency *blkiolat)
95d7067512SJosef Bacik {
96d7067512SJosef Bacik 	return atomic_read(&blkiolat->enabled) > 0;
97d7067512SJosef Bacik }
98d7067512SJosef Bacik 
99d7067512SJosef Bacik struct child_latency_info {
100d7067512SJosef Bacik 	spinlock_t lock;
101d7067512SJosef Bacik 
102d7067512SJosef Bacik 	/* Last time we adjusted the scale of everybody. */
103d7067512SJosef Bacik 	u64 last_scale_event;
104d7067512SJosef Bacik 
105d7067512SJosef Bacik 	/* The latency that we missed. */
106d7067512SJosef Bacik 	u64 scale_lat;
107d7067512SJosef Bacik 
108d7067512SJosef Bacik 	/* Total io's from all of our children for the last summation. */
109d7067512SJosef Bacik 	u64 nr_samples;
110d7067512SJosef Bacik 
111d7067512SJosef Bacik 	/* The guy who actually changed the latency numbers. */
112d7067512SJosef Bacik 	struct iolatency_grp *scale_grp;
113d7067512SJosef Bacik 
114d7067512SJosef Bacik 	/* Cookie to tell if we need to scale up or down. */
115d7067512SJosef Bacik 	atomic_t scale_cookie;
116d7067512SJosef Bacik };
117d7067512SJosef Bacik 
118d7067512SJosef Bacik struct iolatency_grp {
119d7067512SJosef Bacik 	struct blkg_policy_data pd;
120d7067512SJosef Bacik 	struct blk_rq_stat __percpu *stats;
121d7067512SJosef Bacik 	struct blk_iolatency *blkiolat;
122d7067512SJosef Bacik 	struct rq_depth rq_depth;
123d7067512SJosef Bacik 	struct rq_wait rq_wait;
124d7067512SJosef Bacik 	atomic64_t window_start;
125d7067512SJosef Bacik 	atomic_t scale_cookie;
126d7067512SJosef Bacik 	u64 min_lat_nsec;
127d7067512SJosef Bacik 	u64 cur_win_nsec;
128d7067512SJosef Bacik 
129d7067512SJosef Bacik 	/* total running average of our io latency. */
130c480bcf9SDennis Zhou (Facebook) 	u64 lat_avg;
131d7067512SJosef Bacik 
132d7067512SJosef Bacik 	/* Our current number of IO's for the last summation. */
133d7067512SJosef Bacik 	u64 nr_samples;
134d7067512SJosef Bacik 
135d7067512SJosef Bacik 	struct child_latency_info child_lat;
136d7067512SJosef Bacik };
137d7067512SJosef Bacik 
138c480bcf9SDennis Zhou (Facebook) #define BLKIOLATENCY_MIN_WIN_SIZE (100 * NSEC_PER_MSEC)
139c480bcf9SDennis Zhou (Facebook) #define BLKIOLATENCY_MAX_WIN_SIZE NSEC_PER_SEC
140c480bcf9SDennis Zhou (Facebook) /*
141c480bcf9SDennis Zhou (Facebook)  * These are the constants used to fake the fixed-point moving average
142c480bcf9SDennis Zhou (Facebook)  * calculation just like load average.  The call to CALC_LOAD folds
143c480bcf9SDennis Zhou (Facebook)  * (FIXED_1 (2048) - exp_factor) * new_sample into lat_avg.  The sampling
144c480bcf9SDennis Zhou (Facebook)  * window size is bucketed to try to approximately calculate average
145c480bcf9SDennis Zhou (Facebook)  * latency such that 1/exp (decay rate) is [1 min, 2.5 min) when windows
146c480bcf9SDennis Zhou (Facebook)  * elapse immediately.  Note, windows only elapse with IO activity.  Idle
147c480bcf9SDennis Zhou (Facebook)  * periods extend the most recent window.
148c480bcf9SDennis Zhou (Facebook)  */
149c480bcf9SDennis Zhou (Facebook) #define BLKIOLATENCY_NR_EXP_FACTORS 5
150c480bcf9SDennis Zhou (Facebook) #define BLKIOLATENCY_EXP_BUCKET_SIZE (BLKIOLATENCY_MAX_WIN_SIZE / \
151c480bcf9SDennis Zhou (Facebook) 				      (BLKIOLATENCY_NR_EXP_FACTORS - 1))
152c480bcf9SDennis Zhou (Facebook) static const u64 iolatency_exp_factors[BLKIOLATENCY_NR_EXP_FACTORS] = {
153c480bcf9SDennis Zhou (Facebook) 	2045, // exp(1/600) - 600 samples
154c480bcf9SDennis Zhou (Facebook) 	2039, // exp(1/240) - 240 samples
155c480bcf9SDennis Zhou (Facebook) 	2031, // exp(1/120) - 120 samples
156c480bcf9SDennis Zhou (Facebook) 	2023, // exp(1/80)  - 80 samples
157c480bcf9SDennis Zhou (Facebook) 	2014, // exp(1/60)  - 60 samples
158c480bcf9SDennis Zhou (Facebook) };
159c480bcf9SDennis Zhou (Facebook) 
160d7067512SJosef Bacik static inline struct iolatency_grp *pd_to_lat(struct blkg_policy_data *pd)
161d7067512SJosef Bacik {
162d7067512SJosef Bacik 	return pd ? container_of(pd, struct iolatency_grp, pd) : NULL;
163d7067512SJosef Bacik }
164d7067512SJosef Bacik 
165d7067512SJosef Bacik static inline struct iolatency_grp *blkg_to_lat(struct blkcg_gq *blkg)
166d7067512SJosef Bacik {
167d7067512SJosef Bacik 	return pd_to_lat(blkg_to_pd(blkg, &blkcg_policy_iolatency));
168d7067512SJosef Bacik }
169d7067512SJosef Bacik 
170d7067512SJosef Bacik static inline struct blkcg_gq *lat_to_blkg(struct iolatency_grp *iolat)
171d7067512SJosef Bacik {
172d7067512SJosef Bacik 	return pd_to_blkg(&iolat->pd);
173d7067512SJosef Bacik }
174d7067512SJosef Bacik 
175d7067512SJosef Bacik static inline bool iolatency_may_queue(struct iolatency_grp *iolat,
176d7067512SJosef Bacik 				       wait_queue_entry_t *wait,
177d7067512SJosef Bacik 				       bool first_block)
178d7067512SJosef Bacik {
179d7067512SJosef Bacik 	struct rq_wait *rqw = &iolat->rq_wait;
180d7067512SJosef Bacik 
181d7067512SJosef Bacik 	if (first_block && waitqueue_active(&rqw->wait) &&
182d7067512SJosef Bacik 	    rqw->wait.head.next != &wait->entry)
183d7067512SJosef Bacik 		return false;
184d7067512SJosef Bacik 	return rq_wait_inc_below(rqw, iolat->rq_depth.max_depth);
185d7067512SJosef Bacik }
186d7067512SJosef Bacik 
187d7067512SJosef Bacik static void __blkcg_iolatency_throttle(struct rq_qos *rqos,
188d7067512SJosef Bacik 				       struct iolatency_grp *iolat,
189d7067512SJosef Bacik 				       spinlock_t *lock, bool issue_as_root,
190d7067512SJosef Bacik 				       bool use_memdelay)
191d7067512SJosef Bacik 	__releases(lock)
192d7067512SJosef Bacik 	__acquires(lock)
193d7067512SJosef Bacik {
194d7067512SJosef Bacik 	struct rq_wait *rqw = &iolat->rq_wait;
195d7067512SJosef Bacik 	unsigned use_delay = atomic_read(&lat_to_blkg(iolat)->use_delay);
196d7067512SJosef Bacik 	DEFINE_WAIT(wait);
197d7067512SJosef Bacik 	bool first_block = true;
198d7067512SJosef Bacik 
199d7067512SJosef Bacik 	if (use_delay)
200d7067512SJosef Bacik 		blkcg_schedule_throttle(rqos->q, use_memdelay);
201d7067512SJosef Bacik 
202d7067512SJosef Bacik 	/*
203d7067512SJosef Bacik 	 * To avoid priority inversions we want to just take a slot if we are
204d7067512SJosef Bacik 	 * issuing as root.  If we're being killed off there's no point in
205d7067512SJosef Bacik 	 * delaying things, we may have been killed by OOM so throttling may
206d7067512SJosef Bacik 	 * make recovery take even longer, so just let the IO's through so the
207d7067512SJosef Bacik 	 * task can go away.
208d7067512SJosef Bacik 	 */
209d7067512SJosef Bacik 	if (issue_as_root || fatal_signal_pending(current)) {
210d7067512SJosef Bacik 		atomic_inc(&rqw->inflight);
211d7067512SJosef Bacik 		return;
212d7067512SJosef Bacik 	}
213d7067512SJosef Bacik 
214d7067512SJosef Bacik 	if (iolatency_may_queue(iolat, &wait, first_block))
215d7067512SJosef Bacik 		return;
216d7067512SJosef Bacik 
217d7067512SJosef Bacik 	do {
218d7067512SJosef Bacik 		prepare_to_wait_exclusive(&rqw->wait, &wait,
219d7067512SJosef Bacik 					  TASK_UNINTERRUPTIBLE);
220d7067512SJosef Bacik 
221d7067512SJosef Bacik 		if (iolatency_may_queue(iolat, &wait, first_block))
222d7067512SJosef Bacik 			break;
223d7067512SJosef Bacik 		first_block = false;
224d7067512SJosef Bacik 
225d7067512SJosef Bacik 		if (lock) {
226d7067512SJosef Bacik 			spin_unlock_irq(lock);
227d7067512SJosef Bacik 			io_schedule();
228d7067512SJosef Bacik 			spin_lock_irq(lock);
229d7067512SJosef Bacik 		} else {
230d7067512SJosef Bacik 			io_schedule();
231d7067512SJosef Bacik 		}
232d7067512SJosef Bacik 	} while (1);
233d7067512SJosef Bacik 
234d7067512SJosef Bacik 	finish_wait(&rqw->wait, &wait);
235d7067512SJosef Bacik }
236d7067512SJosef Bacik 
237d7067512SJosef Bacik #define SCALE_DOWN_FACTOR 2
238d7067512SJosef Bacik #define SCALE_UP_FACTOR 4
239d7067512SJosef Bacik 
240d7067512SJosef Bacik static inline unsigned long scale_amount(unsigned long qd, bool up)
241d7067512SJosef Bacik {
242d7067512SJosef Bacik 	return max(up ? qd >> SCALE_UP_FACTOR : qd >> SCALE_DOWN_FACTOR, 1UL);
243d7067512SJosef Bacik }
244d7067512SJosef Bacik 
245d7067512SJosef Bacik /*
246d7067512SJosef Bacik  * We scale the qd down faster than we scale up, so we need to use this helper
247d7067512SJosef Bacik  * to adjust the scale_cookie accordingly so we don't prematurely get
248d7067512SJosef Bacik  * scale_cookie at DEFAULT_SCALE_COOKIE and unthrottle too much.
249d7067512SJosef Bacik  *
250d7067512SJosef Bacik  * Each group has their own local copy of the last scale cookie they saw, so if
251d7067512SJosef Bacik  * the global scale cookie goes up or down they know which way they need to go
252d7067512SJosef Bacik  * based on their last knowledge of it.
253d7067512SJosef Bacik  */
254d7067512SJosef Bacik static void scale_cookie_change(struct blk_iolatency *blkiolat,
255d7067512SJosef Bacik 				struct child_latency_info *lat_info,
256d7067512SJosef Bacik 				bool up)
257d7067512SJosef Bacik {
258ff4cee08SJosef Bacik 	unsigned long qd = blkiolat->rqos.q->nr_requests;
259d7067512SJosef Bacik 	unsigned long scale = scale_amount(qd, up);
260d7067512SJosef Bacik 	unsigned long old = atomic_read(&lat_info->scale_cookie);
261d7067512SJosef Bacik 	unsigned long max_scale = qd << 1;
262d7067512SJosef Bacik 	unsigned long diff = 0;
263d7067512SJosef Bacik 
264d7067512SJosef Bacik 	if (old < DEFAULT_SCALE_COOKIE)
265d7067512SJosef Bacik 		diff = DEFAULT_SCALE_COOKIE - old;
266d7067512SJosef Bacik 
267d7067512SJosef Bacik 	if (up) {
268d7067512SJosef Bacik 		if (scale + old > DEFAULT_SCALE_COOKIE)
269d7067512SJosef Bacik 			atomic_set(&lat_info->scale_cookie,
270d7067512SJosef Bacik 				   DEFAULT_SCALE_COOKIE);
271d7067512SJosef Bacik 		else if (diff > qd)
272d7067512SJosef Bacik 			atomic_inc(&lat_info->scale_cookie);
273d7067512SJosef Bacik 		else
274d7067512SJosef Bacik 			atomic_add(scale, &lat_info->scale_cookie);
275d7067512SJosef Bacik 	} else {
276d7067512SJosef Bacik 		/*
277d7067512SJosef Bacik 		 * We don't want to dig a hole so deep that it takes us hours to
278d7067512SJosef Bacik 		 * dig out of it.  Just enough that we don't throttle/unthrottle
279d7067512SJosef Bacik 		 * with jagged workloads but can still unthrottle once pressure
280d7067512SJosef Bacik 		 * has sufficiently dissipated.
281d7067512SJosef Bacik 		 */
282d7067512SJosef Bacik 		if (diff > qd) {
283d7067512SJosef Bacik 			if (diff < max_scale)
284d7067512SJosef Bacik 				atomic_dec(&lat_info->scale_cookie);
285d7067512SJosef Bacik 		} else {
286d7067512SJosef Bacik 			atomic_sub(scale, &lat_info->scale_cookie);
287d7067512SJosef Bacik 		}
288d7067512SJosef Bacik 	}
289d7067512SJosef Bacik }
290d7067512SJosef Bacik 
291d7067512SJosef Bacik /*
292d7067512SJosef Bacik  * Change the queue depth of the iolatency_grp.  We add/subtract 1/16th of the
293d7067512SJosef Bacik  * queue depth at a time so we don't get wild swings and hopefully dial in to
294d7067512SJosef Bacik  * fairer distribution of the overall queue depth.
295d7067512SJosef Bacik  */
296d7067512SJosef Bacik static void scale_change(struct iolatency_grp *iolat, bool up)
297d7067512SJosef Bacik {
298ff4cee08SJosef Bacik 	unsigned long qd = iolat->blkiolat->rqos.q->nr_requests;
299d7067512SJosef Bacik 	unsigned long scale = scale_amount(qd, up);
300d7067512SJosef Bacik 	unsigned long old = iolat->rq_depth.max_depth;
301d7067512SJosef Bacik 
302d7067512SJosef Bacik 	if (old > qd)
303d7067512SJosef Bacik 		old = qd;
304d7067512SJosef Bacik 
305d7067512SJosef Bacik 	if (up) {
306d7067512SJosef Bacik 		if (old == 1 && blkcg_unuse_delay(lat_to_blkg(iolat)))
307d7067512SJosef Bacik 			return;
308d7067512SJosef Bacik 
309d7067512SJosef Bacik 		if (old < qd) {
310d7067512SJosef Bacik 			old += scale;
311d7067512SJosef Bacik 			old = min(old, qd);
312d7067512SJosef Bacik 			iolat->rq_depth.max_depth = old;
313d7067512SJosef Bacik 			wake_up_all(&iolat->rq_wait.wait);
314d7067512SJosef Bacik 		}
315d7067512SJosef Bacik 	} else if (old > 1) {
316d7067512SJosef Bacik 		old >>= 1;
317d7067512SJosef Bacik 		iolat->rq_depth.max_depth = max(old, 1UL);
318d7067512SJosef Bacik 	}
319d7067512SJosef Bacik }
320d7067512SJosef Bacik 
321d7067512SJosef Bacik /* Check our parent and see if the scale cookie has changed. */
322d7067512SJosef Bacik static void check_scale_change(struct iolatency_grp *iolat)
323d7067512SJosef Bacik {
324d7067512SJosef Bacik 	struct iolatency_grp *parent;
325d7067512SJosef Bacik 	struct child_latency_info *lat_info;
326d7067512SJosef Bacik 	unsigned int cur_cookie;
327d7067512SJosef Bacik 	unsigned int our_cookie = atomic_read(&iolat->scale_cookie);
328d7067512SJosef Bacik 	u64 scale_lat;
329d7067512SJosef Bacik 	unsigned int old;
330d7067512SJosef Bacik 	int direction = 0;
331d7067512SJosef Bacik 
332d7067512SJosef Bacik 	if (lat_to_blkg(iolat)->parent == NULL)
333d7067512SJosef Bacik 		return;
334d7067512SJosef Bacik 
335d7067512SJosef Bacik 	parent = blkg_to_lat(lat_to_blkg(iolat)->parent);
336d7067512SJosef Bacik 	if (!parent)
337d7067512SJosef Bacik 		return;
338d7067512SJosef Bacik 
339d7067512SJosef Bacik 	lat_info = &parent->child_lat;
340d7067512SJosef Bacik 	cur_cookie = atomic_read(&lat_info->scale_cookie);
341d7067512SJosef Bacik 	scale_lat = READ_ONCE(lat_info->scale_lat);
342d7067512SJosef Bacik 
343d7067512SJosef Bacik 	if (cur_cookie < our_cookie)
344d7067512SJosef Bacik 		direction = -1;
345d7067512SJosef Bacik 	else if (cur_cookie > our_cookie)
346d7067512SJosef Bacik 		direction = 1;
347d7067512SJosef Bacik 	else
348d7067512SJosef Bacik 		return;
349d7067512SJosef Bacik 
350d7067512SJosef Bacik 	old = atomic_cmpxchg(&iolat->scale_cookie, our_cookie, cur_cookie);
351d7067512SJosef Bacik 
352d7067512SJosef Bacik 	/* Somebody beat us to the punch, just bail. */
353d7067512SJosef Bacik 	if (old != our_cookie)
354d7067512SJosef Bacik 		return;
355d7067512SJosef Bacik 
356d7067512SJosef Bacik 	if (direction < 0 && iolat->min_lat_nsec) {
357d7067512SJosef Bacik 		u64 samples_thresh;
358d7067512SJosef Bacik 
359d7067512SJosef Bacik 		if (!scale_lat || iolat->min_lat_nsec <= scale_lat)
360d7067512SJosef Bacik 			return;
361d7067512SJosef Bacik 
362d7067512SJosef Bacik 		/*
363d7067512SJosef Bacik 		 * Sometimes high priority groups are their own worst enemy, so
364d7067512SJosef Bacik 		 * instead of taking it out on some poor other group that did 5%
365d7067512SJosef Bacik 		 * or less of the IO's for the last summation just skip this
366d7067512SJosef Bacik 		 * scale down event.
367d7067512SJosef Bacik 		 */
368d7067512SJosef Bacik 		samples_thresh = lat_info->nr_samples * 5;
369d7067512SJosef Bacik 		samples_thresh = div64_u64(samples_thresh, 100);
370d7067512SJosef Bacik 		if (iolat->nr_samples <= samples_thresh)
371d7067512SJosef Bacik 			return;
372d7067512SJosef Bacik 	}
373d7067512SJosef Bacik 
374d7067512SJosef Bacik 	/* We're as low as we can go. */
375d7067512SJosef Bacik 	if (iolat->rq_depth.max_depth == 1 && direction < 0) {
376d7067512SJosef Bacik 		blkcg_use_delay(lat_to_blkg(iolat));
377d7067512SJosef Bacik 		return;
378d7067512SJosef Bacik 	}
379d7067512SJosef Bacik 
380d7067512SJosef Bacik 	/* We're back to the default cookie, unthrottle all the things. */
381d7067512SJosef Bacik 	if (cur_cookie == DEFAULT_SCALE_COOKIE) {
382d7067512SJosef Bacik 		blkcg_clear_delay(lat_to_blkg(iolat));
383a284390bSJosef Bacik 		iolat->rq_depth.max_depth = UINT_MAX;
384d7067512SJosef Bacik 		wake_up_all(&iolat->rq_wait.wait);
385d7067512SJosef Bacik 		return;
386d7067512SJosef Bacik 	}
387d7067512SJosef Bacik 
388d7067512SJosef Bacik 	scale_change(iolat, direction > 0);
389d7067512SJosef Bacik }
390d7067512SJosef Bacik 
391d7067512SJosef Bacik static void blkcg_iolatency_throttle(struct rq_qos *rqos, struct bio *bio,
392d7067512SJosef Bacik 				     spinlock_t *lock)
393d7067512SJosef Bacik {
394d7067512SJosef Bacik 	struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
395a7b39b4eSDennis Zhou (Facebook) 	struct blkcg_gq *blkg = bio->bi_blkg;
396d7067512SJosef Bacik 	bool issue_as_root = bio_issue_as_root_blkg(bio);
397d7067512SJosef Bacik 
398d7067512SJosef Bacik 	if (!blk_iolatency_enabled(blkiolat))
399d7067512SJosef Bacik 		return;
400d7067512SJosef Bacik 
401d7067512SJosef Bacik 	while (blkg && blkg->parent) {
402d7067512SJosef Bacik 		struct iolatency_grp *iolat = blkg_to_lat(blkg);
403d7067512SJosef Bacik 		if (!iolat) {
404d7067512SJosef Bacik 			blkg = blkg->parent;
405d7067512SJosef Bacik 			continue;
406d7067512SJosef Bacik 		}
407d7067512SJosef Bacik 
408d7067512SJosef Bacik 		check_scale_change(iolat);
409d7067512SJosef Bacik 		__blkcg_iolatency_throttle(rqos, iolat, lock, issue_as_root,
410d7067512SJosef Bacik 				     (bio->bi_opf & REQ_SWAP) == REQ_SWAP);
411d7067512SJosef Bacik 		blkg = blkg->parent;
412d7067512SJosef Bacik 	}
413d7067512SJosef Bacik 	if (!timer_pending(&blkiolat->timer))
414d7067512SJosef Bacik 		mod_timer(&blkiolat->timer, jiffies + HZ);
415d7067512SJosef Bacik }
416d7067512SJosef Bacik 
417d7067512SJosef Bacik static void iolatency_record_time(struct iolatency_grp *iolat,
418d7067512SJosef Bacik 				  struct bio_issue *issue, u64 now,
419d7067512SJosef Bacik 				  bool issue_as_root)
420d7067512SJosef Bacik {
421d7067512SJosef Bacik 	struct blk_rq_stat *rq_stat;
422d7067512SJosef Bacik 	u64 start = bio_issue_time(issue);
423d7067512SJosef Bacik 	u64 req_time;
424d7067512SJosef Bacik 
42571e9690bSJosef Bacik 	/*
42671e9690bSJosef Bacik 	 * Have to do this so we are truncated to the correct time that our
42771e9690bSJosef Bacik 	 * issue is truncated to.
42871e9690bSJosef Bacik 	 */
42971e9690bSJosef Bacik 	now = __bio_issue_time(now);
43071e9690bSJosef Bacik 
431d7067512SJosef Bacik 	if (now <= start)
432d7067512SJosef Bacik 		return;
433d7067512SJosef Bacik 
434d7067512SJosef Bacik 	req_time = now - start;
435d7067512SJosef Bacik 
436d7067512SJosef Bacik 	/*
437d7067512SJosef Bacik 	 * We don't want to count issue_as_root bio's in the cgroups latency
438d7067512SJosef Bacik 	 * statistics as it could skew the numbers downwards.
439d7067512SJosef Bacik 	 */
440a284390bSJosef Bacik 	if (unlikely(issue_as_root && iolat->rq_depth.max_depth != UINT_MAX)) {
441d7067512SJosef Bacik 		u64 sub = iolat->min_lat_nsec;
442d7067512SJosef Bacik 		if (req_time < sub)
443d7067512SJosef Bacik 			blkcg_add_delay(lat_to_blkg(iolat), now, sub - req_time);
444d7067512SJosef Bacik 		return;
445d7067512SJosef Bacik 	}
446d7067512SJosef Bacik 
447d7067512SJosef Bacik 	rq_stat = get_cpu_ptr(iolat->stats);
448d7067512SJosef Bacik 	blk_rq_stat_add(rq_stat, req_time);
449d7067512SJosef Bacik 	put_cpu_ptr(rq_stat);
450d7067512SJosef Bacik }
451d7067512SJosef Bacik 
452d7067512SJosef Bacik #define BLKIOLATENCY_MIN_ADJUST_TIME (500 * NSEC_PER_MSEC)
453d7067512SJosef Bacik #define BLKIOLATENCY_MIN_GOOD_SAMPLES 5
454d7067512SJosef Bacik 
455d7067512SJosef Bacik static void iolatency_check_latencies(struct iolatency_grp *iolat, u64 now)
456d7067512SJosef Bacik {
457d7067512SJosef Bacik 	struct blkcg_gq *blkg = lat_to_blkg(iolat);
458d7067512SJosef Bacik 	struct iolatency_grp *parent;
459d7067512SJosef Bacik 	struct child_latency_info *lat_info;
460d7067512SJosef Bacik 	struct blk_rq_stat stat;
461d7067512SJosef Bacik 	unsigned long flags;
462c480bcf9SDennis Zhou (Facebook) 	int cpu, exp_idx;
463d7067512SJosef Bacik 
464d7067512SJosef Bacik 	blk_rq_stat_init(&stat);
465d7067512SJosef Bacik 	preempt_disable();
466d7067512SJosef Bacik 	for_each_online_cpu(cpu) {
467d7067512SJosef Bacik 		struct blk_rq_stat *s;
468d7067512SJosef Bacik 		s = per_cpu_ptr(iolat->stats, cpu);
469d7067512SJosef Bacik 		blk_rq_stat_sum(&stat, s);
470d7067512SJosef Bacik 		blk_rq_stat_init(s);
471d7067512SJosef Bacik 	}
472d7067512SJosef Bacik 	preempt_enable();
473d7067512SJosef Bacik 
474d7067512SJosef Bacik 	parent = blkg_to_lat(blkg->parent);
475d7067512SJosef Bacik 	if (!parent)
476d7067512SJosef Bacik 		return;
477d7067512SJosef Bacik 
478d7067512SJosef Bacik 	lat_info = &parent->child_lat;
479d7067512SJosef Bacik 
480c480bcf9SDennis Zhou (Facebook) 	/*
481c480bcf9SDennis Zhou (Facebook) 	 * CALC_LOAD takes in a number stored in fixed point representation.
482c480bcf9SDennis Zhou (Facebook) 	 * Because we are using this for IO time in ns, the values stored
483c480bcf9SDennis Zhou (Facebook) 	 * are significantly larger than the FIXED_1 denominator (2048).
484c480bcf9SDennis Zhou (Facebook) 	 * Therefore, rounding errors in the calculation are negligible and
485c480bcf9SDennis Zhou (Facebook) 	 * can be ignored.
486c480bcf9SDennis Zhou (Facebook) 	 */
487c480bcf9SDennis Zhou (Facebook) 	exp_idx = min_t(int, BLKIOLATENCY_NR_EXP_FACTORS - 1,
488c480bcf9SDennis Zhou (Facebook) 			div64_u64(iolat->cur_win_nsec,
489c480bcf9SDennis Zhou (Facebook) 				  BLKIOLATENCY_EXP_BUCKET_SIZE));
490c480bcf9SDennis Zhou (Facebook) 	CALC_LOAD(iolat->lat_avg, iolatency_exp_factors[exp_idx], stat.mean);
491d7067512SJosef Bacik 
492d7067512SJosef Bacik 	/* Everything is ok and we don't need to adjust the scale. */
493d7067512SJosef Bacik 	if (stat.mean <= iolat->min_lat_nsec &&
494d7067512SJosef Bacik 	    atomic_read(&lat_info->scale_cookie) == DEFAULT_SCALE_COOKIE)
495d7067512SJosef Bacik 		return;
496d7067512SJosef Bacik 
497d7067512SJosef Bacik 	/* Somebody beat us to the punch, just bail. */
498d7067512SJosef Bacik 	spin_lock_irqsave(&lat_info->lock, flags);
499d7067512SJosef Bacik 	lat_info->nr_samples -= iolat->nr_samples;
500d7067512SJosef Bacik 	lat_info->nr_samples += stat.nr_samples;
501d7067512SJosef Bacik 	iolat->nr_samples = stat.nr_samples;
502d7067512SJosef Bacik 
503d7067512SJosef Bacik 	if ((lat_info->last_scale_event >= now ||
504d7067512SJosef Bacik 	    now - lat_info->last_scale_event < BLKIOLATENCY_MIN_ADJUST_TIME) &&
505d7067512SJosef Bacik 	    lat_info->scale_lat <= iolat->min_lat_nsec)
506d7067512SJosef Bacik 		goto out;
507d7067512SJosef Bacik 
508d7067512SJosef Bacik 	if (stat.mean <= iolat->min_lat_nsec &&
509d7067512SJosef Bacik 	    stat.nr_samples >= BLKIOLATENCY_MIN_GOOD_SAMPLES) {
510d7067512SJosef Bacik 		if (lat_info->scale_grp == iolat) {
511d7067512SJosef Bacik 			lat_info->last_scale_event = now;
512d7067512SJosef Bacik 			scale_cookie_change(iolat->blkiolat, lat_info, true);
513d7067512SJosef Bacik 		}
514d7067512SJosef Bacik 	} else if (stat.mean > iolat->min_lat_nsec) {
515d7067512SJosef Bacik 		lat_info->last_scale_event = now;
516d7067512SJosef Bacik 		if (!lat_info->scale_grp ||
517d7067512SJosef Bacik 		    lat_info->scale_lat > iolat->min_lat_nsec) {
518d7067512SJosef Bacik 			WRITE_ONCE(lat_info->scale_lat, iolat->min_lat_nsec);
519d7067512SJosef Bacik 			lat_info->scale_grp = iolat;
520d7067512SJosef Bacik 		}
521d7067512SJosef Bacik 		scale_cookie_change(iolat->blkiolat, lat_info, false);
522d7067512SJosef Bacik 	}
523d7067512SJosef Bacik out:
524d7067512SJosef Bacik 	spin_unlock_irqrestore(&lat_info->lock, flags);
525d7067512SJosef Bacik }
526d7067512SJosef Bacik 
527d7067512SJosef Bacik static void blkcg_iolatency_done_bio(struct rq_qos *rqos, struct bio *bio)
528d7067512SJosef Bacik {
529d7067512SJosef Bacik 	struct blkcg_gq *blkg;
530d7067512SJosef Bacik 	struct rq_wait *rqw;
531d7067512SJosef Bacik 	struct iolatency_grp *iolat;
532d7067512SJosef Bacik 	u64 window_start;
533d7067512SJosef Bacik 	u64 now = ktime_to_ns(ktime_get());
534d7067512SJosef Bacik 	bool issue_as_root = bio_issue_as_root_blkg(bio);
535d7067512SJosef Bacik 	bool enabled = false;
536d7067512SJosef Bacik 
537d7067512SJosef Bacik 	blkg = bio->bi_blkg;
538d7067512SJosef Bacik 	if (!blkg)
539d7067512SJosef Bacik 		return;
540d7067512SJosef Bacik 
541d7067512SJosef Bacik 	iolat = blkg_to_lat(bio->bi_blkg);
542d7067512SJosef Bacik 	if (!iolat)
543d7067512SJosef Bacik 		return;
544d7067512SJosef Bacik 
545d7067512SJosef Bacik 	enabled = blk_iolatency_enabled(iolat->blkiolat);
546d7067512SJosef Bacik 	while (blkg && blkg->parent) {
547d7067512SJosef Bacik 		iolat = blkg_to_lat(blkg);
548d7067512SJosef Bacik 		if (!iolat) {
549d7067512SJosef Bacik 			blkg = blkg->parent;
550d7067512SJosef Bacik 			continue;
551d7067512SJosef Bacik 		}
552d7067512SJosef Bacik 		rqw = &iolat->rq_wait;
553d7067512SJosef Bacik 
554d7067512SJosef Bacik 		atomic_dec(&rqw->inflight);
555d7067512SJosef Bacik 		if (!enabled || iolat->min_lat_nsec == 0)
556d7067512SJosef Bacik 			goto next;
557d7067512SJosef Bacik 		iolatency_record_time(iolat, &bio->bi_issue, now,
558d7067512SJosef Bacik 				      issue_as_root);
559d7067512SJosef Bacik 		window_start = atomic64_read(&iolat->window_start);
560d7067512SJosef Bacik 		if (now > window_start &&
561d7067512SJosef Bacik 		    (now - window_start) >= iolat->cur_win_nsec) {
562d7067512SJosef Bacik 			if (atomic64_cmpxchg(&iolat->window_start,
563d7067512SJosef Bacik 					window_start, now) == window_start)
564d7067512SJosef Bacik 				iolatency_check_latencies(iolat, now);
565d7067512SJosef Bacik 		}
566d7067512SJosef Bacik next:
567d7067512SJosef Bacik 		wake_up(&rqw->wait);
568d7067512SJosef Bacik 		blkg = blkg->parent;
569d7067512SJosef Bacik 	}
570d7067512SJosef Bacik }
571d7067512SJosef Bacik 
572d7067512SJosef Bacik static void blkcg_iolatency_cleanup(struct rq_qos *rqos, struct bio *bio)
573d7067512SJosef Bacik {
574d7067512SJosef Bacik 	struct blkcg_gq *blkg;
575d7067512SJosef Bacik 
576d7067512SJosef Bacik 	blkg = bio->bi_blkg;
577d7067512SJosef Bacik 	while (blkg && blkg->parent) {
578d7067512SJosef Bacik 		struct rq_wait *rqw;
579d7067512SJosef Bacik 		struct iolatency_grp *iolat;
580d7067512SJosef Bacik 
581d7067512SJosef Bacik 		iolat = blkg_to_lat(blkg);
582d7067512SJosef Bacik 		if (!iolat)
583d7067512SJosef Bacik 			goto next;
584d7067512SJosef Bacik 
585d7067512SJosef Bacik 		rqw = &iolat->rq_wait;
586d7067512SJosef Bacik 		atomic_dec(&rqw->inflight);
587d7067512SJosef Bacik 		wake_up(&rqw->wait);
588d7067512SJosef Bacik next:
589d7067512SJosef Bacik 		blkg = blkg->parent;
590d7067512SJosef Bacik 	}
591d7067512SJosef Bacik }
592d7067512SJosef Bacik 
593d7067512SJosef Bacik static void blkcg_iolatency_exit(struct rq_qos *rqos)
594d7067512SJosef Bacik {
595d7067512SJosef Bacik 	struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
596d7067512SJosef Bacik 
597d7067512SJosef Bacik 	del_timer_sync(&blkiolat->timer);
598d7067512SJosef Bacik 	blkcg_deactivate_policy(rqos->q, &blkcg_policy_iolatency);
599d7067512SJosef Bacik 	kfree(blkiolat);
600d7067512SJosef Bacik }
601d7067512SJosef Bacik 
602d7067512SJosef Bacik static struct rq_qos_ops blkcg_iolatency_ops = {
603d7067512SJosef Bacik 	.throttle = blkcg_iolatency_throttle,
604d7067512SJosef Bacik 	.cleanup = blkcg_iolatency_cleanup,
605d7067512SJosef Bacik 	.done_bio = blkcg_iolatency_done_bio,
606d7067512SJosef Bacik 	.exit = blkcg_iolatency_exit,
607d7067512SJosef Bacik };
608d7067512SJosef Bacik 
609d7067512SJosef Bacik static void blkiolatency_timer_fn(struct timer_list *t)
610d7067512SJosef Bacik {
611d7067512SJosef Bacik 	struct blk_iolatency *blkiolat = from_timer(blkiolat, t, timer);
612d7067512SJosef Bacik 	struct blkcg_gq *blkg;
613d7067512SJosef Bacik 	struct cgroup_subsys_state *pos_css;
614d7067512SJosef Bacik 	u64 now = ktime_to_ns(ktime_get());
615d7067512SJosef Bacik 
616d7067512SJosef Bacik 	rcu_read_lock();
617d7067512SJosef Bacik 	blkg_for_each_descendant_pre(blkg, pos_css,
618d7067512SJosef Bacik 				     blkiolat->rqos.q->root_blkg) {
619d7067512SJosef Bacik 		struct iolatency_grp *iolat;
620d7067512SJosef Bacik 		struct child_latency_info *lat_info;
621d7067512SJosef Bacik 		unsigned long flags;
622d7067512SJosef Bacik 		u64 cookie;
623d7067512SJosef Bacik 
624d7067512SJosef Bacik 		/*
625d7067512SJosef Bacik 		 * We could be exiting, don't access the pd unless we have a
626d7067512SJosef Bacik 		 * ref on the blkg.
627d7067512SJosef Bacik 		 */
628101246ecSDennis Zhou (Facebook) 		if (!blkg_tryget(blkg))
629d7067512SJosef Bacik 			continue;
630d7067512SJosef Bacik 
631d7067512SJosef Bacik 		iolat = blkg_to_lat(blkg);
632d7067512SJosef Bacik 		if (!iolat)
63352a1199cSJosef Bacik 			goto next;
634d7067512SJosef Bacik 
635d7067512SJosef Bacik 		lat_info = &iolat->child_lat;
636d7067512SJosef Bacik 		cookie = atomic_read(&lat_info->scale_cookie);
637d7067512SJosef Bacik 
638d7067512SJosef Bacik 		if (cookie >= DEFAULT_SCALE_COOKIE)
639d7067512SJosef Bacik 			goto next;
640d7067512SJosef Bacik 
641d7067512SJosef Bacik 		spin_lock_irqsave(&lat_info->lock, flags);
642d7067512SJosef Bacik 		if (lat_info->last_scale_event >= now)
643d7067512SJosef Bacik 			goto next_lock;
644d7067512SJosef Bacik 
645d7067512SJosef Bacik 		/*
646d7067512SJosef Bacik 		 * We scaled down but don't have a scale_grp, scale up and carry
647d7067512SJosef Bacik 		 * on.
648d7067512SJosef Bacik 		 */
649d7067512SJosef Bacik 		if (lat_info->scale_grp == NULL) {
650d7067512SJosef Bacik 			scale_cookie_change(iolat->blkiolat, lat_info, true);
651d7067512SJosef Bacik 			goto next_lock;
652d7067512SJosef Bacik 		}
653d7067512SJosef Bacik 
654d7067512SJosef Bacik 		/*
655d7067512SJosef Bacik 		 * It's been 5 seconds since our last scale event, clear the
656d7067512SJosef Bacik 		 * scale grp in case the group that needed the scale down isn't
657d7067512SJosef Bacik 		 * doing any IO currently.
658d7067512SJosef Bacik 		 */
659d7067512SJosef Bacik 		if (now - lat_info->last_scale_event >=
660d7067512SJosef Bacik 		    ((u64)NSEC_PER_SEC * 5))
661d7067512SJosef Bacik 			lat_info->scale_grp = NULL;
662d7067512SJosef Bacik next_lock:
663d7067512SJosef Bacik 		spin_unlock_irqrestore(&lat_info->lock, flags);
664d7067512SJosef Bacik next:
665d7067512SJosef Bacik 		blkg_put(blkg);
666d7067512SJosef Bacik 	}
667d7067512SJosef Bacik 	rcu_read_unlock();
668d7067512SJosef Bacik }
669d7067512SJosef Bacik 
670d7067512SJosef Bacik int blk_iolatency_init(struct request_queue *q)
671d7067512SJosef Bacik {
672d7067512SJosef Bacik 	struct blk_iolatency *blkiolat;
673d7067512SJosef Bacik 	struct rq_qos *rqos;
674d7067512SJosef Bacik 	int ret;
675d7067512SJosef Bacik 
676d7067512SJosef Bacik 	blkiolat = kzalloc(sizeof(*blkiolat), GFP_KERNEL);
677d7067512SJosef Bacik 	if (!blkiolat)
678d7067512SJosef Bacik 		return -ENOMEM;
679d7067512SJosef Bacik 
680d7067512SJosef Bacik 	rqos = &blkiolat->rqos;
681d7067512SJosef Bacik 	rqos->id = RQ_QOS_CGROUP;
682d7067512SJosef Bacik 	rqos->ops = &blkcg_iolatency_ops;
683d7067512SJosef Bacik 	rqos->q = q;
684d7067512SJosef Bacik 
685d7067512SJosef Bacik 	rq_qos_add(q, rqos);
686d7067512SJosef Bacik 
687d7067512SJosef Bacik 	ret = blkcg_activate_policy(q, &blkcg_policy_iolatency);
688d7067512SJosef Bacik 	if (ret) {
689d7067512SJosef Bacik 		rq_qos_del(q, rqos);
690d7067512SJosef Bacik 		kfree(blkiolat);
691d7067512SJosef Bacik 		return ret;
692d7067512SJosef Bacik 	}
693d7067512SJosef Bacik 
694d7067512SJosef Bacik 	timer_setup(&blkiolat->timer, blkiolatency_timer_fn, 0);
695d7067512SJosef Bacik 
696d7067512SJosef Bacik 	return 0;
697d7067512SJosef Bacik }
698d7067512SJosef Bacik 
699d7067512SJosef Bacik static void iolatency_set_min_lat_nsec(struct blkcg_gq *blkg, u64 val)
700d7067512SJosef Bacik {
701d7067512SJosef Bacik 	struct iolatency_grp *iolat = blkg_to_lat(blkg);
702d7067512SJosef Bacik 	struct blk_iolatency *blkiolat = iolat->blkiolat;
703d7067512SJosef Bacik 	u64 oldval = iolat->min_lat_nsec;
704d7067512SJosef Bacik 
705d7067512SJosef Bacik 	iolat->min_lat_nsec = val;
706c480bcf9SDennis Zhou (Facebook) 	iolat->cur_win_nsec = max_t(u64, val << 4, BLKIOLATENCY_MIN_WIN_SIZE);
707c480bcf9SDennis Zhou (Facebook) 	iolat->cur_win_nsec = min_t(u64, iolat->cur_win_nsec,
708c480bcf9SDennis Zhou (Facebook) 				    BLKIOLATENCY_MAX_WIN_SIZE);
709d7067512SJosef Bacik 
710d7067512SJosef Bacik 	if (!oldval && val)
711d7067512SJosef Bacik 		atomic_inc(&blkiolat->enabled);
712d7067512SJosef Bacik 	if (oldval && !val)
713d7067512SJosef Bacik 		atomic_dec(&blkiolat->enabled);
714d7067512SJosef Bacik }
715d7067512SJosef Bacik 
716d7067512SJosef Bacik static void iolatency_clear_scaling(struct blkcg_gq *blkg)
717d7067512SJosef Bacik {
718d7067512SJosef Bacik 	if (blkg->parent) {
719d7067512SJosef Bacik 		struct iolatency_grp *iolat = blkg_to_lat(blkg->parent);
720d7067512SJosef Bacik 		struct child_latency_info *lat_info;
721d7067512SJosef Bacik 		if (!iolat)
722d7067512SJosef Bacik 			return;
723d7067512SJosef Bacik 
724d7067512SJosef Bacik 		lat_info = &iolat->child_lat;
725d7067512SJosef Bacik 		spin_lock(&lat_info->lock);
726d7067512SJosef Bacik 		atomic_set(&lat_info->scale_cookie, DEFAULT_SCALE_COOKIE);
727d7067512SJosef Bacik 		lat_info->last_scale_event = 0;
728d7067512SJosef Bacik 		lat_info->scale_grp = NULL;
729d7067512SJosef Bacik 		lat_info->scale_lat = 0;
730d7067512SJosef Bacik 		spin_unlock(&lat_info->lock);
731d7067512SJosef Bacik 	}
732d7067512SJosef Bacik }
733d7067512SJosef Bacik 
734d7067512SJosef Bacik static ssize_t iolatency_set_limit(struct kernfs_open_file *of, char *buf,
735d7067512SJosef Bacik 			     size_t nbytes, loff_t off)
736d7067512SJosef Bacik {
737d7067512SJosef Bacik 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
738d7067512SJosef Bacik 	struct blkcg_gq *blkg;
739d7067512SJosef Bacik 	struct blkg_conf_ctx ctx;
740d7067512SJosef Bacik 	struct iolatency_grp *iolat;
741d7067512SJosef Bacik 	char *p, *tok;
742d7067512SJosef Bacik 	u64 lat_val = 0;
743d7067512SJosef Bacik 	u64 oldval;
744d7067512SJosef Bacik 	int ret;
745d7067512SJosef Bacik 
746d7067512SJosef Bacik 	ret = blkg_conf_prep(blkcg, &blkcg_policy_iolatency, buf, &ctx);
747d7067512SJosef Bacik 	if (ret)
748d7067512SJosef Bacik 		return ret;
749d7067512SJosef Bacik 
750d7067512SJosef Bacik 	iolat = blkg_to_lat(ctx.blkg);
751d7067512SJosef Bacik 	p = ctx.body;
752d7067512SJosef Bacik 
753d7067512SJosef Bacik 	ret = -EINVAL;
754d7067512SJosef Bacik 	while ((tok = strsep(&p, " "))) {
755d7067512SJosef Bacik 		char key[16];
756d7067512SJosef Bacik 		char val[21];	/* 18446744073709551616 */
757d7067512SJosef Bacik 
758d7067512SJosef Bacik 		if (sscanf(tok, "%15[^=]=%20s", key, val) != 2)
759d7067512SJosef Bacik 			goto out;
760d7067512SJosef Bacik 
761d7067512SJosef Bacik 		if (!strcmp(key, "target")) {
762d7067512SJosef Bacik 			u64 v;
763d7067512SJosef Bacik 
764d7067512SJosef Bacik 			if (!strcmp(val, "max"))
765d7067512SJosef Bacik 				lat_val = 0;
766d7067512SJosef Bacik 			else if (sscanf(val, "%llu", &v) == 1)
767d7067512SJosef Bacik 				lat_val = v * NSEC_PER_USEC;
768d7067512SJosef Bacik 			else
769d7067512SJosef Bacik 				goto out;
770d7067512SJosef Bacik 		} else {
771d7067512SJosef Bacik 			goto out;
772d7067512SJosef Bacik 		}
773d7067512SJosef Bacik 	}
774d7067512SJosef Bacik 
775d7067512SJosef Bacik 	/* Walk up the tree to see if our new val is lower than it should be. */
776d7067512SJosef Bacik 	blkg = ctx.blkg;
777d7067512SJosef Bacik 	oldval = iolat->min_lat_nsec;
778d7067512SJosef Bacik 
779d7067512SJosef Bacik 	iolatency_set_min_lat_nsec(blkg, lat_val);
780d7067512SJosef Bacik 	if (oldval != iolat->min_lat_nsec) {
781d7067512SJosef Bacik 		iolatency_clear_scaling(blkg);
782d7067512SJosef Bacik 	}
783d7067512SJosef Bacik 
784d7067512SJosef Bacik 	ret = 0;
785d7067512SJosef Bacik out:
786d7067512SJosef Bacik 	blkg_conf_finish(&ctx);
787d7067512SJosef Bacik 	return ret ?: nbytes;
788d7067512SJosef Bacik }
789d7067512SJosef Bacik 
790d7067512SJosef Bacik static u64 iolatency_prfill_limit(struct seq_file *sf,
791d7067512SJosef Bacik 				  struct blkg_policy_data *pd, int off)
792d7067512SJosef Bacik {
793d7067512SJosef Bacik 	struct iolatency_grp *iolat = pd_to_lat(pd);
794d7067512SJosef Bacik 	const char *dname = blkg_dev_name(pd->blkg);
795d7067512SJosef Bacik 
796d7067512SJosef Bacik 	if (!dname || !iolat->min_lat_nsec)
797d7067512SJosef Bacik 		return 0;
798d7067512SJosef Bacik 	seq_printf(sf, "%s target=%llu\n",
79988b7210cSArnd Bergmann 		   dname, div_u64(iolat->min_lat_nsec, NSEC_PER_USEC));
800d7067512SJosef Bacik 	return 0;
801d7067512SJosef Bacik }
802d7067512SJosef Bacik 
803d7067512SJosef Bacik static int iolatency_print_limit(struct seq_file *sf, void *v)
804d7067512SJosef Bacik {
805d7067512SJosef Bacik 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)),
806d7067512SJosef Bacik 			  iolatency_prfill_limit,
807d7067512SJosef Bacik 			  &blkcg_policy_iolatency, seq_cft(sf)->private, false);
808d7067512SJosef Bacik 	return 0;
809d7067512SJosef Bacik }
810d7067512SJosef Bacik 
811d7067512SJosef Bacik static size_t iolatency_pd_stat(struct blkg_policy_data *pd, char *buf,
812d7067512SJosef Bacik 				size_t size)
813d7067512SJosef Bacik {
814d7067512SJosef Bacik 	struct iolatency_grp *iolat = pd_to_lat(pd);
815c480bcf9SDennis Zhou (Facebook) 	unsigned long long avg_lat = div64_u64(iolat->lat_avg, NSEC_PER_USEC);
816c480bcf9SDennis Zhou (Facebook) 	unsigned long long cur_win = div64_u64(iolat->cur_win_nsec, NSEC_PER_MSEC);
817d7067512SJosef Bacik 
818a284390bSJosef Bacik 	if (iolat->rq_depth.max_depth == UINT_MAX)
819c480bcf9SDennis Zhou (Facebook) 		return scnprintf(buf, size, " depth=max avg_lat=%llu win=%llu",
820c480bcf9SDennis Zhou (Facebook) 				 avg_lat, cur_win);
821d7067512SJosef Bacik 
822c480bcf9SDennis Zhou (Facebook) 	return scnprintf(buf, size, " depth=%u avg_lat=%llu win=%llu",
823c480bcf9SDennis Zhou (Facebook) 			 iolat->rq_depth.max_depth, avg_lat, cur_win);
824d7067512SJosef Bacik }
825d7067512SJosef Bacik 
826d7067512SJosef Bacik 
827d7067512SJosef Bacik static struct blkg_policy_data *iolatency_pd_alloc(gfp_t gfp, int node)
828d7067512SJosef Bacik {
829d7067512SJosef Bacik 	struct iolatency_grp *iolat;
830d7067512SJosef Bacik 
831d7067512SJosef Bacik 	iolat = kzalloc_node(sizeof(*iolat), gfp, node);
832d7067512SJosef Bacik 	if (!iolat)
833d7067512SJosef Bacik 		return NULL;
834d7067512SJosef Bacik 	iolat->stats = __alloc_percpu_gfp(sizeof(struct blk_rq_stat),
835d7067512SJosef Bacik 				       __alignof__(struct blk_rq_stat), gfp);
836d7067512SJosef Bacik 	if (!iolat->stats) {
837d7067512SJosef Bacik 		kfree(iolat);
838d7067512SJosef Bacik 		return NULL;
839d7067512SJosef Bacik 	}
840d7067512SJosef Bacik 	return &iolat->pd;
841d7067512SJosef Bacik }
842d7067512SJosef Bacik 
843d7067512SJosef Bacik static void iolatency_pd_init(struct blkg_policy_data *pd)
844d7067512SJosef Bacik {
845d7067512SJosef Bacik 	struct iolatency_grp *iolat = pd_to_lat(pd);
846d7067512SJosef Bacik 	struct blkcg_gq *blkg = lat_to_blkg(iolat);
847d7067512SJosef Bacik 	struct rq_qos *rqos = blkcg_rq_qos(blkg->q);
848d7067512SJosef Bacik 	struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
849d7067512SJosef Bacik 	u64 now = ktime_to_ns(ktime_get());
850d7067512SJosef Bacik 	int cpu;
851d7067512SJosef Bacik 
852d7067512SJosef Bacik 	for_each_possible_cpu(cpu) {
853d7067512SJosef Bacik 		struct blk_rq_stat *stat;
854d7067512SJosef Bacik 		stat = per_cpu_ptr(iolat->stats, cpu);
855d7067512SJosef Bacik 		blk_rq_stat_init(stat);
856d7067512SJosef Bacik 	}
857d7067512SJosef Bacik 
858d7067512SJosef Bacik 	rq_wait_init(&iolat->rq_wait);
859d7067512SJosef Bacik 	spin_lock_init(&iolat->child_lat.lock);
860ff4cee08SJosef Bacik 	iolat->rq_depth.queue_depth = blkg->q->nr_requests;
861a284390bSJosef Bacik 	iolat->rq_depth.max_depth = UINT_MAX;
862d7067512SJosef Bacik 	iolat->rq_depth.default_depth = iolat->rq_depth.queue_depth;
863d7067512SJosef Bacik 	iolat->blkiolat = blkiolat;
864d7067512SJosef Bacik 	iolat->cur_win_nsec = 100 * NSEC_PER_MSEC;
865d7067512SJosef Bacik 	atomic64_set(&iolat->window_start, now);
866d7067512SJosef Bacik 
867d7067512SJosef Bacik 	/*
868d7067512SJosef Bacik 	 * We init things in list order, so the pd for the parent may not be
869d7067512SJosef Bacik 	 * init'ed yet for whatever reason.
870d7067512SJosef Bacik 	 */
871d7067512SJosef Bacik 	if (blkg->parent && blkg_to_pd(blkg->parent, &blkcg_policy_iolatency)) {
872d7067512SJosef Bacik 		struct iolatency_grp *parent = blkg_to_lat(blkg->parent);
873d7067512SJosef Bacik 		atomic_set(&iolat->scale_cookie,
874d7067512SJosef Bacik 			   atomic_read(&parent->child_lat.scale_cookie));
875d7067512SJosef Bacik 	} else {
876d7067512SJosef Bacik 		atomic_set(&iolat->scale_cookie, DEFAULT_SCALE_COOKIE);
877d7067512SJosef Bacik 	}
878d7067512SJosef Bacik 
879d7067512SJosef Bacik 	atomic_set(&iolat->child_lat.scale_cookie, DEFAULT_SCALE_COOKIE);
880d7067512SJosef Bacik }
881d7067512SJosef Bacik 
882d7067512SJosef Bacik static void iolatency_pd_offline(struct blkg_policy_data *pd)
883d7067512SJosef Bacik {
884d7067512SJosef Bacik 	struct iolatency_grp *iolat = pd_to_lat(pd);
885d7067512SJosef Bacik 	struct blkcg_gq *blkg = lat_to_blkg(iolat);
886d7067512SJosef Bacik 
887d7067512SJosef Bacik 	iolatency_set_min_lat_nsec(blkg, 0);
888d7067512SJosef Bacik 	iolatency_clear_scaling(blkg);
889d7067512SJosef Bacik }
890d7067512SJosef Bacik 
891d7067512SJosef Bacik static void iolatency_pd_free(struct blkg_policy_data *pd)
892d7067512SJosef Bacik {
893d7067512SJosef Bacik 	struct iolatency_grp *iolat = pd_to_lat(pd);
894d7067512SJosef Bacik 	free_percpu(iolat->stats);
895d7067512SJosef Bacik 	kfree(iolat);
896d7067512SJosef Bacik }
897d7067512SJosef Bacik 
898d7067512SJosef Bacik static struct cftype iolatency_files[] = {
899d7067512SJosef Bacik 	{
900d7067512SJosef Bacik 		.name = "latency",
901d7067512SJosef Bacik 		.flags = CFTYPE_NOT_ON_ROOT,
902d7067512SJosef Bacik 		.seq_show = iolatency_print_limit,
903d7067512SJosef Bacik 		.write = iolatency_set_limit,
904d7067512SJosef Bacik 	},
905d7067512SJosef Bacik 	{}
906d7067512SJosef Bacik };
907d7067512SJosef Bacik 
908d7067512SJosef Bacik static struct blkcg_policy blkcg_policy_iolatency = {
909d7067512SJosef Bacik 	.dfl_cftypes	= iolatency_files,
910d7067512SJosef Bacik 	.pd_alloc_fn	= iolatency_pd_alloc,
911d7067512SJosef Bacik 	.pd_init_fn	= iolatency_pd_init,
912d7067512SJosef Bacik 	.pd_offline_fn	= iolatency_pd_offline,
913d7067512SJosef Bacik 	.pd_free_fn	= iolatency_pd_free,
914d7067512SJosef Bacik 	.pd_stat_fn	= iolatency_pd_stat,
915d7067512SJosef Bacik };
916d7067512SJosef Bacik 
917d7067512SJosef Bacik static int __init iolatency_init(void)
918d7067512SJosef Bacik {
919d7067512SJosef Bacik 	return blkcg_policy_register(&blkcg_policy_iolatency);
920d7067512SJosef Bacik }
921d7067512SJosef Bacik 
922d7067512SJosef Bacik static void __exit iolatency_exit(void)
923d7067512SJosef Bacik {
924d7067512SJosef Bacik 	return blkcg_policy_unregister(&blkcg_policy_iolatency);
925d7067512SJosef Bacik }
926d7067512SJosef Bacik 
927d7067512SJosef Bacik module_init(iolatency_init);
928d7067512SJosef Bacik module_exit(iolatency_exit);
929