From nobody Wed Feb 11 06:31:26 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 5F2D9355814 for ; Mon, 2 Feb 2026 22:30:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770071435; cv=none; b=l5pO2zX3OpafeXmcjdAxUIEPas7jkjdNc4h341uSbKC9zIhzMThCZdJ0k3QaJW+oG2Z1xqNnu4tAsI2VKZ0VrQvxeySg45aD46Q8ovqSAh/fpdclgzYMJO7S4XdUC4CvOkSYysID+Hw69hWLR/OLrLexTXmKXW0odzui2R2gj3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770071435; c=relaxed/simple; bh=StGda4ieY4psoFjGEjNVxjWetznThow3xU6gh+iSGPQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EfumpFEr5460C99BWpU7abkiRfSHoBtLO1zkkKtFfIsF1vVE5X1bDrNIMbzc3w1WcnhWVDz0FmT/PJ+oRRF1W/7uTse1rljYFwZsiXm4qUqpaL3QTgOTUduQ1Qazba9bSEJlZeZJFfQ7aL4LWiqYrz4hdTgW453jADGYu9useGw= 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=WqdtwWsE; arc=none smtp.client-ip=209.85.214.201 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="WqdtwWsE" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a0b7eb0a56so44614345ad.1 for ; Mon, 02 Feb 2026 14:30:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770071433; x=1770676233; 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=GUwm2ObTXkw7wnNZ+8gnB1/G5SqZq1H7l6JvFOK25Us=; b=WqdtwWsE/VyYKr8te6sExVIwxbnHxVCYX91Z43WLiwGGQjh84i89Pj46F+wXTM9+tF M4l9V56sZzGKpkp09kBcJYpgQii37mfpOfrOYitVa1qjPOrVhXFFdSb+gjovoXLJf1re qKVW0SgIiJORCm2u1Z5PgYhNtxVwTPQM9HVAe2YEpnPTPQhsCl8/Tsh54BfmuQmqKS1x jtCNsYUdDteUXQMsCOLOS0U6D9S6eVyM7gfMSL6PSe9FmVjUq/RUMM+R7+DaBIKlc+GT m+DcNIwpF2IjC760tK4Vos69LnB2sk8UQFoq4RSf3elJpwrB0yb826WFevEmIjDQi55m Txfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770071433; x=1770676233; 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=GUwm2ObTXkw7wnNZ+8gnB1/G5SqZq1H7l6JvFOK25Us=; b=rXoJ9fV7sLROIkToOyRUSQDhmbT08cYkeDz+U9/E/F7bS+OUZHQZftwSw1uHI6Z3Kn e+23vj8Rod/AIVk4n11uDbWv97VuU5xuKaWv1pPFKKsTMPVJWu/JONpF1tp8wO/ffqIX TaYpBfYvKZjUnBwQA56znn3SdzhWvPcC8i3PTX3jkFBqFWB4EYxcwaWx6NnJuGZExNfr LFESCxf17jPJZdnTTCe/vv4QiKTV75y7y6bL+02PQE0jH9gbQU/VrhBiUKeh149FwU+f i5MNBbTm83xy+m6qk3kfmFF9ctlvfdPlKF1Motz2FACBtit2bIry8UcD+le0W66SJ99k /1UQ== X-Forwarded-Encrypted: i=1; AJvYcCW/gcNGbBQU8BeyRBCIFyIqgCOXS/i/njIjY2DGPeUoyQY9uVnbnWLvUHHUmgGgx2pOXtzRWU8Le7pxEDI=@vger.kernel.org X-Gm-Message-State: AOJu0YzEiWq7s9ehhbjKh3eH/FmdVGs+ZoJ0wuvDxkC8TyGsphfqbpGC eRxt35pSrq8+KBWtcpEjKuITJ3PEp2yWNJv1poz5LaHybfGNyu9KDEsI4DwGzs4NsTknFAkAIre PTXlswUIFlXWx0JkopsedXLowNA== X-Received: from plqu12.prod.google.com ([2002:a17:902:a60c:b0:2a0:7f81:6066]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:166e:b0:2a0:e223:f6e6 with SMTP id d9443c01a7336-2a8d8177fc0mr121846735ad.46.1770071432436; Mon, 02 Feb 2026 14:30:32 -0800 (PST) Date: Mon, 2 Feb 2026 14:29:44 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.53.0.rc1.225.gd81095ad13-goog Message-ID: Subject: [RFC PATCH v2 06/37] KVM: guest_memfd: Update kvm_gmem_populate() to use gmem attributes From: Ackerley Tng To: kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Cc: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, bp@alien8.de, brauner@kernel.org, chao.p.peng@intel.com, chao.p.peng@linux.intel.com, chenhuacai@kernel.org, corbet@lwn.net, dave.hansen@linux.intel.com, david@kernel.org, hpa@zytor.com, ira.weiny@intel.com, jgg@nvidia.com, jmattson@google.com, jroedel@suse.de, jthoughton@google.com, maobibo@loongson.cn, mathieu.desnoyers@efficios.com, maz@kernel.org, mhiramat@kernel.org, michael.roth@amd.com, mingo@redhat.com, mlevitsk@redhat.com, oupton@kernel.org, pankaj.gupta@amd.com, pbonzini@redhat.com, prsampat@amd.com, qperret@google.com, ricarkol@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, rostedt@goodmis.org, seanjc@google.com, shivankg@amd.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, Ackerley Tng Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Ackerley Tng Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 2 +- include/linux/kvm_host.h | 14 +++++++++++++- virt/kvm/guest_memfd.c | 26 +++++++++++++++++++++++--- virt/kvm/kvm_main.c | 8 +++----- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c7de8ff84fd2..25ab6b8901e2 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -7979,7 +7979,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 c12ee89392d8..8f1e10a503f4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2536,12 +2536,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 810fec394075..6b3477b226ad 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -934,10 +934,30 @@ 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; +} + long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src,= long npages, kvm_gmem_populate_cb post_populate, void *opaque) { struct kvm_memory_slot *slot; + struct gmem_inode *gi; void __user *p; =20 int ret =3D 0, max_order; @@ -956,6 +976,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn= , void __user *src, long if (!file) return -EFAULT; =20 + gi =3D GMEM_I(file_inode(file)); + filemap_invalidate_lock(file->f_mapping); =20 npages =3D min_t(ulong, slot->npages - (start_gfn - slot->base_gfn), npag= es); @@ -989,9 +1011,7 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gf= n, void __user *src, long (npages - i) < (1 << max_order)); =20 ret =3D -EINVAL; - while (!kvm_range_has_memory_attributes(kvm, gfn, gfn + (1 << max_order), - KVM_MEMORY_ATTRIBUTE_PRIVATE, - KVM_MEMORY_ATTRIBUTE_PRIVATE)) { + while (!kvm_gmem_range_is_private(gi, index, 1 << max_order, kvm, gfn)) { if (!max_order) goto put_folio_and_exit; max_order--; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 5d06e2c74ae7..666d1f7fbf07 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= )); @@ -2473,7 +2471,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); @@ -2607,7 +2605,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.rc1.225.gd81095ad13-goog