From nobody Tue Apr 7 14:25:06 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 B82FA3659F3 for ; Fri, 13 Mar 2026 06:12:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382380; cv=none; b=ZdUNVxfmqOyVQ8pv+EbidjfBGemjLhch6r+5gDIyK57wT2CMJAjNJu3VDvI0qul1ok7ptlo2eUJLQrYIk8QI0OHymmWpf0F6w7hX+1qP5FWVfAIeuMGMwSa3lPPpzz7U31lhRLTmtoNKPqjKpxWcBJqe+XGXuyswKJMNXmeMKVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382380; c=relaxed/simple; bh=6l4gROx7jzaHEpW5EeWmylCHb6Dln97vDEPH0vMevUU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PU/8hTHmjVWmrExtdlykkTCMwIUaq7N0rCRVY+p5HTgQy4s0xfVpG8wq2pqZU7Z8ZdPwepqT+z1HooGT8V/zmGFvNj7yY+vMEMmZ8IYIdtGfhdeOU0nQM5KrVb2mUGbEWaf2ps0BnkKjNfwqsCk1O9qax3EQX1zwBYxW+9I6A2k= 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=s6X+udTa; arc=none smtp.client-ip=209.85.216.74 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="s6X+udTa" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35a203038c8so863042a91.3 for ; Thu, 12 Mar 2026 23:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382377; x=1773987177; 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=gcDmUTz+2QJCEjrFt10fJ/5y22/0S6e6j1N3ESHDYEw=; b=s6X+udTaeHfaxforCrc3BHP2tH7KdreFjHz1dC5tqs/S6B+uTHNFBAHF/oTDjE2DCK mWnJBtCoouS0Yb8eVC/xGe2A34vFbalVuoCiYuLX6oAxHSvBsCCsBlQfibNc0GlM+w+l Dz8kqQk8Y+IlvkUJabkb+GakANKo32WhaFlzHMzJ+G9oNb+sl6orclxZ/pHX8CJe6s7G E/NQXrwRtjHbp4A8EW9wmRpk/m0+TnuHfqNNH+OlLvkjkw2HJ9BP6mQiwv1Eh8ATknmh 93a/7yF938mvtkoZCQoKRY1U+PW82PVCP+ZiLWgdsRF1m8MtlSMkFtMaMcWsdSy6IIRv jm8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382377; x=1773987177; 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=gcDmUTz+2QJCEjrFt10fJ/5y22/0S6e6j1N3ESHDYEw=; b=PMEh5s70Rz/ilhamfnxHerYVIm8hi8BiYKjHsD4jz2EkRfUBvLibPAyvNEMz6hU1IN k4noLFfphS2RDOGEBvRHxZvuQkl1QksfGgiDMS18TaYJSBSBJKYmriANN5HZqfYvCUVl 9fr0007tHwX3iwLBmYx0B2W8V7m78NHu2J+JmbGNXniYtkmM9zV6JtUwyjNH6IfkxISB QM5SkdL7z4vc5gXMMrYVK4lcJyqKPX0GxMH4H9OyBvJRmAxDRjVkXxd7/zCEZu417PRs mRONd076QKsAFbQULbglChgL19vRwTleICN70mUFRSiOtAUUpc3joJOZW2CgbbkjdVxq KbRg== X-Forwarded-Encrypted: i=1; AJvYcCXGMZr2P12sm89vk9SB+NAVkP42Re/cwa6r4EhHL4/XZpn2x+eC2wqQ4IM0JKpdVlR+dr3E4S3GQU372OY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyetad6mQQQO5vTXVGc/9ZjHnEvthtMjQlJMSCewOEq3/wWpeIu Wy2uMr2hbd3iySx+Cd1/l56sGHzS7uS26p7jscBLzyDc8CXJTD0W4GX3Xxf0GMjbWyOxSNKVr98 CjqNu6jqzfnVvOJ+7MEZhx1lDfQ== X-Received: from pjbie24.prod.google.com ([2002:a17:90b:4018:b0:359:8bb2:c98d]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f85:b0:359:ff8a:ee52 with SMTP id 98e67ed59e1d1-35a21e4edbamr1952130a91.6.1773382376871; Thu, 12 Mar 2026 23:12:56 -0700 (PDT) Date: Fri, 13 Mar 2026 06:12:45 +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=5783; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=6l4gROx7jzaHEpW5EeWmylCHb6Dln97vDEPH0vMevUU=; b=sCHE4aruHhHNVBD2yopcHXCLUXwh+U3kB1yeszQMikNVOBJ9YprFXvSYO12Y8Y2hLpbvia8xs bx8ztRQait9DRBntyze/Gr5L9dhTy5q66mCYMrvVSzvfiBH4+mIYFoW X-Mailer: b4 0.14.3 Message-ID: <20260313-gmem-inplace-conversion-v3-6-5fc12a70ec89@google.com> Subject: [PATCH RFC v3 06/43] KVM: guest_memfd: Update kvm_gmem_populate() to use gmem attributes 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 Update the guest_memfd populate() flow to pull memory attributes from the gmem instance instead of the VM when KVM is not configured to track shared/private status in the VM. Rename the per-VM API to make it clear that it retrieves per-VM attributes, i.e. is not suitable for use outside of flows that are specific to generic per-VM attributes. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Ackerley Tng --- arch/x86/kvm/mmu/mmu.c | 2 +- include/linux/kvm_host.h | 14 +++++++++++++- virt/kvm/guest_memfd.c | 24 +++++++++++++++++++++--- virt/kvm/kvm_main.c | 8 +++----- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 792701b093234..f21e16f22dd7b 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7978,7 +7978,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struc= t kvm_memory_slot *slot, const unsigned long end =3D start + KVM_PAGES_PER_HPAGE(level); =20 if (level =3D=3D PG_LEVEL_2M) - return kvm_range_has_memory_attributes(kvm, start, end, ~0, attrs); + return kvm_range_has_vm_memory_attributes(kvm, start, end, ~0, attrs); =20 for (gfn =3D start; gfn < end; gfn +=3D KVM_PAGES_PER_HPAGE(level - 1)) { if (hugepage_test_mixed(slot, gfn, level - 1) || diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0f84421b3f49e..19f026f8de390 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2534,12 +2534,24 @@ static inline bool kvm_mem_is_private(struct kvm *k= vm, gfn_t gfn) #endif =20 #ifdef CONFIG_KVM_VM_MEMORY_ATTRIBUTES -bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, +extern bool vm_memory_attributes; +bool kvm_range_has_vm_memory_attributes(struct kvm *kvm, gfn_t start, gfn_= t end, 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); +#else +#define vm_memory_attributes false +static inline bool kvm_range_has_vm_memory_attributes(struct kvm *kvm, + gfn_t start, gfn_t end, + unsigned long mask, + unsigned long attrs) +{ + WARN_ONCE(1, "Unexpected call to kvm_range_has_vm_memory_attributes()"); + + return false; +} #endif /* CONFIG_KVM_VM_MEMORY_ATTRIBUTES */ =20 unsigned long kvm_gmem_get_memory_attributes(struct kvm *kvm, gfn_t gfn); diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index e93e95940ec74..501f4d6e87909 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -931,12 +931,31 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memo= ry_slot *slot, EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_pfn); =20 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_POPULATE +static bool kvm_gmem_range_is_private(struct gmem_inode *gi, pgoff_t index, + size_t nr_pages, struct kvm *kvm, gfn_t gfn) +{ + pgoff_t end =3D index + nr_pages - 1; + void *entry; + + if (vm_memory_attributes) + return kvm_range_has_vm_memory_attributes(kvm, gfn, gfn + nr_pages, + KVM_MEMORY_ATTRIBUTE_PRIVATE, + KVM_MEMORY_ATTRIBUTE_PRIVATE); + + mt_for_each(&gi->attributes, entry, index, end) { + if (xa_to_value(entry) !=3D attributes) + return false; + } + + return true; +} =20 static long __kvm_gmem_populate(struct kvm *kvm, struct kvm_memory_slot *s= lot, struct file *file, gfn_t gfn, struct page *src_page, kvm_gmem_populate_cb post_populate, void *opaque) { pgoff_t index =3D kvm_gmem_get_index(slot, gfn); + struct gmem_inode *gi; struct folio *folio; kvm_pfn_t pfn; int ret; @@ -951,9 +970,8 @@ static long __kvm_gmem_populate(struct kvm *kvm, struct= kvm_memory_slot *slot, =20 folio_unlock(folio); =20 - if (!kvm_range_has_memory_attributes(kvm, gfn, gfn + 1, - KVM_MEMORY_ATTRIBUTE_PRIVATE, - KVM_MEMORY_ATTRIBUTE_PRIVATE)) { + gi =3D GMEM_I(file_inode(file)); + if (!kvm_gmem_range_is_private(gi, index, 1, kvm, gfn)) { ret =3D -EINVAL; goto out_put_folio; } diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7bb59679c9316..70b594dafc5cc 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -104,9 +104,7 @@ 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 +bool vm_memory_attributes =3D true; #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= )); @@ -2457,7 +2455,7 @@ static unsigned long kvm_get_vm_memory_attributes(str= uct kvm *kvm, gfn_t gfn) * Returns true if _all_ gfns in the range [@start, @end) have attributes * such that the bits in @mask match @attrs. */ -bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t e= nd, +bool kvm_range_has_vm_memory_attributes(struct kvm *kvm, gfn_t start, gfn_= t end, unsigned long mask, unsigned long attrs) { XA_STATE(xas, &kvm->mem_attr_array, start); @@ -2591,7 +2589,7 @@ static int kvm_vm_set_mem_attributes(struct kvm *kvm,= gfn_t start, gfn_t end, mutex_lock(&kvm->slots_lock); =20 /* Nothing to do if the entire range has the desired attributes. */ - if (kvm_range_has_memory_attributes(kvm, start, end, ~0, attributes)) + if (kvm_range_has_vm_memory_attributes(kvm, start, end, ~0, attributes)) goto out_unlock; =20 /* --=20 2.53.0.851.ga537e3e6e9-goog