xref: /linux-6.15/include/linux/blk-cgroup.h (revision dec223c9)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2eea8f41cSTejun Heo #ifndef _BLK_CGROUP_H
3eea8f41cSTejun Heo #define _BLK_CGROUP_H
4eea8f41cSTejun Heo /*
5eea8f41cSTejun Heo  * Common Block IO controller cgroup interface
6eea8f41cSTejun Heo  *
7eea8f41cSTejun Heo  * Based on ideas and code from CFQ, CFS and BFQ:
8eea8f41cSTejun Heo  * Copyright (C) 2003 Jens Axboe <[email protected]>
9eea8f41cSTejun Heo  *
10eea8f41cSTejun Heo  * Copyright (C) 2008 Fabio Checconi <[email protected]>
11eea8f41cSTejun Heo  *		      Paolo Valente <[email protected]>
12eea8f41cSTejun Heo  *
13eea8f41cSTejun Heo  * Copyright (C) 2009 Vivek Goyal <[email protected]>
14eea8f41cSTejun Heo  * 	              Nauman Rafique <[email protected]>
15eea8f41cSTejun Heo  */
16eea8f41cSTejun Heo 
17eea8f41cSTejun Heo #include <linux/cgroup.h>
18f7331648STejun Heo #include <linux/percpu.h>
1924bdb8efSTejun Heo #include <linux/percpu_counter.h>
20f7331648STejun Heo #include <linux/u64_stats_sync.h>
21eea8f41cSTejun Heo #include <linux/seq_file.h>
22eea8f41cSTejun Heo #include <linux/radix-tree.h>
23eea8f41cSTejun Heo #include <linux/blkdev.h>
24eea8f41cSTejun Heo #include <linux/atomic.h>
25902ec5b6SShaohua Li #include <linux/kthread.h>
265cdf2e3fSDennis Zhou #include <linux/fs.h>
27eea8f41cSTejun Heo 
28d2bcbeabSMuneendra Kumar #define FC_APPID_LEN              129
29d2bcbeabSMuneendra Kumar 
30eea8f41cSTejun Heo #ifdef CONFIG_BLK_CGROUP
31eea8f41cSTejun Heo 
32f7331648STejun Heo enum blkg_iostat_type {
33f7331648STejun Heo 	BLKG_IOSTAT_READ,
34f7331648STejun Heo 	BLKG_IOSTAT_WRITE,
35f7331648STejun Heo 	BLKG_IOSTAT_DISCARD,
36f7331648STejun Heo 
37f7331648STejun Heo 	BLKG_IOSTAT_NR,
38f7331648STejun Heo };
39f7331648STejun Heo 
40f7331648STejun Heo struct blkg_iostat {
41f7331648STejun Heo 	u64				bytes[BLKG_IOSTAT_NR];
42f7331648STejun Heo 	u64				ios[BLKG_IOSTAT_NR];
43f7331648STejun Heo };
44f7331648STejun Heo 
45f7331648STejun Heo struct blkg_iostat_set {
46f7331648STejun Heo 	struct u64_stats_sync		sync;
47f7331648STejun Heo 	struct blkg_iostat		cur;
48f7331648STejun Heo 	struct blkg_iostat		last;
49f7331648STejun Heo };
50f7331648STejun Heo 
51eea8f41cSTejun Heo /* association between a blk cgroup and a request queue */
52eea8f41cSTejun Heo struct blkcg_gq {
53eea8f41cSTejun Heo 	/* Pointer to the associated request_queue */
54eea8f41cSTejun Heo 	struct request_queue		*q;
55eea8f41cSTejun Heo 	struct list_head		q_node;
56eea8f41cSTejun Heo 	struct hlist_node		blkcg_node;
57eea8f41cSTejun Heo 	struct blkcg			*blkcg;
58eea8f41cSTejun Heo 
59eea8f41cSTejun Heo 	/* all non-root blkcg_gq's are guaranteed to have access to parent */
60eea8f41cSTejun Heo 	struct blkcg_gq			*parent;
61eea8f41cSTejun Heo 
62eea8f41cSTejun Heo 	/* reference count */
637fcf2b03SDennis Zhou 	struct percpu_ref		refcnt;
64eea8f41cSTejun Heo 
65eea8f41cSTejun Heo 	/* is this blkg online? protected by both blkcg and q locks */
66eea8f41cSTejun Heo 	bool				online;
67eea8f41cSTejun Heo 
68f7331648STejun Heo 	struct blkg_iostat_set __percpu	*iostat_cpu;
69f7331648STejun Heo 	struct blkg_iostat_set		iostat;
7077ea7338STejun Heo 
71eea8f41cSTejun Heo 	struct blkg_policy_data		*pd[BLKCG_MAX_POLS];
72eea8f41cSTejun Heo 
73d3f77dfdSTejun Heo 	spinlock_t			async_bio_lock;
74d3f77dfdSTejun Heo 	struct bio_list			async_bios;
75d578c770SMing Lei 	union {
76d3f77dfdSTejun Heo 		struct work_struct	async_bio_work;
77d578c770SMing Lei 		struct work_struct	free_work;
78d578c770SMing Lei 	};
79d09d8df3SJosef Bacik 
80d09d8df3SJosef Bacik 	atomic_t			use_delay;
81d09d8df3SJosef Bacik 	atomic64_t			delay_nsec;
82d09d8df3SJosef Bacik 	atomic64_t			delay_start;
83d09d8df3SJosef Bacik 	u64				last_delay;
84d09d8df3SJosef Bacik 	int				last_use;
85d3f77dfdSTejun Heo 
86d3f77dfdSTejun Heo 	struct rcu_head			rcu_head;
87eea8f41cSTejun Heo };
88eea8f41cSTejun Heo 
89496d5e75STejun Heo extern struct cgroup_subsys_state * const blkcg_root_css;
90eea8f41cSTejun Heo 
91672fdcf0SMing Lei void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
92672fdcf0SMing Lei void blkcg_maybe_throttle_current(void);
930fe061b9SDennis Zhou 
940fe061b9SDennis Zhou /**
950fe061b9SDennis Zhou  * bio_blkcg - grab the blkcg associated with a bio
960fe061b9SDennis Zhou  * @bio: target bio
970fe061b9SDennis Zhou  *
980fe061b9SDennis Zhou  * This returns the blkcg associated with a bio, %NULL if not associated.
990fe061b9SDennis Zhou  * Callers are expected to either handle %NULL or know association has been
1000fe061b9SDennis Zhou  * done prior to calling this.
1010fe061b9SDennis Zhou  */
1020fe061b9SDennis Zhou static inline struct blkcg *bio_blkcg(struct bio *bio)
1030fe061b9SDennis Zhou {
104db6638d7SDennis Zhou 	if (bio && bio->bi_blkg)
105db6638d7SDennis Zhou 		return bio->bi_blkg->blkcg;
1060fe061b9SDennis Zhou 	return NULL;
107fd383c2dSTejun Heo }
108fd383c2dSTejun Heo 
109216889aaSChristoph Hellwig bool blk_cgroup_congested(void);
110397c9f46SChristoph Hellwig void blkcg_pin_online(struct cgroup_subsys_state *blkcg_css);
111397c9f46SChristoph Hellwig void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css);
112*dec223c9SChristoph Hellwig struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css);
11359b57717SDennis Zhou (Facebook) 
114eea8f41cSTejun Heo #else	/* CONFIG_BLK_CGROUP */
115eea8f41cSTejun Heo 
116eea8f41cSTejun Heo struct blkcg_gq {
117eea8f41cSTejun Heo };
118eea8f41cSTejun Heo 
119496d5e75STejun Heo #define blkcg_root_css	((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
120496d5e75STejun Heo 
121d09d8df3SJosef Bacik static inline void blkcg_maybe_throttle_current(void) { }
122d09d8df3SJosef Bacik static inline bool blk_cgroup_congested(void) { return false; }
123d09d8df3SJosef Bacik 
124efa7d1c7STejun Heo #ifdef CONFIG_BLOCK
125d09d8df3SJosef Bacik static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
126eea8f41cSTejun Heo static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
127efa7d1c7STejun Heo #endif /* CONFIG_BLOCK */
128672fdcf0SMing Lei 
129eea8f41cSTejun Heo #endif	/* CONFIG_BLK_CGROUP */
130d2bcbeabSMuneendra Kumar 
131db056284SChristoph Hellwig int blkcg_set_fc_appid(char *app_id, u64 cgrp_id, size_t app_id_len);
132db056284SChristoph Hellwig char *blkcg_get_fc_appid(struct bio *bio);
133d2bcbeabSMuneendra Kumar 
134eea8f41cSTejun Heo #endif	/* _BLK_CGROUP_H */
135