Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7197827rwb; Tue, 6 Dec 2022 02:30:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf4tccNwO29g4BtnGGOkUAYIDFJJTCB3Iq3R6zTFlWXNzqZ8WGH3Mcn1BYaYRHprU2zyBB0O X-Received: by 2002:a17:907:9517:b0:7c0:f118:db8b with SMTP id ew23-20020a170907951700b007c0f118db8bmr7717973ejc.664.1670322652963; Tue, 06 Dec 2022 02:30:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670322652; cv=none; d=google.com; s=arc-20160816; b=1FjvDc1Z4X6m2n61FviVKav9LTvsqViihS3bds8+Y0PI8NtH3SF0qzFfup16vjEIdQ 4Apn2BlE9lqJmLtQRtPgXGNtR2GXoXQUnplxNEv+F3RfiF+uOVBiUyBAg+Ny9VyAkacC JjELrZdk0AwgldY6Nsstz5evdIAgd/gkCO/cLOknaCxDoT3NUB3TNYHuFG8ZdJ1Nq360 vZ0ybDnHW27Js7+6iNRO8ggG/Dse6j/50LRAeNLN7PnB6lP4bZKzMc48Ar9KdzfuK7Ls 4wXUV6acH6VRk2xEPeR9aMo1zjRk/sDVuJbAm+OW9W09ri3DJaMrNVgWjSNjpsUKFInh 5AlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=6cQcJI2Oi/rTtc12lLfQW3G92OVbPXC3Mi0U4uLBYE4=; b=MA0vCvjoIRi9c1RGxUZ9QZRG36SWWzIG3AB9wtULz8/d1eDWq7el1cdcN9mIZphZZn Tbj4OLuZJzW7ctvic7p8HaEY062n4CVBs+CKP+1A+3yVfOeyHuxyFb8oOJ+A6+XA88jf +/TuMMccS1y3lMBL2agq5+tV5pANY5AJDVs7Ya6GRMYTQqilojdWTGjqEQLsl8A3pmfV o1FNZ7Wk21TC+HWaqCcNgl5mREy2vAZoh2MaZ54wFzaE8FcCLoRAKbnmP7FAzQH9ArCH qbgqed9Y3NO6EusDogifYEYDw7wkT9ebzFuilxYs3TBclEfC1hm3G8lsRZxc/x6pKXIn d9yg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sh31-20020a1709076e9f00b007c0fa82837esi3727867ejc.222.2022.12.06.02.30.32; Tue, 06 Dec 2022 02:30:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232839AbiLFIlR (ORCPT + 79 others); Tue, 6 Dec 2022 03:41:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232321AbiLFIlO (ORCPT ); Tue, 6 Dec 2022 03:41:14 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E204A2739 for ; Tue, 6 Dec 2022 00:41:13 -0800 (PST) Received: from kwepemm600002.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NRDPy2x08zQj6p; Tue, 6 Dec 2022 16:40:22 +0800 (CST) Received: from localhost.localdomain (10.175.127.227) by kwepemm600002.china.huawei.com (7.193.23.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 6 Dec 2022 16:41:10 +0800 From: Zhong Jinghua To: , , CC: , , , Subject: [PATCH-next] block: fix null-deref in percpu_ref_put Date: Tue, 6 Dec 2022 17:02:08 +0800 Message-ID: <20221206090208.845185-1-zhongjinghua@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600002.china.huawei.com (7.193.23.29) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A problem was find in stable 5.10 and the root cause of it like below. In the use of q_usage_counter of request_queue, blk_cleanup_queue using "wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter))" to wait q_usage_counter becoming zero. however, if the q_usage_counter becoming zero quickly, and percpu_ref_exit will execute and ref->data will be freed, maybe another process will cause a null-defef problem like below: CPU0 CPU1 blk_mq_destroy_queue blk_freeze_queue blk_mq_freeze_queue_wait scsi_end_request percpu_ref_get ... percpu_ref_put atomic_long_sub_and_test blk_put_queue kobject_put kref_put blk_release_queue percpu_ref_exit ref->data -> NULL ref->data->release(ref) -> null-deref As suggested by Ming Lei, fix it by getting the release method before the referebce count is minus 0. Suggested-by: Ming Lei Signed-off-by: Zhong Jinghua --- include/linux/percpu-refcount.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index d73a1c08c3e3..11e717c95acb 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -331,8 +331,11 @@ static inline void percpu_ref_put_many(struct percpu_ref *ref, unsigned long nr) if (__ref_is_percpu(ref, &percpu_count)) this_cpu_sub(*percpu_count, nr); - else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) - ref->data->release(ref); + else { + percpu_ref_func_t *release = ref->data->release; + if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) + release(ref); + } rcu_read_unlock(); } -- 2.31.1