Skip to content

Commit bbb1ebe

Browse files
Christoph Hellwigaxboe
authored andcommitted
blk-cgroup: replace bio_blkcg with bio_blkcg_css
All callers of bio_blkcg actually want the CSS, so replace it with an interface that does return the CSS. This now allows to move struct blkcg_gq to block/blk-cgroup.h instead of exposing it in a public header. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Tejun Heo <tj@kernel.org> Link: https://lore.kernel.org/r/20220420042723.1010598-10-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent f4a6a61 commit bbb1ebe

5 files changed

Lines changed: 97 additions & 89 deletions

File tree

block/blk-cgroup.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ static void blkg_async_bio_workfn(struct work_struct *work)
155155
blk_finish_plug(&plug);
156156
}
157157

158+
/**
159+
* bio_blkcg_css - return the blkcg CSS associated with a bio
160+
* @bio: target bio
161+
*
162+
* This returns the CSS for the blkcg associated with a bio, or %NULL if not
163+
* associated. Callers are expected to either handle %NULL or know association
164+
* has been done prior to calling this.
165+
*/
166+
struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio)
167+
{
168+
if (!bio || !bio->bi_blkg)
169+
return NULL;
170+
return &bio->bi_blkg->blkcg->css;
171+
}
172+
EXPORT_SYMBOL_GPL(bio_blkcg_css);
173+
158174
/**
159175
* blkcg_parent - get the parent of a blkcg
160176
* @blkcg: blkcg of interest
@@ -1938,7 +1954,7 @@ void bio_associate_blkg(struct bio *bio)
19381954
rcu_read_lock();
19391955

19401956
if (bio->bi_blkg)
1941-
css = &bio_blkcg(bio)->css;
1957+
css = bio_blkcg_css(bio);
19421958
else
19431959
css = blkcg_css();
19441960

block/blk-cgroup.h

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,64 @@ struct blkg_policy_data;
2525
#define BLKG_STAT_CPU_BATCH (INT_MAX / 2)
2626

2727
#ifdef CONFIG_BLK_CGROUP
28+
29+
enum blkg_iostat_type {
30+
BLKG_IOSTAT_READ,
31+
BLKG_IOSTAT_WRITE,
32+
BLKG_IOSTAT_DISCARD,
33+
34+
BLKG_IOSTAT_NR,
35+
};
36+
37+
struct blkg_iostat {
38+
u64 bytes[BLKG_IOSTAT_NR];
39+
u64 ios[BLKG_IOSTAT_NR];
40+
};
41+
42+
struct blkg_iostat_set {
43+
struct u64_stats_sync sync;
44+
struct blkg_iostat cur;
45+
struct blkg_iostat last;
46+
};
47+
48+
/* association between a blk cgroup and a request queue */
49+
struct blkcg_gq {
50+
/* Pointer to the associated request_queue */
51+
struct request_queue *q;
52+
struct list_head q_node;
53+
struct hlist_node blkcg_node;
54+
struct blkcg *blkcg;
55+
56+
/* all non-root blkcg_gq's are guaranteed to have access to parent */
57+
struct blkcg_gq *parent;
58+
59+
/* reference count */
60+
struct percpu_ref refcnt;
61+
62+
/* is this blkg online? protected by both blkcg and q locks */
63+
bool online;
64+
65+
struct blkg_iostat_set __percpu *iostat_cpu;
66+
struct blkg_iostat_set iostat;
67+
68+
struct blkg_policy_data *pd[BLKCG_MAX_POLS];
69+
70+
spinlock_t async_bio_lock;
71+
struct bio_list async_bios;
72+
union {
73+
struct work_struct async_bio_work;
74+
struct work_struct free_work;
75+
};
76+
77+
atomic_t use_delay;
78+
atomic64_t delay_nsec;
79+
atomic64_t delay_start;
80+
u64 last_delay;
81+
int last_use;
82+
83+
struct rcu_head rcu_head;
84+
};
85+
2886
struct blkcg {
2987
struct cgroup_subsys_state css;
3088
spinlock_t lock;
@@ -173,9 +231,9 @@ static inline struct cgroup_subsys_state *blkcg_css(void)
173231
*
174232
* In order to avoid priority inversions we sometimes need to issue a bio as if
175233
* it were attached to the root blkg, and then backcharge to the actual owning
176-
* blkg. The idea is we do bio_blkcg() to look up the actual context for the
177-
* bio and attach the appropriate blkg to the bio. Then we call this helper and
178-
* if it is true run with the root blkg for that queue and then do any
234+
* blkg. The idea is we do bio_blkcg_css() to look up the actual context for
235+
* the bio and attach the appropriate blkg to the bio. Then we call this helper
236+
* and if it is true run with the root blkg for that queue and then do any
179237
* backcharging to the originating cgroup once the io is complete.
180238
*/
181239
static inline bool bio_issue_as_root_blkg(struct bio *bio)
@@ -464,6 +522,9 @@ struct blkcg_policy_data {
464522
struct blkcg_policy {
465523
};
466524

525+
struct blkcg {
526+
};
527+
467528
#ifdef CONFIG_BLOCK
468529

469530
static inline struct blkcg_gq *blkg_lookup(struct blkcg *blkcg, void *key) { return NULL; }

drivers/block/loop.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,12 +1829,14 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
18291829
cmd->blkcg_css = NULL;
18301830
cmd->memcg_css = NULL;
18311831
#ifdef CONFIG_BLK_CGROUP
1832-
if (rq->bio && rq->bio->bi_blkg) {
1833-
cmd->blkcg_css = &bio_blkcg(rq->bio)->css;
1832+
if (rq->bio) {
1833+
cmd->blkcg_css = bio_blkcg_css(rq->bio);
18341834
#ifdef CONFIG_MEMCG
1835-
cmd->memcg_css =
1836-
cgroup_get_e_css(cmd->blkcg_css->cgroup,
1837-
&memory_cgrp_subsys);
1835+
if (cmd->blkcg_css) {
1836+
cmd->memcg_css =
1837+
cgroup_get_e_css(cmd->blkcg_css->cgroup,
1838+
&memory_cgrp_subsys);
1839+
}
18381840
#endif
18391841
}
18401842
#endif

include/linux/blk-cgroup.h

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,102 +28,29 @@
2828
#define FC_APPID_LEN 129
2929

3030
#ifdef CONFIG_BLK_CGROUP
31-
32-
enum blkg_iostat_type {
33-
BLKG_IOSTAT_READ,
34-
BLKG_IOSTAT_WRITE,
35-
BLKG_IOSTAT_DISCARD,
36-
37-
BLKG_IOSTAT_NR,
38-
};
39-
40-
struct blkg_iostat {
41-
u64 bytes[BLKG_IOSTAT_NR];
42-
u64 ios[BLKG_IOSTAT_NR];
43-
};
44-
45-
struct blkg_iostat_set {
46-
struct u64_stats_sync sync;
47-
struct blkg_iostat cur;
48-
struct blkg_iostat last;
49-
};
50-
51-
/* association between a blk cgroup and a request queue */
52-
struct blkcg_gq {
53-
/* Pointer to the associated request_queue */
54-
struct request_queue *q;
55-
struct list_head q_node;
56-
struct hlist_node blkcg_node;
57-
struct blkcg *blkcg;
58-
59-
/* all non-root blkcg_gq's are guaranteed to have access to parent */
60-
struct blkcg_gq *parent;
61-
62-
/* reference count */
63-
struct percpu_ref refcnt;
64-
65-
/* is this blkg online? protected by both blkcg and q locks */
66-
bool online;
67-
68-
struct blkg_iostat_set __percpu *iostat_cpu;
69-
struct blkg_iostat_set iostat;
70-
71-
struct blkg_policy_data *pd[BLKCG_MAX_POLS];
72-
73-
spinlock_t async_bio_lock;
74-
struct bio_list async_bios;
75-
union {
76-
struct work_struct async_bio_work;
77-
struct work_struct free_work;
78-
};
79-
80-
atomic_t use_delay;
81-
atomic64_t delay_nsec;
82-
atomic64_t delay_start;
83-
u64 last_delay;
84-
int last_use;
85-
86-
struct rcu_head rcu_head;
87-
};
88-
8931
extern struct cgroup_subsys_state * const blkcg_root_css;
9032

9133
void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay);
9234
void blkcg_maybe_throttle_current(void);
93-
94-
/**
95-
* bio_blkcg - grab the blkcg associated with a bio
96-
* @bio: target bio
97-
*
98-
* This returns the blkcg associated with a bio, %NULL if not associated.
99-
* Callers are expected to either handle %NULL or know association has been
100-
* done prior to calling this.
101-
*/
102-
static inline struct blkcg *bio_blkcg(struct bio *bio)
103-
{
104-
if (bio && bio->bi_blkg)
105-
return bio->bi_blkg->blkcg;
106-
return NULL;
107-
}
108-
10935
bool blk_cgroup_congested(void);
11036
void blkcg_pin_online(struct cgroup_subsys_state *blkcg_css);
11137
void blkcg_unpin_online(struct cgroup_subsys_state *blkcg_css);
11238
struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css);
39+
struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio);
11340

11441
#else /* CONFIG_BLK_CGROUP */
11542

116-
struct blkcg_gq {
117-
};
118-
11943
#define blkcg_root_css ((struct cgroup_subsys_state *)ERR_PTR(-EINVAL))
12044

12145
static inline void blkcg_maybe_throttle_current(void) { }
12246
static inline bool blk_cgroup_congested(void) { return false; }
12347

12448
#ifdef CONFIG_BLOCK
12549
static inline void blkcg_schedule_throttle(struct request_queue *q, bool use_memdelay) { }
126-
static inline struct blkcg *bio_blkcg(struct bio *bio) { return NULL; }
50+
static inline struct cgroup_subsys_state *bio_blkcg_css(struct bio *bio)
51+
{
52+
return NULL;
53+
}
12754
#endif /* CONFIG_BLOCK */
12855

12956
#endif /* CONFIG_BLK_CGROUP */

kernel/trace/blktrace.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,16 +783,18 @@ void blk_trace_shutdown(struct request_queue *q)
783783
#ifdef CONFIG_BLK_CGROUP
784784
static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)
785785
{
786+
struct cgroup_subsys_state *blkcg_css;
786787
struct blk_trace *bt;
787788

788789
/* We don't use the 'bt' value here except as an optimization... */
789790
bt = rcu_dereference_protected(q->blk_trace, 1);
790791
if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP))
791792
return 0;
792793

793-
if (!bio->bi_blkg)
794+
blkcg_css = bio_blkcg_css(bio);
795+
if (!blkcg_css)
794796
return 0;
795-
return cgroup_id(bio_blkcg(bio)->css.cgroup);
797+
return cgroup_id(blkcg_css->cgroup);
796798
}
797799
#else
798800
static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio)

0 commit comments

Comments
 (0)