From mboxrd@z Thu Jan 1 00:00:00 1970 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 0DF12C433F5 for ; Wed, 30 Mar 2022 06:53:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66AF98D0002; Wed, 30 Mar 2022 02:53:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61A568D0001; Wed, 30 Mar 2022 02:53:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB818D0002; Wed, 30 Mar 2022 02:53:13 -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 3CEF18D0001 for ; Wed, 30 Mar 2022 02:53:13 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 103732444B for ; Wed, 30 Mar 2022 06:53:13 +0000 (UTC) X-FDA: 79300135866.07.BEABD95 Received: from mail-yb1-f179.google.com (mail-yb1-f179.google.com [209.85.219.179]) by imf15.hostedemail.com (Postfix) with ESMTP id F3F60A0026 for ; Wed, 30 Mar 2022 06:53:11 +0000 (UTC) Received: by mail-yb1-f179.google.com with SMTP id v35so35388814ybi.10 for ; Tue, 29 Mar 2022 23:53:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=eBzSo1IuAffvd9iEt2NILMJqpADmVwfXfVQFJPM3PLI=; b=kAYoRpp9RINiAX6gGmakqpZiOCu0pfap3QsrG9nwA3zBT2uee70bbf3gbgO/zx5fm5 L2WI/E+Ow+oKhG+rGP9/ELE0bdkQzMpabru264473bY1jHaI8WIjIyt03VvK30m6NUL1 IGizFqWSH+UmjP8VQlNNu8MEjm8l/yGR9H00XMzEYy4XHLCmnnDLGVXyJ3DCF+g/lvP2 S4SsNwg863PozI3QQtELU8kw9fsG5As/XSPI8bAzuiqkMJq24RLUd6GC0uAXrM/I6fDL zFxL6bjXXEIOXPrHRJFQ8NQrv2YI7rbQmF53M71mgjGIP5elWMKs0b8NOXPSiQADILbF HLJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=eBzSo1IuAffvd9iEt2NILMJqpADmVwfXfVQFJPM3PLI=; b=oz2zqahcEX91EBAlQhTYVDqTVbjiTT/PhSdSZn+8WkiaJCai9RmY64t8CuPmlz9erU ujQraNh5u5CYU1hr1Dq7834wIl/Ikpg7WgXvrMr6mMe5YqrqiFpasby9pjNtNtFirVwI K0P5VjoxOw382vQCm8oAgF0zY2kcgGmzr/6B9Nhm17MJVWplTtHIwt7hpwoE6WA8f2WT a+YJycI/nCz1JEwBrfPfIHvMEGaYddP2h5o67GesgILuNq2r2jBXRMoKGOzDMWt0Qifl ip8GRQUrrECPcr1USWjDxx2TX+54e6XFkIcD4wOqcMXWasWGZZ7jwsdxEt/7IKOg78PP nNsA== X-Gm-Message-State: AOAM531v0xtgM8coZQC4SE71vuqAYHL1pZRpBK0iu2sbdMESGJ+ROIvO VQeA8v9e1Y+Ck66DrDC8wffzOR6xnJJ1wYNn/FNNnw== X-Google-Smtp-Source: ABdhPJz1NmSJcW22Bmlc1MqhqmJWo3w4jKN4gY8Qemj+A1qqBdDzOs97xpu9EMhs3p8t4EeZBcqTWFKlkfcfuPlRFzE= X-Received: by 2002:a25:c049:0:b0:634:6751:e8d2 with SMTP id c70-20020a25c049000000b006346751e8d2mr31868071ybf.6.1648623190862; Tue, 29 Mar 2022 23:53:10 -0700 (PDT) MIME-Version: 1.0 References: <20220329045535.45641-1-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Wed, 30 Mar 2022 14:52:34 +0800 Message-ID: Subject: Re: [PATCH v2 RESEND] arm64: mm: hugetlb: add support for free vmemmap pages of HugeTLB To: Barry Song <21cnbao@gmail.com> Cc: Will Deacon , Andrew Morton , David Hildenbrand , "Bodeddula, Balasubramaniam" , Oscar Salvador , Mike Kravetz , David Rientjes , Mark Rutland , Catalin Marinas , james.morse@arm.com, LAK , LKML , Linux-MM , Xiongchun duan , Fam Zheng , Muchun Song Content-Type: text/plain; charset="UTF-8" X-Stat-Signature: nm9rua7szp33gthifoureqsfwjq4u9dz X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: F3F60A0026 Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=kAYoRpp9; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf15.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.219.179 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Rspam-User: X-HE-Tag: 1648623191-364569 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: On Tue, Mar 29, 2022 at 7:44 PM Barry Song <21cnbao@gmail.com> wrote: > > On Tue, Mar 29, 2022 at 5:57 PM Muchun Song wrote: > > > > 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()). > > Is this still true if hugetlb_free_vmemmap_enabled() is false? No. Do you think it is better to add hugetlb_free_vmemmap_enabled() into the if block? Something like the following? + if (hugetlb_free_vmemmap_enabled() && PageHuge(page)) + page = compound_head(page); > > btw, the subject is a bit confusing as it seems it is not bringing up > HUGETLB_PAGE_FREE_VMEMMAP and it seems the feature > has been already there, but we are lacking some fixes for some > functions to make it work. Right. > could we explain this clear in commit > log? maybe we need a better subject for the commit as well. Will do. Thanks.