All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs()
@ 2022-11-28  3:30 ` Waiman Long
  0 siblings, 0 replies; 20+ messages in thread
From: Waiman Long @ 2022-11-28  3:30 UTC (permalink / raw)
  To: Tejun Heo, Jens Axboe
  Cc: cgroups, linux-block, linux-kernel, Ming Lei, Andy Shevchenko,
	Andrew Morton, Michal Koutný,
	Hillf Danton, Waiman Long, Yi Zhang

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,
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2022-11-28 19:11 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-28  3:30 [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs() Waiman Long
2022-11-28  3:30 ` Waiman Long
2022-11-28 14:06 ` Michal Koutný
2022-11-28 15:28   ` Waiman Long
2022-11-28 15:28     ` Waiman Long
2022-11-28 14:14 ` Jens Axboe
2022-11-28 14:14   ` Jens Axboe
2022-11-28 15:38   ` Waiman Long
2022-11-28 15:38     ` Waiman Long
2022-11-28 15:42     ` Jens Axboe
2022-11-28 15:53       ` Waiman Long
2022-11-28 15:53         ` Waiman Long
2022-11-28 18:56 ` Bart Van Assche
2022-11-28 18:56   ` Bart Van Assche
2022-11-28 19:00   ` Jens Axboe
2022-11-28 19:00     ` Jens Axboe
2022-11-28 19:07     ` Andy Shevchenko
2022-11-28 19:07       ` Andy Shevchenko
2022-11-28 19:08       ` Jens Axboe
2022-11-28 19:11         ` Andy Shevchenko

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.