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