Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp31344961rwd; Thu, 6 Jul 2023 20:37:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlGtl2dIdbmrcTMDJfqjJ9rfyb4nh3i0/mNDx5pyJYSBal3EkxemGXS7HnjOvbFvVlXFOd18 X-Received: by 2002:a05:6830:22d3:b0:6b7:cb42:8e17 with SMTP id q19-20020a05683022d300b006b7cb428e17mr3733077otc.4.1688701057098; Thu, 06 Jul 2023 20:37:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688701057; cv=none; d=google.com; s=arc-20160816; b=hL+mMFsg60ueMqiPXvz9G3PSIv4Dl66aU+4naA3LZrF8upwUQofn9Fa3fwEPYNwOya lqhPxmj88iNlaIPPZri+LxzcF6XVBPEehirqVz2CT3GCDf//S6cNUzy8NWZ5aty8PBt4 OR0+8qAjyaZKcE923WE8Jh4UmtHVDLwaoI4FxleACwxiLiSazpPECwQqsC7zuVrsNgjf 4iM1OvqVG7naBQkpE7KUrqRGa5G/ic1IalRcVbiyH0fiU04NnpWfDUFgmudUbQcB3e/V tHhgxWdwsRJ54kul7CsJJSXXuGHNblxgPqqNIIcQYGPDJnmQ8EnWS14hB3UAVHgAc6M8 mEsg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=0egYKMEcbrgPTu3fHUybjvHf3qI/9GmT0C6QxaOBwSg=; fh=yzFK41VSXRhpZC6SunRALAWQIdf3xU6ABFvpzQR/KFg=; b=wNpaQiaufuWvw15hegznqj1mX+jk0DSJzCPpyN318tsDBRe4KEa8ZjjZw+/2HMuSyV ycLSNtf4Sn13zfgL11d9GlskXnSlzP20yCEP682wjmIlGKswYnTV677aHwPRtDDBRv9w z3oicphqFXPcg/TotGb+ZyzZTLgl16n8dx7EHl6eoa6PlqeOVabVjlrIiRMIYhUF1r+s KGFjUkHQfW0w9pR8fmdL63L2fbJxwIVKnWTTveFVce/JZFVhgwcjG1kjJIy77oYF/LJc Z9mfJnhYmWPvaEpbY7mTJRzhA+Xd7fYKYSu4plYmLkFPDcxZpNl/bo3gSiwT92qec1I1 Y67A== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a635609000000b0055ace8caef7si2853899pgb.610.2023.07.06.20.37.23; Thu, 06 Jul 2023 20:37:37 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231652AbjGGDSQ (ORCPT + 99 others); Thu, 6 Jul 2023 23:18:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230159AbjGGDSN (ORCPT ); Thu, 6 Jul 2023 23:18:13 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5827E1BFC; Thu, 6 Jul 2023 20:18:12 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4Qxz9r5JxPz4f3wR2; Fri, 7 Jul 2023 11:18:08 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.127.227]) by APP4 (Coremail) with SMTP id gCh0CgD3mp7vg6dk0X+NNQ--.406S6; Fri, 07 Jul 2023 11:18:09 +0800 (CST) From: Zhong Jinghua To: josef@toxicpanda.com, axboe@kernel.dk Cc: linux-block@vger.kernel.org, nbd@other.debian.org, linux-kernel@vger.kernel.org, zhongjinghua@huaweicloud.com, yi.zhang@huawei.com, yukuai3@huawei.com Subject: [PATCH -next 2/3] nbd: factor out a helper to get nbd_config without holding 'config_lock' Date: Fri, 7 Jul 2023 11:15:35 +0800 Message-Id: <20230707031536.666482-3-zhongjinghua@huaweicloud.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230707031536.666482-1-zhongjinghua@huaweicloud.com> References: <20230707031536.666482-1-zhongjinghua@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgD3mp7vg6dk0X+NNQ--.406S6 X-Coremail-Antispam: 1UD129KBjvJXoWxZr1xJr47Cr45CF45Ar1kZrb_yoW5tFykpF 4UAFW5KrWUGF45uFWv9397WFnxtwn2gFyxGry7G3ZYvF9xArWakF1kK34xAa17Gr97JF45 JFWrWFySka47KrUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9m14x267AKxVWrJVCq3wAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87I2jVAFwI0_Jryl82xGYIkIc2 x26xkF7I0E14v26r4j6ryUM28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48ve4kI8wA2z4x0 Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJw A2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x0267AKxVW0oVCq3wAS 0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2 IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0 Y48IcxkI7VAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwCF04k20xvY0x0EwIxGrwCFx2 IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v2 6r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67 AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IY s7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr 0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x0JUc6pPUUUUU= X-CM-SenderInfo: x2kr0wpmlqwxtxd6x35dzhxuhorxvhhfrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE 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 From: Zhong Jinghua There are no functional changes, just to make code cleaner and prepare to fix null-ptr-dereference while accessing 'nbd->config'. Signed-off-by: Zhong Jinghua --- drivers/block/nbd.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index cd6d78914954..7186a9a49445 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c @@ -393,6 +393,14 @@ static u32 req_to_nbd_cmd_type(struct request *req) } } +static struct nbd_config *nbd_get_config_unlocked(struct nbd_device *nbd) +{ + if (refcount_inc_not_zero(&nbd->config_refs)) + return nbd->config; + + return NULL; +} + static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req) { struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req); @@ -407,13 +415,13 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req) return BLK_EH_DONE; } - if (!refcount_inc_not_zero(&nbd->config_refs)) { + config = nbd_get_config_unlocked(nbd); + if (!config) { cmd->status = BLK_STS_TIMEOUT; __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags); mutex_unlock(&cmd->lock); goto done; } - config = nbd->config; if (config->num_connections > 1 || (config->num_connections == 1 && nbd->tag_set.timeout)) { @@ -975,12 +983,12 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index) struct nbd_sock *nsock; int ret; - if (!refcount_inc_not_zero(&nbd->config_refs)) { + config = nbd_get_config_unlocked(nbd); + if (!config) { dev_err_ratelimited(disk_to_dev(nbd->disk), "Socks array is empty\n"); return -EINVAL; } - config = nbd->config; if (index >= config->num_connections) { dev_err_ratelimited(disk_to_dev(nbd->disk), @@ -1556,6 +1564,7 @@ static int nbd_alloc_and_init_config(struct nbd_device *nbd) static int nbd_open(struct block_device *bdev, fmode_t mode) { struct nbd_device *nbd; + struct nbd_config *config; int ret = 0; mutex_lock(&nbd_index_mutex); @@ -1568,7 +1577,9 @@ static int nbd_open(struct block_device *bdev, fmode_t mode) ret = -ENXIO; goto out; } - if (!refcount_inc_not_zero(&nbd->config_refs)) { + + config = nbd_get_config_unlocked(nbd); + if (!config) { mutex_lock(&nbd->config_lock); if (refcount_inc_not_zero(&nbd->config_refs)) { mutex_unlock(&nbd->config_lock); @@ -1584,7 +1595,7 @@ static int nbd_open(struct block_device *bdev, fmode_t mode) mutex_unlock(&nbd->config_lock); if (max_part) set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); - } else if (nbd_disconnected(nbd->config)) { + } else if (nbd_disconnected(config)) { if (max_part) set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state); } @@ -2194,7 +2205,8 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) } mutex_unlock(&nbd_index_mutex); - if (!refcount_inc_not_zero(&nbd->config_refs)) { + config = nbd_get_config_unlocked(nbd); + if (!config) { dev_err(nbd_to_dev(nbd), "not configured, cannot reconfigure\n"); nbd_put(nbd); @@ -2202,7 +2214,6 @@ static int nbd_genl_reconfigure(struct sk_buff *skb, struct genl_info *info) } mutex_lock(&nbd->config_lock); - config = nbd->config; if (!test_bit(NBD_RT_BOUND, &config->runtime_flags) || !nbd->pid) { dev_err(nbd_to_dev(nbd), -- 2.31.1