Skip to content

Commit 07b05bc

Browse files
dennisszhouaxboe
authored andcommitted
blkcg: convert blkg_lookup_create to find closest blkg
There are several scenarios where blkg_lookup_create can fail. Examples include the blkcg dying, request_queue is dying, or simply being OOM. At the end of the day, most handle this by simply falling back to the q->root_blkg and calling it a day. This patch implements the notion of closest blkg. During blkg_lookup_create, if it fails to create, return the closest blkg found or the q->root_blkg. blkg_try_get_closest is introduced and used during association so a bio is always attached to a blkg. Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Dennis Zhou <dennisszhou@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 49f4c2d commit 07b05bc

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

block/bio.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,21 +2007,24 @@ int bio_associate_blkcg(struct bio *bio, struct cgroup_subsys_state *blkcg_css)
20072007
EXPORT_SYMBOL_GPL(bio_associate_blkcg);
20082008

20092009
/**
2010-
* bio_associate_blkg - associate a bio with the specified blkg
2010+
* bio_associate_blkg - associate a bio with the a blkg
20112011
* @bio: target bio
20122012
* @blkg: the blkg to associate
20132013
*
2014-
* Associate @bio with the blkg specified by @blkg. This is the queue specific
2015-
* blkcg information associated with the @bio, a reference will be taken on the
2016-
* @blkg and will be freed when the bio is freed.
2014+
* This tries to associate @bio with the specified blkg. Association failure
2015+
* is handled by walking up the blkg tree. Therefore, the blkg associated can
2016+
* be anything between @blkg and the root_blkg. This situation only happens
2017+
* when a cgroup is dying and then the remaining bios will spill to the closest
2018+
* alive blkg.
2019+
*
2020+
* A reference will be taken on the @blkg and will be released when @bio is
2021+
* freed.
20172022
*/
20182023
int bio_associate_blkg(struct bio *bio, struct blkcg_gq *blkg)
20192024
{
20202025
if (unlikely(bio->bi_blkg))
20212026
return -EBUSY;
2022-
if (!blkg_try_get(blkg))
2023-
return -ENODEV;
2024-
bio->bi_blkg = blkg;
2027+
bio->bi_blkg = blkg_try_get_closest(blkg);
20252028
return 0;
20262029
}
20272030

block/blk-cgroup.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,8 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg,
268268
* that all non-root blkg's have access to the parent blkg. This function
269269
* should be called under RCU read lock and @q->queue_lock.
270270
*
271-
* Returns pointer to the looked up or created blkg on success, ERR_PTR()
272-
* value on error. If @q is dead, returns ERR_PTR(-EINVAL). If @q is not
273-
* dead and bypassing, returns ERR_PTR(-EBUSY).
271+
* Returns the blkg or the closest blkg if blkg_create fails as it walks
272+
* down from root.
274273
*/
275274
struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
276275
struct request_queue *q)
@@ -285,27 +284,37 @@ struct blkcg_gq *__blkg_lookup_create(struct blkcg *blkcg,
285284
* we shouldn't allow anything to go through for a bypassing queue.
286285
*/
287286
if (unlikely(blk_queue_bypass(q)))
288-
return ERR_PTR(blk_queue_dying(q) ? -ENODEV : -EBUSY);
287+
return q->root_blkg;
289288

290289
blkg = __blkg_lookup(blkcg, q, true);
291290
if (blkg)
292291
return blkg;
293292

294293
/*
295294
* Create blkgs walking down from blkcg_root to @blkcg, so that all
296-
* non-root blkgs have access to their parents.
295+
* non-root blkgs have access to their parents. Returns the closest
296+
* blkg to the intended blkg should blkg_create() fail.
297297
*/
298298
while (true) {
299299
struct blkcg *pos = blkcg;
300300
struct blkcg *parent = blkcg_parent(blkcg);
301-
302-
while (parent && !__blkg_lookup(parent, q, false)) {
301+
struct blkcg_gq *ret_blkg = q->root_blkg;
302+
303+
while (parent) {
304+
blkg = __blkg_lookup(parent, q, false);
305+
if (blkg) {
306+
/* remember closest blkg */
307+
ret_blkg = blkg;
308+
break;
309+
}
303310
pos = parent;
304311
parent = blkcg_parent(parent);
305312
}
306313

307314
blkg = blkg_create(pos, q, NULL);
308-
if (pos == blkcg || IS_ERR(blkg))
315+
if (IS_ERR(blkg))
316+
return ret_blkg;
317+
if (pos == blkcg)
309318
return blkg;
310319
}
311320
}

include/linux/blk-cgroup.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,20 @@ static inline struct blkcg_gq *blkg_try_get(struct blkcg_gq *blkg)
549549
return NULL;
550550
}
551551

552+
/**
553+
* blkg_try_get_closest - try and get a blkg ref on the closet blkg
554+
* @blkg: blkg to get
555+
*
556+
* This walks up the blkg tree to find the closest non-dying blkg and returns
557+
* the blkg that it did association with as it may not be the passed in blkg.
558+
*/
559+
static inline struct blkcg_gq *blkg_try_get_closest(struct blkcg_gq *blkg)
560+
{
561+
while (!atomic_inc_not_zero(&blkg->refcnt))
562+
blkg = blkg->parent;
563+
564+
return blkg;
565+
}
552566

553567
void __blkg_release_rcu(struct rcu_head *rcu);
554568

0 commit comments

Comments
 (0)