KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode
authorMichael Roth <michael.roth@amd.com>
Fri, 29 Mar 2024 21:24:43 +0000 (16:24 -0500)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 10 May 2024 17:11:45 +0000 (13:11 -0400)
commit1d23040caa8bef867572ae814b5f8b5fa44eccd3
treea3ebfe84fdf41d7bc4dde5b1ce3b7ed1379602ed
parentc72ceafbd12cf95e088681ae5e535ef1a78bf0ed
KVM: guest_memfd: Use AS_INACCESSIBLE when creating guest_memfd inode

truncate_inode_pages_range() may attempt to zero pages before truncating
them, and this will occur before arch-specific invalidations can be
triggered via .invalidate_folio/.free_folio hooks via kvm_gmem_aops. For
AMD SEV-SNP this would result in an RMP #PF being generated by the
hardware, which is currently treated as fatal (and even if specifically
allowed for, would not result in anything other than garbage being
written to guest pages due to encryption). On Intel TDX this would also
result in undesirable behavior.

Set the AS_INACCESSIBLE flag to prevent the MM from attempting
unexpected accesses of this sort during operations like truncation.

This may also in some cases yield a decent performance improvement for
guest_memfd userspace implementations that hole-punch ranges immediately
after private->shared conversions via KVM_SET_MEMORY_ATTRIBUTES, since
the current implementation of truncate_inode_pages_range() always ends
up zero'ing an entire 4K range if it is backing by a 2M folio.

Link: https://lore.kernel.org/lkml/ZR9LYhpxTaTk6PJX@google.com/
Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Message-ID: <20240329212444.395559-6-michael.roth@amd.com>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
virt/kvm/guest_memfd.c