From patchwork Tue Feb 8 05:46:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12738203 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4E539C433F5 for ; Tue, 8 Feb 2022 05:48:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=AcmVB3Y5ppsBwnKJyWc8j0uYqKkkpFHjTX3KfWahANE=; b=J/uIYOve1Qlsd+ knBdUSOf9ciNDOsAxjU6ik9OMZKOwyDSJPqc2D7Z9lmWcE1Hz9WNiS7XsZJ4RsvVx/d4w0l71JP5F Jwf167Ehna9RhiAGI5zh2ln5ad477e72+Z7Wn16EJ26sR3//TaKvEpwIbMnjHEmv1PvH2O6orC6uM vnaA2SK/ImdG51UiYzufuI6E8+IVVQKm6jGV25uPxSlkDFDlwLmWBXanVhqriZ+iZ9S7R/oQZvrgP FDsg2wwRDuY+wFvk/IB6ynFQEs9hAT7qP0OnIe76XYBe5W8/fqpAqOrzutXi7X4jbzarM2pIXj31y s2RqbN/1sPenquVY6J2A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nHJLA-00CXmo-1N; Tue, 08 Feb 2022 05:47:00 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nHJL6-00CXlr-2Q for linux-arm-kernel@lists.infradead.org; Tue, 08 Feb 2022 05:46:57 +0000 Received: by mail-pf1-x430.google.com with SMTP id e6so17016536pfc.7 for ; Mon, 07 Feb 2022 21:46:54 -0800 (PST) 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=ZWLmgm9buCbXqY23s5VhjmfJZhCL4RghEpABSyXi0/w=; b=0HYhkWbJnwjpTlx6yPo8iGOzEbRV18XhnOFuEUyVggVDiXTNWhIYIA/aPOw0uT/L09 yYlF5uQgKM92dhsGYUFEULlf710NYp+6d5gV72NVyUEGVTIeAnH/far1EjAK173nhvMA GjeUDLqL5ol5Xc/S8j13re2FPQ9DbSQqW4hnoMvWW0ONXTDzniTB/sZvR27PUX0rMR31 2IaLUB3SKzesIpY3I5KoXVmMF8mTMmaVUWVDnPWX1VyYk6Y9IovfyCE5NfGDDwrp7+vU YDwbvjgGwRCxwOisVBbjwLb+EY70DD8YjVX2AO/x2nJabwLLDDkrf3fSuV6Gb6SzM5iE q6uA== 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=ZWLmgm9buCbXqY23s5VhjmfJZhCL4RghEpABSyXi0/w=; b=LgbAo289RW8MQ2IXyOzRAQ/yy9GGLNKlhn9exAUt+xXDyxMeTVOREK6Qpi64Ln/y/i R9fGNJf0IsblChMJtRLrsBoUpzPESTvcW1BKG4ik26g1cSIfTdnYS/n/aiWPqdDh83DR bM2uRxv0rOuQpN2gRZIGQELbNJwngUw+vt/+vlU30nV5AlvtC26B+UcV9BFEaJTco8gO LNZJgktHNEjDOH9Fz2aXDrMaJFD8Gu/3Zx3EyoltE/zP2RT0gHj0LhmkdJUk7DdPDV3c EnX+gbR15rYPCgePYseiH4vsbJD0rVENihynvqzlUMWouupqPQGgCRXzUM5eDLOO61Jn lw4Q== X-Gm-Message-State: AOAM532G8TTgypjnQephY10fEKZgz1BtcqLSWwCt/k9l/dq7cRZtUHkf 20zIMNF8fj9U2vdYNq9LWnT6zg== X-Google-Smtp-Source: ABdhPJwaR77nsHjzpwtt7ubQtGVX/NP0wNsgi1dJyCP5hRbqwsfRyYHkv7pveQ5ABAYLcpJ/rdv9DQ== X-Received: by 2002:a63:df46:: with SMTP id h6mr2348666pgj.26.1644299214205; Mon, 07 Feb 2022 21:46:54 -0800 (PST) Received: from FVFYT0MHHV2J.tiktokcdn.com ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id 20sm1160680pjf.15.2022.02.07.21.46.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 21:46:53 -0800 (PST) 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 Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, Muchun Song Subject: [PATCH v2 1/2] arm64: avoid flushing icache multiple times on contiguous HugeTLB Date: Tue, 8 Feb 2022 13:46:31 +0800 Message-Id: <20220208054632.66534-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220207_214656_194689_F613372B X-CRM114-Status: GOOD ( 12.72 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org When a contiguous HugeTLB page is mapped, set_pte_at() will be called CONT_PTES/CONT_PMDS times. Therefore, __sync_icache_dcache() will flush cache multiple times if the page is executable (to ensure the I-D cache coherency). However, the first flushing cache already covers subsequent cache flush operations. So only flusing cache for the head page if it is a HugeTLB page to avoid redundant cache flushing. In the next patch, it is also depends on this change since the tail vmemmap pages of HugeTLB is mapped with read-only meanning only head page struct can be modified. Signed-off-by: Muchun Song --- arch/arm64/mm/flush.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/arm64/mm/flush.c b/arch/arm64/mm/flush.c index 2aaf950b906c..a06c6ac770d4 100644 --- a/arch/arm64/mm/flush.c +++ b/arch/arm64/mm/flush.c @@ -52,6 +52,13 @@ void __sync_icache_dcache(pte_t pte) { struct page *page = pte_page(pte); + /* + * HugeTLB pages are always fully mapped, so only setting head page's + * PG_dcache_clean flag is enough. + */ + if (PageHuge(page)) + page = compound_head(page); + if (!test_bit(PG_dcache_clean, &page->flags)) { sync_icache_aliases((unsigned long)page_address(page), (unsigned long)page_address(page) + From patchwork Tue Feb 8 05:46:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12738204 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AFA17C433EF for ; Tue, 8 Feb 2022 05:48:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=cw3n5/KK2/UEEPnmk8rA/EQYJ/9oAwLs2fNAtJyz4fA=; b=OPfKGPLDIuyheC Nd7PFEmZCRglG/mw1omIaEFFN6ISedbKJgqmH5ObgZ/fNzLN2G4GAnRfnswWaclYu9KFnzxDYB7Ge WzVr0WkjyVmfxsX1ok5WGG8w4B0vicFKE6WZ/tFPl+AebcY21AApmeNnS4j00ockYqrGIcHCGY5jv IFhBbVO4HmNyBR561l9iGmOak/duZ+grhCXcErLPhYXIhhOPUL4DZBxr+YlJjey59KECO14e2cIyA gV6AToR5jeEmLAIbMPSESzRyjL+CJI1CPpoRb9f3aZX41EcTHPppx8igWurxYJy2NLbsRy1uGuZWD YR4DdFS4oSFRwrM60PEQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nHJLK-00CXq0-HM; Tue, 08 Feb 2022 05:47:10 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nHJLB-00CXmw-7m for linux-arm-kernel@lists.infradead.org; Tue, 08 Feb 2022 05:47:03 +0000 Received: by mail-pj1-x1033.google.com with SMTP id t14-20020a17090a3e4e00b001b8f6032d96so379613pjm.2 for ; Mon, 07 Feb 2022 21:47:00 -0800 (PST) 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=n5vkvz8TS8A/TWoh79lH9r9rs5OiOfx+UY7K0dS1wLk=; b=xwZoeurRxp/Fg3bZboK4CN/h1HC/Vg2P1EBa4IptVjAPnMpbRK/gio8e46NPXbSFaC 7fG0yYZX49kvJAtJ0Xe950EL3Vp5mjFK7BNO/w/tRA56PqaFarWT8v2+ygtfpPcdWzCT nY72MZd8q+CnrSKehtl2HKAhjf8HtybkzD5pQJIBsz5UlxbuQs7tqGX7usdbQ8Tsn4fl 7wFKlJe79Mz5TYLqmSpHZzQdae3GXihCcJ55cRmOdmR/DLkDAjpioTH0xKQEz9IlfzB1 GK9SsuSPHgMK/ahmN/M80aN8lch8JBxQrPJJY9wcYjmb1wHwae6WoN4279nhb0tzFeUc EmPQ== 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=n5vkvz8TS8A/TWoh79lH9r9rs5OiOfx+UY7K0dS1wLk=; b=YWtYoqMFn1m9uqb/VlCrJbnEvQO2UlU2nN8I9G5SCyB8ST7PWPL4P85zlpnLeeaFmV 5OGFp7mZGvDfj2Ex2Yw0oq47c6KOFSucUxgnP6tpspo4B9igvnxdLDlN1kGspGscu26Z fzuxrJqL1mb+wLXNttvK/jRQhj9pm1zX7XIvm5RF/eFVCoVJNxN81esLy7fOc+qK3BAI Pe7pBd+e8BsxacKRRjusxnDTIZottIp4jCIDwT9N/bDAwpQwR8EwOXl8lHRGltji69S7 rJZ6fEqJ9/v5IvTFUyKwblgf4xTKFRa4350AFjaM2BgQMeD8qwpqeji3+6fTCxKw9pXA SvaQ== X-Gm-Message-State: AOAM533MMiypx43vE8GEyDqBy3301Sp6WyuZS1FSHF7U+l8GvzfI+8tP lNU0el+j8suVwD0QOqkfMpZLzQ== X-Google-Smtp-Source: ABdhPJxqPkFZwo6hekZ7v7KbQ6pLZIIZTjzsBs5X3gMiMmQSytNbA1jYRKeJ5f2FlbHZ9x6l3r+NOA== X-Received: by 2002:a17:90b:3ec8:: with SMTP id rm8mr2633625pjb.207.1644299219989; Mon, 07 Feb 2022 21:46:59 -0800 (PST) Received: from FVFYT0MHHV2J.tiktokcdn.com ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id 20sm1160680pjf.15.2022.02.07.21.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 21:46:59 -0800 (PST) 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 Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, fam.zheng@bytedance.com, Muchun Song Subject: [PATCH v2 2/2] arm64: mm: hugetlb: add support for free vmemmap pages of HugeTLB Date: Tue, 8 Feb 2022 13:46:32 +0800 Message-Id: <20220208054632.66534-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220208054632.66534-1-songmuchun@bytedance.com> References: <20220208054632.66534-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220207_214701_308118_E592105A X-CRM114-Status: GOOD ( 25.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 preparation of freeing vmemmap pages associated with each HugeTLB page is ready, so we can support this feature for arm64 now. The 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. 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 split 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 wrost 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 --- Changes in v2: - Update commit message (Mark Rutland). - Fix flush_dcache_page(). arch/arm64/mm/flush.c | 14 ++++++++++++++ fs/Kconfig | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/arm64/mm/flush.c b/arch/arm64/mm/flush.c index a06c6ac770d4..705484a9b9df 100644 --- a/arch/arm64/mm/flush.c +++ b/arch/arm64/mm/flush.c @@ -75,6 +75,20 @@ EXPORT_SYMBOL_GPL(__sync_icache_dcache); */ void flush_dcache_page(struct page *page) { +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + /* + * 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 see the flag since + * the head vmemmap page frame is reused (more details can refer to + * the comments above page_fixed_fake_head()). + */ + if (PageHuge(page)) + page = compound_head(page); +#endif if (test_bit(PG_dcache_clean, &page->flags)) clear_bit(PG_dcache_clean, &page->flags); } diff --git a/fs/Kconfig b/fs/Kconfig index 7a2b11c0b803..04cfd5bf5ec9 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -247,7 +247,7 @@ config HUGETLB_PAGE config HUGETLB_PAGE_FREE_VMEMMAP def_bool HUGETLB_PAGE - depends on X86_64 + depends on X86_64 || ARM64 depends on SPARSEMEM_VMEMMAP config HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON