xref: /linux/block/blk-cgroup.h (revision a4a508df2aa34f8650afde54ea804321c618f45f)
1672fdcf0SMing Lei /* SPDX-License-Identifier: GPL-2.0 */
2672fdcf0SMing Lei #ifndef _BLK_CGROUP_PRIVATE_H
3672fdcf0SMing Lei #define _BLK_CGROUP_PRIVATE_H
4672fdcf0SMing Lei /*
5672fdcf0SMing Lei  * block cgroup private header
6672fdcf0SMing Lei  *
7672fdcf0SMing Lei  * Based on ideas and code from CFQ, CFS and BFQ:
8672fdcf0SMing Lei  * Copyright (C) 2003 Jens Axboe <axboe@kernel.dk>
9672fdcf0SMing Lei  *
10672fdcf0SMing Lei  * Copyright (C) 2008 Fabio Checconi <fabio@gandalf.sssup.it>
11672fdcf0SMing Lei  *		      Paolo Valente <paolo.valente@unimore.it>
12672fdcf0SMing Lei  *
13672fdcf0SMing Lei  * Copyright (C) 2009 Vivek Goyal <vgoyal@redhat.com>
14672fdcf0SMing Lei  * 	              Nauman Rafique <nauman@google.com>
15672fdcf0SMing Lei  */
16672fdcf0SMing Lei 
17672fdcf0SMing Lei #include <linux/blk-cgroup.h>
18c97ab271SChristoph Hellwig #include <linux/cgroup.h>
19c97ab271SChristoph Hellwig #include <linux/kthread.h>
206b2b0459STejun Heo #include <linux/blk-mq.h>
213b8cc629SWaiman Long #include <linux/llist.h>
22c4e47bbbSJens Axboe #include "blk.h"
23672fdcf0SMing Lei 
24dec223c9SChristoph Hellwig struct blkcg_gq;
25dec223c9SChristoph Hellwig struct blkg_policy_data;
26dec223c9SChristoph Hellwig 
27dec223c9SChristoph Hellwig 
28672fdcf0SMing Lei /* percpu_counter batch for blkg_[rw]stats, per-cpu drift doesn't matter */
29672fdcf0SMing Lei #define BLKG_STAT_CPU_BATCH	(INT_MAX / 2)
30672fdcf0SMing Lei 
31672fdcf0SMing Lei #ifdef CONFIG_BLK_CGROUP
32bbb1ebe7SChristoph Hellwig 
33bbb1ebe7SChristoph Hellwig enum blkg_iostat_type {
34bbb1ebe7SChristoph Hellwig 	BLKG_IOSTAT_READ,
35bbb1ebe7SChristoph Hellwig 	BLKG_IOSTAT_WRITE,
36bbb1ebe7SChristoph Hellwig 	BLKG_IOSTAT_DISCARD,
37bbb1ebe7SChristoph Hellwig 
38bbb1ebe7SChristoph Hellwig 	BLKG_IOSTAT_NR,
39bbb1ebe7SChristoph Hellwig };
40bbb1ebe7SChristoph Hellwig 
41bbb1ebe7SChristoph Hellwig struct blkg_iostat {
42bbb1ebe7SChristoph Hellwig 	u64				bytes[BLKG_IOSTAT_NR];
43bbb1ebe7SChristoph Hellwig 	u64				ios[BLKG_IOSTAT_NR];
44bbb1ebe7SChristoph Hellwig };
45bbb1ebe7SChristoph Hellwig 
46bbb1ebe7SChristoph Hellwig struct blkg_iostat_set {
47bbb1ebe7SChristoph Hellwig 	struct u64_stats_sync		sync;
483b8cc629SWaiman Long 	struct blkcg_gq		       *blkg;
493b8cc629SWaiman Long 	struct llist_node		lnode;
503b8cc629SWaiman Long 	int				lqueued;	/* queued in llist */
51bbb1ebe7SChristoph Hellwig 	struct blkg_iostat		cur;
52bbb1ebe7SChristoph Hellwig 	struct blkg_iostat		last;
53bbb1ebe7SChristoph Hellwig };
54bbb1ebe7SChristoph Hellwig 
55bbb1ebe7SChristoph Hellwig /* association between a blk cgroup and a request queue */
56bbb1ebe7SChristoph Hellwig struct blkcg_gq {
57a06377c5SChristoph Hellwig 	/* Pointer to the associated request_queue */
58a06377c5SChristoph Hellwig 	struct request_queue		*q;
591231039dSChristoph Hellwig 	struct list_head		q_node;
60bbb1ebe7SChristoph Hellwig 	struct hlist_node		blkcg_node;
61bbb1ebe7SChristoph Hellwig 	struct blkcg			*blkcg;
62bbb1ebe7SChristoph Hellwig 
63bbb1ebe7SChristoph Hellwig 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
64bbb1ebe7SChristoph Hellwig 	struct blkcg_gq			*parent;
65bbb1ebe7SChristoph Hellwig 
66bbb1ebe7SChristoph Hellwig 	/* reference count */
67bbb1ebe7SChristoph Hellwig 	struct percpu_ref		refcnt;
68bbb1ebe7SChristoph Hellwig 
69bbb1ebe7SChristoph Hellwig 	/* is this blkg online? protected by both blkcg and q locks */
70bbb1ebe7SChristoph Hellwig 	bool				online;
71bbb1ebe7SChristoph Hellwig 
72bbb1ebe7SChristoph Hellwig 	struct blkg_iostat_set __percpu	*iostat_cpu;
73bbb1ebe7SChristoph Hellwig 	struct blkg_iostat_set		iostat;
74bbb1ebe7SChristoph Hellwig 
75bbb1ebe7SChristoph Hellwig 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
762c275afeSChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_PUNT_BIO
77bbb1ebe7SChristoph Hellwig 	spinlock_t			async_bio_lock;
78bbb1ebe7SChristoph Hellwig 	struct bio_list			async_bios;
792c275afeSChristoph Hellwig #endif
80bbb1ebe7SChristoph Hellwig 	union {
81bbb1ebe7SChristoph Hellwig 		struct work_struct	async_bio_work;
82bbb1ebe7SChristoph Hellwig 		struct work_struct	free_work;
83bbb1ebe7SChristoph Hellwig 	};
84bbb1ebe7SChristoph Hellwig 
85bbb1ebe7SChristoph Hellwig 	atomic_t			use_delay;
86bbb1ebe7SChristoph Hellwig 	atomic64_t			delay_nsec;
87bbb1ebe7SChristoph Hellwig 	atomic64_t			delay_start;
88bbb1ebe7SChristoph Hellwig 	u64				last_delay;
89bbb1ebe7SChristoph Hellwig 	int				last_use;
90bbb1ebe7SChristoph Hellwig 
91bbb1ebe7SChristoph Hellwig 	struct rcu_head			rcu_head;
92bbb1ebe7SChristoph Hellwig };
93bbb1ebe7SChristoph Hellwig 
94dec223c9SChristoph Hellwig struct blkcg {
95dec223c9SChristoph Hellwig 	struct cgroup_subsys_state	css;
96dec223c9SChristoph Hellwig 	spinlock_t			lock;
97dec223c9SChristoph Hellwig 	refcount_t			online_pin;
9889ed6c9aSXiu Jianfeng 	/* If there is block congestion on this cgroup. */
9989ed6c9aSXiu Jianfeng 	atomic_t			congestion_count;
100dec223c9SChristoph Hellwig 
101dec223c9SChristoph Hellwig 	struct radix_tree_root		blkg_tree;
102dec223c9SChristoph Hellwig 	struct blkcg_gq	__rcu		*blkg_hint;
103dec223c9SChristoph Hellwig 	struct hlist_head		blkg_list;
104dec223c9SChristoph Hellwig 
105dec223c9SChristoph Hellwig 	struct blkcg_policy_data	*cpd[BLKCG_MAX_POLS];
106dec223c9SChristoph Hellwig 
107dec223c9SChristoph Hellwig 	struct list_head		all_blkcgs_node;
1083b8cc629SWaiman Long 
1093b8cc629SWaiman Long 	/*
1103b8cc629SWaiman Long 	 * List of updated percpu blkg_iostat_set's since the last flush.
1113b8cc629SWaiman Long 	 */
1123b8cc629SWaiman Long 	struct llist_head __percpu	*lhead;
1133b8cc629SWaiman Long 
114dec223c9SChristoph Hellwig #ifdef CONFIG_BLK_CGROUP_FC_APPID
115dec223c9SChristoph Hellwig 	char                            fc_app_id[FC_APPID_LEN];
116dec223c9SChristoph Hellwig #endif
117dec223c9SChristoph Hellwig #ifdef CONFIG_CGROUP_WRITEBACK
118dec223c9SChristoph Hellwig 	struct list_head		cgwb_list;
119dec223c9SChristoph Hellwig #endif
120dec223c9SChristoph Hellwig };
121dec223c9SChristoph Hellwig 
css_to_blkcg(struct cgroup_subsys_state * css)122dec223c9SChristoph Hellwig static inline struct blkcg *css_to_blkcg(struct cgroup_subsys_state *css)
123dec223c9SChristoph Hellwig {
124dec223c9SChristoph Hellwig 	return css ? container_of(css, struct blkcg, css) : NULL;
125dec223c9SChristoph Hellwig }
126672fdcf0SMing Lei 
127672fdcf0SMing Lei /*
128672fdcf0SMing Lei  * A blkcg_gq (blkg) is association between a block cgroup (blkcg) and a
129672fdcf0SMing Lei  * request_queue (q).  This is used by blkcg policies which need to track
130672fdcf0SMing Lei  * information per blkcg - q pair.
131672fdcf0SMing Lei  *
132672fdcf0SMing Lei  * There can be multiple active blkcg policies and each blkg:policy pair is
133672fdcf0SMing Lei  * represented by a blkg_policy_data which is allocated and freed by each
134672fdcf0SMing Lei  * policy's pd_alloc/free_fn() methods.  A policy can allocate private data
135672fdcf0SMing Lei  * area by allocating larger data structure which embeds blkg_policy_data
136672fdcf0SMing Lei  * at the beginning.
137672fdcf0SMing Lei  */
138672fdcf0SMing Lei struct blkg_policy_data {
139672fdcf0SMing Lei 	/* the blkg and policy id this per-policy data belongs to */
140672fdcf0SMing Lei 	struct blkcg_gq			*blkg;
141672fdcf0SMing Lei 	int				plid;
142dfd6200aSYu Kuai 	bool				online;
143672fdcf0SMing Lei };
144672fdcf0SMing Lei 
145672fdcf0SMing Lei /*
146672fdcf0SMing Lei  * Policies that need to keep per-blkcg data which is independent from any
147672fdcf0SMing Lei  * request_queue associated to it should implement cpd_alloc/free_fn()
148672fdcf0SMing Lei  * methods.  A policy can allocate private data area by allocating larger
149672fdcf0SMing Lei  * data structure which embeds blkcg_policy_data at the beginning.
150672fdcf0SMing Lei  * cpd_init() is invoked to let each policy handle per-blkcg data.
151672fdcf0SMing Lei  */
152672fdcf0SMing Lei struct blkcg_policy_data {
153672fdcf0SMing Lei 	/* the blkcg and policy id this per-policy data belongs to */
154672fdcf0SMing Lei 	struct blkcg			*blkcg;
155672fdcf0SMing Lei 	int				plid;
156672fdcf0SMing Lei };
157672fdcf0SMing Lei 
158672fdcf0SMing Lei typedef struct blkcg_policy_data *(blkcg_pol_alloc_cpd_fn)(gfp_t gfp);
159672fdcf0SMing Lei typedef void (blkcg_pol_init_cpd_fn)(struct blkcg_policy_data *cpd);
160672fdcf0SMing Lei typedef void (blkcg_pol_free_cpd_fn)(struct blkcg_policy_data *cpd);
161672fdcf0SMing Lei typedef void (blkcg_pol_bind_cpd_fn)(struct blkcg_policy_data *cpd);
1620a0b4f79SChristoph Hellwig typedef struct blkg_policy_data *(blkcg_pol_alloc_pd_fn)(struct gendisk *disk,
1630a0b4f79SChristoph Hellwig 		struct blkcg *blkcg, gfp_t gfp);
164672fdcf0SMing Lei typedef void (blkcg_pol_init_pd_fn)(struct blkg_policy_data *pd);
165672fdcf0SMing Lei typedef void (blkcg_pol_online_pd_fn)(struct blkg_policy_data *pd);
166672fdcf0SMing Lei typedef void (blkcg_pol_offline_pd_fn)(struct blkg_policy_data *pd);
167672fdcf0SMing Lei typedef void (blkcg_pol_free_pd_fn)(struct blkg_policy_data *pd);
168672fdcf0SMing Lei typedef void (blkcg_pol_reset_pd_stats_fn)(struct blkg_policy_data *pd);
1693607849dSWolfgang Bumiller typedef void (blkcg_pol_stat_pd_fn)(struct blkg_policy_data *pd,
170672fdcf0SMing Lei 				struct seq_file *s);
171672fdcf0SMing Lei 
172672fdcf0SMing Lei struct blkcg_policy {
173672fdcf0SMing Lei 	int				plid;
174672fdcf0SMing Lei 	/* cgroup files for the policy */
175672fdcf0SMing Lei 	struct cftype			*dfl_cftypes;
176672fdcf0SMing Lei 	struct cftype			*legacy_cftypes;
177672fdcf0SMing Lei 
178672fdcf0SMing Lei 	/* operations */
179672fdcf0SMing Lei 	blkcg_pol_alloc_cpd_fn		*cpd_alloc_fn;
180672fdcf0SMing Lei 	blkcg_pol_free_cpd_fn		*cpd_free_fn;
181672fdcf0SMing Lei 
182672fdcf0SMing Lei 	blkcg_pol_alloc_pd_fn		*pd_alloc_fn;
183672fdcf0SMing Lei 	blkcg_pol_init_pd_fn		*pd_init_fn;
184672fdcf0SMing Lei 	blkcg_pol_online_pd_fn		*pd_online_fn;
185672fdcf0SMing Lei 	blkcg_pol_offline_pd_fn		*pd_offline_fn;
186672fdcf0SMing Lei 	blkcg_pol_free_pd_fn		*pd_free_fn;
187672fdcf0SMing Lei 	blkcg_pol_reset_pd_stats_fn	*pd_reset_stats_fn;
188672fdcf0SMing Lei 	blkcg_pol_stat_pd_fn		*pd_stat_fn;
189672fdcf0SMing Lei };
190672fdcf0SMing Lei 
191672fdcf0SMing Lei extern struct blkcg blkcg_root;
192672fdcf0SMing Lei extern bool blkcg_debug_stats;
193672fdcf0SMing Lei 
1948b8ace08SMing Lei void blkg_init_queue(struct request_queue *q);
1959823538fSChristoph Hellwig int blkcg_init_disk(struct gendisk *disk);
1969823538fSChristoph Hellwig void blkcg_exit_disk(struct gendisk *disk);
197672fdcf0SMing Lei 
198672fdcf0SMing Lei /* Blkio controller policy registration */
199672fdcf0SMing Lei int blkcg_policy_register(struct blkcg_policy *pol);
200672fdcf0SMing Lei void blkcg_policy_unregister(struct blkcg_policy *pol);
20140e4996eSChristoph Hellwig int blkcg_activate_policy(struct gendisk *disk, const struct blkcg_policy *pol);
20240e4996eSChristoph Hellwig void blkcg_deactivate_policy(struct gendisk *disk,
203672fdcf0SMing Lei 			     const struct blkcg_policy *pol);
204672fdcf0SMing Lei 
205672fdcf0SMing Lei const char *blkg_dev_name(struct blkcg_gq *blkg);
206672fdcf0SMing Lei void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
207672fdcf0SMing Lei 		       u64 (*prfill)(struct seq_file *,
208672fdcf0SMing Lei 				     struct blkg_policy_data *, int),
209672fdcf0SMing Lei 		       const struct blkcg_policy *pol, int data,
210672fdcf0SMing Lei 		       bool show_total);
211672fdcf0SMing Lei u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
212672fdcf0SMing Lei 
213672fdcf0SMing Lei struct blkg_conf_ctx {
214faffaab2STejun Heo 	char				*input;
215faffaab2STejun Heo 	char				*body;
216672fdcf0SMing Lei 	struct block_device		*bdev;
217672fdcf0SMing Lei 	struct blkcg_gq			*blkg;
218672fdcf0SMing Lei };
219672fdcf0SMing Lei 
220faffaab2STejun Heo void blkg_conf_init(struct blkg_conf_ctx *ctx, char *input);
221faffaab2STejun Heo int blkg_conf_open_bdev(struct blkg_conf_ctx *ctx);
2229730763fSNilay Shroff unsigned long blkg_conf_open_bdev_frozen(struct blkg_conf_ctx *ctx);
223672fdcf0SMing Lei int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
224faffaab2STejun Heo 		   struct blkg_conf_ctx *ctx);
225faffaab2STejun Heo void blkg_conf_exit(struct blkg_conf_ctx *ctx);
2269730763fSNilay Shroff void blkg_conf_exit_frozen(struct blkg_conf_ctx *ctx, unsigned long memflags);
227672fdcf0SMing Lei 
228672fdcf0SMing Lei /**
229672fdcf0SMing Lei  * bio_issue_as_root_blkg - see if this bio needs to be issued as root blkg
2304fa5c370SRandy Dunlap  * @bio: the target &bio
2314fa5c370SRandy Dunlap  *
2324fa5c370SRandy Dunlap  * Return: true if this bio needs to be submitted with the root blkg context.
233672fdcf0SMing Lei  *
234672fdcf0SMing Lei  * In order to avoid priority inversions we sometimes need to issue a bio as if
235672fdcf0SMing Lei  * it were attached to the root blkg, and then backcharge to the actual owning
236bbb1ebe7SChristoph Hellwig  * blkg.  The idea is we do bio_blkcg_css() to look up the actual context for
237bbb1ebe7SChristoph Hellwig  * the bio and attach the appropriate blkg to the bio.  Then we call this helper
238bbb1ebe7SChristoph Hellwig  * and if it is true run with the root blkg for that queue and then do any
239672fdcf0SMing Lei  * backcharging to the originating cgroup once the io is complete.
240672fdcf0SMing Lei  */
bio_issue_as_root_blkg(struct bio * bio)241672fdcf0SMing Lei static inline bool bio_issue_as_root_blkg(struct bio *bio)
242672fdcf0SMing Lei {
243672fdcf0SMing Lei 	return (bio->bi_opf & (REQ_META | REQ_SWAP)) != 0;
244672fdcf0SMing Lei }
245672fdcf0SMing Lei 
246672fdcf0SMing Lei /**
2479a9c261eSChristoph Hellwig  * blkg_lookup - lookup blkg for the specified blkcg - q pair
248672fdcf0SMing Lei  * @blkcg: blkcg of interest
2499a9c261eSChristoph Hellwig  * @q: request_queue of interest
250672fdcf0SMing Lei  *
2519a9c261eSChristoph Hellwig  * Lookup blkg for the @blkcg - @q pair.
2524fa5c370SRandy Dunlap  *
2534a69f325SChristoph Hellwig  * Must be called in a RCU critical section.
254672fdcf0SMing Lei  */
blkg_lookup(struct blkcg * blkcg,struct request_queue * q)2554a69f325SChristoph Hellwig static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg,
2569a9c261eSChristoph Hellwig 					   struct request_queue *q)
257672fdcf0SMing Lei {
258672fdcf0SMing Lei 	struct blkcg_gq *blkg;
259672fdcf0SMing Lei 
260672fdcf0SMing Lei 	if (blkcg == &blkcg_root)
2619a9c261eSChristoph Hellwig 		return q->root_blkg;
262672fdcf0SMing Lei 
263393cd8ffSMing Lei 	blkg = rcu_dereference_check(blkcg->blkg_hint,
264393cd8ffSMing Lei 			lockdep_is_held(&q->queue_lock));
265a06377c5SChristoph Hellwig 	if (blkg && blkg->q == q)
266672fdcf0SMing Lei 		return blkg;
267672fdcf0SMing Lei 
2689a9c261eSChristoph Hellwig 	blkg = radix_tree_lookup(&blkcg->blkg_tree, q->id);
269a06377c5SChristoph Hellwig 	if (blkg && blkg->q != q)
2704a69f325SChristoph Hellwig 		blkg = NULL;
2714a69f325SChristoph Hellwig 	return blkg;
272672fdcf0SMing Lei }
273672fdcf0SMing Lei 
274672fdcf0SMing Lei /**
2754fa5c370SRandy Dunlap  * blkg_to_pd - get policy private data
276672fdcf0SMing Lei  * @blkg: blkg of interest
277672fdcf0SMing Lei  * @pol: policy of interest
278672fdcf0SMing Lei  *
279672fdcf0SMing Lei  * Return pointer to private data associated with the @blkg-@pol pair.
280672fdcf0SMing Lei  */
blkg_to_pd(struct blkcg_gq * blkg,struct blkcg_policy * pol)281672fdcf0SMing Lei static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
282672fdcf0SMing Lei 						  struct blkcg_policy *pol)
283672fdcf0SMing Lei {
284672fdcf0SMing Lei 	return blkg ? blkg->pd[pol->plid] : NULL;
285672fdcf0SMing Lei }
286672fdcf0SMing Lei 
blkcg_to_cpd(struct blkcg * blkcg,struct blkcg_policy * pol)287672fdcf0SMing Lei static inline struct blkcg_policy_data *blkcg_to_cpd(struct blkcg *blkcg,
288672fdcf0SMing Lei 						     struct blkcg_policy *pol)
289672fdcf0SMing Lei {
290672fdcf0SMing Lei 	return blkcg ? blkcg->cpd[pol->plid] : NULL;
291672fdcf0SMing Lei }
292672fdcf0SMing Lei 
293672fdcf0SMing Lei /**
2944fa5c370SRandy Dunlap  * pd_to_blkg - get blkg associated with policy private data
295672fdcf0SMing Lei  * @pd: policy private data of interest
296672fdcf0SMing Lei  *
297672fdcf0SMing Lei  * @pd is policy private data.  Determine the blkg it's associated with.
298672fdcf0SMing Lei  */
pd_to_blkg(struct blkg_policy_data * pd)299672fdcf0SMing Lei static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd)
300672fdcf0SMing Lei {
301672fdcf0SMing Lei 	return pd ? pd->blkg : NULL;
302672fdcf0SMing Lei }
303672fdcf0SMing Lei 
cpd_to_blkcg(struct blkcg_policy_data * cpd)304672fdcf0SMing Lei static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
305672fdcf0SMing Lei {
306672fdcf0SMing Lei 	return cpd ? cpd->blkcg : NULL;
307672fdcf0SMing Lei }
308672fdcf0SMing Lei 
309672fdcf0SMing Lei /**
310672fdcf0SMing Lei  * blkg_get - get a blkg reference
311672fdcf0SMing Lei  * @blkg: blkg to get
312672fdcf0SMing Lei  *
313672fdcf0SMing Lei  * The caller should be holding an existing reference.
314672fdcf0SMing Lei  */
blkg_get(struct blkcg_gq * blkg)315672fdcf0SMing Lei static inline void blkg_get(struct blkcg_gq *blkg)
316672fdcf0SMing Lei {
317672fdcf0SMing Lei 	percpu_ref_get(&blkg->refcnt);
318672fdcf0SMing Lei }
319672fdcf0SMing Lei 
320672fdcf0SMing Lei /**
321672fdcf0SMing Lei  * blkg_tryget - try and get a blkg reference
322672fdcf0SMing Lei  * @blkg: blkg to get
323672fdcf0SMing Lei  *
324672fdcf0SMing Lei  * This is for use when doing an RCU lookup of the blkg.  We may be in the midst
325672fdcf0SMing Lei  * of freeing this blkg, so we can only use it if the refcnt is not zero.
326672fdcf0SMing Lei  */
blkg_tryget(struct blkcg_gq * blkg)327672fdcf0SMing Lei static inline bool blkg_tryget(struct blkcg_gq *blkg)
328672fdcf0SMing Lei {
329672fdcf0SMing Lei 	return blkg && percpu_ref_tryget(&blkg->refcnt);
330672fdcf0SMing Lei }
331672fdcf0SMing Lei 
332672fdcf0SMing Lei /**
333672fdcf0SMing Lei  * blkg_put - put a blkg reference
334672fdcf0SMing Lei  * @blkg: blkg to put
335672fdcf0SMing Lei  */
blkg_put(struct blkcg_gq * blkg)336672fdcf0SMing Lei static inline void blkg_put(struct blkcg_gq *blkg)
337672fdcf0SMing Lei {
338672fdcf0SMing Lei 	percpu_ref_put(&blkg->refcnt);
339672fdcf0SMing Lei }
340672fdcf0SMing Lei 
341672fdcf0SMing Lei /**
342672fdcf0SMing Lei  * blkg_for_each_descendant_pre - pre-order walk of a blkg's descendants
343672fdcf0SMing Lei  * @d_blkg: loop cursor pointing to the current descendant
344672fdcf0SMing Lei  * @pos_css: used for iteration
345672fdcf0SMing Lei  * @p_blkg: target blkg to walk descendants of
346672fdcf0SMing Lei  *
347672fdcf0SMing Lei  * Walk @c_blkg through the descendants of @p_blkg.  Must be used with RCU
348672fdcf0SMing Lei  * read locked.  If called under either blkcg or queue lock, the iteration
349672fdcf0SMing Lei  * is guaranteed to include all and only online blkgs.  The caller may
350672fdcf0SMing Lei  * update @pos_css by calling css_rightmost_descendant() to skip subtree.
351672fdcf0SMing Lei  * @p_blkg is included in the iteration and the first node to be visited.
352672fdcf0SMing Lei  */
353672fdcf0SMing Lei #define blkg_for_each_descendant_pre(d_blkg, pos_css, p_blkg)		\
354672fdcf0SMing Lei 	css_for_each_descendant_pre((pos_css), &(p_blkg)->blkcg->css)	\
35579fcc5beSChristoph Hellwig 		if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css),	\
356a06377c5SChristoph Hellwig 					    (p_blkg)->q)))
357672fdcf0SMing Lei 
358672fdcf0SMing Lei /**
359672fdcf0SMing Lei  * blkg_for_each_descendant_post - post-order walk of a blkg's descendants
360672fdcf0SMing Lei  * @d_blkg: loop cursor pointing to the current descendant
361672fdcf0SMing Lei  * @pos_css: used for iteration
362672fdcf0SMing Lei  * @p_blkg: target blkg to walk descendants of
363672fdcf0SMing Lei  *
364672fdcf0SMing Lei  * Similar to blkg_for_each_descendant_pre() but performs post-order
365672fdcf0SMing Lei  * traversal instead.  Synchronization rules are the same.  @p_blkg is
366672fdcf0SMing Lei  * included in the iteration and the last node to be visited.
367672fdcf0SMing Lei  */
368672fdcf0SMing Lei #define blkg_for_each_descendant_post(d_blkg, pos_css, p_blkg)		\
369672fdcf0SMing Lei 	css_for_each_descendant_post((pos_css), &(p_blkg)->blkcg->css)	\
37079fcc5beSChristoph Hellwig 		if (((d_blkg) = blkg_lookup(css_to_blkcg(pos_css),	\
371a06377c5SChristoph Hellwig 					    (p_blkg)->q)))
372672fdcf0SMing Lei 
blkcg_use_delay(struct blkcg_gq * blkg)373672fdcf0SMing Lei static inline void blkcg_use_delay(struct blkcg_gq *blkg)
374672fdcf0SMing Lei {
375672fdcf0SMing Lei 	if (WARN_ON_ONCE(atomic_read(&blkg->use_delay) < 0))
376672fdcf0SMing Lei 		return;
377672fdcf0SMing Lei 	if (atomic_add_return(1, &blkg->use_delay) == 1)
37889ed6c9aSXiu Jianfeng 		atomic_inc(&blkg->blkcg->congestion_count);
379672fdcf0SMing Lei }
380672fdcf0SMing Lei 
blkcg_unuse_delay(struct blkcg_gq * blkg)381672fdcf0SMing Lei static inline int blkcg_unuse_delay(struct blkcg_gq *blkg)
382672fdcf0SMing Lei {
383672fdcf0SMing Lei 	int old = atomic_read(&blkg->use_delay);
384672fdcf0SMing Lei 
385672fdcf0SMing Lei 	if (WARN_ON_ONCE(old < 0))
386672fdcf0SMing Lei 		return 0;
387672fdcf0SMing Lei 	if (old == 0)
388672fdcf0SMing Lei 		return 0;
389672fdcf0SMing Lei 
390672fdcf0SMing Lei 	/*
391672fdcf0SMing Lei 	 * We do this song and dance because we can race with somebody else
392672fdcf0SMing Lei 	 * adding or removing delay.  If we just did an atomic_dec we'd end up
393672fdcf0SMing Lei 	 * negative and we'd already be in trouble.  We need to subtract 1 and
394672fdcf0SMing Lei 	 * then check to see if we were the last delay so we can drop the
395672fdcf0SMing Lei 	 * congestion count on the cgroup.
396672fdcf0SMing Lei 	 */
39796388f57SUros Bizjak 	while (old && !atomic_try_cmpxchg(&blkg->use_delay, &old, old - 1))
39896388f57SUros Bizjak 		;
399672fdcf0SMing Lei 
400672fdcf0SMing Lei 	if (old == 0)
401672fdcf0SMing Lei 		return 0;
402672fdcf0SMing Lei 	if (old == 1)
40389ed6c9aSXiu Jianfeng 		atomic_dec(&blkg->blkcg->congestion_count);
404672fdcf0SMing Lei 	return 1;
405672fdcf0SMing Lei }
406672fdcf0SMing Lei 
407672fdcf0SMing Lei /**
408672fdcf0SMing Lei  * blkcg_set_delay - Enable allocator delay mechanism with the specified delay amount
409672fdcf0SMing Lei  * @blkg: target blkg
410672fdcf0SMing Lei  * @delay: delay duration in nsecs
411672fdcf0SMing Lei  *
412672fdcf0SMing Lei  * When enabled with this function, the delay is not decayed and must be
413672fdcf0SMing Lei  * explicitly cleared with blkcg_clear_delay(). Must not be mixed with
414672fdcf0SMing Lei  * blkcg_[un]use_delay() and blkcg_add_delay() usages.
415672fdcf0SMing Lei  */
blkcg_set_delay(struct blkcg_gq * blkg,u64 delay)416672fdcf0SMing Lei static inline void blkcg_set_delay(struct blkcg_gq *blkg, u64 delay)
417672fdcf0SMing Lei {
418672fdcf0SMing Lei 	int old = atomic_read(&blkg->use_delay);
419672fdcf0SMing Lei 
420672fdcf0SMing Lei 	/* We only want 1 person setting the congestion count for this blkg. */
42196388f57SUros Bizjak 	if (!old && atomic_try_cmpxchg(&blkg->use_delay, &old, -1))
42289ed6c9aSXiu Jianfeng 		atomic_inc(&blkg->blkcg->congestion_count);
423672fdcf0SMing Lei 
424672fdcf0SMing Lei 	atomic64_set(&blkg->delay_nsec, delay);
425672fdcf0SMing Lei }
426672fdcf0SMing Lei 
427672fdcf0SMing Lei /**
428672fdcf0SMing Lei  * blkcg_clear_delay - Disable allocator delay mechanism
429672fdcf0SMing Lei  * @blkg: target blkg
430672fdcf0SMing Lei  *
431672fdcf0SMing Lei  * Disable use_delay mechanism. See blkcg_set_delay().
432672fdcf0SMing Lei  */
blkcg_clear_delay(struct blkcg_gq * blkg)433672fdcf0SMing Lei static inline void blkcg_clear_delay(struct blkcg_gq *blkg)
434672fdcf0SMing Lei {
435672fdcf0SMing Lei 	int old = atomic_read(&blkg->use_delay);
436672fdcf0SMing Lei 
437672fdcf0SMing Lei 	/* We only want 1 person clearing the congestion count for this blkg. */
43896388f57SUros Bizjak 	if (old && atomic_try_cmpxchg(&blkg->use_delay, &old, 0))
43989ed6c9aSXiu Jianfeng 		atomic_dec(&blkg->blkcg->congestion_count);
440672fdcf0SMing Lei }
441672fdcf0SMing Lei 
4426b2b0459STejun Heo /**
4436b2b0459STejun Heo  * blk_cgroup_mergeable - Determine whether to allow or disallow merges
4446b2b0459STejun Heo  * @rq: request to merge into
4456b2b0459STejun Heo  * @bio: bio to merge
4466b2b0459STejun Heo  *
4476b2b0459STejun Heo  * @bio and @rq should belong to the same cgroup and their issue_as_root should
4486b2b0459STejun Heo  * match. The latter is necessary as we don't want to throttle e.g. a metadata
4496b2b0459STejun Heo  * update because it happens to be next to a regular IO.
4506b2b0459STejun Heo  */
blk_cgroup_mergeable(struct request * rq,struct bio * bio)4516b2b0459STejun Heo static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio)
4526b2b0459STejun Heo {
4536b2b0459STejun Heo 	return rq->bio->bi_blkg == bio->bi_blkg &&
4546b2b0459STejun Heo 		bio_issue_as_root_blkg(rq->bio) == bio_issue_as_root_blkg(bio);
4556b2b0459STejun Heo }
4566b2b0459STejun Heo 
blkcg_policy_enabled(struct request_queue * q,const struct blkcg_policy * pol)457*bd9fd5beSHan Guangjiang static inline bool blkcg_policy_enabled(struct request_queue *q,
458*bd9fd5beSHan Guangjiang 				const struct blkcg_policy *pol)
459*bd9fd5beSHan Guangjiang {
460*bd9fd5beSHan Guangjiang 	return pol && test_bit(pol->plid, q->blkcg_pols);
461*bd9fd5beSHan Guangjiang }
462*bd9fd5beSHan Guangjiang 
463672fdcf0SMing Lei void blk_cgroup_bio_start(struct bio *bio);
464672fdcf0SMing Lei void blkcg_add_delay(struct blkcg_gq *blkg, u64 now, u64 delta);
465672fdcf0SMing Lei #else	/* CONFIG_BLK_CGROUP */
466672fdcf0SMing Lei 
467672fdcf0SMing Lei struct blkg_policy_data {
468672fdcf0SMing Lei };
469672fdcf0SMing Lei 
470672fdcf0SMing Lei struct blkcg_policy_data {
471672fdcf0SMing Lei };
472672fdcf0SMing Lei 
473672fdcf0SMing Lei struct blkcg_policy {
474672fdcf0SMing Lei };
475672fdcf0SMing Lei 
476bbb1ebe7SChristoph Hellwig struct blkcg {
477bbb1ebe7SChristoph Hellwig };
478bbb1ebe7SChristoph Hellwig 
blkg_lookup(struct blkcg * blkcg,void * key)479672fdcf0SMing Lei static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }
blkg_init_queue(struct request_queue * q)4808b8ace08SMing Lei static inline void blkg_init_queue(struct request_queue *q) { }
blkcg_init_disk(struct gendisk * disk)4819823538fSChristoph Hellwig static inline int blkcg_init_disk(struct gendisk *disk) { return 0; }
blkcg_exit_disk(struct gendisk * disk)4829823538fSChristoph Hellwig static inline void blkcg_exit_disk(struct gendisk *disk) { }
blkcg_policy_register(struct blkcg_policy * pol)483672fdcf0SMing Lei static inline int blkcg_policy_register(struct blkcg_policy *pol) { return 0; }
blkcg_policy_unregister(struct blkcg_policy * pol)484672fdcf0SMing Lei static inline void blkcg_policy_unregister(struct blkcg_policy *pol) { }
blkcg_activate_policy(struct gendisk * disk,const struct blkcg_policy * pol)48540e4996eSChristoph Hellwig static inline int blkcg_activate_policy(struct gendisk *disk,
486672fdcf0SMing Lei 					const struct blkcg_policy *pol) { return 0; }
blkcg_deactivate_policy(struct gendisk * disk,const struct blkcg_policy * pol)48740e4996eSChristoph Hellwig static inline void blkcg_deactivate_policy(struct gendisk *disk,
488672fdcf0SMing Lei 					   const struct blkcg_policy *pol) { }
489672fdcf0SMing Lei 
blkg_to_pd(struct blkcg_gq * blkg,struct blkcg_policy * pol)490672fdcf0SMing Lei static inline struct blkg_policy_data *blkg_to_pd(struct blkcg_gq *blkg,
491672fdcf0SMing Lei 						  struct blkcg_policy *pol) { return NULL; }
pd_to_blkg(struct blkg_policy_data * pd)492672fdcf0SMing Lei static inline struct blkcg_gq *pd_to_blkg(struct blkg_policy_data *pd) { return NULL; }
blkg_get(struct blkcg_gq * blkg)493672fdcf0SMing Lei static inline void blkg_get(struct blkcg_gq *blkg) { }
blkg_put(struct blkcg_gq * blkg)494672fdcf0SMing Lei static inline void blkg_put(struct blkcg_gq *blkg) { }
blk_cgroup_bio_start(struct bio * bio)495672fdcf0SMing Lei static inline void blk_cgroup_bio_start(struct bio *bio) { }
blk_cgroup_mergeable(struct request * rq,struct bio * bio)4966b2b0459STejun Heo static inline bool blk_cgroup_mergeable(struct request *rq, struct bio *bio) { return true; }
497672fdcf0SMing Lei 
498672fdcf0SMing Lei #define blk_queue_for_each_rl(rl, q)	\
499672fdcf0SMing Lei 	for ((rl) = &(q)->root_rl; (rl); (rl) = NULL)
500672fdcf0SMing Lei 
501672fdcf0SMing Lei #endif	/* CONFIG_BLK_CGROUP */
502672fdcf0SMing Lei 
503672fdcf0SMing Lei #endif /* _BLK_CGROUP_PRIVATE_H */
504