From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C56B13DE43D for ; Mon, 27 Jul 2026 10:33:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785148408; cv=none; b=U+fi5V1ChOw+7axSFOsGRwWmmy7K4dvSqiI7emalckkv4AdO4TAalfG4c3x8jvUAboxh93BiU3rL5uCw6aSOyYLlZN4A0b6pOerk4gb1ktE+ux/ydjI9cA4rcmjuXVNXzPh3Cnr7Uuak7ft4vbAKoMS+nR9ZxNeba+dCbt+QEZw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785148408; c=relaxed/simple; bh=AkdjCj2rFTDfe6gPapLgXxrK4O3Sh8VLxLC9wctPUbM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eLoTShV3cUnomgSUHGe7lSbiflqPVAsJxNGIPLKhiisF//nDvq3lbTPQmP2+LBDew5jRnkTeCGVZZIVZucfvGHdm/lwPiMC8lQvAmkYBMuXnFNYnly/9ShfxPAx5V4uLCe5WUH7f5q0Hi0xIgA5Kq1xteNfBdqldniKj+9eo/34= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NEL+gJot; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NEL+gJot" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AB8F1F000E9; Mon, 27 Jul 2026 10:33:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785148404; bh=oLJKZY7z9oL/tDHGyMnLb4jets030xYxa9/DOs/wndk=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=NEL+gJotI4dzLjO9dpkIY9A6naUXytugt48e7S6cNGGLhqDAlEf4EEf+PRSGPnkGD uVB/Hxp4blbinusmWXl1UO4H6pmuENcYVdbU6eWFwD+QDEtlwlVeEM4vq1XG5dSrUT 963kDoDbmzeJ/hfJwChw0GQYl/QfyV+yV49ZM5GKzd17YuSO3rCZM7fFmosSlupDIp zqwiLIu76mzvm3nZfM4Jlfnmodbk9RoGTw50I7NH43ZZ9B2r19tlKl0PsvaZHBfOqF f+H/KKPy1hZ+nW2dEtqLp0kHP4cVxcMY4lK9JTXzAE4svZGx3vNzy2xoi/wf92Kfo4 LkOtZPt/FqDSw== Date: Mon, 27 Jul 2026 13:33:12 +0300 From: Mike Rapoport To: Brendan Jackman Cc: Borislav Petkov , Dave Hansen , Peter Zijlstra , Andrew Morton , David Hildenbrand , Vlastimil Babka , Wei Xu , Johannes Weiner , Zi Yan , Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, Sumit Garg , Will Deacon , rientjes@google.com, "Kalyazin, Nikita" , patrick.roy@linux.dev, "Itazuri, Takahiro" , Andy Lutomirski , David Kaplan , Thomas Gleixner , Yosry Ahmed , Patrick Bellasi , Reiji Watanabe , Sean Christopherson , Nikita Kalyazin Subject: Re: [PATCH v3 01/26] set_memory: add folio_{zap,restore}_direct_map helpers Message-ID: References: <20260726-page_alloc-unmapped-v3-0-6f5729aa9832@google.com> <20260726-page_alloc-unmapped-v3-1-6f5729aa9832@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260726-page_alloc-unmapped-v3-1-6f5729aa9832@google.com> Hi Brendan, On Sun, Jul 26, 2026 at 10:22:34PM +0000, Brendan Jackman wrote: > From: Nikita Kalyazin > > Let's provide folio_{zap,restore}_direct_map helpers as preparation for > supporting removal of the direct map for guest_memfd folios. > In folio_zap_direct_map(), flush TLB to make sure the data is not > accessible. On some architectures, there may be a double TLB flush > issued because set_direct_map_valid_noflush already performs a flush > internally. > > The new helpers need to be accessible to KVM on architectures that > support guest_memfd (x86 and arm64). > > Direct map removal gives guest_memfd the same protection that > memfd_secret does, such as hardening against Spectre-like attacks > through in-kernel gadgets. > > Acked-by: David Hildenbrand (Arm) > Signed-off-by: Nikita Kalyazin > [Added comment, dropped modified set_direct_map API, added highmem check] > Signed-off-by: Brendan Jackman > --- > include/linux/set_memory.h | 13 +++++++++++++ > mm/memory.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 59 insertions(+) > > diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h > index 3030d9245f5ac..1bf2a15bca118 100644 > --- a/include/linux/set_memory.h > +++ b/include/linux/set_memory.h > @@ -40,6 +40,15 @@ static inline int set_direct_map_valid_noflush(struct page *page, > return 0; > } > > +static inline int folio_zap_direct_map(struct folio *folio) > +{ > + return 0; > +} > + > +static inline void folio_restore_direct_map(struct folio *folio) > +{ > +} > + > static inline bool kernel_page_present(struct page *page) > { > return true; > @@ -56,6 +65,10 @@ static inline bool can_set_direct_map(void) > } > #define can_set_direct_map can_set_direct_map > #endif > + > +int folio_zap_direct_map(struct folio *folio); > +void folio_restore_direct_map(struct folio *folio); > + > #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ > > #ifdef CONFIG_X86_64 > diff --git a/mm/memory.c b/mm/memory.c > index a73af1fccb3d0..789c65a6d6a0e 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -78,6 +78,7 @@ > #include > #include > #include > +#include > > #include > > @@ -7758,3 +7759,48 @@ void vma_pgtable_walk_end(struct vm_area_struct *vma) > if (is_vm_hugetlb_page(vma)) > hugetlb_vma_unlock_read(vma); > } > + > +#ifdef CONFIG_ARCH_HAS_SET_DIRECT_MAP > +/** > + * folio_zap_direct_map - remove a folio from the kernel direct map > + * @folio: folio to remove from the direct map > + * > + * Removes the folio from the kernel direct map and flushes the TLB. This may > + * require splitting huge pages in the direct map, which can fail due to memory > + * allocation. So far, only order-0 folios are supported; this guarantees > + * the unmap is either a complete success or a total failure. > + * > + * Return: 0 on success, or a negative error code on failure. > + */ > +int folio_zap_direct_map(struct folio *folio) > +{ > + struct page *page = folio_page(folio, 0); > + unsigned long addr = (unsigned long)page_address(page); > + int ret; > + > + if (folio_test_large(folio) || folio_test_highmem(folio)) > + return -EINVAL; > + > + ret = set_direct_map_valid_noflush(page, 1, false); There was a discussion about slight differences in the semantics of set_direct_map_valid() on x86 and on arm64 and that execmem should apparently switch to set_direct_map_{invalid,default}. Maybe for this series it would be better to add a patch that adds numpages to set_direct_map_{invalid,default}_noflush and use set_direct_map_default_noflush() here? And maybe also pick another Nikita's patch [2] that makes set_direct_map_* to take address? [1] https://lore.kernel.org/all/DJ69RCVRBO0Y.3JCYSW50IC4RC@linux.dev/ [2] https://lore.kernel.org/all/20260410151746.61150-2-kalyazin@amazon.com/ > + flush_tlb_kernel_range(addr, addr + folio_size(folio)); > + > + return ret; > +} > +EXPORT_SYMBOL_FOR_MODULES(folio_zap_direct_map, "kvm"); > + > +/** > + * folio_restore_direct_map - restore the kernel direct map entry for a folio > + * @folio: folio whose direct map entry is to be restored > + * > + * This may only be called after a prior successful folio_zap_direct_map() on > + * the same folio. Because the zap will have already split any huge pages in > + * the direct map, restoration here only updates protection bits and cannot > + * fail. > + */ > +void folio_restore_direct_map(struct folio *folio) > +{ > + WARN_ON_ONCE(set_direct_map_valid_noflush(folio_page(folio, 0), > + folio_nr_pages(folio), true)); > +} > +EXPORT_SYMBOL_FOR_MODULES(folio_restore_direct_map, "kvm"); > +#endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ > > -- > 2.54.0 > -- Sincerely yours, Mike.