From patchwork Thu Mar 31 06:56:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12796779 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 94643C433F5 for ; Thu, 31 Mar 2022 06:57:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF1A38D0001; Thu, 31 Mar 2022 02:57:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9FD16B0073; Thu, 31 Mar 2022 02:57:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C41918D0001; Thu, 31 Mar 2022 02:57:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id B5A7E6B0072 for ; Thu, 31 Mar 2022 02:57:32 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 782852430E for ; Thu, 31 Mar 2022 06:57:32 +0000 (UTC) X-FDA: 79303775544.13.5067C7D Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf01.hostedemail.com (Postfix) with ESMTP id 7D05D4000F for ; Thu, 31 Mar 2022 06:57:28 +0000 (UTC) Received: by mail-pj1-f49.google.com with SMTP id a16-20020a17090a6d9000b001c7d6c1bb13so2697674pjk.4 for ; Wed, 30 Mar 2022 23:57:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=I5exx/RB+d0Ig7pXuqKbMzRv7rQoprfE6EMWAxPn3vk=; b=VxsWEIotkC2VYiWWzOCyD1h39EI03hJn4gYoUq5zL+es6eKEi/fQPJEpTFiQhX51V9 eikNF5Q4nJX9kTCRRilvUSCkBYeRXTclsDYJrtTV3K7xEbiRG2R9cRz5ththfznMM3XC 66QKDnh3Cj3JDimXFfT7EUKNWhHjsObnnQynBYRDAdMdFdKt8UOEDbHAWZejxE0xs4rx snTFc7AtACXs3vBUkU+J5RON6gZUTOjKnm6p9ZnUqRBfbWaVAiLtTSaP8GfA32r9OqOS f7tm7oIJpihmrGFTzDO6n6HNvqTowM06YPD5GLVOL5OO1WiLcWlYeA+WAx20Xiu72BDc B/jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=I5exx/RB+d0Ig7pXuqKbMzRv7rQoprfE6EMWAxPn3vk=; b=CzhC2GiktuZI/W0S1Y0kv4VVOfSf023kWyauFoEoty18QAiteipR1Nw+MorNCR8zvE 7oOCxBX9RcEhJw+vvSZvfOeblMZb0+7L1HzIw0lRGjrs9aKi9+vww5D7ODuLAxtqbjEb GOal6UwnW2w2TzqUIFiRk67+xXjGTqOVWhGujIuDJO0ZWc0trZ/wZ+Eeq9YmfScd6gpq npD6aTzvND0QLf0f+e/KXoTrcfaiLp4056KzPW0im43iblagHaOBNibmiEVk89xYo0eF YRi8ok/WcjTrAeHiDjgNlN964rlVBazF2f6Z/bT9XZytSPt2v0z2UbLhTOHSCWdjNKSD tX3g== X-Gm-Message-State: AOAM532r2XiWfCYrkDjBh7mr9dAcngXdAR2lq/biJYkhChr3wSVi3mkm cYUUp4liaJTB/jl5Xo/iA71dxQ== X-Google-Smtp-Source: ABdhPJxVeMek9ygr1gEJ2F2+WMGJOZmPNv0PKkIoA3/xhuB910rYtwVdeKFiPVghs/O8sDNlYZOp5w== X-Received: by 2002:a17:90b:17c7:b0:1c7:c616:6eb0 with SMTP id me7-20020a17090b17c700b001c7c6166eb0mr4632866pjb.144.1648709847477; Wed, 30 Mar 2022 23:57:27 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d8-20020a056a00198800b004fab740dbe6sm26169846pfl.15.2022.03.30.23.57.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 23:57:27 -0700 (PDT) From: Muchun Song To: will@kernel.org, akpm@linux-foundation.org, david@redhat.com, bodeddub@amazon.com, osalvador@suse.de, mike.kravetz@oracle.com, rientjes@google.com, mark.rutland@arm.com, catalin.marinas@arm.com, james.morse@arm.com, 21cnbao@gmail.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v4 1/2] mm: hugetlb_vmemmap: introduce ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP Date: Thu, 31 Mar 2022 14:56:39 +0800 Message-Id: <20220331065640.5777-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 1acxbkhg1s45tq8i5hcz3s9ynqfengfb Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=VxsWEIot; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf01.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Rspamd-Queue-Id: 7D05D4000F X-HE-Tag: 1648709848-390110 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 feature of minimizing overhead of struct page associated with each HugeTLB page is implemented on x86_64, however, the infrastructure of this feature is already there, we could easily enable it for other architectures. Introduce ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP for other architectures to be easily enabled. Just select this config if they want to enable this feature. Suggested-by: Andrew Morton Signed-off-by: Muchun Song Reviewed-by: Anshuman Khandual --- arch/x86/Kconfig | 1 + fs/Kconfig | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 9f5bd41bf660..e69d42528542 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -119,6 +119,7 @@ config X86 select ARCH_WANTS_DYNAMIC_TASK_STRUCT select ARCH_WANTS_NO_INSTR select ARCH_WANT_HUGE_PMD_SHARE + select ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP if X86_64 select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANTS_THP_SWAP if X86_64 select ARCH_HAS_PARANOID_L1D_FLUSH diff --git a/fs/Kconfig b/fs/Kconfig index 6c7dc1387beb..f6db2af33738 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -245,9 +245,17 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +# +# Select this config option from the architecture Kconfig, if it is preferred +# to enable the feature of minimizing overhead of struct page associated with +# each HugeTLB page. +# +config ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP + bool + config HUGETLB_PAGE_FREE_VMEMMAP def_bool HUGETLB_PAGE - depends on X86_64 + depends on ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP depends on SPARSEMEM_VMEMMAP config HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON From patchwork Thu Mar 31 06:56:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12796780 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 D4BA5C433FE for ; Thu, 31 Mar 2022 06:57:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69EE56B0072; Thu, 31 Mar 2022 02:57:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64CF98D0002; Thu, 31 Mar 2022 02:57:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F0266B0074; Thu, 31 Mar 2022 02:57:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 403896B0072 for ; Thu, 31 Mar 2022 02:57:35 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 085C4113C for ; Thu, 31 Mar 2022 06:57:35 +0000 (UTC) X-FDA: 79303775670.05.DB3F16E Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by imf03.hostedemail.com (Postfix) with ESMTP id 81CAF2000B for ; Thu, 31 Mar 2022 06:57:34 +0000 (UTC) Received: by mail-pl1-f179.google.com with SMTP id i11so22608122plr.1 for ; Wed, 30 Mar 2022 23:57:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nwV4Ab67S/nV7/+Z8UUB2Pccmb87dn/jh1LTq8DOevs=; b=3uldU/51NiLauuIFjpb8KoMXAEQzooUmtBM+5WNgq+HOASj1ysBKKqLdUxcDZjII8e sW2SGz1PXTfYxU6uQ7qpHxFfVjbulGH/3t60h5zfmNUs9ixbyG6AKyvWJdsftzh+ZPdg +GRF37IXTqklfzEPR3VIC7YJM2amiEo3yQ8yXSwGmeK4SrMvwhT5vI1SYsFpwiK1B1EX HVP3BlTl5czOhwHnW4HTZNojmPKyolAs1gzDa6MaSkSR4/MD2DEg5xI9i1BmX9lk7GWi xkb2rPqLbFw7NcgZCgwSXvrE+Xr94sNaXh6vjCfNL1D3JWxpMfxqAP/Qj2c9L6M7RLLx 8Jew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nwV4Ab67S/nV7/+Z8UUB2Pccmb87dn/jh1LTq8DOevs=; b=ZR+dMIcPGNrSPN/S26RyNHIQB+/NMaCVMAGSp9W34sQZ+YAc3fAlYVbGuytWlM0uN8 k+191obgxoLdfDRbTrAsTYCZ8gFj1ggzT4aRI/LR1oQtDDnyQ+cstnZPv9AHg02ObCY2 Qm9Jm5ZaGmPH0v4w9PbNAi0a44wN3/H74vFJRPL8zDkI7iDAKmFlc5yeyut7fFodxmw0 e198sk3vPCJ9g0QIccsmu3UgFS3bEmytQLYYrmjr8eBVZseJ6/XoD9R56+mKtfMwmyos nbQL3gwjHXHSxlm05N3DlDd5wjgn00zx/yMeQRw6coPBsZz5sT1Ntiw7Nv57eegOG8vC 0A0A== X-Gm-Message-State: AOAM5318IEXo0y7W+DTQ6afsu5WkS1Te5Itar/DlEJZ+v0Dlw7TOVvIF WrT8HT8XTWpRXVhi4gohwY3E5g== X-Google-Smtp-Source: ABdhPJxQIefmzGUPyc50+2URUuiATPTUI2ellpZWz0sJIkkNfeQbGulC4f17oKELkil+hJwnkmQerQ== X-Received: by 2002:a17:902:e80b:b0:155:c75a:42f with SMTP id u11-20020a170902e80b00b00155c75a042fmr3560701plg.67.1648709853513; Wed, 30 Mar 2022 23:57:33 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.239]) by smtp.gmail.com with ESMTPSA id d8-20020a056a00198800b004fab740dbe6sm26169846pfl.15.2022.03.30.23.57.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Mar 2022 23:57:33 -0700 (PDT) From: Muchun Song To: will@kernel.org, akpm@linux-foundation.org, david@redhat.com, bodeddub@amazon.com, osalvador@suse.de, mike.kravetz@oracle.com, rientjes@google.com, mark.rutland@arm.com, catalin.marinas@arm.com, james.morse@arm.com, 21cnbao@gmail.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v4 2/2] arm64: mm: hugetlb: Enable HUGETLB_PAGE_FREE_VMEMMAP for arm64 Date: Thu, 31 Mar 2022 14:56:40 +0800 Message-Id: <20220331065640.5777-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220331065640.5777-1-songmuchun@bytedance.com> References: <20220331065640.5777-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Stat-Signature: cb3yky8jx1p7etbh96jta8hao5itmrze Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b="3uldU/51"; spf=pass (imf03.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.179 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 81CAF2000B X-HE-Tag: 1648709854-910230 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 feature of minimizing overhead of struct page associated with each HugeTLB page aims to free its vmemmap pages (used as struct page) to save memory, where is ~14GB/16GB per 1TB HugeTLB pages (2MB/1GB type). In short, when a HugeTLB page is allocated or freed, the vmemmap array representing the range associated with the page will need to be remapped. When a page is allocated, vmemmap pages are freed after remapping. When a page is freed, previously discarded vmemmap pages must be allocated before remapping. More implementations and details can be found here [1]. The infrastructure of freeing vmemmap pages associated with each HugeTLB page is already there, we can easily enable HUGETLB_PAGE_FREE_VMEMMAP for arm64, the only thing to be fixed is flush_dcache_page() . flush_dcache_page() need to be adapted to operate on the head page's flags since the tail vmemmap pages are mapped with read-only after the feature is enabled (clear operation is not permitted). There was some discussions about this in the thread [2], but there was no conclusion in the end. And I copied the concern proposed by Anshuman to here and explain why those concern is superfluous. It is safe to enable it for x86_64 as well as arm64. 1st concern: ''' But what happens when a hot remove section's vmemmap area (which is being teared down) is nearby another vmemmap area which is either created or being destroyed for HugeTLB alloc/free purpose. As you mentioned HugeTLB pages inside the hot remove section might be safe. But what about other HugeTLB areas whose vmemmap area shares page table entries with vmemmap entries for a section being hot removed ? Massive HugeTLB alloc /use/free test cycle using memory just adjacent to a memory hotplug area, which is always added and removed periodically, should be able to expose this problem. ''' Answer: At the time memory is removed, all HugeTLB pages either have been migrated away or dissolved. So there is no race between memory hot remove and free_huge_page_vmemmap(). Therefore, HugeTLB pages inside the hot remove section is safe. Let's talk your question "what about other HugeTLB areas whose vmemmap area shares page table entries with vmemmap entries for a section being hot removed ?", the question is not established. The minimal granularity size of hotplug memory 128MB (on arm64, 4k base page), any HugeTLB smaller than 128MB is within a section, then, there is no share PTE page tables between HugeTLB in this section and ones in other sections and a HugeTLB page could not cross two sections. In this case, the section cannot be freed. Any HugeTLB bigger than 128MB (section size) whose vmemmap pages is an integer multiple of 2MB (PMD-mapped). As long as: 1) HugeTLBs are naturally aligned, power-of-two sizes 2) The HugeTLB size >= the section size 3) The HugeTLB size >= the vmemmap leaf mapping size Then a HugeTLB will not share any leaf page table entries with *anything else*, but will share intermediate entries. In this case, at the time memory is removed, all HugeTLB pages either have been migrated away or dissolved. So there is also no race between memory hot remove and free_huge_page_vmemmap(). 2nd concern: ''' differently, not sure if ptdump would require any synchronization. Dumping an wrong value is probably okay but crashing because a page table entry is being freed after ptdump acquired the pointer is bad. On arm64, ptdump() is protected against hotremove via [get|put]_online_mems(). ''' Answer: The ptdump should be fine since vmemmap_remap_free() only exchanges PTEs or splits the PMD entry (which means allocating a PTE page table). Both operations do not free any page tables (PTE), so ptdump cannot run into a UAF on any page tables. The worst case is just dumping an wrong value. [1] https://lore.kernel.org/all/20210510030027.56044-1-songmuchun@bytedance.com/ [2] https://lore.kernel.org/all/20210518091826.36937-1-songmuchun@bytedance.com/ Signed-off-by: Muchun Song Reviewed-by: Barry Song Reviewed-by: Anshuman Khandual --- v4: - Introduce ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP (implemented in the previous patch) to enable this feature for arm64. v3: - Rework patch's subject. - Clarify the feature of HUGETLB_PAGE_FREE_VMEMMAP is already there in the current code and easyly be enabled for arm64 into commit log. - Add hugetlb_free_vmemmap_enabled() check into flush_dcache_page(). Thanks for Barry's suggestions. v2: - Update commit message (Mark Rutland). - Fix flush_dcache_page(). arch/arm64/Kconfig | 1 + arch/arm64/mm/flush.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index c842878f8133..37f72e3a75d0 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -94,6 +94,7 @@ config ARM64 select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36) + select ARCH_WANT_HUGETLB_PAGE_FREE_VMEMMAP select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANTS_NO_INSTR select ARCH_HAS_UBSAN_SANITIZE_ALL diff --git a/arch/arm64/mm/flush.c b/arch/arm64/mm/flush.c index 2aaf950b906c..c67c1ca856c2 100644 --- a/arch/arm64/mm/flush.c +++ b/arch/arm64/mm/flush.c @@ -68,6 +68,19 @@ EXPORT_SYMBOL_GPL(__sync_icache_dcache); */ void flush_dcache_page(struct page *page) { + /* + * Only the head page's flags of HugeTLB can be cleared since the tail + * vmemmap pages associated with each HugeTLB page are mapped with + * read-only when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is enabled (more + * details can refer to vmemmap_remap_pte()). Although + * __sync_icache_dcache() only set PG_dcache_clean flag on the head + * page struct, some tail page structs still can be seen the flag is + * set since the head vmemmap page frame is reused (more details can + * refer to the comments above page_fixed_fake_head()). + */ + if (hugetlb_free_vmemmap_enabled() && PageHuge(page)) + page = compound_head(page); + if (test_bit(PG_dcache_clean, &page->flags)) clear_bit(PG_dcache_clean, &page->flags); }