All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Yu Kuai <yukuai1@huaweicloud.com>
Cc: jack@suse.cz, tj@kernel.org, josef@toxicpanda.com,
	axboe@kernel.dk, paolo.valente@linaro.org,
	cgroups@vger.kernel.org, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org, yi.zhang@huawei.com,
	"yukuai (C)" <yukuai3@huawei.com>
Subject: Re: [PATCH RFC] block, bfq: switch 'bfqg->ref' to use atomic refcount apis
Date: Mon, 2 Jan 2023 12:22:58 +0100	[thread overview]
Message-ID: <20230102112258.3fixhuialamu6pkd@quack3> (raw)
In-Reply-To: <ba5e74a6-b3de-844d-16b8-84eb429c7058@huaweicloud.com>

On Tue 27-12-22 14:09:40, Yu Kuai wrote:
> Hi, Jan!
> 
> 在 2022/12/27 11:15, Yu Kuai 写道:
> > From: Yu Kuai <yukuai3@huawei.com>
> > 
> > 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 concurrenty 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 <yukuai3@huawei.com>
> > ---
> >   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..aa9c4f02e3a3 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))
> Sorry that here should be '&bfqg->ref'.
> 
> Anyway, I'll wait for you, and send a new version if you think this
> patch make sense.

Yes, the patch makes sense to me so feel free to send fixed version.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

WARNING: multiple messages have this Message-ID (diff)
From: Jan Kara <jack-AlSwsSmVLrQ@public.gmane.org>
To: Yu Kuai <yukuai1-XF6JlduFytWkHkcT6e4Xnw@public.gmane.org>
Cc: jack-AlSwsSmVLrQ@public.gmane.org,
	tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	josef-DigfWCa+lFGyeJad7bwFQA@public.gmane.org,
	axboe-tSWWG44O7X1aa/9Udqfwiw@public.gmane.org,
	paolo.valente-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org,
	cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-block-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	yi.zhang-hv44wF8Li93QT0dZR+AlfA@public.gmane.org,
	"yukuai (C)" <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
Subject: Re: [PATCH RFC] block, bfq: switch 'bfqg->ref' to use atomic refcount apis
Date: Mon, 2 Jan 2023 12:22:58 +0100	[thread overview]
Message-ID: <20230102112258.3fixhuialamu6pkd@quack3> (raw)
In-Reply-To: <ba5e74a6-b3de-844d-16b8-84eb429c7058-XF6JlduFytWkHkcT6e4Xnw@public.gmane.org>

On Tue 27-12-22 14:09:40, Yu Kuai wrote:
> Hi, Jan!
> 
> 在 2022/12/27 11:15, Yu Kuai 写道:
> > From: Yu Kuai <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
> > 
> > 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 concurrenty 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 <yukuai3-hv44wF8Li93QT0dZR+AlfA@public.gmane.org>
> > ---
> >   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..aa9c4f02e3a3 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))
> Sorry that here should be '&bfqg->ref'.
> 
> Anyway, I'll wait for you, and send a new version if you think this
> patch make sense.

Yes, the patch makes sense to me so feel free to send fixed version.

								Honza
-- 
Jan Kara <jack-IBi9RG/b67k@public.gmane.org>
SUSE Labs, CR

  reply	other threads:[~2023-01-02 11:23 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-27  3:15 [PATCH RFC] block, bfq: switch 'bfqg->ref' to use atomic refcount apis Yu Kuai
2022-12-27  4:51 ` kernel test robot
2022-12-27  5:52 ` kernel test robot
2022-12-27  6:09 ` Yu Kuai
2022-12-27  6:09   ` Yu Kuai
2023-01-02 11:22   ` Jan Kara [this message]
2023-01-02 11:22     ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230102112258.3fixhuialamu6pkd@quack3 \
    --to=jack@suse.cz \
    --cc=axboe@kernel.dk \
    --cc=cgroups@vger.kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=paolo.valente@linaro.org \
    --cc=tj@kernel.org \
    --cc=yi.zhang@huawei.com \
    --cc=yukuai1@huaweicloud.com \
    --cc=yukuai3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.