xref: /linux/block/blk-throttle.c (revision 32ee5bc4787dfbdb280b4d81a338dcdd55918c1e)
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>
12e43473b7SVivek Goyal #include "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 
127e43473b7SVivek Goyal 	/* bytes per second rate limits */
128e43473b7SVivek Goyal 	uint64_t bps[2];
129e43473b7SVivek Goyal 
1308e89d13fSVivek Goyal 	/* IOPS limits */
1318e89d13fSVivek Goyal 	unsigned int iops[2];
1328e89d13fSVivek Goyal 
133e43473b7SVivek Goyal 	/* Number of bytes disptached in current slice */
134e43473b7SVivek Goyal 	uint64_t bytes_disp[2];
1358e89d13fSVivek Goyal 	/* Number of bio's dispatched in current slice */
1368e89d13fSVivek Goyal 	unsigned int io_disp[2];
137e43473b7SVivek Goyal 
138e43473b7SVivek Goyal 	/* When did we start a new slice */
139e43473b7SVivek Goyal 	unsigned long slice_start[2];
140e43473b7SVivek Goyal 	unsigned long slice_end[2];
141fe071437SVivek Goyal 
1428a3d2615STejun Heo 	/* Per cpu stats pointer */
1438a3d2615STejun Heo 	struct tg_stats_cpu __percpu *stats_cpu;
1448a3d2615STejun Heo 
1458a3d2615STejun Heo 	/* List of tgs waiting for per cpu stats memory to be allocated */
1468a3d2615STejun Heo 	struct list_head stats_alloc_node;
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 
1688a3d2615STejun Heo /* list and work item to allocate percpu group stats */
1698a3d2615STejun Heo static DEFINE_SPINLOCK(tg_stats_alloc_lock);
1708a3d2615STejun Heo static LIST_HEAD(tg_stats_alloc_list);
1718a3d2615STejun Heo 
1728a3d2615STejun Heo static void tg_stats_alloc_fn(struct work_struct *);
1738a3d2615STejun Heo static DECLARE_DELAYED_WORK(tg_stats_alloc_work, tg_stats_alloc_fn);
1748a3d2615STejun Heo 
17569df0ab0STejun Heo static void throtl_pending_timer_fn(unsigned long arg);
17669df0ab0STejun Heo 
177f95a04afSTejun Heo static inline struct throtl_grp *pd_to_tg(struct blkg_policy_data *pd)
178f95a04afSTejun Heo {
179f95a04afSTejun Heo 	return pd ? container_of(pd, struct throtl_grp, pd) : NULL;
180f95a04afSTejun Heo }
181f95a04afSTejun Heo 
1823c798398STejun Heo static inline struct throtl_grp *blkg_to_tg(struct blkcg_gq *blkg)
1830381411eSTejun Heo {
184f95a04afSTejun Heo 	return pd_to_tg(blkg_to_pd(blkg, &blkcg_policy_throtl));
1850381411eSTejun Heo }
1860381411eSTejun Heo 
1873c798398STejun Heo static inline struct blkcg_gq *tg_to_blkg(struct throtl_grp *tg)
1880381411eSTejun Heo {
189f95a04afSTejun Heo 	return pd_to_blkg(&tg->pd);
1900381411eSTejun Heo }
1910381411eSTejun Heo 
19203d8e111STejun Heo static inline struct throtl_grp *td_root_tg(struct throtl_data *td)
19303d8e111STejun Heo {
19403d8e111STejun Heo 	return blkg_to_tg(td->queue->root_blkg);
19503d8e111STejun Heo }
19603d8e111STejun Heo 
197fda6f272STejun Heo /**
198fda6f272STejun Heo  * sq_to_tg - return the throl_grp the specified service queue belongs to
199fda6f272STejun Heo  * @sq: the throtl_service_queue of interest
200fda6f272STejun Heo  *
201fda6f272STejun Heo  * Return the throtl_grp @sq belongs to.  If @sq is the top-level one
202fda6f272STejun Heo  * embedded in throtl_data, %NULL is returned.
203fda6f272STejun Heo  */
204fda6f272STejun Heo static struct throtl_grp *sq_to_tg(struct throtl_service_queue *sq)
205fda6f272STejun Heo {
206fda6f272STejun Heo 	if (sq && sq->parent_sq)
207fda6f272STejun Heo 		return container_of(sq, struct throtl_grp, service_queue);
208fda6f272STejun Heo 	else
209fda6f272STejun Heo 		return NULL;
210fda6f272STejun Heo }
211fda6f272STejun Heo 
212fda6f272STejun Heo /**
213fda6f272STejun Heo  * sq_to_td - return throtl_data the specified service queue belongs to
214fda6f272STejun Heo  * @sq: the throtl_service_queue of interest
215fda6f272STejun Heo  *
216fda6f272STejun Heo  * A service_queue can be embeded in either a throtl_grp or throtl_data.
217fda6f272STejun Heo  * Determine the associated throtl_data accordingly and return it.
218fda6f272STejun Heo  */
219fda6f272STejun Heo static struct throtl_data *sq_to_td(struct throtl_service_queue *sq)
220fda6f272STejun Heo {
221fda6f272STejun Heo 	struct throtl_grp *tg = sq_to_tg(sq);
222fda6f272STejun Heo 
223fda6f272STejun Heo 	if (tg)
224fda6f272STejun Heo 		return tg->td;
225fda6f272STejun Heo 	else
226fda6f272STejun Heo 		return container_of(sq, struct throtl_data, service_queue);
227fda6f272STejun Heo }
228fda6f272STejun Heo 
229fda6f272STejun Heo /**
230fda6f272STejun Heo  * throtl_log - log debug message via blktrace
231fda6f272STejun Heo  * @sq: the service_queue being reported
232fda6f272STejun Heo  * @fmt: printf format string
233fda6f272STejun Heo  * @args: printf args
234fda6f272STejun Heo  *
235fda6f272STejun Heo  * The messages are prefixed with "throtl BLKG_NAME" if @sq belongs to a
236fda6f272STejun Heo  * throtl_grp; otherwise, just "throtl".
237fda6f272STejun Heo  *
238fda6f272STejun Heo  * TODO: this should be made a function and name formatting should happen
239fda6f272STejun Heo  * after testing whether blktrace is enabled.
240fda6f272STejun Heo  */
241fda6f272STejun Heo #define throtl_log(sq, fmt, args...)	do {				\
242fda6f272STejun Heo 	struct throtl_grp *__tg = sq_to_tg((sq));			\
243fda6f272STejun Heo 	struct throtl_data *__td = sq_to_td((sq));			\
244fda6f272STejun Heo 									\
245fda6f272STejun Heo 	(void)__td;							\
246fda6f272STejun Heo 	if ((__tg)) {							\
24754e7ed12STejun Heo 		char __pbuf[128];					\
24854e7ed12STejun Heo 									\
249fda6f272STejun Heo 		blkg_path(tg_to_blkg(__tg), __pbuf, sizeof(__pbuf));	\
250fda6f272STejun Heo 		blk_add_trace_msg(__td->queue, "throtl %s " fmt, __pbuf, ##args); \
251fda6f272STejun Heo 	} else {							\
252fda6f272STejun Heo 		blk_add_trace_msg(__td->queue, "throtl " fmt, ##args);	\
253fda6f272STejun Heo 	}								\
25454e7ed12STejun Heo } while (0)
255e43473b7SVivek Goyal 
2568a3d2615STejun Heo /*
2578a3d2615STejun Heo  * Worker for allocating per cpu stat for tgs. This is scheduled on the
2583b07e9caSTejun Heo  * system_wq once there are some groups on the alloc_list waiting for
2598a3d2615STejun Heo  * allocation.
2608a3d2615STejun Heo  */
2618a3d2615STejun Heo static void tg_stats_alloc_fn(struct work_struct *work)
2628a3d2615STejun Heo {
2638a3d2615STejun Heo 	static struct tg_stats_cpu *stats_cpu;	/* this fn is non-reentrant */
2648a3d2615STejun Heo 	struct delayed_work *dwork = to_delayed_work(work);
2658a3d2615STejun Heo 	bool empty = false;
2668a3d2615STejun Heo 
2678a3d2615STejun Heo alloc_stats:
2688a3d2615STejun Heo 	if (!stats_cpu) {
2698a3d2615STejun Heo 		stats_cpu = alloc_percpu(struct tg_stats_cpu);
2708a3d2615STejun Heo 		if (!stats_cpu) {
2718a3d2615STejun Heo 			/* allocation failed, try again after some time */
2723b07e9caSTejun Heo 			schedule_delayed_work(dwork, msecs_to_jiffies(10));
2738a3d2615STejun Heo 			return;
2748a3d2615STejun Heo 		}
2758a3d2615STejun Heo 	}
2768a3d2615STejun Heo 
2778a3d2615STejun Heo 	spin_lock_irq(&tg_stats_alloc_lock);
2788a3d2615STejun Heo 
2798a3d2615STejun Heo 	if (!list_empty(&tg_stats_alloc_list)) {
2808a3d2615STejun Heo 		struct throtl_grp *tg = list_first_entry(&tg_stats_alloc_list,
2818a3d2615STejun Heo 							 struct throtl_grp,
2828a3d2615STejun Heo 							 stats_alloc_node);
2838a3d2615STejun Heo 		swap(tg->stats_cpu, stats_cpu);
2848a3d2615STejun Heo 		list_del_init(&tg->stats_alloc_node);
2858a3d2615STejun Heo 	}
2868a3d2615STejun Heo 
2878a3d2615STejun Heo 	empty = list_empty(&tg_stats_alloc_list);
2888a3d2615STejun Heo 	spin_unlock_irq(&tg_stats_alloc_lock);
2898a3d2615STejun Heo 	if (!empty)
2908a3d2615STejun Heo 		goto alloc_stats;
2918a3d2615STejun Heo }
2928a3d2615STejun Heo 
293c5cc2070STejun Heo static void throtl_qnode_init(struct throtl_qnode *qn, struct throtl_grp *tg)
294c5cc2070STejun Heo {
295c5cc2070STejun Heo 	INIT_LIST_HEAD(&qn->node);
296c5cc2070STejun Heo 	bio_list_init(&qn->bios);
297c5cc2070STejun Heo 	qn->tg = tg;
298c5cc2070STejun Heo }
299c5cc2070STejun Heo 
300c5cc2070STejun Heo /**
301c5cc2070STejun Heo  * throtl_qnode_add_bio - add a bio to a throtl_qnode and activate it
302c5cc2070STejun Heo  * @bio: bio being added
303c5cc2070STejun Heo  * @qn: qnode to add bio to
304c5cc2070STejun Heo  * @queued: the service_queue->queued[] list @qn belongs to
305c5cc2070STejun Heo  *
306c5cc2070STejun Heo  * Add @bio to @qn and put @qn on @queued if it's not already on.
307c5cc2070STejun Heo  * @qn->tg's reference count is bumped when @qn is activated.  See the
308c5cc2070STejun Heo  * comment on top of throtl_qnode definition for details.
309c5cc2070STejun Heo  */
310c5cc2070STejun Heo static void throtl_qnode_add_bio(struct bio *bio, struct throtl_qnode *qn,
311c5cc2070STejun Heo 				 struct list_head *queued)
312c5cc2070STejun Heo {
313c5cc2070STejun Heo 	bio_list_add(&qn->bios, bio);
314c5cc2070STejun Heo 	if (list_empty(&qn->node)) {
315c5cc2070STejun Heo 		list_add_tail(&qn->node, queued);
316c5cc2070STejun Heo 		blkg_get(tg_to_blkg(qn->tg));
317c5cc2070STejun Heo 	}
318c5cc2070STejun Heo }
319c5cc2070STejun Heo 
320c5cc2070STejun Heo /**
321c5cc2070STejun Heo  * throtl_peek_queued - peek the first bio on a qnode list
322c5cc2070STejun Heo  * @queued: the qnode list to peek
323c5cc2070STejun Heo  */
324c5cc2070STejun Heo static struct bio *throtl_peek_queued(struct list_head *queued)
325c5cc2070STejun Heo {
326c5cc2070STejun Heo 	struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
327c5cc2070STejun Heo 	struct bio *bio;
328c5cc2070STejun Heo 
329c5cc2070STejun Heo 	if (list_empty(queued))
330c5cc2070STejun Heo 		return NULL;
331c5cc2070STejun Heo 
332c5cc2070STejun Heo 	bio = bio_list_peek(&qn->bios);
333c5cc2070STejun Heo 	WARN_ON_ONCE(!bio);
334c5cc2070STejun Heo 	return bio;
335c5cc2070STejun Heo }
336c5cc2070STejun Heo 
337c5cc2070STejun Heo /**
338c5cc2070STejun Heo  * throtl_pop_queued - pop the first bio form a qnode list
339c5cc2070STejun Heo  * @queued: the qnode list to pop a bio from
340c5cc2070STejun Heo  * @tg_to_put: optional out argument for throtl_grp to put
341c5cc2070STejun Heo  *
342c5cc2070STejun Heo  * Pop the first bio from the qnode list @queued.  After popping, the first
343c5cc2070STejun Heo  * qnode is removed from @queued if empty or moved to the end of @queued so
344c5cc2070STejun Heo  * that the popping order is round-robin.
345c5cc2070STejun Heo  *
346c5cc2070STejun Heo  * When the first qnode is removed, its associated throtl_grp should be put
347c5cc2070STejun Heo  * too.  If @tg_to_put is NULL, this function automatically puts it;
348c5cc2070STejun Heo  * otherwise, *@tg_to_put is set to the throtl_grp to put and the caller is
349c5cc2070STejun Heo  * responsible for putting it.
350c5cc2070STejun Heo  */
351c5cc2070STejun Heo static struct bio *throtl_pop_queued(struct list_head *queued,
352c5cc2070STejun Heo 				     struct throtl_grp **tg_to_put)
353c5cc2070STejun Heo {
354c5cc2070STejun Heo 	struct throtl_qnode *qn = list_first_entry(queued, struct throtl_qnode, node);
355c5cc2070STejun Heo 	struct bio *bio;
356c5cc2070STejun Heo 
357c5cc2070STejun Heo 	if (list_empty(queued))
358c5cc2070STejun Heo 		return NULL;
359c5cc2070STejun Heo 
360c5cc2070STejun Heo 	bio = bio_list_pop(&qn->bios);
361c5cc2070STejun Heo 	WARN_ON_ONCE(!bio);
362c5cc2070STejun Heo 
363c5cc2070STejun Heo 	if (bio_list_empty(&qn->bios)) {
364c5cc2070STejun Heo 		list_del_init(&qn->node);
365c5cc2070STejun Heo 		if (tg_to_put)
366c5cc2070STejun Heo 			*tg_to_put = qn->tg;
367c5cc2070STejun Heo 		else
368c5cc2070STejun Heo 			blkg_put(tg_to_blkg(qn->tg));
369c5cc2070STejun Heo 	} else {
370c5cc2070STejun Heo 		list_move_tail(&qn->node, queued);
371c5cc2070STejun Heo 	}
372c5cc2070STejun Heo 
373c5cc2070STejun Heo 	return bio;
374c5cc2070STejun Heo }
375c5cc2070STejun Heo 
37649a2f1e3STejun Heo /* init a service_queue, assumes the caller zeroed it */
37777216b04STejun Heo static void throtl_service_queue_init(struct throtl_service_queue *sq,
37877216b04STejun Heo 				      struct throtl_service_queue *parent_sq)
37949a2f1e3STejun Heo {
380c5cc2070STejun Heo 	INIT_LIST_HEAD(&sq->queued[0]);
381c5cc2070STejun Heo 	INIT_LIST_HEAD(&sq->queued[1]);
38249a2f1e3STejun Heo 	sq->pending_tree = RB_ROOT;
38377216b04STejun Heo 	sq->parent_sq = parent_sq;
38469df0ab0STejun Heo 	setup_timer(&sq->pending_timer, throtl_pending_timer_fn,
38569df0ab0STejun Heo 		    (unsigned long)sq);
38669df0ab0STejun Heo }
38769df0ab0STejun Heo 
38869df0ab0STejun Heo static void throtl_service_queue_exit(struct throtl_service_queue *sq)
38969df0ab0STejun Heo {
39069df0ab0STejun Heo 	del_timer_sync(&sq->pending_timer);
39149a2f1e3STejun Heo }
39249a2f1e3STejun Heo 
3933c798398STejun Heo static void throtl_pd_init(struct blkcg_gq *blkg)
394a29a171eSVivek Goyal {
3950381411eSTejun Heo 	struct throtl_grp *tg = blkg_to_tg(blkg);
39677216b04STejun Heo 	struct throtl_data *td = blkg->q->td;
397ff26eaadSTejun Heo 	unsigned long flags;
398c5cc2070STejun Heo 	int rw;
399cd1604faSTejun Heo 
40077216b04STejun Heo 	throtl_service_queue_init(&tg->service_queue, &td->service_queue);
401c5cc2070STejun Heo 	for (rw = READ; rw <= WRITE; rw++) {
402c5cc2070STejun Heo 		throtl_qnode_init(&tg->qnode_on_self[rw], tg);
403c5cc2070STejun Heo 		throtl_qnode_init(&tg->qnode_on_parent[rw], tg);
404c5cc2070STejun Heo 	}
405c5cc2070STejun Heo 
406a29a171eSVivek Goyal 	RB_CLEAR_NODE(&tg->rb_node);
40777216b04STejun Heo 	tg->td = td;
408a29a171eSVivek Goyal 
409e56da7e2STejun Heo 	tg->bps[READ] = -1;
410e56da7e2STejun Heo 	tg->bps[WRITE] = -1;
411e56da7e2STejun Heo 	tg->iops[READ] = -1;
412e56da7e2STejun Heo 	tg->iops[WRITE] = -1;
4138a3d2615STejun Heo 
4148a3d2615STejun Heo 	/*
4158a3d2615STejun Heo 	 * Ugh... We need to perform per-cpu allocation for tg->stats_cpu
4168a3d2615STejun Heo 	 * but percpu allocator can't be called from IO path.  Queue tg on
4178a3d2615STejun Heo 	 * tg_stats_alloc_list and allocate from work item.
4188a3d2615STejun Heo 	 */
419ff26eaadSTejun Heo 	spin_lock_irqsave(&tg_stats_alloc_lock, flags);
4208a3d2615STejun Heo 	list_add(&tg->stats_alloc_node, &tg_stats_alloc_list);
4213b07e9caSTejun Heo 	schedule_delayed_work(&tg_stats_alloc_work, 0);
422ff26eaadSTejun Heo 	spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
4238a3d2615STejun Heo }
4248a3d2615STejun Heo 
4253c798398STejun Heo static void throtl_pd_exit(struct blkcg_gq *blkg)
4268a3d2615STejun Heo {
4278a3d2615STejun Heo 	struct throtl_grp *tg = blkg_to_tg(blkg);
428ff26eaadSTejun Heo 	unsigned long flags;
4298a3d2615STejun Heo 
430ff26eaadSTejun Heo 	spin_lock_irqsave(&tg_stats_alloc_lock, flags);
4318a3d2615STejun Heo 	list_del_init(&tg->stats_alloc_node);
432ff26eaadSTejun Heo 	spin_unlock_irqrestore(&tg_stats_alloc_lock, flags);
4338a3d2615STejun Heo 
4348a3d2615STejun Heo 	free_percpu(tg->stats_cpu);
43569df0ab0STejun Heo 
43669df0ab0STejun Heo 	throtl_service_queue_exit(&tg->service_queue);
4378a3d2615STejun Heo }
4388a3d2615STejun Heo 
4393c798398STejun Heo static void throtl_pd_reset_stats(struct blkcg_gq *blkg)
4408a3d2615STejun Heo {
4418a3d2615STejun Heo 	struct throtl_grp *tg = blkg_to_tg(blkg);
4428a3d2615STejun Heo 	int cpu;
4438a3d2615STejun Heo 
4448a3d2615STejun Heo 	if (tg->stats_cpu == NULL)
4458a3d2615STejun Heo 		return;
4468a3d2615STejun Heo 
4478a3d2615STejun Heo 	for_each_possible_cpu(cpu) {
4488a3d2615STejun Heo 		struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
4498a3d2615STejun Heo 
4508a3d2615STejun Heo 		blkg_rwstat_reset(&sc->service_bytes);
4518a3d2615STejun Heo 		blkg_rwstat_reset(&sc->serviced);
4528a3d2615STejun Heo 	}
453a29a171eSVivek Goyal }
454a29a171eSVivek Goyal 
4553c798398STejun Heo static struct throtl_grp *throtl_lookup_tg(struct throtl_data *td,
4563c798398STejun Heo 					   struct blkcg *blkcg)
457e43473b7SVivek Goyal {
458e43473b7SVivek Goyal 	/*
4593c798398STejun Heo 	 * This is the common case when there are no blkcgs.  Avoid lookup
4603c798398STejun Heo 	 * in this case
461be2c6b19SVivek Goyal 	 */
4623c798398STejun Heo 	if (blkcg == &blkcg_root)
46303d8e111STejun Heo 		return td_root_tg(td);
464e43473b7SVivek Goyal 
465e8989faeSTejun Heo 	return blkg_to_tg(blkg_lookup(blkcg, td->queue));
466e43473b7SVivek Goyal }
467e43473b7SVivek Goyal 
468cd1604faSTejun Heo static struct throtl_grp *throtl_lookup_create_tg(struct throtl_data *td,
4693c798398STejun Heo 						  struct blkcg *blkcg)
470e43473b7SVivek Goyal {
471f469a7b4SVivek Goyal 	struct request_queue *q = td->queue;
472cd1604faSTejun Heo 	struct throtl_grp *tg = NULL;
4730a5a7d0eSTejun Heo 
474f469a7b4SVivek Goyal 	/*
4753c798398STejun Heo 	 * This is the common case when there are no blkcgs.  Avoid lookup
4763c798398STejun Heo 	 * in this case
477f469a7b4SVivek Goyal 	 */
4783c798398STejun Heo 	if (blkcg == &blkcg_root) {
47903d8e111STejun Heo 		tg = td_root_tg(td);
480cd1604faSTejun Heo 	} else {
4813c798398STejun Heo 		struct blkcg_gq *blkg;
482cd1604faSTejun Heo 
4833c96cb32STejun Heo 		blkg = blkg_lookup_create(blkcg, q);
484cd1604faSTejun Heo 
485cd1604faSTejun Heo 		/* if %NULL and @q is alive, fall back to root_tg */
486cd1604faSTejun Heo 		if (!IS_ERR(blkg))
4870381411eSTejun Heo 			tg = blkg_to_tg(blkg);
4883f3299d5SBart Van Assche 		else if (!blk_queue_dying(q))
48903d8e111STejun Heo 			tg = td_root_tg(td);
490f469a7b4SVivek Goyal 	}
491f469a7b4SVivek Goyal 
492e43473b7SVivek Goyal 	return tg;
493e43473b7SVivek Goyal }
494e43473b7SVivek Goyal 
4950049af73STejun Heo static struct throtl_grp *
4960049af73STejun Heo throtl_rb_first(struct throtl_service_queue *parent_sq)
497e43473b7SVivek Goyal {
498e43473b7SVivek Goyal 	/* Service tree is empty */
4990049af73STejun Heo 	if (!parent_sq->nr_pending)
500e43473b7SVivek Goyal 		return NULL;
501e43473b7SVivek Goyal 
5020049af73STejun Heo 	if (!parent_sq->first_pending)
5030049af73STejun Heo 		parent_sq->first_pending = rb_first(&parent_sq->pending_tree);
504e43473b7SVivek Goyal 
5050049af73STejun Heo 	if (parent_sq->first_pending)
5060049af73STejun Heo 		return rb_entry_tg(parent_sq->first_pending);
507e43473b7SVivek Goyal 
508e43473b7SVivek Goyal 	return NULL;
509e43473b7SVivek Goyal }
510e43473b7SVivek Goyal 
511e43473b7SVivek Goyal static void rb_erase_init(struct rb_node *n, struct rb_root *root)
512e43473b7SVivek Goyal {
513e43473b7SVivek Goyal 	rb_erase(n, root);
514e43473b7SVivek Goyal 	RB_CLEAR_NODE(n);
515e43473b7SVivek Goyal }
516e43473b7SVivek Goyal 
5170049af73STejun Heo static void throtl_rb_erase(struct rb_node *n,
5180049af73STejun Heo 			    struct throtl_service_queue *parent_sq)
519e43473b7SVivek Goyal {
5200049af73STejun Heo 	if (parent_sq->first_pending == n)
5210049af73STejun Heo 		parent_sq->first_pending = NULL;
5220049af73STejun Heo 	rb_erase_init(n, &parent_sq->pending_tree);
5230049af73STejun Heo 	--parent_sq->nr_pending;
524e43473b7SVivek Goyal }
525e43473b7SVivek Goyal 
5260049af73STejun Heo static void update_min_dispatch_time(struct throtl_service_queue *parent_sq)
527e43473b7SVivek Goyal {
528e43473b7SVivek Goyal 	struct throtl_grp *tg;
529e43473b7SVivek Goyal 
5300049af73STejun Heo 	tg = throtl_rb_first(parent_sq);
531e43473b7SVivek Goyal 	if (!tg)
532e43473b7SVivek Goyal 		return;
533e43473b7SVivek Goyal 
5340049af73STejun Heo 	parent_sq->first_pending_disptime = tg->disptime;
535e43473b7SVivek Goyal }
536e43473b7SVivek Goyal 
53777216b04STejun Heo static void tg_service_queue_add(struct throtl_grp *tg)
538e43473b7SVivek Goyal {
53977216b04STejun Heo 	struct throtl_service_queue *parent_sq = tg->service_queue.parent_sq;
5400049af73STejun Heo 	struct rb_node **node = &parent_sq->pending_tree.rb_node;
541e43473b7SVivek Goyal 	struct rb_node *parent = NULL;
542e43473b7SVivek Goyal 	struct throtl_grp *__tg;
543e43473b7SVivek Goyal 	unsigned long key = tg->disptime;
544e43473b7SVivek Goyal 	int left = 1;
545e43473b7SVivek Goyal 
546e43473b7SVivek Goyal 	while (*node != NULL) {
547e43473b7SVivek Goyal 		parent = *node;
548e43473b7SVivek Goyal 		__tg = rb_entry_tg(parent);
549e43473b7SVivek Goyal 
550e43473b7SVivek Goyal 		if (time_before(key, __tg->disptime))
551e43473b7SVivek Goyal 			node = &parent->rb_left;
552e43473b7SVivek Goyal 		else {
553e43473b7SVivek Goyal 			node = &parent->rb_right;
554e43473b7SVivek Goyal 			left = 0;
555e43473b7SVivek Goyal 		}
556e43473b7SVivek Goyal 	}
557e43473b7SVivek Goyal 
558e43473b7SVivek Goyal 	if (left)
5590049af73STejun Heo 		parent_sq->first_pending = &tg->rb_node;
560e43473b7SVivek Goyal 
561e43473b7SVivek Goyal 	rb_link_node(&tg->rb_node, parent, node);
5620049af73STejun Heo 	rb_insert_color(&tg->rb_node, &parent_sq->pending_tree);
563e43473b7SVivek Goyal }
564e43473b7SVivek Goyal 
56577216b04STejun Heo static void __throtl_enqueue_tg(struct throtl_grp *tg)
566e43473b7SVivek Goyal {
56777216b04STejun Heo 	tg_service_queue_add(tg);
5685b2c16aaSTejun Heo 	tg->flags |= THROTL_TG_PENDING;
56977216b04STejun Heo 	tg->service_queue.parent_sq->nr_pending++;
570e43473b7SVivek Goyal }
571e43473b7SVivek Goyal 
57277216b04STejun Heo static void throtl_enqueue_tg(struct throtl_grp *tg)
573e43473b7SVivek Goyal {
5745b2c16aaSTejun Heo 	if (!(tg->flags & THROTL_TG_PENDING))
57577216b04STejun Heo 		__throtl_enqueue_tg(tg);
576e43473b7SVivek Goyal }
577e43473b7SVivek Goyal 
57877216b04STejun Heo static void __throtl_dequeue_tg(struct throtl_grp *tg)
579e43473b7SVivek Goyal {
58077216b04STejun Heo 	throtl_rb_erase(&tg->rb_node, tg->service_queue.parent_sq);
5815b2c16aaSTejun Heo 	tg->flags &= ~THROTL_TG_PENDING;
582e43473b7SVivek Goyal }
583e43473b7SVivek Goyal 
58477216b04STejun Heo static void throtl_dequeue_tg(struct throtl_grp *tg)
585e43473b7SVivek Goyal {
5865b2c16aaSTejun Heo 	if (tg->flags & THROTL_TG_PENDING)
58777216b04STejun Heo 		__throtl_dequeue_tg(tg);
588e43473b7SVivek Goyal }
589e43473b7SVivek Goyal 
590a9131a27STejun Heo /* Call with queue lock held */
59169df0ab0STejun Heo static void throtl_schedule_pending_timer(struct throtl_service_queue *sq,
59269df0ab0STejun Heo 					  unsigned long expires)
593a9131a27STejun Heo {
59469df0ab0STejun Heo 	mod_timer(&sq->pending_timer, expires);
59569df0ab0STejun Heo 	throtl_log(sq, "schedule timer. delay=%lu jiffies=%lu",
59669df0ab0STejun Heo 		   expires - jiffies, jiffies);
597a9131a27STejun Heo }
598a9131a27STejun Heo 
5997f52f98cSTejun Heo /**
6007f52f98cSTejun Heo  * throtl_schedule_next_dispatch - schedule the next dispatch cycle
6017f52f98cSTejun Heo  * @sq: the service_queue to schedule dispatch for
6027f52f98cSTejun Heo  * @force: force scheduling
6037f52f98cSTejun Heo  *
6047f52f98cSTejun Heo  * Arm @sq->pending_timer so that the next dispatch cycle starts on the
6057f52f98cSTejun Heo  * dispatch time of the first pending child.  Returns %true if either timer
6067f52f98cSTejun Heo  * is armed or there's no pending child left.  %false if the current
6077f52f98cSTejun Heo  * dispatch window is still open and the caller should continue
6087f52f98cSTejun Heo  * dispatching.
6097f52f98cSTejun Heo  *
6107f52f98cSTejun Heo  * If @force is %true, the dispatch timer is always scheduled and this
6117f52f98cSTejun Heo  * function is guaranteed to return %true.  This is to be used when the
6127f52f98cSTejun Heo  * caller can't dispatch itself and needs to invoke pending_timer
6137f52f98cSTejun Heo  * unconditionally.  Note that forced scheduling is likely to induce short
6147f52f98cSTejun Heo  * delay before dispatch starts even if @sq->first_pending_disptime is not
6157f52f98cSTejun Heo  * in the future and thus shouldn't be used in hot paths.
6167f52f98cSTejun Heo  */
6177f52f98cSTejun Heo static bool throtl_schedule_next_dispatch(struct throtl_service_queue *sq,
6187f52f98cSTejun Heo 					  bool force)
619e43473b7SVivek Goyal {
6206a525600STejun Heo 	/* any pending children left? */
621c9e0332eSTejun Heo 	if (!sq->nr_pending)
6227f52f98cSTejun Heo 		return true;
623e43473b7SVivek Goyal 
624c9e0332eSTejun Heo 	update_min_dispatch_time(sq);
625e43473b7SVivek Goyal 
62669df0ab0STejun Heo 	/* is the next dispatch time in the future? */
6277f52f98cSTejun Heo 	if (force || time_after(sq->first_pending_disptime, jiffies)) {
62869df0ab0STejun Heo 		throtl_schedule_pending_timer(sq, sq->first_pending_disptime);
6297f52f98cSTejun Heo 		return true;
63069df0ab0STejun Heo 	}
63169df0ab0STejun Heo 
6327f52f98cSTejun Heo 	/* tell the caller to continue dispatching */
6337f52f98cSTejun Heo 	return false;
634e43473b7SVivek Goyal }
635e43473b7SVivek Goyal 
636*32ee5bc4SVivek Goyal static inline void throtl_start_new_slice_with_credit(struct throtl_grp *tg,
637*32ee5bc4SVivek Goyal 		bool rw, unsigned long start)
638*32ee5bc4SVivek Goyal {
639*32ee5bc4SVivek Goyal 	tg->bytes_disp[rw] = 0;
640*32ee5bc4SVivek Goyal 	tg->io_disp[rw] = 0;
641*32ee5bc4SVivek Goyal 
642*32ee5bc4SVivek Goyal 	/*
643*32ee5bc4SVivek Goyal 	 * Previous slice has expired. We must have trimmed it after last
644*32ee5bc4SVivek Goyal 	 * bio dispatch. That means since start of last slice, we never used
645*32ee5bc4SVivek Goyal 	 * that bandwidth. Do try to make use of that bandwidth while giving
646*32ee5bc4SVivek Goyal 	 * credit.
647*32ee5bc4SVivek Goyal 	 */
648*32ee5bc4SVivek Goyal 	if (time_after_eq(start, tg->slice_start[rw]))
649*32ee5bc4SVivek Goyal 		tg->slice_start[rw] = start;
650*32ee5bc4SVivek Goyal 
651*32ee5bc4SVivek Goyal 	tg->slice_end[rw] = jiffies + throtl_slice;
652*32ee5bc4SVivek Goyal 	throtl_log(&tg->service_queue,
653*32ee5bc4SVivek Goyal 		   "[%c] new slice with credit start=%lu end=%lu jiffies=%lu",
654*32ee5bc4SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
655*32ee5bc4SVivek Goyal 		   tg->slice_end[rw], jiffies);
656*32ee5bc4SVivek Goyal }
657*32ee5bc4SVivek Goyal 
6580f3457f6STejun Heo static inline void throtl_start_new_slice(struct throtl_grp *tg, bool rw)
659e43473b7SVivek Goyal {
660e43473b7SVivek Goyal 	tg->bytes_disp[rw] = 0;
6618e89d13fSVivek Goyal 	tg->io_disp[rw] = 0;
662e43473b7SVivek Goyal 	tg->slice_start[rw] = jiffies;
663e43473b7SVivek Goyal 	tg->slice_end[rw] = jiffies + throtl_slice;
664fda6f272STejun Heo 	throtl_log(&tg->service_queue,
665fda6f272STejun Heo 		   "[%c] new slice start=%lu end=%lu jiffies=%lu",
666e43473b7SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
667e43473b7SVivek Goyal 		   tg->slice_end[rw], jiffies);
668e43473b7SVivek Goyal }
669e43473b7SVivek Goyal 
6700f3457f6STejun Heo static inline void throtl_set_slice_end(struct throtl_grp *tg, bool rw,
6710f3457f6STejun Heo 					unsigned long jiffy_end)
672d1ae8ffdSVivek Goyal {
673d1ae8ffdSVivek Goyal 	tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
674d1ae8ffdSVivek Goyal }
675d1ae8ffdSVivek Goyal 
6760f3457f6STejun Heo static inline void throtl_extend_slice(struct throtl_grp *tg, bool rw,
6770f3457f6STejun Heo 				       unsigned long jiffy_end)
678e43473b7SVivek Goyal {
679e43473b7SVivek Goyal 	tg->slice_end[rw] = roundup(jiffy_end, throtl_slice);
680fda6f272STejun Heo 	throtl_log(&tg->service_queue,
681fda6f272STejun Heo 		   "[%c] extend slice start=%lu end=%lu jiffies=%lu",
682e43473b7SVivek Goyal 		   rw == READ ? 'R' : 'W', tg->slice_start[rw],
683e43473b7SVivek Goyal 		   tg->slice_end[rw], jiffies);
684e43473b7SVivek Goyal }
685e43473b7SVivek Goyal 
686e43473b7SVivek Goyal /* Determine if previously allocated or extended slice is complete or not */
6870f3457f6STejun Heo static bool throtl_slice_used(struct throtl_grp *tg, bool rw)
688e43473b7SVivek Goyal {
689e43473b7SVivek Goyal 	if (time_in_range(jiffies, tg->slice_start[rw], tg->slice_end[rw]))
690e43473b7SVivek Goyal 		return 0;
691e43473b7SVivek Goyal 
692e43473b7SVivek Goyal 	return 1;
693e43473b7SVivek Goyal }
694e43473b7SVivek Goyal 
695e43473b7SVivek Goyal /* Trim the used slices and adjust slice start accordingly */
6960f3457f6STejun Heo static inline void throtl_trim_slice(struct throtl_grp *tg, bool rw)
697e43473b7SVivek Goyal {
6983aad5d3eSVivek Goyal 	unsigned long nr_slices, time_elapsed, io_trim;
6993aad5d3eSVivek Goyal 	u64 bytes_trim, tmp;
700e43473b7SVivek Goyal 
701e43473b7SVivek Goyal 	BUG_ON(time_before(tg->slice_end[rw], tg->slice_start[rw]));
702e43473b7SVivek Goyal 
703e43473b7SVivek Goyal 	/*
704e43473b7SVivek Goyal 	 * If bps are unlimited (-1), then time slice don't get
705e43473b7SVivek Goyal 	 * renewed. Don't try to trim the slice if slice is used. A new
706e43473b7SVivek Goyal 	 * slice will start when appropriate.
707e43473b7SVivek Goyal 	 */
7080f3457f6STejun Heo 	if (throtl_slice_used(tg, rw))
709e43473b7SVivek Goyal 		return;
710e43473b7SVivek Goyal 
711d1ae8ffdSVivek Goyal 	/*
712d1ae8ffdSVivek Goyal 	 * A bio has been dispatched. Also adjust slice_end. It might happen
713d1ae8ffdSVivek Goyal 	 * that initially cgroup limit was very low resulting in high
714d1ae8ffdSVivek Goyal 	 * slice_end, but later limit was bumped up and bio was dispached
715d1ae8ffdSVivek Goyal 	 * sooner, then we need to reduce slice_end. A high bogus slice_end
716d1ae8ffdSVivek Goyal 	 * is bad because it does not allow new slice to start.
717d1ae8ffdSVivek Goyal 	 */
718d1ae8ffdSVivek Goyal 
7190f3457f6STejun Heo 	throtl_set_slice_end(tg, rw, jiffies + throtl_slice);
720d1ae8ffdSVivek Goyal 
721e43473b7SVivek Goyal 	time_elapsed = jiffies - tg->slice_start[rw];
722e43473b7SVivek Goyal 
723e43473b7SVivek Goyal 	nr_slices = time_elapsed / throtl_slice;
724e43473b7SVivek Goyal 
725e43473b7SVivek Goyal 	if (!nr_slices)
726e43473b7SVivek Goyal 		return;
7273aad5d3eSVivek Goyal 	tmp = tg->bps[rw] * throtl_slice * nr_slices;
7283aad5d3eSVivek Goyal 	do_div(tmp, HZ);
7293aad5d3eSVivek Goyal 	bytes_trim = tmp;
730e43473b7SVivek Goyal 
7318e89d13fSVivek Goyal 	io_trim = (tg->iops[rw] * throtl_slice * nr_slices)/HZ;
732e43473b7SVivek Goyal 
7338e89d13fSVivek Goyal 	if (!bytes_trim && !io_trim)
734e43473b7SVivek Goyal 		return;
735e43473b7SVivek Goyal 
736e43473b7SVivek Goyal 	if (tg->bytes_disp[rw] >= bytes_trim)
737e43473b7SVivek Goyal 		tg->bytes_disp[rw] -= bytes_trim;
738e43473b7SVivek Goyal 	else
739e43473b7SVivek Goyal 		tg->bytes_disp[rw] = 0;
740e43473b7SVivek Goyal 
7418e89d13fSVivek Goyal 	if (tg->io_disp[rw] >= io_trim)
7428e89d13fSVivek Goyal 		tg->io_disp[rw] -= io_trim;
7438e89d13fSVivek Goyal 	else
7448e89d13fSVivek Goyal 		tg->io_disp[rw] = 0;
7458e89d13fSVivek Goyal 
746e43473b7SVivek Goyal 	tg->slice_start[rw] += nr_slices * throtl_slice;
747e43473b7SVivek Goyal 
748fda6f272STejun Heo 	throtl_log(&tg->service_queue,
749fda6f272STejun Heo 		   "[%c] trim slice nr=%lu bytes=%llu io=%lu start=%lu end=%lu jiffies=%lu",
7508e89d13fSVivek Goyal 		   rw == READ ? 'R' : 'W', nr_slices, bytes_trim, io_trim,
751e43473b7SVivek Goyal 		   tg->slice_start[rw], tg->slice_end[rw], jiffies);
752e43473b7SVivek Goyal }
753e43473b7SVivek Goyal 
7540f3457f6STejun Heo static bool tg_with_in_iops_limit(struct throtl_grp *tg, struct bio *bio,
7550f3457f6STejun Heo 				  unsigned long *wait)
756e43473b7SVivek Goyal {
757e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
7588e89d13fSVivek Goyal 	unsigned int io_allowed;
759e43473b7SVivek Goyal 	unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
760c49c06e4SVivek Goyal 	u64 tmp;
761e43473b7SVivek Goyal 
7628e89d13fSVivek Goyal 	jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
763e43473b7SVivek Goyal 
7648e89d13fSVivek Goyal 	/* Slice has just started. Consider one slice interval */
7658e89d13fSVivek Goyal 	if (!jiffy_elapsed)
7668e89d13fSVivek Goyal 		jiffy_elapsed_rnd = throtl_slice;
7678e89d13fSVivek Goyal 
7688e89d13fSVivek Goyal 	jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
7698e89d13fSVivek Goyal 
770c49c06e4SVivek Goyal 	/*
771c49c06e4SVivek Goyal 	 * jiffy_elapsed_rnd should not be a big value as minimum iops can be
772c49c06e4SVivek Goyal 	 * 1 then at max jiffy elapsed should be equivalent of 1 second as we
773c49c06e4SVivek Goyal 	 * will allow dispatch after 1 second and after that slice should
774c49c06e4SVivek Goyal 	 * have been trimmed.
775c49c06e4SVivek Goyal 	 */
776c49c06e4SVivek Goyal 
777c49c06e4SVivek Goyal 	tmp = (u64)tg->iops[rw] * jiffy_elapsed_rnd;
778c49c06e4SVivek Goyal 	do_div(tmp, HZ);
779c49c06e4SVivek Goyal 
780c49c06e4SVivek Goyal 	if (tmp > UINT_MAX)
781c49c06e4SVivek Goyal 		io_allowed = UINT_MAX;
782c49c06e4SVivek Goyal 	else
783c49c06e4SVivek Goyal 		io_allowed = tmp;
7848e89d13fSVivek Goyal 
7858e89d13fSVivek Goyal 	if (tg->io_disp[rw] + 1 <= io_allowed) {
786e43473b7SVivek Goyal 		if (wait)
787e43473b7SVivek Goyal 			*wait = 0;
788e43473b7SVivek Goyal 		return 1;
789e43473b7SVivek Goyal 	}
790e43473b7SVivek Goyal 
7918e89d13fSVivek Goyal 	/* Calc approx time to dispatch */
7928e89d13fSVivek Goyal 	jiffy_wait = ((tg->io_disp[rw] + 1) * HZ)/tg->iops[rw] + 1;
7938e89d13fSVivek Goyal 
7948e89d13fSVivek Goyal 	if (jiffy_wait > jiffy_elapsed)
7958e89d13fSVivek Goyal 		jiffy_wait = jiffy_wait - jiffy_elapsed;
7968e89d13fSVivek Goyal 	else
7978e89d13fSVivek Goyal 		jiffy_wait = 1;
7988e89d13fSVivek Goyal 
7998e89d13fSVivek Goyal 	if (wait)
8008e89d13fSVivek Goyal 		*wait = jiffy_wait;
8018e89d13fSVivek Goyal 	return 0;
802e43473b7SVivek Goyal }
803e43473b7SVivek Goyal 
8040f3457f6STejun Heo static bool tg_with_in_bps_limit(struct throtl_grp *tg, struct bio *bio,
8050f3457f6STejun Heo 				 unsigned long *wait)
8068e89d13fSVivek Goyal {
8078e89d13fSVivek Goyal 	bool rw = bio_data_dir(bio);
8083aad5d3eSVivek Goyal 	u64 bytes_allowed, extra_bytes, tmp;
8098e89d13fSVivek Goyal 	unsigned long jiffy_elapsed, jiffy_wait, jiffy_elapsed_rnd;
8108e89d13fSVivek Goyal 
811e43473b7SVivek Goyal 	jiffy_elapsed = jiffy_elapsed_rnd = jiffies - tg->slice_start[rw];
812e43473b7SVivek Goyal 
813e43473b7SVivek Goyal 	/* Slice has just started. Consider one slice interval */
814e43473b7SVivek Goyal 	if (!jiffy_elapsed)
815e43473b7SVivek Goyal 		jiffy_elapsed_rnd = throtl_slice;
816e43473b7SVivek Goyal 
817e43473b7SVivek Goyal 	jiffy_elapsed_rnd = roundup(jiffy_elapsed_rnd, throtl_slice);
818e43473b7SVivek Goyal 
8195e901a2bSVivek Goyal 	tmp = tg->bps[rw] * jiffy_elapsed_rnd;
8205e901a2bSVivek Goyal 	do_div(tmp, HZ);
8213aad5d3eSVivek Goyal 	bytes_allowed = tmp;
822e43473b7SVivek Goyal 
823e43473b7SVivek Goyal 	if (tg->bytes_disp[rw] + bio->bi_size <= bytes_allowed) {
824e43473b7SVivek Goyal 		if (wait)
825e43473b7SVivek Goyal 			*wait = 0;
826e43473b7SVivek Goyal 		return 1;
827e43473b7SVivek Goyal 	}
828e43473b7SVivek Goyal 
829e43473b7SVivek Goyal 	/* Calc approx time to dispatch */
830e43473b7SVivek Goyal 	extra_bytes = tg->bytes_disp[rw] + bio->bi_size - bytes_allowed;
831e43473b7SVivek Goyal 	jiffy_wait = div64_u64(extra_bytes * HZ, tg->bps[rw]);
832e43473b7SVivek Goyal 
833e43473b7SVivek Goyal 	if (!jiffy_wait)
834e43473b7SVivek Goyal 		jiffy_wait = 1;
835e43473b7SVivek Goyal 
836e43473b7SVivek Goyal 	/*
837e43473b7SVivek Goyal 	 * This wait time is without taking into consideration the rounding
838e43473b7SVivek Goyal 	 * up we did. Add that time also.
839e43473b7SVivek Goyal 	 */
840e43473b7SVivek Goyal 	jiffy_wait = jiffy_wait + (jiffy_elapsed_rnd - jiffy_elapsed);
841e43473b7SVivek Goyal 	if (wait)
842e43473b7SVivek Goyal 		*wait = jiffy_wait;
8438e89d13fSVivek Goyal 	return 0;
8448e89d13fSVivek Goyal }
845e43473b7SVivek Goyal 
846af75cd3cSVivek Goyal static bool tg_no_rule_group(struct throtl_grp *tg, bool rw) {
847af75cd3cSVivek Goyal 	if (tg->bps[rw] == -1 && tg->iops[rw] == -1)
848af75cd3cSVivek Goyal 		return 1;
849af75cd3cSVivek Goyal 	return 0;
850af75cd3cSVivek Goyal }
851af75cd3cSVivek Goyal 
8528e89d13fSVivek Goyal /*
8538e89d13fSVivek Goyal  * Returns whether one can dispatch a bio or not. Also returns approx number
8548e89d13fSVivek Goyal  * of jiffies to wait before this bio is with-in IO rate and can be dispatched
8558e89d13fSVivek Goyal  */
8560f3457f6STejun Heo static bool tg_may_dispatch(struct throtl_grp *tg, struct bio *bio,
8570f3457f6STejun Heo 			    unsigned long *wait)
8588e89d13fSVivek Goyal {
8598e89d13fSVivek Goyal 	bool rw = bio_data_dir(bio);
8608e89d13fSVivek Goyal 	unsigned long bps_wait = 0, iops_wait = 0, max_wait = 0;
8618e89d13fSVivek Goyal 
8628e89d13fSVivek Goyal 	/*
8638e89d13fSVivek Goyal  	 * Currently whole state machine of group depends on first bio
8648e89d13fSVivek Goyal 	 * queued in the group bio list. So one should not be calling
8658e89d13fSVivek Goyal 	 * this function with a different bio if there are other bios
8668e89d13fSVivek Goyal 	 * queued.
8678e89d13fSVivek Goyal 	 */
86873f0d49aSTejun Heo 	BUG_ON(tg->service_queue.nr_queued[rw] &&
869c5cc2070STejun Heo 	       bio != throtl_peek_queued(&tg->service_queue.queued[rw]));
8708e89d13fSVivek Goyal 
8718e89d13fSVivek Goyal 	/* If tg->bps = -1, then BW is unlimited */
8728e89d13fSVivek Goyal 	if (tg->bps[rw] == -1 && tg->iops[rw] == -1) {
8738e89d13fSVivek Goyal 		if (wait)
8748e89d13fSVivek Goyal 			*wait = 0;
8758e89d13fSVivek Goyal 		return 1;
8768e89d13fSVivek Goyal 	}
8778e89d13fSVivek Goyal 
8788e89d13fSVivek Goyal 	/*
8798e89d13fSVivek Goyal 	 * If previous slice expired, start a new one otherwise renew/extend
8808e89d13fSVivek Goyal 	 * existing slice to make sure it is at least throtl_slice interval
8818e89d13fSVivek Goyal 	 * long since now.
8828e89d13fSVivek Goyal 	 */
8830f3457f6STejun Heo 	if (throtl_slice_used(tg, rw))
8840f3457f6STejun Heo 		throtl_start_new_slice(tg, rw);
8858e89d13fSVivek Goyal 	else {
8868e89d13fSVivek Goyal 		if (time_before(tg->slice_end[rw], jiffies + throtl_slice))
8870f3457f6STejun Heo 			throtl_extend_slice(tg, rw, jiffies + throtl_slice);
8888e89d13fSVivek Goyal 	}
8898e89d13fSVivek Goyal 
8900f3457f6STejun Heo 	if (tg_with_in_bps_limit(tg, bio, &bps_wait) &&
8910f3457f6STejun Heo 	    tg_with_in_iops_limit(tg, bio, &iops_wait)) {
8928e89d13fSVivek Goyal 		if (wait)
8938e89d13fSVivek Goyal 			*wait = 0;
8948e89d13fSVivek Goyal 		return 1;
8958e89d13fSVivek Goyal 	}
8968e89d13fSVivek Goyal 
8978e89d13fSVivek Goyal 	max_wait = max(bps_wait, iops_wait);
8988e89d13fSVivek Goyal 
8998e89d13fSVivek Goyal 	if (wait)
9008e89d13fSVivek Goyal 		*wait = max_wait;
9018e89d13fSVivek Goyal 
9028e89d13fSVivek Goyal 	if (time_before(tg->slice_end[rw], jiffies + max_wait))
9030f3457f6STejun Heo 		throtl_extend_slice(tg, rw, jiffies + max_wait);
904e43473b7SVivek Goyal 
905e43473b7SVivek Goyal 	return 0;
906e43473b7SVivek Goyal }
907e43473b7SVivek Goyal 
9083c798398STejun Heo static void throtl_update_dispatch_stats(struct blkcg_gq *blkg, u64 bytes,
909629ed0b1STejun Heo 					 int rw)
910629ed0b1STejun Heo {
9118a3d2615STejun Heo 	struct throtl_grp *tg = blkg_to_tg(blkg);
9128a3d2615STejun Heo 	struct tg_stats_cpu *stats_cpu;
913629ed0b1STejun Heo 	unsigned long flags;
914629ed0b1STejun Heo 
915629ed0b1STejun Heo 	/* If per cpu stats are not allocated yet, don't do any accounting. */
9168a3d2615STejun Heo 	if (tg->stats_cpu == NULL)
917629ed0b1STejun Heo 		return;
918629ed0b1STejun Heo 
919629ed0b1STejun Heo 	/*
920629ed0b1STejun Heo 	 * Disabling interrupts to provide mutual exclusion between two
921629ed0b1STejun Heo 	 * writes on same cpu. It probably is not needed for 64bit. Not
922629ed0b1STejun Heo 	 * optimizing that case yet.
923629ed0b1STejun Heo 	 */
924629ed0b1STejun Heo 	local_irq_save(flags);
925629ed0b1STejun Heo 
9268a3d2615STejun Heo 	stats_cpu = this_cpu_ptr(tg->stats_cpu);
927629ed0b1STejun Heo 
928629ed0b1STejun Heo 	blkg_rwstat_add(&stats_cpu->serviced, rw, 1);
929629ed0b1STejun Heo 	blkg_rwstat_add(&stats_cpu->service_bytes, rw, bytes);
930629ed0b1STejun Heo 
931629ed0b1STejun Heo 	local_irq_restore(flags);
932629ed0b1STejun Heo }
933629ed0b1STejun Heo 
934e43473b7SVivek Goyal static void throtl_charge_bio(struct throtl_grp *tg, struct bio *bio)
935e43473b7SVivek Goyal {
936e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
937e43473b7SVivek Goyal 
938e43473b7SVivek Goyal 	/* Charge the bio to the group */
939e43473b7SVivek Goyal 	tg->bytes_disp[rw] += bio->bi_size;
9408e89d13fSVivek Goyal 	tg->io_disp[rw]++;
941e43473b7SVivek Goyal 
9422a0f61e6STejun Heo 	/*
9432a0f61e6STejun Heo 	 * REQ_THROTTLED is used to prevent the same bio to be throttled
9442a0f61e6STejun Heo 	 * more than once as a throttled bio will go through blk-throtl the
9452a0f61e6STejun Heo 	 * second time when it eventually gets issued.  Set it when a bio
9462a0f61e6STejun Heo 	 * is being charged to a tg.
9472a0f61e6STejun Heo 	 *
9482a0f61e6STejun Heo 	 * Dispatch stats aren't recursive and each @bio should only be
9492a0f61e6STejun Heo 	 * accounted by the @tg it was originally associated with.  Let's
9502a0f61e6STejun Heo 	 * update the stats when setting REQ_THROTTLED for the first time
9512a0f61e6STejun Heo 	 * which is guaranteed to be for the @bio's original tg.
9522a0f61e6STejun Heo 	 */
9532a0f61e6STejun Heo 	if (!(bio->bi_rw & REQ_THROTTLED)) {
9542a0f61e6STejun Heo 		bio->bi_rw |= REQ_THROTTLED;
9552a0f61e6STejun Heo 		throtl_update_dispatch_stats(tg_to_blkg(tg), bio->bi_size,
9562a0f61e6STejun Heo 					     bio->bi_rw);
9572a0f61e6STejun Heo 	}
958e43473b7SVivek Goyal }
959e43473b7SVivek Goyal 
960c5cc2070STejun Heo /**
961c5cc2070STejun Heo  * throtl_add_bio_tg - add a bio to the specified throtl_grp
962c5cc2070STejun Heo  * @bio: bio to add
963c5cc2070STejun Heo  * @qn: qnode to use
964c5cc2070STejun Heo  * @tg: the target throtl_grp
965c5cc2070STejun Heo  *
966c5cc2070STejun Heo  * Add @bio to @tg's service_queue using @qn.  If @qn is not specified,
967c5cc2070STejun Heo  * tg->qnode_on_self[] is used.
968c5cc2070STejun Heo  */
969c5cc2070STejun Heo static void throtl_add_bio_tg(struct bio *bio, struct throtl_qnode *qn,
970c5cc2070STejun Heo 			      struct throtl_grp *tg)
971e43473b7SVivek Goyal {
97273f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
973e43473b7SVivek Goyal 	bool rw = bio_data_dir(bio);
974e43473b7SVivek Goyal 
975c5cc2070STejun Heo 	if (!qn)
976c5cc2070STejun Heo 		qn = &tg->qnode_on_self[rw];
977c5cc2070STejun Heo 
9780e9f4164STejun Heo 	/*
9790e9f4164STejun Heo 	 * If @tg doesn't currently have any bios queued in the same
9800e9f4164STejun Heo 	 * direction, queueing @bio can change when @tg should be
9810e9f4164STejun Heo 	 * dispatched.  Mark that @tg was empty.  This is automatically
9820e9f4164STejun Heo 	 * cleaered on the next tg_update_disptime().
9830e9f4164STejun Heo 	 */
9840e9f4164STejun Heo 	if (!sq->nr_queued[rw])
9850e9f4164STejun Heo 		tg->flags |= THROTL_TG_WAS_EMPTY;
9860e9f4164STejun Heo 
987c5cc2070STejun Heo 	throtl_qnode_add_bio(bio, qn, &sq->queued[rw]);
988c5cc2070STejun Heo 
98973f0d49aSTejun Heo 	sq->nr_queued[rw]++;
99077216b04STejun Heo 	throtl_enqueue_tg(tg);
991e43473b7SVivek Goyal }
992e43473b7SVivek Goyal 
99377216b04STejun Heo static void tg_update_disptime(struct throtl_grp *tg)
994e43473b7SVivek Goyal {
99573f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
996e43473b7SVivek Goyal 	unsigned long read_wait = -1, write_wait = -1, min_wait = -1, disptime;
997e43473b7SVivek Goyal 	struct bio *bio;
998e43473b7SVivek Goyal 
999c5cc2070STejun Heo 	if ((bio = throtl_peek_queued(&sq->queued[READ])))
10000f3457f6STejun Heo 		tg_may_dispatch(tg, bio, &read_wait);
1001e43473b7SVivek Goyal 
1002c5cc2070STejun Heo 	if ((bio = throtl_peek_queued(&sq->queued[WRITE])))
10030f3457f6STejun Heo 		tg_may_dispatch(tg, bio, &write_wait);
1004e43473b7SVivek Goyal 
1005e43473b7SVivek Goyal 	min_wait = min(read_wait, write_wait);
1006e43473b7SVivek Goyal 	disptime = jiffies + min_wait;
1007e43473b7SVivek Goyal 
1008e43473b7SVivek Goyal 	/* Update dispatch time */
100977216b04STejun Heo 	throtl_dequeue_tg(tg);
1010e43473b7SVivek Goyal 	tg->disptime = disptime;
101177216b04STejun Heo 	throtl_enqueue_tg(tg);
10120e9f4164STejun Heo 
10130e9f4164STejun Heo 	/* see throtl_add_bio_tg() */
10140e9f4164STejun Heo 	tg->flags &= ~THROTL_TG_WAS_EMPTY;
1015e43473b7SVivek Goyal }
1016e43473b7SVivek Goyal 
1017*32ee5bc4SVivek Goyal static void start_parent_slice_with_credit(struct throtl_grp *child_tg,
1018*32ee5bc4SVivek Goyal 					struct throtl_grp *parent_tg, bool rw)
1019*32ee5bc4SVivek Goyal {
1020*32ee5bc4SVivek Goyal 	if (throtl_slice_used(parent_tg, rw)) {
1021*32ee5bc4SVivek Goyal 		throtl_start_new_slice_with_credit(parent_tg, rw,
1022*32ee5bc4SVivek Goyal 				child_tg->slice_start[rw]);
1023*32ee5bc4SVivek Goyal 	}
1024*32ee5bc4SVivek Goyal 
1025*32ee5bc4SVivek Goyal }
1026*32ee5bc4SVivek Goyal 
102777216b04STejun Heo static void tg_dispatch_one_bio(struct throtl_grp *tg, bool rw)
1028e43473b7SVivek Goyal {
102973f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
10306bc9c2b4STejun Heo 	struct throtl_service_queue *parent_sq = sq->parent_sq;
10316bc9c2b4STejun Heo 	struct throtl_grp *parent_tg = sq_to_tg(parent_sq);
1032c5cc2070STejun Heo 	struct throtl_grp *tg_to_put = NULL;
1033e43473b7SVivek Goyal 	struct bio *bio;
1034e43473b7SVivek Goyal 
1035c5cc2070STejun Heo 	/*
1036c5cc2070STejun Heo 	 * @bio is being transferred from @tg to @parent_sq.  Popping a bio
1037c5cc2070STejun Heo 	 * from @tg may put its reference and @parent_sq might end up
1038c5cc2070STejun Heo 	 * getting released prematurely.  Remember the tg to put and put it
1039c5cc2070STejun Heo 	 * after @bio is transferred to @parent_sq.
1040c5cc2070STejun Heo 	 */
1041c5cc2070STejun Heo 	bio = throtl_pop_queued(&sq->queued[rw], &tg_to_put);
104273f0d49aSTejun Heo 	sq->nr_queued[rw]--;
1043e43473b7SVivek Goyal 
1044e43473b7SVivek Goyal 	throtl_charge_bio(tg, bio);
10456bc9c2b4STejun Heo 
10466bc9c2b4STejun Heo 	/*
10476bc9c2b4STejun Heo 	 * If our parent is another tg, we just need to transfer @bio to
10486bc9c2b4STejun Heo 	 * the parent using throtl_add_bio_tg().  If our parent is
10496bc9c2b4STejun Heo 	 * @td->service_queue, @bio is ready to be issued.  Put it on its
10506bc9c2b4STejun Heo 	 * bio_lists[] and decrease total number queued.  The caller is
10516bc9c2b4STejun Heo 	 * responsible for issuing these bios.
10526bc9c2b4STejun Heo 	 */
10536bc9c2b4STejun Heo 	if (parent_tg) {
1054c5cc2070STejun Heo 		throtl_add_bio_tg(bio, &tg->qnode_on_parent[rw], parent_tg);
1055*32ee5bc4SVivek Goyal 		start_parent_slice_with_credit(tg, parent_tg, rw);
10566bc9c2b4STejun Heo 	} else {
1057c5cc2070STejun Heo 		throtl_qnode_add_bio(bio, &tg->qnode_on_parent[rw],
1058c5cc2070STejun Heo 				     &parent_sq->queued[rw]);
10596bc9c2b4STejun Heo 		BUG_ON(tg->td->nr_queued[rw] <= 0);
10606bc9c2b4STejun Heo 		tg->td->nr_queued[rw]--;
10616bc9c2b4STejun Heo 	}
1062e43473b7SVivek Goyal 
10630f3457f6STejun Heo 	throtl_trim_slice(tg, rw);
10646bc9c2b4STejun Heo 
1065c5cc2070STejun Heo 	if (tg_to_put)
1066c5cc2070STejun Heo 		blkg_put(tg_to_blkg(tg_to_put));
1067e43473b7SVivek Goyal }
1068e43473b7SVivek Goyal 
106977216b04STejun Heo static int throtl_dispatch_tg(struct throtl_grp *tg)
1070e43473b7SVivek Goyal {
107173f0d49aSTejun Heo 	struct throtl_service_queue *sq = &tg->service_queue;
1072e43473b7SVivek Goyal 	unsigned int nr_reads = 0, nr_writes = 0;
1073e43473b7SVivek Goyal 	unsigned int max_nr_reads = throtl_grp_quantum*3/4;
1074c2f6805dSVivek Goyal 	unsigned int max_nr_writes = throtl_grp_quantum - max_nr_reads;
1075e43473b7SVivek Goyal 	struct bio *bio;
1076e43473b7SVivek Goyal 
1077e43473b7SVivek Goyal 	/* Try to dispatch 75% READS and 25% WRITES */
1078e43473b7SVivek Goyal 
1079c5cc2070STejun Heo 	while ((bio = throtl_peek_queued(&sq->queued[READ])) &&
10800f3457f6STejun Heo 	       tg_may_dispatch(tg, bio, NULL)) {
1081e43473b7SVivek Goyal 
108277216b04STejun Heo 		tg_dispatch_one_bio(tg, bio_data_dir(bio));
1083e43473b7SVivek Goyal 		nr_reads++;
1084e43473b7SVivek Goyal 
1085e43473b7SVivek Goyal 		if (nr_reads >= max_nr_reads)
1086e43473b7SVivek Goyal 			break;
1087e43473b7SVivek Goyal 	}
1088e43473b7SVivek Goyal 
1089c5cc2070STejun Heo 	while ((bio = throtl_peek_queued(&sq->queued[WRITE])) &&
10900f3457f6STejun Heo 	       tg_may_dispatch(tg, bio, NULL)) {
1091e43473b7SVivek Goyal 
109277216b04STejun Heo 		tg_dispatch_one_bio(tg, bio_data_dir(bio));
1093e43473b7SVivek Goyal 		nr_writes++;
1094e43473b7SVivek Goyal 
1095e43473b7SVivek Goyal 		if (nr_writes >= max_nr_writes)
1096e43473b7SVivek Goyal 			break;
1097e43473b7SVivek Goyal 	}
1098e43473b7SVivek Goyal 
1099e43473b7SVivek Goyal 	return nr_reads + nr_writes;
1100e43473b7SVivek Goyal }
1101e43473b7SVivek Goyal 
1102651930bcSTejun Heo static int throtl_select_dispatch(struct throtl_service_queue *parent_sq)
1103e43473b7SVivek Goyal {
1104e43473b7SVivek Goyal 	unsigned int nr_disp = 0;
1105e43473b7SVivek Goyal 
1106e43473b7SVivek Goyal 	while (1) {
110773f0d49aSTejun Heo 		struct throtl_grp *tg = throtl_rb_first(parent_sq);
110873f0d49aSTejun Heo 		struct throtl_service_queue *sq = &tg->service_queue;
1109e43473b7SVivek Goyal 
1110e43473b7SVivek Goyal 		if (!tg)
1111e43473b7SVivek Goyal 			break;
1112e43473b7SVivek Goyal 
1113e43473b7SVivek Goyal 		if (time_before(jiffies, tg->disptime))
1114e43473b7SVivek Goyal 			break;
1115e43473b7SVivek Goyal 
111677216b04STejun Heo 		throtl_dequeue_tg(tg);
1117e43473b7SVivek Goyal 
111877216b04STejun Heo 		nr_disp += throtl_dispatch_tg(tg);
1119e43473b7SVivek Goyal 
112073f0d49aSTejun Heo 		if (sq->nr_queued[0] || sq->nr_queued[1])
112177216b04STejun Heo 			tg_update_disptime(tg);
1122e43473b7SVivek Goyal 
1123e43473b7SVivek Goyal 		if (nr_disp >= throtl_quantum)
1124e43473b7SVivek Goyal 			break;
1125e43473b7SVivek Goyal 	}
1126e43473b7SVivek Goyal 
1127e43473b7SVivek Goyal 	return nr_disp;
1128e43473b7SVivek Goyal }
1129e43473b7SVivek Goyal 
11306e1a5704STejun Heo /**
11316e1a5704STejun Heo  * throtl_pending_timer_fn - timer function for service_queue->pending_timer
11326e1a5704STejun Heo  * @arg: the throtl_service_queue being serviced
11336e1a5704STejun Heo  *
11346e1a5704STejun Heo  * This timer is armed when a child throtl_grp with active bio's become
11356e1a5704STejun Heo  * pending and queued on the service_queue's pending_tree and expires when
11366e1a5704STejun Heo  * the first child throtl_grp should be dispatched.  This function
11372e48a530STejun Heo  * dispatches bio's from the children throtl_grps to the parent
11382e48a530STejun Heo  * service_queue.
11392e48a530STejun Heo  *
11402e48a530STejun Heo  * If the parent's parent is another throtl_grp, dispatching is propagated
11412e48a530STejun Heo  * by either arming its pending_timer or repeating dispatch directly.  If
11422e48a530STejun Heo  * the top-level service_tree is reached, throtl_data->dispatch_work is
11432e48a530STejun Heo  * kicked so that the ready bio's are issued.
11446e1a5704STejun Heo  */
114569df0ab0STejun Heo static void throtl_pending_timer_fn(unsigned long arg)
114669df0ab0STejun Heo {
114769df0ab0STejun Heo 	struct throtl_service_queue *sq = (void *)arg;
11482e48a530STejun Heo 	struct throtl_grp *tg = sq_to_tg(sq);
114969df0ab0STejun Heo 	struct throtl_data *td = sq_to_td(sq);
1150cb76199cSTejun Heo 	struct request_queue *q = td->queue;
11512e48a530STejun Heo 	struct throtl_service_queue *parent_sq;
11522e48a530STejun Heo 	bool dispatched;
11536e1a5704STejun Heo 	int ret;
1154e43473b7SVivek Goyal 
1155e43473b7SVivek Goyal 	spin_lock_irq(q->queue_lock);
11562e48a530STejun Heo again:
11572e48a530STejun Heo 	parent_sq = sq->parent_sq;
11582e48a530STejun Heo 	dispatched = false;
1159e43473b7SVivek Goyal 
11607f52f98cSTejun Heo 	while (true) {
1161fda6f272STejun Heo 		throtl_log(sq, "dispatch nr_queued=%u read=%u write=%u",
11622e48a530STejun Heo 			   sq->nr_queued[READ] + sq->nr_queued[WRITE],
11632e48a530STejun Heo 			   sq->nr_queued[READ], sq->nr_queued[WRITE]);
1164e43473b7SVivek Goyal 
11657f52f98cSTejun Heo 		ret = throtl_select_dispatch(sq);
11667f52f98cSTejun Heo 		if (ret) {
11677f52f98cSTejun Heo 			throtl_log(sq, "bios disp=%u", ret);
11687f52f98cSTejun Heo 			dispatched = true;
1169651930bcSTejun Heo 		}
1170e43473b7SVivek Goyal 
11717f52f98cSTejun Heo 		if (throtl_schedule_next_dispatch(sq, false))
11727f52f98cSTejun Heo 			break;
11737f52f98cSTejun Heo 
11747f52f98cSTejun Heo 		/* this dispatch windows is still open, relax and repeat */
11757f52f98cSTejun Heo 		spin_unlock_irq(q->queue_lock);
11767f52f98cSTejun Heo 		cpu_relax();
11777f52f98cSTejun Heo 		spin_lock_irq(q->queue_lock);
11787f52f98cSTejun Heo 	}
11796a525600STejun Heo 
11802e48a530STejun Heo 	if (!dispatched)
11812e48a530STejun Heo 		goto out_unlock;
11826e1a5704STejun Heo 
11832e48a530STejun Heo 	if (parent_sq) {
11842e48a530STejun Heo 		/* @parent_sq is another throl_grp, propagate dispatch */
11852e48a530STejun Heo 		if (tg->flags & THROTL_TG_WAS_EMPTY) {
11862e48a530STejun Heo 			tg_update_disptime(tg);
11872e48a530STejun Heo 			if (!throtl_schedule_next_dispatch(parent_sq, false)) {
11882e48a530STejun Heo 				/* window is already open, repeat dispatching */
11892e48a530STejun Heo 				sq = parent_sq;
11902e48a530STejun Heo 				tg = sq_to_tg(sq);
11912e48a530STejun Heo 				goto again;
11922e48a530STejun Heo 			}
11932e48a530STejun Heo 		}
11942e48a530STejun Heo 	} else {
11952e48a530STejun Heo 		/* reached the top-level, queue issueing */
11962e48a530STejun Heo 		queue_work(kthrotld_workqueue, &td->dispatch_work);
11972e48a530STejun Heo 	}
11982e48a530STejun Heo out_unlock:
11996e1a5704STejun Heo 	spin_unlock_irq(q->queue_lock);
12006e1a5704STejun Heo }
12016e1a5704STejun Heo 
12026e1a5704STejun Heo /**
12036e1a5704STejun Heo  * blk_throtl_dispatch_work_fn - work function for throtl_data->dispatch_work
12046e1a5704STejun Heo  * @work: work item being executed
12056e1a5704STejun Heo  *
12066e1a5704STejun Heo  * This function is queued for execution when bio's reach the bio_lists[]
12076e1a5704STejun Heo  * of throtl_data->service_queue.  Those bio's are ready and issued by this
12086e1a5704STejun Heo  * function.
12096e1a5704STejun Heo  */
12106e1a5704STejun Heo void blk_throtl_dispatch_work_fn(struct work_struct *work)
12116e1a5704STejun Heo {
12126e1a5704STejun Heo 	struct throtl_data *td = container_of(work, struct throtl_data,
12136e1a5704STejun Heo 					      dispatch_work);
12146e1a5704STejun Heo 	struct throtl_service_queue *td_sq = &td->service_queue;
12156e1a5704STejun Heo 	struct request_queue *q = td->queue;
12166e1a5704STejun Heo 	struct bio_list bio_list_on_stack;
12176e1a5704STejun Heo 	struct bio *bio;
12186e1a5704STejun Heo 	struct blk_plug plug;
12196e1a5704STejun Heo 	int rw;
12206e1a5704STejun Heo 
12216e1a5704STejun Heo 	bio_list_init(&bio_list_on_stack);
12226e1a5704STejun Heo 
12236e1a5704STejun Heo 	spin_lock_irq(q->queue_lock);
1224c5cc2070STejun Heo 	for (rw = READ; rw <= WRITE; rw++)
1225c5cc2070STejun Heo 		while ((bio = throtl_pop_queued(&td_sq->queued[rw], NULL)))
1226c5cc2070STejun Heo 			bio_list_add(&bio_list_on_stack, bio);
1227e43473b7SVivek Goyal 	spin_unlock_irq(q->queue_lock);
1228e43473b7SVivek Goyal 
12296e1a5704STejun Heo 	if (!bio_list_empty(&bio_list_on_stack)) {
123069d60eb9SVivek Goyal 		blk_start_plug(&plug);
1231e43473b7SVivek Goyal 		while((bio = bio_list_pop(&bio_list_on_stack)))
1232e43473b7SVivek Goyal 			generic_make_request(bio);
123369d60eb9SVivek Goyal 		blk_finish_plug(&plug);
1234e43473b7SVivek Goyal 	}
1235e43473b7SVivek Goyal }
1236e43473b7SVivek Goyal 
1237f95a04afSTejun Heo static u64 tg_prfill_cpu_rwstat(struct seq_file *sf,
1238f95a04afSTejun Heo 				struct blkg_policy_data *pd, int off)
123941b38b6dSTejun Heo {
1240f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
124141b38b6dSTejun Heo 	struct blkg_rwstat rwstat = { }, tmp;
124241b38b6dSTejun Heo 	int i, cpu;
124341b38b6dSTejun Heo 
124441b38b6dSTejun Heo 	for_each_possible_cpu(cpu) {
12458a3d2615STejun Heo 		struct tg_stats_cpu *sc = per_cpu_ptr(tg->stats_cpu, cpu);
124641b38b6dSTejun Heo 
124741b38b6dSTejun Heo 		tmp = blkg_rwstat_read((void *)sc + off);
124841b38b6dSTejun Heo 		for (i = 0; i < BLKG_RWSTAT_NR; i++)
124941b38b6dSTejun Heo 			rwstat.cnt[i] += tmp.cnt[i];
125041b38b6dSTejun Heo 	}
125141b38b6dSTejun Heo 
1252f95a04afSTejun Heo 	return __blkg_prfill_rwstat(sf, pd, &rwstat);
125341b38b6dSTejun Heo }
125441b38b6dSTejun Heo 
12558a3d2615STejun Heo static int tg_print_cpu_rwstat(struct cgroup *cgrp, struct cftype *cft,
125641b38b6dSTejun Heo 			       struct seq_file *sf)
125741b38b6dSTejun Heo {
12583c798398STejun Heo 	struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
125941b38b6dSTejun Heo 
12603c798398STejun Heo 	blkcg_print_blkgs(sf, blkcg, tg_prfill_cpu_rwstat, &blkcg_policy_throtl,
12615bc4afb1STejun Heo 			  cft->private, true);
126241b38b6dSTejun Heo 	return 0;
126341b38b6dSTejun Heo }
126441b38b6dSTejun Heo 
1265f95a04afSTejun Heo static u64 tg_prfill_conf_u64(struct seq_file *sf, struct blkg_policy_data *pd,
1266f95a04afSTejun Heo 			      int off)
126760c2bc2dSTejun Heo {
1268f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
1269f95a04afSTejun Heo 	u64 v = *(u64 *)((void *)tg + off);
127060c2bc2dSTejun Heo 
1271af133cebSTejun Heo 	if (v == -1)
127260c2bc2dSTejun Heo 		return 0;
1273f95a04afSTejun Heo 	return __blkg_prfill_u64(sf, pd, v);
127460c2bc2dSTejun Heo }
127560c2bc2dSTejun Heo 
1276f95a04afSTejun Heo static u64 tg_prfill_conf_uint(struct seq_file *sf, struct blkg_policy_data *pd,
1277f95a04afSTejun Heo 			       int off)
1278af133cebSTejun Heo {
1279f95a04afSTejun Heo 	struct throtl_grp *tg = pd_to_tg(pd);
1280f95a04afSTejun Heo 	unsigned int v = *(unsigned int *)((void *)tg + off);
1281af133cebSTejun Heo 
1282af133cebSTejun Heo 	if (v == -1)
1283af133cebSTejun Heo 		return 0;
1284f95a04afSTejun Heo 	return __blkg_prfill_u64(sf, pd, v);
1285af133cebSTejun Heo }
1286af133cebSTejun Heo 
1287af133cebSTejun Heo static int tg_print_conf_u64(struct cgroup *cgrp, struct cftype *cft,
128860c2bc2dSTejun Heo 			     struct seq_file *sf)
128960c2bc2dSTejun Heo {
12903c798398STejun Heo 	blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_u64,
12913c798398STejun Heo 			  &blkcg_policy_throtl, cft->private, false);
129260c2bc2dSTejun Heo 	return 0;
129360c2bc2dSTejun Heo }
129460c2bc2dSTejun Heo 
1295af133cebSTejun Heo static int tg_print_conf_uint(struct cgroup *cgrp, struct cftype *cft,
1296af133cebSTejun Heo 			      struct seq_file *sf)
1297e43473b7SVivek Goyal {
12983c798398STejun Heo 	blkcg_print_blkgs(sf, cgroup_to_blkcg(cgrp), tg_prfill_conf_uint,
12993c798398STejun Heo 			  &blkcg_policy_throtl, cft->private, false);
1300af133cebSTejun Heo 	return 0;
1301e43473b7SVivek Goyal }
1302e43473b7SVivek Goyal 
1303af133cebSTejun Heo static int tg_set_conf(struct cgroup *cgrp, struct cftype *cft, const char *buf,
1304af133cebSTejun Heo 		       bool is_u64)
130560c2bc2dSTejun Heo {
13063c798398STejun Heo 	struct blkcg *blkcg = cgroup_to_blkcg(cgrp);
130760c2bc2dSTejun Heo 	struct blkg_conf_ctx ctx;
1308af133cebSTejun Heo 	struct throtl_grp *tg;
130969df0ab0STejun Heo 	struct throtl_service_queue *sq;
131060c2bc2dSTejun Heo 	int ret;
131160c2bc2dSTejun Heo 
13123c798398STejun Heo 	ret = blkg_conf_prep(blkcg, &blkcg_policy_throtl, buf, &ctx);
131360c2bc2dSTejun Heo 	if (ret)
131460c2bc2dSTejun Heo 		return ret;
131560c2bc2dSTejun Heo 
1316af133cebSTejun Heo 	tg = blkg_to_tg(ctx.blkg);
131769df0ab0STejun Heo 	sq = &tg->service_queue;
1318af133cebSTejun Heo 
1319af133cebSTejun Heo 	if (!ctx.v)
1320af133cebSTejun Heo 		ctx.v = -1;
1321af133cebSTejun Heo 
1322af133cebSTejun Heo 	if (is_u64)
1323af133cebSTejun Heo 		*(u64 *)((void *)tg + cft->private) = ctx.v;
1324af133cebSTejun Heo 	else
1325af133cebSTejun Heo 		*(unsigned int *)((void *)tg + cft->private) = ctx.v;
1326af133cebSTejun Heo 
1327fda6f272STejun Heo 	throtl_log(&tg->service_queue,
1328fda6f272STejun Heo 		   "limit change rbps=%llu wbps=%llu riops=%u wiops=%u",
1329632b4493STejun Heo 		   tg->bps[READ], tg->bps[WRITE],
1330632b4493STejun Heo 		   tg->iops[READ], tg->iops[WRITE]);
1331632b4493STejun Heo 
1332632b4493STejun Heo 	/*
1333632b4493STejun Heo 	 * We're already holding queue_lock and know @tg is valid.  Let's
1334632b4493STejun Heo 	 * apply the new config directly.
1335632b4493STejun Heo 	 *
1336632b4493STejun Heo 	 * Restart the slices for both READ and WRITES. It might happen
1337632b4493STejun Heo 	 * that a group's limit are dropped suddenly and we don't want to
1338632b4493STejun Heo 	 * account recently dispatched IO with new low rate.
1339632b4493STejun Heo 	 */
13400f3457f6STejun Heo 	throtl_start_new_slice(tg, 0);
13410f3457f6STejun Heo 	throtl_start_new_slice(tg, 1);
1342632b4493STejun Heo 
13435b2c16aaSTejun Heo 	if (tg->flags & THROTL_TG_PENDING) {
134477216b04STejun Heo 		tg_update_disptime(tg);
13457f52f98cSTejun Heo 		throtl_schedule_next_dispatch(sq->parent_sq, true);
1346632b4493STejun Heo 	}
1347af133cebSTejun Heo 
134860c2bc2dSTejun Heo 	blkg_conf_finish(&ctx);
1349a2b1693bSTejun Heo 	return 0;
135060c2bc2dSTejun Heo }
135160c2bc2dSTejun Heo 
1352af133cebSTejun Heo static int tg_set_conf_u64(struct cgroup *cgrp, struct cftype *cft,
135360c2bc2dSTejun Heo 			   const char *buf)
135460c2bc2dSTejun Heo {
1355af133cebSTejun Heo 	return tg_set_conf(cgrp, cft, buf, true);
135660c2bc2dSTejun Heo }
135760c2bc2dSTejun Heo 
1358af133cebSTejun Heo static int tg_set_conf_uint(struct cgroup *cgrp, struct cftype *cft,
135960c2bc2dSTejun Heo 			    const char *buf)
136060c2bc2dSTejun Heo {
1361af133cebSTejun Heo 	return tg_set_conf(cgrp, cft, buf, false);
136260c2bc2dSTejun Heo }
136360c2bc2dSTejun Heo 
136460c2bc2dSTejun Heo static struct cftype throtl_files[] = {
136560c2bc2dSTejun Heo 	{
136660c2bc2dSTejun Heo 		.name = "throttle.read_bps_device",
1367af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, bps[READ]),
1368af133cebSTejun Heo 		.read_seq_string = tg_print_conf_u64,
1369af133cebSTejun Heo 		.write_string = tg_set_conf_u64,
137060c2bc2dSTejun Heo 		.max_write_len = 256,
137160c2bc2dSTejun Heo 	},
137260c2bc2dSTejun Heo 	{
137360c2bc2dSTejun Heo 		.name = "throttle.write_bps_device",
1374af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, bps[WRITE]),
1375af133cebSTejun Heo 		.read_seq_string = tg_print_conf_u64,
1376af133cebSTejun Heo 		.write_string = tg_set_conf_u64,
137760c2bc2dSTejun Heo 		.max_write_len = 256,
137860c2bc2dSTejun Heo 	},
137960c2bc2dSTejun Heo 	{
138060c2bc2dSTejun Heo 		.name = "throttle.read_iops_device",
1381af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, iops[READ]),
1382af133cebSTejun Heo 		.read_seq_string = tg_print_conf_uint,
1383af133cebSTejun Heo 		.write_string = tg_set_conf_uint,
138460c2bc2dSTejun Heo 		.max_write_len = 256,
138560c2bc2dSTejun Heo 	},
138660c2bc2dSTejun Heo 	{
138760c2bc2dSTejun Heo 		.name = "throttle.write_iops_device",
1388af133cebSTejun Heo 		.private = offsetof(struct throtl_grp, iops[WRITE]),
1389af133cebSTejun Heo 		.read_seq_string = tg_print_conf_uint,
1390af133cebSTejun Heo 		.write_string = tg_set_conf_uint,
139160c2bc2dSTejun Heo 		.max_write_len = 256,
139260c2bc2dSTejun Heo 	},
139360c2bc2dSTejun Heo 	{
139460c2bc2dSTejun Heo 		.name = "throttle.io_service_bytes",
13955bc4afb1STejun Heo 		.private = offsetof(struct tg_stats_cpu, service_bytes),
13968a3d2615STejun Heo 		.read_seq_string = tg_print_cpu_rwstat,
139760c2bc2dSTejun Heo 	},
139860c2bc2dSTejun Heo 	{
139960c2bc2dSTejun Heo 		.name = "throttle.io_serviced",
14005bc4afb1STejun Heo 		.private = offsetof(struct tg_stats_cpu, serviced),
14018a3d2615STejun Heo 		.read_seq_string = tg_print_cpu_rwstat,
140260c2bc2dSTejun Heo 	},
140360c2bc2dSTejun Heo 	{ }	/* terminate */
140460c2bc2dSTejun Heo };
140560c2bc2dSTejun Heo 
1406da527770SVivek Goyal static void throtl_shutdown_wq(struct request_queue *q)
1407e43473b7SVivek Goyal {
1408e43473b7SVivek Goyal 	struct throtl_data *td = q->td;
1409e43473b7SVivek Goyal 
141069df0ab0STejun Heo 	cancel_work_sync(&td->dispatch_work);
1411e43473b7SVivek Goyal }
1412e43473b7SVivek Goyal 
14133c798398STejun Heo static struct blkcg_policy blkcg_policy_throtl = {
1414f9fcc2d3STejun Heo 	.pd_size		= sizeof(struct throtl_grp),
1415f9fcc2d3STejun Heo 	.cftypes		= throtl_files,
1416f9fcc2d3STejun Heo 
14173c798398STejun Heo 	.pd_init_fn		= throtl_pd_init,
14183c798398STejun Heo 	.pd_exit_fn		= throtl_pd_exit,
14193c798398STejun Heo 	.pd_reset_stats_fn	= throtl_pd_reset_stats,
1420e43473b7SVivek Goyal };
1421e43473b7SVivek Goyal 
1422bc16a4f9STejun Heo bool blk_throtl_bio(struct request_queue *q, struct bio *bio)
1423e43473b7SVivek Goyal {
1424e43473b7SVivek Goyal 	struct throtl_data *td = q->td;
1425c5cc2070STejun Heo 	struct throtl_qnode *qn = NULL;
1426e43473b7SVivek Goyal 	struct throtl_grp *tg;
142773f0d49aSTejun Heo 	struct throtl_service_queue *sq;
14280e9f4164STejun Heo 	bool rw = bio_data_dir(bio);
14293c798398STejun Heo 	struct blkcg *blkcg;
1430bc16a4f9STejun Heo 	bool throttled = false;
1431e43473b7SVivek Goyal 
14322a0f61e6STejun Heo 	/* see throtl_charge_bio() */
14332a0f61e6STejun Heo 	if (bio->bi_rw & REQ_THROTTLED)
1434bc16a4f9STejun Heo 		goto out;
1435e43473b7SVivek Goyal 
1436af75cd3cSVivek Goyal 	/*
1437af75cd3cSVivek Goyal 	 * A throtl_grp pointer retrieved under rcu can be used to access
1438af75cd3cSVivek Goyal 	 * basic fields like stats and io rates. If a group has no rules,
1439af75cd3cSVivek Goyal 	 * just update the dispatch stats in lockless manner and return.
1440af75cd3cSVivek Goyal 	 */
1441af75cd3cSVivek Goyal 	rcu_read_lock();
14423c798398STejun Heo 	blkcg = bio_blkcg(bio);
1443cd1604faSTejun Heo 	tg = throtl_lookup_tg(td, blkcg);
1444af75cd3cSVivek Goyal 	if (tg) {
1445af75cd3cSVivek Goyal 		if (tg_no_rule_group(tg, rw)) {
1446629ed0b1STejun Heo 			throtl_update_dispatch_stats(tg_to_blkg(tg),
1447629ed0b1STejun Heo 						     bio->bi_size, bio->bi_rw);
14482a7f1244STejun Heo 			goto out_unlock_rcu;
1449af75cd3cSVivek Goyal 		}
1450af75cd3cSVivek Goyal 	}
1451af75cd3cSVivek Goyal 
1452af75cd3cSVivek Goyal 	/*
1453af75cd3cSVivek Goyal 	 * Either group has not been allocated yet or it is not an unlimited
1454af75cd3cSVivek Goyal 	 * IO group
1455af75cd3cSVivek Goyal 	 */
1456e43473b7SVivek Goyal 	spin_lock_irq(q->queue_lock);
1457cd1604faSTejun Heo 	tg = throtl_lookup_create_tg(td, blkcg);
1458bc16a4f9STejun Heo 	if (unlikely(!tg))
1459bc16a4f9STejun Heo 		goto out_unlock;
1460f469a7b4SVivek Goyal 
146173f0d49aSTejun Heo 	sq = &tg->service_queue;
146273f0d49aSTejun Heo 
14639e660acfSTejun Heo 	while (true) {
14649e660acfSTejun Heo 		/* throtl is FIFO - if bios are already queued, should queue */
14650e9f4164STejun Heo 		if (sq->nr_queued[rw])
14669e660acfSTejun Heo 			break;
1467de701c74SVivek Goyal 
14689e660acfSTejun Heo 		/* if above limits, break to queue */
14699e660acfSTejun Heo 		if (!tg_may_dispatch(tg, bio, NULL))
14709e660acfSTejun Heo 			break;
14719e660acfSTejun Heo 
14729e660acfSTejun Heo 		/* within limits, let's charge and dispatch directly */
1473e43473b7SVivek Goyal 		throtl_charge_bio(tg, bio);
147404521db0SVivek Goyal 
147504521db0SVivek Goyal 		/*
147604521db0SVivek Goyal 		 * We need to trim slice even when bios are not being queued
147704521db0SVivek Goyal 		 * otherwise it might happen that a bio is not queued for
147804521db0SVivek Goyal 		 * a long time and slice keeps on extending and trim is not
147904521db0SVivek Goyal 		 * called for a long time. Now if limits are reduced suddenly
148004521db0SVivek Goyal 		 * we take into account all the IO dispatched so far at new
148104521db0SVivek Goyal 		 * low rate and * newly queued IO gets a really long dispatch
148204521db0SVivek Goyal 		 * time.
148304521db0SVivek Goyal 		 *
148404521db0SVivek Goyal 		 * So keep on trimming slice even if bio is not queued.
148504521db0SVivek Goyal 		 */
14860f3457f6STejun Heo 		throtl_trim_slice(tg, rw);
14879e660acfSTejun Heo 
14889e660acfSTejun Heo 		/*
14899e660acfSTejun Heo 		 * @bio passed through this layer without being throttled.
14909e660acfSTejun Heo 		 * Climb up the ladder.  If we''re already at the top, it
14919e660acfSTejun Heo 		 * can be executed directly.
14929e660acfSTejun Heo 		 */
1493c5cc2070STejun Heo 		qn = &tg->qnode_on_parent[rw];
14949e660acfSTejun Heo 		sq = sq->parent_sq;
14959e660acfSTejun Heo 		tg = sq_to_tg(sq);
14969e660acfSTejun Heo 		if (!tg)
1497bc16a4f9STejun Heo 			goto out_unlock;
1498e43473b7SVivek Goyal 	}
1499e43473b7SVivek Goyal 
15009e660acfSTejun Heo 	/* out-of-limit, queue to @tg */
1501fda6f272STejun Heo 	throtl_log(sq, "[%c] bio. bdisp=%llu sz=%u bps=%llu iodisp=%u iops=%u queued=%d/%d",
15028e89d13fSVivek Goyal 		   rw == READ ? 'R' : 'W',
1503e43473b7SVivek Goyal 		   tg->bytes_disp[rw], bio->bi_size, tg->bps[rw],
15048e89d13fSVivek Goyal 		   tg->io_disp[rw], tg->iops[rw],
150573f0d49aSTejun Heo 		   sq->nr_queued[READ], sq->nr_queued[WRITE]);
1506e43473b7SVivek Goyal 
1507671058fbSTejun Heo 	bio_associate_current(bio);
15086bc9c2b4STejun Heo 	tg->td->nr_queued[rw]++;
1509c5cc2070STejun Heo 	throtl_add_bio_tg(bio, qn, tg);
1510bc16a4f9STejun Heo 	throttled = true;
1511e43473b7SVivek Goyal 
15127f52f98cSTejun Heo 	/*
15137f52f98cSTejun Heo 	 * Update @tg's dispatch time and force schedule dispatch if @tg
15147f52f98cSTejun Heo 	 * was empty before @bio.  The forced scheduling isn't likely to
15157f52f98cSTejun Heo 	 * cause undue delay as @bio is likely to be dispatched directly if
15167f52f98cSTejun Heo 	 * its @tg's disptime is not in the future.
15177f52f98cSTejun Heo 	 */
15180e9f4164STejun Heo 	if (tg->flags & THROTL_TG_WAS_EMPTY) {
151977216b04STejun Heo 		tg_update_disptime(tg);
15207f52f98cSTejun Heo 		throtl_schedule_next_dispatch(tg->service_queue.parent_sq, true);
1521e43473b7SVivek Goyal 	}
1522e43473b7SVivek Goyal 
1523bc16a4f9STejun Heo out_unlock:
1524e43473b7SVivek Goyal 	spin_unlock_irq(q->queue_lock);
15252a7f1244STejun Heo out_unlock_rcu:
15262a7f1244STejun Heo 	rcu_read_unlock();
1527bc16a4f9STejun Heo out:
15282a0f61e6STejun Heo 	/*
15292a0f61e6STejun Heo 	 * As multiple blk-throtls may stack in the same issue path, we
15302a0f61e6STejun Heo 	 * don't want bios to leave with the flag set.  Clear the flag if
15312a0f61e6STejun Heo 	 * being issued.
15322a0f61e6STejun Heo 	 */
15332a0f61e6STejun Heo 	if (!throttled)
15342a0f61e6STejun Heo 		bio->bi_rw &= ~REQ_THROTTLED;
1535bc16a4f9STejun Heo 	return throttled;
1536e43473b7SVivek Goyal }
1537e43473b7SVivek Goyal 
15382a12f0dcSTejun Heo /*
15392a12f0dcSTejun Heo  * Dispatch all bios from all children tg's queued on @parent_sq.  On
15402a12f0dcSTejun Heo  * return, @parent_sq is guaranteed to not have any active children tg's
15412a12f0dcSTejun Heo  * and all bios from previously active tg's are on @parent_sq->bio_lists[].
15422a12f0dcSTejun Heo  */
15432a12f0dcSTejun Heo static void tg_drain_bios(struct throtl_service_queue *parent_sq)
15442a12f0dcSTejun Heo {
15452a12f0dcSTejun Heo 	struct throtl_grp *tg;
15462a12f0dcSTejun Heo 
15472a12f0dcSTejun Heo 	while ((tg = throtl_rb_first(parent_sq))) {
15482a12f0dcSTejun Heo 		struct throtl_service_queue *sq = &tg->service_queue;
15492a12f0dcSTejun Heo 		struct bio *bio;
15502a12f0dcSTejun Heo 
15512a12f0dcSTejun Heo 		throtl_dequeue_tg(tg);
15522a12f0dcSTejun Heo 
1553c5cc2070STejun Heo 		while ((bio = throtl_peek_queued(&sq->queued[READ])))
15542a12f0dcSTejun Heo 			tg_dispatch_one_bio(tg, bio_data_dir(bio));
1555c5cc2070STejun Heo 		while ((bio = throtl_peek_queued(&sq->queued[WRITE])))
15562a12f0dcSTejun Heo 			tg_dispatch_one_bio(tg, bio_data_dir(bio));
15572a12f0dcSTejun Heo 	}
15582a12f0dcSTejun Heo }
15592a12f0dcSTejun Heo 
1560c9a929ddSTejun Heo /**
1561c9a929ddSTejun Heo  * blk_throtl_drain - drain throttled bios
1562c9a929ddSTejun Heo  * @q: request_queue to drain throttled bios for
1563c9a929ddSTejun Heo  *
1564c9a929ddSTejun Heo  * Dispatch all currently throttled bios on @q through ->make_request_fn().
1565c9a929ddSTejun Heo  */
1566c9a929ddSTejun Heo void blk_throtl_drain(struct request_queue *q)
1567c9a929ddSTejun Heo 	__releases(q->queue_lock) __acquires(q->queue_lock)
1568c9a929ddSTejun Heo {
1569c9a929ddSTejun Heo 	struct throtl_data *td = q->td;
15702a12f0dcSTejun Heo 	struct blkcg_gq *blkg;
15712a12f0dcSTejun Heo 	struct cgroup *pos_cgrp;
1572c9a929ddSTejun Heo 	struct bio *bio;
1573651930bcSTejun Heo 	int rw;
1574c9a929ddSTejun Heo 
15758bcb6c7dSAndi Kleen 	queue_lockdep_assert_held(q);
15762a12f0dcSTejun Heo 	rcu_read_lock();
1577c9a929ddSTejun Heo 
15782a12f0dcSTejun Heo 	/*
15792a12f0dcSTejun Heo 	 * Drain each tg while doing post-order walk on the blkg tree, so
15802a12f0dcSTejun Heo 	 * that all bios are propagated to td->service_queue.  It'd be
15812a12f0dcSTejun Heo 	 * better to walk service_queue tree directly but blkg walk is
15822a12f0dcSTejun Heo 	 * easier.
15832a12f0dcSTejun Heo 	 */
15842a12f0dcSTejun Heo 	blkg_for_each_descendant_post(blkg, pos_cgrp, td->queue->root_blkg)
15852a12f0dcSTejun Heo 		tg_drain_bios(&blkg_to_tg(blkg)->service_queue);
158673f0d49aSTejun Heo 
15872a12f0dcSTejun Heo 	tg_drain_bios(&td_root_tg(td)->service_queue);
1588c9a929ddSTejun Heo 
15892a12f0dcSTejun Heo 	/* finally, transfer bios from top-level tg's into the td */
15902a12f0dcSTejun Heo 	tg_drain_bios(&td->service_queue);
15912a12f0dcSTejun Heo 
15922a12f0dcSTejun Heo 	rcu_read_unlock();
1593c9a929ddSTejun Heo 	spin_unlock_irq(q->queue_lock);
1594c9a929ddSTejun Heo 
15952a12f0dcSTejun Heo 	/* all bios now should be in td->service_queue, issue them */
1596651930bcSTejun Heo 	for (rw = READ; rw <= WRITE; rw++)
1597c5cc2070STejun Heo 		while ((bio = throtl_pop_queued(&td->service_queue.queued[rw],
1598c5cc2070STejun Heo 						NULL)))
1599c9a929ddSTejun Heo 			generic_make_request(bio);
1600c9a929ddSTejun Heo 
1601c9a929ddSTejun Heo 	spin_lock_irq(q->queue_lock);
1602c9a929ddSTejun Heo }
1603c9a929ddSTejun Heo 
1604e43473b7SVivek Goyal int blk_throtl_init(struct request_queue *q)
1605e43473b7SVivek Goyal {
1606e43473b7SVivek Goyal 	struct throtl_data *td;
1607a2b1693bSTejun Heo 	int ret;
1608e43473b7SVivek Goyal 
1609e43473b7SVivek Goyal 	td = kzalloc_node(sizeof(*td), GFP_KERNEL, q->node);
1610e43473b7SVivek Goyal 	if (!td)
1611e43473b7SVivek Goyal 		return -ENOMEM;
1612e43473b7SVivek Goyal 
161369df0ab0STejun Heo 	INIT_WORK(&td->dispatch_work, blk_throtl_dispatch_work_fn);
161477216b04STejun Heo 	throtl_service_queue_init(&td->service_queue, NULL);
1615e43473b7SVivek Goyal 
1616cd1604faSTejun Heo 	q->td = td;
161729b12589SVivek Goyal 	td->queue = q;
161802977e4aSVivek Goyal 
1619a2b1693bSTejun Heo 	/* activate policy */
16203c798398STejun Heo 	ret = blkcg_activate_policy(q, &blkcg_policy_throtl);
1621a2b1693bSTejun Heo 	if (ret)
162229b12589SVivek Goyal 		kfree(td);
1623a2b1693bSTejun Heo 	return ret;
1624e43473b7SVivek Goyal }
1625e43473b7SVivek Goyal 
1626e43473b7SVivek Goyal void blk_throtl_exit(struct request_queue *q)
1627e43473b7SVivek Goyal {
1628c875f4d0STejun Heo 	BUG_ON(!q->td);
1629da527770SVivek Goyal 	throtl_shutdown_wq(q);
16303c798398STejun Heo 	blkcg_deactivate_policy(q, &blkcg_policy_throtl);
1631c9a929ddSTejun Heo 	kfree(q->td);
1632e43473b7SVivek Goyal }
1633e43473b7SVivek Goyal 
1634e43473b7SVivek Goyal static int __init throtl_init(void)
1635e43473b7SVivek Goyal {
1636450adcbeSVivek Goyal 	kthrotld_workqueue = alloc_workqueue("kthrotld", WQ_MEM_RECLAIM, 0);
1637450adcbeSVivek Goyal 	if (!kthrotld_workqueue)
1638450adcbeSVivek Goyal 		panic("Failed to create kthrotld\n");
1639450adcbeSVivek Goyal 
16403c798398STejun Heo 	return blkcg_policy_register(&blkcg_policy_throtl);
1641e43473b7SVivek Goyal }
1642e43473b7SVivek Goyal 
1643e43473b7SVivek Goyal module_init(throtl_init);
1644