From: Jens Axboe <axboe@kernel.dk>
To: Waiman Long <longman@redhat.com>, Tejun Heo <tj@kernel.org>
Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org, "Ming Lei" <ming.lei@redhat.com>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Michal Koutný" <mkoutny@suse.com>,
"Hillf Danton" <hdanton@sina.com>,
"Yi Zhang" <yi.zhang@redhat.com>
Subject: Re: [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs()
Date: Mon, 28 Nov 2022 07:14:37 -0700 [thread overview]
Message-ID: <427068db-6695-a1e2-4aa2-033220680eb9@kernel.dk> (raw)
In-Reply-To: <20221128033057.1279383-1-longman@redhat.com>
On 11/27/22 8:30 PM, Waiman Long wrote:
> Commit 951d1e94801f ("blk-cgroup: Flush stats at blkgs destruction
> path") incorrectly assumes that css_get() will always succeed. That may
> not be true if there is no blkg associated with the blkcg. If css_get()
> fails, the subsequent css_put() call may lead to data corruption as
> was illustrated in a test system that it crashed on bootup when that
> commit was included. Also blkcg may be freed at any time leading to
> use-after-free. Fix it by using css_tryget() instead and bail out if
> the tryget fails.
>
> Fixes: 951d1e94801f ("blk-cgroup: Flush stats at blkgs destruction path")
> Reported-by: Yi Zhang <yi.zhang@redhat.com>
> Signed-off-by: Waiman Long <longman@redhat.com>
> ---
> block/blk-cgroup.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
> index 57941d2a8ba3..74fefc8cbcdf 100644
> --- a/block/blk-cgroup.c
> +++ b/block/blk-cgroup.c
> @@ -1088,7 +1088,12 @@ static void blkcg_destroy_blkgs(struct blkcg *blkcg)
>
> might_sleep();
>
> - css_get(&blkcg->css);
> + /*
> + * If css_tryget() fails, there is no blkg to destroy.
> + */
> + if (!css_tryget(&blkcg->css))
> + return;
> +
> spin_lock_irq(&blkcg->lock);
> while (!hlist_empty(&blkcg->blkg_list)) {
> struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first,
This doesn't seem safe to me, but maybe I'm missing something. A tryget
operation can be fine if we're under RCU lock and the entity is freed
appropriately, but what makes it safe here? Could blkcg already be gone
at this point?
--
Jens Axboe
next prev parent reply other threads:[~2022-11-28 14:14 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-28 3:30 [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs() Waiman Long
2022-11-28 14:06 ` Michal Koutný
[not found] ` <20221128140631.GI25160-9OudH3eul5jcvrawFnH+a6VXKuFTiq87@public.gmane.org>
2022-11-28 15:28 ` Waiman Long
2022-11-28 14:14 ` Jens Axboe [this message]
2022-11-28 15:38 ` Waiman Long
2022-11-28 15:42 ` Jens Axboe
[not found] ` <786aacda-b25d-67f6-bad3-0030b0e2637e-tSWWG44O7X1aa/9Udqfwiw@public.gmane.org>
2022-11-28 15:53 ` Waiman Long
[not found] ` <20221128033057.1279383-1-longman-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-11-28 18:56 ` Bart Van Assche
[not found] ` <d08a0059-7c0b-d65f-d184-5b0cb75c08ed-HInyCGIudOg@public.gmane.org>
2022-11-28 19:00 ` Jens Axboe
2022-11-28 19:07 ` Andy Shevchenko
2022-11-28 19:08 ` Jens Axboe
2022-11-28 19:11 ` Andy Shevchenko
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=427068db-6695-a1e2-4aa2-033220680eb9@kernel.dk \
--to=axboe@kernel.dk \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=cgroups@vger.kernel.org \
--cc=hdanton@sina.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=ming.lei@redhat.com \
--cc=mkoutny@suse.com \
--cc=tj@kernel.org \
--cc=yi.zhang@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox