[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20221128033057.1279383-1-longman@redhat.com>
Date: Sun, 27 Nov 2022 22:30:57 -0500
From: Waiman Long <longman@...hat.com>
To: Tejun Heo <tj@...nel.org>, Jens Axboe <axboe@...nel.dk>
Cc: cgroups@...r.kernel.org, linux-block@...r.kernel.org,
linux-kernel@...r.kernel.org, Ming Lei <ming.lei@...hat.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Andrew Morton <akpm@...ux-foundation.org>,
Michal Koutný <mkoutny@...e.com>,
Hillf Danton <hdanton@...a.com>,
Waiman Long <longman@...hat.com>,
Yi Zhang <yi.zhang@...hat.com>
Subject: [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs()
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@...hat.com>
Signed-off-by: Waiman Long <longman@...hat.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,
--
2.31.1
Powered by blists - more mailing lists