Skip to content

Commit c23e2b7

Browse files
sean-jcbonzini
authored andcommitted
KVM: Allow page-sized MMU caches to be initialized with custom 64-bit values
Add support to MMU caches for initializing a page with a custom 64-bit value, e.g. to pre-fill an entire page table with non-zero PTE values. The functionality will be used by x86 to support Intel's TDX, which needs to set bit 63 in all non-present PTEs in order to prevent !PRESENT page faults from getting reflected into the guest (Intel's EPT Violation #VE architecture made the less than brilliant decision of having the per-PTE behavior be opt-out instead of opt-in). Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Message-Id: <5919f685f109a1b0ebc6bd8fc4536ee94bcc172d.1705965635.git.isaku.yamahata@intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent a96cb3b commit c23e2b7

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

include/linux/kvm_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct gfn_to_pfn_cache {
8686
struct kvm_mmu_memory_cache {
8787
gfp_t gfp_zero;
8888
gfp_t gfp_custom;
89+
u64 init_value;
8990
struct kmem_cache *kmem_cache;
9091
int capacity;
9192
int nobjs;

virt/kvm/kvm_main.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,17 @@ static void kvm_flush_shadow_all(struct kvm *kvm)
401401
static inline void *mmu_memory_cache_alloc_obj(struct kvm_mmu_memory_cache *mc,
402402
gfp_t gfp_flags)
403403
{
404+
void *page;
405+
404406
gfp_flags |= mc->gfp_zero;
405407

406408
if (mc->kmem_cache)
407409
return kmem_cache_alloc(mc->kmem_cache, gfp_flags);
408-
else
409-
return (void *)__get_free_page(gfp_flags);
410+
411+
page = (void *)__get_free_page(gfp_flags);
412+
if (page && mc->init_value)
413+
memset64(page, mc->init_value, PAGE_SIZE / sizeof(u64));
414+
return page;
410415
}
411416

412417
int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity, int min)
@@ -421,6 +426,13 @@ int __kvm_mmu_topup_memory_cache(struct kvm_mmu_memory_cache *mc, int capacity,
421426
if (WARN_ON_ONCE(!capacity))
422427
return -EIO;
423428

429+
/*
430+
* Custom init values can be used only for page allocations,
431+
* and obviously conflict with __GFP_ZERO.
432+
*/
433+
if (WARN_ON_ONCE(mc->init_value && (mc->kmem_cache || mc->gfp_zero)))
434+
return -EIO;
435+
424436
mc->objects = kvmalloc_array(capacity, sizeof(void *), gfp);
425437
if (!mc->objects)
426438
return -ENOMEM;

0 commit comments

Comments
 (0)