xref: /linux-6.15/include/linux/blk-cgroup.h (revision db18a53e)
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>
18f7331648STejun Heo #include <linux/percpu.h>
1924bdb8efSTejun Heo #include <linux/percpu_counter.h>
20f7331648STejun Heo #include <linux/u64_stats_sync.h>
21eea8f41cSTejun Heo #include <linux/seq_file.h>
22eea8f41cSTejun Heo #include <linux/radix-tree.h>
23eea8f41cSTejun Heo #include <linux/blkdev.h>
24eea8f41cSTejun Heo #include <linux/atomic.h>
25902ec5b6SShaohua Li #include <linux/kthread.h>
265cdf2e3fSDennis Zhou #include <linux/fs.h>
27eea8f41cSTejun Heo 
2824bdb8efSTejun Heo /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
2924bdb8efSTejun Heo #define BLKG_STAT_CPU_BATCH	(INT_MAX / 2)
3024bdb8efSTejun Heo 
31eea8f41cSTejun Heo /* Max limits for throttle policy */
32eea8f41cSTejun Heo #define THROTL_IOPS_MAX		UINT_MAX
33eea8f41cSTejun Heo 
34eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP
35eea8f41cSTejun Heo 
36f7331648STejun Heo enum blkg_iostat_type {
37f7331648STejun Heo 	BLKG_IOSTAT_READ,
38f7331648STejun Heo 	BLKG_IOSTAT_WRITE,
39f7331648STejun Heo 	BLKG_IOSTAT_DISCARD,
40f7331648STejun Heo 
41f7331648STejun Heo 	BLKG_IOSTAT_NR,
42f7331648STejun Heo };
43f7331648STejun Heo 
44eea8f41cSTejun Heo struct blkcg_gq;
45eea8f41cSTejun Heo 
46eea8f41cSTejun Heo struct blkcg {
47eea8f41cSTejun Heo 	struct cgroup_subsys_state	css;
48eea8f41cSTejun Heo 	spinlock_t			lock;
49d866dbf6STejun Heo 	refcount_t			online_pin;
50eea8f41cSTejun Heo 
51eea8f41cSTejun Heo 	struct radix_tree_root		blkg_tree;
5255679c8dSBart Van Assche 	struct blkcg_gq	__rcu		*blkg_hint;
53eea8f41cSTejun Heo 	struct hlist_head		blkg_list;
54eea8f41cSTejun Heo 
5581437648STejun Heo 	struct blkcg_policy_data	*cpd[BLKCG_MAX_POLS];
5652ebea74STejun Heo 
577876f930STejun Heo 	struct list_head		all_blkcgs_node;
5852ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
5952ebea74STejun Heo 	struct list_head		cgwb_list;
6052ebea74STejun Heo #endif
61eea8f41cSTejun Heo };
62eea8f41cSTejun Heo 
63f7331648STejun Heo struct blkg_iostat {
64f7331648STejun Heo 	u64				bytes[BLKG_IOSTAT_NR];
65f7331648STejun Heo 	u64				ios[BLKG_IOSTAT_NR];
66f7331648STejun Heo };
67f7331648STejun Heo 
68f7331648STejun Heo struct blkg_iostat_set {
69f7331648STejun Heo 	struct u64_stats_sync		sync;
70f7331648STejun Heo 	struct blkg_iostat		cur;
71f7331648STejun Heo 	struct blkg_iostat		last;
72f7331648STejun Heo };
73f7331648STejun Heo 
74e6269c44STejun Heo /*
75eea8f41cSTejun Heo  * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
76eea8f41cSTejun Heo  * request_queue (q).  This is used by blkcg policies which need to track
77eea8f41cSTejun Heo  * information per blkcg - q pair.
78eea8f41cSTejun Heo  *
79001bea73STejun Heo  * There can be multiple active blkcg policies and each blkg:policy pair is
80001bea73STejun Heo  * represented by a blkg_policy_data which is allocated and freed by each
81001bea73STejun Heo  * policy's pd_alloc/free_fn() methods.  A policy can allocate private data
82001bea73STejun Heo  * area by allocating larger data structure which embeds blkg_policy_data
83001bea73STejun Heo  * at the beginning.
84eea8f41cSTejun Heo  */
85eea8f41cSTejun Heo struct blkg_policy_data {
86eea8f41cSTejun Heo 	/* the blkg and policy id this per-policy data belongs to */
87eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
88eea8f41cSTejun Heo 	int				plid;
89eea8f41cSTejun Heo };
90eea8f41cSTejun Heo 
91e4bc13adSLinus Torvalds /*
92e4a9bde9STejun Heo  * Policies that need to keep per-blkcg data which is independent from any
93e4a9bde9STejun Heo  * request_queue associated to it should implement cpd_alloc/free_fn()
94e4a9bde9STejun Heo  * methods.  A policy can allocate private data area by allocating larger
95e4a9bde9STejun Heo  * data structure which embeds blkcg_policy_data at the beginning.
96e4a9bde9STejun Heo  * cpd_init() is invoked to let each policy handle per-blkcg data.
97e4bc13adSLinus Torvalds  */
98e4bc13adSLinus Torvalds struct blkcg_policy_data {
9981437648STejun Heo 	/* the blkcg and policy id this per-policy data belongs to */
10081437648STejun Heo 	struct blkcg			*blkcg;
101e4bc13adSLinus Torvalds 	int				plid;
102e4bc13adSLinus Torvalds };
103e4bc13adSLinus Torvalds 
104eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */
105eea8f41cSTejun Heo struct blkcg_gq {
106eea8f41cSTejun Heo 	/* Pointer to the associated request_queue */
107eea8f41cSTejun Heo 	struct request_queue		*q;
108eea8f41cSTejun Heo 	struct list_head		q_node;
109eea8f41cSTejun Heo 	struct hlist_node		blkcg_node;
110eea8f41cSTejun Heo 	struct blkcg			*blkcg;
111eea8f41cSTejun Heo 
112ce7acfeaSTejun Heo 	/*
113ce7acfeaSTejun Heo 	 * Each blkg gets congested separately and the congestion state is
114ce7acfeaSTejun Heo 	 * propagated to the matching bdi_writeback_congested.
115ce7acfeaSTejun Heo 	 */
116ce7acfeaSTejun Heo 	struct bdi_writeback_congested	*wb_congested;
117ce7acfeaSTejun Heo 
118eea8f41cSTejun Heo 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
119eea8f41cSTejun Heo 	struct blkcg_gq			*parent;
120eea8f41cSTejun Heo 
121eea8f41cSTejun Heo 	/* reference count */
1227fcf2b03SDennis Zhou 	struct percpu_ref		refcnt;
123eea8f41cSTejun Heo 
124eea8f41cSTejun Heo 	/* is this blkg online? protected by both blkcg and q locks */
125eea8f41cSTejun Heo 	bool				online;
126eea8f41cSTejun Heo 
127f7331648STejun Heo 	struct blkg_iostat_set __percpu	*iostat_cpu;
128f7331648STejun Heo 	struct blkg_iostat_set		iostat;
12977ea7338STejun Heo 
130eea8f41cSTejun Heo 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
131eea8f41cSTejun Heo 
132d3f77dfdSTejun Heo 	spinlock_t			async_bio_lock;
133d3f77dfdSTejun Heo 	struct bio_list			async_bios;
134d3f77dfdSTejun Heo 	struct work_struct		async_bio_work;
135d09d8df3SJosef Bacik 
136d09d8df3SJosef Bacik 	atomic_t			use_delay;
137d09d8df3SJosef Bacik 	atomic64_t			delay_nsec;
138d09d8df3SJosef Bacik 	atomic64_t			delay_start;
139d09d8df3SJosef Bacik 	u64				last_delay;
140d09d8df3SJosef Bacik 	int				last_use;
141d3f77dfdSTejun Heo 
142d3f77dfdSTejun Heo 	struct rcu_head			rcu_head;
143eea8f41cSTejun Heo };
144eea8f41cSTejun Heo 
145e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
14681437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
147e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
14869d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
149cf09a8eeSTejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp,
150cf09a8eeSTejun Heo 				struct request_queue *q, struct blkcg *blkcg);
151a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
152a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
153a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
154001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
155a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
156903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
157903d23f0SJosef Bacik 				      size_t size);
158eea8f41cSTejun Heo 
159eea8f41cSTejun Heo struct blkcg_policy {
160eea8f41cSTejun Heo 	int				plid;
161eea8f41cSTejun Heo 	/* cgroup files for the policy */
1622ee867dcSTejun Heo 	struct cftype			*dfl_cftypes;
163880f50e2STejun Heo 	struct cftype			*legacy_cftypes;
164eea8f41cSTejun Heo 
165eea8f41cSTejun Heo 	/* operations */
166e4a9bde9STejun Heo 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
167e4bc13adSLinus Torvalds 	blkcg_pol_init_cpd_fn		*cpd_init_fn;
168e4a9bde9STejun Heo 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
16969d7fde5STejun Heo 	blkcg_pol_bind_cpd_fn		*cpd_bind_fn;
170e4a9bde9STejun Heo 
171001bea73STejun Heo 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
172eea8f41cSTejun Heo 	blkcg_pol_init_pd_fn		*pd_init_fn;
173eea8f41cSTejun Heo 	blkcg_pol_online_pd_fn		*pd_online_fn;
174eea8f41cSTejun Heo 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
175001bea73STejun Heo 	blkcg_pol_free_pd_fn		*pd_free_fn;
176eea8f41cSTejun Heo 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
177903d23f0SJosef Bacik 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
178eea8f41cSTejun Heo };
179eea8f41cSTejun Heo 
180eea8f41cSTejun Heo extern struct blkcg blkcg_root;
181496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css;
18207b0fdecSTejun Heo extern bool blkcg_debug_stats;
183eea8f41cSTejun Heo 
18424f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
18524f29046STejun Heo 				      struct request_queue *q, bool update_hint);
186eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q);
187eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q);
188eea8f41cSTejun Heo 
189eea8f41cSTejun Heo /* Blkio controller policy registration */
190eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol);
191eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol);
192eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q,
193eea8f41cSTejun Heo 			  const struct blkcg_policy *pol);
194eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q,
195eea8f41cSTejun Heo 			     const struct blkcg_policy *pol);
196eea8f41cSTejun Heo 
197dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg);
198eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
199eea8f41cSTejun Heo 		       u64 (*prfill)(struct seq_file *,
200eea8f41cSTejun Heo 				     struct blkg_policy_data *, int),
201eea8f41cSTejun Heo 		       const struct blkcg_policy *pol, int data,
202eea8f41cSTejun Heo 		       bool show_total);
203eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
204eea8f41cSTejun Heo 
205eea8f41cSTejun Heo struct blkg_conf_ctx {
206eea8f41cSTejun Heo 	struct gendisk			*disk;
207eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
20836aa9e5fSTejun Heo 	char				*body;
209eea8f41cSTejun Heo };
210eea8f41cSTejun Heo 
211015d254cSTejun Heo struct gendisk *blkcg_conf_get_disk(char **inputp);
212eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
21336aa9e5fSTejun Heo 		   char *input, struct blkg_conf_ctx *ctx);
214eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx);
215eea8f41cSTejun Heo 
2160fe061b9SDennis Zhou /**
2170fe061b9SDennis Zhou  * blkcg_css - find the current css
2180fe061b9SDennis Zhou  *
2190fe061b9SDennis Zhou  * Find the css associated with either the kthread or the current task.
2200fe061b9SDennis Zhou  * This may return a dying css, so it is up to the caller to use tryget logic
2210fe061b9SDennis Zhou  * to confirm it is alive and well.
2220fe061b9SDennis Zhou  */
2230fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void)
2240fe061b9SDennis Zhou {
2250fe061b9SDennis Zhou 	struct cgroup_subsys_state *css;
2260fe061b9SDennis Zhou 
2270fe061b9SDennis Zhou 	css = kthread_blkcg();
2280fe061b9SDennis Zhou 	if (css)
2290fe061b9SDennis Zhou 		return css;
2300fe061b9SDennis Zhou 	return task_css(current, io_cgrp_id);
2310fe061b9SDennis Zhou }
2320fe061b9SDennis Zhou 
233eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
234eea8f41cSTejun Heo {
235eea8f41cSTejun Heo 	return css ? container_of(css, struct blkcg, css) : NULL;
236eea8f41cSTejun Heo }
237eea8f41cSTejun Heo 
2380fe061b9SDennis Zhou /**
2390fe061b9SDennis Zhou  * __bio_blkcg - internal, inconsistent version to get blkcg
2400fe061b9SDennis Zhou  *
2410fe061b9SDennis Zhou  * DO NOT USE.
2420fe061b9SDennis Zhou  * This function is inconsistent and consequently is dangerous to use.  The
2430fe061b9SDennis Zhou  * first part of the function returns a blkcg where a reference is owned by the
2440fe061b9SDennis Zhou  * bio.  This means it does not need to be rcu protected as it cannot go away
2450fe061b9SDennis Zhou  * with the bio owning a reference to it.  However, the latter potentially gets
2460fe061b9SDennis Zhou  * it from task_css().  This can race against task migration and the cgroup
2470fe061b9SDennis Zhou  * dying.  It is also semantically different as it must be called rcu protected
2480fe061b9SDennis Zhou  * and is susceptible to failure when trying to get a reference to it.
2490fe061b9SDennis Zhou  * Therefore, it is not ok to assume that *_get() will always succeed on the
2500fe061b9SDennis Zhou  * blkcg returned here.
2510fe061b9SDennis Zhou  */
2520fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio)
25327e6fa99SDennis Zhou (Facebook) {
254db6638d7SDennis Zhou 	if (bio && bio->bi_blkg)
255db6638d7SDennis Zhou 		return bio->bi_blkg->blkcg;
2560fe061b9SDennis Zhou 	return css_to_blkcg(blkcg_css());
2570fe061b9SDennis Zhou }
2580fe061b9SDennis Zhou 
2590fe061b9SDennis Zhou /**
2600fe061b9SDennis Zhou  * bio_blkcg - grab the blkcg associated with a bio
2610fe061b9SDennis Zhou  * @bio: target bio
2620fe061b9SDennis Zhou  *
2630fe061b9SDennis Zhou  * This returns the blkcg associated with a bio, %NULL if not associated.
2640fe061b9SDennis Zhou  * Callers are expected to either handle %NULL or know association has been
2650fe061b9SDennis Zhou  * done prior to calling this.
2660fe061b9SDennis Zhou  */
2670fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio)
2680fe061b9SDennis Zhou {
269db6638d7SDennis Zhou 	if (bio && bio->bi_blkg)
270db6638d7SDennis Zhou 		return bio->bi_blkg->blkcg;
2710fe061b9SDennis Zhou 	return NULL;
272fd383c2dSTejun Heo }
273fd383c2dSTejun Heo 
274d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void)
275d09d8df3SJosef Bacik {
276d09d8df3SJosef Bacik 	struct cgroup_subsys_state *css;
277d09d8df3SJosef Bacik 	bool ret = false;
278d09d8df3SJosef Bacik 
279d09d8df3SJosef Bacik 	rcu_read_lock();
280d09d8df3SJosef Bacik 	css = kthread_blkcg();
281d09d8df3SJosef Bacik 	if (!css)
282d09d8df3SJosef Bacik 		css = task_css(current, io_cgrp_id);
283d09d8df3SJosef Bacik 	while (css) {
284d09d8df3SJosef Bacik 		if (atomic_read(&css->cgroup->congestion_count)) {
285d09d8df3SJosef Bacik 			ret = true;
286d09d8df3SJosef Bacik 			break;
287d09d8df3SJosef Bacik 		}
288d09d8df3SJosef Bacik 		css = css->parent;
289d09d8df3SJosef Bacik 	}
290d09d8df3SJosef Bacik 	rcu_read_unlock();
291d09d8df3SJosef Bacik 	return ret;
292d09d8df3SJosef Bacik }
293d09d8df3SJosef Bacik 
294eea8f41cSTejun Heo /**
295c7c98fd3SJosef Bacik  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
296c7c98fd3SJosef Bacik  * @return: true if this bio needs to be submitted with the root blkg context.
297c7c98fd3SJosef Bacik  *
298c7c98fd3SJosef Bacik  * In order to avoid priority inversions we sometimes need to issue a bio as if
299c7c98fd3SJosef Bacik  * it were attached to the root blkg, and then backcharge to the actual owning
300c7c98fd3SJosef Bacik  * blkg.  The idea is we do bio_blkcg() to look up the actual context for the
301c7c98fd3SJosef Bacik  * bio and attach the appropriate blkg to the bio.  Then we call this helper and
302c7c98fd3SJosef Bacik  * if it is true run with the root blkg for that queue and then do any
303c7c98fd3SJosef Bacik  * backcharging to the originating cgroup once the io is complete.
304c7c98fd3SJosef Bacik  */
305c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio)
306c7c98fd3SJosef Bacik {
3070d1e0c7cSJosef Bacik 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
308c7c98fd3SJosef Bacik }
309c7c98fd3SJosef Bacik 
310c7c98fd3SJosef Bacik /**
311eea8f41cSTejun Heo  * blkcg_parent - get the parent of a blkcg
312eea8f41cSTejun Heo  * @blkcg: blkcg of interest
313eea8f41cSTejun Heo  *
314eea8f41cSTejun Heo  * Return the parent blkcg of @blkcg.  Can be called anytime.
315eea8f41cSTejun Heo  */
316eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
317eea8f41cSTejun Heo {
318eea8f41cSTejun Heo 	return css_to_blkcg(blkcg->css.parent);
319eea8f41cSTejun Heo }
320eea8f41cSTejun Heo 
321eea8f41cSTejun Heo /**
32224f29046STejun Heo  * __blkg_lookup - internal version of blkg_lookup()
32324f29046STejun Heo  * @blkcg: blkcg of interest
32424f29046STejun Heo  * @q: request_queue of interest
32524f29046STejun Heo  * @update_hint: whether to update lookup hint with the result or not
32624f29046STejun Heo  *
32724f29046STejun Heo  * This is internal version and shouldn't be used by policy
32824f29046STejun Heo  * implementations.  Looks up blkgs for the @blkcg - @q pair regardless of
32924f29046STejun Heo  * @q's bypass state.  If @update_hint is %true, the caller should be
33024f29046STejun Heo  * holding @q->queue_lock and lookup hint is updated on success.
33124f29046STejun Heo  */
33224f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
33324f29046STejun Heo 					     struct request_queue *q,
33424f29046STejun Heo 					     bool update_hint)
33524f29046STejun Heo {
33624f29046STejun Heo 	struct blkcg_gq *blkg;
33724f29046STejun Heo 
33885b6bc9dSTejun Heo 	if (blkcg == &blkcg_root)
33985b6bc9dSTejun Heo 		return q->root_blkg;
34085b6bc9dSTejun Heo 
34124f29046STejun Heo 	blkg = rcu_dereference(blkcg->blkg_hint);
34224f29046STejun Heo 	if (blkg && blkg->q == q)
34324f29046STejun Heo 		return blkg;
34424f29046STejun Heo 
34524f29046STejun Heo 	return blkg_lookup_slowpath(blkcg, q, update_hint);
34624f29046STejun Heo }
34724f29046STejun Heo 
34824f29046STejun Heo /**
34924f29046STejun Heo  * blkg_lookup - lookup blkg for the specified blkcg - q pair
35024f29046STejun Heo  * @blkcg: blkcg of interest
35124f29046STejun Heo  * @q: request_queue of interest
35224f29046STejun Heo  *
35324f29046STejun Heo  * Lookup blkg for the @blkcg - @q pair.  This function should be called
354012d4a65SBart Van Assche  * under RCU read lock.
35524f29046STejun Heo  */
35624f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
35724f29046STejun Heo 					   struct request_queue *q)
35824f29046STejun Heo {
35924f29046STejun Heo 	WARN_ON_ONCE(!rcu_read_lock_held());
36024f29046STejun Heo 	return __blkg_lookup(blkcg, q, false);
36124f29046STejun Heo }
36224f29046STejun Heo 
36324f29046STejun Heo /**
364b86d865cSBart Van Assche  * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair
3656bad9b21SBart Van Assche  * @q: request_queue of interest
3666bad9b21SBart Van Assche  *
3676bad9b21SBart Van Assche  * Lookup blkg for @q at the root level. See also blkg_lookup().
3686bad9b21SBart Van Assche  */
369b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
3706bad9b21SBart Van Assche {
371b86d865cSBart Van Assche 	return q->root_blkg;
3726bad9b21SBart Van Assche }
3736bad9b21SBart Van Assche 
3746bad9b21SBart Van Assche /**
375eea8f41cSTejun Heo  * blkg_to_pdata - get policy private data
376eea8f41cSTejun Heo  * @blkg: blkg of interest
377eea8f41cSTejun Heo  * @pol: policy of interest
378eea8f41cSTejun Heo  *
379eea8f41cSTejun Heo  * Return pointer to private data associated with the @blkg-@pol pair.
380eea8f41cSTejun Heo  */
381eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
382eea8f41cSTejun Heo 						  struct blkcg_policy *pol)
383eea8f41cSTejun Heo {
384eea8f41cSTejun Heo 	return blkg ? blkg->pd[pol->plid] : NULL;
385eea8f41cSTejun Heo }
386eea8f41cSTejun Heo 
387e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
388e4bc13adSLinus Torvalds 						     struct blkcg_policy *pol)
389e4bc13adSLinus Torvalds {
39081437648STejun Heo 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
391e4bc13adSLinus Torvalds }
392e4bc13adSLinus Torvalds 
393eea8f41cSTejun Heo /**
394eea8f41cSTejun Heo  * pdata_to_blkg - get blkg associated with policy private data
395eea8f41cSTejun Heo  * @pd: policy private data of interest
396eea8f41cSTejun Heo  *
397eea8f41cSTejun Heo  * @pd is policy private data.  Determine the blkg it's associated with.
398eea8f41cSTejun Heo  */
399eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
400eea8f41cSTejun Heo {
401eea8f41cSTejun Heo 	return pd ? pd->blkg : NULL;
402eea8f41cSTejun Heo }
403eea8f41cSTejun Heo 
40481437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
40581437648STejun Heo {
40681437648STejun Heo 	return cpd ? cpd->blkcg : NULL;
40781437648STejun Heo }
40881437648STejun Heo 
40959b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg);
41059b57717SDennis Zhou (Facebook) 
41159b57717SDennis Zhou (Facebook) /**
412d866dbf6STejun Heo  * blkcg_pin_online - pin online state
41359b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
41459b57717SDennis Zhou (Facebook)  *
415d866dbf6STejun Heo  * While pinned, a blkcg is kept online.  This is primarily used to
416d866dbf6STejun Heo  * impedance-match blkg and cgwb lifetimes so that blkg doesn't go offline
417d866dbf6STejun Heo  * while an associated cgwb is still active.
41859b57717SDennis Zhou (Facebook)  */
419d866dbf6STejun Heo static inline void blkcg_pin_online(struct blkcg *blkcg)
42059b57717SDennis Zhou (Facebook) {
421d866dbf6STejun Heo 	refcount_inc(&blkcg->online_pin);
42259b57717SDennis Zhou (Facebook) }
42359b57717SDennis Zhou (Facebook) 
42459b57717SDennis Zhou (Facebook) /**
425d866dbf6STejun Heo  * blkcg_unpin_online - unpin online state
42659b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
42759b57717SDennis Zhou (Facebook)  *
428d866dbf6STejun Heo  * This is primarily used to impedance-match blkg and cgwb lifetimes so
429d866dbf6STejun Heo  * that blkg doesn't go offline while an associated cgwb is still active.
430d866dbf6STejun Heo  * When this count goes to zero, all active cgwbs have finished so the
43159b57717SDennis Zhou (Facebook)  * blkcg can continue destruction by calling blkcg_destroy_blkgs().
43259b57717SDennis Zhou (Facebook)  */
433d866dbf6STejun Heo static inline void blkcg_unpin_online(struct blkcg *blkcg)
43459b57717SDennis Zhou (Facebook) {
4354308a434STejun Heo 	do {
4364308a434STejun Heo 		if (!refcount_dec_and_test(&blkcg->online_pin))
4374308a434STejun Heo 			break;
43859b57717SDennis Zhou (Facebook) 		blkcg_destroy_blkgs(blkcg);
4394308a434STejun Heo 		blkcg = blkcg_parent(blkcg);
4404308a434STejun Heo 	} while (blkcg);
44159b57717SDennis Zhou (Facebook) }
44259b57717SDennis Zhou (Facebook) 
443eea8f41cSTejun Heo /**
444eea8f41cSTejun Heo  * blkg_path - format cgroup path of blkg
445eea8f41cSTejun Heo  * @blkg: blkg of interest
446eea8f41cSTejun Heo  * @buf: target buffer
447eea8f41cSTejun Heo  * @buflen: target buffer length
448eea8f41cSTejun Heo  *
449eea8f41cSTejun Heo  * Format the path of the cgroup of @blkg into @buf.
450eea8f41cSTejun Heo  */
451eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
452eea8f41cSTejun Heo {
4534c737b41STejun Heo 	return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
454eea8f41cSTejun Heo }
455eea8f41cSTejun Heo 
456eea8f41cSTejun Heo /**
457eea8f41cSTejun Heo  * blkg_get - get a blkg reference
458eea8f41cSTejun Heo  * @blkg: blkg to get
459eea8f41cSTejun Heo  *
460eea8f41cSTejun Heo  * The caller should be holding an existing reference.
461eea8f41cSTejun Heo  */
462eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg)
463eea8f41cSTejun Heo {
4647fcf2b03SDennis Zhou 	percpu_ref_get(&blkg->refcnt);
465eea8f41cSTejun Heo }
466eea8f41cSTejun Heo 
467d09d8df3SJosef Bacik /**
4687754f669SDennis Zhou  * blkg_tryget - try and get a blkg reference
469d09d8df3SJosef Bacik  * @blkg: blkg to get
470d09d8df3SJosef Bacik  *
471d09d8df3SJosef Bacik  * This is for use when doing an RCU lookup of the blkg.  We may be in the midst
472d09d8df3SJosef Bacik  * of freeing this blkg, so we can only use it if the refcnt is not zero.
473d09d8df3SJosef Bacik  */
4747754f669SDennis Zhou static inline bool blkg_tryget(struct blkcg_gq *blkg)
475d09d8df3SJosef Bacik {
4766ab21879SDennis Zhou 	return blkg && percpu_ref_tryget(&blkg->refcnt);
47707b05bccSDennis Zhou (Facebook) }
478d09d8df3SJosef Bacik 
479beea9da0SDennis Zhou /**
480eea8f41cSTejun Heo  * blkg_put - put a blkg reference
481eea8f41cSTejun Heo  * @blkg: blkg to put
482eea8f41cSTejun Heo  */
483eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg)
484eea8f41cSTejun Heo {
4857fcf2b03SDennis Zhou 	percpu_ref_put(&blkg->refcnt);
486eea8f41cSTejun Heo }
487eea8f41cSTejun Heo 
488eea8f41cSTejun Heo /**
489eea8f41cSTejun Heo  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
490eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
491eea8f41cSTejun Heo  * @pos_css: used for iteration
492eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
493eea8f41cSTejun Heo  *
494eea8f41cSTejun Heo  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
495eea8f41cSTejun Heo  * read locked.  If called under either blkcg or queue lock, the iteration
496eea8f41cSTejun Heo  * is guaranteed to include all and only online blkgs.  The caller may
497eea8f41cSTejun Heo  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
498eea8f41cSTejun Heo  * @p_blkg is included in the iteration and the first node to be visited.
499eea8f41cSTejun Heo  */
500eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
501eea8f41cSTejun Heo 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
502eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
503eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
504eea8f41cSTejun Heo 
505eea8f41cSTejun Heo /**
506eea8f41cSTejun Heo  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
507eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
508eea8f41cSTejun Heo  * @pos_css: used for iteration
509eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
510eea8f41cSTejun Heo  *
511eea8f41cSTejun Heo  * Similar to blkg_for_each_descendant_pre() but performs post-order
512eea8f41cSTejun Heo  * traversal instead.  Synchronization rules are the same.  @p_blkg is
513eea8f41cSTejun Heo  * included in the iteration and the last node to be visited.
514eea8f41cSTejun Heo  */
515eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
516eea8f41cSTejun Heo 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
517eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
518eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
519eea8f41cSTejun Heo 
520d3f77dfdSTejun Heo bool __blkcg_punt_bio_submit(struct bio *bio);
521d3f77dfdSTejun Heo 
522d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio)
523d3f77dfdSTejun Heo {
524d3f77dfdSTejun Heo 	if (bio->bi_opf & REQ_CGROUP_PUNT)
525d3f77dfdSTejun Heo 		return __blkcg_punt_bio_submit(bio);
526d3f77dfdSTejun Heo 	else
527d3f77dfdSTejun Heo 		return false;
528d3f77dfdSTejun Heo }
529e439bedfSDennis Zhou 
530e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio)
531e439bedfSDennis Zhou {
532e439bedfSDennis Zhou 	bio_issue_init(&bio->bi_issue, bio_sectors(bio));
533e439bedfSDennis Zhou }
534e439bedfSDennis Zhou 
535d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg)
536d09d8df3SJosef Bacik {
53754c52e10STejun Heo 	if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
53854c52e10STejun Heo 		return;
539d09d8df3SJosef Bacik 	if (atomic_add_return(1, &blkg->use_delay) == 1)
540d09d8df3SJosef Bacik 		atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
541d09d8df3SJosef Bacik }
542d09d8df3SJosef Bacik 
543d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
544d09d8df3SJosef Bacik {
545d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
546d09d8df3SJosef Bacik 
54754c52e10STejun Heo 	if (WARN_ON_ONCE(old < 0))
54854c52e10STejun Heo 		return 0;
549d09d8df3SJosef Bacik 	if (old == 0)
550d09d8df3SJosef Bacik 		return 0;
551d09d8df3SJosef Bacik 
552d09d8df3SJosef Bacik 	/*
553d09d8df3SJosef Bacik 	 * We do this song and dance because we can race with somebody else
554d09d8df3SJosef Bacik 	 * adding or removing delay.  If we just did an atomic_dec we'd end up
555d09d8df3SJosef Bacik 	 * negative and we'd already be in trouble.  We need to subtract 1 and
556d09d8df3SJosef Bacik 	 * then check to see if we were the last delay so we can drop the
557d09d8df3SJosef Bacik 	 * congestion count on the cgroup.
558d09d8df3SJosef Bacik 	 */
559d09d8df3SJosef Bacik 	while (old) {
560d09d8df3SJosef Bacik 		int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
561d09d8df3SJosef Bacik 		if (cur == old)
562d09d8df3SJosef Bacik 			break;
563d09d8df3SJosef Bacik 		old = cur;
564d09d8df3SJosef Bacik 	}
565d09d8df3SJosef Bacik 
566d09d8df3SJosef Bacik 	if (old == 0)
567d09d8df3SJosef Bacik 		return 0;
568d09d8df3SJosef Bacik 	if (old == 1)
569d09d8df3SJosef Bacik 		atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
570d09d8df3SJosef Bacik 	return 1;
571d09d8df3SJosef Bacik }
572d09d8df3SJosef Bacik 
57354c52e10STejun Heo /**
57454c52e10STejun Heo  * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
57554c52e10STejun Heo  * @blkg: target blkg
57654c52e10STejun Heo  * @delay: delay duration in nsecs
57754c52e10STejun Heo  *
57854c52e10STejun Heo  * When enabled with this function, the delay is not decayed and must be
57954c52e10STejun Heo  * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
58054c52e10STejun Heo  * blkcg_[un]use_delay() and blkcg_add_delay() usages.
58154c52e10STejun Heo  */
58254c52e10STejun Heo static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
58354c52e10STejun Heo {
58454c52e10STejun Heo 	int old = atomic_read(&blkg->use_delay);
58554c52e10STejun Heo 
58654c52e10STejun Heo 	/* We only want 1 person setting the congestion count for this blkg. */
58754c52e10STejun Heo 	if (!old && atomic_cmpxchg(&blkg->use_delay, old, -1) == old)
58854c52e10STejun Heo 		atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
58954c52e10STejun Heo 
59054c52e10STejun Heo 	atomic64_set(&blkg->delay_nsec, delay);
59154c52e10STejun Heo }
59254c52e10STejun Heo 
59354c52e10STejun Heo /**
59454c52e10STejun Heo  * blkcg_clear_delay - Disable allocator delay mechanism
59554c52e10STejun Heo  * @blkg: target blkg
59654c52e10STejun Heo  *
59754c52e10STejun Heo  * Disable use_delay mechanism. See blkcg_set_delay().
59854c52e10STejun Heo  */
599d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
600d09d8df3SJosef Bacik {
601d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
60254c52e10STejun Heo 
603d09d8df3SJosef Bacik 	/* We only want 1 person clearing the congestion count for this blkg. */
60454c52e10STejun Heo 	if (old && atomic_cmpxchg(&blkg->use_delay, old, 0) == old)
605d09d8df3SJosef Bacik 		atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
606d09d8df3SJosef Bacik }
607d09d8df3SJosef Bacik 
608*db18a53eSChristoph Hellwig void blk_cgroup_bio_start(struct bio *bio);
609d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
610d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
611d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void);
612eea8f41cSTejun Heo #else	/* CONFIG_BLK_CGROUP */
613eea8f41cSTejun Heo 
614efa7d1c7STejun Heo struct blkcg {
615efa7d1c7STejun Heo };
616eea8f41cSTejun Heo 
617eea8f41cSTejun Heo struct blkg_policy_data {
618eea8f41cSTejun Heo };
619eea8f41cSTejun Heo 
620e4bc13adSLinus Torvalds struct blkcg_policy_data {
621e4bc13adSLinus Torvalds };
622e4bc13adSLinus Torvalds 
623eea8f41cSTejun Heo struct blkcg_gq {
624eea8f41cSTejun Heo };
625eea8f41cSTejun Heo 
626eea8f41cSTejun Heo struct blkcg_policy {
627eea8f41cSTejun Heo };
628eea8f41cSTejun Heo 
629496d5e75STejun Heo #define blkcg_root_css	((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
630496d5e75STejun Heo 
631d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { }
632d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; }
633d09d8df3SJosef Bacik 
634efa7d1c7STejun Heo #ifdef CONFIG_BLOCK
635efa7d1c7STejun Heo 
636d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
637d09d8df3SJosef Bacik 
638eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
639b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
640b86d865cSBart Van Assche { return NULL; }
641eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
642eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { }
643eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
644eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
645eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q,
646eea8f41cSTejun Heo 					const struct blkcg_policy *pol) { return 0; }
647eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q,
648eea8f41cSTejun Heo 					   const struct blkcg_policy *pol) { }
649eea8f41cSTejun Heo 
6500fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
651eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
652eea8f41cSTejun Heo 
653eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
654eea8f41cSTejun Heo 						  struct blkcg_policy *pol) { return NULL; }
655eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
656eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
657eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { }
658eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { }
659eea8f41cSTejun Heo 
660d3f77dfdSTejun Heo static inline bool blkcg_punt_bio_submit(struct bio *bio) { return false; }
661e439bedfSDennis Zhou static inline void blkcg_bio_issue_init(struct bio *bio) { }
662*db18a53eSChristoph Hellwig static inline void blk_cgroup_bio_start(struct bio *bio) { }
663ae118896STejun Heo 
664eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
665eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
666eea8f41cSTejun Heo 
667efa7d1c7STejun Heo #endif	/* CONFIG_BLOCK */
668eea8f41cSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
669eea8f41cSTejun Heo #endif	/* _BLK_CGROUP_H */
670