2023-01-03 09:10:43

by Yu Kuai

[permalink] [raw]
Subject: [PATCH] block, bfq: switch 'bfqg->ref' to use atomic refcount apis

From: Yu Kuai <[email protected]>

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]>
---
Changes from RFC:
- refcount_dec_and_test(bfqg->ref) -> refcount_dec_and_test(&bfqg->ref)

block/bfq-cgroup.c | 8 +++-----
block/bfq-iosched.h | 2 +-
2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c
index 1b2829e99dad..7d9b15f0dbd5 100644
--- a/block/bfq-cgroup.c
+++ b/block/bfq-cgroup.c
@@ -316,14 +316,12 @@ struct bfq_group *bfqq_group(struct bfq_queue *bfqq)

static void bfqg_get(struct bfq_group *bfqg)
{
- bfqg->ref++;
+ refcount_inc(&bfqg->ref);
}

static void bfqg_put(struct bfq_group *bfqg)
{
- bfqg->ref--;
-
- if (bfqg->ref == 0)
+ if (refcount_dec_and_test(&bfqg->ref))
kfree(bfqg);
}

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

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

diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h
index 41aa151ccc22..466e4865ace6 100644
--- a/block/bfq-iosched.h
+++ b/block/bfq-iosched.h
@@ -928,7 +928,7 @@ struct bfq_group {
char blkg_path[128];

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

--
2.31.1


2023-01-03 11:08:59

by Jan Kara

[permalink] [raw]
Subject: Re: [PATCH] block, bfq: switch 'bfqg->ref' to use atomic refcount apis

On Tue 03-01-23 16:47:55, Yu Kuai wrote:
> From: Yu Kuai <[email protected]>
>
> 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]>

The patch looks good to me. Feel free to add:

Reviewed-by: Jan Kara <[email protected]>

Honza

> ---
> Changes from RFC:
> - refcount_dec_and_test(bfqg->ref) -> refcount_dec_and_test(&bfqg->ref)
>
> block/bfq-cgroup.c | 8 +++-----
> block/bfq-iosched.h | 2 +-
> 2 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c
> index 1b2829e99dad..7d9b15f0dbd5 100644
> --- a/block/bfq-cgroup.c
> +++ b/block/bfq-cgroup.c
> @@ -316,14 +316,12 @@ struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
>
> static void bfqg_get(struct bfq_group *bfqg)
> {
> - bfqg->ref++;
> + refcount_inc(&bfqg->ref);
> }
>
> static void bfqg_put(struct bfq_group *bfqg)
> {
> - bfqg->ref--;
> -
> - if (bfqg->ref == 0)
> + if (refcount_dec_and_test(&bfqg->ref))
> kfree(bfqg);
> }
>
> @@ -530,7 +528,7 @@ static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
> }
>
> /* see comments in bfq_bic_update_cgroup for why refcounting */
> - bfqg_get(bfqg);
> + refcount_set(&bfqg->ref, 1);
> return &bfqg->pd;
> }
>
> diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h
> index 41aa151ccc22..466e4865ace6 100644
> --- a/block/bfq-iosched.h
> +++ b/block/bfq-iosched.h
> @@ -928,7 +928,7 @@ struct bfq_group {
> char blkg_path[128];
>
> /* reference counter (see comments in bfq_bic_update_cgroup) */
> - int ref;
> + refcount_t ref;
> /* Is bfq_group still online? */
> bool online;
>
> --
> 2.31.1
>
--
Jan Kara <[email protected]>
SUSE Labs, CR

2023-01-16 04:05:55

by Yu Kuai

[permalink] [raw]
Subject: Re: [PATCH] block, bfq: switch 'bfqg->ref' to use atomic refcount apis

Hi, Jens

?? 2023/01/03 16:47, Yu Kuai ะด??:
> From: Yu Kuai <[email protected]>
>
> 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]>

Can you apply this patch?

Thanks,
Kuai
> ---
> Changes from RFC:
> - refcount_dec_and_test(bfqg->ref) -> refcount_dec_and_test(&bfqg->ref)
>
> block/bfq-cgroup.c | 8 +++-----
> block/bfq-iosched.h | 2 +-
> 2 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c
> index 1b2829e99dad..7d9b15f0dbd5 100644
> --- a/block/bfq-cgroup.c
> +++ b/block/bfq-cgroup.c
> @@ -316,14 +316,12 @@ struct bfq_group *bfqq_group(struct bfq_queue *bfqq)
>
> static void bfqg_get(struct bfq_group *bfqg)
> {
> - bfqg->ref++;
> + refcount_inc(&bfqg->ref);
> }
>
> static void bfqg_put(struct bfq_group *bfqg)
> {
> - bfqg->ref--;
> -
> - if (bfqg->ref == 0)
> + if (refcount_dec_and_test(&bfqg->ref))
> kfree(bfqg);
> }
>
> @@ -530,7 +528,7 @@ static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q,
> }
>
> /* see comments in bfq_bic_update_cgroup for why refcounting */
> - bfqg_get(bfqg);
> + refcount_set(&bfqg->ref, 1);
> return &bfqg->pd;
> }
>
> diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h
> index 41aa151ccc22..466e4865ace6 100644
> --- a/block/bfq-iosched.h
> +++ b/block/bfq-iosched.h
> @@ -928,7 +928,7 @@ struct bfq_group {
> char blkg_path[128];
>
> /* reference counter (see comments in bfq_bic_update_cgroup) */
> - int ref;
> + refcount_t ref;
> /* Is bfq_group still online? */
> bool online;
>
>

2023-01-16 04:07:26

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] block, bfq: switch 'bfqg->ref' to use atomic refcount apis


On Tue, 03 Jan 2023 16:47:55 +0800, Yu Kuai wrote:
> 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).
>
> [...]

Applied, thanks!

[1/1] block, bfq: switch 'bfqg->ref' to use atomic refcount apis
commit: 216f764716f34fe68cedc7296ae2043a7727e640

Best regards,
--
Jens Axboe