xref: /linux-6.15/include/linux/blk-cgroup.h (revision f0fcb3ec)
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 	/* request allocation list for this blkcg-q pair */
126eea8f41cSTejun Heo 	struct request_list		rl;
127eea8f41cSTejun Heo 
128eea8f41cSTejun Heo 	/* reference count */
129eea8f41cSTejun Heo 	atomic_t			refcnt;
130eea8f41cSTejun Heo 
131eea8f41cSTejun Heo 	/* is this blkg online? protected by both blkcg and q locks */
132eea8f41cSTejun Heo 	bool				online;
133eea8f41cSTejun Heo 
13477ea7338STejun Heo 	struct blkg_rwstat		stat_bytes;
13577ea7338STejun Heo 	struct blkg_rwstat		stat_ios;
13677ea7338STejun Heo 
137eea8f41cSTejun Heo 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
138eea8f41cSTejun Heo 
139eea8f41cSTejun Heo 	struct rcu_head			rcu_head;
140d09d8df3SJosef Bacik 
141d09d8df3SJosef Bacik 	atomic_t			use_delay;
142d09d8df3SJosef Bacik 	atomic64_t			delay_nsec;
143d09d8df3SJosef Bacik 	atomic64_t			delay_start;
144d09d8df3SJosef Bacik 	u64				last_delay;
145d09d8df3SJosef Bacik 	int				last_use;
146eea8f41cSTejun Heo };
147eea8f41cSTejun Heo 
148e4a9bde9STejun Heo typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
14981437648STejun Heo typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
150e4a9bde9STejun Heo typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
15169d7fde5STejun Heo typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
152001bea73STejun Heo typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(gfp_t gfp, int node);
153a9520cd6STejun Heo typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
154a9520cd6STejun Heo typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
155a9520cd6STejun Heo typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
156001bea73STejun Heo typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
157a9520cd6STejun Heo typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
158903d23f0SJosef Bacik typedef size_t (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd, char *buf,
159903d23f0SJosef Bacik 				      size_t size);
160eea8f41cSTejun Heo 
161eea8f41cSTejun Heo struct blkcg_policy {
162eea8f41cSTejun Heo 	int				plid;
163eea8f41cSTejun Heo 	/* cgroup files for the policy */
1642ee867dcSTejun Heo 	struct cftype			*dfl_cftypes;
165880f50e2STejun Heo 	struct cftype			*legacy_cftypes;
166eea8f41cSTejun Heo 
167eea8f41cSTejun Heo 	/* operations */
168e4a9bde9STejun Heo 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
169e4bc13adSLinus Torvalds 	blkcg_pol_init_cpd_fn		*cpd_init_fn;
170e4a9bde9STejun Heo 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
17169d7fde5STejun Heo 	blkcg_pol_bind_cpd_fn		*cpd_bind_fn;
172e4a9bde9STejun Heo 
173001bea73STejun Heo 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
174eea8f41cSTejun Heo 	blkcg_pol_init_pd_fn		*pd_init_fn;
175eea8f41cSTejun Heo 	blkcg_pol_online_pd_fn		*pd_online_fn;
176eea8f41cSTejun Heo 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
177001bea73STejun Heo 	blkcg_pol_free_pd_fn		*pd_free_fn;
178eea8f41cSTejun Heo 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
179903d23f0SJosef Bacik 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
180eea8f41cSTejun Heo };
181eea8f41cSTejun Heo 
182eea8f41cSTejun Heo extern struct blkcg blkcg_root;
183496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css;
184eea8f41cSTejun Heo 
18524f29046STejun Heo struct blkcg_gq *blkg_lookup_slowpath(struct blkcg *blkcg,
18624f29046STejun Heo 				      struct request_queue *q, bool update_hint);
18749f4c2dcSDennis Zhou (Facebook) struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
18849f4c2dcSDennis Zhou (Facebook) 				      struct request_queue *q);
189eea8f41cSTejun Heo struct blkcg_gq *blkg_lookup_create(struct blkcg *blkcg,
190eea8f41cSTejun Heo 				    struct request_queue *q);
191eea8f41cSTejun Heo int blkcg_init_queue(struct request_queue *q);
192eea8f41cSTejun Heo void blkcg_drain_queue(struct request_queue *q);
193eea8f41cSTejun Heo void blkcg_exit_queue(struct request_queue *q);
194eea8f41cSTejun Heo 
195eea8f41cSTejun Heo /* Blkio controller policy registration */
196eea8f41cSTejun Heo int blkcg_policy_register(struct blkcg_policy *pol);
197eea8f41cSTejun Heo void blkcg_policy_unregister(struct blkcg_policy *pol);
198eea8f41cSTejun Heo int blkcg_activate_policy(struct request_queue *q,
199eea8f41cSTejun Heo 			  const struct blkcg_policy *pol);
200eea8f41cSTejun Heo void blkcg_deactivate_policy(struct request_queue *q,
201eea8f41cSTejun Heo 			     const struct blkcg_policy *pol);
202eea8f41cSTejun Heo 
203dd165eb3STejun Heo const char *blkg_dev_name(struct blkcg_gq *blkg);
204eea8f41cSTejun Heo void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
205eea8f41cSTejun Heo 		       u64 (*prfill)(struct seq_file *,
206eea8f41cSTejun Heo 				     struct blkg_policy_data *, int),
207eea8f41cSTejun Heo 		       const struct blkcg_policy *pol, int data,
208eea8f41cSTejun Heo 		       bool show_total);
209eea8f41cSTejun Heo u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
210eea8f41cSTejun Heo u64 __blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
211eea8f41cSTejun Heo 			 const struct blkg_rwstat *rwstat);
212eea8f41cSTejun Heo u64 blkg_prfill_stat(struct seq_file *sf, struct blkg_policy_data *pd, int off);
213eea8f41cSTejun Heo u64 blkg_prfill_rwstat(struct seq_file *sf, struct blkg_policy_data *pd,
214eea8f41cSTejun Heo 		       int off);
21577ea7338STejun Heo int blkg_print_stat_bytes(struct seq_file *sf, void *v);
21677ea7338STejun Heo int blkg_print_stat_ios(struct seq_file *sf, void *v);
21777ea7338STejun Heo int blkg_print_stat_bytes_recursive(struct seq_file *sf, void *v);
21877ea7338STejun Heo int blkg_print_stat_ios_recursive(struct seq_file *sf, void *v);
219eea8f41cSTejun Heo 
220f12c74caSTejun Heo u64 blkg_stat_recursive_sum(struct blkcg_gq *blkg,
221f12c74caSTejun Heo 			    struct blkcg_policy *pol, int off);
222f12c74caSTejun Heo struct blkg_rwstat blkg_rwstat_recursive_sum(struct blkcg_gq *blkg,
223f12c74caSTejun Heo 					     struct blkcg_policy *pol, int off);
224eea8f41cSTejun Heo 
225eea8f41cSTejun Heo struct blkg_conf_ctx {
226eea8f41cSTejun Heo 	struct gendisk			*disk;
227eea8f41cSTejun Heo 	struct blkcg_gq			*blkg;
22836aa9e5fSTejun Heo 	char				*body;
229eea8f41cSTejun Heo };
230eea8f41cSTejun Heo 
231eea8f41cSTejun Heo int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
23236aa9e5fSTejun Heo 		   char *input, struct blkg_conf_ctx *ctx);
233eea8f41cSTejun Heo void blkg_conf_finish(struct blkg_conf_ctx *ctx);
234eea8f41cSTejun Heo 
23527e6fa99SDennis Zhou (Facebook) /**
23627e6fa99SDennis Zhou (Facebook)  * blkcg_css - find the current css
23727e6fa99SDennis Zhou (Facebook)  *
23827e6fa99SDennis Zhou (Facebook)  * Find the css associated with either the kthread or the current task.
23927e6fa99SDennis Zhou (Facebook)  * This may return a dying css, so it is up to the caller to use tryget logic
24027e6fa99SDennis Zhou (Facebook)  * to confirm it is alive and well.
24127e6fa99SDennis Zhou (Facebook)  */
24227e6fa99SDennis Zhou (Facebook) static inline struct cgroup_subsys_state *blkcg_css(void)
24327e6fa99SDennis Zhou (Facebook) {
24427e6fa99SDennis Zhou (Facebook) 	struct cgroup_subsys_state *css;
24527e6fa99SDennis Zhou (Facebook) 
24627e6fa99SDennis Zhou (Facebook) 	css = kthread_blkcg();
24727e6fa99SDennis Zhou (Facebook) 	if (css)
24827e6fa99SDennis Zhou (Facebook) 		return css;
24927e6fa99SDennis Zhou (Facebook) 	return task_css(current, io_cgrp_id);
25027e6fa99SDennis Zhou (Facebook) }
25127e6fa99SDennis Zhou (Facebook) 
252eea8f41cSTejun Heo static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
253eea8f41cSTejun Heo {
254eea8f41cSTejun Heo 	return css ? container_of(css, struct blkcg, css) : NULL;
255eea8f41cSTejun Heo }
256eea8f41cSTejun Heo 
25727e6fa99SDennis Zhou (Facebook) /**
25827e6fa99SDennis Zhou (Facebook)  * __bio_blkcg - internal version of bio_blkcg for bfq and cfq
25927e6fa99SDennis Zhou (Facebook)  *
26027e6fa99SDennis Zhou (Facebook)  * DO NOT USE.
26127e6fa99SDennis Zhou (Facebook)  * There is a flaw using this version of the function.  In particular, this was
26227e6fa99SDennis Zhou (Facebook)  * used in a broken paradigm where association was called on the given css.  It
26327e6fa99SDennis Zhou (Facebook)  * is possible though that the returned css from task_css() is in the process
26427e6fa99SDennis Zhou (Facebook)  * of dying due to migration of the current task.  So it is improper to assume
26527e6fa99SDennis Zhou (Facebook)  * *_get() is going to succeed.  Both BFQ and CFQ rely on this logic and will
26627e6fa99SDennis Zhou (Facebook)  * take additional work to handle more gracefully.
26727e6fa99SDennis Zhou (Facebook)  */
26827e6fa99SDennis Zhou (Facebook) static inline struct blkcg *__bio_blkcg(struct bio *bio)
269eea8f41cSTejun Heo {
270c839e7a0SDennis Zhou (Facebook) 	if (bio && bio->bi_blkg)
271c839e7a0SDennis Zhou (Facebook) 		return bio->bi_blkg->blkcg;
27227e6fa99SDennis Zhou (Facebook) 	return css_to_blkcg(blkcg_css());
27327e6fa99SDennis Zhou (Facebook) }
27427e6fa99SDennis Zhou (Facebook) 
27527e6fa99SDennis Zhou (Facebook) /**
27627e6fa99SDennis Zhou (Facebook)  * bio_blkcg - grab the blkcg associated with a bio
27727e6fa99SDennis Zhou (Facebook)  * @bio: target bio
27827e6fa99SDennis Zhou (Facebook)  *
27927e6fa99SDennis Zhou (Facebook)  * This returns the blkcg associated with a bio, NULL if not associated.
28027e6fa99SDennis Zhou (Facebook)  * Callers are expected to either handle NULL or know association has been
28127e6fa99SDennis Zhou (Facebook)  * done prior to calling this.
28227e6fa99SDennis Zhou (Facebook)  */
28327e6fa99SDennis Zhou (Facebook) static inline struct blkcg *bio_blkcg(struct bio *bio)
28427e6fa99SDennis Zhou (Facebook) {
285c839e7a0SDennis Zhou (Facebook) 	if (bio && bio->bi_blkg)
286c839e7a0SDennis Zhou (Facebook) 		return bio->bi_blkg->blkcg;
28727e6fa99SDennis Zhou (Facebook) 	return NULL;
288fd383c2dSTejun Heo }
289fd383c2dSTejun Heo 
290d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void)
291d09d8df3SJosef Bacik {
292d09d8df3SJosef Bacik 	struct cgroup_subsys_state *css;
293d09d8df3SJosef Bacik 	bool ret = false;
294d09d8df3SJosef Bacik 
295d09d8df3SJosef Bacik 	rcu_read_lock();
296d09d8df3SJosef Bacik 	css = kthread_blkcg();
297d09d8df3SJosef Bacik 	if (!css)
298d09d8df3SJosef Bacik 		css = task_css(current, io_cgrp_id);
299d09d8df3SJosef Bacik 	while (css) {
300d09d8df3SJosef Bacik 		if (atomic_read(&css->cgroup->congestion_count)) {
301d09d8df3SJosef Bacik 			ret = true;
302d09d8df3SJosef Bacik 			break;
303d09d8df3SJosef Bacik 		}
304d09d8df3SJosef Bacik 		css = css->parent;
305d09d8df3SJosef Bacik 	}
306d09d8df3SJosef Bacik 	rcu_read_unlock();
307d09d8df3SJosef Bacik 	return ret;
308d09d8df3SJosef Bacik }
309d09d8df3SJosef Bacik 
310eea8f41cSTejun Heo /**
311c7c98fd3SJosef Bacik  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
312c7c98fd3SJosef Bacik  * @return: true if this bio needs to be submitted with the root blkg context.
313c7c98fd3SJosef Bacik  *
314c7c98fd3SJosef Bacik  * In order to avoid priority inversions we sometimes need to issue a bio as if
315c7c98fd3SJosef Bacik  * it were attached to the root blkg, and then backcharge to the actual owning
316c7c98fd3SJosef Bacik  * blkg.  The idea is we do bio_blkcg() to look up the actual context for the
317c7c98fd3SJosef Bacik  * bio and attach the appropriate blkg to the bio.  Then we call this helper and
318c7c98fd3SJosef Bacik  * if it is true run with the root blkg for that queue and then do any
319c7c98fd3SJosef Bacik  * backcharging to the originating cgroup once the io is complete.
320c7c98fd3SJosef Bacik  */
321c7c98fd3SJosef Bacik static inline bool bio_issue_as_root_blkg(struct bio *bio)
322c7c98fd3SJosef Bacik {
3230d1e0c7cSJosef Bacik 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
324c7c98fd3SJosef Bacik }
325c7c98fd3SJosef Bacik 
326c7c98fd3SJosef Bacik /**
327eea8f41cSTejun Heo  * blkcg_parent - get the parent of a blkcg
328eea8f41cSTejun Heo  * @blkcg: blkcg of interest
329eea8f41cSTejun Heo  *
330eea8f41cSTejun Heo  * Return the parent blkcg of @blkcg.  Can be called anytime.
331eea8f41cSTejun Heo  */
332eea8f41cSTejun Heo static inline struct blkcg *blkcg_parent(struct blkcg *blkcg)
333eea8f41cSTejun Heo {
334eea8f41cSTejun Heo 	return css_to_blkcg(blkcg->css.parent);
335eea8f41cSTejun Heo }
336eea8f41cSTejun Heo 
337eea8f41cSTejun Heo /**
33824f29046STejun Heo  * __blkg_lookup - internal version of blkg_lookup()
33924f29046STejun Heo  * @blkcg: blkcg of interest
34024f29046STejun Heo  * @q: request_queue of interest
34124f29046STejun Heo  * @update_hint: whether to update lookup hint with the result or not
34224f29046STejun Heo  *
34324f29046STejun Heo  * This is internal version and shouldn't be used by policy
34424f29046STejun Heo  * implementations.  Looks up blkgs for the @blkcg - @q pair regardless of
34524f29046STejun Heo  * @q's bypass state.  If @update_hint is %true, the caller should be
34624f29046STejun Heo  * holding @q->queue_lock and lookup hint is updated on success.
34724f29046STejun Heo  */
34824f29046STejun Heo static inline struct blkcg_gq *__blkg_lookup(struct blkcg *blkcg,
34924f29046STejun Heo 					     struct request_queue *q,
35024f29046STejun Heo 					     bool update_hint)
35124f29046STejun Heo {
35224f29046STejun Heo 	struct blkcg_gq *blkg;
35324f29046STejun Heo 
35485b6bc9dSTejun Heo 	if (blkcg == &blkcg_root)
35585b6bc9dSTejun Heo 		return q->root_blkg;
35685b6bc9dSTejun Heo 
35724f29046STejun Heo 	blkg = rcu_dereference(blkcg->blkg_hint);
35824f29046STejun Heo 	if (blkg && blkg->q == q)
35924f29046STejun Heo 		return blkg;
36024f29046STejun Heo 
36124f29046STejun Heo 	return blkg_lookup_slowpath(blkcg, q, update_hint);
36224f29046STejun Heo }
36324f29046STejun Heo 
36424f29046STejun Heo /**
36524f29046STejun Heo  * blkg_lookup - lookup blkg for the specified blkcg - q pair
36624f29046STejun Heo  * @blkcg: blkcg of interest
36724f29046STejun Heo  * @q: request_queue of interest
36824f29046STejun Heo  *
36924f29046STejun Heo  * Lookup blkg for the @blkcg - @q pair.  This function should be called
37024f29046STejun Heo  * under RCU read lock and is guaranteed to return %NULL if @q is bypassing
37124f29046STejun Heo  * - see blk_queue_bypass_start() for details.
37224f29046STejun Heo  */
37324f29046STejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
37424f29046STejun Heo 					   struct request_queue *q)
37524f29046STejun Heo {
37624f29046STejun Heo 	WARN_ON_ONCE(!rcu_read_lock_held());
37724f29046STejun Heo 
37824f29046STejun Heo 	if (unlikely(blk_queue_bypass(q)))
37924f29046STejun Heo 		return NULL;
38024f29046STejun Heo 	return __blkg_lookup(blkcg, q, false);
38124f29046STejun Heo }
38224f29046STejun Heo 
38324f29046STejun Heo /**
384b86d865cSBart Van Assche  * blk_queue_root_blkg - return blkg for the (blkcg_root, @q) pair
3856bad9b21SBart Van Assche  * @q: request_queue of interest
3866bad9b21SBart Van Assche  *
3876bad9b21SBart Van Assche  * Lookup blkg for @q at the root level. See also blkg_lookup().
3886bad9b21SBart Van Assche  */
389b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
3906bad9b21SBart Van Assche {
391b86d865cSBart Van Assche 	return q->root_blkg;
3926bad9b21SBart Van Assche }
3936bad9b21SBart Van Assche 
3946bad9b21SBart Van Assche /**
395eea8f41cSTejun Heo  * blkg_to_pdata - get policy private data
396eea8f41cSTejun Heo  * @blkg: blkg of interest
397eea8f41cSTejun Heo  * @pol: policy of interest
398eea8f41cSTejun Heo  *
399eea8f41cSTejun Heo  * Return pointer to private data associated with the @blkg-@pol pair.
400eea8f41cSTejun Heo  */
401eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
402eea8f41cSTejun Heo 						  struct blkcg_policy *pol)
403eea8f41cSTejun Heo {
404eea8f41cSTejun Heo 	return blkg ? blkg->pd[pol->plid] : NULL;
405eea8f41cSTejun Heo }
406eea8f41cSTejun Heo 
407e4bc13adSLinus Torvalds static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
408e4bc13adSLinus Torvalds 						     struct blkcg_policy *pol)
409e4bc13adSLinus Torvalds {
41081437648STejun Heo 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
411e4bc13adSLinus Torvalds }
412e4bc13adSLinus Torvalds 
413eea8f41cSTejun Heo /**
414eea8f41cSTejun Heo  * pdata_to_blkg - get blkg associated with policy private data
415eea8f41cSTejun Heo  * @pd: policy private data of interest
416eea8f41cSTejun Heo  *
417eea8f41cSTejun Heo  * @pd is policy private data.  Determine the blkg it's associated with.
418eea8f41cSTejun Heo  */
419eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
420eea8f41cSTejun Heo {
421eea8f41cSTejun Heo 	return pd ? pd->blkg : NULL;
422eea8f41cSTejun Heo }
423eea8f41cSTejun Heo 
42481437648STejun Heo static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
42581437648STejun Heo {
42681437648STejun Heo 	return cpd ? cpd->blkcg : NULL;
42781437648STejun Heo }
42881437648STejun Heo 
42959b57717SDennis Zhou (Facebook) extern void blkcg_destroy_blkgs(struct blkcg *blkcg);
43059b57717SDennis Zhou (Facebook) 
43159b57717SDennis Zhou (Facebook) #ifdef CONFIG_CGROUP_WRITEBACK
43259b57717SDennis Zhou (Facebook) 
43359b57717SDennis Zhou (Facebook) /**
43459b57717SDennis Zhou (Facebook)  * blkcg_cgwb_get - get a reference for blkcg->cgwb_list
43559b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
43659b57717SDennis Zhou (Facebook)  *
43759b57717SDennis Zhou (Facebook)  * This is used to track the number of active wb's related to a blkcg.
43859b57717SDennis Zhou (Facebook)  */
43959b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg)
44059b57717SDennis Zhou (Facebook) {
44159b57717SDennis Zhou (Facebook) 	refcount_inc(&blkcg->cgwb_refcnt);
44259b57717SDennis Zhou (Facebook) }
44359b57717SDennis Zhou (Facebook) 
44459b57717SDennis Zhou (Facebook) /**
44559b57717SDennis Zhou (Facebook)  * blkcg_cgwb_put - put a reference for @blkcg->cgwb_list
44659b57717SDennis Zhou (Facebook)  * @blkcg: blkcg of interest
44759b57717SDennis Zhou (Facebook)  *
44859b57717SDennis Zhou (Facebook)  * This is used to track the number of active wb's related to a blkcg.
44959b57717SDennis Zhou (Facebook)  * When this count goes to zero, all active wb has finished so the
45059b57717SDennis Zhou (Facebook)  * blkcg can continue destruction by calling blkcg_destroy_blkgs().
45159b57717SDennis Zhou (Facebook)  * This work may occur in cgwb_release_workfn() on the cgwb_release
45259b57717SDennis Zhou (Facebook)  * workqueue.
45359b57717SDennis Zhou (Facebook)  */
45459b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg)
45559b57717SDennis Zhou (Facebook) {
45659b57717SDennis Zhou (Facebook) 	if (refcount_dec_and_test(&blkcg->cgwb_refcnt))
45759b57717SDennis Zhou (Facebook) 		blkcg_destroy_blkgs(blkcg);
45859b57717SDennis Zhou (Facebook) }
45959b57717SDennis Zhou (Facebook) 
46059b57717SDennis Zhou (Facebook) #else
46159b57717SDennis Zhou (Facebook) 
46259b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_get(struct blkcg *blkcg) { }
46359b57717SDennis Zhou (Facebook) 
46459b57717SDennis Zhou (Facebook) static inline void blkcg_cgwb_put(struct blkcg *blkcg)
46559b57717SDennis Zhou (Facebook) {
46659b57717SDennis Zhou (Facebook) 	/* wb isn't being accounted, so trigger destruction right away */
46759b57717SDennis Zhou (Facebook) 	blkcg_destroy_blkgs(blkcg);
46859b57717SDennis Zhou (Facebook) }
46959b57717SDennis Zhou (Facebook) 
47059b57717SDennis Zhou (Facebook) #endif
47159b57717SDennis Zhou (Facebook) 
472eea8f41cSTejun Heo /**
473eea8f41cSTejun Heo  * blkg_path - format cgroup path of blkg
474eea8f41cSTejun Heo  * @blkg: blkg of interest
475eea8f41cSTejun Heo  * @buf: target buffer
476eea8f41cSTejun Heo  * @buflen: target buffer length
477eea8f41cSTejun Heo  *
478eea8f41cSTejun Heo  * Format the path of the cgroup of @blkg into @buf.
479eea8f41cSTejun Heo  */
480eea8f41cSTejun Heo static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
481eea8f41cSTejun Heo {
4824c737b41STejun Heo 	return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
483eea8f41cSTejun Heo }
484eea8f41cSTejun Heo 
485eea8f41cSTejun Heo /**
486eea8f41cSTejun Heo  * blkg_get - get a blkg reference
487eea8f41cSTejun Heo  * @blkg: blkg to get
488eea8f41cSTejun Heo  *
489eea8f41cSTejun Heo  * The caller should be holding an existing reference.
490eea8f41cSTejun Heo  */
491eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg)
492eea8f41cSTejun Heo {
493eea8f41cSTejun Heo 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
494eea8f41cSTejun Heo 	atomic_inc(&blkg->refcnt);
495eea8f41cSTejun Heo }
496eea8f41cSTejun Heo 
497d09d8df3SJosef Bacik /**
498d09d8df3SJosef Bacik  * blkg_try_get - try and get a blkg reference
499d09d8df3SJosef Bacik  * @blkg: blkg to get
500d09d8df3SJosef Bacik  *
501d09d8df3SJosef Bacik  * This is for use when doing an RCU lookup of the blkg.  We may be in the midst
502d09d8df3SJosef Bacik  * of freeing this blkg, so we can only use it if the refcnt is not zero.
503d09d8df3SJosef Bacik  */
504d09d8df3SJosef Bacik static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg)
505d09d8df3SJosef Bacik {
506d09d8df3SJosef Bacik 	if (atomic_inc_not_zero(&blkg->refcnt))
507d09d8df3SJosef Bacik 		return blkg;
508d09d8df3SJosef Bacik 	return NULL;
509d09d8df3SJosef Bacik }
510d09d8df3SJosef Bacik 
51107b05bccSDennis Zhou (Facebook) /**
51207b05bccSDennis Zhou (Facebook)  * blkg_try_get_closest - try and get a blkg ref on the closet blkg
51307b05bccSDennis Zhou (Facebook)  * @blkg: blkg to get
51407b05bccSDennis Zhou (Facebook)  *
51507b05bccSDennis Zhou (Facebook)  * This walks up the blkg tree to find the closest non-dying blkg and returns
51607b05bccSDennis Zhou (Facebook)  * the blkg that it did association with as it may not be the passed in blkg.
51707b05bccSDennis Zhou (Facebook)  */
51807b05bccSDennis Zhou (Facebook) static inline struct blkcg_gq *blkg_try_get_closest(struct blkcg_gq *blkg)
51907b05bccSDennis Zhou (Facebook) {
52007b05bccSDennis Zhou (Facebook) 	while (!atomic_inc_not_zero(&blkg->refcnt))
52107b05bccSDennis Zhou (Facebook) 		blkg = blkg->parent;
52207b05bccSDennis Zhou (Facebook) 
52307b05bccSDennis Zhou (Facebook) 	return blkg;
52407b05bccSDennis Zhou (Facebook) }
525d09d8df3SJosef Bacik 
526eea8f41cSTejun Heo void __blkg_release_rcu(struct rcu_head *rcu);
527eea8f41cSTejun Heo 
528eea8f41cSTejun Heo /**
529eea8f41cSTejun Heo  * blkg_put - put a blkg reference
530eea8f41cSTejun Heo  * @blkg: blkg to put
531eea8f41cSTejun Heo  */
532eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg)
533eea8f41cSTejun Heo {
534eea8f41cSTejun Heo 	WARN_ON_ONCE(atomic_read(&blkg->refcnt) <= 0);
535eea8f41cSTejun Heo 	if (atomic_dec_and_test(&blkg->refcnt))
536eea8f41cSTejun Heo 		call_rcu(&blkg->rcu_head, __blkg_release_rcu);
537eea8f41cSTejun Heo }
538eea8f41cSTejun Heo 
539eea8f41cSTejun Heo /**
540eea8f41cSTejun Heo  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
541eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
542eea8f41cSTejun Heo  * @pos_css: used for iteration
543eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
544eea8f41cSTejun Heo  *
545eea8f41cSTejun Heo  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
546eea8f41cSTejun Heo  * read locked.  If called under either blkcg or queue lock, the iteration
547eea8f41cSTejun Heo  * is guaranteed to include all and only online blkgs.  The caller may
548eea8f41cSTejun Heo  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
549eea8f41cSTejun Heo  * @p_blkg is included in the iteration and the first node to be visited.
550eea8f41cSTejun Heo  */
551eea8f41cSTejun Heo #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
552eea8f41cSTejun Heo 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
553eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
554eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
555eea8f41cSTejun Heo 
556eea8f41cSTejun Heo /**
557eea8f41cSTejun Heo  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
558eea8f41cSTejun Heo  * @d_blkg: loop cursor pointing to the current descendant
559eea8f41cSTejun Heo  * @pos_css: used for iteration
560eea8f41cSTejun Heo  * @p_blkg: target blkg to walk descendants of
561eea8f41cSTejun Heo  *
562eea8f41cSTejun Heo  * Similar to blkg_for_each_descendant_pre() but performs post-order
563eea8f41cSTejun Heo  * traversal instead.  Synchronization rules are the same.  @p_blkg is
564eea8f41cSTejun Heo  * included in the iteration and the last node to be visited.
565eea8f41cSTejun Heo  */
566eea8f41cSTejun Heo #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
567eea8f41cSTejun Heo 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
568eea8f41cSTejun Heo 		if (((d_blkg) = __blkg_lookup(css_to_blkcg(pos_css),	\
569eea8f41cSTejun Heo 					      (p_blkg)->q, false)))
570eea8f41cSTejun Heo 
571eea8f41cSTejun Heo /**
572eea8f41cSTejun Heo  * blk_get_rl - get request_list to use
573eea8f41cSTejun Heo  * @q: request_queue of interest
574eea8f41cSTejun Heo  * @bio: bio which will be attached to the allocated request (may be %NULL)
575eea8f41cSTejun Heo  *
576eea8f41cSTejun Heo  * The caller wants to allocate a request from @q to use for @bio.  Find
577eea8f41cSTejun Heo  * the request_list to use and obtain a reference on it.  Should be called
578eea8f41cSTejun Heo  * under queue_lock.  This function is guaranteed to return non-%NULL
579eea8f41cSTejun Heo  * request_list.
580eea8f41cSTejun Heo  */
581eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q,
582eea8f41cSTejun Heo 					      struct bio *bio)
583eea8f41cSTejun Heo {
584eea8f41cSTejun Heo 	struct blkcg *blkcg;
585eea8f41cSTejun Heo 	struct blkcg_gq *blkg;
586eea8f41cSTejun Heo 
587eea8f41cSTejun Heo 	rcu_read_lock();
588eea8f41cSTejun Heo 
589eea8f41cSTejun Heo 	blkcg = bio_blkcg(bio);
590*f0fcb3ecSDennis Zhou (Facebook) 	if (!blkcg)
591*f0fcb3ecSDennis Zhou (Facebook) 		blkcg = css_to_blkcg(blkcg_css());
592eea8f41cSTejun Heo 
593eea8f41cSTejun Heo 	/* bypass blkg lookup and use @q->root_rl directly for root */
594eea8f41cSTejun Heo 	if (blkcg == &blkcg_root)
595eea8f41cSTejun Heo 		goto root_rl;
596eea8f41cSTejun Heo 
597eea8f41cSTejun Heo 	/*
598eea8f41cSTejun Heo 	 * Try to use blkg->rl.  blkg lookup may fail under memory pressure
599eea8f41cSTejun Heo 	 * or if either the blkcg or queue is going away.  Fall back to
600eea8f41cSTejun Heo 	 * root_rl in such cases.
601eea8f41cSTejun Heo 	 */
602ae118896STejun Heo 	blkg = blkg_lookup(blkcg, q);
603ae118896STejun Heo 	if (unlikely(!blkg))
604eea8f41cSTejun Heo 		goto root_rl;
605eea8f41cSTejun Heo 
606*f0fcb3ecSDennis Zhou (Facebook) 	if (!blkg_try_get(blkg))
607*f0fcb3ecSDennis Zhou (Facebook) 		goto root_rl;
608eea8f41cSTejun Heo 	rcu_read_unlock();
609eea8f41cSTejun Heo 	return &blkg->rl;
610eea8f41cSTejun Heo root_rl:
611eea8f41cSTejun Heo 	rcu_read_unlock();
612eea8f41cSTejun Heo 	return &q->root_rl;
613eea8f41cSTejun Heo }
614eea8f41cSTejun Heo 
615eea8f41cSTejun Heo /**
616eea8f41cSTejun Heo  * blk_put_rl - put request_list
617eea8f41cSTejun Heo  * @rl: request_list to put
618eea8f41cSTejun Heo  *
619eea8f41cSTejun Heo  * Put the reference acquired by blk_get_rl().  Should be called under
620eea8f41cSTejun Heo  * queue_lock.
621eea8f41cSTejun Heo  */
622eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl)
623eea8f41cSTejun Heo {
624401efbf8STejun Heo 	if (rl->blkg->blkcg != &blkcg_root)
625eea8f41cSTejun Heo 		blkg_put(rl->blkg);
626eea8f41cSTejun Heo }
627eea8f41cSTejun Heo 
628eea8f41cSTejun Heo /**
629eea8f41cSTejun Heo  * blk_rq_set_rl - associate a request with a request_list
630eea8f41cSTejun Heo  * @rq: request of interest
631eea8f41cSTejun Heo  * @rl: target request_list
632eea8f41cSTejun Heo  *
633eea8f41cSTejun Heo  * Associate @rq with @rl so that accounting and freeing can know the
634eea8f41cSTejun Heo  * request_list @rq came from.
635eea8f41cSTejun Heo  */
636eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl)
637eea8f41cSTejun Heo {
638eea8f41cSTejun Heo 	rq->rl = rl;
639eea8f41cSTejun Heo }
640eea8f41cSTejun Heo 
641eea8f41cSTejun Heo /**
642eea8f41cSTejun Heo  * blk_rq_rl - return the request_list a request came from
643eea8f41cSTejun Heo  * @rq: request of interest
644eea8f41cSTejun Heo  *
645eea8f41cSTejun Heo  * Return the request_list @rq is allocated from.
646eea8f41cSTejun Heo  */
647eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq)
648eea8f41cSTejun Heo {
649eea8f41cSTejun Heo 	return rq->rl;
650eea8f41cSTejun Heo }
651eea8f41cSTejun Heo 
652eea8f41cSTejun Heo struct request_list *__blk_queue_next_rl(struct request_list *rl,
653eea8f41cSTejun Heo 					 struct request_queue *q);
654eea8f41cSTejun Heo /**
655eea8f41cSTejun Heo  * blk_queue_for_each_rl - iterate through all request_lists of a request_queue
656eea8f41cSTejun Heo  *
657eea8f41cSTejun Heo  * Should be used under queue_lock.
658eea8f41cSTejun Heo  */
659eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
660eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = __blk_queue_next_rl((rl), (q)))
661eea8f41cSTejun Heo 
66224bdb8efSTejun Heo static inline int blkg_stat_init(struct blkg_stat *stat, gfp_t gfp)
663eea8f41cSTejun Heo {
66424bdb8efSTejun Heo 	int ret;
66524bdb8efSTejun Heo 
66624bdb8efSTejun Heo 	ret = percpu_counter_init(&stat->cpu_cnt, 0, gfp);
66724bdb8efSTejun Heo 	if (ret)
66824bdb8efSTejun Heo 		return ret;
66924bdb8efSTejun Heo 
670e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
67124bdb8efSTejun Heo 	return 0;
67224bdb8efSTejun Heo }
67324bdb8efSTejun Heo 
67424bdb8efSTejun Heo static inline void blkg_stat_exit(struct blkg_stat *stat)
67524bdb8efSTejun Heo {
67624bdb8efSTejun Heo 	percpu_counter_destroy(&stat->cpu_cnt);
677eea8f41cSTejun Heo }
678eea8f41cSTejun Heo 
679eea8f41cSTejun Heo /**
680eea8f41cSTejun Heo  * blkg_stat_add - add a value to a blkg_stat
681eea8f41cSTejun Heo  * @stat: target blkg_stat
682eea8f41cSTejun Heo  * @val: value to add
683eea8f41cSTejun Heo  *
68424bdb8efSTejun Heo  * Add @val to @stat.  The caller must ensure that IRQ on the same CPU
68524bdb8efSTejun Heo  * don't re-enter this function for the same counter.
686eea8f41cSTejun Heo  */
687eea8f41cSTejun Heo static inline void blkg_stat_add(struct blkg_stat *stat, uint64_t val)
688eea8f41cSTejun Heo {
689104b4e51SNikolay Borisov 	percpu_counter_add_batch(&stat->cpu_cnt, val, BLKG_STAT_CPU_BATCH);
690eea8f41cSTejun Heo }
691eea8f41cSTejun Heo 
692eea8f41cSTejun Heo /**
693eea8f41cSTejun Heo  * blkg_stat_read - read the current value of a blkg_stat
694eea8f41cSTejun Heo  * @stat: blkg_stat to read
695eea8f41cSTejun Heo  */
696eea8f41cSTejun Heo static inline uint64_t blkg_stat_read(struct blkg_stat *stat)
697eea8f41cSTejun Heo {
69824bdb8efSTejun Heo 	return percpu_counter_sum_positive(&stat->cpu_cnt);
699eea8f41cSTejun Heo }
700eea8f41cSTejun Heo 
701eea8f41cSTejun Heo /**
702eea8f41cSTejun Heo  * blkg_stat_reset - reset a blkg_stat
703eea8f41cSTejun Heo  * @stat: blkg_stat to reset
704eea8f41cSTejun Heo  */
705eea8f41cSTejun Heo static inline void blkg_stat_reset(struct blkg_stat *stat)
706eea8f41cSTejun Heo {
70724bdb8efSTejun Heo 	percpu_counter_set(&stat->cpu_cnt, 0);
708e6269c44STejun Heo 	atomic64_set(&stat->aux_cnt, 0);
709eea8f41cSTejun Heo }
710eea8f41cSTejun Heo 
711eea8f41cSTejun Heo /**
712e6269c44STejun Heo  * blkg_stat_add_aux - add a blkg_stat into another's aux count
713eea8f41cSTejun Heo  * @to: the destination blkg_stat
714eea8f41cSTejun Heo  * @from: the source
715eea8f41cSTejun Heo  *
716e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
717eea8f41cSTejun Heo  */
718e6269c44STejun Heo static inline void blkg_stat_add_aux(struct blkg_stat *to,
719e6269c44STejun Heo 				     struct blkg_stat *from)
720eea8f41cSTejun Heo {
721e6269c44STejun Heo 	atomic64_add(blkg_stat_read(from) + atomic64_read(&from->aux_cnt),
722e6269c44STejun Heo 		     &to->aux_cnt);
723eea8f41cSTejun Heo }
724eea8f41cSTejun Heo 
72524bdb8efSTejun Heo static inline int blkg_rwstat_init(struct blkg_rwstat *rwstat, gfp_t gfp)
72624bdb8efSTejun Heo {
72724bdb8efSTejun Heo 	int i, ret;
72824bdb8efSTejun Heo 
72924bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
73024bdb8efSTejun Heo 		ret = percpu_counter_init(&rwstat->cpu_cnt[i], 0, gfp);
73124bdb8efSTejun Heo 		if (ret) {
73224bdb8efSTejun Heo 			while (--i >= 0)
73324bdb8efSTejun Heo 				percpu_counter_destroy(&rwstat->cpu_cnt[i]);
73424bdb8efSTejun Heo 			return ret;
73524bdb8efSTejun Heo 		}
73624bdb8efSTejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
73724bdb8efSTejun Heo 	}
73824bdb8efSTejun Heo 	return 0;
73924bdb8efSTejun Heo }
74024bdb8efSTejun Heo 
74124bdb8efSTejun Heo static inline void blkg_rwstat_exit(struct blkg_rwstat *rwstat)
742eea8f41cSTejun Heo {
743e6269c44STejun Heo 	int i;
744e6269c44STejun Heo 
745e6269c44STejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
74624bdb8efSTejun Heo 		percpu_counter_destroy(&rwstat->cpu_cnt[i]);
747eea8f41cSTejun Heo }
748eea8f41cSTejun Heo 
749eea8f41cSTejun Heo /**
750eea8f41cSTejun Heo  * blkg_rwstat_add - add a value to a blkg_rwstat
751eea8f41cSTejun Heo  * @rwstat: target blkg_rwstat
752ef295ecfSChristoph Hellwig  * @op: REQ_OP and flags
753eea8f41cSTejun Heo  * @val: value to add
754eea8f41cSTejun Heo  *
755eea8f41cSTejun Heo  * Add @val to @rwstat.  The counters are chosen according to @rw.  The
756eea8f41cSTejun Heo  * caller is responsible for synchronizing calls to this function.
757eea8f41cSTejun Heo  */
758eea8f41cSTejun Heo static inline void blkg_rwstat_add(struct blkg_rwstat *rwstat,
759ef295ecfSChristoph Hellwig 				   unsigned int op, uint64_t val)
760eea8f41cSTejun Heo {
76124bdb8efSTejun Heo 	struct percpu_counter *cnt;
762eea8f41cSTejun Heo 
763636620b6STejun Heo 	if (op_is_discard(op))
764636620b6STejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_DISCARD];
765636620b6STejun Heo 	else if (op_is_write(op))
76624bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_WRITE];
767eea8f41cSTejun Heo 	else
76824bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_READ];
769eea8f41cSTejun Heo 
770104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
77124bdb8efSTejun Heo 
772d71d9ae1SChristoph Hellwig 	if (op_is_sync(op))
77324bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_SYNC];
77424bdb8efSTejun Heo 	else
77524bdb8efSTejun Heo 		cnt = &rwstat->cpu_cnt[BLKG_RWSTAT_ASYNC];
77624bdb8efSTejun Heo 
777104b4e51SNikolay Borisov 	percpu_counter_add_batch(cnt, val, BLKG_STAT_CPU_BATCH);
778eea8f41cSTejun Heo }
779eea8f41cSTejun Heo 
780eea8f41cSTejun Heo /**
781eea8f41cSTejun Heo  * blkg_rwstat_read - read the current values of a blkg_rwstat
782eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
783eea8f41cSTejun Heo  *
78424bdb8efSTejun Heo  * Read the current snapshot of @rwstat and return it in the aux counts.
785eea8f41cSTejun Heo  */
786eea8f41cSTejun Heo static inline struct blkg_rwstat blkg_rwstat_read(struct blkg_rwstat *rwstat)
787eea8f41cSTejun Heo {
78824bdb8efSTejun Heo 	struct blkg_rwstat result;
78924bdb8efSTejun Heo 	int i;
790eea8f41cSTejun Heo 
79124bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
79224bdb8efSTejun Heo 		atomic64_set(&result.aux_cnt[i],
79324bdb8efSTejun Heo 			     percpu_counter_sum_positive(&rwstat->cpu_cnt[i]));
79424bdb8efSTejun Heo 	return result;
795eea8f41cSTejun Heo }
796eea8f41cSTejun Heo 
797eea8f41cSTejun Heo /**
798eea8f41cSTejun Heo  * blkg_rwstat_total - read the total count of a blkg_rwstat
799eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to read
800eea8f41cSTejun Heo  *
801eea8f41cSTejun Heo  * Return the total count of @rwstat regardless of the IO direction.  This
802eea8f41cSTejun Heo  * function can be called without synchronization and takes care of u64
803eea8f41cSTejun Heo  * atomicity.
804eea8f41cSTejun Heo  */
805eea8f41cSTejun Heo static inline uint64_t blkg_rwstat_total(struct blkg_rwstat *rwstat)
806eea8f41cSTejun Heo {
807eea8f41cSTejun Heo 	struct blkg_rwstat tmp = blkg_rwstat_read(rwstat);
808eea8f41cSTejun Heo 
80924bdb8efSTejun Heo 	return atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_READ]) +
81024bdb8efSTejun Heo 		atomic64_read(&tmp.aux_cnt[BLKG_RWSTAT_WRITE]);
811eea8f41cSTejun Heo }
812eea8f41cSTejun Heo 
813eea8f41cSTejun Heo /**
814eea8f41cSTejun Heo  * blkg_rwstat_reset - reset a blkg_rwstat
815eea8f41cSTejun Heo  * @rwstat: blkg_rwstat to reset
816eea8f41cSTejun Heo  */
817eea8f41cSTejun Heo static inline void blkg_rwstat_reset(struct blkg_rwstat *rwstat)
818eea8f41cSTejun Heo {
819e6269c44STejun Heo 	int i;
820e6269c44STejun Heo 
82124bdb8efSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++) {
82224bdb8efSTejun Heo 		percpu_counter_set(&rwstat->cpu_cnt[i], 0);
823e6269c44STejun Heo 		atomic64_set(&rwstat->aux_cnt[i], 0);
824eea8f41cSTejun Heo 	}
82524bdb8efSTejun Heo }
826eea8f41cSTejun Heo 
827eea8f41cSTejun Heo /**
828e6269c44STejun Heo  * blkg_rwstat_add_aux - add a blkg_rwstat into another's aux count
829eea8f41cSTejun Heo  * @to: the destination blkg_rwstat
830eea8f41cSTejun Heo  * @from: the source
831eea8f41cSTejun Heo  *
832e6269c44STejun Heo  * Add @from's count including the aux one to @to's aux count.
833eea8f41cSTejun Heo  */
834e6269c44STejun Heo static inline void blkg_rwstat_add_aux(struct blkg_rwstat *to,
835eea8f41cSTejun Heo 				       struct blkg_rwstat *from)
836eea8f41cSTejun Heo {
837ddc21231SArnd Bergmann 	u64 sum[BLKG_RWSTAT_NR];
838eea8f41cSTejun Heo 	int i;
839eea8f41cSTejun Heo 
840eea8f41cSTejun Heo 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
841ddc21231SArnd Bergmann 		sum[i] = percpu_counter_sum_positive(&from->cpu_cnt[i]);
842ddc21231SArnd Bergmann 
843ddc21231SArnd Bergmann 	for (i = 0; i < BLKG_RWSTAT_NR; i++)
844ddc21231SArnd Bergmann 		atomic64_add(sum[i] + atomic64_read(&from->aux_cnt[i]),
845e6269c44STejun Heo 			     &to->aux_cnt[i]);
846eea8f41cSTejun Heo }
847eea8f41cSTejun Heo 
848ae118896STejun Heo #ifdef CONFIG_BLK_DEV_THROTTLING
849ae118896STejun Heo extern bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
850ae118896STejun Heo 			   struct bio *bio);
851ae118896STejun Heo #else
852ae118896STejun Heo static inline bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
853ae118896STejun Heo 				  struct bio *bio) { return false; }
854ae118896STejun Heo #endif
855ae118896STejun Heo 
8565bf9a1f3SDennis Zhou (Facebook) 
8575bf9a1f3SDennis Zhou (Facebook) static inline void blkcg_bio_issue_init(struct bio *bio)
8585bf9a1f3SDennis Zhou (Facebook) {
8595bf9a1f3SDennis Zhou (Facebook) 	bio_issue_init(&bio->bi_issue, bio_sectors(bio));
8605bf9a1f3SDennis Zhou (Facebook) }
8615bf9a1f3SDennis Zhou (Facebook) 
862ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
863ae118896STejun Heo 					 struct bio *bio)
864ae118896STejun Heo {
865ae118896STejun Heo 	struct blkcg_gq *blkg;
866ae118896STejun Heo 	bool throtl = false;
867ae118896STejun Heo 
868ae118896STejun Heo 	rcu_read_lock();
869ae118896STejun Heo 
870a7b39b4eSDennis Zhou (Facebook) 	bio_associate_create_blkg(q, bio);
871a7b39b4eSDennis Zhou (Facebook) 	blkg = bio->bi_blkg;
872ae118896STejun Heo 
873ae118896STejun Heo 	throtl = blk_throtl_bio(q, blkg, bio);
874ae118896STejun Heo 
87577ea7338STejun Heo 	if (!throtl) {
876c454edc2SJosef Bacik 		/*
877c454edc2SJosef Bacik 		 * If the bio is flagged with BIO_QUEUE_ENTERED it means this
878c454edc2SJosef Bacik 		 * is a split bio and we would have already accounted for the
879c454edc2SJosef Bacik 		 * size of the bio.
880c454edc2SJosef Bacik 		 */
881c454edc2SJosef Bacik 		if (!bio_flagged(bio, BIO_QUEUE_ENTERED))
882ef295ecfSChristoph Hellwig 			blkg_rwstat_add(&blkg->stat_bytes, bio->bi_opf,
88377ea7338STejun Heo 					bio->bi_iter.bi_size);
884ef295ecfSChristoph Hellwig 		blkg_rwstat_add(&blkg->stat_ios, bio->bi_opf, 1);
88577ea7338STejun Heo 	}
88677ea7338STejun Heo 
8875bf9a1f3SDennis Zhou (Facebook) 	blkcg_bio_issue_init(bio);
8885bf9a1f3SDennis Zhou (Facebook) 
889ae118896STejun Heo 	rcu_read_unlock();
890ae118896STejun Heo 	return !throtl;
891ae118896STejun Heo }
892ae118896STejun Heo 
893d09d8df3SJosef Bacik static inline void blkcg_use_delay(struct blkcg_gq *blkg)
894d09d8df3SJosef Bacik {
895d09d8df3SJosef Bacik 	if (atomic_add_return(1, &blkg->use_delay) == 1)
896d09d8df3SJosef Bacik 		atomic_inc(&blkg->blkcg->css.cgroup->congestion_count);
897d09d8df3SJosef Bacik }
898d09d8df3SJosef Bacik 
899d09d8df3SJosef Bacik static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
900d09d8df3SJosef Bacik {
901d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
902d09d8df3SJosef Bacik 
903d09d8df3SJosef Bacik 	if (old == 0)
904d09d8df3SJosef Bacik 		return 0;
905d09d8df3SJosef Bacik 
906d09d8df3SJosef Bacik 	/*
907d09d8df3SJosef Bacik 	 * We do this song and dance because we can race with somebody else
908d09d8df3SJosef Bacik 	 * adding or removing delay.  If we just did an atomic_dec we'd end up
909d09d8df3SJosef Bacik 	 * negative and we'd already be in trouble.  We need to subtract 1 and
910d09d8df3SJosef Bacik 	 * then check to see if we were the last delay so we can drop the
911d09d8df3SJosef Bacik 	 * congestion count on the cgroup.
912d09d8df3SJosef Bacik 	 */
913d09d8df3SJosef Bacik 	while (old) {
914d09d8df3SJosef Bacik 		int cur = atomic_cmpxchg(&blkg->use_delay, old, old - 1);
915d09d8df3SJosef Bacik 		if (cur == old)
916d09d8df3SJosef Bacik 			break;
917d09d8df3SJosef Bacik 		old = cur;
918d09d8df3SJosef Bacik 	}
919d09d8df3SJosef Bacik 
920d09d8df3SJosef Bacik 	if (old == 0)
921d09d8df3SJosef Bacik 		return 0;
922d09d8df3SJosef Bacik 	if (old == 1)
923d09d8df3SJosef Bacik 		atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
924d09d8df3SJosef Bacik 	return 1;
925d09d8df3SJosef Bacik }
926d09d8df3SJosef Bacik 
927d09d8df3SJosef Bacik static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
928d09d8df3SJosef Bacik {
929d09d8df3SJosef Bacik 	int old = atomic_read(&blkg->use_delay);
930d09d8df3SJosef Bacik 	if (!old)
931d09d8df3SJosef Bacik 		return;
932d09d8df3SJosef Bacik 	/* We only want 1 person clearing the congestion count for this blkg. */
933d09d8df3SJosef Bacik 	while (old) {
934d09d8df3SJosef Bacik 		int cur = atomic_cmpxchg(&blkg->use_delay, old, 0);
935d09d8df3SJosef Bacik 		if (cur == old) {
936d09d8df3SJosef Bacik 			atomic_dec(&blkg->blkcg->css.cgroup->congestion_count);
937d09d8df3SJosef Bacik 			break;
938d09d8df3SJosef Bacik 		}
939d09d8df3SJosef Bacik 		old = cur;
940d09d8df3SJosef Bacik 	}
941d09d8df3SJosef Bacik }
942d09d8df3SJosef Bacik 
943d09d8df3SJosef Bacik void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
944d09d8df3SJosef Bacik void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
945d09d8df3SJosef Bacik void blkcg_maybe_throttle_current(void);
946eea8f41cSTejun Heo #else	/* CONFIG_BLK_CGROUP */
947eea8f41cSTejun Heo 
948efa7d1c7STejun Heo struct blkcg {
949efa7d1c7STejun Heo };
950eea8f41cSTejun Heo 
951eea8f41cSTejun Heo struct blkg_policy_data {
952eea8f41cSTejun Heo };
953eea8f41cSTejun Heo 
954e4bc13adSLinus Torvalds struct blkcg_policy_data {
955e4bc13adSLinus Torvalds };
956e4bc13adSLinus Torvalds 
957eea8f41cSTejun Heo struct blkcg_gq {
958eea8f41cSTejun Heo };
959eea8f41cSTejun Heo 
960eea8f41cSTejun Heo struct blkcg_policy {
961eea8f41cSTejun Heo };
962eea8f41cSTejun Heo 
963496d5e75STejun Heo #define blkcg_root_css	((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
964496d5e75STejun Heo 
965d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { }
966d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; }
967d09d8df3SJosef Bacik 
968efa7d1c7STejun Heo #ifdef CONFIG_BLOCK
969efa7d1c7STejun Heo 
970d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
971d09d8df3SJosef Bacik 
972eea8f41cSTejun Heo static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
973b86d865cSBart Van Assche static inline struct blkcg_gq *blk_queue_root_blkg(struct request_queue *q)
974b86d865cSBart Van Assche { return NULL; }
975eea8f41cSTejun Heo static inline int blkcg_init_queue(struct request_queue *q) { return 0; }
976eea8f41cSTejun Heo static inline void blkcg_drain_queue(struct request_queue *q) { }
977eea8f41cSTejun Heo static inline void blkcg_exit_queue(struct request_queue *q) { }
978eea8f41cSTejun Heo static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
979eea8f41cSTejun Heo static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
980eea8f41cSTejun Heo static inline int blkcg_activate_policy(struct request_queue *q,
981eea8f41cSTejun Heo 					const struct blkcg_policy *pol) { return 0; }
982eea8f41cSTejun Heo static inline void blkcg_deactivate_policy(struct request_queue *q,
983eea8f41cSTejun Heo 					   const struct blkcg_policy *pol) { }
984eea8f41cSTejun Heo 
98527e6fa99SDennis Zhou (Facebook) static inline struct blkcg *__bio_blkcg(struct bio *bio) { return NULL; }
986eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
987eea8f41cSTejun Heo 
988eea8f41cSTejun Heo static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
989eea8f41cSTejun Heo 						  struct blkcg_policy *pol) { return NULL; }
990eea8f41cSTejun Heo static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
991eea8f41cSTejun Heo static inline char *blkg_path(struct blkcg_gq *blkg) { return NULL; }
992eea8f41cSTejun Heo static inline void blkg_get(struct blkcg_gq *blkg) { }
993eea8f41cSTejun Heo static inline void blkg_put(struct blkcg_gq *blkg) { }
994eea8f41cSTejun Heo 
995eea8f41cSTejun Heo static inline struct request_list *blk_get_rl(struct request_queue *q,
996eea8f41cSTejun Heo 					      struct bio *bio) { return &q->root_rl; }
997eea8f41cSTejun Heo static inline void blk_put_rl(struct request_list *rl) { }
998eea8f41cSTejun Heo static inline void blk_rq_set_rl(struct request *rq, struct request_list *rl) { }
999eea8f41cSTejun Heo static inline struct request_list *blk_rq_rl(struct request *rq) { return &rq->q->root_rl; }
1000eea8f41cSTejun Heo 
10015bf9a1f3SDennis Zhou (Facebook) static inline void blkcg_bio_issue_init(struct bio *bio) { }
1002ae118896STejun Heo static inline bool blkcg_bio_issue_check(struct request_queue *q,
1003ae118896STejun Heo 					 struct bio *bio) { return true; }
1004ae118896STejun Heo 
1005eea8f41cSTejun Heo #define blk_queue_for_each_rl(rl, q)	\
1006eea8f41cSTejun Heo 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
1007eea8f41cSTejun Heo 
1008efa7d1c7STejun Heo #endif	/* CONFIG_BLOCK */
1009eea8f41cSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
1010eea8f41cSTejun Heo #endif	/* _BLK_CGROUP_H */
1011