From: "Roy, Patrick" <roypat@amazon.co.uk>
To: "will@kernel.org" <will@kernel.org>
Cc: "Liam.Howlett@oracle.com" <Liam.Howlett@oracle.com>,
"agordeev@linux.ibm.com" <agordeev@linux.ibm.com>,
"akpm@linux-foundation.org" <akpm@linux-foundation.org>,
"alex@ghiti.fr" <alex@ghiti.fr>,
"andrii@kernel.org" <andrii@kernel.org>,
"anna@kernel.org" <anna@kernel.org>,
"aou@eecs.berkeley.edu" <aou@eecs.berkeley.edu>,
"ast@kernel.org" <ast@kernel.org>,
"axelrasmussen@google.com" <axelrasmussen@google.com>,
"borntraeger@linux.ibm.com" <borntraeger@linux.ibm.com>,
"bp@alien8.de" <bp@alien8.de>,
"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
"brauner@kernel.org" <brauner@kernel.org>,
"catalin.marinas@arm.com" <catalin.marinas@arm.com>,
"chenhuacai@kernel.org" <chenhuacai@kernel.org>,
"corbet@lwn.net" <corbet@lwn.net>,
"daniel@iogearbox.net" <daniel@iogearbox.net>,
"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
"david@redhat.com" <david@redhat.com>,
"derekmn@amazon.co.uk" <derekmn@amazon.co.uk>,
"devel@lists.orangefs.org" <devel@lists.orangefs.org>,
"eddyz87@gmail.com" <eddyz87@gmail.com>,
"gerald.schaefer@linux.ibm.com" <gerald.schaefer@linux.ibm.com>,
"gor@linux.ibm.com" <gor@linux.ibm.com>,
"hannes@cmpxchg.org" <hannes@cmpxchg.org>,
"haoluo@google.com" <haoluo@google.com>,
"hca@linux.ibm.com" <hca@linux.ibm.com>,
"hpa@zytor.com" <hpa@zytor.com>,
"hubcap@omnibond.com" <hubcap@omnibond.com>,
"jack@suse.cz" <jack@suse.cz>,
"Thomson, Jack" <jackabt@amazon.co.uk>,
"jannh@google.com" <jannh@google.com>,
"jgg@ziepe.ca" <jgg@ziepe.ca>,
"jhubbard@nvidia.com" <jhubbard@nvidia.com>,
"joey.gouly@arm.com" <joey.gouly@arm.com>,
"john.fastabend@gmail.com" <john.fastabend@gmail.com>,
"jolsa@kernel.org" <jolsa@kernel.org>,
"Kalyazin, Nikita" <kalyazin@amazon.co.uk>,
"kernel@xen0n.name" <kernel@xen0n.name>,
"kpsingh@kernel.org" <kpsingh@kernel.org>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"kvmarm@lists.linux.dev" <kvmarm@lists.linux.dev>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>,
"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>,
"linux-fsdevel@vger.kernel.org" <linux-fsdevel@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linux-kselftest@vger.kernel.org"
<linux-kselftest@vger.kernel.org>,
"linux-mm@kvack.org" <linux-mm@kvack.org>,
"linux-nfs@vger.kernel.org" <linux-nfs@vger.kernel.org>,
"linux-riscv@lists.infradead.org"
<linux-riscv@lists.infradead.org>,
"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>,
"loongarch@lists.linux.dev" <loongarch@lists.linux.dev>,
"lorenzo.stoakes@oracle.com" <lorenzo.stoakes@oracle.com>,
"luto@kernel.org" <luto@kernel.org>,
"martin.lau@linux.dev" <martin.lau@linux.dev>,
"martin@omnibond.com" <martin@omnibond.com>,
"maz@kernel.org" <maz@kernel.org>,
"mhocko@suse.com" <mhocko@suse.com>,
"mingo@redhat.com" <mingo@redhat.com>,
"oliver.upton@linux.dev" <oliver.upton@linux.dev>,
"palmer@dabbelt.com" <palmer@dabbelt.com>,
"paul.walmsley@sifive.com" <paul.walmsley@sifive.com>,
"pbonzini@redhat.com" <pbonzini@redhat.com>,
"peterx@redhat.com" <peterx@redhat.com>,
"peterz@infradead.org" <peterz@infradead.org>,
"pfalcato@suse.de" <pfalcato@suse.de>,
"Roy, Patrick" <roypat@amazon.co.uk>,
"rppt@kernel.org" <rppt@kernel.org>,
"sdf@fomichev.me" <sdf@fomichev.me>,
"seanjc@google.com" <seanjc@google.com>,
"shakeel.butt@linux.dev" <shakeel.butt@linux.dev>,
"shuah@kernel.org" <shuah@kernel.org>,
"song@kernel.org" <song@kernel.org>,
"surenb@google.com" <surenb@google.com>,
"suzuki.poulose@arm.com" <suzuki.poulose@arm.com>,
"svens@linux.ibm.com" <svens@linux.ibm.com>,
"tglx@linutronix.de" <tglx@linutronix.de>,
"trondmy@kernel.org" <trondmy@kernel.org>,
"vbabka@suse.cz" <vbabka@suse.cz>,
"viro@zeniv.linux.org.uk" <viro@zeniv.linux.org.uk>,
"weixugc@google.com" <weixugc@google.com>,
"willy@infradead.org" <willy@infradead.org>,
"x86@kernel.org" <x86@kernel.org>,
"Cali, Marco" <xmarcalx@amazon.co.uk>,
"yonghong.song@linux.dev" <yonghong.song@linux.dev>,
"yuanchu@google.com" <yuanchu@google.com>,
"yuzenghui@huawei.com" <yuzenghui@huawei.com>,
"zhengqi.arch@bytedance.com" <zhengqi.arch@bytedance.com>
Subject: Re: [PATCH v6 05/11] KVM: guest_memfd: Add flag to remove from direct map
Date: Fri, 19 Sep 2025 08:44:27 +0000 [thread overview]
Message-ID: <20250919084425.30632-1-roypat@amazon.co.uk> (raw)
In-Reply-To: <aMxpvI6Aj8mDsRNm@willie-the-truck>
Hi Will,
On Thu, 2025-09-18 at 21:21 +0100, Will Deacon wrote:
> Hi Patrick,
> We chatted briefly at KVM Forum, so I wanted to chime in here too from
> the arm64 side.
>
> On Fri, Sep 12, 2025 at 09:17:37AM +0000, Roy, Patrick wrote:
>> Add GUEST_MEMFD_FLAG_NO_DIRECT_MAP flag for KVM_CREATE_GUEST_MEMFD()
>> ioctl. When set, guest_memfd folios will be removed from the direct map
>> after preparation, with direct map entries only restored when the folios
>> are freed.
>>
>> To ensure these folios do not end up in places where the kernel cannot
>> deal with them, set AS_NO_DIRECT_MAP on the guest_memfd's struct
>> address_space if GUEST_MEMFD_FLAG_NO_DIRECT_MAP is requested.
>>
>> Add KVM_CAP_GUEST_MEMFD_NO_DIRECT_MAP to let userspace discover whether
>> guest_memfd supports GUEST_MEMFD_FLAG_NO_DIRECT_MAP. Support depends on
>> guest_memfd itself being supported, but also on whether linux supports
>> manipulatomg the direct map at page granularity at all (possible most of
>> the time, outliers being arm64 where its impossible if the direct map
>> has been setup using hugepages, as arm64 cannot break these apart due to
>> break-before-make semantics, and powerpc, which does not select
>> ARCH_HAS_SET_DIRECT_MAP, which also doesn't support guest_memfd anyway
>> though).
>>
>> Note that this flag causes removal of direct map entries for all
>> guest_memfd folios independent of whether they are "shared" or "private"
>> (although current guest_memfd only supports either all folios in the
>> "shared" state, or all folios in the "private" state if
>> GUEST_MEMFD_FLAG_MMAP is not set). The usecase for removing direct map
>> entries of also the shared parts of guest_memfd are a special type of
>> non-CoCo VM where, host userspace is trusted to have access to all of
>> guest memory, but where Spectre-style transient execution attacks
>> through the host kernel's direct map should still be mitigated. In this
>> setup, KVM retains access to guest memory via userspace mappings of
>> guest_memfd, which are reflected back into KVM's memslots via
>> userspace_addr. This is needed for things like MMIO emulation on x86_64
>> to work.
>>
>> Do not perform TLB flushes after direct map manipulations. This is
>> because TLB flushes resulted in a up to 40x elongation of page faults in
>> guest_memfd (scaling with the number of CPU cores), or a 5x elongation
>> of memory population. TLB flushes are not needed for functional
>> correctness (the virt->phys mapping technically stays "correct", the
>> kernel should simply not use it for a while). On the other hand, it means
>> that the desired protection from Spectre-style attacks is not perfect,
>> as an attacker could try to prevent a stale TLB entry from getting
>> evicted, keeping it alive until the page it refers to is used by the
>> guest for some sensitive data, and then targeting it using a
>> spectre-gadget.
>
> I'm really not keen on this last part (at least, for arm64).
>
> If you're not going to bother invalidating the TLB after unmapping from
> the direct map because of performance reasons, you're better off just
> leaving the direct map intact and getting even better performance. On
> arm64, that would mean you could use block mappings too.
Not until we have hardware with the newest BBM goodies I thought. When I
checked earlier this year, a defconfig has the direct map setup at 4k
granularity.
> On the other hand, if you actually care about the security properties
> from the unmap then you need the invalidation so that the mapping
> doesn't linger around. With "modern" CPU features such as pte
> aggregation and shared TLB walk caches it's not unlikely that these
> entries will persist a lot longer than you think and it makes the
> security benefits of this series impossible to reason about.
I agree it's not 100% protection, but it is still better than the status quo. I
would also love to have the TLB flushes, but sadly the performance impact of
them would make this completely unusable for Amazon :/
Mh, thinking about it now though, iirc the performance problems were mostly
because all CPUs needed to acknowledge the flush before the issuing CPU could
continue. Is there a way to have "fire and forget" flushes, where we don't wait
for acknowledgements?
> As a compromise, could we make the TLB invalidation an architecture
> opt-in so that we can have it enabled on arm64, please?
How about instead of an architecture opt-in, we have some sort of opt-out flag
userspace can set? Similar to the PFNMAP stuff KVM already has.
> Will
Best,
Patrick
next prev parent reply other threads:[~2025-09-19 8:44 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-12 9:17 [PATCH v6 00/11] Direct Map Removal Support for guest_memfd Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 01/11] filemap: Pass address_space mapping to ->free_folio() Roy, Patrick
2025-09-12 10:48 ` Pedro Falcato
2025-09-16 6:23 ` Hugh Dickins
2025-09-17 14:52 ` David Hildenbrand
2025-09-19 8:30 ` Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 02/11] arch: export set_direct_map_valid_noflush to KVM module Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 03/11] mm: introduce AS_NO_DIRECT_MAP Roy, Patrick
2025-09-14 7:35 ` Mike Rapoport
2025-09-12 9:17 ` [PATCH v6 04/11] KVM: guest_memfd: Add stub for kvm_arch_gmem_invalidate Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 05/11] KVM: guest_memfd: Add flag to remove from direct map Roy, Patrick
2025-09-14 7:44 ` Mike Rapoport
2025-09-19 8:25 ` Roy, Patrick
2025-09-18 20:21 ` Will Deacon
2025-09-19 8:44 ` Roy, Patrick [this message]
2025-09-12 9:17 ` [PATCH v6 06/11] KVM: selftests: load elf via bounce buffer Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 07/11] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 08/11] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 09/11] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 10/11] KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in existing selftests Roy, Patrick
2025-09-12 9:17 ` [PATCH v6 11/11] KVM: selftests: Test guest execution from direct map removed gmem Roy, Patrick
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250919084425.30632-1-roypat@amazon.co.uk \
--to=roypat@amazon.co.uk \
--cc=Liam.Howlett@oracle.com \
--cc=agordeev@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=alex@ghiti.fr \
--cc=andrii@kernel.org \
--cc=anna@kernel.org \
--cc=aou@eecs.berkeley.edu \
--cc=ast@kernel.org \
--cc=axelrasmussen@google.com \
--cc=borntraeger@linux.ibm.com \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=brauner@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=chenhuacai@kernel.org \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=david@redhat.com \
--cc=derekmn@amazon.co.uk \
--cc=devel@lists.orangefs.org \
--cc=eddyz87@gmail.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hannes@cmpxchg.org \
--cc=haoluo@google.com \
--cc=hca@linux.ibm.com \
--cc=hpa@zytor.com \
--cc=hubcap@omnibond.com \
--cc=jack@suse.cz \
--cc=jackabt@amazon.co.uk \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joey.gouly@arm.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kalyazin@amazon.co.uk \
--cc=kernel@xen0n.name \
--cc=kpsingh@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=linux-s390@vger.kernel.org \
--cc=loongarch@lists.linux.dev \
--cc=lorenzo.stoakes@oracle.com \
--cc=luto@kernel.org \
--cc=martin.lau@linux.dev \
--cc=martin@omnibond.com \
--cc=maz@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=oliver.upton@linux.dev \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=peterz@infradead.org \
--cc=pfalcato@suse.de \
--cc=rppt@kernel.org \
--cc=sdf@fomichev.me \
--cc=seanjc@google.com \
--cc=shakeel.butt@linux.dev \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=surenb@google.com \
--cc=suzuki.poulose@arm.com \
--cc=svens@linux.ibm.com \
--cc=tglx@linutronix.de \
--cc=trondmy@kernel.org \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--cc=weixugc@google.com \
--cc=will@kernel.org \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
--cc=xmarcalx@amazon.co.uk \
--cc=yonghong.song@linux.dev \
--cc=yuanchu@google.com \
--cc=yuzenghui@huawei.com \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).