From patchwork Fri May 6 01:58:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Liu X-Patchwork-Id: 12840441 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEA7FC433FE for ; Fri, 6 May 2022 01:41:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 849AF6B0071; Thu, 5 May 2022 21:41:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AE7D6B0075; Thu, 5 May 2022 21:41:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5163E6B0078; Thu, 5 May 2022 21:41:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 345756B0071 for ; Thu, 5 May 2022 21:41:47 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0DCD320BB4 for ; Fri, 6 May 2022 01:41:47 +0000 (UTC) X-FDA: 79433616654.27.1B1DF23 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf23.hostedemail.com (Postfix) with ESMTP id 4CACB14008F for ; Fri, 6 May 2022 01:41:34 +0000 (UTC) Received: from kwepemi500011.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4KvYF16c8RzhYq4; Fri, 6 May 2022 09:41:09 +0800 (CST) Received: from kwepemm600017.china.huawei.com (7.193.23.234) by kwepemi500011.china.huawei.com (7.221.188.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 6 May 2022 09:41:32 +0800 Received: from localhost.localdomain (10.175.112.125) by kwepemm600017.china.huawei.com (7.193.23.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 6 May 2022 09:41:30 +0800 From: Peng Liu To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH 1/2] include/linux/nodemask.h: create node_available() helper Date: Fri, 6 May 2022 01:58:00 +0000 Message-ID: <20220506015801.757918-2-liupeng256@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220506015801.757918-1-liupeng256@huawei.com> References: <20220506015801.757918-1-liupeng256@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600017.china.huawei.com (7.193.23.234) X-CFilter-Loop: Reflected X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4CACB14008F X-Stat-Signature: gpa7win95o35g8iugjpmocn1fd6c5etf Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf23.hostedemail.com: domain of liupeng256@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=liupeng256@huawei.com X-Rspam-User: X-HE-Tag: 1651801294-649463 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Lots of code dose node != NUMA_NO_NODE && !node_online(node) or node == NUMA_NO_NODE || node_online(node) so create node_available to do this to simplify code. Signed-off-by: Peng Liu --- arch/ia64/hp/common/sba_iommu.c | 2 +- arch/x86/pci/acpi.c | 2 +- drivers/acpi/arm64/iort.c | 2 +- drivers/pci/pci-sysfs.c | 2 +- include/linux/nodemask.h | 3 +++ mm/mempolicy.c | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/arch/ia64/hp/common/sba_iommu.c b/arch/ia64/hp/common/sba_iommu.c index 8ad6946521d8..da3a010bb5fb 100644 --- a/arch/ia64/hp/common/sba_iommu.c +++ b/arch/ia64/hp/common/sba_iommu.c @@ -1969,7 +1969,7 @@ sba_map_ioc_to_node(struct ioc *ioc, acpi_handle handle) unsigned int node; node = acpi_get_node(handle); - if (node != NUMA_NO_NODE && !node_online(node)) + if (!node_available(node)) node = NUMA_NO_NODE; ioc->node = node; diff --git a/arch/x86/pci/acpi.c b/arch/x86/pci/acpi.c index 052f1d78a562..d4909daa44d9 100644 --- a/arch/x86/pci/acpi.c +++ b/arch/x86/pci/acpi.c @@ -254,7 +254,7 @@ static int pci_acpi_root_get_node(struct acpi_pci_root *root) dev_info(&device->dev, FW_BUG "no _PXM; falling back to node %d from hardware (may be inconsistent with ACPI node numbers)\n", node); } - if (node != NUMA_NO_NODE && !node_online(node)) + if (!node_available(node)) node = NUMA_NO_NODE; return node; diff --git a/drivers/acpi/arm64/iort.c b/drivers/acpi/arm64/iort.c index f2f8f05662de..bdf690010b97 100644 --- a/drivers/acpi/arm64/iort.c +++ b/drivers/acpi/arm64/iort.c @@ -1251,7 +1251,7 @@ static int __init arm_smmu_v3_set_proximity(struct device *dev, if (smmu->flags & ACPI_IORT_SMMU_V3_PXM_VALID) { int dev_node = pxm_to_node(smmu->pxm); - if (dev_node != NUMA_NO_NODE && !node_online(dev_node)) + if (!node_available(dev_node)) return -EINVAL; set_dev_node(dev, dev_node); diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c263ffc5884a..502490d26c1d 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -344,7 +344,7 @@ static ssize_t numa_node_store(struct device *dev, if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) return -EINVAL; - if (node != NUMA_NO_NODE && !node_online(node)) + if (!node_available(node)) return -EINVAL; add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h index 567c3ddba2c4..591201a1e646 100644 --- a/include/linux/nodemask.h +++ b/include/linux/nodemask.h @@ -70,6 +70,7 @@ * * int node_online(node) Is some node online? * int node_possible(node) Is some node possible? + * int node_available(node) Is some node available(online or NUMA_NO_NODE)? * * node_set_online(node) set bit 'node' in node_online_map * node_set_offline(node) clear bit 'node' in node_online_map @@ -510,6 +511,8 @@ static inline int node_random(const nodemask_t *mask) #define num_possible_nodes() num_node_state(N_POSSIBLE) #define node_online(node) node_state((node), N_ONLINE) #define node_possible(node) node_state((node), N_POSSIBLE) +#define node_available(node) \ + ((node) == NUMA_NO_NODE || node_state((node), N_ONLINE)) #define for_each_node(node) for_each_node_state(node, N_POSSIBLE) #define for_each_online_node(node) for_each_node_state(node, N_ONLINE) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 8c74107a2b15..7a31b006c5e8 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -141,7 +141,7 @@ int numa_map_to_online_node(int node) { int min_dist = INT_MAX, dist, n, min_node; - if (node == NUMA_NO_NODE || node_online(node)) + if (node_available(node)) return node; min_node = node; From patchwork Fri May 6 01:58:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Liu X-Patchwork-Id: 12840442 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B29DFC433F5 for ; Fri, 6 May 2022 01:41:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4083C6B0074; Thu, 5 May 2022 21:41:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B9B06B0075; Thu, 5 May 2022 21:41:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 257AD6B0078; Thu, 5 May 2022 21:41:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 108246B0074 for ; Thu, 5 May 2022 21:41:52 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id E3A8E80848 for ; Fri, 6 May 2022 01:41:51 +0000 (UTC) X-FDA: 79433616822.19.C6A2A19 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by imf20.hostedemail.com (Postfix) with ESMTP id CDBD21C009E for ; Fri, 6 May 2022 01:41:43 +0000 (UTC) Received: from kwepemi100020.china.huawei.com (unknown [172.30.72.56]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4KvYDJ36N6z1JBs2; Fri, 6 May 2022 09:40:32 +0800 (CST) Received: from kwepemm600017.china.huawei.com (7.193.23.234) by kwepemi100020.china.huawei.com (7.221.188.48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 6 May 2022 09:41:37 +0800 Received: from localhost.localdomain (10.175.112.125) by kwepemm600017.china.huawei.com (7.193.23.234) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 6 May 2022 09:41:35 +0800 From: Peng Liu To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH 2/2] null_blk: fix wrong use of nr_online_nodes Date: Fri, 6 May 2022 01:58:01 +0000 Message-ID: <20220506015801.757918-3-liupeng256@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220506015801.757918-1-liupeng256@huawei.com> References: <20220506015801.757918-1-liupeng256@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.112.125] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm600017.china.huawei.com (7.193.23.234) X-CFilter-Loop: Reflected X-Stat-Signature: n3nc941wekp7mrrt3br8c9rxb8fwgm81 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: CDBD21C009E Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of liupeng256@huawei.com designates 45.249.212.255 as permitted sender) smtp.mailfrom=liupeng256@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com X-Rspam-User: X-HE-Tag: 1651801303-54288 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Certain systems are designed to have sparse/discontiguous nodes, a valid node may be greater than nr_online_nodes. So, the use of "nid >= nr_online_nodes" to judge if a node is online is wrong. Node id is a basic parameter of the system, a user-configured node must be checked as early as possible. Otherwise, it may cause panic when calling some vulnerable functions such as node_online which will cause panic if a very big node is received. Check g_home_node once users config it, and use node_available to make node-checking compatible with sparse/discontiguous nodes. Fixes: 7ff684a683d7 ("null_blk: prevent crash from bad home_node value") Signed-off-by: Peng Liu Suggested-by: Davidlohr Bueso --- drivers/block/null_blk/main.c | 45 ++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c index 05b1120e6623..995348d6e7e7 100644 --- a/drivers/block/null_blk/main.c +++ b/drivers/block/null_blk/main.c @@ -97,7 +97,33 @@ module_param_named(poll_queues, g_poll_queues, int, 0444); MODULE_PARM_DESC(poll_queues, "Number of IOPOLL submission queues"); static int g_home_node = NUMA_NO_NODE; -module_param_named(home_node, g_home_node, int, 0444); + +static int null_param_store_val(const char *str, int *val, int min, int max) +{ + int ret, new_val; + + ret = kstrtoint(str, 10, &new_val); + if (ret) + return -EINVAL; + + if (new_val < min || new_val > max) + return -EINVAL; + + *val = new_val; + return 0; +} + +static int null_set_home_node(const char *str, const struct kernel_param *kp) +{ + return null_param_store_val(str, &g_home_node, 0, MAX_NUMNODES - 1); +} + +static const struct kernel_param_ops null_home_node_param_ops = { + .set = null_set_home_node, + .get = param_get_int, +}; + +device_param_cb(home_node, &null_home_node_param_ops, &g_home_node, 0444); MODULE_PARM_DESC(home_node, "Home node for the device"); #ifdef CONFIG_BLK_DEV_NULL_BLK_FAULT_INJECTION @@ -120,21 +146,6 @@ MODULE_PARM_DESC(init_hctx, "Fault injection to fail hctx init. init_hctx= max) - return -EINVAL; - - *val = new_val; - return 0; -} - static int null_set_queue_mode(const char *str, const struct kernel_param *kp) { return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ); @@ -2107,7 +2118,7 @@ static int __init null_init(void) g_max_sectors = BLK_DEF_MAX_SECTORS; } - if (g_home_node != NUMA_NO_NODE && g_home_node >= nr_online_nodes) { + if (!node_available(g_home_node)) { pr_err("invalid home_node value\n"); g_home_node = NUMA_NO_NODE; }