xref: /linux/block/blk-throttle.c (revision a9520cd6f2ac1fbbf206b915946534c6dddbaae2)
1e43473b7SVivek Goyal /*
2e43473b7SVivek Goyal  * Interface for controlling IO bandwidth on a request queue
3e43473b7SVivek Goyal  *
4e43473b7SVivek Goyal  * Copyright (C) 2010 Vivek Goyal <vgoyal@redhat.com>
5e43473b7SVivek Goyal  */
6e43473b7SVivek Goyal 
7e43473b7SVivek Goyal #include <linux/module.h>
8e43473b7SVivek Goyal #include <linux/slab.h>
9e43473b7SVivek Goyal #include <linux/blkdev.h>
10e43473b7SVivek Goyal #include <linux/bio.h>
11e43473b7SVivek Goyal #include <linux/blktrace_api.h>
12eea8f41cSTejun Heo #include <linux/blk-cgroup.h>
13bc9fcbf9STejun Heo #include "blk.h"
14e43473b7SVivek Goyal 
15e43473b7SVivek Goyal /* Max dispatch from a group in 1 round */
16e43473b7SVivek Goyal static int throtl_grp_quantum = 8;
17e43473b7SVivek Goyal 
18e43473b7SVivek Goyal /* Total max dispatch from all groups in one round */
19e43473b7SVivek Goyal static int throtl_quantum = 32;
20e43473b7SVivek Goyal 
21e43473b7SVivek Goyal /* Throttling is performed over 100ms slice and after that slice is renewed */
22e43473b7SVivek Goyal static unsigned long throtl_slice = HZ/10;	/* 100 ms */
23e43473b7SVivek Goyal 
243c798398STejun Heo static struct blkcg_policy blkcg_policy_throtl;
250381411eSTejun Heo 
26450adcbeSVivek Goyal /* A workqueue to queue throttle related work */
27450adcbeSVivek Goyal static struct workqueue_struct *kthrotld_workqueue;
28450adcbeSVivek Goyal 
29c5cc2070STejun Heo /*
30c5cc2070STejun Heo  * To implement hierarchical throttling, throtl_grps form a tree and bios
31c5cc2070STejun Heo  * are dispatched upwards level by level until they reach the top and get
32c5cc2070STejun Heo  * issued.  When dispatching bios from the children and local group at each
33c5cc2070STejun Heo  * level, if the bios are dispatched into a single bio_list, there's a risk
34c5cc2070STejun Heo  * of a local or child group which can queue many bios at once filling up
35c5cc2070STejun Heo  * the list starving others.
36c5cc2070STejun Heo  *
37c5cc2070STejun Heo  * To avoid such starvation, dispatched bios are queued separately
38c5cc2070STejun Heo  * according to where they came from.  When they are again dispatched to
39c5cc2070STejun Heo  * the parent, they're popped in round-robin order so that no single source
40c5cc2070STejun Heo  * hogs the dispatch window.
41c5cc2070STejun Heo  *
42c5cc2070STejun Heo  * throtl_qnode is used to keep the queued bios separated by their sources.
43c5cc2070STejun Heo  * Bios are queued to throtl_qnode which in turn is queued to
44c5cc2070STejun Heo  * throtl_service_queue and then dispatched in round-robin order.
45c5cc2070STejun Heo  *
46c5cc2070STejun Heo  * It's also used to track the reference counts on blkg's.  A qnode always
47c5cc2070STejun Heo  * belongs to a throtl_grp and gets queued on itself or the parent, so
48c5cc2070STejun Heo  * incrementing the reference of the associated throtl_grp when a qnode is
49c5cc2070STejun Heo  * queued and decrementing when dequeued is enough to keep the whole blkg
50c5cc2070STejun Heo  * tree pinned while bios are in flight.
51c5cc2070STejun Heo  */
52c5cc2070STejun Heo struct throtl_qnode {
53c5cc2070STejun Heo 	struct list_head	node;		/* service_queue->queued[] */
54c5cc2070STejun Heo 	struct bio_list		bios;		/* queued bios */
55c5cc2070STejun Heo 	struct throtl_grp	*tg;		/* tg this qnode belongs to */
56c5cc2070STejun Heo };
57c5cc2070STejun Heo 
58c9e0332eSTejun Heo struct throtl_service_queue {
5977216b04STejun Heo 	struct throtl_service_queue *parent_sq;	/* the parent service_queue */
6077216b04STejun Heo 
6173f0d49aSTejun Heo 	/*
6273f0d49aSTejun Heo 	 * Bios queued directly to this service_queue or dispatched from
6373f0d49aSTejun Heo 	 * children throtl_grp's.
6473f0d49aSTejun Heo 	 */
65c5cc2070STejun Heo 	struct list_head	queued[2];	/* throtl_qnode [READ/WRITE] */
6673f0d49aSTejun Heo 	unsigned int		nr_queued[2];	/* number of queued bios */
6773f0d49aSTejun Heo 
6873f0d49aSTejun Heo 	/*
6973f0d49aSTejun Heo 	 * RB tree of active children throtl_grp's, which are sorted by
7073f0d49aSTejun Heo 	 * their ->disptime.
7173f0d49aSTejun Heo 	 */
72c9e0332eSTejun Heo 	struct rb_root		pending_tree;	/* RB tree of active tgs */
73c9e0332eSTejun Heo 	struct rb_node		*first_pending;	/* first node in the tree */
74c9e0332eSTejun Heo 	unsigned int		nr_pending;	/* # queued in the tree */
75c9e0332eSTejun Heo 	unsigned long		first_pending_disptime;	/* disptime of the first tg */
7669df0ab0STejun Heo 	struct timer_list	pending_timer;	/* fires on first_pending_disptime */
77e43473b7SVivek Goyal };
78e43473b7SVivek Goyal 
795b2c16aaSTejun Heo enum tg_state_flags {
805b2c16aaSTejun Heo 	THROTL_TG_PENDING	= 1 << 0,	/* on parent's pending tree */
810e9f4164STejun Heo 	THROTL_TG_WAS_EMPTY	= 1 << 1,	/* bio_lists[] became non-empty */
825b2c16aaSTejun Heo };
835b2c16aaSTejun Heo 
84e43473b7SVivek Goyal #define rb_entry_tg(node)	rb_entry((node), struct throtl_grp, rb_node)
85e43473b7SVivek Goyal 
868a3d2615STejun Heo /* Per-cpu group stats */
878a3d2615STejun Heo struct tg_stats_cpu {
888a3d2615STejun Heo 	/* total bytes transferred */
898a3d2615STejun Heo 	struct blkg_rwstat		service_bytes;
908a3d2615STejun Heo 	/* total IOs serviced, post merge */
918a3d2615STejun Heo 	struct blkg_rwstat		serviced;
928a3d2615STejun Heo };
938a3d2615STejun Heo 
94e43473b7SVivek Goyal struct throtl_grp {
95f95a04afSTejun Heo 	/* must be the first member */
96f95a04afSTejun Heo 	struct blkg_policy_data pd;
97f95a04afSTejun Heo 
98c9e0332eSTejun Heo 	/* active throtl group service_queue member */
99e43473b7SVivek Goyal 	struct rb_node rb_node;
100e43473b7SVivek Goyal 
1010f3457f6STejun Heo 	/* throtl_data this group belongs to */
1020f3457f6STejun Heo 	struct throtl_data *td;
1030f3457f6STejun Heo 
10449a2f1e3STejun Heo 	/* this group's service queue */
10549a2f1e3STejun Heo 	struct throtl_service_queue service_queue;
10649a2f1e3STejun Heo 
107e43473b7SVivek Goyal 	/*
108c5cc2070STejun Heo 	 * qnode_on_self is used when bios are directly queued to this
109c5cc2070STejun Heo 	 * throtl_grp so that local bios compete fairly with bios
110c5cc2070STejun Heo 	 * dispatched from children.  qnode_on_parent is used when bios are
111c5cc2070STejun Heo 	 * dispatched from this throtl_grp into its parent and will compete
112c5cc2070STejun Heo 	 * with the sibling qnode_on_parents and the parent's
113c5cc2070STejun Heo 	 * qnode_on_self.
114c5cc2070STejun Heo 	 */
115c5cc2070STejun Heo 	struct throtl_qnode qnode_on_self[2];
116c5cc2070STejun Heo 	struct throtl_qnode qnode_on_parent[2];
117c5cc2070STejun Heo 
118c5cc2070STejun Heo 	/*
119e43473b7SVivek Goyal 	 * Dispatch time in jiffies. This is the estimated time when group
120e43473b7SVivek Goyal 	 * will unthrottle and is ready to dispatch more bio. It is used as
121e43473b7SVivek Goyal 	 * key to sort active groups in service tree.
122e43473b7SVivek Goyal 	 */
123e43473b7SVivek Goyal 	unsigned long disptime;
124e43473b7SVivek Goyal 
125e43473b7SVivek Goyal 	unsigned int flags;
126e43473b7SVivek Goyal 
127693e751eSTejun Heo 	/* are there any throtl rules between this group and td? */
128693e751eSTejun Heo 	bool has_rules[2];
129693e751eSTejun Heo 
130e43473b7SVivek Goyal 	/* bytes per second rate limits */
131e43473b7SVivek Goyal 	uint64_t bps[2];
132e43473b7SVivek Goyal 
1338e89d13fSVivek Goyal 	/* IOPS limits */
1348e89d13fSVivek Goyal 	unsigned int iops[2];
1358e89d13fSVivek Goyal 
136e43473b7SVivek Goyal 	/* Number of bytes disptached in current slice */
137e43473b7SVivek Goyal 	uint64_t bytes_disp[2];
1388e89d13fSVivek Goyal 	/* Number of bio's dispatched in current slice */
1398e89d13fSVivek Goyal 	unsigned int io_disp[2];
140e43473b7SVivek Goyal 
141e43473b7SVivek Goyal 	/* When did we start a new slice */
142e43473b7SVivek Goyal 	unsigned long slice_start[2];
143e43473b7SVivek Goyal 	unsigned long slice_end[2];
144fe071437SVivek Goyal 
1458a3d2615STejun Heo 	/* Per cpu stats pointer */
1468a3d2615STejun Heo 	struct tg_stats_cpu __percpu *stats_cpu;
147e43473b7SVivek Goyal };
148e43473b7SVivek Goyal 
149e43473b7SVivek Goyal struct throtl_data
150e43473b7SVivek Goyal {
151e43473b7SVivek Goyal 	/* service tree for active throtl groups */
152c9e0332eSTejun Heo 	struct throtl_service_queue service_queue;
153e43473b7SVivek Goyal 
154e43473b7SVivek Goyal 	struct request_queue *queue;
155e43473b7SVivek Goyal 
156e43473b7SVivek Goyal 	/* Total Number of queued bios on READ and WRITE lists */
157e43473b7SVivek Goyal 	unsigned int nr_queued[2];
158e43473b7SVivek Goyal 
159e43473b7SVivek Goyal 	/*
16002977e4aSVivek Goyal 	 * number of total undestroyed groups
161e43473b7SVivek Goyal 	 */
162e43473b7SVivek Goyal 	unsigned int nr_undestroyed_grps;
163e43473b7SVivek Goyal 
164e43473b7SVivek Goyal 	/* Work for dispatching throttled bios */
16569df0ab0STejun Heo 	struct work_struct dispatch_work;
166e43473b7SVivek Goyal };
167e43473b7SVivek Goyal 
16869df0ab0STejun Heo static void throtl_pending_timer_fn(unsigned long arg);
16969df0ab0STejun Heo 
170f95a04afSTejun Heo static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
171f95a04afSTejun Heo {
172f95a04afSTejun Heo 	return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
173f95a04afSTejun Heo }
174f95a04afSTejun Heo 
1753c798398STejun Heo static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
1760381411eSTejun Heo {
177f95a04afSTejun Heo 	return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
1780381411eSTejun Heo }
1790381411eSTejun Heo 
1803c798398STejun Heo static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
1810381411eSTejun Heo {
182f95a04afSTejun Heo 	return pd_to_blkg(&tg->pd);
1830381411eSTejun Heo }
1840381411eSTejun Heo 
18503d8e111STejun Heo static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
18603d8e111STejun Heo {
18703d8e111STejun Heo 	return blkg_to_tg(td->queue->root_blkg);
18803d8e111STejun Heo }
18903d8e111STejun Heo 
190fda6f272STejun Heo /**
191fda6f272STejun Heo  * sq_to_tg - return the throl_grp the specified service queue belongs to
192fda6f272STejun Heo  * @sq: the throtl_service_queue of interest
193fda6f272STejun Heo  *
194fda6f272STejun Heo  * Return the throtl_grp @sq belongs to.  If @sq is the top-level one
195fda6f272STejun Heo  * embedded in throtl_data, %NULL is returned.
196fda6f272STejun Heo  */
197fda6f272STejun Heo static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
198fda6f272STejun Heo {
199fda6f272STejun Heo 	if (sq && sq->parent_sq)
200fda6f272STejun Heo 		return container_of(sq, struct throtl_grp, service_queue);
201fda6f272STejun Heo 	else
202fda6f272STejun Heo 		return NULL;
203fda6f272STejun Heo }
204fda6f272STejun Heo 
205fda6f272STejun Heo /**
206fda6f272STejun Heo  * sq_to_td - return throtl_data the specified service queue belongs to
207fda6f272STejun Heo  * @sq: the throtl_service_queue of interest
208fda6f272STejun Heo  *
209fda6f272STejun Heo  * A service_queue can be embeded in either a throtl_grp or throtl_data.
210fda6f272STejun Heo  * Determine the associated throtl_data accordingly and return it.
211fda6f272STejun Heo  */
212fda6f272STejun Heo static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
213fda6f272STejun Heo {
214fda6f272STejun Heo 	struct throtl_grp *tg = sq_to_tg(sq);
215fda6f272STejun Heo 
216fda6f272STejun Heo 	if (tg)
217fda6f272STejun Heo 		return tg->td;
218fda6f272STejun Heo 	else
219fda6f272STejun Heo 		return container_of(sq, struct throtl_data, service_queue);
220fda6f272STejun Heo }
221fda6f272STejun Heo 
222fda6f272STejun Heo /**
223fda6f272STejun Heo  * throtl_log - log debug message via blktrace
224fda6f272STejun Heo  * @sq: the service_queue being reported
225fda6f272STejun Heo  * @fmt: printf format string
226fda6f272STejun Heo  * @args: printf args
227fda6f272STejun Heo  *
228fda6f272STejun Heo  * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
229fda6f272STejun Heo  * throtl_grp; otherwise, just "throtl".
230fda6f272STejun Heo  *
231fda6f272STejun Heo  * TODO: this should be made a function and name formatting should happen
232fda6f272STejun Heo  * after testing whether blktrace is enabled.
233fda6f272STejun Heo  */
234fda6f272STejun Heo #define throtl_log(sq, fmt, args...)	do {				\
235fda6f272STejun Heo 	struct throtl_grp *__tg = sq_to_tg((sq));			\
236fda6f272STejun Heo 	struct throtl_data *__td = sq_to_td((sq));			\
237fda6f272STejun Heo 									\
238fda6f272STejun Heo 	(void)__td;							\
239fda6f272STejun Heo 	if ((__tg)) {							\
24054e7ed12STejun Heo 		char __pbuf[128];					\
24154e7ed12STejun Heo 									\
242fda6f272STejun Heo 		blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf));	\
243fda6f272STejun Heo 		blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
244fda6f272STejun Heo 	} else {							\
245fda6f272STejun Heo 		blk_add_trace_msg(__td->queue, "throtl " fmt, ##args);	\
246fda6f272STejun Heo 	}								\
24754e7ed12STejun Heo } while (0)
248e43473b7SVivek Goyal 
249c5cc2070STejun Heo static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
250c5cc2070STejun Heo {
251c5cc2070STejun Heo 	INIT_LIST_HEAD(&qn->node);
252c5cc2070STejun Heo 	bio_list_init(&qn->bios);
253c5cc2070STejun Heo 	qn->tg = tg;
254c5cc2070STejun Heo }
255c5cc2070STejun Heo 
256c5cc2070STejun Heo /**
257c5cc2070STejun Heo  * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
258c5cc2070STejun Heo  * @bio: bio being added
259c5cc2070STejun Heo  * @qn: qnode to add bio to
260c5cc2070STejun Heo  * @queued: the service_queue->queued[] list @qn belongs to
261c5cc2070STejun Heo  *
262c5cc2070STejun Heo  * Add @bio to @qn and put @qn on @queued if it's not already on.
263c5cc2070STejun Heo  * @qn->tg's reference count is bumped when @qn is activated.  See the
264c5cc2070STejun Heo  * comment on top of throtl_qnode definition for details.
265c5cc2070STejun Heo  */
266c5cc2070STejun Heo static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
267c5cc2070STejun Heo 				 struct list_head *queued)
268c5cc2070STejun Heo {
269c5cc2070STejun Heo 	bio_list_add(&qn->bios, bio);
270c5cc2070STejun Heo 	if (list_empty(&qn->node)) {
271c5cc2070STejun Heo 		list_add_tail(&qn->node, queued);
272c5cc2070STejun Heo 		blkg_get(tg_to_blkg(qn->tg));
273c5cc2070STejun Heo 	}
274c5cc2070STejun Heo }
275c5cc2070STejun Heo 
276c5cc2070STejun Heo /**
277c5cc2070STejun Heo  * throtl_peek_queued - peek the first bio on a qnode list
278c5cc2070STejun Heo  * @queued: the qnode list to peek
279c5cc2070STejun Heo  */
280c5cc2070STejun Heo static struct bio *throtl_peek_queued(struct list_head *queued)
281c5cc2070STejun Heo {
282c5cc2070STejun Heo 	struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
283c5cc2070STejun Heo 	struct bio *bio;
284c5cc2070STejun Heo 
285c5cc2070STejun Heo 	if (list_empty(queued))
286c5cc2070STejun Heo 		return NULL;
287c5cc2070STejun Heo 
288c5cc2070STejun Heo 	bio = bio_list_peek(&qn->bios);
289c5cc2070STejun Heo 	WARN_ON_ONCE(!bio);
290c5cc2070STejun Heo 	return bio;
291c5cc2070STejun Heo }
292c5cc2070STejun Heo 
293c5cc2070STejun Heo /**
294c5cc2070STejun Heo  * throtl_pop_queued - pop the first bio form a qnode list
295c5cc2070STejun Heo  * @queued: the qnode list to pop a bio from
296c5cc2070STejun Heo  * @tg_to_put: optional out argument for throtl_grp to put
297c5cc2070STejun Heo  *
298c5cc2070STejun Heo  * Pop the first bio from the qnode list @queued.  After popping, the first
299c5cc2070STejun Heo  * qnode is removed from @queued if empty or moved to the end of @queued so
300c5cc2070STejun Heo  * that the popping order is round-robin.
301c5cc2070STejun Heo  *
302c5cc2070STejun Heo  * When the first qnode is removed, its associated throtl_grp should be put
303c5cc2070STejun Heo  * too.  If @tg_to_put is NULL, this function automatically puts it;
304c5cc2070STejun Heo  * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
305c5cc2070STejun Heo  * responsible for putting it.
306c5cc2070STejun Heo  */
307c5cc2070STejun Heo static struct bio *throtl_pop_queued(struct list_head *queued,
308c5cc2070STejun Heo 				     struct throtl_grp **tg_to_put)
309c5cc2070STejun Heo {
310c5cc2070STejun Heo 	struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
311c5cc2070STejun Heo 	struct bio *bio;
312c5cc2070STejun Heo 
313c5cc2070STejun Heo 	if (list_empty(queued))
314c5cc2070STejun Heo 		return NULL;
315c5cc2070STejun Heo 
316c5cc2070STejun Heo 	bio = bio_list_pop(&qn->bios);
317c5cc2070STejun Heo 	WARN_ON_ONCE(!bio);
318c5cc2070STejun Heo 
319c5cc2070STejun Heo 	if (bio_list_empty(&qn->bios)) {
320c5cc2070STejun Heo 		list_del_init(&qn->node);
321c5cc2070STejun Heo 		if (tg_to_put)
322c5cc2070STejun Heo 			*tg_to_put = qn->tg;
323c5cc2070STejun Heo 		else
324c5cc2070STejun Heo 			blkg_put(tg_to_blkg(qn->tg));
325c5cc2070STejun Heo 	} else {
326c5cc2070STejun Heo 		list_move_tail(&qn->node, queued);
327c5cc2070STejun Heo 	}
328c5cc2070STejun Heo 
329c5cc2070STejun Heo 	return bio;
330c5cc2070STejun Heo }
331c5cc2070STejun Heo 
33249a2f1e3STejun Heo /* init a service_queue, assumes the caller zeroed it */
333b2ce2643STejun Heo static void throtl_service_queue_init(struct throtl_service_queue *sq)
33449a2f1e3STejun Heo {
335c5cc2070STejun Heo 	INIT_LIST_HEAD(&sq->queued[0]);
336c5cc2070STejun Heo 	INIT_LIST_HEAD(&sq->queued[1]);
33749a2f1e3STejun Heo 	sq->pending_tree = RB_ROOT;
33869df0ab0STejun Heo 	setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
33969df0ab0STejun Heo 		    (unsigned long)sq);
34069df0ab0STejun Heo }
34169df0ab0STejun Heo 
342001bea73STejun Heo static struct blkg_policy_data *throtl_pd_alloc(gfp_t gfp, int node)
343001bea73STejun Heo {
3444fb72036STejun Heo 	struct throtl_grp *tg;
345b2ce2643STejun Heo 	int rw, cpu;
3464fb72036STejun Heo 
3474fb72036STejun Heo 	tg = kzalloc_node(sizeof(*tg), gfp, node);
3484fb72036STejun Heo 	if (!tg)
3494fb72036STejun Heo 		return NULL;
3504fb72036STejun Heo 
3514fb72036STejun Heo 	tg->stats_cpu = alloc_percpu_gfp(struct tg_stats_cpu, gfp);
3524fb72036STejun Heo 	if (!tg->stats_cpu) {
3534fb72036STejun Heo 		kfree(tg);
3544fb72036STejun Heo 		return NULL;
3554fb72036STejun Heo 	}
3564fb72036STejun Heo 
357b2ce2643STejun Heo 	throtl_service_queue_init(&tg->service_queue);
358b2ce2643STejun Heo 
359b2ce2643STejun Heo 	for (rw = READ; rw <= WRITE; rw++) {
360b2ce2643STejun Heo 		throtl_qnode_init(&tg->qnode_on_self[rw], tg);
361b2ce2643STejun Heo 		throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
362b2ce2643STejun Heo 	}
363b2ce2643STejun Heo 
364b2ce2643STejun Heo 	RB_CLEAR_NODE(&tg->rb_node);
365b2ce2643STejun Heo 	tg->bps[READ] = -1;
366b2ce2643STejun Heo 	tg->bps[WRITE] = -1;
367b2ce2643STejun Heo 	tg->iops[READ] = -1;
368b2ce2643STejun Heo 	tg->iops[WRITE] = -1;
369b2ce2643STejun Heo 
3704fb72036STejun Heo 	for_each_possible_cpu(cpu) {
3714fb72036STejun Heo 		struct tg_stats_cpu *stats_cpu = per_cpu_ptr(tg->stats_cpu, cpu);
3724fb72036STejun Heo 
3734fb72036STejun Heo 		blkg_rwstat_init(&stats_cpu->service_bytes);
3744fb72036STejun Heo 		blkg_rwstat_init(&stats_cpu->serviced);
3754fb72036STejun Heo 	}
3764fb72036STejun Heo 
3774fb72036STejun Heo 	return &tg->pd;
378001bea73STejun Heo }
379001bea73STejun Heo 
380a9520cd6STejun Heo static void throtl_pd_init(struct blkg_policy_data *pd)
381a29a171eSVivek Goyal {
382a9520cd6STejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
383a9520cd6STejun Heo 	struct blkcg_gq *blkg = tg_to_blkg(tg);
38477216b04STejun Heo 	struct throtl_data *td = blkg->q->td;
385b2ce2643STejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
386cd1604faSTejun Heo 
3879138125bSTejun Heo 	/*
388aa6ec29bSTejun Heo 	 * If on the default hierarchy, we switch to properly hierarchical
3899138125bSTejun Heo 	 * behavior where limits on a given throtl_grp are applied to the
3909138125bSTejun Heo 	 * whole subtree rather than just the group itself.  e.g. If 16M
3919138125bSTejun Heo 	 * read_bps limit is set on the root group, the whole system can't
3929138125bSTejun Heo 	 * exceed 16M for the device.
3939138125bSTejun Heo 	 *
394aa6ec29bSTejun Heo 	 * If not on the default hierarchy, the broken flat hierarchy
3959138125bSTejun Heo 	 * behavior is retained where all throtl_grps are treated as if
3969138125bSTejun Heo 	 * they're all separate root groups right below throtl_data.
3979138125bSTejun Heo 	 * Limits of a group don't interact with limits of other groups
3989138125bSTejun Heo 	 * regardless of the position of the group in the hierarchy.
3999138125bSTejun Heo 	 */
400b2ce2643STejun Heo 	sq->parent_sq = &td->service_queue;
401aa6ec29bSTejun Heo 	if (cgroup_on_dfl(blkg->blkcg->css.cgroup) && blkg->parent)
402b2ce2643STejun Heo 		sq->parent_sq = &blkg_to_tg(blkg->parent)->service_queue;
40377216b04STejun Heo 	tg->td = td;
4048a3d2615STejun Heo }
4058a3d2615STejun Heo 
406693e751eSTejun Heo /*
407693e751eSTejun Heo  * Set has_rules[] if @tg or any of its parents have limits configured.
408693e751eSTejun Heo  * This doesn't require walking up to the top of the hierarchy as the
409693e751eSTejun Heo  * parent's has_rules[] is guaranteed to be correct.
410693e751eSTejun Heo  */
411693e751eSTejun Heo static void tg_update_has_rules(struct throtl_grp *tg)
412693e751eSTejun Heo {
413693e751eSTejun Heo 	struct throtl_grp *parent_tg = sq_to_tg(tg->service_queue.parent_sq);
414693e751eSTejun Heo 	int rw;
415693e751eSTejun Heo 
416693e751eSTejun Heo 	for (rw = READ; rw <= WRITE; rw++)
417693e751eSTejun Heo 		tg->has_rules[rw] = (parent_tg && parent_tg->has_rules[rw]) ||
418693e751eSTejun Heo 				    (tg->bps[rw] != -1 || tg->iops[rw] != -1);
419693e751eSTejun Heo }
420693e751eSTejun Heo 
421a9520cd6STejun Heo static void throtl_pd_online(struct blkg_policy_data *pd)
422693e751eSTejun Heo {
423693e751eSTejun Heo 	/*
424693e751eSTejun Heo 	 * We don't want new groups to escape the limits of its ancestors.
425693e751eSTejun Heo 	 * Update has_rules[] after a new group is brought online.
426693e751eSTejun Heo 	 */
427a9520cd6STejun Heo 	tg_update_has_rules(pd_to_tg(pd));
428693e751eSTejun Heo }
429693e751eSTejun Heo 
430001bea73STejun Heo static void throtl_pd_free(struct blkg_policy_data *pd)
431001bea73STejun Heo {
4324fb72036STejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
4334fb72036STejun Heo 
434b2ce2643STejun Heo 	del_timer_sync(&tg->service_queue.pending_timer);
4354fb72036STejun Heo 	free_percpu(tg->stats_cpu);
4364fb72036STejun Heo 	kfree(tg);
437001bea73STejun Heo }
438001bea73STejun Heo 
439a9520cd6STejun Heo static void throtl_pd_reset_stats(struct blkg_policy_data *pd)
4408a3d2615STejun Heo {
441a9520cd6STejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
4428a3d2615STejun Heo 	int cpu;
4438a3d2615STejun Heo 
4448a3d2615STejun Heo 	for_each_possible_cpu(cpu) {
4458a3d2615STejun Heo 		struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
4468a3d2615STejun Heo 
4478a3d2615STejun Heo 		blkg_rwstat_reset(&sc->service_bytes);
4488a3d2615STejun Heo 		blkg_rwstat_reset(&sc->serviced);
4498a3d2615STejun Heo 	}
450a29a171eSVivek Goyal }
451a29a171eSVivek Goyal 
4523c798398STejun Heo static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
4533c798398STejun Heo 					   struct blkcg *blkcg)
454e43473b7SVivek Goyal {
455e43473b7SVivek Goyal 	/*
4563c798398STejun Heo 	 * This is the common case when there are no blkcgs.  Avoid lookup
4573c798398STejun Heo 	 * in this case
458be2c6b19SVivek Goyal 	 */
4593c798398STejun Heo 	if (blkcg == &blkcg_root)
46003d8e111STejun Heo 		return td_root_tg(td);
461e43473b7SVivek Goyal 
462e8989faeSTejun Heo 	return blkg_to_tg(blkg_lookup(blkcg, td->queue));
463e43473b7SVivek Goyal }
464e43473b7SVivek Goyal 
465cd1604faSTejun Heo static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
4663c798398STejun Heo 						  struct blkcg *blkcg)
467e43473b7SVivek Goyal {
468f469a7b4SVivek Goyal 	struct request_queue *q = td->queue;
469cd1604faSTejun Heo 	struct throtl_grp *tg = NULL;
4700a5a7d0eSTejun Heo 
471f469a7b4SVivek Goyal 	/*
4723c798398STejun Heo 	 * This is the common case when there are no blkcgs.  Avoid lookup
4733c798398STejun Heo 	 * in this case
474f469a7b4SVivek Goyal 	 */
4753c798398STejun Heo 	if (blkcg == &blkcg_root) {
47603d8e111STejun Heo 		tg = td_root_tg(td);
477cd1604faSTejun Heo 	} else {
4783c798398STejun Heo 		struct blkcg_gq *blkg;
479cd1604faSTejun Heo 
4803c96cb32STejun Heo 		blkg = blkg_lookup_create(blkcg, q);
481cd1604faSTejun Heo 
482cd1604faSTejun Heo 		/* if %NULL and @q is alive, fall back to root_tg */
483cd1604faSTejun Heo 		if (!IS_ERR(blkg))
4840381411eSTejun Heo 			tg = blkg_to_tg(blkg);
4853f3299d5SBart Van Assche 		else if (!blk_queue_dying(q))
48603d8e111STejun Heo 			tg = td_root_tg(td);
487f469a7b4SVivek Goyal 	}
488f469a7b4SVivek Goyal 
489e43473b7SVivek Goyal 	return tg;
490e43473b7SVivek Goyal }
491e43473b7SVivek Goyal 
4920049af73STejun Heo static struct throtl_grp *
4930049af73STejun Heo throtl_rb_first(struct throtl_service_queue *parent_sq)
494e43473b7SVivek Goyal {
495e43473b7SVivek Goyal 	/* Service tree is empty */
4960049af73STejun Heo 	if (!parent_sq->nr_pending)
497e43473b7SVivek Goyal 		return NULL;
498e43473b7SVivek Goyal 
4990049af73STejun Heo 	if (!parent_sq->first_pending)
5000049af73STejun Heo 		parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
501e43473b7SVivek Goyal 
5020049af73STejun Heo 	if (parent_sq->first_pending)
5030049af73STejun Heo 		return rb_entry_tg(parent_sq->first_pending);
504e43473b7SVivek Goyal 
505e43473b7SVivek Goyal 	return NULL;
506e43473b7SVivek Goyal }
507e43473b7SVivek Goyal 
508e43473b7SVivek Goyal static void rb_erase_init(struct rb_node *n, struct rb_root *root)
509e43473b7SVivek Goyal {
510e43473b7SVivek Goyal 	rb_erase(n, root);
511e43473b7SVivek Goyal 	RB_CLEAR_NODE(n);
512e43473b7SVivek Goyal }
513e43473b7SVivek Goyal 
5140049af73STejun Heo static void throtl_rb_erase(struct rb_node *n,
5150049af73STejun Heo 			    struct throtl_service_queue *parent_sq)
516e43473b7SVivek Goyal {
5170049af73STejun Heo 	if (parent_sq->first_pending == n)
5180049af73STejun Heo 		parent_sq->first_pending = NULL;
5190049af73STejun Heo 	rb_erase_init(n, &parent_sq->pending_tree);
5200049af73STejun Heo 	--parent_sq->nr_pending;
521e43473b7SVivek Goyal }
522e43473b7SVivek Goyal 
5230049af73STejun Heo static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
524e43473b7SVivek Goyal {
525e43473b7SVivek Goyal 	struct throtl_grp *tg;
526e43473b7SVivek Goyal 
5270049af73STejun Heo 	tg = throtl_rb_first(parent_sq);
528e43473b7SVivek Goyal 	if (!tg)
529e43473b7SVivek Goyal 		return;
530e43473b7SVivek Goyal 
5310049af73STejun Heo 	parent_sq->first_pending_disptime = tg->disptime;
532e43473b7SVivek Goyal }
533e43473b7SVivek Goyal 
53477216b04STejun Heo static void tg_service_queue_add(struct throtl_grp *tg)
535e43473b7SVivek Goyal {
53677216b04STejun Heo 	struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
5370049af73STejun Heo 	struct rb_node **node = &parent_sq->pending_tree.rb_node;
538e43473b7SVivek Goyal 	struct rb_node *parent = NULL;
539e43473b7SVivek Goyal 	struct throtl_grp *__tg;
540e43473b7SVivek Goyal 	unsigned long key = tg->disptime;
541e43473b7SVivek Goyal 	int left = 1;
542e43473b7SVivek Goyal 
543e43473b7SVivek Goyal 	while (*node != NULL) {
544e43473b7SVivek Goyal 		parent = *node;
545e43473b7SVivek Goyal 		__tg = rb_entry_tg(parent);
546e43473b7SVivek Goyal 
547e43473b7SVivek Goyal 		if (time_before(key, __tg->disptime))
548e43473b7SVivek Goyal 			node = &parent->rb_left;
549e43473b7SVivek Goyal 		else {
550e43473b7SVivek Goyal 			node = &parent->rb_right;
551e43473b7SVivek Goyal 			left = 0;
552e43473b7SVivek Goyal 		}
553e43473b7SVivek Goyal 	}
554e43473b7SVivek Goyal 
555e43473b7SVivek Goyal 	if (left)
5560049af73STejun Heo 		parent_sq->first_pending = &tg->rb_node;
557e43473b7SVivek Goyal 
558e43473b7SVivek Goyal 	rb_link_node(&tg->rb_node, parent, node);
5590049af73STejun Heo 	rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
560e43473b7SVivek Goyal }
561e43473b7SVivek Goyal 
56277216b04STejun Heo static void __throtl_enqueue_tg(struct throtl_grp *tg)
563e43473b7SVivek Goyal {
56477216b04STejun Heo 	tg_service_queue_add(tg);
5655b2c16aaSTejun Heo 	tg->flags |= THROTL_TG_PENDING;
56677216b04STejun Heo 	tg->service_queue.parent_sq->nr_pending++;
567e43473b7SVivek Goyal }
568e43473b7SVivek Goyal 
56977216b04STejun Heo static void throtl_enqueue_tg(struct throtl_grp *tg)
570e43473b7SVivek Goyal {
5715b2c16aaSTejun Heo 	if (!(tg->flags & THROTL_TG_PENDING))
57277216b04STejun Heo 		__throtl_enqueue_tg(tg);
573e43473b7SVivek Goyal }
574e43473b7SVivek Goyal 
57577216b04STejun Heo static void __throtl_dequeue_tg(struct throtl_grp *tg)
576e43473b7SVivek Goyal {
57777216b04STejun Heo 	throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
5785b2c16aaSTejun Heo 	tg->flags &= ~THROTL_TG_PENDING;
579e43473b7SVivek Goyal }
580e43473b7SVivek Goyal 
58177216b04STejun Heo static void throtl_dequeue_tg(struct throtl_grp *tg)
582e43473b7SVivek Goyal {
5835b2c16aaSTejun Heo 	if (tg->flags & THROTL_TG_PENDING)
58477216b04STejun Heo 		__throtl_dequeue_tg(tg);
585e43473b7SVivek Goyal }
586e43473b7SVivek Goyal 
587a9131a27STejun Heo /* Call with queue lock held */
58869df0ab0STejun Heo static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
58969df0ab0STejun Heo 					  unsigned long expires)
590a9131a27STejun Heo {
59169df0ab0STejun Heo 	mod_timer(&sq->pending_timer, expires);
59269df0ab0STejun Heo 	throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
59369df0ab0STejun Heo 		   expires - jiffies, jiffies);
594a9131a27STejun Heo }
595a9131a27STejun Heo 
5967f52f98cSTejun Heo /**
5977f52f98cSTejun Heo  * throtl_schedule_next_dispatch - schedule the next dispatch cycle
5987f52f98cSTejun Heo  * @sq: the service_queue to schedule dispatch for
5997f52f98cSTejun Heo  * @force: force scheduling
6007f52f98cSTejun Heo  *
6017f52f98cSTejun Heo  * Arm @sq->pending_timer so that the next dispatch cycle starts on the
6027f52f98cSTejun Heo  * dispatch time of the first pending child.  Returns %true if either timer
6037f52f98cSTejun Heo  * is armed or there's no pending child left.  %false if the current
6047f52f98cSTejun Heo  * dispatch window is still open and the caller should continue
6057f52f98cSTejun Heo  * dispatching.
6067f52f98cSTejun Heo  *
6077f52f98cSTejun Heo  * If @force is %true, the dispatch timer is always scheduled and this
6087f52f98cSTejun Heo  * function is guaranteed to return %true.  This is to be used when the
6097f52f98cSTejun Heo  * caller can't dispatch itself and needs to invoke pending_timer
6107f52f98cSTejun Heo  * unconditionally.  Note that forced scheduling is likely to induce short
6117f52f98cSTejun Heo  * delay before dispatch starts even if @sq->first_pending_disptime is not
6127f52f98cSTejun Heo  * in the future and thus shouldn't be used in hot paths.
6137f52f98cSTejun Heo  */
6147f52f98cSTejun Heo static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
6157f52f98cSTejun Heo 					  bool force)
616e43473b7SVivek Goyal {
6176a525600STejun Heo 	/* any pending children left? */
618c9e0332eSTejun Heo 	if (!sq->nr_pending)
6197f52f98cSTejun Heo 		return true;
620e43473b7SVivek Goyal 
621c9e0332eSTejun Heo 	update_min_dispatch_time(sq);
622e43473b7SVivek Goyal 
62369df0ab0STejun Heo 	/* is the next dispatch time in the future? */
6247f52f98cSTejun Heo 	if (force || time_after(sq->first_pending_disptime, jiffies)) {
62569df0ab0STejun Heo 		throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
6267f52f98cSTejun Heo 		return true;
62769df0ab0STejun Heo 	}
62869df0ab0STejun Heo 
6297f52f98cSTejun Heo 	/* tell the caller to continue dispatching */
6307f52f98cSTejun Heo 	return false;
631e43473b7SVivek Goyal }
632e43473b7SVivek Goyal 
63332ee5bc4SVivek Goyal static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
63432ee5bc4SVivek Goyal 		bool rw, unsigned long start)
63532ee5bc4SVivek Goyal {
63632ee5bc4SVivek Goyal 	tg->bytes_disp[rw] = 0;
63732ee5bc4SVivek Goyal 	tg->io_disp[rw] = 0;
63832ee5bc4SVivek Goyal 
63932ee5bc4SVivek Goyal 	/*
64032ee5bc4SVivek Goyal 	 * Previous slice has expired. We must have trimmed it after last
64132ee5bc4SVivek Goyal 	 * bio dispatch. That means since start of last slice, we never used
64232ee5bc4SVivek Goyal 	 * that bandwidth. Do try to make use of that bandwidth while giving
64332ee5bc4SVivek Goyal 	 * credit.
64432ee5bc4SVivek Goyal 	 */
64532ee5bc4SVivek Goyal 	if (time_after_eq(start, tg->slice_start[rw]))
64632ee5bc4SVivek Goyal 		tg->slice_start[rw] = start;
64732ee5bc4SVivek Goyal 
64832ee5bc4SVivek Goyal 	tg->slice_end[rw] = jiffies + throtl_slice;
64932ee5bc4SVivek Goyal 	throtl_log(&tg->service_queue,
65032ee5bc4SVivek Goyal 		   "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
65132ee5bc4SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
65232ee5bc4SVivek Goyal 		   tg->slice_end[rw], jiffies);
65332ee5bc4SVivek Goyal }
65432ee5bc4SVivek Goyal 
6550f3457f6STejun Heo static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
656e43473b7SVivek Goyal {
657e43473b7SVivek Goyal 	tg->bytes_disp[rw] = 0;
6588e89d13fSVivek Goyal 	tg->io_disp[rw] = 0;
659e43473b7SVivek Goyal 	tg->slice_start[rw] = jiffies;
660e43473b7SVivek Goyal 	tg->slice_end[rw] = jiffies + throtl_slice;
661fda6f272STejun Heo 	throtl_log(&tg->service_queue,
662fda6f272STejun Heo 		   "[%c] new slice start=%lu end=%lu jiffies=%lu",
663e43473b7SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
664e43473b7SVivek Goyal 		   tg->slice_end[rw], jiffies);
665e43473b7SVivek Goyal }
666e43473b7SVivek Goyal 
6670f3457f6STejun Heo static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
6680f3457f6STejun Heo 					unsigned long jiffy_end)
669d1ae8ffdSVivek Goyal {
670d1ae8ffdSVivek Goyal 	tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
671d1ae8ffdSVivek Goyal }
672d1ae8ffdSVivek Goyal 
6730f3457f6STejun Heo static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
6740f3457f6STejun Heo 				       unsigned long jiffy_end)
675e43473b7SVivek Goyal {
676e43473b7SVivek Goyal 	tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
677fda6f272STejun Heo 	throtl_log(&tg->service_queue,
678fda6f272STejun Heo 		   "[%c] extend slice start=%lu end=%lu jiffies=%lu",
679e43473b7SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
680e43473b7SVivek Goyal 		   tg->slice_end[rw], jiffies);
681e43473b7SVivek Goyal }
682e43473b7SVivek Goyal 
683e43473b7SVivek Goyal /* Determine if previously allocated or extended slice is complete or not */
6840f3457f6STejun Heo static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
685e43473b7SVivek Goyal {
686e43473b7SVivek Goyal 	if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
6875cf8c227SFabian Frederick 		return false;
688e43473b7SVivek Goyal 
689e43473b7SVivek Goyal 	return 1;
690e43473b7SVivek Goyal }
691e43473b7SVivek Goyal 
692e43473b7SVivek Goyal /* Trim the used slices and adjust slice start accordingly */
6930f3457f6STejun Heo static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
694e43473b7SVivek Goyal {
6953aad5d3eSVivek Goyal 	unsigned long nr_slices, time_elapsed, io_trim;
6963aad5d3eSVivek Goyal 	u64 bytes_trim, tmp;
697e43473b7SVivek Goyal 
698e43473b7SVivek Goyal 	BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
699e43473b7SVivek Goyal 
700e43473b7SVivek Goyal 	/*
701e43473b7SVivek Goyal 	 * If bps are unlimited (-1), then time slice don't get
702e43473b7SVivek Goyal 	 * renewed. Don't try to trim the slice if slice is used. A new
703e43473b7SVivek Goyal 	 * slice will start when appropriate.
704e43473b7SVivek Goyal 	 */
7050f3457f6STejun Heo 	if (throtl_slice_used(tg, rw))
706e43473b7SVivek Goyal 		return;
707e43473b7SVivek Goyal 
708d1ae8ffdSVivek Goyal 	/*
709d1ae8ffdSVivek Goyal 	 * A bio has been dispatched. Also adjust slice_end. It might happen
710d1ae8ffdSVivek Goyal 	 * that initially cgroup limit was very low resulting in high
711d1ae8ffdSVivek Goyal 	 * slice_end, but later limit was bumped up and bio was dispached
712d1ae8ffdSVivek Goyal 	 * sooner, then we need to reduce slice_end. A high bogus slice_end
713d1ae8ffdSVivek Goyal 	 * is bad because it does not allow new slice to start.
714d1ae8ffdSVivek Goyal 	 */
715d1ae8ffdSVivek Goyal 
7160f3457f6STejun Heo 	throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
717d1ae8ffdSVivek Goyal 
718e43473b7SVivek Goyal 	time_elapsed = jiffies - tg->slice_start[rw];
719e43473b7SVivek Goyal 
720e43473b7SVivek Goyal 	nr_slices = time_elapsed / throtl_slice;
721e43473b7SVivek Goyal 
722e43473b7SVivek Goyal 	if (!nr_slices)
723e43473b7SVivek Goyal 		return;
7243aad5d3eSVivek Goyal 	tmp = tg->bps[rw] * throtl_slice * nr_slices;
7253aad5d3eSVivek Goyal 	do_div(tmp, HZ);
7263aad5d3eSVivek Goyal 	bytes_trim = tmp;
727e43473b7SVivek Goyal 
7288e89d13fSVivek Goyal 	io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
729e43473b7SVivek Goyal 
7308e89d13fSVivek Goyal 	if (!bytes_trim && !io_trim)
731e43473b7SVivek Goyal 		return;
732e43473b7SVivek Goyal 
733e43473b7SVivek Goyal 	if (tg->bytes_disp[rw] >= bytes_trim)
734e43473b7SVivek Goyal 		tg->bytes_disp[rw] -= bytes_trim;
735e43473b7SVivek Goyal 	else
736e43473b7SVivek Goyal 		tg->bytes_disp[rw] = 0;
737e43473b7SVivek Goyal 
7388e89d13fSVivek Goyal 	if (tg->io_disp[rw] >= io_trim)
7398e89d13fSVivek Goyal 		tg->io_disp[rw] -= io_trim;
7408e89d13fSVivek Goyal 	else
7418e89d13fSVivek Goyal 		tg->io_disp[rw] = 0;
7428e89d13fSVivek Goyal 
743e43473b7SVivek Goyal 	tg->slice_start[rw] += nr_slices * throtl_slice;
744e43473b7SVivek Goyal 
745fda6f272STejun Heo 	throtl_log(&tg->service_queue,
746fda6f272STejun Heo 		   "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
7478e89d13fSVivek Goyal 		   rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
748e43473b7SVivek Goyal 		   tg->slice_start[rw], tg->slice_end[rw], jiffies);
749e43473b7SVivek Goyal }
750e43473b7SVivek Goyal 
7510f3457f6STejun Heo static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
7520f3457f6STejun Heo 				  unsigned long *wait)
753e43473b7SVivek Goyal {
754e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
7558e89d13fSVivek Goyal 	unsigned int io_allowed;
756e43473b7SVivek Goyal 	unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
757c49c06e4SVivek Goyal 	u64 tmp;
758e43473b7SVivek Goyal 
7598e89d13fSVivek Goyal 	jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
760e43473b7SVivek Goyal 
7618e89d13fSVivek Goyal 	/* Slice has just started. Consider one slice interval */
7628e89d13fSVivek Goyal 	if (!jiffy_elapsed)
7638e89d13fSVivek Goyal 		jiffy_elapsed_rnd = throtl_slice;
7648e89d13fSVivek Goyal 
7658e89d13fSVivek Goyal 	jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
7668e89d13fSVivek Goyal 
767c49c06e4SVivek Goyal 	/*
768c49c06e4SVivek Goyal 	 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
769c49c06e4SVivek Goyal 	 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
770c49c06e4SVivek Goyal 	 * will allow dispatch after 1 second and after that slice should
771c49c06e4SVivek Goyal 	 * have been trimmed.
772c49c06e4SVivek Goyal 	 */
773c49c06e4SVivek Goyal 
774c49c06e4SVivek Goyal 	tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
775c49c06e4SVivek Goyal 	do_div(tmp, HZ);
776c49c06e4SVivek Goyal 
777c49c06e4SVivek Goyal 	if (tmp > UINT_MAX)
778c49c06e4SVivek Goyal 		io_allowed = UINT_MAX;
779c49c06e4SVivek Goyal 	else
780c49c06e4SVivek Goyal 		io_allowed = tmp;
7818e89d13fSVivek Goyal 
7828e89d13fSVivek Goyal 	if (tg->io_disp[rw] + 1 <= io_allowed) {
783e43473b7SVivek Goyal 		if (wait)
784e43473b7SVivek Goyal 			*wait = 0;
7855cf8c227SFabian Frederick 		return true;
786e43473b7SVivek Goyal 	}
787e43473b7SVivek Goyal 
7888e89d13fSVivek Goyal 	/* Calc approx time to dispatch */
7898e89d13fSVivek Goyal 	jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
7908e89d13fSVivek Goyal 
7918e89d13fSVivek Goyal 	if (jiffy_wait > jiffy_elapsed)
7928e89d13fSVivek Goyal 		jiffy_wait = jiffy_wait - jiffy_elapsed;
7938e89d13fSVivek Goyal 	else
7948e89d13fSVivek Goyal 		jiffy_wait = 1;
7958e89d13fSVivek Goyal 
7968e89d13fSVivek Goyal 	if (wait)
7978e89d13fSVivek Goyal 		*wait = jiffy_wait;
7988e89d13fSVivek Goyal 	return 0;
799e43473b7SVivek Goyal }
800e43473b7SVivek Goyal 
8010f3457f6STejun Heo static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
8020f3457f6STejun Heo 				 unsigned long *wait)
8038e89d13fSVivek Goyal {
8048e89d13fSVivek Goyal 	bool rw = bio_data_dir(bio);
8053aad5d3eSVivek Goyal 	u64 bytes_allowed, extra_bytes, tmp;
8068e89d13fSVivek Goyal 	unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
8078e89d13fSVivek Goyal 
808e43473b7SVivek Goyal 	jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
809e43473b7SVivek Goyal 
810e43473b7SVivek Goyal 	/* Slice has just started. Consider one slice interval */
811e43473b7SVivek Goyal 	if (!jiffy_elapsed)
812e43473b7SVivek Goyal 		jiffy_elapsed_rnd = throtl_slice;
813e43473b7SVivek Goyal 
814e43473b7SVivek Goyal 	jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
815e43473b7SVivek Goyal 
8165e901a2bSVivek Goyal 	tmp = tg->bps[rw] * jiffy_elapsed_rnd;
8175e901a2bSVivek Goyal 	do_div(tmp, HZ);
8183aad5d3eSVivek Goyal 	bytes_allowed = tmp;
819e43473b7SVivek Goyal 
8204f024f37SKent Overstreet 	if (tg->bytes_disp[rw] + bio->bi_iter.bi_size <= bytes_allowed) {
821e43473b7SVivek Goyal 		if (wait)
822e43473b7SVivek Goyal 			*wait = 0;
8235cf8c227SFabian Frederick 		return true;
824e43473b7SVivek Goyal 	}
825e43473b7SVivek Goyal 
826e43473b7SVivek Goyal 	/* Calc approx time to dispatch */
8274f024f37SKent Overstreet 	extra_bytes = tg->bytes_disp[rw] + bio->bi_iter.bi_size - bytes_allowed;
828e43473b7SVivek Goyal 	jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
829e43473b7SVivek Goyal 
830e43473b7SVivek Goyal 	if (!jiffy_wait)
831e43473b7SVivek Goyal 		jiffy_wait = 1;
832e43473b7SVivek Goyal 
833e43473b7SVivek Goyal 	/*
834e43473b7SVivek Goyal 	 * This wait time is without taking into consideration the rounding
835e43473b7SVivek Goyal 	 * up we did. Add that time also.
836e43473b7SVivek Goyal 	 */
837e43473b7SVivek Goyal 	jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
838e43473b7SVivek Goyal 	if (wait)
839e43473b7SVivek Goyal 		*wait = jiffy_wait;
8408e89d13fSVivek Goyal 	return 0;
8418e89d13fSVivek Goyal }
842e43473b7SVivek Goyal 
8438e89d13fSVivek Goyal /*
8448e89d13fSVivek Goyal  * Returns whether one can dispatch a bio or not. Also returns approx number
8458e89d13fSVivek Goyal  * of jiffies to wait before this bio is with-in IO rate and can be dispatched
8468e89d13fSVivek Goyal  */
8470f3457f6STejun Heo static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
8480f3457f6STejun Heo 			    unsigned long *wait)
8498e89d13fSVivek Goyal {
8508e89d13fSVivek Goyal 	bool rw = bio_data_dir(bio);
8518e89d13fSVivek Goyal 	unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
8528e89d13fSVivek Goyal 
8538e89d13fSVivek Goyal 	/*
8548e89d13fSVivek Goyal  	 * Currently whole state machine of group depends on first bio
8558e89d13fSVivek Goyal 	 * queued in the group bio list. So one should not be calling
8568e89d13fSVivek Goyal 	 * this function with a different bio if there are other bios
8578e89d13fSVivek Goyal 	 * queued.
8588e89d13fSVivek Goyal 	 */
85973f0d49aSTejun Heo 	BUG_ON(tg->service_queue.nr_queued[rw] &&
860c5cc2070STejun Heo 	       bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
8618e89d13fSVivek Goyal 
8628e89d13fSVivek Goyal 	/* If tg->bps = -1, then BW is unlimited */
8638e89d13fSVivek Goyal 	if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
8648e89d13fSVivek Goyal 		if (wait)
8658e89d13fSVivek Goyal 			*wait = 0;
8665cf8c227SFabian Frederick 		return true;
8678e89d13fSVivek Goyal 	}
8688e89d13fSVivek Goyal 
8698e89d13fSVivek Goyal 	/*
8708e89d13fSVivek Goyal 	 * If previous slice expired, start a new one otherwise renew/extend
8718e89d13fSVivek Goyal 	 * existing slice to make sure it is at least throtl_slice interval
8728e89d13fSVivek Goyal 	 * long since now.
8738e89d13fSVivek Goyal 	 */
8740f3457f6STejun Heo 	if (throtl_slice_used(tg, rw))
8750f3457f6STejun Heo 		throtl_start_new_slice(tg, rw);
8768e89d13fSVivek Goyal 	else {
8778e89d13fSVivek Goyal 		if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
8780f3457f6STejun Heo 			throtl_extend_slice(tg, rw, jiffies + throtl_slice);
8798e89d13fSVivek Goyal 	}
8808e89d13fSVivek Goyal 
8810f3457f6STejun Heo 	if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
8820f3457f6STejun Heo 	    tg_with_in_iops_limit(tg, bio, &iops_wait)) {
8838e89d13fSVivek Goyal 		if (wait)
8848e89d13fSVivek Goyal 			*wait = 0;
8858e89d13fSVivek Goyal 		return 1;
8868e89d13fSVivek Goyal 	}
8878e89d13fSVivek Goyal 
8888e89d13fSVivek Goyal 	max_wait = max(bps_wait, iops_wait);
8898e89d13fSVivek Goyal 
8908e89d13fSVivek Goyal 	if (wait)
8918e89d13fSVivek Goyal 		*wait = max_wait;
8928e89d13fSVivek Goyal 
8938e89d13fSVivek Goyal 	if (time_before(tg->slice_end[rw], jiffies + max_wait))
8940f3457f6STejun Heo 		throtl_extend_slice(tg, rw, jiffies + max_wait);
895e43473b7SVivek Goyal 
896e43473b7SVivek Goyal 	return 0;
897e43473b7SVivek Goyal }
898e43473b7SVivek Goyal 
8993c798398STejun Heo static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
900629ed0b1STejun Heo 					 int rw)
901629ed0b1STejun Heo {
9028a3d2615STejun Heo 	struct throtl_grp *tg = blkg_to_tg(blkg);
9038a3d2615STejun Heo 	struct tg_stats_cpu *stats_cpu;
904629ed0b1STejun Heo 	unsigned long flags;
905629ed0b1STejun Heo 
906629ed0b1STejun Heo 	/*
907629ed0b1STejun Heo 	 * Disabling interrupts to provide mutual exclusion between two
908629ed0b1STejun Heo 	 * writes on same cpu. It probably is not needed for 64bit. Not
909629ed0b1STejun Heo 	 * optimizing that case yet.
910629ed0b1STejun Heo 	 */
911629ed0b1STejun Heo 	local_irq_save(flags);
912629ed0b1STejun Heo 
9138a3d2615STejun Heo 	stats_cpu = this_cpu_ptr(tg->stats_cpu);
914629ed0b1STejun Heo 
915629ed0b1STejun Heo 	blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
916629ed0b1STejun Heo 	blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
917629ed0b1STejun Heo 
918629ed0b1STejun Heo 	local_irq_restore(flags);
919629ed0b1STejun Heo }
920629ed0b1STejun Heo 
921e43473b7SVivek Goyal static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
922e43473b7SVivek Goyal {
923e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
924e43473b7SVivek Goyal 
925e43473b7SVivek Goyal 	/* Charge the bio to the group */
9264f024f37SKent Overstreet 	tg->bytes_disp[rw] += bio->bi_iter.bi_size;
9278e89d13fSVivek Goyal 	tg->io_disp[rw]++;
928e43473b7SVivek Goyal 
9292a0f61e6STejun Heo 	/*
9302a0f61e6STejun Heo 	 * REQ_THROTTLED is used to prevent the same bio to be throttled
9312a0f61e6STejun Heo 	 * more than once as a throttled bio will go through blk-throtl the
9322a0f61e6STejun Heo 	 * second time when it eventually gets issued.  Set it when a bio
9332a0f61e6STejun Heo 	 * is being charged to a tg.
9342a0f61e6STejun Heo 	 *
9352a0f61e6STejun Heo 	 * Dispatch stats aren't recursive and each @bio should only be
9362a0f61e6STejun Heo 	 * accounted by the @tg it was originally associated with.  Let's
9372a0f61e6STejun Heo 	 * update the stats when setting REQ_THROTTLED for the first time
9382a0f61e6STejun Heo 	 * which is guaranteed to be for the @bio's original tg.
9392a0f61e6STejun Heo 	 */
9402a0f61e6STejun Heo 	if (!(bio->bi_rw & REQ_THROTTLED)) {
9412a0f61e6STejun Heo 		bio->bi_rw |= REQ_THROTTLED;
9424f024f37SKent Overstreet 		throtl_update_dispatch_stats(tg_to_blkg(tg),
9434f024f37SKent Overstreet 					     bio->bi_iter.bi_size, bio->bi_rw);
9442a0f61e6STejun Heo 	}
945e43473b7SVivek Goyal }
946e43473b7SVivek Goyal 
947c5cc2070STejun Heo /**
948c5cc2070STejun Heo  * throtl_add_bio_tg - add a bio to the specified throtl_grp
949c5cc2070STejun Heo  * @bio: bio to add
950c5cc2070STejun Heo  * @qn: qnode to use
951c5cc2070STejun Heo  * @tg: the target throtl_grp
952c5cc2070STejun Heo  *
953c5cc2070STejun Heo  * Add @bio to @tg's service_queue using @qn.  If @qn is not specified,
954c5cc2070STejun Heo  * tg->qnode_on_self[] is used.
955c5cc2070STejun Heo  */
956c5cc2070STejun Heo static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
957c5cc2070STejun Heo 			      struct throtl_grp *tg)
958e43473b7SVivek Goyal {
95973f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
960e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
961e43473b7SVivek Goyal 
962c5cc2070STejun Heo 	if (!qn)
963c5cc2070STejun Heo 		qn = &tg->qnode_on_self[rw];
964c5cc2070STejun Heo 
9650e9f4164STejun Heo 	/*
9660e9f4164STejun Heo 	 * If @tg doesn't currently have any bios queued in the same
9670e9f4164STejun Heo 	 * direction, queueing @bio can change when @tg should be
9680e9f4164STejun Heo 	 * dispatched.  Mark that @tg was empty.  This is automatically
9690e9f4164STejun Heo 	 * cleaered on the next tg_update_disptime().
9700e9f4164STejun Heo 	 */
9710e9f4164STejun Heo 	if (!sq->nr_queued[rw])
9720e9f4164STejun Heo 		tg->flags |= THROTL_TG_WAS_EMPTY;
9730e9f4164STejun Heo 
974c5cc2070STejun Heo 	throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
975c5cc2070STejun Heo 
97673f0d49aSTejun Heo 	sq->nr_queued[rw]++;
97777216b04STejun Heo 	throtl_enqueue_tg(tg);
978e43473b7SVivek Goyal }
979e43473b7SVivek Goyal 
98077216b04STejun Heo static void tg_update_disptime(struct throtl_grp *tg)
981e43473b7SVivek Goyal {
98273f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
983e43473b7SVivek Goyal 	unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
984e43473b7SVivek Goyal 	struct bio *bio;
985e43473b7SVivek Goyal 
986c5cc2070STejun Heo 	if ((bio = throtl_peek_queued(&sq->queued[READ])))
9870f3457f6STejun Heo 		tg_may_dispatch(tg, bio, &read_wait);
988e43473b7SVivek Goyal 
989c5cc2070STejun Heo 	if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
9900f3457f6STejun Heo 		tg_may_dispatch(tg, bio, &write_wait);
991e43473b7SVivek Goyal 
992e43473b7SVivek Goyal 	min_wait = min(read_wait, write_wait);
993e43473b7SVivek Goyal 	disptime = jiffies + min_wait;
994e43473b7SVivek Goyal 
995e43473b7SVivek Goyal 	/* Update dispatch time */
99677216b04STejun Heo 	throtl_dequeue_tg(tg);
997e43473b7SVivek Goyal 	tg->disptime = disptime;
99877216b04STejun Heo 	throtl_enqueue_tg(tg);
9990e9f4164STejun Heo 
10000e9f4164STejun Heo 	/* see throtl_add_bio_tg() */
10010e9f4164STejun Heo 	tg->flags &= ~THROTL_TG_WAS_EMPTY;
1002e43473b7SVivek Goyal }
1003e43473b7SVivek Goyal 
100432ee5bc4SVivek Goyal static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
100532ee5bc4SVivek Goyal 					struct throtl_grp *parent_tg, bool rw)
100632ee5bc4SVivek Goyal {
100732ee5bc4SVivek Goyal 	if (throtl_slice_used(parent_tg, rw)) {
100832ee5bc4SVivek Goyal 		throtl_start_new_slice_with_credit(parent_tg, rw,
100932ee5bc4SVivek Goyal 				child_tg->slice_start[rw]);
101032ee5bc4SVivek Goyal 	}
101132ee5bc4SVivek Goyal 
101232ee5bc4SVivek Goyal }
101332ee5bc4SVivek Goyal 
101477216b04STejun Heo static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
1015e43473b7SVivek Goyal {
101673f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
10176bc9c2b4STejun Heo 	struct throtl_service_queue *parent_sq = sq->parent_sq;
10186bc9c2b4STejun Heo 	struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
1019c5cc2070STejun Heo 	struct throtl_grp *tg_to_put = NULL;
1020e43473b7SVivek Goyal 	struct bio *bio;
1021e43473b7SVivek Goyal 
1022c5cc2070STejun Heo 	/*
1023c5cc2070STejun Heo 	 * @bio is being transferred from @tg to @parent_sq.  Popping a bio
1024c5cc2070STejun Heo 	 * from @tg may put its reference and @parent_sq might end up
1025c5cc2070STejun Heo 	 * getting released prematurely.  Remember the tg to put and put it
1026c5cc2070STejun Heo 	 * after @bio is transferred to @parent_sq.
1027c5cc2070STejun Heo 	 */
1028c5cc2070STejun Heo 	bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
102973f0d49aSTejun Heo 	sq->nr_queued[rw]--;
1030e43473b7SVivek Goyal 
1031e43473b7SVivek Goyal 	throtl_charge_bio(tg, bio);
10326bc9c2b4STejun Heo 
10336bc9c2b4STejun Heo 	/*
10346bc9c2b4STejun Heo 	 * If our parent is another tg, we just need to transfer @bio to
10356bc9c2b4STejun Heo 	 * the parent using throtl_add_bio_tg().  If our parent is
10366bc9c2b4STejun Heo 	 * @td->service_queue, @bio is ready to be issued.  Put it on its
10376bc9c2b4STejun Heo 	 * bio_lists[] and decrease total number queued.  The caller is
10386bc9c2b4STejun Heo 	 * responsible for issuing these bios.
10396bc9c2b4STejun Heo 	 */
10406bc9c2b4STejun Heo 	if (parent_tg) {
1041c5cc2070STejun Heo 		throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
104232ee5bc4SVivek Goyal 		start_parent_slice_with_credit(tg, parent_tg, rw);
10436bc9c2b4STejun Heo 	} else {
1044c5cc2070STejun Heo 		throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1045c5cc2070STejun Heo 				     &parent_sq->queued[rw]);
10466bc9c2b4STejun Heo 		BUG_ON(tg->td->nr_queued[rw] <= 0);
10476bc9c2b4STejun Heo 		tg->td->nr_queued[rw]--;
10486bc9c2b4STejun Heo 	}
1049e43473b7SVivek Goyal 
10500f3457f6STejun Heo 	throtl_trim_slice(tg, rw);
10516bc9c2b4STejun Heo 
1052c5cc2070STejun Heo 	if (tg_to_put)
1053c5cc2070STejun Heo 		blkg_put(tg_to_blkg(tg_to_put));
1054e43473b7SVivek Goyal }
1055e43473b7SVivek Goyal 
105677216b04STejun Heo static int throtl_dispatch_tg(struct throtl_grp *tg)
1057e43473b7SVivek Goyal {
105873f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
1059e43473b7SVivek Goyal 	unsigned int nr_reads = 0, nr_writes = 0;
1060e43473b7SVivek Goyal 	unsigned int max_nr_reads = throtl_grp_quantum*3/4;
1061c2f6805dSVivek Goyal 	unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
1062e43473b7SVivek Goyal 	struct bio *bio;
1063e43473b7SVivek Goyal 
1064e43473b7SVivek Goyal 	/* Try to dispatch 75% READS and 25% WRITES */
1065e43473b7SVivek Goyal 
1066c5cc2070STejun Heo 	while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
10670f3457f6STejun Heo 	       tg_may_dispatch(tg, bio, NULL)) {
1068e43473b7SVivek Goyal 
106977216b04STejun Heo 		tg_dispatch_one_bio(tg, bio_data_dir(bio));
1070e43473b7SVivek Goyal 		nr_reads++;
1071e43473b7SVivek Goyal 
1072e43473b7SVivek Goyal 		if (nr_reads >= max_nr_reads)
1073e43473b7SVivek Goyal 			break;
1074e43473b7SVivek Goyal 	}
1075e43473b7SVivek Goyal 
1076c5cc2070STejun Heo 	while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
10770f3457f6STejun Heo 	       tg_may_dispatch(tg, bio, NULL)) {
1078e43473b7SVivek Goyal 
107977216b04STejun Heo 		tg_dispatch_one_bio(tg, bio_data_dir(bio));
1080e43473b7SVivek Goyal 		nr_writes++;
1081e43473b7SVivek Goyal 
1082e43473b7SVivek Goyal 		if (nr_writes >= max_nr_writes)
1083e43473b7SVivek Goyal 			break;
1084e43473b7SVivek Goyal 	}
1085e43473b7SVivek Goyal 
1086e43473b7SVivek Goyal 	return nr_reads + nr_writes;
1087e43473b7SVivek Goyal }
1088e43473b7SVivek Goyal 
1089651930bcSTejun Heo static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
1090e43473b7SVivek Goyal {
1091e43473b7SVivek Goyal 	unsigned int nr_disp = 0;
1092e43473b7SVivek Goyal 
1093e43473b7SVivek Goyal 	while (1) {
109473f0d49aSTejun Heo 		struct throtl_grp *tg = throtl_rb_first(parent_sq);
109573f0d49aSTejun Heo 		struct throtl_service_queue *sq = &tg->service_queue;
1096e43473b7SVivek Goyal 
1097e43473b7SVivek Goyal 		if (!tg)
1098e43473b7SVivek Goyal 			break;
1099e43473b7SVivek Goyal 
1100e43473b7SVivek Goyal 		if (time_before(jiffies, tg->disptime))
1101e43473b7SVivek Goyal 			break;
1102e43473b7SVivek Goyal 
110377216b04STejun Heo 		throtl_dequeue_tg(tg);
1104e43473b7SVivek Goyal 
110577216b04STejun Heo 		nr_disp += throtl_dispatch_tg(tg);
1106e43473b7SVivek Goyal 
110773f0d49aSTejun Heo 		if (sq->nr_queued[0] || sq->nr_queued[1])
110877216b04STejun Heo 			tg_update_disptime(tg);
1109e43473b7SVivek Goyal 
1110e43473b7SVivek Goyal 		if (nr_disp >= throtl_quantum)
1111e43473b7SVivek Goyal 			break;
1112e43473b7SVivek Goyal 	}
1113e43473b7SVivek Goyal 
1114e43473b7SVivek Goyal 	return nr_disp;
1115e43473b7SVivek Goyal }
1116e43473b7SVivek Goyal 
11176e1a5704STejun Heo /**
11186e1a5704STejun Heo  * throtl_pending_timer_fn - timer function for service_queue->pending_timer
11196e1a5704STejun Heo  * @arg: the throtl_service_queue being serviced
11206e1a5704STejun Heo  *
11216e1a5704STejun Heo  * This timer is armed when a child throtl_grp with active bio's become
11226e1a5704STejun Heo  * pending and queued on the service_queue's pending_tree and expires when
11236e1a5704STejun Heo  * the first child throtl_grp should be dispatched.  This function
11242e48a530STejun Heo  * dispatches bio's from the children throtl_grps to the parent
11252e48a530STejun Heo  * service_queue.
11262e48a530STejun Heo  *
11272e48a530STejun Heo  * If the parent's parent is another throtl_grp, dispatching is propagated
11282e48a530STejun Heo  * by either arming its pending_timer or repeating dispatch directly.  If
11292e48a530STejun Heo  * the top-level service_tree is reached, throtl_data->dispatch_work is
11302e48a530STejun Heo  * kicked so that the ready bio's are issued.
11316e1a5704STejun Heo  */
113269df0ab0STejun Heo static void throtl_pending_timer_fn(unsigned long arg)
113369df0ab0STejun Heo {
113469df0ab0STejun Heo 	struct throtl_service_queue *sq = (void *)arg;
11352e48a530STejun Heo 	struct throtl_grp *tg = sq_to_tg(sq);
113669df0ab0STejun Heo 	struct throtl_data *td = sq_to_td(sq);
1137cb76199cSTejun Heo 	struct request_queue *q = td->queue;
11382e48a530STejun Heo 	struct throtl_service_queue *parent_sq;
11392e48a530STejun Heo 	bool dispatched;
11406e1a5704STejun Heo 	int ret;
1141e43473b7SVivek Goyal 
1142e43473b7SVivek Goyal 	spin_lock_irq(q->queue_lock);
11432e48a530STejun Heo again:
11442e48a530STejun Heo 	parent_sq = sq->parent_sq;
11452e48a530STejun Heo 	dispatched = false;
1146e43473b7SVivek Goyal 
11477f52f98cSTejun Heo 	while (true) {
1148fda6f272STejun Heo 		throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
11492e48a530STejun Heo 			   sq->nr_queued[READ] + sq->nr_queued[WRITE],
11502e48a530STejun Heo 			   sq->nr_queued[READ], sq->nr_queued[WRITE]);
1151e43473b7SVivek Goyal 
11527f52f98cSTejun Heo 		ret = throtl_select_dispatch(sq);
11537f52f98cSTejun Heo 		if (ret) {
11547f52f98cSTejun Heo 			throtl_log(sq, "bios disp=%u", ret);
11557f52f98cSTejun Heo 			dispatched = true;
1156651930bcSTejun Heo 		}
1157e43473b7SVivek Goyal 
11587f52f98cSTejun Heo 		if (throtl_schedule_next_dispatch(sq, false))
11597f52f98cSTejun Heo 			break;
11607f52f98cSTejun Heo 
11617f52f98cSTejun Heo 		/* this dispatch windows is still open, relax and repeat */
11627f52f98cSTejun Heo 		spin_unlock_irq(q->queue_lock);
11637f52f98cSTejun Heo 		cpu_relax();
11647f52f98cSTejun Heo 		spin_lock_irq(q->queue_lock);
11657f52f98cSTejun Heo 	}
11666a525600STejun Heo 
11672e48a530STejun Heo 	if (!dispatched)
11682e48a530STejun Heo 		goto out_unlock;
11696e1a5704STejun Heo 
11702e48a530STejun Heo 	if (parent_sq) {
11712e48a530STejun Heo 		/* @parent_sq is another throl_grp, propagate dispatch */
11722e48a530STejun Heo 		if (tg->flags & THROTL_TG_WAS_EMPTY) {
11732e48a530STejun Heo 			tg_update_disptime(tg);
11742e48a530STejun Heo 			if (!throtl_schedule_next_dispatch(parent_sq, false)) {
11752e48a530STejun Heo 				/* window is already open, repeat dispatching */
11762e48a530STejun Heo 				sq = parent_sq;
11772e48a530STejun Heo 				tg = sq_to_tg(sq);
11782e48a530STejun Heo 				goto again;
11792e48a530STejun Heo 			}
11802e48a530STejun Heo 		}
11812e48a530STejun Heo 	} else {
11822e48a530STejun Heo 		/* reached the top-level, queue issueing */
11832e48a530STejun Heo 		queue_work(kthrotld_workqueue, &td->dispatch_work);
11842e48a530STejun Heo 	}
11852e48a530STejun Heo out_unlock:
11866e1a5704STejun Heo 	spin_unlock_irq(q->queue_lock);
11876e1a5704STejun Heo }
11886e1a5704STejun Heo 
11896e1a5704STejun Heo /**
11906e1a5704STejun Heo  * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
11916e1a5704STejun Heo  * @work: work item being executed
11926e1a5704STejun Heo  *
11936e1a5704STejun Heo  * This function is queued for execution when bio's reach the bio_lists[]
11946e1a5704STejun Heo  * of throtl_data->service_queue.  Those bio's are ready and issued by this
11956e1a5704STejun Heo  * function.
11966e1a5704STejun Heo  */
11978876e140SFabian Frederick static void blk_throtl_dispatch_work_fn(struct work_struct *work)
11986e1a5704STejun Heo {
11996e1a5704STejun Heo 	struct throtl_data *td = container_of(work, struct throtl_data,
12006e1a5704STejun Heo 					      dispatch_work);
12016e1a5704STejun Heo 	struct throtl_service_queue *td_sq = &td->service_queue;
12026e1a5704STejun Heo 	struct request_queue *q = td->queue;
12036e1a5704STejun Heo 	struct bio_list bio_list_on_stack;
12046e1a5704STejun Heo 	struct bio *bio;
12056e1a5704STejun Heo 	struct blk_plug plug;
12066e1a5704STejun Heo 	int rw;
12076e1a5704STejun Heo 
12086e1a5704STejun Heo 	bio_list_init(&bio_list_on_stack);
12096e1a5704STejun Heo 
12106e1a5704STejun Heo 	spin_lock_irq(q->queue_lock);
1211c5cc2070STejun Heo 	for (rw = READ; rw <= WRITE; rw++)
1212c5cc2070STejun Heo 		while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1213c5cc2070STejun Heo 			bio_list_add(&bio_list_on_stack, bio);
1214e43473b7SVivek Goyal 	spin_unlock_irq(q->queue_lock);
1215e43473b7SVivek Goyal 
12166e1a5704STejun Heo 	if (!bio_list_empty(&bio_list_on_stack)) {
121769d60eb9SVivek Goyal 		blk_start_plug(&plug);
1218e43473b7SVivek Goyal 		while((bio = bio_list_pop(&bio_list_on_stack)))
1219e43473b7SVivek Goyal 			generic_make_request(bio);
122069d60eb9SVivek Goyal 		blk_finish_plug(&plug);
1221e43473b7SVivek Goyal 	}
1222e43473b7SVivek Goyal }
1223e43473b7SVivek Goyal 
1224f95a04afSTejun Heo static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1225f95a04afSTejun Heo 				struct blkg_policy_data *pd, int off)
122641b38b6dSTejun Heo {
1227f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
122841b38b6dSTejun Heo 	struct blkg_rwstat rwstat = { }, tmp;
122941b38b6dSTejun Heo 	int i, cpu;
123041b38b6dSTejun Heo 
123141b38b6dSTejun Heo 	for_each_possible_cpu(cpu) {
12328a3d2615STejun Heo 		struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
123341b38b6dSTejun Heo 
123441b38b6dSTejun Heo 		tmp = blkg_rwstat_read((void *)sc + off);
123541b38b6dSTejun Heo 		for (i = 0; i < BLKG_RWSTAT_NR; i++)
123641b38b6dSTejun Heo 			rwstat.cnt[i] += tmp.cnt[i];
123741b38b6dSTejun Heo 	}
123841b38b6dSTejun Heo 
1239f95a04afSTejun Heo 	return __blkg_prfill_rwstat(sf, pd, &rwstat);
124041b38b6dSTejun Heo }
124141b38b6dSTejun Heo 
12422da8ca82STejun Heo static int tg_print_cpu_rwstat(struct seq_file *sf, void *v)
124341b38b6dSTejun Heo {
12442da8ca82STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_cpu_rwstat,
12452da8ca82STejun Heo 			  &blkcg_policy_throtl, seq_cft(sf)->private, true);
124641b38b6dSTejun Heo 	return 0;
124741b38b6dSTejun Heo }
124841b38b6dSTejun Heo 
1249f95a04afSTejun Heo static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1250f95a04afSTejun Heo 			      int off)
125160c2bc2dSTejun Heo {
1252f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
1253f95a04afSTejun Heo 	u64 v = *(u64 *)((void *)tg + off);
125460c2bc2dSTejun Heo 
1255af133cebSTejun Heo 	if (v == -1)
125660c2bc2dSTejun Heo 		return 0;
1257f95a04afSTejun Heo 	return __blkg_prfill_u64(sf, pd, v);
125860c2bc2dSTejun Heo }
125960c2bc2dSTejun Heo 
1260f95a04afSTejun Heo static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1261f95a04afSTejun Heo 			       int off)
1262af133cebSTejun Heo {
1263f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
1264f95a04afSTejun Heo 	unsigned int v = *(unsigned int *)((void *)tg + off);
1265af133cebSTejun Heo 
1266af133cebSTejun Heo 	if (v == -1)
1267af133cebSTejun Heo 		return 0;
1268f95a04afSTejun Heo 	return __blkg_prfill_u64(sf, pd, v);
1269af133cebSTejun Heo }
1270af133cebSTejun Heo 
12712da8ca82STejun Heo static int tg_print_conf_u64(struct seq_file *sf, void *v)
127260c2bc2dSTejun Heo {
12732da8ca82STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_u64,
12742da8ca82STejun Heo 			  &blkcg_policy_throtl, seq_cft(sf)->private, false);
127560c2bc2dSTejun Heo 	return 0;
127660c2bc2dSTejun Heo }
127760c2bc2dSTejun Heo 
12782da8ca82STejun Heo static int tg_print_conf_uint(struct seq_file *sf, void *v)
1279e43473b7SVivek Goyal {
12802da8ca82STejun Heo 	blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), tg_prfill_conf_uint,
12812da8ca82STejun Heo 			  &blkcg_policy_throtl, seq_cft(sf)->private, false);
1282af133cebSTejun Heo 	return 0;
1283e43473b7SVivek Goyal }
1284e43473b7SVivek Goyal 
1285451af504STejun Heo static ssize_t tg_set_conf(struct kernfs_open_file *of,
1286451af504STejun Heo 			   char *buf, size_t nbytes, loff_t off, bool is_u64)
128760c2bc2dSTejun Heo {
1288451af504STejun Heo 	struct blkcg *blkcg = css_to_blkcg(of_css(of));
128960c2bc2dSTejun Heo 	struct blkg_conf_ctx ctx;
1290af133cebSTejun Heo 	struct throtl_grp *tg;
129169df0ab0STejun Heo 	struct throtl_service_queue *sq;
1292693e751eSTejun Heo 	struct blkcg_gq *blkg;
1293492eb21bSTejun Heo 	struct cgroup_subsys_state *pos_css;
129460c2bc2dSTejun Heo 	int ret;
129560c2bc2dSTejun Heo 
12963c798398STejun Heo 	ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
129760c2bc2dSTejun Heo 	if (ret)
129860c2bc2dSTejun Heo 		return ret;
129960c2bc2dSTejun Heo 
1300af133cebSTejun Heo 	tg = blkg_to_tg(ctx.blkg);
130169df0ab0STejun Heo 	sq = &tg->service_queue;
1302af133cebSTejun Heo 
1303af133cebSTejun Heo 	if (!ctx.v)
1304af133cebSTejun Heo 		ctx.v = -1;
1305af133cebSTejun Heo 
1306af133cebSTejun Heo 	if (is_u64)
1307451af504STejun Heo 		*(u64 *)((void *)tg + of_cft(of)->private) = ctx.v;
1308af133cebSTejun Heo 	else
1309451af504STejun Heo 		*(unsigned int *)((void *)tg + of_cft(of)->private) = ctx.v;
1310af133cebSTejun Heo 
1311fda6f272STejun Heo 	throtl_log(&tg->service_queue,
1312fda6f272STejun Heo 		   "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1313632b4493STejun Heo 		   tg->bps[READ], tg->bps[WRITE],
1314632b4493STejun Heo 		   tg->iops[READ], tg->iops[WRITE]);
1315632b4493STejun Heo 
1316632b4493STejun Heo 	/*
1317693e751eSTejun Heo 	 * Update has_rules[] flags for the updated tg's subtree.  A tg is
1318693e751eSTejun Heo 	 * considered to have rules if either the tg itself or any of its
1319693e751eSTejun Heo 	 * ancestors has rules.  This identifies groups without any
1320693e751eSTejun Heo 	 * restrictions in the whole hierarchy and allows them to bypass
1321693e751eSTejun Heo 	 * blk-throttle.
1322693e751eSTejun Heo 	 */
1323492eb21bSTejun Heo 	blkg_for_each_descendant_pre(blkg, pos_css, ctx.blkg)
1324693e751eSTejun Heo 		tg_update_has_rules(blkg_to_tg(blkg));
1325693e751eSTejun Heo 
1326693e751eSTejun Heo 	/*
1327632b4493STejun Heo 	 * We're already holding queue_lock and know @tg is valid.  Let's
1328632b4493STejun Heo 	 * apply the new config directly.
1329632b4493STejun Heo 	 *
1330632b4493STejun Heo 	 * Restart the slices for both READ and WRITES. It might happen
1331632b4493STejun Heo 	 * that a group's limit are dropped suddenly and we don't want to
1332632b4493STejun Heo 	 * account recently dispatched IO with new low rate.
1333632b4493STejun Heo 	 */
13340f3457f6STejun Heo 	throtl_start_new_slice(tg, 0);
13350f3457f6STejun Heo 	throtl_start_new_slice(tg, 1);
1336632b4493STejun Heo 
13375b2c16aaSTejun Heo 	if (tg->flags & THROTL_TG_PENDING) {
133877216b04STejun Heo 		tg_update_disptime(tg);
13397f52f98cSTejun Heo 		throtl_schedule_next_dispatch(sq->parent_sq, true);
1340632b4493STejun Heo 	}
1341af133cebSTejun Heo 
134260c2bc2dSTejun Heo 	blkg_conf_finish(&ctx);
1343451af504STejun Heo 	return nbytes;
134460c2bc2dSTejun Heo }
134560c2bc2dSTejun Heo 
1346451af504STejun Heo static ssize_t tg_set_conf_u64(struct kernfs_open_file *of,
1347451af504STejun Heo 			       char *buf, size_t nbytes, loff_t off)
134860c2bc2dSTejun Heo {
1349451af504STejun Heo 	return tg_set_conf(of, buf, nbytes, off, true);
135060c2bc2dSTejun Heo }
135160c2bc2dSTejun Heo 
1352451af504STejun Heo static ssize_t tg_set_conf_uint(struct kernfs_open_file *of,
1353451af504STejun Heo 				char *buf, size_t nbytes, loff_t off)
135460c2bc2dSTejun Heo {
1355451af504STejun Heo 	return tg_set_conf(of, buf, nbytes, off, false);
135660c2bc2dSTejun Heo }
135760c2bc2dSTejun Heo 
135860c2bc2dSTejun Heo static struct cftype throtl_files[] = {
135960c2bc2dSTejun Heo 	{
136060c2bc2dSTejun Heo 		.name = "throttle.read_bps_device",
1361af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, bps[READ]),
13622da8ca82STejun Heo 		.seq_show = tg_print_conf_u64,
1363451af504STejun Heo 		.write = tg_set_conf_u64,
136460c2bc2dSTejun Heo 	},
136560c2bc2dSTejun Heo 	{
136660c2bc2dSTejun Heo 		.name = "throttle.write_bps_device",
1367af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, bps[WRITE]),
13682da8ca82STejun Heo 		.seq_show = tg_print_conf_u64,
1369451af504STejun Heo 		.write = tg_set_conf_u64,
137060c2bc2dSTejun Heo 	},
137160c2bc2dSTejun Heo 	{
137260c2bc2dSTejun Heo 		.name = "throttle.read_iops_device",
1373af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, iops[READ]),
13742da8ca82STejun Heo 		.seq_show = tg_print_conf_uint,
1375451af504STejun Heo 		.write = tg_set_conf_uint,
137660c2bc2dSTejun Heo 	},
137760c2bc2dSTejun Heo 	{
137860c2bc2dSTejun Heo 		.name = "throttle.write_iops_device",
1379af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, iops[WRITE]),
13802da8ca82STejun Heo 		.seq_show = tg_print_conf_uint,
1381451af504STejun Heo 		.write = tg_set_conf_uint,
138260c2bc2dSTejun Heo 	},
138360c2bc2dSTejun Heo 	{
138460c2bc2dSTejun Heo 		.name = "throttle.io_service_bytes",
13855bc4afb1STejun Heo 		.private = offsetof(struct tg_stats_cpu, service_bytes),
13862da8ca82STejun Heo 		.seq_show = tg_print_cpu_rwstat,
138760c2bc2dSTejun Heo 	},
138860c2bc2dSTejun Heo 	{
138960c2bc2dSTejun Heo 		.name = "throttle.io_serviced",
13905bc4afb1STejun Heo 		.private = offsetof(struct tg_stats_cpu, serviced),
13912da8ca82STejun Heo 		.seq_show = tg_print_cpu_rwstat,
139260c2bc2dSTejun Heo 	},
139360c2bc2dSTejun Heo 	{ }	/* terminate */
139460c2bc2dSTejun Heo };
139560c2bc2dSTejun Heo 
1396da527770SVivek Goyal static void throtl_shutdown_wq(struct request_queue *q)
1397e43473b7SVivek Goyal {
1398e43473b7SVivek Goyal 	struct throtl_data *td = q->td;
1399e43473b7SVivek Goyal 
140069df0ab0STejun Heo 	cancel_work_sync(&td->dispatch_work);
1401e43473b7SVivek Goyal }
1402e43473b7SVivek Goyal 
14033c798398STejun Heo static struct blkcg_policy blkcg_policy_throtl = {
1404f9fcc2d3STejun Heo 	.cftypes		= throtl_files,
1405f9fcc2d3STejun Heo 
1406001bea73STejun Heo 	.pd_alloc_fn		= throtl_pd_alloc,
14073c798398STejun Heo 	.pd_init_fn		= throtl_pd_init,
1408693e751eSTejun Heo 	.pd_online_fn		= throtl_pd_online,
1409001bea73STejun Heo 	.pd_free_fn		= throtl_pd_free,
14103c798398STejun Heo 	.pd_reset_stats_fn	= throtl_pd_reset_stats,
1411e43473b7SVivek Goyal };
1412e43473b7SVivek Goyal 
1413bc16a4f9STejun Heo bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1414e43473b7SVivek Goyal {
1415e43473b7SVivek Goyal 	struct throtl_data *td = q->td;
1416c5cc2070STejun Heo 	struct throtl_qnode *qn = NULL;
1417e43473b7SVivek Goyal 	struct throtl_grp *tg;
141873f0d49aSTejun Heo 	struct throtl_service_queue *sq;
14190e9f4164STejun Heo 	bool rw = bio_data_dir(bio);
14203c798398STejun Heo 	struct blkcg *blkcg;
1421bc16a4f9STejun Heo 	bool throttled = false;
1422e43473b7SVivek Goyal 
14232a0f61e6STejun Heo 	/* see throtl_charge_bio() */
14242a0f61e6STejun Heo 	if (bio->bi_rw & REQ_THROTTLED)
1425bc16a4f9STejun Heo 		goto out;
1426e43473b7SVivek Goyal 
1427af75cd3cSVivek Goyal 	/*
1428af75cd3cSVivek Goyal 	 * A throtl_grp pointer retrieved under rcu can be used to access
1429af75cd3cSVivek Goyal 	 * basic fields like stats and io rates. If a group has no rules,
1430af75cd3cSVivek Goyal 	 * just update the dispatch stats in lockless manner and return.
1431af75cd3cSVivek Goyal 	 */
1432af75cd3cSVivek Goyal 	rcu_read_lock();
14333c798398STejun Heo 	blkcg = bio_blkcg(bio);
1434cd1604faSTejun Heo 	tg = throtl_lookup_tg(td, blkcg);
1435af75cd3cSVivek Goyal 	if (tg) {
1436693e751eSTejun Heo 		if (!tg->has_rules[rw]) {
1437629ed0b1STejun Heo 			throtl_update_dispatch_stats(tg_to_blkg(tg),
14384f024f37SKent Overstreet 					bio->bi_iter.bi_size, bio->bi_rw);
14392a7f1244STejun Heo 			goto out_unlock_rcu;
1440af75cd3cSVivek Goyal 		}
1441af75cd3cSVivek Goyal 	}
1442af75cd3cSVivek Goyal 
1443af75cd3cSVivek Goyal 	/*
1444af75cd3cSVivek Goyal 	 * Either group has not been allocated yet or it is not an unlimited
1445af75cd3cSVivek Goyal 	 * IO group
1446af75cd3cSVivek Goyal 	 */
1447e43473b7SVivek Goyal 	spin_lock_irq(q->queue_lock);
1448cd1604faSTejun Heo 	tg = throtl_lookup_create_tg(td, blkcg);
1449bc16a4f9STejun Heo 	if (unlikely(!tg))
1450bc16a4f9STejun Heo 		goto out_unlock;
1451f469a7b4SVivek Goyal 
145273f0d49aSTejun Heo 	sq = &tg->service_queue;
145373f0d49aSTejun Heo 
14549e660acfSTejun Heo 	while (true) {
14559e660acfSTejun Heo 		/* throtl is FIFO - if bios are already queued, should queue */
14560e9f4164STejun Heo 		if (sq->nr_queued[rw])
14579e660acfSTejun Heo 			break;
1458de701c74SVivek Goyal 
14599e660acfSTejun Heo 		/* if above limits, break to queue */
14609e660acfSTejun Heo 		if (!tg_may_dispatch(tg, bio, NULL))
14619e660acfSTejun Heo 			break;
14629e660acfSTejun Heo 
14639e660acfSTejun Heo 		/* within limits, let's charge and dispatch directly */
1464e43473b7SVivek Goyal 		throtl_charge_bio(tg, bio);
146504521db0SVivek Goyal 
146604521db0SVivek Goyal 		/*
146704521db0SVivek Goyal 		 * We need to trim slice even when bios are not being queued
146804521db0SVivek Goyal 		 * otherwise it might happen that a bio is not queued for
146904521db0SVivek Goyal 		 * a long time and slice keeps on extending and trim is not
147004521db0SVivek Goyal 		 * called for a long time. Now if limits are reduced suddenly
147104521db0SVivek Goyal 		 * we take into account all the IO dispatched so far at new
147204521db0SVivek Goyal 		 * low rate and * newly queued IO gets a really long dispatch
147304521db0SVivek Goyal 		 * time.
147404521db0SVivek Goyal 		 *
147504521db0SVivek Goyal 		 * So keep on trimming slice even if bio is not queued.
147604521db0SVivek Goyal 		 */
14770f3457f6STejun Heo 		throtl_trim_slice(tg, rw);
14789e660acfSTejun Heo 
14799e660acfSTejun Heo 		/*
14809e660acfSTejun Heo 		 * @bio passed through this layer without being throttled.
14819e660acfSTejun Heo 		 * Climb up the ladder.  If we''re already at the top, it
14829e660acfSTejun Heo 		 * can be executed directly.
14839e660acfSTejun Heo 		 */
1484c5cc2070STejun Heo 		qn = &tg->qnode_on_parent[rw];
14859e660acfSTejun Heo 		sq = sq->parent_sq;
14869e660acfSTejun Heo 		tg = sq_to_tg(sq);
14879e660acfSTejun Heo 		if (!tg)
1488bc16a4f9STejun Heo 			goto out_unlock;
1489e43473b7SVivek Goyal 	}
1490e43473b7SVivek Goyal 
14919e660acfSTejun Heo 	/* out-of-limit, queue to @tg */
1492fda6f272STejun Heo 	throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
14938e89d13fSVivek Goyal 		   rw == READ ? 'R' : 'W',
14944f024f37SKent Overstreet 		   tg->bytes_disp[rw], bio->bi_iter.bi_size, tg->bps[rw],
14958e89d13fSVivek Goyal 		   tg->io_disp[rw], tg->iops[rw],
149673f0d49aSTejun Heo 		   sq->nr_queued[READ], sq->nr_queued[WRITE]);
1497e43473b7SVivek Goyal 
1498671058fbSTejun Heo 	bio_associate_current(bio);
14996bc9c2b4STejun Heo 	tg->td->nr_queued[rw]++;
1500c5cc2070STejun Heo 	throtl_add_bio_tg(bio, qn, tg);
1501bc16a4f9STejun Heo 	throttled = true;
1502e43473b7SVivek Goyal 
15037f52f98cSTejun Heo 	/*
15047f52f98cSTejun Heo 	 * Update @tg's dispatch time and force schedule dispatch if @tg
15057f52f98cSTejun Heo 	 * was empty before @bio.  The forced scheduling isn't likely to
15067f52f98cSTejun Heo 	 * cause undue delay as @bio is likely to be dispatched directly if
15077f52f98cSTejun Heo 	 * its @tg's disptime is not in the future.
15087f52f98cSTejun Heo 	 */
15090e9f4164STejun Heo 	if (tg->flags & THROTL_TG_WAS_EMPTY) {
151077216b04STejun Heo 		tg_update_disptime(tg);
15117f52f98cSTejun Heo 		throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
1512e43473b7SVivek Goyal 	}
1513e43473b7SVivek Goyal 
1514bc16a4f9STejun Heo out_unlock:
1515e43473b7SVivek Goyal 	spin_unlock_irq(q->queue_lock);
15162a7f1244STejun Heo out_unlock_rcu:
15172a7f1244STejun Heo 	rcu_read_unlock();
1518bc16a4f9STejun Heo out:
15192a0f61e6STejun Heo 	/*
15202a0f61e6STejun Heo 	 * As multiple blk-throtls may stack in the same issue path, we
15212a0f61e6STejun Heo 	 * don't want bios to leave with the flag set.  Clear the flag if
15222a0f61e6STejun Heo 	 * being issued.
15232a0f61e6STejun Heo 	 */
15242a0f61e6STejun Heo 	if (!throttled)
15252a0f61e6STejun Heo 		bio->bi_rw &= ~REQ_THROTTLED;
1526bc16a4f9STejun Heo 	return throttled;
1527e43473b7SVivek Goyal }
1528e43473b7SVivek Goyal 
15292a12f0dcSTejun Heo /*
15302a12f0dcSTejun Heo  * Dispatch all bios from all children tg's queued on @parent_sq.  On
15312a12f0dcSTejun Heo  * return, @parent_sq is guaranteed to not have any active children tg's
15322a12f0dcSTejun Heo  * and all bios from previously active tg's are on @parent_sq->bio_lists[].
15332a12f0dcSTejun Heo  */
15342a12f0dcSTejun Heo static void tg_drain_bios(struct throtl_service_queue *parent_sq)
15352a12f0dcSTejun Heo {
15362a12f0dcSTejun Heo 	struct throtl_grp *tg;
15372a12f0dcSTejun Heo 
15382a12f0dcSTejun Heo 	while ((tg = throtl_rb_first(parent_sq))) {
15392a12f0dcSTejun Heo 		struct throtl_service_queue *sq = &tg->service_queue;
15402a12f0dcSTejun Heo 		struct bio *bio;
15412a12f0dcSTejun Heo 
15422a12f0dcSTejun Heo 		throtl_dequeue_tg(tg);
15432a12f0dcSTejun Heo 
1544c5cc2070STejun Heo 		while ((bio = throtl_peek_queued(&sq->queued[READ])))
15452a12f0dcSTejun Heo 			tg_dispatch_one_bio(tg, bio_data_dir(bio));
1546c5cc2070STejun Heo 		while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
15472a12f0dcSTejun Heo 			tg_dispatch_one_bio(tg, bio_data_dir(bio));
15482a12f0dcSTejun Heo 	}
15492a12f0dcSTejun Heo }
15502a12f0dcSTejun Heo 
1551c9a929ddSTejun Heo /**
1552c9a929ddSTejun Heo  * blk_throtl_drain - drain throttled bios
1553c9a929ddSTejun Heo  * @q: request_queue to drain throttled bios for
1554c9a929ddSTejun Heo  *
1555c9a929ddSTejun Heo  * Dispatch all currently throttled bios on @q through ->make_request_fn().
1556c9a929ddSTejun Heo  */
1557c9a929ddSTejun Heo void blk_throtl_drain(struct request_queue *q)
1558c9a929ddSTejun Heo 	__releases(q->queue_lock) __acquires(q->queue_lock)
1559c9a929ddSTejun Heo {
1560c9a929ddSTejun Heo 	struct throtl_data *td = q->td;
15612a12f0dcSTejun Heo 	struct blkcg_gq *blkg;
1562492eb21bSTejun Heo 	struct cgroup_subsys_state *pos_css;
1563c9a929ddSTejun Heo 	struct bio *bio;
1564651930bcSTejun Heo 	int rw;
1565c9a929ddSTejun Heo 
15668bcb6c7dSAndi Kleen 	queue_lockdep_assert_held(q);
15672a12f0dcSTejun Heo 	rcu_read_lock();
1568c9a929ddSTejun Heo 
15692a12f0dcSTejun Heo 	/*
15702a12f0dcSTejun Heo 	 * Drain each tg while doing post-order walk on the blkg tree, so
15712a12f0dcSTejun Heo 	 * that all bios are propagated to td->service_queue.  It'd be
15722a12f0dcSTejun Heo 	 * better to walk service_queue tree directly but blkg walk is
15732a12f0dcSTejun Heo 	 * easier.
15742a12f0dcSTejun Heo 	 */
1575492eb21bSTejun Heo 	blkg_for_each_descendant_post(blkg, pos_css, td->queue->root_blkg)
15762a12f0dcSTejun Heo 		tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
157773f0d49aSTejun Heo 
15782a12f0dcSTejun Heo 	/* finally, transfer bios from top-level tg's into the td */
15792a12f0dcSTejun Heo 	tg_drain_bios(&td->service_queue);
15802a12f0dcSTejun Heo 
15812a12f0dcSTejun Heo 	rcu_read_unlock();
1582c9a929ddSTejun Heo 	spin_unlock_irq(q->queue_lock);
1583c9a929ddSTejun Heo 
15842a12f0dcSTejun Heo 	/* all bios now should be in td->service_queue, issue them */
1585651930bcSTejun Heo 	for (rw = READ; rw <= WRITE; rw++)
1586c5cc2070STejun Heo 		while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1587c5cc2070STejun Heo 						NULL)))
1588c9a929ddSTejun Heo 			generic_make_request(bio);
1589c9a929ddSTejun Heo 
1590c9a929ddSTejun Heo 	spin_lock_irq(q->queue_lock);
1591c9a929ddSTejun Heo }
1592c9a929ddSTejun Heo 
1593e43473b7SVivek Goyal int blk_throtl_init(struct request_queue *q)
1594e43473b7SVivek Goyal {
1595e43473b7SVivek Goyal 	struct throtl_data *td;
1596a2b1693bSTejun Heo 	int ret;
1597e43473b7SVivek Goyal 
1598e43473b7SVivek Goyal 	td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1599e43473b7SVivek Goyal 	if (!td)
1600e43473b7SVivek Goyal 		return -ENOMEM;
1601e43473b7SVivek Goyal 
160269df0ab0STejun Heo 	INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
1603b2ce2643STejun Heo 	throtl_service_queue_init(&td->service_queue);
1604e43473b7SVivek Goyal 
1605cd1604faSTejun Heo 	q->td = td;
160629b12589SVivek Goyal 	td->queue = q;
160702977e4aSVivek Goyal 
1608a2b1693bSTejun Heo 	/* activate policy */
16093c798398STejun Heo 	ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1610a2b1693bSTejun Heo 	if (ret)
161129b12589SVivek Goyal 		kfree(td);
1612a2b1693bSTejun Heo 	return ret;
1613e43473b7SVivek Goyal }
1614e43473b7SVivek Goyal 
1615e43473b7SVivek Goyal void blk_throtl_exit(struct request_queue *q)
1616e43473b7SVivek Goyal {
1617c875f4d0STejun Heo 	BUG_ON(!q->td);
1618da527770SVivek Goyal 	throtl_shutdown_wq(q);
16193c798398STejun Heo 	blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1620c9a929ddSTejun Heo 	kfree(q->td);
1621e43473b7SVivek Goyal }
1622e43473b7SVivek Goyal 
1623e43473b7SVivek Goyal static int __init throtl_init(void)
1624e43473b7SVivek Goyal {
1625450adcbeSVivek Goyal 	kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1626450adcbeSVivek Goyal 	if (!kthrotld_workqueue)
1627450adcbeSVivek Goyal 		panic("Failed to create kthrotld\n");
1628450adcbeSVivek Goyal 
16293c798398STejun Heo 	return blkcg_policy_register(&blkcg_policy_throtl);
1630e43473b7SVivek Goyal }
1631e43473b7SVivek Goyal 
1632e43473b7SVivek Goyal module_init(throtl_init);
1633