Skip to content

Commit 2268c0f

Browse files
dennisszhouaxboe
authored andcommitted
blkcg: introduce common blkg association logic
There are 3 ways blkg association can happen: association with the current css, with the page css (swap), or from the wbc css (writeback). This patch handles how association is done for the first case where we are associating bsaed on the current css. If there is already a blkg associated, the css will be reused and association will be redone as the request_queue may have changed. Signed-off-by: Dennis Zhou <dennis@kernel.org> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent beea9da commit 2268c0f

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

block/bio.c

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,21 @@ int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
20092009
EXPORT_SYMBOL_GPL(bio_associate_blkcg);
20102010

20112011
/**
2012-
* bio_associate_blkg - associate a bio with the a blkg
2012+
* bio_disassociate_blkg - puts back the blkg reference if associated
2013+
* @bio: target bio
2014+
*
2015+
* Helper to disassociate the blkg from @bio if a blkg is associated.
2016+
*/
2017+
void bio_disassociate_blkg(struct bio *bio)
2018+
{
2019+
if (bio->bi_blkg) {
2020+
blkg_put(bio->bi_blkg);
2021+
bio->bi_blkg = NULL;
2022+
}
2023+
}
2024+
2025+
/**
2026+
* __bio_associate_blkg - associate a bio with the a blkg
20132027
* @bio: target bio
20142028
* @blkg: the blkg to associate
20152029
*
@@ -2022,12 +2036,42 @@ EXPORT_SYMBOL_GPL(bio_associate_blkcg);
20222036
* A reference will be taken on the @blkg and will be released when @bio is
20232037
* freed.
20242038
*/
2025-
int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
2039+
static void __bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
20262040
{
2027-
if (unlikely(bio->bi_blkg))
2028-
return -EBUSY;
2041+
bio_disassociate_blkg(bio);
2042+
20292043
bio->bi_blkg = blkg_try_get_closest(blkg);
2030-
return 0;
2044+
}
2045+
2046+
/**
2047+
* bio_associate_blkg - associate a bio with a blkg
2048+
* @bio: target bio
2049+
*
2050+
* Associate @bio with the blkg found from the bio's css and request_queue.
2051+
* If one is not found, bio_lookup_blkg() creates the blkg. If a blkg is
2052+
* already associated, the css is reused and association redone as the
2053+
* request_queue may have changed.
2054+
*/
2055+
void bio_associate_blkg(struct bio *bio)
2056+
{
2057+
struct request_queue *q = bio->bi_disk->queue;
2058+
struct blkcg *blkcg;
2059+
struct blkcg_gq *blkg;
2060+
2061+
rcu_read_lock();
2062+
2063+
bio_associate_blkcg(bio, NULL);
2064+
blkcg = bio_blkcg(bio);
2065+
2066+
if (!blkcg->css.parent) {
2067+
__bio_associate_blkg(bio, q->root_blkg);
2068+
} else {
2069+
blkg = blkg_lookup_create(blkcg, q);
2070+
2071+
__bio_associate_blkg(bio, blkg);
2072+
}
2073+
2074+
rcu_read_unlock();
20312075
}
20322076

20332077
/**
@@ -2040,10 +2084,7 @@ void bio_disassociate_task(struct bio *bio)
20402084
css_put(bio->bi_css);
20412085
bio->bi_css = NULL;
20422086
}
2043-
if (bio->bi_blkg) {
2044-
blkg_put(bio->bi_blkg);
2045-
bio->bi_blkg = NULL;
2046-
}
2087+
bio_disassociate_blkg(bio);
20472088
}
20482089

20492090
/**
@@ -2055,6 +2096,9 @@ void bio_clone_blkcg_association(struct bio *dst, struct bio *src)
20552096
{
20562097
if (src->bi_css)
20572098
WARN_ON(bio_associate_blkcg(dst, src->bi_css));
2099+
2100+
if (src->bi_blkg)
2101+
__bio_associate_blkg(dst, src->bi_blkg);
20582102
}
20592103
EXPORT_SYMBOL_GPL(bio_clone_blkcg_association);
20602104
#endif /* CONFIG_BLK_CGROUP */

block/blk-iolatency.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -472,21 +472,15 @@ static void check_scale_change(struct iolatency_grp *iolat)
472472
static void blkcg_iolatency_throttle(struct rq_qos *rqos, struct bio *bio)
473473
{
474474
struct blk_iolatency *blkiolat = BLKIOLATENCY(rqos);
475-
struct blkcg *blkcg;
476475
struct blkcg_gq *blkg;
477-
struct request_queue *q = rqos->q;
478476
bool issue_as_root = bio_issue_as_root_blkg(bio);
479477

480478
if (!blk_iolatency_enabled(blkiolat))
481479
return;
482480

483-
rcu_read_lock();
484-
bio_associate_blkcg(bio, NULL);
485-
blkcg = bio_blkcg(bio);
486-
blkg = blkg_lookup_create(blkcg, q);
481+
bio_associate_blkg(bio);
482+
blkg = bio->bi_blkg;
487483
bio_issue_init(&bio->bi_issue, bio_sectors(bio));
488-
bio_associate_blkg(bio, blkg);
489-
rcu_read_unlock();
490484

491485
while (blkg && blkg->parent) {
492486
struct iolatency_grp *iolat = blkg_to_lat(blkg);

block/blk-throttle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,10 +2115,10 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
21152115
}
21162116
#endif
21172117

2118-
static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
2118+
static void blk_throtl_assoc_bio(struct bio *bio)
21192119
{
21202120
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2121-
bio_associate_blkg(bio, tg_to_blkg(tg));
2121+
bio_associate_blkg(bio);
21222122
bio_issue_init(&bio->bi_issue, bio_sectors(bio));
21232123
#endif
21242124
}
@@ -2143,7 +2143,7 @@ bool blk_throtl_bio(struct request_queue *q, struct blkcg_gq *blkg,
21432143

21442144
throtl_update_latency_buckets(td);
21452145

2146-
blk_throtl_assoc_bio(tg, bio);
2146+
blk_throtl_assoc_bio(bio);
21472147
blk_throtl_update_idletime(tg);
21482148

21492149
sq = &tg->service_queue;

include/linux/bio.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,15 @@ static inline int bio_associate_blkcg_from_page(struct bio *bio,
511511

512512
#ifdef CONFIG_BLK_CGROUP
513513
int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css);
514-
int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg);
514+
void bio_disassociate_blkg(struct bio *bio);
515+
void bio_associate_blkg(struct bio *bio);
515516
void bio_disassociate_task(struct bio *bio);
516517
void bio_clone_blkcg_association(struct bio *dst, struct bio *src);
517518
#else /* CONFIG_BLK_CGROUP */
518519
static inline int bio_associate_blkcg(struct bio *bio,
519520
struct cgroup_subsys_state *blkcg_css) { return 0; }
521+
static inline void bio_disassociate_blkg(struct bio *bio) { }
522+
static inline void bio_associate_blkg(struct bio *bio) { }
520523
static inline void bio_disassociate_task(struct bio *bio) { }
521524
static inline void bio_clone_blkcg_association(struct bio *dst,
522525
struct bio *src) { }

0 commit comments

Comments
 (0)