From patchwork Thu Sep 8 07:58:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12969729 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 E43DEC38145 for ; Thu, 8 Sep 2022 07:58:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F2566B0072; Thu, 8 Sep 2022 03:58:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A2706B0073; Thu, 8 Sep 2022 03:58:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3431C8D0002; Thu, 8 Sep 2022 03:58:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 23ACC6B0072 for ; Thu, 8 Sep 2022 03:58:49 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E76D4A1A1A for ; Thu, 8 Sep 2022 07:58:48 +0000 (UTC) X-FDA: 79888166736.22.059B6E0 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf20.hostedemail.com (Postfix) with ESMTP id B7A0B1C009A for ; Thu, 8 Sep 2022 07:58:47 +0000 (UTC) Received: by mail-pf1-f172.google.com with SMTP id b23so545191pfp.9 for ; Thu, 08 Sep 2022 00:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=dN1vu9YkPP2qvJgARkUVS3Lj2dJbT8tyN8H4FWEm0MA=; b=euS70EXnFyEuCkfB97RoVTfQScHu6QidJEayfqWFXllx6tKNbBDs8Aqmkx9TtVL12O TTq6BHiy3Isdo97zquLkpB1THGASkfqZ3BWxO2AqRvTdP2cR6xiMaQejSbm/PoPX3m0G OBtLuoI8XsX/QCbYSuCERMB/p3HDnuO6xd7/z5l9DGjkPVJJ8FLdKvSzAm8l75rggPPQ Gt9I+4k7JbdvuQvFMcer0YtnXbUSAbUaQDiwQWnjsVUd1G4KFasZbLWn3PbuSF3wayCO p5i4SHWeezrRST417Vp3epc7DIaEdTiwpGjgQygjXj5EgLWCMVGVf7lWqebLHYrIOaWd kKAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=dN1vu9YkPP2qvJgARkUVS3Lj2dJbT8tyN8H4FWEm0MA=; b=u5U3WreL2Wqs3VT31jNYFLjo+/rMOpCfNXqpaQxQ/zuY5dCuSpLYMrc9jh5Et9Jx/t mNsKAO5bwJiAoSqitaOfWjz6I2+bw0a5OVrCpmEGX1zqPRJVfIGdgmUGAiiIfvAsgYy3 YHBqXwPB4JDw1nNhSGADwgrYpXNb4MV56OQknkSYzwstPIGRzalqgmRigZTMLj+WQS+R 7MzBhvZJsDnxgKixWXGUXUm6livTxx+1ah/aWIcEbvdoJNU6EJVK8JD+Zzn8GYhs3Xbb Rm19D+bjviYamucqaHO0A33HrInxDXLaHCz+x4kUE9Oox4jpOgT1Ip6Vp9MUaMP1Nn2F qDGg== X-Gm-Message-State: ACgBeo1u9sN8VZ0+wZ5CBORzdPcErAAC+AoX7w2KMK7CGnyDIOsUvcyd 3jbBY+qFf2y3KbNJE10t1VUuFA== X-Google-Smtp-Source: AA6agR49G6QcDPmAfuup5cvjmfzK6UHcAZrEeHq1TcrNSokGuLivx7bOYj8Y++Xb1F3n0B7DFKo9zA== X-Received: by 2002:a05:6a00:1650:b0:52f:20d6:e858 with SMTP id m16-20020a056a00165000b0052f20d6e858mr7863601pfc.36.1662623926544; Thu, 08 Sep 2022 00:58:46 -0700 (PDT) Received: from PXLDJ45XCM.bytedance.net ([139.177.225.231]) by smtp.gmail.com with ESMTPSA id p12-20020a170902bd0c00b001745919b197sm13629980pls.243.2022.09.08.00.58.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Sep 2022 00:58:45 -0700 (PDT) From: Muchun Song To: gregkh@linuxfoundation.org, rafael@kernel.org, mike.kravetz@oracle.com, songmuchun@bytedance.com, akpm@linux-foundation.org, osalvador@suse.de, david@redhat.com, ying.huang@intel.com, aneesh.kumar@linux.ibm.com, rientjes@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, muchun.song@linux.dev Subject: [PATCH v3] mm: hugetlb: eliminate memory-less nodes handling Date: Thu, 8 Sep 2022 15:58:26 +0800 Message-Id: <20220908075826.37094-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.1 (Apple Git-133) MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662623928; a=rsa-sha256; cv=none; b=6QOPJ70fp9G1W0na/Zz79ODYxaAFJdPRpAJbwH2lvQzGudSiLfmDGjPWmpmZC0+0RqIbSz 3K7VRvQNQp6aeNyEUMTdhovaXZBBDxurhHhf9nHp6D0cr4y3aGtuv6yJiKw0tvslh81tlX dHhOy+cTqblEIjhwtDtmpqxhHkthISM= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=euS70EXn; spf=pass (imf20.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662623928; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=dN1vu9YkPP2qvJgARkUVS3Lj2dJbT8tyN8H4FWEm0MA=; b=EsglL7JEIgsT15BrVq8vib/dZvLE1CD6IBBdysxj+uVVLrXPvu2T3gUdThLpJtoQNX38+k +c23+ZaetB/0ns7Whw1Dr5zRW4sYX08BtkiA7WIBSZmARllxS/cY46Dykz+4Pa6Diyh5Pm 3U5T3e1+bID77qNx/wR/fJIRn7jnBhU= X-Stat-Signature: hca5nkr4m5x4jpfdzofcoeszohuaa16r X-Rspamd-Queue-Id: B7A0B1C009A X-Rspamd-Server: rspam11 X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=euS70EXn; spf=pass (imf20.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-HE-Tag: 1662623927-902938 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: The memory-notify-based approach aims to handle meory-less nodes, however, it just adds the complexity of code as pointed by David in thread [1]. The handling of memory-less nodes is introduced by commit 4faf8d950ec4 ("hugetlb: handle memory hot-plug events"). From its commit message, we cannot find any necessity of handling this case. So, we can simply register/unregister sysfs entries in register_node/unregister_node to simlify the code. BTW, hotplug callback added because in hugetlb_register_all_nodes() we register sysfs nodes only for N_MEMORY nodes, seeing commit 9b5e5d0fdc91, which said it was a preparation for handling memory-less nodes via memory hotplug. Since we want to remove memory hotplug, so make sure we only register per-node sysfs for online (N_ONLINE) nodes in hugetlb_register_all_nodes(). https://lore.kernel.org/linux-mm/60933ffc-b850-976c-78a0-0ee6e0ea9ef0@redhat.com/ [1] Suggested-by: David Hildenbrand Signed-off-by: Muchun Song Acked-by: David Hildenbrand --- v3: - Fix 'struct node' is not declared reported by LTP. v2: - Move declaration of function related to hugetlb to hugetlb.h (David). - Introduce hugetlb_sysfs_initialized() and call it from hugetlb_sysfs_init() (David). - Move hugetlb_register_all_nodes() into hugetlb_sysfs_init() (David). - Fix implicit-function-declaration reported by LKP. - Register per-node sysfs for online (N_ONLINE) nodes instead of N_MEMORY (Aneesh). drivers/base/node.c | 8 +++-- include/linux/hugetlb.h | 14 +++++++++ mm/hugetlb.c | 81 ++++++++++++++++++++++--------------------------- 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/drivers/base/node.c b/drivers/base/node.c index ed391cb09999..80b1e91b9608 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -20,6 +20,7 @@ #include #include #include +#include static struct bus_type node_subsys = { .name = "node", @@ -608,10 +609,12 @@ static int register_node(struct node *node, int num) node->dev.groups = node_dev_groups; error = device_register(&node->dev); - if (error) + if (error) { put_device(&node->dev); - else + } else { + hugetlb_register_node(node); compaction_register_node(node); + } return error; } @@ -625,6 +628,7 @@ static int register_node(struct node *node, int num) */ void unregister_node(struct node *node) { + hugetlb_unregister_node(node); compaction_unregister_node(node); node_remove_accesses(node); node_remove_caches(node); diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 852f911d676e..2d98d0ac4a48 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -16,6 +16,7 @@ struct ctl_table; struct user_struct; struct mmu_gather; +struct node; #ifndef is_hugepd typedef struct { unsigned long pd; } hugepd_t; @@ -967,6 +968,11 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, } #endif +#ifdef CONFIG_NUMA +void hugetlb_register_node(struct node *node); +void hugetlb_unregister_node(struct node *node); +#endif + #else /* CONFIG_HUGETLB_PAGE */ struct hstate {}; @@ -1141,6 +1147,14 @@ static inline void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) { } + +static inline void hugetlb_register_node(struct node *node) +{ +} + +static inline void hugetlb_unregister_node(struct node *node) +{ +} #endif /* CONFIG_HUGETLB_PAGE */ static inline spinlock_t *huge_pte_lock(struct hstate *h, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d0617d64d718..06ac469582da 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3880,24 +3880,14 @@ static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent, return 0; } -static void __init hugetlb_sysfs_init(void) -{ - struct hstate *h; - int err; - - hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); - if (!hugepages_kobj) - return; +#ifdef CONFIG_NUMA +static bool hugetlb_sysfs_initialized __ro_after_init; - for_each_hstate(h) { - err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, - hstate_kobjs, &hstate_attr_group); - if (err) - pr_err("HugeTLB: Unable to add hstate %s", h->name); - } +static inline void hugetlb_mark_sysfs_initialized(void) +{ + hugetlb_sysfs_initialized = true; } -#ifdef CONFIG_NUMA /* * node_hstate/s - associate per node hstate attributes, via their kobjects, @@ -3953,7 +3943,7 @@ static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) * Unregister hstate attributes from a single node device. * No-op if no hstate attributes attached. */ -static void hugetlb_unregister_node(struct node *node) +void hugetlb_unregister_node(struct node *node) { struct hstate *h; struct node_hstate *nhs = &node_hstates[node->dev.id]; @@ -3983,19 +3973,22 @@ static void hugetlb_unregister_node(struct node *node) * Register hstate attributes for a single node device. * No-op if attributes already registered. */ -static int hugetlb_register_node(struct node *node) +void hugetlb_register_node(struct node *node) { struct hstate *h; struct node_hstate *nhs = &node_hstates[node->dev.id]; int err; + if (!hugetlb_sysfs_initialized) + return; + if (nhs->hugepages_kobj) - return 0; /* already allocated */ + return; /* already allocated */ nhs->hugepages_kobj = kobject_create_and_add("hugepages", &node->dev.kobj); if (!nhs->hugepages_kobj) - return -ENOMEM; + return; for_each_hstate(h) { err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj, @@ -4005,28 +3998,9 @@ static int hugetlb_register_node(struct node *node) pr_err("HugeTLB: Unable to add hstate %s for node %d\n", h->name, node->dev.id); hugetlb_unregister_node(node); - return -ENOMEM; + break; } } - return 0; -} - -static int __meminit hugetlb_memory_callback(struct notifier_block *self, - unsigned long action, void *arg) -{ - int ret = 0; - struct memory_notify *mnb = arg; - int nid = mnb->status_change_nid; - - if (nid == NUMA_NO_NODE) - return NOTIFY_DONE; - - if (action == MEM_GOING_ONLINE) - ret = hugetlb_register_node(node_devices[nid]); - else if (action == MEM_CANCEL_ONLINE || action == MEM_OFFLINE) - hugetlb_unregister_node(node_devices[nid]); - - return notifier_from_errno(ret); } /* @@ -4038,11 +4012,8 @@ static void __init hugetlb_register_all_nodes(void) { int nid; - get_online_mems(); - hotplug_memory_notifier(hugetlb_memory_callback, 0); - for_each_node_state(nid, N_MEMORY) + for_each_online_node(nid) hugetlb_register_node(node_devices[nid]); - put_online_mems(); } #else /* !CONFIG_NUMA */ @@ -4056,8 +4027,31 @@ static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp) static void hugetlb_register_all_nodes(void) { } +static inline void hugetlb_mark_sysfs_initialized(void) +{ +} #endif +static void __init hugetlb_sysfs_init(void) +{ + struct hstate *h; + int err; + + hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); + if (!hugepages_kobj) + return; + + for_each_hstate(h) { + err = hugetlb_sysfs_add_hstate(h, hugepages_kobj, + hstate_kobjs, &hstate_attr_group); + if (err) + pr_err("HugeTLB: Unable to add hstate %s", h->name); + } + + hugetlb_mark_sysfs_initialized(); + hugetlb_register_all_nodes(); +} + static int __init hugetlb_init(void) { int i; @@ -4112,7 +4106,6 @@ static int __init hugetlb_init(void) report_hugepages(); hugetlb_sysfs_init(); - hugetlb_register_all_nodes(); hugetlb_cgroup_file_init(); #ifdef CONFIG_SMP