Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7141820rwb; Tue, 6 Dec 2022 01:34:01 -0800 (PST) X-Google-Smtp-Source: AA0mqf6RKOmeDc9VWf2Fz+aY1DvD3K/bYt6gD3RueG5VR869PnWqR2nk9aeN8acM7zmpIWHlvTqq X-Received: by 2002:a17:902:e311:b0:189:c7f1:c2a1 with SMTP id q17-20020a170902e31100b00189c7f1c2a1mr14563540plc.141.1670319241024; Tue, 06 Dec 2022 01:34:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670319241; cv=none; d=google.com; s=arc-20160816; b=PzyiyfwhOJfb6XWziH1Kj5hJNFpb/uYTOB7upJhK0XGUvcwU8DHRZgq+eONkJpY8EG bJ5QNusPcGE66g6pV3wRuR/YlQSa1Som1tFKvgh26MV+69+YgwmshuKeJPToGz7iBT8W P3QQa5v6r9ZLHXIoQQQ4BRHzjS9oQo6VMb4ObmVYReZHVAh7O98/M4ztEcR+seQzY7bQ Y3tzx5K2DRy9VlrwkNSeOtIRk7h7U0UUdKvo32FuXRJ8BxKSuQvrPaIYb5oKbJZYwTVG BliiDDp1iiwg4JHkeRbOW9y5x0IYynCkF4LLWUPRdY4K0qaCEqbpdJDcpLwVWWTibM5L dGXw== 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=T95Q9g+rJho/VkjhH51R9IDvSCCW1Ha84HigxlmeQGGCO+MiXDpMPrAnCk1D+y37vB YlMU3PCjTuBMBF/huOTEXpGJeuVbcOZoWkl6/j6YCslA70oZhQrkowaBC6WM4TgUIOIa 7beeXH0JIZ5Fl1zdzg1HcdjYxbLYyuLgctcQVVP3LVjz810dvtsRIbYVrXyQwWRrO0xx oxW3EmZaFMRZuzfhiaRDoxfpVlW953t1hjSBRA8+XC5E/wi2zqiT5p3poqEcSCDSEStf 3WopZ9Aj3eINespoED/QOwrAsQNp3zIA109Fm9snppng6bDVinNN7/SBtqKFCQxwaVNh 6qsw== 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 q29-20020a63f95d000000b00477dc113782si17131613pgk.600.2022.12.06.01.33.50; Tue, 06 Dec 2022 01:34:01 -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 S234153AbiLFIsx (ORCPT + 79 others); Tue, 6 Dec 2022 03:48:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230230AbiLFIsq (ORCPT ); Tue, 6 Dec 2022 03:48:46 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0186EC16 for ; Tue, 6 Dec 2022 00:48:44 -0800 (PST) Received: from kwepemm600002.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4NRDVp17v9zqSvj; Tue, 6 Dec 2022 16:44:34 +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:48:42 +0800 From: Zhong Jinghua To: , , CC: , , , , Subject: [PATCH-next] block: fix null-deref in percpu_ref_put Date: Tue, 6 Dec 2022 17:09:39 +0800 Message-ID: <20221206090939.871239-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: dggems701-chm.china.huawei.com (10.3.19.178) 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