xref: /linux-6.15/include/linux/blk-cgroup.h (revision beea9da0)
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,
38636620b6STejun Heo 	BLKG_RWSTAT_DISCARD,
39eea8f41cSTejun Heo 
40eea8f41cSTejun Heo 	BLKG_RWSTAT_NR,
41eea8f41cSTejun Heo 	BLKG_RWSTAT_TOTAL = BLKG_RWSTAT_NR,
42eea8f41cSTejun Heo };
43eea8f41cSTejun Heo 
44eea8f41cSTejun Heo struct blkcg_gq;
45eea8f41cSTejun Heo 
46eea8f41cSTejun Heo struct blkcg {
47eea8f41cSTejun Heo 	struct cgroup_subsys_state	css;
48eea8f41cSTejun Heo 	spinlock_t			lock;
49eea8f41cSTejun Heo 
50eea8f41cSTejun Heo 	struct radix_tree_root		blkg_tree;
5155679c8dSBart Van Assche 	struct blkcg_gq	__rcu		*blkg_hint;
52eea8f41cSTejun Heo 	struct hlist_head		blkg_list;
53eea8f41cSTejun Heo 
5481437648STejun Heo 	struct blkcg_policy_data	*cpd[BLKCG_MAX_POLS];
5552ebea74STejun Heo 
567876f930STejun Heo 	struct list_head		all_blkcgs_node;
5752ebea74STejun Heo #ifdef CONFIG_CGROUP_WRITEBACK
5852ebea74STejun Heo 	struct list_head		cgwb_list;
5959b57717SDennis Zhou (Facebook) 	refcount_t			cgwb_refcnt;
6052ebea74STejun Heo #endif
61eea8f41cSTejun Heo };
62eea8f41cSTejun Heo 
63e6269c44STejun Heo /*
64e6269c44STejun Heo  * blkg_[rw]stat->aux_cnt is excluded for local stats but included for
6524bdb8efSTejun Heo  * recursive.  Used to carry stats of dead children, and, for blkg_rwstat,
6624bdb8efSTejun Heo  * to carry result values from read and sum operations.
67e6269c44STejun Heo  */
68eea8f41cSTejun Heo struct blkg_stat {
6924bdb8efSTejun Heo 	struct percpu_counter		cpu_cnt;
70e6269c44STejun Heo 	atomic64_t			aux_cnt;
71eea8f41cSTejun Heo };
72eea8f41cSTejun Heo 
73eea8f41cSTejun Heo struct blkg_rwstat {
7424bdb8efSTejun Heo 	struct percpu_counter		cpu_cnt[BLKG_RWSTAT_NR];
75e6269c44STejun Heo 	atomic64_t			aux_cnt[BLKG_RWSTAT_NR];
76eea8f41cSTejun Heo };
77eea8f41cSTejun Heo 
78eea8f41cSTejun Heo /*
79eea8f41cSTejun Heo  * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
80eea8f41cSTejun Heo  * request_queue (q).  This is used by blkcg policies which need to track
81eea8f41cSTejun Heo  * information per blkcg - q pair.
82eea8f41cSTejun Heo  *
83001bea73STejun Heo  * There can be multiple active blkcg policies and each blkg:policy pair is
84001bea73STejun Heo  * represented by a blkg_policy_data which is allocated and freed by each
85001bea73STejun Heo  * policy's pd_alloc/free_fn() methods.  A policy can allocate private data
86001bea73STejun Heo  * area by allocating larger data structure which embeds blkg_policy_data
87001bea73STejun Heo  * at the beginning.
88eea8f41cSTejun Heo  */
89eea8f41cSTejun Heo struct blkg_policy_data {
90eea8f41cSTejun Heo 	/* the blkg and policy id this per-policy data belongs to */
91eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
92eea8f41cSTejun Heo 	int				plid;
93eea8f41cSTejun Heo };
94eea8f41cSTejun Heo 
95e4bc13adSLinus Torvalds /*
96e4a9bde9STejun Heo  * Policies that need to keep per-blkcg data which is independent from any
97e4a9bde9STejun Heo  * request_queue associated to it should implement cpd_alloc/free_fn()
98e4a9bde9STejun Heo  * methods.  A policy can allocate private data area by allocating larger
99e4a9bde9STejun Heo  * data structure which embeds blkcg_policy_data at the beginning.
100e4a9bde9STejun Heo  * cpd_init() is invoked to let each policy handle per-blkcg data.
101e4bc13adSLinus Torvalds  */
102e4bc13adSLinus Torvalds struct blkcg_policy_data {
10381437648STejun Heo 	/* the blkcg and policy id this per-policy data belongs to */
10481437648STejun Heo 	struct blkcg			*blkcg;
105e4bc13adSLinus Torvalds 	int				plid;
106e4bc13adSLinus Torvalds };
107e4bc13adSLinus Torvalds 
108eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */
109eea8f41cSTejun Heo struct blkcg_gq {
110eea8f41cSTejun Heo 	/* Pointer to the associated request_queue */
111eea8f41cSTejun Heo 	struct request_queue		*q;
112eea8f41cSTejun Heo 	struct list_head		q_node;
113eea8f41cSTejun Heo 	struct hlist_node		blkcg_node;
114eea8f41cSTejun Heo 	struct blkcg			*blkcg;
115eea8f41cSTejun Heo 
116ce7acfeaSTejun Heo 	/*
117ce7acfeaSTejun Heo 	 * Each blkg gets congested separately and the congestion state is
118ce7acfeaSTejun Heo 	 * propagated to the matching bdi_writeback_congested.
119ce7acfeaSTejun Heo 	 */
120ce7acfeaSTejun Heo 	struct bdi_writeback_congested	*wb_congested;
121ce7acfeaSTejun Heo 
122eea8f41cSTejun Heo 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
123eea8f41cSTejun Heo 	struct blkcg_gq			*parent;
124eea8f41cSTejun Heo 
125eea8f41cSTejun Heo 	/* reference count */
126b5f2954dSDennis Zhou 	atomic_t			refcnt;
127eea8f41cSTejun Heo 
128eea8f41cSTejun Heo 	/* is this blkg online? protected by both blkcg and q locks */
129eea8f41cSTejun Heo 	bool				online;
130eea8f41cSTejun Heo 
13177ea7338STejun Heo 	struct blkg_rwstat		stat_bytes;
13277ea7338STejun Heo 	struct blkg_rwstat		stat_ios;
13377ea7338STejun Heo 
134eea8f41cSTejun Heo 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
135eea8f41cSTejun Heo 
136eea8f41cSTejun Heo 	struct rcu_head			rcu_head;
137d09d8df3SJosef Bacik 
138d09d8df3SJosef Bacik 	atomic_t			use_delay;
139d09d8df3SJosef Bacik 	atomic64_t			delay_nsec;
140d09d8df3SJosef Bacik 	atomic64_t			delay_start;
141d09d8df3SJosef Bacik 	u64				last_delay;
142d09d8df3SJosef Bacik 	int				last_use;
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);
149001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node);
150a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
151a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
152a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
153001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
154a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
155903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
156903d23f0SJosef Bacik 				      size_t size);
157eea8f41cSTejun Heo 
158eea8f41cSTejun Heo struct blkcg_policy {
159eea8f41cSTejun Heo 	int				plid;
160eea8f41cSTejun Heo 	/* cgroup files for the policy */
1612ee867dcSTejun Heo 	struct cftype			*dfl_cftypes;
162880f50e2STejun Heo 	struct cftype			*legacy_cftypes;
163eea8f41cSTejun Heo 
164eea8f41cSTejun Heo 	/* operations */
165e4a9bde9STejun Heo 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
166e4bc13adSLinus Torvalds 	blkcg_pol_init_cpd_fn		*cpd_init_fn;
167e4a9bde9STejun Heo 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
16869d7fde5STejun Heo 	blkcg_pol_bind_cpd_fn		*cpd_bind_fn;
169e4a9bde9STejun Heo 
170001bea73STejun Heo 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
171eea8f41cSTejun Heo 	blkcg_pol_init_pd_fn		*pd_init_fn;
172eea8f41cSTejun Heo 	blkcg_pol_online_pd_fn		*pd_online_fn;
173eea8f41cSTejun Heo 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
174001bea73STejun Heo 	blkcg_pol_free_pd_fn		*pd_free_fn;
175eea8f41cSTejun Heo 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
176903d23f0SJosef Bacik 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
177eea8f41cSTejun Heo };
178eea8f41cSTejun Heo 
179eea8f41cSTejun Heo extern struct blkcg blkcg_root;
180496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css;
181eea8f41cSTejun Heo 
18224f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
18324f29046STejun Heo 				      struct request_queue *q, bool update_hint);
184b978962aSDennis Zhou struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
185b978962aSDennis Zhou 				      struct request_queue *q);
186eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
187eea8f41cSTejun Heo 				    struct request_queue *q);
188eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q);
189eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q);
190eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q);
191eea8f41cSTejun Heo 
192eea8f41cSTejun Heo /* Blkio controller policy registration */
193eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol);
194eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol);
195eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q,
196eea8f41cSTejun Heo 			  const struct blkcg_policy *pol);
197eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q,
198eea8f41cSTejun Heo 			     const struct blkcg_policy *pol);
199eea8f41cSTejun Heo 
200dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg);
201eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
202eea8f41cSTejun Heo 		       u64 (*prfill)(struct seq_file *,
203eea8f41cSTejun Heo 				     struct blkg_policy_data *, int),
204eea8f41cSTejun Heo 		       const struct blkcg_policy *pol, int data,
205eea8f41cSTejun Heo 		       bool show_total);
206eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
207eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
208eea8f41cSTejun Heo 			 const struct blkg_rwstat *rwstat);
209eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
210eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
211eea8f41cSTejun Heo 		       int off);
21277ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v);
21377ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v);
21477ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v);
21577ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v);
216eea8f41cSTejun Heo 
217f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
218f12c74caSTejun Heo 			    struct blkcg_policy *pol, int off);
219f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
220f12c74caSTejun Heo 					     struct blkcg_policy *pol, int off);
221eea8f41cSTejun Heo 
222eea8f41cSTejun Heo struct blkg_conf_ctx {
223eea8f41cSTejun Heo 	struct gendisk			*disk;
224eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
22536aa9e5fSTejun Heo 	char				*body;
226eea8f41cSTejun Heo };
227eea8f41cSTejun Heo 
228eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
22936aa9e5fSTejun Heo 		   char *input, struct blkg_conf_ctx *ctx);
230eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx);
231eea8f41cSTejun Heo 
2320fe061b9SDennis Zhou /**
2330fe061b9SDennis Zhou  * blkcg_css - find the current css
2340fe061b9SDennis Zhou  *
2350fe061b9SDennis Zhou  * Find the css associated with either the kthread or the current task.
2360fe061b9SDennis Zhou  * This may return a dying css, so it is up to the caller to use tryget logic
2370fe061b9SDennis Zhou  * to confirm it is alive and well.
2380fe061b9SDennis Zhou  */
2390fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_css(void)
2400fe061b9SDennis Zhou {
2410fe061b9SDennis Zhou 	struct cgroup_subsys_state *css;
2420fe061b9SDennis Zhou 
2430fe061b9SDennis Zhou 	css = kthread_blkcg();
2440fe061b9SDennis Zhou 	if (css)
2450fe061b9SDennis Zhou 		return css;
2460fe061b9SDennis Zhou 	return task_css(current, io_cgrp_id);
2470fe061b9SDennis Zhou }
2480fe061b9SDennis Zhou 
2490fe061b9SDennis Zhou /**
2500fe061b9SDennis Zhou  * blkcg_get_css - find and get a reference to the css
2510fe061b9SDennis Zhou  *
2520fe061b9SDennis Zhou  * Find the css associated with either the kthread or the current task.
2530fe061b9SDennis Zhou  * This takes a reference on the blkcg which will need to be managed by the
2540fe061b9SDennis Zhou  * caller.
2550fe061b9SDennis Zhou  */
2560fe061b9SDennis Zhou static inline struct cgroup_subsys_state *blkcg_get_css(void)
2570fe061b9SDennis Zhou {
2580fe061b9SDennis Zhou 	struct cgroup_subsys_state *css;
2590fe061b9SDennis Zhou 
2600fe061b9SDennis Zhou 	rcu_read_lock();
2610fe061b9SDennis Zhou 
2620fe061b9SDennis Zhou 	css = kthread_blkcg();
2630fe061b9SDennis Zhou 	if (css) {
2640fe061b9SDennis Zhou 		css_get(css);
2650fe061b9SDennis Zhou 	} else {
2660fe061b9SDennis Zhou 		/*
2670fe061b9SDennis Zhou 		 * This is a bit complicated.  It is possible task_css() is
2680fe061b9SDennis Zhou 		 * seeing an old css pointer here.  This is caused by the
2690fe061b9SDennis Zhou 		 * current thread migrating away from this cgroup and this
2700fe061b9SDennis Zhou 		 * cgroup dying.  css_tryget() will fail when trying to take a
2710fe061b9SDennis Zhou 		 * ref on a cgroup that's ref count has hit 0.
2720fe061b9SDennis Zhou 		 *
2730fe061b9SDennis Zhou 		 * Therefore, if it does fail, this means current must have
2740fe061b9SDennis Zhou 		 * been swapped away already and this is waiting for it to
2750fe061b9SDennis Zhou 		 * propagate on the polling cpu.  Hence the use of cpu_relax().
2760fe061b9SDennis Zhou 		 */
2770fe061b9SDennis Zhou 		while (true) {
2780fe061b9SDennis Zhou 			css = task_css(current, io_cgrp_id);
2790fe061b9SDennis Zhou 			if (likely(css_tryget(css)))
2800fe061b9SDennis Zhou 				break;
2810fe061b9SDennis Zhou 			cpu_relax();
2820fe061b9SDennis Zhou 		}
2830fe061b9SDennis Zhou 	}
2840fe061b9SDennis Zhou 
2850fe061b9SDennis Zhou 	rcu_read_unlock();
2860fe061b9SDennis Zhou 
2870fe061b9SDennis Zhou 	return css;
2880fe061b9SDennis Zhou }
28927e6fa99SDennis Zhou (Facebook) 
290eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
291eea8f41cSTejun Heo {
292eea8f41cSTejun Heo 	return css ? container_of(css, struct blkcg, css) : NULL;
293eea8f41cSTejun Heo }
294eea8f41cSTejun Heo 
2950fe061b9SDennis Zhou /**
2960fe061b9SDennis Zhou  * __bio_blkcg - internal, inconsistent version to get blkcg
2970fe061b9SDennis Zhou  *
2980fe061b9SDennis Zhou  * DO NOT USE.
2990fe061b9SDennis Zhou  * This function is inconsistent and consequently is dangerous to use.  The
3000fe061b9SDennis Zhou  * first part of the function returns a blkcg where a reference is owned by the
3010fe061b9SDennis Zhou  * bio.  This means it does not need to be rcu protected as it cannot go away
3020fe061b9SDennis Zhou  * with the bio owning a reference to it.  However, the latter potentially gets
3030fe061b9SDennis Zhou  * it from task_css().  This can race against task migration and the cgroup
3040fe061b9SDennis Zhou  * dying.  It is also semantically different as it must be called rcu protected
3050fe061b9SDennis Zhou  * and is susceptible to failure when trying to get a reference to it.
3060fe061b9SDennis Zhou  * Therefore, it is not ok to assume that *_get() will always succeed on the
3070fe061b9SDennis Zhou  * blkcg returned here.
3080fe061b9SDennis Zhou  */
3090fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio)
31027e6fa99SDennis Zhou (Facebook) {
311b5f2954dSDennis Zhou 	if (bio && bio->bi_css)
312b5f2954dSDennis Zhou 		return css_to_blkcg(bio->bi_css);
3130fe061b9SDennis Zhou 	return css_to_blkcg(blkcg_css());
3140fe061b9SDennis Zhou }
3150fe061b9SDennis Zhou 
3160fe061b9SDennis Zhou /**
3170fe061b9SDennis Zhou  * bio_blkcg - grab the blkcg associated with a bio
3180fe061b9SDennis Zhou  * @bio: target bio
3190fe061b9SDennis Zhou  *
3200fe061b9SDennis Zhou  * This returns the blkcg associated with a bio, %NULL if not associated.
3210fe061b9SDennis Zhou  * Callers are expected to either handle %NULL or know association has been
3220fe061b9SDennis Zhou  * done prior to calling this.
3230fe061b9SDennis Zhou  */
3240fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio)
3250fe061b9SDennis Zhou {
3260fe061b9SDennis Zhou 	if (bio && bio->bi_css)
3270fe061b9SDennis Zhou 		return css_to_blkcg(bio->bi_css);
3280fe061b9SDennis Zhou 	return NULL;
329fd383c2dSTejun Heo }
330fd383c2dSTejun Heo 
331d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void)
332d09d8df3SJosef Bacik {
333d09d8df3SJosef Bacik 	struct cgroup_subsys_state *css;
334d09d8df3SJosef Bacik 	bool ret = false;
335d09d8df3SJosef Bacik 
336d09d8df3SJosef Bacik 	rcu_read_lock();
337d09d8df3SJosef Bacik 	css = kthread_blkcg();
338d09d8df3SJosef Bacik 	if (!css)
339d09d8df3SJosef Bacik 		css = task_css(current, io_cgrp_id);
340d09d8df3SJosef Bacik 	while (css) {
341d09d8df3SJosef Bacik 		if (atomic_read(&css->cgroup->congestion_count)) {
342d09d8df3SJosef Bacik 			ret = true;
343d09d8df3SJosef Bacik 			break;
344d09d8df3SJosef Bacik 		}
345d09d8df3SJosef Bacik 		css = css->parent;
346d09d8df3SJosef Bacik 	}
347d09d8df3SJosef Bacik 	rcu_read_unlock();
348d09d8df3SJosef Bacik 	return ret;
349d09d8df3SJosef Bacik }
350d09d8df3SJosef Bacik 
351eea8f41cSTejun Heo /**
352c7c98fd3SJosef Bacik  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
353c7c98fd3SJosef Bacik  * @return: true if this bio needs to be submitted with the root blkg context.
354c7c98fd3SJosef Bacik  *
355c7c98fd3SJosef Bacik  * In order to avoid priority inversions we sometimes need to issue a bio as if
356c7c98fd3SJosef Bacik  * it were attached to the root blkg, and then backcharge to the actual owning
357c7c98fd3SJosef Bacik  * blkg.  The idea is we do bio_blkcg() to look up the actual context for the
358c7c98fd3SJosef Bacik  * bio and attach the appropriate blkg to the bio.  Then we call this helper and
359c7c98fd3SJosef Bacik  * if it is true run with the root blkg for that queue and then do any
360c7c98fd3SJosef Bacik  * backcharging to the originating cgroup once the io is complete.
361c7c98fd3SJosef Bacik  */
362c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio)
363c7c98fd3SJosef Bacik {
3640d1e0c7cSJosef Bacik 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
365c7c98fd3SJosef Bacik }
366c7c98fd3SJosef Bacik 
367c7c98fd3SJosef Bacik /**
368eea8f41cSTejun Heo  * blkcg_parent - get the parent of a blkcg
369eea8f41cSTejun Heo  * @blkcg: blkcg of interest
370eea8f41cSTejun Heo  *
371eea8f41cSTejun Heo  * Return the parent blkcg of @blkcg.  Can be called anytime.
372eea8f41cSTejun Heo  */
373eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
374eea8f41cSTejun Heo {
375eea8f41cSTejun Heo 	return css_to_blkcg(blkcg->css.parent);
376eea8f41cSTejun Heo }
377eea8f41cSTejun Heo 
378eea8f41cSTejun Heo /**
37924f29046STejun Heo  * __blkg_lookup - internal version of blkg_lookup()
38024f29046STejun Heo  * @blkcg: blkcg of interest
38124f29046STejun Heo  * @q: request_queue of interest
38224f29046STejun Heo  * @update_hint: whether to update lookup hint with the result or not
38324f29046STejun Heo  *
38424f29046STejun Heo  * This is internal version and shouldn't be used by policy
38524f29046STejun Heo  * implementations.  Looks up blkgs for the @blkcg - @q pair regardless of
38624f29046STejun Heo  * @q's bypass state.  If @update_hint is %true, the caller should be
38724f29046STejun Heo  * holding @q->queue_lock and lookup hint is updated on success.
38824f29046STejun Heo  */
38924f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
39024f29046STejun Heo 					     struct request_queue *q,
39124f29046STejun Heo 					     bool update_hint)
39224f29046STejun Heo {
39324f29046STejun Heo 	struct blkcg_gq *blkg;
39424f29046STejun Heo 
39585b6bc9dSTejun Heo 	if (blkcg == &blkcg_root)
39685b6bc9dSTejun Heo 		return q->root_blkg;
39785b6bc9dSTejun Heo 
39824f29046STejun Heo 	blkg = rcu_dereference(blkcg->blkg_hint);
39924f29046STejun Heo 	if (blkg && blkg->q == q)
40024f29046STejun Heo 		return blkg;
40124f29046STejun Heo 
40224f29046STejun Heo 	return blkg_lookup_slowpath(blkcg, q, update_hint);
40324f29046STejun Heo }
40424f29046STejun Heo 
40524f29046STejun Heo /**
40624f29046STejun Heo  * blkg_lookup - lookup blkg for the specified blkcg - q pair
40724f29046STejun Heo  * @blkcg: blkcg of interest
40824f29046STejun Heo  * @q: request_queue of interest
40924f29046STejun Heo  *
41024f29046STejun Heo  * Lookup blkg for the @blkcg - @q pair.  This function should be called
4118f4236d9SChristoph Hellwig  * under RCU read loc.
41224f29046STejun Heo  */
41324f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
41424f29046STejun Heo 					   struct request_queue *q)
41524f29046STejun Heo {
41624f29046STejun Heo 	WARN_ON_ONCE(!rcu_read_lock_held());
41724f29046STejun Heo 	return __blkg_lookup(blkcg, q, false);
41824f29046STejun Heo }
41924f29046STejun Heo 
42024f29046STejun Heo /**
421b86d865cSBart Van Assche  * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair
4226bad9b21SBart Van Assche  * @q: request_queue of interest
4236bad9b21SBart Van Assche  *
4246bad9b21SBart Van Assche  * Lookup blkg for @q at the root level. See also blkg_lookup().
4256bad9b21SBart Van Assche  */
426b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
4276bad9b21SBart Van Assche {
428b86d865cSBart Van Assche 	return q->root_blkg;
4296bad9b21SBart Van Assche }
4306bad9b21SBart Van Assche 
4316bad9b21SBart Van Assche /**
432eea8f41cSTejun Heo  * blkg_to_pdata - get policy private data
433eea8f41cSTejun Heo  * @blkg: blkg of interest
434eea8f41cSTejun Heo  * @pol: policy of interest
435eea8f41cSTejun Heo  *
436eea8f41cSTejun Heo  * Return pointer to private data associated with the @blkg-@pol pair.
437eea8f41cSTejun Heo  */
438eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
439eea8f41cSTejun Heo 						  struct blkcg_policy *pol)
440eea8f41cSTejun Heo {
441eea8f41cSTejun Heo 	return blkg ? blkg->pd[pol->plid] : NULL;
442eea8f41cSTejun Heo }
443eea8f41cSTejun Heo 
444e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
445e4bc13adSLinus Torvalds 						     struct blkcg_policy *pol)
446e4bc13adSLinus Torvalds {
44781437648STejun Heo 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
448e4bc13adSLinus Torvalds }
449e4bc13adSLinus Torvalds 
450eea8f41cSTejun Heo /**
451eea8f41cSTejun Heo  * pdata_to_blkg - get blkg associated with policy private data
452eea8f41cSTejun Heo  * @pd: policy private data of interest
453eea8f41cSTejun Heo  *
454eea8f41cSTejun Heo  * @pd is policy private data.  Determine the blkg it's associated with.
455eea8f41cSTejun Heo  */
456eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
457eea8f41cSTejun Heo {
458eea8f41cSTejun Heo 	return pd ? pd->blkg : NULL;
459eea8f41cSTejun Heo }
460eea8f41cSTejun Heo 
46181437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
46281437648STejun Heo {
46381437648STejun Heo 	return cpd ? cpd->blkcg : NULL;
46481437648STejun Heo }
46581437648STejun Heo 
46659b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg);
46759b57717SDennis Zhou (Facebook) 
46859b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK
46959b57717SDennis Zhou (Facebook) 
47059b57717SDennis Zhou (Facebook) /**
47159b57717SDennis Zhou (Facebook)  * blkcg_cgwb_get - get a reference for blkcg->cgwb_list
47259b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
47359b57717SDennis Zhou (Facebook)  *
47459b57717SDennis Zhou (Facebook)  * This is used to track the number of active wb's related to a blkcg.
47559b57717SDennis Zhou (Facebook)  */
47659b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg)
47759b57717SDennis Zhou (Facebook) {
47859b57717SDennis Zhou (Facebook) 	refcount_inc(&blkcg->cgwb_refcnt);
47959b57717SDennis Zhou (Facebook) }
48059b57717SDennis Zhou (Facebook) 
48159b57717SDennis Zhou (Facebook) /**
48259b57717SDennis Zhou (Facebook)  * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list
48359b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
48459b57717SDennis Zhou (Facebook)  *
48559b57717SDennis Zhou (Facebook)  * This is used to track the number of active wb's related to a blkcg.
48659b57717SDennis Zhou (Facebook)  * When this count goes to zero, all active wb has finished so the
48759b57717SDennis Zhou (Facebook)  * blkcg can continue destruction by calling blkcg_destroy_blkgs().
48859b57717SDennis Zhou (Facebook)  * This work may occur in cgwb_release_workfn() on the cgwb_release
48959b57717SDennis Zhou (Facebook)  * workqueue.
49059b57717SDennis Zhou (Facebook)  */
49159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg)
49259b57717SDennis Zhou (Facebook) {
49359b57717SDennis Zhou (Facebook) 	if (refcount_dec_and_test(&blkcg->cgwb_refcnt))
49459b57717SDennis Zhou (Facebook) 		blkcg_destroy_blkgs(blkcg);
49559b57717SDennis Zhou (Facebook) }
49659b57717SDennis Zhou (Facebook) 
49759b57717SDennis Zhou (Facebook) #else
49859b57717SDennis Zhou (Facebook) 
49959b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { }
50059b57717SDennis Zhou (Facebook) 
50159b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg)
50259b57717SDennis Zhou (Facebook) {
50359b57717SDennis Zhou (Facebook) 	/* wb isn't being accounted, so trigger destruction right away */
50459b57717SDennis Zhou (Facebook) 	blkcg_destroy_blkgs(blkcg);
50559b57717SDennis Zhou (Facebook) }
50659b57717SDennis Zhou (Facebook) 
50759b57717SDennis Zhou (Facebook) #endif
50859b57717SDennis Zhou (Facebook) 
509eea8f41cSTejun Heo /**
510eea8f41cSTejun Heo  * blkg_path - format cgroup path of blkg
511eea8f41cSTejun Heo  * @blkg: blkg of interest
512eea8f41cSTejun Heo  * @buf: target buffer
513eea8f41cSTejun Heo  * @buflen: target buffer length
514eea8f41cSTejun Heo  *
515eea8f41cSTejun Heo  * Format the path of the cgroup of @blkg into @buf.
516eea8f41cSTejun Heo  */
517eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
518eea8f41cSTejun Heo {
5194c737b41STejun Heo 	return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
520eea8f41cSTejun Heo }
521eea8f41cSTejun Heo 
522eea8f41cSTejun Heo /**
523eea8f41cSTejun Heo  * blkg_get - get a blkg reference
524eea8f41cSTejun Heo  * @blkg: blkg to get
525eea8f41cSTejun Heo  *
526eea8f41cSTejun Heo  * The caller should be holding an existing reference.
527eea8f41cSTejun Heo  */
528eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg)
529eea8f41cSTejun Heo {
530b5f2954dSDennis Zhou 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
531b5f2954dSDennis Zhou 	atomic_inc(&blkg->refcnt);
532eea8f41cSTejun Heo }
533eea8f41cSTejun Heo 
534d09d8df3SJosef Bacik /**
535b5f2954dSDennis Zhou  * blkg_try_get - try and get a blkg reference
536d09d8df3SJosef Bacik  * @blkg: blkg to get
537d09d8df3SJosef Bacik  *
538d09d8df3SJosef Bacik  * This is for use when doing an RCU lookup of the blkg.  We may be in the midst
539d09d8df3SJosef Bacik  * of freeing this blkg, so we can only use it if the refcnt is not zero.
540d09d8df3SJosef Bacik  */
541b5f2954dSDennis Zhou static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg)
542d09d8df3SJosef Bacik {
543b5f2954dSDennis Zhou 	if (atomic_inc_not_zero(&blkg->refcnt))
54407b05bccSDennis Zhou (Facebook) 		return blkg;
545b5f2954dSDennis Zhou 	return NULL;
54607b05bccSDennis Zhou (Facebook) }
547d09d8df3SJosef Bacik 
548*beea9da0SDennis Zhou /**
549*beea9da0SDennis Zhou  * blkg_try_get_closest - try and get a blkg ref on the closet blkg
550*beea9da0SDennis Zhou  * @blkg: blkg to get
551*beea9da0SDennis Zhou  *
552*beea9da0SDennis Zhou  * This walks up the blkg tree to find the closest non-dying blkg and returns
553*beea9da0SDennis Zhou  * the blkg that it did association with as it may not be the passed in blkg.
554*beea9da0SDennis Zhou  */
555*beea9da0SDennis Zhou static inline struct blkcg_gq *blkg_try_get_closest(struct blkcg_gq *blkg)
556*beea9da0SDennis Zhou {
557*beea9da0SDennis Zhou 	while (!atomic_inc_not_zero(&blkg->refcnt))
558*beea9da0SDennis Zhou 		blkg = blkg->parent;
559*beea9da0SDennis Zhou 
560*beea9da0SDennis Zhou 	return blkg;
561*beea9da0SDennis Zhou }
562b5f2954dSDennis Zhou 
563b5f2954dSDennis Zhou void __blkg_release_rcu(struct rcu_head *rcu);
564b5f2954dSDennis Zhou 
565eea8f41cSTejun Heo /**
566eea8f41cSTejun Heo  * blkg_put - put a blkg reference
567eea8f41cSTejun Heo  * @blkg: blkg to put
568eea8f41cSTejun Heo  */
569eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg)
570eea8f41cSTejun Heo {
571b5f2954dSDennis Zhou 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
572b5f2954dSDennis Zhou 	if (atomic_dec_and_test(&blkg->refcnt))
573b5f2954dSDennis Zhou 		call_rcu(&blkg->rcu_head, __blkg_release_rcu);
574eea8f41cSTejun Heo }
575eea8f41cSTejun Heo 
576eea8f41cSTejun Heo /**
577eea8f41cSTejun Heo  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
578eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
579eea8f41cSTejun Heo  * @pos_css: used for iteration
580eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
581eea8f41cSTejun Heo  *
582eea8f41cSTejun Heo  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
583eea8f41cSTejun Heo  * read locked.  If called under either blkcg or queue lock, the iteration
584eea8f41cSTejun Heo  * is guaranteed to include all and only online blkgs.  The caller may
585eea8f41cSTejun Heo  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
586eea8f41cSTejun Heo  * @p_blkg is included in the iteration and the first node to be visited.
587eea8f41cSTejun Heo  */
588eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
589eea8f41cSTejun Heo 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
590eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
591eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
592eea8f41cSTejun Heo 
593eea8f41cSTejun Heo /**
594eea8f41cSTejun Heo  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
595eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
596eea8f41cSTejun Heo  * @pos_css: used for iteration
597eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
598eea8f41cSTejun Heo  *
599eea8f41cSTejun Heo  * Similar to blkg_for_each_descendant_pre() but performs post-order
600eea8f41cSTejun Heo  * traversal instead.  Synchronization rules are the same.  @p_blkg is
601eea8f41cSTejun Heo  * included in the iteration and the last node to be visited.
602eea8f41cSTejun Heo  */
603eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
604eea8f41cSTejun Heo 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
605eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
606eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
607eea8f41cSTejun Heo 
60824bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp)
609eea8f41cSTejun Heo {
61024bdb8efSTejun Heo 	int ret;
61124bdb8efSTejun Heo 
61224bdb8efSTejun Heo 	ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
61324bdb8efSTejun Heo 	if (ret)
61424bdb8efSTejun Heo 		return ret;
61524bdb8efSTejun Heo 
616e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
61724bdb8efSTejun Heo 	return 0;
61824bdb8efSTejun Heo }
61924bdb8efSTejun Heo 
62024bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat)
62124bdb8efSTejun Heo {
62224bdb8efSTejun Heo 	percpu_counter_destroy(&stat->cpu_cnt);
623eea8f41cSTejun Heo }
624eea8f41cSTejun Heo 
625eea8f41cSTejun Heo /**
626eea8f41cSTejun Heo  * blkg_stat_add - add a value to a blkg_stat
627eea8f41cSTejun Heo  * @stat: target blkg_stat
628eea8f41cSTejun Heo  * @val: value to add
629eea8f41cSTejun Heo  *
63024bdb8efSTejun Heo  * Add @val to @stat.  The caller must ensure that IRQ on the same CPU
63124bdb8efSTejun Heo  * don't re-enter this function for the same counter.
632eea8f41cSTejun Heo  */
633eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
634eea8f41cSTejun Heo {
635104b4e51SNikolay Borisov 	percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
636eea8f41cSTejun Heo }
637eea8f41cSTejun Heo 
638eea8f41cSTejun Heo /**
639eea8f41cSTejun Heo  * blkg_stat_read - read the current value of a blkg_stat
640eea8f41cSTejun Heo  * @stat: blkg_stat to read
641eea8f41cSTejun Heo  */
642eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
643eea8f41cSTejun Heo {
64424bdb8efSTejun Heo 	return percpu_counter_sum_positive(&stat->cpu_cnt);
645eea8f41cSTejun Heo }
646eea8f41cSTejun Heo 
647eea8f41cSTejun Heo /**
648eea8f41cSTejun Heo  * blkg_stat_reset - reset a blkg_stat
649eea8f41cSTejun Heo  * @stat: blkg_stat to reset
650eea8f41cSTejun Heo  */
651eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat)
652eea8f41cSTejun Heo {
65324bdb8efSTejun Heo 	percpu_counter_set(&stat->cpu_cnt, 0);
654e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
655eea8f41cSTejun Heo }
656eea8f41cSTejun Heo 
657eea8f41cSTejun Heo /**
658e6269c44STejun Heo  * blkg_stat_add_aux - add a blkg_stat into another's aux count
659eea8f41cSTejun Heo  * @to: the destination blkg_stat
660eea8f41cSTejun Heo  * @from: the source
661eea8f41cSTejun Heo  *
662e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
663eea8f41cSTejun Heo  */
664e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to,
665e6269c44STejun Heo 				     struct blkg_stat *from)
666eea8f41cSTejun Heo {
667e6269c44STejun Heo 	atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt),
668e6269c44STejun Heo 		     &to->aux_cnt);
669eea8f41cSTejun Heo }
670eea8f41cSTejun Heo 
67124bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
67224bdb8efSTejun Heo {
67324bdb8efSTejun Heo 	int i, ret;
67424bdb8efSTejun Heo 
67524bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
67624bdb8efSTejun Heo 		ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
67724bdb8efSTejun Heo 		if (ret) {
67824bdb8efSTejun Heo 			while (--i >= 0)
67924bdb8efSTejun Heo 				percpu_counter_destroy(&rwstat->cpu_cnt[i]);
68024bdb8efSTejun Heo 			return ret;
68124bdb8efSTejun Heo 		}
68224bdb8efSTejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
68324bdb8efSTejun Heo 	}
68424bdb8efSTejun Heo 	return 0;
68524bdb8efSTejun Heo }
68624bdb8efSTejun Heo 
68724bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
688eea8f41cSTejun Heo {
689e6269c44STejun Heo 	int i;
690e6269c44STejun Heo 
691e6269c44STejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
69224bdb8efSTejun Heo 		percpu_counter_destroy(&rwstat->cpu_cnt[i]);
693eea8f41cSTejun Heo }
694eea8f41cSTejun Heo 
695eea8f41cSTejun Heo /**
696eea8f41cSTejun Heo  * blkg_rwstat_add - add a value to a blkg_rwstat
697eea8f41cSTejun Heo  * @rwstat: target blkg_rwstat
698ef295ecfSChristoph Hellwig  * @op: REQ_OP and flags
699eea8f41cSTejun Heo  * @val: value to add
700eea8f41cSTejun Heo  *
701eea8f41cSTejun Heo  * Add @val to @rwstat.  The counters are chosen according to @rw.  The
702eea8f41cSTejun Heo  * caller is responsible for synchronizing calls to this function.
703eea8f41cSTejun Heo  */
704eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
705ef295ecfSChristoph Hellwig 				   unsigned int op, uint64_t val)
706eea8f41cSTejun Heo {
70724bdb8efSTejun Heo 	struct percpu_counter *cnt;
708eea8f41cSTejun Heo 
709636620b6STejun Heo 	if (op_is_discard(op))
710636620b6STejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD];
711636620b6STejun Heo 	else if (op_is_write(op))
71224bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE];
713eea8f41cSTejun Heo 	else
71424bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ];
715eea8f41cSTejun Heo 
716104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
71724bdb8efSTejun Heo 
718d71d9ae1SChristoph Hellwig 	if (op_is_sync(op))
71924bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC];
72024bdb8efSTejun Heo 	else
72124bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC];
72224bdb8efSTejun Heo 
723104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
724eea8f41cSTejun Heo }
725eea8f41cSTejun Heo 
726eea8f41cSTejun Heo /**
727eea8f41cSTejun Heo  * blkg_rwstat_read - read the current values of a blkg_rwstat
728eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
729eea8f41cSTejun Heo  *
73024bdb8efSTejun Heo  * Read the current snapshot of @rwstat and return it in the aux counts.
731eea8f41cSTejun Heo  */
732eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
733eea8f41cSTejun Heo {
73424bdb8efSTejun Heo 	struct blkg_rwstat result;
73524bdb8efSTejun Heo 	int i;
736eea8f41cSTejun Heo 
73724bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
73824bdb8efSTejun Heo 		atomic64_set(&result.aux_cnt[i],
73924bdb8efSTejun Heo 			     percpu_counter_sum_positive(&rwstat->cpu_cnt[i]));
74024bdb8efSTejun Heo 	return result;
741eea8f41cSTejun Heo }
742eea8f41cSTejun Heo 
743eea8f41cSTejun Heo /**
744eea8f41cSTejun Heo  * blkg_rwstat_total - read the total count of a blkg_rwstat
745eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
746eea8f41cSTejun Heo  *
747eea8f41cSTejun Heo  * Return the total count of @rwstat regardless of the IO direction.  This
748eea8f41cSTejun Heo  * function can be called without synchronization and takes care of u64
749eea8f41cSTejun Heo  * atomicity.
750eea8f41cSTejun Heo  */
751eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
752eea8f41cSTejun Heo {
753eea8f41cSTejun Heo 	struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
754eea8f41cSTejun Heo 
75524bdb8efSTejun Heo 	return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
75624bdb8efSTejun Heo 		atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
757eea8f41cSTejun Heo }
758eea8f41cSTejun Heo 
759eea8f41cSTejun Heo /**
760eea8f41cSTejun Heo  * blkg_rwstat_reset - reset a blkg_rwstat
761eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to reset
762eea8f41cSTejun Heo  */
763eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
764eea8f41cSTejun Heo {
765e6269c44STejun Heo 	int i;
766e6269c44STejun Heo 
76724bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
76824bdb8efSTejun Heo 		percpu_counter_set(&rwstat->cpu_cnt[i], 0);
769e6269c44STejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
770eea8f41cSTejun Heo 	}
77124bdb8efSTejun Heo }
772eea8f41cSTejun Heo 
773eea8f41cSTejun Heo /**
774e6269c44STejun Heo  * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count
775eea8f41cSTejun Heo  * @to: the destination blkg_rwstat
776eea8f41cSTejun Heo  * @from: the source
777eea8f41cSTejun Heo  *
778e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
779eea8f41cSTejun Heo  */
780e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to,
781eea8f41cSTejun Heo 				       struct blkg_rwstat *from)
782eea8f41cSTejun Heo {
783ddc21231SArnd Bergmann 	u64 sum[BLKG_RWSTAT_NR];
784eea8f41cSTejun Heo 	int i;
785eea8f41cSTejun Heo 
786eea8f41cSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
787ddc21231SArnd Bergmann 		sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]);
788ddc21231SArnd Bergmann 
789ddc21231SArnd Bergmann 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
790ddc21231SArnd Bergmann 		atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]),
791e6269c44STejun Heo 			     &to->aux_cnt[i]);
792eea8f41cSTejun Heo }
793eea8f41cSTejun Heo 
794ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING
795ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
796ae118896STejun Heo 			   struct bio *bio);
797ae118896STejun Heo #else
798ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
799ae118896STejun Heo 				  struct bio *bio) { return false; }
800ae118896STejun Heo #endif
801ae118896STejun Heo 
802ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
803ae118896STejun Heo 					 struct bio *bio)
804ae118896STejun Heo {
805b5f2954dSDennis Zhou 	struct blkcg *blkcg;
806ae118896STejun Heo 	struct blkcg_gq *blkg;
807ae118896STejun Heo 	bool throtl = false;
808ae118896STejun Heo 
809ae118896STejun Heo 	rcu_read_lock();
810ae118896STejun Heo 
811b5f2954dSDennis Zhou 	/* associate blkcg if bio hasn't attached one */
8120fe061b9SDennis Zhou 	bio_associate_blkcg(bio, NULL);
8130fe061b9SDennis Zhou 	blkcg = bio_blkcg(bio);
814*beea9da0SDennis Zhou 	blkg = blkg_lookup_create(blkcg, q);
815ae118896STejun Heo 
816ae118896STejun Heo 	throtl = blk_throtl_bio(q, blkg, bio);
817ae118896STejun Heo 
81877ea7338STejun Heo 	if (!throtl) {
819b5f2954dSDennis Zhou 		blkg = blkg ?: q->root_blkg;
820c454edc2SJosef Bacik 		/*
821c454edc2SJosef Bacik 		 * If the bio is flagged with BIO_QUEUE_ENTERED it means this
822c454edc2SJosef Bacik 		 * is a split bio and we would have already accounted for the
823c454edc2SJosef Bacik 		 * size of the bio.
824c454edc2SJosef Bacik 		 */
825c454edc2SJosef Bacik 		if (!bio_flagged(bio, BIO_QUEUE_ENTERED))
826ef295ecfSChristoph Hellwig 			blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf,
82777ea7338STejun Heo 					bio->bi_iter.bi_size);
828ef295ecfSChristoph Hellwig 		blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1);
82977ea7338STejun Heo 	}
83077ea7338STejun Heo 
831ae118896STejun Heo 	rcu_read_unlock();
832ae118896STejun Heo 	return !throtl;
833ae118896STejun Heo }
834ae118896STejun Heo 
835d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg)
836d09d8df3SJosef Bacik {
837d09d8df3SJosef Bacik 	if (atomic_add_return(1, &blkg->use_delay) == 1)
838d09d8df3SJosef Bacik 		atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
839d09d8df3SJosef Bacik }
840d09d8df3SJosef Bacik 
841d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
842d09d8df3SJosef Bacik {
843d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
844d09d8df3SJosef Bacik 
845d09d8df3SJosef Bacik 	if (old == 0)
846d09d8df3SJosef Bacik 		return 0;
847d09d8df3SJosef Bacik 
848d09d8df3SJosef Bacik 	/*
849d09d8df3SJosef Bacik 	 * We do this song and dance because we can race with somebody else
850d09d8df3SJosef Bacik 	 * adding or removing delay.  If we just did an atomic_dec we'd end up
851d09d8df3SJosef Bacik 	 * negative and we'd already be in trouble.  We need to subtract 1 and
852d09d8df3SJosef Bacik 	 * then check to see if we were the last delay so we can drop the
853d09d8df3SJosef Bacik 	 * congestion count on the cgroup.
854d09d8df3SJosef Bacik 	 */
855d09d8df3SJosef Bacik 	while (old) {
856d09d8df3SJosef Bacik 		int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
857d09d8df3SJosef Bacik 		if (cur == old)
858d09d8df3SJosef Bacik 			break;
859d09d8df3SJosef Bacik 		old = cur;
860d09d8df3SJosef Bacik 	}
861d09d8df3SJosef Bacik 
862d09d8df3SJosef Bacik 	if (old == 0)
863d09d8df3SJosef Bacik 		return 0;
864d09d8df3SJosef Bacik 	if (old == 1)
865d09d8df3SJosef Bacik 		atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
866d09d8df3SJosef Bacik 	return 1;
867d09d8df3SJosef Bacik }
868d09d8df3SJosef Bacik 
869d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
870d09d8df3SJosef Bacik {
871d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
872d09d8df3SJosef Bacik 	if (!old)
873d09d8df3SJosef Bacik 		return;
874d09d8df3SJosef Bacik 	/* We only want 1 person clearing the congestion count for this blkg. */
875d09d8df3SJosef Bacik 	while (old) {
876d09d8df3SJosef Bacik 		int cur = atomic_cmpxchg(&blkg->use_delay, old, 0);
877d09d8df3SJosef Bacik 		if (cur == old) {
878d09d8df3SJosef Bacik 			atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
879d09d8df3SJosef Bacik 			break;
880d09d8df3SJosef Bacik 		}
881d09d8df3SJosef Bacik 		old = cur;
882d09d8df3SJosef Bacik 	}
883d09d8df3SJosef Bacik }
884d09d8df3SJosef Bacik 
885d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
886d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
887d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void);
888eea8f41cSTejun Heo #else	/* CONFIG_BLK_CGROUP */
889eea8f41cSTejun Heo 
890efa7d1c7STejun Heo struct blkcg {
891efa7d1c7STejun Heo };
892eea8f41cSTejun Heo 
893eea8f41cSTejun Heo struct blkg_policy_data {
894eea8f41cSTejun Heo };
895eea8f41cSTejun Heo 
896e4bc13adSLinus Torvalds struct blkcg_policy_data {
897e4bc13adSLinus Torvalds };
898e4bc13adSLinus Torvalds 
899eea8f41cSTejun Heo struct blkcg_gq {
900eea8f41cSTejun Heo };
901eea8f41cSTejun Heo 
902eea8f41cSTejun Heo struct blkcg_policy {
903eea8f41cSTejun Heo };
904eea8f41cSTejun Heo 
905496d5e75STejun Heo #define blkcg_root_css	((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
906496d5e75STejun Heo 
907d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { }
908d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; }
909d09d8df3SJosef Bacik 
910efa7d1c7STejun Heo #ifdef CONFIG_BLOCK
911efa7d1c7STejun Heo 
912d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
913d09d8df3SJosef Bacik 
914eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
915b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
916b86d865cSBart Van Assche { return NULL; }
917eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
918eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { }
919eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { }
920eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
921eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
922eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q,
923eea8f41cSTejun Heo 					const struct blkcg_policy *pol) { return 0; }
924eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q,
925eea8f41cSTejun Heo 					   const struct blkcg_policy *pol) { }
926eea8f41cSTejun Heo 
9270fe061b9SDennis Zhou static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
928eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
929eea8f41cSTejun Heo 
930eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
931eea8f41cSTejun Heo 						  struct blkcg_policy *pol) { return NULL; }
932eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
933eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
934eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { }
935eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { }
936eea8f41cSTejun Heo 
937ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
938ae118896STejun Heo 					 struct bio *bio) { return true; }
939ae118896STejun Heo 
940eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
941eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
942eea8f41cSTejun Heo 
943efa7d1c7STejun Heo #endif	/* CONFIG_BLOCK */
944eea8f41cSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
945eea8f41cSTejun Heo #endif	/* _BLK_CGROUP_H */
946