Skip to content

Commit 216f764

Browse files
YuKuai-huaweiaxboe
authored andcommitted
block, bfq: switch 'bfqg->ref' to use atomic refcount apis
The updating of 'bfqg->ref' should be protected by 'bfqd->lock', however, during code review, we found that bfq_pd_free() update 'bfqg->ref' without holding the lock, which is problematic: 1) bfq_pd_free() triggered by removing cgroup is called asynchronously; 2) bfqq will grab bfqg reference, and exit bfqq will drop the reference, which can concurrent with 1). Unfortunately, 'bfqd->lock' can't be held here because 'bfqd' might already be freed in bfq_pd_free(). Fix the problem by using atomic refcount apis. Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent ee16c40 commit 216f764

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

block/bfq-cgroup.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,12 @@ struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
316316

317317
static void bfqg_get(struct bfq_group *bfqg)
318318
{
319-
bfqg->ref++;
319+
refcount_inc(&bfqg->ref);
320320
}
321321

322322
static void bfqg_put(struct bfq_group *bfqg)
323323
{
324-
bfqg->ref--;
325-
326-
if (bfqg->ref == 0)
324+
if (refcount_dec_and_test(&bfqg->ref))
327325
kfree(bfqg);
328326
}
329327

@@ -530,7 +528,7 @@ static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
530528
}
531529

532530
/* see comments in bfq_bic_update_cgroup for why refcounting */
533-
bfqg_get(bfqg);
531+
refcount_set(&bfqg->ref, 1);
534532
return &bfqg->pd;
535533
}
536534

block/bfq-iosched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ struct bfq_group {
928928
char blkg_path[128];
929929

930930
/* reference counter (see comments in bfq_bic_update_cgroup) */
931-
int ref;
931+
refcount_t ref;
932932
/* Is bfq_group still online? */
933933
bool online;
934934

0 commit comments

Comments
 (0)