From patchwork Mon Nov 28 03:30:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13057011 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE159C4321E for ; Mon, 28 Nov 2022 03:32:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229929AbiK1Dcm (ORCPT ); Sun, 27 Nov 2022 22:32:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229907AbiK1Dch (ORCPT ); Sun, 27 Nov 2022 22:32:37 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8DB913CC2 for ; Sun, 27 Nov 2022 19:31:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669606292; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Mh8+HZNMN2F6HzSwmWPr16ecpqC3Mw7WO0DSOHuRXuQ=; b=JD1i+j+QXnc1HbipMzQVoy70Q7r2FqnP54sOXtsrQmDNrJYY8FhPMvAPYDDPkRkEkBnvGN kdW0JcwqJFKbAynjcX2aZ8pdq0uyjfV+qt/sQ/TrZ7msaY2wB419SA3L+0Po3ODOqsTDVy O0NlNcKEyNaQMH4wBTuSK854fI/wc2g= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-664-HrKrfLHKPnuPTFPzc0qKUA-1; Sun, 27 Nov 2022 22:31:28 -0500 X-MC-Unique: HrKrfLHKPnuPTFPzc0qKUA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E4C43811E67; Mon, 28 Nov 2022 03:31:27 +0000 (UTC) Received: from llong.com (unknown [10.22.32.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5A17492B08; Mon, 28 Nov 2022 03:31:26 +0000 (UTC) From: Waiman Long To: Tejun Heo , Jens Axboe Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Ming Lei , Andy Shevchenko , Andrew Morton , =?utf-8?q?Michal_Koutn=C3=BD?= , Hillf Danton , Waiman Long , Yi Zhang Subject: [PATCH-block] blk-cgroup: Use css_tryget() in blkcg_destroy_blkgs() Date: Sun, 27 Nov 2022 22:30:57 -0500 Message-Id: <20221128033057.1279383-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org 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 Signed-off-by: Waiman Long --- 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,