From nobody Tue Apr 7 14:20:31 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7131B35AC01 for ; Fri, 13 Mar 2026 06:12:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382376; cv=none; b=mhOrxH+MiiCQpIEetal9OaxVpzpjLZXs+oWFE8ub7gkAe1PrxZT4QsWSLQ1mwRDc789gbqtSalVdN90O4L2krd7E5MJQoQn5Jfw47MNSYrfhI9FtgJjvwKLfFAZlJoVXysDL4F8vWAZ1qtMXbTPsxemlFCTrDw+MsL29TDjIkIk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382376; c=relaxed/simple; bh=Z8Fj9mzRSl86D/84DXYcJ2/GuewGJS5xI9UqDcwvm0o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GH77nzUzkvnotmBjHTZLm2Kq5sCdeGgwHzFi21+TcIQqXfKn7HMIQ2zwmDsBlg/9iClRq6pxmi4m4pQ0c2TdysbzrzRMyQrxeyo/I25noB5gYBDNk6XCtPybzKe4JrhaeultXt9X50Pzq7456ifFVyHnl3XUFCyhWTIYl2Pgra4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OuyetPqf; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OuyetPqf" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c73a4983fc0so1063253a12.3 for ; Thu, 12 Mar 2026 23:12:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382374; x=1773987174; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iOyUOG17doN86gJPVX53Tfmosd3UCLbDnOJKtetJrAM=; b=OuyetPqf10ovjXqc2ys18ZV9UUQ8MibbOXMF/KkPgr/vkBWIrMjb6Q5GsxhsIKuJFN ofT55mTXOxBamg6HXibWGTm9TgVBm9y/9kk704QbfEQ/bCypz24EpedZRMCCsdj+oqZW DpbgP4nkVlZ1EnXxooPWvYNCqFvRona31G3+6BtUZeIwJXu3hCWrxWqC2vs4AHzFpwea AsrxrGTA4MxtMg141jkghZujVt8KxWY7Em0e+O+4kbVr4xS2YkAmY4LATQxglEQtybin dcKnnaIiB2ceAR5Prnvie+zJlua21xJ9PoaPlGrmQGU1mpzhA+MNgbRsO578iAonhOko YQ9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382374; x=1773987174; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iOyUOG17doN86gJPVX53Tfmosd3UCLbDnOJKtetJrAM=; b=G+SVB8X5RYr+wkjKsmFn8eUQIigTaTcI6hMouKEA2OevzOLvh3w0UTbLDUBeduYP38 l8P1X4n3dwDm1zRUKFFT9jzkNNIXB39a4GolWitmdEATCDRibZCYhE5Lg3LpKrsauppQ 1HHbAFzeDJRlSJSn/+b51/BjUzYxpqHehHjn8QanzSza31uu/YdHhBtQdnWkKcwaggxY R/5x//Jk6YUz0cAP+ZWHeqHxR/DbKPB1UlniCHhHRVUguC/7LwXbWMFwmb+zSBcexwwO l/t/alBxxdyagpfpCprncfJ9PvTbZnx8Us0ZglvWcY67totfhRvIcXJD4W04LEggqMH4 j30A== X-Forwarded-Encrypted: i=1; AJvYcCWiRmgaSAxBXgr8S+3y+qz0jnETX/AdWkbh7t6QpM2hq7g8OIkJS6W0I9NM3ksr2SFA9WDTm9JThaDR/ss=@vger.kernel.org X-Gm-Message-State: AOJu0YzfXe6nSULFmuVUEl4LmXYYCtKxKhEYHkPNWeVjJnPum12cQTEz oN3QoVH1SgwOmhlRQww1e7jxBwXR+ViLMey0/wqgnWTvYQTnap5CyewkyiDsZ0KtDz0H53hE8WR S0MjN3TkRMq1fwslP6k/l1FPOLg== X-Received: from pgbcv10.prod.google.com ([2002:a05:6a02:420a:b0:c73:87bb:2ffd]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9f10:b0:394:2fbb:bc6b with SMTP id adf61e73a8af0-398ecd3634dmr1711573637.46.1773382373538; Thu, 12 Mar 2026 23:12:53 -0700 (PDT) Date: Fri, 13 Mar 2026 06:12:43 +0000 In-Reply-To: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773382364; l=7204; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=OpURI5RSMWwx8gZDv6qxQgEUGnaqN+TPyr1JkglhGlo=; b=k5SF6DJF7PJeu7qJeWlFaZ6kFJRp/jCmmCdmie5NE14HvmREX17tKbPFuUg+/3mpd1KKXfsWW v21k8SXCouJA3qqPw2JPzsx1/a2DXt5r5WIR4rUmfe36s//U16OENi2 X-Mailer: b4 0.14.3 Message-ID: <20260313-gmem-inplace-conversion-v3-4-5fc12a70ec89@google.com> Subject: [PATCH RFC v3 04/43] KVM: Stub in ability to disable per-VM memory attribute tracking From: Ackerley Tng To: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jroedel@suse.de, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Jason Gunthorpe , Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Ackerley Tng Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Sean Christopherson Introduce the basic infrastructure to allow per-VM memory attribute tracking to be disabled. This will be built-upon in a later patch, where a module param can disable per-VM memory attribute tracking. Split the Kconfig option into a base KVM_MEMORY_ATTRIBUTES and the existing KVM_VM_MEMORY_ATTRIBUTES. The base option provides the core plumbing, while the latter enables the full per-VM tracking via an xarray and the associated ioctls. kvm_get_memory_attributes() now performs a static call that either looks up kvm->mem_attr_array with CONFIG_KVM_VM_MEMORY_ATTRIBUTES is enabled, or just returns 0 otherwise. The static call can be patched depending on whether per-VM tracking is enabled by the CONFIG. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 +- include/linux/kvm_host.h | 23 ++++++++++++--------- virt/kvm/Kconfig | 4 ++++ virt/kvm/kvm_main.c | 44 +++++++++++++++++++++++++++++++++++++= +++- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index cf3d2bdababc7..537f25121e345 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2329,7 +2329,7 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_force= d_root_level, int tdp_max_root_level, int tdp_huge_page_level); =20 =20 -#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +#ifdef CONFIG_KVM_MEMORY_ATTRIBUTES #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem) #endif =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4ba42a1278d5f..5393854ca5966 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2513,19 +2513,15 @@ static inline bool kvm_memslot_is_gmem_only(const s= truct kvm_memory_slot *slot) return slot->flags & KVM_MEMSLOT_GMEM_ONLY; } =20 -#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +#ifdef CONFIG_KVM_MEMORY_ATTRIBUTES +typedef unsigned long (kvm_get_memory_attributes_t)(struct kvm *kvm, gfn_t= gfn); +DECLARE_STATIC_CALL(__kvm_get_memory_attributes, kvm_get_memory_attributes= _t); + static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn= _t gfn) { - return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); + return static_call(__kvm_get_memory_attributes)(kvm, gfn); } =20 -bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, - unsigned long mask, unsigned long attrs); -bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, - struct kvm_gfn_range *range); -bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, - struct kvm_gfn_range *range); - static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { return kvm_get_memory_attributes(kvm, gfn) & KVM_MEMORY_ATTRIBUTE_PRIVATE; @@ -2535,6 +2531,15 @@ static inline bool kvm_mem_is_private(struct kvm *kv= m, gfn_t gfn) { return false; } +#endif + +#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, + unsigned long mask, unsigned long attrs); +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, + struct kvm_gfn_range *range); #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ =20 #ifdef CONFIG_KVM_GUEST_MEMFD diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 5119cb37145fc..3fea89c45cfb4 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -100,7 +100,11 @@ config KVM_ELIDE_TLB_FLUSH_IF_YOUNG config KVM_MMU_LOCKLESS_AGING bool =20 +config KVM_MEMORY_ATTRIBUTES + bool + config KVM_VM_MEMORY_ATTRIBUTES + select KVM_MEMORY_ATTRIBUTES bool =20 config KVM_GUEST_MEMFD diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 793a2c8476b09..dbdcef90a4950 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -102,6 +102,17 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(halt_poll_ns_shrink); static bool allow_unsafe_mappings; module_param(allow_unsafe_mappings, bool, 0444); =20 +#ifdef CONFIG_KVM_MEMORY_ATTRIBUTES +#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +static bool vm_memory_attributes =3D true; +#else +#define vm_memory_attributes false +#endif +DEFINE_STATIC_CALL_RET0(__kvm_get_memory_attributes, kvm_get_memory_attrib= utes_t); +EXPORT_SYMBOL_FOR_KVM_INTERNAL(STATIC_CALL_KEY(__kvm_get_memory_attributes= )); +EXPORT_SYMBOL_FOR_KVM_INTERNAL(STATIC_CALL_TRAMP(__kvm_get_memory_attribut= es)); +#endif + /* * Ordering of locks: * @@ -2425,7 +2436,7 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *k= vm, } #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ =20 -#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +#ifdef CONFIG_KVM_MEMORY_ATTRIBUTES static u64 kvm_supported_mem_attributes(struct kvm *kvm) { #ifdef kvm_arch_has_private_mem @@ -2436,6 +2447,12 @@ static u64 kvm_supported_mem_attributes(struct kvm *= kvm) return 0; } =20 +#ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES +static unsigned long kvm_get_vm_memory_attributes(struct kvm *kvm, gfn_t g= fn) +{ + return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); +} + /* * Returns true if _all_ gfns in the range [@start, @end) have attributes * such that the bits in @mask match @attrs. @@ -2632,7 +2649,24 @@ static int kvm_vm_ioctl_set_mem_attributes(struct kv= m *kvm, =20 return kvm_vm_set_mem_attributes(kvm, start, end, attrs->attributes); } +#else /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ +static unsigned long kvm_get_vm_memory_attributes(struct kvm *kvm, gfn_t g= fn) +{ + BUILD_BUG_ON(1); +} #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ +static void kvm_init_memory_attributes(void) +{ + if (vm_memory_attributes) + static_call_update(__kvm_get_memory_attributes, + kvm_get_vm_memory_attributes); + else + static_call_update(__kvm_get_memory_attributes, + (void *)__static_call_return0); +} +#else /* CONFIG_KVM_MEMORY_ATTRIBUTES */ +static void kvm_init_memory_attributes(void) { } +#endif /* CONFIG_KVM_MEMORY_ATTRIBUTES */ =20 struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn) { @@ -4932,6 +4966,9 @@ static int kvm_vm_ioctl_check_extension_generic(struc= t kvm *kvm, long arg) return 1; #ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES case KVM_CAP_MEMORY_ATTRIBUTES: + if (!vm_memory_attributes) + return 0; + return kvm_supported_mem_attributes(kvm); #endif #ifdef CONFIG_KVM_GUEST_MEMFD @@ -5338,6 +5375,10 @@ static long kvm_vm_ioctl(struct file *filp, case KVM_SET_MEMORY_ATTRIBUTES: { struct kvm_memory_attributes attrs; =20 + r =3D -ENOTTY; + if (!vm_memory_attributes) + goto out; + r =3D -EFAULT; if (copy_from_user(&attrs, argp, sizeof(attrs))) goto out; @@ -6528,6 +6569,7 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align,= struct module *module) kvm_preempt_ops.sched_in =3D kvm_sched_in; kvm_preempt_ops.sched_out =3D kvm_sched_out; =20 + kvm_init_memory_attributes(); kvm_init_debug(); =20 r =3D kvm_vfio_ops_init(); --=20 2.53.0.851.ga537e3e6e9-goog