xref: /linux-6.15/include/linux/blk-cgroup.h (revision 0d1e0c7c)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2eea8f41cSTejun Heo #ifndef _BLK_CGROUP_H
3eea8f41cSTejun Heo #define _BLK_CGROUP_H
4eea8f41cSTejun Heo /*
5eea8f41cSTejun Heo  * Common Block IO controller cgroup interface
6eea8f41cSTejun Heo  *
7eea8f41cSTejun Heo  * Based on ideas and code from CFQ, CFS and BFQ:
8eea8f41cSTejun Heo  * Copyright (C) 2003 Jens Axboe <[email protected]>
9eea8f41cSTejun Heo  *
10eea8f41cSTejun Heo  * Copyright (C) 2008 Fabio Checconi <[email protected]>
11eea8f41cSTejun Heo  *		      Paolo Valente <[email protected]>
12eea8f41cSTejun Heo  *
13eea8f41cSTejun Heo  * Copyright (C) 2009 Vivek Goyal <[email protected]>
14eea8f41cSTejun Heo  * 	              Nauman Rafique <[email protected]>
15eea8f41cSTejun Heo  */
16eea8f41cSTejun Heo 
17eea8f41cSTejun Heo #include <linux/cgroup.h>
1824bdb8efSTejun Heo #include <linux/percpu_counter.h>
19eea8f41cSTejun Heo #include <linux/seq_file.h>
20eea8f41cSTejun Heo #include <linux/radix-tree.h>
21eea8f41cSTejun Heo #include <linux/blkdev.h>
22eea8f41cSTejun Heo #include <linux/atomic.h>
23902ec5b6SShaohua Li #include <linux/kthread.h>
24eea8f41cSTejun Heo 
2524bdb8efSTejun Heo /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
2624bdb8efSTejun Heo #define BLKG_STAT_CPU_BATCH	(INT_MAX / 2)
2724bdb8efSTejun Heo 
28eea8f41cSTejun Heo /* Max limits for throttle policy */
29eea8f41cSTejun Heo #define THROTL_IOPS_MAX		UINT_MAX
30eea8f41cSTejun Heo 
31eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP
32eea8f41cSTejun Heo 
33eea8f41cSTejun Heo enum blkg_rwstat_type {
34eea8f41cSTejun Heo 	BLKG_RWSTAT_READ,
35eea8f41cSTejun Heo 	BLKG_RWSTAT_WRITE,
36eea8f41cSTejun Heo 	BLKG_RWSTAT_SYNC,
37eea8f41cSTejun Heo 	BLKG_RWSTAT_ASYNC,
38eea8f41cSTejun Heo 
39eea8f41cSTejun Heo 	BLKG_RWSTAT_NR,
40eea8f41cSTejun Heo 	BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
41eea8f41cSTejun Heo };
42eea8f41cSTejun Heo 
43eea8f41cSTejun Heo struct blkcg_gq;
44eea8f41cSTejun Heo 
45eea8f41cSTejun Heo struct blkcg {
46eea8f41cSTejun Heo 	struct cgroup_subsys_state	css;
47eea8f41cSTejun Heo 	spinlock_t			lock;
48eea8f41cSTejun Heo 
49eea8f41cSTejun Heo 	struct radix_tree_root		blkg_tree;
5055679c8dSBart Van Assche 	struct blkcg_gq	__rcu		*blkg_hint;
51eea8f41cSTejun Heo 	struct hlist_head		blkg_list;
52eea8f41cSTejun Heo 
5381437648STejun Heo 	struct blkcg_policy_data	*cpd[BLKCG_MAX_POLS];
5452ebea74STejun Heo 
557876f930STejun Heo 	struct list_head		all_blkcgs_node;
5652ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
5752ebea74STejun Heo 	struct list_head		cgwb_list;
5852ebea74STejun Heo #endif
59eea8f41cSTejun Heo };
60eea8f41cSTejun Heo 
61e6269c44STejun Heo /*
62e6269c44STejun Heo  * blkg_[rw]stat->aux_cnt is excluded for local stats but included for
6324bdb8efSTejun Heo  * recursive.  Used to carry stats of dead children, and, for blkg_rwstat,
6424bdb8efSTejun Heo  * to carry result values from read and sum operations.
65e6269c44STejun Heo  */
66eea8f41cSTejun Heo struct blkg_stat {
6724bdb8efSTejun Heo 	struct percpu_counter		cpu_cnt;
68e6269c44STejun Heo 	atomic64_t			aux_cnt;
69eea8f41cSTejun Heo };
70eea8f41cSTejun Heo 
71eea8f41cSTejun Heo struct blkg_rwstat {
7224bdb8efSTejun Heo 	struct percpu_counter		cpu_cnt[BLKG_RWSTAT_NR];
73e6269c44STejun Heo 	atomic64_t			aux_cnt[BLKG_RWSTAT_NR];
74eea8f41cSTejun Heo };
75eea8f41cSTejun Heo 
76eea8f41cSTejun Heo /*
77eea8f41cSTejun Heo  * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
78eea8f41cSTejun Heo  * request_queue (q).  This is used by blkcg policies which need to track
79eea8f41cSTejun Heo  * information per blkcg - q pair.
80eea8f41cSTejun Heo  *
81001bea73STejun Heo  * There can be multiple active blkcg policies and each blkg:policy pair is
82001bea73STejun Heo  * represented by a blkg_policy_data which is allocated and freed by each
83001bea73STejun Heo  * policy's pd_alloc/free_fn() methods.  A policy can allocate private data
84001bea73STejun Heo  * area by allocating larger data structure which embeds blkg_policy_data
85001bea73STejun Heo  * at the beginning.
86eea8f41cSTejun Heo  */
87eea8f41cSTejun Heo struct blkg_policy_data {
88eea8f41cSTejun Heo 	/* the blkg and policy id this per-policy data belongs to */
89eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
90eea8f41cSTejun Heo 	int				plid;
914c699480SJoseph Qi 	bool				offline;
92eea8f41cSTejun Heo };
93eea8f41cSTejun Heo 
94e4bc13adSLinus Torvalds /*
95e4a9bde9STejun Heo  * Policies that need to keep per-blkcg data which is independent from any
96e4a9bde9STejun Heo  * request_queue associated to it should implement cpd_alloc/free_fn()
97e4a9bde9STejun Heo  * methods.  A policy can allocate private data area by allocating larger
98e4a9bde9STejun Heo  * data structure which embeds blkcg_policy_data at the beginning.
99e4a9bde9STejun Heo  * cpd_init() is invoked to let each policy handle per-blkcg data.
100e4bc13adSLinus Torvalds  */
101e4bc13adSLinus Torvalds struct blkcg_policy_data {
10281437648STejun Heo 	/* the blkcg and policy id this per-policy data belongs to */
10381437648STejun Heo 	struct blkcg			*blkcg;
104e4bc13adSLinus Torvalds 	int				plid;
105e4bc13adSLinus Torvalds };
106e4bc13adSLinus Torvalds 
107eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */
108eea8f41cSTejun Heo struct blkcg_gq {
109eea8f41cSTejun Heo 	/* Pointer to the associated request_queue */
110eea8f41cSTejun Heo 	struct request_queue		*q;
111eea8f41cSTejun Heo 	struct list_head		q_node;
112eea8f41cSTejun Heo 	struct hlist_node		blkcg_node;
113eea8f41cSTejun Heo 	struct blkcg			*blkcg;
114eea8f41cSTejun Heo 
115ce7acfeaSTejun Heo 	/*
116ce7acfeaSTejun Heo 	 * Each blkg gets congested separately and the congestion state is
117ce7acfeaSTejun Heo 	 * propagated to the matching bdi_writeback_congested.
118ce7acfeaSTejun Heo 	 */
119ce7acfeaSTejun Heo 	struct bdi_writeback_congested	*wb_congested;
120ce7acfeaSTejun Heo 
121eea8f41cSTejun Heo 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
122eea8f41cSTejun Heo 	struct blkcg_gq			*parent;
123eea8f41cSTejun Heo 
124eea8f41cSTejun Heo 	/* request allocation list for this blkcg-q pair */
125eea8f41cSTejun Heo 	struct request_list		rl;
126eea8f41cSTejun Heo 
127eea8f41cSTejun Heo 	/* reference count */
128eea8f41cSTejun Heo 	atomic_t			refcnt;
129eea8f41cSTejun Heo 
130eea8f41cSTejun Heo 	/* is this blkg online? protected by both blkcg and q locks */
131eea8f41cSTejun Heo 	bool				online;
132eea8f41cSTejun Heo 
13377ea7338STejun Heo 	struct blkg_rwstat		stat_bytes;
13477ea7338STejun Heo 	struct blkg_rwstat		stat_ios;
13577ea7338STejun Heo 
136eea8f41cSTejun Heo 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
137eea8f41cSTejun Heo 
138eea8f41cSTejun Heo 	struct rcu_head			rcu_head;
139eea8f41cSTejun Heo };
140eea8f41cSTejun Heo 
141e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
14281437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
143e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
14469d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
145001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node);
146a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
147a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
148a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
149001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
150a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
151903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
152903d23f0SJosef Bacik 				      size_t size);
153eea8f41cSTejun Heo 
154eea8f41cSTejun Heo struct blkcg_policy {
155eea8f41cSTejun Heo 	int				plid;
156eea8f41cSTejun Heo 	/* cgroup files for the policy */
1572ee867dcSTejun Heo 	struct cftype			*dfl_cftypes;
158880f50e2STejun Heo 	struct cftype			*legacy_cftypes;
159eea8f41cSTejun Heo 
160eea8f41cSTejun Heo 	/* operations */
161e4a9bde9STejun Heo 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
162e4bc13adSLinus Torvalds 	blkcg_pol_init_cpd_fn		*cpd_init_fn;
163e4a9bde9STejun Heo 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
16469d7fde5STejun Heo 	blkcg_pol_bind_cpd_fn		*cpd_bind_fn;
165e4a9bde9STejun Heo 
166001bea73STejun Heo 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
167eea8f41cSTejun Heo 	blkcg_pol_init_pd_fn		*pd_init_fn;
168eea8f41cSTejun Heo 	blkcg_pol_online_pd_fn		*pd_online_fn;
169eea8f41cSTejun Heo 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
170001bea73STejun Heo 	blkcg_pol_free_pd_fn		*pd_free_fn;
171eea8f41cSTejun Heo 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
172903d23f0SJosef Bacik 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
173eea8f41cSTejun Heo };
174eea8f41cSTejun Heo 
175eea8f41cSTejun Heo extern struct blkcg blkcg_root;
176496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css;
177eea8f41cSTejun Heo 
17824f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
17924f29046STejun Heo 				      struct request_queue *q, bool update_hint);
180eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
181eea8f41cSTejun Heo 				    struct request_queue *q);
182eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q);
183eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q);
184eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q);
185eea8f41cSTejun Heo 
186eea8f41cSTejun Heo /* Blkio controller policy registration */
187eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol);
188eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol);
189eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q,
190eea8f41cSTejun Heo 			  const struct blkcg_policy *pol);
191eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q,
192eea8f41cSTejun Heo 			     const struct blkcg_policy *pol);
193eea8f41cSTejun Heo 
194dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg);
195eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
196eea8f41cSTejun Heo 		       u64 (*prfill)(struct seq_file *,
197eea8f41cSTejun Heo 				     struct blkg_policy_data *, int),
198eea8f41cSTejun Heo 		       const struct blkcg_policy *pol, int data,
199eea8f41cSTejun Heo 		       bool show_total);
200eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
201eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
202eea8f41cSTejun Heo 			 const struct blkg_rwstat *rwstat);
203eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
204eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
205eea8f41cSTejun Heo 		       int off);
20677ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v);
20777ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v);
20877ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v);
20977ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v);
210eea8f41cSTejun Heo 
211f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
212f12c74caSTejun Heo 			    struct blkcg_policy *pol, int off);
213f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
214f12c74caSTejun Heo 					     struct blkcg_policy *pol, int off);
215eea8f41cSTejun Heo 
216eea8f41cSTejun Heo struct blkg_conf_ctx {
217eea8f41cSTejun Heo 	struct gendisk			*disk;
218eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
21936aa9e5fSTejun Heo 	char				*body;
220eea8f41cSTejun Heo };
221eea8f41cSTejun Heo 
222eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
22336aa9e5fSTejun Heo 		   char *input, struct blkg_conf_ctx *ctx);
224eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx);
225eea8f41cSTejun Heo 
226eea8f41cSTejun Heo 
227eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
228eea8f41cSTejun Heo {
229eea8f41cSTejun Heo 	return css ? container_of(css, struct blkcg, css) : NULL;
230eea8f41cSTejun Heo }
231eea8f41cSTejun Heo 
232eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio)
233eea8f41cSTejun Heo {
234902ec5b6SShaohua Li 	struct cgroup_subsys_state *css;
235902ec5b6SShaohua Li 
236eea8f41cSTejun Heo 	if (bio && bio->bi_css)
237eea8f41cSTejun Heo 		return css_to_blkcg(bio->bi_css);
238902ec5b6SShaohua Li 	css = kthread_blkcg();
239902ec5b6SShaohua Li 	if (css)
240902ec5b6SShaohua Li 		return css_to_blkcg(css);
241902ec5b6SShaohua Li 	return css_to_blkcg(task_css(current, io_cgrp_id));
242fd383c2dSTejun Heo }
243fd383c2dSTejun Heo 
244eea8f41cSTejun Heo /**
245c7c98fd3SJosef Bacik  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
246c7c98fd3SJosef Bacik  * @return: true if this bio needs to be submitted with the root blkg context.
247c7c98fd3SJosef Bacik  *
248c7c98fd3SJosef Bacik  * In order to avoid priority inversions we sometimes need to issue a bio as if
249c7c98fd3SJosef Bacik  * it were attached to the root blkg, and then backcharge to the actual owning
250c7c98fd3SJosef Bacik  * blkg.  The idea is we do bio_blkcg() to look up the actual context for the
251c7c98fd3SJosef Bacik  * bio and attach the appropriate blkg to the bio.  Then we call this helper and
252c7c98fd3SJosef Bacik  * if it is true run with the root blkg for that queue and then do any
253c7c98fd3SJosef Bacik  * backcharging to the originating cgroup once the io is complete.
254c7c98fd3SJosef Bacik  */
255c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio)
256c7c98fd3SJosef Bacik {
257*0d1e0c7cSJosef Bacik 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
258c7c98fd3SJosef Bacik }
259c7c98fd3SJosef Bacik 
260c7c98fd3SJosef Bacik /**
261eea8f41cSTejun Heo  * blkcg_parent - get the parent of a blkcg
262eea8f41cSTejun Heo  * @blkcg: blkcg of interest
263eea8f41cSTejun Heo  *
264eea8f41cSTejun Heo  * Return the parent blkcg of @blkcg.  Can be called anytime.
265eea8f41cSTejun Heo  */
266eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
267eea8f41cSTejun Heo {
268eea8f41cSTejun Heo 	return css_to_blkcg(blkcg->css.parent);
269eea8f41cSTejun Heo }
270eea8f41cSTejun Heo 
271eea8f41cSTejun Heo /**
27224f29046STejun Heo  * __blkg_lookup - internal version of blkg_lookup()
27324f29046STejun Heo  * @blkcg: blkcg of interest
27424f29046STejun Heo  * @q: request_queue of interest
27524f29046STejun Heo  * @update_hint: whether to update lookup hint with the result or not
27624f29046STejun Heo  *
27724f29046STejun Heo  * This is internal version and shouldn't be used by policy
27824f29046STejun Heo  * implementations.  Looks up blkgs for the @blkcg - @q pair regardless of
27924f29046STejun Heo  * @q's bypass state.  If @update_hint is %true, the caller should be
28024f29046STejun Heo  * holding @q->queue_lock and lookup hint is updated on success.
28124f29046STejun Heo  */
28224f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
28324f29046STejun Heo 					     struct request_queue *q,
28424f29046STejun Heo 					     bool update_hint)
28524f29046STejun Heo {
28624f29046STejun Heo 	struct blkcg_gq *blkg;
28724f29046STejun Heo 
28885b6bc9dSTejun Heo 	if (blkcg == &blkcg_root)
28985b6bc9dSTejun Heo 		return q->root_blkg;
29085b6bc9dSTejun Heo 
29124f29046STejun Heo 	blkg = rcu_dereference(blkcg->blkg_hint);
29224f29046STejun Heo 	if (blkg && blkg->q == q)
29324f29046STejun Heo 		return blkg;
29424f29046STejun Heo 
29524f29046STejun Heo 	return blkg_lookup_slowpath(blkcg, q, update_hint);
29624f29046STejun Heo }
29724f29046STejun Heo 
29824f29046STejun Heo /**
29924f29046STejun Heo  * blkg_lookup - lookup blkg for the specified blkcg - q pair
30024f29046STejun Heo  * @blkcg: blkcg of interest
30124f29046STejun Heo  * @q: request_queue of interest
30224f29046STejun Heo  *
30324f29046STejun Heo  * Lookup blkg for the @blkcg - @q pair.  This function should be called
30424f29046STejun Heo  * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
30524f29046STejun Heo  * - see blk_queue_bypass_start() for details.
30624f29046STejun Heo  */
30724f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
30824f29046STejun Heo 					   struct request_queue *q)
30924f29046STejun Heo {
31024f29046STejun Heo 	WARN_ON_ONCE(!rcu_read_lock_held());
31124f29046STejun Heo 
31224f29046STejun Heo 	if (unlikely(blk_queue_bypass(q)))
31324f29046STejun Heo 		return NULL;
31424f29046STejun Heo 	return __blkg_lookup(blkcg, q, false);
31524f29046STejun Heo }
31624f29046STejun Heo 
31724f29046STejun Heo /**
318eea8f41cSTejun Heo  * blkg_to_pdata - get policy private data
319eea8f41cSTejun Heo  * @blkg: blkg of interest
320eea8f41cSTejun Heo  * @pol: policy of interest
321eea8f41cSTejun Heo  *
322eea8f41cSTejun Heo  * Return pointer to private data associated with the @blkg-@pol pair.
323eea8f41cSTejun Heo  */
324eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
325eea8f41cSTejun Heo 						  struct blkcg_policy *pol)
326eea8f41cSTejun Heo {
327eea8f41cSTejun Heo 	return blkg ? blkg->pd[pol->plid] : NULL;
328eea8f41cSTejun Heo }
329eea8f41cSTejun Heo 
330e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
331e4bc13adSLinus Torvalds 						     struct blkcg_policy *pol)
332e4bc13adSLinus Torvalds {
33381437648STejun Heo 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
334e4bc13adSLinus Torvalds }
335e4bc13adSLinus Torvalds 
336eea8f41cSTejun Heo /**
337eea8f41cSTejun Heo  * pdata_to_blkg - get blkg associated with policy private data
338eea8f41cSTejun Heo  * @pd: policy private data of interest
339eea8f41cSTejun Heo  *
340eea8f41cSTejun Heo  * @pd is policy private data.  Determine the blkg it's associated with.
341eea8f41cSTejun Heo  */
342eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
343eea8f41cSTejun Heo {
344eea8f41cSTejun Heo 	return pd ? pd->blkg : NULL;
345eea8f41cSTejun Heo }
346eea8f41cSTejun Heo 
34781437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
34881437648STejun Heo {
34981437648STejun Heo 	return cpd ? cpd->blkcg : NULL;
35081437648STejun Heo }
35181437648STejun Heo 
352eea8f41cSTejun Heo /**
353eea8f41cSTejun Heo  * blkg_path - format cgroup path of blkg
354eea8f41cSTejun Heo  * @blkg: blkg of interest
355eea8f41cSTejun Heo  * @buf: target buffer
356eea8f41cSTejun Heo  * @buflen: target buffer length
357eea8f41cSTejun Heo  *
358eea8f41cSTejun Heo  * Format the path of the cgroup of @blkg into @buf.
359eea8f41cSTejun Heo  */
360eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
361eea8f41cSTejun Heo {
3624c737b41STejun Heo 	return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
363eea8f41cSTejun Heo }
364eea8f41cSTejun Heo 
365eea8f41cSTejun Heo /**
366eea8f41cSTejun Heo  * blkg_get - get a blkg reference
367eea8f41cSTejun Heo  * @blkg: blkg to get
368eea8f41cSTejun Heo  *
369eea8f41cSTejun Heo  * The caller should be holding an existing reference.
370eea8f41cSTejun Heo  */
371eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg)
372eea8f41cSTejun Heo {
373eea8f41cSTejun Heo 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
374eea8f41cSTejun Heo 	atomic_inc(&blkg->refcnt);
375eea8f41cSTejun Heo }
376eea8f41cSTejun Heo 
377eea8f41cSTejun Heo void __blkg_release_rcu(struct rcu_head *rcu);
378eea8f41cSTejun Heo 
379eea8f41cSTejun Heo /**
380eea8f41cSTejun Heo  * blkg_put - put a blkg reference
381eea8f41cSTejun Heo  * @blkg: blkg to put
382eea8f41cSTejun Heo  */
383eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg)
384eea8f41cSTejun Heo {
385eea8f41cSTejun Heo 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
386eea8f41cSTejun Heo 	if (atomic_dec_and_test(&blkg->refcnt))
387eea8f41cSTejun Heo 		call_rcu(&blkg->rcu_head, __blkg_release_rcu);
388eea8f41cSTejun Heo }
389eea8f41cSTejun Heo 
390eea8f41cSTejun Heo /**
391eea8f41cSTejun Heo  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
392eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
393eea8f41cSTejun Heo  * @pos_css: used for iteration
394eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
395eea8f41cSTejun Heo  *
396eea8f41cSTejun Heo  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
397eea8f41cSTejun Heo  * read locked.  If called under either blkcg or queue lock, the iteration
398eea8f41cSTejun Heo  * is guaranteed to include all and only online blkgs.  The caller may
399eea8f41cSTejun Heo  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
400eea8f41cSTejun Heo  * @p_blkg is included in the iteration and the first node to be visited.
401eea8f41cSTejun Heo  */
402eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
403eea8f41cSTejun Heo 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
404eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
405eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
406eea8f41cSTejun Heo 
407eea8f41cSTejun Heo /**
408eea8f41cSTejun Heo  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
409eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
410eea8f41cSTejun Heo  * @pos_css: used for iteration
411eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
412eea8f41cSTejun Heo  *
413eea8f41cSTejun Heo  * Similar to blkg_for_each_descendant_pre() but performs post-order
414eea8f41cSTejun Heo  * traversal instead.  Synchronization rules are the same.  @p_blkg is
415eea8f41cSTejun Heo  * included in the iteration and the last node to be visited.
416eea8f41cSTejun Heo  */
417eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
418eea8f41cSTejun Heo 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
419eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
420eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
421eea8f41cSTejun Heo 
422eea8f41cSTejun Heo /**
423eea8f41cSTejun Heo  * blk_get_rl - get request_list to use
424eea8f41cSTejun Heo  * @q: request_queue of interest
425eea8f41cSTejun Heo  * @bio: bio which will be attached to the allocated request (may be %NULL)
426eea8f41cSTejun Heo  *
427eea8f41cSTejun Heo  * The caller wants to allocate a request from @q to use for @bio.  Find
428eea8f41cSTejun Heo  * the request_list to use and obtain a reference on it.  Should be called
429eea8f41cSTejun Heo  * under queue_lock.  This function is guaranteed to return non-%NULL
430eea8f41cSTejun Heo  * request_list.
431eea8f41cSTejun Heo  */
432eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q,
433eea8f41cSTejun Heo 					      struct bio *bio)
434eea8f41cSTejun Heo {
435eea8f41cSTejun Heo 	struct blkcg *blkcg;
436eea8f41cSTejun Heo 	struct blkcg_gq *blkg;
437eea8f41cSTejun Heo 
438eea8f41cSTejun Heo 	rcu_read_lock();
439eea8f41cSTejun Heo 
440eea8f41cSTejun Heo 	blkcg = bio_blkcg(bio);
441eea8f41cSTejun Heo 
442eea8f41cSTejun Heo 	/* bypass blkg lookup and use @q->root_rl directly for root */
443eea8f41cSTejun Heo 	if (blkcg == &blkcg_root)
444eea8f41cSTejun Heo 		goto root_rl;
445eea8f41cSTejun Heo 
446eea8f41cSTejun Heo 	/*
447eea8f41cSTejun Heo 	 * Try to use blkg->rl.  blkg lookup may fail under memory pressure
448eea8f41cSTejun Heo 	 * or if either the blkcg or queue is going away.  Fall back to
449eea8f41cSTejun Heo 	 * root_rl in such cases.
450eea8f41cSTejun Heo 	 */
451ae118896STejun Heo 	blkg = blkg_lookup(blkcg, q);
452ae118896STejun Heo 	if (unlikely(!blkg))
453eea8f41cSTejun Heo 		goto root_rl;
454eea8f41cSTejun Heo 
455eea8f41cSTejun Heo 	blkg_get(blkg);
456eea8f41cSTejun Heo 	rcu_read_unlock();
457eea8f41cSTejun Heo 	return &blkg->rl;
458eea8f41cSTejun Heo root_rl:
459eea8f41cSTejun Heo 	rcu_read_unlock();
460eea8f41cSTejun Heo 	return &q->root_rl;
461eea8f41cSTejun Heo }
462eea8f41cSTejun Heo 
463eea8f41cSTejun Heo /**
464eea8f41cSTejun Heo  * blk_put_rl - put request_list
465eea8f41cSTejun Heo  * @rl: request_list to put
466eea8f41cSTejun Heo  *
467eea8f41cSTejun Heo  * Put the reference acquired by blk_get_rl().  Should be called under
468eea8f41cSTejun Heo  * queue_lock.
469eea8f41cSTejun Heo  */
470eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl)
471eea8f41cSTejun Heo {
472401efbf8STejun Heo 	if (rl->blkg->blkcg != &blkcg_root)
473eea8f41cSTejun Heo 		blkg_put(rl->blkg);
474eea8f41cSTejun Heo }
475eea8f41cSTejun Heo 
476eea8f41cSTejun Heo /**
477eea8f41cSTejun Heo  * blk_rq_set_rl - associate a request with a request_list
478eea8f41cSTejun Heo  * @rq: request of interest
479eea8f41cSTejun Heo  * @rl: target request_list
480eea8f41cSTejun Heo  *
481eea8f41cSTejun Heo  * Associate @rq with @rl so that accounting and freeing can know the
482eea8f41cSTejun Heo  * request_list @rq came from.
483eea8f41cSTejun Heo  */
484eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
485eea8f41cSTejun Heo {
486eea8f41cSTejun Heo 	rq->rl = rl;
487eea8f41cSTejun Heo }
488eea8f41cSTejun Heo 
489eea8f41cSTejun Heo /**
490eea8f41cSTejun Heo  * blk_rq_rl - return the request_list a request came from
491eea8f41cSTejun Heo  * @rq: request of interest
492eea8f41cSTejun Heo  *
493eea8f41cSTejun Heo  * Return the request_list @rq is allocated from.
494eea8f41cSTejun Heo  */
495eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq)
496eea8f41cSTejun Heo {
497eea8f41cSTejun Heo 	return rq->rl;
498eea8f41cSTejun Heo }
499eea8f41cSTejun Heo 
500eea8f41cSTejun Heo struct request_list *__blk_queue_next_rl(struct request_list *rl,
501eea8f41cSTejun Heo 					 struct request_queue *q);
502eea8f41cSTejun Heo /**
503eea8f41cSTejun Heo  * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
504eea8f41cSTejun Heo  *
505eea8f41cSTejun Heo  * Should be used under queue_lock.
506eea8f41cSTejun Heo  */
507eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
508eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
509eea8f41cSTejun Heo 
51024bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp)
511eea8f41cSTejun Heo {
51224bdb8efSTejun Heo 	int ret;
51324bdb8efSTejun Heo 
51424bdb8efSTejun Heo 	ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
51524bdb8efSTejun Heo 	if (ret)
51624bdb8efSTejun Heo 		return ret;
51724bdb8efSTejun Heo 
518e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
51924bdb8efSTejun Heo 	return 0;
52024bdb8efSTejun Heo }
52124bdb8efSTejun Heo 
52224bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat)
52324bdb8efSTejun Heo {
52424bdb8efSTejun Heo 	percpu_counter_destroy(&stat->cpu_cnt);
525eea8f41cSTejun Heo }
526eea8f41cSTejun Heo 
527eea8f41cSTejun Heo /**
528eea8f41cSTejun Heo  * blkg_stat_add - add a value to a blkg_stat
529eea8f41cSTejun Heo  * @stat: target blkg_stat
530eea8f41cSTejun Heo  * @val: value to add
531eea8f41cSTejun Heo  *
53224bdb8efSTejun Heo  * Add @val to @stat.  The caller must ensure that IRQ on the same CPU
53324bdb8efSTejun Heo  * don't re-enter this function for the same counter.
534eea8f41cSTejun Heo  */
535eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
536eea8f41cSTejun Heo {
537104b4e51SNikolay Borisov 	percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
538eea8f41cSTejun Heo }
539eea8f41cSTejun Heo 
540eea8f41cSTejun Heo /**
541eea8f41cSTejun Heo  * blkg_stat_read - read the current value of a blkg_stat
542eea8f41cSTejun Heo  * @stat: blkg_stat to read
543eea8f41cSTejun Heo  */
544eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
545eea8f41cSTejun Heo {
54624bdb8efSTejun Heo 	return percpu_counter_sum_positive(&stat->cpu_cnt);
547eea8f41cSTejun Heo }
548eea8f41cSTejun Heo 
549eea8f41cSTejun Heo /**
550eea8f41cSTejun Heo  * blkg_stat_reset - reset a blkg_stat
551eea8f41cSTejun Heo  * @stat: blkg_stat to reset
552eea8f41cSTejun Heo  */
553eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat)
554eea8f41cSTejun Heo {
55524bdb8efSTejun Heo 	percpu_counter_set(&stat->cpu_cnt, 0);
556e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
557eea8f41cSTejun Heo }
558eea8f41cSTejun Heo 
559eea8f41cSTejun Heo /**
560e6269c44STejun Heo  * blkg_stat_add_aux - add a blkg_stat into another's aux count
561eea8f41cSTejun Heo  * @to: the destination blkg_stat
562eea8f41cSTejun Heo  * @from: the source
563eea8f41cSTejun Heo  *
564e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
565eea8f41cSTejun Heo  */
566e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to,
567e6269c44STejun Heo 				     struct blkg_stat *from)
568eea8f41cSTejun Heo {
569e6269c44STejun Heo 	atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt),
570e6269c44STejun Heo 		     &to->aux_cnt);
571eea8f41cSTejun Heo }
572eea8f41cSTejun Heo 
57324bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
57424bdb8efSTejun Heo {
57524bdb8efSTejun Heo 	int i, ret;
57624bdb8efSTejun Heo 
57724bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
57824bdb8efSTejun Heo 		ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
57924bdb8efSTejun Heo 		if (ret) {
58024bdb8efSTejun Heo 			while (--i >= 0)
58124bdb8efSTejun Heo 				percpu_counter_destroy(&rwstat->cpu_cnt[i]);
58224bdb8efSTejun Heo 			return ret;
58324bdb8efSTejun Heo 		}
58424bdb8efSTejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
58524bdb8efSTejun Heo 	}
58624bdb8efSTejun Heo 	return 0;
58724bdb8efSTejun Heo }
58824bdb8efSTejun Heo 
58924bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
590eea8f41cSTejun Heo {
591e6269c44STejun Heo 	int i;
592e6269c44STejun Heo 
593e6269c44STejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
59424bdb8efSTejun Heo 		percpu_counter_destroy(&rwstat->cpu_cnt[i]);
595eea8f41cSTejun Heo }
596eea8f41cSTejun Heo 
597eea8f41cSTejun Heo /**
598eea8f41cSTejun Heo  * blkg_rwstat_add - add a value to a blkg_rwstat
599eea8f41cSTejun Heo  * @rwstat: target blkg_rwstat
600ef295ecfSChristoph Hellwig  * @op: REQ_OP and flags
601eea8f41cSTejun Heo  * @val: value to add
602eea8f41cSTejun Heo  *
603eea8f41cSTejun Heo  * Add @val to @rwstat.  The counters are chosen according to @rw.  The
604eea8f41cSTejun Heo  * caller is responsible for synchronizing calls to this function.
605eea8f41cSTejun Heo  */
606eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
607ef295ecfSChristoph Hellwig 				   unsigned int op, uint64_t val)
608eea8f41cSTejun Heo {
60924bdb8efSTejun Heo 	struct percpu_counter *cnt;
610eea8f41cSTejun Heo 
61163a4cc24SMike Christie 	if (op_is_write(op))
61224bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE];
613eea8f41cSTejun Heo 	else
61424bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ];
615eea8f41cSTejun Heo 
616104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
61724bdb8efSTejun Heo 
618d71d9ae1SChristoph Hellwig 	if (op_is_sync(op))
61924bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC];
62024bdb8efSTejun Heo 	else
62124bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC];
62224bdb8efSTejun Heo 
623104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
624eea8f41cSTejun Heo }
625eea8f41cSTejun Heo 
626eea8f41cSTejun Heo /**
627eea8f41cSTejun Heo  * blkg_rwstat_read - read the current values of a blkg_rwstat
628eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
629eea8f41cSTejun Heo  *
63024bdb8efSTejun Heo  * Read the current snapshot of @rwstat and return it in the aux counts.
631eea8f41cSTejun Heo  */
632eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
633eea8f41cSTejun Heo {
63424bdb8efSTejun Heo 	struct blkg_rwstat result;
63524bdb8efSTejun Heo 	int i;
636eea8f41cSTejun Heo 
63724bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
63824bdb8efSTejun Heo 		atomic64_set(&result.aux_cnt[i],
63924bdb8efSTejun Heo 			     percpu_counter_sum_positive(&rwstat->cpu_cnt[i]));
64024bdb8efSTejun Heo 	return result;
641eea8f41cSTejun Heo }
642eea8f41cSTejun Heo 
643eea8f41cSTejun Heo /**
644eea8f41cSTejun Heo  * blkg_rwstat_total - read the total count of a blkg_rwstat
645eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
646eea8f41cSTejun Heo  *
647eea8f41cSTejun Heo  * Return the total count of @rwstat regardless of the IO direction.  This
648eea8f41cSTejun Heo  * function can be called without synchronization and takes care of u64
649eea8f41cSTejun Heo  * atomicity.
650eea8f41cSTejun Heo  */
651eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
652eea8f41cSTejun Heo {
653eea8f41cSTejun Heo 	struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
654eea8f41cSTejun Heo 
65524bdb8efSTejun Heo 	return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
65624bdb8efSTejun Heo 		atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
657eea8f41cSTejun Heo }
658eea8f41cSTejun Heo 
659eea8f41cSTejun Heo /**
660eea8f41cSTejun Heo  * blkg_rwstat_reset - reset a blkg_rwstat
661eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to reset
662eea8f41cSTejun Heo  */
663eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
664eea8f41cSTejun Heo {
665e6269c44STejun Heo 	int i;
666e6269c44STejun Heo 
66724bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
66824bdb8efSTejun Heo 		percpu_counter_set(&rwstat->cpu_cnt[i], 0);
669e6269c44STejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
670eea8f41cSTejun Heo 	}
67124bdb8efSTejun Heo }
672eea8f41cSTejun Heo 
673eea8f41cSTejun Heo /**
674e6269c44STejun Heo  * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count
675eea8f41cSTejun Heo  * @to: the destination blkg_rwstat
676eea8f41cSTejun Heo  * @from: the source
677eea8f41cSTejun Heo  *
678e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
679eea8f41cSTejun Heo  */
680e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to,
681eea8f41cSTejun Heo 				       struct blkg_rwstat *from)
682eea8f41cSTejun Heo {
683ddc21231SArnd Bergmann 	u64 sum[BLKG_RWSTAT_NR];
684eea8f41cSTejun Heo 	int i;
685eea8f41cSTejun Heo 
686eea8f41cSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
687ddc21231SArnd Bergmann 		sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]);
688ddc21231SArnd Bergmann 
689ddc21231SArnd Bergmann 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
690ddc21231SArnd Bergmann 		atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]),
691e6269c44STejun Heo 			     &to->aux_cnt[i]);
692eea8f41cSTejun Heo }
693eea8f41cSTejun Heo 
694ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING
695ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
696ae118896STejun Heo 			   struct bio *bio);
697ae118896STejun Heo #else
698ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
699ae118896STejun Heo 				  struct bio *bio) { return false; }
700ae118896STejun Heo #endif
701ae118896STejun Heo 
702ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
703ae118896STejun Heo 					 struct bio *bio)
704ae118896STejun Heo {
705ae118896STejun Heo 	struct blkcg *blkcg;
706ae118896STejun Heo 	struct blkcg_gq *blkg;
707ae118896STejun Heo 	bool throtl = false;
708ae118896STejun Heo 
709ae118896STejun Heo 	rcu_read_lock();
710ae118896STejun Heo 	blkcg = bio_blkcg(bio);
711ae118896STejun Heo 
712007cc56bSShaohua Li 	/* associate blkcg if bio hasn't attached one */
713007cc56bSShaohua Li 	bio_associate_blkcg(bio, &blkcg->css);
714007cc56bSShaohua Li 
715ae118896STejun Heo 	blkg = blkg_lookup(blkcg, q);
716ae118896STejun Heo 	if (unlikely(!blkg)) {
717ae118896STejun Heo 		spin_lock_irq(q->queue_lock);
718ae118896STejun Heo 		blkg = blkg_lookup_create(blkcg, q);
719ae118896STejun Heo 		if (IS_ERR(blkg))
720ae118896STejun Heo 			blkg = NULL;
721ae118896STejun Heo 		spin_unlock_irq(q->queue_lock);
722ae118896STejun Heo 	}
723ae118896STejun Heo 
724ae118896STejun Heo 	throtl = blk_throtl_bio(q, blkg, bio);
725ae118896STejun Heo 
72677ea7338STejun Heo 	if (!throtl) {
72777ea7338STejun Heo 		blkg = blkg ?: q->root_blkg;
728ef295ecfSChristoph Hellwig 		blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf,
72977ea7338STejun Heo 				bio->bi_iter.bi_size);
730ef295ecfSChristoph Hellwig 		blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1);
73177ea7338STejun Heo 	}
73277ea7338STejun Heo 
733ae118896STejun Heo 	rcu_read_unlock();
734ae118896STejun Heo 	return !throtl;
735ae118896STejun Heo }
736ae118896STejun Heo 
737eea8f41cSTejun Heo #else	/* CONFIG_BLK_CGROUP */
738eea8f41cSTejun Heo 
739efa7d1c7STejun Heo struct blkcg {
740efa7d1c7STejun Heo };
741eea8f41cSTejun Heo 
742eea8f41cSTejun Heo struct blkg_policy_data {
743eea8f41cSTejun Heo };
744eea8f41cSTejun Heo 
745e4bc13adSLinus Torvalds struct blkcg_policy_data {
746e4bc13adSLinus Torvalds };
747e4bc13adSLinus Torvalds 
748eea8f41cSTejun Heo struct blkcg_gq {
749eea8f41cSTejun Heo };
750eea8f41cSTejun Heo 
751eea8f41cSTejun Heo struct blkcg_policy {
752eea8f41cSTejun Heo };
753eea8f41cSTejun Heo 
754496d5e75STejun Heo #define blkcg_root_css	((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
755496d5e75STejun Heo 
756efa7d1c7STejun Heo #ifdef CONFIG_BLOCK
757efa7d1c7STejun Heo 
758eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
759eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
760eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { }
761eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { }
762eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
763eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
764eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q,
765eea8f41cSTejun Heo 					const struct blkcg_policy *pol) { return 0; }
766eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q,
767eea8f41cSTejun Heo 					   const struct blkcg_policy *pol) { }
768eea8f41cSTejun Heo 
769eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
770eea8f41cSTejun Heo 
771eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
772eea8f41cSTejun Heo 						  struct blkcg_policy *pol) { return NULL; }
773eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
774eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
775eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { }
776eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { }
777eea8f41cSTejun Heo 
778eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q,
779eea8f41cSTejun Heo 					      struct bio *bio) { return &q->root_rl; }
780eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) { }
781eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
782eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
783eea8f41cSTejun Heo 
784ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
785ae118896STejun Heo 					 struct bio *bio) { return true; }
786ae118896STejun Heo 
787eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
788eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
789eea8f41cSTejun Heo 
790efa7d1c7STejun Heo #endif	/* CONFIG_BLOCK */
791eea8f41cSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
792eea8f41cSTejun Heo #endif	/* _BLK_CGROUP_H */
793