Skip to content

Commit 3111885

Browse files
dennisszhouaxboe
authored andcommitted
blkcg: use tryget logic when associating a blkg with a bio
There is a very small change a bio gets caught up in a really unfortunate race between a task migration, cgroup exiting, and itself trying to associate with a blkg. This is due to css offlining being performed after the css->refcnt is killed which triggers removal of blkgs that reach their blkg->refcnt of 0. To avoid this, association with a blkg should use tryget and fallback to using the root_blkg. Fixes: 08e18ea ("block: add bi_blkg to the bio for cgroups") Reviewed-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: Dennis Zhou <dennisszhou@gmail.com> Cc: Jiufei Xue <jiufei.xue@linux.alibaba.com> Cc: Joseph Qi <joseph.qi@linux.alibaba.com> Cc: Tejun Heo <tj@kernel.org> Cc: Josef Bacik <josef@toxicpanda.com> Cc: Jens Axboe <axboe@kernel.dk> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 59b5771 commit 3111885

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

block/bio.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,8 @@ int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
20152015
{
20162016
if (unlikely(bio->bi_blkg))
20172017
return -EBUSY;
2018-
blkg_get(blkg);
2018+
if (!blkg_try_get(blkg))
2019+
return -ENODEV;
20192020
bio->bi_blkg = blkg;
20202021
return 0;
20212022
}

block/blk-throttle.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,8 +2129,9 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
21292129
static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
21302130
{
21312131
#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
2132-
if (bio->bi_css)
2133-
bio_associate_blkg(bio, tg_to_blkg(tg));
2132+
/* fallback to root_blkg if we fail to get a blkg ref */
2133+
if (bio->bi_css && (bio_associate_blkg(bio, tg_to_blkg(tg)) == -ENODEV))
2134+
bio_associate_blkg(bio, bio->bi_disk->queue->root_blkg);
21342135
bio_issue_init(&bio->bi_issue, bio_sectors(bio));
21352136
#endif
21362137
}

0 commit comments

Comments
 (0)